Linux 4.9.243
[linux/fpc-iii.git] / drivers / gpu / drm / bridge / adv7511 / adv7511_drv.c
blob1b2fae915eccb2901d63730a59a5526b9853b2e8
1 /*
2 * Analog Devices ADV7511 HDMI transmitter driver
4 * Copyright 2012 Analog Devices Inc.
6 * Licensed under the GPL-2.
7 */
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 <drm/drmP.h>
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_edid.h>
20 #include "adv7511.h"
22 /* ADI recommended values for proper operation. */
23 static const struct reg_sequence adv7511_fixed_registers[] = {
24 { 0x98, 0x03 },
25 { 0x9a, 0xe0 },
26 { 0x9c, 0x30 },
27 { 0x9d, 0x61 },
28 { 0xa2, 0xa4 },
29 { 0xa3, 0xa4 },
30 { 0xe0, 0xd0 },
31 { 0xf9, 0x00 },
32 { 0x55, 0x02 },
35 /* -----------------------------------------------------------------------------
36 * Register access
39 static const uint8_t adv7511_register_defaults[] = {
40 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
41 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
42 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
43 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
44 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
45 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
47 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
48 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
49 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
50 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
59 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
60 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
61 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
65 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
66 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
68 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
71 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
76 switch (reg) {
77 case ADV7511_REG_CHIP_REVISION:
78 case ADV7511_REG_SPDIF_FREQ:
79 case ADV7511_REG_CTS_AUTOMATIC1:
80 case ADV7511_REG_CTS_AUTOMATIC2:
81 case ADV7511_REG_VIC_DETECTED:
82 case ADV7511_REG_VIC_SEND:
83 case ADV7511_REG_AUX_VIC_DETECTED:
84 case ADV7511_REG_STATUS:
85 case ADV7511_REG_GC(1):
86 case ADV7511_REG_INT(0):
87 case ADV7511_REG_INT(1):
88 case ADV7511_REG_PLL_STATUS:
89 case ADV7511_REG_AN(0):
90 case ADV7511_REG_AN(1):
91 case ADV7511_REG_AN(2):
92 case ADV7511_REG_AN(3):
93 case ADV7511_REG_AN(4):
94 case ADV7511_REG_AN(5):
95 case ADV7511_REG_AN(6):
96 case ADV7511_REG_AN(7):
97 case ADV7511_REG_HDCP_STATUS:
98 case ADV7511_REG_BCAPS:
99 case ADV7511_REG_BKSV(0):
100 case ADV7511_REG_BKSV(1):
101 case ADV7511_REG_BKSV(2):
102 case ADV7511_REG_BKSV(3):
103 case ADV7511_REG_BKSV(4):
104 case ADV7511_REG_DDC_STATUS:
105 case ADV7511_REG_EDID_READ_CTRL:
106 case ADV7511_REG_BSTATUS(0):
107 case ADV7511_REG_BSTATUS(1):
108 case ADV7511_REG_CHIP_ID_HIGH:
109 case ADV7511_REG_CHIP_ID_LOW:
110 return true;
113 return false;
116 static const struct regmap_config adv7511_regmap_config = {
117 .reg_bits = 8,
118 .val_bits = 8,
120 .max_register = 0xff,
121 .cache_type = REGCACHE_RBTREE,
122 .reg_defaults_raw = adv7511_register_defaults,
123 .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
125 .volatile_reg = adv7511_register_volatile,
128 /* -----------------------------------------------------------------------------
129 * Hardware configuration
132 static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
133 const uint16_t *coeff,
134 unsigned int scaling_factor)
136 unsigned int i;
138 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
139 ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
141 if (enable) {
142 for (i = 0; i < 12; ++i) {
143 regmap_update_bits(adv7511->regmap,
144 ADV7511_REG_CSC_UPPER(i),
145 0x1f, coeff[i] >> 8);
146 regmap_write(adv7511->regmap,
147 ADV7511_REG_CSC_LOWER(i),
148 coeff[i] & 0xff);
152 if (enable)
153 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
154 0xe0, 0x80 | (scaling_factor << 5));
155 else
156 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
157 0x80, 0x00);
159 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
160 ADV7511_CSC_UPDATE_MODE, 0);
163 static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
165 if (packet & 0xff)
166 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
167 packet, 0xff);
169 if (packet & 0xff00) {
170 packet >>= 8;
171 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
172 packet, 0xff);
175 return 0;
178 static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
180 if (packet & 0xff)
181 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
182 packet, 0x00);
184 if (packet & 0xff00) {
185 packet >>= 8;
186 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
187 packet, 0x00);
190 return 0;
193 /* Coefficients for adv7511 color space conversion */
194 static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
195 0x0734, 0x04ad, 0x0000, 0x1c1b,
196 0x1ddc, 0x04ad, 0x1f24, 0x0135,
197 0x0000, 0x04ad, 0x087c, 0x1b77,
200 static void adv7511_set_config_csc(struct adv7511 *adv7511,
201 struct drm_connector *connector,
202 bool rgb)
204 struct adv7511_video_config config;
205 bool output_format_422, output_format_ycbcr;
206 unsigned int mode;
207 uint8_t infoframe[17];
209 if (adv7511->edid)
210 config.hdmi_mode = drm_detect_hdmi_monitor(adv7511->edid);
211 else
212 config.hdmi_mode = false;
214 hdmi_avi_infoframe_init(&config.avi_infoframe);
216 config.avi_infoframe.scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
218 if (rgb) {
219 config.csc_enable = false;
220 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
221 } else {
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) &&
227 config.hdmi_mode) {
228 config.csc_enable = false;
229 config.avi_infoframe.colorspace =
230 HDMI_COLORSPACE_YUV422;
231 } else {
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;
244 break;
245 case HDMI_COLORSPACE_YUV422:
246 output_format_422 = true;
247 output_format_ycbcr = true;
248 break;
249 default:
250 output_format_422 = false;
251 output_format_ycbcr = false;
252 break;
254 } else {
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,
273 sizeof(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
302 ? 5 : 0;
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;
307 else
308 input_id = config->embedded_sync ? 2 : 1;
310 regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
311 input_id);
312 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
313 (color_depth << 4) |
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);
342 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(1),
343 ADV7511_INT1_DDC_ERROR);
347 * Per spec it is allowed to pulse the HPD signal to indicate that the
348 * EDID information has changed. Some monitors do this when they wakeup
349 * from standby or are enabled. When the HPD goes low the adv7511 is
350 * reset and the outputs are disabled which might cause the monitor to
351 * go to standby again. To avoid this we ignore the HPD pin for the
352 * first few seconds after enabling the output.
354 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
355 ADV7511_REG_POWER2_HPD_SRC_MASK,
356 ADV7511_REG_POWER2_HPD_SRC_NONE);
359 static void adv7511_power_on(struct adv7511 *adv7511)
361 __adv7511_power_on(adv7511);
364 * Most of the registers are reset during power down or when HPD is low.
366 regcache_sync(adv7511->regmap);
368 if (adv7511->type == ADV7533)
369 adv7533_dsi_power_on(adv7511);
370 adv7511->powered = true;
373 static void __adv7511_power_off(struct adv7511 *adv7511)
375 /* TODO: setup additional power down modes */
376 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
377 ADV7511_POWER_POWER_DOWN,
378 ADV7511_POWER_POWER_DOWN);
379 regcache_mark_dirty(adv7511->regmap);
382 static void adv7511_power_off(struct adv7511 *adv7511)
384 __adv7511_power_off(adv7511);
385 if (adv7511->type == ADV7533)
386 adv7533_dsi_power_off(adv7511);
387 adv7511->powered = false;
390 /* -----------------------------------------------------------------------------
391 * Interrupt and hotplug detection
394 static bool adv7511_hpd(struct adv7511 *adv7511)
396 unsigned int irq0;
397 int ret;
399 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
400 if (ret < 0)
401 return false;
403 if (irq0 & ADV7511_INT0_HPD) {
404 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
405 ADV7511_INT0_HPD);
406 return true;
409 return false;
412 static void adv7511_hpd_work(struct work_struct *work)
414 struct adv7511 *adv7511 = container_of(work, struct adv7511, hpd_work);
415 enum drm_connector_status status;
416 unsigned int val;
417 int ret;
419 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
420 if (ret < 0)
421 status = connector_status_disconnected;
422 else if (val & ADV7511_STATUS_HPD)
423 status = connector_status_connected;
424 else
425 status = connector_status_disconnected;
428 * The bridge resets its registers on unplug. So when we get a plug
429 * event and we're already supposed to be powered, cycle the bridge to
430 * restore its state.
432 if (status == connector_status_connected &&
433 adv7511->connector.status == connector_status_disconnected &&
434 adv7511->powered) {
435 regcache_mark_dirty(adv7511->regmap);
436 adv7511_power_on(adv7511);
439 if (adv7511->connector.status != status) {
440 adv7511->connector.status = status;
441 drm_kms_helper_hotplug_event(adv7511->connector.dev);
445 static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
447 unsigned int irq0, irq1;
448 int ret;
450 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
451 if (ret < 0)
452 return ret;
454 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
455 if (ret < 0)
456 return ret;
458 regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
459 regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
461 if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder)
462 schedule_work(&adv7511->hpd_work);
464 if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
465 adv7511->edid_read = true;
467 if (adv7511->i2c_main->irq)
468 wake_up_all(&adv7511->wq);
471 return 0;
474 static irqreturn_t adv7511_irq_handler(int irq, void *devid)
476 struct adv7511 *adv7511 = devid;
477 int ret;
479 ret = adv7511_irq_process(adv7511, true);
480 return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
483 /* -----------------------------------------------------------------------------
484 * EDID retrieval
487 static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
489 int ret;
491 if (adv7511->i2c_main->irq) {
492 ret = wait_event_interruptible_timeout(adv7511->wq,
493 adv7511->edid_read, msecs_to_jiffies(timeout));
494 } else {
495 for (; timeout > 0; timeout -= 25) {
496 ret = adv7511_irq_process(adv7511, false);
497 if (ret < 0)
498 break;
500 if (adv7511->edid_read)
501 break;
503 msleep(25);
507 return adv7511->edid_read ? 0 : -EIO;
510 static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
511 size_t len)
513 struct adv7511 *adv7511 = data;
514 struct i2c_msg xfer[2];
515 uint8_t offset;
516 unsigned int i;
517 int ret;
519 if (len > 128)
520 return -EINVAL;
522 if (adv7511->current_edid_segment != block / 2) {
523 unsigned int status;
525 ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
526 &status);
527 if (ret < 0)
528 return ret;
530 if (status != 2) {
531 adv7511->edid_read = false;
532 regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
533 block);
534 ret = adv7511_wait_for_edid(adv7511, 200);
535 if (ret < 0)
536 return ret;
539 /* Break this apart, hopefully more I2C controllers will
540 * support 64 byte transfers than 256 byte transfers
543 xfer[0].addr = adv7511->i2c_edid->addr;
544 xfer[0].flags = 0;
545 xfer[0].len = 1;
546 xfer[0].buf = &offset;
547 xfer[1].addr = adv7511->i2c_edid->addr;
548 xfer[1].flags = I2C_M_RD;
549 xfer[1].len = 64;
550 xfer[1].buf = adv7511->edid_buf;
552 offset = 0;
554 for (i = 0; i < 4; ++i) {
555 ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
556 ARRAY_SIZE(xfer));
557 if (ret < 0)
558 return ret;
559 else if (ret != 2)
560 return -EIO;
562 xfer[1].buf += 64;
563 offset += 64;
566 adv7511->current_edid_segment = block / 2;
569 if (block % 2 == 0)
570 memcpy(buf, adv7511->edid_buf, len);
571 else
572 memcpy(buf, adv7511->edid_buf + 128, len);
574 return 0;
577 /* -----------------------------------------------------------------------------
578 * ADV75xx helpers
581 static int adv7511_get_modes(struct adv7511 *adv7511,
582 struct drm_connector *connector)
584 struct edid *edid;
585 unsigned int count;
587 /* Reading the EDID only works if the device is powered */
588 if (!adv7511->powered) {
589 unsigned int edid_i2c_addr =
590 (adv7511->i2c_main->addr << 1) + 4;
592 __adv7511_power_on(adv7511);
594 /* Reset the EDID_I2C_ADDR register as it might be cleared */
595 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
596 edid_i2c_addr);
599 edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
601 if (!adv7511->powered)
602 __adv7511_power_off(adv7511);
604 kfree(adv7511->edid);
605 adv7511->edid = edid;
606 if (!edid)
607 return 0;
609 drm_mode_connector_update_edid_property(connector, edid);
610 count = drm_add_edid_modes(connector, edid);
612 adv7511_set_config_csc(adv7511, connector, adv7511->rgb);
614 return count;
617 static enum drm_connector_status
618 adv7511_detect(struct adv7511 *adv7511, struct drm_connector *connector)
620 enum drm_connector_status status;
621 unsigned int val;
622 bool hpd;
623 int ret;
625 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
626 if (ret < 0)
627 return connector_status_disconnected;
629 if (val & ADV7511_STATUS_HPD)
630 status = connector_status_connected;
631 else
632 status = connector_status_disconnected;
634 hpd = adv7511_hpd(adv7511);
636 /* The chip resets itself when the cable is disconnected, so in case
637 * there is a pending HPD interrupt and the cable is connected there was
638 * at least one transition from disconnected to connected and the chip
639 * has to be reinitialized. */
640 if (status == connector_status_connected && hpd && adv7511->powered) {
641 regcache_mark_dirty(adv7511->regmap);
642 adv7511_power_on(adv7511);
643 adv7511_get_modes(adv7511, connector);
644 if (adv7511->status == connector_status_connected)
645 status = connector_status_disconnected;
646 } else {
647 /* Renable HPD sensing */
648 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
649 ADV7511_REG_POWER2_HPD_SRC_MASK,
650 ADV7511_REG_POWER2_HPD_SRC_BOTH);
653 adv7511->status = status;
654 return status;
657 static int adv7511_mode_valid(struct adv7511 *adv7511,
658 struct drm_display_mode *mode)
660 if (mode->clock > 165000)
661 return MODE_CLOCK_HIGH;
663 return MODE_OK;
666 static void adv7511_mode_set(struct adv7511 *adv7511,
667 struct drm_display_mode *mode,
668 struct drm_display_mode *adj_mode)
670 unsigned int low_refresh_rate;
671 unsigned int hsync_polarity = 0;
672 unsigned int vsync_polarity = 0;
674 if (adv7511->embedded_sync) {
675 unsigned int hsync_offset, hsync_len;
676 unsigned int vsync_offset, vsync_len;
678 hsync_offset = adj_mode->crtc_hsync_start -
679 adj_mode->crtc_hdisplay;
680 vsync_offset = adj_mode->crtc_vsync_start -
681 adj_mode->crtc_vdisplay;
682 hsync_len = adj_mode->crtc_hsync_end -
683 adj_mode->crtc_hsync_start;
684 vsync_len = adj_mode->crtc_vsync_end -
685 adj_mode->crtc_vsync_start;
687 /* The hardware vsync generator has a off-by-one bug */
688 vsync_offset += 1;
690 regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
691 ((hsync_offset >> 10) & 0x7) << 5);
692 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
693 (hsync_offset >> 2) & 0xff);
694 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
695 ((hsync_offset & 0x3) << 6) |
696 ((hsync_len >> 4) & 0x3f));
697 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
698 ((hsync_len & 0xf) << 4) |
699 ((vsync_offset >> 6) & 0xf));
700 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
701 ((vsync_offset & 0x3f) << 2) |
702 ((vsync_len >> 8) & 0x3));
703 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
704 vsync_len & 0xff);
706 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
707 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
708 } else {
709 enum adv7511_sync_polarity mode_hsync_polarity;
710 enum adv7511_sync_polarity mode_vsync_polarity;
713 * If the input signal is always low or always high we want to
714 * invert or let it passthrough depending on the polarity of the
715 * current mode.
717 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
718 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
719 else
720 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
722 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
723 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
724 else
725 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
727 if (adv7511->hsync_polarity != mode_hsync_polarity &&
728 adv7511->hsync_polarity !=
729 ADV7511_SYNC_POLARITY_PASSTHROUGH)
730 hsync_polarity = 1;
732 if (adv7511->vsync_polarity != mode_vsync_polarity &&
733 adv7511->vsync_polarity !=
734 ADV7511_SYNC_POLARITY_PASSTHROUGH)
735 vsync_polarity = 1;
738 if (drm_mode_vrefresh(mode) <= 24)
739 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
740 else if (drm_mode_vrefresh(mode) <= 25)
741 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
742 else if (drm_mode_vrefresh(mode) <= 30)
743 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
744 else
745 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
747 regmap_update_bits(adv7511->regmap, 0xfb,
748 0x6, low_refresh_rate << 1);
749 regmap_update_bits(adv7511->regmap, 0x17,
750 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
752 if (adv7511->type == ADV7533)
753 adv7533_mode_set(adv7511, adj_mode);
755 drm_mode_copy(&adv7511->curr_mode, adj_mode);
758 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
759 * supposed to give better results.
762 adv7511->f_tmds = mode->clock;
765 /* Connector funcs */
766 static struct adv7511 *connector_to_adv7511(struct drm_connector *connector)
768 return container_of(connector, struct adv7511, connector);
771 static int adv7511_connector_get_modes(struct drm_connector *connector)
773 struct adv7511 *adv = connector_to_adv7511(connector);
775 return adv7511_get_modes(adv, connector);
778 static enum drm_mode_status
779 adv7511_connector_mode_valid(struct drm_connector *connector,
780 struct drm_display_mode *mode)
782 struct adv7511 *adv = connector_to_adv7511(connector);
784 return adv7511_mode_valid(adv, mode);
787 static struct drm_connector_helper_funcs adv7511_connector_helper_funcs = {
788 .get_modes = adv7511_connector_get_modes,
789 .mode_valid = adv7511_connector_mode_valid,
792 static enum drm_connector_status
793 adv7511_connector_detect(struct drm_connector *connector, bool force)
795 struct adv7511 *adv = connector_to_adv7511(connector);
797 return adv7511_detect(adv, connector);
800 static struct drm_connector_funcs adv7511_connector_funcs = {
801 .dpms = drm_atomic_helper_connector_dpms,
802 .fill_modes = drm_helper_probe_single_connector_modes,
803 .detect = adv7511_connector_detect,
804 .destroy = drm_connector_cleanup,
805 .reset = drm_atomic_helper_connector_reset,
806 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
807 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
810 /* Bridge funcs */
811 static struct adv7511 *bridge_to_adv7511(struct drm_bridge *bridge)
813 return container_of(bridge, struct adv7511, bridge);
816 static void adv7511_bridge_enable(struct drm_bridge *bridge)
818 struct adv7511 *adv = bridge_to_adv7511(bridge);
820 adv7511_power_on(adv);
823 static void adv7511_bridge_disable(struct drm_bridge *bridge)
825 struct adv7511 *adv = bridge_to_adv7511(bridge);
827 adv7511_power_off(adv);
830 static void adv7511_bridge_mode_set(struct drm_bridge *bridge,
831 struct drm_display_mode *mode,
832 struct drm_display_mode *adj_mode)
834 struct adv7511 *adv = bridge_to_adv7511(bridge);
836 adv7511_mode_set(adv, mode, adj_mode);
839 static int adv7511_bridge_attach(struct drm_bridge *bridge)
841 struct adv7511 *adv = bridge_to_adv7511(bridge);
842 int ret;
844 if (!bridge->encoder) {
845 DRM_ERROR("Parent encoder object not found");
846 return -ENODEV;
849 adv->connector.polled = DRM_CONNECTOR_POLL_HPD;
851 ret = drm_connector_init(bridge->dev, &adv->connector,
852 &adv7511_connector_funcs,
853 DRM_MODE_CONNECTOR_HDMIA);
854 if (ret) {
855 DRM_ERROR("Failed to initialize connector with drm\n");
856 return ret;
858 drm_connector_helper_add(&adv->connector,
859 &adv7511_connector_helper_funcs);
860 drm_mode_connector_attach_encoder(&adv->connector, bridge->encoder);
862 if (adv->type == ADV7533)
863 ret = adv7533_attach_dsi(adv);
865 return ret;
868 static struct drm_bridge_funcs adv7511_bridge_funcs = {
869 .enable = adv7511_bridge_enable,
870 .disable = adv7511_bridge_disable,
871 .mode_set = adv7511_bridge_mode_set,
872 .attach = adv7511_bridge_attach,
875 /* -----------------------------------------------------------------------------
876 * Probe & remove
879 static int adv7511_parse_dt(struct device_node *np,
880 struct adv7511_link_config *config)
882 const char *str;
883 int ret;
885 of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
886 if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
887 config->input_color_depth != 12)
888 return -EINVAL;
890 ret = of_property_read_string(np, "adi,input-colorspace", &str);
891 if (ret < 0)
892 return ret;
894 if (!strcmp(str, "rgb"))
895 config->input_colorspace = HDMI_COLORSPACE_RGB;
896 else if (!strcmp(str, "yuv422"))
897 config->input_colorspace = HDMI_COLORSPACE_YUV422;
898 else if (!strcmp(str, "yuv444"))
899 config->input_colorspace = HDMI_COLORSPACE_YUV444;
900 else
901 return -EINVAL;
903 ret = of_property_read_string(np, "adi,input-clock", &str);
904 if (ret < 0)
905 return ret;
907 if (!strcmp(str, "1x"))
908 config->input_clock = ADV7511_INPUT_CLOCK_1X;
909 else if (!strcmp(str, "2x"))
910 config->input_clock = ADV7511_INPUT_CLOCK_2X;
911 else if (!strcmp(str, "ddr"))
912 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
913 else
914 return -EINVAL;
916 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
917 config->input_clock != ADV7511_INPUT_CLOCK_1X) {
918 ret = of_property_read_u32(np, "adi,input-style",
919 &config->input_style);
920 if (ret)
921 return ret;
923 if (config->input_style < 1 || config->input_style > 3)
924 return -EINVAL;
926 ret = of_property_read_string(np, "adi,input-justification",
927 &str);
928 if (ret < 0)
929 return ret;
931 if (!strcmp(str, "left"))
932 config->input_justification =
933 ADV7511_INPUT_JUSTIFICATION_LEFT;
934 else if (!strcmp(str, "evenly"))
935 config->input_justification =
936 ADV7511_INPUT_JUSTIFICATION_EVENLY;
937 else if (!strcmp(str, "right"))
938 config->input_justification =
939 ADV7511_INPUT_JUSTIFICATION_RIGHT;
940 else
941 return -EINVAL;
943 } else {
944 config->input_style = 1;
945 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
948 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
949 if (config->clock_delay < -1200 || config->clock_delay > 1600)
950 return -EINVAL;
952 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
954 /* Hardcode the sync pulse configurations for now. */
955 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
956 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
957 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
959 return 0;
962 static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
964 struct adv7511_link_config link_config;
965 struct adv7511 *adv7511;
966 struct device *dev = &i2c->dev;
967 unsigned int main_i2c_addr = i2c->addr << 1;
968 unsigned int edid_i2c_addr = main_i2c_addr + 4;
969 unsigned int val;
970 int ret;
972 if (!dev->of_node)
973 return -EINVAL;
975 adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
976 if (!adv7511)
977 return -ENOMEM;
979 adv7511->powered = false;
980 adv7511->status = connector_status_disconnected;
982 if (dev->of_node)
983 adv7511->type = (enum adv7511_type)of_device_get_match_data(dev);
984 else
985 adv7511->type = id->driver_data;
987 memset(&link_config, 0, sizeof(link_config));
989 if (adv7511->type == ADV7511)
990 ret = adv7511_parse_dt(dev->of_node, &link_config);
991 else
992 ret = adv7533_parse_dt(dev->of_node, adv7511);
993 if (ret)
994 return ret;
997 * The power down GPIO is optional. If present, toggle it from active to
998 * inactive to wake up the encoder.
1000 adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
1001 if (IS_ERR(adv7511->gpio_pd))
1002 return PTR_ERR(adv7511->gpio_pd);
1004 if (adv7511->gpio_pd) {
1005 mdelay(5);
1006 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
1009 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
1010 if (IS_ERR(adv7511->regmap))
1011 return PTR_ERR(adv7511->regmap);
1013 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
1014 if (ret)
1015 return ret;
1016 dev_dbg(dev, "Rev. %d\n", val);
1018 if (adv7511->type == ADV7511)
1019 ret = regmap_register_patch(adv7511->regmap,
1020 adv7511_fixed_registers,
1021 ARRAY_SIZE(adv7511_fixed_registers));
1022 else
1023 ret = adv7533_patch_registers(adv7511);
1024 if (ret)
1025 return ret;
1027 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
1028 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
1029 main_i2c_addr - 0xa);
1030 regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR,
1031 main_i2c_addr - 2);
1033 adv7511_packet_disable(adv7511, 0xffff);
1035 adv7511->i2c_main = i2c;
1036 adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
1037 if (!adv7511->i2c_edid)
1038 return -ENOMEM;
1040 if (adv7511->type == ADV7533) {
1041 ret = adv7533_init_cec(adv7511);
1042 if (ret)
1043 goto err_i2c_unregister_edid;
1046 INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
1048 if (i2c->irq) {
1049 init_waitqueue_head(&adv7511->wq);
1051 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
1052 adv7511_irq_handler,
1053 IRQF_ONESHOT, dev_name(dev),
1054 adv7511);
1055 if (ret)
1056 goto err_unregister_cec;
1059 /* CEC is unused for now */
1060 regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
1061 ADV7511_CEC_CTRL_POWER_DOWN);
1063 adv7511_power_off(adv7511);
1065 i2c_set_clientdata(i2c, adv7511);
1067 if (adv7511->type == ADV7511)
1068 adv7511_set_link_config(adv7511, &link_config);
1070 adv7511->bridge.funcs = &adv7511_bridge_funcs;
1071 adv7511->bridge.of_node = dev->of_node;
1073 ret = drm_bridge_add(&adv7511->bridge);
1074 if (ret) {
1075 dev_err(dev, "failed to add adv7511 bridge\n");
1076 goto err_unregister_cec;
1079 return 0;
1081 err_unregister_cec:
1082 adv7533_uninit_cec(adv7511);
1083 err_i2c_unregister_edid:
1084 i2c_unregister_device(adv7511->i2c_edid);
1086 return ret;
1089 static int adv7511_remove(struct i2c_client *i2c)
1091 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1093 if (adv7511->type == ADV7533) {
1094 adv7533_detach_dsi(adv7511);
1095 adv7533_uninit_cec(adv7511);
1098 drm_bridge_remove(&adv7511->bridge);
1100 i2c_unregister_device(adv7511->i2c_edid);
1102 kfree(adv7511->edid);
1104 return 0;
1107 static const struct i2c_device_id adv7511_i2c_ids[] = {
1108 { "adv7511", ADV7511 },
1109 { "adv7511w", ADV7511 },
1110 { "adv7513", ADV7511 },
1111 #ifdef CONFIG_DRM_I2C_ADV7533
1112 { "adv7533", ADV7533 },
1113 #endif
1116 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1118 static const struct of_device_id adv7511_of_ids[] = {
1119 { .compatible = "adi,adv7511", .data = (void *)ADV7511 },
1120 { .compatible = "adi,adv7511w", .data = (void *)ADV7511 },
1121 { .compatible = "adi,adv7513", .data = (void *)ADV7511 },
1122 #ifdef CONFIG_DRM_I2C_ADV7533
1123 { .compatible = "adi,adv7533", .data = (void *)ADV7533 },
1124 #endif
1127 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1129 static struct mipi_dsi_driver adv7533_dsi_driver = {
1130 .driver.name = "adv7533",
1133 static struct i2c_driver adv7511_driver = {
1134 .driver = {
1135 .name = "adv7511",
1136 .of_match_table = adv7511_of_ids,
1138 .id_table = adv7511_i2c_ids,
1139 .probe = adv7511_probe,
1140 .remove = adv7511_remove,
1143 static int __init adv7511_init(void)
1145 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1146 mipi_dsi_driver_register(&adv7533_dsi_driver);
1148 return i2c_add_driver(&adv7511_driver);
1150 module_init(adv7511_init);
1152 static void __exit adv7511_exit(void)
1154 i2c_del_driver(&adv7511_driver);
1156 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1157 mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1159 module_exit(adv7511_exit);
1161 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1162 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1163 MODULE_LICENSE("GPL");