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> |
| 20 | #include <linux/pinctrl/machine.h> |
| 21 | #include <linux/pinctrl/pinctrl.h> |
| 22 | #include <linux/pinctrl/pinconf.h> |
| 23 | #include "core.h" |
| 24 | #include "pinconf.h" |
| 25 | |
| 26 | int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin, |
| 27 | unsigned long *config) |
| 28 | { |
| 29 | const struct pinconf_ops *ops = pctldev->desc->confops; |
| 30 | |
| 31 | if (!ops || !ops->pin_config_get) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 32 | dev_err(pctldev->dev, "cannot get pin configuration, missing " |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 33 | "pin_config_get() function in driver\n"); |
| 34 | return -EINVAL; |
| 35 | } |
| 36 | |
| 37 | return ops->pin_config_get(pctldev, pin, config); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * pin_config_get() - get the configuration of a single pin parameter |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 42 | * @dev_name: name of the pin controller device for this pin |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 43 | * @name: name of the pin to get the config for |
| 44 | * @config: the config pointed to by this argument will be filled in with the |
| 45 | * current pin state, it can be used directly by drivers as a numeral, or |
| 46 | * it can be dereferenced to any struct. |
| 47 | */ |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 48 | int pin_config_get(const char *dev_name, const char *name, |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 49 | unsigned long *config) |
| 50 | { |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 51 | struct pinctrl_dev *pctldev; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 52 | int pin; |
| 53 | |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 54 | pctldev = get_pinctrl_dev_from_dev(NULL, dev_name); |
| 55 | if (!pctldev) |
| 56 | return -EINVAL; |
| 57 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 58 | pin = pin_get_from_name(pctldev, name); |
| 59 | if (pin < 0) |
| 60 | return pin; |
| 61 | |
| 62 | return pin_config_get_for_pin(pctldev, pin, config); |
| 63 | } |
| 64 | EXPORT_SYMBOL(pin_config_get); |
| 65 | |
| 66 | int pin_config_set_for_pin(struct pinctrl_dev *pctldev, unsigned pin, |
| 67 | unsigned long config) |
| 68 | { |
| 69 | const struct pinconf_ops *ops = pctldev->desc->confops; |
| 70 | int ret; |
| 71 | |
| 72 | if (!ops || !ops->pin_config_set) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 73 | dev_err(pctldev->dev, "cannot configure pin, missing " |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 74 | "config function in driver\n"); |
| 75 | return -EINVAL; |
| 76 | } |
| 77 | |
| 78 | ret = ops->pin_config_set(pctldev, pin, config); |
| 79 | if (ret) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 80 | dev_err(pctldev->dev, |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 81 | "unable to set pin configuration on pin %d\n", pin); |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * pin_config_set() - set the configuration of a single pin parameter |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 90 | * @dev_name: name of pin controller device for this pin |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 91 | * @name: name of the pin to set the config for |
| 92 | * @config: the config in this argument will contain the desired pin state, it |
| 93 | * can be used directly by drivers as a numeral, or it can be dereferenced |
| 94 | * to any struct. |
| 95 | */ |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 96 | int pin_config_set(const char *dev_name, const char *name, |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 97 | unsigned long config) |
| 98 | { |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 99 | struct pinctrl_dev *pctldev; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 100 | int pin; |
| 101 | |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 102 | pctldev = get_pinctrl_dev_from_dev(NULL, dev_name); |
| 103 | if (!pctldev) |
| 104 | return -EINVAL; |
| 105 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 106 | pin = pin_get_from_name(pctldev, name); |
| 107 | if (pin < 0) |
| 108 | return pin; |
| 109 | |
| 110 | return pin_config_set_for_pin(pctldev, pin, config); |
| 111 | } |
| 112 | EXPORT_SYMBOL(pin_config_set); |
| 113 | |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 114 | 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] | 115 | unsigned long *config) |
| 116 | { |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 117 | struct pinctrl_dev *pctldev; |
| 118 | const struct pinconf_ops *ops; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 119 | int selector; |
| 120 | |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 121 | pctldev = get_pinctrl_dev_from_dev(NULL, dev_name); |
| 122 | if (!pctldev) |
| 123 | return -EINVAL; |
| 124 | ops = pctldev->desc->confops; |
| 125 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 126 | if (!ops || !ops->pin_config_group_get) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 127 | dev_err(pctldev->dev, "cannot get configuration for pin " |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 128 | "group, missing group config get function in " |
| 129 | "driver\n"); |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | |
| 133 | selector = pinctrl_get_group_selector(pctldev, pin_group); |
| 134 | if (selector < 0) |
| 135 | return selector; |
| 136 | |
| 137 | return ops->pin_config_group_get(pctldev, selector, config); |
| 138 | } |
| 139 | EXPORT_SYMBOL(pin_config_group_get); |
| 140 | |
| 141 | |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 142 | int pin_config_group_set(const char *dev_name, const char *pin_group, |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 143 | unsigned long config) |
| 144 | { |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 145 | struct pinctrl_dev *pctldev; |
| 146 | const struct pinconf_ops *ops; |
| 147 | const struct pinctrl_ops *pctlops; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 148 | int selector; |
| 149 | const unsigned *pins; |
| 150 | unsigned num_pins; |
| 151 | int ret; |
| 152 | int i; |
| 153 | |
Stephen Warren | 43699de | 2011-12-15 16:57:17 -0700 | [diff] [blame^] | 154 | pctldev = get_pinctrl_dev_from_dev(NULL, dev_name); |
| 155 | if (!pctldev) |
| 156 | return -EINVAL; |
| 157 | ops = pctldev->desc->confops; |
| 158 | pctlops = pctldev->desc->pctlops; |
| 159 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 160 | if (!ops || (!ops->pin_config_group_set && !ops->pin_config_set)) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 161 | dev_err(pctldev->dev, "cannot configure pin group, missing " |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 162 | "config function in driver\n"); |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | selector = pinctrl_get_group_selector(pctldev, pin_group); |
| 167 | if (selector < 0) |
| 168 | return selector; |
| 169 | |
| 170 | ret = pctlops->get_group_pins(pctldev, selector, &pins, &num_pins); |
| 171 | if (ret) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 172 | dev_err(pctldev->dev, "cannot configure pin group, error " |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 173 | "getting pins\n"); |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * If the pin controller supports handling entire groups we use that |
| 179 | * capability. |
| 180 | */ |
| 181 | if (ops->pin_config_group_set) { |
| 182 | ret = ops->pin_config_group_set(pctldev, selector, config); |
| 183 | /* |
| 184 | * If the pin controller prefer that a certain group be handled |
| 185 | * pin-by-pin as well, it returns -EAGAIN. |
| 186 | */ |
| 187 | if (ret != -EAGAIN) |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * If the controller cannot handle entire groups, we configure each pin |
| 193 | * individually. |
| 194 | */ |
| 195 | if (!ops->pin_config_set) |
| 196 | return 0; |
| 197 | |
| 198 | for (i = 0; i < num_pins; i++) { |
| 199 | ret = ops->pin_config_set(pctldev, pins[i], config); |
| 200 | if (ret < 0) |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | EXPORT_SYMBOL(pin_config_group_set); |
| 207 | |
| 208 | int pinconf_check_ops(const struct pinconf_ops *ops) |
| 209 | { |
| 210 | /* We must be able to read out pin status */ |
| 211 | if (!ops->pin_config_get && !ops->pin_config_group_get) |
| 212 | return -EINVAL; |
| 213 | /* We have to be able to config the pins in SOME way */ |
| 214 | if (!ops->pin_config_set && !ops->pin_config_group_set) |
| 215 | return -EINVAL; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | #ifdef CONFIG_DEBUG_FS |
| 220 | |
| 221 | static void pinconf_dump_pin(struct pinctrl_dev *pctldev, |
| 222 | struct seq_file *s, int pin) |
| 223 | { |
| 224 | const struct pinconf_ops *ops = pctldev->desc->confops; |
| 225 | |
| 226 | if (ops && ops->pin_config_dbg_show) |
| 227 | ops->pin_config_dbg_show(pctldev, s, pin); |
| 228 | } |
| 229 | |
| 230 | static int pinconf_pins_show(struct seq_file *s, void *what) |
| 231 | { |
| 232 | struct pinctrl_dev *pctldev = s->private; |
| 233 | unsigned pin; |
| 234 | |
| 235 | seq_puts(s, "Pin config settings per pin\n"); |
| 236 | seq_puts(s, "Format: pin (name): pinmux setting array\n"); |
| 237 | |
| 238 | /* The highest pin number need to be included in the loop, thus <= */ |
| 239 | for (pin = 0; pin <= pctldev->desc->maxpin; pin++) { |
| 240 | struct pin_desc *desc; |
| 241 | |
| 242 | desc = pin_desc_get(pctldev, pin); |
| 243 | /* Pin space may be sparse */ |
| 244 | if (desc == NULL) |
| 245 | continue; |
| 246 | |
| 247 | seq_printf(s, "pin %d (%s):", pin, |
| 248 | desc->name ? desc->name : "unnamed"); |
| 249 | |
| 250 | pinconf_dump_pin(pctldev, s, pin); |
| 251 | |
| 252 | seq_printf(s, "\n"); |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static void pinconf_dump_group(struct pinctrl_dev *pctldev, |
| 259 | struct seq_file *s, unsigned selector, |
| 260 | const char *gname) |
| 261 | { |
| 262 | const struct pinconf_ops *ops = pctldev->desc->confops; |
| 263 | |
| 264 | if (ops && ops->pin_config_group_dbg_show) |
| 265 | ops->pin_config_group_dbg_show(pctldev, s, selector); |
| 266 | } |
| 267 | |
| 268 | static int pinconf_groups_show(struct seq_file *s, void *what) |
| 269 | { |
| 270 | struct pinctrl_dev *pctldev = s->private; |
| 271 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 272 | const struct pinconf_ops *ops = pctldev->desc->confops; |
| 273 | unsigned selector = 0; |
| 274 | |
| 275 | if (!ops || !ops->pin_config_group_get) |
| 276 | return 0; |
| 277 | |
| 278 | seq_puts(s, "Pin config settings per pin group\n"); |
| 279 | seq_puts(s, "Format: group (name): pinmux setting array\n"); |
| 280 | |
| 281 | while (pctlops->list_groups(pctldev, selector) >= 0) { |
| 282 | const char *gname = pctlops->get_group_name(pctldev, selector); |
| 283 | |
| 284 | seq_printf(s, "%u (%s):", selector, gname); |
| 285 | pinconf_dump_group(pctldev, s, selector, gname); |
| 286 | selector++; |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static int pinconf_pins_open(struct inode *inode, struct file *file) |
| 293 | { |
| 294 | return single_open(file, pinconf_pins_show, inode->i_private); |
| 295 | } |
| 296 | |
| 297 | static int pinconf_groups_open(struct inode *inode, struct file *file) |
| 298 | { |
| 299 | return single_open(file, pinconf_groups_show, inode->i_private); |
| 300 | } |
| 301 | |
| 302 | static const struct file_operations pinconf_pins_ops = { |
| 303 | .open = pinconf_pins_open, |
| 304 | .read = seq_read, |
| 305 | .llseek = seq_lseek, |
| 306 | .release = single_release, |
| 307 | }; |
| 308 | |
| 309 | static const struct file_operations pinconf_groups_ops = { |
| 310 | .open = pinconf_groups_open, |
| 311 | .read = seq_read, |
| 312 | .llseek = seq_lseek, |
| 313 | .release = single_release, |
| 314 | }; |
| 315 | |
| 316 | void pinconf_init_device_debugfs(struct dentry *devroot, |
| 317 | struct pinctrl_dev *pctldev) |
| 318 | { |
| 319 | debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO, |
| 320 | devroot, pctldev, &pinconf_pins_ops); |
| 321 | debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO, |
| 322 | devroot, pctldev, &pinconf_groups_ops); |
| 323 | } |
| 324 | |
| 325 | #endif |