1 /* drivers/video/backlight/ili9320.c
3 * ILI9320 LCD controller driver core.
5 * Copyright 2007 Simtec Electronics
6 * http://armlinux.simtec.co.uk/
7 * Ben Dooks <ben@simtec.co.uk>
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.
14 #include <linux/delay.h>
15 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/lcd.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
22 #include <linux/spi/spi.h>
24 #include <video/ili9320.h>
29 static inline int ili9320_write_spi(struct ili9320
*ili
,
33 struct ili9320_spi
*spi
= &ili
->access
.spi
;
34 unsigned char *addr
= spi
->buffer_addr
;
35 unsigned char *data
= spi
->buffer_data
;
37 /* spi message consits of:
38 * first byte: ID and operation
41 addr
[0] = spi
->id
| ILI9320_SPI_INDEX
| ILI9320_SPI_WRITE
;
45 /* second message is the data to transfer */
47 data
[0] = spi
->id
| ILI9320_SPI_DATA
| ILI9320_SPI_WRITE
;
51 return spi_sync(spi
->dev
, &spi
->message
);
54 int ili9320_write(struct ili9320
*ili
, unsigned int reg
, unsigned int value
)
56 dev_dbg(ili
->dev
, "write: reg=%02x, val=%04x\n", reg
, value
);
57 return ili
->write(ili
, reg
, value
);
60 EXPORT_SYMBOL_GPL(ili9320_write
);
62 int ili9320_write_regs(struct ili9320
*ili
,
63 struct ili9320_reg
*values
,
69 for (index
= 0; index
< nr_values
; index
++, values
++) {
70 ret
= ili9320_write(ili
, values
->address
, values
->value
);
78 EXPORT_SYMBOL_GPL(ili9320_write_regs
);
80 static void ili9320_reset(struct ili9320
*lcd
)
82 struct ili9320_platdata
*cfg
= lcd
->platdata
;
94 static inline int ili9320_init_chip(struct ili9320
*lcd
)
100 ret
= lcd
->client
->init(lcd
, lcd
->platdata
);
102 dev_err(lcd
->dev
, "failed to initialise display\n");
106 lcd
->initialised
= 1;
110 static inline int ili9320_power_on(struct ili9320
*lcd
)
112 if (!lcd
->initialised
)
113 ili9320_init_chip(lcd
);
115 lcd
->display1
|= (ILI9320_DISPLAY1_D(3) | ILI9320_DISPLAY1_BASEE
);
116 ili9320_write(lcd
, ILI9320_DISPLAY1
, lcd
->display1
);
121 static inline int ili9320_power_off(struct ili9320
*lcd
)
123 lcd
->display1
&= ~(ILI9320_DISPLAY1_D(3) | ILI9320_DISPLAY1_BASEE
);
124 ili9320_write(lcd
, ILI9320_DISPLAY1
, lcd
->display1
);
129 #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL)
131 static int ili9320_power(struct ili9320
*lcd
, int power
)
135 dev_dbg(lcd
->dev
, "power %d => %d\n", lcd
->power
, power
);
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
);
145 dev_warn(lcd
->dev
, "failed to set power mode %d\n", power
);
150 static inline struct ili9320
*to_our_lcd(struct lcd_device
*lcd
)
152 return lcd_get_data(lcd
);
155 static int ili9320_set_power(struct lcd_device
*ld
, int power
)
157 struct ili9320
*lcd
= to_our_lcd(ld
);
159 return ili9320_power(lcd
, power
);
162 static int ili9320_get_power(struct lcd_device
*ld
)
164 struct ili9320
*lcd
= to_our_lcd(ld
);
169 static struct lcd_ops ili9320_ops
= {
170 .get_power
= ili9320_get_power
,
171 .set_power
= ili9320_set_power
,
174 static void __devinit
ili9320_setup_spi(struct ili9320
*ili
,
175 struct spi_device
*dev
)
177 struct ili9320_spi
*spi
= &ili
->access
.spi
;
179 ili
->write
= ili9320_write_spi
;
182 /* fill the two messages we are going to use to send the data
183 * with, the first the address followed by the data. The datasheet
184 * says they should be done as two distinct cycles of the SPI CS line.
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;
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
);
200 int __devinit
ili9320_probe_spi(struct spi_device
*spi
,
201 struct ili9320_client
*client
)
203 struct ili9320_platdata
*cfg
= spi
->dev
.platform_data
;
204 struct device
*dev
= &spi
->dev
;
206 struct lcd_device
*lcd
;
209 /* verify we where given some information */
212 dev_err(dev
, "no platform data supplied\n");
216 if (cfg
->hsize
<= 0 || cfg
->vsize
<= 0 || cfg
->reset
== NULL
) {
217 dev_err(dev
, "invalid platform data supplied\n");
221 /* allocate and initialse our state */
223 ili
= kzalloc(sizeof(struct ili9320
), GFP_KERNEL
);
225 dev_err(dev
, "no memory for device\n");
229 ili
->access
.spi
.id
= ILI9320_SPI_IDCODE
| ILI9320_SPI_ID(1);
232 ili
->client
= client
;
233 ili
->power
= FB_BLANK_POWERDOWN
;
236 dev_set_drvdata(&spi
->dev
, ili
);
238 ili9320_setup_spi(ili
, spi
);
240 lcd
= lcd_device_register("ili9320", dev
, ili
, &ili9320_ops
);
242 dev_err(dev
, "failed to register lcd device\n");
249 dev_info(dev
, "initialising %s\n", client
->name
);
251 ret
= ili9320_power(ili
, FB_BLANK_UNBLANK
);
253 dev_err(dev
, "failed to set lcd power state\n");
260 lcd_device_unregister(lcd
);
268 EXPORT_SYMBOL_GPL(ili9320_probe_spi
);
270 int __devexit
ili9320_remove(struct ili9320
*ili
)
272 ili9320_power(ili
, FB_BLANK_POWERDOWN
);
274 lcd_device_unregister(ili
->lcd
);
280 EXPORT_SYMBOL_GPL(ili9320_remove
);
283 int ili9320_suspend(struct ili9320
*lcd
, pm_message_t state
)
287 dev_dbg(lcd
->dev
, "%s: event %d\n", __func__
, state
.event
);
289 if (state
.event
== PM_EVENT_SUSPEND
) {
290 ret
= ili9320_power(lcd
, FB_BLANK_POWERDOWN
);
292 if (lcd
->platdata
->suspend
== ILI9320_SUSPEND_DEEP
) {
293 ili9320_write(lcd
, ILI9320_POWER1
, lcd
->power1
|
295 ILI9320_POWER1_DSTB
);
296 lcd
->initialised
= 0;
305 EXPORT_SYMBOL_GPL(ili9320_suspend
);
307 int ili9320_resume(struct ili9320
*lcd
)
309 dev_info(lcd
->dev
, "resuming from power state %d\n", lcd
->power
);
311 if (lcd
->platdata
->suspend
== ILI9320_SUSPEND_DEEP
) {
312 ili9320_write(lcd
, ILI9320_POWER1
, 0x00);
315 return ili9320_power(lcd
, FB_BLANK_UNBLANK
);
318 EXPORT_SYMBOL_GPL(ili9320_resume
);
321 /* Power down all displays on reboot, poweroff or halt */
322 void ili9320_shutdown(struct ili9320
*lcd
)
324 ili9320_power(lcd
, FB_BLANK_POWERDOWN
);
327 EXPORT_SYMBOL_GPL(ili9320_shutdown
);
329 MODULE_AUTHOR("Ben Dooks <ben-linux@fluff.org>");
330 MODULE_DESCRIPTION("ILI9320 LCD Driver");
331 MODULE_LICENSE("GPL v2");