of_gpio.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. OF_GPIO_SINGLE_ENDED = 0x2,
  29. OF_GPIO_OPEN_DRAIN = 0x4,
  30. OF_GPIO_TRANSITORY = 0x8,
  31. };
  32. #ifdef CONFIG_OF_GPIO
  33. /*
  34. * OF GPIO chip for memory mapped banks
  35. */
  36. struct of_mm_gpio_chip {
  37. struct gpio_chip gc;
  38. void (*save_regs)(struct of_mm_gpio_chip *mm_gc);
  39. void __iomem *regs;
  40. };
  41. static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
  42. {
  43. return container_of(gc, struct of_mm_gpio_chip, gc);
  44. }
  45. extern int of_get_named_gpio_flags(struct device_node *np,
  46. const char *list_name, int index, enum of_gpio_flags *flags);
  47. extern int of_mm_gpiochip_add_data(struct device_node *np,
  48. struct of_mm_gpio_chip *mm_gc,
  49. void *data);
  50. static inline int of_mm_gpiochip_add(struct device_node *np,
  51. struct of_mm_gpio_chip *mm_gc)
  52. {
  53. return of_mm_gpiochip_add_data(np, mm_gc, NULL);
  54. }
  55. extern void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc);
  56. extern int of_gpio_simple_xlate(struct gpio_chip *gc,
  57. const struct of_phandle_args *gpiospec,
  58. u32 *flags);
  59. #else /* CONFIG_OF_GPIO */
  60. /* Drivers may not strictly depend on the GPIO support, so let them link. */
  61. static inline int of_get_named_gpio_flags(struct device_node *np,
  62. const char *list_name, int index, enum of_gpio_flags *flags)
  63. {
  64. if (flags)
  65. *flags = 0;
  66. return -ENOSYS;
  67. }
  68. static inline int of_gpio_simple_xlate(struct gpio_chip *gc,
  69. const struct of_phandle_args *gpiospec,
  70. u32 *flags)
  71. {
  72. return -ENOSYS;
  73. }
  74. #endif /* CONFIG_OF_GPIO */
  75. /**
  76. * of_gpio_named_count() - Count GPIOs for a device
  77. * @np: device node to count GPIOs for
  78. * @propname: property name containing gpio specifier(s)
  79. *
  80. * The function returns the count of GPIOs specified for a node.
  81. * Note that the empty GPIO specifiers count too. Returns either
  82. * Number of gpios defined in property,
  83. * -EINVAL for an incorrectly formed gpios property, or
  84. * -ENOENT for a missing gpios property
  85. *
  86. * Example:
  87. * gpios = <0
  88. * &gpio1 1 2
  89. * 0
  90. * &gpio2 3 4>;
  91. *
  92. * The above example defines four GPIOs, two of which are not specified.
  93. * This function will return '4'
  94. */
  95. static inline int of_gpio_named_count(struct device_node *np, const char* propname)
  96. {
  97. return of_count_phandle_with_args(np, propname, "#gpio-cells");
  98. }
  99. /**
  100. * of_gpio_count() - Count GPIOs for a device
  101. * @np: device node to count GPIOs for
  102. *
  103. * Same as of_gpio_named_count, but hard coded to use the 'gpios' property
  104. */
  105. static inline int of_gpio_count(struct device_node *np)
  106. {
  107. return of_gpio_named_count(np, "gpios");
  108. }
  109. static inline int of_get_gpio_flags(struct device_node *np, int index,
  110. enum of_gpio_flags *flags)
  111. {
  112. return of_get_named_gpio_flags(np, "gpios", index, flags);
  113. }
  114. /**
  115. * of_get_named_gpio() - Get a GPIO number to use with GPIO API
  116. * @np: device node to get GPIO from
  117. * @propname: Name of property containing gpio specifier(s)
  118. * @index: index of the GPIO
  119. *
  120. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  121. * value on the error condition.
  122. */
  123. static inline int of_get_named_gpio(struct device_node *np,
  124. const char *propname, int index)
  125. {
  126. return of_get_named_gpio_flags(np, propname, index, NULL);
  127. }
  128. /**
  129. * of_get_gpio() - Get a GPIO number to use with GPIO API
  130. * @np: device node to get GPIO from
  131. * @index: index of the GPIO
  132. *
  133. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  134. * value on the error condition.
  135. */
  136. static inline int of_get_gpio(struct device_node *np, int index)
  137. {
  138. return of_get_gpio_flags(np, index, NULL);
  139. }
  140. #endif /* __LINUX_OF_GPIO_H */