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