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