gpio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 *gc;
  35. struct of_gpio_chip *of_gc = NULL;
  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. &gc, &gpio_spec);
  41. if (ret) {
  42. pr_debug("%s: can't parse gpios property\n", __func__);
  43. goto err0;
  44. }
  45. of_gc = gc->data;
  46. if (!of_gc) {
  47. pr_debug("%s: gpio controller %s isn't registered\n",
  48. np->full_name, gc->full_name);
  49. ret = -ENODEV;
  50. goto err1;
  51. }
  52. gpio_cells = of_get_property(gc, "#gpio-cells", &size);
  53. if (!gpio_cells || size != sizeof(*gpio_cells) ||
  54. be32_to_cpup(gpio_cells) != of_gc->gpio_cells) {
  55. pr_debug("%s: wrong #gpio-cells for %s\n",
  56. np->full_name, gc->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 = of_gc->xlate(of_gc, np, gpio_spec, flags);
  64. if (ret < 0)
  65. goto err1;
  66. ret += of_gc->gc.base;
  67. err1:
  68. of_node_put(gc);
  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. * @of_gc: pointer to the of_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 of_gpio_chip *of_gc, struct device_node *np,
  116. const void *gpio_spec, enum of_gpio_flags *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 (of_gc->gpio_cells < 2) {
  127. WARN_ON(1);
  128. return -EINVAL;
  129. }
  130. if (n > of_gc->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. *
  147. * 2) In the of_gpio_chip structure:
  148. * - gpio_cells
  149. * - xlate callback (optional)
  150. *
  151. * 3) In the of_mm_gpio_chip structure:
  152. * - save_regs callback (optional)
  153. *
  154. * If succeeded, this function will map bank's memory and will
  155. * do all necessary work for you. Then you'll able to use .regs
  156. * to manage GPIOs from the callbacks.
  157. */
  158. int of_mm_gpiochip_add(struct device_node *np,
  159. struct of_mm_gpio_chip *mm_gc)
  160. {
  161. int ret = -ENOMEM;
  162. struct of_gpio_chip *of_gc = &mm_gc->of_gc;
  163. struct gpio_chip *gc = &of_gc->gc;
  164. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  165. if (!gc->label)
  166. goto err0;
  167. mm_gc->regs = of_iomap(np, 0);
  168. if (!mm_gc->regs)
  169. goto err1;
  170. gc->base = -1;
  171. if (!of_gc->xlate)
  172. of_gc->xlate = of_gpio_simple_xlate;
  173. if (mm_gc->save_regs)
  174. mm_gc->save_regs(mm_gc);
  175. np->data = of_gc;
  176. ret = gpiochip_add(gc);
  177. if (ret)
  178. goto err2;
  179. /* We don't want to lose the node and its ->data */
  180. of_node_get(np);
  181. pr_debug("%s: registered as generic GPIO chip, base is %d\n",
  182. np->full_name, gc->base);
  183. return 0;
  184. err2:
  185. np->data = NULL;
  186. iounmap(mm_gc->regs);
  187. err1:
  188. kfree(gc->label);
  189. err0:
  190. pr_err("%s: GPIO chip registration failed with status %d\n",
  191. np->full_name, ret);
  192. return ret;
  193. }
  194. EXPORT_SYMBOL(of_mm_gpiochip_add);