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