gpiolib-of.c 7.3 KB

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