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