xref: /linux/drivers/gpu/drm/bridge/adv7511/adv7511_drv.c (revision be1ca3ee8f97067fee87fda73ea5959d5ab75bbf)
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 static const struct regmap_config adv7511_packet_config = {
136 	.reg_bits = 8,
137 	.val_bits = 8,
138 
139 	.max_register = 0xff,
140 };
141 
142 /* -----------------------------------------------------------------------------
143  * Hardware configuration
144  */
145 
146 static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
147 				 const uint16_t *coeff,
148 				 unsigned int scaling_factor)
149 {
150 	unsigned int i;
151 
152 	regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
153 			   ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
154 
155 	if (enable) {
156 		for (i = 0; i < 12; ++i) {
157 			regmap_update_bits(adv7511->regmap,
158 					   ADV7511_REG_CSC_UPPER(i),
159 					   0x1f, coeff[i] >> 8);
160 			regmap_write(adv7511->regmap,
161 				     ADV7511_REG_CSC_LOWER(i),
162 				     coeff[i] & 0xff);
163 		}
164 	}
165 
166 	if (enable)
167 		regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
168 				   0xe0, 0x80 | (scaling_factor << 5));
169 	else
170 		regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
171 				   0x80, 0x00);
172 
173 	regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
174 			   ADV7511_CSC_UPDATE_MODE, 0);
175 }
176 
177 static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
178 {
179 	if (packet & 0xff)
180 		regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
181 				   packet, 0xff);
182 
183 	if (packet & 0xff00) {
184 		packet >>= 8;
185 		regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
186 				   packet, 0xff);
187 	}
188 
189 	return 0;
190 }
191 
192 static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
193 {
194 	if (packet & 0xff)
195 		regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
196 				   packet, 0x00);
197 
198 	if (packet & 0xff00) {
199 		packet >>= 8;
200 		regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
201 				   packet, 0x00);
202 	}
203 
204 	return 0;
205 }
206 
207 /* Coefficients for adv7511 color space conversion */
208 static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
209 	0x0734, 0x04ad, 0x0000, 0x1c1b,
210 	0x1ddc, 0x04ad, 0x1f24, 0x0135,
211 	0x0000, 0x04ad, 0x087c, 0x1b77,
212 };
213 
214 static void adv7511_set_config_csc(struct adv7511 *adv7511,
215 				   struct drm_connector *connector,
216 				   bool rgb)
217 {
218 	struct adv7511_video_config config;
219 	bool output_format_422, output_format_ycbcr;
220 	unsigned int mode;
221 
222 	if (rgb) {
223 		config.csc_enable = false;
224 		output_format_422 = false;
225 		output_format_ycbcr = false;
226 	} else {
227 		config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
228 		config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
229 
230 		if ((connector->display_info.color_formats &
231 		     DRM_COLOR_FORMAT_YCBCR422) &&
232 		    connector->display_info.is_hdmi) {
233 			config.csc_enable = false;
234 			output_format_422 = true;
235 			output_format_ycbcr = true;
236 		} else {
237 			config.csc_enable = true;
238 			output_format_422 = false;
239 			output_format_ycbcr = false;
240 		}
241 	}
242 
243 	if (connector->display_info.is_hdmi)
244 		mode = ADV7511_HDMI_CFG_MODE_HDMI;
245 	else
246 		mode = ADV7511_HDMI_CFG_MODE_DVI;
247 
248 	adv7511_set_colormap(adv7511, config.csc_enable,
249 			     config.csc_coefficents,
250 			     config.csc_scaling_factor);
251 
252 	regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
253 			   (output_format_422 << 7) | output_format_ycbcr);
254 
255 	regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
256 			   ADV7511_HDMI_CFG_MODE_MASK, mode);
257 }
258 
259 static void adv7511_set_link_config(struct adv7511 *adv7511,
260 				    const struct adv7511_link_config *config)
261 {
262 	/*
263 	 * The input style values documented in the datasheet don't match the
264 	 * hardware register field values :-(
265 	 */
266 	static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
267 
268 	unsigned int clock_delay;
269 	unsigned int color_depth;
270 	unsigned int input_id;
271 
272 	clock_delay = (config->clock_delay + 1200) / 400;
273 	color_depth = config->input_color_depth == 8 ? 3
274 		    : (config->input_color_depth == 10 ? 1 : 2);
275 
276 	/* TODO Support input ID 6 */
277 	if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
278 		input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
279 			 ? 5 : 0;
280 	else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
281 		input_id = config->embedded_sync ? 8 : 7;
282 	else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
283 		input_id = config->embedded_sync ? 4 : 3;
284 	else
285 		input_id = config->embedded_sync ? 2 : 1;
286 
287 	regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
288 			   input_id);
289 	regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
290 			   (color_depth << 4) |
291 			   (input_styles[config->input_style] << 2));
292 	regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
293 		     config->input_justification << 3);
294 	regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
295 		     config->sync_pulse << 2);
296 
297 	regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
298 
299 	adv7511->embedded_sync = config->embedded_sync;
300 	adv7511->hsync_polarity = config->hsync_polarity;
301 	adv7511->vsync_polarity = config->vsync_polarity;
302 	adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
303 }
304 
305 static void __adv7511_power_on(struct adv7511 *adv7511)
306 {
307 	adv7511->current_edid_segment = -1;
308 
309 	regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
310 			   ADV7511_POWER_POWER_DOWN, 0);
311 	if (adv7511->i2c_main->irq) {
312 		/*
313 		 * Documentation says the INT_ENABLE registers are reset in
314 		 * POWER_DOWN mode. My 7511w preserved the bits, however.
315 		 * Still, let's be safe and stick to the documentation.
316 		 */
317 		regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
318 			     ADV7511_INT0_EDID_READY | ADV7511_INT0_HPD);
319 		regmap_update_bits(adv7511->regmap,
320 				   ADV7511_REG_INT_ENABLE(1),
321 				   ADV7511_INT1_DDC_ERROR,
322 				   ADV7511_INT1_DDC_ERROR);
323 	}
324 
325 	/*
326 	 * Per spec it is allowed to pulse the HPD signal to indicate that the
327 	 * EDID information has changed. Some monitors do this when they wakeup
328 	 * from standby or are enabled. When the HPD goes low the adv7511 is
329 	 * reset and the outputs are disabled which might cause the monitor to
330 	 * go to standby again. To avoid this we ignore the HPD pin for the
331 	 * first few seconds after enabling the output. On the other hand
332 	 * adv7535 require to enable HPD Override bit for proper HPD.
333 	 */
334 	if (adv7511->info->hpd_override_enable)
335 		regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
336 				   ADV7535_REG_POWER2_HPD_OVERRIDE,
337 				   ADV7535_REG_POWER2_HPD_OVERRIDE);
338 	else
339 		regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
340 				   ADV7511_REG_POWER2_HPD_SRC_MASK,
341 				   ADV7511_REG_POWER2_HPD_SRC_NONE);
342 }
343 
344 static void adv7511_power_on(struct adv7511 *adv7511)
345 {
346 	__adv7511_power_on(adv7511);
347 
348 	/*
349 	 * Most of the registers are reset during power down or when HPD is low.
350 	 */
351 	regcache_sync(adv7511->regmap);
352 
353 	if (adv7511->info->has_dsi)
354 		adv7533_dsi_power_on(adv7511);
355 	adv7511->powered = true;
356 }
357 
358 static void __adv7511_power_off(struct adv7511 *adv7511)
359 {
360 	/* TODO: setup additional power down modes */
361 	if (adv7511->info->hpd_override_enable)
362 		regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
363 				   ADV7535_REG_POWER2_HPD_OVERRIDE, 0);
364 
365 	regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
366 			   ADV7511_POWER_POWER_DOWN,
367 			   ADV7511_POWER_POWER_DOWN);
368 	regmap_update_bits(adv7511->regmap,
369 			   ADV7511_REG_INT_ENABLE(1),
370 			   ADV7511_INT1_DDC_ERROR, 0);
371 	regcache_mark_dirty(adv7511->regmap);
372 }
373 
374 static void adv7511_power_off(struct adv7511 *adv7511)
375 {
376 	__adv7511_power_off(adv7511);
377 	if (adv7511->info->has_dsi)
378 		adv7533_dsi_power_off(adv7511);
379 	adv7511->powered = false;
380 }
381 
382 /* -----------------------------------------------------------------------------
383  * Interrupt and hotplug detection
384  */
385 
386 static bool adv7511_hpd(struct adv7511 *adv7511)
387 {
388 	unsigned int irq0;
389 	int ret;
390 
391 	ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
392 	if (ret < 0)
393 		return false;
394 
395 	if (irq0 & ADV7511_INT0_HPD) {
396 		regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
397 			     ADV7511_INT0_HPD);
398 		return true;
399 	}
400 
401 	return false;
402 }
403 
404 static void adv7511_hpd_work(struct work_struct *work)
405 {
406 	struct adv7511 *adv7511 = container_of(work, struct adv7511, hpd_work);
407 	enum drm_connector_status status;
408 	unsigned int val;
409 	int ret;
410 
411 	ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
412 	if (ret < 0)
413 		status = connector_status_disconnected;
414 	else if (val & ADV7511_STATUS_HPD)
415 		status = connector_status_connected;
416 	else
417 		status = connector_status_disconnected;
418 
419 	/*
420 	 * The bridge resets its registers on unplug. So when we get a plug
421 	 * event and we're already supposed to be powered, cycle the bridge to
422 	 * restore its state.
423 	 */
424 	if (status == connector_status_connected &&
425 	    adv7511->status == connector_status_disconnected &&
426 	    adv7511->powered) {
427 		regcache_mark_dirty(adv7511->regmap);
428 		adv7511_power_on(adv7511);
429 	}
430 
431 	if (adv7511->status != status) {
432 		adv7511->status = status;
433 
434 		drm_bridge_hpd_notify(&adv7511->bridge, status);
435 	}
436 }
437 
438 static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
439 {
440 	unsigned int irq0, irq1;
441 	int ret;
442 	int cec_status = IRQ_NONE;
443 	int irq_status = IRQ_NONE;
444 
445 	ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
446 	if (ret < 0)
447 		return ret;
448 
449 	ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
450 	if (ret < 0)
451 		return ret;
452 
453 	regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
454 	regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
455 
456 	if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder) {
457 		schedule_work(&adv7511->hpd_work);
458 		irq_status = IRQ_HANDLED;
459 	}
460 
461 	if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
462 		adv7511->edid_read = true;
463 
464 		if (adv7511->i2c_main->irq)
465 			wake_up_all(&adv7511->wq);
466 		irq_status = IRQ_HANDLED;
467 	}
468 
469 #ifdef CONFIG_DRM_I2C_ADV7511_CEC
470 	cec_status = adv7511_cec_irq_process(adv7511, irq1);
471 #endif
472 
473 	/* If there is no IRQ to handle, exit indicating no IRQ data */
474 	if (irq_status == IRQ_HANDLED || cec_status == IRQ_HANDLED)
475 		return IRQ_HANDLED;
476 
477 	return IRQ_NONE;
478 }
479 
480 static irqreturn_t adv7511_irq_handler(int irq, void *devid)
481 {
482 	struct adv7511 *adv7511 = devid;
483 	int ret;
484 
485 	ret = adv7511_irq_process(adv7511, true);
486 	return ret < 0 ? IRQ_NONE : ret;
487 }
488 
489 /* -----------------------------------------------------------------------------
490  * EDID retrieval
491  */
492 
493 static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
494 {
495 	int ret;
496 
497 	if (adv7511->i2c_main->irq) {
498 		ret = wait_event_interruptible_timeout(adv7511->wq,
499 				adv7511->edid_read, msecs_to_jiffies(timeout));
500 	} else {
501 		for (; timeout > 0; timeout -= 25) {
502 			ret = adv7511_irq_process(adv7511, false);
503 			if (ret < 0)
504 				break;
505 
506 			if (adv7511->edid_read)
507 				break;
508 
509 			msleep(25);
510 		}
511 	}
512 
513 	return adv7511->edid_read ? 0 : -EIO;
514 }
515 
516 static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
517 				  size_t len)
518 {
519 	struct adv7511 *adv7511 = data;
520 	struct i2c_msg xfer[2];
521 	uint8_t offset;
522 	unsigned int i;
523 	int ret;
524 
525 	if (len > 128)
526 		return -EINVAL;
527 
528 	if (adv7511->current_edid_segment != block / 2) {
529 		unsigned int status;
530 
531 		ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
532 				  &status);
533 		if (ret < 0)
534 			return ret;
535 
536 		if (status != 2) {
537 			adv7511->edid_read = false;
538 			regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
539 				     block);
540 			ret = adv7511_wait_for_edid(adv7511, 200);
541 			if (ret < 0)
542 				return ret;
543 		}
544 
545 		/* Break this apart, hopefully more I2C controllers will
546 		 * support 64 byte transfers than 256 byte transfers
547 		 */
548 
549 		xfer[0].addr = adv7511->i2c_edid->addr;
550 		xfer[0].flags = 0;
551 		xfer[0].len = 1;
552 		xfer[0].buf = &offset;
553 		xfer[1].addr = adv7511->i2c_edid->addr;
554 		xfer[1].flags = I2C_M_RD;
555 		xfer[1].len = 64;
556 		xfer[1].buf = adv7511->edid_buf;
557 
558 		offset = 0;
559 
560 		for (i = 0; i < 4; ++i) {
561 			ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
562 					   ARRAY_SIZE(xfer));
563 			if (ret < 0)
564 				return ret;
565 			else if (ret != 2)
566 				return -EIO;
567 
568 			xfer[1].buf += 64;
569 			offset += 64;
570 		}
571 
572 		adv7511->current_edid_segment = block / 2;
573 	}
574 
575 	if (block % 2 == 0)
576 		memcpy(buf, adv7511->edid_buf, len);
577 	else
578 		memcpy(buf, adv7511->edid_buf + 128, len);
579 
580 	return 0;
581 }
582 
583 /* -----------------------------------------------------------------------------
584  * ADV75xx helpers
585  */
586 
587 static const struct drm_edid *adv7511_edid_read(struct adv7511 *adv7511,
588 						struct drm_connector *connector)
589 {
590 	const struct drm_edid *drm_edid;
591 
592 	/* Reading the EDID only works if the device is powered */
593 	if (!adv7511->powered) {
594 		unsigned int edid_i2c_addr =
595 					(adv7511->i2c_edid->addr << 1);
596 
597 		__adv7511_power_on(adv7511);
598 
599 		/* Reset the EDID_I2C_ADDR register as it might be cleared */
600 		regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
601 			     edid_i2c_addr);
602 	}
603 
604 	drm_edid = drm_edid_read_custom(connector, adv7511_get_edid_block, adv7511);
605 
606 	if (!adv7511->powered)
607 		__adv7511_power_off(adv7511);
608 
609 	return drm_edid;
610 }
611 
612 static enum drm_connector_status
613 adv7511_detect(struct adv7511 *adv7511)
614 {
615 	enum drm_connector_status status;
616 	unsigned int val;
617 	bool hpd;
618 	int ret;
619 
620 	ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
621 	if (ret < 0)
622 		return connector_status_disconnected;
623 
624 	if (val & ADV7511_STATUS_HPD)
625 		status = connector_status_connected;
626 	else
627 		status = connector_status_disconnected;
628 
629 	hpd = adv7511_hpd(adv7511);
630 
631 	/* The chip resets itself when the cable is disconnected, so in case
632 	 * there is a pending HPD interrupt and the cable is connected there was
633 	 * at least one transition from disconnected to connected and the chip
634 	 * has to be reinitialized. */
635 	if (status == connector_status_connected && hpd && adv7511->powered) {
636 		regcache_mark_dirty(adv7511->regmap);
637 		adv7511_power_on(adv7511);
638 		if (adv7511->status == connector_status_connected)
639 			status = connector_status_disconnected;
640 	} else {
641 		/* Renable HPD sensing */
642 		if (adv7511->info->hpd_override_enable)
643 			regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
644 					   ADV7535_REG_POWER2_HPD_OVERRIDE,
645 					   ADV7535_REG_POWER2_HPD_OVERRIDE);
646 		else
647 			regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
648 					   ADV7511_REG_POWER2_HPD_SRC_MASK,
649 					   ADV7511_REG_POWER2_HPD_SRC_BOTH);
650 	}
651 
652 	adv7511->status = status;
653 	return status;
654 }
655 
656 static void adv7511_mode_set(struct adv7511 *adv7511,
657 			     const struct drm_display_mode *adj_mode)
658 {
659 	unsigned int low_refresh_rate;
660 	unsigned int hsync_polarity = 0;
661 	unsigned int vsync_polarity = 0;
662 
663 	if (adv7511->embedded_sync) {
664 		unsigned int hsync_offset, hsync_len;
665 		unsigned int vsync_offset, vsync_len;
666 
667 		hsync_offset = adj_mode->crtc_hsync_start -
668 			       adj_mode->crtc_hdisplay;
669 		vsync_offset = adj_mode->crtc_vsync_start -
670 			       adj_mode->crtc_vdisplay;
671 		hsync_len = adj_mode->crtc_hsync_end -
672 			    adj_mode->crtc_hsync_start;
673 		vsync_len = adj_mode->crtc_vsync_end -
674 			    adj_mode->crtc_vsync_start;
675 
676 		/* The hardware vsync generator has a off-by-one bug */
677 		vsync_offset += 1;
678 
679 		regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
680 			     ((hsync_offset >> 10) & 0x7) << 5);
681 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
682 			     (hsync_offset >> 2) & 0xff);
683 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
684 			     ((hsync_offset & 0x3) << 6) |
685 			     ((hsync_len >> 4) & 0x3f));
686 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
687 			     ((hsync_len & 0xf) << 4) |
688 			     ((vsync_offset >> 6) & 0xf));
689 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
690 			     ((vsync_offset & 0x3f) << 2) |
691 			     ((vsync_len >> 8) & 0x3));
692 		regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
693 			     vsync_len & 0xff);
694 
695 		hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
696 		vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
697 	} else {
698 		enum adv7511_sync_polarity mode_hsync_polarity;
699 		enum adv7511_sync_polarity mode_vsync_polarity;
700 
701 		/**
702 		 * If the input signal is always low or always high we want to
703 		 * invert or let it passthrough depending on the polarity of the
704 		 * current mode.
705 		 **/
706 		if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
707 			mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
708 		else
709 			mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
710 
711 		if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
712 			mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
713 		else
714 			mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
715 
716 		if (adv7511->hsync_polarity != mode_hsync_polarity &&
717 		    adv7511->hsync_polarity !=
718 		    ADV7511_SYNC_POLARITY_PASSTHROUGH)
719 			hsync_polarity = 1;
720 
721 		if (adv7511->vsync_polarity != mode_vsync_polarity &&
722 		    adv7511->vsync_polarity !=
723 		    ADV7511_SYNC_POLARITY_PASSTHROUGH)
724 			vsync_polarity = 1;
725 	}
726 
727 	if (drm_mode_vrefresh(adj_mode) <= 24)
728 		low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
729 	else if (drm_mode_vrefresh(adj_mode) <= 25)
730 		low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
731 	else if (drm_mode_vrefresh(adj_mode) <= 30)
732 		low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
733 	else
734 		low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
735 
736 	if (adv7511->info->type == ADV7511)
737 		regmap_update_bits(adv7511->regmap, 0xfb,
738 				   0x6, low_refresh_rate << 1);
739 	else
740 		regmap_update_bits(adv7511->regmap, 0x4a,
741 				   0xc, low_refresh_rate << 2);
742 
743 	regmap_update_bits(adv7511->regmap, 0x17,
744 		0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
745 
746 	drm_mode_copy(&adv7511->curr_mode, adj_mode);
747 
748 	/* Update horizontal/vertical porch params */
749 	if (adv7511->info->has_dsi && adv7511->use_timing_gen)
750 		adv7533_dsi_config_timing_gen(adv7511);
751 
752 	/*
753 	 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
754 	 * supposed to give better results.
755 	 */
756 
757 	adv7511->f_tmds = adj_mode->clock;
758 }
759 
760 static int adv7511_connector_init(struct adv7511 *adv)
761 {
762 	struct drm_bridge *bridge = &adv->bridge;
763 	struct drm_connector *connector;
764 
765 	connector = drm_bridge_connector_init(bridge->dev, bridge->encoder);
766 	if (IS_ERR(connector)) {
767 		DRM_ERROR("Failed to initialize connector with drm\n");
768 		return PTR_ERR(connector);
769 	}
770 
771 	drm_connector_attach_encoder(connector, bridge->encoder);
772 
773 	return 0;
774 }
775 
776 /* -----------------------------------------------------------------------------
777  * DRM Bridge Operations
778  */
779 
780 static const struct adv7511 *bridge_to_adv7511_const(const struct drm_bridge *bridge)
781 {
782 	return container_of(bridge, struct adv7511, bridge);
783 }
784 
785 static void adv7511_bridge_atomic_enable(struct drm_bridge *bridge,
786 					 struct drm_atomic_state *state)
787 {
788 	struct adv7511 *adv = bridge_to_adv7511(bridge);
789 	struct drm_connector *connector;
790 	struct drm_connector_state *conn_state;
791 	struct drm_crtc_state *crtc_state;
792 
793 	adv7511_power_on(adv);
794 
795 	connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder);
796 	if (WARN_ON(!connector))
797 		return;
798 
799 	conn_state = drm_atomic_get_new_connector_state(state, connector);
800 	if (WARN_ON(!conn_state))
801 		return;
802 
803 	crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
804 	if (WARN_ON(!crtc_state))
805 		return;
806 
807 	adv7511_set_config_csc(adv, connector, adv->rgb);
808 
809 	adv7511_mode_set(adv, &crtc_state->adjusted_mode);
810 
811 	drm_atomic_helper_connector_hdmi_update_infoframes(connector, state);
812 }
813 
814 static void adv7511_bridge_atomic_disable(struct drm_bridge *bridge,
815 					  struct drm_atomic_state *state)
816 {
817 	struct adv7511 *adv = bridge_to_adv7511(bridge);
818 
819 	adv7511_power_off(adv);
820 }
821 
822 static enum drm_mode_status
823 adv7511_bridge_hdmi_tmds_char_rate_valid(const struct drm_bridge *bridge,
824 					 const struct drm_display_mode *mode,
825 					 unsigned long long tmds_rate)
826 {
827 	const struct adv7511 *adv = bridge_to_adv7511_const(bridge);
828 
829 	if (tmds_rate > 1000ULL * adv->info->max_mode_clock_khz)
830 		return MODE_CLOCK_HIGH;
831 
832 	return MODE_OK;
833 }
834 
835 static enum drm_mode_status adv7511_bridge_mode_valid(struct drm_bridge *bridge,
836 						      const struct drm_display_info *info,
837 		const struct drm_display_mode *mode)
838 {
839 	struct adv7511 *adv = bridge_to_adv7511(bridge);
840 
841 	if (!adv->info->has_dsi)
842 		return MODE_OK;
843 
844 	return adv7533_mode_valid(adv, mode);
845 }
846 
847 static int adv7511_bridge_attach(struct drm_bridge *bridge,
848 				 struct drm_encoder *encoder,
849 				 enum drm_bridge_attach_flags flags)
850 {
851 	struct adv7511 *adv = bridge_to_adv7511(bridge);
852 	int ret = 0;
853 
854 	if (adv->next_bridge) {
855 		ret = drm_bridge_attach(encoder, adv->next_bridge, bridge,
856 					flags | DRM_BRIDGE_ATTACH_NO_CONNECTOR);
857 		if (ret)
858 			return ret;
859 	}
860 
861 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
862 		ret = adv7511_connector_init(adv);
863 		if (ret < 0)
864 			return ret;
865 	}
866 
867 	if (adv->i2c_main->irq)
868 		regmap_write(adv->regmap, ADV7511_REG_INT_ENABLE(0),
869 			     ADV7511_INT0_HPD);
870 
871 	return ret;
872 }
873 
874 static enum drm_connector_status
875 adv7511_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)
876 {
877 	struct adv7511 *adv = bridge_to_adv7511(bridge);
878 
879 	return adv7511_detect(adv);
880 }
881 
882 static const struct drm_edid *adv7511_bridge_edid_read(struct drm_bridge *bridge,
883 						       struct drm_connector *connector)
884 {
885 	struct adv7511 *adv = bridge_to_adv7511(bridge);
886 
887 	return adv7511_edid_read(adv, connector);
888 }
889 
890 static int adv7511_bridge_hdmi_clear_audio_infoframe(struct drm_bridge *bridge)
891 {
892 	struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
893 
894 	adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AUDIO_INFOFRAME);
895 
896 	return 0;
897 }
898 
899 static int adv7511_bridge_hdmi_clear_avi_infoframe(struct drm_bridge *bridge)
900 {
901 	struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
902 
903 	adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
904 
905 	return 0;
906 }
907 
908 static int adv7511_bridge_hdmi_clear_spd_infoframe(struct drm_bridge *bridge)
909 {
910 	struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
911 
912 	adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_SPD);
913 
914 	return 0;
915 }
916 
917 static int adv7511_bridge_hdmi_clear_hdmi_infoframe(struct drm_bridge *bridge)
918 {
919 	struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
920 
921 	adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_SPARE1);
922 
923 	return 0;
924 }
925 
926 static int adv7511_bridge_hdmi_write_audio_infoframe(struct drm_bridge *bridge,
927 						     const u8 *buffer, size_t len)
928 {
929 	struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
930 
931 	/* send current Audio infoframe values while updating */
932 	regmap_update_bits(adv7511->regmap, ADV7511_REG_INFOFRAME_UPDATE,
933 			   BIT(5), BIT(5));
934 
935 	/* The Audio infoframe id is not configurable */
936 	regmap_bulk_write(adv7511->regmap, ADV7511_REG_AUDIO_INFOFRAME_VERSION,
937 			  buffer + 1, len - 1);
938 
939 	/* use Audio infoframe updated info */
940 	regmap_update_bits(adv7511->regmap, ADV7511_REG_INFOFRAME_UPDATE,
941 			   BIT(5), 0);
942 
943 	adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AUDIO_INFOFRAME);
944 
945 	return 0;
946 }
947 
948 static int adv7511_bridge_hdmi_write_avi_infoframe(struct drm_bridge *bridge,
949 						   const u8 *buffer, size_t len)
950 {
951 	struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
952 
953 	/* send current AVI infoframe values while updating */
954 	regmap_update_bits(adv7511->regmap, ADV7511_REG_INFOFRAME_UPDATE,
955 			   BIT(6), BIT(6));
956 
957 	/* The AVI infoframe id is not configurable */
958 	regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
959 			  buffer + 1, len - 1);
960 
961 	regmap_write(adv7511->regmap, ADV7511_REG_AUDIO_INFOFRAME_LENGTH, 0x2);
962 	regmap_write(adv7511->regmap, ADV7511_REG_AUDIO_INFOFRAME(1), 0x1);
963 
964 	/* use AVI infoframe updated info */
965 	regmap_update_bits(adv7511->regmap, ADV7511_REG_INFOFRAME_UPDATE,
966 			   BIT(6), 0);
967 
968 	adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
969 
970 	return 0;
971 }
972 
973 static int adv7511_bridge_hdmi_write_spd_infoframe(struct drm_bridge *bridge,
974 						   const u8 *buffer, size_t len)
975 {
976 	struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
977 
978 	adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_SPD);
979 	regmap_bulk_write(adv7511->regmap_packet, ADV7511_PACKET_SPD(0),
980 			  buffer, len);
981 	adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_SPD);
982 
983 	return 0;
984 }
985 
986 static int adv7511_bridge_hdmi_write_hdmi_infoframe(struct drm_bridge *bridge,
987 						    const u8 *buffer, size_t len)
988 {
989 	struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
990 
991 	adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_SPARE1);
992 	regmap_bulk_write(adv7511->regmap_packet, ADV7511_PACKET_SPARE1(0),
993 			  buffer, len);
994 	adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_SPARE1);
995 
996 	return 0;
997 }
998 
999 static const struct drm_bridge_funcs adv7511_bridge_funcs = {
1000 	.mode_valid = adv7511_bridge_mode_valid,
1001 	.attach = adv7511_bridge_attach,
1002 	.detect = adv7511_bridge_detect,
1003 	.edid_read = adv7511_bridge_edid_read,
1004 
1005 	.atomic_enable = adv7511_bridge_atomic_enable,
1006 	.atomic_disable = adv7511_bridge_atomic_disable,
1007 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
1008 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
1009 	.atomic_reset = drm_atomic_helper_bridge_reset,
1010 
1011 	.hdmi_tmds_char_rate_valid = adv7511_bridge_hdmi_tmds_char_rate_valid,
1012 	.hdmi_clear_audio_infoframe = adv7511_bridge_hdmi_clear_audio_infoframe,
1013 	.hdmi_write_audio_infoframe = adv7511_bridge_hdmi_write_audio_infoframe,
1014 	.hdmi_clear_avi_infoframe = adv7511_bridge_hdmi_clear_avi_infoframe,
1015 	.hdmi_write_avi_infoframe = adv7511_bridge_hdmi_write_avi_infoframe,
1016 	.hdmi_clear_spd_infoframe = adv7511_bridge_hdmi_clear_spd_infoframe,
1017 	.hdmi_write_spd_infoframe = adv7511_bridge_hdmi_write_spd_infoframe,
1018 	.hdmi_clear_hdmi_infoframe = adv7511_bridge_hdmi_clear_hdmi_infoframe,
1019 	.hdmi_write_hdmi_infoframe = adv7511_bridge_hdmi_write_hdmi_infoframe,
1020 
1021 	.hdmi_audio_startup = adv7511_hdmi_audio_startup,
1022 	.hdmi_audio_prepare = adv7511_hdmi_audio_prepare,
1023 	.hdmi_audio_shutdown = adv7511_hdmi_audio_shutdown,
1024 
1025 	.hdmi_cec_init = adv7511_cec_init,
1026 	.hdmi_cec_enable = adv7511_cec_enable,
1027 	.hdmi_cec_log_addr = adv7511_cec_log_addr,
1028 	.hdmi_cec_transmit = adv7511_cec_transmit,
1029 };
1030 
1031 /* -----------------------------------------------------------------------------
1032  * Probe & remove
1033  */
1034 
1035 static const char * const adv7511_supply_names[] = {
1036 	"avdd",
1037 	"dvdd",
1038 	"pvdd",
1039 	"bgvdd",
1040 	"dvdd-3v",
1041 };
1042 
1043 static const char * const adv7533_supply_names[] = {
1044 	"avdd",
1045 	"dvdd",
1046 	"pvdd",
1047 	"a2vdd",
1048 	"v3p3",
1049 	"v1p2",
1050 };
1051 
1052 static int adv7511_init_regulators(struct adv7511 *adv)
1053 {
1054 	const char * const *supply_names = adv->info->supply_names;
1055 	unsigned int num_supplies = adv->info->num_supplies;
1056 	struct device *dev = &adv->i2c_main->dev;
1057 	unsigned int i;
1058 	int ret;
1059 
1060 	adv->supplies = devm_kcalloc(dev, num_supplies,
1061 				     sizeof(*adv->supplies), GFP_KERNEL);
1062 	if (!adv->supplies)
1063 		return -ENOMEM;
1064 
1065 	for (i = 0; i < num_supplies; i++)
1066 		adv->supplies[i].supply = supply_names[i];
1067 
1068 	ret = devm_regulator_bulk_get(dev, num_supplies, adv->supplies);
1069 	if (ret)
1070 		return ret;
1071 
1072 	return regulator_bulk_enable(num_supplies, adv->supplies);
1073 }
1074 
1075 static void adv7511_uninit_regulators(struct adv7511 *adv)
1076 {
1077 	regulator_bulk_disable(adv->info->num_supplies, adv->supplies);
1078 }
1079 
1080 static bool adv7511_cec_register_volatile(struct device *dev, unsigned int reg)
1081 {
1082 	struct i2c_client *i2c = to_i2c_client(dev);
1083 	struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1084 
1085 	reg -= adv7511->info->reg_cec_offset;
1086 
1087 	switch (reg) {
1088 	case ADV7511_REG_CEC_RX1_FRAME_HDR:
1089 	case ADV7511_REG_CEC_RX1_FRAME_DATA0 ... ADV7511_REG_CEC_RX1_FRAME_DATA0 + 14:
1090 	case ADV7511_REG_CEC_RX1_FRAME_LEN:
1091 	case ADV7511_REG_CEC_RX2_FRAME_HDR:
1092 	case ADV7511_REG_CEC_RX2_FRAME_DATA0 ... ADV7511_REG_CEC_RX2_FRAME_DATA0 + 14:
1093 	case ADV7511_REG_CEC_RX2_FRAME_LEN:
1094 	case ADV7511_REG_CEC_RX3_FRAME_HDR:
1095 	case ADV7511_REG_CEC_RX3_FRAME_DATA0 ... ADV7511_REG_CEC_RX3_FRAME_DATA0 + 14:
1096 	case ADV7511_REG_CEC_RX3_FRAME_LEN:
1097 	case ADV7511_REG_CEC_RX_STATUS:
1098 	case ADV7511_REG_CEC_RX_BUFFERS:
1099 	case ADV7511_REG_CEC_TX_LOW_DRV_CNT:
1100 		return true;
1101 	}
1102 
1103 	return false;
1104 }
1105 
1106 static const struct regmap_config adv7511_cec_regmap_config = {
1107 	.reg_bits = 8,
1108 	.val_bits = 8,
1109 
1110 	.max_register = 0xff,
1111 	.cache_type = REGCACHE_MAPLE,
1112 	.volatile_reg = adv7511_cec_register_volatile,
1113 };
1114 
1115 static int adv7511_init_cec_regmap(struct adv7511 *adv)
1116 {
1117 	int ret;
1118 
1119 	adv->i2c_cec = i2c_new_ancillary_device(adv->i2c_main, "cec",
1120 						ADV7511_CEC_I2C_ADDR_DEFAULT);
1121 	if (IS_ERR(adv->i2c_cec))
1122 		return PTR_ERR(adv->i2c_cec);
1123 
1124 	regmap_write(adv->regmap, ADV7511_REG_CEC_I2C_ADDR,
1125 		     adv->i2c_cec->addr << 1);
1126 
1127 	i2c_set_clientdata(adv->i2c_cec, adv);
1128 
1129 	adv->regmap_cec = devm_regmap_init_i2c(adv->i2c_cec,
1130 					&adv7511_cec_regmap_config);
1131 	if (IS_ERR(adv->regmap_cec)) {
1132 		ret = PTR_ERR(adv->regmap_cec);
1133 		goto err;
1134 	}
1135 
1136 	if (adv->info->reg_cec_offset == ADV7533_REG_CEC_OFFSET) {
1137 		ret = adv7533_patch_cec_registers(adv);
1138 		if (ret)
1139 			goto err;
1140 	}
1141 
1142 	return 0;
1143 err:
1144 	i2c_unregister_device(adv->i2c_cec);
1145 	return ret;
1146 }
1147 
1148 static int adv7511_parse_dt(struct device_node *np,
1149 			    struct adv7511_link_config *config)
1150 {
1151 	const char *str;
1152 	int ret;
1153 
1154 	of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
1155 	if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
1156 	    config->input_color_depth != 12)
1157 		return -EINVAL;
1158 
1159 	ret = of_property_read_string(np, "adi,input-colorspace", &str);
1160 	if (ret < 0)
1161 		return ret;
1162 
1163 	if (!strcmp(str, "rgb"))
1164 		config->input_colorspace = HDMI_COLORSPACE_RGB;
1165 	else if (!strcmp(str, "yuv422"))
1166 		config->input_colorspace = HDMI_COLORSPACE_YUV422;
1167 	else if (!strcmp(str, "yuv444"))
1168 		config->input_colorspace = HDMI_COLORSPACE_YUV444;
1169 	else
1170 		return -EINVAL;
1171 
1172 	ret = of_property_read_string(np, "adi,input-clock", &str);
1173 	if (ret < 0)
1174 		return ret;
1175 
1176 	if (!strcmp(str, "1x"))
1177 		config->input_clock = ADV7511_INPUT_CLOCK_1X;
1178 	else if (!strcmp(str, "2x"))
1179 		config->input_clock = ADV7511_INPUT_CLOCK_2X;
1180 	else if (!strcmp(str, "ddr"))
1181 		config->input_clock = ADV7511_INPUT_CLOCK_DDR;
1182 	else
1183 		return -EINVAL;
1184 
1185 	if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
1186 	    config->input_clock != ADV7511_INPUT_CLOCK_1X) {
1187 		ret = of_property_read_u32(np, "adi,input-style",
1188 					   &config->input_style);
1189 		if (ret)
1190 			return ret;
1191 
1192 		if (config->input_style < 1 || config->input_style > 3)
1193 			return -EINVAL;
1194 
1195 		ret = of_property_read_string(np, "adi,input-justification",
1196 					      &str);
1197 		if (ret < 0)
1198 			return ret;
1199 
1200 		if (!strcmp(str, "left"))
1201 			config->input_justification =
1202 				ADV7511_INPUT_JUSTIFICATION_LEFT;
1203 		else if (!strcmp(str, "evenly"))
1204 			config->input_justification =
1205 				ADV7511_INPUT_JUSTIFICATION_EVENLY;
1206 		else if (!strcmp(str, "right"))
1207 			config->input_justification =
1208 				ADV7511_INPUT_JUSTIFICATION_RIGHT;
1209 		else
1210 			return -EINVAL;
1211 
1212 	} else {
1213 		config->input_style = 1;
1214 		config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
1215 	}
1216 
1217 	of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
1218 	if (config->clock_delay < -1200 || config->clock_delay > 1600)
1219 		return -EINVAL;
1220 
1221 	config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
1222 
1223 	/* Hardcode the sync pulse configurations for now. */
1224 	config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
1225 	config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1226 	config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1227 
1228 	return 0;
1229 }
1230 
1231 static int adv7511_probe(struct i2c_client *i2c)
1232 {
1233 	struct adv7511_link_config link_config;
1234 	struct adv7511 *adv7511;
1235 	struct device *dev = &i2c->dev;
1236 	unsigned int val;
1237 	int ret;
1238 
1239 	if (!dev->of_node)
1240 		return -EINVAL;
1241 
1242 	adv7511 = devm_drm_bridge_alloc(dev, struct adv7511, bridge,
1243 					&adv7511_bridge_funcs);
1244 	if (IS_ERR(adv7511))
1245 		return PTR_ERR(adv7511);
1246 
1247 	adv7511->i2c_main = i2c;
1248 	adv7511->powered = false;
1249 	adv7511->status = connector_status_disconnected;
1250 	adv7511->info = i2c_get_match_data(i2c);
1251 
1252 	memset(&link_config, 0, sizeof(link_config));
1253 
1254 	ret = drm_of_find_panel_or_bridge(dev->of_node, 1, -1, NULL,
1255 					  &adv7511->next_bridge);
1256 	if (ret && ret != -ENODEV)
1257 		return ret;
1258 
1259 	if (adv7511->info->link_config)
1260 		ret = adv7511_parse_dt(dev->of_node, &link_config);
1261 	else
1262 		ret = adv7533_parse_dt(dev->of_node, adv7511);
1263 	if (ret)
1264 		return ret;
1265 
1266 	ret = adv7511_init_regulators(adv7511);
1267 	if (ret) {
1268 		dev_err_probe(dev, ret, "failed to init regulators\n");
1269 		goto err_of_node_put;
1270 	}
1271 
1272 	/*
1273 	 * The power down GPIO is optional. If present, toggle it from active to
1274 	 * inactive to wake up the encoder.
1275 	 */
1276 	adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
1277 	if (IS_ERR(adv7511->gpio_pd)) {
1278 		ret = PTR_ERR(adv7511->gpio_pd);
1279 		goto uninit_regulators;
1280 	}
1281 
1282 	if (adv7511->gpio_pd) {
1283 		usleep_range(5000, 6000);
1284 		gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
1285 	}
1286 
1287 	adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
1288 	if (IS_ERR(adv7511->regmap)) {
1289 		ret = PTR_ERR(adv7511->regmap);
1290 		goto uninit_regulators;
1291 	}
1292 
1293 	ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
1294 	if (ret)
1295 		goto uninit_regulators;
1296 	dev_dbg(dev, "Rev. %d\n", val);
1297 
1298 	if (adv7511->info->type == ADV7511)
1299 		ret = regmap_register_patch(adv7511->regmap,
1300 					    adv7511_fixed_registers,
1301 					    ARRAY_SIZE(adv7511_fixed_registers));
1302 	else
1303 		ret = adv7533_patch_registers(adv7511);
1304 	if (ret)
1305 		goto uninit_regulators;
1306 
1307 	adv7511_packet_disable(adv7511, 0xffff);
1308 
1309 	adv7511->i2c_edid = i2c_new_ancillary_device(i2c, "edid",
1310 					ADV7511_EDID_I2C_ADDR_DEFAULT);
1311 	if (IS_ERR(adv7511->i2c_edid)) {
1312 		ret = PTR_ERR(adv7511->i2c_edid);
1313 		goto uninit_regulators;
1314 	}
1315 
1316 	regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
1317 		     adv7511->i2c_edid->addr << 1);
1318 
1319 	adv7511->i2c_packet = i2c_new_ancillary_device(i2c, "packet",
1320 					ADV7511_PACKET_I2C_ADDR_DEFAULT);
1321 	if (IS_ERR(adv7511->i2c_packet)) {
1322 		ret = PTR_ERR(adv7511->i2c_packet);
1323 		goto err_i2c_unregister_edid;
1324 	}
1325 
1326 	adv7511->regmap_packet = devm_regmap_init_i2c(adv7511->i2c_packet,
1327 						      &adv7511_packet_config);
1328 	if (IS_ERR(adv7511->regmap_packet)) {
1329 		ret = PTR_ERR(adv7511->regmap_packet);
1330 		goto err_i2c_unregister_packet;
1331 	}
1332 
1333 	regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
1334 		     adv7511->i2c_packet->addr << 1);
1335 
1336 	ret = adv7511_init_cec_regmap(adv7511);
1337 	if (ret)
1338 		goto err_i2c_unregister_packet;
1339 
1340 	INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
1341 
1342 	adv7511_power_off(adv7511);
1343 
1344 	i2c_set_clientdata(i2c, adv7511);
1345 
1346 	if (adv7511->info->link_config)
1347 		adv7511_set_link_config(adv7511, &link_config);
1348 
1349 	regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
1350 		     ADV7511_CEC_CTRL_POWER_DOWN);
1351 
1352 	adv7511->bridge.ops = DRM_BRIDGE_OP_DETECT |
1353 		DRM_BRIDGE_OP_EDID |
1354 		DRM_BRIDGE_OP_HDMI |
1355 		DRM_BRIDGE_OP_HDMI_SPD_INFOFRAME;
1356 	if (adv7511->i2c_main->irq)
1357 		adv7511->bridge.ops |= DRM_BRIDGE_OP_HPD;
1358 
1359 	adv7511->bridge.vendor = "Analog";
1360 	adv7511->bridge.product = adv7511->info->name;
1361 
1362 #ifdef CONFIG_DRM_I2C_ADV7511_AUDIO
1363 	adv7511->bridge.ops |= DRM_BRIDGE_OP_HDMI_AUDIO;
1364 	adv7511->bridge.hdmi_audio_dev = dev;
1365 	adv7511->bridge.hdmi_audio_max_i2s_playback_channels = 2;
1366 	adv7511->bridge.hdmi_audio_i2s_formats = (SNDRV_PCM_FMTBIT_S16_LE |
1367 						  SNDRV_PCM_FMTBIT_S20_3LE |
1368 						  SNDRV_PCM_FMTBIT_S24_3LE |
1369 						  SNDRV_PCM_FMTBIT_S24_LE |
1370 						  SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE),
1371 	adv7511->bridge.hdmi_audio_spdif_playback = 1;
1372 	adv7511->bridge.hdmi_audio_dai_port = 2;
1373 #endif
1374 
1375 #ifdef CONFIG_DRM_I2C_ADV7511_CEC
1376 	adv7511->bridge.ops |= DRM_BRIDGE_OP_HDMI_CEC_ADAPTER;
1377 	adv7511->bridge.hdmi_cec_dev = dev;
1378 	adv7511->bridge.hdmi_cec_adapter_name = dev_name(dev);
1379 	adv7511->bridge.hdmi_cec_available_las = ADV7511_MAX_ADDRS;
1380 #endif
1381 
1382 	adv7511->bridge.of_node = dev->of_node;
1383 	adv7511->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
1384 
1385 	drm_bridge_add(&adv7511->bridge);
1386 
1387 	if (i2c->irq) {
1388 		init_waitqueue_head(&adv7511->wq);
1389 
1390 		ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
1391 						adv7511_irq_handler,
1392 						IRQF_ONESHOT | IRQF_SHARED,
1393 						dev_name(dev),
1394 						adv7511);
1395 		if (ret)
1396 			goto err_unregister_audio;
1397 	}
1398 
1399 	if (adv7511->info->has_dsi) {
1400 		ret = adv7533_attach_dsi(adv7511);
1401 		if (ret)
1402 			goto err_unregister_audio;
1403 	}
1404 
1405 	return 0;
1406 
1407 err_unregister_audio:
1408 	drm_bridge_remove(&adv7511->bridge);
1409 	i2c_unregister_device(adv7511->i2c_cec);
1410 	clk_disable_unprepare(adv7511->cec_clk);
1411 err_i2c_unregister_packet:
1412 	i2c_unregister_device(adv7511->i2c_packet);
1413 err_i2c_unregister_edid:
1414 	i2c_unregister_device(adv7511->i2c_edid);
1415 uninit_regulators:
1416 	adv7511_uninit_regulators(adv7511);
1417 err_of_node_put:
1418 	of_node_put(adv7511->host_node);
1419 
1420 	return ret;
1421 }
1422 
1423 static void adv7511_remove(struct i2c_client *i2c)
1424 {
1425 	struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1426 
1427 	of_node_put(adv7511->host_node);
1428 
1429 	adv7511_uninit_regulators(adv7511);
1430 
1431 	drm_bridge_remove(&adv7511->bridge);
1432 
1433 	i2c_unregister_device(adv7511->i2c_cec);
1434 	clk_disable_unprepare(adv7511->cec_clk);
1435 
1436 	i2c_unregister_device(adv7511->i2c_packet);
1437 	i2c_unregister_device(adv7511->i2c_edid);
1438 }
1439 
1440 static const struct adv7511_chip_info adv7511_chip_info = {
1441 	.type = ADV7511,
1442 	.name = "ADV7511",
1443 	.max_mode_clock_khz = 165000,
1444 	.supply_names = adv7511_supply_names,
1445 	.num_supplies = ARRAY_SIZE(adv7511_supply_names),
1446 	.link_config = true,
1447 };
1448 
1449 static const struct adv7511_chip_info adv7533_chip_info = {
1450 	.type = ADV7533,
1451 	.name = "ADV7533",
1452 	.max_mode_clock_khz = 80000,
1453 	.max_lane_freq_khz = 800000,
1454 	.supply_names = adv7533_supply_names,
1455 	.num_supplies = ARRAY_SIZE(adv7533_supply_names),
1456 	.reg_cec_offset = ADV7533_REG_CEC_OFFSET,
1457 	.has_dsi = true,
1458 };
1459 
1460 static const struct adv7511_chip_info adv7535_chip_info = {
1461 	.type = ADV7535,
1462 	.name = "ADV7535",
1463 	.max_mode_clock_khz = 148500,
1464 	.max_lane_freq_khz = 891000,
1465 	.supply_names = adv7533_supply_names,
1466 	.num_supplies = ARRAY_SIZE(adv7533_supply_names),
1467 	.reg_cec_offset = ADV7533_REG_CEC_OFFSET,
1468 	.has_dsi = true,
1469 	.hpd_override_enable = true,
1470 };
1471 
1472 static const struct i2c_device_id adv7511_i2c_ids[] = {
1473 	{ "adv7511", (kernel_ulong_t)&adv7511_chip_info },
1474 	{ "adv7511w", (kernel_ulong_t)&adv7511_chip_info },
1475 	{ "adv7513", (kernel_ulong_t)&adv7511_chip_info },
1476 	{ "adv7533", (kernel_ulong_t)&adv7533_chip_info },
1477 	{ "adv7535", (kernel_ulong_t)&adv7535_chip_info },
1478 	{ }
1479 };
1480 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1481 
1482 static const struct of_device_id adv7511_of_ids[] = {
1483 	{ .compatible = "adi,adv7511", .data = &adv7511_chip_info },
1484 	{ .compatible = "adi,adv7511w", .data = &adv7511_chip_info },
1485 	{ .compatible = "adi,adv7513", .data = &adv7511_chip_info },
1486 	{ .compatible = "adi,adv7533", .data = &adv7533_chip_info },
1487 	{ .compatible = "adi,adv7535", .data = &adv7535_chip_info },
1488 	{ }
1489 };
1490 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1491 
1492 static struct mipi_dsi_driver adv7533_dsi_driver = {
1493 	.driver.name = "adv7533",
1494 };
1495 
1496 static struct i2c_driver adv7511_driver = {
1497 	.driver = {
1498 		.name = "adv7511",
1499 		.of_match_table = adv7511_of_ids,
1500 	},
1501 	.id_table = adv7511_i2c_ids,
1502 	.probe = adv7511_probe,
1503 	.remove = adv7511_remove,
1504 };
1505 
1506 static int __init adv7511_init(void)
1507 {
1508 	int ret;
1509 
1510 	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) {
1511 		ret = mipi_dsi_driver_register(&adv7533_dsi_driver);
1512 		if (ret)
1513 			return ret;
1514 	}
1515 
1516 	ret = i2c_add_driver(&adv7511_driver);
1517 	if (ret) {
1518 		if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1519 			mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1520 	}
1521 
1522 	return ret;
1523 }
1524 module_init(adv7511_init);
1525 
1526 static void __exit adv7511_exit(void)
1527 {
1528 	i2c_del_driver(&adv7511_driver);
1529 
1530 	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1531 		mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1532 }
1533 module_exit(adv7511_exit);
1534 
1535 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1536 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1537 MODULE_LICENSE("GPL");
1538