gpio.c 5.9 KB

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