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