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