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/rwsem.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  * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
35  * of the @descs array.
36  * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
37  * at device creation time.
38  * @label: a descriptive name for the GPIO device, such as the part number
39  * or name of the IP component in a System on Chip.
40  * @data: per-instance data assigned by the driver
41  * @list: links gpio_device:s together for traversal
42  * @line_state_notifier: used to notify subscribers about lines being
43  *                       requested, released or reconfigured
44  * @device_notifier: used to notify character device wait queues about the GPIO
45  *                   device being unregistered
46  * @sem: protects the structure from a NULL-pointer dereference of @chip by
47  *       user-space operations when the device gets unregistered during
48  *       a hot-unplug event
49  * @pin_ranges: range of pins served by the GPIO driver
50  *
51  * This state container holds most of the runtime variable data
52  * for a GPIO device and can hold references and live on after the
53  * GPIO chip has been removed, if it is still being used from
54  * userspace.
55  */
56 struct gpio_device {
57 	struct device		dev;
58 	struct cdev		chrdev;
59 	int			id;
60 	struct device		*mockdev;
61 	struct module		*owner;
62 	struct gpio_chip	*chip;
63 	struct gpio_desc	*descs;
64 	int			base;
65 	u16			ngpio;
66 	const char		*label;
67 	void			*data;
68 	struct list_head        list;
69 	struct blocking_notifier_head line_state_notifier;
70 	struct blocking_notifier_head device_notifier;
71 	struct rw_semaphore	sem;
72 
73 #ifdef CONFIG_PINCTRL
74 	/*
75 	 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
76 	 * describe the actual pin range which they serve in an SoC. This
77 	 * information would be used by pinctrl subsystem to configure
78 	 * corresponding pins for gpio usage.
79 	 */
80 	struct list_head pin_ranges;
81 #endif
82 };
83 
to_gpio_device(struct device * dev)84 static inline struct gpio_device *to_gpio_device(struct device *dev)
85 {
86 	return container_of(dev, struct gpio_device, dev);
87 }
88 
89 /* gpio suffixes used for ACPI and device tree lookup */
90 static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
91 
92 /**
93  * struct gpio_array - Opaque descriptor for a structure of GPIO array attributes
94  *
95  * @desc:		Array of pointers to the GPIO descriptors
96  * @size:		Number of elements in desc
97  * @chip:		Parent GPIO chip
98  * @get_mask:		Get mask used in fastpath
99  * @set_mask:		Set mask used in fastpath
100  * @invert_mask:	Invert mask used in fastpath
101  *
102  * This structure is attached to struct gpiod_descs obtained from
103  * gpiod_get_array() and can be passed back to get/set array functions in order
104  * to activate fast processing path if applicable.
105  */
106 struct gpio_array {
107 	struct gpio_desc	**desc;
108 	unsigned int		size;
109 	struct gpio_chip	*chip;
110 	unsigned long		*get_mask;
111 	unsigned long		*set_mask;
112 	unsigned long		invert_mask[];
113 };
114 
115 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
116 
117 #define for_each_gpio_desc(gc, desc)					\
118 	for (unsigned int __i = 0;					\
119 	     __i < gc->ngpio && (desc = gpiochip_get_desc(gc, __i));	\
120 	     __i++)							\
121 
122 #define for_each_gpio_desc_with_flag(gc, desc, flag)			\
123 	for_each_gpio_desc(gc, desc)					\
124 		if (!test_bit(flag, &desc->flags)) {} else
125 
126 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
127 				  unsigned int array_size,
128 				  struct gpio_desc **desc_array,
129 				  struct gpio_array *array_info,
130 				  unsigned long *value_bitmap);
131 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
132 				  unsigned int array_size,
133 				  struct gpio_desc **desc_array,
134 				  struct gpio_array *array_info,
135 				  unsigned long *value_bitmap);
136 
137 extern spinlock_t gpio_lock;
138 extern struct list_head gpio_devices;
139 
140 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action);
141 
142 /**
143  * struct gpio_desc - Opaque descriptor for a GPIO
144  *
145  * @gdev:		Pointer to the parent GPIO device
146  * @flags:		Binary descriptor flags
147  * @label:		Name of the consumer
148  * @name:		Line name
149  * @hog:		Pointer to the device node that hogs this line (if any)
150  * @debounce_period_us:	Debounce period in microseconds
151  *
152  * These are obtained using gpiod_get() and are preferable to the old
153  * integer-based handles.
154  *
155  * Contrary to integers, a pointer to a &struct gpio_desc is guaranteed to be
156  * valid until the GPIO is released.
157  */
158 struct gpio_desc {
159 	struct gpio_device	*gdev;
160 	unsigned long		flags;
161 /* flag symbols are bit numbers */
162 #define FLAG_REQUESTED	0
163 #define FLAG_IS_OUT	1
164 #define FLAG_EXPORT	2	/* protected by sysfs_lock */
165 #define FLAG_SYSFS	3	/* exported via /sys/class/gpio/control */
166 #define FLAG_ACTIVE_LOW	6	/* value has active low */
167 #define FLAG_OPEN_DRAIN	7	/* Gpio is open drain type */
168 #define FLAG_OPEN_SOURCE 8	/* Gpio is open source type */
169 #define FLAG_USED_AS_IRQ 9	/* GPIO is connected to an IRQ */
170 #define FLAG_IRQ_IS_ENABLED 10	/* GPIO is connected to an enabled IRQ */
171 #define FLAG_IS_HOGGED	11	/* GPIO is hogged */
172 #define FLAG_TRANSITORY 12	/* GPIO may lose value in sleep or reset */
173 #define FLAG_PULL_UP    13	/* GPIO has pull up enabled */
174 #define FLAG_PULL_DOWN  14	/* GPIO has pull down enabled */
175 #define FLAG_BIAS_DISABLE    15	/* GPIO has pull disabled */
176 #define FLAG_EDGE_RISING     16	/* GPIO CDEV detects rising edge events */
177 #define FLAG_EDGE_FALLING    17	/* GPIO CDEV detects falling edge events */
178 #define FLAG_EVENT_CLOCK_REALTIME	18 /* GPIO CDEV reports REALTIME timestamps in events */
179 #define FLAG_EVENT_CLOCK_HTE		19 /* GPIO CDEV reports hardware timestamps in events */
180 
181 	/* Connection label */
182 	const char		*label;
183 	/* Name of the GPIO */
184 	const char		*name;
185 #ifdef CONFIG_OF_DYNAMIC
186 	struct device_node	*hog;
187 #endif
188 #ifdef CONFIG_GPIO_CDEV
189 	/* debounce period in microseconds */
190 	unsigned int		debounce_period_us;
191 #endif
192 };
193 
194 #define gpiod_not_found(desc)		(IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
195 
196 int gpiod_request(struct gpio_desc *desc, const char *label);
197 void gpiod_free(struct gpio_desc *desc);
198 
gpiod_request_user(struct gpio_desc * desc,const char * label)199 static inline int gpiod_request_user(struct gpio_desc *desc, const char *label)
200 {
201 	int ret;
202 
203 	ret = gpiod_request(desc, label);
204 	if (ret == -EPROBE_DEFER)
205 		ret = -ENODEV;
206 
207 	return ret;
208 }
209 
210 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
211 		unsigned long lflags, enum gpiod_flags dflags);
212 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce);
213 int gpiod_hog(struct gpio_desc *desc, const char *name,
214 		unsigned long lflags, enum gpiod_flags dflags);
215 int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev);
216 
217 /*
218  * Return the GPIO number of the passed descriptor relative to its chip
219  */
gpio_chip_hwgpio(const struct gpio_desc * desc)220 static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
221 {
222 	return desc - &desc->gdev->descs[0];
223 }
224 
225 /* With descriptor prefix */
226 
227 #define gpiod_emerg(desc, fmt, ...)					       \
228 	pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
229 		 ##__VA_ARGS__)
230 #define gpiod_crit(desc, fmt, ...)					       \
231 	pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
232 		 ##__VA_ARGS__)
233 #define gpiod_err(desc, fmt, ...)					       \
234 	pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",  \
235 		 ##__VA_ARGS__)
236 #define gpiod_warn(desc, fmt, ...)					       \
237 	pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
238 		 ##__VA_ARGS__)
239 #define gpiod_info(desc, fmt, ...)					       \
240 	pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
241 		 ##__VA_ARGS__)
242 #define gpiod_dbg(desc, fmt, ...)					       \
243 	pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
244 		 ##__VA_ARGS__)
245 
246 /* With chip prefix */
247 
248 #define chip_emerg(gc, fmt, ...)					\
249 	dev_emerg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
250 #define chip_crit(gc, fmt, ...)					\
251 	dev_crit(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
252 #define chip_err(gc, fmt, ...)					\
253 	dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
254 #define chip_warn(gc, fmt, ...)					\
255 	dev_warn(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
256 #define chip_info(gc, fmt, ...)					\
257 	dev_info(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
258 #define chip_dbg(gc, fmt, ...)					\
259 	dev_dbg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
260 
261 #endif /* GPIOLIB_H */
262