of_gpio.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. struct device_node;
  21. /*
  22. * This is Linux-specific flags. By default controllers' and Linux' mapping
  23. * match, but GPIO controllers are free to translate their own flags to
  24. * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
  25. */
  26. enum of_gpio_flags {
  27. OF_GPIO_ACTIVE_LOW = 0x1,
  28. };
  29. #ifdef CONFIG_OF_GPIO
  30. /*
  31. * OF GPIO chip for memory mapped banks
  32. */
  33. struct of_mm_gpio_chip {
  34. struct gpio_chip gc;
  35. void (*save_regs)(struct of_mm_gpio_chip *mm_gc);
  36. void __iomem *regs;
  37. };
  38. static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
  39. {
  40. return container_of(gc, struct of_mm_gpio_chip, gc);
  41. }
  42. extern int of_get_named_gpio_flags(struct device_node *np,
  43. const char *list_name, int index, enum of_gpio_flags *flags);
  44. extern unsigned int of_gpio_named_count(struct device_node *np,
  45. const char* propname);
  46. extern int of_mm_gpiochip_add(struct device_node *np,
  47. struct of_mm_gpio_chip *mm_gc);
  48. extern void of_gpiochip_add(struct gpio_chip *gc);
  49. extern void of_gpiochip_remove(struct gpio_chip *gc);
  50. extern struct gpio_chip *of_node_to_gpiochip(struct device_node *np);
  51. extern int of_gpio_simple_xlate(struct gpio_chip *gc,
  52. const struct of_phandle_args *gpiospec,
  53. u32 *flags);
  54. #else /* CONFIG_OF_GPIO */
  55. /* Drivers may not strictly depend on the GPIO support, so let them link. */
  56. static inline int of_get_named_gpio_flags(struct device_node *np,
  57. const char *list_name, int index, enum of_gpio_flags *flags)
  58. {
  59. return -ENOSYS;
  60. }
  61. static inline unsigned int of_gpio_named_count(struct device_node *np,
  62. const char* propname)
  63. {
  64. return 0;
  65. }
  66. static inline int of_gpio_simple_xlate(struct gpio_chip *gc,
  67. const struct of_phandle_args *gpiospec,
  68. u32 *flags)
  69. {
  70. return -ENOSYS;
  71. }
  72. static inline void of_gpiochip_add(struct gpio_chip *gc) { }
  73. static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
  74. #endif /* CONFIG_OF_GPIO */
  75. /**
  76. * of_gpio_count - Count GPIOs for a device
  77. * @np: device node to count GPIOs for
  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. static inline unsigned int of_gpio_count(struct device_node *np)
  92. {
  93. return of_gpio_named_count(np, "gpios");
  94. }
  95. /**
  96. * of_get_gpio_flags() - Get a GPIO number and flags to use with GPIO API
  97. * @np: device node to get GPIO from
  98. * @index: index of the GPIO
  99. * @flags: a flags pointer to fill in
  100. *
  101. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  102. * value on the error condition. If @flags is not NULL the function also fills
  103. * in flags for the GPIO.
  104. */
  105. static inline int of_get_gpio_flags(struct device_node *np, int index,
  106. enum of_gpio_flags *flags)
  107. {
  108. return of_get_named_gpio_flags(np, "gpios", index, flags);
  109. }
  110. /**
  111. * of_get_named_gpio() - Get a GPIO number to use with GPIO API
  112. * @np: device node to get GPIO from
  113. * @propname: Name of property containing gpio specifier(s)
  114. * @index: index of the GPIO
  115. *
  116. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  117. * value on the error condition.
  118. */
  119. static inline int of_get_named_gpio(struct device_node *np,
  120. const char *propname, int index)
  121. {
  122. return of_get_named_gpio_flags(np, propname, index, NULL);
  123. }
  124. /**
  125. * of_get_gpio() - Get a GPIO number to use with GPIO API
  126. * @np: device node to get GPIO from
  127. * @index: index of the GPIO
  128. *
  129. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  130. * value on the error condition.
  131. */
  132. static inline int of_get_gpio(struct device_node *np, int index)
  133. {
  134. return of_get_gpio_flags(np, index, NULL);
  135. }
  136. #endif /* __LINUX_OF_GPIO_H */