xref: /openbmc/linux/include/linux/of_gpio.h (revision d621e8ba)
1863fbf49SAnton Vorontsov /*
2863fbf49SAnton Vorontsov  * OF helpers for the GPIO API
3863fbf49SAnton Vorontsov  *
4863fbf49SAnton Vorontsov  * Copyright (c) 2007-2008  MontaVista Software, Inc.
5863fbf49SAnton Vorontsov  *
6863fbf49SAnton Vorontsov  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
7863fbf49SAnton Vorontsov  *
8863fbf49SAnton Vorontsov  * This program is free software; you can redistribute it and/or modify
9863fbf49SAnton Vorontsov  * it under the terms of the GNU General Public License as published by
10863fbf49SAnton Vorontsov  * the Free Software Foundation; either version 2 of the License, or
11863fbf49SAnton Vorontsov  * (at your option) any later version.
12863fbf49SAnton Vorontsov  */
13863fbf49SAnton Vorontsov 
14863fbf49SAnton Vorontsov #ifndef __LINUX_OF_GPIO_H
15863fbf49SAnton Vorontsov #define __LINUX_OF_GPIO_H
16863fbf49SAnton Vorontsov 
17b908b53dSAnton Vorontsov #include <linux/compiler.h>
18b908b53dSAnton Vorontsov #include <linux/kernel.h>
19863fbf49SAnton Vorontsov #include <linux/errno.h>
2018ad7a61SWolfgang Grandegger #include <linux/gpio.h>
2115c9a0acSGrant Likely #include <linux/of.h>
22863fbf49SAnton Vorontsov 
23b908b53dSAnton Vorontsov struct device_node;
24b908b53dSAnton Vorontsov 
25b908b53dSAnton Vorontsov /*
26b908b53dSAnton Vorontsov  * This is Linux-specific flags. By default controllers' and Linux' mapping
27b908b53dSAnton Vorontsov  * match, but GPIO controllers are free to translate their own flags to
28b908b53dSAnton Vorontsov  * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
29b908b53dSAnton Vorontsov  */
30b908b53dSAnton Vorontsov enum of_gpio_flags {
31b908b53dSAnton Vorontsov 	OF_GPIO_ACTIVE_LOW = 0x1,
32b908b53dSAnton Vorontsov };
33b908b53dSAnton Vorontsov 
34863fbf49SAnton Vorontsov #ifdef CONFIG_OF_GPIO
35863fbf49SAnton Vorontsov 
36863fbf49SAnton Vorontsov /*
37863fbf49SAnton Vorontsov  * OF GPIO chip for memory mapped banks
38863fbf49SAnton Vorontsov  */
39863fbf49SAnton Vorontsov struct of_mm_gpio_chip {
40a19e3da5SAnton Vorontsov 	struct gpio_chip gc;
41863fbf49SAnton Vorontsov 	void (*save_regs)(struct of_mm_gpio_chip *mm_gc);
42863fbf49SAnton Vorontsov 	void __iomem *regs;
43863fbf49SAnton Vorontsov };
44863fbf49SAnton Vorontsov 
45863fbf49SAnton Vorontsov static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
46863fbf49SAnton Vorontsov {
47a19e3da5SAnton Vorontsov 	return container_of(gc, struct of_mm_gpio_chip, gc);
48863fbf49SAnton Vorontsov }
49863fbf49SAnton Vorontsov 
50f01d9075SAlexandre Courbot extern int of_get_named_gpio_flags(struct device_node *np,
51a6b09191SJohn Bonesio 		const char *list_name, int index, enum of_gpio_flags *flags);
52a6b09191SJohn Bonesio 
53863fbf49SAnton Vorontsov extern int of_mm_gpiochip_add(struct device_node *np,
54863fbf49SAnton Vorontsov 			      struct of_mm_gpio_chip *mm_gc);
55d621e8baSRicardo Ribalda Delgado extern void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc);
56594fa265SGrant Likely 
57391c970cSAnton Vorontsov extern void of_gpiochip_add(struct gpio_chip *gc);
58391c970cSAnton Vorontsov extern void of_gpiochip_remove(struct gpio_chip *gc);
5915c9a0acSGrant Likely extern int of_gpio_simple_xlate(struct gpio_chip *gc,
6015c9a0acSGrant Likely 				const struct of_phandle_args *gpiospec,
6115c9a0acSGrant Likely 				u32 *flags);
62594fa265SGrant Likely 
63a19e3da5SAnton Vorontsov #else /* CONFIG_OF_GPIO */
64863fbf49SAnton Vorontsov 
65863fbf49SAnton Vorontsov /* Drivers may not strictly depend on the GPIO support, so let them link. */
66f01d9075SAlexandre Courbot static inline int of_get_named_gpio_flags(struct device_node *np,
67a6b09191SJohn Bonesio 		const char *list_name, int index, enum of_gpio_flags *flags)
68863fbf49SAnton Vorontsov {
69f01d9075SAlexandre Courbot 	return -ENOSYS;
70863fbf49SAnton Vorontsov }
71863fbf49SAnton Vorontsov 
723038bbdfSJamie Iles static inline int of_gpio_simple_xlate(struct gpio_chip *gc,
7315c9a0acSGrant Likely 				       const struct of_phandle_args *gpiospec,
7415c9a0acSGrant Likely 				       u32 *flags)
753038bbdfSJamie Iles {
763038bbdfSJamie Iles 	return -ENOSYS;
773038bbdfSJamie Iles }
783038bbdfSJamie Iles 
79391c970cSAnton Vorontsov static inline void of_gpiochip_add(struct gpio_chip *gc) { }
80391c970cSAnton Vorontsov static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
81391c970cSAnton Vorontsov 
82863fbf49SAnton Vorontsov #endif /* CONFIG_OF_GPIO */
83863fbf49SAnton Vorontsov 
84b908b53dSAnton Vorontsov /**
85e80beb27SGrant Likely  * of_gpio_named_count() - Count GPIOs for a device
86ff64abefSJean-Christophe PLAGNIOL-VILLARD  * @np:		device node to count GPIOs for
87e80beb27SGrant Likely  * @propname:	property name containing gpio specifier(s)
88ff64abefSJean-Christophe PLAGNIOL-VILLARD  *
89ff64abefSJean-Christophe PLAGNIOL-VILLARD  * The function returns the count of GPIOs specified for a node.
90e80beb27SGrant Likely  * Note that the empty GPIO specifiers count too. Returns either
91e80beb27SGrant Likely  *   Number of gpios defined in property,
92e80beb27SGrant Likely  *   -EINVAL for an incorrectly formed gpios property, or
93e80beb27SGrant Likely  *   -ENOENT for a missing gpios property
94ff64abefSJean-Christophe PLAGNIOL-VILLARD  *
95e80beb27SGrant Likely  * Example:
96ff64abefSJean-Christophe PLAGNIOL-VILLARD  * gpios = <0
97e80beb27SGrant Likely  *          &gpio1 1 2
98ff64abefSJean-Christophe PLAGNIOL-VILLARD  *          0
99e80beb27SGrant Likely  *          &gpio2 3 4>;
100ff64abefSJean-Christophe PLAGNIOL-VILLARD  *
101e80beb27SGrant Likely  * The above example defines four GPIOs, two of which are not specified.
102e80beb27SGrant Likely  * This function will return '4'
103ff64abefSJean-Christophe PLAGNIOL-VILLARD  */
104e80beb27SGrant Likely static inline int of_gpio_named_count(struct device_node *np, const char* propname)
105e80beb27SGrant Likely {
106e80beb27SGrant Likely 	return of_count_phandle_with_args(np, propname, "#gpio-cells");
107e80beb27SGrant Likely }
108e80beb27SGrant Likely 
109e80beb27SGrant Likely /**
110e80beb27SGrant Likely  * of_gpio_count() - Count GPIOs for a device
111e80beb27SGrant Likely  * @np:		device node to count GPIOs for
112e80beb27SGrant Likely  *
113e80beb27SGrant Likely  * Same as of_gpio_named_count, but hard coded to use the 'gpios' property
114e80beb27SGrant Likely  */
115e80beb27SGrant Likely static inline int of_gpio_count(struct device_node *np)
116ff64abefSJean-Christophe PLAGNIOL-VILLARD {
117ff64abefSJean-Christophe PLAGNIOL-VILLARD 	return of_gpio_named_count(np, "gpios");
118ff64abefSJean-Christophe PLAGNIOL-VILLARD }
119ff64abefSJean-Christophe PLAGNIOL-VILLARD 
120a6b09191SJohn Bonesio static inline int of_get_gpio_flags(struct device_node *np, int index,
121a6b09191SJohn Bonesio 		      enum of_gpio_flags *flags)
122a6b09191SJohn Bonesio {
123a6b09191SJohn Bonesio 	return of_get_named_gpio_flags(np, "gpios", index, flags);
124a6b09191SJohn Bonesio }
125a6b09191SJohn Bonesio 
126a6b09191SJohn Bonesio /**
127a6b09191SJohn Bonesio  * of_get_named_gpio() - Get a GPIO number to use with GPIO API
128a6b09191SJohn Bonesio  * @np:		device node to get GPIO from
129a6b09191SJohn Bonesio  * @propname:	Name of property containing gpio specifier(s)
130a6b09191SJohn Bonesio  * @index:	index of the GPIO
131a6b09191SJohn Bonesio  *
132a6b09191SJohn Bonesio  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
133a6b09191SJohn Bonesio  * value on the error condition.
134a6b09191SJohn Bonesio  */
135a6b09191SJohn Bonesio static inline int of_get_named_gpio(struct device_node *np,
136a6b09191SJohn Bonesio                                    const char *propname, int index)
137a6b09191SJohn Bonesio {
138a6b09191SJohn Bonesio 	return of_get_named_gpio_flags(np, propname, index, NULL);
139a6b09191SJohn Bonesio }
140a6b09191SJohn Bonesio 
141a6b09191SJohn Bonesio /**
142a6b09191SJohn Bonesio  * of_get_gpio() - Get a GPIO number to use with GPIO API
143b908b53dSAnton Vorontsov  * @np:		device node to get GPIO from
144b908b53dSAnton Vorontsov  * @index:	index of the GPIO
145b908b53dSAnton Vorontsov  *
146b908b53dSAnton Vorontsov  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
147b908b53dSAnton Vorontsov  * value on the error condition.
148b908b53dSAnton Vorontsov  */
149b908b53dSAnton Vorontsov static inline int of_get_gpio(struct device_node *np, int index)
150b908b53dSAnton Vorontsov {
151b908b53dSAnton Vorontsov 	return of_get_gpio_flags(np, index, NULL);
152b908b53dSAnton Vorontsov }
153b908b53dSAnton Vorontsov 
154863fbf49SAnton Vorontsov #endif /* __LINUX_OF_GPIO_H */
155