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