Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Core driver for the pin config portions of the pin control subsystem |
| 3 | * |
| 4 | * Copyright (C) 2011 ST-Ericsson SA |
| 5 | * Written on behalf of Linaro for ST-Ericsson |
| 6 | * |
| 7 | * Author: Linus Walleij <linus.walleij@linaro.org> |
| 8 | * |
| 9 | * License terms: GNU General Public License (GPL) version 2 |
| 10 | */ |
| 11 | #define pr_fmt(fmt) "pinconfig core: " fmt |
| 12 | |
| 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> |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 21 | #include <linux/pinctrl/machine.h> |
| 22 | #include <linux/pinctrl/pinctrl.h> |
| 23 | #include <linux/pinctrl/pinconf.h> |
| 24 | #include "core.h" |
| 25 | #include "pinconf.h" |
| 26 | |
Stephen Warren | 2b69425 | 2012-02-19 23:45:46 -0700 | [diff] [blame] | 27 | int pinconf_check_ops(struct pinctrl_dev *pctldev) |
| 28 | { |
| 29 | const struct pinconf_ops *ops = pctldev->desc->confops; |
| 30 | |
Stephen Warren | 2b69425 | 2012-02-19 23:45:46 -0700 | [diff] [blame] | 31 | /* We have to be able to config the pins in SOME way */ |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 32 | if (!ops->pin_config_set && !ops->pin_config_group_set) { |
| 33 | dev_err(pctldev->dev, |
| 34 | "pinconf has to be able to set a pins config\n"); |
Stephen Warren | 2b69425 | 2012-02-19 23:45:46 -0700 | [diff] [blame] | 35 | return -EINVAL; |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 36 | } |
Stephen Warren | 2b69425 | 2012-02-19 23:45:46 -0700 | [diff] [blame] | 37 | return 0; |
| 38 | } |
| 39 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 40 | int pinconf_validate_map(struct pinctrl_map const *map, int i) |
| 41 | { |
| 42 | if (!map->data.configs.group_or_pin) { |
| 43 | pr_err("failed to register map %s (%d): no group/pin given\n", |
| 44 | map->name, i); |
| 45 | return -EINVAL; |
| 46 | } |
| 47 | |
Dong Aisheng | c95df2d | 2012-05-14 19:06:36 +0800 | [diff] [blame] | 48 | if (!map->data.configs.num_configs || |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 49 | !map->data.configs.configs) { |
Dong Aisheng | c95df2d | 2012-05-14 19:06:36 +0800 | [diff] [blame] | 50 | pr_err("failed to register map %s (%d): no configs given\n", |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 51 | map->name, i); |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
Linus Walleij | 394349f | 2011-11-24 18:27:15 +0100 | [diff] [blame] | 58 | int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin, |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 59 | unsigned long *config) |
| 60 | { |
| 61 | const struct pinconf_ops *ops = pctldev->desc->confops; |
| 62 | |
| 63 | if (!ops || !ops->pin_config_get) { |
Masahiro Yamada | ca67f10 | 2015-07-30 17:27:44 +0900 | [diff] [blame] | 64 | dev_dbg(pctldev->dev, |
| 65 | "cannot get pin configuration, .pin_config_get missing in driver\n"); |
Alexandre Belloni | c420619 | 2013-12-09 11:38:29 +0100 | [diff] [blame] | 66 | return -ENOTSUPP; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return ops->pin_config_get(pctldev, pin, config); |
| 70 | } |
| 71 | |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame] | 72 | int pin_config_group_get(const char *dev_name, const char *pin_group, |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 73 | unsigned long *config) |
| 74 | { |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame] | 75 | struct pinctrl_dev *pctldev; |
| 76 | const struct pinconf_ops *ops; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 77 | int selector, ret; |
| 78 | |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 79 | pctldev = get_pinctrl_dev_from_devname(dev_name); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 80 | if (!pctldev) { |
| 81 | ret = -EINVAL; |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 82 | return ret; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 83 | } |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 84 | |
| 85 | mutex_lock(&pctldev->mutex); |
| 86 | |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame] | 87 | ops = pctldev->desc->confops; |
| 88 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 89 | if (!ops || !ops->pin_config_group_get) { |
Markus Elfring | e4d0305 | 2017-05-02 10:22:47 +0200 | [diff] [blame] | 90 | dev_dbg(pctldev->dev, |
| 91 | "cannot get configuration for pin group, missing group config get function in driver\n"); |
Alexandre Belloni | c420619 | 2013-12-09 11:38:29 +0100 | [diff] [blame] | 92 | ret = -ENOTSUPP; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 93 | goto unlock; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | selector = pinctrl_get_group_selector(pctldev, pin_group); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 97 | if (selector < 0) { |
| 98 | ret = selector; |
| 99 | goto unlock; |
| 100 | } |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 101 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 102 | ret = ops->pin_config_group_get(pctldev, selector, config); |
| 103 | |
| 104 | unlock: |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 105 | mutex_unlock(&pctldev->mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 106 | return ret; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 107 | } |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 108 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 109 | int pinconf_map_to_setting(struct pinctrl_map const *map, |
| 110 | struct pinctrl_setting *setting) |
| 111 | { |
| 112 | struct pinctrl_dev *pctldev = setting->pctldev; |
Linus Walleij | 70b3637 | 2012-03-12 21:38:29 +0100 | [diff] [blame] | 113 | int pin; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 114 | |
| 115 | switch (setting->type) { |
| 116 | case PIN_MAP_TYPE_CONFIGS_PIN: |
Linus Walleij | 70b3637 | 2012-03-12 21:38:29 +0100 | [diff] [blame] | 117 | pin = pin_get_from_name(pctldev, |
| 118 | map->data.configs.group_or_pin); |
| 119 | if (pin < 0) { |
| 120 | dev_err(pctldev->dev, "could not map pin config for \"%s\"", |
| 121 | map->data.configs.group_or_pin); |
| 122 | return pin; |
| 123 | } |
| 124 | setting->data.configs.group_or_pin = pin; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 125 | break; |
| 126 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
Linus Walleij | 70b3637 | 2012-03-12 21:38:29 +0100 | [diff] [blame] | 127 | pin = pinctrl_get_group_selector(pctldev, |
| 128 | map->data.configs.group_or_pin); |
| 129 | if (pin < 0) { |
| 130 | dev_err(pctldev->dev, "could not map group config for \"%s\"", |
| 131 | map->data.configs.group_or_pin); |
| 132 | return pin; |
| 133 | } |
| 134 | setting->data.configs.group_or_pin = pin; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 135 | break; |
| 136 | default: |
| 137 | return -EINVAL; |
| 138 | } |
| 139 | |
| 140 | setting->data.configs.num_configs = map->data.configs.num_configs; |
| 141 | setting->data.configs.configs = map->data.configs.configs; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | void pinconf_free_setting(struct pinctrl_setting const *setting) |
| 147 | { |
| 148 | } |
| 149 | |
| 150 | int pinconf_apply_setting(struct pinctrl_setting const *setting) |
| 151 | { |
| 152 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 153 | const struct pinconf_ops *ops = pctldev->desc->confops; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 154 | int ret; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 155 | |
| 156 | if (!ops) { |
| 157 | dev_err(pctldev->dev, "missing confops\n"); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | switch (setting->type) { |
| 162 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 163 | if (!ops->pin_config_set) { |
| 164 | dev_err(pctldev->dev, "missing pin_config_set op\n"); |
| 165 | return -EINVAL; |
| 166 | } |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 167 | ret = ops->pin_config_set(pctldev, |
| 168 | setting->data.configs.group_or_pin, |
| 169 | setting->data.configs.configs, |
| 170 | setting->data.configs.num_configs); |
| 171 | if (ret < 0) { |
| 172 | dev_err(pctldev->dev, |
| 173 | "pin_config_set op failed for pin %d\n", |
| 174 | setting->data.configs.group_or_pin); |
| 175 | return ret; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 176 | } |
| 177 | break; |
| 178 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 179 | if (!ops->pin_config_group_set) { |
| 180 | dev_err(pctldev->dev, |
| 181 | "missing pin_config_group_set op\n"); |
| 182 | return -EINVAL; |
| 183 | } |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 184 | ret = ops->pin_config_group_set(pctldev, |
| 185 | setting->data.configs.group_or_pin, |
| 186 | setting->data.configs.configs, |
| 187 | setting->data.configs.num_configs); |
| 188 | if (ret < 0) { |
| 189 | dev_err(pctldev->dev, |
| 190 | "pin_config_group_set op failed for group %d\n", |
| 191 | setting->data.configs.group_or_pin); |
| 192 | return ret; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 193 | } |
| 194 | break; |
| 195 | default: |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Mika Westerberg | 15381bc | 2017-01-23 15:34:33 +0300 | [diff] [blame] | 202 | int pinconf_set_config(struct pinctrl_dev *pctldev, unsigned pin, |
| 203 | unsigned long *configs, size_t nconfigs) |
| 204 | { |
| 205 | const struct pinconf_ops *ops; |
| 206 | |
| 207 | ops = pctldev->desc->confops; |
| 208 | if (!ops) |
| 209 | return -ENOTSUPP; |
| 210 | |
| 211 | return ops->pin_config_set(pctldev, pin, configs, nconfigs); |
| 212 | } |
| 213 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 214 | #ifdef CONFIG_DEBUG_FS |
| 215 | |
kbuild test robot | 6de52c1 | 2015-07-17 21:37:09 +0800 | [diff] [blame] | 216 | static void pinconf_show_config(struct seq_file *s, struct pinctrl_dev *pctldev, |
Jon Hunter | d96310a | 2015-07-14 11:17:59 +0100 | [diff] [blame] | 217 | unsigned long *configs, unsigned num_configs) |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 218 | { |
Stephen Warren | 6cb4158 | 2012-04-13 10:49:06 -0600 | [diff] [blame] | 219 | const struct pinconf_ops *confops; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 220 | int i; |
| 221 | |
Stephen Warren | 6cb4158 | 2012-04-13 10:49:06 -0600 | [diff] [blame] | 222 | if (pctldev) |
| 223 | confops = pctldev->desc->confops; |
| 224 | else |
| 225 | confops = NULL; |
| 226 | |
Jon Hunter | d96310a | 2015-07-14 11:17:59 +0100 | [diff] [blame] | 227 | for (i = 0; i < num_configs; i++) { |
| 228 | seq_puts(s, "config "); |
| 229 | if (confops && confops->pin_config_config_dbg_show) |
| 230 | confops->pin_config_config_dbg_show(pctldev, s, |
| 231 | configs[i]); |
| 232 | else |
| 233 | seq_printf(s, "%08lx", configs[i]); |
Markus Elfring | 47352a6 | 2017-05-01 22:24:29 +0200 | [diff] [blame] | 234 | seq_putc(s, '\n'); |
Jon Hunter | d96310a | 2015-07-14 11:17:59 +0100 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | void pinconf_show_map(struct seq_file *s, struct pinctrl_map const *map) |
| 239 | { |
| 240 | struct pinctrl_dev *pctldev; |
| 241 | |
| 242 | pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name); |
| 243 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 244 | switch (map->type) { |
| 245 | case PIN_MAP_TYPE_CONFIGS_PIN: |
Markus Elfring | de2eae2 | 2017-05-02 09:52:50 +0200 | [diff] [blame] | 246 | seq_puts(s, "pin "); |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 247 | break; |
| 248 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
Markus Elfring | de2eae2 | 2017-05-02 09:52:50 +0200 | [diff] [blame] | 249 | seq_puts(s, "group "); |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 250 | break; |
| 251 | default: |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | seq_printf(s, "%s\n", map->data.configs.group_or_pin); |
| 256 | |
Jon Hunter | d96310a | 2015-07-14 11:17:59 +0100 | [diff] [blame] | 257 | pinconf_show_config(s, pctldev, map->data.configs.configs, |
| 258 | map->data.configs.num_configs); |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void pinconf_show_setting(struct seq_file *s, |
| 262 | struct pinctrl_setting const *setting) |
| 263 | { |
| 264 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 265 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 266 | struct pin_desc *desc; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 267 | |
| 268 | switch (setting->type) { |
| 269 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 270 | desc = pin_desc_get(setting->pctldev, |
| 271 | setting->data.configs.group_or_pin); |
Masahiro Yamada | cf9d994 | 2016-05-24 14:26:26 +0900 | [diff] [blame] | 272 | seq_printf(s, "pin %s (%d)", desc->name, |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 273 | setting->data.configs.group_or_pin); |
| 274 | break; |
| 275 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 276 | seq_printf(s, "group %s (%d)", |
| 277 | pctlops->get_group_name(pctldev, |
| 278 | setting->data.configs.group_or_pin), |
| 279 | setting->data.configs.group_or_pin); |
| 280 | break; |
| 281 | default: |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | /* |
Andy Shevchenko | 3ec440e | 2017-02-28 16:59:56 +0200 | [diff] [blame] | 286 | * FIXME: We should really get the pin controller to dump the config |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 287 | * values, so they can be decoded to something meaningful. |
| 288 | */ |
Jon Hunter | d96310a | 2015-07-14 11:17:59 +0100 | [diff] [blame] | 289 | pinconf_show_config(s, pctldev, setting->data.configs.configs, |
| 290 | setting->data.configs.num_configs); |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 293 | static void pinconf_dump_pin(struct pinctrl_dev *pctldev, |
| 294 | struct seq_file *s, int pin) |
| 295 | { |
| 296 | const struct pinconf_ops *ops = pctldev->desc->confops; |
| 297 | |
Linus Walleij | 394349f | 2011-11-24 18:27:15 +0100 | [diff] [blame] | 298 | /* no-op when not using generic pin config */ |
Soren Brinkmann | dd4d01f | 2015-01-09 07:43:46 -0800 | [diff] [blame] | 299 | pinconf_generic_dump_pins(pctldev, s, NULL, pin); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 300 | if (ops && ops->pin_config_dbg_show) |
| 301 | ops->pin_config_dbg_show(pctldev, s, pin); |
| 302 | } |
| 303 | |
| 304 | static int pinconf_pins_show(struct seq_file *s, void *what) |
| 305 | { |
| 306 | struct pinctrl_dev *pctldev = s->private; |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 307 | unsigned i, pin; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 308 | |
| 309 | seq_puts(s, "Pin config settings per pin\n"); |
Dong Aisheng | 2aeefe0 | 2012-04-16 22:07:24 +0800 | [diff] [blame] | 310 | seq_puts(s, "Format: pin (name): configs\n"); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 311 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 312 | mutex_lock(&pctldev->mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 313 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 314 | /* The pin number can be retrived from the pin controller descriptor */ |
Stephen Warren | 546edd8 | 2012-01-06 13:38:31 -0700 | [diff] [blame] | 315 | for (i = 0; i < pctldev->desc->npins; i++) { |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 316 | struct pin_desc *desc; |
| 317 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 318 | pin = pctldev->desc->pins[i].number; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 319 | desc = pin_desc_get(pctldev, pin); |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 320 | /* Skip if we cannot search the pin */ |
Markus Elfring | 76ce37f | 2017-05-02 10:01:57 +0200 | [diff] [blame] | 321 | if (!desc) |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 322 | continue; |
| 323 | |
Masahiro Yamada | a672eb5 | 2016-05-25 15:37:27 +0900 | [diff] [blame] | 324 | seq_printf(s, "pin %d (%s): ", pin, desc->name); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 325 | |
| 326 | pinconf_dump_pin(pctldev, s, pin); |
Markus Elfring | 47352a6 | 2017-05-01 22:24:29 +0200 | [diff] [blame] | 327 | seq_putc(s, '\n'); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 330 | mutex_unlock(&pctldev->mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 331 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static void pinconf_dump_group(struct pinctrl_dev *pctldev, |
| 336 | struct seq_file *s, unsigned selector, |
| 337 | const char *gname) |
| 338 | { |
| 339 | const struct pinconf_ops *ops = pctldev->desc->confops; |
| 340 | |
Linus Walleij | 394349f | 2011-11-24 18:27:15 +0100 | [diff] [blame] | 341 | /* no-op when not using generic pin config */ |
Soren Brinkmann | dd4d01f | 2015-01-09 07:43:46 -0800 | [diff] [blame] | 342 | pinconf_generic_dump_pins(pctldev, s, gname, 0); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 343 | if (ops && ops->pin_config_group_dbg_show) |
| 344 | ops->pin_config_group_dbg_show(pctldev, s, selector); |
| 345 | } |
| 346 | |
| 347 | static int pinconf_groups_show(struct seq_file *s, void *what) |
| 348 | { |
| 349 | struct pinctrl_dev *pctldev = s->private; |
| 350 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 351 | unsigned ngroups = pctlops->get_groups_count(pctldev); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 352 | unsigned selector = 0; |
| 353 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 354 | seq_puts(s, "Pin config settings per pin group\n"); |
Dong Aisheng | 2aeefe0 | 2012-04-16 22:07:24 +0800 | [diff] [blame] | 355 | seq_puts(s, "Format: group (name): configs\n"); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 356 | |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 357 | while (selector < ngroups) { |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 358 | const char *gname = pctlops->get_group_name(pctldev, selector); |
| 359 | |
Masahiro Yamada | a672eb5 | 2016-05-25 15:37:27 +0900 | [diff] [blame] | 360 | seq_printf(s, "%u (%s): ", selector, gname); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 361 | pinconf_dump_group(pctldev, s, selector, gname); |
Markus Elfring | 47352a6 | 2017-05-01 22:24:29 +0200 | [diff] [blame] | 362 | seq_putc(s, '\n'); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 363 | selector++; |
| 364 | } |
| 365 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static int pinconf_pins_open(struct inode *inode, struct file *file) |
| 370 | { |
| 371 | return single_open(file, pinconf_pins_show, inode->i_private); |
| 372 | } |
| 373 | |
| 374 | static int pinconf_groups_open(struct inode *inode, struct file *file) |
| 375 | { |
| 376 | return single_open(file, pinconf_groups_show, inode->i_private); |
| 377 | } |
| 378 | |
| 379 | static const struct file_operations pinconf_pins_ops = { |
| 380 | .open = pinconf_pins_open, |
| 381 | .read = seq_read, |
| 382 | .llseek = seq_lseek, |
| 383 | .release = single_release, |
| 384 | }; |
| 385 | |
| 386 | static const struct file_operations pinconf_groups_ops = { |
| 387 | .open = pinconf_groups_open, |
| 388 | .read = seq_read, |
| 389 | .llseek = seq_lseek, |
| 390 | .release = single_release, |
| 391 | }; |
| 392 | |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 393 | #define MAX_NAME_LEN 15 |
| 394 | |
| 395 | struct dbg_cfg { |
| 396 | enum pinctrl_map_type map_type; |
Markus Elfring | e8c5d75 | 2017-05-02 10:32:19 +0200 | [diff] [blame^] | 397 | char dev_name[MAX_NAME_LEN + 1]; |
| 398 | char state_name[MAX_NAME_LEN + 1]; |
| 399 | char pin_name[MAX_NAME_LEN + 1]; |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | /* |
| 403 | * Goal is to keep this structure as global in order to simply read the |
| 404 | * pinconf-config file after a write to check config is as expected |
| 405 | */ |
| 406 | static struct dbg_cfg pinconf_dbg_conf; |
| 407 | |
| 408 | /** |
| 409 | * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl |
| 410 | * map, of the dev/pin/state that was last written to pinconf-config file. |
| 411 | * @s: string filled in with config description |
| 412 | * @d: not used |
| 413 | */ |
| 414 | static int pinconf_dbg_config_print(struct seq_file *s, void *d) |
| 415 | { |
| 416 | struct pinctrl_maps *maps_node; |
| 417 | const struct pinctrl_map *map; |
Russell King - ARM Linux | 8a9dcc3 | 2013-07-28 13:13:00 +0100 | [diff] [blame] | 418 | const struct pinctrl_map *found = NULL; |
| 419 | struct pinctrl_dev *pctldev; |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 420 | struct dbg_cfg *dbg = &pinconf_dbg_conf; |
Laurent Meunier | d99c805 | 2015-10-30 15:15:51 +0100 | [diff] [blame] | 421 | int i; |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 422 | |
Linus Walleij | a386267 | 2013-05-27 15:53:32 +0200 | [diff] [blame] | 423 | mutex_lock(&pinctrl_maps_mutex); |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 424 | |
| 425 | /* Parse the pinctrl map and look for the elected pin/state */ |
| 426 | for_each_maps(maps_node, i, map) { |
| 427 | if (map->type != dbg->map_type) |
| 428 | continue; |
| 429 | if (strcmp(map->dev_name, dbg->dev_name)) |
| 430 | continue; |
| 431 | if (strcmp(map->name, dbg->state_name)) |
| 432 | continue; |
| 433 | |
Laurent Meunier | d99c805 | 2015-10-30 15:15:51 +0100 | [diff] [blame] | 434 | if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) { |
| 435 | /* We found the right pin */ |
| 436 | found = map; |
| 437 | break; |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | if (!found) { |
| 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"); |
| 448 | goto exit; |
| 449 | } |
| 450 | |
Russell King - ARM Linux | 8a9dcc3 | 2013-07-28 13:13:00 +0100 | [diff] [blame] | 451 | pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name); |
Jon Hunter | d96310a | 2015-07-14 11:17:59 +0100 | [diff] [blame] | 452 | seq_printf(s, "Dev %s has config of %s in state %s:\n", |
| 453 | dbg->dev_name, dbg->pin_name, dbg->state_name); |
| 454 | pinconf_show_config(s, pctldev, found->data.configs.configs, |
| 455 | found->data.configs.num_configs); |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 456 | |
| 457 | exit: |
Linus Walleij | a386267 | 2013-05-27 15:53:32 +0200 | [diff] [blame] | 458 | mutex_unlock(&pinctrl_maps_mutex); |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl |
| 465 | * map, of a dev/pin/state entry based on user entries to pinconf-config |
| 466 | * @user_buf: contains the modification request with expected format: |
Jon Hunter | 7562998 | 2015-07-14 11:17:58 +0100 | [diff] [blame] | 467 | * modify <config> <devicename> <state> <name> <newvalue> |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 468 | * modify is literal string, alternatives like add/delete not supported yet |
Jon Hunter | 7562998 | 2015-07-14 11:17:58 +0100 | [diff] [blame] | 469 | * <config> is the configuration to be changed. Supported configs are |
| 470 | * "config_pin" or "config_group", alternatives like config_mux are not |
| 471 | * supported yet. |
| 472 | * <devicename> <state> <name> are values that should match the pinctrl-maps |
Andy Shevchenko | 3ec440e | 2017-02-28 16:59:56 +0200 | [diff] [blame] | 473 | * <newvalue> reflects the new config and is driver dependent |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 474 | */ |
Vincent Stehlé | 3b59e43 | 2013-09-16 22:50:32 +0200 | [diff] [blame] | 475 | static ssize_t pinconf_dbg_config_write(struct file *file, |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 476 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 477 | { |
| 478 | struct pinctrl_maps *maps_node; |
| 479 | const struct pinctrl_map *map; |
Russell King - ARM Linux | 8a9dcc3 | 2013-07-28 13:13:00 +0100 | [diff] [blame] | 480 | const struct pinctrl_map *found = NULL; |
| 481 | struct pinctrl_dev *pctldev; |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 482 | const struct pinconf_ops *confops = NULL; |
| 483 | struct dbg_cfg *dbg = &pinconf_dbg_conf; |
| 484 | const struct pinctrl_map_configs *configs; |
Markus Elfring | e8c5d75 | 2017-05-02 10:32:19 +0200 | [diff] [blame^] | 485 | char config[MAX_NAME_LEN + 1]; |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 486 | char buf[128]; |
| 487 | char *b = &buf[0]; |
| 488 | int buf_size; |
| 489 | char *token; |
| 490 | int i; |
| 491 | |
| 492 | /* Get userspace string and assure termination */ |
Dan Carpenter | 81d36c4 | 2013-09-17 13:47:54 +0300 | [diff] [blame] | 493 | buf_size = min(count, sizeof(buf) - 1); |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 494 | if (copy_from_user(buf, user_buf, buf_size)) |
| 495 | return -EFAULT; |
| 496 | buf[buf_size] = 0; |
| 497 | |
| 498 | /* |
| 499 | * need to parse entry and extract parameters: |
| 500 | * modify configs_pin devicename state pinname newvalue |
| 501 | */ |
| 502 | |
| 503 | /* Get arg: 'modify' */ |
| 504 | token = strsep(&b, " "); |
| 505 | if (!token) |
| 506 | return -EINVAL; |
| 507 | if (strcmp(token, "modify")) |
| 508 | return -EINVAL; |
| 509 | |
Jon Hunter | 7562998 | 2015-07-14 11:17:58 +0100 | [diff] [blame] | 510 | /* |
| 511 | * Get arg type: "config_pin" and "config_group" |
| 512 | * types are supported so far |
| 513 | */ |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 514 | token = strsep(&b, " "); |
| 515 | if (!token) |
| 516 | return -EINVAL; |
Jon Hunter | 7562998 | 2015-07-14 11:17:58 +0100 | [diff] [blame] | 517 | if (!strcmp(token, "config_pin")) |
| 518 | dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN; |
| 519 | else if (!strcmp(token, "config_group")) |
| 520 | dbg->map_type = PIN_MAP_TYPE_CONFIGS_GROUP; |
| 521 | else |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 522 | return -EINVAL; |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 523 | |
| 524 | /* get arg 'device_name' */ |
| 525 | token = strsep(&b, " "); |
Markus Elfring | 76ce37f | 2017-05-02 10:01:57 +0200 | [diff] [blame] | 526 | if (!token) |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 527 | return -EINVAL; |
| 528 | if (strlen(token) >= MAX_NAME_LEN) |
| 529 | return -EINVAL; |
| 530 | strncpy(dbg->dev_name, token, MAX_NAME_LEN); |
| 531 | |
| 532 | /* get arg 'state_name' */ |
| 533 | token = strsep(&b, " "); |
Markus Elfring | 76ce37f | 2017-05-02 10:01:57 +0200 | [diff] [blame] | 534 | if (!token) |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 535 | return -EINVAL; |
| 536 | if (strlen(token) >= MAX_NAME_LEN) |
| 537 | return -EINVAL; |
| 538 | strncpy(dbg->state_name, token, MAX_NAME_LEN); |
| 539 | |
| 540 | /* get arg 'pin_name' */ |
| 541 | token = strsep(&b, " "); |
Markus Elfring | 76ce37f | 2017-05-02 10:01:57 +0200 | [diff] [blame] | 542 | if (!token) |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 543 | return -EINVAL; |
| 544 | if (strlen(token) >= MAX_NAME_LEN) |
| 545 | return -EINVAL; |
| 546 | strncpy(dbg->pin_name, token, MAX_NAME_LEN); |
| 547 | |
| 548 | /* get new_value of config' */ |
| 549 | token = strsep(&b, " "); |
Markus Elfring | 76ce37f | 2017-05-02 10:01:57 +0200 | [diff] [blame] | 550 | if (!token) |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 551 | return -EINVAL; |
| 552 | if (strlen(token) >= MAX_NAME_LEN) |
| 553 | return -EINVAL; |
| 554 | strncpy(config, token, MAX_NAME_LEN); |
| 555 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 556 | mutex_lock(&pinctrl_maps_mutex); |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 557 | |
| 558 | /* Parse the pinctrl map and look for the selected dev/state/pin */ |
| 559 | for_each_maps(maps_node, i, map) { |
| 560 | if (strcmp(map->dev_name, dbg->dev_name)) |
| 561 | continue; |
| 562 | if (map->type != dbg->map_type) |
| 563 | continue; |
| 564 | if (strcmp(map->name, dbg->state_name)) |
| 565 | continue; |
| 566 | |
| 567 | /* we found the right pin / state, so overwrite config */ |
| 568 | if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) { |
Russell King - ARM Linux | 8a9dcc3 | 2013-07-28 13:13:00 +0100 | [diff] [blame] | 569 | found = map; |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 570 | break; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | if (!found) { |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 575 | count = -EINVAL; |
Laurent Meunier | cb6d315 | 2013-04-24 13:05:50 +0200 | [diff] [blame] | 576 | goto exit; |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 577 | } |
| 578 | |
Russell King - ARM Linux | 8a9dcc3 | 2013-07-28 13:13:00 +0100 | [diff] [blame] | 579 | pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name); |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 580 | if (pctldev) |
| 581 | confops = pctldev->desc->confops; |
| 582 | |
| 583 | if (confops && confops->pin_config_dbg_parse_modify) { |
Russell King - ARM Linux | 8a9dcc3 | 2013-07-28 13:13:00 +0100 | [diff] [blame] | 584 | configs = &found->data.configs; |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 585 | for (i = 0; i < configs->num_configs; i++) { |
| 586 | confops->pin_config_dbg_parse_modify(pctldev, |
| 587 | config, |
| 588 | &configs->configs[i]); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | exit: |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 593 | mutex_unlock(&pinctrl_maps_mutex); |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 594 | |
| 595 | return count; |
| 596 | } |
| 597 | |
| 598 | static int pinconf_dbg_config_open(struct inode *inode, struct file *file) |
| 599 | { |
| 600 | return single_open(file, pinconf_dbg_config_print, inode->i_private); |
| 601 | } |
| 602 | |
| 603 | static const struct file_operations pinconf_dbg_pinconfig_fops = { |
| 604 | .open = pinconf_dbg_config_open, |
| 605 | .write = pinconf_dbg_config_write, |
| 606 | .read = seq_read, |
| 607 | .llseek = seq_lseek, |
| 608 | .release = single_release, |
| 609 | .owner = THIS_MODULE, |
| 610 | }; |
| 611 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 612 | void pinconf_init_device_debugfs(struct dentry *devroot, |
| 613 | struct pinctrl_dev *pctldev) |
| 614 | { |
| 615 | debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO, |
| 616 | devroot, pctldev, &pinconf_pins_ops); |
| 617 | debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO, |
| 618 | devroot, pctldev, &pinconf_groups_ops); |
Laurent Meunier | f07512e | 2013-04-18 10:48:07 +0200 | [diff] [blame] | 619 | debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP), |
| 620 | devroot, pctldev, &pinconf_dbg_pinconfig_fops); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | #endif |