1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Analog Devices ADV7511 HDMI transmitter driver
4  *
5  * Copyright 2012 Analog Devices Inc.
6  */
7 
8 #include <linux/clk.h>
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 
15 #include <media/cec.h>
16 
17 #include <drm/drm_atomic.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_edid.h>
20 #include <drm/drm_print.h>
21 #include <drm/drm_probe_helper.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 || adv7511->type == ADV7535)
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 || adv7511->type == ADV7535)
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 	/*
433 	 * The bridge resets its registers on unplug. So when we get a plug
434 	 * event and we're already supposed to be powered, cycle the bridge to
435 	 * restore its state.
436 	 */
437 	if (status == connector_status_connected &&
438 	    adv7511->connector.status == connector_status_disconnected &&
439 	    adv7511->powered) {
440 		regcache_mark_dirty(adv7511->regmap);
441 		adv7511_power_on(adv7511);
442 	}
443 
444 	if (adv7511->connector.status != status) {
445 		adv7511->connector.status = status;
446 
447 		if (adv7511->connector.dev) {
448 			if (status == connector_status_disconnected)
449 				cec_phys_addr_invalidate(adv7511->cec_adap);
450 			drm_kms_helper_hotplug_event(adv7511->connector.dev);
451 		} else {
452 			drm_bridge_hpd_notify(&adv7511->bridge, status);
453 		}
454 	}
455 }
456 
457 static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
458 {
459 	unsigned int irq0, irq1;
460 	int ret;
461 
462 	ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
463 	if (ret < 0)
464 		return ret;
465 
466 	ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
467 	if (ret < 0)
468 		return ret;
469 
470 	regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
471 	regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
472 
473 	if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder)
474 		schedule_work(&adv7511->hpd_work);
475 
476 	if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
477 		adv7511->edid_read = true;
478 
479 		if (adv7511->i2c_main->irq)
480 			wake_up_all(&adv7511->wq);
481 	}
482 
483 #ifdef CONFIG_DRM_I2C_ADV7511_CEC
484 	adv7511_cec_irq_process(adv7511, irq1);
485 #endif
486 
487 	return 0;
488 }
489 
490 static irqreturn_t adv7511_irq_handler(int irq, void *devid)
491 {
492 	struct adv7511 *adv7511 = devid;
493 	int ret;
494 
495 	ret = adv7511_irq_process(adv7511, true);
496 	return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
497 }
498 
499 /* -----------------------------------------------------------------------------
500  * EDID retrieval
501  */
502 
503 static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
504 {
505 	int ret;
506 
507 	if (adv7511->i2c_main->irq) {
508 		ret = wait_event_interruptible_timeout(adv7511->wq,
509 				adv7511->edid_read, msecs_to_jiffies(timeout));
510 	} else {
511 		for (; timeout > 0; timeout -= 25) {
512 			ret = adv7511_irq_process(adv7511, false);
513 			if (ret < 0)
514 				break;
515 
516 			if (adv7511->edid_read)
517 				break;
518 
519 			msleep(25);
520 		}
521 	}
522 
523 	return adv7511->edid_read ? 0 : -EIO;
524 }
525 
526 static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
527 				  size_t len)
528 {
529 	struct adv7511 *adv7511 = data;
530 	struct i2c_msg xfer[2];
531 	uint8_t offset;
532 	unsigned int i;
533 	int ret;
534 
535 	if (len > 128)
536 		return -EINVAL;
537 
538 	if (adv7511->current_edid_segment != block / 2) {
539 		unsigned int status;
540 
541 		ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
542 				  &status);
543 		if (ret < 0)
544 			return ret;
545 
546 		if (status != 2) {
547 			adv7511->edid_read = false;
548 			regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
549 				     block);
550 			ret = adv7511_wait_for_edid(adv7511, 200);
551 			if (ret < 0)
552 				return ret;
553 		}
554 
555 		/* Break this apart, hopefully more I2C controllers will
556 		 * support 64 byte transfers than 256 byte transfers
557 		 */
558 
559 		xfer[0].addr = adv7511->i2c_edid->addr;
560 		xfer[0].flags = 0;
561 		xfer[0].len = 1;
562 		xfer[0].buf = &offset;
563 		xfer[1].addr = adv7511->i2c_edid->addr;
564 		xfer[1].flags = I2C_M_RD;
565 		xfer[1].len = 64;
566 		xfer[1].buf = adv7511->edid_buf;
567 
568 		offset = 0;
569 
570 		for (i = 0; i < 4; ++i) {
571 			ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
572 					   ARRAY_SIZE(xfer));
573 			if (ret < 0)
574 				return ret;
575 			else if (ret != 2)
576 				return -EIO;
577 
578 			xfer[1].buf += 64;
579 			offset += 64;
580 		}
581 
582 		adv7511->current_edid_segment = block / 2;
583 	}
584 
585 	if (block % 2 == 0)
586 		memcpy(buf, adv7511->edid_buf, len);
587 	else
588 		memcpy(buf, adv7511->edid_buf + 128, len);
589 
590 	return 0;
591 }
592 
593 /* -----------------------------------------------------------------------------
594  * ADV75xx helpers
595  */
596 
597 static struct edid *adv7511_get_edid(struct adv7511 *adv7511,
598 				     struct drm_connector *connector)
599 {
600 	struct edid *edid;
601 
602 	/* Reading the EDID only works if the device is powered */
603 	if (!adv7511->powered) {
604 		unsigned int edid_i2c_addr =
605 					(adv7511->i2c_edid->addr << 1);
606 
607 		__adv7511_power_on(adv7511);
608 
609 		/* Reset the EDID_I2C_ADDR register as it might be cleared */
610 		regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
611 			     edid_i2c_addr);
612 	}
613 
614 	edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
615 
616 	if (!adv7511->powered)
617 		__adv7511_power_off(adv7511);
618 
619 	adv7511_set_config_csc(adv7511, connector, adv7511->rgb,
620 			       drm_detect_hdmi_monitor(edid));
621 
622 	cec_s_phys_addr_from_edid(adv7511->cec_adap, edid);
623 
624 	return edid;
625 }
626 
627 static int adv7511_get_modes(struct adv7511 *adv7511,
628 			     struct drm_connector *connector)
629 {
630 	struct edid *edid;
631 	unsigned int count;
632 
633 	edid = adv7511_get_edid(adv7511, connector);
634 
635 	drm_connector_update_edid_property(connector, edid);
636 	count = drm_add_edid_modes(connector, edid);
637 
638 	kfree(edid);
639 
640 	return count;
641 }
642 
643 static enum drm_connector_status
644 adv7511_detect(struct adv7511 *adv7511, struct drm_connector *connector)
645 {
646 	enum drm_connector_status status;
647 	unsigned int val;
648 	bool hpd;
649 	int ret;
650 
651 	ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
652 	if (ret < 0)
653 		return connector_status_disconnected;
654 
655 	if (val & ADV7511_STATUS_HPD)
656 		status = connector_status_connected;
657 	else
658 		status = connector_status_disconnected;
659 
660 	hpd = adv7511_hpd(adv7511);
661 
662 	/* The chip resets itself when the cable is disconnected, so in case
663 	 * there is a pending HPD interrupt and the cable is connected there was
664 	 * at least one transition from disconnected to connected and the chip
665 	 * has to be reinitialized. */
666 	if (status == connector_status_connected && hpd && adv7511->powered) {
667 		regcache_mark_dirty(adv7511->regmap);
668 		adv7511_power_on(adv7511);
669 		if (connector)
670 			adv7511_get_modes(adv7511, connector);
671 		if (adv7511->status == connector_status_connected)
672 			status = connector_status_disconnected;
673 	} else {
674 		/* Renable HPD sensing */
675 		regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
676 				   ADV7511_REG_POWER2_HPD_SRC_MASK,
677 				   ADV7511_REG_POWER2_HPD_SRC_BOTH);
678 	}
679 
680 	adv7511->status = status;
681 	return status;
682 }
683 
684 static enum drm_mode_status adv7511_mode_valid(struct adv7511 *adv7511,
685 			      struct drm_display_mode *mode)
686 {
687 	if (mode->clock > 165000)
688 		return MODE_CLOCK_HIGH;
689 
690 	return MODE_OK;
691 }
692 
693 static void adv7511_mode_set(struct adv7511 *adv7511,
694 			     const struct drm_display_mode *mode,
695 			     const struct drm_display_mode *adj_mode)
696 {
697 	unsigned int low_refresh_rate;
698 	unsigned int hsync_polarity = 0;
699 	unsigned int vsync_polarity = 0;
700 
701 	if (adv7511->embedded_sync) {
702 		unsigned int hsync_offset, hsync_len;
703 		unsigned int vsync_offset, vsync_len;
704 
705 		hsync_offset = adj_mode->crtc_hsync_start -
706 			       adj_mode->crtc_hdisplay;
707 		vsync_offset = adj_mode->crtc_vsync_start -
708 			       adj_mode->crtc_vdisplay;
709 		hsync_len = adj_mode->crtc_hsync_end -
710 			    adj_mode->crtc_hsync_start;
711 		vsync_len = adj_mode->crtc_vsync_end -
712 			    adj_mode->crtc_vsync_start;
713 
714 		/* The hardware vsync generator has a off-by-one bug */
715 		vsync_offset += 1;
716 
717 		regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
718 			     ((hsync_offset >> 10) & 0x7) << 5);
719 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
720 			     (hsync_offset >> 2) & 0xff);
721 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
722 			     ((hsync_offset & 0x3) << 6) |
723 			     ((hsync_len >> 4) & 0x3f));
724 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
725 			     ((hsync_len & 0xf) << 4) |
726 			     ((vsync_offset >> 6) & 0xf));
727 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
728 			     ((vsync_offset & 0x3f) << 2) |
729 			     ((vsync_len >> 8) & 0x3));
730 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
731 			     vsync_len & 0xff);
732 
733 		hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
734 		vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
735 	} else {
736 		enum adv7511_sync_polarity mode_hsync_polarity;
737 		enum adv7511_sync_polarity mode_vsync_polarity;
738 
739 		/**
740 		 * If the input signal is always low or always high we want to
741 		 * invert or let it passthrough depending on the polarity of the
742 		 * current mode.
743 		 **/
744 		if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
745 			mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
746 		else
747 			mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
748 
749 		if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
750 			mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
751 		else
752 			mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
753 
754 		if (adv7511->hsync_polarity != mode_hsync_polarity &&
755 		    adv7511->hsync_polarity !=
756 		    ADV7511_SYNC_POLARITY_PASSTHROUGH)
757 			hsync_polarity = 1;
758 
759 		if (adv7511->vsync_polarity != mode_vsync_polarity &&
760 		    adv7511->vsync_polarity !=
761 		    ADV7511_SYNC_POLARITY_PASSTHROUGH)
762 			vsync_polarity = 1;
763 	}
764 
765 	if (drm_mode_vrefresh(mode) <= 24)
766 		low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
767 	else if (drm_mode_vrefresh(mode) <= 25)
768 		low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
769 	else if (drm_mode_vrefresh(mode) <= 30)
770 		low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
771 	else
772 		low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
773 
774 	regmap_update_bits(adv7511->regmap, 0xfb,
775 		0x6, low_refresh_rate << 1);
776 	regmap_update_bits(adv7511->regmap, 0x17,
777 		0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
778 
779 	if (adv7511->type == ADV7533 || adv7511->type == ADV7535)
780 		adv7533_mode_set(adv7511, adj_mode);
781 
782 	drm_mode_copy(&adv7511->curr_mode, adj_mode);
783 
784 	/*
785 	 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
786 	 * supposed to give better results.
787 	 */
788 
789 	adv7511->f_tmds = mode->clock;
790 }
791 
792 /* -----------------------------------------------------------------------------
793  * DRM Connector Operations
794  */
795 
796 static struct adv7511 *connector_to_adv7511(struct drm_connector *connector)
797 {
798 	return container_of(connector, struct adv7511, connector);
799 }
800 
801 static int adv7511_connector_get_modes(struct drm_connector *connector)
802 {
803 	struct adv7511 *adv = connector_to_adv7511(connector);
804 
805 	return adv7511_get_modes(adv, connector);
806 }
807 
808 static enum drm_mode_status
809 adv7511_connector_mode_valid(struct drm_connector *connector,
810 			     struct drm_display_mode *mode)
811 {
812 	struct adv7511 *adv = connector_to_adv7511(connector);
813 
814 	return adv7511_mode_valid(adv, mode);
815 }
816 
817 static struct drm_connector_helper_funcs adv7511_connector_helper_funcs = {
818 	.get_modes = adv7511_connector_get_modes,
819 	.mode_valid = adv7511_connector_mode_valid,
820 };
821 
822 static enum drm_connector_status
823 adv7511_connector_detect(struct drm_connector *connector, bool force)
824 {
825 	struct adv7511 *adv = connector_to_adv7511(connector);
826 
827 	return adv7511_detect(adv, connector);
828 }
829 
830 static const struct drm_connector_funcs adv7511_connector_funcs = {
831 	.fill_modes = drm_helper_probe_single_connector_modes,
832 	.detect = adv7511_connector_detect,
833 	.destroy = drm_connector_cleanup,
834 	.reset = drm_atomic_helper_connector_reset,
835 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
836 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
837 };
838 
839 static int adv7511_connector_init(struct adv7511 *adv)
840 {
841 	struct drm_bridge *bridge = &adv->bridge;
842 	int ret;
843 
844 	if (!bridge->encoder) {
845 		DRM_ERROR("Parent encoder object not found");
846 		return -ENODEV;
847 	}
848 
849 	if (adv->i2c_main->irq)
850 		adv->connector.polled = DRM_CONNECTOR_POLL_HPD;
851 	else
852 		adv->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
853 				DRM_CONNECTOR_POLL_DISCONNECT;
854 
855 	ret = drm_connector_init(bridge->dev, &adv->connector,
856 				 &adv7511_connector_funcs,
857 				 DRM_MODE_CONNECTOR_HDMIA);
858 	if (ret < 0) {
859 		DRM_ERROR("Failed to initialize connector with drm\n");
860 		return ret;
861 	}
862 	drm_connector_helper_add(&adv->connector,
863 				 &adv7511_connector_helper_funcs);
864 	drm_connector_attach_encoder(&adv->connector, bridge->encoder);
865 
866 	return 0;
867 }
868 
869 /* -----------------------------------------------------------------------------
870  * DRM Bridge Operations
871  */
872 
873 static struct adv7511 *bridge_to_adv7511(struct drm_bridge *bridge)
874 {
875 	return container_of(bridge, struct adv7511, bridge);
876 }
877 
878 static void adv7511_bridge_enable(struct drm_bridge *bridge)
879 {
880 	struct adv7511 *adv = bridge_to_adv7511(bridge);
881 
882 	adv7511_power_on(adv);
883 }
884 
885 static void adv7511_bridge_disable(struct drm_bridge *bridge)
886 {
887 	struct adv7511 *adv = bridge_to_adv7511(bridge);
888 
889 	adv7511_power_off(adv);
890 }
891 
892 static void adv7511_bridge_mode_set(struct drm_bridge *bridge,
893 				    const struct drm_display_mode *mode,
894 				    const struct drm_display_mode *adj_mode)
895 {
896 	struct adv7511 *adv = bridge_to_adv7511(bridge);
897 
898 	adv7511_mode_set(adv, mode, adj_mode);
899 }
900 
901 static int adv7511_bridge_attach(struct drm_bridge *bridge,
902 				 enum drm_bridge_attach_flags flags)
903 {
904 	struct adv7511 *adv = bridge_to_adv7511(bridge);
905 	int ret = 0;
906 
907 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
908 		ret = adv7511_connector_init(adv);
909 		if (ret < 0)
910 			return ret;
911 	}
912 
913 	if (adv->i2c_main->irq)
914 		regmap_write(adv->regmap, ADV7511_REG_INT_ENABLE(0),
915 			     ADV7511_INT0_HPD);
916 
917 	return ret;
918 }
919 
920 static enum drm_connector_status adv7511_bridge_detect(struct drm_bridge *bridge)
921 {
922 	struct adv7511 *adv = bridge_to_adv7511(bridge);
923 
924 	return adv7511_detect(adv, NULL);
925 }
926 
927 static struct edid *adv7511_bridge_get_edid(struct drm_bridge *bridge,
928 					    struct drm_connector *connector)
929 {
930 	struct adv7511 *adv = bridge_to_adv7511(bridge);
931 
932 	return adv7511_get_edid(adv, connector);
933 }
934 
935 static void adv7511_bridge_hpd_notify(struct drm_bridge *bridge,
936 				      enum drm_connector_status status)
937 {
938 	struct adv7511 *adv = bridge_to_adv7511(bridge);
939 
940 	if (status == connector_status_disconnected)
941 		cec_phys_addr_invalidate(adv->cec_adap);
942 }
943 
944 static const struct drm_bridge_funcs adv7511_bridge_funcs = {
945 	.enable = adv7511_bridge_enable,
946 	.disable = adv7511_bridge_disable,
947 	.mode_set = adv7511_bridge_mode_set,
948 	.attach = adv7511_bridge_attach,
949 	.detect = adv7511_bridge_detect,
950 	.get_edid = adv7511_bridge_get_edid,
951 	.hpd_notify = adv7511_bridge_hpd_notify,
952 };
953 
954 /* -----------------------------------------------------------------------------
955  * Probe & remove
956  */
957 
958 static const char * const adv7511_supply_names[] = {
959 	"avdd",
960 	"dvdd",
961 	"pvdd",
962 	"bgvdd",
963 	"dvdd-3v",
964 };
965 
966 static const char * const adv7533_supply_names[] = {
967 	"avdd",
968 	"dvdd",
969 	"pvdd",
970 	"a2vdd",
971 	"v3p3",
972 	"v1p2",
973 };
974 
975 static int adv7511_init_regulators(struct adv7511 *adv)
976 {
977 	struct device *dev = &adv->i2c_main->dev;
978 	const char * const *supply_names;
979 	unsigned int i;
980 	int ret;
981 
982 	if (adv->type == ADV7511) {
983 		supply_names = adv7511_supply_names;
984 		adv->num_supplies = ARRAY_SIZE(adv7511_supply_names);
985 	} else {
986 		supply_names = adv7533_supply_names;
987 		adv->num_supplies = ARRAY_SIZE(adv7533_supply_names);
988 	}
989 
990 	adv->supplies = devm_kcalloc(dev, adv->num_supplies,
991 				     sizeof(*adv->supplies), GFP_KERNEL);
992 	if (!adv->supplies)
993 		return -ENOMEM;
994 
995 	for (i = 0; i < adv->num_supplies; i++)
996 		adv->supplies[i].supply = supply_names[i];
997 
998 	ret = devm_regulator_bulk_get(dev, adv->num_supplies, adv->supplies);
999 	if (ret)
1000 		return ret;
1001 
1002 	return regulator_bulk_enable(adv->num_supplies, adv->supplies);
1003 }
1004 
1005 static void adv7511_uninit_regulators(struct adv7511 *adv)
1006 {
1007 	regulator_bulk_disable(adv->num_supplies, adv->supplies);
1008 }
1009 
1010 static bool adv7511_cec_register_volatile(struct device *dev, unsigned int reg)
1011 {
1012 	struct i2c_client *i2c = to_i2c_client(dev);
1013 	struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1014 
1015 	if (adv7511->type == ADV7533 || adv7511->type == ADV7535)
1016 		reg -= ADV7533_REG_CEC_OFFSET;
1017 
1018 	switch (reg) {
1019 	case ADV7511_REG_CEC_RX_FRAME_HDR:
1020 	case ADV7511_REG_CEC_RX_FRAME_DATA0...
1021 		ADV7511_REG_CEC_RX_FRAME_DATA0 + 14:
1022 	case ADV7511_REG_CEC_RX_FRAME_LEN:
1023 	case ADV7511_REG_CEC_RX_BUFFERS:
1024 	case ADV7511_REG_CEC_TX_LOW_DRV_CNT:
1025 		return true;
1026 	}
1027 
1028 	return false;
1029 }
1030 
1031 static const struct regmap_config adv7511_cec_regmap_config = {
1032 	.reg_bits = 8,
1033 	.val_bits = 8,
1034 
1035 	.max_register = 0xff,
1036 	.cache_type = REGCACHE_RBTREE,
1037 	.volatile_reg = adv7511_cec_register_volatile,
1038 };
1039 
1040 static int adv7511_init_cec_regmap(struct adv7511 *adv)
1041 {
1042 	int ret;
1043 
1044 	adv->i2c_cec = i2c_new_ancillary_device(adv->i2c_main, "cec",
1045 						ADV7511_CEC_I2C_ADDR_DEFAULT);
1046 	if (IS_ERR(adv->i2c_cec))
1047 		return PTR_ERR(adv->i2c_cec);
1048 	i2c_set_clientdata(adv->i2c_cec, adv);
1049 
1050 	adv->regmap_cec = devm_regmap_init_i2c(adv->i2c_cec,
1051 					&adv7511_cec_regmap_config);
1052 	if (IS_ERR(adv->regmap_cec)) {
1053 		ret = PTR_ERR(adv->regmap_cec);
1054 		goto err;
1055 	}
1056 
1057 	if (adv->type == ADV7533 || adv->type == ADV7535) {
1058 		ret = adv7533_patch_cec_registers(adv);
1059 		if (ret)
1060 			goto err;
1061 	}
1062 
1063 	return 0;
1064 err:
1065 	i2c_unregister_device(adv->i2c_cec);
1066 	return ret;
1067 }
1068 
1069 static int adv7511_parse_dt(struct device_node *np,
1070 			    struct adv7511_link_config *config)
1071 {
1072 	const char *str;
1073 	int ret;
1074 
1075 	of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
1076 	if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
1077 	    config->input_color_depth != 12)
1078 		return -EINVAL;
1079 
1080 	ret = of_property_read_string(np, "adi,input-colorspace", &str);
1081 	if (ret < 0)
1082 		return ret;
1083 
1084 	if (!strcmp(str, "rgb"))
1085 		config->input_colorspace = HDMI_COLORSPACE_RGB;
1086 	else if (!strcmp(str, "yuv422"))
1087 		config->input_colorspace = HDMI_COLORSPACE_YUV422;
1088 	else if (!strcmp(str, "yuv444"))
1089 		config->input_colorspace = HDMI_COLORSPACE_YUV444;
1090 	else
1091 		return -EINVAL;
1092 
1093 	ret = of_property_read_string(np, "adi,input-clock", &str);
1094 	if (ret < 0)
1095 		return ret;
1096 
1097 	if (!strcmp(str, "1x"))
1098 		config->input_clock = ADV7511_INPUT_CLOCK_1X;
1099 	else if (!strcmp(str, "2x"))
1100 		config->input_clock = ADV7511_INPUT_CLOCK_2X;
1101 	else if (!strcmp(str, "ddr"))
1102 		config->input_clock = ADV7511_INPUT_CLOCK_DDR;
1103 	else
1104 		return -EINVAL;
1105 
1106 	if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
1107 	    config->input_clock != ADV7511_INPUT_CLOCK_1X) {
1108 		ret = of_property_read_u32(np, "adi,input-style",
1109 					   &config->input_style);
1110 		if (ret)
1111 			return ret;
1112 
1113 		if (config->input_style < 1 || config->input_style > 3)
1114 			return -EINVAL;
1115 
1116 		ret = of_property_read_string(np, "adi,input-justification",
1117 					      &str);
1118 		if (ret < 0)
1119 			return ret;
1120 
1121 		if (!strcmp(str, "left"))
1122 			config->input_justification =
1123 				ADV7511_INPUT_JUSTIFICATION_LEFT;
1124 		else if (!strcmp(str, "evenly"))
1125 			config->input_justification =
1126 				ADV7511_INPUT_JUSTIFICATION_EVENLY;
1127 		else if (!strcmp(str, "right"))
1128 			config->input_justification =
1129 				ADV7511_INPUT_JUSTIFICATION_RIGHT;
1130 		else
1131 			return -EINVAL;
1132 
1133 	} else {
1134 		config->input_style = 1;
1135 		config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
1136 	}
1137 
1138 	of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
1139 	if (config->clock_delay < -1200 || config->clock_delay > 1600)
1140 		return -EINVAL;
1141 
1142 	config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
1143 
1144 	/* Hardcode the sync pulse configurations for now. */
1145 	config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
1146 	config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1147 	config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1148 
1149 	return 0;
1150 }
1151 
1152 static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
1153 {
1154 	struct adv7511_link_config link_config;
1155 	struct adv7511 *adv7511;
1156 	struct device *dev = &i2c->dev;
1157 	unsigned int val;
1158 	int ret;
1159 
1160 	if (!dev->of_node)
1161 		return -EINVAL;
1162 
1163 	adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
1164 	if (!adv7511)
1165 		return -ENOMEM;
1166 
1167 	adv7511->i2c_main = i2c;
1168 	adv7511->powered = false;
1169 	adv7511->status = connector_status_disconnected;
1170 
1171 	if (dev->of_node)
1172 		adv7511->type = (enum adv7511_type)of_device_get_match_data(dev);
1173 	else
1174 		adv7511->type = id->driver_data;
1175 
1176 	memset(&link_config, 0, sizeof(link_config));
1177 
1178 	if (adv7511->type == ADV7511)
1179 		ret = adv7511_parse_dt(dev->of_node, &link_config);
1180 	else
1181 		ret = adv7533_parse_dt(dev->of_node, adv7511);
1182 	if (ret)
1183 		return ret;
1184 
1185 	ret = adv7511_init_regulators(adv7511);
1186 	if (ret) {
1187 		dev_err(dev, "failed to init regulators\n");
1188 		return ret;
1189 	}
1190 
1191 	/*
1192 	 * The power down GPIO is optional. If present, toggle it from active to
1193 	 * inactive to wake up the encoder.
1194 	 */
1195 	adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
1196 	if (IS_ERR(adv7511->gpio_pd)) {
1197 		ret = PTR_ERR(adv7511->gpio_pd);
1198 		goto uninit_regulators;
1199 	}
1200 
1201 	if (adv7511->gpio_pd) {
1202 		usleep_range(5000, 6000);
1203 		gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
1204 	}
1205 
1206 	adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
1207 	if (IS_ERR(adv7511->regmap)) {
1208 		ret = PTR_ERR(adv7511->regmap);
1209 		goto uninit_regulators;
1210 	}
1211 
1212 	ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
1213 	if (ret)
1214 		goto uninit_regulators;
1215 	dev_dbg(dev, "Rev. %d\n", val);
1216 
1217 	if (adv7511->type == ADV7511)
1218 		ret = regmap_register_patch(adv7511->regmap,
1219 					    adv7511_fixed_registers,
1220 					    ARRAY_SIZE(adv7511_fixed_registers));
1221 	else
1222 		ret = adv7533_patch_registers(adv7511);
1223 	if (ret)
1224 		goto uninit_regulators;
1225 
1226 	adv7511_packet_disable(adv7511, 0xffff);
1227 
1228 	adv7511->i2c_edid = i2c_new_ancillary_device(i2c, "edid",
1229 					ADV7511_EDID_I2C_ADDR_DEFAULT);
1230 	if (IS_ERR(adv7511->i2c_edid)) {
1231 		ret = PTR_ERR(adv7511->i2c_edid);
1232 		goto uninit_regulators;
1233 	}
1234 
1235 	regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
1236 		     adv7511->i2c_edid->addr << 1);
1237 
1238 	adv7511->i2c_packet = i2c_new_ancillary_device(i2c, "packet",
1239 					ADV7511_PACKET_I2C_ADDR_DEFAULT);
1240 	if (IS_ERR(adv7511->i2c_packet)) {
1241 		ret = PTR_ERR(adv7511->i2c_packet);
1242 		goto err_i2c_unregister_edid;
1243 	}
1244 
1245 	regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
1246 		     adv7511->i2c_packet->addr << 1);
1247 
1248 	ret = adv7511_init_cec_regmap(adv7511);
1249 	if (ret)
1250 		goto err_i2c_unregister_packet;
1251 
1252 	regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR,
1253 		     adv7511->i2c_cec->addr << 1);
1254 
1255 	INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
1256 
1257 	if (i2c->irq) {
1258 		init_waitqueue_head(&adv7511->wq);
1259 
1260 		ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
1261 						adv7511_irq_handler,
1262 						IRQF_ONESHOT, dev_name(dev),
1263 						adv7511);
1264 		if (ret)
1265 			goto err_unregister_cec;
1266 	}
1267 
1268 	adv7511_power_off(adv7511);
1269 
1270 	i2c_set_clientdata(i2c, adv7511);
1271 
1272 	if (adv7511->type == ADV7511)
1273 		adv7511_set_link_config(adv7511, &link_config);
1274 
1275 	ret = adv7511_cec_init(dev, adv7511);
1276 	if (ret)
1277 		goto err_unregister_cec;
1278 
1279 	adv7511->bridge.funcs = &adv7511_bridge_funcs;
1280 	adv7511->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID
1281 			    | DRM_BRIDGE_OP_HPD;
1282 	adv7511->bridge.of_node = dev->of_node;
1283 	adv7511->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
1284 
1285 	drm_bridge_add(&adv7511->bridge);
1286 
1287 	adv7511_audio_init(dev, adv7511);
1288 
1289 	if (adv7511->type == ADV7533 || adv7511->type == ADV7535) {
1290 		ret = adv7533_attach_dsi(adv7511);
1291 		if (ret)
1292 			goto err_unregister_audio;
1293 	}
1294 
1295 	return 0;
1296 
1297 err_unregister_audio:
1298 	adv7511_audio_exit(adv7511);
1299 	drm_bridge_remove(&adv7511->bridge);
1300 err_unregister_cec:
1301 	i2c_unregister_device(adv7511->i2c_cec);
1302 	clk_disable_unprepare(adv7511->cec_clk);
1303 err_i2c_unregister_packet:
1304 	i2c_unregister_device(adv7511->i2c_packet);
1305 err_i2c_unregister_edid:
1306 	i2c_unregister_device(adv7511->i2c_edid);
1307 uninit_regulators:
1308 	adv7511_uninit_regulators(adv7511);
1309 
1310 	return ret;
1311 }
1312 
1313 static int adv7511_remove(struct i2c_client *i2c)
1314 {
1315 	struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1316 
1317 	i2c_unregister_device(adv7511->i2c_cec);
1318 	clk_disable_unprepare(adv7511->cec_clk);
1319 
1320 	adv7511_uninit_regulators(adv7511);
1321 
1322 	drm_bridge_remove(&adv7511->bridge);
1323 
1324 	adv7511_audio_exit(adv7511);
1325 
1326 	cec_unregister_adapter(adv7511->cec_adap);
1327 
1328 	i2c_unregister_device(adv7511->i2c_packet);
1329 	i2c_unregister_device(adv7511->i2c_edid);
1330 
1331 	return 0;
1332 }
1333 
1334 static const struct i2c_device_id adv7511_i2c_ids[] = {
1335 	{ "adv7511", ADV7511 },
1336 	{ "adv7511w", ADV7511 },
1337 	{ "adv7513", ADV7511 },
1338 	{ "adv7533", ADV7533 },
1339 	{ "adv7535", ADV7535 },
1340 	{ }
1341 };
1342 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1343 
1344 static const struct of_device_id adv7511_of_ids[] = {
1345 	{ .compatible = "adi,adv7511", .data = (void *)ADV7511 },
1346 	{ .compatible = "adi,adv7511w", .data = (void *)ADV7511 },
1347 	{ .compatible = "adi,adv7513", .data = (void *)ADV7511 },
1348 	{ .compatible = "adi,adv7533", .data = (void *)ADV7533 },
1349 	{ .compatible = "adi,adv7535", .data = (void *)ADV7535 },
1350 	{ }
1351 };
1352 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1353 
1354 static struct mipi_dsi_driver adv7533_dsi_driver = {
1355 	.driver.name = "adv7533",
1356 };
1357 
1358 static struct i2c_driver adv7511_driver = {
1359 	.driver = {
1360 		.name = "adv7511",
1361 		.of_match_table = adv7511_of_ids,
1362 	},
1363 	.id_table = adv7511_i2c_ids,
1364 	.probe = adv7511_probe,
1365 	.remove = adv7511_remove,
1366 };
1367 
1368 static int __init adv7511_init(void)
1369 {
1370 	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1371 		mipi_dsi_driver_register(&adv7533_dsi_driver);
1372 
1373 	return i2c_add_driver(&adv7511_driver);
1374 }
1375 module_init(adv7511_init);
1376 
1377 static void __exit adv7511_exit(void)
1378 {
1379 	i2c_del_driver(&adv7511_driver);
1380 
1381 	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1382 		mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1383 }
1384 module_exit(adv7511_exit);
1385 
1386 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1387 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1388 MODULE_LICENSE("GPL");
1389