]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/pinctrl/pinconf.c
pinctrl: API changes to support multiple states per device
[mv-sheeva.git] / drivers / pinctrl / pinconf.c
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
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,
40                            unsigned long *config)
41 {
42         const struct pinconf_ops *ops = pctldev->desc->confops;
43
44         if (!ops || !ops->pin_config_get) {
45                 dev_err(pctldev->dev, "cannot get pin configuration, missing "
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
55  * @dev_name: name of the pin controller device for this pin
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  */
61 int pin_config_get(const char *dev_name, const char *name,
62                           unsigned long *config)
63 {
64         struct pinctrl_dev *pctldev;
65         int pin;
66
67         mutex_lock(&pinctrl_mutex);
68
69         pctldev = get_pinctrl_dev_from_devname(dev_name);
70         if (!pctldev) {
71                 pin = -EINVAL;
72                 goto unlock;
73         }
74
75         pin = pin_get_from_name(pctldev, name);
76         if (pin < 0)
77                 goto unlock;
78
79         pin = pin_config_get_for_pin(pctldev, pin, config);
80
81 unlock:
82         mutex_unlock(&pinctrl_mutex);
83         return pin;
84 }
85 EXPORT_SYMBOL(pin_config_get);
86
87 static int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
88                            unsigned long config)
89 {
90         const struct pinconf_ops *ops = pctldev->desc->confops;
91         int ret;
92
93         if (!ops || !ops->pin_config_set) {
94                 dev_err(pctldev->dev, "cannot configure pin, missing "
95                         "config function in driver\n");
96                 return -EINVAL;
97         }
98
99         ret = ops->pin_config_set(pctldev, pin, config);
100         if (ret) {
101                 dev_err(pctldev->dev,
102                         "unable to set pin configuration on pin %d\n", pin);
103                 return ret;
104         }
105
106         return 0;
107 }
108
109 /**
110  * pin_config_set() - set the configuration of a single pin parameter
111  * @dev_name: name of pin controller device for this pin
112  * @name: name of the pin to set the config for
113  * @config: the config in this argument will contain the desired pin state, it
114  *      can be used directly by drivers as a numeral, or it can be dereferenced
115  *      to any struct.
116  */
117 int pin_config_set(const char *dev_name, const char *name,
118                    unsigned long config)
119 {
120         struct pinctrl_dev *pctldev;
121         int pin, ret;
122
123         mutex_lock(&pinctrl_mutex);
124
125         pctldev = get_pinctrl_dev_from_devname(dev_name);
126         if (!pctldev) {
127                 ret = -EINVAL;
128                 goto unlock;
129         }
130
131         pin = pin_get_from_name(pctldev, name);
132         if (pin < 0) {
133                 ret = pin;
134                 goto unlock;
135         }
136
137         ret = pin_config_set_for_pin(pctldev, pin, config);
138
139 unlock:
140         mutex_unlock(&pinctrl_mutex);
141         return ret;
142 }
143 EXPORT_SYMBOL(pin_config_set);
144
145 int pin_config_group_get(const char *dev_name, const char *pin_group,
146                          unsigned long *config)
147 {
148         struct pinctrl_dev *pctldev;
149         const struct pinconf_ops *ops;
150         int selector, ret;
151
152         mutex_lock(&pinctrl_mutex);
153
154         pctldev = get_pinctrl_dev_from_devname(dev_name);
155         if (!pctldev) {
156                 ret = -EINVAL;
157                 goto unlock;
158         }
159         ops = pctldev->desc->confops;
160
161         if (!ops || !ops->pin_config_group_get) {
162                 dev_err(pctldev->dev, "cannot get configuration for pin "
163                         "group, missing group config get function in "
164                         "driver\n");
165                 ret = -EINVAL;
166                 goto unlock;
167         }
168
169         selector = pinctrl_get_group_selector(pctldev, pin_group);
170         if (selector < 0) {
171                 ret = selector;
172                 goto unlock;
173         }
174
175         ret = ops->pin_config_group_get(pctldev, selector, config);
176
177 unlock:
178         mutex_unlock(&pinctrl_mutex);
179         return ret;
180 }
181 EXPORT_SYMBOL(pin_config_group_get);
182
183 int pin_config_group_set(const char *dev_name, const char *pin_group,
184                          unsigned long config)
185 {
186         struct pinctrl_dev *pctldev;
187         const struct pinconf_ops *ops;
188         const struct pinctrl_ops *pctlops;
189         int selector;
190         const unsigned *pins;
191         unsigned num_pins;
192         int ret;
193         int i;
194
195         mutex_lock(&pinctrl_mutex);
196
197         pctldev = get_pinctrl_dev_from_devname(dev_name);
198         if (!pctldev) {
199                 ret = -EINVAL;
200                 goto unlock;
201         }
202         ops = pctldev->desc->confops;
203         pctlops = pctldev->desc->pctlops;
204
205         if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
206                 dev_err(pctldev->dev, "cannot configure pin group, missing "
207                         "config function in driver\n");
208                 ret = -EINVAL;
209                 goto unlock;
210         }
211
212         selector = pinctrl_get_group_selector(pctldev, pin_group);
213         if (selector < 0) {
214                 ret = selector;
215                 goto unlock;
216         }
217
218         ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
219         if (ret) {
220                 dev_err(pctldev->dev, "cannot configure pin group, error "
221                         "getting pins\n");
222                 goto unlock;
223         }
224
225         /*
226          * If the pin controller supports handling entire groups we use that
227          * capability.
228          */
229         if (ops->pin_config_group_set) {
230                 ret = ops->pin_config_group_set(pctldev, selector, config);
231                 /*
232                  * If the pin controller prefer that a certain group be handled
233                  * pin-by-pin as well, it returns -EAGAIN.
234                  */
235                 if (ret != -EAGAIN)
236                         goto unlock;
237         }
238
239         /*
240          * If the controller cannot handle entire groups, we configure each pin
241          * individually.
242          */
243         if (!ops->pin_config_set) {
244                 ret = 0;
245                 goto unlock;
246         }
247
248         for (i = 0; i < num_pins; i++) {
249                 ret = ops->pin_config_set(pctldev, pins[i], config);
250                 if (ret < 0)
251                         goto unlock;
252         }
253
254         ret = 0;
255
256 unlock:
257         mutex_unlock(&pinctrl_mutex);
258
259         return ret;
260 }
261 EXPORT_SYMBOL(pin_config_group_set);
262
263 #ifdef CONFIG_DEBUG_FS
264
265 static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
266                              struct seq_file *s, int pin)
267 {
268         const struct pinconf_ops *ops = pctldev->desc->confops;
269
270         if (ops && ops->pin_config_dbg_show)
271                 ops->pin_config_dbg_show(pctldev, s, pin);
272 }
273
274 static int pinconf_pins_show(struct seq_file *s, void *what)
275 {
276         struct pinctrl_dev *pctldev = s->private;
277         unsigned i, pin;
278
279         seq_puts(s, "Pin config settings per pin\n");
280         seq_puts(s, "Format: pin (name): pinmux setting array\n");
281
282         mutex_lock(&pinctrl_mutex);
283
284         /* The pin number can be retrived from the pin controller descriptor */
285         for (i = 0; i < pctldev->desc->npins; i++) {
286                 struct pin_desc *desc;
287
288                 pin = pctldev->desc->pins[i].number;
289                 desc = pin_desc_get(pctldev, pin);
290                 /* Skip if we cannot search the pin */
291                 if (desc == NULL)
292                         continue;
293
294                 seq_printf(s, "pin %d (%s):", pin,
295                            desc->name ? desc->name : "unnamed");
296
297                 pinconf_dump_pin(pctldev, s, pin);
298
299                 seq_printf(s, "\n");
300         }
301
302         mutex_unlock(&pinctrl_mutex);
303
304         return 0;
305 }
306
307 static void pinconf_dump_group(struct pinctrl_dev *pctldev,
308                                struct seq_file *s, unsigned selector,
309                                const char *gname)
310 {
311         const struct pinconf_ops *ops = pctldev->desc->confops;
312
313         if (ops && ops->pin_config_group_dbg_show)
314                 ops->pin_config_group_dbg_show(pctldev, s, selector);
315 }
316
317 static int pinconf_groups_show(struct seq_file *s, void *what)
318 {
319         struct pinctrl_dev *pctldev = s->private;
320         const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
321         const struct pinconf_ops *ops = pctldev->desc->confops;
322         unsigned selector = 0;
323
324         if (!ops || !ops->pin_config_group_get)
325                 return 0;
326
327         seq_puts(s, "Pin config settings per pin group\n");
328         seq_puts(s, "Format: group (name): pinmux setting array\n");
329
330         mutex_lock(&pinctrl_mutex);
331
332         while (pctlops->list_groups(pctldev, selector) >= 0) {
333                 const char *gname = pctlops->get_group_name(pctldev, selector);
334
335                 seq_printf(s, "%u (%s):", selector, gname);
336                 pinconf_dump_group(pctldev, s, selector, gname);
337                 seq_printf(s, "\n");
338
339                 selector++;
340         }
341
342         mutex_unlock(&pinctrl_mutex);
343
344         return 0;
345 }
346
347 static int pinconf_pins_open(struct inode *inode, struct file *file)
348 {
349         return single_open(file, pinconf_pins_show, inode->i_private);
350 }
351
352 static int pinconf_groups_open(struct inode *inode, struct file *file)
353 {
354         return single_open(file, pinconf_groups_show, inode->i_private);
355 }
356
357 static const struct file_operations pinconf_pins_ops = {
358         .open           = pinconf_pins_open,
359         .read           = seq_read,
360         .llseek         = seq_lseek,
361         .release        = single_release,
362 };
363
364 static const struct file_operations pinconf_groups_ops = {
365         .open           = pinconf_groups_open,
366         .read           = seq_read,
367         .llseek         = seq_lseek,
368         .release        = single_release,
369 };
370
371 void pinconf_init_device_debugfs(struct dentry *devroot,
372                          struct pinctrl_dev *pctldev)
373 {
374         debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
375                             devroot, pctldev, &pinconf_pins_ops);
376         debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
377                             devroot, pctldev, &pinconf_groups_ops);
378 }
379
380 #endif