2 * LCD panel driver for TPO TD043MTEA1
4 * Author: Gražvydas Ignotas <notasas@gmail.com>
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.
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>
20 #include <video/omapdss.h>
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)
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)
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)
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 | \
46 #define TPO_R03_VAL_STANDBY (TPO_R03_DRIVING_CAP_100 | \
47 TPO_R03_EN_PRE_CHARGE | TPO_R03_SOFTWARE_CTL)
49 static const u16 tpo_td043_def_gamma[12] = {
50 105, 315, 381, 431, 490, 537, 579, 686, 780, 837, 880, 1023
53 struct tpo_td043_device {
54 struct spi_device *spi;
55 struct regulator *vcc_reg;
56 struct regulator *vdd_reg;
64 u32 power_on_resume:1;
67 static int tpo_td043_write(struct spi_device *spi, u8 addr, u8 data)
70 struct spi_transfer xfer;
76 memset(&xfer, 0, sizeof(xfer));
78 w = ((u16)addr << 10) | (1 << 8) | data;
80 xfer.bits_per_word = 16;
82 spi_message_add_tail(&xfer, &m);
84 r = spi_sync(spi, &m);
86 dev_warn(&spi->dev, "failed to write to LCD reg (%d)\n", r);
90 static void tpo_td043_write_gamma(struct spi_device *spi, u16 gamma[12])
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);
99 for (val = i = 0; i < 4; i++)
100 val |= (gamma[i+4] & 0x300) >> ((i + 1) * 2);
101 tpo_td043_write(spi, 0x12, val);
103 for (val = i = 0; i < 4; i++)
104 val |= (gamma[i+8] & 0x300) >> ((i + 1) * 2);
105 tpo_td043_write(spi, 0x13, val);
107 /* gamma bits [7:0] */
108 for (val = i = 0; i < 12; i++)
109 tpo_td043_write(spi, 0x14 + i, gamma[i] & 0xff);
112 static int tpo_td043_write_mirror(struct spi_device *spi, bool h, bool v)
114 u8 reg4 = TPO_R04_NFLIP_H | TPO_R04_NFLIP_V | \
115 TPO_R04_CP_CLK_FREQ_1H | TPO_R04_VGL_FREQ_1H;
117 reg4 &= ~TPO_R04_NFLIP_H;
119 reg4 &= ~TPO_R04_NFLIP_V;
121 return tpo_td043_write(spi, 4, reg4);
124 static int tpo_td043_set_hmirror(struct omap_dss_device *dssdev, bool enable)
126 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&dssdev->dev);
128 tpo_td043->hmirror = enable;
129 return tpo_td043_write_mirror(tpo_td043->spi, tpo_td043->hmirror,
133 static bool tpo_td043_get_hmirror(struct omap_dss_device *dssdev)
135 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&dssdev->dev);
137 return tpo_td043->hmirror;
140 static ssize_t tpo_td043_vmirror_show(struct device *dev,
141 struct device_attribute *attr, char *buf)
143 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
145 return snprintf(buf, PAGE_SIZE, "%d\n", tpo_td043->vmirror);
148 static ssize_t tpo_td043_vmirror_store(struct device *dev,
149 struct device_attribute *attr, const char *buf, size_t count)
151 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
155 ret = kstrtoint(buf, 0, &val);
161 ret = tpo_td043_write_mirror(tpo_td043->spi, tpo_td043->hmirror, val);
165 tpo_td043->vmirror = val;
170 static ssize_t tpo_td043_mode_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
173 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
175 return snprintf(buf, PAGE_SIZE, "%d\n", tpo_td043->mode);
178 static ssize_t tpo_td043_mode_store(struct device *dev,
179 struct device_attribute *attr, const char *buf, size_t count)
181 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
185 ret = kstrtol(buf, 0, &val);
186 if (ret != 0 || val & ~7)
189 tpo_td043->mode = val;
191 val |= TPO_R02_NCLK_RISING;
192 tpo_td043_write(tpo_td043->spi, 2, val);
197 static ssize_t tpo_td043_gamma_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
200 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
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]);
217 static ssize_t tpo_td043_gamma_store(struct device *dev,
218 struct device_attribute *attr, const char *buf, size_t count)
220 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
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]);
232 for (i = 0; i < 12; i++)
233 tpo_td043->gamma[i] = g[i];
235 tpo_td043_write_gamma(tpo_td043->spi, tpo_td043->gamma);
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);
247 static struct attribute *tpo_td043_attrs[] = {
248 &dev_attr_vmirror.attr,
250 &dev_attr_gamma.attr,
254 static struct attribute_group tpo_td043_attr_group = {
255 .attrs = tpo_td043_attrs,
258 static const struct omap_video_timings tpo_td043_timings = {
262 .pixel_clock = 36000,
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 */
277 static int tpo_td043_power_on(struct tpo_td043_device *tpo_td043)
279 int nreset_gpio = tpo_td043->nreset_gpio;
281 if (tpo_td043->powered_on)
284 regulator_enable(tpo_td043->vcc_reg);
285 regulator_enable(tpo_td043->vdd_reg);
287 /* wait for regulator to stabilize */
290 if (gpio_is_valid(nreset_gpio))
291 gpio_set_value(nreset_gpio, 1);
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,
300 tpo_td043_write_gamma(tpo_td043->spi, tpo_td043->gamma);
302 tpo_td043->powered_on = 1;
306 static void tpo_td043_power_off(struct tpo_td043_device *tpo_td043)
308 int nreset_gpio = tpo_td043->nreset_gpio;
310 if (!tpo_td043->powered_on)
313 tpo_td043_write(tpo_td043->spi, 3,
314 TPO_R03_VAL_STANDBY | TPO_R03_EN_PWM);
316 if (gpio_is_valid(nreset_gpio))
317 gpio_set_value(nreset_gpio, 0);
319 /* wait for at least 2 vsyncs before cutting off power */
322 tpo_td043_write(tpo_td043->spi, 3, TPO_R03_VAL_STANDBY);
324 regulator_disable(tpo_td043->vdd_reg);
325 regulator_disable(tpo_td043->vcc_reg);
327 tpo_td043->powered_on = 0;
330 static int tpo_td043_enable_dss(struct omap_dss_device *dssdev)
332 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&dssdev->dev);
335 if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
338 r = omapdss_dpi_display_enable(dssdev);
342 if (dssdev->platform_enable) {
343 r = dssdev->platform_enable(dssdev);
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.
352 if (!tpo_td043->spi_suspended) {
353 r = tpo_td043_power_on(tpo_td043);
358 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
362 omapdss_dpi_display_disable(dssdev);
367 static void tpo_td043_disable_dss(struct omap_dss_device *dssdev)
369 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&dssdev->dev);
371 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
374 if (dssdev->platform_disable)
375 dssdev->platform_disable(dssdev);
377 omapdss_dpi_display_disable(dssdev);
379 if (!tpo_td043->spi_suspended)
380 tpo_td043_power_off(tpo_td043);
383 static int tpo_td043_enable(struct omap_dss_device *dssdev)
385 dev_dbg(&dssdev->dev, "enable\n");
387 return tpo_td043_enable_dss(dssdev);
390 static void tpo_td043_disable(struct omap_dss_device *dssdev)
392 dev_dbg(&dssdev->dev, "disable\n");
394 tpo_td043_disable_dss(dssdev);
396 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
399 static int tpo_td043_suspend(struct omap_dss_device *dssdev)
401 dev_dbg(&dssdev->dev, "suspend\n");
403 tpo_td043_disable_dss(dssdev);
405 dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
410 static int tpo_td043_resume(struct omap_dss_device *dssdev)
412 dev_dbg(&dssdev->dev, "resume\n");
414 return tpo_td043_enable_dss(dssdev);
417 static int tpo_td043_probe(struct omap_dss_device *dssdev)
419 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&dssdev->dev);
420 int nreset_gpio = dssdev->reset_gpio;
423 dev_dbg(&dssdev->dev, "probe\n");
425 if (tpo_td043 == NULL) {
426 dev_err(&dssdev->dev, "missing tpo_td043_device\n");
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;
435 tpo_td043->mode = TPO_R02_MODE_800x480;
436 memcpy(tpo_td043->gamma, tpo_td043_def_gamma, sizeof(tpo_td043->gamma));
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);
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;
452 if (gpio_is_valid(nreset_gpio)) {
453 ret = gpio_request(nreset_gpio, "lcd reset");
455 dev_err(&dssdev->dev, "couldn't request reset GPIO\n");
459 ret = gpio_direction_output(nreset_gpio, 0);
461 dev_err(&dssdev->dev, "couldn't set GPIO direction\n");
462 goto fail_gpio_direction;
466 ret = sysfs_create_group(&dssdev->dev.kobj, &tpo_td043_attr_group);
468 dev_warn(&dssdev->dev, "failed to create sysfs files\n");
473 gpio_free(nreset_gpio);
475 regulator_put(tpo_td043->vdd_reg);
477 regulator_put(tpo_td043->vcc_reg);
483 static void tpo_td043_remove(struct omap_dss_device *dssdev)
485 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&dssdev->dev);
486 int nreset_gpio = dssdev->reset_gpio;
488 dev_dbg(&dssdev->dev, "remove\n");
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);
497 static void tpo_td043_set_timings(struct omap_dss_device *dssdev,
498 struct omap_video_timings *timings)
500 dpi_set_timings(dssdev, timings);
503 static int tpo_td043_check_timings(struct omap_dss_device *dssdev,
504 struct omap_video_timings *timings)
506 return dpi_check_timings(dssdev, timings);
509 static struct omap_dss_driver tpo_td043_driver = {
510 .probe = tpo_td043_probe,
511 .remove = tpo_td043_remove,
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,
520 .set_timings = tpo_td043_set_timings,
521 .check_timings = tpo_td043_check_timings,
524 .name = "tpo_td043mtea1_panel",
525 .owner = THIS_MODULE,
529 static int tpo_td043_spi_probe(struct spi_device *spi)
531 struct omap_dss_device *dssdev = spi->dev.platform_data;
532 struct tpo_td043_device *tpo_td043;
535 if (dssdev == NULL) {
536 dev_err(&spi->dev, "missing dssdev\n");
540 spi->bits_per_word = 16;
541 spi->mode = SPI_MODE_0;
543 ret = spi_setup(spi);
545 dev_err(&spi->dev, "spi_setup failed: %d\n", ret);
549 tpo_td043 = kzalloc(sizeof(*tpo_td043), GFP_KERNEL);
550 if (tpo_td043 == NULL)
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);
558 omap_dss_register_driver(&tpo_td043_driver);
563 static int __devexit tpo_td043_spi_remove(struct spi_device *spi)
565 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&spi->dev);
567 omap_dss_unregister_driver(&tpo_td043_driver);
573 #ifdef CONFIG_PM_SLEEP
574 static int tpo_td043_spi_suspend(struct device *dev)
576 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
578 dev_dbg(dev, "tpo_td043_spi_suspend, tpo %p\n", tpo_td043);
580 tpo_td043->power_on_resume = tpo_td043->powered_on;
581 tpo_td043_power_off(tpo_td043);
582 tpo_td043->spi_suspended = 1;
587 static int tpo_td043_spi_resume(struct device *dev)
589 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
592 dev_dbg(dev, "tpo_td043_spi_resume\n");
594 if (tpo_td043->power_on_resume) {
595 ret = tpo_td043_power_on(tpo_td043);
599 tpo_td043->spi_suspended = 0;
605 static SIMPLE_DEV_PM_OPS(tpo_td043_spi_pm,
606 tpo_td043_spi_suspend, tpo_td043_spi_resume);
608 static struct spi_driver tpo_td043_spi_driver = {
610 .name = "tpo_td043mtea1_panel_spi",
611 .bus = &spi_bus_type,
612 .owner = THIS_MODULE,
613 .pm = &tpo_td043_spi_pm,
615 .probe = tpo_td043_spi_probe,
616 .remove = __devexit_p(tpo_td043_spi_remove),
619 static int __init tpo_td043_init(void)
621 return spi_register_driver(&tpo_td043_spi_driver);
624 static void __exit tpo_td043_exit(void)
626 spi_unregister_driver(&tpo_td043_spi_driver);
629 module_init(tpo_td043_init);
630 module_exit(tpo_td043_exit);
632 MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
633 MODULE_DESCRIPTION("TPO TD043MTEA1 LCD Driver");
634 MODULE_LICENSE("GPL");