drm/nouveau: fence: fix undefined fence state after emit
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / bridge / lontium-lt9611.c
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 | MIPI_DSI_MODE_VIDEO_NO_HSA |
778                           MIPI_DSI_MODE_VIDEO_NO_HFP | MIPI_DSI_MODE_VIDEO_NO_HBP |
779                           MIPI_DSI_MODE_NO_EOT_PACKET;
780
781         ret = devm_mipi_dsi_attach(dev, dsi);
782         if (ret < 0) {
783                 dev_err(dev, "failed to attach dsi to host\n");
784                 return ERR_PTR(ret);
785         }
786
787         return dsi;
788 }
789
790 static int lt9611_bridge_attach(struct drm_bridge *bridge,
791                                 enum drm_bridge_attach_flags flags)
792 {
793         struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
794
795         return drm_bridge_attach(bridge->encoder, lt9611->next_bridge,
796                                  bridge, flags);
797 }
798
799 static enum drm_mode_status lt9611_bridge_mode_valid(struct drm_bridge *bridge,
800                                                      const struct drm_display_info *info,
801                                                      const struct drm_display_mode *mode)
802 {
803         struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
804
805         if (mode->hdisplay > 3840)
806                 return MODE_BAD_HVALUE;
807
808         if (mode->vdisplay > 2160)
809                 return MODE_BAD_VVALUE;
810
811         if (mode->hdisplay == 3840 &&
812             mode->vdisplay == 2160 &&
813             drm_mode_vrefresh(mode) > 30)
814                 return MODE_CLOCK_HIGH;
815
816         if (mode->hdisplay > 2000 && !lt9611->dsi1_node)
817                 return MODE_PANEL;
818         else
819                 return MODE_OK;
820 }
821
822 static void lt9611_bridge_atomic_pre_enable(struct drm_bridge *bridge,
823                                             struct drm_bridge_state *old_bridge_state)
824 {
825         struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
826         static const struct reg_sequence reg_cfg[] = {
827                 { 0x8102, 0x12 },
828                 { 0x8123, 0x40 },
829                 { 0x8130, 0xea },
830                 { 0x8011, 0xfa },
831         };
832
833         if (!lt9611->sleep)
834                 return;
835
836         regmap_multi_reg_write(lt9611->regmap,
837                                reg_cfg, ARRAY_SIZE(reg_cfg));
838
839         lt9611->sleep = false;
840 }
841
842 static void
843 lt9611_bridge_atomic_post_disable(struct drm_bridge *bridge,
844                                   struct drm_bridge_state *old_bridge_state)
845 {
846         struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
847
848         lt9611_sleep_setup(lt9611);
849 }
850
851 static struct edid *lt9611_bridge_get_edid(struct drm_bridge *bridge,
852                                            struct drm_connector *connector)
853 {
854         struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
855
856         lt9611_power_on(lt9611);
857         return drm_do_get_edid(connector, lt9611_get_edid_block, lt9611);
858 }
859
860 static void lt9611_bridge_hpd_enable(struct drm_bridge *bridge)
861 {
862         struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
863
864         lt9611_enable_hpd_interrupts(lt9611);
865 }
866
867 #define MAX_INPUT_SEL_FORMATS   1
868
869 static u32 *
870 lt9611_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
871                                  struct drm_bridge_state *bridge_state,
872                                  struct drm_crtc_state *crtc_state,
873                                  struct drm_connector_state *conn_state,
874                                  u32 output_fmt,
875                                  unsigned int *num_input_fmts)
876 {
877         u32 *input_fmts;
878
879         *num_input_fmts = 0;
880
881         input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
882                              GFP_KERNEL);
883         if (!input_fmts)
884                 return NULL;
885
886         /* This is the DSI-end bus format */
887         input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
888         *num_input_fmts = 1;
889
890         return input_fmts;
891 }
892
893 static const struct drm_bridge_funcs lt9611_bridge_funcs = {
894         .attach = lt9611_bridge_attach,
895         .mode_valid = lt9611_bridge_mode_valid,
896         .detect = lt9611_bridge_detect,
897         .get_edid = lt9611_bridge_get_edid,
898         .hpd_enable = lt9611_bridge_hpd_enable,
899
900         .atomic_pre_enable = lt9611_bridge_atomic_pre_enable,
901         .atomic_enable = lt9611_bridge_atomic_enable,
902         .atomic_disable = lt9611_bridge_atomic_disable,
903         .atomic_post_disable = lt9611_bridge_atomic_post_disable,
904         .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
905         .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
906         .atomic_reset = drm_atomic_helper_bridge_reset,
907         .atomic_get_input_bus_fmts = lt9611_atomic_get_input_bus_fmts,
908 };
909
910 static int lt9611_parse_dt(struct device *dev,
911                            struct lt9611 *lt9611)
912 {
913         lt9611->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1);
914         if (!lt9611->dsi0_node) {
915                 dev_err(lt9611->dev, "failed to get remote node for primary dsi\n");
916                 return -ENODEV;
917         }
918
919         lt9611->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1);
920
921         lt9611->ac_mode = of_property_read_bool(dev->of_node, "lt,ac-mode");
922
923         return drm_of_find_panel_or_bridge(dev->of_node, 2, -1, NULL, &lt9611->next_bridge);
924 }
925
926 static int lt9611_gpio_init(struct lt9611 *lt9611)
927 {
928         struct device *dev = lt9611->dev;
929
930         lt9611->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
931         if (IS_ERR(lt9611->reset_gpio)) {
932                 dev_err(dev, "failed to acquire reset gpio\n");
933                 return PTR_ERR(lt9611->reset_gpio);
934         }
935
936         lt9611->enable_gpio = devm_gpiod_get_optional(dev, "enable",
937                                                       GPIOD_OUT_LOW);
938         if (IS_ERR(lt9611->enable_gpio)) {
939                 dev_err(dev, "failed to acquire enable gpio\n");
940                 return PTR_ERR(lt9611->enable_gpio);
941         }
942
943         return 0;
944 }
945
946 static int lt9611_read_device_rev(struct lt9611 *lt9611)
947 {
948         unsigned int rev;
949         int ret;
950
951         regmap_write(lt9611->regmap, 0x80ee, 0x01);
952         ret = regmap_read(lt9611->regmap, 0x8002, &rev);
953         if (ret)
954                 dev_err(lt9611->dev, "failed to read revision: %d\n", ret);
955         else
956                 dev_info(lt9611->dev, "LT9611 revision: 0x%x\n", rev);
957
958         return ret;
959 }
960
961 static int lt9611_hdmi_hw_params(struct device *dev, void *data,
962                                  struct hdmi_codec_daifmt *fmt,
963                                  struct hdmi_codec_params *hparms)
964 {
965         struct lt9611 *lt9611 = data;
966
967         if (hparms->sample_rate == 48000)
968                 regmap_write(lt9611->regmap, 0x840f, 0x2b);
969         else if (hparms->sample_rate == 96000)
970                 regmap_write(lt9611->regmap, 0x840f, 0xab);
971         else
972                 return -EINVAL;
973
974         regmap_write(lt9611->regmap, 0x8435, 0x00);
975         regmap_write(lt9611->regmap, 0x8436, 0x18);
976         regmap_write(lt9611->regmap, 0x8437, 0x00);
977
978         return 0;
979 }
980
981 static int lt9611_audio_startup(struct device *dev, void *data)
982 {
983         struct lt9611 *lt9611 = data;
984
985         regmap_write(lt9611->regmap, 0x82d6, 0x8c);
986         regmap_write(lt9611->regmap, 0x82d7, 0x04);
987
988         regmap_write(lt9611->regmap, 0x8406, 0x08);
989         regmap_write(lt9611->regmap, 0x8407, 0x10);
990
991         regmap_write(lt9611->regmap, 0x8434, 0xd5);
992
993         return 0;
994 }
995
996 static void lt9611_audio_shutdown(struct device *dev, void *data)
997 {
998         struct lt9611 *lt9611 = data;
999
1000         regmap_write(lt9611->regmap, 0x8406, 0x00);
1001         regmap_write(lt9611->regmap, 0x8407, 0x00);
1002 }
1003
1004 static int lt9611_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
1005                                       struct device_node *endpoint)
1006 {
1007         struct of_endpoint of_ep;
1008         int ret;
1009
1010         ret = of_graph_parse_endpoint(endpoint, &of_ep);
1011         if (ret < 0)
1012                 return ret;
1013
1014         /*
1015          * HDMI sound should be located as reg = <2>
1016          * Then, it is sound port 0
1017          */
1018         if (of_ep.port == 2)
1019                 return 0;
1020
1021         return -EINVAL;
1022 }
1023
1024 static const struct hdmi_codec_ops lt9611_codec_ops = {
1025         .hw_params      = lt9611_hdmi_hw_params,
1026         .audio_shutdown = lt9611_audio_shutdown,
1027         .audio_startup  = lt9611_audio_startup,
1028         .get_dai_id     = lt9611_hdmi_i2s_get_dai_id,
1029 };
1030
1031 static struct hdmi_codec_pdata codec_data = {
1032         .ops = &lt9611_codec_ops,
1033         .max_i2s_channels = 8,
1034         .i2s = 1,
1035 };
1036
1037 static int lt9611_audio_init(struct device *dev, struct lt9611 *lt9611)
1038 {
1039         codec_data.data = lt9611;
1040         lt9611->audio_pdev =
1041                 platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
1042                                               PLATFORM_DEVID_AUTO,
1043                                               &codec_data, sizeof(codec_data));
1044
1045         return PTR_ERR_OR_ZERO(lt9611->audio_pdev);
1046 }
1047
1048 static void lt9611_audio_exit(struct lt9611 *lt9611)
1049 {
1050         if (lt9611->audio_pdev) {
1051                 platform_device_unregister(lt9611->audio_pdev);
1052                 lt9611->audio_pdev = NULL;
1053         }
1054 }
1055
1056 static int lt9611_probe(struct i2c_client *client)
1057 {
1058         struct lt9611 *lt9611;
1059         struct device *dev = &client->dev;
1060         int ret;
1061
1062         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1063                 dev_err(dev, "device doesn't support I2C\n");
1064                 return -ENODEV;
1065         }
1066
1067         lt9611 = devm_kzalloc(dev, sizeof(*lt9611), GFP_KERNEL);
1068         if (!lt9611)
1069                 return -ENOMEM;
1070
1071         lt9611->dev = dev;
1072         lt9611->client = client;
1073         lt9611->sleep = false;
1074
1075         lt9611->regmap = devm_regmap_init_i2c(client, &lt9611_regmap_config);
1076         if (IS_ERR(lt9611->regmap)) {
1077                 dev_err(lt9611->dev, "regmap i2c init failed\n");
1078                 return PTR_ERR(lt9611->regmap);
1079         }
1080
1081         ret = lt9611_parse_dt(dev, lt9611);
1082         if (ret) {
1083                 dev_err(dev, "failed to parse device tree\n");
1084                 return ret;
1085         }
1086
1087         ret = lt9611_gpio_init(lt9611);
1088         if (ret < 0)
1089                 goto err_of_put;
1090
1091         ret = lt9611_regulator_init(lt9611);
1092         if (ret < 0)
1093                 goto err_of_put;
1094
1095         lt9611_assert_5v(lt9611);
1096
1097         ret = lt9611_regulator_enable(lt9611);
1098         if (ret)
1099                 goto err_of_put;
1100
1101         lt9611_reset(lt9611);
1102
1103         ret = lt9611_read_device_rev(lt9611);
1104         if (ret) {
1105                 dev_err(dev, "failed to read chip rev\n");
1106                 goto err_disable_regulators;
1107         }
1108
1109         ret = devm_request_threaded_irq(dev, client->irq, NULL,
1110                                         lt9611_irq_thread_handler,
1111                                         IRQF_ONESHOT, "lt9611", lt9611);
1112         if (ret) {
1113                 dev_err(dev, "failed to request irq\n");
1114                 goto err_disable_regulators;
1115         }
1116
1117         i2c_set_clientdata(client, lt9611);
1118
1119         lt9611->bridge.funcs = &lt9611_bridge_funcs;
1120         lt9611->bridge.of_node = client->dev.of_node;
1121         lt9611->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID |
1122                              DRM_BRIDGE_OP_HPD | DRM_BRIDGE_OP_MODES;
1123         lt9611->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
1124
1125         drm_bridge_add(&lt9611->bridge);
1126
1127         /* Attach primary DSI */
1128         lt9611->dsi0 = lt9611_attach_dsi(lt9611, lt9611->dsi0_node);
1129         if (IS_ERR(lt9611->dsi0)) {
1130                 ret = PTR_ERR(lt9611->dsi0);
1131                 goto err_remove_bridge;
1132         }
1133
1134         /* Attach secondary DSI, if specified */
1135         if (lt9611->dsi1_node) {
1136                 lt9611->dsi1 = lt9611_attach_dsi(lt9611, lt9611->dsi1_node);
1137                 if (IS_ERR(lt9611->dsi1)) {
1138                         ret = PTR_ERR(lt9611->dsi1);
1139                         goto err_remove_bridge;
1140                 }
1141         }
1142
1143         lt9611_enable_hpd_interrupts(lt9611);
1144
1145         ret = lt9611_audio_init(dev, lt9611);
1146         if (ret)
1147                 goto err_remove_bridge;
1148
1149         return 0;
1150
1151 err_remove_bridge:
1152         drm_bridge_remove(&lt9611->bridge);
1153
1154 err_disable_regulators:
1155         regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies);
1156
1157 err_of_put:
1158         of_node_put(lt9611->dsi1_node);
1159         of_node_put(lt9611->dsi0_node);
1160
1161         return ret;
1162 }
1163
1164 static void lt9611_remove(struct i2c_client *client)
1165 {
1166         struct lt9611 *lt9611 = i2c_get_clientdata(client);
1167
1168         disable_irq(client->irq);
1169         lt9611_audio_exit(lt9611);
1170         drm_bridge_remove(&lt9611->bridge);
1171
1172         regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies);
1173
1174         of_node_put(lt9611->dsi1_node);
1175         of_node_put(lt9611->dsi0_node);
1176 }
1177
1178 static struct i2c_device_id lt9611_id[] = {
1179         { "lontium,lt9611", 0 },
1180         {}
1181 };
1182 MODULE_DEVICE_TABLE(i2c, lt9611_id);
1183
1184 static const struct of_device_id lt9611_match_table[] = {
1185         { .compatible = "lontium,lt9611" },
1186         { }
1187 };
1188 MODULE_DEVICE_TABLE(of, lt9611_match_table);
1189
1190 static struct i2c_driver lt9611_driver = {
1191         .driver = {
1192                 .name = "lt9611",
1193                 .of_match_table = lt9611_match_table,
1194         },
1195         .probe = lt9611_probe,
1196         .remove = lt9611_remove,
1197         .id_table = lt9611_id,
1198 };
1199 module_i2c_driver(lt9611_driver);
1200
1201 MODULE_LICENSE("GPL v2");