gpiolib-of.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/gpio/consumer.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/pinctrl/pinctrl.h>
  22. #include <linux/slab.h>
  23. struct gpio_desc;
  24. /* Private data structure for of_gpiochip_find_and_xlate */
  25. struct gg_data {
  26. enum of_gpio_flags *flags;
  27. struct of_phandle_args gpiospec;
  28. struct gpio_desc *out_gpio;
  29. };
  30. /* Private function for resolving node pointer to gpio_chip */
  31. static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data)
  32. {
  33. struct gg_data *gg_data = data;
  34. int ret;
  35. if ((gc->of_node != gg_data->gpiospec.np) ||
  36. (gc->of_gpio_n_cells != gg_data->gpiospec.args_count) ||
  37. (!gc->of_xlate))
  38. return false;
  39. ret = gc->of_xlate(gc, &gg_data->gpiospec, gg_data->flags);
  40. if (ret < 0)
  41. return false;
  42. gg_data->out_gpio = gpio_to_desc(ret + gc->base);
  43. return true;
  44. }
  45. /**
  46. * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
  47. * @np: device node to get GPIO from
  48. * @propname: property name containing gpio specifier(s)
  49. * @index: index of the GPIO
  50. * @flags: a flags pointer to fill in
  51. *
  52. * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  53. * value on the error condition. If @flags is not NULL the function also fills
  54. * in flags for the GPIO.
  55. */
  56. struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  57. const char *propname, int index, enum of_gpio_flags *flags)
  58. {
  59. /* Return -EPROBE_DEFER to support probe() functions to be called
  60. * later when the GPIO actually becomes available
  61. */
  62. struct gg_data gg_data = {
  63. .flags = flags,
  64. .out_gpio = ERR_PTR(-EPROBE_DEFER)
  65. };
  66. int ret;
  67. /* .of_xlate might decide to not fill in the flags, so clear it. */
  68. if (flags)
  69. *flags = 0;
  70. ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
  71. &gg_data.gpiospec);
  72. if (ret) {
  73. pr_debug("%s: can't parse gpios property of node '%s[%d]'\n",
  74. __func__, np->full_name, index);
  75. return ERR_PTR(ret);
  76. }
  77. gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
  78. of_node_put(gg_data.gpiospec.np);
  79. pr_debug("%s exited with status %d\n", __func__,
  80. PTR_RET(gg_data.out_gpio));
  81. return gg_data.out_gpio;
  82. }
  83. EXPORT_SYMBOL(of_get_named_gpiod_flags);
  84. /**
  85. * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
  86. * @gc: pointer to the gpio_chip structure
  87. * @np: device node of the GPIO chip
  88. * @gpio_spec: gpio specifier as found in the device tree
  89. * @flags: a flags pointer to fill in
  90. *
  91. * This is simple translation function, suitable for the most 1:1 mapped
  92. * gpio chips. This function performs only one sanity check: whether gpio
  93. * is less than ngpios (that is specified in the gpio_chip).
  94. */
  95. int of_gpio_simple_xlate(struct gpio_chip *gc,
  96. const struct of_phandle_args *gpiospec, u32 *flags)
  97. {
  98. /*
  99. * We're discouraging gpio_cells < 2, since that way you'll have to
  100. * write your own xlate function (that will have to retrive the GPIO
  101. * number and the flags from a single gpio cell -- this is possible,
  102. * but not recommended).
  103. */
  104. if (gc->of_gpio_n_cells < 2) {
  105. WARN_ON(1);
  106. return -EINVAL;
  107. }
  108. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  109. return -EINVAL;
  110. if (gpiospec->args[0] >= gc->ngpio)
  111. return -EINVAL;
  112. if (flags)
  113. *flags = gpiospec->args[1];
  114. return gpiospec->args[0];
  115. }
  116. EXPORT_SYMBOL(of_gpio_simple_xlate);
  117. /**
  118. * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
  119. * @np: device node of the GPIO chip
  120. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  121. *
  122. * To use this function you should allocate and fill mm_gc with:
  123. *
  124. * 1) In the gpio_chip structure:
  125. * - all the callbacks
  126. * - of_gpio_n_cells
  127. * - of_xlate callback (optional)
  128. *
  129. * 3) In the of_mm_gpio_chip structure:
  130. * - save_regs callback (optional)
  131. *
  132. * If succeeded, this function will map bank's memory and will
  133. * do all necessary work for you. Then you'll able to use .regs
  134. * to manage GPIOs from the callbacks.
  135. */
  136. int of_mm_gpiochip_add(struct device_node *np,
  137. struct of_mm_gpio_chip *mm_gc)
  138. {
  139. int ret = -ENOMEM;
  140. struct gpio_chip *gc = &mm_gc->gc;
  141. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  142. if (!gc->label)
  143. goto err0;
  144. mm_gc->regs = of_iomap(np, 0);
  145. if (!mm_gc->regs)
  146. goto err1;
  147. gc->base = -1;
  148. if (mm_gc->save_regs)
  149. mm_gc->save_regs(mm_gc);
  150. mm_gc->gc.of_node = np;
  151. ret = gpiochip_add(gc);
  152. if (ret)
  153. goto err2;
  154. return 0;
  155. err2:
  156. iounmap(mm_gc->regs);
  157. err1:
  158. kfree(gc->label);
  159. err0:
  160. pr_err("%s: GPIO chip registration failed with status %d\n",
  161. np->full_name, ret);
  162. return ret;
  163. }
  164. EXPORT_SYMBOL(of_mm_gpiochip_add);
  165. #ifdef CONFIG_PINCTRL
  166. static void of_gpiochip_add_pin_range(struct gpio_chip *chip)
  167. {
  168. struct device_node *np = chip->of_node;
  169. struct of_phandle_args pinspec;
  170. struct pinctrl_dev *pctldev;
  171. int index = 0, ret;
  172. const char *name;
  173. static const char group_names_propname[] = "gpio-ranges-group-names";
  174. struct property *group_names;
  175. if (!np)
  176. return;
  177. group_names = of_find_property(np, group_names_propname, NULL);
  178. for (;; index++) {
  179. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
  180. index, &pinspec);
  181. if (ret)
  182. break;
  183. pctldev = of_pinctrl_get(pinspec.np);
  184. if (!pctldev)
  185. break;
  186. if (pinspec.args[2]) {
  187. if (group_names) {
  188. ret = of_property_read_string_index(np,
  189. group_names_propname,
  190. index, &name);
  191. if (strlen(name)) {
  192. pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
  193. np->full_name);
  194. break;
  195. }
  196. }
  197. /* npins != 0: linear range */
  198. ret = gpiochip_add_pin_range(chip,
  199. pinctrl_dev_get_devname(pctldev),
  200. pinspec.args[0],
  201. pinspec.args[1],
  202. pinspec.args[2]);
  203. if (ret)
  204. break;
  205. } else {
  206. /* npins == 0: special range */
  207. if (pinspec.args[1]) {
  208. pr_err("%s: Illegal gpio-range format.\n",
  209. np->full_name);
  210. break;
  211. }
  212. if (!group_names) {
  213. pr_err("%s: GPIO group range requested but no %s property.\n",
  214. np->full_name, group_names_propname);
  215. break;
  216. }
  217. ret = of_property_read_string_index(np,
  218. group_names_propname,
  219. index, &name);
  220. if (ret)
  221. break;
  222. if (!strlen(name)) {
  223. pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
  224. np->full_name);
  225. break;
  226. }
  227. ret = gpiochip_add_pingroup_range(chip, pctldev,
  228. pinspec.args[0], name);
  229. if (ret)
  230. break;
  231. }
  232. }
  233. }
  234. #else
  235. static void of_gpiochip_add_pin_range(struct gpio_chip *chip) {}
  236. #endif
  237. void of_gpiochip_add(struct gpio_chip *chip)
  238. {
  239. if ((!chip->of_node) && (chip->dev))
  240. chip->of_node = chip->dev->of_node;
  241. if (!chip->of_node)
  242. return;
  243. if (!chip->of_xlate) {
  244. chip->of_gpio_n_cells = 2;
  245. chip->of_xlate = of_gpio_simple_xlate;
  246. }
  247. of_gpiochip_add_pin_range(chip);
  248. of_node_get(chip->of_node);
  249. }
  250. void of_gpiochip_remove(struct gpio_chip *chip)
  251. {
  252. gpiochip_remove_pin_ranges(chip);
  253. if (chip->of_node)
  254. of_node_put(chip->of_node);
  255. }