2 * Analog Devices ADV7511 HDMI transmitter driver
4 * Copyright 2012 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/device.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/slab.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_edid.h>
19 #include <drm/drm_encoder_slave.h>
24 struct i2c_client *i2c_main;
25 struct i2c_client *i2c_edid;
27 struct regmap *regmap;
28 struct regmap *packet_memory_regmap;
29 enum drm_connector_status status;
34 unsigned int current_edid_segment;
35 uint8_t edid_buf[256];
38 struct drm_encoder *encoder;
41 enum adv7511_sync_polarity vsync_polarity;
42 enum adv7511_sync_polarity hsync_polarity;
47 struct gpio_desc *gpio_pd;
50 static struct adv7511 *encoder_to_adv7511(struct drm_encoder *encoder)
52 return to_encoder_slave(encoder)->slave_priv;
55 /* ADI recommended values for proper operation. */
56 static const struct reg_default adv7511_fixed_registers[] = {
68 /* -----------------------------------------------------------------------------
72 static const uint8_t adv7511_register_defaults[] = {
73 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
74 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
75 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
76 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
77 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
78 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
80 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
81 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
82 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
88 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
90 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
92 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
93 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
94 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
95 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
98 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
99 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
101 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
104 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
107 static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
110 case ADV7511_REG_CHIP_REVISION:
111 case ADV7511_REG_SPDIF_FREQ:
112 case ADV7511_REG_CTS_AUTOMATIC1:
113 case ADV7511_REG_CTS_AUTOMATIC2:
114 case ADV7511_REG_VIC_DETECTED:
115 case ADV7511_REG_VIC_SEND:
116 case ADV7511_REG_AUX_VIC_DETECTED:
117 case ADV7511_REG_STATUS:
118 case ADV7511_REG_GC(1):
119 case ADV7511_REG_INT(0):
120 case ADV7511_REG_INT(1):
121 case ADV7511_REG_PLL_STATUS:
122 case ADV7511_REG_AN(0):
123 case ADV7511_REG_AN(1):
124 case ADV7511_REG_AN(2):
125 case ADV7511_REG_AN(3):
126 case ADV7511_REG_AN(4):
127 case ADV7511_REG_AN(5):
128 case ADV7511_REG_AN(6):
129 case ADV7511_REG_AN(7):
130 case ADV7511_REG_HDCP_STATUS:
131 case ADV7511_REG_BCAPS:
132 case ADV7511_REG_BKSV(0):
133 case ADV7511_REG_BKSV(1):
134 case ADV7511_REG_BKSV(2):
135 case ADV7511_REG_BKSV(3):
136 case ADV7511_REG_BKSV(4):
137 case ADV7511_REG_DDC_STATUS:
138 case ADV7511_REG_BSTATUS(0):
139 case ADV7511_REG_BSTATUS(1):
140 case ADV7511_REG_CHIP_ID_HIGH:
141 case ADV7511_REG_CHIP_ID_LOW:
148 static const struct regmap_config adv7511_regmap_config = {
152 .max_register = 0xff,
153 .cache_type = REGCACHE_RBTREE,
154 .reg_defaults_raw = adv7511_register_defaults,
155 .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
157 .volatile_reg = adv7511_register_volatile,
160 /* -----------------------------------------------------------------------------
161 * Hardware configuration
164 static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
165 const uint16_t *coeff,
166 unsigned int scaling_factor)
170 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
171 ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
174 for (i = 0; i < 12; ++i) {
175 regmap_update_bits(adv7511->regmap,
176 ADV7511_REG_CSC_UPPER(i),
177 0x1f, coeff[i] >> 8);
178 regmap_write(adv7511->regmap,
179 ADV7511_REG_CSC_LOWER(i),
185 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
186 0xe0, 0x80 | (scaling_factor << 5));
188 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
191 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
192 ADV7511_CSC_UPDATE_MODE, 0);
195 static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
198 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
201 if (packet & 0xff00) {
203 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
210 static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
213 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
216 if (packet & 0xff00) {
218 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
225 /* Coefficients for adv7511 color space conversion */
226 static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
227 0x0734, 0x04ad, 0x0000, 0x1c1b,
228 0x1ddc, 0x04ad, 0x1f24, 0x0135,
229 0x0000, 0x04ad, 0x087c, 0x1b77,
232 static void adv7511_set_config_csc(struct adv7511 *adv7511,
233 struct drm_connector *connector,
236 struct adv7511_video_config config;
237 bool output_format_422, output_format_ycbcr;
239 uint8_t infoframe[17];
242 config.hdmi_mode = drm_detect_hdmi_monitor(adv7511->edid);
244 config.hdmi_mode = false;
246 hdmi_avi_infoframe_init(&config.avi_infoframe);
248 config.avi_infoframe.scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
251 config.csc_enable = false;
252 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
254 config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
255 config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
257 if ((connector->display_info.color_formats &
258 DRM_COLOR_FORMAT_YCRCB422) &&
260 config.csc_enable = false;
261 config.avi_infoframe.colorspace =
262 HDMI_COLORSPACE_YUV422;
264 config.csc_enable = true;
265 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
269 if (config.hdmi_mode) {
270 mode = ADV7511_HDMI_CFG_MODE_HDMI;
272 switch (config.avi_infoframe.colorspace) {
273 case HDMI_COLORSPACE_YUV444:
274 output_format_422 = false;
275 output_format_ycbcr = true;
277 case HDMI_COLORSPACE_YUV422:
278 output_format_422 = true;
279 output_format_ycbcr = true;
282 output_format_422 = false;
283 output_format_ycbcr = false;
287 mode = ADV7511_HDMI_CFG_MODE_DVI;
288 output_format_422 = false;
289 output_format_ycbcr = false;
292 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
294 adv7511_set_colormap(adv7511, config.csc_enable,
295 config.csc_coefficents,
296 config.csc_scaling_factor);
298 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
299 (output_format_422 << 7) | output_format_ycbcr);
301 regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
302 ADV7511_HDMI_CFG_MODE_MASK, mode);
304 hdmi_avi_infoframe_pack(&config.avi_infoframe, infoframe,
307 /* The AVI infoframe id is not configurable */
308 regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
309 infoframe + 1, sizeof(infoframe) - 1);
311 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
314 static void adv7511_set_link_config(struct adv7511 *adv7511,
315 const struct adv7511_link_config *config)
318 * The input style values documented in the datasheet don't match the
319 * hardware register field values :-(
321 static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
323 unsigned int clock_delay;
324 unsigned int color_depth;
325 unsigned int input_id;
327 clock_delay = (config->clock_delay + 1200) / 400;
328 color_depth = config->input_color_depth == 8 ? 3
329 : (config->input_color_depth == 10 ? 1 : 2);
331 /* TODO Support input ID 6 */
332 if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
333 input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
335 else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
336 input_id = config->embedded_sync ? 8 : 7;
337 else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
338 input_id = config->embedded_sync ? 4 : 3;
340 input_id = config->embedded_sync ? 2 : 1;
342 regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
344 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
346 (input_styles[config->input_style] << 2));
347 regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
348 config->input_justification << 3);
349 regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
350 config->sync_pulse << 2);
352 regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
354 adv7511->embedded_sync = config->embedded_sync;
355 adv7511->hsync_polarity = config->hsync_polarity;
356 adv7511->vsync_polarity = config->vsync_polarity;
357 adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
360 /* -----------------------------------------------------------------------------
361 * Interrupt and hotplug detection
364 static bool adv7511_hpd(struct adv7511 *adv7511)
369 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
373 if (irq0 & ADV7511_INT0_HDP) {
374 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
382 static irqreturn_t adv7511_irq_handler(int irq, void *devid)
384 struct adv7511 *adv7511 = devid;
386 if (adv7511_hpd(adv7511))
387 drm_helper_hpd_irq_event(adv7511->encoder->dev);
389 wake_up_all(&adv7511->wq);
394 static unsigned int adv7511_is_interrupt_pending(struct adv7511 *adv7511,
397 unsigned int irq0, irq1;
398 unsigned int pending;
401 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
404 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
408 pending = (irq1 << 8) | irq0;
410 return pending & irq;
413 static int adv7511_wait_for_interrupt(struct adv7511 *adv7511, int irq,
416 unsigned int pending;
419 if (adv7511->i2c_main->irq) {
420 ret = wait_event_interruptible_timeout(adv7511->wq,
421 adv7511_is_interrupt_pending(adv7511, irq),
422 msecs_to_jiffies(timeout));
425 pending = adv7511_is_interrupt_pending(adv7511, irq);
430 pending = adv7511_is_interrupt_pending(adv7511, irq);
435 } while (timeout >= 25);
441 /* -----------------------------------------------------------------------------
445 static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
448 struct adv7511 *adv7511 = data;
449 struct i2c_msg xfer[2];
457 if (adv7511->current_edid_segment != block / 2) {
460 ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
466 regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
468 ret = adv7511_wait_for_interrupt(adv7511,
469 ADV7511_INT0_EDID_READY |
470 ADV7511_INT1_DDC_ERROR, 200);
472 if (!(ret & ADV7511_INT0_EDID_READY))
476 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
477 ADV7511_INT0_EDID_READY | ADV7511_INT1_DDC_ERROR);
479 /* Break this apart, hopefully more I2C controllers will
480 * support 64 byte transfers than 256 byte transfers
483 xfer[0].addr = adv7511->i2c_edid->addr;
486 xfer[0].buf = &offset;
487 xfer[1].addr = adv7511->i2c_edid->addr;
488 xfer[1].flags = I2C_M_RD;
490 xfer[1].buf = adv7511->edid_buf;
494 for (i = 0; i < 4; ++i) {
495 ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
506 adv7511->current_edid_segment = block / 2;
510 memcpy(buf, adv7511->edid_buf, len);
512 memcpy(buf, adv7511->edid_buf + 128, len);
517 /* -----------------------------------------------------------------------------
521 static int adv7511_get_modes(struct drm_encoder *encoder,
522 struct drm_connector *connector)
524 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
528 /* Reading the EDID only works if the device is powered */
529 if (adv7511->dpms_mode != DRM_MODE_DPMS_ON) {
530 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
531 ADV7511_INT0_EDID_READY | ADV7511_INT1_DDC_ERROR);
532 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
533 ADV7511_POWER_POWER_DOWN, 0);
534 adv7511->current_edid_segment = -1;
537 edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
539 if (adv7511->dpms_mode != DRM_MODE_DPMS_ON)
540 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
541 ADV7511_POWER_POWER_DOWN,
542 ADV7511_POWER_POWER_DOWN);
544 kfree(adv7511->edid);
545 adv7511->edid = edid;
549 drm_mode_connector_update_edid_property(connector, edid);
550 count = drm_add_edid_modes(connector, edid);
552 adv7511_set_config_csc(adv7511, connector, adv7511->rgb);
557 static void adv7511_encoder_dpms(struct drm_encoder *encoder, int mode)
559 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
562 case DRM_MODE_DPMS_ON:
563 adv7511->current_edid_segment = -1;
565 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
566 ADV7511_INT0_EDID_READY | ADV7511_INT1_DDC_ERROR);
567 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
568 ADV7511_POWER_POWER_DOWN, 0);
570 * Per spec it is allowed to pulse the HDP signal to indicate
571 * that the EDID information has changed. Some monitors do this
572 * when they wakeup from standby or are enabled. When the HDP
573 * goes low the adv7511 is reset and the outputs are disabled
574 * which might cause the monitor to go to standby again. To
575 * avoid this we ignore the HDP pin for the first few seconds
576 * after enabeling the output.
578 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
579 ADV7511_REG_POWER2_HDP_SRC_MASK,
580 ADV7511_REG_POWER2_HDP_SRC_NONE);
581 /* Most of the registers are reset during power down or
584 regcache_sync(adv7511->regmap);
587 /* TODO: setup additional power down modes */
588 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
589 ADV7511_POWER_POWER_DOWN,
590 ADV7511_POWER_POWER_DOWN);
591 regcache_mark_dirty(adv7511->regmap);
595 adv7511->dpms_mode = mode;
598 static enum drm_connector_status
599 adv7511_encoder_detect(struct drm_encoder *encoder,
600 struct drm_connector *connector)
602 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
603 enum drm_connector_status status;
608 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
610 return connector_status_disconnected;
612 if (val & ADV7511_STATUS_HPD)
613 status = connector_status_connected;
615 status = connector_status_disconnected;
617 hpd = adv7511_hpd(adv7511);
619 /* The chip resets itself when the cable is disconnected, so in case
620 * there is a pending HPD interrupt and the cable is connected there was
621 * at least one transition from disconnected to connected and the chip
622 * has to be reinitialized. */
623 if (status == connector_status_connected && hpd &&
624 adv7511->dpms_mode == DRM_MODE_DPMS_ON) {
625 regcache_mark_dirty(adv7511->regmap);
626 adv7511_encoder_dpms(encoder, adv7511->dpms_mode);
627 adv7511_get_modes(encoder, connector);
628 if (adv7511->status == connector_status_connected)
629 status = connector_status_disconnected;
631 /* Renable HDP sensing */
632 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
633 ADV7511_REG_POWER2_HDP_SRC_MASK,
634 ADV7511_REG_POWER2_HDP_SRC_BOTH);
637 adv7511->status = status;
641 static int adv7511_encoder_mode_valid(struct drm_encoder *encoder,
642 struct drm_display_mode *mode)
644 if (mode->clock > 165000)
645 return MODE_CLOCK_HIGH;
650 static void adv7511_encoder_mode_set(struct drm_encoder *encoder,
651 struct drm_display_mode *mode,
652 struct drm_display_mode *adj_mode)
654 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
655 unsigned int low_refresh_rate;
656 unsigned int hsync_polarity = 0;
657 unsigned int vsync_polarity = 0;
659 if (adv7511->embedded_sync) {
660 unsigned int hsync_offset, hsync_len;
661 unsigned int vsync_offset, vsync_len;
663 hsync_offset = adj_mode->crtc_hsync_start -
664 adj_mode->crtc_hdisplay;
665 vsync_offset = adj_mode->crtc_vsync_start -
666 adj_mode->crtc_vdisplay;
667 hsync_len = adj_mode->crtc_hsync_end -
668 adj_mode->crtc_hsync_start;
669 vsync_len = adj_mode->crtc_vsync_end -
670 adj_mode->crtc_vsync_start;
672 /* The hardware vsync generator has a off-by-one bug */
675 regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
676 ((hsync_offset >> 10) & 0x7) << 5);
677 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
678 (hsync_offset >> 2) & 0xff);
679 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
680 ((hsync_offset & 0x3) << 6) |
681 ((hsync_len >> 4) & 0x3f));
682 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
683 ((hsync_len & 0xf) << 4) |
684 ((vsync_offset >> 6) & 0xf));
685 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
686 ((vsync_offset & 0x3f) << 2) |
687 ((vsync_len >> 8) & 0x3));
688 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
691 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
692 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
694 enum adv7511_sync_polarity mode_hsync_polarity;
695 enum adv7511_sync_polarity mode_vsync_polarity;
698 * If the input signal is always low or always high we want to
699 * invert or let it passthrough depending on the polarity of the
702 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
703 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
705 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
707 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
708 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
710 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
712 if (adv7511->hsync_polarity != mode_hsync_polarity &&
713 adv7511->hsync_polarity !=
714 ADV7511_SYNC_POLARITY_PASSTHROUGH)
717 if (adv7511->vsync_polarity != mode_vsync_polarity &&
718 adv7511->vsync_polarity !=
719 ADV7511_SYNC_POLARITY_PASSTHROUGH)
723 if (mode->vrefresh <= 24000)
724 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
725 else if (mode->vrefresh <= 25000)
726 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
727 else if (mode->vrefresh <= 30000)
728 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
730 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
732 regmap_update_bits(adv7511->regmap, 0xfb,
733 0x6, low_refresh_rate << 1);
734 regmap_update_bits(adv7511->regmap, 0x17,
735 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
738 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
739 * supposed to give better results.
742 adv7511->f_tmds = mode->clock;
745 static struct drm_encoder_slave_funcs adv7511_encoder_funcs = {
746 .dpms = adv7511_encoder_dpms,
747 .mode_valid = adv7511_encoder_mode_valid,
748 .mode_set = adv7511_encoder_mode_set,
749 .detect = adv7511_encoder_detect,
750 .get_modes = adv7511_get_modes,
753 /* -----------------------------------------------------------------------------
757 static int adv7511_parse_dt(struct device_node *np,
758 struct adv7511_link_config *config)
763 memset(config, 0, sizeof(*config));
765 of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
766 if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
767 config->input_color_depth != 12)
770 ret = of_property_read_string(np, "adi,input-colorspace", &str);
774 if (!strcmp(str, "rgb"))
775 config->input_colorspace = HDMI_COLORSPACE_RGB;
776 else if (!strcmp(str, "yuv422"))
777 config->input_colorspace = HDMI_COLORSPACE_YUV422;
778 else if (!strcmp(str, "yuv444"))
779 config->input_colorspace = HDMI_COLORSPACE_YUV444;
783 ret = of_property_read_string(np, "adi,input-clock", &str);
787 if (!strcmp(str, "1x"))
788 config->input_clock = ADV7511_INPUT_CLOCK_1X;
789 else if (!strcmp(str, "2x"))
790 config->input_clock = ADV7511_INPUT_CLOCK_2X;
791 else if (!strcmp(str, "ddr"))
792 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
796 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
797 config->input_clock != ADV7511_INPUT_CLOCK_1X) {
798 ret = of_property_read_u32(np, "adi,input-style",
799 &config->input_style);
803 if (config->input_style < 1 || config->input_style > 3)
806 ret = of_property_read_string(np, "adi,input-justification",
811 if (!strcmp(str, "left"))
812 config->input_justification =
813 ADV7511_INPUT_JUSTIFICATION_LEFT;
814 else if (!strcmp(str, "evenly"))
815 config->input_justification =
816 ADV7511_INPUT_JUSTIFICATION_EVENLY;
817 else if (!strcmp(str, "right"))
818 config->input_justification =
819 ADV7511_INPUT_JUSTIFICATION_RIGHT;
824 config->input_style = 1;
825 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
828 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
829 if (config->clock_delay < -1200 || config->clock_delay > 1600)
832 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
834 /* Hardcode the sync pulse configurations for now. */
835 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
836 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
837 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
842 static const int edid_i2c_addr = 0x7e;
843 static const int packet_i2c_addr = 0x70;
844 static const int cec_i2c_addr = 0x78;
846 static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
848 struct adv7511_link_config link_config;
849 struct adv7511 *adv7511;
850 struct device *dev = &i2c->dev;
857 adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
861 adv7511->dpms_mode = DRM_MODE_DPMS_OFF;
862 adv7511->status = connector_status_disconnected;
864 ret = adv7511_parse_dt(dev->of_node, &link_config);
869 * The power down GPIO is optional. If present, toggle it from active to
870 * inactive to wake up the encoder.
872 adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
873 if (IS_ERR(adv7511->gpio_pd))
874 return PTR_ERR(adv7511->gpio_pd);
876 if (adv7511->gpio_pd) {
878 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
881 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
882 if (IS_ERR(adv7511->regmap))
883 return PTR_ERR(adv7511->regmap);
885 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
888 dev_dbg(dev, "Rev. %d\n", val);
890 ret = regmap_register_patch(adv7511->regmap, adv7511_fixed_registers,
891 ARRAY_SIZE(adv7511_fixed_registers));
895 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
896 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
898 regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR, cec_i2c_addr);
899 adv7511_packet_disable(adv7511, 0xffff);
901 adv7511->i2c_main = i2c;
902 adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
903 if (!adv7511->i2c_edid)
907 init_waitqueue_head(&adv7511->wq);
909 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
911 IRQF_ONESHOT, dev_name(dev),
914 goto err_i2c_unregister_device;
917 /* CEC is unused for now */
918 regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
919 ADV7511_CEC_CTRL_POWER_DOWN);
921 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
922 ADV7511_POWER_POWER_DOWN, ADV7511_POWER_POWER_DOWN);
924 adv7511->current_edid_segment = -1;
926 i2c_set_clientdata(i2c, adv7511);
928 adv7511_set_link_config(adv7511, &link_config);
932 err_i2c_unregister_device:
933 i2c_unregister_device(adv7511->i2c_edid);
938 static int adv7511_remove(struct i2c_client *i2c)
940 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
942 i2c_unregister_device(adv7511->i2c_edid);
944 kfree(adv7511->edid);
949 static int adv7511_encoder_init(struct i2c_client *i2c, struct drm_device *dev,
950 struct drm_encoder_slave *encoder)
953 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
955 encoder->slave_priv = adv7511;
956 encoder->slave_funcs = &adv7511_encoder_funcs;
958 adv7511->encoder = &encoder->base;
963 static const struct i2c_device_id adv7511_i2c_ids[] = {
969 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
971 static const struct of_device_id adv7511_of_ids[] = {
972 { .compatible = "adi,adv7511", },
973 { .compatible = "adi,adv7511w", },
974 { .compatible = "adi,adv7513", },
977 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
979 static struct drm_i2c_encoder_driver adv7511_driver = {
983 .of_match_table = adv7511_of_ids,
985 .id_table = adv7511_i2c_ids,
986 .probe = adv7511_probe,
987 .remove = adv7511_remove,
990 .encoder_init = adv7511_encoder_init,
993 static int __init adv7511_init(void)
995 return drm_i2c_encoder_register(THIS_MODULE, &adv7511_driver);
997 module_init(adv7511_init);
999 static void __exit adv7511_exit(void)
1001 drm_i2c_encoder_unregister(&adv7511_driver);
1003 module_exit(adv7511_exit);
1005 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1006 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1007 MODULE_LICENSE("GPL");