xref: /linux-6.15/include/linux/gpio.h (revision 8ce258f6)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
256a46b61SLinus Walleij /*
3447e140eSAndy Shevchenko  * NOTE: This header *must not* be included.
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 
17380c7ba3SAndy Shevchenko struct device;
18380c7ba3SAndy Shevchenko 
19c001fb72SRandy Dunlap /* make these flag values available regardless of GPIO kconfig options */
208c045ca5SAndy Shevchenko #define GPIOF_IN		((1 << 0))
218c045ca5SAndy Shevchenko #define GPIOF_OUT_INIT_LOW	((0 << 0) | (0 << 1))
228c045ca5SAndy Shevchenko #define GPIOF_OUT_INIT_HIGH	((0 << 0) | (1 << 1))
23c001fb72SRandy Dunlap 
24feb83699SMark Brown /**
25feb83699SMark Brown  * struct gpio - a structure describing a GPIO with configuration
26feb83699SMark Brown  * @gpio:	the GPIO number
27feb83699SMark Brown  * @flags:	GPIO configuration as specified by GPIOF_*
28feb83699SMark Brown  * @label:	a literal description string of this GPIO
29feb83699SMark Brown  */
30feb83699SMark Brown struct gpio {
31feb83699SMark Brown 	unsigned	gpio;
32feb83699SMark Brown 	unsigned long	flags;
33feb83699SMark Brown 	const char	*label;
34feb83699SMark Brown };
35feb83699SMark Brown 
3676ec9d18SAlexandre Courbot #ifdef CONFIG_GPIOLIB
377563bbf8SMark Brown 
38eccb7a00SArnd Bergmann #include <linux/gpio/consumer.h>
39eccb7a00SArnd Bergmann 
40eccb7a00SArnd Bergmann /*
41eccb7a00SArnd Bergmann  * "valid" GPIO numbers are nonnegative and may be passed to
42eccb7a00SArnd Bergmann  * setup routines like gpio_request().  Only some valid numbers
43eccb7a00SArnd Bergmann  * can successfully be requested and used.
44eccb7a00SArnd Bergmann  *
45eccb7a00SArnd Bergmann  * Invalid GPIO numbers are useful for indicating no-such-GPIO in
46eccb7a00SArnd Bergmann  * platform data and other tables.
47eccb7a00SArnd Bergmann  */
gpio_is_valid(int number)48eccb7a00SArnd Bergmann static inline bool gpio_is_valid(int number)
497563bbf8SMark Brown {
50eccb7a00SArnd Bergmann 	/* only non-negative numbers are valid */
51eccb7a00SArnd Bergmann 	return number >= 0;
527563bbf8SMark Brown }
537563bbf8SMark Brown 
54eccb7a00SArnd Bergmann /*
55eccb7a00SArnd Bergmann  * Platforms may implement their GPIO interface with library code,
56eccb7a00SArnd Bergmann  * at a small performance cost for non-inlined operations and some
57eccb7a00SArnd Bergmann  * extra memory (for code and for per-GPIO table entries).
58eccb7a00SArnd Bergmann  */
59eccb7a00SArnd Bergmann 
60eccb7a00SArnd Bergmann /*
61eccb7a00SArnd Bergmann  * At the end we want all GPIOs to be dynamically allocated from 0.
62eccb7a00SArnd Bergmann  * However, some legacy drivers still perform fixed allocation.
63eccb7a00SArnd Bergmann  * Until they are all fixed, leave 0-512 space for them.
64eccb7a00SArnd Bergmann  */
65eccb7a00SArnd Bergmann #define GPIO_DYNAMIC_BASE	512
668a7a6103SAndy Shevchenko /*
678a7a6103SAndy Shevchenko  * Define the maximum of the possible GPIO in the global numberspace.
688a7a6103SAndy Shevchenko  * While the GPIO base and numbers are positive, we limit it with signed
698a7a6103SAndy Shevchenko  * maximum as a lot of code is using negative values for special cases.
708a7a6103SAndy Shevchenko  */
718a7a6103SAndy Shevchenko #define GPIO_DYNAMIC_MAX	INT_MAX
72eccb7a00SArnd Bergmann 
73eccb7a00SArnd Bergmann /* Always use the library code for GPIO management calls,
74eccb7a00SArnd Bergmann  * or when sleeping may be involved.
75eccb7a00SArnd Bergmann  */
76eccb7a00SArnd Bergmann int gpio_request(unsigned gpio, const char *label);
77eccb7a00SArnd Bergmann void gpio_free(unsigned gpio);
78eccb7a00SArnd Bergmann 
gpio_direction_input(unsigned gpio)79eccb7a00SArnd Bergmann static inline int gpio_direction_input(unsigned gpio)
807563bbf8SMark Brown {
81eccb7a00SArnd Bergmann 	return gpiod_direction_input(gpio_to_desc(gpio));
82eccb7a00SArnd Bergmann }
gpio_direction_output(unsigned gpio,int value)83eccb7a00SArnd Bergmann static inline int gpio_direction_output(unsigned gpio, int value)
84eccb7a00SArnd Bergmann {
85eccb7a00SArnd Bergmann 	return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
867563bbf8SMark Brown }
877563bbf8SMark Brown 
gpio_get_value_cansleep(unsigned gpio)88eccb7a00SArnd Bergmann static inline int gpio_get_value_cansleep(unsigned gpio)
897563bbf8SMark Brown {
90eccb7a00SArnd Bergmann 	return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
91eccb7a00SArnd Bergmann }
gpio_set_value_cansleep(unsigned gpio,int value)92eccb7a00SArnd Bergmann static inline void gpio_set_value_cansleep(unsigned gpio, int value)
93eccb7a00SArnd Bergmann {
94*8ce258f6SBartosz Golaszewski 	gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
95eccb7a00SArnd Bergmann }
96eccb7a00SArnd Bergmann 
gpio_get_value(unsigned gpio)97eccb7a00SArnd Bergmann static inline int gpio_get_value(unsigned gpio)
98eccb7a00SArnd Bergmann {
99eccb7a00SArnd Bergmann 	return gpiod_get_raw_value(gpio_to_desc(gpio));
100eccb7a00SArnd Bergmann }
gpio_set_value(unsigned gpio,int value)101eccb7a00SArnd Bergmann static inline void gpio_set_value(unsigned gpio, int value)
102eccb7a00SArnd Bergmann {
103*8ce258f6SBartosz Golaszewski 	gpiod_set_raw_value(gpio_to_desc(gpio), value);
104eccb7a00SArnd Bergmann }
105eccb7a00SArnd Bergmann 
gpio_to_irq(unsigned gpio)106eccb7a00SArnd Bergmann static inline int gpio_to_irq(unsigned gpio)
107eccb7a00SArnd Bergmann {
108eccb7a00SArnd Bergmann 	return gpiod_to_irq(gpio_to_desc(gpio));
109eccb7a00SArnd Bergmann }
110eccb7a00SArnd Bergmann 
111eccb7a00SArnd Bergmann int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
112eccb7a00SArnd Bergmann 
113403c1d0bSLinus Walleij int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
114403c1d0bSLinus Walleij int devm_gpio_request_one(struct device *dev, unsigned gpio,
115403c1d0bSLinus Walleij 			  unsigned long flags, const char *label);
116403c1d0bSLinus Walleij 
11776ec9d18SAlexandre Courbot #else /* ! CONFIG_GPIOLIB */
1187560fa60SDavid Brownell 
1193d599d1cSUwe Kleine-König #include <linux/kernel.h>
1206ea0205bSDavid Brownell 
121380c7ba3SAndy Shevchenko #include <asm/bug.h>
122380c7ba3SAndy Shevchenko #include <asm/errno.h>
123a4177ee7SJani Nikula 
gpio_is_valid(int number)1243474cb3cSJoe Perches static inline bool gpio_is_valid(int number)
1257560fa60SDavid Brownell {
1263474cb3cSJoe Perches 	return false;
1277560fa60SDavid Brownell }
1287560fa60SDavid Brownell 
gpio_request(unsigned gpio,const char * label)129d8a3515eSLinus Torvalds static inline int gpio_request(unsigned gpio, const char *label)
1307560fa60SDavid Brownell {
1317560fa60SDavid Brownell 	return -ENOSYS;
1327560fa60SDavid Brownell }
1337560fa60SDavid Brownell 
gpio_request_one(unsigned gpio,unsigned long flags,const char * label)134323b7fe8SWolfram Sang static inline int gpio_request_one(unsigned gpio,
1355f829e40SWolfram Sang 					unsigned long flags, const char *label)
1365f829e40SWolfram Sang {
1375f829e40SWolfram Sang 	return -ENOSYS;
1385f829e40SWolfram Sang }
1395f829e40SWolfram Sang 
gpio_free(unsigned gpio)1407560fa60SDavid Brownell static inline void gpio_free(unsigned gpio)
1417560fa60SDavid Brownell {
1423d599d1cSUwe Kleine-König 	might_sleep();
1433d599d1cSUwe Kleine-König 
1447560fa60SDavid Brownell 	/* GPIO can never have been requested */
1457560fa60SDavid Brownell 	WARN_ON(1);
1467560fa60SDavid Brownell }
1477560fa60SDavid Brownell 
gpio_direction_input(unsigned gpio)148d8a3515eSLinus Torvalds static inline int gpio_direction_input(unsigned gpio)
1497560fa60SDavid Brownell {
1507560fa60SDavid Brownell 	return -ENOSYS;
1517560fa60SDavid Brownell }
1527560fa60SDavid Brownell 
gpio_direction_output(unsigned gpio,int value)153d8a3515eSLinus Torvalds static inline int gpio_direction_output(unsigned gpio, int value)
1547560fa60SDavid Brownell {
1557560fa60SDavid Brownell 	return -ENOSYS;
1567560fa60SDavid Brownell }
1577560fa60SDavid Brownell 
gpio_get_value(unsigned gpio)1587560fa60SDavid Brownell static inline int gpio_get_value(unsigned gpio)
1597560fa60SDavid Brownell {
1607560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1617560fa60SDavid Brownell 	WARN_ON(1);
1627560fa60SDavid Brownell 	return 0;
1637560fa60SDavid Brownell }
1647560fa60SDavid Brownell 
gpio_set_value(unsigned gpio,int value)1657560fa60SDavid Brownell static inline void gpio_set_value(unsigned gpio, int value)
1667560fa60SDavid Brownell {
1677560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
1687560fa60SDavid Brownell 	WARN_ON(1);
1697560fa60SDavid Brownell }
1707560fa60SDavid Brownell 
gpio_get_value_cansleep(unsigned gpio)1717560fa60SDavid Brownell static inline int gpio_get_value_cansleep(unsigned gpio)
1727560fa60SDavid Brownell {
1737560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1747560fa60SDavid Brownell 	WARN_ON(1);
1757560fa60SDavid Brownell 	return 0;
1767560fa60SDavid Brownell }
1777560fa60SDavid Brownell 
gpio_set_value_cansleep(unsigned gpio,int value)1787560fa60SDavid Brownell static inline void gpio_set_value_cansleep(unsigned gpio, int value)
1797560fa60SDavid Brownell {
1807560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
1817560fa60SDavid Brownell 	WARN_ON(1);
1827560fa60SDavid Brownell }
1837560fa60SDavid Brownell 
gpio_to_irq(unsigned gpio)1847560fa60SDavid Brownell static inline int gpio_to_irq(unsigned gpio)
1857560fa60SDavid Brownell {
1867560fa60SDavid Brownell 	/* GPIO can never have been requested or set as input */
1877560fa60SDavid Brownell 	WARN_ON(1);
1887560fa60SDavid Brownell 	return -EINVAL;
1897560fa60SDavid Brownell }
1907560fa60SDavid Brownell 
devm_gpio_request(struct device * dev,unsigned gpio,const char * label)191403c1d0bSLinus Walleij static inline int devm_gpio_request(struct device *dev, unsigned gpio,
192403c1d0bSLinus Walleij 				    const char *label)
193403c1d0bSLinus Walleij {
194403c1d0bSLinus Walleij 	WARN_ON(1);
195403c1d0bSLinus Walleij 	return -EINVAL;
196403c1d0bSLinus Walleij }
197403c1d0bSLinus Walleij 
devm_gpio_request_one(struct device * dev,unsigned gpio,unsigned long flags,const char * label)198403c1d0bSLinus Walleij static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
199403c1d0bSLinus Walleij 					unsigned long flags, const char *label)
200403c1d0bSLinus Walleij {
201403c1d0bSLinus Walleij 	WARN_ON(1);
202403c1d0bSLinus Walleij 	return -EINVAL;
203403c1d0bSLinus Walleij }
204403c1d0bSLinus Walleij 
20576ec9d18SAlexandre Courbot #endif /* ! CONFIG_GPIOLIB */
2067560fa60SDavid Brownell 
2077560fa60SDavid Brownell #endif /* __LINUX_GPIO_H */
208