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 */
21struct pinctrl;
22struct pinctrl_state;
23struct device;
24
25#ifdef CONFIG_PINCTRL
26
27/* External interface to pin control */
28extern int pinctrl_gpio_request(unsigned gpio);
29extern void pinctrl_gpio_free(unsigned gpio);
30extern int pinctrl_gpio_direction_input(unsigned gpio);
31extern int pinctrl_gpio_direction_output(unsigned gpio);
32extern int pinctrl_gpio_set_config(unsigned gpio, unsigned long config);
33
34extern struct pinctrl * __must_check pinctrl_get(struct device *dev);
35extern void pinctrl_put(struct pinctrl *p);
36extern struct pinctrl_state * __must_check pinctrl_lookup_state(
37 struct pinctrl *p,
38 const char *name);
39extern int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *s);
40
41extern struct pinctrl * __must_check devm_pinctrl_get(struct device *dev);
42extern void devm_pinctrl_put(struct pinctrl *p);
43
44#ifdef CONFIG_PM
45extern int pinctrl_pm_select_default_state(struct device *dev);
46extern int pinctrl_pm_select_sleep_state(struct device *dev);
47extern int pinctrl_pm_select_idle_state(struct device *dev);
48#else
49static inline int pinctrl_pm_select_default_state(struct device *dev)
50{
51 return 0;
52}
53static inline int pinctrl_pm_select_sleep_state(struct device *dev)
54{
55 return 0;
56}
57static inline int pinctrl_pm_select_idle_state(struct device *dev)
58{
59 return 0;
60}
61#endif
62
63#else /* !CONFIG_PINCTRL */
64
65static inline int pinctrl_gpio_request(unsigned gpio)
66{
67 return 0;
68}
69
70static inline void pinctrl_gpio_free(unsigned gpio)
71{
72}
73
74static inline int pinctrl_gpio_direction_input(unsigned gpio)
75{
76 return 0;
77}
78
79static inline int pinctrl_gpio_direction_output(unsigned gpio)
80{
81 return 0;
82}
83
84static inline int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
85{
86 return 0;
87}
88
89static inline struct pinctrl * __must_check pinctrl_get(struct device *dev)
90{
91 return NULL;
92}
93
94static inline void pinctrl_put(struct pinctrl *p)
95{
96}
97
98static inline struct pinctrl_state * __must_check pinctrl_lookup_state(
99 struct pinctrl *p,
100 const char *name)
101{
102 return NULL;
103}
104
105static inline int pinctrl_select_state(struct pinctrl *p,
106 struct pinctrl_state *s)
107{
108 return 0;
109}
110
111static inline struct pinctrl * __must_check devm_pinctrl_get(struct device *dev)
112{
113 return NULL;
114}
115
116static inline void devm_pinctrl_put(struct pinctrl *p)
117{
118}
119
120static inline int pinctrl_pm_select_default_state(struct device *dev)
121{
122 return 0;
123}
124
125static inline int pinctrl_pm_select_sleep_state(struct device *dev)
126{
127 return 0;
128}
129
130static inline int pinctrl_pm_select_idle_state(struct device *dev)
131{
132 return 0;
133}
134
135#endif /* CONFIG_PINCTRL */
136
137static 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
163static 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
169static 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
195static 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