Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / gpu / drm / omapdrm / displays / panel-dsi-cm.c
blob15399a1a666b8703fc1799a99160921d9cd21bff
1 /*
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.
12 /* #define DEBUG */
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. */
33 #define TCH 0
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;
46 struct videomode vm;
48 struct platform_device *pdev;
50 struct mutex lock;
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;
69 int width_mm;
70 int height_mm;
72 struct omap_dsi_pin_config pin_config;
74 /* runtime variables */
75 bool enabled;
77 bool te_enabled;
79 atomic_t do_update;
80 int channel;
82 struct delayed_work te_timeout_work;
84 bool intro_printed;
86 struct workqueue_struct *workqueue;
88 bool ulps_enabled;
89 unsigned 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;
107 if (ddata->bldev)
108 backlight = ddata->bldev;
109 else if (ddata->extbldev)
110 backlight = ddata->extbldev;
111 else
112 return;
114 if (enable) {
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;
118 } else {
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;
146 int r;
147 u8 buf[1];
149 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd, buf, 1);
151 if (r < 0)
152 return r;
154 *data = buf[0];
156 return 0;
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;
177 u8 cmd;
178 int r;
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);
184 if (r)
185 return r;
187 hw_guard_start(ddata, 120);
189 usleep_range(5000, 10000);
191 return 0;
194 static int dsicm_sleep_out(struct panel_drv_data *ddata)
196 int r;
198 hw_guard_wait(ddata);
200 r = dsicm_dcs_write_0(ddata, MIPI_DCS_EXIT_SLEEP_MODE);
201 if (r)
202 return r;
204 hw_guard_start(ddata, 120);
206 usleep_range(5000, 10000);
208 return 0;
211 static int dsicm_get_id(struct panel_drv_data *ddata, u8 *id1, u8 *id2, u8 *id3)
213 int r;
215 r = dsicm_dcs_read_1(ddata, DCS_GET_ID1, id1);
216 if (r)
217 return r;
218 r = dsicm_dcs_read_1(ddata, DCS_GET_ID2, id2);
219 if (r)
220 return r;
221 r = dsicm_dcs_read_1(ddata, DCS_GET_ID3, id3);
222 if (r)
223 return r;
225 return 0;
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;
232 int r;
233 u16 x1 = x;
234 u16 x2 = x + w - 1;
235 u16 y1 = y;
236 u16 y2 = y + h - 1;
238 u8 buf[5];
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));
246 if (r)
247 return r;
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));
256 if (r)
257 return r;
259 in->ops.dsi->bta_sync(in, ddata->channel);
261 return r;
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;
279 int r;
281 if (ddata->ulps_enabled)
282 return 0;
284 dsicm_cancel_ulps_work(ddata);
286 r = _dsicm_enable_te(ddata, false);
287 if (r)
288 goto err;
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;
297 return 0;
299 err:
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);
307 return r;
310 static int dsicm_exit_ulps(struct panel_drv_data *ddata)
312 struct omap_dss_device *in = ddata->in;
313 int r;
315 if (!ddata->ulps_enabled)
316 return 0;
318 r = in->ops.dsi->enable(in);
319 if (r) {
320 dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
321 goto err1;
324 in->ops.dsi->enable_hs(in, ddata->channel, true);
326 r = _dsicm_enable_te(ddata, true);
327 if (r) {
328 dev_err(&ddata->pdev->dev, "failed to re-enable TE");
329 goto err2;
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;
339 return 0;
341 err2:
342 dev_err(&ddata->pdev->dev, "failed to exit ULPS");
344 r = dsicm_panel_reset(ddata);
345 if (!r) {
346 if (ddata->ext_te_gpio)
347 enable_irq(gpiod_to_irq(ddata->ext_te_gpio));
348 ddata->ulps_enabled = false;
350 err1:
351 dsicm_queue_ulps_work(ddata);
353 return r;
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);
363 return 0;
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;
370 int r = 0;
371 int level;
373 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
374 dev->props.power == FB_BLANK_UNBLANK)
375 level = dev->props.brightness;
376 else
377 level = 0;
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);
387 if (!r)
388 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, level);
390 in->ops.dsi->bus_unlock(in);
393 mutex_unlock(&ddata->lock);
395 return r;
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;
404 return 0;
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;
418 u8 errors = 0;
419 int r;
421 mutex_lock(&ddata->lock);
423 if (ddata->enabled) {
424 in->ops.dsi->bus_lock(in);
426 r = dsicm_wake_up(ddata);
427 if (!r)
428 r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS,
429 &errors);
431 in->ops.dsi->bus_unlock(in);
432 } else {
433 r = -ENODEV;
436 mutex_unlock(&ddata->lock);
438 if (r)
439 return r;
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;
450 u8 id1, id2, id3;
451 int r;
453 mutex_lock(&ddata->lock);
455 if (ddata->enabled) {
456 in->ops.dsi->bus_lock(in);
458 r = dsicm_wake_up(ddata);
459 if (!r)
460 r = dsicm_get_id(ddata, &id1, &id2, &id3);
462 in->ops.dsi->bus_unlock(in);
463 } else {
464 r = -ENODEV;
467 mutex_unlock(&ddata->lock);
469 if (r)
470 return r;
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;
482 unsigned long t;
483 int r;
485 r = kstrtoul(buf, 0, &t);
486 if (r)
487 return r;
489 mutex_lock(&ddata->lock);
491 if (ddata->enabled) {
492 in->ops.dsi->bus_lock(in);
494 if (t)
495 r = dsicm_enter_ulps(ddata);
496 else
497 r = dsicm_wake_up(ddata);
499 in->ops.dsi->bus_unlock(in);
502 mutex_unlock(&ddata->lock);
504 if (r)
505 return r;
507 return count;
510 static ssize_t dsicm_show_ulps(struct device *dev,
511 struct device_attribute *attr,
512 char *buf)
514 struct platform_device *pdev = to_platform_device(dev);
515 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
516 unsigned t;
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;
532 unsigned long t;
533 int r;
535 r = kstrtoul(buf, 0, &t);
536 if (r)
537 return r;
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);
551 if (r)
552 return r;
554 return count;
557 static ssize_t dsicm_show_ulps_timeout(struct device *dev,
558 struct device_attribute *attr,
559 char *buf)
561 struct platform_device *pdev = to_platform_device(dev);
562 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
563 unsigned t;
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,
582 &dev_attr_ulps.attr,
583 &dev_attr_ulps_timeout.attr,
584 NULL,
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);
594 udelay(10);
595 /* reset the panel */
596 gpiod_set_value(ddata->reset_gpio, 0);
597 /* assert reset */
598 udelay(10);
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;
607 u8 id1, id2, id3;
608 int r;
609 struct omap_dss_dsi_config dsi_config = {
610 .mode = OMAP_DSS_DSI_CMD_MODE,
611 .pixel_format = OMAP_DSS_DSI_FMT_RGB888,
612 .vm = &ddata->vm,
613 .hs_clk_min = 150000000,
614 .hs_clk_max = 300000000,
615 .lp_clk_min = 7000000,
616 .lp_clk_max = 10000000,
619 if (ddata->vpnl) {
620 r = regulator_enable(ddata->vpnl);
621 if (r) {
622 dev_err(&ddata->pdev->dev,
623 "failed to enable VPNL: %d\n", r);
624 return r;
628 if (ddata->vddi) {
629 r = regulator_enable(ddata->vddi);
630 if (r) {
631 dev_err(&ddata->pdev->dev,
632 "failed to enable VDDI: %d\n", r);
633 goto err_vpnl;
637 if (ddata->pin_config.num_pins > 0) {
638 r = in->ops.dsi->configure_pins(in, &ddata->pin_config);
639 if (r) {
640 dev_err(&ddata->pdev->dev,
641 "failed to configure DSI pins\n");
642 goto err_vddi;
646 r = in->ops.dsi->set_config(in, &dsi_config);
647 if (r) {
648 dev_err(&ddata->pdev->dev, "failed to configure DSI\n");
649 goto err_vddi;
652 r = in->ops.dsi->enable(in);
653 if (r) {
654 dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
655 goto err_vddi;
658 dsicm_hw_reset(ddata);
660 in->ops.dsi->enable_hs(in, ddata->channel, false);
662 r = dsicm_sleep_out(ddata);
663 if (r)
664 goto err;
666 r = dsicm_get_id(ddata, &id1, &id2, &id3);
667 if (r)
668 goto err;
670 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff);
671 if (r)
672 goto err;
674 r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY,
675 (1<<2) | (1<<5)); /* BL | BCTRL */
676 if (r)
677 goto err;
679 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT,
680 MIPI_DCS_PIXEL_FMT_24BIT);
681 if (r)
682 goto err;
684 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON);
685 if (r)
686 goto err;
688 r = _dsicm_enable_te(ddata, ddata->te_enabled);
689 if (r)
690 goto err;
692 r = in->ops.dsi->enable_video_output(in, ddata->channel);
693 if (r)
694 goto err;
696 ddata->enabled = 1;
698 if (!ddata->intro_printed) {
699 dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n",
700 id1, id2, id3);
701 ddata->intro_printed = true;
704 in->ops.dsi->enable_hs(in, ddata->channel, true);
706 return 0;
707 err:
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);
713 err_vddi:
714 if (ddata->vddi)
715 regulator_disable(ddata->vddi);
716 err_vpnl:
717 if (ddata->vpnl)
718 regulator_disable(ddata->vpnl);
720 return r;
723 static void dsicm_power_off(struct panel_drv_data *ddata)
725 struct omap_dss_device *in = ddata->in;
726 int r;
728 in->ops.dsi->disable_video_output(in, ddata->channel);
730 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF);
731 if (!r)
732 r = dsicm_sleep_in(ddata);
734 if (r) {
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);
742 if (ddata->vddi)
743 regulator_disable(ddata->vddi);
744 if (ddata->vpnl)
745 regulator_disable(ddata->vpnl);
747 ddata->enabled = 0;
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 omap_dss_device *in = ddata->in;
763 struct device *dev = &ddata->pdev->dev;
764 int r;
766 if (omapdss_device_is_connected(dssdev))
767 return 0;
769 r = in->ops.dsi->connect(in, dssdev);
770 if (r) {
771 dev_err(dev, "Failed to connect to video source\n");
772 return r;
775 r = in->ops.dsi->request_vc(ddata->in, &ddata->channel);
776 if (r) {
777 dev_err(dev, "failed to get virtual channel\n");
778 goto err_req_vc;
781 r = in->ops.dsi->set_vc_id(ddata->in, ddata->channel, TCH);
782 if (r) {
783 dev_err(dev, "failed to set VC_ID\n");
784 goto err_vc_id;
787 return 0;
789 err_vc_id:
790 in->ops.dsi->release_vc(ddata->in, ddata->channel);
791 err_req_vc:
792 in->ops.dsi->disconnect(in, dssdev);
793 return r;
796 static void dsicm_disconnect(struct omap_dss_device *dssdev)
798 struct panel_drv_data *ddata = to_panel_data(dssdev);
799 struct omap_dss_device *in = ddata->in;
801 if (!omapdss_device_is_connected(dssdev))
802 return;
804 in->ops.dsi->release_vc(in, ddata->channel);
805 in->ops.dsi->disconnect(in, dssdev);
808 static int dsicm_enable(struct omap_dss_device *dssdev)
810 struct panel_drv_data *ddata = to_panel_data(dssdev);
811 struct omap_dss_device *in = ddata->in;
812 int r;
814 dev_dbg(&ddata->pdev->dev, "enable\n");
816 mutex_lock(&ddata->lock);
818 if (!omapdss_device_is_connected(dssdev)) {
819 r = -ENODEV;
820 goto err;
823 if (omapdss_device_is_enabled(dssdev)) {
824 r = 0;
825 goto err;
828 in->ops.dsi->bus_lock(in);
830 r = dsicm_power_on(ddata);
832 in->ops.dsi->bus_unlock(in);
834 if (r)
835 goto err;
837 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
839 mutex_unlock(&ddata->lock);
841 dsicm_bl_power(ddata, true);
843 return 0;
844 err:
845 dev_dbg(&ddata->pdev->dev, "enable failed\n");
846 mutex_unlock(&ddata->lock);
847 return r;
850 static void dsicm_disable(struct omap_dss_device *dssdev)
852 struct panel_drv_data *ddata = to_panel_data(dssdev);
853 struct omap_dss_device *in = ddata->in;
854 int r;
856 dev_dbg(&ddata->pdev->dev, "disable\n");
858 dsicm_bl_power(ddata, false);
860 mutex_lock(&ddata->lock);
862 dsicm_cancel_ulps_work(ddata);
864 in->ops.dsi->bus_lock(in);
866 if (omapdss_device_is_enabled(dssdev)) {
867 r = dsicm_wake_up(ddata);
868 if (!r)
869 dsicm_power_off(ddata);
872 in->ops.dsi->bus_unlock(in);
874 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
876 mutex_unlock(&ddata->lock);
879 static void dsicm_framedone_cb(int err, void *data)
881 struct panel_drv_data *ddata = data;
882 struct omap_dss_device *in = ddata->in;
884 dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err);
885 in->ops.dsi->bus_unlock(ddata->in);
888 static irqreturn_t dsicm_te_isr(int irq, void *data)
890 struct panel_drv_data *ddata = data;
891 struct omap_dss_device *in = ddata->in;
892 int old;
893 int r;
895 old = atomic_cmpxchg(&ddata->do_update, 1, 0);
897 if (old) {
898 cancel_delayed_work(&ddata->te_timeout_work);
900 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
901 ddata);
902 if (r)
903 goto err;
906 return IRQ_HANDLED;
907 err:
908 dev_err(&ddata->pdev->dev, "start update failed\n");
909 in->ops.dsi->bus_unlock(in);
910 return IRQ_HANDLED;
913 static void dsicm_te_timeout_work_callback(struct work_struct *work)
915 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
916 te_timeout_work.work);
917 struct omap_dss_device *in = ddata->in;
919 dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n");
921 atomic_set(&ddata->do_update, 0);
922 in->ops.dsi->bus_unlock(in);
925 static int dsicm_update(struct omap_dss_device *dssdev,
926 u16 x, u16 y, u16 w, u16 h)
928 struct panel_drv_data *ddata = to_panel_data(dssdev);
929 struct omap_dss_device *in = ddata->in;
930 int r;
932 dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
934 mutex_lock(&ddata->lock);
935 in->ops.dsi->bus_lock(in);
937 r = dsicm_wake_up(ddata);
938 if (r)
939 goto err;
941 if (!ddata->enabled) {
942 r = 0;
943 goto err;
946 /* XXX no need to send this every frame, but dsi break if not done */
947 r = dsicm_set_update_window(ddata, 0, 0,
948 dssdev->panel.vm.hactive,
949 dssdev->panel.vm.vactive);
950 if (r)
951 goto err;
953 if (ddata->te_enabled && ddata->ext_te_gpio) {
954 schedule_delayed_work(&ddata->te_timeout_work,
955 msecs_to_jiffies(250));
956 atomic_set(&ddata->do_update, 1);
957 } else {
958 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
959 ddata);
960 if (r)
961 goto err;
964 /* note: no bus_unlock here. unlock is in framedone_cb */
965 mutex_unlock(&ddata->lock);
966 return 0;
967 err:
968 in->ops.dsi->bus_unlock(in);
969 mutex_unlock(&ddata->lock);
970 return r;
973 static int dsicm_sync(struct omap_dss_device *dssdev)
975 struct panel_drv_data *ddata = to_panel_data(dssdev);
976 struct omap_dss_device *in = ddata->in;
978 dev_dbg(&ddata->pdev->dev, "sync\n");
980 mutex_lock(&ddata->lock);
981 in->ops.dsi->bus_lock(in);
982 in->ops.dsi->bus_unlock(in);
983 mutex_unlock(&ddata->lock);
985 dev_dbg(&ddata->pdev->dev, "sync done\n");
987 return 0;
990 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable)
992 struct omap_dss_device *in = ddata->in;
993 int r;
995 if (enable)
996 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0);
997 else
998 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF);
1000 if (!ddata->ext_te_gpio)
1001 in->ops.dsi->enable_te(in, enable);
1003 /* possible panel bug */
1004 msleep(100);
1006 return r;
1009 static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable)
1011 struct panel_drv_data *ddata = to_panel_data(dssdev);
1012 struct omap_dss_device *in = ddata->in;
1013 int r;
1015 mutex_lock(&ddata->lock);
1017 if (ddata->te_enabled == enable)
1018 goto end;
1020 in->ops.dsi->bus_lock(in);
1022 if (ddata->enabled) {
1023 r = dsicm_wake_up(ddata);
1024 if (r)
1025 goto err;
1027 r = _dsicm_enable_te(ddata, enable);
1028 if (r)
1029 goto err;
1032 ddata->te_enabled = enable;
1034 in->ops.dsi->bus_unlock(in);
1035 end:
1036 mutex_unlock(&ddata->lock);
1038 return 0;
1039 err:
1040 in->ops.dsi->bus_unlock(in);
1041 mutex_unlock(&ddata->lock);
1043 return r;
1046 static int dsicm_get_te(struct omap_dss_device *dssdev)
1048 struct panel_drv_data *ddata = to_panel_data(dssdev);
1049 int r;
1051 mutex_lock(&ddata->lock);
1052 r = ddata->te_enabled;
1053 mutex_unlock(&ddata->lock);
1055 return r;
1058 static int dsicm_memory_read(struct omap_dss_device *dssdev,
1059 void *buf, size_t size,
1060 u16 x, u16 y, u16 w, u16 h)
1062 struct panel_drv_data *ddata = to_panel_data(dssdev);
1063 struct omap_dss_device *in = ddata->in;
1064 int r;
1065 int first = 1;
1066 int plen;
1067 unsigned buf_used = 0;
1069 if (size < w * h * 3)
1070 return -ENOMEM;
1072 mutex_lock(&ddata->lock);
1074 if (!ddata->enabled) {
1075 r = -ENODEV;
1076 goto err1;
1079 size = min((u32)w * h * 3,
1080 dssdev->panel.vm.hactive * dssdev->panel.vm.vactive * 3);
1082 in->ops.dsi->bus_lock(in);
1084 r = dsicm_wake_up(ddata);
1085 if (r)
1086 goto err2;
1088 /* plen 1 or 2 goes into short packet. until checksum error is fixed,
1089 * use short packets. plen 32 works, but bigger packets seem to cause
1090 * an error. */
1091 if (size % 2)
1092 plen = 1;
1093 else
1094 plen = 2;
1096 dsicm_set_update_window(ddata, x, y, w, h);
1098 r = in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, plen);
1099 if (r)
1100 goto err2;
1102 while (buf_used < size) {
1103 u8 dcs_cmd = first ? 0x2e : 0x3e;
1104 first = 0;
1106 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd,
1107 buf + buf_used, size - buf_used);
1109 if (r < 0) {
1110 dev_err(dssdev->dev, "read error\n");
1111 goto err3;
1114 buf_used += r;
1116 if (r < plen) {
1117 dev_err(&ddata->pdev->dev, "short read\n");
1118 break;
1121 if (signal_pending(current)) {
1122 dev_err(&ddata->pdev->dev, "signal pending, "
1123 "aborting memory read\n");
1124 r = -ERESTARTSYS;
1125 goto err3;
1129 r = buf_used;
1131 err3:
1132 in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, 1);
1133 err2:
1134 in->ops.dsi->bus_unlock(in);
1135 err1:
1136 mutex_unlock(&ddata->lock);
1137 return r;
1140 static void dsicm_ulps_work(struct work_struct *work)
1142 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
1143 ulps_work.work);
1144 struct omap_dss_device *dssdev = &ddata->dssdev;
1145 struct omap_dss_device *in = ddata->in;
1147 mutex_lock(&ddata->lock);
1149 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) {
1150 mutex_unlock(&ddata->lock);
1151 return;
1154 in->ops.dsi->bus_lock(in);
1156 dsicm_enter_ulps(ddata);
1158 in->ops.dsi->bus_unlock(in);
1159 mutex_unlock(&ddata->lock);
1162 static void dsicm_get_timings(struct omap_dss_device *dssdev,
1163 struct videomode *vm)
1165 struct panel_drv_data *ddata = to_panel_data(dssdev);
1167 *vm = ddata->vm;
1170 static int dsicm_check_timings(struct omap_dss_device *dssdev,
1171 struct videomode *vm)
1173 struct panel_drv_data *ddata = to_panel_data(dssdev);
1174 int ret = 0;
1176 if (vm->hactive != ddata->vm.hactive)
1177 ret = -EINVAL;
1179 if (vm->vactive != ddata->vm.vactive)
1180 ret = -EINVAL;
1182 if (ret) {
1183 dev_warn(dssdev->dev, "wrong resolution: %d x %d",
1184 vm->hactive, vm->vactive);
1185 dev_warn(dssdev->dev, "panel resolution: %d x %d",
1186 ddata->vm.hactive, ddata->vm.vactive);
1189 return ret;
1192 static void dsicm_get_size(struct omap_dss_device *dssdev,
1193 unsigned int *width, unsigned int *height)
1195 struct panel_drv_data *ddata = to_panel_data(dssdev);
1197 *width = ddata->width_mm;
1198 *height = ddata->height_mm;
1201 static struct omap_dss_driver dsicm_ops = {
1202 .connect = dsicm_connect,
1203 .disconnect = dsicm_disconnect,
1205 .enable = dsicm_enable,
1206 .disable = dsicm_disable,
1208 .update = dsicm_update,
1209 .sync = dsicm_sync,
1211 .get_timings = dsicm_get_timings,
1212 .check_timings = dsicm_check_timings,
1213 .get_size = dsicm_get_size,
1215 .enable_te = dsicm_enable_te,
1216 .get_te = dsicm_get_te,
1218 .memory_read = dsicm_memory_read,
1221 static int dsicm_probe_of(struct platform_device *pdev)
1223 struct device_node *node = pdev->dev.of_node;
1224 struct device_node *backlight;
1225 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1226 struct omap_dss_device *in;
1227 struct display_timing timing;
1228 int err;
1230 ddata->reset_gpio = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
1231 if (IS_ERR(ddata->reset_gpio)) {
1232 err = PTR_ERR(ddata->reset_gpio);
1233 dev_err(&pdev->dev, "reset gpio request failed: %d", err);
1234 return err;
1237 ddata->ext_te_gpio = devm_gpiod_get_optional(&pdev->dev, "te",
1238 GPIOD_IN);
1239 if (IS_ERR(ddata->ext_te_gpio)) {
1240 err = PTR_ERR(ddata->ext_te_gpio);
1241 dev_err(&pdev->dev, "TE gpio request failed: %d", err);
1242 return err;
1245 err = of_get_display_timing(node, "panel-timing", &timing);
1246 if (!err) {
1247 videomode_from_timing(&timing, &ddata->vm);
1248 if (!ddata->vm.pixelclock)
1249 ddata->vm.pixelclock =
1250 ddata->vm.hactive * ddata->vm.vactive * 60;
1251 } else {
1252 dev_warn(&pdev->dev,
1253 "failed to get video timing, using defaults\n");
1256 ddata->width_mm = 0;
1257 of_property_read_u32(node, "width-mm", &ddata->width_mm);
1259 ddata->height_mm = 0;
1260 of_property_read_u32(node, "height-mm", &ddata->height_mm);
1262 in = omapdss_of_find_source_for_first_ep(node);
1263 if (IS_ERR(in)) {
1264 dev_err(&pdev->dev, "failed to find video source\n");
1265 return PTR_ERR(in);
1268 ddata->vpnl = devm_regulator_get_optional(&pdev->dev, "vpnl");
1269 if (IS_ERR(ddata->vpnl)) {
1270 err = PTR_ERR(ddata->vpnl);
1271 if (err == -EPROBE_DEFER)
1272 return err;
1273 ddata->vpnl = NULL;
1276 ddata->vddi = devm_regulator_get_optional(&pdev->dev, "vddi");
1277 if (IS_ERR(ddata->vddi)) {
1278 err = PTR_ERR(ddata->vddi);
1279 if (err == -EPROBE_DEFER)
1280 return err;
1281 ddata->vddi = NULL;
1284 ddata->in = in;
1286 backlight = of_parse_phandle(node, "backlight", 0);
1287 if (backlight) {
1288 ddata->extbldev = of_find_backlight_by_node(backlight);
1289 of_node_put(backlight);
1291 if (!ddata->extbldev)
1292 return -EPROBE_DEFER;
1293 } else {
1294 /* assume native backlight support */
1295 ddata->use_dsi_backlight = true;
1298 /* TODO: ulps */
1300 return 0;
1303 static int dsicm_probe(struct platform_device *pdev)
1305 struct panel_drv_data *ddata;
1306 struct backlight_device *bldev = NULL;
1307 struct device *dev = &pdev->dev;
1308 struct omap_dss_device *dssdev;
1309 int r;
1311 dev_dbg(dev, "probe\n");
1313 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
1314 if (!ddata)
1315 return -ENOMEM;
1317 platform_set_drvdata(pdev, ddata);
1318 ddata->pdev = pdev;
1320 if (!pdev->dev.of_node)
1321 return -ENODEV;
1323 ddata->vm.hactive = 864;
1324 ddata->vm.vactive = 480;
1325 ddata->vm.pixelclock = 864 * 480 * 60;
1327 r = dsicm_probe_of(pdev);
1328 if (r)
1329 return r;
1331 dssdev = &ddata->dssdev;
1332 dssdev->dev = dev;
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);
1343 if (r) {
1344 dev_err(dev, "Failed to register panel\n");
1345 goto err_reg;
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),
1354 dsicm_te_isr,
1355 IRQF_TRIGGER_RISING,
1356 "taal vsync", ddata);
1358 if (r) {
1359 dev_err(dev, "IRQ request failed\n");
1360 goto err_reg;
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) {
1371 r = -ENOMEM;
1372 goto err_reg;
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)) {
1386 r = PTR_ERR(bldev);
1387 goto err_bl;
1390 ddata->bldev = bldev;
1393 r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
1394 if (r) {
1395 dev_err(dev, "failed to create sysfs files\n");
1396 goto err_bl;
1399 return 0;
1401 err_bl:
1402 destroy_workqueue(ddata->workqueue);
1403 err_reg:
1404 if (ddata->extbldev)
1405 put_device(&ddata->extbldev->dev);
1407 return r;
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 omap_dss_put_device(ddata->in);
1429 dsicm_cancel_ulps_work(ddata);
1430 destroy_workqueue(ddata->workqueue);
1432 /* reset, to be sure that the panel is in a valid state */
1433 dsicm_hw_reset(ddata);
1435 return 0;
1438 static const struct of_device_id dsicm_of_match[] = {
1439 { .compatible = "omapdss,panel-dsi-cm", },
1443 MODULE_DEVICE_TABLE(of, dsicm_of_match);
1445 static struct platform_driver dsicm_driver = {
1446 .probe = dsicm_probe,
1447 .remove = __exit_p(dsicm_remove),
1448 .driver = {
1449 .name = "panel-dsi-cm",
1450 .of_match_table = dsicm_of_match,
1451 .suppress_bind_attrs = true,
1455 module_platform_driver(dsicm_driver);
1457 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
1458 MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
1459 MODULE_LICENSE("GPL");