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;
647 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
648 return MODE_NO_INTERLACE;
653 static void adv7511_encoder_mode_set(struct drm_encoder *encoder,
654 struct drm_display_mode *mode,
655 struct drm_display_mode *adj_mode)
657 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
658 unsigned int low_refresh_rate;
659 unsigned int hsync_polarity = 0;
660 unsigned int vsync_polarity = 0;
662 if (adv7511->embedded_sync) {
663 unsigned int hsync_offset, hsync_len;
664 unsigned int vsync_offset, vsync_len;
666 hsync_offset = adj_mode->crtc_hsync_start -
667 adj_mode->crtc_hdisplay;
668 vsync_offset = adj_mode->crtc_vsync_start -
669 adj_mode->crtc_vdisplay;
670 hsync_len = adj_mode->crtc_hsync_end -
671 adj_mode->crtc_hsync_start;
672 vsync_len = adj_mode->crtc_vsync_end -
673 adj_mode->crtc_vsync_start;
675 /* The hardware vsync generator has a off-by-one bug */
678 regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
679 ((hsync_offset >> 10) & 0x7) << 5);
680 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
681 (hsync_offset >> 2) & 0xff);
682 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
683 ((hsync_offset & 0x3) << 6) |
684 ((hsync_len >> 4) & 0x3f));
685 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
686 ((hsync_len & 0xf) << 4) |
687 ((vsync_offset >> 6) & 0xf));
688 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
689 ((vsync_offset & 0x3f) << 2) |
690 ((vsync_len >> 8) & 0x3));
691 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
694 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
695 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
697 enum adv7511_sync_polarity mode_hsync_polarity;
698 enum adv7511_sync_polarity mode_vsync_polarity;
701 * If the input signal is always low or always high we want to
702 * invert or let it passthrough depending on the polarity of the
705 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
706 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
708 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
710 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
711 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
713 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
715 if (adv7511->hsync_polarity != mode_hsync_polarity &&
716 adv7511->hsync_polarity !=
717 ADV7511_SYNC_POLARITY_PASSTHROUGH)
720 if (adv7511->vsync_polarity != mode_vsync_polarity &&
721 adv7511->vsync_polarity !=
722 ADV7511_SYNC_POLARITY_PASSTHROUGH)
726 if (mode->vrefresh <= 24000)
727 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
728 else if (mode->vrefresh <= 25000)
729 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
730 else if (mode->vrefresh <= 30000)
731 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
733 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
735 regmap_update_bits(adv7511->regmap, 0xfb,
736 0x6, low_refresh_rate << 1);
737 regmap_update_bits(adv7511->regmap, 0x17,
738 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
741 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
742 * supposed to give better results.
745 adv7511->f_tmds = mode->clock;
748 static struct drm_encoder_slave_funcs adv7511_encoder_funcs = {
749 .dpms = adv7511_encoder_dpms,
750 .mode_valid = adv7511_encoder_mode_valid,
751 .mode_set = adv7511_encoder_mode_set,
752 .detect = adv7511_encoder_detect,
753 .get_modes = adv7511_get_modes,
756 /* -----------------------------------------------------------------------------
760 static int adv7511_parse_dt(struct device_node *np,
761 struct adv7511_link_config *config)
766 memset(config, 0, sizeof(*config));
768 of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
769 if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
770 config->input_color_depth != 12)
773 ret = of_property_read_string(np, "adi,input-colorspace", &str);
777 if (!strcmp(str, "rgb"))
778 config->input_colorspace = HDMI_COLORSPACE_RGB;
779 else if (!strcmp(str, "yuv422"))
780 config->input_colorspace = HDMI_COLORSPACE_YUV422;
781 else if (!strcmp(str, "yuv444"))
782 config->input_colorspace = HDMI_COLORSPACE_YUV444;
786 ret = of_property_read_string(np, "adi,input-clock", &str);
790 if (!strcmp(str, "1x"))
791 config->input_clock = ADV7511_INPUT_CLOCK_1X;
792 else if (!strcmp(str, "2x"))
793 config->input_clock = ADV7511_INPUT_CLOCK_2X;
794 else if (!strcmp(str, "ddr"))
795 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
799 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
800 config->input_clock != ADV7511_INPUT_CLOCK_1X) {
801 ret = of_property_read_u32(np, "adi,input-style",
802 &config->input_style);
806 if (config->input_style < 1 || config->input_style > 3)
809 ret = of_property_read_string(np, "adi,input-justification",
814 if (!strcmp(str, "left"))
815 config->input_justification =
816 ADV7511_INPUT_JUSTIFICATION_LEFT;
817 else if (!strcmp(str, "evenly"))
818 config->input_justification =
819 ADV7511_INPUT_JUSTIFICATION_EVENLY;
820 else if (!strcmp(str, "right"))
821 config->input_justification =
822 ADV7511_INPUT_JUSTIFICATION_RIGHT;
827 config->input_style = 1;
828 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
831 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
832 if (config->clock_delay < -1200 || config->clock_delay > 1600)
835 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
837 /* Hardcode the sync pulse configurations for now. */
838 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
839 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
840 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
845 static const int edid_i2c_addr = 0x7e;
846 static const int packet_i2c_addr = 0x70;
847 static const int cec_i2c_addr = 0x78;
849 static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
851 struct adv7511_link_config link_config;
852 struct adv7511 *adv7511;
853 struct device *dev = &i2c->dev;
860 adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
864 adv7511->dpms_mode = DRM_MODE_DPMS_OFF;
865 adv7511->status = connector_status_disconnected;
867 ret = adv7511_parse_dt(dev->of_node, &link_config);
872 * The power down GPIO is optional. If present, toggle it from active to
873 * inactive to wake up the encoder.
875 adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
876 if (IS_ERR(adv7511->gpio_pd))
877 return PTR_ERR(adv7511->gpio_pd);
879 if (adv7511->gpio_pd) {
881 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
884 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
885 if (IS_ERR(adv7511->regmap))
886 return PTR_ERR(adv7511->regmap);
888 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
891 dev_dbg(dev, "Rev. %d\n", val);
893 ret = regmap_register_patch(adv7511->regmap, adv7511_fixed_registers,
894 ARRAY_SIZE(adv7511_fixed_registers));
898 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
899 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
901 regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR, cec_i2c_addr);
902 adv7511_packet_disable(adv7511, 0xffff);
904 adv7511->i2c_main = i2c;
905 adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
906 if (!adv7511->i2c_edid)
910 init_waitqueue_head(&adv7511->wq);
912 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
914 IRQF_ONESHOT, dev_name(dev),
917 goto err_i2c_unregister_device;
920 /* CEC is unused for now */
921 regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
922 ADV7511_CEC_CTRL_POWER_DOWN);
924 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
925 ADV7511_POWER_POWER_DOWN, ADV7511_POWER_POWER_DOWN);
927 adv7511->current_edid_segment = -1;
929 i2c_set_clientdata(i2c, adv7511);
931 adv7511_set_link_config(adv7511, &link_config);
935 err_i2c_unregister_device:
936 i2c_unregister_device(adv7511->i2c_edid);
941 static int adv7511_remove(struct i2c_client *i2c)
943 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
945 i2c_unregister_device(adv7511->i2c_edid);
947 kfree(adv7511->edid);
952 static int adv7511_encoder_init(struct i2c_client *i2c, struct drm_device *dev,
953 struct drm_encoder_slave *encoder)
956 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
958 encoder->slave_priv = adv7511;
959 encoder->slave_funcs = &adv7511_encoder_funcs;
961 adv7511->encoder = &encoder->base;
966 static const struct i2c_device_id adv7511_i2c_ids[] = {
972 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
974 static const struct of_device_id adv7511_of_ids[] = {
975 { .compatible = "adi,adv7511", },
976 { .compatible = "adi,adv7511w", },
977 { .compatible = "adi,adv7513", },
980 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
982 static struct drm_i2c_encoder_driver adv7511_driver = {
986 .of_match_table = adv7511_of_ids,
988 .id_table = adv7511_i2c_ids,
989 .probe = adv7511_probe,
990 .remove = adv7511_remove,
993 .encoder_init = adv7511_encoder_init,
996 static int __init adv7511_init(void)
998 return drm_i2c_encoder_register(THIS_MODULE, &adv7511_driver);
1000 module_init(adv7511_init);
1002 static void __exit adv7511_exit(void)
1004 drm_i2c_encoder_unregister(&adv7511_driver);
1006 module_exit(adv7511_exit);
1009 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1010 MODULE_LICENSE("GPL");