kernel: update linux 3.8 to 3.8.8
[openwrt.git] / target / linux / xburst / patches-3.8 / 0007-Add-ili8960-lcd-driver.patch
1 From 2248fb9c47fe7e72c735d6c16aba27bf92e1673a Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sun, 1 Aug 2010 21:19:40 +0200
4 Subject: [PATCH 07/21] Add ili8960 lcd driver
5
6 ---
7  drivers/video/backlight/Kconfig   |    7 +
8  drivers/video/backlight/Makefile  |    1 +
9  drivers/video/backlight/ili8960.c |  263 +++++++++++++++++++++++++++++++++++++
10  3 files changed, 271 insertions(+), 0 deletions(-)
11  create mode 100644 drivers/video/backlight/ili8960.c
12
13 --- a/drivers/video/backlight/Kconfig
14 +++ b/drivers/video/backlight/Kconfig
15 @@ -59,6 +59,13 @@ config LCD_LTV350QV
16  
17           The LTV350QV panel is present on all ATSTK1000 boards.
18  
19 +config LCD_ILI8960
20 +       tristate "Ilitek ili8960 LCD driver"
21 +       depends on LCD_CLASS_DEVICE && SPI
22 +       default n
23 +       help
24 +         Driver for the Ilitek ili8960 LCD controller chip.
25 +
26  config LCD_ILI9320
27         tristate "ILI Technology ILI9320 controller support"
28         depends on SPI
29 --- a/drivers/video/backlight/Makefile
30 +++ b/drivers/video/backlight/Makefile
31 @@ -6,6 +6,7 @@ obj-$(CONFIG_LCD_HP700)            += jornada72
32  obj-$(CONFIG_LCD_L4F00242T03)     += l4f00242t03.o
33  obj-$(CONFIG_LCD_LMS283GF05)      += lms283gf05.o
34  obj-$(CONFIG_LCD_LTV350QV)        += ltv350qv.o
35 +obj-$(CONFIG_LCD_ILI8960)         += ili8960.o
36  obj-$(CONFIG_LCD_ILI9320)         += ili9320.o
37  obj-$(CONFIG_LCD_PLATFORM)        += platform_lcd.o
38  obj-$(CONFIG_LCD_VGG2432A4)       += vgg2432a4.o
39 --- /dev/null
40 +++ b/drivers/video/backlight/ili8960.c
41 @@ -0,0 +1,263 @@
42 +/*
43 + *  Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
44 + *  Driver for Ilitek ili8960 LCD
45 + *
46 + *  This program is free software; you can redistribute         it and/or modify it
47 + *  under  the terms of         the GNU General  Public License as published by the
48 + *  Free Software Foundation;  either version 2 of the License, or (at your
49 + *  option) any later version.
50 + *
51 + *  You should have received a copy of the  GNU General Public License along
52 + *  with this program; if not, write  to the Free Software Foundation, Inc.,
53 + *  675 Mass Ave, Cambridge, MA 02139, USA.
54 + *
55 + */
56 +
57 +#include <linux/module.h>
58 +#include <linux/spi/spi.h>
59 +#include <linux/lcd.h>
60 +#include <linux/backlight.h>
61 +#include <linux/delay.h>
62 +
63 +struct ili8960 {
64 +       struct spi_device *spi;
65 +       struct lcd_device *lcd;
66 +       struct backlight_device *bl;
67 +       bool enabled;
68 +       unsigned int brightness;
69 +};
70 +
71 +#define ILI8960_REG_BRIGHTNESS 0x03
72 +#define ILI8960_REG_POWER      0x05
73 +#define ILI8960_REG_CONTRAST   0x0d
74 +
75 +static int ili8960_write_reg(struct spi_device *spi, uint8_t reg,
76 +                               uint8_t data)
77 +{
78 +       uint8_t buf[2];
79 +       buf[0] = ((reg & 0x40) << 1) | (reg & 0x3f);
80 +       buf[1] = data;
81 +
82 +       return spi_write(spi, buf, sizeof(buf));
83 +}
84 +
85 +static int ili8960_programm_power(struct spi_device *spi, bool enabled)
86 +{
87 +       int ret;
88 +
89 +       if (enabled)
90 +               mdelay(20);
91 +
92 +       ret = ili8960_write_reg(spi, ILI8960_REG_POWER, enabled ? 0xc7 : 0xc6);
93 +
94 +       if (!enabled)
95 +               mdelay(20);
96 +
97 +       return ret;
98 +}
99 +
100 +static int ili8960_set_power(struct lcd_device *lcd, int power)
101 +{
102 +       struct ili8960 *ili8960 = lcd_get_data(lcd);
103 +
104 +       switch (power) {
105 +       case FB_BLANK_UNBLANK:
106 +               ili8960->enabled = true;
107 +               break;
108 +       default:
109 +               ili8960->enabled = false;
110 +               break;
111 +       }
112 +
113 +       return ili8960_programm_power(ili8960->spi, ili8960->enabled);
114 +}
115 +
116 +static int ili8960_get_power(struct lcd_device *lcd)
117 +{
118 +       struct ili8960 *ili8960 = lcd_get_data(lcd);
119 +       return ili8960->enabled ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
120 +}
121 +
122 +static int ili8960_set_contrast(struct lcd_device *lcd, int contrast)
123 +{
124 +       struct ili8960 *ili8960 = lcd_get_data(lcd);
125 +
126 +       return ili8960_write_reg(ili8960->spi, ILI8960_REG_CONTRAST, contrast);
127 +}
128 +
129 +static int ili8960_set_mode(struct lcd_device *lcd, struct fb_videomode *mode)
130 +{
131 +       if (mode->xres != 320 && mode->yres != 240)
132 +               return -EINVAL;
133 +
134 +       return 0;
135 +}
136 +
137 +static int ili8960_set_brightness(struct ili8960 *ili8960, int brightness)
138 +{
139 +       int ret;
140 +
141 +       ret = ili8960_write_reg(ili8960->spi, ILI8960_REG_BRIGHTNESS, brightness);
142 +
143 +       if (ret == 0)
144 +               ili8960->brightness = brightness;
145 +
146 +       return ret;
147 +}
148 +
149 +static ssize_t ili8960_show_brightness(struct device *dev,
150 +               struct device_attribute *attr, char *buf)
151 +{
152 +       struct lcd_device *ld = to_lcd_device(dev);
153 +       struct ili8960 *ili8960 = lcd_get_data(ld);
154 +
155 +       return sprintf(buf, "%u\n", ili8960->brightness);
156 +}
157 +
158 +static ssize_t ili8960_store_brightness(struct device *dev,
159 +               struct device_attribute *attr, const char *buf, size_t count)
160 +{
161 +       struct lcd_device *ld = to_lcd_device(dev);
162 +       struct ili8960 *ili8960 = lcd_get_data(ld);
163 +       unsigned long brightness;
164 +       int ret;
165 +
166 +       ret = strict_strtoul(buf, 0, &brightness);
167 +       if (ret)
168 +               return ret;
169 +
170 +       if (brightness > 255)
171 +               return -EINVAL;
172 +
173 +       ili8960_set_brightness(ili8960, brightness);
174 +
175 +       return count;
176 +}
177 +
178 +
179 +static DEVICE_ATTR(brightness, 0644, ili8960_show_brightness,
180 +       ili8960_store_brightness);
181 +
182 +static struct lcd_ops ili8960_lcd_ops = {
183 +       .set_power = ili8960_set_power,
184 +       .get_power = ili8960_get_power,
185 +       .set_contrast = ili8960_set_contrast,
186 +       .set_mode = ili8960_set_mode,
187 +};
188 +
189 +static int ili8960_probe(struct spi_device *spi)
190 +{
191 +       int ret;
192 +       struct ili8960 *ili8960;
193 +
194 +       ili8960 = kmalloc(sizeof(*ili8960), GFP_KERNEL);
195 +       if (!ili8960)
196 +               return -ENOMEM;
197 +
198 +       spi->bits_per_word = 8;
199 +       spi->mode = SPI_MODE_3;
200 +
201 +       ret = spi_setup(spi);
202 +       if (ret) {
203 +               dev_err(&spi->dev, "Failed to setup spi\n");
204 +               goto err_free_ili8960;
205 +       }
206 +
207 +       ili8960->spi = spi;
208 +
209 +       ili8960->lcd = lcd_device_register("ili8960-lcd", &spi->dev, ili8960,
210 +                                               &ili8960_lcd_ops);
211 +
212 +       if (IS_ERR(ili8960->lcd)) {
213 +               ret = PTR_ERR(ili8960->lcd);
214 +               dev_err(&spi->dev, "Failed to register lcd device: %d\n", ret);
215 +               goto err_free_ili8960;
216 +       }
217 +
218 +       ili8960->lcd->props.max_contrast = 255;
219 +
220 +       ret = device_create_file(&ili8960->lcd->dev, &dev_attr_brightness);
221 +       if (ret)
222 +               goto err_unregister_lcd;
223 +
224 +       ili8960_programm_power(ili8960->spi, true);
225 +       ili8960->enabled = true;
226 +
227 +       spi_set_drvdata(spi, ili8960);
228 +
229 +       ili8960_write_reg(spi, 0x13, 0x01);
230 +
231 +       return 0;
232 +err_unregister_lcd:
233 +       lcd_device_unregister(ili8960->lcd);
234 +err_free_ili8960:
235 +       kfree(ili8960);
236 +       return ret;
237 +}
238 +
239 +static int ili8960_remove(struct spi_device *spi)
240 +{
241 +       struct ili8960 *ili8960 = spi_get_drvdata(spi);
242 +
243 +       device_remove_file(&ili8960->lcd->dev, &dev_attr_brightness);
244 +       lcd_device_unregister(ili8960->lcd);
245 +
246 +       spi_set_drvdata(spi, NULL);
247 +       kfree(ili8960);
248 +       return 0;
249 +}
250 +
251 +#ifdef CONFIG_PM
252 +
253 +static int ili8960_suspend(struct spi_device *spi, pm_message_t state)
254 +{
255 +       struct ili8960 *ili8960 = spi_get_drvdata(spi);
256 +
257 +       if (ili8960->enabled)
258 +               ili8960_programm_power(ili8960->spi, false);
259 +
260 +       return 0;
261 +}
262 +
263 +static int ili8960_resume(struct spi_device *spi)
264 +{
265 +       struct ili8960 *ili8960 = spi_get_drvdata(spi);
266 +
267 +       if (ili8960->enabled)
268 +               ili8960_programm_power(ili8960->spi, true);
269 +
270 +       return 0;
271 +}
272 +
273 +#else
274 +#define ili8960_suspend NULL
275 +#define ili8960_resume NULL
276 +#endif
277 +
278 +static struct spi_driver ili8960_driver = {
279 +       .driver = {
280 +               .name = "ili8960",
281 +               .owner = THIS_MODULE,
282 +       },
283 +       .probe = ili8960_probe,
284 +       .remove = ili8960_remove,
285 +       .suspend = ili8960_suspend,
286 +       .resume = ili8960_resume,
287 +};
288 +
289 +static int __init ili8960_init(void)
290 +{
291 +       return spi_register_driver(&ili8960_driver);
292 +}
293 +module_init(ili8960_init);
294 +
295 +static void __exit ili8960_exit(void)
296 +{
297 +       spi_unregister_driver(&ili8960_driver);
298 +}
299 +module_exit(ili8960_exit)
300 +
301 +MODULE_AUTHOR("Lars-Peter Clausen");
302 +MODULE_LICENSE("GPL");
303 +MODULE_DESCRIPTION("LCD driver for Ilitek ili8960");
304 +MODULE_ALIAS("spi:ili8960");