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