1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Power control for Samsung LTV350QV Quarter VGA LCD Panel
4  *
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>
15 
16 #include "ltv350qv.h"
17 
18 #define POWER_IS_ON(pwr)	((pwr) <= FB_BLANK_NORMAL)
19 
20 struct ltv350qv {
21 	struct spi_device	*spi;
22 	u8			*buffer;
23 	int			power;
24 	struct lcd_device	*ld;
25 };
26 
27 /*
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.
32  *
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.
36  */
37 static int ltv350qv_write_reg(struct ltv350qv *lcd, u8 reg, u16 val)
38 {
39 	struct spi_message msg;
40 	struct spi_transfer index_xfer = {
41 		.len		= 3,
42 		.cs_change	= 1,
43 	};
44 	struct spi_transfer value_xfer = {
45 		.len		= 3,
46 	};
47 
48 	spi_message_init(&msg);
49 
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);
56 
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);
63 
64 	return spi_sync(lcd->spi, &msg);
65 }
66 
67 /* The comments are taken straight from the data sheet */
68 static int ltv350qv_power_on(struct ltv350qv *lcd)
69 {
70 	int ret;
71 
72 	/* Power On Reset Display off State */
73 	if (ltv350qv_write_reg(lcd, LTV_PWRCTL1, 0x0000))
74 		goto err;
75 	usleep_range(15000, 16000);
76 
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;
82 
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;
88 
89 	msleep(55);
90 
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;
121 
122 	/* Wait more than 2 frames */
123 	msleep(20);
124 
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;
134 
135 	/* Display should now be ON. Phew. */
136 	return 0;
137 
138 err_disp_on:
139 	/*
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.
143 	 */
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;
157 }
158 
159 static int ltv350qv_power_off(struct ltv350qv *lcd)
160 {
161 	int ret;
162 
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));
170 
171 	/* Power down setting 1 */
172 	ret |= ltv350qv_write_reg(lcd, LTV_PWRCTL2, 0x0000);
173 
174 	/* Wait at least 1 ms */
175 	usleep_range(1000, 1100);
176 
177 	/* Power down setting 2 */
178 	ret |= ltv350qv_write_reg(lcd, LTV_PWRCTL1, LTV_VCOM_DISABLE);
179 
180 	/*
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?
184 	 */
185 	if (ret)
186 		return -EIO;
187 
188 	/* Display power should now be OFF */
189 	return 0;
190 }
191 
192 static int ltv350qv_power(struct ltv350qv *lcd, int power)
193 {
194 	int ret = 0;
195 
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);
200 
201 	if (!ret)
202 		lcd->power = power;
203 
204 	return ret;
205 }
206 
207 static int ltv350qv_set_power(struct lcd_device *ld, int power)
208 {
209 	struct ltv350qv *lcd = lcd_get_data(ld);
210 
211 	return ltv350qv_power(lcd, power);
212 }
213 
214 static int ltv350qv_get_power(struct lcd_device *ld)
215 {
216 	struct ltv350qv *lcd = lcd_get_data(ld);
217 
218 	return lcd->power;
219 }
220 
221 static struct lcd_ops ltv_ops = {
222 	.get_power	= ltv350qv_get_power,
223 	.set_power	= ltv350qv_set_power,
224 };
225 
226 static int ltv350qv_probe(struct spi_device *spi)
227 {
228 	struct ltv350qv *lcd;
229 	struct lcd_device *ld;
230 	int ret;
231 
232 	lcd = devm_kzalloc(&spi->dev, sizeof(struct ltv350qv), GFP_KERNEL);
233 	if (!lcd)
234 		return -ENOMEM;
235 
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;
241 
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);
246 
247 	lcd->ld = ld;
248 
249 	ret = ltv350qv_power(lcd, FB_BLANK_UNBLANK);
250 	if (ret)
251 		return ret;
252 
253 	spi_set_drvdata(spi, lcd);
254 
255 	return 0;
256 }
257 
258 static int ltv350qv_remove(struct spi_device *spi)
259 {
260 	struct ltv350qv *lcd = spi_get_drvdata(spi);
261 
262 	ltv350qv_power(lcd, FB_BLANK_POWERDOWN);
263 	return 0;
264 }
265 
266 #ifdef CONFIG_PM_SLEEP
267 static int ltv350qv_suspend(struct device *dev)
268 {
269 	struct ltv350qv *lcd = dev_get_drvdata(dev);
270 
271 	return ltv350qv_power(lcd, FB_BLANK_POWERDOWN);
272 }
273 
274 static int ltv350qv_resume(struct device *dev)
275 {
276 	struct ltv350qv *lcd = dev_get_drvdata(dev);
277 
278 	return ltv350qv_power(lcd, FB_BLANK_UNBLANK);
279 }
280 #endif
281 
282 static SIMPLE_DEV_PM_OPS(ltv350qv_pm_ops, ltv350qv_suspend, ltv350qv_resume);
283 
284 /* Power down all displays on reboot, poweroff or halt */
285 static void ltv350qv_shutdown(struct spi_device *spi)
286 {
287 	struct ltv350qv *lcd = spi_get_drvdata(spi);
288 
289 	ltv350qv_power(lcd, FB_BLANK_POWERDOWN);
290 }
291 
292 static struct spi_driver ltv350qv_driver = {
293 	.driver = {
294 		.name		= "ltv350qv",
295 		.pm		= &ltv350qv_pm_ops,
296 	},
297 
298 	.probe		= ltv350qv_probe,
299 	.remove		= ltv350qv_remove,
300 	.shutdown	= ltv350qv_shutdown,
301 };
302 
303 module_spi_driver(ltv350qv_driver);
304 
305 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
306 MODULE_DESCRIPTION("Samsung LTV350QV LCD Driver");
307 MODULE_LICENSE("GPL");
308 MODULE_ALIAS("spi:ltv350qv");
309