2 * Core driver for the pin config portions of the pin control subsystem
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
7 * Author: Linus Walleij <linus.walleij@linaro.org>
9 * License terms: GNU General Public License (GPL) version 2
11 #define pr_fmt(fmt) "pinconfig core: " fmt
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>
26 int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
27 unsigned long *config)
29 const struct pinconf_ops *ops = pctldev->desc->confops;
31 if (!ops || !ops->pin_config_get) {
32 dev_err(pctldev->dev, "cannot get pin configuration, missing "
33 "pin_config_get() function in driver\n");
37 return ops->pin_config_get(pctldev, pin, config);
41 * pin_config_get() - get the configuration of a single pin parameter
42 * @pctldev: pin controller device for this pin
43 * @name: name of the pin to get the config for
44 * @config: the config pointed to by this argument will be filled in with the
45 * current pin state, it can be used directly by drivers as a numeral, or
46 * it can be dereferenced to any struct.
48 int pin_config_get(struct pinctrl_dev *pctldev, const char *name,
49 unsigned long *config)
53 pin = pin_get_from_name(pctldev, name);
57 return pin_config_get_for_pin(pctldev, pin, config);
59 EXPORT_SYMBOL(pin_config_get);
61 int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
64 const struct pinconf_ops *ops = pctldev->desc->confops;
67 if (!ops || !ops->pin_config_set) {
68 dev_err(pctldev->dev, "cannot configure pin, missing "
69 "config function in driver\n");
73 ret = ops->pin_config_set(pctldev, pin, config);
76 "unable to set pin configuration on pin %d\n", pin);
84 * pin_config_set() - set the configuration of a single pin parameter
85 * @pctldev: pin controller device for this pin
86 * @name: name of the pin to set the config for
87 * @config: the config in this argument will contain the desired pin state, it
88 * can be used directly by drivers as a numeral, or it can be dereferenced
91 int pin_config_set(struct pinctrl_dev *pctldev, const char *name,
96 pin = pin_get_from_name(pctldev, name);
100 return pin_config_set_for_pin(pctldev, pin, config);
102 EXPORT_SYMBOL(pin_config_set);
104 int pin_config_group_get(struct pinctrl_dev *pctldev, const char *pin_group,
105 unsigned long *config)
107 const struct pinconf_ops *ops = pctldev->desc->confops;
110 if (!ops || !ops->pin_config_group_get) {
111 dev_err(pctldev->dev, "cannot get configuration for pin "
112 "group, missing group config get function in "
117 selector = pinctrl_get_group_selector(pctldev, pin_group);
121 return ops->pin_config_group_get(pctldev, selector, config);
123 EXPORT_SYMBOL(pin_config_group_get);
126 int pin_config_group_set(struct pinctrl_dev *pctldev, const char *pin_group,
127 unsigned long config)
129 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
130 const struct pinconf_ops *ops = pctldev->desc->confops;
132 const unsigned *pins;
137 if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
138 dev_err(pctldev->dev, "cannot configure pin group, missing "
139 "config function in driver\n");
143 selector = pinctrl_get_group_selector(pctldev, pin_group);
147 ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
149 dev_err(pctldev->dev, "cannot configure pin group, error "
155 * If the pin controller supports handling entire groups we use that
158 if (ops->pin_config_group_set) {
159 ret = ops->pin_config_group_set(pctldev, selector, config);
161 * If the pin controller prefer that a certain group be handled
162 * pin-by-pin as well, it returns -EAGAIN.
169 * If the controller cannot handle entire groups, we configure each pin
172 if (!ops->pin_config_set)
175 for (i = 0; i < num_pins; i++) {
176 ret = ops->pin_config_set(pctldev, pins[i], config);
183 EXPORT_SYMBOL(pin_config_group_set);
185 int pinconf_check_ops(const struct pinconf_ops *ops)
187 /* We must be able to read out pin status */
188 if (!ops->pin_config_get && !ops->pin_config_group_get)
190 /* We have to be able to config the pins in SOME way */
191 if (!ops->pin_config_set && !ops->pin_config_group_set)
196 #ifdef CONFIG_DEBUG_FS
198 static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
199 struct seq_file *s, int pin)
201 const struct pinconf_ops *ops = pctldev->desc->confops;
203 if (ops && ops->pin_config_dbg_show)
204 ops->pin_config_dbg_show(pctldev, s, pin);
207 static int pinconf_pins_show(struct seq_file *s, void *what)
209 struct pinctrl_dev *pctldev = s->private;
212 seq_puts(s, "Pin config settings per pin\n");
213 seq_puts(s, "Format: pin (name): pinmux setting array\n");
215 /* The highest pin number need to be included in the loop, thus <= */
216 for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
217 struct pin_desc *desc;
219 desc = pin_desc_get(pctldev, pin);
220 /* Pin space may be sparse */
224 seq_printf(s, "pin %d (%s):", pin,
225 desc->name ? desc->name : "unnamed");
227 pinconf_dump_pin(pctldev, s, pin);
235 static void pinconf_dump_group(struct pinctrl_dev *pctldev,
236 struct seq_file *s, unsigned selector,
239 const struct pinconf_ops *ops = pctldev->desc->confops;
241 if (ops && ops->pin_config_group_dbg_show)
242 ops->pin_config_group_dbg_show(pctldev, s, selector);
245 static int pinconf_groups_show(struct seq_file *s, void *what)
247 struct pinctrl_dev *pctldev = s->private;
248 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
249 const struct pinconf_ops *ops = pctldev->desc->confops;
250 unsigned selector = 0;
252 if (!ops || !ops->pin_config_group_get)
255 seq_puts(s, "Pin config settings per pin group\n");
256 seq_puts(s, "Format: group (name): pinmux setting array\n");
258 while (pctlops->list_groups(pctldev, selector) >= 0) {
259 const char *gname = pctlops->get_group_name(pctldev, selector);
261 seq_printf(s, "%u (%s):", selector, gname);
262 pinconf_dump_group(pctldev, s, selector, gname);
269 static int pinconf_pins_open(struct inode *inode, struct file *file)
271 return single_open(file, pinconf_pins_show, inode->i_private);
274 static int pinconf_groups_open(struct inode *inode, struct file *file)
276 return single_open(file, pinconf_groups_show, inode->i_private);
279 static const struct file_operations pinconf_pins_ops = {
280 .open = pinconf_pins_open,
283 .release = single_release,
286 static const struct file_operations pinconf_groups_ops = {
287 .open = pinconf_groups_open,
290 .release = single_release,
293 void pinconf_init_device_debugfs(struct dentry *devroot,
294 struct pinctrl_dev *pctldev)
296 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
297 devroot, pctldev, &pinconf_pins_ops);
298 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
299 devroot, pctldev, &pinconf_groups_ops);