sii902x.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2018 Renesas Electronics
  4. *
  5. * Copyright (C) 2016 Atmel
  6. * Bo Shen <voice.shen@atmel.com>
  7. *
  8. * Authors: Bo Shen <voice.shen@atmel.com>
  9. * Boris Brezillon <boris.brezillon@free-electrons.com>
  10. * Wu, Songjun <Songjun.Wu@atmel.com>
  11. *
  12. * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
  13. */
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/i2c-mux.h>
  16. #include <linux/i2c.h>
  17. #include <linux/module.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/clk.h>
  21. #include <drm/drm_atomic_helper.h>
  22. #include <drm/drm_bridge.h>
  23. #include <drm/drm_drv.h>
  24. #include <drm/drm_edid.h>
  25. #include <drm/drm_print.h>
  26. #include <drm/drm_probe_helper.h>
  27. #include <sound/hdmi-codec.h>
  28. #define SII902X_TPI_VIDEO_DATA 0x0
  29. #define SII902X_TPI_PIXEL_REPETITION 0x8
  30. #define SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT BIT(5)
  31. #define SII902X_TPI_AVI_PIXEL_REP_RISING_EDGE BIT(4)
  32. #define SII902X_TPI_AVI_PIXEL_REP_4X 3
  33. #define SII902X_TPI_AVI_PIXEL_REP_2X 1
  34. #define SII902X_TPI_AVI_PIXEL_REP_NONE 0
  35. #define SII902X_TPI_CLK_RATIO_HALF (0 << 6)
  36. #define SII902X_TPI_CLK_RATIO_1X (1 << 6)
  37. #define SII902X_TPI_CLK_RATIO_2X (2 << 6)
  38. #define SII902X_TPI_CLK_RATIO_4X (3 << 6)
  39. #define SII902X_TPI_AVI_IN_FORMAT 0x9
  40. #define SII902X_TPI_AVI_INPUT_BITMODE_12BIT BIT(7)
  41. #define SII902X_TPI_AVI_INPUT_DITHER BIT(6)
  42. #define SII902X_TPI_AVI_INPUT_RANGE_LIMITED (2 << 2)
  43. #define SII902X_TPI_AVI_INPUT_RANGE_FULL (1 << 2)
  44. #define SII902X_TPI_AVI_INPUT_RANGE_AUTO (0 << 2)
  45. #define SII902X_TPI_AVI_INPUT_COLORSPACE_BLACK (3 << 0)
  46. #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV422 (2 << 0)
  47. #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV444 (1 << 0)
  48. #define SII902X_TPI_AVI_INPUT_COLORSPACE_RGB (0 << 0)
  49. #define SII902X_TPI_AVI_INFOFRAME 0x0c
  50. #define SII902X_SYS_CTRL_DATA 0x1a
  51. #define SII902X_SYS_CTRL_PWR_DWN BIT(4)
  52. #define SII902X_SYS_CTRL_AV_MUTE BIT(3)
  53. #define SII902X_SYS_CTRL_DDC_BUS_REQ BIT(2)
  54. #define SII902X_SYS_CTRL_DDC_BUS_GRTD BIT(1)
  55. #define SII902X_SYS_CTRL_OUTPUT_MODE BIT(0)
  56. #define SII902X_SYS_CTRL_OUTPUT_HDMI 1
  57. #define SII902X_SYS_CTRL_OUTPUT_DVI 0
  58. #define SII902X_REG_CHIPID(n) (0x1b + (n))
  59. #define SII902X_PWR_STATE_CTRL 0x1e
  60. #define SII902X_AVI_POWER_STATE_MSK GENMASK(1, 0)
  61. #define SII902X_AVI_POWER_STATE_D(l) ((l) & SII902X_AVI_POWER_STATE_MSK)
  62. /* Audio */
  63. #define SII902X_TPI_I2S_ENABLE_MAPPING_REG 0x1f
  64. #define SII902X_TPI_I2S_CONFIG_FIFO0 (0 << 0)
  65. #define SII902X_TPI_I2S_CONFIG_FIFO1 (1 << 0)
  66. #define SII902X_TPI_I2S_CONFIG_FIFO2 (2 << 0)
  67. #define SII902X_TPI_I2S_CONFIG_FIFO3 (3 << 0)
  68. #define SII902X_TPI_I2S_LEFT_RIGHT_SWAP (1 << 2)
  69. #define SII902X_TPI_I2S_AUTO_DOWNSAMPLE (1 << 3)
  70. #define SII902X_TPI_I2S_SELECT_SD0 (0 << 4)
  71. #define SII902X_TPI_I2S_SELECT_SD1 (1 << 4)
  72. #define SII902X_TPI_I2S_SELECT_SD2 (2 << 4)
  73. #define SII902X_TPI_I2S_SELECT_SD3 (3 << 4)
  74. #define SII902X_TPI_I2S_FIFO_ENABLE (1 << 7)
  75. #define SII902X_TPI_I2S_INPUT_CONFIG_REG 0x20
  76. #define SII902X_TPI_I2S_FIRST_BIT_SHIFT_YES (0 << 0)
  77. #define SII902X_TPI_I2S_FIRST_BIT_SHIFT_NO (1 << 0)
  78. #define SII902X_TPI_I2S_SD_DIRECTION_MSB_FIRST (0 << 1)
  79. #define SII902X_TPI_I2S_SD_DIRECTION_LSB_FIRST (1 << 1)
  80. #define SII902X_TPI_I2S_SD_JUSTIFY_LEFT (0 << 2)
  81. #define SII902X_TPI_I2S_SD_JUSTIFY_RIGHT (1 << 2)
  82. #define SII902X_TPI_I2S_WS_POLARITY_LOW (0 << 3)
  83. #define SII902X_TPI_I2S_WS_POLARITY_HIGH (1 << 3)
  84. #define SII902X_TPI_I2S_MCLK_MULTIPLIER_128 (0 << 4)
  85. #define SII902X_TPI_I2S_MCLK_MULTIPLIER_256 (1 << 4)
  86. #define SII902X_TPI_I2S_MCLK_MULTIPLIER_384 (2 << 4)
  87. #define SII902X_TPI_I2S_MCLK_MULTIPLIER_512 (3 << 4)
  88. #define SII902X_TPI_I2S_MCLK_MULTIPLIER_768 (4 << 4)
  89. #define SII902X_TPI_I2S_MCLK_MULTIPLIER_1024 (5 << 4)
  90. #define SII902X_TPI_I2S_MCLK_MULTIPLIER_1152 (6 << 4)
  91. #define SII902X_TPI_I2S_MCLK_MULTIPLIER_192 (7 << 4)
  92. #define SII902X_TPI_I2S_SCK_EDGE_FALLING (0 << 7)
  93. #define SII902X_TPI_I2S_SCK_EDGE_RISING (1 << 7)
  94. #define SII902X_TPI_I2S_STRM_HDR_BASE 0x21
  95. #define SII902X_TPI_I2S_STRM_HDR_SIZE 5
  96. #define SII902X_TPI_AUDIO_CONFIG_BYTE2_REG 0x26
  97. #define SII902X_TPI_AUDIO_CODING_STREAM_HEADER (0 << 0)
  98. #define SII902X_TPI_AUDIO_CODING_PCM (1 << 0)
  99. #define SII902X_TPI_AUDIO_CODING_AC3 (2 << 0)
  100. #define SII902X_TPI_AUDIO_CODING_MPEG1 (3 << 0)
  101. #define SII902X_TPI_AUDIO_CODING_MP3 (4 << 0)
  102. #define SII902X_TPI_AUDIO_CODING_MPEG2 (5 << 0)
  103. #define SII902X_TPI_AUDIO_CODING_AAC (6 << 0)
  104. #define SII902X_TPI_AUDIO_CODING_DTS (7 << 0)
  105. #define SII902X_TPI_AUDIO_CODING_ATRAC (8 << 0)
  106. #define SII902X_TPI_AUDIO_MUTE_DISABLE (0 << 4)
  107. #define SII902X_TPI_AUDIO_MUTE_ENABLE (1 << 4)
  108. #define SII902X_TPI_AUDIO_LAYOUT_2_CHANNELS (0 << 5)
  109. #define SII902X_TPI_AUDIO_LAYOUT_8_CHANNELS (1 << 5)
  110. #define SII902X_TPI_AUDIO_INTERFACE_DISABLE (0 << 6)
  111. #define SII902X_TPI_AUDIO_INTERFACE_SPDIF (1 << 6)
  112. #define SII902X_TPI_AUDIO_INTERFACE_I2S (2 << 6)
  113. #define SII902X_TPI_AUDIO_CONFIG_BYTE3_REG 0x27
  114. #define SII902X_TPI_AUDIO_FREQ_STREAM (0 << 3)
  115. #define SII902X_TPI_AUDIO_FREQ_32KHZ (1 << 3)
  116. #define SII902X_TPI_AUDIO_FREQ_44KHZ (2 << 3)
  117. #define SII902X_TPI_AUDIO_FREQ_48KHZ (3 << 3)
  118. #define SII902X_TPI_AUDIO_FREQ_88KHZ (4 << 3)
  119. #define SII902X_TPI_AUDIO_FREQ_96KHZ (5 << 3)
  120. #define SII902X_TPI_AUDIO_FREQ_176KHZ (6 << 3)
  121. #define SII902X_TPI_AUDIO_FREQ_192KHZ (7 << 3)
  122. #define SII902X_TPI_AUDIO_SAMPLE_SIZE_STREAM (0 << 6)
  123. #define SII902X_TPI_AUDIO_SAMPLE_SIZE_16 (1 << 6)
  124. #define SII902X_TPI_AUDIO_SAMPLE_SIZE_20 (2 << 6)
  125. #define SII902X_TPI_AUDIO_SAMPLE_SIZE_24 (3 << 6)
  126. #define SII902X_TPI_AUDIO_CONFIG_BYTE4_REG 0x28
  127. #define SII902X_INT_ENABLE 0x3c
  128. #define SII902X_INT_STATUS 0x3d
  129. #define SII902X_HOTPLUG_EVENT BIT(0)
  130. #define SII902X_PLUGGED_STATUS BIT(2)
  131. #define SII902X_REG_TPI_RQB 0xc7
  132. /* Indirect internal register access */
  133. #define SII902X_IND_SET_PAGE 0xbc
  134. #define SII902X_IND_OFFSET 0xbd
  135. #define SII902X_IND_VALUE 0xbe
  136. #define SII902X_TPI_MISC_INFOFRAME_BASE 0xbf
  137. #define SII902X_TPI_MISC_INFOFRAME_END 0xde
  138. #define SII902X_TPI_MISC_INFOFRAME_SIZE \
  139. (SII902X_TPI_MISC_INFOFRAME_END - SII902X_TPI_MISC_INFOFRAME_BASE)
  140. #define SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS 500
  141. #define SII902X_AUDIO_PORT_INDEX 3
  142. struct sii902x {
  143. struct i2c_client *i2c;
  144. struct regmap *regmap;
  145. struct drm_bridge bridge;
  146. struct drm_connector connector;
  147. struct gpio_desc *reset_gpio;
  148. struct i2c_mux_core *i2cmux;
  149. struct regulator_bulk_data supplies[2];
  150. /*
  151. * Mutex protects audio and video functions from interfering
  152. * each other, by keeping their i2c command sequences atomic.
  153. */
  154. struct mutex mutex;
  155. struct sii902x_audio {
  156. struct platform_device *pdev;
  157. struct clk *mclk;
  158. u32 i2s_fifo_sequence[4];
  159. } audio;
  160. };
  161. static int sii902x_read_unlocked(struct i2c_client *i2c, u8 reg, u8 *val)
  162. {
  163. union i2c_smbus_data data;
  164. int ret;
  165. ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
  166. I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data);
  167. if (ret < 0)
  168. return ret;
  169. *val = data.byte;
  170. return 0;
  171. }
  172. static int sii902x_write_unlocked(struct i2c_client *i2c, u8 reg, u8 val)
  173. {
  174. union i2c_smbus_data data;
  175. data.byte = val;
  176. return __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
  177. I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA,
  178. &data);
  179. }
  180. static int sii902x_update_bits_unlocked(struct i2c_client *i2c, u8 reg, u8 mask,
  181. u8 val)
  182. {
  183. int ret;
  184. u8 status;
  185. ret = sii902x_read_unlocked(i2c, reg, &status);
  186. if (ret)
  187. return ret;
  188. status &= ~mask;
  189. status |= val & mask;
  190. return sii902x_write_unlocked(i2c, reg, status);
  191. }
  192. static inline struct sii902x *bridge_to_sii902x(struct drm_bridge *bridge)
  193. {
  194. return container_of(bridge, struct sii902x, bridge);
  195. }
  196. static inline struct sii902x *connector_to_sii902x(struct drm_connector *con)
  197. {
  198. return container_of(con, struct sii902x, connector);
  199. }
  200. static void sii902x_reset(struct sii902x *sii902x)
  201. {
  202. if (!sii902x->reset_gpio)
  203. return;
  204. gpiod_set_value(sii902x->reset_gpio, 1);
  205. /* The datasheet says treset-min = 100us. Make it 150us to be sure. */
  206. usleep_range(150, 200);
  207. gpiod_set_value(sii902x->reset_gpio, 0);
  208. }
  209. static enum drm_connector_status
  210. sii902x_connector_detect(struct drm_connector *connector, bool force)
  211. {
  212. struct sii902x *sii902x = connector_to_sii902x(connector);
  213. unsigned int status;
  214. mutex_lock(&sii902x->mutex);
  215. regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status);
  216. mutex_unlock(&sii902x->mutex);
  217. return (status & SII902X_PLUGGED_STATUS) ?
  218. connector_status_connected : connector_status_disconnected;
  219. }
  220. static const struct drm_connector_funcs sii902x_connector_funcs = {
  221. .detect = sii902x_connector_detect,
  222. .fill_modes = drm_helper_probe_single_connector_modes,
  223. .destroy = drm_connector_cleanup,
  224. .reset = drm_atomic_helper_connector_reset,
  225. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  226. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  227. };
  228. static int sii902x_get_modes(struct drm_connector *connector)
  229. {
  230. struct sii902x *sii902x = connector_to_sii902x(connector);
  231. u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  232. u8 output_mode = SII902X_SYS_CTRL_OUTPUT_DVI;
  233. struct edid *edid;
  234. int num = 0, ret;
  235. mutex_lock(&sii902x->mutex);
  236. edid = drm_get_edid(connector, sii902x->i2cmux->adapter[0]);
  237. drm_connector_update_edid_property(connector, edid);
  238. if (edid) {
  239. if (drm_detect_hdmi_monitor(edid))
  240. output_mode = SII902X_SYS_CTRL_OUTPUT_HDMI;
  241. num = drm_add_edid_modes(connector, edid);
  242. kfree(edid);
  243. }
  244. ret = drm_display_info_set_bus_formats(&connector->display_info,
  245. &bus_format, 1);
  246. if (ret)
  247. goto error_out;
  248. ret = regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA,
  249. SII902X_SYS_CTRL_OUTPUT_MODE, output_mode);
  250. if (ret)
  251. goto error_out;
  252. ret = num;
  253. error_out:
  254. mutex_unlock(&sii902x->mutex);
  255. return ret;
  256. }
  257. static enum drm_mode_status sii902x_mode_valid(struct drm_connector *connector,
  258. struct drm_display_mode *mode)
  259. {
  260. /* TODO: check mode */
  261. return MODE_OK;
  262. }
  263. static const struct drm_connector_helper_funcs sii902x_connector_helper_funcs = {
  264. .get_modes = sii902x_get_modes,
  265. .mode_valid = sii902x_mode_valid,
  266. };
  267. static void sii902x_bridge_disable(struct drm_bridge *bridge)
  268. {
  269. struct sii902x *sii902x = bridge_to_sii902x(bridge);
  270. mutex_lock(&sii902x->mutex);
  271. regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA,
  272. SII902X_SYS_CTRL_PWR_DWN,
  273. SII902X_SYS_CTRL_PWR_DWN);
  274. mutex_unlock(&sii902x->mutex);
  275. }
  276. static void sii902x_bridge_enable(struct drm_bridge *bridge)
  277. {
  278. struct sii902x *sii902x = bridge_to_sii902x(bridge);
  279. mutex_lock(&sii902x->mutex);
  280. regmap_update_bits(sii902x->regmap, SII902X_PWR_STATE_CTRL,
  281. SII902X_AVI_POWER_STATE_MSK,
  282. SII902X_AVI_POWER_STATE_D(0));
  283. regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA,
  284. SII902X_SYS_CTRL_PWR_DWN, 0);
  285. mutex_unlock(&sii902x->mutex);
  286. }
  287. static void sii902x_bridge_mode_set(struct drm_bridge *bridge,
  288. const struct drm_display_mode *mode,
  289. const struct drm_display_mode *adj)
  290. {
  291. struct sii902x *sii902x = bridge_to_sii902x(bridge);
  292. struct regmap *regmap = sii902x->regmap;
  293. u8 buf[HDMI_INFOFRAME_SIZE(AVI)];
  294. struct hdmi_avi_infoframe frame;
  295. u16 pixel_clock_10kHz = adj->clock / 10;
  296. int ret;
  297. buf[0] = pixel_clock_10kHz & 0xff;
  298. buf[1] = pixel_clock_10kHz >> 8;
  299. buf[2] = drm_mode_vrefresh(adj);
  300. buf[3] = 0x00;
  301. buf[4] = adj->hdisplay;
  302. buf[5] = adj->hdisplay >> 8;
  303. buf[6] = adj->vdisplay;
  304. buf[7] = adj->vdisplay >> 8;
  305. buf[8] = SII902X_TPI_CLK_RATIO_1X | SII902X_TPI_AVI_PIXEL_REP_NONE |
  306. SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT;
  307. buf[9] = SII902X_TPI_AVI_INPUT_RANGE_AUTO |
  308. SII902X_TPI_AVI_INPUT_COLORSPACE_RGB;
  309. mutex_lock(&sii902x->mutex);
  310. ret = regmap_bulk_write(regmap, SII902X_TPI_VIDEO_DATA, buf, 10);
  311. if (ret)
  312. goto out;
  313. ret = drm_hdmi_avi_infoframe_from_display_mode(&frame,
  314. &sii902x->connector, adj);
  315. if (ret < 0) {
  316. DRM_ERROR("couldn't fill AVI infoframe\n");
  317. goto out;
  318. }
  319. ret = hdmi_avi_infoframe_pack(&frame, buf, sizeof(buf));
  320. if (ret < 0) {
  321. DRM_ERROR("failed to pack AVI infoframe: %d\n", ret);
  322. goto out;
  323. }
  324. /* Do not send the infoframe header, but keep the CRC field. */
  325. regmap_bulk_write(regmap, SII902X_TPI_AVI_INFOFRAME,
  326. buf + HDMI_INFOFRAME_HEADER_SIZE - 1,
  327. HDMI_AVI_INFOFRAME_SIZE + 1);
  328. out:
  329. mutex_unlock(&sii902x->mutex);
  330. }
  331. static int sii902x_bridge_attach(struct drm_bridge *bridge,
  332. enum drm_bridge_attach_flags flags)
  333. {
  334. struct sii902x *sii902x = bridge_to_sii902x(bridge);
  335. struct drm_device *drm = bridge->dev;
  336. int ret;
  337. if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR) {
  338. DRM_ERROR("Fix bridge driver to make connector optional!");
  339. return -EINVAL;
  340. }
  341. drm_connector_helper_add(&sii902x->connector,
  342. &sii902x_connector_helper_funcs);
  343. if (!drm_core_check_feature(drm, DRIVER_ATOMIC)) {
  344. dev_err(&sii902x->i2c->dev,
  345. "sii902x driver is only compatible with DRM devices supporting atomic updates\n");
  346. return -ENOTSUPP;
  347. }
  348. ret = drm_connector_init(drm, &sii902x->connector,
  349. &sii902x_connector_funcs,
  350. DRM_MODE_CONNECTOR_HDMIA);
  351. if (ret)
  352. return ret;
  353. if (sii902x->i2c->irq > 0)
  354. sii902x->connector.polled = DRM_CONNECTOR_POLL_HPD;
  355. else
  356. sii902x->connector.polled = DRM_CONNECTOR_POLL_CONNECT;
  357. drm_connector_attach_encoder(&sii902x->connector, bridge->encoder);
  358. return 0;
  359. }
  360. static const struct drm_bridge_funcs sii902x_bridge_funcs = {
  361. .attach = sii902x_bridge_attach,
  362. .mode_set = sii902x_bridge_mode_set,
  363. .disable = sii902x_bridge_disable,
  364. .enable = sii902x_bridge_enable,
  365. };
  366. static int sii902x_mute(struct sii902x *sii902x, bool mute)
  367. {
  368. struct device *dev = &sii902x->i2c->dev;
  369. unsigned int val = mute ? SII902X_TPI_AUDIO_MUTE_ENABLE :
  370. SII902X_TPI_AUDIO_MUTE_DISABLE;
  371. dev_dbg(dev, "%s: %s\n", __func__, mute ? "Muted" : "Unmuted");
  372. return regmap_update_bits(sii902x->regmap,
  373. SII902X_TPI_AUDIO_CONFIG_BYTE2_REG,
  374. SII902X_TPI_AUDIO_MUTE_ENABLE, val);
  375. }
  376. static const int sii902x_mclk_div_table[] = {
  377. 128, 256, 384, 512, 768, 1024, 1152, 192 };
  378. static int sii902x_select_mclk_div(u8 *i2s_config_reg, unsigned int rate,
  379. unsigned int mclk)
  380. {
  381. int div = mclk / rate;
  382. int distance = 100000;
  383. u8 i, nearest = 0;
  384. for (i = 0; i < ARRAY_SIZE(sii902x_mclk_div_table); i++) {
  385. unsigned int d = abs(div - sii902x_mclk_div_table[i]);
  386. if (d >= distance)
  387. continue;
  388. nearest = i;
  389. distance = d;
  390. if (d == 0)
  391. break;
  392. }
  393. *i2s_config_reg |= nearest << 4;
  394. return sii902x_mclk_div_table[nearest];
  395. }
  396. static const struct sii902x_sample_freq {
  397. u32 freq;
  398. u8 val;
  399. } sii902x_sample_freq[] = {
  400. { .freq = 32000, .val = SII902X_TPI_AUDIO_FREQ_32KHZ },
  401. { .freq = 44000, .val = SII902X_TPI_AUDIO_FREQ_44KHZ },
  402. { .freq = 48000, .val = SII902X_TPI_AUDIO_FREQ_48KHZ },
  403. { .freq = 88000, .val = SII902X_TPI_AUDIO_FREQ_88KHZ },
  404. { .freq = 96000, .val = SII902X_TPI_AUDIO_FREQ_96KHZ },
  405. { .freq = 176000, .val = SII902X_TPI_AUDIO_FREQ_176KHZ },
  406. { .freq = 192000, .val = SII902X_TPI_AUDIO_FREQ_192KHZ },
  407. };
  408. static int sii902x_audio_hw_params(struct device *dev, void *data,
  409. struct hdmi_codec_daifmt *daifmt,
  410. struct hdmi_codec_params *params)
  411. {
  412. struct sii902x *sii902x = dev_get_drvdata(dev);
  413. u8 i2s_config_reg = SII902X_TPI_I2S_SD_DIRECTION_MSB_FIRST;
  414. u8 config_byte2_reg = (SII902X_TPI_AUDIO_INTERFACE_I2S |
  415. SII902X_TPI_AUDIO_MUTE_ENABLE |
  416. SII902X_TPI_AUDIO_CODING_PCM);
  417. u8 config_byte3_reg = 0;
  418. u8 infoframe_buf[HDMI_INFOFRAME_SIZE(AUDIO)];
  419. unsigned long mclk_rate;
  420. int i, ret;
  421. if (daifmt->bit_clk_master || daifmt->frame_clk_master) {
  422. dev_dbg(dev, "%s: I2S master mode not supported\n", __func__);
  423. return -EINVAL;
  424. }
  425. switch (daifmt->fmt) {
  426. case HDMI_I2S:
  427. i2s_config_reg |= SII902X_TPI_I2S_FIRST_BIT_SHIFT_YES |
  428. SII902X_TPI_I2S_SD_JUSTIFY_LEFT;
  429. break;
  430. case HDMI_RIGHT_J:
  431. i2s_config_reg |= SII902X_TPI_I2S_SD_JUSTIFY_RIGHT;
  432. break;
  433. case HDMI_LEFT_J:
  434. i2s_config_reg |= SII902X_TPI_I2S_SD_JUSTIFY_LEFT;
  435. break;
  436. default:
  437. dev_dbg(dev, "%s: Unsupported i2s format %u\n", __func__,
  438. daifmt->fmt);
  439. return -EINVAL;
  440. }
  441. if (daifmt->bit_clk_inv)
  442. i2s_config_reg |= SII902X_TPI_I2S_SCK_EDGE_FALLING;
  443. else
  444. i2s_config_reg |= SII902X_TPI_I2S_SCK_EDGE_RISING;
  445. if (daifmt->frame_clk_inv)
  446. i2s_config_reg |= SII902X_TPI_I2S_WS_POLARITY_LOW;
  447. else
  448. i2s_config_reg |= SII902X_TPI_I2S_WS_POLARITY_HIGH;
  449. if (params->channels > 2)
  450. config_byte2_reg |= SII902X_TPI_AUDIO_LAYOUT_8_CHANNELS;
  451. else
  452. config_byte2_reg |= SII902X_TPI_AUDIO_LAYOUT_2_CHANNELS;
  453. switch (params->sample_width) {
  454. case 16:
  455. config_byte3_reg |= SII902X_TPI_AUDIO_SAMPLE_SIZE_16;
  456. break;
  457. case 20:
  458. config_byte3_reg |= SII902X_TPI_AUDIO_SAMPLE_SIZE_20;
  459. break;
  460. case 24:
  461. case 32:
  462. config_byte3_reg |= SII902X_TPI_AUDIO_SAMPLE_SIZE_24;
  463. break;
  464. default:
  465. dev_err(dev, "%s: Unsupported sample width %u\n", __func__,
  466. params->sample_width);
  467. return -EINVAL;
  468. }
  469. for (i = 0; i < ARRAY_SIZE(sii902x_sample_freq); i++) {
  470. if (params->sample_rate == sii902x_sample_freq[i].freq) {
  471. config_byte3_reg |= sii902x_sample_freq[i].val;
  472. break;
  473. }
  474. }
  475. ret = clk_prepare_enable(sii902x->audio.mclk);
  476. if (ret) {
  477. dev_err(dev, "Enabling mclk failed: %d\n", ret);
  478. return ret;
  479. }
  480. if (sii902x->audio.mclk) {
  481. mclk_rate = clk_get_rate(sii902x->audio.mclk);
  482. ret = sii902x_select_mclk_div(&i2s_config_reg,
  483. params->sample_rate, mclk_rate);
  484. if (mclk_rate != ret * params->sample_rate)
  485. dev_dbg(dev, "Inaccurate reference clock (%ld/%d != %u)\n",
  486. mclk_rate, ret, params->sample_rate);
  487. }
  488. mutex_lock(&sii902x->mutex);
  489. ret = regmap_write(sii902x->regmap,
  490. SII902X_TPI_AUDIO_CONFIG_BYTE2_REG,
  491. config_byte2_reg);
  492. if (ret < 0)
  493. goto out;
  494. ret = regmap_write(sii902x->regmap, SII902X_TPI_I2S_INPUT_CONFIG_REG,
  495. i2s_config_reg);
  496. if (ret)
  497. goto out;
  498. for (i = 0; i < ARRAY_SIZE(sii902x->audio.i2s_fifo_sequence) &&
  499. sii902x->audio.i2s_fifo_sequence[i]; i++)
  500. regmap_write(sii902x->regmap,
  501. SII902X_TPI_I2S_ENABLE_MAPPING_REG,
  502. sii902x->audio.i2s_fifo_sequence[i]);
  503. ret = regmap_write(sii902x->regmap, SII902X_TPI_AUDIO_CONFIG_BYTE3_REG,
  504. config_byte3_reg);
  505. if (ret)
  506. goto out;
  507. ret = regmap_bulk_write(sii902x->regmap, SII902X_TPI_I2S_STRM_HDR_BASE,
  508. params->iec.status,
  509. min((size_t) SII902X_TPI_I2S_STRM_HDR_SIZE,
  510. sizeof(params->iec.status)));
  511. if (ret)
  512. goto out;
  513. ret = hdmi_audio_infoframe_pack(&params->cea, infoframe_buf,
  514. sizeof(infoframe_buf));
  515. if (ret < 0) {
  516. dev_err(dev, "%s: Failed to pack audio infoframe: %d\n",
  517. __func__, ret);
  518. goto out;
  519. }
  520. ret = regmap_bulk_write(sii902x->regmap,
  521. SII902X_TPI_MISC_INFOFRAME_BASE,
  522. infoframe_buf,
  523. min(ret, SII902X_TPI_MISC_INFOFRAME_SIZE));
  524. if (ret)
  525. goto out;
  526. /* Decode Level 0 Packets */
  527. ret = regmap_write(sii902x->regmap, SII902X_IND_SET_PAGE, 0x02);
  528. if (ret)
  529. goto out;
  530. ret = regmap_write(sii902x->regmap, SII902X_IND_OFFSET, 0x24);
  531. if (ret)
  532. goto out;
  533. ret = regmap_write(sii902x->regmap, SII902X_IND_VALUE, 0x02);
  534. if (ret)
  535. goto out;
  536. dev_dbg(dev, "%s: hdmi audio enabled\n", __func__);
  537. out:
  538. mutex_unlock(&sii902x->mutex);
  539. if (ret) {
  540. clk_disable_unprepare(sii902x->audio.mclk);
  541. dev_err(dev, "%s: hdmi audio enable failed: %d\n", __func__,
  542. ret);
  543. }
  544. return ret;
  545. }
  546. static void sii902x_audio_shutdown(struct device *dev, void *data)
  547. {
  548. struct sii902x *sii902x = dev_get_drvdata(dev);
  549. mutex_lock(&sii902x->mutex);
  550. regmap_write(sii902x->regmap, SII902X_TPI_AUDIO_CONFIG_BYTE2_REG,
  551. SII902X_TPI_AUDIO_INTERFACE_DISABLE);
  552. mutex_unlock(&sii902x->mutex);
  553. clk_disable_unprepare(sii902x->audio.mclk);
  554. }
  555. static int sii902x_audio_mute(struct device *dev, void *data,
  556. bool enable, int direction)
  557. {
  558. struct sii902x *sii902x = dev_get_drvdata(dev);
  559. mutex_lock(&sii902x->mutex);
  560. sii902x_mute(sii902x, enable);
  561. mutex_unlock(&sii902x->mutex);
  562. return 0;
  563. }
  564. static int sii902x_audio_get_eld(struct device *dev, void *data,
  565. uint8_t *buf, size_t len)
  566. {
  567. struct sii902x *sii902x = dev_get_drvdata(dev);
  568. mutex_lock(&sii902x->mutex);
  569. memcpy(buf, sii902x->connector.eld,
  570. min(sizeof(sii902x->connector.eld), len));
  571. mutex_unlock(&sii902x->mutex);
  572. return 0;
  573. }
  574. static int sii902x_audio_get_dai_id(struct snd_soc_component *component,
  575. struct device_node *endpoint)
  576. {
  577. struct of_endpoint of_ep;
  578. int ret;
  579. ret = of_graph_parse_endpoint(endpoint, &of_ep);
  580. if (ret < 0)
  581. return ret;
  582. /*
  583. * HDMI sound should be located at reg = <3>
  584. * Return expected DAI index 0.
  585. */
  586. if (of_ep.port == SII902X_AUDIO_PORT_INDEX)
  587. return 0;
  588. return -EINVAL;
  589. }
  590. static const struct hdmi_codec_ops sii902x_audio_codec_ops = {
  591. .hw_params = sii902x_audio_hw_params,
  592. .audio_shutdown = sii902x_audio_shutdown,
  593. .mute_stream = sii902x_audio_mute,
  594. .get_eld = sii902x_audio_get_eld,
  595. .get_dai_id = sii902x_audio_get_dai_id,
  596. .no_capture_mute = 1,
  597. };
  598. static int sii902x_audio_codec_init(struct sii902x *sii902x,
  599. struct device *dev)
  600. {
  601. static const u8 audio_fifo_id[] = {
  602. SII902X_TPI_I2S_CONFIG_FIFO0,
  603. SII902X_TPI_I2S_CONFIG_FIFO1,
  604. SII902X_TPI_I2S_CONFIG_FIFO2,
  605. SII902X_TPI_I2S_CONFIG_FIFO3,
  606. };
  607. static const u8 i2s_lane_id[] = {
  608. SII902X_TPI_I2S_SELECT_SD0,
  609. SII902X_TPI_I2S_SELECT_SD1,
  610. SII902X_TPI_I2S_SELECT_SD2,
  611. SII902X_TPI_I2S_SELECT_SD3,
  612. };
  613. struct hdmi_codec_pdata codec_data = {
  614. .ops = &sii902x_audio_codec_ops,
  615. .i2s = 1, /* Only i2s support for now. */
  616. .spdif = 0,
  617. .max_i2s_channels = 0,
  618. };
  619. u8 lanes[4];
  620. int num_lanes, i;
  621. if (!of_property_read_bool(dev->of_node, "#sound-dai-cells")) {
  622. dev_dbg(dev, "%s: No \"#sound-dai-cells\", no audio\n",
  623. __func__);
  624. return 0;
  625. }
  626. num_lanes = of_property_read_variable_u8_array(dev->of_node,
  627. "sil,i2s-data-lanes",
  628. lanes, 1,
  629. ARRAY_SIZE(lanes));
  630. if (num_lanes == -EINVAL) {
  631. dev_dbg(dev,
  632. "%s: No \"sil,i2s-data-lanes\", use default <0>\n",
  633. __func__);
  634. num_lanes = 1;
  635. lanes[0] = 0;
  636. } else if (num_lanes < 0) {
  637. dev_err(dev,
  638. "%s: Error gettin \"sil,i2s-data-lanes\": %d\n",
  639. __func__, num_lanes);
  640. return num_lanes;
  641. }
  642. codec_data.max_i2s_channels = 2 * num_lanes;
  643. for (i = 0; i < num_lanes; i++)
  644. sii902x->audio.i2s_fifo_sequence[i] |= audio_fifo_id[i] |
  645. i2s_lane_id[lanes[i]] | SII902X_TPI_I2S_FIFO_ENABLE;
  646. sii902x->audio.mclk = devm_clk_get_optional(dev, "mclk");
  647. if (IS_ERR(sii902x->audio.mclk)) {
  648. dev_err(dev, "%s: No clock (audio mclk) found: %ld\n",
  649. __func__, PTR_ERR(sii902x->audio.mclk));
  650. return PTR_ERR(sii902x->audio.mclk);
  651. }
  652. sii902x->audio.pdev = platform_device_register_data(
  653. dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
  654. &codec_data, sizeof(codec_data));
  655. return PTR_ERR_OR_ZERO(sii902x->audio.pdev);
  656. }
  657. static const struct regmap_range sii902x_volatile_ranges[] = {
  658. { .range_min = 0, .range_max = 0xff },
  659. };
  660. static const struct regmap_access_table sii902x_volatile_table = {
  661. .yes_ranges = sii902x_volatile_ranges,
  662. .n_yes_ranges = ARRAY_SIZE(sii902x_volatile_ranges),
  663. };
  664. static const struct regmap_config sii902x_regmap_config = {
  665. .reg_bits = 8,
  666. .val_bits = 8,
  667. .disable_locking = true, /* struct sii902x mutex should be enough */
  668. .max_register = SII902X_TPI_MISC_INFOFRAME_END,
  669. .volatile_table = &sii902x_volatile_table,
  670. .cache_type = REGCACHE_NONE,
  671. };
  672. static irqreturn_t sii902x_interrupt(int irq, void *data)
  673. {
  674. struct sii902x *sii902x = data;
  675. unsigned int status = 0;
  676. mutex_lock(&sii902x->mutex);
  677. regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status);
  678. regmap_write(sii902x->regmap, SII902X_INT_STATUS, status);
  679. mutex_unlock(&sii902x->mutex);
  680. if ((status & SII902X_HOTPLUG_EVENT) && sii902x->bridge.dev)
  681. drm_helper_hpd_irq_event(sii902x->bridge.dev);
  682. return IRQ_HANDLED;
  683. }
  684. /*
  685. * The purpose of sii902x_i2c_bypass_select is to enable the pass through
  686. * mode of the HDMI transmitter. Do not use regmap from within this function,
  687. * only use sii902x_*_unlocked functions to read/modify/write registers.
  688. * We are holding the parent adapter lock here, keep this in mind before
  689. * adding more i2c transactions.
  690. *
  691. * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere
  692. * in this driver, we need to make sure that we only touch 0x1A[2:1] from
  693. * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that
  694. * we leave the remaining bits as we have found them.
  695. */
  696. static int sii902x_i2c_bypass_select(struct i2c_mux_core *mux, u32 chan_id)
  697. {
  698. struct sii902x *sii902x = i2c_mux_priv(mux);
  699. struct device *dev = &sii902x->i2c->dev;
  700. unsigned long timeout;
  701. u8 status;
  702. int ret;
  703. ret = sii902x_update_bits_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
  704. SII902X_SYS_CTRL_DDC_BUS_REQ,
  705. SII902X_SYS_CTRL_DDC_BUS_REQ);
  706. if (ret)
  707. return ret;
  708. timeout = jiffies +
  709. msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS);
  710. do {
  711. ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
  712. &status);
  713. if (ret)
  714. return ret;
  715. } while (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD) &&
  716. time_before(jiffies, timeout));
  717. if (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD)) {
  718. dev_err(dev, "Failed to acquire the i2c bus\n");
  719. return -ETIMEDOUT;
  720. }
  721. return sii902x_write_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
  722. status);
  723. }
  724. /*
  725. * The purpose of sii902x_i2c_bypass_deselect is to disable the pass through
  726. * mode of the HDMI transmitter. Do not use regmap from within this function,
  727. * only use sii902x_*_unlocked functions to read/modify/write registers.
  728. * We are holding the parent adapter lock here, keep this in mind before
  729. * adding more i2c transactions.
  730. *
  731. * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere
  732. * in this driver, we need to make sure that we only touch 0x1A[2:1] from
  733. * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that
  734. * we leave the remaining bits as we have found them.
  735. */
  736. static int sii902x_i2c_bypass_deselect(struct i2c_mux_core *mux, u32 chan_id)
  737. {
  738. struct sii902x *sii902x = i2c_mux_priv(mux);
  739. struct device *dev = &sii902x->i2c->dev;
  740. unsigned long timeout;
  741. unsigned int retries;
  742. u8 status;
  743. int ret;
  744. /*
  745. * When the HDMI transmitter is in pass through mode, we need an
  746. * (undocumented) additional delay between STOP and START conditions
  747. * to guarantee the bus won't get stuck.
  748. */
  749. udelay(30);
  750. /*
  751. * Sometimes the I2C bus can stall after failure to use the
  752. * EDID channel. Retry a few times to see if things clear
  753. * up, else continue anyway.
  754. */
  755. retries = 5;
  756. do {
  757. ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
  758. &status);
  759. retries--;
  760. } while (ret && retries);
  761. if (ret) {
  762. dev_err(dev, "failed to read status (%d)\n", ret);
  763. return ret;
  764. }
  765. ret = sii902x_update_bits_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
  766. SII902X_SYS_CTRL_DDC_BUS_REQ |
  767. SII902X_SYS_CTRL_DDC_BUS_GRTD, 0);
  768. if (ret)
  769. return ret;
  770. timeout = jiffies +
  771. msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS);
  772. do {
  773. ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
  774. &status);
  775. if (ret)
  776. return ret;
  777. } while (status & (SII902X_SYS_CTRL_DDC_BUS_REQ |
  778. SII902X_SYS_CTRL_DDC_BUS_GRTD) &&
  779. time_before(jiffies, timeout));
  780. if (status & (SII902X_SYS_CTRL_DDC_BUS_REQ |
  781. SII902X_SYS_CTRL_DDC_BUS_GRTD)) {
  782. dev_err(dev, "failed to release the i2c bus\n");
  783. return -ETIMEDOUT;
  784. }
  785. return 0;
  786. }
  787. static const struct drm_bridge_timings default_sii902x_timings = {
  788. .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
  789. | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE
  790. | DRM_BUS_FLAG_DE_HIGH,
  791. };
  792. static int sii902x_init(struct sii902x *sii902x)
  793. {
  794. struct device *dev = &sii902x->i2c->dev;
  795. unsigned int status = 0;
  796. u8 chipid[4];
  797. int ret;
  798. sii902x_reset(sii902x);
  799. ret = regmap_write(sii902x->regmap, SII902X_REG_TPI_RQB, 0x0);
  800. if (ret)
  801. return ret;
  802. ret = regmap_bulk_read(sii902x->regmap, SII902X_REG_CHIPID(0),
  803. &chipid, 4);
  804. if (ret) {
  805. dev_err(dev, "regmap_read failed %d\n", ret);
  806. return ret;
  807. }
  808. if (chipid[0] != 0xb0) {
  809. dev_err(dev, "Invalid chipid: %02x (expecting 0xb0)\n",
  810. chipid[0]);
  811. return -EINVAL;
  812. }
  813. /* Clear all pending interrupts */
  814. regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status);
  815. regmap_write(sii902x->regmap, SII902X_INT_STATUS, status);
  816. if (sii902x->i2c->irq > 0) {
  817. regmap_write(sii902x->regmap, SII902X_INT_ENABLE,
  818. SII902X_HOTPLUG_EVENT);
  819. ret = devm_request_threaded_irq(dev, sii902x->i2c->irq, NULL,
  820. sii902x_interrupt,
  821. IRQF_ONESHOT, dev_name(dev),
  822. sii902x);
  823. if (ret)
  824. return ret;
  825. }
  826. sii902x->bridge.funcs = &sii902x_bridge_funcs;
  827. sii902x->bridge.of_node = dev->of_node;
  828. sii902x->bridge.timings = &default_sii902x_timings;
  829. drm_bridge_add(&sii902x->bridge);
  830. sii902x_audio_codec_init(sii902x, dev);
  831. i2c_set_clientdata(sii902x->i2c, sii902x);
  832. sii902x->i2cmux = i2c_mux_alloc(sii902x->i2c->adapter, dev,
  833. 1, 0, I2C_MUX_GATE,
  834. sii902x_i2c_bypass_select,
  835. sii902x_i2c_bypass_deselect);
  836. if (!sii902x->i2cmux)
  837. return -ENOMEM;
  838. sii902x->i2cmux->priv = sii902x;
  839. return i2c_mux_add_adapter(sii902x->i2cmux, 0, 0, 0);
  840. }
  841. static int sii902x_probe(struct i2c_client *client,
  842. const struct i2c_device_id *id)
  843. {
  844. struct device *dev = &client->dev;
  845. struct sii902x *sii902x;
  846. int ret;
  847. ret = i2c_check_functionality(client->adapter,
  848. I2C_FUNC_SMBUS_BYTE_DATA);
  849. if (!ret) {
  850. dev_err(dev, "I2C adapter not suitable\n");
  851. return -EIO;
  852. }
  853. sii902x = devm_kzalloc(dev, sizeof(*sii902x), GFP_KERNEL);
  854. if (!sii902x)
  855. return -ENOMEM;
  856. sii902x->i2c = client;
  857. sii902x->regmap = devm_regmap_init_i2c(client, &sii902x_regmap_config);
  858. if (IS_ERR(sii902x->regmap))
  859. return PTR_ERR(sii902x->regmap);
  860. sii902x->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  861. GPIOD_OUT_LOW);
  862. if (IS_ERR(sii902x->reset_gpio)) {
  863. dev_err(dev, "Failed to retrieve/request reset gpio: %ld\n",
  864. PTR_ERR(sii902x->reset_gpio));
  865. return PTR_ERR(sii902x->reset_gpio);
  866. }
  867. mutex_init(&sii902x->mutex);
  868. sii902x->supplies[0].supply = "iovcc";
  869. sii902x->supplies[1].supply = "cvcc12";
  870. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(sii902x->supplies),
  871. sii902x->supplies);
  872. if (ret < 0)
  873. return ret;
  874. ret = regulator_bulk_enable(ARRAY_SIZE(sii902x->supplies),
  875. sii902x->supplies);
  876. if (ret < 0) {
  877. dev_err_probe(dev, ret, "Failed to enable supplies");
  878. return ret;
  879. }
  880. ret = sii902x_init(sii902x);
  881. if (ret < 0) {
  882. regulator_bulk_disable(ARRAY_SIZE(sii902x->supplies),
  883. sii902x->supplies);
  884. }
  885. return ret;
  886. }
  887. static int sii902x_remove(struct i2c_client *client)
  888. {
  889. struct sii902x *sii902x = i2c_get_clientdata(client);
  890. i2c_mux_del_adapters(sii902x->i2cmux);
  891. drm_bridge_remove(&sii902x->bridge);
  892. regulator_bulk_disable(ARRAY_SIZE(sii902x->supplies),
  893. sii902x->supplies);
  894. return 0;
  895. }
  896. static const struct of_device_id sii902x_dt_ids[] = {
  897. { .compatible = "sil,sii9022", },
  898. { }
  899. };
  900. MODULE_DEVICE_TABLE(of, sii902x_dt_ids);
  901. static const struct i2c_device_id sii902x_i2c_ids[] = {
  902. { "sii9022", 0 },
  903. { },
  904. };
  905. MODULE_DEVICE_TABLE(i2c, sii902x_i2c_ids);
  906. static struct i2c_driver sii902x_driver = {
  907. .probe = sii902x_probe,
  908. .remove = sii902x_remove,
  909. .driver = {
  910. .name = "sii902x",
  911. .of_match_table = sii902x_dt_ids,
  912. },
  913. .id_table = sii902x_i2c_ids,
  914. };
  915. module_i2c_driver(sii902x_driver);
  916. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  917. MODULE_DESCRIPTION("SII902x RGB -> HDMI bridges");
  918. MODULE_LICENSE("GPL");