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