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