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