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