Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 21 | |
| 22 | #include <linux/spi/spi.h> |
| 23 | |
| 24 | #include <video/ili9320.h> |
| 25 | |
| 26 | #include "ili9320.h" |
| 27 | |
| 28 | |
| 29 | static 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 | /* spi message consits of: |
| 38 | * first byte: ID and operation |
| 39 | */ |
| 40 | |
| 41 | addr[0] = spi->id | ILI9320_SPI_INDEX | ILI9320_SPI_WRITE; |
| 42 | addr[1] = reg >> 8; |
| 43 | addr[2] = reg; |
| 44 | |
| 45 | /* second message is the data to transfer */ |
| 46 | |
| 47 | data[0] = spi->id | ILI9320_SPI_DATA | ILI9320_SPI_WRITE; |
Jingoo Han | c0b6cc4 | 2012-12-17 16:00:19 -0800 | [diff] [blame] | 48 | data[1] = value >> 8; |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 49 | data[2] = value; |
| 50 | |
| 51 | return spi_sync(spi->dev, &spi->message); |
| 52 | } |
| 53 | |
| 54 | int 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 | } |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 59 | EXPORT_SYMBOL_GPL(ili9320_write); |
| 60 | |
| 61 | int ili9320_write_regs(struct ili9320 *ili, |
Jingoo Han | 3fd0043 | 2012-12-17 16:00:54 -0800 | [diff] [blame] | 62 | const struct ili9320_reg *values, |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 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 | } |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 76 | EXPORT_SYMBOL_GPL(ili9320_write_regs); |
| 77 | |
| 78 | static void ili9320_reset(struct ili9320 *lcd) |
| 79 | { |
| 80 | struct ili9320_platdata *cfg = lcd->platdata; |
| 81 | |
| 82 | cfg->reset(1); |
| 83 | mdelay(50); |
| 84 | |
| 85 | cfg->reset(0); |
| 86 | mdelay(50); |
| 87 | |
| 88 | cfg->reset(1); |
| 89 | mdelay(100); |
| 90 | } |
| 91 | |
| 92 | static inline int ili9320_init_chip(struct ili9320 *lcd) |
| 93 | { |
| 94 | int ret; |
| 95 | |
| 96 | ili9320_reset(lcd); |
| 97 | |
| 98 | ret = lcd->client->init(lcd, lcd->platdata); |
| 99 | if (ret != 0) { |
| 100 | dev_err(lcd->dev, "failed to initialise display\n"); |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | lcd->initialised = 1; |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static inline int ili9320_power_on(struct ili9320 *lcd) |
| 109 | { |
| 110 | if (!lcd->initialised) |
| 111 | ili9320_init_chip(lcd); |
| 112 | |
| 113 | lcd->display1 |= (ILI9320_DISPLAY1_D(3) | ILI9320_DISPLAY1_BASEE); |
| 114 | ili9320_write(lcd, ILI9320_DISPLAY1, lcd->display1); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static inline int ili9320_power_off(struct ili9320 *lcd) |
| 120 | { |
| 121 | lcd->display1 &= ~(ILI9320_DISPLAY1_D(3) | ILI9320_DISPLAY1_BASEE); |
| 122 | ili9320_write(lcd, ILI9320_DISPLAY1, lcd->display1); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL) |
| 128 | |
| 129 | static int ili9320_power(struct ili9320 *lcd, int power) |
| 130 | { |
| 131 | int ret = 0; |
| 132 | |
| 133 | dev_dbg(lcd->dev, "power %d => %d\n", lcd->power, power); |
| 134 | |
| 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); |
| 139 | |
| 140 | if (ret == 0) |
| 141 | lcd->power = power; |
| 142 | else |
| 143 | dev_warn(lcd->dev, "failed to set power mode %d\n", power); |
| 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | static inline struct ili9320 *to_our_lcd(struct lcd_device *lcd) |
| 149 | { |
| 150 | return lcd_get_data(lcd); |
| 151 | } |
| 152 | |
| 153 | static int ili9320_set_power(struct lcd_device *ld, int power) |
| 154 | { |
| 155 | struct ili9320 *lcd = to_our_lcd(ld); |
| 156 | |
| 157 | return ili9320_power(lcd, power); |
| 158 | } |
| 159 | |
| 160 | static int ili9320_get_power(struct lcd_device *ld) |
| 161 | { |
| 162 | struct ili9320 *lcd = to_our_lcd(ld); |
| 163 | |
| 164 | return lcd->power; |
| 165 | } |
| 166 | |
| 167 | static struct lcd_ops ili9320_ops = { |
| 168 | .get_power = ili9320_get_power, |
| 169 | .set_power = ili9320_set_power, |
| 170 | }; |
| 171 | |
Bill Pemberton | 1b9e450 | 2012-11-19 13:21:46 -0500 | [diff] [blame] | 172 | static void ili9320_setup_spi(struct ili9320 *ili, |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 173 | struct spi_device *dev) |
| 174 | { |
| 175 | struct ili9320_spi *spi = &ili->access.spi; |
| 176 | |
| 177 | ili->write = ili9320_write_spi; |
| 178 | spi->dev = dev; |
| 179 | |
| 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. |
| 183 | */ |
| 184 | |
| 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; |
| 192 | |
| 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); |
| 196 | } |
| 197 | |
Bill Pemberton | 1b9e450 | 2012-11-19 13:21:46 -0500 | [diff] [blame] | 198 | int ili9320_probe_spi(struct spi_device *spi, |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 199 | struct ili9320_client *client) |
| 200 | { |
Jingoo Han | c512794c | 2013-11-12 15:09:04 -0800 | [diff] [blame] | 201 | struct ili9320_platdata *cfg = dev_get_platdata(&spi->dev); |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 202 | struct device *dev = &spi->dev; |
| 203 | struct ili9320 *ili; |
| 204 | struct lcd_device *lcd; |
| 205 | int ret = 0; |
| 206 | |
| 207 | /* verify we where given some information */ |
| 208 | |
| 209 | if (cfg == NULL) { |
| 210 | dev_err(dev, "no platform data supplied\n"); |
| 211 | return -EINVAL; |
| 212 | } |
| 213 | |
| 214 | if (cfg->hsize <= 0 || cfg->vsize <= 0 || cfg->reset == NULL) { |
| 215 | dev_err(dev, "invalid platform data supplied\n"); |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | |
| 219 | /* allocate and initialse our state */ |
| 220 | |
Jingoo Han | 9828eb0 | 2012-05-29 15:07:22 -0700 | [diff] [blame] | 221 | ili = devm_kzalloc(&spi->dev, sizeof(struct ili9320), GFP_KERNEL); |
Jingoo Han | a82bdcf | 2014-04-03 14:49:02 -0700 | [diff] [blame] | 222 | if (ili == NULL) |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 223 | return -ENOMEM; |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 224 | |
| 225 | ili->access.spi.id = ILI9320_SPI_IDCODE | ILI9320_SPI_ID(1); |
| 226 | |
| 227 | ili->dev = dev; |
| 228 | ili->client = client; |
| 229 | ili->power = FB_BLANK_POWERDOWN; |
| 230 | ili->platdata = cfg; |
| 231 | |
Jingoo Han | 5a24b01 | 2013-04-29 16:17:56 -0700 | [diff] [blame] | 232 | spi_set_drvdata(spi, ili); |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 233 | |
| 234 | ili9320_setup_spi(ili, spi); |
| 235 | |
Jingoo Han | 5690378 | 2013-11-12 15:09:27 -0800 | [diff] [blame] | 236 | lcd = devm_lcd_device_register(&spi->dev, "ili9320", dev, ili, |
| 237 | &ili9320_ops); |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 238 | if (IS_ERR(lcd)) { |
| 239 | dev_err(dev, "failed to register lcd device\n"); |
Jingoo Han | 9828eb0 | 2012-05-29 15:07:22 -0700 | [diff] [blame] | 240 | return PTR_ERR(lcd); |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | ili->lcd = lcd; |
| 244 | |
| 245 | dev_info(dev, "initialising %s\n", client->name); |
| 246 | |
| 247 | ret = ili9320_power(ili, FB_BLANK_UNBLANK); |
| 248 | if (ret != 0) { |
| 249 | dev_err(dev, "failed to set lcd power state\n"); |
Jingoo Han | 5690378 | 2013-11-12 15:09:27 -0800 | [diff] [blame] | 250 | return ret; |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | return 0; |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 254 | } |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 255 | EXPORT_SYMBOL_GPL(ili9320_probe_spi); |
| 256 | |
Arnd Bergmann | ff94404 | 2012-05-15 15:02:58 -0600 | [diff] [blame] | 257 | int ili9320_remove(struct ili9320 *ili) |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 258 | { |
| 259 | ili9320_power(ili, FB_BLANK_POWERDOWN); |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 260 | return 0; |
| 261 | } |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 262 | EXPORT_SYMBOL_GPL(ili9320_remove); |
| 263 | |
Jingoo Han | eb39ad2 | 2013-04-29 16:18:01 -0700 | [diff] [blame] | 264 | #ifdef CONFIG_PM_SLEEP |
| 265 | int ili9320_suspend(struct ili9320 *lcd) |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 266 | { |
| 267 | int ret; |
| 268 | |
Jingoo Han | eb39ad2 | 2013-04-29 16:18:01 -0700 | [diff] [blame] | 269 | ret = ili9320_power(lcd, FB_BLANK_POWERDOWN); |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 270 | |
Jingoo Han | eb39ad2 | 2013-04-29 16:18:01 -0700 | [diff] [blame] | 271 | if (lcd->platdata->suspend == ILI9320_SUSPEND_DEEP) { |
| 272 | ili9320_write(lcd, ILI9320_POWER1, lcd->power1 | |
| 273 | ILI9320_POWER1_SLP | |
| 274 | ILI9320_POWER1_DSTB); |
| 275 | lcd->initialised = 0; |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Jingoo Han | eb39ad2 | 2013-04-29 16:18:01 -0700 | [diff] [blame] | 278 | return ret; |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 279 | } |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 280 | EXPORT_SYMBOL_GPL(ili9320_suspend); |
| 281 | |
| 282 | int ili9320_resume(struct ili9320 *lcd) |
| 283 | { |
| 284 | dev_info(lcd->dev, "resuming from power state %d\n", lcd->power); |
| 285 | |
Jingoo Han | c0b6cc4 | 2012-12-17 16:00:19 -0800 | [diff] [blame] | 286 | if (lcd->platdata->suspend == ILI9320_SUSPEND_DEEP) |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 287 | ili9320_write(lcd, ILI9320_POWER1, 0x00); |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 288 | |
| 289 | return ili9320_power(lcd, FB_BLANK_UNBLANK); |
| 290 | } |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 291 | EXPORT_SYMBOL_GPL(ili9320_resume); |
| 292 | #endif |
| 293 | |
| 294 | /* Power down all displays on reboot, poweroff or halt */ |
| 295 | void ili9320_shutdown(struct ili9320 *lcd) |
| 296 | { |
| 297 | ili9320_power(lcd, FB_BLANK_POWERDOWN); |
| 298 | } |
Ben Dooks | cccb6d3 | 2008-07-23 21:31:37 -0700 | [diff] [blame] | 299 | EXPORT_SYMBOL_GPL(ili9320_shutdown); |
| 300 | |
| 301 | MODULE_AUTHOR("Ben Dooks <ben-linux@fluff.org>"); |
| 302 | MODULE_DESCRIPTION("ILI9320 LCD Driver"); |
| 303 | MODULE_LICENSE("GPL v2"); |