]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/omapdrm/displays/panel-dsi-cm.c
drm/omap: Pass drm_display_mode to .check_timings() and .set_timings()
[linux.git] / drivers / gpu / drm / omapdrm / displays / panel-dsi-cm.c
1 /*
2  * Generic DSI Command Mode panel driver
3  *
4  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
5  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published by
9  * the Free Software Foundation.
10  */
11
12 /* #define DEBUG */
13
14 #include <linux/backlight.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/interrupt.h>
18 #include <linux/jiffies.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched/signal.h>
22 #include <linux/slab.h>
23 #include <linux/workqueue.h>
24 #include <linux/of_device.h>
25 #include <linux/regulator/consumer.h>
26
27 #include <drm/drm_connector.h>
28
29 #include <video/mipi_display.h>
30 #include <video/of_display_timing.h>
31
32 #include "../dss/omapdss.h"
33
34 /* DSI Virtual channel. Hardcoded for now. */
35 #define TCH 0
36
37 #define DCS_READ_NUM_ERRORS     0x05
38 #define DCS_BRIGHTNESS          0x51
39 #define DCS_CTRL_DISPLAY        0x53
40 #define DCS_GET_ID1             0xda
41 #define DCS_GET_ID2             0xdb
42 #define DCS_GET_ID3             0xdc
43
44 struct panel_drv_data {
45         struct omap_dss_device dssdev;
46         struct omap_dss_device *src;
47
48         struct videomode vm;
49
50         struct platform_device *pdev;
51
52         struct mutex lock;
53
54         struct backlight_device *bldev;
55         struct backlight_device *extbldev;
56
57         unsigned long   hw_guard_end;   /* next value of jiffies when we can
58                                          * issue the next sleep in/out command
59                                          */
60         unsigned long   hw_guard_wait;  /* max guard time in jiffies */
61
62         /* panel HW configuration from DT or platform data */
63         struct gpio_desc *reset_gpio;
64         struct gpio_desc *ext_te_gpio;
65
66         struct regulator *vpnl;
67         struct regulator *vddi;
68
69         bool use_dsi_backlight;
70
71         int width_mm;
72         int height_mm;
73
74         struct omap_dsi_pin_config pin_config;
75
76         /* runtime variables */
77         bool enabled;
78
79         bool te_enabled;
80
81         atomic_t do_update;
82         int channel;
83
84         struct delayed_work te_timeout_work;
85
86         bool intro_printed;
87
88         struct workqueue_struct *workqueue;
89
90         bool ulps_enabled;
91         unsigned int ulps_timeout;
92         struct delayed_work ulps_work;
93 };
94
95 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
96
97 static irqreturn_t dsicm_te_isr(int irq, void *data);
98 static void dsicm_te_timeout_work_callback(struct work_struct *work);
99 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable);
100
101 static int dsicm_panel_reset(struct panel_drv_data *ddata);
102
103 static void dsicm_ulps_work(struct work_struct *work);
104
105 static void dsicm_bl_power(struct panel_drv_data *ddata, bool enable)
106 {
107         struct backlight_device *backlight;
108
109         if (ddata->bldev)
110                 backlight = ddata->bldev;
111         else if (ddata->extbldev)
112                 backlight = ddata->extbldev;
113         else
114                 return;
115
116         if (enable) {
117                 backlight->props.fb_blank = FB_BLANK_UNBLANK;
118                 backlight->props.state = ~(BL_CORE_FBBLANK | BL_CORE_SUSPENDED);
119                 backlight->props.power = FB_BLANK_UNBLANK;
120         } else {
121                 backlight->props.fb_blank = FB_BLANK_NORMAL;
122                 backlight->props.power = FB_BLANK_POWERDOWN;
123                 backlight->props.state |= BL_CORE_FBBLANK | BL_CORE_SUSPENDED;
124         }
125
126         backlight_update_status(backlight);
127 }
128
129 static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec)
130 {
131         ddata->hw_guard_wait = msecs_to_jiffies(guard_msec);
132         ddata->hw_guard_end = jiffies + ddata->hw_guard_wait;
133 }
134
135 static void hw_guard_wait(struct panel_drv_data *ddata)
136 {
137         unsigned long wait = ddata->hw_guard_end - jiffies;
138
139         if ((long)wait > 0 && wait <= ddata->hw_guard_wait) {
140                 set_current_state(TASK_UNINTERRUPTIBLE);
141                 schedule_timeout(wait);
142         }
143 }
144
145 static int dsicm_dcs_read_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 *data)
146 {
147         struct omap_dss_device *src = ddata->src;
148         int r;
149         u8 buf[1];
150
151         r = src->ops->dsi.dcs_read(src, ddata->channel, dcs_cmd, buf, 1);
152
153         if (r < 0)
154                 return r;
155
156         *data = buf[0];
157
158         return 0;
159 }
160
161 static int dsicm_dcs_write_0(struct panel_drv_data *ddata, u8 dcs_cmd)
162 {
163         struct omap_dss_device *src = ddata->src;
164
165         return src->ops->dsi.dcs_write(src, ddata->channel, &dcs_cmd, 1);
166 }
167
168 static int dsicm_dcs_write_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 param)
169 {
170         struct omap_dss_device *src = ddata->src;
171         u8 buf[2] = { dcs_cmd, param };
172
173         return src->ops->dsi.dcs_write(src, ddata->channel, buf, 2);
174 }
175
176 static int dsicm_sleep_in(struct panel_drv_data *ddata)
177
178 {
179         struct omap_dss_device *src = ddata->src;
180         u8 cmd;
181         int r;
182
183         hw_guard_wait(ddata);
184
185         cmd = MIPI_DCS_ENTER_SLEEP_MODE;
186         r = src->ops->dsi.dcs_write_nosync(src, ddata->channel, &cmd, 1);
187         if (r)
188                 return r;
189
190         hw_guard_start(ddata, 120);
191
192         usleep_range(5000, 10000);
193
194         return 0;
195 }
196
197 static int dsicm_sleep_out(struct panel_drv_data *ddata)
198 {
199         int r;
200
201         hw_guard_wait(ddata);
202
203         r = dsicm_dcs_write_0(ddata, MIPI_DCS_EXIT_SLEEP_MODE);
204         if (r)
205                 return r;
206
207         hw_guard_start(ddata, 120);
208
209         usleep_range(5000, 10000);
210
211         return 0;
212 }
213
214 static int dsicm_get_id(struct panel_drv_data *ddata, u8 *id1, u8 *id2, u8 *id3)
215 {
216         int r;
217
218         r = dsicm_dcs_read_1(ddata, DCS_GET_ID1, id1);
219         if (r)
220                 return r;
221         r = dsicm_dcs_read_1(ddata, DCS_GET_ID2, id2);
222         if (r)
223                 return r;
224         r = dsicm_dcs_read_1(ddata, DCS_GET_ID3, id3);
225         if (r)
226                 return r;
227
228         return 0;
229 }
230
231 static int dsicm_set_update_window(struct panel_drv_data *ddata,
232                 u16 x, u16 y, u16 w, u16 h)
233 {
234         struct omap_dss_device *src = ddata->src;
235         int r;
236         u16 x1 = x;
237         u16 x2 = x + w - 1;
238         u16 y1 = y;
239         u16 y2 = y + h - 1;
240
241         u8 buf[5];
242         buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS;
243         buf[1] = (x1 >> 8) & 0xff;
244         buf[2] = (x1 >> 0) & 0xff;
245         buf[3] = (x2 >> 8) & 0xff;
246         buf[4] = (x2 >> 0) & 0xff;
247
248         r = src->ops->dsi.dcs_write_nosync(src, ddata->channel, buf, sizeof(buf));
249         if (r)
250                 return r;
251
252         buf[0] = MIPI_DCS_SET_PAGE_ADDRESS;
253         buf[1] = (y1 >> 8) & 0xff;
254         buf[2] = (y1 >> 0) & 0xff;
255         buf[3] = (y2 >> 8) & 0xff;
256         buf[4] = (y2 >> 0) & 0xff;
257
258         r = src->ops->dsi.dcs_write_nosync(src, ddata->channel, buf, sizeof(buf));
259         if (r)
260                 return r;
261
262         src->ops->dsi.bta_sync(src, ddata->channel);
263
264         return r;
265 }
266
267 static void dsicm_queue_ulps_work(struct panel_drv_data *ddata)
268 {
269         if (ddata->ulps_timeout > 0)
270                 queue_delayed_work(ddata->workqueue, &ddata->ulps_work,
271                                 msecs_to_jiffies(ddata->ulps_timeout));
272 }
273
274 static void dsicm_cancel_ulps_work(struct panel_drv_data *ddata)
275 {
276         cancel_delayed_work(&ddata->ulps_work);
277 }
278
279 static int dsicm_enter_ulps(struct panel_drv_data *ddata)
280 {
281         struct omap_dss_device *src = ddata->src;
282         int r;
283
284         if (ddata->ulps_enabled)
285                 return 0;
286
287         dsicm_cancel_ulps_work(ddata);
288
289         r = _dsicm_enable_te(ddata, false);
290         if (r)
291                 goto err;
292
293         if (ddata->ext_te_gpio)
294                 disable_irq(gpiod_to_irq(ddata->ext_te_gpio));
295
296         src->ops->dsi.disable(src, false, true);
297
298         ddata->ulps_enabled = true;
299
300         return 0;
301
302 err:
303         dev_err(&ddata->pdev->dev, "enter ULPS failed");
304         dsicm_panel_reset(ddata);
305
306         ddata->ulps_enabled = false;
307
308         dsicm_queue_ulps_work(ddata);
309
310         return r;
311 }
312
313 static int dsicm_exit_ulps(struct panel_drv_data *ddata)
314 {
315         struct omap_dss_device *src = ddata->src;
316         int r;
317
318         if (!ddata->ulps_enabled)
319                 return 0;
320
321         src->ops->enable(src);
322         src->ops->dsi.enable_hs(src, ddata->channel, true);
323
324         r = _dsicm_enable_te(ddata, true);
325         if (r) {
326                 dev_err(&ddata->pdev->dev, "failed to re-enable TE");
327                 goto err2;
328         }
329
330         if (ddata->ext_te_gpio)
331                 enable_irq(gpiod_to_irq(ddata->ext_te_gpio));
332
333         dsicm_queue_ulps_work(ddata);
334
335         ddata->ulps_enabled = false;
336
337         return 0;
338
339 err2:
340         dev_err(&ddata->pdev->dev, "failed to exit ULPS");
341
342         r = dsicm_panel_reset(ddata);
343         if (!r) {
344                 if (ddata->ext_te_gpio)
345                         enable_irq(gpiod_to_irq(ddata->ext_te_gpio));
346                 ddata->ulps_enabled = false;
347         }
348
349         dsicm_queue_ulps_work(ddata);
350
351         return r;
352 }
353
354 static int dsicm_wake_up(struct panel_drv_data *ddata)
355 {
356         if (ddata->ulps_enabled)
357                 return dsicm_exit_ulps(ddata);
358
359         dsicm_cancel_ulps_work(ddata);
360         dsicm_queue_ulps_work(ddata);
361         return 0;
362 }
363
364 static int dsicm_bl_update_status(struct backlight_device *dev)
365 {
366         struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
367         struct omap_dss_device *src = ddata->src;
368         int r = 0;
369         int level;
370
371         if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
372                         dev->props.power == FB_BLANK_UNBLANK)
373                 level = dev->props.brightness;
374         else
375                 level = 0;
376
377         dev_dbg(&ddata->pdev->dev, "update brightness to %d\n", level);
378
379         mutex_lock(&ddata->lock);
380
381         if (ddata->enabled) {
382                 src->ops->dsi.bus_lock(src);
383
384                 r = dsicm_wake_up(ddata);
385                 if (!r)
386                         r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, level);
387
388                 src->ops->dsi.bus_unlock(src);
389         }
390
391         mutex_unlock(&ddata->lock);
392
393         return r;
394 }
395
396 static int dsicm_bl_get_intensity(struct backlight_device *dev)
397 {
398         if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
399                         dev->props.power == FB_BLANK_UNBLANK)
400                 return dev->props.brightness;
401
402         return 0;
403 }
404
405 static const struct backlight_ops dsicm_bl_ops = {
406         .get_brightness = dsicm_bl_get_intensity,
407         .update_status  = dsicm_bl_update_status,
408 };
409
410 static ssize_t dsicm_num_errors_show(struct device *dev,
411                 struct device_attribute *attr, char *buf)
412 {
413         struct platform_device *pdev = to_platform_device(dev);
414         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
415         struct omap_dss_device *src = ddata->src;
416         u8 errors = 0;
417         int r;
418
419         mutex_lock(&ddata->lock);
420
421         if (ddata->enabled) {
422                 src->ops->dsi.bus_lock(src);
423
424                 r = dsicm_wake_up(ddata);
425                 if (!r)
426                         r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS,
427                                         &errors);
428
429                 src->ops->dsi.bus_unlock(src);
430         } else {
431                 r = -ENODEV;
432         }
433
434         mutex_unlock(&ddata->lock);
435
436         if (r)
437                 return r;
438
439         return snprintf(buf, PAGE_SIZE, "%d\n", errors);
440 }
441
442 static ssize_t dsicm_hw_revision_show(struct device *dev,
443                 struct device_attribute *attr, char *buf)
444 {
445         struct platform_device *pdev = to_platform_device(dev);
446         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
447         struct omap_dss_device *src = ddata->src;
448         u8 id1, id2, id3;
449         int r;
450
451         mutex_lock(&ddata->lock);
452
453         if (ddata->enabled) {
454                 src->ops->dsi.bus_lock(src);
455
456                 r = dsicm_wake_up(ddata);
457                 if (!r)
458                         r = dsicm_get_id(ddata, &id1, &id2, &id3);
459
460                 src->ops->dsi.bus_unlock(src);
461         } else {
462                 r = -ENODEV;
463         }
464
465         mutex_unlock(&ddata->lock);
466
467         if (r)
468                 return r;
469
470         return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
471 }
472
473 static ssize_t dsicm_store_ulps(struct device *dev,
474                 struct device_attribute *attr,
475                 const char *buf, size_t count)
476 {
477         struct platform_device *pdev = to_platform_device(dev);
478         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
479         struct omap_dss_device *src = ddata->src;
480         unsigned long t;
481         int r;
482
483         r = kstrtoul(buf, 0, &t);
484         if (r)
485                 return r;
486
487         mutex_lock(&ddata->lock);
488
489         if (ddata->enabled) {
490                 src->ops->dsi.bus_lock(src);
491
492                 if (t)
493                         r = dsicm_enter_ulps(ddata);
494                 else
495                         r = dsicm_wake_up(ddata);
496
497                 src->ops->dsi.bus_unlock(src);
498         }
499
500         mutex_unlock(&ddata->lock);
501
502         if (r)
503                 return r;
504
505         return count;
506 }
507
508 static ssize_t dsicm_show_ulps(struct device *dev,
509                 struct device_attribute *attr,
510                 char *buf)
511 {
512         struct platform_device *pdev = to_platform_device(dev);
513         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
514         unsigned int t;
515
516         mutex_lock(&ddata->lock);
517         t = ddata->ulps_enabled;
518         mutex_unlock(&ddata->lock);
519
520         return snprintf(buf, PAGE_SIZE, "%u\n", t);
521 }
522
523 static ssize_t dsicm_store_ulps_timeout(struct device *dev,
524                 struct device_attribute *attr,
525                 const char *buf, size_t count)
526 {
527         struct platform_device *pdev = to_platform_device(dev);
528         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
529         struct omap_dss_device *src = ddata->src;
530         unsigned long t;
531         int r;
532
533         r = kstrtoul(buf, 0, &t);
534         if (r)
535                 return r;
536
537         mutex_lock(&ddata->lock);
538         ddata->ulps_timeout = t;
539
540         if (ddata->enabled) {
541                 /* dsicm_wake_up will restart the timer */
542                 src->ops->dsi.bus_lock(src);
543                 r = dsicm_wake_up(ddata);
544                 src->ops->dsi.bus_unlock(src);
545         }
546
547         mutex_unlock(&ddata->lock);
548
549         if (r)
550                 return r;
551
552         return count;
553 }
554
555 static ssize_t dsicm_show_ulps_timeout(struct device *dev,
556                 struct device_attribute *attr,
557                 char *buf)
558 {
559         struct platform_device *pdev = to_platform_device(dev);
560         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
561         unsigned int t;
562
563         mutex_lock(&ddata->lock);
564         t = ddata->ulps_timeout;
565         mutex_unlock(&ddata->lock);
566
567         return snprintf(buf, PAGE_SIZE, "%u\n", t);
568 }
569
570 static DEVICE_ATTR(num_dsi_errors, S_IRUGO, dsicm_num_errors_show, NULL);
571 static DEVICE_ATTR(hw_revision, S_IRUGO, dsicm_hw_revision_show, NULL);
572 static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
573                 dsicm_show_ulps, dsicm_store_ulps);
574 static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
575                 dsicm_show_ulps_timeout, dsicm_store_ulps_timeout);
576
577 static struct attribute *dsicm_attrs[] = {
578         &dev_attr_num_dsi_errors.attr,
579         &dev_attr_hw_revision.attr,
580         &dev_attr_ulps.attr,
581         &dev_attr_ulps_timeout.attr,
582         NULL,
583 };
584
585 static const struct attribute_group dsicm_attr_group = {
586         .attrs = dsicm_attrs,
587 };
588
589 static void dsicm_hw_reset(struct panel_drv_data *ddata)
590 {
591         gpiod_set_value(ddata->reset_gpio, 1);
592         udelay(10);
593         /* reset the panel */
594         gpiod_set_value(ddata->reset_gpio, 0);
595         /* assert reset */
596         udelay(10);
597         gpiod_set_value(ddata->reset_gpio, 1);
598         /* wait after releasing reset */
599         usleep_range(5000, 10000);
600 }
601
602 static int dsicm_power_on(struct panel_drv_data *ddata)
603 {
604         struct omap_dss_device *src = ddata->src;
605         u8 id1, id2, id3;
606         int r;
607         struct omap_dss_dsi_config dsi_config = {
608                 .mode = OMAP_DSS_DSI_CMD_MODE,
609                 .pixel_format = OMAP_DSS_DSI_FMT_RGB888,
610                 .vm = &ddata->vm,
611                 .hs_clk_min = 150000000,
612                 .hs_clk_max = 300000000,
613                 .lp_clk_min = 7000000,
614                 .lp_clk_max = 10000000,
615         };
616
617         if (ddata->vpnl) {
618                 r = regulator_enable(ddata->vpnl);
619                 if (r) {
620                         dev_err(&ddata->pdev->dev,
621                                 "failed to enable VPNL: %d\n", r);
622                         return r;
623                 }
624         }
625
626         if (ddata->vddi) {
627                 r = regulator_enable(ddata->vddi);
628                 if (r) {
629                         dev_err(&ddata->pdev->dev,
630                                 "failed to enable VDDI: %d\n", r);
631                         goto err_vpnl;
632                 }
633         }
634
635         if (ddata->pin_config.num_pins > 0) {
636                 r = src->ops->dsi.configure_pins(src, &ddata->pin_config);
637                 if (r) {
638                         dev_err(&ddata->pdev->dev,
639                                 "failed to configure DSI pins\n");
640                         goto err_vddi;
641                 }
642         }
643
644         r = src->ops->dsi.set_config(src, &dsi_config);
645         if (r) {
646                 dev_err(&ddata->pdev->dev, "failed to configure DSI\n");
647                 goto err_vddi;
648         }
649
650         src->ops->enable(src);
651
652         dsicm_hw_reset(ddata);
653
654         src->ops->dsi.enable_hs(src, ddata->channel, false);
655
656         r = dsicm_sleep_out(ddata);
657         if (r)
658                 goto err;
659
660         r = dsicm_get_id(ddata, &id1, &id2, &id3);
661         if (r)
662                 goto err;
663
664         r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff);
665         if (r)
666                 goto err;
667
668         r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY,
669                         (1<<2) | (1<<5));       /* BL | BCTRL */
670         if (r)
671                 goto err;
672
673         r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT,
674                 MIPI_DCS_PIXEL_FMT_24BIT);
675         if (r)
676                 goto err;
677
678         r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON);
679         if (r)
680                 goto err;
681
682         r = _dsicm_enable_te(ddata, ddata->te_enabled);
683         if (r)
684                 goto err;
685
686         r = src->ops->dsi.enable_video_output(src, ddata->channel);
687         if (r)
688                 goto err;
689
690         ddata->enabled = 1;
691
692         if (!ddata->intro_printed) {
693                 dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n",
694                         id1, id2, id3);
695                 ddata->intro_printed = true;
696         }
697
698         src->ops->dsi.enable_hs(src, ddata->channel, true);
699
700         return 0;
701 err:
702         dev_err(&ddata->pdev->dev, "error while enabling panel, issuing HW reset\n");
703
704         dsicm_hw_reset(ddata);
705
706         src->ops->dsi.disable(src, true, false);
707 err_vddi:
708         if (ddata->vddi)
709                 regulator_disable(ddata->vddi);
710 err_vpnl:
711         if (ddata->vpnl)
712                 regulator_disable(ddata->vpnl);
713
714         return r;
715 }
716
717 static void dsicm_power_off(struct panel_drv_data *ddata)
718 {
719         struct omap_dss_device *src = ddata->src;
720         int r;
721
722         src->ops->dsi.disable_video_output(src, ddata->channel);
723
724         r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF);
725         if (!r)
726                 r = dsicm_sleep_in(ddata);
727
728         if (r) {
729                 dev_err(&ddata->pdev->dev,
730                                 "error disabling panel, issuing HW reset\n");
731                 dsicm_hw_reset(ddata);
732         }
733
734         src->ops->dsi.disable(src, true, false);
735
736         if (ddata->vddi)
737                 regulator_disable(ddata->vddi);
738         if (ddata->vpnl)
739                 regulator_disable(ddata->vpnl);
740
741         ddata->enabled = 0;
742 }
743
744 static int dsicm_panel_reset(struct panel_drv_data *ddata)
745 {
746         dev_err(&ddata->pdev->dev, "performing LCD reset\n");
747
748         dsicm_power_off(ddata);
749         dsicm_hw_reset(ddata);
750         return dsicm_power_on(ddata);
751 }
752
753 static int dsicm_connect(struct omap_dss_device *src,
754                          struct omap_dss_device *dst)
755 {
756         struct panel_drv_data *ddata = to_panel_data(dst);
757         struct device *dev = &ddata->pdev->dev;
758         int r;
759
760         r = src->ops->dsi.request_vc(src, &ddata->channel);
761         if (r) {
762                 dev_err(dev, "failed to get virtual channel\n");
763                 return r;
764         }
765
766         r = src->ops->dsi.set_vc_id(src, ddata->channel, TCH);
767         if (r) {
768                 dev_err(dev, "failed to set VC_ID\n");
769                 src->ops->dsi.release_vc(src, ddata->channel);
770                 return r;
771         }
772
773         ddata->src = src;
774         return 0;
775 }
776
777 static void dsicm_disconnect(struct omap_dss_device *src,
778                              struct omap_dss_device *dst)
779 {
780         struct panel_drv_data *ddata = to_panel_data(dst);
781
782         src->ops->dsi.release_vc(src, ddata->channel);
783         ddata->src = NULL;
784 }
785
786 static void dsicm_enable(struct omap_dss_device *dssdev)
787 {
788         struct panel_drv_data *ddata = to_panel_data(dssdev);
789         struct omap_dss_device *src = ddata->src;
790         int r;
791
792         mutex_lock(&ddata->lock);
793
794         src->ops->dsi.bus_lock(src);
795
796         r = dsicm_power_on(ddata);
797
798         src->ops->dsi.bus_unlock(src);
799
800         if (r)
801                 goto err;
802
803         mutex_unlock(&ddata->lock);
804
805         dsicm_bl_power(ddata, true);
806
807         return;
808 err:
809         dev_dbg(&ddata->pdev->dev, "enable failed (%d)\n", r);
810         mutex_unlock(&ddata->lock);
811 }
812
813 static void dsicm_disable(struct omap_dss_device *dssdev)
814 {
815         struct panel_drv_data *ddata = to_panel_data(dssdev);
816         struct omap_dss_device *src = ddata->src;
817         int r;
818
819         dsicm_bl_power(ddata, false);
820
821         mutex_lock(&ddata->lock);
822
823         dsicm_cancel_ulps_work(ddata);
824
825         src->ops->dsi.bus_lock(src);
826
827         r = dsicm_wake_up(ddata);
828         if (!r)
829                 dsicm_power_off(ddata);
830
831         src->ops->dsi.bus_unlock(src);
832
833         mutex_unlock(&ddata->lock);
834 }
835
836 static void dsicm_framedone_cb(int err, void *data)
837 {
838         struct panel_drv_data *ddata = data;
839         struct omap_dss_device *src = ddata->src;
840
841         dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err);
842         src->ops->dsi.bus_unlock(src);
843 }
844
845 static irqreturn_t dsicm_te_isr(int irq, void *data)
846 {
847         struct panel_drv_data *ddata = data;
848         struct omap_dss_device *src = ddata->src;
849         int old;
850         int r;
851
852         old = atomic_cmpxchg(&ddata->do_update, 1, 0);
853
854         if (old) {
855                 cancel_delayed_work(&ddata->te_timeout_work);
856
857                 r = src->ops->dsi.update(src, ddata->channel, dsicm_framedone_cb,
858                                 ddata);
859                 if (r)
860                         goto err;
861         }
862
863         return IRQ_HANDLED;
864 err:
865         dev_err(&ddata->pdev->dev, "start update failed\n");
866         src->ops->dsi.bus_unlock(src);
867         return IRQ_HANDLED;
868 }
869
870 static void dsicm_te_timeout_work_callback(struct work_struct *work)
871 {
872         struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
873                                         te_timeout_work.work);
874         struct omap_dss_device *src = ddata->src;
875
876         dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n");
877
878         atomic_set(&ddata->do_update, 0);
879         src->ops->dsi.bus_unlock(src);
880 }
881
882 static int dsicm_update(struct omap_dss_device *dssdev,
883                                     u16 x, u16 y, u16 w, u16 h)
884 {
885         struct panel_drv_data *ddata = to_panel_data(dssdev);
886         struct omap_dss_device *src = ddata->src;
887         int r;
888
889         dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
890
891         mutex_lock(&ddata->lock);
892         src->ops->dsi.bus_lock(src);
893
894         r = dsicm_wake_up(ddata);
895         if (r)
896                 goto err;
897
898         if (!ddata->enabled) {
899                 r = 0;
900                 goto err;
901         }
902
903         /* XXX no need to send this every frame, but dsi break if not done */
904         r = dsicm_set_update_window(ddata, 0, 0, ddata->vm.hactive,
905                                     ddata->vm.vactive);
906         if (r)
907                 goto err;
908
909         if (ddata->te_enabled && ddata->ext_te_gpio) {
910                 schedule_delayed_work(&ddata->te_timeout_work,
911                                 msecs_to_jiffies(250));
912                 atomic_set(&ddata->do_update, 1);
913         } else {
914                 r = src->ops->dsi.update(src, ddata->channel, dsicm_framedone_cb,
915                                 ddata);
916                 if (r)
917                         goto err;
918         }
919
920         /* note: no bus_unlock here. unlock is src framedone_cb */
921         mutex_unlock(&ddata->lock);
922         return 0;
923 err:
924         src->ops->dsi.bus_unlock(src);
925         mutex_unlock(&ddata->lock);
926         return r;
927 }
928
929 static int dsicm_sync(struct omap_dss_device *dssdev)
930 {
931         struct panel_drv_data *ddata = to_panel_data(dssdev);
932         struct omap_dss_device *src = ddata->src;
933
934         dev_dbg(&ddata->pdev->dev, "sync\n");
935
936         mutex_lock(&ddata->lock);
937         src->ops->dsi.bus_lock(src);
938         src->ops->dsi.bus_unlock(src);
939         mutex_unlock(&ddata->lock);
940
941         dev_dbg(&ddata->pdev->dev, "sync done\n");
942
943         return 0;
944 }
945
946 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable)
947 {
948         struct omap_dss_device *src = ddata->src;
949         int r;
950
951         if (enable)
952                 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0);
953         else
954                 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF);
955
956         if (!ddata->ext_te_gpio)
957                 src->ops->dsi.enable_te(src, enable);
958
959         /* possible panel bug */
960         msleep(100);
961
962         return r;
963 }
964
965 static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable)
966 {
967         struct panel_drv_data *ddata = to_panel_data(dssdev);
968         struct omap_dss_device *src = ddata->src;
969         int r;
970
971         mutex_lock(&ddata->lock);
972
973         if (ddata->te_enabled == enable)
974                 goto end;
975
976         src->ops->dsi.bus_lock(src);
977
978         if (ddata->enabled) {
979                 r = dsicm_wake_up(ddata);
980                 if (r)
981                         goto err;
982
983                 r = _dsicm_enable_te(ddata, enable);
984                 if (r)
985                         goto err;
986         }
987
988         ddata->te_enabled = enable;
989
990         src->ops->dsi.bus_unlock(src);
991 end:
992         mutex_unlock(&ddata->lock);
993
994         return 0;
995 err:
996         src->ops->dsi.bus_unlock(src);
997         mutex_unlock(&ddata->lock);
998
999         return r;
1000 }
1001
1002 static int dsicm_get_te(struct omap_dss_device *dssdev)
1003 {
1004         struct panel_drv_data *ddata = to_panel_data(dssdev);
1005         int r;
1006
1007         mutex_lock(&ddata->lock);
1008         r = ddata->te_enabled;
1009         mutex_unlock(&ddata->lock);
1010
1011         return r;
1012 }
1013
1014 static int dsicm_memory_read(struct omap_dss_device *dssdev,
1015                 void *buf, size_t size,
1016                 u16 x, u16 y, u16 w, u16 h)
1017 {
1018         struct panel_drv_data *ddata = to_panel_data(dssdev);
1019         struct omap_dss_device *src = ddata->src;
1020         int r;
1021         int first = 1;
1022         int plen;
1023         unsigned int buf_used = 0;
1024
1025         if (size < w * h * 3)
1026                 return -ENOMEM;
1027
1028         mutex_lock(&ddata->lock);
1029
1030         if (!ddata->enabled) {
1031                 r = -ENODEV;
1032                 goto err1;
1033         }
1034
1035         size = min((u32)w * h * 3,
1036                    ddata->vm.hactive * ddata->vm.vactive * 3);
1037
1038         src->ops->dsi.bus_lock(src);
1039
1040         r = dsicm_wake_up(ddata);
1041         if (r)
1042                 goto err2;
1043
1044         /* plen 1 or 2 goes into short packet. until checksum error is fixed,
1045          * use short packets. plen 32 works, but bigger packets seem to cause
1046          * an error. */
1047         if (size % 2)
1048                 plen = 1;
1049         else
1050                 plen = 2;
1051
1052         dsicm_set_update_window(ddata, x, y, w, h);
1053
1054         r = src->ops->dsi.set_max_rx_packet_size(src, ddata->channel, plen);
1055         if (r)
1056                 goto err2;
1057
1058         while (buf_used < size) {
1059                 u8 dcs_cmd = first ? 0x2e : 0x3e;
1060                 first = 0;
1061
1062                 r = src->ops->dsi.dcs_read(src, ddata->channel, dcs_cmd,
1063                                 buf + buf_used, size - buf_used);
1064
1065                 if (r < 0) {
1066                         dev_err(dssdev->dev, "read error\n");
1067                         goto err3;
1068                 }
1069
1070                 buf_used += r;
1071
1072                 if (r < plen) {
1073                         dev_err(&ddata->pdev->dev, "short read\n");
1074                         break;
1075                 }
1076
1077                 if (signal_pending(current)) {
1078                         dev_err(&ddata->pdev->dev, "signal pending, "
1079                                         "aborting memory read\n");
1080                         r = -ERESTARTSYS;
1081                         goto err3;
1082                 }
1083         }
1084
1085         r = buf_used;
1086
1087 err3:
1088         src->ops->dsi.set_max_rx_packet_size(src, ddata->channel, 1);
1089 err2:
1090         src->ops->dsi.bus_unlock(src);
1091 err1:
1092         mutex_unlock(&ddata->lock);
1093         return r;
1094 }
1095
1096 static void dsicm_ulps_work(struct work_struct *work)
1097 {
1098         struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
1099                         ulps_work.work);
1100         struct omap_dss_device *dssdev = &ddata->dssdev;
1101         struct omap_dss_device *src = ddata->src;
1102
1103         mutex_lock(&ddata->lock);
1104
1105         if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) {
1106                 mutex_unlock(&ddata->lock);
1107                 return;
1108         }
1109
1110         src->ops->dsi.bus_lock(src);
1111
1112         dsicm_enter_ulps(ddata);
1113
1114         src->ops->dsi.bus_unlock(src);
1115         mutex_unlock(&ddata->lock);
1116 }
1117
1118 static int dsicm_get_modes(struct omap_dss_device *dssdev,
1119                            struct drm_connector *connector)
1120 {
1121         struct panel_drv_data *ddata = to_panel_data(dssdev);
1122
1123         connector->display_info.width_mm = ddata->width_mm;
1124         connector->display_info.height_mm = ddata->height_mm;
1125
1126         return omapdss_display_get_modes(connector, &ddata->vm);
1127 }
1128
1129 static int dsicm_check_timings(struct omap_dss_device *dssdev,
1130                                struct drm_display_mode *mode)
1131 {
1132         struct panel_drv_data *ddata = to_panel_data(dssdev);
1133         int ret = 0;
1134
1135         if (mode->hdisplay != ddata->vm.hactive)
1136                 ret = -EINVAL;
1137
1138         if (mode->vdisplay != ddata->vm.vactive)
1139                 ret = -EINVAL;
1140
1141         if (ret) {
1142                 dev_warn(dssdev->dev, "wrong resolution: %d x %d",
1143                          mode->hdisplay, mode->vdisplay);
1144                 dev_warn(dssdev->dev, "panel resolution: %d x %d",
1145                          ddata->vm.hactive, ddata->vm.vactive);
1146         }
1147
1148         return ret;
1149 }
1150
1151 static const struct omap_dss_device_ops dsicm_ops = {
1152         .connect        = dsicm_connect,
1153         .disconnect     = dsicm_disconnect,
1154
1155         .enable         = dsicm_enable,
1156         .disable        = dsicm_disable,
1157
1158         .get_modes      = dsicm_get_modes,
1159         .check_timings  = dsicm_check_timings,
1160 };
1161
1162 static const struct omap_dss_driver dsicm_dss_driver = {
1163         .update         = dsicm_update,
1164         .sync           = dsicm_sync,
1165
1166         .enable_te      = dsicm_enable_te,
1167         .get_te         = dsicm_get_te,
1168
1169         .memory_read    = dsicm_memory_read,
1170 };
1171
1172 static int dsicm_probe_of(struct platform_device *pdev)
1173 {
1174         struct device_node *node = pdev->dev.of_node;
1175         struct device_node *backlight;
1176         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1177         struct display_timing timing;
1178         int err;
1179
1180         ddata->reset_gpio = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
1181         if (IS_ERR(ddata->reset_gpio)) {
1182                 err = PTR_ERR(ddata->reset_gpio);
1183                 dev_err(&pdev->dev, "reset gpio request failed: %d", err);
1184                 return err;
1185         }
1186
1187         ddata->ext_te_gpio = devm_gpiod_get_optional(&pdev->dev, "te",
1188                                                      GPIOD_IN);
1189         if (IS_ERR(ddata->ext_te_gpio)) {
1190                 err = PTR_ERR(ddata->ext_te_gpio);
1191                 dev_err(&pdev->dev, "TE gpio request failed: %d", err);
1192                 return err;
1193         }
1194
1195         err = of_get_display_timing(node, "panel-timing", &timing);
1196         if (!err) {
1197                 videomode_from_timing(&timing, &ddata->vm);
1198                 if (!ddata->vm.pixelclock)
1199                         ddata->vm.pixelclock =
1200                                 ddata->vm.hactive * ddata->vm.vactive * 60;
1201         } else {
1202                 dev_warn(&pdev->dev,
1203                          "failed to get video timing, using defaults\n");
1204         }
1205
1206         ddata->width_mm = 0;
1207         of_property_read_u32(node, "width-mm", &ddata->width_mm);
1208
1209         ddata->height_mm = 0;
1210         of_property_read_u32(node, "height-mm", &ddata->height_mm);
1211
1212         ddata->vpnl = devm_regulator_get_optional(&pdev->dev, "vpnl");
1213         if (IS_ERR(ddata->vpnl)) {
1214                 err = PTR_ERR(ddata->vpnl);
1215                 if (err == -EPROBE_DEFER)
1216                         return err;
1217                 ddata->vpnl = NULL;
1218         }
1219
1220         ddata->vddi = devm_regulator_get_optional(&pdev->dev, "vddi");
1221         if (IS_ERR(ddata->vddi)) {
1222                 err = PTR_ERR(ddata->vddi);
1223                 if (err == -EPROBE_DEFER)
1224                         return err;
1225                 ddata->vddi = NULL;
1226         }
1227
1228         backlight = of_parse_phandle(node, "backlight", 0);
1229         if (backlight) {
1230                 ddata->extbldev = of_find_backlight_by_node(backlight);
1231                 of_node_put(backlight);
1232
1233                 if (!ddata->extbldev)
1234                         return -EPROBE_DEFER;
1235         } else {
1236                 /* assume native backlight support */
1237                 ddata->use_dsi_backlight = true;
1238         }
1239
1240         /* TODO: ulps */
1241
1242         return 0;
1243 }
1244
1245 static int dsicm_probe(struct platform_device *pdev)
1246 {
1247         struct panel_drv_data *ddata;
1248         struct backlight_device *bldev = NULL;
1249         struct device *dev = &pdev->dev;
1250         struct omap_dss_device *dssdev;
1251         int r;
1252
1253         dev_dbg(dev, "probe\n");
1254
1255         ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
1256         if (!ddata)
1257                 return -ENOMEM;
1258
1259         platform_set_drvdata(pdev, ddata);
1260         ddata->pdev = pdev;
1261
1262         ddata->vm.hactive = 864;
1263         ddata->vm.vactive = 480;
1264         ddata->vm.pixelclock = 864 * 480 * 60;
1265
1266         r = dsicm_probe_of(pdev);
1267         if (r)
1268                 return r;
1269
1270         dssdev = &ddata->dssdev;
1271         dssdev->dev = dev;
1272         dssdev->ops = &dsicm_ops;
1273         dssdev->driver = &dsicm_dss_driver;
1274         dssdev->type = OMAP_DISPLAY_TYPE_DSI;
1275         dssdev->owner = THIS_MODULE;
1276         dssdev->of_ports = BIT(0);
1277         dssdev->ops_flags = OMAP_DSS_DEVICE_OP_MODES;
1278
1279         dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
1280                 OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
1281
1282         omapdss_display_init(dssdev);
1283         omapdss_device_register(dssdev);
1284
1285         mutex_init(&ddata->lock);
1286
1287         atomic_set(&ddata->do_update, 0);
1288
1289         if (ddata->ext_te_gpio) {
1290                 r = devm_request_irq(dev, gpiod_to_irq(ddata->ext_te_gpio),
1291                                 dsicm_te_isr,
1292                                 IRQF_TRIGGER_RISING,
1293                                 "taal vsync", ddata);
1294
1295                 if (r) {
1296                         dev_err(dev, "IRQ request failed\n");
1297                         goto err_reg;
1298                 }
1299
1300                 INIT_DEFERRABLE_WORK(&ddata->te_timeout_work,
1301                                         dsicm_te_timeout_work_callback);
1302
1303                 dev_dbg(dev, "Using GPIO TE\n");
1304         }
1305
1306         ddata->workqueue = create_singlethread_workqueue("dsicm_wq");
1307         if (!ddata->workqueue) {
1308                 r = -ENOMEM;
1309                 goto err_reg;
1310         }
1311         INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work);
1312
1313         dsicm_hw_reset(ddata);
1314
1315         if (ddata->use_dsi_backlight) {
1316                 struct backlight_properties props = { 0 };
1317                 props.max_brightness = 255;
1318                 props.type = BACKLIGHT_RAW;
1319
1320                 bldev = devm_backlight_device_register(dev, dev_name(dev),
1321                         dev, ddata, &dsicm_bl_ops, &props);
1322                 if (IS_ERR(bldev)) {
1323                         r = PTR_ERR(bldev);
1324                         goto err_bl;
1325                 }
1326
1327                 ddata->bldev = bldev;
1328         }
1329
1330         r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
1331         if (r) {
1332                 dev_err(dev, "failed to create sysfs files\n");
1333                 goto err_bl;
1334         }
1335
1336         return 0;
1337
1338 err_bl:
1339         destroy_workqueue(ddata->workqueue);
1340 err_reg:
1341         if (ddata->extbldev)
1342                 put_device(&ddata->extbldev->dev);
1343
1344         return r;
1345 }
1346
1347 static int __exit dsicm_remove(struct platform_device *pdev)
1348 {
1349         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1350         struct omap_dss_device *dssdev = &ddata->dssdev;
1351
1352         dev_dbg(&pdev->dev, "remove\n");
1353
1354         omapdss_device_unregister(dssdev);
1355
1356         if (omapdss_device_is_enabled(dssdev))
1357                 dsicm_disable(dssdev);
1358         omapdss_device_disconnect(ddata->src, dssdev);
1359
1360         sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group);
1361
1362         if (ddata->extbldev)
1363                 put_device(&ddata->extbldev->dev);
1364
1365         dsicm_cancel_ulps_work(ddata);
1366         destroy_workqueue(ddata->workqueue);
1367
1368         /* reset, to be sure that the panel is in a valid state */
1369         dsicm_hw_reset(ddata);
1370
1371         return 0;
1372 }
1373
1374 static const struct of_device_id dsicm_of_match[] = {
1375         { .compatible = "omapdss,panel-dsi-cm", },
1376         {},
1377 };
1378
1379 MODULE_DEVICE_TABLE(of, dsicm_of_match);
1380
1381 static struct platform_driver dsicm_driver = {
1382         .probe = dsicm_probe,
1383         .remove = __exit_p(dsicm_remove),
1384         .driver = {
1385                 .name = "panel-dsi-cm",
1386                 .of_match_table = dsicm_of_match,
1387                 .suppress_bind_attrs = true,
1388         },
1389 };
1390
1391 module_platform_driver(dsicm_driver);
1392
1393 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
1394 MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
1395 MODULE_LICENSE("GPL");