adv7511_drv.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. /*
  2. * Analog Devices ADV7511 HDMI transmitter driver
  3. *
  4. * Copyright 2012 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/module.h>
  11. #include <linux/of_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/clk.h>
  14. #include <drm/drmP.h>
  15. #include <drm/drm_atomic.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_edid.h>
  18. #include <media/cec.h>
  19. #include "adv7511.h"
  20. /* ADI recommended values for proper operation. */
  21. static const struct reg_sequence adv7511_fixed_registers[] = {
  22. { 0x98, 0x03 },
  23. { 0x9a, 0xe0 },
  24. { 0x9c, 0x30 },
  25. { 0x9d, 0x61 },
  26. { 0xa2, 0xa4 },
  27. { 0xa3, 0xa4 },
  28. { 0xe0, 0xd0 },
  29. { 0xf9, 0x00 },
  30. { 0x55, 0x02 },
  31. };
  32. /* -----------------------------------------------------------------------------
  33. * Register access
  34. */
  35. static const uint8_t adv7511_register_defaults[] = {
  36. 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
  37. 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
  38. 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
  39. 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
  40. 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
  41. 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
  43. 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
  44. 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
  45. 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
  46. 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
  47. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
  49. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
  55. 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
  56. 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
  57. 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
  61. 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
  62. 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
  64. 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
  67. 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. };
  69. static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
  70. {
  71. switch (reg) {
  72. case ADV7511_REG_CHIP_REVISION:
  73. case ADV7511_REG_SPDIF_FREQ:
  74. case ADV7511_REG_CTS_AUTOMATIC1:
  75. case ADV7511_REG_CTS_AUTOMATIC2:
  76. case ADV7511_REG_VIC_DETECTED:
  77. case ADV7511_REG_VIC_SEND:
  78. case ADV7511_REG_AUX_VIC_DETECTED:
  79. case ADV7511_REG_STATUS:
  80. case ADV7511_REG_GC(1):
  81. case ADV7511_REG_INT(0):
  82. case ADV7511_REG_INT(1):
  83. case ADV7511_REG_PLL_STATUS:
  84. case ADV7511_REG_AN(0):
  85. case ADV7511_REG_AN(1):
  86. case ADV7511_REG_AN(2):
  87. case ADV7511_REG_AN(3):
  88. case ADV7511_REG_AN(4):
  89. case ADV7511_REG_AN(5):
  90. case ADV7511_REG_AN(6):
  91. case ADV7511_REG_AN(7):
  92. case ADV7511_REG_HDCP_STATUS:
  93. case ADV7511_REG_BCAPS:
  94. case ADV7511_REG_BKSV(0):
  95. case ADV7511_REG_BKSV(1):
  96. case ADV7511_REG_BKSV(2):
  97. case ADV7511_REG_BKSV(3):
  98. case ADV7511_REG_BKSV(4):
  99. case ADV7511_REG_DDC_STATUS:
  100. case ADV7511_REG_EDID_READ_CTRL:
  101. case ADV7511_REG_BSTATUS(0):
  102. case ADV7511_REG_BSTATUS(1):
  103. case ADV7511_REG_CHIP_ID_HIGH:
  104. case ADV7511_REG_CHIP_ID_LOW:
  105. return true;
  106. }
  107. return false;
  108. }
  109. static const struct regmap_config adv7511_regmap_config = {
  110. .reg_bits = 8,
  111. .val_bits = 8,
  112. .max_register = 0xff,
  113. .cache_type = REGCACHE_RBTREE,
  114. .reg_defaults_raw = adv7511_register_defaults,
  115. .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
  116. .volatile_reg = adv7511_register_volatile,
  117. };
  118. /* -----------------------------------------------------------------------------
  119. * Hardware configuration
  120. */
  121. static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
  122. const uint16_t *coeff,
  123. unsigned int scaling_factor)
  124. {
  125. unsigned int i;
  126. regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
  127. ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
  128. if (enable) {
  129. for (i = 0; i < 12; ++i) {
  130. regmap_update_bits(adv7511->regmap,
  131. ADV7511_REG_CSC_UPPER(i),
  132. 0x1f, coeff[i] >> 8);
  133. regmap_write(adv7511->regmap,
  134. ADV7511_REG_CSC_LOWER(i),
  135. coeff[i] & 0xff);
  136. }
  137. }
  138. if (enable)
  139. regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
  140. 0xe0, 0x80 | (scaling_factor << 5));
  141. else
  142. regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
  143. 0x80, 0x00);
  144. regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
  145. ADV7511_CSC_UPDATE_MODE, 0);
  146. }
  147. static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
  148. {
  149. if (packet & 0xff)
  150. regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
  151. packet, 0xff);
  152. if (packet & 0xff00) {
  153. packet >>= 8;
  154. regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
  155. packet, 0xff);
  156. }
  157. return 0;
  158. }
  159. static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
  160. {
  161. if (packet & 0xff)
  162. regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
  163. packet, 0x00);
  164. if (packet & 0xff00) {
  165. packet >>= 8;
  166. regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
  167. packet, 0x00);
  168. }
  169. return 0;
  170. }
  171. /* Coefficients for adv7511 color space conversion */
  172. static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
  173. 0x0734, 0x04ad, 0x0000, 0x1c1b,
  174. 0x1ddc, 0x04ad, 0x1f24, 0x0135,
  175. 0x0000, 0x04ad, 0x087c, 0x1b77,
  176. };
  177. static void adv7511_set_config_csc(struct adv7511 *adv7511,
  178. struct drm_connector *connector,
  179. bool rgb, bool hdmi_mode)
  180. {
  181. struct adv7511_video_config config;
  182. bool output_format_422, output_format_ycbcr;
  183. unsigned int mode;
  184. uint8_t infoframe[17];
  185. config.hdmi_mode = hdmi_mode;
  186. hdmi_avi_infoframe_init(&config.avi_infoframe);
  187. config.avi_infoframe.scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
  188. if (rgb) {
  189. config.csc_enable = false;
  190. config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
  191. } else {
  192. config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
  193. config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
  194. if ((connector->display_info.color_formats &
  195. DRM_COLOR_FORMAT_YCRCB422) &&
  196. config.hdmi_mode) {
  197. config.csc_enable = false;
  198. config.avi_infoframe.colorspace =
  199. HDMI_COLORSPACE_YUV422;
  200. } else {
  201. config.csc_enable = true;
  202. config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
  203. }
  204. }
  205. if (config.hdmi_mode) {
  206. mode = ADV7511_HDMI_CFG_MODE_HDMI;
  207. switch (config.avi_infoframe.colorspace) {
  208. case HDMI_COLORSPACE_YUV444:
  209. output_format_422 = false;
  210. output_format_ycbcr = true;
  211. break;
  212. case HDMI_COLORSPACE_YUV422:
  213. output_format_422 = true;
  214. output_format_ycbcr = true;
  215. break;
  216. default:
  217. output_format_422 = false;
  218. output_format_ycbcr = false;
  219. break;
  220. }
  221. } else {
  222. mode = ADV7511_HDMI_CFG_MODE_DVI;
  223. output_format_422 = false;
  224. output_format_ycbcr = false;
  225. }
  226. adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
  227. adv7511_set_colormap(adv7511, config.csc_enable,
  228. config.csc_coefficents,
  229. config.csc_scaling_factor);
  230. regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
  231. (output_format_422 << 7) | output_format_ycbcr);
  232. regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
  233. ADV7511_HDMI_CFG_MODE_MASK, mode);
  234. hdmi_avi_infoframe_pack(&config.avi_infoframe, infoframe,
  235. sizeof(infoframe));
  236. /* The AVI infoframe id is not configurable */
  237. regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
  238. infoframe + 1, sizeof(infoframe) - 1);
  239. adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
  240. }
  241. static void adv7511_set_link_config(struct adv7511 *adv7511,
  242. const struct adv7511_link_config *config)
  243. {
  244. /*
  245. * The input style values documented in the datasheet don't match the
  246. * hardware register field values :-(
  247. */
  248. static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
  249. unsigned int clock_delay;
  250. unsigned int color_depth;
  251. unsigned int input_id;
  252. clock_delay = (config->clock_delay + 1200) / 400;
  253. color_depth = config->input_color_depth == 8 ? 3
  254. : (config->input_color_depth == 10 ? 1 : 2);
  255. /* TODO Support input ID 6 */
  256. if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
  257. input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
  258. ? 5 : 0;
  259. else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
  260. input_id = config->embedded_sync ? 8 : 7;
  261. else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
  262. input_id = config->embedded_sync ? 4 : 3;
  263. else
  264. input_id = config->embedded_sync ? 2 : 1;
  265. regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
  266. input_id);
  267. regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
  268. (color_depth << 4) |
  269. (input_styles[config->input_style] << 2));
  270. regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
  271. config->input_justification << 3);
  272. regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
  273. config->sync_pulse << 2);
  274. regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
  275. adv7511->embedded_sync = config->embedded_sync;
  276. adv7511->hsync_polarity = config->hsync_polarity;
  277. adv7511->vsync_polarity = config->vsync_polarity;
  278. adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
  279. }
  280. static void __adv7511_power_on(struct adv7511 *adv7511)
  281. {
  282. adv7511->current_edid_segment = -1;
  283. regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
  284. ADV7511_POWER_POWER_DOWN, 0);
  285. if (adv7511->i2c_main->irq) {
  286. /*
  287. * Documentation says the INT_ENABLE registers are reset in
  288. * POWER_DOWN mode. My 7511w preserved the bits, however.
  289. * Still, let's be safe and stick to the documentation.
  290. */
  291. regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
  292. ADV7511_INT0_EDID_READY | ADV7511_INT0_HPD);
  293. regmap_update_bits(adv7511->regmap,
  294. ADV7511_REG_INT_ENABLE(1),
  295. ADV7511_INT1_DDC_ERROR,
  296. ADV7511_INT1_DDC_ERROR);
  297. }
  298. /*
  299. * Per spec it is allowed to pulse the HPD signal to indicate that the
  300. * EDID information has changed. Some monitors do this when they wakeup
  301. * from standby or are enabled. When the HPD goes low the adv7511 is
  302. * reset and the outputs are disabled which might cause the monitor to
  303. * go to standby again. To avoid this we ignore the HPD pin for the
  304. * first few seconds after enabling the output.
  305. */
  306. regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
  307. ADV7511_REG_POWER2_HPD_SRC_MASK,
  308. ADV7511_REG_POWER2_HPD_SRC_NONE);
  309. }
  310. static void adv7511_power_on(struct adv7511 *adv7511)
  311. {
  312. __adv7511_power_on(adv7511);
  313. /*
  314. * Most of the registers are reset during power down or when HPD is low.
  315. */
  316. regcache_sync(adv7511->regmap);
  317. if (adv7511->type == ADV7533)
  318. adv7533_dsi_power_on(adv7511);
  319. adv7511->powered = true;
  320. }
  321. static void __adv7511_power_off(struct adv7511 *adv7511)
  322. {
  323. /* TODO: setup additional power down modes */
  324. regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
  325. ADV7511_POWER_POWER_DOWN,
  326. ADV7511_POWER_POWER_DOWN);
  327. regmap_update_bits(adv7511->regmap,
  328. ADV7511_REG_INT_ENABLE(1),
  329. ADV7511_INT1_DDC_ERROR, 0);
  330. regcache_mark_dirty(adv7511->regmap);
  331. }
  332. static void adv7511_power_off(struct adv7511 *adv7511)
  333. {
  334. __adv7511_power_off(adv7511);
  335. if (adv7511->type == ADV7533)
  336. adv7533_dsi_power_off(adv7511);
  337. adv7511->powered = false;
  338. }
  339. /* -----------------------------------------------------------------------------
  340. * Interrupt and hotplug detection
  341. */
  342. static bool adv7511_hpd(struct adv7511 *adv7511)
  343. {
  344. unsigned int irq0;
  345. int ret;
  346. ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
  347. if (ret < 0)
  348. return false;
  349. if (irq0 & ADV7511_INT0_HPD) {
  350. regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
  351. ADV7511_INT0_HPD);
  352. return true;
  353. }
  354. return false;
  355. }
  356. static void adv7511_hpd_work(struct work_struct *work)
  357. {
  358. struct adv7511 *adv7511 = container_of(work, struct adv7511, hpd_work);
  359. enum drm_connector_status status;
  360. unsigned int val;
  361. int ret;
  362. ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
  363. if (ret < 0)
  364. status = connector_status_disconnected;
  365. else if (val & ADV7511_STATUS_HPD)
  366. status = connector_status_connected;
  367. else
  368. status = connector_status_disconnected;
  369. if (adv7511->connector.status != status) {
  370. adv7511->connector.status = status;
  371. if (status == connector_status_disconnected)
  372. cec_phys_addr_invalidate(adv7511->cec_adap);
  373. drm_kms_helper_hotplug_event(adv7511->connector.dev);
  374. }
  375. }
  376. static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
  377. {
  378. unsigned int irq0, irq1;
  379. int ret;
  380. ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
  381. if (ret < 0)
  382. return ret;
  383. ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
  384. if (ret < 0)
  385. return ret;
  386. regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
  387. regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
  388. if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder)
  389. schedule_work(&adv7511->hpd_work);
  390. if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
  391. adv7511->edid_read = true;
  392. if (adv7511->i2c_main->irq)
  393. wake_up_all(&adv7511->wq);
  394. }
  395. #ifdef CONFIG_DRM_I2C_ADV7511_CEC
  396. adv7511_cec_irq_process(adv7511, irq1);
  397. #endif
  398. return 0;
  399. }
  400. static irqreturn_t adv7511_irq_handler(int irq, void *devid)
  401. {
  402. struct adv7511 *adv7511 = devid;
  403. int ret;
  404. ret = adv7511_irq_process(adv7511, true);
  405. return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
  406. }
  407. /* -----------------------------------------------------------------------------
  408. * EDID retrieval
  409. */
  410. static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
  411. {
  412. int ret;
  413. if (adv7511->i2c_main->irq) {
  414. ret = wait_event_interruptible_timeout(adv7511->wq,
  415. adv7511->edid_read, msecs_to_jiffies(timeout));
  416. } else {
  417. for (; timeout > 0; timeout -= 25) {
  418. ret = adv7511_irq_process(adv7511, false);
  419. if (ret < 0)
  420. break;
  421. if (adv7511->edid_read)
  422. break;
  423. msleep(25);
  424. }
  425. }
  426. return adv7511->edid_read ? 0 : -EIO;
  427. }
  428. static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
  429. size_t len)
  430. {
  431. struct adv7511 *adv7511 = data;
  432. struct i2c_msg xfer[2];
  433. uint8_t offset;
  434. unsigned int i;
  435. int ret;
  436. if (len > 128)
  437. return -EINVAL;
  438. if (adv7511->current_edid_segment != block / 2) {
  439. unsigned int status;
  440. ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
  441. &status);
  442. if (ret < 0)
  443. return ret;
  444. if (status != 2) {
  445. adv7511->edid_read = false;
  446. regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
  447. block);
  448. ret = adv7511_wait_for_edid(adv7511, 200);
  449. if (ret < 0)
  450. return ret;
  451. }
  452. /* Break this apart, hopefully more I2C controllers will
  453. * support 64 byte transfers than 256 byte transfers
  454. */
  455. xfer[0].addr = adv7511->i2c_edid->addr;
  456. xfer[0].flags = 0;
  457. xfer[0].len = 1;
  458. xfer[0].buf = &offset;
  459. xfer[1].addr = adv7511->i2c_edid->addr;
  460. xfer[1].flags = I2C_M_RD;
  461. xfer[1].len = 64;
  462. xfer[1].buf = adv7511->edid_buf;
  463. offset = 0;
  464. for (i = 0; i < 4; ++i) {
  465. ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
  466. ARRAY_SIZE(xfer));
  467. if (ret < 0)
  468. return ret;
  469. else if (ret != 2)
  470. return -EIO;
  471. xfer[1].buf += 64;
  472. offset += 64;
  473. }
  474. adv7511->current_edid_segment = block / 2;
  475. }
  476. if (block % 2 == 0)
  477. memcpy(buf, adv7511->edid_buf, len);
  478. else
  479. memcpy(buf, adv7511->edid_buf + 128, len);
  480. return 0;
  481. }
  482. /* -----------------------------------------------------------------------------
  483. * ADV75xx helpers
  484. */
  485. static int adv7511_get_modes(struct adv7511 *adv7511,
  486. struct drm_connector *connector)
  487. {
  488. struct edid *edid;
  489. unsigned int count;
  490. /* Reading the EDID only works if the device is powered */
  491. if (!adv7511->powered) {
  492. unsigned int edid_i2c_addr =
  493. (adv7511->i2c_main->addr << 1) + 4;
  494. __adv7511_power_on(adv7511);
  495. /* Reset the EDID_I2C_ADDR register as it might be cleared */
  496. regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
  497. edid_i2c_addr);
  498. }
  499. edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
  500. if (!adv7511->powered)
  501. __adv7511_power_off(adv7511);
  502. drm_mode_connector_update_edid_property(connector, edid);
  503. count = drm_add_edid_modes(connector, edid);
  504. adv7511_set_config_csc(adv7511, connector, adv7511->rgb,
  505. drm_detect_hdmi_monitor(edid));
  506. cec_s_phys_addr_from_edid(adv7511->cec_adap, edid);
  507. kfree(edid);
  508. return count;
  509. }
  510. static enum drm_connector_status
  511. adv7511_detect(struct adv7511 *adv7511, struct drm_connector *connector)
  512. {
  513. enum drm_connector_status status;
  514. unsigned int val;
  515. bool hpd;
  516. int ret;
  517. ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
  518. if (ret < 0)
  519. return connector_status_disconnected;
  520. if (val & ADV7511_STATUS_HPD)
  521. status = connector_status_connected;
  522. else
  523. status = connector_status_disconnected;
  524. hpd = adv7511_hpd(adv7511);
  525. /* The chip resets itself when the cable is disconnected, so in case
  526. * there is a pending HPD interrupt and the cable is connected there was
  527. * at least one transition from disconnected to connected and the chip
  528. * has to be reinitialized. */
  529. if (status == connector_status_connected && hpd && adv7511->powered) {
  530. regcache_mark_dirty(adv7511->regmap);
  531. adv7511_power_on(adv7511);
  532. adv7511_get_modes(adv7511, connector);
  533. if (adv7511->status == connector_status_connected)
  534. status = connector_status_disconnected;
  535. } else {
  536. /* Renable HPD sensing */
  537. regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
  538. ADV7511_REG_POWER2_HPD_SRC_MASK,
  539. ADV7511_REG_POWER2_HPD_SRC_BOTH);
  540. }
  541. adv7511->status = status;
  542. return status;
  543. }
  544. static int adv7511_mode_valid(struct adv7511 *adv7511,
  545. struct drm_display_mode *mode)
  546. {
  547. if (mode->clock > 165000)
  548. return MODE_CLOCK_HIGH;
  549. return MODE_OK;
  550. }
  551. static void adv7511_mode_set(struct adv7511 *adv7511,
  552. struct drm_display_mode *mode,
  553. struct drm_display_mode *adj_mode)
  554. {
  555. unsigned int low_refresh_rate;
  556. unsigned int hsync_polarity = 0;
  557. unsigned int vsync_polarity = 0;
  558. if (adv7511->embedded_sync) {
  559. unsigned int hsync_offset, hsync_len;
  560. unsigned int vsync_offset, vsync_len;
  561. hsync_offset = adj_mode->crtc_hsync_start -
  562. adj_mode->crtc_hdisplay;
  563. vsync_offset = adj_mode->crtc_vsync_start -
  564. adj_mode->crtc_vdisplay;
  565. hsync_len = adj_mode->crtc_hsync_end -
  566. adj_mode->crtc_hsync_start;
  567. vsync_len = adj_mode->crtc_vsync_end -
  568. adj_mode->crtc_vsync_start;
  569. /* The hardware vsync generator has a off-by-one bug */
  570. vsync_offset += 1;
  571. regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
  572. ((hsync_offset >> 10) & 0x7) << 5);
  573. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
  574. (hsync_offset >> 2) & 0xff);
  575. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
  576. ((hsync_offset & 0x3) << 6) |
  577. ((hsync_len >> 4) & 0x3f));
  578. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
  579. ((hsync_len & 0xf) << 4) |
  580. ((vsync_offset >> 6) & 0xf));
  581. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
  582. ((vsync_offset & 0x3f) << 2) |
  583. ((vsync_len >> 8) & 0x3));
  584. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
  585. vsync_len & 0xff);
  586. hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
  587. vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
  588. } else {
  589. enum adv7511_sync_polarity mode_hsync_polarity;
  590. enum adv7511_sync_polarity mode_vsync_polarity;
  591. /**
  592. * If the input signal is always low or always high we want to
  593. * invert or let it passthrough depending on the polarity of the
  594. * current mode.
  595. **/
  596. if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
  597. mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
  598. else
  599. mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
  600. if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
  601. mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
  602. else
  603. mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
  604. if (adv7511->hsync_polarity != mode_hsync_polarity &&
  605. adv7511->hsync_polarity !=
  606. ADV7511_SYNC_POLARITY_PASSTHROUGH)
  607. hsync_polarity = 1;
  608. if (adv7511->vsync_polarity != mode_vsync_polarity &&
  609. adv7511->vsync_polarity !=
  610. ADV7511_SYNC_POLARITY_PASSTHROUGH)
  611. vsync_polarity = 1;
  612. }
  613. if (mode->vrefresh <= 24000)
  614. low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
  615. else if (mode->vrefresh <= 25000)
  616. low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
  617. else if (mode->vrefresh <= 30000)
  618. low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
  619. else
  620. low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
  621. regmap_update_bits(adv7511->regmap, 0xfb,
  622. 0x6, low_refresh_rate << 1);
  623. regmap_update_bits(adv7511->regmap, 0x17,
  624. 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
  625. if (adv7511->type == ADV7533)
  626. adv7533_mode_set(adv7511, adj_mode);
  627. drm_mode_copy(&adv7511->curr_mode, adj_mode);
  628. /*
  629. * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
  630. * supposed to give better results.
  631. */
  632. adv7511->f_tmds = mode->clock;
  633. }
  634. /* Connector funcs */
  635. static struct adv7511 *connector_to_adv7511(struct drm_connector *connector)
  636. {
  637. return container_of(connector, struct adv7511, connector);
  638. }
  639. static int adv7511_connector_get_modes(struct drm_connector *connector)
  640. {
  641. struct adv7511 *adv = connector_to_adv7511(connector);
  642. return adv7511_get_modes(adv, connector);
  643. }
  644. static enum drm_mode_status
  645. adv7511_connector_mode_valid(struct drm_connector *connector,
  646. struct drm_display_mode *mode)
  647. {
  648. struct adv7511 *adv = connector_to_adv7511(connector);
  649. return adv7511_mode_valid(adv, mode);
  650. }
  651. static struct drm_connector_helper_funcs adv7511_connector_helper_funcs = {
  652. .get_modes = adv7511_connector_get_modes,
  653. .mode_valid = adv7511_connector_mode_valid,
  654. };
  655. static enum drm_connector_status
  656. adv7511_connector_detect(struct drm_connector *connector, bool force)
  657. {
  658. struct adv7511 *adv = connector_to_adv7511(connector);
  659. return adv7511_detect(adv, connector);
  660. }
  661. static const struct drm_connector_funcs adv7511_connector_funcs = {
  662. .fill_modes = drm_helper_probe_single_connector_modes,
  663. .detect = adv7511_connector_detect,
  664. .destroy = drm_connector_cleanup,
  665. .reset = drm_atomic_helper_connector_reset,
  666. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  667. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  668. };
  669. /* Bridge funcs */
  670. static struct adv7511 *bridge_to_adv7511(struct drm_bridge *bridge)
  671. {
  672. return container_of(bridge, struct adv7511, bridge);
  673. }
  674. static void adv7511_bridge_enable(struct drm_bridge *bridge)
  675. {
  676. struct adv7511 *adv = bridge_to_adv7511(bridge);
  677. adv7511_power_on(adv);
  678. }
  679. static void adv7511_bridge_disable(struct drm_bridge *bridge)
  680. {
  681. struct adv7511 *adv = bridge_to_adv7511(bridge);
  682. adv7511_power_off(adv);
  683. }
  684. static void adv7511_bridge_mode_set(struct drm_bridge *bridge,
  685. struct drm_display_mode *mode,
  686. struct drm_display_mode *adj_mode)
  687. {
  688. struct adv7511 *adv = bridge_to_adv7511(bridge);
  689. adv7511_mode_set(adv, mode, adj_mode);
  690. }
  691. static int adv7511_bridge_attach(struct drm_bridge *bridge)
  692. {
  693. struct adv7511 *adv = bridge_to_adv7511(bridge);
  694. int ret;
  695. if (!bridge->encoder) {
  696. DRM_ERROR("Parent encoder object not found");
  697. return -ENODEV;
  698. }
  699. if (adv->i2c_main->irq)
  700. adv->connector.polled = DRM_CONNECTOR_POLL_HPD;
  701. else
  702. adv->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
  703. DRM_CONNECTOR_POLL_DISCONNECT;
  704. ret = drm_connector_init(bridge->dev, &adv->connector,
  705. &adv7511_connector_funcs,
  706. DRM_MODE_CONNECTOR_HDMIA);
  707. if (ret) {
  708. DRM_ERROR("Failed to initialize connector with drm\n");
  709. return ret;
  710. }
  711. drm_connector_helper_add(&adv->connector,
  712. &adv7511_connector_helper_funcs);
  713. drm_mode_connector_attach_encoder(&adv->connector, bridge->encoder);
  714. if (adv->type == ADV7533)
  715. ret = adv7533_attach_dsi(adv);
  716. if (adv->i2c_main->irq)
  717. regmap_write(adv->regmap, ADV7511_REG_INT_ENABLE(0),
  718. ADV7511_INT0_HPD);
  719. return ret;
  720. }
  721. static const struct drm_bridge_funcs adv7511_bridge_funcs = {
  722. .enable = adv7511_bridge_enable,
  723. .disable = adv7511_bridge_disable,
  724. .mode_set = adv7511_bridge_mode_set,
  725. .attach = adv7511_bridge_attach,
  726. };
  727. /* -----------------------------------------------------------------------------
  728. * Probe & remove
  729. */
  730. static const char * const adv7511_supply_names[] = {
  731. "avdd",
  732. "dvdd",
  733. "pvdd",
  734. "bgvdd",
  735. "dvdd-3v",
  736. };
  737. static const char * const adv7533_supply_names[] = {
  738. "avdd",
  739. "dvdd",
  740. "pvdd",
  741. "a2vdd",
  742. "v3p3",
  743. "v1p2",
  744. };
  745. static int adv7511_init_regulators(struct adv7511 *adv)
  746. {
  747. struct device *dev = &adv->i2c_main->dev;
  748. const char * const *supply_names;
  749. unsigned int i;
  750. int ret;
  751. if (adv->type == ADV7511) {
  752. supply_names = adv7511_supply_names;
  753. adv->num_supplies = ARRAY_SIZE(adv7511_supply_names);
  754. } else {
  755. supply_names = adv7533_supply_names;
  756. adv->num_supplies = ARRAY_SIZE(adv7533_supply_names);
  757. }
  758. adv->supplies = devm_kcalloc(dev, adv->num_supplies,
  759. sizeof(*adv->supplies), GFP_KERNEL);
  760. if (!adv->supplies)
  761. return -ENOMEM;
  762. for (i = 0; i < adv->num_supplies; i++)
  763. adv->supplies[i].supply = supply_names[i];
  764. ret = devm_regulator_bulk_get(dev, adv->num_supplies, adv->supplies);
  765. if (ret)
  766. return ret;
  767. return regulator_bulk_enable(adv->num_supplies, adv->supplies);
  768. }
  769. static void adv7511_uninit_regulators(struct adv7511 *adv)
  770. {
  771. regulator_bulk_disable(adv->num_supplies, adv->supplies);
  772. }
  773. static bool adv7511_cec_register_volatile(struct device *dev, unsigned int reg)
  774. {
  775. struct i2c_client *i2c = to_i2c_client(dev);
  776. struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
  777. if (adv7511->type == ADV7533)
  778. reg -= ADV7533_REG_CEC_OFFSET;
  779. switch (reg) {
  780. case ADV7511_REG_CEC_RX_FRAME_HDR:
  781. case ADV7511_REG_CEC_RX_FRAME_DATA0...
  782. ADV7511_REG_CEC_RX_FRAME_DATA0 + 14:
  783. case ADV7511_REG_CEC_RX_FRAME_LEN:
  784. case ADV7511_REG_CEC_RX_BUFFERS:
  785. case ADV7511_REG_CEC_TX_LOW_DRV_CNT:
  786. return true;
  787. }
  788. return false;
  789. }
  790. static const struct regmap_config adv7511_cec_regmap_config = {
  791. .reg_bits = 8,
  792. .val_bits = 8,
  793. .max_register = 0xff,
  794. .cache_type = REGCACHE_RBTREE,
  795. .volatile_reg = adv7511_cec_register_volatile,
  796. };
  797. static int adv7511_init_cec_regmap(struct adv7511 *adv)
  798. {
  799. int ret;
  800. adv->i2c_cec = i2c_new_dummy(adv->i2c_main->adapter,
  801. adv->i2c_main->addr - 1);
  802. if (!adv->i2c_cec)
  803. return -ENOMEM;
  804. i2c_set_clientdata(adv->i2c_cec, adv);
  805. adv->regmap_cec = devm_regmap_init_i2c(adv->i2c_cec,
  806. &adv7511_cec_regmap_config);
  807. if (IS_ERR(adv->regmap_cec)) {
  808. ret = PTR_ERR(adv->regmap_cec);
  809. goto err;
  810. }
  811. if (adv->type == ADV7533) {
  812. ret = adv7533_patch_cec_registers(adv);
  813. if (ret)
  814. goto err;
  815. }
  816. return 0;
  817. err:
  818. i2c_unregister_device(adv->i2c_cec);
  819. return ret;
  820. }
  821. static int adv7511_parse_dt(struct device_node *np,
  822. struct adv7511_link_config *config)
  823. {
  824. const char *str;
  825. int ret;
  826. of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
  827. if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
  828. config->input_color_depth != 12)
  829. return -EINVAL;
  830. ret = of_property_read_string(np, "adi,input-colorspace", &str);
  831. if (ret < 0)
  832. return ret;
  833. if (!strcmp(str, "rgb"))
  834. config->input_colorspace = HDMI_COLORSPACE_RGB;
  835. else if (!strcmp(str, "yuv422"))
  836. config->input_colorspace = HDMI_COLORSPACE_YUV422;
  837. else if (!strcmp(str, "yuv444"))
  838. config->input_colorspace = HDMI_COLORSPACE_YUV444;
  839. else
  840. return -EINVAL;
  841. ret = of_property_read_string(np, "adi,input-clock", &str);
  842. if (ret < 0)
  843. return ret;
  844. if (!strcmp(str, "1x"))
  845. config->input_clock = ADV7511_INPUT_CLOCK_1X;
  846. else if (!strcmp(str, "2x"))
  847. config->input_clock = ADV7511_INPUT_CLOCK_2X;
  848. else if (!strcmp(str, "ddr"))
  849. config->input_clock = ADV7511_INPUT_CLOCK_DDR;
  850. else
  851. return -EINVAL;
  852. if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
  853. config->input_clock != ADV7511_INPUT_CLOCK_1X) {
  854. ret = of_property_read_u32(np, "adi,input-style",
  855. &config->input_style);
  856. if (ret)
  857. return ret;
  858. if (config->input_style < 1 || config->input_style > 3)
  859. return -EINVAL;
  860. ret = of_property_read_string(np, "adi,input-justification",
  861. &str);
  862. if (ret < 0)
  863. return ret;
  864. if (!strcmp(str, "left"))
  865. config->input_justification =
  866. ADV7511_INPUT_JUSTIFICATION_LEFT;
  867. else if (!strcmp(str, "evenly"))
  868. config->input_justification =
  869. ADV7511_INPUT_JUSTIFICATION_EVENLY;
  870. else if (!strcmp(str, "right"))
  871. config->input_justification =
  872. ADV7511_INPUT_JUSTIFICATION_RIGHT;
  873. else
  874. return -EINVAL;
  875. } else {
  876. config->input_style = 1;
  877. config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
  878. }
  879. of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
  880. if (config->clock_delay < -1200 || config->clock_delay > 1600)
  881. return -EINVAL;
  882. config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
  883. /* Hardcode the sync pulse configurations for now. */
  884. config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
  885. config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
  886. config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
  887. return 0;
  888. }
  889. static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
  890. {
  891. struct adv7511_link_config link_config;
  892. struct adv7511 *adv7511;
  893. struct device *dev = &i2c->dev;
  894. unsigned int main_i2c_addr = i2c->addr << 1;
  895. unsigned int edid_i2c_addr = main_i2c_addr + 4;
  896. unsigned int val;
  897. int ret;
  898. if (!dev->of_node)
  899. return -EINVAL;
  900. adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
  901. if (!adv7511)
  902. return -ENOMEM;
  903. adv7511->i2c_main = i2c;
  904. adv7511->powered = false;
  905. adv7511->status = connector_status_disconnected;
  906. if (dev->of_node)
  907. adv7511->type = (enum adv7511_type)of_device_get_match_data(dev);
  908. else
  909. adv7511->type = id->driver_data;
  910. memset(&link_config, 0, sizeof(link_config));
  911. if (adv7511->type == ADV7511)
  912. ret = adv7511_parse_dt(dev->of_node, &link_config);
  913. else
  914. ret = adv7533_parse_dt(dev->of_node, adv7511);
  915. if (ret)
  916. return ret;
  917. ret = adv7511_init_regulators(adv7511);
  918. if (ret) {
  919. dev_err(dev, "failed to init regulators\n");
  920. return ret;
  921. }
  922. /*
  923. * The power down GPIO is optional. If present, toggle it from active to
  924. * inactive to wake up the encoder.
  925. */
  926. adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
  927. if (IS_ERR(adv7511->gpio_pd)) {
  928. ret = PTR_ERR(adv7511->gpio_pd);
  929. goto uninit_regulators;
  930. }
  931. if (adv7511->gpio_pd) {
  932. mdelay(5);
  933. gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
  934. }
  935. adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
  936. if (IS_ERR(adv7511->regmap)) {
  937. ret = PTR_ERR(adv7511->regmap);
  938. goto uninit_regulators;
  939. }
  940. ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
  941. if (ret)
  942. goto uninit_regulators;
  943. dev_dbg(dev, "Rev. %d\n", val);
  944. if (adv7511->type == ADV7511)
  945. ret = regmap_register_patch(adv7511->regmap,
  946. adv7511_fixed_registers,
  947. ARRAY_SIZE(adv7511_fixed_registers));
  948. else
  949. ret = adv7533_patch_registers(adv7511);
  950. if (ret)
  951. goto uninit_regulators;
  952. regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
  953. regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
  954. main_i2c_addr - 0xa);
  955. regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR,
  956. main_i2c_addr - 2);
  957. adv7511_packet_disable(adv7511, 0xffff);
  958. adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
  959. if (!adv7511->i2c_edid) {
  960. ret = -ENOMEM;
  961. goto uninit_regulators;
  962. }
  963. ret = adv7511_init_cec_regmap(adv7511);
  964. if (ret)
  965. goto err_i2c_unregister_edid;
  966. INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
  967. if (i2c->irq) {
  968. init_waitqueue_head(&adv7511->wq);
  969. ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
  970. adv7511_irq_handler,
  971. IRQF_ONESHOT, dev_name(dev),
  972. adv7511);
  973. if (ret)
  974. goto err_unregister_cec;
  975. }
  976. adv7511_power_off(adv7511);
  977. i2c_set_clientdata(i2c, adv7511);
  978. if (adv7511->type == ADV7511)
  979. adv7511_set_link_config(adv7511, &link_config);
  980. ret = adv7511_cec_init(dev, adv7511);
  981. if (ret)
  982. goto err_unregister_cec;
  983. adv7511->bridge.funcs = &adv7511_bridge_funcs;
  984. adv7511->bridge.of_node = dev->of_node;
  985. drm_bridge_add(&adv7511->bridge);
  986. adv7511_audio_init(dev, adv7511);
  987. return 0;
  988. err_unregister_cec:
  989. i2c_unregister_device(adv7511->i2c_cec);
  990. if (adv7511->cec_clk)
  991. clk_disable_unprepare(adv7511->cec_clk);
  992. err_i2c_unregister_edid:
  993. i2c_unregister_device(adv7511->i2c_edid);
  994. uninit_regulators:
  995. adv7511_uninit_regulators(adv7511);
  996. return ret;
  997. }
  998. static int adv7511_remove(struct i2c_client *i2c)
  999. {
  1000. struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
  1001. if (adv7511->type == ADV7533)
  1002. adv7533_detach_dsi(adv7511);
  1003. i2c_unregister_device(adv7511->i2c_cec);
  1004. if (adv7511->cec_clk)
  1005. clk_disable_unprepare(adv7511->cec_clk);
  1006. adv7511_uninit_regulators(adv7511);
  1007. drm_bridge_remove(&adv7511->bridge);
  1008. adv7511_audio_exit(adv7511);
  1009. cec_unregister_adapter(adv7511->cec_adap);
  1010. i2c_unregister_device(adv7511->i2c_edid);
  1011. return 0;
  1012. }
  1013. static const struct i2c_device_id adv7511_i2c_ids[] = {
  1014. { "adv7511", ADV7511 },
  1015. { "adv7511w", ADV7511 },
  1016. { "adv7513", ADV7511 },
  1017. #ifdef CONFIG_DRM_I2C_ADV7533
  1018. { "adv7533", ADV7533 },
  1019. #endif
  1020. { }
  1021. };
  1022. MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
  1023. static const struct of_device_id adv7511_of_ids[] = {
  1024. { .compatible = "adi,adv7511", .data = (void *)ADV7511 },
  1025. { .compatible = "adi,adv7511w", .data = (void *)ADV7511 },
  1026. { .compatible = "adi,adv7513", .data = (void *)ADV7511 },
  1027. #ifdef CONFIG_DRM_I2C_ADV7533
  1028. { .compatible = "adi,adv7533", .data = (void *)ADV7533 },
  1029. #endif
  1030. { }
  1031. };
  1032. MODULE_DEVICE_TABLE(of, adv7511_of_ids);
  1033. static struct mipi_dsi_driver adv7533_dsi_driver = {
  1034. .driver.name = "adv7533",
  1035. };
  1036. static struct i2c_driver adv7511_driver = {
  1037. .driver = {
  1038. .name = "adv7511",
  1039. .of_match_table = adv7511_of_ids,
  1040. },
  1041. .id_table = adv7511_i2c_ids,
  1042. .probe = adv7511_probe,
  1043. .remove = adv7511_remove,
  1044. };
  1045. static int __init adv7511_init(void)
  1046. {
  1047. if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
  1048. mipi_dsi_driver_register(&adv7533_dsi_driver);
  1049. return i2c_add_driver(&adv7511_driver);
  1050. }
  1051. module_init(adv7511_init);
  1052. static void __exit adv7511_exit(void)
  1053. {
  1054. i2c_del_driver(&adv7511_driver);
  1055. if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
  1056. mipi_dsi_driver_unregister(&adv7533_dsi_driver);
  1057. }
  1058. module_exit(adv7511_exit);
  1059. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  1060. MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
  1061. MODULE_LICENSE("GPL");