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