xref: /linux/drivers/gpu/drm/bridge/adv7511/adv7511_drv.c (revision 917b10d90990fd2138b5dbc2d22cfa428c070ade)
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 adv7511_bridge_detect(struct drm_bridge *bridge)
868 {
869 	struct adv7511 *adv = bridge_to_adv7511(bridge);
870 
871 	return adv7511_detect(adv);
872 }
873 
874 static const struct drm_edid *adv7511_bridge_edid_read(struct drm_bridge *bridge,
875 						       struct drm_connector *connector)
876 {
877 	struct adv7511 *adv = bridge_to_adv7511(bridge);
878 
879 	return adv7511_edid_read(adv, connector);
880 }
881 
882 static int adv7511_bridge_hdmi_clear_infoframe(struct drm_bridge *bridge,
883 					       enum hdmi_infoframe_type type)
884 {
885 	struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
886 
887 	switch (type) {
888 	case HDMI_INFOFRAME_TYPE_AVI:
889 		adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
890 		break;
891 	default:
892 		drm_dbg_driver(adv7511->bridge.dev, "Unsupported HDMI InfoFrame %x\n", type);
893 		break;
894 	}
895 
896 	return 0;
897 }
898 
899 static int adv7511_bridge_hdmi_write_infoframe(struct drm_bridge *bridge,
900 					       enum hdmi_infoframe_type type,
901 					       const u8 *buffer, size_t len)
902 {
903 	struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
904 
905 	adv7511_bridge_hdmi_clear_infoframe(bridge, type);
906 
907 	switch (type) {
908 	case HDMI_INFOFRAME_TYPE_AVI:
909 		/* The AVI infoframe id is not configurable */
910 		regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
911 				  buffer + 1, len - 1);
912 
913 		adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
914 		break;
915 	default:
916 		drm_dbg_driver(adv7511->bridge.dev, "Unsupported HDMI InfoFrame %x\n", type);
917 		break;
918 	}
919 
920 	return 0;
921 }
922 
923 static const struct drm_bridge_funcs adv7511_bridge_funcs = {
924 	.mode_valid = adv7511_bridge_mode_valid,
925 	.attach = adv7511_bridge_attach,
926 	.detect = adv7511_bridge_detect,
927 	.edid_read = adv7511_bridge_edid_read,
928 
929 	.atomic_enable = adv7511_bridge_atomic_enable,
930 	.atomic_disable = adv7511_bridge_atomic_disable,
931 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
932 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
933 	.atomic_reset = drm_atomic_helper_bridge_reset,
934 
935 	.hdmi_tmds_char_rate_valid = adv7511_bridge_hdmi_tmds_char_rate_valid,
936 	.hdmi_clear_infoframe = adv7511_bridge_hdmi_clear_infoframe,
937 	.hdmi_write_infoframe = adv7511_bridge_hdmi_write_infoframe,
938 
939 	.hdmi_audio_startup = adv7511_hdmi_audio_startup,
940 	.hdmi_audio_prepare = adv7511_hdmi_audio_prepare,
941 	.hdmi_audio_shutdown = adv7511_hdmi_audio_shutdown,
942 
943 	.hdmi_cec_init = adv7511_cec_init,
944 	.hdmi_cec_enable = adv7511_cec_enable,
945 	.hdmi_cec_log_addr = adv7511_cec_log_addr,
946 	.hdmi_cec_transmit = adv7511_cec_transmit,
947 };
948 
949 /* -----------------------------------------------------------------------------
950  * Probe & remove
951  */
952 
953 static const char * const adv7511_supply_names[] = {
954 	"avdd",
955 	"dvdd",
956 	"pvdd",
957 	"bgvdd",
958 	"dvdd-3v",
959 };
960 
961 static const char * const adv7533_supply_names[] = {
962 	"avdd",
963 	"dvdd",
964 	"pvdd",
965 	"a2vdd",
966 	"v3p3",
967 	"v1p2",
968 };
969 
970 static int adv7511_init_regulators(struct adv7511 *adv)
971 {
972 	const char * const *supply_names = adv->info->supply_names;
973 	unsigned int num_supplies = adv->info->num_supplies;
974 	struct device *dev = &adv->i2c_main->dev;
975 	unsigned int i;
976 	int ret;
977 
978 	adv->supplies = devm_kcalloc(dev, num_supplies,
979 				     sizeof(*adv->supplies), GFP_KERNEL);
980 	if (!adv->supplies)
981 		return -ENOMEM;
982 
983 	for (i = 0; i < num_supplies; i++)
984 		adv->supplies[i].supply = supply_names[i];
985 
986 	ret = devm_regulator_bulk_get(dev, num_supplies, adv->supplies);
987 	if (ret)
988 		return ret;
989 
990 	return regulator_bulk_enable(num_supplies, adv->supplies);
991 }
992 
993 static void adv7511_uninit_regulators(struct adv7511 *adv)
994 {
995 	regulator_bulk_disable(adv->info->num_supplies, adv->supplies);
996 }
997 
998 static bool adv7511_cec_register_volatile(struct device *dev, unsigned int reg)
999 {
1000 	struct i2c_client *i2c = to_i2c_client(dev);
1001 	struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1002 
1003 	reg -= adv7511->info->reg_cec_offset;
1004 
1005 	switch (reg) {
1006 	case ADV7511_REG_CEC_RX1_FRAME_HDR:
1007 	case ADV7511_REG_CEC_RX1_FRAME_DATA0 ... ADV7511_REG_CEC_RX1_FRAME_DATA0 + 14:
1008 	case ADV7511_REG_CEC_RX1_FRAME_LEN:
1009 	case ADV7511_REG_CEC_RX2_FRAME_HDR:
1010 	case ADV7511_REG_CEC_RX2_FRAME_DATA0 ... ADV7511_REG_CEC_RX2_FRAME_DATA0 + 14:
1011 	case ADV7511_REG_CEC_RX2_FRAME_LEN:
1012 	case ADV7511_REG_CEC_RX3_FRAME_HDR:
1013 	case ADV7511_REG_CEC_RX3_FRAME_DATA0 ... ADV7511_REG_CEC_RX3_FRAME_DATA0 + 14:
1014 	case ADV7511_REG_CEC_RX3_FRAME_LEN:
1015 	case ADV7511_REG_CEC_RX_STATUS:
1016 	case ADV7511_REG_CEC_RX_BUFFERS:
1017 	case ADV7511_REG_CEC_TX_LOW_DRV_CNT:
1018 		return true;
1019 	}
1020 
1021 	return false;
1022 }
1023 
1024 static const struct regmap_config adv7511_cec_regmap_config = {
1025 	.reg_bits = 8,
1026 	.val_bits = 8,
1027 
1028 	.max_register = 0xff,
1029 	.cache_type = REGCACHE_MAPLE,
1030 	.volatile_reg = adv7511_cec_register_volatile,
1031 };
1032 
1033 static int adv7511_init_cec_regmap(struct adv7511 *adv)
1034 {
1035 	int ret;
1036 
1037 	adv->i2c_cec = i2c_new_ancillary_device(adv->i2c_main, "cec",
1038 						ADV7511_CEC_I2C_ADDR_DEFAULT);
1039 	if (IS_ERR(adv->i2c_cec))
1040 		return PTR_ERR(adv->i2c_cec);
1041 
1042 	regmap_write(adv->regmap, ADV7511_REG_CEC_I2C_ADDR,
1043 		     adv->i2c_cec->addr << 1);
1044 
1045 	i2c_set_clientdata(adv->i2c_cec, adv);
1046 
1047 	adv->regmap_cec = devm_regmap_init_i2c(adv->i2c_cec,
1048 					&adv7511_cec_regmap_config);
1049 	if (IS_ERR(adv->regmap_cec)) {
1050 		ret = PTR_ERR(adv->regmap_cec);
1051 		goto err;
1052 	}
1053 
1054 	if (adv->info->reg_cec_offset == ADV7533_REG_CEC_OFFSET) {
1055 		ret = adv7533_patch_cec_registers(adv);
1056 		if (ret)
1057 			goto err;
1058 	}
1059 
1060 	return 0;
1061 err:
1062 	i2c_unregister_device(adv->i2c_cec);
1063 	return ret;
1064 }
1065 
1066 static int adv7511_parse_dt(struct device_node *np,
1067 			    struct adv7511_link_config *config)
1068 {
1069 	const char *str;
1070 	int ret;
1071 
1072 	of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
1073 	if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
1074 	    config->input_color_depth != 12)
1075 		return -EINVAL;
1076 
1077 	ret = of_property_read_string(np, "adi,input-colorspace", &str);
1078 	if (ret < 0)
1079 		return ret;
1080 
1081 	if (!strcmp(str, "rgb"))
1082 		config->input_colorspace = HDMI_COLORSPACE_RGB;
1083 	else if (!strcmp(str, "yuv422"))
1084 		config->input_colorspace = HDMI_COLORSPACE_YUV422;
1085 	else if (!strcmp(str, "yuv444"))
1086 		config->input_colorspace = HDMI_COLORSPACE_YUV444;
1087 	else
1088 		return -EINVAL;
1089 
1090 	ret = of_property_read_string(np, "adi,input-clock", &str);
1091 	if (ret < 0)
1092 		return ret;
1093 
1094 	if (!strcmp(str, "1x"))
1095 		config->input_clock = ADV7511_INPUT_CLOCK_1X;
1096 	else if (!strcmp(str, "2x"))
1097 		config->input_clock = ADV7511_INPUT_CLOCK_2X;
1098 	else if (!strcmp(str, "ddr"))
1099 		config->input_clock = ADV7511_INPUT_CLOCK_DDR;
1100 	else
1101 		return -EINVAL;
1102 
1103 	if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
1104 	    config->input_clock != ADV7511_INPUT_CLOCK_1X) {
1105 		ret = of_property_read_u32(np, "adi,input-style",
1106 					   &config->input_style);
1107 		if (ret)
1108 			return ret;
1109 
1110 		if (config->input_style < 1 || config->input_style > 3)
1111 			return -EINVAL;
1112 
1113 		ret = of_property_read_string(np, "adi,input-justification",
1114 					      &str);
1115 		if (ret < 0)
1116 			return ret;
1117 
1118 		if (!strcmp(str, "left"))
1119 			config->input_justification =
1120 				ADV7511_INPUT_JUSTIFICATION_LEFT;
1121 		else if (!strcmp(str, "evenly"))
1122 			config->input_justification =
1123 				ADV7511_INPUT_JUSTIFICATION_EVENLY;
1124 		else if (!strcmp(str, "right"))
1125 			config->input_justification =
1126 				ADV7511_INPUT_JUSTIFICATION_RIGHT;
1127 		else
1128 			return -EINVAL;
1129 
1130 	} else {
1131 		config->input_style = 1;
1132 		config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
1133 	}
1134 
1135 	of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
1136 	if (config->clock_delay < -1200 || config->clock_delay > 1600)
1137 		return -EINVAL;
1138 
1139 	config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
1140 
1141 	/* Hardcode the sync pulse configurations for now. */
1142 	config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
1143 	config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1144 	config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1145 
1146 	return 0;
1147 }
1148 
1149 static int adv7511_probe(struct i2c_client *i2c)
1150 {
1151 	struct adv7511_link_config link_config;
1152 	struct adv7511 *adv7511;
1153 	struct device *dev = &i2c->dev;
1154 	unsigned int val;
1155 	int ret;
1156 
1157 	if (!dev->of_node)
1158 		return -EINVAL;
1159 
1160 	adv7511 = devm_drm_bridge_alloc(dev, struct adv7511, bridge,
1161 					&adv7511_bridge_funcs);
1162 	if (IS_ERR(adv7511))
1163 		return PTR_ERR(adv7511);
1164 
1165 	adv7511->i2c_main = i2c;
1166 	adv7511->powered = false;
1167 	adv7511->status = connector_status_disconnected;
1168 	adv7511->info = i2c_get_match_data(i2c);
1169 
1170 	memset(&link_config, 0, sizeof(link_config));
1171 
1172 	ret = drm_of_find_panel_or_bridge(dev->of_node, 1, -1, NULL,
1173 					  &adv7511->next_bridge);
1174 	if (ret && ret != -ENODEV)
1175 		return ret;
1176 
1177 	if (adv7511->info->link_config)
1178 		ret = adv7511_parse_dt(dev->of_node, &link_config);
1179 	else
1180 		ret = adv7533_parse_dt(dev->of_node, adv7511);
1181 	if (ret)
1182 		return ret;
1183 
1184 	ret = adv7511_init_regulators(adv7511);
1185 	if (ret) {
1186 		dev_err_probe(dev, ret, "failed to init regulators\n");
1187 		goto err_of_node_put;
1188 	}
1189 
1190 	/*
1191 	 * The power down GPIO is optional. If present, toggle it from active to
1192 	 * inactive to wake up the encoder.
1193 	 */
1194 	adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
1195 	if (IS_ERR(adv7511->gpio_pd)) {
1196 		ret = PTR_ERR(adv7511->gpio_pd);
1197 		goto uninit_regulators;
1198 	}
1199 
1200 	if (adv7511->gpio_pd) {
1201 		usleep_range(5000, 6000);
1202 		gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
1203 	}
1204 
1205 	adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
1206 	if (IS_ERR(adv7511->regmap)) {
1207 		ret = PTR_ERR(adv7511->regmap);
1208 		goto uninit_regulators;
1209 	}
1210 
1211 	ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
1212 	if (ret)
1213 		goto uninit_regulators;
1214 	dev_dbg(dev, "Rev. %d\n", val);
1215 
1216 	if (adv7511->info->type == ADV7511)
1217 		ret = regmap_register_patch(adv7511->regmap,
1218 					    adv7511_fixed_registers,
1219 					    ARRAY_SIZE(adv7511_fixed_registers));
1220 	else
1221 		ret = adv7533_patch_registers(adv7511);
1222 	if (ret)
1223 		goto uninit_regulators;
1224 
1225 	adv7511_packet_disable(adv7511, 0xffff);
1226 
1227 	adv7511->i2c_edid = i2c_new_ancillary_device(i2c, "edid",
1228 					ADV7511_EDID_I2C_ADDR_DEFAULT);
1229 	if (IS_ERR(adv7511->i2c_edid)) {
1230 		ret = PTR_ERR(adv7511->i2c_edid);
1231 		goto uninit_regulators;
1232 	}
1233 
1234 	regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
1235 		     adv7511->i2c_edid->addr << 1);
1236 
1237 	adv7511->i2c_packet = i2c_new_ancillary_device(i2c, "packet",
1238 					ADV7511_PACKET_I2C_ADDR_DEFAULT);
1239 	if (IS_ERR(adv7511->i2c_packet)) {
1240 		ret = PTR_ERR(adv7511->i2c_packet);
1241 		goto err_i2c_unregister_edid;
1242 	}
1243 
1244 	regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
1245 		     adv7511->i2c_packet->addr << 1);
1246 
1247 	ret = adv7511_init_cec_regmap(adv7511);
1248 	if (ret)
1249 		goto err_i2c_unregister_packet;
1250 
1251 	INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
1252 
1253 	adv7511_power_off(adv7511);
1254 
1255 	i2c_set_clientdata(i2c, adv7511);
1256 
1257 	if (adv7511->info->link_config)
1258 		adv7511_set_link_config(adv7511, &link_config);
1259 
1260 	regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
1261 		     ADV7511_CEC_CTRL_POWER_DOWN);
1262 
1263 	adv7511->bridge.ops = DRM_BRIDGE_OP_DETECT |
1264 		DRM_BRIDGE_OP_EDID |
1265 		DRM_BRIDGE_OP_HDMI;
1266 	if (adv7511->i2c_main->irq)
1267 		adv7511->bridge.ops |= DRM_BRIDGE_OP_HPD;
1268 
1269 	adv7511->bridge.vendor = "Analog";
1270 	adv7511->bridge.product = adv7511->info->name;
1271 
1272 #ifdef CONFIG_DRM_I2C_ADV7511_AUDIO
1273 	adv7511->bridge.ops |= DRM_BRIDGE_OP_HDMI_AUDIO;
1274 	adv7511->bridge.hdmi_audio_dev = dev;
1275 	adv7511->bridge.hdmi_audio_max_i2s_playback_channels = 2;
1276 	adv7511->bridge.hdmi_audio_i2s_formats = (SNDRV_PCM_FMTBIT_S16_LE |
1277 						  SNDRV_PCM_FMTBIT_S20_3LE |
1278 						  SNDRV_PCM_FMTBIT_S24_3LE |
1279 						  SNDRV_PCM_FMTBIT_S24_LE |
1280 						  SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE),
1281 	adv7511->bridge.hdmi_audio_spdif_playback = 1;
1282 	adv7511->bridge.hdmi_audio_dai_port = 2;
1283 #endif
1284 
1285 #ifdef CONFIG_DRM_I2C_ADV7511_CEC
1286 	adv7511->bridge.ops |= DRM_BRIDGE_OP_HDMI_CEC_ADAPTER;
1287 	adv7511->bridge.hdmi_cec_dev = dev;
1288 	adv7511->bridge.hdmi_cec_adapter_name = dev_name(dev);
1289 	adv7511->bridge.hdmi_cec_available_las = ADV7511_MAX_ADDRS;
1290 #endif
1291 
1292 	adv7511->bridge.of_node = dev->of_node;
1293 	adv7511->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
1294 
1295 	drm_bridge_add(&adv7511->bridge);
1296 
1297 	if (i2c->irq) {
1298 		init_waitqueue_head(&adv7511->wq);
1299 
1300 		ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
1301 						adv7511_irq_handler,
1302 						IRQF_ONESHOT | IRQF_SHARED,
1303 						dev_name(dev),
1304 						adv7511);
1305 		if (ret)
1306 			goto err_unregister_audio;
1307 	}
1308 
1309 	if (adv7511->info->has_dsi) {
1310 		ret = adv7533_attach_dsi(adv7511);
1311 		if (ret)
1312 			goto err_unregister_audio;
1313 	}
1314 
1315 	return 0;
1316 
1317 err_unregister_audio:
1318 	drm_bridge_remove(&adv7511->bridge);
1319 	i2c_unregister_device(adv7511->i2c_cec);
1320 	clk_disable_unprepare(adv7511->cec_clk);
1321 err_i2c_unregister_packet:
1322 	i2c_unregister_device(adv7511->i2c_packet);
1323 err_i2c_unregister_edid:
1324 	i2c_unregister_device(adv7511->i2c_edid);
1325 uninit_regulators:
1326 	adv7511_uninit_regulators(adv7511);
1327 err_of_node_put:
1328 	of_node_put(adv7511->host_node);
1329 
1330 	return ret;
1331 }
1332 
1333 static void adv7511_remove(struct i2c_client *i2c)
1334 {
1335 	struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1336 
1337 	of_node_put(adv7511->host_node);
1338 
1339 	adv7511_uninit_regulators(adv7511);
1340 
1341 	drm_bridge_remove(&adv7511->bridge);
1342 
1343 	i2c_unregister_device(adv7511->i2c_cec);
1344 	clk_disable_unprepare(adv7511->cec_clk);
1345 
1346 	i2c_unregister_device(adv7511->i2c_packet);
1347 	i2c_unregister_device(adv7511->i2c_edid);
1348 }
1349 
1350 static const struct adv7511_chip_info adv7511_chip_info = {
1351 	.type = ADV7511,
1352 	.name = "ADV7511",
1353 	.max_mode_clock_khz = 165000,
1354 	.supply_names = adv7511_supply_names,
1355 	.num_supplies = ARRAY_SIZE(adv7511_supply_names),
1356 	.link_config = true,
1357 };
1358 
1359 static const struct adv7511_chip_info adv7533_chip_info = {
1360 	.type = ADV7533,
1361 	.name = "ADV7533",
1362 	.max_mode_clock_khz = 80000,
1363 	.max_lane_freq_khz = 800000,
1364 	.supply_names = adv7533_supply_names,
1365 	.num_supplies = ARRAY_SIZE(adv7533_supply_names),
1366 	.reg_cec_offset = ADV7533_REG_CEC_OFFSET,
1367 	.has_dsi = true,
1368 };
1369 
1370 static const struct adv7511_chip_info adv7535_chip_info = {
1371 	.type = ADV7535,
1372 	.name = "ADV7535",
1373 	.max_mode_clock_khz = 148500,
1374 	.max_lane_freq_khz = 891000,
1375 	.supply_names = adv7533_supply_names,
1376 	.num_supplies = ARRAY_SIZE(adv7533_supply_names),
1377 	.reg_cec_offset = ADV7533_REG_CEC_OFFSET,
1378 	.has_dsi = true,
1379 	.hpd_override_enable = true,
1380 };
1381 
1382 static const struct i2c_device_id adv7511_i2c_ids[] = {
1383 	{ "adv7511", (kernel_ulong_t)&adv7511_chip_info },
1384 	{ "adv7511w", (kernel_ulong_t)&adv7511_chip_info },
1385 	{ "adv7513", (kernel_ulong_t)&adv7511_chip_info },
1386 	{ "adv7533", (kernel_ulong_t)&adv7533_chip_info },
1387 	{ "adv7535", (kernel_ulong_t)&adv7535_chip_info },
1388 	{ }
1389 };
1390 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1391 
1392 static const struct of_device_id adv7511_of_ids[] = {
1393 	{ .compatible = "adi,adv7511", .data = &adv7511_chip_info },
1394 	{ .compatible = "adi,adv7511w", .data = &adv7511_chip_info },
1395 	{ .compatible = "adi,adv7513", .data = &adv7511_chip_info },
1396 	{ .compatible = "adi,adv7533", .data = &adv7533_chip_info },
1397 	{ .compatible = "adi,adv7535", .data = &adv7535_chip_info },
1398 	{ }
1399 };
1400 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1401 
1402 static struct mipi_dsi_driver adv7533_dsi_driver = {
1403 	.driver.name = "adv7533",
1404 };
1405 
1406 static struct i2c_driver adv7511_driver = {
1407 	.driver = {
1408 		.name = "adv7511",
1409 		.of_match_table = adv7511_of_ids,
1410 	},
1411 	.id_table = adv7511_i2c_ids,
1412 	.probe = adv7511_probe,
1413 	.remove = adv7511_remove,
1414 };
1415 
1416 static int __init adv7511_init(void)
1417 {
1418 	int ret;
1419 
1420 	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) {
1421 		ret = mipi_dsi_driver_register(&adv7533_dsi_driver);
1422 		if (ret)
1423 			return ret;
1424 	}
1425 
1426 	ret = i2c_add_driver(&adv7511_driver);
1427 	if (ret) {
1428 		if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1429 			mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1430 	}
1431 
1432 	return ret;
1433 }
1434 module_init(adv7511_init);
1435 
1436 static void __exit adv7511_exit(void)
1437 {
1438 	i2c_del_driver(&adv7511_driver);
1439 
1440 	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1441 		mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1442 }
1443 module_exit(adv7511_exit);
1444 
1445 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1446 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1447 MODULE_LICENSE("GPL");
1448