]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/gpu/drm/bridge/adv7511/adv7511_drv.c
drm/bridge: adv7511: Fix mutex deadlock when interrupts are disabled
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / bridge / adv7511 / adv7511_drv.c
1 /*
2 * Analog Devices ADV7511 HDMI transmitter driver
3 *
4 * Copyright 2012 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2.
7 */
8
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
16 #include <drm/drmP.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>
21
22 #include "adv7511.h"
23
24 struct adv7511 {
25 struct i2c_client *i2c_main;
26 struct i2c_client *i2c_edid;
27
28 struct regmap *regmap;
29 struct regmap *packet_memory_regmap;
30 enum drm_connector_status status;
31 bool powered;
32
33 unsigned int f_tmds;
34
35 unsigned int current_edid_segment;
36 uint8_t edid_buf[256];
37 bool edid_read;
38
39 wait_queue_head_t wq;
40 struct drm_bridge bridge;
41 struct drm_connector connector;
42
43 bool embedded_sync;
44 enum adv7511_sync_polarity vsync_polarity;
45 enum adv7511_sync_polarity hsync_polarity;
46 bool rgb;
47
48 struct edid *edid;
49
50 struct gpio_desc *gpio_pd;
51 };
52
53 /* ADI recommended values for proper operation. */
54 static const struct reg_sequence adv7511_fixed_registers[] = {
55 { 0x98, 0x03 },
56 { 0x9a, 0xe0 },
57 { 0x9c, 0x30 },
58 { 0x9d, 0x61 },
59 { 0xa2, 0xa4 },
60 { 0xa3, 0xa4 },
61 { 0xe0, 0xd0 },
62 { 0xf9, 0x00 },
63 { 0x55, 0x02 },
64 };
65
66 /* -----------------------------------------------------------------------------
67 * Register access
68 */
69
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,
103 };
104
105 static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
106 {
107 switch (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:
141 return true;
142 }
143
144 return false;
145 }
146
147 static const struct regmap_config adv7511_regmap_config = {
148 .reg_bits = 8,
149 .val_bits = 8,
150
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),
155
156 .volatile_reg = adv7511_register_volatile,
157 };
158
159 /* -----------------------------------------------------------------------------
160 * Hardware configuration
161 */
162
163 static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
164 const uint16_t *coeff,
165 unsigned int scaling_factor)
166 {
167 unsigned int i;
168
169 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
170 ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
171
172 if (enable) {
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),
179 coeff[i] & 0xff);
180 }
181 }
182
183 if (enable)
184 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
185 0xe0, 0x80 | (scaling_factor << 5));
186 else
187 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
188 0x80, 0x00);
189
190 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
191 ADV7511_CSC_UPDATE_MODE, 0);
192 }
193
194 static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
195 {
196 if (packet & 0xff)
197 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
198 packet, 0xff);
199
200 if (packet & 0xff00) {
201 packet >>= 8;
202 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
203 packet, 0xff);
204 }
205
206 return 0;
207 }
208
209 static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
210 {
211 if (packet & 0xff)
212 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
213 packet, 0x00);
214
215 if (packet & 0xff00) {
216 packet >>= 8;
217 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
218 packet, 0x00);
219 }
220
221 return 0;
222 }
223
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,
229 };
230
231 static void adv7511_set_config_csc(struct adv7511 *adv7511,
232 struct drm_connector *connector,
233 bool rgb)
234 {
235 struct adv7511_video_config config;
236 bool output_format_422, output_format_ycbcr;
237 unsigned int mode;
238 uint8_t infoframe[17];
239
240 if (adv7511->edid)
241 config.hdmi_mode = drm_detect_hdmi_monitor(adv7511->edid);
242 else
243 config.hdmi_mode = false;
244
245 hdmi_avi_infoframe_init(&config.avi_infoframe);
246
247 config.avi_infoframe.scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
248
249 if (rgb) {
250 config.csc_enable = false;
251 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
252 } else {
253 config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
254 config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
255
256 if ((connector->display_info.color_formats &
257 DRM_COLOR_FORMAT_YCRCB422) &&
258 config.hdmi_mode) {
259 config.csc_enable = false;
260 config.avi_infoframe.colorspace =
261 HDMI_COLORSPACE_YUV422;
262 } else {
263 config.csc_enable = true;
264 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
265 }
266 }
267
268 if (config.hdmi_mode) {
269 mode = ADV7511_HDMI_CFG_MODE_HDMI;
270
271 switch (config.avi_infoframe.colorspace) {
272 case HDMI_COLORSPACE_YUV444:
273 output_format_422 = false;
274 output_format_ycbcr = true;
275 break;
276 case HDMI_COLORSPACE_YUV422:
277 output_format_422 = true;
278 output_format_ycbcr = true;
279 break;
280 default:
281 output_format_422 = false;
282 output_format_ycbcr = false;
283 break;
284 }
285 } else {
286 mode = ADV7511_HDMI_CFG_MODE_DVI;
287 output_format_422 = false;
288 output_format_ycbcr = false;
289 }
290
291 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
292
293 adv7511_set_colormap(adv7511, config.csc_enable,
294 config.csc_coefficents,
295 config.csc_scaling_factor);
296
297 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
298 (output_format_422 << 7) | output_format_ycbcr);
299
300 regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
301 ADV7511_HDMI_CFG_MODE_MASK, mode);
302
303 hdmi_avi_infoframe_pack(&config.avi_infoframe, infoframe,
304 sizeof(infoframe));
305
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);
309
310 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
311 }
312
313 static void adv7511_set_link_config(struct adv7511 *adv7511,
314 const struct adv7511_link_config *config)
315 {
316 /*
317 * The input style values documented in the datasheet don't match the
318 * hardware register field values :-(
319 */
320 static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
321
322 unsigned int clock_delay;
323 unsigned int color_depth;
324 unsigned int input_id;
325
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);
329
330 /* TODO Support input ID 6 */
331 if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
332 input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
333 ? 5 : 0;
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;
338 else
339 input_id = config->embedded_sync ? 2 : 1;
340
341 regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
342 input_id);
343 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
344 (color_depth << 4) |
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);
350
351 regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
352
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;
357 }
358
359 static void adv7511_power_on(struct adv7511 *adv7511)
360 {
361 adv7511->current_edid_segment = -1;
362
363 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
364 ADV7511_POWER_POWER_DOWN, 0);
365 if (adv7511->i2c_main->irq) {
366 /*
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.
370 */
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);
375 }
376
377 /*
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.
384 */
385 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
386 ADV7511_REG_POWER2_HPD_SRC_MASK,
387 ADV7511_REG_POWER2_HPD_SRC_NONE);
388
389 /*
390 * Most of the registers are reset during power down or when HPD is low.
391 */
392 regcache_sync(adv7511->regmap);
393
394 adv7511->powered = true;
395 }
396
397 static void adv7511_power_off(struct adv7511 *adv7511)
398 {
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);
404
405 adv7511->powered = false;
406 }
407
408 /* -----------------------------------------------------------------------------
409 * Interrupt and hotplug detection
410 */
411
412 static bool adv7511_hpd(struct adv7511 *adv7511)
413 {
414 unsigned int irq0;
415 int ret;
416
417 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
418 if (ret < 0)
419 return false;
420
421 if (irq0 & ADV7511_INT0_HPD) {
422 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
423 ADV7511_INT0_HPD);
424 return true;
425 }
426
427 return false;
428 }
429
430 static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
431 {
432 unsigned int irq0, irq1;
433 int ret;
434
435 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
436 if (ret < 0)
437 return ret;
438
439 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
440 if (ret < 0)
441 return ret;
442
443 regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
444 regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
445
446 if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder)
447 drm_helper_hpd_irq_event(adv7511->connector.dev);
448
449 if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
450 adv7511->edid_read = true;
451
452 if (adv7511->i2c_main->irq)
453 wake_up_all(&adv7511->wq);
454 }
455
456 return 0;
457 }
458
459 static irqreturn_t adv7511_irq_handler(int irq, void *devid)
460 {
461 struct adv7511 *adv7511 = devid;
462 int ret;
463
464 ret = adv7511_irq_process(adv7511, true);
465 return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
466 }
467
468 /* -----------------------------------------------------------------------------
469 * EDID retrieval
470 */
471
472 static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
473 {
474 int ret;
475
476 if (adv7511->i2c_main->irq) {
477 ret = wait_event_interruptible_timeout(adv7511->wq,
478 adv7511->edid_read, msecs_to_jiffies(timeout));
479 } else {
480 for (; timeout > 0; timeout -= 25) {
481 ret = adv7511_irq_process(adv7511, false);
482 if (ret < 0)
483 break;
484
485 if (adv7511->edid_read)
486 break;
487
488 msleep(25);
489 }
490 }
491
492 return adv7511->edid_read ? 0 : -EIO;
493 }
494
495 static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
496 size_t len)
497 {
498 struct adv7511 *adv7511 = data;
499 struct i2c_msg xfer[2];
500 uint8_t offset;
501 unsigned int i;
502 int ret;
503
504 if (len > 128)
505 return -EINVAL;
506
507 if (adv7511->current_edid_segment != block / 2) {
508 unsigned int status;
509
510 ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
511 &status);
512 if (ret < 0)
513 return ret;
514
515 if (status != 2) {
516 adv7511->edid_read = false;
517 regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
518 block);
519 ret = adv7511_wait_for_edid(adv7511, 200);
520 if (ret < 0)
521 return ret;
522 }
523
524 /* Break this apart, hopefully more I2C controllers will
525 * support 64 byte transfers than 256 byte transfers
526 */
527
528 xfer[0].addr = adv7511->i2c_edid->addr;
529 xfer[0].flags = 0;
530 xfer[0].len = 1;
531 xfer[0].buf = &offset;
532 xfer[1].addr = adv7511->i2c_edid->addr;
533 xfer[1].flags = I2C_M_RD;
534 xfer[1].len = 64;
535 xfer[1].buf = adv7511->edid_buf;
536
537 offset = 0;
538
539 for (i = 0; i < 4; ++i) {
540 ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
541 ARRAY_SIZE(xfer));
542 if (ret < 0)
543 return ret;
544 else if (ret != 2)
545 return -EIO;
546
547 xfer[1].buf += 64;
548 offset += 64;
549 }
550
551 adv7511->current_edid_segment = block / 2;
552 }
553
554 if (block % 2 == 0)
555 memcpy(buf, adv7511->edid_buf, len);
556 else
557 memcpy(buf, adv7511->edid_buf + 128, len);
558
559 return 0;
560 }
561
562 /* -----------------------------------------------------------------------------
563 * ADV75xx helpers
564 */
565
566 static int adv7511_get_modes(struct adv7511 *adv7511,
567 struct drm_connector *connector)
568 {
569 struct edid *edid;
570 unsigned int count;
571
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);
581 }
582 adv7511->current_edid_segment = -1;
583 }
584
585 edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
586
587 if (!adv7511->powered)
588 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
589 ADV7511_POWER_POWER_DOWN,
590 ADV7511_POWER_POWER_DOWN);
591
592 kfree(adv7511->edid);
593 adv7511->edid = edid;
594 if (!edid)
595 return 0;
596
597 drm_mode_connector_update_edid_property(connector, edid);
598 count = drm_add_edid_modes(connector, edid);
599
600 adv7511_set_config_csc(adv7511, connector, adv7511->rgb);
601
602 return count;
603 }
604
605 static enum drm_connector_status
606 adv7511_detect(struct adv7511 *adv7511, struct drm_connector *connector)
607 {
608 enum drm_connector_status status;
609 unsigned int val;
610 bool hpd;
611 int ret;
612
613 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
614 if (ret < 0)
615 return connector_status_disconnected;
616
617 if (val & ADV7511_STATUS_HPD)
618 status = connector_status_connected;
619 else
620 status = connector_status_disconnected;
621
622 hpd = adv7511_hpd(adv7511);
623
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;
634 } else {
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);
639 }
640
641 adv7511->status = status;
642 return status;
643 }
644
645 static int adv7511_mode_valid(struct adv7511 *adv7511,
646 struct drm_display_mode *mode)
647 {
648 if (mode->clock > 165000)
649 return MODE_CLOCK_HIGH;
650
651 return MODE_OK;
652 }
653
654 static void adv7511_mode_set(struct adv7511 *adv7511,
655 struct drm_display_mode *mode,
656 struct drm_display_mode *adj_mode)
657 {
658 unsigned int low_refresh_rate;
659 unsigned int hsync_polarity = 0;
660 unsigned int vsync_polarity = 0;
661
662 if (adv7511->embedded_sync) {
663 unsigned int hsync_offset, hsync_len;
664 unsigned int vsync_offset, vsync_len;
665
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;
674
675 /* The hardware vsync generator has a off-by-one bug */
676 vsync_offset += 1;
677
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),
692 vsync_len & 0xff);
693
694 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
695 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
696 } else {
697 enum adv7511_sync_polarity mode_hsync_polarity;
698 enum adv7511_sync_polarity mode_vsync_polarity;
699
700 /**
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
703 * current mode.
704 **/
705 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
706 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
707 else
708 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
709
710 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
711 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
712 else
713 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
714
715 if (adv7511->hsync_polarity != mode_hsync_polarity &&
716 adv7511->hsync_polarity !=
717 ADV7511_SYNC_POLARITY_PASSTHROUGH)
718 hsync_polarity = 1;
719
720 if (adv7511->vsync_polarity != mode_vsync_polarity &&
721 adv7511->vsync_polarity !=
722 ADV7511_SYNC_POLARITY_PASSTHROUGH)
723 vsync_polarity = 1;
724 }
725
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;
732 else
733 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
734
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));
739
740 /*
741 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
742 * supposed to give better results.
743 */
744
745 adv7511->f_tmds = mode->clock;
746 }
747
748 /* Connector funcs */
749 static struct adv7511 *connector_to_adv7511(struct drm_connector *connector)
750 {
751 return container_of(connector, struct adv7511, connector);
752 }
753
754 static int adv7511_connector_get_modes(struct drm_connector *connector)
755 {
756 struct adv7511 *adv = connector_to_adv7511(connector);
757
758 return adv7511_get_modes(adv, connector);
759 }
760
761 static enum drm_mode_status
762 adv7511_connector_mode_valid(struct drm_connector *connector,
763 struct drm_display_mode *mode)
764 {
765 struct adv7511 *adv = connector_to_adv7511(connector);
766
767 return adv7511_mode_valid(adv, mode);
768 }
769
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,
773 };
774
775 static enum drm_connector_status
776 adv7511_connector_detect(struct drm_connector *connector, bool force)
777 {
778 struct adv7511 *adv = connector_to_adv7511(connector);
779
780 return adv7511_detect(adv, connector);
781 }
782
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,
791 };
792
793 /* Bridge funcs */
794 static struct adv7511 *bridge_to_adv7511(struct drm_bridge *bridge)
795 {
796 return container_of(bridge, struct adv7511, bridge);
797 }
798
799 static void adv7511_bridge_enable(struct drm_bridge *bridge)
800 {
801 struct adv7511 *adv = bridge_to_adv7511(bridge);
802
803 adv7511_power_on(adv);
804 }
805
806 static void adv7511_bridge_disable(struct drm_bridge *bridge)
807 {
808 struct adv7511 *adv = bridge_to_adv7511(bridge);
809
810 adv7511_power_off(adv);
811 }
812
813 static void adv7511_bridge_mode_set(struct drm_bridge *bridge,
814 struct drm_display_mode *mode,
815 struct drm_display_mode *adj_mode)
816 {
817 struct adv7511 *adv = bridge_to_adv7511(bridge);
818
819 adv7511_mode_set(adv, mode, adj_mode);
820 }
821
822 static int adv7511_bridge_attach(struct drm_bridge *bridge)
823 {
824 struct adv7511 *adv = bridge_to_adv7511(bridge);
825 int ret;
826
827 if (!bridge->encoder) {
828 DRM_ERROR("Parent encoder object not found");
829 return -ENODEV;
830 }
831
832 adv->connector.polled = DRM_CONNECTOR_POLL_HPD;
833
834 ret = drm_connector_init(bridge->dev, &adv->connector,
835 &adv7511_connector_funcs,
836 DRM_MODE_CONNECTOR_HDMIA);
837 if (ret) {
838 DRM_ERROR("Failed to initialize connector with drm\n");
839 return ret;
840 }
841 drm_connector_helper_add(&adv->connector,
842 &adv7511_connector_helper_funcs);
843 drm_mode_connector_attach_encoder(&adv->connector, bridge->encoder);
844
845 return ret;
846 }
847
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,
853 };
854
855 /* -----------------------------------------------------------------------------
856 * Probe & remove
857 */
858
859 static int adv7511_parse_dt(struct device_node *np,
860 struct adv7511_link_config *config)
861 {
862 const char *str;
863 int ret;
864
865 memset(config, 0, sizeof(*config));
866
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)
870 return -EINVAL;
871
872 ret = of_property_read_string(np, "adi,input-colorspace", &str);
873 if (ret < 0)
874 return ret;
875
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;
882 else
883 return -EINVAL;
884
885 ret = of_property_read_string(np, "adi,input-clock", &str);
886 if (ret < 0)
887 return ret;
888
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;
895 else
896 return -EINVAL;
897
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);
902 if (ret)
903 return ret;
904
905 if (config->input_style < 1 || config->input_style > 3)
906 return -EINVAL;
907
908 ret = of_property_read_string(np, "adi,input-justification",
909 &str);
910 if (ret < 0)
911 return ret;
912
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;
922 else
923 return -EINVAL;
924
925 } else {
926 config->input_style = 1;
927 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
928 }
929
930 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
931 if (config->clock_delay < -1200 || config->clock_delay > 1600)
932 return -EINVAL;
933
934 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
935
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;
940
941 return 0;
942 }
943
944 static const int edid_i2c_addr = 0x7e;
945 static const int packet_i2c_addr = 0x70;
946 static const int cec_i2c_addr = 0x78;
947
948 static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
949 {
950 struct adv7511_link_config link_config;
951 struct adv7511 *adv7511;
952 struct device *dev = &i2c->dev;
953 unsigned int val;
954 int ret;
955
956 if (!dev->of_node)
957 return -EINVAL;
958
959 adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
960 if (!adv7511)
961 return -ENOMEM;
962
963 adv7511->powered = false;
964 adv7511->status = connector_status_disconnected;
965
966 ret = adv7511_parse_dt(dev->of_node, &link_config);
967 if (ret)
968 return ret;
969
970 /*
971 * The power down GPIO is optional. If present, toggle it from active to
972 * inactive to wake up the encoder.
973 */
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);
977
978 if (adv7511->gpio_pd) {
979 mdelay(5);
980 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
981 }
982
983 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
984 if (IS_ERR(adv7511->regmap))
985 return PTR_ERR(adv7511->regmap);
986
987 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
988 if (ret)
989 return ret;
990 dev_dbg(dev, "Rev. %d\n", val);
991
992 ret = regmap_register_patch(adv7511->regmap, adv7511_fixed_registers,
993 ARRAY_SIZE(adv7511_fixed_registers));
994 if (ret)
995 return ret;
996
997 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
998 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
999 packet_i2c_addr);
1000 regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR, cec_i2c_addr);
1001 adv7511_packet_disable(adv7511, 0xffff);
1002
1003 adv7511->i2c_main = i2c;
1004 adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
1005 if (!adv7511->i2c_edid)
1006 return -ENOMEM;
1007
1008 if (i2c->irq) {
1009 init_waitqueue_head(&adv7511->wq);
1010
1011 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
1012 adv7511_irq_handler,
1013 IRQF_ONESHOT, dev_name(dev),
1014 adv7511);
1015 if (ret)
1016 goto err_i2c_unregister_device;
1017 }
1018
1019 /* CEC is unused for now */
1020 regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
1021 ADV7511_CEC_CTRL_POWER_DOWN);
1022
1023 adv7511_power_off(adv7511);
1024
1025 i2c_set_clientdata(i2c, adv7511);
1026
1027 adv7511_set_link_config(adv7511, &link_config);
1028
1029 adv7511->bridge.funcs = &adv7511_bridge_funcs;
1030 adv7511->bridge.of_node = dev->of_node;
1031
1032 ret = drm_bridge_add(&adv7511->bridge);
1033 if (ret) {
1034 dev_err(dev, "failed to add adv7511 bridge\n");
1035 goto err_i2c_unregister_device;
1036 }
1037
1038 return 0;
1039
1040 err_i2c_unregister_device:
1041 i2c_unregister_device(adv7511->i2c_edid);
1042
1043 return ret;
1044 }
1045
1046 static int adv7511_remove(struct i2c_client *i2c)
1047 {
1048 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1049
1050 drm_bridge_remove(&adv7511->bridge);
1051
1052 i2c_unregister_device(adv7511->i2c_edid);
1053
1054 kfree(adv7511->edid);
1055
1056 return 0;
1057 }
1058
1059 static const struct i2c_device_id adv7511_i2c_ids[] = {
1060 { "adv7511", 0 },
1061 { "adv7511w", 0 },
1062 { "adv7513", 0 },
1063 { }
1064 };
1065 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1066
1067 static const struct of_device_id adv7511_of_ids[] = {
1068 { .compatible = "adi,adv7511", },
1069 { .compatible = "adi,adv7511w", },
1070 { .compatible = "adi,adv7513", },
1071 { }
1072 };
1073 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1074
1075 static struct i2c_driver adv7511_driver = {
1076 .driver = {
1077 .name = "adv7511",
1078 .of_match_table = adv7511_of_ids,
1079 },
1080 .id_table = adv7511_i2c_ids,
1081 .probe = adv7511_probe,
1082 .remove = adv7511_remove,
1083 };
1084
1085 module_i2c_driver(adv7511_driver);
1086
1087 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1088 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1089 MODULE_LICENSE("GPL");