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