gpio-uclass.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 <malloc.h>
  10. #include <asm/gpio.h>
  11. #include <linux/ctype.h>
  12. /**
  13. * gpio_to_device() - Convert global GPIO number to device, number
  14. * gpio: The numeric representation of the GPIO
  15. *
  16. * Convert the GPIO number to an entry in the list of GPIOs
  17. * or GPIO blocks registered with the GPIO controller. Returns
  18. * entry on success, NULL on error.
  19. */
  20. static int gpio_to_device(unsigned int gpio, struct udevice **devp,
  21. unsigned int *offset)
  22. {
  23. struct gpio_dev_priv *uc_priv;
  24. struct udevice *dev;
  25. int ret;
  26. for (ret = uclass_first_device(UCLASS_GPIO, &dev);
  27. dev;
  28. ret = uclass_next_device(&dev)) {
  29. uc_priv = dev->uclass_priv;
  30. if (gpio >= uc_priv->gpio_base &&
  31. gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
  32. *devp = dev;
  33. *offset = gpio - uc_priv->gpio_base;
  34. return 0;
  35. }
  36. }
  37. /* No such GPIO */
  38. return ret ? ret : -EINVAL;
  39. }
  40. int gpio_lookup_name(const char *name, struct udevice **devp,
  41. unsigned int *offsetp, unsigned int *gpiop)
  42. {
  43. struct gpio_dev_priv *uc_priv = NULL;
  44. struct udevice *dev;
  45. ulong offset;
  46. int numeric;
  47. int ret;
  48. if (devp)
  49. *devp = NULL;
  50. numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
  51. for (ret = uclass_first_device(UCLASS_GPIO, &dev);
  52. dev;
  53. ret = uclass_next_device(&dev)) {
  54. int len;
  55. uc_priv = dev->uclass_priv;
  56. if (numeric != -1) {
  57. offset = numeric - uc_priv->gpio_base;
  58. /* Allow GPIOs to be numbered from 0 */
  59. if (offset >= 0 && offset < uc_priv->gpio_count)
  60. break;
  61. }
  62. len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
  63. if (!strncasecmp(name, uc_priv->bank_name, len)) {
  64. if (!strict_strtoul(name + len, 10, &offset))
  65. break;
  66. }
  67. }
  68. if (!dev)
  69. return ret ? ret : -EINVAL;
  70. if (devp)
  71. *devp = dev;
  72. if (offsetp)
  73. *offsetp = offset;
  74. if (gpiop)
  75. *gpiop = uc_priv->gpio_base + offset;
  76. return 0;
  77. }
  78. /**
  79. * gpio_request() - [COMPAT] Request GPIO
  80. * gpio: GPIO number
  81. * label: Name for the requested GPIO
  82. *
  83. * The label is copied and allocated so the caller does not need to keep
  84. * the pointer around.
  85. *
  86. * This function implements the API that's compatible with current
  87. * GPIO API used in U-Boot. The request is forwarded to particular
  88. * GPIO driver. Returns 0 on success, negative value on error.
  89. */
  90. int gpio_request(unsigned gpio, const char *label)
  91. {
  92. struct gpio_dev_priv *uc_priv;
  93. unsigned int offset;
  94. struct udevice *dev;
  95. char *str;
  96. int ret;
  97. ret = gpio_to_device(gpio, &dev, &offset);
  98. if (ret)
  99. return ret;
  100. uc_priv = dev->uclass_priv;
  101. if (uc_priv->name[offset])
  102. return -EBUSY;
  103. str = strdup(label);
  104. if (!str)
  105. return -ENOMEM;
  106. if (gpio_get_ops(dev)->request) {
  107. ret = gpio_get_ops(dev)->request(dev, offset, label);
  108. if (ret) {
  109. free(str);
  110. return ret;
  111. }
  112. }
  113. uc_priv->name[offset] = str;
  114. return 0;
  115. }
  116. /**
  117. * gpio_free() - [COMPAT] Relinquish GPIO
  118. * gpio: GPIO number
  119. *
  120. * This function implements the API that's compatible with current
  121. * GPIO API used in U-Boot. The request is forwarded to particular
  122. * GPIO driver. Returns 0 on success, negative value on error.
  123. */
  124. int gpio_free(unsigned gpio)
  125. {
  126. struct gpio_dev_priv *uc_priv;
  127. unsigned int offset;
  128. struct udevice *dev;
  129. int ret;
  130. ret = gpio_to_device(gpio, &dev, &offset);
  131. if (ret)
  132. return ret;
  133. uc_priv = dev->uclass_priv;
  134. if (!uc_priv->name[offset])
  135. return -ENXIO;
  136. if (gpio_get_ops(dev)->free) {
  137. ret = gpio_get_ops(dev)->free(dev, offset);
  138. if (ret)
  139. return ret;
  140. }
  141. free(uc_priv->name[offset]);
  142. uc_priv->name[offset] = NULL;
  143. return 0;
  144. }
  145. static int check_reserved(struct udevice *dev, unsigned offset,
  146. const char *func)
  147. {
  148. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  149. if (!uc_priv->name[offset]) {
  150. printf("%s: %s: error: gpio %s%d not reserved\n",
  151. dev->name, func,
  152. uc_priv->bank_name ? uc_priv->bank_name : "", offset);
  153. return -EBUSY;
  154. }
  155. return 0;
  156. }
  157. /**
  158. * gpio_direction_input() - [COMPAT] Set GPIO direction to input
  159. * gpio: GPIO number
  160. *
  161. * This function implements the API that's compatible with current
  162. * GPIO API used in U-Boot. The request is forwarded to particular
  163. * GPIO driver. Returns 0 on success, negative value on error.
  164. */
  165. int gpio_direction_input(unsigned gpio)
  166. {
  167. unsigned int offset;
  168. struct udevice *dev;
  169. int ret;
  170. ret = gpio_to_device(gpio, &dev, &offset);
  171. if (ret)
  172. return ret;
  173. ret = check_reserved(dev, offset, "dir_input");
  174. return ret ? ret : gpio_get_ops(dev)->direction_input(dev, offset);
  175. }
  176. /**
  177. * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
  178. * gpio: GPIO number
  179. * value: Logical value to be set on the GPIO pin
  180. *
  181. * This function implements the API that's compatible with current
  182. * GPIO API used in U-Boot. The request is forwarded to particular
  183. * GPIO driver. Returns 0 on success, negative value on error.
  184. */
  185. int gpio_direction_output(unsigned gpio, int value)
  186. {
  187. unsigned int offset;
  188. struct udevice *dev;
  189. int ret;
  190. ret = gpio_to_device(gpio, &dev, &offset);
  191. if (ret)
  192. return ret;
  193. ret = check_reserved(dev, offset, "dir_output");
  194. return ret ? ret :
  195. gpio_get_ops(dev)->direction_output(dev, offset, value);
  196. }
  197. /**
  198. * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
  199. * gpio: GPIO number
  200. *
  201. * This function implements the API that's compatible with current
  202. * GPIO API used in U-Boot. The request is forwarded to particular
  203. * GPIO driver. Returns the value of the GPIO pin, or negative value
  204. * on error.
  205. */
  206. int gpio_get_value(unsigned gpio)
  207. {
  208. unsigned int offset;
  209. struct udevice *dev;
  210. int ret;
  211. ret = gpio_to_device(gpio, &dev, &offset);
  212. if (ret)
  213. return ret;
  214. ret = check_reserved(dev, offset, "get_value");
  215. return ret ? ret : gpio_get_ops(dev)->get_value(dev, offset);
  216. }
  217. /**
  218. * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
  219. * gpio: GPIO number
  220. * value: Logical value to be set on the GPIO pin.
  221. *
  222. * This function implements the API that's compatible with current
  223. * GPIO API used in U-Boot. The request is forwarded to particular
  224. * GPIO driver. Returns 0 on success, negative value on error.
  225. */
  226. int gpio_set_value(unsigned gpio, int value)
  227. {
  228. unsigned int offset;
  229. struct udevice *dev;
  230. int ret;
  231. ret = gpio_to_device(gpio, &dev, &offset);
  232. if (ret)
  233. return ret;
  234. ret = check_reserved(dev, offset, "set_value");
  235. return ret ? ret : gpio_get_ops(dev)->set_value(dev, offset, value);
  236. }
  237. const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
  238. {
  239. struct gpio_dev_priv *priv;
  240. /* Must be called on an active device */
  241. priv = dev->uclass_priv;
  242. assert(priv);
  243. *bit_count = priv->gpio_count;
  244. return priv->bank_name;
  245. }
  246. static const char * const gpio_function[GPIOF_COUNT] = {
  247. "input",
  248. "output",
  249. "unused",
  250. "unknown",
  251. "func",
  252. };
  253. int get_function(struct udevice *dev, int offset, bool skip_unused,
  254. const char **namep)
  255. {
  256. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  257. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  258. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  259. if (!device_active(dev))
  260. return -ENODEV;
  261. if (offset < 0 || offset >= uc_priv->gpio_count)
  262. return -EINVAL;
  263. if (namep)
  264. *namep = uc_priv->name[offset];
  265. if (skip_unused && !uc_priv->name[offset])
  266. return GPIOF_UNUSED;
  267. if (ops->get_function) {
  268. int ret;
  269. ret = ops->get_function(dev, offset);
  270. if (ret < 0)
  271. return ret;
  272. if (ret >= ARRAY_SIZE(gpio_function))
  273. return -ENODATA;
  274. return ret;
  275. }
  276. return GPIOF_UNKNOWN;
  277. }
  278. int gpio_get_function(struct udevice *dev, int offset, const char **namep)
  279. {
  280. return get_function(dev, offset, true, namep);
  281. }
  282. int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
  283. {
  284. return get_function(dev, offset, false, namep);
  285. }
  286. int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
  287. {
  288. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  289. struct gpio_dev_priv *priv;
  290. char *str = buf;
  291. int func;
  292. int ret;
  293. int len;
  294. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  295. *buf = 0;
  296. priv = dev->uclass_priv;
  297. ret = gpio_get_raw_function(dev, offset, NULL);
  298. if (ret < 0)
  299. return ret;
  300. func = ret;
  301. len = snprintf(str, buffsize, "%s%d: %s",
  302. priv->bank_name ? priv->bank_name : "",
  303. offset, gpio_function[func]);
  304. if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
  305. func == GPIOF_UNUSED) {
  306. const char *label;
  307. bool used;
  308. ret = ops->get_value(dev, offset);
  309. if (ret < 0)
  310. return ret;
  311. used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
  312. snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
  313. ret,
  314. used ? 'x' : ' ',
  315. used ? " " : "",
  316. label ? label : "");
  317. }
  318. return 0;
  319. }
  320. /* We need to renumber the GPIOs when any driver is probed/removed */
  321. static int gpio_renumber(struct udevice *removed_dev)
  322. {
  323. struct gpio_dev_priv *uc_priv;
  324. struct udevice *dev;
  325. struct uclass *uc;
  326. unsigned base;
  327. int ret;
  328. ret = uclass_get(UCLASS_GPIO, &uc);
  329. if (ret)
  330. return ret;
  331. /* Ensure that we have a base for each bank */
  332. base = 0;
  333. uclass_foreach_dev(dev, uc) {
  334. if (device_active(dev) && dev != removed_dev) {
  335. uc_priv = dev->uclass_priv;
  336. uc_priv->gpio_base = base;
  337. base += uc_priv->gpio_count;
  338. }
  339. }
  340. return 0;
  341. }
  342. static int gpio_post_probe(struct udevice *dev)
  343. {
  344. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  345. uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
  346. if (!uc_priv->name)
  347. return -ENOMEM;
  348. return gpio_renumber(NULL);
  349. }
  350. static int gpio_pre_remove(struct udevice *dev)
  351. {
  352. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  353. int i;
  354. for (i = 0; i < uc_priv->gpio_count; i++) {
  355. if (uc_priv->name[i])
  356. free(uc_priv->name[i]);
  357. }
  358. free(uc_priv->name);
  359. return gpio_renumber(dev);
  360. }
  361. UCLASS_DRIVER(gpio) = {
  362. .id = UCLASS_GPIO,
  363. .name = "gpio",
  364. .post_probe = gpio_post_probe,
  365. .pre_remove = gpio_pre_remove,
  366. .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
  367. };