1 /*
2 * Consumer interface the pin control subsystem
3 *
4 * Copyright (C) 2012 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
9 *
10 * License terms: GNU General Public License (GPL) version 2
11 */
12 #ifndef __LINUX_PINCTRL_CONSUMER_H
13 #define __LINUX_PINCTRL_CONSUMER_H
14
15 #include <linux/err.h>
16 #include <linux/list.h>
17 #include <linux/seq_file.h>
18 #include <linux/pinctrl/pinctrl-state.h>
19
20 /* This struct is private to the core and should be regarded as a cookie */
21 struct pinctrl;
22 struct pinctrl_state;
23 struct device;
24
25 #ifdef CONFIG_PINCTRL
26
27 /* External interface to pin control */
28 extern int pinctrl_gpio_request(unsigned gpio);
29 extern void pinctrl_gpio_free(unsigned gpio);
30 extern int pinctrl_gpio_direction_input(unsigned gpio);
31 extern int pinctrl_gpio_direction_output(unsigned gpio);
32 extern int pinctrl_gpio_set_config(unsigned gpio, unsigned long config);
33
34 extern struct pinctrl * __must_check pinctrl_get(struct device *dev);
35 extern void pinctrl_put(struct pinctrl *p);
36 extern struct pinctrl_state * __must_check pinctrl_lookup_state(
37 struct pinctrl *p,
38 const char *name);
39 extern int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *s);
40
41 extern struct pinctrl * __must_check devm_pinctrl_get(struct device *dev);
42 extern void devm_pinctrl_put(struct pinctrl *p);
43
44 #ifdef CONFIG_PM
45 extern int pinctrl_pm_select_default_state(struct device *dev);
46 extern int pinctrl_pm_select_sleep_state(struct device *dev);
47 extern int pinctrl_pm_select_idle_state(struct device *dev);
48 #else
pinctrl_pm_select_default_state(struct device * dev)49 static inline int pinctrl_pm_select_default_state(struct device *dev)
50 {
51 return 0;
52 }
pinctrl_pm_select_sleep_state(struct device * dev)53 static inline int pinctrl_pm_select_sleep_state(struct device *dev)
54 {
55 return 0;
56 }
pinctrl_pm_select_idle_state(struct device * dev)57 static inline int pinctrl_pm_select_idle_state(struct device *dev)
58 {
59 return 0;
60 }
61 #endif
62
63 #else /* !CONFIG_PINCTRL */
64
pinctrl_gpio_request(unsigned gpio)65 static inline int pinctrl_gpio_request(unsigned gpio)
66 {
67 return 0;
68 }
69
pinctrl_gpio_free(unsigned gpio)70 static inline void pinctrl_gpio_free(unsigned gpio)
71 {
72 }
73
pinctrl_gpio_direction_input(unsigned gpio)74 static inline int pinctrl_gpio_direction_input(unsigned gpio)
75 {
76 return 0;
77 }
78
pinctrl_gpio_direction_output(unsigned gpio)79 static inline int pinctrl_gpio_direction_output(unsigned gpio)
80 {
81 return 0;
82 }
83
pinctrl_gpio_set_config(unsigned gpio,unsigned long config)84 static inline int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
85 {
86 return 0;
87 }
88
pinctrl_get(struct device * dev)89 static inline struct pinctrl * __must_check pinctrl_get(struct device *dev)
90 {
91 return NULL;
92 }
93
pinctrl_put(struct pinctrl * p)94 static inline void pinctrl_put(struct pinctrl *p)
95 {
96 }
97
pinctrl_lookup_state(struct pinctrl * p,const char * name)98 static inline struct pinctrl_state * __must_check pinctrl_lookup_state(
99 struct pinctrl *p,
100 const char *name)
101 {
102 return NULL;
103 }
104
pinctrl_select_state(struct pinctrl * p,struct pinctrl_state * s)105 static inline int pinctrl_select_state(struct pinctrl *p,
106 struct pinctrl_state *s)
107 {
108 return 0;
109 }
110
devm_pinctrl_get(struct device * dev)111 static inline struct pinctrl * __must_check devm_pinctrl_get(struct device *dev)
112 {
113 return NULL;
114 }
115
devm_pinctrl_put(struct pinctrl * p)116 static inline void devm_pinctrl_put(struct pinctrl *p)
117 {
118 }
119
pinctrl_pm_select_default_state(struct device * dev)120 static inline int pinctrl_pm_select_default_state(struct device *dev)
121 {
122 return 0;
123 }
124
pinctrl_pm_select_sleep_state(struct device * dev)125 static inline int pinctrl_pm_select_sleep_state(struct device *dev)
126 {
127 return 0;
128 }
129
pinctrl_pm_select_idle_state(struct device * dev)130 static inline int pinctrl_pm_select_idle_state(struct device *dev)
131 {
132 return 0;
133 }
134
135 #endif /* CONFIG_PINCTRL */
136
pinctrl_get_select(struct device * dev,const char * name)137 static inline struct pinctrl * __must_check pinctrl_get_select(
138 struct device *dev, const char *name)
139 {
140 struct pinctrl *p;
141 struct pinctrl_state *s;
142 int ret;
143
144 p = pinctrl_get(dev);
145 if (IS_ERR(p))
146 return p;
147
148 s = pinctrl_lookup_state(p, name);
149 if (IS_ERR(s)) {
150 pinctrl_put(p);
151 return ERR_CAST(s);
152 }
153
154 ret = pinctrl_select_state(p, s);
155 if (ret < 0) {
156 pinctrl_put(p);
157 return ERR_PTR(ret);
158 }
159
160 return p;
161 }
162
pinctrl_get_select_default(struct device * dev)163 static inline struct pinctrl * __must_check pinctrl_get_select_default(
164 struct device *dev)
165 {
166 return pinctrl_get_select(dev, PINCTRL_STATE_DEFAULT);
167 }
168
devm_pinctrl_get_select(struct device * dev,const char * name)169 static inline struct pinctrl * __must_check devm_pinctrl_get_select(
170 struct device *dev, const char *name)
171 {
172 struct pinctrl *p;
173 struct pinctrl_state *s;
174 int ret;
175
176 p = devm_pinctrl_get(dev);
177 if (IS_ERR(p))
178 return p;
179
180 s = pinctrl_lookup_state(p, name);
181 if (IS_ERR(s)) {
182 devm_pinctrl_put(p);
183 return ERR_CAST(s);
184 }
185
186 ret = pinctrl_select_state(p, s);
187 if (ret < 0) {
188 devm_pinctrl_put(p);
189 return ERR_PTR(ret);
190 }
191
192 return p;
193 }
194
devm_pinctrl_get_select_default(struct device * dev)195 static inline struct pinctrl * __must_check devm_pinctrl_get_select_default(
196 struct device *dev)
197 {
198 return devm_pinctrl_get_select(dev, PINCTRL_STATE_DEFAULT);
199 }
200
201 #endif /* __LINUX_PINCTRL_CONSUMER_H */
202