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 	drm_connector_attach_encoder(&lt9611->connector, bridge->encoder);
817 
818 	if (!bridge->encoder) {
819 		DRM_ERROR("Parent encoder object not found");
820 		return -ENODEV;
821 	}
822 
823 	return 0;
824 }
825 
826 static int lt9611_bridge_attach(struct drm_bridge *bridge,
827 				enum drm_bridge_attach_flags flags)
828 {
829 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
830 	int ret;
831 
832 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
833 		ret = lt9611_connector_init(bridge, lt9611);
834 		if (ret < 0)
835 			return ret;
836 	}
837 
838 	return 0;
839 }
840 
841 static enum drm_mode_status lt9611_bridge_mode_valid(struct drm_bridge *bridge,
842 						     const struct drm_display_info *info,
843 						     const struct drm_display_mode *mode)
844 {
845 	struct lt9611_mode *lt9611_mode = lt9611_find_mode(mode);
846 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
847 
848 	if (!lt9611_mode)
849 		return MODE_BAD;
850 	else if (lt9611_mode->intfs > 1 && !lt9611->dsi1)
851 		return MODE_PANEL;
852 	else
853 		return MODE_OK;
854 }
855 
856 static void lt9611_bridge_pre_enable(struct drm_bridge *bridge)
857 {
858 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
859 
860 	if (!lt9611->sleep)
861 		return;
862 
863 	lt9611_reset(lt9611);
864 	regmap_write(lt9611->regmap, 0x80ee, 0x01);
865 
866 	lt9611->sleep = false;
867 }
868 
869 static void
870 lt9611_bridge_atomic_post_disable(struct drm_bridge *bridge,
871 				  struct drm_bridge_state *old_bridge_state)
872 {
873 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
874 
875 	lt9611_sleep_setup(lt9611);
876 }
877 
878 static void lt9611_bridge_mode_set(struct drm_bridge *bridge,
879 				   const struct drm_display_mode *mode,
880 				   const struct drm_display_mode *adj_mode)
881 {
882 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
883 	struct hdmi_avi_infoframe avi_frame;
884 	int ret;
885 
886 	lt9611_bridge_pre_enable(bridge);
887 
888 	lt9611_mipi_input_digital(lt9611, mode);
889 	lt9611_pll_setup(lt9611, mode);
890 	lt9611_mipi_video_setup(lt9611, mode);
891 	lt9611_pcr_setup(lt9611, mode);
892 
893 	ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame,
894 						       &lt9611->connector,
895 						       mode);
896 	if (!ret)
897 		lt9611->vic = avi_frame.video_code;
898 }
899 
900 static enum drm_connector_status lt9611_bridge_detect(struct drm_bridge *bridge)
901 {
902 	return __lt9611_detect(bridge_to_lt9611(bridge));
903 }
904 
905 static struct edid *lt9611_bridge_get_edid(struct drm_bridge *bridge,
906 					   struct drm_connector *connector)
907 {
908 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
909 
910 	lt9611_power_on(lt9611);
911 	return drm_do_get_edid(connector, lt9611_get_edid_block, lt9611);
912 }
913 
914 static void lt9611_bridge_hpd_enable(struct drm_bridge *bridge)
915 {
916 	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
917 
918 	lt9611_enable_hpd_interrupts(lt9611);
919 }
920 
921 #define MAX_INPUT_SEL_FORMATS	1
922 
923 static u32 *
924 lt9611_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
925 				 struct drm_bridge_state *bridge_state,
926 				 struct drm_crtc_state *crtc_state,
927 				 struct drm_connector_state *conn_state,
928 				 u32 output_fmt,
929 				 unsigned int *num_input_fmts)
930 {
931 	u32 *input_fmts;
932 
933 	*num_input_fmts = 0;
934 
935 	input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
936 			     GFP_KERNEL);
937 	if (!input_fmts)
938 		return NULL;
939 
940 	/* This is the DSI-end bus format */
941 	input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
942 	*num_input_fmts = 1;
943 
944 	return input_fmts;
945 }
946 
947 static const struct drm_bridge_funcs lt9611_bridge_funcs = {
948 	.attach = lt9611_bridge_attach,
949 	.mode_valid = lt9611_bridge_mode_valid,
950 	.mode_set = lt9611_bridge_mode_set,
951 	.detect = lt9611_bridge_detect,
952 	.get_edid = lt9611_bridge_get_edid,
953 	.hpd_enable = lt9611_bridge_hpd_enable,
954 
955 	.atomic_enable = lt9611_bridge_atomic_enable,
956 	.atomic_disable = lt9611_bridge_atomic_disable,
957 	.atomic_post_disable = lt9611_bridge_atomic_post_disable,
958 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
959 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
960 	.atomic_reset = drm_atomic_helper_bridge_reset,
961 	.atomic_get_input_bus_fmts = lt9611_atomic_get_input_bus_fmts,
962 };
963 
964 static int lt9611_parse_dt(struct device *dev,
965 			   struct lt9611 *lt9611)
966 {
967 	lt9611->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1);
968 	if (!lt9611->dsi0_node) {
969 		dev_err(lt9611->dev, "failed to get remote node for primary dsi\n");
970 		return -ENODEV;
971 	}
972 
973 	lt9611->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1);
974 
975 	lt9611->ac_mode = of_property_read_bool(dev->of_node, "lt,ac-mode");
976 
977 	return 0;
978 }
979 
980 static int lt9611_gpio_init(struct lt9611 *lt9611)
981 {
982 	struct device *dev = lt9611->dev;
983 
984 	lt9611->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
985 	if (IS_ERR(lt9611->reset_gpio)) {
986 		dev_err(dev, "failed to acquire reset gpio\n");
987 		return PTR_ERR(lt9611->reset_gpio);
988 	}
989 
990 	lt9611->enable_gpio = devm_gpiod_get_optional(dev, "enable",
991 						      GPIOD_OUT_LOW);
992 	if (IS_ERR(lt9611->enable_gpio)) {
993 		dev_err(dev, "failed to acquire enable gpio\n");
994 		return PTR_ERR(lt9611->enable_gpio);
995 	}
996 
997 	return 0;
998 }
999 
1000 static int lt9611_read_device_rev(struct lt9611 *lt9611)
1001 {
1002 	unsigned int rev;
1003 	int ret;
1004 
1005 	regmap_write(lt9611->regmap, 0x80ee, 0x01);
1006 	ret = regmap_read(lt9611->regmap, 0x8002, &rev);
1007 	if (ret)
1008 		dev_err(lt9611->dev, "failed to read revision: %d\n", ret);
1009 	else
1010 		dev_info(lt9611->dev, "LT9611 revision: 0x%x\n", rev);
1011 
1012 	return ret;
1013 }
1014 
1015 static int lt9611_hdmi_hw_params(struct device *dev, void *data,
1016 				 struct hdmi_codec_daifmt *fmt,
1017 				 struct hdmi_codec_params *hparms)
1018 {
1019 	struct lt9611 *lt9611 = data;
1020 
1021 	if (hparms->sample_rate == 48000)
1022 		regmap_write(lt9611->regmap, 0x840f, 0x2b);
1023 	else if (hparms->sample_rate == 96000)
1024 		regmap_write(lt9611->regmap, 0x840f, 0xab);
1025 	else
1026 		return -EINVAL;
1027 
1028 	regmap_write(lt9611->regmap, 0x8435, 0x00);
1029 	regmap_write(lt9611->regmap, 0x8436, 0x18);
1030 	regmap_write(lt9611->regmap, 0x8437, 0x00);
1031 
1032 	return 0;
1033 }
1034 
1035 static int lt9611_audio_startup(struct device *dev, void *data)
1036 {
1037 	struct lt9611 *lt9611 = data;
1038 
1039 	regmap_write(lt9611->regmap, 0x82d6, 0x8c);
1040 	regmap_write(lt9611->regmap, 0x82d7, 0x04);
1041 
1042 	regmap_write(lt9611->regmap, 0x8406, 0x08);
1043 	regmap_write(lt9611->regmap, 0x8407, 0x10);
1044 
1045 	regmap_write(lt9611->regmap, 0x8434, 0xd5);
1046 
1047 	return 0;
1048 }
1049 
1050 static void lt9611_audio_shutdown(struct device *dev, void *data)
1051 {
1052 	struct lt9611 *lt9611 = data;
1053 
1054 	regmap_write(lt9611->regmap, 0x8406, 0x00);
1055 	regmap_write(lt9611->regmap, 0x8407, 0x00);
1056 }
1057 
1058 static int lt9611_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
1059 				      struct device_node *endpoint)
1060 {
1061 	struct of_endpoint of_ep;
1062 	int ret;
1063 
1064 	ret = of_graph_parse_endpoint(endpoint, &of_ep);
1065 	if (ret < 0)
1066 		return ret;
1067 
1068 	/*
1069 	 * HDMI sound should be located as reg = <2>
1070 	 * Then, it is sound port 0
1071 	 */
1072 	if (of_ep.port == 2)
1073 		return 0;
1074 
1075 	return -EINVAL;
1076 }
1077 
1078 static const struct hdmi_codec_ops lt9611_codec_ops = {
1079 	.hw_params	= lt9611_hdmi_hw_params,
1080 	.audio_shutdown = lt9611_audio_shutdown,
1081 	.audio_startup	= lt9611_audio_startup,
1082 	.get_dai_id	= lt9611_hdmi_i2s_get_dai_id,
1083 };
1084 
1085 static struct hdmi_codec_pdata codec_data = {
1086 	.ops = &lt9611_codec_ops,
1087 	.max_i2s_channels = 8,
1088 	.i2s = 1,
1089 };
1090 
1091 static int lt9611_audio_init(struct device *dev, struct lt9611 *lt9611)
1092 {
1093 	codec_data.data = lt9611;
1094 	lt9611->audio_pdev =
1095 		platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
1096 					      PLATFORM_DEVID_AUTO,
1097 					      &codec_data, sizeof(codec_data));
1098 
1099 	return PTR_ERR_OR_ZERO(lt9611->audio_pdev);
1100 }
1101 
1102 static void lt9611_audio_exit(struct lt9611 *lt9611)
1103 {
1104 	if (lt9611->audio_pdev) {
1105 		platform_device_unregister(lt9611->audio_pdev);
1106 		lt9611->audio_pdev = NULL;
1107 	}
1108 }
1109 
1110 static int lt9611_probe(struct i2c_client *client,
1111 			const struct i2c_device_id *id)
1112 {
1113 	struct lt9611 *lt9611;
1114 	struct device *dev = &client->dev;
1115 	int ret;
1116 
1117 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1118 		dev_err(dev, "device doesn't support I2C\n");
1119 		return -ENODEV;
1120 	}
1121 
1122 	lt9611 = devm_kzalloc(dev, sizeof(*lt9611), GFP_KERNEL);
1123 	if (!lt9611)
1124 		return -ENOMEM;
1125 
1126 	lt9611->dev = dev;
1127 	lt9611->client = client;
1128 	lt9611->sleep = false;
1129 
1130 	lt9611->regmap = devm_regmap_init_i2c(client, &lt9611_regmap_config);
1131 	if (IS_ERR(lt9611->regmap)) {
1132 		dev_err(lt9611->dev, "regmap i2c init failed\n");
1133 		return PTR_ERR(lt9611->regmap);
1134 	}
1135 
1136 	ret = lt9611_parse_dt(dev, lt9611);
1137 	if (ret) {
1138 		dev_err(dev, "failed to parse device tree\n");
1139 		return ret;
1140 	}
1141 
1142 	ret = lt9611_gpio_init(lt9611);
1143 	if (ret < 0)
1144 		goto err_of_put;
1145 
1146 	ret = lt9611_regulator_init(lt9611);
1147 	if (ret < 0)
1148 		goto err_of_put;
1149 
1150 	lt9611_assert_5v(lt9611);
1151 
1152 	ret = lt9611_regulator_enable(lt9611);
1153 	if (ret)
1154 		goto err_of_put;
1155 
1156 	lt9611_reset(lt9611);
1157 
1158 	ret = lt9611_read_device_rev(lt9611);
1159 	if (ret) {
1160 		dev_err(dev, "failed to read chip rev\n");
1161 		goto err_disable_regulators;
1162 	}
1163 
1164 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
1165 					lt9611_irq_thread_handler,
1166 					IRQF_ONESHOT, "lt9611", lt9611);
1167 	if (ret) {
1168 		dev_err(dev, "failed to request irq\n");
1169 		goto err_disable_regulators;
1170 	}
1171 
1172 	i2c_set_clientdata(client, lt9611);
1173 
1174 	lt9611->bridge.funcs = &lt9611_bridge_funcs;
1175 	lt9611->bridge.of_node = client->dev.of_node;
1176 	lt9611->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID |
1177 			     DRM_BRIDGE_OP_HPD | DRM_BRIDGE_OP_MODES;
1178 	lt9611->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
1179 
1180 	drm_bridge_add(&lt9611->bridge);
1181 
1182 	/* Attach primary DSI */
1183 	lt9611->dsi0 = lt9611_attach_dsi(lt9611, lt9611->dsi0_node);
1184 	if (IS_ERR(lt9611->dsi0)) {
1185 		ret = PTR_ERR(lt9611->dsi0);
1186 		goto err_remove_bridge;
1187 	}
1188 
1189 	/* Attach secondary DSI, if specified */
1190 	if (lt9611->dsi1_node) {
1191 		lt9611->dsi1 = lt9611_attach_dsi(lt9611, lt9611->dsi1_node);
1192 		if (IS_ERR(lt9611->dsi1)) {
1193 			ret = PTR_ERR(lt9611->dsi1);
1194 			goto err_remove_bridge;
1195 		}
1196 	}
1197 
1198 	lt9611_enable_hpd_interrupts(lt9611);
1199 
1200 	ret = lt9611_audio_init(dev, lt9611);
1201 	if (ret)
1202 		goto err_remove_bridge;
1203 
1204 	return 0;
1205 
1206 err_remove_bridge:
1207 	drm_bridge_remove(&lt9611->bridge);
1208 
1209 err_disable_regulators:
1210 	regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies);
1211 
1212 err_of_put:
1213 	of_node_put(lt9611->dsi1_node);
1214 	of_node_put(lt9611->dsi0_node);
1215 
1216 	return ret;
1217 }
1218 
1219 static int lt9611_remove(struct i2c_client *client)
1220 {
1221 	struct lt9611 *lt9611 = i2c_get_clientdata(client);
1222 
1223 	disable_irq(client->irq);
1224 	lt9611_audio_exit(lt9611);
1225 	drm_bridge_remove(&lt9611->bridge);
1226 
1227 	regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies);
1228 
1229 	of_node_put(lt9611->dsi1_node);
1230 	of_node_put(lt9611->dsi0_node);
1231 
1232 	return 0;
1233 }
1234 
1235 static struct i2c_device_id lt9611_id[] = {
1236 	{ "lontium,lt9611", 0 },
1237 	{}
1238 };
1239 MODULE_DEVICE_TABLE(i2c, lt9611_id);
1240 
1241 static const struct of_device_id lt9611_match_table[] = {
1242 	{ .compatible = "lontium,lt9611" },
1243 	{ }
1244 };
1245 MODULE_DEVICE_TABLE(of, lt9611_match_table);
1246 
1247 static struct i2c_driver lt9611_driver = {
1248 	.driver = {
1249 		.name = "lt9611",
1250 		.of_match_table = lt9611_match_table,
1251 	},
1252 	.probe = lt9611_probe,
1253 	.remove = lt9611_remove,
1254 	.id_table = lt9611_id,
1255 };
1256 module_i2c_driver(lt9611_driver);
1257 
1258 MODULE_LICENSE("GPL v2");
1259