xref: /linux/drivers/gpu/drm/bridge/adv7511/adv7511_drv.c (revision b4ada0618eed0fbd1b1630f73deb048c592b06a1)
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.h>
13 #include <linux/slab.h>
14 
15 #include <sound/pcm.h>
16 
17 #include <drm/drm_atomic.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_bridge_connector.h>
20 #include <drm/drm_edid.h>
21 #include <drm/drm_of.h>
22 #include <drm/drm_print.h>
23 #include <drm/drm_probe_helper.h>
24 #include <drm/display/drm_hdmi_helper.h>
25 #include <drm/display/drm_hdmi_state_helper.h>
26 
27 #include "adv7511.h"
28 
29 /* ADI recommended values for proper operation. */
30 static const struct reg_sequence adv7511_fixed_registers[] = {
31 	{ 0x98, 0x03 },
32 	{ 0x9a, 0xe0 },
33 	{ 0x9c, 0x30 },
34 	{ 0x9d, 0x61 },
35 	{ 0xa2, 0xa4 },
36 	{ 0xa3, 0xa4 },
37 	{ 0xe0, 0xd0 },
38 	{ 0xf9, 0x00 },
39 	{ 0x55, 0x02 },
40 };
41 
42 /* -----------------------------------------------------------------------------
43  * Register access
44  */
45 
46 static const uint8_t adv7511_register_defaults[] = {
47 	0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
48 	0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
49 	0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
50 	0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
51 	0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
52 	0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
53 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
54 	0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
55 	0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
56 	0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
57 	0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
58 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
60 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 	0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
62 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
64 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 	0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
66 	0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
67 	0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
68 	0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
69 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
70 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
72 	0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
73 	0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
74 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
75 	0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
76 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 	0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
78 	0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 };
80 
81 static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
82 {
83 	switch (reg) {
84 	case ADV7511_REG_CHIP_REVISION:
85 	case ADV7511_REG_SPDIF_FREQ:
86 	case ADV7511_REG_CTS_AUTOMATIC1:
87 	case ADV7511_REG_CTS_AUTOMATIC2:
88 	case ADV7511_REG_VIC_DETECTED:
89 	case ADV7511_REG_VIC_SEND:
90 	case ADV7511_REG_AUX_VIC_DETECTED:
91 	case ADV7511_REG_STATUS:
92 	case ADV7511_REG_GC(1):
93 	case ADV7511_REG_INT(0):
94 	case ADV7511_REG_INT(1):
95 	case ADV7511_REG_PLL_STATUS:
96 	case ADV7511_REG_AN(0):
97 	case ADV7511_REG_AN(1):
98 	case ADV7511_REG_AN(2):
99 	case ADV7511_REG_AN(3):
100 	case ADV7511_REG_AN(4):
101 	case ADV7511_REG_AN(5):
102 	case ADV7511_REG_AN(6):
103 	case ADV7511_REG_AN(7):
104 	case ADV7511_REG_HDCP_STATUS:
105 	case ADV7511_REG_BCAPS:
106 	case ADV7511_REG_BKSV(0):
107 	case ADV7511_REG_BKSV(1):
108 	case ADV7511_REG_BKSV(2):
109 	case ADV7511_REG_BKSV(3):
110 	case ADV7511_REG_BKSV(4):
111 	case ADV7511_REG_DDC_STATUS:
112 	case ADV7511_REG_EDID_READ_CTRL:
113 	case ADV7511_REG_BSTATUS(0):
114 	case ADV7511_REG_BSTATUS(1):
115 	case ADV7511_REG_CHIP_ID_HIGH:
116 	case ADV7511_REG_CHIP_ID_LOW:
117 		return true;
118 	}
119 
120 	return false;
121 }
122 
123 static const struct regmap_config adv7511_regmap_config = {
124 	.reg_bits = 8,
125 	.val_bits = 8,
126 
127 	.max_register = 0xff,
128 	.cache_type = REGCACHE_MAPLE,
129 	.reg_defaults_raw = adv7511_register_defaults,
130 	.num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
131 
132 	.volatile_reg = adv7511_register_volatile,
133 };
134 
135 /* -----------------------------------------------------------------------------
136  * Hardware configuration
137  */
138 
139 static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
140 				 const uint16_t *coeff,
141 				 unsigned int scaling_factor)
142 {
143 	unsigned int i;
144 
145 	regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
146 			   ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
147 
148 	if (enable) {
149 		for (i = 0; i < 12; ++i) {
150 			regmap_update_bits(adv7511->regmap,
151 					   ADV7511_REG_CSC_UPPER(i),
152 					   0x1f, coeff[i] >> 8);
153 			regmap_write(adv7511->regmap,
154 				     ADV7511_REG_CSC_LOWER(i),
155 				     coeff[i] & 0xff);
156 		}
157 	}
158 
159 	if (enable)
160 		regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
161 				   0xe0, 0x80 | (scaling_factor << 5));
162 	else
163 		regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
164 				   0x80, 0x00);
165 
166 	regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
167 			   ADV7511_CSC_UPDATE_MODE, 0);
168 }
169 
170 static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
171 {
172 	if (packet & 0xff)
173 		regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
174 				   packet, 0xff);
175 
176 	if (packet & 0xff00) {
177 		packet >>= 8;
178 		regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
179 				   packet, 0xff);
180 	}
181 
182 	return 0;
183 }
184 
185 static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
186 {
187 	if (packet & 0xff)
188 		regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
189 				   packet, 0x00);
190 
191 	if (packet & 0xff00) {
192 		packet >>= 8;
193 		regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
194 				   packet, 0x00);
195 	}
196 
197 	return 0;
198 }
199 
200 /* Coefficients for adv7511 color space conversion */
201 static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
202 	0x0734, 0x04ad, 0x0000, 0x1c1b,
203 	0x1ddc, 0x04ad, 0x1f24, 0x0135,
204 	0x0000, 0x04ad, 0x087c, 0x1b77,
205 };
206 
207 static void adv7511_set_config_csc(struct adv7511 *adv7511,
208 				   struct drm_connector *connector,
209 				   bool rgb)
210 {
211 	struct adv7511_video_config config;
212 	bool output_format_422, output_format_ycbcr;
213 	unsigned int mode;
214 
215 	if (rgb) {
216 		config.csc_enable = false;
217 		output_format_422 = false;
218 		output_format_ycbcr = false;
219 	} else {
220 		config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
221 		config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
222 
223 		if ((connector->display_info.color_formats &
224 		     DRM_COLOR_FORMAT_YCBCR422) &&
225 		    connector->display_info.is_hdmi) {
226 			config.csc_enable = false;
227 			output_format_422 = true;
228 			output_format_ycbcr = true;
229 		} else {
230 			config.csc_enable = true;
231 			output_format_422 = false;
232 			output_format_ycbcr = false;
233 		}
234 	}
235 
236 	if (connector->display_info.is_hdmi)
237 		mode = ADV7511_HDMI_CFG_MODE_HDMI;
238 	else
239 		mode = ADV7511_HDMI_CFG_MODE_DVI;
240 
241 	adv7511_set_colormap(adv7511, config.csc_enable,
242 			     config.csc_coefficents,
243 			     config.csc_scaling_factor);
244 
245 	regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
246 			   (output_format_422 << 7) | output_format_ycbcr);
247 
248 	regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
249 			   ADV7511_HDMI_CFG_MODE_MASK, mode);
250 }
251 
252 static void adv7511_set_link_config(struct adv7511 *adv7511,
253 				    const struct adv7511_link_config *config)
254 {
255 	/*
256 	 * The input style values documented in the datasheet don't match the
257 	 * hardware register field values :-(
258 	 */
259 	static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
260 
261 	unsigned int clock_delay;
262 	unsigned int color_depth;
263 	unsigned int input_id;
264 
265 	clock_delay = (config->clock_delay + 1200) / 400;
266 	color_depth = config->input_color_depth == 8 ? 3
267 		    : (config->input_color_depth == 10 ? 1 : 2);
268 
269 	/* TODO Support input ID 6 */
270 	if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
271 		input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
272 			 ? 5 : 0;
273 	else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
274 		input_id = config->embedded_sync ? 8 : 7;
275 	else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
276 		input_id = config->embedded_sync ? 4 : 3;
277 	else
278 		input_id = config->embedded_sync ? 2 : 1;
279 
280 	regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
281 			   input_id);
282 	regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
283 			   (color_depth << 4) |
284 			   (input_styles[config->input_style] << 2));
285 	regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
286 		     config->input_justification << 3);
287 	regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
288 		     config->sync_pulse << 2);
289 
290 	regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
291 
292 	adv7511->embedded_sync = config->embedded_sync;
293 	adv7511->hsync_polarity = config->hsync_polarity;
294 	adv7511->vsync_polarity = config->vsync_polarity;
295 	adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
296 }
297 
298 static void __adv7511_power_on(struct adv7511 *adv7511)
299 {
300 	adv7511->current_edid_segment = -1;
301 
302 	regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
303 			   ADV7511_POWER_POWER_DOWN, 0);
304 	if (adv7511->i2c_main->irq) {
305 		/*
306 		 * Documentation says the INT_ENABLE registers are reset in
307 		 * POWER_DOWN mode. My 7511w preserved the bits, however.
308 		 * Still, let's be safe and stick to the documentation.
309 		 */
310 		regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
311 			     ADV7511_INT0_EDID_READY | ADV7511_INT0_HPD);
312 		regmap_update_bits(adv7511->regmap,
313 				   ADV7511_REG_INT_ENABLE(1),
314 				   ADV7511_INT1_DDC_ERROR,
315 				   ADV7511_INT1_DDC_ERROR);
316 	}
317 
318 	/*
319 	 * Per spec it is allowed to pulse the HPD signal to indicate that the
320 	 * EDID information has changed. Some monitors do this when they wakeup
321 	 * from standby or are enabled. When the HPD goes low the adv7511 is
322 	 * reset and the outputs are disabled which might cause the monitor to
323 	 * go to standby again. To avoid this we ignore the HPD pin for the
324 	 * first few seconds after enabling the output. On the other hand
325 	 * adv7535 require to enable HPD Override bit for proper HPD.
326 	 */
327 	if (adv7511->info->hpd_override_enable)
328 		regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
329 				   ADV7535_REG_POWER2_HPD_OVERRIDE,
330 				   ADV7535_REG_POWER2_HPD_OVERRIDE);
331 	else
332 		regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
333 				   ADV7511_REG_POWER2_HPD_SRC_MASK,
334 				   ADV7511_REG_POWER2_HPD_SRC_NONE);
335 }
336 
337 static void adv7511_power_on(struct adv7511 *adv7511)
338 {
339 	__adv7511_power_on(adv7511);
340 
341 	/*
342 	 * Most of the registers are reset during power down or when HPD is low.
343 	 */
344 	regcache_sync(adv7511->regmap);
345 
346 	if (adv7511->info->has_dsi)
347 		adv7533_dsi_power_on(adv7511);
348 	adv7511->powered = true;
349 }
350 
351 static void __adv7511_power_off(struct adv7511 *adv7511)
352 {
353 	/* TODO: setup additional power down modes */
354 	if (adv7511->info->hpd_override_enable)
355 		regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
356 				   ADV7535_REG_POWER2_HPD_OVERRIDE, 0);
357 
358 	regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
359 			   ADV7511_POWER_POWER_DOWN,
360 			   ADV7511_POWER_POWER_DOWN);
361 	regmap_update_bits(adv7511->regmap,
362 			   ADV7511_REG_INT_ENABLE(1),
363 			   ADV7511_INT1_DDC_ERROR, 0);
364 	regcache_mark_dirty(adv7511->regmap);
365 }
366 
367 static void adv7511_power_off(struct adv7511 *adv7511)
368 {
369 	__adv7511_power_off(adv7511);
370 	if (adv7511->info->has_dsi)
371 		adv7533_dsi_power_off(adv7511);
372 	adv7511->powered = false;
373 }
374 
375 /* -----------------------------------------------------------------------------
376  * Interrupt and hotplug detection
377  */
378 
379 static bool adv7511_hpd(struct adv7511 *adv7511)
380 {
381 	unsigned int irq0;
382 	int ret;
383 
384 	ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
385 	if (ret < 0)
386 		return false;
387 
388 	if (irq0 & ADV7511_INT0_HPD) {
389 		regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
390 			     ADV7511_INT0_HPD);
391 		return true;
392 	}
393 
394 	return false;
395 }
396 
397 static void adv7511_hpd_work(struct work_struct *work)
398 {
399 	struct adv7511 *adv7511 = container_of(work, struct adv7511, hpd_work);
400 	enum drm_connector_status status;
401 	unsigned int val;
402 	int ret;
403 
404 	ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
405 	if (ret < 0)
406 		status = connector_status_disconnected;
407 	else if (val & ADV7511_STATUS_HPD)
408 		status = connector_status_connected;
409 	else
410 		status = connector_status_disconnected;
411 
412 	/*
413 	 * The bridge resets its registers on unplug. So when we get a plug
414 	 * event and we're already supposed to be powered, cycle the bridge to
415 	 * restore its state.
416 	 */
417 	if (status == connector_status_connected &&
418 	    adv7511->status == connector_status_disconnected &&
419 	    adv7511->powered) {
420 		regcache_mark_dirty(adv7511->regmap);
421 		adv7511_power_on(adv7511);
422 	}
423 
424 	if (adv7511->status != status) {
425 		adv7511->status = status;
426 
427 		drm_bridge_hpd_notify(&adv7511->bridge, status);
428 	}
429 }
430 
431 static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
432 {
433 	unsigned int irq0, irq1;
434 	int ret;
435 	int cec_status = IRQ_NONE;
436 	int irq_status = IRQ_NONE;
437 
438 	ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
439 	if (ret < 0)
440 		return ret;
441 
442 	ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
443 	if (ret < 0)
444 		return ret;
445 
446 	regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
447 	regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
448 
449 	if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder) {
450 		schedule_work(&adv7511->hpd_work);
451 		irq_status = IRQ_HANDLED;
452 	}
453 
454 	if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
455 		adv7511->edid_read = true;
456 
457 		if (adv7511->i2c_main->irq)
458 			wake_up_all(&adv7511->wq);
459 		irq_status = IRQ_HANDLED;
460 	}
461 
462 #ifdef CONFIG_DRM_I2C_ADV7511_CEC
463 	cec_status = adv7511_cec_irq_process(adv7511, irq1);
464 #endif
465 
466 	/* If there is no IRQ to handle, exit indicating no IRQ data */
467 	if (irq_status == IRQ_HANDLED || cec_status == IRQ_HANDLED)
468 		return IRQ_HANDLED;
469 
470 	return IRQ_NONE;
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 : ret;
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 const struct drm_edid *adv7511_edid_read(struct adv7511 *adv7511,
581 						struct drm_connector *connector)
582 {
583 	const struct drm_edid *drm_edid;
584 
585 	/* Reading the EDID only works if the device is powered */
586 	if (!adv7511->powered) {
587 		unsigned int edid_i2c_addr =
588 					(adv7511->i2c_edid->addr << 1);
589 
590 		__adv7511_power_on(adv7511);
591 
592 		/* Reset the EDID_I2C_ADDR register as it might be cleared */
593 		regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
594 			     edid_i2c_addr);
595 	}
596 
597 	drm_edid = drm_edid_read_custom(connector, adv7511_get_edid_block, adv7511);
598 
599 	if (!adv7511->powered)
600 		__adv7511_power_off(adv7511);
601 
602 	return drm_edid;
603 }
604 
605 static enum drm_connector_status
606 adv7511_detect(struct adv7511 *adv7511)
607 {
608 	enum drm_connector_status status;
609 	unsigned int val;
610 	bool hpd;
611 	int ret;
612 
613 	ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
614 	if (ret < 0)
615 		return connector_status_disconnected;
616 
617 	if (val & ADV7511_STATUS_HPD)
618 		status = connector_status_connected;
619 	else
620 		status = connector_status_disconnected;
621 
622 	hpd = adv7511_hpd(adv7511);
623 
624 	/* The chip resets itself when the cable is disconnected, so in case
625 	 * there is a pending HPD interrupt and the cable is connected there was
626 	 * at least one transition from disconnected to connected and the chip
627 	 * has to be reinitialized. */
628 	if (status == connector_status_connected && hpd && adv7511->powered) {
629 		regcache_mark_dirty(adv7511->regmap);
630 		adv7511_power_on(adv7511);
631 		if (adv7511->status == connector_status_connected)
632 			status = connector_status_disconnected;
633 	} else {
634 		/* Renable HPD sensing */
635 		if (adv7511->info->hpd_override_enable)
636 			regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
637 					   ADV7535_REG_POWER2_HPD_OVERRIDE,
638 					   ADV7535_REG_POWER2_HPD_OVERRIDE);
639 		else
640 			regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
641 					   ADV7511_REG_POWER2_HPD_SRC_MASK,
642 					   ADV7511_REG_POWER2_HPD_SRC_BOTH);
643 	}
644 
645 	adv7511->status = status;
646 	return status;
647 }
648 
649 static void adv7511_mode_set(struct adv7511 *adv7511,
650 			     const struct drm_display_mode *adj_mode)
651 {
652 	unsigned int low_refresh_rate;
653 	unsigned int hsync_polarity = 0;
654 	unsigned int vsync_polarity = 0;
655 
656 	if (adv7511->embedded_sync) {
657 		unsigned int hsync_offset, hsync_len;
658 		unsigned int vsync_offset, vsync_len;
659 
660 		hsync_offset = adj_mode->crtc_hsync_start -
661 			       adj_mode->crtc_hdisplay;
662 		vsync_offset = adj_mode->crtc_vsync_start -
663 			       adj_mode->crtc_vdisplay;
664 		hsync_len = adj_mode->crtc_hsync_end -
665 			    adj_mode->crtc_hsync_start;
666 		vsync_len = adj_mode->crtc_vsync_end -
667 			    adj_mode->crtc_vsync_start;
668 
669 		/* The hardware vsync generator has a off-by-one bug */
670 		vsync_offset += 1;
671 
672 		regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
673 			     ((hsync_offset >> 10) & 0x7) << 5);
674 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
675 			     (hsync_offset >> 2) & 0xff);
676 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
677 			     ((hsync_offset & 0x3) << 6) |
678 			     ((hsync_len >> 4) & 0x3f));
679 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
680 			     ((hsync_len & 0xf) << 4) |
681 			     ((vsync_offset >> 6) & 0xf));
682 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
683 			     ((vsync_offset & 0x3f) << 2) |
684 			     ((vsync_len >> 8) & 0x3));
685 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
686 			     vsync_len & 0xff);
687 
688 		hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
689 		vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
690 	} else {
691 		enum adv7511_sync_polarity mode_hsync_polarity;
692 		enum adv7511_sync_polarity mode_vsync_polarity;
693 
694 		/**
695 		 * If the input signal is always low or always high we want to
696 		 * invert or let it passthrough depending on the polarity of the
697 		 * current mode.
698 		 **/
699 		if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
700 			mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
701 		else
702 			mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
703 
704 		if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
705 			mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
706 		else
707 			mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
708 
709 		if (adv7511->hsync_polarity != mode_hsync_polarity &&
710 		    adv7511->hsync_polarity !=
711 		    ADV7511_SYNC_POLARITY_PASSTHROUGH)
712 			hsync_polarity = 1;
713 
714 		if (adv7511->vsync_polarity != mode_vsync_polarity &&
715 		    adv7511->vsync_polarity !=
716 		    ADV7511_SYNC_POLARITY_PASSTHROUGH)
717 			vsync_polarity = 1;
718 	}
719 
720 	if (drm_mode_vrefresh(adj_mode) <= 24)
721 		low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
722 	else if (drm_mode_vrefresh(adj_mode) <= 25)
723 		low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
724 	else if (drm_mode_vrefresh(adj_mode) <= 30)
725 		low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
726 	else
727 		low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
728 
729 	if (adv7511->info->type == ADV7511)
730 		regmap_update_bits(adv7511->regmap, 0xfb,
731 				   0x6, low_refresh_rate << 1);
732 	else
733 		regmap_update_bits(adv7511->regmap, 0x4a,
734 				   0xc, low_refresh_rate << 2);
735 
736 	regmap_update_bits(adv7511->regmap, 0x17,
737 		0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
738 
739 	drm_mode_copy(&adv7511->curr_mode, adj_mode);
740 
741 	/* Update horizontal/vertical porch params */
742 	if (adv7511->info->has_dsi && adv7511->use_timing_gen)
743 		adv7533_dsi_config_timing_gen(adv7511);
744 
745 	/*
746 	 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
747 	 * supposed to give better results.
748 	 */
749 
750 	adv7511->f_tmds = adj_mode->clock;
751 }
752 
753 static int adv7511_connector_init(struct adv7511 *adv)
754 {
755 	struct drm_bridge *bridge = &adv->bridge;
756 	struct drm_connector *connector;
757 
758 	connector = drm_bridge_connector_init(bridge->dev, bridge->encoder);
759 	if (IS_ERR(connector)) {
760 		DRM_ERROR("Failed to initialize connector with drm\n");
761 		return PTR_ERR(connector);
762 	}
763 
764 	drm_connector_attach_encoder(connector, bridge->encoder);
765 
766 	return 0;
767 }
768 
769 /* -----------------------------------------------------------------------------
770  * DRM Bridge Operations
771  */
772 
773 static const struct adv7511 *bridge_to_adv7511_const(const struct drm_bridge *bridge)
774 {
775 	return container_of(bridge, struct adv7511, bridge);
776 }
777 
778 static void adv7511_bridge_atomic_enable(struct drm_bridge *bridge,
779 					 struct drm_atomic_state *state)
780 {
781 	struct adv7511 *adv = bridge_to_adv7511(bridge);
782 	struct drm_connector *connector;
783 	struct drm_connector_state *conn_state;
784 	struct drm_crtc_state *crtc_state;
785 
786 	adv7511_power_on(adv);
787 
788 	connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder);
789 	if (WARN_ON(!connector))
790 		return;
791 
792 	conn_state = drm_atomic_get_new_connector_state(state, connector);
793 	if (WARN_ON(!conn_state))
794 		return;
795 
796 	crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
797 	if (WARN_ON(!crtc_state))
798 		return;
799 
800 	adv7511_set_config_csc(adv, connector, adv->rgb);
801 
802 	adv7511_mode_set(adv, &crtc_state->adjusted_mode);
803 
804 	drm_atomic_helper_connector_hdmi_update_infoframes(connector, state);
805 }
806 
807 static void adv7511_bridge_atomic_disable(struct drm_bridge *bridge,
808 					  struct drm_atomic_state *state)
809 {
810 	struct adv7511 *adv = bridge_to_adv7511(bridge);
811 
812 	adv7511_power_off(adv);
813 }
814 
815 static enum drm_mode_status
816 adv7511_bridge_hdmi_tmds_char_rate_valid(const struct drm_bridge *bridge,
817 					 const struct drm_display_mode *mode,
818 					 unsigned long long tmds_rate)
819 {
820 	const struct adv7511 *adv = bridge_to_adv7511_const(bridge);
821 
822 	if (tmds_rate > 1000ULL * adv->info->max_mode_clock_khz)
823 		return MODE_CLOCK_HIGH;
824 
825 	return MODE_OK;
826 }
827 
828 static enum drm_mode_status adv7511_bridge_mode_valid(struct drm_bridge *bridge,
829 						      const struct drm_display_info *info,
830 		const struct drm_display_mode *mode)
831 {
832 	struct adv7511 *adv = bridge_to_adv7511(bridge);
833 
834 	if (!adv->info->has_dsi)
835 		return MODE_OK;
836 
837 	return adv7533_mode_valid(adv, mode);
838 }
839 
840 static int adv7511_bridge_attach(struct drm_bridge *bridge,
841 				 struct drm_encoder *encoder,
842 				 enum drm_bridge_attach_flags flags)
843 {
844 	struct adv7511 *adv = bridge_to_adv7511(bridge);
845 	int ret = 0;
846 
847 	if (adv->next_bridge) {
848 		ret = drm_bridge_attach(encoder, adv->next_bridge, bridge,
849 					flags | DRM_BRIDGE_ATTACH_NO_CONNECTOR);
850 		if (ret)
851 			return ret;
852 	}
853 
854 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
855 		ret = adv7511_connector_init(adv);
856 		if (ret < 0)
857 			return ret;
858 	}
859 
860 	if (adv->i2c_main->irq)
861 		regmap_write(adv->regmap, ADV7511_REG_INT_ENABLE(0),
862 			     ADV7511_INT0_HPD);
863 
864 	return ret;
865 }
866 
867 static enum drm_connector_status
868 adv7511_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)
869 {
870 	struct adv7511 *adv = bridge_to_adv7511(bridge);
871 
872 	return adv7511_detect(adv);
873 }
874 
875 static const struct drm_edid *adv7511_bridge_edid_read(struct drm_bridge *bridge,
876 						       struct drm_connector *connector)
877 {
878 	struct adv7511 *adv = bridge_to_adv7511(bridge);
879 
880 	return adv7511_edid_read(adv, connector);
881 }
882 
883 static int adv7511_bridge_hdmi_clear_infoframe(struct drm_bridge *bridge,
884 					       enum hdmi_infoframe_type type)
885 {
886 	struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
887 
888 	switch (type) {
889 	case HDMI_INFOFRAME_TYPE_AVI:
890 		adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
891 		break;
892 	default:
893 		drm_dbg_driver(adv7511->bridge.dev, "Unsupported HDMI InfoFrame %x\n", type);
894 		break;
895 	}
896 
897 	return 0;
898 }
899 
900 static int adv7511_bridge_hdmi_write_infoframe(struct drm_bridge *bridge,
901 					       enum hdmi_infoframe_type type,
902 					       const u8 *buffer, size_t len)
903 {
904 	struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
905 
906 	adv7511_bridge_hdmi_clear_infoframe(bridge, type);
907 
908 	switch (type) {
909 	case HDMI_INFOFRAME_TYPE_AVI:
910 		/* The AVI infoframe id is not configurable */
911 		regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
912 				  buffer + 1, len - 1);
913 
914 		adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
915 		break;
916 	default:
917 		drm_dbg_driver(adv7511->bridge.dev, "Unsupported HDMI InfoFrame %x\n", type);
918 		break;
919 	}
920 
921 	return 0;
922 }
923 
924 static const struct drm_bridge_funcs adv7511_bridge_funcs = {
925 	.mode_valid = adv7511_bridge_mode_valid,
926 	.attach = adv7511_bridge_attach,
927 	.detect = adv7511_bridge_detect,
928 	.edid_read = adv7511_bridge_edid_read,
929 
930 	.atomic_enable = adv7511_bridge_atomic_enable,
931 	.atomic_disable = adv7511_bridge_atomic_disable,
932 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
933 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
934 	.atomic_reset = drm_atomic_helper_bridge_reset,
935 
936 	.hdmi_tmds_char_rate_valid = adv7511_bridge_hdmi_tmds_char_rate_valid,
937 	.hdmi_clear_infoframe = adv7511_bridge_hdmi_clear_infoframe,
938 	.hdmi_write_infoframe = adv7511_bridge_hdmi_write_infoframe,
939 
940 	.hdmi_audio_startup = adv7511_hdmi_audio_startup,
941 	.hdmi_audio_prepare = adv7511_hdmi_audio_prepare,
942 	.hdmi_audio_shutdown = adv7511_hdmi_audio_shutdown,
943 
944 	.hdmi_cec_init = adv7511_cec_init,
945 	.hdmi_cec_enable = adv7511_cec_enable,
946 	.hdmi_cec_log_addr = adv7511_cec_log_addr,
947 	.hdmi_cec_transmit = adv7511_cec_transmit,
948 };
949 
950 /* -----------------------------------------------------------------------------
951  * Probe & remove
952  */
953 
954 static const char * const adv7511_supply_names[] = {
955 	"avdd",
956 	"dvdd",
957 	"pvdd",
958 	"bgvdd",
959 	"dvdd-3v",
960 };
961 
962 static const char * const adv7533_supply_names[] = {
963 	"avdd",
964 	"dvdd",
965 	"pvdd",
966 	"a2vdd",
967 	"v3p3",
968 	"v1p2",
969 };
970 
971 static int adv7511_init_regulators(struct adv7511 *adv)
972 {
973 	const char * const *supply_names = adv->info->supply_names;
974 	unsigned int num_supplies = adv->info->num_supplies;
975 	struct device *dev = &adv->i2c_main->dev;
976 	unsigned int i;
977 	int ret;
978 
979 	adv->supplies = devm_kcalloc(dev, num_supplies,
980 				     sizeof(*adv->supplies), GFP_KERNEL);
981 	if (!adv->supplies)
982 		return -ENOMEM;
983 
984 	for (i = 0; i < num_supplies; i++)
985 		adv->supplies[i].supply = supply_names[i];
986 
987 	ret = devm_regulator_bulk_get(dev, num_supplies, adv->supplies);
988 	if (ret)
989 		return ret;
990 
991 	return regulator_bulk_enable(num_supplies, adv->supplies);
992 }
993 
994 static void adv7511_uninit_regulators(struct adv7511 *adv)
995 {
996 	regulator_bulk_disable(adv->info->num_supplies, adv->supplies);
997 }
998 
999 static bool adv7511_cec_register_volatile(struct device *dev, unsigned int reg)
1000 {
1001 	struct i2c_client *i2c = to_i2c_client(dev);
1002 	struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1003 
1004 	reg -= adv7511->info->reg_cec_offset;
1005 
1006 	switch (reg) {
1007 	case ADV7511_REG_CEC_RX1_FRAME_HDR:
1008 	case ADV7511_REG_CEC_RX1_FRAME_DATA0 ... ADV7511_REG_CEC_RX1_FRAME_DATA0 + 14:
1009 	case ADV7511_REG_CEC_RX1_FRAME_LEN:
1010 	case ADV7511_REG_CEC_RX2_FRAME_HDR:
1011 	case ADV7511_REG_CEC_RX2_FRAME_DATA0 ... ADV7511_REG_CEC_RX2_FRAME_DATA0 + 14:
1012 	case ADV7511_REG_CEC_RX2_FRAME_LEN:
1013 	case ADV7511_REG_CEC_RX3_FRAME_HDR:
1014 	case ADV7511_REG_CEC_RX3_FRAME_DATA0 ... ADV7511_REG_CEC_RX3_FRAME_DATA0 + 14:
1015 	case ADV7511_REG_CEC_RX3_FRAME_LEN:
1016 	case ADV7511_REG_CEC_RX_STATUS:
1017 	case ADV7511_REG_CEC_RX_BUFFERS:
1018 	case ADV7511_REG_CEC_TX_LOW_DRV_CNT:
1019 		return true;
1020 	}
1021 
1022 	return false;
1023 }
1024 
1025 static const struct regmap_config adv7511_cec_regmap_config = {
1026 	.reg_bits = 8,
1027 	.val_bits = 8,
1028 
1029 	.max_register = 0xff,
1030 	.cache_type = REGCACHE_MAPLE,
1031 	.volatile_reg = adv7511_cec_register_volatile,
1032 };
1033 
1034 static int adv7511_init_cec_regmap(struct adv7511 *adv)
1035 {
1036 	int ret;
1037 
1038 	adv->i2c_cec = i2c_new_ancillary_device(adv->i2c_main, "cec",
1039 						ADV7511_CEC_I2C_ADDR_DEFAULT);
1040 	if (IS_ERR(adv->i2c_cec))
1041 		return PTR_ERR(adv->i2c_cec);
1042 
1043 	regmap_write(adv->regmap, ADV7511_REG_CEC_I2C_ADDR,
1044 		     adv->i2c_cec->addr << 1);
1045 
1046 	i2c_set_clientdata(adv->i2c_cec, adv);
1047 
1048 	adv->regmap_cec = devm_regmap_init_i2c(adv->i2c_cec,
1049 					&adv7511_cec_regmap_config);
1050 	if (IS_ERR(adv->regmap_cec)) {
1051 		ret = PTR_ERR(adv->regmap_cec);
1052 		goto err;
1053 	}
1054 
1055 	if (adv->info->reg_cec_offset == ADV7533_REG_CEC_OFFSET) {
1056 		ret = adv7533_patch_cec_registers(adv);
1057 		if (ret)
1058 			goto err;
1059 	}
1060 
1061 	return 0;
1062 err:
1063 	i2c_unregister_device(adv->i2c_cec);
1064 	return ret;
1065 }
1066 
1067 static int adv7511_parse_dt(struct device_node *np,
1068 			    struct adv7511_link_config *config)
1069 {
1070 	const char *str;
1071 	int ret;
1072 
1073 	of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
1074 	if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
1075 	    config->input_color_depth != 12)
1076 		return -EINVAL;
1077 
1078 	ret = of_property_read_string(np, "adi,input-colorspace", &str);
1079 	if (ret < 0)
1080 		return ret;
1081 
1082 	if (!strcmp(str, "rgb"))
1083 		config->input_colorspace = HDMI_COLORSPACE_RGB;
1084 	else if (!strcmp(str, "yuv422"))
1085 		config->input_colorspace = HDMI_COLORSPACE_YUV422;
1086 	else if (!strcmp(str, "yuv444"))
1087 		config->input_colorspace = HDMI_COLORSPACE_YUV444;
1088 	else
1089 		return -EINVAL;
1090 
1091 	ret = of_property_read_string(np, "adi,input-clock", &str);
1092 	if (ret < 0)
1093 		return ret;
1094 
1095 	if (!strcmp(str, "1x"))
1096 		config->input_clock = ADV7511_INPUT_CLOCK_1X;
1097 	else if (!strcmp(str, "2x"))
1098 		config->input_clock = ADV7511_INPUT_CLOCK_2X;
1099 	else if (!strcmp(str, "ddr"))
1100 		config->input_clock = ADV7511_INPUT_CLOCK_DDR;
1101 	else
1102 		return -EINVAL;
1103 
1104 	if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
1105 	    config->input_clock != ADV7511_INPUT_CLOCK_1X) {
1106 		ret = of_property_read_u32(np, "adi,input-style",
1107 					   &config->input_style);
1108 		if (ret)
1109 			return ret;
1110 
1111 		if (config->input_style < 1 || config->input_style > 3)
1112 			return -EINVAL;
1113 
1114 		ret = of_property_read_string(np, "adi,input-justification",
1115 					      &str);
1116 		if (ret < 0)
1117 			return ret;
1118 
1119 		if (!strcmp(str, "left"))
1120 			config->input_justification =
1121 				ADV7511_INPUT_JUSTIFICATION_LEFT;
1122 		else if (!strcmp(str, "evenly"))
1123 			config->input_justification =
1124 				ADV7511_INPUT_JUSTIFICATION_EVENLY;
1125 		else if (!strcmp(str, "right"))
1126 			config->input_justification =
1127 				ADV7511_INPUT_JUSTIFICATION_RIGHT;
1128 		else
1129 			return -EINVAL;
1130 
1131 	} else {
1132 		config->input_style = 1;
1133 		config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
1134 	}
1135 
1136 	of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
1137 	if (config->clock_delay < -1200 || config->clock_delay > 1600)
1138 		return -EINVAL;
1139 
1140 	config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
1141 
1142 	/* Hardcode the sync pulse configurations for now. */
1143 	config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
1144 	config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1145 	config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1146 
1147 	return 0;
1148 }
1149 
1150 static int adv7511_probe(struct i2c_client *i2c)
1151 {
1152 	struct adv7511_link_config link_config;
1153 	struct adv7511 *adv7511;
1154 	struct device *dev = &i2c->dev;
1155 	unsigned int val;
1156 	int ret;
1157 
1158 	if (!dev->of_node)
1159 		return -EINVAL;
1160 
1161 	adv7511 = devm_drm_bridge_alloc(dev, struct adv7511, bridge,
1162 					&adv7511_bridge_funcs);
1163 	if (IS_ERR(adv7511))
1164 		return PTR_ERR(adv7511);
1165 
1166 	adv7511->i2c_main = i2c;
1167 	adv7511->powered = false;
1168 	adv7511->status = connector_status_disconnected;
1169 	adv7511->info = i2c_get_match_data(i2c);
1170 
1171 	memset(&link_config, 0, sizeof(link_config));
1172 
1173 	ret = drm_of_find_panel_or_bridge(dev->of_node, 1, -1, NULL,
1174 					  &adv7511->next_bridge);
1175 	if (ret && ret != -ENODEV)
1176 		return ret;
1177 
1178 	if (adv7511->info->link_config)
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_probe(dev, ret, "failed to init regulators\n");
1188 		goto err_of_node_put;
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->info->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 	INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
1253 
1254 	adv7511_power_off(adv7511);
1255 
1256 	i2c_set_clientdata(i2c, adv7511);
1257 
1258 	if (adv7511->info->link_config)
1259 		adv7511_set_link_config(adv7511, &link_config);
1260 
1261 	regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
1262 		     ADV7511_CEC_CTRL_POWER_DOWN);
1263 
1264 	adv7511->bridge.ops = DRM_BRIDGE_OP_DETECT |
1265 		DRM_BRIDGE_OP_EDID |
1266 		DRM_BRIDGE_OP_HDMI;
1267 	if (adv7511->i2c_main->irq)
1268 		adv7511->bridge.ops |= DRM_BRIDGE_OP_HPD;
1269 
1270 	adv7511->bridge.vendor = "Analog";
1271 	adv7511->bridge.product = adv7511->info->name;
1272 
1273 #ifdef CONFIG_DRM_I2C_ADV7511_AUDIO
1274 	adv7511->bridge.ops |= DRM_BRIDGE_OP_HDMI_AUDIO;
1275 	adv7511->bridge.hdmi_audio_dev = dev;
1276 	adv7511->bridge.hdmi_audio_max_i2s_playback_channels = 2;
1277 	adv7511->bridge.hdmi_audio_i2s_formats = (SNDRV_PCM_FMTBIT_S16_LE |
1278 						  SNDRV_PCM_FMTBIT_S20_3LE |
1279 						  SNDRV_PCM_FMTBIT_S24_3LE |
1280 						  SNDRV_PCM_FMTBIT_S24_LE |
1281 						  SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE),
1282 	adv7511->bridge.hdmi_audio_spdif_playback = 1;
1283 	adv7511->bridge.hdmi_audio_dai_port = 2;
1284 #endif
1285 
1286 #ifdef CONFIG_DRM_I2C_ADV7511_CEC
1287 	adv7511->bridge.ops |= DRM_BRIDGE_OP_HDMI_CEC_ADAPTER;
1288 	adv7511->bridge.hdmi_cec_dev = dev;
1289 	adv7511->bridge.hdmi_cec_adapter_name = dev_name(dev);
1290 	adv7511->bridge.hdmi_cec_available_las = ADV7511_MAX_ADDRS;
1291 #endif
1292 
1293 	adv7511->bridge.of_node = dev->of_node;
1294 	adv7511->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
1295 
1296 	drm_bridge_add(&adv7511->bridge);
1297 
1298 	if (i2c->irq) {
1299 		init_waitqueue_head(&adv7511->wq);
1300 
1301 		ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
1302 						adv7511_irq_handler,
1303 						IRQF_ONESHOT | IRQF_SHARED,
1304 						dev_name(dev),
1305 						adv7511);
1306 		if (ret)
1307 			goto err_unregister_audio;
1308 	}
1309 
1310 	if (adv7511->info->has_dsi) {
1311 		ret = adv7533_attach_dsi(adv7511);
1312 		if (ret)
1313 			goto err_unregister_audio;
1314 	}
1315 
1316 	return 0;
1317 
1318 err_unregister_audio:
1319 	drm_bridge_remove(&adv7511->bridge);
1320 	i2c_unregister_device(adv7511->i2c_cec);
1321 	clk_disable_unprepare(adv7511->cec_clk);
1322 err_i2c_unregister_packet:
1323 	i2c_unregister_device(adv7511->i2c_packet);
1324 err_i2c_unregister_edid:
1325 	i2c_unregister_device(adv7511->i2c_edid);
1326 uninit_regulators:
1327 	adv7511_uninit_regulators(adv7511);
1328 err_of_node_put:
1329 	of_node_put(adv7511->host_node);
1330 
1331 	return ret;
1332 }
1333 
1334 static void adv7511_remove(struct i2c_client *i2c)
1335 {
1336 	struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1337 
1338 	of_node_put(adv7511->host_node);
1339 
1340 	adv7511_uninit_regulators(adv7511);
1341 
1342 	drm_bridge_remove(&adv7511->bridge);
1343 
1344 	i2c_unregister_device(adv7511->i2c_cec);
1345 	clk_disable_unprepare(adv7511->cec_clk);
1346 
1347 	i2c_unregister_device(adv7511->i2c_packet);
1348 	i2c_unregister_device(adv7511->i2c_edid);
1349 }
1350 
1351 static const struct adv7511_chip_info adv7511_chip_info = {
1352 	.type = ADV7511,
1353 	.name = "ADV7511",
1354 	.max_mode_clock_khz = 165000,
1355 	.supply_names = adv7511_supply_names,
1356 	.num_supplies = ARRAY_SIZE(adv7511_supply_names),
1357 	.link_config = true,
1358 };
1359 
1360 static const struct adv7511_chip_info adv7533_chip_info = {
1361 	.type = ADV7533,
1362 	.name = "ADV7533",
1363 	.max_mode_clock_khz = 80000,
1364 	.max_lane_freq_khz = 800000,
1365 	.supply_names = adv7533_supply_names,
1366 	.num_supplies = ARRAY_SIZE(adv7533_supply_names),
1367 	.reg_cec_offset = ADV7533_REG_CEC_OFFSET,
1368 	.has_dsi = true,
1369 };
1370 
1371 static const struct adv7511_chip_info adv7535_chip_info = {
1372 	.type = ADV7535,
1373 	.name = "ADV7535",
1374 	.max_mode_clock_khz = 148500,
1375 	.max_lane_freq_khz = 891000,
1376 	.supply_names = adv7533_supply_names,
1377 	.num_supplies = ARRAY_SIZE(adv7533_supply_names),
1378 	.reg_cec_offset = ADV7533_REG_CEC_OFFSET,
1379 	.has_dsi = true,
1380 	.hpd_override_enable = true,
1381 };
1382 
1383 static const struct i2c_device_id adv7511_i2c_ids[] = {
1384 	{ "adv7511", (kernel_ulong_t)&adv7511_chip_info },
1385 	{ "adv7511w", (kernel_ulong_t)&adv7511_chip_info },
1386 	{ "adv7513", (kernel_ulong_t)&adv7511_chip_info },
1387 	{ "adv7533", (kernel_ulong_t)&adv7533_chip_info },
1388 	{ "adv7535", (kernel_ulong_t)&adv7535_chip_info },
1389 	{ }
1390 };
1391 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1392 
1393 static const struct of_device_id adv7511_of_ids[] = {
1394 	{ .compatible = "adi,adv7511", .data = &adv7511_chip_info },
1395 	{ .compatible = "adi,adv7511w", .data = &adv7511_chip_info },
1396 	{ .compatible = "adi,adv7513", .data = &adv7511_chip_info },
1397 	{ .compatible = "adi,adv7533", .data = &adv7533_chip_info },
1398 	{ .compatible = "adi,adv7535", .data = &adv7535_chip_info },
1399 	{ }
1400 };
1401 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1402 
1403 static struct mipi_dsi_driver adv7533_dsi_driver = {
1404 	.driver.name = "adv7533",
1405 };
1406 
1407 static struct i2c_driver adv7511_driver = {
1408 	.driver = {
1409 		.name = "adv7511",
1410 		.of_match_table = adv7511_of_ids,
1411 	},
1412 	.id_table = adv7511_i2c_ids,
1413 	.probe = adv7511_probe,
1414 	.remove = adv7511_remove,
1415 };
1416 
1417 static int __init adv7511_init(void)
1418 {
1419 	int ret;
1420 
1421 	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) {
1422 		ret = mipi_dsi_driver_register(&adv7533_dsi_driver);
1423 		if (ret)
1424 			return ret;
1425 	}
1426 
1427 	ret = i2c_add_driver(&adv7511_driver);
1428 	if (ret) {
1429 		if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1430 			mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1431 	}
1432 
1433 	return ret;
1434 }
1435 module_init(adv7511_init);
1436 
1437 static void __exit adv7511_exit(void)
1438 {
1439 	i2c_del_driver(&adv7511_driver);
1440 
1441 	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1442 		mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1443 }
1444 module_exit(adv7511_exit);
1445 
1446 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1447 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1448 MODULE_LICENSE("GPL");
1449