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