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 * @dev_name: name of the 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(const char *dev_name
, const char *name
,
49 unsigned long *config
)
51 struct pinctrl_dev
*pctldev
;
54 pctldev
= get_pinctrl_dev_from_dev(NULL
, dev_name
);
58 pin
= pin_get_from_name(pctldev
, name
);
62 return pin_config_get_for_pin(pctldev
, pin
, config
);
64 EXPORT_SYMBOL(pin_config_get
);
66 int pin_config_set_for_pin(struct pinctrl_dev
*pctldev
, unsigned pin
,
69 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
72 if (!ops
|| !ops
->pin_config_set
) {
73 dev_err(pctldev
->dev
, "cannot configure pin, missing "
74 "config function in driver\n");
78 ret
= ops
->pin_config_set(pctldev
, pin
, config
);
81 "unable to set pin configuration on pin %d\n", pin
);
89 * pin_config_set() - set the configuration of a single pin parameter
90 * @dev_name: name of pin controller device for this pin
91 * @name: name of the pin to set the config for
92 * @config: the config in this argument will contain the desired pin state, it
93 * can be used directly by drivers as a numeral, or it can be dereferenced
96 int pin_config_set(const char *dev_name
, const char *name
,
99 struct pinctrl_dev
*pctldev
;
102 pctldev
= get_pinctrl_dev_from_dev(NULL
, dev_name
);
106 pin
= pin_get_from_name(pctldev
, name
);
110 return pin_config_set_for_pin(pctldev
, pin
, config
);
112 EXPORT_SYMBOL(pin_config_set
);
114 int pin_config_group_get(const char *dev_name
, const char *pin_group
,
115 unsigned long *config
)
117 struct pinctrl_dev
*pctldev
;
118 const struct pinconf_ops
*ops
;
121 pctldev
= get_pinctrl_dev_from_dev(NULL
, dev_name
);
124 ops
= pctldev
->desc
->confops
;
126 if (!ops
|| !ops
->pin_config_group_get
) {
127 dev_err(pctldev
->dev
, "cannot get configuration for pin "
128 "group, missing group config get function in "
133 selector
= pinctrl_get_group_selector(pctldev
, pin_group
);
137 return ops
->pin_config_group_get(pctldev
, selector
, config
);
139 EXPORT_SYMBOL(pin_config_group_get
);
142 int pin_config_group_set(const char *dev_name
, const char *pin_group
,
143 unsigned long config
)
145 struct pinctrl_dev
*pctldev
;
146 const struct pinconf_ops
*ops
;
147 const struct pinctrl_ops
*pctlops
;
149 const unsigned *pins
;
154 pctldev
= get_pinctrl_dev_from_dev(NULL
, dev_name
);
157 ops
= pctldev
->desc
->confops
;
158 pctlops
= pctldev
->desc
->pctlops
;
160 if (!ops
|| (!ops
->pin_config_group_set
&& !ops
->pin_config_set
)) {
161 dev_err(pctldev
->dev
, "cannot configure pin group, missing "
162 "config function in driver\n");
166 selector
= pinctrl_get_group_selector(pctldev
, pin_group
);
170 ret
= pctlops
->get_group_pins(pctldev
, selector
, &pins
, &num_pins
);
172 dev_err(pctldev
->dev
, "cannot configure pin group, error "
178 * If the pin controller supports handling entire groups we use that
181 if (ops
->pin_config_group_set
) {
182 ret
= ops
->pin_config_group_set(pctldev
, selector
, config
);
184 * If the pin controller prefer that a certain group be handled
185 * pin-by-pin as well, it returns -EAGAIN.
192 * If the controller cannot handle entire groups, we configure each pin
195 if (!ops
->pin_config_set
)
198 for (i
= 0; i
< num_pins
; i
++) {
199 ret
= ops
->pin_config_set(pctldev
, pins
[i
], config
);
206 EXPORT_SYMBOL(pin_config_group_set
);
208 int pinconf_check_ops(const struct pinconf_ops
*ops
)
210 /* We must be able to read out pin status */
211 if (!ops
->pin_config_get
&& !ops
->pin_config_group_get
)
213 /* We have to be able to config the pins in SOME way */
214 if (!ops
->pin_config_set
&& !ops
->pin_config_group_set
)
219 #ifdef CONFIG_DEBUG_FS
221 static void pinconf_dump_pin(struct pinctrl_dev
*pctldev
,
222 struct seq_file
*s
, int pin
)
224 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
226 if (ops
&& ops
->pin_config_dbg_show
)
227 ops
->pin_config_dbg_show(pctldev
, s
, pin
);
230 static int pinconf_pins_show(struct seq_file
*s
, void *what
)
232 struct pinctrl_dev
*pctldev
= s
->private;
235 seq_puts(s
, "Pin config settings per pin\n");
236 seq_puts(s
, "Format: pin (name): pinmux setting array\n");
238 /* The pin number can be retrived from the pin controller descriptor */
239 for (i
= 0; pin
< pctldev
->desc
->npins
; i
++) {
240 struct pin_desc
*desc
;
242 pin
= pctldev
->desc
->pins
[i
].number
;
243 desc
= pin_desc_get(pctldev
, pin
);
244 /* Skip if we cannot search the pin */
248 seq_printf(s
, "pin %d (%s):", pin
,
249 desc
->name
? desc
->name
: "unnamed");
251 pinconf_dump_pin(pctldev
, s
, pin
);
259 static void pinconf_dump_group(struct pinctrl_dev
*pctldev
,
260 struct seq_file
*s
, unsigned selector
,
263 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
265 if (ops
&& ops
->pin_config_group_dbg_show
)
266 ops
->pin_config_group_dbg_show(pctldev
, s
, selector
);
269 static int pinconf_groups_show(struct seq_file
*s
, void *what
)
271 struct pinctrl_dev
*pctldev
= s
->private;
272 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
273 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
274 unsigned selector
= 0;
276 if (!ops
|| !ops
->pin_config_group_get
)
279 seq_puts(s
, "Pin config settings per pin group\n");
280 seq_puts(s
, "Format: group (name): pinmux setting array\n");
282 while (pctlops
->list_groups(pctldev
, selector
) >= 0) {
283 const char *gname
= pctlops
->get_group_name(pctldev
, selector
);
285 seq_printf(s
, "%u (%s):", selector
, gname
);
286 pinconf_dump_group(pctldev
, s
, selector
, gname
);
293 static int pinconf_pins_open(struct inode
*inode
, struct file
*file
)
295 return single_open(file
, pinconf_pins_show
, inode
->i_private
);
298 static int pinconf_groups_open(struct inode
*inode
, struct file
*file
)
300 return single_open(file
, pinconf_groups_show
, inode
->i_private
);
303 static const struct file_operations pinconf_pins_ops
= {
304 .open
= pinconf_pins_open
,
307 .release
= single_release
,
310 static const struct file_operations pinconf_groups_ops
= {
311 .open
= pinconf_groups_open
,
314 .release
= single_release
,
317 void pinconf_init_device_debugfs(struct dentry
*devroot
,
318 struct pinctrl_dev
*pctldev
)
320 debugfs_create_file("pinconf-pins", S_IFREG
| S_IRUGO
,
321 devroot
, pctldev
, &pinconf_pins_ops
);
322 debugfs_create_file("pinconf-groups", S_IFREG
| S_IRUGO
,
323 devroot
, pctldev
, &pinconf_groups_ops
);