gpiolib.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 acpi_device *adev,
  31. const char *propname, int index,
  32. struct acpi_gpio_info *info);
  33. #else
  34. static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
  35. static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
  36. static inline void
  37. acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
  38. static inline void
  39. acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
  40. static inline struct gpio_desc *
  41. acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
  42. int index, struct acpi_gpio_info *info)
  43. {
  44. return ERR_PTR(-ENOSYS);
  45. }
  46. #endif
  47. struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  48. const char *list_name, int index, enum of_gpio_flags *flags);
  49. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
  50. extern struct spinlock gpio_lock;
  51. extern struct list_head gpio_chips;
  52. struct gpio_desc {
  53. struct gpio_chip *chip;
  54. unsigned long flags;
  55. /* flag symbols are bit numbers */
  56. #define FLAG_REQUESTED 0
  57. #define FLAG_IS_OUT 1
  58. #define FLAG_EXPORT 2 /* protected by sysfs_lock */
  59. #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
  60. #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
  61. #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
  62. #define FLAG_ACTIVE_LOW 6 /* value has active low */
  63. #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
  64. #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
  65. #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
  66. #define FLAG_SYSFS_DIR 10 /* show sysfs direction attribute */
  67. #define ID_SHIFT 16 /* add new flags before this one */
  68. #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
  69. #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
  70. const char *label;
  71. };
  72. int gpiod_request(struct gpio_desc *desc, const char *label);
  73. void gpiod_free(struct gpio_desc *desc);
  74. /*
  75. * Return the GPIO number of the passed descriptor relative to its chip
  76. */
  77. static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
  78. {
  79. return desc - &desc->chip->desc[0];
  80. }
  81. /* With descriptor prefix */
  82. #define gpiod_emerg(desc, fmt, ...) \
  83. pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  84. ##__VA_ARGS__)
  85. #define gpiod_crit(desc, fmt, ...) \
  86. pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  87. ##__VA_ARGS__)
  88. #define gpiod_err(desc, fmt, ...) \
  89. pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  90. ##__VA_ARGS__)
  91. #define gpiod_warn(desc, fmt, ...) \
  92. pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  93. ##__VA_ARGS__)
  94. #define gpiod_info(desc, fmt, ...) \
  95. pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  96. ##__VA_ARGS__)
  97. #define gpiod_dbg(desc, fmt, ...) \
  98. pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  99. ##__VA_ARGS__)
  100. /* With chip prefix */
  101. #define chip_emerg(chip, fmt, ...) \
  102. pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  103. #define chip_crit(chip, fmt, ...) \
  104. pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  105. #define chip_err(chip, fmt, ...) \
  106. pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  107. #define chip_warn(chip, fmt, ...) \
  108. pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  109. #define chip_info(chip, fmt, ...) \
  110. pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  111. #define chip_dbg(chip, fmt, ...) \
  112. pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  113. #ifdef CONFIG_GPIO_SYSFS
  114. int gpiochip_export(struct gpio_chip *chip);
  115. void gpiochip_unexport(struct gpio_chip *chip);
  116. #else
  117. static inline int gpiochip_export(struct gpio_chip *chip)
  118. {
  119. return 0;
  120. }
  121. static inline void gpiochip_unexport(struct gpio_chip *chip)
  122. {
  123. }
  124. #endif /* CONFIG_GPIO_SYSFS */
  125. #endif /* GPIOLIB_H */