adv7511_drv.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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. /*
  370. * The bridge resets its registers on unplug. So when we get a plug
  371. * event and we're already supposed to be powered, cycle the bridge to
  372. * restore its state.
  373. */
  374. if (status == connector_status_connected &&
  375. adv7511->connector.status == connector_status_disconnected &&
  376. adv7511->powered) {
  377. regcache_mark_dirty(adv7511->regmap);
  378. adv7511_power_on(adv7511);
  379. }
  380. if (adv7511->connector.status != status) {
  381. adv7511->connector.status = status;
  382. if (status == connector_status_disconnected)
  383. cec_phys_addr_invalidate(adv7511->cec_adap);
  384. drm_kms_helper_hotplug_event(adv7511->connector.dev);
  385. }
  386. }
  387. static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
  388. {
  389. unsigned int irq0, irq1;
  390. int ret;
  391. ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
  392. if (ret < 0)
  393. return ret;
  394. ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
  395. if (ret < 0)
  396. return ret;
  397. regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
  398. regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
  399. if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder)
  400. schedule_work(&adv7511->hpd_work);
  401. if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
  402. adv7511->edid_read = true;
  403. if (adv7511->i2c_main->irq)
  404. wake_up_all(&adv7511->wq);
  405. }
  406. #ifdef CONFIG_DRM_I2C_ADV7511_CEC
  407. adv7511_cec_irq_process(adv7511, irq1);
  408. #endif
  409. return 0;
  410. }
  411. static irqreturn_t adv7511_irq_handler(int irq, void *devid)
  412. {
  413. struct adv7511 *adv7511 = devid;
  414. int ret;
  415. ret = adv7511_irq_process(adv7511, true);
  416. return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
  417. }
  418. /* -----------------------------------------------------------------------------
  419. * EDID retrieval
  420. */
  421. static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
  422. {
  423. int ret;
  424. if (adv7511->i2c_main->irq) {
  425. ret = wait_event_interruptible_timeout(adv7511->wq,
  426. adv7511->edid_read, msecs_to_jiffies(timeout));
  427. } else {
  428. for (; timeout > 0; timeout -= 25) {
  429. ret = adv7511_irq_process(adv7511, false);
  430. if (ret < 0)
  431. break;
  432. if (adv7511->edid_read)
  433. break;
  434. msleep(25);
  435. }
  436. }
  437. return adv7511->edid_read ? 0 : -EIO;
  438. }
  439. static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
  440. size_t len)
  441. {
  442. struct adv7511 *adv7511 = data;
  443. struct i2c_msg xfer[2];
  444. uint8_t offset;
  445. unsigned int i;
  446. int ret;
  447. if (len > 128)
  448. return -EINVAL;
  449. if (adv7511->current_edid_segment != block / 2) {
  450. unsigned int status;
  451. ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
  452. &status);
  453. if (ret < 0)
  454. return ret;
  455. if (status != 2) {
  456. adv7511->edid_read = false;
  457. regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
  458. block);
  459. ret = adv7511_wait_for_edid(adv7511, 200);
  460. if (ret < 0)
  461. return ret;
  462. }
  463. /* Break this apart, hopefully more I2C controllers will
  464. * support 64 byte transfers than 256 byte transfers
  465. */
  466. xfer[0].addr = adv7511->i2c_edid->addr;
  467. xfer[0].flags = 0;
  468. xfer[0].len = 1;
  469. xfer[0].buf = &offset;
  470. xfer[1].addr = adv7511->i2c_edid->addr;
  471. xfer[1].flags = I2C_M_RD;
  472. xfer[1].len = 64;
  473. xfer[1].buf = adv7511->edid_buf;
  474. offset = 0;
  475. for (i = 0; i < 4; ++i) {
  476. ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
  477. ARRAY_SIZE(xfer));
  478. if (ret < 0)
  479. return ret;
  480. else if (ret != 2)
  481. return -EIO;
  482. xfer[1].buf += 64;
  483. offset += 64;
  484. }
  485. adv7511->current_edid_segment = block / 2;
  486. }
  487. if (block % 2 == 0)
  488. memcpy(buf, adv7511->edid_buf, len);
  489. else
  490. memcpy(buf, adv7511->edid_buf + 128, len);
  491. return 0;
  492. }
  493. /* -----------------------------------------------------------------------------
  494. * ADV75xx helpers
  495. */
  496. static int adv7511_get_modes(struct adv7511 *adv7511,
  497. struct drm_connector *connector)
  498. {
  499. struct edid *edid;
  500. unsigned int count;
  501. /* Reading the EDID only works if the device is powered */
  502. if (!adv7511->powered) {
  503. unsigned int edid_i2c_addr =
  504. (adv7511->i2c_edid->addr << 1);
  505. __adv7511_power_on(adv7511);
  506. /* Reset the EDID_I2C_ADDR register as it might be cleared */
  507. regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
  508. edid_i2c_addr);
  509. }
  510. edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
  511. if (!adv7511->powered)
  512. __adv7511_power_off(adv7511);
  513. drm_connector_update_edid_property(connector, edid);
  514. count = drm_add_edid_modes(connector, edid);
  515. adv7511_set_config_csc(adv7511, connector, adv7511->rgb,
  516. drm_detect_hdmi_monitor(edid));
  517. cec_s_phys_addr_from_edid(adv7511->cec_adap, edid);
  518. kfree(edid);
  519. return count;
  520. }
  521. static enum drm_connector_status
  522. adv7511_detect(struct adv7511 *adv7511, struct drm_connector *connector)
  523. {
  524. enum drm_connector_status status;
  525. unsigned int val;
  526. bool hpd;
  527. int ret;
  528. ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
  529. if (ret < 0)
  530. return connector_status_disconnected;
  531. if (val & ADV7511_STATUS_HPD)
  532. status = connector_status_connected;
  533. else
  534. status = connector_status_disconnected;
  535. hpd = adv7511_hpd(adv7511);
  536. /* The chip resets itself when the cable is disconnected, so in case
  537. * there is a pending HPD interrupt and the cable is connected there was
  538. * at least one transition from disconnected to connected and the chip
  539. * has to be reinitialized. */
  540. if (status == connector_status_connected && hpd && adv7511->powered) {
  541. regcache_mark_dirty(adv7511->regmap);
  542. adv7511_power_on(adv7511);
  543. adv7511_get_modes(adv7511, connector);
  544. if (adv7511->status == connector_status_connected)
  545. status = connector_status_disconnected;
  546. } else {
  547. /* Renable HPD sensing */
  548. regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
  549. ADV7511_REG_POWER2_HPD_SRC_MASK,
  550. ADV7511_REG_POWER2_HPD_SRC_BOTH);
  551. }
  552. adv7511->status = status;
  553. return status;
  554. }
  555. static enum drm_mode_status adv7511_mode_valid(struct adv7511 *adv7511,
  556. struct drm_display_mode *mode)
  557. {
  558. if (mode->clock > 165000)
  559. return MODE_CLOCK_HIGH;
  560. return MODE_OK;
  561. }
  562. static void adv7511_mode_set(struct adv7511 *adv7511,
  563. struct drm_display_mode *mode,
  564. struct drm_display_mode *adj_mode)
  565. {
  566. unsigned int low_refresh_rate;
  567. unsigned int hsync_polarity = 0;
  568. unsigned int vsync_polarity = 0;
  569. if (adv7511->embedded_sync) {
  570. unsigned int hsync_offset, hsync_len;
  571. unsigned int vsync_offset, vsync_len;
  572. hsync_offset = adj_mode->crtc_hsync_start -
  573. adj_mode->crtc_hdisplay;
  574. vsync_offset = adj_mode->crtc_vsync_start -
  575. adj_mode->crtc_vdisplay;
  576. hsync_len = adj_mode->crtc_hsync_end -
  577. adj_mode->crtc_hsync_start;
  578. vsync_len = adj_mode->crtc_vsync_end -
  579. adj_mode->crtc_vsync_start;
  580. /* The hardware vsync generator has a off-by-one bug */
  581. vsync_offset += 1;
  582. regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
  583. ((hsync_offset >> 10) & 0x7) << 5);
  584. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
  585. (hsync_offset >> 2) & 0xff);
  586. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
  587. ((hsync_offset & 0x3) << 6) |
  588. ((hsync_len >> 4) & 0x3f));
  589. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
  590. ((hsync_len & 0xf) << 4) |
  591. ((vsync_offset >> 6) & 0xf));
  592. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
  593. ((vsync_offset & 0x3f) << 2) |
  594. ((vsync_len >> 8) & 0x3));
  595. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
  596. vsync_len & 0xff);
  597. hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
  598. vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
  599. } else {
  600. enum adv7511_sync_polarity mode_hsync_polarity;
  601. enum adv7511_sync_polarity mode_vsync_polarity;
  602. /**
  603. * If the input signal is always low or always high we want to
  604. * invert or let it passthrough depending on the polarity of the
  605. * current mode.
  606. **/
  607. if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
  608. mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
  609. else
  610. mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
  611. if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
  612. mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
  613. else
  614. mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
  615. if (adv7511->hsync_polarity != mode_hsync_polarity &&
  616. adv7511->hsync_polarity !=
  617. ADV7511_SYNC_POLARITY_PASSTHROUGH)
  618. hsync_polarity = 1;
  619. if (adv7511->vsync_polarity != mode_vsync_polarity &&
  620. adv7511->vsync_polarity !=
  621. ADV7511_SYNC_POLARITY_PASSTHROUGH)
  622. vsync_polarity = 1;
  623. }
  624. if (mode->vrefresh <= 24000)
  625. low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
  626. else if (mode->vrefresh <= 25000)
  627. low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
  628. else if (mode->vrefresh <= 30000)
  629. low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
  630. else
  631. low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
  632. regmap_update_bits(adv7511->regmap, 0xfb,
  633. 0x6, low_refresh_rate << 1);
  634. regmap_update_bits(adv7511->regmap, 0x17,
  635. 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
  636. if (adv7511->type == ADV7533)
  637. adv7533_mode_set(adv7511, adj_mode);
  638. drm_mode_copy(&adv7511->curr_mode, adj_mode);
  639. /*
  640. * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
  641. * supposed to give better results.
  642. */
  643. adv7511->f_tmds = mode->clock;
  644. }
  645. /* Connector funcs */
  646. static struct adv7511 *connector_to_adv7511(struct drm_connector *connector)
  647. {
  648. return container_of(connector, struct adv7511, connector);
  649. }
  650. static int adv7511_connector_get_modes(struct drm_connector *connector)
  651. {
  652. struct adv7511 *adv = connector_to_adv7511(connector);
  653. return adv7511_get_modes(adv, connector);
  654. }
  655. static enum drm_mode_status
  656. adv7511_connector_mode_valid(struct drm_connector *connector,
  657. struct drm_display_mode *mode)
  658. {
  659. struct adv7511 *adv = connector_to_adv7511(connector);
  660. return adv7511_mode_valid(adv, mode);
  661. }
  662. static struct drm_connector_helper_funcs adv7511_connector_helper_funcs = {
  663. .get_modes = adv7511_connector_get_modes,
  664. .mode_valid = adv7511_connector_mode_valid,
  665. };
  666. static enum drm_connector_status
  667. adv7511_connector_detect(struct drm_connector *connector, bool force)
  668. {
  669. struct adv7511 *adv = connector_to_adv7511(connector);
  670. return adv7511_detect(adv, connector);
  671. }
  672. static const struct drm_connector_funcs adv7511_connector_funcs = {
  673. .fill_modes = drm_helper_probe_single_connector_modes,
  674. .detect = adv7511_connector_detect,
  675. .destroy = drm_connector_cleanup,
  676. .reset = drm_atomic_helper_connector_reset,
  677. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  678. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  679. };
  680. /* Bridge funcs */
  681. static struct adv7511 *bridge_to_adv7511(struct drm_bridge *bridge)
  682. {
  683. return container_of(bridge, struct adv7511, bridge);
  684. }
  685. static void adv7511_bridge_enable(struct drm_bridge *bridge)
  686. {
  687. struct adv7511 *adv = bridge_to_adv7511(bridge);
  688. adv7511_power_on(adv);
  689. }
  690. static void adv7511_bridge_disable(struct drm_bridge *bridge)
  691. {
  692. struct adv7511 *adv = bridge_to_adv7511(bridge);
  693. adv7511_power_off(adv);
  694. }
  695. static void adv7511_bridge_mode_set(struct drm_bridge *bridge,
  696. struct drm_display_mode *mode,
  697. struct drm_display_mode *adj_mode)
  698. {
  699. struct adv7511 *adv = bridge_to_adv7511(bridge);
  700. adv7511_mode_set(adv, mode, adj_mode);
  701. }
  702. static int adv7511_bridge_attach(struct drm_bridge *bridge)
  703. {
  704. struct adv7511 *adv = bridge_to_adv7511(bridge);
  705. int ret;
  706. if (!bridge->encoder) {
  707. DRM_ERROR("Parent encoder object not found");
  708. return -ENODEV;
  709. }
  710. if (adv->i2c_main->irq)
  711. adv->connector.polled = DRM_CONNECTOR_POLL_HPD;
  712. else
  713. adv->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
  714. DRM_CONNECTOR_POLL_DISCONNECT;
  715. ret = drm_connector_init(bridge->dev, &adv->connector,
  716. &adv7511_connector_funcs,
  717. DRM_MODE_CONNECTOR_HDMIA);
  718. if (ret) {
  719. DRM_ERROR("Failed to initialize connector with drm\n");
  720. return ret;
  721. }
  722. drm_connector_helper_add(&adv->connector,
  723. &adv7511_connector_helper_funcs);
  724. drm_connector_attach_encoder(&adv->connector, bridge->encoder);
  725. if (adv->type == ADV7533)
  726. ret = adv7533_attach_dsi(adv);
  727. if (adv->i2c_main->irq)
  728. regmap_write(adv->regmap, ADV7511_REG_INT_ENABLE(0),
  729. ADV7511_INT0_HPD);
  730. return ret;
  731. }
  732. static const struct drm_bridge_funcs adv7511_bridge_funcs = {
  733. .enable = adv7511_bridge_enable,
  734. .disable = adv7511_bridge_disable,
  735. .mode_set = adv7511_bridge_mode_set,
  736. .attach = adv7511_bridge_attach,
  737. };
  738. /* -----------------------------------------------------------------------------
  739. * Probe & remove
  740. */
  741. static const char * const adv7511_supply_names[] = {
  742. "avdd",
  743. "dvdd",
  744. "pvdd",
  745. "bgvdd",
  746. "dvdd-3v",
  747. };
  748. static const char * const adv7533_supply_names[] = {
  749. "avdd",
  750. "dvdd",
  751. "pvdd",
  752. "a2vdd",
  753. "v3p3",
  754. "v1p2",
  755. };
  756. static int adv7511_init_regulators(struct adv7511 *adv)
  757. {
  758. struct device *dev = &adv->i2c_main->dev;
  759. const char * const *supply_names;
  760. unsigned int i;
  761. int ret;
  762. if (adv->type == ADV7511) {
  763. supply_names = adv7511_supply_names;
  764. adv->num_supplies = ARRAY_SIZE(adv7511_supply_names);
  765. } else {
  766. supply_names = adv7533_supply_names;
  767. adv->num_supplies = ARRAY_SIZE(adv7533_supply_names);
  768. }
  769. adv->supplies = devm_kcalloc(dev, adv->num_supplies,
  770. sizeof(*adv->supplies), GFP_KERNEL);
  771. if (!adv->supplies)
  772. return -ENOMEM;
  773. for (i = 0; i < adv->num_supplies; i++)
  774. adv->supplies[i].supply = supply_names[i];
  775. ret = devm_regulator_bulk_get(dev, adv->num_supplies, adv->supplies);
  776. if (ret)
  777. return ret;
  778. return regulator_bulk_enable(adv->num_supplies, adv->supplies);
  779. }
  780. static void adv7511_uninit_regulators(struct adv7511 *adv)
  781. {
  782. regulator_bulk_disable(adv->num_supplies, adv->supplies);
  783. }
  784. static bool adv7511_cec_register_volatile(struct device *dev, unsigned int reg)
  785. {
  786. struct i2c_client *i2c = to_i2c_client(dev);
  787. struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
  788. if (adv7511->type == ADV7533)
  789. reg -= ADV7533_REG_CEC_OFFSET;
  790. switch (reg) {
  791. case ADV7511_REG_CEC_RX_FRAME_HDR:
  792. case ADV7511_REG_CEC_RX_FRAME_DATA0...
  793. ADV7511_REG_CEC_RX_FRAME_DATA0 + 14:
  794. case ADV7511_REG_CEC_RX_FRAME_LEN:
  795. case ADV7511_REG_CEC_RX_BUFFERS:
  796. case ADV7511_REG_CEC_TX_LOW_DRV_CNT:
  797. return true;
  798. }
  799. return false;
  800. }
  801. static const struct regmap_config adv7511_cec_regmap_config = {
  802. .reg_bits = 8,
  803. .val_bits = 8,
  804. .max_register = 0xff,
  805. .cache_type = REGCACHE_RBTREE,
  806. .volatile_reg = adv7511_cec_register_volatile,
  807. };
  808. static int adv7511_init_cec_regmap(struct adv7511 *adv)
  809. {
  810. int ret;
  811. adv->i2c_cec = i2c_new_secondary_device(adv->i2c_main, "cec",
  812. ADV7511_CEC_I2C_ADDR_DEFAULT);
  813. if (!adv->i2c_cec)
  814. return -EINVAL;
  815. i2c_set_clientdata(adv->i2c_cec, adv);
  816. adv->regmap_cec = devm_regmap_init_i2c(adv->i2c_cec,
  817. &adv7511_cec_regmap_config);
  818. if (IS_ERR(adv->regmap_cec)) {
  819. ret = PTR_ERR(adv->regmap_cec);
  820. goto err;
  821. }
  822. if (adv->type == ADV7533) {
  823. ret = adv7533_patch_cec_registers(adv);
  824. if (ret)
  825. goto err;
  826. }
  827. return 0;
  828. err:
  829. i2c_unregister_device(adv->i2c_cec);
  830. return ret;
  831. }
  832. static int adv7511_parse_dt(struct device_node *np,
  833. struct adv7511_link_config *config)
  834. {
  835. const char *str;
  836. int ret;
  837. of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
  838. if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
  839. config->input_color_depth != 12)
  840. return -EINVAL;
  841. ret = of_property_read_string(np, "adi,input-colorspace", &str);
  842. if (ret < 0)
  843. return ret;
  844. if (!strcmp(str, "rgb"))
  845. config->input_colorspace = HDMI_COLORSPACE_RGB;
  846. else if (!strcmp(str, "yuv422"))
  847. config->input_colorspace = HDMI_COLORSPACE_YUV422;
  848. else if (!strcmp(str, "yuv444"))
  849. config->input_colorspace = HDMI_COLORSPACE_YUV444;
  850. else
  851. return -EINVAL;
  852. ret = of_property_read_string(np, "adi,input-clock", &str);
  853. if (ret < 0)
  854. return ret;
  855. if (!strcmp(str, "1x"))
  856. config->input_clock = ADV7511_INPUT_CLOCK_1X;
  857. else if (!strcmp(str, "2x"))
  858. config->input_clock = ADV7511_INPUT_CLOCK_2X;
  859. else if (!strcmp(str, "ddr"))
  860. config->input_clock = ADV7511_INPUT_CLOCK_DDR;
  861. else
  862. return -EINVAL;
  863. if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
  864. config->input_clock != ADV7511_INPUT_CLOCK_1X) {
  865. ret = of_property_read_u32(np, "adi,input-style",
  866. &config->input_style);
  867. if (ret)
  868. return ret;
  869. if (config->input_style < 1 || config->input_style > 3)
  870. return -EINVAL;
  871. ret = of_property_read_string(np, "adi,input-justification",
  872. &str);
  873. if (ret < 0)
  874. return ret;
  875. if (!strcmp(str, "left"))
  876. config->input_justification =
  877. ADV7511_INPUT_JUSTIFICATION_LEFT;
  878. else if (!strcmp(str, "evenly"))
  879. config->input_justification =
  880. ADV7511_INPUT_JUSTIFICATION_EVENLY;
  881. else if (!strcmp(str, "right"))
  882. config->input_justification =
  883. ADV7511_INPUT_JUSTIFICATION_RIGHT;
  884. else
  885. return -EINVAL;
  886. } else {
  887. config->input_style = 1;
  888. config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
  889. }
  890. of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
  891. if (config->clock_delay < -1200 || config->clock_delay > 1600)
  892. return -EINVAL;
  893. config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
  894. /* Hardcode the sync pulse configurations for now. */
  895. config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
  896. config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
  897. config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
  898. return 0;
  899. }
  900. static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
  901. {
  902. struct adv7511_link_config link_config;
  903. struct adv7511 *adv7511;
  904. struct device *dev = &i2c->dev;
  905. unsigned int val;
  906. int ret;
  907. if (!dev->of_node)
  908. return -EINVAL;
  909. adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
  910. if (!adv7511)
  911. return -ENOMEM;
  912. adv7511->i2c_main = i2c;
  913. adv7511->powered = false;
  914. adv7511->status = connector_status_disconnected;
  915. if (dev->of_node)
  916. adv7511->type = (enum adv7511_type)of_device_get_match_data(dev);
  917. else
  918. adv7511->type = id->driver_data;
  919. memset(&link_config, 0, sizeof(link_config));
  920. if (adv7511->type == ADV7511)
  921. ret = adv7511_parse_dt(dev->of_node, &link_config);
  922. else
  923. ret = adv7533_parse_dt(dev->of_node, adv7511);
  924. if (ret)
  925. return ret;
  926. ret = adv7511_init_regulators(adv7511);
  927. if (ret) {
  928. dev_err(dev, "failed to init regulators\n");
  929. return ret;
  930. }
  931. /*
  932. * The power down GPIO is optional. If present, toggle it from active to
  933. * inactive to wake up the encoder.
  934. */
  935. adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
  936. if (IS_ERR(adv7511->gpio_pd)) {
  937. ret = PTR_ERR(adv7511->gpio_pd);
  938. goto uninit_regulators;
  939. }
  940. if (adv7511->gpio_pd) {
  941. usleep_range(5000, 6000);
  942. gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
  943. }
  944. adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
  945. if (IS_ERR(adv7511->regmap)) {
  946. ret = PTR_ERR(adv7511->regmap);
  947. goto uninit_regulators;
  948. }
  949. ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
  950. if (ret)
  951. goto uninit_regulators;
  952. dev_dbg(dev, "Rev. %d\n", val);
  953. if (adv7511->type == ADV7511)
  954. ret = regmap_register_patch(adv7511->regmap,
  955. adv7511_fixed_registers,
  956. ARRAY_SIZE(adv7511_fixed_registers));
  957. else
  958. ret = adv7533_patch_registers(adv7511);
  959. if (ret)
  960. goto uninit_regulators;
  961. adv7511_packet_disable(adv7511, 0xffff);
  962. adv7511->i2c_edid = i2c_new_secondary_device(i2c, "edid",
  963. ADV7511_EDID_I2C_ADDR_DEFAULT);
  964. if (!adv7511->i2c_edid) {
  965. ret = -EINVAL;
  966. goto uninit_regulators;
  967. }
  968. regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
  969. adv7511->i2c_edid->addr << 1);
  970. adv7511->i2c_packet = i2c_new_secondary_device(i2c, "packet",
  971. ADV7511_PACKET_I2C_ADDR_DEFAULT);
  972. if (!adv7511->i2c_packet) {
  973. ret = -EINVAL;
  974. goto err_i2c_unregister_edid;
  975. }
  976. regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
  977. adv7511->i2c_packet->addr << 1);
  978. ret = adv7511_init_cec_regmap(adv7511);
  979. if (ret)
  980. goto err_i2c_unregister_packet;
  981. regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR,
  982. adv7511->i2c_cec->addr << 1);
  983. INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
  984. if (i2c->irq) {
  985. init_waitqueue_head(&adv7511->wq);
  986. ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
  987. adv7511_irq_handler,
  988. IRQF_ONESHOT, dev_name(dev),
  989. adv7511);
  990. if (ret)
  991. goto err_unregister_cec;
  992. }
  993. adv7511_power_off(adv7511);
  994. i2c_set_clientdata(i2c, adv7511);
  995. if (adv7511->type == ADV7511)
  996. adv7511_set_link_config(adv7511, &link_config);
  997. ret = adv7511_cec_init(dev, adv7511);
  998. if (ret)
  999. goto err_unregister_cec;
  1000. adv7511->bridge.funcs = &adv7511_bridge_funcs;
  1001. adv7511->bridge.of_node = dev->of_node;
  1002. drm_bridge_add(&adv7511->bridge);
  1003. adv7511_audio_init(dev, adv7511);
  1004. return 0;
  1005. err_unregister_cec:
  1006. i2c_unregister_device(adv7511->i2c_cec);
  1007. if (adv7511->cec_clk)
  1008. clk_disable_unprepare(adv7511->cec_clk);
  1009. err_i2c_unregister_packet:
  1010. i2c_unregister_device(adv7511->i2c_packet);
  1011. err_i2c_unregister_edid:
  1012. i2c_unregister_device(adv7511->i2c_edid);
  1013. uninit_regulators:
  1014. adv7511_uninit_regulators(adv7511);
  1015. return ret;
  1016. }
  1017. static int adv7511_remove(struct i2c_client *i2c)
  1018. {
  1019. struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
  1020. if (adv7511->type == ADV7533)
  1021. adv7533_detach_dsi(adv7511);
  1022. i2c_unregister_device(adv7511->i2c_cec);
  1023. if (adv7511->cec_clk)
  1024. clk_disable_unprepare(adv7511->cec_clk);
  1025. adv7511_uninit_regulators(adv7511);
  1026. drm_bridge_remove(&adv7511->bridge);
  1027. adv7511_audio_exit(adv7511);
  1028. cec_unregister_adapter(adv7511->cec_adap);
  1029. i2c_unregister_device(adv7511->i2c_packet);
  1030. i2c_unregister_device(adv7511->i2c_edid);
  1031. return 0;
  1032. }
  1033. static const struct i2c_device_id adv7511_i2c_ids[] = {
  1034. { "adv7511", ADV7511 },
  1035. { "adv7511w", ADV7511 },
  1036. { "adv7513", ADV7511 },
  1037. #ifdef CONFIG_DRM_I2C_ADV7533
  1038. { "adv7533", ADV7533 },
  1039. #endif
  1040. { }
  1041. };
  1042. MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
  1043. static const struct of_device_id adv7511_of_ids[] = {
  1044. { .compatible = "adi,adv7511", .data = (void *)ADV7511 },
  1045. { .compatible = "adi,adv7511w", .data = (void *)ADV7511 },
  1046. { .compatible = "adi,adv7513", .data = (void *)ADV7511 },
  1047. #ifdef CONFIG_DRM_I2C_ADV7533
  1048. { .compatible = "adi,adv7533", .data = (void *)ADV7533 },
  1049. #endif
  1050. { }
  1051. };
  1052. MODULE_DEVICE_TABLE(of, adv7511_of_ids);
  1053. static struct mipi_dsi_driver adv7533_dsi_driver = {
  1054. .driver.name = "adv7533",
  1055. };
  1056. static struct i2c_driver adv7511_driver = {
  1057. .driver = {
  1058. .name = "adv7511",
  1059. .of_match_table = adv7511_of_ids,
  1060. },
  1061. .id_table = adv7511_i2c_ids,
  1062. .probe = adv7511_probe,
  1063. .remove = adv7511_remove,
  1064. };
  1065. static int __init adv7511_init(void)
  1066. {
  1067. if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
  1068. mipi_dsi_driver_register(&adv7533_dsi_driver);
  1069. return i2c_add_driver(&adv7511_driver);
  1070. }
  1071. module_init(adv7511_init);
  1072. static void __exit adv7511_exit(void)
  1073. {
  1074. i2c_del_driver(&adv7511_driver);
  1075. if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
  1076. mipi_dsi_driver_unregister(&adv7533_dsi_driver);
  1077. }
  1078. module_exit(adv7511_exit);
  1079. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  1080. MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
  1081. MODULE_LICENSE("GPL");