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