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