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