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