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>
23 * struct gpio_device - internal state container for GPIO devices
24 * @id: numerical ID number for the GPIO chip
25 * @dev: the GPIO device struct
26 * @chrdev: character device for the GPIO device
27 * @mockdev: class device used by the deprecated sysfs interface (may be
29 * @owner: helps prevent removal of modules exporting active GPIOs
30 * @chip: pointer to the corresponding gpiochip, holding static
31 * data for this device
32 * @descs: array of ngpio descriptors.
33 * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
34 * of the @descs array.
35 * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
36 * at device creation time.
37 * @label: a descriptive name for the GPIO device, such as the part number
38 * or name of the IP component in a System on Chip.
39 * @data: per-instance data assigned by the driver
40 * @list: links gpio_device:s together for traversal
42 * This state container holds most of the runtime variable data
43 * for a GPIO device and can hold references and live on after the
44 * GPIO chip has been removed, if it is still being used from
51 struct device *mockdev;
53 struct gpio_chip *chip;
54 struct gpio_desc *descs;
59 struct list_head list;
63 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
64 * describe the actual pin range which they serve in an SoC. This
65 * information would be used by pinctrl subsystem to configure
66 * corresponding pins for gpio usage.
68 struct list_head pin_ranges;
73 * struct acpi_gpio_info - ACPI GPIO specific information
74 * @adev: reference to ACPI device which consumes GPIO resource
75 * @flags: GPIO initialization flags
76 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
77 * @pin_config: pin bias as provided by ACPI
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;
92 /* gpio suffixes used for ACPI and device tree lookup */
93 static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
96 struct gpio_desc *of_find_gpio(struct device *dev,
99 unsigned long *lookupflags);
100 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
101 const char *list_name, int index, enum of_gpio_flags *flags);
102 int of_gpiochip_add(struct gpio_chip *gc);
103 void of_gpiochip_remove(struct gpio_chip *gc);
105 static inline struct gpio_desc *of_find_gpio(struct device *dev,
108 unsigned long *lookupflags)
110 return ERR_PTR(-ENOENT);
112 static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
113 const char *list_name, int index, enum of_gpio_flags *flags)
115 return ERR_PTR(-ENOENT);
117 static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; }
118 static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
119 #endif /* CONFIG_OF_GPIO */
122 void acpi_gpiochip_add(struct gpio_chip *chip);
123 void acpi_gpiochip_remove(struct gpio_chip *chip);
125 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
126 void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
128 int acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags,
129 struct acpi_gpio_info *info);
130 int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
131 struct acpi_gpio_info *info);
133 struct gpio_desc *acpi_find_gpio(struct device *dev,
136 enum gpiod_flags *dflags,
137 unsigned long *lookupflags);
138 struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
139 const char *propname, int index,
140 struct acpi_gpio_info *info);
142 int acpi_gpio_count(struct device *dev, const char *con_id);
144 bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
146 static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
147 static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
150 acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
153 acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
156 acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
161 acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
162 struct acpi_gpio_info *info)
167 static inline struct gpio_desc *
168 acpi_find_gpio(struct device *dev, const char *con_id,
169 unsigned int idx, enum gpiod_flags *dflags,
170 unsigned long *lookupflags)
172 return ERR_PTR(-ENOENT);
174 static inline struct gpio_desc *
175 acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
176 int index, struct acpi_gpio_info *info)
178 return ERR_PTR(-ENXIO);
180 static inline int acpi_gpio_count(struct device *dev, const char *con_id)
185 static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
193 struct gpio_desc **desc;
195 struct gpio_chip *chip;
196 unsigned long *get_mask;
197 unsigned long *set_mask;
198 unsigned long invert_mask[];
201 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
202 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
203 unsigned int array_size,
204 struct gpio_desc **desc_array,
205 struct gpio_array *array_info,
206 unsigned long *value_bitmap);
207 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
208 unsigned int array_size,
209 struct gpio_desc **desc_array,
210 struct gpio_array *array_info,
211 unsigned long *value_bitmap);
213 extern spinlock_t gpio_lock;
214 extern struct list_head gpio_devices;
217 struct gpio_device *gdev;
219 /* flag symbols are bit numbers */
220 #define FLAG_REQUESTED 0
221 #define FLAG_IS_OUT 1
222 #define FLAG_EXPORT 2 /* protected by sysfs_lock */
223 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
224 #define FLAG_ACTIVE_LOW 6 /* value has active low */
225 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
226 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
227 #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
228 #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
229 #define FLAG_IS_HOGGED 11 /* GPIO is hogged */
230 #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
231 #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
232 #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
234 /* Connection label */
236 /* Name of the GPIO */
240 int gpiod_request(struct gpio_desc *desc, const char *label);
241 void gpiod_free(struct gpio_desc *desc);
242 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
243 unsigned long lflags, enum gpiod_flags dflags);
244 int gpiod_hog(struct gpio_desc *desc, const char *name,
245 unsigned long lflags, enum gpiod_flags dflags);
248 * Return the GPIO number of the passed descriptor relative to its chip
250 static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
252 return desc - &desc->gdev->descs[0];
255 /* With descriptor prefix */
257 #define gpiod_emerg(desc, fmt, ...) \
258 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
260 #define gpiod_crit(desc, fmt, ...) \
261 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
263 #define gpiod_err(desc, fmt, ...) \
264 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
266 #define gpiod_warn(desc, fmt, ...) \
267 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
269 #define gpiod_info(desc, fmt, ...) \
270 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
272 #define gpiod_dbg(desc, fmt, ...) \
273 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
276 /* With chip prefix */
278 #define chip_emerg(chip, fmt, ...) \
279 dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
280 #define chip_crit(chip, fmt, ...) \
281 dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
282 #define chip_err(chip, fmt, ...) \
283 dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
284 #define chip_warn(chip, fmt, ...) \
285 dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
286 #define chip_info(chip, fmt, ...) \
287 dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
288 #define chip_dbg(chip, fmt, ...) \
289 dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
291 #ifdef CONFIG_GPIO_SYSFS
293 int gpiochip_sysfs_register(struct gpio_device *gdev);
294 void gpiochip_sysfs_unregister(struct gpio_device *gdev);
298 static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
303 static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
307 #endif /* CONFIG_GPIO_SYSFS */
309 #endif /* GPIOLIB_H */