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