x86/boot: Rename overlapping memcpy() to memmove()
[linux/fpc-iii.git] / drivers / video / fbdev / omap2 / omapfb / displays / panel-tpo-td043mtea1.c
blob68e3b68a29200675aed79653af0c6ccf4a27e852
1 /*
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 | \
47 TPO_R03_SOFTWARE_CTL)
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;
62 int data_lines;
64 struct spi_device *spi;
65 struct regulator *vcc_reg;
66 int nreset_gpio;
67 u16 gamma[12];
68 u32 mode;
69 u32 hmirror:1;
70 u32 vmirror:1;
71 u32 powered_on:1;
72 u32 spi_suspended:1;
73 u32 power_on_resume:1;
76 static const struct omap_video_timings tpo_td043_timings = {
77 .x_res = 800,
78 .y_res = 480,
80 .pixelclock = 36000000,
82 .hsw = 1,
83 .hfp = 68,
84 .hbp = 214,
86 .vsw = 1,
87 .vfp = 39,
88 .vbp = 34,
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;
103 u16 w;
104 int r;
106 spi_message_init(&m);
108 memset(&xfer, 0, sizeof(xfer));
110 w = ((u16)addr << 10) | (1 << 8) | data;
111 xfer.tx_buf = &w;
112 xfer.bits_per_word = 16;
113 xfer.len = 2;
114 spi_message_add_tail(&xfer, &m);
116 r = spi_sync(spi, &m);
117 if (r < 0)
118 dev_warn(&spi->dev, "failed to write to LCD reg (%d)\n", r);
119 return r;
122 static void tpo_td043_write_gamma(struct spi_device *spi, u16 gamma[12])
124 u8 i, val;
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;
148 if (h)
149 reg4 &= ~TPO_R04_NFLIP_H;
150 if (v)
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,
162 ddata->vmirror);
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);
184 int val;
185 int ret;
187 ret = kstrtoint(buf, 0, &val);
188 if (ret < 0)
189 return ret;
191 val = !!val;
193 ret = tpo_td043_write_mirror(ddata->spi, ddata->hmirror, val);
194 if (ret < 0)
195 return ret;
197 ddata->vmirror = val;
199 return count;
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);
214 long val;
215 int ret;
217 ret = kstrtol(buf, 0, &val);
218 if (ret != 0 || val & ~7)
219 return -EINVAL;
221 ddata->mode = val;
223 val |= TPO_R02_NCLK_RISING;
224 tpo_td043_write(ddata->spi, 2, val);
226 return count;
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);
233 ssize_t len = 0;
234 int ret;
235 int i;
237 for (i = 0; i < ARRAY_SIZE(ddata->gamma); i++) {
238 ret = snprintf(buf + len, PAGE_SIZE - len, "%u ",
239 ddata->gamma[i]);
240 if (ret < 0)
241 return ret;
242 len += ret;
244 buf[len - 1] = '\n';
246 return len;
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);
253 unsigned int g[12];
254 int ret;
255 int i;
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]);
261 if (ret != 12)
262 return -EINVAL;
264 for (i = 0; i < 12; i++)
265 ddata->gamma[i] = g[i];
267 tpo_td043_write_gamma(ddata->spi, ddata->gamma);
269 return count;
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,
281 &dev_attr_mode.attr,
282 &dev_attr_gamma.attr,
283 NULL,
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)
292 int r;
294 if (ddata->powered_on)
295 return 0;
297 r = regulator_enable(ddata->vcc_reg);
298 if (r != 0)
299 return r;
301 /* wait for panel to stabilize */
302 msleep(160);
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,
313 ddata->vmirror);
314 tpo_td043_write_gamma(ddata->spi, ddata->gamma);
316 ddata->powered_on = 1;
317 return 0;
320 static void tpo_td043_power_off(struct panel_drv_data *ddata)
322 if (!ddata->powered_on)
323 return;
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 */
332 msleep(50);
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;
345 int r;
347 if (omapdss_device_is_connected(dssdev))
348 return 0;
350 r = in->ops.dpi->connect(in, dssdev);
351 if (r)
352 return r;
354 return 0;
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))
363 return;
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;
372 int r;
374 if (!omapdss_device_is_connected(dssdev))
375 return -ENODEV;
377 if (omapdss_device_is_enabled(dssdev))
378 return 0;
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);
385 if (r)
386 return r;
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);
394 if (r) {
395 in->ops.dpi->disable(in);
396 return r;
400 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
402 return 0;
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))
411 return;
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);
479 if (in == NULL) {
480 dev_err(&spi->dev, "failed to find video source '%s'\n",
481 pdata->source);
482 return -EPROBE_DEFER;
484 ddata->in = in;
486 ddata->data_lines = pdata->data_lines;
488 dssdev = &ddata->dssdev;
489 dssdev->name = pdata->name;
491 return 0;
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;
499 int gpio;
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");
504 return gpio;
506 ddata->nreset_gpio = gpio;
508 in = omapdss_of_find_source_for_first_ep(node);
509 if (IS_ERR(in)) {
510 dev_err(&spi->dev, "failed to find video source\n");
511 return PTR_ERR(in);
514 ddata->in = in;
516 return 0;
519 static int tpo_td043_probe(struct spi_device *spi)
521 struct panel_drv_data *ddata;
522 struct omap_dss_device *dssdev;
523 int r;
525 dev_dbg(&spi->dev, "%s\n", __func__);
527 spi->bits_per_word = 16;
528 spi->mode = SPI_MODE_0;
530 r = spi_setup(spi);
531 if (r < 0) {
532 dev_err(&spi->dev, "spi_setup failed: %d\n", r);
533 return r;
536 ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
537 if (ddata == NULL)
538 return -ENOMEM;
540 dev_set_drvdata(&spi->dev, ddata);
542 ddata->spi = spi;
544 if (dev_get_platdata(&spi->dev)) {
545 r = tpo_td043_probe_pdata(spi);
546 if (r)
547 return r;
548 } else if (spi->dev.of_node) {
549 r = tpo_td043_probe_of(spi);
550 if (r)
551 return r;
552 } else {
553 return -ENODEV;
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);
563 goto err_regulator;
566 if (gpio_is_valid(ddata->nreset_gpio)) {
567 r = devm_gpio_request_one(&spi->dev,
568 ddata->nreset_gpio, GPIOF_OUT_INIT_LOW,
569 "lcd reset");
570 if (r < 0) {
571 dev_err(&spi->dev, "couldn't request reset GPIO\n");
572 goto err_gpio_req;
576 r = sysfs_create_group(&spi->dev.kobj, &tpo_td043_attr_group);
577 if (r) {
578 dev_err(&spi->dev, "failed to create sysfs files\n");
579 goto err_sysfs;
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);
592 if (r) {
593 dev_err(&spi->dev, "Failed to register panel\n");
594 goto err_reg;
597 return 0;
599 err_reg:
600 sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
601 err_sysfs:
602 err_gpio_req:
603 err_regulator:
604 omap_dss_put_device(ddata->in);
605 return r;
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);
625 return 0;
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;
639 return 0;
642 static int tpo_td043_spi_resume(struct device *dev)
644 struct panel_drv_data *ddata = dev_get_drvdata(dev);
645 int ret;
647 dev_dbg(dev, "tpo_td043_spi_resume\n");
649 if (ddata->power_on_resume) {
650 ret = tpo_td043_power_on(ddata);
651 if (ret)
652 return ret;
654 ddata->spi_suspended = 0;
656 return 0;
658 #endif
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 = {
671 .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");