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