1 /* 2 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com> 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <linux/libfdt.h> 9 #include <linux/err.h> 10 #include <linux/list.h> 11 #include <dm.h> 12 #include <dm/lists.h> 13 #include <dm/pinctrl.h> 14 #include <dm/util.h> 15 #include <dm/of_access.h> 16 17 DECLARE_GLOBAL_DATA_PTR; 18 19 int pinctrl_decode_pin_config(const void *blob, int node) 20 { 21 int flags = 0; 22 23 if (fdtdec_get_bool(blob, node, "bias-pull-up")) 24 flags |= 1 << PIN_CONFIG_BIAS_PULL_UP; 25 else if (fdtdec_get_bool(blob, node, "bias-pull-down")) 26 flags |= 1 << PIN_CONFIG_BIAS_PULL_DOWN; 27 28 return flags; 29 } 30 31 #if CONFIG_IS_ENABLED(PINCTRL_FULL) 32 /** 33 * pinctrl_config_one() - apply pinctrl settings for a single node 34 * 35 * @config: pin configuration node 36 * @return: 0 on success, or negative error code on failure 37 */ 38 static int pinctrl_config_one(struct udevice *config) 39 { 40 struct udevice *pctldev; 41 const struct pinctrl_ops *ops; 42 43 pctldev = config; 44 for (;;) { 45 pctldev = dev_get_parent(pctldev); 46 if (!pctldev) { 47 dev_err(config, "could not find pctldev\n"); 48 return -EINVAL; 49 } 50 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL) 51 break; 52 } 53 54 ops = pinctrl_get_ops(pctldev); 55 return ops->set_state(pctldev, config); 56 } 57 58 /** 59 * pinctrl_select_state_full() - full implementation of pinctrl_select_state 60 * 61 * @dev: peripheral device 62 * @statename: state name, like "default" 63 * @return: 0 on success, or negative error code on failure 64 */ 65 static int pinctrl_select_state_full(struct udevice *dev, const char *statename) 66 { 67 char propname[32]; /* long enough */ 68 const fdt32_t *list; 69 uint32_t phandle; 70 struct udevice *config; 71 int state, size, i, ret; 72 73 state = dev_read_stringlist_search(dev, "pinctrl-names", statename); 74 if (state < 0) { 75 char *end; 76 /* 77 * If statename is not found in "pinctrl-names", 78 * assume statename is just the integer state ID. 79 */ 80 state = simple_strtoul(statename, &end, 10); 81 if (*end) 82 return -ENOSYS; 83 } 84 85 snprintf(propname, sizeof(propname), "pinctrl-%d", state); 86 list = dev_read_prop(dev, propname, &size); 87 if (!list) 88 return -ENOSYS; 89 90 size /= sizeof(*list); 91 for (i = 0; i < size; i++) { 92 phandle = fdt32_to_cpu(*list++); 93 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle, 94 &config); 95 if (ret) { 96 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n", 97 __func__, ret); 98 continue; 99 } 100 101 ret = pinctrl_config_one(config); 102 if (ret) { 103 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n", 104 __func__, ret); 105 continue; 106 } 107 } 108 109 return 0; 110 } 111 112 /** 113 * pinconfig_post_bind() - post binding for PINCONFIG uclass 114 * Recursively bind its children as pinconfig devices. 115 * 116 * @dev: pinconfig device 117 * @return: 0 on success, or negative error code on failure 118 */ 119 static int pinconfig_post_bind(struct udevice *dev) 120 { 121 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC); 122 const char *name; 123 ofnode node; 124 int ret; 125 126 if (!dev_of_valid(dev)) 127 return 0; 128 129 dev_for_each_subnode(node, dev) { 130 if (pre_reloc_only && 131 !ofnode_pre_reloc(node)) 132 continue; 133 /* 134 * If this node has "compatible" property, this is not 135 * a pin configuration node, but a normal device. skip. 136 */ 137 ofnode_get_property(node, "compatible", &ret); 138 if (ret >= 0) 139 continue; 140 141 if (ret != -FDT_ERR_NOTFOUND) 142 return ret; 143 144 name = ofnode_get_name(node); 145 if (!name) 146 return -EINVAL; 147 ret = device_bind_driver_to_node(dev, "pinconfig", name, 148 node, NULL); 149 if (ret) 150 return ret; 151 } 152 153 return 0; 154 } 155 156 UCLASS_DRIVER(pinconfig) = { 157 .id = UCLASS_PINCONFIG, 158 #if CONFIG_IS_ENABLED(PINCONF_RECURSIVE) 159 .post_bind = pinconfig_post_bind, 160 #endif 161 .name = "pinconfig", 162 }; 163 164 U_BOOT_DRIVER(pinconfig_generic) = { 165 .name = "pinconfig", 166 .id = UCLASS_PINCONFIG, 167 }; 168 169 #else 170 static int pinconfig_post_bind(struct udevice *dev) 171 { 172 return 0; 173 } 174 175 static int pinctrl_select_state_full(struct udevice *dev, const char *statename) 176 { 177 return 0; 178 } 179 #endif 180 181 static int 182 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset, 183 struct udevice **pctldev, 184 unsigned int *pin_selector) 185 { 186 struct ofnode_phandle_args args; 187 unsigned gpio_offset, pfc_base, pfc_pins; 188 int ret; 189 190 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3, 191 0, &args); 192 if (ret) { 193 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n", 194 __func__, ret); 195 return ret; 196 } 197 198 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL, 199 args.node, pctldev); 200 if (ret) { 201 dev_dbg(dev, 202 "%s: uclass_get_device_by_of_offset failed: err=%d\n", 203 __func__, ret); 204 return ret; 205 } 206 207 gpio_offset = args.args[0]; 208 pfc_base = args.args[1]; 209 pfc_pins = args.args[2]; 210 211 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) { 212 dev_dbg(dev, 213 "%s: GPIO can not be mapped to pincontrol pin\n", 214 __func__); 215 return -EINVAL; 216 } 217 218 offset -= gpio_offset; 219 offset += pfc_base; 220 *pin_selector = offset; 221 222 return 0; 223 } 224 225 /** 226 * pinctrl_gpio_request() - request a single pin to be used as GPIO 227 * 228 * @dev: GPIO peripheral device 229 * @offset: the GPIO pin offset from the GPIO controller 230 * @return: 0 on success, or negative error code on failure 231 */ 232 int pinctrl_gpio_request(struct udevice *dev, unsigned offset) 233 { 234 const struct pinctrl_ops *ops; 235 struct udevice *pctldev; 236 unsigned int pin_selector; 237 int ret; 238 239 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset, 240 &pctldev, &pin_selector); 241 if (ret) 242 return ret; 243 244 ops = pinctrl_get_ops(pctldev); 245 assert(ops); 246 if (!ops->gpio_request_enable) 247 return -ENOSYS; 248 249 return ops->gpio_request_enable(pctldev, pin_selector); 250 } 251 252 /** 253 * pinctrl_gpio_free() - free a single pin used as GPIO 254 * 255 * @dev: GPIO peripheral device 256 * @offset: the GPIO pin offset from the GPIO controller 257 * @return: 0 on success, or negative error code on failure 258 */ 259 int pinctrl_gpio_free(struct udevice *dev, unsigned offset) 260 { 261 const struct pinctrl_ops *ops; 262 struct udevice *pctldev; 263 unsigned int pin_selector; 264 int ret; 265 266 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset, 267 &pctldev, &pin_selector); 268 if (ret) 269 return ret; 270 271 ops = pinctrl_get_ops(pctldev); 272 assert(ops); 273 if (!ops->gpio_disable_free) 274 return -ENOSYS; 275 276 return ops->gpio_disable_free(pctldev, pin_selector); 277 } 278 279 /** 280 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state 281 * 282 * @dev: peripheral device 283 * @return: 0 on success, or negative error code on failure 284 */ 285 static int pinctrl_select_state_simple(struct udevice *dev) 286 { 287 struct udevice *pctldev; 288 struct pinctrl_ops *ops; 289 int ret; 290 291 /* 292 * For most system, there is only one pincontroller device. But in 293 * case of multiple pincontroller devices, probe the one with sequence 294 * number 0 (defined by alias) to avoid race condition. 295 */ 296 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev); 297 if (ret) 298 /* if not found, get the first one */ 299 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev); 300 if (ret) 301 return ret; 302 303 ops = pinctrl_get_ops(pctldev); 304 if (!ops->set_state_simple) { 305 dev_dbg(dev, "set_state_simple op missing\n"); 306 return -ENOSYS; 307 } 308 309 return ops->set_state_simple(pctldev, dev); 310 } 311 312 int pinctrl_select_state(struct udevice *dev, const char *statename) 313 { 314 /* 315 * Some device which is logical like mmc.blk, do not have 316 * a valid ofnode. 317 */ 318 if (!ofnode_valid(dev->node)) 319 return 0; 320 /* 321 * Try full-implemented pinctrl first. 322 * If it fails or is not implemented, try simple one. 323 */ 324 if (CONFIG_IS_ENABLED(PINCTRL_FULL)) 325 return pinctrl_select_state_full(dev, statename); 326 327 return pinctrl_select_state_simple(dev); 328 } 329 330 int pinctrl_request(struct udevice *dev, int func, int flags) 331 { 332 struct pinctrl_ops *ops = pinctrl_get_ops(dev); 333 334 if (!ops->request) 335 return -ENOSYS; 336 337 return ops->request(dev, func, flags); 338 } 339 340 int pinctrl_request_noflags(struct udevice *dev, int func) 341 { 342 return pinctrl_request(dev, func, 0); 343 } 344 345 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph) 346 { 347 struct pinctrl_ops *ops = pinctrl_get_ops(dev); 348 349 if (!ops->get_periph_id) 350 return -ENOSYS; 351 352 return ops->get_periph_id(dev, periph); 353 } 354 355 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index) 356 { 357 struct pinctrl_ops *ops = pinctrl_get_ops(dev); 358 359 if (!ops->get_gpio_mux) 360 return -ENOSYS; 361 362 return ops->get_gpio_mux(dev, banknum, index); 363 } 364 365 int pinctrl_get_pins_count(struct udevice *dev) 366 { 367 struct pinctrl_ops *ops = pinctrl_get_ops(dev); 368 369 if (!ops->get_pins_count) 370 return -ENOSYS; 371 372 return ops->get_pins_count(dev); 373 } 374 375 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf, 376 int size) 377 { 378 struct pinctrl_ops *ops = pinctrl_get_ops(dev); 379 380 if (!ops->get_pin_name) 381 return -ENOSYS; 382 383 snprintf(buf, size, ops->get_pin_name(dev, selector)); 384 385 return 0; 386 } 387 388 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf, 389 int size) 390 { 391 struct pinctrl_ops *ops = pinctrl_get_ops(dev); 392 393 if (!ops->get_pin_muxing) 394 return -ENOSYS; 395 396 return ops->get_pin_muxing(dev, selector, buf, size); 397 } 398 399 /** 400 * pinconfig_post_bind() - post binding for PINCTRL uclass 401 * Recursively bind child nodes as pinconfig devices in case of full pinctrl. 402 * 403 * @dev: pinctrl device 404 * @return: 0 on success, or negative error code on failure 405 */ 406 static int __maybe_unused pinctrl_post_bind(struct udevice *dev) 407 { 408 const struct pinctrl_ops *ops = pinctrl_get_ops(dev); 409 410 if (!ops) { 411 dev_dbg(dev, "ops is not set. Do not bind.\n"); 412 return -EINVAL; 413 } 414 415 /* 416 * If set_state callback is set, we assume this pinctrl driver is the 417 * full implementation. In this case, its child nodes should be bound 418 * so that peripheral devices can easily search in parent devices 419 * during later DT-parsing. 420 */ 421 if (ops->set_state) 422 return pinconfig_post_bind(dev); 423 424 return 0; 425 } 426 427 UCLASS_DRIVER(pinctrl) = { 428 .id = UCLASS_PINCTRL, 429 .post_bind = pinctrl_post_bind, 430 .flags = DM_UC_FLAG_SEQ_ALIAS, 431 .name = "pinctrl", 432 }; 433