]>
Commit | Line | Data |
---|---|---|
ae6b4d85 LW |
1 | /* |
2 | * Core driver for the pin config portions of the pin control subsystem | |
3 | * | |
4 | * Copyright (C) 2011 ST-Ericsson SA | |
5 | * Written on behalf of Linaro for ST-Ericsson | |
6 | * | |
7 | * Author: Linus Walleij <linus.walleij@linaro.org> | |
8 | * | |
9 | * License terms: GNU General Public License (GPL) version 2 | |
10 | */ | |
11 | #define pr_fmt(fmt) "pinconfig core: " fmt | |
12 | ||
13 | #include <linux/kernel.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/device.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/debugfs.h> | |
19 | #include <linux/seq_file.h> | |
20 | #include <linux/pinctrl/machine.h> | |
21 | #include <linux/pinctrl/pinctrl.h> | |
22 | #include <linux/pinctrl/pinconf.h> | |
23 | #include "core.h" | |
24 | #include "pinconf.h" | |
25 | ||
2b694250 SW |
26 | int pinconf_check_ops(struct pinctrl_dev *pctldev) |
27 | { | |
28 | const struct pinconf_ops *ops = pctldev->desc->confops; | |
29 | ||
30 | /* We must be able to read out pin status */ | |
31 | if (!ops->pin_config_get && !ops->pin_config_group_get) | |
32 | return -EINVAL; | |
33 | /* We have to be able to config the pins in SOME way */ | |
34 | if (!ops->pin_config_set && !ops->pin_config_group_set) | |
35 | return -EINVAL; | |
36 | return 0; | |
37 | } | |
38 | ||
39 | static int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin, | |
ae6b4d85 LW |
40 | unsigned long *config) |
41 | { | |
42 | const struct pinconf_ops *ops = pctldev->desc->confops; | |
43 | ||
44 | if (!ops || !ops->pin_config_get) { | |
51cd24ee | 45 | dev_err(pctldev->dev, "cannot get pin configuration, missing " |
ae6b4d85 LW |
46 | "pin_config_get() function in driver\n"); |
47 | return -EINVAL; | |
48 | } | |
49 | ||
50 | return ops->pin_config_get(pctldev, pin, config); | |
51 | } | |
52 | ||
53 | /** | |
54 | * pin_config_get() - get the configuration of a single pin parameter | |
43699dea | 55 | * @dev_name: name of the pin controller device for this pin |
ae6b4d85 LW |
56 | * @name: name of the pin to get the config for |
57 | * @config: the config pointed to by this argument will be filled in with the | |
58 | * current pin state, it can be used directly by drivers as a numeral, or | |
59 | * it can be dereferenced to any struct. | |
60 | */ | |
43699dea | 61 | int pin_config_get(const char *dev_name, const char *name, |
ae6b4d85 LW |
62 | unsigned long *config) |
63 | { | |
43699dea | 64 | struct pinctrl_dev *pctldev; |
ae6b4d85 LW |
65 | int pin; |
66 | ||
9dfac4fd | 67 | pctldev = get_pinctrl_dev_from_devname(dev_name); |
43699dea SW |
68 | if (!pctldev) |
69 | return -EINVAL; | |
70 | ||
ae6b4d85 LW |
71 | pin = pin_get_from_name(pctldev, name); |
72 | if (pin < 0) | |
73 | return pin; | |
74 | ||
75 | return pin_config_get_for_pin(pctldev, pin, config); | |
76 | } | |
77 | EXPORT_SYMBOL(pin_config_get); | |
78 | ||
2b694250 | 79 | static int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin, |
ae6b4d85 LW |
80 | unsigned long config) |
81 | { | |
82 | const struct pinconf_ops *ops = pctldev->desc->confops; | |
83 | int ret; | |
84 | ||
85 | if (!ops || !ops->pin_config_set) { | |
51cd24ee | 86 | dev_err(pctldev->dev, "cannot configure pin, missing " |
ae6b4d85 LW |
87 | "config function in driver\n"); |
88 | return -EINVAL; | |
89 | } | |
90 | ||
91 | ret = ops->pin_config_set(pctldev, pin, config); | |
92 | if (ret) { | |
51cd24ee | 93 | dev_err(pctldev->dev, |
ae6b4d85 LW |
94 | "unable to set pin configuration on pin %d\n", pin); |
95 | return ret; | |
96 | } | |
97 | ||
98 | return 0; | |
99 | } | |
100 | ||
101 | /** | |
102 | * pin_config_set() - set the configuration of a single pin parameter | |
43699dea | 103 | * @dev_name: name of pin controller device for this pin |
ae6b4d85 LW |
104 | * @name: name of the pin to set the config for |
105 | * @config: the config in this argument will contain the desired pin state, it | |
106 | * can be used directly by drivers as a numeral, or it can be dereferenced | |
107 | * to any struct. | |
108 | */ | |
43699dea | 109 | int pin_config_set(const char *dev_name, const char *name, |
ae6b4d85 LW |
110 | unsigned long config) |
111 | { | |
43699dea | 112 | struct pinctrl_dev *pctldev; |
ae6b4d85 LW |
113 | int pin; |
114 | ||
9dfac4fd | 115 | pctldev = get_pinctrl_dev_from_devname(dev_name); |
43699dea SW |
116 | if (!pctldev) |
117 | return -EINVAL; | |
118 | ||
ae6b4d85 LW |
119 | pin = pin_get_from_name(pctldev, name); |
120 | if (pin < 0) | |
121 | return pin; | |
122 | ||
123 | return pin_config_set_for_pin(pctldev, pin, config); | |
124 | } | |
125 | EXPORT_SYMBOL(pin_config_set); | |
126 | ||
43699dea | 127 | int pin_config_group_get(const char *dev_name, const char *pin_group, |
ae6b4d85 LW |
128 | unsigned long *config) |
129 | { | |
43699dea SW |
130 | struct pinctrl_dev *pctldev; |
131 | const struct pinconf_ops *ops; | |
ae6b4d85 LW |
132 | int selector; |
133 | ||
9dfac4fd | 134 | pctldev = get_pinctrl_dev_from_devname(dev_name); |
43699dea SW |
135 | if (!pctldev) |
136 | return -EINVAL; | |
137 | ops = pctldev->desc->confops; | |
138 | ||
ae6b4d85 | 139 | if (!ops || !ops->pin_config_group_get) { |
51cd24ee | 140 | dev_err(pctldev->dev, "cannot get configuration for pin " |
ae6b4d85 LW |
141 | "group, missing group config get function in " |
142 | "driver\n"); | |
143 | return -EINVAL; | |
144 | } | |
145 | ||
146 | selector = pinctrl_get_group_selector(pctldev, pin_group); | |
147 | if (selector < 0) | |
148 | return selector; | |
149 | ||
150 | return ops->pin_config_group_get(pctldev, selector, config); | |
151 | } | |
152 | EXPORT_SYMBOL(pin_config_group_get); | |
153 | ||
43699dea | 154 | int pin_config_group_set(const char *dev_name, const char *pin_group, |
ae6b4d85 LW |
155 | unsigned long config) |
156 | { | |
43699dea SW |
157 | struct pinctrl_dev *pctldev; |
158 | const struct pinconf_ops *ops; | |
159 | const struct pinctrl_ops *pctlops; | |
ae6b4d85 LW |
160 | int selector; |
161 | const unsigned *pins; | |
162 | unsigned num_pins; | |
163 | int ret; | |
164 | int i; | |
165 | ||
9dfac4fd | 166 | pctldev = get_pinctrl_dev_from_devname(dev_name); |
43699dea SW |
167 | if (!pctldev) |
168 | return -EINVAL; | |
169 | ops = pctldev->desc->confops; | |
170 | pctlops = pctldev->desc->pctlops; | |
171 | ||
ae6b4d85 | 172 | if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) { |
51cd24ee | 173 | dev_err(pctldev->dev, "cannot configure pin group, missing " |
ae6b4d85 LW |
174 | "config function in driver\n"); |
175 | return -EINVAL; | |
176 | } | |
177 | ||
178 | selector = pinctrl_get_group_selector(pctldev, pin_group); | |
179 | if (selector < 0) | |
180 | return selector; | |
181 | ||
182 | ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins); | |
183 | if (ret) { | |
51cd24ee | 184 | dev_err(pctldev->dev, "cannot configure pin group, error " |
ae6b4d85 LW |
185 | "getting pins\n"); |
186 | return ret; | |
187 | } | |
188 | ||
189 | /* | |
190 | * If the pin controller supports handling entire groups we use that | |
191 | * capability. | |
192 | */ | |
193 | if (ops->pin_config_group_set) { | |
194 | ret = ops->pin_config_group_set(pctldev, selector, config); | |
195 | /* | |
196 | * If the pin controller prefer that a certain group be handled | |
197 | * pin-by-pin as well, it returns -EAGAIN. | |
198 | */ | |
199 | if (ret != -EAGAIN) | |
200 | return ret; | |
201 | } | |
202 | ||
203 | /* | |
204 | * If the controller cannot handle entire groups, we configure each pin | |
205 | * individually. | |
206 | */ | |
207 | if (!ops->pin_config_set) | |
208 | return 0; | |
209 | ||
210 | for (i = 0; i < num_pins; i++) { | |
211 | ret = ops->pin_config_set(pctldev, pins[i], config); | |
212 | if (ret < 0) | |
213 | return ret; | |
214 | } | |
215 | ||
216 | return 0; | |
217 | } | |
218 | EXPORT_SYMBOL(pin_config_group_set); | |
219 | ||
ae6b4d85 LW |
220 | #ifdef CONFIG_DEBUG_FS |
221 | ||
222 | static void pinconf_dump_pin(struct pinctrl_dev *pctldev, | |
223 | struct seq_file *s, int pin) | |
224 | { | |
225 | const struct pinconf_ops *ops = pctldev->desc->confops; | |
226 | ||
227 | if (ops && ops->pin_config_dbg_show) | |
228 | ops->pin_config_dbg_show(pctldev, s, pin); | |
229 | } | |
230 | ||
231 | static int pinconf_pins_show(struct seq_file *s, void *what) | |
232 | { | |
233 | struct pinctrl_dev *pctldev = s->private; | |
706e8520 | 234 | unsigned i, pin; |
ae6b4d85 LW |
235 | |
236 | seq_puts(s, "Pin config settings per pin\n"); | |
237 | seq_puts(s, "Format: pin (name): pinmux setting array\n"); | |
238 | ||
706e8520 | 239 | /* The pin number can be retrived from the pin controller descriptor */ |
546edd83 | 240 | for (i = 0; i < pctldev->desc->npins; i++) { |
ae6b4d85 LW |
241 | struct pin_desc *desc; |
242 | ||
706e8520 | 243 | pin = pctldev->desc->pins[i].number; |
ae6b4d85 | 244 | desc = pin_desc_get(pctldev, pin); |
706e8520 | 245 | /* Skip if we cannot search the pin */ |
ae6b4d85 LW |
246 | if (desc == NULL) |
247 | continue; | |
248 | ||
249 | seq_printf(s, "pin %d (%s):", pin, | |
250 | desc->name ? desc->name : "unnamed"); | |
251 | ||
252 | pinconf_dump_pin(pctldev, s, pin); | |
253 | ||
254 | seq_printf(s, "\n"); | |
255 | } | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
260 | static void pinconf_dump_group(struct pinctrl_dev *pctldev, | |
261 | struct seq_file *s, unsigned selector, | |
262 | const char *gname) | |
263 | { | |
264 | const struct pinconf_ops *ops = pctldev->desc->confops; | |
265 | ||
266 | if (ops && ops->pin_config_group_dbg_show) | |
267 | ops->pin_config_group_dbg_show(pctldev, s, selector); | |
268 | } | |
269 | ||
270 | static int pinconf_groups_show(struct seq_file *s, void *what) | |
271 | { | |
272 | struct pinctrl_dev *pctldev = s->private; | |
273 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; | |
274 | const struct pinconf_ops *ops = pctldev->desc->confops; | |
275 | unsigned selector = 0; | |
276 | ||
277 | if (!ops || !ops->pin_config_group_get) | |
278 | return 0; | |
279 | ||
280 | seq_puts(s, "Pin config settings per pin group\n"); | |
281 | seq_puts(s, "Format: group (name): pinmux setting array\n"); | |
282 | ||
283 | while (pctlops->list_groups(pctldev, selector) >= 0) { | |
284 | const char *gname = pctlops->get_group_name(pctldev, selector); | |
285 | ||
286 | seq_printf(s, "%u (%s):", selector, gname); | |
287 | pinconf_dump_group(pctldev, s, selector, gname); | |
288 | selector++; | |
289 | } | |
290 | ||
291 | return 0; | |
292 | } | |
293 | ||
294 | static int pinconf_pins_open(struct inode *inode, struct file *file) | |
295 | { | |
296 | return single_open(file, pinconf_pins_show, inode->i_private); | |
297 | } | |
298 | ||
299 | static int pinconf_groups_open(struct inode *inode, struct file *file) | |
300 | { | |
301 | return single_open(file, pinconf_groups_show, inode->i_private); | |
302 | } | |
303 | ||
304 | static const struct file_operations pinconf_pins_ops = { | |
305 | .open = pinconf_pins_open, | |
306 | .read = seq_read, | |
307 | .llseek = seq_lseek, | |
308 | .release = single_release, | |
309 | }; | |
310 | ||
311 | static const struct file_operations pinconf_groups_ops = { | |
312 | .open = pinconf_groups_open, | |
313 | .read = seq_read, | |
314 | .llseek = seq_lseek, | |
315 | .release = single_release, | |
316 | }; | |
317 | ||
318 | void pinconf_init_device_debugfs(struct dentry *devroot, | |
319 | struct pinctrl_dev *pctldev) | |
320 | { | |
321 | debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO, | |
322 | devroot, pctldev, &pinconf_pins_ops); | |
323 | debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO, | |
324 | devroot, pctldev, &pinconf_groups_ops); | |
325 | } | |
326 | ||
327 | #endif |