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 mutex_lock(&pinctrl_mutex);
94 pctldev = get_pinctrl_dev_from_devname(dev_name);
100 pin = pin_get_from_name(pctldev, name);
104 pin = pin_config_get_for_pin(pctldev, pin, config);
107 mutex_unlock(&pinctrl_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 mutex_lock(&pinctrl_mutex);
150 pctldev = get_pinctrl_dev_from_devname(dev_name);
156 pin = pin_get_from_name(pctldev, name);
162 ret = pin_config_set_for_pin(pctldev, pin, config);
165 mutex_unlock(&pinctrl_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 mutex_lock(&pinctrl_mutex);
179 pctldev = get_pinctrl_dev_from_devname(dev_name);
184 ops = pctldev->desc->confops;
186 if (!ops || !ops->pin_config_group_get) {
187 dev_err(pctldev->dev, "cannot get configuration for pin "
188 "group, missing group config get function in "
194 selector = pinctrl_get_group_selector(pctldev, pin_group);
200 ret = ops->pin_config_group_get(pctldev, selector, config);
203 mutex_unlock(&pinctrl_mutex);
206 EXPORT_SYMBOL(pin_config_group_get);
208 int pin_config_group_set(const char *dev_name, const char *pin_group,
209 unsigned long config)
211 struct pinctrl_dev *pctldev;
212 const struct pinconf_ops *ops;
213 const struct pinctrl_ops *pctlops;
215 const unsigned *pins;
220 mutex_lock(&pinctrl_mutex);
222 pctldev = get_pinctrl_dev_from_devname(dev_name);
227 ops = pctldev->desc->confops;
228 pctlops = pctldev->desc->pctlops;
230 if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
231 dev_err(pctldev->dev, "cannot configure pin group, missing "
232 "config function in driver\n");
237 selector = pinctrl_get_group_selector(pctldev, pin_group);
243 ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
245 dev_err(pctldev->dev, "cannot configure pin group, error "
251 * If the pin controller supports handling entire groups we use that
254 if (ops->pin_config_group_set) {
255 ret = ops->pin_config_group_set(pctldev, selector, config);
257 * If the pin controller prefer that a certain group be handled
258 * pin-by-pin as well, it returns -EAGAIN.
265 * If the controller cannot handle entire groups, we configure each pin
268 if (!ops->pin_config_set) {
273 for (i = 0; i < num_pins; i++) {
274 ret = ops->pin_config_set(pctldev, pins[i], config);
282 mutex_unlock(&pinctrl_mutex);
286 EXPORT_SYMBOL(pin_config_group_set);
288 int pinconf_map_to_setting(struct pinctrl_map const *map,
289 struct pinctrl_setting *setting)
291 struct pinctrl_dev *pctldev = setting->pctldev;
294 switch (setting->type) {
295 case PIN_MAP_TYPE_CONFIGS_PIN:
296 pin = pin_get_from_name(pctldev,
297 map->data.configs.group_or_pin);
299 dev_err(pctldev->dev, "could not map pin config for \"%s\"",
300 map->data.configs.group_or_pin);
303 setting->data.configs.group_or_pin = pin;
305 case PIN_MAP_TYPE_CONFIGS_GROUP:
306 pin = pinctrl_get_group_selector(pctldev,
307 map->data.configs.group_or_pin);
309 dev_err(pctldev->dev, "could not map group config for \"%s\"",
310 map->data.configs.group_or_pin);
313 setting->data.configs.group_or_pin = pin;
319 setting->data.configs.num_configs = map->data.configs.num_configs;
320 setting->data.configs.configs = map->data.configs.configs;
325 void pinconf_free_setting(struct pinctrl_setting const *setting)
329 int pinconf_apply_setting(struct pinctrl_setting const *setting)
331 struct pinctrl_dev *pctldev = setting->pctldev;
332 const struct pinconf_ops *ops = pctldev->desc->confops;
336 dev_err(pctldev->dev, "missing confops\n");
340 switch (setting->type) {
341 case PIN_MAP_TYPE_CONFIGS_PIN:
342 if (!ops->pin_config_set) {
343 dev_err(pctldev->dev, "missing pin_config_set op\n");
346 for (i = 0; i < setting->data.configs.num_configs; i++) {
347 ret = ops->pin_config_set(pctldev,
348 setting->data.configs.group_or_pin,
349 setting->data.configs.configs[i]);
351 dev_err(pctldev->dev,
352 "pin_config_set op failed for pin %d config %08lx\n",
353 setting->data.configs.group_or_pin,
354 setting->data.configs.configs[i]);
359 case PIN_MAP_TYPE_CONFIGS_GROUP:
360 if (!ops->pin_config_group_set) {
361 dev_err(pctldev->dev,
362 "missing pin_config_group_set op\n");
365 for (i = 0; i < setting->data.configs.num_configs; i++) {
366 ret = ops->pin_config_group_set(pctldev,
367 setting->data.configs.group_or_pin,
368 setting->data.configs.configs[i]);
370 dev_err(pctldev->dev,
371 "pin_config_group_set op failed for group %d config %08lx\n",
372 setting->data.configs.group_or_pin,
373 setting->data.configs.configs[i]);
385 #ifdef CONFIG_DEBUG_FS
387 void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
389 struct pinctrl_dev *pctldev;
390 const struct pinconf_ops *confops;
393 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
395 confops = pctldev->desc->confops;
400 case PIN_MAP_TYPE_CONFIGS_PIN:
401 seq_printf(s, "pin ");
403 case PIN_MAP_TYPE_CONFIGS_GROUP:
404 seq_printf(s, "group ");
410 seq_printf(s, "%s\n", map->data.configs.group_or_pin);
412 for (i = 0; i < map->data.configs.num_configs; i++) {
413 seq_printf(s, "config ");
414 if (confops && confops->pin_config_config_dbg_show)
415 confops->pin_config_config_dbg_show(pctldev, s,
416 map->data.configs.configs[i]);
418 seq_printf(s, "%08lx", map->data.configs.configs[i]);
423 void pinconf_show_setting(struct seq_file *s,
424 struct pinctrl_setting const *setting)
426 struct pinctrl_dev *pctldev = setting->pctldev;
427 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
428 const struct pinconf_ops *confops = pctldev->desc->confops;
429 struct pin_desc *desc;
432 switch (setting->type) {
433 case PIN_MAP_TYPE_CONFIGS_PIN:
434 desc = pin_desc_get(setting->pctldev,
435 setting->data.configs.group_or_pin);
436 seq_printf(s, "pin %s (%d)",
437 desc->name ? desc->name : "unnamed",
438 setting->data.configs.group_or_pin);
440 case PIN_MAP_TYPE_CONFIGS_GROUP:
441 seq_printf(s, "group %s (%d)",
442 pctlops->get_group_name(pctldev,
443 setting->data.configs.group_or_pin),
444 setting->data.configs.group_or_pin);
451 * FIXME: We should really get the pin controler to dump the config
452 * values, so they can be decoded to something meaningful.
454 for (i = 0; i < setting->data.configs.num_configs; i++) {
456 if (confops && confops->pin_config_config_dbg_show)
457 confops->pin_config_config_dbg_show(pctldev, s,
458 setting->data.configs.configs[i]);
460 seq_printf(s, "%08lx",
461 setting->data.configs.configs[i]);
467 static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
468 struct seq_file *s, int pin)
470 const struct pinconf_ops *ops = pctldev->desc->confops;
472 /* no-op when not using generic pin config */
473 pinconf_generic_dump_pin(pctldev, s, pin);
474 if (ops && ops->pin_config_dbg_show)
475 ops->pin_config_dbg_show(pctldev, s, pin);
478 static int pinconf_pins_show(struct seq_file *s, void *what)
480 struct pinctrl_dev *pctldev = s->private;
481 const struct pinconf_ops *ops = pctldev->desc->confops;
484 if (!ops || !ops->pin_config_get)
487 seq_puts(s, "Pin config settings per pin\n");
488 seq_puts(s, "Format: pin (name): configs\n");
490 mutex_lock(&pinctrl_mutex);
492 /* The pin number can be retrived from the pin controller descriptor */
493 for (i = 0; i < pctldev->desc->npins; i++) {
494 struct pin_desc *desc;
496 pin = pctldev->desc->pins[i].number;
497 desc = pin_desc_get(pctldev, pin);
498 /* Skip if we cannot search the pin */
502 seq_printf(s, "pin %d (%s):", pin,
503 desc->name ? desc->name : "unnamed");
505 pinconf_dump_pin(pctldev, s, pin);
510 mutex_unlock(&pinctrl_mutex);
515 static void pinconf_dump_group(struct pinctrl_dev *pctldev,
516 struct seq_file *s, unsigned selector,
519 const struct pinconf_ops *ops = pctldev->desc->confops;
521 /* no-op when not using generic pin config */
522 pinconf_generic_dump_group(pctldev, s, gname);
523 if (ops && ops->pin_config_group_dbg_show)
524 ops->pin_config_group_dbg_show(pctldev, s, selector);
527 static int pinconf_groups_show(struct seq_file *s, void *what)
529 struct pinctrl_dev *pctldev = s->private;
530 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
531 const struct pinconf_ops *ops = pctldev->desc->confops;
532 unsigned ngroups = pctlops->get_groups_count(pctldev);
533 unsigned selector = 0;
535 if (!ops || !ops->pin_config_group_get)
538 seq_puts(s, "Pin config settings per pin group\n");
539 seq_puts(s, "Format: group (name): configs\n");
541 while (selector < ngroups) {
542 const char *gname = pctlops->get_group_name(pctldev, selector);
544 seq_printf(s, "%u (%s):", selector, gname);
545 pinconf_dump_group(pctldev, s, selector, gname);
554 static int pinconf_pins_open(struct inode *inode, struct file *file)
556 return single_open(file, pinconf_pins_show, inode->i_private);
559 static int pinconf_groups_open(struct inode *inode, struct file *file)
561 return single_open(file, pinconf_groups_show, inode->i_private);
564 static const struct file_operations pinconf_pins_ops = {
565 .open = pinconf_pins_open,
568 .release = single_release,
571 static const struct file_operations pinconf_groups_ops = {
572 .open = pinconf_groups_open,
575 .release = single_release,
578 #define MAX_NAME_LEN 15
581 enum pinctrl_map_type map_type;
582 char dev_name[MAX_NAME_LEN+1];
583 char state_name[MAX_NAME_LEN+1];
584 char pin_name[MAX_NAME_LEN+1];
588 * Goal is to keep this structure as global in order to simply read the
589 * pinconf-config file after a write to check config is as expected
591 static struct dbg_cfg pinconf_dbg_conf;
594 * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
595 * map, of the dev/pin/state that was last written to pinconf-config file.
596 * @s: string filled in with config description
599 static int pinconf_dbg_config_print(struct seq_file *s, void *d)
601 struct pinctrl_maps *maps_node;
602 const struct pinctrl_map *map;
603 struct pinctrl_dev *pctldev = NULL;
604 const struct pinconf_ops *confops = NULL;
605 const struct pinctrl_map_configs *configs;
606 struct dbg_cfg *dbg = &pinconf_dbg_conf;
609 unsigned long config;
611 mutex_lock(&pinctrl_mutex);
613 /* Parse the pinctrl map and look for the elected pin/state */
614 for_each_maps(maps_node, i, map) {
615 if (map->type != dbg->map_type)
617 if (strcmp(map->dev_name, dbg->dev_name))
619 if (strcmp(map->name, dbg->state_name))
622 for (j = 0; j < map->data.configs.num_configs; j++) {
623 if (!strcmp(map->data.configs.group_or_pin,
626 * We found the right pin / state, read the
627 * config and he pctldev for later use
629 configs = &map->data.configs;
630 pctldev = get_pinctrl_dev_from_devname
631 (map->ctrl_dev_name);
639 seq_printf(s, "No config found for dev/state/pin, expected:\n");
640 seq_printf(s, "Searched dev:%s\n", dbg->dev_name);
641 seq_printf(s, "Searched state:%s\n", dbg->state_name);
642 seq_printf(s, "Searched pin:%s\n", dbg->pin_name);
643 seq_printf(s, "Use: modify config_pin <devname> "\
644 "<state> <pinname> <value>\n");
648 config = *(configs->configs);
649 seq_printf(s, "Dev %s has config of %s in state %s: 0x%08lX\n",
650 dbg->dev_name, dbg->pin_name,
651 dbg->state_name, config);
654 confops = pctldev->desc->confops;
656 if (confops && confops->pin_config_config_dbg_show)
657 confops->pin_config_config_dbg_show(pctldev, s, config);
660 mutex_unlock(&pinctrl_mutex);
666 * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
667 * map, of a dev/pin/state entry based on user entries to pinconf-config
668 * @user_buf: contains the modification request with expected format:
669 * modify config_pin <devicename> <state> <pinname> <newvalue>
670 * modify is literal string, alternatives like add/delete not supported yet
671 * config_pin is literal, alternatives like config_mux not supported yet
672 * <devicename> <state> <pinname> are values that should match the pinctrl-maps
673 * <newvalue> reflects the new config and is driver dependant
675 static int pinconf_dbg_config_write(struct file *file,
676 const char __user *user_buf, size_t count, loff_t *ppos)
678 struct pinctrl_maps *maps_node;
679 const struct pinctrl_map *map;
680 struct pinctrl_dev *pctldev = NULL;
681 const struct pinconf_ops *confops = NULL;
682 struct dbg_cfg *dbg = &pinconf_dbg_conf;
683 const struct pinctrl_map_configs *configs;
684 char config[MAX_NAME_LEN+1];
692 /* Get userspace string and assure termination */
693 buf_size = min(count, (sizeof(buf)-1));
694 if (copy_from_user(buf, user_buf, buf_size))
699 * need to parse entry and extract parameters:
700 * modify configs_pin devicename state pinname newvalue
703 /* Get arg: 'modify' */
704 token = strsep(&b, " ");
707 if (strcmp(token, "modify"))
710 /* Get arg type: "config_pin" type supported so far */
711 token = strsep(&b, " ");
714 if (strcmp(token, "config_pin"))
716 dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN;
718 /* get arg 'device_name' */
719 token = strsep(&b, " ");
722 if (strlen(token) >= MAX_NAME_LEN)
724 strncpy(dbg->dev_name, token, MAX_NAME_LEN);
726 /* get arg 'state_name' */
727 token = strsep(&b, " ");
730 if (strlen(token) >= MAX_NAME_LEN)
732 strncpy(dbg->state_name, token, MAX_NAME_LEN);
734 /* get arg 'pin_name' */
735 token = strsep(&b, " ");
738 if (strlen(token) >= MAX_NAME_LEN)
740 strncpy(dbg->pin_name, token, MAX_NAME_LEN);
742 /* get new_value of config' */
743 token = strsep(&b, " ");
746 if (strlen(token) >= MAX_NAME_LEN)
748 strncpy(config, token, MAX_NAME_LEN);
750 mutex_lock(&pinctrl_mutex);
752 /* Parse the pinctrl map and look for the selected dev/state/pin */
753 for_each_maps(maps_node, i, map) {
754 if (strcmp(map->dev_name, dbg->dev_name))
756 if (map->type != dbg->map_type)
758 if (strcmp(map->name, dbg->state_name))
761 /* we found the right pin / state, so overwrite config */
762 if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
764 pctldev = get_pinctrl_dev_from_devname(
766 configs = &map->data.configs;
777 confops = pctldev->desc->confops;
779 if (confops && confops->pin_config_dbg_parse_modify) {
780 for (i = 0; i < configs->num_configs; i++) {
781 confops->pin_config_dbg_parse_modify(pctldev,
783 &configs->configs[i]);
788 mutex_unlock(&pinctrl_mutex);
793 static int pinconf_dbg_config_open(struct inode *inode, struct file *file)
795 return single_open(file, pinconf_dbg_config_print, inode->i_private);
798 static const struct file_operations pinconf_dbg_pinconfig_fops = {
799 .open = pinconf_dbg_config_open,
800 .write = pinconf_dbg_config_write,
803 .release = single_release,
804 .owner = THIS_MODULE,
807 void pinconf_init_device_debugfs(struct dentry *devroot,
808 struct pinctrl_dev *pctldev)
810 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
811 devroot, pctldev, &pinconf_pins_ops);
812 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
813 devroot, pctldev, &pinconf_groups_ops);
814 debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP),
815 devroot, pctldev, &pinconf_dbg_pinconfig_fops);