gpio.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/slab.h>
  21. /**
  22. * of_get_named_gpio_flags() - Get a GPIO number and flags to use with GPIO API
  23. * @np: device node to get GPIO from
  24. * @propname: property name containing gpio specifier(s)
  25. * @index: index of the GPIO
  26. * @flags: a flags pointer to fill in
  27. *
  28. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  29. * value on the error condition. If @flags is not NULL the function also fills
  30. * in flags for the GPIO.
  31. */
  32. int of_get_named_gpio_flags(struct device_node *np, const char *propname,
  33. int index, enum of_gpio_flags *flags)
  34. {
  35. int ret;
  36. struct gpio_chip *gc;
  37. struct of_phandle_args gpiospec;
  38. ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
  39. &gpiospec);
  40. if (ret) {
  41. pr_debug("%s: can't parse gpios property\n", __func__);
  42. goto err0;
  43. }
  44. gc = of_node_to_gpiochip(gpiospec.np);
  45. if (!gc) {
  46. pr_debug("%s: gpio controller %s isn't registered\n",
  47. np->full_name, gpiospec.np->full_name);
  48. ret = -ENODEV;
  49. goto err1;
  50. }
  51. if (gpiospec.args_count != gc->of_gpio_n_cells) {
  52. pr_debug("%s: wrong #gpio-cells for %s\n",
  53. np->full_name, gpiospec.np->full_name);
  54. ret = -EINVAL;
  55. goto err1;
  56. }
  57. /* .xlate might decide to not fill in the flags, so clear it. */
  58. if (flags)
  59. *flags = 0;
  60. ret = gc->of_xlate(gc, &gpiospec, flags);
  61. if (ret < 0)
  62. goto err1;
  63. ret += gc->base;
  64. err1:
  65. of_node_put(gpiospec.np);
  66. err0:
  67. pr_debug("%s exited with status %d\n", __func__, ret);
  68. return ret;
  69. }
  70. EXPORT_SYMBOL(of_get_named_gpio_flags);
  71. /**
  72. * of_gpio_count - Count GPIOs for a device
  73. * @np: device node to count GPIOs for
  74. *
  75. * The function returns the count of GPIOs specified for a node.
  76. *
  77. * Note that the empty GPIO specifiers counts too. For example,
  78. *
  79. * gpios = <0
  80. * &pio1 1 2
  81. * 0
  82. * &pio2 3 4>;
  83. *
  84. * defines four GPIOs (so this function will return 4), two of which
  85. * are not specified.
  86. */
  87. unsigned int of_gpio_count(struct device_node *np)
  88. {
  89. unsigned int cnt = 0;
  90. do {
  91. int ret;
  92. ret = of_parse_phandle_with_args(np, "gpios", "#gpio-cells",
  93. cnt, NULL);
  94. /* A hole in the gpios = <> counts anyway. */
  95. if (ret < 0 && ret != -EEXIST)
  96. break;
  97. } while (++cnt);
  98. return cnt;
  99. }
  100. EXPORT_SYMBOL(of_gpio_count);
  101. /**
  102. * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
  103. * @gc: pointer to the gpio_chip structure
  104. * @np: device node of the GPIO chip
  105. * @gpio_spec: gpio specifier as found in the device tree
  106. * @flags: a flags pointer to fill in
  107. *
  108. * This is simple translation function, suitable for the most 1:1 mapped
  109. * gpio chips. This function performs only one sanity check: whether gpio
  110. * is less than ngpios (that is specified in the gpio_chip).
  111. */
  112. int of_gpio_simple_xlate(struct gpio_chip *gc,
  113. const struct of_phandle_args *gpiospec, u32 *flags)
  114. {
  115. /*
  116. * We're discouraging gpio_cells < 2, since that way you'll have to
  117. * write your own xlate function (that will have to retrive the GPIO
  118. * number and the flags from a single gpio cell -- this is possible,
  119. * but not recommended).
  120. */
  121. if (gc->of_gpio_n_cells < 2) {
  122. WARN_ON(1);
  123. return -EINVAL;
  124. }
  125. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  126. return -EINVAL;
  127. if (gpiospec->args[0] > gc->ngpio)
  128. return -EINVAL;
  129. if (flags)
  130. *flags = gpiospec->args[1];
  131. return gpiospec->args[0];
  132. }
  133. EXPORT_SYMBOL(of_gpio_simple_xlate);
  134. /**
  135. * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
  136. * @np: device node of the GPIO chip
  137. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  138. *
  139. * To use this function you should allocate and fill mm_gc with:
  140. *
  141. * 1) In the gpio_chip structure:
  142. * - all the callbacks
  143. * - of_gpio_n_cells
  144. * - of_xlate callback (optional)
  145. *
  146. * 3) In the of_mm_gpio_chip structure:
  147. * - save_regs callback (optional)
  148. *
  149. * If succeeded, this function will map bank's memory and will
  150. * do all necessary work for you. Then you'll able to use .regs
  151. * to manage GPIOs from the callbacks.
  152. */
  153. int of_mm_gpiochip_add(struct device_node *np,
  154. struct of_mm_gpio_chip *mm_gc)
  155. {
  156. int ret = -ENOMEM;
  157. struct gpio_chip *gc = &mm_gc->gc;
  158. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  159. if (!gc->label)
  160. goto err0;
  161. mm_gc->regs = of_iomap(np, 0);
  162. if (!mm_gc->regs)
  163. goto err1;
  164. gc->base = -1;
  165. if (mm_gc->save_regs)
  166. mm_gc->save_regs(mm_gc);
  167. mm_gc->gc.of_node = np;
  168. ret = gpiochip_add(gc);
  169. if (ret)
  170. goto err2;
  171. return 0;
  172. err2:
  173. iounmap(mm_gc->regs);
  174. err1:
  175. kfree(gc->label);
  176. err0:
  177. pr_err("%s: GPIO chip registration failed with status %d\n",
  178. np->full_name, ret);
  179. return ret;
  180. }
  181. EXPORT_SYMBOL(of_mm_gpiochip_add);
  182. void of_gpiochip_add(struct gpio_chip *chip)
  183. {
  184. if ((!chip->of_node) && (chip->dev))
  185. chip->of_node = chip->dev->of_node;
  186. if (!chip->of_node)
  187. return;
  188. if (!chip->of_xlate) {
  189. chip->of_gpio_n_cells = 2;
  190. chip->of_xlate = of_gpio_simple_xlate;
  191. }
  192. of_node_get(chip->of_node);
  193. }
  194. void of_gpiochip_remove(struct gpio_chip *chip)
  195. {
  196. if (chip->of_node)
  197. of_node_put(chip->of_node);
  198. }
  199. /* Private function for resolving node pointer to gpio_chip */
  200. static int of_gpiochip_is_match(struct gpio_chip *chip, void *data)
  201. {
  202. return chip->of_node == data;
  203. }
  204. struct gpio_chip *of_node_to_gpiochip(struct device_node *np)
  205. {
  206. return gpiochip_find(np, of_gpiochip_is_match);
  207. }