diff options
Diffstat (limited to 'drivers/video/backlight/ili9320.c')
-rw-r--r-- | drivers/video/backlight/ili9320.c | 330 |
1 files changed, 330 insertions, 0 deletions
diff --git a/drivers/video/backlight/ili9320.c b/drivers/video/backlight/ili9320.c new file mode 100644 index 000000000000..ba89b41b639c --- /dev/null +++ b/drivers/video/backlight/ili9320.c | |||
@@ -0,0 +1,330 @@ | |||
1 | /* drivers/video/backlight/ili9320.c | ||
2 | * | ||
3 | * ILI9320 LCD controller driver core. | ||
4 | * | ||
5 | * Copyright 2007 Simtec Electronics | ||
6 | * http://armlinux.simtec.co.uk/ | ||
7 | * Ben Dooks <ben@simtec.co.uk> | ||
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/delay.h> | ||
15 | #include <linux/err.h> | ||
16 | #include <linux/fb.h> | ||
17 | #include <linux/init.h> | ||
18 | #include <linux/lcd.h> | ||
19 | #include <linux/module.h> | ||
20 | |||
21 | #include <linux/spi/spi.h> | ||
22 | |||
23 | #include <video/ili9320.h> | ||
24 | |||
25 | #include "ili9320.h" | ||
26 | |||
27 | |||
28 | static inline int ili9320_write_spi(struct ili9320 *ili, | ||
29 | unsigned int reg, | ||
30 | unsigned int value) | ||
31 | { | ||
32 | struct ili9320_spi *spi = &ili->access.spi; | ||
33 | unsigned char *addr = spi->buffer_addr; | ||
34 | unsigned char *data = spi->buffer_data; | ||
35 | |||
36 | /* spi message consits of: | ||
37 | * first byte: ID and operation | ||
38 | */ | ||
39 | |||
40 | addr[0] = spi->id | ILI9320_SPI_INDEX | ILI9320_SPI_WRITE; | ||
41 | addr[1] = reg >> 8; | ||
42 | addr[2] = reg; | ||
43 | |||
44 | /* second message is the data to transfer */ | ||
45 | |||
46 | data[0] = spi->id | ILI9320_SPI_DATA | ILI9320_SPI_WRITE; | ||
47 | data[1] = value >> 8; | ||
48 | data[2] = value; | ||
49 | |||
50 | return spi_sync(spi->dev, &spi->message); | ||
51 | } | ||
52 | |||
53 | int ili9320_write(struct ili9320 *ili, unsigned int reg, unsigned int value) | ||
54 | { | ||
55 | dev_dbg(ili->dev, "write: reg=%02x, val=%04x\n", reg, value); | ||
56 | return ili->write(ili, reg, value); | ||
57 | } | ||
58 | |||
59 | EXPORT_SYMBOL_GPL(ili9320_write); | ||
60 | |||
61 | int ili9320_write_regs(struct ili9320 *ili, | ||
62 | struct ili9320_reg *values, | ||
63 | int nr_values) | ||
64 | { | ||
65 | int index; | ||
66 | int ret; | ||
67 | |||
68 | for (index = 0; index < nr_values; index++, values++) { | ||
69 | ret = ili9320_write(ili, values->address, values->value); | ||
70 | if (ret != 0) | ||
71 | return ret; | ||
72 | } | ||
73 | |||
74 | return 0; | ||
75 | } | ||
76 | |||
77 | EXPORT_SYMBOL_GPL(ili9320_write_regs); | ||
78 | |||
79 | static void ili9320_reset(struct ili9320 *lcd) | ||
80 | { | ||
81 | struct ili9320_platdata *cfg = lcd->platdata; | ||
82 | |||
83 | cfg->reset(1); | ||
84 | mdelay(50); | ||
85 | |||
86 | cfg->reset(0); | ||
87 | mdelay(50); | ||
88 | |||
89 | cfg->reset(1); | ||
90 | mdelay(100); | ||
91 | } | ||
92 | |||
93 | static inline int ili9320_init_chip(struct ili9320 *lcd) | ||
94 | { | ||
95 | int ret; | ||
96 | |||
97 | ili9320_reset(lcd); | ||
98 | |||
99 | ret = lcd->client->init(lcd, lcd->platdata); | ||
100 | if (ret != 0) { | ||
101 | dev_err(lcd->dev, "failed to initialise display\n"); | ||
102 | return ret; | ||
103 | } | ||
104 | |||
105 | lcd->initialised = 1; | ||
106 | return 0; | ||
107 | } | ||
108 | |||
109 | static inline int ili9320_power_on(struct ili9320 *lcd) | ||
110 | { | ||
111 | if (!lcd->initialised) | ||
112 | ili9320_init_chip(lcd); | ||
113 | |||
114 | lcd->display1 |= (ILI9320_DISPLAY1_D(3) | ILI9320_DISPLAY1_BASEE); | ||
115 | ili9320_write(lcd, ILI9320_DISPLAY1, lcd->display1); | ||
116 | |||
117 | return 0; | ||
118 | } | ||
119 | |||
120 | static inline int ili9320_power_off(struct ili9320 *lcd) | ||
121 | { | ||
122 | lcd->display1 &= ~(ILI9320_DISPLAY1_D(3) | ILI9320_DISPLAY1_BASEE); | ||
123 | ili9320_write(lcd, ILI9320_DISPLAY1, lcd->display1); | ||
124 | |||
125 | return 0; | ||
126 | } | ||
127 | |||
128 | #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL) | ||
129 | |||
130 | static int ili9320_power(struct ili9320 *lcd, int power) | ||
131 | { | ||
132 | int ret = 0; | ||
133 | |||
134 | dev_dbg(lcd->dev, "power %d => %d\n", lcd->power, power); | ||
135 | |||
136 | if (POWER_IS_ON(power) && !POWER_IS_ON(lcd->power)) | ||
137 | ret = ili9320_power_on(lcd); | ||
138 | else if (!POWER_IS_ON(power) && POWER_IS_ON(lcd->power)) | ||
139 | ret = ili9320_power_off(lcd); | ||
140 | |||
141 | if (ret == 0) | ||
142 | lcd->power = power; | ||
143 | else | ||
144 | dev_warn(lcd->dev, "failed to set power mode %d\n", power); | ||
145 | |||
146 | return ret; | ||
147 | } | ||
148 | |||
149 | static inline struct ili9320 *to_our_lcd(struct lcd_device *lcd) | ||
150 | { | ||
151 | return lcd_get_data(lcd); | ||
152 | } | ||
153 | |||
154 | static int ili9320_set_power(struct lcd_device *ld, int power) | ||
155 | { | ||
156 | struct ili9320 *lcd = to_our_lcd(ld); | ||
157 | |||
158 | return ili9320_power(lcd, power); | ||
159 | } | ||
160 | |||
161 | static int ili9320_get_power(struct lcd_device *ld) | ||
162 | { | ||
163 | struct ili9320 *lcd = to_our_lcd(ld); | ||
164 | |||
165 | return lcd->power; | ||
166 | } | ||
167 | |||
168 | static struct lcd_ops ili9320_ops = { | ||
169 | .get_power = ili9320_get_power, | ||
170 | .set_power = ili9320_set_power, | ||
171 | }; | ||
172 | |||
173 | static void __devinit ili9320_setup_spi(struct ili9320 *ili, | ||
174 | struct spi_device *dev) | ||
175 | { | ||
176 | struct ili9320_spi *spi = &ili->access.spi; | ||
177 | |||
178 | ili->write = ili9320_write_spi; | ||
179 | spi->dev = dev; | ||
180 | |||
181 | /* fill the two messages we are going to use to send the data | ||
182 | * with, the first the address followed by the data. The datasheet | ||
183 | * says they should be done as two distinct cycles of the SPI CS line. | ||
184 | */ | ||
185 | |||
186 | spi->xfer[0].tx_buf = spi->buffer_addr; | ||
187 | spi->xfer[1].tx_buf = spi->buffer_data; | ||
188 | spi->xfer[0].len = 3; | ||
189 | spi->xfer[1].len = 3; | ||
190 | spi->xfer[0].bits_per_word = 8; | ||
191 | spi->xfer[1].bits_per_word = 8; | ||
192 | spi->xfer[0].cs_change = 1; | ||
193 | |||
194 | spi_message_init(&spi->message); | ||
195 | spi_message_add_tail(&spi->xfer[0], &spi->message); | ||
196 | spi_message_add_tail(&spi->xfer[1], &spi->message); | ||
197 | } | ||
198 | |||
199 | int __devinit ili9320_probe_spi(struct spi_device *spi, | ||
200 | struct ili9320_client *client) | ||
201 | { | ||
202 | struct ili9320_platdata *cfg = spi->dev.platform_data; | ||
203 | struct device *dev = &spi->dev; | ||
204 | struct ili9320 *ili; | ||
205 | struct lcd_device *lcd; | ||
206 | int ret = 0; | ||
207 | |||
208 | /* verify we where given some information */ | ||
209 | |||
210 | if (cfg == NULL) { | ||
211 | dev_err(dev, "no platform data supplied\n"); | ||
212 | return -EINVAL; | ||
213 | } | ||
214 | |||
215 | if (cfg->hsize <= 0 || cfg->vsize <= 0 || cfg->reset == NULL) { | ||
216 | dev_err(dev, "invalid platform data supplied\n"); | ||
217 | return -EINVAL; | ||
218 | } | ||
219 | |||
220 | /* allocate and initialse our state */ | ||
221 | |||
222 | ili = kzalloc(sizeof(struct ili9320), GFP_KERNEL); | ||
223 | if (ili == NULL) { | ||
224 | dev_err(dev, "no memory for device\n"); | ||
225 | return -ENOMEM; | ||
226 | } | ||
227 | |||
228 | ili->access.spi.id = ILI9320_SPI_IDCODE | ILI9320_SPI_ID(1); | ||
229 | |||
230 | ili->dev = dev; | ||
231 | ili->client = client; | ||
232 | ili->power = FB_BLANK_POWERDOWN; | ||
233 | ili->platdata = cfg; | ||
234 | |||
235 | dev_set_drvdata(&spi->dev, ili); | ||
236 | |||
237 | ili9320_setup_spi(ili, spi); | ||
238 | |||
239 | lcd = lcd_device_register("ili9320", dev, ili, &ili9320_ops); | ||
240 | if (IS_ERR(lcd)) { | ||
241 | dev_err(dev, "failed to register lcd device\n"); | ||
242 | ret = PTR_ERR(lcd); | ||
243 | goto err_free; | ||
244 | } | ||
245 | |||
246 | ili->lcd = lcd; | ||
247 | |||
248 | dev_info(dev, "initialising %s\n", client->name); | ||
249 | |||
250 | ret = ili9320_power(ili, FB_BLANK_UNBLANK); | ||
251 | if (ret != 0) { | ||
252 | dev_err(dev, "failed to set lcd power state\n"); | ||
253 | goto err_unregister; | ||
254 | } | ||
255 | |||
256 | return 0; | ||
257 | |||
258 | err_unregister: | ||
259 | lcd_device_unregister(lcd); | ||
260 | |||
261 | err_free: | ||
262 | kfree(ili); | ||
263 | |||
264 | return ret; | ||
265 | } | ||
266 | |||
267 | EXPORT_SYMBOL_GPL(ili9320_probe_spi); | ||
268 | |||
269 | int __devexit ili9320_remove(struct ili9320 *ili) | ||
270 | { | ||
271 | ili9320_power(ili, FB_BLANK_POWERDOWN); | ||
272 | |||
273 | lcd_device_unregister(ili->lcd); | ||
274 | kfree(ili); | ||
275 | |||
276 | return 0; | ||
277 | } | ||
278 | |||
279 | EXPORT_SYMBOL_GPL(ili9320_remove); | ||
280 | |||
281 | #ifdef CONFIG_PM | ||
282 | int ili9320_suspend(struct ili9320 *lcd, pm_message_t state) | ||
283 | { | ||
284 | int ret; | ||
285 | |||
286 | dev_dbg(lcd->dev, "%s: event %d\n", __func__, state.event); | ||
287 | |||
288 | if (state.event == PM_EVENT_SUSPEND) { | ||
289 | ret = ili9320_power(lcd, FB_BLANK_POWERDOWN); | ||
290 | |||
291 | if (lcd->platdata->suspend == ILI9320_SUSPEND_DEEP) { | ||
292 | ili9320_write(lcd, ILI9320_POWER1, lcd->power1 | | ||
293 | ILI9320_POWER1_SLP | | ||
294 | ILI9320_POWER1_DSTB); | ||
295 | lcd->initialised = 0; | ||
296 | } | ||
297 | |||
298 | return ret; | ||
299 | } | ||
300 | |||
301 | return 0; | ||
302 | } | ||
303 | |||
304 | EXPORT_SYMBOL_GPL(ili9320_suspend); | ||
305 | |||
306 | int ili9320_resume(struct ili9320 *lcd) | ||
307 | { | ||
308 | dev_info(lcd->dev, "resuming from power state %d\n", lcd->power); | ||
309 | |||
310 | if (lcd->platdata->suspend == ILI9320_SUSPEND_DEEP) { | ||
311 | ili9320_write(lcd, ILI9320_POWER1, 0x00); | ||
312 | } | ||
313 | |||
314 | return ili9320_power(lcd, FB_BLANK_UNBLANK); | ||
315 | } | ||
316 | |||
317 | EXPORT_SYMBOL_GPL(ili9320_resume); | ||
318 | #endif | ||
319 | |||
320 | /* Power down all displays on reboot, poweroff or halt */ | ||
321 | void ili9320_shutdown(struct ili9320 *lcd) | ||
322 | { | ||
323 | ili9320_power(lcd, FB_BLANK_POWERDOWN); | ||
324 | } | ||
325 | |||
326 | EXPORT_SYMBOL_GPL(ili9320_shutdown); | ||
327 | |||
328 | MODULE_AUTHOR("Ben Dooks <ben-linux@fluff.org>"); | ||
329 | MODULE_DESCRIPTION("ILI9320 LCD Driver"); | ||
330 | MODULE_LICENSE("GPL v2"); | ||