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 pinconf_check_ops(struct pinctrl_dev *pctldev)
28 const struct pinconf_ops *ops = pctldev->desc->confops;
30 /* We must be able to read out pin status */
31 if (!ops->pin_config_get && !ops->pin_config_group_get)
33 /* We have to be able to config the pins in SOME way */
34 if (!ops->pin_config_set && !ops->pin_config_group_set)
39 static int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
40 unsigned long *config)
42 const struct pinconf_ops *ops = pctldev->desc->confops;
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");
50 return ops->pin_config_get(pctldev, pin, config);
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.
61 int pin_config_get(const char *dev_name, const char *name,
62 unsigned long *config)
64 struct pinctrl_dev *pctldev;
67 pctldev = get_pinctrl_dev_from_devname(dev_name);
71 pin = pin_get_from_name(pctldev, name);
75 return pin_config_get_for_pin(pctldev, pin, config);
77 EXPORT_SYMBOL(pin_config_get);
79 static int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
82 const struct pinconf_ops *ops = pctldev->desc->confops;
85 if (!ops || !ops->pin_config_set) {
86 dev_err(pctldev->dev, "cannot configure pin, missing "
87 "config function in driver\n");
91 ret = ops->pin_config_set(pctldev, pin, config);
94 "unable to set pin configuration on pin %d\n", pin);
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
109 int pin_config_set(const char *dev_name, const char *name,
110 unsigned long config)
112 struct pinctrl_dev *pctldev;
115 pctldev = get_pinctrl_dev_from_devname(dev_name);
119 pin = pin_get_from_name(pctldev, name);
123 return pin_config_set_for_pin(pctldev, pin, config);
125 EXPORT_SYMBOL(pin_config_set);
127 int pin_config_group_get(const char *dev_name, const char *pin_group,
128 unsigned long *config)
130 struct pinctrl_dev *pctldev;
131 const struct pinconf_ops *ops;
134 pctldev = get_pinctrl_dev_from_devname(dev_name);
137 ops = pctldev->desc->confops;
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 "
146 selector = pinctrl_get_group_selector(pctldev, pin_group);
150 return ops->pin_config_group_get(pctldev, selector, config);
152 EXPORT_SYMBOL(pin_config_group_get);
154 int pin_config_group_set(const char *dev_name, const char *pin_group,
155 unsigned long config)
157 struct pinctrl_dev *pctldev;
158 const struct pinconf_ops *ops;
159 const struct pinctrl_ops *pctlops;
161 const unsigned *pins;
166 pctldev = get_pinctrl_dev_from_devname(dev_name);
169 ops = pctldev->desc->confops;
170 pctlops = pctldev->desc->pctlops;
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");
178 selector = pinctrl_get_group_selector(pctldev, pin_group);
182 ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
184 dev_err(pctldev->dev, "cannot configure pin group, error "
190 * If the pin controller supports handling entire groups we use that
193 if (ops->pin_config_group_set) {
194 ret = ops->pin_config_group_set(pctldev, selector, config);
196 * If the pin controller prefer that a certain group be handled
197 * pin-by-pin as well, it returns -EAGAIN.
204 * If the controller cannot handle entire groups, we configure each pin
207 if (!ops->pin_config_set)
210 for (i = 0; i < num_pins; i++) {
211 ret = ops->pin_config_set(pctldev, pins[i], config);
218 EXPORT_SYMBOL(pin_config_group_set);
220 #ifdef CONFIG_DEBUG_FS
222 static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
223 struct seq_file *s, int pin)
225 const struct pinconf_ops *ops = pctldev->desc->confops;
227 if (ops && ops->pin_config_dbg_show)
228 ops->pin_config_dbg_show(pctldev, s, pin);
231 static int pinconf_pins_show(struct seq_file *s, void *what)
233 struct pinctrl_dev *pctldev = s->private;
236 seq_puts(s, "Pin config settings per pin\n");
237 seq_puts(s, "Format: pin (name): pinmux setting array\n");
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;
243 pin = pctldev->desc->pins[i].number;
244 desc = pin_desc_get(pctldev, pin);
245 /* Skip if we cannot search the pin */
249 seq_printf(s, "pin %d (%s):", pin,
250 desc->name ? desc->name : "unnamed");
252 pinconf_dump_pin(pctldev, s, pin);
260 static void pinconf_dump_group(struct pinctrl_dev *pctldev,
261 struct seq_file *s, unsigned selector,
264 const struct pinconf_ops *ops = pctldev->desc->confops;
266 if (ops && ops->pin_config_group_dbg_show)
267 ops->pin_config_group_dbg_show(pctldev, s, selector);
270 static int pinconf_groups_show(struct seq_file *s, void *what)
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;
277 if (!ops || !ops->pin_config_group_get)
280 seq_puts(s, "Pin config settings per pin group\n");
281 seq_puts(s, "Format: group (name): pinmux setting array\n");
283 while (pctlops->list_groups(pctldev, selector) >= 0) {
284 const char *gname = pctlops->get_group_name(pctldev, selector);
286 seq_printf(s, "%u (%s):", selector, gname);
287 pinconf_dump_group(pctldev, s, selector, gname);
296 static int pinconf_pins_open(struct inode *inode, struct file *file)
298 return single_open(file, pinconf_pins_show, inode->i_private);
301 static int pinconf_groups_open(struct inode *inode, struct file *file)
303 return single_open(file, pinconf_groups_show, inode->i_private);
306 static const struct file_operations pinconf_pins_ops = {
307 .open = pinconf_pins_open,
310 .release = single_release,
313 static const struct file_operations pinconf_groups_ops = {
314 .open = pinconf_groups_open,
317 .release = single_release,
320 void pinconf_init_device_debugfs(struct dentry *devroot,
321 struct pinctrl_dev *pctldev)
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);