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