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 have to be able to config the pins in SOME way */
31 if (!ops
->pin_config_set
&& !ops
->pin_config_group_set
) {
33 "pinconf has to be able to set a pins config\n");
39 int pinconf_validate_map(const struct pinctrl_map
*map
, int i
)
41 if (!map
->data
.configs
.group_or_pin
) {
42 pr_err("failed to register map %s (%d): no group/pin given\n",
47 if (!map
->data
.configs
.num_configs
||
48 !map
->data
.configs
.configs
) {
49 pr_err("failed to register map %s (%d): no configs given\n",
57 int pin_config_get_for_pin(struct pinctrl_dev
*pctldev
, unsigned pin
,
58 unsigned long *config
)
60 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
62 if (!ops
|| !ops
->pin_config_get
) {
64 "cannot get pin configuration, .pin_config_get missing in driver\n");
68 return ops
->pin_config_get(pctldev
, pin
, config
);
71 int pin_config_group_get(const char *dev_name
, const char *pin_group
,
72 unsigned long *config
)
74 struct pinctrl_dev
*pctldev
;
75 const struct pinconf_ops
*ops
;
78 pctldev
= get_pinctrl_dev_from_devname(dev_name
);
84 mutex_lock(&pctldev
->mutex
);
86 ops
= pctldev
->desc
->confops
;
88 if (!ops
|| !ops
->pin_config_group_get
) {
90 "cannot get configuration for pin group, missing group config get function in driver\n");
95 selector
= pinctrl_get_group_selector(pctldev
, pin_group
);
101 ret
= ops
->pin_config_group_get(pctldev
, selector
, config
);
104 mutex_unlock(&pctldev
->mutex
);
108 int pinconf_map_to_setting(const struct pinctrl_map
*map
,
109 struct pinctrl_setting
*setting
)
111 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
114 switch (setting
->type
) {
115 case PIN_MAP_TYPE_CONFIGS_PIN
:
116 pin
= pin_get_from_name(pctldev
,
117 map
->data
.configs
.group_or_pin
);
119 dev_err(pctldev
->dev
, "could not map pin config for \"%s\"",
120 map
->data
.configs
.group_or_pin
);
123 setting
->data
.configs
.group_or_pin
= pin
;
125 case PIN_MAP_TYPE_CONFIGS_GROUP
:
126 pin
= pinctrl_get_group_selector(pctldev
,
127 map
->data
.configs
.group_or_pin
);
129 dev_err(pctldev
->dev
, "could not map group config for \"%s\"",
130 map
->data
.configs
.group_or_pin
);
133 setting
->data
.configs
.group_or_pin
= pin
;
139 setting
->data
.configs
.num_configs
= map
->data
.configs
.num_configs
;
140 setting
->data
.configs
.configs
= map
->data
.configs
.configs
;
145 void pinconf_free_setting(const struct pinctrl_setting
*setting
)
149 int pinconf_apply_setting(const struct pinctrl_setting
*setting
)
151 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
152 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
156 dev_err(pctldev
->dev
, "missing confops\n");
160 switch (setting
->type
) {
161 case PIN_MAP_TYPE_CONFIGS_PIN
:
162 if (!ops
->pin_config_set
) {
163 dev_err(pctldev
->dev
, "missing pin_config_set op\n");
166 ret
= ops
->pin_config_set(pctldev
,
167 setting
->data
.configs
.group_or_pin
,
168 setting
->data
.configs
.configs
,
169 setting
->data
.configs
.num_configs
);
171 dev_err(pctldev
->dev
,
172 "pin_config_set op failed for pin %d\n",
173 setting
->data
.configs
.group_or_pin
);
177 case PIN_MAP_TYPE_CONFIGS_GROUP
:
178 if (!ops
->pin_config_group_set
) {
179 dev_err(pctldev
->dev
,
180 "missing pin_config_group_set op\n");
183 ret
= ops
->pin_config_group_set(pctldev
,
184 setting
->data
.configs
.group_or_pin
,
185 setting
->data
.configs
.configs
,
186 setting
->data
.configs
.num_configs
);
188 dev_err(pctldev
->dev
,
189 "pin_config_group_set op failed for group %d\n",
190 setting
->data
.configs
.group_or_pin
);
201 int pinconf_set_config(struct pinctrl_dev
*pctldev
, unsigned pin
,
202 unsigned long *configs
, size_t nconfigs
)
204 const struct pinconf_ops
*ops
;
206 ops
= pctldev
->desc
->confops
;
207 if (!ops
|| !ops
->pin_config_set
)
210 return ops
->pin_config_set(pctldev
, pin
, configs
, nconfigs
);
213 #ifdef CONFIG_DEBUG_FS
215 static void pinconf_show_config(struct seq_file
*s
, struct pinctrl_dev
*pctldev
,
216 unsigned long *configs
, unsigned num_configs
)
218 const struct pinconf_ops
*confops
;
222 confops
= pctldev
->desc
->confops
;
226 for (i
= 0; i
< num_configs
; i
++) {
227 seq_puts(s
, "config ");
228 if (confops
&& confops
->pin_config_config_dbg_show
)
229 confops
->pin_config_config_dbg_show(pctldev
, s
,
232 seq_printf(s
, "%08lx", configs
[i
]);
237 void pinconf_show_map(struct seq_file
*s
, const struct pinctrl_map
*map
)
239 struct pinctrl_dev
*pctldev
;
241 pctldev
= get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
244 case PIN_MAP_TYPE_CONFIGS_PIN
:
247 case PIN_MAP_TYPE_CONFIGS_GROUP
:
248 seq_puts(s
, "group ");
254 seq_printf(s
, "%s\n", map
->data
.configs
.group_or_pin
);
256 pinconf_show_config(s
, pctldev
, map
->data
.configs
.configs
,
257 map
->data
.configs
.num_configs
);
260 void pinconf_show_setting(struct seq_file
*s
,
261 const struct pinctrl_setting
*setting
)
263 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
264 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
265 struct pin_desc
*desc
;
267 switch (setting
->type
) {
268 case PIN_MAP_TYPE_CONFIGS_PIN
:
269 desc
= pin_desc_get(setting
->pctldev
,
270 setting
->data
.configs
.group_or_pin
);
271 seq_printf(s
, "pin %s (%d)", desc
->name
,
272 setting
->data
.configs
.group_or_pin
);
274 case PIN_MAP_TYPE_CONFIGS_GROUP
:
275 seq_printf(s
, "group %s (%d)",
276 pctlops
->get_group_name(pctldev
,
277 setting
->data
.configs
.group_or_pin
),
278 setting
->data
.configs
.group_or_pin
);
285 * FIXME: We should really get the pin controller to dump the config
286 * values, so they can be decoded to something meaningful.
288 pinconf_show_config(s
, pctldev
, setting
->data
.configs
.configs
,
289 setting
->data
.configs
.num_configs
);
292 static void pinconf_dump_pin(struct pinctrl_dev
*pctldev
,
293 struct seq_file
*s
, int pin
)
295 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
297 /* no-op when not using generic pin config */
298 pinconf_generic_dump_pins(pctldev
, s
, NULL
, pin
);
299 if (ops
&& ops
->pin_config_dbg_show
)
300 ops
->pin_config_dbg_show(pctldev
, s
, pin
);
303 static int pinconf_pins_show(struct seq_file
*s
, void *what
)
305 struct pinctrl_dev
*pctldev
= s
->private;
308 seq_puts(s
, "Pin config settings per pin\n");
309 seq_puts(s
, "Format: pin (name): configs\n");
311 mutex_lock(&pctldev
->mutex
);
313 /* The pin number can be retrived from the pin controller descriptor */
314 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
315 struct pin_desc
*desc
;
317 pin
= pctldev
->desc
->pins
[i
].number
;
318 desc
= pin_desc_get(pctldev
, pin
);
319 /* Skip if we cannot search the pin */
323 seq_printf(s
, "pin %d (%s): ", pin
, desc
->name
);
325 pinconf_dump_pin(pctldev
, s
, pin
);
329 mutex_unlock(&pctldev
->mutex
);
334 static void pinconf_dump_group(struct pinctrl_dev
*pctldev
,
335 struct seq_file
*s
, unsigned selector
,
338 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
340 /* no-op when not using generic pin config */
341 pinconf_generic_dump_pins(pctldev
, s
, gname
, 0);
342 if (ops
&& ops
->pin_config_group_dbg_show
)
343 ops
->pin_config_group_dbg_show(pctldev
, s
, selector
);
346 static int pinconf_groups_show(struct seq_file
*s
, void *what
)
348 struct pinctrl_dev
*pctldev
= s
->private;
349 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
350 unsigned ngroups
= pctlops
->get_groups_count(pctldev
);
351 unsigned selector
= 0;
353 seq_puts(s
, "Pin config settings per pin group\n");
354 seq_puts(s
, "Format: group (name): configs\n");
356 while (selector
< ngroups
) {
357 const char *gname
= pctlops
->get_group_name(pctldev
, selector
);
359 seq_printf(s
, "%u (%s): ", selector
, gname
);
360 pinconf_dump_group(pctldev
, s
, selector
, gname
);
368 DEFINE_SHOW_ATTRIBUTE(pinconf_pins
);
369 DEFINE_SHOW_ATTRIBUTE(pinconf_groups
);
371 void pinconf_init_device_debugfs(struct dentry
*devroot
,
372 struct pinctrl_dev
*pctldev
)
374 debugfs_create_file("pinconf-pins", S_IFREG
| S_IRUGO
,
375 devroot
, pctldev
, &pinconf_pins_fops
);
376 debugfs_create_file("pinconf-groups", S_IFREG
| S_IRUGO
,
377 devroot
, pctldev
, &pinconf_groups_fops
);