blob: e0a453790a40f77ad17ce49b200d0be45e8ef8df [file] [log] [blame]
Linus Walleijae6b4d82011-10-19 18:14:33 +02001/*
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
Stephen Warren2b694252012-02-19 23:45:46 -070026int 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,
Linus Walleijae6b4d82011-10-19 18:14:33 +020040 unsigned long *config)
41{
42 const struct pinconf_ops *ops = pctldev->desc->confops;
43
44 if (!ops || !ops->pin_config_get) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070045 dev_err(pctldev->dev, "cannot get pin configuration, missing "
Linus Walleijae6b4d82011-10-19 18:14:33 +020046 "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
Stephen Warren43699de2011-12-15 16:57:17 -070055 * @dev_name: name of the pin controller device for this pin
Linus Walleijae6b4d82011-10-19 18:14:33 +020056 * @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 */
Stephen Warren43699de2011-12-15 16:57:17 -070061int pin_config_get(const char *dev_name, const char *name,
Linus Walleijae6b4d82011-10-19 18:14:33 +020062 unsigned long *config)
63{
Stephen Warren43699de2011-12-15 16:57:17 -070064 struct pinctrl_dev *pctldev;
Linus Walleijae6b4d82011-10-19 18:14:33 +020065 int pin;
66
Stephen Warren57b676f2012-03-02 13:05:44 -070067 mutex_lock(&pinctrl_mutex);
68
Linus Walleij9dfac4f2012-02-01 18:02:47 +010069 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -070070 if (!pctldev) {
71 pin = -EINVAL;
72 goto unlock;
73 }
Stephen Warren43699de2011-12-15 16:57:17 -070074
Linus Walleijae6b4d82011-10-19 18:14:33 +020075 pin = pin_get_from_name(pctldev, name);
76 if (pin < 0)
Stephen Warren57b676f2012-03-02 13:05:44 -070077 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +020078
Stephen Warren57b676f2012-03-02 13:05:44 -070079 pin = pin_config_get_for_pin(pctldev, pin, config);
80
81unlock:
82 mutex_unlock(&pinctrl_mutex);
83 return pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +020084}
85EXPORT_SYMBOL(pin_config_get);
86
Stephen Warren2b694252012-02-19 23:45:46 -070087static int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
Linus Walleijae6b4d82011-10-19 18:14:33 +020088 unsigned long config)
89{
90 const struct pinconf_ops *ops = pctldev->desc->confops;
91 int ret;
92
93 if (!ops || !ops->pin_config_set) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070094 dev_err(pctldev->dev, "cannot configure pin, missing "
Linus Walleijae6b4d82011-10-19 18:14:33 +020095 "config function in driver\n");
96 return -EINVAL;
97 }
98
99 ret = ops->pin_config_set(pctldev, pin, config);
100 if (ret) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700101 dev_err(pctldev->dev,
Linus Walleijae6b4d82011-10-19 18:14:33 +0200102 "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
Stephen Warren43699de2011-12-15 16:57:17 -0700111 * @dev_name: name of pin controller device for this pin
Linus Walleijae6b4d82011-10-19 18:14:33 +0200112 * @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 */
Stephen Warren43699de2011-12-15 16:57:17 -0700117int pin_config_set(const char *dev_name, const char *name,
Linus Walleijae6b4d82011-10-19 18:14:33 +0200118 unsigned long config)
119{
Stephen Warren43699de2011-12-15 16:57:17 -0700120 struct pinctrl_dev *pctldev;
Stephen Warren57b676f2012-03-02 13:05:44 -0700121 int pin, ret;
122
123 mutex_lock(&pinctrl_mutex);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200124
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100125 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -0700126 if (!pctldev) {
127 ret = -EINVAL;
128 goto unlock;
129 }
Stephen Warren43699de2011-12-15 16:57:17 -0700130
Linus Walleijae6b4d82011-10-19 18:14:33 +0200131 pin = pin_get_from_name(pctldev, name);
Stephen Warren57b676f2012-03-02 13:05:44 -0700132 if (pin < 0) {
133 ret = pin;
134 goto unlock;
135 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200136
Stephen Warren57b676f2012-03-02 13:05:44 -0700137 ret = pin_config_set_for_pin(pctldev, pin, config);
138
139unlock:
140 mutex_unlock(&pinctrl_mutex);
141 return ret;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200142}
143EXPORT_SYMBOL(pin_config_set);
144
Stephen Warren43699de2011-12-15 16:57:17 -0700145int pin_config_group_get(const char *dev_name, const char *pin_group,
Linus Walleijae6b4d82011-10-19 18:14:33 +0200146 unsigned long *config)
147{
Stephen Warren43699de2011-12-15 16:57:17 -0700148 struct pinctrl_dev *pctldev;
149 const struct pinconf_ops *ops;
Stephen Warren57b676f2012-03-02 13:05:44 -0700150 int selector, ret;
151
152 mutex_lock(&pinctrl_mutex);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200153
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100154 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -0700155 if (!pctldev) {
156 ret = -EINVAL;
157 goto unlock;
158 }
Stephen Warren43699de2011-12-15 16:57:17 -0700159 ops = pctldev->desc->confops;
160
Linus Walleijae6b4d82011-10-19 18:14:33 +0200161 if (!ops || !ops->pin_config_group_get) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700162 dev_err(pctldev->dev, "cannot get configuration for pin "
Linus Walleijae6b4d82011-10-19 18:14:33 +0200163 "group, missing group config get function in "
164 "driver\n");
Stephen Warren57b676f2012-03-02 13:05:44 -0700165 ret = -EINVAL;
166 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200167 }
168
169 selector = pinctrl_get_group_selector(pctldev, pin_group);
Stephen Warren57b676f2012-03-02 13:05:44 -0700170 if (selector < 0) {
171 ret = selector;
172 goto unlock;
173 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200174
Stephen Warren57b676f2012-03-02 13:05:44 -0700175 ret = ops->pin_config_group_get(pctldev, selector, config);
176
177unlock:
178 mutex_unlock(&pinctrl_mutex);
179 return ret;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200180}
181EXPORT_SYMBOL(pin_config_group_get);
182
Stephen Warren43699de2011-12-15 16:57:17 -0700183int pin_config_group_set(const char *dev_name, const char *pin_group,
Linus Walleijae6b4d82011-10-19 18:14:33 +0200184 unsigned long config)
185{
Stephen Warren43699de2011-12-15 16:57:17 -0700186 struct pinctrl_dev *pctldev;
187 const struct pinconf_ops *ops;
188 const struct pinctrl_ops *pctlops;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200189 int selector;
190 const unsigned *pins;
191 unsigned num_pins;
192 int ret;
193 int i;
194
Stephen Warren57b676f2012-03-02 13:05:44 -0700195 mutex_lock(&pinctrl_mutex);
196
Linus Walleij9dfac4f2012-02-01 18:02:47 +0100197 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -0700198 if (!pctldev) {
199 ret = -EINVAL;
200 goto unlock;
201 }
Stephen Warren43699de2011-12-15 16:57:17 -0700202 ops = pctldev->desc->confops;
203 pctlops = pctldev->desc->pctlops;
204
Linus Walleijae6b4d82011-10-19 18:14:33 +0200205 if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700206 dev_err(pctldev->dev, "cannot configure pin group, missing "
Linus Walleijae6b4d82011-10-19 18:14:33 +0200207 "config function in driver\n");
Stephen Warren57b676f2012-03-02 13:05:44 -0700208 ret = -EINVAL;
209 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200210 }
211
212 selector = pinctrl_get_group_selector(pctldev, pin_group);
Stephen Warren57b676f2012-03-02 13:05:44 -0700213 if (selector < 0) {
214 ret = selector;
215 goto unlock;
216 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200217
218 ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
219 if (ret) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700220 dev_err(pctldev->dev, "cannot configure pin group, error "
Linus Walleijae6b4d82011-10-19 18:14:33 +0200221 "getting pins\n");
Stephen Warren57b676f2012-03-02 13:05:44 -0700222 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200223 }
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)
Stephen Warren57b676f2012-03-02 13:05:44 -0700236 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200237 }
238
239 /*
240 * If the controller cannot handle entire groups, we configure each pin
241 * individually.
242 */
Stephen Warren57b676f2012-03-02 13:05:44 -0700243 if (!ops->pin_config_set) {
244 ret = 0;
245 goto unlock;
246 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200247
248 for (i = 0; i < num_pins; i++) {
249 ret = ops->pin_config_set(pctldev, pins[i], config);
250 if (ret < 0)
Stephen Warren57b676f2012-03-02 13:05:44 -0700251 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200252 }
253
Stephen Warren57b676f2012-03-02 13:05:44 -0700254 ret = 0;
255
256unlock:
257 mutex_unlock(&pinctrl_mutex);
258
259 return ret;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200260}
261EXPORT_SYMBOL(pin_config_group_set);
262
Linus Walleijae6b4d82011-10-19 18:14:33 +0200263#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;
Chanho Park706e8522012-01-03 16:47:50 +0900277 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200278
279 seq_puts(s, "Pin config settings per pin\n");
280 seq_puts(s, "Format: pin (name): pinmux setting array\n");
281
Stephen Warren57b676f2012-03-02 13:05:44 -0700282 mutex_lock(&pinctrl_mutex);
283
Chanho Park706e8522012-01-03 16:47:50 +0900284 /* The pin number can be retrived from the pin controller descriptor */
Stephen Warren546edd82012-01-06 13:38:31 -0700285 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200286 struct pin_desc *desc;
287
Chanho Park706e8522012-01-03 16:47:50 +0900288 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200289 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900290 /* Skip if we cannot search the pin */
Linus Walleijae6b4d82011-10-19 18:14:33 +0200291 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
Stephen Warren57b676f2012-03-02 13:05:44 -0700302 mutex_unlock(&pinctrl_mutex);
303
Linus Walleijae6b4d82011-10-19 18:14:33 +0200304 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
Stephen Warren57b676f2012-03-02 13:05:44 -0700330 mutex_lock(&pinctrl_mutex);
331
Linus Walleijae6b4d82011-10-19 18:14:33 +0200332 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);
Stephen Warrenf7b90062012-02-19 23:45:58 -0700337 seq_printf(s, "\n");
338
Linus Walleijae6b4d82011-10-19 18:14:33 +0200339 selector++;
340 }
341
Stephen Warren57b676f2012-03-02 13:05:44 -0700342 mutex_unlock(&pinctrl_mutex);
343
Linus Walleijae6b4d82011-10-19 18:14:33 +0200344 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