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