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