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
9#ifndef GPIOLIB_H
10#define GPIOLIB_H
11
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/spinlock.h>
20#include <linux/srcu.h>
21#include <linux/workqueue.h>
22
23#define GPIOCHIP_NAME "gpiochip"
24
25/**
26 * struct gpio_device - internal state container for GPIO devices
27 * @dev: the GPIO device struct
28 * @chrdev: character device for the GPIO device
29 * @id: numerical ID number for the GPIO chip
30 * @mockdev: class device used by the deprecated sysfs interface (may be
31 * NULL)
32 * @owner: helps prevent removal of modules exporting active GPIOs
33 * @chip: pointer to the corresponding gpiochip, holding static
34 * data for this device
35 * @descs: array of ngpio descriptors.
36 * @valid_mask: If not %NULL, holds bitmask of GPIOs which are valid to be
37 * used from the chip.
38 * @desc_srcu: ensures consistent state of GPIO descriptors exposed to users
39 * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
40 * of the @descs array.
41 * @can_sleep: indicate whether the GPIO chip driver's callbacks can sleep
42 * implying that they cannot be used from atomic context
43 * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
44 * at device creation time.
45 * @label: a descriptive name for the GPIO device, such as the part number
46 * or name of the IP component in a System on Chip.
47 * @data: per-instance data assigned by the driver
48 * @list: links gpio_device:s together for traversal
49 * @line_state_notifier: used to notify subscribers about lines being
50 * requested, released or reconfigured
51 * @line_state_lock: RW-spinlock protecting the line state notifier
52 * @line_state_wq: used to emit line state events from a separate thread in
53 * process context
54 * @device_notifier: used to notify character device wait queues about the GPIO
55 * device being unregistered
56 * @srcu: protects the pointer to the underlying GPIO chip
57 * @pin_ranges: range of pins served by the GPIO driver
58 *
59 * This state container holds most of the runtime variable data
60 * for a GPIO device and can hold references and live on after the
61 * GPIO chip has been removed, if it is still being used from
62 * userspace.
63 */
64struct gpio_device {
65 struct device dev;
66 struct cdev chrdev;
67 int id;
68 struct device *mockdev;
69 struct module *owner;
70 struct gpio_chip __rcu *chip;
71 struct gpio_desc *descs;
72 unsigned long *valid_mask;
73 struct srcu_struct desc_srcu;
74 unsigned int base;
75 u16 ngpio;
76 bool can_sleep;
77 const char *label;
78 void *data;
79 struct list_head list;
80 struct raw_notifier_head line_state_notifier;
81 rwlock_t line_state_lock;
82 struct workqueue_struct *line_state_wq;
83 struct blocking_notifier_head device_notifier;
84 struct srcu_struct srcu;
85
86#ifdef CONFIG_PINCTRL
87 /*
88 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
89 * describe the actual pin range which they serve in an SoC. This
90 * information would be used by pinctrl subsystem to configure
91 * corresponding pins for gpio usage.
92 */
93 struct list_head pin_ranges;
94#endif
95};
96
97static inline struct gpio_device *to_gpio_device(struct device *dev)
98{
99 return container_of(dev, struct gpio_device, dev);
100}
101
102/* GPIO suffixes used for ACPI and device tree lookup */
103extern const char *const gpio_suffixes[];
104
105#define for_each_gpio_property_name(propname, con_id) \
106 for (const char * const *__suffixes = gpio_suffixes; \
107 *__suffixes && ({ \
108 const char *__gs = *__suffixes; \
109 \
110 if (con_id) \
111 snprintf(propname, sizeof(propname), "%s-%s", con_id, __gs); \
112 else \
113 snprintf(propname, sizeof(propname), "%s", __gs); \
114 1; \
115 }); \
116 __suffixes++)
117
118/**
119 * struct gpio_array - Opaque descriptor for a structure of GPIO array attributes
120 *
121 * @desc: Array of pointers to the GPIO descriptors
122 * @size: Number of elements in desc
123 * @gdev: Parent GPIO device
124 * @get_mask: Get mask used in fastpath
125 * @set_mask: Set mask used in fastpath
126 * @invert_mask: Invert mask used in fastpath
127 *
128 * This structure is attached to struct gpiod_descs obtained from
129 * gpiod_get_array() and can be passed back to get/set array functions in order
130 * to activate fast processing path if applicable.
131 */
132struct gpio_array {
133 struct gpio_desc **desc;
134 unsigned int size;
135 struct gpio_device *gdev;
136 unsigned long *get_mask;
137 unsigned long *set_mask;
138 unsigned long invert_mask[];
139};
140
141#define for_each_gpio_desc(gc, desc) \
142 for (unsigned int __i = 0; \
143 __i < gc->ngpio && (desc = gpiochip_get_desc(gc, __i)); \
144 __i++) \
145
146#define for_each_gpio_desc_with_flag(gc, desc, flag) \
147 for_each_gpio_desc(gc, desc) \
148 if (!test_bit(flag, &desc->flags)) {} else
149
150int gpiod_get_array_value_complex(bool raw, bool can_sleep,
151 unsigned int array_size,
152 struct gpio_desc **desc_array,
153 struct gpio_array *array_info,
154 unsigned long *value_bitmap);
155int gpiod_set_array_value_complex(bool raw, bool can_sleep,
156 unsigned int array_size,
157 struct gpio_desc **desc_array,
158 struct gpio_array *array_info,
159 unsigned long *value_bitmap);
160
161int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
162
163void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action);
164int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value);
165int gpiod_direction_input_nonotify(struct gpio_desc *desc);
166
167struct gpio_desc_label {
168 struct rcu_head rh;
169 char str[];
170};
171
172/**
173 * struct gpio_desc - Opaque descriptor for a GPIO
174 *
175 * @gdev: Pointer to the parent GPIO device
176 * @flags: Binary descriptor flags
177 * @label: Name of the consumer
178 * @name: Line name
179 * @hog: Pointer to the device node that hogs this line (if any)
180 * @debounce_period_us: Debounce period in microseconds
181 *
182 * These are obtained using gpiod_get() and are preferable to the old
183 * integer-based handles.
184 *
185 * Contrary to integers, a pointer to a &struct gpio_desc is guaranteed to be
186 * valid until the GPIO is released.
187 */
188struct gpio_desc {
189 struct gpio_device *gdev;
190 unsigned long flags;
191/* flag symbols are bit numbers */
192#define FLAG_REQUESTED 0
193#define FLAG_IS_OUT 1
194#define FLAG_EXPORT 2 /* protected by sysfs_lock */
195#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
196#define FLAG_ACTIVE_LOW 6 /* value has active low */
197#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
198#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
199#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
200#define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
201#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
202#define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
203#define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
204#define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
205#define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */
206#define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */
207#define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */
208#define FLAG_EVENT_CLOCK_REALTIME 18 /* GPIO CDEV reports REALTIME timestamps in events */
209#define FLAG_EVENT_CLOCK_HTE 19 /* GPIO CDEV reports hardware timestamps in events */
210
211 /* Connection label */
212 struct gpio_desc_label __rcu *label;
213 /* Name of the GPIO */
214 const char *name;
215#ifdef CONFIG_OF_DYNAMIC
216 struct device_node *hog;
217#endif
218#ifdef CONFIG_GPIO_CDEV
219 /* debounce period in microseconds */
220 unsigned int debounce_period_us;
221#endif
222};
223
224#define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
225
226struct gpio_chip_guard {
227 struct gpio_device *gdev;
228 struct gpio_chip *gc;
229 int idx;
230};
231
232DEFINE_CLASS(gpio_chip_guard,
233 struct gpio_chip_guard,
234 srcu_read_unlock(&_T.gdev->srcu, _T.idx),
235 ({
236 struct gpio_chip_guard _guard;
237
238 _guard.gdev = desc->gdev;
239 _guard.idx = srcu_read_lock(&_guard.gdev->srcu);
240 _guard.gc = srcu_dereference(_guard.gdev->chip,
241 &_guard.gdev->srcu);
242
243 _guard;
244 }),
245 struct gpio_desc *desc)
246
247int gpiod_request(struct gpio_desc *desc, const char *label);
248void gpiod_free(struct gpio_desc *desc);
249
250static inline int gpiod_request_user(struct gpio_desc *desc, const char *label)
251{
252 int ret;
253
254 ret = gpiod_request(desc, label);
255 if (ret == -EPROBE_DEFER)
256 ret = -ENODEV;
257
258 return ret;
259}
260
261struct gpio_desc *gpiod_find_and_request(struct device *consumer,
262 struct fwnode_handle *fwnode,
263 const char *con_id,
264 unsigned int idx,
265 enum gpiod_flags flags,
266 const char *label,
267 bool platform_lookup_allowed);
268
269int gpio_do_set_config(struct gpio_desc *desc, unsigned long config);
270int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
271 unsigned long lflags, enum gpiod_flags dflags);
272int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce);
273int gpiod_hog(struct gpio_desc *desc, const char *name,
274 unsigned long lflags, enum gpiod_flags dflags);
275int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev);
276struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
277const char *gpiod_get_label(struct gpio_desc *desc);
278
279/*
280 * Return the GPIO number of the passed descriptor relative to its chip
281 */
282static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
283{
284 return desc - &desc->gdev->descs[0];
285}
286
287/* With descriptor prefix */
288
289#define gpiod_err(desc, fmt, ...) \
290do { \
291 scoped_guard(srcu, &desc->gdev->desc_srcu) { \
292 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
293 gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
294 } \
295} while (0)
296
297#define gpiod_warn(desc, fmt, ...) \
298do { \
299 scoped_guard(srcu, &desc->gdev->desc_srcu) { \
300 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
301 gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
302 } \
303} while (0)
304
305#define gpiod_dbg(desc, fmt, ...) \
306do { \
307 scoped_guard(srcu, &desc->gdev->desc_srcu) { \
308 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
309 gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
310 } \
311} while (0)
312
313/* With chip prefix */
314
315#define chip_err(gc, fmt, ...) \
316 dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
317#define chip_warn(gc, fmt, ...) \
318 dev_warn(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
319#define chip_info(gc, fmt, ...) \
320 dev_info(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
321#define chip_dbg(gc, fmt, ...) \
322 dev_dbg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
323
324#endif /* GPIOLIB_H */
325

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of linux/drivers/gpio/gpiolib.h