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