xref: /linux/drivers/gpio/gpiolib.h (revision 1334d2a3b3235d062e5e1f51aebe7a64ed57cf72) !
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  * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
41  * of the @descs array.
42  * @can_sleep: indicate whether the GPIO chip driver's callbacks can sleep
43  * implying that they cannot be used from atomic context
44  * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
45  * at device creation time.
46  * @label: a descriptive name for the GPIO device, such as the part number
47  * or name of the IP component in a System on Chip.
48  * @data: per-instance data assigned by the driver
49  * @list: links gpio_device:s together for traversal
50  * @line_state_notifier: used to notify subscribers about lines being
51  *                       requested, released or reconfigured
52  * @line_state_lock: RW-spinlock protecting the line state notifier
53  * @line_state_wq: used to emit line state events from a separate thread in
54  *                 process context
55  * @device_notifier: used to notify character device wait queues about the GPIO
56  *                   device being unregistered
57  * @srcu: protects the pointer to the underlying GPIO chip
58  * @pin_ranges: range of pins served by the GPIO driver
59  *
60  * This state container holds most of the runtime variable data
61  * for a GPIO device and can hold references and live on after the
62  * GPIO chip has been removed, if it is still being used from
63  * userspace.
64  */
65 struct gpio_device {
66 	struct device		dev;
67 	struct cdev		chrdev;
68 	int			id;
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 
to_gpio_device(struct device * dev)97 static 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 */
103 extern 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 			strscpy(propname, __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  */
132 struct 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 
150 int 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);
155 int 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 
161 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
162 
163 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action);
164 int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value);
165 int gpiod_direction_input_nonotify(struct gpio_desc *desc);
166 
167 struct 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  */
188 struct gpio_desc {
189 	struct gpio_device	*gdev;
190 	unsigned long		flags;
191 /* flag symbols are bit numbers */
192 #define GPIOD_FLAG_REQUESTED		0  /* GPIO is in use */
193 #define GPIOD_FLAG_IS_OUT		1  /* GPIO is in output mode */
194 #define GPIOD_FLAG_EXPORT		2  /* GPIO is exported to user-space */
195 #define GPIOD_FLAG_SYSFS		3  /* GPIO is exported via /sys/class/gpio */
196 #define GPIOD_FLAG_ACTIVE_LOW		6  /* GPIO is active-low */
197 #define GPIOD_FLAG_OPEN_DRAIN		7  /* GPIO is open drain type */
198 #define GPIOD_FLAG_OPEN_SOURCE		8  /* GPIO is open source type */
199 #define GPIOD_FLAG_USED_AS_IRQ		9  /* GPIO is connected to an IRQ */
200 #define GPIOD_FLAG_IRQ_IS_ENABLED	10 /* GPIO is connected to an enabled IRQ */
201 #define GPIOD_FLAG_IS_HOGGED		11 /* GPIO is hogged */
202 #define GPIOD_FLAG_TRANSITORY		12 /* GPIO may lose value in sleep or reset */
203 #define GPIOD_FLAG_PULL_UP		13 /* GPIO has pull up enabled */
204 #define GPIOD_FLAG_PULL_DOWN		14 /* GPIO has pull down enabled */
205 #define GPIOD_FLAG_BIAS_DISABLE		15 /* GPIO has pull disabled */
206 #define GPIOD_FLAG_EDGE_RISING		16 /* GPIO CDEV detects rising edge events */
207 #define GPIOD_FLAG_EDGE_FALLING		17 /* GPIO CDEV detects falling edge events */
208 #define GPIOD_FLAG_EVENT_CLOCK_REALTIME	18 /* GPIO CDEV reports REALTIME timestamps in events */
209 #define GPIOD_FLAG_EVENT_CLOCK_HTE	19 /* GPIO CDEV reports hardware timestamps in events */
210 #define GPIOD_FLAG_SHARED		20 /* GPIO is shared by multiple consumers */
211 #define GPIOD_FLAG_SHARED_PROXY		21 /* GPIO is a virtual proxy to a physically shared pin. */
212 
213 	/* Connection label */
214 	struct gpio_desc_label __rcu *label;
215 	/* Name of the GPIO */
216 	const char		*name;
217 #ifdef CONFIG_OF_DYNAMIC
218 	struct device_node	*hog;
219 #endif
220 #ifdef CONFIG_GPIO_CDEV
221 	/* debounce period in microseconds */
222 	unsigned int		debounce_period_us;
223 #endif
224 };
225 
226 #define gpiod_not_found(desc)		(IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
227 
228 struct gpio_chip_guard {
229 	struct gpio_device *gdev;
230 	struct gpio_chip *gc;
231 	int idx;
232 };
233 
234 DEFINE_CLASS(gpio_chip_guard,
235 	     struct gpio_chip_guard,
236 	     srcu_read_unlock(&_T.gdev->srcu, _T.idx),
237 	     ({
238 		struct gpio_chip_guard _guard;
239 
240 		_guard.gdev = desc->gdev;
241 		_guard.idx = srcu_read_lock(&_guard.gdev->srcu);
242 		_guard.gc = srcu_dereference(_guard.gdev->chip,
243 					     &_guard.gdev->srcu);
244 
245 		_guard;
246 	     }),
247 	     struct gpio_desc *desc)
248 
249 int gpiod_request(struct gpio_desc *desc, const char *label);
250 int gpiod_request_commit(struct gpio_desc *desc, const char *label);
251 void gpiod_free(struct gpio_desc *desc);
252 void gpiod_free_commit(struct gpio_desc *desc);
253 
gpiod_request_user(struct gpio_desc * desc,const char * label)254 static inline int gpiod_request_user(struct gpio_desc *desc, const char *label)
255 {
256 	int ret;
257 
258 	ret = gpiod_request(desc, label);
259 	if (ret == -EPROBE_DEFER)
260 		ret = -ENODEV;
261 
262 	return ret;
263 }
264 
265 struct gpio_desc *gpiod_find_and_request(struct device *consumer,
266 					 struct fwnode_handle *fwnode,
267 					 const char *con_id,
268 					 unsigned int idx,
269 					 enum gpiod_flags flags,
270 					 const char *label,
271 					 bool platform_lookup_allowed);
272 
273 int gpio_do_set_config(struct gpio_desc *desc, unsigned long config);
274 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
275 		unsigned long lflags, enum gpiod_flags dflags);
276 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce);
277 int gpiod_hog(struct gpio_desc *desc, const char *name,
278 		unsigned long lflags, enum gpiod_flags dflags);
279 int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode);
280 int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev);
281 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
282 const char *gpiod_get_label(struct gpio_desc *desc);
283 
284 /* With descriptor prefix */
285 
286 #define __gpiod_pr(level, desc, fmt, ...) \
287 do { \
288 	scoped_guard(srcu, &desc->gdev->desc_srcu) { \
289 		pr_##level("gpio-%d (%s): " fmt, desc_to_gpio(desc), \
290 			   gpiod_get_label(desc) ?: "?", ##__VA_ARGS__); \
291 	} \
292 } while (0)
293 
294 #define gpiod_err(desc, fmt, ...) __gpiod_pr(err, desc, fmt, ##__VA_ARGS__)
295 #define gpiod_warn(desc, fmt, ...) __gpiod_pr(warn, desc, fmt, ##__VA_ARGS__)
296 #define gpiod_dbg(desc, fmt, ...) __gpiod_pr(debug, desc, fmt, ##__VA_ARGS__)
297 
298 /* With chip prefix */
299 
300 #define __gpiochip_pr(level, gc, fmt, ...) \
301 do { \
302 	dev_##level(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__); \
303 } while (0)
304 
305 #define gpiochip_err(gc, fmt, ...) __gpiochip_pr(err, gc, fmt, ##__VA_ARGS__)
306 #define gpiochip_warn(gc, fmt, ...) __gpiochip_pr(warn, gc, fmt, ##__VA_ARGS__)
307 #define gpiochip_info(gc, fmt, ...) __gpiochip_pr(info, gc, fmt, ##__VA_ARGS__)
308 #define gpiochip_dbg(gc, fmt, ...) __gpiochip_pr(dbg, gc, fmt, ##__VA_ARGS__)
309 
310 #endif /* GPIOLIB_H */
311