2 * TPO TD043MTEA1 Panel driver
4 * Author: Gražvydas Ignotas <notasas@gmail.com>
5 * Converted to new DSS device model: Tomi Valkeinen <tomi.valkeinen@ti.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/delay.h>
15 #include <linux/spi/spi.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/of_gpio.h>
22 #include "../dss/omapdss.h"
24 #define TPO_R02_MODE(x) ((x) & 7)
25 #define TPO_R02_MODE_800x480 7
26 #define TPO_R02_NCLK_RISING BIT(3)
27 #define TPO_R02_HSYNC_HIGH BIT(4)
28 #define TPO_R02_VSYNC_HIGH BIT(5)
30 #define TPO_R03_NSTANDBY BIT(0)
31 #define TPO_R03_EN_CP_CLK BIT(1)
32 #define TPO_R03_EN_VGL_PUMP BIT(2)
33 #define TPO_R03_EN_PWM BIT(3)
34 #define TPO_R03_DRIVING_CAP_100 BIT(4)
35 #define TPO_R03_EN_PRE_CHARGE BIT(6)
36 #define TPO_R03_SOFTWARE_CTL BIT(7)
38 #define TPO_R04_NFLIP_H BIT(0)
39 #define TPO_R04_NFLIP_V BIT(1)
40 #define TPO_R04_CP_CLK_FREQ_1H BIT(2)
41 #define TPO_R04_VGL_FREQ_1H BIT(4)
43 #define TPO_R03_VAL_NORMAL (TPO_R03_NSTANDBY | TPO_R03_EN_CP_CLK | \
44 TPO_R03_EN_VGL_PUMP | TPO_R03_EN_PWM | \
45 TPO_R03_DRIVING_CAP_100 | TPO_R03_EN_PRE_CHARGE | \
48 #define TPO_R03_VAL_STANDBY (TPO_R03_DRIVING_CAP_100 | \
49 TPO_R03_EN_PRE_CHARGE | TPO_R03_SOFTWARE_CTL)
51 static const u16 tpo_td043_def_gamma[12] = {
52 105, 315, 381, 431, 490, 537, 579, 686, 780, 837, 880, 1023
55 struct panel_drv_data {
56 struct omap_dss_device dssdev;
57 struct omap_dss_device *in;
61 struct spi_device *spi;
62 struct regulator *vcc_reg;
70 u32 power_on_resume:1;
73 static const struct videomode tpo_td043_vm = {
77 .pixelclock = 36000000,
87 .flags = DISPLAY_FLAGS_HSYNC_LOW | DISPLAY_FLAGS_VSYNC_LOW |
88 DISPLAY_FLAGS_DE_HIGH | DISPLAY_FLAGS_SYNC_POSEDGE |
89 DISPLAY_FLAGS_PIXDATA_NEGEDGE,
91 * Note: According to the panel documentation:
92 * SYNC needs to be driven on the FALLING edge
96 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
98 static int tpo_td043_write(struct spi_device *spi, u8 addr, u8 data)
100 struct spi_message m;
101 struct spi_transfer xfer;
105 spi_message_init(&m);
107 memset(&xfer, 0, sizeof(xfer));
109 w = ((u16)addr << 10) | (1 << 8) | data;
111 xfer.bits_per_word = 16;
113 spi_message_add_tail(&xfer, &m);
115 r = spi_sync(spi, &m);
117 dev_warn(&spi->dev, "failed to write to LCD reg (%d)\n", r);
121 static void tpo_td043_write_gamma(struct spi_device *spi, u16 gamma[12])
125 /* gamma bits [9:8] */
126 for (val = i = 0; i < 4; i++)
127 val |= (gamma[i] & 0x300) >> ((i + 1) * 2);
128 tpo_td043_write(spi, 0x11, val);
130 for (val = i = 0; i < 4; i++)
131 val |= (gamma[i+4] & 0x300) >> ((i + 1) * 2);
132 tpo_td043_write(spi, 0x12, val);
134 for (val = i = 0; i < 4; i++)
135 val |= (gamma[i+8] & 0x300) >> ((i + 1) * 2);
136 tpo_td043_write(spi, 0x13, val);
138 /* gamma bits [7:0] */
139 for (val = i = 0; i < 12; i++)
140 tpo_td043_write(spi, 0x14 + i, gamma[i] & 0xff);
143 static int tpo_td043_write_mirror(struct spi_device *spi, bool h, bool v)
145 u8 reg4 = TPO_R04_NFLIP_H | TPO_R04_NFLIP_V |
146 TPO_R04_CP_CLK_FREQ_1H | TPO_R04_VGL_FREQ_1H;
148 reg4 &= ~TPO_R04_NFLIP_H;
150 reg4 &= ~TPO_R04_NFLIP_V;
152 return tpo_td043_write(spi, 4, reg4);
155 static int tpo_td043_set_hmirror(struct omap_dss_device *dssdev, bool enable)
157 struct panel_drv_data *ddata = dev_get_drvdata(dssdev->dev);
159 ddata->hmirror = enable;
160 return tpo_td043_write_mirror(ddata->spi, ddata->hmirror,
164 static bool tpo_td043_get_hmirror(struct omap_dss_device *dssdev)
166 struct panel_drv_data *ddata = dev_get_drvdata(dssdev->dev);
168 return ddata->hmirror;
171 static ssize_t tpo_td043_vmirror_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
174 struct panel_drv_data *ddata = dev_get_drvdata(dev);
176 return snprintf(buf, PAGE_SIZE, "%d\n", ddata->vmirror);
179 static ssize_t tpo_td043_vmirror_store(struct device *dev,
180 struct device_attribute *attr, const char *buf, size_t count)
182 struct panel_drv_data *ddata = dev_get_drvdata(dev);
186 ret = kstrtoint(buf, 0, &val);
192 ret = tpo_td043_write_mirror(ddata->spi, ddata->hmirror, val);
196 ddata->vmirror = val;
201 static ssize_t tpo_td043_mode_show(struct device *dev,
202 struct device_attribute *attr, char *buf)
204 struct panel_drv_data *ddata = dev_get_drvdata(dev);
206 return snprintf(buf, PAGE_SIZE, "%d\n", ddata->mode);
209 static ssize_t tpo_td043_mode_store(struct device *dev,
210 struct device_attribute *attr, const char *buf, size_t count)
212 struct panel_drv_data *ddata = dev_get_drvdata(dev);
216 ret = kstrtol(buf, 0, &val);
217 if (ret != 0 || val & ~7)
222 val |= TPO_R02_NCLK_RISING;
223 tpo_td043_write(ddata->spi, 2, val);
228 static ssize_t tpo_td043_gamma_show(struct device *dev,
229 struct device_attribute *attr, char *buf)
231 struct panel_drv_data *ddata = dev_get_drvdata(dev);
236 for (i = 0; i < ARRAY_SIZE(ddata->gamma); i++) {
237 ret = snprintf(buf + len, PAGE_SIZE - len, "%u ",
248 static ssize_t tpo_td043_gamma_store(struct device *dev,
249 struct device_attribute *attr, const char *buf, size_t count)
251 struct panel_drv_data *ddata = dev_get_drvdata(dev);
256 ret = sscanf(buf, "%u %u %u %u %u %u %u %u %u %u %u %u",
257 &g[0], &g[1], &g[2], &g[3], &g[4], &g[5],
258 &g[6], &g[7], &g[8], &g[9], &g[10], &g[11]);
263 for (i = 0; i < 12; i++)
264 ddata->gamma[i] = g[i];
266 tpo_td043_write_gamma(ddata->spi, ddata->gamma);
271 static DEVICE_ATTR(vmirror, S_IRUGO | S_IWUSR,
272 tpo_td043_vmirror_show, tpo_td043_vmirror_store);
273 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
274 tpo_td043_mode_show, tpo_td043_mode_store);
275 static DEVICE_ATTR(gamma, S_IRUGO | S_IWUSR,
276 tpo_td043_gamma_show, tpo_td043_gamma_store);
278 static struct attribute *tpo_td043_attrs[] = {
279 &dev_attr_vmirror.attr,
281 &dev_attr_gamma.attr,
285 static const struct attribute_group tpo_td043_attr_group = {
286 .attrs = tpo_td043_attrs,
289 static int tpo_td043_power_on(struct panel_drv_data *ddata)
293 if (ddata->powered_on)
296 r = regulator_enable(ddata->vcc_reg);
300 /* wait for panel to stabilize */
303 if (gpio_is_valid(ddata->nreset_gpio))
304 gpio_set_value(ddata->nreset_gpio, 1);
306 tpo_td043_write(ddata->spi, 2,
307 TPO_R02_MODE(ddata->mode) | TPO_R02_NCLK_RISING);
308 tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_NORMAL);
309 tpo_td043_write(ddata->spi, 0x20, 0xf0);
310 tpo_td043_write(ddata->spi, 0x21, 0xf0);
311 tpo_td043_write_mirror(ddata->spi, ddata->hmirror,
313 tpo_td043_write_gamma(ddata->spi, ddata->gamma);
315 ddata->powered_on = 1;
319 static void tpo_td043_power_off(struct panel_drv_data *ddata)
321 if (!ddata->powered_on)
324 tpo_td043_write(ddata->spi, 3,
325 TPO_R03_VAL_STANDBY | TPO_R03_EN_PWM);
327 if (gpio_is_valid(ddata->nreset_gpio))
328 gpio_set_value(ddata->nreset_gpio, 0);
330 /* wait for at least 2 vsyncs before cutting off power */
333 tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_STANDBY);
335 regulator_disable(ddata->vcc_reg);
337 ddata->powered_on = 0;
340 static int tpo_td043_connect(struct omap_dss_device *dssdev)
342 struct panel_drv_data *ddata = to_panel_data(dssdev);
343 struct omap_dss_device *in;
346 if (omapdss_device_is_connected(dssdev))
349 in = omapdss_of_find_source_for_first_ep(dssdev->dev->of_node);
351 dev_err(dssdev->dev, "failed to find video source\n");
355 r = in->ops.dpi->connect(in, dssdev);
357 omap_dss_put_device(in);
365 static void tpo_td043_disconnect(struct omap_dss_device *dssdev)
367 struct panel_drv_data *ddata = to_panel_data(dssdev);
368 struct omap_dss_device *in = ddata->in;
370 if (!omapdss_device_is_connected(dssdev))
373 in->ops.dpi->disconnect(in, dssdev);
375 omap_dss_put_device(in);
379 static int tpo_td043_enable(struct omap_dss_device *dssdev)
381 struct panel_drv_data *ddata = to_panel_data(dssdev);
382 struct omap_dss_device *in = ddata->in;
385 if (!omapdss_device_is_connected(dssdev))
388 if (omapdss_device_is_enabled(dssdev))
391 in->ops.dpi->set_timings(in, &ddata->vm);
393 r = in->ops.dpi->enable(in);
398 * If we are resuming from system suspend, SPI clocks might not be
399 * enabled yet, so we'll program the LCD from SPI PM resume callback.
401 if (!ddata->spi_suspended) {
402 r = tpo_td043_power_on(ddata);
404 in->ops.dpi->disable(in);
409 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
414 static void tpo_td043_disable(struct omap_dss_device *dssdev)
416 struct panel_drv_data *ddata = to_panel_data(dssdev);
417 struct omap_dss_device *in = ddata->in;
419 if (!omapdss_device_is_enabled(dssdev))
422 in->ops.dpi->disable(in);
424 if (!ddata->spi_suspended)
425 tpo_td043_power_off(ddata);
427 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
430 static void tpo_td043_set_timings(struct omap_dss_device *dssdev,
431 struct videomode *vm)
433 struct panel_drv_data *ddata = to_panel_data(dssdev);
434 struct omap_dss_device *in = ddata->in;
437 dssdev->panel.vm = *vm;
439 in->ops.dpi->set_timings(in, vm);
442 static void tpo_td043_get_timings(struct omap_dss_device *dssdev,
443 struct videomode *vm)
445 struct panel_drv_data *ddata = to_panel_data(dssdev);
450 static int tpo_td043_check_timings(struct omap_dss_device *dssdev,
451 struct videomode *vm)
453 struct panel_drv_data *ddata = to_panel_data(dssdev);
454 struct omap_dss_device *in = ddata->in;
456 return in->ops.dpi->check_timings(in, vm);
459 static struct omap_dss_driver tpo_td043_ops = {
460 .connect = tpo_td043_connect,
461 .disconnect = tpo_td043_disconnect,
463 .enable = tpo_td043_enable,
464 .disable = tpo_td043_disable,
466 .set_timings = tpo_td043_set_timings,
467 .get_timings = tpo_td043_get_timings,
468 .check_timings = tpo_td043_check_timings,
470 .set_mirror = tpo_td043_set_hmirror,
471 .get_mirror = tpo_td043_get_hmirror,
474 static int tpo_td043_probe_of(struct spi_device *spi)
476 struct device_node *node = spi->dev.of_node;
477 struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
480 gpio = of_get_named_gpio(node, "reset-gpios", 0);
481 if (!gpio_is_valid(gpio)) {
482 dev_err(&spi->dev, "failed to parse enable gpio\n");
485 ddata->nreset_gpio = gpio;
490 static int tpo_td043_probe(struct spi_device *spi)
492 struct panel_drv_data *ddata;
493 struct omap_dss_device *dssdev;
496 dev_dbg(&spi->dev, "%s\n", __func__);
498 spi->bits_per_word = 16;
499 spi->mode = SPI_MODE_0;
503 dev_err(&spi->dev, "spi_setup failed: %d\n", r);
507 ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
511 dev_set_drvdata(&spi->dev, ddata);
515 r = tpo_td043_probe_of(spi);
519 ddata->mode = TPO_R02_MODE_800x480;
520 memcpy(ddata->gamma, tpo_td043_def_gamma, sizeof(ddata->gamma));
522 ddata->vcc_reg = devm_regulator_get(&spi->dev, "vcc");
523 if (IS_ERR(ddata->vcc_reg)) {
524 dev_err(&spi->dev, "failed to get LCD VCC regulator\n");
525 r = PTR_ERR(ddata->vcc_reg);
529 if (gpio_is_valid(ddata->nreset_gpio)) {
530 r = devm_gpio_request_one(&spi->dev,
531 ddata->nreset_gpio, GPIOF_OUT_INIT_LOW,
534 dev_err(&spi->dev, "couldn't request reset GPIO\n");
539 r = sysfs_create_group(&spi->dev.kobj, &tpo_td043_attr_group);
541 dev_err(&spi->dev, "failed to create sysfs files\n");
545 ddata->vm = tpo_td043_vm;
547 dssdev = &ddata->dssdev;
548 dssdev->dev = &spi->dev;
549 dssdev->driver = &tpo_td043_ops;
550 dssdev->type = OMAP_DISPLAY_TYPE_DPI;
551 dssdev->owner = THIS_MODULE;
552 dssdev->panel.vm = ddata->vm;
554 r = omapdss_register_display(dssdev);
556 dev_err(&spi->dev, "Failed to register panel\n");
563 sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
570 static int tpo_td043_remove(struct spi_device *spi)
572 struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
573 struct omap_dss_device *dssdev = &ddata->dssdev;
575 dev_dbg(&ddata->spi->dev, "%s\n", __func__);
577 omapdss_unregister_display(dssdev);
579 tpo_td043_disable(dssdev);
580 tpo_td043_disconnect(dssdev);
582 sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
587 #ifdef CONFIG_PM_SLEEP
588 static int tpo_td043_spi_suspend(struct device *dev)
590 struct panel_drv_data *ddata = dev_get_drvdata(dev);
592 dev_dbg(dev, "tpo_td043_spi_suspend, tpo %p\n", ddata);
594 ddata->power_on_resume = ddata->powered_on;
595 tpo_td043_power_off(ddata);
596 ddata->spi_suspended = 1;
601 static int tpo_td043_spi_resume(struct device *dev)
603 struct panel_drv_data *ddata = dev_get_drvdata(dev);
606 dev_dbg(dev, "tpo_td043_spi_resume\n");
608 if (ddata->power_on_resume) {
609 ret = tpo_td043_power_on(ddata);
613 ddata->spi_suspended = 0;
619 static SIMPLE_DEV_PM_OPS(tpo_td043_spi_pm,
620 tpo_td043_spi_suspend, tpo_td043_spi_resume);
622 static const struct of_device_id tpo_td043_of_match[] = {
623 { .compatible = "omapdss,tpo,td043mtea1", },
627 MODULE_DEVICE_TABLE(of, tpo_td043_of_match);
629 static struct spi_driver tpo_td043_spi_driver = {
631 .name = "panel-tpo-td043mtea1",
632 .pm = &tpo_td043_spi_pm,
633 .of_match_table = tpo_td043_of_match,
634 .suppress_bind_attrs = true,
636 .probe = tpo_td043_probe,
637 .remove = tpo_td043_remove,
640 module_spi_driver(tpo_td043_spi_driver);
642 MODULE_ALIAS("spi:tpo,td043mtea1");
643 MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
644 MODULE_DESCRIPTION("TPO TD043MTEA1 LCD Driver");
645 MODULE_LICENSE("GPL");