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