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