]> Git Repo - linux.git/blob - drivers/gpio/gpiolib.h
Merge tag 'ti-k3-dt-for-v6.11-part2' into ti-k3-dts-next
[linux.git] / drivers / gpio / gpiolib.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Internal GPIO functions.
4  *
5  * Copyright (C) 2013, Intel Corporation
6  * Author: Mika Westerberg <[email protected]>
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/srcu.h>
20
21 #define GPIOCHIP_NAME   "gpiochip"
22
23 /**
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
29  * NULL)
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
51  *
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
55  * userspace.
56  */
57 struct gpio_device {
58         struct device           dev;
59         struct cdev             chrdev;
60         int                     id;
61         struct device           *mockdev;
62         struct module           *owner;
63         struct gpio_chip __rcu  *chip;
64         struct gpio_desc        *descs;
65         struct srcu_struct      desc_srcu;
66         unsigned int            base;
67         u16                     ngpio;
68         bool                    can_sleep;
69         const char              *label;
70         void                    *data;
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;
75
76 #ifdef CONFIG_PINCTRL
77         /*
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.
82          */
83         struct list_head pin_ranges;
84 #endif
85 };
86
87 static inline struct gpio_device *to_gpio_device(struct device *dev)
88 {
89         return container_of(dev, struct gpio_device, dev);
90 }
91
92 /* gpio suffixes used for ACPI and device tree lookup */
93 extern const char *const gpio_suffixes[];
94 extern const size_t gpio_suffix_count;
95
96 /**
97  * struct gpio_array - Opaque descriptor for a structure of GPIO array attributes
98  *
99  * @desc:               Array of pointers to the GPIO descriptors
100  * @size:               Number of elements in desc
101  * @chip:               Parent GPIO chip
102  * @get_mask:           Get mask used in fastpath
103  * @set_mask:           Set mask used in fastpath
104  * @invert_mask:        Invert mask used in fastpath
105  *
106  * This structure is attached to struct gpiod_descs obtained from
107  * gpiod_get_array() and can be passed back to get/set array functions in order
108  * to activate fast processing path if applicable.
109  */
110 struct gpio_array {
111         struct gpio_desc        **desc;
112         unsigned int            size;
113         struct gpio_chip        *chip;
114         unsigned long           *get_mask;
115         unsigned long           *set_mask;
116         unsigned long           invert_mask[];
117 };
118
119 #define for_each_gpio_desc(gc, desc)                                    \
120         for (unsigned int __i = 0;                                      \
121              __i < gc->ngpio && (desc = gpiochip_get_desc(gc, __i));    \
122              __i++)                                                     \
123
124 #define for_each_gpio_desc_with_flag(gc, desc, flag)                    \
125         for_each_gpio_desc(gc, desc)                                    \
126                 if (!test_bit(flag, &desc->flags)) {} else
127
128 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
129                                   unsigned int array_size,
130                                   struct gpio_desc **desc_array,
131                                   struct gpio_array *array_info,
132                                   unsigned long *value_bitmap);
133 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
134                                   unsigned int array_size,
135                                   struct gpio_desc **desc_array,
136                                   struct gpio_array *array_info,
137                                   unsigned long *value_bitmap);
138
139 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
140
141 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action);
142
143 struct gpio_desc_label {
144         struct rcu_head rh;
145         char str[];
146 };
147
148 /**
149  * struct gpio_desc - Opaque descriptor for a GPIO
150  *
151  * @gdev:               Pointer to the parent GPIO device
152  * @flags:              Binary descriptor flags
153  * @label:              Name of the consumer
154  * @name:               Line name
155  * @hog:                Pointer to the device node that hogs this line (if any)
156  *
157  * These are obtained using gpiod_get() and are preferable to the old
158  * integer-based handles.
159  *
160  * Contrary to integers, a pointer to a &struct gpio_desc is guaranteed to be
161  * valid until the GPIO is released.
162  */
163 struct gpio_desc {
164         struct gpio_device      *gdev;
165         unsigned long           flags;
166 /* flag symbols are bit numbers */
167 #define FLAG_REQUESTED  0
168 #define FLAG_IS_OUT     1
169 #define FLAG_EXPORT     2       /* protected by sysfs_lock */
170 #define FLAG_SYSFS      3       /* exported via /sys/class/gpio/control */
171 #define FLAG_ACTIVE_LOW 6       /* value has active low */
172 #define FLAG_OPEN_DRAIN 7       /* Gpio is open drain type */
173 #define FLAG_OPEN_SOURCE 8      /* Gpio is open source type */
174 #define FLAG_USED_AS_IRQ 9      /* GPIO is connected to an IRQ */
175 #define FLAG_IRQ_IS_ENABLED 10  /* GPIO is connected to an enabled IRQ */
176 #define FLAG_IS_HOGGED  11      /* GPIO is hogged */
177 #define FLAG_TRANSITORY 12      /* GPIO may lose value in sleep or reset */
178 #define FLAG_PULL_UP    13      /* GPIO has pull up enabled */
179 #define FLAG_PULL_DOWN  14      /* GPIO has pull down enabled */
180 #define FLAG_BIAS_DISABLE    15 /* GPIO has pull disabled */
181 #define FLAG_EDGE_RISING     16 /* GPIO CDEV detects rising edge events */
182 #define FLAG_EDGE_FALLING    17 /* GPIO CDEV detects falling edge events */
183 #define FLAG_EVENT_CLOCK_REALTIME       18 /* GPIO CDEV reports REALTIME timestamps in events */
184 #define FLAG_EVENT_CLOCK_HTE            19 /* GPIO CDEV reports hardware timestamps in events */
185
186         /* Connection label */
187         struct gpio_desc_label __rcu *label;
188         /* Name of the GPIO */
189         const char              *name;
190 #ifdef CONFIG_OF_DYNAMIC
191         struct device_node      *hog;
192 #endif
193 };
194
195 #define gpiod_not_found(desc)           (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
196
197 struct gpio_chip_guard {
198         struct gpio_device *gdev;
199         struct gpio_chip *gc;
200         int idx;
201 };
202
203 DEFINE_CLASS(gpio_chip_guard,
204              struct gpio_chip_guard,
205              srcu_read_unlock(&_T.gdev->srcu, _T.idx),
206              ({
207                 struct gpio_chip_guard _guard;
208
209                 _guard.gdev = desc->gdev;
210                 _guard.idx = srcu_read_lock(&_guard.gdev->srcu);
211                 _guard.gc = srcu_dereference(_guard.gdev->chip,
212                                              &_guard.gdev->srcu);
213
214                 _guard;
215              }),
216              struct gpio_desc *desc)
217
218 int gpiod_request(struct gpio_desc *desc, const char *label);
219 void gpiod_free(struct gpio_desc *desc);
220
221 static inline int gpiod_request_user(struct gpio_desc *desc, const char *label)
222 {
223         int ret;
224
225         ret = gpiod_request(desc, label);
226         if (ret == -EPROBE_DEFER)
227                 ret = -ENODEV;
228
229         return ret;
230 }
231
232 struct gpio_desc *gpiod_find_and_request(struct device *consumer,
233                                          struct fwnode_handle *fwnode,
234                                          const char *con_id,
235                                          unsigned int idx,
236                                          enum gpiod_flags flags,
237                                          const char *label,
238                                          bool platform_lookup_allowed);
239
240 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
241                 unsigned long lflags, enum gpiod_flags dflags);
242 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce);
243 int gpiod_hog(struct gpio_desc *desc, const char *name,
244                 unsigned long lflags, enum gpiod_flags dflags);
245 int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev);
246 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
247 const char *gpiod_get_label(struct gpio_desc *desc);
248
249 /*
250  * Return the GPIO number of the passed descriptor relative to its chip
251  */
252 static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
253 {
254         return desc - &desc->gdev->descs[0];
255 }
256
257 /* With descriptor prefix */
258
259 #define gpiod_err(desc, fmt, ...) \
260 do { \
261         scoped_guard(srcu, &desc->gdev->desc_srcu) { \
262                 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
263                        gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
264         } \
265 } while (0)
266
267 #define gpiod_warn(desc, fmt, ...) \
268 do { \
269         scoped_guard(srcu, &desc->gdev->desc_srcu) { \
270                 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
271                         gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
272         } \
273 } while (0)
274
275 #define gpiod_dbg(desc, fmt, ...) \
276 do { \
277         scoped_guard(srcu, &desc->gdev->desc_srcu) { \
278                 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
279                          gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \
280         } \
281 } while (0)
282
283 /* With chip prefix */
284
285 #define chip_err(gc, fmt, ...)                                  \
286         dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
287 #define chip_warn(gc, fmt, ...)                                 \
288         dev_warn(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
289 #define chip_info(gc, fmt, ...)                                 \
290         dev_info(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
291 #define chip_dbg(gc, fmt, ...)                                  \
292         dev_dbg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
293
294 #endif /* GPIOLIB_H */
This page took 0.054245 seconds and 4 git commands to generate.