tosa_lcd.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * LCD / Backlight control code for Sharp SL-6000x (tosa)
  3. *
  4. * Copyright (c) 2005 Dirk Opfer
  5. * Copyright (c) 2007,2008 Dmitry Baryshkov
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/i2c.h>
  17. #include <linux/slab.h>
  18. #include <linux/gpio.h>
  19. #include <linux/delay.h>
  20. #include <linux/lcd.h>
  21. #include <linux/fb.h>
  22. #include <asm/mach/sharpsl_param.h>
  23. #include <mach/tosa.h>
  24. #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL)
  25. #define TG_REG0_VQV 0x0001
  26. #define TG_REG0_COLOR 0x0002
  27. #define TG_REG0_UD 0x0004
  28. #define TG_REG0_LR 0x0008
  29. #define DAC_BASE 0x4e
  30. struct tosa_lcd_data {
  31. struct spi_device *spi;
  32. struct lcd_device *lcd;
  33. struct i2c_client *i2c;
  34. int lcd_power;
  35. bool is_vga;
  36. };
  37. static int tosa_tg_send(struct spi_device *spi, int adrs, uint8_t data)
  38. {
  39. u8 buf[1];
  40. struct spi_message msg;
  41. struct spi_transfer xfer = {
  42. .len = 1,
  43. .cs_change = 1,
  44. .tx_buf = buf,
  45. };
  46. buf[0] = ((adrs & 0x07) << 5) | (data & 0x1f);
  47. spi_message_init(&msg);
  48. spi_message_add_tail(&xfer, &msg);
  49. return spi_sync(spi, &msg);
  50. }
  51. int tosa_bl_enable(struct spi_device *spi, int enable)
  52. {
  53. /* bl_enable GP04=1 otherwise GP04=0*/
  54. return tosa_tg_send(spi, TG_GPODR2, enable ? 0x01 : 0x00);
  55. }
  56. EXPORT_SYMBOL(tosa_bl_enable);
  57. static void tosa_lcd_tg_init(struct tosa_lcd_data *data)
  58. {
  59. /* TG on */
  60. gpio_set_value(TOSA_GPIO_TG_ON, 0);
  61. mdelay(60);
  62. /* delayed 0clk TCTL signal for VGA */
  63. tosa_tg_send(data->spi, TG_TPOSCTL, 0x00);
  64. /* GPOS0=powercontrol, GPOS1=GPIO, GPOS2=TCTL */
  65. tosa_tg_send(data->spi, TG_GPOSR, 0x02);
  66. }
  67. static void tosa_lcd_tg_on(struct tosa_lcd_data *data)
  68. {
  69. struct spi_device *spi = data->spi;
  70. int value = TG_REG0_COLOR | TG_REG0_UD | TG_REG0_LR;
  71. if (data->is_vga)
  72. value |= TG_REG0_VQV;
  73. tosa_tg_send(spi, TG_PNLCTL, value);
  74. /* TG LCD pannel power up */
  75. tosa_tg_send(spi, TG_PINICTL, 0x4);
  76. mdelay(50);
  77. /* TG LCD GVSS */
  78. tosa_tg_send(spi, TG_PINICTL, 0x0);
  79. if (!data->i2c) {
  80. /*
  81. * after the pannel is powered up the first time,
  82. * we can access the i2c bus so probe for the DAC
  83. */
  84. struct i2c_adapter *adap = i2c_get_adapter(0);
  85. struct i2c_board_info info = {
  86. .type = "tosa-bl",
  87. .addr = DAC_BASE,
  88. .platform_data = data->spi,
  89. };
  90. data->i2c = i2c_new_device(adap, &info);
  91. }
  92. }
  93. static void tosa_lcd_tg_off(struct tosa_lcd_data *data)
  94. {
  95. struct spi_device *spi = data->spi;
  96. /* TG LCD VHSA off */
  97. tosa_tg_send(spi, TG_PINICTL, 0x4);
  98. mdelay(50);
  99. /* TG LCD signal off */
  100. tosa_tg_send(spi, TG_PINICTL, 0x6);
  101. mdelay(50);
  102. /* TG Off */
  103. gpio_set_value(TOSA_GPIO_TG_ON, 1);
  104. mdelay(100);
  105. }
  106. int tosa_lcd_set_power(struct lcd_device *lcd, int power)
  107. {
  108. struct tosa_lcd_data *data = lcd_get_data(lcd);
  109. if (POWER_IS_ON(power) && !POWER_IS_ON(data->lcd_power))
  110. tosa_lcd_tg_on(data);
  111. if (!POWER_IS_ON(power) && POWER_IS_ON(data->lcd_power))
  112. tosa_lcd_tg_off(data);
  113. data->lcd_power = power;
  114. return 0;
  115. }
  116. static int tosa_lcd_get_power(struct lcd_device *lcd)
  117. {
  118. struct tosa_lcd_data *data = lcd_get_data(lcd);
  119. return data->lcd_power;
  120. }
  121. static int tosa_lcd_set_mode(struct lcd_device *lcd, struct fb_videomode *mode)
  122. {
  123. struct tosa_lcd_data *data = lcd_get_data(lcd);
  124. if (mode->xres == 320 || mode->yres == 320)
  125. data->is_vga = false;
  126. else
  127. data->is_vga = true;
  128. if (POWER_IS_ON(data->lcd_power))
  129. tosa_lcd_tg_on(data);
  130. return 0;
  131. }
  132. static struct lcd_ops tosa_lcd_ops = {
  133. .set_power = tosa_lcd_set_power,
  134. .get_power = tosa_lcd_get_power,
  135. .set_mode = tosa_lcd_set_mode,
  136. };
  137. static int tosa_lcd_probe(struct spi_device *spi)
  138. {
  139. int ret;
  140. struct tosa_lcd_data *data;
  141. data = devm_kzalloc(&spi->dev, sizeof(struct tosa_lcd_data),
  142. GFP_KERNEL);
  143. if (!data)
  144. return -ENOMEM;
  145. data->is_vga = true; /* default to VGA mode */
  146. /*
  147. * bits_per_word cannot be configured in platform data
  148. */
  149. spi->bits_per_word = 8;
  150. ret = spi_setup(spi);
  151. if (ret < 0)
  152. return ret;
  153. data->spi = spi;
  154. spi_set_drvdata(spi, data);
  155. ret = devm_gpio_request_one(&spi->dev, TOSA_GPIO_TG_ON,
  156. GPIOF_OUT_INIT_LOW, "tg #pwr");
  157. if (ret < 0)
  158. return ret;
  159. mdelay(60);
  160. tosa_lcd_tg_init(data);
  161. tosa_lcd_tg_on(data);
  162. data->lcd = devm_lcd_device_register(&spi->dev, "tosa-lcd", &spi->dev,
  163. data, &tosa_lcd_ops);
  164. if (IS_ERR(data->lcd)) {
  165. ret = PTR_ERR(data->lcd);
  166. data->lcd = NULL;
  167. goto err_register;
  168. }
  169. return 0;
  170. err_register:
  171. tosa_lcd_tg_off(data);
  172. return ret;
  173. }
  174. static int tosa_lcd_remove(struct spi_device *spi)
  175. {
  176. struct tosa_lcd_data *data = spi_get_drvdata(spi);
  177. if (data->i2c)
  178. i2c_unregister_device(data->i2c);
  179. tosa_lcd_tg_off(data);
  180. return 0;
  181. }
  182. #ifdef CONFIG_PM_SLEEP
  183. static int tosa_lcd_suspend(struct device *dev)
  184. {
  185. struct tosa_lcd_data *data = dev_get_drvdata(dev);
  186. tosa_lcd_tg_off(data);
  187. return 0;
  188. }
  189. static int tosa_lcd_resume(struct device *dev)
  190. {
  191. struct tosa_lcd_data *data = dev_get_drvdata(dev);
  192. tosa_lcd_tg_init(data);
  193. if (POWER_IS_ON(data->lcd_power))
  194. tosa_lcd_tg_on(data);
  195. else
  196. tosa_lcd_tg_off(data);
  197. return 0;
  198. }
  199. #endif
  200. static SIMPLE_DEV_PM_OPS(tosa_lcd_pm_ops, tosa_lcd_suspend, tosa_lcd_resume);
  201. static struct spi_driver tosa_lcd_driver = {
  202. .driver = {
  203. .name = "tosa-lcd",
  204. .pm = &tosa_lcd_pm_ops,
  205. },
  206. .probe = tosa_lcd_probe,
  207. .remove = tosa_lcd_remove,
  208. };
  209. module_spi_driver(tosa_lcd_driver);
  210. MODULE_AUTHOR("Dmitry Baryshkov");
  211. MODULE_LICENSE("GPL v2");
  212. MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA");
  213. MODULE_ALIAS("spi:tosa-lcd");