xref: /openbmc/linux/include/linux/gpio.h (revision b2441318)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
27560fa60SDavid Brownell #ifndef __LINUX_GPIO_H
37560fa60SDavid Brownell #define __LINUX_GPIO_H
47560fa60SDavid Brownell 
57563bbf8SMark Brown #include <linux/errno.h>
67563bbf8SMark Brown 
7f7b370a3SRichard Genoud /* see Documentation/gpio/gpio-legacy.txt */
87560fa60SDavid Brownell 
9c001fb72SRandy Dunlap /* make these flag values available regardless of GPIO kconfig options */
10c001fb72SRandy Dunlap #define GPIOF_DIR_OUT	(0 << 0)
11c001fb72SRandy Dunlap #define GPIOF_DIR_IN	(1 << 0)
12c001fb72SRandy Dunlap 
13c001fb72SRandy Dunlap #define GPIOF_INIT_LOW	(0 << 1)
14c001fb72SRandy Dunlap #define GPIOF_INIT_HIGH	(1 << 1)
15c001fb72SRandy Dunlap 
16c001fb72SRandy Dunlap #define GPIOF_IN		(GPIOF_DIR_IN)
17c001fb72SRandy Dunlap #define GPIOF_OUT_INIT_LOW	(GPIOF_DIR_OUT | GPIOF_INIT_LOW)
18c001fb72SRandy Dunlap #define GPIOF_OUT_INIT_HIGH	(GPIOF_DIR_OUT | GPIOF_INIT_HIGH)
19c001fb72SRandy Dunlap 
2079a9becdSAlexandre Courbot /* Gpio pin is active-low */
2179a9becdSAlexandre Courbot #define GPIOF_ACTIVE_LOW        (1 << 2)
2279a9becdSAlexandre Courbot 
23aca5ce14SLaxman Dewangan /* Gpio pin is open drain */
2479a9becdSAlexandre Courbot #define GPIOF_OPEN_DRAIN	(1 << 3)
25aca5ce14SLaxman Dewangan 
2625553ff0SLaxman Dewangan /* Gpio pin is open source */
2779a9becdSAlexandre Courbot #define GPIOF_OPEN_SOURCE	(1 << 4)
2825553ff0SLaxman Dewangan 
2979a9becdSAlexandre Courbot #define GPIOF_EXPORT		(1 << 5)
3079a9becdSAlexandre Courbot #define GPIOF_EXPORT_CHANGEABLE	(1 << 6)
31fc3a1f04SWolfram Sang #define GPIOF_EXPORT_DIR_FIXED	(GPIOF_EXPORT)
32fc3a1f04SWolfram Sang #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
33fc3a1f04SWolfram Sang 
34feb83699SMark Brown /**
35feb83699SMark Brown  * struct gpio - a structure describing a GPIO with configuration
36feb83699SMark Brown  * @gpio:	the GPIO number
37feb83699SMark Brown  * @flags:	GPIO configuration as specified by GPIOF_*
38feb83699SMark Brown  * @label:	a literal description string of this GPIO
39feb83699SMark Brown  */
40feb83699SMark Brown struct gpio {
41feb83699SMark Brown 	unsigned	gpio;
42feb83699SMark Brown 	unsigned long	flags;
43feb83699SMark Brown 	const char	*label;
44feb83699SMark Brown };
45feb83699SMark Brown 
4676ec9d18SAlexandre Courbot #ifdef CONFIG_GPIOLIB
477563bbf8SMark Brown 
487563bbf8SMark Brown #ifdef CONFIG_ARCH_HAVE_CUSTOM_GPIO_H
497560fa60SDavid Brownell #include <asm/gpio.h>
507563bbf8SMark Brown #else
517563bbf8SMark Brown 
527563bbf8SMark Brown #include <asm-generic/gpio.h>
537563bbf8SMark Brown 
547563bbf8SMark Brown static inline int gpio_get_value(unsigned int gpio)
557563bbf8SMark Brown {
567563bbf8SMark Brown 	return __gpio_get_value(gpio);
577563bbf8SMark Brown }
587563bbf8SMark Brown 
597563bbf8SMark Brown static inline void gpio_set_value(unsigned int gpio, int value)
607563bbf8SMark Brown {
617563bbf8SMark Brown 	__gpio_set_value(gpio, value);
627563bbf8SMark Brown }
637563bbf8SMark Brown 
647563bbf8SMark Brown static inline int gpio_cansleep(unsigned int gpio)
657563bbf8SMark Brown {
667563bbf8SMark Brown 	return __gpio_cansleep(gpio);
677563bbf8SMark Brown }
687563bbf8SMark Brown 
697563bbf8SMark Brown static inline int gpio_to_irq(unsigned int gpio)
707563bbf8SMark Brown {
717563bbf8SMark Brown 	return __gpio_to_irq(gpio);
727563bbf8SMark Brown }
737563bbf8SMark Brown 
747563bbf8SMark Brown static inline int irq_to_gpio(unsigned int irq)
757563bbf8SMark Brown {
767563bbf8SMark Brown 	return -EINVAL;
777563bbf8SMark Brown }
787563bbf8SMark Brown 
79165adc9cSLinus Walleij #endif /* ! CONFIG_ARCH_HAVE_CUSTOM_GPIO_H */
807560fa60SDavid Brownell 
81403c1d0bSLinus Walleij /* CONFIG_GPIOLIB: bindings for managed devices that want to request gpios */
82403c1d0bSLinus Walleij 
83403c1d0bSLinus Walleij struct device;
84403c1d0bSLinus Walleij 
85403c1d0bSLinus Walleij int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
86403c1d0bSLinus Walleij int devm_gpio_request_one(struct device *dev, unsigned gpio,
87403c1d0bSLinus Walleij 			  unsigned long flags, const char *label);
88403c1d0bSLinus Walleij void devm_gpio_free(struct device *dev, unsigned int gpio);
89403c1d0bSLinus Walleij 
9076ec9d18SAlexandre Courbot #else /* ! CONFIG_GPIOLIB */
917560fa60SDavid Brownell 
923d599d1cSUwe Kleine-König #include <linux/kernel.h>
936ea0205bSDavid Brownell #include <linux/types.h>
94187f1882SPaul Gortmaker #include <linux/bug.h>
95586a87e6SChristian Ruppert #include <linux/pinctrl/pinctrl.h>
966ea0205bSDavid Brownell 
97a4177ee7SJani Nikula struct device;
984e4438b8SAnton Vorontsov struct gpio_chip;
99a4177ee7SJani Nikula 
1003474cb3cSJoe Perches static inline bool gpio_is_valid(int number)
1017560fa60SDavid Brownell {
1023474cb3cSJoe Perches 	return false;
1037560fa60SDavid Brownell }
1047560fa60SDavid Brownell 
105d8a3515eSLinus Torvalds static inline int gpio_request(unsigned gpio, const char *label)
1067560fa60SDavid Brownell {
1077560fa60SDavid Brownell 	return -ENOSYS;
1087560fa60SDavid Brownell }
1097560fa60SDavid Brownell 
110323b7fe8SWolfram Sang static inline int gpio_request_one(unsigned gpio,
1115f829e40SWolfram Sang 					unsigned long flags, const char *label)
1125f829e40SWolfram Sang {
1135f829e40SWolfram Sang 	return -ENOSYS;
1145f829e40SWolfram Sang }
1155f829e40SWolfram Sang 
1167c295975SLars-Peter Clausen static inline int gpio_request_array(const struct gpio *array, size_t num)
1175f829e40SWolfram Sang {
1185f829e40SWolfram Sang 	return -ENOSYS;
1195f829e40SWolfram Sang }
1205f829e40SWolfram Sang 
1217560fa60SDavid Brownell static inline void gpio_free(unsigned gpio)
1227560fa60SDavid Brownell {
1233d599d1cSUwe Kleine-König 	might_sleep();
1243d599d1cSUwe Kleine-König 
1257560fa60SDavid Brownell 	/* GPIO can never have been requested */
1267560fa60SDavid Brownell 	WARN_ON(1);
1277560fa60SDavid Brownell }
1287560fa60SDavid Brownell 
1297c295975SLars-Peter Clausen static inline void gpio_free_array(const struct gpio *array, size_t num)
1305f829e40SWolfram Sang {
1315f829e40SWolfram Sang 	might_sleep();
1325f829e40SWolfram Sang 
1335f829e40SWolfram Sang 	/* GPIO can never have been requested */
1345f829e40SWolfram Sang 	WARN_ON(1);
1355f829e40SWolfram Sang }
1365f829e40SWolfram Sang 
137d8a3515eSLinus Torvalds static inline int gpio_direction_input(unsigned gpio)
1387560fa60SDavid Brownell {
1397560fa60SDavid Brownell 	return -ENOSYS;
1407560fa60SDavid Brownell }
1417560fa60SDavid Brownell 
142d8a3515eSLinus Torvalds static inline int gpio_direction_output(unsigned gpio, int value)
1437560fa60SDavid Brownell {
1447560fa60SDavid Brownell 	return -ENOSYS;
1457560fa60SDavid Brownell }
1467560fa60SDavid Brownell 
147c4b5be98SFelipe Balbi static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
148c4b5be98SFelipe Balbi {
149c4b5be98SFelipe Balbi 	return -ENOSYS;
150c4b5be98SFelipe Balbi }
151c4b5be98SFelipe Balbi 
1527560fa60SDavid Brownell static inline int gpio_get_value(unsigned gpio)
1537560fa60SDavid Brownell {
1547560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1557560fa60SDavid Brownell 	WARN_ON(1);
1567560fa60SDavid Brownell 	return 0;
1577560fa60SDavid Brownell }
1587560fa60SDavid Brownell 
1597560fa60SDavid Brownell static inline void gpio_set_value(unsigned gpio, int value)
1607560fa60SDavid Brownell {
1617560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
1627560fa60SDavid Brownell 	WARN_ON(1);
1637560fa60SDavid Brownell }
1647560fa60SDavid Brownell 
1657560fa60SDavid Brownell static inline int gpio_cansleep(unsigned gpio)
1667560fa60SDavid Brownell {
1677560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1687560fa60SDavid Brownell 	WARN_ON(1);
1697560fa60SDavid Brownell 	return 0;
1707560fa60SDavid Brownell }
1717560fa60SDavid Brownell 
1727560fa60SDavid Brownell static inline int gpio_get_value_cansleep(unsigned gpio)
1737560fa60SDavid Brownell {
1747560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1757560fa60SDavid Brownell 	WARN_ON(1);
1767560fa60SDavid Brownell 	return 0;
1777560fa60SDavid Brownell }
1787560fa60SDavid Brownell 
1797560fa60SDavid Brownell static inline void gpio_set_value_cansleep(unsigned gpio, int value)
1807560fa60SDavid Brownell {
1817560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
1827560fa60SDavid Brownell 	WARN_ON(1);
1837560fa60SDavid Brownell }
1847560fa60SDavid Brownell 
185d8f388d8SDavid Brownell static inline int gpio_export(unsigned gpio, bool direction_may_change)
186d8f388d8SDavid Brownell {
187d8f388d8SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
188d8f388d8SDavid Brownell 	WARN_ON(1);
189d8f388d8SDavid Brownell 	return -EINVAL;
190d8f388d8SDavid Brownell }
191d8f388d8SDavid Brownell 
192a4177ee7SJani Nikula static inline int gpio_export_link(struct device *dev, const char *name,
193a4177ee7SJani Nikula 				unsigned gpio)
194a4177ee7SJani Nikula {
195a4177ee7SJani Nikula 	/* GPIO can never have been exported */
196a4177ee7SJani Nikula 	WARN_ON(1);
197a4177ee7SJani Nikula 	return -EINVAL;
198a4177ee7SJani Nikula }
199a4177ee7SJani Nikula 
200d8f388d8SDavid Brownell static inline void gpio_unexport(unsigned gpio)
201d8f388d8SDavid Brownell {
202d8f388d8SDavid Brownell 	/* GPIO can never have been exported */
203d8f388d8SDavid Brownell 	WARN_ON(1);
204d8f388d8SDavid Brownell }
205d8f388d8SDavid 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 
213e3a2e878SAlexandre Courbot static inline int gpiochip_lock_as_irq(struct gpio_chip *chip,
214e3a2e878SAlexandre Courbot 				       unsigned int offset)
215d468bf9eSLinus Walleij {
216d468bf9eSLinus Walleij 	WARN_ON(1);
217d468bf9eSLinus Walleij 	return -EINVAL;
218d468bf9eSLinus Walleij }
219d468bf9eSLinus Walleij 
220e3a2e878SAlexandre Courbot static inline void gpiochip_unlock_as_irq(struct gpio_chip *chip,
221d468bf9eSLinus Walleij 					  unsigned int offset)
222d468bf9eSLinus Walleij {
223d468bf9eSLinus Walleij 	WARN_ON(1);
224d468bf9eSLinus Walleij }
225d468bf9eSLinus Walleij 
2267560fa60SDavid Brownell static inline int irq_to_gpio(unsigned irq)
2277560fa60SDavid Brownell {
2287560fa60SDavid Brownell 	/* irq can never have been returned from gpio_to_irq() */
2297560fa60SDavid Brownell 	WARN_ON(1);
2307560fa60SDavid Brownell 	return -EINVAL;
2317560fa60SDavid Brownell }
2327560fa60SDavid Brownell 
2331e63d7b9SLinus Walleij static inline int
234165adc9cSLinus Walleij gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
235316511c0SLinus Walleij 		       unsigned int gpio_offset, unsigned int pin_offset,
2363f0f8670SLinus Walleij 		       unsigned int npins)
237165adc9cSLinus Walleij {
23850309a9cSLinus Walleij 	WARN_ON(1);
23950309a9cSLinus Walleij 	return -EINVAL;
240165adc9cSLinus Walleij }
241165adc9cSLinus Walleij 
242586a87e6SChristian Ruppert static inline int
243586a87e6SChristian Ruppert gpiochip_add_pingroup_range(struct gpio_chip *chip,
244586a87e6SChristian Ruppert 			struct pinctrl_dev *pctldev,
245586a87e6SChristian Ruppert 			unsigned int gpio_offset, const char *pin_group)
246586a87e6SChristian Ruppert {
247586a87e6SChristian Ruppert 	WARN_ON(1);
248586a87e6SChristian Ruppert 	return -EINVAL;
249586a87e6SChristian Ruppert }
250586a87e6SChristian Ruppert 
251165adc9cSLinus Walleij static inline void
252165adc9cSLinus Walleij gpiochip_remove_pin_ranges(struct gpio_chip *chip)
253165adc9cSLinus Walleij {
25450309a9cSLinus Walleij 	WARN_ON(1);
255165adc9cSLinus Walleij }
256165adc9cSLinus Walleij 
257403c1d0bSLinus Walleij static inline int devm_gpio_request(struct device *dev, unsigned gpio,
258403c1d0bSLinus Walleij 				    const char *label)
259403c1d0bSLinus Walleij {
260403c1d0bSLinus Walleij 	WARN_ON(1);
261403c1d0bSLinus Walleij 	return -EINVAL;
262403c1d0bSLinus Walleij }
263403c1d0bSLinus Walleij 
264403c1d0bSLinus Walleij static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
265403c1d0bSLinus Walleij 					unsigned long flags, const char *label)
266403c1d0bSLinus Walleij {
267403c1d0bSLinus Walleij 	WARN_ON(1);
268403c1d0bSLinus Walleij 	return -EINVAL;
269403c1d0bSLinus Walleij }
270403c1d0bSLinus Walleij 
271403c1d0bSLinus Walleij static inline void devm_gpio_free(struct device *dev, unsigned int gpio)
272403c1d0bSLinus Walleij {
273403c1d0bSLinus Walleij 	WARN_ON(1);
274403c1d0bSLinus Walleij }
275403c1d0bSLinus Walleij 
27676ec9d18SAlexandre Courbot #endif /* ! CONFIG_GPIOLIB */
2777560fa60SDavid Brownell 
2787560fa60SDavid Brownell #endif /* __LINUX_GPIO_H */
279