gpiolib-of.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 a gpio chip, but the translation failed.
  43. * Store translation error in out_gpio.
  44. * Return false to keep looking, as more than one gpio chip
  45. * could be registered per of-node.
  46. */
  47. gg_data->out_gpio = ERR_PTR(ret);
  48. return false;
  49. }
  50. gg_data->out_gpio = gpiochip_get_desc(gc, ret);
  51. return true;
  52. }
  53. /**
  54. * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
  55. * @np: device node to get GPIO from
  56. * @propname: property name containing gpio specifier(s)
  57. * @index: index of the GPIO
  58. * @flags: a flags pointer to fill in
  59. *
  60. * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  61. * value on the error condition. If @flags is not NULL the function also fills
  62. * in flags for the GPIO.
  63. */
  64. struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  65. const char *propname, int index, enum of_gpio_flags *flags)
  66. {
  67. /* Return -EPROBE_DEFER to support probe() functions to be called
  68. * later when the GPIO actually becomes available
  69. */
  70. struct gg_data gg_data = {
  71. .flags = flags,
  72. .out_gpio = ERR_PTR(-EPROBE_DEFER)
  73. };
  74. int ret;
  75. /* .of_xlate might decide to not fill in the flags, so clear it. */
  76. if (flags)
  77. *flags = 0;
  78. ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
  79. &gg_data.gpiospec);
  80. if (ret) {
  81. pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
  82. __func__, propname, np->full_name, index);
  83. return ERR_PTR(ret);
  84. }
  85. gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
  86. of_node_put(gg_data.gpiospec.np);
  87. pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n",
  88. __func__, propname, np->full_name, index,
  89. PTR_ERR_OR_ZERO(gg_data.out_gpio));
  90. return gg_data.out_gpio;
  91. }
  92. int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
  93. int index, enum of_gpio_flags *flags)
  94. {
  95. struct gpio_desc *desc;
  96. desc = of_get_named_gpiod_flags(np, list_name, index, flags);
  97. if (IS_ERR(desc))
  98. return PTR_ERR(desc);
  99. else
  100. return desc_to_gpio(desc);
  101. }
  102. EXPORT_SYMBOL(of_get_named_gpio_flags);
  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. /**
  185. * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
  186. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  187. */
  188. void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
  189. {
  190. struct gpio_chip *gc = &mm_gc->gc;
  191. if (!mm_gc)
  192. return;
  193. gpiochip_remove(gc);
  194. iounmap(mm_gc->regs);
  195. kfree(gc->label);
  196. }
  197. EXPORT_SYMBOL(of_mm_gpiochip_remove);
  198. #ifdef CONFIG_PINCTRL
  199. static void of_gpiochip_add_pin_range(struct gpio_chip *chip)
  200. {
  201. struct device_node *np = chip->of_node;
  202. struct of_phandle_args pinspec;
  203. struct pinctrl_dev *pctldev;
  204. int index = 0, ret;
  205. const char *name;
  206. static const char group_names_propname[] = "gpio-ranges-group-names";
  207. struct property *group_names;
  208. if (!np)
  209. return;
  210. group_names = of_find_property(np, group_names_propname, NULL);
  211. for (;; index++) {
  212. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
  213. index, &pinspec);
  214. if (ret)
  215. break;
  216. pctldev = of_pinctrl_get(pinspec.np);
  217. if (!pctldev)
  218. break;
  219. if (pinspec.args[2]) {
  220. if (group_names) {
  221. ret = of_property_read_string_index(np,
  222. group_names_propname,
  223. index, &name);
  224. if (strlen(name)) {
  225. pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
  226. np->full_name);
  227. break;
  228. }
  229. }
  230. /* npins != 0: linear range */
  231. ret = gpiochip_add_pin_range(chip,
  232. pinctrl_dev_get_devname(pctldev),
  233. pinspec.args[0],
  234. pinspec.args[1],
  235. pinspec.args[2]);
  236. if (ret)
  237. break;
  238. } else {
  239. /* npins == 0: special range */
  240. if (pinspec.args[1]) {
  241. pr_err("%s: Illegal gpio-range format.\n",
  242. np->full_name);
  243. break;
  244. }
  245. if (!group_names) {
  246. pr_err("%s: GPIO group range requested but no %s property.\n",
  247. np->full_name, group_names_propname);
  248. break;
  249. }
  250. ret = of_property_read_string_index(np,
  251. group_names_propname,
  252. index, &name);
  253. if (ret)
  254. break;
  255. if (!strlen(name)) {
  256. pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
  257. np->full_name);
  258. break;
  259. }
  260. ret = gpiochip_add_pingroup_range(chip, pctldev,
  261. pinspec.args[0], name);
  262. if (ret)
  263. break;
  264. }
  265. }
  266. }
  267. #else
  268. static void of_gpiochip_add_pin_range(struct gpio_chip *chip) {}
  269. #endif
  270. void of_gpiochip_add(struct gpio_chip *chip)
  271. {
  272. if ((!chip->of_node) && (chip->dev))
  273. chip->of_node = chip->dev->of_node;
  274. if (!chip->of_node)
  275. return;
  276. if (!chip->of_xlate) {
  277. chip->of_gpio_n_cells = 2;
  278. chip->of_xlate = of_gpio_simple_xlate;
  279. }
  280. of_gpiochip_add_pin_range(chip);
  281. of_node_get(chip->of_node);
  282. }
  283. void of_gpiochip_remove(struct gpio_chip *chip)
  284. {
  285. gpiochip_remove_pin_ranges(chip);
  286. of_node_put(chip->of_node);
  287. }