gpio.c 5.9 KB

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