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>
19 #include <plat/display.h>
21 #define TPO_R02_MODE(x) ((x) & 7)
22 #define TPO_R02_MODE_800x480 7
23 #define TPO_R02_NCLK_RISING BIT(3)
24 #define TPO_R02_HSYNC_HIGH BIT(4)
25 #define TPO_R02_VSYNC_HIGH BIT(5)
27 #define TPO_R03_NSTANDBY BIT(0)
28 #define TPO_R03_EN_CP_CLK BIT(1)
29 #define TPO_R03_EN_VGL_PUMP BIT(2)
30 #define TPO_R03_EN_PWM BIT(3)
31 #define TPO_R03_DRIVING_CAP_100 BIT(4)
32 #define TPO_R03_EN_PRE_CHARGE BIT(6)
33 #define TPO_R03_SOFTWARE_CTL BIT(7)
35 #define TPO_R04_NFLIP_H BIT(0)
36 #define TPO_R04_NFLIP_V BIT(1)
37 #define TPO_R04_CP_CLK_FREQ_1H BIT(2)
38 #define TPO_R04_VGL_FREQ_1H BIT(4)
40 #define TPO_R03_VAL_NORMAL (TPO_R03_NSTANDBY | TPO_R03_EN_CP_CLK | \
41 TPO_R03_EN_VGL_PUMP | TPO_R03_EN_PWM | \
42 TPO_R03_DRIVING_CAP_100 | TPO_R03_EN_PRE_CHARGE | \
45 #define TPO_R03_VAL_STANDBY (TPO_R03_DRIVING_CAP_100 | \
46 TPO_R03_EN_PRE_CHARGE | TPO_R03_SOFTWARE_CTL)
48 static const u16 tpo_td043_def_gamma
[12] = {
49 106, 200, 289, 375, 460, 543, 625, 705, 785, 864, 942, 1020
52 struct tpo_td043_device
{
53 struct spi_device
*spi
;
54 struct regulator
*vcc_reg
;
61 static int tpo_td043_write(struct spi_device
*spi
, u8 addr
, u8 data
)
64 struct spi_transfer xfer
;
70 memset(&xfer
, 0, sizeof(xfer
));
72 w
= ((u16
)addr
<< 10) | (1 << 8) | data
;
74 xfer
.bits_per_word
= 16;
76 spi_message_add_tail(&xfer
, &m
);
78 r
= spi_sync(spi
, &m
);
80 dev_warn(&spi
->dev
, "failed to write to LCD reg (%d)\n", r
);
84 static void tpo_td043_write_gamma(struct spi_device
*spi
, u16 gamma
[12])
88 /* gamma bits [9:8] */
89 for (val
= i
= 0; i
< 4; i
++)
90 val
|= (gamma
[i
] & 0x300) >> ((i
+ 1) * 2);
91 tpo_td043_write(spi
, 0x11, val
);
93 for (val
= i
= 0; i
< 4; i
++)
94 val
|= (gamma
[i
+4] & 0x300) >> ((i
+ 1) * 2);
95 tpo_td043_write(spi
, 0x12, val
);
97 for (val
= i
= 0; i
< 4; i
++)
98 val
|= (gamma
[i
+8] & 0x300) >> ((i
+ 1) * 2);
99 tpo_td043_write(spi
, 0x13, val
);
101 /* gamma bits [7:0] */
102 for (val
= i
= 0; i
< 12; i
++)
103 tpo_td043_write(spi
, 0x14 + i
, gamma
[i
] & 0xff);
106 static int tpo_td043_write_mirror(struct spi_device
*spi
, bool h
, bool v
)
108 u8 reg4
= TPO_R04_NFLIP_H
| TPO_R04_NFLIP_V
| \
109 TPO_R04_CP_CLK_FREQ_1H
| TPO_R04_VGL_FREQ_1H
;
111 reg4
&= ~TPO_R04_NFLIP_H
;
113 reg4
&= ~TPO_R04_NFLIP_V
;
115 return tpo_td043_write(spi
, 4, reg4
);
118 static int tpo_td043_set_hmirror(struct omap_dss_device
*dssdev
, bool enable
)
120 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(&dssdev
->dev
);
122 tpo_td043
->hmirror
= enable
;
123 return tpo_td043_write_mirror(tpo_td043
->spi
, tpo_td043
->hmirror
,
127 static bool tpo_td043_get_hmirror(struct omap_dss_device
*dssdev
)
129 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(&dssdev
->dev
);
131 return tpo_td043
->hmirror
;
134 static ssize_t
tpo_td043_vmirror_show(struct device
*dev
,
135 struct device_attribute
*attr
, char *buf
)
137 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(dev
);
139 return snprintf(buf
, PAGE_SIZE
, "%d\n", tpo_td043
->vmirror
);
142 static ssize_t
tpo_td043_vmirror_store(struct device
*dev
,
143 struct device_attribute
*attr
, const char *buf
, size_t count
)
145 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(dev
);
149 ret
= strict_strtol(buf
, 0, &val
);
153 ret
= tpo_td043_write_mirror(tpo_td043
->spi
, tpo_td043
->hmirror
, val
);
157 tpo_td043
->vmirror
= val
;
162 static ssize_t
tpo_td043_mode_show(struct device
*dev
,
163 struct device_attribute
*attr
, char *buf
)
165 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(dev
);
167 return snprintf(buf
, PAGE_SIZE
, "%d\n", tpo_td043
->mode
);
170 static ssize_t
tpo_td043_mode_store(struct device
*dev
,
171 struct device_attribute
*attr
, const char *buf
, size_t count
)
173 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(dev
);
177 ret
= strict_strtol(buf
, 0, &val
);
178 if (ret
!= 0 || val
& ~7)
181 tpo_td043
->mode
= val
;
183 val
|= TPO_R02_NCLK_RISING
;
184 tpo_td043_write(tpo_td043
->spi
, 2, val
);
189 static ssize_t
tpo_td043_gamma_show(struct device
*dev
,
190 struct device_attribute
*attr
, char *buf
)
192 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(dev
);
197 for (i
= 0; i
< ARRAY_SIZE(tpo_td043
->gamma
); i
++) {
198 ret
= snprintf(buf
+ len
, PAGE_SIZE
- len
, "%u ",
199 tpo_td043
->gamma
[i
]);
209 static ssize_t
tpo_td043_gamma_store(struct device
*dev
,
210 struct device_attribute
*attr
, const char *buf
, size_t count
)
212 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(dev
);
217 ret
= sscanf(buf
, "%u %u %u %u %u %u %u %u %u %u %u %u",
218 &g
[0], &g
[1], &g
[2], &g
[3], &g
[4], &g
[5],
219 &g
[6], &g
[7], &g
[8], &g
[9], &g
[10], &g
[11]);
224 for (i
= 0; i
< 12; i
++)
225 tpo_td043
->gamma
[i
] = g
[i
];
227 tpo_td043_write_gamma(tpo_td043
->spi
, tpo_td043
->gamma
);
232 static DEVICE_ATTR(vmirror
, S_IRUGO
| S_IWUSR
,
233 tpo_td043_vmirror_show
, tpo_td043_vmirror_store
);
234 static DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
235 tpo_td043_mode_show
, tpo_td043_mode_store
);
236 static DEVICE_ATTR(gamma
, S_IRUGO
| S_IWUSR
,
237 tpo_td043_gamma_show
, tpo_td043_gamma_store
);
239 static struct attribute
*tpo_td043_attrs
[] = {
240 &dev_attr_vmirror
.attr
,
242 &dev_attr_gamma
.attr
,
246 static struct attribute_group tpo_td043_attr_group
= {
247 .attrs
= tpo_td043_attrs
,
250 static const struct omap_video_timings tpo_td043_timings
= {
254 .pixel_clock
= 36000,
265 static int tpo_td043_power_on(struct omap_dss_device
*dssdev
)
267 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(&dssdev
->dev
);
268 int nreset_gpio
= dssdev
->reset_gpio
;
271 r
= omapdss_dpi_display_enable(dssdev
);
275 if (dssdev
->platform_enable
) {
276 r
= dssdev
->platform_enable(dssdev
);
281 regulator_enable(tpo_td043
->vcc_reg
);
283 /* wait for power up */
286 if (gpio_is_valid(nreset_gpio
))
287 gpio_set_value(nreset_gpio
, 1);
289 tpo_td043_write(tpo_td043
->spi
, 2,
290 TPO_R02_MODE(tpo_td043
->mode
) | TPO_R02_NCLK_RISING
);
291 tpo_td043_write(tpo_td043
->spi
, 3, TPO_R03_VAL_NORMAL
);
292 tpo_td043_write(tpo_td043
->spi
, 0x20, 0xf0);
293 tpo_td043_write(tpo_td043
->spi
, 0x21, 0xf0);
294 tpo_td043_write_mirror(tpo_td043
->spi
, tpo_td043
->hmirror
,
296 tpo_td043_write_gamma(tpo_td043
->spi
, tpo_td043
->gamma
);
300 omapdss_dpi_display_disable(dssdev
);
305 static void tpo_td043_power_off(struct omap_dss_device
*dssdev
)
307 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(&dssdev
->dev
);
308 int nreset_gpio
= dssdev
->reset_gpio
;
310 tpo_td043_write(tpo_td043
->spi
, 3,
311 TPO_R03_VAL_STANDBY
| TPO_R03_EN_PWM
);
313 if (gpio_is_valid(nreset_gpio
))
314 gpio_set_value(nreset_gpio
, 0);
316 /* wait for at least 2 vsyncs before cutting off power */
319 tpo_td043_write(tpo_td043
->spi
, 3, TPO_R03_VAL_STANDBY
);
321 regulator_disable(tpo_td043
->vcc_reg
);
323 if (dssdev
->platform_disable
)
324 dssdev
->platform_disable(dssdev
);
326 omapdss_dpi_display_disable(dssdev
);
329 static int tpo_td043_enable(struct omap_dss_device
*dssdev
)
333 dev_dbg(&dssdev
->dev
, "enable\n");
335 ret
= tpo_td043_power_on(dssdev
);
339 dssdev
->state
= OMAP_DSS_DISPLAY_ACTIVE
;
344 static void tpo_td043_disable(struct omap_dss_device
*dssdev
)
346 dev_dbg(&dssdev
->dev
, "disable\n");
348 tpo_td043_power_off(dssdev
);
350 dssdev
->state
= OMAP_DSS_DISPLAY_DISABLED
;
353 static int tpo_td043_suspend(struct omap_dss_device
*dssdev
)
355 tpo_td043_power_off(dssdev
);
356 dssdev
->state
= OMAP_DSS_DISPLAY_SUSPENDED
;
360 static int tpo_td043_resume(struct omap_dss_device
*dssdev
)
364 r
= tpo_td043_power_on(dssdev
);
368 dssdev
->state
= OMAP_DSS_DISPLAY_ACTIVE
;
373 static int tpo_td043_probe(struct omap_dss_device
*dssdev
)
375 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(&dssdev
->dev
);
376 int nreset_gpio
= dssdev
->reset_gpio
;
379 dev_dbg(&dssdev
->dev
, "probe\n");
381 if (tpo_td043
== NULL
) {
382 dev_err(&dssdev
->dev
, "missing tpo_td043_device\n");
386 dssdev
->panel
.config
= OMAP_DSS_LCD_TFT
| OMAP_DSS_LCD_IHS
|
387 OMAP_DSS_LCD_IVS
| OMAP_DSS_LCD_IPC
;
388 dssdev
->panel
.timings
= tpo_td043_timings
;
389 dssdev
->ctrl
.pixel_size
= 24;
391 tpo_td043
->mode
= TPO_R02_MODE_800x480
;
392 memcpy(tpo_td043
->gamma
, tpo_td043_def_gamma
, sizeof(tpo_td043
->gamma
));
394 tpo_td043
->vcc_reg
= regulator_get(&dssdev
->dev
, "vcc");
395 if (IS_ERR(tpo_td043
->vcc_reg
)) {
396 dev_err(&dssdev
->dev
, "failed to get LCD VCC regulator\n");
397 ret
= PTR_ERR(tpo_td043
->vcc_reg
);
401 if (gpio_is_valid(nreset_gpio
)) {
402 ret
= gpio_request(nreset_gpio
, "lcd reset");
404 dev_err(&dssdev
->dev
, "couldn't request reset GPIO\n");
408 ret
= gpio_direction_output(nreset_gpio
, 0);
410 dev_err(&dssdev
->dev
, "couldn't set GPIO direction\n");
411 goto fail_gpio_direction
;
415 ret
= sysfs_create_group(&dssdev
->dev
.kobj
, &tpo_td043_attr_group
);
417 dev_warn(&dssdev
->dev
, "failed to create sysfs files\n");
422 gpio_free(nreset_gpio
);
424 regulator_put(tpo_td043
->vcc_reg
);
430 static void tpo_td043_remove(struct omap_dss_device
*dssdev
)
432 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(&dssdev
->dev
);
433 int nreset_gpio
= dssdev
->reset_gpio
;
435 dev_dbg(&dssdev
->dev
, "remove\n");
437 sysfs_remove_group(&dssdev
->dev
.kobj
, &tpo_td043_attr_group
);
438 regulator_put(tpo_td043
->vcc_reg
);
439 if (gpio_is_valid(nreset_gpio
))
440 gpio_free(nreset_gpio
);
443 static struct omap_dss_driver tpo_td043_driver
= {
444 .probe
= tpo_td043_probe
,
445 .remove
= tpo_td043_remove
,
447 .enable
= tpo_td043_enable
,
448 .disable
= tpo_td043_disable
,
449 .suspend
= tpo_td043_suspend
,
450 .resume
= tpo_td043_resume
,
451 .set_mirror
= tpo_td043_set_hmirror
,
452 .get_mirror
= tpo_td043_get_hmirror
,
455 .name
= "tpo_td043mtea1_panel",
456 .owner
= THIS_MODULE
,
460 static int tpo_td043_spi_probe(struct spi_device
*spi
)
462 struct omap_dss_device
*dssdev
= spi
->dev
.platform_data
;
463 struct tpo_td043_device
*tpo_td043
;
466 if (dssdev
== NULL
) {
467 dev_err(&spi
->dev
, "missing dssdev\n");
471 spi
->bits_per_word
= 16;
472 spi
->mode
= SPI_MODE_0
;
474 ret
= spi_setup(spi
);
476 dev_err(&spi
->dev
, "spi_setup failed: %d\n", ret
);
480 tpo_td043
= kzalloc(sizeof(*tpo_td043
), GFP_KERNEL
);
481 if (tpo_td043
== NULL
)
484 tpo_td043
->spi
= spi
;
485 dev_set_drvdata(&spi
->dev
, tpo_td043
);
486 dev_set_drvdata(&dssdev
->dev
, tpo_td043
);
488 omap_dss_register_driver(&tpo_td043_driver
);
493 static int __devexit
tpo_td043_spi_remove(struct spi_device
*spi
)
495 struct tpo_td043_device
*tpo_td043
= dev_get_drvdata(&spi
->dev
);
497 omap_dss_unregister_driver(&tpo_td043_driver
);
503 static struct spi_driver tpo_td043_spi_driver
= {
505 .name
= "tpo_td043mtea1_panel_spi",
506 .bus
= &spi_bus_type
,
507 .owner
= THIS_MODULE
,
509 .probe
= tpo_td043_spi_probe
,
510 .remove
= __devexit_p(tpo_td043_spi_remove
),
513 static int __init
tpo_td043_init(void)
515 return spi_register_driver(&tpo_td043_spi_driver
);
518 static void __exit
tpo_td043_exit(void)
520 spi_unregister_driver(&tpo_td043_spi_driver
);
523 module_init(tpo_td043_init
);
524 module_exit(tpo_td043_exit
);
526 MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
527 MODULE_DESCRIPTION("TPO TD043MTEA1 LCD Driver");
528 MODULE_LICENSE("GPL");