gpio.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /**
  122. * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
  123. * @np: device node of the GPIO chip
  124. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  125. *
  126. * To use this function you should allocate and fill mm_gc with:
  127. *
  128. * 1) In the gpio_chip structure:
  129. * - all the callbacks
  130. *
  131. * 2) In the of_gpio_chip structure:
  132. * - gpio_cells
  133. * - xlate callback (optional)
  134. *
  135. * 3) In the of_mm_gpio_chip structure:
  136. * - save_regs callback (optional)
  137. *
  138. * If succeeded, this function will map bank's memory and will
  139. * do all necessary work for you. Then you'll able to use .regs
  140. * to manage GPIOs from the callbacks.
  141. */
  142. int of_mm_gpiochip_add(struct device_node *np,
  143. struct of_mm_gpio_chip *mm_gc)
  144. {
  145. int ret = -ENOMEM;
  146. struct of_gpio_chip *of_gc = &mm_gc->of_gc;
  147. struct gpio_chip *gc = &of_gc->gc;
  148. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  149. if (!gc->label)
  150. goto err0;
  151. mm_gc->regs = of_iomap(np, 0);
  152. if (!mm_gc->regs)
  153. goto err1;
  154. gc->base = -1;
  155. if (!of_gc->xlate)
  156. of_gc->xlate = of_gpio_simple_xlate;
  157. if (mm_gc->save_regs)
  158. mm_gc->save_regs(mm_gc);
  159. np->data = of_gc;
  160. ret = gpiochip_add(gc);
  161. if (ret)
  162. goto err2;
  163. /* We don't want to lose the node and its ->data */
  164. of_node_get(np);
  165. pr_debug("%s: registered as generic GPIO chip, base is %d\n",
  166. np->full_name, gc->base);
  167. return 0;
  168. err2:
  169. np->data = NULL;
  170. iounmap(mm_gc->regs);
  171. err1:
  172. kfree(gc->label);
  173. err0:
  174. pr_err("%s: GPIO chip registration failed with status %d\n",
  175. np->full_name, ret);
  176. return ret;
  177. }
  178. EXPORT_SYMBOL(of_mm_gpiochip_add);