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 ret = dev_read_stringlist_search(dev, "gpio-line-names", line_name);
1175 if (ret < 0)
1176 return ret;
1177
1178 desc->dev = dev;
1179 desc->offset = ret;
1180 desc->flags = 0;
1181
1182 ret = dm_gpio_request(desc, line_name);
1183 if (ret) {
1184 debug("%s: dm_gpio_requestf failed\n", __func__);
1185 return ret;
1186 }
1187
1188 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
1189 if (ret)
1190 debug("%s: dm_gpio_set_dir failed\n", __func__);
1191
1192 return ret;
1193}
1194
1195int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1196 struct gpio_desc *desc, int max_count,
1197 int flags)
1198{
1199 int count;
1200 int ret;
1201
1202 for (count = 0; count < max_count; count++) {
1203 ret = _gpio_request_by_name_nodev(node, list_name, count,
1204 &desc[count], flags, true);
1205 if (ret == -ENOENT)
1206 break;
1207 else if (ret)
1208 goto err;
1209 }
1210
1211
1212 return count;
1213
1214err:
1215 gpio_free_list_nodev(desc, count - 1);
1216
1217 return ret;
1218}
1219
1220int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1221 struct gpio_desc *desc, int max_count,
1222 int flags)
1223{
1224
1225
1226
1227
1228
1229 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1230 max_count, flags);
1231}
1232
1233int gpio_get_list_count(struct udevice *dev, const char *list_name)
1234{
1235 int ret;
1236
1237 ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells",
1238 -ENOENT);
1239 if (ret < 0) {
1240 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1241 __func__, dev->name, list_name, ret);
1242 }
1243
1244 return ret;
1245}
1246#endif
1247
1248#if CONFIG_IS_ENABLED(OF_PLATDATA)
1249int gpio_request_by_phandle(struct udevice *dev,
1250 const struct phandle_2_arg *cells,
1251 struct gpio_desc *desc, int flags)
1252{
1253 struct ofnode_phandle_args args;
1254 struct udevice *gpio_dev;
1255 const int index = 0;
1256 int ret;
1257
1258 ret = device_get_by_ofplat_idx(cells->idx, &gpio_dev);
1259 if (ret)
1260 return ret;
1261 args.args[0] = cells->arg[0];
1262 args.args[1] = cells->arg[1];
1263
1264 return gpio_request_tail(ret, NULL, &args, NULL, index, desc, flags,
1265 index > 0, gpio_dev);
1266}
1267#endif
1268
1269int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1270{
1271
1272 return _dm_gpio_free(desc->dev, desc->offset);
1273}
1274
1275int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1276{
1277 int i;
1278
1279
1280 for (i = 0; i < count; i++)
1281 dm_gpio_free(dev, &desc[i]);
1282
1283 return 0;
1284}
1285
1286int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1287{
1288 return gpio_free_list(NULL, desc, count);
1289}
1290
1291
1292static int gpio_renumber(struct udevice *removed_dev)
1293{
1294 struct gpio_dev_priv *uc_priv;
1295 struct udevice *dev;
1296 struct uclass *uc;
1297 unsigned base;
1298 int ret;
1299
1300 ret = uclass_get(UCLASS_GPIO, &uc);
1301 if (ret)
1302 return ret;
1303
1304
1305 base = 0;
1306 uclass_foreach_dev(dev, uc) {
1307 if (device_active(dev) && dev != removed_dev) {
1308 uc_priv = dev_get_uclass_priv(dev);
1309 uc_priv->gpio_base = base;
1310 base += uc_priv->gpio_count;
1311 }
1312 }
1313
1314 return 0;
1315}
1316
1317int gpio_get_number(const struct gpio_desc *desc)
1318{
1319 struct udevice *dev = desc->dev;
1320 struct gpio_dev_priv *uc_priv;
1321
1322 if (!dev)
1323 return -1;
1324 uc_priv = dev_get_uclass_priv(dev);
1325
1326 return uc_priv->gpio_base + desc->offset;
1327}
1328
1329static int gpio_post_probe(struct udevice *dev)
1330{
1331 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1332
1333 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1334 if (!uc_priv->name)
1335 return -ENOMEM;
1336
1337 return gpio_renumber(NULL);
1338}
1339
1340static int gpio_pre_remove(struct udevice *dev)
1341{
1342 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1343 int i;
1344
1345 for (i = 0; i < uc_priv->gpio_count; i++) {
1346 if (uc_priv->name[i])
1347 free(uc_priv->name[i]);
1348 }
1349 free(uc_priv->name);
1350
1351 return gpio_renumber(dev);
1352}
1353
1354int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1355 char *list_name, int index, int flags,
1356 int dtflags, struct gpio_desc *desc)
1357{
1358 struct ofnode_phandle_args args;
1359
1360 args.node = ofnode_null();
1361 args.args_count = 2;
1362 args.args[0] = index;
1363 args.args[1] = dtflags;
1364
1365 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1366 flags, 0, dev);
1367}
1368
1369static void devm_gpiod_release(struct udevice *dev, void *res)
1370{
1371 dm_gpio_free(dev, res);
1372}
1373
1374static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1375{
1376 return res == data;
1377}
1378
1379struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1380 unsigned int index, int flags)
1381{
1382 int rc;
1383 struct gpio_desc *desc;
1384 char *propname;
1385 static const char suffix[] = "-gpios";
1386
1387 propname = malloc(strlen(id) + sizeof(suffix));
1388 if (!propname) {
1389 rc = -ENOMEM;
1390 goto end;
1391 }
1392
1393 strcpy(propname, id);
1394 strcat(propname, suffix);
1395
1396 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1397 __GFP_ZERO);
1398 if (unlikely(!desc)) {
1399 rc = -ENOMEM;
1400 goto end;
1401 }
1402
1403 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1404
1405end:
1406 if (propname)
1407 free(propname);
1408
1409 if (rc)
1410 return ERR_PTR(rc);
1411
1412 devres_add(dev, desc);
1413
1414 return desc;
1415}
1416
1417struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1418 const char *id,
1419 unsigned int index,
1420 int flags)
1421{
1422 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1423
1424 if (IS_ERR(desc))
1425 return NULL;
1426
1427 return desc;
1428}
1429
1430void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1431{
1432 int rc;
1433
1434 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1435 WARN_ON(rc);
1436}
1437
1438static int gpio_post_bind(struct udevice *dev)
1439{
1440#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1441 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1442 static int reloc_done;
1443
1444 if (!reloc_done) {
1445 if (ops->request)
1446 ops->request += gd->reloc_off;
1447 if (ops->rfree)
1448 ops->rfree += gd->reloc_off;
1449 if (ops->direction_input)
1450 ops->direction_input += gd->reloc_off;
1451 if (ops->direction_output)
1452 ops->direction_output += gd->reloc_off;
1453 if (ops->get_value)
1454 ops->get_value += gd->reloc_off;
1455 if (ops->set_value)
1456 ops->set_value += gd->reloc_off;
1457 if (ops->get_function)
1458 ops->get_function += gd->reloc_off;
1459 if (ops->xlate)
1460 ops->xlate += gd->reloc_off;
1461 if (ops->set_flags)
1462 ops->set_flags += gd->reloc_off;
1463 if (ops->get_flags)
1464 ops->get_flags += gd->reloc_off;
1465
1466 reloc_done++;
1467 }
1468#endif
1469
1470 if (CONFIG_IS_ENABLED(GPIO_HOG)) {
1471 struct udevice *child;
1472 ofnode node;
1473
1474 dev_for_each_subnode(node, dev) {
1475 if (ofnode_read_bool(node, "gpio-hog")) {
1476 const char *name = ofnode_get_name(node);
1477 int ret;
1478
1479 ret = device_bind_driver_to_node(dev,
1480 "gpio_hog",
1481 name, node,
1482 &child);
1483 if (ret)
1484 return ret;
1485
1486
1487
1488
1489
1490
1491 dev_or_flags(child, DM_FLAG_PROBE_AFTER_BIND);
1492 }
1493 }
1494 }
1495
1496 return 0;
1497}
1498
1499UCLASS_DRIVER(gpio) = {
1500 .id = UCLASS_GPIO,
1501 .name = "gpio",
1502 .flags = DM_UC_FLAG_SEQ_ALIAS,
1503 .post_probe = gpio_post_probe,
1504 .post_bind = gpio_post_bind,
1505 .pre_remove = gpio_pre_remove,
1506 .per_device_auto = sizeof(struct gpio_dev_priv),
1507};
1508