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