xref: /openbmc/linux/include/linux/gpio.h (revision 7d0b8064)
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 
35feb83699SMark Brown /**
36feb83699SMark Brown  * struct gpio - a structure describing a GPIO with configuration
37feb83699SMark Brown  * @gpio:	the GPIO number
38feb83699SMark Brown  * @flags:	GPIO configuration as specified by GPIOF_*
39feb83699SMark Brown  * @label:	a literal description string of this GPIO
40feb83699SMark Brown  */
41feb83699SMark Brown struct gpio {
42feb83699SMark Brown 	unsigned	gpio;
43feb83699SMark Brown 	unsigned long	flags;
44feb83699SMark Brown 	const char	*label;
45feb83699SMark Brown };
46feb83699SMark Brown 
4776ec9d18SAlexandre Courbot #ifdef CONFIG_GPIOLIB
487563bbf8SMark Brown 
49eccb7a00SArnd Bergmann #include <linux/gpio/consumer.h>
50eccb7a00SArnd Bergmann 
51eccb7a00SArnd Bergmann /*
52eccb7a00SArnd Bergmann  * "valid" GPIO numbers are nonnegative and may be passed to
53eccb7a00SArnd Bergmann  * setup routines like gpio_request().  Only some valid numbers
54eccb7a00SArnd Bergmann  * can successfully be requested and used.
55eccb7a00SArnd Bergmann  *
56eccb7a00SArnd Bergmann  * Invalid GPIO numbers are useful for indicating no-such-GPIO in
57eccb7a00SArnd Bergmann  * platform data and other tables.
58eccb7a00SArnd Bergmann  */
59eccb7a00SArnd Bergmann static inline bool gpio_is_valid(int number)
607563bbf8SMark Brown {
61eccb7a00SArnd Bergmann 	/* only non-negative numbers are valid */
62eccb7a00SArnd Bergmann 	return number >= 0;
637563bbf8SMark Brown }
647563bbf8SMark Brown 
65eccb7a00SArnd Bergmann /*
66eccb7a00SArnd Bergmann  * Platforms may implement their GPIO interface with library code,
67eccb7a00SArnd Bergmann  * at a small performance cost for non-inlined operations and some
68eccb7a00SArnd Bergmann  * extra memory (for code and for per-GPIO table entries).
69eccb7a00SArnd Bergmann  */
70eccb7a00SArnd Bergmann 
71eccb7a00SArnd Bergmann /*
72eccb7a00SArnd Bergmann  * At the end we want all GPIOs to be dynamically allocated from 0.
73eccb7a00SArnd Bergmann  * However, some legacy drivers still perform fixed allocation.
74eccb7a00SArnd Bergmann  * Until they are all fixed, leave 0-512 space for them.
75eccb7a00SArnd Bergmann  */
76eccb7a00SArnd Bergmann #define GPIO_DYNAMIC_BASE	512
77eccb7a00SArnd Bergmann 
78eccb7a00SArnd Bergmann /* Always use the library code for GPIO management calls,
79eccb7a00SArnd Bergmann  * or when sleeping may be involved.
80eccb7a00SArnd Bergmann  */
81eccb7a00SArnd Bergmann int gpio_request(unsigned gpio, const char *label);
82eccb7a00SArnd Bergmann void gpio_free(unsigned gpio);
83eccb7a00SArnd Bergmann 
84eccb7a00SArnd Bergmann static inline int gpio_direction_input(unsigned gpio)
857563bbf8SMark Brown {
86eccb7a00SArnd Bergmann 	return gpiod_direction_input(gpio_to_desc(gpio));
87eccb7a00SArnd Bergmann }
88eccb7a00SArnd Bergmann static inline int gpio_direction_output(unsigned gpio, int value)
89eccb7a00SArnd Bergmann {
90eccb7a00SArnd Bergmann 	return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
917563bbf8SMark Brown }
927563bbf8SMark Brown 
93eccb7a00SArnd Bergmann static inline int gpio_get_value_cansleep(unsigned gpio)
947563bbf8SMark Brown {
95eccb7a00SArnd Bergmann 	return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
96eccb7a00SArnd Bergmann }
97eccb7a00SArnd Bergmann static inline void gpio_set_value_cansleep(unsigned gpio, int value)
98eccb7a00SArnd Bergmann {
99eccb7a00SArnd Bergmann 	return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
100eccb7a00SArnd Bergmann }
101eccb7a00SArnd Bergmann 
102eccb7a00SArnd Bergmann static inline int gpio_get_value(unsigned gpio)
103eccb7a00SArnd Bergmann {
104eccb7a00SArnd Bergmann 	return gpiod_get_raw_value(gpio_to_desc(gpio));
105eccb7a00SArnd Bergmann }
106eccb7a00SArnd Bergmann static inline void gpio_set_value(unsigned gpio, int value)
107eccb7a00SArnd Bergmann {
108eccb7a00SArnd Bergmann 	return gpiod_set_raw_value(gpio_to_desc(gpio), value);
109eccb7a00SArnd Bergmann }
110eccb7a00SArnd Bergmann 
111eccb7a00SArnd Bergmann static inline int gpio_to_irq(unsigned gpio)
112eccb7a00SArnd Bergmann {
113eccb7a00SArnd Bergmann 	return gpiod_to_irq(gpio_to_desc(gpio));
114eccb7a00SArnd Bergmann }
115eccb7a00SArnd Bergmann 
116eccb7a00SArnd Bergmann int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
117eccb7a00SArnd Bergmann int gpio_request_array(const struct gpio *array, size_t num);
118eccb7a00SArnd Bergmann void gpio_free_array(const struct gpio *array, size_t num);
119eccb7a00SArnd Bergmann 
120403c1d0bSLinus Walleij /* CONFIG_GPIOLIB: bindings for managed devices that want to request gpios */
121403c1d0bSLinus Walleij 
122403c1d0bSLinus Walleij int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
123403c1d0bSLinus Walleij int devm_gpio_request_one(struct device *dev, unsigned gpio,
124403c1d0bSLinus Walleij 			  unsigned long flags, const char *label);
125403c1d0bSLinus Walleij 
12676ec9d18SAlexandre Courbot #else /* ! CONFIG_GPIOLIB */
1277560fa60SDavid Brownell 
1283d599d1cSUwe Kleine-König #include <linux/kernel.h>
1296ea0205bSDavid Brownell 
130*380c7ba3SAndy Shevchenko #include <asm/bug.h>
131*380c7ba3SAndy Shevchenko #include <asm/errno.h>
132a4177ee7SJani Nikula 
1333474cb3cSJoe Perches static inline bool gpio_is_valid(int number)
1347560fa60SDavid Brownell {
1353474cb3cSJoe Perches 	return false;
1367560fa60SDavid Brownell }
1377560fa60SDavid Brownell 
138d8a3515eSLinus Torvalds static inline int gpio_request(unsigned gpio, const char *label)
1397560fa60SDavid Brownell {
1407560fa60SDavid Brownell 	return -ENOSYS;
1417560fa60SDavid Brownell }
1427560fa60SDavid Brownell 
143323b7fe8SWolfram Sang static inline int gpio_request_one(unsigned gpio,
1445f829e40SWolfram Sang 					unsigned long flags, const char *label)
1455f829e40SWolfram Sang {
1465f829e40SWolfram Sang 	return -ENOSYS;
1475f829e40SWolfram Sang }
1485f829e40SWolfram Sang 
1497c295975SLars-Peter Clausen static inline int gpio_request_array(const struct gpio *array, size_t num)
1505f829e40SWolfram Sang {
1515f829e40SWolfram Sang 	return -ENOSYS;
1525f829e40SWolfram Sang }
1535f829e40SWolfram Sang 
1547560fa60SDavid Brownell static inline void gpio_free(unsigned gpio)
1557560fa60SDavid Brownell {
1563d599d1cSUwe Kleine-König 	might_sleep();
1573d599d1cSUwe Kleine-König 
1587560fa60SDavid Brownell 	/* GPIO can never have been requested */
1597560fa60SDavid Brownell 	WARN_ON(1);
1607560fa60SDavid Brownell }
1617560fa60SDavid Brownell 
1627c295975SLars-Peter Clausen static inline void gpio_free_array(const struct gpio *array, size_t num)
1635f829e40SWolfram Sang {
1645f829e40SWolfram Sang 	might_sleep();
1655f829e40SWolfram Sang 
1665f829e40SWolfram Sang 	/* GPIO can never have been requested */
1675f829e40SWolfram Sang 	WARN_ON(1);
1685f829e40SWolfram Sang }
1695f829e40SWolfram Sang 
170d8a3515eSLinus Torvalds static inline int gpio_direction_input(unsigned gpio)
1717560fa60SDavid Brownell {
1727560fa60SDavid Brownell 	return -ENOSYS;
1737560fa60SDavid Brownell }
1747560fa60SDavid Brownell 
175d8a3515eSLinus Torvalds static inline int gpio_direction_output(unsigned gpio, int value)
1767560fa60SDavid Brownell {
1777560fa60SDavid Brownell 	return -ENOSYS;
1787560fa60SDavid Brownell }
1797560fa60SDavid Brownell 
1807560fa60SDavid Brownell static inline int gpio_get_value(unsigned gpio)
1817560fa60SDavid Brownell {
1827560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1837560fa60SDavid Brownell 	WARN_ON(1);
1847560fa60SDavid Brownell 	return 0;
1857560fa60SDavid Brownell }
1867560fa60SDavid Brownell 
1877560fa60SDavid Brownell static inline void gpio_set_value(unsigned gpio, int value)
1887560fa60SDavid Brownell {
1897560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
1907560fa60SDavid Brownell 	WARN_ON(1);
1917560fa60SDavid Brownell }
1927560fa60SDavid Brownell 
1937560fa60SDavid Brownell static inline int gpio_get_value_cansleep(unsigned gpio)
1947560fa60SDavid Brownell {
1957560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1967560fa60SDavid Brownell 	WARN_ON(1);
1977560fa60SDavid Brownell 	return 0;
1987560fa60SDavid Brownell }
1997560fa60SDavid Brownell 
2007560fa60SDavid Brownell static inline void gpio_set_value_cansleep(unsigned gpio, int value)
2017560fa60SDavid Brownell {
2027560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
2037560fa60SDavid Brownell 	WARN_ON(1);
2047560fa60SDavid Brownell }
2057560fa60SDavid Brownell 
2067560fa60SDavid Brownell static inline int gpio_to_irq(unsigned gpio)
2077560fa60SDavid Brownell {
2087560fa60SDavid Brownell 	/* GPIO can never have been requested or set as input */
2097560fa60SDavid Brownell 	WARN_ON(1);
2107560fa60SDavid Brownell 	return -EINVAL;
2117560fa60SDavid Brownell }
2127560fa60SDavid Brownell 
213403c1d0bSLinus Walleij static inline int devm_gpio_request(struct device *dev, unsigned gpio,
214403c1d0bSLinus Walleij 				    const char *label)
215403c1d0bSLinus Walleij {
216403c1d0bSLinus Walleij 	WARN_ON(1);
217403c1d0bSLinus Walleij 	return -EINVAL;
218403c1d0bSLinus Walleij }
219403c1d0bSLinus Walleij 
220403c1d0bSLinus Walleij static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
221403c1d0bSLinus Walleij 					unsigned long flags, const char *label)
222403c1d0bSLinus Walleij {
223403c1d0bSLinus Walleij 	WARN_ON(1);
224403c1d0bSLinus Walleij 	return -EINVAL;
225403c1d0bSLinus Walleij }
226403c1d0bSLinus Walleij 
22776ec9d18SAlexandre Courbot #endif /* ! CONFIG_GPIOLIB */
2287560fa60SDavid Brownell 
2297560fa60SDavid Brownell #endif /* __LINUX_GPIO_H */
230