ltv350qv.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Power control for Samsung LTV350QV Quarter VGA LCD Panel
  3. *
  4. * Copyright (C) 2006, 2007 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/fb.h>
  13. #include <linux/init.h>
  14. #include <linux/lcd.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/spi/spi.h>
  18. #include "ltv350qv.h"
  19. #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL)
  20. struct ltv350qv {
  21. struct spi_device *spi;
  22. u8 *buffer;
  23. int power;
  24. struct lcd_device *ld;
  25. };
  26. /*
  27. * The power-on and power-off sequences are taken from the
  28. * LTV350QV-F04 data sheet from Samsung. The register definitions are
  29. * taken from the S6F2002 command list also from Samsung. Both
  30. * documents are distributed with the AVR32 Linux BSP CD from Atmel.
  31. *
  32. * There's still some voodoo going on here, but it's a lot better than
  33. * in the first incarnation of the driver where all we had was the raw
  34. * numbers from the initialization sequence.
  35. */
  36. static int ltv350qv_write_reg(struct ltv350qv *lcd, u8 reg, u16 val)
  37. {
  38. struct spi_message msg;
  39. struct spi_transfer index_xfer = {
  40. .len = 3,
  41. .cs_change = 1,
  42. };
  43. struct spi_transfer value_xfer = {
  44. .len = 3,
  45. };
  46. spi_message_init(&msg);
  47. /* register index */
  48. lcd->buffer[0] = LTV_OPC_INDEX;
  49. lcd->buffer[1] = 0x00;
  50. lcd->buffer[2] = reg & 0x7f;
  51. index_xfer.tx_buf = lcd->buffer;
  52. spi_message_add_tail(&index_xfer, &msg);
  53. /* register value */
  54. lcd->buffer[4] = LTV_OPC_DATA;
  55. lcd->buffer[5] = val >> 8;
  56. lcd->buffer[6] = val;
  57. value_xfer.tx_buf = lcd->buffer + 4;
  58. spi_message_add_tail(&value_xfer, &msg);
  59. return spi_sync(lcd->spi, &msg);
  60. }
  61. /* The comments are taken straight from the data sheet */
  62. static int ltv350qv_power_on(struct ltv350qv *lcd)
  63. {
  64. int ret;
  65. /* Power On Reset Display off State */
  66. if (ltv350qv_write_reg(lcd, LTV_PWRCTL1, 0x0000))
  67. goto err;
  68. usleep_range(15000, 16000);
  69. /* Power Setting Function 1 */
  70. if (ltv350qv_write_reg(lcd, LTV_PWRCTL1, LTV_VCOM_DISABLE))
  71. goto err;
  72. if (ltv350qv_write_reg(lcd, LTV_PWRCTL2, LTV_VCOML_ENABLE))
  73. goto err_power1;
  74. /* Power Setting Function 2 */
  75. if (ltv350qv_write_reg(lcd, LTV_PWRCTL1,
  76. LTV_VCOM_DISABLE | LTV_DRIVE_CURRENT(5)
  77. | LTV_SUPPLY_CURRENT(5)))
  78. goto err_power2;
  79. msleep(55);
  80. /* Instruction Setting */
  81. ret = ltv350qv_write_reg(lcd, LTV_IFCTL,
  82. LTV_NMD | LTV_REV | LTV_NL(0x1d));
  83. ret |= ltv350qv_write_reg(lcd, LTV_DATACTL,
  84. LTV_DS_SAME | LTV_CHS_480
  85. | LTV_DF_RGB | LTV_RGB_BGR);
  86. ret |= ltv350qv_write_reg(lcd, LTV_ENTRY_MODE,
  87. LTV_VSPL_ACTIVE_LOW
  88. | LTV_HSPL_ACTIVE_LOW
  89. | LTV_DPL_SAMPLE_RISING
  90. | LTV_EPL_ACTIVE_LOW
  91. | LTV_SS_RIGHT_TO_LEFT);
  92. ret |= ltv350qv_write_reg(lcd, LTV_GATECTL1, LTV_CLW(3));
  93. ret |= ltv350qv_write_reg(lcd, LTV_GATECTL2,
  94. LTV_NW_INV_1LINE | LTV_FWI(3));
  95. ret |= ltv350qv_write_reg(lcd, LTV_VBP, 0x000a);
  96. ret |= ltv350qv_write_reg(lcd, LTV_HBP, 0x0021);
  97. ret |= ltv350qv_write_reg(lcd, LTV_SOTCTL, LTV_SDT(3) | LTV_EQ(0));
  98. ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(0), 0x0103);
  99. ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(1), 0x0301);
  100. ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(2), 0x1f0f);
  101. ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(3), 0x1f0f);
  102. ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(4), 0x0707);
  103. ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(5), 0x0307);
  104. ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(6), 0x0707);
  105. ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(7), 0x0000);
  106. ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(8), 0x0004);
  107. ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(9), 0x0000);
  108. if (ret)
  109. goto err_settings;
  110. /* Wait more than 2 frames */
  111. msleep(20);
  112. /* Display On Sequence */
  113. ret = ltv350qv_write_reg(lcd, LTV_PWRCTL1,
  114. LTV_VCOM_DISABLE | LTV_VCOMOUT_ENABLE
  115. | LTV_POWER_ON | LTV_DRIVE_CURRENT(5)
  116. | LTV_SUPPLY_CURRENT(5));
  117. ret |= ltv350qv_write_reg(lcd, LTV_GATECTL2,
  118. LTV_NW_INV_1LINE | LTV_DSC | LTV_FWI(3));
  119. if (ret)
  120. goto err_disp_on;
  121. /* Display should now be ON. Phew. */
  122. return 0;
  123. err_disp_on:
  124. /*
  125. * Try to recover. Error handling probably isn't very useful
  126. * at this point, just make a best effort to switch the panel
  127. * off.
  128. */
  129. ltv350qv_write_reg(lcd, LTV_PWRCTL1,
  130. LTV_VCOM_DISABLE | LTV_DRIVE_CURRENT(5)
  131. | LTV_SUPPLY_CURRENT(5));
  132. ltv350qv_write_reg(lcd, LTV_GATECTL2,
  133. LTV_NW_INV_1LINE | LTV_FWI(3));
  134. err_settings:
  135. err_power2:
  136. err_power1:
  137. ltv350qv_write_reg(lcd, LTV_PWRCTL2, 0x0000);
  138. usleep_range(1000, 1100);
  139. err:
  140. ltv350qv_write_reg(lcd, LTV_PWRCTL1, LTV_VCOM_DISABLE);
  141. return -EIO;
  142. }
  143. static int ltv350qv_power_off(struct ltv350qv *lcd)
  144. {
  145. int ret;
  146. /* Display Off Sequence */
  147. ret = ltv350qv_write_reg(lcd, LTV_PWRCTL1,
  148. LTV_VCOM_DISABLE
  149. | LTV_DRIVE_CURRENT(5)
  150. | LTV_SUPPLY_CURRENT(5));
  151. ret |= ltv350qv_write_reg(lcd, LTV_GATECTL2,
  152. LTV_NW_INV_1LINE | LTV_FWI(3));
  153. /* Power down setting 1 */
  154. ret |= ltv350qv_write_reg(lcd, LTV_PWRCTL2, 0x0000);
  155. /* Wait at least 1 ms */
  156. usleep_range(1000, 1100);
  157. /* Power down setting 2 */
  158. ret |= ltv350qv_write_reg(lcd, LTV_PWRCTL1, LTV_VCOM_DISABLE);
  159. /*
  160. * No point in trying to recover here. If we can't switch the
  161. * panel off, what are we supposed to do other than inform the
  162. * user about the failure?
  163. */
  164. if (ret)
  165. return -EIO;
  166. /* Display power should now be OFF */
  167. return 0;
  168. }
  169. static int ltv350qv_power(struct ltv350qv *lcd, int power)
  170. {
  171. int ret = 0;
  172. if (POWER_IS_ON(power) && !POWER_IS_ON(lcd->power))
  173. ret = ltv350qv_power_on(lcd);
  174. else if (!POWER_IS_ON(power) && POWER_IS_ON(lcd->power))
  175. ret = ltv350qv_power_off(lcd);
  176. if (!ret)
  177. lcd->power = power;
  178. return ret;
  179. }
  180. static int ltv350qv_set_power(struct lcd_device *ld, int power)
  181. {
  182. struct ltv350qv *lcd = lcd_get_data(ld);
  183. return ltv350qv_power(lcd, power);
  184. }
  185. static int ltv350qv_get_power(struct lcd_device *ld)
  186. {
  187. struct ltv350qv *lcd = lcd_get_data(ld);
  188. return lcd->power;
  189. }
  190. static struct lcd_ops ltv_ops = {
  191. .get_power = ltv350qv_get_power,
  192. .set_power = ltv350qv_set_power,
  193. };
  194. static int ltv350qv_probe(struct spi_device *spi)
  195. {
  196. struct ltv350qv *lcd;
  197. struct lcd_device *ld;
  198. int ret;
  199. lcd = devm_kzalloc(&spi->dev, sizeof(struct ltv350qv), GFP_KERNEL);
  200. if (!lcd)
  201. return -ENOMEM;
  202. lcd->spi = spi;
  203. lcd->power = FB_BLANK_POWERDOWN;
  204. lcd->buffer = devm_kzalloc(&spi->dev, 8, GFP_KERNEL);
  205. if (!lcd->buffer)
  206. return -ENOMEM;
  207. ld = devm_lcd_device_register(&spi->dev, "ltv350qv", &spi->dev, lcd,
  208. &ltv_ops);
  209. if (IS_ERR(ld))
  210. return PTR_ERR(ld);
  211. lcd->ld = ld;
  212. ret = ltv350qv_power(lcd, FB_BLANK_UNBLANK);
  213. if (ret)
  214. return ret;
  215. spi_set_drvdata(spi, lcd);
  216. return 0;
  217. }
  218. static int ltv350qv_remove(struct spi_device *spi)
  219. {
  220. struct ltv350qv *lcd = spi_get_drvdata(spi);
  221. ltv350qv_power(lcd, FB_BLANK_POWERDOWN);
  222. return 0;
  223. }
  224. #ifdef CONFIG_PM_SLEEP
  225. static int ltv350qv_suspend(struct device *dev)
  226. {
  227. struct ltv350qv *lcd = dev_get_drvdata(dev);
  228. return ltv350qv_power(lcd, FB_BLANK_POWERDOWN);
  229. }
  230. static int ltv350qv_resume(struct device *dev)
  231. {
  232. struct ltv350qv *lcd = dev_get_drvdata(dev);
  233. return ltv350qv_power(lcd, FB_BLANK_UNBLANK);
  234. }
  235. #endif
  236. static SIMPLE_DEV_PM_OPS(ltv350qv_pm_ops, ltv350qv_suspend, ltv350qv_resume);
  237. /* Power down all displays on reboot, poweroff or halt */
  238. static void ltv350qv_shutdown(struct spi_device *spi)
  239. {
  240. struct ltv350qv *lcd = spi_get_drvdata(spi);
  241. ltv350qv_power(lcd, FB_BLANK_POWERDOWN);
  242. }
  243. static struct spi_driver ltv350qv_driver = {
  244. .driver = {
  245. .name = "ltv350qv",
  246. .owner = THIS_MODULE,
  247. .pm = &ltv350qv_pm_ops,
  248. },
  249. .probe = ltv350qv_probe,
  250. .remove = ltv350qv_remove,
  251. .shutdown = ltv350qv_shutdown,
  252. };
  253. module_spi_driver(ltv350qv_driver);
  254. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  255. MODULE_DESCRIPTION("Samsung LTV350QV LCD Driver");
  256. MODULE_LICENSE("GPL");
  257. MODULE_ALIAS("spi:ltv350qv");