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) { |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 1146 | debug("%s: uclass_get_device_by_ofnode failed\n", |
| 1147 | __func__); |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1148 | goto err; |
| 1149 | } |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1150 | } |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 1151 | ret = gpio_find_and_xlate(desc, args); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1152 | if (ret) { |
| 1153 | debug("%s: gpio_find_and_xlate failed\n", __func__); |
| 1154 | goto err; |
| 1155 | } |
| 1156 | ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s", |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1157 | nodename, list_name, index); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1158 | if (ret) { |
| 1159 | debug("%s: dm_gpio_requestf failed\n", __func__); |
| 1160 | goto err; |
| 1161 | } |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 1162 | |
| 1163 | /* Keep any direction flags provided by the devicetree */ |
| 1164 | ret = dm_gpio_set_dir_flags(desc, |
| 1165 | flags | (desc->flags & GPIOD_MASK_DIR)); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1166 | if (ret) { |
| 1167 | debug("%s: dm_gpio_set_dir failed\n", __func__); |
| 1168 | goto err; |
| 1169 | } |
| 1170 | |
| 1171 | return 0; |
| 1172 | err: |
| 1173 | 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] | 1174 | __func__, nodename, list_name, index, ret); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1175 | return ret; |
| 1176 | } |
| 1177 | |
Simon Glass | 9288265 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 1178 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 1179 | static int _gpio_request_by_name_nodev(ofnode node, const char *list_name, |
| 1180 | int index, struct gpio_desc *desc, |
| 1181 | int flags, bool add_index) |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 1182 | { |
| 1183 | struct ofnode_phandle_args args; |
| 1184 | int ret; |
| 1185 | |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 1186 | ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0, |
| 1187 | index, &args); |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 1188 | |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1189 | return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name, |
| 1190 | index, desc, flags, add_index, NULL); |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 1191 | } |
| 1192 | |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 1193 | 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] | 1194 | struct gpio_desc *desc, int flags) |
| 1195 | { |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 1196 | return _gpio_request_by_name_nodev(node, list_name, index, desc, flags, |
| 1197 | index > 0); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 1200 | 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] | 1201 | struct gpio_desc *desc, int flags) |
| 1202 | { |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 1203 | struct ofnode_phandle_args args; |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1204 | ofnode node; |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 1205 | int ret; |
| 1206 | |
| 1207 | ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, |
| 1208 | index, &args); |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1209 | node = dev_ofnode(dev); |
| 1210 | return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name, |
| 1211 | index, desc, flags, index > 0, NULL); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Andrew Jeffery | 2e62c6b | 2022-01-31 13:54:05 +1030 | [diff] [blame] | 1214 | int gpio_request_by_line_name(struct udevice *dev, const char *line_name, |
| 1215 | struct gpio_desc *desc, int flags) |
| 1216 | { |
| 1217 | int ret; |
| 1218 | |
Rasmus Villemoes | f4e744d | 2023-03-17 21:12:22 +0100 | [diff] [blame] | 1219 | if (!dev) { |
| 1220 | uclass_foreach_dev_probe(UCLASS_GPIO, dev) |
| 1221 | if (!gpio_request_by_line_name(dev, line_name, desc, flags)) |
| 1222 | return 0; |
| 1223 | return -ENOENT; |
| 1224 | } |
| 1225 | |
Andrew Jeffery | 2e62c6b | 2022-01-31 13:54:05 +1030 | [diff] [blame] | 1226 | ret = dev_read_stringlist_search(dev, "gpio-line-names", line_name); |
| 1227 | if (ret < 0) |
| 1228 | return ret; |
| 1229 | |
| 1230 | desc->dev = dev; |
| 1231 | desc->offset = ret; |
| 1232 | desc->flags = 0; |
| 1233 | |
| 1234 | ret = dm_gpio_request(desc, line_name); |
| 1235 | if (ret) { |
| 1236 | debug("%s: dm_gpio_requestf failed\n", __func__); |
| 1237 | return ret; |
| 1238 | } |
| 1239 | |
| 1240 | ret = dm_gpio_set_dir_flags(desc, flags | desc->flags); |
| 1241 | if (ret) |
| 1242 | debug("%s: dm_gpio_set_dir failed\n", __func__); |
| 1243 | |
| 1244 | return ret; |
| 1245 | } |
| 1246 | |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 1247 | 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] | 1248 | struct gpio_desc *desc, int max_count, |
| 1249 | int flags) |
| 1250 | { |
| 1251 | int count; |
| 1252 | int ret; |
| 1253 | |
Przemyslaw Marczak | 44fc536 | 2015-03-31 18:57:16 +0200 | [diff] [blame] | 1254 | for (count = 0; count < max_count; count++) { |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 1255 | ret = _gpio_request_by_name_nodev(node, list_name, count, |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1256 | &desc[count], flags, true); |
| 1257 | if (ret == -ENOENT) |
| 1258 | break; |
| 1259 | else if (ret) |
| 1260 | goto err; |
| 1261 | } |
| 1262 | |
| 1263 | /* We ran out of GPIOs in the list */ |
| 1264 | return count; |
| 1265 | |
| 1266 | err: |
Rasmus Villemoes | c9ca11c | 2023-04-19 12:10:13 +0200 | [diff] [blame] | 1267 | gpio_free_list_nodev(desc, count); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1268 | |
| 1269 | return ret; |
| 1270 | } |
| 1271 | |
| 1272 | int gpio_request_list_by_name(struct udevice *dev, const char *list_name, |
| 1273 | struct gpio_desc *desc, int max_count, |
| 1274 | int flags) |
| 1275 | { |
| 1276 | /* |
| 1277 | * This isn't ideal since we don't use dev->name in the debug() |
| 1278 | * calls in gpio_request_by_name(), but we can do this until |
| 1279 | * gpio_request_list_by_name_nodev() can be dropped. |
| 1280 | */ |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 1281 | return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc, |
| 1282 | max_count, flags); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | int gpio_get_list_count(struct udevice *dev, const char *list_name) |
| 1286 | { |
| 1287 | int ret; |
| 1288 | |
Sean Anderson | 8ad66c5 | 2021-04-20 10:50:54 -0400 | [diff] [blame] | 1289 | ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells", |
| 1290 | -ENOENT); |
| 1291 | if (ret < 0) { |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1292 | debug("%s: Node '%s', property '%s', GPIO count failed: %d\n", |
| 1293 | __func__, dev->name, list_name, ret); |
| 1294 | } |
| 1295 | |
| 1296 | return ret; |
| 1297 | } |
Simon Glass | ea38372 | 2021-02-04 21:21:54 -0700 | [diff] [blame] | 1298 | #endif /* OF_PLATDATA */ |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1299 | |
Simon Glass | 2149e11 | 2021-08-07 07:24:12 -0600 | [diff] [blame] | 1300 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 1301 | int gpio_request_by_phandle(struct udevice *dev, |
| 1302 | const struct phandle_2_arg *cells, |
| 1303 | struct gpio_desc *desc, int flags) |
| 1304 | { |
| 1305 | struct ofnode_phandle_args args; |
| 1306 | struct udevice *gpio_dev; |
| 1307 | const int index = 0; |
| 1308 | int ret; |
| 1309 | |
| 1310 | ret = device_get_by_ofplat_idx(cells->idx, &gpio_dev); |
| 1311 | if (ret) |
| 1312 | return ret; |
| 1313 | args.args[0] = cells->arg[0]; |
| 1314 | args.args[1] = cells->arg[1]; |
| 1315 | |
| 1316 | return gpio_request_tail(ret, NULL, &args, NULL, index, desc, flags, |
| 1317 | index > 0, gpio_dev); |
| 1318 | } |
| 1319 | #endif |
| 1320 | |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 1321 | int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc) |
| 1322 | { |
| 1323 | /* For now, we don't do any checking of dev */ |
| 1324 | return _dm_gpio_free(desc->dev, desc->offset); |
| 1325 | } |
| 1326 | |
| 1327 | int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count) |
| 1328 | { |
| 1329 | int i; |
| 1330 | |
| 1331 | /* For now, we don't do any checking of dev */ |
| 1332 | for (i = 0; i < count; i++) |
| 1333 | dm_gpio_free(dev, &desc[i]); |
| 1334 | |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
| 1338 | int gpio_free_list_nodev(struct gpio_desc *desc, int count) |
| 1339 | { |
| 1340 | return gpio_free_list(NULL, desc, count); |
| 1341 | } |
| 1342 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1343 | /* We need to renumber the GPIOs when any driver is probed/removed */ |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 1344 | static int gpio_renumber(struct udevice *removed_dev) |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1345 | { |
| 1346 | struct gpio_dev_priv *uc_priv; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 1347 | struct udevice *dev; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1348 | struct uclass *uc; |
| 1349 | unsigned base; |
| 1350 | int ret; |
| 1351 | |
| 1352 | ret = uclass_get(UCLASS_GPIO, &uc); |
| 1353 | if (ret) |
| 1354 | return ret; |
| 1355 | |
| 1356 | /* Ensure that we have a base for each bank */ |
| 1357 | base = 0; |
| 1358 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 1359 | if (device_active(dev) && dev != removed_dev) { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 1360 | uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1361 | uc_priv->gpio_base = base; |
| 1362 | base += uc_priv->gpio_count; |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
Simon Glass | fd83897 | 2016-03-06 19:27:51 -0700 | [diff] [blame] | 1369 | int gpio_get_number(const struct gpio_desc *desc) |
Simon Glass | 94f54d1 | 2015-03-25 12:21:58 -0600 | [diff] [blame] | 1370 | { |
| 1371 | struct udevice *dev = desc->dev; |
| 1372 | struct gpio_dev_priv *uc_priv; |
| 1373 | |
| 1374 | if (!dev) |
| 1375 | return -1; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 1376 | uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 94f54d1 | 2015-03-25 12:21:58 -0600 | [diff] [blame] | 1377 | |
| 1378 | return uc_priv->gpio_base + desc->offset; |
| 1379 | } |
| 1380 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 1381 | static int gpio_post_probe(struct udevice *dev) |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1382 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 1383 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 1384 | |
| 1385 | uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *)); |
| 1386 | if (!uc_priv->name) |
| 1387 | return -ENOMEM; |
Marek Vasut | 3a18847 | 2023-08-02 01:26:02 +0200 | [diff] [blame] | 1388 | |
| 1389 | uc_priv->claimed = calloc(DIV_ROUND_UP(uc_priv->gpio_count, |
| 1390 | GPIO_ALLOC_BITS), |
| 1391 | GPIO_ALLOC_BITS / 8); |
| 1392 | if (!uc_priv->claimed) { |
| 1393 | free(uc_priv->name); |
| 1394 | return -ENOMEM; |
| 1395 | } |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 1396 | |
| 1397 | return gpio_renumber(NULL); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 1400 | static int gpio_pre_remove(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 | int i; |
| 1404 | |
| 1405 | for (i = 0; i < uc_priv->gpio_count; i++) { |
| 1406 | if (uc_priv->name[i]) |
| 1407 | free(uc_priv->name[i]); |
| 1408 | } |
Marek Vasut | 3a18847 | 2023-08-02 01:26:02 +0200 | [diff] [blame] | 1409 | free(uc_priv->claimed); |
Simon Glass | 0f4517d | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 1410 | free(uc_priv->name); |
| 1411 | |
| 1412 | return gpio_renumber(dev); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1415 | int gpio_dev_request_index(struct udevice *dev, const char *nodename, |
| 1416 | char *list_name, int index, int flags, |
| 1417 | int dtflags, struct gpio_desc *desc) |
| 1418 | { |
| 1419 | struct ofnode_phandle_args args; |
| 1420 | |
| 1421 | args.node = ofnode_null(); |
| 1422 | args.args_count = 2; |
| 1423 | args.args[0] = index; |
| 1424 | args.args[1] = dtflags; |
| 1425 | |
| 1426 | return gpio_request_tail(0, nodename, &args, list_name, index, desc, |
| 1427 | flags, 0, dev); |
| 1428 | } |
| 1429 | |
Jean-Jacques Hiblot | 775d6a2 | 2020-09-11 13:43:34 +0530 | [diff] [blame] | 1430 | static void devm_gpiod_release(struct udevice *dev, void *res) |
| 1431 | { |
| 1432 | dm_gpio_free(dev, res); |
| 1433 | } |
| 1434 | |
| 1435 | static int devm_gpiod_match(struct udevice *dev, void *res, void *data) |
| 1436 | { |
| 1437 | return res == data; |
| 1438 | } |
| 1439 | |
| 1440 | struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id, |
| 1441 | unsigned int index, int flags) |
| 1442 | { |
| 1443 | int rc; |
| 1444 | struct gpio_desc *desc; |
| 1445 | char *propname; |
| 1446 | static const char suffix[] = "-gpios"; |
| 1447 | |
| 1448 | propname = malloc(strlen(id) + sizeof(suffix)); |
| 1449 | if (!propname) { |
| 1450 | rc = -ENOMEM; |
| 1451 | goto end; |
| 1452 | } |
| 1453 | |
| 1454 | strcpy(propname, id); |
| 1455 | strcat(propname, suffix); |
| 1456 | |
| 1457 | desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc), |
| 1458 | __GFP_ZERO); |
| 1459 | if (unlikely(!desc)) { |
| 1460 | rc = -ENOMEM; |
| 1461 | goto end; |
| 1462 | } |
| 1463 | |
| 1464 | rc = gpio_request_by_name(dev, propname, index, desc, flags); |
| 1465 | |
| 1466 | end: |
| 1467 | if (propname) |
| 1468 | free(propname); |
| 1469 | |
| 1470 | if (rc) |
| 1471 | return ERR_PTR(rc); |
| 1472 | |
| 1473 | devres_add(dev, desc); |
| 1474 | |
| 1475 | return desc; |
| 1476 | } |
| 1477 | |
| 1478 | struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev, |
| 1479 | const char *id, |
| 1480 | unsigned int index, |
| 1481 | int flags) |
| 1482 | { |
| 1483 | struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags); |
| 1484 | |
| 1485 | if (IS_ERR(desc)) |
| 1486 | return NULL; |
| 1487 | |
| 1488 | return desc; |
| 1489 | } |
| 1490 | |
| 1491 | void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc) |
| 1492 | { |
| 1493 | int rc; |
| 1494 | |
| 1495 | rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc); |
| 1496 | WARN_ON(rc); |
| 1497 | } |
| 1498 | |
Michal Simek | 5f7202c | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1499 | static int gpio_post_bind(struct udevice *dev) |
| 1500 | { |
| 1501 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 1502 | struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev); |
| 1503 | static int reloc_done; |
| 1504 | |
| 1505 | if (!reloc_done) { |
| 1506 | if (ops->request) |
| 1507 | ops->request += gd->reloc_off; |
Simon Glass | b3a4754 | 2020-02-04 20:15:17 -0700 | [diff] [blame] | 1508 | if (ops->rfree) |
| 1509 | ops->rfree += gd->reloc_off; |
Michal Simek | 5f7202c | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1510 | if (ops->direction_input) |
| 1511 | ops->direction_input += gd->reloc_off; |
| 1512 | if (ops->direction_output) |
| 1513 | ops->direction_output += gd->reloc_off; |
| 1514 | if (ops->get_value) |
| 1515 | ops->get_value += gd->reloc_off; |
| 1516 | if (ops->set_value) |
| 1517 | ops->set_value += gd->reloc_off; |
Michal Simek | 5f7202c | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1518 | if (ops->get_function) |
| 1519 | ops->get_function += gd->reloc_off; |
| 1520 | if (ops->xlate) |
| 1521 | ops->xlate += gd->reloc_off; |
Simon Glass | 54befdd | 2021-02-04 21:21:55 -0700 | [diff] [blame] | 1522 | if (ops->set_flags) |
| 1523 | ops->set_flags += gd->reloc_off; |
Simon Glass | d063ce9 | 2021-02-04 21:21:56 -0700 | [diff] [blame] | 1524 | if (ops->get_flags) |
| 1525 | ops->get_flags += gd->reloc_off; |
Michal Simek | 5f7202c | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1526 | |
| 1527 | reloc_done++; |
| 1528 | } |
| 1529 | #endif |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1530 | |
Simon Glass | e418c63 | 2023-06-01 10:22:30 -0600 | [diff] [blame] | 1531 | if (CONFIG_IS_ENABLED(GPIO_HOG) && dev_has_ofnode(dev)) { |
Eddie James | 652b869 | 2022-02-07 17:09:01 -0600 | [diff] [blame] | 1532 | struct udevice *child; |
| 1533 | ofnode node; |
| 1534 | |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 1535 | dev_for_each_subnode(node, dev) { |
| 1536 | if (ofnode_read_bool(node, "gpio-hog")) { |
| 1537 | const char *name = ofnode_get_name(node); |
| 1538 | int ret; |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1539 | |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 1540 | ret = device_bind_driver_to_node(dev, |
| 1541 | "gpio_hog", |
| 1542 | name, node, |
| 1543 | &child); |
| 1544 | if (ret) |
| 1545 | return ret; |
Marek Vasut | ba3982f | 2022-09-22 17:53:26 +0200 | [diff] [blame] | 1546 | |
| 1547 | /* |
| 1548 | * Make sure gpio-hogs are probed after bind |
| 1549 | * since hogs can be essential to the hardware |
| 1550 | * system. |
| 1551 | */ |
| 1552 | dev_or_flags(child, DM_FLAG_PROBE_AFTER_BIND); |
Heiko Schocher | 58e4c38 | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 1553 | } |
Heiko Schocher | 39cb340 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1554 | } |
| 1555 | } |
Marek Vasut | ba3982f | 2022-09-22 17:53:26 +0200 | [diff] [blame] | 1556 | |
Michal Simek | 5f7202c | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1557 | return 0; |
| 1558 | } |
| 1559 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1560 | UCLASS_DRIVER(gpio) = { |
| 1561 | .id = UCLASS_GPIO, |
| 1562 | .name = "gpio", |
Bhuvanchandra DV | b1a1fc9 | 2015-06-01 18:37:15 +0530 | [diff] [blame] | 1563 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1564 | .post_probe = gpio_post_probe, |
Michal Simek | 5f7202c | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1565 | .post_bind = gpio_post_bind, |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1566 | .pre_remove = gpio_pre_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1567 | .per_device_auto = sizeof(struct gpio_dev_priv), |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1568 | }; |