xref: /openbmc/linux/include/linux/gpio.h (revision eccb7a00)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
256a46b61SLinus Walleij /*
356a46b61SLinus Walleij  * <linux/gpio.h>
456a46b61SLinus Walleij  *
556a46b61SLinus Walleij  * This is the LEGACY GPIO bulk include file, including legacy APIs. It is
656a46b61SLinus Walleij  * used for GPIO drivers still referencing the global GPIO numberspace,
756a46b61SLinus Walleij  * and should not be included in new code.
856a46b61SLinus Walleij  *
956a46b61SLinus Walleij  * If you're implementing a GPIO driver, only include <linux/gpio/driver.h>
1056a46b61SLinus Walleij  * If you're implementing a GPIO consumer, only include <linux/gpio/consumer.h>
1156a46b61SLinus Walleij  */
127560fa60SDavid Brownell #ifndef __LINUX_GPIO_H
137560fa60SDavid Brownell #define __LINUX_GPIO_H
147560fa60SDavid Brownell 
157563bbf8SMark Brown #include <linux/errno.h>
16*eccb7a00SArnd Bergmann #include <linux/types.h>
177563bbf8SMark Brown 
1860a86668SMauro Carvalho Chehab /* see Documentation/driver-api/gpio/legacy.rst */
197560fa60SDavid Brownell 
20c001fb72SRandy Dunlap /* make these flag values available regardless of GPIO kconfig options */
21c001fb72SRandy Dunlap #define GPIOF_DIR_OUT	(0 << 0)
22c001fb72SRandy Dunlap #define GPIOF_DIR_IN	(1 << 0)
23c001fb72SRandy Dunlap 
24c001fb72SRandy Dunlap #define GPIOF_INIT_LOW	(0 << 1)
25c001fb72SRandy Dunlap #define GPIOF_INIT_HIGH	(1 << 1)
26c001fb72SRandy Dunlap 
27c001fb72SRandy Dunlap #define GPIOF_IN		(GPIOF_DIR_IN)
28c001fb72SRandy Dunlap #define GPIOF_OUT_INIT_LOW	(GPIOF_DIR_OUT | GPIOF_INIT_LOW)
29c001fb72SRandy Dunlap #define GPIOF_OUT_INIT_HIGH	(GPIOF_DIR_OUT | GPIOF_INIT_HIGH)
30c001fb72SRandy Dunlap 
3179a9becdSAlexandre Courbot /* Gpio pin is active-low */
3279a9becdSAlexandre Courbot #define GPIOF_ACTIVE_LOW        (1 << 2)
3379a9becdSAlexandre Courbot 
34aca5ce14SLaxman Dewangan /* Gpio pin is open drain */
3579a9becdSAlexandre Courbot #define GPIOF_OPEN_DRAIN	(1 << 3)
36aca5ce14SLaxman Dewangan 
3725553ff0SLaxman Dewangan /* Gpio pin is open source */
3879a9becdSAlexandre Courbot #define GPIOF_OPEN_SOURCE	(1 << 4)
3925553ff0SLaxman Dewangan 
4079a9becdSAlexandre Courbot #define GPIOF_EXPORT		(1 << 5)
4179a9becdSAlexandre Courbot #define GPIOF_EXPORT_CHANGEABLE	(1 << 6)
42fc3a1f04SWolfram Sang #define GPIOF_EXPORT_DIR_FIXED	(GPIOF_EXPORT)
43fc3a1f04SWolfram Sang #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
44fc3a1f04SWolfram Sang 
45feb83699SMark Brown /**
46feb83699SMark Brown  * struct gpio - a structure describing a GPIO with configuration
47feb83699SMark Brown  * @gpio:	the GPIO number
48feb83699SMark Brown  * @flags:	GPIO configuration as specified by GPIOF_*
49feb83699SMark Brown  * @label:	a literal description string of this GPIO
50feb83699SMark Brown  */
51feb83699SMark Brown struct gpio {
52feb83699SMark Brown 	unsigned	gpio;
53feb83699SMark Brown 	unsigned long	flags;
54feb83699SMark Brown 	const char	*label;
55feb83699SMark Brown };
56feb83699SMark Brown 
5776ec9d18SAlexandre Courbot #ifdef CONFIG_GPIOLIB
587563bbf8SMark Brown 
59*eccb7a00SArnd Bergmann #include <linux/gpio/consumer.h>
60*eccb7a00SArnd Bergmann 
61*eccb7a00SArnd Bergmann /*
62*eccb7a00SArnd Bergmann  * "valid" GPIO numbers are nonnegative and may be passed to
63*eccb7a00SArnd Bergmann  * setup routines like gpio_request().  Only some valid numbers
64*eccb7a00SArnd Bergmann  * can successfully be requested and used.
65*eccb7a00SArnd Bergmann  *
66*eccb7a00SArnd Bergmann  * Invalid GPIO numbers are useful for indicating no-such-GPIO in
67*eccb7a00SArnd Bergmann  * platform data and other tables.
68*eccb7a00SArnd Bergmann  */
69*eccb7a00SArnd Bergmann static inline bool gpio_is_valid(int number)
707563bbf8SMark Brown {
71*eccb7a00SArnd Bergmann 	/* only non-negative numbers are valid */
72*eccb7a00SArnd Bergmann 	return number >= 0;
737563bbf8SMark Brown }
747563bbf8SMark Brown 
75*eccb7a00SArnd Bergmann /*
76*eccb7a00SArnd Bergmann  * Platforms may implement their GPIO interface with library code,
77*eccb7a00SArnd Bergmann  * at a small performance cost for non-inlined operations and some
78*eccb7a00SArnd Bergmann  * extra memory (for code and for per-GPIO table entries).
79*eccb7a00SArnd Bergmann  */
80*eccb7a00SArnd Bergmann 
81*eccb7a00SArnd Bergmann /*
82*eccb7a00SArnd Bergmann  * At the end we want all GPIOs to be dynamically allocated from 0.
83*eccb7a00SArnd Bergmann  * However, some legacy drivers still perform fixed allocation.
84*eccb7a00SArnd Bergmann  * Until they are all fixed, leave 0-512 space for them.
85*eccb7a00SArnd Bergmann  */
86*eccb7a00SArnd Bergmann #define GPIO_DYNAMIC_BASE	512
87*eccb7a00SArnd Bergmann 
88*eccb7a00SArnd Bergmann /* Always use the library code for GPIO management calls,
89*eccb7a00SArnd Bergmann  * or when sleeping may be involved.
90*eccb7a00SArnd Bergmann  */
91*eccb7a00SArnd Bergmann int gpio_request(unsigned gpio, const char *label);
92*eccb7a00SArnd Bergmann void gpio_free(unsigned gpio);
93*eccb7a00SArnd Bergmann 
94*eccb7a00SArnd Bergmann static inline int gpio_direction_input(unsigned gpio)
957563bbf8SMark Brown {
96*eccb7a00SArnd Bergmann 	return gpiod_direction_input(gpio_to_desc(gpio));
97*eccb7a00SArnd Bergmann }
98*eccb7a00SArnd Bergmann static inline int gpio_direction_output(unsigned gpio, int value)
99*eccb7a00SArnd Bergmann {
100*eccb7a00SArnd Bergmann 	return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
1017563bbf8SMark Brown }
1027563bbf8SMark Brown 
103*eccb7a00SArnd Bergmann static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
1047563bbf8SMark Brown {
105*eccb7a00SArnd Bergmann 	return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
1067563bbf8SMark Brown }
1077563bbf8SMark Brown 
108*eccb7a00SArnd Bergmann static inline int gpio_get_value_cansleep(unsigned gpio)
1097563bbf8SMark Brown {
110*eccb7a00SArnd Bergmann 	return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
111*eccb7a00SArnd Bergmann }
112*eccb7a00SArnd Bergmann static inline void gpio_set_value_cansleep(unsigned gpio, int value)
113*eccb7a00SArnd Bergmann {
114*eccb7a00SArnd Bergmann 	return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
115*eccb7a00SArnd Bergmann }
116*eccb7a00SArnd Bergmann 
117*eccb7a00SArnd Bergmann static inline int gpio_get_value(unsigned gpio)
118*eccb7a00SArnd Bergmann {
119*eccb7a00SArnd Bergmann 	return gpiod_get_raw_value(gpio_to_desc(gpio));
120*eccb7a00SArnd Bergmann }
121*eccb7a00SArnd Bergmann static inline void gpio_set_value(unsigned gpio, int value)
122*eccb7a00SArnd Bergmann {
123*eccb7a00SArnd Bergmann 	return gpiod_set_raw_value(gpio_to_desc(gpio), value);
124*eccb7a00SArnd Bergmann }
125*eccb7a00SArnd Bergmann 
126*eccb7a00SArnd Bergmann static inline int gpio_cansleep(unsigned gpio)
127*eccb7a00SArnd Bergmann {
128*eccb7a00SArnd Bergmann 	return gpiod_cansleep(gpio_to_desc(gpio));
129*eccb7a00SArnd Bergmann }
130*eccb7a00SArnd Bergmann 
131*eccb7a00SArnd Bergmann static inline int gpio_to_irq(unsigned gpio)
132*eccb7a00SArnd Bergmann {
133*eccb7a00SArnd Bergmann 	return gpiod_to_irq(gpio_to_desc(gpio));
134*eccb7a00SArnd Bergmann }
135*eccb7a00SArnd Bergmann 
136*eccb7a00SArnd Bergmann int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
137*eccb7a00SArnd Bergmann int gpio_request_array(const struct gpio *array, size_t num);
138*eccb7a00SArnd Bergmann void gpio_free_array(const struct gpio *array, size_t num);
139*eccb7a00SArnd Bergmann 
140*eccb7a00SArnd Bergmann /*
141*eccb7a00SArnd Bergmann  * A sysfs interface can be exported by individual drivers if they want,
142*eccb7a00SArnd Bergmann  * but more typically is configured entirely from userspace.
143*eccb7a00SArnd Bergmann  */
144*eccb7a00SArnd Bergmann static inline int gpio_export(unsigned gpio, bool direction_may_change)
145*eccb7a00SArnd Bergmann {
146*eccb7a00SArnd Bergmann 	return gpiod_export(gpio_to_desc(gpio), direction_may_change);
147*eccb7a00SArnd Bergmann }
148*eccb7a00SArnd Bergmann 
149*eccb7a00SArnd Bergmann static inline void gpio_unexport(unsigned gpio)
150*eccb7a00SArnd Bergmann {
151*eccb7a00SArnd Bergmann 	gpiod_unexport(gpio_to_desc(gpio));
1527563bbf8SMark Brown }
1537563bbf8SMark Brown 
154403c1d0bSLinus Walleij /* CONFIG_GPIOLIB: bindings for managed devices that want to request gpios */
155403c1d0bSLinus Walleij 
156403c1d0bSLinus Walleij struct device;
157403c1d0bSLinus Walleij 
158403c1d0bSLinus Walleij int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
159403c1d0bSLinus Walleij int devm_gpio_request_one(struct device *dev, unsigned gpio,
160403c1d0bSLinus Walleij 			  unsigned long flags, const char *label);
161403c1d0bSLinus Walleij 
16276ec9d18SAlexandre Courbot #else /* ! CONFIG_GPIOLIB */
1637560fa60SDavid Brownell 
16408a149c4SAndy Shevchenko #include <linux/bug.h>
1653d599d1cSUwe Kleine-König #include <linux/kernel.h>
1666ea0205bSDavid Brownell 
167a4177ee7SJani Nikula struct device;
1684e4438b8SAnton Vorontsov struct gpio_chip;
169a4177ee7SJani Nikula 
1703474cb3cSJoe Perches static inline bool gpio_is_valid(int number)
1717560fa60SDavid Brownell {
1723474cb3cSJoe Perches 	return false;
1737560fa60SDavid Brownell }
1747560fa60SDavid Brownell 
175d8a3515eSLinus Torvalds static inline int gpio_request(unsigned gpio, const char *label)
1767560fa60SDavid Brownell {
1777560fa60SDavid Brownell 	return -ENOSYS;
1787560fa60SDavid Brownell }
1797560fa60SDavid Brownell 
180323b7fe8SWolfram Sang static inline int gpio_request_one(unsigned gpio,
1815f829e40SWolfram Sang 					unsigned long flags, const char *label)
1825f829e40SWolfram Sang {
1835f829e40SWolfram Sang 	return -ENOSYS;
1845f829e40SWolfram Sang }
1855f829e40SWolfram Sang 
1867c295975SLars-Peter Clausen static inline int gpio_request_array(const struct gpio *array, size_t num)
1875f829e40SWolfram Sang {
1885f829e40SWolfram Sang 	return -ENOSYS;
1895f829e40SWolfram Sang }
1905f829e40SWolfram Sang 
1917560fa60SDavid Brownell static inline void gpio_free(unsigned gpio)
1927560fa60SDavid Brownell {
1933d599d1cSUwe Kleine-König 	might_sleep();
1943d599d1cSUwe Kleine-König 
1957560fa60SDavid Brownell 	/* GPIO can never have been requested */
1967560fa60SDavid Brownell 	WARN_ON(1);
1977560fa60SDavid Brownell }
1987560fa60SDavid Brownell 
1997c295975SLars-Peter Clausen static inline void gpio_free_array(const struct gpio *array, size_t num)
2005f829e40SWolfram Sang {
2015f829e40SWolfram Sang 	might_sleep();
2025f829e40SWolfram Sang 
2035f829e40SWolfram Sang 	/* GPIO can never have been requested */
2045f829e40SWolfram Sang 	WARN_ON(1);
2055f829e40SWolfram Sang }
2065f829e40SWolfram Sang 
207d8a3515eSLinus Torvalds static inline int gpio_direction_input(unsigned gpio)
2087560fa60SDavid Brownell {
2097560fa60SDavid Brownell 	return -ENOSYS;
2107560fa60SDavid Brownell }
2117560fa60SDavid Brownell 
212d8a3515eSLinus Torvalds static inline int gpio_direction_output(unsigned gpio, int value)
2137560fa60SDavid Brownell {
2147560fa60SDavid Brownell 	return -ENOSYS;
2157560fa60SDavid Brownell }
2167560fa60SDavid Brownell 
217c4b5be98SFelipe Balbi static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
218c4b5be98SFelipe Balbi {
219c4b5be98SFelipe Balbi 	return -ENOSYS;
220c4b5be98SFelipe Balbi }
221c4b5be98SFelipe Balbi 
2227560fa60SDavid Brownell static inline int gpio_get_value(unsigned gpio)
2237560fa60SDavid Brownell {
2247560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
2257560fa60SDavid Brownell 	WARN_ON(1);
2267560fa60SDavid Brownell 	return 0;
2277560fa60SDavid Brownell }
2287560fa60SDavid Brownell 
2297560fa60SDavid Brownell static inline void gpio_set_value(unsigned gpio, int value)
2307560fa60SDavid Brownell {
2317560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
2327560fa60SDavid Brownell 	WARN_ON(1);
2337560fa60SDavid Brownell }
2347560fa60SDavid Brownell 
2357560fa60SDavid Brownell static inline int gpio_cansleep(unsigned gpio)
2367560fa60SDavid Brownell {
2377560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
2387560fa60SDavid Brownell 	WARN_ON(1);
2397560fa60SDavid Brownell 	return 0;
2407560fa60SDavid Brownell }
2417560fa60SDavid Brownell 
2427560fa60SDavid Brownell static inline int gpio_get_value_cansleep(unsigned gpio)
2437560fa60SDavid Brownell {
2447560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
2457560fa60SDavid Brownell 	WARN_ON(1);
2467560fa60SDavid Brownell 	return 0;
2477560fa60SDavid Brownell }
2487560fa60SDavid Brownell 
2497560fa60SDavid Brownell static inline void gpio_set_value_cansleep(unsigned gpio, int value)
2507560fa60SDavid Brownell {
2517560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
2527560fa60SDavid Brownell 	WARN_ON(1);
2537560fa60SDavid Brownell }
2547560fa60SDavid Brownell 
255d8f388d8SDavid Brownell static inline int gpio_export(unsigned gpio, bool direction_may_change)
256d8f388d8SDavid Brownell {
257d8f388d8SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
258d8f388d8SDavid Brownell 	WARN_ON(1);
259d8f388d8SDavid Brownell 	return -EINVAL;
260d8f388d8SDavid Brownell }
261d8f388d8SDavid Brownell 
262d8f388d8SDavid Brownell static inline void gpio_unexport(unsigned gpio)
263d8f388d8SDavid Brownell {
264d8f388d8SDavid Brownell 	/* GPIO can never have been exported */
265d8f388d8SDavid Brownell 	WARN_ON(1);
266d8f388d8SDavid Brownell }
267d8f388d8SDavid Brownell 
2687560fa60SDavid Brownell static inline int gpio_to_irq(unsigned gpio)
2697560fa60SDavid Brownell {
2707560fa60SDavid Brownell 	/* GPIO can never have been requested or set as input */
2717560fa60SDavid Brownell 	WARN_ON(1);
2727560fa60SDavid Brownell 	return -EINVAL;
2737560fa60SDavid Brownell }
2747560fa60SDavid Brownell 
275403c1d0bSLinus Walleij static inline int devm_gpio_request(struct device *dev, unsigned gpio,
276403c1d0bSLinus Walleij 				    const char *label)
277403c1d0bSLinus Walleij {
278403c1d0bSLinus Walleij 	WARN_ON(1);
279403c1d0bSLinus Walleij 	return -EINVAL;
280403c1d0bSLinus Walleij }
281403c1d0bSLinus Walleij 
282403c1d0bSLinus Walleij static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
283403c1d0bSLinus Walleij 					unsigned long flags, const char *label)
284403c1d0bSLinus Walleij {
285403c1d0bSLinus Walleij 	WARN_ON(1);
286403c1d0bSLinus Walleij 	return -EINVAL;
287403c1d0bSLinus Walleij }
288403c1d0bSLinus Walleij 
28976ec9d18SAlexandre Courbot #endif /* ! CONFIG_GPIOLIB */
2907560fa60SDavid Brownell 
2917560fa60SDavid Brownell #endif /* __LINUX_GPIO_H */
292