xref: /openbmc/linux/include/linux/of_gpio.h (revision ff64abef)
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 
50a6b09191SJohn Bonesio 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 
53ff64abefSJean-Christophe PLAGNIOL-VILLARD extern unsigned int of_gpio_named_count(struct device_node *np,
54ff64abefSJean-Christophe PLAGNIOL-VILLARD 					const char* propname);
55b908b53dSAnton Vorontsov 
56863fbf49SAnton Vorontsov extern int of_mm_gpiochip_add(struct device_node *np,
57863fbf49SAnton Vorontsov 			      struct of_mm_gpio_chip *mm_gc);
58594fa265SGrant Likely 
59391c970cSAnton Vorontsov extern void of_gpiochip_add(struct gpio_chip *gc);
60391c970cSAnton Vorontsov extern void of_gpiochip_remove(struct gpio_chip *gc);
61594fa265SGrant Likely extern struct gpio_chip *of_node_to_gpiochip(struct device_node *np);
6215c9a0acSGrant Likely extern int of_gpio_simple_xlate(struct gpio_chip *gc,
6315c9a0acSGrant Likely 				const struct of_phandle_args *gpiospec,
6415c9a0acSGrant Likely 				u32 *flags);
65594fa265SGrant Likely 
66a19e3da5SAnton Vorontsov #else /* CONFIG_OF_GPIO */
67863fbf49SAnton Vorontsov 
68863fbf49SAnton Vorontsov /* Drivers may not strictly depend on the GPIO support, so let them link. */
69a6b09191SJohn Bonesio static inline int of_get_named_gpio_flags(struct device_node *np,
70a6b09191SJohn Bonesio 		const char *list_name, int index, enum of_gpio_flags *flags)
71863fbf49SAnton Vorontsov {
72863fbf49SAnton Vorontsov 	return -ENOSYS;
73863fbf49SAnton Vorontsov }
74863fbf49SAnton Vorontsov 
75ff64abefSJean-Christophe PLAGNIOL-VILLARD static inline unsigned int of_gpio_named_count(struct device_node *np,
76ff64abefSJean-Christophe PLAGNIOL-VILLARD 					const char* propname)
7774982092SAnton Vorontsov {
7874982092SAnton Vorontsov 	return 0;
7974982092SAnton Vorontsov }
8074982092SAnton Vorontsov 
813038bbdfSJamie Iles static inline int of_gpio_simple_xlate(struct gpio_chip *gc,
8215c9a0acSGrant Likely 				       const struct of_phandle_args *gpiospec,
8315c9a0acSGrant Likely 				       u32 *flags)
843038bbdfSJamie Iles {
853038bbdfSJamie Iles 	return -ENOSYS;
863038bbdfSJamie Iles }
873038bbdfSJamie Iles 
88391c970cSAnton Vorontsov static inline void of_gpiochip_add(struct gpio_chip *gc) { }
89391c970cSAnton Vorontsov static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
90391c970cSAnton Vorontsov 
91863fbf49SAnton Vorontsov #endif /* CONFIG_OF_GPIO */
92863fbf49SAnton Vorontsov 
93b908b53dSAnton Vorontsov /**
94ff64abefSJean-Christophe PLAGNIOL-VILLARD  * of_gpio_count - Count GPIOs for a device
95ff64abefSJean-Christophe PLAGNIOL-VILLARD  * @np:		device node to count GPIOs for
96ff64abefSJean-Christophe PLAGNIOL-VILLARD  *
97ff64abefSJean-Christophe PLAGNIOL-VILLARD  * The function returns the count of GPIOs specified for a node.
98ff64abefSJean-Christophe PLAGNIOL-VILLARD  *
99ff64abefSJean-Christophe PLAGNIOL-VILLARD  * Note that the empty GPIO specifiers counts too. For example,
100ff64abefSJean-Christophe PLAGNIOL-VILLARD  *
101ff64abefSJean-Christophe PLAGNIOL-VILLARD  * gpios = <0
102ff64abefSJean-Christophe PLAGNIOL-VILLARD  *          &pio1 1 2
103ff64abefSJean-Christophe PLAGNIOL-VILLARD  *          0
104ff64abefSJean-Christophe PLAGNIOL-VILLARD  *          &pio2 3 4>;
105ff64abefSJean-Christophe PLAGNIOL-VILLARD  *
106ff64abefSJean-Christophe PLAGNIOL-VILLARD  * defines four GPIOs (so this function will return 4), two of which
107ff64abefSJean-Christophe PLAGNIOL-VILLARD  * are not specified.
108ff64abefSJean-Christophe PLAGNIOL-VILLARD  */
109ff64abefSJean-Christophe PLAGNIOL-VILLARD static inline unsigned int of_gpio_count(struct device_node *np)
110ff64abefSJean-Christophe PLAGNIOL-VILLARD {
111ff64abefSJean-Christophe PLAGNIOL-VILLARD 	return of_gpio_named_count(np, "gpios");
112ff64abefSJean-Christophe PLAGNIOL-VILLARD }
113ff64abefSJean-Christophe PLAGNIOL-VILLARD 
114ff64abefSJean-Christophe PLAGNIOL-VILLARD /**
115a6b09191SJohn Bonesio  * of_get_gpio_flags() - Get a GPIO number and flags to use with GPIO API
116a6b09191SJohn Bonesio  * @np:		device node to get GPIO from
117a6b09191SJohn Bonesio  * @index:	index of the GPIO
118a6b09191SJohn Bonesio  * @flags:	a flags pointer to fill in
119a6b09191SJohn Bonesio  *
120a6b09191SJohn Bonesio  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
121a6b09191SJohn Bonesio  * value on the error condition. If @flags is not NULL the function also fills
122a6b09191SJohn Bonesio  * in flags for the GPIO.
123a6b09191SJohn Bonesio  */
124a6b09191SJohn Bonesio static inline int of_get_gpio_flags(struct device_node *np, int index,
125a6b09191SJohn Bonesio 		      enum of_gpio_flags *flags)
126a6b09191SJohn Bonesio {
127a6b09191SJohn Bonesio 	return of_get_named_gpio_flags(np, "gpios", index, flags);
128a6b09191SJohn Bonesio }
129a6b09191SJohn Bonesio 
130a6b09191SJohn Bonesio /**
131a6b09191SJohn Bonesio  * of_get_named_gpio() - Get a GPIO number to use with GPIO API
132a6b09191SJohn Bonesio  * @np:		device node to get GPIO from
133a6b09191SJohn Bonesio  * @propname:	Name of property containing gpio specifier(s)
134a6b09191SJohn Bonesio  * @index:	index of the GPIO
135a6b09191SJohn Bonesio  *
136a6b09191SJohn Bonesio  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
137a6b09191SJohn Bonesio  * value on the error condition.
138a6b09191SJohn Bonesio  */
139a6b09191SJohn Bonesio static inline int of_get_named_gpio(struct device_node *np,
140a6b09191SJohn Bonesio                                    const char *propname, int index)
141a6b09191SJohn Bonesio {
142a6b09191SJohn Bonesio 	return of_get_named_gpio_flags(np, propname, index, NULL);
143a6b09191SJohn Bonesio }
144a6b09191SJohn Bonesio 
145a6b09191SJohn Bonesio /**
146a6b09191SJohn Bonesio  * of_get_gpio() - Get a GPIO number to use with GPIO API
147b908b53dSAnton Vorontsov  * @np:		device node to get GPIO from
148b908b53dSAnton Vorontsov  * @index:	index of the GPIO
149b908b53dSAnton Vorontsov  *
150b908b53dSAnton Vorontsov  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
151b908b53dSAnton Vorontsov  * value on the error condition.
152b908b53dSAnton Vorontsov  */
153b908b53dSAnton Vorontsov static inline int of_get_gpio(struct device_node *np, int index)
154b908b53dSAnton Vorontsov {
155b908b53dSAnton Vorontsov 	return of_get_gpio_flags(np, index, NULL);
156b908b53dSAnton Vorontsov }
157b908b53dSAnton Vorontsov 
158863fbf49SAnton Vorontsov #endif /* __LINUX_OF_GPIO_H */
159