OMAPDSS: DPI: change regulator handling
[linux-2.6/btrfs-unstable.git] / drivers / video / omap2 / displays / panel-tpo-td043mtea1.c
blob7729b6fa6f97748bac06ca9e44a94fdae65df74d
1 /*
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>
21 #include <video/omap-panel-data.h>
23 #define TPO_R02_MODE(x) ((x) & 7)
24 #define TPO_R02_MODE_800x480 7
25 #define TPO_R02_NCLK_RISING BIT(3)
26 #define TPO_R02_HSYNC_HIGH BIT(4)
27 #define TPO_R02_VSYNC_HIGH BIT(5)
29 #define TPO_R03_NSTANDBY BIT(0)
30 #define TPO_R03_EN_CP_CLK BIT(1)
31 #define TPO_R03_EN_VGL_PUMP BIT(2)
32 #define TPO_R03_EN_PWM BIT(3)
33 #define TPO_R03_DRIVING_CAP_100 BIT(4)
34 #define TPO_R03_EN_PRE_CHARGE BIT(6)
35 #define TPO_R03_SOFTWARE_CTL BIT(7)
37 #define TPO_R04_NFLIP_H BIT(0)
38 #define TPO_R04_NFLIP_V BIT(1)
39 #define TPO_R04_CP_CLK_FREQ_1H BIT(2)
40 #define TPO_R04_VGL_FREQ_1H BIT(4)
42 #define TPO_R03_VAL_NORMAL (TPO_R03_NSTANDBY | TPO_R03_EN_CP_CLK | \
43 TPO_R03_EN_VGL_PUMP | TPO_R03_EN_PWM | \
44 TPO_R03_DRIVING_CAP_100 | TPO_R03_EN_PRE_CHARGE | \
45 TPO_R03_SOFTWARE_CTL)
47 #define TPO_R03_VAL_STANDBY (TPO_R03_DRIVING_CAP_100 | \
48 TPO_R03_EN_PRE_CHARGE | TPO_R03_SOFTWARE_CTL)
50 static const u16 tpo_td043_def_gamma[12] = {
51 105, 315, 381, 431, 490, 537, 579, 686, 780, 837, 880, 1023
54 struct tpo_td043_device {
55 struct spi_device *spi;
56 struct regulator *vcc_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;
67 /* used to pass spi_device from SPI to DSS portion of the driver */
68 static struct tpo_td043_device *g_tpo_td043;
70 static int tpo_td043_write(struct spi_device *spi, u8 addr, u8 data)
72 struct spi_message m;
73 struct spi_transfer xfer;
74 u16 w;
75 int r;
77 spi_message_init(&m);
79 memset(&xfer, 0, sizeof(xfer));
81 w = ((u16)addr << 10) | (1 << 8) | data;
82 xfer.tx_buf = &w;
83 xfer.bits_per_word = 16;
84 xfer.len = 2;
85 spi_message_add_tail(&xfer, &m);
87 r = spi_sync(spi, &m);
88 if (r < 0)
89 dev_warn(&spi->dev, "failed to write to LCD reg (%d)\n", r);
90 return r;
93 static void tpo_td043_write_gamma(struct spi_device *spi, u16 gamma[12])
95 u8 i, val;
97 /* gamma bits [9:8] */
98 for (val = i = 0; i < 4; i++)
99 val |= (gamma[i] & 0x300) >> ((i + 1) * 2);
100 tpo_td043_write(spi, 0x11, val);
102 for (val = i = 0; i < 4; i++)
103 val |= (gamma[i+4] & 0x300) >> ((i + 1) * 2);
104 tpo_td043_write(spi, 0x12, val);
106 for (val = i = 0; i < 4; i++)
107 val |= (gamma[i+8] & 0x300) >> ((i + 1) * 2);
108 tpo_td043_write(spi, 0x13, val);
110 /* gamma bits [7:0] */
111 for (val = i = 0; i < 12; i++)
112 tpo_td043_write(spi, 0x14 + i, gamma[i] & 0xff);
115 static int tpo_td043_write_mirror(struct spi_device *spi, bool h, bool v)
117 u8 reg4 = TPO_R04_NFLIP_H | TPO_R04_NFLIP_V | \
118 TPO_R04_CP_CLK_FREQ_1H | TPO_R04_VGL_FREQ_1H;
119 if (h)
120 reg4 &= ~TPO_R04_NFLIP_H;
121 if (v)
122 reg4 &= ~TPO_R04_NFLIP_V;
124 return tpo_td043_write(spi, 4, reg4);
127 static int tpo_td043_set_hmirror(struct omap_dss_device *dssdev, bool enable)
129 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dssdev->dev);
131 tpo_td043->hmirror = enable;
132 return tpo_td043_write_mirror(tpo_td043->spi, tpo_td043->hmirror,
133 tpo_td043->vmirror);
136 static bool tpo_td043_get_hmirror(struct omap_dss_device *dssdev)
138 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dssdev->dev);
140 return tpo_td043->hmirror;
143 static ssize_t tpo_td043_vmirror_show(struct device *dev,
144 struct device_attribute *attr, char *buf)
146 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
148 return snprintf(buf, PAGE_SIZE, "%d\n", tpo_td043->vmirror);
151 static ssize_t tpo_td043_vmirror_store(struct device *dev,
152 struct device_attribute *attr, const char *buf, size_t count)
154 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
155 int val;
156 int ret;
158 ret = kstrtoint(buf, 0, &val);
159 if (ret < 0)
160 return ret;
162 val = !!val;
164 ret = tpo_td043_write_mirror(tpo_td043->spi, tpo_td043->hmirror, val);
165 if (ret < 0)
166 return ret;
168 tpo_td043->vmirror = val;
170 return count;
173 static ssize_t tpo_td043_mode_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
178 return snprintf(buf, PAGE_SIZE, "%d\n", tpo_td043->mode);
181 static ssize_t tpo_td043_mode_store(struct device *dev,
182 struct device_attribute *attr, const char *buf, size_t count)
184 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
185 long val;
186 int ret;
188 ret = kstrtol(buf, 0, &val);
189 if (ret != 0 || val & ~7)
190 return -EINVAL;
192 tpo_td043->mode = val;
194 val |= TPO_R02_NCLK_RISING;
195 tpo_td043_write(tpo_td043->spi, 2, val);
197 return count;
200 static ssize_t tpo_td043_gamma_show(struct device *dev,
201 struct device_attribute *attr, char *buf)
203 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
204 ssize_t len = 0;
205 int ret;
206 int i;
208 for (i = 0; i < ARRAY_SIZE(tpo_td043->gamma); i++) {
209 ret = snprintf(buf + len, PAGE_SIZE - len, "%u ",
210 tpo_td043->gamma[i]);
211 if (ret < 0)
212 return ret;
213 len += ret;
215 buf[len - 1] = '\n';
217 return len;
220 static ssize_t tpo_td043_gamma_store(struct device *dev,
221 struct device_attribute *attr, const char *buf, size_t count)
223 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
224 unsigned int g[12];
225 int ret;
226 int i;
228 ret = sscanf(buf, "%u %u %u %u %u %u %u %u %u %u %u %u",
229 &g[0], &g[1], &g[2], &g[3], &g[4], &g[5],
230 &g[6], &g[7], &g[8], &g[9], &g[10], &g[11]);
232 if (ret != 12)
233 return -EINVAL;
235 for (i = 0; i < 12; i++)
236 tpo_td043->gamma[i] = g[i];
238 tpo_td043_write_gamma(tpo_td043->spi, tpo_td043->gamma);
240 return count;
243 static DEVICE_ATTR(vmirror, S_IRUGO | S_IWUSR,
244 tpo_td043_vmirror_show, tpo_td043_vmirror_store);
245 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
246 tpo_td043_mode_show, tpo_td043_mode_store);
247 static DEVICE_ATTR(gamma, S_IRUGO | S_IWUSR,
248 tpo_td043_gamma_show, tpo_td043_gamma_store);
250 static struct attribute *tpo_td043_attrs[] = {
251 &dev_attr_vmirror.attr,
252 &dev_attr_mode.attr,
253 &dev_attr_gamma.attr,
254 NULL,
257 static struct attribute_group tpo_td043_attr_group = {
258 .attrs = tpo_td043_attrs,
261 static const struct omap_video_timings tpo_td043_timings = {
262 .x_res = 800,
263 .y_res = 480,
265 .pixel_clock = 36000,
267 .hsw = 1,
268 .hfp = 68,
269 .hbp = 214,
271 .vsw = 1,
272 .vfp = 39,
273 .vbp = 34,
275 .vsync_level = OMAPDSS_SIG_ACTIVE_LOW,
276 .hsync_level = OMAPDSS_SIG_ACTIVE_LOW,
277 .data_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE,
278 .de_level = OMAPDSS_SIG_ACTIVE_HIGH,
279 .sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES,
282 static inline struct panel_tpo_td043_data
283 *get_panel_data(const struct omap_dss_device *dssdev)
285 return (struct panel_tpo_td043_data *) dssdev->data;
288 static int tpo_td043_power_on(struct tpo_td043_device *tpo_td043)
290 int r;
292 if (tpo_td043->powered_on)
293 return 0;
295 r = regulator_enable(tpo_td043->vcc_reg);
296 if (r != 0)
297 return r;
299 /* wait for panel to stabilize */
300 msleep(160);
302 if (gpio_is_valid(tpo_td043->nreset_gpio))
303 gpio_set_value(tpo_td043->nreset_gpio, 1);
305 tpo_td043_write(tpo_td043->spi, 2,
306 TPO_R02_MODE(tpo_td043->mode) | TPO_R02_NCLK_RISING);
307 tpo_td043_write(tpo_td043->spi, 3, TPO_R03_VAL_NORMAL);
308 tpo_td043_write(tpo_td043->spi, 0x20, 0xf0);
309 tpo_td043_write(tpo_td043->spi, 0x21, 0xf0);
310 tpo_td043_write_mirror(tpo_td043->spi, tpo_td043->hmirror,
311 tpo_td043->vmirror);
312 tpo_td043_write_gamma(tpo_td043->spi, tpo_td043->gamma);
314 tpo_td043->powered_on = 1;
315 return 0;
318 static void tpo_td043_power_off(struct tpo_td043_device *tpo_td043)
320 if (!tpo_td043->powered_on)
321 return;
323 tpo_td043_write(tpo_td043->spi, 3,
324 TPO_R03_VAL_STANDBY | TPO_R03_EN_PWM);
326 if (gpio_is_valid(tpo_td043->nreset_gpio))
327 gpio_set_value(tpo_td043->nreset_gpio, 0);
329 /* wait for at least 2 vsyncs before cutting off power */
330 msleep(50);
332 tpo_td043_write(tpo_td043->spi, 3, TPO_R03_VAL_STANDBY);
334 regulator_disable(tpo_td043->vcc_reg);
336 tpo_td043->powered_on = 0;
339 static int tpo_td043_enable_dss(struct omap_dss_device *dssdev)
341 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dssdev->dev);
342 int r;
344 if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
345 return 0;
347 omapdss_dpi_set_timings(dssdev, &dssdev->panel.timings);
348 omapdss_dpi_set_data_lines(dssdev, dssdev->phy.dpi.data_lines);
350 r = omapdss_dpi_display_enable(dssdev);
351 if (r)
352 goto err0;
355 * If we are resuming from system suspend, SPI clocks might not be
356 * enabled yet, so we'll program the LCD from SPI PM resume callback.
358 if (!tpo_td043->spi_suspended) {
359 r = tpo_td043_power_on(tpo_td043);
360 if (r)
361 goto err1;
364 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
366 return 0;
367 err1:
368 omapdss_dpi_display_disable(dssdev);
369 err0:
370 return r;
373 static void tpo_td043_disable_dss(struct omap_dss_device *dssdev)
375 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dssdev->dev);
377 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
378 return;
380 omapdss_dpi_display_disable(dssdev);
382 if (!tpo_td043->spi_suspended)
383 tpo_td043_power_off(tpo_td043);
386 static int tpo_td043_enable(struct omap_dss_device *dssdev)
388 dev_dbg(dssdev->dev, "enable\n");
390 return tpo_td043_enable_dss(dssdev);
393 static void tpo_td043_disable(struct omap_dss_device *dssdev)
395 dev_dbg(dssdev->dev, "disable\n");
397 tpo_td043_disable_dss(dssdev);
399 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
402 static int tpo_td043_probe(struct omap_dss_device *dssdev)
404 struct tpo_td043_device *tpo_td043 = g_tpo_td043;
405 struct panel_tpo_td043_data *pdata = get_panel_data(dssdev);
406 int ret = 0;
408 dev_dbg(dssdev->dev, "probe\n");
410 if (tpo_td043 == NULL) {
411 dev_err(dssdev->dev, "missing tpo_td043_device\n");
412 return -ENODEV;
415 if (!pdata)
416 return -EINVAL;
418 tpo_td043->nreset_gpio = pdata->nreset_gpio;
420 dssdev->panel.timings = tpo_td043_timings;
421 dssdev->ctrl.pixel_size = 24;
423 tpo_td043->mode = TPO_R02_MODE_800x480;
424 memcpy(tpo_td043->gamma, tpo_td043_def_gamma, sizeof(tpo_td043->gamma));
426 tpo_td043->vcc_reg = regulator_get(dssdev->dev, "vcc");
427 if (IS_ERR(tpo_td043->vcc_reg)) {
428 dev_err(dssdev->dev, "failed to get LCD VCC regulator\n");
429 ret = PTR_ERR(tpo_td043->vcc_reg);
430 goto fail_regulator;
433 if (gpio_is_valid(tpo_td043->nreset_gpio)) {
434 ret = devm_gpio_request_one(dssdev->dev,
435 tpo_td043->nreset_gpio, GPIOF_OUT_INIT_LOW,
436 "lcd reset");
437 if (ret < 0) {
438 dev_err(dssdev->dev, "couldn't request reset GPIO\n");
439 goto fail_gpio_req;
443 ret = sysfs_create_group(&dssdev->dev->kobj, &tpo_td043_attr_group);
444 if (ret)
445 dev_warn(dssdev->dev, "failed to create sysfs files\n");
447 dev_set_drvdata(dssdev->dev, tpo_td043);
449 return 0;
451 fail_gpio_req:
452 regulator_put(tpo_td043->vcc_reg);
453 fail_regulator:
454 kfree(tpo_td043);
455 return ret;
458 static void tpo_td043_remove(struct omap_dss_device *dssdev)
460 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dssdev->dev);
462 dev_dbg(dssdev->dev, "remove\n");
464 sysfs_remove_group(&dssdev->dev->kobj, &tpo_td043_attr_group);
465 regulator_put(tpo_td043->vcc_reg);
468 static void tpo_td043_set_timings(struct omap_dss_device *dssdev,
469 struct omap_video_timings *timings)
471 omapdss_dpi_set_timings(dssdev, timings);
473 dssdev->panel.timings = *timings;
476 static int tpo_td043_check_timings(struct omap_dss_device *dssdev,
477 struct omap_video_timings *timings)
479 return dpi_check_timings(dssdev, timings);
482 static struct omap_dss_driver tpo_td043_driver = {
483 .probe = tpo_td043_probe,
484 .remove = tpo_td043_remove,
486 .enable = tpo_td043_enable,
487 .disable = tpo_td043_disable,
488 .set_mirror = tpo_td043_set_hmirror,
489 .get_mirror = tpo_td043_get_hmirror,
491 .set_timings = tpo_td043_set_timings,
492 .check_timings = tpo_td043_check_timings,
494 .driver = {
495 .name = "tpo_td043mtea1_panel",
496 .owner = THIS_MODULE,
500 static int tpo_td043_spi_probe(struct spi_device *spi)
502 struct omap_dss_device *dssdev = spi->dev.platform_data;
503 struct tpo_td043_device *tpo_td043;
504 int ret;
506 if (dssdev == NULL) {
507 dev_err(&spi->dev, "missing dssdev\n");
508 return -ENODEV;
511 if (g_tpo_td043 != NULL)
512 return -EBUSY;
514 spi->bits_per_word = 16;
515 spi->mode = SPI_MODE_0;
517 ret = spi_setup(spi);
518 if (ret < 0) {
519 dev_err(&spi->dev, "spi_setup failed: %d\n", ret);
520 return ret;
523 tpo_td043 = kzalloc(sizeof(*tpo_td043), GFP_KERNEL);
524 if (tpo_td043 == NULL)
525 return -ENOMEM;
527 tpo_td043->spi = spi;
528 dev_set_drvdata(&spi->dev, tpo_td043);
529 g_tpo_td043 = tpo_td043;
531 omap_dss_register_driver(&tpo_td043_driver);
533 return 0;
536 static int tpo_td043_spi_remove(struct spi_device *spi)
538 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(&spi->dev);
540 omap_dss_unregister_driver(&tpo_td043_driver);
541 kfree(tpo_td043);
542 g_tpo_td043 = NULL;
544 return 0;
547 #ifdef CONFIG_PM_SLEEP
548 static int tpo_td043_spi_suspend(struct device *dev)
550 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
552 dev_dbg(dev, "tpo_td043_spi_suspend, tpo %p\n", tpo_td043);
554 tpo_td043->power_on_resume = tpo_td043->powered_on;
555 tpo_td043_power_off(tpo_td043);
556 tpo_td043->spi_suspended = 1;
558 return 0;
561 static int tpo_td043_spi_resume(struct device *dev)
563 struct tpo_td043_device *tpo_td043 = dev_get_drvdata(dev);
564 int ret;
566 dev_dbg(dev, "tpo_td043_spi_resume\n");
568 if (tpo_td043->power_on_resume) {
569 ret = tpo_td043_power_on(tpo_td043);
570 if (ret)
571 return ret;
573 tpo_td043->spi_suspended = 0;
575 return 0;
577 #endif
579 static SIMPLE_DEV_PM_OPS(tpo_td043_spi_pm,
580 tpo_td043_spi_suspend, tpo_td043_spi_resume);
582 static struct spi_driver tpo_td043_spi_driver = {
583 .driver = {
584 .name = "tpo_td043mtea1_panel_spi",
585 .owner = THIS_MODULE,
586 .pm = &tpo_td043_spi_pm,
588 .probe = tpo_td043_spi_probe,
589 .remove = tpo_td043_spi_remove,
592 module_spi_driver(tpo_td043_spi_driver);
594 MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
595 MODULE_DESCRIPTION("TPO TD043MTEA1 LCD Driver");
596 MODULE_LICENSE("GPL");