1 /*
2  * l4f00242t03.c -- support for Epson L4F00242T03 LCD
3  *
4  * Copyright 2007-2009 Freescale Semiconductor, Inc. All Rights Reserved.
5  *
6  * Copyright (c) 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
7  * 	Inspired by Marek Vasut work in l4f00242t03.c
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/delay.h>
17 #include <linux/gpio.h>
18 #include <linux/lcd.h>
19 #include <linux/slab.h>
20 #include <linux/regulator/consumer.h>
21 
22 #include <linux/spi/spi.h>
23 #include <linux/spi/l4f00242t03.h>
24 
25 struct l4f00242t03_priv {
26 	struct spi_device	*spi;
27 	struct lcd_device	*ld;
28 	int lcd_state;
29 	struct regulator *io_reg;
30 	struct regulator *core_reg;
31 };
32 
33 
l4f00242t03_reset(unsigned int gpio)34 static void l4f00242t03_reset(unsigned int gpio)
35 {
36 	pr_debug("l4f00242t03_reset.\n");
37 	gpio_set_value(gpio, 1);
38 	mdelay(100);
39 	gpio_set_value(gpio, 0);
40 	mdelay(10);	/* tRES >= 100us */
41 	gpio_set_value(gpio, 1);
42 	mdelay(20);
43 }
44 
45 #define param(x) ((x) | 0x100)
46 
l4f00242t03_lcd_init(struct spi_device * spi)47 static void l4f00242t03_lcd_init(struct spi_device *spi)
48 {
49 	struct l4f00242t03_pdata *pdata = spi->dev.platform_data;
50 	struct l4f00242t03_priv *priv = dev_get_drvdata(&spi->dev);
51 	const u16 cmd[] = { 0x36, param(0), 0x3A, param(0x60) };
52 
53 	dev_dbg(&spi->dev, "initializing LCD\n");
54 
55 	if (priv->io_reg) {
56 		regulator_set_voltage(priv->io_reg, 1800000, 1800000);
57 		regulator_enable(priv->io_reg);
58 	}
59 
60 	if (priv->core_reg) {
61 		regulator_set_voltage(priv->core_reg, 2800000, 2800000);
62 		regulator_enable(priv->core_reg);
63 	}
64 
65 	l4f00242t03_reset(pdata->reset_gpio);
66 
67 	gpio_set_value(pdata->data_enable_gpio, 1);
68 	msleep(60);
69 	spi_write(spi, (const u8 *)cmd, ARRAY_SIZE(cmd) * sizeof(u16));
70 }
71 
l4f00242t03_lcd_powerdown(struct spi_device * spi)72 static void l4f00242t03_lcd_powerdown(struct spi_device *spi)
73 {
74 	struct l4f00242t03_pdata *pdata = spi->dev.platform_data;
75 	struct l4f00242t03_priv *priv = dev_get_drvdata(&spi->dev);
76 
77 	dev_dbg(&spi->dev, "Powering down LCD\n");
78 
79 	gpio_set_value(pdata->data_enable_gpio, 0);
80 
81 	if (priv->io_reg)
82 		regulator_disable(priv->io_reg);
83 
84 	if (priv->core_reg)
85 		regulator_disable(priv->core_reg);
86 }
87 
l4f00242t03_lcd_power_get(struct lcd_device * ld)88 static int l4f00242t03_lcd_power_get(struct lcd_device *ld)
89 {
90 	struct l4f00242t03_priv *priv = lcd_get_data(ld);
91 
92 	return priv->lcd_state;
93 }
94 
l4f00242t03_lcd_power_set(struct lcd_device * ld,int power)95 static int l4f00242t03_lcd_power_set(struct lcd_device *ld, int power)
96 {
97 	struct l4f00242t03_priv *priv = lcd_get_data(ld);
98 	struct spi_device *spi = priv->spi;
99 
100 	const u16 slpout = 0x11;
101 	const u16 dison = 0x29;
102 
103 	const u16 slpin = 0x10;
104 	const u16 disoff = 0x28;
105 
106 	if (power <= FB_BLANK_NORMAL) {
107 		if (priv->lcd_state <= FB_BLANK_NORMAL) {
108 			/* Do nothing, the LCD is running */
109 		} else if (priv->lcd_state < FB_BLANK_POWERDOWN) {
110 			dev_dbg(&spi->dev, "Resuming LCD\n");
111 
112 			spi_write(spi, (const u8 *)&slpout, sizeof(u16));
113 			msleep(60);
114 			spi_write(spi, (const u8 *)&dison, sizeof(u16));
115 		} else {
116 			/* priv->lcd_state == FB_BLANK_POWERDOWN */
117 			l4f00242t03_lcd_init(spi);
118 			priv->lcd_state = FB_BLANK_VSYNC_SUSPEND;
119 			l4f00242t03_lcd_power_set(priv->ld, power);
120 		}
121 	} else if (power < FB_BLANK_POWERDOWN) {
122 		if (priv->lcd_state <= FB_BLANK_NORMAL) {
123 			/* Send the display in standby */
124 			dev_dbg(&spi->dev, "Standby the LCD\n");
125 
126 			spi_write(spi, (const u8 *)&disoff, sizeof(u16));
127 			msleep(60);
128 			spi_write(spi, (const u8 *)&slpin, sizeof(u16));
129 		} else if (priv->lcd_state < FB_BLANK_POWERDOWN) {
130 			/* Do nothing, the LCD is already in standby */
131 		} else {
132 			/* priv->lcd_state == FB_BLANK_POWERDOWN */
133 			l4f00242t03_lcd_init(spi);
134 			priv->lcd_state = FB_BLANK_UNBLANK;
135 			l4f00242t03_lcd_power_set(ld, power);
136 		}
137 	} else {
138 		/* power == FB_BLANK_POWERDOWN */
139 		if (priv->lcd_state != FB_BLANK_POWERDOWN) {
140 			/* Clear the screen before shutting down */
141 			spi_write(spi, (const u8 *)&disoff, sizeof(u16));
142 			msleep(60);
143 			l4f00242t03_lcd_powerdown(spi);
144 		}
145 	}
146 
147 	priv->lcd_state = power;
148 
149 	return 0;
150 }
151 
152 static struct lcd_ops l4f_ops = {
153 	.set_power	= l4f00242t03_lcd_power_set,
154 	.get_power	= l4f00242t03_lcd_power_get,
155 };
156 
l4f00242t03_probe(struct spi_device * spi)157 static int __devinit l4f00242t03_probe(struct spi_device *spi)
158 {
159 	struct l4f00242t03_priv *priv;
160 	struct l4f00242t03_pdata *pdata = spi->dev.platform_data;
161 	int ret;
162 
163 	if (pdata == NULL) {
164 		dev_err(&spi->dev, "Uninitialized platform data.\n");
165 		return -EINVAL;
166 	}
167 
168 	priv = kzalloc(sizeof(struct l4f00242t03_priv), GFP_KERNEL);
169 
170 	if (priv == NULL) {
171 		dev_err(&spi->dev, "No memory for this device.\n");
172 		return -ENOMEM;
173 	}
174 
175 	dev_set_drvdata(&spi->dev, priv);
176 	spi->bits_per_word = 9;
177 	spi_setup(spi);
178 
179 	priv->spi = spi;
180 
181 	ret = gpio_request(pdata->reset_gpio, "lcd l4f00242t03 reset");
182 	if (ret) {
183 		dev_err(&spi->dev,
184 			"Unable to get the lcd l4f00242t03 reset gpio.\n");
185 		goto err;
186 	}
187 
188 	ret = gpio_direction_output(pdata->reset_gpio, 1);
189 	if (ret)
190 		goto err2;
191 
192 	ret = gpio_request(pdata->data_enable_gpio,
193 				"lcd l4f00242t03 data enable");
194 	if (ret) {
195 		dev_err(&spi->dev,
196 			"Unable to get the lcd l4f00242t03 data en gpio.\n");
197 		goto err2;
198 	}
199 
200 	ret = gpio_direction_output(pdata->data_enable_gpio, 0);
201 	if (ret)
202 		goto err3;
203 
204 	if (pdata->io_supply) {
205 		priv->io_reg = regulator_get(NULL, pdata->io_supply);
206 
207 		if (IS_ERR(priv->io_reg)) {
208 			pr_err("%s: Unable to get the IO regulator\n",
209 								__func__);
210 			goto err3;
211 		}
212 	}
213 
214 	if (pdata->core_supply) {
215 		priv->core_reg = regulator_get(NULL, pdata->core_supply);
216 
217 		if (IS_ERR(priv->core_reg)) {
218 			pr_err("%s: Unable to get the core regulator\n",
219 								__func__);
220 			goto err4;
221 		}
222 	}
223 
224 	priv->ld = lcd_device_register("l4f00242t03",
225 					&spi->dev, priv, &l4f_ops);
226 	if (IS_ERR(priv->ld)) {
227 		ret = PTR_ERR(priv->ld);
228 		goto err5;
229 	}
230 
231 	/* Init the LCD */
232 	l4f00242t03_lcd_init(spi);
233 	priv->lcd_state = FB_BLANK_VSYNC_SUSPEND;
234 	l4f00242t03_lcd_power_set(priv->ld, FB_BLANK_UNBLANK);
235 
236 	dev_info(&spi->dev, "Epson l4f00242t03 lcd probed.\n");
237 
238 	return 0;
239 
240 err5:
241 	if (priv->core_reg)
242 		regulator_put(priv->core_reg);
243 err4:
244 	if (priv->io_reg)
245 		regulator_put(priv->io_reg);
246 err3:
247 	gpio_free(pdata->data_enable_gpio);
248 err2:
249 	gpio_free(pdata->reset_gpio);
250 err:
251 	kfree(priv);
252 
253 	return ret;
254 }
255 
l4f00242t03_remove(struct spi_device * spi)256 static int __devexit l4f00242t03_remove(struct spi_device *spi)
257 {
258 	struct l4f00242t03_priv *priv = dev_get_drvdata(&spi->dev);
259 	struct l4f00242t03_pdata *pdata = priv->spi->dev.platform_data;
260 
261 	l4f00242t03_lcd_power_set(priv->ld, FB_BLANK_POWERDOWN);
262 	lcd_device_unregister(priv->ld);
263 
264 	dev_set_drvdata(&spi->dev, NULL);
265 
266 	gpio_free(pdata->data_enable_gpio);
267 	gpio_free(pdata->reset_gpio);
268 
269 	if (priv->io_reg)
270 		regulator_put(priv->io_reg);
271 	if (priv->core_reg)
272 		regulator_put(priv->core_reg);
273 
274 	kfree(priv);
275 
276 	return 0;
277 }
278 
l4f00242t03_shutdown(struct spi_device * spi)279 static void l4f00242t03_shutdown(struct spi_device *spi)
280 {
281 	struct l4f00242t03_priv *priv = dev_get_drvdata(&spi->dev);
282 
283 	if (priv)
284 		l4f00242t03_lcd_power_set(priv->ld, FB_BLANK_POWERDOWN);
285 
286 }
287 
288 static struct spi_driver l4f00242t03_driver = {
289 	.driver = {
290 		.name	= "l4f00242t03",
291 		.owner	= THIS_MODULE,
292 	},
293 	.probe		= l4f00242t03_probe,
294 	.remove		= __devexit_p(l4f00242t03_remove),
295 	.shutdown	= l4f00242t03_shutdown,
296 };
297 
l4f00242t03_init(void)298 static __init int l4f00242t03_init(void)
299 {
300 	return spi_register_driver(&l4f00242t03_driver);
301 }
302 
l4f00242t03_exit(void)303 static __exit void l4f00242t03_exit(void)
304 {
305 	spi_unregister_driver(&l4f00242t03_driver);
306 }
307 
308 module_init(l4f00242t03_init);
309 module_exit(l4f00242t03_exit);
310 
311 MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>");
312 MODULE_DESCRIPTION("EPSON L4F00242T03 LCD");
313 MODULE_LICENSE("GPL v2");
314