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