Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 8 | #include <linux/libfdt.h> |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 9 | #include <linux/err.h> |
| 10 | #include <linux/list.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 12 | #include <dm/lists.h> |
| 13 | #include <dm/pinctrl.h> |
Heiko Stübner | 9a2cdca | 2017-02-18 19:46:21 +0100 | [diff] [blame] | 14 | #include <dm/util.h> |
Kever Yang | a589762 | 2018-02-09 10:56:24 +0800 | [diff] [blame] | 15 | #include <dm/of_access.h> |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Simon Glass | a098447 | 2016-01-21 19:43:26 -0700 | [diff] [blame] | 19 | int pinctrl_decode_pin_config(const void *blob, int node) |
| 20 | { |
| 21 | int flags = 0; |
| 22 | |
| 23 | if (fdtdec_get_bool(blob, node, "bias-pull-up")) |
| 24 | flags |= 1 << PIN_CONFIG_BIAS_PULL_UP; |
| 25 | else if (fdtdec_get_bool(blob, node, "bias-pull-down")) |
| 26 | flags |= 1 << PIN_CONFIG_BIAS_PULL_DOWN; |
| 27 | |
| 28 | return flags; |
| 29 | } |
| 30 | |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 31 | #if CONFIG_IS_ENABLED(PINCTRL_FULL) |
| 32 | /** |
| 33 | * pinctrl_config_one() - apply pinctrl settings for a single node |
| 34 | * |
| 35 | * @config: pin configuration node |
| 36 | * @return: 0 on success, or negative error code on failure |
| 37 | */ |
| 38 | static int pinctrl_config_one(struct udevice *config) |
| 39 | { |
| 40 | struct udevice *pctldev; |
| 41 | const struct pinctrl_ops *ops; |
| 42 | |
| 43 | pctldev = config; |
| 44 | for (;;) { |
| 45 | pctldev = dev_get_parent(pctldev); |
| 46 | if (!pctldev) { |
| 47 | dev_err(config, "could not find pctldev\n"); |
| 48 | return -EINVAL; |
| 49 | } |
| 50 | if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL) |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | ops = pinctrl_get_ops(pctldev); |
| 55 | return ops->set_state(pctldev, config); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * pinctrl_select_state_full() - full implementation of pinctrl_select_state |
| 60 | * |
| 61 | * @dev: peripheral device |
| 62 | * @statename: state name, like "default" |
| 63 | * @return: 0 on success, or negative error code on failure |
| 64 | */ |
| 65 | static int pinctrl_select_state_full(struct udevice *dev, const char *statename) |
| 66 | { |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 67 | char propname[32]; /* long enough */ |
| 68 | const fdt32_t *list; |
| 69 | uint32_t phandle; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 70 | struct udevice *config; |
| 71 | int state, size, i, ret; |
| 72 | |
Kever Yang | a589762 | 2018-02-09 10:56:24 +0800 | [diff] [blame] | 73 | state = dev_read_stringlist_search(dev, "pinctrl-names", statename); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 74 | if (state < 0) { |
| 75 | char *end; |
| 76 | /* |
| 77 | * If statename is not found in "pinctrl-names", |
| 78 | * assume statename is just the integer state ID. |
| 79 | */ |
| 80 | state = simple_strtoul(statename, &end, 10); |
| 81 | if (*end) |
| 82 | return -EINVAL; |
| 83 | } |
| 84 | |
| 85 | snprintf(propname, sizeof(propname), "pinctrl-%d", state); |
Kever Yang | a589762 | 2018-02-09 10:56:24 +0800 | [diff] [blame] | 86 | list = dev_read_prop(dev, propname, &size); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 87 | if (!list) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | size /= sizeof(*list); |
| 91 | for (i = 0; i < size; i++) { |
| 92 | phandle = fdt32_to_cpu(*list++); |
Kever Yang | a589762 | 2018-02-09 10:56:24 +0800 | [diff] [blame] | 93 | ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle, |
| 94 | &config); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 95 | if (ret) |
| 96 | return ret; |
| 97 | |
| 98 | ret = pinctrl_config_one(config); |
| 99 | if (ret) |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /** |
Masahiro Yamada | 0cbfe5c | 2016-08-19 18:26:54 +0900 | [diff] [blame] | 107 | * pinconfig_post_bind() - post binding for PINCONFIG uclass |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 108 | * Recursively bind its children as pinconfig devices. |
| 109 | * |
| 110 | * @dev: pinconfig device |
| 111 | * @return: 0 on success, or negative error code on failure |
| 112 | */ |
| 113 | static int pinconfig_post_bind(struct udevice *dev) |
| 114 | { |
Simon Glass | 9cefa47 | 2015-12-29 05:22:52 -0700 | [diff] [blame] | 115 | bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 116 | const char *name; |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 117 | ofnode node; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 118 | int ret; |
| 119 | |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 120 | dev_for_each_subnode(node, dev) { |
Simon Glass | 9cefa47 | 2015-12-29 05:22:52 -0700 | [diff] [blame] | 121 | if (pre_reloc_only && |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 122 | !ofnode_pre_reloc(node)) |
Simon Glass | 9cefa47 | 2015-12-29 05:22:52 -0700 | [diff] [blame] | 123 | continue; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 124 | /* |
| 125 | * If this node has "compatible" property, this is not |
| 126 | * a pin configuration node, but a normal device. skip. |
| 127 | */ |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 128 | ofnode_get_property(node, "compatible", &ret); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 129 | if (ret >= 0) |
| 130 | continue; |
| 131 | |
| 132 | if (ret != -FDT_ERR_NOTFOUND) |
| 133 | return ret; |
| 134 | |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 135 | name = ofnode_get_name(node); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 136 | if (!name) |
| 137 | return -EINVAL; |
| 138 | ret = device_bind_driver_to_node(dev, "pinconfig", name, |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 139 | node, NULL); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 140 | if (ret) |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | UCLASS_DRIVER(pinconfig) = { |
| 148 | .id = UCLASS_PINCONFIG, |
| 149 | .post_bind = pinconfig_post_bind, |
| 150 | .name = "pinconfig", |
| 151 | }; |
| 152 | |
| 153 | U_BOOT_DRIVER(pinconfig_generic) = { |
| 154 | .name = "pinconfig", |
| 155 | .id = UCLASS_PINCONFIG, |
| 156 | }; |
| 157 | |
| 158 | #else |
| 159 | static int pinctrl_select_state_full(struct udevice *dev, const char *statename) |
| 160 | { |
| 161 | return -ENODEV; |
| 162 | } |
| 163 | |
| 164 | static int pinconfig_post_bind(struct udevice *dev) |
| 165 | { |
Masahiro Yamada | 7778b03 | 2015-09-06 01:44:50 +0900 | [diff] [blame] | 166 | return 0; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 167 | } |
| 168 | #endif |
| 169 | |
| 170 | /** |
| 171 | * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state |
| 172 | * |
| 173 | * @dev: peripheral device |
| 174 | * @return: 0 on success, or negative error code on failure |
| 175 | */ |
| 176 | static int pinctrl_select_state_simple(struct udevice *dev) |
| 177 | { |
| 178 | struct udevice *pctldev; |
| 179 | struct pinctrl_ops *ops; |
| 180 | int ret; |
| 181 | |
| 182 | /* |
| 183 | * For simplicity, assume the first device of PINCTRL uclass |
| 184 | * is the correct one. This is most likely OK as there is |
| 185 | * usually only one pinctrl device on the system. |
| 186 | */ |
| 187 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev); |
| 188 | if (ret) |
| 189 | return ret; |
| 190 | |
| 191 | ops = pinctrl_get_ops(pctldev); |
| 192 | if (!ops->set_state_simple) { |
| 193 | dev_dbg(dev, "set_state_simple op missing\n"); |
| 194 | return -ENOSYS; |
| 195 | } |
| 196 | |
| 197 | return ops->set_state_simple(pctldev, dev); |
| 198 | } |
| 199 | |
| 200 | int pinctrl_select_state(struct udevice *dev, const char *statename) |
| 201 | { |
| 202 | /* |
| 203 | * Try full-implemented pinctrl first. |
| 204 | * If it fails or is not implemented, try simple one. |
| 205 | */ |
| 206 | if (pinctrl_select_state_full(dev, statename)) |
| 207 | return pinctrl_select_state_simple(dev); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Simon Glass | ac2ee65 | 2015-08-30 16:55:13 -0600 | [diff] [blame] | 212 | int pinctrl_request(struct udevice *dev, int func, int flags) |
| 213 | { |
| 214 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 215 | |
| 216 | if (!ops->request) |
| 217 | return -ENOSYS; |
| 218 | |
| 219 | return ops->request(dev, func, flags); |
| 220 | } |
| 221 | |
| 222 | int pinctrl_request_noflags(struct udevice *dev, int func) |
| 223 | { |
| 224 | return pinctrl_request(dev, func, 0); |
| 225 | } |
| 226 | |
| 227 | int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph) |
| 228 | { |
| 229 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 230 | |
| 231 | if (!ops->get_periph_id) |
| 232 | return -ENOSYS; |
| 233 | |
| 234 | return ops->get_periph_id(dev, periph); |
| 235 | } |
| 236 | |
Simon Glass | 2d4fa3c | 2016-01-21 19:43:56 -0700 | [diff] [blame] | 237 | int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index) |
| 238 | { |
| 239 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 240 | |
| 241 | if (!ops->get_gpio_mux) |
| 242 | return -ENOSYS; |
| 243 | |
| 244 | return ops->get_gpio_mux(dev, banknum, index); |
| 245 | } |
| 246 | |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 247 | /** |
Masahiro Yamada | 0cbfe5c | 2016-08-19 18:26:54 +0900 | [diff] [blame] | 248 | * pinconfig_post_bind() - post binding for PINCTRL uclass |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 249 | * Recursively bind child nodes as pinconfig devices in case of full pinctrl. |
| 250 | * |
| 251 | * @dev: pinctrl device |
| 252 | * @return: 0 on success, or negative error code on failure |
| 253 | */ |
| 254 | static int pinctrl_post_bind(struct udevice *dev) |
| 255 | { |
| 256 | const struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 257 | |
| 258 | if (!ops) { |
| 259 | dev_dbg(dev, "ops is not set. Do not bind.\n"); |
| 260 | return -EINVAL; |
| 261 | } |
| 262 | |
| 263 | /* |
Masahiro Yamada | 7778b03 | 2015-09-06 01:44:50 +0900 | [diff] [blame] | 264 | * If set_state callback is set, we assume this pinctrl driver is the |
| 265 | * full implementation. In this case, its child nodes should be bound |
| 266 | * so that peripheral devices can easily search in parent devices |
| 267 | * during later DT-parsing. |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 268 | */ |
Masahiro Yamada | 7778b03 | 2015-09-06 01:44:50 +0900 | [diff] [blame] | 269 | if (ops->set_state) |
| 270 | return pinconfig_post_bind(dev); |
| 271 | |
| 272 | return 0; |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | UCLASS_DRIVER(pinctrl) = { |
| 276 | .id = UCLASS_PINCTRL, |
| 277 | .post_bind = pinctrl_post_bind, |
Thomas Abraham | 87adc64 | 2016-04-23 22:18:07 +0530 | [diff] [blame] | 278 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 279 | .name = "pinctrl", |
| 280 | }; |