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