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