2 * Analog Devices ADV7511 HDMI transmitter driver
4 * Copyright 2012 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/device.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/slab.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_edid.h>
19 #include <drm/drm_encoder_slave.h>
24 struct i2c_client
*i2c_main
;
25 struct i2c_client
*i2c_edid
;
27 struct regmap
*regmap
;
28 struct regmap
*packet_memory_regmap
;
29 enum drm_connector_status status
;
34 unsigned int current_edid_segment
;
35 uint8_t edid_buf
[256];
39 struct drm_encoder
*encoder
;
42 enum adv7511_sync_polarity vsync_polarity
;
43 enum adv7511_sync_polarity hsync_polarity
;
48 struct gpio_desc
*gpio_pd
;
51 static struct adv7511
*encoder_to_adv7511(struct drm_encoder
*encoder
)
53 return to_encoder_slave(encoder
)->slave_priv
;
56 /* ADI recommended values for proper operation. */
57 static const struct reg_default adv7511_fixed_registers
[] = {
69 /* -----------------------------------------------------------------------------
73 static const uint8_t adv7511_register_defaults
[] = {
74 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
75 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
76 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
77 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
78 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
79 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
81 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
82 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
83 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
84 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
85 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
93 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
94 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
95 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
97 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
99 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
100 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
102 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
103 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
105 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108 static bool adv7511_register_volatile(struct device
*dev
, unsigned int reg
)
111 case ADV7511_REG_CHIP_REVISION
:
112 case ADV7511_REG_SPDIF_FREQ
:
113 case ADV7511_REG_CTS_AUTOMATIC1
:
114 case ADV7511_REG_CTS_AUTOMATIC2
:
115 case ADV7511_REG_VIC_DETECTED
:
116 case ADV7511_REG_VIC_SEND
:
117 case ADV7511_REG_AUX_VIC_DETECTED
:
118 case ADV7511_REG_STATUS
:
119 case ADV7511_REG_GC(1):
120 case ADV7511_REG_INT(0):
121 case ADV7511_REG_INT(1):
122 case ADV7511_REG_PLL_STATUS
:
123 case ADV7511_REG_AN(0):
124 case ADV7511_REG_AN(1):
125 case ADV7511_REG_AN(2):
126 case ADV7511_REG_AN(3):
127 case ADV7511_REG_AN(4):
128 case ADV7511_REG_AN(5):
129 case ADV7511_REG_AN(6):
130 case ADV7511_REG_AN(7):
131 case ADV7511_REG_HDCP_STATUS
:
132 case ADV7511_REG_BCAPS
:
133 case ADV7511_REG_BKSV(0):
134 case ADV7511_REG_BKSV(1):
135 case ADV7511_REG_BKSV(2):
136 case ADV7511_REG_BKSV(3):
137 case ADV7511_REG_BKSV(4):
138 case ADV7511_REG_DDC_STATUS
:
139 case ADV7511_REG_BSTATUS(0):
140 case ADV7511_REG_BSTATUS(1):
141 case ADV7511_REG_CHIP_ID_HIGH
:
142 case ADV7511_REG_CHIP_ID_LOW
:
149 static const struct regmap_config adv7511_regmap_config
= {
153 .max_register
= 0xff,
154 .cache_type
= REGCACHE_RBTREE
,
155 .reg_defaults_raw
= adv7511_register_defaults
,
156 .num_reg_defaults_raw
= ARRAY_SIZE(adv7511_register_defaults
),
158 .volatile_reg
= adv7511_register_volatile
,
161 /* -----------------------------------------------------------------------------
162 * Hardware configuration
165 static void adv7511_set_colormap(struct adv7511
*adv7511
, bool enable
,
166 const uint16_t *coeff
,
167 unsigned int scaling_factor
)
171 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_CSC_UPPER(1),
172 ADV7511_CSC_UPDATE_MODE
, ADV7511_CSC_UPDATE_MODE
);
175 for (i
= 0; i
< 12; ++i
) {
176 regmap_update_bits(adv7511
->regmap
,
177 ADV7511_REG_CSC_UPPER(i
),
178 0x1f, coeff
[i
] >> 8);
179 regmap_write(adv7511
->regmap
,
180 ADV7511_REG_CSC_LOWER(i
),
186 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_CSC_UPPER(0),
187 0xe0, 0x80 | (scaling_factor
<< 5));
189 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_CSC_UPPER(0),
192 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_CSC_UPPER(1),
193 ADV7511_CSC_UPDATE_MODE
, 0);
196 static int adv7511_packet_enable(struct adv7511
*adv7511
, unsigned int packet
)
199 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE0
,
202 if (packet
& 0xff00) {
204 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
211 static int adv7511_packet_disable(struct adv7511
*adv7511
, unsigned int packet
)
214 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE0
,
217 if (packet
& 0xff00) {
219 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
226 /* Coefficients for adv7511 color space conversion */
227 static const uint16_t adv7511_csc_ycbcr_to_rgb
[] = {
228 0x0734, 0x04ad, 0x0000, 0x1c1b,
229 0x1ddc, 0x04ad, 0x1f24, 0x0135,
230 0x0000, 0x04ad, 0x087c, 0x1b77,
233 static void adv7511_set_config_csc(struct adv7511
*adv7511
,
234 struct drm_connector
*connector
,
237 struct adv7511_video_config config
;
238 bool output_format_422
, output_format_ycbcr
;
240 uint8_t infoframe
[17];
243 config
.hdmi_mode
= drm_detect_hdmi_monitor(adv7511
->edid
);
245 config
.hdmi_mode
= false;
247 hdmi_avi_infoframe_init(&config
.avi_infoframe
);
249 config
.avi_infoframe
.scan_mode
= HDMI_SCAN_MODE_UNDERSCAN
;
252 config
.csc_enable
= false;
253 config
.avi_infoframe
.colorspace
= HDMI_COLORSPACE_RGB
;
255 config
.csc_scaling_factor
= ADV7511_CSC_SCALING_4
;
256 config
.csc_coefficents
= adv7511_csc_ycbcr_to_rgb
;
258 if ((connector
->display_info
.color_formats
&
259 DRM_COLOR_FORMAT_YCRCB422
) &&
261 config
.csc_enable
= false;
262 config
.avi_infoframe
.colorspace
=
263 HDMI_COLORSPACE_YUV422
;
265 config
.csc_enable
= true;
266 config
.avi_infoframe
.colorspace
= HDMI_COLORSPACE_RGB
;
270 if (config
.hdmi_mode
) {
271 mode
= ADV7511_HDMI_CFG_MODE_HDMI
;
273 switch (config
.avi_infoframe
.colorspace
) {
274 case HDMI_COLORSPACE_YUV444
:
275 output_format_422
= false;
276 output_format_ycbcr
= true;
278 case HDMI_COLORSPACE_YUV422
:
279 output_format_422
= true;
280 output_format_ycbcr
= true;
283 output_format_422
= false;
284 output_format_ycbcr
= false;
288 mode
= ADV7511_HDMI_CFG_MODE_DVI
;
289 output_format_422
= false;
290 output_format_ycbcr
= false;
293 adv7511_packet_disable(adv7511
, ADV7511_PACKET_ENABLE_AVI_INFOFRAME
);
295 adv7511_set_colormap(adv7511
, config
.csc_enable
,
296 config
.csc_coefficents
,
297 config
.csc_scaling_factor
);
299 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_VIDEO_INPUT_CFG1
, 0x81,
300 (output_format_422
<< 7) | output_format_ycbcr
);
302 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_HDCP_HDMI_CFG
,
303 ADV7511_HDMI_CFG_MODE_MASK
, mode
);
305 hdmi_avi_infoframe_pack(&config
.avi_infoframe
, infoframe
,
308 /* The AVI infoframe id is not configurable */
309 regmap_bulk_write(adv7511
->regmap
, ADV7511_REG_AVI_INFOFRAME_VERSION
,
310 infoframe
+ 1, sizeof(infoframe
) - 1);
312 adv7511_packet_enable(adv7511
, ADV7511_PACKET_ENABLE_AVI_INFOFRAME
);
315 static void adv7511_set_link_config(struct adv7511
*adv7511
,
316 const struct adv7511_link_config
*config
)
319 * The input style values documented in the datasheet don't match the
320 * hardware register field values :-(
322 static const unsigned int input_styles
[4] = { 0, 2, 1, 3 };
324 unsigned int clock_delay
;
325 unsigned int color_depth
;
326 unsigned int input_id
;
328 clock_delay
= (config
->clock_delay
+ 1200) / 400;
329 color_depth
= config
->input_color_depth
== 8 ? 3
330 : (config
->input_color_depth
== 10 ? 1 : 2);
332 /* TODO Support input ID 6 */
333 if (config
->input_colorspace
!= HDMI_COLORSPACE_YUV422
)
334 input_id
= config
->input_clock
== ADV7511_INPUT_CLOCK_DDR
336 else if (config
->input_clock
== ADV7511_INPUT_CLOCK_DDR
)
337 input_id
= config
->embedded_sync
? 8 : 7;
338 else if (config
->input_clock
== ADV7511_INPUT_CLOCK_2X
)
339 input_id
= config
->embedded_sync
? 4 : 3;
341 input_id
= config
->embedded_sync
? 2 : 1;
343 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_I2C_FREQ_ID_CFG
, 0xf,
345 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_VIDEO_INPUT_CFG1
, 0x7e,
347 (input_styles
[config
->input_style
] << 2));
348 regmap_write(adv7511
->regmap
, ADV7511_REG_VIDEO_INPUT_CFG2
,
349 config
->input_justification
<< 3);
350 regmap_write(adv7511
->regmap
, ADV7511_REG_TIMING_GEN_SEQ
,
351 config
->sync_pulse
<< 2);
353 regmap_write(adv7511
->regmap
, 0xba, clock_delay
<< 5);
355 adv7511
->embedded_sync
= config
->embedded_sync
;
356 adv7511
->hsync_polarity
= config
->hsync_polarity
;
357 adv7511
->vsync_polarity
= config
->vsync_polarity
;
358 adv7511
->rgb
= config
->input_colorspace
== HDMI_COLORSPACE_RGB
;
361 static void adv7511_power_on(struct adv7511
*adv7511
)
363 adv7511
->current_edid_segment
= -1;
365 regmap_write(adv7511
->regmap
, ADV7511_REG_INT(0),
366 ADV7511_INT0_EDID_READY
);
367 regmap_write(adv7511
->regmap
, ADV7511_REG_INT(1),
368 ADV7511_INT1_DDC_ERROR
);
369 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_POWER
,
370 ADV7511_POWER_POWER_DOWN
, 0);
373 * Per spec it is allowed to pulse the HDP signal to indicate that the
374 * EDID information has changed. Some monitors do this when they wakeup
375 * from standby or are enabled. When the HDP goes low the adv7511 is
376 * reset and the outputs are disabled which might cause the monitor to
377 * go to standby again. To avoid this we ignore the HDP pin for the
378 * first few seconds after enabling the output.
380 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_POWER2
,
381 ADV7511_REG_POWER2_HDP_SRC_MASK
,
382 ADV7511_REG_POWER2_HDP_SRC_NONE
);
385 * Most of the registers are reset during power down or when HPD is low.
387 regcache_sync(adv7511
->regmap
);
389 adv7511
->powered
= true;
392 static void adv7511_power_off(struct adv7511
*adv7511
)
394 /* TODO: setup additional power down modes */
395 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_POWER
,
396 ADV7511_POWER_POWER_DOWN
,
397 ADV7511_POWER_POWER_DOWN
);
398 regcache_mark_dirty(adv7511
->regmap
);
400 adv7511
->powered
= false;
403 /* -----------------------------------------------------------------------------
404 * Interrupt and hotplug detection
407 static bool adv7511_hpd(struct adv7511
*adv7511
)
412 ret
= regmap_read(adv7511
->regmap
, ADV7511_REG_INT(0), &irq0
);
416 if (irq0
& ADV7511_INT0_HDP
) {
417 regmap_write(adv7511
->regmap
, ADV7511_REG_INT(0),
425 static int adv7511_irq_process(struct adv7511
*adv7511
)
427 unsigned int irq0
, irq1
;
430 ret
= regmap_read(adv7511
->regmap
, ADV7511_REG_INT(0), &irq0
);
434 ret
= regmap_read(adv7511
->regmap
, ADV7511_REG_INT(1), &irq1
);
438 regmap_write(adv7511
->regmap
, ADV7511_REG_INT(0), irq0
);
439 regmap_write(adv7511
->regmap
, ADV7511_REG_INT(1), irq1
);
441 if (irq0
& ADV7511_INT0_HDP
&& adv7511
->encoder
)
442 drm_helper_hpd_irq_event(adv7511
->encoder
->dev
);
444 if (irq0
& ADV7511_INT0_EDID_READY
|| irq1
& ADV7511_INT1_DDC_ERROR
) {
445 adv7511
->edid_read
= true;
447 if (adv7511
->i2c_main
->irq
)
448 wake_up_all(&adv7511
->wq
);
454 static irqreturn_t
adv7511_irq_handler(int irq
, void *devid
)
456 struct adv7511
*adv7511
= devid
;
459 ret
= adv7511_irq_process(adv7511
);
460 return ret
< 0 ? IRQ_NONE
: IRQ_HANDLED
;
463 /* -----------------------------------------------------------------------------
467 static int adv7511_wait_for_edid(struct adv7511
*adv7511
, int timeout
)
471 if (adv7511
->i2c_main
->irq
) {
472 ret
= wait_event_interruptible_timeout(adv7511
->wq
,
473 adv7511
->edid_read
, msecs_to_jiffies(timeout
));
475 for (; timeout
> 0; timeout
-= 25) {
476 ret
= adv7511_irq_process(adv7511
);
480 if (adv7511
->edid_read
)
487 return adv7511
->edid_read
? 0 : -EIO
;
490 static int adv7511_get_edid_block(void *data
, u8
*buf
, unsigned int block
,
493 struct adv7511
*adv7511
= data
;
494 struct i2c_msg xfer
[2];
502 if (adv7511
->current_edid_segment
!= block
/ 2) {
505 ret
= regmap_read(adv7511
->regmap
, ADV7511_REG_DDC_STATUS
,
511 adv7511
->edid_read
= false;
512 regmap_write(adv7511
->regmap
, ADV7511_REG_EDID_SEGMENT
,
514 ret
= adv7511_wait_for_edid(adv7511
, 200);
519 /* Break this apart, hopefully more I2C controllers will
520 * support 64 byte transfers than 256 byte transfers
523 xfer
[0].addr
= adv7511
->i2c_edid
->addr
;
526 xfer
[0].buf
= &offset
;
527 xfer
[1].addr
= adv7511
->i2c_edid
->addr
;
528 xfer
[1].flags
= I2C_M_RD
;
530 xfer
[1].buf
= adv7511
->edid_buf
;
534 for (i
= 0; i
< 4; ++i
) {
535 ret
= i2c_transfer(adv7511
->i2c_edid
->adapter
, xfer
,
546 adv7511
->current_edid_segment
= block
/ 2;
550 memcpy(buf
, adv7511
->edid_buf
, len
);
552 memcpy(buf
, adv7511
->edid_buf
+ 128, len
);
557 /* -----------------------------------------------------------------------------
561 static int adv7511_get_modes(struct drm_encoder
*encoder
,
562 struct drm_connector
*connector
)
564 struct adv7511
*adv7511
= encoder_to_adv7511(encoder
);
568 /* Reading the EDID only works if the device is powered */
569 if (!adv7511
->powered
) {
570 regmap_write(adv7511
->regmap
, ADV7511_REG_INT(0),
571 ADV7511_INT0_EDID_READY
);
572 regmap_write(adv7511
->regmap
, ADV7511_REG_INT(1),
573 ADV7511_INT1_DDC_ERROR
);
574 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_POWER
,
575 ADV7511_POWER_POWER_DOWN
, 0);
576 adv7511
->current_edid_segment
= -1;
579 edid
= drm_do_get_edid(connector
, adv7511_get_edid_block
, adv7511
);
581 if (!adv7511
->powered
)
582 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_POWER
,
583 ADV7511_POWER_POWER_DOWN
,
584 ADV7511_POWER_POWER_DOWN
);
586 kfree(adv7511
->edid
);
587 adv7511
->edid
= edid
;
591 drm_mode_connector_update_edid_property(connector
, edid
);
592 count
= drm_add_edid_modes(connector
, edid
);
594 adv7511_set_config_csc(adv7511
, connector
, adv7511
->rgb
);
599 static void adv7511_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
601 struct adv7511
*adv7511
= encoder_to_adv7511(encoder
);
603 if (mode
== DRM_MODE_DPMS_ON
)
604 adv7511_power_on(adv7511
);
606 adv7511_power_off(adv7511
);
609 static enum drm_connector_status
610 adv7511_encoder_detect(struct drm_encoder
*encoder
,
611 struct drm_connector
*connector
)
613 struct adv7511
*adv7511
= encoder_to_adv7511(encoder
);
614 enum drm_connector_status status
;
619 ret
= regmap_read(adv7511
->regmap
, ADV7511_REG_STATUS
, &val
);
621 return connector_status_disconnected
;
623 if (val
& ADV7511_STATUS_HPD
)
624 status
= connector_status_connected
;
626 status
= connector_status_disconnected
;
628 hpd
= adv7511_hpd(adv7511
);
630 /* The chip resets itself when the cable is disconnected, so in case
631 * there is a pending HPD interrupt and the cable is connected there was
632 * at least one transition from disconnected to connected and the chip
633 * has to be reinitialized. */
634 if (status
== connector_status_connected
&& hpd
&& adv7511
->powered
) {
635 regcache_mark_dirty(adv7511
->regmap
);
636 adv7511_power_on(adv7511
);
637 adv7511_get_modes(encoder
, connector
);
638 if (adv7511
->status
== connector_status_connected
)
639 status
= connector_status_disconnected
;
641 /* Renable HDP sensing */
642 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_POWER2
,
643 ADV7511_REG_POWER2_HDP_SRC_MASK
,
644 ADV7511_REG_POWER2_HDP_SRC_BOTH
);
647 adv7511
->status
= status
;
651 static int adv7511_encoder_mode_valid(struct drm_encoder
*encoder
,
652 struct drm_display_mode
*mode
)
654 if (mode
->clock
> 165000)
655 return MODE_CLOCK_HIGH
;
660 static void adv7511_encoder_mode_set(struct drm_encoder
*encoder
,
661 struct drm_display_mode
*mode
,
662 struct drm_display_mode
*adj_mode
)
664 struct adv7511
*adv7511
= encoder_to_adv7511(encoder
);
665 unsigned int low_refresh_rate
;
666 unsigned int hsync_polarity
= 0;
667 unsigned int vsync_polarity
= 0;
669 if (adv7511
->embedded_sync
) {
670 unsigned int hsync_offset
, hsync_len
;
671 unsigned int vsync_offset
, vsync_len
;
673 hsync_offset
= adj_mode
->crtc_hsync_start
-
674 adj_mode
->crtc_hdisplay
;
675 vsync_offset
= adj_mode
->crtc_vsync_start
-
676 adj_mode
->crtc_vdisplay
;
677 hsync_len
= adj_mode
->crtc_hsync_end
-
678 adj_mode
->crtc_hsync_start
;
679 vsync_len
= adj_mode
->crtc_vsync_end
-
680 adj_mode
->crtc_vsync_start
;
682 /* The hardware vsync generator has a off-by-one bug */
685 regmap_write(adv7511
->regmap
, ADV7511_REG_HSYNC_PLACEMENT_MSB
,
686 ((hsync_offset
>> 10) & 0x7) << 5);
687 regmap_write(adv7511
->regmap
, ADV7511_REG_SYNC_DECODER(0),
688 (hsync_offset
>> 2) & 0xff);
689 regmap_write(adv7511
->regmap
, ADV7511_REG_SYNC_DECODER(1),
690 ((hsync_offset
& 0x3) << 6) |
691 ((hsync_len
>> 4) & 0x3f));
692 regmap_write(adv7511
->regmap
, ADV7511_REG_SYNC_DECODER(2),
693 ((hsync_len
& 0xf) << 4) |
694 ((vsync_offset
>> 6) & 0xf));
695 regmap_write(adv7511
->regmap
, ADV7511_REG_SYNC_DECODER(3),
696 ((vsync_offset
& 0x3f) << 2) |
697 ((vsync_len
>> 8) & 0x3));
698 regmap_write(adv7511
->regmap
, ADV7511_REG_SYNC_DECODER(4),
701 hsync_polarity
= !(adj_mode
->flags
& DRM_MODE_FLAG_PHSYNC
);
702 vsync_polarity
= !(adj_mode
->flags
& DRM_MODE_FLAG_PVSYNC
);
704 enum adv7511_sync_polarity mode_hsync_polarity
;
705 enum adv7511_sync_polarity mode_vsync_polarity
;
708 * If the input signal is always low or always high we want to
709 * invert or let it passthrough depending on the polarity of the
712 if (adj_mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
713 mode_hsync_polarity
= ADV7511_SYNC_POLARITY_LOW
;
715 mode_hsync_polarity
= ADV7511_SYNC_POLARITY_HIGH
;
717 if (adj_mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
718 mode_vsync_polarity
= ADV7511_SYNC_POLARITY_LOW
;
720 mode_vsync_polarity
= ADV7511_SYNC_POLARITY_HIGH
;
722 if (adv7511
->hsync_polarity
!= mode_hsync_polarity
&&
723 adv7511
->hsync_polarity
!=
724 ADV7511_SYNC_POLARITY_PASSTHROUGH
)
727 if (adv7511
->vsync_polarity
!= mode_vsync_polarity
&&
728 adv7511
->vsync_polarity
!=
729 ADV7511_SYNC_POLARITY_PASSTHROUGH
)
733 if (mode
->vrefresh
<= 24000)
734 low_refresh_rate
= ADV7511_LOW_REFRESH_RATE_24HZ
;
735 else if (mode
->vrefresh
<= 25000)
736 low_refresh_rate
= ADV7511_LOW_REFRESH_RATE_25HZ
;
737 else if (mode
->vrefresh
<= 30000)
738 low_refresh_rate
= ADV7511_LOW_REFRESH_RATE_30HZ
;
740 low_refresh_rate
= ADV7511_LOW_REFRESH_RATE_NONE
;
742 regmap_update_bits(adv7511
->regmap
, 0xfb,
743 0x6, low_refresh_rate
<< 1);
744 regmap_update_bits(adv7511
->regmap
, 0x17,
745 0x60, (vsync_polarity
<< 6) | (hsync_polarity
<< 5));
748 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
749 * supposed to give better results.
752 adv7511
->f_tmds
= mode
->clock
;
755 static struct drm_encoder_slave_funcs adv7511_encoder_funcs
= {
756 .dpms
= adv7511_encoder_dpms
,
757 .mode_valid
= adv7511_encoder_mode_valid
,
758 .mode_set
= adv7511_encoder_mode_set
,
759 .detect
= adv7511_encoder_detect
,
760 .get_modes
= adv7511_get_modes
,
763 /* -----------------------------------------------------------------------------
767 static int adv7511_parse_dt(struct device_node
*np
,
768 struct adv7511_link_config
*config
)
773 memset(config
, 0, sizeof(*config
));
775 of_property_read_u32(np
, "adi,input-depth", &config
->input_color_depth
);
776 if (config
->input_color_depth
!= 8 && config
->input_color_depth
!= 10 &&
777 config
->input_color_depth
!= 12)
780 ret
= of_property_read_string(np
, "adi,input-colorspace", &str
);
784 if (!strcmp(str
, "rgb"))
785 config
->input_colorspace
= HDMI_COLORSPACE_RGB
;
786 else if (!strcmp(str
, "yuv422"))
787 config
->input_colorspace
= HDMI_COLORSPACE_YUV422
;
788 else if (!strcmp(str
, "yuv444"))
789 config
->input_colorspace
= HDMI_COLORSPACE_YUV444
;
793 ret
= of_property_read_string(np
, "adi,input-clock", &str
);
797 if (!strcmp(str
, "1x"))
798 config
->input_clock
= ADV7511_INPUT_CLOCK_1X
;
799 else if (!strcmp(str
, "2x"))
800 config
->input_clock
= ADV7511_INPUT_CLOCK_2X
;
801 else if (!strcmp(str
, "ddr"))
802 config
->input_clock
= ADV7511_INPUT_CLOCK_DDR
;
806 if (config
->input_colorspace
== HDMI_COLORSPACE_YUV422
||
807 config
->input_clock
!= ADV7511_INPUT_CLOCK_1X
) {
808 ret
= of_property_read_u32(np
, "adi,input-style",
809 &config
->input_style
);
813 if (config
->input_style
< 1 || config
->input_style
> 3)
816 ret
= of_property_read_string(np
, "adi,input-justification",
821 if (!strcmp(str
, "left"))
822 config
->input_justification
=
823 ADV7511_INPUT_JUSTIFICATION_LEFT
;
824 else if (!strcmp(str
, "evenly"))
825 config
->input_justification
=
826 ADV7511_INPUT_JUSTIFICATION_EVENLY
;
827 else if (!strcmp(str
, "right"))
828 config
->input_justification
=
829 ADV7511_INPUT_JUSTIFICATION_RIGHT
;
834 config
->input_style
= 1;
835 config
->input_justification
= ADV7511_INPUT_JUSTIFICATION_LEFT
;
838 of_property_read_u32(np
, "adi,clock-delay", &config
->clock_delay
);
839 if (config
->clock_delay
< -1200 || config
->clock_delay
> 1600)
842 config
->embedded_sync
= of_property_read_bool(np
, "adi,embedded-sync");
844 /* Hardcode the sync pulse configurations for now. */
845 config
->sync_pulse
= ADV7511_INPUT_SYNC_PULSE_NONE
;
846 config
->vsync_polarity
= ADV7511_SYNC_POLARITY_PASSTHROUGH
;
847 config
->hsync_polarity
= ADV7511_SYNC_POLARITY_PASSTHROUGH
;
852 static const int edid_i2c_addr
= 0x7e;
853 static const int packet_i2c_addr
= 0x70;
854 static const int cec_i2c_addr
= 0x78;
856 static int adv7511_probe(struct i2c_client
*i2c
, const struct i2c_device_id
*id
)
858 struct adv7511_link_config link_config
;
859 struct adv7511
*adv7511
;
860 struct device
*dev
= &i2c
->dev
;
867 adv7511
= devm_kzalloc(dev
, sizeof(*adv7511
), GFP_KERNEL
);
871 adv7511
->powered
= false;
872 adv7511
->status
= connector_status_disconnected
;
874 ret
= adv7511_parse_dt(dev
->of_node
, &link_config
);
879 * The power down GPIO is optional. If present, toggle it from active to
880 * inactive to wake up the encoder.
882 adv7511
->gpio_pd
= devm_gpiod_get_optional(dev
, "pd", GPIOD_OUT_HIGH
);
883 if (IS_ERR(adv7511
->gpio_pd
))
884 return PTR_ERR(adv7511
->gpio_pd
);
886 if (adv7511
->gpio_pd
) {
888 gpiod_set_value_cansleep(adv7511
->gpio_pd
, 0);
891 adv7511
->regmap
= devm_regmap_init_i2c(i2c
, &adv7511_regmap_config
);
892 if (IS_ERR(adv7511
->regmap
))
893 return PTR_ERR(adv7511
->regmap
);
895 ret
= regmap_read(adv7511
->regmap
, ADV7511_REG_CHIP_REVISION
, &val
);
898 dev_dbg(dev
, "Rev. %d\n", val
);
900 ret
= regmap_register_patch(adv7511
->regmap
, adv7511_fixed_registers
,
901 ARRAY_SIZE(adv7511_fixed_registers
));
905 regmap_write(adv7511
->regmap
, ADV7511_REG_EDID_I2C_ADDR
, edid_i2c_addr
);
906 regmap_write(adv7511
->regmap
, ADV7511_REG_PACKET_I2C_ADDR
,
908 regmap_write(adv7511
->regmap
, ADV7511_REG_CEC_I2C_ADDR
, cec_i2c_addr
);
909 adv7511_packet_disable(adv7511
, 0xffff);
911 adv7511
->i2c_main
= i2c
;
912 adv7511
->i2c_edid
= i2c_new_dummy(i2c
->adapter
, edid_i2c_addr
>> 1);
913 if (!adv7511
->i2c_edid
)
917 init_waitqueue_head(&adv7511
->wq
);
919 ret
= devm_request_threaded_irq(dev
, i2c
->irq
, NULL
,
921 IRQF_ONESHOT
, dev_name(dev
),
924 goto err_i2c_unregister_device
;
927 /* CEC is unused for now */
928 regmap_write(adv7511
->regmap
, ADV7511_REG_CEC_CTRL
,
929 ADV7511_CEC_CTRL_POWER_DOWN
);
931 adv7511_power_off(adv7511
);
933 i2c_set_clientdata(i2c
, adv7511
);
935 adv7511_set_link_config(adv7511
, &link_config
);
939 err_i2c_unregister_device
:
940 i2c_unregister_device(adv7511
->i2c_edid
);
945 static int adv7511_remove(struct i2c_client
*i2c
)
947 struct adv7511
*adv7511
= i2c_get_clientdata(i2c
);
949 i2c_unregister_device(adv7511
->i2c_edid
);
951 kfree(adv7511
->edid
);
956 static int adv7511_encoder_init(struct i2c_client
*i2c
, struct drm_device
*dev
,
957 struct drm_encoder_slave
*encoder
)
960 struct adv7511
*adv7511
= i2c_get_clientdata(i2c
);
962 encoder
->slave_priv
= adv7511
;
963 encoder
->slave_funcs
= &adv7511_encoder_funcs
;
965 adv7511
->encoder
= &encoder
->base
;
970 static const struct i2c_device_id adv7511_i2c_ids
[] = {
976 MODULE_DEVICE_TABLE(i2c
, adv7511_i2c_ids
);
978 static const struct of_device_id adv7511_of_ids
[] = {
979 { .compatible
= "adi,adv7511", },
980 { .compatible
= "adi,adv7511w", },
981 { .compatible
= "adi,adv7513", },
984 MODULE_DEVICE_TABLE(of
, adv7511_of_ids
);
986 static struct drm_i2c_encoder_driver adv7511_driver
= {
990 .of_match_table
= adv7511_of_ids
,
992 .id_table
= adv7511_i2c_ids
,
993 .probe
= adv7511_probe
,
994 .remove
= adv7511_remove
,
997 .encoder_init
= adv7511_encoder_init
,
1000 static int __init
adv7511_init(void)
1002 return drm_i2c_encoder_register(THIS_MODULE
, &adv7511_driver
);
1004 module_init(adv7511_init
);
1006 static void __exit
adv7511_exit(void)
1008 drm_i2c_encoder_unregister(&adv7511_driver
);
1010 module_exit(adv7511_exit
);
1012 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1013 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1014 MODULE_LICENSE("GPL");