xref: /openbmc/linux/include/linux/gpio.h (revision 79a9becd)
17560fa60SDavid Brownell #ifndef __LINUX_GPIO_H
27560fa60SDavid Brownell #define __LINUX_GPIO_H
37560fa60SDavid Brownell 
47563bbf8SMark Brown #include <linux/errno.h>
57563bbf8SMark Brown 
67560fa60SDavid Brownell /* see Documentation/gpio.txt */
77560fa60SDavid Brownell 
8c001fb72SRandy Dunlap /* make these flag values available regardless of GPIO kconfig options */
9c001fb72SRandy Dunlap #define GPIOF_DIR_OUT	(0 << 0)
10c001fb72SRandy Dunlap #define GPIOF_DIR_IN	(1 << 0)
11c001fb72SRandy Dunlap 
12c001fb72SRandy Dunlap #define GPIOF_INIT_LOW	(0 << 1)
13c001fb72SRandy Dunlap #define GPIOF_INIT_HIGH	(1 << 1)
14c001fb72SRandy Dunlap 
15c001fb72SRandy Dunlap #define GPIOF_IN		(GPIOF_DIR_IN)
16c001fb72SRandy Dunlap #define GPIOF_OUT_INIT_LOW	(GPIOF_DIR_OUT | GPIOF_INIT_LOW)
17c001fb72SRandy Dunlap #define GPIOF_OUT_INIT_HIGH	(GPIOF_DIR_OUT | GPIOF_INIT_HIGH)
18c001fb72SRandy Dunlap 
1979a9becdSAlexandre Courbot /* Gpio pin is active-low */
2079a9becdSAlexandre Courbot #define GPIOF_ACTIVE_LOW        (1 << 2)
2179a9becdSAlexandre Courbot 
22aca5ce14SLaxman Dewangan /* Gpio pin is open drain */
2379a9becdSAlexandre Courbot #define GPIOF_OPEN_DRAIN	(1 << 3)
24aca5ce14SLaxman Dewangan 
2525553ff0SLaxman Dewangan /* Gpio pin is open source */
2679a9becdSAlexandre Courbot #define GPIOF_OPEN_SOURCE	(1 << 4)
2725553ff0SLaxman Dewangan 
2879a9becdSAlexandre Courbot #define GPIOF_EXPORT		(1 << 5)
2979a9becdSAlexandre Courbot #define GPIOF_EXPORT_CHANGEABLE	(1 << 6)
30fc3a1f04SWolfram Sang #define GPIOF_EXPORT_DIR_FIXED	(GPIOF_EXPORT)
31fc3a1f04SWolfram Sang #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
32fc3a1f04SWolfram Sang 
33feb83699SMark Brown /**
34feb83699SMark Brown  * struct gpio - a structure describing a GPIO with configuration
35feb83699SMark Brown  * @gpio:	the GPIO number
36feb83699SMark Brown  * @flags:	GPIO configuration as specified by GPIOF_*
37feb83699SMark Brown  * @label:	a literal description string of this GPIO
38feb83699SMark Brown  */
39feb83699SMark Brown struct gpio {
40feb83699SMark Brown 	unsigned	gpio;
41feb83699SMark Brown 	unsigned long	flags;
42feb83699SMark Brown 	const char	*label;
43feb83699SMark Brown };
44feb83699SMark Brown 
4576ec9d18SAlexandre Courbot #ifdef CONFIG_GPIOLIB
467563bbf8SMark Brown 
477563bbf8SMark Brown #ifdef CONFIG_ARCH_HAVE_CUSTOM_GPIO_H
487560fa60SDavid Brownell #include <asm/gpio.h>
497563bbf8SMark Brown #else
507563bbf8SMark Brown 
517563bbf8SMark Brown #include <asm-generic/gpio.h>
527563bbf8SMark Brown 
537563bbf8SMark Brown static inline int gpio_get_value(unsigned int gpio)
547563bbf8SMark Brown {
557563bbf8SMark Brown 	return __gpio_get_value(gpio);
567563bbf8SMark Brown }
577563bbf8SMark Brown 
587563bbf8SMark Brown static inline void gpio_set_value(unsigned int gpio, int value)
597563bbf8SMark Brown {
607563bbf8SMark Brown 	__gpio_set_value(gpio, value);
617563bbf8SMark Brown }
627563bbf8SMark Brown 
637563bbf8SMark Brown static inline int gpio_cansleep(unsigned int gpio)
647563bbf8SMark Brown {
657563bbf8SMark Brown 	return __gpio_cansleep(gpio);
667563bbf8SMark Brown }
677563bbf8SMark Brown 
687563bbf8SMark Brown static inline int gpio_to_irq(unsigned int gpio)
697563bbf8SMark Brown {
707563bbf8SMark Brown 	return __gpio_to_irq(gpio);
717563bbf8SMark Brown }
727563bbf8SMark Brown 
737563bbf8SMark Brown static inline int irq_to_gpio(unsigned int irq)
747563bbf8SMark Brown {
757563bbf8SMark Brown 	return -EINVAL;
767563bbf8SMark Brown }
777563bbf8SMark Brown 
78165adc9cSLinus Walleij #endif /* ! CONFIG_ARCH_HAVE_CUSTOM_GPIO_H */
797560fa60SDavid Brownell 
8076ec9d18SAlexandre Courbot #else /* ! CONFIG_GPIOLIB */
817560fa60SDavid Brownell 
823d599d1cSUwe Kleine-König #include <linux/kernel.h>
836ea0205bSDavid Brownell #include <linux/types.h>
846ea0205bSDavid Brownell #include <linux/errno.h>
85187f1882SPaul Gortmaker #include <linux/bug.h>
866ea0205bSDavid Brownell 
87a4177ee7SJani Nikula struct device;
884e4438b8SAnton Vorontsov struct gpio_chip;
89a4177ee7SJani Nikula 
903474cb3cSJoe Perches static inline bool gpio_is_valid(int number)
917560fa60SDavid Brownell {
923474cb3cSJoe Perches 	return false;
937560fa60SDavid Brownell }
947560fa60SDavid Brownell 
95d8a3515eSLinus Torvalds static inline int gpio_request(unsigned gpio, const char *label)
967560fa60SDavid Brownell {
977560fa60SDavid Brownell 	return -ENOSYS;
987560fa60SDavid Brownell }
997560fa60SDavid Brownell 
100323b7fe8SWolfram Sang static inline int gpio_request_one(unsigned gpio,
1015f829e40SWolfram Sang 					unsigned long flags, const char *label)
1025f829e40SWolfram Sang {
1035f829e40SWolfram Sang 	return -ENOSYS;
1045f829e40SWolfram Sang }
1055f829e40SWolfram Sang 
1067c295975SLars-Peter Clausen static inline int gpio_request_array(const struct gpio *array, size_t num)
1075f829e40SWolfram Sang {
1085f829e40SWolfram Sang 	return -ENOSYS;
1095f829e40SWolfram Sang }
1105f829e40SWolfram Sang 
1117560fa60SDavid Brownell static inline void gpio_free(unsigned gpio)
1127560fa60SDavid Brownell {
1133d599d1cSUwe Kleine-König 	might_sleep();
1143d599d1cSUwe Kleine-König 
1157560fa60SDavid Brownell 	/* GPIO can never have been requested */
1167560fa60SDavid Brownell 	WARN_ON(1);
1177560fa60SDavid Brownell }
1187560fa60SDavid Brownell 
1197c295975SLars-Peter Clausen static inline void gpio_free_array(const struct gpio *array, size_t num)
1205f829e40SWolfram Sang {
1215f829e40SWolfram Sang 	might_sleep();
1225f829e40SWolfram Sang 
1235f829e40SWolfram Sang 	/* GPIO can never have been requested */
1245f829e40SWolfram Sang 	WARN_ON(1);
1255f829e40SWolfram Sang }
1265f829e40SWolfram Sang 
127d8a3515eSLinus Torvalds static inline int gpio_direction_input(unsigned gpio)
1287560fa60SDavid Brownell {
1297560fa60SDavid Brownell 	return -ENOSYS;
1307560fa60SDavid Brownell }
1317560fa60SDavid Brownell 
132d8a3515eSLinus Torvalds static inline int gpio_direction_output(unsigned gpio, int value)
1337560fa60SDavid Brownell {
1347560fa60SDavid Brownell 	return -ENOSYS;
1357560fa60SDavid Brownell }
1367560fa60SDavid Brownell 
137c4b5be98SFelipe Balbi static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
138c4b5be98SFelipe Balbi {
139c4b5be98SFelipe Balbi 	return -ENOSYS;
140c4b5be98SFelipe Balbi }
141c4b5be98SFelipe Balbi 
1427560fa60SDavid Brownell static inline int gpio_get_value(unsigned gpio)
1437560fa60SDavid Brownell {
1447560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1457560fa60SDavid Brownell 	WARN_ON(1);
1467560fa60SDavid Brownell 	return 0;
1477560fa60SDavid Brownell }
1487560fa60SDavid Brownell 
1497560fa60SDavid Brownell static inline void gpio_set_value(unsigned gpio, int value)
1507560fa60SDavid Brownell {
1517560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
1527560fa60SDavid Brownell 	WARN_ON(1);
1537560fa60SDavid Brownell }
1547560fa60SDavid Brownell 
1557560fa60SDavid Brownell static inline int gpio_cansleep(unsigned gpio)
1567560fa60SDavid Brownell {
1577560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1587560fa60SDavid Brownell 	WARN_ON(1);
1597560fa60SDavid Brownell 	return 0;
1607560fa60SDavid Brownell }
1617560fa60SDavid Brownell 
1627560fa60SDavid Brownell static inline int gpio_get_value_cansleep(unsigned gpio)
1637560fa60SDavid Brownell {
1647560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1657560fa60SDavid Brownell 	WARN_ON(1);
1667560fa60SDavid Brownell 	return 0;
1677560fa60SDavid Brownell }
1687560fa60SDavid Brownell 
1697560fa60SDavid Brownell static inline void gpio_set_value_cansleep(unsigned gpio, int value)
1707560fa60SDavid Brownell {
1717560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
1727560fa60SDavid Brownell 	WARN_ON(1);
1737560fa60SDavid Brownell }
1747560fa60SDavid Brownell 
175d8f388d8SDavid Brownell static inline int gpio_export(unsigned gpio, bool direction_may_change)
176d8f388d8SDavid Brownell {
177d8f388d8SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
178d8f388d8SDavid Brownell 	WARN_ON(1);
179d8f388d8SDavid Brownell 	return -EINVAL;
180d8f388d8SDavid Brownell }
181d8f388d8SDavid Brownell 
182a4177ee7SJani Nikula static inline int gpio_export_link(struct device *dev, const char *name,
183a4177ee7SJani Nikula 				unsigned gpio)
184a4177ee7SJani Nikula {
185a4177ee7SJani Nikula 	/* GPIO can never have been exported */
186a4177ee7SJani Nikula 	WARN_ON(1);
187a4177ee7SJani Nikula 	return -EINVAL;
188a4177ee7SJani Nikula }
189a4177ee7SJani Nikula 
19007697461SJani Nikula static inline int gpio_sysfs_set_active_low(unsigned gpio, int value)
19107697461SJani Nikula {
19207697461SJani Nikula 	/* GPIO can never have been requested */
19307697461SJani Nikula 	WARN_ON(1);
19407697461SJani Nikula 	return -EINVAL;
19507697461SJani Nikula }
196a4177ee7SJani Nikula 
197d8f388d8SDavid Brownell static inline void gpio_unexport(unsigned gpio)
198d8f388d8SDavid Brownell {
199d8f388d8SDavid Brownell 	/* GPIO can never have been exported */
200d8f388d8SDavid Brownell 	WARN_ON(1);
201d8f388d8SDavid Brownell }
202d8f388d8SDavid Brownell 
2037560fa60SDavid Brownell static inline int gpio_to_irq(unsigned gpio)
2047560fa60SDavid Brownell {
2057560fa60SDavid Brownell 	/* GPIO can never have been requested or set as input */
2067560fa60SDavid Brownell 	WARN_ON(1);
2077560fa60SDavid Brownell 	return -EINVAL;
2087560fa60SDavid Brownell }
2097560fa60SDavid Brownell 
210d468bf9eSLinus Walleij static inline int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
211d468bf9eSLinus Walleij {
212d468bf9eSLinus Walleij 	WARN_ON(1);
213d468bf9eSLinus Walleij 	return -EINVAL;
214d468bf9eSLinus Walleij }
215d468bf9eSLinus Walleij 
216d468bf9eSLinus Walleij static inline void gpio_unlock_as_irq(struct gpio_chip *chip,
217d468bf9eSLinus Walleij 				      unsigned int offset)
218d468bf9eSLinus Walleij {
219d468bf9eSLinus Walleij 	WARN_ON(1);
220d468bf9eSLinus Walleij }
221d468bf9eSLinus Walleij 
2227560fa60SDavid Brownell static inline int irq_to_gpio(unsigned irq)
2237560fa60SDavid Brownell {
2247560fa60SDavid Brownell 	/* irq can never have been returned from gpio_to_irq() */
2257560fa60SDavid Brownell 	WARN_ON(1);
2267560fa60SDavid Brownell 	return -EINVAL;
2277560fa60SDavid Brownell }
2287560fa60SDavid Brownell 
2291e63d7b9SLinus Walleij static inline int
230165adc9cSLinus Walleij gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
231316511c0SLinus Walleij 		       unsigned int gpio_offset, unsigned int pin_offset,
2323f0f8670SLinus Walleij 		       unsigned int npins)
233165adc9cSLinus Walleij {
23450309a9cSLinus Walleij 	WARN_ON(1);
23550309a9cSLinus Walleij 	return -EINVAL;
236165adc9cSLinus Walleij }
237165adc9cSLinus Walleij 
238165adc9cSLinus Walleij static inline void
239165adc9cSLinus Walleij gpiochip_remove_pin_ranges(struct gpio_chip *chip)
240165adc9cSLinus Walleij {
24150309a9cSLinus Walleij 	WARN_ON(1);
242165adc9cSLinus Walleij }
243165adc9cSLinus Walleij 
24476ec9d18SAlexandre Courbot #endif /* ! CONFIG_GPIOLIB */
2457560fa60SDavid Brownell 
2466a89a314SShawn Guo struct device;
2476a89a314SShawn Guo 
2486a89a314SShawn Guo /* bindings for managed devices that want to request gpios */
2496a89a314SShawn Guo int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
2506a89a314SShawn Guo int devm_gpio_request_one(struct device *dev, unsigned gpio,
2516a89a314SShawn Guo 			  unsigned long flags, const char *label);
2526a89a314SShawn Guo void devm_gpio_free(struct device *dev, unsigned int gpio);
2536a89a314SShawn Guo 
2547560fa60SDavid Brownell #endif /* __LINUX_GPIO_H */
255