Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * l4f00242t03.c -- support for Epson L4F00242T03 LCD |
| 3 | * |
| 4 | * Copyright 2007-2009 Freescale Semiconductor, Inc. All Rights Reserved. |
| 5 | * |
| 6 | * Copyright (c) 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com> |
| 7 | * Inspired by Marek Vasut work in l4f00242t03.c |
| 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/device.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/gpio.h> |
| 18 | #include <linux/lcd.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 20 | #include <linux/regulator/consumer.h> |
| 21 | |
| 22 | #include <linux/spi/spi.h> |
| 23 | #include <linux/spi/l4f00242t03.h> |
| 24 | |
| 25 | struct l4f00242t03_priv { |
| 26 | struct spi_device *spi; |
| 27 | struct lcd_device *ld; |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 28 | int lcd_state; |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 29 | struct regulator *io_reg; |
| 30 | struct regulator *core_reg; |
| 31 | }; |
| 32 | |
| 33 | |
| 34 | static void l4f00242t03_reset(unsigned int gpio) |
| 35 | { |
| 36 | pr_debug("l4f00242t03_reset.\n"); |
| 37 | gpio_set_value(gpio, 1); |
| 38 | mdelay(100); |
| 39 | gpio_set_value(gpio, 0); |
| 40 | mdelay(10); /* tRES >= 100us */ |
| 41 | gpio_set_value(gpio, 1); |
| 42 | mdelay(20); |
| 43 | } |
| 44 | |
| 45 | #define param(x) ((x) | 0x100) |
| 46 | |
| 47 | static void l4f00242t03_lcd_init(struct spi_device *spi) |
| 48 | { |
| 49 | struct l4f00242t03_pdata *pdata = spi->dev.platform_data; |
| 50 | struct l4f00242t03_priv *priv = dev_get_drvdata(&spi->dev); |
| 51 | const u16 cmd[] = { 0x36, param(0), 0x3A, param(0x60) }; |
| 52 | |
| 53 | dev_dbg(&spi->dev, "initializing LCD\n"); |
| 54 | |
| 55 | if (priv->io_reg) { |
| 56 | regulator_set_voltage(priv->io_reg, 1800000, 1800000); |
| 57 | regulator_enable(priv->io_reg); |
| 58 | } |
| 59 | |
| 60 | if (priv->core_reg) { |
| 61 | regulator_set_voltage(priv->core_reg, 2800000, 2800000); |
| 62 | regulator_enable(priv->core_reg); |
| 63 | } |
| 64 | |
| 65 | gpio_set_value(pdata->data_enable_gpio, 1); |
| 66 | msleep(60); |
| 67 | spi_write(spi, (const u8 *)cmd, ARRAY_SIZE(cmd) * sizeof(u16)); |
| 68 | } |
| 69 | |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 70 | static void l4f00242t03_lcd_powerdown(struct spi_device *spi) |
| 71 | { |
| 72 | struct l4f00242t03_pdata *pdata = spi->dev.platform_data; |
| 73 | struct l4f00242t03_priv *priv = dev_get_drvdata(&spi->dev); |
| 74 | |
| 75 | dev_dbg(&spi->dev, "Powering down LCD\n"); |
| 76 | |
| 77 | gpio_set_value(pdata->data_enable_gpio, 0); |
| 78 | |
| 79 | if (priv->io_reg) |
| 80 | regulator_disable(priv->io_reg); |
| 81 | |
| 82 | if (priv->core_reg) |
| 83 | regulator_disable(priv->core_reg); |
| 84 | } |
| 85 | |
| 86 | static int l4f00242t03_lcd_power_get(struct lcd_device *ld) |
| 87 | { |
| 88 | struct l4f00242t03_priv *priv = lcd_get_data(ld); |
| 89 | |
| 90 | return priv->lcd_state; |
| 91 | } |
| 92 | |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 93 | static int l4f00242t03_lcd_power_set(struct lcd_device *ld, int power) |
| 94 | { |
| 95 | struct l4f00242t03_priv *priv = lcd_get_data(ld); |
| 96 | struct spi_device *spi = priv->spi; |
| 97 | |
| 98 | const u16 slpout = 0x11; |
| 99 | const u16 dison = 0x29; |
| 100 | |
| 101 | const u16 slpin = 0x10; |
| 102 | const u16 disoff = 0x28; |
| 103 | |
Marek Vasut | 4dbdf88 | 2010-11-11 14:05:26 -0800 | [diff] [blame] | 104 | if (power <= FB_BLANK_NORMAL) { |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 105 | if (priv->lcd_state <= FB_BLANK_NORMAL) { |
| 106 | /* Do nothing, the LCD is running */ |
| 107 | } else if (priv->lcd_state < FB_BLANK_POWERDOWN) { |
| 108 | dev_dbg(&spi->dev, "Resuming LCD\n"); |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 109 | |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 110 | spi_write(spi, (const u8 *)&slpout, sizeof(u16)); |
| 111 | msleep(60); |
| 112 | spi_write(spi, (const u8 *)&dison, sizeof(u16)); |
| 113 | } else { |
| 114 | /* priv->lcd_state == FB_BLANK_POWERDOWN */ |
| 115 | l4f00242t03_lcd_init(spi); |
| 116 | priv->lcd_state = FB_BLANK_VSYNC_SUSPEND; |
| 117 | l4f00242t03_lcd_power_set(priv->ld, power); |
| 118 | } |
| 119 | } else if (power < FB_BLANK_POWERDOWN) { |
| 120 | if (priv->lcd_state <= FB_BLANK_NORMAL) { |
| 121 | /* Send the display in standby */ |
| 122 | dev_dbg(&spi->dev, "Standby the LCD\n"); |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 123 | |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 124 | spi_write(spi, (const u8 *)&disoff, sizeof(u16)); |
| 125 | msleep(60); |
| 126 | spi_write(spi, (const u8 *)&slpin, sizeof(u16)); |
| 127 | } else if (priv->lcd_state < FB_BLANK_POWERDOWN) { |
| 128 | /* Do nothing, the LCD is already in standby */ |
| 129 | } else { |
| 130 | /* priv->lcd_state == FB_BLANK_POWERDOWN */ |
| 131 | l4f00242t03_lcd_init(spi); |
| 132 | priv->lcd_state = FB_BLANK_UNBLANK; |
| 133 | l4f00242t03_lcd_power_set(ld, power); |
| 134 | } |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 135 | } else { |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 136 | /* power == FB_BLANK_POWERDOWN */ |
| 137 | /* Clear the screen before shutting down */ |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 138 | spi_write(spi, (const u8 *)&disoff, sizeof(u16)); |
| 139 | msleep(60); |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 140 | l4f00242t03_lcd_powerdown(spi); |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 143 | priv->lcd_state = power; |
| 144 | |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static struct lcd_ops l4f_ops = { |
| 149 | .set_power = l4f00242t03_lcd_power_set, |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 150 | .get_power = l4f00242t03_lcd_power_get, |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | static int __devinit l4f00242t03_probe(struct spi_device *spi) |
| 154 | { |
| 155 | struct l4f00242t03_priv *priv; |
| 156 | struct l4f00242t03_pdata *pdata = spi->dev.platform_data; |
| 157 | int ret; |
| 158 | |
| 159 | if (pdata == NULL) { |
| 160 | dev_err(&spi->dev, "Uninitialized platform data.\n"); |
| 161 | return -EINVAL; |
| 162 | } |
| 163 | |
| 164 | priv = kzalloc(sizeof(struct l4f00242t03_priv), GFP_KERNEL); |
| 165 | |
| 166 | if (priv == NULL) { |
| 167 | dev_err(&spi->dev, "No memory for this device.\n"); |
Axel Lin | beb0a43 | 2010-05-03 14:42:44 +0800 | [diff] [blame] | 168 | return -ENOMEM; |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | dev_set_drvdata(&spi->dev, priv); |
| 172 | spi->bits_per_word = 9; |
| 173 | spi_setup(spi); |
| 174 | |
| 175 | priv->spi = spi; |
| 176 | |
| 177 | ret = gpio_request(pdata->reset_gpio, "lcd l4f00242t03 reset"); |
| 178 | if (ret) { |
| 179 | dev_err(&spi->dev, |
| 180 | "Unable to get the lcd l4f00242t03 reset gpio.\n"); |
Axel Lin | beb0a43 | 2010-05-03 14:42:44 +0800 | [diff] [blame] | 181 | goto err; |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | ret = gpio_direction_output(pdata->reset_gpio, 1); |
| 185 | if (ret) |
| 186 | goto err2; |
| 187 | |
| 188 | ret = gpio_request(pdata->data_enable_gpio, |
| 189 | "lcd l4f00242t03 data enable"); |
| 190 | if (ret) { |
| 191 | dev_err(&spi->dev, |
| 192 | "Unable to get the lcd l4f00242t03 data en gpio.\n"); |
Axel Lin | beb0a43 | 2010-05-03 14:42:44 +0800 | [diff] [blame] | 193 | goto err2; |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | ret = gpio_direction_output(pdata->data_enable_gpio, 0); |
| 197 | if (ret) |
| 198 | goto err3; |
| 199 | |
| 200 | if (pdata->io_supply) { |
| 201 | priv->io_reg = regulator_get(NULL, pdata->io_supply); |
| 202 | |
| 203 | if (IS_ERR(priv->io_reg)) { |
| 204 | pr_err("%s: Unable to get the IO regulator\n", |
| 205 | __func__); |
| 206 | goto err3; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (pdata->core_supply) { |
| 211 | priv->core_reg = regulator_get(NULL, pdata->core_supply); |
| 212 | |
| 213 | if (IS_ERR(priv->core_reg)) { |
| 214 | pr_err("%s: Unable to get the core regulator\n", |
| 215 | __func__); |
| 216 | goto err4; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | priv->ld = lcd_device_register("l4f00242t03", |
| 221 | &spi->dev, priv, &l4f_ops); |
| 222 | if (IS_ERR(priv->ld)) { |
| 223 | ret = PTR_ERR(priv->ld); |
| 224 | goto err5; |
| 225 | } |
| 226 | |
| 227 | /* Init the LCD */ |
| 228 | l4f00242t03_reset(pdata->reset_gpio); |
| 229 | l4f00242t03_lcd_init(spi); |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 230 | priv->lcd_state = FB_BLANK_VSYNC_SUSPEND; |
| 231 | l4f00242t03_lcd_power_set(priv->ld, FB_BLANK_UNBLANK); |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 232 | |
| 233 | dev_info(&spi->dev, "Epson l4f00242t03 lcd probed.\n"); |
| 234 | |
| 235 | return 0; |
| 236 | |
| 237 | err5: |
| 238 | if (priv->core_reg) |
| 239 | regulator_put(priv->core_reg); |
| 240 | err4: |
| 241 | if (priv->io_reg) |
| 242 | regulator_put(priv->io_reg); |
| 243 | err3: |
| 244 | gpio_free(pdata->data_enable_gpio); |
| 245 | err2: |
| 246 | gpio_free(pdata->reset_gpio); |
| 247 | err: |
| 248 | kfree(priv); |
| 249 | |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | static int __devexit l4f00242t03_remove(struct spi_device *spi) |
| 254 | { |
| 255 | struct l4f00242t03_priv *priv = dev_get_drvdata(&spi->dev); |
| 256 | struct l4f00242t03_pdata *pdata = priv->spi->dev.platform_data; |
| 257 | |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 258 | l4f00242t03_lcd_power_set(priv->ld, FB_BLANK_POWERDOWN); |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 259 | lcd_device_unregister(priv->ld); |
| 260 | |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 261 | dev_set_drvdata(&spi->dev, NULL); |
| 262 | |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 263 | gpio_free(pdata->data_enable_gpio); |
| 264 | gpio_free(pdata->reset_gpio); |
| 265 | |
| 266 | if (priv->io_reg) |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 267 | regulator_put(priv->io_reg); |
Alberto Panizzo | 47306fc | 2010-04-26 22:51:08 +0200 | [diff] [blame] | 268 | if (priv->core_reg) |
| 269 | regulator_put(priv->core_reg); |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 270 | |
| 271 | kfree(priv); |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 276 | static void l4f00242t03_shutdown(struct spi_device *spi) |
| 277 | { |
| 278 | struct l4f00242t03_priv *priv = dev_get_drvdata(&spi->dev); |
| 279 | |
| 280 | if (priv) |
| 281 | l4f00242t03_lcd_power_set(priv->ld, FB_BLANK_POWERDOWN); |
| 282 | |
| 283 | } |
| 284 | |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 285 | static struct spi_driver l4f00242t03_driver = { |
| 286 | .driver = { |
| 287 | .name = "l4f00242t03", |
| 288 | .owner = THIS_MODULE, |
| 289 | }, |
| 290 | .probe = l4f00242t03_probe, |
| 291 | .remove = __devexit_p(l4f00242t03_remove), |
Alberto Panizzo | 95558ad | 2011-01-12 16:59:22 -0800 | [diff] [blame^] | 292 | .shutdown = l4f00242t03_shutdown, |
Alberto Panizzo | e7fb9c4 | 2009-12-18 16:42:11 +0100 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | static __init int l4f00242t03_init(void) |
| 296 | { |
| 297 | return spi_register_driver(&l4f00242t03_driver); |
| 298 | } |
| 299 | |
| 300 | static __exit void l4f00242t03_exit(void) |
| 301 | { |
| 302 | spi_unregister_driver(&l4f00242t03_driver); |
| 303 | } |
| 304 | |
| 305 | module_init(l4f00242t03_init); |
| 306 | module_exit(l4f00242t03_exit); |
| 307 | |
| 308 | MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>"); |
| 309 | MODULE_DESCRIPTION("EPSON L4F00242T03 LCD"); |
Alberto Panizzo | c3cf2e4 | 2010-01-19 09:30:50 +0100 | [diff] [blame] | 310 | MODULE_LICENSE("GPL v2"); |