pinctrl: API changes to support multiple states per device
[deliverable/linux.git] / drivers / pinctrl / pinconf.c
CommitLineData
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
26int 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
39static 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 61int 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
57b676f9
SW
67 mutex_lock(&pinctrl_mutex);
68
9dfac4fd 69 pctldev = get_pinctrl_dev_from_devname(dev_name);
57b676f9
SW
70 if (!pctldev) {
71 pin = -EINVAL;
72 goto unlock;
73 }
43699dea 74
ae6b4d85
LW
75 pin = pin_get_from_name(pctldev, name);
76 if (pin < 0)
57b676f9 77 goto unlock;
ae6b4d85 78
57b676f9
SW
79 pin = pin_config_get_for_pin(pctldev, pin, config);
80
81unlock:
82 mutex_unlock(&pinctrl_mutex);
83 return pin;
ae6b4d85
LW
84}
85EXPORT_SYMBOL(pin_config_get);
86
2b694250 87static int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
ae6b4d85
LW
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) {
51cd24ee 94 dev_err(pctldev->dev, "cannot configure pin, missing "
ae6b4d85
LW
95 "config function in driver\n");
96 return -EINVAL;
97 }
98
99 ret = ops->pin_config_set(pctldev, pin, config);
100 if (ret) {
51cd24ee 101 dev_err(pctldev->dev,
ae6b4d85
LW
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
43699dea 111 * @dev_name: name of pin controller device for this pin
ae6b4d85
LW
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 */
43699dea 117int pin_config_set(const char *dev_name, const char *name,
ae6b4d85
LW
118 unsigned long config)
119{
43699dea 120 struct pinctrl_dev *pctldev;
57b676f9
SW
121 int pin, ret;
122
123 mutex_lock(&pinctrl_mutex);
ae6b4d85 124
9dfac4fd 125 pctldev = get_pinctrl_dev_from_devname(dev_name);
57b676f9
SW
126 if (!pctldev) {
127 ret = -EINVAL;
128 goto unlock;
129 }
43699dea 130
ae6b4d85 131 pin = pin_get_from_name(pctldev, name);
57b676f9
SW
132 if (pin < 0) {
133 ret = pin;
134 goto unlock;
135 }
136
137 ret = pin_config_set_for_pin(pctldev, pin, config);
ae6b4d85 138
57b676f9
SW
139unlock:
140 mutex_unlock(&pinctrl_mutex);
141 return ret;
ae6b4d85
LW
142}
143EXPORT_SYMBOL(pin_config_set);
144
43699dea 145int pin_config_group_get(const char *dev_name, const char *pin_group,
ae6b4d85
LW
146 unsigned long *config)
147{
43699dea
SW
148 struct pinctrl_dev *pctldev;
149 const struct pinconf_ops *ops;
57b676f9
SW
150 int selector, ret;
151
152 mutex_lock(&pinctrl_mutex);
ae6b4d85 153
9dfac4fd 154 pctldev = get_pinctrl_dev_from_devname(dev_name);
57b676f9
SW
155 if (!pctldev) {
156 ret = -EINVAL;
157 goto unlock;
158 }
43699dea
SW
159 ops = pctldev->desc->confops;
160
ae6b4d85 161 if (!ops || !ops->pin_config_group_get) {
51cd24ee 162 dev_err(pctldev->dev, "cannot get configuration for pin "
ae6b4d85
LW
163 "group, missing group config get function in "
164 "driver\n");
57b676f9
SW
165 ret = -EINVAL;
166 goto unlock;
ae6b4d85
LW
167 }
168
169 selector = pinctrl_get_group_selector(pctldev, pin_group);
57b676f9
SW
170 if (selector < 0) {
171 ret = selector;
172 goto unlock;
173 }
ae6b4d85 174
57b676f9
SW
175 ret = ops->pin_config_group_get(pctldev, selector, config);
176
177unlock:
178 mutex_unlock(&pinctrl_mutex);
179 return ret;
ae6b4d85
LW
180}
181EXPORT_SYMBOL(pin_config_group_get);
182
43699dea 183int pin_config_group_set(const char *dev_name, const char *pin_group,
ae6b4d85
LW
184 unsigned long config)
185{
43699dea
SW
186 struct pinctrl_dev *pctldev;
187 const struct pinconf_ops *ops;
188 const struct pinctrl_ops *pctlops;
ae6b4d85
LW
189 int selector;
190 const unsigned *pins;
191 unsigned num_pins;
192 int ret;
193 int i;
194
57b676f9
SW
195 mutex_lock(&pinctrl_mutex);
196
9dfac4fd 197 pctldev = get_pinctrl_dev_from_devname(dev_name);
57b676f9
SW
198 if (!pctldev) {
199 ret = -EINVAL;
200 goto unlock;
201 }
43699dea
SW
202 ops = pctldev->desc->confops;
203 pctlops = pctldev->desc->pctlops;
204
ae6b4d85 205 if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
51cd24ee 206 dev_err(pctldev->dev, "cannot configure pin group, missing "
ae6b4d85 207 "config function in driver\n");
57b676f9
SW
208 ret = -EINVAL;
209 goto unlock;
ae6b4d85
LW
210 }
211
212 selector = pinctrl_get_group_selector(pctldev, pin_group);
57b676f9
SW
213 if (selector < 0) {
214 ret = selector;
215 goto unlock;
216 }
ae6b4d85
LW
217
218 ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
219 if (ret) {
51cd24ee 220 dev_err(pctldev->dev, "cannot configure pin group, error "
ae6b4d85 221 "getting pins\n");
57b676f9 222 goto unlock;
ae6b4d85
LW
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)
57b676f9 236 goto unlock;
ae6b4d85
LW
237 }
238
239 /*
240 * If the controller cannot handle entire groups, we configure each pin
241 * individually.
242 */
57b676f9
SW
243 if (!ops->pin_config_set) {
244 ret = 0;
245 goto unlock;
246 }
ae6b4d85
LW
247
248 for (i = 0; i < num_pins; i++) {
249 ret = ops->pin_config_set(pctldev, pins[i], config);
250 if (ret < 0)
57b676f9 251 goto unlock;
ae6b4d85
LW
252 }
253
57b676f9
SW
254 ret = 0;
255
256unlock:
257 mutex_unlock(&pinctrl_mutex);
258
259 return ret;
ae6b4d85
LW
260}
261EXPORT_SYMBOL(pin_config_group_set);
262
ae6b4d85
LW
263#ifdef CONFIG_DEBUG_FS
264
265static 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
274static int pinconf_pins_show(struct seq_file *s, void *what)
275{
276 struct pinctrl_dev *pctldev = s->private;
706e8520 277 unsigned i, pin;
ae6b4d85
LW
278
279 seq_puts(s, "Pin config settings per pin\n");
280 seq_puts(s, "Format: pin (name): pinmux setting array\n");
281
57b676f9
SW
282 mutex_lock(&pinctrl_mutex);
283
706e8520 284 /* The pin number can be retrived from the pin controller descriptor */
546edd83 285 for (i = 0; i < pctldev->desc->npins; i++) {
ae6b4d85
LW
286 struct pin_desc *desc;
287
706e8520 288 pin = pctldev->desc->pins[i].number;
ae6b4d85 289 desc = pin_desc_get(pctldev, pin);
706e8520 290 /* Skip if we cannot search the pin */
ae6b4d85
LW
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
57b676f9
SW
302 mutex_unlock(&pinctrl_mutex);
303
ae6b4d85
LW
304 return 0;
305}
306
307static 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
317static 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
57b676f9
SW
330 mutex_lock(&pinctrl_mutex);
331
ae6b4d85
LW
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);
f7b9006f
SW
337 seq_printf(s, "\n");
338
ae6b4d85
LW
339 selector++;
340 }
341
57b676f9
SW
342 mutex_unlock(&pinctrl_mutex);
343
ae6b4d85
LW
344 return 0;
345}
346
347static int pinconf_pins_open(struct inode *inode, struct file *file)
348{
349 return single_open(file, pinconf_pins_show, inode->i_private);
350}
351
352static int pinconf_groups_open(struct inode *inode, struct file *file)
353{
354 return single_open(file, pinconf_groups_show, inode->i_private);
355}
356
357static 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
364static 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
371void 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
This page took 0.046525 seconds and 5 git commands to generate.