gpio-uclass.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <asm/gpio.h>
  10. /**
  11. * gpio_to_device() - Convert global GPIO number to device, number
  12. * gpio: The numeric representation of the GPIO
  13. *
  14. * Convert the GPIO number to an entry in the list of GPIOs
  15. * or GPIO blocks registered with the GPIO controller. Returns
  16. * entry on success, NULL on error.
  17. */
  18. static int gpio_to_device(unsigned int gpio, struct udevice **devp,
  19. unsigned int *offset)
  20. {
  21. struct gpio_dev_priv *uc_priv;
  22. struct udevice *dev;
  23. int ret;
  24. for (ret = uclass_first_device(UCLASS_GPIO, &dev);
  25. dev;
  26. ret = uclass_next_device(&dev)) {
  27. uc_priv = dev->uclass_priv;
  28. if (gpio >= uc_priv->gpio_base &&
  29. gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
  30. *devp = dev;
  31. *offset = gpio - uc_priv->gpio_base;
  32. return 0;
  33. }
  34. }
  35. /* No such GPIO */
  36. return ret ? ret : -EINVAL;
  37. }
  38. int gpio_lookup_name(const char *name, struct udevice **devp,
  39. unsigned int *offsetp, unsigned int *gpiop)
  40. {
  41. struct gpio_dev_priv *uc_priv;
  42. struct udevice *dev;
  43. int ret;
  44. if (devp)
  45. *devp = NULL;
  46. for (ret = uclass_first_device(UCLASS_GPIO, &dev);
  47. dev;
  48. ret = uclass_next_device(&dev)) {
  49. ulong offset;
  50. int len;
  51. uc_priv = dev->uclass_priv;
  52. len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
  53. if (!strncasecmp(name, uc_priv->bank_name, len)) {
  54. if (strict_strtoul(name + len, 10, &offset))
  55. continue;
  56. if (devp)
  57. *devp = dev;
  58. if (offsetp)
  59. *offsetp = offset;
  60. if (gpiop)
  61. *gpiop = uc_priv->gpio_base + offset;
  62. return 0;
  63. }
  64. }
  65. return ret ? ret : -EINVAL;
  66. }
  67. /**
  68. * gpio_request() - [COMPAT] Request GPIO
  69. * gpio: GPIO number
  70. * label: Name for the requested GPIO
  71. *
  72. * This function implements the API that's compatible with current
  73. * GPIO API used in U-Boot. The request is forwarded to particular
  74. * GPIO driver. Returns 0 on success, negative value on error.
  75. */
  76. int gpio_request(unsigned gpio, const char *label)
  77. {
  78. unsigned int offset;
  79. struct udevice *dev;
  80. int ret;
  81. ret = gpio_to_device(gpio, &dev, &offset);
  82. if (ret)
  83. return ret;
  84. if (!gpio_get_ops(dev)->request)
  85. return 0;
  86. return gpio_get_ops(dev)->request(dev, offset, label);
  87. }
  88. /**
  89. * gpio_free() - [COMPAT] Relinquish GPIO
  90. * gpio: GPIO number
  91. *
  92. * This function implements the API that's compatible with current
  93. * GPIO API used in U-Boot. The request is forwarded to particular
  94. * GPIO driver. Returns 0 on success, negative value on error.
  95. */
  96. int gpio_free(unsigned gpio)
  97. {
  98. unsigned int offset;
  99. struct udevice *dev;
  100. int ret;
  101. ret = gpio_to_device(gpio, &dev, &offset);
  102. if (ret)
  103. return ret;
  104. if (!gpio_get_ops(dev)->free)
  105. return 0;
  106. return gpio_get_ops(dev)->free(dev, offset);
  107. }
  108. /**
  109. * gpio_direction_input() - [COMPAT] Set GPIO direction to input
  110. * gpio: GPIO number
  111. *
  112. * This function implements the API that's compatible with current
  113. * GPIO API used in U-Boot. The request is forwarded to particular
  114. * GPIO driver. Returns 0 on success, negative value on error.
  115. */
  116. int gpio_direction_input(unsigned gpio)
  117. {
  118. unsigned int offset;
  119. struct udevice *dev;
  120. int ret;
  121. ret = gpio_to_device(gpio, &dev, &offset);
  122. if (ret)
  123. return ret;
  124. return gpio_get_ops(dev)->direction_input(dev, offset);
  125. }
  126. /**
  127. * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
  128. * gpio: GPIO number
  129. * value: Logical value to be set on the GPIO pin
  130. *
  131. * This function implements the API that's compatible with current
  132. * GPIO API used in U-Boot. The request is forwarded to particular
  133. * GPIO driver. Returns 0 on success, negative value on error.
  134. */
  135. int gpio_direction_output(unsigned gpio, int value)
  136. {
  137. unsigned int offset;
  138. struct udevice *dev;
  139. int ret;
  140. ret = gpio_to_device(gpio, &dev, &offset);
  141. if (ret)
  142. return ret;
  143. return gpio_get_ops(dev)->direction_output(dev, offset, value);
  144. }
  145. /**
  146. * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
  147. * gpio: GPIO number
  148. *
  149. * This function implements the API that's compatible with current
  150. * GPIO API used in U-Boot. The request is forwarded to particular
  151. * GPIO driver. Returns the value of the GPIO pin, or negative value
  152. * on error.
  153. */
  154. int gpio_get_value(unsigned gpio)
  155. {
  156. unsigned int offset;
  157. struct udevice *dev;
  158. int ret;
  159. ret = gpio_to_device(gpio, &dev, &offset);
  160. if (ret)
  161. return ret;
  162. return gpio_get_ops(dev)->get_value(dev, offset);
  163. }
  164. /**
  165. * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
  166. * gpio: GPIO number
  167. * value: Logical value to be set on the GPIO pin.
  168. *
  169. * This function implements the API that's compatible with current
  170. * GPIO API used in U-Boot. The request is forwarded to particular
  171. * GPIO driver. Returns 0 on success, negative value on error.
  172. */
  173. int gpio_set_value(unsigned gpio, int value)
  174. {
  175. unsigned int offset;
  176. struct udevice *dev;
  177. int ret;
  178. ret = gpio_to_device(gpio, &dev, &offset);
  179. if (ret)
  180. return ret;
  181. return gpio_get_ops(dev)->set_value(dev, offset, value);
  182. }
  183. const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
  184. {
  185. struct gpio_dev_priv *priv;
  186. /* Must be called on an active device */
  187. priv = dev->uclass_priv;
  188. assert(priv);
  189. *bit_count = priv->gpio_count;
  190. return priv->bank_name;
  191. }
  192. /* We need to renumber the GPIOs when any driver is probed/removed */
  193. static int gpio_renumber(void)
  194. {
  195. struct gpio_dev_priv *uc_priv;
  196. struct udevice *dev;
  197. struct uclass *uc;
  198. unsigned base;
  199. int ret;
  200. ret = uclass_get(UCLASS_GPIO, &uc);
  201. if (ret)
  202. return ret;
  203. /* Ensure that we have a base for each bank */
  204. base = 0;
  205. uclass_foreach_dev(dev, uc) {
  206. if (device_active(dev)) {
  207. uc_priv = dev->uclass_priv;
  208. uc_priv->gpio_base = base;
  209. base += uc_priv->gpio_count;
  210. }
  211. }
  212. return 0;
  213. }
  214. static int gpio_post_probe(struct udevice *dev)
  215. {
  216. return gpio_renumber();
  217. }
  218. static int gpio_pre_remove(struct udevice *dev)
  219. {
  220. return gpio_renumber();
  221. }
  222. UCLASS_DRIVER(gpio) = {
  223. .id = UCLASS_GPIO,
  224. .name = "gpio",
  225. .post_probe = gpio_post_probe,
  226. .pre_remove = gpio_pre_remove,
  227. .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
  228. };