gpio.c 5.4 KB

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