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