Merge branch 'for-linus' of git://git.o-hand.com/linux-rpurdie-leds
[pandora-kernel.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/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_on:1;
29         struct regulator *io_reg;
30         struct regulator *core_reg;
31 };
32
33
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
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         gpio_set_value(pdata->data_enable_gpio, 1);
66         msleep(60);
67         spi_write(spi, (const u8 *)cmd, ARRAY_SIZE(cmd) * sizeof(u16));
68 }
69
70 static int l4f00242t03_lcd_power_set(struct lcd_device *ld, int power)
71 {
72         struct l4f00242t03_priv *priv = lcd_get_data(ld);
73         struct spi_device *spi = priv->spi;
74
75         const u16 slpout = 0x11;
76         const u16 dison = 0x29;
77
78         const u16 slpin = 0x10;
79         const u16 disoff = 0x28;
80
81         if (power) {
82                 if (priv->lcd_on)
83                         return 0;
84
85                 dev_dbg(&spi->dev, "turning on LCD\n");
86
87                 spi_write(spi, (const u8 *)&slpout, sizeof(u16));
88                 msleep(60);
89                 spi_write(spi, (const u8 *)&dison, sizeof(u16));
90
91                 priv->lcd_on = 1;
92         } else {
93                 if (!priv->lcd_on)
94                         return 0;
95
96                 dev_dbg(&spi->dev, "turning off LCD\n");
97
98                 spi_write(spi, (const u8 *)&disoff, sizeof(u16));
99                 msleep(60);
100                 spi_write(spi, (const u8 *)&slpin, sizeof(u16));
101
102                 priv->lcd_on = 0;
103         }
104
105         return 0;
106 }
107
108 static struct lcd_ops l4f_ops = {
109         .set_power      = l4f00242t03_lcd_power_set,
110         .get_power      = NULL,
111 };
112
113 static int __devinit l4f00242t03_probe(struct spi_device *spi)
114 {
115         struct l4f00242t03_priv *priv;
116         struct l4f00242t03_pdata *pdata = spi->dev.platform_data;
117         int ret;
118
119         if (pdata == NULL) {
120                 dev_err(&spi->dev, "Uninitialized platform data.\n");
121                 return -EINVAL;
122         }
123
124         priv = kzalloc(sizeof(struct l4f00242t03_priv), GFP_KERNEL);
125
126         if (priv == NULL) {
127                 dev_err(&spi->dev, "No memory for this device.\n");
128                 ret = -ENOMEM;
129                 goto err;
130         }
131
132         dev_set_drvdata(&spi->dev, priv);
133         spi->bits_per_word = 9;
134         spi_setup(spi);
135
136         priv->spi = spi;
137
138         ret = gpio_request(pdata->reset_gpio, "lcd l4f00242t03 reset");
139         if (ret) {
140                 dev_err(&spi->dev,
141                         "Unable to get the lcd l4f00242t03 reset gpio.\n");
142                 return ret;
143         }
144
145         ret = gpio_direction_output(pdata->reset_gpio, 1);
146         if (ret)
147                 goto err2;
148
149         ret = gpio_request(pdata->data_enable_gpio,
150                                 "lcd l4f00242t03 data enable");
151         if (ret) {
152                 dev_err(&spi->dev,
153                         "Unable to get the lcd l4f00242t03 data en gpio.\n");
154                 return ret;
155         }
156
157         ret = gpio_direction_output(pdata->data_enable_gpio, 0);
158         if (ret)
159                 goto err3;
160
161         if (pdata->io_supply) {
162                 priv->io_reg = regulator_get(NULL, pdata->io_supply);
163
164                 if (IS_ERR(priv->io_reg)) {
165                         pr_err("%s: Unable to get the IO regulator\n",
166                                                                 __func__);
167                         goto err3;
168                 }
169         }
170
171         if (pdata->core_supply) {
172                 priv->core_reg = regulator_get(NULL, pdata->core_supply);
173
174                 if (IS_ERR(priv->core_reg)) {
175                         pr_err("%s: Unable to get the core regulator\n",
176                                                                 __func__);
177                         goto err4;
178                 }
179         }
180
181         priv->ld = lcd_device_register("l4f00242t03",
182                                         &spi->dev, priv, &l4f_ops);
183         if (IS_ERR(priv->ld)) {
184                 ret = PTR_ERR(priv->ld);
185                 goto err5;
186         }
187
188         /* Init the LCD */
189         l4f00242t03_reset(pdata->reset_gpio);
190         l4f00242t03_lcd_init(spi);
191         l4f00242t03_lcd_power_set(priv->ld, 1);
192
193         dev_info(&spi->dev, "Epson l4f00242t03 lcd probed.\n");
194
195         return 0;
196
197 err5:
198         if (priv->core_reg)
199                 regulator_put(priv->core_reg);
200 err4:
201         if (priv->io_reg)
202                 regulator_put(priv->io_reg);
203 err3:
204         gpio_free(pdata->data_enable_gpio);
205 err2:
206         gpio_free(pdata->reset_gpio);
207 err:
208         kfree(priv);
209
210         return ret;
211 }
212
213 static int __devexit l4f00242t03_remove(struct spi_device *spi)
214 {
215         struct l4f00242t03_priv *priv = dev_get_drvdata(&spi->dev);
216         struct l4f00242t03_pdata *pdata = priv->spi->dev.platform_data;
217
218         l4f00242t03_lcd_power_set(priv->ld, 0);
219         lcd_device_unregister(priv->ld);
220
221         gpio_free(pdata->data_enable_gpio);
222         gpio_free(pdata->reset_gpio);
223
224         if (priv->io_reg)
225                 regulator_put(priv->core_reg);
226         if (priv->core_reg)
227                 regulator_put(priv->io_reg);
228
229         kfree(priv);
230
231         return 0;
232 }
233
234 static struct spi_driver l4f00242t03_driver = {
235         .driver = {
236                 .name   = "l4f00242t03",
237                 .owner  = THIS_MODULE,
238         },
239         .probe          = l4f00242t03_probe,
240         .remove         = __devexit_p(l4f00242t03_remove),
241 };
242
243 static __init int l4f00242t03_init(void)
244 {
245         return spi_register_driver(&l4f00242t03_driver);
246 }
247
248 static __exit void l4f00242t03_exit(void)
249 {
250         spi_unregister_driver(&l4f00242t03_driver);
251 }
252
253 module_init(l4f00242t03_init);
254 module_exit(l4f00242t03_exit);
255
256 MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>");
257 MODULE_DESCRIPTION("EPSON L4F00242T03 LCD");
258 MODULE_LICENSE("GPL v2");