Merge tag 'regmap-v4.11' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6-block.git] / drivers / gpu / drm / omapdrm / displays / panel-dsi-cm.c
CommitLineData
dbc23840
TV
1/*
2 * Generic DSI Command Mode panel driver
3 *
4 * Copyright (C) 2013 Texas Instruments
5 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
6 *
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.
10 */
11
12/* #define DEBUG */
13
14#include <linux/backlight.h>
15#include <linux/delay.h>
d0196c8d 16#include <linux/gpio/consumer.h>
dbc23840
TV
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.h>
22#include <linux/slab.h>
23#include <linux/workqueue.h>
cdeeaca2
TV
24#include <linux/of_device.h>
25#include <linux/of_gpio.h>
dbc23840 26
dbc23840
TV
27#include <video/mipi_display.h>
28
32043da7
PU
29#include "../dss/omapdss.h"
30
dbc23840
TV
31/* DSI Virtual channel. Hardcoded for now. */
32#define TCH 0
33
34#define DCS_READ_NUM_ERRORS 0x05
35#define DCS_BRIGHTNESS 0x51
36#define DCS_CTRL_DISPLAY 0x53
37#define DCS_GET_ID1 0xda
38#define DCS_GET_ID2 0xdb
39#define DCS_GET_ID3 0xdc
40
41struct panel_drv_data {
42 struct omap_dss_device dssdev;
43 struct omap_dss_device *in;
44
da11bbbb 45 struct videomode vm;
dbc23840
TV
46
47 struct platform_device *pdev;
48
49 struct mutex lock;
50
51 struct backlight_device *bldev;
52
53 unsigned long hw_guard_end; /* next value of jiffies when we can
54 * issue the next sleep in/out command
55 */
56 unsigned long hw_guard_wait; /* max guard time in jiffies */
57
58 /* panel HW configuration from DT or platform data */
59 int reset_gpio;
60 int ext_te_gpio;
61
62 bool use_dsi_backlight;
63
64 struct omap_dsi_pin_config pin_config;
65
66 /* runtime variables */
67 bool enabled;
68
69 bool te_enabled;
70
71 atomic_t do_update;
72 int channel;
73
74 struct delayed_work te_timeout_work;
75
76 bool intro_printed;
77
78 struct workqueue_struct *workqueue;
79
80 bool ulps_enabled;
81 unsigned ulps_timeout;
82 struct delayed_work ulps_work;
83};
84
85#define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
86
87static irqreturn_t dsicm_te_isr(int irq, void *data);
88static void dsicm_te_timeout_work_callback(struct work_struct *work);
89static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable);
90
91static int dsicm_panel_reset(struct panel_drv_data *ddata);
92
93static void dsicm_ulps_work(struct work_struct *work);
94
95static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec)
96{
97 ddata->hw_guard_wait = msecs_to_jiffies(guard_msec);
98 ddata->hw_guard_end = jiffies + ddata->hw_guard_wait;
99}
100
101static void hw_guard_wait(struct panel_drv_data *ddata)
102{
103 unsigned long wait = ddata->hw_guard_end - jiffies;
104
105 if ((long)wait > 0 && wait <= ddata->hw_guard_wait) {
106 set_current_state(TASK_UNINTERRUPTIBLE);
107 schedule_timeout(wait);
108 }
109}
110
111static int dsicm_dcs_read_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 *data)
112{
113 struct omap_dss_device *in = ddata->in;
114 int r;
115 u8 buf[1];
116
117 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd, buf, 1);
118
119 if (r < 0)
120 return r;
121
122 *data = buf[0];
123
124 return 0;
125}
126
127static int dsicm_dcs_write_0(struct panel_drv_data *ddata, u8 dcs_cmd)
128{
129 struct omap_dss_device *in = ddata->in;
130 return in->ops.dsi->dcs_write(in, ddata->channel, &dcs_cmd, 1);
131}
132
133static int dsicm_dcs_write_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 param)
134{
135 struct omap_dss_device *in = ddata->in;
136 u8 buf[2] = { dcs_cmd, param };
137
138 return in->ops.dsi->dcs_write(in, ddata->channel, buf, 2);
139}
140
141static int dsicm_sleep_in(struct panel_drv_data *ddata)
142
143{
144 struct omap_dss_device *in = ddata->in;
145 u8 cmd;
146 int r;
147
148 hw_guard_wait(ddata);
149
150 cmd = MIPI_DCS_ENTER_SLEEP_MODE;
151 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, &cmd, 1);
152 if (r)
153 return r;
154
155 hw_guard_start(ddata, 120);
156
157 usleep_range(5000, 10000);
158
159 return 0;
160}
161
162static int dsicm_sleep_out(struct panel_drv_data *ddata)
163{
164 int r;
165
166 hw_guard_wait(ddata);
167
168 r = dsicm_dcs_write_0(ddata, MIPI_DCS_EXIT_SLEEP_MODE);
169 if (r)
170 return r;
171
172 hw_guard_start(ddata, 120);
173
174 usleep_range(5000, 10000);
175
176 return 0;
177}
178
179static int dsicm_get_id(struct panel_drv_data *ddata, u8 *id1, u8 *id2, u8 *id3)
180{
181 int r;
182
183 r = dsicm_dcs_read_1(ddata, DCS_GET_ID1, id1);
184 if (r)
185 return r;
186 r = dsicm_dcs_read_1(ddata, DCS_GET_ID2, id2);
187 if (r)
188 return r;
189 r = dsicm_dcs_read_1(ddata, DCS_GET_ID3, id3);
190 if (r)
191 return r;
192
193 return 0;
194}
195
196static int dsicm_set_update_window(struct panel_drv_data *ddata,
197 u16 x, u16 y, u16 w, u16 h)
198{
199 struct omap_dss_device *in = ddata->in;
200 int r;
201 u16 x1 = x;
202 u16 x2 = x + w - 1;
203 u16 y1 = y;
204 u16 y2 = y + h - 1;
205
206 u8 buf[5];
207 buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS;
208 buf[1] = (x1 >> 8) & 0xff;
209 buf[2] = (x1 >> 0) & 0xff;
210 buf[3] = (x2 >> 8) & 0xff;
211 buf[4] = (x2 >> 0) & 0xff;
212
213 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
214 if (r)
215 return r;
216
217 buf[0] = MIPI_DCS_SET_PAGE_ADDRESS;
218 buf[1] = (y1 >> 8) & 0xff;
219 buf[2] = (y1 >> 0) & 0xff;
220 buf[3] = (y2 >> 8) & 0xff;
221 buf[4] = (y2 >> 0) & 0xff;
222
223 r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
224 if (r)
225 return r;
226
227 in->ops.dsi->bta_sync(in, ddata->channel);
228
229 return r;
230}
231
232static void dsicm_queue_ulps_work(struct panel_drv_data *ddata)
233{
234 if (ddata->ulps_timeout > 0)
235 queue_delayed_work(ddata->workqueue, &ddata->ulps_work,
236 msecs_to_jiffies(ddata->ulps_timeout));
237}
238
239static void dsicm_cancel_ulps_work(struct panel_drv_data *ddata)
240{
241 cancel_delayed_work(&ddata->ulps_work);
242}
243
244static int dsicm_enter_ulps(struct panel_drv_data *ddata)
245{
246 struct omap_dss_device *in = ddata->in;
247 int r;
248
249 if (ddata->ulps_enabled)
250 return 0;
251
252 dsicm_cancel_ulps_work(ddata);
253
254 r = _dsicm_enable_te(ddata, false);
255 if (r)
256 goto err;
257
258 if (gpio_is_valid(ddata->ext_te_gpio))
259 disable_irq(gpio_to_irq(ddata->ext_te_gpio));
260
261 in->ops.dsi->disable(in, false, true);
262
263 ddata->ulps_enabled = true;
264
265 return 0;
266
267err:
268 dev_err(&ddata->pdev->dev, "enter ULPS failed");
269 dsicm_panel_reset(ddata);
270
271 ddata->ulps_enabled = false;
272
273 dsicm_queue_ulps_work(ddata);
274
275 return r;
276}
277
278static int dsicm_exit_ulps(struct panel_drv_data *ddata)
279{
280 struct omap_dss_device *in = ddata->in;
281 int r;
282
283 if (!ddata->ulps_enabled)
284 return 0;
285
286 r = in->ops.dsi->enable(in);
287 if (r) {
288 dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
289 goto err1;
290 }
291
292 in->ops.dsi->enable_hs(in, ddata->channel, true);
293
294 r = _dsicm_enable_te(ddata, true);
295 if (r) {
296 dev_err(&ddata->pdev->dev, "failed to re-enable TE");
297 goto err2;
298 }
299
300 if (gpio_is_valid(ddata->ext_te_gpio))
301 enable_irq(gpio_to_irq(ddata->ext_te_gpio));
302
303 dsicm_queue_ulps_work(ddata);
304
305 ddata->ulps_enabled = false;
306
307 return 0;
308
309err2:
310 dev_err(&ddata->pdev->dev, "failed to exit ULPS");
311
312 r = dsicm_panel_reset(ddata);
313 if (!r) {
314 if (gpio_is_valid(ddata->ext_te_gpio))
315 enable_irq(gpio_to_irq(ddata->ext_te_gpio));
316 ddata->ulps_enabled = false;
317 }
318err1:
319 dsicm_queue_ulps_work(ddata);
320
321 return r;
322}
323
324static int dsicm_wake_up(struct panel_drv_data *ddata)
325{
326 if (ddata->ulps_enabled)
327 return dsicm_exit_ulps(ddata);
328
329 dsicm_cancel_ulps_work(ddata);
330 dsicm_queue_ulps_work(ddata);
331 return 0;
332}
333
334static int dsicm_bl_update_status(struct backlight_device *dev)
335{
336 struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
337 struct omap_dss_device *in = ddata->in;
338 int r;
339 int level;
340
341 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
342 dev->props.power == FB_BLANK_UNBLANK)
343 level = dev->props.brightness;
344 else
345 level = 0;
346
347 dev_dbg(&ddata->pdev->dev, "update brightness to %d\n", level);
348
349 mutex_lock(&ddata->lock);
350
351 if (ddata->enabled) {
352 in->ops.dsi->bus_lock(in);
353
354 r = dsicm_wake_up(ddata);
355 if (!r)
356 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, level);
357
358 in->ops.dsi->bus_unlock(in);
359 } else {
360 r = 0;
361 }
362
363 mutex_unlock(&ddata->lock);
364
365 return r;
366}
367
368static int dsicm_bl_get_intensity(struct backlight_device *dev)
369{
370 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
371 dev->props.power == FB_BLANK_UNBLANK)
372 return dev->props.brightness;
373
374 return 0;
375}
376
377static const struct backlight_ops dsicm_bl_ops = {
378 .get_brightness = dsicm_bl_get_intensity,
379 .update_status = dsicm_bl_update_status,
380};
381
382static void dsicm_get_resolution(struct omap_dss_device *dssdev,
383 u16 *xres, u16 *yres)
384{
da11bbbb
PU
385 *xres = dssdev->panel.vm.hactive;
386 *yres = dssdev->panel.vm.vactive;
dbc23840
TV
387}
388
389static ssize_t dsicm_num_errors_show(struct device *dev,
390 struct device_attribute *attr, char *buf)
391{
392 struct platform_device *pdev = to_platform_device(dev);
393 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
394 struct omap_dss_device *in = ddata->in;
395 u8 errors = 0;
396 int r;
397
398 mutex_lock(&ddata->lock);
399
400 if (ddata->enabled) {
401 in->ops.dsi->bus_lock(in);
402
403 r = dsicm_wake_up(ddata);
404 if (!r)
405 r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS,
406 &errors);
407
408 in->ops.dsi->bus_unlock(in);
409 } else {
410 r = -ENODEV;
411 }
412
413 mutex_unlock(&ddata->lock);
414
415 if (r)
416 return r;
417
418 return snprintf(buf, PAGE_SIZE, "%d\n", errors);
419}
420
421static ssize_t dsicm_hw_revision_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
423{
424 struct platform_device *pdev = to_platform_device(dev);
425 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
426 struct omap_dss_device *in = ddata->in;
427 u8 id1, id2, id3;
428 int r;
429
430 mutex_lock(&ddata->lock);
431
432 if (ddata->enabled) {
433 in->ops.dsi->bus_lock(in);
434
435 r = dsicm_wake_up(ddata);
436 if (!r)
437 r = dsicm_get_id(ddata, &id1, &id2, &id3);
438
439 in->ops.dsi->bus_unlock(in);
440 } else {
441 r = -ENODEV;
442 }
443
444 mutex_unlock(&ddata->lock);
445
446 if (r)
447 return r;
448
449 return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
450}
451
452static ssize_t dsicm_store_ulps(struct device *dev,
453 struct device_attribute *attr,
454 const char *buf, size_t count)
455{
456 struct platform_device *pdev = to_platform_device(dev);
457 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
458 struct omap_dss_device *in = ddata->in;
459 unsigned long t;
460 int r;
461
462 r = kstrtoul(buf, 0, &t);
463 if (r)
464 return r;
465
466 mutex_lock(&ddata->lock);
467
468 if (ddata->enabled) {
469 in->ops.dsi->bus_lock(in);
470
471 if (t)
472 r = dsicm_enter_ulps(ddata);
473 else
474 r = dsicm_wake_up(ddata);
475
476 in->ops.dsi->bus_unlock(in);
477 }
478
479 mutex_unlock(&ddata->lock);
480
481 if (r)
482 return r;
483
484 return count;
485}
486
487static ssize_t dsicm_show_ulps(struct device *dev,
488 struct device_attribute *attr,
489 char *buf)
490{
491 struct platform_device *pdev = to_platform_device(dev);
492 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
493 unsigned t;
494
495 mutex_lock(&ddata->lock);
496 t = ddata->ulps_enabled;
497 mutex_unlock(&ddata->lock);
498
499 return snprintf(buf, PAGE_SIZE, "%u\n", t);
500}
501
502static ssize_t dsicm_store_ulps_timeout(struct device *dev,
503 struct device_attribute *attr,
504 const char *buf, size_t count)
505{
506 struct platform_device *pdev = to_platform_device(dev);
507 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
508 struct omap_dss_device *in = ddata->in;
509 unsigned long t;
510 int r;
511
512 r = kstrtoul(buf, 0, &t);
513 if (r)
514 return r;
515
516 mutex_lock(&ddata->lock);
517 ddata->ulps_timeout = t;
518
519 if (ddata->enabled) {
520 /* dsicm_wake_up will restart the timer */
521 in->ops.dsi->bus_lock(in);
522 r = dsicm_wake_up(ddata);
523 in->ops.dsi->bus_unlock(in);
524 }
525
526 mutex_unlock(&ddata->lock);
527
528 if (r)
529 return r;
530
531 return count;
532}
533
534static ssize_t dsicm_show_ulps_timeout(struct device *dev,
535 struct device_attribute *attr,
536 char *buf)
537{
538 struct platform_device *pdev = to_platform_device(dev);
539 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
540 unsigned t;
541
542 mutex_lock(&ddata->lock);
543 t = ddata->ulps_timeout;
544 mutex_unlock(&ddata->lock);
545
546 return snprintf(buf, PAGE_SIZE, "%u\n", t);
547}
548
549static DEVICE_ATTR(num_dsi_errors, S_IRUGO, dsicm_num_errors_show, NULL);
550static DEVICE_ATTR(hw_revision, S_IRUGO, dsicm_hw_revision_show, NULL);
551static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
552 dsicm_show_ulps, dsicm_store_ulps);
553static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
554 dsicm_show_ulps_timeout, dsicm_store_ulps_timeout);
555
556static struct attribute *dsicm_attrs[] = {
557 &dev_attr_num_dsi_errors.attr,
558 &dev_attr_hw_revision.attr,
559 &dev_attr_ulps.attr,
560 &dev_attr_ulps_timeout.attr,
561 NULL,
562};
563
564static struct attribute_group dsicm_attr_group = {
565 .attrs = dsicm_attrs,
566};
567
568static void dsicm_hw_reset(struct panel_drv_data *ddata)
569{
570 if (!gpio_is_valid(ddata->reset_gpio))
571 return;
572
573 gpio_set_value(ddata->reset_gpio, 1);
574 udelay(10);
575 /* reset the panel */
576 gpio_set_value(ddata->reset_gpio, 0);
577 /* assert reset */
578 udelay(10);
579 gpio_set_value(ddata->reset_gpio, 1);
580 /* wait after releasing reset */
581 usleep_range(5000, 10000);
582}
583
584static int dsicm_power_on(struct panel_drv_data *ddata)
585{
586 struct omap_dss_device *in = ddata->in;
587 u8 id1, id2, id3;
588 int r;
589 struct omap_dss_dsi_config dsi_config = {
590 .mode = OMAP_DSS_DSI_CMD_MODE,
591 .pixel_format = OMAP_DSS_DSI_FMT_RGB888,
da11bbbb 592 .vm = &ddata->vm,
dbc23840
TV
593 .hs_clk_min = 150000000,
594 .hs_clk_max = 300000000,
595 .lp_clk_min = 7000000,
596 .lp_clk_max = 10000000,
597 };
598
cdeeaca2
TV
599 if (ddata->pin_config.num_pins > 0) {
600 r = in->ops.dsi->configure_pins(in, &ddata->pin_config);
601 if (r) {
602 dev_err(&ddata->pdev->dev,
603 "failed to configure DSI pins\n");
604 goto err0;
605 }
cf6ac4ce 606 }
dbc23840
TV
607
608 r = in->ops.dsi->set_config(in, &dsi_config);
609 if (r) {
610 dev_err(&ddata->pdev->dev, "failed to configure DSI\n");
611 goto err0;
612 }
613
614 r = in->ops.dsi->enable(in);
615 if (r) {
616 dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
617 goto err0;
618 }
619
620 dsicm_hw_reset(ddata);
621
622 in->ops.dsi->enable_hs(in, ddata->channel, false);
623
624 r = dsicm_sleep_out(ddata);
625 if (r)
626 goto err;
627
628 r = dsicm_get_id(ddata, &id1, &id2, &id3);
629 if (r)
630 goto err;
631
632 r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff);
633 if (r)
634 goto err;
635
636 r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY,
637 (1<<2) | (1<<5)); /* BL | BCTRL */
638 if (r)
639 goto err;
640
641 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT,
642 MIPI_DCS_PIXEL_FMT_24BIT);
643 if (r)
644 goto err;
645
646 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON);
647 if (r)
648 goto err;
649
650 r = _dsicm_enable_te(ddata, ddata->te_enabled);
651 if (r)
652 goto err;
653
654 r = in->ops.dsi->enable_video_output(in, ddata->channel);
655 if (r)
656 goto err;
657
658 ddata->enabled = 1;
659
660 if (!ddata->intro_printed) {
661 dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n",
662 id1, id2, id3);
663 ddata->intro_printed = true;
664 }
665
666 in->ops.dsi->enable_hs(in, ddata->channel, true);
667
668 return 0;
669err:
670 dev_err(&ddata->pdev->dev, "error while enabling panel, issuing HW reset\n");
671
672 dsicm_hw_reset(ddata);
673
674 in->ops.dsi->disable(in, true, false);
675err0:
676 return r;
677}
678
679static void dsicm_power_off(struct panel_drv_data *ddata)
680{
681 struct omap_dss_device *in = ddata->in;
682 int r;
683
684 in->ops.dsi->disable_video_output(in, ddata->channel);
685
686 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF);
687 if (!r)
688 r = dsicm_sleep_in(ddata);
689
690 if (r) {
691 dev_err(&ddata->pdev->dev,
692 "error disabling panel, issuing HW reset\n");
693 dsicm_hw_reset(ddata);
694 }
695
696 in->ops.dsi->disable(in, true, false);
697
698 ddata->enabled = 0;
699}
700
701static int dsicm_panel_reset(struct panel_drv_data *ddata)
702{
703 dev_err(&ddata->pdev->dev, "performing LCD reset\n");
704
705 dsicm_power_off(ddata);
706 dsicm_hw_reset(ddata);
707 return dsicm_power_on(ddata);
708}
709
710static int dsicm_connect(struct omap_dss_device *dssdev)
711{
712 struct panel_drv_data *ddata = to_panel_data(dssdev);
713 struct omap_dss_device *in = ddata->in;
714 struct device *dev = &ddata->pdev->dev;
715 int r;
716
717 if (omapdss_device_is_connected(dssdev))
718 return 0;
719
720 r = in->ops.dsi->connect(in, dssdev);
721 if (r) {
722 dev_err(dev, "Failed to connect to video source\n");
723 return r;
724 }
725
726 r = in->ops.dsi->request_vc(ddata->in, &ddata->channel);
727 if (r) {
728 dev_err(dev, "failed to get virtual channel\n");
729 goto err_req_vc;
730 }
731
732 r = in->ops.dsi->set_vc_id(ddata->in, ddata->channel, TCH);
733 if (r) {
734 dev_err(dev, "failed to set VC_ID\n");
735 goto err_vc_id;
736 }
737
738 return 0;
739
740err_vc_id:
741 in->ops.dsi->release_vc(ddata->in, ddata->channel);
742err_req_vc:
743 in->ops.dsi->disconnect(in, dssdev);
744 return r;
745}
746
747static void dsicm_disconnect(struct omap_dss_device *dssdev)
748{
749 struct panel_drv_data *ddata = to_panel_data(dssdev);
750 struct omap_dss_device *in = ddata->in;
751
752 if (!omapdss_device_is_connected(dssdev))
753 return;
754
755 in->ops.dsi->release_vc(in, ddata->channel);
756 in->ops.dsi->disconnect(in, dssdev);
757}
758
759static int dsicm_enable(struct omap_dss_device *dssdev)
760{
761 struct panel_drv_data *ddata = to_panel_data(dssdev);
762 struct omap_dss_device *in = ddata->in;
763 int r;
764
765 dev_dbg(&ddata->pdev->dev, "enable\n");
766
767 mutex_lock(&ddata->lock);
768
769 if (!omapdss_device_is_connected(dssdev)) {
770 r = -ENODEV;
771 goto err;
772 }
773
774 if (omapdss_device_is_enabled(dssdev)) {
775 r = 0;
776 goto err;
777 }
778
779 in->ops.dsi->bus_lock(in);
780
781 r = dsicm_power_on(ddata);
782
783 in->ops.dsi->bus_unlock(in);
784
785 if (r)
786 goto err;
787
788 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
789
790 mutex_unlock(&ddata->lock);
791
792 return 0;
793err:
794 dev_dbg(&ddata->pdev->dev, "enable failed\n");
795 mutex_unlock(&ddata->lock);
796 return r;
797}
798
799static void dsicm_disable(struct omap_dss_device *dssdev)
800{
801 struct panel_drv_data *ddata = to_panel_data(dssdev);
802 struct omap_dss_device *in = ddata->in;
803 int r;
804
805 dev_dbg(&ddata->pdev->dev, "disable\n");
806
807 mutex_lock(&ddata->lock);
808
809 dsicm_cancel_ulps_work(ddata);
810
811 in->ops.dsi->bus_lock(in);
812
813 if (omapdss_device_is_enabled(dssdev)) {
814 r = dsicm_wake_up(ddata);
815 if (!r)
816 dsicm_power_off(ddata);
817 }
818
819 in->ops.dsi->bus_unlock(in);
820
821 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
822
823 mutex_unlock(&ddata->lock);
824}
825
826static void dsicm_framedone_cb(int err, void *data)
827{
828 struct panel_drv_data *ddata = data;
829 struct omap_dss_device *in = ddata->in;
830
831 dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err);
832 in->ops.dsi->bus_unlock(ddata->in);
833}
834
835static irqreturn_t dsicm_te_isr(int irq, void *data)
836{
837 struct panel_drv_data *ddata = data;
838 struct omap_dss_device *in = ddata->in;
839 int old;
840 int r;
841
842 old = atomic_cmpxchg(&ddata->do_update, 1, 0);
843
844 if (old) {
845 cancel_delayed_work(&ddata->te_timeout_work);
846
847 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
848 ddata);
849 if (r)
850 goto err;
851 }
852
853 return IRQ_HANDLED;
854err:
855 dev_err(&ddata->pdev->dev, "start update failed\n");
856 in->ops.dsi->bus_unlock(in);
857 return IRQ_HANDLED;
858}
859
860static void dsicm_te_timeout_work_callback(struct work_struct *work)
861{
862 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
863 te_timeout_work.work);
864 struct omap_dss_device *in = ddata->in;
865
866 dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n");
867
868 atomic_set(&ddata->do_update, 0);
869 in->ops.dsi->bus_unlock(in);
870}
871
872static int dsicm_update(struct omap_dss_device *dssdev,
873 u16 x, u16 y, u16 w, u16 h)
874{
875 struct panel_drv_data *ddata = to_panel_data(dssdev);
876 struct omap_dss_device *in = ddata->in;
877 int r;
878
879 dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
880
881 mutex_lock(&ddata->lock);
882 in->ops.dsi->bus_lock(in);
883
884 r = dsicm_wake_up(ddata);
885 if (r)
886 goto err;
887
888 if (!ddata->enabled) {
889 r = 0;
890 goto err;
891 }
892
893 /* XXX no need to send this every frame, but dsi break if not done */
894 r = dsicm_set_update_window(ddata, 0, 0,
da11bbbb
PU
895 dssdev->panel.vm.hactive,
896 dssdev->panel.vm.vactive);
dbc23840
TV
897 if (r)
898 goto err;
899
900 if (ddata->te_enabled && gpio_is_valid(ddata->ext_te_gpio)) {
901 schedule_delayed_work(&ddata->te_timeout_work,
902 msecs_to_jiffies(250));
903 atomic_set(&ddata->do_update, 1);
904 } else {
905 r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
906 ddata);
907 if (r)
908 goto err;
909 }
910
911 /* note: no bus_unlock here. unlock is in framedone_cb */
912 mutex_unlock(&ddata->lock);
913 return 0;
914err:
915 in->ops.dsi->bus_unlock(in);
916 mutex_unlock(&ddata->lock);
917 return r;
918}
919
920static int dsicm_sync(struct omap_dss_device *dssdev)
921{
922 struct panel_drv_data *ddata = to_panel_data(dssdev);
923 struct omap_dss_device *in = ddata->in;
924
925 dev_dbg(&ddata->pdev->dev, "sync\n");
926
927 mutex_lock(&ddata->lock);
928 in->ops.dsi->bus_lock(in);
929 in->ops.dsi->bus_unlock(in);
930 mutex_unlock(&ddata->lock);
931
932 dev_dbg(&ddata->pdev->dev, "sync done\n");
933
934 return 0;
935}
936
937static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable)
938{
939 struct omap_dss_device *in = ddata->in;
940 int r;
941
942 if (enable)
943 r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0);
944 else
945 r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF);
946
947 if (!gpio_is_valid(ddata->ext_te_gpio))
948 in->ops.dsi->enable_te(in, enable);
949
950 /* possible panel bug */
951 msleep(100);
952
953 return r;
954}
955
956static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable)
957{
958 struct panel_drv_data *ddata = to_panel_data(dssdev);
959 struct omap_dss_device *in = ddata->in;
960 int r;
961
962 mutex_lock(&ddata->lock);
963
964 if (ddata->te_enabled == enable)
965 goto end;
966
967 in->ops.dsi->bus_lock(in);
968
969 if (ddata->enabled) {
970 r = dsicm_wake_up(ddata);
971 if (r)
972 goto err;
973
974 r = _dsicm_enable_te(ddata, enable);
975 if (r)
976 goto err;
977 }
978
979 ddata->te_enabled = enable;
980
981 in->ops.dsi->bus_unlock(in);
982end:
983 mutex_unlock(&ddata->lock);
984
985 return 0;
986err:
987 in->ops.dsi->bus_unlock(in);
988 mutex_unlock(&ddata->lock);
989
990 return r;
991}
992
993static int dsicm_get_te(struct omap_dss_device *dssdev)
994{
995 struct panel_drv_data *ddata = to_panel_data(dssdev);
996 int r;
997
998 mutex_lock(&ddata->lock);
999 r = ddata->te_enabled;
1000 mutex_unlock(&ddata->lock);
1001
1002 return r;
1003}
1004
1005static int dsicm_memory_read(struct omap_dss_device *dssdev,
1006 void *buf, size_t size,
1007 u16 x, u16 y, u16 w, u16 h)
1008{
1009 struct panel_drv_data *ddata = to_panel_data(dssdev);
1010 struct omap_dss_device *in = ddata->in;
1011 int r;
1012 int first = 1;
1013 int plen;
1014 unsigned buf_used = 0;
1015
1016 if (size < w * h * 3)
1017 return -ENOMEM;
1018
1019 mutex_lock(&ddata->lock);
1020
1021 if (!ddata->enabled) {
1022 r = -ENODEV;
1023 goto err1;
1024 }
1025
7aa91e76 1026 size = min((u32)w * h * 3,
da11bbbb 1027 dssdev->panel.vm.hactive * dssdev->panel.vm.vactive * 3);
dbc23840
TV
1028
1029 in->ops.dsi->bus_lock(in);
1030
1031 r = dsicm_wake_up(ddata);
1032 if (r)
1033 goto err2;
1034
1035 /* plen 1 or 2 goes into short packet. until checksum error is fixed,
1036 * use short packets. plen 32 works, but bigger packets seem to cause
1037 * an error. */
1038 if (size % 2)
1039 plen = 1;
1040 else
1041 plen = 2;
1042
1043 dsicm_set_update_window(ddata, x, y, w, h);
1044
1045 r = in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, plen);
1046 if (r)
1047 goto err2;
1048
1049 while (buf_used < size) {
1050 u8 dcs_cmd = first ? 0x2e : 0x3e;
1051 first = 0;
1052
1053 r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd,
1054 buf + buf_used, size - buf_used);
1055
1056 if (r < 0) {
1057 dev_err(dssdev->dev, "read error\n");
1058 goto err3;
1059 }
1060
1061 buf_used += r;
1062
1063 if (r < plen) {
1064 dev_err(&ddata->pdev->dev, "short read\n");
1065 break;
1066 }
1067
1068 if (signal_pending(current)) {
1069 dev_err(&ddata->pdev->dev, "signal pending, "
1070 "aborting memory read\n");
1071 r = -ERESTARTSYS;
1072 goto err3;
1073 }
1074 }
1075
1076 r = buf_used;
1077
1078err3:
1079 in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, 1);
1080err2:
1081 in->ops.dsi->bus_unlock(in);
1082err1:
1083 mutex_unlock(&ddata->lock);
1084 return r;
1085}
1086
1087static void dsicm_ulps_work(struct work_struct *work)
1088{
1089 struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
1090 ulps_work.work);
1091 struct omap_dss_device *dssdev = &ddata->dssdev;
1092 struct omap_dss_device *in = ddata->in;
1093
1094 mutex_lock(&ddata->lock);
1095
1096 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) {
1097 mutex_unlock(&ddata->lock);
1098 return;
1099 }
1100
1101 in->ops.dsi->bus_lock(in);
1102
1103 dsicm_enter_ulps(ddata);
1104
1105 in->ops.dsi->bus_unlock(in);
1106 mutex_unlock(&ddata->lock);
1107}
1108
1109static struct omap_dss_driver dsicm_ops = {
1110 .connect = dsicm_connect,
1111 .disconnect = dsicm_disconnect,
1112
1113 .enable = dsicm_enable,
1114 .disable = dsicm_disable,
1115
1116 .update = dsicm_update,
1117 .sync = dsicm_sync,
1118
1119 .get_resolution = dsicm_get_resolution,
1120 .get_recommended_bpp = omapdss_default_get_recommended_bpp,
1121
1122 .enable_te = dsicm_enable_te,
1123 .get_te = dsicm_get_te,
1124
1125 .memory_read = dsicm_memory_read,
1126};
1127
cdeeaca2
TV
1128static int dsicm_probe_of(struct platform_device *pdev)
1129{
1130 struct device_node *node = pdev->dev.of_node;
1131 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1132 struct omap_dss_device *in;
1133 int gpio;
1134
1135 gpio = of_get_named_gpio(node, "reset-gpios", 0);
1136 if (!gpio_is_valid(gpio)) {
1137 dev_err(&pdev->dev, "failed to parse reset gpio\n");
1138 return gpio;
1139 }
1140 ddata->reset_gpio = gpio;
1141
1142 gpio = of_get_named_gpio(node, "te-gpios", 0);
1143 if (gpio_is_valid(gpio) || gpio == -ENOENT) {
1144 ddata->ext_te_gpio = gpio;
1145 } else {
1146 dev_err(&pdev->dev, "failed to parse TE gpio\n");
1147 return gpio;
1148 }
1149
1150 in = omapdss_of_find_source_for_first_ep(node);
1151 if (IS_ERR(in)) {
1152 dev_err(&pdev->dev, "failed to find video source\n");
1153 return PTR_ERR(in);
1154 }
1155
1156 ddata->in = in;
1157
1158 /* TODO: ulps, backlight */
1159
1160 return 0;
1161}
1162
dbc23840
TV
1163static int dsicm_probe(struct platform_device *pdev)
1164{
1165 struct backlight_properties props;
1166 struct panel_drv_data *ddata;
1167 struct backlight_device *bldev = NULL;
1168 struct device *dev = &pdev->dev;
1169 struct omap_dss_device *dssdev;
1170 int r;
1171
1172 dev_dbg(dev, "probe\n");
1173
1174 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
1175 if (!ddata)
1176 return -ENOMEM;
1177
1178 platform_set_drvdata(pdev, ddata);
1179 ddata->pdev = pdev;
1180
55f6fca3 1181 if (!pdev->dev.of_node)
dbc23840 1182 return -ENODEV;
3d1f0f59
TV
1183
1184 r = dsicm_probe_of(pdev);
1185 if (r)
1186 return r;
dbc23840 1187
da11bbbb
PU
1188 ddata->vm.hactive = 864;
1189 ddata->vm.vactive = 480;
1190 ddata->vm.pixelclock = 864 * 480 * 60;
dbc23840
TV
1191
1192 dssdev = &ddata->dssdev;
1193 dssdev->dev = dev;
1194 dssdev->driver = &dsicm_ops;
da11bbbb 1195 dssdev->panel.vm = ddata->vm;
dbc23840
TV
1196 dssdev->type = OMAP_DISPLAY_TYPE_DSI;
1197 dssdev->owner = THIS_MODULE;
1198
1199 dssdev->panel.dsi_pix_fmt = OMAP_DSS_DSI_FMT_RGB888;
1200 dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
1201 OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
1202
1203 r = omapdss_register_display(dssdev);
1204 if (r) {
1205 dev_err(dev, "Failed to register panel\n");
1206 goto err_reg;
1207 }
1208
1209 mutex_init(&ddata->lock);
1210
1211 atomic_set(&ddata->do_update, 0);
1212
1213 if (gpio_is_valid(ddata->reset_gpio)) {
1214 r = devm_gpio_request_one(dev, ddata->reset_gpio,
1215 GPIOF_OUT_INIT_LOW, "taal rst");
1216 if (r) {
1217 dev_err(dev, "failed to request reset gpio\n");
1218 return r;
1219 }
1220 }
1221
1222 if (gpio_is_valid(ddata->ext_te_gpio)) {
1223 r = devm_gpio_request_one(dev, ddata->ext_te_gpio,
1224 GPIOF_IN, "taal irq");
1225 if (r) {
1226 dev_err(dev, "GPIO request failed\n");
1227 return r;
1228 }
1229
1230 r = devm_request_irq(dev, gpio_to_irq(ddata->ext_te_gpio),
1231 dsicm_te_isr,
1232 IRQF_TRIGGER_RISING,
1233 "taal vsync", ddata);
1234
1235 if (r) {
1236 dev_err(dev, "IRQ request failed\n");
1237 return r;
1238 }
1239
1240 INIT_DEFERRABLE_WORK(&ddata->te_timeout_work,
1241 dsicm_te_timeout_work_callback);
1242
1243 dev_dbg(dev, "Using GPIO TE\n");
1244 }
1245
1246 ddata->workqueue = create_singlethread_workqueue("dsicm_wq");
1247 if (ddata->workqueue == NULL) {
1248 dev_err(dev, "can't create workqueue\n");
1249 return -ENOMEM;
1250 }
1251 INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work);
1252
1253 dsicm_hw_reset(ddata);
1254
1255 if (ddata->use_dsi_backlight) {
1256 memset(&props, 0, sizeof(struct backlight_properties));
1257 props.max_brightness = 255;
1258
1259 props.type = BACKLIGHT_RAW;
1260 bldev = backlight_device_register(dev_name(dev),
1261 dev, ddata, &dsicm_bl_ops, &props);
1262 if (IS_ERR(bldev)) {
1263 r = PTR_ERR(bldev);
1264 goto err_bl;
1265 }
1266
1267 ddata->bldev = bldev;
1268
1269 bldev->props.fb_blank = FB_BLANK_UNBLANK;
1270 bldev->props.power = FB_BLANK_UNBLANK;
1271 bldev->props.brightness = 255;
1272
1273 dsicm_bl_update_status(bldev);
1274 }
1275
1276 r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
1277 if (r) {
1278 dev_err(dev, "failed to create sysfs files\n");
1279 goto err_sysfs_create;
1280 }
1281
1282 return 0;
1283
1284err_sysfs_create:
76e4c327 1285 backlight_device_unregister(bldev);
dbc23840
TV
1286err_bl:
1287 destroy_workqueue(ddata->workqueue);
1288err_reg:
1289 return r;
1290}
1291
1292static int __exit dsicm_remove(struct platform_device *pdev)
1293{
1294 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1295 struct omap_dss_device *dssdev = &ddata->dssdev;
1296 struct backlight_device *bldev;
1297
1298 dev_dbg(&pdev->dev, "remove\n");
1299
1300 omapdss_unregister_display(dssdev);
1301
1302 dsicm_disable(dssdev);
1303 dsicm_disconnect(dssdev);
1304
1305 sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group);
1306
1307 bldev = ddata->bldev;
1308 if (bldev != NULL) {
1309 bldev->props.power = FB_BLANK_POWERDOWN;
1310 dsicm_bl_update_status(bldev);
1311 backlight_device_unregister(bldev);
1312 }
1313
1314 omap_dss_put_device(ddata->in);
1315
1316 dsicm_cancel_ulps_work(ddata);
1317 destroy_workqueue(ddata->workqueue);
1318
1319 /* reset, to be sure that the panel is in a valid state */
1320 dsicm_hw_reset(ddata);
1321
1322 return 0;
1323}
1324
cdeeaca2
TV
1325static const struct of_device_id dsicm_of_match[] = {
1326 { .compatible = "omapdss,panel-dsi-cm", },
1327 {},
1328};
1329
1330MODULE_DEVICE_TABLE(of, dsicm_of_match);
1331
dbc23840
TV
1332static struct platform_driver dsicm_driver = {
1333 .probe = dsicm_probe,
1334 .remove = __exit_p(dsicm_remove),
1335 .driver = {
1336 .name = "panel-dsi-cm",
cdeeaca2 1337 .of_match_table = dsicm_of_match,
422ccbd5 1338 .suppress_bind_attrs = true,
dbc23840
TV
1339 },
1340};
1341
1342module_platform_driver(dsicm_driver);
1343
1344MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
1345MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
1346MODULE_LICENSE("GPL");