gpiolib-of.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_simple_xlate - translate gpio_spec to the GPIO number and flags
  80. * @gc: pointer to the gpio_chip structure
  81. * @np: device node of the GPIO chip
  82. * @gpio_spec: gpio specifier as found in the device tree
  83. * @flags: a flags pointer to fill in
  84. *
  85. * This is simple translation function, suitable for the most 1:1 mapped
  86. * gpio chips. This function performs only one sanity check: whether gpio
  87. * is less than ngpios (that is specified in the gpio_chip).
  88. */
  89. int of_gpio_simple_xlate(struct gpio_chip *gc,
  90. const struct of_phandle_args *gpiospec, u32 *flags)
  91. {
  92. /*
  93. * We're discouraging gpio_cells < 2, since that way you'll have to
  94. * write your own xlate function (that will have to retrive the GPIO
  95. * number and the flags from a single gpio cell -- this is possible,
  96. * but not recommended).
  97. */
  98. if (gc->of_gpio_n_cells < 2) {
  99. WARN_ON(1);
  100. return -EINVAL;
  101. }
  102. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  103. return -EINVAL;
  104. if (gpiospec->args[0] >= gc->ngpio)
  105. return -EINVAL;
  106. if (flags)
  107. *flags = gpiospec->args[1];
  108. return gpiospec->args[0];
  109. }
  110. EXPORT_SYMBOL(of_gpio_simple_xlate);
  111. /**
  112. * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
  113. * @np: device node of the GPIO chip
  114. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  115. *
  116. * To use this function you should allocate and fill mm_gc with:
  117. *
  118. * 1) In the gpio_chip structure:
  119. * - all the callbacks
  120. * - of_gpio_n_cells
  121. * - of_xlate callback (optional)
  122. *
  123. * 3) In the of_mm_gpio_chip structure:
  124. * - save_regs callback (optional)
  125. *
  126. * If succeeded, this function will map bank's memory and will
  127. * do all necessary work for you. Then you'll able to use .regs
  128. * to manage GPIOs from the callbacks.
  129. */
  130. int of_mm_gpiochip_add(struct device_node *np,
  131. struct of_mm_gpio_chip *mm_gc)
  132. {
  133. int ret = -ENOMEM;
  134. struct gpio_chip *gc = &mm_gc->gc;
  135. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  136. if (!gc->label)
  137. goto err0;
  138. mm_gc->regs = of_iomap(np, 0);
  139. if (!mm_gc->regs)
  140. goto err1;
  141. gc->base = -1;
  142. if (mm_gc->save_regs)
  143. mm_gc->save_regs(mm_gc);
  144. mm_gc->gc.of_node = np;
  145. ret = gpiochip_add(gc);
  146. if (ret)
  147. goto err2;
  148. return 0;
  149. err2:
  150. iounmap(mm_gc->regs);
  151. err1:
  152. kfree(gc->label);
  153. err0:
  154. pr_err("%s: GPIO chip registration failed with status %d\n",
  155. np->full_name, ret);
  156. return ret;
  157. }
  158. EXPORT_SYMBOL(of_mm_gpiochip_add);
  159. #ifdef CONFIG_PINCTRL
  160. static void of_gpiochip_add_pin_range(struct gpio_chip *chip)
  161. {
  162. struct device_node *np = chip->of_node;
  163. struct of_phandle_args pinspec;
  164. struct pinctrl_dev *pctldev;
  165. int index = 0, ret;
  166. if (!np)
  167. return;
  168. for (;; index++) {
  169. ret = of_parse_phandle_with_args(np, "gpio-ranges",
  170. "#gpio-range-cells", index, &pinspec);
  171. if (ret)
  172. break;
  173. pctldev = of_pinctrl_get(pinspec.np);
  174. if (!pctldev)
  175. break;
  176. /*
  177. * This assumes that the n GPIO pins are consecutive in the
  178. * GPIO number space, and that the pins are also consecutive
  179. * in their local number space. Currently it is not possible
  180. * to add different ranges for one and the same GPIO chip,
  181. * as the code assumes that we have one consecutive range
  182. * on both, mapping 1-to-1.
  183. *
  184. * TODO: make the OF bindings handle multiple sparse ranges
  185. * on the same GPIO chip.
  186. */
  187. ret = gpiochip_add_pin_range(chip,
  188. pinctrl_dev_get_devname(pctldev),
  189. 0, /* offset in gpiochip */
  190. pinspec.args[0],
  191. pinspec.args[1]);
  192. if (ret)
  193. break;
  194. }
  195. }
  196. #else
  197. static void of_gpiochip_add_pin_range(struct gpio_chip *chip) {}
  198. #endif
  199. void of_gpiochip_add(struct gpio_chip *chip)
  200. {
  201. if ((!chip->of_node) && (chip->dev))
  202. chip->of_node = chip->dev->of_node;
  203. if (!chip->of_node)
  204. return;
  205. if (!chip->of_xlate) {
  206. chip->of_gpio_n_cells = 2;
  207. chip->of_xlate = of_gpio_simple_xlate;
  208. }
  209. of_gpiochip_add_pin_range(chip);
  210. of_node_get(chip->of_node);
  211. }
  212. void of_gpiochip_remove(struct gpio_chip *chip)
  213. {
  214. gpiochip_remove_pin_ranges(chip);
  215. if (chip->of_node)
  216. of_node_put(chip->of_node);
  217. }