mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / video / omap2 / displays-new / panel-dsi-cm.c
blobaaaea6469cd9ebe723bd332f272a6dba76366c53
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.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
26 #include <video/omapdss.h>
27 #include <video/omap-panel-data.h>
28 #include <video/mipi_display.h>
30 /* DSI Virtual channel. Hardcoded for now. */
31 #define TCH 0
33 #define DCS_READ_NUM_ERRORS 0x05
34 #define DCS_BRIGHTNESS 0x51
35 #define DCS_CTRL_DISPLAY 0x53
36 #define DCS_GET_ID1 0xda
37 #define DCS_GET_ID2 0xdb
38 #define DCS_GET_ID3 0xdc
40 struct panel_drv_data {
41 struct omap_dss_device dssdev;
42 struct omap_dss_device *in;
44 struct omap_video_timings timings;
46 struct platform_device *pdev;
48 struct mutex lock;
50 struct backlight_device *bldev;
52 unsigned long hw_guard_end; /* next value of jiffies when we can
53 * issue the next sleep in/out command
55 unsigned long hw_guard_wait; /* max guard time in jiffies */
57 /* panel HW configuration from DT or platform data */
58 int reset_gpio;
59 int ext_te_gpio;
61 bool use_dsi_backlight;
63 struct omap_dsi_pin_config pin_config;
65 /* runtime variables */
66 bool enabled;
68 bool te_enabled;
70 atomic_t do_update;
71 int channel;
73 struct delayed_work te_timeout_work;
75 bool intro_printed;
77 struct workqueue_struct *workqueue;
79 bool ulps_enabled;
80 unsigned ulps_timeout;
81 struct delayed_work ulps_work;
84 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
86 static irqreturn_t dsicm_te_isr(int irq, void *data);
87 static void dsicm_te_timeout_work_callback(struct work_struct *work);
88 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable);
90 static int dsicm_panel_reset(struct panel_drv_data *ddata);
92 static void dsicm_ulps_work(struct work_struct *work);
94 static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec)
96 ddata->hw_guard_wait = msecs_to_jiffies(guard_msec);
97 ddata->hw_guard_end = jiffies + ddata->hw_guard_wait;
100 static void hw_guard_wait(struct panel_drv_data *ddata)
102 unsigned long wait = ddata->hw_guard_end - jiffies;
104 if ((long)wait > 0 && wait <= ddata->hw_guard_wait) {
105 set_current_state(TASK_UNINTERRUPTIBLE);
106 schedule_timeout(wait);
110 static int dsicm_dcs_read_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 *data)
112 struct omap_dss_device *in = ddata->in;
113 int r;
114 u8 buf[1];
116 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd, buf, 1);
118 if (r < 0)
119 return r;
121 *data = buf[0];
123 return 0;
126 static int dsicm_dcs_write_0(struct panel_drv_data *ddata, u8 dcs_cmd)
128 struct omap_dss_device *in = ddata->in;
129 return in->ops.dsi->dcs_write(in, ddata->channel, &dcs_cmd, 1);
132 static int dsicm_dcs_write_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 param)
134 struct omap_dss_device *in = ddata->in;
135 u8 buf[2] = { dcs_cmd, param };
137 return in->ops.dsi->dcs_write(in, ddata->channel, buf, 2);
140 static int dsicm_sleep_in(struct panel_drv_data *ddata)
143 struct omap_dss_device *in = ddata->in;
144 u8 cmd;
145 int r;
147 hw_guard_wait(ddata);
149 cmd = MIPI_DCS_ENTER_SLEEP_MODE;
150 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, &cmd, 1);
151 if (r)
152 return r;
154 hw_guard_start(ddata, 120);
156 usleep_range(5000, 10000);
158 return 0;
161 static int dsicm_sleep_out(struct panel_drv_data *ddata)
163 int r;
165 hw_guard_wait(ddata);
167 r = dsicm_dcs_write_0(ddata, MIPI_DCS_EXIT_SLEEP_MODE);
168 if (r)
169 return r;
171 hw_guard_start(ddata, 120);
173 usleep_range(5000, 10000);
175 return 0;
178 static int dsicm_get_id(struct panel_drv_data *ddata, u8 *id1, u8 *id2, u8 *id3)
180 int r;
182 r = dsicm_dcs_read_1(ddata, DCS_GET_ID1, id1);
183 if (r)
184 return r;
185 r = dsicm_dcs_read_1(ddata, DCS_GET_ID2, id2);
186 if (r)
187 return r;
188 r = dsicm_dcs_read_1(ddata, DCS_GET_ID3, id3);
189 if (r)
190 return r;
192 return 0;
195 static int dsicm_set_update_window(struct panel_drv_data *ddata,
196 u16 x, u16 y, u16 w, u16 h)
198 struct omap_dss_device *in = ddata->in;
199 int r;
200 u16 x1 = x;
201 u16 x2 = x + w - 1;
202 u16 y1 = y;
203 u16 y2 = y + h - 1;
205 u8 buf[5];
206 buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS;
207 buf[1] = (x1 >> 8) & 0xff;
208 buf[2] = (x1 >> 0) & 0xff;
209 buf[3] = (x2 >> 8) & 0xff;
210 buf[4] = (x2 >> 0) & 0xff;
212 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
213 if (r)
214 return r;
216 buf[0] = MIPI_DCS_SET_PAGE_ADDRESS;
217 buf[1] = (y1 >> 8) & 0xff;
218 buf[2] = (y1 >> 0) & 0xff;
219 buf[3] = (y2 >> 8) & 0xff;
220 buf[4] = (y2 >> 0) & 0xff;
222 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
223 if (r)
224 return r;
226 in->ops.dsi->bta_sync(in, ddata->channel);
228 return r;
231 static void dsicm_queue_ulps_work(struct panel_drv_data *ddata)
233 if (ddata->ulps_timeout > 0)
234 queue_delayed_work(ddata->workqueue, &ddata->ulps_work,
235 msecs_to_jiffies(ddata->ulps_timeout));
238 static void dsicm_cancel_ulps_work(struct panel_drv_data *ddata)
240 cancel_delayed_work(&ddata->ulps_work);
243 static int dsicm_enter_ulps(struct panel_drv_data *ddata)
245 struct omap_dss_device *in = ddata->in;
246 int r;
248 if (ddata->ulps_enabled)
249 return 0;
251 dsicm_cancel_ulps_work(ddata);
253 r = _dsicm_enable_te(ddata, false);
254 if (r)
255 goto err;
257 if (gpio_is_valid(ddata->ext_te_gpio))
258 disable_irq(gpio_to_irq(ddata->ext_te_gpio));
260 in->ops.dsi->disable(in, false, true);
262 ddata->ulps_enabled = true;
264 return 0;
266 err:
267 dev_err(&ddata->pdev->dev, "enter ULPS failed");
268 dsicm_panel_reset(ddata);
270 ddata->ulps_enabled = false;
272 dsicm_queue_ulps_work(ddata);
274 return r;
277 static int dsicm_exit_ulps(struct panel_drv_data *ddata)
279 struct omap_dss_device *in = ddata->in;
280 int r;
282 if (!ddata->ulps_enabled)
283 return 0;
285 r = in->ops.dsi->enable(in);
286 if (r) {
287 dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
288 goto err1;
291 in->ops.dsi->enable_hs(in, ddata->channel, true);
293 r = _dsicm_enable_te(ddata, true);
294 if (r) {
295 dev_err(&ddata->pdev->dev, "failed to re-enable TE");
296 goto err2;
299 if (gpio_is_valid(ddata->ext_te_gpio))
300 enable_irq(gpio_to_irq(ddata->ext_te_gpio));
302 dsicm_queue_ulps_work(ddata);
304 ddata->ulps_enabled = false;
306 return 0;
308 err2:
309 dev_err(&ddata->pdev->dev, "failed to exit ULPS");
311 r = dsicm_panel_reset(ddata);
312 if (!r) {
313 if (gpio_is_valid(ddata->ext_te_gpio))
314 enable_irq(gpio_to_irq(ddata->ext_te_gpio));
315 ddata->ulps_enabled = false;
317 err1:
318 dsicm_queue_ulps_work(ddata);
320 return r;
323 static int dsicm_wake_up(struct panel_drv_data *ddata)
325 if (ddata->ulps_enabled)
326 return dsicm_exit_ulps(ddata);
328 dsicm_cancel_ulps_work(ddata);
329 dsicm_queue_ulps_work(ddata);
330 return 0;
333 static int dsicm_bl_update_status(struct backlight_device *dev)
335 struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
336 struct omap_dss_device *in = ddata->in;
337 int r;
338 int level;
340 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
341 dev->props.power == FB_BLANK_UNBLANK)
342 level = dev->props.brightness;
343 else
344 level = 0;
346 dev_dbg(&ddata->pdev->dev, "update brightness to %d\n", level);
348 mutex_lock(&ddata->lock);
350 if (ddata->enabled) {
351 in->ops.dsi->bus_lock(in);
353 r = dsicm_wake_up(ddata);
354 if (!r)
355 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, level);
357 in->ops.dsi->bus_unlock(in);
358 } else {
359 r = 0;
362 mutex_unlock(&ddata->lock);
364 return r;
367 static int dsicm_bl_get_intensity(struct backlight_device *dev)
369 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
370 dev->props.power == FB_BLANK_UNBLANK)
371 return dev->props.brightness;
373 return 0;
376 static const struct backlight_ops dsicm_bl_ops = {
377 .get_brightness = dsicm_bl_get_intensity,
378 .update_status = dsicm_bl_update_status,
381 static void dsicm_get_resolution(struct omap_dss_device *dssdev,
382 u16 *xres, u16 *yres)
384 *xres = dssdev->panel.timings.x_res;
385 *yres = dssdev->panel.timings.y_res;
388 static ssize_t dsicm_num_errors_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
391 struct platform_device *pdev = to_platform_device(dev);
392 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
393 struct omap_dss_device *in = ddata->in;
394 u8 errors = 0;
395 int r;
397 mutex_lock(&ddata->lock);
399 if (ddata->enabled) {
400 in->ops.dsi->bus_lock(in);
402 r = dsicm_wake_up(ddata);
403 if (!r)
404 r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS,
405 &errors);
407 in->ops.dsi->bus_unlock(in);
408 } else {
409 r = -ENODEV;
412 mutex_unlock(&ddata->lock);
414 if (r)
415 return r;
417 return snprintf(buf, PAGE_SIZE, "%d\n", errors);
420 static ssize_t dsicm_hw_revision_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
423 struct platform_device *pdev = to_platform_device(dev);
424 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
425 struct omap_dss_device *in = ddata->in;
426 u8 id1, id2, id3;
427 int r;
429 mutex_lock(&ddata->lock);
431 if (ddata->enabled) {
432 in->ops.dsi->bus_lock(in);
434 r = dsicm_wake_up(ddata);
435 if (!r)
436 r = dsicm_get_id(ddata, &id1, &id2, &id3);
438 in->ops.dsi->bus_unlock(in);
439 } else {
440 r = -ENODEV;
443 mutex_unlock(&ddata->lock);
445 if (r)
446 return r;
448 return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
451 static ssize_t dsicm_store_ulps(struct device *dev,
452 struct device_attribute *attr,
453 const char *buf, size_t count)
455 struct platform_device *pdev = to_platform_device(dev);
456 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
457 struct omap_dss_device *in = ddata->in;
458 unsigned long t;
459 int r;
461 r = kstrtoul(buf, 0, &t);
462 if (r)
463 return r;
465 mutex_lock(&ddata->lock);
467 if (ddata->enabled) {
468 in->ops.dsi->bus_lock(in);
470 if (t)
471 r = dsicm_enter_ulps(ddata);
472 else
473 r = dsicm_wake_up(ddata);
475 in->ops.dsi->bus_unlock(in);
478 mutex_unlock(&ddata->lock);
480 if (r)
481 return r;
483 return count;
486 static ssize_t dsicm_show_ulps(struct device *dev,
487 struct device_attribute *attr,
488 char *buf)
490 struct platform_device *pdev = to_platform_device(dev);
491 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
492 unsigned t;
494 mutex_lock(&ddata->lock);
495 t = ddata->ulps_enabled;
496 mutex_unlock(&ddata->lock);
498 return snprintf(buf, PAGE_SIZE, "%u\n", t);
501 static ssize_t dsicm_store_ulps_timeout(struct device *dev,
502 struct device_attribute *attr,
503 const char *buf, size_t count)
505 struct platform_device *pdev = to_platform_device(dev);
506 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
507 struct omap_dss_device *in = ddata->in;
508 unsigned long t;
509 int r;
511 r = kstrtoul(buf, 0, &t);
512 if (r)
513 return r;
515 mutex_lock(&ddata->lock);
516 ddata->ulps_timeout = t;
518 if (ddata->enabled) {
519 /* dsicm_wake_up will restart the timer */
520 in->ops.dsi->bus_lock(in);
521 r = dsicm_wake_up(ddata);
522 in->ops.dsi->bus_unlock(in);
525 mutex_unlock(&ddata->lock);
527 if (r)
528 return r;
530 return count;
533 static ssize_t dsicm_show_ulps_timeout(struct device *dev,
534 struct device_attribute *attr,
535 char *buf)
537 struct platform_device *pdev = to_platform_device(dev);
538 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
539 unsigned t;
541 mutex_lock(&ddata->lock);
542 t = ddata->ulps_timeout;
543 mutex_unlock(&ddata->lock);
545 return snprintf(buf, PAGE_SIZE, "%u\n", t);
548 static DEVICE_ATTR(num_dsi_errors, S_IRUGO, dsicm_num_errors_show, NULL);
549 static DEVICE_ATTR(hw_revision, S_IRUGO, dsicm_hw_revision_show, NULL);
550 static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
551 dsicm_show_ulps, dsicm_store_ulps);
552 static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
553 dsicm_show_ulps_timeout, dsicm_store_ulps_timeout);
555 static struct attribute *dsicm_attrs[] = {
556 &dev_attr_num_dsi_errors.attr,
557 &dev_attr_hw_revision.attr,
558 &dev_attr_ulps.attr,
559 &dev_attr_ulps_timeout.attr,
560 NULL,
563 static struct attribute_group dsicm_attr_group = {
564 .attrs = dsicm_attrs,
567 static void dsicm_hw_reset(struct panel_drv_data *ddata)
569 if (!gpio_is_valid(ddata->reset_gpio))
570 return;
572 gpio_set_value(ddata->reset_gpio, 1);
573 udelay(10);
574 /* reset the panel */
575 gpio_set_value(ddata->reset_gpio, 0);
576 /* assert reset */
577 udelay(10);
578 gpio_set_value(ddata->reset_gpio, 1);
579 /* wait after releasing reset */
580 usleep_range(5000, 10000);
583 static int dsicm_power_on(struct panel_drv_data *ddata)
585 struct omap_dss_device *in = ddata->in;
586 u8 id1, id2, id3;
587 int r;
588 struct omap_dss_dsi_config dsi_config = {
589 .mode = OMAP_DSS_DSI_CMD_MODE,
590 .pixel_format = OMAP_DSS_DSI_FMT_RGB888,
591 .timings = &ddata->timings,
592 .hs_clk_min = 150000000,
593 .hs_clk_max = 300000000,
594 .lp_clk_min = 7000000,
595 .lp_clk_max = 10000000,
598 r = in->ops.dsi->configure_pins(in, &ddata->pin_config);
599 if (r) {
600 dev_err(&ddata->pdev->dev, "failed to configure DSI pins\n");
601 goto err0;
604 r = in->ops.dsi->set_config(in, &dsi_config);
605 if (r) {
606 dev_err(&ddata->pdev->dev, "failed to configure DSI\n");
607 goto err0;
610 r = in->ops.dsi->enable(in);
611 if (r) {
612 dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
613 goto err0;
616 dsicm_hw_reset(ddata);
618 in->ops.dsi->enable_hs(in, ddata->channel, false);
620 r = dsicm_sleep_out(ddata);
621 if (r)
622 goto err;
624 r = dsicm_get_id(ddata, &id1, &id2, &id3);
625 if (r)
626 goto err;
628 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff);
629 if (r)
630 goto err;
632 r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY,
633 (1<<2) | (1<<5)); /* BL | BCTRL */
634 if (r)
635 goto err;
637 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT,
638 MIPI_DCS_PIXEL_FMT_24BIT);
639 if (r)
640 goto err;
642 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON);
643 if (r)
644 goto err;
646 r = _dsicm_enable_te(ddata, ddata->te_enabled);
647 if (r)
648 goto err;
650 r = in->ops.dsi->enable_video_output(in, ddata->channel);
651 if (r)
652 goto err;
654 ddata->enabled = 1;
656 if (!ddata->intro_printed) {
657 dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n",
658 id1, id2, id3);
659 ddata->intro_printed = true;
662 in->ops.dsi->enable_hs(in, ddata->channel, true);
664 return 0;
665 err:
666 dev_err(&ddata->pdev->dev, "error while enabling panel, issuing HW reset\n");
668 dsicm_hw_reset(ddata);
670 in->ops.dsi->disable(in, true, false);
671 err0:
672 return r;
675 static void dsicm_power_off(struct panel_drv_data *ddata)
677 struct omap_dss_device *in = ddata->in;
678 int r;
680 in->ops.dsi->disable_video_output(in, ddata->channel);
682 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF);
683 if (!r)
684 r = dsicm_sleep_in(ddata);
686 if (r) {
687 dev_err(&ddata->pdev->dev,
688 "error disabling panel, issuing HW reset\n");
689 dsicm_hw_reset(ddata);
692 in->ops.dsi->disable(in, true, false);
694 ddata->enabled = 0;
697 static int dsicm_panel_reset(struct panel_drv_data *ddata)
699 dev_err(&ddata->pdev->dev, "performing LCD reset\n");
701 dsicm_power_off(ddata);
702 dsicm_hw_reset(ddata);
703 return dsicm_power_on(ddata);
706 static int dsicm_connect(struct omap_dss_device *dssdev)
708 struct panel_drv_data *ddata = to_panel_data(dssdev);
709 struct omap_dss_device *in = ddata->in;
710 struct device *dev = &ddata->pdev->dev;
711 int r;
713 if (omapdss_device_is_connected(dssdev))
714 return 0;
716 r = in->ops.dsi->connect(in, dssdev);
717 if (r) {
718 dev_err(dev, "Failed to connect to video source\n");
719 return r;
722 r = in->ops.dsi->request_vc(ddata->in, &ddata->channel);
723 if (r) {
724 dev_err(dev, "failed to get virtual channel\n");
725 goto err_req_vc;
728 r = in->ops.dsi->set_vc_id(ddata->in, ddata->channel, TCH);
729 if (r) {
730 dev_err(dev, "failed to set VC_ID\n");
731 goto err_vc_id;
734 return 0;
736 err_vc_id:
737 in->ops.dsi->release_vc(ddata->in, ddata->channel);
738 err_req_vc:
739 in->ops.dsi->disconnect(in, dssdev);
740 return r;
743 static void dsicm_disconnect(struct omap_dss_device *dssdev)
745 struct panel_drv_data *ddata = to_panel_data(dssdev);
746 struct omap_dss_device *in = ddata->in;
748 if (!omapdss_device_is_connected(dssdev))
749 return;
751 in->ops.dsi->release_vc(in, ddata->channel);
752 in->ops.dsi->disconnect(in, dssdev);
755 static int dsicm_enable(struct omap_dss_device *dssdev)
757 struct panel_drv_data *ddata = to_panel_data(dssdev);
758 struct omap_dss_device *in = ddata->in;
759 int r;
761 dev_dbg(&ddata->pdev->dev, "enable\n");
763 mutex_lock(&ddata->lock);
765 if (!omapdss_device_is_connected(dssdev)) {
766 r = -ENODEV;
767 goto err;
770 if (omapdss_device_is_enabled(dssdev)) {
771 r = 0;
772 goto err;
775 in->ops.dsi->bus_lock(in);
777 r = dsicm_power_on(ddata);
779 in->ops.dsi->bus_unlock(in);
781 if (r)
782 goto err;
784 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
786 mutex_unlock(&ddata->lock);
788 return 0;
789 err:
790 dev_dbg(&ddata->pdev->dev, "enable failed\n");
791 mutex_unlock(&ddata->lock);
792 return r;
795 static void dsicm_disable(struct omap_dss_device *dssdev)
797 struct panel_drv_data *ddata = to_panel_data(dssdev);
798 struct omap_dss_device *in = ddata->in;
799 int r;
801 dev_dbg(&ddata->pdev->dev, "disable\n");
803 mutex_lock(&ddata->lock);
805 dsicm_cancel_ulps_work(ddata);
807 in->ops.dsi->bus_lock(in);
809 if (omapdss_device_is_enabled(dssdev)) {
810 r = dsicm_wake_up(ddata);
811 if (!r)
812 dsicm_power_off(ddata);
815 in->ops.dsi->bus_unlock(in);
817 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
819 mutex_unlock(&ddata->lock);
822 static void dsicm_framedone_cb(int err, void *data)
824 struct panel_drv_data *ddata = data;
825 struct omap_dss_device *in = ddata->in;
827 dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err);
828 in->ops.dsi->bus_unlock(ddata->in);
831 static irqreturn_t dsicm_te_isr(int irq, void *data)
833 struct panel_drv_data *ddata = data;
834 struct omap_dss_device *in = ddata->in;
835 int old;
836 int r;
838 old = atomic_cmpxchg(&ddata->do_update, 1, 0);
840 if (old) {
841 cancel_delayed_work(&ddata->te_timeout_work);
843 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
844 ddata);
845 if (r)
846 goto err;
849 return IRQ_HANDLED;
850 err:
851 dev_err(&ddata->pdev->dev, "start update failed\n");
852 in->ops.dsi->bus_unlock(in);
853 return IRQ_HANDLED;
856 static void dsicm_te_timeout_work_callback(struct work_struct *work)
858 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
859 te_timeout_work.work);
860 struct omap_dss_device *in = ddata->in;
862 dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n");
864 atomic_set(&ddata->do_update, 0);
865 in->ops.dsi->bus_unlock(in);
868 static int dsicm_update(struct omap_dss_device *dssdev,
869 u16 x, u16 y, u16 w, u16 h)
871 struct panel_drv_data *ddata = to_panel_data(dssdev);
872 struct omap_dss_device *in = ddata->in;
873 int r;
875 dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
877 mutex_lock(&ddata->lock);
878 in->ops.dsi->bus_lock(in);
880 r = dsicm_wake_up(ddata);
881 if (r)
882 goto err;
884 if (!ddata->enabled) {
885 r = 0;
886 goto err;
889 /* XXX no need to send this every frame, but dsi break if not done */
890 r = dsicm_set_update_window(ddata, 0, 0,
891 dssdev->panel.timings.x_res,
892 dssdev->panel.timings.y_res);
893 if (r)
894 goto err;
896 if (ddata->te_enabled && gpio_is_valid(ddata->ext_te_gpio)) {
897 schedule_delayed_work(&ddata->te_timeout_work,
898 msecs_to_jiffies(250));
899 atomic_set(&ddata->do_update, 1);
900 } else {
901 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
902 ddata);
903 if (r)
904 goto err;
907 /* note: no bus_unlock here. unlock is in framedone_cb */
908 mutex_unlock(&ddata->lock);
909 return 0;
910 err:
911 in->ops.dsi->bus_unlock(in);
912 mutex_unlock(&ddata->lock);
913 return r;
916 static int dsicm_sync(struct omap_dss_device *dssdev)
918 struct panel_drv_data *ddata = to_panel_data(dssdev);
919 struct omap_dss_device *in = ddata->in;
921 dev_dbg(&ddata->pdev->dev, "sync\n");
923 mutex_lock(&ddata->lock);
924 in->ops.dsi->bus_lock(in);
925 in->ops.dsi->bus_unlock(in);
926 mutex_unlock(&ddata->lock);
928 dev_dbg(&ddata->pdev->dev, "sync done\n");
930 return 0;
933 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable)
935 struct omap_dss_device *in = ddata->in;
936 int r;
938 if (enable)
939 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0);
940 else
941 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF);
943 if (!gpio_is_valid(ddata->ext_te_gpio))
944 in->ops.dsi->enable_te(in, enable);
946 /* possible panel bug */
947 msleep(100);
949 return r;
952 static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable)
954 struct panel_drv_data *ddata = to_panel_data(dssdev);
955 struct omap_dss_device *in = ddata->in;
956 int r;
958 mutex_lock(&ddata->lock);
960 if (ddata->te_enabled == enable)
961 goto end;
963 in->ops.dsi->bus_lock(in);
965 if (ddata->enabled) {
966 r = dsicm_wake_up(ddata);
967 if (r)
968 goto err;
970 r = _dsicm_enable_te(ddata, enable);
971 if (r)
972 goto err;
975 ddata->te_enabled = enable;
977 in->ops.dsi->bus_unlock(in);
978 end:
979 mutex_unlock(&ddata->lock);
981 return 0;
982 err:
983 in->ops.dsi->bus_unlock(in);
984 mutex_unlock(&ddata->lock);
986 return r;
989 static int dsicm_get_te(struct omap_dss_device *dssdev)
991 struct panel_drv_data *ddata = to_panel_data(dssdev);
992 int r;
994 mutex_lock(&ddata->lock);
995 r = ddata->te_enabled;
996 mutex_unlock(&ddata->lock);
998 return r;
1001 static int dsicm_memory_read(struct omap_dss_device *dssdev,
1002 void *buf, size_t size,
1003 u16 x, u16 y, u16 w, u16 h)
1005 struct panel_drv_data *ddata = to_panel_data(dssdev);
1006 struct omap_dss_device *in = ddata->in;
1007 int r;
1008 int first = 1;
1009 int plen;
1010 unsigned buf_used = 0;
1012 if (size < w * h * 3)
1013 return -ENOMEM;
1015 mutex_lock(&ddata->lock);
1017 if (!ddata->enabled) {
1018 r = -ENODEV;
1019 goto err1;
1022 size = min(w * h * 3,
1023 dssdev->panel.timings.x_res *
1024 dssdev->panel.timings.y_res * 3);
1026 in->ops.dsi->bus_lock(in);
1028 r = dsicm_wake_up(ddata);
1029 if (r)
1030 goto err2;
1032 /* plen 1 or 2 goes into short packet. until checksum error is fixed,
1033 * use short packets. plen 32 works, but bigger packets seem to cause
1034 * an error. */
1035 if (size % 2)
1036 plen = 1;
1037 else
1038 plen = 2;
1040 dsicm_set_update_window(ddata, x, y, w, h);
1042 r = in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, plen);
1043 if (r)
1044 goto err2;
1046 while (buf_used < size) {
1047 u8 dcs_cmd = first ? 0x2e : 0x3e;
1048 first = 0;
1050 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd,
1051 buf + buf_used, size - buf_used);
1053 if (r < 0) {
1054 dev_err(dssdev->dev, "read error\n");
1055 goto err3;
1058 buf_used += r;
1060 if (r < plen) {
1061 dev_err(&ddata->pdev->dev, "short read\n");
1062 break;
1065 if (signal_pending(current)) {
1066 dev_err(&ddata->pdev->dev, "signal pending, "
1067 "aborting memory read\n");
1068 r = -ERESTARTSYS;
1069 goto err3;
1073 r = buf_used;
1075 err3:
1076 in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, 1);
1077 err2:
1078 in->ops.dsi->bus_unlock(in);
1079 err1:
1080 mutex_unlock(&ddata->lock);
1081 return r;
1084 static void dsicm_ulps_work(struct work_struct *work)
1086 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
1087 ulps_work.work);
1088 struct omap_dss_device *dssdev = &ddata->dssdev;
1089 struct omap_dss_device *in = ddata->in;
1091 mutex_lock(&ddata->lock);
1093 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) {
1094 mutex_unlock(&ddata->lock);
1095 return;
1098 in->ops.dsi->bus_lock(in);
1100 dsicm_enter_ulps(ddata);
1102 in->ops.dsi->bus_unlock(in);
1103 mutex_unlock(&ddata->lock);
1106 static struct omap_dss_driver dsicm_ops = {
1107 .connect = dsicm_connect,
1108 .disconnect = dsicm_disconnect,
1110 .enable = dsicm_enable,
1111 .disable = dsicm_disable,
1113 .update = dsicm_update,
1114 .sync = dsicm_sync,
1116 .get_resolution = dsicm_get_resolution,
1117 .get_recommended_bpp = omapdss_default_get_recommended_bpp,
1119 .enable_te = dsicm_enable_te,
1120 .get_te = dsicm_get_te,
1122 .memory_read = dsicm_memory_read,
1125 static int dsicm_probe_pdata(struct platform_device *pdev)
1127 const struct panel_dsicm_platform_data *pdata;
1128 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1129 struct omap_dss_device *dssdev, *in;
1131 pdata = dev_get_platdata(&pdev->dev);
1133 in = omap_dss_find_output(pdata->source);
1134 if (in == NULL) {
1135 dev_err(&pdev->dev, "failed to find video source\n");
1136 return -EPROBE_DEFER;
1138 ddata->in = in;
1140 ddata->reset_gpio = pdata->reset_gpio;
1142 if (pdata->use_ext_te)
1143 ddata->ext_te_gpio = pdata->ext_te_gpio;
1144 else
1145 ddata->ext_te_gpio = -1;
1147 ddata->ulps_timeout = pdata->ulps_timeout;
1149 ddata->use_dsi_backlight = pdata->use_dsi_backlight;
1151 ddata->pin_config = pdata->pin_config;
1153 dssdev = &ddata->dssdev;
1154 dssdev->name = pdata->name;
1156 return 0;
1159 static int dsicm_probe(struct platform_device *pdev)
1161 struct backlight_properties props;
1162 struct panel_drv_data *ddata;
1163 struct backlight_device *bldev = NULL;
1164 struct device *dev = &pdev->dev;
1165 struct omap_dss_device *dssdev;
1166 int r;
1168 dev_dbg(dev, "probe\n");
1170 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
1171 if (!ddata)
1172 return -ENOMEM;
1174 platform_set_drvdata(pdev, ddata);
1175 ddata->pdev = pdev;
1177 if (dev_get_platdata(dev)) {
1178 r = dsicm_probe_pdata(pdev);
1179 if (r)
1180 return r;
1181 } else {
1182 return -ENODEV;
1185 ddata->timings.x_res = 864;
1186 ddata->timings.y_res = 480;
1187 ddata->timings.pixel_clock = DIV_ROUND_UP(864 * 480 * 60, 1000);
1189 dssdev = &ddata->dssdev;
1190 dssdev->dev = dev;
1191 dssdev->driver = &dsicm_ops;
1192 dssdev->panel.timings = ddata->timings;
1193 dssdev->type = OMAP_DISPLAY_TYPE_DSI;
1194 dssdev->owner = THIS_MODULE;
1196 dssdev->panel.dsi_pix_fmt = OMAP_DSS_DSI_FMT_RGB888;
1197 dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
1198 OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
1200 r = omapdss_register_display(dssdev);
1201 if (r) {
1202 dev_err(dev, "Failed to register panel\n");
1203 goto err_reg;
1206 mutex_init(&ddata->lock);
1208 atomic_set(&ddata->do_update, 0);
1210 if (gpio_is_valid(ddata->reset_gpio)) {
1211 r = devm_gpio_request_one(dev, ddata->reset_gpio,
1212 GPIOF_OUT_INIT_LOW, "taal rst");
1213 if (r) {
1214 dev_err(dev, "failed to request reset gpio\n");
1215 return r;
1219 if (gpio_is_valid(ddata->ext_te_gpio)) {
1220 r = devm_gpio_request_one(dev, ddata->ext_te_gpio,
1221 GPIOF_IN, "taal irq");
1222 if (r) {
1223 dev_err(dev, "GPIO request failed\n");
1224 return r;
1227 r = devm_request_irq(dev, gpio_to_irq(ddata->ext_te_gpio),
1228 dsicm_te_isr,
1229 IRQF_TRIGGER_RISING,
1230 "taal vsync", ddata);
1232 if (r) {
1233 dev_err(dev, "IRQ request failed\n");
1234 return r;
1237 INIT_DEFERRABLE_WORK(&ddata->te_timeout_work,
1238 dsicm_te_timeout_work_callback);
1240 dev_dbg(dev, "Using GPIO TE\n");
1243 ddata->workqueue = create_singlethread_workqueue("dsicm_wq");
1244 if (ddata->workqueue == NULL) {
1245 dev_err(dev, "can't create workqueue\n");
1246 return -ENOMEM;
1248 INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work);
1250 dsicm_hw_reset(ddata);
1252 if (ddata->use_dsi_backlight) {
1253 memset(&props, 0, sizeof(struct backlight_properties));
1254 props.max_brightness = 255;
1256 props.type = BACKLIGHT_RAW;
1257 bldev = backlight_device_register(dev_name(dev),
1258 dev, ddata, &dsicm_bl_ops, &props);
1259 if (IS_ERR(bldev)) {
1260 r = PTR_ERR(bldev);
1261 goto err_bl;
1264 ddata->bldev = bldev;
1266 bldev->props.fb_blank = FB_BLANK_UNBLANK;
1267 bldev->props.power = FB_BLANK_UNBLANK;
1268 bldev->props.brightness = 255;
1270 dsicm_bl_update_status(bldev);
1273 r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
1274 if (r) {
1275 dev_err(dev, "failed to create sysfs files\n");
1276 goto err_sysfs_create;
1279 return 0;
1281 err_sysfs_create:
1282 if (bldev != NULL)
1283 backlight_device_unregister(bldev);
1284 err_bl:
1285 destroy_workqueue(ddata->workqueue);
1286 err_reg:
1287 return r;
1290 static int __exit dsicm_remove(struct platform_device *pdev)
1292 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1293 struct omap_dss_device *dssdev = &ddata->dssdev;
1294 struct backlight_device *bldev;
1296 dev_dbg(&pdev->dev, "remove\n");
1298 omapdss_unregister_display(dssdev);
1300 dsicm_disable(dssdev);
1301 dsicm_disconnect(dssdev);
1303 sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group);
1305 bldev = ddata->bldev;
1306 if (bldev != NULL) {
1307 bldev->props.power = FB_BLANK_POWERDOWN;
1308 dsicm_bl_update_status(bldev);
1309 backlight_device_unregister(bldev);
1312 omap_dss_put_device(ddata->in);
1314 dsicm_cancel_ulps_work(ddata);
1315 destroy_workqueue(ddata->workqueue);
1317 /* reset, to be sure that the panel is in a valid state */
1318 dsicm_hw_reset(ddata);
1320 return 0;
1323 static struct platform_driver dsicm_driver = {
1324 .probe = dsicm_probe,
1325 .remove = __exit_p(dsicm_remove),
1326 .driver = {
1327 .name = "panel-dsi-cm",
1328 .owner = THIS_MODULE,
1332 module_platform_driver(dsicm_driver);
1334 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
1335 MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
1336 MODULE_LICENSE("GPL");