2 * Generic DSI Command Mode panel driver
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
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.
14 #include <linux/backlight.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/interrupt.h>
18 #include <linux/jiffies.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched/signal.h>
22 #include <linux/slab.h>
23 #include <linux/workqueue.h>
24 #include <linux/of_device.h>
25 #include <linux/regulator/consumer.h>
27 #include <drm/drm_connector.h>
29 #include <video/mipi_display.h>
30 #include <video/of_display_timing.h>
32 #include "../dss/omapdss.h"
34 /* DSI Virtual channel. Hardcoded for now. */
37 #define DCS_READ_NUM_ERRORS 0x05
38 #define DCS_BRIGHTNESS 0x51
39 #define DCS_CTRL_DISPLAY 0x53
40 #define DCS_GET_ID1 0xda
41 #define DCS_GET_ID2 0xdb
42 #define DCS_GET_ID3 0xdc
44 struct panel_drv_data {
45 struct omap_dss_device dssdev;
46 struct omap_dss_device *src;
50 struct platform_device *pdev;
54 struct backlight_device *bldev;
55 struct backlight_device *extbldev;
57 unsigned long hw_guard_end; /* next value of jiffies when we can
58 * issue the next sleep in/out command
60 unsigned long hw_guard_wait; /* max guard time in jiffies */
62 /* panel HW configuration from DT or platform data */
63 struct gpio_desc *reset_gpio;
64 struct gpio_desc *ext_te_gpio;
66 struct regulator *vpnl;
67 struct regulator *vddi;
69 bool use_dsi_backlight;
74 struct omap_dsi_pin_config pin_config;
76 /* runtime variables */
84 struct delayed_work te_timeout_work;
88 struct workqueue_struct *workqueue;
91 unsigned int ulps_timeout;
92 struct delayed_work ulps_work;
95 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
97 static irqreturn_t dsicm_te_isr(int irq, void *data);
98 static void dsicm_te_timeout_work_callback(struct work_struct *work);
99 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable);
101 static int dsicm_panel_reset(struct panel_drv_data *ddata);
103 static void dsicm_ulps_work(struct work_struct *work);
105 static void dsicm_bl_power(struct panel_drv_data *ddata, bool enable)
107 struct backlight_device *backlight;
110 backlight = ddata->bldev;
111 else if (ddata->extbldev)
112 backlight = ddata->extbldev;
117 backlight->props.fb_blank = FB_BLANK_UNBLANK;
118 backlight->props.state = ~(BL_CORE_FBBLANK | BL_CORE_SUSPENDED);
119 backlight->props.power = FB_BLANK_UNBLANK;
121 backlight->props.fb_blank = FB_BLANK_NORMAL;
122 backlight->props.power = FB_BLANK_POWERDOWN;
123 backlight->props.state |= BL_CORE_FBBLANK | BL_CORE_SUSPENDED;
126 backlight_update_status(backlight);
129 static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec)
131 ddata->hw_guard_wait = msecs_to_jiffies(guard_msec);
132 ddata->hw_guard_end = jiffies + ddata->hw_guard_wait;
135 static void hw_guard_wait(struct panel_drv_data *ddata)
137 unsigned long wait = ddata->hw_guard_end - jiffies;
139 if ((long)wait > 0 && wait <= ddata->hw_guard_wait) {
140 set_current_state(TASK_UNINTERRUPTIBLE);
141 schedule_timeout(wait);
145 static int dsicm_dcs_read_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 *data)
147 struct omap_dss_device *src = ddata->src;
151 r = src->ops->dsi.dcs_read(src, ddata->channel, dcs_cmd, buf, 1);
161 static int dsicm_dcs_write_0(struct panel_drv_data *ddata, u8 dcs_cmd)
163 struct omap_dss_device *src = ddata->src;
165 return src->ops->dsi.dcs_write(src, ddata->channel, &dcs_cmd, 1);
168 static int dsicm_dcs_write_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 param)
170 struct omap_dss_device *src = ddata->src;
171 u8 buf[2] = { dcs_cmd, param };
173 return src->ops->dsi.dcs_write(src, ddata->channel, buf, 2);
176 static int dsicm_sleep_in(struct panel_drv_data *ddata)
179 struct omap_dss_device *src = ddata->src;
183 hw_guard_wait(ddata);
185 cmd = MIPI_DCS_ENTER_SLEEP_MODE;
186 r = src->ops->dsi.dcs_write_nosync(src, ddata->channel, &cmd, 1);
190 hw_guard_start(ddata, 120);
192 usleep_range(5000, 10000);
197 static int dsicm_sleep_out(struct panel_drv_data *ddata)
201 hw_guard_wait(ddata);
203 r = dsicm_dcs_write_0(ddata, MIPI_DCS_EXIT_SLEEP_MODE);
207 hw_guard_start(ddata, 120);
209 usleep_range(5000, 10000);
214 static int dsicm_get_id(struct panel_drv_data *ddata, u8 *id1, u8 *id2, u8 *id3)
218 r = dsicm_dcs_read_1(ddata, DCS_GET_ID1, id1);
221 r = dsicm_dcs_read_1(ddata, DCS_GET_ID2, id2);
224 r = dsicm_dcs_read_1(ddata, DCS_GET_ID3, id3);
231 static int dsicm_set_update_window(struct panel_drv_data *ddata,
232 u16 x, u16 y, u16 w, u16 h)
234 struct omap_dss_device *src = ddata->src;
242 buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS;
243 buf[1] = (x1 >> 8) & 0xff;
244 buf[2] = (x1 >> 0) & 0xff;
245 buf[3] = (x2 >> 8) & 0xff;
246 buf[4] = (x2 >> 0) & 0xff;
248 r = src->ops->dsi.dcs_write_nosync(src, ddata->channel, buf, sizeof(buf));
252 buf[0] = MIPI_DCS_SET_PAGE_ADDRESS;
253 buf[1] = (y1 >> 8) & 0xff;
254 buf[2] = (y1 >> 0) & 0xff;
255 buf[3] = (y2 >> 8) & 0xff;
256 buf[4] = (y2 >> 0) & 0xff;
258 r = src->ops->dsi.dcs_write_nosync(src, ddata->channel, buf, sizeof(buf));
262 src->ops->dsi.bta_sync(src, ddata->channel);
267 static void dsicm_queue_ulps_work(struct panel_drv_data *ddata)
269 if (ddata->ulps_timeout > 0)
270 queue_delayed_work(ddata->workqueue, &ddata->ulps_work,
271 msecs_to_jiffies(ddata->ulps_timeout));
274 static void dsicm_cancel_ulps_work(struct panel_drv_data *ddata)
276 cancel_delayed_work(&ddata->ulps_work);
279 static int dsicm_enter_ulps(struct panel_drv_data *ddata)
281 struct omap_dss_device *src = ddata->src;
284 if (ddata->ulps_enabled)
287 dsicm_cancel_ulps_work(ddata);
289 r = _dsicm_enable_te(ddata, false);
293 if (ddata->ext_te_gpio)
294 disable_irq(gpiod_to_irq(ddata->ext_te_gpio));
296 src->ops->dsi.disable(src, false, true);
298 ddata->ulps_enabled = true;
303 dev_err(&ddata->pdev->dev, "enter ULPS failed");
304 dsicm_panel_reset(ddata);
306 ddata->ulps_enabled = false;
308 dsicm_queue_ulps_work(ddata);
313 static int dsicm_exit_ulps(struct panel_drv_data *ddata)
315 struct omap_dss_device *src = ddata->src;
318 if (!ddata->ulps_enabled)
321 src->ops->enable(src);
322 src->ops->dsi.enable_hs(src, ddata->channel, true);
324 r = _dsicm_enable_te(ddata, true);
326 dev_err(&ddata->pdev->dev, "failed to re-enable TE");
330 if (ddata->ext_te_gpio)
331 enable_irq(gpiod_to_irq(ddata->ext_te_gpio));
333 dsicm_queue_ulps_work(ddata);
335 ddata->ulps_enabled = false;
340 dev_err(&ddata->pdev->dev, "failed to exit ULPS");
342 r = dsicm_panel_reset(ddata);
344 if (ddata->ext_te_gpio)
345 enable_irq(gpiod_to_irq(ddata->ext_te_gpio));
346 ddata->ulps_enabled = false;
349 dsicm_queue_ulps_work(ddata);
354 static int dsicm_wake_up(struct panel_drv_data *ddata)
356 if (ddata->ulps_enabled)
357 return dsicm_exit_ulps(ddata);
359 dsicm_cancel_ulps_work(ddata);
360 dsicm_queue_ulps_work(ddata);
364 static int dsicm_bl_update_status(struct backlight_device *dev)
366 struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
367 struct omap_dss_device *src = ddata->src;
371 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
372 dev->props.power == FB_BLANK_UNBLANK)
373 level = dev->props.brightness;
377 dev_dbg(&ddata->pdev->dev, "update brightness to %d\n", level);
379 mutex_lock(&ddata->lock);
381 if (ddata->enabled) {
382 src->ops->dsi.bus_lock(src);
384 r = dsicm_wake_up(ddata);
386 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, level);
388 src->ops->dsi.bus_unlock(src);
391 mutex_unlock(&ddata->lock);
396 static int dsicm_bl_get_intensity(struct backlight_device *dev)
398 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
399 dev->props.power == FB_BLANK_UNBLANK)
400 return dev->props.brightness;
405 static const struct backlight_ops dsicm_bl_ops = {
406 .get_brightness = dsicm_bl_get_intensity,
407 .update_status = dsicm_bl_update_status,
410 static ssize_t dsicm_num_errors_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
413 struct platform_device *pdev = to_platform_device(dev);
414 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
415 struct omap_dss_device *src = ddata->src;
419 mutex_lock(&ddata->lock);
421 if (ddata->enabled) {
422 src->ops->dsi.bus_lock(src);
424 r = dsicm_wake_up(ddata);
426 r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS,
429 src->ops->dsi.bus_unlock(src);
434 mutex_unlock(&ddata->lock);
439 return snprintf(buf, PAGE_SIZE, "%d\n", errors);
442 static ssize_t dsicm_hw_revision_show(struct device *dev,
443 struct device_attribute *attr, char *buf)
445 struct platform_device *pdev = to_platform_device(dev);
446 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
447 struct omap_dss_device *src = ddata->src;
451 mutex_lock(&ddata->lock);
453 if (ddata->enabled) {
454 src->ops->dsi.bus_lock(src);
456 r = dsicm_wake_up(ddata);
458 r = dsicm_get_id(ddata, &id1, &id2, &id3);
460 src->ops->dsi.bus_unlock(src);
465 mutex_unlock(&ddata->lock);
470 return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
473 static ssize_t dsicm_store_ulps(struct device *dev,
474 struct device_attribute *attr,
475 const char *buf, size_t count)
477 struct platform_device *pdev = to_platform_device(dev);
478 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
479 struct omap_dss_device *src = ddata->src;
483 r = kstrtoul(buf, 0, &t);
487 mutex_lock(&ddata->lock);
489 if (ddata->enabled) {
490 src->ops->dsi.bus_lock(src);
493 r = dsicm_enter_ulps(ddata);
495 r = dsicm_wake_up(ddata);
497 src->ops->dsi.bus_unlock(src);
500 mutex_unlock(&ddata->lock);
508 static ssize_t dsicm_show_ulps(struct device *dev,
509 struct device_attribute *attr,
512 struct platform_device *pdev = to_platform_device(dev);
513 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
516 mutex_lock(&ddata->lock);
517 t = ddata->ulps_enabled;
518 mutex_unlock(&ddata->lock);
520 return snprintf(buf, PAGE_SIZE, "%u\n", t);
523 static ssize_t dsicm_store_ulps_timeout(struct device *dev,
524 struct device_attribute *attr,
525 const char *buf, size_t count)
527 struct platform_device *pdev = to_platform_device(dev);
528 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
529 struct omap_dss_device *src = ddata->src;
533 r = kstrtoul(buf, 0, &t);
537 mutex_lock(&ddata->lock);
538 ddata->ulps_timeout = t;
540 if (ddata->enabled) {
541 /* dsicm_wake_up will restart the timer */
542 src->ops->dsi.bus_lock(src);
543 r = dsicm_wake_up(ddata);
544 src->ops->dsi.bus_unlock(src);
547 mutex_unlock(&ddata->lock);
555 static ssize_t dsicm_show_ulps_timeout(struct device *dev,
556 struct device_attribute *attr,
559 struct platform_device *pdev = to_platform_device(dev);
560 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
563 mutex_lock(&ddata->lock);
564 t = ddata->ulps_timeout;
565 mutex_unlock(&ddata->lock);
567 return snprintf(buf, PAGE_SIZE, "%u\n", t);
570 static DEVICE_ATTR(num_dsi_errors, S_IRUGO, dsicm_num_errors_show, NULL);
571 static DEVICE_ATTR(hw_revision, S_IRUGO, dsicm_hw_revision_show, NULL);
572 static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
573 dsicm_show_ulps, dsicm_store_ulps);
574 static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
575 dsicm_show_ulps_timeout, dsicm_store_ulps_timeout);
577 static struct attribute *dsicm_attrs[] = {
578 &dev_attr_num_dsi_errors.attr,
579 &dev_attr_hw_revision.attr,
581 &dev_attr_ulps_timeout.attr,
585 static const struct attribute_group dsicm_attr_group = {
586 .attrs = dsicm_attrs,
589 static void dsicm_hw_reset(struct panel_drv_data *ddata)
591 gpiod_set_value(ddata->reset_gpio, 1);
593 /* reset the panel */
594 gpiod_set_value(ddata->reset_gpio, 0);
597 gpiod_set_value(ddata->reset_gpio, 1);
598 /* wait after releasing reset */
599 usleep_range(5000, 10000);
602 static int dsicm_power_on(struct panel_drv_data *ddata)
604 struct omap_dss_device *src = ddata->src;
607 struct omap_dss_dsi_config dsi_config = {
608 .mode = OMAP_DSS_DSI_CMD_MODE,
609 .pixel_format = OMAP_DSS_DSI_FMT_RGB888,
611 .hs_clk_min = 150000000,
612 .hs_clk_max = 300000000,
613 .lp_clk_min = 7000000,
614 .lp_clk_max = 10000000,
618 r = regulator_enable(ddata->vpnl);
620 dev_err(&ddata->pdev->dev,
621 "failed to enable VPNL: %d\n", r);
627 r = regulator_enable(ddata->vddi);
629 dev_err(&ddata->pdev->dev,
630 "failed to enable VDDI: %d\n", r);
635 if (ddata->pin_config.num_pins > 0) {
636 r = src->ops->dsi.configure_pins(src, &ddata->pin_config);
638 dev_err(&ddata->pdev->dev,
639 "failed to configure DSI pins\n");
644 r = src->ops->dsi.set_config(src, &dsi_config);
646 dev_err(&ddata->pdev->dev, "failed to configure DSI\n");
650 src->ops->enable(src);
652 dsicm_hw_reset(ddata);
654 src->ops->dsi.enable_hs(src, ddata->channel, false);
656 r = dsicm_sleep_out(ddata);
660 r = dsicm_get_id(ddata, &id1, &id2, &id3);
664 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff);
668 r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY,
669 (1<<2) | (1<<5)); /* BL | BCTRL */
673 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT,
674 MIPI_DCS_PIXEL_FMT_24BIT);
678 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON);
682 r = _dsicm_enable_te(ddata, ddata->te_enabled);
686 r = src->ops->dsi.enable_video_output(src, ddata->channel);
692 if (!ddata->intro_printed) {
693 dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n",
695 ddata->intro_printed = true;
698 src->ops->dsi.enable_hs(src, ddata->channel, true);
702 dev_err(&ddata->pdev->dev, "error while enabling panel, issuing HW reset\n");
704 dsicm_hw_reset(ddata);
706 src->ops->dsi.disable(src, true, false);
709 regulator_disable(ddata->vddi);
712 regulator_disable(ddata->vpnl);
717 static void dsicm_power_off(struct panel_drv_data *ddata)
719 struct omap_dss_device *src = ddata->src;
722 src->ops->dsi.disable_video_output(src, ddata->channel);
724 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF);
726 r = dsicm_sleep_in(ddata);
729 dev_err(&ddata->pdev->dev,
730 "error disabling panel, issuing HW reset\n");
731 dsicm_hw_reset(ddata);
734 src->ops->dsi.disable(src, true, false);
737 regulator_disable(ddata->vddi);
739 regulator_disable(ddata->vpnl);
744 static int dsicm_panel_reset(struct panel_drv_data *ddata)
746 dev_err(&ddata->pdev->dev, "performing LCD reset\n");
748 dsicm_power_off(ddata);
749 dsicm_hw_reset(ddata);
750 return dsicm_power_on(ddata);
753 static int dsicm_connect(struct omap_dss_device *src,
754 struct omap_dss_device *dst)
756 struct panel_drv_data *ddata = to_panel_data(dst);
757 struct device *dev = &ddata->pdev->dev;
760 r = src->ops->dsi.request_vc(src, &ddata->channel);
762 dev_err(dev, "failed to get virtual channel\n");
766 r = src->ops->dsi.set_vc_id(src, ddata->channel, TCH);
768 dev_err(dev, "failed to set VC_ID\n");
769 src->ops->dsi.release_vc(src, ddata->channel);
777 static void dsicm_disconnect(struct omap_dss_device *src,
778 struct omap_dss_device *dst)
780 struct panel_drv_data *ddata = to_panel_data(dst);
782 src->ops->dsi.release_vc(src, ddata->channel);
786 static void dsicm_enable(struct omap_dss_device *dssdev)
788 struct panel_drv_data *ddata = to_panel_data(dssdev);
789 struct omap_dss_device *src = ddata->src;
792 mutex_lock(&ddata->lock);
794 src->ops->dsi.bus_lock(src);
796 r = dsicm_power_on(ddata);
798 src->ops->dsi.bus_unlock(src);
803 mutex_unlock(&ddata->lock);
805 dsicm_bl_power(ddata, true);
809 dev_dbg(&ddata->pdev->dev, "enable failed (%d)\n", r);
810 mutex_unlock(&ddata->lock);
813 static void dsicm_disable(struct omap_dss_device *dssdev)
815 struct panel_drv_data *ddata = to_panel_data(dssdev);
816 struct omap_dss_device *src = ddata->src;
819 dsicm_bl_power(ddata, false);
821 mutex_lock(&ddata->lock);
823 dsicm_cancel_ulps_work(ddata);
825 src->ops->dsi.bus_lock(src);
827 r = dsicm_wake_up(ddata);
829 dsicm_power_off(ddata);
831 src->ops->dsi.bus_unlock(src);
833 mutex_unlock(&ddata->lock);
836 static void dsicm_framedone_cb(int err, void *data)
838 struct panel_drv_data *ddata = data;
839 struct omap_dss_device *src = ddata->src;
841 dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err);
842 src->ops->dsi.bus_unlock(src);
845 static irqreturn_t dsicm_te_isr(int irq, void *data)
847 struct panel_drv_data *ddata = data;
848 struct omap_dss_device *src = ddata->src;
852 old = atomic_cmpxchg(&ddata->do_update, 1, 0);
855 cancel_delayed_work(&ddata->te_timeout_work);
857 r = src->ops->dsi.update(src, ddata->channel, dsicm_framedone_cb,
865 dev_err(&ddata->pdev->dev, "start update failed\n");
866 src->ops->dsi.bus_unlock(src);
870 static void dsicm_te_timeout_work_callback(struct work_struct *work)
872 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
873 te_timeout_work.work);
874 struct omap_dss_device *src = ddata->src;
876 dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n");
878 atomic_set(&ddata->do_update, 0);
879 src->ops->dsi.bus_unlock(src);
882 static int dsicm_update(struct omap_dss_device *dssdev,
883 u16 x, u16 y, u16 w, u16 h)
885 struct panel_drv_data *ddata = to_panel_data(dssdev);
886 struct omap_dss_device *src = ddata->src;
889 dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
891 mutex_lock(&ddata->lock);
892 src->ops->dsi.bus_lock(src);
894 r = dsicm_wake_up(ddata);
898 if (!ddata->enabled) {
903 /* XXX no need to send this every frame, but dsi break if not done */
904 r = dsicm_set_update_window(ddata, 0, 0, ddata->vm.hactive,
909 if (ddata->te_enabled && ddata->ext_te_gpio) {
910 schedule_delayed_work(&ddata->te_timeout_work,
911 msecs_to_jiffies(250));
912 atomic_set(&ddata->do_update, 1);
914 r = src->ops->dsi.update(src, ddata->channel, dsicm_framedone_cb,
920 /* note: no bus_unlock here. unlock is src framedone_cb */
921 mutex_unlock(&ddata->lock);
924 src->ops->dsi.bus_unlock(src);
925 mutex_unlock(&ddata->lock);
929 static int dsicm_sync(struct omap_dss_device *dssdev)
931 struct panel_drv_data *ddata = to_panel_data(dssdev);
932 struct omap_dss_device *src = ddata->src;
934 dev_dbg(&ddata->pdev->dev, "sync\n");
936 mutex_lock(&ddata->lock);
937 src->ops->dsi.bus_lock(src);
938 src->ops->dsi.bus_unlock(src);
939 mutex_unlock(&ddata->lock);
941 dev_dbg(&ddata->pdev->dev, "sync done\n");
946 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable)
948 struct omap_dss_device *src = ddata->src;
952 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0);
954 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF);
956 if (!ddata->ext_te_gpio)
957 src->ops->dsi.enable_te(src, enable);
959 /* possible panel bug */
965 static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable)
967 struct panel_drv_data *ddata = to_panel_data(dssdev);
968 struct omap_dss_device *src = ddata->src;
971 mutex_lock(&ddata->lock);
973 if (ddata->te_enabled == enable)
976 src->ops->dsi.bus_lock(src);
978 if (ddata->enabled) {
979 r = dsicm_wake_up(ddata);
983 r = _dsicm_enable_te(ddata, enable);
988 ddata->te_enabled = enable;
990 src->ops->dsi.bus_unlock(src);
992 mutex_unlock(&ddata->lock);
996 src->ops->dsi.bus_unlock(src);
997 mutex_unlock(&ddata->lock);
1002 static int dsicm_get_te(struct omap_dss_device *dssdev)
1004 struct panel_drv_data *ddata = to_panel_data(dssdev);
1007 mutex_lock(&ddata->lock);
1008 r = ddata->te_enabled;
1009 mutex_unlock(&ddata->lock);
1014 static int dsicm_memory_read(struct omap_dss_device *dssdev,
1015 void *buf, size_t size,
1016 u16 x, u16 y, u16 w, u16 h)
1018 struct panel_drv_data *ddata = to_panel_data(dssdev);
1019 struct omap_dss_device *src = ddata->src;
1023 unsigned int buf_used = 0;
1025 if (size < w * h * 3)
1028 mutex_lock(&ddata->lock);
1030 if (!ddata->enabled) {
1035 size = min((u32)w * h * 3,
1036 ddata->vm.hactive * ddata->vm.vactive * 3);
1038 src->ops->dsi.bus_lock(src);
1040 r = dsicm_wake_up(ddata);
1044 /* plen 1 or 2 goes into short packet. until checksum error is fixed,
1045 * use short packets. plen 32 works, but bigger packets seem to cause
1052 dsicm_set_update_window(ddata, x, y, w, h);
1054 r = src->ops->dsi.set_max_rx_packet_size(src, ddata->channel, plen);
1058 while (buf_used < size) {
1059 u8 dcs_cmd = first ? 0x2e : 0x3e;
1062 r = src->ops->dsi.dcs_read(src, ddata->channel, dcs_cmd,
1063 buf + buf_used, size - buf_used);
1066 dev_err(dssdev->dev, "read error\n");
1073 dev_err(&ddata->pdev->dev, "short read\n");
1077 if (signal_pending(current)) {
1078 dev_err(&ddata->pdev->dev, "signal pending, "
1079 "aborting memory read\n");
1088 src->ops->dsi.set_max_rx_packet_size(src, ddata->channel, 1);
1090 src->ops->dsi.bus_unlock(src);
1092 mutex_unlock(&ddata->lock);
1096 static void dsicm_ulps_work(struct work_struct *work)
1098 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
1100 struct omap_dss_device *dssdev = &ddata->dssdev;
1101 struct omap_dss_device *src = ddata->src;
1103 mutex_lock(&ddata->lock);
1105 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) {
1106 mutex_unlock(&ddata->lock);
1110 src->ops->dsi.bus_lock(src);
1112 dsicm_enter_ulps(ddata);
1114 src->ops->dsi.bus_unlock(src);
1115 mutex_unlock(&ddata->lock);
1118 static int dsicm_get_modes(struct omap_dss_device *dssdev,
1119 struct drm_connector *connector)
1121 struct panel_drv_data *ddata = to_panel_data(dssdev);
1123 connector->display_info.width_mm = ddata->width_mm;
1124 connector->display_info.height_mm = ddata->height_mm;
1126 return omapdss_display_get_modes(connector, &ddata->vm);
1129 static int dsicm_check_timings(struct omap_dss_device *dssdev,
1130 struct drm_display_mode *mode)
1132 struct panel_drv_data *ddata = to_panel_data(dssdev);
1135 if (mode->hdisplay != ddata->vm.hactive)
1138 if (mode->vdisplay != ddata->vm.vactive)
1142 dev_warn(dssdev->dev, "wrong resolution: %d x %d",
1143 mode->hdisplay, mode->vdisplay);
1144 dev_warn(dssdev->dev, "panel resolution: %d x %d",
1145 ddata->vm.hactive, ddata->vm.vactive);
1151 static const struct omap_dss_device_ops dsicm_ops = {
1152 .connect = dsicm_connect,
1153 .disconnect = dsicm_disconnect,
1155 .enable = dsicm_enable,
1156 .disable = dsicm_disable,
1158 .get_modes = dsicm_get_modes,
1159 .check_timings = dsicm_check_timings,
1162 static const struct omap_dss_driver dsicm_dss_driver = {
1163 .update = dsicm_update,
1166 .enable_te = dsicm_enable_te,
1167 .get_te = dsicm_get_te,
1169 .memory_read = dsicm_memory_read,
1172 static int dsicm_probe_of(struct platform_device *pdev)
1174 struct device_node *node = pdev->dev.of_node;
1175 struct device_node *backlight;
1176 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1177 struct display_timing timing;
1180 ddata->reset_gpio = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
1181 if (IS_ERR(ddata->reset_gpio)) {
1182 err = PTR_ERR(ddata->reset_gpio);
1183 dev_err(&pdev->dev, "reset gpio request failed: %d", err);
1187 ddata->ext_te_gpio = devm_gpiod_get_optional(&pdev->dev, "te",
1189 if (IS_ERR(ddata->ext_te_gpio)) {
1190 err = PTR_ERR(ddata->ext_te_gpio);
1191 dev_err(&pdev->dev, "TE gpio request failed: %d", err);
1195 err = of_get_display_timing(node, "panel-timing", &timing);
1197 videomode_from_timing(&timing, &ddata->vm);
1198 if (!ddata->vm.pixelclock)
1199 ddata->vm.pixelclock =
1200 ddata->vm.hactive * ddata->vm.vactive * 60;
1202 dev_warn(&pdev->dev,
1203 "failed to get video timing, using defaults\n");
1206 ddata->width_mm = 0;
1207 of_property_read_u32(node, "width-mm", &ddata->width_mm);
1209 ddata->height_mm = 0;
1210 of_property_read_u32(node, "height-mm", &ddata->height_mm);
1212 ddata->vpnl = devm_regulator_get_optional(&pdev->dev, "vpnl");
1213 if (IS_ERR(ddata->vpnl)) {
1214 err = PTR_ERR(ddata->vpnl);
1215 if (err == -EPROBE_DEFER)
1220 ddata->vddi = devm_regulator_get_optional(&pdev->dev, "vddi");
1221 if (IS_ERR(ddata->vddi)) {
1222 err = PTR_ERR(ddata->vddi);
1223 if (err == -EPROBE_DEFER)
1228 backlight = of_parse_phandle(node, "backlight", 0);
1230 ddata->extbldev = of_find_backlight_by_node(backlight);
1231 of_node_put(backlight);
1233 if (!ddata->extbldev)
1234 return -EPROBE_DEFER;
1236 /* assume native backlight support */
1237 ddata->use_dsi_backlight = true;
1245 static int dsicm_probe(struct platform_device *pdev)
1247 struct panel_drv_data *ddata;
1248 struct backlight_device *bldev = NULL;
1249 struct device *dev = &pdev->dev;
1250 struct omap_dss_device *dssdev;
1253 dev_dbg(dev, "probe\n");
1255 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
1259 platform_set_drvdata(pdev, ddata);
1262 ddata->vm.hactive = 864;
1263 ddata->vm.vactive = 480;
1264 ddata->vm.pixelclock = 864 * 480 * 60;
1266 r = dsicm_probe_of(pdev);
1270 dssdev = &ddata->dssdev;
1272 dssdev->ops = &dsicm_ops;
1273 dssdev->driver = &dsicm_dss_driver;
1274 dssdev->type = OMAP_DISPLAY_TYPE_DSI;
1275 dssdev->display = true;
1276 dssdev->owner = THIS_MODULE;
1277 dssdev->of_ports = BIT(0);
1278 dssdev->ops_flags = OMAP_DSS_DEVICE_OP_MODES;
1280 dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
1281 OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
1283 omapdss_display_init(dssdev);
1284 omapdss_device_register(dssdev);
1286 mutex_init(&ddata->lock);
1288 atomic_set(&ddata->do_update, 0);
1290 if (ddata->ext_te_gpio) {
1291 r = devm_request_irq(dev, gpiod_to_irq(ddata->ext_te_gpio),
1293 IRQF_TRIGGER_RISING,
1294 "taal vsync", ddata);
1297 dev_err(dev, "IRQ request failed\n");
1301 INIT_DEFERRABLE_WORK(&ddata->te_timeout_work,
1302 dsicm_te_timeout_work_callback);
1304 dev_dbg(dev, "Using GPIO TE\n");
1307 ddata->workqueue = create_singlethread_workqueue("dsicm_wq");
1308 if (!ddata->workqueue) {
1312 INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work);
1314 dsicm_hw_reset(ddata);
1316 if (ddata->use_dsi_backlight) {
1317 struct backlight_properties props = { 0 };
1318 props.max_brightness = 255;
1319 props.type = BACKLIGHT_RAW;
1321 bldev = devm_backlight_device_register(dev, dev_name(dev),
1322 dev, ddata, &dsicm_bl_ops, &props);
1323 if (IS_ERR(bldev)) {
1328 ddata->bldev = bldev;
1331 r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
1333 dev_err(dev, "failed to create sysfs files\n");
1340 destroy_workqueue(ddata->workqueue);
1342 if (ddata->extbldev)
1343 put_device(&ddata->extbldev->dev);
1348 static int __exit dsicm_remove(struct platform_device *pdev)
1350 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1351 struct omap_dss_device *dssdev = &ddata->dssdev;
1353 dev_dbg(&pdev->dev, "remove\n");
1355 omapdss_device_unregister(dssdev);
1357 if (omapdss_device_is_enabled(dssdev))
1358 dsicm_disable(dssdev);
1359 omapdss_device_disconnect(ddata->src, dssdev);
1361 sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group);
1363 if (ddata->extbldev)
1364 put_device(&ddata->extbldev->dev);
1366 dsicm_cancel_ulps_work(ddata);
1367 destroy_workqueue(ddata->workqueue);
1369 /* reset, to be sure that the panel is in a valid state */
1370 dsicm_hw_reset(ddata);
1375 static const struct of_device_id dsicm_of_match[] = {
1376 { .compatible = "omapdss,panel-dsi-cm", },
1380 MODULE_DEVICE_TABLE(of, dsicm_of_match);
1382 static struct platform_driver dsicm_driver = {
1383 .probe = dsicm_probe,
1384 .remove = __exit_p(dsicm_remove),
1386 .name = "panel-dsi-cm",
1387 .of_match_table = dsicm_of_match,
1388 .suppress_bind_attrs = true,
1392 module_platform_driver(dsicm_driver);
1394 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
1395 MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
1396 MODULE_LICENSE("GPL");