Merge tag 'perf-kprobes-2020-12-14' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / drivers / gpu / drm / bridge / lontium-lt9611uxc.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/firmware.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/mutex.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 #include <linux/wait.h>
17
18 #include <sound/hdmi-codec.h>
19
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_bridge.h>
22 #include <drm/drm_mipi_dsi.h>
23 #include <drm/drm_print.h>
24 #include <drm/drm_probe_helper.h>
25
26 #define EDID_BLOCK_SIZE 128
27 #define EDID_NUM_BLOCKS 2
28
29 struct lt9611uxc {
30         struct device *dev;
31         struct drm_bridge bridge;
32         struct drm_connector connector;
33
34         struct regmap *regmap;
35         /* Protects all accesses to registers by stopping the on-chip MCU */
36         struct mutex ocm_lock;
37
38         struct wait_queue_head wq;
39
40         struct device_node *dsi0_node;
41         struct device_node *dsi1_node;
42         struct mipi_dsi_device *dsi0;
43         struct mipi_dsi_device *dsi1;
44         struct platform_device *audio_pdev;
45
46         struct gpio_desc *reset_gpio;
47         struct gpio_desc *enable_gpio;
48
49         struct regulator_bulk_data supplies[2];
50
51         struct i2c_client *client;
52
53         bool hpd_supported;
54         bool edid_read;
55         uint8_t fw_version;
56 };
57
58 #define LT9611_PAGE_CONTROL     0xff
59
60 static const struct regmap_range_cfg lt9611uxc_ranges[] = {
61         {
62                 .name = "register_range",
63                 .range_min =  0,
64                 .range_max = 0xd0ff,
65                 .selector_reg = LT9611_PAGE_CONTROL,
66                 .selector_mask = 0xff,
67                 .selector_shift = 0,
68                 .window_start = 0,
69                 .window_len = 0x100,
70         },
71 };
72
73 static const struct regmap_config lt9611uxc_regmap_config = {
74         .reg_bits = 8,
75         .val_bits = 8,
76         .max_register = 0xffff,
77         .ranges = lt9611uxc_ranges,
78         .num_ranges = ARRAY_SIZE(lt9611uxc_ranges),
79 };
80
81 struct lt9611uxc_mode {
82         u16 hdisplay;
83         u16 vdisplay;
84         u8 vrefresh;
85 };
86
87 /*
88  * This chip supports only a fixed set of modes.
89  * Enumerate them here to check whether the mode is supported.
90  */
91 static struct lt9611uxc_mode lt9611uxc_modes[] = {
92         { 1920, 1080, 60 },
93         { 1920, 1080, 30 },
94         { 1920, 1080, 25 },
95         { 1366, 768, 60 },
96         { 1360, 768, 60 },
97         { 1280, 1024, 60 },
98         { 1280, 800, 60 },
99         { 1280, 720, 60 },
100         { 1280, 720, 50 },
101         { 1280, 720, 30 },
102         { 1152, 864, 60 },
103         { 1024, 768, 60 },
104         { 800, 600, 60 },
105         { 720, 576, 50 },
106         { 720, 480, 60 },
107         { 640, 480, 60 },
108 };
109
110 static struct lt9611uxc *bridge_to_lt9611uxc(struct drm_bridge *bridge)
111 {
112         return container_of(bridge, struct lt9611uxc, bridge);
113 }
114
115 static struct lt9611uxc *connector_to_lt9611uxc(struct drm_connector *connector)
116 {
117         return container_of(connector, struct lt9611uxc, connector);
118 }
119
120 static void lt9611uxc_lock(struct lt9611uxc *lt9611uxc)
121 {
122         mutex_lock(&lt9611uxc->ocm_lock);
123         regmap_write(lt9611uxc->regmap, 0x80ee, 0x01);
124 }
125
126 static void lt9611uxc_unlock(struct lt9611uxc *lt9611uxc)
127 {
128         regmap_write(lt9611uxc->regmap, 0x80ee, 0x00);
129         msleep(50);
130         mutex_unlock(&lt9611uxc->ocm_lock);
131 }
132
133 static irqreturn_t lt9611uxc_irq_thread_handler(int irq, void *dev_id)
134 {
135         struct lt9611uxc *lt9611uxc = dev_id;
136         unsigned int irq_status = 0;
137         unsigned int hpd_status = 0;
138
139         lt9611uxc_lock(lt9611uxc);
140
141         regmap_read(lt9611uxc->regmap, 0xb022, &irq_status);
142         regmap_read(lt9611uxc->regmap, 0xb023, &hpd_status);
143         if (irq_status)
144                 regmap_write(lt9611uxc->regmap, 0xb022, 0);
145
146         lt9611uxc_unlock(lt9611uxc);
147
148         if (irq_status & BIT(0))
149                 lt9611uxc->edid_read = !!(hpd_status & BIT(0));
150
151         if (irq_status & BIT(1)) {
152                 if (lt9611uxc->connector.dev)
153                         drm_kms_helper_hotplug_event(lt9611uxc->connector.dev);
154                 else
155                         drm_bridge_hpd_notify(&lt9611uxc->bridge, !!(hpd_status & BIT(1)));
156         }
157
158         return IRQ_HANDLED;
159 }
160
161 static void lt9611uxc_reset(struct lt9611uxc *lt9611uxc)
162 {
163         gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 1);
164         msleep(20);
165
166         gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 0);
167         msleep(20);
168
169         gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 1);
170         msleep(300);
171 }
172
173 static void lt9611uxc_assert_5v(struct lt9611uxc *lt9611uxc)
174 {
175         if (!lt9611uxc->enable_gpio)
176                 return;
177
178         gpiod_set_value_cansleep(lt9611uxc->enable_gpio, 1);
179         msleep(20);
180 }
181
182 static int lt9611uxc_regulator_init(struct lt9611uxc *lt9611uxc)
183 {
184         int ret;
185
186         lt9611uxc->supplies[0].supply = "vdd";
187         lt9611uxc->supplies[1].supply = "vcc";
188
189         ret = devm_regulator_bulk_get(lt9611uxc->dev, 2, lt9611uxc->supplies);
190         if (ret < 0)
191                 return ret;
192
193         return regulator_set_load(lt9611uxc->supplies[0].consumer, 200000);
194 }
195
196 static int lt9611uxc_regulator_enable(struct lt9611uxc *lt9611uxc)
197 {
198         int ret;
199
200         ret = regulator_enable(lt9611uxc->supplies[0].consumer);
201         if (ret < 0)
202                 return ret;
203
204         usleep_range(1000, 10000); /* 50000 according to dtsi */
205
206         ret = regulator_enable(lt9611uxc->supplies[1].consumer);
207         if (ret < 0) {
208                 regulator_disable(lt9611uxc->supplies[0].consumer);
209                 return ret;
210         }
211
212         return 0;
213 }
214
215 static struct lt9611uxc_mode *lt9611uxc_find_mode(const struct drm_display_mode *mode)
216 {
217         int i;
218
219         for (i = 0; i < ARRAY_SIZE(lt9611uxc_modes); i++) {
220                 if (lt9611uxc_modes[i].hdisplay == mode->hdisplay &&
221                     lt9611uxc_modes[i].vdisplay == mode->vdisplay &&
222                     lt9611uxc_modes[i].vrefresh == drm_mode_vrefresh(mode)) {
223                         return &lt9611uxc_modes[i];
224                 }
225         }
226
227         return NULL;
228 }
229
230 static struct mipi_dsi_device *lt9611uxc_attach_dsi(struct lt9611uxc *lt9611uxc,
231                                                     struct device_node *dsi_node)
232 {
233         const struct mipi_dsi_device_info info = { "lt9611uxc", 0, NULL };
234         struct mipi_dsi_device *dsi;
235         struct mipi_dsi_host *host;
236         int ret;
237
238         host = of_find_mipi_dsi_host_by_node(dsi_node);
239         if (!host) {
240                 dev_err(lt9611uxc->dev, "failed to find dsi host\n");
241                 return ERR_PTR(-EPROBE_DEFER);
242         }
243
244         dsi = mipi_dsi_device_register_full(host, &info);
245         if (IS_ERR(dsi)) {
246                 dev_err(lt9611uxc->dev, "failed to create dsi device\n");
247                 return dsi;
248         }
249
250         dsi->lanes = 4;
251         dsi->format = MIPI_DSI_FMT_RGB888;
252         dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
253                           MIPI_DSI_MODE_VIDEO_HSE;
254
255         ret = mipi_dsi_attach(dsi);
256         if (ret < 0) {
257                 dev_err(lt9611uxc->dev, "failed to attach dsi to host\n");
258                 mipi_dsi_device_unregister(dsi);
259                 return ERR_PTR(ret);
260         }
261
262         return dsi;
263 }
264
265 static int lt9611uxc_connector_get_modes(struct drm_connector *connector)
266 {
267         struct lt9611uxc *lt9611uxc = connector_to_lt9611uxc(connector);
268         unsigned int count;
269         struct edid *edid;
270
271         edid = lt9611uxc->bridge.funcs->get_edid(&lt9611uxc->bridge, connector);
272         drm_connector_update_edid_property(connector, edid);
273         count = drm_add_edid_modes(connector, edid);
274         kfree(edid);
275
276         return count;
277 }
278
279 static enum drm_connector_status lt9611uxc_connector_detect(struct drm_connector *connector,
280                                                             bool force)
281 {
282         struct lt9611uxc *lt9611uxc = connector_to_lt9611uxc(connector);
283
284         return lt9611uxc->bridge.funcs->detect(&lt9611uxc->bridge);
285 }
286
287 static enum drm_mode_status lt9611uxc_connector_mode_valid(struct drm_connector *connector,
288                                                            struct drm_display_mode *mode)
289 {
290         struct lt9611uxc_mode *lt9611uxc_mode = lt9611uxc_find_mode(mode);
291
292         return lt9611uxc_mode ? MODE_OK : MODE_BAD;
293 }
294
295 static const struct drm_connector_helper_funcs lt9611uxc_bridge_connector_helper_funcs = {
296         .get_modes = lt9611uxc_connector_get_modes,
297         .mode_valid = lt9611uxc_connector_mode_valid,
298 };
299
300 static const struct drm_connector_funcs lt9611uxc_bridge_connector_funcs = {
301         .fill_modes = drm_helper_probe_single_connector_modes,
302         .detect = lt9611uxc_connector_detect,
303         .destroy = drm_connector_cleanup,
304         .reset = drm_atomic_helper_connector_reset,
305         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
306         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
307 };
308
309 static int lt9611uxc_connector_init(struct drm_bridge *bridge, struct lt9611uxc *lt9611uxc)
310 {
311         int ret;
312
313         if (!bridge->encoder) {
314                 DRM_ERROR("Parent encoder object not found");
315                 return -ENODEV;
316         }
317
318         drm_connector_helper_add(&lt9611uxc->connector,
319                                  &lt9611uxc_bridge_connector_helper_funcs);
320         ret = drm_connector_init(bridge->dev, &lt9611uxc->connector,
321                                  &lt9611uxc_bridge_connector_funcs,
322                                  DRM_MODE_CONNECTOR_HDMIA);
323         if (ret) {
324                 DRM_ERROR("Failed to initialize connector with drm\n");
325                 return ret;
326         }
327
328         return drm_connector_attach_encoder(&lt9611uxc->connector, bridge->encoder);
329 }
330
331 static void lt9611uxc_bridge_detach(struct drm_bridge *bridge)
332 {
333         struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
334
335         if (lt9611uxc->dsi1) {
336                 mipi_dsi_detach(lt9611uxc->dsi1);
337                 mipi_dsi_device_unregister(lt9611uxc->dsi1);
338         }
339
340         mipi_dsi_detach(lt9611uxc->dsi0);
341         mipi_dsi_device_unregister(lt9611uxc->dsi0);
342 }
343
344 static int lt9611uxc_bridge_attach(struct drm_bridge *bridge,
345                                    enum drm_bridge_attach_flags flags)
346 {
347         struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
348         int ret;
349
350         if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
351                 ret = lt9611uxc_connector_init(bridge, lt9611uxc);
352                 if (ret < 0)
353                         return ret;
354         }
355
356         /* Attach primary DSI */
357         lt9611uxc->dsi0 = lt9611uxc_attach_dsi(lt9611uxc, lt9611uxc->dsi0_node);
358         if (IS_ERR(lt9611uxc->dsi0))
359                 return PTR_ERR(lt9611uxc->dsi0);
360
361         /* Attach secondary DSI, if specified */
362         if (lt9611uxc->dsi1_node) {
363                 lt9611uxc->dsi1 = lt9611uxc_attach_dsi(lt9611uxc, lt9611uxc->dsi1_node);
364                 if (IS_ERR(lt9611uxc->dsi1)) {
365                         ret = PTR_ERR(lt9611uxc->dsi1);
366                         goto err_unregister_dsi0;
367                 }
368         }
369
370         return 0;
371
372 err_unregister_dsi0:
373         mipi_dsi_detach(lt9611uxc->dsi0);
374         mipi_dsi_device_unregister(lt9611uxc->dsi0);
375
376         return ret;
377 }
378
379 static enum drm_mode_status
380 lt9611uxc_bridge_mode_valid(struct drm_bridge *bridge,
381                             const struct drm_display_info *info,
382                             const struct drm_display_mode *mode)
383 {
384         struct lt9611uxc_mode *lt9611uxc_mode;
385
386         lt9611uxc_mode = lt9611uxc_find_mode(mode);
387
388         return lt9611uxc_mode ? MODE_OK : MODE_BAD;
389 }
390
391 static void lt9611uxc_video_setup(struct lt9611uxc *lt9611uxc,
392                                   const struct drm_display_mode *mode)
393 {
394         u32 h_total, hactive, hsync_len, hfront_porch;
395         u32 v_total, vactive, vsync_len, vfront_porch;
396
397         h_total = mode->htotal;
398         v_total = mode->vtotal;
399
400         hactive = mode->hdisplay;
401         hsync_len = mode->hsync_end - mode->hsync_start;
402         hfront_porch = mode->hsync_start - mode->hdisplay;
403
404         vactive = mode->vdisplay;
405         vsync_len = mode->vsync_end - mode->vsync_start;
406         vfront_porch = mode->vsync_start - mode->vdisplay;
407
408         regmap_write(lt9611uxc->regmap, 0xd00d, (u8)(v_total / 256));
409         regmap_write(lt9611uxc->regmap, 0xd00e, (u8)(v_total % 256));
410
411         regmap_write(lt9611uxc->regmap, 0xd00f, (u8)(vactive / 256));
412         regmap_write(lt9611uxc->regmap, 0xd010, (u8)(vactive % 256));
413
414         regmap_write(lt9611uxc->regmap, 0xd011, (u8)(h_total / 256));
415         regmap_write(lt9611uxc->regmap, 0xd012, (u8)(h_total % 256));
416
417         regmap_write(lt9611uxc->regmap, 0xd013, (u8)(hactive / 256));
418         regmap_write(lt9611uxc->regmap, 0xd014, (u8)(hactive % 256));
419
420         regmap_write(lt9611uxc->regmap, 0xd015, (u8)(vsync_len % 256));
421
422         regmap_update_bits(lt9611uxc->regmap, 0xd016, 0xf, (u8)(hsync_len / 256));
423         regmap_write(lt9611uxc->regmap, 0xd017, (u8)(hsync_len % 256));
424
425         regmap_update_bits(lt9611uxc->regmap, 0xd018, 0xf, (u8)(vfront_porch / 256));
426         regmap_write(lt9611uxc->regmap, 0xd019, (u8)(vfront_porch % 256));
427
428         regmap_update_bits(lt9611uxc->regmap, 0xd01a, 0xf, (u8)(hfront_porch / 256));
429         regmap_write(lt9611uxc->regmap, 0xd01b, (u8)(hfront_porch % 256));
430 }
431
432 static void lt9611uxc_bridge_mode_set(struct drm_bridge *bridge,
433                                       const struct drm_display_mode *mode,
434                                       const struct drm_display_mode *adj_mode)
435 {
436         struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
437
438         lt9611uxc_lock(lt9611uxc);
439         lt9611uxc_video_setup(lt9611uxc, mode);
440         lt9611uxc_unlock(lt9611uxc);
441 }
442
443 static enum drm_connector_status lt9611uxc_bridge_detect(struct drm_bridge *bridge)
444 {
445         struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
446         unsigned int reg_val = 0;
447         int ret;
448         int connected = 1;
449
450         if (lt9611uxc->hpd_supported) {
451                 lt9611uxc_lock(lt9611uxc);
452                 ret = regmap_read(lt9611uxc->regmap, 0xb023, &reg_val);
453                 lt9611uxc_unlock(lt9611uxc);
454
455                 if (ret)
456                         dev_err(lt9611uxc->dev, "failed to read hpd status: %d\n", ret);
457                 else
458                         connected  = reg_val & BIT(1);
459         }
460
461         return connected ?  connector_status_connected :
462                                 connector_status_disconnected;
463 }
464
465 static int lt9611uxc_wait_for_edid(struct lt9611uxc *lt9611uxc)
466 {
467         return wait_event_interruptible_timeout(lt9611uxc->wq, lt9611uxc->edid_read,
468                         msecs_to_jiffies(100));
469 }
470
471 static int lt9611uxc_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
472 {
473         struct lt9611uxc *lt9611uxc = data;
474         int ret;
475
476         if (len > EDID_BLOCK_SIZE)
477                 return -EINVAL;
478
479         if (block >= EDID_NUM_BLOCKS)
480                 return -EINVAL;
481
482         lt9611uxc_lock(lt9611uxc);
483
484         regmap_write(lt9611uxc->regmap, 0xb00b, 0x10);
485
486         regmap_write(lt9611uxc->regmap, 0xb00a, block * EDID_BLOCK_SIZE);
487
488         ret = regmap_noinc_read(lt9611uxc->regmap, 0xb0b0, buf, len);
489         if (ret)
490                 dev_err(lt9611uxc->dev, "edid read failed: %d\n", ret);
491
492         lt9611uxc_unlock(lt9611uxc);
493
494         return 0;
495 };
496
497 static struct edid *lt9611uxc_bridge_get_edid(struct drm_bridge *bridge,
498                                               struct drm_connector *connector)
499 {
500         struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
501         int ret;
502
503         ret = lt9611uxc_wait_for_edid(lt9611uxc);
504         if (ret < 0) {
505                 dev_err(lt9611uxc->dev, "wait for EDID failed: %d\n", ret);
506                 return ERR_PTR(ret);
507         }
508
509         return drm_do_get_edid(connector, lt9611uxc_get_edid_block, lt9611uxc);
510 }
511
512 static const struct drm_bridge_funcs lt9611uxc_bridge_funcs = {
513         .attach = lt9611uxc_bridge_attach,
514         .detach = lt9611uxc_bridge_detach,
515         .mode_valid = lt9611uxc_bridge_mode_valid,
516         .mode_set = lt9611uxc_bridge_mode_set,
517         .detect = lt9611uxc_bridge_detect,
518         .get_edid = lt9611uxc_bridge_get_edid,
519 };
520
521 static int lt9611uxc_parse_dt(struct device *dev,
522                               struct lt9611uxc *lt9611uxc)
523 {
524         lt9611uxc->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1);
525         if (!lt9611uxc->dsi0_node) {
526                 dev_err(lt9611uxc->dev, "failed to get remote node for primary dsi\n");
527                 return -ENODEV;
528         }
529
530         lt9611uxc->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1);
531
532         return 0;
533 }
534
535 static int lt9611uxc_gpio_init(struct lt9611uxc *lt9611uxc)
536 {
537         struct device *dev = lt9611uxc->dev;
538
539         lt9611uxc->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
540         if (IS_ERR(lt9611uxc->reset_gpio)) {
541                 dev_err(dev, "failed to acquire reset gpio\n");
542                 return PTR_ERR(lt9611uxc->reset_gpio);
543         }
544
545         lt9611uxc->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
546         if (IS_ERR(lt9611uxc->enable_gpio)) {
547                 dev_err(dev, "failed to acquire enable gpio\n");
548                 return PTR_ERR(lt9611uxc->enable_gpio);
549         }
550
551         return 0;
552 }
553
554 static int lt9611uxc_read_device_rev(struct lt9611uxc *lt9611uxc)
555 {
556         unsigned int rev0, rev1, rev2;
557         int ret;
558
559         lt9611uxc_lock(lt9611uxc);
560
561         ret = regmap_read(lt9611uxc->regmap, 0x8100, &rev0);
562         ret |= regmap_read(lt9611uxc->regmap, 0x8101, &rev1);
563         ret |= regmap_read(lt9611uxc->regmap, 0x8102, &rev2);
564         if (ret)
565                 dev_err(lt9611uxc->dev, "failed to read revision: %d\n", ret);
566         else
567                 dev_info(lt9611uxc->dev, "LT9611 revision: 0x%02x.%02x.%02x\n", rev0, rev1, rev2);
568
569         lt9611uxc_unlock(lt9611uxc);
570
571         return ret;
572 }
573
574 static int lt9611uxc_read_version(struct lt9611uxc *lt9611uxc)
575 {
576         unsigned int rev;
577         int ret;
578
579         lt9611uxc_lock(lt9611uxc);
580
581         ret = regmap_read(lt9611uxc->regmap, 0xb021, &rev);
582         if (ret)
583                 dev_err(lt9611uxc->dev, "failed to read revision: %d\n", ret);
584         else
585                 dev_info(lt9611uxc->dev, "LT9611 version: 0x%02x\n", rev);
586
587         lt9611uxc_unlock(lt9611uxc);
588
589         return ret < 0 ? ret : rev;
590 }
591
592 static int lt9611uxc_hdmi_hw_params(struct device *dev, void *data,
593                                     struct hdmi_codec_daifmt *fmt,
594                                     struct hdmi_codec_params *hparms)
595 {
596         /*
597          * LT9611UXC will automatically detect rate and sample size, so no need
598          * to setup anything here.
599          */
600         return 0;
601 }
602
603 static void lt9611uxc_audio_shutdown(struct device *dev, void *data)
604 {
605 }
606
607 static int lt9611uxc_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
608                                          struct device_node *endpoint)
609 {
610         struct of_endpoint of_ep;
611         int ret;
612
613         ret = of_graph_parse_endpoint(endpoint, &of_ep);
614         if (ret < 0)
615                 return ret;
616
617         /*
618          * HDMI sound should be located as reg = <2>
619          * Then, it is sound port 0
620          */
621         if (of_ep.port == 2)
622                 return 0;
623
624         return -EINVAL;
625 }
626
627 static const struct hdmi_codec_ops lt9611uxc_codec_ops = {
628         .hw_params      = lt9611uxc_hdmi_hw_params,
629         .audio_shutdown = lt9611uxc_audio_shutdown,
630         .get_dai_id     = lt9611uxc_hdmi_i2s_get_dai_id,
631 };
632
633 static int lt9611uxc_audio_init(struct device *dev, struct lt9611uxc *lt9611uxc)
634 {
635         struct hdmi_codec_pdata codec_data = {
636                 .ops = &lt9611uxc_codec_ops,
637                 .max_i2s_channels = 2,
638                 .i2s = 1,
639                 .data = lt9611uxc,
640         };
641
642         lt9611uxc->audio_pdev =
643                 platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
644                                               PLATFORM_DEVID_AUTO,
645                                               &codec_data, sizeof(codec_data));
646
647         return PTR_ERR_OR_ZERO(lt9611uxc->audio_pdev);
648 }
649
650 static void lt9611uxc_audio_exit(struct lt9611uxc *lt9611uxc)
651 {
652         if (lt9611uxc->audio_pdev) {
653                 platform_device_unregister(lt9611uxc->audio_pdev);
654                 lt9611uxc->audio_pdev = NULL;
655         }
656 }
657
658 #define LT9611UXC_FW_PAGE_SIZE 32
659 static void lt9611uxc_firmware_write_page(struct lt9611uxc *lt9611uxc, u16 addr, const u8 *buf)
660 {
661         struct reg_sequence seq_write_prepare[] = {
662                 REG_SEQ0(0x805a, 0x04),
663                 REG_SEQ0(0x805a, 0x00),
664
665                 REG_SEQ0(0x805e, 0xdf),
666                 REG_SEQ0(0x805a, 0x20),
667                 REG_SEQ0(0x805a, 0x00),
668                 REG_SEQ0(0x8058, 0x21),
669         };
670
671         struct reg_sequence seq_write_addr[] = {
672                 REG_SEQ0(0x805b, (addr >> 16) & 0xff),
673                 REG_SEQ0(0x805c, (addr >> 8) & 0xff),
674                 REG_SEQ0(0x805d, addr & 0xff),
675                 REG_SEQ0(0x805a, 0x10),
676                 REG_SEQ0(0x805a, 0x00),
677         };
678
679         regmap_write(lt9611uxc->regmap, 0x8108, 0xbf);
680         msleep(20);
681         regmap_write(lt9611uxc->regmap, 0x8108, 0xff);
682         msleep(20);
683         regmap_multi_reg_write(lt9611uxc->regmap, seq_write_prepare, ARRAY_SIZE(seq_write_prepare));
684         regmap_noinc_write(lt9611uxc->regmap, 0x8059, buf, LT9611UXC_FW_PAGE_SIZE);
685         regmap_multi_reg_write(lt9611uxc->regmap, seq_write_addr, ARRAY_SIZE(seq_write_addr));
686         msleep(20);
687 }
688
689 static void lt9611uxc_firmware_read_page(struct lt9611uxc *lt9611uxc, u16 addr, char *buf)
690 {
691         struct reg_sequence seq_read_page[] = {
692                 REG_SEQ0(0x805a, 0xa0),
693                 REG_SEQ0(0x805a, 0x80),
694                 REG_SEQ0(0x805b, (addr >> 16) & 0xff),
695                 REG_SEQ0(0x805c, (addr >> 8) & 0xff),
696                 REG_SEQ0(0x805d, addr & 0xff),
697                 REG_SEQ0(0x805a, 0x90),
698                 REG_SEQ0(0x805a, 0x80),
699                 REG_SEQ0(0x8058, 0x21),
700         };
701
702         regmap_multi_reg_write(lt9611uxc->regmap, seq_read_page, ARRAY_SIZE(seq_read_page));
703         regmap_noinc_read(lt9611uxc->regmap, 0x805f, buf, LT9611UXC_FW_PAGE_SIZE);
704 }
705
706 static char *lt9611uxc_firmware_read(struct lt9611uxc *lt9611uxc, size_t size)
707 {
708         struct reg_sequence seq_read_setup[] = {
709                 REG_SEQ0(0x805a, 0x84),
710                 REG_SEQ0(0x805a, 0x80),
711         };
712
713         char *readbuf;
714         u16 offset;
715
716         readbuf = kzalloc(ALIGN(size, 32), GFP_KERNEL);
717         if (!readbuf)
718                 return NULL;
719
720         regmap_multi_reg_write(lt9611uxc->regmap, seq_read_setup, ARRAY_SIZE(seq_read_setup));
721
722         for (offset = 0;
723              offset < size;
724              offset += LT9611UXC_FW_PAGE_SIZE)
725                 lt9611uxc_firmware_read_page(lt9611uxc, offset, &readbuf[offset]);
726
727         return readbuf;
728 }
729
730 static int lt9611uxc_firmware_update(struct lt9611uxc *lt9611uxc)
731 {
732         int ret;
733         u16 offset;
734         size_t remain;
735         char *readbuf;
736         const struct firmware *fw;
737
738         struct reg_sequence seq_setup[] = {
739                 REG_SEQ0(0x805e, 0xdf),
740                 REG_SEQ0(0x8058, 0x00),
741                 REG_SEQ0(0x8059, 0x50),
742                 REG_SEQ0(0x805a, 0x10),
743                 REG_SEQ0(0x805a, 0x00),
744         };
745
746
747         struct reg_sequence seq_block_erase[] = {
748                 REG_SEQ0(0x805a, 0x04),
749                 REG_SEQ0(0x805a, 0x00),
750                 REG_SEQ0(0x805b, 0x00),
751                 REG_SEQ0(0x805c, 0x00),
752                 REG_SEQ0(0x805d, 0x00),
753                 REG_SEQ0(0x805a, 0x01),
754                 REG_SEQ0(0x805a, 0x00),
755         };
756
757         ret = request_firmware(&fw, "lt9611uxc_fw.bin", lt9611uxc->dev);
758         if (ret < 0)
759                 return ret;
760
761         dev_info(lt9611uxc->dev, "Updating firmware\n");
762         lt9611uxc_lock(lt9611uxc);
763
764         regmap_multi_reg_write(lt9611uxc->regmap, seq_setup, ARRAY_SIZE(seq_setup));
765
766         /*
767          * Need erase block 2 timess here. Sometimes, block erase can fail.
768          * This is a workaroud.
769          */
770         regmap_multi_reg_write(lt9611uxc->regmap, seq_block_erase, ARRAY_SIZE(seq_block_erase));
771         msleep(3000);
772         regmap_multi_reg_write(lt9611uxc->regmap, seq_block_erase, ARRAY_SIZE(seq_block_erase));
773         msleep(3000);
774
775         for (offset = 0, remain = fw->size;
776              remain >= LT9611UXC_FW_PAGE_SIZE;
777              offset += LT9611UXC_FW_PAGE_SIZE, remain -= LT9611UXC_FW_PAGE_SIZE)
778                 lt9611uxc_firmware_write_page(lt9611uxc, offset, fw->data + offset);
779
780         if (remain > 0) {
781                 char buf[LT9611UXC_FW_PAGE_SIZE];
782
783                 memset(buf, 0xff, LT9611UXC_FW_PAGE_SIZE);
784                 memcpy(buf, fw->data + offset, remain);
785                 lt9611uxc_firmware_write_page(lt9611uxc, offset, buf);
786         }
787         msleep(20);
788
789         readbuf = lt9611uxc_firmware_read(lt9611uxc, fw->size);
790         if (!readbuf) {
791                 ret = -ENOMEM;
792                 goto out;
793         }
794
795         if (!memcmp(readbuf, fw->data, fw->size)) {
796                 dev_err(lt9611uxc->dev, "Firmware update failed\n");
797                 print_hex_dump(KERN_ERR, "fw: ", DUMP_PREFIX_OFFSET, 16, 1, readbuf, fw->size, false);
798                 ret = -EINVAL;
799         } else {
800                 dev_info(lt9611uxc->dev, "Firmware updates successfully\n");
801                 ret = 0;
802         }
803         kfree(readbuf);
804
805 out:
806         lt9611uxc_unlock(lt9611uxc);
807         lt9611uxc_reset(lt9611uxc);
808         release_firmware(fw);
809
810         return ret;
811 }
812
813 static ssize_t lt9611uxc_firmware_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
814 {
815         struct lt9611uxc *lt9611uxc = dev_get_drvdata(dev);
816         int ret;
817
818         ret = lt9611uxc_firmware_update(lt9611uxc);
819         if (ret < 0)
820                 return ret;
821         return len;
822 }
823
824 static ssize_t lt9611uxc_firmware_show(struct device *dev, struct device_attribute *attr, char *buf)
825 {
826         struct lt9611uxc *lt9611uxc = dev_get_drvdata(dev);
827
828         return snprintf(buf, PAGE_SIZE, "%02x\n", lt9611uxc->fw_version);
829 }
830
831 static DEVICE_ATTR_RW(lt9611uxc_firmware);
832
833 static struct attribute *lt9611uxc_attrs[] = {
834         &dev_attr_lt9611uxc_firmware.attr,
835         NULL,
836 };
837
838 static const struct attribute_group lt9611uxc_attr_group = {
839         .attrs = lt9611uxc_attrs,
840 };
841
842 static const struct attribute_group *lt9611uxc_attr_groups[] = {
843         &lt9611uxc_attr_group,
844         NULL,
845 };
846
847 static int lt9611uxc_probe(struct i2c_client *client,
848                            const struct i2c_device_id *id)
849 {
850         struct lt9611uxc *lt9611uxc;
851         struct device *dev = &client->dev;
852         int ret;
853         bool fw_updated = false;
854
855         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
856                 dev_err(dev, "device doesn't support I2C\n");
857                 return -ENODEV;
858         }
859
860         lt9611uxc = devm_kzalloc(dev, sizeof(*lt9611uxc), GFP_KERNEL);
861         if (!lt9611uxc)
862                 return -ENOMEM;
863
864         lt9611uxc->dev = &client->dev;
865         lt9611uxc->client = client;
866         mutex_init(&lt9611uxc->ocm_lock);
867
868         lt9611uxc->regmap = devm_regmap_init_i2c(client, &lt9611uxc_regmap_config);
869         if (IS_ERR(lt9611uxc->regmap)) {
870                 dev_err(lt9611uxc->dev, "regmap i2c init failed\n");
871                 return PTR_ERR(lt9611uxc->regmap);
872         }
873
874         ret = lt9611uxc_parse_dt(&client->dev, lt9611uxc);
875         if (ret) {
876                 dev_err(dev, "failed to parse device tree\n");
877                 return ret;
878         }
879
880         ret = lt9611uxc_gpio_init(lt9611uxc);
881         if (ret < 0)
882                 goto err_of_put;
883
884         ret = lt9611uxc_regulator_init(lt9611uxc);
885         if (ret < 0)
886                 goto err_of_put;
887
888         lt9611uxc_assert_5v(lt9611uxc);
889
890         ret = lt9611uxc_regulator_enable(lt9611uxc);
891         if (ret)
892                 goto err_of_put;
893
894         lt9611uxc_reset(lt9611uxc);
895
896         ret = lt9611uxc_read_device_rev(lt9611uxc);
897         if (ret) {
898                 dev_err(dev, "failed to read chip rev\n");
899                 goto err_disable_regulators;
900         }
901
902 retry:
903         ret = lt9611uxc_read_version(lt9611uxc);
904         if (ret < 0) {
905                 dev_err(dev, "failed to read FW version\n");
906                 goto err_disable_regulators;
907         } else if (ret == 0) {
908                 if (!fw_updated) {
909                         fw_updated = true;
910                         dev_err(dev, "FW version 0, enforcing firmware update\n");
911                         ret = lt9611uxc_firmware_update(lt9611uxc);
912                         if (ret < 0)
913                                 goto err_disable_regulators;
914                         else
915                                 goto retry;
916                 } else {
917                         dev_err(dev, "FW version 0, update failed\n");
918                         ret = -EOPNOTSUPP;
919                         goto err_disable_regulators;
920                 }
921         } else if (ret < 0x40) {
922                 dev_info(dev, "FW version 0x%x, HPD not supported\n", ret);
923         } else {
924                 lt9611uxc->hpd_supported = true;
925         }
926         lt9611uxc->fw_version = ret;
927
928         init_waitqueue_head(&lt9611uxc->wq);
929         ret = devm_request_threaded_irq(dev, client->irq, NULL,
930                                         lt9611uxc_irq_thread_handler,
931                                         IRQF_ONESHOT, "lt9611uxc", lt9611uxc);
932         if (ret) {
933                 dev_err(dev, "failed to request irq\n");
934                 goto err_disable_regulators;
935         }
936
937         i2c_set_clientdata(client, lt9611uxc);
938
939         lt9611uxc->bridge.funcs = &lt9611uxc_bridge_funcs;
940         lt9611uxc->bridge.of_node = client->dev.of_node;
941         lt9611uxc->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID;
942         if (lt9611uxc->hpd_supported)
943                 lt9611uxc->bridge.ops |= DRM_BRIDGE_OP_HPD;
944         lt9611uxc->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
945
946         drm_bridge_add(&lt9611uxc->bridge);
947
948         return lt9611uxc_audio_init(dev, lt9611uxc);
949
950 err_disable_regulators:
951         regulator_bulk_disable(ARRAY_SIZE(lt9611uxc->supplies), lt9611uxc->supplies);
952
953 err_of_put:
954         of_node_put(lt9611uxc->dsi1_node);
955         of_node_put(lt9611uxc->dsi0_node);
956
957         return ret;
958 }
959
960 static int lt9611uxc_remove(struct i2c_client *client)
961 {
962         struct lt9611uxc *lt9611uxc = i2c_get_clientdata(client);
963
964         disable_irq(client->irq);
965         lt9611uxc_audio_exit(lt9611uxc);
966         drm_bridge_remove(&lt9611uxc->bridge);
967
968         mutex_destroy(&lt9611uxc->ocm_lock);
969
970         regulator_bulk_disable(ARRAY_SIZE(lt9611uxc->supplies), lt9611uxc->supplies);
971
972         of_node_put(lt9611uxc->dsi1_node);
973         of_node_put(lt9611uxc->dsi0_node);
974
975         return 0;
976 }
977
978 static struct i2c_device_id lt9611uxc_id[] = {
979         { "lontium,lt9611uxc", 0 },
980         { /* sentinel */ }
981 };
982
983 static const struct of_device_id lt9611uxc_match_table[] = {
984         { .compatible = "lontium,lt9611uxc" },
985         { /* sentinel */ }
986 };
987 MODULE_DEVICE_TABLE(of, lt9611uxc_match_table);
988
989 static struct i2c_driver lt9611uxc_driver = {
990         .driver = {
991                 .name = "lt9611uxc",
992                 .of_match_table = lt9611uxc_match_table,
993                 .dev_groups = lt9611uxc_attr_groups,
994         },
995         .probe = lt9611uxc_probe,
996         .remove = lt9611uxc_remove,
997         .id_table = lt9611uxc_id,
998 };
999 module_i2c_driver(lt9611uxc_driver);
1000
1001 MODULE_AUTHOR("Dmitry Baryshkov <dmitry.baryshkov@linaro.org>");
1002 MODULE_LICENSE("GPL v2");