gpiolib.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Internal GPIO functions.
  4. *
  5. * Copyright (C) 2013, Intel Corporation
  6. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  7. */
  8. #ifndef GPIOLIB_H
  9. #define GPIOLIB_H
  10. #include <linux/gpio/driver.h>
  11. #include <linux/gpio/consumer.h> /* for enum gpiod_flags */
  12. #include <linux/err.h>
  13. #include <linux/device.h>
  14. #include <linux/module.h>
  15. #include <linux/cdev.h>
  16. #define GPIOCHIP_NAME "gpiochip"
  17. /**
  18. * struct gpio_device - internal state container for GPIO devices
  19. * @id: numerical ID number for the GPIO chip
  20. * @dev: the GPIO device struct
  21. * @chrdev: character device for the GPIO device
  22. * @mockdev: class device used by the deprecated sysfs interface (may be
  23. * NULL)
  24. * @owner: helps prevent removal of modules exporting active GPIOs
  25. * @chip: pointer to the corresponding gpiochip, holding static
  26. * data for this device
  27. * @descs: array of ngpio descriptors.
  28. * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
  29. * of the @descs array.
  30. * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
  31. * at device creation time.
  32. * @label: a descriptive name for the GPIO device, such as the part number
  33. * or name of the IP component in a System on Chip.
  34. * @data: per-instance data assigned by the driver
  35. * @list: links gpio_device:s together for traversal
  36. *
  37. * This state container holds most of the runtime variable data
  38. * for a GPIO device and can hold references and live on after the
  39. * GPIO chip has been removed, if it is still being used from
  40. * userspace.
  41. */
  42. struct gpio_device {
  43. int id;
  44. struct device dev;
  45. struct cdev chrdev;
  46. struct device *mockdev;
  47. struct module *owner;
  48. struct gpio_chip *chip;
  49. struct gpio_desc *descs;
  50. int base;
  51. u16 ngpio;
  52. const char *label;
  53. void *data;
  54. struct list_head list;
  55. struct blocking_notifier_head notifier;
  56. #ifdef CONFIG_PINCTRL
  57. /*
  58. * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
  59. * describe the actual pin range which they serve in an SoC. This
  60. * information would be used by pinctrl subsystem to configure
  61. * corresponding pins for gpio usage.
  62. */
  63. struct list_head pin_ranges;
  64. #endif
  65. };
  66. /* gpio suffixes used for ACPI and device tree lookup */
  67. static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
  68. struct gpio_array {
  69. struct gpio_desc **desc;
  70. unsigned int size;
  71. struct gpio_chip *chip;
  72. unsigned long *get_mask;
  73. unsigned long *set_mask;
  74. unsigned long invert_mask[];
  75. };
  76. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
  77. int gpiod_get_array_value_complex(bool raw, bool can_sleep,
  78. unsigned int array_size,
  79. struct gpio_desc **desc_array,
  80. struct gpio_array *array_info,
  81. unsigned long *value_bitmap);
  82. int gpiod_set_array_value_complex(bool raw, bool can_sleep,
  83. unsigned int array_size,
  84. struct gpio_desc **desc_array,
  85. struct gpio_array *array_info,
  86. unsigned long *value_bitmap);
  87. extern spinlock_t gpio_lock;
  88. extern struct list_head gpio_devices;
  89. struct gpio_desc {
  90. struct gpio_device *gdev;
  91. unsigned long flags;
  92. /* flag symbols are bit numbers */
  93. #define FLAG_REQUESTED 0
  94. #define FLAG_IS_OUT 1
  95. #define FLAG_EXPORT 2 /* protected by sysfs_lock */
  96. #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
  97. #define FLAG_ACTIVE_LOW 6 /* value has active low */
  98. #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
  99. #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
  100. #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
  101. #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
  102. #define FLAG_IS_HOGGED 11 /* GPIO is hogged */
  103. #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
  104. #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
  105. #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
  106. #define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */
  107. #define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */
  108. #define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */
  109. /* Connection label */
  110. const char *label;
  111. /* Name of the GPIO */
  112. const char *name;
  113. #ifdef CONFIG_OF_DYNAMIC
  114. struct device_node *hog;
  115. #endif
  116. #ifdef CONFIG_GPIO_CDEV
  117. /* debounce period in microseconds */
  118. unsigned int debounce_period_us;
  119. #endif
  120. };
  121. int gpiod_request(struct gpio_desc *desc, const char *label);
  122. void gpiod_free(struct gpio_desc *desc);
  123. int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
  124. unsigned long lflags, enum gpiod_flags dflags);
  125. int gpiod_hog(struct gpio_desc *desc, const char *name,
  126. unsigned long lflags, enum gpiod_flags dflags);
  127. /*
  128. * Return the GPIO number of the passed descriptor relative to its chip
  129. */
  130. static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
  131. {
  132. return desc - &desc->gdev->descs[0];
  133. }
  134. /* With descriptor prefix */
  135. #define gpiod_emerg(desc, fmt, ...) \
  136. pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  137. ##__VA_ARGS__)
  138. #define gpiod_crit(desc, fmt, ...) \
  139. pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  140. ##__VA_ARGS__)
  141. #define gpiod_err(desc, fmt, ...) \
  142. pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  143. ##__VA_ARGS__)
  144. #define gpiod_warn(desc, fmt, ...) \
  145. pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  146. ##__VA_ARGS__)
  147. #define gpiod_info(desc, fmt, ...) \
  148. pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  149. ##__VA_ARGS__)
  150. #define gpiod_dbg(desc, fmt, ...) \
  151. pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  152. ##__VA_ARGS__)
  153. /* With chip prefix */
  154. #define chip_emerg(gc, fmt, ...) \
  155. dev_emerg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  156. #define chip_crit(gc, fmt, ...) \
  157. dev_crit(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  158. #define chip_err(gc, fmt, ...) \
  159. dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  160. #define chip_warn(gc, fmt, ...) \
  161. dev_warn(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  162. #define chip_info(gc, fmt, ...) \
  163. dev_info(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  164. #define chip_dbg(gc, fmt, ...) \
  165. dev_dbg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
  166. #endif /* GPIOLIB_H */