1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Internal GPIO functions.
5 * Copyright (C) 2013, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
12 #include <linux/cdev.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/gpio/consumer.h> /* for enum gpiod_flags */
16 #include <linux/gpio/driver.h>
17 #include <linux/module.h>
18 #include <linux/notifier.h>
19 #include <linux/srcu.h>
21 #define GPIOCHIP_NAME "gpiochip"
24 * struct gpio_device - internal state container for GPIO devices
25 * @dev: the GPIO device struct
26 * @chrdev: character device for the GPIO device
27 * @id: numerical ID number for the GPIO chip
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 * @desc_srcu: ensures consistent state of GPIO descriptors exposed to users
35 * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
36 * of the @descs array.
37 * @can_sleep: indicate whether the GPIO chip driver's callbacks can sleep
38 * implying that they cannot be used from atomic context
39 * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
40 * at device creation time.
41 * @label: a descriptive name for the GPIO device, such as the part number
42 * or name of the IP component in a System on Chip.
43 * @data: per-instance data assigned by the driver
44 * @list: links gpio_device:s together for traversal
45 * @line_state_notifier: used to notify subscribers about lines being
46 * requested, released or reconfigured
47 * @device_notifier: used to notify character device wait queues about the GPIO
48 * device being unregistered
49 * @srcu: protects the pointer to the underlying GPIO chip
50 * @pin_ranges: range of pins served by the GPIO driver
52 * This state container holds most of the runtime variable data
53 * for a GPIO device and can hold references and live on after the
54 * GPIO chip has been removed, if it is still being used from
61 struct device *mockdev;
63 struct gpio_chip __rcu *chip;
64 struct gpio_desc *descs;
65 struct srcu_struct desc_srcu;
71 struct list_head list;
72 struct blocking_notifier_head line_state_notifier;
73 struct blocking_notifier_head device_notifier;
74 struct srcu_struct srcu;
78 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
79 * describe the actual pin range which they serve in an SoC. This
80 * information would be used by pinctrl subsystem to configure
81 * corresponding pins for gpio usage.
83 struct list_head pin_ranges;
87 static inline struct gpio_device *to_gpio_device(struct device *dev)
89 return container_of(dev, struct gpio_device, dev);
92 /* gpio suffixes used for ACPI and device tree lookup */
93 static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
96 * struct gpio_array - Opaque descriptor for a structure of GPIO array attributes
98 * @desc: Array of pointers to the GPIO descriptors
99 * @size: Number of elements in desc
100 * @chip: Parent GPIO chip
101 * @get_mask: Get mask used in fastpath
102 * @set_mask: Set mask used in fastpath
103 * @invert_mask: Invert mask used in fastpath
105 * This structure is attached to struct gpiod_descs obtained from
106 * gpiod_get_array() and can be passed back to get/set array functions in order
107 * to activate fast processing path if applicable.
110 struct gpio_desc **desc;
112 struct gpio_chip *chip;
113 unsigned long *get_mask;
114 unsigned long *set_mask;
115 unsigned long invert_mask[];
118 #define for_each_gpio_desc(gc, desc) \
119 for (unsigned int __i = 0; \
120 __i < gc->ngpio && (desc = gpiochip_get_desc(gc, __i)); \
123 #define for_each_gpio_desc_with_flag(gc, desc, flag) \
124 for_each_gpio_desc(gc, desc) \
125 if (!test_bit(flag, &desc->flags)) {} else
127 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
128 unsigned int array_size,
129 struct gpio_desc **desc_array,
130 struct gpio_array *array_info,
131 unsigned long *value_bitmap);
132 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
133 unsigned int array_size,
134 struct gpio_desc **desc_array,
135 struct gpio_array *array_info,
136 unsigned long *value_bitmap);
138 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
140 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action);
142 struct gpio_desc_label {
148 * struct gpio_desc - Opaque descriptor for a GPIO
150 * @gdev: Pointer to the parent GPIO device
151 * @flags: Binary descriptor flags
152 * @label: Name of the consumer
154 * @hog: Pointer to the device node that hogs this line (if any)
156 * These are obtained using gpiod_get() and are preferable to the old
157 * integer-based handles.
159 * Contrary to integers, a pointer to a &struct gpio_desc is guaranteed to be
160 * valid until the GPIO is released.
163 struct gpio_device *gdev;
165 /* flag symbols are bit numbers */
166 #define FLAG_REQUESTED 0
167 #define FLAG_IS_OUT 1
168 #define FLAG_EXPORT 2 /* protected by sysfs_lock */
169 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
170 #define FLAG_ACTIVE_LOW 6 /* value has active low */
171 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
172 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
173 #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
174 #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
175 #define FLAG_IS_HOGGED 11 /* GPIO is hogged */
176 #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
177 #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
178 #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
179 #define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */
180 #define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */
181 #define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */
182 #define FLAG_EVENT_CLOCK_REALTIME 18 /* GPIO CDEV reports REALTIME timestamps in events */
183 #define FLAG_EVENT_CLOCK_HTE 19 /* GPIO CDEV reports hardware timestamps in events */
185 /* Connection label */
186 struct gpio_desc_label __rcu *label;
187 /* Name of the GPIO */
189 #ifdef CONFIG_OF_DYNAMIC
190 struct device_node *hog;
194 #define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
196 struct gpio_chip_guard {
197 struct gpio_device *gdev;
198 struct gpio_chip *gc;
202 DEFINE_CLASS(gpio_chip_guard,
203 struct gpio_chip_guard,
204 srcu_read_unlock(&_T.gdev->srcu, _T.idx),
206 struct gpio_chip_guard _guard;
208 _guard.gdev = desc->gdev;
209 _guard.idx = srcu_read_lock(&_guard.gdev->srcu);
210 _guard.gc = srcu_dereference(_guard.gdev->chip,
215 struct gpio_desc *desc)
217 int gpiod_request(struct gpio_desc *desc, const char *label);
218 void gpiod_free(struct gpio_desc *desc);
220 static inline int gpiod_request_user(struct gpio_desc *desc, const char *label)
224 ret = gpiod_request(desc, label);
225 if (ret == -EPROBE_DEFER)
231 struct gpio_desc *gpiod_find_and_request(struct device *consumer,
232 struct fwnode_handle *fwnode,
235 enum gpiod_flags flags,
237 bool platform_lookup_allowed);
239 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
240 unsigned long lflags, enum gpiod_flags dflags);
241 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce);
242 int gpiod_hog(struct gpio_desc *desc, const char *name,
243 unsigned long lflags, enum gpiod_flags dflags);
244 int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev);
245 const char *gpiod_get_label(struct gpio_desc *desc);
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_err(desc, fmt, ...) \
259 scoped_guard(srcu, &desc->gdev->desc_srcu) { \
260 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
261 gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
265 #define gpiod_warn(desc, fmt, ...) \
267 scoped_guard(srcu, &desc->gdev->desc_srcu) { \
268 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
269 gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
273 #define gpiod_dbg(desc, fmt, ...) \
275 scoped_guard(srcu, &desc->gdev->desc_srcu) { \
276 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
277 gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
281 /* With chip prefix */
283 #define chip_err(gc, fmt, ...) \
284 dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
285 #define chip_warn(gc, fmt, ...) \
286 dev_warn(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
287 #define chip_info(gc, fmt, ...) \
288 dev_info(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
289 #define chip_dbg(gc, fmt, ...) \
290 dev_dbg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
292 #endif /* GPIOLIB_H */