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>
15 #include <linux/of_graph.h>
18 #include <drm/drm_crtc_helper.h>
19 #include <drm/drm_edid.h>
20 #include <drm/drm_encoder_slave.h>
21 #include <drm/drm_atomic.h>
22 #include <drm/drm_atomic_helper.h>
23 #include <drm/drm_mipi_dsi.h>
33 struct i2c_client *i2c_main;
34 struct i2c_client *i2c_edid;
35 struct i2c_client *i2c_cec;
37 struct regmap *regmap;
38 struct regmap *regmap_cec;
39 enum drm_connector_status status;
42 struct drm_display_mode curr_mode;
46 unsigned int current_edid_segment;
47 uint8_t edid_buf[256];
51 struct drm_encoder *encoder;
53 struct drm_connector connector;
54 struct drm_bridge bridge;
57 enum adv7511_sync_polarity vsync_polarity;
58 enum adv7511_sync_polarity hsync_polarity;
63 struct gpio_desc *gpio_pd;
65 /* ADV7533 DSI RX related params */
66 struct device_node *host_node;
67 struct mipi_dsi_device *dsi;
70 enum adv7511_type type;
73 static struct adv7511 *encoder_to_adv7511(struct drm_encoder *encoder)
75 return to_encoder_slave(encoder)->slave_priv;
78 /* ADI recommended values for proper operation. */
79 static const struct reg_sequence adv7511_fixed_registers[] = {
91 /* ADI recommended values for proper operation. */
92 static const struct reg_default adv7533_fixed_registers[] = {
101 static const struct reg_default adv7533_cec_fixed_registers[] = {
108 /* -----------------------------------------------------------------------------
112 static const uint8_t adv7511_register_defaults[] = {
113 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
114 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
115 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
116 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
117 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
118 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
119 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
120 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
121 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
122 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
123 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
124 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
126 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
130 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
132 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
133 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
134 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
136 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
137 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
138 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
139 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
140 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
141 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
142 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
144 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147 static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
150 case ADV7511_REG_CHIP_REVISION:
151 case ADV7511_REG_SPDIF_FREQ:
152 case ADV7511_REG_CTS_AUTOMATIC1:
153 case ADV7511_REG_CTS_AUTOMATIC2:
154 case ADV7511_REG_VIC_DETECTED:
155 case ADV7511_REG_VIC_SEND:
156 case ADV7511_REG_AUX_VIC_DETECTED:
157 case ADV7511_REG_STATUS:
158 case ADV7511_REG_GC(1):
159 case ADV7511_REG_INT(0):
160 case ADV7511_REG_INT(1):
161 case ADV7511_REG_PLL_STATUS:
162 case ADV7511_REG_AN(0):
163 case ADV7511_REG_AN(1):
164 case ADV7511_REG_AN(2):
165 case ADV7511_REG_AN(3):
166 case ADV7511_REG_AN(4):
167 case ADV7511_REG_AN(5):
168 case ADV7511_REG_AN(6):
169 case ADV7511_REG_AN(7):
170 case ADV7511_REG_HDCP_STATUS:
171 case ADV7511_REG_BCAPS:
172 case ADV7511_REG_BKSV(0):
173 case ADV7511_REG_BKSV(1):
174 case ADV7511_REG_BKSV(2):
175 case ADV7511_REG_BKSV(3):
176 case ADV7511_REG_BKSV(4):
177 case ADV7511_REG_DDC_STATUS:
178 case ADV7511_REG_BSTATUS(0):
179 case ADV7511_REG_BSTATUS(1):
180 case ADV7511_REG_CHIP_ID_HIGH:
181 case ADV7511_REG_CHIP_ID_LOW:
188 static const struct regmap_config adv7511_regmap_config = {
192 .max_register = 0xff,
193 .cache_type = REGCACHE_RBTREE,
194 .reg_defaults_raw = adv7511_register_defaults,
195 .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
197 .volatile_reg = adv7511_register_volatile,
200 static const struct regmap_config adv7533_cec_regmap_config = {
204 .max_register = 0xff,
205 .cache_type = REGCACHE_RBTREE,
209 /* -----------------------------------------------------------------------------
210 * Hardware configuration
213 static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
214 const uint16_t *coeff,
215 unsigned int scaling_factor)
219 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
220 ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
223 for (i = 0; i < 12; ++i) {
224 regmap_update_bits(adv7511->regmap,
225 ADV7511_REG_CSC_UPPER(i),
226 0x1f, coeff[i] >> 8);
227 regmap_write(adv7511->regmap,
228 ADV7511_REG_CSC_LOWER(i),
234 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
235 0xe0, 0x80 | (scaling_factor << 5));
237 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
240 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
241 ADV7511_CSC_UPDATE_MODE, 0);
244 static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
247 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
250 if (packet & 0xff00) {
252 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
259 static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
262 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
265 if (packet & 0xff00) {
267 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
274 /* Coefficients for adv7511 color space conversion */
275 static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
276 0x0734, 0x04ad, 0x0000, 0x1c1b,
277 0x1ddc, 0x04ad, 0x1f24, 0x0135,
278 0x0000, 0x04ad, 0x087c, 0x1b77,
281 static void adv7511_set_config_csc(struct adv7511 *adv7511,
282 struct drm_connector *connector,
285 struct adv7511_video_config config;
286 bool output_format_422, output_format_ycbcr;
288 uint8_t infoframe[17];
291 config.hdmi_mode = drm_detect_hdmi_monitor(adv7511->edid);
293 config.hdmi_mode = false;
295 hdmi_avi_infoframe_init(&config.avi_infoframe);
297 config.avi_infoframe.scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
300 config.csc_enable = false;
301 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
303 config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
304 config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
306 if ((connector->display_info.color_formats &
307 DRM_COLOR_FORMAT_YCRCB422) &&
309 config.csc_enable = false;
310 config.avi_infoframe.colorspace =
311 HDMI_COLORSPACE_YUV422;
313 config.csc_enable = true;
314 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
318 if (config.hdmi_mode) {
319 mode = ADV7511_HDMI_CFG_MODE_HDMI;
321 switch (config.avi_infoframe.colorspace) {
322 case HDMI_COLORSPACE_YUV444:
323 output_format_422 = false;
324 output_format_ycbcr = true;
326 case HDMI_COLORSPACE_YUV422:
327 output_format_422 = true;
328 output_format_ycbcr = true;
331 output_format_422 = false;
332 output_format_ycbcr = false;
336 mode = ADV7511_HDMI_CFG_MODE_DVI;
337 output_format_422 = false;
338 output_format_ycbcr = false;
341 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
343 adv7511_set_colormap(adv7511, config.csc_enable,
344 config.csc_coefficents,
345 config.csc_scaling_factor);
347 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
348 (output_format_422 << 7) | output_format_ycbcr);
350 regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
351 ADV7511_HDMI_CFG_MODE_MASK, mode);
353 hdmi_avi_infoframe_pack(&config.avi_infoframe, infoframe,
356 /* The AVI infoframe id is not configurable */
357 regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
358 infoframe + 1, sizeof(infoframe) - 1);
360 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
363 static void adv7511_set_link_config(struct adv7511 *adv7511,
364 const struct adv7511_link_config *config)
367 * The input style values documented in the datasheet don't match the
368 * hardware register field values :-(
370 static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
372 unsigned int clock_delay;
373 unsigned int color_depth;
374 unsigned int input_id;
376 clock_delay = (config->clock_delay + 1200) / 400;
377 color_depth = config->input_color_depth == 8 ? 3
378 : (config->input_color_depth == 10 ? 1 : 2);
380 /* TODO Support input ID 6 */
381 if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
382 input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
384 else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
385 input_id = config->embedded_sync ? 8 : 7;
386 else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
387 input_id = config->embedded_sync ? 4 : 3;
389 input_id = config->embedded_sync ? 2 : 1;
391 regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
393 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
395 (input_styles[config->input_style] << 2));
396 regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
397 config->input_justification << 3);
398 regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
399 config->sync_pulse << 2);
401 regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
403 adv7511->embedded_sync = config->embedded_sync;
404 adv7511->hsync_polarity = config->hsync_polarity;
405 adv7511->vsync_polarity = config->vsync_polarity;
406 adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
409 static void adv7511_dsi_config_tgen(struct adv7511 *adv7511)
411 struct mipi_dsi_device *dsi = adv7511->dsi;
412 struct drm_display_mode *mode = &adv7511->curr_mode;
413 u8 clock_div_by_lanes[] = { 6, 4, 3 }; /* 2, 3, 4 lanes */
414 unsigned int hsw, hfp, hbp, vsw, vfp, vbp;
416 hsw = mode->hsync_end - mode->hsync_start;
417 hfp = mode->hsync_start - mode->hdisplay;
418 hbp = mode->htotal - mode->hsync_end;
419 vsw = mode->vsync_end - mode->vsync_start;
420 vfp = mode->vsync_start - mode->vdisplay;
421 vbp = mode->vtotal - mode->vsync_end;
423 /* set pixel clock divider mode */
424 regmap_write(adv7511->regmap_cec, 0x16,
425 clock_div_by_lanes[dsi->lanes - 2] << 3);
427 /* horizontal porch params */
428 regmap_write(adv7511->regmap_cec, 0x28, mode->htotal >> 4);
429 regmap_write(adv7511->regmap_cec, 0x29, (mode->htotal << 4) & 0xff);
430 regmap_write(adv7511->regmap_cec, 0x2a, hsw >> 4);
431 regmap_write(adv7511->regmap_cec, 0x2b, (hsw << 4) & 0xff);
432 regmap_write(adv7511->regmap_cec, 0x2c, hfp >> 4);
433 regmap_write(adv7511->regmap_cec, 0x2d, (hfp << 4) & 0xff);
434 regmap_write(adv7511->regmap_cec, 0x2e, hbp >> 4);
435 regmap_write(adv7511->regmap_cec, 0x2f, (hbp << 4) & 0xff);
437 /* vertical porch params */
438 regmap_write(adv7511->regmap_cec, 0x30, mode->vtotal >> 4);
439 regmap_write(adv7511->regmap_cec, 0x31, (mode->vtotal << 4) & 0xff);
440 regmap_write(adv7511->regmap_cec, 0x32, vsw >> 4);
441 regmap_write(adv7511->regmap_cec, 0x33, (vsw << 4) & 0xff);
442 regmap_write(adv7511->regmap_cec, 0x34, vfp >> 4);
443 regmap_write(adv7511->regmap_cec, 0x35, (vfp << 4) & 0xff);
444 regmap_write(adv7511->regmap_cec, 0x36, vbp >> 4);
445 regmap_write(adv7511->regmap_cec, 0x37, (vbp << 4) & 0xff);
448 static void adv7511_dsi_receiver_dpms(struct adv7511 *adv7511)
450 if (adv7511->type != ADV7533)
453 if (adv7511->powered) {
454 struct mipi_dsi_device *dsi = adv7511->dsi;
456 adv7511_dsi_config_tgen(adv7511);
458 /* set number of dsi lanes */
459 regmap_write(adv7511->regmap_cec, 0x1c, dsi->lanes << 4);
461 /* reset internal timing generator */
462 regmap_write(adv7511->regmap_cec, 0x27, 0xcb);
463 regmap_write(adv7511->regmap_cec, 0x27, 0x8b);
464 regmap_write(adv7511->regmap_cec, 0x27, 0xcb);
467 regmap_write(adv7511->regmap_cec, 0x03, 0x89);
468 /* disable test mode */
469 regmap_write(adv7511->regmap_cec, 0x55, 0x00);
471 regmap_write(adv7511->regmap_cec, 0x03, 0x0b);
472 regmap_write(adv7511->regmap_cec, 0x27, 0x0b);
476 static void adv7511_power_on(struct adv7511 *adv7511)
478 adv7511->current_edid_segment = -1;
480 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
481 ADV7511_INT0_EDID_READY);
482 regmap_write(adv7511->regmap, ADV7511_REG_INT(1),
483 ADV7511_INT1_DDC_ERROR);
484 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
485 ADV7511_POWER_POWER_DOWN, 0);
488 * Per spec it is allowed to pulse the HDP signal to indicate that the
489 * EDID information has changed. Some monitors do this when they wakeup
490 * from standby or are enabled. When the HDP goes low the adv7511 is
491 * reset and the outputs are disabled which might cause the monitor to
492 * go to standby again. To avoid this we ignore the HDP pin for the
493 * first few seconds after enabling the output.
495 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
496 ADV7511_REG_POWER2_HDP_SRC_MASK,
497 ADV7511_REG_POWER2_HDP_SRC_NONE);
500 * Most of the registers are reset during power down or when HPD is low.
502 regcache_sync(adv7511->regmap);
504 if (adv7511->type == ADV7533)
505 regmap_register_patch(adv7511->regmap_cec,
506 adv7533_cec_fixed_registers,
507 ARRAY_SIZE(adv7533_cec_fixed_registers));
508 adv7511->powered = true;
510 adv7511_dsi_receiver_dpms(adv7511);
513 static void adv7511_power_off(struct adv7511 *adv7511)
515 /* TODO: setup additional power down modes */
516 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
517 ADV7511_POWER_POWER_DOWN,
518 ADV7511_POWER_POWER_DOWN);
519 regcache_mark_dirty(adv7511->regmap);
521 adv7511->powered = false;
523 adv7511_dsi_receiver_dpms(adv7511);
526 /* -----------------------------------------------------------------------------
527 * Interrupt and hotplug detection
530 static bool adv7511_hpd(struct adv7511 *adv7511)
535 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
539 if (irq0 & ADV7511_INT0_HDP) {
540 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
548 static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
550 unsigned int irq0, irq1;
553 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
557 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
561 regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
562 regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
564 if (process_hpd && irq0 & ADV7511_INT0_HDP && adv7511->encoder)
565 drm_helper_hpd_irq_event(adv7511->encoder->dev);
567 if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
568 adv7511->edid_read = true;
570 if (adv7511->i2c_main->irq)
571 wake_up_all(&adv7511->wq);
577 static irqreturn_t adv7511_irq_handler(int irq, void *devid)
579 struct adv7511 *adv7511 = devid;
582 ret = adv7511_irq_process(adv7511, true);
583 return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
586 /* -----------------------------------------------------------------------------
590 static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
594 if (adv7511->i2c_main->irq) {
595 ret = wait_event_interruptible_timeout(adv7511->wq,
596 adv7511->edid_read, msecs_to_jiffies(timeout));
598 for (; timeout > 0; timeout -= 25) {
599 ret = adv7511_irq_process(adv7511, false);
603 if (adv7511->edid_read)
610 return adv7511->edid_read ? 0 : -EIO;
613 static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
616 struct adv7511 *adv7511 = data;
617 struct i2c_msg xfer[2];
625 if (adv7511->current_edid_segment != block / 2) {
628 ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
634 adv7511->edid_read = false;
635 regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
637 ret = adv7511_wait_for_edid(adv7511, 200);
642 /* Break this apart, hopefully more I2C controllers will
643 * support 64 byte transfers than 256 byte transfers
646 xfer[0].addr = adv7511->i2c_edid->addr;
649 xfer[0].buf = &offset;
650 xfer[1].addr = adv7511->i2c_edid->addr;
651 xfer[1].flags = I2C_M_RD;
653 xfer[1].buf = adv7511->edid_buf;
657 for (i = 0; i < 4; ++i) {
658 ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
669 adv7511->current_edid_segment = block / 2;
673 memcpy(buf, adv7511->edid_buf, len);
675 memcpy(buf, adv7511->edid_buf + 128, len);
680 /* -----------------------------------------------------------------------------
683 static int adv7511_get_modes(struct adv7511 *adv7511,
684 struct drm_connector *connector)
689 /* Reading the EDID only works if the device is powered */
690 if (!adv7511->powered) {
691 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
692 ADV7511_REG_POWER2_HDP_SRC_MASK,
693 ADV7511_REG_POWER2_HDP_SRC_NONE);
694 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
695 ADV7511_INT0_EDID_READY);
696 regmap_write(adv7511->regmap, ADV7511_REG_INT(1),
697 ADV7511_INT1_DDC_ERROR);
698 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
699 ADV7511_POWER_POWER_DOWN, 0);
700 adv7511->current_edid_segment = -1;
703 edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
705 if (!adv7511->powered)
706 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
707 ADV7511_POWER_POWER_DOWN,
708 ADV7511_POWER_POWER_DOWN);
710 kfree(adv7511->edid);
711 adv7511->edid = edid;
715 drm_mode_connector_update_edid_property(connector, edid);
716 count = drm_add_edid_modes(connector, edid);
718 adv7511_set_config_csc(adv7511, connector, adv7511->rgb);
723 static enum drm_connector_status
724 adv7511_detect(struct adv7511 *adv7511,
725 struct drm_connector *connector)
727 enum drm_connector_status status;
732 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
734 return connector_status_disconnected;
736 if (val & ADV7511_STATUS_HPD)
737 status = connector_status_connected;
739 status = connector_status_disconnected;
741 hpd = adv7511_hpd(adv7511);
743 /* The chip resets itself when the cable is disconnected, so in case
744 * there is a pending HPD interrupt and the cable is connected there was
745 * at least one transition from disconnected to connected and the chip
746 * has to be reinitialized. */
747 if (status == connector_status_connected && hpd && adv7511->powered) {
748 regcache_mark_dirty(adv7511->regmap);
749 adv7511_power_on(adv7511);
750 adv7511_get_modes(adv7511, connector);
751 if (adv7511->status == connector_status_connected)
752 status = connector_status_disconnected;
754 /* Renable HDP sensing */
755 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
756 ADV7511_REG_POWER2_HDP_SRC_MASK,
757 ADV7511_REG_POWER2_HDP_SRC_BOTH);
760 adv7511->status = status;
764 static int adv7511_mode_valid(struct adv7511 *adv7511,
765 const struct drm_display_mode *mode)
767 if (mode->clock > 165000)
768 return MODE_CLOCK_HIGH;
773 static void adv7511_mode_set(struct adv7511 *adv7511,
774 struct drm_display_mode *mode,
775 struct drm_display_mode *adj_mode)
777 unsigned int low_refresh_rate;
778 unsigned int hsync_polarity = 0;
779 unsigned int vsync_polarity = 0;
781 if (adv7511->embedded_sync) {
782 unsigned int hsync_offset, hsync_len;
783 unsigned int vsync_offset, vsync_len;
785 hsync_offset = adj_mode->crtc_hsync_start -
786 adj_mode->crtc_hdisplay;
787 vsync_offset = adj_mode->crtc_vsync_start -
788 adj_mode->crtc_vdisplay;
789 hsync_len = adj_mode->crtc_hsync_end -
790 adj_mode->crtc_hsync_start;
791 vsync_len = adj_mode->crtc_vsync_end -
792 adj_mode->crtc_vsync_start;
794 /* The hardware vsync generator has a off-by-one bug */
797 regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
798 ((hsync_offset >> 10) & 0x7) << 5);
799 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
800 (hsync_offset >> 2) & 0xff);
801 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
802 ((hsync_offset & 0x3) << 6) |
803 ((hsync_len >> 4) & 0x3f));
804 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
805 ((hsync_len & 0xf) << 4) |
806 ((vsync_offset >> 6) & 0xf));
807 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
808 ((vsync_offset & 0x3f) << 2) |
809 ((vsync_len >> 8) & 0x3));
810 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
813 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
814 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
816 enum adv7511_sync_polarity mode_hsync_polarity;
817 enum adv7511_sync_polarity mode_vsync_polarity;
820 * If the input signal is always low or always high we want to
821 * invert or let it passthrough depending on the polarity of the
824 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
825 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
827 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
829 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
830 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
832 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
834 if (adv7511->hsync_polarity != mode_hsync_polarity &&
835 adv7511->hsync_polarity !=
836 ADV7511_SYNC_POLARITY_PASSTHROUGH)
839 if (adv7511->vsync_polarity != mode_vsync_polarity &&
840 adv7511->vsync_polarity !=
841 ADV7511_SYNC_POLARITY_PASSTHROUGH)
845 if (mode->vrefresh <= 24000)
846 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
847 else if (mode->vrefresh <= 25000)
848 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
849 else if (mode->vrefresh <= 30000)
850 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
852 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
854 regmap_update_bits(adv7511->regmap, 0xfb,
855 0x6, low_refresh_rate << 1);
856 regmap_update_bits(adv7511->regmap, 0x17,
857 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
859 drm_mode_copy(&adv7511->curr_mode, adj_mode);
862 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
863 * supposed to give better results.
866 adv7511->f_tmds = mode->clock;
869 /* -----------------------------------------------------------------------------
873 static int adv7511_encoder_get_modes(struct drm_encoder *encoder,
874 struct drm_connector *connector)
876 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
878 return adv7511_get_modes(adv7511, connector);
881 static void adv7511_encoder_dpms(struct drm_encoder *encoder, int mode)
883 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
885 if (mode == DRM_MODE_DPMS_ON)
886 adv7511_power_on(adv7511);
888 adv7511_power_off(adv7511);
891 static enum drm_connector_status
892 adv7511_encoder_detect(struct drm_encoder *encoder,
893 struct drm_connector *connector)
895 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
897 return adv7511_detect(adv7511, connector);
900 static int adv7511_encoder_mode_valid(struct drm_encoder *encoder,
901 struct drm_display_mode *mode)
903 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
905 return adv7511_mode_valid(adv7511, mode);
908 static void adv7511_encoder_mode_set(struct drm_encoder *encoder,
909 struct drm_display_mode *mode,
910 struct drm_display_mode *adj_mode)
912 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
914 adv7511_mode_set(adv7511, mode, adj_mode);
917 static struct drm_encoder_slave_funcs adv7511_encoder_funcs = {
918 .dpms = adv7511_encoder_dpms,
919 .mode_valid = adv7511_encoder_mode_valid,
920 .mode_set = adv7511_encoder_mode_set,
921 .detect = adv7511_encoder_detect,
922 .get_modes = adv7511_encoder_get_modes,
925 /* -----------------------------------------------------------------------------
926 * Bridge and connector functions
929 static struct adv7511 *connector_to_adv7511(struct drm_connector *connector)
931 return container_of(connector, struct adv7511, connector);
934 /* Connector helper functions */
935 static int adv7533_connector_get_modes(struct drm_connector *connector)
937 struct adv7511 *adv = connector_to_adv7511(connector);
939 return adv7511_get_modes(adv, connector);
942 static struct drm_encoder *
943 adv7533_connector_best_encoder(struct drm_connector *connector)
945 struct adv7511 *adv = connector_to_adv7511(connector);
947 return adv->bridge.encoder;
950 static enum drm_mode_status
951 adv7533_connector_mode_valid(struct drm_connector *connector,
952 struct drm_display_mode *mode)
954 struct adv7511 *adv = connector_to_adv7511(connector);
956 return adv7511_mode_valid(adv, mode);
959 static struct drm_connector_helper_funcs adv7533_connector_helper_funcs = {
960 .get_modes = adv7533_connector_get_modes,
961 .best_encoder = adv7533_connector_best_encoder,
962 .mode_valid = adv7533_connector_mode_valid,
965 static enum drm_connector_status
966 adv7533_connector_detect(struct drm_connector *connector, bool force)
968 struct adv7511 *adv = connector_to_adv7511(connector);
970 return adv7511_detect(adv, connector);
973 static struct drm_connector_funcs adv7533_connector_funcs = {
974 .dpms = drm_atomic_helper_connector_dpms,
975 .fill_modes = drm_helper_probe_single_connector_modes,
976 .detect = adv7533_connector_detect,
977 .destroy = drm_connector_cleanup,
978 .reset = drm_atomic_helper_connector_reset,
979 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
980 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
984 static struct adv7511 *bridge_to_adv7511(struct drm_bridge *bridge)
986 return container_of(bridge, struct adv7511, bridge);
989 static void adv7533_bridge_pre_enable(struct drm_bridge *bridge)
991 struct adv7511 *adv = bridge_to_adv7511(bridge);
993 adv7511_power_on(adv);
996 static void adv7533_bridge_post_disable(struct drm_bridge *bridge)
998 struct adv7511 *adv = bridge_to_adv7511(bridge);
1000 adv7511_power_off(adv);
1003 static void adv7533_bridge_enable(struct drm_bridge *bridge)
1007 static void adv7533_bridge_disable(struct drm_bridge *bridge)
1011 static void adv7533_bridge_mode_set(struct drm_bridge *bridge,
1012 struct drm_display_mode *mode,
1013 struct drm_display_mode *adj_mode)
1015 struct adv7511 *adv = bridge_to_adv7511(bridge);
1017 adv7511_mode_set(adv, mode, adj_mode);
1020 static int adv7533_attach_dsi(struct adv7511 *adv7511)
1022 struct device *dev = &adv7511->i2c_main->dev;
1023 struct mipi_dsi_device *dsi;
1024 struct mipi_dsi_host *host;
1027 host = of_find_mipi_dsi_host_by_node(adv7511->host_node);
1029 dev_err(dev, "failed to find dsi host\n");
1030 return -EPROBE_DEFER;
1033 /* can adv7533 virtual channel be non-zero? */
1034 dsi = mipi_dsi_new_dummy(host, 0);
1036 dev_err(dev, "failed to create dummy dsi device\n");
1038 goto err_dsi_device;
1043 dsi->lanes = adv7511->num_dsi_lanes;
1044 dsi->format = MIPI_DSI_FMT_RGB888;
1045 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE
1046 | MIPI_DSI_MODE_EOT_PACKET | MIPI_DSI_MODE_VIDEO_HSE;
1048 ret = mipi_dsi_attach(dsi);
1050 dev_err(dev, "failed to attach dsi to host\n");
1051 goto err_dsi_attach;
1057 mipi_dsi_unregister_device(dsi);
1062 static int adv7533_bridge_attach(struct drm_bridge *bridge)
1064 struct adv7511 *adv = bridge_to_adv7511(bridge);
1067 adv->encoder = bridge->encoder;
1069 if (!bridge->encoder) {
1070 DRM_ERROR("Parent encoder object not found");
1074 adv->connector.polled = DRM_CONNECTOR_POLL_HPD;
1075 ret = drm_connector_init(bridge->dev, &adv->connector,
1076 &adv7533_connector_funcs, DRM_MODE_CONNECTOR_HDMIA);
1078 DRM_ERROR("Failed to initialize connector with drm\n");
1081 drm_connector_helper_add(&adv->connector,
1082 &adv7533_connector_helper_funcs);
1083 drm_connector_register(&adv->connector);
1084 drm_mode_connector_attach_encoder(&adv->connector, adv->encoder);
1086 drm_helper_hpd_irq_event(adv->connector.dev);
1088 adv7533_attach_dsi(adv);
1093 static struct drm_bridge_funcs adv7533_bridge_funcs = {
1094 .pre_enable = adv7533_bridge_pre_enable,
1095 .enable = adv7533_bridge_enable,
1096 .disable = adv7533_bridge_disable,
1097 .post_disable = adv7533_bridge_post_disable,
1098 .mode_set = adv7533_bridge_mode_set,
1099 .attach = adv7533_bridge_attach,
1102 /* -----------------------------------------------------------------------------
1106 static int adv7511_parse_dt(struct device_node *np,
1107 struct adv7511_link_config *config)
1112 of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
1113 if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
1114 config->input_color_depth != 12)
1117 ret = of_property_read_string(np, "adi,input-colorspace", &str);
1121 if (!strcmp(str, "rgb"))
1122 config->input_colorspace = HDMI_COLORSPACE_RGB;
1123 else if (!strcmp(str, "yuv422"))
1124 config->input_colorspace = HDMI_COLORSPACE_YUV422;
1125 else if (!strcmp(str, "yuv444"))
1126 config->input_colorspace = HDMI_COLORSPACE_YUV444;
1130 ret = of_property_read_string(np, "adi,input-clock", &str);
1134 if (!strcmp(str, "1x"))
1135 config->input_clock = ADV7511_INPUT_CLOCK_1X;
1136 else if (!strcmp(str, "2x"))
1137 config->input_clock = ADV7511_INPUT_CLOCK_2X;
1138 else if (!strcmp(str, "ddr"))
1139 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
1143 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
1144 config->input_clock != ADV7511_INPUT_CLOCK_1X) {
1145 ret = of_property_read_u32(np, "adi,input-style",
1146 &config->input_style);
1150 if (config->input_style < 1 || config->input_style > 3)
1153 ret = of_property_read_string(np, "adi,input-justification",
1158 if (!strcmp(str, "left"))
1159 config->input_justification =
1160 ADV7511_INPUT_JUSTIFICATION_LEFT;
1161 else if (!strcmp(str, "evenly"))
1162 config->input_justification =
1163 ADV7511_INPUT_JUSTIFICATION_EVENLY;
1164 else if (!strcmp(str, "right"))
1165 config->input_justification =
1166 ADV7511_INPUT_JUSTIFICATION_RIGHT;
1171 config->input_style = 1;
1172 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
1175 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
1176 if (config->clock_delay < -1200 || config->clock_delay > 1600)
1179 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
1181 /* Hardcode the sync pulse configurations for now. */
1182 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
1183 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1184 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1189 static int adv7533_parse_dt(struct device_node *np, struct adv7511 *adv7511)
1192 struct device_node *endpoint;
1194 of_property_read_u32(np, "adi,dsi-lanes", &num_lanes);
1196 if (num_lanes < 1 || num_lanes > 4)
1199 adv7511->num_dsi_lanes = num_lanes;
1201 endpoint = of_graph_get_next_endpoint(np, NULL);
1203 DRM_ERROR("adv dsi input endpoint not found\n");
1207 adv7511->host_node = of_graph_get_remote_port_parent(endpoint);
1208 if (!adv7511->host_node) {
1209 DRM_ERROR("dsi host node not found\n");
1210 of_node_put(endpoint);
1214 of_node_put(endpoint);
1215 of_node_put(adv7511->host_node);
1217 /* TODO: Check if these need to be parsed by DT or not */
1218 adv7511->rgb = true;
1219 adv7511->embedded_sync = false;
1224 static const int edid_i2c_addr = 0x7e;
1225 static const int packet_i2c_addr = 0x70;
1226 static const int cec_i2c_addr = 0x78;
1228 static const struct of_device_id adv7511_of_ids[] = {
1229 { .compatible = "adi,adv7511", .data = (void *) ADV7511 },
1230 { .compatible = "adi,adv7511w", .data = (void *) ADV7511 },
1231 { .compatible = "adi,adv7513", .data = (void *) ADV7511 },
1232 { .compatible = "adi,adv7533", .data = (void *) ADV7533 },
1235 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1237 static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
1239 struct adv7511_link_config link_config;
1240 struct adv7511 *adv7511;
1241 struct device *dev = &i2c->dev;
1248 adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
1252 adv7511->powered = false;
1253 adv7511->status = connector_status_disconnected;
1256 const struct of_device_id *of_id;
1258 of_id = of_match_node(adv7511_of_ids, dev->of_node);
1259 adv7511->type = (enum adv7511_type) of_id->data;
1261 adv7511->type = id->driver_data;
1264 memset(&link_config, 0, sizeof(link_config));
1266 if (adv7511->type == ADV7511)
1267 ret = adv7511_parse_dt(dev->of_node, &link_config);
1269 ret = adv7533_parse_dt(dev->of_node, adv7511);
1274 * The power down GPIO is optional. If present, toggle it from active to
1275 * inactive to wake up the encoder.
1277 adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
1278 if (IS_ERR(adv7511->gpio_pd))
1279 return PTR_ERR(adv7511->gpio_pd);
1281 if (adv7511->gpio_pd) {
1283 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
1286 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
1287 if (IS_ERR(adv7511->regmap))
1288 return PTR_ERR(adv7511->regmap);
1290 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
1293 dev_dbg(dev, "Rev. %d\n", val);
1295 if (adv7511->type == ADV7511) {
1296 ret = regmap_register_patch(adv7511->regmap,
1297 adv7511_fixed_registers,
1298 ARRAY_SIZE(adv7511_fixed_registers));
1302 ret = regmap_register_patch(adv7511->regmap,
1303 adv7533_fixed_registers,
1304 ARRAY_SIZE(adv7533_fixed_registers));
1309 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
1310 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
1312 regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR, cec_i2c_addr);
1313 adv7511_packet_disable(adv7511, 0xffff);
1315 adv7511->i2c_main = i2c;
1316 adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
1317 if (!adv7511->i2c_edid)
1320 adv7511->i2c_cec = i2c_new_dummy(i2c->adapter, cec_i2c_addr >> 1);
1321 if (!adv7511->i2c_cec) {
1323 goto err_i2c_unregister_edid;
1326 adv7511->regmap_cec = devm_regmap_init_i2c(adv7511->i2c_cec,
1327 &adv7533_cec_regmap_config);
1328 if (IS_ERR(adv7511->regmap_cec)) {
1329 ret = PTR_ERR(adv7511->regmap_cec);
1330 goto err_i2c_unregister_cec;
1333 if (adv7511->type == ADV7533) {
1334 ret = regmap_register_patch(adv7511->regmap_cec,
1335 adv7533_cec_fixed_registers,
1336 ARRAY_SIZE(adv7533_cec_fixed_registers));
1342 init_waitqueue_head(&adv7511->wq);
1344 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
1345 adv7511_irq_handler,
1346 IRQF_ONESHOT, dev_name(dev),
1349 goto err_i2c_unregister_cec;
1352 /* CEC is unused for now */
1353 regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
1354 ADV7511_CEC_CTRL_POWER_DOWN);
1356 adv7511_power_off(adv7511);
1358 i2c_set_clientdata(i2c, adv7511);
1360 if (adv7511->type == ADV7511)
1361 adv7511_set_link_config(adv7511, &link_config);
1363 if (adv7511->type == ADV7533) {
1364 adv7511->bridge.funcs = &adv7533_bridge_funcs;
1365 adv7511->bridge.of_node = dev->of_node;
1367 ret = drm_bridge_add(&adv7511->bridge);
1369 dev_err(dev, "failed to add adv7533 bridge\n");
1370 goto err_i2c_unregister_cec;
1376 err_i2c_unregister_cec:
1377 i2c_unregister_device(adv7511->i2c_cec);
1378 err_i2c_unregister_edid:
1379 i2c_unregister_device(adv7511->i2c_edid);
1384 static int adv7511_remove(struct i2c_client *i2c)
1386 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1388 i2c_unregister_device(adv7511->i2c_cec);
1389 i2c_unregister_device(adv7511->i2c_edid);
1391 kfree(adv7511->edid);
1393 if (adv7511->type == ADV7533) {
1394 mipi_dsi_detach(adv7511->dsi);
1395 mipi_dsi_unregister_device(adv7511->dsi);
1396 drm_bridge_remove(&adv7511->bridge);
1402 static int adv7511_encoder_init(struct i2c_client *i2c, struct drm_device *dev,
1403 struct drm_encoder_slave *encoder)
1406 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1408 if (adv7511->type == ADV7533)
1411 encoder->slave_priv = adv7511;
1412 encoder->slave_funcs = &adv7511_encoder_funcs;
1414 adv7511->encoder = &encoder->base;
1419 static const struct i2c_device_id adv7511_i2c_ids[] = {
1420 { "adv7511", ADV7511 },
1421 { "adv7511w", ADV7511 },
1422 { "adv7513", ADV7511 },
1423 { "adv7533", ADV7533 },
1426 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1428 static struct drm_i2c_encoder_driver adv7511_driver = {
1432 .of_match_table = adv7511_of_ids,
1434 .id_table = adv7511_i2c_ids,
1435 .probe = adv7511_probe,
1436 .remove = adv7511_remove,
1439 .encoder_init = adv7511_encoder_init,
1442 static int __init adv7511_init(void)
1444 return drm_i2c_encoder_register(THIS_MODULE, &adv7511_driver);
1446 module_init(adv7511_init);
1448 static void __exit adv7511_exit(void)
1450 drm_i2c_encoder_unregister(&adv7511_driver);
1452 module_exit(adv7511_exit);
1454 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1455 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1456 MODULE_LICENSE("GPL");