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
) {
64 dev_dbg(pctldev
->dev
, "cannot get pin configuration, missing "
65 "pin_config_get() function 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 #ifdef CONFIG_DEBUG_FS
205 void pinconf_show_map(struct seq_file
*s
, struct pinctrl_map
const *map
)
207 struct pinctrl_dev
*pctldev
;
208 const struct pinconf_ops
*confops
;
211 pctldev
= get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
213 confops
= pctldev
->desc
->confops
;
218 case PIN_MAP_TYPE_CONFIGS_PIN
:
219 seq_printf(s
, "pin ");
221 case PIN_MAP_TYPE_CONFIGS_GROUP
:
222 seq_printf(s
, "group ");
228 seq_printf(s
, "%s\n", map
->data
.configs
.group_or_pin
);
230 for (i
= 0; i
< map
->data
.configs
.num_configs
; i
++) {
231 seq_printf(s
, "config ");
232 if (confops
&& confops
->pin_config_config_dbg_show
)
233 confops
->pin_config_config_dbg_show(pctldev
, s
,
234 map
->data
.configs
.configs
[i
]);
236 seq_printf(s
, "%08lx", map
->data
.configs
.configs
[i
]);
241 void pinconf_show_setting(struct seq_file
*s
,
242 struct pinctrl_setting
const *setting
)
244 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
245 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
246 const struct pinconf_ops
*confops
= pctldev
->desc
->confops
;
247 struct pin_desc
*desc
;
250 switch (setting
->type
) {
251 case PIN_MAP_TYPE_CONFIGS_PIN
:
252 desc
= pin_desc_get(setting
->pctldev
,
253 setting
->data
.configs
.group_or_pin
);
254 seq_printf(s
, "pin %s (%d)",
255 desc
->name
? desc
->name
: "unnamed",
256 setting
->data
.configs
.group_or_pin
);
258 case PIN_MAP_TYPE_CONFIGS_GROUP
:
259 seq_printf(s
, "group %s (%d)",
260 pctlops
->get_group_name(pctldev
,
261 setting
->data
.configs
.group_or_pin
),
262 setting
->data
.configs
.group_or_pin
);
269 * FIXME: We should really get the pin controler to dump the config
270 * values, so they can be decoded to something meaningful.
272 for (i
= 0; i
< setting
->data
.configs
.num_configs
; i
++) {
274 if (confops
&& confops
->pin_config_config_dbg_show
)
275 confops
->pin_config_config_dbg_show(pctldev
, s
,
276 setting
->data
.configs
.configs
[i
]);
278 seq_printf(s
, "%08lx",
279 setting
->data
.configs
.configs
[i
]);
285 static void pinconf_dump_pin(struct pinctrl_dev
*pctldev
,
286 struct seq_file
*s
, int pin
)
288 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
290 /* no-op when not using generic pin config */
291 pinconf_generic_dump_pin(pctldev
, s
, pin
);
292 if (ops
&& ops
->pin_config_dbg_show
)
293 ops
->pin_config_dbg_show(pctldev
, s
, pin
);
296 static int pinconf_pins_show(struct seq_file
*s
, void *what
)
298 struct pinctrl_dev
*pctldev
= s
->private;
301 seq_puts(s
, "Pin config settings per pin\n");
302 seq_puts(s
, "Format: pin (name): configs\n");
304 mutex_lock(&pctldev
->mutex
);
306 /* The pin number can be retrived from the pin controller descriptor */
307 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
308 struct pin_desc
*desc
;
310 pin
= pctldev
->desc
->pins
[i
].number
;
311 desc
= pin_desc_get(pctldev
, pin
);
312 /* Skip if we cannot search the pin */
316 seq_printf(s
, "pin %d (%s):", pin
,
317 desc
->name
? desc
->name
: "unnamed");
319 pinconf_dump_pin(pctldev
, s
, pin
);
324 mutex_unlock(&pctldev
->mutex
);
329 static void pinconf_dump_group(struct pinctrl_dev
*pctldev
,
330 struct seq_file
*s
, unsigned selector
,
333 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
335 /* no-op when not using generic pin config */
336 pinconf_generic_dump_group(pctldev
, s
, gname
);
337 if (ops
&& ops
->pin_config_group_dbg_show
)
338 ops
->pin_config_group_dbg_show(pctldev
, s
, selector
);
341 static int pinconf_groups_show(struct seq_file
*s
, void *what
)
343 struct pinctrl_dev
*pctldev
= s
->private;
344 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
345 unsigned ngroups
= pctlops
->get_groups_count(pctldev
);
346 unsigned selector
= 0;
348 seq_puts(s
, "Pin config settings per pin group\n");
349 seq_puts(s
, "Format: group (name): configs\n");
351 while (selector
< ngroups
) {
352 const char *gname
= pctlops
->get_group_name(pctldev
, selector
);
354 seq_printf(s
, "%u (%s):", selector
, gname
);
355 pinconf_dump_group(pctldev
, s
, selector
, gname
);
364 static int pinconf_pins_open(struct inode
*inode
, struct file
*file
)
366 return single_open(file
, pinconf_pins_show
, inode
->i_private
);
369 static int pinconf_groups_open(struct inode
*inode
, struct file
*file
)
371 return single_open(file
, pinconf_groups_show
, inode
->i_private
);
374 static const struct file_operations pinconf_pins_ops
= {
375 .open
= pinconf_pins_open
,
378 .release
= single_release
,
381 static const struct file_operations pinconf_groups_ops
= {
382 .open
= pinconf_groups_open
,
385 .release
= single_release
,
388 #define MAX_NAME_LEN 15
391 enum pinctrl_map_type map_type
;
392 char dev_name
[MAX_NAME_LEN
+1];
393 char state_name
[MAX_NAME_LEN
+1];
394 char pin_name
[MAX_NAME_LEN
+1];
398 * Goal is to keep this structure as global in order to simply read the
399 * pinconf-config file after a write to check config is as expected
401 static struct dbg_cfg pinconf_dbg_conf
;
404 * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
405 * map, of the dev/pin/state that was last written to pinconf-config file.
406 * @s: string filled in with config description
409 static int pinconf_dbg_config_print(struct seq_file
*s
, void *d
)
411 struct pinctrl_maps
*maps_node
;
412 const struct pinctrl_map
*map
;
413 const struct pinctrl_map
*found
= NULL
;
414 struct pinctrl_dev
*pctldev
;
415 const struct pinconf_ops
*confops
= NULL
;
416 struct dbg_cfg
*dbg
= &pinconf_dbg_conf
;
418 unsigned long config
;
420 mutex_lock(&pinctrl_maps_mutex
);
422 /* Parse the pinctrl map and look for the elected pin/state */
423 for_each_maps(maps_node
, i
, map
) {
424 if (map
->type
!= dbg
->map_type
)
426 if (strcmp(map
->dev_name
, dbg
->dev_name
))
428 if (strcmp(map
->name
, dbg
->state_name
))
431 for (j
= 0; j
< map
->data
.configs
.num_configs
; j
++) {
432 if (!strcmp(map
->data
.configs
.group_or_pin
,
434 /* We found the right pin / state */
442 seq_printf(s
, "No config found for dev/state/pin, expected:\n");
443 seq_printf(s
, "Searched dev:%s\n", dbg
->dev_name
);
444 seq_printf(s
, "Searched state:%s\n", dbg
->state_name
);
445 seq_printf(s
, "Searched pin:%s\n", dbg
->pin_name
);
446 seq_printf(s
, "Use: modify config_pin <devname> "\
447 "<state> <pinname> <value>\n");
451 pctldev
= get_pinctrl_dev_from_devname(found
->ctrl_dev_name
);
452 config
= *found
->data
.configs
.configs
;
453 seq_printf(s
, "Dev %s has config of %s in state %s: 0x%08lX\n",
454 dbg
->dev_name
, dbg
->pin_name
,
455 dbg
->state_name
, config
);
458 confops
= pctldev
->desc
->confops
;
460 if (confops
&& confops
->pin_config_config_dbg_show
)
461 confops
->pin_config_config_dbg_show(pctldev
, s
, config
);
464 mutex_unlock(&pinctrl_maps_mutex
);
470 * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
471 * map, of a dev/pin/state entry based on user entries to pinconf-config
472 * @user_buf: contains the modification request with expected format:
473 * modify config_pin <devicename> <state> <pinname> <newvalue>
474 * modify is literal string, alternatives like add/delete not supported yet
475 * config_pin is literal, alternatives like config_mux not supported yet
476 * <devicename> <state> <pinname> are values that should match the pinctrl-maps
477 * <newvalue> reflects the new config and is driver dependant
479 static ssize_t
pinconf_dbg_config_write(struct file
*file
,
480 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
482 struct pinctrl_maps
*maps_node
;
483 const struct pinctrl_map
*map
;
484 const struct pinctrl_map
*found
= NULL
;
485 struct pinctrl_dev
*pctldev
;
486 const struct pinconf_ops
*confops
= NULL
;
487 struct dbg_cfg
*dbg
= &pinconf_dbg_conf
;
488 const struct pinctrl_map_configs
*configs
;
489 char config
[MAX_NAME_LEN
+1];
496 /* Get userspace string and assure termination */
497 buf_size
= min(count
, sizeof(buf
) - 1);
498 if (copy_from_user(buf
, user_buf
, buf_size
))
503 * need to parse entry and extract parameters:
504 * modify configs_pin devicename state pinname newvalue
507 /* Get arg: 'modify' */
508 token
= strsep(&b
, " ");
511 if (strcmp(token
, "modify"))
514 /* Get arg type: "config_pin" type supported so far */
515 token
= strsep(&b
, " ");
518 if (strcmp(token
, "config_pin"))
520 dbg
->map_type
= PIN_MAP_TYPE_CONFIGS_PIN
;
522 /* get arg 'device_name' */
523 token
= strsep(&b
, " ");
526 if (strlen(token
) >= MAX_NAME_LEN
)
528 strncpy(dbg
->dev_name
, token
, MAX_NAME_LEN
);
530 /* get arg 'state_name' */
531 token
= strsep(&b
, " ");
534 if (strlen(token
) >= MAX_NAME_LEN
)
536 strncpy(dbg
->state_name
, token
, MAX_NAME_LEN
);
538 /* get arg 'pin_name' */
539 token
= strsep(&b
, " ");
542 if (strlen(token
) >= MAX_NAME_LEN
)
544 strncpy(dbg
->pin_name
, token
, MAX_NAME_LEN
);
546 /* get new_value of config' */
547 token
= strsep(&b
, " ");
550 if (strlen(token
) >= MAX_NAME_LEN
)
552 strncpy(config
, token
, MAX_NAME_LEN
);
554 mutex_lock(&pinctrl_maps_mutex
);
556 /* Parse the pinctrl map and look for the selected dev/state/pin */
557 for_each_maps(maps_node
, i
, map
) {
558 if (strcmp(map
->dev_name
, dbg
->dev_name
))
560 if (map
->type
!= dbg
->map_type
)
562 if (strcmp(map
->name
, dbg
->state_name
))
565 /* we found the right pin / state, so overwrite config */
566 if (!strcmp(map
->data
.configs
.group_or_pin
, dbg
->pin_name
)) {
577 pctldev
= get_pinctrl_dev_from_devname(found
->ctrl_dev_name
);
579 confops
= pctldev
->desc
->confops
;
581 if (confops
&& confops
->pin_config_dbg_parse_modify
) {
582 configs
= &found
->data
.configs
;
583 for (i
= 0; i
< configs
->num_configs
; i
++) {
584 confops
->pin_config_dbg_parse_modify(pctldev
,
586 &configs
->configs
[i
]);
591 mutex_unlock(&pinctrl_maps_mutex
);
596 static int pinconf_dbg_config_open(struct inode
*inode
, struct file
*file
)
598 return single_open(file
, pinconf_dbg_config_print
, inode
->i_private
);
601 static const struct file_operations pinconf_dbg_pinconfig_fops
= {
602 .open
= pinconf_dbg_config_open
,
603 .write
= pinconf_dbg_config_write
,
606 .release
= single_release
,
607 .owner
= THIS_MODULE
,
610 void pinconf_init_device_debugfs(struct dentry
*devroot
,
611 struct pinctrl_dev
*pctldev
)
613 debugfs_create_file("pinconf-pins", S_IFREG
| S_IRUGO
,
614 devroot
, pctldev
, &pinconf_pins_ops
);
615 debugfs_create_file("pinconf-groups", S_IFREG
| S_IRUGO
,
616 devroot
, pctldev
, &pinconf_groups_ops
);
617 debugfs_create_file("pinconf-config", (S_IRUGO
| S_IWUSR
| S_IWGRP
),
618 devroot
, pctldev
, &pinconf_dbg_pinconfig_fops
);