gpiolib.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Internal GPIO functions.
  3. *
  4. * Copyright (C) 2013, Intel Corporation
  5. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #ifndef GPIOLIB_H
  12. #define GPIOLIB_H
  13. #include <linux/err.h>
  14. #include <linux/device.h>
  15. enum of_gpio_flags;
  16. /**
  17. * struct acpi_gpio_info - ACPI GPIO specific information
  18. * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
  19. * @active_low: in case of @gpioint, the pin is active low
  20. */
  21. struct acpi_gpio_info {
  22. bool gpioint;
  23. bool active_low;
  24. };
  25. #ifdef CONFIG_ACPI
  26. void acpi_gpiochip_add(struct gpio_chip *chip);
  27. void acpi_gpiochip_remove(struct gpio_chip *chip);
  28. void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
  29. void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
  30. struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
  31. struct acpi_gpio_info *info);
  32. #else
  33. static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
  34. static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
  35. static inline void
  36. acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
  37. static inline void
  38. acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
  39. static inline struct gpio_desc *
  40. acpi_get_gpiod_by_index(struct device *dev, int index,
  41. struct acpi_gpio_info *info)
  42. {
  43. return ERR_PTR(-ENOSYS);
  44. }
  45. #endif
  46. struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  47. const char *list_name, int index, enum of_gpio_flags *flags);
  48. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
  49. extern struct spinlock gpio_lock;
  50. extern struct list_head gpio_chips;
  51. struct gpio_desc {
  52. struct gpio_chip *chip;
  53. unsigned long flags;
  54. /* flag symbols are bit numbers */
  55. #define FLAG_REQUESTED 0
  56. #define FLAG_IS_OUT 1
  57. #define FLAG_EXPORT 2 /* protected by sysfs_lock */
  58. #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
  59. #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
  60. #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
  61. #define FLAG_ACTIVE_LOW 6 /* value has active low */
  62. #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
  63. #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
  64. #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
  65. #define ID_SHIFT 16 /* add new flags before this one */
  66. #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
  67. #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
  68. const char *label;
  69. };
  70. int gpiod_request(struct gpio_desc *desc, const char *label);
  71. void gpiod_free(struct gpio_desc *desc);
  72. /*
  73. * Return the GPIO number of the passed descriptor relative to its chip
  74. */
  75. static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
  76. {
  77. return desc - &desc->chip->desc[0];
  78. }
  79. /* With descriptor prefix */
  80. #define gpiod_emerg(desc, fmt, ...) \
  81. pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  82. ##__VA_ARGS__)
  83. #define gpiod_crit(desc, fmt, ...) \
  84. pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  85. ##__VA_ARGS__)
  86. #define gpiod_err(desc, fmt, ...) \
  87. pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  88. ##__VA_ARGS__)
  89. #define gpiod_warn(desc, fmt, ...) \
  90. pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  91. ##__VA_ARGS__)
  92. #define gpiod_info(desc, fmt, ...) \
  93. pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  94. ##__VA_ARGS__)
  95. #define gpiod_dbg(desc, fmt, ...) \
  96. pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  97. ##__VA_ARGS__)
  98. /* With chip prefix */
  99. #define chip_emerg(chip, fmt, ...) \
  100. pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  101. #define chip_crit(chip, fmt, ...) \
  102. pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  103. #define chip_err(chip, fmt, ...) \
  104. pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  105. #define chip_warn(chip, fmt, ...) \
  106. pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  107. #define chip_info(chip, fmt, ...) \
  108. pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  109. #define chip_dbg(chip, fmt, ...) \
  110. pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  111. #ifdef CONFIG_GPIO_SYSFS
  112. int gpiochip_export(struct gpio_chip *chip);
  113. void gpiochip_unexport(struct gpio_chip *chip);
  114. #else
  115. static inline int gpiochip_export(struct gpio_chip *chip)
  116. {
  117. return 0;
  118. }
  119. static inline void gpiochip_unexport(struct gpio_chip *chip)
  120. {
  121. }
  122. #endif /* CONFIG_GPIO_SYSFS */
  123. #endif /* GPIOLIB_H */