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 work_struct hpd_work;
41 struct drm_encoder *encoder;
42 struct drm_connector connector;
45 enum adv7511_sync_polarity vsync_polarity;
46 enum adv7511_sync_polarity hsync_polarity;
51 struct gpio_desc *gpio_pd;
54 static const int edid_i2c_addr = 0x7e;
55 static const int packet_i2c_addr = 0x70;
56 static const int cec_i2c_addr = 0x78;
58 static struct adv7511 *encoder_to_adv7511(struct drm_encoder *encoder)
60 return to_encoder_slave(encoder)->slave_priv;
63 /* ADI recommended values for proper operation. */
64 static const struct reg_sequence adv7511_fixed_registers[] = {
76 /* -----------------------------------------------------------------------------
80 static const uint8_t adv7511_register_defaults[] = {
81 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
82 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
83 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
84 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
85 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
86 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
88 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
89 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
90 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
92 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
94 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
98 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
100 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
101 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
102 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
103 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
104 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
106 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
107 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
108 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
109 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
110 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
112 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115 static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
118 case ADV7511_REG_CHIP_REVISION:
119 case ADV7511_REG_SPDIF_FREQ:
120 case ADV7511_REG_CTS_AUTOMATIC1:
121 case ADV7511_REG_CTS_AUTOMATIC2:
122 case ADV7511_REG_VIC_DETECTED:
123 case ADV7511_REG_VIC_SEND:
124 case ADV7511_REG_AUX_VIC_DETECTED:
125 case ADV7511_REG_STATUS:
126 case ADV7511_REG_GC(1):
127 case ADV7511_REG_INT(0):
128 case ADV7511_REG_INT(1):
129 case ADV7511_REG_PLL_STATUS:
130 case ADV7511_REG_AN(0):
131 case ADV7511_REG_AN(1):
132 case ADV7511_REG_AN(2):
133 case ADV7511_REG_AN(3):
134 case ADV7511_REG_AN(4):
135 case ADV7511_REG_AN(5):
136 case ADV7511_REG_AN(6):
137 case ADV7511_REG_AN(7):
138 case ADV7511_REG_HDCP_STATUS:
139 case ADV7511_REG_BCAPS:
140 case ADV7511_REG_BKSV(0):
141 case ADV7511_REG_BKSV(1):
142 case ADV7511_REG_BKSV(2):
143 case ADV7511_REG_BKSV(3):
144 case ADV7511_REG_BKSV(4):
145 case ADV7511_REG_DDC_STATUS:
146 case ADV7511_REG_BSTATUS(0):
147 case ADV7511_REG_BSTATUS(1):
148 case ADV7511_REG_CHIP_ID_HIGH:
149 case ADV7511_REG_CHIP_ID_LOW:
156 static const struct regmap_config adv7511_regmap_config = {
160 .max_register = 0xff,
161 .cache_type = REGCACHE_RBTREE,
162 .reg_defaults_raw = adv7511_register_defaults,
163 .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
165 .volatile_reg = adv7511_register_volatile,
168 /* -----------------------------------------------------------------------------
169 * Hardware configuration
172 static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
173 const uint16_t *coeff,
174 unsigned int scaling_factor)
178 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
179 ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
182 for (i = 0; i < 12; ++i) {
183 regmap_update_bits(adv7511->regmap,
184 ADV7511_REG_CSC_UPPER(i),
185 0x1f, coeff[i] >> 8);
186 regmap_write(adv7511->regmap,
187 ADV7511_REG_CSC_LOWER(i),
193 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
194 0xe0, 0x80 | (scaling_factor << 5));
196 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
199 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
200 ADV7511_CSC_UPDATE_MODE, 0);
203 static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
206 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
209 if (packet & 0xff00) {
211 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
218 static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
221 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
224 if (packet & 0xff00) {
226 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
233 /* Coefficients for adv7511 color space conversion */
234 static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
235 0x0734, 0x04ad, 0x0000, 0x1c1b,
236 0x1ddc, 0x04ad, 0x1f24, 0x0135,
237 0x0000, 0x04ad, 0x087c, 0x1b77,
240 static void adv7511_set_config_csc(struct adv7511 *adv7511,
241 struct drm_connector *connector,
244 struct adv7511_video_config config;
245 bool output_format_422, output_format_ycbcr;
247 uint8_t infoframe[17];
250 config.hdmi_mode = drm_detect_hdmi_monitor(adv7511->edid);
252 config.hdmi_mode = false;
254 hdmi_avi_infoframe_init(&config.avi_infoframe);
256 config.avi_infoframe.scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
259 config.csc_enable = false;
260 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
262 config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
263 config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
265 if ((connector->display_info.color_formats &
266 DRM_COLOR_FORMAT_YCRCB422) &&
268 config.csc_enable = false;
269 config.avi_infoframe.colorspace =
270 HDMI_COLORSPACE_YUV422;
272 config.csc_enable = true;
273 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
277 if (config.hdmi_mode) {
278 mode = ADV7511_HDMI_CFG_MODE_HDMI;
280 switch (config.avi_infoframe.colorspace) {
281 case HDMI_COLORSPACE_YUV444:
282 output_format_422 = false;
283 output_format_ycbcr = true;
285 case HDMI_COLORSPACE_YUV422:
286 output_format_422 = true;
287 output_format_ycbcr = true;
290 output_format_422 = false;
291 output_format_ycbcr = false;
295 mode = ADV7511_HDMI_CFG_MODE_DVI;
296 output_format_422 = false;
297 output_format_ycbcr = false;
300 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
302 adv7511_set_colormap(adv7511, config.csc_enable,
303 config.csc_coefficents,
304 config.csc_scaling_factor);
306 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
307 (output_format_422 << 7) | output_format_ycbcr);
309 regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
310 ADV7511_HDMI_CFG_MODE_MASK, mode);
312 hdmi_avi_infoframe_pack(&config.avi_infoframe, infoframe,
315 /* The AVI infoframe id is not configurable */
316 regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
317 infoframe + 1, sizeof(infoframe) - 1);
319 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
322 static void adv7511_set_link_config(struct adv7511 *adv7511,
323 const struct adv7511_link_config *config)
326 * The input style values documented in the datasheet don't match the
327 * hardware register field values :-(
329 static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
331 unsigned int clock_delay;
332 unsigned int color_depth;
333 unsigned int input_id;
335 clock_delay = (config->clock_delay + 1200) / 400;
336 color_depth = config->input_color_depth == 8 ? 3
337 : (config->input_color_depth == 10 ? 1 : 2);
339 /* TODO Support input ID 6 */
340 if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
341 input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
343 else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
344 input_id = config->embedded_sync ? 8 : 7;
345 else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
346 input_id = config->embedded_sync ? 4 : 3;
348 input_id = config->embedded_sync ? 2 : 1;
350 regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
352 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
354 (input_styles[config->input_style] << 2));
355 regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
356 config->input_justification << 3);
357 regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
358 config->sync_pulse << 2);
360 regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
362 adv7511->embedded_sync = config->embedded_sync;
363 adv7511->hsync_polarity = config->hsync_polarity;
364 adv7511->vsync_polarity = config->vsync_polarity;
365 adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
368 static void adv7511_power_on(struct adv7511 *adv7511)
370 adv7511->current_edid_segment = -1;
372 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
373 ADV7511_POWER_POWER_DOWN, 0);
374 if (adv7511->i2c_main->irq) {
376 * Documentation says the INT_ENABLE registers are reset in
377 * POWER_DOWN mode. My 7511w preserved the bits, however.
378 * Still, let's be safe and stick to the documentation.
380 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
381 ADV7511_INT0_EDID_READY);
382 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(1),
383 ADV7511_INT1_DDC_ERROR);
387 * Per spec it is allowed to pulse the HDP signal to indicate that the
388 * EDID information has changed. Some monitors do this when they wakeup
389 * from standby or are enabled. When the HDP goes low the adv7511 is
390 * reset and the outputs are disabled which might cause the monitor to
391 * go to standby again. To avoid this we ignore the HDP pin for the
392 * first few seconds after enabling the output.
394 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
395 ADV7511_REG_POWER2_HDP_SRC_MASK,
396 ADV7511_REG_POWER2_HDP_SRC_NONE);
399 * Most of the registers are reset during power down or when HPD is low.
401 regcache_sync(adv7511->regmap);
403 adv7511->powered = true;
406 static void adv7511_power_off(struct adv7511 *adv7511)
408 /* TODO: setup additional power down modes */
409 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
410 ADV7511_POWER_POWER_DOWN,
411 ADV7511_POWER_POWER_DOWN);
412 regcache_mark_dirty(adv7511->regmap);
414 adv7511->powered = false;
417 /* -----------------------------------------------------------------------------
418 * Interrupt and hotplug detection
421 static bool adv7511_hpd(struct adv7511 *adv7511)
426 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
430 if (irq0 & ADV7511_INT0_HDP) {
431 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
439 static void adv7511_hpd_work(struct work_struct *work)
441 struct adv7511 *adv7511 = container_of(work, struct adv7511, hpd_work);
442 enum drm_connector_status status;
445 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
447 status = connector_status_disconnected;
448 else if (val & ADV7511_STATUS_HPD)
449 status = connector_status_connected;
451 status = connector_status_disconnected;
454 * The bridge resets its registers on unplug. So when we get a plug
455 * event and we're already supposed to be powered, cycle the bridge to
458 if (status == connector_status_connected &&
459 adv7511->connector.status == connector_status_disconnected &&
461 regcache_mark_dirty(adv7511->regmap);
462 adv7511_power_on(adv7511);
465 if (adv7511->connector.status != status) {
466 adv7511->connector.status = status;
467 drm_kms_helper_hotplug_event(adv7511->connector.dev);
471 static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
473 unsigned int irq0, irq1;
476 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
480 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
484 regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
485 regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
487 if (process_hpd && irq0 & ADV7511_INT0_HDP && adv7511->encoder)
488 schedule_work(&adv7511->hpd_work);
490 if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
491 adv7511->edid_read = true;
493 if (adv7511->i2c_main->irq)
494 wake_up_all(&adv7511->wq);
500 static irqreturn_t adv7511_irq_handler(int irq, void *devid)
502 struct adv7511 *adv7511 = devid;
505 ret = adv7511_irq_process(adv7511, true);
506 return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
509 /* -----------------------------------------------------------------------------
513 static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
517 if (adv7511->i2c_main->irq) {
518 ret = wait_event_interruptible_timeout(adv7511->wq,
519 adv7511->edid_read, msecs_to_jiffies(timeout));
521 for (; timeout > 0; timeout -= 25) {
522 ret = adv7511_irq_process(adv7511, false);
526 if (adv7511->edid_read)
533 return adv7511->edid_read ? 0 : -EIO;
536 static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
539 struct adv7511 *adv7511 = data;
540 struct i2c_msg xfer[2];
548 if (adv7511->current_edid_segment != block / 2) {
551 ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
557 adv7511->edid_read = false;
558 regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
560 ret = adv7511_wait_for_edid(adv7511, 200);
565 /* Break this apart, hopefully more I2C controllers will
566 * support 64 byte transfers than 256 byte transfers
569 xfer[0].addr = adv7511->i2c_edid->addr;
572 xfer[0].buf = &offset;
573 xfer[1].addr = adv7511->i2c_edid->addr;
574 xfer[1].flags = I2C_M_RD;
576 xfer[1].buf = adv7511->edid_buf;
580 for (i = 0; i < 4; ++i) {
581 ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
592 adv7511->current_edid_segment = block / 2;
596 memcpy(buf, adv7511->edid_buf, len);
598 memcpy(buf, adv7511->edid_buf + 128, len);
603 /* -----------------------------------------------------------------------------
607 static int adv7511_get_modes(struct drm_encoder *encoder,
608 struct drm_connector *connector)
610 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
614 /* Reading the EDID only works if the device is powered */
615 if (!adv7511->powered) {
616 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
617 ADV7511_POWER_POWER_DOWN, 0);
618 if (adv7511->i2c_main->irq) {
619 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
620 ADV7511_INT0_EDID_READY);
621 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(1),
622 ADV7511_INT1_DDC_ERROR);
624 adv7511->current_edid_segment = -1;
625 /* Reset the EDID_I2C_ADDR register as it might be cleared */
626 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
630 edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
632 if (!adv7511->powered)
633 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
634 ADV7511_POWER_POWER_DOWN,
635 ADV7511_POWER_POWER_DOWN);
637 kfree(adv7511->edid);
638 adv7511->edid = edid;
642 drm_mode_connector_update_edid_property(connector, edid);
643 count = drm_add_edid_modes(connector, edid);
645 adv7511_set_config_csc(adv7511, connector, adv7511->rgb);
650 static void adv7511_encoder_dpms(struct drm_encoder *encoder, int mode)
652 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
654 if (mode == DRM_MODE_DPMS_ON)
655 adv7511_power_on(adv7511);
657 adv7511_power_off(adv7511);
660 static enum drm_connector_status
661 adv7511_encoder_detect(struct drm_encoder *encoder,
662 struct drm_connector *connector)
664 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
665 enum drm_connector_status status;
670 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
672 return connector_status_disconnected;
674 if (val & ADV7511_STATUS_HPD)
675 status = connector_status_connected;
677 status = connector_status_disconnected;
679 hpd = adv7511_hpd(adv7511);
681 /* The chip resets itself when the cable is disconnected, so in case
682 * there is a pending HPD interrupt and the cable is connected there was
683 * at least one transition from disconnected to connected and the chip
684 * has to be reinitialized. */
685 if (status == connector_status_connected && hpd && adv7511->powered) {
686 regcache_mark_dirty(adv7511->regmap);
687 adv7511_power_on(adv7511);
688 adv7511_get_modes(encoder, connector);
689 if (adv7511->status == connector_status_connected)
690 status = connector_status_disconnected;
692 /* Renable HDP sensing */
693 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
694 ADV7511_REG_POWER2_HDP_SRC_MASK,
695 ADV7511_REG_POWER2_HDP_SRC_BOTH);
698 adv7511->status = status;
702 static int adv7511_encoder_mode_valid(struct drm_encoder *encoder,
703 struct drm_display_mode *mode)
705 if (mode->clock > 165000)
706 return MODE_CLOCK_HIGH;
711 static void adv7511_encoder_mode_set(struct drm_encoder *encoder,
712 struct drm_display_mode *mode,
713 struct drm_display_mode *adj_mode)
715 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
716 unsigned int low_refresh_rate;
717 unsigned int hsync_polarity = 0;
718 unsigned int vsync_polarity = 0;
720 if (adv7511->embedded_sync) {
721 unsigned int hsync_offset, hsync_len;
722 unsigned int vsync_offset, vsync_len;
724 hsync_offset = adj_mode->crtc_hsync_start -
725 adj_mode->crtc_hdisplay;
726 vsync_offset = adj_mode->crtc_vsync_start -
727 adj_mode->crtc_vdisplay;
728 hsync_len = adj_mode->crtc_hsync_end -
729 adj_mode->crtc_hsync_start;
730 vsync_len = adj_mode->crtc_vsync_end -
731 adj_mode->crtc_vsync_start;
733 /* The hardware vsync generator has a off-by-one bug */
736 regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
737 ((hsync_offset >> 10) & 0x7) << 5);
738 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
739 (hsync_offset >> 2) & 0xff);
740 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
741 ((hsync_offset & 0x3) << 6) |
742 ((hsync_len >> 4) & 0x3f));
743 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
744 ((hsync_len & 0xf) << 4) |
745 ((vsync_offset >> 6) & 0xf));
746 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
747 ((vsync_offset & 0x3f) << 2) |
748 ((vsync_len >> 8) & 0x3));
749 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
752 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
753 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
755 enum adv7511_sync_polarity mode_hsync_polarity;
756 enum adv7511_sync_polarity mode_vsync_polarity;
759 * If the input signal is always low or always high we want to
760 * invert or let it passthrough depending on the polarity of the
763 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
764 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
766 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
768 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
769 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
771 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
773 if (adv7511->hsync_polarity != mode_hsync_polarity &&
774 adv7511->hsync_polarity !=
775 ADV7511_SYNC_POLARITY_PASSTHROUGH)
778 if (adv7511->vsync_polarity != mode_vsync_polarity &&
779 adv7511->vsync_polarity !=
780 ADV7511_SYNC_POLARITY_PASSTHROUGH)
784 if (drm_mode_vrefresh(mode) <= 24)
785 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
786 else if (drm_mode_vrefresh(mode) <= 25)
787 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
788 else if (drm_mode_vrefresh(mode) <= 30)
789 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
791 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
793 regmap_update_bits(adv7511->regmap, 0xfb,
794 0x6, low_refresh_rate << 1);
795 regmap_update_bits(adv7511->regmap, 0x17,
796 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
799 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
800 * supposed to give better results.
803 adv7511->f_tmds = mode->clock;
806 static struct drm_encoder_slave_funcs adv7511_encoder_funcs = {
807 .dpms = adv7511_encoder_dpms,
808 .mode_valid = adv7511_encoder_mode_valid,
809 .mode_set = adv7511_encoder_mode_set,
810 .detect = adv7511_encoder_detect,
811 .get_modes = adv7511_get_modes,
814 /* -----------------------------------------------------------------------------
818 static int adv7511_parse_dt(struct device_node *np,
819 struct adv7511_link_config *config)
824 memset(config, 0, sizeof(*config));
826 of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
827 if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
828 config->input_color_depth != 12)
831 ret = of_property_read_string(np, "adi,input-colorspace", &str);
835 if (!strcmp(str, "rgb"))
836 config->input_colorspace = HDMI_COLORSPACE_RGB;
837 else if (!strcmp(str, "yuv422"))
838 config->input_colorspace = HDMI_COLORSPACE_YUV422;
839 else if (!strcmp(str, "yuv444"))
840 config->input_colorspace = HDMI_COLORSPACE_YUV444;
844 ret = of_property_read_string(np, "adi,input-clock", &str);
848 if (!strcmp(str, "1x"))
849 config->input_clock = ADV7511_INPUT_CLOCK_1X;
850 else if (!strcmp(str, "2x"))
851 config->input_clock = ADV7511_INPUT_CLOCK_2X;
852 else if (!strcmp(str, "ddr"))
853 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
857 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
858 config->input_clock != ADV7511_INPUT_CLOCK_1X) {
859 ret = of_property_read_u32(np, "adi,input-style",
860 &config->input_style);
864 if (config->input_style < 1 || config->input_style > 3)
867 ret = of_property_read_string(np, "adi,input-justification",
872 if (!strcmp(str, "left"))
873 config->input_justification =
874 ADV7511_INPUT_JUSTIFICATION_LEFT;
875 else if (!strcmp(str, "evenly"))
876 config->input_justification =
877 ADV7511_INPUT_JUSTIFICATION_EVENLY;
878 else if (!strcmp(str, "right"))
879 config->input_justification =
880 ADV7511_INPUT_JUSTIFICATION_RIGHT;
885 config->input_style = 1;
886 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
889 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
890 if (config->clock_delay < -1200 || config->clock_delay > 1600)
893 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
895 /* Hardcode the sync pulse configurations for now. */
896 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
897 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
898 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
903 static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
905 struct adv7511_link_config link_config;
906 struct adv7511 *adv7511;
907 struct device *dev = &i2c->dev;
914 adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
918 adv7511->powered = false;
919 adv7511->status = connector_status_disconnected;
921 ret = adv7511_parse_dt(dev->of_node, &link_config);
926 * The power down GPIO is optional. If present, toggle it from active to
927 * inactive to wake up the encoder.
929 adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
930 if (IS_ERR(adv7511->gpio_pd))
931 return PTR_ERR(adv7511->gpio_pd);
933 if (adv7511->gpio_pd) {
935 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
938 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
939 if (IS_ERR(adv7511->regmap))
940 return PTR_ERR(adv7511->regmap);
942 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
945 dev_dbg(dev, "Rev. %d\n", val);
947 ret = regmap_register_patch(adv7511->regmap, adv7511_fixed_registers,
948 ARRAY_SIZE(adv7511_fixed_registers));
952 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
953 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
955 regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR, cec_i2c_addr);
956 adv7511_packet_disable(adv7511, 0xffff);
958 adv7511->i2c_main = i2c;
959 adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
960 if (!adv7511->i2c_edid)
963 INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
966 init_waitqueue_head(&adv7511->wq);
968 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
970 IRQF_ONESHOT, dev_name(dev),
973 goto err_i2c_unregister_device;
976 /* CEC is unused for now */
977 regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
978 ADV7511_CEC_CTRL_POWER_DOWN);
980 adv7511_power_off(adv7511);
982 i2c_set_clientdata(i2c, adv7511);
984 adv7511_set_link_config(adv7511, &link_config);
988 err_i2c_unregister_device:
989 i2c_unregister_device(adv7511->i2c_edid);
994 static int adv7511_remove(struct i2c_client *i2c)
996 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
998 i2c_unregister_device(adv7511->i2c_edid);
1000 kfree(adv7511->edid);
1005 static int adv7511_encoder_init(struct i2c_client *i2c, struct drm_device *dev,
1006 struct drm_encoder_slave *encoder)
1009 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1011 encoder->slave_priv = adv7511;
1012 encoder->slave_funcs = &adv7511_encoder_funcs;
1014 adv7511->encoder = &encoder->base;
1019 static const struct i2c_device_id adv7511_i2c_ids[] = {
1025 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1027 static const struct of_device_id adv7511_of_ids[] = {
1028 { .compatible = "adi,adv7511", },
1029 { .compatible = "adi,adv7511w", },
1030 { .compatible = "adi,adv7513", },
1033 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1035 static struct drm_i2c_encoder_driver adv7511_driver = {
1039 .of_match_table = adv7511_of_ids,
1041 .id_table = adv7511_i2c_ids,
1042 .probe = adv7511_probe,
1043 .remove = adv7511_remove,
1046 .encoder_init = adv7511_encoder_init,
1049 static int __init adv7511_init(void)
1051 return drm_i2c_encoder_register(THIS_MODULE, &adv7511_driver);
1053 module_init(adv7511_init);
1055 static void __exit adv7511_exit(void)
1057 drm_i2c_encoder_unregister(&adv7511_driver);
1059 module_exit(adv7511_exit);
1061 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1062 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1063 MODULE_LICENSE("GPL");