gpiolib-of.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/module.h>
  16. #include <linux/io.h>
  17. #include <linux/gpio.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/slab.h>
  22. /* Private data structure for of_gpiochip_find_and_xlate */
  23. struct gg_data {
  24. enum of_gpio_flags *flags;
  25. struct of_phandle_args gpiospec;
  26. int out_gpio;
  27. };
  28. /* Private function for resolving node pointer to gpio_chip */
  29. static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data)
  30. {
  31. struct gg_data *gg_data = data;
  32. int ret;
  33. if ((gc->of_node != gg_data->gpiospec.np) ||
  34. (gc->of_gpio_n_cells != gg_data->gpiospec.args_count) ||
  35. (!gc->of_xlate))
  36. return false;
  37. ret = gc->of_xlate(gc, &gg_data->gpiospec, gg_data->flags);
  38. if (ret < 0)
  39. return false;
  40. gg_data->out_gpio = ret + gc->base;
  41. return true;
  42. }
  43. /**
  44. * of_get_named_gpio_flags() - Get a GPIO number and flags to use with GPIO API
  45. * @np: device node to get GPIO from
  46. * @propname: property name containing gpio specifier(s)
  47. * @index: index of the GPIO
  48. * @flags: a flags pointer to fill in
  49. *
  50. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  51. * value on the error condition. If @flags is not NULL the function also fills
  52. * in flags for the GPIO.
  53. */
  54. int of_get_named_gpio_flags(struct device_node *np, const char *propname,
  55. int index, enum of_gpio_flags *flags)
  56. {
  57. /* Return -EPROBE_DEFER to support probe() functions to be called
  58. * later when the GPIO actually becomes available
  59. */
  60. struct gg_data gg_data = { .flags = flags, .out_gpio = -EPROBE_DEFER };
  61. int ret;
  62. /* .of_xlate might decide to not fill in the flags, so clear it. */
  63. if (flags)
  64. *flags = 0;
  65. ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
  66. &gg_data.gpiospec);
  67. if (ret) {
  68. pr_debug("%s: can't parse gpios property\n", __func__);
  69. return ret;
  70. }
  71. gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
  72. of_node_put(gg_data.gpiospec.np);
  73. pr_debug("%s exited with status %d\n", __func__, gg_data.out_gpio);
  74. return gg_data.out_gpio;
  75. }
  76. EXPORT_SYMBOL(of_get_named_gpio_flags);
  77. /**
  78. * of_gpio_named_count - Count GPIOs for a device
  79. * @np: device node to count GPIOs for
  80. * @propname: property name containing gpio specifier(s)
  81. *
  82. * The function returns the count of GPIOs specified for a node.
  83. *
  84. * Note that the empty GPIO specifiers counts too. For example,
  85. *
  86. * gpios = <0
  87. * &pio1 1 2
  88. * 0
  89. * &pio2 3 4>;
  90. *
  91. * defines four GPIOs (so this function will return 4), two of which
  92. * are not specified.
  93. */
  94. unsigned int of_gpio_named_count(struct device_node *np, const char* propname)
  95. {
  96. unsigned int cnt = 0;
  97. do {
  98. int ret;
  99. ret = of_parse_phandle_with_args(np, propname, "#gpio-cells",
  100. cnt, NULL);
  101. /* A hole in the gpios = <> counts anyway. */
  102. if (ret < 0 && ret != -EEXIST)
  103. break;
  104. } while (++cnt);
  105. return cnt;
  106. }
  107. EXPORT_SYMBOL(of_gpio_named_count);
  108. /**
  109. * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
  110. * @gc: pointer to the gpio_chip structure
  111. * @np: device node of the GPIO chip
  112. * @gpio_spec: gpio specifier as found in the device tree
  113. * @flags: a flags pointer to fill in
  114. *
  115. * This is simple translation function, suitable for the most 1:1 mapped
  116. * gpio chips. This function performs only one sanity check: whether gpio
  117. * is less than ngpios (that is specified in the gpio_chip).
  118. */
  119. int of_gpio_simple_xlate(struct gpio_chip *gc,
  120. const struct of_phandle_args *gpiospec, u32 *flags)
  121. {
  122. /*
  123. * We're discouraging gpio_cells < 2, since that way you'll have to
  124. * write your own xlate function (that will have to retrive the GPIO
  125. * number and the flags from a single gpio cell -- this is possible,
  126. * but not recommended).
  127. */
  128. if (gc->of_gpio_n_cells < 2) {
  129. WARN_ON(1);
  130. return -EINVAL;
  131. }
  132. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  133. return -EINVAL;
  134. if (gpiospec->args[0] >= gc->ngpio)
  135. return -EINVAL;
  136. if (flags)
  137. *flags = gpiospec->args[1];
  138. return gpiospec->args[0];
  139. }
  140. EXPORT_SYMBOL(of_gpio_simple_xlate);
  141. /**
  142. * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
  143. * @np: device node of the GPIO chip
  144. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  145. *
  146. * To use this function you should allocate and fill mm_gc with:
  147. *
  148. * 1) In the gpio_chip structure:
  149. * - all the callbacks
  150. * - of_gpio_n_cells
  151. * - of_xlate callback (optional)
  152. *
  153. * 3) In the of_mm_gpio_chip structure:
  154. * - save_regs callback (optional)
  155. *
  156. * If succeeded, this function will map bank's memory and will
  157. * do all necessary work for you. Then you'll able to use .regs
  158. * to manage GPIOs from the callbacks.
  159. */
  160. int of_mm_gpiochip_add(struct device_node *np,
  161. struct of_mm_gpio_chip *mm_gc)
  162. {
  163. int ret = -ENOMEM;
  164. struct gpio_chip *gc = &mm_gc->gc;
  165. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  166. if (!gc->label)
  167. goto err0;
  168. mm_gc->regs = of_iomap(np, 0);
  169. if (!mm_gc->regs)
  170. goto err1;
  171. gc->base = -1;
  172. if (mm_gc->save_regs)
  173. mm_gc->save_regs(mm_gc);
  174. mm_gc->gc.of_node = np;
  175. ret = gpiochip_add(gc);
  176. if (ret)
  177. goto err2;
  178. return 0;
  179. err2:
  180. iounmap(mm_gc->regs);
  181. err1:
  182. kfree(gc->label);
  183. err0:
  184. pr_err("%s: GPIO chip registration failed with status %d\n",
  185. np->full_name, ret);
  186. return ret;
  187. }
  188. EXPORT_SYMBOL(of_mm_gpiochip_add);
  189. void of_gpiochip_add(struct gpio_chip *chip)
  190. {
  191. if ((!chip->of_node) && (chip->dev))
  192. chip->of_node = chip->dev->of_node;
  193. if (!chip->of_node)
  194. return;
  195. if (!chip->of_xlate) {
  196. chip->of_gpio_n_cells = 2;
  197. chip->of_xlate = of_gpio_simple_xlate;
  198. }
  199. of_node_get(chip->of_node);
  200. }
  201. void of_gpiochip_remove(struct gpio_chip *chip)
  202. {
  203. if (chip->of_node)
  204. of_node_put(chip->of_node);
  205. }