blob: fc395c97a2892735ad5e3a7f1bf9a7a95dec5d86 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse821d182014-02-26 15:59:24 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glasse821d182014-02-26 15:59:24 -07004 */
5
Simon Glasse43eae22021-02-04 21:22:08 -07006#define LOG_CATEGORY UCLASS_GPIO
7
Simon Glasse821d182014-02-26 15:59:24 -07008#include <common.h>
9#include <dm.h>
Simon Glass2149e112021-08-07 07:24:12 -060010#include <dt-structs.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Jean-Jacques Hiblot775d6a22020-09-11 13:43:34 +053012#include <dm/devres.h>
13#include <dm/device_compat.h>
Heiko Schocher39cb3402019-06-12 06:11:46 +020014#include <dm/device-internal.h>
15#include <dm/lists.h>
16#include <dm/uclass-internal.h>
Eric Nelson786e98d2016-04-24 16:32:40 -070017#include <dt-bindings/gpio/gpio.h>
Simon Glasse821d182014-02-26 15:59:24 -070018#include <errno.h>
Simon Glassd3322bb2015-01-05 20:05:28 -070019#include <fdtdec.h>
Simon Glass0f4517d2014-10-04 11:29:42 -060020#include <malloc.h>
Simon Glass3176b6c2020-07-07 13:11:44 -060021#include <acpi/acpi_device.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Simon Glasse821d182014-02-26 15:59:24 -070023#include <asm/gpio.h>
Simon Glass0f2af882020-05-10 11:40:05 -060024#include <dm/device_compat.h>
Masahiro Yamada78eeb912016-01-24 23:27:48 +090025#include <linux/bug.h>
Simon Glass43b2e1a2014-10-22 21:37:01 -060026#include <linux/ctype.h>
Simon Glass247ccf22021-02-04 21:22:09 -070027#include <linux/delay.h>
Simon Glasse821d182014-02-26 15:59:24 -070028
Simon Glass16e10402015-01-05 20:05:29 -070029DECLARE_GLOBAL_DATA_PTR;
30
Marek Vasut3a188472023-08-02 01:26:02 +020031#define GPIO_ALLOC_BITS 32
32
Simon Glasse821d182014-02-26 15:59:24 -070033/**
Patrick Delaunay758bba92020-01-13 11:35:01 +010034 * 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 */
40static 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 Glasse821d182014-02-26 15:59:24 -070050 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glasse821d182014-02-26 15:59:24 -070051 *
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 Glassce555292015-01-05 20:05:27 -070055 *
56 * @gpio: The numeric representation of the GPIO
57 * @desc: Returns description (desc->flags will always be 0)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010058 * Return: 0 if found, -ENOENT if not found
Simon Glasse821d182014-02-26 15:59:24 -070059 */
Simon Glassce555292015-01-05 20:05:27 -070060static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070061{
62 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020063 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -070064
Michal Suchanek91c96fe2022-10-12 21:58:08 +020065 for (uclass_first_device(UCLASS_GPIO, &dev);
Simon Glasse821d182014-02-26 15:59:24 -070066 dev;
Michal Suchanek91c96fe2022-10-12 21:58:08 +020067 uclass_next_device(&dev)) {
Simon Glassde0977b2015-03-05 12:25:20 -070068 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -070069 if (gpio >= uc_priv->gpio_base &&
70 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Patrick Delaunay758bba92020-01-13 11:35:01 +010071 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
Simon Glasse821d182014-02-26 15:59:24 -070072 return 0;
73 }
74 }
75
76 /* No such GPIO */
Michal Suchanek91c96fe2022-10-12 21:58:08 +020077 return -ENOENT;
Simon Glasse821d182014-02-26 15:59:24 -070078}
79
Marek Vasut3a188472023-08-02 01:26:02 +020080/**
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 */
89static 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 */
102static 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 */
115static 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 Schochera3e793c2020-05-22 11:08:59 +0200120#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 */
132static int dm_gpio_lookup_label(const char *name,
133 struct gpio_dev_priv *uc_priv, ulong *offset)
134{
Heiko Schochera3e793c2020-05-22 11:08:59 +0200135 int i;
136
137 *offset = -1;
Heiko Schochera3e793c2020-05-22 11:08:59 +0200138 for (i = 0; i < uc_priv->gpio_count; i++) {
Marek Vasut3a188472023-08-02 01:26:02 +0200139 if (!gpio_is_claimed(uc_priv, i))
Heiko Schochera3e793c2020-05-22 11:08:59 +0200140 continue;
Rasmus Villemoes48828082022-10-03 11:02:45 +0200141 if (!strcmp(name, uc_priv->name[i])) {
Heiko Schochera3e793c2020-05-22 11:08:59 +0200142 *offset = i;
143 return 0;
144 }
145 }
146 return -ENOENT;
147}
148#else
149static int
150dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
151 ulong *offset)
152{
153 return -ENOENT;
154}
155#endif
156
Simon Glass215bcc72015-06-23 15:38:40 -0600157int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -0700158{
Simon Glass43b2e1a2014-10-22 21:37:01 -0600159 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200160 struct udevice *dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600161 ulong offset;
162 int numeric;
Simon Glasse821d182014-02-26 15:59:24 -0700163
Simon Glassff9b9032021-07-24 09:03:30 -0600164 numeric = isdigit(*name) ? dectoul(name, NULL) : -1;
Michal Suchanek91c96fe2022-10-12 21:58:08 +0200165 for (uclass_first_device(UCLASS_GPIO, &dev);
Simon Glasse821d182014-02-26 15:59:24 -0700166 dev;
Michal Suchanek91c96fe2022-10-12 21:58:08 +0200167 uclass_next_device(&dev)) {
Simon Glasse821d182014-02-26 15:59:24 -0700168 int len;
169
Simon Glassde0977b2015-03-05 12:25:20 -0700170 uc_priv = dev_get_uclass_priv(dev);
Simon Glass43b2e1a2014-10-22 21:37:01 -0600171 if (numeric != -1) {
172 offset = numeric - uc_priv->gpio_base;
173 /* Allow GPIOs to be numbered from 0 */
Tom Rini26fd9682017-05-10 15:20:15 -0400174 if (offset < uc_priv->gpio_count)
Simon Glass43b2e1a2014-10-22 21:37:01 -0600175 break;
176 }
177
Simon Glasse821d182014-02-26 15:59:24 -0700178 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
179
Simon Glassd4acf632014-06-11 23:29:47 -0600180 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glass43b2e1a2014-10-22 21:37:01 -0600181 if (!strict_strtoul(name + len, 10, &offset))
Samuel Holland8f53a332021-09-11 17:05:51 -0500182 if (offset < uc_priv->gpio_count)
183 break;
Simon Glasse821d182014-02-26 15:59:24 -0700184 }
Heiko Schochera3e793c2020-05-22 11:08:59 +0200185
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 Glasse821d182014-02-26 15:59:24 -0700192 }
193
Simon Glass43b2e1a2014-10-22 21:37:01 -0600194 if (!dev)
Michal Suchanek91c96fe2022-10-12 21:58:08 +0200195 return -EINVAL;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600196
Patrick Delaunay758bba92020-01-13 11:35:01 +0100197 gpio_desc_init(desc, dev, offset);
Simon Glass215bcc72015-06-23 15:38:40 -0600198
199 return 0;
200}
201
202int 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 Glass43b2e1a2014-10-22 21:37:01 -0600214 if (devp)
Simon Glass215bcc72015-06-23 15:38:40 -0600215 *devp = desc.dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600216 if (offsetp)
Simon Glass215bcc72015-06-23 15:38:40 -0600217 *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 Glass43b2e1a2014-10-22 21:37:01 -0600223
224 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700225}
226
Samuel Holland8e10c712021-09-11 17:05:53 -0500227unsigned 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 Glass12faa022017-05-18 20:09:18 -0600255int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
256 struct ofnode_phandle_args *args)
Eric Nelson786e98d2016-04-24 16:32:40 -0700257{
Samuel Holland78acb922021-09-11 17:05:52 -0500258 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
259
Eric Nelson786e98d2016-04-24 16:32:40 -0700260 if (args->args_count < 1)
261 return -EINVAL;
262
263 desc->offset = args->args[0];
Samuel Holland78acb922021-09-11 17:05:52 -0500264 if (desc->offset >= uc_priv->gpio_count)
265 return -EINVAL;
Eric Nelson786e98d2016-04-24 16:32:40 -0700266
267 if (args->args_count < 2)
268 return 0;
269
Samuel Holland8e10c712021-09-11 17:05:53 -0500270 desc->flags = gpio_flags_xlate(args->args[1]);
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100271
Eric Nelson786e98d2016-04-24 16:32:40 -0700272 return 0;
273}
274
Simon Glass16e10402015-01-05 20:05:29 -0700275static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600276 struct ofnode_phandle_args *args)
Simon Glassd3322bb2015-01-05 20:05:28 -0700277{
Simon Glass49f315a2021-02-04 21:22:05 -0700278 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassd3322bb2015-01-05 20:05:28 -0700279
Eric Nelson786e98d2016-04-24 16:32:40 -0700280 if (ops->xlate)
281 return ops->xlate(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700282 else
Eric Nelson786e98d2016-04-24 16:32:40 -0700283 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700284}
285
Simon Glass2149e112021-08-07 07:24:12 -0600286#if CONFIG_IS_ENABLED(GPIO_HOG)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200287
288struct gpio_hog_priv {
289 struct gpio_desc gpiod;
290};
291
292struct gpio_hog_data {
293 int gpiod_flags;
294 int value;
295 u32 val[2];
296};
297
Simon Glassaad29ae2020-12-03 16:55:21 -0700298static int gpio_hog_of_to_plat(struct udevice *dev)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200299{
Simon Glassfa20e932020-12-03 16:55:20 -0700300 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200301 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 Schocher58e4c382019-07-17 06:59:51 +0200323 if (nodename)
324 device_set_name(dev, nodename);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200325
326 return 0;
327}
328
329static int gpio_hog_probe(struct udevice *dev)
330{
Simon Glassfa20e932020-12-03 16:55:20 -0700331 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200332 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 Schocher58e4c382019-07-17 06:59:51 +0200343
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 Schocher39cb3402019-06-12 06:11:46 +0200352
353 return 0;
354}
355
Heiko Schocher58e4c382019-07-17 06:59:51 +0200356int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200357{
358 struct udevice *dev;
359
Heiko Schocher58e4c382019-07-17 06:59:51 +0200360 *desc = NULL;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200361 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
362 struct gpio_hog_priv *priv = dev_get_priv(dev);
363
Heiko Schocher58e4c382019-07-17 06:59:51 +0200364 *desc = &priv->gpiod;
365 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200366 }
367
Heiko Schocher58e4c382019-07-17 06:59:51 +0200368 return -ENODEV;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200369}
370
371U_BOOT_DRIVER(gpio_hog) = {
372 .name = "gpio_hog",
373 .id = UCLASS_NOP,
Simon Glassaad29ae2020-12-03 16:55:21 -0700374 .of_to_plat = gpio_hog_of_to_plat,
Heiko Schocher39cb3402019-06-12 06:11:46 +0200375 .probe = gpio_hog_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700376 .priv_auto = sizeof(struct gpio_hog_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700377 .plat_auto = sizeof(struct gpio_hog_data),
Heiko Schocher39cb3402019-06-12 06:11:46 +0200378};
379#else
Heiko Schocher58e4c382019-07-17 06:59:51 +0200380int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200381{
Heiko Schocher58e4c382019-07-17 06:59:51 +0200382 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200383}
384#endif
385
Simon Glass047cdb32015-06-23 15:38:41 -0600386int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassce555292015-01-05 20:05:27 -0700387{
Simon Glass49f315a2021-02-04 21:22:05 -0700388 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700389 struct udevice *dev = desc->dev;
390 struct gpio_dev_priv *uc_priv;
391 char *str;
392 int ret;
393
Simon Glassde0977b2015-03-05 12:25:20 -0700394 uc_priv = dev_get_uclass_priv(dev);
Marek Vasut3a188472023-08-02 01:26:02 +0200395 if (gpio_is_claimed(uc_priv, desc->offset))
Simon Glassce555292015-01-05 20:05:27 -0700396 return -EBUSY;
397 str = strdup(label);
398 if (!str)
399 return -ENOMEM;
Simon Glass49f315a2021-02-04 21:22:05 -0700400 if (ops->request) {
401 ret = ops->request(dev, desc->offset, label);
Simon Glassce555292015-01-05 20:05:27 -0700402 if (ret) {
403 free(str);
404 return ret;
405 }
406 }
Marek Vasut3a188472023-08-02 01:26:02 +0200407
408 gpio_set_claim(uc_priv, desc->offset);
Simon Glassce555292015-01-05 20:05:27 -0700409 uc_priv->name[desc->offset] = str;
410
411 return 0;
412}
413
Simon Glass16e10402015-01-05 20:05:29 -0700414static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
415{
Simon Glass7611ac62019-09-25 08:56:27 -0600416#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass16e10402015-01-05 20:05:29 -0700417 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 Glassee8a3d92015-12-29 05:22:48 -0700424#else
425 return dm_gpio_request(desc, fmt);
426#endif
Simon Glass16e10402015-01-05 20:05:29 -0700427}
428
Simon Glasse821d182014-02-26 15:59:24 -0700429/**
430 * gpio_request() - [COMPAT] Request GPIO
431 * gpio: GPIO number
432 * label: Name for the requested GPIO
433 *
Simon Glass0f4517d2014-10-04 11:29:42 -0600434 * The label is copied and allocated so the caller does not need to keep
435 * the pointer around.
436 *
Simon Glasse821d182014-02-26 15:59:24 -0700437 * 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 */
441int gpio_request(unsigned gpio, const char *label)
442{
Simon Glassce555292015-01-05 20:05:27 -0700443 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700444 int ret;
445
Simon Glassce555292015-01-05 20:05:27 -0700446 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700447 if (ret)
448 return ret;
449
Simon Glassce555292015-01-05 20:05:27 -0700450 return dm_gpio_request(&desc, label);
Simon Glasse821d182014-02-26 15:59:24 -0700451}
452
453/**
Simon Glass1b27d602014-10-04 11:29:49 -0600454 * 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 */
463int gpio_requestf(unsigned gpio, const char *fmt, ...)
464{
Simon Glass7611ac62019-09-25 08:56:27 -0600465#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass1b27d602014-10-04 11:29:49 -0600466 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 Glassee8a3d92015-12-29 05:22:48 -0700473#else
474 return gpio_request(gpio, fmt);
475#endif
Simon Glass1b27d602014-10-04 11:29:49 -0600476}
477
Simon Glassce555292015-01-05 20:05:27 -0700478int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glasse821d182014-02-26 15:59:24 -0700479{
Simon Glass49f315a2021-02-04 21:22:05 -0700480 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600481 struct gpio_dev_priv *uc_priv;
Simon Glasse821d182014-02-26 15:59:24 -0700482 int ret;
483
Simon Glassde0977b2015-03-05 12:25:20 -0700484 uc_priv = dev_get_uclass_priv(dev);
Marek Vasut3a188472023-08-02 01:26:02 +0200485 if (!gpio_is_claimed(uc_priv, offset))
Simon Glass0f4517d2014-10-04 11:29:42 -0600486 return -ENXIO;
Simon Glass49f315a2021-02-04 21:22:05 -0700487 if (ops->rfree) {
488 ret = ops->rfree(dev, offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600489 if (ret)
490 return ret;
491 }
492
Marek Vasut3a188472023-08-02 01:26:02 +0200493 gpio_clear_claim(uc_priv, offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600494 free(uc_priv->name[offset]);
495 uc_priv->name[offset] = NULL;
496
497 return 0;
498}
499
Simon Glassce555292015-01-05 20:05:27 -0700500/**
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 */
508int 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 Glassfd838972016-03-06 19:27:51 -0700520static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glass0f4517d2014-10-04 11:29:42 -0600521{
Simon Glass230c1432015-07-02 18:16:16 -0600522 struct gpio_dev_priv *uc_priv;
523
524 if (!dm_gpio_is_valid(desc))
525 return -ENOENT;
Simon Glass0f4517d2014-10-04 11:29:42 -0600526
Simon Glass230c1432015-07-02 18:16:16 -0600527 uc_priv = dev_get_uclass_priv(desc->dev);
Marek Vasut3a188472023-08-02 01:26:02 +0200528 if (!gpio_is_claimed(uc_priv, desc->offset)) {
Simon Glass0f4517d2014-10-04 11:29:42 -0600529 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassce555292015-01-05 20:05:27 -0700530 desc->dev->name, func,
531 uc_priv->bank_name ? uc_priv->bank_name : "",
532 desc->offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600533 return -EBUSY;
534 }
535
536 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700537}
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 */
547int gpio_direction_input(unsigned gpio)
548{
Simon Glassce555292015-01-05 20:05:27 -0700549 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700550 int ret;
551
Simon Glassce555292015-01-05 20:05:27 -0700552 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700553 if (ret)
554 return ret;
555
Simon Glass722f9682021-02-04 21:22:04 -0700556 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
Simon Glasse821d182014-02-26 15:59:24 -0700557}
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 */
568int gpio_direction_output(unsigned gpio, int value)
569{
Simon Glassce555292015-01-05 20:05:27 -0700570 struct gpio_desc desc;
Simon Glass722f9682021-02-04 21:22:04 -0700571 ulong flags;
Simon Glassce555292015-01-05 20:05:27 -0700572 int ret;
573
574 ret = gpio_to_device(gpio, &desc);
575 if (ret)
576 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700577
Simon Glass722f9682021-02-04 21:22:04 -0700578 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 Glassce555292015-01-05 20:05:27 -0700582}
583
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100584static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassce555292015-01-05 20:05:27 -0700585{
Simon Glass49f315a2021-02-04 21:22:05 -0700586 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700587 int value;
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100588
Simon Glass49f315a2021-02-04 21:22:05 -0700589 value = ops->get_value(desc->dev, desc->offset);
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100590
591 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
592}
593
594int dm_gpio_get_value(const struct gpio_desc *desc)
595{
Simon Glassce555292015-01-05 20:05:27 -0700596 int ret;
597
598 ret = check_reserved(desc, "get_value");
599 if (ret)
600 return ret;
601
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100602 return _gpio_get_value(desc);
Simon Glassce555292015-01-05 20:05:27 -0700603}
604
Simon Glassfd838972016-03-06 19:27:51 -0700605int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassce555292015-01-05 20:05:27 -0700606{
Simon Glass7b893f92021-02-04 21:22:03 -0700607 const struct dm_gpio_ops *ops;
Simon Glassce555292015-01-05 20:05:27 -0700608 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 Armstrongf51b8da2020-05-05 10:43:17 +0200616
Simon Glass7b893f92021-02-04 21:22:03 -0700617 /* 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 Armstrongf51b8da2020-05-05 10:43:17 +0200629 /*
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 Glass7b893f92021-02-04 21:22:03 -0700635 return ops->direction_input(desc->dev, desc->offset);
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200636 else if (desc->flags & GPIOD_OPEN_DRAIN ||
637 desc->flags & GPIOD_OPEN_SOURCE)
Simon Glass7b893f92021-02-04 21:22:03 -0700638 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 Armstrongf51b8da2020-05-05 10:43:17 +0200643
Simon Glassce555292015-01-05 20:05:27 -0700644 return 0;
645}
646
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100647/* check dir flags invalid configuration */
648static 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 Delaunay5c1c06e2020-01-13 11:35:07 +0100656 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 Delaunay187c45f2020-01-13 11:35:04 +0100668 return 0;
669}
670
Simon Glass7b893f92021-02-04 21:22:03 -0700671/**
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 Schuchardt47b4c022022-01-19 18:05:50 +0100680 * Return: 0 if OK, -ve on error
Simon Glass7b893f92021-02-04 21:22:03 -0700681 */
Simon Glass54befdd2021-02-04 21:21:55 -0700682static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
Simon Glassce555292015-01-05 20:05:27 -0700683{
684 struct udevice *dev = desc->dev;
Simon Glass49f315a2021-02-04 21:22:05 -0700685 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100686 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100687 int ret = 0;
Simon Glasse821d182014-02-26 15:59:24 -0700688
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100689 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 Glass7b893f92021-02-04 21:22:03 -0700700 /* 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 Glass54befdd2021-02-04 21:21:55 -0700705 /* GPIOD_ are directly managed by driver in set_flags */
706 if (ops->set_flags) {
707 ret = ops->set_flags(dev, desc->offset, flags);
Patrick Delaunay684326f2020-01-13 11:35:09 +0100708 } else {
709 if (flags & GPIOD_IS_OUT) {
Simon Glass7b893f92021-02-04 21:22:03 -0700710 bool value = flags & GPIOD_IS_OUT_ACTIVE;
711
712 ret = ops->direction_output(dev, desc->offset, value);
Patrick Delaunay684326f2020-01-13 11:35:09 +0100713 } else if (flags & GPIOD_IS_IN) {
714 ret = ops->direction_input(dev, desc->offset);
715 }
Simon Glassce555292015-01-05 20:05:27 -0700716 }
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100717
718 return ret;
719}
720
Simon Glass7b893f92021-02-04 21:22:03 -0700721int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100722{
Simon Glass7b893f92021-02-04 21:22:03 -0700723 ulong flags;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100724 int ret;
725
726 ret = check_reserved(desc, "set_dir_flags");
Simon Glassce555292015-01-05 20:05:27 -0700727 if (ret)
728 return ret;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100729
Simon Glass7b893f92021-02-04 21:22:03 -0700730 flags = (desc->flags & ~clr) | set;
731
Simon Glass54befdd2021-02-04 21:21:55 -0700732 ret = _dm_gpio_set_flags(desc, flags);
Simon Glass7b893f92021-02-04 21:22:03 -0700733 if (ret)
734 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700735
Simon Glass7b893f92021-02-04 21:22:03 -0700736 /* save the flags also in descriptor */
737 desc->flags = flags;
738
739 return 0;
740}
741
742int 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 Glassce555292015-01-05 20:05:27 -0700746}
747
Simon Glass247ccf22021-02-04 21:22:09 -0700748int 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 Glass909bee32021-02-04 21:21:57 -0700763int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100764{
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100765 struct udevice *dev = desc->dev;
766 int ret, value;
Simon Glass49f315a2021-02-04 21:22:05 -0700767 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glassd063ce92021-02-04 21:21:56 -0700768 ulong flags;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100769
Simon Glassd063ce92021-02-04 21:21:56 -0700770 ret = check_reserved(desc, "get_flags");
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100771 if (ret)
772 return ret;
773
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100774 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
Simon Glassd063ce92021-02-04 21:21:56 -0700775 if (ops->get_flags) {
776 ret = ops->get_flags(dev, desc->offset, &flags);
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100777 if (ret)
778 return ret;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100779
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100780 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
Simon Glassd063ce92021-02-04 21:21:56 -0700781 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100782 if (desc->flags & GPIOD_ACTIVE_LOW)
783 value = !value;
Simon Glassd063ce92021-02-04 21:21:56 -0700784 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
785 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100786 if (value)
Simon Glassd063ce92021-02-04 21:21:56 -0700787 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100788 } else {
Simon Glassd063ce92021-02-04 21:21:56 -0700789 flags = desc->flags;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100790 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
Simon Glassd063ce92021-02-04 21:21:56 -0700791 flags &= ~GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100792 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
Simon Glassd063ce92021-02-04 21:21:56 -0700793 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100794 }
Simon Glassd063ce92021-02-04 21:21:56 -0700795 *flagsp = flags;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100796
797 return 0;
798}
799
Simon Glasse821d182014-02-26 15:59:24 -0700800/**
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 */
809int gpio_get_value(unsigned gpio)
810{
Simon Glasse821d182014-02-26 15:59:24 -0700811 int ret;
812
Simon Glassce555292015-01-05 20:05:27 -0700813 struct gpio_desc desc;
814
815 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700816 if (ret)
817 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700818 return dm_gpio_get_value(&desc);
Simon Glasse821d182014-02-26 15:59:24 -0700819}
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 */
830int gpio_set_value(unsigned gpio, int value)
831{
Simon Glassce555292015-01-05 20:05:27 -0700832 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700833 int ret;
834
Simon Glassce555292015-01-05 20:05:27 -0700835 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700836 if (ret)
837 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700838 return dm_gpio_set_value(&desc, value);
Simon Glasse821d182014-02-26 15:59:24 -0700839}
840
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200841const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glasse821d182014-02-26 15:59:24 -0700842{
843 struct gpio_dev_priv *priv;
844
845 /* Must be called on an active device */
Simon Glassde0977b2015-03-05 12:25:20 -0700846 priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700847 assert(priv);
848
849 *bit_count = priv->gpio_count;
850 return priv->bank_name;
851}
852
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600853static const char * const gpio_function[GPIOF_COUNT] = {
854 "input",
855 "output",
856 "unused",
857 "unknown",
858 "func",
859};
860
Masahiro Yamada286c2522017-06-22 16:50:25 +0900861static int get_function(struct udevice *dev, int offset, bool skip_unused,
862 const char **namep)
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600863{
Simon Glassde0977b2015-03-05 12:25:20 -0700864 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass49f315a2021-02-04 21:22:05 -0700865 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600866
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 Vasut3a188472023-08-02 01:26:02 +0200874 if (skip_unused && !gpio_is_claimed(uc_priv, offset))
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600875 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
890int gpio_get_function(struct udevice *dev, int offset, const char **namep)
891{
892 return get_function(dev, offset, true, namep);
893}
894
895int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
896{
897 return get_function(dev, offset, false, namep);
898}
899
Simon Glass6b1ef592014-10-04 11:29:44 -0600900int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
901{
Simon Glass49f315a2021-02-04 21:22:05 -0700902 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600903 struct gpio_dev_priv *priv;
904 char *str = buf;
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200905 const char *label;
Simon Glass6b1ef592014-10-04 11:29:44 -0600906 int func;
907 int ret;
908 int len;
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200909 bool used;
Simon Glass6b1ef592014-10-04 11:29:44 -0600910
911 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
912
913 *buf = 0;
Simon Glassde0977b2015-03-05 12:25:20 -0700914 priv = dev_get_uclass_priv(dev);
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200915 ret = gpio_get_raw_function(dev, offset, &label);
Simon Glass6b1ef592014-10-04 11:29:44 -0600916 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 Glass6b1ef592014-10-04 11:29:44 -0600922
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200923 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 Glass6b1ef592014-10-04 11:29:44 -0600930 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 Chotardeb8f3a72022-08-30 14:09:12 +0200937 label ? " " : "",
Simon Glass6b1ef592014-10-04 11:29:44 -0600938 label ? label : "");
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200939 break;
Simon Glass6b1ef592014-10-04 11:29:44 -0600940 }
941
942 return 0;
943}
944
Simon Glass3176b6c2020-07-07 13:11:44 -0600945#if CONFIG_IS_ENABLED(ACPIGEN)
946int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
947{
Simon Glass49f315a2021-02-04 21:22:05 -0700948 const struct dm_gpio_ops *ops;
Simon Glass3176b6c2020-07-07 13:11:44 -0600949
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 Glassbef54db2015-04-14 21:03:20 -0600966int 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;
986err:
987 for (i--; i >= 0; i--)
988 gpio_free(gpio_num_array[i]);
989
990 return ret;
991}
992
Simon Glass2c97a8f2014-11-10 18:00:21 -0700993/*
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 Glassbef54db2015-04-14 21:03:20 -0600997int gpio_get_values_as_int(const int *gpio_list)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700998{
999 int gpio;
1000 unsigned bitmask = 1;
1001 unsigned vector = 0;
Simon Glassbef54db2015-04-14 21:03:20 -06001002 int ret;
Simon Glass2c97a8f2014-11-10 18:00:21 -07001003
1004 while (bitmask &&
Simon Glassbef54db2015-04-14 21:03:20 -06001005 ((gpio = *gpio_list++) != -1)) {
1006 ret = gpio_get_value(gpio);
1007 if (ret < 0)
1008 return ret;
1009 else if (ret)
Simon Glass2c97a8f2014-11-10 18:00:21 -07001010 vector |= bitmask;
1011 bitmask <<= 1;
1012 }
Simon Glassbef54db2015-04-14 21:03:20 -06001013
Simon Glass2c97a8f2014-11-10 18:00:21 -07001014 return vector;
1015}
1016
Simon Glassfd838972016-03-06 19:27:51 -07001017int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassdf1687d2016-03-06 19:27:50 -07001018{
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 Glass247ccf22021-02-04 21:22:09 -07001031
1032 return vector;
1033}
1034
1035int 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 Glassdf1687d2016-03-06 19:27:50 -07001096
1097 return vector;
1098}
1099
Heiko Schocher58e4c382019-07-17 06:59:51 +02001100/**
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 Schocher39cb3402019-06-12 06:11:46 +02001132static int gpio_request_tail(int ret, const char *nodename,
Simon Glass12faa022017-05-18 20:09:18 -06001133 struct ofnode_phandle_args *args,
1134 const char *list_name, int index,
Heiko Schocher39cb3402019-06-12 06:11:46 +02001135 struct gpio_desc *desc, int flags,
Heiko Schocher58e4c382019-07-17 06:59:51 +02001136 bool add_index, struct udevice *gpio_dev)
Simon Glass16e10402015-01-05 20:05:29 -07001137{
Patrick Delaunay758bba92020-01-13 11:35:01 +01001138 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass12faa022017-05-18 20:09:18 -06001139 if (ret)
Simon Glass16e10402015-01-05 20:05:29 -07001140 goto err;
Simon Glass16e10402015-01-05 20:05:29 -07001141
Heiko Schocher39cb3402019-06-12 06:11:46 +02001142 if (!desc->dev) {
1143 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1144 &desc->dev);
1145 if (ret) {
Heiko Schocher58e4c382019-07-17 06:59:51 +02001146 debug("%s: uclass_get_device_by_ofnode failed\n",
1147 __func__);
Heiko Schocher39cb3402019-06-12 06:11:46 +02001148 goto err;
1149 }
Simon Glass16e10402015-01-05 20:05:29 -07001150 }
Simon Glass12faa022017-05-18 20:09:18 -06001151 ret = gpio_find_and_xlate(desc, args);
Simon Glass16e10402015-01-05 20:05:29 -07001152 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 Schocher39cb3402019-06-12 06:11:46 +02001157 nodename, list_name, index);
Simon Glass16e10402015-01-05 20:05:29 -07001158 if (ret) {
1159 debug("%s: dm_gpio_requestf failed\n", __func__);
1160 goto err;
1161 }
Simon Glass7b893f92021-02-04 21:22:03 -07001162
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 Glass16e10402015-01-05 20:05:29 -07001166 if (ret) {
1167 debug("%s: dm_gpio_set_dir failed\n", __func__);
1168 goto err;
1169 }
1170
1171 return 0;
1172err:
1173 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher39cb3402019-06-12 06:11:46 +02001174 __func__, nodename, list_name, index, ret);
Simon Glass16e10402015-01-05 20:05:29 -07001175 return ret;
1176}
1177
Simon Glass92882652021-08-07 07:24:04 -06001178#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass1d9af1f2017-05-30 21:47:09 -06001179static 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 Glass12faa022017-05-18 20:09:18 -06001182{
1183 struct ofnode_phandle_args args;
1184 int ret;
1185
Simon Glass1d9af1f2017-05-30 21:47:09 -06001186 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1187 index, &args);
Simon Glass12faa022017-05-18 20:09:18 -06001188
Heiko Schocher39cb3402019-06-12 06:11:46 +02001189 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1190 index, desc, flags, add_index, NULL);
Simon Glass12faa022017-05-18 20:09:18 -06001191}
1192
Simon Glass1d9af1f2017-05-30 21:47:09 -06001193int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -07001194 struct gpio_desc *desc, int flags)
1195{
Simon Glass1d9af1f2017-05-30 21:47:09 -06001196 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1197 index > 0);
Simon Glass16e10402015-01-05 20:05:29 -07001198}
1199
Simon Glass1d9af1f2017-05-30 21:47:09 -06001200int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -07001201 struct gpio_desc *desc, int flags)
1202{
Simon Glass1d9af1f2017-05-30 21:47:09 -06001203 struct ofnode_phandle_args args;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001204 ofnode node;
Simon Glass1d9af1f2017-05-30 21:47:09 -06001205 int ret;
1206
1207 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1208 index, &args);
Heiko Schocher39cb3402019-06-12 06:11:46 +02001209 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 Glass16e10402015-01-05 20:05:29 -07001212}
1213
Andrew Jeffery2e62c6b2022-01-31 13:54:05 +10301214int 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 Villemoesf4e744d2023-03-17 21:12:22 +01001219 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 Jeffery2e62c6b2022-01-31 13:54:05 +10301226 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 Glass1d9af1f2017-05-30 21:47:09 -06001247int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass16e10402015-01-05 20:05:29 -07001248 struct gpio_desc *desc, int max_count,
1249 int flags)
1250{
1251 int count;
1252 int ret;
1253
Przemyslaw Marczak44fc5362015-03-31 18:57:16 +02001254 for (count = 0; count < max_count; count++) {
Simon Glass1d9af1f2017-05-30 21:47:09 -06001255 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass16e10402015-01-05 20:05:29 -07001256 &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
1266err:
Rasmus Villemoesc9ca11c2023-04-19 12:10:13 +02001267 gpio_free_list_nodev(desc, count);
Simon Glass16e10402015-01-05 20:05:29 -07001268
1269 return ret;
1270}
1271
1272int 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 Glass1d9af1f2017-05-30 21:47:09 -06001281 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1282 max_count, flags);
Simon Glass16e10402015-01-05 20:05:29 -07001283}
1284
1285int gpio_get_list_count(struct udevice *dev, const char *list_name)
1286{
1287 int ret;
1288
Sean Anderson8ad66c52021-04-20 10:50:54 -04001289 ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells",
1290 -ENOENT);
1291 if (ret < 0) {
Simon Glass16e10402015-01-05 20:05:29 -07001292 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 Glassea383722021-02-04 21:21:54 -07001298#endif /* OF_PLATDATA */
Simon Glass16e10402015-01-05 20:05:29 -07001299
Simon Glass2149e112021-08-07 07:24:12 -06001300#if CONFIG_IS_ENABLED(OF_PLATDATA)
1301int 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 Glass16e10402015-01-05 20:05:29 -07001321int 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
1327int 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
1338int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1339{
1340 return gpio_free_list(NULL, desc, count);
1341}
1342
Simon Glasse821d182014-02-26 15:59:24 -07001343/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glass0f4517d2014-10-04 11:29:42 -06001344static int gpio_renumber(struct udevice *removed_dev)
Simon Glasse821d182014-02-26 15:59:24 -07001345{
1346 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001347 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -07001348 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 Glass0f4517d2014-10-04 11:29:42 -06001359 if (device_active(dev) && dev != removed_dev) {
Simon Glassde0977b2015-03-05 12:25:20 -07001360 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001361 uc_priv->gpio_base = base;
1362 base += uc_priv->gpio_count;
1363 }
1364 }
1365
1366 return 0;
1367}
1368
Simon Glassfd838972016-03-06 19:27:51 -07001369int gpio_get_number(const struct gpio_desc *desc)
Simon Glass94f54d12015-03-25 12:21:58 -06001370{
1371 struct udevice *dev = desc->dev;
1372 struct gpio_dev_priv *uc_priv;
1373
1374 if (!dev)
1375 return -1;
Simon Glass95588622020-12-22 19:30:28 -07001376 uc_priv = dev_get_uclass_priv(dev);
Simon Glass94f54d12015-03-25 12:21:58 -06001377
1378 return uc_priv->gpio_base + desc->offset;
1379}
1380
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001381static int gpio_post_probe(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001382{
Simon Glassde0977b2015-03-05 12:25:20 -07001383 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001384
1385 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1386 if (!uc_priv->name)
1387 return -ENOMEM;
Marek Vasut3a188472023-08-02 01:26:02 +02001388
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 Glass0f4517d2014-10-04 11:29:42 -06001396
1397 return gpio_renumber(NULL);
Simon Glasse821d182014-02-26 15:59:24 -07001398}
1399
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001400static int gpio_pre_remove(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001401{
Simon Glassde0977b2015-03-05 12:25:20 -07001402 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001403 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 Vasut3a188472023-08-02 01:26:02 +02001409 free(uc_priv->claimed);
Simon Glass0f4517d2014-10-04 11:29:42 -06001410 free(uc_priv->name);
1411
1412 return gpio_renumber(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001413}
1414
Heiko Schocher39cb3402019-06-12 06:11:46 +02001415int 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 Hiblot775d6a22020-09-11 13:43:34 +05301430static void devm_gpiod_release(struct udevice *dev, void *res)
1431{
1432 dm_gpio_free(dev, res);
1433}
1434
1435static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1436{
1437 return res == data;
1438}
1439
1440struct 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
1466end:
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
1478struct 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
1491void 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 Simek5f7202c2018-07-12 12:42:27 +02001499static 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 Glassb3a47542020-02-04 20:15:17 -07001508 if (ops->rfree)
1509 ops->rfree += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001510 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 Simek5f7202c2018-07-12 12:42:27 +02001518 if (ops->get_function)
1519 ops->get_function += gd->reloc_off;
1520 if (ops->xlate)
1521 ops->xlate += gd->reloc_off;
Simon Glass54befdd2021-02-04 21:21:55 -07001522 if (ops->set_flags)
1523 ops->set_flags += gd->reloc_off;
Simon Glassd063ce92021-02-04 21:21:56 -07001524 if (ops->get_flags)
1525 ops->get_flags += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001526
1527 reloc_done++;
1528 }
1529#endif
Heiko Schocher39cb3402019-06-12 06:11:46 +02001530
Simon Glasse418c632023-06-01 10:22:30 -06001531 if (CONFIG_IS_ENABLED(GPIO_HOG) && dev_has_ofnode(dev)) {
Eddie James652b8692022-02-07 17:09:01 -06001532 struct udevice *child;
1533 ofnode node;
1534
Heiko Schocher58e4c382019-07-17 06:59:51 +02001535 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 Schocher39cb3402019-06-12 06:11:46 +02001539
Heiko Schocher58e4c382019-07-17 06:59:51 +02001540 ret = device_bind_driver_to_node(dev,
1541 "gpio_hog",
1542 name, node,
1543 &child);
1544 if (ret)
1545 return ret;
Marek Vasutba3982f2022-09-22 17:53:26 +02001546
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 Schocher58e4c382019-07-17 06:59:51 +02001553 }
Heiko Schocher39cb3402019-06-12 06:11:46 +02001554 }
1555 }
Marek Vasutba3982f2022-09-22 17:53:26 +02001556
Michal Simek5f7202c2018-07-12 12:42:27 +02001557 return 0;
1558}
1559
Simon Glasse821d182014-02-26 15:59:24 -07001560UCLASS_DRIVER(gpio) = {
1561 .id = UCLASS_GPIO,
1562 .name = "gpio",
Bhuvanchandra DVb1a1fc92015-06-01 18:37:15 +05301563 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glasse821d182014-02-26 15:59:24 -07001564 .post_probe = gpio_post_probe,
Michal Simek5f7202c2018-07-12 12:42:27 +02001565 .post_bind = gpio_post_bind,
Simon Glasse821d182014-02-26 15:59:24 -07001566 .pre_remove = gpio_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001567 .per_device_auto = sizeof(struct gpio_dev_priv),
Simon Glasse821d182014-02-26 15:59:24 -07001568};