gpio.c 5.3 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/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 u32 *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. *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 u32 *gpio = gpio_spec;
  118. /*
  119. * We're discouraging gpio_cells < 2, since that way you'll have to
  120. * write your own xlate function (that will have to retrive the GPIO
  121. * number and the flags from a single gpio cell -- this is possible,
  122. * but not recommended).
  123. */
  124. if (of_gc->gpio_cells < 2) {
  125. WARN_ON(1);
  126. return -EINVAL;
  127. }
  128. if (*gpio > of_gc->gc.ngpio)
  129. return -EINVAL;
  130. if (flags)
  131. *flags = gpio[1];
  132. return *gpio;
  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. *
  145. * 2) In the of_gpio_chip structure:
  146. * - gpio_cells
  147. * - 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 of_gpio_chip *of_gc = &mm_gc->of_gc;
  161. struct gpio_chip *gc = &of_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 (!of_gc->xlate)
  170. of_gc->xlate = of_gpio_simple_xlate;
  171. if (mm_gc->save_regs)
  172. mm_gc->save_regs(mm_gc);
  173. np->data = of_gc;
  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);