blob: fa140e04d5fa72f18237054c2b10f23f77355d83 [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
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100647 return MODE_OK;
648}
649
650static void adv7511_encoder_mode_set(struct drm_encoder *encoder,
651 struct drm_display_mode *mode,
652 struct drm_display_mode *adj_mode)
653{
654 struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
655 unsigned int low_refresh_rate;
656 unsigned int hsync_polarity = 0;
657 unsigned int vsync_polarity = 0;
658
659 if (adv7511->embedded_sync) {
660 unsigned int hsync_offset, hsync_len;
661 unsigned int vsync_offset, vsync_len;
662
663 hsync_offset = adj_mode->crtc_hsync_start -
664 adj_mode->crtc_hdisplay;
665 vsync_offset = adj_mode->crtc_vsync_start -
666 adj_mode->crtc_vdisplay;
667 hsync_len = adj_mode->crtc_hsync_end -
668 adj_mode->crtc_hsync_start;
669 vsync_len = adj_mode->crtc_vsync_end -
670 adj_mode->crtc_vsync_start;
671
672 /* The hardware vsync generator has a off-by-one bug */
673 vsync_offset += 1;
674
675 regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
676 ((hsync_offset >> 10) & 0x7) << 5);
677 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
678 (hsync_offset >> 2) & 0xff);
679 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
680 ((hsync_offset & 0x3) << 6) |
681 ((hsync_len >> 4) & 0x3f));
682 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
683 ((hsync_len & 0xf) << 4) |
684 ((vsync_offset >> 6) & 0xf));
685 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
686 ((vsync_offset & 0x3f) << 2) |
687 ((vsync_len >> 8) & 0x3));
688 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
689 vsync_len & 0xff);
690
691 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
692 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
693 } else {
694 enum adv7511_sync_polarity mode_hsync_polarity;
695 enum adv7511_sync_polarity mode_vsync_polarity;
696
697 /**
698 * If the input signal is always low or always high we want to
699 * invert or let it passthrough depending on the polarity of the
700 * current mode.
701 **/
702 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
703 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
704 else
705 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
706
707 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
708 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
709 else
710 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
711
712 if (adv7511->hsync_polarity != mode_hsync_polarity &&
713 adv7511->hsync_polarity !=
714 ADV7511_SYNC_POLARITY_PASSTHROUGH)
715 hsync_polarity = 1;
716
717 if (adv7511->vsync_polarity != mode_vsync_polarity &&
718 adv7511->vsync_polarity !=
719 ADV7511_SYNC_POLARITY_PASSTHROUGH)
720 vsync_polarity = 1;
721 }
722
723 if (mode->vrefresh <= 24000)
724 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
725 else if (mode->vrefresh <= 25000)
726 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
727 else if (mode->vrefresh <= 30000)
728 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
729 else
730 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
731
732 regmap_update_bits(adv7511->regmap, 0xfb,
733 0x6, low_refresh_rate << 1);
734 regmap_update_bits(adv7511->regmap, 0x17,
735 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
736
737 /*
738 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
739 * supposed to give better results.
740 */
741
742 adv7511->f_tmds = mode->clock;
743}
744
745static struct drm_encoder_slave_funcs adv7511_encoder_funcs = {
746 .dpms = adv7511_encoder_dpms,
747 .mode_valid = adv7511_encoder_mode_valid,
748 .mode_set = adv7511_encoder_mode_set,
749 .detect = adv7511_encoder_detect,
750 .get_modes = adv7511_get_modes,
751};
752
753/* -----------------------------------------------------------------------------
754 * Probe & remove
755 */
756
757static int adv7511_parse_dt(struct device_node *np,
758 struct adv7511_link_config *config)
759{
760 const char *str;
761 int ret;
762
763 memset(config, 0, sizeof(*config));
764
765 of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
766 if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
767 config->input_color_depth != 12)
768 return -EINVAL;
769
770 ret = of_property_read_string(np, "adi,input-colorspace", &str);
771 if (ret < 0)
772 return ret;
773
774 if (!strcmp(str, "rgb"))
775 config->input_colorspace = HDMI_COLORSPACE_RGB;
776 else if (!strcmp(str, "yuv422"))
777 config->input_colorspace = HDMI_COLORSPACE_YUV422;
778 else if (!strcmp(str, "yuv444"))
779 config->input_colorspace = HDMI_COLORSPACE_YUV444;
780 else
781 return -EINVAL;
782
783 ret = of_property_read_string(np, "adi,input-clock", &str);
784 if (ret < 0)
785 return ret;
786
787 if (!strcmp(str, "1x"))
788 config->input_clock = ADV7511_INPUT_CLOCK_1X;
789 else if (!strcmp(str, "2x"))
790 config->input_clock = ADV7511_INPUT_CLOCK_2X;
791 else if (!strcmp(str, "ddr"))
792 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
793 else
794 return -EINVAL;
795
796 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
797 config->input_clock != ADV7511_INPUT_CLOCK_1X) {
798 ret = of_property_read_u32(np, "adi,input-style",
799 &config->input_style);
800 if (ret)
801 return ret;
802
803 if (config->input_style < 1 || config->input_style > 3)
804 return -EINVAL;
805
806 ret = of_property_read_string(np, "adi,input-justification",
807 &str);
808 if (ret < 0)
809 return ret;
810
811 if (!strcmp(str, "left"))
812 config->input_justification =
813 ADV7511_INPUT_JUSTIFICATION_LEFT;
814 else if (!strcmp(str, "evenly"))
815 config->input_justification =
816 ADV7511_INPUT_JUSTIFICATION_EVENLY;
817 else if (!strcmp(str, "right"))
818 config->input_justification =
819 ADV7511_INPUT_JUSTIFICATION_RIGHT;
820 else
821 return -EINVAL;
822
823 } else {
824 config->input_style = 1;
825 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
826 }
827
828 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
829 if (config->clock_delay < -1200 || config->clock_delay > 1600)
830 return -EINVAL;
831
832 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
833
834 /* Hardcode the sync pulse configurations for now. */
835 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
836 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
837 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
838
839 return 0;
840}
841
842static const int edid_i2c_addr = 0x7e;
843static const int packet_i2c_addr = 0x70;
844static const int cec_i2c_addr = 0x78;
845
846static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
847{
848 struct adv7511_link_config link_config;
849 struct adv7511 *adv7511;
850 struct device *dev = &i2c->dev;
851 unsigned int val;
852 int ret;
853
854 if (!dev->of_node)
855 return -EINVAL;
856
857 adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
858 if (!adv7511)
859 return -ENOMEM;
860
861 adv7511->dpms_mode = DRM_MODE_DPMS_OFF;
862 adv7511->status = connector_status_disconnected;
863
864 ret = adv7511_parse_dt(dev->of_node, &link_config);
865 if (ret)
866 return ret;
867
868 /*
869 * The power down GPIO is optional. If present, toggle it from active to
870 * inactive to wake up the encoder.
871 */
872 adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
873 if (IS_ERR(adv7511->gpio_pd))
874 return PTR_ERR(adv7511->gpio_pd);
875
876 if (adv7511->gpio_pd) {
877 mdelay(5);
878 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
879 }
880
881 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
882 if (IS_ERR(adv7511->regmap))
883 return PTR_ERR(adv7511->regmap);
884
885 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
886 if (ret)
887 return ret;
888 dev_dbg(dev, "Rev. %d\n", val);
889
890 ret = regmap_register_patch(adv7511->regmap, adv7511_fixed_registers,
891 ARRAY_SIZE(adv7511_fixed_registers));
892 if (ret)
893 return ret;
894
895 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
896 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
897 packet_i2c_addr);
898 regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR, cec_i2c_addr);
899 adv7511_packet_disable(adv7511, 0xffff);
900
901 adv7511->i2c_main = i2c;
902 adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
903 if (!adv7511->i2c_edid)
904 return -ENOMEM;
905
906 if (i2c->irq) {
907 init_waitqueue_head(&adv7511->wq);
908
909 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
910 adv7511_irq_handler,
911 IRQF_ONESHOT, dev_name(dev),
912 adv7511);
913 if (ret)
914 goto err_i2c_unregister_device;
915 }
916
917 /* CEC is unused for now */
918 regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
919 ADV7511_CEC_CTRL_POWER_DOWN);
920
921 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
922 ADV7511_POWER_POWER_DOWN, ADV7511_POWER_POWER_DOWN);
923
924 adv7511->current_edid_segment = -1;
925
926 i2c_set_clientdata(i2c, adv7511);
927
928 adv7511_set_link_config(adv7511, &link_config);
929
930 return 0;
931
932err_i2c_unregister_device:
933 i2c_unregister_device(adv7511->i2c_edid);
934
935 return ret;
936}
937
938static int adv7511_remove(struct i2c_client *i2c)
939{
940 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
941
942 i2c_unregister_device(adv7511->i2c_edid);
943
944 kfree(adv7511->edid);
945
946 return 0;
947}
948
949static int adv7511_encoder_init(struct i2c_client *i2c, struct drm_device *dev,
950 struct drm_encoder_slave *encoder)
951{
952
953 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
954
955 encoder->slave_priv = adv7511;
956 encoder->slave_funcs = &adv7511_encoder_funcs;
957
958 adv7511->encoder = &encoder->base;
959
960 return 0;
961}
962
963static const struct i2c_device_id adv7511_i2c_ids[] = {
964 { "adv7511", 0 },
965 { "adv7511w", 0 },
966 { "adv7513", 0 },
967 { }
968};
969MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
970
971static const struct of_device_id adv7511_of_ids[] = {
972 { .compatible = "adi,adv7511", },
973 { .compatible = "adi,adv7511w", },
974 { .compatible = "adi,adv7513", },
975 { }
976};
977MODULE_DEVICE_TABLE(of, adv7511_of_ids);
978
979static struct drm_i2c_encoder_driver adv7511_driver = {
980 .i2c_driver = {
981 .driver = {
982 .name = "adv7511",
983 .of_match_table = adv7511_of_ids,
984 },
985 .id_table = adv7511_i2c_ids,
986 .probe = adv7511_probe,
987 .remove = adv7511_remove,
988 },
989
990 .encoder_init = adv7511_encoder_init,
991};
992
993static int __init adv7511_init(void)
994{
995 return drm_i2c_encoder_register(THIS_MODULE, &adv7511_driver);
996}
997module_init(adv7511_init);
998
999static void __exit adv7511_exit(void)
1000{
1001 drm_i2c_encoder_unregister(&adv7511_driver);
1002}
1003module_exit(adv7511_exit);
1004
1005MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1006MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1007MODULE_LICENSE("GPL");