blob: 11d69b67a903a7d53ddc97dedec5ca0fb9e701d5 [file] [log] [blame]
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +02001/*
2 * Taal DSI command mode panel
3 *
4 * Copyright (C) 2009 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.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 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*#define DEBUG*/
21
22#include <linux/module.h>
23#include <linux/delay.h>
24#include <linux/err.h>
25#include <linux/jiffies.h>
26#include <linux/sched.h>
27#include <linux/backlight.h>
28#include <linux/fb.h>
29#include <linux/interrupt.h>
30#include <linux/gpio.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33
34#include <plat/display.h>
35
36/* DSI Virtual channel. Hardcoded for now. */
37#define TCH 0
38
39#define DCS_READ_NUM_ERRORS 0x05
40#define DCS_READ_POWER_MODE 0x0a
41#define DCS_READ_MADCTL 0x0b
42#define DCS_READ_PIXEL_FORMAT 0x0c
43#define DCS_RDDSDR 0x0f
44#define DCS_SLEEP_IN 0x10
45#define DCS_SLEEP_OUT 0x11
46#define DCS_DISPLAY_OFF 0x28
47#define DCS_DISPLAY_ON 0x29
48#define DCS_COLUMN_ADDR 0x2a
49#define DCS_PAGE_ADDR 0x2b
50#define DCS_MEMORY_WRITE 0x2c
51#define DCS_TEAR_OFF 0x34
52#define DCS_TEAR_ON 0x35
53#define DCS_MEM_ACC_CTRL 0x36
54#define DCS_PIXEL_FORMAT 0x3a
55#define DCS_BRIGHTNESS 0x51
56#define DCS_CTRL_DISPLAY 0x53
57#define DCS_WRITE_CABC 0x55
58#define DCS_READ_CABC 0x56
59#define DCS_GET_ID1 0xda
60#define DCS_GET_ID2 0xdb
61#define DCS_GET_ID3 0xdc
62
63/* #define TAAL_USE_ESD_CHECK */
64#define TAAL_ESD_CHECK_PERIOD msecs_to_jiffies(5000)
65
66struct taal_data {
67 struct backlight_device *bldev;
68
69 unsigned long hw_guard_end; /* next value of jiffies when we can
70 * issue the next sleep in/out command
71 */
72 unsigned long hw_guard_wait; /* max guard time in jiffies */
73
74 struct omap_dss_device *dssdev;
75
76 bool enabled;
77 u8 rotate;
78 bool mirror;
79
80 bool te_enabled;
81 bool use_ext_te;
82 struct completion te_completion;
83
84 bool use_dsi_bl;
85
86 bool cabc_broken;
87 unsigned cabc_mode;
88
89 bool intro_printed;
90
91 struct workqueue_struct *esd_wq;
92 struct delayed_work esd_work;
93};
94
95static void taal_esd_work(struct work_struct *work);
96
97static void hw_guard_start(struct taal_data *td, int guard_msec)
98{
99 td->hw_guard_wait = msecs_to_jiffies(guard_msec);
100 td->hw_guard_end = jiffies + td->hw_guard_wait;
101}
102
103static void hw_guard_wait(struct taal_data *td)
104{
105 unsigned long wait = td->hw_guard_end - jiffies;
106
107 if ((long)wait > 0 && wait <= td->hw_guard_wait) {
108 set_current_state(TASK_UNINTERRUPTIBLE);
109 schedule_timeout(wait);
110 }
111}
112
113static int taal_dcs_read_1(u8 dcs_cmd, u8 *data)
114{
115 int r;
116 u8 buf[1];
117
118 r = dsi_vc_dcs_read(TCH, dcs_cmd, buf, 1);
119
120 if (r < 0)
121 return r;
122
123 *data = buf[0];
124
125 return 0;
126}
127
128static int taal_dcs_write_0(u8 dcs_cmd)
129{
130 return dsi_vc_dcs_write(TCH, &dcs_cmd, 1);
131}
132
133static int taal_dcs_write_1(u8 dcs_cmd, u8 param)
134{
135 u8 buf[2];
136 buf[0] = dcs_cmd;
137 buf[1] = param;
138 return dsi_vc_dcs_write(TCH, buf, 2);
139}
140
141static int taal_sleep_in(struct taal_data *td)
142
143{
144 u8 cmd;
145 int r;
146
147 hw_guard_wait(td);
148
149 cmd = DCS_SLEEP_IN;
150 r = dsi_vc_dcs_write_nosync(TCH, &cmd, 1);
151 if (r)
152 return r;
153
154 hw_guard_start(td, 120);
155
156 msleep(5);
157
158 return 0;
159}
160
161static int taal_sleep_out(struct taal_data *td)
162{
163 int r;
164
165 hw_guard_wait(td);
166
167 r = taal_dcs_write_0(DCS_SLEEP_OUT);
168 if (r)
169 return r;
170
171 hw_guard_start(td, 120);
172
173 msleep(5);
174
175 return 0;
176}
177
178static int taal_get_id(u8 *id1, u8 *id2, u8 *id3)
179{
180 int r;
181
182 r = taal_dcs_read_1(DCS_GET_ID1, id1);
183 if (r)
184 return r;
185 r = taal_dcs_read_1(DCS_GET_ID2, id2);
186 if (r)
187 return r;
188 r = taal_dcs_read_1(DCS_GET_ID3, id3);
189 if (r)
190 return r;
191
192 return 0;
193}
194
195static int taal_set_addr_mode(u8 rotate, bool mirror)
196{
197 int r;
198 u8 mode;
199 int b5, b6, b7;
200
201 r = taal_dcs_read_1(DCS_READ_MADCTL, &mode);
202 if (r)
203 return r;
204
205 switch (rotate) {
206 default:
207 case 0:
208 b7 = 0;
209 b6 = 0;
210 b5 = 0;
211 break;
212 case 1:
213 b7 = 0;
214 b6 = 1;
215 b5 = 1;
216 break;
217 case 2:
218 b7 = 1;
219 b6 = 1;
220 b5 = 0;
221 break;
222 case 3:
223 b7 = 1;
224 b6 = 0;
225 b5 = 1;
226 break;
227 }
228
229 if (mirror)
230 b6 = !b6;
231
232 mode &= ~((1<<7) | (1<<6) | (1<<5));
233 mode |= (b7 << 7) | (b6 << 6) | (b5 << 5);
234
235 return taal_dcs_write_1(DCS_MEM_ACC_CTRL, mode);
236}
237
238static int taal_set_update_window(u16 x, u16 y, u16 w, u16 h)
239{
240 int r;
241 u16 x1 = x;
242 u16 x2 = x + w - 1;
243 u16 y1 = y;
244 u16 y2 = y + h - 1;
245
246 u8 buf[5];
247 buf[0] = DCS_COLUMN_ADDR;
248 buf[1] = (x1 >> 8) & 0xff;
249 buf[2] = (x1 >> 0) & 0xff;
250 buf[3] = (x2 >> 8) & 0xff;
251 buf[4] = (x2 >> 0) & 0xff;
252
253 r = dsi_vc_dcs_write_nosync(TCH, buf, sizeof(buf));
254 if (r)
255 return r;
256
257 buf[0] = DCS_PAGE_ADDR;
258 buf[1] = (y1 >> 8) & 0xff;
259 buf[2] = (y1 >> 0) & 0xff;
260 buf[3] = (y2 >> 8) & 0xff;
261 buf[4] = (y2 >> 0) & 0xff;
262
263 r = dsi_vc_dcs_write_nosync(TCH, buf, sizeof(buf));
264 if (r)
265 return r;
266
267 dsi_vc_send_bta_sync(TCH);
268
269 return r;
270}
271
272static int taal_bl_update_status(struct backlight_device *dev)
273{
274 struct omap_dss_device *dssdev = dev_get_drvdata(&dev->dev);
275 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
276 int r;
277 int level;
278
279 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
280 dev->props.power == FB_BLANK_UNBLANK)
281 level = dev->props.brightness;
282 else
283 level = 0;
284
285 dev_dbg(&dssdev->dev, "update brightness to %d\n", level);
286
287 if (td->use_dsi_bl) {
288 if (td->enabled) {
289 dsi_bus_lock();
290 r = taal_dcs_write_1(DCS_BRIGHTNESS, level);
291 dsi_bus_unlock();
292 if (r)
293 return r;
294 }
295 } else {
296 if (!dssdev->set_backlight)
297 return -EINVAL;
298
299 r = dssdev->set_backlight(dssdev, level);
300 if (r)
301 return r;
302 }
303
304 return 0;
305}
306
307static int taal_bl_get_intensity(struct backlight_device *dev)
308{
309 if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
310 dev->props.power == FB_BLANK_UNBLANK)
311 return dev->props.brightness;
312
313 return 0;
314}
315
316static struct backlight_ops taal_bl_ops = {
317 .get_brightness = taal_bl_get_intensity,
318 .update_status = taal_bl_update_status,
319};
320
321static void taal_get_timings(struct omap_dss_device *dssdev,
322 struct omap_video_timings *timings)
323{
324 *timings = dssdev->panel.timings;
325}
326
327static void taal_get_resolution(struct omap_dss_device *dssdev,
328 u16 *xres, u16 *yres)
329{
330 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
331
332 if (td->rotate == 0 || td->rotate == 2) {
333 *xres = dssdev->panel.timings.x_res;
334 *yres = dssdev->panel.timings.y_res;
335 } else {
336 *yres = dssdev->panel.timings.x_res;
337 *xres = dssdev->panel.timings.y_res;
338 }
339}
340
341static irqreturn_t taal_te_isr(int irq, void *data)
342{
343 struct omap_dss_device *dssdev = data;
344 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
345
346 complete_all(&td->te_completion);
347
348 return IRQ_HANDLED;
349}
350
351static ssize_t taal_num_errors_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
353{
354 struct omap_dss_device *dssdev = to_dss_device(dev);
355 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
356 u8 errors;
357 int r;
358
359 if (td->enabled) {
360 dsi_bus_lock();
361 r = taal_dcs_read_1(DCS_READ_NUM_ERRORS, &errors);
362 dsi_bus_unlock();
363 } else {
364 r = -ENODEV;
365 }
366
367 if (r)
368 return r;
369
370 return snprintf(buf, PAGE_SIZE, "%d\n", errors);
371}
372
373static ssize_t taal_hw_revision_show(struct device *dev,
374 struct device_attribute *attr, char *buf)
375{
376 struct omap_dss_device *dssdev = to_dss_device(dev);
377 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
378 u8 id1, id2, id3;
379 int r;
380
381 if (td->enabled) {
382 dsi_bus_lock();
383 r = taal_get_id(&id1, &id2, &id3);
384 dsi_bus_unlock();
385 } else {
386 r = -ENODEV;
387 }
388
389 if (r)
390 return r;
391
392 return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
393}
394
395static const char *cabc_modes[] = {
396 "off", /* used also always when CABC is not supported */
397 "ui",
398 "still-image",
399 "moving-image",
400};
401
402static ssize_t show_cabc_mode(struct device *dev,
403 struct device_attribute *attr,
404 char *buf)
405{
406 struct omap_dss_device *dssdev = to_dss_device(dev);
407 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
408 const char *mode_str;
409 int mode;
410 int len;
411
412 mode = td->cabc_mode;
413
414 mode_str = "unknown";
415 if (mode >= 0 && mode < ARRAY_SIZE(cabc_modes))
416 mode_str = cabc_modes[mode];
417 len = snprintf(buf, PAGE_SIZE, "%s\n", mode_str);
418
419 return len < PAGE_SIZE - 1 ? len : PAGE_SIZE - 1;
420}
421
422static ssize_t store_cabc_mode(struct device *dev,
423 struct device_attribute *attr,
424 const char *buf, size_t count)
425{
426 struct omap_dss_device *dssdev = to_dss_device(dev);
427 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
428 int i;
429
430 for (i = 0; i < ARRAY_SIZE(cabc_modes); i++) {
431 if (sysfs_streq(cabc_modes[i], buf))
432 break;
433 }
434
435 if (i == ARRAY_SIZE(cabc_modes))
436 return -EINVAL;
437
438 if (td->enabled) {
439 dsi_bus_lock();
440 if (!td->cabc_broken)
441 taal_dcs_write_1(DCS_WRITE_CABC, i);
442 dsi_bus_unlock();
443 }
444
445 td->cabc_mode = i;
446
447 return count;
448}
449
450static ssize_t show_cabc_available_modes(struct device *dev,
451 struct device_attribute *attr,
452 char *buf)
453{
454 int len;
455 int i;
456
457 for (i = 0, len = 0;
458 len < PAGE_SIZE && i < ARRAY_SIZE(cabc_modes); i++)
459 len += snprintf(&buf[len], PAGE_SIZE - len, "%s%s%s",
460 i ? " " : "", cabc_modes[i],
461 i == ARRAY_SIZE(cabc_modes) - 1 ? "\n" : "");
462
463 return len < PAGE_SIZE ? len : PAGE_SIZE - 1;
464}
465
466static DEVICE_ATTR(num_dsi_errors, S_IRUGO, taal_num_errors_show, NULL);
467static DEVICE_ATTR(hw_revision, S_IRUGO, taal_hw_revision_show, NULL);
468static DEVICE_ATTR(cabc_mode, S_IRUGO | S_IWUSR,
469 show_cabc_mode, store_cabc_mode);
470static DEVICE_ATTR(cabc_available_modes, S_IRUGO,
471 show_cabc_available_modes, NULL);
472
473static struct attribute *taal_attrs[] = {
474 &dev_attr_num_dsi_errors.attr,
475 &dev_attr_hw_revision.attr,
476 &dev_attr_cabc_mode.attr,
477 &dev_attr_cabc_available_modes.attr,
478 NULL,
479};
480
481static struct attribute_group taal_attr_group = {
482 .attrs = taal_attrs,
483};
484
485static int taal_probe(struct omap_dss_device *dssdev)
486{
487 struct taal_data *td;
488 struct backlight_device *bldev;
489 int r;
490
491 const struct omap_video_timings taal_panel_timings = {
492 .x_res = 864,
493 .y_res = 480,
494 };
495
496 dev_dbg(&dssdev->dev, "probe\n");
497
498 dssdev->panel.config = OMAP_DSS_LCD_TFT;
499 dssdev->panel.timings = taal_panel_timings;
500 dssdev->ctrl.pixel_size = 24;
501
502 td = kzalloc(sizeof(*td), GFP_KERNEL);
503 if (!td) {
504 r = -ENOMEM;
505 goto err0;
506 }
507 td->dssdev = dssdev;
508
509 td->esd_wq = create_singlethread_workqueue("taal_esd");
510 if (td->esd_wq == NULL) {
511 dev_err(&dssdev->dev, "can't create ESD workqueue\n");
512 r = -ENOMEM;
Aaro Koskinen92fe0ff2009-12-09 17:26:25 +0100513 goto err1;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200514 }
515 INIT_DELAYED_WORK_DEFERRABLE(&td->esd_work, taal_esd_work);
516
517 dev_set_drvdata(&dssdev->dev, td);
518
519 dssdev->get_timings = taal_get_timings;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200520
521 /* if no platform set_backlight() defined, presume DSI backlight
522 * control */
523 if (!dssdev->set_backlight)
524 td->use_dsi_bl = true;
525
526 bldev = backlight_device_register("taal", &dssdev->dev, dssdev,
527 &taal_bl_ops);
528 if (IS_ERR(bldev)) {
529 r = PTR_ERR(bldev);
Aaro Koskinen92fe0ff2009-12-09 17:26:25 +0100530 goto err2;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200531 }
532
533 td->bldev = bldev;
534
535 bldev->props.fb_blank = FB_BLANK_UNBLANK;
536 bldev->props.power = FB_BLANK_UNBLANK;
537 if (td->use_dsi_bl) {
538 bldev->props.max_brightness = 255;
539 bldev->props.brightness = 255;
540 } else {
541 bldev->props.max_brightness = 127;
542 bldev->props.brightness = 127;
543 }
544
545 taal_bl_update_status(bldev);
546
547 if (dssdev->phy.dsi.ext_te) {
548 int gpio = dssdev->phy.dsi.ext_te_gpio;
549
550 r = gpio_request(gpio, "taal irq");
551 if (r) {
552 dev_err(&dssdev->dev, "GPIO request failed\n");
553 goto err3;
554 }
555
556 gpio_direction_input(gpio);
557
558 r = request_irq(gpio_to_irq(gpio), taal_te_isr,
559 IRQF_DISABLED | IRQF_TRIGGER_RISING,
560 "taal vsync", dssdev);
561
562 if (r) {
563 dev_err(&dssdev->dev, "IRQ request failed\n");
564 gpio_free(gpio);
565 goto err3;
566 }
567
568 init_completion(&td->te_completion);
569
570 td->use_ext_te = true;
571 }
572
573 r = sysfs_create_group(&dssdev->dev.kobj, &taal_attr_group);
574 if (r) {
575 dev_err(&dssdev->dev, "failed to create sysfs files\n");
576 goto err4;
577 }
578
579 return 0;
580err4:
581 if (td->use_ext_te) {
582 int gpio = dssdev->phy.dsi.ext_te_gpio;
583 free_irq(gpio_to_irq(gpio), dssdev);
584 gpio_free(gpio);
585 }
586err3:
587 backlight_device_unregister(bldev);
588err2:
589 cancel_delayed_work_sync(&td->esd_work);
590 destroy_workqueue(td->esd_wq);
591err1:
592 kfree(td);
593err0:
594 return r;
595}
596
597static void taal_remove(struct omap_dss_device *dssdev)
598{
599 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
600 struct backlight_device *bldev;
601
602 dev_dbg(&dssdev->dev, "remove\n");
603
604 sysfs_remove_group(&dssdev->dev.kobj, &taal_attr_group);
605
606 if (td->use_ext_te) {
607 int gpio = dssdev->phy.dsi.ext_te_gpio;
608 free_irq(gpio_to_irq(gpio), dssdev);
609 gpio_free(gpio);
610 }
611
612 bldev = td->bldev;
613 bldev->props.power = FB_BLANK_POWERDOWN;
614 taal_bl_update_status(bldev);
615 backlight_device_unregister(bldev);
616
617 cancel_delayed_work_sync(&td->esd_work);
618 destroy_workqueue(td->esd_wq);
619
620 kfree(td);
621}
622
623static int taal_enable(struct omap_dss_device *dssdev)
624{
625 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
626 u8 id1, id2, id3;
627 int r;
628
629 dev_dbg(&dssdev->dev, "enable\n");
630
631 if (dssdev->platform_enable) {
632 r = dssdev->platform_enable(dssdev);
633 if (r)
634 return r;
635 }
636
637 /* it seems we have to wait a bit until taal is ready */
638 msleep(5);
639
640 r = taal_sleep_out(td);
641 if (r)
642 goto err;
643
644 r = taal_get_id(&id1, &id2, &id3);
645 if (r)
646 goto err;
647
648 /* on early revisions CABC is broken */
649 if (id2 == 0x00 || id2 == 0xff || id2 == 0x81)
650 td->cabc_broken = true;
651
652 taal_dcs_write_1(DCS_BRIGHTNESS, 0xff);
653 taal_dcs_write_1(DCS_CTRL_DISPLAY, (1<<2) | (1<<5)); /* BL | BCTRL */
654
655 taal_dcs_write_1(DCS_PIXEL_FORMAT, 0x7); /* 24bit/pixel */
656
657 taal_set_addr_mode(td->rotate, td->mirror);
658 if (!td->cabc_broken)
659 taal_dcs_write_1(DCS_WRITE_CABC, td->cabc_mode);
660
661 taal_dcs_write_0(DCS_DISPLAY_ON);
662
663#ifdef TAAL_USE_ESD_CHECK
664 queue_delayed_work(td->esd_wq, &td->esd_work, TAAL_ESD_CHECK_PERIOD);
665#endif
666
667 td->enabled = 1;
668
669 if (!td->intro_printed) {
670 dev_info(&dssdev->dev, "revision %02x.%02x.%02x\n",
671 id1, id2, id3);
672 if (td->cabc_broken)
673 dev_info(&dssdev->dev,
674 "old Taal version, CABC disabled\n");
675 td->intro_printed = true;
676 }
677
678 return 0;
679err:
680 if (dssdev->platform_disable)
681 dssdev->platform_disable(dssdev);
682
683 return r;
684}
685
686static void taal_disable(struct omap_dss_device *dssdev)
687{
688 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
689
690 dev_dbg(&dssdev->dev, "disable\n");
691
692 cancel_delayed_work(&td->esd_work);
693
694 taal_dcs_write_0(DCS_DISPLAY_OFF);
695 taal_sleep_in(td);
696
697 /* wait a bit so that the message goes through */
698 msleep(10);
699
700 if (dssdev->platform_disable)
701 dssdev->platform_disable(dssdev);
702
703 td->enabled = 0;
704}
705
706static int taal_suspend(struct omap_dss_device *dssdev)
707{
708 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
709 struct backlight_device *bldev = td->bldev;
710
711 bldev->props.power = FB_BLANK_POWERDOWN;
712 taal_bl_update_status(bldev);
713
714 return 0;
715}
716
717static int taal_resume(struct omap_dss_device *dssdev)
718{
719 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
720 struct backlight_device *bldev = td->bldev;
721
722 bldev->props.power = FB_BLANK_UNBLANK;
723 taal_bl_update_status(bldev);
724
725 return 0;
726}
727
728static void taal_setup_update(struct omap_dss_device *dssdev,
729 u16 x, u16 y, u16 w, u16 h)
730{
731 taal_set_update_window(x, y, w, h);
732}
733
734static int taal_enable_te(struct omap_dss_device *dssdev, bool enable)
735{
736 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
737 int r;
738
739 td->te_enabled = enable;
740
741 if (enable)
742 r = taal_dcs_write_1(DCS_TEAR_ON, 0);
743 else
744 r = taal_dcs_write_0(DCS_TEAR_OFF);
745
746 return r;
747}
748
749static int taal_wait_te(struct omap_dss_device *dssdev)
750{
751 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
752 long wait = msecs_to_jiffies(500);
753
754 if (!td->use_ext_te || !td->te_enabled)
755 return 0;
756
757 INIT_COMPLETION(td->te_completion);
758 wait = wait_for_completion_timeout(&td->te_completion, wait);
759 if (wait == 0) {
760 dev_err(&dssdev->dev, "timeout waiting TE\n");
761 return -ETIME;
762 }
763
764 return 0;
765}
766
767static int taal_rotate(struct omap_dss_device *dssdev, u8 rotate)
768{
769 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
770 int r;
771
772 dev_dbg(&dssdev->dev, "rotate %d\n", rotate);
773
Tomi Valkeinen87424e12010-01-08 16:52:48 +0200774 dsi_bus_lock();
775
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200776 if (td->enabled) {
777 r = taal_set_addr_mode(rotate, td->mirror);
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200778 if (r)
Tomi Valkeinen87424e12010-01-08 16:52:48 +0200779 goto err;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200780 }
781
782 td->rotate = rotate;
783
Tomi Valkeinen87424e12010-01-08 16:52:48 +0200784 dsi_bus_unlock();
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200785 return 0;
Tomi Valkeinen87424e12010-01-08 16:52:48 +0200786err:
787 dsi_bus_unlock();
788 return r;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200789}
790
791static u8 taal_get_rotate(struct omap_dss_device *dssdev)
792{
793 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
794 return td->rotate;
795}
796
797static int taal_mirror(struct omap_dss_device *dssdev, bool enable)
798{
799 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
800 int r;
801
802 dev_dbg(&dssdev->dev, "mirror %d\n", enable);
803
Tomi Valkeinen8d8aa612010-01-08 16:30:33 +0200804 dsi_bus_lock();
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200805 if (td->enabled) {
806 r = taal_set_addr_mode(td->rotate, enable);
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200807 if (r)
Tomi Valkeinen8d8aa612010-01-08 16:30:33 +0200808 goto err;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200809 }
810
811 td->mirror = enable;
812
Tomi Valkeinen8d8aa612010-01-08 16:30:33 +0200813 dsi_bus_unlock();
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200814 return 0;
Tomi Valkeinen8d8aa612010-01-08 16:30:33 +0200815err:
816 dsi_bus_unlock();
817 return r;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200818}
819
820static bool taal_get_mirror(struct omap_dss_device *dssdev)
821{
822 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
823 return td->mirror;
824}
825
826static int taal_run_test(struct omap_dss_device *dssdev, int test_num)
827{
828 u8 id1, id2, id3;
829 int r;
830
Tomi Valkeinen1a75ef42010-01-08 16:21:28 +0200831 dsi_bus_lock();
832
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200833 r = taal_dcs_read_1(DCS_GET_ID1, &id1);
834 if (r)
Tomi Valkeinen1a75ef42010-01-08 16:21:28 +0200835 goto err;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200836 r = taal_dcs_read_1(DCS_GET_ID2, &id2);
837 if (r)
Tomi Valkeinen1a75ef42010-01-08 16:21:28 +0200838 goto err;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200839 r = taal_dcs_read_1(DCS_GET_ID3, &id3);
840 if (r)
Tomi Valkeinen1a75ef42010-01-08 16:21:28 +0200841 goto err;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200842
Tomi Valkeinen1a75ef42010-01-08 16:21:28 +0200843 dsi_bus_unlock();
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200844 return 0;
Tomi Valkeinen1a75ef42010-01-08 16:21:28 +0200845err:
846 dsi_bus_unlock();
847 return r;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200848}
849
850static int taal_memory_read(struct omap_dss_device *dssdev,
851 void *buf, size_t size,
852 u16 x, u16 y, u16 w, u16 h)
853{
854 int r;
855 int first = 1;
856 int plen;
857 unsigned buf_used = 0;
Tomi Valkeinenc75d9462010-01-08 16:56:44 +0200858 struct taal_data *td = dev_get_drvdata(&dssdev->dev);
859
860 if (!td->enabled)
861 return -ENODEV;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200862
863 if (size < w * h * 3)
864 return -ENOMEM;
865
866 size = min(w * h * 3,
867 dssdev->panel.timings.x_res *
868 dssdev->panel.timings.y_res * 3);
869
Tomi Valkeinenc75d9462010-01-08 16:56:44 +0200870 dsi_bus_lock();
871
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200872 /* plen 1 or 2 goes into short packet. until checksum error is fixed,
873 * use short packets. plen 32 works, but bigger packets seem to cause
874 * an error. */
875 if (size % 2)
876 plen = 1;
877 else
878 plen = 2;
879
Tomi Valkeinenc75d9462010-01-08 16:56:44 +0200880 taal_set_update_window(x, y, w, h);
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200881
882 r = dsi_vc_set_max_rx_packet_size(TCH, plen);
883 if (r)
Tomi Valkeinenc75d9462010-01-08 16:56:44 +0200884 goto err0;
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200885
886 while (buf_used < size) {
887 u8 dcs_cmd = first ? 0x2e : 0x3e;
888 first = 0;
889
890 r = dsi_vc_dcs_read(TCH, dcs_cmd,
891 buf + buf_used, size - buf_used);
892
893 if (r < 0) {
894 dev_err(&dssdev->dev, "read error\n");
895 goto err;
896 }
897
898 buf_used += r;
899
900 if (r < plen) {
901 dev_err(&dssdev->dev, "short read\n");
902 break;
903 }
904
905 if (signal_pending(current)) {
906 dev_err(&dssdev->dev, "signal pending, "
907 "aborting memory read\n");
908 r = -ERESTARTSYS;
909 goto err;
910 }
911 }
912
913 r = buf_used;
914
915err:
916 dsi_vc_set_max_rx_packet_size(TCH, 1);
Tomi Valkeinenc75d9462010-01-08 16:56:44 +0200917err0:
918 dsi_bus_unlock();
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200919 return r;
920}
921
922static void taal_esd_work(struct work_struct *work)
923{
924 struct taal_data *td = container_of(work, struct taal_data,
925 esd_work.work);
926 struct omap_dss_device *dssdev = td->dssdev;
927 u8 state1, state2;
928 int r;
929
930 if (!td->enabled)
931 return;
932
933 dsi_bus_lock();
934
935 r = taal_dcs_read_1(DCS_RDDSDR, &state1);
936 if (r) {
937 dev_err(&dssdev->dev, "failed to read Taal status\n");
938 goto err;
939 }
940
941 /* Run self diagnostics */
942 r = taal_sleep_out(td);
943 if (r) {
944 dev_err(&dssdev->dev, "failed to run Taal self-diagnostics\n");
945 goto err;
946 }
947
948 r = taal_dcs_read_1(DCS_RDDSDR, &state2);
949 if (r) {
950 dev_err(&dssdev->dev, "failed to read Taal status\n");
951 goto err;
952 }
953
954 /* Each sleep out command will trigger a self diagnostic and flip
955 * Bit6 if the test passes.
956 */
957 if (!((state1 ^ state2) & (1 << 6))) {
958 dev_err(&dssdev->dev, "LCD self diagnostics failed\n");
959 goto err;
960 }
961 /* Self-diagnostics result is also shown on TE GPIO line. We need
962 * to re-enable TE after self diagnostics */
963 if (td->use_ext_te && td->te_enabled)
964 taal_enable_te(dssdev, true);
965
966 dsi_bus_unlock();
967
968 queue_delayed_work(td->esd_wq, &td->esd_work, TAAL_ESD_CHECK_PERIOD);
969
970 return;
971err:
972 dev_err(&dssdev->dev, "performing LCD reset\n");
973
974 taal_disable(dssdev);
975 taal_enable(dssdev);
976
977 dsi_bus_unlock();
978
979 queue_delayed_work(td->esd_wq, &td->esd_work, TAAL_ESD_CHECK_PERIOD);
980}
981
982static struct omap_dss_driver taal_driver = {
983 .probe = taal_probe,
984 .remove = taal_remove,
985
986 .enable = taal_enable,
987 .disable = taal_disable,
988 .suspend = taal_suspend,
989 .resume = taal_resume,
990
991 .setup_update = taal_setup_update,
Tomi Valkeinen96adcec2010-01-11 13:54:33 +0200992 .get_resolution = taal_get_resolution,
Tomi Valkeinenf133a9d2009-10-28 11:31:05 +0200993 .enable_te = taal_enable_te,
994 .wait_for_te = taal_wait_te,
995 .set_rotate = taal_rotate,
996 .get_rotate = taal_get_rotate,
997 .set_mirror = taal_mirror,
998 .get_mirror = taal_get_mirror,
999 .run_test = taal_run_test,
1000 .memory_read = taal_memory_read,
1001
1002 .driver = {
1003 .name = "taal",
1004 .owner = THIS_MODULE,
1005 },
1006};
1007
1008static int __init taal_init(void)
1009{
1010 omap_dss_register_driver(&taal_driver);
1011
1012 return 0;
1013}
1014
1015static void __exit taal_exit(void)
1016{
1017 omap_dss_unregister_driver(&taal_driver);
1018}
1019
1020module_init(taal_init);
1021module_exit(taal_exit);
1022
1023MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
1024MODULE_DESCRIPTION("Taal Driver");
1025MODULE_LICENSE("GPL");