1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Internal GPIO functions.
5 * Copyright (C) 2013, Intel Corporation
12 #include <linux/gpio/driver.h>
13 #include <linux/gpio/consumer.h> /* for enum gpiod_flags */
14 #include <linux/err.h>
15 #include <linux/device.h>
16 #include <linux/module.h>
17 #include <linux/cdev.h>
20 enum gpio_lookup_flags;
24 * struct gpio_device - internal state container for GPIO devices
25 * @id: numerical ID number for the GPIO chip
26 * @dev: the GPIO device struct
27 * @chrdev: character device for the GPIO device
28 * @mockdev: class device used by the deprecated sysfs interface (may be
30 * @owner: helps prevent removal of modules exporting active GPIOs
31 * @chip: pointer to the corresponding gpiochip, holding static
32 * data for this device
33 * @descs: array of ngpio descriptors.
34 * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
35 * of the @descs array.
36 * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
37 * at device creation time.
38 * @label: a descriptive name for the GPIO device, such as the part number
39 * or name of the IP component in a System on Chip.
40 * @data: per-instance data assigned by the driver
41 * @list: links gpio_device:s together for traversal
43 * This state container holds most of the runtime variable data
44 * for a GPIO device and can hold references and live on after the
45 * GPIO chip has been removed, if it is still being used from
52 struct device *mockdev;
54 struct gpio_chip *chip;
55 struct gpio_desc *descs;
60 struct list_head list;
64 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
65 * describe the actual pin range which they serve in an SoC. This
66 * information would be used by pinctrl subsystem to configure
67 * corresponding pins for gpio usage.
69 struct list_head pin_ranges;
74 * struct acpi_gpio_info - ACPI GPIO specific information
75 * @adev: reference to ACPI device which consumes GPIO resource
76 * @flags: GPIO initialization flags
77 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
78 * @polarity: interrupt polarity as provided by ACPI
79 * @triggering: triggering type as provided by ACPI
80 * @quirks: Linux specific quirks as provided by struct acpi_gpio_mapping
82 struct acpi_gpio_info {
83 struct acpi_device *adev;
84 enum gpiod_flags flags;
91 /* gpio suffixes used for ACPI and device tree lookup */
92 static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
95 struct gpio_desc *of_find_gpio(struct device *dev,
98 enum gpio_lookup_flags *flags);
99 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
100 const char *list_name, int index, enum of_gpio_flags *flags);
101 int of_gpiochip_add(struct gpio_chip *gc);
102 void of_gpiochip_remove(struct gpio_chip *gc);
104 static inline struct gpio_desc *of_find_gpio(struct device *dev,
107 enum gpio_lookup_flags *flags)
109 return ERR_PTR(-ENOENT);
111 static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
112 const char *list_name, int index, enum of_gpio_flags *flags)
114 return ERR_PTR(-ENOENT);
116 static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; }
117 static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
118 #endif /* CONFIG_OF_GPIO */
121 void acpi_gpiochip_add(struct gpio_chip *chip);
122 void acpi_gpiochip_remove(struct gpio_chip *chip);
124 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
125 void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
127 int acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags,
128 struct acpi_gpio_info *info);
130 struct gpio_desc *acpi_find_gpio(struct device *dev,
133 enum gpiod_flags *dflags,
134 enum gpio_lookup_flags *lookupflags);
135 struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
136 const char *propname, int index,
137 struct acpi_gpio_info *info);
139 int acpi_gpio_count(struct device *dev, const char *con_id);
141 bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
143 static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
144 static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
147 acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
150 acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
153 acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
158 static inline struct gpio_desc *
159 acpi_find_gpio(struct device *dev, const char *con_id,
160 unsigned int idx, enum gpiod_flags *dflags,
161 enum gpio_lookup_flags *lookupflags)
163 return ERR_PTR(-ENOENT);
165 static inline struct gpio_desc *
166 acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
167 int index, struct acpi_gpio_info *info)
169 return ERR_PTR(-ENXIO);
171 static inline int acpi_gpio_count(struct device *dev, const char *con_id)
176 static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
184 struct gpio_desc **desc;
186 struct gpio_chip *chip;
187 unsigned long *get_mask;
188 unsigned long *set_mask;
189 unsigned long invert_mask[];
192 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
193 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
194 unsigned int array_size,
195 struct gpio_desc **desc_array,
196 struct gpio_array *array_info,
197 unsigned long *value_bitmap);
198 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
199 unsigned int array_size,
200 struct gpio_desc **desc_array,
201 struct gpio_array *array_info,
202 unsigned long *value_bitmap);
204 /* This is just passed between gpiolib and devres */
205 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
206 const char *propname, int index,
207 enum gpiod_flags dflags,
210 extern struct spinlock gpio_lock;
211 extern struct list_head gpio_devices;
214 struct gpio_device *gdev;
216 /* flag symbols are bit numbers */
217 #define FLAG_REQUESTED 0
218 #define FLAG_IS_OUT 1
219 #define FLAG_EXPORT 2 /* protected by sysfs_lock */
220 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
221 #define FLAG_ACTIVE_LOW 6 /* value has active low */
222 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
223 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
224 #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
225 #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
226 #define FLAG_IS_HOGGED 11 /* GPIO is hogged */
227 #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
229 /* Connection label */
231 /* Name of the GPIO */
235 int gpiod_request(struct gpio_desc *desc, const char *label);
236 void gpiod_free(struct gpio_desc *desc);
237 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
238 unsigned long lflags, enum gpiod_flags dflags);
239 int gpiod_hog(struct gpio_desc *desc, const char *name,
240 unsigned long lflags, enum gpiod_flags dflags);
243 * Return the GPIO number of the passed descriptor relative to its chip
245 static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
247 return desc - &desc->gdev->descs[0];
250 void devprop_gpiochip_set_names(struct gpio_chip *chip,
251 const struct fwnode_handle *fwnode);
253 /* With descriptor prefix */
255 #define gpiod_emerg(desc, fmt, ...) \
256 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
258 #define gpiod_crit(desc, fmt, ...) \
259 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
261 #define gpiod_err(desc, fmt, ...) \
262 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
264 #define gpiod_warn(desc, fmt, ...) \
265 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
267 #define gpiod_info(desc, fmt, ...) \
268 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
270 #define gpiod_dbg(desc, fmt, ...) \
271 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
274 /* With chip prefix */
276 #define chip_emerg(chip, fmt, ...) \
277 dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
278 #define chip_crit(chip, fmt, ...) \
279 dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
280 #define chip_err(chip, fmt, ...) \
281 dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
282 #define chip_warn(chip, fmt, ...) \
283 dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
284 #define chip_info(chip, fmt, ...) \
285 dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
286 #define chip_dbg(chip, fmt, ...) \
287 dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
289 #ifdef CONFIG_GPIO_SYSFS
291 int gpiochip_sysfs_register(struct gpio_device *gdev);
292 void gpiochip_sysfs_unregister(struct gpio_device *gdev);
296 static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
301 static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
305 #endif /* CONFIG_GPIO_SYSFS */
307 #endif /* GPIOLIB_H */