WIP FPC-III support
[linux/fpc-iii.git] / drivers / video / backlight / ltv350qv.c
blob5cbf621e48bd3be2e7e71ef8534a3b9ee52f45b2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Power control for Samsung LTV350QV Quarter VGA LCD Panel
5 * Copyright (C) 2006, 2007 Atmel Corporation
6 */
7 #include <linux/delay.h>
8 #include <linux/err.h>
9 #include <linux/fb.h>
10 #include <linux/init.h>
11 #include <linux/lcd.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
16 #include "ltv350qv.h"
18 #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL)
20 struct ltv350qv {
21 struct spi_device *spi;
22 u8 *buffer;
23 int power;
24 struct lcd_device *ld;
28 * The power-on and power-off sequences are taken from the
29 * LTV350QV-F04 data sheet from Samsung. The register definitions are
30 * taken from the S6F2002 command list also from Samsung. Both
31 * documents are distributed with the AVR32 Linux BSP CD from Atmel.
33 * There's still some voodoo going on here, but it's a lot better than
34 * in the first incarnation of the driver where all we had was the raw
35 * numbers from the initialization sequence.
37 static int ltv350qv_write_reg(struct ltv350qv *lcd, u8 reg, u16 val)
39 struct spi_message msg;
40 struct spi_transfer index_xfer = {
41 .len = 3,
42 .cs_change = 1,
44 struct spi_transfer value_xfer = {
45 .len = 3,
48 spi_message_init(&msg);
50 /* register index */
51 lcd->buffer[0] = LTV_OPC_INDEX;
52 lcd->buffer[1] = 0x00;
53 lcd->buffer[2] = reg & 0x7f;
54 index_xfer.tx_buf = lcd->buffer;
55 spi_message_add_tail(&index_xfer, &msg);
57 /* register value */
58 lcd->buffer[4] = LTV_OPC_DATA;
59 lcd->buffer[5] = val >> 8;
60 lcd->buffer[6] = val;
61 value_xfer.tx_buf = lcd->buffer + 4;
62 spi_message_add_tail(&value_xfer, &msg);
64 return spi_sync(lcd->spi, &msg);
67 /* The comments are taken straight from the data sheet */
68 static int ltv350qv_power_on(struct ltv350qv *lcd)
70 int ret;
72 /* Power On Reset Display off State */
73 if (ltv350qv_write_reg(lcd, LTV_PWRCTL1, 0x0000))
74 goto err;
75 usleep_range(15000, 16000);
77 /* Power Setting Function 1 */
78 if (ltv350qv_write_reg(lcd, LTV_PWRCTL1, LTV_VCOM_DISABLE))
79 goto err;
80 if (ltv350qv_write_reg(lcd, LTV_PWRCTL2, LTV_VCOML_ENABLE))
81 goto err_power1;
83 /* Power Setting Function 2 */
84 if (ltv350qv_write_reg(lcd, LTV_PWRCTL1,
85 LTV_VCOM_DISABLE | LTV_DRIVE_CURRENT(5)
86 | LTV_SUPPLY_CURRENT(5)))
87 goto err_power2;
89 msleep(55);
91 /* Instruction Setting */
92 ret = ltv350qv_write_reg(lcd, LTV_IFCTL,
93 LTV_NMD | LTV_REV | LTV_NL(0x1d));
94 ret |= ltv350qv_write_reg(lcd, LTV_DATACTL,
95 LTV_DS_SAME | LTV_CHS_480
96 | LTV_DF_RGB | LTV_RGB_BGR);
97 ret |= ltv350qv_write_reg(lcd, LTV_ENTRY_MODE,
98 LTV_VSPL_ACTIVE_LOW
99 | LTV_HSPL_ACTIVE_LOW
100 | LTV_DPL_SAMPLE_RISING
101 | LTV_EPL_ACTIVE_LOW
102 | LTV_SS_RIGHT_TO_LEFT);
103 ret |= ltv350qv_write_reg(lcd, LTV_GATECTL1, LTV_CLW(3));
104 ret |= ltv350qv_write_reg(lcd, LTV_GATECTL2,
105 LTV_NW_INV_1LINE | LTV_FWI(3));
106 ret |= ltv350qv_write_reg(lcd, LTV_VBP, 0x000a);
107 ret |= ltv350qv_write_reg(lcd, LTV_HBP, 0x0021);
108 ret |= ltv350qv_write_reg(lcd, LTV_SOTCTL, LTV_SDT(3) | LTV_EQ(0));
109 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(0), 0x0103);
110 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(1), 0x0301);
111 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(2), 0x1f0f);
112 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(3), 0x1f0f);
113 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(4), 0x0707);
114 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(5), 0x0307);
115 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(6), 0x0707);
116 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(7), 0x0000);
117 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(8), 0x0004);
118 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(9), 0x0000);
119 if (ret)
120 goto err_settings;
122 /* Wait more than 2 frames */
123 msleep(20);
125 /* Display On Sequence */
126 ret = ltv350qv_write_reg(lcd, LTV_PWRCTL1,
127 LTV_VCOM_DISABLE | LTV_VCOMOUT_ENABLE
128 | LTV_POWER_ON | LTV_DRIVE_CURRENT(5)
129 | LTV_SUPPLY_CURRENT(5));
130 ret |= ltv350qv_write_reg(lcd, LTV_GATECTL2,
131 LTV_NW_INV_1LINE | LTV_DSC | LTV_FWI(3));
132 if (ret)
133 goto err_disp_on;
135 /* Display should now be ON. Phew. */
136 return 0;
138 err_disp_on:
140 * Try to recover. Error handling probably isn't very useful
141 * at this point, just make a best effort to switch the panel
142 * off.
144 ltv350qv_write_reg(lcd, LTV_PWRCTL1,
145 LTV_VCOM_DISABLE | LTV_DRIVE_CURRENT(5)
146 | LTV_SUPPLY_CURRENT(5));
147 ltv350qv_write_reg(lcd, LTV_GATECTL2,
148 LTV_NW_INV_1LINE | LTV_FWI(3));
149 err_settings:
150 err_power2:
151 err_power1:
152 ltv350qv_write_reg(lcd, LTV_PWRCTL2, 0x0000);
153 usleep_range(1000, 1100);
154 err:
155 ltv350qv_write_reg(lcd, LTV_PWRCTL1, LTV_VCOM_DISABLE);
156 return -EIO;
159 static int ltv350qv_power_off(struct ltv350qv *lcd)
161 int ret;
163 /* Display Off Sequence */
164 ret = ltv350qv_write_reg(lcd, LTV_PWRCTL1,
165 LTV_VCOM_DISABLE
166 | LTV_DRIVE_CURRENT(5)
167 | LTV_SUPPLY_CURRENT(5));
168 ret |= ltv350qv_write_reg(lcd, LTV_GATECTL2,
169 LTV_NW_INV_1LINE | LTV_FWI(3));
171 /* Power down setting 1 */
172 ret |= ltv350qv_write_reg(lcd, LTV_PWRCTL2, 0x0000);
174 /* Wait at least 1 ms */
175 usleep_range(1000, 1100);
177 /* Power down setting 2 */
178 ret |= ltv350qv_write_reg(lcd, LTV_PWRCTL1, LTV_VCOM_DISABLE);
181 * No point in trying to recover here. If we can't switch the
182 * panel off, what are we supposed to do other than inform the
183 * user about the failure?
185 if (ret)
186 return -EIO;
188 /* Display power should now be OFF */
189 return 0;
192 static int ltv350qv_power(struct ltv350qv *lcd, int power)
194 int ret = 0;
196 if (POWER_IS_ON(power) && !POWER_IS_ON(lcd->power))
197 ret = ltv350qv_power_on(lcd);
198 else if (!POWER_IS_ON(power) && POWER_IS_ON(lcd->power))
199 ret = ltv350qv_power_off(lcd);
201 if (!ret)
202 lcd->power = power;
204 return ret;
207 static int ltv350qv_set_power(struct lcd_device *ld, int power)
209 struct ltv350qv *lcd = lcd_get_data(ld);
211 return ltv350qv_power(lcd, power);
214 static int ltv350qv_get_power(struct lcd_device *ld)
216 struct ltv350qv *lcd = lcd_get_data(ld);
218 return lcd->power;
221 static struct lcd_ops ltv_ops = {
222 .get_power = ltv350qv_get_power,
223 .set_power = ltv350qv_set_power,
226 static int ltv350qv_probe(struct spi_device *spi)
228 struct ltv350qv *lcd;
229 struct lcd_device *ld;
230 int ret;
232 lcd = devm_kzalloc(&spi->dev, sizeof(struct ltv350qv), GFP_KERNEL);
233 if (!lcd)
234 return -ENOMEM;
236 lcd->spi = spi;
237 lcd->power = FB_BLANK_POWERDOWN;
238 lcd->buffer = devm_kzalloc(&spi->dev, 8, GFP_KERNEL);
239 if (!lcd->buffer)
240 return -ENOMEM;
242 ld = devm_lcd_device_register(&spi->dev, "ltv350qv", &spi->dev, lcd,
243 &ltv_ops);
244 if (IS_ERR(ld))
245 return PTR_ERR(ld);
247 lcd->ld = ld;
249 ret = ltv350qv_power(lcd, FB_BLANK_UNBLANK);
250 if (ret)
251 return ret;
253 spi_set_drvdata(spi, lcd);
255 return 0;
258 static int ltv350qv_remove(struct spi_device *spi)
260 struct ltv350qv *lcd = spi_get_drvdata(spi);
262 ltv350qv_power(lcd, FB_BLANK_POWERDOWN);
263 return 0;
266 #ifdef CONFIG_PM_SLEEP
267 static int ltv350qv_suspend(struct device *dev)
269 struct ltv350qv *lcd = dev_get_drvdata(dev);
271 return ltv350qv_power(lcd, FB_BLANK_POWERDOWN);
274 static int ltv350qv_resume(struct device *dev)
276 struct ltv350qv *lcd = dev_get_drvdata(dev);
278 return ltv350qv_power(lcd, FB_BLANK_UNBLANK);
280 #endif
282 static SIMPLE_DEV_PM_OPS(ltv350qv_pm_ops, ltv350qv_suspend, ltv350qv_resume);
284 /* Power down all displays on reboot, poweroff or halt */
285 static void ltv350qv_shutdown(struct spi_device *spi)
287 struct ltv350qv *lcd = spi_get_drvdata(spi);
289 ltv350qv_power(lcd, FB_BLANK_POWERDOWN);
292 static struct spi_driver ltv350qv_driver = {
293 .driver = {
294 .name = "ltv350qv",
295 .pm = &ltv350qv_pm_ops,
298 .probe = ltv350qv_probe,
299 .remove = ltv350qv_remove,
300 .shutdown = ltv350qv_shutdown,
303 module_spi_driver(ltv350qv_driver);
305 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
306 MODULE_DESCRIPTION("Samsung LTV350QV LCD Driver");
307 MODULE_LICENSE("GPL");
308 MODULE_ALIAS("spi:ltv350qv");