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