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