adv7511.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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/i2c.h>
  11. #include <linux/module.h>
  12. #include <linux/regmap.h>
  13. #include <linux/slab.h>
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include <drm/drm_edid.h>
  17. #include <drm/drm_encoder_slave.h>
  18. #include "adv7511.h"
  19. struct adv7511 {
  20. struct i2c_client *i2c_main;
  21. struct i2c_client *i2c_edid;
  22. struct regmap *regmap;
  23. struct regmap *packet_memory_regmap;
  24. enum drm_connector_status status;
  25. bool powered;
  26. unsigned int f_tmds;
  27. unsigned int current_edid_segment;
  28. uint8_t edid_buf[256];
  29. bool edid_read;
  30. wait_queue_head_t wq;
  31. struct drm_encoder *encoder;
  32. bool embedded_sync;
  33. enum adv7511_sync_polarity vsync_polarity;
  34. enum adv7511_sync_polarity hsync_polarity;
  35. bool rgb;
  36. struct edid *edid;
  37. struct gpio_desc *gpio_pd;
  38. };
  39. static struct adv7511 *encoder_to_adv7511(struct drm_encoder *encoder)
  40. {
  41. return to_encoder_slave(encoder)->slave_priv;
  42. }
  43. /* ADI recommended values for proper operation. */
  44. static const struct reg_sequence adv7511_fixed_registers[] = {
  45. { 0x98, 0x03 },
  46. { 0x9a, 0xe0 },
  47. { 0x9c, 0x30 },
  48. { 0x9d, 0x61 },
  49. { 0xa2, 0xa4 },
  50. { 0xa3, 0xa4 },
  51. { 0xe0, 0xd0 },
  52. { 0xf9, 0x00 },
  53. { 0x55, 0x02 },
  54. };
  55. /* -----------------------------------------------------------------------------
  56. * Register access
  57. */
  58. static const uint8_t adv7511_register_defaults[] = {
  59. 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
  60. 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
  61. 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
  62. 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
  63. 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
  64. 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
  66. 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
  67. 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
  68. 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73. 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
  78. 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
  79. 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
  80. 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
  81. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
  82. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
  84. 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
  85. 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
  86. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
  87. 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
  90. 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  91. };
  92. static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
  93. {
  94. switch (reg) {
  95. case ADV7511_REG_CHIP_REVISION:
  96. case ADV7511_REG_SPDIF_FREQ:
  97. case ADV7511_REG_CTS_AUTOMATIC1:
  98. case ADV7511_REG_CTS_AUTOMATIC2:
  99. case ADV7511_REG_VIC_DETECTED:
  100. case ADV7511_REG_VIC_SEND:
  101. case ADV7511_REG_AUX_VIC_DETECTED:
  102. case ADV7511_REG_STATUS:
  103. case ADV7511_REG_GC(1):
  104. case ADV7511_REG_INT(0):
  105. case ADV7511_REG_INT(1):
  106. case ADV7511_REG_PLL_STATUS:
  107. case ADV7511_REG_AN(0):
  108. case ADV7511_REG_AN(1):
  109. case ADV7511_REG_AN(2):
  110. case ADV7511_REG_AN(3):
  111. case ADV7511_REG_AN(4):
  112. case ADV7511_REG_AN(5):
  113. case ADV7511_REG_AN(6):
  114. case ADV7511_REG_AN(7):
  115. case ADV7511_REG_HDCP_STATUS:
  116. case ADV7511_REG_BCAPS:
  117. case ADV7511_REG_BKSV(0):
  118. case ADV7511_REG_BKSV(1):
  119. case ADV7511_REG_BKSV(2):
  120. case ADV7511_REG_BKSV(3):
  121. case ADV7511_REG_BKSV(4):
  122. case ADV7511_REG_DDC_STATUS:
  123. case ADV7511_REG_EDID_READ_CTRL:
  124. case ADV7511_REG_BSTATUS(0):
  125. case ADV7511_REG_BSTATUS(1):
  126. case ADV7511_REG_CHIP_ID_HIGH:
  127. case ADV7511_REG_CHIP_ID_LOW:
  128. return true;
  129. }
  130. return false;
  131. }
  132. static const struct regmap_config adv7511_regmap_config = {
  133. .reg_bits = 8,
  134. .val_bits = 8,
  135. .max_register = 0xff,
  136. .cache_type = REGCACHE_RBTREE,
  137. .reg_defaults_raw = adv7511_register_defaults,
  138. .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
  139. .volatile_reg = adv7511_register_volatile,
  140. };
  141. /* -----------------------------------------------------------------------------
  142. * Hardware configuration
  143. */
  144. static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
  145. const uint16_t *coeff,
  146. unsigned int scaling_factor)
  147. {
  148. unsigned int i;
  149. regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
  150. ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
  151. if (enable) {
  152. for (i = 0; i < 12; ++i) {
  153. regmap_update_bits(adv7511->regmap,
  154. ADV7511_REG_CSC_UPPER(i),
  155. 0x1f, coeff[i] >> 8);
  156. regmap_write(adv7511->regmap,
  157. ADV7511_REG_CSC_LOWER(i),
  158. coeff[i] & 0xff);
  159. }
  160. }
  161. if (enable)
  162. regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
  163. 0xe0, 0x80 | (scaling_factor << 5));
  164. else
  165. regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
  166. 0x80, 0x00);
  167. regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
  168. ADV7511_CSC_UPDATE_MODE, 0);
  169. }
  170. static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
  171. {
  172. if (packet & 0xff)
  173. regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
  174. packet, 0xff);
  175. if (packet & 0xff00) {
  176. packet >>= 8;
  177. regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
  178. packet, 0xff);
  179. }
  180. return 0;
  181. }
  182. static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
  183. {
  184. if (packet & 0xff)
  185. regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
  186. packet, 0x00);
  187. if (packet & 0xff00) {
  188. packet >>= 8;
  189. regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
  190. packet, 0x00);
  191. }
  192. return 0;
  193. }
  194. /* Coefficients for adv7511 color space conversion */
  195. static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
  196. 0x0734, 0x04ad, 0x0000, 0x1c1b,
  197. 0x1ddc, 0x04ad, 0x1f24, 0x0135,
  198. 0x0000, 0x04ad, 0x087c, 0x1b77,
  199. };
  200. static void adv7511_set_config_csc(struct adv7511 *adv7511,
  201. struct drm_connector *connector,
  202. bool rgb)
  203. {
  204. struct adv7511_video_config config;
  205. bool output_format_422, output_format_ycbcr;
  206. unsigned int mode;
  207. uint8_t infoframe[17];
  208. if (adv7511->edid)
  209. config.hdmi_mode = drm_detect_hdmi_monitor(adv7511->edid);
  210. else
  211. config.hdmi_mode = false;
  212. hdmi_avi_infoframe_init(&config.avi_infoframe);
  213. config.avi_infoframe.scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
  214. if (rgb) {
  215. config.csc_enable = false;
  216. config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
  217. } else {
  218. config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
  219. config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
  220. if ((connector->display_info.color_formats &
  221. DRM_COLOR_FORMAT_YCRCB422) &&
  222. config.hdmi_mode) {
  223. config.csc_enable = false;
  224. config.avi_infoframe.colorspace =
  225. HDMI_COLORSPACE_YUV422;
  226. } else {
  227. config.csc_enable = true;
  228. config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
  229. }
  230. }
  231. if (config.hdmi_mode) {
  232. mode = ADV7511_HDMI_CFG_MODE_HDMI;
  233. switch (config.avi_infoframe.colorspace) {
  234. case HDMI_COLORSPACE_YUV444:
  235. output_format_422 = false;
  236. output_format_ycbcr = true;
  237. break;
  238. case HDMI_COLORSPACE_YUV422:
  239. output_format_422 = true;
  240. output_format_ycbcr = true;
  241. break;
  242. default:
  243. output_format_422 = false;
  244. output_format_ycbcr = false;
  245. break;
  246. }
  247. } else {
  248. mode = ADV7511_HDMI_CFG_MODE_DVI;
  249. output_format_422 = false;
  250. output_format_ycbcr = false;
  251. }
  252. adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
  253. adv7511_set_colormap(adv7511, config.csc_enable,
  254. config.csc_coefficents,
  255. config.csc_scaling_factor);
  256. regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
  257. (output_format_422 << 7) | output_format_ycbcr);
  258. regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
  259. ADV7511_HDMI_CFG_MODE_MASK, mode);
  260. hdmi_avi_infoframe_pack(&config.avi_infoframe, infoframe,
  261. sizeof(infoframe));
  262. /* The AVI infoframe id is not configurable */
  263. regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
  264. infoframe + 1, sizeof(infoframe) - 1);
  265. adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
  266. }
  267. static void adv7511_set_link_config(struct adv7511 *adv7511,
  268. const struct adv7511_link_config *config)
  269. {
  270. /*
  271. * The input style values documented in the datasheet don't match the
  272. * hardware register field values :-(
  273. */
  274. static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
  275. unsigned int clock_delay;
  276. unsigned int color_depth;
  277. unsigned int input_id;
  278. clock_delay = (config->clock_delay + 1200) / 400;
  279. color_depth = config->input_color_depth == 8 ? 3
  280. : (config->input_color_depth == 10 ? 1 : 2);
  281. /* TODO Support input ID 6 */
  282. if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
  283. input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
  284. ? 5 : 0;
  285. else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
  286. input_id = config->embedded_sync ? 8 : 7;
  287. else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
  288. input_id = config->embedded_sync ? 4 : 3;
  289. else
  290. input_id = config->embedded_sync ? 2 : 1;
  291. regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
  292. input_id);
  293. regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
  294. (color_depth << 4) |
  295. (input_styles[config->input_style] << 2));
  296. regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
  297. config->input_justification << 3);
  298. regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
  299. config->sync_pulse << 2);
  300. regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
  301. adv7511->embedded_sync = config->embedded_sync;
  302. adv7511->hsync_polarity = config->hsync_polarity;
  303. adv7511->vsync_polarity = config->vsync_polarity;
  304. adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
  305. }
  306. static void adv7511_power_on(struct adv7511 *adv7511)
  307. {
  308. adv7511->current_edid_segment = -1;
  309. regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
  310. ADV7511_POWER_POWER_DOWN, 0);
  311. if (adv7511->i2c_main->irq) {
  312. /*
  313. * Documentation says the INT_ENABLE registers are reset in
  314. * POWER_DOWN mode. My 7511w preserved the bits, however.
  315. * Still, let's be safe and stick to the documentation.
  316. */
  317. regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
  318. ADV7511_INT0_EDID_READY);
  319. regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(1),
  320. ADV7511_INT1_DDC_ERROR);
  321. }
  322. /*
  323. * Per spec it is allowed to pulse the HPD signal to indicate that the
  324. * EDID information has changed. Some monitors do this when they wakeup
  325. * from standby or are enabled. When the HPD goes low the adv7511 is
  326. * reset and the outputs are disabled which might cause the monitor to
  327. * go to standby again. To avoid this we ignore the HPD pin for the
  328. * first few seconds after enabling the output.
  329. */
  330. regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
  331. ADV7511_REG_POWER2_HPD_SRC_MASK,
  332. ADV7511_REG_POWER2_HPD_SRC_NONE);
  333. /*
  334. * Most of the registers are reset during power down or when HPD is low.
  335. */
  336. regcache_sync(adv7511->regmap);
  337. adv7511->powered = true;
  338. }
  339. static void adv7511_power_off(struct adv7511 *adv7511)
  340. {
  341. /* TODO: setup additional power down modes */
  342. regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
  343. ADV7511_POWER_POWER_DOWN,
  344. ADV7511_POWER_POWER_DOWN);
  345. regcache_mark_dirty(adv7511->regmap);
  346. adv7511->powered = false;
  347. }
  348. /* -----------------------------------------------------------------------------
  349. * Interrupt and hotplug detection
  350. */
  351. static bool adv7511_hpd(struct adv7511 *adv7511)
  352. {
  353. unsigned int irq0;
  354. int ret;
  355. ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
  356. if (ret < 0)
  357. return false;
  358. if (irq0 & ADV7511_INT0_HPD) {
  359. regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
  360. ADV7511_INT0_HPD);
  361. return true;
  362. }
  363. return false;
  364. }
  365. static int adv7511_irq_process(struct adv7511 *adv7511)
  366. {
  367. unsigned int irq0, irq1;
  368. int ret;
  369. ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
  370. if (ret < 0)
  371. return ret;
  372. ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
  373. if (ret < 0)
  374. return ret;
  375. regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
  376. regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
  377. if (irq0 & ADV7511_INT0_HPD && adv7511->encoder)
  378. drm_helper_hpd_irq_event(adv7511->encoder->dev);
  379. if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
  380. adv7511->edid_read = true;
  381. if (adv7511->i2c_main->irq)
  382. wake_up_all(&adv7511->wq);
  383. }
  384. return 0;
  385. }
  386. static irqreturn_t adv7511_irq_handler(int irq, void *devid)
  387. {
  388. struct adv7511 *adv7511 = devid;
  389. int ret;
  390. ret = adv7511_irq_process(adv7511);
  391. return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
  392. }
  393. /* -----------------------------------------------------------------------------
  394. * EDID retrieval
  395. */
  396. static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
  397. {
  398. int ret;
  399. if (adv7511->i2c_main->irq) {
  400. ret = wait_event_interruptible_timeout(adv7511->wq,
  401. adv7511->edid_read, msecs_to_jiffies(timeout));
  402. } else {
  403. for (; timeout > 0; timeout -= 25) {
  404. ret = adv7511_irq_process(adv7511);
  405. if (ret < 0)
  406. break;
  407. if (adv7511->edid_read)
  408. break;
  409. msleep(25);
  410. }
  411. }
  412. return adv7511->edid_read ? 0 : -EIO;
  413. }
  414. static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
  415. size_t len)
  416. {
  417. struct adv7511 *adv7511 = data;
  418. struct i2c_msg xfer[2];
  419. uint8_t offset;
  420. unsigned int i;
  421. int ret;
  422. if (len > 128)
  423. return -EINVAL;
  424. if (adv7511->current_edid_segment != block / 2) {
  425. unsigned int status;
  426. ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
  427. &status);
  428. if (ret < 0)
  429. return ret;
  430. if (status != 2) {
  431. adv7511->edid_read = false;
  432. regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
  433. block);
  434. ret = adv7511_wait_for_edid(adv7511, 200);
  435. if (ret < 0)
  436. return ret;
  437. }
  438. /* Break this apart, hopefully more I2C controllers will
  439. * support 64 byte transfers than 256 byte transfers
  440. */
  441. xfer[0].addr = adv7511->i2c_edid->addr;
  442. xfer[0].flags = 0;
  443. xfer[0].len = 1;
  444. xfer[0].buf = &offset;
  445. xfer[1].addr = adv7511->i2c_edid->addr;
  446. xfer[1].flags = I2C_M_RD;
  447. xfer[1].len = 64;
  448. xfer[1].buf = adv7511->edid_buf;
  449. offset = 0;
  450. for (i = 0; i < 4; ++i) {
  451. ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
  452. ARRAY_SIZE(xfer));
  453. if (ret < 0)
  454. return ret;
  455. else if (ret != 2)
  456. return -EIO;
  457. xfer[1].buf += 64;
  458. offset += 64;
  459. }
  460. adv7511->current_edid_segment = block / 2;
  461. }
  462. if (block % 2 == 0)
  463. memcpy(buf, adv7511->edid_buf, len);
  464. else
  465. memcpy(buf, adv7511->edid_buf + 128, len);
  466. return 0;
  467. }
  468. /* -----------------------------------------------------------------------------
  469. * Encoder operations
  470. */
  471. static int adv7511_get_modes(struct drm_encoder *encoder,
  472. struct drm_connector *connector)
  473. {
  474. struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
  475. struct edid *edid;
  476. unsigned int count;
  477. /* Reading the EDID only works if the device is powered */
  478. if (!adv7511->powered) {
  479. regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
  480. ADV7511_POWER_POWER_DOWN, 0);
  481. if (adv7511->i2c_main->irq) {
  482. regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
  483. ADV7511_INT0_EDID_READY);
  484. regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(1),
  485. ADV7511_INT1_DDC_ERROR);
  486. }
  487. adv7511->current_edid_segment = -1;
  488. }
  489. edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
  490. if (!adv7511->powered)
  491. regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
  492. ADV7511_POWER_POWER_DOWN,
  493. ADV7511_POWER_POWER_DOWN);
  494. kfree(adv7511->edid);
  495. adv7511->edid = edid;
  496. if (!edid)
  497. return 0;
  498. drm_mode_connector_update_edid_property(connector, edid);
  499. count = drm_add_edid_modes(connector, edid);
  500. adv7511_set_config_csc(adv7511, connector, adv7511->rgb);
  501. return count;
  502. }
  503. static void adv7511_encoder_dpms(struct drm_encoder *encoder, int mode)
  504. {
  505. struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
  506. if (mode == DRM_MODE_DPMS_ON)
  507. adv7511_power_on(adv7511);
  508. else
  509. adv7511_power_off(adv7511);
  510. }
  511. static enum drm_connector_status
  512. adv7511_encoder_detect(struct drm_encoder *encoder,
  513. struct drm_connector *connector)
  514. {
  515. struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
  516. enum drm_connector_status status;
  517. unsigned int val;
  518. bool hpd;
  519. int ret;
  520. ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
  521. if (ret < 0)
  522. return connector_status_disconnected;
  523. if (val & ADV7511_STATUS_HPD)
  524. status = connector_status_connected;
  525. else
  526. status = connector_status_disconnected;
  527. hpd = adv7511_hpd(adv7511);
  528. /* The chip resets itself when the cable is disconnected, so in case
  529. * there is a pending HPD interrupt and the cable is connected there was
  530. * at least one transition from disconnected to connected and the chip
  531. * has to be reinitialized. */
  532. if (status == connector_status_connected && hpd && adv7511->powered) {
  533. regcache_mark_dirty(adv7511->regmap);
  534. adv7511_power_on(adv7511);
  535. adv7511_get_modes(encoder, connector);
  536. if (adv7511->status == connector_status_connected)
  537. status = connector_status_disconnected;
  538. } else {
  539. /* Renable HPD sensing */
  540. regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
  541. ADV7511_REG_POWER2_HPD_SRC_MASK,
  542. ADV7511_REG_POWER2_HPD_SRC_BOTH);
  543. }
  544. adv7511->status = status;
  545. return status;
  546. }
  547. static int adv7511_encoder_mode_valid(struct drm_encoder *encoder,
  548. struct drm_display_mode *mode)
  549. {
  550. if (mode->clock > 165000)
  551. return MODE_CLOCK_HIGH;
  552. return MODE_OK;
  553. }
  554. static void adv7511_encoder_mode_set(struct drm_encoder *encoder,
  555. struct drm_display_mode *mode,
  556. struct drm_display_mode *adj_mode)
  557. {
  558. struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
  559. unsigned int low_refresh_rate;
  560. unsigned int hsync_polarity = 0;
  561. unsigned int vsync_polarity = 0;
  562. if (adv7511->embedded_sync) {
  563. unsigned int hsync_offset, hsync_len;
  564. unsigned int vsync_offset, vsync_len;
  565. hsync_offset = adj_mode->crtc_hsync_start -
  566. adj_mode->crtc_hdisplay;
  567. vsync_offset = adj_mode->crtc_vsync_start -
  568. adj_mode->crtc_vdisplay;
  569. hsync_len = adj_mode->crtc_hsync_end -
  570. adj_mode->crtc_hsync_start;
  571. vsync_len = adj_mode->crtc_vsync_end -
  572. adj_mode->crtc_vsync_start;
  573. /* The hardware vsync generator has a off-by-one bug */
  574. vsync_offset += 1;
  575. regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
  576. ((hsync_offset >> 10) & 0x7) << 5);
  577. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
  578. (hsync_offset >> 2) & 0xff);
  579. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
  580. ((hsync_offset & 0x3) << 6) |
  581. ((hsync_len >> 4) & 0x3f));
  582. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
  583. ((hsync_len & 0xf) << 4) |
  584. ((vsync_offset >> 6) & 0xf));
  585. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
  586. ((vsync_offset & 0x3f) << 2) |
  587. ((vsync_len >> 8) & 0x3));
  588. regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
  589. vsync_len & 0xff);
  590. hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
  591. vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
  592. } else {
  593. enum adv7511_sync_polarity mode_hsync_polarity;
  594. enum adv7511_sync_polarity mode_vsync_polarity;
  595. /**
  596. * If the input signal is always low or always high we want to
  597. * invert or let it passthrough depending on the polarity of the
  598. * current mode.
  599. **/
  600. if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
  601. mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
  602. else
  603. mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
  604. if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
  605. mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
  606. else
  607. mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
  608. if (adv7511->hsync_polarity != mode_hsync_polarity &&
  609. adv7511->hsync_polarity !=
  610. ADV7511_SYNC_POLARITY_PASSTHROUGH)
  611. hsync_polarity = 1;
  612. if (adv7511->vsync_polarity != mode_vsync_polarity &&
  613. adv7511->vsync_polarity !=
  614. ADV7511_SYNC_POLARITY_PASSTHROUGH)
  615. vsync_polarity = 1;
  616. }
  617. if (mode->vrefresh <= 24000)
  618. low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
  619. else if (mode->vrefresh <= 25000)
  620. low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
  621. else if (mode->vrefresh <= 30000)
  622. low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
  623. else
  624. low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
  625. regmap_update_bits(adv7511->regmap, 0xfb,
  626. 0x6, low_refresh_rate << 1);
  627. regmap_update_bits(adv7511->regmap, 0x17,
  628. 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
  629. /*
  630. * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
  631. * supposed to give better results.
  632. */
  633. adv7511->f_tmds = mode->clock;
  634. }
  635. static const struct drm_encoder_slave_funcs adv7511_encoder_funcs = {
  636. .dpms = adv7511_encoder_dpms,
  637. .mode_valid = adv7511_encoder_mode_valid,
  638. .mode_set = adv7511_encoder_mode_set,
  639. .detect = adv7511_encoder_detect,
  640. .get_modes = adv7511_get_modes,
  641. };
  642. /* -----------------------------------------------------------------------------
  643. * Probe & remove
  644. */
  645. static int adv7511_parse_dt(struct device_node *np,
  646. struct adv7511_link_config *config)
  647. {
  648. const char *str;
  649. int ret;
  650. memset(config, 0, sizeof(*config));
  651. of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
  652. if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
  653. config->input_color_depth != 12)
  654. return -EINVAL;
  655. ret = of_property_read_string(np, "adi,input-colorspace", &str);
  656. if (ret < 0)
  657. return ret;
  658. if (!strcmp(str, "rgb"))
  659. config->input_colorspace = HDMI_COLORSPACE_RGB;
  660. else if (!strcmp(str, "yuv422"))
  661. config->input_colorspace = HDMI_COLORSPACE_YUV422;
  662. else if (!strcmp(str, "yuv444"))
  663. config->input_colorspace = HDMI_COLORSPACE_YUV444;
  664. else
  665. return -EINVAL;
  666. ret = of_property_read_string(np, "adi,input-clock", &str);
  667. if (ret < 0)
  668. return ret;
  669. if (!strcmp(str, "1x"))
  670. config->input_clock = ADV7511_INPUT_CLOCK_1X;
  671. else if (!strcmp(str, "2x"))
  672. config->input_clock = ADV7511_INPUT_CLOCK_2X;
  673. else if (!strcmp(str, "ddr"))
  674. config->input_clock = ADV7511_INPUT_CLOCK_DDR;
  675. else
  676. return -EINVAL;
  677. if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
  678. config->input_clock != ADV7511_INPUT_CLOCK_1X) {
  679. ret = of_property_read_u32(np, "adi,input-style",
  680. &config->input_style);
  681. if (ret)
  682. return ret;
  683. if (config->input_style < 1 || config->input_style > 3)
  684. return -EINVAL;
  685. ret = of_property_read_string(np, "adi,input-justification",
  686. &str);
  687. if (ret < 0)
  688. return ret;
  689. if (!strcmp(str, "left"))
  690. config->input_justification =
  691. ADV7511_INPUT_JUSTIFICATION_LEFT;
  692. else if (!strcmp(str, "evenly"))
  693. config->input_justification =
  694. ADV7511_INPUT_JUSTIFICATION_EVENLY;
  695. else if (!strcmp(str, "right"))
  696. config->input_justification =
  697. ADV7511_INPUT_JUSTIFICATION_RIGHT;
  698. else
  699. return -EINVAL;
  700. } else {
  701. config->input_style = 1;
  702. config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
  703. }
  704. of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
  705. if (config->clock_delay < -1200 || config->clock_delay > 1600)
  706. return -EINVAL;
  707. config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
  708. /* Hardcode the sync pulse configurations for now. */
  709. config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
  710. config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
  711. config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
  712. return 0;
  713. }
  714. static const int edid_i2c_addr = 0x7e;
  715. static const int packet_i2c_addr = 0x70;
  716. static const int cec_i2c_addr = 0x78;
  717. static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
  718. {
  719. struct adv7511_link_config link_config;
  720. struct adv7511 *adv7511;
  721. struct device *dev = &i2c->dev;
  722. unsigned int val;
  723. int ret;
  724. if (!dev->of_node)
  725. return -EINVAL;
  726. adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
  727. if (!adv7511)
  728. return -ENOMEM;
  729. adv7511->powered = false;
  730. adv7511->status = connector_status_disconnected;
  731. ret = adv7511_parse_dt(dev->of_node, &link_config);
  732. if (ret)
  733. return ret;
  734. /*
  735. * The power down GPIO is optional. If present, toggle it from active to
  736. * inactive to wake up the encoder.
  737. */
  738. adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
  739. if (IS_ERR(adv7511->gpio_pd))
  740. return PTR_ERR(adv7511->gpio_pd);
  741. if (adv7511->gpio_pd) {
  742. mdelay(5);
  743. gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
  744. }
  745. adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
  746. if (IS_ERR(adv7511->regmap))
  747. return PTR_ERR(adv7511->regmap);
  748. ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
  749. if (ret)
  750. return ret;
  751. dev_dbg(dev, "Rev. %d\n", val);
  752. ret = regmap_register_patch(adv7511->regmap, adv7511_fixed_registers,
  753. ARRAY_SIZE(adv7511_fixed_registers));
  754. if (ret)
  755. return ret;
  756. regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
  757. regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
  758. packet_i2c_addr);
  759. regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR, cec_i2c_addr);
  760. adv7511_packet_disable(adv7511, 0xffff);
  761. adv7511->i2c_main = i2c;
  762. adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
  763. if (!adv7511->i2c_edid)
  764. return -ENOMEM;
  765. if (i2c->irq) {
  766. init_waitqueue_head(&adv7511->wq);
  767. ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
  768. adv7511_irq_handler,
  769. IRQF_ONESHOT, dev_name(dev),
  770. adv7511);
  771. if (ret)
  772. goto err_i2c_unregister_device;
  773. }
  774. /* CEC is unused for now */
  775. regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
  776. ADV7511_CEC_CTRL_POWER_DOWN);
  777. adv7511_power_off(adv7511);
  778. i2c_set_clientdata(i2c, adv7511);
  779. adv7511_set_link_config(adv7511, &link_config);
  780. return 0;
  781. err_i2c_unregister_device:
  782. i2c_unregister_device(adv7511->i2c_edid);
  783. return ret;
  784. }
  785. static int adv7511_remove(struct i2c_client *i2c)
  786. {
  787. struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
  788. i2c_unregister_device(adv7511->i2c_edid);
  789. kfree(adv7511->edid);
  790. return 0;
  791. }
  792. static int adv7511_encoder_init(struct i2c_client *i2c, struct drm_device *dev,
  793. struct drm_encoder_slave *encoder)
  794. {
  795. struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
  796. encoder->slave_priv = adv7511;
  797. encoder->slave_funcs = &adv7511_encoder_funcs;
  798. adv7511->encoder = &encoder->base;
  799. return 0;
  800. }
  801. static const struct i2c_device_id adv7511_i2c_ids[] = {
  802. { "adv7511", 0 },
  803. { "adv7511w", 0 },
  804. { "adv7513", 0 },
  805. { }
  806. };
  807. MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
  808. static const struct of_device_id adv7511_of_ids[] = {
  809. { .compatible = "adi,adv7511", },
  810. { .compatible = "adi,adv7511w", },
  811. { .compatible = "adi,adv7513", },
  812. { }
  813. };
  814. MODULE_DEVICE_TABLE(of, adv7511_of_ids);
  815. static struct drm_i2c_encoder_driver adv7511_driver = {
  816. .i2c_driver = {
  817. .driver = {
  818. .name = "adv7511",
  819. .of_match_table = adv7511_of_ids,
  820. },
  821. .id_table = adv7511_i2c_ids,
  822. .probe = adv7511_probe,
  823. .remove = adv7511_remove,
  824. },
  825. .encoder_init = adv7511_encoder_init,
  826. };
  827. static int __init adv7511_init(void)
  828. {
  829. return drm_i2c_encoder_register(THIS_MODULE, &adv7511_driver);
  830. }
  831. module_init(adv7511_init);
  832. static void __exit adv7511_exit(void)
  833. {
  834. drm_i2c_encoder_unregister(&adv7511_driver);
  835. }
  836. module_exit(adv7511_exit);
  837. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  838. MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
  839. MODULE_LICENSE("GPL");