xref: /openbmc/linux/include/linux/gpio.h (revision 380c7ba3)
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 
15eccb7a00SArnd Bergmann #include <linux/types.h>
167563bbf8SMark Brown 
17*380c7ba3SAndy Shevchenko struct device;
18*380c7ba3SAndy Shevchenko 
1960a86668SMauro Carvalho Chehab /* see Documentation/driver-api/gpio/legacy.rst */
207560fa60SDavid Brownell 
21c001fb72SRandy Dunlap /* make these flag values available regardless of GPIO kconfig options */
22c001fb72SRandy Dunlap #define GPIOF_DIR_OUT	(0 << 0)
23c001fb72SRandy Dunlap #define GPIOF_DIR_IN	(1 << 0)
24c001fb72SRandy Dunlap 
25c001fb72SRandy Dunlap #define GPIOF_INIT_LOW	(0 << 1)
26c001fb72SRandy Dunlap #define GPIOF_INIT_HIGH	(1 << 1)
27c001fb72SRandy Dunlap 
28c001fb72SRandy Dunlap #define GPIOF_IN		(GPIOF_DIR_IN)
29c001fb72SRandy Dunlap #define GPIOF_OUT_INIT_LOW	(GPIOF_DIR_OUT | GPIOF_INIT_LOW)
30c001fb72SRandy Dunlap #define GPIOF_OUT_INIT_HIGH	(GPIOF_DIR_OUT | GPIOF_INIT_HIGH)
31c001fb72SRandy Dunlap 
3279a9becdSAlexandre Courbot /* Gpio pin is active-low */
3379a9becdSAlexandre Courbot #define GPIOF_ACTIVE_LOW        (1 << 2)
3479a9becdSAlexandre Courbot 
35aca5ce14SLaxman Dewangan /* Gpio pin is open drain */
3679a9becdSAlexandre Courbot #define GPIOF_OPEN_DRAIN	(1 << 3)
37aca5ce14SLaxman Dewangan 
3825553ff0SLaxman Dewangan /* Gpio pin is open source */
3979a9becdSAlexandre Courbot #define GPIOF_OPEN_SOURCE	(1 << 4)
4025553ff0SLaxman Dewangan 
4179a9becdSAlexandre Courbot #define GPIOF_EXPORT		(1 << 5)
4279a9becdSAlexandre Courbot #define GPIOF_EXPORT_CHANGEABLE	(1 << 6)
43fc3a1f04SWolfram Sang #define GPIOF_EXPORT_DIR_FIXED	(GPIOF_EXPORT)
44fc3a1f04SWolfram Sang #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
45fc3a1f04SWolfram Sang 
46feb83699SMark Brown /**
47feb83699SMark Brown  * struct gpio - a structure describing a GPIO with configuration
48feb83699SMark Brown  * @gpio:	the GPIO number
49feb83699SMark Brown  * @flags:	GPIO configuration as specified by GPIOF_*
50feb83699SMark Brown  * @label:	a literal description string of this GPIO
51feb83699SMark Brown  */
52feb83699SMark Brown struct gpio {
53feb83699SMark Brown 	unsigned	gpio;
54feb83699SMark Brown 	unsigned long	flags;
55feb83699SMark Brown 	const char	*label;
56feb83699SMark Brown };
57feb83699SMark Brown 
5876ec9d18SAlexandre Courbot #ifdef CONFIG_GPIOLIB
597563bbf8SMark Brown 
60eccb7a00SArnd Bergmann #include <linux/gpio/consumer.h>
61eccb7a00SArnd Bergmann 
62eccb7a00SArnd Bergmann /*
63eccb7a00SArnd Bergmann  * "valid" GPIO numbers are nonnegative and may be passed to
64eccb7a00SArnd Bergmann  * setup routines like gpio_request().  Only some valid numbers
65eccb7a00SArnd Bergmann  * can successfully be requested and used.
66eccb7a00SArnd Bergmann  *
67eccb7a00SArnd Bergmann  * Invalid GPIO numbers are useful for indicating no-such-GPIO in
68eccb7a00SArnd Bergmann  * platform data and other tables.
69eccb7a00SArnd Bergmann  */
70eccb7a00SArnd Bergmann static inline bool gpio_is_valid(int number)
717563bbf8SMark Brown {
72eccb7a00SArnd Bergmann 	/* only non-negative numbers are valid */
73eccb7a00SArnd Bergmann 	return number >= 0;
747563bbf8SMark Brown }
757563bbf8SMark Brown 
76eccb7a00SArnd Bergmann /*
77eccb7a00SArnd Bergmann  * Platforms may implement their GPIO interface with library code,
78eccb7a00SArnd Bergmann  * at a small performance cost for non-inlined operations and some
79eccb7a00SArnd Bergmann  * extra memory (for code and for per-GPIO table entries).
80eccb7a00SArnd Bergmann  */
81eccb7a00SArnd Bergmann 
82eccb7a00SArnd Bergmann /*
83eccb7a00SArnd Bergmann  * At the end we want all GPIOs to be dynamically allocated from 0.
84eccb7a00SArnd Bergmann  * However, some legacy drivers still perform fixed allocation.
85eccb7a00SArnd Bergmann  * Until they are all fixed, leave 0-512 space for them.
86eccb7a00SArnd Bergmann  */
87eccb7a00SArnd Bergmann #define GPIO_DYNAMIC_BASE	512
88eccb7a00SArnd Bergmann 
89eccb7a00SArnd Bergmann /* Always use the library code for GPIO management calls,
90eccb7a00SArnd Bergmann  * or when sleeping may be involved.
91eccb7a00SArnd Bergmann  */
92eccb7a00SArnd Bergmann int gpio_request(unsigned gpio, const char *label);
93eccb7a00SArnd Bergmann void gpio_free(unsigned gpio);
94eccb7a00SArnd Bergmann 
95eccb7a00SArnd Bergmann static inline int gpio_direction_input(unsigned gpio)
967563bbf8SMark Brown {
97eccb7a00SArnd Bergmann 	return gpiod_direction_input(gpio_to_desc(gpio));
98eccb7a00SArnd Bergmann }
99eccb7a00SArnd Bergmann static inline int gpio_direction_output(unsigned gpio, int value)
100eccb7a00SArnd Bergmann {
101eccb7a00SArnd Bergmann 	return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
1027563bbf8SMark Brown }
1037563bbf8SMark Brown 
104eccb7a00SArnd Bergmann static inline int gpio_get_value_cansleep(unsigned gpio)
1057563bbf8SMark Brown {
106eccb7a00SArnd Bergmann 	return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
107eccb7a00SArnd Bergmann }
108eccb7a00SArnd Bergmann static inline void gpio_set_value_cansleep(unsigned gpio, int value)
109eccb7a00SArnd Bergmann {
110eccb7a00SArnd Bergmann 	return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
111eccb7a00SArnd Bergmann }
112eccb7a00SArnd Bergmann 
113eccb7a00SArnd Bergmann static inline int gpio_get_value(unsigned gpio)
114eccb7a00SArnd Bergmann {
115eccb7a00SArnd Bergmann 	return gpiod_get_raw_value(gpio_to_desc(gpio));
116eccb7a00SArnd Bergmann }
117eccb7a00SArnd Bergmann static inline void gpio_set_value(unsigned gpio, int value)
118eccb7a00SArnd Bergmann {
119eccb7a00SArnd Bergmann 	return gpiod_set_raw_value(gpio_to_desc(gpio), value);
120eccb7a00SArnd Bergmann }
121eccb7a00SArnd Bergmann 
122eccb7a00SArnd Bergmann static inline int gpio_cansleep(unsigned gpio)
123eccb7a00SArnd Bergmann {
124eccb7a00SArnd Bergmann 	return gpiod_cansleep(gpio_to_desc(gpio));
125eccb7a00SArnd Bergmann }
126eccb7a00SArnd Bergmann 
127eccb7a00SArnd Bergmann static inline int gpio_to_irq(unsigned gpio)
128eccb7a00SArnd Bergmann {
129eccb7a00SArnd Bergmann 	return gpiod_to_irq(gpio_to_desc(gpio));
130eccb7a00SArnd Bergmann }
131eccb7a00SArnd Bergmann 
132eccb7a00SArnd Bergmann int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
133eccb7a00SArnd Bergmann int gpio_request_array(const struct gpio *array, size_t num);
134eccb7a00SArnd Bergmann void gpio_free_array(const struct gpio *array, size_t num);
135eccb7a00SArnd Bergmann 
136403c1d0bSLinus Walleij /* CONFIG_GPIOLIB: bindings for managed devices that want to request gpios */
137403c1d0bSLinus Walleij 
138403c1d0bSLinus Walleij int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
139403c1d0bSLinus Walleij int devm_gpio_request_one(struct device *dev, unsigned gpio,
140403c1d0bSLinus Walleij 			  unsigned long flags, const char *label);
141403c1d0bSLinus Walleij 
14276ec9d18SAlexandre Courbot #else /* ! CONFIG_GPIOLIB */
1437560fa60SDavid Brownell 
1443d599d1cSUwe Kleine-König #include <linux/kernel.h>
1456ea0205bSDavid Brownell 
146*380c7ba3SAndy Shevchenko #include <asm/bug.h>
147*380c7ba3SAndy Shevchenko #include <asm/errno.h>
148a4177ee7SJani Nikula 
1493474cb3cSJoe Perches static inline bool gpio_is_valid(int number)
1507560fa60SDavid Brownell {
1513474cb3cSJoe Perches 	return false;
1527560fa60SDavid Brownell }
1537560fa60SDavid Brownell 
154d8a3515eSLinus Torvalds static inline int gpio_request(unsigned gpio, const char *label)
1557560fa60SDavid Brownell {
1567560fa60SDavid Brownell 	return -ENOSYS;
1577560fa60SDavid Brownell }
1587560fa60SDavid Brownell 
159323b7fe8SWolfram Sang static inline int gpio_request_one(unsigned gpio,
1605f829e40SWolfram Sang 					unsigned long flags, const char *label)
1615f829e40SWolfram Sang {
1625f829e40SWolfram Sang 	return -ENOSYS;
1635f829e40SWolfram Sang }
1645f829e40SWolfram Sang 
1657c295975SLars-Peter Clausen static inline int gpio_request_array(const struct gpio *array, size_t num)
1665f829e40SWolfram Sang {
1675f829e40SWolfram Sang 	return -ENOSYS;
1685f829e40SWolfram Sang }
1695f829e40SWolfram Sang 
1707560fa60SDavid Brownell static inline void gpio_free(unsigned gpio)
1717560fa60SDavid Brownell {
1723d599d1cSUwe Kleine-König 	might_sleep();
1733d599d1cSUwe Kleine-König 
1747560fa60SDavid Brownell 	/* GPIO can never have been requested */
1757560fa60SDavid Brownell 	WARN_ON(1);
1767560fa60SDavid Brownell }
1777560fa60SDavid Brownell 
1787c295975SLars-Peter Clausen static inline void gpio_free_array(const struct gpio *array, size_t num)
1795f829e40SWolfram Sang {
1805f829e40SWolfram Sang 	might_sleep();
1815f829e40SWolfram Sang 
1825f829e40SWolfram Sang 	/* GPIO can never have been requested */
1835f829e40SWolfram Sang 	WARN_ON(1);
1845f829e40SWolfram Sang }
1855f829e40SWolfram Sang 
186d8a3515eSLinus Torvalds static inline int gpio_direction_input(unsigned gpio)
1877560fa60SDavid Brownell {
1887560fa60SDavid Brownell 	return -ENOSYS;
1897560fa60SDavid Brownell }
1907560fa60SDavid Brownell 
191d8a3515eSLinus Torvalds static inline int gpio_direction_output(unsigned gpio, int value)
1927560fa60SDavid Brownell {
1937560fa60SDavid Brownell 	return -ENOSYS;
1947560fa60SDavid Brownell }
1957560fa60SDavid Brownell 
1967560fa60SDavid Brownell static inline int gpio_get_value(unsigned gpio)
1977560fa60SDavid Brownell {
1987560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1997560fa60SDavid Brownell 	WARN_ON(1);
2007560fa60SDavid Brownell 	return 0;
2017560fa60SDavid Brownell }
2027560fa60SDavid Brownell 
2037560fa60SDavid Brownell static inline void gpio_set_value(unsigned gpio, int value)
2047560fa60SDavid Brownell {
2057560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
2067560fa60SDavid Brownell 	WARN_ON(1);
2077560fa60SDavid Brownell }
2087560fa60SDavid Brownell 
2097560fa60SDavid Brownell static inline int gpio_cansleep(unsigned gpio)
2107560fa60SDavid Brownell {
2117560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
2127560fa60SDavid Brownell 	WARN_ON(1);
2137560fa60SDavid Brownell 	return 0;
2147560fa60SDavid Brownell }
2157560fa60SDavid Brownell 
2167560fa60SDavid Brownell static inline int gpio_get_value_cansleep(unsigned gpio)
2177560fa60SDavid Brownell {
2187560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
2197560fa60SDavid Brownell 	WARN_ON(1);
2207560fa60SDavid Brownell 	return 0;
2217560fa60SDavid Brownell }
2227560fa60SDavid Brownell 
2237560fa60SDavid Brownell static inline void gpio_set_value_cansleep(unsigned gpio, int value)
2247560fa60SDavid Brownell {
2257560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
2267560fa60SDavid Brownell 	WARN_ON(1);
2277560fa60SDavid Brownell }
2287560fa60SDavid Brownell 
2297560fa60SDavid Brownell static inline int gpio_to_irq(unsigned gpio)
2307560fa60SDavid Brownell {
2317560fa60SDavid Brownell 	/* GPIO can never have been requested or set as input */
2327560fa60SDavid Brownell 	WARN_ON(1);
2337560fa60SDavid Brownell 	return -EINVAL;
2347560fa60SDavid Brownell }
2357560fa60SDavid Brownell 
236403c1d0bSLinus Walleij static inline int devm_gpio_request(struct device *dev, unsigned gpio,
237403c1d0bSLinus Walleij 				    const char *label)
238403c1d0bSLinus Walleij {
239403c1d0bSLinus Walleij 	WARN_ON(1);
240403c1d0bSLinus Walleij 	return -EINVAL;
241403c1d0bSLinus Walleij }
242403c1d0bSLinus Walleij 
243403c1d0bSLinus Walleij static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
244403c1d0bSLinus Walleij 					unsigned long flags, const char *label)
245403c1d0bSLinus Walleij {
246403c1d0bSLinus Walleij 	WARN_ON(1);
247403c1d0bSLinus Walleij 	return -EINVAL;
248403c1d0bSLinus Walleij }
249403c1d0bSLinus Walleij 
25076ec9d18SAlexandre Courbot #endif /* ! CONFIG_GPIOLIB */
2517560fa60SDavid Brownell 
2527560fa60SDavid Brownell #endif /* __LINUX_GPIO_H */
253