gpio: remove the open_drain API and ops
[oweals/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 #include <common.h>
7 #include <dm.h>
8 #include <dm/device-internal.h>
9 #include <dm/lists.h>
10 #include <dm/uclass-internal.h>
11 #include <dt-bindings/gpio/gpio.h>
12 #include <errno.h>
13 #include <fdtdec.h>
14 #include <malloc.h>
15 #include <asm/gpio.h>
16 #include <linux/bug.h>
17 #include <linux/ctype.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 /**
22  * gpio_to_device() - Convert global GPIO number to device, number
23  *
24  * Convert the GPIO number to an entry in the list of GPIOs
25  * or GPIO blocks registered with the GPIO controller. Returns
26  * entry on success, NULL on error.
27  *
28  * @gpio:       The numeric representation of the GPIO
29  * @desc:       Returns description (desc->flags will always be 0)
30  * @return 0 if found, -ENOENT if not found
31  */
32 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
33 {
34         struct gpio_dev_priv *uc_priv;
35         struct udevice *dev;
36         int ret;
37
38         for (ret = uclass_first_device(UCLASS_GPIO, &dev);
39              dev;
40              ret = uclass_next_device(&dev)) {
41                 uc_priv = dev_get_uclass_priv(dev);
42                 if (gpio >= uc_priv->gpio_base &&
43                     gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
44                         desc->dev = dev;
45                         desc->offset = gpio - uc_priv->gpio_base;
46                         desc->flags = 0;
47                         return 0;
48                 }
49         }
50
51         /* No such GPIO */
52         return ret ? ret : -ENOENT;
53 }
54
55 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
56 {
57         struct gpio_dev_priv *uc_priv = NULL;
58         struct udevice *dev;
59         ulong offset;
60         int numeric;
61         int ret;
62
63         numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
64         for (ret = uclass_first_device(UCLASS_GPIO, &dev);
65              dev;
66              ret = uclass_next_device(&dev)) {
67                 int len;
68
69                 uc_priv = dev_get_uclass_priv(dev);
70                 if (numeric != -1) {
71                         offset = numeric - uc_priv->gpio_base;
72                         /* Allow GPIOs to be numbered from 0 */
73                         if (offset < uc_priv->gpio_count)
74                                 break;
75                 }
76
77                 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
78
79                 if (!strncasecmp(name, uc_priv->bank_name, len)) {
80                         if (!strict_strtoul(name + len, 10, &offset))
81                                 break;
82                 }
83         }
84
85         if (!dev)
86                 return ret ? ret : -EINVAL;
87
88         desc->dev = dev;
89         desc->offset = offset;
90
91         return 0;
92 }
93
94 int gpio_lookup_name(const char *name, struct udevice **devp,
95                      unsigned int *offsetp, unsigned int *gpiop)
96 {
97         struct gpio_desc desc;
98         int ret;
99
100         if (devp)
101                 *devp = NULL;
102         ret = dm_gpio_lookup_name(name, &desc);
103         if (ret)
104                 return ret;
105
106         if (devp)
107                 *devp = desc.dev;
108         if (offsetp)
109                 *offsetp = desc.offset;
110         if (gpiop) {
111                 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
112
113                 *gpiop = uc_priv->gpio_base + desc.offset;
114         }
115
116         return 0;
117 }
118
119 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
120                           struct ofnode_phandle_args *args)
121 {
122         if (args->args_count < 1)
123                 return -EINVAL;
124
125         desc->offset = args->args[0];
126
127         if (args->args_count < 2)
128                 return 0;
129
130         if (args->args[1] & GPIO_ACTIVE_LOW)
131                 desc->flags = GPIOD_ACTIVE_LOW;
132
133         return 0;
134 }
135
136 static int gpio_find_and_xlate(struct gpio_desc *desc,
137                                struct ofnode_phandle_args *args)
138 {
139         struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
140
141         if (ops->xlate)
142                 return ops->xlate(desc->dev, desc, args);
143         else
144                 return gpio_xlate_offs_flags(desc->dev, desc, args);
145 }
146
147 #if defined(CONFIG_GPIO_HOG)
148
149 struct gpio_hog_priv {
150         struct gpio_desc gpiod;
151 };
152
153 struct gpio_hog_data {
154         int gpiod_flags;
155         int value;
156         u32 val[2];
157 };
158
159 static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
160 {
161         struct gpio_hog_data *plat = dev_get_platdata(dev);
162         const char *nodename;
163         int ret;
164
165         plat->value = 0;
166         if (dev_read_bool(dev, "input")) {
167                 plat->gpiod_flags = GPIOD_IS_IN;
168         } else if (dev_read_bool(dev, "output-high")) {
169                 plat->value = 1;
170                 plat->gpiod_flags = GPIOD_IS_OUT;
171         } else if (dev_read_bool(dev, "output-low")) {
172                 plat->gpiod_flags = GPIOD_IS_OUT;
173         } else {
174                 printf("%s: missing gpio-hog state.\n", __func__);
175                 return -EINVAL;
176         }
177         ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
178         if (ret) {
179                 printf("%s: wrong gpios property, 2 values needed %d\n",
180                        __func__, ret);
181                 return ret;
182         }
183         nodename = dev_read_string(dev, "line-name");
184         if (nodename)
185                 device_set_name(dev, nodename);
186
187         return 0;
188 }
189
190 static int gpio_hog_probe(struct udevice *dev)
191 {
192         struct gpio_hog_data *plat = dev_get_platdata(dev);
193         struct gpio_hog_priv *priv = dev_get_priv(dev);
194         int ret;
195
196         ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
197                                      plat->val[0], plat->gpiod_flags,
198                                      plat->val[1], &priv->gpiod);
199         if (ret < 0) {
200                 debug("%s: node %s could not get gpio.\n", __func__,
201                       dev->name);
202                 return ret;
203         }
204
205         if (plat->gpiod_flags == GPIOD_IS_OUT) {
206                 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
207                 if (ret < 0) {
208                         debug("%s: node %s could not set gpio.\n", __func__,
209                               dev->name);
210                         return ret;
211                 }
212         }
213
214         return 0;
215 }
216
217 int gpio_hog_probe_all(void)
218 {
219         struct udevice *dev;
220         int ret;
221         int retval = 0;
222
223         for (uclass_first_device(UCLASS_NOP, &dev);
224              dev;
225              uclass_find_next_device(&dev)) {
226                 if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
227                         ret = device_probe(dev);
228                         if (ret) {
229                                 printf("Failed to probe device %s err: %d\n",
230                                        dev->name, ret);
231                                 retval = ret;
232                         }
233                 }
234         }
235
236         return retval;
237 }
238
239 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
240 {
241         struct udevice *dev;
242
243         *desc = NULL;
244         gpio_hog_probe_all();
245         if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
246                 struct gpio_hog_priv *priv = dev_get_priv(dev);
247
248                 *desc = &priv->gpiod;
249                 return 0;
250         }
251
252         return -ENODEV;
253 }
254
255 U_BOOT_DRIVER(gpio_hog) = {
256         .name   = "gpio_hog",
257         .id     = UCLASS_NOP,
258         .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
259         .probe = gpio_hog_probe,
260         .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
261         .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
262 };
263 #else
264 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
265 {
266         return 0;
267 }
268 #endif
269
270 int dm_gpio_request(struct gpio_desc *desc, const char *label)
271 {
272         struct udevice *dev = desc->dev;
273         struct gpio_dev_priv *uc_priv;
274         char *str;
275         int ret;
276
277         uc_priv = dev_get_uclass_priv(dev);
278         if (uc_priv->name[desc->offset])
279                 return -EBUSY;
280         str = strdup(label);
281         if (!str)
282                 return -ENOMEM;
283         if (gpio_get_ops(dev)->request) {
284                 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
285                 if (ret) {
286                         free(str);
287                         return ret;
288                 }
289         }
290         uc_priv->name[desc->offset] = str;
291
292         return 0;
293 }
294
295 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
296 {
297 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
298         va_list args;
299         char buf[40];
300
301         va_start(args, fmt);
302         vscnprintf(buf, sizeof(buf), fmt, args);
303         va_end(args);
304         return dm_gpio_request(desc, buf);
305 #else
306         return dm_gpio_request(desc, fmt);
307 #endif
308 }
309
310 /**
311  * gpio_request() - [COMPAT] Request GPIO
312  * gpio:        GPIO number
313  * label:       Name for the requested GPIO
314  *
315  * The label is copied and allocated so the caller does not need to keep
316  * the pointer around.
317  *
318  * This function implements the API that's compatible with current
319  * GPIO API used in U-Boot. The request is forwarded to particular
320  * GPIO driver. Returns 0 on success, negative value on error.
321  */
322 int gpio_request(unsigned gpio, const char *label)
323 {
324         struct gpio_desc desc;
325         int ret;
326
327         ret = gpio_to_device(gpio, &desc);
328         if (ret)
329                 return ret;
330
331         return dm_gpio_request(&desc, label);
332 }
333
334 /**
335  * gpio_requestf() - [COMPAT] Request GPIO
336  * @gpio:       GPIO number
337  * @fmt:        Format string for the requested GPIO
338  * @...:        Arguments for the printf() format string
339  *
340  * This function implements the API that's compatible with current
341  * GPIO API used in U-Boot. The request is forwarded to particular
342  * GPIO driver. Returns 0 on success, negative value on error.
343  */
344 int gpio_requestf(unsigned gpio, const char *fmt, ...)
345 {
346 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
347         va_list args;
348         char buf[40];
349
350         va_start(args, fmt);
351         vscnprintf(buf, sizeof(buf), fmt, args);
352         va_end(args);
353         return gpio_request(gpio, buf);
354 #else
355         return gpio_request(gpio, fmt);
356 #endif
357 }
358
359 int _dm_gpio_free(struct udevice *dev, uint offset)
360 {
361         struct gpio_dev_priv *uc_priv;
362         int ret;
363
364         uc_priv = dev_get_uclass_priv(dev);
365         if (!uc_priv->name[offset])
366                 return -ENXIO;
367         if (gpio_get_ops(dev)->rfree) {
368                 ret = gpio_get_ops(dev)->rfree(dev, offset);
369                 if (ret)
370                         return ret;
371         }
372
373         free(uc_priv->name[offset]);
374         uc_priv->name[offset] = NULL;
375
376         return 0;
377 }
378
379 /**
380  * gpio_free() - [COMPAT] Relinquish GPIO
381  * gpio:        GPIO number
382  *
383  * This function implements the API that's compatible with current
384  * GPIO API used in U-Boot. The request is forwarded to particular
385  * GPIO driver. Returns 0 on success, negative value on error.
386  */
387 int gpio_free(unsigned gpio)
388 {
389         struct gpio_desc desc;
390         int ret;
391
392         ret = gpio_to_device(gpio, &desc);
393         if (ret)
394                 return ret;
395
396         return _dm_gpio_free(desc.dev, desc.offset);
397 }
398
399 static int check_reserved(const struct gpio_desc *desc, const char *func)
400 {
401         struct gpio_dev_priv *uc_priv;
402
403         if (!dm_gpio_is_valid(desc))
404                 return -ENOENT;
405
406         uc_priv = dev_get_uclass_priv(desc->dev);
407         if (!uc_priv->name[desc->offset]) {
408                 printf("%s: %s: error: gpio %s%d not reserved\n",
409                        desc->dev->name, func,
410                        uc_priv->bank_name ? uc_priv->bank_name : "",
411                        desc->offset);
412                 return -EBUSY;
413         }
414
415         return 0;
416 }
417
418 /**
419  * gpio_direction_input() - [COMPAT] Set GPIO direction to input
420  * gpio:        GPIO number
421  *
422  * This function implements the API that's compatible with current
423  * GPIO API used in U-Boot. The request is forwarded to particular
424  * GPIO driver. Returns 0 on success, negative value on error.
425  */
426 int gpio_direction_input(unsigned gpio)
427 {
428         struct gpio_desc desc;
429         int ret;
430
431         ret = gpio_to_device(gpio, &desc);
432         if (ret)
433                 return ret;
434         ret = check_reserved(&desc, "dir_input");
435         if (ret)
436                 return ret;
437
438         return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
439 }
440
441 /**
442  * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
443  * gpio:        GPIO number
444  * value:       Logical value to be set on the GPIO pin
445  *
446  * This function implements the API that's compatible with current
447  * GPIO API used in U-Boot. The request is forwarded to particular
448  * GPIO driver. Returns 0 on success, negative value on error.
449  */
450 int gpio_direction_output(unsigned gpio, int value)
451 {
452         struct gpio_desc desc;
453         int ret;
454
455         ret = gpio_to_device(gpio, &desc);
456         if (ret)
457                 return ret;
458         ret = check_reserved(&desc, "dir_output");
459         if (ret)
460                 return ret;
461
462         return gpio_get_ops(desc.dev)->direction_output(desc.dev,
463                                                         desc.offset, value);
464 }
465
466 int dm_gpio_get_value(const struct gpio_desc *desc)
467 {
468         int value;
469         int ret;
470
471         ret = check_reserved(desc, "get_value");
472         if (ret)
473                 return ret;
474
475         value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
476
477         return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
478 }
479
480 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
481 {
482         int ret;
483
484         ret = check_reserved(desc, "set_value");
485         if (ret)
486                 return ret;
487
488         if (desc->flags & GPIOD_ACTIVE_LOW)
489                 value = !value;
490         gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
491         return 0;
492 }
493
494 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
495 {
496         struct udevice *dev = desc->dev;
497         struct dm_gpio_ops *ops = gpio_get_ops(dev);
498         int ret;
499
500         ret = check_reserved(desc, "set_dir");
501         if (ret)
502                 return ret;
503
504         if (flags & GPIOD_IS_OUT) {
505                 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
506
507                 if (flags & GPIOD_ACTIVE_LOW)
508                         value = !value;
509                 ret = ops->direction_output(dev, desc->offset, value);
510         } else  if (flags & GPIOD_IS_IN) {
511                 ret = ops->direction_input(dev, desc->offset);
512         }
513         if (ret)
514                 return ret;
515         /*
516          * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
517          * futures
518          */
519         desc->flags = flags;
520
521         return 0;
522 }
523
524 int dm_gpio_set_dir(struct gpio_desc *desc)
525 {
526         return dm_gpio_set_dir_flags(desc, desc->flags);
527 }
528
529 /**
530  * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
531  * gpio:        GPIO number
532  *
533  * This function implements the API that's compatible with current
534  * GPIO API used in U-Boot. The request is forwarded to particular
535  * GPIO driver. Returns the value of the GPIO pin, or negative value
536  * on error.
537  */
538 int gpio_get_value(unsigned gpio)
539 {
540         int ret;
541
542         struct gpio_desc desc;
543
544         ret = gpio_to_device(gpio, &desc);
545         if (ret)
546                 return ret;
547         return dm_gpio_get_value(&desc);
548 }
549
550 /**
551  * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
552  * gpio:        GPIO number
553  * value:       Logical value to be set on the GPIO pin.
554  *
555  * This function implements the API that's compatible with current
556  * GPIO API used in U-Boot. The request is forwarded to particular
557  * GPIO driver. Returns 0 on success, negative value on error.
558  */
559 int gpio_set_value(unsigned gpio, int value)
560 {
561         struct gpio_desc desc;
562         int ret;
563
564         ret = gpio_to_device(gpio, &desc);
565         if (ret)
566                 return ret;
567         return dm_gpio_set_value(&desc, value);
568 }
569
570 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
571 {
572         struct gpio_dev_priv *priv;
573
574         /* Must be called on an active device */
575         priv = dev_get_uclass_priv(dev);
576         assert(priv);
577
578         *bit_count = priv->gpio_count;
579         return priv->bank_name;
580 }
581
582 static const char * const gpio_function[GPIOF_COUNT] = {
583         "input",
584         "output",
585         "unused",
586         "unknown",
587         "func",
588 };
589
590 static int get_function(struct udevice *dev, int offset, bool skip_unused,
591                         const char **namep)
592 {
593         struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
594         struct dm_gpio_ops *ops = gpio_get_ops(dev);
595
596         BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
597         if (!device_active(dev))
598                 return -ENODEV;
599         if (offset < 0 || offset >= uc_priv->gpio_count)
600                 return -EINVAL;
601         if (namep)
602                 *namep = uc_priv->name[offset];
603         if (skip_unused && !uc_priv->name[offset])
604                 return GPIOF_UNUSED;
605         if (ops->get_function) {
606                 int ret;
607
608                 ret = ops->get_function(dev, offset);
609                 if (ret < 0)
610                         return ret;
611                 if (ret >= ARRAY_SIZE(gpio_function))
612                         return -ENODATA;
613                 return ret;
614         }
615
616         return GPIOF_UNKNOWN;
617 }
618
619 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
620 {
621         return get_function(dev, offset, true, namep);
622 }
623
624 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
625 {
626         return get_function(dev, offset, false, namep);
627 }
628
629 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
630 {
631         struct dm_gpio_ops *ops = gpio_get_ops(dev);
632         struct gpio_dev_priv *priv;
633         char *str = buf;
634         int func;
635         int ret;
636         int len;
637
638         BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
639
640         *buf = 0;
641         priv = dev_get_uclass_priv(dev);
642         ret = gpio_get_raw_function(dev, offset, NULL);
643         if (ret < 0)
644                 return ret;
645         func = ret;
646         len = snprintf(str, buffsize, "%s%d: %s",
647                        priv->bank_name ? priv->bank_name : "",
648                        offset, gpio_function[func]);
649         if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
650             func == GPIOF_UNUSED) {
651                 const char *label;
652                 bool used;
653
654                 ret = ops->get_value(dev, offset);
655                 if (ret < 0)
656                         return ret;
657                 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
658                 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
659                          ret,
660                          used ? 'x' : ' ',
661                          used ? " " : "",
662                          label ? label : "");
663         }
664
665         return 0;
666 }
667
668 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
669 {
670         int i, ret;
671         int gpio;
672
673         for (i = 0; i < 32; i++) {
674                 gpio = gpio_num_array[i];
675                 if (gpio == -1)
676                         break;
677                 ret = gpio_requestf(gpio, fmt, i);
678                 if (ret)
679                         goto err;
680                 ret = gpio_direction_input(gpio);
681                 if (ret) {
682                         gpio_free(gpio);
683                         goto err;
684                 }
685         }
686
687         return 0;
688 err:
689         for (i--; i >= 0; i--)
690                 gpio_free(gpio_num_array[i]);
691
692         return ret;
693 }
694
695 /*
696  * get a number comprised of multiple GPIO values. gpio_num_array points to
697  * the array of gpio pin numbers to scan, terminated by -1.
698  */
699 int gpio_get_values_as_int(const int *gpio_list)
700 {
701         int gpio;
702         unsigned bitmask = 1;
703         unsigned vector = 0;
704         int ret;
705
706         while (bitmask &&
707                ((gpio = *gpio_list++) != -1)) {
708                 ret = gpio_get_value(gpio);
709                 if (ret < 0)
710                         return ret;
711                 else if (ret)
712                         vector |= bitmask;
713                 bitmask <<= 1;
714         }
715
716         return vector;
717 }
718
719 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
720 {
721         unsigned bitmask = 1;
722         unsigned vector = 0;
723         int ret, i;
724
725         for (i = 0; i < count; i++) {
726                 ret = dm_gpio_get_value(&desc_list[i]);
727                 if (ret < 0)
728                         return ret;
729                 else if (ret)
730                         vector |= bitmask;
731                 bitmask <<= 1;
732         }
733
734         return vector;
735 }
736
737 /**
738  * gpio_request_tail: common work for requesting a gpio.
739  *
740  * ret:         return value from previous work in function which calls
741  *              this function.
742  *              This seems bogus (why calling this function instead not
743  *              calling it and end caller function instead?).
744  *              Because on error in caller function we want to set some
745  *              default values in gpio desc and have a common error
746  *              debug message, which provides this function.
747  * nodename:    Name of node for which gpio gets requested
748  *              used for gpio label name.
749  * args:        pointer to output arguments structure
750  * list_name:   Name of GPIO list
751  *              used for gpio label name.
752  * index:       gpio index in gpio list
753  *              used for gpio label name.
754  * desc:        pointer to gpio descriptor, filled from this
755  *              function.
756  * flags:       gpio flags to use.
757  * add_index:   should index added to gpio label name
758  * gpio_dev:    pointer to gpio device from which the gpio
759  *              will be requested. If NULL try to get the
760  *              gpio device with uclass_get_device_by_ofnode()
761  *
762  * return:      In error case this function sets default values in
763  *              gpio descriptor, also emmits a debug message.
764  *              On success it returns 0 else the error code from
765  *              function calls, or the error code passed through
766  *              ret to this function.
767  *
768  */
769 static int gpio_request_tail(int ret, const char *nodename,
770                              struct ofnode_phandle_args *args,
771                              const char *list_name, int index,
772                              struct gpio_desc *desc, int flags,
773                              bool add_index, struct udevice *gpio_dev)
774 {
775         desc->dev = gpio_dev;
776         desc->offset = 0;
777         desc->flags = 0;
778         if (ret)
779                 goto err;
780
781         if (!desc->dev) {
782                 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
783                                                   &desc->dev);
784                 if (ret) {
785                         debug("%s: uclass_get_device_by_ofnode failed\n",
786                               __func__);
787                         goto err;
788                 }
789         }
790         ret = gpio_find_and_xlate(desc, args);
791         if (ret) {
792                 debug("%s: gpio_find_and_xlate failed\n", __func__);
793                 goto err;
794         }
795         ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
796                                nodename, list_name, index);
797         if (ret) {
798                 debug("%s: dm_gpio_requestf failed\n", __func__);
799                 goto err;
800         }
801         ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
802         if (ret) {
803                 debug("%s: dm_gpio_set_dir failed\n", __func__);
804                 goto err;
805         }
806
807         return 0;
808 err:
809         debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
810               __func__, nodename, list_name, index, ret);
811         return ret;
812 }
813
814 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
815                                        int index, struct gpio_desc *desc,
816                                        int flags, bool add_index)
817 {
818         struct ofnode_phandle_args args;
819         int ret;
820
821         ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
822                                              index, &args);
823
824         return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
825                                  index, desc, flags, add_index, NULL);
826 }
827
828 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
829                                struct gpio_desc *desc, int flags)
830 {
831         return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
832                                            index > 0);
833 }
834
835 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
836                          struct gpio_desc *desc, int flags)
837 {
838         struct ofnode_phandle_args args;
839         ofnode node;
840         int ret;
841
842         ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
843                                          index, &args);
844         node = dev_ofnode(dev);
845         return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
846                                  index, desc, flags, index > 0, NULL);
847 }
848
849 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
850                                     struct gpio_desc *desc, int max_count,
851                                     int flags)
852 {
853         int count;
854         int ret;
855
856         for (count = 0; count < max_count; count++) {
857                 ret = _gpio_request_by_name_nodev(node, list_name, count,
858                                                   &desc[count], flags, true);
859                 if (ret == -ENOENT)
860                         break;
861                 else if (ret)
862                         goto err;
863         }
864
865         /* We ran out of GPIOs in the list */
866         return count;
867
868 err:
869         gpio_free_list_nodev(desc, count - 1);
870
871         return ret;
872 }
873
874 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
875                               struct gpio_desc *desc, int max_count,
876                               int flags)
877 {
878         /*
879          * This isn't ideal since we don't use dev->name in the debug()
880          * calls in gpio_request_by_name(), but we can do this until
881          * gpio_request_list_by_name_nodev() can be dropped.
882          */
883         return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
884                                                max_count, flags);
885 }
886
887 int gpio_get_list_count(struct udevice *dev, const char *list_name)
888 {
889         int ret;
890
891         ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
892                                              list_name, "#gpio-cells", 0, -1,
893                                              NULL);
894         if (ret) {
895                 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
896                       __func__, dev->name, list_name, ret);
897         }
898
899         return ret;
900 }
901
902 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
903 {
904         /* For now, we don't do any checking of dev */
905         return _dm_gpio_free(desc->dev, desc->offset);
906 }
907
908 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
909 {
910         int i;
911
912         /* For now, we don't do any checking of dev */
913         for (i = 0; i < count; i++)
914                 dm_gpio_free(dev, &desc[i]);
915
916         return 0;
917 }
918
919 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
920 {
921         return gpio_free_list(NULL, desc, count);
922 }
923
924 /* We need to renumber the GPIOs when any driver is probed/removed */
925 static int gpio_renumber(struct udevice *removed_dev)
926 {
927         struct gpio_dev_priv *uc_priv;
928         struct udevice *dev;
929         struct uclass *uc;
930         unsigned base;
931         int ret;
932
933         ret = uclass_get(UCLASS_GPIO, &uc);
934         if (ret)
935                 return ret;
936
937         /* Ensure that we have a base for each bank */
938         base = 0;
939         uclass_foreach_dev(dev, uc) {
940                 if (device_active(dev) && dev != removed_dev) {
941                         uc_priv = dev_get_uclass_priv(dev);
942                         uc_priv->gpio_base = base;
943                         base += uc_priv->gpio_count;
944                 }
945         }
946
947         return 0;
948 }
949
950 int gpio_get_number(const struct gpio_desc *desc)
951 {
952         struct udevice *dev = desc->dev;
953         struct gpio_dev_priv *uc_priv;
954
955         if (!dev)
956                 return -1;
957         uc_priv = dev->uclass_priv;
958
959         return uc_priv->gpio_base + desc->offset;
960 }
961
962 static int gpio_post_probe(struct udevice *dev)
963 {
964         struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
965
966         uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
967         if (!uc_priv->name)
968                 return -ENOMEM;
969
970         return gpio_renumber(NULL);
971 }
972
973 static int gpio_pre_remove(struct udevice *dev)
974 {
975         struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
976         int i;
977
978         for (i = 0; i < uc_priv->gpio_count; i++) {
979                 if (uc_priv->name[i])
980                         free(uc_priv->name[i]);
981         }
982         free(uc_priv->name);
983
984         return gpio_renumber(dev);
985 }
986
987 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
988                            char *list_name, int index, int flags,
989                            int dtflags, struct gpio_desc *desc)
990 {
991         struct ofnode_phandle_args args;
992
993         args.node =  ofnode_null();
994         args.args_count = 2;
995         args.args[0] = index;
996         args.args[1] = dtflags;
997
998         return gpio_request_tail(0, nodename, &args, list_name, index, desc,
999                                  flags, 0, dev);
1000 }
1001
1002 static int gpio_post_bind(struct udevice *dev)
1003 {
1004         struct udevice *child;
1005         ofnode node;
1006
1007 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1008         struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1009         static int reloc_done;
1010
1011         if (!reloc_done) {
1012                 if (ops->request)
1013                         ops->request += gd->reloc_off;
1014                 if (ops->rfree)
1015                         ops->rfree += gd->reloc_off;
1016                 if (ops->direction_input)
1017                         ops->direction_input += gd->reloc_off;
1018                 if (ops->direction_output)
1019                         ops->direction_output += gd->reloc_off;
1020                 if (ops->get_value)
1021                         ops->get_value += gd->reloc_off;
1022                 if (ops->set_value)
1023                         ops->set_value += gd->reloc_off;
1024                 if (ops->get_function)
1025                         ops->get_function += gd->reloc_off;
1026                 if (ops->xlate)
1027                         ops->xlate += gd->reloc_off;
1028
1029                 reloc_done++;
1030         }
1031 #endif
1032
1033         if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1034                 dev_for_each_subnode(node, dev) {
1035                         if (ofnode_read_bool(node, "gpio-hog")) {
1036                                 const char *name = ofnode_get_name(node);
1037                                 int ret;
1038
1039                                 ret = device_bind_driver_to_node(dev,
1040                                                                  "gpio_hog",
1041                                                                  name, node,
1042                                                                  &child);
1043                                 if (ret)
1044                                         return ret;
1045                         }
1046                 }
1047         }
1048         return 0;
1049 }
1050
1051 UCLASS_DRIVER(gpio) = {
1052         .id             = UCLASS_GPIO,
1053         .name           = "gpio",
1054         .flags          = DM_UC_FLAG_SEQ_ALIAS,
1055         .post_probe     = gpio_post_probe,
1056         .post_bind      = gpio_post_bind,
1057         .pre_remove     = gpio_pre_remove,
1058         .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
1059 };