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