1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog Devices ADV7511 HDMI transmitter driver
5 * Copyright 2012 Analog Devices Inc.
9 #include <linux/device.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/slab.h>
15 #include <media/cec.h>
17 #include <drm/drm_atomic.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_edid.h>
20 #include <drm/drm_print.h>
21 #include <drm/drm_probe_helper.h>
25 /* ADI recommended values for proper operation. */
26 static const struct reg_sequence adv7511_fixed_registers[] = {
38 /* -----------------------------------------------------------------------------
42 static const uint8_t adv7511_register_defaults[] = {
43 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
44 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
45 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
46 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
47 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
48 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
50 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
51 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
52 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
62 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
63 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
64 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
68 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
69 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
71 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
74 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
80 case ADV7511_REG_CHIP_REVISION:
81 case ADV7511_REG_SPDIF_FREQ:
82 case ADV7511_REG_CTS_AUTOMATIC1:
83 case ADV7511_REG_CTS_AUTOMATIC2:
84 case ADV7511_REG_VIC_DETECTED:
85 case ADV7511_REG_VIC_SEND:
86 case ADV7511_REG_AUX_VIC_DETECTED:
87 case ADV7511_REG_STATUS:
88 case ADV7511_REG_GC(1):
89 case ADV7511_REG_INT(0):
90 case ADV7511_REG_INT(1):
91 case ADV7511_REG_PLL_STATUS:
92 case ADV7511_REG_AN(0):
93 case ADV7511_REG_AN(1):
94 case ADV7511_REG_AN(2):
95 case ADV7511_REG_AN(3):
96 case ADV7511_REG_AN(4):
97 case ADV7511_REG_AN(5):
98 case ADV7511_REG_AN(6):
99 case ADV7511_REG_AN(7):
100 case ADV7511_REG_HDCP_STATUS:
101 case ADV7511_REG_BCAPS:
102 case ADV7511_REG_BKSV(0):
103 case ADV7511_REG_BKSV(1):
104 case ADV7511_REG_BKSV(2):
105 case ADV7511_REG_BKSV(3):
106 case ADV7511_REG_BKSV(4):
107 case ADV7511_REG_DDC_STATUS:
108 case ADV7511_REG_EDID_READ_CTRL:
109 case ADV7511_REG_BSTATUS(0):
110 case ADV7511_REG_BSTATUS(1):
111 case ADV7511_REG_CHIP_ID_HIGH:
112 case ADV7511_REG_CHIP_ID_LOW:
119 static const struct regmap_config adv7511_regmap_config = {
123 .max_register = 0xff,
124 .cache_type = REGCACHE_RBTREE,
125 .reg_defaults_raw = adv7511_register_defaults,
126 .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
128 .volatile_reg = adv7511_register_volatile,
131 /* -----------------------------------------------------------------------------
132 * Hardware configuration
135 static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
136 const uint16_t *coeff,
137 unsigned int scaling_factor)
141 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
142 ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
145 for (i = 0; i < 12; ++i) {
146 regmap_update_bits(adv7511->regmap,
147 ADV7511_REG_CSC_UPPER(i),
148 0x1f, coeff[i] >> 8);
149 regmap_write(adv7511->regmap,
150 ADV7511_REG_CSC_LOWER(i),
156 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
157 0xe0, 0x80 | (scaling_factor << 5));
159 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
162 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
163 ADV7511_CSC_UPDATE_MODE, 0);
166 static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
169 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
172 if (packet & 0xff00) {
174 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
181 static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
184 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
187 if (packet & 0xff00) {
189 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
196 /* Coefficients for adv7511 color space conversion */
197 static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
198 0x0734, 0x04ad, 0x0000, 0x1c1b,
199 0x1ddc, 0x04ad, 0x1f24, 0x0135,
200 0x0000, 0x04ad, 0x087c, 0x1b77,
203 static void adv7511_set_config_csc(struct adv7511 *adv7511,
204 struct drm_connector *connector,
205 bool rgb, bool hdmi_mode)
207 struct adv7511_video_config config;
208 bool output_format_422, output_format_ycbcr;
210 uint8_t infoframe[17];
212 config.hdmi_mode = hdmi_mode;
214 hdmi_avi_infoframe_init(&config.avi_infoframe);
216 config.avi_infoframe.scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
219 config.csc_enable = false;
220 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
222 config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
223 config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
225 if ((connector->display_info.color_formats &
226 DRM_COLOR_FORMAT_YCRCB422) &&
228 config.csc_enable = false;
229 config.avi_infoframe.colorspace =
230 HDMI_COLORSPACE_YUV422;
232 config.csc_enable = true;
233 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
237 if (config.hdmi_mode) {
238 mode = ADV7511_HDMI_CFG_MODE_HDMI;
240 switch (config.avi_infoframe.colorspace) {
241 case HDMI_COLORSPACE_YUV444:
242 output_format_422 = false;
243 output_format_ycbcr = true;
245 case HDMI_COLORSPACE_YUV422:
246 output_format_422 = true;
247 output_format_ycbcr = true;
250 output_format_422 = false;
251 output_format_ycbcr = false;
255 mode = ADV7511_HDMI_CFG_MODE_DVI;
256 output_format_422 = false;
257 output_format_ycbcr = false;
260 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
262 adv7511_set_colormap(adv7511, config.csc_enable,
263 config.csc_coefficents,
264 config.csc_scaling_factor);
266 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
267 (output_format_422 << 7) | output_format_ycbcr);
269 regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
270 ADV7511_HDMI_CFG_MODE_MASK, mode);
272 hdmi_avi_infoframe_pack(&config.avi_infoframe, infoframe,
275 /* The AVI infoframe id is not configurable */
276 regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
277 infoframe + 1, sizeof(infoframe) - 1);
279 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
282 static void adv7511_set_link_config(struct adv7511 *adv7511,
283 const struct adv7511_link_config *config)
286 * The input style values documented in the datasheet don't match the
287 * hardware register field values :-(
289 static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
291 unsigned int clock_delay;
292 unsigned int color_depth;
293 unsigned int input_id;
295 clock_delay = (config->clock_delay + 1200) / 400;
296 color_depth = config->input_color_depth == 8 ? 3
297 : (config->input_color_depth == 10 ? 1 : 2);
299 /* TODO Support input ID 6 */
300 if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
301 input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
303 else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
304 input_id = config->embedded_sync ? 8 : 7;
305 else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
306 input_id = config->embedded_sync ? 4 : 3;
308 input_id = config->embedded_sync ? 2 : 1;
310 regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
312 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
314 (input_styles[config->input_style] << 2));
315 regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
316 config->input_justification << 3);
317 regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
318 config->sync_pulse << 2);
320 regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
322 adv7511->embedded_sync = config->embedded_sync;
323 adv7511->hsync_polarity = config->hsync_polarity;
324 adv7511->vsync_polarity = config->vsync_polarity;
325 adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
328 static void __adv7511_power_on(struct adv7511 *adv7511)
330 adv7511->current_edid_segment = -1;
332 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
333 ADV7511_POWER_POWER_DOWN, 0);
334 if (adv7511->i2c_main->irq) {
336 * Documentation says the INT_ENABLE registers are reset in
337 * POWER_DOWN mode. My 7511w preserved the bits, however.
338 * Still, let's be safe and stick to the documentation.
340 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
341 ADV7511_INT0_EDID_READY | ADV7511_INT0_HPD);
342 regmap_update_bits(adv7511->regmap,
343 ADV7511_REG_INT_ENABLE(1),
344 ADV7511_INT1_DDC_ERROR,
345 ADV7511_INT1_DDC_ERROR);
349 * Per spec it is allowed to pulse the HPD signal to indicate that the
350 * EDID information has changed. Some monitors do this when they wakeup
351 * from standby or are enabled. When the HPD goes low the adv7511 is
352 * reset and the outputs are disabled which might cause the monitor to
353 * go to standby again. To avoid this we ignore the HPD pin for the
354 * first few seconds after enabling the output.
356 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
357 ADV7511_REG_POWER2_HPD_SRC_MASK,
358 ADV7511_REG_POWER2_HPD_SRC_NONE);
361 static void adv7511_power_on(struct adv7511 *adv7511)
363 __adv7511_power_on(adv7511);
366 * Most of the registers are reset during power down or when HPD is low.
368 regcache_sync(adv7511->regmap);
370 if (adv7511->type == ADV7533)
371 adv7533_dsi_power_on(adv7511);
372 adv7511->powered = true;
375 static void __adv7511_power_off(struct adv7511 *adv7511)
377 /* TODO: setup additional power down modes */
378 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
379 ADV7511_POWER_POWER_DOWN,
380 ADV7511_POWER_POWER_DOWN);
381 regmap_update_bits(adv7511->regmap,
382 ADV7511_REG_INT_ENABLE(1),
383 ADV7511_INT1_DDC_ERROR, 0);
384 regcache_mark_dirty(adv7511->regmap);
387 static void adv7511_power_off(struct adv7511 *adv7511)
389 __adv7511_power_off(adv7511);
390 if (adv7511->type == ADV7533)
391 adv7533_dsi_power_off(adv7511);
392 adv7511->powered = false;
395 /* -----------------------------------------------------------------------------
396 * Interrupt and hotplug detection
399 static bool adv7511_hpd(struct adv7511 *adv7511)
404 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
408 if (irq0 & ADV7511_INT0_HPD) {
409 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
417 static void adv7511_hpd_work(struct work_struct *work)
419 struct adv7511 *adv7511 = container_of(work, struct adv7511, hpd_work);
420 enum drm_connector_status status;
424 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
426 status = connector_status_disconnected;
427 else if (val & ADV7511_STATUS_HPD)
428 status = connector_status_connected;
430 status = connector_status_disconnected;
433 * The bridge resets its registers on unplug. So when we get a plug
434 * event and we're already supposed to be powered, cycle the bridge to
437 if (status == connector_status_connected &&
438 adv7511->connector.status == connector_status_disconnected &&
440 regcache_mark_dirty(adv7511->regmap);
441 adv7511_power_on(adv7511);
444 if (adv7511->connector.status != status) {
445 adv7511->connector.status = status;
446 if (status == connector_status_disconnected)
447 cec_phys_addr_invalidate(adv7511->cec_adap);
448 drm_kms_helper_hotplug_event(adv7511->connector.dev);
452 static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
454 unsigned int irq0, irq1;
457 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
461 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
465 regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
466 regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
468 if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder)
469 schedule_work(&adv7511->hpd_work);
471 if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
472 adv7511->edid_read = true;
474 if (adv7511->i2c_main->irq)
475 wake_up_all(&adv7511->wq);
478 #ifdef CONFIG_DRM_I2C_ADV7511_CEC
479 adv7511_cec_irq_process(adv7511, irq1);
485 static irqreturn_t adv7511_irq_handler(int irq, void *devid)
487 struct adv7511 *adv7511 = devid;
490 ret = adv7511_irq_process(adv7511, true);
491 return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
494 /* -----------------------------------------------------------------------------
498 static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
502 if (adv7511->i2c_main->irq) {
503 ret = wait_event_interruptible_timeout(adv7511->wq,
504 adv7511->edid_read, msecs_to_jiffies(timeout));
506 for (; timeout > 0; timeout -= 25) {
507 ret = adv7511_irq_process(adv7511, false);
511 if (adv7511->edid_read)
518 return adv7511->edid_read ? 0 : -EIO;
521 static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
524 struct adv7511 *adv7511 = data;
525 struct i2c_msg xfer[2];
533 if (adv7511->current_edid_segment != block / 2) {
536 ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
542 adv7511->edid_read = false;
543 regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
545 ret = adv7511_wait_for_edid(adv7511, 200);
550 /* Break this apart, hopefully more I2C controllers will
551 * support 64 byte transfers than 256 byte transfers
554 xfer[0].addr = adv7511->i2c_edid->addr;
557 xfer[0].buf = &offset;
558 xfer[1].addr = adv7511->i2c_edid->addr;
559 xfer[1].flags = I2C_M_RD;
561 xfer[1].buf = adv7511->edid_buf;
565 for (i = 0; i < 4; ++i) {
566 ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
577 adv7511->current_edid_segment = block / 2;
581 memcpy(buf, adv7511->edid_buf, len);
583 memcpy(buf, adv7511->edid_buf + 128, len);
588 /* -----------------------------------------------------------------------------
592 static int adv7511_get_modes(struct adv7511 *adv7511,
593 struct drm_connector *connector)
598 /* Reading the EDID only works if the device is powered */
599 if (!adv7511->powered) {
600 unsigned int edid_i2c_addr =
601 (adv7511->i2c_edid->addr << 1);
603 __adv7511_power_on(adv7511);
605 /* Reset the EDID_I2C_ADDR register as it might be cleared */
606 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
610 edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
612 if (!adv7511->powered)
613 __adv7511_power_off(adv7511);
616 drm_connector_update_edid_property(connector, edid);
617 count = drm_add_edid_modes(connector, edid);
619 adv7511_set_config_csc(adv7511, connector, adv7511->rgb,
620 drm_detect_hdmi_monitor(edid));
622 cec_s_phys_addr_from_edid(adv7511->cec_adap, edid);
629 static enum drm_connector_status
630 adv7511_detect(struct adv7511 *adv7511, struct drm_connector *connector)
632 enum drm_connector_status status;
637 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
639 return connector_status_disconnected;
641 if (val & ADV7511_STATUS_HPD)
642 status = connector_status_connected;
644 status = connector_status_disconnected;
646 hpd = adv7511_hpd(adv7511);
648 /* The chip resets itself when the cable is disconnected, so in case
649 * there is a pending HPD interrupt and the cable is connected there was
650 * at least one transition from disconnected to connected and the chip
651 * has to be reinitialized. */
652 if (status == connector_status_connected && hpd && adv7511->powered) {
653 regcache_mark_dirty(adv7511->regmap);
654 adv7511_power_on(adv7511);
655 adv7511_get_modes(adv7511, connector);
656 if (adv7511->status == connector_status_connected)
657 status = connector_status_disconnected;
659 /* Renable HPD sensing */
660 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
661 ADV7511_REG_POWER2_HPD_SRC_MASK,
662 ADV7511_REG_POWER2_HPD_SRC_BOTH);
665 adv7511->status = status;
669 static enum drm_mode_status adv7511_mode_valid(struct adv7511 *adv7511,
670 struct drm_display_mode *mode)
672 if (mode->clock > 165000)
673 return MODE_CLOCK_HIGH;
678 static void adv7511_mode_set(struct adv7511 *adv7511,
679 const struct drm_display_mode *mode,
680 const struct drm_display_mode *adj_mode)
682 unsigned int low_refresh_rate;
683 unsigned int hsync_polarity = 0;
684 unsigned int vsync_polarity = 0;
686 if (adv7511->embedded_sync) {
687 unsigned int hsync_offset, hsync_len;
688 unsigned int vsync_offset, vsync_len;
690 hsync_offset = adj_mode->crtc_hsync_start -
691 adj_mode->crtc_hdisplay;
692 vsync_offset = adj_mode->crtc_vsync_start -
693 adj_mode->crtc_vdisplay;
694 hsync_len = adj_mode->crtc_hsync_end -
695 adj_mode->crtc_hsync_start;
696 vsync_len = adj_mode->crtc_vsync_end -
697 adj_mode->crtc_vsync_start;
699 /* The hardware vsync generator has a off-by-one bug */
702 regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
703 ((hsync_offset >> 10) & 0x7) << 5);
704 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
705 (hsync_offset >> 2) & 0xff);
706 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
707 ((hsync_offset & 0x3) << 6) |
708 ((hsync_len >> 4) & 0x3f));
709 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
710 ((hsync_len & 0xf) << 4) |
711 ((vsync_offset >> 6) & 0xf));
712 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
713 ((vsync_offset & 0x3f) << 2) |
714 ((vsync_len >> 8) & 0x3));
715 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
718 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
719 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
721 enum adv7511_sync_polarity mode_hsync_polarity;
722 enum adv7511_sync_polarity mode_vsync_polarity;
725 * If the input signal is always low or always high we want to
726 * invert or let it passthrough depending on the polarity of the
729 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
730 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
732 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
734 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
735 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
737 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
739 if (adv7511->hsync_polarity != mode_hsync_polarity &&
740 adv7511->hsync_polarity !=
741 ADV7511_SYNC_POLARITY_PASSTHROUGH)
744 if (adv7511->vsync_polarity != mode_vsync_polarity &&
745 adv7511->vsync_polarity !=
746 ADV7511_SYNC_POLARITY_PASSTHROUGH)
750 if (drm_mode_vrefresh(mode) <= 24)
751 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
752 else if (drm_mode_vrefresh(mode) <= 25)
753 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
754 else if (drm_mode_vrefresh(mode) <= 30)
755 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
757 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
759 regmap_update_bits(adv7511->regmap, 0xfb,
760 0x6, low_refresh_rate << 1);
761 regmap_update_bits(adv7511->regmap, 0x17,
762 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
764 if (adv7511->type == ADV7533)
765 adv7533_mode_set(adv7511, adj_mode);
767 drm_mode_copy(&adv7511->curr_mode, adj_mode);
770 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
771 * supposed to give better results.
774 adv7511->f_tmds = mode->clock;
777 /* Connector funcs */
778 static struct adv7511 *connector_to_adv7511(struct drm_connector *connector)
780 return container_of(connector, struct adv7511, connector);
783 static int adv7511_connector_get_modes(struct drm_connector *connector)
785 struct adv7511 *adv = connector_to_adv7511(connector);
787 return adv7511_get_modes(adv, connector);
790 static enum drm_mode_status
791 adv7511_connector_mode_valid(struct drm_connector *connector,
792 struct drm_display_mode *mode)
794 struct adv7511 *adv = connector_to_adv7511(connector);
796 return adv7511_mode_valid(adv, mode);
799 static struct drm_connector_helper_funcs adv7511_connector_helper_funcs = {
800 .get_modes = adv7511_connector_get_modes,
801 .mode_valid = adv7511_connector_mode_valid,
804 static enum drm_connector_status
805 adv7511_connector_detect(struct drm_connector *connector, bool force)
807 struct adv7511 *adv = connector_to_adv7511(connector);
809 return adv7511_detect(adv, connector);
812 static const struct drm_connector_funcs adv7511_connector_funcs = {
813 .fill_modes = drm_helper_probe_single_connector_modes,
814 .detect = adv7511_connector_detect,
815 .destroy = drm_connector_cleanup,
816 .reset = drm_atomic_helper_connector_reset,
817 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
818 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
822 static struct adv7511 *bridge_to_adv7511(struct drm_bridge *bridge)
824 return container_of(bridge, struct adv7511, bridge);
827 static void adv7511_bridge_enable(struct drm_bridge *bridge)
829 struct adv7511 *adv = bridge_to_adv7511(bridge);
831 adv7511_power_on(adv);
834 static void adv7511_bridge_disable(struct drm_bridge *bridge)
836 struct adv7511 *adv = bridge_to_adv7511(bridge);
838 adv7511_power_off(adv);
841 static void adv7511_bridge_mode_set(struct drm_bridge *bridge,
842 const struct drm_display_mode *mode,
843 const struct drm_display_mode *adj_mode)
845 struct adv7511 *adv = bridge_to_adv7511(bridge);
847 adv7511_mode_set(adv, mode, adj_mode);
850 static int adv7511_bridge_attach(struct drm_bridge *bridge)
852 struct adv7511 *adv = bridge_to_adv7511(bridge);
855 if (!bridge->encoder) {
856 DRM_ERROR("Parent encoder object not found");
860 if (adv->i2c_main->irq)
861 adv->connector.polled = DRM_CONNECTOR_POLL_HPD;
863 adv->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
864 DRM_CONNECTOR_POLL_DISCONNECT;
866 ret = drm_connector_init(bridge->dev, &adv->connector,
867 &adv7511_connector_funcs,
868 DRM_MODE_CONNECTOR_HDMIA);
870 DRM_ERROR("Failed to initialize connector with drm\n");
873 drm_connector_helper_add(&adv->connector,
874 &adv7511_connector_helper_funcs);
875 drm_connector_attach_encoder(&adv->connector, bridge->encoder);
877 if (adv->i2c_main->irq)
878 regmap_write(adv->regmap, ADV7511_REG_INT_ENABLE(0),
884 static const struct drm_bridge_funcs adv7511_bridge_funcs = {
885 .enable = adv7511_bridge_enable,
886 .disable = adv7511_bridge_disable,
887 .mode_set = adv7511_bridge_mode_set,
888 .attach = adv7511_bridge_attach,
891 /* -----------------------------------------------------------------------------
895 static const char * const adv7511_supply_names[] = {
903 static const char * const adv7533_supply_names[] = {
912 static int adv7511_init_regulators(struct adv7511 *adv)
914 struct device *dev = &adv->i2c_main->dev;
915 const char * const *supply_names;
919 if (adv->type == ADV7511) {
920 supply_names = adv7511_supply_names;
921 adv->num_supplies = ARRAY_SIZE(adv7511_supply_names);
923 supply_names = adv7533_supply_names;
924 adv->num_supplies = ARRAY_SIZE(adv7533_supply_names);
927 adv->supplies = devm_kcalloc(dev, adv->num_supplies,
928 sizeof(*adv->supplies), GFP_KERNEL);
932 for (i = 0; i < adv->num_supplies; i++)
933 adv->supplies[i].supply = supply_names[i];
935 ret = devm_regulator_bulk_get(dev, adv->num_supplies, adv->supplies);
939 return regulator_bulk_enable(adv->num_supplies, adv->supplies);
942 static void adv7511_uninit_regulators(struct adv7511 *adv)
944 regulator_bulk_disable(adv->num_supplies, adv->supplies);
947 static bool adv7511_cec_register_volatile(struct device *dev, unsigned int reg)
949 struct i2c_client *i2c = to_i2c_client(dev);
950 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
952 if (adv7511->type == ADV7533)
953 reg -= ADV7533_REG_CEC_OFFSET;
956 case ADV7511_REG_CEC_RX_FRAME_HDR:
957 case ADV7511_REG_CEC_RX_FRAME_DATA0...
958 ADV7511_REG_CEC_RX_FRAME_DATA0 + 14:
959 case ADV7511_REG_CEC_RX_FRAME_LEN:
960 case ADV7511_REG_CEC_RX_BUFFERS:
961 case ADV7511_REG_CEC_TX_LOW_DRV_CNT:
968 static const struct regmap_config adv7511_cec_regmap_config = {
972 .max_register = 0xff,
973 .cache_type = REGCACHE_RBTREE,
974 .volatile_reg = adv7511_cec_register_volatile,
977 static int adv7511_init_cec_regmap(struct adv7511 *adv)
981 adv->i2c_cec = i2c_new_secondary_device(adv->i2c_main, "cec",
982 ADV7511_CEC_I2C_ADDR_DEFAULT);
985 i2c_set_clientdata(adv->i2c_cec, adv);
987 adv->regmap_cec = devm_regmap_init_i2c(adv->i2c_cec,
988 &adv7511_cec_regmap_config);
989 if (IS_ERR(adv->regmap_cec)) {
990 ret = PTR_ERR(adv->regmap_cec);
994 if (adv->type == ADV7533) {
995 ret = adv7533_patch_cec_registers(adv);
1002 i2c_unregister_device(adv->i2c_cec);
1006 static int adv7511_parse_dt(struct device_node *np,
1007 struct adv7511_link_config *config)
1012 of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
1013 if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
1014 config->input_color_depth != 12)
1017 ret = of_property_read_string(np, "adi,input-colorspace", &str);
1021 if (!strcmp(str, "rgb"))
1022 config->input_colorspace = HDMI_COLORSPACE_RGB;
1023 else if (!strcmp(str, "yuv422"))
1024 config->input_colorspace = HDMI_COLORSPACE_YUV422;
1025 else if (!strcmp(str, "yuv444"))
1026 config->input_colorspace = HDMI_COLORSPACE_YUV444;
1030 ret = of_property_read_string(np, "adi,input-clock", &str);
1034 if (!strcmp(str, "1x"))
1035 config->input_clock = ADV7511_INPUT_CLOCK_1X;
1036 else if (!strcmp(str, "2x"))
1037 config->input_clock = ADV7511_INPUT_CLOCK_2X;
1038 else if (!strcmp(str, "ddr"))
1039 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
1043 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
1044 config->input_clock != ADV7511_INPUT_CLOCK_1X) {
1045 ret = of_property_read_u32(np, "adi,input-style",
1046 &config->input_style);
1050 if (config->input_style < 1 || config->input_style > 3)
1053 ret = of_property_read_string(np, "adi,input-justification",
1058 if (!strcmp(str, "left"))
1059 config->input_justification =
1060 ADV7511_INPUT_JUSTIFICATION_LEFT;
1061 else if (!strcmp(str, "evenly"))
1062 config->input_justification =
1063 ADV7511_INPUT_JUSTIFICATION_EVENLY;
1064 else if (!strcmp(str, "right"))
1065 config->input_justification =
1066 ADV7511_INPUT_JUSTIFICATION_RIGHT;
1071 config->input_style = 1;
1072 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
1075 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
1076 if (config->clock_delay < -1200 || config->clock_delay > 1600)
1079 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
1081 /* Hardcode the sync pulse configurations for now. */
1082 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
1083 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1084 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1089 static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
1091 struct adv7511_link_config link_config;
1092 struct adv7511 *adv7511;
1093 struct device *dev = &i2c->dev;
1100 adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
1104 adv7511->i2c_main = i2c;
1105 adv7511->powered = false;
1106 adv7511->status = connector_status_disconnected;
1109 adv7511->type = (enum adv7511_type)of_device_get_match_data(dev);
1111 adv7511->type = id->driver_data;
1113 memset(&link_config, 0, sizeof(link_config));
1115 if (adv7511->type == ADV7511)
1116 ret = adv7511_parse_dt(dev->of_node, &link_config);
1118 ret = adv7533_parse_dt(dev->of_node, adv7511);
1122 ret = adv7511_init_regulators(adv7511);
1124 dev_err(dev, "failed to init regulators\n");
1129 * The power down GPIO is optional. If present, toggle it from active to
1130 * inactive to wake up the encoder.
1132 adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
1133 if (IS_ERR(adv7511->gpio_pd)) {
1134 ret = PTR_ERR(adv7511->gpio_pd);
1135 goto uninit_regulators;
1138 if (adv7511->gpio_pd) {
1139 usleep_range(5000, 6000);
1140 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
1143 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
1144 if (IS_ERR(adv7511->regmap)) {
1145 ret = PTR_ERR(adv7511->regmap);
1146 goto uninit_regulators;
1149 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
1151 goto uninit_regulators;
1152 dev_dbg(dev, "Rev. %d\n", val);
1154 if (adv7511->type == ADV7511)
1155 ret = regmap_register_patch(adv7511->regmap,
1156 adv7511_fixed_registers,
1157 ARRAY_SIZE(adv7511_fixed_registers));
1159 ret = adv7533_patch_registers(adv7511);
1161 goto uninit_regulators;
1163 adv7511_packet_disable(adv7511, 0xffff);
1165 adv7511->i2c_edid = i2c_new_secondary_device(i2c, "edid",
1166 ADV7511_EDID_I2C_ADDR_DEFAULT);
1167 if (!adv7511->i2c_edid) {
1169 goto uninit_regulators;
1172 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
1173 adv7511->i2c_edid->addr << 1);
1175 adv7511->i2c_packet = i2c_new_secondary_device(i2c, "packet",
1176 ADV7511_PACKET_I2C_ADDR_DEFAULT);
1177 if (!adv7511->i2c_packet) {
1179 goto err_i2c_unregister_edid;
1182 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
1183 adv7511->i2c_packet->addr << 1);
1185 ret = adv7511_init_cec_regmap(adv7511);
1187 goto err_i2c_unregister_packet;
1189 regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR,
1190 adv7511->i2c_cec->addr << 1);
1192 INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
1195 init_waitqueue_head(&adv7511->wq);
1197 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
1198 adv7511_irq_handler,
1199 IRQF_ONESHOT, dev_name(dev),
1202 goto err_unregister_cec;
1205 adv7511_power_off(adv7511);
1207 i2c_set_clientdata(i2c, adv7511);
1209 if (adv7511->type == ADV7511)
1210 adv7511_set_link_config(adv7511, &link_config);
1212 ret = adv7511_cec_init(dev, adv7511);
1214 goto err_unregister_cec;
1216 adv7511->bridge.funcs = &adv7511_bridge_funcs;
1217 adv7511->bridge.of_node = dev->of_node;
1219 drm_bridge_add(&adv7511->bridge);
1221 adv7511_audio_init(dev, adv7511);
1223 if (adv7511->type == ADV7533) {
1224 ret = adv7533_attach_dsi(adv7511);
1226 goto err_remove_bridge;
1232 drm_bridge_remove(&adv7511->bridge);
1234 i2c_unregister_device(adv7511->i2c_cec);
1235 if (adv7511->cec_clk)
1236 clk_disable_unprepare(adv7511->cec_clk);
1237 err_i2c_unregister_packet:
1238 i2c_unregister_device(adv7511->i2c_packet);
1239 err_i2c_unregister_edid:
1240 i2c_unregister_device(adv7511->i2c_edid);
1242 adv7511_uninit_regulators(adv7511);
1247 static int adv7511_remove(struct i2c_client *i2c)
1249 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1251 if (adv7511->type == ADV7533)
1252 adv7533_detach_dsi(adv7511);
1253 i2c_unregister_device(adv7511->i2c_cec);
1254 if (adv7511->cec_clk)
1255 clk_disable_unprepare(adv7511->cec_clk);
1257 adv7511_uninit_regulators(adv7511);
1259 drm_bridge_remove(&adv7511->bridge);
1261 adv7511_audio_exit(adv7511);
1263 cec_unregister_adapter(adv7511->cec_adap);
1265 i2c_unregister_device(adv7511->i2c_packet);
1266 i2c_unregister_device(adv7511->i2c_edid);
1271 static const struct i2c_device_id adv7511_i2c_ids[] = {
1272 { "adv7511", ADV7511 },
1273 { "adv7511w", ADV7511 },
1274 { "adv7513", ADV7511 },
1275 #ifdef CONFIG_DRM_I2C_ADV7533
1276 { "adv7533", ADV7533 },
1280 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1282 static const struct of_device_id adv7511_of_ids[] = {
1283 { .compatible = "adi,adv7511", .data = (void *)ADV7511 },
1284 { .compatible = "adi,adv7511w", .data = (void *)ADV7511 },
1285 { .compatible = "adi,adv7513", .data = (void *)ADV7511 },
1286 #ifdef CONFIG_DRM_I2C_ADV7533
1287 { .compatible = "adi,adv7533", .data = (void *)ADV7533 },
1291 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1293 static struct mipi_dsi_driver adv7533_dsi_driver = {
1294 .driver.name = "adv7533",
1297 static struct i2c_driver adv7511_driver = {
1300 .of_match_table = adv7511_of_ids,
1302 .id_table = adv7511_i2c_ids,
1303 .probe = adv7511_probe,
1304 .remove = adv7511_remove,
1307 static int __init adv7511_init(void)
1309 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1310 mipi_dsi_driver_register(&adv7533_dsi_driver);
1312 return i2c_add_driver(&adv7511_driver);
1314 module_init(adv7511_init);
1316 static void __exit adv7511_exit(void)
1318 i2c_del_driver(&adv7511_driver);
1320 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1321 mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1323 module_exit(adv7511_exit);
1325 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1326 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1327 MODULE_LICENSE("GPL");