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