Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / drivers / gpio / gpiolib.h
CommitLineData
dae5f0af 1/* SPDX-License-Identifier: GPL-2.0 */
664e3e5a
MW
2/*
3 * Internal GPIO functions.
4 *
5 * Copyright (C) 2013, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
664e3e5a
MW
7 */
8
9#ifndef GPIOLIB_H
10#define GPIOLIB_H
11
0f93a345 12#include <linux/cdev.h>
5ccff852 13#include <linux/device.h>
0f93a345
BG
14#include <linux/err.h>
15#include <linux/gpio/consumer.h> /* for enum gpiod_flags */
16#include <linux/gpio/driver.h>
ff2b1359 17#include <linux/module.h>
e2051394 18#include <linux/notifier.h>
dcb73cba 19#include <linux/spinlock.h>
be711caa 20#include <linux/srcu.h>
7b9b77a8 21#include <linux/workqueue.h>
5ccff852 22
ddd8891e
GU
23#define GPIOCHIP_NAME "gpiochip"
24
ff2b1359
LW
25/**
26 * struct gpio_device - internal state container for GPIO devices
ff2b1359 27 * @dev: the GPIO device struct
3c702e99 28 * @chrdev: character device for the GPIO device
3b7c7478 29 * @id: numerical ID number for the GPIO chip
afbc4f31
LW
30 * @mockdev: class device used by the deprecated sysfs interface (may be
31 * NULL)
ff2b1359
LW
32 * @owner: helps prevent removal of modules exporting active GPIOs
33 * @chip: pointer to the corresponding gpiochip, holding static
34 * data for this device
1c3cdb18 35 * @descs: array of ngpio descriptors.
8015443e
MV
36 * @valid_mask: If not %NULL, holds bitmask of GPIOs which are valid to be
37 * used from the chip.
7765ffed 38 * @desc_srcu: ensures consistent state of GPIO descriptors exposed to users
fdeb8e15
LW
39 * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
40 * of the @descs array.
8a5b477b
BG
41 * @can_sleep: indicate whether the GPIO chip driver's callbacks can sleep
42 * implying that they cannot be used from atomic context
fdeb8e15
LW
43 * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
44 * at device creation time.
df4878e9
LW
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.
43c54eca 47 * @data: per-instance data assigned by the driver
ff2b1359 48 * @list: links gpio_device:s together for traversal
17a7ca35
BG
49 * @line_state_notifier: used to notify subscribers about lines being
50 * requested, released or reconfigured
dcb73cba 51 * @line_state_lock: RW-spinlock protecting the line state notifier
7b9b77a8
BG
52 * @line_state_wq: used to emit line state events from a separate thread in
53 * process context
a067419b
BG
54 * @device_notifier: used to notify character device wait queues about the GPIO
55 * device being unregistered
47d8b4c1 56 * @srcu: protects the pointer to the underlying GPIO chip
4398693a 57 * @pin_ranges: range of pins served by the GPIO driver
ff2b1359
LW
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 {
ff2b1359 65 struct device dev;
3c702e99 66 struct cdev chrdev;
3b7c7478 67 int id;
afbc4f31 68 struct device *mockdev;
ff2b1359 69 struct module *owner;
d83cee3d 70 struct gpio_chip __rcu *chip;
1c3cdb18 71 struct gpio_desc *descs;
8015443e 72 unsigned long *valid_mask;
7765ffed 73 struct srcu_struct desc_srcu;
8a7a6103 74 unsigned int base;
fdeb8e15 75 u16 ngpio;
8a5b477b 76 bool can_sleep;
3b469b0a 77 const char *label;
43c54eca 78 void *data;
ff2b1359 79 struct list_head list;
dcb73cba
BG
80 struct raw_notifier_head line_state_notifier;
81 rwlock_t line_state_lock;
7b9b77a8 82 struct workqueue_struct *line_state_wq;
a067419b 83 struct blocking_notifier_head device_notifier;
47d8b4c1 84 struct srcu_struct srcu;
20ec3e39
LW
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
ff2b1359
LW
95};
96
3b7c7478
AS
97static inline struct gpio_device *to_gpio_device(struct device *dev)
98{
99 return container_of(dev, struct gpio_device, dev);
100}
101
4b91188d 102/* GPIO suffixes used for ACPI and device tree lookup */
7e92061f 103extern const char *const gpio_suffixes[];
7f2e553a 104
ef3d4b94 105#define for_each_gpio_property_name(propname, con_id) \
4b91188d
AS
106 for (const char * const *__suffixes = gpio_suffixes; \
107 *__suffixes && ({ \
108 const char *__gs = *__suffixes; \
ef3d4b94
AS
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 }); \
4b91188d 116 __suffixes++)
ef3d4b94 117
4398693a
BG
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
81570d6a 123 * @gdev: Parent GPIO device
4398693a
BG
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 */
bf9346f5
JK
132struct gpio_array {
133 struct gpio_desc **desc;
134 unsigned int size;
81570d6a 135 struct gpio_device *gdev;
bf9346f5
JK
136 unsigned long *get_mask;
137 unsigned long *set_mask;
138 unsigned long invert_mask[];
139};
140
66f46e37 141#define for_each_gpio_desc(gc, desc) \
57017edd
AS
142 for (unsigned int __i = 0; \
143 __i < gc->ngpio && (desc = gpiochip_get_desc(gc, __i)); \
144 __i++) \
66f46e37
AS
145
146#define for_each_gpio_desc_with_flag(gc, desc, flag) \
147 for_each_gpio_desc(gc, desc) \
80c78fbe
AS
148 if (!test_bit(flag, &desc->flags)) {} else
149
eec1d566
LW
150int gpiod_get_array_value_complex(bool raw, bool can_sleep,
151 unsigned int array_size,
152 struct gpio_desc **desc_array,
77588c14 153 struct gpio_array *array_info,
b9762beb 154 unsigned long *value_bitmap);
3027743f 155int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3c940660
GU
156 unsigned int array_size,
157 struct gpio_desc **desc_array,
158 struct gpio_array *array_info,
159 unsigned long *value_bitmap);
1bd6b601 160
f42dafe3
BG
161int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
162
9ce4ed5b 163void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action);
07c61d4d
BG
164int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value);
165int gpiod_direction_input_nonotify(struct gpio_desc *desc);
4398693a 166
a86d2769
BG
167struct gpio_desc_label {
168 struct rcu_head rh;
169 char str[];
170};
171
4398693a
BG
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)
81625f36 180 * @debounce_period_us: Debounce period in microseconds
4398693a
BG
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 */
0eb4c6c2 188struct gpio_desc {
fdeb8e15 189 struct gpio_device *gdev;
0eb4c6c2
AC
190 unsigned long flags;
191/* flag symbols are bit numbers */
a45faa2a
AS
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 */
0eb4c6c2 210
c0017ed7 211 /* Connection label */
a86d2769 212 struct gpio_desc_label __rcu *label;
c0017ed7
MP
213 /* Name of the GPIO */
214 const char *name;
63636d95
GU
215#ifdef CONFIG_OF_DYNAMIC
216 struct device_node *hog;
217#endif
81625f36
BG
218#ifdef CONFIG_GPIO_CDEV
219 /* debounce period in microseconds */
220 unsigned int debounce_period_us;
221#endif
0eb4c6c2
AC
222};
223
7b58696d
AS
224#define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
225
d83cee3d
BG
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);
d82b9e08
BG
240 _guard.gc = srcu_dereference(_guard.gdev->chip,
241 &_guard.gdev->srcu);
d83cee3d
BG
242
243 _guard;
244 }),
245 struct gpio_desc *desc)
246
0eb4c6c2
AC
247int gpiod_request(struct gpio_desc *desc, const char *label);
248void gpiod_free(struct gpio_desc *desc);
95a4eed7
AS
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
0d776cfd
SB
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
07c61d4d 269int gpio_do_set_config(struct gpio_desc *desc, unsigned long config);
c29fd9eb
AS
270int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
271 unsigned long lflags, enum gpiod_flags dflags);
f725edd8 272int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce);
f625d460
BP
273int gpiod_hog(struct gpio_desc *desc, const char *name,
274 unsigned long lflags, enum gpiod_flags dflags);
55b2395e 275int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev);
6f2a8750 276struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
d23dc4a9 277const char *gpiod_get_label(struct gpio_desc *desc);
0eb4c6c2
AC
278
279/*
280 * Return the GPIO number of the passed descriptor relative to its chip
281 */
4a5c886e 282static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
0eb4c6c2 283{
fdeb8e15 284 return desc - &desc->gdev->descs[0];
0eb4c6c2
AC
285}
286
287/* With descriptor prefix */
288
1f2bcb8c
BG
289#define gpiod_err(desc, fmt, ...) \
290do { \
7765ffed 291 scoped_guard(srcu, &desc->gdev->desc_srcu) { \
1f2bcb8c
BG
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 { \
7765ffed 299 scoped_guard(srcu, &desc->gdev->desc_srcu) { \
1f2bcb8c
BG
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 { \
7765ffed 307 scoped_guard(srcu, &desc->gdev->desc_srcu) { \
1f2bcb8c
BG
308 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
309 gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
310 } \
311} while (0)
0eb4c6c2
AC
312
313/* With chip prefix */
314
a5e93436
GU
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__)
0eb4c6c2 323
664e3e5a 324#endif /* GPIOLIB_H */