]> Pileus Git - ~andy/linux/blob - drivers/video/omap2/displays/panel-taal.c
Merge branches 'acpi_pad', 'acpica', 'apei-bugzilla-43282', 'battery', 'cpuidle-coupl...
[~andy/linux] / drivers / video / omap2 / displays / panel-taal.c
1 /*
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/workqueue.h>
32 #include <linux/slab.h>
33 #include <linux/mutex.h>
34
35 #include <video/omapdss.h>
36 #include <video/omap-panel-nokia-dsi.h>
37 #include <video/mipi_display.h>
38
39 /* DSI Virtual channel. Hardcoded for now. */
40 #define TCH 0
41
42 #define DCS_READ_NUM_ERRORS     0x05
43 #define DCS_BRIGHTNESS          0x51
44 #define DCS_CTRL_DISPLAY        0x53
45 #define DCS_WRITE_CABC          0x55
46 #define DCS_READ_CABC           0x56
47 #define DCS_GET_ID1             0xda
48 #define DCS_GET_ID2             0xdb
49 #define DCS_GET_ID3             0xdc
50
51 static irqreturn_t taal_te_isr(int irq, void *data);
52 static void taal_te_timeout_work_callback(struct work_struct *work);
53 static int _taal_enable_te(struct omap_dss_device *dssdev, bool enable);
54
55 static int taal_panel_reset(struct omap_dss_device *dssdev);
56
57 /**
58  * struct panel_config - panel configuration
59  * @name: panel name
60  * @type: panel type
61  * @timings: panel resolution
62  * @sleep: various panel specific delays, passed to msleep() if non-zero
63  * @reset_sequence: reset sequence timings, passed to udelay() if non-zero
64  * @regulators: array of panel regulators
65  * @num_regulators: number of regulators in the array
66  */
67 struct panel_config {
68         const char *name;
69         int type;
70
71         struct omap_video_timings timings;
72
73         struct {
74                 unsigned int sleep_in;
75                 unsigned int sleep_out;
76                 unsigned int hw_reset;
77                 unsigned int enable_te;
78         } sleep;
79
80         struct {
81                 unsigned int high;
82                 unsigned int low;
83         } reset_sequence;
84
85 };
86
87 enum {
88         PANEL_TAAL,
89 };
90
91 static struct panel_config panel_configs[] = {
92         {
93                 .name           = "taal",
94                 .type           = PANEL_TAAL,
95                 .timings        = {
96                         .x_res          = 864,
97                         .y_res          = 480,
98                 },
99                 .sleep          = {
100                         .sleep_in       = 5,
101                         .sleep_out      = 5,
102                         .hw_reset       = 5,
103                         .enable_te      = 100, /* possible panel bug */
104                 },
105                 .reset_sequence = {
106                         .high           = 10,
107                         .low            = 10,
108                 },
109         },
110 };
111
112 struct taal_data {
113         struct mutex lock;
114
115         struct backlight_device *bldev;
116
117         unsigned long   hw_guard_end;   /* next value of jiffies when we can
118                                          * issue the next sleep in/out command
119                                          */
120         unsigned long   hw_guard_wait;  /* max guard time in jiffies */
121
122         struct omap_dss_device *dssdev;
123
124         bool enabled;
125         u8 rotate;
126         bool mirror;
127
128         bool te_enabled;
129
130         atomic_t do_update;
131         int channel;
132
133         struct delayed_work te_timeout_work;
134
135         bool cabc_broken;
136         unsigned cabc_mode;
137
138         bool intro_printed;
139
140         struct workqueue_struct *workqueue;
141
142         struct delayed_work esd_work;
143         unsigned esd_interval;
144
145         bool ulps_enabled;
146         unsigned ulps_timeout;
147         struct delayed_work ulps_work;
148
149         struct panel_config *panel_config;
150 };
151
152 static inline struct nokia_dsi_panel_data
153 *get_panel_data(const struct omap_dss_device *dssdev)
154 {
155         return (struct nokia_dsi_panel_data *) dssdev->data;
156 }
157
158 static void taal_esd_work(struct work_struct *work);
159 static void taal_ulps_work(struct work_struct *work);
160
161 static void hw_guard_start(struct taal_data *td, int guard_msec)
162 {
163         td->hw_guard_wait = msecs_to_jiffies(guard_msec);
164         td->hw_guard_end = jiffies + td->hw_guard_wait;
165 }
166
167 static void hw_guard_wait(struct taal_data *td)
168 {
169         unsigned long wait = td->hw_guard_end - jiffies;
170
171         if ((long)wait > 0 && wait <= td->hw_guard_wait) {
172                 set_current_state(TASK_UNINTERRUPTIBLE);
173                 schedule_timeout(wait);
174         }
175 }
176
177 static int taal_dcs_read_1(struct taal_data *td, u8 dcs_cmd, u8 *data)
178 {
179         int r;
180         u8 buf[1];
181
182         r = dsi_vc_dcs_read(td->dssdev, td->channel, dcs_cmd, buf, 1);
183
184         if (r < 0)
185                 return r;
186
187         *data = buf[0];
188
189         return 0;
190 }
191
192 static int taal_dcs_write_0(struct taal_data *td, u8 dcs_cmd)
193 {
194         return dsi_vc_dcs_write(td->dssdev, td->channel, &dcs_cmd, 1);
195 }
196
197 static int taal_dcs_write_1(struct taal_data *td, u8 dcs_cmd, u8 param)
198 {
199         u8 buf[2];
200         buf[0] = dcs_cmd;
201         buf[1] = param;
202         return dsi_vc_dcs_write(td->dssdev, td->channel, buf, 2);
203 }
204
205 static int taal_sleep_in(struct taal_data *td)
206
207 {
208         u8 cmd;
209         int r;
210
211         hw_guard_wait(td);
212
213         cmd = MIPI_DCS_ENTER_SLEEP_MODE;
214         r = dsi_vc_dcs_write_nosync(td->dssdev, td->channel, &cmd, 1);
215         if (r)
216                 return r;
217
218         hw_guard_start(td, 120);
219
220         if (td->panel_config->sleep.sleep_in)
221                 msleep(td->panel_config->sleep.sleep_in);
222
223         return 0;
224 }
225
226 static int taal_sleep_out(struct taal_data *td)
227 {
228         int r;
229
230         hw_guard_wait(td);
231
232         r = taal_dcs_write_0(td, MIPI_DCS_EXIT_SLEEP_MODE);
233         if (r)
234                 return r;
235
236         hw_guard_start(td, 120);
237
238         if (td->panel_config->sleep.sleep_out)
239                 msleep(td->panel_config->sleep.sleep_out);
240
241         return 0;
242 }
243
244 static int taal_get_id(struct taal_data *td, u8 *id1, u8 *id2, u8 *id3)
245 {
246         int r;
247
248         r = taal_dcs_read_1(td, DCS_GET_ID1, id1);
249         if (r)
250                 return r;
251         r = taal_dcs_read_1(td, DCS_GET_ID2, id2);
252         if (r)
253                 return r;
254         r = taal_dcs_read_1(td, DCS_GET_ID3, id3);
255         if (r)
256                 return r;
257
258         return 0;
259 }
260
261 static int taal_set_addr_mode(struct taal_data *td, u8 rotate, bool mirror)
262 {
263         int r;
264         u8 mode;
265         int b5, b6, b7;
266
267         r = taal_dcs_read_1(td, MIPI_DCS_GET_ADDRESS_MODE, &mode);
268         if (r)
269                 return r;
270
271         switch (rotate) {
272         default:
273         case 0:
274                 b7 = 0;
275                 b6 = 0;
276                 b5 = 0;
277                 break;
278         case 1:
279                 b7 = 0;
280                 b6 = 1;
281                 b5 = 1;
282                 break;
283         case 2:
284                 b7 = 1;
285                 b6 = 1;
286                 b5 = 0;
287                 break;
288         case 3:
289                 b7 = 1;
290                 b6 = 0;
291                 b5 = 1;
292                 break;
293         }
294
295         if (mirror)
296                 b6 = !b6;
297
298         mode &= ~((1<<7) | (1<<6) | (1<<5));
299         mode |= (b7 << 7) | (b6 << 6) | (b5 << 5);
300
301         return taal_dcs_write_1(td, MIPI_DCS_SET_ADDRESS_MODE, mode);
302 }
303
304 static int taal_set_update_window(struct taal_data *td,
305                 u16 x, u16 y, u16 w, u16 h)
306 {
307         int r;
308         u16 x1 = x;
309         u16 x2 = x + w - 1;
310         u16 y1 = y;
311         u16 y2 = y + h - 1;
312
313         u8 buf[5];
314         buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS;
315         buf[1] = (x1 >> 8) & 0xff;
316         buf[2] = (x1 >> 0) & 0xff;
317         buf[3] = (x2 >> 8) & 0xff;
318         buf[4] = (x2 >> 0) & 0xff;
319
320         r = dsi_vc_dcs_write_nosync(td->dssdev, td->channel, buf, sizeof(buf));
321         if (r)
322                 return r;
323
324         buf[0] = MIPI_DCS_SET_PAGE_ADDRESS;
325         buf[1] = (y1 >> 8) & 0xff;
326         buf[2] = (y1 >> 0) & 0xff;
327         buf[3] = (y2 >> 8) & 0xff;
328         buf[4] = (y2 >> 0) & 0xff;
329
330         r = dsi_vc_dcs_write_nosync(td->dssdev, td->channel, buf, sizeof(buf));
331         if (r)
332                 return r;
333
334         dsi_vc_send_bta_sync(td->dssdev, td->channel);
335
336         return r;
337 }
338
339 static void taal_queue_esd_work(struct omap_dss_device *dssdev)
340 {
341         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
342
343         if (td->esd_interval > 0)
344                 queue_delayed_work(td->workqueue, &td->esd_work,
345                                 msecs_to_jiffies(td->esd_interval));
346 }
347
348 static void taal_cancel_esd_work(struct omap_dss_device *dssdev)
349 {
350         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
351
352         cancel_delayed_work(&td->esd_work);
353 }
354
355 static void taal_queue_ulps_work(struct omap_dss_device *dssdev)
356 {
357         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
358
359         if (td->ulps_timeout > 0)
360                 queue_delayed_work(td->workqueue, &td->ulps_work,
361                                 msecs_to_jiffies(td->ulps_timeout));
362 }
363
364 static void taal_cancel_ulps_work(struct omap_dss_device *dssdev)
365 {
366         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
367
368         cancel_delayed_work(&td->ulps_work);
369 }
370
371 static int taal_enter_ulps(struct omap_dss_device *dssdev)
372 {
373         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
374         struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
375         int r;
376
377         if (td->ulps_enabled)
378                 return 0;
379
380         taal_cancel_ulps_work(dssdev);
381
382         r = _taal_enable_te(dssdev, false);
383         if (r)
384                 goto err;
385
386         disable_irq(gpio_to_irq(panel_data->ext_te_gpio));
387
388         omapdss_dsi_display_disable(dssdev, false, true);
389
390         td->ulps_enabled = true;
391
392         return 0;
393
394 err:
395         dev_err(&dssdev->dev, "enter ULPS failed");
396         taal_panel_reset(dssdev);
397
398         td->ulps_enabled = false;
399
400         taal_queue_ulps_work(dssdev);
401
402         return r;
403 }
404
405 static int taal_exit_ulps(struct omap_dss_device *dssdev)
406 {
407         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
408         struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
409         int r;
410
411         if (!td->ulps_enabled)
412                 return 0;
413
414         r = omapdss_dsi_display_enable(dssdev);
415         if (r) {
416                 dev_err(&dssdev->dev, "failed to enable DSI\n");
417                 goto err1;
418         }
419
420         omapdss_dsi_vc_enable_hs(dssdev, td->channel, true);
421
422         r = _taal_enable_te(dssdev, true);
423         if (r) {
424                 dev_err(&dssdev->dev, "failed to re-enable TE");
425                 goto err2;
426         }
427
428         enable_irq(gpio_to_irq(panel_data->ext_te_gpio));
429
430         taal_queue_ulps_work(dssdev);
431
432         td->ulps_enabled = false;
433
434         return 0;
435
436 err2:
437         dev_err(&dssdev->dev, "failed to exit ULPS");
438
439         r = taal_panel_reset(dssdev);
440         if (!r) {
441                 enable_irq(gpio_to_irq(panel_data->ext_te_gpio));
442                 td->ulps_enabled = false;
443         }
444 err1:
445         taal_queue_ulps_work(dssdev);
446
447         return r;
448 }
449
450 static int taal_wake_up(struct omap_dss_device *dssdev)
451 {
452         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
453
454         if (td->ulps_enabled)
455                 return taal_exit_ulps(dssdev);
456
457         taal_cancel_ulps_work(dssdev);
458         taal_queue_ulps_work(dssdev);
459         return 0;
460 }
461
462 static int taal_bl_update_status(struct backlight_device *dev)
463 {
464         struct omap_dss_device *dssdev = dev_get_drvdata(&dev->dev);
465         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
466         int r;
467         int level;
468
469         if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
470                         dev->props.power == FB_BLANK_UNBLANK)
471                 level = dev->props.brightness;
472         else
473                 level = 0;
474
475         dev_dbg(&dssdev->dev, "update brightness to %d\n", level);
476
477         mutex_lock(&td->lock);
478
479         if (td->enabled) {
480                 dsi_bus_lock(dssdev);
481
482                 r = taal_wake_up(dssdev);
483                 if (!r)
484                         r = taal_dcs_write_1(td, DCS_BRIGHTNESS, level);
485
486                 dsi_bus_unlock(dssdev);
487         } else {
488                 r = 0;
489         }
490
491         mutex_unlock(&td->lock);
492
493         return r;
494 }
495
496 static int taal_bl_get_intensity(struct backlight_device *dev)
497 {
498         if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
499                         dev->props.power == FB_BLANK_UNBLANK)
500                 return dev->props.brightness;
501
502         return 0;
503 }
504
505 static const struct backlight_ops taal_bl_ops = {
506         .get_brightness = taal_bl_get_intensity,
507         .update_status  = taal_bl_update_status,
508 };
509
510 static void taal_get_resolution(struct omap_dss_device *dssdev,
511                 u16 *xres, u16 *yres)
512 {
513         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
514
515         if (td->rotate == 0 || td->rotate == 2) {
516                 *xres = dssdev->panel.timings.x_res;
517                 *yres = dssdev->panel.timings.y_res;
518         } else {
519                 *yres = dssdev->panel.timings.x_res;
520                 *xres = dssdev->panel.timings.y_res;
521         }
522 }
523
524 static ssize_t taal_num_errors_show(struct device *dev,
525                 struct device_attribute *attr, char *buf)
526 {
527         struct omap_dss_device *dssdev = to_dss_device(dev);
528         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
529         u8 errors = 0;
530         int r;
531
532         mutex_lock(&td->lock);
533
534         if (td->enabled) {
535                 dsi_bus_lock(dssdev);
536
537                 r = taal_wake_up(dssdev);
538                 if (!r)
539                         r = taal_dcs_read_1(td, DCS_READ_NUM_ERRORS, &errors);
540
541                 dsi_bus_unlock(dssdev);
542         } else {
543                 r = -ENODEV;
544         }
545
546         mutex_unlock(&td->lock);
547
548         if (r)
549                 return r;
550
551         return snprintf(buf, PAGE_SIZE, "%d\n", errors);
552 }
553
554 static ssize_t taal_hw_revision_show(struct device *dev,
555                 struct device_attribute *attr, char *buf)
556 {
557         struct omap_dss_device *dssdev = to_dss_device(dev);
558         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
559         u8 id1, id2, id3;
560         int r;
561
562         mutex_lock(&td->lock);
563
564         if (td->enabled) {
565                 dsi_bus_lock(dssdev);
566
567                 r = taal_wake_up(dssdev);
568                 if (!r)
569                         r = taal_get_id(td, &id1, &id2, &id3);
570
571                 dsi_bus_unlock(dssdev);
572         } else {
573                 r = -ENODEV;
574         }
575
576         mutex_unlock(&td->lock);
577
578         if (r)
579                 return r;
580
581         return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
582 }
583
584 static const char *cabc_modes[] = {
585         "off",          /* used also always when CABC is not supported */
586         "ui",
587         "still-image",
588         "moving-image",
589 };
590
591 static ssize_t show_cabc_mode(struct device *dev,
592                 struct device_attribute *attr,
593                 char *buf)
594 {
595         struct omap_dss_device *dssdev = to_dss_device(dev);
596         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
597         const char *mode_str;
598         int mode;
599         int len;
600
601         mode = td->cabc_mode;
602
603         mode_str = "unknown";
604         if (mode >= 0 && mode < ARRAY_SIZE(cabc_modes))
605                 mode_str = cabc_modes[mode];
606         len = snprintf(buf, PAGE_SIZE, "%s\n", mode_str);
607
608         return len < PAGE_SIZE - 1 ? len : PAGE_SIZE - 1;
609 }
610
611 static ssize_t store_cabc_mode(struct device *dev,
612                 struct device_attribute *attr,
613                 const char *buf, size_t count)
614 {
615         struct omap_dss_device *dssdev = to_dss_device(dev);
616         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
617         int i;
618         int r;
619
620         for (i = 0; i < ARRAY_SIZE(cabc_modes); i++) {
621                 if (sysfs_streq(cabc_modes[i], buf))
622                         break;
623         }
624
625         if (i == ARRAY_SIZE(cabc_modes))
626                 return -EINVAL;
627
628         mutex_lock(&td->lock);
629
630         if (td->enabled) {
631                 dsi_bus_lock(dssdev);
632
633                 if (!td->cabc_broken) {
634                         r = taal_wake_up(dssdev);
635                         if (r)
636                                 goto err;
637
638                         r = taal_dcs_write_1(td, DCS_WRITE_CABC, i);
639                         if (r)
640                                 goto err;
641                 }
642
643                 dsi_bus_unlock(dssdev);
644         }
645
646         td->cabc_mode = i;
647
648         mutex_unlock(&td->lock);
649
650         return count;
651 err:
652         dsi_bus_unlock(dssdev);
653         mutex_unlock(&td->lock);
654         return r;
655 }
656
657 static ssize_t show_cabc_available_modes(struct device *dev,
658                 struct device_attribute *attr,
659                 char *buf)
660 {
661         int len;
662         int i;
663
664         for (i = 0, len = 0;
665              len < PAGE_SIZE && i < ARRAY_SIZE(cabc_modes); i++)
666                 len += snprintf(&buf[len], PAGE_SIZE - len, "%s%s%s",
667                         i ? " " : "", cabc_modes[i],
668                         i == ARRAY_SIZE(cabc_modes) - 1 ? "\n" : "");
669
670         return len < PAGE_SIZE ? len : PAGE_SIZE - 1;
671 }
672
673 static ssize_t taal_store_esd_interval(struct device *dev,
674                 struct device_attribute *attr,
675                 const char *buf, size_t count)
676 {
677         struct omap_dss_device *dssdev = to_dss_device(dev);
678         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
679
680         unsigned long t;
681         int r;
682
683         r = strict_strtoul(buf, 10, &t);
684         if (r)
685                 return r;
686
687         mutex_lock(&td->lock);
688         taal_cancel_esd_work(dssdev);
689         td->esd_interval = t;
690         if (td->enabled)
691                 taal_queue_esd_work(dssdev);
692         mutex_unlock(&td->lock);
693
694         return count;
695 }
696
697 static ssize_t taal_show_esd_interval(struct device *dev,
698                 struct device_attribute *attr,
699                 char *buf)
700 {
701         struct omap_dss_device *dssdev = to_dss_device(dev);
702         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
703         unsigned t;
704
705         mutex_lock(&td->lock);
706         t = td->esd_interval;
707         mutex_unlock(&td->lock);
708
709         return snprintf(buf, PAGE_SIZE, "%u\n", t);
710 }
711
712 static ssize_t taal_store_ulps(struct device *dev,
713                 struct device_attribute *attr,
714                 const char *buf, size_t count)
715 {
716         struct omap_dss_device *dssdev = to_dss_device(dev);
717         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
718         unsigned long t;
719         int r;
720
721         r = strict_strtoul(buf, 10, &t);
722         if (r)
723                 return r;
724
725         mutex_lock(&td->lock);
726
727         if (td->enabled) {
728                 dsi_bus_lock(dssdev);
729
730                 if (t)
731                         r = taal_enter_ulps(dssdev);
732                 else
733                         r = taal_wake_up(dssdev);
734
735                 dsi_bus_unlock(dssdev);
736         }
737
738         mutex_unlock(&td->lock);
739
740         if (r)
741                 return r;
742
743         return count;
744 }
745
746 static ssize_t taal_show_ulps(struct device *dev,
747                 struct device_attribute *attr,
748                 char *buf)
749 {
750         struct omap_dss_device *dssdev = to_dss_device(dev);
751         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
752         unsigned t;
753
754         mutex_lock(&td->lock);
755         t = td->ulps_enabled;
756         mutex_unlock(&td->lock);
757
758         return snprintf(buf, PAGE_SIZE, "%u\n", t);
759 }
760
761 static ssize_t taal_store_ulps_timeout(struct device *dev,
762                 struct device_attribute *attr,
763                 const char *buf, size_t count)
764 {
765         struct omap_dss_device *dssdev = to_dss_device(dev);
766         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
767         unsigned long t;
768         int r;
769
770         r = strict_strtoul(buf, 10, &t);
771         if (r)
772                 return r;
773
774         mutex_lock(&td->lock);
775         td->ulps_timeout = t;
776
777         if (td->enabled) {
778                 /* taal_wake_up will restart the timer */
779                 dsi_bus_lock(dssdev);
780                 r = taal_wake_up(dssdev);
781                 dsi_bus_unlock(dssdev);
782         }
783
784         mutex_unlock(&td->lock);
785
786         if (r)
787                 return r;
788
789         return count;
790 }
791
792 static ssize_t taal_show_ulps_timeout(struct device *dev,
793                 struct device_attribute *attr,
794                 char *buf)
795 {
796         struct omap_dss_device *dssdev = to_dss_device(dev);
797         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
798         unsigned t;
799
800         mutex_lock(&td->lock);
801         t = td->ulps_timeout;
802         mutex_unlock(&td->lock);
803
804         return snprintf(buf, PAGE_SIZE, "%u\n", t);
805 }
806
807 static DEVICE_ATTR(num_dsi_errors, S_IRUGO, taal_num_errors_show, NULL);
808 static DEVICE_ATTR(hw_revision, S_IRUGO, taal_hw_revision_show, NULL);
809 static DEVICE_ATTR(cabc_mode, S_IRUGO | S_IWUSR,
810                 show_cabc_mode, store_cabc_mode);
811 static DEVICE_ATTR(cabc_available_modes, S_IRUGO,
812                 show_cabc_available_modes, NULL);
813 static DEVICE_ATTR(esd_interval, S_IRUGO | S_IWUSR,
814                 taal_show_esd_interval, taal_store_esd_interval);
815 static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
816                 taal_show_ulps, taal_store_ulps);
817 static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
818                 taal_show_ulps_timeout, taal_store_ulps_timeout);
819
820 static struct attribute *taal_attrs[] = {
821         &dev_attr_num_dsi_errors.attr,
822         &dev_attr_hw_revision.attr,
823         &dev_attr_cabc_mode.attr,
824         &dev_attr_cabc_available_modes.attr,
825         &dev_attr_esd_interval.attr,
826         &dev_attr_ulps.attr,
827         &dev_attr_ulps_timeout.attr,
828         NULL,
829 };
830
831 static struct attribute_group taal_attr_group = {
832         .attrs = taal_attrs,
833 };
834
835 static void taal_hw_reset(struct omap_dss_device *dssdev)
836 {
837         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
838         struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
839
840         if (panel_data->reset_gpio == -1)
841                 return;
842
843         gpio_set_value(panel_data->reset_gpio, 1);
844         if (td->panel_config->reset_sequence.high)
845                 udelay(td->panel_config->reset_sequence.high);
846         /* reset the panel */
847         gpio_set_value(panel_data->reset_gpio, 0);
848         /* assert reset */
849         if (td->panel_config->reset_sequence.low)
850                 udelay(td->panel_config->reset_sequence.low);
851         gpio_set_value(panel_data->reset_gpio, 1);
852         /* wait after releasing reset */
853         if (td->panel_config->sleep.hw_reset)
854                 msleep(td->panel_config->sleep.hw_reset);
855 }
856
857 static int taal_probe(struct omap_dss_device *dssdev)
858 {
859         struct backlight_properties props;
860         struct taal_data *td;
861         struct backlight_device *bldev = NULL;
862         struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
863         struct panel_config *panel_config = NULL;
864         int r, i;
865
866         dev_dbg(&dssdev->dev, "probe\n");
867
868         if (!panel_data || !panel_data->name) {
869                 r = -EINVAL;
870                 goto err;
871         }
872
873         for (i = 0; i < ARRAY_SIZE(panel_configs); i++) {
874                 if (strcmp(panel_data->name, panel_configs[i].name) == 0) {
875                         panel_config = &panel_configs[i];
876                         break;
877                 }
878         }
879
880         if (!panel_config) {
881                 r = -EINVAL;
882                 goto err;
883         }
884
885         dssdev->panel.config = OMAP_DSS_LCD_TFT;
886         dssdev->panel.timings = panel_config->timings;
887         dssdev->panel.dsi_pix_fmt = OMAP_DSS_DSI_FMT_RGB888;
888
889         td = kzalloc(sizeof(*td), GFP_KERNEL);
890         if (!td) {
891                 r = -ENOMEM;
892                 goto err;
893         }
894         td->dssdev = dssdev;
895         td->panel_config = panel_config;
896         td->esd_interval = panel_data->esd_interval;
897         td->ulps_enabled = false;
898         td->ulps_timeout = panel_data->ulps_timeout;
899
900         mutex_init(&td->lock);
901
902         atomic_set(&td->do_update, 0);
903
904         td->workqueue = create_singlethread_workqueue("taal_esd");
905         if (td->workqueue == NULL) {
906                 dev_err(&dssdev->dev, "can't create ESD workqueue\n");
907                 r = -ENOMEM;
908                 goto err_wq;
909         }
910         INIT_DELAYED_WORK_DEFERRABLE(&td->esd_work, taal_esd_work);
911         INIT_DELAYED_WORK(&td->ulps_work, taal_ulps_work);
912
913         dev_set_drvdata(&dssdev->dev, td);
914
915         if (gpio_is_valid(panel_data->reset_gpio)) {
916                 r = gpio_request_one(panel_data->reset_gpio, GPIOF_OUT_INIT_LOW,
917                                 "taal rst");
918                 if (r) {
919                         dev_err(&dssdev->dev, "failed to request reset gpio\n");
920                         goto err_rst_gpio;
921                 }
922         }
923
924         taal_hw_reset(dssdev);
925
926         if (panel_data->use_dsi_backlight) {
927                 memset(&props, 0, sizeof(struct backlight_properties));
928                 props.max_brightness = 255;
929
930                 props.type = BACKLIGHT_RAW;
931                 bldev = backlight_device_register(dev_name(&dssdev->dev),
932                                 &dssdev->dev, dssdev, &taal_bl_ops, &props);
933                 if (IS_ERR(bldev)) {
934                         r = PTR_ERR(bldev);
935                         goto err_bl;
936                 }
937
938                 td->bldev = bldev;
939
940                 bldev->props.fb_blank = FB_BLANK_UNBLANK;
941                 bldev->props.power = FB_BLANK_UNBLANK;
942                 bldev->props.brightness = 255;
943
944                 taal_bl_update_status(bldev);
945         }
946
947         if (panel_data->use_ext_te) {
948                 int gpio = panel_data->ext_te_gpio;
949
950                 r = gpio_request_one(gpio, GPIOF_IN, "taal irq");
951                 if (r) {
952                         dev_err(&dssdev->dev, "GPIO request failed\n");
953                         goto err_gpio;
954                 }
955
956                 r = request_irq(gpio_to_irq(gpio), taal_te_isr,
957                                 IRQF_TRIGGER_RISING,
958                                 "taal vsync", dssdev);
959
960                 if (r) {
961                         dev_err(&dssdev->dev, "IRQ request failed\n");
962                         gpio_free(gpio);
963                         goto err_irq;
964                 }
965
966                 INIT_DELAYED_WORK_DEFERRABLE(&td->te_timeout_work,
967                                         taal_te_timeout_work_callback);
968
969                 dev_dbg(&dssdev->dev, "Using GPIO TE\n");
970         }
971
972         r = omap_dsi_request_vc(dssdev, &td->channel);
973         if (r) {
974                 dev_err(&dssdev->dev, "failed to get virtual channel\n");
975                 goto err_req_vc;
976         }
977
978         r = omap_dsi_set_vc_id(dssdev, td->channel, TCH);
979         if (r) {
980                 dev_err(&dssdev->dev, "failed to set VC_ID\n");
981                 goto err_vc_id;
982         }
983
984         r = sysfs_create_group(&dssdev->dev.kobj, &taal_attr_group);
985         if (r) {
986                 dev_err(&dssdev->dev, "failed to create sysfs files\n");
987                 goto err_vc_id;
988         }
989
990         return 0;
991
992 err_vc_id:
993         omap_dsi_release_vc(dssdev, td->channel);
994 err_req_vc:
995         if (panel_data->use_ext_te)
996                 free_irq(gpio_to_irq(panel_data->ext_te_gpio), dssdev);
997 err_irq:
998         if (panel_data->use_ext_te)
999                 gpio_free(panel_data->ext_te_gpio);
1000 err_gpio:
1001         if (bldev != NULL)
1002                 backlight_device_unregister(bldev);
1003 err_bl:
1004         if (gpio_is_valid(panel_data->reset_gpio))
1005                 gpio_free(panel_data->reset_gpio);
1006 err_rst_gpio:
1007         destroy_workqueue(td->workqueue);
1008 err_wq:
1009         kfree(td);
1010 err:
1011         return r;
1012 }
1013
1014 static void __exit taal_remove(struct omap_dss_device *dssdev)
1015 {
1016         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1017         struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1018         struct backlight_device *bldev;
1019
1020         dev_dbg(&dssdev->dev, "remove\n");
1021
1022         sysfs_remove_group(&dssdev->dev.kobj, &taal_attr_group);
1023         omap_dsi_release_vc(dssdev, td->channel);
1024
1025         if (panel_data->use_ext_te) {
1026                 int gpio = panel_data->ext_te_gpio;
1027                 free_irq(gpio_to_irq(gpio), dssdev);
1028                 gpio_free(gpio);
1029         }
1030
1031         bldev = td->bldev;
1032         if (bldev != NULL) {
1033                 bldev->props.power = FB_BLANK_POWERDOWN;
1034                 taal_bl_update_status(bldev);
1035                 backlight_device_unregister(bldev);
1036         }
1037
1038         taal_cancel_ulps_work(dssdev);
1039         taal_cancel_esd_work(dssdev);
1040         destroy_workqueue(td->workqueue);
1041
1042         /* reset, to be sure that the panel is in a valid state */
1043         taal_hw_reset(dssdev);
1044
1045         if (gpio_is_valid(panel_data->reset_gpio))
1046                 gpio_free(panel_data->reset_gpio);
1047
1048         kfree(td);
1049 }
1050
1051 static int taal_power_on(struct omap_dss_device *dssdev)
1052 {
1053         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1054         struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1055         u8 id1, id2, id3;
1056         int r;
1057
1058         r = omapdss_dsi_configure_pins(dssdev, &panel_data->pin_config);
1059         if (r) {
1060                 dev_err(&dssdev->dev, "failed to configure DSI pins\n");
1061                 goto err0;
1062         };
1063
1064         r = omapdss_dsi_display_enable(dssdev);
1065         if (r) {
1066                 dev_err(&dssdev->dev, "failed to enable DSI\n");
1067                 goto err0;
1068         }
1069
1070         taal_hw_reset(dssdev);
1071
1072         omapdss_dsi_vc_enable_hs(dssdev, td->channel, false);
1073
1074         r = taal_sleep_out(td);
1075         if (r)
1076                 goto err;
1077
1078         r = taal_get_id(td, &id1, &id2, &id3);
1079         if (r)
1080                 goto err;
1081
1082         /* on early Taal revisions CABC is broken */
1083         if (td->panel_config->type == PANEL_TAAL &&
1084                 (id2 == 0x00 || id2 == 0xff || id2 == 0x81))
1085                 td->cabc_broken = true;
1086
1087         r = taal_dcs_write_1(td, DCS_BRIGHTNESS, 0xff);
1088         if (r)
1089                 goto err;
1090
1091         r = taal_dcs_write_1(td, DCS_CTRL_DISPLAY,
1092                         (1<<2) | (1<<5));       /* BL | BCTRL */
1093         if (r)
1094                 goto err;
1095
1096         r = taal_dcs_write_1(td, MIPI_DCS_SET_PIXEL_FORMAT,
1097                 MIPI_DCS_PIXEL_FMT_24BIT);
1098         if (r)
1099                 goto err;
1100
1101         r = taal_set_addr_mode(td, td->rotate, td->mirror);
1102         if (r)
1103                 goto err;
1104
1105         if (!td->cabc_broken) {
1106                 r = taal_dcs_write_1(td, DCS_WRITE_CABC, td->cabc_mode);
1107                 if (r)
1108                         goto err;
1109         }
1110
1111         r = taal_dcs_write_0(td, MIPI_DCS_SET_DISPLAY_ON);
1112         if (r)
1113                 goto err;
1114
1115         r = _taal_enable_te(dssdev, td->te_enabled);
1116         if (r)
1117                 goto err;
1118
1119         r = dsi_enable_video_output(dssdev, td->channel);
1120         if (r)
1121                 goto err;
1122
1123         td->enabled = 1;
1124
1125         if (!td->intro_printed) {
1126                 dev_info(&dssdev->dev, "%s panel revision %02x.%02x.%02x\n",
1127                         td->panel_config->name, id1, id2, id3);
1128                 if (td->cabc_broken)
1129                         dev_info(&dssdev->dev,
1130                                         "old Taal version, CABC disabled\n");
1131                 td->intro_printed = true;
1132         }
1133
1134         omapdss_dsi_vc_enable_hs(dssdev, td->channel, true);
1135
1136         return 0;
1137 err:
1138         dev_err(&dssdev->dev, "error while enabling panel, issuing HW reset\n");
1139
1140         taal_hw_reset(dssdev);
1141
1142         omapdss_dsi_display_disable(dssdev, true, false);
1143 err0:
1144         return r;
1145 }
1146
1147 static void taal_power_off(struct omap_dss_device *dssdev)
1148 {
1149         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1150         int r;
1151
1152         dsi_disable_video_output(dssdev, td->channel);
1153
1154         r = taal_dcs_write_0(td, MIPI_DCS_SET_DISPLAY_OFF);
1155         if (!r)
1156                 r = taal_sleep_in(td);
1157
1158         if (r) {
1159                 dev_err(&dssdev->dev,
1160                                 "error disabling panel, issuing HW reset\n");
1161                 taal_hw_reset(dssdev);
1162         }
1163
1164         omapdss_dsi_display_disable(dssdev, true, false);
1165
1166         td->enabled = 0;
1167 }
1168
1169 static int taal_panel_reset(struct omap_dss_device *dssdev)
1170 {
1171         dev_err(&dssdev->dev, "performing LCD reset\n");
1172
1173         taal_power_off(dssdev);
1174         taal_hw_reset(dssdev);
1175         return taal_power_on(dssdev);
1176 }
1177
1178 static int taal_enable(struct omap_dss_device *dssdev)
1179 {
1180         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1181         int r;
1182
1183         dev_dbg(&dssdev->dev, "enable\n");
1184
1185         mutex_lock(&td->lock);
1186
1187         if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED) {
1188                 r = -EINVAL;
1189                 goto err;
1190         }
1191
1192         dsi_bus_lock(dssdev);
1193
1194         r = taal_power_on(dssdev);
1195
1196         dsi_bus_unlock(dssdev);
1197
1198         if (r)
1199                 goto err;
1200
1201         taal_queue_esd_work(dssdev);
1202
1203         dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
1204
1205         mutex_unlock(&td->lock);
1206
1207         return 0;
1208 err:
1209         dev_dbg(&dssdev->dev, "enable failed\n");
1210         mutex_unlock(&td->lock);
1211         return r;
1212 }
1213
1214 static void taal_disable(struct omap_dss_device *dssdev)
1215 {
1216         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1217
1218         dev_dbg(&dssdev->dev, "disable\n");
1219
1220         mutex_lock(&td->lock);
1221
1222         taal_cancel_ulps_work(dssdev);
1223         taal_cancel_esd_work(dssdev);
1224
1225         dsi_bus_lock(dssdev);
1226
1227         if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
1228                 int r;
1229
1230                 r = taal_wake_up(dssdev);
1231                 if (!r)
1232                         taal_power_off(dssdev);
1233         }
1234
1235         dsi_bus_unlock(dssdev);
1236
1237         dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
1238
1239         mutex_unlock(&td->lock);
1240 }
1241
1242 static int taal_suspend(struct omap_dss_device *dssdev)
1243 {
1244         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1245         int r;
1246
1247         dev_dbg(&dssdev->dev, "suspend\n");
1248
1249         mutex_lock(&td->lock);
1250
1251         if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE) {
1252                 r = -EINVAL;
1253                 goto err;
1254         }
1255
1256         taal_cancel_ulps_work(dssdev);
1257         taal_cancel_esd_work(dssdev);
1258
1259         dsi_bus_lock(dssdev);
1260
1261         r = taal_wake_up(dssdev);
1262         if (!r)
1263                 taal_power_off(dssdev);
1264
1265         dsi_bus_unlock(dssdev);
1266
1267         dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
1268
1269         mutex_unlock(&td->lock);
1270
1271         return 0;
1272 err:
1273         mutex_unlock(&td->lock);
1274         return r;
1275 }
1276
1277 static int taal_resume(struct omap_dss_device *dssdev)
1278 {
1279         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1280         int r;
1281
1282         dev_dbg(&dssdev->dev, "resume\n");
1283
1284         mutex_lock(&td->lock);
1285
1286         if (dssdev->state != OMAP_DSS_DISPLAY_SUSPENDED) {
1287                 r = -EINVAL;
1288                 goto err;
1289         }
1290
1291         dsi_bus_lock(dssdev);
1292
1293         r = taal_power_on(dssdev);
1294
1295         dsi_bus_unlock(dssdev);
1296
1297         if (r) {
1298                 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
1299         } else {
1300                 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
1301                 taal_queue_esd_work(dssdev);
1302         }
1303
1304         mutex_unlock(&td->lock);
1305
1306         return r;
1307 err:
1308         mutex_unlock(&td->lock);
1309         return r;
1310 }
1311
1312 static void taal_framedone_cb(int err, void *data)
1313 {
1314         struct omap_dss_device *dssdev = data;
1315         dev_dbg(&dssdev->dev, "framedone, err %d\n", err);
1316         dsi_bus_unlock(dssdev);
1317 }
1318
1319 static irqreturn_t taal_te_isr(int irq, void *data)
1320 {
1321         struct omap_dss_device *dssdev = data;
1322         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1323         int old;
1324         int r;
1325
1326         old = atomic_cmpxchg(&td->do_update, 1, 0);
1327
1328         if (old) {
1329                 cancel_delayed_work(&td->te_timeout_work);
1330
1331                 r = omap_dsi_update(dssdev, td->channel, taal_framedone_cb,
1332                                 dssdev);
1333                 if (r)
1334                         goto err;
1335         }
1336
1337         return IRQ_HANDLED;
1338 err:
1339         dev_err(&dssdev->dev, "start update failed\n");
1340         dsi_bus_unlock(dssdev);
1341         return IRQ_HANDLED;
1342 }
1343
1344 static void taal_te_timeout_work_callback(struct work_struct *work)
1345 {
1346         struct taal_data *td = container_of(work, struct taal_data,
1347                                         te_timeout_work.work);
1348         struct omap_dss_device *dssdev = td->dssdev;
1349
1350         dev_err(&dssdev->dev, "TE not received for 250ms!\n");
1351
1352         atomic_set(&td->do_update, 0);
1353         dsi_bus_unlock(dssdev);
1354 }
1355
1356 static int taal_update(struct omap_dss_device *dssdev,
1357                                     u16 x, u16 y, u16 w, u16 h)
1358 {
1359         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1360         struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1361         int r;
1362
1363         dev_dbg(&dssdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
1364
1365         mutex_lock(&td->lock);
1366         dsi_bus_lock(dssdev);
1367
1368         r = taal_wake_up(dssdev);
1369         if (r)
1370                 goto err;
1371
1372         if (!td->enabled) {
1373                 r = 0;
1374                 goto err;
1375         }
1376
1377         /* XXX no need to send this every frame, but dsi break if not done */
1378         r = taal_set_update_window(td, 0, 0,
1379                         td->panel_config->timings.x_res,
1380                         td->panel_config->timings.y_res);
1381         if (r)
1382                 goto err;
1383
1384         if (td->te_enabled && panel_data->use_ext_te) {
1385                 schedule_delayed_work(&td->te_timeout_work,
1386                                 msecs_to_jiffies(250));
1387                 atomic_set(&td->do_update, 1);
1388         } else {
1389                 r = omap_dsi_update(dssdev, td->channel, taal_framedone_cb,
1390                                 dssdev);
1391                 if (r)
1392                         goto err;
1393         }
1394
1395         /* note: no bus_unlock here. unlock is in framedone_cb */
1396         mutex_unlock(&td->lock);
1397         return 0;
1398 err:
1399         dsi_bus_unlock(dssdev);
1400         mutex_unlock(&td->lock);
1401         return r;
1402 }
1403
1404 static int taal_sync(struct omap_dss_device *dssdev)
1405 {
1406         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1407
1408         dev_dbg(&dssdev->dev, "sync\n");
1409
1410         mutex_lock(&td->lock);
1411         dsi_bus_lock(dssdev);
1412         dsi_bus_unlock(dssdev);
1413         mutex_unlock(&td->lock);
1414
1415         dev_dbg(&dssdev->dev, "sync done\n");
1416
1417         return 0;
1418 }
1419
1420 static int _taal_enable_te(struct omap_dss_device *dssdev, bool enable)
1421 {
1422         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1423         struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1424         int r;
1425
1426         if (enable)
1427                 r = taal_dcs_write_1(td, MIPI_DCS_SET_TEAR_ON, 0);
1428         else
1429                 r = taal_dcs_write_0(td, MIPI_DCS_SET_TEAR_OFF);
1430
1431         if (!panel_data->use_ext_te)
1432                 omapdss_dsi_enable_te(dssdev, enable);
1433
1434         if (td->panel_config->sleep.enable_te)
1435                 msleep(td->panel_config->sleep.enable_te);
1436
1437         return r;
1438 }
1439
1440 static int taal_enable_te(struct omap_dss_device *dssdev, bool enable)
1441 {
1442         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1443         int r;
1444
1445         mutex_lock(&td->lock);
1446
1447         if (td->te_enabled == enable)
1448                 goto end;
1449
1450         dsi_bus_lock(dssdev);
1451
1452         if (td->enabled) {
1453                 r = taal_wake_up(dssdev);
1454                 if (r)
1455                         goto err;
1456
1457                 r = _taal_enable_te(dssdev, enable);
1458                 if (r)
1459                         goto err;
1460         }
1461
1462         td->te_enabled = enable;
1463
1464         dsi_bus_unlock(dssdev);
1465 end:
1466         mutex_unlock(&td->lock);
1467
1468         return 0;
1469 err:
1470         dsi_bus_unlock(dssdev);
1471         mutex_unlock(&td->lock);
1472
1473         return r;
1474 }
1475
1476 static int taal_get_te(struct omap_dss_device *dssdev)
1477 {
1478         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1479         int r;
1480
1481         mutex_lock(&td->lock);
1482         r = td->te_enabled;
1483         mutex_unlock(&td->lock);
1484
1485         return r;
1486 }
1487
1488 static int taal_rotate(struct omap_dss_device *dssdev, u8 rotate)
1489 {
1490         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1491         int r;
1492
1493         dev_dbg(&dssdev->dev, "rotate %d\n", rotate);
1494
1495         mutex_lock(&td->lock);
1496
1497         if (td->rotate == rotate)
1498                 goto end;
1499
1500         dsi_bus_lock(dssdev);
1501
1502         if (td->enabled) {
1503                 r = taal_wake_up(dssdev);
1504                 if (r)
1505                         goto err;
1506
1507                 r = taal_set_addr_mode(td, rotate, td->mirror);
1508                 if (r)
1509                         goto err;
1510         }
1511
1512         td->rotate = rotate;
1513
1514         dsi_bus_unlock(dssdev);
1515 end:
1516         mutex_unlock(&td->lock);
1517         return 0;
1518 err:
1519         dsi_bus_unlock(dssdev);
1520         mutex_unlock(&td->lock);
1521         return r;
1522 }
1523
1524 static u8 taal_get_rotate(struct omap_dss_device *dssdev)
1525 {
1526         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1527         int r;
1528
1529         mutex_lock(&td->lock);
1530         r = td->rotate;
1531         mutex_unlock(&td->lock);
1532
1533         return r;
1534 }
1535
1536 static int taal_mirror(struct omap_dss_device *dssdev, bool enable)
1537 {
1538         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1539         int r;
1540
1541         dev_dbg(&dssdev->dev, "mirror %d\n", enable);
1542
1543         mutex_lock(&td->lock);
1544
1545         if (td->mirror == enable)
1546                 goto end;
1547
1548         dsi_bus_lock(dssdev);
1549         if (td->enabled) {
1550                 r = taal_wake_up(dssdev);
1551                 if (r)
1552                         goto err;
1553
1554                 r = taal_set_addr_mode(td, td->rotate, enable);
1555                 if (r)
1556                         goto err;
1557         }
1558
1559         td->mirror = enable;
1560
1561         dsi_bus_unlock(dssdev);
1562 end:
1563         mutex_unlock(&td->lock);
1564         return 0;
1565 err:
1566         dsi_bus_unlock(dssdev);
1567         mutex_unlock(&td->lock);
1568         return r;
1569 }
1570
1571 static bool taal_get_mirror(struct omap_dss_device *dssdev)
1572 {
1573         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1574         int r;
1575
1576         mutex_lock(&td->lock);
1577         r = td->mirror;
1578         mutex_unlock(&td->lock);
1579
1580         return r;
1581 }
1582
1583 static int taal_run_test(struct omap_dss_device *dssdev, int test_num)
1584 {
1585         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1586         u8 id1, id2, id3;
1587         int r;
1588
1589         mutex_lock(&td->lock);
1590
1591         if (!td->enabled) {
1592                 r = -ENODEV;
1593                 goto err1;
1594         }
1595
1596         dsi_bus_lock(dssdev);
1597
1598         r = taal_wake_up(dssdev);
1599         if (r)
1600                 goto err2;
1601
1602         r = taal_dcs_read_1(td, DCS_GET_ID1, &id1);
1603         if (r)
1604                 goto err2;
1605         r = taal_dcs_read_1(td, DCS_GET_ID2, &id2);
1606         if (r)
1607                 goto err2;
1608         r = taal_dcs_read_1(td, DCS_GET_ID3, &id3);
1609         if (r)
1610                 goto err2;
1611
1612         dsi_bus_unlock(dssdev);
1613         mutex_unlock(&td->lock);
1614         return 0;
1615 err2:
1616         dsi_bus_unlock(dssdev);
1617 err1:
1618         mutex_unlock(&td->lock);
1619         return r;
1620 }
1621
1622 static int taal_memory_read(struct omap_dss_device *dssdev,
1623                 void *buf, size_t size,
1624                 u16 x, u16 y, u16 w, u16 h)
1625 {
1626         int r;
1627         int first = 1;
1628         int plen;
1629         unsigned buf_used = 0;
1630         struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1631
1632         if (size < w * h * 3)
1633                 return -ENOMEM;
1634
1635         mutex_lock(&td->lock);
1636
1637         if (!td->enabled) {
1638                 r = -ENODEV;
1639                 goto err1;
1640         }
1641
1642         size = min(w * h * 3,
1643                         dssdev->panel.timings.x_res *
1644                         dssdev->panel.timings.y_res * 3);
1645
1646         dsi_bus_lock(dssdev);
1647
1648         r = taal_wake_up(dssdev);
1649         if (r)
1650                 goto err2;
1651
1652         /* plen 1 or 2 goes into short packet. until checksum error is fixed,
1653          * use short packets. plen 32 works, but bigger packets seem to cause
1654          * an error. */
1655         if (size % 2)
1656                 plen = 1;
1657         else
1658                 plen = 2;
1659
1660         taal_set_update_window(td, x, y, w, h);
1661
1662         r = dsi_vc_set_max_rx_packet_size(dssdev, td->channel, plen);
1663         if (r)
1664                 goto err2;
1665
1666         while (buf_used < size) {
1667                 u8 dcs_cmd = first ? 0x2e : 0x3e;
1668                 first = 0;
1669
1670                 r = dsi_vc_dcs_read(dssdev, td->channel, dcs_cmd,
1671                                 buf + buf_used, size - buf_used);
1672
1673                 if (r < 0) {
1674                         dev_err(&dssdev->dev, "read error\n");
1675                         goto err3;
1676                 }
1677
1678                 buf_used += r;
1679
1680                 if (r < plen) {
1681                         dev_err(&dssdev->dev, "short read\n");
1682                         break;
1683                 }
1684
1685                 if (signal_pending(current)) {
1686                         dev_err(&dssdev->dev, "signal pending, "
1687                                         "aborting memory read\n");
1688                         r = -ERESTARTSYS;
1689                         goto err3;
1690                 }
1691         }
1692
1693         r = buf_used;
1694
1695 err3:
1696         dsi_vc_set_max_rx_packet_size(dssdev, td->channel, 1);
1697 err2:
1698         dsi_bus_unlock(dssdev);
1699 err1:
1700         mutex_unlock(&td->lock);
1701         return r;
1702 }
1703
1704 static void taal_ulps_work(struct work_struct *work)
1705 {
1706         struct taal_data *td = container_of(work, struct taal_data,
1707                         ulps_work.work);
1708         struct omap_dss_device *dssdev = td->dssdev;
1709
1710         mutex_lock(&td->lock);
1711
1712         if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !td->enabled) {
1713                 mutex_unlock(&td->lock);
1714                 return;
1715         }
1716
1717         dsi_bus_lock(dssdev);
1718
1719         taal_enter_ulps(dssdev);
1720
1721         dsi_bus_unlock(dssdev);
1722         mutex_unlock(&td->lock);
1723 }
1724
1725 static void taal_esd_work(struct work_struct *work)
1726 {
1727         struct taal_data *td = container_of(work, struct taal_data,
1728                         esd_work.work);
1729         struct omap_dss_device *dssdev = td->dssdev;
1730         struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1731         u8 state1, state2;
1732         int r;
1733
1734         mutex_lock(&td->lock);
1735
1736         if (!td->enabled) {
1737                 mutex_unlock(&td->lock);
1738                 return;
1739         }
1740
1741         dsi_bus_lock(dssdev);
1742
1743         r = taal_wake_up(dssdev);
1744         if (r) {
1745                 dev_err(&dssdev->dev, "failed to exit ULPS\n");
1746                 goto err;
1747         }
1748
1749         r = taal_dcs_read_1(td, MIPI_DCS_GET_DIAGNOSTIC_RESULT, &state1);
1750         if (r) {
1751                 dev_err(&dssdev->dev, "failed to read Taal status\n");
1752                 goto err;
1753         }
1754
1755         /* Run self diagnostics */
1756         r = taal_sleep_out(td);
1757         if (r) {
1758                 dev_err(&dssdev->dev, "failed to run Taal self-diagnostics\n");
1759                 goto err;
1760         }
1761
1762         r = taal_dcs_read_1(td, MIPI_DCS_GET_DIAGNOSTIC_RESULT, &state2);
1763         if (r) {
1764                 dev_err(&dssdev->dev, "failed to read Taal status\n");
1765                 goto err;
1766         }
1767
1768         /* Each sleep out command will trigger a self diagnostic and flip
1769          * Bit6 if the test passes.
1770          */
1771         if (!((state1 ^ state2) & (1 << 6))) {
1772                 dev_err(&dssdev->dev, "LCD self diagnostics failed\n");
1773                 goto err;
1774         }
1775         /* Self-diagnostics result is also shown on TE GPIO line. We need
1776          * to re-enable TE after self diagnostics */
1777         if (td->te_enabled && panel_data->use_ext_te) {
1778                 r = taal_dcs_write_1(td, MIPI_DCS_SET_TEAR_ON, 0);
1779                 if (r)
1780                         goto err;
1781         }
1782
1783         dsi_bus_unlock(dssdev);
1784
1785         taal_queue_esd_work(dssdev);
1786
1787         mutex_unlock(&td->lock);
1788         return;
1789 err:
1790         dev_err(&dssdev->dev, "performing LCD reset\n");
1791
1792         taal_panel_reset(dssdev);
1793
1794         dsi_bus_unlock(dssdev);
1795
1796         taal_queue_esd_work(dssdev);
1797
1798         mutex_unlock(&td->lock);
1799 }
1800
1801 static struct omap_dss_driver taal_driver = {
1802         .probe          = taal_probe,
1803         .remove         = __exit_p(taal_remove),
1804
1805         .enable         = taal_enable,
1806         .disable        = taal_disable,
1807         .suspend        = taal_suspend,
1808         .resume         = taal_resume,
1809
1810         .update         = taal_update,
1811         .sync           = taal_sync,
1812
1813         .get_resolution = taal_get_resolution,
1814         .get_recommended_bpp = omapdss_default_get_recommended_bpp,
1815
1816         .enable_te      = taal_enable_te,
1817         .get_te         = taal_get_te,
1818
1819         .set_rotate     = taal_rotate,
1820         .get_rotate     = taal_get_rotate,
1821         .set_mirror     = taal_mirror,
1822         .get_mirror     = taal_get_mirror,
1823         .run_test       = taal_run_test,
1824         .memory_read    = taal_memory_read,
1825
1826         .driver         = {
1827                 .name   = "taal",
1828                 .owner  = THIS_MODULE,
1829         },
1830 };
1831
1832 static int __init taal_init(void)
1833 {
1834         omap_dss_register_driver(&taal_driver);
1835
1836         return 0;
1837 }
1838
1839 static void __exit taal_exit(void)
1840 {
1841         omap_dss_unregister_driver(&taal_driver);
1842 }
1843
1844 module_init(taal_init);
1845 module_exit(taal_exit);
1846
1847 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
1848 MODULE_DESCRIPTION("Taal Driver");
1849 MODULE_LICENSE("GPL");