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