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