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/uaccess.h>
21 #include <linux/pinctrl/machine.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinconf.h>
27 int pinconf_check_ops(struct pinctrl_dev
*pctldev
)
29 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
31 /* We have to be able to config the pins in SOME way */
32 if (!ops
->pin_config_set
&& !ops
->pin_config_group_set
) {
34 "pinconf has to be able to set a pins config\n");
40 int pinconf_validate_map(struct pinctrl_map
const *map
, int i
)
42 if (!map
->data
.configs
.group_or_pin
) {
43 pr_err("failed to register map %s (%d): no group/pin given\n",
48 if (!map
->data
.configs
.num_configs
||
49 !map
->data
.configs
.configs
) {
50 pr_err("failed to register map %s (%d): no configs given\n",
58 int pin_config_get_for_pin(struct pinctrl_dev
*pctldev
, unsigned pin
,
59 unsigned long *config
)
61 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
63 if (!ops
|| !ops
->pin_config_get
) {
65 "cannot get pin configuration, .pin_config_get missing in driver\n");
69 return ops
->pin_config_get(pctldev
, pin
, config
);
72 int pin_config_group_get(const char *dev_name
, const char *pin_group
,
73 unsigned long *config
)
75 struct pinctrl_dev
*pctldev
;
76 const struct pinconf_ops
*ops
;
79 pctldev
= get_pinctrl_dev_from_devname(dev_name
);
85 mutex_lock(&pctldev
->mutex
);
87 ops
= pctldev
->desc
->confops
;
89 if (!ops
|| !ops
->pin_config_group_get
) {
90 dev_dbg(pctldev
->dev
, "cannot get configuration for pin "
91 "group, missing group config get function in "
97 selector
= pinctrl_get_group_selector(pctldev
, pin_group
);
103 ret
= ops
->pin_config_group_get(pctldev
, selector
, config
);
106 mutex_unlock(&pctldev
->mutex
);
110 int pinconf_map_to_setting(struct pinctrl_map
const *map
,
111 struct pinctrl_setting
*setting
)
113 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
116 switch (setting
->type
) {
117 case PIN_MAP_TYPE_CONFIGS_PIN
:
118 pin
= pin_get_from_name(pctldev
,
119 map
->data
.configs
.group_or_pin
);
121 dev_err(pctldev
->dev
, "could not map pin config for \"%s\"",
122 map
->data
.configs
.group_or_pin
);
125 setting
->data
.configs
.group_or_pin
= pin
;
127 case PIN_MAP_TYPE_CONFIGS_GROUP
:
128 pin
= pinctrl_get_group_selector(pctldev
,
129 map
->data
.configs
.group_or_pin
);
131 dev_err(pctldev
->dev
, "could not map group config for \"%s\"",
132 map
->data
.configs
.group_or_pin
);
135 setting
->data
.configs
.group_or_pin
= pin
;
141 setting
->data
.configs
.num_configs
= map
->data
.configs
.num_configs
;
142 setting
->data
.configs
.configs
= map
->data
.configs
.configs
;
147 void pinconf_free_setting(struct pinctrl_setting
const *setting
)
151 int pinconf_apply_setting(struct pinctrl_setting
const *setting
)
153 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
154 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
158 dev_err(pctldev
->dev
, "missing confops\n");
162 switch (setting
->type
) {
163 case PIN_MAP_TYPE_CONFIGS_PIN
:
164 if (!ops
->pin_config_set
) {
165 dev_err(pctldev
->dev
, "missing pin_config_set op\n");
168 ret
= ops
->pin_config_set(pctldev
,
169 setting
->data
.configs
.group_or_pin
,
170 setting
->data
.configs
.configs
,
171 setting
->data
.configs
.num_configs
);
173 dev_err(pctldev
->dev
,
174 "pin_config_set op failed for pin %d\n",
175 setting
->data
.configs
.group_or_pin
);
179 case PIN_MAP_TYPE_CONFIGS_GROUP
:
180 if (!ops
->pin_config_group_set
) {
181 dev_err(pctldev
->dev
,
182 "missing pin_config_group_set op\n");
185 ret
= ops
->pin_config_group_set(pctldev
,
186 setting
->data
.configs
.group_or_pin
,
187 setting
->data
.configs
.configs
,
188 setting
->data
.configs
.num_configs
);
190 dev_err(pctldev
->dev
,
191 "pin_config_group_set op failed for group %d\n",
192 setting
->data
.configs
.group_or_pin
);
203 int pinconf_set_config(struct pinctrl_dev
*pctldev
, unsigned pin
,
204 unsigned long *configs
, size_t nconfigs
)
206 const struct pinconf_ops
*ops
;
208 ops
= pctldev
->desc
->confops
;
212 return ops
->pin_config_set(pctldev
, pin
, configs
, nconfigs
);
215 #ifdef CONFIG_DEBUG_FS
217 static void pinconf_show_config(struct seq_file
*s
, struct pinctrl_dev
*pctldev
,
218 unsigned long *configs
, unsigned num_configs
)
220 const struct pinconf_ops
*confops
;
224 confops
= pctldev
->desc
->confops
;
228 for (i
= 0; i
< num_configs
; i
++) {
229 seq_puts(s
, "config ");
230 if (confops
&& confops
->pin_config_config_dbg_show
)
231 confops
->pin_config_config_dbg_show(pctldev
, s
,
234 seq_printf(s
, "%08lx", configs
[i
]);
239 void pinconf_show_map(struct seq_file
*s
, struct pinctrl_map
const *map
)
241 struct pinctrl_dev
*pctldev
;
243 pctldev
= get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
246 case PIN_MAP_TYPE_CONFIGS_PIN
:
247 seq_printf(s
, "pin ");
249 case PIN_MAP_TYPE_CONFIGS_GROUP
:
250 seq_printf(s
, "group ");
256 seq_printf(s
, "%s\n", map
->data
.configs
.group_or_pin
);
258 pinconf_show_config(s
, pctldev
, map
->data
.configs
.configs
,
259 map
->data
.configs
.num_configs
);
262 void pinconf_show_setting(struct seq_file
*s
,
263 struct pinctrl_setting
const *setting
)
265 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
266 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
267 struct pin_desc
*desc
;
269 switch (setting
->type
) {
270 case PIN_MAP_TYPE_CONFIGS_PIN
:
271 desc
= pin_desc_get(setting
->pctldev
,
272 setting
->data
.configs
.group_or_pin
);
273 seq_printf(s
, "pin %s (%d)", desc
->name
,
274 setting
->data
.configs
.group_or_pin
);
276 case PIN_MAP_TYPE_CONFIGS_GROUP
:
277 seq_printf(s
, "group %s (%d)",
278 pctlops
->get_group_name(pctldev
,
279 setting
->data
.configs
.group_or_pin
),
280 setting
->data
.configs
.group_or_pin
);
287 * FIXME: We should really get the pin controler to dump the config
288 * values, so they can be decoded to something meaningful.
290 pinconf_show_config(s
, pctldev
, setting
->data
.configs
.configs
,
291 setting
->data
.configs
.num_configs
);
294 static void pinconf_dump_pin(struct pinctrl_dev
*pctldev
,
295 struct seq_file
*s
, int pin
)
297 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
299 /* no-op when not using generic pin config */
300 pinconf_generic_dump_pins(pctldev
, s
, NULL
, pin
);
301 if (ops
&& ops
->pin_config_dbg_show
)
302 ops
->pin_config_dbg_show(pctldev
, s
, pin
);
305 static int pinconf_pins_show(struct seq_file
*s
, void *what
)
307 struct pinctrl_dev
*pctldev
= s
->private;
310 seq_puts(s
, "Pin config settings per pin\n");
311 seq_puts(s
, "Format: pin (name): configs\n");
313 mutex_lock(&pctldev
->mutex
);
315 /* The pin number can be retrived from the pin controller descriptor */
316 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
317 struct pin_desc
*desc
;
319 pin
= pctldev
->desc
->pins
[i
].number
;
320 desc
= pin_desc_get(pctldev
, pin
);
321 /* Skip if we cannot search the pin */
325 seq_printf(s
, "pin %d (%s): ", pin
, desc
->name
);
327 pinconf_dump_pin(pctldev
, s
, pin
);
332 mutex_unlock(&pctldev
->mutex
);
337 static void pinconf_dump_group(struct pinctrl_dev
*pctldev
,
338 struct seq_file
*s
, unsigned selector
,
341 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
343 /* no-op when not using generic pin config */
344 pinconf_generic_dump_pins(pctldev
, s
, gname
, 0);
345 if (ops
&& ops
->pin_config_group_dbg_show
)
346 ops
->pin_config_group_dbg_show(pctldev
, s
, selector
);
349 static int pinconf_groups_show(struct seq_file
*s
, void *what
)
351 struct pinctrl_dev
*pctldev
= s
->private;
352 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
353 unsigned ngroups
= pctlops
->get_groups_count(pctldev
);
354 unsigned selector
= 0;
356 seq_puts(s
, "Pin config settings per pin group\n");
357 seq_puts(s
, "Format: group (name): configs\n");
359 while (selector
< ngroups
) {
360 const char *gname
= pctlops
->get_group_name(pctldev
, selector
);
362 seq_printf(s
, "%u (%s): ", selector
, gname
);
363 pinconf_dump_group(pctldev
, s
, selector
, gname
);
372 static int pinconf_pins_open(struct inode
*inode
, struct file
*file
)
374 return single_open(file
, pinconf_pins_show
, inode
->i_private
);
377 static int pinconf_groups_open(struct inode
*inode
, struct file
*file
)
379 return single_open(file
, pinconf_groups_show
, inode
->i_private
);
382 static const struct file_operations pinconf_pins_ops
= {
383 .open
= pinconf_pins_open
,
386 .release
= single_release
,
389 static const struct file_operations pinconf_groups_ops
= {
390 .open
= pinconf_groups_open
,
393 .release
= single_release
,
396 #define MAX_NAME_LEN 15
399 enum pinctrl_map_type map_type
;
400 char dev_name
[MAX_NAME_LEN
+1];
401 char state_name
[MAX_NAME_LEN
+1];
402 char pin_name
[MAX_NAME_LEN
+1];
406 * Goal is to keep this structure as global in order to simply read the
407 * pinconf-config file after a write to check config is as expected
409 static struct dbg_cfg pinconf_dbg_conf
;
412 * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
413 * map, of the dev/pin/state that was last written to pinconf-config file.
414 * @s: string filled in with config description
417 static int pinconf_dbg_config_print(struct seq_file
*s
, void *d
)
419 struct pinctrl_maps
*maps_node
;
420 const struct pinctrl_map
*map
;
421 const struct pinctrl_map
*found
= NULL
;
422 struct pinctrl_dev
*pctldev
;
423 struct dbg_cfg
*dbg
= &pinconf_dbg_conf
;
426 mutex_lock(&pinctrl_maps_mutex
);
428 /* Parse the pinctrl map and look for the elected pin/state */
429 for_each_maps(maps_node
, i
, map
) {
430 if (map
->type
!= dbg
->map_type
)
432 if (strcmp(map
->dev_name
, dbg
->dev_name
))
434 if (strcmp(map
->name
, dbg
->state_name
))
437 if (!strcmp(map
->data
.configs
.group_or_pin
, dbg
->pin_name
)) {
438 /* We found the right pin */
445 seq_printf(s
, "No config found for dev/state/pin, expected:\n");
446 seq_printf(s
, "Searched dev:%s\n", dbg
->dev_name
);
447 seq_printf(s
, "Searched state:%s\n", dbg
->state_name
);
448 seq_printf(s
, "Searched pin:%s\n", dbg
->pin_name
);
449 seq_printf(s
, "Use: modify config_pin <devname> "\
450 "<state> <pinname> <value>\n");
454 pctldev
= get_pinctrl_dev_from_devname(found
->ctrl_dev_name
);
455 seq_printf(s
, "Dev %s has config of %s in state %s:\n",
456 dbg
->dev_name
, dbg
->pin_name
, dbg
->state_name
);
457 pinconf_show_config(s
, pctldev
, found
->data
.configs
.configs
,
458 found
->data
.configs
.num_configs
);
461 mutex_unlock(&pinctrl_maps_mutex
);
467 * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
468 * map, of a dev/pin/state entry based on user entries to pinconf-config
469 * @user_buf: contains the modification request with expected format:
470 * modify <config> <devicename> <state> <name> <newvalue>
471 * modify is literal string, alternatives like add/delete not supported yet
472 * <config> is the configuration to be changed. Supported configs are
473 * "config_pin" or "config_group", alternatives like config_mux are not
475 * <devicename> <state> <name> are values that should match the pinctrl-maps
476 * <newvalue> reflects the new config and is driver dependant
478 static ssize_t
pinconf_dbg_config_write(struct file
*file
,
479 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
481 struct pinctrl_maps
*maps_node
;
482 const struct pinctrl_map
*map
;
483 const struct pinctrl_map
*found
= NULL
;
484 struct pinctrl_dev
*pctldev
;
485 const struct pinconf_ops
*confops
= NULL
;
486 struct dbg_cfg
*dbg
= &pinconf_dbg_conf
;
487 const struct pinctrl_map_configs
*configs
;
488 char config
[MAX_NAME_LEN
+1];
495 /* Get userspace string and assure termination */
496 buf_size
= min(count
, sizeof(buf
) - 1);
497 if (copy_from_user(buf
, user_buf
, buf_size
))
502 * need to parse entry and extract parameters:
503 * modify configs_pin devicename state pinname newvalue
506 /* Get arg: 'modify' */
507 token
= strsep(&b
, " ");
510 if (strcmp(token
, "modify"))
514 * Get arg type: "config_pin" and "config_group"
515 * types are supported so far
517 token
= strsep(&b
, " ");
520 if (!strcmp(token
, "config_pin"))
521 dbg
->map_type
= PIN_MAP_TYPE_CONFIGS_PIN
;
522 else if (!strcmp(token
, "config_group"))
523 dbg
->map_type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
527 /* get arg 'device_name' */
528 token
= strsep(&b
, " ");
531 if (strlen(token
) >= MAX_NAME_LEN
)
533 strncpy(dbg
->dev_name
, token
, MAX_NAME_LEN
);
535 /* get arg 'state_name' */
536 token
= strsep(&b
, " ");
539 if (strlen(token
) >= MAX_NAME_LEN
)
541 strncpy(dbg
->state_name
, token
, MAX_NAME_LEN
);
543 /* get arg 'pin_name' */
544 token
= strsep(&b
, " ");
547 if (strlen(token
) >= MAX_NAME_LEN
)
549 strncpy(dbg
->pin_name
, token
, MAX_NAME_LEN
);
551 /* get new_value of config' */
552 token
= strsep(&b
, " ");
555 if (strlen(token
) >= MAX_NAME_LEN
)
557 strncpy(config
, token
, MAX_NAME_LEN
);
559 mutex_lock(&pinctrl_maps_mutex
);
561 /* Parse the pinctrl map and look for the selected dev/state/pin */
562 for_each_maps(maps_node
, i
, map
) {
563 if (strcmp(map
->dev_name
, dbg
->dev_name
))
565 if (map
->type
!= dbg
->map_type
)
567 if (strcmp(map
->name
, dbg
->state_name
))
570 /* we found the right pin / state, so overwrite config */
571 if (!strcmp(map
->data
.configs
.group_or_pin
, dbg
->pin_name
)) {
582 pctldev
= get_pinctrl_dev_from_devname(found
->ctrl_dev_name
);
584 confops
= pctldev
->desc
->confops
;
586 if (confops
&& confops
->pin_config_dbg_parse_modify
) {
587 configs
= &found
->data
.configs
;
588 for (i
= 0; i
< configs
->num_configs
; i
++) {
589 confops
->pin_config_dbg_parse_modify(pctldev
,
591 &configs
->configs
[i
]);
596 mutex_unlock(&pinctrl_maps_mutex
);
601 static int pinconf_dbg_config_open(struct inode
*inode
, struct file
*file
)
603 return single_open(file
, pinconf_dbg_config_print
, inode
->i_private
);
606 static const struct file_operations pinconf_dbg_pinconfig_fops
= {
607 .open
= pinconf_dbg_config_open
,
608 .write
= pinconf_dbg_config_write
,
611 .release
= single_release
,
612 .owner
= THIS_MODULE
,
615 void pinconf_init_device_debugfs(struct dentry
*devroot
,
616 struct pinctrl_dev
*pctldev
)
618 debugfs_create_file("pinconf-pins", S_IFREG
| S_IRUGO
,
619 devroot
, pctldev
, &pinconf_pins_ops
);
620 debugfs_create_file("pinconf-groups", S_IFREG
| S_IRUGO
,
621 devroot
, pctldev
, &pinconf_groups_ops
);
622 debugfs_create_file("pinconf-config", (S_IRUGO
| S_IWUSR
| S_IWGRP
),
623 devroot
, pctldev
, &pinconf_dbg_pinconfig_fops
);