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(PINCONFIG_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 if (!ops || !ops->gpio_request_enable) 246 return -ENOTSUPP; 247 248 return ops->gpio_request_enable(pctldev, pin_selector); 249 } 250 251 /** 252 * pinctrl_gpio_free() - free a single pin used as GPIO 253 * 254 * @dev: GPIO peripheral device 255 * @offset: the GPIO pin offset from the GPIO controller 256 * @return: 0 on success, or negative error code on failure 257 */ 258 int pinctrl_gpio_free(struct udevice *dev, unsigned offset) 259 { 260 const struct pinctrl_ops *ops; 261 struct udevice *pctldev; 262 unsigned int pin_selector; 263 int ret; 264 265 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset, 266 &pctldev, &pin_selector); 267 if (ret) 268 return ret; 269 270 ops = pinctrl_get_ops(pctldev); 271 if (!ops || !ops->gpio_disable_free) 272 return -ENOTSUPP; 273 274 return ops->gpio_disable_free(pctldev, pin_selector); 275 } 276 277 /** 278 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state 279 * 280 * @dev: peripheral device 281 * @return: 0 on success, or negative error code on failure 282 */ 283 static int pinctrl_select_state_simple(struct udevice *dev) 284 { 285 struct udevice *pctldev; 286 struct pinctrl_ops *ops; 287 int ret; 288 289 /* 290 * For most system, there is only one pincontroller device. But in 291 * case of multiple pincontroller devices, probe the one with sequence 292 * number 0 (defined by alias) to avoid race condition. 293 */ 294 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev); 295 if (ret) 296 /* if not found, get the first one */ 297 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev); 298 if (ret) 299 return ret; 300 301 ops = pinctrl_get_ops(pctldev); 302 if (!ops->set_state_simple) { 303 dev_dbg(dev, "set_state_simple op missing\n"); 304 return -ENOSYS; 305 } 306 307 return ops->set_state_simple(pctldev, dev); 308 } 309 310 int pinctrl_select_state(struct udevice *dev, const char *statename) 311 { 312 /* 313 * Some device which is logical like mmc.blk, do not have 314 * a valid ofnode. 315 */ 316 if (!ofnode_valid(dev->node)) 317 return 0; 318 /* 319 * Try full-implemented pinctrl first. 320 * If it fails or is not implemented, try simple one. 321 */ 322 if (CONFIG_IS_ENABLED(PINCTRL_FULL)) 323 return pinctrl_select_state_full(dev, statename); 324 325 return pinctrl_select_state_simple(dev); 326 } 327 328 int pinctrl_request(struct udevice *dev, int func, int flags) 329 { 330 struct pinctrl_ops *ops = pinctrl_get_ops(dev); 331 332 if (!ops->request) 333 return -ENOSYS; 334 335 return ops->request(dev, func, flags); 336 } 337 338 int pinctrl_request_noflags(struct udevice *dev, int func) 339 { 340 return pinctrl_request(dev, func, 0); 341 } 342 343 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph) 344 { 345 struct pinctrl_ops *ops = pinctrl_get_ops(dev); 346 347 if (!ops->get_periph_id) 348 return -ENOSYS; 349 350 return ops->get_periph_id(dev, periph); 351 } 352 353 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index) 354 { 355 struct pinctrl_ops *ops = pinctrl_get_ops(dev); 356 357 if (!ops->get_gpio_mux) 358 return -ENOSYS; 359 360 return ops->get_gpio_mux(dev, banknum, index); 361 } 362 363 int pinctrl_get_pins_count(struct udevice *dev) 364 { 365 struct pinctrl_ops *ops = pinctrl_get_ops(dev); 366 367 if (!ops->get_pins_count) 368 return -ENOSYS; 369 370 return ops->get_pins_count(dev); 371 } 372 373 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf, 374 int size) 375 { 376 struct pinctrl_ops *ops = pinctrl_get_ops(dev); 377 378 if (!ops->get_pin_name) 379 return -ENOSYS; 380 381 snprintf(buf, size, ops->get_pin_name(dev, selector)); 382 383 return 0; 384 } 385 386 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf, 387 int size) 388 { 389 struct pinctrl_ops *ops = pinctrl_get_ops(dev); 390 391 if (!ops->get_pin_muxing) 392 return -ENOSYS; 393 394 return ops->get_pin_muxing(dev, selector, buf, size); 395 } 396 397 /** 398 * pinconfig_post_bind() - post binding for PINCTRL uclass 399 * Recursively bind child nodes as pinconfig devices in case of full pinctrl. 400 * 401 * @dev: pinctrl device 402 * @return: 0 on success, or negative error code on failure 403 */ 404 static int pinctrl_post_bind(struct udevice *dev) 405 { 406 const struct pinctrl_ops *ops = pinctrl_get_ops(dev); 407 408 if (!ops) { 409 dev_dbg(dev, "ops is not set. Do not bind.\n"); 410 return -EINVAL; 411 } 412 413 /* 414 * If set_state callback is set, we assume this pinctrl driver is the 415 * full implementation. In this case, its child nodes should be bound 416 * so that peripheral devices can easily search in parent devices 417 * during later DT-parsing. 418 */ 419 if (ops->set_state) 420 return pinconfig_post_bind(dev); 421 422 return 0; 423 } 424 425 UCLASS_DRIVER(pinctrl) = { 426 .id = UCLASS_PINCTRL, 427 .post_bind = pinctrl_post_bind, 428 .flags = DM_UC_FLAG_SEQ_ALIAS, 429 .name = "pinctrl", 430 }; 431