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