gpio-uclass.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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_requestf() - [COMPAT] Request GPIO
  118. * @gpio: GPIO number
  119. * @fmt: Format string for the requested GPIO
  120. * @...: Arguments for the printf() format string
  121. *
  122. * This function implements the API that's compatible with current
  123. * GPIO API used in U-Boot. The request is forwarded to particular
  124. * GPIO driver. Returns 0 on success, negative value on error.
  125. */
  126. int gpio_requestf(unsigned gpio, const char *fmt, ...)
  127. {
  128. va_list args;
  129. char buf[40];
  130. va_start(args, fmt);
  131. vscnprintf(buf, sizeof(buf), fmt, args);
  132. va_end(args);
  133. return gpio_request(gpio, buf);
  134. }
  135. /**
  136. * gpio_free() - [COMPAT] Relinquish GPIO
  137. * gpio: GPIO number
  138. *
  139. * This function implements the API that's compatible with current
  140. * GPIO API used in U-Boot. The request is forwarded to particular
  141. * GPIO driver. Returns 0 on success, negative value on error.
  142. */
  143. int gpio_free(unsigned gpio)
  144. {
  145. struct gpio_dev_priv *uc_priv;
  146. unsigned int offset;
  147. struct udevice *dev;
  148. int ret;
  149. ret = gpio_to_device(gpio, &dev, &offset);
  150. if (ret)
  151. return ret;
  152. uc_priv = dev->uclass_priv;
  153. if (!uc_priv->name[offset])
  154. return -ENXIO;
  155. if (gpio_get_ops(dev)->free) {
  156. ret = gpio_get_ops(dev)->free(dev, offset);
  157. if (ret)
  158. return ret;
  159. }
  160. free(uc_priv->name[offset]);
  161. uc_priv->name[offset] = NULL;
  162. return 0;
  163. }
  164. static int check_reserved(struct udevice *dev, unsigned offset,
  165. const char *func)
  166. {
  167. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  168. if (!uc_priv->name[offset]) {
  169. printf("%s: %s: error: gpio %s%d not reserved\n",
  170. dev->name, func,
  171. uc_priv->bank_name ? uc_priv->bank_name : "", offset);
  172. return -EBUSY;
  173. }
  174. return 0;
  175. }
  176. /**
  177. * gpio_direction_input() - [COMPAT] Set GPIO direction to input
  178. * gpio: GPIO number
  179. *
  180. * This function implements the API that's compatible with current
  181. * GPIO API used in U-Boot. The request is forwarded to particular
  182. * GPIO driver. Returns 0 on success, negative value on error.
  183. */
  184. int gpio_direction_input(unsigned gpio)
  185. {
  186. unsigned int offset;
  187. struct udevice *dev;
  188. int ret;
  189. ret = gpio_to_device(gpio, &dev, &offset);
  190. if (ret)
  191. return ret;
  192. ret = check_reserved(dev, offset, "dir_input");
  193. return ret ? ret : gpio_get_ops(dev)->direction_input(dev, offset);
  194. }
  195. /**
  196. * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
  197. * gpio: GPIO number
  198. * value: Logical value to be set on the GPIO pin
  199. *
  200. * This function implements the API that's compatible with current
  201. * GPIO API used in U-Boot. The request is forwarded to particular
  202. * GPIO driver. Returns 0 on success, negative value on error.
  203. */
  204. int gpio_direction_output(unsigned gpio, int value)
  205. {
  206. unsigned int offset;
  207. struct udevice *dev;
  208. int ret;
  209. ret = gpio_to_device(gpio, &dev, &offset);
  210. if (ret)
  211. return ret;
  212. ret = check_reserved(dev, offset, "dir_output");
  213. return ret ? ret :
  214. gpio_get_ops(dev)->direction_output(dev, offset, value);
  215. }
  216. /**
  217. * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
  218. * gpio: GPIO number
  219. *
  220. * This function implements the API that's compatible with current
  221. * GPIO API used in U-Boot. The request is forwarded to particular
  222. * GPIO driver. Returns the value of the GPIO pin, or negative value
  223. * on error.
  224. */
  225. int gpio_get_value(unsigned gpio)
  226. {
  227. unsigned int offset;
  228. struct udevice *dev;
  229. int ret;
  230. ret = gpio_to_device(gpio, &dev, &offset);
  231. if (ret)
  232. return ret;
  233. ret = check_reserved(dev, offset, "get_value");
  234. return ret ? ret : gpio_get_ops(dev)->get_value(dev, offset);
  235. }
  236. /**
  237. * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
  238. * gpio: GPIO number
  239. * value: Logical value to be set on the GPIO pin.
  240. *
  241. * This function implements the API that's compatible with current
  242. * GPIO API used in U-Boot. The request is forwarded to particular
  243. * GPIO driver. Returns 0 on success, negative value on error.
  244. */
  245. int gpio_set_value(unsigned gpio, int value)
  246. {
  247. unsigned int offset;
  248. struct udevice *dev;
  249. int ret;
  250. ret = gpio_to_device(gpio, &dev, &offset);
  251. if (ret)
  252. return ret;
  253. ret = check_reserved(dev, offset, "set_value");
  254. return ret ? ret : gpio_get_ops(dev)->set_value(dev, offset, value);
  255. }
  256. const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
  257. {
  258. struct gpio_dev_priv *priv;
  259. /* Must be called on an active device */
  260. priv = dev->uclass_priv;
  261. assert(priv);
  262. *bit_count = priv->gpio_count;
  263. return priv->bank_name;
  264. }
  265. static const char * const gpio_function[GPIOF_COUNT] = {
  266. "input",
  267. "output",
  268. "unused",
  269. "unknown",
  270. "func",
  271. };
  272. int get_function(struct udevice *dev, int offset, bool skip_unused,
  273. const char **namep)
  274. {
  275. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  276. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  277. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  278. if (!device_active(dev))
  279. return -ENODEV;
  280. if (offset < 0 || offset >= uc_priv->gpio_count)
  281. return -EINVAL;
  282. if (namep)
  283. *namep = uc_priv->name[offset];
  284. if (skip_unused && !uc_priv->name[offset])
  285. return GPIOF_UNUSED;
  286. if (ops->get_function) {
  287. int ret;
  288. ret = ops->get_function(dev, offset);
  289. if (ret < 0)
  290. return ret;
  291. if (ret >= ARRAY_SIZE(gpio_function))
  292. return -ENODATA;
  293. return ret;
  294. }
  295. return GPIOF_UNKNOWN;
  296. }
  297. int gpio_get_function(struct udevice *dev, int offset, const char **namep)
  298. {
  299. return get_function(dev, offset, true, namep);
  300. }
  301. int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
  302. {
  303. return get_function(dev, offset, false, namep);
  304. }
  305. int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
  306. {
  307. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  308. struct gpio_dev_priv *priv;
  309. char *str = buf;
  310. int func;
  311. int ret;
  312. int len;
  313. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  314. *buf = 0;
  315. priv = dev->uclass_priv;
  316. ret = gpio_get_raw_function(dev, offset, NULL);
  317. if (ret < 0)
  318. return ret;
  319. func = ret;
  320. len = snprintf(str, buffsize, "%s%d: %s",
  321. priv->bank_name ? priv->bank_name : "",
  322. offset, gpio_function[func]);
  323. if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
  324. func == GPIOF_UNUSED) {
  325. const char *label;
  326. bool used;
  327. ret = ops->get_value(dev, offset);
  328. if (ret < 0)
  329. return ret;
  330. used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
  331. snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
  332. ret,
  333. used ? 'x' : ' ',
  334. used ? " " : "",
  335. label ? label : "");
  336. }
  337. return 0;
  338. }
  339. /*
  340. * get a number comprised of multiple GPIO values. gpio_num_array points to
  341. * the array of gpio pin numbers to scan, terminated by -1.
  342. */
  343. unsigned gpio_get_values_as_int(const int *gpio_num_array)
  344. {
  345. int gpio;
  346. unsigned bitmask = 1;
  347. unsigned vector = 0;
  348. while (bitmask &&
  349. ((gpio = *gpio_num_array++) != -1)) {
  350. if (gpio_get_value(gpio))
  351. vector |= bitmask;
  352. bitmask <<= 1;
  353. }
  354. return vector;
  355. }
  356. /* We need to renumber the GPIOs when any driver is probed/removed */
  357. static int gpio_renumber(struct udevice *removed_dev)
  358. {
  359. struct gpio_dev_priv *uc_priv;
  360. struct udevice *dev;
  361. struct uclass *uc;
  362. unsigned base;
  363. int ret;
  364. ret = uclass_get(UCLASS_GPIO, &uc);
  365. if (ret)
  366. return ret;
  367. /* Ensure that we have a base for each bank */
  368. base = 0;
  369. uclass_foreach_dev(dev, uc) {
  370. if (device_active(dev) && dev != removed_dev) {
  371. uc_priv = dev->uclass_priv;
  372. uc_priv->gpio_base = base;
  373. base += uc_priv->gpio_count;
  374. }
  375. }
  376. return 0;
  377. }
  378. static int gpio_post_probe(struct udevice *dev)
  379. {
  380. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  381. uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
  382. if (!uc_priv->name)
  383. return -ENOMEM;
  384. return gpio_renumber(NULL);
  385. }
  386. static int gpio_pre_remove(struct udevice *dev)
  387. {
  388. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  389. int i;
  390. for (i = 0; i < uc_priv->gpio_count; i++) {
  391. if (uc_priv->name[i])
  392. free(uc_priv->name[i]);
  393. }
  394. free(uc_priv->name);
  395. return gpio_renumber(dev);
  396. }
  397. UCLASS_DRIVER(gpio) = {
  398. .id = UCLASS_GPIO,
  399. .name = "gpio",
  400. .post_probe = gpio_post_probe,
  401. .pre_remove = gpio_pre_remove,
  402. .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
  403. };