1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * TPO TD043MTEA1 Panel driver
5 * Author: Gražvydas Ignotas <notasas@gmail.com>
6 * Converted to new DSS device model: Tomi Valkeinen <tomi.valkeinen@ti.com>
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/spi/spi.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
17 #include <video/omapfb_dss.h>
19 #define TPO_R02_MODE(x) ((x) & 7)
20 #define TPO_R02_MODE_800x480 7
21 #define TPO_R02_NCLK_RISING BIT(3)
22 #define TPO_R02_HSYNC_HIGH BIT(4)
23 #define TPO_R02_VSYNC_HIGH BIT(5)
25 #define TPO_R03_NSTANDBY BIT(0)
26 #define TPO_R03_EN_CP_CLK BIT(1)
27 #define TPO_R03_EN_VGL_PUMP BIT(2)
28 #define TPO_R03_EN_PWM BIT(3)
29 #define TPO_R03_DRIVING_CAP_100 BIT(4)
30 #define TPO_R03_EN_PRE_CHARGE BIT(6)
31 #define TPO_R03_SOFTWARE_CTL BIT(7)
33 #define TPO_R04_NFLIP_H BIT(0)
34 #define TPO_R04_NFLIP_V BIT(1)
35 #define TPO_R04_CP_CLK_FREQ_1H BIT(2)
36 #define TPO_R04_VGL_FREQ_1H BIT(4)
38 #define TPO_R03_VAL_NORMAL (TPO_R03_NSTANDBY | TPO_R03_EN_CP_CLK | \
39 TPO_R03_EN_VGL_PUMP | TPO_R03_EN_PWM | \
40 TPO_R03_DRIVING_CAP_100 | TPO_R03_EN_PRE_CHARGE | \
43 #define TPO_R03_VAL_STANDBY (TPO_R03_DRIVING_CAP_100 | \
44 TPO_R03_EN_PRE_CHARGE | TPO_R03_SOFTWARE_CTL)
46 static const u16 tpo_td043_def_gamma
[12] = {
47 105, 315, 381, 431, 490, 537, 579, 686, 780, 837, 880, 1023
50 struct panel_drv_data
{
51 struct omap_dss_device dssdev
;
52 struct omap_dss_device
*in
;
54 struct omap_video_timings videomode
;
58 struct spi_device
*spi
;
59 struct regulator
*vcc_reg
;
60 struct gpio_desc
*reset_gpio
;
67 u32 power_on_resume
:1;
70 static const struct omap_video_timings tpo_td043_timings
= {
74 .pixelclock
= 36000000,
84 .vsync_level
= OMAPDSS_SIG_ACTIVE_LOW
,
85 .hsync_level
= OMAPDSS_SIG_ACTIVE_LOW
,
86 .data_pclk_edge
= OMAPDSS_DRIVE_SIG_FALLING_EDGE
,
87 .de_level
= OMAPDSS_SIG_ACTIVE_HIGH
,
88 .sync_pclk_edge
= OMAPDSS_DRIVE_SIG_RISING_EDGE
,
91 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
93 static int tpo_td043_write(struct spi_device
*spi
, u8 addr
, u8 data
)
96 struct spi_transfer xfer
;
100 spi_message_init(&m
);
102 memset(&xfer
, 0, sizeof(xfer
));
104 w
= ((u16
)addr
<< 10) | (1 << 8) | data
;
106 xfer
.bits_per_word
= 16;
108 spi_message_add_tail(&xfer
, &m
);
110 r
= spi_sync(spi
, &m
);
112 dev_warn(&spi
->dev
, "failed to write to LCD reg (%d)\n", r
);
116 static void tpo_td043_write_gamma(struct spi_device
*spi
, u16 gamma
[12])
120 /* gamma bits [9:8] */
121 for (val
= i
= 0; i
< 4; i
++)
122 val
|= (gamma
[i
] & 0x300) >> ((i
+ 1) * 2);
123 tpo_td043_write(spi
, 0x11, val
);
125 for (val
= i
= 0; i
< 4; i
++)
126 val
|= (gamma
[i
+4] & 0x300) >> ((i
+ 1) * 2);
127 tpo_td043_write(spi
, 0x12, val
);
129 for (val
= i
= 0; i
< 4; i
++)
130 val
|= (gamma
[i
+8] & 0x300) >> ((i
+ 1) * 2);
131 tpo_td043_write(spi
, 0x13, val
);
133 /* gamma bits [7:0] */
134 for (val
= i
= 0; i
< 12; i
++)
135 tpo_td043_write(spi
, 0x14 + i
, gamma
[i
] & 0xff);
138 static int tpo_td043_write_mirror(struct spi_device
*spi
, bool h
, bool v
)
140 u8 reg4
= TPO_R04_NFLIP_H
| TPO_R04_NFLIP_V
|
141 TPO_R04_CP_CLK_FREQ_1H
| TPO_R04_VGL_FREQ_1H
;
143 reg4
&= ~TPO_R04_NFLIP_H
;
145 reg4
&= ~TPO_R04_NFLIP_V
;
147 return tpo_td043_write(spi
, 4, reg4
);
150 static int tpo_td043_set_hmirror(struct omap_dss_device
*dssdev
, bool enable
)
152 struct panel_drv_data
*ddata
= dev_get_drvdata(dssdev
->dev
);
154 ddata
->hmirror
= enable
;
155 return tpo_td043_write_mirror(ddata
->spi
, ddata
->hmirror
,
159 static bool tpo_td043_get_hmirror(struct omap_dss_device
*dssdev
)
161 struct panel_drv_data
*ddata
= dev_get_drvdata(dssdev
->dev
);
163 return ddata
->hmirror
;
166 static ssize_t
tpo_td043_vmirror_show(struct device
*dev
,
167 struct device_attribute
*attr
, char *buf
)
169 struct panel_drv_data
*ddata
= dev_get_drvdata(dev
);
171 return sysfs_emit(buf
, "%d\n", ddata
->vmirror
);
174 static ssize_t
tpo_td043_vmirror_store(struct device
*dev
,
175 struct device_attribute
*attr
, const char *buf
, size_t count
)
177 struct panel_drv_data
*ddata
= dev_get_drvdata(dev
);
181 ret
= kstrtoint(buf
, 0, &val
);
187 ret
= tpo_td043_write_mirror(ddata
->spi
, ddata
->hmirror
, val
);
191 ddata
->vmirror
= val
;
196 static ssize_t
tpo_td043_mode_show(struct device
*dev
,
197 struct device_attribute
*attr
, char *buf
)
199 struct panel_drv_data
*ddata
= dev_get_drvdata(dev
);
201 return sysfs_emit(buf
, "%d\n", ddata
->mode
);
204 static ssize_t
tpo_td043_mode_store(struct device
*dev
,
205 struct device_attribute
*attr
, const char *buf
, size_t count
)
207 struct panel_drv_data
*ddata
= dev_get_drvdata(dev
);
211 ret
= kstrtol(buf
, 0, &val
);
212 if (ret
!= 0 || val
& ~7)
217 val
|= TPO_R02_NCLK_RISING
;
218 tpo_td043_write(ddata
->spi
, 2, val
);
223 static ssize_t
tpo_td043_gamma_show(struct device
*dev
,
224 struct device_attribute
*attr
, char *buf
)
226 struct panel_drv_data
*ddata
= dev_get_drvdata(dev
);
230 for (i
= 0; i
< ARRAY_SIZE(ddata
->gamma
); i
++)
231 len
+= sysfs_emit_at(buf
, len
, "%u ", ddata
->gamma
[i
]);
238 static ssize_t
tpo_td043_gamma_store(struct device
*dev
,
239 struct device_attribute
*attr
, const char *buf
, size_t count
)
241 struct panel_drv_data
*ddata
= dev_get_drvdata(dev
);
246 ret
= sscanf(buf
, "%u %u %u %u %u %u %u %u %u %u %u %u",
247 &g
[0], &g
[1], &g
[2], &g
[3], &g
[4], &g
[5],
248 &g
[6], &g
[7], &g
[8], &g
[9], &g
[10], &g
[11]);
253 for (i
= 0; i
< 12; i
++)
254 ddata
->gamma
[i
] = g
[i
];
256 tpo_td043_write_gamma(ddata
->spi
, ddata
->gamma
);
261 static DEVICE_ATTR(vmirror
, S_IRUGO
| S_IWUSR
,
262 tpo_td043_vmirror_show
, tpo_td043_vmirror_store
);
263 static DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
264 tpo_td043_mode_show
, tpo_td043_mode_store
);
265 static DEVICE_ATTR(gamma
, S_IRUGO
| S_IWUSR
,
266 tpo_td043_gamma_show
, tpo_td043_gamma_store
);
268 static struct attribute
*tpo_td043_attrs
[] = {
269 &dev_attr_vmirror
.attr
,
271 &dev_attr_gamma
.attr
,
275 static const struct attribute_group tpo_td043_attr_group
= {
276 .attrs
= tpo_td043_attrs
,
279 static int tpo_td043_power_on(struct panel_drv_data
*ddata
)
283 if (ddata
->powered_on
)
286 r
= regulator_enable(ddata
->vcc_reg
);
290 /* wait for panel to stabilize */
293 gpiod_set_value_cansleep(ddata
->reset_gpio
, 0);
295 tpo_td043_write(ddata
->spi
, 2,
296 TPO_R02_MODE(ddata
->mode
) | TPO_R02_NCLK_RISING
);
297 tpo_td043_write(ddata
->spi
, 3, TPO_R03_VAL_NORMAL
);
298 tpo_td043_write(ddata
->spi
, 0x20, 0xf0);
299 tpo_td043_write(ddata
->spi
, 0x21, 0xf0);
300 tpo_td043_write_mirror(ddata
->spi
, ddata
->hmirror
,
302 tpo_td043_write_gamma(ddata
->spi
, ddata
->gamma
);
304 ddata
->powered_on
= 1;
308 static void tpo_td043_power_off(struct panel_drv_data
*ddata
)
310 if (!ddata
->powered_on
)
313 tpo_td043_write(ddata
->spi
, 3,
314 TPO_R03_VAL_STANDBY
| TPO_R03_EN_PWM
);
316 gpiod_set_value_cansleep(ddata
->reset_gpio
, 1);
318 /* wait for at least 2 vsyncs before cutting off power */
321 tpo_td043_write(ddata
->spi
, 3, TPO_R03_VAL_STANDBY
);
323 regulator_disable(ddata
->vcc_reg
);
325 ddata
->powered_on
= 0;
328 static int tpo_td043_connect(struct omap_dss_device
*dssdev
)
330 struct panel_drv_data
*ddata
= to_panel_data(dssdev
);
331 struct omap_dss_device
*in
= ddata
->in
;
333 if (omapdss_device_is_connected(dssdev
))
336 return in
->ops
.dpi
->connect(in
, dssdev
);
339 static void tpo_td043_disconnect(struct omap_dss_device
*dssdev
)
341 struct panel_drv_data
*ddata
= to_panel_data(dssdev
);
342 struct omap_dss_device
*in
= ddata
->in
;
344 if (!omapdss_device_is_connected(dssdev
))
347 in
->ops
.dpi
->disconnect(in
, dssdev
);
350 static int tpo_td043_enable(struct omap_dss_device
*dssdev
)
352 struct panel_drv_data
*ddata
= to_panel_data(dssdev
);
353 struct omap_dss_device
*in
= ddata
->in
;
356 if (!omapdss_device_is_connected(dssdev
))
359 if (omapdss_device_is_enabled(dssdev
))
362 if (ddata
->data_lines
)
363 in
->ops
.dpi
->set_data_lines(in
, ddata
->data_lines
);
364 in
->ops
.dpi
->set_timings(in
, &ddata
->videomode
);
366 r
= in
->ops
.dpi
->enable(in
);
371 * If we are resuming from system suspend, SPI clocks might not be
372 * enabled yet, so we'll program the LCD from SPI PM resume callback.
374 if (!ddata
->spi_suspended
) {
375 r
= tpo_td043_power_on(ddata
);
377 in
->ops
.dpi
->disable(in
);
382 dssdev
->state
= OMAP_DSS_DISPLAY_ACTIVE
;
387 static void tpo_td043_disable(struct omap_dss_device
*dssdev
)
389 struct panel_drv_data
*ddata
= to_panel_data(dssdev
);
390 struct omap_dss_device
*in
= ddata
->in
;
392 if (!omapdss_device_is_enabled(dssdev
))
395 in
->ops
.dpi
->disable(in
);
397 if (!ddata
->spi_suspended
)
398 tpo_td043_power_off(ddata
);
400 dssdev
->state
= OMAP_DSS_DISPLAY_DISABLED
;
403 static void tpo_td043_set_timings(struct omap_dss_device
*dssdev
,
404 struct omap_video_timings
*timings
)
406 struct panel_drv_data
*ddata
= to_panel_data(dssdev
);
407 struct omap_dss_device
*in
= ddata
->in
;
409 ddata
->videomode
= *timings
;
410 dssdev
->panel
.timings
= *timings
;
412 in
->ops
.dpi
->set_timings(in
, timings
);
415 static void tpo_td043_get_timings(struct omap_dss_device
*dssdev
,
416 struct omap_video_timings
*timings
)
418 struct panel_drv_data
*ddata
= to_panel_data(dssdev
);
420 *timings
= ddata
->videomode
;
423 static int tpo_td043_check_timings(struct omap_dss_device
*dssdev
,
424 struct omap_video_timings
*timings
)
426 struct panel_drv_data
*ddata
= to_panel_data(dssdev
);
427 struct omap_dss_device
*in
= ddata
->in
;
429 return in
->ops
.dpi
->check_timings(in
, timings
);
432 static struct omap_dss_driver tpo_td043_ops
= {
433 .connect
= tpo_td043_connect
,
434 .disconnect
= tpo_td043_disconnect
,
436 .enable
= tpo_td043_enable
,
437 .disable
= tpo_td043_disable
,
439 .set_timings
= tpo_td043_set_timings
,
440 .get_timings
= tpo_td043_get_timings
,
441 .check_timings
= tpo_td043_check_timings
,
443 .set_mirror
= tpo_td043_set_hmirror
,
444 .get_mirror
= tpo_td043_get_hmirror
,
446 .get_resolution
= omapdss_default_get_resolution
,
449 static int tpo_td043_probe(struct spi_device
*spi
)
451 struct panel_drv_data
*ddata
;
452 struct omap_dss_device
*dssdev
;
455 dev_dbg(&spi
->dev
, "%s\n", __func__
);
457 if (!spi
->dev
.of_node
)
460 spi
->bits_per_word
= 16;
461 spi
->mode
= SPI_MODE_0
;
465 dev_err(&spi
->dev
, "spi_setup failed: %d\n", r
);
469 ddata
= devm_kzalloc(&spi
->dev
, sizeof(*ddata
), GFP_KERNEL
);
473 dev_set_drvdata(&spi
->dev
, ddata
);
477 ddata
->in
= omapdss_of_find_source_for_first_ep(spi
->dev
.of_node
);
478 r
= PTR_ERR_OR_ZERO(ddata
->in
);
480 dev_err(&spi
->dev
, "failed to find video source: %d\n", r
);
484 ddata
->mode
= TPO_R02_MODE_800x480
;
485 memcpy(ddata
->gamma
, tpo_td043_def_gamma
, sizeof(ddata
->gamma
));
487 ddata
->vcc_reg
= devm_regulator_get(&spi
->dev
, "vcc");
488 if (IS_ERR(ddata
->vcc_reg
)) {
489 r
= dev_err_probe(&spi
->dev
, PTR_ERR(ddata
->vcc_reg
),
490 "failed to get LCD VCC regulator\n");
494 ddata
->reset_gpio
= devm_gpiod_get(&spi
->dev
, "reset", GPIOD_OUT_HIGH
);
495 r
= PTR_ERR_OR_ZERO(ddata
->reset_gpio
);
497 dev_err(&spi
->dev
, "couldn't request reset GPIO\n");
501 gpiod_set_consumer_name(ddata
->reset_gpio
, "lcd reset");
503 r
= sysfs_create_group(&spi
->dev
.kobj
, &tpo_td043_attr_group
);
505 dev_err(&spi
->dev
, "failed to create sysfs files\n");
509 ddata
->videomode
= tpo_td043_timings
;
511 dssdev
= &ddata
->dssdev
;
512 dssdev
->dev
= &spi
->dev
;
513 dssdev
->driver
= &tpo_td043_ops
;
514 dssdev
->type
= OMAP_DISPLAY_TYPE_DPI
;
515 dssdev
->owner
= THIS_MODULE
;
516 dssdev
->panel
.timings
= ddata
->videomode
;
518 r
= omapdss_register_display(dssdev
);
520 dev_err(&spi
->dev
, "Failed to register panel\n");
527 sysfs_remove_group(&spi
->dev
.kobj
, &tpo_td043_attr_group
);
531 omap_dss_put_device(ddata
->in
);
535 static void tpo_td043_remove(struct spi_device
*spi
)
537 struct panel_drv_data
*ddata
= dev_get_drvdata(&spi
->dev
);
538 struct omap_dss_device
*dssdev
= &ddata
->dssdev
;
539 struct omap_dss_device
*in
= ddata
->in
;
541 dev_dbg(&ddata
->spi
->dev
, "%s\n", __func__
);
543 omapdss_unregister_display(dssdev
);
545 tpo_td043_disable(dssdev
);
546 tpo_td043_disconnect(dssdev
);
548 omap_dss_put_device(in
);
550 sysfs_remove_group(&spi
->dev
.kobj
, &tpo_td043_attr_group
);
553 #ifdef CONFIG_PM_SLEEP
554 static int tpo_td043_spi_suspend(struct device
*dev
)
556 struct panel_drv_data
*ddata
= dev_get_drvdata(dev
);
558 dev_dbg(dev
, "tpo_td043_spi_suspend, tpo %p\n", ddata
);
560 ddata
->power_on_resume
= ddata
->powered_on
;
561 tpo_td043_power_off(ddata
);
562 ddata
->spi_suspended
= 1;
567 static int tpo_td043_spi_resume(struct device
*dev
)
569 struct panel_drv_data
*ddata
= dev_get_drvdata(dev
);
572 dev_dbg(dev
, "tpo_td043_spi_resume\n");
574 if (ddata
->power_on_resume
) {
575 ret
= tpo_td043_power_on(ddata
);
579 ddata
->spi_suspended
= 0;
585 static SIMPLE_DEV_PM_OPS(tpo_td043_spi_pm
,
586 tpo_td043_spi_suspend
, tpo_td043_spi_resume
);
588 static const struct of_device_id tpo_td043_of_match
[] = {
589 { .compatible
= "omapdss,tpo,td043mtea1", },
593 MODULE_DEVICE_TABLE(of
, tpo_td043_of_match
);
595 static struct spi_driver tpo_td043_spi_driver
= {
597 .name
= "panel-tpo-td043mtea1",
598 .pm
= &tpo_td043_spi_pm
,
599 .of_match_table
= tpo_td043_of_match
,
600 .suppress_bind_attrs
= true,
602 .probe
= tpo_td043_probe
,
603 .remove
= tpo_td043_remove
,
606 module_spi_driver(tpo_td043_spi_driver
);
608 MODULE_ALIAS("spi:tpo,td043mtea1");
609 MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
610 MODULE_DESCRIPTION("TPO TD043MTEA1 LCD Driver");
611 MODULE_LICENSE("GPL");