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