gpiolib-of.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_is_match */
  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. struct gg_data gg_data = { .flags = flags, .out_gpio = -ENODEV };
  58. int ret;
  59. /* .of_xlate might decide to not fill in the flags, so clear it. */
  60. if (flags)
  61. *flags = 0;
  62. ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
  63. &gg_data.gpiospec);
  64. if (ret) {
  65. pr_debug("%s: can't parse gpios property\n", __func__);
  66. return -EINVAL;
  67. }
  68. gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
  69. of_node_put(gg_data.gpiospec.np);
  70. pr_debug("%s exited with status %d\n", __func__, ret);
  71. return gg_data.out_gpio;
  72. }
  73. EXPORT_SYMBOL(of_get_named_gpio_flags);
  74. /**
  75. * of_gpio_named_count - Count GPIOs for a device
  76. * @np: device node to count GPIOs for
  77. * @propname: property name containing gpio specifier(s)
  78. *
  79. * The function returns the count of GPIOs specified for a node.
  80. *
  81. * Note that the empty GPIO specifiers counts too. For example,
  82. *
  83. * gpios = <0
  84. * &pio1 1 2
  85. * 0
  86. * &pio2 3 4>;
  87. *
  88. * defines four GPIOs (so this function will return 4), two of which
  89. * are not specified.
  90. */
  91. unsigned int of_gpio_named_count(struct device_node *np, const char* propname)
  92. {
  93. unsigned int cnt = 0;
  94. do {
  95. int ret;
  96. ret = of_parse_phandle_with_args(np, propname, "#gpio-cells",
  97. cnt, NULL);
  98. /* A hole in the gpios = <> counts anyway. */
  99. if (ret < 0 && ret != -EEXIST)
  100. break;
  101. } while (++cnt);
  102. return cnt;
  103. }
  104. EXPORT_SYMBOL(of_gpio_named_count);
  105. /**
  106. * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
  107. * @gc: pointer to the gpio_chip structure
  108. * @np: device node of the GPIO chip
  109. * @gpio_spec: gpio specifier as found in the device tree
  110. * @flags: a flags pointer to fill in
  111. *
  112. * This is simple translation function, suitable for the most 1:1 mapped
  113. * gpio chips. This function performs only one sanity check: whether gpio
  114. * is less than ngpios (that is specified in the gpio_chip).
  115. */
  116. int of_gpio_simple_xlate(struct gpio_chip *gc,
  117. const struct of_phandle_args *gpiospec, u32 *flags)
  118. {
  119. /*
  120. * We're discouraging gpio_cells < 2, since that way you'll have to
  121. * write your own xlate function (that will have to retrive the GPIO
  122. * number and the flags from a single gpio cell -- this is possible,
  123. * but not recommended).
  124. */
  125. if (gc->of_gpio_n_cells < 2) {
  126. WARN_ON(1);
  127. return -EINVAL;
  128. }
  129. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  130. return -EINVAL;
  131. if (gpiospec->args[0] >= gc->ngpio)
  132. return -EINVAL;
  133. if (flags)
  134. *flags = gpiospec->args[1];
  135. return gpiospec->args[0];
  136. }
  137. EXPORT_SYMBOL(of_gpio_simple_xlate);
  138. /**
  139. * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
  140. * @np: device node of the GPIO chip
  141. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  142. *
  143. * To use this function you should allocate and fill mm_gc with:
  144. *
  145. * 1) In the gpio_chip structure:
  146. * - all the callbacks
  147. * - of_gpio_n_cells
  148. * - of_xlate callback (optional)
  149. *
  150. * 3) In the of_mm_gpio_chip structure:
  151. * - save_regs callback (optional)
  152. *
  153. * If succeeded, this function will map bank's memory and will
  154. * do all necessary work for you. Then you'll able to use .regs
  155. * to manage GPIOs from the callbacks.
  156. */
  157. int of_mm_gpiochip_add(struct device_node *np,
  158. struct of_mm_gpio_chip *mm_gc)
  159. {
  160. int ret = -ENOMEM;
  161. struct gpio_chip *gc = &mm_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 (mm_gc->save_regs)
  170. mm_gc->save_regs(mm_gc);
  171. mm_gc->gc.of_node = np;
  172. ret = gpiochip_add(gc);
  173. if (ret)
  174. goto err2;
  175. return 0;
  176. err2:
  177. iounmap(mm_gc->regs);
  178. err1:
  179. kfree(gc->label);
  180. err0:
  181. pr_err("%s: GPIO chip registration failed with status %d\n",
  182. np->full_name, ret);
  183. return ret;
  184. }
  185. EXPORT_SYMBOL(of_mm_gpiochip_add);
  186. void of_gpiochip_add(struct gpio_chip *chip)
  187. {
  188. if ((!chip->of_node) && (chip->dev))
  189. chip->of_node = chip->dev->of_node;
  190. if (!chip->of_node)
  191. return;
  192. if (!chip->of_xlate) {
  193. chip->of_gpio_n_cells = 2;
  194. chip->of_xlate = of_gpio_simple_xlate;
  195. }
  196. of_node_get(chip->of_node);
  197. }
  198. void of_gpiochip_remove(struct gpio_chip *chip)
  199. {
  200. if (chip->of_node)
  201. of_node_put(chip->of_node);
  202. }