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