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];
39 struct drm_encoder *encoder;
42 enum adv7511_sync_polarity vsync_polarity;
43 enum adv7511_sync_polarity hsync_polarity;
48 struct gpio_desc *gpio_pd;
51 static struct adv7511 *encoder_to_adv7511(struct drm_encoder *encoder)
53 return to_encoder_slave(encoder)->slave_priv;
56 /* ADI recommended values for proper operation. */
57 static const struct reg_sequence adv7511_fixed_registers[] = {
69 /* -----------------------------------------------------------------------------
73 static const uint8_t adv7511_register_defaults[] = {
74 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
75 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
76 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
77 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
78 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
79 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
81 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
82 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
83 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
84 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
85 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
93 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
94 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
95 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
97 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
99 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
100 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
102 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
103 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
105 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108 static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
111 case ADV7511_REG_CHIP_REVISION:
112 case ADV7511_REG_SPDIF_FREQ:
113 case ADV7511_REG_CTS_AUTOMATIC1:
114 case ADV7511_REG_CTS_AUTOMATIC2:
115 case ADV7511_REG_VIC_DETECTED:
116 case ADV7511_REG_VIC_SEND:
117 case ADV7511_REG_AUX_VIC_DETECTED:
118 case ADV7511_REG_STATUS:
119 case ADV7511_REG_GC(1):
120 case ADV7511_REG_INT(0):
121 case ADV7511_REG_INT(1):
122 case ADV7511_REG_PLL_STATUS:
123 case ADV7511_REG_AN(0):
124 case ADV7511_REG_AN(1):
125 case ADV7511_REG_AN(2):
126 case ADV7511_REG_AN(3):
127 case ADV7511_REG_AN(4):
128 case ADV7511_REG_AN(5):
129 case ADV7511_REG_AN(6):
130 case ADV7511_REG_AN(7):
131 case ADV7511_REG_HDCP_STATUS:
132 case ADV7511_REG_BCAPS:
133 case ADV7511_REG_BKSV(0):
134 case ADV7511_REG_BKSV(1):
135 case ADV7511_REG_BKSV(2):
136 case ADV7511_REG_BKSV(3):
137 case ADV7511_REG_BKSV(4):
138 case ADV7511_REG_DDC_STATUS:
139 case ADV7511_REG_EDID_READ_CTRL:
140 case ADV7511_REG_BSTATUS(0):
141 case ADV7511_REG_BSTATUS(1):
142 case ADV7511_REG_CHIP_ID_HIGH:
143 case ADV7511_REG_CHIP_ID_LOW:
150 static const struct regmap_config adv7511_regmap_config = {
154 .max_register = 0xff,
155 .cache_type = REGCACHE_RBTREE,
156 .reg_defaults_raw = adv7511_register_defaults,
157 .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
159 .volatile_reg = adv7511_register_volatile,
162 /* -----------------------------------------------------------------------------
163 * Hardware configuration
166 static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
167 const uint16_t *coeff,
168 unsigned int scaling_factor)
172 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
173 ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
176 for (i = 0; i < 12; ++i) {
177 regmap_update_bits(adv7511->regmap,
178 ADV7511_REG_CSC_UPPER(i),
179 0x1f, coeff[i] >> 8);
180 regmap_write(adv7511->regmap,
181 ADV7511_REG_CSC_LOWER(i),
187 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
188 0xe0, 0x80 | (scaling_factor << 5));
190 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
193 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
194 ADV7511_CSC_UPDATE_MODE, 0);
197 static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
200 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
203 if (packet & 0xff00) {
205 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
212 static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
215 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
218 if (packet & 0xff00) {
220 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
227 /* Coefficients for adv7511 color space conversion */
228 static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
229 0x0734, 0x04ad, 0x0000, 0x1c1b,
230 0x1ddc, 0x04ad, 0x1f24, 0x0135,
231 0x0000, 0x04ad, 0x087c, 0x1b77,
234 static void adv7511_set_config_csc(struct adv7511 *adv7511,
235 struct drm_connector *connector,
238 struct adv7511_video_config config;
239 bool output_format_422, output_format_ycbcr;
241 uint8_t infoframe[17];
244 config.hdmi_mode = drm_detect_hdmi_monitor(adv7511->edid);
246 config.hdmi_mode = false;
248 hdmi_avi_infoframe_init(&config.avi_infoframe);
250 config.avi_infoframe.scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
253 config.csc_enable = false;
254 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
256 config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
257 config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
259 if ((connector->display_info.color_formats &
260 DRM_COLOR_FORMAT_YCRCB422) &&
262 config.csc_enable = false;
263 config.avi_infoframe.colorspace =
264 HDMI_COLORSPACE_YUV422;
266 config.csc_enable = true;
267 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
271 if (config.hdmi_mode) {
272 mode = ADV7511_HDMI_CFG_MODE_HDMI;
274 switch (config.avi_infoframe.colorspace) {
275 case HDMI_COLORSPACE_YUV444:
276 output_format_422 = false;
277 output_format_ycbcr = true;
279 case HDMI_COLORSPACE_YUV422:
280 output_format_422 = true;
281 output_format_ycbcr = true;
284 output_format_422 = false;
285 output_format_ycbcr = false;
289 mode = ADV7511_HDMI_CFG_MODE_DVI;
290 output_format_422 = false;
291 output_format_ycbcr = false;
294 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
296 adv7511_set_colormap(adv7511, config.csc_enable,
297 config.csc_coefficents,
298 config.csc_scaling_factor);
300 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
301 (output_format_422 << 7) | output_format_ycbcr);
303 regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
304 ADV7511_HDMI_CFG_MODE_MASK, mode);
306 hdmi_avi_infoframe_pack(&config.avi_infoframe, infoframe,
309 /* The AVI infoframe id is not configurable */
310 regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
311 infoframe + 1, sizeof(infoframe) - 1);
313 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
316 static void adv7511_set_link_config(struct adv7511 *adv7511,
317 const struct adv7511_link_config *config)
320 * The input style values documented in the datasheet don't match the
321 * hardware register field values :-(
323 static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
325 unsigned int clock_delay;
326 unsigned int color_depth;
327 unsigned int input_id;
329 clock_delay = (config->clock_delay + 1200) / 400;
330 color_depth = config->input_color_depth == 8 ? 3
331 : (config->input_color_depth == 10 ? 1 : 2);
333 /* TODO Support input ID 6 */
334 if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
335 input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
337 else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
338 input_id = config->embedded_sync ? 8 : 7;
339 else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
340 input_id = config->embedded_sync ? 4 : 3;
342 input_id = config->embedded_sync ? 2 : 1;
344 regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
346 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
348 (input_styles[config->input_style] << 2));
349 regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
350 config->input_justification << 3);
351 regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
352 config->sync_pulse << 2);
354 regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
356 adv7511->embedded_sync = config->embedded_sync;
357 adv7511->hsync_polarity = config->hsync_polarity;
358 adv7511->vsync_polarity = config->vsync_polarity;
359 adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
362 static void adv7511_power_on(struct adv7511 *adv7511)
364 adv7511->current_edid_segment = -1;
366 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
367 ADV7511_POWER_POWER_DOWN, 0);
368 if (adv7511->i2c_main->irq) {
370 * Documentation says the INT_ENABLE registers are reset in
371 * POWER_DOWN mode. My 7511w preserved the bits, however.
372 * Still, let's be safe and stick to the documentation.
374 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
375 ADV7511_INT0_EDID_READY);
376 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(1),
377 ADV7511_INT1_DDC_ERROR);
381 * Per spec it is allowed to pulse the HPD signal to indicate that the
382 * EDID information has changed. Some monitors do this when they wakeup
383 * from standby or are enabled. When the HPD goes low the adv7511 is
384 * reset and the outputs are disabled which might cause the monitor to
385 * go to standby again. To avoid this we ignore the HPD pin for the
386 * first few seconds after enabling the output.
388 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
389 ADV7511_REG_POWER2_HPD_SRC_MASK,
390 ADV7511_REG_POWER2_HPD_SRC_NONE);
393 * Most of the registers are reset during power down or when HPD is low.
395 regcache_sync(adv7511->regmap);
397 adv7511->powered = true;
400 static void adv7511_power_off(struct adv7511 *adv7511)
402 /* TODO: setup additional power down modes */
403 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
404 ADV7511_POWER_POWER_DOWN,
405 ADV7511_POWER_POWER_DOWN);
406 regcache_mark_dirty(adv7511->regmap);
408 adv7511->powered = false;
411 /* -----------------------------------------------------------------------------
412 * Interrupt and hotplug detection
415 static bool adv7511_hpd(struct adv7511 *adv7511)
420 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
424 if (irq0 & ADV7511_INT0_HPD) {
425 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
433 static int adv7511_irq_process(struct adv7511 *adv7511)
435 unsigned int irq0, irq1;
438 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
442 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
446 regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
447 regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
449 if (irq0 & ADV7511_INT0_HPD && adv7511->encoder)
450 drm_helper_hpd_irq_event(adv7511->encoder->dev);
452 if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
453 adv7511->edid_read = true;
455 if (adv7511->i2c_main->irq)
456 wake_up_all(&adv7511->wq);
462 static irqreturn_t adv7511_irq_handler(int irq, void *devid)
464 struct adv7511 *adv7511 = devid;
467 ret = adv7511_irq_process(adv7511);
468 return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
471 /* -----------------------------------------------------------------------------
475 static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
479 if (adv7511->i2c_main->irq) {
480 ret = wait_event_interruptible_timeout(adv7511->wq,
481 adv7511->edid_read, msecs_to_jiffies(timeout));
483 for (; timeout > 0; timeout -= 25) {
484 ret = adv7511_irq_process(adv7511);
488 if (adv7511->edid_read)
495 return adv7511->edid_read ? 0 : -EIO;
498 static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
501 struct adv7511 *adv7511 = data;
502 struct i2c_msg xfer[2];
510 if (adv7511->current_edid_segment != block / 2) {
513 ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
519 adv7511->edid_read = false;
520 regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
522 ret = adv7511_wait_for_edid(adv7511, 200);
527 /* Break this apart, hopefully more I2C controllers will
528 * support 64 byte transfers than 256 byte transfers
531 xfer[0].addr = adv7511->i2c_edid->addr;
534 xfer[0].buf = &offset;
535 xfer[1].addr = adv7511->i2c_edid->addr;
536 xfer[1].flags = I2C_M_RD;
538 xfer[1].buf = adv7511->edid_buf;
542 for (i = 0; i < 4; ++i) {
543 ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
554 adv7511->current_edid_segment = block / 2;
558 memcpy(buf, adv7511->edid_buf, len);
560 memcpy(buf, adv7511->edid_buf + 128, len);
565 /* -----------------------------------------------------------------------------
569 static int adv7511_get_modes(struct drm_encoder *encoder,
570 struct drm_connector *connector)
572 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
576 /* Reading the EDID only works if the device is powered */
577 if (!adv7511->powered) {
578 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
579 ADV7511_POWER_POWER_DOWN, 0);
580 if (adv7511->i2c_main->irq) {
581 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
582 ADV7511_INT0_EDID_READY);
583 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(1),
584 ADV7511_INT1_DDC_ERROR);
586 adv7511->current_edid_segment = -1;
589 edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
591 if (!adv7511->powered)
592 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
593 ADV7511_POWER_POWER_DOWN,
594 ADV7511_POWER_POWER_DOWN);
596 kfree(adv7511->edid);
597 adv7511->edid = edid;
601 drm_mode_connector_update_edid_property(connector, edid);
602 count = drm_add_edid_modes(connector, edid);
604 adv7511_set_config_csc(adv7511, connector, adv7511->rgb);
609 static void adv7511_encoder_dpms(struct drm_encoder *encoder, int mode)
611 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
613 if (mode == DRM_MODE_DPMS_ON)
614 adv7511_power_on(adv7511);
616 adv7511_power_off(adv7511);
619 static enum drm_connector_status
620 adv7511_encoder_detect(struct drm_encoder *encoder,
621 struct drm_connector *connector)
623 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
624 enum drm_connector_status status;
629 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
631 return connector_status_disconnected;
633 if (val & ADV7511_STATUS_HPD)
634 status = connector_status_connected;
636 status = connector_status_disconnected;
638 hpd = adv7511_hpd(adv7511);
640 /* The chip resets itself when the cable is disconnected, so in case
641 * there is a pending HPD interrupt and the cable is connected there was
642 * at least one transition from disconnected to connected and the chip
643 * has to be reinitialized. */
644 if (status == connector_status_connected && hpd && adv7511->powered) {
645 regcache_mark_dirty(adv7511->regmap);
646 adv7511_power_on(adv7511);
647 adv7511_get_modes(encoder, connector);
648 if (adv7511->status == connector_status_connected)
649 status = connector_status_disconnected;
651 /* Renable HPD sensing */
652 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
653 ADV7511_REG_POWER2_HPD_SRC_MASK,
654 ADV7511_REG_POWER2_HPD_SRC_BOTH);
657 adv7511->status = status;
661 static int adv7511_encoder_mode_valid(struct drm_encoder *encoder,
662 struct drm_display_mode *mode)
664 if (mode->clock > 165000)
665 return MODE_CLOCK_HIGH;
670 static void adv7511_encoder_mode_set(struct drm_encoder *encoder,
671 struct drm_display_mode *mode,
672 struct drm_display_mode *adj_mode)
674 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
675 unsigned int low_refresh_rate;
676 unsigned int hsync_polarity = 0;
677 unsigned int vsync_polarity = 0;
679 if (adv7511->embedded_sync) {
680 unsigned int hsync_offset, hsync_len;
681 unsigned int vsync_offset, vsync_len;
683 hsync_offset = adj_mode->crtc_hsync_start -
684 adj_mode->crtc_hdisplay;
685 vsync_offset = adj_mode->crtc_vsync_start -
686 adj_mode->crtc_vdisplay;
687 hsync_len = adj_mode->crtc_hsync_end -
688 adj_mode->crtc_hsync_start;
689 vsync_len = adj_mode->crtc_vsync_end -
690 adj_mode->crtc_vsync_start;
692 /* The hardware vsync generator has a off-by-one bug */
695 regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
696 ((hsync_offset >> 10) & 0x7) << 5);
697 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
698 (hsync_offset >> 2) & 0xff);
699 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
700 ((hsync_offset & 0x3) << 6) |
701 ((hsync_len >> 4) & 0x3f));
702 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
703 ((hsync_len & 0xf) << 4) |
704 ((vsync_offset >> 6) & 0xf));
705 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
706 ((vsync_offset & 0x3f) << 2) |
707 ((vsync_len >> 8) & 0x3));
708 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
711 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
712 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
714 enum adv7511_sync_polarity mode_hsync_polarity;
715 enum adv7511_sync_polarity mode_vsync_polarity;
718 * If the input signal is always low or always high we want to
719 * invert or let it passthrough depending on the polarity of the
722 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
723 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
725 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
727 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
728 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
730 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
732 if (adv7511->hsync_polarity != mode_hsync_polarity &&
733 adv7511->hsync_polarity !=
734 ADV7511_SYNC_POLARITY_PASSTHROUGH)
737 if (adv7511->vsync_polarity != mode_vsync_polarity &&
738 adv7511->vsync_polarity !=
739 ADV7511_SYNC_POLARITY_PASSTHROUGH)
743 if (mode->vrefresh <= 24000)
744 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
745 else if (mode->vrefresh <= 25000)
746 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
747 else if (mode->vrefresh <= 30000)
748 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
750 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
752 regmap_update_bits(adv7511->regmap, 0xfb,
753 0x6, low_refresh_rate << 1);
754 regmap_update_bits(adv7511->regmap, 0x17,
755 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
758 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
759 * supposed to give better results.
762 adv7511->f_tmds = mode->clock;
765 static const struct drm_encoder_slave_funcs adv7511_encoder_funcs = {
766 .dpms = adv7511_encoder_dpms,
767 .mode_valid = adv7511_encoder_mode_valid,
768 .mode_set = adv7511_encoder_mode_set,
769 .detect = adv7511_encoder_detect,
770 .get_modes = adv7511_get_modes,
773 /* -----------------------------------------------------------------------------
777 static int adv7511_parse_dt(struct device_node *np,
778 struct adv7511_link_config *config)
783 memset(config, 0, sizeof(*config));
785 of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
786 if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
787 config->input_color_depth != 12)
790 ret = of_property_read_string(np, "adi,input-colorspace", &str);
794 if (!strcmp(str, "rgb"))
795 config->input_colorspace = HDMI_COLORSPACE_RGB;
796 else if (!strcmp(str, "yuv422"))
797 config->input_colorspace = HDMI_COLORSPACE_YUV422;
798 else if (!strcmp(str, "yuv444"))
799 config->input_colorspace = HDMI_COLORSPACE_YUV444;
803 ret = of_property_read_string(np, "adi,input-clock", &str);
807 if (!strcmp(str, "1x"))
808 config->input_clock = ADV7511_INPUT_CLOCK_1X;
809 else if (!strcmp(str, "2x"))
810 config->input_clock = ADV7511_INPUT_CLOCK_2X;
811 else if (!strcmp(str, "ddr"))
812 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
816 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
817 config->input_clock != ADV7511_INPUT_CLOCK_1X) {
818 ret = of_property_read_u32(np, "adi,input-style",
819 &config->input_style);
823 if (config->input_style < 1 || config->input_style > 3)
826 ret = of_property_read_string(np, "adi,input-justification",
831 if (!strcmp(str, "left"))
832 config->input_justification =
833 ADV7511_INPUT_JUSTIFICATION_LEFT;
834 else if (!strcmp(str, "evenly"))
835 config->input_justification =
836 ADV7511_INPUT_JUSTIFICATION_EVENLY;
837 else if (!strcmp(str, "right"))
838 config->input_justification =
839 ADV7511_INPUT_JUSTIFICATION_RIGHT;
844 config->input_style = 1;
845 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
848 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
849 if (config->clock_delay < -1200 || config->clock_delay > 1600)
852 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
854 /* Hardcode the sync pulse configurations for now. */
855 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
856 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
857 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
862 static const int edid_i2c_addr = 0x7e;
863 static const int packet_i2c_addr = 0x70;
864 static const int cec_i2c_addr = 0x78;
866 static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
868 struct adv7511_link_config link_config;
869 struct adv7511 *adv7511;
870 struct device *dev = &i2c->dev;
877 adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
881 adv7511->powered = false;
882 adv7511->status = connector_status_disconnected;
884 ret = adv7511_parse_dt(dev->of_node, &link_config);
889 * The power down GPIO is optional. If present, toggle it from active to
890 * inactive to wake up the encoder.
892 adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
893 if (IS_ERR(adv7511->gpio_pd))
894 return PTR_ERR(adv7511->gpio_pd);
896 if (adv7511->gpio_pd) {
898 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
901 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
902 if (IS_ERR(adv7511->regmap))
903 return PTR_ERR(adv7511->regmap);
905 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
908 dev_dbg(dev, "Rev. %d\n", val);
910 ret = regmap_register_patch(adv7511->regmap, adv7511_fixed_registers,
911 ARRAY_SIZE(adv7511_fixed_registers));
915 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
916 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
918 regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR, cec_i2c_addr);
919 adv7511_packet_disable(adv7511, 0xffff);
921 adv7511->i2c_main = i2c;
922 adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
923 if (!adv7511->i2c_edid)
927 init_waitqueue_head(&adv7511->wq);
929 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
931 IRQF_ONESHOT, dev_name(dev),
934 goto err_i2c_unregister_device;
937 /* CEC is unused for now */
938 regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
939 ADV7511_CEC_CTRL_POWER_DOWN);
941 adv7511_power_off(adv7511);
943 i2c_set_clientdata(i2c, adv7511);
945 adv7511_set_link_config(adv7511, &link_config);
949 err_i2c_unregister_device:
950 i2c_unregister_device(adv7511->i2c_edid);
955 static int adv7511_remove(struct i2c_client *i2c)
957 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
959 i2c_unregister_device(adv7511->i2c_edid);
961 kfree(adv7511->edid);
966 static int adv7511_encoder_init(struct i2c_client *i2c, struct drm_device *dev,
967 struct drm_encoder_slave *encoder)
970 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
972 encoder->slave_priv = adv7511;
973 encoder->slave_funcs = &adv7511_encoder_funcs;
975 adv7511->encoder = &encoder->base;
980 static const struct i2c_device_id adv7511_i2c_ids[] = {
986 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
988 static const struct of_device_id adv7511_of_ids[] = {
989 { .compatible = "adi,adv7511", },
990 { .compatible = "adi,adv7511w", },
991 { .compatible = "adi,adv7513", },
994 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
996 static struct drm_i2c_encoder_driver adv7511_driver = {
1000 .of_match_table = adv7511_of_ids,
1002 .id_table = adv7511_i2c_ids,
1003 .probe = adv7511_probe,
1004 .remove = adv7511_remove,
1007 .encoder_init = adv7511_encoder_init,
1010 static int __init adv7511_init(void)
1012 return drm_i2c_encoder_register(THIS_MODULE, &adv7511_driver);
1014 module_init(adv7511_init);
1016 static void __exit adv7511_exit(void)
1018 drm_i2c_encoder_unregister(&adv7511_driver);
1020 module_exit(adv7511_exit);
1023 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1024 MODULE_LICENSE("GPL");