gpio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/io.h>
  16. #include <linux/of.h>
  17. #include <linux/slab.h>
  18. #include <linux/of_gpio.h>
  19. #include <asm/prom.h>
  20. /**
  21. * of_get_gpio_flags - Get a GPIO number and flags to use with GPIO API
  22. * @np: device node to get GPIO from
  23. * @index: index of the GPIO
  24. * @flags: a flags pointer to fill in
  25. *
  26. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  27. * value on the error condition. If @flags is not NULL the function also fills
  28. * in flags for the GPIO.
  29. */
  30. int of_get_gpio_flags(struct device_node *np, int index,
  31. enum of_gpio_flags *flags)
  32. {
  33. int ret;
  34. struct device_node *gpio_np;
  35. struct gpio_chip *gc;
  36. int size;
  37. const void *gpio_spec;
  38. const __be32 *gpio_cells;
  39. ret = of_parse_phandles_with_args(np, "gpios", "#gpio-cells", index,
  40. &gpio_np, &gpio_spec);
  41. if (ret) {
  42. pr_debug("%s: can't parse gpios property\n", __func__);
  43. goto err0;
  44. }
  45. gc = gpio_np->data;
  46. if (!gc) {
  47. pr_debug("%s: gpio controller %s isn't registered\n",
  48. np->full_name, gpio_np->full_name);
  49. ret = -ENODEV;
  50. goto err1;
  51. }
  52. gpio_cells = of_get_property(gpio_np, "#gpio-cells", &size);
  53. if (!gpio_cells || size != sizeof(*gpio_cells) ||
  54. be32_to_cpup(gpio_cells) != gc->of_gpio_n_cells) {
  55. pr_debug("%s: wrong #gpio-cells for %s\n",
  56. np->full_name, gpio_np->full_name);
  57. ret = -EINVAL;
  58. goto err1;
  59. }
  60. /* .xlate might decide to not fill in the flags, so clear it. */
  61. if (flags)
  62. *flags = 0;
  63. ret = gc->of_xlate(gc, np, gpio_spec, flags);
  64. if (ret < 0)
  65. goto err1;
  66. ret += gc->base;
  67. err1:
  68. of_node_put(gpio_np);
  69. err0:
  70. pr_debug("%s exited with status %d\n", __func__, ret);
  71. return ret;
  72. }
  73. EXPORT_SYMBOL(of_get_gpio_flags);
  74. /**
  75. * of_gpio_count - Count GPIOs for a device
  76. * @np: device node to count GPIOs for
  77. *
  78. * The function returns the count of GPIOs specified for a node.
  79. *
  80. * Note that the empty GPIO specifiers counts too. For example,
  81. *
  82. * gpios = <0
  83. * &pio1 1 2
  84. * 0
  85. * &pio2 3 4>;
  86. *
  87. * defines four GPIOs (so this function will return 4), two of which
  88. * are not specified.
  89. */
  90. unsigned int of_gpio_count(struct device_node *np)
  91. {
  92. unsigned int cnt = 0;
  93. do {
  94. int ret;
  95. ret = of_parse_phandles_with_args(np, "gpios", "#gpio-cells",
  96. cnt, NULL, NULL);
  97. /* A hole in the gpios = <> counts anyway. */
  98. if (ret < 0 && ret != -EEXIST)
  99. break;
  100. } while (++cnt);
  101. return cnt;
  102. }
  103. EXPORT_SYMBOL(of_gpio_count);
  104. /**
  105. * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
  106. * @gc: pointer to the gpio_chip structure
  107. * @np: device node of the GPIO chip
  108. * @gpio_spec: gpio specifier as found in the device tree
  109. * @flags: a flags pointer to fill in
  110. *
  111. * This is simple translation function, suitable for the most 1:1 mapped
  112. * gpio chips. This function performs only one sanity check: whether gpio
  113. * is less than ngpios (that is specified in the gpio_chip).
  114. */
  115. int of_gpio_simple_xlate(struct gpio_chip *gc, struct device_node *np,
  116. const void *gpio_spec, u32 *flags)
  117. {
  118. const __be32 *gpio = gpio_spec;
  119. const u32 n = be32_to_cpup(gpio);
  120. /*
  121. * We're discouraging gpio_cells < 2, since that way you'll have to
  122. * write your own xlate function (that will have to retrive the GPIO
  123. * number and the flags from a single gpio cell -- this is possible,
  124. * but not recommended).
  125. */
  126. if (gc->of_gpio_n_cells < 2) {
  127. WARN_ON(1);
  128. return -EINVAL;
  129. }
  130. if (n > gc->ngpio)
  131. return -EINVAL;
  132. if (flags)
  133. *flags = be32_to_cpu(gpio[1]);
  134. return n;
  135. }
  136. EXPORT_SYMBOL(of_gpio_simple_xlate);
  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 (!gc->of_xlate)
  169. gc->of_xlate = of_gpio_simple_xlate;
  170. if (mm_gc->save_regs)
  171. mm_gc->save_regs(mm_gc);
  172. np->data = &mm_gc->gc;
  173. mm_gc->gc.of_node = np;
  174. ret = gpiochip_add(gc);
  175. if (ret)
  176. goto err2;
  177. /* We don't want to lose the node and its ->data */
  178. of_node_get(np);
  179. pr_debug("%s: registered as generic GPIO chip, base is %d\n",
  180. np->full_name, gc->base);
  181. return 0;
  182. err2:
  183. np->data = NULL;
  184. iounmap(mm_gc->regs);
  185. err1:
  186. kfree(gc->label);
  187. err0:
  188. pr_err("%s: GPIO chip registration failed with status %d\n",
  189. np->full_name, ret);
  190. return ret;
  191. }
  192. EXPORT_SYMBOL(of_mm_gpiochip_add);