via-gpio.c 7.1 KB

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