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 <video/mipi_display.h>
28 #include <video/of_display_timing.h>
30 #include "../dss/omapdss.h"
32 /* DSI Virtual channel. Hardcoded for now. */
35 #define DCS_READ_NUM_ERRORS 0x05
36 #define DCS_BRIGHTNESS 0x51
37 #define DCS_CTRL_DISPLAY 0x53
38 #define DCS_GET_ID1 0xda
39 #define DCS_GET_ID2 0xdb
40 #define DCS_GET_ID3 0xdc
42 struct panel_drv_data {
43 struct omap_dss_device dssdev;
44 struct omap_dss_device *in;
48 struct platform_device *pdev;
52 struct backlight_device *bldev;
53 struct backlight_device *extbldev;
55 unsigned long hw_guard_end; /* next value of jiffies when we can
56 * issue the next sleep in/out command
58 unsigned long hw_guard_wait; /* max guard time in jiffies */
60 /* panel HW configuration from DT or platform data */
61 struct gpio_desc *reset_gpio;
62 struct gpio_desc *ext_te_gpio;
64 struct regulator *vpnl;
65 struct regulator *vddi;
67 bool use_dsi_backlight;
72 struct omap_dsi_pin_config pin_config;
74 /* runtime variables */
82 struct delayed_work te_timeout_work;
86 struct workqueue_struct *workqueue;
89 unsigned int ulps_timeout;
90 struct delayed_work ulps_work;
93 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
95 static irqreturn_t dsicm_te_isr(int irq, void *data);
96 static void dsicm_te_timeout_work_callback(struct work_struct *work);
97 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable);
99 static int dsicm_panel_reset(struct panel_drv_data *ddata);
101 static void dsicm_ulps_work(struct work_struct *work);
103 static void dsicm_bl_power(struct panel_drv_data *ddata, bool enable)
105 struct backlight_device *backlight;
108 backlight = ddata->bldev;
109 else if (ddata->extbldev)
110 backlight = ddata->extbldev;
115 backlight->props.fb_blank = FB_BLANK_UNBLANK;
116 backlight->props.state = ~(BL_CORE_FBBLANK | BL_CORE_SUSPENDED);
117 backlight->props.power = FB_BLANK_UNBLANK;
119 backlight->props.fb_blank = FB_BLANK_NORMAL;
120 backlight->props.power = FB_BLANK_POWERDOWN;
121 backlight->props.state |= BL_CORE_FBBLANK | BL_CORE_SUSPENDED;
124 backlight_update_status(backlight);
127 static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec)
129 ddata->hw_guard_wait = msecs_to_jiffies(guard_msec);
130 ddata->hw_guard_end = jiffies + ddata->hw_guard_wait;
133 static void hw_guard_wait(struct panel_drv_data *ddata)
135 unsigned long wait = ddata->hw_guard_end - jiffies;
137 if ((long)wait > 0 && wait <= ddata->hw_guard_wait) {
138 set_current_state(TASK_UNINTERRUPTIBLE);
139 schedule_timeout(wait);
143 static int dsicm_dcs_read_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 *data)
145 struct omap_dss_device *in = ddata->in;
149 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd, buf, 1);
159 static int dsicm_dcs_write_0(struct panel_drv_data *ddata, u8 dcs_cmd)
161 struct omap_dss_device *in = ddata->in;
162 return in->ops.dsi->dcs_write(in, ddata->channel, &dcs_cmd, 1);
165 static int dsicm_dcs_write_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 param)
167 struct omap_dss_device *in = ddata->in;
168 u8 buf[2] = { dcs_cmd, param };
170 return in->ops.dsi->dcs_write(in, ddata->channel, buf, 2);
173 static int dsicm_sleep_in(struct panel_drv_data *ddata)
176 struct omap_dss_device *in = ddata->in;
180 hw_guard_wait(ddata);
182 cmd = MIPI_DCS_ENTER_SLEEP_MODE;
183 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, &cmd, 1);
187 hw_guard_start(ddata, 120);
189 usleep_range(5000, 10000);
194 static int dsicm_sleep_out(struct panel_drv_data *ddata)
198 hw_guard_wait(ddata);
200 r = dsicm_dcs_write_0(ddata, MIPI_DCS_EXIT_SLEEP_MODE);
204 hw_guard_start(ddata, 120);
206 usleep_range(5000, 10000);
211 static int dsicm_get_id(struct panel_drv_data *ddata, u8 *id1, u8 *id2, u8 *id3)
215 r = dsicm_dcs_read_1(ddata, DCS_GET_ID1, id1);
218 r = dsicm_dcs_read_1(ddata, DCS_GET_ID2, id2);
221 r = dsicm_dcs_read_1(ddata, DCS_GET_ID3, id3);
228 static int dsicm_set_update_window(struct panel_drv_data *ddata,
229 u16 x, u16 y, u16 w, u16 h)
231 struct omap_dss_device *in = ddata->in;
239 buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS;
240 buf[1] = (x1 >> 8) & 0xff;
241 buf[2] = (x1 >> 0) & 0xff;
242 buf[3] = (x2 >> 8) & 0xff;
243 buf[4] = (x2 >> 0) & 0xff;
245 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
249 buf[0] = MIPI_DCS_SET_PAGE_ADDRESS;
250 buf[1] = (y1 >> 8) & 0xff;
251 buf[2] = (y1 >> 0) & 0xff;
252 buf[3] = (y2 >> 8) & 0xff;
253 buf[4] = (y2 >> 0) & 0xff;
255 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
259 in->ops.dsi->bta_sync(in, ddata->channel);
264 static void dsicm_queue_ulps_work(struct panel_drv_data *ddata)
266 if (ddata->ulps_timeout > 0)
267 queue_delayed_work(ddata->workqueue, &ddata->ulps_work,
268 msecs_to_jiffies(ddata->ulps_timeout));
271 static void dsicm_cancel_ulps_work(struct panel_drv_data *ddata)
273 cancel_delayed_work(&ddata->ulps_work);
276 static int dsicm_enter_ulps(struct panel_drv_data *ddata)
278 struct omap_dss_device *in = ddata->in;
281 if (ddata->ulps_enabled)
284 dsicm_cancel_ulps_work(ddata);
286 r = _dsicm_enable_te(ddata, false);
290 if (ddata->ext_te_gpio)
291 disable_irq(gpiod_to_irq(ddata->ext_te_gpio));
293 in->ops.dsi->disable(in, false, true);
295 ddata->ulps_enabled = true;
300 dev_err(&ddata->pdev->dev, "enter ULPS failed");
301 dsicm_panel_reset(ddata);
303 ddata->ulps_enabled = false;
305 dsicm_queue_ulps_work(ddata);
310 static int dsicm_exit_ulps(struct panel_drv_data *ddata)
312 struct omap_dss_device *in = ddata->in;
315 if (!ddata->ulps_enabled)
318 r = in->ops.dsi->enable(in);
320 dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
324 in->ops.dsi->enable_hs(in, ddata->channel, true);
326 r = _dsicm_enable_te(ddata, true);
328 dev_err(&ddata->pdev->dev, "failed to re-enable TE");
332 if (ddata->ext_te_gpio)
333 enable_irq(gpiod_to_irq(ddata->ext_te_gpio));
335 dsicm_queue_ulps_work(ddata);
337 ddata->ulps_enabled = false;
342 dev_err(&ddata->pdev->dev, "failed to exit ULPS");
344 r = dsicm_panel_reset(ddata);
346 if (ddata->ext_te_gpio)
347 enable_irq(gpiod_to_irq(ddata->ext_te_gpio));
348 ddata->ulps_enabled = false;
351 dsicm_queue_ulps_work(ddata);
356 static int dsicm_wake_up(struct panel_drv_data *ddata)
358 if (ddata->ulps_enabled)
359 return dsicm_exit_ulps(ddata);
361 dsicm_cancel_ulps_work(ddata);
362 dsicm_queue_ulps_work(ddata);
366 static int dsicm_bl_update_status(struct backlight_device *dev)
368 struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
369 struct omap_dss_device *in = ddata->in;
373 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
374 dev->props.power == FB_BLANK_UNBLANK)
375 level = dev->props.brightness;
379 dev_dbg(&ddata->pdev->dev, "update brightness to %d\n", level);
381 mutex_lock(&ddata->lock);
383 if (ddata->enabled) {
384 in->ops.dsi->bus_lock(in);
386 r = dsicm_wake_up(ddata);
388 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, level);
390 in->ops.dsi->bus_unlock(in);
393 mutex_unlock(&ddata->lock);
398 static int dsicm_bl_get_intensity(struct backlight_device *dev)
400 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
401 dev->props.power == FB_BLANK_UNBLANK)
402 return dev->props.brightness;
407 static const struct backlight_ops dsicm_bl_ops = {
408 .get_brightness = dsicm_bl_get_intensity,
409 .update_status = dsicm_bl_update_status,
412 static ssize_t dsicm_num_errors_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
415 struct platform_device *pdev = to_platform_device(dev);
416 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
417 struct omap_dss_device *in = ddata->in;
421 mutex_lock(&ddata->lock);
423 if (ddata->enabled) {
424 in->ops.dsi->bus_lock(in);
426 r = dsicm_wake_up(ddata);
428 r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS,
431 in->ops.dsi->bus_unlock(in);
436 mutex_unlock(&ddata->lock);
441 return snprintf(buf, PAGE_SIZE, "%d\n", errors);
444 static ssize_t dsicm_hw_revision_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
447 struct platform_device *pdev = to_platform_device(dev);
448 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
449 struct omap_dss_device *in = ddata->in;
453 mutex_lock(&ddata->lock);
455 if (ddata->enabled) {
456 in->ops.dsi->bus_lock(in);
458 r = dsicm_wake_up(ddata);
460 r = dsicm_get_id(ddata, &id1, &id2, &id3);
462 in->ops.dsi->bus_unlock(in);
467 mutex_unlock(&ddata->lock);
472 return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
475 static ssize_t dsicm_store_ulps(struct device *dev,
476 struct device_attribute *attr,
477 const char *buf, size_t count)
479 struct platform_device *pdev = to_platform_device(dev);
480 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
481 struct omap_dss_device *in = ddata->in;
485 r = kstrtoul(buf, 0, &t);
489 mutex_lock(&ddata->lock);
491 if (ddata->enabled) {
492 in->ops.dsi->bus_lock(in);
495 r = dsicm_enter_ulps(ddata);
497 r = dsicm_wake_up(ddata);
499 in->ops.dsi->bus_unlock(in);
502 mutex_unlock(&ddata->lock);
510 static ssize_t dsicm_show_ulps(struct device *dev,
511 struct device_attribute *attr,
514 struct platform_device *pdev = to_platform_device(dev);
515 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
518 mutex_lock(&ddata->lock);
519 t = ddata->ulps_enabled;
520 mutex_unlock(&ddata->lock);
522 return snprintf(buf, PAGE_SIZE, "%u\n", t);
525 static ssize_t dsicm_store_ulps_timeout(struct device *dev,
526 struct device_attribute *attr,
527 const char *buf, size_t count)
529 struct platform_device *pdev = to_platform_device(dev);
530 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
531 struct omap_dss_device *in = ddata->in;
535 r = kstrtoul(buf, 0, &t);
539 mutex_lock(&ddata->lock);
540 ddata->ulps_timeout = t;
542 if (ddata->enabled) {
543 /* dsicm_wake_up will restart the timer */
544 in->ops.dsi->bus_lock(in);
545 r = dsicm_wake_up(ddata);
546 in->ops.dsi->bus_unlock(in);
549 mutex_unlock(&ddata->lock);
557 static ssize_t dsicm_show_ulps_timeout(struct device *dev,
558 struct device_attribute *attr,
561 struct platform_device *pdev = to_platform_device(dev);
562 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
565 mutex_lock(&ddata->lock);
566 t = ddata->ulps_timeout;
567 mutex_unlock(&ddata->lock);
569 return snprintf(buf, PAGE_SIZE, "%u\n", t);
572 static DEVICE_ATTR(num_dsi_errors, S_IRUGO, dsicm_num_errors_show, NULL);
573 static DEVICE_ATTR(hw_revision, S_IRUGO, dsicm_hw_revision_show, NULL);
574 static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
575 dsicm_show_ulps, dsicm_store_ulps);
576 static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
577 dsicm_show_ulps_timeout, dsicm_store_ulps_timeout);
579 static struct attribute *dsicm_attrs[] = {
580 &dev_attr_num_dsi_errors.attr,
581 &dev_attr_hw_revision.attr,
583 &dev_attr_ulps_timeout.attr,
587 static const struct attribute_group dsicm_attr_group = {
588 .attrs = dsicm_attrs,
591 static void dsicm_hw_reset(struct panel_drv_data *ddata)
593 gpiod_set_value(ddata->reset_gpio, 1);
595 /* reset the panel */
596 gpiod_set_value(ddata->reset_gpio, 0);
599 gpiod_set_value(ddata->reset_gpio, 1);
600 /* wait after releasing reset */
601 usleep_range(5000, 10000);
604 static int dsicm_power_on(struct panel_drv_data *ddata)
606 struct omap_dss_device *in = ddata->in;
609 struct omap_dss_dsi_config dsi_config = {
610 .mode = OMAP_DSS_DSI_CMD_MODE,
611 .pixel_format = OMAP_DSS_DSI_FMT_RGB888,
613 .hs_clk_min = 150000000,
614 .hs_clk_max = 300000000,
615 .lp_clk_min = 7000000,
616 .lp_clk_max = 10000000,
620 r = regulator_enable(ddata->vpnl);
622 dev_err(&ddata->pdev->dev,
623 "failed to enable VPNL: %d\n", r);
629 r = regulator_enable(ddata->vddi);
631 dev_err(&ddata->pdev->dev,
632 "failed to enable VDDI: %d\n", r);
637 if (ddata->pin_config.num_pins > 0) {
638 r = in->ops.dsi->configure_pins(in, &ddata->pin_config);
640 dev_err(&ddata->pdev->dev,
641 "failed to configure DSI pins\n");
646 r = in->ops.dsi->set_config(in, &dsi_config);
648 dev_err(&ddata->pdev->dev, "failed to configure DSI\n");
652 r = in->ops.dsi->enable(in);
654 dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
658 dsicm_hw_reset(ddata);
660 in->ops.dsi->enable_hs(in, ddata->channel, false);
662 r = dsicm_sleep_out(ddata);
666 r = dsicm_get_id(ddata, &id1, &id2, &id3);
670 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff);
674 r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY,
675 (1<<2) | (1<<5)); /* BL | BCTRL */
679 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT,
680 MIPI_DCS_PIXEL_FMT_24BIT);
684 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON);
688 r = _dsicm_enable_te(ddata, ddata->te_enabled);
692 r = in->ops.dsi->enable_video_output(in, ddata->channel);
698 if (!ddata->intro_printed) {
699 dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n",
701 ddata->intro_printed = true;
704 in->ops.dsi->enable_hs(in, ddata->channel, true);
708 dev_err(&ddata->pdev->dev, "error while enabling panel, issuing HW reset\n");
710 dsicm_hw_reset(ddata);
712 in->ops.dsi->disable(in, true, false);
715 regulator_disable(ddata->vddi);
718 regulator_disable(ddata->vpnl);
723 static void dsicm_power_off(struct panel_drv_data *ddata)
725 struct omap_dss_device *in = ddata->in;
728 in->ops.dsi->disable_video_output(in, ddata->channel);
730 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF);
732 r = dsicm_sleep_in(ddata);
735 dev_err(&ddata->pdev->dev,
736 "error disabling panel, issuing HW reset\n");
737 dsicm_hw_reset(ddata);
740 in->ops.dsi->disable(in, true, false);
743 regulator_disable(ddata->vddi);
745 regulator_disable(ddata->vpnl);
750 static int dsicm_panel_reset(struct panel_drv_data *ddata)
752 dev_err(&ddata->pdev->dev, "performing LCD reset\n");
754 dsicm_power_off(ddata);
755 dsicm_hw_reset(ddata);
756 return dsicm_power_on(ddata);
759 static int dsicm_connect(struct omap_dss_device *dssdev)
761 struct panel_drv_data *ddata = to_panel_data(dssdev);
762 struct device *dev = &ddata->pdev->dev;
763 struct omap_dss_device *in;
766 if (omapdss_device_is_connected(dssdev))
769 in = omapdss_of_find_source_for_first_ep(dssdev->dev->of_node);
771 dev_err(dssdev->dev, "failed to find video source\n");
775 r = in->ops.dsi->connect(in, dssdev);
777 dev_err(dev, "Failed to connect to video source\n");
781 r = in->ops.dsi->request_vc(in, &ddata->channel);
783 dev_err(dev, "failed to get virtual channel\n");
787 r = in->ops.dsi->set_vc_id(in, ddata->channel, TCH);
789 dev_err(dev, "failed to set VC_ID\n");
797 in->ops.dsi->release_vc(in, ddata->channel);
799 in->ops.dsi->disconnect(in, dssdev);
801 omap_dss_put_device(in);
805 static void dsicm_disconnect(struct omap_dss_device *dssdev)
807 struct panel_drv_data *ddata = to_panel_data(dssdev);
808 struct omap_dss_device *in = ddata->in;
810 if (!omapdss_device_is_connected(dssdev))
813 in->ops.dsi->release_vc(in, ddata->channel);
814 in->ops.dsi->disconnect(in, dssdev);
816 omap_dss_put_device(in);
820 static int dsicm_enable(struct omap_dss_device *dssdev)
822 struct panel_drv_data *ddata = to_panel_data(dssdev);
823 struct omap_dss_device *in = ddata->in;
826 dev_dbg(&ddata->pdev->dev, "enable\n");
828 mutex_lock(&ddata->lock);
830 if (!omapdss_device_is_connected(dssdev)) {
835 if (omapdss_device_is_enabled(dssdev)) {
840 in->ops.dsi->bus_lock(in);
842 r = dsicm_power_on(ddata);
844 in->ops.dsi->bus_unlock(in);
849 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
851 mutex_unlock(&ddata->lock);
853 dsicm_bl_power(ddata, true);
857 dev_dbg(&ddata->pdev->dev, "enable failed\n");
858 mutex_unlock(&ddata->lock);
862 static void dsicm_disable(struct omap_dss_device *dssdev)
864 struct panel_drv_data *ddata = to_panel_data(dssdev);
865 struct omap_dss_device *in = ddata->in;
868 dev_dbg(&ddata->pdev->dev, "disable\n");
870 dsicm_bl_power(ddata, false);
872 mutex_lock(&ddata->lock);
874 dsicm_cancel_ulps_work(ddata);
876 in->ops.dsi->bus_lock(in);
878 if (omapdss_device_is_enabled(dssdev)) {
879 r = dsicm_wake_up(ddata);
881 dsicm_power_off(ddata);
884 in->ops.dsi->bus_unlock(in);
886 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
888 mutex_unlock(&ddata->lock);
891 static void dsicm_framedone_cb(int err, void *data)
893 struct panel_drv_data *ddata = data;
894 struct omap_dss_device *in = ddata->in;
896 dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err);
897 in->ops.dsi->bus_unlock(ddata->in);
900 static irqreturn_t dsicm_te_isr(int irq, void *data)
902 struct panel_drv_data *ddata = data;
903 struct omap_dss_device *in = ddata->in;
907 old = atomic_cmpxchg(&ddata->do_update, 1, 0);
910 cancel_delayed_work(&ddata->te_timeout_work);
912 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
920 dev_err(&ddata->pdev->dev, "start update failed\n");
921 in->ops.dsi->bus_unlock(in);
925 static void dsicm_te_timeout_work_callback(struct work_struct *work)
927 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
928 te_timeout_work.work);
929 struct omap_dss_device *in = ddata->in;
931 dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n");
933 atomic_set(&ddata->do_update, 0);
934 in->ops.dsi->bus_unlock(in);
937 static int dsicm_update(struct omap_dss_device *dssdev,
938 u16 x, u16 y, u16 w, u16 h)
940 struct panel_drv_data *ddata = to_panel_data(dssdev);
941 struct omap_dss_device *in = ddata->in;
944 dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
946 mutex_lock(&ddata->lock);
947 in->ops.dsi->bus_lock(in);
949 r = dsicm_wake_up(ddata);
953 if (!ddata->enabled) {
958 /* XXX no need to send this every frame, but dsi break if not done */
959 r = dsicm_set_update_window(ddata, 0, 0,
960 dssdev->panel.vm.hactive,
961 dssdev->panel.vm.vactive);
965 if (ddata->te_enabled && ddata->ext_te_gpio) {
966 schedule_delayed_work(&ddata->te_timeout_work,
967 msecs_to_jiffies(250));
968 atomic_set(&ddata->do_update, 1);
970 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
976 /* note: no bus_unlock here. unlock is in framedone_cb */
977 mutex_unlock(&ddata->lock);
980 in->ops.dsi->bus_unlock(in);
981 mutex_unlock(&ddata->lock);
985 static int dsicm_sync(struct omap_dss_device *dssdev)
987 struct panel_drv_data *ddata = to_panel_data(dssdev);
988 struct omap_dss_device *in = ddata->in;
990 dev_dbg(&ddata->pdev->dev, "sync\n");
992 mutex_lock(&ddata->lock);
993 in->ops.dsi->bus_lock(in);
994 in->ops.dsi->bus_unlock(in);
995 mutex_unlock(&ddata->lock);
997 dev_dbg(&ddata->pdev->dev, "sync done\n");
1002 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable)
1004 struct omap_dss_device *in = ddata->in;
1008 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0);
1010 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF);
1012 if (!ddata->ext_te_gpio)
1013 in->ops.dsi->enable_te(in, enable);
1015 /* possible panel bug */
1021 static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable)
1023 struct panel_drv_data *ddata = to_panel_data(dssdev);
1024 struct omap_dss_device *in = ddata->in;
1027 mutex_lock(&ddata->lock);
1029 if (ddata->te_enabled == enable)
1032 in->ops.dsi->bus_lock(in);
1034 if (ddata->enabled) {
1035 r = dsicm_wake_up(ddata);
1039 r = _dsicm_enable_te(ddata, enable);
1044 ddata->te_enabled = enable;
1046 in->ops.dsi->bus_unlock(in);
1048 mutex_unlock(&ddata->lock);
1052 in->ops.dsi->bus_unlock(in);
1053 mutex_unlock(&ddata->lock);
1058 static int dsicm_get_te(struct omap_dss_device *dssdev)
1060 struct panel_drv_data *ddata = to_panel_data(dssdev);
1063 mutex_lock(&ddata->lock);
1064 r = ddata->te_enabled;
1065 mutex_unlock(&ddata->lock);
1070 static int dsicm_memory_read(struct omap_dss_device *dssdev,
1071 void *buf, size_t size,
1072 u16 x, u16 y, u16 w, u16 h)
1074 struct panel_drv_data *ddata = to_panel_data(dssdev);
1075 struct omap_dss_device *in = ddata->in;
1079 unsigned int buf_used = 0;
1081 if (size < w * h * 3)
1084 mutex_lock(&ddata->lock);
1086 if (!ddata->enabled) {
1091 size = min((u32)w * h * 3,
1092 dssdev->panel.vm.hactive * dssdev->panel.vm.vactive * 3);
1094 in->ops.dsi->bus_lock(in);
1096 r = dsicm_wake_up(ddata);
1100 /* plen 1 or 2 goes into short packet. until checksum error is fixed,
1101 * use short packets. plen 32 works, but bigger packets seem to cause
1108 dsicm_set_update_window(ddata, x, y, w, h);
1110 r = in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, plen);
1114 while (buf_used < size) {
1115 u8 dcs_cmd = first ? 0x2e : 0x3e;
1118 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd,
1119 buf + buf_used, size - buf_used);
1122 dev_err(dssdev->dev, "read error\n");
1129 dev_err(&ddata->pdev->dev, "short read\n");
1133 if (signal_pending(current)) {
1134 dev_err(&ddata->pdev->dev, "signal pending, "
1135 "aborting memory read\n");
1144 in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, 1);
1146 in->ops.dsi->bus_unlock(in);
1148 mutex_unlock(&ddata->lock);
1152 static void dsicm_ulps_work(struct work_struct *work)
1154 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
1156 struct omap_dss_device *dssdev = &ddata->dssdev;
1157 struct omap_dss_device *in = ddata->in;
1159 mutex_lock(&ddata->lock);
1161 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) {
1162 mutex_unlock(&ddata->lock);
1166 in->ops.dsi->bus_lock(in);
1168 dsicm_enter_ulps(ddata);
1170 in->ops.dsi->bus_unlock(in);
1171 mutex_unlock(&ddata->lock);
1174 static void dsicm_get_timings(struct omap_dss_device *dssdev,
1175 struct videomode *vm)
1177 struct panel_drv_data *ddata = to_panel_data(dssdev);
1182 static int dsicm_check_timings(struct omap_dss_device *dssdev,
1183 struct videomode *vm)
1185 struct panel_drv_data *ddata = to_panel_data(dssdev);
1188 if (vm->hactive != ddata->vm.hactive)
1191 if (vm->vactive != ddata->vm.vactive)
1195 dev_warn(dssdev->dev, "wrong resolution: %d x %d",
1196 vm->hactive, vm->vactive);
1197 dev_warn(dssdev->dev, "panel resolution: %d x %d",
1198 ddata->vm.hactive, ddata->vm.vactive);
1204 static void dsicm_get_size(struct omap_dss_device *dssdev,
1205 unsigned int *width, unsigned int *height)
1207 struct panel_drv_data *ddata = to_panel_data(dssdev);
1209 *width = ddata->width_mm;
1210 *height = ddata->height_mm;
1213 static struct omap_dss_driver dsicm_ops = {
1214 .connect = dsicm_connect,
1215 .disconnect = dsicm_disconnect,
1217 .enable = dsicm_enable,
1218 .disable = dsicm_disable,
1220 .update = dsicm_update,
1223 .get_timings = dsicm_get_timings,
1224 .check_timings = dsicm_check_timings,
1225 .get_size = dsicm_get_size,
1227 .enable_te = dsicm_enable_te,
1228 .get_te = dsicm_get_te,
1230 .memory_read = dsicm_memory_read,
1233 static int dsicm_probe_of(struct platform_device *pdev)
1235 struct device_node *node = pdev->dev.of_node;
1236 struct device_node *backlight;
1237 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1238 struct display_timing timing;
1241 ddata->reset_gpio = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
1242 if (IS_ERR(ddata->reset_gpio)) {
1243 err = PTR_ERR(ddata->reset_gpio);
1244 dev_err(&pdev->dev, "reset gpio request failed: %d", err);
1248 ddata->ext_te_gpio = devm_gpiod_get_optional(&pdev->dev, "te",
1250 if (IS_ERR(ddata->ext_te_gpio)) {
1251 err = PTR_ERR(ddata->ext_te_gpio);
1252 dev_err(&pdev->dev, "TE gpio request failed: %d", err);
1256 err = of_get_display_timing(node, "panel-timing", &timing);
1258 videomode_from_timing(&timing, &ddata->vm);
1259 if (!ddata->vm.pixelclock)
1260 ddata->vm.pixelclock =
1261 ddata->vm.hactive * ddata->vm.vactive * 60;
1263 dev_warn(&pdev->dev,
1264 "failed to get video timing, using defaults\n");
1267 ddata->width_mm = 0;
1268 of_property_read_u32(node, "width-mm", &ddata->width_mm);
1270 ddata->height_mm = 0;
1271 of_property_read_u32(node, "height-mm", &ddata->height_mm);
1273 ddata->vpnl = devm_regulator_get_optional(&pdev->dev, "vpnl");
1274 if (IS_ERR(ddata->vpnl)) {
1275 err = PTR_ERR(ddata->vpnl);
1276 if (err == -EPROBE_DEFER)
1281 ddata->vddi = devm_regulator_get_optional(&pdev->dev, "vddi");
1282 if (IS_ERR(ddata->vddi)) {
1283 err = PTR_ERR(ddata->vddi);
1284 if (err == -EPROBE_DEFER)
1289 backlight = of_parse_phandle(node, "backlight", 0);
1291 ddata->extbldev = of_find_backlight_by_node(backlight);
1292 of_node_put(backlight);
1294 if (!ddata->extbldev)
1295 return -EPROBE_DEFER;
1297 /* assume native backlight support */
1298 ddata->use_dsi_backlight = true;
1306 static int dsicm_probe(struct platform_device *pdev)
1308 struct panel_drv_data *ddata;
1309 struct backlight_device *bldev = NULL;
1310 struct device *dev = &pdev->dev;
1311 struct omap_dss_device *dssdev;
1314 dev_dbg(dev, "probe\n");
1316 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
1320 platform_set_drvdata(pdev, ddata);
1323 ddata->vm.hactive = 864;
1324 ddata->vm.vactive = 480;
1325 ddata->vm.pixelclock = 864 * 480 * 60;
1327 r = dsicm_probe_of(pdev);
1331 dssdev = &ddata->dssdev;
1333 dssdev->driver = &dsicm_ops;
1334 dssdev->panel.vm = ddata->vm;
1335 dssdev->type = OMAP_DISPLAY_TYPE_DSI;
1336 dssdev->owner = THIS_MODULE;
1338 dssdev->panel.dsi_pix_fmt = OMAP_DSS_DSI_FMT_RGB888;
1339 dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
1340 OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
1342 r = omapdss_register_display(dssdev);
1344 dev_err(dev, "Failed to register panel\n");
1348 mutex_init(&ddata->lock);
1350 atomic_set(&ddata->do_update, 0);
1352 if (ddata->ext_te_gpio) {
1353 r = devm_request_irq(dev, gpiod_to_irq(ddata->ext_te_gpio),
1355 IRQF_TRIGGER_RISING,
1356 "taal vsync", ddata);
1359 dev_err(dev, "IRQ request failed\n");
1363 INIT_DEFERRABLE_WORK(&ddata->te_timeout_work,
1364 dsicm_te_timeout_work_callback);
1366 dev_dbg(dev, "Using GPIO TE\n");
1369 ddata->workqueue = create_singlethread_workqueue("dsicm_wq");
1370 if (!ddata->workqueue) {
1374 INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work);
1376 dsicm_hw_reset(ddata);
1378 if (ddata->use_dsi_backlight) {
1379 struct backlight_properties props = { 0 };
1380 props.max_brightness = 255;
1381 props.type = BACKLIGHT_RAW;
1383 bldev = devm_backlight_device_register(dev, dev_name(dev),
1384 dev, ddata, &dsicm_bl_ops, &props);
1385 if (IS_ERR(bldev)) {
1390 ddata->bldev = bldev;
1393 r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
1395 dev_err(dev, "failed to create sysfs files\n");
1402 destroy_workqueue(ddata->workqueue);
1404 if (ddata->extbldev)
1405 put_device(&ddata->extbldev->dev);
1410 static int __exit dsicm_remove(struct platform_device *pdev)
1412 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1413 struct omap_dss_device *dssdev = &ddata->dssdev;
1415 dev_dbg(&pdev->dev, "remove\n");
1417 omapdss_unregister_display(dssdev);
1419 dsicm_disable(dssdev);
1420 dsicm_disconnect(dssdev);
1422 sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group);
1424 if (ddata->extbldev)
1425 put_device(&ddata->extbldev->dev);
1427 dsicm_cancel_ulps_work(ddata);
1428 destroy_workqueue(ddata->workqueue);
1430 /* reset, to be sure that the panel is in a valid state */
1431 dsicm_hw_reset(ddata);
1436 static const struct of_device_id dsicm_of_match[] = {
1437 { .compatible = "omapdss,panel-dsi-cm", },
1441 MODULE_DEVICE_TABLE(of, dsicm_of_match);
1443 static struct platform_driver dsicm_driver = {
1444 .probe = dsicm_probe,
1445 .remove = __exit_p(dsicm_remove),
1447 .name = "panel-dsi-cm",
1448 .of_match_table = dsicm_of_match,
1449 .suppress_bind_attrs = true,
1453 module_platform_driver(dsicm_driver);
1455 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
1456 MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
1457 MODULE_LICENSE("GPL");