xref: /openbmc/linux/include/linux/of_gpio.h (revision 046e14af)
1af6074fcSRob Herring /* SPDX-License-Identifier: GPL-2.0+ */
2863fbf49SAnton Vorontsov /*
3863fbf49SAnton Vorontsov  * OF helpers for the GPIO API
4863fbf49SAnton Vorontsov  *
5863fbf49SAnton Vorontsov  * Copyright (c) 2007-2008  MontaVista Software, Inc.
6863fbf49SAnton Vorontsov  *
7863fbf49SAnton Vorontsov  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8863fbf49SAnton Vorontsov  */
9863fbf49SAnton Vorontsov 
10863fbf49SAnton Vorontsov #ifndef __LINUX_OF_GPIO_H
11863fbf49SAnton Vorontsov #define __LINUX_OF_GPIO_H
12863fbf49SAnton Vorontsov 
13b908b53dSAnton Vorontsov #include <linux/compiler.h>
14046e14afSAndy Shevchenko #include <linux/gpio/driver.h>
15046e14afSAndy Shevchenko #include <linux/gpio.h>		/* FIXME: Shouldn't be here */
1615c9a0acSGrant Likely #include <linux/of.h>
17863fbf49SAnton Vorontsov 
18b908b53dSAnton Vorontsov struct device_node;
19b908b53dSAnton Vorontsov 
20b908b53dSAnton Vorontsov /*
21b908b53dSAnton Vorontsov  * This is Linux-specific flags. By default controllers' and Linux' mapping
22b908b53dSAnton Vorontsov  * match, but GPIO controllers are free to translate their own flags to
23b908b53dSAnton Vorontsov  * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
24b908b53dSAnton Vorontsov  */
25b908b53dSAnton Vorontsov enum of_gpio_flags {
26b908b53dSAnton Vorontsov 	OF_GPIO_ACTIVE_LOW = 0x1,
2790b665f6SLaurent Pinchart 	OF_GPIO_SINGLE_ENDED = 0x2,
284c0facddSLaxman Dewangan 	OF_GPIO_OPEN_DRAIN = 0x4,
29e10f72bfSAndrew Jeffery 	OF_GPIO_TRANSITORY = 0x8,
30d449991cSThomas Petazzoni 	OF_GPIO_PULL_UP = 0x10,
31d449991cSThomas Petazzoni 	OF_GPIO_PULL_DOWN = 0x20,
32b908b53dSAnton Vorontsov };
33b908b53dSAnton Vorontsov 
34863fbf49SAnton Vorontsov #ifdef CONFIG_OF_GPIO
35863fbf49SAnton Vorontsov 
36046e14afSAndy Shevchenko #include <linux/kernel.h>
37046e14afSAndy Shevchenko 
38863fbf49SAnton Vorontsov /*
39863fbf49SAnton Vorontsov  * OF GPIO chip for memory mapped banks
40863fbf49SAnton Vorontsov  */
41863fbf49SAnton Vorontsov struct of_mm_gpio_chip {
42a19e3da5SAnton Vorontsov 	struct gpio_chip gc;
43863fbf49SAnton Vorontsov 	void (*save_regs)(struct of_mm_gpio_chip *mm_gc);
44863fbf49SAnton Vorontsov 	void __iomem *regs;
45863fbf49SAnton Vorontsov };
46863fbf49SAnton Vorontsov 
47863fbf49SAnton Vorontsov static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
48863fbf49SAnton Vorontsov {
49a19e3da5SAnton Vorontsov 	return container_of(gc, struct of_mm_gpio_chip, gc);
50863fbf49SAnton Vorontsov }
51863fbf49SAnton Vorontsov 
52f01d9075SAlexandre Courbot extern int of_get_named_gpio_flags(struct device_node *np,
53a6b09191SJohn Bonesio 		const char *list_name, int index, enum of_gpio_flags *flags);
54a6b09191SJohn Bonesio 
553208b0f0SLinus Walleij extern int of_mm_gpiochip_add_data(struct device_node *np,
563208b0f0SLinus Walleij 				   struct of_mm_gpio_chip *mm_gc,
573208b0f0SLinus Walleij 				   void *data);
583208b0f0SLinus Walleij static inline int of_mm_gpiochip_add(struct device_node *np,
593208b0f0SLinus Walleij 				     struct of_mm_gpio_chip *mm_gc)
603208b0f0SLinus Walleij {
613208b0f0SLinus Walleij 	return of_mm_gpiochip_add_data(np, mm_gc, NULL);
623208b0f0SLinus Walleij }
63d621e8baSRicardo Ribalda Delgado extern void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc);
64594fa265SGrant Likely 
65a19e3da5SAnton Vorontsov #else /* CONFIG_OF_GPIO */
66863fbf49SAnton Vorontsov 
67046e14afSAndy Shevchenko #include <linux/errno.h>
68046e14afSAndy Shevchenko 
69863fbf49SAnton Vorontsov /* Drivers may not strictly depend on the GPIO support, so let them link. */
70f01d9075SAlexandre Courbot static inline int of_get_named_gpio_flags(struct device_node *np,
71a6b09191SJohn Bonesio 		const char *list_name, int index, enum of_gpio_flags *flags)
72863fbf49SAnton Vorontsov {
73427e0dc5SArnd Bergmann 	if (flags)
74427e0dc5SArnd Bergmann 		*flags = 0;
75427e0dc5SArnd Bergmann 
76f01d9075SAlexandre Courbot 	return -ENOSYS;
77863fbf49SAnton Vorontsov }
78863fbf49SAnton Vorontsov 
79863fbf49SAnton Vorontsov #endif /* CONFIG_OF_GPIO */
80863fbf49SAnton Vorontsov 
81b908b53dSAnton Vorontsov /**
82e80beb27SGrant Likely  * of_gpio_named_count() - Count GPIOs for a device
83ff64abefSJean-Christophe PLAGNIOL-VILLARD  * @np:		device node to count GPIOs for
84e80beb27SGrant Likely  * @propname:	property name containing gpio specifier(s)
85ff64abefSJean-Christophe PLAGNIOL-VILLARD  *
86ff64abefSJean-Christophe PLAGNIOL-VILLARD  * The function returns the count of GPIOs specified for a node.
87e80beb27SGrant Likely  * Note that the empty GPIO specifiers count too. Returns either
88e80beb27SGrant Likely  *   Number of gpios defined in property,
89e80beb27SGrant Likely  *   -EINVAL for an incorrectly formed gpios property, or
90e80beb27SGrant Likely  *   -ENOENT for a missing gpios property
91ff64abefSJean-Christophe PLAGNIOL-VILLARD  *
92e80beb27SGrant Likely  * Example:
93ff64abefSJean-Christophe PLAGNIOL-VILLARD  * gpios = <0
94e80beb27SGrant Likely  *          &gpio1 1 2
95ff64abefSJean-Christophe PLAGNIOL-VILLARD  *          0
96e80beb27SGrant Likely  *          &gpio2 3 4>;
97ff64abefSJean-Christophe PLAGNIOL-VILLARD  *
98e80beb27SGrant Likely  * The above example defines four GPIOs, two of which are not specified.
99e80beb27SGrant Likely  * This function will return '4'
100ff64abefSJean-Christophe PLAGNIOL-VILLARD  */
101e80beb27SGrant Likely static inline int of_gpio_named_count(struct device_node *np, const char* propname)
102e80beb27SGrant Likely {
103e80beb27SGrant Likely 	return of_count_phandle_with_args(np, propname, "#gpio-cells");
104e80beb27SGrant Likely }
105e80beb27SGrant Likely 
106e80beb27SGrant Likely /**
107e80beb27SGrant Likely  * of_gpio_count() - Count GPIOs for a device
108e80beb27SGrant Likely  * @np:		device node to count GPIOs for
109e80beb27SGrant Likely  *
110e80beb27SGrant Likely  * Same as of_gpio_named_count, but hard coded to use the 'gpios' property
111e80beb27SGrant Likely  */
112e80beb27SGrant Likely static inline int of_gpio_count(struct device_node *np)
113ff64abefSJean-Christophe PLAGNIOL-VILLARD {
114ff64abefSJean-Christophe PLAGNIOL-VILLARD 	return of_gpio_named_count(np, "gpios");
115ff64abefSJean-Christophe PLAGNIOL-VILLARD }
116ff64abefSJean-Christophe PLAGNIOL-VILLARD 
117a6b09191SJohn Bonesio static inline int of_get_gpio_flags(struct device_node *np, int index,
118a6b09191SJohn Bonesio 		      enum of_gpio_flags *flags)
119a6b09191SJohn Bonesio {
120a6b09191SJohn Bonesio 	return of_get_named_gpio_flags(np, "gpios", index, flags);
121a6b09191SJohn Bonesio }
122a6b09191SJohn Bonesio 
123a6b09191SJohn Bonesio /**
124a6b09191SJohn Bonesio  * of_get_named_gpio() - Get a GPIO number to use with GPIO API
125a6b09191SJohn Bonesio  * @np:		device node to get GPIO from
126a6b09191SJohn Bonesio  * @propname:	Name of property containing gpio specifier(s)
127a6b09191SJohn Bonesio  * @index:	index of the GPIO
128a6b09191SJohn Bonesio  *
129a6b09191SJohn Bonesio  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
130a6b09191SJohn Bonesio  * value on the error condition.
131a6b09191SJohn Bonesio  */
132a6b09191SJohn Bonesio static inline int of_get_named_gpio(struct device_node *np,
133a6b09191SJohn Bonesio                                    const char *propname, int index)
134a6b09191SJohn Bonesio {
135a6b09191SJohn Bonesio 	return of_get_named_gpio_flags(np, propname, index, NULL);
136a6b09191SJohn Bonesio }
137a6b09191SJohn Bonesio 
138a6b09191SJohn Bonesio /**
139a6b09191SJohn Bonesio  * of_get_gpio() - Get a GPIO number to use with GPIO API
140b908b53dSAnton Vorontsov  * @np:		device node to get GPIO from
141b908b53dSAnton Vorontsov  * @index:	index of the GPIO
142b908b53dSAnton Vorontsov  *
143b908b53dSAnton Vorontsov  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
144b908b53dSAnton Vorontsov  * value on the error condition.
145b908b53dSAnton Vorontsov  */
146b908b53dSAnton Vorontsov static inline int of_get_gpio(struct device_node *np, int index)
147b908b53dSAnton Vorontsov {
148b908b53dSAnton Vorontsov 	return of_get_gpio_flags(np, index, NULL);
149b908b53dSAnton Vorontsov }
150b908b53dSAnton Vorontsov 
151863fbf49SAnton Vorontsov #endif /* __LINUX_OF_GPIO_H */
152