l4f00242t03.c 6.8 KB

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