blob: a00880e446c8631de4b1d2128d2ded554b5fbe17 [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
Simon Glasse821d182014-02-26 15:59:24 -070031/**
Patrick Delaunay758bba92020-01-13 11:35:01 +010032 * gpio_desc_init() - Initialize the GPIO descriptor
33 *
34 * @desc: GPIO descriptor to initialize
35 * @dev: GPIO device
36 * @offset: Offset of device GPIO
37 */
38static void gpio_desc_init(struct gpio_desc *desc,
39 struct udevice *dev,
40 uint offset)
41{
42 desc->dev = dev;
43 desc->offset = offset;
44 desc->flags = 0;
45}
46
47/**
Simon Glasse821d182014-02-26 15:59:24 -070048 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glasse821d182014-02-26 15:59:24 -070049 *
50 * Convert the GPIO number to an entry in the list of GPIOs
51 * or GPIO blocks registered with the GPIO controller. Returns
52 * entry on success, NULL on error.
Simon Glassce555292015-01-05 20:05:27 -070053 *
54 * @gpio: The numeric representation of the GPIO
55 * @desc: Returns description (desc->flags will always be 0)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010056 * Return: 0 if found, -ENOENT if not found
Simon Glasse821d182014-02-26 15:59:24 -070057 */
Simon Glassce555292015-01-05 20:05:27 -070058static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070059{
60 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020061 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -070062 int ret;
63
64 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
65 dev;
66 ret = uclass_next_device(&dev)) {
Simon Glassde0977b2015-03-05 12:25:20 -070067 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -070068 if (gpio >= uc_priv->gpio_base &&
69 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Patrick Delaunay758bba92020-01-13 11:35:01 +010070 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
Simon Glasse821d182014-02-26 15:59:24 -070071 return 0;
72 }
73 }
74
75 /* No such GPIO */
Simon Glassce555292015-01-05 20:05:27 -070076 return ret ? ret : -ENOENT;
Simon Glasse821d182014-02-26 15:59:24 -070077}
78
Heiko Schochera3e793c2020-05-22 11:08:59 +020079#if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
80/**
81 * dm_gpio_lookup_label() - look for name in gpio device
82 *
83 * search in uc_priv, if there is a gpio with labelname same
84 * as name.
85 *
86 * @name: name which is searched
87 * @uc_priv: gpio_dev_priv pointer.
88 * @offset: gpio offset within the device
89 * @return: 0 if found, -ENOENT if not.
90 */
91static int dm_gpio_lookup_label(const char *name,
92 struct gpio_dev_priv *uc_priv, ulong *offset)
93{
94 int len;
95 int i;
96
97 *offset = -1;
98 len = strlen(name);
99 for (i = 0; i < uc_priv->gpio_count; i++) {
100 if (!uc_priv->name[i])
101 continue;
102 if (!strncmp(name, uc_priv->name[i], len)) {
103 *offset = i;
104 return 0;
105 }
106 }
107 return -ENOENT;
108}
109#else
110static int
111dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
112 ulong *offset)
113{
114 return -ENOENT;
115}
116#endif
117
Simon Glass215bcc72015-06-23 15:38:40 -0600118int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -0700119{
Simon Glass43b2e1a2014-10-22 21:37:01 -0600120 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200121 struct udevice *dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600122 ulong offset;
123 int numeric;
Simon Glasse821d182014-02-26 15:59:24 -0700124 int ret;
125
Simon Glassff9b9032021-07-24 09:03:30 -0600126 numeric = isdigit(*name) ? dectoul(name, NULL) : -1;
Simon Glasse821d182014-02-26 15:59:24 -0700127 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
128 dev;
129 ret = uclass_next_device(&dev)) {
Simon Glasse821d182014-02-26 15:59:24 -0700130 int len;
131
Simon Glassde0977b2015-03-05 12:25:20 -0700132 uc_priv = dev_get_uclass_priv(dev);
Simon Glass43b2e1a2014-10-22 21:37:01 -0600133 if (numeric != -1) {
134 offset = numeric - uc_priv->gpio_base;
135 /* Allow GPIOs to be numbered from 0 */
Tom Rini26fd9682017-05-10 15:20:15 -0400136 if (offset < uc_priv->gpio_count)
Simon Glass43b2e1a2014-10-22 21:37:01 -0600137 break;
138 }
139
Simon Glasse821d182014-02-26 15:59:24 -0700140 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
141
Simon Glassd4acf632014-06-11 23:29:47 -0600142 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glass43b2e1a2014-10-22 21:37:01 -0600143 if (!strict_strtoul(name + len, 10, &offset))
Samuel Holland8f53a332021-09-11 17:05:51 -0500144 if (offset < uc_priv->gpio_count)
145 break;
Simon Glasse821d182014-02-26 15:59:24 -0700146 }
Heiko Schochera3e793c2020-05-22 11:08:59 +0200147
148 /*
149 * if we did not found a gpio through its bank
150 * name, we search for a valid gpio label.
151 */
152 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
153 break;
Simon Glasse821d182014-02-26 15:59:24 -0700154 }
155
Simon Glass43b2e1a2014-10-22 21:37:01 -0600156 if (!dev)
157 return ret ? ret : -EINVAL;
158
Patrick Delaunay758bba92020-01-13 11:35:01 +0100159 gpio_desc_init(desc, dev, offset);
Simon Glass215bcc72015-06-23 15:38:40 -0600160
161 return 0;
162}
163
164int gpio_lookup_name(const char *name, struct udevice **devp,
165 unsigned int *offsetp, unsigned int *gpiop)
166{
167 struct gpio_desc desc;
168 int ret;
169
170 if (devp)
171 *devp = NULL;
172 ret = dm_gpio_lookup_name(name, &desc);
173 if (ret)
174 return ret;
175
Simon Glass43b2e1a2014-10-22 21:37:01 -0600176 if (devp)
Simon Glass215bcc72015-06-23 15:38:40 -0600177 *devp = desc.dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600178 if (offsetp)
Simon Glass215bcc72015-06-23 15:38:40 -0600179 *offsetp = desc.offset;
180 if (gpiop) {
181 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
182
183 *gpiop = uc_priv->gpio_base + desc.offset;
184 }
Simon Glass43b2e1a2014-10-22 21:37:01 -0600185
186 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700187}
188
Samuel Holland8e10c712021-09-11 17:05:53 -0500189unsigned long gpio_flags_xlate(uint32_t arg)
190{
191 unsigned long flags = 0;
192
193 if (arg & GPIO_ACTIVE_LOW)
194 flags |= GPIOD_ACTIVE_LOW;
195
196 /*
197 * need to test 2 bits for gpio output binding:
198 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
199 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
200 */
201 if (arg & GPIO_SINGLE_ENDED) {
202 if (arg & GPIO_LINE_OPEN_DRAIN)
203 flags |= GPIOD_OPEN_DRAIN;
204 else
205 flags |= GPIOD_OPEN_SOURCE;
206 }
207
208 if (arg & GPIO_PULL_UP)
209 flags |= GPIOD_PULL_UP;
210
211 if (arg & GPIO_PULL_DOWN)
212 flags |= GPIOD_PULL_DOWN;
213
214 return flags;
215}
216
Simon Glass12faa022017-05-18 20:09:18 -0600217int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
218 struct ofnode_phandle_args *args)
Eric Nelson786e98d2016-04-24 16:32:40 -0700219{
Samuel Holland78acb922021-09-11 17:05:52 -0500220 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
221
Eric Nelson786e98d2016-04-24 16:32:40 -0700222 if (args->args_count < 1)
223 return -EINVAL;
224
225 desc->offset = args->args[0];
Samuel Holland78acb922021-09-11 17:05:52 -0500226 if (desc->offset >= uc_priv->gpio_count)
227 return -EINVAL;
Eric Nelson786e98d2016-04-24 16:32:40 -0700228
229 if (args->args_count < 2)
230 return 0;
231
Samuel Holland8e10c712021-09-11 17:05:53 -0500232 desc->flags = gpio_flags_xlate(args->args[1]);
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100233
Eric Nelson786e98d2016-04-24 16:32:40 -0700234 return 0;
235}
236
Simon Glass16e10402015-01-05 20:05:29 -0700237static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600238 struct ofnode_phandle_args *args)
Simon Glassd3322bb2015-01-05 20:05:28 -0700239{
Simon Glass49f315a2021-02-04 21:22:05 -0700240 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassd3322bb2015-01-05 20:05:28 -0700241
Eric Nelson786e98d2016-04-24 16:32:40 -0700242 if (ops->xlate)
243 return ops->xlate(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700244 else
Eric Nelson786e98d2016-04-24 16:32:40 -0700245 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700246}
247
Simon Glass2149e112021-08-07 07:24:12 -0600248#if CONFIG_IS_ENABLED(GPIO_HOG)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200249
250struct gpio_hog_priv {
251 struct gpio_desc gpiod;
252};
253
254struct gpio_hog_data {
255 int gpiod_flags;
256 int value;
257 u32 val[2];
258};
259
Simon Glassaad29ae2020-12-03 16:55:21 -0700260static int gpio_hog_of_to_plat(struct udevice *dev)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200261{
Simon Glassfa20e932020-12-03 16:55:20 -0700262 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200263 const char *nodename;
264 int ret;
265
266 plat->value = 0;
267 if (dev_read_bool(dev, "input")) {
268 plat->gpiod_flags = GPIOD_IS_IN;
269 } else if (dev_read_bool(dev, "output-high")) {
270 plat->value = 1;
271 plat->gpiod_flags = GPIOD_IS_OUT;
272 } else if (dev_read_bool(dev, "output-low")) {
273 plat->gpiod_flags = GPIOD_IS_OUT;
274 } else {
275 printf("%s: missing gpio-hog state.\n", __func__);
276 return -EINVAL;
277 }
278 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
279 if (ret) {
280 printf("%s: wrong gpios property, 2 values needed %d\n",
281 __func__, ret);
282 return ret;
283 }
284 nodename = dev_read_string(dev, "line-name");
Heiko Schocher58e4c382019-07-17 06:59:51 +0200285 if (nodename)
286 device_set_name(dev, nodename);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200287
288 return 0;
289}
290
291static int gpio_hog_probe(struct udevice *dev)
292{
Simon Glassfa20e932020-12-03 16:55:20 -0700293 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200294 struct gpio_hog_priv *priv = dev_get_priv(dev);
295 int ret;
296
297 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
298 plat->val[0], plat->gpiod_flags,
299 plat->val[1], &priv->gpiod);
300 if (ret < 0) {
301 debug("%s: node %s could not get gpio.\n", __func__,
302 dev->name);
303 return ret;
304 }
Heiko Schocher58e4c382019-07-17 06:59:51 +0200305
306 if (plat->gpiod_flags == GPIOD_IS_OUT) {
307 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
308 if (ret < 0) {
309 debug("%s: node %s could not set gpio.\n", __func__,
310 dev->name);
311 return ret;
312 }
313 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200314
315 return 0;
316}
317
318int gpio_hog_probe_all(void)
319{
320 struct udevice *dev;
321 int ret;
Heiko Schocher58e4c382019-07-17 06:59:51 +0200322 int retval = 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200323
324 for (uclass_first_device(UCLASS_NOP, &dev);
325 dev;
326 uclass_find_next_device(&dev)) {
Simon Glass65130cd2020-12-28 20:34:56 -0700327 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
Heiko Schocher39cb3402019-06-12 06:11:46 +0200328 ret = device_probe(dev);
Heiko Schocher58e4c382019-07-17 06:59:51 +0200329 if (ret) {
330 printf("Failed to probe device %s err: %d\n",
331 dev->name, ret);
332 retval = ret;
333 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200334 }
335 }
336
Heiko Schocher58e4c382019-07-17 06:59:51 +0200337 return retval;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200338}
339
Heiko Schocher58e4c382019-07-17 06:59:51 +0200340int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200341{
342 struct udevice *dev;
343
Heiko Schocher58e4c382019-07-17 06:59:51 +0200344 *desc = NULL;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200345 gpio_hog_probe_all();
346 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
347 struct gpio_hog_priv *priv = dev_get_priv(dev);
348
Heiko Schocher58e4c382019-07-17 06:59:51 +0200349 *desc = &priv->gpiod;
350 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200351 }
352
Heiko Schocher58e4c382019-07-17 06:59:51 +0200353 return -ENODEV;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200354}
355
356U_BOOT_DRIVER(gpio_hog) = {
357 .name = "gpio_hog",
358 .id = UCLASS_NOP,
Simon Glassaad29ae2020-12-03 16:55:21 -0700359 .of_to_plat = gpio_hog_of_to_plat,
Heiko Schocher39cb3402019-06-12 06:11:46 +0200360 .probe = gpio_hog_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700361 .priv_auto = sizeof(struct gpio_hog_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700362 .plat_auto = sizeof(struct gpio_hog_data),
Heiko Schocher39cb3402019-06-12 06:11:46 +0200363};
364#else
Heiko Schocher58e4c382019-07-17 06:59:51 +0200365int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200366{
Heiko Schocher58e4c382019-07-17 06:59:51 +0200367 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200368}
369#endif
370
Simon Glass047cdb32015-06-23 15:38:41 -0600371int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassce555292015-01-05 20:05:27 -0700372{
Simon Glass49f315a2021-02-04 21:22:05 -0700373 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700374 struct udevice *dev = desc->dev;
375 struct gpio_dev_priv *uc_priv;
376 char *str;
377 int ret;
378
Simon Glassde0977b2015-03-05 12:25:20 -0700379 uc_priv = dev_get_uclass_priv(dev);
Simon Glassce555292015-01-05 20:05:27 -0700380 if (uc_priv->name[desc->offset])
381 return -EBUSY;
382 str = strdup(label);
383 if (!str)
384 return -ENOMEM;
Simon Glass49f315a2021-02-04 21:22:05 -0700385 if (ops->request) {
386 ret = ops->request(dev, desc->offset, label);
Simon Glassce555292015-01-05 20:05:27 -0700387 if (ret) {
388 free(str);
389 return ret;
390 }
391 }
392 uc_priv->name[desc->offset] = str;
393
394 return 0;
395}
396
Simon Glass16e10402015-01-05 20:05:29 -0700397static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
398{
Simon Glass7611ac62019-09-25 08:56:27 -0600399#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass16e10402015-01-05 20:05:29 -0700400 va_list args;
401 char buf[40];
402
403 va_start(args, fmt);
404 vscnprintf(buf, sizeof(buf), fmt, args);
405 va_end(args);
406 return dm_gpio_request(desc, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700407#else
408 return dm_gpio_request(desc, fmt);
409#endif
Simon Glass16e10402015-01-05 20:05:29 -0700410}
411
Simon Glasse821d182014-02-26 15:59:24 -0700412/**
413 * gpio_request() - [COMPAT] Request GPIO
414 * gpio: GPIO number
415 * label: Name for the requested GPIO
416 *
Simon Glass0f4517d2014-10-04 11:29:42 -0600417 * The label is copied and allocated so the caller does not need to keep
418 * the pointer around.
419 *
Simon Glasse821d182014-02-26 15:59:24 -0700420 * This function implements the API that's compatible with current
421 * GPIO API used in U-Boot. The request is forwarded to particular
422 * GPIO driver. Returns 0 on success, negative value on error.
423 */
424int gpio_request(unsigned gpio, const char *label)
425{
Simon Glassce555292015-01-05 20:05:27 -0700426 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700427 int ret;
428
Simon Glassce555292015-01-05 20:05:27 -0700429 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700430 if (ret)
431 return ret;
432
Simon Glassce555292015-01-05 20:05:27 -0700433 return dm_gpio_request(&desc, label);
Simon Glasse821d182014-02-26 15:59:24 -0700434}
435
436/**
Simon Glass1b27d602014-10-04 11:29:49 -0600437 * gpio_requestf() - [COMPAT] Request GPIO
438 * @gpio: GPIO number
439 * @fmt: Format string for the requested GPIO
440 * @...: Arguments for the printf() format string
441 *
442 * This function implements the API that's compatible with current
443 * GPIO API used in U-Boot. The request is forwarded to particular
444 * GPIO driver. Returns 0 on success, negative value on error.
445 */
446int gpio_requestf(unsigned gpio, const char *fmt, ...)
447{
Simon Glass7611ac62019-09-25 08:56:27 -0600448#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass1b27d602014-10-04 11:29:49 -0600449 va_list args;
450 char buf[40];
451
452 va_start(args, fmt);
453 vscnprintf(buf, sizeof(buf), fmt, args);
454 va_end(args);
455 return gpio_request(gpio, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700456#else
457 return gpio_request(gpio, fmt);
458#endif
Simon Glass1b27d602014-10-04 11:29:49 -0600459}
460
Simon Glassce555292015-01-05 20:05:27 -0700461int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glasse821d182014-02-26 15:59:24 -0700462{
Simon Glass49f315a2021-02-04 21:22:05 -0700463 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600464 struct gpio_dev_priv *uc_priv;
Simon Glasse821d182014-02-26 15:59:24 -0700465 int ret;
466
Simon Glassde0977b2015-03-05 12:25:20 -0700467 uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600468 if (!uc_priv->name[offset])
469 return -ENXIO;
Simon Glass49f315a2021-02-04 21:22:05 -0700470 if (ops->rfree) {
471 ret = ops->rfree(dev, offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600472 if (ret)
473 return ret;
474 }
475
476 free(uc_priv->name[offset]);
477 uc_priv->name[offset] = NULL;
478
479 return 0;
480}
481
Simon Glassce555292015-01-05 20:05:27 -0700482/**
483 * gpio_free() - [COMPAT] Relinquish GPIO
484 * gpio: GPIO number
485 *
486 * This function implements the API that's compatible with current
487 * GPIO API used in U-Boot. The request is forwarded to particular
488 * GPIO driver. Returns 0 on success, negative value on error.
489 */
490int gpio_free(unsigned gpio)
491{
492 struct gpio_desc desc;
493 int ret;
494
495 ret = gpio_to_device(gpio, &desc);
496 if (ret)
497 return ret;
498
499 return _dm_gpio_free(desc.dev, desc.offset);
500}
501
Simon Glassfd838972016-03-06 19:27:51 -0700502static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glass0f4517d2014-10-04 11:29:42 -0600503{
Simon Glass230c1432015-07-02 18:16:16 -0600504 struct gpio_dev_priv *uc_priv;
505
506 if (!dm_gpio_is_valid(desc))
507 return -ENOENT;
Simon Glass0f4517d2014-10-04 11:29:42 -0600508
Simon Glass230c1432015-07-02 18:16:16 -0600509 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700510 if (!uc_priv->name[desc->offset]) {
Simon Glass0f4517d2014-10-04 11:29:42 -0600511 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassce555292015-01-05 20:05:27 -0700512 desc->dev->name, func,
513 uc_priv->bank_name ? uc_priv->bank_name : "",
514 desc->offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600515 return -EBUSY;
516 }
517
518 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700519}
520
521/**
522 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
523 * gpio: GPIO number
524 *
525 * This function implements the API that's compatible with current
526 * GPIO API used in U-Boot. The request is forwarded to particular
527 * GPIO driver. Returns 0 on success, negative value on error.
528 */
529int gpio_direction_input(unsigned gpio)
530{
Simon Glassce555292015-01-05 20:05:27 -0700531 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700532 int ret;
533
Simon Glassce555292015-01-05 20:05:27 -0700534 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700535 if (ret)
536 return ret;
537
Simon Glass722f9682021-02-04 21:22:04 -0700538 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
Simon Glasse821d182014-02-26 15:59:24 -0700539}
540
541/**
542 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
543 * gpio: GPIO number
544 * value: Logical value to be set on the GPIO pin
545 *
546 * This function implements the API that's compatible with current
547 * GPIO API used in U-Boot. The request is forwarded to particular
548 * GPIO driver. Returns 0 on success, negative value on error.
549 */
550int gpio_direction_output(unsigned gpio, int value)
551{
Simon Glassce555292015-01-05 20:05:27 -0700552 struct gpio_desc desc;
Simon Glass722f9682021-02-04 21:22:04 -0700553 ulong flags;
Simon Glassce555292015-01-05 20:05:27 -0700554 int ret;
555
556 ret = gpio_to_device(gpio, &desc);
557 if (ret)
558 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700559
Simon Glass722f9682021-02-04 21:22:04 -0700560 flags = GPIOD_IS_OUT;
561 if (value)
562 flags |= GPIOD_IS_OUT_ACTIVE;
563 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
Simon Glassce555292015-01-05 20:05:27 -0700564}
565
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100566static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassce555292015-01-05 20:05:27 -0700567{
Simon Glass49f315a2021-02-04 21:22:05 -0700568 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700569 int value;
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100570
Simon Glass49f315a2021-02-04 21:22:05 -0700571 value = ops->get_value(desc->dev, desc->offset);
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100572
573 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
574}
575
576int dm_gpio_get_value(const struct gpio_desc *desc)
577{
Simon Glassce555292015-01-05 20:05:27 -0700578 int ret;
579
580 ret = check_reserved(desc, "get_value");
581 if (ret)
582 return ret;
583
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100584 return _gpio_get_value(desc);
Simon Glassce555292015-01-05 20:05:27 -0700585}
586
Simon Glassfd838972016-03-06 19:27:51 -0700587int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassce555292015-01-05 20:05:27 -0700588{
Simon Glass7b893f92021-02-04 21:22:03 -0700589 const struct dm_gpio_ops *ops;
Simon Glassce555292015-01-05 20:05:27 -0700590 int ret;
591
592 ret = check_reserved(desc, "set_value");
593 if (ret)
594 return ret;
595
596 if (desc->flags & GPIOD_ACTIVE_LOW)
597 value = !value;
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200598
Simon Glass7b893f92021-02-04 21:22:03 -0700599 /* GPIOD_ are directly managed by driver in set_flags */
600 ops = gpio_get_ops(desc->dev);
601 if (ops->set_flags) {
602 ulong flags = desc->flags;
603
604 if (value)
605 flags |= GPIOD_IS_OUT_ACTIVE;
606 else
607 flags &= ~GPIOD_IS_OUT_ACTIVE;
608 return ops->set_flags(desc->dev, desc->offset, flags);
609 }
610
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200611 /*
612 * Emulate open drain by not actively driving the line high or
613 * Emulate open source by not actively driving the line low
614 */
615 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
616 (desc->flags & GPIOD_OPEN_SOURCE && !value))
Simon Glass7b893f92021-02-04 21:22:03 -0700617 return ops->direction_input(desc->dev, desc->offset);
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200618 else if (desc->flags & GPIOD_OPEN_DRAIN ||
619 desc->flags & GPIOD_OPEN_SOURCE)
Simon Glass7b893f92021-02-04 21:22:03 -0700620 return ops->direction_output(desc->dev, desc->offset, value);
621
622 ret = ops->set_value(desc->dev, desc->offset, value);
623 if (ret)
624 return ret;
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200625
Simon Glassce555292015-01-05 20:05:27 -0700626 return 0;
627}
628
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100629/* check dir flags invalid configuration */
630static int check_dir_flags(ulong flags)
631{
632 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
633 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
634 __func__, flags);
635 return -EINVAL;
636 }
637
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100638 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
639 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
640 __func__, flags);
641 return -EINVAL;
642 }
643
644 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
645 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
646 __func__, flags);
647 return -EINVAL;
648 }
649
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100650 return 0;
651}
652
Simon Glass7b893f92021-02-04 21:22:03 -0700653/**
654 * _dm_gpio_set_flags() - Send flags to the driver
655 *
656 * This uses the best available method to send the given flags to the driver.
657 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
658 * of GPIOD_IS_OUT_ACTIVE.
659 *
660 * @desc: GPIO description
661 * @flags: flags value to set
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100662 * Return: 0 if OK, -ve on error
Simon Glass7b893f92021-02-04 21:22:03 -0700663 */
Simon Glass54befdd2021-02-04 21:21:55 -0700664static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
Simon Glassce555292015-01-05 20:05:27 -0700665{
666 struct udevice *dev = desc->dev;
Simon Glass49f315a2021-02-04 21:22:05 -0700667 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100668 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100669 int ret = 0;
Simon Glasse821d182014-02-26 15:59:24 -0700670
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100671 ret = check_dir_flags(flags);
672 if (ret) {
673 dev_dbg(dev,
674 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
675 desc->dev->name,
676 uc_priv->bank_name ? uc_priv->bank_name : "",
677 desc->offset, flags);
678
679 return ret;
680 }
681
Simon Glass7b893f92021-02-04 21:22:03 -0700682 /* If active low, invert the output state */
683 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
684 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
685 flags ^= GPIOD_IS_OUT_ACTIVE;
686
Simon Glass54befdd2021-02-04 21:21:55 -0700687 /* GPIOD_ are directly managed by driver in set_flags */
688 if (ops->set_flags) {
689 ret = ops->set_flags(dev, desc->offset, flags);
Patrick Delaunay684326f2020-01-13 11:35:09 +0100690 } else {
691 if (flags & GPIOD_IS_OUT) {
Simon Glass7b893f92021-02-04 21:22:03 -0700692 bool value = flags & GPIOD_IS_OUT_ACTIVE;
693
694 ret = ops->direction_output(dev, desc->offset, value);
Patrick Delaunay684326f2020-01-13 11:35:09 +0100695 } else if (flags & GPIOD_IS_IN) {
696 ret = ops->direction_input(dev, desc->offset);
697 }
Simon Glassce555292015-01-05 20:05:27 -0700698 }
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100699
700 return ret;
701}
702
Simon Glass7b893f92021-02-04 21:22:03 -0700703int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100704{
Simon Glass7b893f92021-02-04 21:22:03 -0700705 ulong flags;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100706 int ret;
707
708 ret = check_reserved(desc, "set_dir_flags");
Simon Glassce555292015-01-05 20:05:27 -0700709 if (ret)
710 return ret;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100711
Simon Glass7b893f92021-02-04 21:22:03 -0700712 flags = (desc->flags & ~clr) | set;
713
Simon Glass54befdd2021-02-04 21:21:55 -0700714 ret = _dm_gpio_set_flags(desc, flags);
Simon Glass7b893f92021-02-04 21:22:03 -0700715 if (ret)
716 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700717
Simon Glass7b893f92021-02-04 21:22:03 -0700718 /* save the flags also in descriptor */
719 desc->flags = flags;
720
721 return 0;
722}
723
724int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
725{
726 /* combine the requested flags (for IN/OUT) and the descriptor flags */
727 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
Simon Glassce555292015-01-05 20:05:27 -0700728}
729
Simon Glass247ccf22021-02-04 21:22:09 -0700730int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
731 ulong set)
732{
733 int ret;
734 int i;
735
736 for (i = 0; i < count; i++) {
737 ret = dm_gpio_clrset_flags(&desc[i], clr, set);
738 if (ret)
739 return log_ret(ret);
740 }
741
742 return 0;
743}
744
Simon Glass909bee32021-02-04 21:21:57 -0700745int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100746{
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100747 struct udevice *dev = desc->dev;
748 int ret, value;
Simon Glass49f315a2021-02-04 21:22:05 -0700749 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glassd063ce92021-02-04 21:21:56 -0700750 ulong flags;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100751
Simon Glassd063ce92021-02-04 21:21:56 -0700752 ret = check_reserved(desc, "get_flags");
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100753 if (ret)
754 return ret;
755
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100756 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
Simon Glassd063ce92021-02-04 21:21:56 -0700757 if (ops->get_flags) {
758 ret = ops->get_flags(dev, desc->offset, &flags);
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100759 if (ret)
760 return ret;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100761
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100762 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
Simon Glassd063ce92021-02-04 21:21:56 -0700763 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100764 if (desc->flags & GPIOD_ACTIVE_LOW)
765 value = !value;
Simon Glassd063ce92021-02-04 21:21:56 -0700766 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
767 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100768 if (value)
Simon Glassd063ce92021-02-04 21:21:56 -0700769 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100770 } else {
Simon Glassd063ce92021-02-04 21:21:56 -0700771 flags = desc->flags;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100772 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
Simon Glassd063ce92021-02-04 21:21:56 -0700773 flags &= ~GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100774 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
Simon Glassd063ce92021-02-04 21:21:56 -0700775 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100776 }
Simon Glassd063ce92021-02-04 21:21:56 -0700777 *flagsp = flags;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100778
779 return 0;
780}
781
Simon Glasse821d182014-02-26 15:59:24 -0700782/**
783 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
784 * gpio: GPIO number
785 *
786 * This function implements the API that's compatible with current
787 * GPIO API used in U-Boot. The request is forwarded to particular
788 * GPIO driver. Returns the value of the GPIO pin, or negative value
789 * on error.
790 */
791int gpio_get_value(unsigned gpio)
792{
Simon Glasse821d182014-02-26 15:59:24 -0700793 int ret;
794
Simon Glassce555292015-01-05 20:05:27 -0700795 struct gpio_desc desc;
796
797 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700798 if (ret)
799 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700800 return dm_gpio_get_value(&desc);
Simon Glasse821d182014-02-26 15:59:24 -0700801}
802
803/**
804 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
805 * gpio: GPIO number
806 * value: Logical value to be set on the GPIO pin.
807 *
808 * This function implements the API that's compatible with current
809 * GPIO API used in U-Boot. The request is forwarded to particular
810 * GPIO driver. Returns 0 on success, negative value on error.
811 */
812int gpio_set_value(unsigned gpio, int value)
813{
Simon Glassce555292015-01-05 20:05:27 -0700814 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700815 int ret;
816
Simon Glassce555292015-01-05 20:05:27 -0700817 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700818 if (ret)
819 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700820 return dm_gpio_set_value(&desc, value);
Simon Glasse821d182014-02-26 15:59:24 -0700821}
822
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200823const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glasse821d182014-02-26 15:59:24 -0700824{
825 struct gpio_dev_priv *priv;
826
827 /* Must be called on an active device */
Simon Glassde0977b2015-03-05 12:25:20 -0700828 priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700829 assert(priv);
830
831 *bit_count = priv->gpio_count;
832 return priv->bank_name;
833}
834
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600835static const char * const gpio_function[GPIOF_COUNT] = {
836 "input",
837 "output",
838 "unused",
839 "unknown",
840 "func",
841};
842
Masahiro Yamada286c2522017-06-22 16:50:25 +0900843static int get_function(struct udevice *dev, int offset, bool skip_unused,
844 const char **namep)
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600845{
Simon Glassde0977b2015-03-05 12:25:20 -0700846 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass49f315a2021-02-04 21:22:05 -0700847 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600848
849 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
850 if (!device_active(dev))
851 return -ENODEV;
852 if (offset < 0 || offset >= uc_priv->gpio_count)
853 return -EINVAL;
854 if (namep)
855 *namep = uc_priv->name[offset];
856 if (skip_unused && !uc_priv->name[offset])
857 return GPIOF_UNUSED;
858 if (ops->get_function) {
859 int ret;
860
861 ret = ops->get_function(dev, offset);
862 if (ret < 0)
863 return ret;
864 if (ret >= ARRAY_SIZE(gpio_function))
865 return -ENODATA;
866 return ret;
867 }
868
869 return GPIOF_UNKNOWN;
870}
871
872int gpio_get_function(struct udevice *dev, int offset, const char **namep)
873{
874 return get_function(dev, offset, true, namep);
875}
876
877int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
878{
879 return get_function(dev, offset, false, namep);
880}
881
Simon Glass6b1ef592014-10-04 11:29:44 -0600882int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
883{
Simon Glass49f315a2021-02-04 21:22:05 -0700884 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600885 struct gpio_dev_priv *priv;
886 char *str = buf;
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200887 const char *label;
Simon Glass6b1ef592014-10-04 11:29:44 -0600888 int func;
889 int ret;
890 int len;
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200891 bool used;
Simon Glass6b1ef592014-10-04 11:29:44 -0600892
893 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
894
895 *buf = 0;
Simon Glassde0977b2015-03-05 12:25:20 -0700896 priv = dev_get_uclass_priv(dev);
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200897 ret = gpio_get_raw_function(dev, offset, &label);
Simon Glass6b1ef592014-10-04 11:29:44 -0600898 if (ret < 0)
899 return ret;
900 func = ret;
901 len = snprintf(str, buffsize, "%s%d: %s",
902 priv->bank_name ? priv->bank_name : "",
903 offset, gpio_function[func]);
Simon Glass6b1ef592014-10-04 11:29:44 -0600904
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200905 switch (func) {
906 case GPIOF_FUNC:
907 snprintf(str + len, buffsize - len, " %s", label ? label : "");
908 break;
909 case GPIOF_INPUT:
910 case GPIOF_OUTPUT:
911 case GPIOF_UNUSED:
Simon Glass6b1ef592014-10-04 11:29:44 -0600912 ret = ops->get_value(dev, offset);
913 if (ret < 0)
914 return ret;
915 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
916 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
917 ret,
918 used ? 'x' : ' ',
Patrice Chotardeb8f3a72022-08-30 14:09:12 +0200919 label ? " " : "",
Simon Glass6b1ef592014-10-04 11:29:44 -0600920 label ? label : "");
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200921 break;
Simon Glass6b1ef592014-10-04 11:29:44 -0600922 }
923
924 return 0;
925}
926
Simon Glass3176b6c2020-07-07 13:11:44 -0600927#if CONFIG_IS_ENABLED(ACPIGEN)
928int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
929{
Simon Glass49f315a2021-02-04 21:22:05 -0700930 const struct dm_gpio_ops *ops;
Simon Glass3176b6c2020-07-07 13:11:44 -0600931
932 memset(gpio, '\0', sizeof(*gpio));
933 if (!dm_gpio_is_valid(desc)) {
934 /* Indicate that the GPIO is not valid */
935 gpio->pin_count = 0;
936 gpio->pins[0] = 0;
937 return -EINVAL;
938 }
939
940 ops = gpio_get_ops(desc->dev);
941 if (!ops->get_acpi)
942 return -ENOSYS;
943
944 return ops->get_acpi(desc, gpio);
945}
946#endif
947
Simon Glassbef54db2015-04-14 21:03:20 -0600948int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
949{
950 int i, ret;
951 int gpio;
952
953 for (i = 0; i < 32; i++) {
954 gpio = gpio_num_array[i];
955 if (gpio == -1)
956 break;
957 ret = gpio_requestf(gpio, fmt, i);
958 if (ret)
959 goto err;
960 ret = gpio_direction_input(gpio);
961 if (ret) {
962 gpio_free(gpio);
963 goto err;
964 }
965 }
966
967 return 0;
968err:
969 for (i--; i >= 0; i--)
970 gpio_free(gpio_num_array[i]);
971
972 return ret;
973}
974
Simon Glass2c97a8f2014-11-10 18:00:21 -0700975/*
976 * get a number comprised of multiple GPIO values. gpio_num_array points to
977 * the array of gpio pin numbers to scan, terminated by -1.
978 */
Simon Glassbef54db2015-04-14 21:03:20 -0600979int gpio_get_values_as_int(const int *gpio_list)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700980{
981 int gpio;
982 unsigned bitmask = 1;
983 unsigned vector = 0;
Simon Glassbef54db2015-04-14 21:03:20 -0600984 int ret;
Simon Glass2c97a8f2014-11-10 18:00:21 -0700985
986 while (bitmask &&
Simon Glassbef54db2015-04-14 21:03:20 -0600987 ((gpio = *gpio_list++) != -1)) {
988 ret = gpio_get_value(gpio);
989 if (ret < 0)
990 return ret;
991 else if (ret)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700992 vector |= bitmask;
993 bitmask <<= 1;
994 }
Simon Glassbef54db2015-04-14 21:03:20 -0600995
Simon Glass2c97a8f2014-11-10 18:00:21 -0700996 return vector;
997}
998
Simon Glassfd838972016-03-06 19:27:51 -0700999int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassdf1687d2016-03-06 19:27:50 -07001000{
1001 unsigned bitmask = 1;
1002 unsigned vector = 0;
1003 int ret, i;
1004
1005 for (i = 0; i < count; i++) {
1006 ret = dm_gpio_get_value(&desc_list[i]);
1007 if (ret < 0)
1008 return ret;
1009 else if (ret)
1010 vector |= bitmask;
1011 bitmask <<= 1;
1012 }
Simon Glass247ccf22021-02-04 21:22:09 -07001013
1014 return vector;
1015}
1016
1017int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
1018 int count)
1019{
1020 static const char tristate[] = "01z";
1021 enum {
1022 PULLUP,
1023 PULLDOWN,
1024
1025 NUM_OPTIONS,
1026 };
1027 int vals[NUM_OPTIONS];
1028 uint mask;
1029 uint vector = 0;
1030 int ret, i;
1031
1032 /*
1033 * Limit to 19 digits which should be plenty. This avoids overflow of a
1034 * 32-bit int
1035 */
1036 assert(count < 20);
1037
1038 for (i = 0; i < NUM_OPTIONS; i++) {
1039 uint flags = GPIOD_IS_IN;
1040
1041 flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
1042 ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
1043 flags);
1044 if (ret)
1045 return log_msg_ret("pu", ret);
1046
1047 /* Give the lines time to settle */
1048 udelay(10);
1049
1050 ret = dm_gpio_get_values_as_int(desc_list, count);
1051 if (ret < 0)
1052 return log_msg_ret("get1", ret);
1053 vals[i] = ret;
1054 }
1055
1056 log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
1057 for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
1058 uint pd = vals[PULLDOWN] & mask ? 1 : 0;
1059 uint pu = vals[PULLUP] & mask ? 1 : 0;
1060 uint digit;
1061
1062 /*
1063 * Get value with internal pulldown active. If this is 1 then
1064 * there is a stronger external pullup, which we call 1. If not
1065 * then call it 0.
1066 *
1067 * If the values differ then the pin is floating so we call
1068 * this a 2.
1069 */
1070 if (pu == pd)
1071 digit = pd;
1072 else
1073 digit = 2;
1074 log_debug("%c ", tristate[digit]);
1075 vector = 3 * vector + digit;
1076 }
1077 log_debug("vector=%d\n", vector);
Simon Glassdf1687d2016-03-06 19:27:50 -07001078
1079 return vector;
1080}
1081
Heiko Schocher58e4c382019-07-17 06:59:51 +02001082/**
1083 * gpio_request_tail: common work for requesting a gpio.
1084 *
1085 * ret: return value from previous work in function which calls
1086 * this function.
1087 * This seems bogus (why calling this function instead not
1088 * calling it and end caller function instead?).
1089 * Because on error in caller function we want to set some
1090 * default values in gpio desc and have a common error
1091 * debug message, which provides this function.
1092 * nodename: Name of node for which gpio gets requested
1093 * used for gpio label name.
1094 * args: pointer to output arguments structure
1095 * list_name: Name of GPIO list
1096 * used for gpio label name.
1097 * index: gpio index in gpio list
1098 * used for gpio label name.
1099 * desc: pointer to gpio descriptor, filled from this
1100 * function.
1101 * flags: gpio flags to use.
1102 * add_index: should index added to gpio label name
1103 * gpio_dev: pointer to gpio device from which the gpio
1104 * will be requested. If NULL try to get the
1105 * gpio device with uclass_get_device_by_ofnode()
1106 *
1107 * return: In error case this function sets default values in
1108 * gpio descriptor, also emmits a debug message.
1109 * On success it returns 0 else the error code from
1110 * function calls, or the error code passed through
1111 * ret to this function.
1112 *
1113 */
Heiko Schocher39cb3402019-06-12 06:11:46 +02001114static int gpio_request_tail(int ret, const char *nodename,
Simon Glass12faa022017-05-18 20:09:18 -06001115 struct ofnode_phandle_args *args,
1116 const char *list_name, int index,
Heiko Schocher39cb3402019-06-12 06:11:46 +02001117 struct gpio_desc *desc, int flags,
Heiko Schocher58e4c382019-07-17 06:59:51 +02001118 bool add_index, struct udevice *gpio_dev)
Simon Glass16e10402015-01-05 20:05:29 -07001119{
Patrick Delaunay758bba92020-01-13 11:35:01 +01001120 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass12faa022017-05-18 20:09:18 -06001121 if (ret)
Simon Glass16e10402015-01-05 20:05:29 -07001122 goto err;
Simon Glass16e10402015-01-05 20:05:29 -07001123
Heiko Schocher39cb3402019-06-12 06:11:46 +02001124 if (!desc->dev) {
1125 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1126 &desc->dev);
1127 if (ret) {
Heiko Schocher58e4c382019-07-17 06:59:51 +02001128 debug("%s: uclass_get_device_by_ofnode failed\n",
1129 __func__);
Heiko Schocher39cb3402019-06-12 06:11:46 +02001130 goto err;
1131 }
Simon Glass16e10402015-01-05 20:05:29 -07001132 }
Simon Glass12faa022017-05-18 20:09:18 -06001133 ret = gpio_find_and_xlate(desc, args);
Simon Glass16e10402015-01-05 20:05:29 -07001134 if (ret) {
1135 debug("%s: gpio_find_and_xlate failed\n", __func__);
1136 goto err;
1137 }
1138 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher39cb3402019-06-12 06:11:46 +02001139 nodename, list_name, index);
Simon Glass16e10402015-01-05 20:05:29 -07001140 if (ret) {
1141 debug("%s: dm_gpio_requestf failed\n", __func__);
1142 goto err;
1143 }
Simon Glass7b893f92021-02-04 21:22:03 -07001144
1145 /* Keep any direction flags provided by the devicetree */
1146 ret = dm_gpio_set_dir_flags(desc,
1147 flags | (desc->flags & GPIOD_MASK_DIR));
Simon Glass16e10402015-01-05 20:05:29 -07001148 if (ret) {
1149 debug("%s: dm_gpio_set_dir failed\n", __func__);
1150 goto err;
1151 }
1152
1153 return 0;
1154err:
1155 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher39cb3402019-06-12 06:11:46 +02001156 __func__, nodename, list_name, index, ret);
Simon Glass16e10402015-01-05 20:05:29 -07001157 return ret;
1158}
1159
Simon Glass92882652021-08-07 07:24:04 -06001160#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass1d9af1f2017-05-30 21:47:09 -06001161static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1162 int index, struct gpio_desc *desc,
1163 int flags, bool add_index)
Simon Glass12faa022017-05-18 20:09:18 -06001164{
1165 struct ofnode_phandle_args args;
1166 int ret;
1167
Simon Glass1d9af1f2017-05-30 21:47:09 -06001168 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1169 index, &args);
Simon Glass12faa022017-05-18 20:09:18 -06001170
Heiko Schocher39cb3402019-06-12 06:11:46 +02001171 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1172 index, desc, flags, add_index, NULL);
Simon Glass12faa022017-05-18 20:09:18 -06001173}
1174
Simon Glass1d9af1f2017-05-30 21:47:09 -06001175int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -07001176 struct gpio_desc *desc, int flags)
1177{
Simon Glass1d9af1f2017-05-30 21:47:09 -06001178 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1179 index > 0);
Simon Glass16e10402015-01-05 20:05:29 -07001180}
1181
Simon Glass1d9af1f2017-05-30 21:47:09 -06001182int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -07001183 struct gpio_desc *desc, int flags)
1184{
Simon Glass1d9af1f2017-05-30 21:47:09 -06001185 struct ofnode_phandle_args args;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001186 ofnode node;
Simon Glass1d9af1f2017-05-30 21:47:09 -06001187 int ret;
1188
1189 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1190 index, &args);
Heiko Schocher39cb3402019-06-12 06:11:46 +02001191 node = dev_ofnode(dev);
1192 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1193 index, desc, flags, index > 0, NULL);
Simon Glass16e10402015-01-05 20:05:29 -07001194}
1195
Andrew Jeffery2e62c6b2022-01-31 13:54:05 +10301196int gpio_request_by_line_name(struct udevice *dev, const char *line_name,
1197 struct gpio_desc *desc, int flags)
1198{
1199 int ret;
1200
1201 ret = dev_read_stringlist_search(dev, "gpio-line-names", line_name);
1202 if (ret < 0)
1203 return ret;
1204
1205 desc->dev = dev;
1206 desc->offset = ret;
1207 desc->flags = 0;
1208
1209 ret = dm_gpio_request(desc, line_name);
1210 if (ret) {
1211 debug("%s: dm_gpio_requestf failed\n", __func__);
1212 return ret;
1213 }
1214
1215 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
1216 if (ret)
1217 debug("%s: dm_gpio_set_dir failed\n", __func__);
1218
1219 return ret;
1220}
1221
Simon Glass1d9af1f2017-05-30 21:47:09 -06001222int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass16e10402015-01-05 20:05:29 -07001223 struct gpio_desc *desc, int max_count,
1224 int flags)
1225{
1226 int count;
1227 int ret;
1228
Przemyslaw Marczak44fc5362015-03-31 18:57:16 +02001229 for (count = 0; count < max_count; count++) {
Simon Glass1d9af1f2017-05-30 21:47:09 -06001230 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass16e10402015-01-05 20:05:29 -07001231 &desc[count], flags, true);
1232 if (ret == -ENOENT)
1233 break;
1234 else if (ret)
1235 goto err;
1236 }
1237
1238 /* We ran out of GPIOs in the list */
1239 return count;
1240
1241err:
1242 gpio_free_list_nodev(desc, count - 1);
1243
1244 return ret;
1245}
1246
1247int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1248 struct gpio_desc *desc, int max_count,
1249 int flags)
1250{
1251 /*
1252 * This isn't ideal since we don't use dev->name in the debug()
1253 * calls in gpio_request_by_name(), but we can do this until
1254 * gpio_request_list_by_name_nodev() can be dropped.
1255 */
Simon Glass1d9af1f2017-05-30 21:47:09 -06001256 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1257 max_count, flags);
Simon Glass16e10402015-01-05 20:05:29 -07001258}
1259
1260int gpio_get_list_count(struct udevice *dev, const char *list_name)
1261{
1262 int ret;
1263
Sean Anderson8ad66c52021-04-20 10:50:54 -04001264 ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells",
1265 -ENOENT);
1266 if (ret < 0) {
Simon Glass16e10402015-01-05 20:05:29 -07001267 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1268 __func__, dev->name, list_name, ret);
1269 }
1270
1271 return ret;
1272}
Simon Glassea383722021-02-04 21:21:54 -07001273#endif /* OF_PLATDATA */
Simon Glass16e10402015-01-05 20:05:29 -07001274
Simon Glass2149e112021-08-07 07:24:12 -06001275#if CONFIG_IS_ENABLED(OF_PLATDATA)
1276int gpio_request_by_phandle(struct udevice *dev,
1277 const struct phandle_2_arg *cells,
1278 struct gpio_desc *desc, int flags)
1279{
1280 struct ofnode_phandle_args args;
1281 struct udevice *gpio_dev;
1282 const int index = 0;
1283 int ret;
1284
1285 ret = device_get_by_ofplat_idx(cells->idx, &gpio_dev);
1286 if (ret)
1287 return ret;
1288 args.args[0] = cells->arg[0];
1289 args.args[1] = cells->arg[1];
1290
1291 return gpio_request_tail(ret, NULL, &args, NULL, index, desc, flags,
1292 index > 0, gpio_dev);
1293}
1294#endif
1295
Simon Glass16e10402015-01-05 20:05:29 -07001296int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1297{
1298 /* For now, we don't do any checking of dev */
1299 return _dm_gpio_free(desc->dev, desc->offset);
1300}
1301
1302int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1303{
1304 int i;
1305
1306 /* For now, we don't do any checking of dev */
1307 for (i = 0; i < count; i++)
1308 dm_gpio_free(dev, &desc[i]);
1309
1310 return 0;
1311}
1312
1313int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1314{
1315 return gpio_free_list(NULL, desc, count);
1316}
1317
Simon Glasse821d182014-02-26 15:59:24 -07001318/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glass0f4517d2014-10-04 11:29:42 -06001319static int gpio_renumber(struct udevice *removed_dev)
Simon Glasse821d182014-02-26 15:59:24 -07001320{
1321 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001322 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -07001323 struct uclass *uc;
1324 unsigned base;
1325 int ret;
1326
1327 ret = uclass_get(UCLASS_GPIO, &uc);
1328 if (ret)
1329 return ret;
1330
1331 /* Ensure that we have a base for each bank */
1332 base = 0;
1333 uclass_foreach_dev(dev, uc) {
Simon Glass0f4517d2014-10-04 11:29:42 -06001334 if (device_active(dev) && dev != removed_dev) {
Simon Glassde0977b2015-03-05 12:25:20 -07001335 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001336 uc_priv->gpio_base = base;
1337 base += uc_priv->gpio_count;
1338 }
1339 }
1340
1341 return 0;
1342}
1343
Simon Glassfd838972016-03-06 19:27:51 -07001344int gpio_get_number(const struct gpio_desc *desc)
Simon Glass94f54d12015-03-25 12:21:58 -06001345{
1346 struct udevice *dev = desc->dev;
1347 struct gpio_dev_priv *uc_priv;
1348
1349 if (!dev)
1350 return -1;
Simon Glass95588622020-12-22 19:30:28 -07001351 uc_priv = dev_get_uclass_priv(dev);
Simon Glass94f54d12015-03-25 12:21:58 -06001352
1353 return uc_priv->gpio_base + desc->offset;
1354}
1355
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001356static int gpio_post_probe(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001357{
Simon Glassde0977b2015-03-05 12:25:20 -07001358 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001359
1360 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1361 if (!uc_priv->name)
1362 return -ENOMEM;
1363
1364 return gpio_renumber(NULL);
Simon Glasse821d182014-02-26 15:59:24 -07001365}
1366
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001367static int gpio_pre_remove(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001368{
Simon Glassde0977b2015-03-05 12:25:20 -07001369 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001370 int i;
1371
1372 for (i = 0; i < uc_priv->gpio_count; i++) {
1373 if (uc_priv->name[i])
1374 free(uc_priv->name[i]);
1375 }
1376 free(uc_priv->name);
1377
1378 return gpio_renumber(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001379}
1380
Heiko Schocher39cb3402019-06-12 06:11:46 +02001381int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1382 char *list_name, int index, int flags,
1383 int dtflags, struct gpio_desc *desc)
1384{
1385 struct ofnode_phandle_args args;
1386
1387 args.node = ofnode_null();
1388 args.args_count = 2;
1389 args.args[0] = index;
1390 args.args[1] = dtflags;
1391
1392 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1393 flags, 0, dev);
1394}
1395
Jean-Jacques Hiblot775d6a22020-09-11 13:43:34 +05301396static void devm_gpiod_release(struct udevice *dev, void *res)
1397{
1398 dm_gpio_free(dev, res);
1399}
1400
1401static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1402{
1403 return res == data;
1404}
1405
1406struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1407 unsigned int index, int flags)
1408{
1409 int rc;
1410 struct gpio_desc *desc;
1411 char *propname;
1412 static const char suffix[] = "-gpios";
1413
1414 propname = malloc(strlen(id) + sizeof(suffix));
1415 if (!propname) {
1416 rc = -ENOMEM;
1417 goto end;
1418 }
1419
1420 strcpy(propname, id);
1421 strcat(propname, suffix);
1422
1423 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1424 __GFP_ZERO);
1425 if (unlikely(!desc)) {
1426 rc = -ENOMEM;
1427 goto end;
1428 }
1429
1430 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1431
1432end:
1433 if (propname)
1434 free(propname);
1435
1436 if (rc)
1437 return ERR_PTR(rc);
1438
1439 devres_add(dev, desc);
1440
1441 return desc;
1442}
1443
1444struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1445 const char *id,
1446 unsigned int index,
1447 int flags)
1448{
1449 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1450
1451 if (IS_ERR(desc))
1452 return NULL;
1453
1454 return desc;
1455}
1456
1457void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1458{
1459 int rc;
1460
1461 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1462 WARN_ON(rc);
1463}
1464
Michal Simek5f7202c2018-07-12 12:42:27 +02001465static int gpio_post_bind(struct udevice *dev)
1466{
1467#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1468 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1469 static int reloc_done;
1470
1471 if (!reloc_done) {
1472 if (ops->request)
1473 ops->request += gd->reloc_off;
Simon Glassb3a47542020-02-04 20:15:17 -07001474 if (ops->rfree)
1475 ops->rfree += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001476 if (ops->direction_input)
1477 ops->direction_input += gd->reloc_off;
1478 if (ops->direction_output)
1479 ops->direction_output += gd->reloc_off;
1480 if (ops->get_value)
1481 ops->get_value += gd->reloc_off;
1482 if (ops->set_value)
1483 ops->set_value += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001484 if (ops->get_function)
1485 ops->get_function += gd->reloc_off;
1486 if (ops->xlate)
1487 ops->xlate += gd->reloc_off;
Simon Glass54befdd2021-02-04 21:21:55 -07001488 if (ops->set_flags)
1489 ops->set_flags += gd->reloc_off;
Simon Glassd063ce92021-02-04 21:21:56 -07001490 if (ops->get_flags)
1491 ops->get_flags += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001492
1493 reloc_done++;
1494 }
1495#endif
Heiko Schocher39cb3402019-06-12 06:11:46 +02001496
Eddie James652b8692022-02-07 17:09:01 -06001497 if (CONFIG_IS_ENABLED(GPIO_HOG)) {
1498 struct udevice *child;
1499 ofnode node;
1500
Heiko Schocher58e4c382019-07-17 06:59:51 +02001501 dev_for_each_subnode(node, dev) {
1502 if (ofnode_read_bool(node, "gpio-hog")) {
1503 const char *name = ofnode_get_name(node);
1504 int ret;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001505
Heiko Schocher58e4c382019-07-17 06:59:51 +02001506 ret = device_bind_driver_to_node(dev,
1507 "gpio_hog",
1508 name, node,
1509 &child);
1510 if (ret)
1511 return ret;
1512 }
Heiko Schocher39cb3402019-06-12 06:11:46 +02001513 }
1514 }
Michal Simek5f7202c2018-07-12 12:42:27 +02001515 return 0;
1516}
1517
Simon Glasse821d182014-02-26 15:59:24 -07001518UCLASS_DRIVER(gpio) = {
1519 .id = UCLASS_GPIO,
1520 .name = "gpio",
Bhuvanchandra DVb1a1fc92015-06-01 18:37:15 +05301521 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glasse821d182014-02-26 15:59:24 -07001522 .post_probe = gpio_post_probe,
Michal Simek5f7202c2018-07-12 12:42:27 +02001523 .post_bind = gpio_post_bind,
Simon Glasse821d182014-02-26 15:59:24 -07001524 .pre_remove = gpio_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001525 .per_device_auto = sizeof(struct gpio_dev_priv),
Simon Glasse821d182014-02-26 15:59:24 -07001526};