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 | |
Christoph Muellner | 5ff7768 | 2019-01-02 15:09:18 +0100 | [diff] [blame] | 30 | /* |
| 31 | * TODO: this function is temporary for v2019.01. |
| 32 | * It should be renamed to pinctrl_decode_pin_config(), |
| 33 | * the original pinctrl_decode_pin_config() function should |
| 34 | * be removed and all callers of the original function should |
| 35 | * be migrated to use the new one. |
| 36 | */ |
| 37 | int pinctrl_decode_pin_config_dm(struct udevice *dev) |
| 38 | { |
| 39 | int pinconfig = 0; |
| 40 | |
| 41 | if (dev->uclass->uc_drv->id != UCLASS_PINCONFIG) |
| 42 | return -EINVAL; |
| 43 | |
| 44 | if (dev_read_bool(dev, "bias-pull-up")) |
| 45 | pinconfig |= 1 << PIN_CONFIG_BIAS_PULL_UP; |
| 46 | else if (dev_read_bool(dev, "bias-pull-down")) |
| 47 | pinconfig |= 1 << PIN_CONFIG_BIAS_PULL_DOWN; |
| 48 | |
| 49 | return pinconfig; |
| 50 | } |
| 51 | |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 52 | #if CONFIG_IS_ENABLED(PINCTRL_FULL) |
| 53 | /** |
| 54 | * pinctrl_config_one() - apply pinctrl settings for a single node |
| 55 | * |
| 56 | * @config: pin configuration node |
| 57 | * @return: 0 on success, or negative error code on failure |
| 58 | */ |
| 59 | static int pinctrl_config_one(struct udevice *config) |
| 60 | { |
| 61 | struct udevice *pctldev; |
| 62 | const struct pinctrl_ops *ops; |
| 63 | |
| 64 | pctldev = config; |
| 65 | for (;;) { |
| 66 | pctldev = dev_get_parent(pctldev); |
| 67 | if (!pctldev) { |
| 68 | dev_err(config, "could not find pctldev\n"); |
| 69 | return -EINVAL; |
| 70 | } |
| 71 | if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL) |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | ops = pinctrl_get_ops(pctldev); |
| 76 | return ops->set_state(pctldev, config); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * pinctrl_select_state_full() - full implementation of pinctrl_select_state |
| 81 | * |
| 82 | * @dev: peripheral device |
| 83 | * @statename: state name, like "default" |
| 84 | * @return: 0 on success, or negative error code on failure |
| 85 | */ |
| 86 | static int pinctrl_select_state_full(struct udevice *dev, const char *statename) |
| 87 | { |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 88 | char propname[32]; /* long enough */ |
| 89 | const fdt32_t *list; |
| 90 | uint32_t phandle; |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 91 | struct udevice *config; |
| 92 | int state, size, i, ret; |
| 93 | |
Kever Yang | 1e656ad | 2018-02-09 10:56:24 +0800 | [diff] [blame] | 94 | state = dev_read_stringlist_search(dev, "pinctrl-names", statename); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 95 | if (state < 0) { |
| 96 | char *end; |
| 97 | /* |
| 98 | * If statename is not found in "pinctrl-names", |
| 99 | * assume statename is just the integer state ID. |
| 100 | */ |
| 101 | state = simple_strtoul(statename, &end, 10); |
| 102 | if (*end) |
| 103 | return -EINVAL; |
| 104 | } |
| 105 | |
| 106 | snprintf(propname, sizeof(propname), "pinctrl-%d", state); |
Kever Yang | 1e656ad | 2018-02-09 10:56:24 +0800 | [diff] [blame] | 107 | list = dev_read_prop(dev, propname, &size); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 108 | if (!list) |
| 109 | return -EINVAL; |
| 110 | |
| 111 | size /= sizeof(*list); |
| 112 | for (i = 0; i < size; i++) { |
| 113 | phandle = fdt32_to_cpu(*list++); |
Kever Yang | 1e656ad | 2018-02-09 10:56:24 +0800 | [diff] [blame] | 114 | ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle, |
| 115 | &config); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 116 | if (ret) |
| 117 | return ret; |
| 118 | |
| 119 | ret = pinctrl_config_one(config); |
| 120 | if (ret) |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /** |
Masahiro Yamada | f835706 | 2016-08-19 18:26:54 +0900 | [diff] [blame] | 128 | * pinconfig_post_bind() - post binding for PINCONFIG uclass |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 129 | * Recursively bind its children as pinconfig devices. |
| 130 | * |
| 131 | * @dev: pinconfig device |
| 132 | * @return: 0 on success, or negative error code on failure |
| 133 | */ |
| 134 | static int pinconfig_post_bind(struct udevice *dev) |
| 135 | { |
Simon Glass | 5589a81 | 2015-12-29 05:22:52 -0700 | [diff] [blame] | 136 | bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 137 | const char *name; |
Simon Glass | 45a2686 | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 138 | ofnode node; |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 139 | int ret; |
| 140 | |
Simon Glass | 45a2686 | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 141 | dev_for_each_subnode(node, dev) { |
Lukasz Majewski | b6a6238 | 2019-01-09 23:05:02 +0100 | [diff] [blame] | 142 | if (pre_reloc_only && |
| 143 | !ofnode_pre_reloc(node)) |
Simon Glass | 5589a81 | 2015-12-29 05:22:52 -0700 | [diff] [blame] | 144 | continue; |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 145 | /* |
| 146 | * If this node has "compatible" property, this is not |
| 147 | * a pin configuration node, but a normal device. skip. |
| 148 | */ |
Masahiro Yamada | 61e51ba | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 149 | ofnode_get_property(node, "compatible", &ret); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 150 | if (ret >= 0) |
| 151 | continue; |
| 152 | |
| 153 | if (ret != -FDT_ERR_NOTFOUND) |
| 154 | return ret; |
| 155 | |
Simon Glass | 45a2686 | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 156 | name = ofnode_get_name(node); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 157 | if (!name) |
| 158 | return -EINVAL; |
| 159 | ret = device_bind_driver_to_node(dev, "pinconfig", name, |
Simon Glass | 45a2686 | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 160 | node, NULL); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 161 | if (ret) |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | UCLASS_DRIVER(pinconfig) = { |
| 169 | .id = UCLASS_PINCONFIG, |
| 170 | .post_bind = pinconfig_post_bind, |
| 171 | .name = "pinconfig", |
| 172 | }; |
| 173 | |
| 174 | U_BOOT_DRIVER(pinconfig_generic) = { |
| 175 | .name = "pinconfig", |
| 176 | .id = UCLASS_PINCONFIG, |
| 177 | }; |
| 178 | |
| 179 | #else |
| 180 | static int pinctrl_select_state_full(struct udevice *dev, const char *statename) |
| 181 | { |
| 182 | return -ENODEV; |
| 183 | } |
| 184 | |
| 185 | static int pinconfig_post_bind(struct udevice *dev) |
| 186 | { |
Masahiro Yamada | 8a5f612 | 2015-09-06 01:44:50 +0900 | [diff] [blame] | 187 | return 0; |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 188 | } |
| 189 | #endif |
| 190 | |
| 191 | /** |
| 192 | * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state |
| 193 | * |
| 194 | * @dev: peripheral device |
| 195 | * @return: 0 on success, or negative error code on failure |
| 196 | */ |
| 197 | static int pinctrl_select_state_simple(struct udevice *dev) |
| 198 | { |
| 199 | struct udevice *pctldev; |
| 200 | struct pinctrl_ops *ops; |
| 201 | int ret; |
| 202 | |
| 203 | /* |
| 204 | * For simplicity, assume the first device of PINCTRL uclass |
| 205 | * is the correct one. This is most likely OK as there is |
| 206 | * usually only one pinctrl device on the system. |
| 207 | */ |
| 208 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev); |
| 209 | if (ret) |
| 210 | return ret; |
| 211 | |
| 212 | ops = pinctrl_get_ops(pctldev); |
| 213 | if (!ops->set_state_simple) { |
| 214 | dev_dbg(dev, "set_state_simple op missing\n"); |
| 215 | return -ENOSYS; |
| 216 | } |
| 217 | |
| 218 | return ops->set_state_simple(pctldev, dev); |
| 219 | } |
| 220 | |
| 221 | int pinctrl_select_state(struct udevice *dev, const char *statename) |
| 222 | { |
| 223 | /* |
Kever Yang | f717b4c | 2018-04-18 17:54:04 +0800 | [diff] [blame] | 224 | * Some device which is logical like mmc.blk, do not have |
| 225 | * a valid ofnode. |
| 226 | */ |
| 227 | if (!ofnode_valid(dev->node)) |
| 228 | return 0; |
| 229 | /* |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 230 | * Try full-implemented pinctrl first. |
| 231 | * If it fails or is not implemented, try simple one. |
| 232 | */ |
| 233 | if (pinctrl_select_state_full(dev, statename)) |
| 234 | return pinctrl_select_state_simple(dev); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Simon Glass | c5acf4a | 2015-08-30 16:55:13 -0600 | [diff] [blame] | 239 | int pinctrl_request(struct udevice *dev, int func, int flags) |
| 240 | { |
| 241 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 242 | |
| 243 | if (!ops->request) |
| 244 | return -ENOSYS; |
| 245 | |
| 246 | return ops->request(dev, func, flags); |
| 247 | } |
| 248 | |
| 249 | int pinctrl_request_noflags(struct udevice *dev, int func) |
| 250 | { |
| 251 | return pinctrl_request(dev, func, 0); |
| 252 | } |
| 253 | |
| 254 | int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph) |
| 255 | { |
| 256 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 257 | |
| 258 | if (!ops->get_periph_id) |
| 259 | return -ENOSYS; |
| 260 | |
| 261 | return ops->get_periph_id(dev, periph); |
| 262 | } |
| 263 | |
Simon Glass | 77eaa19 | 2016-01-21 19:43:56 -0700 | [diff] [blame] | 264 | int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index) |
| 265 | { |
| 266 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 267 | |
| 268 | if (!ops->get_gpio_mux) |
| 269 | return -ENOSYS; |
| 270 | |
| 271 | return ops->get_gpio_mux(dev, banknum, index); |
| 272 | } |
| 273 | |
Patrice Chotard | 8bbb5b2 | 2018-10-24 14:10:14 +0200 | [diff] [blame] | 274 | int pinctrl_get_pins_count(struct udevice *dev) |
| 275 | { |
| 276 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 277 | |
| 278 | if (!ops->get_pins_count) |
| 279 | return -ENOSYS; |
| 280 | |
| 281 | return ops->get_pins_count(dev); |
| 282 | } |
| 283 | |
| 284 | int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf, |
| 285 | int size) |
| 286 | { |
| 287 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 288 | |
| 289 | if (!ops->get_pin_name) |
| 290 | return -ENOSYS; |
| 291 | |
| 292 | snprintf(buf, size, ops->get_pin_name(dev, selector)); |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
Patrice Chotard | f55a0c0 | 2018-10-24 14:10:13 +0200 | [diff] [blame] | 297 | int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf, |
| 298 | int size) |
| 299 | { |
| 300 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 301 | |
| 302 | if (!ops->get_pin_muxing) |
| 303 | return -ENOSYS; |
| 304 | |
| 305 | return ops->get_pin_muxing(dev, selector, buf, size); |
| 306 | } |
| 307 | |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 308 | /** |
Masahiro Yamada | f835706 | 2016-08-19 18:26:54 +0900 | [diff] [blame] | 309 | * pinconfig_post_bind() - post binding for PINCTRL uclass |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 310 | * Recursively bind child nodes as pinconfig devices in case of full pinctrl. |
| 311 | * |
| 312 | * @dev: pinctrl device |
| 313 | * @return: 0 on success, or negative error code on failure |
| 314 | */ |
| 315 | static int pinctrl_post_bind(struct udevice *dev) |
| 316 | { |
| 317 | const struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 318 | |
| 319 | if (!ops) { |
| 320 | dev_dbg(dev, "ops is not set. Do not bind.\n"); |
| 321 | return -EINVAL; |
| 322 | } |
| 323 | |
| 324 | /* |
Masahiro Yamada | 8a5f612 | 2015-09-06 01:44:50 +0900 | [diff] [blame] | 325 | * If set_state callback is set, we assume this pinctrl driver is the |
| 326 | * full implementation. In this case, its child nodes should be bound |
| 327 | * so that peripheral devices can easily search in parent devices |
| 328 | * during later DT-parsing. |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 329 | */ |
Masahiro Yamada | 8a5f612 | 2015-09-06 01:44:50 +0900 | [diff] [blame] | 330 | if (ops->set_state) |
| 331 | return pinconfig_post_bind(dev); |
| 332 | |
| 333 | return 0; |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | UCLASS_DRIVER(pinctrl) = { |
| 337 | .id = UCLASS_PINCTRL, |
| 338 | .post_bind = pinctrl_post_bind, |
Thomas Abraham | ac98527 | 2016-04-23 22:18:07 +0530 | [diff] [blame] | 339 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 340 | .name = "pinctrl", |
| 341 | }; |