xref: /openbmc/linux/include/linux/gpio.h (revision 08a149c4)
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>
167563bbf8SMark Brown 
1760a86668SMauro Carvalho Chehab /* see Documentation/driver-api/gpio/legacy.rst */
187560fa60SDavid Brownell 
19c001fb72SRandy Dunlap /* make these flag values available regardless of GPIO kconfig options */
20c001fb72SRandy Dunlap #define GPIOF_DIR_OUT	(0 << 0)
21c001fb72SRandy Dunlap #define GPIOF_DIR_IN	(1 << 0)
22c001fb72SRandy Dunlap 
23c001fb72SRandy Dunlap #define GPIOF_INIT_LOW	(0 << 1)
24c001fb72SRandy Dunlap #define GPIOF_INIT_HIGH	(1 << 1)
25c001fb72SRandy Dunlap 
26c001fb72SRandy Dunlap #define GPIOF_IN		(GPIOF_DIR_IN)
27c001fb72SRandy Dunlap #define GPIOF_OUT_INIT_LOW	(GPIOF_DIR_OUT | GPIOF_INIT_LOW)
28c001fb72SRandy Dunlap #define GPIOF_OUT_INIT_HIGH	(GPIOF_DIR_OUT | GPIOF_INIT_HIGH)
29c001fb72SRandy Dunlap 
3079a9becdSAlexandre Courbot /* Gpio pin is active-low */
3179a9becdSAlexandre Courbot #define GPIOF_ACTIVE_LOW        (1 << 2)
3279a9becdSAlexandre Courbot 
33aca5ce14SLaxman Dewangan /* Gpio pin is open drain */
3479a9becdSAlexandre Courbot #define GPIOF_OPEN_DRAIN	(1 << 3)
35aca5ce14SLaxman Dewangan 
3625553ff0SLaxman Dewangan /* Gpio pin is open source */
3779a9becdSAlexandre Courbot #define GPIOF_OPEN_SOURCE	(1 << 4)
3825553ff0SLaxman Dewangan 
3979a9becdSAlexandre Courbot #define GPIOF_EXPORT		(1 << 5)
4079a9becdSAlexandre Courbot #define GPIOF_EXPORT_CHANGEABLE	(1 << 6)
41fc3a1f04SWolfram Sang #define GPIOF_EXPORT_DIR_FIXED	(GPIOF_EXPORT)
42fc3a1f04SWolfram Sang #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
43fc3a1f04SWolfram Sang 
44feb83699SMark Brown /**
45feb83699SMark Brown  * struct gpio - a structure describing a GPIO with configuration
46feb83699SMark Brown  * @gpio:	the GPIO number
47feb83699SMark Brown  * @flags:	GPIO configuration as specified by GPIOF_*
48feb83699SMark Brown  * @label:	a literal description string of this GPIO
49feb83699SMark Brown  */
50feb83699SMark Brown struct gpio {
51feb83699SMark Brown 	unsigned	gpio;
52feb83699SMark Brown 	unsigned long	flags;
53feb83699SMark Brown 	const char	*label;
54feb83699SMark Brown };
55feb83699SMark Brown 
5676ec9d18SAlexandre Courbot #ifdef CONFIG_GPIOLIB
577563bbf8SMark Brown 
587563bbf8SMark Brown #ifdef CONFIG_ARCH_HAVE_CUSTOM_GPIO_H
597560fa60SDavid Brownell #include <asm/gpio.h>
607563bbf8SMark Brown #else
617563bbf8SMark Brown 
627563bbf8SMark Brown #include <asm-generic/gpio.h>
637563bbf8SMark Brown 
647563bbf8SMark Brown static inline int gpio_get_value(unsigned int gpio)
657563bbf8SMark Brown {
667563bbf8SMark Brown 	return __gpio_get_value(gpio);
677563bbf8SMark Brown }
687563bbf8SMark Brown 
697563bbf8SMark Brown static inline void gpio_set_value(unsigned int gpio, int value)
707563bbf8SMark Brown {
717563bbf8SMark Brown 	__gpio_set_value(gpio, value);
727563bbf8SMark Brown }
737563bbf8SMark Brown 
747563bbf8SMark Brown static inline int gpio_cansleep(unsigned int gpio)
757563bbf8SMark Brown {
767563bbf8SMark Brown 	return __gpio_cansleep(gpio);
777563bbf8SMark Brown }
787563bbf8SMark Brown 
797563bbf8SMark Brown static inline int gpio_to_irq(unsigned int gpio)
807563bbf8SMark Brown {
817563bbf8SMark Brown 	return __gpio_to_irq(gpio);
827563bbf8SMark Brown }
837563bbf8SMark Brown 
847563bbf8SMark Brown static inline int irq_to_gpio(unsigned int irq)
857563bbf8SMark Brown {
867563bbf8SMark Brown 	return -EINVAL;
877563bbf8SMark Brown }
887563bbf8SMark Brown 
89165adc9cSLinus Walleij #endif /* ! CONFIG_ARCH_HAVE_CUSTOM_GPIO_H */
907560fa60SDavid Brownell 
91403c1d0bSLinus Walleij /* CONFIG_GPIOLIB: bindings for managed devices that want to request gpios */
92403c1d0bSLinus Walleij 
93403c1d0bSLinus Walleij struct device;
94403c1d0bSLinus Walleij 
95403c1d0bSLinus Walleij int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
96403c1d0bSLinus Walleij int devm_gpio_request_one(struct device *dev, unsigned gpio,
97403c1d0bSLinus Walleij 			  unsigned long flags, const char *label);
98403c1d0bSLinus Walleij 
9976ec9d18SAlexandre Courbot #else /* ! CONFIG_GPIOLIB */
1007560fa60SDavid Brownell 
101*08a149c4SAndy Shevchenko #include <linux/bug.h>
1023d599d1cSUwe Kleine-König #include <linux/kernel.h>
1036ea0205bSDavid Brownell #include <linux/types.h>
1046ea0205bSDavid Brownell 
105a4177ee7SJani Nikula struct device;
1064e4438b8SAnton Vorontsov struct gpio_chip;
107a4177ee7SJani Nikula 
1083474cb3cSJoe Perches static inline bool gpio_is_valid(int number)
1097560fa60SDavid Brownell {
1103474cb3cSJoe Perches 	return false;
1117560fa60SDavid Brownell }
1127560fa60SDavid Brownell 
113d8a3515eSLinus Torvalds static inline int gpio_request(unsigned gpio, const char *label)
1147560fa60SDavid Brownell {
1157560fa60SDavid Brownell 	return -ENOSYS;
1167560fa60SDavid Brownell }
1177560fa60SDavid Brownell 
118323b7fe8SWolfram Sang static inline int gpio_request_one(unsigned gpio,
1195f829e40SWolfram Sang 					unsigned long flags, const char *label)
1205f829e40SWolfram Sang {
1215f829e40SWolfram Sang 	return -ENOSYS;
1225f829e40SWolfram Sang }
1235f829e40SWolfram Sang 
1247c295975SLars-Peter Clausen static inline int gpio_request_array(const struct gpio *array, size_t num)
1255f829e40SWolfram Sang {
1265f829e40SWolfram Sang 	return -ENOSYS;
1275f829e40SWolfram Sang }
1285f829e40SWolfram Sang 
1297560fa60SDavid Brownell static inline void gpio_free(unsigned gpio)
1307560fa60SDavid Brownell {
1313d599d1cSUwe Kleine-König 	might_sleep();
1323d599d1cSUwe Kleine-König 
1337560fa60SDavid Brownell 	/* GPIO can never have been requested */
1347560fa60SDavid Brownell 	WARN_ON(1);
1357560fa60SDavid Brownell }
1367560fa60SDavid Brownell 
1377c295975SLars-Peter Clausen static inline void gpio_free_array(const struct gpio *array, size_t num)
1385f829e40SWolfram Sang {
1395f829e40SWolfram Sang 	might_sleep();
1405f829e40SWolfram Sang 
1415f829e40SWolfram Sang 	/* GPIO can never have been requested */
1425f829e40SWolfram Sang 	WARN_ON(1);
1435f829e40SWolfram Sang }
1445f829e40SWolfram Sang 
145d8a3515eSLinus Torvalds static inline int gpio_direction_input(unsigned gpio)
1467560fa60SDavid Brownell {
1477560fa60SDavid Brownell 	return -ENOSYS;
1487560fa60SDavid Brownell }
1497560fa60SDavid Brownell 
150d8a3515eSLinus Torvalds static inline int gpio_direction_output(unsigned gpio, int value)
1517560fa60SDavid Brownell {
1527560fa60SDavid Brownell 	return -ENOSYS;
1537560fa60SDavid Brownell }
1547560fa60SDavid Brownell 
155c4b5be98SFelipe Balbi static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
156c4b5be98SFelipe Balbi {
157c4b5be98SFelipe Balbi 	return -ENOSYS;
158c4b5be98SFelipe Balbi }
159c4b5be98SFelipe Balbi 
1607560fa60SDavid Brownell static inline int gpio_get_value(unsigned gpio)
1617560fa60SDavid Brownell {
1627560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1637560fa60SDavid Brownell 	WARN_ON(1);
1647560fa60SDavid Brownell 	return 0;
1657560fa60SDavid Brownell }
1667560fa60SDavid Brownell 
1677560fa60SDavid Brownell static inline void gpio_set_value(unsigned gpio, int value)
1687560fa60SDavid Brownell {
1697560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
1707560fa60SDavid Brownell 	WARN_ON(1);
1717560fa60SDavid Brownell }
1727560fa60SDavid Brownell 
1737560fa60SDavid Brownell static inline int gpio_cansleep(unsigned gpio)
1747560fa60SDavid Brownell {
1757560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1767560fa60SDavid Brownell 	WARN_ON(1);
1777560fa60SDavid Brownell 	return 0;
1787560fa60SDavid Brownell }
1797560fa60SDavid Brownell 
1807560fa60SDavid Brownell static inline int gpio_get_value_cansleep(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_cansleep(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 
193d8f388d8SDavid Brownell static inline int gpio_export(unsigned gpio, bool direction_may_change)
194d8f388d8SDavid Brownell {
195d8f388d8SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
196d8f388d8SDavid Brownell 	WARN_ON(1);
197d8f388d8SDavid Brownell 	return -EINVAL;
198d8f388d8SDavid Brownell }
199d8f388d8SDavid Brownell 
200a4177ee7SJani Nikula static inline int gpio_export_link(struct device *dev, const char *name,
201a4177ee7SJani Nikula 				unsigned gpio)
202a4177ee7SJani Nikula {
203a4177ee7SJani Nikula 	/* GPIO can never have been exported */
204a4177ee7SJani Nikula 	WARN_ON(1);
205a4177ee7SJani Nikula 	return -EINVAL;
206a4177ee7SJani Nikula }
207a4177ee7SJani Nikula 
208d8f388d8SDavid Brownell static inline void gpio_unexport(unsigned gpio)
209d8f388d8SDavid Brownell {
210d8f388d8SDavid Brownell 	/* GPIO can never have been exported */
211d8f388d8SDavid Brownell 	WARN_ON(1);
212d8f388d8SDavid Brownell }
213d8f388d8SDavid Brownell 
2147560fa60SDavid Brownell static inline int gpio_to_irq(unsigned gpio)
2157560fa60SDavid Brownell {
2167560fa60SDavid Brownell 	/* GPIO can never have been requested or set as input */
2177560fa60SDavid Brownell 	WARN_ON(1);
2187560fa60SDavid Brownell 	return -EINVAL;
2197560fa60SDavid Brownell }
2207560fa60SDavid Brownell 
2217560fa60SDavid Brownell static inline int irq_to_gpio(unsigned irq)
2227560fa60SDavid Brownell {
2237560fa60SDavid Brownell 	/* irq can never have been returned from gpio_to_irq() */
2247560fa60SDavid Brownell 	WARN_ON(1);
2257560fa60SDavid Brownell 	return -EINVAL;
2267560fa60SDavid Brownell }
2277560fa60SDavid Brownell 
228403c1d0bSLinus Walleij static inline int devm_gpio_request(struct device *dev, unsigned gpio,
229403c1d0bSLinus Walleij 				    const char *label)
230403c1d0bSLinus Walleij {
231403c1d0bSLinus Walleij 	WARN_ON(1);
232403c1d0bSLinus Walleij 	return -EINVAL;
233403c1d0bSLinus Walleij }
234403c1d0bSLinus Walleij 
235403c1d0bSLinus Walleij static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
236403c1d0bSLinus Walleij 					unsigned long flags, const char *label)
237403c1d0bSLinus Walleij {
238403c1d0bSLinus Walleij 	WARN_ON(1);
239403c1d0bSLinus Walleij 	return -EINVAL;
240403c1d0bSLinus Walleij }
241403c1d0bSLinus Walleij 
24276ec9d18SAlexandre Courbot #endif /* ! CONFIG_GPIOLIB */
2437560fa60SDavid Brownell 
2447560fa60SDavid Brownell #endif /* __LINUX_GPIO_H */
245