l4f00242t03.c 7.3 KB

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