gpio.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * OF helpers for the GPIO API
  3. *
  4. * Copyright (c) 2007-2008 MontaVista Software, Inc.
  5. *
  6. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/module.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/slab.h>
  22. /**
  23. * of_get_named_gpio_flags() - Get a GPIO number and flags to use with GPIO API
  24. * @np: device node to get GPIO from
  25. * @propname: property name containing gpio specifier(s)
  26. * @index: index of the GPIO
  27. * @flags: a flags pointer to fill in
  28. *
  29. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  30. * value on the error condition. If @flags is not NULL the function also fills
  31. * in flags for the GPIO.
  32. */
  33. int of_get_named_gpio_flags(struct device_node *np, const char *propname,
  34. int index, enum of_gpio_flags *flags)
  35. {
  36. int ret;
  37. struct gpio_chip *gc;
  38. struct of_phandle_args gpiospec;
  39. ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
  40. &gpiospec);
  41. if (ret) {
  42. pr_debug("%s: can't parse gpios property\n", __func__);
  43. goto err0;
  44. }
  45. gc = of_node_to_gpiochip(gpiospec.np);
  46. if (!gc) {
  47. pr_debug("%s: gpio controller %s isn't registered\n",
  48. np->full_name, gpiospec.np->full_name);
  49. ret = -EPROBE_DEFER;
  50. goto err1;
  51. }
  52. if (gpiospec.args_count != gc->of_gpio_n_cells) {
  53. pr_debug("%s: wrong #gpio-cells for %s\n",
  54. np->full_name, gpiospec.np->full_name);
  55. ret = -EINVAL;
  56. goto err1;
  57. }
  58. /* .xlate might decide to not fill in the flags, so clear it. */
  59. if (flags)
  60. *flags = 0;
  61. ret = gc->of_xlate(gc, &gpiospec, flags);
  62. if (ret < 0)
  63. goto err1;
  64. ret += gc->base;
  65. err1:
  66. of_node_put(gpiospec.np);
  67. err0:
  68. pr_debug("%s exited with status %d\n", __func__, ret);
  69. return ret;
  70. }
  71. EXPORT_SYMBOL(of_get_named_gpio_flags);
  72. /**
  73. * of_gpio_named_count - Count GPIOs for a device
  74. * @np: device node to count GPIOs for
  75. * @propname: property name containing gpio specifier(s)
  76. *
  77. * The function returns the count of GPIOs specified for a node.
  78. *
  79. * Note that the empty GPIO specifiers counts too. For example,
  80. *
  81. * gpios = <0
  82. * &pio1 1 2
  83. * 0
  84. * &pio2 3 4>;
  85. *
  86. * defines four GPIOs (so this function will return 4), two of which
  87. * are not specified.
  88. */
  89. unsigned int of_gpio_named_count(struct device_node *np, const char* propname)
  90. {
  91. unsigned int cnt = 0;
  92. do {
  93. int ret;
  94. ret = of_parse_phandle_with_args(np, propname, "#gpio-cells",
  95. cnt, NULL);
  96. /* A hole in the gpios = <> counts anyway. */
  97. if (ret < 0 && ret != -EEXIST)
  98. break;
  99. } while (++cnt);
  100. return cnt;
  101. }
  102. EXPORT_SYMBOL(of_gpio_named_count);
  103. /**
  104. * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
  105. * @gc: pointer to the gpio_chip structure
  106. * @np: device node of the GPIO chip
  107. * @gpio_spec: gpio specifier as found in the device tree
  108. * @flags: a flags pointer to fill in
  109. *
  110. * This is simple translation function, suitable for the most 1:1 mapped
  111. * gpio chips. This function performs only one sanity check: whether gpio
  112. * is less than ngpios (that is specified in the gpio_chip).
  113. */
  114. int of_gpio_simple_xlate(struct gpio_chip *gc,
  115. const struct of_phandle_args *gpiospec, u32 *flags)
  116. {
  117. /*
  118. * We're discouraging gpio_cells < 2, since that way you'll have to
  119. * write your own xlate function (that will have to retrive the GPIO
  120. * number and the flags from a single gpio cell -- this is possible,
  121. * but not recommended).
  122. */
  123. if (gc->of_gpio_n_cells < 2) {
  124. WARN_ON(1);
  125. return -EINVAL;
  126. }
  127. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  128. return -EINVAL;
  129. if (gpiospec->args[0] >= gc->ngpio)
  130. return -EINVAL;
  131. if (flags)
  132. *flags = gpiospec->args[1];
  133. return gpiospec->args[0];
  134. }
  135. EXPORT_SYMBOL(of_gpio_simple_xlate);
  136. /**
  137. * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
  138. * @np: device node of the GPIO chip
  139. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  140. *
  141. * To use this function you should allocate and fill mm_gc with:
  142. *
  143. * 1) In the gpio_chip structure:
  144. * - all the callbacks
  145. * - of_gpio_n_cells
  146. * - of_xlate callback (optional)
  147. *
  148. * 3) In the of_mm_gpio_chip structure:
  149. * - save_regs callback (optional)
  150. *
  151. * If succeeded, this function will map bank's memory and will
  152. * do all necessary work for you. Then you'll able to use .regs
  153. * to manage GPIOs from the callbacks.
  154. */
  155. int of_mm_gpiochip_add(struct device_node *np,
  156. struct of_mm_gpio_chip *mm_gc)
  157. {
  158. int ret = -ENOMEM;
  159. struct gpio_chip *gc = &mm_gc->gc;
  160. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  161. if (!gc->label)
  162. goto err0;
  163. mm_gc->regs = of_iomap(np, 0);
  164. if (!mm_gc->regs)
  165. goto err1;
  166. gc->base = -1;
  167. if (mm_gc->save_regs)
  168. mm_gc->save_regs(mm_gc);
  169. mm_gc->gc.of_node = np;
  170. ret = gpiochip_add(gc);
  171. if (ret)
  172. goto err2;
  173. return 0;
  174. err2:
  175. iounmap(mm_gc->regs);
  176. err1:
  177. kfree(gc->label);
  178. err0:
  179. pr_err("%s: GPIO chip registration failed with status %d\n",
  180. np->full_name, ret);
  181. return ret;
  182. }
  183. EXPORT_SYMBOL(of_mm_gpiochip_add);
  184. #ifdef CONFIG_PINCTRL
  185. void of_gpiochip_add_pin_range(struct gpio_chip *chip)
  186. {
  187. struct device_node *np = chip->of_node;
  188. struct of_phandle_args pinspec;
  189. struct pinctrl_dev *pctldev;
  190. int index = 0, ret;
  191. if (!np)
  192. return;
  193. do {
  194. ret = of_parse_phandle_with_args(np, "gpio-ranges",
  195. "#gpio-range-cells", index, &pinspec);
  196. if (ret)
  197. break;
  198. pctldev = of_pinctrl_get(pinspec.np);
  199. if (!pctldev)
  200. break;
  201. ret = gpiochip_add_pin_range(chip,
  202. pinctrl_dev_get_name(pctldev),
  203. pinspec.args[0],
  204. pinspec.args[1]);
  205. if (ret)
  206. break;
  207. } while (index++);
  208. }
  209. void of_gpiochip_remove_pin_range(struct gpio_chip *chip)
  210. {
  211. struct gpio_pin_range *pin_range, *tmp;
  212. list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
  213. list_del(&pin_range->node);
  214. pinctrl_remove_gpio_range(pin_range->pctldev,
  215. &pin_range->range);
  216. }
  217. }
  218. #else
  219. void of_gpiochip_add_pin_range(struct gpio_chip *chip) {}
  220. void of_gpiochip_remove_pin_range(struct gpio_chip *chip) {}
  221. #endif
  222. void of_gpiochip_add(struct gpio_chip *chip)
  223. {
  224. if ((!chip->of_node) && (chip->dev))
  225. chip->of_node = chip->dev->of_node;
  226. if (!chip->of_node)
  227. return;
  228. if (!chip->of_xlate) {
  229. chip->of_gpio_n_cells = 2;
  230. chip->of_xlate = of_gpio_simple_xlate;
  231. }
  232. of_gpiochip_add_pin_range(chip);
  233. of_node_get(chip->of_node);
  234. }
  235. void of_gpiochip_remove(struct gpio_chip *chip)
  236. {
  237. of_gpiochip_remove_pin_range(chip);
  238. if (chip->of_node)
  239. of_node_put(chip->of_node);
  240. }
  241. /* Private function for resolving node pointer to gpio_chip */
  242. static int of_gpiochip_is_match(struct gpio_chip *chip, const void *data)
  243. {
  244. return chip->of_node == data;
  245. }
  246. struct gpio_chip *of_node_to_gpiochip(struct device_node *np)
  247. {
  248. return gpiochip_find(np, of_gpiochip_is_match);
  249. }