]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/pinctrl/pinconf.c
pinctrl: fix the pin descriptor kerneldoc
[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         pctldev = get_pinctrl_dev_from_devname(dev_name);
68         if (!pctldev)
69                 return -EINVAL;
70
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
79 static int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
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) {
86                 dev_err(pctldev->dev, "cannot configure pin, missing "
87                         "config function in driver\n");
88                 return -EINVAL;
89         }
90
91         ret = ops->pin_config_set(pctldev, pin, config);
92         if (ret) {
93                 dev_err(pctldev->dev,
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
103  * @dev_name: name of pin controller device for this pin
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  */
109 int pin_config_set(const char *dev_name, const char *name,
110                    unsigned long config)
111 {
112         struct pinctrl_dev *pctldev;
113         int pin;
114
115         pctldev = get_pinctrl_dev_from_devname(dev_name);
116         if (!pctldev)
117                 return -EINVAL;
118
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
127 int pin_config_group_get(const char *dev_name, const char *pin_group,
128                          unsigned long *config)
129 {
130         struct pinctrl_dev *pctldev;
131         const struct pinconf_ops *ops;
132         int selector;
133
134         pctldev = get_pinctrl_dev_from_devname(dev_name);
135         if (!pctldev)
136                 return -EINVAL;
137         ops = pctldev->desc->confops;
138
139         if (!ops || !ops->pin_config_group_get) {
140                 dev_err(pctldev->dev, "cannot get configuration for pin "
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
154 int pin_config_group_set(const char *dev_name, const char *pin_group,
155                          unsigned long config)
156 {
157         struct pinctrl_dev *pctldev;
158         const struct pinconf_ops *ops;
159         const struct pinctrl_ops *pctlops;
160         int selector;
161         const unsigned *pins;
162         unsigned num_pins;
163         int ret;
164         int i;
165
166         pctldev = get_pinctrl_dev_from_devname(dev_name);
167         if (!pctldev)
168                 return -EINVAL;
169         ops = pctldev->desc->confops;
170         pctlops = pctldev->desc->pctlops;
171
172         if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
173                 dev_err(pctldev->dev, "cannot configure pin group, missing "
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) {
184                 dev_err(pctldev->dev, "cannot configure pin group, error "
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
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;
234         unsigned i, pin;
235
236         seq_puts(s, "Pin config settings per pin\n");
237         seq_puts(s, "Format: pin (name): pinmux setting array\n");
238
239         /* The pin number can be retrived from the pin controller descriptor */
240         for (i = 0; i < pctldev->desc->npins; i++) {
241                 struct pin_desc *desc;
242
243                 pin = pctldev->desc->pins[i].number;
244                 desc = pin_desc_get(pctldev, pin);
245                 /* Skip if we cannot search the pin */
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                 seq_printf(s, "\n");
289
290                 selector++;
291         }
292
293         return 0;
294 }
295
296 static int pinconf_pins_open(struct inode *inode, struct file *file)
297 {
298         return single_open(file, pinconf_pins_show, inode->i_private);
299 }
300
301 static int pinconf_groups_open(struct inode *inode, struct file *file)
302 {
303         return single_open(file, pinconf_groups_show, inode->i_private);
304 }
305
306 static const struct file_operations pinconf_pins_ops = {
307         .open           = pinconf_pins_open,
308         .read           = seq_read,
309         .llseek         = seq_lseek,
310         .release        = single_release,
311 };
312
313 static const struct file_operations pinconf_groups_ops = {
314         .open           = pinconf_groups_open,
315         .read           = seq_read,
316         .llseek         = seq_lseek,
317         .release        = single_release,
318 };
319
320 void pinconf_init_device_debugfs(struct dentry *devroot,
321                          struct pinctrl_dev *pctldev)
322 {
323         debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
324                             devroot, pctldev, &pinconf_pins_ops);
325         debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
326                             devroot, pctldev, &pinconf_groups_ops);
327 }
328
329 #endif