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