]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/of_gpio.h
gpio: Fix build breakage
[karo-tx-linux.git] / include / linux / of_gpio.h
1 /*
2  * OF helpers for the GPIO API
3  *
4  * Copyright (c) 2007-2008  MontaVista Software, Inc.
5  *
6  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #ifndef __LINUX_OF_GPIO_H
15 #define __LINUX_OF_GPIO_H
16
17 #include <linux/compiler.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/gpio.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/of.h>
23
24 struct device_node;
25 struct gpio_desc;
26
27 /*
28  * This is Linux-specific flags. By default controllers' and Linux' mapping
29  * match, but GPIO controllers are free to translate their own flags to
30  * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
31  */
32 enum of_gpio_flags {
33         OF_GPIO_ACTIVE_LOW = 0x1,
34 };
35
36 #ifdef CONFIG_OF_GPIO
37
38 /*
39  * OF GPIO chip for memory mapped banks
40  */
41 struct of_mm_gpio_chip {
42         struct gpio_chip gc;
43         void (*save_regs)(struct of_mm_gpio_chip *mm_gc);
44         void __iomem *regs;
45 };
46
47 static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
48 {
49         return container_of(gc, struct of_mm_gpio_chip, gc);
50 }
51
52 extern struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
53                 const char *list_name, int index, enum of_gpio_flags *flags);
54
55 extern int of_mm_gpiochip_add(struct device_node *np,
56                               struct of_mm_gpio_chip *mm_gc);
57
58 extern void of_gpiochip_add(struct gpio_chip *gc);
59 extern void of_gpiochip_remove(struct gpio_chip *gc);
60 extern int of_gpio_simple_xlate(struct gpio_chip *gc,
61                                 const struct of_phandle_args *gpiospec,
62                                 u32 *flags);
63
64 #else /* CONFIG_OF_GPIO */
65
66 /* Drivers may not strictly depend on the GPIO support, so let them link. */
67 static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
68                 const char *list_name, int index, enum of_gpio_flags *flags)
69 {
70         return ERR_PTR(-ENOSYS);
71 }
72
73 static inline int of_gpio_simple_xlate(struct gpio_chip *gc,
74                                        const struct of_phandle_args *gpiospec,
75                                        u32 *flags)
76 {
77         return -ENOSYS;
78 }
79
80 static inline void of_gpiochip_add(struct gpio_chip *gc) { }
81 static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
82
83 #endif /* CONFIG_OF_GPIO */
84
85 static inline int of_get_named_gpio_flags(struct device_node *np,
86                 const char *list_name, int index, enum of_gpio_flags *flags)
87 {
88         struct gpio_desc *desc;
89         desc = of_get_named_gpiod_flags(np, list_name, index, flags);
90
91         if (IS_ERR(desc))
92                 return PTR_ERR(desc);
93         else
94                 return desc_to_gpio(desc);
95 }
96
97 /**
98  * of_gpio_named_count() - Count GPIOs for a device
99  * @np:         device node to count GPIOs for
100  * @propname:   property name containing gpio specifier(s)
101  *
102  * The function returns the count of GPIOs specified for a node.
103  * Note that the empty GPIO specifiers count too. Returns either
104  *   Number of gpios defined in property,
105  *   -EINVAL for an incorrectly formed gpios property, or
106  *   -ENOENT for a missing gpios property
107  *
108  * Example:
109  * gpios = <0
110  *          &gpio1 1 2
111  *          0
112  *          &gpio2 3 4>;
113  *
114  * The above example defines four GPIOs, two of which are not specified.
115  * This function will return '4'
116  */
117 static inline int of_gpio_named_count(struct device_node *np, const char* propname)
118 {
119         return of_count_phandle_with_args(np, propname, "#gpio-cells");
120 }
121
122 /**
123  * of_gpio_count() - Count GPIOs for a device
124  * @np:         device node to count GPIOs for
125  *
126  * Same as of_gpio_named_count, but hard coded to use the 'gpios' property
127  */
128 static inline int of_gpio_count(struct device_node *np)
129 {
130         return of_gpio_named_count(np, "gpios");
131 }
132
133 /**
134  * of_get_gpiod_flags() - Get a GPIO descriptor and flags to use with GPIO API
135  * @np:         device node to get GPIO from
136  * @index:      index of the GPIO
137  * @flags:      a flags pointer to fill in
138  *
139  * Returns GPIO descriptor to use with Linux generic GPIO API, or a errno
140  * value on the error condition. If @flags is not NULL the function also fills
141  * in flags for the GPIO.
142  */
143 static inline struct gpio_desc *of_get_gpiod_flags(struct device_node *np,
144                                         int index, enum of_gpio_flags *flags)
145 {
146         return of_get_named_gpiod_flags(np, "gpios", index, flags);
147 }
148
149 static inline int of_get_gpio_flags(struct device_node *np, int index,
150                       enum of_gpio_flags *flags)
151 {
152         return of_get_named_gpio_flags(np, "gpios", index, flags);
153 }
154
155 /**
156  * of_get_named_gpio() - Get a GPIO number to use with GPIO API
157  * @np:         device node to get GPIO from
158  * @propname:   Name of property containing gpio specifier(s)
159  * @index:      index of the GPIO
160  *
161  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
162  * value on the error condition.
163  */
164 static inline int of_get_named_gpio(struct device_node *np,
165                                    const char *propname, int index)
166 {
167         return of_get_named_gpio_flags(np, propname, index, NULL);
168 }
169
170 /**
171  * of_get_gpio() - Get a GPIO number to use with GPIO API
172  * @np:         device node to get GPIO from
173  * @index:      index of the GPIO
174  *
175  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
176  * value on the error condition.
177  */
178 static inline int of_get_gpio(struct device_node *np, int index)
179 {
180         return of_get_gpio_flags(np, index, NULL);
181 }
182
183 #endif /* __LINUX_OF_GPIO_H */