Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 8 | #include <dm/device-internal.h> |
| 9 | #include <dm/lists.h> |
| 10 | #include <dm/uclass-internal.h> |
Eric Nelson | 786e98d | 2016-04-24 16:32:40 -0700 | [diff] [blame] | 11 | #include <dt-bindings/gpio/gpio.h> |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 12 | #include <errno.h> |
Simon Glass | d3322bb | 2015-01-05 20:05:28 -0700 | [diff] [blame] | 13 | #include <fdtdec.h> |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 15 | #include <asm/gpio.h> |
Masahiro Yamada | 78eeb91 | 2016-01-24 23:27:48 +0900 | [diff] [blame] | 16 | #include <linux/bug.h> |
Simon Glass | 43b2e1a | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 17 | #include <linux/ctype.h> |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 18 | |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 21 | /** |
Patrick Delaunay | 758bba9 | 2020-01-13 11:35:01 +0100 | [diff] [blame] | 22 | * gpio_desc_init() - Initialize the GPIO descriptor |
| 23 | * |
| 24 | * @desc: GPIO descriptor to initialize |
| 25 | * @dev: GPIO device |
| 26 | * @offset: Offset of device GPIO |
| 27 | */ |
| 28 | static void gpio_desc_init(struct gpio_desc *desc, |
| 29 | struct udevice *dev, |
| 30 | uint offset) |
| 31 | { |
| 32 | desc->dev = dev; |
| 33 | desc->offset = offset; |
| 34 | desc->flags = 0; |
| 35 | } |
| 36 | |
| 37 | /** |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 38 | * gpio_to_device() - Convert global GPIO number to device, number |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 39 | * |
| 40 | * Convert the GPIO number to an entry in the list of GPIOs |
| 41 | * or GPIO blocks registered with the GPIO controller. Returns |
| 42 | * entry on success, NULL on error. |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 43 | * |
| 44 | * @gpio: The numeric representation of the GPIO |
| 45 | * @desc: Returns description (desc->flags will always be 0) |
| 46 | * @return 0 if found, -ENOENT if not found |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 47 | */ |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 48 | static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc) |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 49 | { |
| 50 | struct gpio_dev_priv *uc_priv; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 51 | struct udevice *dev; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 52 | int ret; |
| 53 | |
| 54 | for (ret = uclass_first_device(UCLASS_GPIO, &dev); |
| 55 | dev; |
| 56 | ret = uclass_next_device(&dev)) { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 57 | uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 58 | if (gpio >= uc_priv->gpio_base && |
| 59 | gpio < uc_priv->gpio_base + uc_priv->gpio_count) { |
Patrick Delaunay | 758bba9 | 2020-01-13 11:35:01 +0100 | [diff] [blame] | 60 | gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /* No such GPIO */ |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 66 | return ret ? ret : -ENOENT; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Simon Glass | 215bcc7 | 2015-06-23 15:38:40 -0600 | [diff] [blame] | 69 | int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc) |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 70 | { |
Simon Glass | 43b2e1a | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 71 | struct gpio_dev_priv *uc_priv = NULL; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 72 | struct udevice *dev; |
Simon Glass | 43b2e1a | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 73 | ulong offset; |
| 74 | int numeric; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 75 | int ret; |
| 76 | |
Simon Glass | 43b2e1a | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 77 | numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 78 | for (ret = uclass_first_device(UCLASS_GPIO, &dev); |
| 79 | dev; |
| 80 | ret = uclass_next_device(&dev)) { |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 81 | int len; |
| 82 | |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 83 | uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 43b2e1a | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 84 | if (numeric != -1) { |
| 85 | offset = numeric - uc_priv->gpio_base; |
| 86 | /* Allow GPIOs to be numbered from 0 */ |
Tom Rini | 26fd968 | 2017-05-10 15:20:15 -0400 | [diff] [blame] | 87 | if (offset < uc_priv->gpio_count) |
Simon Glass | 43b2e1a | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 91 | len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0; |
| 92 | |
Simon Glass | d4acf63 | 2014-06-11 23:29:47 -0600 | [diff] [blame] | 93 | if (!strncasecmp(name, uc_priv->bank_name, len)) { |
Simon Glass | 43b2e1a | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 94 | if (!strict_strtoul(name + len, 10, &offset)) |
| 95 | break; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Simon Glass | 43b2e1a | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 99 | if (!dev) |
| 100 | return ret ? ret : -EINVAL; |
| 101 | |
Patrick Delaunay | 758bba9 | 2020-01-13 11:35:01 +0100 | [diff] [blame] | 102 | gpio_desc_init(desc, dev, offset); |
Simon Glass | 215bcc7 | 2015-06-23 15:38:40 -0600 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | int gpio_lookup_name(const char *name, struct udevice **devp, |
| 108 | unsigned int *offsetp, unsigned int *gpiop) |
| 109 | { |
| 110 | struct gpio_desc desc; |
| 111 | int ret; |
| 112 | |
| 113 | if (devp) |
| 114 | *devp = NULL; |
| 115 | ret = dm_gpio_lookup_name(name, &desc); |
| 116 | if (ret) |
| 117 | return ret; |
| 118 | |
Simon Glass | 43b2e1a | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 119 | if (devp) |
Simon Glass | 215bcc7 | 2015-06-23 15:38:40 -0600 | [diff] [blame] | 120 | *devp = desc.dev; |
Simon Glass | 43b2e1a | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 121 | if (offsetp) |
Simon Glass | 215bcc7 | 2015-06-23 15:38:40 -0600 | [diff] [blame] | 122 | *offsetp = desc.offset; |
| 123 | if (gpiop) { |
| 124 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev); |
| 125 | |
| 126 | *gpiop = uc_priv->gpio_base + desc.offset; |
| 127 | } |
Simon Glass | 43b2e1a | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 128 | |
| 129 | return 0; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 132 | int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc, |
| 133 | struct ofnode_phandle_args *args) |
Eric Nelson | 786e98d | 2016-04-24 16:32:40 -0700 | [diff] [blame] | 134 | { |
| 135 | if (args->args_count < 1) |
| 136 | return -EINVAL; |
| 137 | |
| 138 | desc->offset = args->args[0]; |
| 139 | |
| 140 | if (args->args_count < 2) |
| 141 | return 0; |
| 142 | |
| 143 | if (args->args[1] & GPIO_ACTIVE_LOW) |
| 144 | desc->flags = GPIOD_ACTIVE_LOW; |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 149 | static int gpio_find_and_xlate(struct gpio_desc *desc, |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 150 | struct ofnode_phandle_args *args) |
Simon Glass | d3322bb | 2015-01-05 20:05:28 -0700 | [diff] [blame] | 151 | { |
| 152 | struct dm_gpio_ops *ops = gpio_get_ops(desc->dev); |
| 153 | |
Eric Nelson | 786e98d | 2016-04-24 16:32:40 -0700 | [diff] [blame] | 154 | if (ops->xlate) |
| 155 | return ops->xlate(desc->dev, desc, args); |
Simon Glass | d3322bb | 2015-01-05 20:05:28 -0700 | [diff] [blame] | 156 | else |
Eric Nelson | 786e98d | 2016-04-24 16:32:40 -0700 | [diff] [blame] | 157 | return gpio_xlate_offs_flags(desc->dev, desc, args); |
Simon Glass | d3322bb | 2015-01-05 20:05:28 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 160 | #if defined(CONFIG_GPIO_HOG) |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 161 | |
| 162 | struct gpio_hog_priv { |
| 163 | struct gpio_desc gpiod; |
| 164 | }; |
| 165 | |
| 166 | struct gpio_hog_data { |
| 167 | int gpiod_flags; |
| 168 | int value; |
| 169 | u32 val[2]; |
| 170 | }; |
| 171 | |
| 172 | static int gpio_hog_ofdata_to_platdata(struct udevice *dev) |
| 173 | { |
| 174 | struct gpio_hog_data *plat = dev_get_platdata(dev); |
| 175 | const char *nodename; |
| 176 | int ret; |
| 177 | |
| 178 | plat->value = 0; |
| 179 | if (dev_read_bool(dev, "input")) { |
| 180 | plat->gpiod_flags = GPIOD_IS_IN; |
| 181 | } else if (dev_read_bool(dev, "output-high")) { |
| 182 | plat->value = 1; |
| 183 | plat->gpiod_flags = GPIOD_IS_OUT; |
| 184 | } else if (dev_read_bool(dev, "output-low")) { |
| 185 | plat->gpiod_flags = GPIOD_IS_OUT; |
| 186 | } else { |
| 187 | printf("%s: missing gpio-hog state.\n", __func__); |
| 188 | return -EINVAL; |
| 189 | } |
| 190 | ret = dev_read_u32_array(dev, "gpios", plat->val, 2); |
| 191 | if (ret) { |
| 192 | printf("%s: wrong gpios property, 2 values needed %d\n", |
| 193 | __func__, ret); |
| 194 | return ret; |
| 195 | } |
| 196 | nodename = dev_read_string(dev, "line-name"); |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 197 | if (nodename) |
| 198 | device_set_name(dev, nodename); |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int gpio_hog_probe(struct udevice *dev) |
| 204 | { |
| 205 | struct gpio_hog_data *plat = dev_get_platdata(dev); |
| 206 | struct gpio_hog_priv *priv = dev_get_priv(dev); |
| 207 | int ret; |
| 208 | |
| 209 | ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog", |
| 210 | plat->val[0], plat->gpiod_flags, |
| 211 | plat->val[1], &priv->gpiod); |
| 212 | if (ret < 0) { |
| 213 | debug("%s: node %s could not get gpio.\n", __func__, |
| 214 | dev->name); |
| 215 | return ret; |
| 216 | } |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 217 | |
| 218 | if (plat->gpiod_flags == GPIOD_IS_OUT) { |
| 219 | ret = dm_gpio_set_value(&priv->gpiod, plat->value); |
| 220 | if (ret < 0) { |
| 221 | debug("%s: node %s could not set gpio.\n", __func__, |
| 222 | dev->name); |
| 223 | return ret; |
| 224 | } |
| 225 | } |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | int gpio_hog_probe_all(void) |
| 231 | { |
| 232 | struct udevice *dev; |
| 233 | int ret; |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 234 | int retval = 0; |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 235 | |
| 236 | for (uclass_first_device(UCLASS_NOP, &dev); |
| 237 | dev; |
| 238 | uclass_find_next_device(&dev)) { |
| 239 | if (dev->driver == DM_GET_DRIVER(gpio_hog)) { |
| 240 | ret = device_probe(dev); |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 241 | if (ret) { |
| 242 | printf("Failed to probe device %s err: %d\n", |
| 243 | dev->name, ret); |
| 244 | retval = ret; |
| 245 | } |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 249 | return retval; |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 252 | int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc) |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 253 | { |
| 254 | struct udevice *dev; |
| 255 | |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 256 | *desc = NULL; |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 257 | gpio_hog_probe_all(); |
| 258 | if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) { |
| 259 | struct gpio_hog_priv *priv = dev_get_priv(dev); |
| 260 | |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 261 | *desc = &priv->gpiod; |
| 262 | return 0; |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 265 | return -ENODEV; |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | U_BOOT_DRIVER(gpio_hog) = { |
| 269 | .name = "gpio_hog", |
| 270 | .id = UCLASS_NOP, |
| 271 | .ofdata_to_platdata = gpio_hog_ofdata_to_platdata, |
| 272 | .probe = gpio_hog_probe, |
| 273 | .priv_auto_alloc_size = sizeof(struct gpio_hog_priv), |
| 274 | .platdata_auto_alloc_size = sizeof(struct gpio_hog_data), |
| 275 | }; |
| 276 | #else |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 277 | int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc) |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 278 | { |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 279 | return 0; |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 280 | } |
| 281 | #endif |
| 282 | |
Simon Glass | 047cdb3 | 2015-06-23 15:38:41 -0600 | [diff] [blame] | 283 | int dm_gpio_request(struct gpio_desc *desc, const char *label) |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 284 | { |
| 285 | struct udevice *dev = desc->dev; |
| 286 | struct gpio_dev_priv *uc_priv; |
| 287 | char *str; |
| 288 | int ret; |
| 289 | |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 290 | uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 291 | if (uc_priv->name[desc->offset]) |
| 292 | return -EBUSY; |
| 293 | str = strdup(label); |
| 294 | if (!str) |
| 295 | return -ENOMEM; |
| 296 | if (gpio_get_ops(dev)->request) { |
| 297 | ret = gpio_get_ops(dev)->request(dev, desc->offset, label); |
| 298 | if (ret) { |
| 299 | free(str); |
| 300 | return ret; |
| 301 | } |
| 302 | } |
| 303 | uc_priv->name[desc->offset] = str; |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 308 | static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...) |
| 309 | { |
Simon Glass | 7611ac6 | 2019-09-25 08:56:27 -0600 | [diff] [blame] | 310 | #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 311 | va_list args; |
| 312 | char buf[40]; |
| 313 | |
| 314 | va_start(args, fmt); |
| 315 | vscnprintf(buf, sizeof(buf), fmt, args); |
| 316 | va_end(args); |
| 317 | return dm_gpio_request(desc, buf); |
Simon Glass | ee8a3d9 | 2015-12-29 05:22:48 -0700 | [diff] [blame] | 318 | #else |
| 319 | return dm_gpio_request(desc, fmt); |
| 320 | #endif |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 323 | /** |
| 324 | * gpio_request() - [COMPAT] Request GPIO |
| 325 | * gpio: GPIO number |
| 326 | * label: Name for the requested GPIO |
| 327 | * |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 328 | * The label is copied and allocated so the caller does not need to keep |
| 329 | * the pointer around. |
| 330 | * |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 331 | * This function implements the API that's compatible with current |
| 332 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 333 | * GPIO driver. Returns 0 on success, negative value on error. |
| 334 | */ |
| 335 | int gpio_request(unsigned gpio, const char *label) |
| 336 | { |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 337 | struct gpio_desc desc; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 338 | int ret; |
| 339 | |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 340 | ret = gpio_to_device(gpio, &desc); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 341 | if (ret) |
| 342 | return ret; |
| 343 | |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 344 | return dm_gpio_request(&desc, label); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /** |
Simon Glass | 1b27d60 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 348 | * gpio_requestf() - [COMPAT] Request GPIO |
| 349 | * @gpio: GPIO number |
| 350 | * @fmt: Format string for the requested GPIO |
| 351 | * @...: Arguments for the printf() format string |
| 352 | * |
| 353 | * This function implements the API that's compatible with current |
| 354 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 355 | * GPIO driver. Returns 0 on success, negative value on error. |
| 356 | */ |
| 357 | int gpio_requestf(unsigned gpio, const char *fmt, ...) |
| 358 | { |
Simon Glass | 7611ac6 | 2019-09-25 08:56:27 -0600 | [diff] [blame] | 359 | #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF) |
Simon Glass | 1b27d60 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 360 | va_list args; |
| 361 | char buf[40]; |
| 362 | |
| 363 | va_start(args, fmt); |
| 364 | vscnprintf(buf, sizeof(buf), fmt, args); |
| 365 | va_end(args); |
| 366 | return gpio_request(gpio, buf); |
Simon Glass | ee8a3d9 | 2015-12-29 05:22:48 -0700 | [diff] [blame] | 367 | #else |
| 368 | return gpio_request(gpio, fmt); |
| 369 | #endif |
Simon Glass | 1b27d60 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 370 | } |
| 371 | |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 372 | int _dm_gpio_free(struct udevice *dev, uint offset) |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 373 | { |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 374 | struct gpio_dev_priv *uc_priv; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 375 | int ret; |
| 376 | |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 377 | uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 378 | if (!uc_priv->name[offset]) |
| 379 | return -ENXIO; |
Simon Glass | b3a4754 | 2020-02-04 20:15:17 -0700 | [diff] [blame] | 380 | if (gpio_get_ops(dev)->rfree) { |
| 381 | ret = gpio_get_ops(dev)->rfree(dev, offset); |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 382 | if (ret) |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | free(uc_priv->name[offset]); |
| 387 | uc_priv->name[offset] = NULL; |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 392 | /** |
| 393 | * gpio_free() - [COMPAT] Relinquish GPIO |
| 394 | * gpio: GPIO number |
| 395 | * |
| 396 | * This function implements the API that's compatible with current |
| 397 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 398 | * GPIO driver. Returns 0 on success, negative value on error. |
| 399 | */ |
| 400 | int gpio_free(unsigned gpio) |
| 401 | { |
| 402 | struct gpio_desc desc; |
| 403 | int ret; |
| 404 | |
| 405 | ret = gpio_to_device(gpio, &desc); |
| 406 | if (ret) |
| 407 | return ret; |
| 408 | |
| 409 | return _dm_gpio_free(desc.dev, desc.offset); |
| 410 | } |
| 411 | |
Simon Glass | fd83897 | 2016-03-06 19:27:51 -0700 | [diff] [blame] | 412 | static int check_reserved(const struct gpio_desc *desc, const char *func) |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 413 | { |
Simon Glass | 230c143 | 2015-07-02 18:16:16 -0600 | [diff] [blame] | 414 | struct gpio_dev_priv *uc_priv; |
| 415 | |
| 416 | if (!dm_gpio_is_valid(desc)) |
| 417 | return -ENOENT; |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 418 | |
Simon Glass | 230c143 | 2015-07-02 18:16:16 -0600 | [diff] [blame] | 419 | uc_priv = dev_get_uclass_priv(desc->dev); |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 420 | if (!uc_priv->name[desc->offset]) { |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 421 | printf("%s: %s: error: gpio %s%d not reserved\n", |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 422 | desc->dev->name, func, |
| 423 | uc_priv->bank_name ? uc_priv->bank_name : "", |
| 424 | desc->offset); |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 425 | return -EBUSY; |
| 426 | } |
| 427 | |
| 428 | return 0; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /** |
| 432 | * gpio_direction_input() - [COMPAT] Set GPIO direction to input |
| 433 | * gpio: GPIO number |
| 434 | * |
| 435 | * This function implements the API that's compatible with current |
| 436 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 437 | * GPIO driver. Returns 0 on success, negative value on error. |
| 438 | */ |
| 439 | int gpio_direction_input(unsigned gpio) |
| 440 | { |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 441 | struct gpio_desc desc; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 442 | int ret; |
| 443 | |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 444 | ret = gpio_to_device(gpio, &desc); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 445 | if (ret) |
| 446 | return ret; |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 447 | ret = check_reserved(&desc, "dir_input"); |
| 448 | if (ret) |
| 449 | return ret; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 450 | |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 451 | return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /** |
| 455 | * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value |
| 456 | * gpio: GPIO number |
| 457 | * value: Logical value to be set on the GPIO pin |
| 458 | * |
| 459 | * This function implements the API that's compatible with current |
| 460 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 461 | * GPIO driver. Returns 0 on success, negative value on error. |
| 462 | */ |
| 463 | int gpio_direction_output(unsigned gpio, int value) |
| 464 | { |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 465 | struct gpio_desc desc; |
| 466 | int ret; |
| 467 | |
| 468 | ret = gpio_to_device(gpio, &desc); |
| 469 | if (ret) |
| 470 | return ret; |
| 471 | ret = check_reserved(&desc, "dir_output"); |
| 472 | if (ret) |
| 473 | return ret; |
| 474 | |
| 475 | return gpio_get_ops(desc.dev)->direction_output(desc.dev, |
| 476 | desc.offset, value); |
| 477 | } |
| 478 | |
Patrick Delaunay | 4c2c96f | 2020-01-13 11:35:02 +0100 | [diff] [blame^] | 479 | static int _gpio_get_value(const struct gpio_desc *desc) |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 480 | { |
| 481 | int value; |
Patrick Delaunay | 4c2c96f | 2020-01-13 11:35:02 +0100 | [diff] [blame^] | 482 | |
| 483 | value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset); |
| 484 | |
| 485 | return desc->flags & GPIOD_ACTIVE_LOW ? !value : value; |
| 486 | } |
| 487 | |
| 488 | int dm_gpio_get_value(const struct gpio_desc *desc) |
| 489 | { |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 490 | int ret; |
| 491 | |
| 492 | ret = check_reserved(desc, "get_value"); |
| 493 | if (ret) |
| 494 | return ret; |
| 495 | |
Patrick Delaunay | 4c2c96f | 2020-01-13 11:35:02 +0100 | [diff] [blame^] | 496 | return _gpio_get_value(desc); |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Simon Glass | fd83897 | 2016-03-06 19:27:51 -0700 | [diff] [blame] | 499 | int dm_gpio_set_value(const struct gpio_desc *desc, int value) |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 500 | { |
| 501 | int ret; |
| 502 | |
| 503 | ret = check_reserved(desc, "set_value"); |
| 504 | if (ret) |
| 505 | return ret; |
| 506 | |
| 507 | if (desc->flags & GPIOD_ACTIVE_LOW) |
| 508 | value = !value; |
| 509 | gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value); |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags) |
| 514 | { |
| 515 | struct udevice *dev = desc->dev; |
| 516 | struct dm_gpio_ops *ops = gpio_get_ops(dev); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 517 | int ret; |
| 518 | |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 519 | ret = check_reserved(desc, "set_dir"); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 520 | if (ret) |
| 521 | return ret; |
| 522 | |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 523 | if (flags & GPIOD_IS_OUT) { |
| 524 | int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0; |
| 525 | |
| 526 | if (flags & GPIOD_ACTIVE_LOW) |
| 527 | value = !value; |
| 528 | ret = ops->direction_output(dev, desc->offset, value); |
| 529 | } else if (flags & GPIOD_IS_IN) { |
| 530 | ret = ops->direction_input(dev, desc->offset); |
| 531 | } |
| 532 | if (ret) |
| 533 | return ret; |
| 534 | /* |
| 535 | * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in |
| 536 | * futures |
| 537 | */ |
| 538 | desc->flags = flags; |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | int dm_gpio_set_dir(struct gpio_desc *desc) |
| 544 | { |
| 545 | return dm_gpio_set_dir_flags(desc, desc->flags); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /** |
| 549 | * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value |
| 550 | * gpio: GPIO number |
| 551 | * |
| 552 | * This function implements the API that's compatible with current |
| 553 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 554 | * GPIO driver. Returns the value of the GPIO pin, or negative value |
| 555 | * on error. |
| 556 | */ |
| 557 | int gpio_get_value(unsigned gpio) |
| 558 | { |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 559 | int ret; |
| 560 | |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 561 | struct gpio_desc desc; |
| 562 | |
| 563 | ret = gpio_to_device(gpio, &desc); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 564 | if (ret) |
| 565 | return ret; |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 566 | return dm_gpio_get_value(&desc); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /** |
| 570 | * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin |
| 571 | * gpio: GPIO number |
| 572 | * value: Logical value to be set on the GPIO pin. |
| 573 | * |
| 574 | * This function implements the API that's compatible with current |
| 575 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 576 | * GPIO driver. Returns 0 on success, negative value on error. |
| 577 | */ |
| 578 | int gpio_set_value(unsigned gpio, int value) |
| 579 | { |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 580 | struct gpio_desc desc; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 581 | int ret; |
| 582 | |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 583 | ret = gpio_to_device(gpio, &desc); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 584 | if (ret) |
| 585 | return ret; |
Simon Glass | ce55529 | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 586 | return dm_gpio_set_value(&desc, value); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 589 | const char *gpio_get_bank_info(struct udevice *dev, int *bit_count) |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 590 | { |
| 591 | struct gpio_dev_priv *priv; |
| 592 | |
| 593 | /* Must be called on an active device */ |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 594 | priv = dev_get_uclass_priv(dev); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 595 | assert(priv); |
| 596 | |
| 597 | *bit_count = priv->gpio_count; |
| 598 | return priv->bank_name; |
| 599 | } |
| 600 | |
Simon Glass | 6e5e6dd | 2014-10-04 11:29:43 -0600 | [diff] [blame] | 601 | static const char * const gpio_function[GPIOF_COUNT] = { |
| 602 | "input", |
| 603 | "output", |
| 604 | "unused", |
| 605 | "unknown", |
| 606 | "func", |
| 607 | }; |
| 608 | |
Masahiro Yamada | 286c252 | 2017-06-22 16:50:25 +0900 | [diff] [blame] | 609 | static int get_function(struct udevice *dev, int offset, bool skip_unused, |
| 610 | const char **namep) |
Simon Glass | 6e5e6dd | 2014-10-04 11:29:43 -0600 | [diff] [blame] | 611 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 612 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 6e5e6dd | 2014-10-04 11:29:43 -0600 | [diff] [blame] | 613 | struct dm_gpio_ops *ops = gpio_get_ops(dev); |
| 614 | |
| 615 | BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function)); |
| 616 | if (!device_active(dev)) |
| 617 | return -ENODEV; |
| 618 | if (offset < 0 || offset >= uc_priv->gpio_count) |
| 619 | return -EINVAL; |
| 620 | if (namep) |
| 621 | *namep = uc_priv->name[offset]; |
| 622 | if (skip_unused && !uc_priv->name[offset]) |
| 623 | return GPIOF_UNUSED; |
| 624 | if (ops->get_function) { |
| 625 | int ret; |
| 626 | |
| 627 | ret = ops->get_function(dev, offset); |
| 628 | if (ret < 0) |
| 629 | return ret; |
| 630 | if (ret >= ARRAY_SIZE(gpio_function)) |
| 631 | return -ENODATA; |
| 632 | return ret; |
| 633 | } |
| 634 | |
| 635 | return GPIOF_UNKNOWN; |
| 636 | } |
| 637 | |
| 638 | int gpio_get_function(struct udevice *dev, int offset, const char **namep) |
| 639 | { |
| 640 | return get_function(dev, offset, true, namep); |
| 641 | } |
| 642 | |
| 643 | int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep) |
| 644 | { |
| 645 | return get_function(dev, offset, false, namep); |
| 646 | } |
| 647 | |
Simon Glass | 6b1ef59 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 648 | int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize) |
| 649 | { |
| 650 | struct dm_gpio_ops *ops = gpio_get_ops(dev); |
| 651 | struct gpio_dev_priv *priv; |
| 652 | char *str = buf; |
| 653 | int func; |
| 654 | int ret; |
| 655 | int len; |
| 656 | |
| 657 | BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function)); |
| 658 | |
| 659 | *buf = 0; |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 660 | priv = dev_get_uclass_priv(dev); |
Simon Glass | 6b1ef59 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 661 | ret = gpio_get_raw_function(dev, offset, NULL); |
| 662 | if (ret < 0) |
| 663 | return ret; |
| 664 | func = ret; |
| 665 | len = snprintf(str, buffsize, "%s%d: %s", |
| 666 | priv->bank_name ? priv->bank_name : "", |
| 667 | offset, gpio_function[func]); |
| 668 | if (func == GPIOF_INPUT || func == GPIOF_OUTPUT || |
| 669 | func == GPIOF_UNUSED) { |
| 670 | const char *label; |
| 671 | bool used; |
| 672 | |
| 673 | ret = ops->get_value(dev, offset); |
| 674 | if (ret < 0) |
| 675 | return ret; |
| 676 | used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED; |
| 677 | snprintf(str + len, buffsize - len, ": %d [%c]%s%s", |
| 678 | ret, |
| 679 | used ? 'x' : ' ', |
| 680 | used ? " " : "", |
| 681 | label ? label : ""); |
| 682 | } |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
Simon Glass | bef54db | 2015-04-14 21:03:20 -0600 | [diff] [blame] | 687 | int gpio_claim_vector(const int *gpio_num_array, const char *fmt) |
| 688 | { |
| 689 | int i, ret; |
| 690 | int gpio; |
| 691 | |
| 692 | for (i = 0; i < 32; i++) { |
| 693 | gpio = gpio_num_array[i]; |
| 694 | if (gpio == -1) |
| 695 | break; |
| 696 | ret = gpio_requestf(gpio, fmt, i); |
| 697 | if (ret) |
| 698 | goto err; |
| 699 | ret = gpio_direction_input(gpio); |
| 700 | if (ret) { |
| 701 | gpio_free(gpio); |
| 702 | goto err; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return 0; |
| 707 | err: |
| 708 | for (i--; i >= 0; i--) |
| 709 | gpio_free(gpio_num_array[i]); |
| 710 | |
| 711 | return ret; |
| 712 | } |
| 713 | |
Simon Glass | 2c97a8f | 2014-11-10 18:00:21 -0700 | [diff] [blame] | 714 | /* |
| 715 | * get a number comprised of multiple GPIO values. gpio_num_array points to |
| 716 | * the array of gpio pin numbers to scan, terminated by -1. |
| 717 | */ |
Simon Glass | bef54db | 2015-04-14 21:03:20 -0600 | [diff] [blame] | 718 | int gpio_get_values_as_int(const int *gpio_list) |
Simon Glass | 2c97a8f | 2014-11-10 18:00:21 -0700 | [diff] [blame] | 719 | { |
| 720 | int gpio; |
| 721 | unsigned bitmask = 1; |
| 722 | unsigned vector = 0; |
Simon Glass | bef54db | 2015-04-14 21:03:20 -0600 | [diff] [blame] | 723 | int ret; |
Simon Glass | 2c97a8f | 2014-11-10 18:00:21 -0700 | [diff] [blame] | 724 | |
| 725 | while (bitmask && |
Simon Glass | bef54db | 2015-04-14 21:03:20 -0600 | [diff] [blame] | 726 | ((gpio = *gpio_list++) != -1)) { |
| 727 | ret = gpio_get_value(gpio); |
| 728 | if (ret < 0) |
| 729 | return ret; |
| 730 | else if (ret) |
Simon Glass | 2c97a8f | 2014-11-10 18:00:21 -0700 | [diff] [blame] | 731 | vector |= bitmask; |
| 732 | bitmask <<= 1; |
| 733 | } |
Simon Glass | bef54db | 2015-04-14 21:03:20 -0600 | [diff] [blame] | 734 | |
Simon Glass | 2c97a8f | 2014-11-10 18:00:21 -0700 | [diff] [blame] | 735 | return vector; |
| 736 | } |
| 737 | |
Simon Glass | fd83897 | 2016-03-06 19:27:51 -0700 | [diff] [blame] | 738 | int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count) |
Simon Glass | df1687d | 2016-03-06 19:27:50 -0700 | [diff] [blame] | 739 | { |
| 740 | unsigned bitmask = 1; |
| 741 | unsigned vector = 0; |
| 742 | int ret, i; |
| 743 | |
| 744 | for (i = 0; i < count; i++) { |
| 745 | ret = dm_gpio_get_value(&desc_list[i]); |
| 746 | if (ret < 0) |
| 747 | return ret; |
| 748 | else if (ret) |
| 749 | vector |= bitmask; |
| 750 | bitmask <<= 1; |
| 751 | } |
| 752 | |
| 753 | return vector; |
| 754 | } |
| 755 | |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 756 | /** |
| 757 | * gpio_request_tail: common work for requesting a gpio. |
| 758 | * |
| 759 | * ret: return value from previous work in function which calls |
| 760 | * this function. |
| 761 | * This seems bogus (why calling this function instead not |
| 762 | * calling it and end caller function instead?). |
| 763 | * Because on error in caller function we want to set some |
| 764 | * default values in gpio desc and have a common error |
| 765 | * debug message, which provides this function. |
| 766 | * nodename: Name of node for which gpio gets requested |
| 767 | * used for gpio label name. |
| 768 | * args: pointer to output arguments structure |
| 769 | * list_name: Name of GPIO list |
| 770 | * used for gpio label name. |
| 771 | * index: gpio index in gpio list |
| 772 | * used for gpio label name. |
| 773 | * desc: pointer to gpio descriptor, filled from this |
| 774 | * function. |
| 775 | * flags: gpio flags to use. |
| 776 | * add_index: should index added to gpio label name |
| 777 | * gpio_dev: pointer to gpio device from which the gpio |
| 778 | * will be requested. If NULL try to get the |
| 779 | * gpio device with uclass_get_device_by_ofnode() |
| 780 | * |
| 781 | * return: In error case this function sets default values in |
| 782 | * gpio descriptor, also emmits a debug message. |
| 783 | * On success it returns 0 else the error code from |
| 784 | * function calls, or the error code passed through |
| 785 | * ret to this function. |
| 786 | * |
| 787 | */ |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 788 | static int gpio_request_tail(int ret, const char *nodename, |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 789 | struct ofnode_phandle_args *args, |
| 790 | const char *list_name, int index, |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 791 | struct gpio_desc *desc, int flags, |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 792 | bool add_index, struct udevice *gpio_dev) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 793 | { |
Patrick Delaunay | 758bba9 | 2020-01-13 11:35:01 +0100 | [diff] [blame] | 794 | gpio_desc_init(desc, gpio_dev, 0); |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 795 | if (ret) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 796 | goto err; |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 797 | |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 798 | if (!desc->dev) { |
| 799 | ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node, |
| 800 | &desc->dev); |
| 801 | if (ret) { |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 802 | debug("%s: uclass_get_device_by_ofnode failed\n", |
| 803 | __func__); |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 804 | goto err; |
| 805 | } |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 806 | } |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 807 | ret = gpio_find_and_xlate(desc, args); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 808 | if (ret) { |
| 809 | debug("%s: gpio_find_and_xlate failed\n", __func__); |
| 810 | goto err; |
| 811 | } |
| 812 | ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s", |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 813 | nodename, list_name, index); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 814 | if (ret) { |
| 815 | debug("%s: dm_gpio_requestf failed\n", __func__); |
| 816 | goto err; |
| 817 | } |
| 818 | ret = dm_gpio_set_dir_flags(desc, flags | desc->flags); |
| 819 | if (ret) { |
| 820 | debug("%s: dm_gpio_set_dir failed\n", __func__); |
| 821 | goto err; |
| 822 | } |
| 823 | |
| 824 | return 0; |
| 825 | err: |
| 826 | debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n", |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 827 | __func__, nodename, list_name, index, ret); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 828 | return ret; |
| 829 | } |
| 830 | |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 831 | static int _gpio_request_by_name_nodev(ofnode node, const char *list_name, |
| 832 | int index, struct gpio_desc *desc, |
| 833 | int flags, bool add_index) |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 834 | { |
| 835 | struct ofnode_phandle_args args; |
| 836 | int ret; |
| 837 | |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 838 | ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0, |
| 839 | index, &args); |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 840 | |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 841 | return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name, |
| 842 | index, desc, flags, add_index, NULL); |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 843 | } |
| 844 | |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 845 | int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index, |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 846 | struct gpio_desc *desc, int flags) |
| 847 | { |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 848 | return _gpio_request_by_name_nodev(node, list_name, index, desc, flags, |
| 849 | index > 0); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 852 | int gpio_request_by_name(struct udevice *dev, const char *list_name, int index, |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 853 | struct gpio_desc *desc, int flags) |
| 854 | { |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 855 | struct ofnode_phandle_args args; |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 856 | ofnode node; |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 857 | int ret; |
| 858 | |
| 859 | ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, |
| 860 | index, &args); |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 861 | node = dev_ofnode(dev); |
| 862 | return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name, |
| 863 | index, desc, flags, index > 0, NULL); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 866 | int gpio_request_list_by_name_nodev(ofnode node, const char *list_name, |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 867 | struct gpio_desc *desc, int max_count, |
| 868 | int flags) |
| 869 | { |
| 870 | int count; |
| 871 | int ret; |
| 872 | |
Przemyslaw Marczak | 44fc536 | 2015-03-31 18:57:16 +0200 | [diff] [blame] | 873 | for (count = 0; count < max_count; count++) { |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 874 | ret = _gpio_request_by_name_nodev(node, list_name, count, |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 875 | &desc[count], flags, true); |
| 876 | if (ret == -ENOENT) |
| 877 | break; |
| 878 | else if (ret) |
| 879 | goto err; |
| 880 | } |
| 881 | |
| 882 | /* We ran out of GPIOs in the list */ |
| 883 | return count; |
| 884 | |
| 885 | err: |
| 886 | gpio_free_list_nodev(desc, count - 1); |
| 887 | |
| 888 | return ret; |
| 889 | } |
| 890 | |
| 891 | int gpio_request_list_by_name(struct udevice *dev, const char *list_name, |
| 892 | struct gpio_desc *desc, int max_count, |
| 893 | int flags) |
| 894 | { |
| 895 | /* |
| 896 | * This isn't ideal since we don't use dev->name in the debug() |
| 897 | * calls in gpio_request_by_name(), but we can do this until |
| 898 | * gpio_request_list_by_name_nodev() can be dropped. |
| 899 | */ |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 900 | return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc, |
| 901 | max_count, flags); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | int gpio_get_list_count(struct udevice *dev, const char *list_name) |
| 905 | { |
| 906 | int ret; |
| 907 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 908 | ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev), |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 909 | list_name, "#gpio-cells", 0, -1, |
| 910 | NULL); |
| 911 | if (ret) { |
| 912 | debug("%s: Node '%s', property '%s', GPIO count failed: %d\n", |
| 913 | __func__, dev->name, list_name, ret); |
| 914 | } |
| 915 | |
| 916 | return ret; |
| 917 | } |
| 918 | |
| 919 | int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc) |
| 920 | { |
| 921 | /* For now, we don't do any checking of dev */ |
| 922 | return _dm_gpio_free(desc->dev, desc->offset); |
| 923 | } |
| 924 | |
| 925 | int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count) |
| 926 | { |
| 927 | int i; |
| 928 | |
| 929 | /* For now, we don't do any checking of dev */ |
| 930 | for (i = 0; i < count; i++) |
| 931 | dm_gpio_free(dev, &desc[i]); |
| 932 | |
| 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | int gpio_free_list_nodev(struct gpio_desc *desc, int count) |
| 937 | { |
| 938 | return gpio_free_list(NULL, desc, count); |
| 939 | } |
| 940 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 941 | /* We need to renumber the GPIOs when any driver is probed/removed */ |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 942 | static int gpio_renumber(struct udevice *removed_dev) |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 943 | { |
| 944 | struct gpio_dev_priv *uc_priv; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 945 | struct udevice *dev; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 946 | struct uclass *uc; |
| 947 | unsigned base; |
| 948 | int ret; |
| 949 | |
| 950 | ret = uclass_get(UCLASS_GPIO, &uc); |
| 951 | if (ret) |
| 952 | return ret; |
| 953 | |
| 954 | /* Ensure that we have a base for each bank */ |
| 955 | base = 0; |
| 956 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 957 | if (device_active(dev) && dev != removed_dev) { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 958 | uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 959 | uc_priv->gpio_base = base; |
| 960 | base += uc_priv->gpio_count; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
Simon Glass | fd83897 | 2016-03-06 19:27:51 -0700 | [diff] [blame] | 967 | int gpio_get_number(const struct gpio_desc *desc) |
Simon Glass | 94f54d1 | 2015-03-25 12:21:58 -0600 | [diff] [blame] | 968 | { |
| 969 | struct udevice *dev = desc->dev; |
| 970 | struct gpio_dev_priv *uc_priv; |
| 971 | |
| 972 | if (!dev) |
| 973 | return -1; |
| 974 | uc_priv = dev->uclass_priv; |
| 975 | |
| 976 | return uc_priv->gpio_base + desc->offset; |
| 977 | } |
| 978 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 979 | static int gpio_post_probe(struct udevice *dev) |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 980 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 981 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 982 | |
| 983 | uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *)); |
| 984 | if (!uc_priv->name) |
| 985 | return -ENOMEM; |
| 986 | |
| 987 | return gpio_renumber(NULL); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 990 | static int gpio_pre_remove(struct udevice *dev) |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 991 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 992 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 993 | int i; |
| 994 | |
| 995 | for (i = 0; i < uc_priv->gpio_count; i++) { |
| 996 | if (uc_priv->name[i]) |
| 997 | free(uc_priv->name[i]); |
| 998 | } |
| 999 | free(uc_priv->name); |
| 1000 | |
| 1001 | return gpio_renumber(dev); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1004 | int gpio_dev_request_index(struct udevice *dev, const char *nodename, |
| 1005 | char *list_name, int index, int flags, |
| 1006 | int dtflags, struct gpio_desc *desc) |
| 1007 | { |
| 1008 | struct ofnode_phandle_args args; |
| 1009 | |
| 1010 | args.node = ofnode_null(); |
| 1011 | args.args_count = 2; |
| 1012 | args.args[0] = index; |
| 1013 | args.args[1] = dtflags; |
| 1014 | |
| 1015 | return gpio_request_tail(0, nodename, &args, list_name, index, desc, |
| 1016 | flags, 0, dev); |
| 1017 | } |
| 1018 | |
Michal Simek | 5f7202c | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1019 | static int gpio_post_bind(struct udevice *dev) |
| 1020 | { |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1021 | struct udevice *child; |
| 1022 | ofnode node; |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1023 | |
Michal Simek | 5f7202c | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1024 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 1025 | struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev); |
| 1026 | static int reloc_done; |
| 1027 | |
| 1028 | if (!reloc_done) { |
| 1029 | if (ops->request) |
| 1030 | ops->request += gd->reloc_off; |
Simon Glass | b3a4754 | 2020-02-04 20:15:17 -0700 | [diff] [blame] | 1031 | if (ops->rfree) |
| 1032 | ops->rfree += gd->reloc_off; |
Michal Simek | 5f7202c | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1033 | if (ops->direction_input) |
| 1034 | ops->direction_input += gd->reloc_off; |
| 1035 | if (ops->direction_output) |
| 1036 | ops->direction_output += gd->reloc_off; |
| 1037 | if (ops->get_value) |
| 1038 | ops->get_value += gd->reloc_off; |
| 1039 | if (ops->set_value) |
| 1040 | ops->set_value += gd->reloc_off; |
Michal Simek | 5f7202c | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1041 | if (ops->get_function) |
| 1042 | ops->get_function += gd->reloc_off; |
| 1043 | if (ops->xlate) |
| 1044 | ops->xlate += gd->reloc_off; |
| 1045 | |
| 1046 | reloc_done++; |
| 1047 | } |
| 1048 | #endif |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1049 | |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 1050 | if (IS_ENABLED(CONFIG_GPIO_HOG)) { |
| 1051 | dev_for_each_subnode(node, dev) { |
| 1052 | if (ofnode_read_bool(node, "gpio-hog")) { |
| 1053 | const char *name = ofnode_get_name(node); |
| 1054 | int ret; |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1055 | |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 1056 | ret = device_bind_driver_to_node(dev, |
| 1057 | "gpio_hog", |
| 1058 | name, node, |
| 1059 | &child); |
| 1060 | if (ret) |
| 1061 | return ret; |
| 1062 | } |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1063 | } |
| 1064 | } |
Michal Simek | 5f7202c | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1065 | return 0; |
| 1066 | } |
| 1067 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1068 | UCLASS_DRIVER(gpio) = { |
| 1069 | .id = UCLASS_GPIO, |
| 1070 | .name = "gpio", |
Bhuvanchandra DV | b1a1fc9 | 2015-06-01 18:37:15 +0530 | [diff] [blame] | 1071 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1072 | .post_probe = gpio_post_probe, |
Michal Simek | 5f7202c | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1073 | .post_bind = gpio_post_bind, |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1074 | .pre_remove = gpio_pre_remove, |
| 1075 | .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv), |
| 1076 | }; |