blob: faf1c0c5ab2eb9300b9acef0a337bfd7bc8a0d64 [file] [log] [blame]
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001/*
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_crtc_helper.h>
18#include <drm/drm_edid.h>
19#include <drm/drm_encoder_slave.h>
20
21#include "adv7511.h"
22
23struct adv7511 {
24 struct i2c_client *i2c_main;
25 struct i2c_client *i2c_edid;
26
27 struct regmap *regmap;
28 struct regmap *packet_memory_regmap;
29 enum drm_connector_status status;
30 int dpms_mode;
31
32 unsigned int f_tmds;
33
34 unsigned int current_edid_segment;
35 uint8_t edid_buf[256];
36
37 wait_queue_head_t wq;
38 struct drm_encoder *encoder;
39
40 bool embedded_sync;
41 enum adv7511_sync_polarity vsync_polarity;
42 enum adv7511_sync_polarity hsync_polarity;
43 bool rgb;
44
45 struct edid *edid;
46
47 struct gpio_desc *gpio_pd;
48};
49
50static struct adv7511 *encoder_to_adv7511(struct drm_encoder *encoder)
51{
52 return to_encoder_slave(encoder)->slave_priv;
53}
54
55/* ADI recommended values for proper operation. */
56static const struct reg_default adv7511_fixed_registers[] = {
57 { 0x98, 0x03 },
58 { 0x9a, 0xe0 },
59 { 0x9c, 0x30 },
60 { 0x9d, 0x61 },
61 { 0xa2, 0xa4 },
62 { 0xa3, 0xa4 },
63 { 0xe0, 0xd0 },
64 { 0xf9, 0x00 },
65 { 0x55, 0x02 },
66};
67
68/* -----------------------------------------------------------------------------
69 * Register access
70 */
71
72static const uint8_t adv7511_register_defaults[] = {
73 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
74 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
75 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
76 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
77 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
78 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
80 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
81 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
82 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
88 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
90 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
92 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
93 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
94 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
95 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
98 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
99 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
101 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
104 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105};
106
107static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
108{
109 switch (reg) {
110 case ADV7511_REG_CHIP_REVISION:
111 case ADV7511_REG_SPDIF_FREQ:
112 case ADV7511_REG_CTS_AUTOMATIC1:
113 case ADV7511_REG_CTS_AUTOMATIC2:
114 case ADV7511_REG_VIC_DETECTED:
115 case ADV7511_REG_VIC_SEND:
116 case ADV7511_REG_AUX_VIC_DETECTED:
117 case ADV7511_REG_STATUS:
118 case ADV7511_REG_GC(1):
119 case ADV7511_REG_INT(0):
120 case ADV7511_REG_INT(1):
121 case ADV7511_REG_PLL_STATUS:
122 case ADV7511_REG_AN(0):
123 case ADV7511_REG_AN(1):
124 case ADV7511_REG_AN(2):
125 case ADV7511_REG_AN(3):
126 case ADV7511_REG_AN(4):
127 case ADV7511_REG_AN(5):
128 case ADV7511_REG_AN(6):
129 case ADV7511_REG_AN(7):
130 case ADV7511_REG_HDCP_STATUS:
131 case ADV7511_REG_BCAPS:
132 case ADV7511_REG_BKSV(0):
133 case ADV7511_REG_BKSV(1):
134 case ADV7511_REG_BKSV(2):
135 case ADV7511_REG_BKSV(3):
136 case ADV7511_REG_BKSV(4):
137 case ADV7511_REG_DDC_STATUS:
138 case ADV7511_REG_BSTATUS(0):
139 case ADV7511_REG_BSTATUS(1):
140 case ADV7511_REG_CHIP_ID_HIGH:
141 case ADV7511_REG_CHIP_ID_LOW:
142 return true;
143 }
144
145 return false;
146}
147
148static const struct regmap_config adv7511_regmap_config = {
149 .reg_bits = 8,
150 .val_bits = 8,
151
152 .max_register = 0xff,
153 .cache_type = REGCACHE_RBTREE,
154 .reg_defaults_raw = adv7511_register_defaults,
155 .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
156
157 .volatile_reg = adv7511_register_volatile,
158};
159
160/* -----------------------------------------------------------------------------
161 * Hardware configuration
162 */
163
164static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
165 const uint16_t *coeff,
166 unsigned int scaling_factor)
167{
168 unsigned int i;
169
170 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
171 ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
172
173 if (enable) {
174 for (i = 0; i < 12; ++i) {
175 regmap_update_bits(adv7511->regmap,
176 ADV7511_REG_CSC_UPPER(i),
177 0x1f, coeff[i] >> 8);
178 regmap_write(adv7511->regmap,
179 ADV7511_REG_CSC_LOWER(i),
180 coeff[i] & 0xff);
181 }
182 }
183
184 if (enable)
185 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
186 0xe0, 0x80 | (scaling_factor << 5));
187 else
188 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
189 0x80, 0x00);
190
191 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
192 ADV7511_CSC_UPDATE_MODE, 0);
193}
194
195static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
196{
197 if (packet & 0xff)
198 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
199 packet, 0xff);
200
201 if (packet & 0xff00) {
202 packet >>= 8;
203 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
204 packet, 0xff);
205 }
206
207 return 0;
208}
209
210static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
211{
212 if (packet & 0xff)
213 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
214 packet, 0x00);
215
216 if (packet & 0xff00) {
217 packet >>= 8;
218 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
219 packet, 0x00);
220 }
221
222 return 0;
223}
224
225/* Coefficients for adv7511 color space conversion */
226static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
227 0x0734, 0x04ad, 0x0000, 0x1c1b,
228 0x1ddc, 0x04ad, 0x1f24, 0x0135,
229 0x0000, 0x04ad, 0x087c, 0x1b77,
230};
231
232static void adv7511_set_config_csc(struct adv7511 *adv7511,
233 struct drm_connector *connector,
234 bool rgb)
235{
236 struct adv7511_video_config config;
237 bool output_format_422, output_format_ycbcr;
238 unsigned int mode;
239 uint8_t infoframe[17];
240
241 if (adv7511->edid)
242 config.hdmi_mode = drm_detect_hdmi_monitor(adv7511->edid);
243 else
244 config.hdmi_mode = false;
245
246 hdmi_avi_infoframe_init(&config.avi_infoframe);
247
248 config.avi_infoframe.scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
249
250 if (rgb) {
251 config.csc_enable = false;
252 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
253 } else {
254 config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
255 config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
256
257 if ((connector->display_info.color_formats &
258 DRM_COLOR_FORMAT_YCRCB422) &&
259 config.hdmi_mode) {
260 config.csc_enable = false;
261 config.avi_infoframe.colorspace =
262 HDMI_COLORSPACE_YUV422;
263 } else {
264 config.csc_enable = true;
265 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
266 }
267 }
268
269 if (config.hdmi_mode) {
270 mode = ADV7511_HDMI_CFG_MODE_HDMI;
271
272 switch (config.avi_infoframe.colorspace) {
273 case HDMI_COLORSPACE_YUV444:
274 output_format_422 = false;
275 output_format_ycbcr = true;
276 break;
277 case HDMI_COLORSPACE_YUV422:
278 output_format_422 = true;
279 output_format_ycbcr = true;
280 break;
281 default:
282 output_format_422 = false;
283 output_format_ycbcr = false;
284 break;
285 }
286 } else {
287 mode = ADV7511_HDMI_CFG_MODE_DVI;
288 output_format_422 = false;
289 output_format_ycbcr = false;
290 }
291
292 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
293
294 adv7511_set_colormap(adv7511, config.csc_enable,
295 config.csc_coefficents,
296 config.csc_scaling_factor);
297
298 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
299 (output_format_422 << 7) | output_format_ycbcr);
300
301 regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
302 ADV7511_HDMI_CFG_MODE_MASK, mode);
303
304 hdmi_avi_infoframe_pack(&config.avi_infoframe, infoframe,
305 sizeof(infoframe));
306
307 /* The AVI infoframe id is not configurable */
308 regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
309 infoframe + 1, sizeof(infoframe) - 1);
310
311 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
312}
313
314static void adv7511_set_link_config(struct adv7511 *adv7511,
315 const struct adv7511_link_config *config)
316{
317 /*
318 * The input style values documented in the datasheet don't match the
319 * hardware register field values :-(
320 */
321 static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
322
323 unsigned int clock_delay;
324 unsigned int color_depth;
325 unsigned int input_id;
326
327 clock_delay = (config->clock_delay + 1200) / 400;
328 color_depth = config->input_color_depth == 8 ? 3
329 : (config->input_color_depth == 10 ? 1 : 2);
330
331 /* TODO Support input ID 6 */
332 if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
333 input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
334 ? 5 : 0;
335 else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
336 input_id = config->embedded_sync ? 8 : 7;
337 else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
338 input_id = config->embedded_sync ? 4 : 3;
339 else
340 input_id = config->embedded_sync ? 2 : 1;
341
342 regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
343 input_id);
344 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
345 (color_depth << 4) |
346 (input_styles[config->input_style] << 2));
347 regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
348 config->input_justification << 3);
349 regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
350 config->sync_pulse << 2);
351
352 regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
353
354 adv7511->embedded_sync = config->embedded_sync;
355 adv7511->hsync_polarity = config->hsync_polarity;
356 adv7511->vsync_polarity = config->vsync_polarity;
357 adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
358}
359
360/* -----------------------------------------------------------------------------
361 * Interrupt and hotplug detection
362 */
363
364static bool adv7511_hpd(struct adv7511 *adv7511)
365{
366 unsigned int irq0;
367 int ret;
368
369 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
370 if (ret < 0)
371 return false;
372
373 if (irq0 & ADV7511_INT0_HDP) {
374 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
375 ADV7511_INT0_HDP);
376 return true;
377 }
378
379 return false;
380}
381
382static irqreturn_t adv7511_irq_handler(int irq, void *devid)
383{
384 struct adv7511 *adv7511 = devid;
385
386 if (adv7511_hpd(adv7511))
387 drm_helper_hpd_irq_event(adv7511->encoder->dev);
388
389 wake_up_all(&adv7511->wq);
390
391 return IRQ_HANDLED;
392}
393
394static unsigned int adv7511_is_interrupt_pending(struct adv7511 *adv7511,
395 unsigned int irq)
396{
397 unsigned int irq0, irq1;
398 unsigned int pending;
399 int ret;
400
401 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
402 if (ret < 0)
403 return 0;
404 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
405 if (ret < 0)
406 return 0;
407
408 pending = (irq1 << 8) | irq0;
409
410 return pending & irq;
411}
412
413static int adv7511_wait_for_interrupt(struct adv7511 *adv7511, int irq,
414 int timeout)
415{
416 unsigned int pending;
417 int ret;
418
419 if (adv7511->i2c_main->irq) {
420 ret = wait_event_interruptible_timeout(adv7511->wq,
421 adv7511_is_interrupt_pending(adv7511, irq),
422 msecs_to_jiffies(timeout));
423 if (ret <= 0)
424 return 0;
425 pending = adv7511_is_interrupt_pending(adv7511, irq);
426 } else {
427 if (timeout < 25)
428 timeout = 25;
429 do {
430 pending = adv7511_is_interrupt_pending(adv7511, irq);
431 if (pending)
432 break;
433 msleep(25);
434 timeout -= 25;
435 } while (timeout >= 25);
436 }
437
438 return pending;
439}
440
441/* -----------------------------------------------------------------------------
442 * EDID retrieval
443 */
444
445static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
446 size_t len)
447{
448 struct adv7511 *adv7511 = data;
449 struct i2c_msg xfer[2];
450 uint8_t offset;
451 unsigned int i;
452 int ret;
453
454 if (len > 128)
455 return -EINVAL;
456
457 if (adv7511->current_edid_segment != block / 2) {
458 unsigned int status;
459
460 ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
461 &status);
462 if (ret < 0)
463 return ret;
464
465 if (status != 2) {
466 regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
467 block);
468 ret = adv7511_wait_for_interrupt(adv7511,
469 ADV7511_INT0_EDID_READY |
470 ADV7511_INT1_DDC_ERROR, 200);
471
472 if (!(ret & ADV7511_INT0_EDID_READY))
473 return -EIO;
474 }
475
476 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
477 ADV7511_INT0_EDID_READY | ADV7511_INT1_DDC_ERROR);
478
479 /* Break this apart, hopefully more I2C controllers will
480 * support 64 byte transfers than 256 byte transfers
481 */
482
483 xfer[0].addr = adv7511->i2c_edid->addr;
484 xfer[0].flags = 0;
485 xfer[0].len = 1;
486 xfer[0].buf = &offset;
487 xfer[1].addr = adv7511->i2c_edid->addr;
488 xfer[1].flags = I2C_M_RD;
489 xfer[1].len = 64;
490 xfer[1].buf = adv7511->edid_buf;
491
492 offset = 0;
493
494 for (i = 0; i < 4; ++i) {
495 ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
496 ARRAY_SIZE(xfer));
497 if (ret < 0)
498 return ret;
499 else if (ret != 2)
500 return -EIO;
501
502 xfer[1].buf += 64;
503 offset += 64;
504 }
505
506 adv7511->current_edid_segment = block / 2;
507 }
508
509 if (block % 2 == 0)
510 memcpy(buf, adv7511->edid_buf, len);
511 else
512 memcpy(buf, adv7511->edid_buf + 128, len);
513
514 return 0;
515}
516
517/* -----------------------------------------------------------------------------
518 * Encoder operations
519 */
520
521static int adv7511_get_modes(struct drm_encoder *encoder,
522 struct drm_connector *connector)
523{
524 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
525 struct edid *edid;
526 unsigned int count;
527
528 /* Reading the EDID only works if the device is powered */
529 if (adv7511->dpms_mode != DRM_MODE_DPMS_ON) {
530 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
531 ADV7511_INT0_EDID_READY | ADV7511_INT1_DDC_ERROR);
532 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
533 ADV7511_POWER_POWER_DOWN, 0);
534 adv7511->current_edid_segment = -1;
535 }
536
537 edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
538
539 if (adv7511->dpms_mode != DRM_MODE_DPMS_ON)
540 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
541 ADV7511_POWER_POWER_DOWN,
542 ADV7511_POWER_POWER_DOWN);
543
544 kfree(adv7511->edid);
545 adv7511->edid = edid;
546 if (!edid)
547 return 0;
548
549 drm_mode_connector_update_edid_property(connector, edid);
550 count = drm_add_edid_modes(connector, edid);
551
552 adv7511_set_config_csc(adv7511, connector, adv7511->rgb);
553
554 return count;
555}
556
557static void adv7511_encoder_dpms(struct drm_encoder *encoder, int mode)
558{
559 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
560
561 switch (mode) {
562 case DRM_MODE_DPMS_ON:
563 adv7511->current_edid_segment = -1;
564
565 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
566 ADV7511_INT0_EDID_READY | ADV7511_INT1_DDC_ERROR);
567 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
568 ADV7511_POWER_POWER_DOWN, 0);
569 /*
570 * Per spec it is allowed to pulse the HDP signal to indicate
571 * that the EDID information has changed. Some monitors do this
572 * when they wakeup from standby or are enabled. When the HDP
573 * goes low the adv7511 is reset and the outputs are disabled
574 * which might cause the monitor to go to standby again. To
575 * avoid this we ignore the HDP pin for the first few seconds
576 * after enabeling the output.
577 */
578 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
579 ADV7511_REG_POWER2_HDP_SRC_MASK,
580 ADV7511_REG_POWER2_HDP_SRC_NONE);
581 /* Most of the registers are reset during power down or
582 * when HPD is low
583 */
584 regcache_sync(adv7511->regmap);
585 break;
586 default:
587 /* TODO: setup additional power down modes */
588 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
589 ADV7511_POWER_POWER_DOWN,
590 ADV7511_POWER_POWER_DOWN);
591 regcache_mark_dirty(adv7511->regmap);
592 break;
593 }
594
595 adv7511->dpms_mode = mode;
596}
597
598static enum drm_connector_status
599adv7511_encoder_detect(struct drm_encoder *encoder,
600 struct drm_connector *connector)
601{
602 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
603 enum drm_connector_status status;
604 unsigned int val;
605 bool hpd;
606 int ret;
607
608 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
609 if (ret < 0)
610 return connector_status_disconnected;
611
612 if (val & ADV7511_STATUS_HPD)
613 status = connector_status_connected;
614 else
615 status = connector_status_disconnected;
616
617 hpd = adv7511_hpd(adv7511);
618
619 /* The chip resets itself when the cable is disconnected, so in case
620 * there is a pending HPD interrupt and the cable is connected there was
621 * at least one transition from disconnected to connected and the chip
622 * has to be reinitialized. */
623 if (status == connector_status_connected && hpd &&
624 adv7511->dpms_mode == DRM_MODE_DPMS_ON) {
625 regcache_mark_dirty(adv7511->regmap);
626 adv7511_encoder_dpms(encoder, adv7511->dpms_mode);
627 adv7511_get_modes(encoder, connector);
628 if (adv7511->status == connector_status_connected)
629 status = connector_status_disconnected;
630 } else {
631 /* Renable HDP sensing */
632 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
633 ADV7511_REG_POWER2_HDP_SRC_MASK,
634 ADV7511_REG_POWER2_HDP_SRC_BOTH);
635 }
636
637 adv7511->status = status;
638 return status;
639}
640
641static int adv7511_encoder_mode_valid(struct drm_encoder *encoder,
642 struct drm_display_mode *mode)
643{
644 if (mode->clock > 165000)
645 return MODE_CLOCK_HIGH;
646
647 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
648 return MODE_NO_INTERLACE;
649
650 return MODE_OK;
651}
652
653static void adv7511_encoder_mode_set(struct drm_encoder *encoder,
654 struct drm_display_mode *mode,
655 struct drm_display_mode *adj_mode)
656{
657 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
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
748static struct drm_encoder_slave_funcs adv7511_encoder_funcs = {
749 .dpms = adv7511_encoder_dpms,
750 .mode_valid = adv7511_encoder_mode_valid,
751 .mode_set = adv7511_encoder_mode_set,
752 .detect = adv7511_encoder_detect,
753 .get_modes = adv7511_get_modes,
754};
755
756/* -----------------------------------------------------------------------------
757 * Probe & remove
758 */
759
760static int adv7511_parse_dt(struct device_node *np,
761 struct adv7511_link_config *config)
762{
763 const char *str;
764 int ret;
765
766 memset(config, 0, sizeof(*config));
767
768 of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
769 if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
770 config->input_color_depth != 12)
771 return -EINVAL;
772
773 ret = of_property_read_string(np, "adi,input-colorspace", &str);
774 if (ret < 0)
775 return ret;
776
777 if (!strcmp(str, "rgb"))
778 config->input_colorspace = HDMI_COLORSPACE_RGB;
779 else if (!strcmp(str, "yuv422"))
780 config->input_colorspace = HDMI_COLORSPACE_YUV422;
781 else if (!strcmp(str, "yuv444"))
782 config->input_colorspace = HDMI_COLORSPACE_YUV444;
783 else
784 return -EINVAL;
785
786 ret = of_property_read_string(np, "adi,input-clock", &str);
787 if (ret < 0)
788 return ret;
789
790 if (!strcmp(str, "1x"))
791 config->input_clock = ADV7511_INPUT_CLOCK_1X;
792 else if (!strcmp(str, "2x"))
793 config->input_clock = ADV7511_INPUT_CLOCK_2X;
794 else if (!strcmp(str, "ddr"))
795 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
796 else
797 return -EINVAL;
798
799 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
800 config->input_clock != ADV7511_INPUT_CLOCK_1X) {
801 ret = of_property_read_u32(np, "adi,input-style",
802 &config->input_style);
803 if (ret)
804 return ret;
805
806 if (config->input_style < 1 || config->input_style > 3)
807 return -EINVAL;
808
809 ret = of_property_read_string(np, "adi,input-justification",
810 &str);
811 if (ret < 0)
812 return ret;
813
814 if (!strcmp(str, "left"))
815 config->input_justification =
816 ADV7511_INPUT_JUSTIFICATION_LEFT;
817 else if (!strcmp(str, "evenly"))
818 config->input_justification =
819 ADV7511_INPUT_JUSTIFICATION_EVENLY;
820 else if (!strcmp(str, "right"))
821 config->input_justification =
822 ADV7511_INPUT_JUSTIFICATION_RIGHT;
823 else
824 return -EINVAL;
825
826 } else {
827 config->input_style = 1;
828 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
829 }
830
831 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
832 if (config->clock_delay < -1200 || config->clock_delay > 1600)
833 return -EINVAL;
834
835 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
836
837 /* Hardcode the sync pulse configurations for now. */
838 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
839 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
840 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
841
842 return 0;
843}
844
845static const int edid_i2c_addr = 0x7e;
846static const int packet_i2c_addr = 0x70;
847static const int cec_i2c_addr = 0x78;
848
849static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
850{
851 struct adv7511_link_config link_config;
852 struct adv7511 *adv7511;
853 struct device *dev = &i2c->dev;
854 unsigned int val;
855 int ret;
856
857 if (!dev->of_node)
858 return -EINVAL;
859
860 adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
861 if (!adv7511)
862 return -ENOMEM;
863
864 adv7511->dpms_mode = DRM_MODE_DPMS_OFF;
865 adv7511->status = connector_status_disconnected;
866
867 ret = adv7511_parse_dt(dev->of_node, &link_config);
868 if (ret)
869 return ret;
870
871 /*
872 * The power down GPIO is optional. If present, toggle it from active to
873 * inactive to wake up the encoder.
874 */
875 adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
876 if (IS_ERR(adv7511->gpio_pd))
877 return PTR_ERR(adv7511->gpio_pd);
878
879 if (adv7511->gpio_pd) {
880 mdelay(5);
881 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
882 }
883
884 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
885 if (IS_ERR(adv7511->regmap))
886 return PTR_ERR(adv7511->regmap);
887
888 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
889 if (ret)
890 return ret;
891 dev_dbg(dev, "Rev. %d\n", val);
892
893 ret = regmap_register_patch(adv7511->regmap, adv7511_fixed_registers,
894 ARRAY_SIZE(adv7511_fixed_registers));
895 if (ret)
896 return ret;
897
898 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
899 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
900 packet_i2c_addr);
901 regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR, cec_i2c_addr);
902 adv7511_packet_disable(adv7511, 0xffff);
903
904 adv7511->i2c_main = i2c;
905 adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
906 if (!adv7511->i2c_edid)
907 return -ENOMEM;
908
909 if (i2c->irq) {
910 init_waitqueue_head(&adv7511->wq);
911
912 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
913 adv7511_irq_handler,
914 IRQF_ONESHOT, dev_name(dev),
915 adv7511);
916 if (ret)
917 goto err_i2c_unregister_device;
918 }
919
920 /* CEC is unused for now */
921 regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
922 ADV7511_CEC_CTRL_POWER_DOWN);
923
924 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
925 ADV7511_POWER_POWER_DOWN, ADV7511_POWER_POWER_DOWN);
926
927 adv7511->current_edid_segment = -1;
928
929 i2c_set_clientdata(i2c, adv7511);
930
931 adv7511_set_link_config(adv7511, &link_config);
932
933 return 0;
934
935err_i2c_unregister_device:
936 i2c_unregister_device(adv7511->i2c_edid);
937
938 return ret;
939}
940
941static int adv7511_remove(struct i2c_client *i2c)
942{
943 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
944
945 i2c_unregister_device(adv7511->i2c_edid);
946
947 kfree(adv7511->edid);
948
949 return 0;
950}
951
952static int adv7511_encoder_init(struct i2c_client *i2c, struct drm_device *dev,
953 struct drm_encoder_slave *encoder)
954{
955
956 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
957
958 encoder->slave_priv = adv7511;
959 encoder->slave_funcs = &adv7511_encoder_funcs;
960
961 adv7511->encoder = &encoder->base;
962
963 return 0;
964}
965
966static const struct i2c_device_id adv7511_i2c_ids[] = {
967 { "adv7511", 0 },
968 { "adv7511w", 0 },
969 { "adv7513", 0 },
970 { }
971};
972MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
973
974static const struct of_device_id adv7511_of_ids[] = {
975 { .compatible = "adi,adv7511", },
976 { .compatible = "adi,adv7511w", },
977 { .compatible = "adi,adv7513", },
978 { }
979};
980MODULE_DEVICE_TABLE(of, adv7511_of_ids);
981
982static struct drm_i2c_encoder_driver adv7511_driver = {
983 .i2c_driver = {
984 .driver = {
985 .name = "adv7511",
986 .of_match_table = adv7511_of_ids,
987 },
988 .id_table = adv7511_i2c_ids,
989 .probe = adv7511_probe,
990 .remove = adv7511_remove,
991 },
992
993 .encoder_init = adv7511_encoder_init,
994};
995
996static int __init adv7511_init(void)
997{
998 return drm_i2c_encoder_register(THIS_MODULE, &adv7511_driver);
999}
1000module_init(adv7511_init);
1001
1002static void __exit adv7511_exit(void)
1003{
1004 drm_i2c_encoder_unregister(&adv7511_driver);
1005}
1006module_exit(adv7511_exit);
1007
1008MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1009MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1010MODULE_LICENSE("GPL");