gpiolib-of.c 7.6 KB

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