Masahiro Yamada | d90a5a3 | 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> |
| 8 | #include <libfdt.h> |
| 9 | #include <linux/err.h> |
| 10 | #include <linux/list.h> |
Simon Glass | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 12 | #include <dm/lists.h> |
| 13 | #include <dm/pinctrl.h> |
Heiko Stübner | 27326c7 | 2017-02-18 19:46:21 +0100 | [diff] [blame] | 14 | #include <dm/util.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 | |
Masahiro Yamada | d90a5a3 | 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 | { |
| 66 | const void *fdt = gd->fdt_blob; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 67 | int node = dev_of_offset(dev); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 68 | char propname[32]; /* long enough */ |
| 69 | const fdt32_t *list; |
| 70 | uint32_t phandle; |
| 71 | int config_node; |
| 72 | struct udevice *config; |
| 73 | int state, size, i, ret; |
| 74 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 75 | state = fdt_stringlist_search(fdt, node, "pinctrl-names", statename); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 76 | if (state < 0) { |
| 77 | char *end; |
| 78 | /* |
| 79 | * If statename is not found in "pinctrl-names", |
| 80 | * assume statename is just the integer state ID. |
| 81 | */ |
| 82 | state = simple_strtoul(statename, &end, 10); |
| 83 | if (*end) |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | |
| 87 | snprintf(propname, sizeof(propname), "pinctrl-%d", state); |
| 88 | list = fdt_getprop(fdt, node, propname, &size); |
| 89 | if (!list) |
| 90 | return -EINVAL; |
| 91 | |
| 92 | size /= sizeof(*list); |
| 93 | for (i = 0; i < size; i++) { |
| 94 | phandle = fdt32_to_cpu(*list++); |
| 95 | |
| 96 | config_node = fdt_node_offset_by_phandle(fdt, phandle); |
| 97 | if (config_node < 0) { |
| 98 | dev_err(dev, "prop %s index %d invalid phandle\n", |
| 99 | propname, i); |
| 100 | return -EINVAL; |
| 101 | } |
| 102 | ret = uclass_get_device_by_of_offset(UCLASS_PINCONFIG, |
| 103 | config_node, &config); |
| 104 | if (ret) |
| 105 | return ret; |
| 106 | |
| 107 | ret = pinctrl_config_one(config); |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /** |
Masahiro Yamada | f835706 | 2016-08-19 18:26:54 +0900 | [diff] [blame] | 116 | * pinconfig_post_bind() - post binding for PINCONFIG uclass |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 117 | * Recursively bind its children as pinconfig devices. |
| 118 | * |
| 119 | * @dev: pinconfig device |
| 120 | * @return: 0 on success, or negative error code on failure |
| 121 | */ |
| 122 | static int pinconfig_post_bind(struct udevice *dev) |
| 123 | { |
Simon Glass | 5589a81 | 2015-12-29 05:22:52 -0700 | [diff] [blame] | 124 | bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 125 | const char *name; |
Simon Glass | 45a2686 | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 126 | ofnode node; |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 127 | int ret; |
| 128 | |
Simon Glass | 45a2686 | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 129 | dev_for_each_subnode(node, dev) { |
Simon Glass | 5589a81 | 2015-12-29 05:22:52 -0700 | [diff] [blame] | 130 | if (pre_reloc_only && |
Simon Glass | 45a2686 | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 131 | !ofnode_pre_reloc(node)) |
Simon Glass | 5589a81 | 2015-12-29 05:22:52 -0700 | [diff] [blame] | 132 | continue; |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 133 | /* |
| 134 | * If this node has "compatible" property, this is not |
| 135 | * a pin configuration node, but a normal device. skip. |
| 136 | */ |
Masahiro Yamada | 61e51ba | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 137 | ofnode_get_property(node, "compatible", &ret); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 138 | if (ret >= 0) |
| 139 | continue; |
| 140 | |
| 141 | if (ret != -FDT_ERR_NOTFOUND) |
| 142 | return ret; |
| 143 | |
Simon Glass | 45a2686 | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 144 | name = ofnode_get_name(node); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 145 | if (!name) |
| 146 | return -EINVAL; |
| 147 | ret = device_bind_driver_to_node(dev, "pinconfig", name, |
Simon Glass | 45a2686 | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 148 | node, NULL); |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 149 | if (ret) |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | UCLASS_DRIVER(pinconfig) = { |
| 157 | .id = UCLASS_PINCONFIG, |
| 158 | .post_bind = pinconfig_post_bind, |
| 159 | .name = "pinconfig", |
| 160 | }; |
| 161 | |
| 162 | U_BOOT_DRIVER(pinconfig_generic) = { |
| 163 | .name = "pinconfig", |
| 164 | .id = UCLASS_PINCONFIG, |
| 165 | }; |
| 166 | |
| 167 | #else |
| 168 | static int pinctrl_select_state_full(struct udevice *dev, const char *statename) |
| 169 | { |
| 170 | return -ENODEV; |
| 171 | } |
| 172 | |
| 173 | static int pinconfig_post_bind(struct udevice *dev) |
| 174 | { |
Masahiro Yamada | 8a5f612 | 2015-09-06 01:44:50 +0900 | [diff] [blame] | 175 | return 0; |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 176 | } |
| 177 | #endif |
| 178 | |
| 179 | /** |
| 180 | * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state |
| 181 | * |
| 182 | * @dev: peripheral device |
| 183 | * @return: 0 on success, or negative error code on failure |
| 184 | */ |
| 185 | static int pinctrl_select_state_simple(struct udevice *dev) |
| 186 | { |
| 187 | struct udevice *pctldev; |
| 188 | struct pinctrl_ops *ops; |
| 189 | int ret; |
| 190 | |
| 191 | /* |
| 192 | * For simplicity, assume the first device of PINCTRL uclass |
| 193 | * is the correct one. This is most likely OK as there is |
| 194 | * usually only one pinctrl device on the system. |
| 195 | */ |
| 196 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev); |
| 197 | if (ret) |
| 198 | return ret; |
| 199 | |
| 200 | ops = pinctrl_get_ops(pctldev); |
| 201 | if (!ops->set_state_simple) { |
| 202 | dev_dbg(dev, "set_state_simple op missing\n"); |
| 203 | return -ENOSYS; |
| 204 | } |
| 205 | |
| 206 | return ops->set_state_simple(pctldev, dev); |
| 207 | } |
| 208 | |
| 209 | int pinctrl_select_state(struct udevice *dev, const char *statename) |
| 210 | { |
| 211 | /* |
| 212 | * Try full-implemented pinctrl first. |
| 213 | * If it fails or is not implemented, try simple one. |
| 214 | */ |
| 215 | if (pinctrl_select_state_full(dev, statename)) |
| 216 | return pinctrl_select_state_simple(dev); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
Simon Glass | c5acf4a | 2015-08-30 16:55:13 -0600 | [diff] [blame] | 221 | int pinctrl_request(struct udevice *dev, int func, int flags) |
| 222 | { |
| 223 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 224 | |
| 225 | if (!ops->request) |
| 226 | return -ENOSYS; |
| 227 | |
| 228 | return ops->request(dev, func, flags); |
| 229 | } |
| 230 | |
| 231 | int pinctrl_request_noflags(struct udevice *dev, int func) |
| 232 | { |
| 233 | return pinctrl_request(dev, func, 0); |
| 234 | } |
| 235 | |
| 236 | int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph) |
| 237 | { |
| 238 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 239 | |
| 240 | if (!ops->get_periph_id) |
| 241 | return -ENOSYS; |
| 242 | |
| 243 | return ops->get_periph_id(dev, periph); |
| 244 | } |
| 245 | |
Simon Glass | 77eaa19 | 2016-01-21 19:43:56 -0700 | [diff] [blame] | 246 | int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index) |
| 247 | { |
| 248 | struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 249 | |
| 250 | if (!ops->get_gpio_mux) |
| 251 | return -ENOSYS; |
| 252 | |
| 253 | return ops->get_gpio_mux(dev, banknum, index); |
| 254 | } |
| 255 | |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 256 | /** |
Masahiro Yamada | f835706 | 2016-08-19 18:26:54 +0900 | [diff] [blame] | 257 | * pinconfig_post_bind() - post binding for PINCTRL uclass |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 258 | * Recursively bind child nodes as pinconfig devices in case of full pinctrl. |
| 259 | * |
| 260 | * @dev: pinctrl device |
| 261 | * @return: 0 on success, or negative error code on failure |
| 262 | */ |
| 263 | static int pinctrl_post_bind(struct udevice *dev) |
| 264 | { |
| 265 | const struct pinctrl_ops *ops = pinctrl_get_ops(dev); |
| 266 | |
| 267 | if (!ops) { |
| 268 | dev_dbg(dev, "ops is not set. Do not bind.\n"); |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | |
| 272 | /* |
Masahiro Yamada | 8a5f612 | 2015-09-06 01:44:50 +0900 | [diff] [blame] | 273 | * If set_state callback is set, we assume this pinctrl driver is the |
| 274 | * full implementation. In this case, its child nodes should be bound |
| 275 | * so that peripheral devices can easily search in parent devices |
| 276 | * during later DT-parsing. |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 277 | */ |
Masahiro Yamada | 8a5f612 | 2015-09-06 01:44:50 +0900 | [diff] [blame] | 278 | if (ops->set_state) |
| 279 | return pinconfig_post_bind(dev); |
| 280 | |
| 281 | return 0; |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | UCLASS_DRIVER(pinctrl) = { |
| 285 | .id = UCLASS_PINCTRL, |
| 286 | .post_bind = pinctrl_post_bind, |
Thomas Abraham | ac98527 | 2016-04-23 22:18:07 +0530 | [diff] [blame] | 287 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Masahiro Yamada | d90a5a3 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 288 | .name = "pinctrl", |
| 289 | }; |