]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/i2c/adv7511.c
eec5885d6e9e86fe6ae8c3bc41f19d33eff42975
[karo-tx-linux.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 #include <linux/of_graph.h>
16
17 #include <drm/drmP.h>
18 #include <drm/drm_crtc_helper.h>
19 #include <drm/drm_edid.h>
20 #include <drm/drm_encoder_slave.h>
21 #include <drm/drm_atomic.h>
22 #include <drm/drm_atomic_helper.h>
23 #include <drm/drm_mipi_dsi.h>
24
25 #include "adv7511.h"
26
27 enum adv7511_type {
28         ADV7511,
29         ADV7533,
30 };
31
32 struct adv7511 {
33         struct i2c_client *i2c_main;
34         struct i2c_client *i2c_edid;
35         struct i2c_client *i2c_cec;
36
37         struct regmap *regmap;
38         struct regmap *regmap_cec;
39         enum drm_connector_status status;
40         bool powered;
41
42         struct drm_display_mode curr_mode;
43
44         unsigned int f_tmds;
45
46         unsigned int current_edid_segment;
47         uint8_t edid_buf[256];
48         bool edid_read;
49
50         wait_queue_head_t wq;
51         struct drm_encoder *encoder;
52
53         struct drm_connector connector;
54         struct drm_bridge bridge;
55
56         bool embedded_sync;
57         enum adv7511_sync_polarity vsync_polarity;
58         enum adv7511_sync_polarity hsync_polarity;
59         bool rgb;
60
61         struct edid *edid;
62
63         struct gpio_desc *gpio_pd;
64
65         /* ADV7533 DSI RX related params */
66         struct device_node *host_node;
67         struct mipi_dsi_device *dsi;
68         u8 num_dsi_lanes;
69
70         enum adv7511_type type;
71 };
72
73 static struct adv7511 *encoder_to_adv7511(struct drm_encoder *encoder)
74 {
75         return to_encoder_slave(encoder)->slave_priv;
76 }
77
78 /* ADI recommended values for proper operation. */
79 static const struct reg_sequence adv7511_fixed_registers[] = {
80         { 0x98, 0x03 },
81         { 0x9a, 0xe0 },
82         { 0x9c, 0x30 },
83         { 0x9d, 0x61 },
84         { 0xa2, 0xa4 },
85         { 0xa3, 0xa4 },
86         { 0xe0, 0xd0 },
87         { 0xf9, 0x00 },
88         { 0x55, 0x02 },
89 };
90
91 /* ADI recommended values for proper operation. */
92 static const struct reg_default adv7533_fixed_registers[] = {
93         { 0x16, 0x20 },
94         { 0x9a, 0xe0 },
95         { 0xba, 0x70 },
96         { 0xde, 0x82 },
97         { 0xe4, 0x40 },
98         { 0xe5, 0x80 },
99 };
100
101 static const struct reg_default adv7533_cec_fixed_registers[] = {
102         { 0x15, 0xd0 },
103         { 0x17, 0xd0 },
104         { 0x24, 0x20 },
105         { 0x57, 0x11 },
106 };
107
108 /* -----------------------------------------------------------------------------
109  * Register access
110  */
111
112 static const uint8_t adv7511_register_defaults[] = {
113         0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
114         0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
115         0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
116         0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
117         0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
118         0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
119         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
120         0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
121         0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
122         0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
123         0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
124         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
126         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127         0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
128         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
130         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131         0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
132         0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
133         0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
134         0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
135         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
136         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
137         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
138         0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
139         0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
140         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
141         0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
142         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143         0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
144         0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145 };
146
147 static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
148 {
149         switch (reg) {
150         case ADV7511_REG_CHIP_REVISION:
151         case ADV7511_REG_SPDIF_FREQ:
152         case ADV7511_REG_CTS_AUTOMATIC1:
153         case ADV7511_REG_CTS_AUTOMATIC2:
154         case ADV7511_REG_VIC_DETECTED:
155         case ADV7511_REG_VIC_SEND:
156         case ADV7511_REG_AUX_VIC_DETECTED:
157         case ADV7511_REG_STATUS:
158         case ADV7511_REG_GC(1):
159         case ADV7511_REG_INT(0):
160         case ADV7511_REG_INT(1):
161         case ADV7511_REG_PLL_STATUS:
162         case ADV7511_REG_AN(0):
163         case ADV7511_REG_AN(1):
164         case ADV7511_REG_AN(2):
165         case ADV7511_REG_AN(3):
166         case ADV7511_REG_AN(4):
167         case ADV7511_REG_AN(5):
168         case ADV7511_REG_AN(6):
169         case ADV7511_REG_AN(7):
170         case ADV7511_REG_HDCP_STATUS:
171         case ADV7511_REG_BCAPS:
172         case ADV7511_REG_BKSV(0):
173         case ADV7511_REG_BKSV(1):
174         case ADV7511_REG_BKSV(2):
175         case ADV7511_REG_BKSV(3):
176         case ADV7511_REG_BKSV(4):
177         case ADV7511_REG_DDC_STATUS:
178         case ADV7511_REG_BSTATUS(0):
179         case ADV7511_REG_BSTATUS(1):
180         case ADV7511_REG_CHIP_ID_HIGH:
181         case ADV7511_REG_CHIP_ID_LOW:
182                 return true;
183         }
184
185         return false;
186 }
187
188 static const struct regmap_config adv7511_regmap_config = {
189         .reg_bits = 8,
190         .val_bits = 8,
191
192         .max_register = 0xff,
193         .cache_type = REGCACHE_RBTREE,
194         .reg_defaults_raw = adv7511_register_defaults,
195         .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
196
197         .volatile_reg = adv7511_register_volatile,
198 };
199
200 static const struct regmap_config adv7533_cec_regmap_config = {
201         .reg_bits = 8,
202         .val_bits = 8,
203
204         .max_register = 0xff,
205         .cache_type = REGCACHE_RBTREE,
206 };
207
208
209 /* -----------------------------------------------------------------------------
210  * Hardware configuration
211  */
212
213 static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
214                                  const uint16_t *coeff,
215                                  unsigned int scaling_factor)
216 {
217         unsigned int i;
218
219         regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
220                            ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
221
222         if (enable) {
223                 for (i = 0; i < 12; ++i) {
224                         regmap_update_bits(adv7511->regmap,
225                                            ADV7511_REG_CSC_UPPER(i),
226                                            0x1f, coeff[i] >> 8);
227                         regmap_write(adv7511->regmap,
228                                      ADV7511_REG_CSC_LOWER(i),
229                                      coeff[i] & 0xff);
230                 }
231         }
232
233         if (enable)
234                 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
235                                    0xe0, 0x80 | (scaling_factor << 5));
236         else
237                 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
238                                    0x80, 0x00);
239
240         regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
241                            ADV7511_CSC_UPDATE_MODE, 0);
242 }
243
244 static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
245 {
246         if (packet & 0xff)
247                 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
248                                    packet, 0xff);
249
250         if (packet & 0xff00) {
251                 packet >>= 8;
252                 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
253                                    packet, 0xff);
254         }
255
256         return 0;
257 }
258
259 static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
260 {
261         if (packet & 0xff)
262                 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
263                                    packet, 0x00);
264
265         if (packet & 0xff00) {
266                 packet >>= 8;
267                 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
268                                    packet, 0x00);
269         }
270
271         return 0;
272 }
273
274 /* Coefficients for adv7511 color space conversion */
275 static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
276         0x0734, 0x04ad, 0x0000, 0x1c1b,
277         0x1ddc, 0x04ad, 0x1f24, 0x0135,
278         0x0000, 0x04ad, 0x087c, 0x1b77,
279 };
280
281 static void adv7511_set_config_csc(struct adv7511 *adv7511,
282                                    struct drm_connector *connector,
283                                    bool rgb)
284 {
285         struct adv7511_video_config config;
286         bool output_format_422, output_format_ycbcr;
287         unsigned int mode;
288         uint8_t infoframe[17];
289
290         if (adv7511->edid)
291                 config.hdmi_mode = drm_detect_hdmi_monitor(adv7511->edid);
292         else
293                 config.hdmi_mode = false;
294
295         hdmi_avi_infoframe_init(&config.avi_infoframe);
296
297         config.avi_infoframe.scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
298
299         if (rgb) {
300                 config.csc_enable = false;
301                 config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
302         } else {
303                 config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
304                 config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
305
306                 if ((connector->display_info.color_formats &
307                      DRM_COLOR_FORMAT_YCRCB422) &&
308                     config.hdmi_mode) {
309                         config.csc_enable = false;
310                         config.avi_infoframe.colorspace =
311                                 HDMI_COLORSPACE_YUV422;
312                 } else {
313                         config.csc_enable = true;
314                         config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
315                 }
316         }
317
318         if (config.hdmi_mode) {
319                 mode = ADV7511_HDMI_CFG_MODE_HDMI;
320
321                 switch (config.avi_infoframe.colorspace) {
322                 case HDMI_COLORSPACE_YUV444:
323                         output_format_422 = false;
324                         output_format_ycbcr = true;
325                         break;
326                 case HDMI_COLORSPACE_YUV422:
327                         output_format_422 = true;
328                         output_format_ycbcr = true;
329                         break;
330                 default:
331                         output_format_422 = false;
332                         output_format_ycbcr = false;
333                         break;
334                 }
335         } else {
336                 mode = ADV7511_HDMI_CFG_MODE_DVI;
337                 output_format_422 = false;
338                 output_format_ycbcr = false;
339         }
340
341         adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
342
343         adv7511_set_colormap(adv7511, config.csc_enable,
344                              config.csc_coefficents,
345                              config.csc_scaling_factor);
346
347         regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
348                            (output_format_422 << 7) | output_format_ycbcr);
349
350         regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
351                            ADV7511_HDMI_CFG_MODE_MASK, mode);
352
353         hdmi_avi_infoframe_pack(&config.avi_infoframe, infoframe,
354                                 sizeof(infoframe));
355
356         /* The AVI infoframe id is not configurable */
357         regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
358                           infoframe + 1, sizeof(infoframe) - 1);
359
360         adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
361 }
362
363 static void adv7511_set_link_config(struct adv7511 *adv7511,
364                                     const struct adv7511_link_config *config)
365 {
366         /*
367          * The input style values documented in the datasheet don't match the
368          * hardware register field values :-(
369          */
370         static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
371
372         unsigned int clock_delay;
373         unsigned int color_depth;
374         unsigned int input_id;
375
376         clock_delay = (config->clock_delay + 1200) / 400;
377         color_depth = config->input_color_depth == 8 ? 3
378                     : (config->input_color_depth == 10 ? 1 : 2);
379
380         /* TODO Support input ID 6 */
381         if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
382                 input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
383                          ? 5 : 0;
384         else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
385                 input_id = config->embedded_sync ? 8 : 7;
386         else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
387                 input_id = config->embedded_sync ? 4 : 3;
388         else
389                 input_id = config->embedded_sync ? 2 : 1;
390
391         regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
392                            input_id);
393         regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
394                            (color_depth << 4) |
395                            (input_styles[config->input_style] << 2));
396         regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
397                      config->input_justification << 3);
398         regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
399                      config->sync_pulse << 2);
400
401         regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
402
403         adv7511->embedded_sync = config->embedded_sync;
404         adv7511->hsync_polarity = config->hsync_polarity;
405         adv7511->vsync_polarity = config->vsync_polarity;
406         adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
407 }
408
409 static void adv7511_dsi_config_tgen(struct adv7511 *adv7511)
410 {
411         struct mipi_dsi_device *dsi = adv7511->dsi;
412         struct drm_display_mode *mode = &adv7511->curr_mode;
413         u8 clock_div_by_lanes[] = { 6, 4, 3 }; /* 2, 3, 4 lanes */
414         unsigned int hsw, hfp, hbp, vsw, vfp, vbp;
415
416         hsw = mode->hsync_end - mode->hsync_start;
417         hfp = mode->hsync_start - mode->hdisplay;
418         hbp = mode->htotal - mode->hsync_end;
419         vsw = mode->vsync_end - mode->vsync_start;
420         vfp = mode->vsync_start - mode->vdisplay;
421         vbp = mode->vtotal - mode->vsync_end;
422
423         /* set pixel clock divider mode */
424         regmap_write(adv7511->regmap_cec, 0x16,
425                         clock_div_by_lanes[dsi->lanes - 2] << 3);
426
427         /* horizontal porch params */
428         regmap_write(adv7511->regmap_cec, 0x28, mode->htotal >> 4);
429         regmap_write(adv7511->regmap_cec, 0x29, (mode->htotal << 4) & 0xff);
430         regmap_write(adv7511->regmap_cec, 0x2a, hsw >> 4);
431         regmap_write(adv7511->regmap_cec, 0x2b, (hsw << 4) & 0xff);
432         regmap_write(adv7511->regmap_cec, 0x2c, hfp >> 4);
433         regmap_write(adv7511->regmap_cec, 0x2d, (hfp << 4) & 0xff);
434         regmap_write(adv7511->regmap_cec, 0x2e, hbp >> 4);
435         regmap_write(adv7511->regmap_cec, 0x2f, (hbp << 4) & 0xff);
436
437         /* vertical porch params */
438         regmap_write(adv7511->regmap_cec, 0x30, mode->vtotal >> 4);
439         regmap_write(adv7511->regmap_cec, 0x31, (mode->vtotal << 4) & 0xff);
440         regmap_write(adv7511->regmap_cec, 0x32, vsw >> 4);
441         regmap_write(adv7511->regmap_cec, 0x33, (vsw << 4) & 0xff);
442         regmap_write(adv7511->regmap_cec, 0x34, vfp >> 4);
443         regmap_write(adv7511->regmap_cec, 0x35, (vfp << 4) & 0xff);
444         regmap_write(adv7511->regmap_cec, 0x36, vbp >> 4);
445         regmap_write(adv7511->regmap_cec, 0x37, (vbp << 4) & 0xff);
446 }
447
448 static void adv7511_dsi_receiver_dpms(struct adv7511 *adv7511)
449 {
450         if (adv7511->type != ADV7533)
451                 return;
452
453         if (adv7511->powered) {
454                 struct mipi_dsi_device *dsi = adv7511->dsi;
455
456                 adv7511_dsi_config_tgen(adv7511);
457
458                 /* set number of dsi lanes */
459                 regmap_write(adv7511->regmap_cec, 0x1c, dsi->lanes << 4);
460
461                 /* reset internal timing generator */
462                 regmap_write(adv7511->regmap_cec, 0x27, 0xcb);
463                 regmap_write(adv7511->regmap_cec, 0x27, 0x8b);
464                 regmap_write(adv7511->regmap_cec, 0x27, 0xcb);
465
466                 /* enable hdmi */
467                 regmap_write(adv7511->regmap_cec, 0x03, 0x89);
468                 /* disable test mode */
469                 regmap_write(adv7511->regmap_cec, 0x55, 0x00);
470         } else {
471                 regmap_write(adv7511->regmap_cec, 0x03, 0x0b);
472                 regmap_write(adv7511->regmap_cec, 0x27, 0x0b);
473         }
474 }
475
476 static void adv7511_power_on(struct adv7511 *adv7511)
477 {
478         adv7511->current_edid_segment = -1;
479
480         regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
481                      ADV7511_INT0_EDID_READY);
482         regmap_write(adv7511->regmap, ADV7511_REG_INT(1),
483                      ADV7511_INT1_DDC_ERROR);
484         regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
485                            ADV7511_POWER_POWER_DOWN, 0);
486
487         /*
488          * Per spec it is allowed to pulse the HDP signal to indicate that the
489          * EDID information has changed. Some monitors do this when they wakeup
490          * from standby or are enabled. When the HDP goes low the adv7511 is
491          * reset and the outputs are disabled which might cause the monitor to
492          * go to standby again. To avoid this we ignore the HDP pin for the
493          * first few seconds after enabling the output.
494          */
495         regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
496                            ADV7511_REG_POWER2_HDP_SRC_MASK,
497                            ADV7511_REG_POWER2_HDP_SRC_NONE);
498
499         /*
500          * Most of the registers are reset during power down or when HPD is low.
501          */
502         regcache_sync(adv7511->regmap);
503
504         if (adv7511->type == ADV7533)
505                 regmap_register_patch(adv7511->regmap_cec,
506                                       adv7533_cec_fixed_registers,
507                                       ARRAY_SIZE(adv7533_cec_fixed_registers));
508         adv7511->powered = true;
509
510         adv7511_dsi_receiver_dpms(adv7511);
511 }
512
513 static void adv7511_power_off(struct adv7511 *adv7511)
514 {
515         /* TODO: setup additional power down modes */
516         regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
517                            ADV7511_POWER_POWER_DOWN,
518                            ADV7511_POWER_POWER_DOWN);
519         regcache_mark_dirty(adv7511->regmap);
520
521         adv7511->powered = false;
522
523         adv7511_dsi_receiver_dpms(adv7511);
524 }
525
526 /* -----------------------------------------------------------------------------
527  * Interrupt and hotplug detection
528  */
529
530 static bool adv7511_hpd(struct adv7511 *adv7511)
531 {
532         unsigned int irq0;
533         int ret;
534
535         ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
536         if (ret < 0)
537                 return false;
538
539         if (irq0 & ADV7511_INT0_HDP) {
540                 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
541                              ADV7511_INT0_HDP);
542                 return true;
543         }
544
545         return false;
546 }
547
548 static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
549 {
550         unsigned int irq0, irq1;
551         int ret;
552
553         ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
554         if (ret < 0)
555                 return ret;
556
557         ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
558         if (ret < 0)
559                 return ret;
560
561         regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
562         regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
563
564         if (process_hpd && irq0 & ADV7511_INT0_HDP && adv7511->encoder)
565                 drm_helper_hpd_irq_event(adv7511->encoder->dev);
566
567         if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
568                 adv7511->edid_read = true;
569
570                 if (adv7511->i2c_main->irq)
571                         wake_up_all(&adv7511->wq);
572         }
573
574         return 0;
575 }
576
577 static irqreturn_t adv7511_irq_handler(int irq, void *devid)
578 {
579         struct adv7511 *adv7511 = devid;
580         int ret;
581
582         ret = adv7511_irq_process(adv7511, true);
583         return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
584 }
585
586 /* -----------------------------------------------------------------------------
587  * EDID retrieval
588  */
589
590 static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
591 {
592         int ret;
593
594         if (adv7511->i2c_main->irq) {
595                 ret = wait_event_interruptible_timeout(adv7511->wq,
596                                 adv7511->edid_read, msecs_to_jiffies(timeout));
597         } else {
598                 for (; timeout > 0; timeout -= 25) {
599                         ret = adv7511_irq_process(adv7511, false);
600                         if (ret < 0)
601                                 break;
602
603                         if (adv7511->edid_read)
604                                 break;
605
606                         msleep(25);
607                 }
608         }
609
610         return adv7511->edid_read ? 0 : -EIO;
611 }
612
613 static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
614                                   size_t len)
615 {
616         struct adv7511 *adv7511 = data;
617         struct i2c_msg xfer[2];
618         uint8_t offset;
619         unsigned int i;
620         int ret;
621
622         if (len > 128)
623                 return -EINVAL;
624
625         if (adv7511->current_edid_segment != block / 2) {
626                 unsigned int status;
627
628                 ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
629                                   &status);
630                 if (ret < 0)
631                         return ret;
632
633                 if (status != 2) {
634                         adv7511->edid_read = false;
635                         regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
636                                      block);
637                         ret = adv7511_wait_for_edid(adv7511, 200);
638                         if (ret < 0)
639                                 return ret;
640                 }
641
642                 /* Break this apart, hopefully more I2C controllers will
643                  * support 64 byte transfers than 256 byte transfers
644                  */
645
646                 xfer[0].addr = adv7511->i2c_edid->addr;
647                 xfer[0].flags = 0;
648                 xfer[0].len = 1;
649                 xfer[0].buf = &offset;
650                 xfer[1].addr = adv7511->i2c_edid->addr;
651                 xfer[1].flags = I2C_M_RD;
652                 xfer[1].len = 64;
653                 xfer[1].buf = adv7511->edid_buf;
654
655                 offset = 0;
656
657                 for (i = 0; i < 4; ++i) {
658                         ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
659                                            ARRAY_SIZE(xfer));
660                         if (ret < 0)
661                                 return ret;
662                         else if (ret != 2)
663                                 return -EIO;
664
665                         xfer[1].buf += 64;
666                         offset += 64;
667                 }
668
669                 adv7511->current_edid_segment = block / 2;
670         }
671
672         if (block % 2 == 0)
673                 memcpy(buf, adv7511->edid_buf, len);
674         else
675                 memcpy(buf, adv7511->edid_buf + 128, len);
676
677         return 0;
678 }
679
680 /* -----------------------------------------------------------------------------
681  * ADV75xx helpers
682  */
683 static int adv7511_get_modes(struct adv7511 *adv7511,
684                 struct drm_connector *connector)
685 {
686         struct edid *edid;
687         unsigned int count;
688
689         /* Reading the EDID only works if the device is powered */
690         if (!adv7511->powered) {
691                 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
692                                    ADV7511_REG_POWER2_HDP_SRC_MASK,
693                                    ADV7511_REG_POWER2_HDP_SRC_NONE);
694                 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
695                              ADV7511_INT0_EDID_READY);
696                 regmap_write(adv7511->regmap, ADV7511_REG_INT(1),
697                              ADV7511_INT1_DDC_ERROR);
698                 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
699                                    ADV7511_POWER_POWER_DOWN, 0);
700                 adv7511->current_edid_segment = -1;
701         }
702
703         edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
704
705         if (!adv7511->powered)
706                 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
707                                    ADV7511_POWER_POWER_DOWN,
708                                    ADV7511_POWER_POWER_DOWN);
709
710         kfree(adv7511->edid);
711         adv7511->edid = edid;
712         if (!edid)
713                 return 0;
714
715         drm_mode_connector_update_edid_property(connector, edid);
716         count = drm_add_edid_modes(connector, edid);
717
718         adv7511_set_config_csc(adv7511, connector, adv7511->rgb);
719
720         return count;
721 }
722
723 static enum drm_connector_status
724 adv7511_detect(struct adv7511 *adv7511,
725                        struct drm_connector *connector)
726 {
727         enum drm_connector_status status;
728         unsigned int val;
729         bool hpd;
730         int ret;
731
732         ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
733         if (ret < 0)
734                 return connector_status_disconnected;
735
736         if (val & ADV7511_STATUS_HPD)
737                 status = connector_status_connected;
738         else
739                 status = connector_status_disconnected;
740
741         hpd = adv7511_hpd(adv7511);
742
743         /* The chip resets itself when the cable is disconnected, so in case
744          * there is a pending HPD interrupt and the cable is connected there was
745          * at least one transition from disconnected to connected and the chip
746          * has to be reinitialized. */
747         if (status == connector_status_connected && hpd && adv7511->powered) {
748                 regcache_mark_dirty(adv7511->regmap);
749                 adv7511_power_on(adv7511);
750                 adv7511_get_modes(adv7511, connector);
751                 if (adv7511->status == connector_status_connected)
752                         status = connector_status_disconnected;
753         } else {
754                 /* Renable HDP sensing */
755                 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
756                                    ADV7511_REG_POWER2_HDP_SRC_MASK,
757                                    ADV7511_REG_POWER2_HDP_SRC_BOTH);
758         }
759
760         adv7511->status = status;
761         return status;
762 }
763
764 static int adv7511_mode_valid(struct adv7511 *adv7511,
765                                      const struct drm_display_mode *mode)
766 {
767         if (mode->clock > 165000)
768                 return MODE_CLOCK_HIGH;
769
770         return MODE_OK;
771 }
772
773 static void adv7511_mode_set(struct adv7511 *adv7511,
774                                      struct drm_display_mode *mode,
775                                      struct drm_display_mode *adj_mode)
776 {
777         unsigned int low_refresh_rate;
778         unsigned int hsync_polarity = 0;
779         unsigned int vsync_polarity = 0;
780
781         if (adv7511->embedded_sync) {
782                 unsigned int hsync_offset, hsync_len;
783                 unsigned int vsync_offset, vsync_len;
784
785                 hsync_offset = adj_mode->crtc_hsync_start -
786                                adj_mode->crtc_hdisplay;
787                 vsync_offset = adj_mode->crtc_vsync_start -
788                                adj_mode->crtc_vdisplay;
789                 hsync_len = adj_mode->crtc_hsync_end -
790                             adj_mode->crtc_hsync_start;
791                 vsync_len = adj_mode->crtc_vsync_end -
792                             adj_mode->crtc_vsync_start;
793
794                 /* The hardware vsync generator has a off-by-one bug */
795                 vsync_offset += 1;
796
797                 regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
798                              ((hsync_offset >> 10) & 0x7) << 5);
799                 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
800                              (hsync_offset >> 2) & 0xff);
801                 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
802                              ((hsync_offset & 0x3) << 6) |
803                              ((hsync_len >> 4) & 0x3f));
804                 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
805                              ((hsync_len & 0xf) << 4) |
806                              ((vsync_offset >> 6) & 0xf));
807                 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
808                              ((vsync_offset & 0x3f) << 2) |
809                              ((vsync_len >> 8) & 0x3));
810                 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
811                              vsync_len & 0xff);
812
813                 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
814                 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
815         } else {
816                 enum adv7511_sync_polarity mode_hsync_polarity;
817                 enum adv7511_sync_polarity mode_vsync_polarity;
818
819                 /**
820                  * If the input signal is always low or always high we want to
821                  * invert or let it passthrough depending on the polarity of the
822                  * current mode.
823                  **/
824                 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
825                         mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
826                 else
827                         mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
828
829                 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
830                         mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
831                 else
832                         mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
833
834                 if (adv7511->hsync_polarity != mode_hsync_polarity &&
835                     adv7511->hsync_polarity !=
836                     ADV7511_SYNC_POLARITY_PASSTHROUGH)
837                         hsync_polarity = 1;
838
839                 if (adv7511->vsync_polarity != mode_vsync_polarity &&
840                     adv7511->vsync_polarity !=
841                     ADV7511_SYNC_POLARITY_PASSTHROUGH)
842                         vsync_polarity = 1;
843         }
844
845         if (mode->vrefresh <= 24000)
846                 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
847         else if (mode->vrefresh <= 25000)
848                 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
849         else if (mode->vrefresh <= 30000)
850                 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
851         else
852                 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
853
854         regmap_update_bits(adv7511->regmap, 0xfb,
855                 0x6, low_refresh_rate << 1);
856         regmap_update_bits(adv7511->regmap, 0x17,
857                 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
858
859         drm_mode_copy(&adv7511->curr_mode, adj_mode);
860
861         /*
862          * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
863          * supposed to give better results.
864          */
865
866         adv7511->f_tmds = mode->clock;
867 }
868
869 /* -----------------------------------------------------------------------------
870  * Encoder operations
871  */
872
873 static int adv7511_encoder_get_modes(struct drm_encoder *encoder,
874                              struct drm_connector *connector)
875 {
876         struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
877
878         return adv7511_get_modes(adv7511, connector);
879 }
880
881 static void adv7511_encoder_dpms(struct drm_encoder *encoder, int mode)
882 {
883         struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
884
885         if (mode == DRM_MODE_DPMS_ON)
886                 adv7511_power_on(adv7511);
887         else
888                 adv7511_power_off(adv7511);
889 }
890
891 static enum drm_connector_status
892 adv7511_encoder_detect(struct drm_encoder *encoder,
893                        struct drm_connector *connector)
894 {
895         struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
896
897         return adv7511_detect(adv7511, connector);
898 }
899
900 static int adv7511_encoder_mode_valid(struct drm_encoder *encoder,
901                                       struct drm_display_mode *mode)
902 {
903         struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
904
905         return adv7511_mode_valid(adv7511, mode);
906 }
907
908 static void adv7511_encoder_mode_set(struct drm_encoder *encoder,
909                                      struct drm_display_mode *mode,
910                                      struct drm_display_mode *adj_mode)
911 {
912         struct adv7511 *adv7511 = encoder_to_adv7511(encoder);
913
914         adv7511_mode_set(adv7511, mode, adj_mode);
915 }
916
917 static struct drm_encoder_slave_funcs adv7511_encoder_funcs = {
918         .dpms = adv7511_encoder_dpms,
919         .mode_valid = adv7511_encoder_mode_valid,
920         .mode_set = adv7511_encoder_mode_set,
921         .detect = adv7511_encoder_detect,
922         .get_modes = adv7511_encoder_get_modes,
923 };
924
925 /* -----------------------------------------------------------------------------
926  * Bridge and connector functions
927  */
928
929 static struct adv7511 *connector_to_adv7511(struct drm_connector *connector)
930 {
931         return container_of(connector, struct adv7511, connector);
932 }
933
934 /* Connector helper functions */
935 static int adv7533_connector_get_modes(struct drm_connector *connector)
936 {
937         struct adv7511 *adv = connector_to_adv7511(connector);
938
939         return adv7511_get_modes(adv, connector);
940 }
941
942 static struct drm_encoder *
943 adv7533_connector_best_encoder(struct drm_connector *connector)
944 {
945         struct adv7511 *adv = connector_to_adv7511(connector);
946
947         return adv->bridge.encoder;
948 }
949
950 static enum drm_mode_status
951 adv7533_connector_mode_valid(struct drm_connector *connector,
952                                 struct drm_display_mode *mode)
953 {
954         struct adv7511 *adv = connector_to_adv7511(connector);
955
956         return adv7511_mode_valid(adv, mode);
957 }
958
959 static struct drm_connector_helper_funcs adv7533_connector_helper_funcs = {
960         .get_modes = adv7533_connector_get_modes,
961         .best_encoder = adv7533_connector_best_encoder,
962         .mode_valid = adv7533_connector_mode_valid,
963 };
964
965 static enum drm_connector_status
966 adv7533_connector_detect(struct drm_connector *connector, bool force)
967 {
968         struct adv7511 *adv = connector_to_adv7511(connector);
969
970         return adv7511_detect(adv, connector);
971 }
972
973 static struct drm_connector_funcs adv7533_connector_funcs = {
974         .dpms = drm_atomic_helper_connector_dpms,
975         .fill_modes = drm_helper_probe_single_connector_modes,
976         .detect = adv7533_connector_detect,
977         .destroy = drm_connector_cleanup,
978         .reset = drm_atomic_helper_connector_reset,
979         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
980         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
981 };
982
983 /* Bridge funcs */
984 static struct adv7511 *bridge_to_adv7511(struct drm_bridge *bridge)
985 {
986         return container_of(bridge, struct adv7511, bridge);
987 }
988
989 static void adv7533_bridge_pre_enable(struct drm_bridge *bridge)
990 {
991         struct adv7511 *adv = bridge_to_adv7511(bridge);
992
993         adv7511_power_on(adv);
994 }
995
996 static void adv7533_bridge_post_disable(struct drm_bridge *bridge)
997 {
998         struct adv7511 *adv = bridge_to_adv7511(bridge);
999
1000         adv7511_power_off(adv);
1001 }
1002
1003 static void adv7533_bridge_enable(struct drm_bridge *bridge)
1004 {
1005 }
1006
1007 static void adv7533_bridge_disable(struct drm_bridge *bridge)
1008 {
1009 }
1010
1011 static void adv7533_bridge_mode_set(struct drm_bridge *bridge,
1012                                      struct drm_display_mode *mode,
1013                                      struct drm_display_mode *adj_mode)
1014 {
1015         struct adv7511 *adv = bridge_to_adv7511(bridge);
1016
1017         adv7511_mode_set(adv, mode, adj_mode);
1018 }
1019
1020 static int adv7533_attach_dsi(struct adv7511 *adv7511)
1021 {
1022         struct device *dev = &adv7511->i2c_main->dev;
1023         struct mipi_dsi_device *dsi;
1024         struct mipi_dsi_host *host;
1025         int ret;
1026
1027         host = of_find_mipi_dsi_host_by_node(adv7511->host_node);
1028         if (!host) {
1029                 dev_err(dev, "failed to find dsi host\n");
1030                 return -EPROBE_DEFER;
1031         }
1032
1033         /* can adv7533 virtual channel be non-zero? */
1034         dsi = mipi_dsi_new_dummy(host, 0);
1035         if (IS_ERR(dsi)) {
1036                 dev_err(dev, "failed to create dummy dsi device\n");
1037                 ret = PTR_ERR(dsi);
1038                 goto err_dsi_device;
1039         }
1040
1041         adv7511->dsi = dsi;
1042
1043         dsi->lanes = adv7511->num_dsi_lanes;
1044         dsi->format = MIPI_DSI_FMT_RGB888;
1045         dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE
1046                         | MIPI_DSI_MODE_EOT_PACKET | MIPI_DSI_MODE_VIDEO_HSE;
1047
1048         ret = mipi_dsi_attach(dsi);
1049         if (ret < 0) {
1050                 dev_err(dev, "failed to attach dsi to host\n");
1051                 goto err_dsi_attach;
1052         }
1053
1054         return 0;
1055
1056 err_dsi_attach:
1057         mipi_dsi_unregister_device(dsi);
1058 err_dsi_device:
1059         return ret;
1060 }
1061
1062 static int adv7533_bridge_attach(struct drm_bridge *bridge)
1063 {
1064         struct adv7511 *adv = bridge_to_adv7511(bridge);
1065         int ret;
1066
1067         adv->encoder = bridge->encoder;
1068
1069         if (!bridge->encoder) {
1070                 DRM_ERROR("Parent encoder object not found");
1071                 return -ENODEV;
1072         }
1073
1074         adv->connector.polled = DRM_CONNECTOR_POLL_HPD;
1075         ret = drm_connector_init(bridge->dev, &adv->connector,
1076                         &adv7533_connector_funcs, DRM_MODE_CONNECTOR_HDMIA);
1077         if (ret) {
1078                 DRM_ERROR("Failed to initialize connector with drm\n");
1079                 return ret;
1080         }
1081         drm_connector_helper_add(&adv->connector,
1082                                         &adv7533_connector_helper_funcs);
1083         drm_connector_register(&adv->connector);
1084         drm_mode_connector_attach_encoder(&adv->connector, adv->encoder);
1085
1086         drm_helper_hpd_irq_event(adv->connector.dev);
1087
1088         adv7533_attach_dsi(adv);
1089
1090         return ret;
1091 }
1092
1093 static struct drm_bridge_funcs adv7533_bridge_funcs = {
1094         .pre_enable = adv7533_bridge_pre_enable,
1095         .enable = adv7533_bridge_enable,
1096         .disable = adv7533_bridge_disable,
1097         .post_disable = adv7533_bridge_post_disable,
1098         .mode_set = adv7533_bridge_mode_set,
1099         .attach = adv7533_bridge_attach,
1100 };
1101
1102 /* -----------------------------------------------------------------------------
1103  * Probe & remove
1104  */
1105
1106 static int adv7511_parse_dt(struct device_node *np,
1107                             struct adv7511_link_config *config)
1108 {
1109         const char *str;
1110         int ret;
1111
1112         of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
1113         if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
1114             config->input_color_depth != 12)
1115                 return -EINVAL;
1116
1117         ret = of_property_read_string(np, "adi,input-colorspace", &str);
1118         if (ret < 0)
1119                 return ret;
1120
1121         if (!strcmp(str, "rgb"))
1122                 config->input_colorspace = HDMI_COLORSPACE_RGB;
1123         else if (!strcmp(str, "yuv422"))
1124                 config->input_colorspace = HDMI_COLORSPACE_YUV422;
1125         else if (!strcmp(str, "yuv444"))
1126                 config->input_colorspace = HDMI_COLORSPACE_YUV444;
1127         else
1128                 return -EINVAL;
1129
1130         ret = of_property_read_string(np, "adi,input-clock", &str);
1131         if (ret < 0)
1132                 return ret;
1133
1134         if (!strcmp(str, "1x"))
1135                 config->input_clock = ADV7511_INPUT_CLOCK_1X;
1136         else if (!strcmp(str, "2x"))
1137                 config->input_clock = ADV7511_INPUT_CLOCK_2X;
1138         else if (!strcmp(str, "ddr"))
1139                 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
1140         else
1141                 return -EINVAL;
1142
1143         if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
1144             config->input_clock != ADV7511_INPUT_CLOCK_1X) {
1145                 ret = of_property_read_u32(np, "adi,input-style",
1146                                            &config->input_style);
1147                 if (ret)
1148                         return ret;
1149
1150                 if (config->input_style < 1 || config->input_style > 3)
1151                         return -EINVAL;
1152
1153                 ret = of_property_read_string(np, "adi,input-justification",
1154                                               &str);
1155                 if (ret < 0)
1156                         return ret;
1157
1158                 if (!strcmp(str, "left"))
1159                         config->input_justification =
1160                                 ADV7511_INPUT_JUSTIFICATION_LEFT;
1161                 else if (!strcmp(str, "evenly"))
1162                         config->input_justification =
1163                                 ADV7511_INPUT_JUSTIFICATION_EVENLY;
1164                 else if (!strcmp(str, "right"))
1165                         config->input_justification =
1166                                 ADV7511_INPUT_JUSTIFICATION_RIGHT;
1167                 else
1168                         return -EINVAL;
1169
1170         } else {
1171                 config->input_style = 1;
1172                 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
1173         }
1174
1175         of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
1176         if (config->clock_delay < -1200 || config->clock_delay > 1600)
1177                 return -EINVAL;
1178
1179         config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
1180
1181         /* Hardcode the sync pulse configurations for now. */
1182         config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
1183         config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1184         config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1185
1186         return 0;
1187 }
1188
1189 static int adv7533_parse_dt(struct device_node *np, struct adv7511 *adv7511)
1190 {
1191         u32 num_lanes;
1192         struct device_node *endpoint;
1193
1194         of_property_read_u32(np, "adi,dsi-lanes", &num_lanes);
1195
1196         if (num_lanes < 1 || num_lanes > 4)
1197                 return -EINVAL;
1198
1199         adv7511->num_dsi_lanes = num_lanes;
1200
1201         endpoint = of_graph_get_next_endpoint(np, NULL);
1202         if (!endpoint) {
1203                 DRM_ERROR("adv dsi input endpoint not found\n");
1204                 return -ENODEV;
1205         }
1206
1207         adv7511->host_node = of_graph_get_remote_port_parent(endpoint);
1208         if (!adv7511->host_node) {
1209                 DRM_ERROR("dsi host node not found\n");
1210                 of_node_put(endpoint);
1211                 return -ENODEV;
1212         }
1213
1214         of_node_put(endpoint);
1215         of_node_put(adv7511->host_node);
1216
1217         /* TODO: Check if these need to be parsed by DT or not */
1218         adv7511->rgb = true;
1219         adv7511->embedded_sync = false;
1220
1221         return 0;
1222 }
1223
1224 static const int edid_i2c_addr = 0x7e;
1225 static const int packet_i2c_addr = 0x70;
1226 static const int cec_i2c_addr = 0x78;
1227
1228 static const struct of_device_id adv7511_of_ids[] = {
1229         { .compatible = "adi,adv7511", .data = (void *) ADV7511 },
1230         { .compatible = "adi,adv7511w", .data = (void *) ADV7511 },
1231         { .compatible = "adi,adv7513", .data = (void *) ADV7511 },
1232         { .compatible = "adi,adv7533", .data = (void *) ADV7533 },
1233         { }
1234 };
1235 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1236
1237 static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
1238 {
1239         struct adv7511_link_config link_config;
1240         struct adv7511 *adv7511;
1241         struct device *dev = &i2c->dev;
1242         unsigned int val;
1243         int ret;
1244
1245         if (!dev->of_node)
1246                 return -EINVAL;
1247
1248         adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
1249         if (!adv7511)
1250                 return -ENOMEM;
1251
1252         adv7511->powered = false;
1253         adv7511->status = connector_status_disconnected;
1254
1255         if (dev->of_node) {
1256                 const struct of_device_id *of_id;
1257
1258                 of_id = of_match_node(adv7511_of_ids, dev->of_node);
1259                 adv7511->type = (enum adv7511_type) of_id->data;
1260         } else {
1261                 adv7511->type = id->driver_data;
1262         }
1263
1264         memset(&link_config, 0, sizeof(link_config));
1265
1266         if (adv7511->type == ADV7511)
1267                 ret = adv7511_parse_dt(dev->of_node, &link_config);
1268         else
1269                 ret = adv7533_parse_dt(dev->of_node, adv7511);
1270         if (ret)
1271                 return ret;
1272
1273         /*
1274          * The power down GPIO is optional. If present, toggle it from active to
1275          * inactive to wake up the encoder.
1276          */
1277         adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
1278         if (IS_ERR(adv7511->gpio_pd))
1279                 return PTR_ERR(adv7511->gpio_pd);
1280
1281         if (adv7511->gpio_pd) {
1282                 mdelay(5);
1283                 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
1284         }
1285
1286         adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
1287         if (IS_ERR(adv7511->regmap))
1288                 return PTR_ERR(adv7511->regmap);
1289
1290         ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
1291         if (ret)
1292                 return ret;
1293         dev_dbg(dev, "Rev. %d\n", val);
1294
1295         if (adv7511->type == ADV7511) {
1296                 ret = regmap_register_patch(adv7511->regmap,
1297                                 adv7511_fixed_registers,
1298                                 ARRAY_SIZE(adv7511_fixed_registers));
1299                 if (ret)
1300                         return ret;
1301         } else {
1302                 ret = regmap_register_patch(adv7511->regmap,
1303                                 adv7533_fixed_registers,
1304                                 ARRAY_SIZE(adv7533_fixed_registers));
1305                 if (ret)
1306                         return ret;
1307         }
1308
1309         regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
1310         regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
1311                      packet_i2c_addr);
1312         regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR, cec_i2c_addr);
1313         adv7511_packet_disable(adv7511, 0xffff);
1314
1315         adv7511->i2c_main = i2c;
1316         adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
1317         if (!adv7511->i2c_edid)
1318                 return -ENOMEM;
1319
1320         adv7511->i2c_cec = i2c_new_dummy(i2c->adapter, cec_i2c_addr >> 1);
1321         if (!adv7511->i2c_cec) {
1322                 ret = -ENOMEM;
1323                 goto err_i2c_unregister_edid;
1324         }
1325
1326         adv7511->regmap_cec = devm_regmap_init_i2c(adv7511->i2c_cec,
1327                                         &adv7533_cec_regmap_config);
1328         if (IS_ERR(adv7511->regmap_cec)) {
1329                 ret = PTR_ERR(adv7511->regmap_cec);
1330                 goto err_i2c_unregister_cec;
1331         }
1332
1333         if (adv7511->type == ADV7533) {
1334                 ret = regmap_register_patch(adv7511->regmap_cec,
1335                                 adv7533_cec_fixed_registers,
1336                                 ARRAY_SIZE(adv7533_cec_fixed_registers));
1337                 if (ret)
1338                         return ret;
1339         }
1340
1341         if (i2c->irq) {
1342                 init_waitqueue_head(&adv7511->wq);
1343
1344                 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
1345                                                 adv7511_irq_handler,
1346                                                 IRQF_ONESHOT, dev_name(dev),
1347                                                 adv7511);
1348                 if (ret)
1349                         goto err_i2c_unregister_cec;
1350         }
1351
1352         /* CEC is unused for now */
1353         regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
1354                      ADV7511_CEC_CTRL_POWER_DOWN);
1355
1356         adv7511_power_off(adv7511);
1357
1358         i2c_set_clientdata(i2c, adv7511);
1359
1360         if (adv7511->type == ADV7511)
1361                 adv7511_set_link_config(adv7511, &link_config);
1362
1363         if (adv7511->type == ADV7533) {
1364                 adv7511->bridge.funcs = &adv7533_bridge_funcs;
1365                 adv7511->bridge.of_node = dev->of_node;
1366
1367                 ret = drm_bridge_add(&adv7511->bridge);
1368                 if (ret) {
1369                         dev_err(dev, "failed to add adv7533 bridge\n");
1370                         goto err_i2c_unregister_cec;
1371                 }
1372         }
1373
1374         return 0;
1375
1376 err_i2c_unregister_cec:
1377         i2c_unregister_device(adv7511->i2c_cec);
1378 err_i2c_unregister_edid:
1379         i2c_unregister_device(adv7511->i2c_edid);
1380
1381         return ret;
1382 }
1383
1384 static int adv7511_remove(struct i2c_client *i2c)
1385 {
1386         struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1387
1388         i2c_unregister_device(adv7511->i2c_cec);
1389         i2c_unregister_device(adv7511->i2c_edid);
1390
1391         kfree(adv7511->edid);
1392
1393         if (adv7511->type == ADV7533) {
1394                 mipi_dsi_detach(adv7511->dsi);
1395                 mipi_dsi_unregister_device(adv7511->dsi);
1396                 drm_bridge_remove(&adv7511->bridge);
1397         }
1398
1399         return 0;
1400 }
1401
1402 static int adv7511_encoder_init(struct i2c_client *i2c, struct drm_device *dev,
1403                                 struct drm_encoder_slave *encoder)
1404 {
1405
1406         struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1407
1408         if (adv7511->type == ADV7533)
1409                 return -ENODEV;
1410
1411         encoder->slave_priv = adv7511;
1412         encoder->slave_funcs = &adv7511_encoder_funcs;
1413
1414         adv7511->encoder = &encoder->base;
1415
1416         return 0;
1417 }
1418
1419 static const struct i2c_device_id adv7511_i2c_ids[] = {
1420         { "adv7511", ADV7511 },
1421         { "adv7511w", ADV7511 },
1422         { "adv7513", ADV7511 },
1423         { "adv7533", ADV7533 },
1424         { }
1425 };
1426 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1427
1428 static struct drm_i2c_encoder_driver adv7511_driver = {
1429         .i2c_driver = {
1430                 .driver = {
1431                         .name = "adv7511",
1432                         .of_match_table = adv7511_of_ids,
1433                 },
1434                 .id_table = adv7511_i2c_ids,
1435                 .probe = adv7511_probe,
1436                 .remove = adv7511_remove,
1437         },
1438
1439         .encoder_init = adv7511_encoder_init,
1440 };
1441
1442 static int __init adv7511_init(void)
1443 {
1444         return drm_i2c_encoder_register(THIS_MODULE, &adv7511_driver);
1445 }
1446 module_init(adv7511_init);
1447
1448 static void __exit adv7511_exit(void)
1449 {
1450         drm_i2c_encoder_unregister(&adv7511_driver);
1451 }
1452 module_exit(adv7511_exit);
1453
1454 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1455 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1456 MODULE_LICENSE("GPL");