tosa_lcd.c 5.7 KB

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