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.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/of_gpio.h>
22 #include <video/omapdss.h>
23 #include <video/omap-panel-data.h>
25 #define TPO_R02_MODE(x) ((x) & 7)
26 #define TPO_R02_MODE_800x480 7
27 #define TPO_R02_NCLK_RISING BIT(3)
28 #define TPO_R02_HSYNC_HIGH BIT(4)
29 #define TPO_R02_VSYNC_HIGH BIT(5)
31 #define TPO_R03_NSTANDBY BIT(0)
32 #define TPO_R03_EN_CP_CLK BIT(1)
33 #define TPO_R03_EN_VGL_PUMP BIT(2)
34 #define TPO_R03_EN_PWM BIT(3)
35 #define TPO_R03_DRIVING_CAP_100 BIT(4)
36 #define TPO_R03_EN_PRE_CHARGE BIT(6)
37 #define TPO_R03_SOFTWARE_CTL BIT(7)
39 #define TPO_R04_NFLIP_H BIT(0)
40 #define TPO_R04_NFLIP_V BIT(1)
41 #define TPO_R04_CP_CLK_FREQ_1H BIT(2)
42 #define TPO_R04_VGL_FREQ_1H BIT(4)
44 #define TPO_R03_VAL_NORMAL (TPO_R03_NSTANDBY | TPO_R03_EN_CP_CLK | \
45 TPO_R03_EN_VGL_PUMP | TPO_R03_EN_PWM | \
46 TPO_R03_DRIVING_CAP_100 | TPO_R03_EN_PRE_CHARGE | \
49 #define TPO_R03_VAL_STANDBY (TPO_R03_DRIVING_CAP_100 | \
50 TPO_R03_EN_PRE_CHARGE | TPO_R03_SOFTWARE_CTL)
52 static const u16 tpo_td043_def_gamma[12] = {
53 105, 315, 381, 431, 490, 537, 579, 686, 780, 837, 880, 1023
56 struct panel_drv_data {
57 struct omap_dss_device dssdev;
58 struct omap_dss_device *in;
60 struct omap_video_timings videomode;
64 struct spi_device *spi;
65 struct regulator *vcc_reg;
73 u32 power_on_resume:1;
76 static const struct omap_video_timings tpo_td043_timings = {
80 .pixelclock = 36000000,
90 .vsync_level = OMAPDSS_SIG_ACTIVE_LOW,
91 .hsync_level = OMAPDSS_SIG_ACTIVE_LOW,
92 .data_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE,
93 .de_level = OMAPDSS_SIG_ACTIVE_HIGH,
94 .sync_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE,
97 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
99 static int tpo_td043_write(struct spi_device *spi, u8 addr, u8 data)
101 struct spi_message m;
102 struct spi_transfer xfer;
106 spi_message_init(&m);
108 memset(&xfer, 0, sizeof(xfer));
110 w = ((u16)addr << 10) | (1 << 8) | data;
112 xfer.bits_per_word = 16;
114 spi_message_add_tail(&xfer, &m);
116 r = spi_sync(spi, &m);
118 dev_warn(&spi->dev, "failed to write to LCD reg (%d)\n", r);
122 static void tpo_td043_write_gamma(struct spi_device *spi, u16 gamma[12])
126 /* gamma bits [9:8] */
127 for (val = i = 0; i < 4; i++)
128 val |= (gamma[i] & 0x300) >> ((i + 1) * 2);
129 tpo_td043_write(spi, 0x11, val);
131 for (val = i = 0; i < 4; i++)
132 val |= (gamma[i+4] & 0x300) >> ((i + 1) * 2);
133 tpo_td043_write(spi, 0x12, val);
135 for (val = i = 0; i < 4; i++)
136 val |= (gamma[i+8] & 0x300) >> ((i + 1) * 2);
137 tpo_td043_write(spi, 0x13, val);
139 /* gamma bits [7:0] */
140 for (val = i = 0; i < 12; i++)
141 tpo_td043_write(spi, 0x14 + i, gamma[i] & 0xff);
144 static int tpo_td043_write_mirror(struct spi_device *spi, bool h, bool v)
146 u8 reg4 = TPO_R04_NFLIP_H | TPO_R04_NFLIP_V |
147 TPO_R04_CP_CLK_FREQ_1H | TPO_R04_VGL_FREQ_1H;
149 reg4 &= ~TPO_R04_NFLIP_H;
151 reg4 &= ~TPO_R04_NFLIP_V;
153 return tpo_td043_write(spi, 4, reg4);
156 static int tpo_td043_set_hmirror(struct omap_dss_device *dssdev, bool enable)
158 struct panel_drv_data *ddata = dev_get_drvdata(dssdev->dev);
160 ddata->hmirror = enable;
161 return tpo_td043_write_mirror(ddata->spi, ddata->hmirror,
165 static bool tpo_td043_get_hmirror(struct omap_dss_device *dssdev)
167 struct panel_drv_data *ddata = dev_get_drvdata(dssdev->dev);
169 return ddata->hmirror;
172 static ssize_t tpo_td043_vmirror_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
175 struct panel_drv_data *ddata = dev_get_drvdata(dev);
177 return snprintf(buf, PAGE_SIZE, "%d\n", ddata->vmirror);
180 static ssize_t tpo_td043_vmirror_store(struct device *dev,
181 struct device_attribute *attr, const char *buf, size_t count)
183 struct panel_drv_data *ddata = dev_get_drvdata(dev);
187 ret = kstrtoint(buf, 0, &val);
193 ret = tpo_td043_write_mirror(ddata->spi, ddata->hmirror, val);
197 ddata->vmirror = val;
202 static ssize_t tpo_td043_mode_show(struct device *dev,
203 struct device_attribute *attr, char *buf)
205 struct panel_drv_data *ddata = dev_get_drvdata(dev);
207 return snprintf(buf, PAGE_SIZE, "%d\n", ddata->mode);
210 static ssize_t tpo_td043_mode_store(struct device *dev,
211 struct device_attribute *attr, const char *buf, size_t count)
213 struct panel_drv_data *ddata = dev_get_drvdata(dev);
217 ret = kstrtol(buf, 0, &val);
218 if (ret != 0 || val & ~7)
223 val |= TPO_R02_NCLK_RISING;
224 tpo_td043_write(ddata->spi, 2, val);
229 static ssize_t tpo_td043_gamma_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
232 struct panel_drv_data *ddata = dev_get_drvdata(dev);
237 for (i = 0; i < ARRAY_SIZE(ddata->gamma); i++) {
238 ret = snprintf(buf + len, PAGE_SIZE - len, "%u ",
249 static ssize_t tpo_td043_gamma_store(struct device *dev,
250 struct device_attribute *attr, const char *buf, size_t count)
252 struct panel_drv_data *ddata = dev_get_drvdata(dev);
257 ret = sscanf(buf, "%u %u %u %u %u %u %u %u %u %u %u %u",
258 &g[0], &g[1], &g[2], &g[3], &g[4], &g[5],
259 &g[6], &g[7], &g[8], &g[9], &g[10], &g[11]);
264 for (i = 0; i < 12; i++)
265 ddata->gamma[i] = g[i];
267 tpo_td043_write_gamma(ddata->spi, ddata->gamma);
272 static DEVICE_ATTR(vmirror, S_IRUGO | S_IWUSR,
273 tpo_td043_vmirror_show, tpo_td043_vmirror_store);
274 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
275 tpo_td043_mode_show, tpo_td043_mode_store);
276 static DEVICE_ATTR(gamma, S_IRUGO | S_IWUSR,
277 tpo_td043_gamma_show, tpo_td043_gamma_store);
279 static struct attribute *tpo_td043_attrs[] = {
280 &dev_attr_vmirror.attr,
282 &dev_attr_gamma.attr,
286 static struct attribute_group tpo_td043_attr_group = {
287 .attrs = tpo_td043_attrs,
290 static int tpo_td043_power_on(struct panel_drv_data *ddata)
294 if (ddata->powered_on)
297 r = regulator_enable(ddata->vcc_reg);
301 /* wait for panel to stabilize */
304 if (gpio_is_valid(ddata->nreset_gpio))
305 gpio_set_value(ddata->nreset_gpio, 1);
307 tpo_td043_write(ddata->spi, 2,
308 TPO_R02_MODE(ddata->mode) | TPO_R02_NCLK_RISING);
309 tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_NORMAL);
310 tpo_td043_write(ddata->spi, 0x20, 0xf0);
311 tpo_td043_write(ddata->spi, 0x21, 0xf0);
312 tpo_td043_write_mirror(ddata->spi, ddata->hmirror,
314 tpo_td043_write_gamma(ddata->spi, ddata->gamma);
316 ddata->powered_on = 1;
320 static void tpo_td043_power_off(struct panel_drv_data *ddata)
322 if (!ddata->powered_on)
325 tpo_td043_write(ddata->spi, 3,
326 TPO_R03_VAL_STANDBY | TPO_R03_EN_PWM);
328 if (gpio_is_valid(ddata->nreset_gpio))
329 gpio_set_value(ddata->nreset_gpio, 0);
331 /* wait for at least 2 vsyncs before cutting off power */
334 tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_STANDBY);
336 regulator_disable(ddata->vcc_reg);
338 ddata->powered_on = 0;
341 static int tpo_td043_connect(struct omap_dss_device *dssdev)
343 struct panel_drv_data *ddata = to_panel_data(dssdev);
344 struct omap_dss_device *in = ddata->in;
347 if (omapdss_device_is_connected(dssdev))
350 r = in->ops.dpi->connect(in, dssdev);
357 static void tpo_td043_disconnect(struct omap_dss_device *dssdev)
359 struct panel_drv_data *ddata = to_panel_data(dssdev);
360 struct omap_dss_device *in = ddata->in;
362 if (!omapdss_device_is_connected(dssdev))
365 in->ops.dpi->disconnect(in, dssdev);
368 static int tpo_td043_enable(struct omap_dss_device *dssdev)
370 struct panel_drv_data *ddata = to_panel_data(dssdev);
371 struct omap_dss_device *in = ddata->in;
374 if (!omapdss_device_is_connected(dssdev))
377 if (omapdss_device_is_enabled(dssdev))
380 if (ddata->data_lines)
381 in->ops.dpi->set_data_lines(in, ddata->data_lines);
382 in->ops.dpi->set_timings(in, &ddata->videomode);
384 r = in->ops.dpi->enable(in);
389 * If we are resuming from system suspend, SPI clocks might not be
390 * enabled yet, so we'll program the LCD from SPI PM resume callback.
392 if (!ddata->spi_suspended) {
393 r = tpo_td043_power_on(ddata);
395 in->ops.dpi->disable(in);
400 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
405 static void tpo_td043_disable(struct omap_dss_device *dssdev)
407 struct panel_drv_data *ddata = to_panel_data(dssdev);
408 struct omap_dss_device *in = ddata->in;
410 if (!omapdss_device_is_enabled(dssdev))
413 in->ops.dpi->disable(in);
415 if (!ddata->spi_suspended)
416 tpo_td043_power_off(ddata);
418 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
421 static void tpo_td043_set_timings(struct omap_dss_device *dssdev,
422 struct omap_video_timings *timings)
424 struct panel_drv_data *ddata = to_panel_data(dssdev);
425 struct omap_dss_device *in = ddata->in;
427 ddata->videomode = *timings;
428 dssdev->panel.timings = *timings;
430 in->ops.dpi->set_timings(in, timings);
433 static void tpo_td043_get_timings(struct omap_dss_device *dssdev,
434 struct omap_video_timings *timings)
436 struct panel_drv_data *ddata = to_panel_data(dssdev);
438 *timings = ddata->videomode;
441 static int tpo_td043_check_timings(struct omap_dss_device *dssdev,
442 struct omap_video_timings *timings)
444 struct panel_drv_data *ddata = to_panel_data(dssdev);
445 struct omap_dss_device *in = ddata->in;
447 return in->ops.dpi->check_timings(in, timings);
450 static struct omap_dss_driver tpo_td043_ops = {
451 .connect = tpo_td043_connect,
452 .disconnect = tpo_td043_disconnect,
454 .enable = tpo_td043_enable,
455 .disable = tpo_td043_disable,
457 .set_timings = tpo_td043_set_timings,
458 .get_timings = tpo_td043_get_timings,
459 .check_timings = tpo_td043_check_timings,
461 .set_mirror = tpo_td043_set_hmirror,
462 .get_mirror = tpo_td043_get_hmirror,
464 .get_resolution = omapdss_default_get_resolution,
468 static int tpo_td043_probe_pdata(struct spi_device *spi)
470 const struct panel_tpo_td043mtea1_platform_data *pdata;
471 struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
472 struct omap_dss_device *dssdev, *in;
474 pdata = dev_get_platdata(&spi->dev);
476 ddata->nreset_gpio = pdata->nreset_gpio;
478 in = omap_dss_find_output(pdata->source);
480 dev_err(&spi->dev, "failed to find video source '%s'\n",
482 return -EPROBE_DEFER;
486 ddata->data_lines = pdata->data_lines;
488 dssdev = &ddata->dssdev;
489 dssdev->name = pdata->name;
494 static int tpo_td043_probe_of(struct spi_device *spi)
496 struct device_node *node = spi->dev.of_node;
497 struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
498 struct omap_dss_device *in;
501 gpio = of_get_named_gpio(node, "reset-gpios", 0);
502 if (!gpio_is_valid(gpio)) {
503 dev_err(&spi->dev, "failed to parse enable gpio\n");
506 ddata->nreset_gpio = gpio;
508 in = omapdss_of_find_source_for_first_ep(node);
510 dev_err(&spi->dev, "failed to find video source\n");
519 static int tpo_td043_probe(struct spi_device *spi)
521 struct panel_drv_data *ddata;
522 struct omap_dss_device *dssdev;
525 dev_dbg(&spi->dev, "%s\n", __func__);
527 spi->bits_per_word = 16;
528 spi->mode = SPI_MODE_0;
532 dev_err(&spi->dev, "spi_setup failed: %d\n", r);
536 ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
540 dev_set_drvdata(&spi->dev, ddata);
544 if (dev_get_platdata(&spi->dev)) {
545 r = tpo_td043_probe_pdata(spi);
548 } else if (spi->dev.of_node) {
549 r = tpo_td043_probe_of(spi);
556 ddata->mode = TPO_R02_MODE_800x480;
557 memcpy(ddata->gamma, tpo_td043_def_gamma, sizeof(ddata->gamma));
559 ddata->vcc_reg = devm_regulator_get(&spi->dev, "vcc");
560 if (IS_ERR(ddata->vcc_reg)) {
561 dev_err(&spi->dev, "failed to get LCD VCC regulator\n");
562 r = PTR_ERR(ddata->vcc_reg);
566 if (gpio_is_valid(ddata->nreset_gpio)) {
567 r = devm_gpio_request_one(&spi->dev,
568 ddata->nreset_gpio, GPIOF_OUT_INIT_LOW,
571 dev_err(&spi->dev, "couldn't request reset GPIO\n");
576 r = sysfs_create_group(&spi->dev.kobj, &tpo_td043_attr_group);
578 dev_err(&spi->dev, "failed to create sysfs files\n");
582 ddata->videomode = tpo_td043_timings;
584 dssdev = &ddata->dssdev;
585 dssdev->dev = &spi->dev;
586 dssdev->driver = &tpo_td043_ops;
587 dssdev->type = OMAP_DISPLAY_TYPE_DPI;
588 dssdev->owner = THIS_MODULE;
589 dssdev->panel.timings = ddata->videomode;
591 r = omapdss_register_display(dssdev);
593 dev_err(&spi->dev, "Failed to register panel\n");
600 sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
604 omap_dss_put_device(ddata->in);
608 static int tpo_td043_remove(struct spi_device *spi)
610 struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
611 struct omap_dss_device *dssdev = &ddata->dssdev;
612 struct omap_dss_device *in = ddata->in;
614 dev_dbg(&ddata->spi->dev, "%s\n", __func__);
616 omapdss_unregister_display(dssdev);
618 tpo_td043_disable(dssdev);
619 tpo_td043_disconnect(dssdev);
621 omap_dss_put_device(in);
623 sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
628 #ifdef CONFIG_PM_SLEEP
629 static int tpo_td043_spi_suspend(struct device *dev)
631 struct panel_drv_data *ddata = dev_get_drvdata(dev);
633 dev_dbg(dev, "tpo_td043_spi_suspend, tpo %p\n", ddata);
635 ddata->power_on_resume = ddata->powered_on;
636 tpo_td043_power_off(ddata);
637 ddata->spi_suspended = 1;
642 static int tpo_td043_spi_resume(struct device *dev)
644 struct panel_drv_data *ddata = dev_get_drvdata(dev);
647 dev_dbg(dev, "tpo_td043_spi_resume\n");
649 if (ddata->power_on_resume) {
650 ret = tpo_td043_power_on(ddata);
654 ddata->spi_suspended = 0;
660 static SIMPLE_DEV_PM_OPS(tpo_td043_spi_pm,
661 tpo_td043_spi_suspend, tpo_td043_spi_resume);
663 static const struct of_device_id tpo_td043_of_match[] = {
664 { .compatible = "omapdss,tpo,td043mtea1", },
668 MODULE_DEVICE_TABLE(of, tpo_td043_of_match);
670 static struct spi_driver tpo_td043_spi_driver = {
672 .name = "panel-tpo-td043mtea1",
673 .pm = &tpo_td043_spi_pm,
674 .of_match_table = tpo_td043_of_match,
675 .suppress_bind_attrs = true,
677 .probe = tpo_td043_probe,
678 .remove = tpo_td043_remove,
681 module_spi_driver(tpo_td043_spi_driver);
683 MODULE_ALIAS("spi:tpo,td043mtea1");
684 MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
685 MODULE_DESCRIPTION("TPO TD043MTEA1 LCD Driver");
686 MODULE_LICENSE("GPL");