gpio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 - Get a GPIO number from the device tree to use with GPIO API
  21. * @np: device node to get GPIO from
  22. * @index: index of the GPIO
  23. *
  24. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  25. * value on the error condition.
  26. */
  27. int of_get_gpio(struct device_node *np, int index)
  28. {
  29. int ret = -EINVAL;
  30. struct device_node *gc;
  31. struct of_gpio_chip *of_gc = NULL;
  32. int size;
  33. const u32 *gpios;
  34. u32 nr_cells;
  35. int i;
  36. const void *gpio_spec;
  37. const u32 *gpio_cells;
  38. int gpio_index = 0;
  39. gpios = of_get_property(np, "gpios", &size);
  40. if (!gpios) {
  41. ret = -ENOENT;
  42. goto err0;
  43. }
  44. nr_cells = size / sizeof(u32);
  45. for (i = 0; i < nr_cells; gpio_index++) {
  46. const phandle *gpio_phandle;
  47. gpio_phandle = gpios + i;
  48. gpio_spec = gpio_phandle + 1;
  49. /* one cell hole in the gpios = <>; */
  50. if (!*gpio_phandle) {
  51. if (gpio_index == index)
  52. return -ENOENT;
  53. i++;
  54. continue;
  55. }
  56. gc = of_find_node_by_phandle(*gpio_phandle);
  57. if (!gc) {
  58. pr_debug("%s: could not find phandle for gpios\n",
  59. np->full_name);
  60. goto err0;
  61. }
  62. of_gc = gc->data;
  63. if (!of_gc) {
  64. pr_debug("%s: gpio controller %s isn't registered\n",
  65. np->full_name, gc->full_name);
  66. goto err1;
  67. }
  68. gpio_cells = of_get_property(gc, "#gpio-cells", &size);
  69. if (!gpio_cells || size != sizeof(*gpio_cells) ||
  70. *gpio_cells != of_gc->gpio_cells) {
  71. pr_debug("%s: wrong #gpio-cells for %s\n",
  72. np->full_name, gc->full_name);
  73. goto err1;
  74. }
  75. /* Next phandle is at phandle cells + #gpio-cells */
  76. i += sizeof(*gpio_phandle) / sizeof(u32) + *gpio_cells;
  77. if (i >= nr_cells + 1) {
  78. pr_debug("%s: insufficient gpio-spec length\n",
  79. np->full_name);
  80. goto err1;
  81. }
  82. if (gpio_index == index)
  83. break;
  84. of_gc = NULL;
  85. of_node_put(gc);
  86. }
  87. if (!of_gc) {
  88. ret = -ENOENT;
  89. goto err0;
  90. }
  91. ret = of_gc->xlate(of_gc, np, gpio_spec);
  92. if (ret < 0)
  93. goto err1;
  94. ret += of_gc->gc.base;
  95. err1:
  96. of_node_put(gc);
  97. err0:
  98. pr_debug("%s exited with status %d\n", __func__, ret);
  99. return ret;
  100. }
  101. EXPORT_SYMBOL(of_get_gpio);
  102. /**
  103. * of_gpio_simple_xlate - translate gpio_spec to the GPIO number
  104. * @of_gc: pointer to the of_gpio_chip structure
  105. * @np: device node of the GPIO chip
  106. * @gpio_spec: gpio specifier as found in the device tree
  107. *
  108. * This is simple translation function, suitable for the most 1:1 mapped
  109. * gpio chips. This function performs only one sanity check: whether gpio
  110. * is less than ngpios (that is specified in the gpio_chip).
  111. */
  112. int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np,
  113. const void *gpio_spec)
  114. {
  115. const u32 *gpio = gpio_spec;
  116. if (*gpio > of_gc->gc.ngpio)
  117. return -EINVAL;
  118. return *gpio;
  119. }
  120. EXPORT_SYMBOL(of_gpio_simple_xlate);
  121. /* Should be sufficient for now, later we'll use dynamic bases. */
  122. #if defined(CONFIG_PPC32) || defined(CONFIG_SPARC32)
  123. #define GPIOS_PER_CHIP 32
  124. #else
  125. #define GPIOS_PER_CHIP 64
  126. #endif
  127. static int of_get_gpiochip_base(struct device_node *np)
  128. {
  129. struct device_node *gc = NULL;
  130. int gpiochip_base = 0;
  131. while ((gc = of_find_all_nodes(gc))) {
  132. if (!of_get_property(gc, "gpio-controller", NULL))
  133. continue;
  134. if (gc != np) {
  135. gpiochip_base += GPIOS_PER_CHIP;
  136. continue;
  137. }
  138. of_node_put(gc);
  139. if (gpiochip_base >= ARCH_NR_GPIOS)
  140. return -ENOSPC;
  141. return gpiochip_base;
  142. }
  143. return -ENOENT;
  144. }
  145. /**
  146. * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
  147. * @np: device node of the GPIO chip
  148. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  149. *
  150. * To use this function you should allocate and fill mm_gc with:
  151. *
  152. * 1) In the gpio_chip structure:
  153. * - all the callbacks
  154. *
  155. * 2) In the of_gpio_chip structure:
  156. * - gpio_cells
  157. * - xlate callback (optional)
  158. *
  159. * 3) In the of_mm_gpio_chip structure:
  160. * - save_regs callback (optional)
  161. *
  162. * If succeeded, this function will map bank's memory and will
  163. * do all necessary work for you. Then you'll able to use .regs
  164. * to manage GPIOs from the callbacks.
  165. */
  166. int of_mm_gpiochip_add(struct device_node *np,
  167. struct of_mm_gpio_chip *mm_gc)
  168. {
  169. int ret = -ENOMEM;
  170. struct of_gpio_chip *of_gc = &mm_gc->of_gc;
  171. struct gpio_chip *gc = &of_gc->gc;
  172. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  173. if (!gc->label)
  174. goto err0;
  175. mm_gc->regs = of_iomap(np, 0);
  176. if (!mm_gc->regs)
  177. goto err1;
  178. gc->base = of_get_gpiochip_base(np);
  179. if (gc->base < 0) {
  180. ret = gc->base;
  181. goto err1;
  182. }
  183. if (!of_gc->xlate)
  184. of_gc->xlate = of_gpio_simple_xlate;
  185. if (mm_gc->save_regs)
  186. mm_gc->save_regs(mm_gc);
  187. np->data = of_gc;
  188. ret = gpiochip_add(gc);
  189. if (ret)
  190. goto err2;
  191. /* We don't want to lose the node and its ->data */
  192. of_node_get(np);
  193. pr_debug("%s: registered as generic GPIO chip, base is %d\n",
  194. np->full_name, gc->base);
  195. return 0;
  196. err2:
  197. np->data = NULL;
  198. iounmap(mm_gc->regs);
  199. err1:
  200. kfree(gc->label);
  201. err0:
  202. pr_err("%s: GPIO chip registration failed with status %d\n",
  203. np->full_name, ret);
  204. return ret;
  205. }
  206. EXPORT_SYMBOL(of_mm_gpiochip_add);