Linus Walleij | 394349f | 2011-11-24 18:27:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Core driver for the generic 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 | |
| 12 | #define pr_fmt(fmt) "generic pinconfig core: " fmt |
| 13 | |
| 14 | #include <linux/kernel.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/pinctrl.h> |
| 21 | #include <linux/pinctrl/pinconf.h> |
| 22 | #include <linux/pinctrl/pinconf-generic.h> |
Heiko Stübner | 06c0400 | 2013-06-10 21:40:29 +0200 | [diff] [blame] | 23 | #include <linux/of.h> |
Linus Walleij | 394349f | 2011-11-24 18:27:15 +0100 | [diff] [blame] | 24 | #include "core.h" |
| 25 | #include "pinconf.h" |
| 26 | |
| 27 | #ifdef CONFIG_DEBUG_FS |
| 28 | |
| 29 | struct pin_config_item { |
| 30 | const enum pin_config_param param; |
| 31 | const char * const display; |
| 32 | const char * const format; |
| 33 | }; |
| 34 | |
| 35 | #define PCONFDUMP(a, b, c) { .param = a, .display = b, .format = c } |
| 36 | |
| 37 | struct pin_config_item conf_items[] = { |
| 38 | PCONFDUMP(PIN_CONFIG_BIAS_DISABLE, "input bias disabled", NULL), |
| 39 | PCONFDUMP(PIN_CONFIG_BIAS_HIGH_IMPEDANCE, "input bias high impedance", NULL), |
James Hogan | b9ae045 | 2013-05-24 17:21:12 +0100 | [diff] [blame] | 40 | PCONFDUMP(PIN_CONFIG_BIAS_BUS_HOLD, "input bias bus hold", NULL), |
Linus Walleij | 394349f | 2011-11-24 18:27:15 +0100 | [diff] [blame] | 41 | PCONFDUMP(PIN_CONFIG_BIAS_PULL_UP, "input bias pull up", NULL), |
| 42 | PCONFDUMP(PIN_CONFIG_BIAS_PULL_DOWN, "input bias pull down", NULL), |
Heiko Stübner | a4e478c | 2013-06-06 16:44:25 +0200 | [diff] [blame] | 43 | PCONFDUMP(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, |
| 44 | "input bias pull to pin specific state", NULL), |
Linus Walleij | 394349f | 2011-11-24 18:27:15 +0100 | [diff] [blame] | 45 | PCONFDUMP(PIN_CONFIG_DRIVE_PUSH_PULL, "output drive push pull", NULL), |
| 46 | PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_DRAIN, "output drive open drain", NULL), |
| 47 | PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_SOURCE, "output drive open source", NULL), |
James Hogan | 2dd1521 | 2013-05-24 17:21:11 +0100 | [diff] [blame] | 48 | PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH, "output drive strength", "mA"), |
Haojian Zhuang | eda8952b | 2013-02-12 01:10:57 +0800 | [diff] [blame] | 49 | PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT_ENABLE, "input schmitt enabled", NULL), |
Linus Walleij | 394349f | 2011-11-24 18:27:15 +0100 | [diff] [blame] | 50 | PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT, "input schmitt trigger", NULL), |
| 51 | PCONFDUMP(PIN_CONFIG_INPUT_DEBOUNCE, "input debounce", "time units"), |
| 52 | PCONFDUMP(PIN_CONFIG_POWER_SOURCE, "pin power source", "selector"), |
Haojian Zhuang | 8d08d96 | 2013-01-18 15:31:15 +0800 | [diff] [blame] | 53 | PCONFDUMP(PIN_CONFIG_SLEW_RATE, "slew rate", NULL), |
Linus Walleij | 394349f | 2011-11-24 18:27:15 +0100 | [diff] [blame] | 54 | PCONFDUMP(PIN_CONFIG_LOW_POWER_MODE, "pin low power", "mode"), |
Linus Walleij | 50ec39a | 2013-01-04 17:57:40 +0100 | [diff] [blame] | 55 | PCONFDUMP(PIN_CONFIG_OUTPUT, "pin output", "level"), |
Linus Walleij | 394349f | 2011-11-24 18:27:15 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | void pinconf_generic_dump_pin(struct pinctrl_dev *pctldev, |
| 59 | struct seq_file *s, unsigned pin) |
| 60 | { |
| 61 | const struct pinconf_ops *ops = pctldev->desc->confops; |
| 62 | int i; |
| 63 | |
| 64 | if (!ops->is_generic) |
| 65 | return; |
| 66 | |
| 67 | for(i = 0; i < ARRAY_SIZE(conf_items); i++) { |
| 68 | unsigned long config; |
| 69 | int ret; |
| 70 | |
| 71 | /* We want to check out this parameter */ |
| 72 | config = pinconf_to_config_packed(conf_items[i].param, 0); |
| 73 | ret = pin_config_get_for_pin(pctldev, pin, &config); |
| 74 | /* These are legal errors */ |
| 75 | if (ret == -EINVAL || ret == -ENOTSUPP) |
| 76 | continue; |
| 77 | if (ret) { |
| 78 | seq_printf(s, "ERROR READING CONFIG SETTING %d ", i); |
| 79 | continue; |
| 80 | } |
| 81 | /* Space between multiple configs */ |
| 82 | seq_puts(s, " "); |
| 83 | seq_puts(s, conf_items[i].display); |
| 84 | /* Print unit if available */ |
| 85 | if (conf_items[i].format && |
| 86 | pinconf_to_config_argument(config) != 0) |
| 87 | seq_printf(s, " (%u %s)", |
| 88 | pinconf_to_config_argument(config), |
| 89 | conf_items[i].format); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void pinconf_generic_dump_group(struct pinctrl_dev *pctldev, |
| 94 | struct seq_file *s, const char *gname) |
| 95 | { |
| 96 | const struct pinconf_ops *ops = pctldev->desc->confops; |
| 97 | int i; |
| 98 | |
| 99 | if (!ops->is_generic) |
| 100 | return; |
| 101 | |
| 102 | for(i = 0; i < ARRAY_SIZE(conf_items); i++) { |
| 103 | unsigned long config; |
| 104 | int ret; |
| 105 | |
| 106 | /* We want to check out this parameter */ |
| 107 | config = pinconf_to_config_packed(conf_items[i].param, 0); |
| 108 | ret = pin_config_group_get(dev_name(pctldev->dev), gname, |
| 109 | &config); |
| 110 | /* These are legal errors */ |
| 111 | if (ret == -EINVAL || ret == -ENOTSUPP) |
| 112 | continue; |
| 113 | if (ret) { |
| 114 | seq_printf(s, "ERROR READING CONFIG SETTING %d ", i); |
| 115 | continue; |
| 116 | } |
| 117 | /* Space between multiple configs */ |
| 118 | seq_puts(s, " "); |
| 119 | seq_puts(s, conf_items[i].display); |
| 120 | /* Print unit if available */ |
| 121 | if (conf_items[i].format && config != 0) |
| 122 | seq_printf(s, " (%u %s)", |
| 123 | pinconf_to_config_argument(config), |
| 124 | conf_items[i].format); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | #endif |
Heiko Stübner | 06c0400 | 2013-06-10 21:40:29 +0200 | [diff] [blame] | 129 | |
| 130 | #ifdef CONFIG_OF |
| 131 | struct pinconf_generic_dt_params { |
| 132 | const char * const property; |
| 133 | enum pin_config_param param; |
| 134 | u32 default_value; |
| 135 | }; |
| 136 | |
| 137 | static struct pinconf_generic_dt_params dt_params[] = { |
| 138 | { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 }, |
| 139 | { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 }, |
| 140 | { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 }, |
Heiko Stübner | b87b814 | 2013-06-14 17:42:49 +0200 | [diff] [blame] | 141 | { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 }, |
| 142 | { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 }, |
| 143 | { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 }, |
Heiko Stübner | 06c0400 | 2013-06-10 21:40:29 +0200 | [diff] [blame] | 144 | { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 }, |
| 145 | { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 }, |
| 146 | { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 }, |
| 147 | { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 }, |
| 148 | { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 }, |
| 149 | { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 }, |
| 150 | { "input-schmitt", PIN_CONFIG_INPUT_SCHMITT, 0 }, |
| 151 | { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 }, |
| 152 | { "power-source", PIN_CONFIG_POWER_SOURCE, 0 }, |
| 153 | { "slew-rate", PIN_CONFIG_SLEW_RATE, 0 }, |
Heiko Stübner | b87b814 | 2013-06-14 17:42:49 +0200 | [diff] [blame] | 154 | { "low-power-enable", PIN_CONFIG_LOW_POWER_MODE, 1 }, |
| 155 | { "low-power-disable", PIN_CONFIG_LOW_POWER_MODE, 0 }, |
Heiko Stübner | 06c0400 | 2013-06-10 21:40:29 +0200 | [diff] [blame] | 156 | { "output-low", PIN_CONFIG_OUTPUT, 0, }, |
| 157 | { "output-high", PIN_CONFIG_OUTPUT, 1, }, |
| 158 | }; |
| 159 | |
| 160 | /** |
| 161 | * pinconf_generic_parse_dt_config() |
| 162 | * parse the config properties into generic pinconfig values. |
| 163 | * @np: node containing the pinconfig properties |
| 164 | * @configs: array with nconfigs entries containing the generic pinconf values |
| 165 | * @nconfigs: umber of configurations |
| 166 | */ |
| 167 | int pinconf_generic_parse_dt_config(struct device_node *np, |
| 168 | unsigned long **configs, |
| 169 | unsigned int *nconfigs) |
| 170 | { |
Heiko Stübner | 162e808 | 2013-06-14 17:43:55 +0200 | [diff] [blame] | 171 | unsigned long *cfg; |
Heiko Stübner | 06c0400 | 2013-06-10 21:40:29 +0200 | [diff] [blame] | 172 | unsigned int ncfg = 0; |
| 173 | int ret; |
| 174 | int i; |
| 175 | u32 val; |
| 176 | |
| 177 | if (!np) |
| 178 | return -EINVAL; |
| 179 | |
Heiko Stübner | 162e808 | 2013-06-14 17:43:55 +0200 | [diff] [blame] | 180 | /* allocate a temporary array big enough to hold one of each option */ |
| 181 | cfg = kzalloc(sizeof(*cfg) * ARRAY_SIZE(dt_params), GFP_KERNEL); |
| 182 | if (!cfg) |
| 183 | return -ENOMEM; |
| 184 | |
Heiko Stübner | 06c0400 | 2013-06-10 21:40:29 +0200 | [diff] [blame] | 185 | for (i = 0; i < ARRAY_SIZE(dt_params); i++) { |
| 186 | struct pinconf_generic_dt_params *par = &dt_params[i]; |
| 187 | ret = of_property_read_u32(np, par->property, &val); |
| 188 | |
| 189 | /* property not found */ |
| 190 | if (ret == -EINVAL) |
| 191 | continue; |
| 192 | |
| 193 | /* use default value, when no value is specified */ |
| 194 | if (ret) |
| 195 | val = par->default_value; |
| 196 | |
| 197 | pr_debug("found %s with value %u\n", par->property, val); |
| 198 | cfg[ncfg] = pinconf_to_config_packed(par->param, val); |
| 199 | ncfg++; |
| 200 | } |
| 201 | |
Heiko Stübner | 162e808 | 2013-06-14 17:43:55 +0200 | [diff] [blame] | 202 | ret = 0; |
| 203 | |
Heiko Stübner | 829a05d | 2013-06-14 17:43:21 +0200 | [diff] [blame] | 204 | /* no configs found at all */ |
| 205 | if (ncfg == 0) { |
| 206 | *configs = NULL; |
| 207 | *nconfigs = 0; |
Heiko Stübner | 162e808 | 2013-06-14 17:43:55 +0200 | [diff] [blame] | 208 | goto out; |
Heiko Stübner | 829a05d | 2013-06-14 17:43:21 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Heiko Stübner | 06c0400 | 2013-06-10 21:40:29 +0200 | [diff] [blame] | 211 | /* |
| 212 | * Now limit the number of configs to the real number of |
| 213 | * found properties. |
| 214 | */ |
| 215 | *configs = kzalloc(ncfg * sizeof(unsigned long), GFP_KERNEL); |
Heiko Stübner | 162e808 | 2013-06-14 17:43:55 +0200 | [diff] [blame] | 216 | if (!*configs) { |
| 217 | ret = -ENOMEM; |
| 218 | goto out; |
| 219 | } |
Heiko Stübner | 06c0400 | 2013-06-10 21:40:29 +0200 | [diff] [blame] | 220 | |
Heiko Stübner | 162e808 | 2013-06-14 17:43:55 +0200 | [diff] [blame] | 221 | memcpy(*configs, cfg, ncfg * sizeof(unsigned long)); |
Heiko Stübner | 06c0400 | 2013-06-10 21:40:29 +0200 | [diff] [blame] | 222 | *nconfigs = ncfg; |
Heiko Stübner | 162e808 | 2013-06-14 17:43:55 +0200 | [diff] [blame] | 223 | |
| 224 | out: |
| 225 | kfree(cfg); |
| 226 | return ret; |
Heiko Stübner | 06c0400 | 2013-06-10 21:40:29 +0200 | [diff] [blame] | 227 | } |
| 228 | #endif |