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