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