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