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_atomic.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_crtc_helper.h>
20 #include <drm/drm_edid.h>
25 struct i2c_client
*i2c_main
;
26 struct i2c_client
*i2c_edid
;
28 struct regmap
*regmap
;
29 struct regmap
*packet_memory_regmap
;
30 enum drm_connector_status status
;
35 unsigned int current_edid_segment
;
36 uint8_t edid_buf
[256];
40 struct drm_bridge bridge
;
41 struct drm_connector connector
;
44 enum adv7511_sync_polarity vsync_polarity
;
45 enum adv7511_sync_polarity hsync_polarity
;
50 struct gpio_desc
*gpio_pd
;
53 /* ADI recommended values for proper operation. */
54 static const struct reg_sequence adv7511_fixed_registers
[] = {
66 /* -----------------------------------------------------------------------------
70 static const uint8_t adv7511_register_defaults
[] = {
71 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
72 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
73 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
74 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
75 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
76 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
78 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
79 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
80 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
88 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
90 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
91 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
92 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
94 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
96 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
97 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
98 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
99 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
102 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 static bool adv7511_register_volatile(struct device
*dev
, unsigned int reg
)
108 case ADV7511_REG_CHIP_REVISION
:
109 case ADV7511_REG_SPDIF_FREQ
:
110 case ADV7511_REG_CTS_AUTOMATIC1
:
111 case ADV7511_REG_CTS_AUTOMATIC2
:
112 case ADV7511_REG_VIC_DETECTED
:
113 case ADV7511_REG_VIC_SEND
:
114 case ADV7511_REG_AUX_VIC_DETECTED
:
115 case ADV7511_REG_STATUS
:
116 case ADV7511_REG_GC(1):
117 case ADV7511_REG_INT(0):
118 case ADV7511_REG_INT(1):
119 case ADV7511_REG_PLL_STATUS
:
120 case ADV7511_REG_AN(0):
121 case ADV7511_REG_AN(1):
122 case ADV7511_REG_AN(2):
123 case ADV7511_REG_AN(3):
124 case ADV7511_REG_AN(4):
125 case ADV7511_REG_AN(5):
126 case ADV7511_REG_AN(6):
127 case ADV7511_REG_AN(7):
128 case ADV7511_REG_HDCP_STATUS
:
129 case ADV7511_REG_BCAPS
:
130 case ADV7511_REG_BKSV(0):
131 case ADV7511_REG_BKSV(1):
132 case ADV7511_REG_BKSV(2):
133 case ADV7511_REG_BKSV(3):
134 case ADV7511_REG_BKSV(4):
135 case ADV7511_REG_DDC_STATUS
:
136 case ADV7511_REG_EDID_READ_CTRL
:
137 case ADV7511_REG_BSTATUS(0):
138 case ADV7511_REG_BSTATUS(1):
139 case ADV7511_REG_CHIP_ID_HIGH
:
140 case ADV7511_REG_CHIP_ID_LOW
:
147 static const struct regmap_config adv7511_regmap_config
= {
151 .max_register
= 0xff,
152 .cache_type
= REGCACHE_RBTREE
,
153 .reg_defaults_raw
= adv7511_register_defaults
,
154 .num_reg_defaults_raw
= ARRAY_SIZE(adv7511_register_defaults
),
156 .volatile_reg
= adv7511_register_volatile
,
159 /* -----------------------------------------------------------------------------
160 * Hardware configuration
163 static void adv7511_set_colormap(struct adv7511
*adv7511
, bool enable
,
164 const uint16_t *coeff
,
165 unsigned int scaling_factor
)
169 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_CSC_UPPER(1),
170 ADV7511_CSC_UPDATE_MODE
, ADV7511_CSC_UPDATE_MODE
);
173 for (i
= 0; i
< 12; ++i
) {
174 regmap_update_bits(adv7511
->regmap
,
175 ADV7511_REG_CSC_UPPER(i
),
176 0x1f, coeff
[i
] >> 8);
177 regmap_write(adv7511
->regmap
,
178 ADV7511_REG_CSC_LOWER(i
),
184 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_CSC_UPPER(0),
185 0xe0, 0x80 | (scaling_factor
<< 5));
187 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_CSC_UPPER(0),
190 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_CSC_UPPER(1),
191 ADV7511_CSC_UPDATE_MODE
, 0);
194 static int adv7511_packet_enable(struct adv7511
*adv7511
, unsigned int packet
)
197 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE0
,
200 if (packet
& 0xff00) {
202 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
209 static int adv7511_packet_disable(struct adv7511
*adv7511
, unsigned int packet
)
212 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE0
,
215 if (packet
& 0xff00) {
217 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
224 /* Coefficients for adv7511 color space conversion */
225 static const uint16_t adv7511_csc_ycbcr_to_rgb
[] = {
226 0x0734, 0x04ad, 0x0000, 0x1c1b,
227 0x1ddc, 0x04ad, 0x1f24, 0x0135,
228 0x0000, 0x04ad, 0x087c, 0x1b77,
231 static void adv7511_set_config_csc(struct adv7511
*adv7511
,
232 struct drm_connector
*connector
,
235 struct adv7511_video_config config
;
236 bool output_format_422
, output_format_ycbcr
;
238 uint8_t infoframe
[17];
241 config
.hdmi_mode
= drm_detect_hdmi_monitor(adv7511
->edid
);
243 config
.hdmi_mode
= false;
245 hdmi_avi_infoframe_init(&config
.avi_infoframe
);
247 config
.avi_infoframe
.scan_mode
= HDMI_SCAN_MODE_UNDERSCAN
;
250 config
.csc_enable
= false;
251 config
.avi_infoframe
.colorspace
= HDMI_COLORSPACE_RGB
;
253 config
.csc_scaling_factor
= ADV7511_CSC_SCALING_4
;
254 config
.csc_coefficents
= adv7511_csc_ycbcr_to_rgb
;
256 if ((connector
->display_info
.color_formats
&
257 DRM_COLOR_FORMAT_YCRCB422
) &&
259 config
.csc_enable
= false;
260 config
.avi_infoframe
.colorspace
=
261 HDMI_COLORSPACE_YUV422
;
263 config
.csc_enable
= true;
264 config
.avi_infoframe
.colorspace
= HDMI_COLORSPACE_RGB
;
268 if (config
.hdmi_mode
) {
269 mode
= ADV7511_HDMI_CFG_MODE_HDMI
;
271 switch (config
.avi_infoframe
.colorspace
) {
272 case HDMI_COLORSPACE_YUV444
:
273 output_format_422
= false;
274 output_format_ycbcr
= true;
276 case HDMI_COLORSPACE_YUV422
:
277 output_format_422
= true;
278 output_format_ycbcr
= true;
281 output_format_422
= false;
282 output_format_ycbcr
= false;
286 mode
= ADV7511_HDMI_CFG_MODE_DVI
;
287 output_format_422
= false;
288 output_format_ycbcr
= false;
291 adv7511_packet_disable(adv7511
, ADV7511_PACKET_ENABLE_AVI_INFOFRAME
);
293 adv7511_set_colormap(adv7511
, config
.csc_enable
,
294 config
.csc_coefficents
,
295 config
.csc_scaling_factor
);
297 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_VIDEO_INPUT_CFG1
, 0x81,
298 (output_format_422
<< 7) | output_format_ycbcr
);
300 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_HDCP_HDMI_CFG
,
301 ADV7511_HDMI_CFG_MODE_MASK
, mode
);
303 hdmi_avi_infoframe_pack(&config
.avi_infoframe
, infoframe
,
306 /* The AVI infoframe id is not configurable */
307 regmap_bulk_write(adv7511
->regmap
, ADV7511_REG_AVI_INFOFRAME_VERSION
,
308 infoframe
+ 1, sizeof(infoframe
) - 1);
310 adv7511_packet_enable(adv7511
, ADV7511_PACKET_ENABLE_AVI_INFOFRAME
);
313 static void adv7511_set_link_config(struct adv7511
*adv7511
,
314 const struct adv7511_link_config
*config
)
317 * The input style values documented in the datasheet don't match the
318 * hardware register field values :-(
320 static const unsigned int input_styles
[4] = { 0, 2, 1, 3 };
322 unsigned int clock_delay
;
323 unsigned int color_depth
;
324 unsigned int input_id
;
326 clock_delay
= (config
->clock_delay
+ 1200) / 400;
327 color_depth
= config
->input_color_depth
== 8 ? 3
328 : (config
->input_color_depth
== 10 ? 1 : 2);
330 /* TODO Support input ID 6 */
331 if (config
->input_colorspace
!= HDMI_COLORSPACE_YUV422
)
332 input_id
= config
->input_clock
== ADV7511_INPUT_CLOCK_DDR
334 else if (config
->input_clock
== ADV7511_INPUT_CLOCK_DDR
)
335 input_id
= config
->embedded_sync
? 8 : 7;
336 else if (config
->input_clock
== ADV7511_INPUT_CLOCK_2X
)
337 input_id
= config
->embedded_sync
? 4 : 3;
339 input_id
= config
->embedded_sync
? 2 : 1;
341 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_I2C_FREQ_ID_CFG
, 0xf,
343 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_VIDEO_INPUT_CFG1
, 0x7e,
345 (input_styles
[config
->input_style
] << 2));
346 regmap_write(adv7511
->regmap
, ADV7511_REG_VIDEO_INPUT_CFG2
,
347 config
->input_justification
<< 3);
348 regmap_write(adv7511
->regmap
, ADV7511_REG_TIMING_GEN_SEQ
,
349 config
->sync_pulse
<< 2);
351 regmap_write(adv7511
->regmap
, 0xba, clock_delay
<< 5);
353 adv7511
->embedded_sync
= config
->embedded_sync
;
354 adv7511
->hsync_polarity
= config
->hsync_polarity
;
355 adv7511
->vsync_polarity
= config
->vsync_polarity
;
356 adv7511
->rgb
= config
->input_colorspace
== HDMI_COLORSPACE_RGB
;
359 static void adv7511_power_on(struct adv7511
*adv7511
)
361 adv7511
->current_edid_segment
= -1;
363 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_POWER
,
364 ADV7511_POWER_POWER_DOWN
, 0);
365 if (adv7511
->i2c_main
->irq
) {
367 * Documentation says the INT_ENABLE registers are reset in
368 * POWER_DOWN mode. My 7511w preserved the bits, however.
369 * Still, let's be safe and stick to the documentation.
371 regmap_write(adv7511
->regmap
, ADV7511_REG_INT_ENABLE(0),
372 ADV7511_INT0_EDID_READY
);
373 regmap_write(adv7511
->regmap
, ADV7511_REG_INT_ENABLE(1),
374 ADV7511_INT1_DDC_ERROR
);
378 * Per spec it is allowed to pulse the HPD signal to indicate that the
379 * EDID information has changed. Some monitors do this when they wakeup
380 * from standby or are enabled. When the HPD goes low the adv7511 is
381 * reset and the outputs are disabled which might cause the monitor to
382 * go to standby again. To avoid this we ignore the HPD pin for the
383 * first few seconds after enabling the output.
385 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_POWER2
,
386 ADV7511_REG_POWER2_HPD_SRC_MASK
,
387 ADV7511_REG_POWER2_HPD_SRC_NONE
);
390 * Most of the registers are reset during power down or when HPD is low.
392 regcache_sync(adv7511
->regmap
);
394 adv7511
->powered
= true;
397 static void adv7511_power_off(struct adv7511
*adv7511
)
399 /* TODO: setup additional power down modes */
400 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_POWER
,
401 ADV7511_POWER_POWER_DOWN
,
402 ADV7511_POWER_POWER_DOWN
);
403 regcache_mark_dirty(adv7511
->regmap
);
405 adv7511
->powered
= false;
408 /* -----------------------------------------------------------------------------
409 * Interrupt and hotplug detection
412 static bool adv7511_hpd(struct adv7511
*adv7511
)
417 ret
= regmap_read(adv7511
->regmap
, ADV7511_REG_INT(0), &irq0
);
421 if (irq0
& ADV7511_INT0_HPD
) {
422 regmap_write(adv7511
->regmap
, ADV7511_REG_INT(0),
430 static int adv7511_irq_process(struct adv7511
*adv7511
, bool process_hpd
)
432 unsigned int irq0
, irq1
;
435 ret
= regmap_read(adv7511
->regmap
, ADV7511_REG_INT(0), &irq0
);
439 ret
= regmap_read(adv7511
->regmap
, ADV7511_REG_INT(1), &irq1
);
443 regmap_write(adv7511
->regmap
, ADV7511_REG_INT(0), irq0
);
444 regmap_write(adv7511
->regmap
, ADV7511_REG_INT(1), irq1
);
446 if (process_hpd
&& irq0
& ADV7511_INT0_HPD
&& adv7511
->bridge
.encoder
)
447 drm_helper_hpd_irq_event(adv7511
->connector
.dev
);
449 if (irq0
& ADV7511_INT0_EDID_READY
|| irq1
& ADV7511_INT1_DDC_ERROR
) {
450 adv7511
->edid_read
= true;
452 if (adv7511
->i2c_main
->irq
)
453 wake_up_all(&adv7511
->wq
);
459 static irqreturn_t
adv7511_irq_handler(int irq
, void *devid
)
461 struct adv7511
*adv7511
= devid
;
464 ret
= adv7511_irq_process(adv7511
, true);
465 return ret
< 0 ? IRQ_NONE
: IRQ_HANDLED
;
468 /* -----------------------------------------------------------------------------
472 static int adv7511_wait_for_edid(struct adv7511
*adv7511
, int timeout
)
476 if (adv7511
->i2c_main
->irq
) {
477 ret
= wait_event_interruptible_timeout(adv7511
->wq
,
478 adv7511
->edid_read
, msecs_to_jiffies(timeout
));
480 for (; timeout
> 0; timeout
-= 25) {
481 ret
= adv7511_irq_process(adv7511
, false);
485 if (adv7511
->edid_read
)
492 return adv7511
->edid_read
? 0 : -EIO
;
495 static int adv7511_get_edid_block(void *data
, u8
*buf
, unsigned int block
,
498 struct adv7511
*adv7511
= data
;
499 struct i2c_msg xfer
[2];
507 if (adv7511
->current_edid_segment
!= block
/ 2) {
510 ret
= regmap_read(adv7511
->regmap
, ADV7511_REG_DDC_STATUS
,
516 adv7511
->edid_read
= false;
517 regmap_write(adv7511
->regmap
, ADV7511_REG_EDID_SEGMENT
,
519 ret
= adv7511_wait_for_edid(adv7511
, 200);
524 /* Break this apart, hopefully more I2C controllers will
525 * support 64 byte transfers than 256 byte transfers
528 xfer
[0].addr
= adv7511
->i2c_edid
->addr
;
531 xfer
[0].buf
= &offset
;
532 xfer
[1].addr
= adv7511
->i2c_edid
->addr
;
533 xfer
[1].flags
= I2C_M_RD
;
535 xfer
[1].buf
= adv7511
->edid_buf
;
539 for (i
= 0; i
< 4; ++i
) {
540 ret
= i2c_transfer(adv7511
->i2c_edid
->adapter
, xfer
,
551 adv7511
->current_edid_segment
= block
/ 2;
555 memcpy(buf
, adv7511
->edid_buf
, len
);
557 memcpy(buf
, adv7511
->edid_buf
+ 128, len
);
562 /* -----------------------------------------------------------------------------
566 static int adv7511_get_modes(struct adv7511
*adv7511
,
567 struct drm_connector
*connector
)
572 /* Reading the EDID only works if the device is powered */
573 if (!adv7511
->powered
) {
574 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_POWER
,
575 ADV7511_POWER_POWER_DOWN
, 0);
576 if (adv7511
->i2c_main
->irq
) {
577 regmap_write(adv7511
->regmap
, ADV7511_REG_INT_ENABLE(0),
578 ADV7511_INT0_EDID_READY
);
579 regmap_write(adv7511
->regmap
, ADV7511_REG_INT_ENABLE(1),
580 ADV7511_INT1_DDC_ERROR
);
582 adv7511
->current_edid_segment
= -1;
585 edid
= drm_do_get_edid(connector
, adv7511_get_edid_block
, adv7511
);
587 if (!adv7511
->powered
)
588 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_POWER
,
589 ADV7511_POWER_POWER_DOWN
,
590 ADV7511_POWER_POWER_DOWN
);
592 kfree(adv7511
->edid
);
593 adv7511
->edid
= edid
;
597 drm_mode_connector_update_edid_property(connector
, edid
);
598 count
= drm_add_edid_modes(connector
, edid
);
600 adv7511_set_config_csc(adv7511
, connector
, adv7511
->rgb
);
605 static enum drm_connector_status
606 adv7511_detect(struct adv7511
*adv7511
, struct drm_connector
*connector
)
608 enum drm_connector_status status
;
613 ret
= regmap_read(adv7511
->regmap
, ADV7511_REG_STATUS
, &val
);
615 return connector_status_disconnected
;
617 if (val
& ADV7511_STATUS_HPD
)
618 status
= connector_status_connected
;
620 status
= connector_status_disconnected
;
622 hpd
= adv7511_hpd(adv7511
);
624 /* The chip resets itself when the cable is disconnected, so in case
625 * there is a pending HPD interrupt and the cable is connected there was
626 * at least one transition from disconnected to connected and the chip
627 * has to be reinitialized. */
628 if (status
== connector_status_connected
&& hpd
&& adv7511
->powered
) {
629 regcache_mark_dirty(adv7511
->regmap
);
630 adv7511_power_on(adv7511
);
631 adv7511_get_modes(adv7511
, connector
);
632 if (adv7511
->status
== connector_status_connected
)
633 status
= connector_status_disconnected
;
635 /* Renable HPD sensing */
636 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_POWER2
,
637 ADV7511_REG_POWER2_HPD_SRC_MASK
,
638 ADV7511_REG_POWER2_HPD_SRC_BOTH
);
641 adv7511
->status
= status
;
645 static int adv7511_mode_valid(struct adv7511
*adv7511
,
646 struct drm_display_mode
*mode
)
648 if (mode
->clock
> 165000)
649 return MODE_CLOCK_HIGH
;
654 static void adv7511_mode_set(struct adv7511
*adv7511
,
655 struct drm_display_mode
*mode
,
656 struct drm_display_mode
*adj_mode
)
658 unsigned int low_refresh_rate
;
659 unsigned int hsync_polarity
= 0;
660 unsigned int vsync_polarity
= 0;
662 if (adv7511
->embedded_sync
) {
663 unsigned int hsync_offset
, hsync_len
;
664 unsigned int vsync_offset
, vsync_len
;
666 hsync_offset
= adj_mode
->crtc_hsync_start
-
667 adj_mode
->crtc_hdisplay
;
668 vsync_offset
= adj_mode
->crtc_vsync_start
-
669 adj_mode
->crtc_vdisplay
;
670 hsync_len
= adj_mode
->crtc_hsync_end
-
671 adj_mode
->crtc_hsync_start
;
672 vsync_len
= adj_mode
->crtc_vsync_end
-
673 adj_mode
->crtc_vsync_start
;
675 /* The hardware vsync generator has a off-by-one bug */
678 regmap_write(adv7511
->regmap
, ADV7511_REG_HSYNC_PLACEMENT_MSB
,
679 ((hsync_offset
>> 10) & 0x7) << 5);
680 regmap_write(adv7511
->regmap
, ADV7511_REG_SYNC_DECODER(0),
681 (hsync_offset
>> 2) & 0xff);
682 regmap_write(adv7511
->regmap
, ADV7511_REG_SYNC_DECODER(1),
683 ((hsync_offset
& 0x3) << 6) |
684 ((hsync_len
>> 4) & 0x3f));
685 regmap_write(adv7511
->regmap
, ADV7511_REG_SYNC_DECODER(2),
686 ((hsync_len
& 0xf) << 4) |
687 ((vsync_offset
>> 6) & 0xf));
688 regmap_write(adv7511
->regmap
, ADV7511_REG_SYNC_DECODER(3),
689 ((vsync_offset
& 0x3f) << 2) |
690 ((vsync_len
>> 8) & 0x3));
691 regmap_write(adv7511
->regmap
, ADV7511_REG_SYNC_DECODER(4),
694 hsync_polarity
= !(adj_mode
->flags
& DRM_MODE_FLAG_PHSYNC
);
695 vsync_polarity
= !(adj_mode
->flags
& DRM_MODE_FLAG_PVSYNC
);
697 enum adv7511_sync_polarity mode_hsync_polarity
;
698 enum adv7511_sync_polarity mode_vsync_polarity
;
701 * If the input signal is always low or always high we want to
702 * invert or let it passthrough depending on the polarity of the
705 if (adj_mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
706 mode_hsync_polarity
= ADV7511_SYNC_POLARITY_LOW
;
708 mode_hsync_polarity
= ADV7511_SYNC_POLARITY_HIGH
;
710 if (adj_mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
711 mode_vsync_polarity
= ADV7511_SYNC_POLARITY_LOW
;
713 mode_vsync_polarity
= ADV7511_SYNC_POLARITY_HIGH
;
715 if (adv7511
->hsync_polarity
!= mode_hsync_polarity
&&
716 adv7511
->hsync_polarity
!=
717 ADV7511_SYNC_POLARITY_PASSTHROUGH
)
720 if (adv7511
->vsync_polarity
!= mode_vsync_polarity
&&
721 adv7511
->vsync_polarity
!=
722 ADV7511_SYNC_POLARITY_PASSTHROUGH
)
726 if (mode
->vrefresh
<= 24000)
727 low_refresh_rate
= ADV7511_LOW_REFRESH_RATE_24HZ
;
728 else if (mode
->vrefresh
<= 25000)
729 low_refresh_rate
= ADV7511_LOW_REFRESH_RATE_25HZ
;
730 else if (mode
->vrefresh
<= 30000)
731 low_refresh_rate
= ADV7511_LOW_REFRESH_RATE_30HZ
;
733 low_refresh_rate
= ADV7511_LOW_REFRESH_RATE_NONE
;
735 regmap_update_bits(adv7511
->regmap
, 0xfb,
736 0x6, low_refresh_rate
<< 1);
737 regmap_update_bits(adv7511
->regmap
, 0x17,
738 0x60, (vsync_polarity
<< 6) | (hsync_polarity
<< 5));
741 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
742 * supposed to give better results.
745 adv7511
->f_tmds
= mode
->clock
;
748 /* Connector funcs */
749 static struct adv7511
*connector_to_adv7511(struct drm_connector
*connector
)
751 return container_of(connector
, struct adv7511
, connector
);
754 static int adv7511_connector_get_modes(struct drm_connector
*connector
)
756 struct adv7511
*adv
= connector_to_adv7511(connector
);
758 return adv7511_get_modes(adv
, connector
);
761 static enum drm_mode_status
762 adv7511_connector_mode_valid(struct drm_connector
*connector
,
763 struct drm_display_mode
*mode
)
765 struct adv7511
*adv
= connector_to_adv7511(connector
);
767 return adv7511_mode_valid(adv
, mode
);
770 static struct drm_connector_helper_funcs adv7511_connector_helper_funcs
= {
771 .get_modes
= adv7511_connector_get_modes
,
772 .mode_valid
= adv7511_connector_mode_valid
,
775 static enum drm_connector_status
776 adv7511_connector_detect(struct drm_connector
*connector
, bool force
)
778 struct adv7511
*adv
= connector_to_adv7511(connector
);
780 return adv7511_detect(adv
, connector
);
783 static struct drm_connector_funcs adv7511_connector_funcs
= {
784 .dpms
= drm_atomic_helper_connector_dpms
,
785 .fill_modes
= drm_helper_probe_single_connector_modes
,
786 .detect
= adv7511_connector_detect
,
787 .destroy
= drm_connector_cleanup
,
788 .reset
= drm_atomic_helper_connector_reset
,
789 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
790 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
794 static struct adv7511
*bridge_to_adv7511(struct drm_bridge
*bridge
)
796 return container_of(bridge
, struct adv7511
, bridge
);
799 static void adv7511_bridge_enable(struct drm_bridge
*bridge
)
801 struct adv7511
*adv
= bridge_to_adv7511(bridge
);
803 adv7511_power_on(adv
);
806 static void adv7511_bridge_disable(struct drm_bridge
*bridge
)
808 struct adv7511
*adv
= bridge_to_adv7511(bridge
);
810 adv7511_power_off(adv
);
813 static void adv7511_bridge_mode_set(struct drm_bridge
*bridge
,
814 struct drm_display_mode
*mode
,
815 struct drm_display_mode
*adj_mode
)
817 struct adv7511
*adv
= bridge_to_adv7511(bridge
);
819 adv7511_mode_set(adv
, mode
, adj_mode
);
822 static int adv7511_bridge_attach(struct drm_bridge
*bridge
)
824 struct adv7511
*adv
= bridge_to_adv7511(bridge
);
827 if (!bridge
->encoder
) {
828 DRM_ERROR("Parent encoder object not found");
832 adv
->connector
.polled
= DRM_CONNECTOR_POLL_HPD
;
834 ret
= drm_connector_init(bridge
->dev
, &adv
->connector
,
835 &adv7511_connector_funcs
,
836 DRM_MODE_CONNECTOR_HDMIA
);
838 DRM_ERROR("Failed to initialize connector with drm\n");
841 drm_connector_helper_add(&adv
->connector
,
842 &adv7511_connector_helper_funcs
);
843 drm_mode_connector_attach_encoder(&adv
->connector
, bridge
->encoder
);
848 static struct drm_bridge_funcs adv7511_bridge_funcs
= {
849 .enable
= adv7511_bridge_enable
,
850 .disable
= adv7511_bridge_disable
,
851 .mode_set
= adv7511_bridge_mode_set
,
852 .attach
= adv7511_bridge_attach
,
855 /* -----------------------------------------------------------------------------
859 static int adv7511_parse_dt(struct device_node
*np
,
860 struct adv7511_link_config
*config
)
865 memset(config
, 0, sizeof(*config
));
867 of_property_read_u32(np
, "adi,input-depth", &config
->input_color_depth
);
868 if (config
->input_color_depth
!= 8 && config
->input_color_depth
!= 10 &&
869 config
->input_color_depth
!= 12)
872 ret
= of_property_read_string(np
, "adi,input-colorspace", &str
);
876 if (!strcmp(str
, "rgb"))
877 config
->input_colorspace
= HDMI_COLORSPACE_RGB
;
878 else if (!strcmp(str
, "yuv422"))
879 config
->input_colorspace
= HDMI_COLORSPACE_YUV422
;
880 else if (!strcmp(str
, "yuv444"))
881 config
->input_colorspace
= HDMI_COLORSPACE_YUV444
;
885 ret
= of_property_read_string(np
, "adi,input-clock", &str
);
889 if (!strcmp(str
, "1x"))
890 config
->input_clock
= ADV7511_INPUT_CLOCK_1X
;
891 else if (!strcmp(str
, "2x"))
892 config
->input_clock
= ADV7511_INPUT_CLOCK_2X
;
893 else if (!strcmp(str
, "ddr"))
894 config
->input_clock
= ADV7511_INPUT_CLOCK_DDR
;
898 if (config
->input_colorspace
== HDMI_COLORSPACE_YUV422
||
899 config
->input_clock
!= ADV7511_INPUT_CLOCK_1X
) {
900 ret
= of_property_read_u32(np
, "adi,input-style",
901 &config
->input_style
);
905 if (config
->input_style
< 1 || config
->input_style
> 3)
908 ret
= of_property_read_string(np
, "adi,input-justification",
913 if (!strcmp(str
, "left"))
914 config
->input_justification
=
915 ADV7511_INPUT_JUSTIFICATION_LEFT
;
916 else if (!strcmp(str
, "evenly"))
917 config
->input_justification
=
918 ADV7511_INPUT_JUSTIFICATION_EVENLY
;
919 else if (!strcmp(str
, "right"))
920 config
->input_justification
=
921 ADV7511_INPUT_JUSTIFICATION_RIGHT
;
926 config
->input_style
= 1;
927 config
->input_justification
= ADV7511_INPUT_JUSTIFICATION_LEFT
;
930 of_property_read_u32(np
, "adi,clock-delay", &config
->clock_delay
);
931 if (config
->clock_delay
< -1200 || config
->clock_delay
> 1600)
934 config
->embedded_sync
= of_property_read_bool(np
, "adi,embedded-sync");
936 /* Hardcode the sync pulse configurations for now. */
937 config
->sync_pulse
= ADV7511_INPUT_SYNC_PULSE_NONE
;
938 config
->vsync_polarity
= ADV7511_SYNC_POLARITY_PASSTHROUGH
;
939 config
->hsync_polarity
= ADV7511_SYNC_POLARITY_PASSTHROUGH
;
944 static const int edid_i2c_addr
= 0x7e;
945 static const int packet_i2c_addr
= 0x70;
946 static const int cec_i2c_addr
= 0x78;
948 static int adv7511_probe(struct i2c_client
*i2c
, const struct i2c_device_id
*id
)
950 struct adv7511_link_config link_config
;
951 struct adv7511
*adv7511
;
952 struct device
*dev
= &i2c
->dev
;
959 adv7511
= devm_kzalloc(dev
, sizeof(*adv7511
), GFP_KERNEL
);
963 adv7511
->powered
= false;
964 adv7511
->status
= connector_status_disconnected
;
966 ret
= adv7511_parse_dt(dev
->of_node
, &link_config
);
971 * The power down GPIO is optional. If present, toggle it from active to
972 * inactive to wake up the encoder.
974 adv7511
->gpio_pd
= devm_gpiod_get_optional(dev
, "pd", GPIOD_OUT_HIGH
);
975 if (IS_ERR(adv7511
->gpio_pd
))
976 return PTR_ERR(adv7511
->gpio_pd
);
978 if (adv7511
->gpio_pd
) {
980 gpiod_set_value_cansleep(adv7511
->gpio_pd
, 0);
983 adv7511
->regmap
= devm_regmap_init_i2c(i2c
, &adv7511_regmap_config
);
984 if (IS_ERR(adv7511
->regmap
))
985 return PTR_ERR(adv7511
->regmap
);
987 ret
= regmap_read(adv7511
->regmap
, ADV7511_REG_CHIP_REVISION
, &val
);
990 dev_dbg(dev
, "Rev. %d\n", val
);
992 ret
= regmap_register_patch(adv7511
->regmap
, adv7511_fixed_registers
,
993 ARRAY_SIZE(adv7511_fixed_registers
));
997 regmap_write(adv7511
->regmap
, ADV7511_REG_EDID_I2C_ADDR
, edid_i2c_addr
);
998 regmap_write(adv7511
->regmap
, ADV7511_REG_PACKET_I2C_ADDR
,
1000 regmap_write(adv7511
->regmap
, ADV7511_REG_CEC_I2C_ADDR
, cec_i2c_addr
);
1001 adv7511_packet_disable(adv7511
, 0xffff);
1003 adv7511
->i2c_main
= i2c
;
1004 adv7511
->i2c_edid
= i2c_new_dummy(i2c
->adapter
, edid_i2c_addr
>> 1);
1005 if (!adv7511
->i2c_edid
)
1009 init_waitqueue_head(&adv7511
->wq
);
1011 ret
= devm_request_threaded_irq(dev
, i2c
->irq
, NULL
,
1012 adv7511_irq_handler
,
1013 IRQF_ONESHOT
, dev_name(dev
),
1016 goto err_i2c_unregister_device
;
1019 /* CEC is unused for now */
1020 regmap_write(adv7511
->regmap
, ADV7511_REG_CEC_CTRL
,
1021 ADV7511_CEC_CTRL_POWER_DOWN
);
1023 adv7511_power_off(adv7511
);
1025 i2c_set_clientdata(i2c
, adv7511
);
1027 adv7511_set_link_config(adv7511
, &link_config
);
1029 adv7511
->bridge
.funcs
= &adv7511_bridge_funcs
;
1030 adv7511
->bridge
.of_node
= dev
->of_node
;
1032 ret
= drm_bridge_add(&adv7511
->bridge
);
1034 dev_err(dev
, "failed to add adv7511 bridge\n");
1035 goto err_i2c_unregister_device
;
1040 err_i2c_unregister_device
:
1041 i2c_unregister_device(adv7511
->i2c_edid
);
1046 static int adv7511_remove(struct i2c_client
*i2c
)
1048 struct adv7511
*adv7511
= i2c_get_clientdata(i2c
);
1050 drm_bridge_remove(&adv7511
->bridge
);
1052 i2c_unregister_device(adv7511
->i2c_edid
);
1054 kfree(adv7511
->edid
);
1059 static const struct i2c_device_id adv7511_i2c_ids
[] = {
1065 MODULE_DEVICE_TABLE(i2c
, adv7511_i2c_ids
);
1067 static const struct of_device_id adv7511_of_ids
[] = {
1068 { .compatible
= "adi,adv7511", },
1069 { .compatible
= "adi,adv7511w", },
1070 { .compatible
= "adi,adv7513", },
1073 MODULE_DEVICE_TABLE(of
, adv7511_of_ids
);
1075 static struct i2c_driver adv7511_driver
= {
1078 .of_match_table
= adv7511_of_ids
,
1080 .id_table
= adv7511_i2c_ids
,
1081 .probe
= adv7511_probe
,
1082 .remove
= adv7511_remove
,
1085 module_i2c_driver(adv7511_driver
);
1087 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1088 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1089 MODULE_LICENSE("GPL");