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 must be able to read out pin status */
31 if (!ops->pin_config_get && !ops->pin_config_group_get) {
33 "pinconf must be able to read out pin status\n");
36 /* We have to be able to config the pins in SOME way */
37 if (!ops->pin_config_set && !ops->pin_config_group_set) {
39 "pinconf has to be able to set a pins config\n");
45 int pinconf_validate_map(struct pinctrl_map const *map, int i)
47 if (!map->data.configs.group_or_pin) {
48 pr_err("failed to register map %s (%d): no group/pin given\n",
53 if (!map->data.configs.num_configs ||
54 !map->data.configs.configs) {
55 pr_err("failed to register map %s (%d): no configs given\n",
63 int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
64 unsigned long *config)
66 const struct pinconf_ops *ops = pctldev->desc->confops;
68 if (!ops || !ops->pin_config_get) {
69 dev_err(pctldev->dev, "cannot get pin configuration, missing "
70 "pin_config_get() function in driver\n");
74 return ops->pin_config_get(pctldev, pin, config);
78 * pin_config_get() - get the configuration of a single pin parameter
79 * @dev_name: name of the pin controller device for this pin
80 * @name: name of the pin to get the config for
81 * @config: the config pointed to by this argument will be filled in with the
82 * current pin state, it can be used directly by drivers as a numeral, or
83 * it can be dereferenced to any struct.
85 int pin_config_get(const char *dev_name, const char *name,
86 unsigned long *config)
88 struct pinctrl_dev *pctldev;
91 mutex_lock(&pinctrl_mutex);
93 pctldev = get_pinctrl_dev_from_devname(dev_name);
99 pin = pin_get_from_name(pctldev, name);
103 pin = pin_config_get_for_pin(pctldev, pin, config);
106 mutex_unlock(&pinctrl_mutex);
109 EXPORT_SYMBOL(pin_config_get);
111 static int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
112 unsigned long config)
114 const struct pinconf_ops *ops = pctldev->desc->confops;
117 if (!ops || !ops->pin_config_set) {
118 dev_err(pctldev->dev, "cannot configure pin, missing "
119 "config function in driver\n");
123 ret = ops->pin_config_set(pctldev, pin, config);
125 dev_err(pctldev->dev,
126 "unable to set pin configuration on pin %d\n", pin);
134 * pin_config_set() - set the configuration of a single pin parameter
135 * @dev_name: name of pin controller device for this pin
136 * @name: name of the pin to set the config for
137 * @config: the config in this argument will contain the desired pin state, it
138 * can be used directly by drivers as a numeral, or it can be dereferenced
141 int pin_config_set(const char *dev_name, const char *name,
142 unsigned long config)
144 struct pinctrl_dev *pctldev;
147 mutex_lock(&pinctrl_mutex);
149 pctldev = get_pinctrl_dev_from_devname(dev_name);
155 pin = pin_get_from_name(pctldev, name);
161 ret = pin_config_set_for_pin(pctldev, pin, config);
164 mutex_unlock(&pinctrl_mutex);
167 EXPORT_SYMBOL(pin_config_set);
169 int pin_config_group_get(const char *dev_name, const char *pin_group,
170 unsigned long *config)
172 struct pinctrl_dev *pctldev;
173 const struct pinconf_ops *ops;
176 mutex_lock(&pinctrl_mutex);
178 pctldev = get_pinctrl_dev_from_devname(dev_name);
183 ops = pctldev->desc->confops;
185 if (!ops || !ops->pin_config_group_get) {
186 dev_err(pctldev->dev, "cannot get configuration for pin "
187 "group, missing group config get function in "
193 selector = pinctrl_get_group_selector(pctldev, pin_group);
199 ret = ops->pin_config_group_get(pctldev, selector, config);
202 mutex_unlock(&pinctrl_mutex);
205 EXPORT_SYMBOL(pin_config_group_get);
207 int pin_config_group_set(const char *dev_name, const char *pin_group,
208 unsigned long config)
210 struct pinctrl_dev *pctldev;
211 const struct pinconf_ops *ops;
212 const struct pinctrl_ops *pctlops;
214 const unsigned *pins;
219 mutex_lock(&pinctrl_mutex);
221 pctldev = get_pinctrl_dev_from_devname(dev_name);
226 ops = pctldev->desc->confops;
227 pctlops = pctldev->desc->pctlops;
229 if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) {
230 dev_err(pctldev->dev, "cannot configure pin group, missing "
231 "config function in driver\n");
236 selector = pinctrl_get_group_selector(pctldev, pin_group);
242 ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins);
244 dev_err(pctldev->dev, "cannot configure pin group, error "
250 * If the pin controller supports handling entire groups we use that
253 if (ops->pin_config_group_set) {
254 ret = ops->pin_config_group_set(pctldev, selector, config);
256 * If the pin controller prefer that a certain group be handled
257 * pin-by-pin as well, it returns -EAGAIN.
264 * If the controller cannot handle entire groups, we configure each pin
267 if (!ops->pin_config_set) {
272 for (i = 0; i < num_pins; i++) {
273 ret = ops->pin_config_set(pctldev, pins[i], config);
281 mutex_unlock(&pinctrl_mutex);
285 EXPORT_SYMBOL(pin_config_group_set);
287 int pinconf_map_to_setting(struct pinctrl_map const *map,
288 struct pinctrl_setting *setting)
290 struct pinctrl_dev *pctldev = setting->pctldev;
293 switch (setting->type) {
294 case PIN_MAP_TYPE_CONFIGS_PIN:
295 pin = pin_get_from_name(pctldev,
296 map->data.configs.group_or_pin);
298 dev_err(pctldev->dev, "could not map pin config for \"%s\"",
299 map->data.configs.group_or_pin);
302 setting->data.configs.group_or_pin = pin;
304 case PIN_MAP_TYPE_CONFIGS_GROUP:
305 pin = pinctrl_get_group_selector(pctldev,
306 map->data.configs.group_or_pin);
308 dev_err(pctldev->dev, "could not map group config for \"%s\"",
309 map->data.configs.group_or_pin);
312 setting->data.configs.group_or_pin = pin;
318 setting->data.configs.num_configs = map->data.configs.num_configs;
319 setting->data.configs.configs = map->data.configs.configs;
324 void pinconf_free_setting(struct pinctrl_setting const *setting)
328 int pinconf_apply_setting(struct pinctrl_setting const *setting)
330 struct pinctrl_dev *pctldev = setting->pctldev;
331 const struct pinconf_ops *ops = pctldev->desc->confops;
335 dev_err(pctldev->dev, "missing confops\n");
339 switch (setting->type) {
340 case PIN_MAP_TYPE_CONFIGS_PIN:
341 if (!ops->pin_config_set) {
342 dev_err(pctldev->dev, "missing pin_config_set op\n");
345 for (i = 0; i < setting->data.configs.num_configs; i++) {
346 ret = ops->pin_config_set(pctldev,
347 setting->data.configs.group_or_pin,
348 setting->data.configs.configs[i]);
350 dev_err(pctldev->dev,
351 "pin_config_set op failed for pin %d config %08lx\n",
352 setting->data.configs.group_or_pin,
353 setting->data.configs.configs[i]);
358 case PIN_MAP_TYPE_CONFIGS_GROUP:
359 if (!ops->pin_config_group_set) {
360 dev_err(pctldev->dev,
361 "missing pin_config_group_set op\n");
364 for (i = 0; i < setting->data.configs.num_configs; i++) {
365 ret = ops->pin_config_group_set(pctldev,
366 setting->data.configs.group_or_pin,
367 setting->data.configs.configs[i]);
369 dev_err(pctldev->dev,
370 "pin_config_group_set op failed for group %d config %08lx\n",
371 setting->data.configs.group_or_pin,
372 setting->data.configs.configs[i]);
384 #ifdef CONFIG_DEBUG_FS
386 void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map)
388 struct pinctrl_dev *pctldev;
389 const struct pinconf_ops *confops;
392 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
394 confops = pctldev->desc->confops;
399 case PIN_MAP_TYPE_CONFIGS_PIN:
400 seq_printf(s, "pin ");
402 case PIN_MAP_TYPE_CONFIGS_GROUP:
403 seq_printf(s, "group ");
409 seq_printf(s, "%s\n", map->data.configs.group_or_pin);
411 for (i = 0; i < map->data.configs.num_configs; i++) {
412 seq_printf(s, "config ");
413 if (confops && confops->pin_config_config_dbg_show)
414 confops->pin_config_config_dbg_show(pctldev, s,
415 map->data.configs.configs[i]);
417 seq_printf(s, "%08lx", map->data.configs.configs[i]);
422 void pinconf_show_setting(struct seq_file *s,
423 struct pinctrl_setting const *setting)
425 struct pinctrl_dev *pctldev = setting->pctldev;
426 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
427 const struct pinconf_ops *confops = pctldev->desc->confops;
428 struct pin_desc *desc;
431 switch (setting->type) {
432 case PIN_MAP_TYPE_CONFIGS_PIN:
433 desc = pin_desc_get(setting->pctldev,
434 setting->data.configs.group_or_pin);
435 seq_printf(s, "pin %s (%d)",
436 desc->name ? desc->name : "unnamed",
437 setting->data.configs.group_or_pin);
439 case PIN_MAP_TYPE_CONFIGS_GROUP:
440 seq_printf(s, "group %s (%d)",
441 pctlops->get_group_name(pctldev,
442 setting->data.configs.group_or_pin),
443 setting->data.configs.group_or_pin);
450 * FIXME: We should really get the pin controler to dump the config
451 * values, so they can be decoded to something meaningful.
453 for (i = 0; i < setting->data.configs.num_configs; i++) {
455 if (confops && confops->pin_config_config_dbg_show)
456 confops->pin_config_config_dbg_show(pctldev, s,
457 setting->data.configs.configs[i]);
459 seq_printf(s, "%08lx",
460 setting->data.configs.configs[i]);
466 static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
467 struct seq_file *s, int pin)
469 const struct pinconf_ops *ops = pctldev->desc->confops;
471 /* no-op when not using generic pin config */
472 pinconf_generic_dump_pin(pctldev, s, pin);
473 if (ops && ops->pin_config_dbg_show)
474 ops->pin_config_dbg_show(pctldev, s, pin);
477 static int pinconf_pins_show(struct seq_file *s, void *what)
479 struct pinctrl_dev *pctldev = s->private;
480 const struct pinconf_ops *ops = pctldev->desc->confops;
483 if (!ops || !ops->pin_config_get)
486 seq_puts(s, "Pin config settings per pin\n");
487 seq_puts(s, "Format: pin (name): configs\n");
489 mutex_lock(&pinctrl_mutex);
491 /* The pin number can be retrived from the pin controller descriptor */
492 for (i = 0; i < pctldev->desc->npins; i++) {
493 struct pin_desc *desc;
495 pin = pctldev->desc->pins[i].number;
496 desc = pin_desc_get(pctldev, pin);
497 /* Skip if we cannot search the pin */
501 seq_printf(s, "pin %d (%s):", pin,
502 desc->name ? desc->name : "unnamed");
504 pinconf_dump_pin(pctldev, s, pin);
509 mutex_unlock(&pinctrl_mutex);
514 static void pinconf_dump_group(struct pinctrl_dev *pctldev,
515 struct seq_file *s, unsigned selector,
518 const struct pinconf_ops *ops = pctldev->desc->confops;
520 /* no-op when not using generic pin config */
521 pinconf_generic_dump_group(pctldev, s, gname);
522 if (ops && ops->pin_config_group_dbg_show)
523 ops->pin_config_group_dbg_show(pctldev, s, selector);
526 static int pinconf_groups_show(struct seq_file *s, void *what)
528 struct pinctrl_dev *pctldev = s->private;
529 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
530 const struct pinconf_ops *ops = pctldev->desc->confops;
531 unsigned ngroups = pctlops->get_groups_count(pctldev);
532 unsigned selector = 0;
534 if (!ops || !ops->pin_config_group_get)
537 seq_puts(s, "Pin config settings per pin group\n");
538 seq_puts(s, "Format: group (name): configs\n");
540 while (selector < ngroups) {
541 const char *gname = pctlops->get_group_name(pctldev, selector);
543 seq_printf(s, "%u (%s):", selector, gname);
544 pinconf_dump_group(pctldev, s, selector, gname);
553 static int pinconf_pins_open(struct inode *inode, struct file *file)
555 return single_open(file, pinconf_pins_show, inode->i_private);
558 static int pinconf_groups_open(struct inode *inode, struct file *file)
560 return single_open(file, pinconf_groups_show, inode->i_private);
563 static const struct file_operations pinconf_pins_ops = {
564 .open = pinconf_pins_open,
567 .release = single_release,
570 static const struct file_operations pinconf_groups_ops = {
571 .open = pinconf_groups_open,
574 .release = single_release,
577 /* 32bit read/write ressources */
578 #define MAX_NAME_LEN 16
579 char dbg_pinname[MAX_NAME_LEN]; /* shared: name of the state of the pin*/
580 char dbg_state_name[MAX_NAME_LEN]; /* shared: state of the pin*/
581 static u32 dbg_config; /* shared: config to be read/set for the pin & state*/
583 static int pinconf_dbg_pinname_print(struct seq_file *s, void *d)
585 if (strlen(dbg_pinname))
586 seq_printf(s, "%s\n", dbg_pinname);
588 seq_printf(s, "No pin name set\n");
592 static int pinconf_dbg_pinname_open(struct inode *inode, struct file *file)
594 return single_open(file, pinconf_dbg_pinname_print, inode->i_private);
597 static int pinconf_dbg_pinname_write(struct file *file,
598 const char __user *user_buf, size_t count, loff_t *ppos)
602 if (count > MAX_NAME_LEN)
605 err = sscanf(user_buf, "%15s", dbg_pinname);
613 static const struct file_operations pinconf_dbg_pinname_fops = {
614 .open = pinconf_dbg_pinname_open,
615 .write = pinconf_dbg_pinname_write,
618 .release = single_release,
619 .owner = THIS_MODULE,
622 static int pinconf_dbg_state_print(struct seq_file *s, void *d)
624 if (strlen(dbg_state_name))
625 seq_printf(s, "%s\n", dbg_state_name);
627 seq_printf(s, "No pin state set\n");
631 static int pinconf_dbg_state_open(struct inode *inode, struct file *file)
633 return single_open(file, pinconf_dbg_state_print, inode->i_private);
636 static int pinconf_dbg_state_write(struct file *file,
637 const char __user *user_buf, size_t count, loff_t *ppos)
641 if (count > MAX_NAME_LEN)
644 err = sscanf(user_buf, "%15s", dbg_state_name);
652 static const struct file_operations pinconf_dbg_pinstate_fops = {
653 .open = pinconf_dbg_state_open,
654 .write = pinconf_dbg_state_write,
657 .release = single_release,
658 .owner = THIS_MODULE,
662 * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
663 * map, of a pin/state pair based on pinname and state that have been
664 * selected with the debugfs entries pinconf-name and pinconf-state
665 * @s: contains the 32bits config to be written
668 static int pinconf_dbg_config_print(struct seq_file *s, void *d)
670 struct pinctrl_maps *maps_node;
671 struct pinctrl_map const *map;
672 struct pinctrl_dev *pctldev = NULL;
673 struct pinconf_ops *confops = NULL;
677 mutex_lock(&pinctrl_mutex);
679 /* Parse the pinctrl map and look for the elected pin/state */
680 for_each_maps(maps_node, i, map) {
681 if (map->type != PIN_MAP_TYPE_CONFIGS_PIN)
684 if (strncmp(map->name, dbg_state_name, MAX_NAME_LEN) > 0)
687 for (j = 0; j < map->data.configs.num_configs; j++) {
688 if (0 == strncmp(map->data.configs.group_or_pin,
689 dbg_pinname, MAX_NAME_LEN)) {
690 /* We found the right pin / state, read the
691 * config and store the pctldev */
692 dbg_config = map->data.configs.configs[j];
693 pctldev = get_pinctrl_dev_from_devname
694 (map->ctrl_dev_name);
701 mutex_unlock(&pinctrl_mutex);
704 seq_printf(s, "Config of %s in state %s: 0x%08X\n", dbg_pinname,
705 dbg_state_name, dbg_config);
708 confops = pctldev->desc->confops;
710 if (confops && confops->pin_config_config_dbg_show)
711 confops->pin_config_config_dbg_show(pctldev,
714 seq_printf(s, "No pin found for defined name/state\n");
720 static int pinconf_dbg_config_open(struct inode *inode, struct file *file)
722 return single_open(file, pinconf_dbg_config_print, inode->i_private);
726 * pinconf_dbg_config_write() - overwrite the pinctrl config in thepinctrl
727 * map, of a pin/state pair based on pinname and state that have been
728 * selected with the debugfs entries pinconf-name and pinconf-state
730 static int pinconf_dbg_config_write(struct file *file,
731 const char __user *user_buf, size_t count, loff_t *ppos)
734 unsigned long config;
735 struct pinctrl_maps *maps_node;
736 struct pinctrl_map const *map;
739 err = kstrtoul_from_user(user_buf, count, 0, &config);
746 mutex_lock(&pinctrl_mutex);
748 /* Parse the pinctrl map and look for the selected pin/state */
749 for_each_maps(maps_node, i, map) {
750 if (map->type != PIN_MAP_TYPE_CONFIGS_PIN)
753 if (strncmp(map->name, dbg_state_name, MAX_NAME_LEN) > 0)
756 /* we found the right pin / state, so overwrite config */
757 for (j = 0; j < map->data.configs.num_configs; j++) {
758 if (strncmp(map->data.configs.group_or_pin, dbg_pinname,
760 map->data.configs.configs[j] = dbg_config;
764 mutex_unlock(&pinctrl_mutex);
769 static const struct file_operations pinconf_dbg_pinconfig_fops = {
770 .open = pinconf_dbg_config_open,
771 .write = pinconf_dbg_config_write,
774 .release = single_release,
775 .owner = THIS_MODULE,
778 void pinconf_init_device_debugfs(struct dentry *devroot,
779 struct pinctrl_dev *pctldev)
781 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
782 devroot, pctldev, &pinconf_pins_ops);
783 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
784 devroot, pctldev, &pinconf_groups_ops);
785 debugfs_create_file("pinconf-name", (S_IRUGO | S_IWUSR | S_IWGRP),
786 devroot, pctldev, &pinconf_dbg_pinname_fops);
787 debugfs_create_file("pinconf-state", (S_IRUGO | S_IWUSR | S_IWGRP),
788 devroot, pctldev, &pinconf_dbg_pinstate_fops);
789 debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP),
790 devroot, pctldev, &pinconf_dbg_pinconfig_fops);