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 must be able to read out pin status */
32 if (!ops->pin_config_get && !ops->pin_config_group_get) {
34 "pinconf must be able to read out pin status\n");
37 /* We have to be able to config the pins in SOME way */
38 if (!ops->pin_config_set && !ops->pin_config_group_set) {
40 "pinconf has to be able to set a pins config\n");
46 int pinconf_validate_map(struct pinctrl_map const *map, int i)
48 if (!map->data.configs.group_or_pin) {
49 pr_err("failed to register map %s (%d): no group/pin given\n",
54 if (!map->data.configs.num_configs ||
55 !map->data.configs.configs) {
56 pr_err("failed to register map %s (%d): no configs given\n",
64 int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
65 unsigned long *config)
67 const struct pinconf_ops *ops = pctldev->desc->confops;
69 if (!ops || !ops->pin_config_get) {
70 dev_err(pctldev->dev, "cannot get pin configuration, missing "
71 "pin_config_get() function in driver\n");
75 return ops->pin_config_get(pctldev, pin, config);
79 * pin_config_get() - get the configuration of a single pin parameter
80 * @dev_name: name of the pin controller device for this pin
81 * @name: name of the pin to get the config for
82 * @config: the config pointed to by this argument will be filled in with the
83 * current pin state, it can be used directly by drivers as a numeral, or
84 * it can be dereferenced to any struct.
86 int pin_config_get(const char *dev_name, const char *name,
87 unsigned long *config)
89 struct pinctrl_dev *pctldev;
92 pctldev = get_pinctrl_dev_from_devname(dev_name);
98 mutex_lock(&pctldev->mutex);
100 pin = pin_get_from_name(pctldev, name);
104 pin = pin_config_get_for_pin(pctldev, pin, config);
107 mutex_unlock(&pctldev->mutex);
110 EXPORT_SYMBOL(pin_config_get);
112 static int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
113 unsigned long config)
115 const struct pinconf_ops *ops = pctldev->desc->confops;
118 if (!ops || !ops->pin_config_set) {
119 dev_err(pctldev->dev, "cannot configure pin, missing "
120 "config function in driver\n");
124 ret = ops->pin_config_set(pctldev, pin, config);
126 dev_err(pctldev->dev,
127 "unable to set pin configuration on pin %d\n", pin);
135 * pin_config_set() - set the configuration of a single pin parameter
136 * @dev_name: name of pin controller device for this pin
137 * @name: name of the pin to set the config for
138 * @config: the config in this argument will contain the desired pin state, it
139 * can be used directly by drivers as a numeral, or it can be dereferenced
142 int pin_config_set(const char *dev_name, const char *name,
143 unsigned long config)
145 struct pinctrl_dev *pctldev;
148 pctldev = get_pinctrl_dev_from_devname(dev_name);
154 mutex_lock(&pctldev->mutex);
156 pin = pin_get_from_name(pctldev, name);
162 ret = pin_config_set_for_pin(pctldev, pin, config);
165 mutex_unlock(&pctldev->mutex);
168 EXPORT_SYMBOL(pin_config_set);
170 int pin_config_group_get(const char *dev_name, const char *pin_group,
171 unsigned long *config)
173 struct pinctrl_dev *pctldev;
174 const struct pinconf_ops *ops;
177 pctldev = get_pinctrl_dev_from_devname(dev_name);
183 mutex_lock(&pctldev->mutex);
185 ops = pctldev->desc->confops;
187 if (!ops || !ops->pin_config_group_get) {
188 dev_err(pctldev->dev, "cannot get configuration for pin "
189 "group, missing group config get function in "
195 selector = pinctrl_get_group_selector(pctldev, pin_group);
201 ret = ops->pin_config_group_get(pctldev, selector, config);
204 mutex_unlock(&pctldev->mutex);
207 EXPORT_SYMBOL(pin_config_group_get);
209 int pin_config_group_set(const char *dev_name, const char *pin_group,
210 unsigned long config)
212 struct pinctrl_dev *pctldev;
213 const struct pinconf_ops *ops;
214 const struct pinctrl_ops *pctlops;
216 const unsigned *pins;
221 pctldev = get_pinctrl_dev_from_devname(dev_name);
227 mutex_lock(&pctldev->mutex);
229 ops = pctldev->desc->confops;
230 pctlops = pctldev->desc->pctlops;
232 if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
233 dev_err(pctldev->dev, "cannot configure pin group, missing "
234 "config function in driver\n");
239 selector = pinctrl_get_group_selector(pctldev, pin_group);
245 ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
247 dev_err(pctldev->dev, "cannot configure pin group, error "
253 * If the pin controller supports handling entire groups we use that
256 if (ops->pin_config_group_set) {
257 ret = ops->pin_config_group_set(pctldev, selector, config);
259 * If the pin controller prefer that a certain group be handled
260 * pin-by-pin as well, it returns -EAGAIN.
267 * If the controller cannot handle entire groups, we configure each pin
270 if (!ops->pin_config_set) {
275 for (i = 0; i < num_pins; i++) {
276 ret = ops->pin_config_set(pctldev, pins[i], config);
284 mutex_unlock(&pctldev->mutex);
288 EXPORT_SYMBOL(pin_config_group_set);
290 int pinconf_map_to_setting(struct pinctrl_map const *map,
291 struct pinctrl_setting *setting)
293 struct pinctrl_dev *pctldev = setting->pctldev;
296 switch (setting->type) {
297 case PIN_MAP_TYPE_CONFIGS_PIN:
298 pin = pin_get_from_name(pctldev,
299 map->data.configs.group_or_pin);
301 dev_err(pctldev->dev, "could not map pin config for \"%s\"",
302 map->data.configs.group_or_pin);
305 setting->data.configs.group_or_pin = pin;
307 case PIN_MAP_TYPE_CONFIGS_GROUP:
308 pin = pinctrl_get_group_selector(pctldev,
309 map->data.configs.group_or_pin);
311 dev_err(pctldev->dev, "could not map group config for \"%s\"",
312 map->data.configs.group_or_pin);
315 setting->data.configs.group_or_pin = pin;
321 setting->data.configs.num_configs = map->data.configs.num_configs;
322 setting->data.configs.configs = map->data.configs.configs;
327 void pinconf_free_setting(struct pinctrl_setting const *setting)
331 int pinconf_apply_setting(struct pinctrl_setting const *setting)
333 struct pinctrl_dev *pctldev = setting->pctldev;
334 const struct pinconf_ops *ops = pctldev->desc->confops;
338 dev_err(pctldev->dev, "missing confops\n");
342 switch (setting->type) {
343 case PIN_MAP_TYPE_CONFIGS_PIN:
344 if (!ops->pin_config_set) {
345 dev_err(pctldev->dev, "missing pin_config_set op\n");
348 for (i = 0; i < setting->data.configs.num_configs; i++) {
349 ret = ops->pin_config_set(pctldev,
350 setting->data.configs.group_or_pin,
351 setting->data.configs.configs[i]);
353 dev_err(pctldev->dev,
354 "pin_config_set op failed for pin %d config %08lx\n",
355 setting->data.configs.group_or_pin,
356 setting->data.configs.configs[i]);
361 case PIN_MAP_TYPE_CONFIGS_GROUP:
362 if (!ops->pin_config_group_set) {
363 dev_err(pctldev->dev,
364 "missing pin_config_group_set op\n");
367 for (i = 0; i < setting->data.configs.num_configs; i++) {
368 ret = ops->pin_config_group_set(pctldev,
369 setting->data.configs.group_or_pin,
370 setting->data.configs.configs[i]);
372 dev_err(pctldev->dev,
373 "pin_config_group_set op failed for group %d config %08lx\n",
374 setting->data.configs.group_or_pin,
375 setting->data.configs.configs[i]);
387 #ifdef CONFIG_DEBUG_FS
389 void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
391 struct pinctrl_dev *pctldev;
392 const struct pinconf_ops *confops;
395 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
397 confops = pctldev->desc->confops;
402 case PIN_MAP_TYPE_CONFIGS_PIN:
403 seq_printf(s, "pin ");
405 case PIN_MAP_TYPE_CONFIGS_GROUP:
406 seq_printf(s, "group ");
412 seq_printf(s, "%s\n", map->data.configs.group_or_pin);
414 for (i = 0; i < map->data.configs.num_configs; i++) {
415 seq_printf(s, "config ");
416 if (confops && confops->pin_config_config_dbg_show)
417 confops->pin_config_config_dbg_show(pctldev, s,
418 map->data.configs.configs[i]);
420 seq_printf(s, "%08lx", map->data.configs.configs[i]);
425 void pinconf_show_setting(struct seq_file *s,
426 struct pinctrl_setting const *setting)
428 struct pinctrl_dev *pctldev = setting->pctldev;
429 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
430 const struct pinconf_ops *confops = pctldev->desc->confops;
431 struct pin_desc *desc;
434 switch (setting->type) {
435 case PIN_MAP_TYPE_CONFIGS_PIN:
436 desc = pin_desc_get(setting->pctldev,
437 setting->data.configs.group_or_pin);
438 seq_printf(s, "pin %s (%d)",
439 desc->name ? desc->name : "unnamed",
440 setting->data.configs.group_or_pin);
442 case PIN_MAP_TYPE_CONFIGS_GROUP:
443 seq_printf(s, "group %s (%d)",
444 pctlops->get_group_name(pctldev,
445 setting->data.configs.group_or_pin),
446 setting->data.configs.group_or_pin);
453 * FIXME: We should really get the pin controler to dump the config
454 * values, so they can be decoded to something meaningful.
456 for (i = 0; i < setting->data.configs.num_configs; i++) {
458 if (confops && confops->pin_config_config_dbg_show)
459 confops->pin_config_config_dbg_show(pctldev, s,
460 setting->data.configs.configs[i]);
462 seq_printf(s, "%08lx",
463 setting->data.configs.configs[i]);
469 static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
470 struct seq_file *s, int pin)
472 const struct pinconf_ops *ops = pctldev->desc->confops;
474 /* no-op when not using generic pin config */
475 pinconf_generic_dump_pin(pctldev, s, pin);
476 if (ops && ops->pin_config_dbg_show)
477 ops->pin_config_dbg_show(pctldev, s, pin);
480 static int pinconf_pins_show(struct seq_file *s, void *what)
482 struct pinctrl_dev *pctldev = s->private;
483 const struct pinconf_ops *ops = pctldev->desc->confops;
486 if (!ops || !ops->pin_config_get)
489 seq_puts(s, "Pin config settings per pin\n");
490 seq_puts(s, "Format: pin (name): configs\n");
492 mutex_lock(&pctldev->mutex);
494 /* The pin number can be retrived from the pin controller descriptor */
495 for (i = 0; i < pctldev->desc->npins; i++) {
496 struct pin_desc *desc;
498 pin = pctldev->desc->pins[i].number;
499 desc = pin_desc_get(pctldev, pin);
500 /* Skip if we cannot search the pin */
504 seq_printf(s, "pin %d (%s):", pin,
505 desc->name ? desc->name : "unnamed");
507 pinconf_dump_pin(pctldev, s, pin);
512 mutex_unlock(&pctldev->mutex);
517 static void pinconf_dump_group(struct pinctrl_dev *pctldev,
518 struct seq_file *s, unsigned selector,
521 const struct pinconf_ops *ops = pctldev->desc->confops;
523 /* no-op when not using generic pin config */
524 pinconf_generic_dump_group(pctldev, s, gname);
525 if (ops && ops->pin_config_group_dbg_show)
526 ops->pin_config_group_dbg_show(pctldev, s, selector);
529 static int pinconf_groups_show(struct seq_file *s, void *what)
531 struct pinctrl_dev *pctldev = s->private;
532 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
533 const struct pinconf_ops *ops = pctldev->desc->confops;
534 unsigned ngroups = pctlops->get_groups_count(pctldev);
535 unsigned selector = 0;
537 if (!ops || !ops->pin_config_group_get)
540 seq_puts(s, "Pin config settings per pin group\n");
541 seq_puts(s, "Format: group (name): configs\n");
543 while (selector < ngroups) {
544 const char *gname = pctlops->get_group_name(pctldev, selector);
546 seq_printf(s, "%u (%s):", selector, gname);
547 pinconf_dump_group(pctldev, s, selector, gname);
556 static int pinconf_pins_open(struct inode *inode, struct file *file)
558 return single_open(file, pinconf_pins_show, inode->i_private);
561 static int pinconf_groups_open(struct inode *inode, struct file *file)
563 return single_open(file, pinconf_groups_show, inode->i_private);
566 static const struct file_operations pinconf_pins_ops = {
567 .open = pinconf_pins_open,
570 .release = single_release,
573 static const struct file_operations pinconf_groups_ops = {
574 .open = pinconf_groups_open,
577 .release = single_release,
580 #define MAX_NAME_LEN 15
583 enum pinctrl_map_type map_type;
584 char dev_name[MAX_NAME_LEN+1];
585 char state_name[MAX_NAME_LEN+1];
586 char pin_name[MAX_NAME_LEN+1];
590 * Goal is to keep this structure as global in order to simply read the
591 * pinconf-config file after a write to check config is as expected
593 static struct dbg_cfg pinconf_dbg_conf;
596 * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
597 * map, of the dev/pin/state that was last written to pinconf-config file.
598 * @s: string filled in with config description
601 static int pinconf_dbg_config_print(struct seq_file *s, void *d)
603 struct pinctrl_maps *maps_node;
604 const struct pinctrl_map *map;
605 struct pinctrl_dev *pctldev = NULL;
606 const struct pinconf_ops *confops = NULL;
607 const struct pinctrl_map_configs *configs;
608 struct dbg_cfg *dbg = &pinconf_dbg_conf;
611 unsigned long config;
613 mutex_lock(&pctldev->mutex);
615 /* Parse the pinctrl map and look for the elected pin/state */
616 for_each_maps(maps_node, i, map) {
617 if (map->type != dbg->map_type)
619 if (strcmp(map->dev_name, dbg->dev_name))
621 if (strcmp(map->name, dbg->state_name))
624 for (j = 0; j < map->data.configs.num_configs; j++) {
625 if (!strcmp(map->data.configs.group_or_pin,
628 * We found the right pin / state, read the
629 * config and he pctldev for later use
631 configs = &map->data.configs;
632 pctldev = get_pinctrl_dev_from_devname
633 (map->ctrl_dev_name);
641 seq_printf(s, "No config found for dev/state/pin, expected:\n");
642 seq_printf(s, "Searched dev:%s\n", dbg->dev_name);
643 seq_printf(s, "Searched state:%s\n", dbg->state_name);
644 seq_printf(s, "Searched pin:%s\n", dbg->pin_name);
645 seq_printf(s, "Use: modify config_pin <devname> "\
646 "<state> <pinname> <value>\n");
650 config = *(configs->configs);
651 seq_printf(s, "Dev %s has config of %s in state %s: 0x%08lX\n",
652 dbg->dev_name, dbg->pin_name,
653 dbg->state_name, config);
656 confops = pctldev->desc->confops;
658 if (confops && confops->pin_config_config_dbg_show)
659 confops->pin_config_config_dbg_show(pctldev, s, config);
662 mutex_unlock(&pctldev->mutex);
668 * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
669 * map, of a dev/pin/state entry based on user entries to pinconf-config
670 * @user_buf: contains the modification request with expected format:
671 * modify config_pin <devicename> <state> <pinname> <newvalue>
672 * modify is literal string, alternatives like add/delete not supported yet
673 * config_pin is literal, alternatives like config_mux not supported yet
674 * <devicename> <state> <pinname> are values that should match the pinctrl-maps
675 * <newvalue> reflects the new config and is driver dependant
677 static int pinconf_dbg_config_write(struct file *file,
678 const char __user *user_buf, size_t count, loff_t *ppos)
680 struct pinctrl_maps *maps_node;
681 const struct pinctrl_map *map;
682 struct pinctrl_dev *pctldev = NULL;
683 const struct pinconf_ops *confops = NULL;
684 struct dbg_cfg *dbg = &pinconf_dbg_conf;
685 const struct pinctrl_map_configs *configs;
686 char config[MAX_NAME_LEN+1];
694 /* Get userspace string and assure termination */
695 buf_size = min(count, (sizeof(buf)-1));
696 if (copy_from_user(buf, user_buf, buf_size))
701 * need to parse entry and extract parameters:
702 * modify configs_pin devicename state pinname newvalue
705 /* Get arg: 'modify' */
706 token = strsep(&b, " ");
709 if (strcmp(token, "modify"))
712 /* Get arg type: "config_pin" type supported so far */
713 token = strsep(&b, " ");
716 if (strcmp(token, "config_pin"))
718 dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN;
720 /* get arg 'device_name' */
721 token = strsep(&b, " ");
724 if (strlen(token) >= MAX_NAME_LEN)
726 strncpy(dbg->dev_name, token, MAX_NAME_LEN);
728 /* get arg 'state_name' */
729 token = strsep(&b, " ");
732 if (strlen(token) >= MAX_NAME_LEN)
734 strncpy(dbg->state_name, token, MAX_NAME_LEN);
736 /* get arg 'pin_name' */
737 token = strsep(&b, " ");
740 if (strlen(token) >= MAX_NAME_LEN)
742 strncpy(dbg->pin_name, token, MAX_NAME_LEN);
744 /* get new_value of config' */
745 token = strsep(&b, " ");
748 if (strlen(token) >= MAX_NAME_LEN)
750 strncpy(config, token, MAX_NAME_LEN);
752 mutex_lock(&pinctrl_maps_mutex);
754 /* Parse the pinctrl map and look for the selected dev/state/pin */
755 for_each_maps(maps_node, i, map) {
756 if (strcmp(map->dev_name, dbg->dev_name))
758 if (map->type != dbg->map_type)
760 if (strcmp(map->name, dbg->state_name))
763 /* we found the right pin / state, so overwrite config */
764 if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
766 pctldev = get_pinctrl_dev_from_devname(
768 configs = &map->data.configs;
779 confops = pctldev->desc->confops;
781 if (confops && confops->pin_config_dbg_parse_modify) {
782 for (i = 0; i < configs->num_configs; i++) {
783 confops->pin_config_dbg_parse_modify(pctldev,
785 &configs->configs[i]);
790 mutex_unlock(&pinctrl_maps_mutex);
795 static int pinconf_dbg_config_open(struct inode *inode, struct file *file)
797 return single_open(file, pinconf_dbg_config_print, inode->i_private);
800 static const struct file_operations pinconf_dbg_pinconfig_fops = {
801 .open = pinconf_dbg_config_open,
802 .write = pinconf_dbg_config_write,
805 .release = single_release,
806 .owner = THIS_MODULE,
809 void pinconf_init_device_debugfs(struct dentry *devroot,
810 struct pinctrl_dev *pctldev)
812 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
813 devroot, pctldev, &pinconf_pins_ops);
814 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
815 devroot, pctldev, &pinconf_groups_ops);
816 debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP),
817 devroot, pctldev, &pinconf_dbg_pinconfig_fops);