OMAPDSS: TPO-TD03MTEA1: add vdd regulator
[pandora-kernel.git] / drivers / video / omap2 / displays / panel-tpo-td043mtea1.c
1 /*
2  * LCD panel driver for TPO TD043MTEA1
3  *
4  * Author: Gražvydas Ignotas <notasas@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/spi/spi.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/gpio.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19
20 #include <video/omapdss.h>
21
22 #define TPO_R02_MODE(x)         ((x) & 7)
23 #define TPO_R02_MODE_800x480    7
24 #define TPO_R02_NCLK_RISING     BIT(3)
25 #define TPO_R02_HSYNC_HIGH      BIT(4)
26 #define TPO_R02_VSYNC_HIGH      BIT(5)
27
28 #define TPO_R03_NSTANDBY        BIT(0)
29 #define TPO_R03_EN_CP_CLK       BIT(1)
30 #define TPO_R03_EN_VGL_PUMP     BIT(2)
31 #define TPO_R03_EN_PWM          BIT(3)
32 #define TPO_R03_DRIVING_CAP_100 BIT(4)
33 #define TPO_R03_EN_PRE_CHARGE   BIT(6)
34 #define TPO_R03_SOFTWARE_CTL    BIT(7)
35
36 #define TPO_R04_NFLIP_H         BIT(0)
37 #define TPO_R04_NFLIP_V         BIT(1)
38 #define TPO_R04_CP_CLK_FREQ_1H  BIT(2)
39 #define TPO_R04_VGL_FREQ_1H     BIT(4)
40
41 #define TPO_R03_VAL_NORMAL (TPO_R03_NSTANDBY | TPO_R03_EN_CP_CLK | \
42                         TPO_R03_EN_VGL_PUMP |  TPO_R03_EN_PWM | \
43                         TPO_R03_DRIVING_CAP_100 | TPO_R03_EN_PRE_CHARGE | \
44                         TPO_R03_SOFTWARE_CTL)
45
46 #define TPO_R03_VAL_STANDBY (TPO_R03_DRIVING_CAP_100 | \
47                         TPO_R03_EN_PRE_CHARGE | TPO_R03_SOFTWARE_CTL)
48
49 static const u16 tpo_td043_def_gamma[12] = {
50         105, 315, 381, 431, 490, 537, 579, 686, 780, 837, 880, 1023
51 };
52
53 struct tpo_td043_device {
54         struct spi_device *spi;
55         struct regulator *vcc_reg;
56         struct regulator *vdd_reg;
57         int nreset_gpio;
58         u16 gamma[12];
59         u32 mode;
60         u32 hmirror:1;
61         u32 vmirror:1;
62         u32 powered_on:1;
63         u32 spi_suspended:1;
64         u32 power_on_resume:1;
65 };
66
67 static int tpo_td043_write(struct spi_device *spi, u8 addr, u8 data)
68 {
69         struct spi_message      m;
70         struct spi_transfer     xfer;
71         u16                     w;
72         int                     r;
73
74         spi_message_init(&m);
75
76         memset(&xfer, 0, sizeof(xfer));
77
78         w = ((u16)addr << 10) | (1 << 8) | data;
79         xfer.tx_buf = &w;
80         xfer.bits_per_word = 16;
81         xfer.len = 2;
82         spi_message_add_tail(&xfer, &m);
83
84         r = spi_sync(spi, &m);
85         if (r < 0)
86                 dev_warn(&spi->dev, "failed to write to LCD reg (%d)\n", r);
87         return r;
88 }
89
90 static void tpo_td043_write_gamma(struct spi_device *spi, u16 gamma[12])
91 {
92         u8 i, val;
93
94         /* gamma bits [9:8] */
95         for (val = i = 0; i < 4; i++)
96                 val |= (gamma[i] & 0x300) >> ((i + 1) * 2);
97         tpo_td043_write(spi, 0x11, val);
98
99         for (val = i = 0; i < 4; i++)
100                 val |= (gamma[i+4] & 0x300) >> ((i + 1) * 2);
101         tpo_td043_write(spi, 0x12, val);
102
103         for (val = i = 0; i < 4; i++)
104                 val |= (gamma[i+8] & 0x300) >> ((i + 1) * 2);
105         tpo_td043_write(spi, 0x13, val);
106
107         /* gamma bits [7:0] */
108         for (val = i = 0; i < 12; i++)
109                 tpo_td043_write(spi, 0x14 + i, gamma[i] & 0xff);
110 }
111
112 static int tpo_td043_write_mirror(struct spi_device *spi, bool h, bool v)
113 {
114         u8 reg4 = TPO_R04_NFLIP_H | TPO_R04_NFLIP_V | \
115                 TPO_R04_CP_CLK_FREQ_1H | TPO_R04_VGL_FREQ_1H;
116         if (h)
117                 reg4 &= ~TPO_R04_NFLIP_H;
118         if (v)
119                 reg4 &= ~TPO_R04_NFLIP_V;
120
121         return tpo_td043_write(spi, 4, reg4);
122 }
123
124 static int tpo_td043_set_hmirror(struct omap_dss_device *dssdev, bool enable)
125 {
126         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&dssdev->dev);
127
128         tpo_td043->hmirror = enable;
129         return tpo_td043_write_mirror(tpo_td043->spi, tpo_td043->hmirror,
130                         tpo_td043->vmirror);
131 }
132
133 static bool tpo_td043_get_hmirror(struct omap_dss_device *dssdev)
134 {
135         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&dssdev->dev);
136
137         return tpo_td043->hmirror;
138 }
139
140 static ssize_t tpo_td043_vmirror_show(struct device *dev,
141         struct device_attribute *attr, char *buf)
142 {
143         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
144
145         return snprintf(buf, PAGE_SIZE, "%d\n", tpo_td043->vmirror);
146 }
147
148 static ssize_t tpo_td043_vmirror_store(struct device *dev,
149         struct device_attribute *attr, const char *buf, size_t count)
150 {
151         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
152         int val;
153         int ret;
154
155         ret = kstrtoint(buf, 0, &val);
156         if (ret < 0)
157                 return ret;
158
159         val = !!val;
160
161         ret = tpo_td043_write_mirror(tpo_td043->spi, tpo_td043->hmirror, val);
162         if (ret < 0)
163                 return ret;
164
165         tpo_td043->vmirror = val;
166
167         return count;
168 }
169
170 static ssize_t tpo_td043_mode_show(struct device *dev,
171         struct device_attribute *attr, char *buf)
172 {
173         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
174
175         return snprintf(buf, PAGE_SIZE, "%d\n", tpo_td043->mode);
176 }
177
178 static ssize_t tpo_td043_mode_store(struct device *dev,
179         struct device_attribute *attr, const char *buf, size_t count)
180 {
181         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
182         long val;
183         int ret;
184
185         ret = kstrtol(buf, 0, &val);
186         if (ret != 0 || val & ~7)
187                 return -EINVAL;
188
189         tpo_td043->mode = val;
190
191         val |= TPO_R02_NCLK_RISING;
192         tpo_td043_write(tpo_td043->spi, 2, val);
193
194         return count;
195 }
196
197 static ssize_t tpo_td043_gamma_show(struct device *dev,
198         struct device_attribute *attr, char *buf)
199 {
200         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
201         ssize_t len = 0;
202         int ret;
203         int i;
204
205         for (i = 0; i < ARRAY_SIZE(tpo_td043->gamma); i++) {
206                 ret = snprintf(buf + len, PAGE_SIZE - len, "%u ",
207                                 tpo_td043->gamma[i]);
208                 if (ret < 0)
209                         return ret;
210                 len += ret;
211         }
212         buf[len - 1] = '\n';
213
214         return len;
215 }
216
217 static ssize_t tpo_td043_gamma_store(struct device *dev,
218         struct device_attribute *attr, const char *buf, size_t count)
219 {
220         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
221         unsigned int g[12];
222         int ret;
223         int i;
224
225         ret = sscanf(buf, "%u %u %u %u %u %u %u %u %u %u %u %u",
226                         &g[0], &g[1], &g[2], &g[3], &g[4], &g[5],
227                         &g[6], &g[7], &g[8], &g[9], &g[10], &g[11]);
228
229         if (ret != 12)
230                 return -EINVAL;
231
232         for (i = 0; i < 12; i++)
233                 tpo_td043->gamma[i] = g[i];
234
235         tpo_td043_write_gamma(tpo_td043->spi, tpo_td043->gamma);
236
237         return count;
238 }
239
240 static DEVICE_ATTR(vmirror, S_IRUGO | S_IWUSR,
241                 tpo_td043_vmirror_show, tpo_td043_vmirror_store);
242 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
243                 tpo_td043_mode_show, tpo_td043_mode_store);
244 static DEVICE_ATTR(gamma, S_IRUGO | S_IWUSR,
245                 tpo_td043_gamma_show, tpo_td043_gamma_store);
246
247 static struct attribute *tpo_td043_attrs[] = {
248         &dev_attr_vmirror.attr,
249         &dev_attr_mode.attr,
250         &dev_attr_gamma.attr,
251         NULL,
252 };
253
254 static struct attribute_group tpo_td043_attr_group = {
255         .attrs = tpo_td043_attrs,
256 };
257
258 static const struct omap_video_timings tpo_td043_timings = {
259         .x_res          = 800,
260         .y_res          = 480,
261
262         .pixel_clock    = 36000,
263
264         /* note:
265          * hbp+hsw must be 215
266          * if vbp+vsw < 32, the panel tears at the bottom
267          * if vbp+vsw > 35, it wraps from the top */
268         .hsw            = 1,
269         .hfp            = 150,
270         .hbp            = 214,
271
272         .vsw            = 1,
273         .vfp            = 0,
274         .vbp            = 34,
275 };
276
277 static int tpo_td043_power_on(struct tpo_td043_device *tpo_td043)
278 {
279         int nreset_gpio = tpo_td043->nreset_gpio;
280
281         if (tpo_td043->powered_on)
282                 return 0;
283
284         regulator_enable(tpo_td043->vcc_reg);
285         regulator_enable(tpo_td043->vdd_reg);
286
287         /* wait for regulator to stabilize */
288         msleep(160);
289
290         if (gpio_is_valid(nreset_gpio))
291                 gpio_set_value(nreset_gpio, 1);
292
293         tpo_td043_write(tpo_td043->spi, 2,
294                         TPO_R02_MODE(tpo_td043->mode) | TPO_R02_NCLK_RISING);
295         tpo_td043_write(tpo_td043->spi, 3, TPO_R03_VAL_NORMAL);
296         tpo_td043_write(tpo_td043->spi, 0x20, 0xf0);
297         tpo_td043_write(tpo_td043->spi, 0x21, 0xf0);
298         tpo_td043_write_mirror(tpo_td043->spi, tpo_td043->hmirror,
299                         tpo_td043->vmirror);
300         tpo_td043_write_gamma(tpo_td043->spi, tpo_td043->gamma);
301
302         tpo_td043->powered_on = 1;
303         return 0;
304 }
305
306 static void tpo_td043_power_off(struct tpo_td043_device *tpo_td043)
307 {
308         int nreset_gpio = tpo_td043->nreset_gpio;
309
310         if (!tpo_td043->powered_on)
311                 return;
312
313         tpo_td043_write(tpo_td043->spi, 3,
314                         TPO_R03_VAL_STANDBY | TPO_R03_EN_PWM);
315
316         if (gpio_is_valid(nreset_gpio))
317                 gpio_set_value(nreset_gpio, 0);
318
319         /* wait for at least 2 vsyncs before cutting off power */
320         msleep(50);
321
322         tpo_td043_write(tpo_td043->spi, 3, TPO_R03_VAL_STANDBY);
323
324         regulator_disable(tpo_td043->vdd_reg);
325         regulator_disable(tpo_td043->vcc_reg);
326
327         tpo_td043->powered_on = 0;
328 }
329
330 static int tpo_td043_enable_dss(struct omap_dss_device *dssdev)
331 {
332         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&dssdev->dev);
333         int r;
334
335         if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
336                 return 0;
337
338         r = omapdss_dpi_display_enable(dssdev);
339         if (r)
340                 goto err0;
341
342         if (dssdev->platform_enable) {
343                 r = dssdev->platform_enable(dssdev);
344                 if (r)
345                         goto err1;
346         }
347
348         /* 
349          * If we are resuming from system suspend, SPI clocks might not be
350          * enabled yet, so we'll program the LCD from SPI PM resume callback.
351          */
352         if (!tpo_td043->spi_suspended) {
353                 r = tpo_td043_power_on(tpo_td043);
354                 if (r)
355                         goto err1;
356         }
357
358         dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
359
360         return 0;
361 err1:
362         omapdss_dpi_display_disable(dssdev);
363 err0:
364         return r;
365 }
366
367 static void tpo_td043_disable_dss(struct omap_dss_device *dssdev)
368 {
369         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&dssdev->dev);
370
371         if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
372                 return;
373
374         if (dssdev->platform_disable)
375                 dssdev->platform_disable(dssdev);
376
377         omapdss_dpi_display_disable(dssdev);
378
379         if (!tpo_td043->spi_suspended)
380                 tpo_td043_power_off(tpo_td043);
381 }
382
383 static int tpo_td043_enable(struct omap_dss_device *dssdev)
384 {
385         dev_dbg(&dssdev->dev, "enable\n");
386
387         return tpo_td043_enable_dss(dssdev);
388 }
389
390 static void tpo_td043_disable(struct omap_dss_device *dssdev)
391 {
392         dev_dbg(&dssdev->dev, "disable\n");
393
394         tpo_td043_disable_dss(dssdev);
395
396         dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
397 }
398
399 static int tpo_td043_suspend(struct omap_dss_device *dssdev)
400 {
401         dev_dbg(&dssdev->dev, "suspend\n");
402
403         tpo_td043_disable_dss(dssdev);
404
405         dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
406
407         return 0;
408 }
409
410 static int tpo_td043_resume(struct omap_dss_device *dssdev)
411 {
412         dev_dbg(&dssdev->dev, "resume\n");
413
414         return tpo_td043_enable_dss(dssdev);
415 }
416
417 static int tpo_td043_probe(struct omap_dss_device *dssdev)
418 {
419         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&dssdev->dev);
420         int nreset_gpio = dssdev->reset_gpio;
421         int ret = 0;
422
423         dev_dbg(&dssdev->dev, "probe\n");
424
425         if (tpo_td043 == NULL) {
426                 dev_err(&dssdev->dev, "missing tpo_td043_device\n");
427                 return -ENODEV;
428         }
429
430         dssdev->panel.config = OMAP_DSS_LCD_TFT | OMAP_DSS_LCD_IHS |
431                                 OMAP_DSS_LCD_IVS | OMAP_DSS_LCD_IPC;
432         dssdev->panel.timings = tpo_td043_timings;
433         dssdev->ctrl.pixel_size = 24;
434
435         tpo_td043->mode = TPO_R02_MODE_800x480;
436         memcpy(tpo_td043->gamma, tpo_td043_def_gamma, sizeof(tpo_td043->gamma));
437
438         tpo_td043->vcc_reg = regulator_get(&dssdev->dev, "vcc");
439         if (IS_ERR(tpo_td043->vcc_reg)) {
440                 dev_err(&dssdev->dev, "failed to get LCD VCC regulator\n");
441                 ret = PTR_ERR(tpo_td043->vcc_reg);
442                 goto fail_regulator;
443         }
444
445         tpo_td043->vdd_reg = regulator_get(&dssdev->dev, "vdd");
446         if (IS_ERR(tpo_td043->vdd_reg)) {
447                 dev_err(&dssdev->dev, "failed to get LCD VDD regulator\n");
448                 ret = PTR_ERR(tpo_td043->vdd_reg);
449                 goto fail_vdd_regulator;
450         }
451
452         if (gpio_is_valid(nreset_gpio)) {
453                 ret = gpio_request(nreset_gpio, "lcd reset");
454                 if (ret < 0) {
455                         dev_err(&dssdev->dev, "couldn't request reset GPIO\n");
456                         goto fail_gpio_req;
457                 }
458
459                 ret = gpio_direction_output(nreset_gpio, 0);
460                 if (ret < 0) {
461                         dev_err(&dssdev->dev, "couldn't set GPIO direction\n");
462                         goto fail_gpio_direction;
463                 }
464         }
465
466         ret = sysfs_create_group(&dssdev->dev.kobj, &tpo_td043_attr_group);
467         if (ret)
468                 dev_warn(&dssdev->dev, "failed to create sysfs files\n");
469
470         return 0;
471
472 fail_gpio_direction:
473         gpio_free(nreset_gpio);
474 fail_gpio_req:
475         regulator_put(tpo_td043->vdd_reg);
476 fail_vdd_regulator:
477         regulator_put(tpo_td043->vcc_reg);
478 fail_regulator:
479         kfree(tpo_td043);
480         return ret;
481 }
482
483 static void tpo_td043_remove(struct omap_dss_device *dssdev)
484 {
485         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&dssdev->dev);
486         int nreset_gpio = dssdev->reset_gpio;
487
488         dev_dbg(&dssdev->dev, "remove\n");
489
490         sysfs_remove_group(&dssdev->dev.kobj, &tpo_td043_attr_group);
491         regulator_put(tpo_td043->vdd_reg);
492         regulator_put(tpo_td043->vcc_reg);
493         if (gpio_is_valid(nreset_gpio))
494                 gpio_free(nreset_gpio);
495 }
496
497 static void tpo_td043_set_timings(struct omap_dss_device *dssdev,
498                 struct omap_video_timings *timings)
499 {
500         dpi_set_timings(dssdev, timings);
501 }
502
503 static int tpo_td043_check_timings(struct omap_dss_device *dssdev,
504                 struct omap_video_timings *timings)
505 {
506         return dpi_check_timings(dssdev, timings);
507 }
508
509 static struct omap_dss_driver tpo_td043_driver = {
510         .probe          = tpo_td043_probe,
511         .remove         = tpo_td043_remove,
512
513         .enable         = tpo_td043_enable,
514         .disable        = tpo_td043_disable,
515         .suspend        = tpo_td043_suspend,
516         .resume         = tpo_td043_resume,
517         .set_mirror     = tpo_td043_set_hmirror,
518         .get_mirror     = tpo_td043_get_hmirror,
519
520         .set_timings    = tpo_td043_set_timings,
521         .check_timings  = tpo_td043_check_timings,
522
523         .driver         = {
524                 .name   = "tpo_td043mtea1_panel",
525                 .owner  = THIS_MODULE,
526         },
527 };
528
529 static int tpo_td043_spi_probe(struct spi_device *spi)
530 {
531         struct omap_dss_device *dssdev = spi->dev.platform_data;
532         struct tpo_td043_device *tpo_td043;
533         int ret;
534
535         if (dssdev == NULL) {
536                 dev_err(&spi->dev, "missing dssdev\n");
537                 return -ENODEV;
538         }
539
540         spi->bits_per_word = 16;
541         spi->mode = SPI_MODE_0;
542
543         ret = spi_setup(spi);
544         if (ret < 0) {
545                 dev_err(&spi->dev, "spi_setup failed: %d\n", ret);
546                 return ret;
547         }
548
549         tpo_td043 = kzalloc(sizeof(*tpo_td043), GFP_KERNEL);
550         if (tpo_td043 == NULL)
551                 return -ENOMEM;
552
553         tpo_td043->spi = spi;
554         tpo_td043->nreset_gpio = dssdev->reset_gpio;
555         dev_set_drvdata(&spi->dev, tpo_td043);
556         dev_set_drvdata(&dssdev->dev, tpo_td043);
557
558         omap_dss_register_driver(&tpo_td043_driver);
559
560         return 0;
561 }
562
563 static int __devexit tpo_td043_spi_remove(struct spi_device *spi)
564 {
565         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&spi->dev);
566
567         omap_dss_unregister_driver(&tpo_td043_driver);
568         kfree(tpo_td043);
569
570         return 0;
571 }
572
573 #ifdef CONFIG_PM_SLEEP
574 static int tpo_td043_spi_suspend(struct device *dev)
575 {
576         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
577
578         dev_dbg(dev, "tpo_td043_spi_suspend, tpo %p\n", tpo_td043);
579
580         tpo_td043->power_on_resume = tpo_td043->powered_on;
581         tpo_td043_power_off(tpo_td043);
582         tpo_td043->spi_suspended = 1;
583
584         return 0;
585 }
586
587 static int tpo_td043_spi_resume(struct device *dev)
588 {
589         struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
590         int ret;
591
592         dev_dbg(dev, "tpo_td043_spi_resume\n");
593
594         if (tpo_td043->power_on_resume) {
595                 ret = tpo_td043_power_on(tpo_td043);
596                 if (ret)
597                         return ret;
598         }
599         tpo_td043->spi_suspended = 0;
600
601         return 0;
602 }
603 #endif
604
605 static SIMPLE_DEV_PM_OPS(tpo_td043_spi_pm,
606         tpo_td043_spi_suspend, tpo_td043_spi_resume);
607
608 static struct spi_driver tpo_td043_spi_driver = {
609         .driver = {
610                 .name   = "tpo_td043mtea1_panel_spi",
611                 .bus    = &spi_bus_type,
612                 .owner  = THIS_MODULE,
613                 .pm     = &tpo_td043_spi_pm,
614         },
615         .probe  = tpo_td043_spi_probe,
616         .remove = __devexit_p(tpo_td043_spi_remove),
617 };
618
619 static int __init tpo_td043_init(void)
620 {
621         return spi_register_driver(&tpo_td043_spi_driver);
622 }
623
624 static void __exit tpo_td043_exit(void)
625 {
626         spi_unregister_driver(&tpo_td043_spi_driver);
627 }
628
629 module_init(tpo_td043_init);
630 module_exit(tpo_td043_exit);
631
632 MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
633 MODULE_DESCRIPTION("TPO TD043MTEA1 LCD Driver");
634 MODULE_LICENSE("GPL");