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 | |
Simon Glass | a098447 | 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 | f8efa63 | 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 | f8efa63 | 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 | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 69 | struct udevice *config; |
| 70 | int state, size, i, ret; |
| 71 | |
Kever Yang | a589762 | 2018-02-09 10:56:24 +0800 | [diff] [blame] | 72 | state = dev_read_stringlist_search(dev, "pinctrl-names", statename); |
Masahiro Yamada | f8efa63 | 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 | a589762 | 2018-02-09 10:56:24 +0800 | [diff] [blame] | 85 | list = dev_read_prop(dev, propname, &size); |
Masahiro Yamada | f8efa63 | 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 | a589762 | 2018-02-09 10:56:24 +0800 | [diff] [blame] | 92 | ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle, |
| 93 | &config); |
Masahiro Yamada | f8efa63 | 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 | 0cbfe5c | 2016-08-19 18:26:54 +0900 | [diff] [blame] | 106 | * pinconfig_post_bind() - post binding for PINCONFIG uclass |
Masahiro Yamada | f8efa63 | 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 | 9cefa47 | 2015-12-29 05:22:52 -0700 | [diff] [blame] | 114 | bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 115 | const char *name; |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 116 | ofnode node; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 117 | int ret; |
| 118 | |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 119 | dev_for_each_subnode(node, dev) { |
Simon Glass | 9cefa47 | 2015-12-29 05:22:52 -0700 | [diff] [blame] | 120 | if (pre_reloc_only && |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 121 | !ofnode_pre_reloc(node)) |
Simon Glass | 9cefa47 | 2015-12-29 05:22:52 -0700 | [diff] [blame] | 122 | continue; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 123 | /* |
| 124 | * If this node has "compatible" property, this is not |
| 125 | * a pin configuration node, but a normal device. skip. |
| 126 | */ |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 127 | ofnode_get_property(node, "compatible", &ret); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 128 | if (ret >= 0) |
| 129 | continue; |
| 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, |
| 148 | .post_bind = pinconfig_post_bind, |
| 149 | .name = "pinconfig", |
| 150 | }; |
| 151 | |
| 152 | U_BOOT_DRIVER(pinconfig_generic) = { |
| 153 | .name = "pinconfig", |
| 154 | .id = UCLASS_PINCONFIG, |
| 155 | }; |
| 156 | |
| 157 | #else |
| 158 | static int pinctrl_select_state_full(struct udevice *dev, const char *statename) |
| 159 | { |
| 160 | return -ENODEV; |
| 161 | } |
| 162 | |
| 163 | static int pinconfig_post_bind(struct udevice *dev) |
| 164 | { |
Masahiro Yamada | 7778b03 | 2015-09-06 01:44:50 +0900 | [diff] [blame] | 165 | return 0; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 166 | } |
| 167 | #endif |
| 168 | |
| 169 | /** |
| 170 | * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state |
| 171 | * |
| 172 | * @dev: peripheral device |
| 173 | * @return: 0 on success, or negative error code on failure |
| 174 | */ |
| 175 | static int pinctrl_select_state_simple(struct udevice *dev) |
| 176 | { |
| 177 | struct udevice *pctldev; |
| 178 | struct pinctrl_ops *ops; |
| 179 | int ret; |
| 180 | |
| 181 | /* |
| 182 | * For simplicity, assume the first device of PINCTRL uclass |
| 183 | * is the correct one. This is most likely OK as there is |
| 184 | * usually only one pinctrl device on the system. |
| 185 | */ |
| 186 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev); |
| 187 | if (ret) |
| 188 | return ret; |
| 189 | |
| 190 | ops = pinctrl_get_ops(pctldev); |
| 191 | if (!ops->set_state_simple) { |
| 192 | dev_dbg(dev, "set_state_simple op missing\n"); |
| 193 | return -ENOSYS; |
| 194 | } |
| 195 | |
| 196 | return ops->set_state_simple(pctldev, dev); |
| 197 | } |
| 198 | |
| 199 | int pinctrl_select_state(struct udevice *dev, const char *statename) |
| 200 | { |
| 201 | /* |
Kever Yang | 16ed971 | 2018-04-18 17:54:04 +0800 | [diff] [blame^] | 202 | * Some device which is logical like mmc.blk, do not have |
| 203 | * a valid ofnode. |
| 204 | */ |
| 205 | if (!ofnode_valid(dev->node)) |
| 206 | return 0; |
| 207 | /* |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 208 | * Try full-implemented pinctrl first. |
| 209 | * If it fails or is not implemented, try simple one. |
| 210 | */ |
| 211 | if (pinctrl_select_state_full(dev, statename)) |
| 212 | return pinctrl_select_state_simple(dev); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Simon Glass | ac2ee65 | 2015-08-30 16:55:13 -0600 | [diff] [blame] | 217 | int pinctrl_request(struct udevice *dev, int func, int flags) |
| 218 | { |
| 219 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 220 | |
| 221 | if (!ops->request) |
| 222 | return -ENOSYS; |
| 223 | |
| 224 | return ops->request(dev, func, flags); |
| 225 | } |
| 226 | |
| 227 | int pinctrl_request_noflags(struct udevice *dev, int func) |
| 228 | { |
| 229 | return pinctrl_request(dev, func, 0); |
| 230 | } |
| 231 | |
| 232 | int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph) |
| 233 | { |
| 234 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 235 | |
| 236 | if (!ops->get_periph_id) |
| 237 | return -ENOSYS; |
| 238 | |
| 239 | return ops->get_periph_id(dev, periph); |
| 240 | } |
| 241 | |
Simon Glass | 2d4fa3c | 2016-01-21 19:43:56 -0700 | [diff] [blame] | 242 | int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index) |
| 243 | { |
| 244 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 245 | |
| 246 | if (!ops->get_gpio_mux) |
| 247 | return -ENOSYS; |
| 248 | |
| 249 | return ops->get_gpio_mux(dev, banknum, index); |
| 250 | } |
| 251 | |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 252 | /** |
Masahiro Yamada | 0cbfe5c | 2016-08-19 18:26:54 +0900 | [diff] [blame] | 253 | * pinconfig_post_bind() - post binding for PINCTRL uclass |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 254 | * Recursively bind child nodes as pinconfig devices in case of full pinctrl. |
| 255 | * |
| 256 | * @dev: pinctrl device |
| 257 | * @return: 0 on success, or negative error code on failure |
| 258 | */ |
| 259 | static int pinctrl_post_bind(struct udevice *dev) |
| 260 | { |
| 261 | const struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 262 | |
| 263 | if (!ops) { |
| 264 | dev_dbg(dev, "ops is not set. Do not bind.\n"); |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
| 268 | /* |
Masahiro Yamada | 7778b03 | 2015-09-06 01:44:50 +0900 | [diff] [blame] | 269 | * If set_state callback is set, we assume this pinctrl driver is the |
| 270 | * full implementation. In this case, its child nodes should be bound |
| 271 | * so that peripheral devices can easily search in parent devices |
| 272 | * during later DT-parsing. |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 273 | */ |
Masahiro Yamada | 7778b03 | 2015-09-06 01:44:50 +0900 | [diff] [blame] | 274 | if (ops->set_state) |
| 275 | return pinconfig_post_bind(dev); |
| 276 | |
| 277 | return 0; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | UCLASS_DRIVER(pinctrl) = { |
| 281 | .id = UCLASS_PINCTRL, |
| 282 | .post_bind = pinctrl_post_bind, |
Thomas Abraham | 87adc64 | 2016-04-23 22:18:07 +0530 | [diff] [blame] | 283 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 284 | .name = "pinctrl", |
| 285 | }; |