1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Core driver for the generic pin config portions of the pin control subsystem 4 * 5 * Copyright (C) 2011 ST-Ericsson SA 6 * Written on behalf of Linaro for ST-Ericsson 7 * 8 * Author: Linus Walleij <linus.walleij@linaro.org> 9 */ 10 11 #define pr_fmt(fmt) "generic pinconfig core: " fmt 12 13 #include <linux/array_size.h> 14 #include <linux/debugfs.h> 15 #include <linux/device.h> 16 #include <linux/init.h> 17 #include <linux/module.h> 18 #include <linux/of.h> 19 #include <linux/property.h> 20 #include <linux/slab.h> 21 #include <linux/seq_file.h> 22 23 #include <linux/pinctrl/pinconf-generic.h> 24 #include <linux/pinctrl/pinconf.h> 25 #include <linux/pinctrl/pinctrl.h> 26 27 #include "core.h" 28 #include "pinconf.h" 29 #include "pinctrl-utils.h" 30 31 #ifdef CONFIG_DEBUG_FS 32 static const struct pin_config_item conf_items[] = { 33 PCONFDUMP(PIN_CONFIG_BIAS_BUS_HOLD, "input bias bus hold", NULL, false), 34 PCONFDUMP(PIN_CONFIG_BIAS_DISABLE, "input bias disabled", NULL, false), 35 PCONFDUMP(PIN_CONFIG_BIAS_HIGH_IMPEDANCE, "input bias high impedance", NULL, false), 36 PCONFDUMP(PIN_CONFIG_BIAS_PULL_DOWN, "input bias pull down", "ohms", true), 37 PCONFDUMP(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 38 "input bias pull to pin specific state", "ohms", true), 39 PCONFDUMP(PIN_CONFIG_BIAS_PULL_UP, "input bias pull up", "ohms", true), 40 PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_DRAIN, "output drive open drain", NULL, false), 41 PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_SOURCE, "output drive open source", NULL, false), 42 PCONFDUMP(PIN_CONFIG_DRIVE_PUSH_PULL, "output drive push pull", NULL, false), 43 PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH, "output drive strength", "mA", true), 44 PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH_UA, "output drive strength", "uA", true), 45 PCONFDUMP(PIN_CONFIG_INPUT_DEBOUNCE, "input debounce", "usec", true), 46 PCONFDUMP(PIN_CONFIG_INPUT_ENABLE, "input enabled", NULL, false), 47 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT, "input schmitt trigger", NULL, false), 48 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT_UV, "input schmitt threshold", "uV", true), 49 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT_ENABLE, "input schmitt enabled", NULL, false), 50 PCONFDUMP(PIN_CONFIG_MODE_LOW_POWER, "pin low power", "mode", true), 51 PCONFDUMP(PIN_CONFIG_OUTPUT_ENABLE, "output enabled", NULL, false), 52 PCONFDUMP(PIN_CONFIG_LEVEL, "pin output", "level", true), 53 PCONFDUMP(PIN_CONFIG_OUTPUT_IMPEDANCE_OHMS, "output impedance", "ohms", true), 54 PCONFDUMP(PIN_CONFIG_POWER_SOURCE, "pin power source", "selector", true), 55 PCONFDUMP(PIN_CONFIG_SLEEP_HARDWARE_STATE, "sleep hardware state", NULL, false), 56 PCONFDUMP(PIN_CONFIG_SLEW_RATE, "slew rate", NULL, true), 57 PCONFDUMP(PIN_CONFIG_SKEW_DELAY, "skew delay", NULL, true), 58 PCONFDUMP(PIN_CONFIG_SKEW_DELAY_INPUT_PS, "input skew delay", "ps", true), 59 PCONFDUMP(PIN_CONFIG_SKEW_DELAY_OUTPUT_PS, "output skew delay", "ps", true), 60 PCONFDUMP(PIN_CONFIG_INPUT_VOLTAGE_UV, "input voltage in microvolt", "uV", true), 61 }; 62 63 static void pinconf_generic_dump_one(struct pinctrl_dev *pctldev, 64 struct seq_file *s, const char *gname, 65 unsigned int pin, 66 const struct pin_config_item *items, 67 int nitems, int *print_sep) 68 { 69 int i; 70 71 for (i = 0; i < nitems; i++) { 72 const struct pin_config_item *item = &items[i]; 73 unsigned long config; 74 int ret; 75 76 /* We want to check out this parameter */ 77 config = pinconf_to_config_packed(item->param, 0); 78 if (gname) 79 ret = pin_config_group_get(dev_name(pctldev->dev), 80 gname, &config); 81 else 82 ret = pin_config_get_for_pin(pctldev, pin, &config); 83 /* These are legal errors */ 84 if (ret == -EINVAL || ret == -ENOTSUPP) 85 continue; 86 if (ret) { 87 seq_printf(s, "ERROR READING CONFIG SETTING %d ", i); 88 continue; 89 } 90 /* comma between multiple configs */ 91 if (*print_sep) 92 seq_puts(s, ", "); 93 *print_sep = 1; 94 seq_puts(s, item->display); 95 /* Print unit if available */ 96 if (item->has_arg) { 97 u32 val = pinconf_to_config_argument(config); 98 99 if (item->format) 100 seq_printf(s, " (%u %s)", val, item->format); 101 else 102 seq_printf(s, " (0x%x)", val); 103 104 if (item->values && item->num_values) { 105 if (val < item->num_values) 106 seq_printf(s, " \"%s\"", item->values[val]); 107 else 108 seq_puts(s, " \"(unknown)\""); 109 } 110 } 111 } 112 } 113 114 /** 115 * pinconf_generic_dump_pins - Print information about pin or group of pins 116 * @pctldev: Pincontrol device 117 * @s: File to print to 118 * @gname: Group name specifying pins 119 * @pin: Pin number specifying pin 120 * 121 * Print the pinconf configuration for the requested pin(s) to @s. Pins can be 122 * specified either by pin using @pin or by group using @gname. Only one needs 123 * to be specified the other can be NULL/0. 124 */ 125 void pinconf_generic_dump_pins(struct pinctrl_dev *pctldev, struct seq_file *s, 126 const char *gname, unsigned int pin) 127 { 128 const struct pinconf_ops *ops = pctldev->desc->confops; 129 int print_sep = 0; 130 131 if (!ops->is_generic) 132 return; 133 134 /* generic parameters */ 135 pinconf_generic_dump_one(pctldev, s, gname, pin, conf_items, 136 ARRAY_SIZE(conf_items), &print_sep); 137 /* driver-specific parameters */ 138 if (pctldev->desc->num_custom_params && 139 pctldev->desc->custom_conf_items) 140 pinconf_generic_dump_one(pctldev, s, gname, pin, 141 pctldev->desc->custom_conf_items, 142 pctldev->desc->num_custom_params, 143 &print_sep); 144 } 145 146 void pinconf_generic_dump_config(struct pinctrl_dev *pctldev, 147 struct seq_file *s, unsigned long config) 148 { 149 int i; 150 151 for (i = 0; i < ARRAY_SIZE(conf_items); i++) { 152 if (pinconf_to_config_param(config) != conf_items[i].param) 153 continue; 154 seq_printf(s, "%s: 0x%x", conf_items[i].display, 155 pinconf_to_config_argument(config)); 156 } 157 158 if (!pctldev->desc->num_custom_params || 159 !pctldev->desc->custom_conf_items) 160 return; 161 162 for (i = 0; i < pctldev->desc->num_custom_params; i++) { 163 if (pinconf_to_config_param(config) != 164 pctldev->desc->custom_conf_items[i].param) 165 continue; 166 seq_printf(s, "%s: 0x%x", 167 pctldev->desc->custom_conf_items[i].display, 168 pinconf_to_config_argument(config)); 169 } 170 } 171 EXPORT_SYMBOL_GPL(pinconf_generic_dump_config); 172 #endif 173 174 #ifdef CONFIG_OF 175 static const struct pinconf_generic_params dt_params[] = { 176 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 }, 177 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 }, 178 { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 }, 179 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 }, 180 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 }, 181 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 }, 182 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 }, 183 { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 }, 184 { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 }, 185 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 }, 186 { "drive-strength-microamp", PIN_CONFIG_DRIVE_STRENGTH_UA, 0 }, 187 { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 }, 188 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 }, 189 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 }, 190 { "input-schmitt", PIN_CONFIG_INPUT_SCHMITT, 0 }, 191 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 }, 192 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 }, 193 { "input-schmitt-microvolts", PIN_CONFIG_INPUT_SCHMITT_UV, 0 }, 194 { "low-power-disable", PIN_CONFIG_MODE_LOW_POWER, 0 }, 195 { "low-power-enable", PIN_CONFIG_MODE_LOW_POWER, 1 }, 196 { "output-disable", PIN_CONFIG_OUTPUT_ENABLE, 0 }, 197 { "output-enable", PIN_CONFIG_OUTPUT_ENABLE, 1 }, 198 { "output-high", PIN_CONFIG_LEVEL, 1, }, 199 { "output-impedance-ohms", PIN_CONFIG_OUTPUT_IMPEDANCE_OHMS, 0 }, 200 { "output-low", PIN_CONFIG_LEVEL, 0, }, 201 { "power-source", PIN_CONFIG_POWER_SOURCE, 0 }, 202 { "sleep-hardware-state", PIN_CONFIG_SLEEP_HARDWARE_STATE, 0 }, 203 { "slew-rate", PIN_CONFIG_SLEW_RATE, 0 }, 204 { "skew-delay", PIN_CONFIG_SKEW_DELAY, 0 }, 205 { "skew-delay-input-ps", PIN_CONFIG_SKEW_DELAY_INPUT_PS, 0 }, 206 { "skew-delay-output-ps", PIN_CONFIG_SKEW_DELAY_OUTPUT_PS, 0 }, 207 { "input-threshold-voltage-microvolt", PIN_CONFIG_INPUT_VOLTAGE_UV, 0 }, 208 }; 209 210 /** 211 * parse_fw_cfg() - Parse firmware pinconf parameters 212 * @fwnode: firmware node 213 * @params: Array of describing generic parameters 214 * @count: Number of entries in @params 215 * @cfg: Array of parsed config options 216 * @ncfg: Number of entries in @cfg 217 * 218 * Parse the config options described in @params from @fwnode and puts the result 219 * in @cfg. @cfg does not need to be empty, entries are added beginning at 220 * @ncfg. @ncfg is updated to reflect the number of entries after parsing. @cfg 221 * needs to have enough memory allocated to hold all possible entries. 222 */ 223 static int parse_fw_cfg(struct fwnode_handle *fwnode, 224 const struct pinconf_generic_params *params, 225 unsigned int count, unsigned long *cfg, 226 unsigned int *ncfg) 227 { 228 int i, test; 229 230 unsigned long *properties __free(bitmap) = bitmap_zalloc(count, GFP_KERNEL); 231 232 for (i = 0; i < count; i++) { 233 u32 val; 234 int ret; 235 const struct pinconf_generic_params *par = ¶ms[i]; 236 237 if (par->values && par->num_values) { 238 ret = fwnode_property_match_property_string(fwnode, 239 par->property, 240 par->values, par->num_values); 241 if (ret == -ENOENT) 242 return ret; 243 if (ret >= 0) { 244 val = ret; 245 ret = 0; 246 } 247 } else { 248 ret = fwnode_property_read_u32(fwnode, par->property, &val); 249 } 250 251 /* property not found */ 252 if (ret == -EINVAL) 253 continue; 254 255 /* use default value, when no value is specified */ 256 if (ret) 257 val = par->default_value; 258 259 /* if param is greater than count, these are custom properties */ 260 if (par->param <= count) { 261 ret = test_and_set_bit(par->param, properties); 262 if (ret) { 263 pr_err("%pfw: conflicting setting detected for %s\n", 264 fwnode, par->property); 265 return -EINVAL; 266 } 267 } 268 269 pr_debug("found %s with value %u\n", par->property, val); 270 cfg[*ncfg] = pinconf_to_config_packed(par->param, val); 271 (*ncfg)++; 272 } 273 274 if (test_bit(PIN_CONFIG_DRIVE_STRENGTH, properties) && 275 test_bit(PIN_CONFIG_DRIVE_STRENGTH_UA, properties)) 276 pr_err("%pfw: cannot have multiple drive strength properties\n", 277 fwnode); 278 279 test = test_bit(PIN_CONFIG_BIAS_BUS_HOLD, properties) + 280 test_bit(PIN_CONFIG_BIAS_DISABLE, properties) + 281 test_bit(PIN_CONFIG_BIAS_HIGH_IMPEDANCE, properties) + 282 test_bit(PIN_CONFIG_BIAS_PULL_UP, properties) + 283 test_bit(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, properties) + 284 test_bit(PIN_CONFIG_BIAS_PULL_DOWN, properties); 285 if (test > 1) 286 pr_err("%pfw: cannot have multiple bias configurations\n", 287 fwnode); 288 289 test = test_bit(PIN_CONFIG_DRIVE_OPEN_DRAIN, properties) + 290 test_bit(PIN_CONFIG_DRIVE_OPEN_SOURCE, properties) + 291 test_bit(PIN_CONFIG_DRIVE_PUSH_PULL, properties); 292 if (test > 1) 293 pr_err("%pfw: cannot have multiple drive configurations\n", 294 fwnode); 295 296 return 0; 297 } 298 299 /** 300 * pinconf_generic_parse_dt_pinmux() 301 * parse the pinmux properties into generic pin mux values. 302 * @np: node containing the pinmux properties 303 * @dev: pincontrol core device 304 * @pid: array with pin identity entries 305 * @pmux: array with pin mux value entries 306 * @npins: number of pins 307 * 308 * pinmux property: mux value [0,7]bits and pin identity [8,31]bits. 309 */ 310 int pinconf_generic_parse_dt_pinmux(struct device_node *np, struct device *dev, 311 unsigned int **pid, unsigned int **pmux, 312 unsigned int *npins) 313 { 314 struct fwnode_handle *fwnode = of_fwnode_handle(np); 315 unsigned int *pid_t; 316 unsigned int *pmux_t; 317 unsigned int npins_t, i; 318 int ret; 319 320 ret = fwnode_property_count_u32(fwnode, "pinmux"); 321 if (ret < 0) { 322 dev_info(dev, "Missing pinmux property\n"); 323 return ret; 324 } 325 326 npins_t = ret; 327 if (npins_t == 0) { 328 dev_info(dev, "pinmux property doesn't have entries\n"); 329 return -ENODATA; 330 } 331 332 if (!pid || !pmux || !npins) { 333 dev_err(dev, "parameters error\n"); 334 return -EINVAL; 335 } 336 337 pid_t = devm_kcalloc(dev, npins_t, sizeof(*pid_t), GFP_KERNEL); 338 pmux_t = devm_kcalloc(dev, npins_t, sizeof(*pmux_t), GFP_KERNEL); 339 if (!pid_t || !pmux_t) { 340 dev_err(dev, "kalloc memory fail\n"); 341 return -ENOMEM; 342 } 343 344 ret = fwnode_property_read_u32_array(fwnode, "pinmux", pmux_t, npins_t); 345 if (ret) { 346 dev_err(dev, "get pinmux value fail\n"); 347 goto exit; 348 } 349 350 for (i = 0; i < npins_t; i++) { 351 pid_t[i] = pmux_t[i] >> 8; 352 pmux_t[i] = pmux_t[i] & 0xff; 353 } 354 *pid = pid_t; 355 *pmux = pmux_t; 356 *npins = npins_t; 357 358 return 0; 359 exit: 360 devm_kfree(dev, pid_t); 361 devm_kfree(dev, pmux_t); 362 return ret; 363 } 364 EXPORT_SYMBOL_GPL(pinconf_generic_parse_dt_pinmux); 365 366 /** 367 * pinconf_generic_parse_dt_config() 368 * parse the config properties into generic pinconfig values. 369 * @np: node containing the pinconfig properties 370 * @pctldev: pincontrol device 371 * @configs: array with nconfigs entries containing the generic pinconf values 372 * must be freed when no longer necessary. 373 * @nconfigs: number of configurations 374 */ 375 int pinconf_generic_parse_dt_config(struct device_node *np, 376 struct pinctrl_dev *pctldev, 377 unsigned long **configs, 378 unsigned int *nconfigs) 379 { 380 unsigned long *cfg; 381 unsigned int max_cfg, ncfg = 0; 382 struct fwnode_handle *fwnode; 383 int ret; 384 385 fwnode = of_fwnode_handle(np); 386 if (!fwnode) 387 return -EINVAL; 388 389 /* allocate a temporary array big enough to hold one of each option */ 390 max_cfg = ARRAY_SIZE(dt_params); 391 if (pctldev) 392 max_cfg += pctldev->desc->num_custom_params; 393 cfg = kcalloc(max_cfg, sizeof(*cfg), GFP_KERNEL); 394 if (!cfg) 395 return -ENOMEM; 396 397 ret = parse_fw_cfg(fwnode, dt_params, ARRAY_SIZE(dt_params), cfg, &ncfg); 398 if (ret) 399 goto out; 400 if (pctldev && pctldev->desc->num_custom_params && 401 pctldev->desc->custom_params) { 402 ret = parse_fw_cfg(fwnode, pctldev->desc->custom_params, 403 pctldev->desc->num_custom_params, cfg, &ncfg); 404 if (ret) 405 goto out; 406 } 407 408 /* no configs found at all */ 409 if (ncfg == 0) { 410 *configs = NULL; 411 *nconfigs = 0; 412 goto out; 413 } 414 415 /* 416 * Now limit the number of configs to the real number of 417 * found properties. 418 */ 419 *configs = kmemdup_array(cfg, ncfg, sizeof(unsigned long), GFP_KERNEL); 420 if (!*configs) { 421 ret = -ENOMEM; 422 goto out; 423 } 424 425 *nconfigs = ncfg; 426 427 out: 428 kfree(cfg); 429 return ret; 430 } 431 EXPORT_SYMBOL_GPL(pinconf_generic_parse_dt_config); 432 433 int pinconf_generic_dt_subnode_to_map(struct pinctrl_dev *pctldev, 434 struct device_node *np, struct pinctrl_map **map, 435 unsigned int *reserved_maps, unsigned int *num_maps, 436 enum pinctrl_map_type type) 437 { 438 int ret; 439 const char *function; 440 struct device *dev = pctldev->dev; 441 unsigned long *configs = NULL; 442 unsigned int num_configs = 0; 443 unsigned int reserve, strings_count; 444 struct property *prop; 445 const char *group; 446 const char *subnode_target_type = "pins"; 447 448 ret = of_property_count_strings(np, "pins"); 449 if (ret < 0) { 450 ret = of_property_count_strings(np, "groups"); 451 if (ret < 0) 452 /* skip this node; may contain config child nodes */ 453 return 0; 454 if (type == PIN_MAP_TYPE_INVALID) 455 type = PIN_MAP_TYPE_CONFIGS_GROUP; 456 subnode_target_type = "groups"; 457 } else { 458 if (type == PIN_MAP_TYPE_INVALID) 459 type = PIN_MAP_TYPE_CONFIGS_PIN; 460 } 461 strings_count = ret; 462 463 ret = of_property_read_string(np, "function", &function); 464 if (ret < 0) { 465 /* EINVAL=missing, which is fine since it's optional */ 466 if (ret != -EINVAL) 467 dev_err(dev, "%pOF: could not parse property function\n", 468 np); 469 function = NULL; 470 } 471 472 ret = pinconf_generic_parse_dt_config(np, pctldev, &configs, 473 &num_configs); 474 if (ret < 0) { 475 dev_err(dev, "%pOF: could not parse node property\n", np); 476 return ret; 477 } 478 479 reserve = 0; 480 if (function != NULL) 481 reserve++; 482 if (num_configs) 483 reserve++; 484 485 reserve *= strings_count; 486 487 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps, 488 num_maps, reserve); 489 if (ret < 0) 490 goto exit; 491 492 of_property_for_each_string(np, subnode_target_type, prop, group) { 493 if (function) { 494 ret = pinctrl_utils_add_map_mux(pctldev, map, 495 reserved_maps, num_maps, group, 496 function); 497 if (ret < 0) 498 goto exit; 499 } 500 501 if (num_configs) { 502 ret = pinctrl_utils_add_map_configs(pctldev, map, 503 reserved_maps, num_maps, group, configs, 504 num_configs, type); 505 if (ret < 0) 506 goto exit; 507 } 508 } 509 ret = 0; 510 511 exit: 512 kfree(configs); 513 return ret; 514 } 515 EXPORT_SYMBOL_GPL(pinconf_generic_dt_subnode_to_map); 516 517 int pinconf_generic_dt_node_to_map(struct pinctrl_dev *pctldev, 518 struct device_node *np_config, struct pinctrl_map **map, 519 unsigned int *num_maps, enum pinctrl_map_type type) 520 { 521 unsigned int reserved_maps; 522 int ret; 523 524 reserved_maps = 0; 525 *map = NULL; 526 *num_maps = 0; 527 528 ret = pinconf_generic_dt_subnode_to_map(pctldev, np_config, map, 529 &reserved_maps, num_maps, type); 530 if (ret < 0) 531 goto exit; 532 533 for_each_available_child_of_node_scoped(np_config, np) { 534 ret = pinconf_generic_dt_subnode_to_map(pctldev, np, map, 535 &reserved_maps, num_maps, type); 536 if (ret < 0) 537 goto exit; 538 } 539 return 0; 540 541 exit: 542 pinctrl_utils_free_map(pctldev, *map, *num_maps); 543 return ret; 544 } 545 EXPORT_SYMBOL_GPL(pinconf_generic_dt_node_to_map); 546 547 void pinconf_generic_dt_free_map(struct pinctrl_dev *pctldev, 548 struct pinctrl_map *map, 549 unsigned int num_maps) 550 { 551 pinctrl_utils_free_map(pctldev, map, num_maps); 552 } 553 EXPORT_SYMBOL_GPL(pinconf_generic_dt_free_map); 554 555 #endif 556