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