adv7511.c 27 KB

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