gpiolib-of.c 7.8 KB

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