Linux 4.16.11
[linux/fpc-iii.git] / drivers / video / fbdev / omap2 / omapfb / displays / panel-dsi-cm.c
blobbef4315300905a691c32373feb29e70ab1ff421e
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 platform_device *pdev = to_platform_device(dev);
391 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
392 struct omap_dss_device *in = ddata->in;
393 u8 errors = 0;
394 int r;
396 mutex_lock(&ddata->lock);
398 if (ddata->enabled) {
399 in->ops.dsi->bus_lock(in);
401 r = dsicm_wake_up(ddata);
402 if (!r)
403 r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS,
404 &errors);
406 in->ops.dsi->bus_unlock(in);
407 } else {
408 r = -ENODEV;
411 mutex_unlock(&ddata->lock);
413 if (r)
414 return r;
416 return snprintf(buf, PAGE_SIZE, "%d\n", errors);
419 static ssize_t dsicm_hw_revision_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
422 struct platform_device *pdev = to_platform_device(dev);
423 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
424 struct omap_dss_device *in = ddata->in;
425 u8 id1, id2, id3;
426 int r;
428 mutex_lock(&ddata->lock);
430 if (ddata->enabled) {
431 in->ops.dsi->bus_lock(in);
433 r = dsicm_wake_up(ddata);
434 if (!r)
435 r = dsicm_get_id(ddata, &id1, &id2, &id3);
437 in->ops.dsi->bus_unlock(in);
438 } else {
439 r = -ENODEV;
442 mutex_unlock(&ddata->lock);
444 if (r)
445 return r;
447 return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
450 static ssize_t dsicm_store_ulps(struct device *dev,
451 struct device_attribute *attr,
452 const char *buf, size_t count)
454 struct platform_device *pdev = to_platform_device(dev);
455 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
456 struct omap_dss_device *in = ddata->in;
457 unsigned long t;
458 int r;
460 r = kstrtoul(buf, 0, &t);
461 if (r)
462 return r;
464 mutex_lock(&ddata->lock);
466 if (ddata->enabled) {
467 in->ops.dsi->bus_lock(in);
469 if (t)
470 r = dsicm_enter_ulps(ddata);
471 else
472 r = dsicm_wake_up(ddata);
474 in->ops.dsi->bus_unlock(in);
477 mutex_unlock(&ddata->lock);
479 if (r)
480 return r;
482 return count;
485 static ssize_t dsicm_show_ulps(struct device *dev,
486 struct device_attribute *attr,
487 char *buf)
489 struct platform_device *pdev = to_platform_device(dev);
490 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
491 unsigned t;
493 mutex_lock(&ddata->lock);
494 t = ddata->ulps_enabled;
495 mutex_unlock(&ddata->lock);
497 return snprintf(buf, PAGE_SIZE, "%u\n", t);
500 static ssize_t dsicm_store_ulps_timeout(struct device *dev,
501 struct device_attribute *attr,
502 const char *buf, size_t count)
504 struct platform_device *pdev = to_platform_device(dev);
505 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
506 struct omap_dss_device *in = ddata->in;
507 unsigned long t;
508 int r;
510 r = kstrtoul(buf, 0, &t);
511 if (r)
512 return r;
514 mutex_lock(&ddata->lock);
515 ddata->ulps_timeout = t;
517 if (ddata->enabled) {
518 /* dsicm_wake_up will restart the timer */
519 in->ops.dsi->bus_lock(in);
520 r = dsicm_wake_up(ddata);
521 in->ops.dsi->bus_unlock(in);
524 mutex_unlock(&ddata->lock);
526 if (r)
527 return r;
529 return count;
532 static ssize_t dsicm_show_ulps_timeout(struct device *dev,
533 struct device_attribute *attr,
534 char *buf)
536 struct platform_device *pdev = to_platform_device(dev);
537 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
538 unsigned t;
540 mutex_lock(&ddata->lock);
541 t = ddata->ulps_timeout;
542 mutex_unlock(&ddata->lock);
544 return snprintf(buf, PAGE_SIZE, "%u\n", t);
547 static DEVICE_ATTR(num_dsi_errors, S_IRUGO, dsicm_num_errors_show, NULL);
548 static DEVICE_ATTR(hw_revision, S_IRUGO, dsicm_hw_revision_show, NULL);
549 static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
550 dsicm_show_ulps, dsicm_store_ulps);
551 static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
552 dsicm_show_ulps_timeout, dsicm_store_ulps_timeout);
554 static struct attribute *dsicm_attrs[] = {
555 &dev_attr_num_dsi_errors.attr,
556 &dev_attr_hw_revision.attr,
557 &dev_attr_ulps.attr,
558 &dev_attr_ulps_timeout.attr,
559 NULL,
562 static const struct attribute_group dsicm_attr_group = {
563 .attrs = dsicm_attrs,
566 static void dsicm_hw_reset(struct panel_drv_data *ddata)
568 if (!gpio_is_valid(ddata->reset_gpio))
569 return;
571 gpio_set_value(ddata->reset_gpio, 1);
572 udelay(10);
573 /* reset the panel */
574 gpio_set_value(ddata->reset_gpio, 0);
575 /* assert reset */
576 udelay(10);
577 gpio_set_value(ddata->reset_gpio, 1);
578 /* wait after releasing reset */
579 usleep_range(5000, 10000);
582 static int dsicm_power_on(struct panel_drv_data *ddata)
584 struct omap_dss_device *in = ddata->in;
585 u8 id1, id2, id3;
586 int r;
587 struct omap_dss_dsi_config dsi_config = {
588 .mode = OMAP_DSS_DSI_CMD_MODE,
589 .pixel_format = OMAP_DSS_DSI_FMT_RGB888,
590 .timings = &ddata->timings,
591 .hs_clk_min = 150000000,
592 .hs_clk_max = 300000000,
593 .lp_clk_min = 7000000,
594 .lp_clk_max = 10000000,
597 if (ddata->pin_config.num_pins > 0) {
598 r = in->ops.dsi->configure_pins(in, &ddata->pin_config);
599 if (r) {
600 dev_err(&ddata->pdev->dev,
601 "failed to configure DSI pins\n");
602 goto err0;
606 r = in->ops.dsi->set_config(in, &dsi_config);
607 if (r) {
608 dev_err(&ddata->pdev->dev, "failed to configure DSI\n");
609 goto err0;
612 r = in->ops.dsi->enable(in);
613 if (r) {
614 dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
615 goto err0;
618 dsicm_hw_reset(ddata);
620 in->ops.dsi->enable_hs(in, ddata->channel, false);
622 r = dsicm_sleep_out(ddata);
623 if (r)
624 goto err;
626 r = dsicm_get_id(ddata, &id1, &id2, &id3);
627 if (r)
628 goto err;
630 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff);
631 if (r)
632 goto err;
634 r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY,
635 (1<<2) | (1<<5)); /* BL | BCTRL */
636 if (r)
637 goto err;
639 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT,
640 MIPI_DCS_PIXEL_FMT_24BIT);
641 if (r)
642 goto err;
644 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON);
645 if (r)
646 goto err;
648 r = _dsicm_enable_te(ddata, ddata->te_enabled);
649 if (r)
650 goto err;
652 r = in->ops.dsi->enable_video_output(in, ddata->channel);
653 if (r)
654 goto err;
656 ddata->enabled = 1;
658 if (!ddata->intro_printed) {
659 dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n",
660 id1, id2, id3);
661 ddata->intro_printed = true;
664 in->ops.dsi->enable_hs(in, ddata->channel, true);
666 return 0;
667 err:
668 dev_err(&ddata->pdev->dev, "error while enabling panel, issuing HW reset\n");
670 dsicm_hw_reset(ddata);
672 in->ops.dsi->disable(in, true, false);
673 err0:
674 return r;
677 static void dsicm_power_off(struct panel_drv_data *ddata)
679 struct omap_dss_device *in = ddata->in;
680 int r;
682 in->ops.dsi->disable_video_output(in, ddata->channel);
684 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF);
685 if (!r)
686 r = dsicm_sleep_in(ddata);
688 if (r) {
689 dev_err(&ddata->pdev->dev,
690 "error disabling panel, issuing HW reset\n");
691 dsicm_hw_reset(ddata);
694 in->ops.dsi->disable(in, true, false);
696 ddata->enabled = 0;
699 static int dsicm_panel_reset(struct panel_drv_data *ddata)
701 dev_err(&ddata->pdev->dev, "performing LCD reset\n");
703 dsicm_power_off(ddata);
704 dsicm_hw_reset(ddata);
705 return dsicm_power_on(ddata);
708 static int dsicm_connect(struct omap_dss_device *dssdev)
710 struct panel_drv_data *ddata = to_panel_data(dssdev);
711 struct omap_dss_device *in = ddata->in;
712 struct device *dev = &ddata->pdev->dev;
713 int r;
715 if (omapdss_device_is_connected(dssdev))
716 return 0;
718 r = in->ops.dsi->connect(in, dssdev);
719 if (r) {
720 dev_err(dev, "Failed to connect to video source\n");
721 return r;
724 r = in->ops.dsi->request_vc(ddata->in, &ddata->channel);
725 if (r) {
726 dev_err(dev, "failed to get virtual channel\n");
727 goto err_req_vc;
730 r = in->ops.dsi->set_vc_id(ddata->in, ddata->channel, TCH);
731 if (r) {
732 dev_err(dev, "failed to set VC_ID\n");
733 goto err_vc_id;
736 return 0;
738 err_vc_id:
739 in->ops.dsi->release_vc(ddata->in, ddata->channel);
740 err_req_vc:
741 in->ops.dsi->disconnect(in, dssdev);
742 return r;
745 static void dsicm_disconnect(struct omap_dss_device *dssdev)
747 struct panel_drv_data *ddata = to_panel_data(dssdev);
748 struct omap_dss_device *in = ddata->in;
750 if (!omapdss_device_is_connected(dssdev))
751 return;
753 in->ops.dsi->release_vc(in, ddata->channel);
754 in->ops.dsi->disconnect(in, dssdev);
757 static int dsicm_enable(struct omap_dss_device *dssdev)
759 struct panel_drv_data *ddata = to_panel_data(dssdev);
760 struct omap_dss_device *in = ddata->in;
761 int r;
763 dev_dbg(&ddata->pdev->dev, "enable\n");
765 mutex_lock(&ddata->lock);
767 if (!omapdss_device_is_connected(dssdev)) {
768 r = -ENODEV;
769 goto err;
772 if (omapdss_device_is_enabled(dssdev)) {
773 r = 0;
774 goto err;
777 in->ops.dsi->bus_lock(in);
779 r = dsicm_power_on(ddata);
781 in->ops.dsi->bus_unlock(in);
783 if (r)
784 goto err;
786 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
788 mutex_unlock(&ddata->lock);
790 return 0;
791 err:
792 dev_dbg(&ddata->pdev->dev, "enable failed\n");
793 mutex_unlock(&ddata->lock);
794 return r;
797 static void dsicm_disable(struct omap_dss_device *dssdev)
799 struct panel_drv_data *ddata = to_panel_data(dssdev);
800 struct omap_dss_device *in = ddata->in;
801 int r;
803 dev_dbg(&ddata->pdev->dev, "disable\n");
805 mutex_lock(&ddata->lock);
807 dsicm_cancel_ulps_work(ddata);
809 in->ops.dsi->bus_lock(in);
811 if (omapdss_device_is_enabled(dssdev)) {
812 r = dsicm_wake_up(ddata);
813 if (!r)
814 dsicm_power_off(ddata);
817 in->ops.dsi->bus_unlock(in);
819 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
821 mutex_unlock(&ddata->lock);
824 static void dsicm_framedone_cb(int err, void *data)
826 struct panel_drv_data *ddata = data;
827 struct omap_dss_device *in = ddata->in;
829 dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err);
830 in->ops.dsi->bus_unlock(ddata->in);
833 static irqreturn_t dsicm_te_isr(int irq, void *data)
835 struct panel_drv_data *ddata = data;
836 struct omap_dss_device *in = ddata->in;
837 int old;
838 int r;
840 old = atomic_cmpxchg(&ddata->do_update, 1, 0);
842 if (old) {
843 cancel_delayed_work(&ddata->te_timeout_work);
845 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
846 ddata);
847 if (r)
848 goto err;
851 return IRQ_HANDLED;
852 err:
853 dev_err(&ddata->pdev->dev, "start update failed\n");
854 in->ops.dsi->bus_unlock(in);
855 return IRQ_HANDLED;
858 static void dsicm_te_timeout_work_callback(struct work_struct *work)
860 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
861 te_timeout_work.work);
862 struct omap_dss_device *in = ddata->in;
864 dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n");
866 atomic_set(&ddata->do_update, 0);
867 in->ops.dsi->bus_unlock(in);
870 static int dsicm_update(struct omap_dss_device *dssdev,
871 u16 x, u16 y, u16 w, u16 h)
873 struct panel_drv_data *ddata = to_panel_data(dssdev);
874 struct omap_dss_device *in = ddata->in;
875 int r;
877 dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
879 mutex_lock(&ddata->lock);
880 in->ops.dsi->bus_lock(in);
882 r = dsicm_wake_up(ddata);
883 if (r)
884 goto err;
886 if (!ddata->enabled) {
887 r = 0;
888 goto err;
891 /* XXX no need to send this every frame, but dsi break if not done */
892 r = dsicm_set_update_window(ddata, 0, 0,
893 dssdev->panel.timings.x_res,
894 dssdev->panel.timings.y_res);
895 if (r)
896 goto err;
898 if (ddata->te_enabled && gpio_is_valid(ddata->ext_te_gpio)) {
899 schedule_delayed_work(&ddata->te_timeout_work,
900 msecs_to_jiffies(250));
901 atomic_set(&ddata->do_update, 1);
902 } else {
903 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
904 ddata);
905 if (r)
906 goto err;
909 /* note: no bus_unlock here. unlock is in framedone_cb */
910 mutex_unlock(&ddata->lock);
911 return 0;
912 err:
913 in->ops.dsi->bus_unlock(in);
914 mutex_unlock(&ddata->lock);
915 return r;
918 static int dsicm_sync(struct omap_dss_device *dssdev)
920 struct panel_drv_data *ddata = to_panel_data(dssdev);
921 struct omap_dss_device *in = ddata->in;
923 dev_dbg(&ddata->pdev->dev, "sync\n");
925 mutex_lock(&ddata->lock);
926 in->ops.dsi->bus_lock(in);
927 in->ops.dsi->bus_unlock(in);
928 mutex_unlock(&ddata->lock);
930 dev_dbg(&ddata->pdev->dev, "sync done\n");
932 return 0;
935 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable)
937 struct omap_dss_device *in = ddata->in;
938 int r;
940 if (enable)
941 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0);
942 else
943 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF);
945 if (!gpio_is_valid(ddata->ext_te_gpio))
946 in->ops.dsi->enable_te(in, enable);
948 /* possible panel bug */
949 msleep(100);
951 return r;
954 static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable)
956 struct panel_drv_data *ddata = to_panel_data(dssdev);
957 struct omap_dss_device *in = ddata->in;
958 int r;
960 mutex_lock(&ddata->lock);
962 if (ddata->te_enabled == enable)
963 goto end;
965 in->ops.dsi->bus_lock(in);
967 if (ddata->enabled) {
968 r = dsicm_wake_up(ddata);
969 if (r)
970 goto err;
972 r = _dsicm_enable_te(ddata, enable);
973 if (r)
974 goto err;
977 ddata->te_enabled = enable;
979 in->ops.dsi->bus_unlock(in);
980 end:
981 mutex_unlock(&ddata->lock);
983 return 0;
984 err:
985 in->ops.dsi->bus_unlock(in);
986 mutex_unlock(&ddata->lock);
988 return r;
991 static int dsicm_get_te(struct omap_dss_device *dssdev)
993 struct panel_drv_data *ddata = to_panel_data(dssdev);
994 int r;
996 mutex_lock(&ddata->lock);
997 r = ddata->te_enabled;
998 mutex_unlock(&ddata->lock);
1000 return r;
1003 static int dsicm_memory_read(struct omap_dss_device *dssdev,
1004 void *buf, size_t size,
1005 u16 x, u16 y, u16 w, u16 h)
1007 struct panel_drv_data *ddata = to_panel_data(dssdev);
1008 struct omap_dss_device *in = ddata->in;
1009 int r;
1010 int first = 1;
1011 int plen;
1012 unsigned buf_used = 0;
1014 if (size < w * h * 3)
1015 return -ENOMEM;
1017 mutex_lock(&ddata->lock);
1019 if (!ddata->enabled) {
1020 r = -ENODEV;
1021 goto err1;
1024 size = min(w * h * 3,
1025 dssdev->panel.timings.x_res *
1026 dssdev->panel.timings.y_res * 3);
1028 in->ops.dsi->bus_lock(in);
1030 r = dsicm_wake_up(ddata);
1031 if (r)
1032 goto err2;
1034 /* plen 1 or 2 goes into short packet. until checksum error is fixed,
1035 * use short packets. plen 32 works, but bigger packets seem to cause
1036 * an error. */
1037 if (size % 2)
1038 plen = 1;
1039 else
1040 plen = 2;
1042 dsicm_set_update_window(ddata, x, y, w, h);
1044 r = in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, plen);
1045 if (r)
1046 goto err2;
1048 while (buf_used < size) {
1049 u8 dcs_cmd = first ? 0x2e : 0x3e;
1050 first = 0;
1052 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd,
1053 buf + buf_used, size - buf_used);
1055 if (r < 0) {
1056 dev_err(dssdev->dev, "read error\n");
1057 goto err3;
1060 buf_used += r;
1062 if (r < plen) {
1063 dev_err(&ddata->pdev->dev, "short read\n");
1064 break;
1067 if (signal_pending(current)) {
1068 dev_err(&ddata->pdev->dev, "signal pending, "
1069 "aborting memory read\n");
1070 r = -ERESTARTSYS;
1071 goto err3;
1075 r = buf_used;
1077 err3:
1078 in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, 1);
1079 err2:
1080 in->ops.dsi->bus_unlock(in);
1081 err1:
1082 mutex_unlock(&ddata->lock);
1083 return r;
1086 static void dsicm_ulps_work(struct work_struct *work)
1088 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
1089 ulps_work.work);
1090 struct omap_dss_device *dssdev = &ddata->dssdev;
1091 struct omap_dss_device *in = ddata->in;
1093 mutex_lock(&ddata->lock);
1095 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) {
1096 mutex_unlock(&ddata->lock);
1097 return;
1100 in->ops.dsi->bus_lock(in);
1102 dsicm_enter_ulps(ddata);
1104 in->ops.dsi->bus_unlock(in);
1105 mutex_unlock(&ddata->lock);
1108 static struct omap_dss_driver dsicm_ops = {
1109 .connect = dsicm_connect,
1110 .disconnect = dsicm_disconnect,
1112 .enable = dsicm_enable,
1113 .disable = dsicm_disable,
1115 .update = dsicm_update,
1116 .sync = dsicm_sync,
1118 .get_resolution = dsicm_get_resolution,
1119 .get_recommended_bpp = omapdss_default_get_recommended_bpp,
1121 .enable_te = dsicm_enable_te,
1122 .get_te = dsicm_get_te,
1124 .memory_read = dsicm_memory_read,
1127 static int dsicm_probe_of(struct platform_device *pdev)
1129 struct device_node *node = pdev->dev.of_node;
1130 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1131 struct omap_dss_device *in;
1132 int gpio;
1134 gpio = of_get_named_gpio(node, "reset-gpios", 0);
1135 if (!gpio_is_valid(gpio)) {
1136 dev_err(&pdev->dev, "failed to parse reset gpio\n");
1137 return gpio;
1139 ddata->reset_gpio = gpio;
1141 gpio = of_get_named_gpio(node, "te-gpios", 0);
1142 if (gpio_is_valid(gpio) || gpio == -ENOENT) {
1143 ddata->ext_te_gpio = gpio;
1144 } else {
1145 dev_err(&pdev->dev, "failed to parse TE gpio\n");
1146 return gpio;
1149 in = omapdss_of_find_source_for_first_ep(node);
1150 if (IS_ERR(in)) {
1151 dev_err(&pdev->dev, "failed to find video source\n");
1152 return PTR_ERR(in);
1155 ddata->in = in;
1157 /* TODO: ulps, backlight */
1159 return 0;
1162 static int dsicm_probe(struct platform_device *pdev)
1164 struct backlight_properties props;
1165 struct panel_drv_data *ddata;
1166 struct backlight_device *bldev = NULL;
1167 struct device *dev = &pdev->dev;
1168 struct omap_dss_device *dssdev;
1169 int r;
1171 dev_dbg(dev, "probe\n");
1173 if (!pdev->dev.of_node)
1174 return -ENODEV;
1176 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
1177 if (!ddata)
1178 return -ENOMEM;
1180 platform_set_drvdata(pdev, ddata);
1181 ddata->pdev = pdev;
1183 r = dsicm_probe_of(pdev);
1184 if (r)
1185 return r;
1187 ddata->timings.x_res = 864;
1188 ddata->timings.y_res = 480;
1189 ddata->timings.pixelclock = 864 * 480 * 60;
1191 dssdev = &ddata->dssdev;
1192 dssdev->dev = dev;
1193 dssdev->driver = &dsicm_ops;
1194 dssdev->panel.timings = ddata->timings;
1195 dssdev->type = OMAP_DISPLAY_TYPE_DSI;
1196 dssdev->owner = THIS_MODULE;
1198 dssdev->panel.dsi_pix_fmt = OMAP_DSS_DSI_FMT_RGB888;
1199 dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
1200 OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
1202 r = omapdss_register_display(dssdev);
1203 if (r) {
1204 dev_err(dev, "Failed to register panel\n");
1205 goto err_reg;
1208 mutex_init(&ddata->lock);
1210 atomic_set(&ddata->do_update, 0);
1212 if (gpio_is_valid(ddata->reset_gpio)) {
1213 r = devm_gpio_request_one(dev, ddata->reset_gpio,
1214 GPIOF_OUT_INIT_LOW, "taal rst");
1215 if (r) {
1216 dev_err(dev, "failed to request reset gpio\n");
1217 return r;
1221 if (gpio_is_valid(ddata->ext_te_gpio)) {
1222 r = devm_gpio_request_one(dev, ddata->ext_te_gpio,
1223 GPIOF_IN, "taal irq");
1224 if (r) {
1225 dev_err(dev, "GPIO request failed\n");
1226 return r;
1229 r = devm_request_irq(dev, gpio_to_irq(ddata->ext_te_gpio),
1230 dsicm_te_isr,
1231 IRQF_TRIGGER_RISING,
1232 "taal vsync", ddata);
1234 if (r) {
1235 dev_err(dev, "IRQ request failed\n");
1236 return r;
1239 INIT_DEFERRABLE_WORK(&ddata->te_timeout_work,
1240 dsicm_te_timeout_work_callback);
1242 dev_dbg(dev, "Using GPIO TE\n");
1245 INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work);
1247 dsicm_hw_reset(ddata);
1249 if (ddata->use_dsi_backlight) {
1250 memset(&props, 0, sizeof(struct backlight_properties));
1251 props.max_brightness = 255;
1253 props.type = BACKLIGHT_RAW;
1254 bldev = backlight_device_register(dev_name(dev),
1255 dev, ddata, &dsicm_bl_ops, &props);
1256 if (IS_ERR(bldev)) {
1257 r = PTR_ERR(bldev);
1258 goto err_reg;
1261 ddata->bldev = bldev;
1263 bldev->props.fb_blank = FB_BLANK_UNBLANK;
1264 bldev->props.power = FB_BLANK_UNBLANK;
1265 bldev->props.brightness = 255;
1267 dsicm_bl_update_status(bldev);
1270 r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
1271 if (r) {
1272 dev_err(dev, "failed to create sysfs files\n");
1273 goto err_sysfs_create;
1276 return 0;
1278 err_sysfs_create:
1279 if (bldev != NULL)
1280 backlight_device_unregister(bldev);
1281 err_reg:
1282 return r;
1285 static int __exit dsicm_remove(struct platform_device *pdev)
1287 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1288 struct omap_dss_device *dssdev = &ddata->dssdev;
1289 struct backlight_device *bldev;
1291 dev_dbg(&pdev->dev, "remove\n");
1293 omapdss_unregister_display(dssdev);
1295 dsicm_disable(dssdev);
1296 dsicm_disconnect(dssdev);
1298 sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group);
1300 bldev = ddata->bldev;
1301 if (bldev != NULL) {
1302 bldev->props.power = FB_BLANK_POWERDOWN;
1303 dsicm_bl_update_status(bldev);
1304 backlight_device_unregister(bldev);
1307 omap_dss_put_device(ddata->in);
1309 dsicm_cancel_ulps_work(ddata);
1311 /* reset, to be sure that the panel is in a valid state */
1312 dsicm_hw_reset(ddata);
1314 return 0;
1317 static const struct of_device_id dsicm_of_match[] = {
1318 { .compatible = "omapdss,panel-dsi-cm", },
1322 MODULE_DEVICE_TABLE(of, dsicm_of_match);
1324 static struct platform_driver dsicm_driver = {
1325 .probe = dsicm_probe,
1326 .remove = __exit_p(dsicm_remove),
1327 .driver = {
1328 .name = "panel-dsi-cm",
1329 .of_match_table = dsicm_of_match,
1330 .suppress_bind_attrs = true,
1334 module_platform_driver(dsicm_driver);
1336 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
1337 MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
1338 MODULE_LICENSE("GPL");