Linux 4.19.133
[linux/fpc-iii.git] / drivers / video / fbdev / omap2 / omapfb / displays / panel-dsi-cm.c
blob87497a00241fdb0cb69882a94ca94d8df1bed91a
1 /*
2 * Generic DSI Command Mode panel driver
4 * Copyright (C) 2013 Texas Instruments
5 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
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.
12 /* #define DEBUG */
14 #include <linux/backlight.h>
15 #include <linux/delay.h>
16 #include <linux/fb.h>
17 #include <linux/gpio.h>
18 #include <linux/interrupt.h>
19 #include <linux/jiffies.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/sched/signal.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <linux/of_device.h>
26 #include <linux/of_gpio.h>
28 #include <video/omapfb_dss.h>
29 #include <video/mipi_display.h>
31 /* DSI Virtual channel. Hardcoded for now. */
32 #define TCH 0
34 #define DCS_READ_NUM_ERRORS 0x05
35 #define DCS_BRIGHTNESS 0x51
36 #define DCS_CTRL_DISPLAY 0x53
37 #define DCS_GET_ID1 0xda
38 #define DCS_GET_ID2 0xdb
39 #define DCS_GET_ID3 0xdc
41 struct panel_drv_data {
42 struct omap_dss_device dssdev;
43 struct omap_dss_device *in;
45 struct omap_video_timings timings;
47 struct platform_device *pdev;
49 struct mutex lock;
51 struct backlight_device *bldev;
53 unsigned long hw_guard_end; /* next value of jiffies when we can
54 * issue the next sleep in/out command
56 unsigned long hw_guard_wait; /* max guard time in jiffies */
58 /* panel HW configuration from DT or platform data */
59 int reset_gpio;
60 int ext_te_gpio;
62 bool use_dsi_backlight;
64 struct omap_dsi_pin_config pin_config;
66 /* runtime variables */
67 bool enabled;
69 bool te_enabled;
71 atomic_t do_update;
72 int channel;
74 struct delayed_work te_timeout_work;
76 bool intro_printed;
78 bool ulps_enabled;
79 unsigned ulps_timeout;
80 struct delayed_work ulps_work;
83 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
85 static irqreturn_t dsicm_te_isr(int irq, void *data);
86 static void dsicm_te_timeout_work_callback(struct work_struct *work);
87 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable);
89 static int dsicm_panel_reset(struct panel_drv_data *ddata);
91 static void dsicm_ulps_work(struct work_struct *work);
93 static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec)
95 ddata->hw_guard_wait = msecs_to_jiffies(guard_msec);
96 ddata->hw_guard_end = jiffies + ddata->hw_guard_wait;
99 static void hw_guard_wait(struct panel_drv_data *ddata)
101 unsigned long wait = ddata->hw_guard_end - jiffies;
103 if ((long)wait > 0 && time_before_eq(wait, ddata->hw_guard_wait)) {
104 set_current_state(TASK_UNINTERRUPTIBLE);
105 schedule_timeout(wait);
109 static int dsicm_dcs_read_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 *data)
111 struct omap_dss_device *in = ddata->in;
112 int r;
113 u8 buf[1];
115 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd, buf, 1);
117 if (r < 0)
118 return r;
120 *data = buf[0];
122 return 0;
125 static int dsicm_dcs_write_0(struct panel_drv_data *ddata, u8 dcs_cmd)
127 struct omap_dss_device *in = ddata->in;
128 return in->ops.dsi->dcs_write(in, ddata->channel, &dcs_cmd, 1);
131 static int dsicm_dcs_write_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 param)
133 struct omap_dss_device *in = ddata->in;
134 u8 buf[2] = { dcs_cmd, param };
136 return in->ops.dsi->dcs_write(in, ddata->channel, buf, 2);
139 static int dsicm_sleep_in(struct panel_drv_data *ddata)
142 struct omap_dss_device *in = ddata->in;
143 u8 cmd;
144 int r;
146 hw_guard_wait(ddata);
148 cmd = MIPI_DCS_ENTER_SLEEP_MODE;
149 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, &cmd, 1);
150 if (r)
151 return r;
153 hw_guard_start(ddata, 120);
155 usleep_range(5000, 10000);
157 return 0;
160 static int dsicm_sleep_out(struct panel_drv_data *ddata)
162 int r;
164 hw_guard_wait(ddata);
166 r = dsicm_dcs_write_0(ddata, MIPI_DCS_EXIT_SLEEP_MODE);
167 if (r)
168 return r;
170 hw_guard_start(ddata, 120);
172 usleep_range(5000, 10000);
174 return 0;
177 static int dsicm_get_id(struct panel_drv_data *ddata, u8 *id1, u8 *id2, u8 *id3)
179 int r;
181 r = dsicm_dcs_read_1(ddata, DCS_GET_ID1, id1);
182 if (r)
183 return r;
184 r = dsicm_dcs_read_1(ddata, DCS_GET_ID2, id2);
185 if (r)
186 return r;
187 r = dsicm_dcs_read_1(ddata, DCS_GET_ID3, id3);
188 if (r)
189 return r;
191 return 0;
194 static int dsicm_set_update_window(struct panel_drv_data *ddata,
195 u16 x, u16 y, u16 w, u16 h)
197 struct omap_dss_device *in = ddata->in;
198 int r;
199 u16 x1 = x;
200 u16 x2 = x + w - 1;
201 u16 y1 = y;
202 u16 y2 = y + h - 1;
204 u8 buf[5];
205 buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS;
206 buf[1] = (x1 >> 8) & 0xff;
207 buf[2] = (x1 >> 0) & 0xff;
208 buf[3] = (x2 >> 8) & 0xff;
209 buf[4] = (x2 >> 0) & 0xff;
211 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
212 if (r)
213 return r;
215 buf[0] = MIPI_DCS_SET_PAGE_ADDRESS;
216 buf[1] = (y1 >> 8) & 0xff;
217 buf[2] = (y1 >> 0) & 0xff;
218 buf[3] = (y2 >> 8) & 0xff;
219 buf[4] = (y2 >> 0) & 0xff;
221 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
222 if (r)
223 return r;
225 in->ops.dsi->bta_sync(in, ddata->channel);
227 return r;
230 static void dsicm_queue_ulps_work(struct panel_drv_data *ddata)
232 if (ddata->ulps_timeout > 0)
233 schedule_delayed_work(&ddata->ulps_work,
234 msecs_to_jiffies(ddata->ulps_timeout));
237 static void dsicm_cancel_ulps_work(struct panel_drv_data *ddata)
239 cancel_delayed_work(&ddata->ulps_work);
242 static int dsicm_enter_ulps(struct panel_drv_data *ddata)
244 struct omap_dss_device *in = ddata->in;
245 int r;
247 if (ddata->ulps_enabled)
248 return 0;
250 dsicm_cancel_ulps_work(ddata);
252 r = _dsicm_enable_te(ddata, false);
253 if (r)
254 goto err;
256 if (gpio_is_valid(ddata->ext_te_gpio))
257 disable_irq(gpio_to_irq(ddata->ext_te_gpio));
259 in->ops.dsi->disable(in, false, true);
261 ddata->ulps_enabled = true;
263 return 0;
265 err:
266 dev_err(&ddata->pdev->dev, "enter ULPS failed");
267 dsicm_panel_reset(ddata);
269 ddata->ulps_enabled = false;
271 dsicm_queue_ulps_work(ddata);
273 return r;
276 static int dsicm_exit_ulps(struct panel_drv_data *ddata)
278 struct omap_dss_device *in = ddata->in;
279 int r;
281 if (!ddata->ulps_enabled)
282 return 0;
284 r = in->ops.dsi->enable(in);
285 if (r) {
286 dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
287 goto err1;
290 in->ops.dsi->enable_hs(in, ddata->channel, true);
292 r = _dsicm_enable_te(ddata, true);
293 if (r) {
294 dev_err(&ddata->pdev->dev, "failed to re-enable TE");
295 goto err2;
298 if (gpio_is_valid(ddata->ext_te_gpio))
299 enable_irq(gpio_to_irq(ddata->ext_te_gpio));
301 dsicm_queue_ulps_work(ddata);
303 ddata->ulps_enabled = false;
305 return 0;
307 err2:
308 dev_err(&ddata->pdev->dev, "failed to exit ULPS");
310 r = dsicm_panel_reset(ddata);
311 if (!r) {
312 if (gpio_is_valid(ddata->ext_te_gpio))
313 enable_irq(gpio_to_irq(ddata->ext_te_gpio));
314 ddata->ulps_enabled = false;
316 err1:
317 dsicm_queue_ulps_work(ddata);
319 return r;
322 static int dsicm_wake_up(struct panel_drv_data *ddata)
324 if (ddata->ulps_enabled)
325 return dsicm_exit_ulps(ddata);
327 dsicm_cancel_ulps_work(ddata);
328 dsicm_queue_ulps_work(ddata);
329 return 0;
332 static int dsicm_bl_update_status(struct backlight_device *dev)
334 struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
335 struct omap_dss_device *in = ddata->in;
336 int r;
337 int level;
339 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
340 dev->props.power == FB_BLANK_UNBLANK)
341 level = dev->props.brightness;
342 else
343 level = 0;
345 dev_dbg(&ddata->pdev->dev, "update brightness to %d\n", level);
347 mutex_lock(&ddata->lock);
349 if (ddata->enabled) {
350 in->ops.dsi->bus_lock(in);
352 r = dsicm_wake_up(ddata);
353 if (!r)
354 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, level);
356 in->ops.dsi->bus_unlock(in);
357 } else {
358 r = 0;
361 mutex_unlock(&ddata->lock);
363 return r;
366 static int dsicm_bl_get_intensity(struct backlight_device *dev)
368 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
369 dev->props.power == FB_BLANK_UNBLANK)
370 return dev->props.brightness;
372 return 0;
375 static const struct backlight_ops dsicm_bl_ops = {
376 .get_brightness = dsicm_bl_get_intensity,
377 .update_status = dsicm_bl_update_status,
380 static void dsicm_get_resolution(struct omap_dss_device *dssdev,
381 u16 *xres, u16 *yres)
383 *xres = dssdev->panel.timings.x_res;
384 *yres = dssdev->panel.timings.y_res;
387 static ssize_t dsicm_num_errors_show(struct device *dev,
388 struct device_attribute *attr, char *buf)
390 struct panel_drv_data *ddata = dev_get_drvdata(dev);
391 struct omap_dss_device *in = ddata->in;
392 u8 errors = 0;
393 int r;
395 mutex_lock(&ddata->lock);
397 if (ddata->enabled) {
398 in->ops.dsi->bus_lock(in);
400 r = dsicm_wake_up(ddata);
401 if (!r)
402 r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS,
403 &errors);
405 in->ops.dsi->bus_unlock(in);
406 } else {
407 r = -ENODEV;
410 mutex_unlock(&ddata->lock);
412 if (r)
413 return r;
415 return snprintf(buf, PAGE_SIZE, "%d\n", errors);
418 static ssize_t dsicm_hw_revision_show(struct device *dev,
419 struct device_attribute *attr, char *buf)
421 struct panel_drv_data *ddata = dev_get_drvdata(dev);
422 struct omap_dss_device *in = ddata->in;
423 u8 id1, id2, id3;
424 int r;
426 mutex_lock(&ddata->lock);
428 if (ddata->enabled) {
429 in->ops.dsi->bus_lock(in);
431 r = dsicm_wake_up(ddata);
432 if (!r)
433 r = dsicm_get_id(ddata, &id1, &id2, &id3);
435 in->ops.dsi->bus_unlock(in);
436 } else {
437 r = -ENODEV;
440 mutex_unlock(&ddata->lock);
442 if (r)
443 return r;
445 return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
448 static ssize_t dsicm_store_ulps(struct device *dev,
449 struct device_attribute *attr,
450 const char *buf, size_t count)
452 struct panel_drv_data *ddata = dev_get_drvdata(dev);
453 struct omap_dss_device *in = ddata->in;
454 unsigned long t;
455 int r;
457 r = kstrtoul(buf, 0, &t);
458 if (r)
459 return r;
461 mutex_lock(&ddata->lock);
463 if (ddata->enabled) {
464 in->ops.dsi->bus_lock(in);
466 if (t)
467 r = dsicm_enter_ulps(ddata);
468 else
469 r = dsicm_wake_up(ddata);
471 in->ops.dsi->bus_unlock(in);
474 mutex_unlock(&ddata->lock);
476 if (r)
477 return r;
479 return count;
482 static ssize_t dsicm_show_ulps(struct device *dev,
483 struct device_attribute *attr,
484 char *buf)
486 struct panel_drv_data *ddata = dev_get_drvdata(dev);
487 unsigned t;
489 mutex_lock(&ddata->lock);
490 t = ddata->ulps_enabled;
491 mutex_unlock(&ddata->lock);
493 return snprintf(buf, PAGE_SIZE, "%u\n", t);
496 static ssize_t dsicm_store_ulps_timeout(struct device *dev,
497 struct device_attribute *attr,
498 const char *buf, size_t count)
500 struct panel_drv_data *ddata = dev_get_drvdata(dev);
501 struct omap_dss_device *in = ddata->in;
502 unsigned long t;
503 int r;
505 r = kstrtoul(buf, 0, &t);
506 if (r)
507 return r;
509 mutex_lock(&ddata->lock);
510 ddata->ulps_timeout = t;
512 if (ddata->enabled) {
513 /* dsicm_wake_up will restart the timer */
514 in->ops.dsi->bus_lock(in);
515 r = dsicm_wake_up(ddata);
516 in->ops.dsi->bus_unlock(in);
519 mutex_unlock(&ddata->lock);
521 if (r)
522 return r;
524 return count;
527 static ssize_t dsicm_show_ulps_timeout(struct device *dev,
528 struct device_attribute *attr,
529 char *buf)
531 struct panel_drv_data *ddata = dev_get_drvdata(dev);
532 unsigned t;
534 mutex_lock(&ddata->lock);
535 t = ddata->ulps_timeout;
536 mutex_unlock(&ddata->lock);
538 return snprintf(buf, PAGE_SIZE, "%u\n", t);
541 static DEVICE_ATTR(num_dsi_errors, S_IRUGO, dsicm_num_errors_show, NULL);
542 static DEVICE_ATTR(hw_revision, S_IRUGO, dsicm_hw_revision_show, NULL);
543 static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
544 dsicm_show_ulps, dsicm_store_ulps);
545 static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
546 dsicm_show_ulps_timeout, dsicm_store_ulps_timeout);
548 static struct attribute *dsicm_attrs[] = {
549 &dev_attr_num_dsi_errors.attr,
550 &dev_attr_hw_revision.attr,
551 &dev_attr_ulps.attr,
552 &dev_attr_ulps_timeout.attr,
553 NULL,
556 static const struct attribute_group dsicm_attr_group = {
557 .attrs = dsicm_attrs,
560 static void dsicm_hw_reset(struct panel_drv_data *ddata)
562 if (!gpio_is_valid(ddata->reset_gpio))
563 return;
565 gpio_set_value(ddata->reset_gpio, 1);
566 udelay(10);
567 /* reset the panel */
568 gpio_set_value(ddata->reset_gpio, 0);
569 /* assert reset */
570 udelay(10);
571 gpio_set_value(ddata->reset_gpio, 1);
572 /* wait after releasing reset */
573 usleep_range(5000, 10000);
576 static int dsicm_power_on(struct panel_drv_data *ddata)
578 struct omap_dss_device *in = ddata->in;
579 u8 id1, id2, id3;
580 int r;
581 struct omap_dss_dsi_config dsi_config = {
582 .mode = OMAP_DSS_DSI_CMD_MODE,
583 .pixel_format = OMAP_DSS_DSI_FMT_RGB888,
584 .timings = &ddata->timings,
585 .hs_clk_min = 150000000,
586 .hs_clk_max = 300000000,
587 .lp_clk_min = 7000000,
588 .lp_clk_max = 10000000,
591 if (ddata->pin_config.num_pins > 0) {
592 r = in->ops.dsi->configure_pins(in, &ddata->pin_config);
593 if (r) {
594 dev_err(&ddata->pdev->dev,
595 "failed to configure DSI pins\n");
596 goto err0;
600 r = in->ops.dsi->set_config(in, &dsi_config);
601 if (r) {
602 dev_err(&ddata->pdev->dev, "failed to configure DSI\n");
603 goto err0;
606 r = in->ops.dsi->enable(in);
607 if (r) {
608 dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
609 goto err0;
612 dsicm_hw_reset(ddata);
614 in->ops.dsi->enable_hs(in, ddata->channel, false);
616 r = dsicm_sleep_out(ddata);
617 if (r)
618 goto err;
620 r = dsicm_get_id(ddata, &id1, &id2, &id3);
621 if (r)
622 goto err;
624 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff);
625 if (r)
626 goto err;
628 r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY,
629 (1<<2) | (1<<5)); /* BL | BCTRL */
630 if (r)
631 goto err;
633 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT,
634 MIPI_DCS_PIXEL_FMT_24BIT);
635 if (r)
636 goto err;
638 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON);
639 if (r)
640 goto err;
642 r = _dsicm_enable_te(ddata, ddata->te_enabled);
643 if (r)
644 goto err;
646 r = in->ops.dsi->enable_video_output(in, ddata->channel);
647 if (r)
648 goto err;
650 ddata->enabled = 1;
652 if (!ddata->intro_printed) {
653 dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n",
654 id1, id2, id3);
655 ddata->intro_printed = true;
658 in->ops.dsi->enable_hs(in, ddata->channel, true);
660 return 0;
661 err:
662 dev_err(&ddata->pdev->dev, "error while enabling panel, issuing HW reset\n");
664 dsicm_hw_reset(ddata);
666 in->ops.dsi->disable(in, true, false);
667 err0:
668 return r;
671 static void dsicm_power_off(struct panel_drv_data *ddata)
673 struct omap_dss_device *in = ddata->in;
674 int r;
676 in->ops.dsi->disable_video_output(in, ddata->channel);
678 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF);
679 if (!r)
680 r = dsicm_sleep_in(ddata);
682 if (r) {
683 dev_err(&ddata->pdev->dev,
684 "error disabling panel, issuing HW reset\n");
685 dsicm_hw_reset(ddata);
688 in->ops.dsi->disable(in, true, false);
690 ddata->enabled = 0;
693 static int dsicm_panel_reset(struct panel_drv_data *ddata)
695 dev_err(&ddata->pdev->dev, "performing LCD reset\n");
697 dsicm_power_off(ddata);
698 dsicm_hw_reset(ddata);
699 return dsicm_power_on(ddata);
702 static int dsicm_connect(struct omap_dss_device *dssdev)
704 struct panel_drv_data *ddata = to_panel_data(dssdev);
705 struct omap_dss_device *in = ddata->in;
706 struct device *dev = &ddata->pdev->dev;
707 int r;
709 if (omapdss_device_is_connected(dssdev))
710 return 0;
712 r = in->ops.dsi->connect(in, dssdev);
713 if (r) {
714 dev_err(dev, "Failed to connect to video source\n");
715 return r;
718 r = in->ops.dsi->request_vc(ddata->in, &ddata->channel);
719 if (r) {
720 dev_err(dev, "failed to get virtual channel\n");
721 goto err_req_vc;
724 r = in->ops.dsi->set_vc_id(ddata->in, ddata->channel, TCH);
725 if (r) {
726 dev_err(dev, "failed to set VC_ID\n");
727 goto err_vc_id;
730 return 0;
732 err_vc_id:
733 in->ops.dsi->release_vc(ddata->in, ddata->channel);
734 err_req_vc:
735 in->ops.dsi->disconnect(in, dssdev);
736 return r;
739 static void dsicm_disconnect(struct omap_dss_device *dssdev)
741 struct panel_drv_data *ddata = to_panel_data(dssdev);
742 struct omap_dss_device *in = ddata->in;
744 if (!omapdss_device_is_connected(dssdev))
745 return;
747 in->ops.dsi->release_vc(in, ddata->channel);
748 in->ops.dsi->disconnect(in, dssdev);
751 static int dsicm_enable(struct omap_dss_device *dssdev)
753 struct panel_drv_data *ddata = to_panel_data(dssdev);
754 struct omap_dss_device *in = ddata->in;
755 int r;
757 dev_dbg(&ddata->pdev->dev, "enable\n");
759 mutex_lock(&ddata->lock);
761 if (!omapdss_device_is_connected(dssdev)) {
762 r = -ENODEV;
763 goto err;
766 if (omapdss_device_is_enabled(dssdev)) {
767 r = 0;
768 goto err;
771 in->ops.dsi->bus_lock(in);
773 r = dsicm_power_on(ddata);
775 in->ops.dsi->bus_unlock(in);
777 if (r)
778 goto err;
780 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
782 mutex_unlock(&ddata->lock);
784 return 0;
785 err:
786 dev_dbg(&ddata->pdev->dev, "enable failed\n");
787 mutex_unlock(&ddata->lock);
788 return r;
791 static void dsicm_disable(struct omap_dss_device *dssdev)
793 struct panel_drv_data *ddata = to_panel_data(dssdev);
794 struct omap_dss_device *in = ddata->in;
795 int r;
797 dev_dbg(&ddata->pdev->dev, "disable\n");
799 mutex_lock(&ddata->lock);
801 dsicm_cancel_ulps_work(ddata);
803 in->ops.dsi->bus_lock(in);
805 if (omapdss_device_is_enabled(dssdev)) {
806 r = dsicm_wake_up(ddata);
807 if (!r)
808 dsicm_power_off(ddata);
811 in->ops.dsi->bus_unlock(in);
813 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
815 mutex_unlock(&ddata->lock);
818 static void dsicm_framedone_cb(int err, void *data)
820 struct panel_drv_data *ddata = data;
821 struct omap_dss_device *in = ddata->in;
823 dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err);
824 in->ops.dsi->bus_unlock(ddata->in);
827 static irqreturn_t dsicm_te_isr(int irq, void *data)
829 struct panel_drv_data *ddata = data;
830 struct omap_dss_device *in = ddata->in;
831 int old;
832 int r;
834 old = atomic_cmpxchg(&ddata->do_update, 1, 0);
836 if (old) {
837 cancel_delayed_work(&ddata->te_timeout_work);
839 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
840 ddata);
841 if (r)
842 goto err;
845 return IRQ_HANDLED;
846 err:
847 dev_err(&ddata->pdev->dev, "start update failed\n");
848 in->ops.dsi->bus_unlock(in);
849 return IRQ_HANDLED;
852 static void dsicm_te_timeout_work_callback(struct work_struct *work)
854 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
855 te_timeout_work.work);
856 struct omap_dss_device *in = ddata->in;
858 dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n");
860 atomic_set(&ddata->do_update, 0);
861 in->ops.dsi->bus_unlock(in);
864 static int dsicm_update(struct omap_dss_device *dssdev,
865 u16 x, u16 y, u16 w, u16 h)
867 struct panel_drv_data *ddata = to_panel_data(dssdev);
868 struct omap_dss_device *in = ddata->in;
869 int r;
871 dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
873 mutex_lock(&ddata->lock);
874 in->ops.dsi->bus_lock(in);
876 r = dsicm_wake_up(ddata);
877 if (r)
878 goto err;
880 if (!ddata->enabled) {
881 r = 0;
882 goto err;
885 /* XXX no need to send this every frame, but dsi break if not done */
886 r = dsicm_set_update_window(ddata, 0, 0,
887 dssdev->panel.timings.x_res,
888 dssdev->panel.timings.y_res);
889 if (r)
890 goto err;
892 if (ddata->te_enabled && gpio_is_valid(ddata->ext_te_gpio)) {
893 schedule_delayed_work(&ddata->te_timeout_work,
894 msecs_to_jiffies(250));
895 atomic_set(&ddata->do_update, 1);
896 } else {
897 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
898 ddata);
899 if (r)
900 goto err;
903 /* note: no bus_unlock here. unlock is in framedone_cb */
904 mutex_unlock(&ddata->lock);
905 return 0;
906 err:
907 in->ops.dsi->bus_unlock(in);
908 mutex_unlock(&ddata->lock);
909 return r;
912 static int dsicm_sync(struct omap_dss_device *dssdev)
914 struct panel_drv_data *ddata = to_panel_data(dssdev);
915 struct omap_dss_device *in = ddata->in;
917 dev_dbg(&ddata->pdev->dev, "sync\n");
919 mutex_lock(&ddata->lock);
920 in->ops.dsi->bus_lock(in);
921 in->ops.dsi->bus_unlock(in);
922 mutex_unlock(&ddata->lock);
924 dev_dbg(&ddata->pdev->dev, "sync done\n");
926 return 0;
929 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable)
931 struct omap_dss_device *in = ddata->in;
932 int r;
934 if (enable)
935 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0);
936 else
937 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF);
939 if (!gpio_is_valid(ddata->ext_te_gpio))
940 in->ops.dsi->enable_te(in, enable);
942 /* possible panel bug */
943 msleep(100);
945 return r;
948 static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable)
950 struct panel_drv_data *ddata = to_panel_data(dssdev);
951 struct omap_dss_device *in = ddata->in;
952 int r;
954 mutex_lock(&ddata->lock);
956 if (ddata->te_enabled == enable)
957 goto end;
959 in->ops.dsi->bus_lock(in);
961 if (ddata->enabled) {
962 r = dsicm_wake_up(ddata);
963 if (r)
964 goto err;
966 r = _dsicm_enable_te(ddata, enable);
967 if (r)
968 goto err;
971 ddata->te_enabled = enable;
973 in->ops.dsi->bus_unlock(in);
974 end:
975 mutex_unlock(&ddata->lock);
977 return 0;
978 err:
979 in->ops.dsi->bus_unlock(in);
980 mutex_unlock(&ddata->lock);
982 return r;
985 static int dsicm_get_te(struct omap_dss_device *dssdev)
987 struct panel_drv_data *ddata = to_panel_data(dssdev);
988 int r;
990 mutex_lock(&ddata->lock);
991 r = ddata->te_enabled;
992 mutex_unlock(&ddata->lock);
994 return r;
997 static int dsicm_memory_read(struct omap_dss_device *dssdev,
998 void *buf, size_t size,
999 u16 x, u16 y, u16 w, u16 h)
1001 struct panel_drv_data *ddata = to_panel_data(dssdev);
1002 struct omap_dss_device *in = ddata->in;
1003 int r;
1004 int first = 1;
1005 int plen;
1006 unsigned buf_used = 0;
1008 if (size < w * h * 3)
1009 return -ENOMEM;
1011 mutex_lock(&ddata->lock);
1013 if (!ddata->enabled) {
1014 r = -ENODEV;
1015 goto err1;
1018 size = min(w * h * 3,
1019 dssdev->panel.timings.x_res *
1020 dssdev->panel.timings.y_res * 3);
1022 in->ops.dsi->bus_lock(in);
1024 r = dsicm_wake_up(ddata);
1025 if (r)
1026 goto err2;
1028 /* plen 1 or 2 goes into short packet. until checksum error is fixed,
1029 * use short packets. plen 32 works, but bigger packets seem to cause
1030 * an error. */
1031 if (size % 2)
1032 plen = 1;
1033 else
1034 plen = 2;
1036 dsicm_set_update_window(ddata, x, y, w, h);
1038 r = in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, plen);
1039 if (r)
1040 goto err2;
1042 while (buf_used < size) {
1043 u8 dcs_cmd = first ? 0x2e : 0x3e;
1044 first = 0;
1046 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd,
1047 buf + buf_used, size - buf_used);
1049 if (r < 0) {
1050 dev_err(dssdev->dev, "read error\n");
1051 goto err3;
1054 buf_used += r;
1056 if (r < plen) {
1057 dev_err(&ddata->pdev->dev, "short read\n");
1058 break;
1061 if (signal_pending(current)) {
1062 dev_err(&ddata->pdev->dev, "signal pending, "
1063 "aborting memory read\n");
1064 r = -ERESTARTSYS;
1065 goto err3;
1069 r = buf_used;
1071 err3:
1072 in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, 1);
1073 err2:
1074 in->ops.dsi->bus_unlock(in);
1075 err1:
1076 mutex_unlock(&ddata->lock);
1077 return r;
1080 static void dsicm_ulps_work(struct work_struct *work)
1082 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
1083 ulps_work.work);
1084 struct omap_dss_device *dssdev = &ddata->dssdev;
1085 struct omap_dss_device *in = ddata->in;
1087 mutex_lock(&ddata->lock);
1089 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) {
1090 mutex_unlock(&ddata->lock);
1091 return;
1094 in->ops.dsi->bus_lock(in);
1096 dsicm_enter_ulps(ddata);
1098 in->ops.dsi->bus_unlock(in);
1099 mutex_unlock(&ddata->lock);
1102 static struct omap_dss_driver dsicm_ops = {
1103 .connect = dsicm_connect,
1104 .disconnect = dsicm_disconnect,
1106 .enable = dsicm_enable,
1107 .disable = dsicm_disable,
1109 .update = dsicm_update,
1110 .sync = dsicm_sync,
1112 .get_resolution = dsicm_get_resolution,
1113 .get_recommended_bpp = omapdss_default_get_recommended_bpp,
1115 .enable_te = dsicm_enable_te,
1116 .get_te = dsicm_get_te,
1118 .memory_read = dsicm_memory_read,
1121 static int dsicm_probe_of(struct platform_device *pdev)
1123 struct device_node *node = pdev->dev.of_node;
1124 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1125 struct omap_dss_device *in;
1126 int gpio;
1128 gpio = of_get_named_gpio(node, "reset-gpios", 0);
1129 if (!gpio_is_valid(gpio)) {
1130 dev_err(&pdev->dev, "failed to parse reset gpio\n");
1131 return gpio;
1133 ddata->reset_gpio = gpio;
1135 gpio = of_get_named_gpio(node, "te-gpios", 0);
1136 if (gpio_is_valid(gpio) || gpio == -ENOENT) {
1137 ddata->ext_te_gpio = gpio;
1138 } else {
1139 dev_err(&pdev->dev, "failed to parse TE gpio\n");
1140 return gpio;
1143 in = omapdss_of_find_source_for_first_ep(node);
1144 if (IS_ERR(in)) {
1145 dev_err(&pdev->dev, "failed to find video source\n");
1146 return PTR_ERR(in);
1149 ddata->in = in;
1151 /* TODO: ulps, backlight */
1153 return 0;
1156 static int dsicm_probe(struct platform_device *pdev)
1158 struct backlight_properties props;
1159 struct panel_drv_data *ddata;
1160 struct backlight_device *bldev = NULL;
1161 struct device *dev = &pdev->dev;
1162 struct omap_dss_device *dssdev;
1163 int r;
1165 dev_dbg(dev, "probe\n");
1167 if (!pdev->dev.of_node)
1168 return -ENODEV;
1170 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
1171 if (!ddata)
1172 return -ENOMEM;
1174 platform_set_drvdata(pdev, ddata);
1175 ddata->pdev = pdev;
1177 r = dsicm_probe_of(pdev);
1178 if (r)
1179 return r;
1181 ddata->timings.x_res = 864;
1182 ddata->timings.y_res = 480;
1183 ddata->timings.pixelclock = 864 * 480 * 60;
1185 dssdev = &ddata->dssdev;
1186 dssdev->dev = dev;
1187 dssdev->driver = &dsicm_ops;
1188 dssdev->panel.timings = ddata->timings;
1189 dssdev->type = OMAP_DISPLAY_TYPE_DSI;
1190 dssdev->owner = THIS_MODULE;
1192 dssdev->panel.dsi_pix_fmt = OMAP_DSS_DSI_FMT_RGB888;
1193 dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
1194 OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
1196 r = omapdss_register_display(dssdev);
1197 if (r) {
1198 dev_err(dev, "Failed to register panel\n");
1199 goto err_reg;
1202 mutex_init(&ddata->lock);
1204 atomic_set(&ddata->do_update, 0);
1206 if (gpio_is_valid(ddata->reset_gpio)) {
1207 r = devm_gpio_request_one(dev, ddata->reset_gpio,
1208 GPIOF_OUT_INIT_LOW, "taal rst");
1209 if (r) {
1210 dev_err(dev, "failed to request reset gpio\n");
1211 return r;
1215 if (gpio_is_valid(ddata->ext_te_gpio)) {
1216 r = devm_gpio_request_one(dev, ddata->ext_te_gpio,
1217 GPIOF_IN, "taal irq");
1218 if (r) {
1219 dev_err(dev, "GPIO request failed\n");
1220 return r;
1223 r = devm_request_irq(dev, gpio_to_irq(ddata->ext_te_gpio),
1224 dsicm_te_isr,
1225 IRQF_TRIGGER_RISING,
1226 "taal vsync", ddata);
1228 if (r) {
1229 dev_err(dev, "IRQ request failed\n");
1230 return r;
1233 INIT_DEFERRABLE_WORK(&ddata->te_timeout_work,
1234 dsicm_te_timeout_work_callback);
1236 dev_dbg(dev, "Using GPIO TE\n");
1239 INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work);
1241 dsicm_hw_reset(ddata);
1243 if (ddata->use_dsi_backlight) {
1244 memset(&props, 0, sizeof(struct backlight_properties));
1245 props.max_brightness = 255;
1247 props.type = BACKLIGHT_RAW;
1248 bldev = backlight_device_register(dev_name(dev),
1249 dev, ddata, &dsicm_bl_ops, &props);
1250 if (IS_ERR(bldev)) {
1251 r = PTR_ERR(bldev);
1252 goto err_reg;
1255 ddata->bldev = bldev;
1257 bldev->props.fb_blank = FB_BLANK_UNBLANK;
1258 bldev->props.power = FB_BLANK_UNBLANK;
1259 bldev->props.brightness = 255;
1261 dsicm_bl_update_status(bldev);
1264 r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
1265 if (r) {
1266 dev_err(dev, "failed to create sysfs files\n");
1267 goto err_sysfs_create;
1270 return 0;
1272 err_sysfs_create:
1273 if (bldev != NULL)
1274 backlight_device_unregister(bldev);
1275 err_reg:
1276 return r;
1279 static int __exit dsicm_remove(struct platform_device *pdev)
1281 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1282 struct omap_dss_device *dssdev = &ddata->dssdev;
1283 struct backlight_device *bldev;
1285 dev_dbg(&pdev->dev, "remove\n");
1287 omapdss_unregister_display(dssdev);
1289 dsicm_disable(dssdev);
1290 dsicm_disconnect(dssdev);
1292 sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group);
1294 bldev = ddata->bldev;
1295 if (bldev != NULL) {
1296 bldev->props.power = FB_BLANK_POWERDOWN;
1297 dsicm_bl_update_status(bldev);
1298 backlight_device_unregister(bldev);
1301 omap_dss_put_device(ddata->in);
1303 dsicm_cancel_ulps_work(ddata);
1305 /* reset, to be sure that the panel is in a valid state */
1306 dsicm_hw_reset(ddata);
1308 return 0;
1311 static const struct of_device_id dsicm_of_match[] = {
1312 { .compatible = "omapdss,panel-dsi-cm", },
1316 MODULE_DEVICE_TABLE(of, dsicm_of_match);
1318 static struct platform_driver dsicm_driver = {
1319 .probe = dsicm_probe,
1320 .remove = __exit_p(dsicm_remove),
1321 .driver = {
1322 .name = "panel-dsi-cm",
1323 .of_match_table = dsicm_of_match,
1324 .suppress_bind_attrs = true,
1328 module_platform_driver(dsicm_driver);
1330 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
1331 MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
1332 MODULE_LICENSE("GPL");