of_gpio.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifndef __LINUX_OF_GPIO_H
  14. #define __LINUX_OF_GPIO_H
  15. #include <linux/compiler.h>
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/gpio.h>
  19. #include <linux/of.h>
  20. #include <linux/gpio/consumer.h>
  21. struct device_node;
  22. /*
  23. * This is Linux-specific flags. By default controllers' and Linux' mapping
  24. * match, but GPIO controllers are free to translate their own flags to
  25. * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
  26. */
  27. enum of_gpio_flags {
  28. OF_GPIO_ACTIVE_LOW = 0x1,
  29. };
  30. #ifdef CONFIG_OF_GPIO
  31. /*
  32. * OF GPIO chip for memory mapped banks
  33. */
  34. struct of_mm_gpio_chip {
  35. struct gpio_chip gc;
  36. void (*save_regs)(struct of_mm_gpio_chip *mm_gc);
  37. void __iomem *regs;
  38. };
  39. static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
  40. {
  41. return container_of(gc, struct of_mm_gpio_chip, gc);
  42. }
  43. extern struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  44. const char *list_name, int index, enum of_gpio_flags *flags);
  45. extern int of_mm_gpiochip_add(struct device_node *np,
  46. struct of_mm_gpio_chip *mm_gc);
  47. extern void of_gpiochip_add(struct gpio_chip *gc);
  48. extern void of_gpiochip_remove(struct gpio_chip *gc);
  49. extern int of_gpio_simple_xlate(struct gpio_chip *gc,
  50. const struct of_phandle_args *gpiospec,
  51. u32 *flags);
  52. #else /* CONFIG_OF_GPIO */
  53. /* Drivers may not strictly depend on the GPIO support, so let them link. */
  54. static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  55. const char *list_name, int index, enum of_gpio_flags *flags)
  56. {
  57. return ERR_PTR(-ENOSYS);
  58. }
  59. static inline int of_gpio_simple_xlate(struct gpio_chip *gc,
  60. const struct of_phandle_args *gpiospec,
  61. u32 *flags)
  62. {
  63. return -ENOSYS;
  64. }
  65. static inline void of_gpiochip_add(struct gpio_chip *gc) { }
  66. static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
  67. #endif /* CONFIG_OF_GPIO */
  68. static inline int of_get_named_gpio_flags(struct device_node *np,
  69. const char *list_name, int index, enum of_gpio_flags *flags)
  70. {
  71. struct gpio_desc *desc;
  72. desc = of_get_named_gpiod_flags(np, list_name, index, flags);
  73. if (IS_ERR(desc))
  74. return PTR_ERR(desc);
  75. else
  76. return desc_to_gpio(desc);
  77. }
  78. /**
  79. * of_gpio_named_count() - Count GPIOs for a device
  80. * @np: device node to count GPIOs for
  81. * @propname: property name containing gpio specifier(s)
  82. *
  83. * The function returns the count of GPIOs specified for a node.
  84. * Note that the empty GPIO specifiers count too. Returns either
  85. * Number of gpios defined in property,
  86. * -EINVAL for an incorrectly formed gpios property, or
  87. * -ENOENT for a missing gpios property
  88. *
  89. * Example:
  90. * gpios = <0
  91. * &gpio1 1 2
  92. * 0
  93. * &gpio2 3 4>;
  94. *
  95. * The above example defines four GPIOs, two of which are not specified.
  96. * This function will return '4'
  97. */
  98. static inline int of_gpio_named_count(struct device_node *np, const char* propname)
  99. {
  100. return of_count_phandle_with_args(np, propname, "#gpio-cells");
  101. }
  102. /**
  103. * of_gpio_count() - Count GPIOs for a device
  104. * @np: device node to count GPIOs for
  105. *
  106. * Same as of_gpio_named_count, but hard coded to use the 'gpios' property
  107. */
  108. static inline int of_gpio_count(struct device_node *np)
  109. {
  110. return of_gpio_named_count(np, "gpios");
  111. }
  112. /**
  113. * of_get_gpiod_flags() - Get a GPIO descriptor and flags to use with GPIO API
  114. * @np: device node to get GPIO from
  115. * @index: index of the GPIO
  116. * @flags: a flags pointer to fill in
  117. *
  118. * Returns GPIO descriptor to use with Linux generic GPIO API, or a errno
  119. * value on the error condition. If @flags is not NULL the function also fills
  120. * in flags for the GPIO.
  121. */
  122. static inline struct gpio_desc *of_get_gpiod_flags(struct device_node *np,
  123. int index, enum of_gpio_flags *flags)
  124. {
  125. return of_get_named_gpiod_flags(np, "gpios", index, flags);
  126. }
  127. static inline int of_get_gpio_flags(struct device_node *np, int index,
  128. enum of_gpio_flags *flags)
  129. {
  130. return of_get_named_gpio_flags(np, "gpios", index, flags);
  131. }
  132. /**
  133. * of_get_named_gpio() - Get a GPIO number to use with GPIO API
  134. * @np: device node to get GPIO from
  135. * @propname: Name of property containing gpio specifier(s)
  136. * @index: index of the GPIO
  137. *
  138. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  139. * value on the error condition.
  140. */
  141. static inline int of_get_named_gpio(struct device_node *np,
  142. const char *propname, int index)
  143. {
  144. return of_get_named_gpio_flags(np, propname, index, NULL);
  145. }
  146. /**
  147. * of_get_gpio() - Get a GPIO number to use with GPIO API
  148. * @np: device node to get GPIO from
  149. * @index: index of the GPIO
  150. *
  151. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  152. * value on the error condition.
  153. */
  154. static inline int of_get_gpio(struct device_node *np, int index)
  155. {
  156. return of_get_gpio_flags(np, index, NULL);
  157. }
  158. #endif /* __LINUX_OF_GPIO_H */