via-gpio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Support for viafb GPIO ports.
  4. *
  5. * Copyright 2009 Jonathan Corbet <corbet@lwn.net>
  6. */
  7. #include <linux/spinlock.h>
  8. #include <linux/gpio/driver.h>
  9. #include <linux/gpio/machine.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/via-core.h>
  12. #include "via-gpio.h"
  13. /*
  14. * The ports we know about. Note that the port-25 gpios are not
  15. * mentioned in the datasheet.
  16. */
  17. struct viafb_gpio {
  18. char *vg_name; /* Data sheet name */
  19. u16 vg_io_port;
  20. u8 vg_port_index;
  21. int vg_mask_shift;
  22. };
  23. static struct viafb_gpio viafb_all_gpios[] = {
  24. {
  25. .vg_name = "VGPIO0", /* Guess - not in datasheet */
  26. .vg_io_port = VIASR,
  27. .vg_port_index = 0x25,
  28. .vg_mask_shift = 1
  29. },
  30. {
  31. .vg_name = "VGPIO1",
  32. .vg_io_port = VIASR,
  33. .vg_port_index = 0x25,
  34. .vg_mask_shift = 0
  35. },
  36. {
  37. .vg_name = "VGPIO2", /* aka DISPCLKI0 */
  38. .vg_io_port = VIASR,
  39. .vg_port_index = 0x2c,
  40. .vg_mask_shift = 1
  41. },
  42. {
  43. .vg_name = "VGPIO3", /* aka DISPCLKO0 */
  44. .vg_io_port = VIASR,
  45. .vg_port_index = 0x2c,
  46. .vg_mask_shift = 0
  47. },
  48. {
  49. .vg_name = "VGPIO4", /* DISPCLKI1 */
  50. .vg_io_port = VIASR,
  51. .vg_port_index = 0x3d,
  52. .vg_mask_shift = 1
  53. },
  54. {
  55. .vg_name = "VGPIO5", /* DISPCLKO1 */
  56. .vg_io_port = VIASR,
  57. .vg_port_index = 0x3d,
  58. .vg_mask_shift = 0
  59. },
  60. };
  61. #define VIAFB_NUM_GPIOS ARRAY_SIZE(viafb_all_gpios)
  62. /*
  63. * This structure controls the active GPIOs, which may be a subset
  64. * of those which are known.
  65. */
  66. struct viafb_gpio_cfg {
  67. struct gpio_chip gpio_chip;
  68. struct viafb_dev *vdev;
  69. struct viafb_gpio *active_gpios[VIAFB_NUM_GPIOS];
  70. const char *gpio_names[VIAFB_NUM_GPIOS];
  71. };
  72. /*
  73. * GPIO access functions
  74. */
  75. static int via_gpio_set(struct gpio_chip *chip, unsigned int nr, int value)
  76. {
  77. struct viafb_gpio_cfg *cfg = gpiochip_get_data(chip);
  78. u8 reg;
  79. struct viafb_gpio *gpio;
  80. unsigned long flags;
  81. spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
  82. gpio = cfg->active_gpios[nr];
  83. reg = via_read_reg(VIASR, gpio->vg_port_index);
  84. reg |= 0x40 << gpio->vg_mask_shift; /* output enable */
  85. if (value)
  86. reg |= 0x10 << gpio->vg_mask_shift;
  87. else
  88. reg &= ~(0x10 << gpio->vg_mask_shift);
  89. via_write_reg(VIASR, gpio->vg_port_index, reg);
  90. spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
  91. return 0;
  92. }
  93. static int via_gpio_dir_out(struct gpio_chip *chip, unsigned int nr,
  94. int value)
  95. {
  96. return via_gpio_set(chip, nr, value);
  97. }
  98. /*
  99. * Set the input direction. I'm not sure this is right; we should
  100. * be able to do input without disabling output.
  101. */
  102. static int via_gpio_dir_input(struct gpio_chip *chip, unsigned int nr)
  103. {
  104. struct viafb_gpio_cfg *cfg = gpiochip_get_data(chip);
  105. struct viafb_gpio *gpio;
  106. unsigned long flags;
  107. spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
  108. gpio = cfg->active_gpios[nr];
  109. via_write_reg_mask(VIASR, gpio->vg_port_index, 0,
  110. 0x40 << gpio->vg_mask_shift);
  111. spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
  112. return 0;
  113. }
  114. static int via_gpio_get(struct gpio_chip *chip, unsigned int nr)
  115. {
  116. struct viafb_gpio_cfg *cfg = gpiochip_get_data(chip);
  117. u8 reg;
  118. struct viafb_gpio *gpio;
  119. unsigned long flags;
  120. spin_lock_irqsave(&cfg->vdev->reg_lock, flags);
  121. gpio = cfg->active_gpios[nr];
  122. reg = via_read_reg(VIASR, gpio->vg_port_index);
  123. spin_unlock_irqrestore(&cfg->vdev->reg_lock, flags);
  124. return !!(reg & (0x04 << gpio->vg_mask_shift));
  125. }
  126. static struct viafb_gpio_cfg viafb_gpio_config = {
  127. .gpio_chip = {
  128. .label = "VIAFB onboard GPIO",
  129. .owner = THIS_MODULE,
  130. .direction_output = via_gpio_dir_out,
  131. .set = via_gpio_set,
  132. .direction_input = via_gpio_dir_input,
  133. .get = via_gpio_get,
  134. .base = -1,
  135. .ngpio = 0,
  136. .can_sleep = 0
  137. }
  138. };
  139. /*
  140. * Manage the software enable bit.
  141. */
  142. static void viafb_gpio_enable(struct viafb_gpio *gpio)
  143. {
  144. via_write_reg_mask(VIASR, gpio->vg_port_index, 0x02, 0x02);
  145. }
  146. static void viafb_gpio_disable(struct viafb_gpio *gpio)
  147. {
  148. via_write_reg_mask(VIASR, gpio->vg_port_index, 0, 0x02);
  149. }
  150. #ifdef CONFIG_PM
  151. static int viafb_gpio_suspend(void *private)
  152. {
  153. return 0;
  154. }
  155. static int viafb_gpio_resume(void *private)
  156. {
  157. int i;
  158. for (i = 0; i < viafb_gpio_config.gpio_chip.ngpio; i += 2)
  159. viafb_gpio_enable(viafb_gpio_config.active_gpios[i]);
  160. return 0;
  161. }
  162. static struct viafb_pm_hooks viafb_gpio_pm_hooks = {
  163. .suspend = viafb_gpio_suspend,
  164. .resume = viafb_gpio_resume
  165. };
  166. #endif /* CONFIG_PM */
  167. static struct gpiod_lookup_table viafb_gpio_table = {
  168. .dev_id = "viafb-camera",
  169. .table = {
  170. GPIO_LOOKUP("via-gpio", 2, "VGPIO2", GPIO_ACTIVE_LOW),
  171. GPIO_LOOKUP("via-gpio", 3, "VGPIO3", GPIO_ACTIVE_HIGH),
  172. { }
  173. },
  174. };
  175. /*
  176. * Platform device stuff.
  177. */
  178. static int viafb_gpio_probe(struct platform_device *platdev)
  179. {
  180. struct viafb_dev *vdev = platdev->dev.platform_data;
  181. struct via_port_cfg *port_cfg = vdev->port_cfg;
  182. int i, ngpio = 0, ret;
  183. struct viafb_gpio *gpio;
  184. unsigned long flags;
  185. /*
  186. * Set up entries for all GPIOs which have been configured to
  187. * operate as such (as opposed to as i2c ports).
  188. */
  189. for (i = 0; i < VIAFB_NUM_PORTS; i++) {
  190. if (port_cfg[i].mode != VIA_MODE_GPIO)
  191. continue;
  192. for (gpio = viafb_all_gpios;
  193. gpio < viafb_all_gpios + VIAFB_NUM_GPIOS; gpio++)
  194. if (gpio->vg_port_index == port_cfg[i].ioport_index) {
  195. viafb_gpio_config.active_gpios[ngpio] = gpio;
  196. viafb_gpio_config.gpio_names[ngpio] =
  197. gpio->vg_name;
  198. ngpio++;
  199. }
  200. }
  201. viafb_gpio_config.gpio_chip.ngpio = ngpio;
  202. viafb_gpio_config.gpio_chip.names = viafb_gpio_config.gpio_names;
  203. viafb_gpio_config.vdev = vdev;
  204. if (ngpio == 0) {
  205. printk(KERN_INFO "viafb: no GPIOs configured\n");
  206. return 0;
  207. }
  208. /*
  209. * Enable the ports. They come in pairs, with a single
  210. * enable bit for both.
  211. */
  212. spin_lock_irqsave(&viafb_gpio_config.vdev->reg_lock, flags);
  213. for (i = 0; i < ngpio; i += 2)
  214. viafb_gpio_enable(viafb_gpio_config.active_gpios[i]);
  215. spin_unlock_irqrestore(&viafb_gpio_config.vdev->reg_lock, flags);
  216. /*
  217. * Get registered.
  218. */
  219. viafb_gpio_config.gpio_chip.base = -1; /* Dynamic */
  220. viafb_gpio_config.gpio_chip.label = "via-gpio";
  221. ret = gpiochip_add_data(&viafb_gpio_config.gpio_chip,
  222. &viafb_gpio_config);
  223. if (ret) {
  224. printk(KERN_ERR "viafb: failed to add gpios (%d)\n", ret);
  225. viafb_gpio_config.gpio_chip.ngpio = 0;
  226. }
  227. gpiod_add_lookup_table(&viafb_gpio_table);
  228. #ifdef CONFIG_PM
  229. viafb_pm_register(&viafb_gpio_pm_hooks);
  230. #endif
  231. return ret;
  232. }
  233. static void viafb_gpio_remove(struct platform_device *platdev)
  234. {
  235. unsigned long flags;
  236. int i;
  237. #ifdef CONFIG_PM
  238. viafb_pm_unregister(&viafb_gpio_pm_hooks);
  239. #endif
  240. /*
  241. * Get unregistered.
  242. */
  243. if (viafb_gpio_config.gpio_chip.ngpio > 0) {
  244. gpiochip_remove(&viafb_gpio_config.gpio_chip);
  245. }
  246. /*
  247. * Disable the ports.
  248. */
  249. spin_lock_irqsave(&viafb_gpio_config.vdev->reg_lock, flags);
  250. for (i = 0; i < viafb_gpio_config.gpio_chip.ngpio; i += 2)
  251. viafb_gpio_disable(viafb_gpio_config.active_gpios[i]);
  252. viafb_gpio_config.gpio_chip.ngpio = 0;
  253. spin_unlock_irqrestore(&viafb_gpio_config.vdev->reg_lock, flags);
  254. }
  255. static struct platform_driver via_gpio_driver = {
  256. .driver = {
  257. .name = "viafb-gpio",
  258. },
  259. .probe = viafb_gpio_probe,
  260. .remove = viafb_gpio_remove,
  261. };
  262. int viafb_gpio_init(void)
  263. {
  264. return platform_driver_register(&via_gpio_driver);
  265. }
  266. void viafb_gpio_exit(void)
  267. {
  268. platform_driver_unregister(&via_gpio_driver);
  269. }