1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4  * Copyright (c) 2019-2020. Linaro Limited.
5  */
6 
7 #include <linux/gpio/consumer.h>
8 #include <linux/i2c.h>
9 #include <linux/interrupt.h>
10 #include <linux/media-bus-format.h>
11 #include <linux/module.h>
12 #include <linux/of_graph.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/consumer.h>
16 
17 #include <sound/hdmi-codec.h>
18 
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_bridge.h>
21 #include <drm/drm_mipi_dsi.h>
22 #include <drm/drm_print.h>
23 #include <drm/drm_probe_helper.h>
24 
25 #define EDID_SEG_SIZE	256
26 #define EDID_LEN	32
27 #define EDID_LOOP	8
28 #define KEY_DDC_ACCS_DONE 0x02
29 #define DDC_NO_ACK	0x50
30 
31 #define LT9611_4LANES	0
32 
33 struct lt9611 {
34 	struct device *dev;
35 	struct drm_bridge bridge;
36 	struct drm_connector connector;
37 
38 	struct regmap *regmap;
39 
40 	struct device_node *dsi0_node;
41 	struct device_node *dsi1_node;
42 	struct mipi_dsi_device *dsi0;
43 	struct mipi_dsi_device *dsi1;
44 	struct platform_device *audio_pdev;
45 
46 	bool ac_mode;
47 
48 	struct gpio_desc *reset_gpio;
49 	struct gpio_desc *enable_gpio;
50 
51 	bool power_on;
52 	bool sleep;
53 
54 	struct regulator_bulk_data supplies[2];
55 
56 	struct i2c_client *client;
57 
58 	enum drm_connector_status status;
59 
60 	u8 edid_buf[EDID_SEG_SIZE];
61 	u32 vic;
62 };
63 
64 #define LT9611_PAGE_CONTROL	0xff
65 
66 static const struct regmap_range_cfg lt9611_ranges[] = {
67 	{
68 		.name = "register_range",
69 		.range_min =  0,
70 		.range_max = 0x85ff,
71 		.selector_reg = LT9611_PAGE_CONTROL,
72 		.selector_mask = 0xff,
73 		.selector_shift = 0,
74 		.window_start = 0,
75 		.window_len = 0x100,
76 	},
77 };
78 
79 static const struct regmap_config lt9611_regmap_config = {
80 	.reg_bits = 8,
81 	.val_bits = 8,
82 	.max_register = 0xffff,
83 	.ranges = lt9611_ranges,
84 	.num_ranges = ARRAY_SIZE(lt9611_ranges),
85 };
86 
87 struct lt9611_mode {
88 	u16 hdisplay;
89 	u16 vdisplay;
90 	u8 vrefresh;
91 	u8 lanes;
92 	u8 intfs;
93 };
94 
95 static struct lt9611_mode lt9611_modes[] = {
96 	{ 3840, 2160, 30, 4, 2 }, /* 3840x2160 24bit 30Hz 4Lane 2ports */
97 	{ 1920, 1080, 60, 4, 1 }, /* 1080P 24bit 60Hz 4lane 1port */
98 	{ 1920, 1080, 30, 3, 1 }, /* 1080P 24bit 30Hz 3lane 1port */
99 	{ 1920, 1080, 24, 3, 1 },
100 	{ 720, 480, 60, 4, 1 },
101 	{ 720, 576, 50, 2, 1 },
102 	{ 640, 480, 60, 2, 1 },
103 };
104 
105 static struct lt9611 *bridge_to_lt9611(struct drm_bridge *bridge)
106 {
107 	return container_of(bridge, struct lt9611, bridge);
108 }
109 
110 static struct lt9611 *connector_to_lt9611(struct drm_connector *connector)
111 {
112 	return container_of(connector, struct lt9611, connector);
113 }
114 
115 static int lt9611_mipi_input_analog(struct lt9611 *lt9611)
116 {
117 	const struct reg_sequence reg_cfg[] = {
118 		{ 0x8106, 0x40 }, /* port A rx current */
119 		{ 0x810a, 0xfe }, /* port A ldo voltage set */
120 		{ 0x810b, 0xbf }, /* enable port A lprx */
121 		{ 0x8111, 0x40 }, /* port B rx current */
122 		{ 0x8115, 0xfe }, /* port B ldo voltage set */
123 		{ 0x8116, 0xbf }, /* enable port B lprx */
124 
125 		{ 0x811c, 0x03 }, /* PortA clk lane no-LP mode */
126 		{ 0x8120, 0x03 }, /* PortB clk lane with-LP mode */
127 	};
128 
129 	return regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
130 }
131 
132 static int lt9611_mipi_input_digital(struct lt9611 *lt9611,
133 				     const struct drm_display_mode *mode)
134 {
135 	struct reg_sequence reg_cfg[] = {
136 		{ 0x8300, LT9611_4LANES },
137 		{ 0x830a, 0x00 },
138 		{ 0x824f, 0x80 },
139 		{ 0x8250, 0x10 },
140 		{ 0x8302, 0x0a },
141 		{ 0x8306, 0x0a },
142 	};
143 
144 	if (mode->hdisplay == 3840)
145 		reg_cfg[1].def = 0x03;
146 
147 	return regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
148 }
149 
150 static void lt9611_mipi_video_setup(struct lt9611 *lt9611,
151 				    const struct drm_display_mode *mode)
152 {
153 	u32 h_total, hactive, hsync_len, hfront_porch, hsync_porch;
154 	u32 v_total, vactive, vsync_len, vfront_porch, vsync_porch;
155 
156 	h_total = mode->htotal;
157 	v_total = mode->vtotal;
158 
159 	hactive = mode->hdisplay;
160 	hsync_len = mode->hsync_end - mode->hsync_start;
161 	hfront_porch = mode->hsync_start - mode->hdisplay;
162 	hsync_porch = hsync_len + mode->htotal - mode->hsync_end;
163 
164 	vactive = mode->vdisplay;
165 	vsync_len = mode->vsync_end - mode->vsync_start;
166 	vfront_porch = mode->vsync_start - mode->vdisplay;
167 	vsync_porch = vsync_len + mode->vtotal - mode->vsync_end;
168 
169 	regmap_write(lt9611->regmap, 0x830d, (u8)(v_total / 256));
170 	regmap_write(lt9611->regmap, 0x830e, (u8)(v_total % 256));
171 
172 	regmap_write(lt9611->regmap, 0x830f, (u8)(vactive / 256));
173 	regmap_write(lt9611->regmap, 0x8310, (u8)(vactive % 256));
174 
175 	regmap_write(lt9611->regmap, 0x8311, (u8)(h_total / 256));
176 	regmap_write(lt9611->regmap, 0x8312, (u8)(h_total % 256));
177 
178 	regmap_write(lt9611->regmap, 0x8313, (u8)(hactive / 256));
179 	regmap_write(lt9611->regmap, 0x8314, (u8)(hactive % 256));
180 
181 	regmap_write(lt9611->regmap, 0x8315, (u8)(vsync_len % 256));
182 	regmap_write(lt9611->regmap, 0x8316, (u8)(hsync_len % 256));
183 
184 	regmap_write(lt9611->regmap, 0x8317, (u8)(vfront_porch % 256));
185 
186 	regmap_write(lt9611->regmap, 0x8318, (u8)(vsync_porch % 256));
187 
188 	regmap_write(lt9611->regmap, 0x8319, (u8)(hfront_porch % 256));
189 
190 	regmap_write(lt9611->regmap, 0x831a, (u8)(hsync_porch / 256));
191 	regmap_write(lt9611->regmap, 0x831b, (u8)(hsync_porch % 256));
192 }
193 
194 static void lt9611_pcr_setup(struct lt9611 *lt9611, const struct drm_display_mode *mode)
195 {
196 	const struct reg_sequence reg_cfg[] = {
197 		{ 0x830b, 0x01 },
198 		{ 0x830c, 0x10 },
199 		{ 0x8348, 0x00 },
200 		{ 0x8349, 0x81 },
201 
202 		/* stage 1 */
203 		{ 0x8321, 0x4a },
204 		{ 0x8324, 0x71 },
205 		{ 0x8325, 0x30 },
206 		{ 0x832a, 0x01 },
207 
208 		/* stage 2 */
209 		{ 0x834a, 0x40 },
210 		{ 0x831d, 0x10 },
211 
212 		/* MK limit */
213 		{ 0x832d, 0x38 },
214 		{ 0x8331, 0x08 },
215 	};
216 	const struct reg_sequence reg_cfg2[] = {
217 		{ 0x830b, 0x03 },
218 		{ 0x830c, 0xd0 },
219 		{ 0x8348, 0x03 },
220 		{ 0x8349, 0xe0 },
221 		{ 0x8324, 0x72 },
222 		{ 0x8325, 0x00 },
223 		{ 0x832a, 0x01 },
224 		{ 0x834a, 0x10 },
225 		{ 0x831d, 0x10 },
226 		{ 0x8326, 0x37 },
227 	};
228 
229 	regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
230 
231 	switch (mode->hdisplay) {
232 	case 640:
233 		regmap_write(lt9611->regmap, 0x8326, 0x14);
234 		break;
235 	case 1920:
236 		regmap_write(lt9611->regmap, 0x8326, 0x37);
237 		break;
238 	case 3840:
239 		regmap_multi_reg_write(lt9611->regmap, reg_cfg2, ARRAY_SIZE(reg_cfg2));
240 		break;
241 	}
242 
243 	/* pcr rst */
244 	regmap_write(lt9611->regmap, 0x8011, 0x5a);
245 	regmap_write(lt9611->regmap, 0x8011, 0xfa);
246 }
247 
248 static int lt9611_pll_setup(struct lt9611 *lt9611, const struct drm_display_mode *mode)
249 {
250 	unsigned int pclk = mode->clock;
251 	const struct reg_sequence reg_cfg[] = {
252 		/* txpll init */
253 		{ 0x8123, 0x40 },
254 		{ 0x8124, 0x64 },
255 		{ 0x8125, 0x80 },
256 		{ 0x8126, 0x55 },
257 		{ 0x812c, 0x37 },
258 		{ 0x812f, 0x01 },
259 		{ 0x8126, 0x55 },
260 		{ 0x8127, 0x66 },
261 		{ 0x8128, 0x88 },
262 	};
263 
264 	regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
265 
266 	if (pclk > 150000)
267 		regmap_write(lt9611->regmap, 0x812d, 0x88);
268 	else if (pclk > 70000)
269 		regmap_write(lt9611->regmap, 0x812d, 0x99);
270 	else
271 		regmap_write(lt9611->regmap, 0x812d, 0xaa);
272 
273 	/*
274 	 * first divide pclk by 2 first
275 	 *  - write divide by 64k to 19:16 bits which means shift by 17
276 	 *  - write divide by 256 to 15:8 bits which means shift by 9
277 	 *  - write remainder to 7:0 bits, which means shift by 1
278 	 */
279 	regmap_write(lt9611->regmap, 0x82e3, pclk >> 17); /* pclk[19:16] */
280 	regmap_write(lt9611->regmap, 0x82e4, pclk >> 9);  /* pclk[15:8]  */
281 	regmap_write(lt9611->regmap, 0x82e5, pclk >> 1);  /* pclk[7:0]   */
282 
283 	regmap_write(lt9611->regmap, 0x82de, 0x20);
284 	regmap_write(lt9611->regmap, 0x82de, 0xe0);
285 
286 	regmap_write(lt9611->regmap, 0x8016, 0xf1);
287 	regmap_write(lt9611->regmap, 0x8016, 0xf3);
288 
289 	return 0;
290 }
291 
292 static int lt9611_read_video_check(struct lt9611 *lt9611, unsigned int reg)
293 {
294 	unsigned int temp, temp2;
295 	int ret;
296 
297 	ret = regmap_read(lt9611->regmap, reg, &temp);
298 	if (ret)
299 		return ret;
300 	temp <<= 8;
301 	ret = regmap_read(lt9611->regmap, reg + 1, &temp2);
302 	if (ret)
303 		return ret;
304 
305 	return (temp + temp2);
306 }
307 
308 static int lt9611_video_check(struct lt9611 *lt9611)
309 {
310 	u32 v_total, vactive, hactive_a, hactive_b, h_total_sysclk;
311 	int temp;
312 
313 	/* top module video check */
314 
315 	/* vactive */
316 	temp = lt9611_read_video_check(lt9611, 0x8282);
317 	if (temp < 0)
318 		goto end;
319 	vactive = temp;
320 
321 	/* v_total */
322 	temp = lt9611_read_video_check(lt9611, 0x826c);
323 	if (temp < 0)
324 		goto end;
325 	v_total = temp;
326 
327 	/* h_total_sysclk */
328 	temp = lt9611_read_video_check(lt9611, 0x8286);
329 	if (temp < 0)
330 		goto end;
331 	h_total_sysclk = temp;
332 
333 	/* hactive_a */
334 	temp = lt9611_read_video_check(lt9611, 0x8382);
335 	if (temp < 0)
336 		goto end;
337 	hactive_a = temp / 3;
338 
339 	/* hactive_b */
340 	temp = lt9611_read_video_check(lt9611, 0x8386);
341 	if (temp < 0)
342 		goto end;
343 	hactive_b = temp / 3;
344 
345 	dev_info(lt9611->dev,
346 		 "video check: hactive_a=%d, hactive_b=%d, vactive=%d, v_total=%d, h_total_sysclk=%d\n",
347 		 hactive_a, hactive_b, vactive, v_total, h_total_sysclk);
348 
349 	return 0;
350 
351 end:
352 	dev_err(lt9611->dev, "read video check error\n");
353 	return temp;
354 }
355 
356 static void lt9611_hdmi_tx_digital(struct lt9611 *lt9611)
357 {
358 	regmap_write(lt9611->regmap, 0x8443, 0x46 - lt9611->vic);
359 	regmap_write(lt9611->regmap, 0x8447, lt9611->vic);
360 	regmap_write(lt9611->regmap, 0x843d, 0x0a); /* UD1 infoframe */
361 
362 	regmap_write(lt9611->regmap, 0x82d6, 0x8c);
363 	regmap_write(lt9611->regmap, 0x82d7, 0x04);
364 }
365 
366 static void lt9611_hdmi_tx_phy(struct lt9611 *lt9611)
367 {
368 	struct reg_sequence reg_cfg[] = {
369 		{ 0x8130, 0x6a },
370 		{ 0x8131, 0x44 }, /* HDMI DC mode */
371 		{ 0x8132, 0x4a },
372 		{ 0x8133, 0x0b },
373 		{ 0x8134, 0x00 },
374 		{ 0x8135, 0x00 },
375 		{ 0x8136, 0x00 },
376 		{ 0x8137, 0x44 },
377 		{ 0x813f, 0x0f },
378 		{ 0x8140, 0xa0 },
379 		{ 0x8141, 0xa0 },
380 		{ 0x8142, 0xa0 },
381 		{ 0x8143, 0xa0 },
382 		{ 0x8144, 0x0a },
383 	};
384 
385 	/* HDMI AC mode */
386 	if (lt9611->ac_mode)
387 		reg_cfg[2].def = 0x73;
388 
389 	regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
390 }
391 
392 static irqreturn_t lt9611_irq_thread_handler(int irq, void *dev_id)
393 {
394 	struct lt9611 *lt9611 = dev_id;
395 	unsigned int irq_flag0 = 0;
396 	unsigned int irq_flag3 = 0;
397 
398 	regmap_read(lt9611->regmap, 0x820f, &irq_flag3);
399 	regmap_read(lt9611->regmap, 0x820c, &irq_flag0);
400 
401 	/* hpd changed low */
402 	if (irq_flag3 & 0x80) {
403 		dev_info(lt9611->dev, "hdmi cable disconnected\n");
404 
405 		regmap_write(lt9611->regmap, 0x8207, 0xbf);
406 		regmap_write(lt9611->regmap, 0x8207, 0x3f);
407 	}
408 
409 	/* hpd changed high */
410 	if (irq_flag3 & 0x40) {
411 		dev_info(lt9611->dev, "hdmi cable connected\n");
412 
413 		regmap_write(lt9611->regmap, 0x8207, 0x7f);
414 		regmap_write(lt9611->regmap, 0x8207, 0x3f);
415 	}
416 
417 	if (irq_flag3 & 0xc0 && lt9611->bridge.dev)
418 		drm_kms_helper_hotplug_event(lt9611->bridge.dev);
419 
420 	/* video input changed */
421 	if (irq_flag0 & 0x01) {
422 		dev_info(lt9611->dev, "video input changed\n");
423 		regmap_write(lt9611->regmap, 0x829e, 0xff);
424 		regmap_write(lt9611->regmap, 0x829e, 0xf7);
425 		regmap_write(lt9611->regmap, 0x8204, 0xff);
426 		regmap_write(lt9611->regmap, 0x8204, 0xfe);
427 	}
428 
429 	return IRQ_HANDLED;
430 }
431 
432 static void lt9611_enable_hpd_interrupts(struct lt9611 *lt9611)
433 {
434 	unsigned int val;
435 
436 	regmap_read(lt9611->regmap, 0x8203, &val);
437 
438 	val &= ~0xc0;
439 	regmap_write(lt9611->regmap, 0x8203, val);
440 	regmap_write(lt9611->regmap, 0x8207, 0xff); /* clear */
441 	regmap_write(lt9611->regmap, 0x8207, 0x3f);
442 }
443 
444 static void lt9611_sleep_setup(struct lt9611 *lt9611)
445 {
446 	const struct reg_sequence sleep_setup[] = {
447 		{ 0x8024, 0x76 },
448 		{ 0x8023, 0x01 },
449 		{ 0x8157, 0x03 }, /* set addr pin as output */
450 		{ 0x8149, 0x0b },
451 		{ 0x8151, 0x30 }, /* disable IRQ */
452 		{ 0x8102, 0x48 }, /* MIPI Rx power down */
453 		{ 0x8123, 0x80 },
454 		{ 0x8130, 0x00 },
455 		{ 0x8100, 0x01 }, /* bandgap power down */
456 		{ 0x8101, 0x00 }, /* system clk power down */
457 	};
458 
459 	regmap_multi_reg_write(lt9611->regmap,
460 			       sleep_setup, ARRAY_SIZE(sleep_setup));
461 	lt9611->sleep = true;
462 }
463 
464 static int lt9611_power_on(struct lt9611 *lt9611)
465 {
466 	int ret;
467 	const struct reg_sequence seq[] = {
468 		/* LT9611_System_Init */
469 		{ 0x8101, 0x18 }, /* sel xtal clock */
470 
471 		/* timer for frequency meter */
472 		{ 0x821b, 0x69 }, /* timer 2 */
473 		{ 0x821c, 0x78 },
474 		{ 0x82cb, 0x69 }, /* timer 1 */
475 		{ 0x82cc, 0x78 },
476 
477 		/* irq init */
478 		{ 0x8251, 0x01 },
479 		{ 0x8258, 0x0a }, /* hpd irq */
480 		{ 0x8259, 0x80 }, /* hpd debounce width */
481 		{ 0x829e, 0xf7 }, /* video check irq */
482 
483 		/* power consumption for work */
484 		{ 0x8004, 0xf0 },
485 		{ 0x8006, 0xf0 },
486 		{ 0x800a, 0x80 },
487 		{ 0x800b, 0x40 },
488 		{ 0x800d, 0xef },
489 		{ 0x8011, 0xfa },
490 	};
491 
492 	if (lt9611->power_on)
493 		return 0;
494 
495 	ret = regmap_multi_reg_write(lt9611->regmap, seq, ARRAY_SIZE(seq));
496 	if (!ret)
497 		lt9611->power_on = true;
498 
499 	return ret;
500 }
501 
502 static int lt9611_power_off(struct lt9611 *lt9611)
503 {
504 	int ret;
505 
506 	ret = regmap_write(lt9611->regmap, 0x8130, 0x6a);
507 	if (!ret)
508 		lt9611->power_on = false;
509 
510 	return ret;
511 }
512 
513 static void lt9611_reset(struct lt9611 *lt9611)
514 {
515 	gpiod_set_value_cansleep(lt9611->reset_gpio, 1);
516 	msleep(20);
517 
518 	gpiod_set_value_cansleep(lt9611->reset_gpio, 0);
519 	msleep(20);
520 
521 	gpiod_set_value_cansleep(lt9611->reset_gpio, 1);
522 	msleep(100);
523 }
524 
525 static void lt9611_assert_5v(struct lt9611 *lt9611)
526 {
527 	if (!lt9611->enable_gpio)
528 		return;
529 
530 	gpiod_set_value_cansleep(lt9611->enable_gpio, 1);
531 	msleep(20);
532 }
533 
534 static int lt9611_regulator_init(struct lt9611 *lt9611)
535 {
536 	int ret;
537 
538 	lt9611->supplies[0].supply = "vdd";
539 	lt9611->supplies[1].supply = "vcc";
540 
541 	ret = devm_regulator_bulk_get(lt9611->dev, 2, lt9611->supplies);
542 	if (ret < 0)
543 		return ret;
544 
545 	return regulator_set_load(lt9611->supplies[0].consumer, 300000);
546 }
547 
548 static int lt9611_regulator_enable(struct lt9611 *lt9611)
549 {
550 	int ret;
551 
552 	ret = regulator_enable(lt9611->supplies[0].consumer);
553 	if (ret < 0)
554 		return ret;
555 
556 	usleep_range(1000, 10000);
557 
558 	ret = regulator_enable(lt9611->supplies[1].consumer);
559 	if (ret < 0) {
560 		regulator_disable(lt9611->supplies[0].consumer);
561 		return ret;
562 	}
563 
564 	return 0;
565 }
566 
567 static struct lt9611_mode *lt9611_find_mode(const struct drm_display_mode *mode)
568 {
569 	int i;
570 
571 	for (i = 0; i < ARRAY_SIZE(lt9611_modes); i++) {
572 		if (lt9611_modes[i].hdisplay == mode->hdisplay &&
573 		    lt9611_modes[i].vdisplay == mode->vdisplay &&
574 		    lt9611_modes[i].vrefresh == drm_mode_vrefresh(mode)) {
575 			return &lt9611_modes[i];
576 		}
577 	}
578 
579 	return NULL;
580 }
581 
582 /* connector funcs */
583 static enum drm_connector_status __lt9611_detect(struct lt9611 *lt9611)
584 {
585 	unsigned int reg_val = 0;
586 	int connected = 0;
587 
588 	regmap_read(lt9611->regmap, 0x825e, &reg_val);
589 	connected  = (reg_val & (BIT(2) | BIT(0)));
590 
591 	lt9611->status = connected ?  connector_status_connected :
592 				connector_status_disconnected;
593 
594 	return lt9611->status;
595 }
596 
597 static enum drm_connector_status
598 lt9611_connector_detect(struct drm_connector *connector, bool force)
599 {
600 	return __lt9611_detect(connector_to_lt9611(connector));
601 }
602 
603 static int lt9611_read_edid(struct lt9611 *lt9611)
604 {
605 	unsigned int temp;
606 	int ret = 0;
607 	int i, j;
608 
609 	/* memset to clear old buffer, if any */
610 	memset(lt9611->edid_buf, 0, sizeof(lt9611->edid_buf));
611 
612 	regmap_write(lt9611->regmap, 0x8503, 0xc9);
613 
614 	/* 0xA0 is EDID device address */
615 	regmap_write(lt9611->regmap, 0x8504, 0xa0);
616 	/* 0x00 is EDID offset address */
617 	regmap_write(lt9611->regmap, 0x8505, 0x00);
618 
619 	/* length for read */
620 	regmap_write(lt9611->regmap, 0x8506, EDID_LEN);
621 	regmap_write(lt9611->regmap, 0x8514, 0x7f);
622 
623 	for (i = 0; i < EDID_LOOP; i++) {
624 		/* offset address */
625 		regmap_write(lt9611->regmap, 0x8505, i * EDID_LEN);
626 		regmap_write(lt9611->regmap, 0x8507, 0x36);
627 		regmap_write(lt9611->regmap, 0x8507, 0x31);
628 		regmap_write(lt9611->regmap, 0x8507, 0x37);
629 		usleep_range(5000, 10000);
630 
631 		regmap_read(lt9611->regmap, 0x8540, &temp);
632 
633 		if (temp & KEY_DDC_ACCS_DONE) {
634 			for (j = 0; j < EDID_LEN; j++) {
635 				regmap_read(lt9611->regmap, 0x8583, &temp);
636 				lt9611->edid_buf[i * EDID_LEN + j] = temp;
637 			}
638 
639 		} else if (temp & DDC_NO_ACK) { /* DDC No Ack or Abitration lost */
640 			dev_err(lt9611->dev, "read edid failed: no ack\n");
641 			ret = -EIO;
642 			goto end;
643 
644 		} else {
645 			dev_err(lt9611->dev, "read edid failed: access not done\n");
646 			ret = -EIO;
647 			goto end;
648 		}
649 	}
650 
651 end:
652 	regmap_write(lt9611->regmap, 0x8507, 0x1f);
653 	return ret;
654 }
655 
656 static int
657 lt9611_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
658 {
659 	struct lt9611 *lt9611 = data;
660 	int ret;
661 
662 	if (len > 128)
663 		return -EINVAL;
664 
665 	/* supports up to 1 extension block */
666 	/* TODO: add support for more extension blocks */
667 	if (block > 1)
668 		return -EINVAL;
669 
670 	if (block == 0) {
671 		ret = lt9611_read_edid(lt9611);
672 		if (ret) {
673 			dev_err(lt9611->dev, "edid read failed\n");
674 			return ret;
675 		}
676 	}
677 
678 	block %= 2;
679 	memcpy(buf, lt9611->edid_buf + (block * 128), len);
680 
681 	return 0;
682 }
683 
684 static int lt9611_connector_get_modes(struct drm_connector *connector)
685 {
686 	struct lt9611 *lt9611 = connector_to_lt9611(connector);
687 	unsigned int count;
688 	struct edid *edid;
689 
690 	lt9611_power_on(lt9611);
691 	edid = drm_do_get_edid(connector, lt9611_get_edid_block, lt9611);
692 	drm_connector_update_edid_property(connector, edid);
693 	count = drm_add_edid_modes(connector, edid);
694 	kfree(edid);
695 
696 	return count;
697 }
698 
699 static enum drm_mode_status
700 lt9611_connector_mode_valid(struct drm_connector *connector,
701 			    struct drm_display_mode *mode)
702 {
703 	struct lt9611_mode *lt9611_mode = lt9611_find_mode(mode);
704 
705 	return lt9611_mode ? MODE_OK : MODE_BAD;
706 }
707 
708 /* bridge funcs */
709 static void
710 lt9611_bridge_atomic_enable(struct drm_bridge *bridge,
711 			    struct drm_bridge_state *old_bridge_state)
712 {
713 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
714 
715 	if (lt9611_power_on(lt9611)) {
716 		dev_err(lt9611->dev, "power on failed\n");
717 		return;
718 	}
719 
720 	lt9611_mipi_input_analog(lt9611);
721 	lt9611_hdmi_tx_digital(lt9611);
722 	lt9611_hdmi_tx_phy(lt9611);
723 
724 	msleep(500);
725 
726 	lt9611_video_check(lt9611);
727 
728 	/* Enable HDMI output */
729 	regmap_write(lt9611->regmap, 0x8130, 0xea);
730 }
731 
732 static void
733 lt9611_bridge_atomic_disable(struct drm_bridge *bridge,
734 			     struct drm_bridge_state *old_bridge_state)
735 {
736 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
737 	int ret;
738 
739 	/* Disable HDMI output */
740 	ret = regmap_write(lt9611->regmap, 0x8130, 0x6a);
741 	if (ret) {
742 		dev_err(lt9611->dev, "video on failed\n");
743 		return;
744 	}
745 
746 	if (lt9611_power_off(lt9611)) {
747 		dev_err(lt9611->dev, "power on failed\n");
748 		return;
749 	}
750 }
751 
752 static struct
753 drm_connector_helper_funcs lt9611_bridge_connector_helper_funcs = {
754 	.get_modes = lt9611_connector_get_modes,
755 	.mode_valid = lt9611_connector_mode_valid,
756 };
757 
758 static const struct drm_connector_funcs lt9611_bridge_connector_funcs = {
759 	.fill_modes = drm_helper_probe_single_connector_modes,
760 	.detect = lt9611_connector_detect,
761 	.destroy = drm_connector_cleanup,
762 	.reset = drm_atomic_helper_connector_reset,
763 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
764 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
765 };
766 
767 static struct mipi_dsi_device *lt9611_attach_dsi(struct lt9611 *lt9611,
768 						 struct device_node *dsi_node)
769 {
770 	const struct mipi_dsi_device_info info = { "lt9611", 0, NULL };
771 	struct mipi_dsi_device *dsi;
772 	struct mipi_dsi_host *host;
773 	struct device *dev = lt9611->dev;
774 	int ret;
775 
776 	host = of_find_mipi_dsi_host_by_node(dsi_node);
777 	if (!host) {
778 		dev_err(lt9611->dev, "failed to find dsi host\n");
779 		return ERR_PTR(-EPROBE_DEFER);
780 	}
781 
782 	dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
783 	if (IS_ERR(dsi)) {
784 		dev_err(lt9611->dev, "failed to create dsi device\n");
785 		return dsi;
786 	}
787 
788 	dsi->lanes = 4;
789 	dsi->format = MIPI_DSI_FMT_RGB888;
790 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
791 			  MIPI_DSI_MODE_VIDEO_HSE;
792 
793 	ret = devm_mipi_dsi_attach(dev, dsi);
794 	if (ret < 0) {
795 		dev_err(dev, "failed to attach dsi to host\n");
796 		return ERR_PTR(ret);
797 	}
798 
799 	return dsi;
800 }
801 
802 static int lt9611_connector_init(struct drm_bridge *bridge, struct lt9611 *lt9611)
803 {
804 	int ret;
805 
806 	ret = drm_connector_init(bridge->dev, &lt9611->connector,
807 				 &lt9611_bridge_connector_funcs,
808 				 DRM_MODE_CONNECTOR_HDMIA);
809 	if (ret) {
810 		DRM_ERROR("Failed to initialize connector with drm\n");
811 		return ret;
812 	}
813 
814 	drm_connector_helper_add(&lt9611->connector,
815 				 &lt9611_bridge_connector_helper_funcs);
816 
817 	if (!bridge->encoder) {
818 		DRM_ERROR("Parent encoder object not found");
819 		return -ENODEV;
820 	}
821 
822 	drm_connector_attach_encoder(&lt9611->connector, bridge->encoder);
823 
824 	return 0;
825 }
826 
827 static int lt9611_bridge_attach(struct drm_bridge *bridge,
828 				enum drm_bridge_attach_flags flags)
829 {
830 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
831 	int ret;
832 
833 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
834 		ret = lt9611_connector_init(bridge, lt9611);
835 		if (ret < 0)
836 			return ret;
837 	}
838 
839 	return 0;
840 }
841 
842 static enum drm_mode_status lt9611_bridge_mode_valid(struct drm_bridge *bridge,
843 						     const struct drm_display_info *info,
844 						     const struct drm_display_mode *mode)
845 {
846 	struct lt9611_mode *lt9611_mode = lt9611_find_mode(mode);
847 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
848 
849 	if (!lt9611_mode)
850 		return MODE_BAD;
851 	else if (lt9611_mode->intfs > 1 && !lt9611->dsi1)
852 		return MODE_PANEL;
853 	else
854 		return MODE_OK;
855 }
856 
857 static void lt9611_bridge_pre_enable(struct drm_bridge *bridge)
858 {
859 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
860 
861 	if (!lt9611->sleep)
862 		return;
863 
864 	lt9611_reset(lt9611);
865 	regmap_write(lt9611->regmap, 0x80ee, 0x01);
866 
867 	lt9611->sleep = false;
868 }
869 
870 static void
871 lt9611_bridge_atomic_post_disable(struct drm_bridge *bridge,
872 				  struct drm_bridge_state *old_bridge_state)
873 {
874 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
875 
876 	lt9611_sleep_setup(lt9611);
877 }
878 
879 static void lt9611_bridge_mode_set(struct drm_bridge *bridge,
880 				   const struct drm_display_mode *mode,
881 				   const struct drm_display_mode *adj_mode)
882 {
883 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
884 	struct hdmi_avi_infoframe avi_frame;
885 	int ret;
886 
887 	lt9611_bridge_pre_enable(bridge);
888 
889 	lt9611_mipi_input_digital(lt9611, mode);
890 	lt9611_pll_setup(lt9611, mode);
891 	lt9611_mipi_video_setup(lt9611, mode);
892 	lt9611_pcr_setup(lt9611, mode);
893 
894 	ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame,
895 						       &lt9611->connector,
896 						       mode);
897 	if (!ret)
898 		lt9611->vic = avi_frame.video_code;
899 }
900 
901 static enum drm_connector_status lt9611_bridge_detect(struct drm_bridge *bridge)
902 {
903 	return __lt9611_detect(bridge_to_lt9611(bridge));
904 }
905 
906 static struct edid *lt9611_bridge_get_edid(struct drm_bridge *bridge,
907 					   struct drm_connector *connector)
908 {
909 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
910 
911 	lt9611_power_on(lt9611);
912 	return drm_do_get_edid(connector, lt9611_get_edid_block, lt9611);
913 }
914 
915 static void lt9611_bridge_hpd_enable(struct drm_bridge *bridge)
916 {
917 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
918 
919 	lt9611_enable_hpd_interrupts(lt9611);
920 }
921 
922 #define MAX_INPUT_SEL_FORMATS	1
923 
924 static u32 *
925 lt9611_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
926 				 struct drm_bridge_state *bridge_state,
927 				 struct drm_crtc_state *crtc_state,
928 				 struct drm_connector_state *conn_state,
929 				 u32 output_fmt,
930 				 unsigned int *num_input_fmts)
931 {
932 	u32 *input_fmts;
933 
934 	*num_input_fmts = 0;
935 
936 	input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
937 			     GFP_KERNEL);
938 	if (!input_fmts)
939 		return NULL;
940 
941 	/* This is the DSI-end bus format */
942 	input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
943 	*num_input_fmts = 1;
944 
945 	return input_fmts;
946 }
947 
948 static const struct drm_bridge_funcs lt9611_bridge_funcs = {
949 	.attach = lt9611_bridge_attach,
950 	.mode_valid = lt9611_bridge_mode_valid,
951 	.mode_set = lt9611_bridge_mode_set,
952 	.detect = lt9611_bridge_detect,
953 	.get_edid = lt9611_bridge_get_edid,
954 	.hpd_enable = lt9611_bridge_hpd_enable,
955 
956 	.atomic_enable = lt9611_bridge_atomic_enable,
957 	.atomic_disable = lt9611_bridge_atomic_disable,
958 	.atomic_post_disable = lt9611_bridge_atomic_post_disable,
959 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
960 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
961 	.atomic_reset = drm_atomic_helper_bridge_reset,
962 	.atomic_get_input_bus_fmts = lt9611_atomic_get_input_bus_fmts,
963 };
964 
965 static int lt9611_parse_dt(struct device *dev,
966 			   struct lt9611 *lt9611)
967 {
968 	lt9611->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1);
969 	if (!lt9611->dsi0_node) {
970 		dev_err(lt9611->dev, "failed to get remote node for primary dsi\n");
971 		return -ENODEV;
972 	}
973 
974 	lt9611->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1);
975 
976 	lt9611->ac_mode = of_property_read_bool(dev->of_node, "lt,ac-mode");
977 
978 	return 0;
979 }
980 
981 static int lt9611_gpio_init(struct lt9611 *lt9611)
982 {
983 	struct device *dev = lt9611->dev;
984 
985 	lt9611->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
986 	if (IS_ERR(lt9611->reset_gpio)) {
987 		dev_err(dev, "failed to acquire reset gpio\n");
988 		return PTR_ERR(lt9611->reset_gpio);
989 	}
990 
991 	lt9611->enable_gpio = devm_gpiod_get_optional(dev, "enable",
992 						      GPIOD_OUT_LOW);
993 	if (IS_ERR(lt9611->enable_gpio)) {
994 		dev_err(dev, "failed to acquire enable gpio\n");
995 		return PTR_ERR(lt9611->enable_gpio);
996 	}
997 
998 	return 0;
999 }
1000 
1001 static int lt9611_read_device_rev(struct lt9611 *lt9611)
1002 {
1003 	unsigned int rev;
1004 	int ret;
1005 
1006 	regmap_write(lt9611->regmap, 0x80ee, 0x01);
1007 	ret = regmap_read(lt9611->regmap, 0x8002, &rev);
1008 	if (ret)
1009 		dev_err(lt9611->dev, "failed to read revision: %d\n", ret);
1010 	else
1011 		dev_info(lt9611->dev, "LT9611 revision: 0x%x\n", rev);
1012 
1013 	return ret;
1014 }
1015 
1016 static int lt9611_hdmi_hw_params(struct device *dev, void *data,
1017 				 struct hdmi_codec_daifmt *fmt,
1018 				 struct hdmi_codec_params *hparms)
1019 {
1020 	struct lt9611 *lt9611 = data;
1021 
1022 	if (hparms->sample_rate == 48000)
1023 		regmap_write(lt9611->regmap, 0x840f, 0x2b);
1024 	else if (hparms->sample_rate == 96000)
1025 		regmap_write(lt9611->regmap, 0x840f, 0xab);
1026 	else
1027 		return -EINVAL;
1028 
1029 	regmap_write(lt9611->regmap, 0x8435, 0x00);
1030 	regmap_write(lt9611->regmap, 0x8436, 0x18);
1031 	regmap_write(lt9611->regmap, 0x8437, 0x00);
1032 
1033 	return 0;
1034 }
1035 
1036 static int lt9611_audio_startup(struct device *dev, void *data)
1037 {
1038 	struct lt9611 *lt9611 = data;
1039 
1040 	regmap_write(lt9611->regmap, 0x82d6, 0x8c);
1041 	regmap_write(lt9611->regmap, 0x82d7, 0x04);
1042 
1043 	regmap_write(lt9611->regmap, 0x8406, 0x08);
1044 	regmap_write(lt9611->regmap, 0x8407, 0x10);
1045 
1046 	regmap_write(lt9611->regmap, 0x8434, 0xd5);
1047 
1048 	return 0;
1049 }
1050 
1051 static void lt9611_audio_shutdown(struct device *dev, void *data)
1052 {
1053 	struct lt9611 *lt9611 = data;
1054 
1055 	regmap_write(lt9611->regmap, 0x8406, 0x00);
1056 	regmap_write(lt9611->regmap, 0x8407, 0x00);
1057 }
1058 
1059 static int lt9611_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
1060 				      struct device_node *endpoint)
1061 {
1062 	struct of_endpoint of_ep;
1063 	int ret;
1064 
1065 	ret = of_graph_parse_endpoint(endpoint, &of_ep);
1066 	if (ret < 0)
1067 		return ret;
1068 
1069 	/*
1070 	 * HDMI sound should be located as reg = <2>
1071 	 * Then, it is sound port 0
1072 	 */
1073 	if (of_ep.port == 2)
1074 		return 0;
1075 
1076 	return -EINVAL;
1077 }
1078 
1079 static const struct hdmi_codec_ops lt9611_codec_ops = {
1080 	.hw_params	= lt9611_hdmi_hw_params,
1081 	.audio_shutdown = lt9611_audio_shutdown,
1082 	.audio_startup	= lt9611_audio_startup,
1083 	.get_dai_id	= lt9611_hdmi_i2s_get_dai_id,
1084 };
1085 
1086 static struct hdmi_codec_pdata codec_data = {
1087 	.ops = &lt9611_codec_ops,
1088 	.max_i2s_channels = 8,
1089 	.i2s = 1,
1090 };
1091 
1092 static int lt9611_audio_init(struct device *dev, struct lt9611 *lt9611)
1093 {
1094 	codec_data.data = lt9611;
1095 	lt9611->audio_pdev =
1096 		platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
1097 					      PLATFORM_DEVID_AUTO,
1098 					      &codec_data, sizeof(codec_data));
1099 
1100 	return PTR_ERR_OR_ZERO(lt9611->audio_pdev);
1101 }
1102 
1103 static void lt9611_audio_exit(struct lt9611 *lt9611)
1104 {
1105 	if (lt9611->audio_pdev) {
1106 		platform_device_unregister(lt9611->audio_pdev);
1107 		lt9611->audio_pdev = NULL;
1108 	}
1109 }
1110 
1111 static int lt9611_probe(struct i2c_client *client,
1112 			const struct i2c_device_id *id)
1113 {
1114 	struct lt9611 *lt9611;
1115 	struct device *dev = &client->dev;
1116 	int ret;
1117 
1118 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1119 		dev_err(dev, "device doesn't support I2C\n");
1120 		return -ENODEV;
1121 	}
1122 
1123 	lt9611 = devm_kzalloc(dev, sizeof(*lt9611), GFP_KERNEL);
1124 	if (!lt9611)
1125 		return -ENOMEM;
1126 
1127 	lt9611->dev = dev;
1128 	lt9611->client = client;
1129 	lt9611->sleep = false;
1130 
1131 	lt9611->regmap = devm_regmap_init_i2c(client, &lt9611_regmap_config);
1132 	if (IS_ERR(lt9611->regmap)) {
1133 		dev_err(lt9611->dev, "regmap i2c init failed\n");
1134 		return PTR_ERR(lt9611->regmap);
1135 	}
1136 
1137 	ret = lt9611_parse_dt(dev, lt9611);
1138 	if (ret) {
1139 		dev_err(dev, "failed to parse device tree\n");
1140 		return ret;
1141 	}
1142 
1143 	ret = lt9611_gpio_init(lt9611);
1144 	if (ret < 0)
1145 		goto err_of_put;
1146 
1147 	ret = lt9611_regulator_init(lt9611);
1148 	if (ret < 0)
1149 		goto err_of_put;
1150 
1151 	lt9611_assert_5v(lt9611);
1152 
1153 	ret = lt9611_regulator_enable(lt9611);
1154 	if (ret)
1155 		goto err_of_put;
1156 
1157 	lt9611_reset(lt9611);
1158 
1159 	ret = lt9611_read_device_rev(lt9611);
1160 	if (ret) {
1161 		dev_err(dev, "failed to read chip rev\n");
1162 		goto err_disable_regulators;
1163 	}
1164 
1165 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
1166 					lt9611_irq_thread_handler,
1167 					IRQF_ONESHOT, "lt9611", lt9611);
1168 	if (ret) {
1169 		dev_err(dev, "failed to request irq\n");
1170 		goto err_disable_regulators;
1171 	}
1172 
1173 	i2c_set_clientdata(client, lt9611);
1174 
1175 	lt9611->bridge.funcs = &lt9611_bridge_funcs;
1176 	lt9611->bridge.of_node = client->dev.of_node;
1177 	lt9611->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID |
1178 			     DRM_BRIDGE_OP_HPD | DRM_BRIDGE_OP_MODES;
1179 	lt9611->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
1180 
1181 	drm_bridge_add(&lt9611->bridge);
1182 
1183 	/* Attach primary DSI */
1184 	lt9611->dsi0 = lt9611_attach_dsi(lt9611, lt9611->dsi0_node);
1185 	if (IS_ERR(lt9611->dsi0)) {
1186 		ret = PTR_ERR(lt9611->dsi0);
1187 		goto err_remove_bridge;
1188 	}
1189 
1190 	/* Attach secondary DSI, if specified */
1191 	if (lt9611->dsi1_node) {
1192 		lt9611->dsi1 = lt9611_attach_dsi(lt9611, lt9611->dsi1_node);
1193 		if (IS_ERR(lt9611->dsi1)) {
1194 			ret = PTR_ERR(lt9611->dsi1);
1195 			goto err_remove_bridge;
1196 		}
1197 	}
1198 
1199 	lt9611_enable_hpd_interrupts(lt9611);
1200 
1201 	ret = lt9611_audio_init(dev, lt9611);
1202 	if (ret)
1203 		goto err_remove_bridge;
1204 
1205 	return 0;
1206 
1207 err_remove_bridge:
1208 	drm_bridge_remove(&lt9611->bridge);
1209 
1210 err_disable_regulators:
1211 	regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies);
1212 
1213 err_of_put:
1214 	of_node_put(lt9611->dsi1_node);
1215 	of_node_put(lt9611->dsi0_node);
1216 
1217 	return ret;
1218 }
1219 
1220 static void lt9611_remove(struct i2c_client *client)
1221 {
1222 	struct lt9611 *lt9611 = i2c_get_clientdata(client);
1223 
1224 	disable_irq(client->irq);
1225 	lt9611_audio_exit(lt9611);
1226 	drm_bridge_remove(&lt9611->bridge);
1227 
1228 	regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies);
1229 
1230 	of_node_put(lt9611->dsi1_node);
1231 	of_node_put(lt9611->dsi0_node);
1232 }
1233 
1234 static struct i2c_device_id lt9611_id[] = {
1235 	{ "lontium,lt9611", 0 },
1236 	{}
1237 };
1238 MODULE_DEVICE_TABLE(i2c, lt9611_id);
1239 
1240 static const struct of_device_id lt9611_match_table[] = {
1241 	{ .compatible = "lontium,lt9611" },
1242 	{ }
1243 };
1244 MODULE_DEVICE_TABLE(of, lt9611_match_table);
1245 
1246 static struct i2c_driver lt9611_driver = {
1247 	.driver = {
1248 		.name = "lt9611",
1249 		.of_match_table = lt9611_match_table,
1250 	},
1251 	.probe = lt9611_probe,
1252 	.remove = lt9611_remove,
1253 	.id_table = lt9611_id,
1254 };
1255 module_i2c_driver(lt9611_driver);
1256 
1257 MODULE_LICENSE("GPL v2");
1258