gpio-vx855.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Linux GPIOlib driver for the VIA VX855 integrated southbridge GPIO
  4. *
  5. * Copyright (C) 2009 VIA Technologies, Inc.
  6. * Copyright (C) 2010 One Laptop per Child
  7. * Author: Harald Welte <[email protected]>
  8. * All rights reserved.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/slab.h>
  14. #include <linux/device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pci.h>
  17. #include <linux/io.h>
  18. #define MODULE_NAME "vx855_gpio"
  19. /* The VX855 south bridge has the following GPIO pins:
  20. * GPI 0...13 General Purpose Input
  21. * GPO 0...12 General Purpose Output
  22. * GPIO 0...14 General Purpose I/O (Open-Drain)
  23. */
  24. #define NR_VX855_GPI 14
  25. #define NR_VX855_GPO 13
  26. #define NR_VX855_GPIO 15
  27. #define NR_VX855_GPInO (NR_VX855_GPI + NR_VX855_GPO)
  28. #define NR_VX855_GP (NR_VX855_GPI + NR_VX855_GPO + NR_VX855_GPIO)
  29. struct vx855_gpio {
  30. struct gpio_chip gpio;
  31. spinlock_t lock;
  32. u32 io_gpi;
  33. u32 io_gpo;
  34. };
  35. /* resolve a GPIx into the corresponding bit position */
  36. static inline u_int32_t gpi_i_bit(int i)
  37. {
  38. if (i < 10)
  39. return 1 << i;
  40. else
  41. return 1 << (i + 14);
  42. }
  43. static inline u_int32_t gpo_o_bit(int i)
  44. {
  45. if (i < 11)
  46. return 1 << i;
  47. else
  48. return 1 << (i + 14);
  49. }
  50. static inline u_int32_t gpio_i_bit(int i)
  51. {
  52. if (i < 14)
  53. return 1 << (i + 10);
  54. else
  55. return 1 << (i + 14);
  56. }
  57. static inline u_int32_t gpio_o_bit(int i)
  58. {
  59. if (i < 14)
  60. return 1 << (i + 11);
  61. else
  62. return 1 << (i + 13);
  63. }
  64. /* Mapping between numeric GPIO ID and the actual GPIO hardware numbering:
  65. * 0..13 GPI 0..13
  66. * 14..26 GPO 0..12
  67. * 27..41 GPIO 0..14
  68. */
  69. static int vx855gpio_direction_input(struct gpio_chip *gpio,
  70. unsigned int nr)
  71. {
  72. struct vx855_gpio *vg = gpiochip_get_data(gpio);
  73. unsigned long flags;
  74. u_int32_t reg_out;
  75. /* Real GPI bits are always in input direction */
  76. if (nr < NR_VX855_GPI)
  77. return 0;
  78. /* Real GPO bits cannot be put in output direction */
  79. if (nr < NR_VX855_GPInO)
  80. return -EINVAL;
  81. /* Open Drain GPIO have to be set to one */
  82. spin_lock_irqsave(&vg->lock, flags);
  83. reg_out = inl(vg->io_gpo);
  84. reg_out |= gpio_o_bit(nr - NR_VX855_GPInO);
  85. outl(reg_out, vg->io_gpo);
  86. spin_unlock_irqrestore(&vg->lock, flags);
  87. return 0;
  88. }
  89. static int vx855gpio_get(struct gpio_chip *gpio, unsigned int nr)
  90. {
  91. struct vx855_gpio *vg = gpiochip_get_data(gpio);
  92. u_int32_t reg_in;
  93. int ret = 0;
  94. if (nr < NR_VX855_GPI) {
  95. reg_in = inl(vg->io_gpi);
  96. if (reg_in & gpi_i_bit(nr))
  97. ret = 1;
  98. } else if (nr < NR_VX855_GPInO) {
  99. /* GPO don't have an input bit, we need to read it
  100. * back from the output register */
  101. reg_in = inl(vg->io_gpo);
  102. if (reg_in & gpo_o_bit(nr - NR_VX855_GPI))
  103. ret = 1;
  104. } else {
  105. reg_in = inl(vg->io_gpi);
  106. if (reg_in & gpio_i_bit(nr - NR_VX855_GPInO))
  107. ret = 1;
  108. }
  109. return ret;
  110. }
  111. static void vx855gpio_set(struct gpio_chip *gpio, unsigned int nr,
  112. int val)
  113. {
  114. struct vx855_gpio *vg = gpiochip_get_data(gpio);
  115. unsigned long flags;
  116. u_int32_t reg_out;
  117. /* True GPI cannot be switched to output mode */
  118. if (nr < NR_VX855_GPI)
  119. return;
  120. spin_lock_irqsave(&vg->lock, flags);
  121. reg_out = inl(vg->io_gpo);
  122. if (nr < NR_VX855_GPInO) {
  123. if (val)
  124. reg_out |= gpo_o_bit(nr - NR_VX855_GPI);
  125. else
  126. reg_out &= ~gpo_o_bit(nr - NR_VX855_GPI);
  127. } else {
  128. if (val)
  129. reg_out |= gpio_o_bit(nr - NR_VX855_GPInO);
  130. else
  131. reg_out &= ~gpio_o_bit(nr - NR_VX855_GPInO);
  132. }
  133. outl(reg_out, vg->io_gpo);
  134. spin_unlock_irqrestore(&vg->lock, flags);
  135. }
  136. static int vx855gpio_direction_output(struct gpio_chip *gpio,
  137. unsigned int nr, int val)
  138. {
  139. /* True GPI cannot be switched to output mode */
  140. if (nr < NR_VX855_GPI)
  141. return -EINVAL;
  142. /* True GPO don't need to be switched to output mode,
  143. * and GPIO are open-drain, i.e. also need no switching,
  144. * so all we do is set the level */
  145. vx855gpio_set(gpio, nr, val);
  146. return 0;
  147. }
  148. static int vx855gpio_set_config(struct gpio_chip *gpio, unsigned int nr,
  149. unsigned long config)
  150. {
  151. enum pin_config_param param = pinconf_to_config_param(config);
  152. /* The GPI cannot be single-ended */
  153. if (nr < NR_VX855_GPI)
  154. return -EINVAL;
  155. /* The GPO's are push-pull */
  156. if (nr < NR_VX855_GPInO) {
  157. if (param != PIN_CONFIG_DRIVE_PUSH_PULL)
  158. return -ENOTSUPP;
  159. return 0;
  160. }
  161. /* The GPIO's are open drain */
  162. if (param != PIN_CONFIG_DRIVE_OPEN_DRAIN)
  163. return -ENOTSUPP;
  164. return 0;
  165. }
  166. static const char *vx855gpio_names[NR_VX855_GP] = {
  167. "VX855_GPI0", "VX855_GPI1", "VX855_GPI2", "VX855_GPI3", "VX855_GPI4",
  168. "VX855_GPI5", "VX855_GPI6", "VX855_GPI7", "VX855_GPI8", "VX855_GPI9",
  169. "VX855_GPI10", "VX855_GPI11", "VX855_GPI12", "VX855_GPI13",
  170. "VX855_GPO0", "VX855_GPO1", "VX855_GPO2", "VX855_GPO3", "VX855_GPO4",
  171. "VX855_GPO5", "VX855_GPO6", "VX855_GPO7", "VX855_GPO8", "VX855_GPO9",
  172. "VX855_GPO10", "VX855_GPO11", "VX855_GPO12",
  173. "VX855_GPIO0", "VX855_GPIO1", "VX855_GPIO2", "VX855_GPIO3",
  174. "VX855_GPIO4", "VX855_GPIO5", "VX855_GPIO6", "VX855_GPIO7",
  175. "VX855_GPIO8", "VX855_GPIO9", "VX855_GPIO10", "VX855_GPIO11",
  176. "VX855_GPIO12", "VX855_GPIO13", "VX855_GPIO14"
  177. };
  178. static void vx855gpio_gpio_setup(struct vx855_gpio *vg)
  179. {
  180. struct gpio_chip *c = &vg->gpio;
  181. c->label = "VX855 South Bridge";
  182. c->owner = THIS_MODULE;
  183. c->direction_input = vx855gpio_direction_input;
  184. c->direction_output = vx855gpio_direction_output;
  185. c->get = vx855gpio_get;
  186. c->set = vx855gpio_set;
  187. c->set_config = vx855gpio_set_config;
  188. c->dbg_show = NULL;
  189. c->base = 0;
  190. c->ngpio = NR_VX855_GP;
  191. c->can_sleep = false;
  192. c->names = vx855gpio_names;
  193. }
  194. /* This platform device is ordinarily registered by the vx855 mfd driver */
  195. static int vx855gpio_probe(struct platform_device *pdev)
  196. {
  197. struct resource *res_gpi;
  198. struct resource *res_gpo;
  199. struct vx855_gpio *vg;
  200. res_gpi = platform_get_resource(pdev, IORESOURCE_IO, 0);
  201. res_gpo = platform_get_resource(pdev, IORESOURCE_IO, 1);
  202. if (!res_gpi || !res_gpo)
  203. return -EBUSY;
  204. vg = devm_kzalloc(&pdev->dev, sizeof(*vg), GFP_KERNEL);
  205. if (!vg)
  206. return -ENOMEM;
  207. platform_set_drvdata(pdev, vg);
  208. dev_info(&pdev->dev, "found VX855 GPIO controller\n");
  209. vg->io_gpi = res_gpi->start;
  210. vg->io_gpo = res_gpo->start;
  211. spin_lock_init(&vg->lock);
  212. /*
  213. * A single byte is used to control various GPIO ports on the VX855,
  214. * and in the case of the OLPC XO-1.5, some of those ports are used
  215. * for switches that are interpreted and exposed through ACPI. ACPI
  216. * will have reserved the region, so our own reservation will not
  217. * succeed. Ignore and continue.
  218. */
  219. if (!devm_request_region(&pdev->dev, res_gpi->start,
  220. resource_size(res_gpi), MODULE_NAME "_gpi"))
  221. dev_warn(&pdev->dev,
  222. "GPI I/O resource busy, probably claimed by ACPI\n");
  223. if (!devm_request_region(&pdev->dev, res_gpo->start,
  224. resource_size(res_gpo), MODULE_NAME "_gpo"))
  225. dev_warn(&pdev->dev,
  226. "GPO I/O resource busy, probably claimed by ACPI\n");
  227. vx855gpio_gpio_setup(vg);
  228. return devm_gpiochip_add_data(&pdev->dev, &vg->gpio, vg);
  229. }
  230. static struct platform_driver vx855gpio_driver = {
  231. .driver = {
  232. .name = MODULE_NAME,
  233. },
  234. .probe = vx855gpio_probe,
  235. };
  236. module_platform_driver(vx855gpio_driver);
  237. MODULE_LICENSE("GPL");
  238. MODULE_AUTHOR("Harald Welte <[email protected]>");
  239. MODULE_DESCRIPTION("GPIO driver for the VIA VX855 chipset");
  240. MODULE_ALIAS("platform:vx855_gpio");