xref: /openbmc/linux/include/linux/gpio.h (revision 60a86668)
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 void devm_gpio_free(struct device *dev, unsigned int gpio);
99403c1d0bSLinus Walleij 
10076ec9d18SAlexandre Courbot #else /* ! CONFIG_GPIOLIB */
1017560fa60SDavid Brownell 
1023d599d1cSUwe Kleine-König #include <linux/kernel.h>
1036ea0205bSDavid Brownell #include <linux/types.h>
104187f1882SPaul Gortmaker #include <linux/bug.h>
105586a87e6SChristian Ruppert #include <linux/pinctrl/pinctrl.h>
1066ea0205bSDavid Brownell 
107a4177ee7SJani Nikula struct device;
1084e4438b8SAnton Vorontsov struct gpio_chip;
109a4177ee7SJani Nikula 
1103474cb3cSJoe Perches static inline bool gpio_is_valid(int number)
1117560fa60SDavid Brownell {
1123474cb3cSJoe Perches 	return false;
1137560fa60SDavid Brownell }
1147560fa60SDavid Brownell 
115d8a3515eSLinus Torvalds static inline int gpio_request(unsigned gpio, const char *label)
1167560fa60SDavid Brownell {
1177560fa60SDavid Brownell 	return -ENOSYS;
1187560fa60SDavid Brownell }
1197560fa60SDavid Brownell 
120323b7fe8SWolfram Sang static inline int gpio_request_one(unsigned gpio,
1215f829e40SWolfram Sang 					unsigned long flags, const char *label)
1225f829e40SWolfram Sang {
1235f829e40SWolfram Sang 	return -ENOSYS;
1245f829e40SWolfram Sang }
1255f829e40SWolfram Sang 
1267c295975SLars-Peter Clausen static inline int gpio_request_array(const struct gpio *array, size_t num)
1275f829e40SWolfram Sang {
1285f829e40SWolfram Sang 	return -ENOSYS;
1295f829e40SWolfram Sang }
1305f829e40SWolfram Sang 
1317560fa60SDavid Brownell static inline void gpio_free(unsigned gpio)
1327560fa60SDavid Brownell {
1333d599d1cSUwe Kleine-König 	might_sleep();
1343d599d1cSUwe Kleine-König 
1357560fa60SDavid Brownell 	/* GPIO can never have been requested */
1367560fa60SDavid Brownell 	WARN_ON(1);
1377560fa60SDavid Brownell }
1387560fa60SDavid Brownell 
1397c295975SLars-Peter Clausen static inline void gpio_free_array(const struct gpio *array, size_t num)
1405f829e40SWolfram Sang {
1415f829e40SWolfram Sang 	might_sleep();
1425f829e40SWolfram Sang 
1435f829e40SWolfram Sang 	/* GPIO can never have been requested */
1445f829e40SWolfram Sang 	WARN_ON(1);
1455f829e40SWolfram Sang }
1465f829e40SWolfram Sang 
147d8a3515eSLinus Torvalds static inline int gpio_direction_input(unsigned gpio)
1487560fa60SDavid Brownell {
1497560fa60SDavid Brownell 	return -ENOSYS;
1507560fa60SDavid Brownell }
1517560fa60SDavid Brownell 
152d8a3515eSLinus Torvalds static inline int gpio_direction_output(unsigned gpio, int value)
1537560fa60SDavid Brownell {
1547560fa60SDavid Brownell 	return -ENOSYS;
1557560fa60SDavid Brownell }
1567560fa60SDavid Brownell 
157c4b5be98SFelipe Balbi static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
158c4b5be98SFelipe Balbi {
159c4b5be98SFelipe Balbi 	return -ENOSYS;
160c4b5be98SFelipe Balbi }
161c4b5be98SFelipe Balbi 
1627560fa60SDavid Brownell static inline int gpio_get_value(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(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 
1757560fa60SDavid Brownell static inline int gpio_cansleep(unsigned gpio)
1767560fa60SDavid Brownell {
1777560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1787560fa60SDavid Brownell 	WARN_ON(1);
1797560fa60SDavid Brownell 	return 0;
1807560fa60SDavid Brownell }
1817560fa60SDavid Brownell 
1827560fa60SDavid Brownell static inline int gpio_get_value_cansleep(unsigned gpio)
1837560fa60SDavid Brownell {
1847560fa60SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
1857560fa60SDavid Brownell 	WARN_ON(1);
1867560fa60SDavid Brownell 	return 0;
1877560fa60SDavid Brownell }
1887560fa60SDavid Brownell 
1897560fa60SDavid Brownell static inline void gpio_set_value_cansleep(unsigned gpio, int value)
1907560fa60SDavid Brownell {
1917560fa60SDavid Brownell 	/* GPIO can never have been requested or set as output */
1927560fa60SDavid Brownell 	WARN_ON(1);
1937560fa60SDavid Brownell }
1947560fa60SDavid Brownell 
195d8f388d8SDavid Brownell static inline int gpio_export(unsigned gpio, bool direction_may_change)
196d8f388d8SDavid Brownell {
197d8f388d8SDavid Brownell 	/* GPIO can never have been requested or set as {in,out}put */
198d8f388d8SDavid Brownell 	WARN_ON(1);
199d8f388d8SDavid Brownell 	return -EINVAL;
200d8f388d8SDavid Brownell }
201d8f388d8SDavid Brownell 
202a4177ee7SJani Nikula static inline int gpio_export_link(struct device *dev, const char *name,
203a4177ee7SJani Nikula 				unsigned gpio)
204a4177ee7SJani Nikula {
205a4177ee7SJani Nikula 	/* GPIO can never have been exported */
206a4177ee7SJani Nikula 	WARN_ON(1);
207a4177ee7SJani Nikula 	return -EINVAL;
208a4177ee7SJani Nikula }
209a4177ee7SJani Nikula 
210d8f388d8SDavid Brownell static inline void gpio_unexport(unsigned gpio)
211d8f388d8SDavid Brownell {
212d8f388d8SDavid Brownell 	/* GPIO can never have been exported */
213d8f388d8SDavid Brownell 	WARN_ON(1);
214d8f388d8SDavid Brownell }
215d8f388d8SDavid Brownell 
2167560fa60SDavid Brownell static inline int gpio_to_irq(unsigned gpio)
2177560fa60SDavid Brownell {
2187560fa60SDavid Brownell 	/* GPIO can never have been requested or set as input */
2197560fa60SDavid Brownell 	WARN_ON(1);
2207560fa60SDavid Brownell 	return -EINVAL;
2217560fa60SDavid Brownell }
2227560fa60SDavid Brownell 
223e3a2e878SAlexandre Courbot static inline int gpiochip_lock_as_irq(struct gpio_chip *chip,
224e3a2e878SAlexandre Courbot 				       unsigned int offset)
225d468bf9eSLinus Walleij {
226d468bf9eSLinus Walleij 	WARN_ON(1);
227d468bf9eSLinus Walleij 	return -EINVAL;
228d468bf9eSLinus Walleij }
229d468bf9eSLinus Walleij 
230e3a2e878SAlexandre Courbot static inline void gpiochip_unlock_as_irq(struct gpio_chip *chip,
231d468bf9eSLinus Walleij 					  unsigned int offset)
232d468bf9eSLinus Walleij {
233d468bf9eSLinus Walleij 	WARN_ON(1);
234d468bf9eSLinus Walleij }
235d468bf9eSLinus Walleij 
2367560fa60SDavid Brownell static inline int irq_to_gpio(unsigned irq)
2377560fa60SDavid Brownell {
2387560fa60SDavid Brownell 	/* irq can never have been returned from gpio_to_irq() */
2397560fa60SDavid Brownell 	WARN_ON(1);
2407560fa60SDavid Brownell 	return -EINVAL;
2417560fa60SDavid Brownell }
2427560fa60SDavid Brownell 
2431e63d7b9SLinus Walleij static inline int
244165adc9cSLinus Walleij gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
245316511c0SLinus Walleij 		       unsigned int gpio_offset, unsigned int pin_offset,
2463f0f8670SLinus Walleij 		       unsigned int npins)
247165adc9cSLinus Walleij {
24850309a9cSLinus Walleij 	WARN_ON(1);
24950309a9cSLinus Walleij 	return -EINVAL;
250165adc9cSLinus Walleij }
251165adc9cSLinus Walleij 
252586a87e6SChristian Ruppert static inline int
253586a87e6SChristian Ruppert gpiochip_add_pingroup_range(struct gpio_chip *chip,
254586a87e6SChristian Ruppert 			struct pinctrl_dev *pctldev,
255586a87e6SChristian Ruppert 			unsigned int gpio_offset, const char *pin_group)
256586a87e6SChristian Ruppert {
257586a87e6SChristian Ruppert 	WARN_ON(1);
258586a87e6SChristian Ruppert 	return -EINVAL;
259586a87e6SChristian Ruppert }
260586a87e6SChristian Ruppert 
261165adc9cSLinus Walleij static inline void
262165adc9cSLinus Walleij gpiochip_remove_pin_ranges(struct gpio_chip *chip)
263165adc9cSLinus Walleij {
26450309a9cSLinus Walleij 	WARN_ON(1);
265165adc9cSLinus Walleij }
266165adc9cSLinus Walleij 
267403c1d0bSLinus Walleij static inline int devm_gpio_request(struct device *dev, unsigned gpio,
268403c1d0bSLinus Walleij 				    const char *label)
269403c1d0bSLinus Walleij {
270403c1d0bSLinus Walleij 	WARN_ON(1);
271403c1d0bSLinus Walleij 	return -EINVAL;
272403c1d0bSLinus Walleij }
273403c1d0bSLinus Walleij 
274403c1d0bSLinus Walleij static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
275403c1d0bSLinus Walleij 					unsigned long flags, const char *label)
276403c1d0bSLinus Walleij {
277403c1d0bSLinus Walleij 	WARN_ON(1);
278403c1d0bSLinus Walleij 	return -EINVAL;
279403c1d0bSLinus Walleij }
280403c1d0bSLinus Walleij 
281403c1d0bSLinus Walleij static inline void devm_gpio_free(struct device *dev, unsigned int gpio)
282403c1d0bSLinus Walleij {
283403c1d0bSLinus Walleij 	WARN_ON(1);
284403c1d0bSLinus Walleij }
285403c1d0bSLinus Walleij 
28676ec9d18SAlexandre Courbot #endif /* ! CONFIG_GPIOLIB */
2877560fa60SDavid Brownell 
2887560fa60SDavid Brownell #endif /* __LINUX_GPIO_H */
289