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
);
59 EXPORT_SYMBOL_GPL(ili9320_write
);
61 int ili9320_write_regs(struct ili9320
*ili
,
62 const struct ili9320_reg
*values
,
68 for (index
= 0; index
< nr_values
; index
++, values
++) {
69 ret
= ili9320_write(ili
, values
->address
, values
->value
);
76 EXPORT_SYMBOL_GPL(ili9320_write_regs
);
78 static void ili9320_reset(struct ili9320
*lcd
)
80 struct ili9320_platdata
*cfg
= lcd
->platdata
;
92 static inline int ili9320_init_chip(struct ili9320
*lcd
)
98 ret
= lcd
->client
->init(lcd
, lcd
->platdata
);
100 dev_err(lcd
->dev
, "failed to initialise display\n");
104 lcd
->initialised
= 1;
108 static inline int ili9320_power_on(struct ili9320
*lcd
)
110 if (!lcd
->initialised
)
111 ili9320_init_chip(lcd
);
113 lcd
->display1
|= (ILI9320_DISPLAY1_D(3) | ILI9320_DISPLAY1_BASEE
);
114 ili9320_write(lcd
, ILI9320_DISPLAY1
, lcd
->display1
);
119 static inline int ili9320_power_off(struct ili9320
*lcd
)
121 lcd
->display1
&= ~(ILI9320_DISPLAY1_D(3) | ILI9320_DISPLAY1_BASEE
);
122 ili9320_write(lcd
, ILI9320_DISPLAY1
, lcd
->display1
);
127 #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL)
129 static int ili9320_power(struct ili9320
*lcd
, int power
)
133 dev_dbg(lcd
->dev
, "power %d => %d\n", lcd
->power
, power
);
135 if (POWER_IS_ON(power
) && !POWER_IS_ON(lcd
->power
))
136 ret
= ili9320_power_on(lcd
);
137 else if (!POWER_IS_ON(power
) && POWER_IS_ON(lcd
->power
))
138 ret
= ili9320_power_off(lcd
);
143 dev_warn(lcd
->dev
, "failed to set power mode %d\n", power
);
148 static inline struct ili9320
*to_our_lcd(struct lcd_device
*lcd
)
150 return lcd_get_data(lcd
);
153 static int ili9320_set_power(struct lcd_device
*ld
, int power
)
155 struct ili9320
*lcd
= to_our_lcd(ld
);
157 return ili9320_power(lcd
, power
);
160 static int ili9320_get_power(struct lcd_device
*ld
)
162 struct ili9320
*lcd
= to_our_lcd(ld
);
167 static struct lcd_ops ili9320_ops
= {
168 .get_power
= ili9320_get_power
,
169 .set_power
= ili9320_set_power
,
172 static void ili9320_setup_spi(struct ili9320
*ili
,
173 struct spi_device
*dev
)
175 struct ili9320_spi
*spi
= &ili
->access
.spi
;
177 ili
->write
= ili9320_write_spi
;
180 /* fill the two messages we are going to use to send the data
181 * with, the first the address followed by the data. The datasheet
182 * says they should be done as two distinct cycles of the SPI CS line.
185 spi
->xfer
[0].tx_buf
= spi
->buffer_addr
;
186 spi
->xfer
[1].tx_buf
= spi
->buffer_data
;
187 spi
->xfer
[0].len
= 3;
188 spi
->xfer
[1].len
= 3;
189 spi
->xfer
[0].bits_per_word
= 8;
190 spi
->xfer
[1].bits_per_word
= 8;
191 spi
->xfer
[0].cs_change
= 1;
193 spi_message_init(&spi
->message
);
194 spi_message_add_tail(&spi
->xfer
[0], &spi
->message
);
195 spi_message_add_tail(&spi
->xfer
[1], &spi
->message
);
198 int ili9320_probe_spi(struct spi_device
*spi
,
199 struct ili9320_client
*client
)
201 struct ili9320_platdata
*cfg
= dev_get_platdata(&spi
->dev
);
202 struct device
*dev
= &spi
->dev
;
204 struct lcd_device
*lcd
;
207 /* verify we where given some information */
210 dev_err(dev
, "no platform data supplied\n");
214 if (cfg
->hsize
<= 0 || cfg
->vsize
<= 0 || cfg
->reset
== NULL
) {
215 dev_err(dev
, "invalid platform data supplied\n");
219 /* allocate and initialse our state */
221 ili
= devm_kzalloc(&spi
->dev
, sizeof(struct ili9320
), GFP_KERNEL
);
225 ili
->access
.spi
.id
= ILI9320_SPI_IDCODE
| ILI9320_SPI_ID(1);
228 ili
->client
= client
;
229 ili
->power
= FB_BLANK_POWERDOWN
;
232 spi_set_drvdata(spi
, ili
);
234 ili9320_setup_spi(ili
, spi
);
236 lcd
= devm_lcd_device_register(&spi
->dev
, "ili9320", dev
, ili
,
239 dev_err(dev
, "failed to register lcd device\n");
245 dev_info(dev
, "initialising %s\n", client
->name
);
247 ret
= ili9320_power(ili
, FB_BLANK_UNBLANK
);
249 dev_err(dev
, "failed to set lcd power state\n");
255 EXPORT_SYMBOL_GPL(ili9320_probe_spi
);
257 int ili9320_remove(struct ili9320
*ili
)
259 ili9320_power(ili
, FB_BLANK_POWERDOWN
);
262 EXPORT_SYMBOL_GPL(ili9320_remove
);
264 #ifdef CONFIG_PM_SLEEP
265 int ili9320_suspend(struct ili9320
*lcd
)
269 ret
= ili9320_power(lcd
, FB_BLANK_POWERDOWN
);
271 if (lcd
->platdata
->suspend
== ILI9320_SUSPEND_DEEP
) {
272 ili9320_write(lcd
, ILI9320_POWER1
, lcd
->power1
|
274 ILI9320_POWER1_DSTB
);
275 lcd
->initialised
= 0;
280 EXPORT_SYMBOL_GPL(ili9320_suspend
);
282 int ili9320_resume(struct ili9320
*lcd
)
284 dev_info(lcd
->dev
, "resuming from power state %d\n", lcd
->power
);
286 if (lcd
->platdata
->suspend
== ILI9320_SUSPEND_DEEP
)
287 ili9320_write(lcd
, ILI9320_POWER1
, 0x00);
289 return ili9320_power(lcd
, FB_BLANK_UNBLANK
);
291 EXPORT_SYMBOL_GPL(ili9320_resume
);
294 /* Power down all displays on reboot, poweroff or halt */
295 void ili9320_shutdown(struct ili9320
*lcd
)
297 ili9320_power(lcd
, FB_BLANK_POWERDOWN
);
299 EXPORT_SYMBOL_GPL(ili9320_shutdown
);
301 MODULE_AUTHOR("Ben Dooks <ben-linux@fluff.org>");
302 MODULE_DESCRIPTION("ILI9320 LCD Driver");
303 MODULE_LICENSE("GPL v2");