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