adv7511.c 27 KB

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