1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2013 Google, Inc
4 */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <dm/device-internal.h>
9 #include <dm/lists.h>
10 #include <dm/uclass-internal.h>
11 #include <dt-bindings/gpio/gpio.h>
12 #include <errno.h>
13 #include <fdtdec.h>
14 #include <malloc.h>
15 #include <asm/gpio.h>
16 #include <linux/bug.h>
17 #include <linux/ctype.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 /**
22 * gpio_to_device() - Convert global GPIO number to device, number
23 *
24 * Convert the GPIO number to an entry in the list of GPIOs
25 * or GPIO blocks registered with the GPIO controller. Returns
26 * entry on success, NULL on error.
27 *
28 * @gpio: The numeric representation of the GPIO
29 * @desc: Returns description (desc->flags will always be 0)
30 * @return 0 if found, -ENOENT if not found
31 */
gpio_to_device(unsigned int gpio,struct gpio_desc * desc)32 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
33 {
34 struct gpio_dev_priv *uc_priv;
35 struct udevice *dev;
36 int ret;
37
38 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
39 dev;
40 ret = uclass_next_device(&dev)) {
41 uc_priv = dev_get_uclass_priv(dev);
42 if (gpio >= uc_priv->gpio_base &&
43 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
44 desc->dev = dev;
45 desc->offset = gpio - uc_priv->gpio_base;
46 desc->flags = 0;
47 return 0;
48 }
49 }
50
51 /* No such GPIO */
52 return ret ? ret : -ENOENT;
53 }
54
dm_gpio_lookup_name(const char * name,struct gpio_desc * desc)55 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
56 {
57 struct gpio_dev_priv *uc_priv = NULL;
58 struct udevice *dev;
59 ulong offset;
60 int numeric;
61 int ret;
62
63 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
64 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
65 dev;
66 ret = uclass_next_device(&dev)) {
67 int len;
68
69 uc_priv = dev_get_uclass_priv(dev);
70 if (numeric != -1) {
71 offset = numeric - uc_priv->gpio_base;
72 /* Allow GPIOs to be numbered from 0 */
73 if (offset < uc_priv->gpio_count)
74 break;
75 }
76
77 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
78
79 if (!strncasecmp(name, uc_priv->bank_name, len)) {
80 if (!strict_strtoul(name + len, 10, &offset))
81 break;
82 }
83 }
84
85 if (!dev)
86 return ret ? ret : -EINVAL;
87
88 desc->dev = dev;
89 desc->offset = offset;
90
91 return 0;
92 }
93
gpio_lookup_name(const char * name,struct udevice ** devp,unsigned int * offsetp,unsigned int * gpiop)94 int gpio_lookup_name(const char *name, struct udevice **devp,
95 unsigned int *offsetp, unsigned int *gpiop)
96 {
97 struct gpio_desc desc;
98 int ret;
99
100 if (devp)
101 *devp = NULL;
102 ret = dm_gpio_lookup_name(name, &desc);
103 if (ret)
104 return ret;
105
106 if (devp)
107 *devp = desc.dev;
108 if (offsetp)
109 *offsetp = desc.offset;
110 if (gpiop) {
111 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
112
113 *gpiop = uc_priv->gpio_base + desc.offset;
114 }
115
116 return 0;
117 }
118
gpio_xlate_offs_flags(struct udevice * dev,struct gpio_desc * desc,struct ofnode_phandle_args * args)119 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
120 struct ofnode_phandle_args *args)
121 {
122 if (args->args_count < 1)
123 return -EINVAL;
124
125 desc->offset = args->args[0];
126
127 if (args->args_count < 2)
128 return 0;
129
130 if (args->args[1] & GPIO_ACTIVE_LOW)
131 desc->flags = GPIOD_ACTIVE_LOW;
132
133 return 0;
134 }
135
gpio_find_and_xlate(struct gpio_desc * desc,struct ofnode_phandle_args * args)136 static int gpio_find_and_xlate(struct gpio_desc *desc,
137 struct ofnode_phandle_args *args)
138 {
139 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
140
141 if (ops->xlate)
142 return ops->xlate(desc->dev, desc, args);
143 else
144 return gpio_xlate_offs_flags(desc->dev, desc, args);
145 }
146
147 #if CONFIG_IS_ENABLED(GPIO_HOG)
148
149 struct gpio_hog_priv {
150 struct gpio_desc gpiod;
151 };
152
153 struct gpio_hog_data {
154 int gpiod_flags;
155 int value;
156 u32 val[2];
157 };
158
gpio_hog_ofdata_to_platdata(struct udevice * dev)159 static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
160 {
161 struct gpio_hog_data *plat = dev_get_platdata(dev);
162 const char *nodename;
163 int ret;
164
165 plat->value = 0;
166 if (dev_read_bool(dev, "input")) {
167 plat->gpiod_flags = GPIOD_IS_IN;
168 } else if (dev_read_bool(dev, "output-high")) {
169 plat->value = 1;
170 plat->gpiod_flags = GPIOD_IS_OUT;
171 } else if (dev_read_bool(dev, "output-low")) {
172 plat->gpiod_flags = GPIOD_IS_OUT;
173 } else {
174 printf("%s: missing gpio-hog state.\n", __func__);
175 return -EINVAL;
176 }
177 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
178 if (ret) {
179 printf("%s: wrong gpios property, 2 values needed %d\n",
180 __func__, ret);
181 return ret;
182 }
183 nodename = dev_read_string(dev, "line-name");
184 if (nodename)
185 device_set_name(dev, nodename);
186
187 return 0;
188 }
189
gpio_hog_probe(struct udevice * dev)190 static int gpio_hog_probe(struct udevice *dev)
191 {
192 struct gpio_hog_data *plat = dev_get_platdata(dev);
193 struct gpio_hog_priv *priv = dev_get_priv(dev);
194 int ret;
195
196 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
197 plat->val[0], plat->gpiod_flags,
198 plat->val[1], &priv->gpiod);
199 if (ret < 0) {
200 debug("%s: node %s could not get gpio.\n", __func__,
201 dev->name);
202 return ret;
203 }
204
205 if (plat->gpiod_flags == GPIOD_IS_OUT) {
206 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
207 if (ret < 0) {
208 debug("%s: node %s could not set gpio.\n", __func__,
209 dev->name);
210 return ret;
211 }
212 }
213
214 return 0;
215 }
216
gpio_hog_probe_all(void)217 int gpio_hog_probe_all(void)
218 {
219 struct udevice *dev;
220 int ret;
221 int retval = 0;
222
223 for (uclass_first_device(UCLASS_NOP, &dev);
224 dev;
225 uclass_find_next_device(&dev)) {
226 if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
227 ret = device_probe(dev);
228 if (ret) {
229 printf("Failed to probe device %s err: %d\n",
230 dev->name, ret);
231 retval = ret;
232 }
233 }
234 }
235
236 return retval;
237 }
238
gpio_hog_lookup_name(const char * name,struct gpio_desc ** desc)239 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
240 {
241 struct udevice *dev;
242
243 *desc = NULL;
244 gpio_hog_probe_all();
245 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
246 struct gpio_hog_priv *priv = dev_get_priv(dev);
247
248 *desc = &priv->gpiod;
249 return 0;
250 }
251
252 return -ENODEV;
253 }
254
255 U_BOOT_DRIVER(gpio_hog) = {
256 .name = "gpio_hog",
257 .id = UCLASS_NOP,
258 .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
259 .probe = gpio_hog_probe,
260 .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
261 .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
262 };
263 #else
gpio_hog_lookup_name(const char * name,struct gpio_desc ** desc)264 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
265 {
266 return 0;
267 }
268 #endif
269
dm_gpio_request(struct gpio_desc * desc,const char * label)270 int dm_gpio_request(struct gpio_desc *desc, const char *label)
271 {
272 struct udevice *dev = desc->dev;
273 struct gpio_dev_priv *uc_priv;
274 char *str;
275 int ret;
276
277 uc_priv = dev_get_uclass_priv(dev);
278 if (uc_priv->name[desc->offset])
279 return -EBUSY;
280 str = strdup(label);
281 if (!str)
282 return -ENOMEM;
283 if (gpio_get_ops(dev)->request) {
284 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
285 if (ret) {
286 free(str);
287 return ret;
288 }
289 }
290 uc_priv->name[desc->offset] = str;
291
292 return 0;
293 }
294
dm_gpio_requestf(struct gpio_desc * desc,const char * fmt,...)295 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
296 {
297 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
298 va_list args;
299 char buf[40];
300
301 va_start(args, fmt);
302 vscnprintf(buf, sizeof(buf), fmt, args);
303 va_end(args);
304 return dm_gpio_request(desc, buf);
305 #else
306 return dm_gpio_request(desc, fmt);
307 #endif
308 }
309
310 /**
311 * gpio_request() - [COMPAT] Request GPIO
312 * gpio: GPIO number
313 * label: Name for the requested GPIO
314 *
315 * The label is copied and allocated so the caller does not need to keep
316 * the pointer around.
317 *
318 * This function implements the API that's compatible with current
319 * GPIO API used in U-Boot. The request is forwarded to particular
320 * GPIO driver. Returns 0 on success, negative value on error.
321 */
gpio_request(unsigned gpio,const char * label)322 int gpio_request(unsigned gpio, const char *label)
323 {
324 struct gpio_desc desc;
325 int ret;
326
327 ret = gpio_to_device(gpio, &desc);
328 if (ret)
329 return ret;
330
331 return dm_gpio_request(&desc, label);
332 }
333
334 /**
335 * gpio_requestf() - [COMPAT] Request GPIO
336 * @gpio: GPIO number
337 * @fmt: Format string for the requested GPIO
338 * @...: Arguments for the printf() format string
339 *
340 * This function implements the API that's compatible with current
341 * GPIO API used in U-Boot. The request is forwarded to particular
342 * GPIO driver. Returns 0 on success, negative value on error.
343 */
gpio_requestf(unsigned gpio,const char * fmt,...)344 int gpio_requestf(unsigned gpio, const char *fmt, ...)
345 {
346 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
347 va_list args;
348 char buf[40];
349
350 va_start(args, fmt);
351 vscnprintf(buf, sizeof(buf), fmt, args);
352 va_end(args);
353 return gpio_request(gpio, buf);
354 #else
355 return gpio_request(gpio, fmt);
356 #endif
357 }
358
_dm_gpio_free(struct udevice * dev,uint offset)359 int _dm_gpio_free(struct udevice *dev, uint offset)
360 {
361 struct gpio_dev_priv *uc_priv;
362 int ret;
363
364 uc_priv = dev_get_uclass_priv(dev);
365 if (!uc_priv->name[offset])
366 return -ENXIO;
367 if (gpio_get_ops(dev)->free) {
368 ret = gpio_get_ops(dev)->free(dev, offset);
369 if (ret)
370 return ret;
371 }
372
373 free(uc_priv->name[offset]);
374 uc_priv->name[offset] = NULL;
375
376 return 0;
377 }
378
379 /**
380 * gpio_free() - [COMPAT] Relinquish GPIO
381 * gpio: GPIO number
382 *
383 * This function implements the API that's compatible with current
384 * GPIO API used in U-Boot. The request is forwarded to particular
385 * GPIO driver. Returns 0 on success, negative value on error.
386 */
gpio_free(unsigned gpio)387 int gpio_free(unsigned gpio)
388 {
389 struct gpio_desc desc;
390 int ret;
391
392 ret = gpio_to_device(gpio, &desc);
393 if (ret)
394 return ret;
395
396 return _dm_gpio_free(desc.dev, desc.offset);
397 }
398
check_reserved(const struct gpio_desc * desc,const char * func)399 static int check_reserved(const struct gpio_desc *desc, const char *func)
400 {
401 struct gpio_dev_priv *uc_priv;
402
403 if (!dm_gpio_is_valid(desc))
404 return -ENOENT;
405
406 uc_priv = dev_get_uclass_priv(desc->dev);
407 if (!uc_priv->name[desc->offset]) {
408 printf("%s: %s: error: gpio %s%d not reserved\n",
409 desc->dev->name, func,
410 uc_priv->bank_name ? uc_priv->bank_name : "",
411 desc->offset);
412 return -EBUSY;
413 }
414
415 return 0;
416 }
417
418 /**
419 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
420 * gpio: GPIO number
421 *
422 * This function implements the API that's compatible with current
423 * GPIO API used in U-Boot. The request is forwarded to particular
424 * GPIO driver. Returns 0 on success, negative value on error.
425 */
gpio_direction_input(unsigned gpio)426 int gpio_direction_input(unsigned gpio)
427 {
428 struct gpio_desc desc;
429 int ret;
430
431 ret = gpio_to_device(gpio, &desc);
432 if (ret)
433 return ret;
434 ret = check_reserved(&desc, "dir_input");
435 if (ret)
436 return ret;
437
438 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
439 }
440
441 /**
442 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
443 * gpio: GPIO number
444 * value: Logical value to be set on the GPIO pin
445 *
446 * This function implements the API that's compatible with current
447 * GPIO API used in U-Boot. The request is forwarded to particular
448 * GPIO driver. Returns 0 on success, negative value on error.
449 */
gpio_direction_output(unsigned gpio,int value)450 int gpio_direction_output(unsigned gpio, int value)
451 {
452 struct gpio_desc desc;
453 int ret;
454
455 ret = gpio_to_device(gpio, &desc);
456 if (ret)
457 return ret;
458 ret = check_reserved(&desc, "dir_output");
459 if (ret)
460 return ret;
461
462 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
463 desc.offset, value);
464 }
465
dm_gpio_get_value(const struct gpio_desc * desc)466 int dm_gpio_get_value(const struct gpio_desc *desc)
467 {
468 int value;
469 int ret;
470
471 ret = check_reserved(desc, "get_value");
472 if (ret)
473 return ret;
474
475 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
476
477 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
478 }
479
dm_gpio_set_value(const struct gpio_desc * desc,int value)480 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
481 {
482 int ret;
483
484 ret = check_reserved(desc, "set_value");
485 if (ret)
486 return ret;
487
488 if (desc->flags & GPIOD_ACTIVE_LOW)
489 value = !value;
490 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
491 return 0;
492 }
493
dm_gpio_get_open_drain(struct gpio_desc * desc)494 int dm_gpio_get_open_drain(struct gpio_desc *desc)
495 {
496 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
497 int ret;
498
499 ret = check_reserved(desc, "get_open_drain");
500 if (ret)
501 return ret;
502
503 if (ops->set_open_drain)
504 return ops->get_open_drain(desc->dev, desc->offset);
505 else
506 return -ENOSYS;
507 }
508
dm_gpio_set_open_drain(struct gpio_desc * desc,int value)509 int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
510 {
511 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
512 int ret;
513
514 ret = check_reserved(desc, "set_open_drain");
515 if (ret)
516 return ret;
517
518 if (ops->set_open_drain)
519 ret = ops->set_open_drain(desc->dev, desc->offset, value);
520 else
521 return 0; /* feature not supported -> ignore setting */
522
523 return ret;
524 }
525
dm_gpio_set_dir_flags(struct gpio_desc * desc,ulong flags)526 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
527 {
528 struct udevice *dev = desc->dev;
529 struct dm_gpio_ops *ops = gpio_get_ops(dev);
530 int ret;
531
532 ret = check_reserved(desc, "set_dir");
533 if (ret)
534 return ret;
535
536 if (flags & GPIOD_IS_OUT) {
537 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
538
539 if (flags & GPIOD_ACTIVE_LOW)
540 value = !value;
541 ret = ops->direction_output(dev, desc->offset, value);
542 } else if (flags & GPIOD_IS_IN) {
543 ret = ops->direction_input(dev, desc->offset);
544 }
545 if (ret)
546 return ret;
547 /*
548 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
549 * futures
550 */
551 desc->flags = flags;
552
553 return 0;
554 }
555
dm_gpio_set_dir(struct gpio_desc * desc)556 int dm_gpio_set_dir(struct gpio_desc *desc)
557 {
558 return dm_gpio_set_dir_flags(desc, desc->flags);
559 }
560
561 /**
562 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
563 * gpio: GPIO number
564 *
565 * This function implements the API that's compatible with current
566 * GPIO API used in U-Boot. The request is forwarded to particular
567 * GPIO driver. Returns the value of the GPIO pin, or negative value
568 * on error.
569 */
gpio_get_value(unsigned gpio)570 int gpio_get_value(unsigned gpio)
571 {
572 int ret;
573
574 struct gpio_desc desc;
575
576 ret = gpio_to_device(gpio, &desc);
577 if (ret)
578 return ret;
579 return dm_gpio_get_value(&desc);
580 }
581
582 /**
583 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
584 * gpio: GPIO number
585 * value: Logical value to be set on the GPIO pin.
586 *
587 * This function implements the API that's compatible with current
588 * GPIO API used in U-Boot. The request is forwarded to particular
589 * GPIO driver. Returns 0 on success, negative value on error.
590 */
gpio_set_value(unsigned gpio,int value)591 int gpio_set_value(unsigned gpio, int value)
592 {
593 struct gpio_desc desc;
594 int ret;
595
596 ret = gpio_to_device(gpio, &desc);
597 if (ret)
598 return ret;
599 return dm_gpio_set_value(&desc, value);
600 }
601
gpio_get_bank_info(struct udevice * dev,int * bit_count)602 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
603 {
604 struct gpio_dev_priv *priv;
605
606 /* Must be called on an active device */
607 priv = dev_get_uclass_priv(dev);
608 assert(priv);
609
610 *bit_count = priv->gpio_count;
611 return priv->bank_name;
612 }
613
614 static const char * const gpio_function[GPIOF_COUNT] = {
615 "input",
616 "output",
617 "unused",
618 "unknown",
619 "func",
620 };
621
get_function(struct udevice * dev,int offset,bool skip_unused,const char ** namep)622 static int get_function(struct udevice *dev, int offset, bool skip_unused,
623 const char **namep)
624 {
625 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
626 struct dm_gpio_ops *ops = gpio_get_ops(dev);
627
628 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
629 if (!device_active(dev))
630 return -ENODEV;
631 if (offset < 0 || offset >= uc_priv->gpio_count)
632 return -EINVAL;
633 if (namep)
634 *namep = uc_priv->name[offset];
635 if (skip_unused && !uc_priv->name[offset])
636 return GPIOF_UNUSED;
637 if (ops->get_function) {
638 int ret;
639
640 ret = ops->get_function(dev, offset);
641 if (ret < 0)
642 return ret;
643 if (ret >= ARRAY_SIZE(gpio_function))
644 return -ENODATA;
645 return ret;
646 }
647
648 return GPIOF_UNKNOWN;
649 }
650
gpio_get_function(struct udevice * dev,int offset,const char ** namep)651 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
652 {
653 return get_function(dev, offset, true, namep);
654 }
655
gpio_get_raw_function(struct udevice * dev,int offset,const char ** namep)656 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
657 {
658 return get_function(dev, offset, false, namep);
659 }
660
gpio_get_status(struct udevice * dev,int offset,char * buf,int buffsize)661 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
662 {
663 struct dm_gpio_ops *ops = gpio_get_ops(dev);
664 struct gpio_dev_priv *priv;
665 char *str = buf;
666 int func;
667 int ret;
668 int len;
669
670 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
671
672 *buf = 0;
673 priv = dev_get_uclass_priv(dev);
674 ret = gpio_get_raw_function(dev, offset, NULL);
675 if (ret < 0)
676 return ret;
677 func = ret;
678 len = snprintf(str, buffsize, "%s%d: %s",
679 priv->bank_name ? priv->bank_name : "",
680 offset, gpio_function[func]);
681 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
682 func == GPIOF_UNUSED) {
683 const char *label;
684 bool used;
685
686 ret = ops->get_value(dev, offset);
687 if (ret < 0)
688 return ret;
689 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
690 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
691 ret,
692 used ? 'x' : ' ',
693 used ? " " : "",
694 label ? label : "");
695 }
696
697 return 0;
698 }
699
gpio_claim_vector(const int * gpio_num_array,const char * fmt)700 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
701 {
702 int i, ret;
703 int gpio;
704
705 for (i = 0; i < 32; i++) {
706 gpio = gpio_num_array[i];
707 if (gpio == -1)
708 break;
709 ret = gpio_requestf(gpio, fmt, i);
710 if (ret)
711 goto err;
712 ret = gpio_direction_input(gpio);
713 if (ret) {
714 gpio_free(gpio);
715 goto err;
716 }
717 }
718
719 return 0;
720 err:
721 for (i--; i >= 0; i--)
722 gpio_free(gpio_num_array[i]);
723
724 return ret;
725 }
726
727 /*
728 * get a number comprised of multiple GPIO values. gpio_num_array points to
729 * the array of gpio pin numbers to scan, terminated by -1.
730 */
gpio_get_values_as_int(const int * gpio_list)731 int gpio_get_values_as_int(const int *gpio_list)
732 {
733 int gpio;
734 unsigned bitmask = 1;
735 unsigned vector = 0;
736 int ret;
737
738 while (bitmask &&
739 ((gpio = *gpio_list++) != -1)) {
740 ret = gpio_get_value(gpio);
741 if (ret < 0)
742 return ret;
743 else if (ret)
744 vector |= bitmask;
745 bitmask <<= 1;
746 }
747
748 return vector;
749 }
750
dm_gpio_get_values_as_int(const struct gpio_desc * desc_list,int count)751 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
752 {
753 unsigned bitmask = 1;
754 unsigned vector = 0;
755 int ret, i;
756
757 for (i = 0; i < count; i++) {
758 ret = dm_gpio_get_value(&desc_list[i]);
759 if (ret < 0)
760 return ret;
761 else if (ret)
762 vector |= bitmask;
763 bitmask <<= 1;
764 }
765
766 return vector;
767 }
768
769 /**
770 * gpio_request_tail: common work for requesting a gpio.
771 *
772 * ret: return value from previous work in function which calls
773 * this function.
774 * This seems bogus (why calling this function instead not
775 * calling it and end caller function instead?).
776 * Because on error in caller function we want to set some
777 * default values in gpio desc and have a common error
778 * debug message, which provides this function.
779 * nodename: Name of node for which gpio gets requested
780 * used for gpio label name.
781 * args: pointer to output arguments structure
782 * list_name: Name of GPIO list
783 * used for gpio label name.
784 * index: gpio index in gpio list
785 * used for gpio label name.
786 * desc: pointer to gpio descriptor, filled from this
787 * function.
788 * flags: gpio flags to use.
789 * add_index: should index added to gpio label name
790 * gpio_dev: pointer to gpio device from which the gpio
791 * will be requested. If NULL try to get the
792 * gpio device with uclass_get_device_by_ofnode()
793 *
794 * return: In error case this function sets default values in
795 * gpio descriptor, also emmits a debug message.
796 * On success it returns 0 else the error code from
797 * function calls, or the error code passed through
798 * ret to this function.
799 *
800 */
gpio_request_tail(int ret,const char * nodename,struct ofnode_phandle_args * args,const char * list_name,int index,struct gpio_desc * desc,int flags,bool add_index,struct udevice * gpio_dev)801 static int gpio_request_tail(int ret, const char *nodename,
802 struct ofnode_phandle_args *args,
803 const char *list_name, int index,
804 struct gpio_desc *desc, int flags,
805 bool add_index, struct udevice *gpio_dev)
806 {
807 desc->dev = gpio_dev;
808 desc->offset = 0;
809 desc->flags = 0;
810 if (ret)
811 goto err;
812
813 if (!desc->dev) {
814 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
815 &desc->dev);
816 if (ret) {
817 debug("%s: uclass_get_device_by_ofnode failed\n",
818 __func__);
819 goto err;
820 }
821 }
822 ret = gpio_find_and_xlate(desc, args);
823 if (ret) {
824 debug("%s: gpio_find_and_xlate failed\n", __func__);
825 goto err;
826 }
827 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
828 nodename, list_name, index);
829 if (ret) {
830 debug("%s: dm_gpio_requestf failed\n", __func__);
831 goto err;
832 }
833 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
834 if (ret) {
835 debug("%s: dm_gpio_set_dir failed\n", __func__);
836 goto err;
837 }
838
839 return 0;
840 err:
841 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
842 __func__, nodename, list_name, index, ret);
843 return ret;
844 }
845
_gpio_request_by_name_nodev(ofnode node,const char * list_name,int index,struct gpio_desc * desc,int flags,bool add_index)846 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
847 int index, struct gpio_desc *desc,
848 int flags, bool add_index)
849 {
850 struct ofnode_phandle_args args;
851 int ret;
852
853 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
854 index, &args);
855
856 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
857 index, desc, flags, add_index, NULL);
858 }
859
gpio_request_by_name_nodev(ofnode node,const char * list_name,int index,struct gpio_desc * desc,int flags)860 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
861 struct gpio_desc *desc, int flags)
862 {
863 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
864 index > 0);
865 }
866
gpio_request_by_name(struct udevice * dev,const char * list_name,int index,struct gpio_desc * desc,int flags)867 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
868 struct gpio_desc *desc, int flags)
869 {
870 struct ofnode_phandle_args args;
871 ofnode node;
872 int ret;
873
874 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
875 index, &args);
876 node = dev_ofnode(dev);
877 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
878 index, desc, flags, index > 0, NULL);
879 }
880
gpio_request_list_by_name_nodev(ofnode node,const char * list_name,struct gpio_desc * desc,int max_count,int flags)881 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
882 struct gpio_desc *desc, int max_count,
883 int flags)
884 {
885 int count;
886 int ret;
887
888 for (count = 0; count < max_count; count++) {
889 ret = _gpio_request_by_name_nodev(node, list_name, count,
890 &desc[count], flags, true);
891 if (ret == -ENOENT)
892 break;
893 else if (ret)
894 goto err;
895 }
896
897 /* We ran out of GPIOs in the list */
898 return count;
899
900 err:
901 gpio_free_list_nodev(desc, count - 1);
902
903 return ret;
904 }
905
gpio_request_list_by_name(struct udevice * dev,const char * list_name,struct gpio_desc * desc,int max_count,int flags)906 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
907 struct gpio_desc *desc, int max_count,
908 int flags)
909 {
910 /*
911 * This isn't ideal since we don't use dev->name in the debug()
912 * calls in gpio_request_by_name(), but we can do this until
913 * gpio_request_list_by_name_nodev() can be dropped.
914 */
915 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
916 max_count, flags);
917 }
918
gpio_get_list_count(struct udevice * dev,const char * list_name)919 int gpio_get_list_count(struct udevice *dev, const char *list_name)
920 {
921 int ret;
922
923 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
924 list_name, "#gpio-cells", 0, -1,
925 NULL);
926 if (ret) {
927 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
928 __func__, dev->name, list_name, ret);
929 }
930
931 return ret;
932 }
933
dm_gpio_free(struct udevice * dev,struct gpio_desc * desc)934 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
935 {
936 /* For now, we don't do any checking of dev */
937 return _dm_gpio_free(desc->dev, desc->offset);
938 }
939
gpio_free_list(struct udevice * dev,struct gpio_desc * desc,int count)940 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
941 {
942 int i;
943
944 /* For now, we don't do any checking of dev */
945 for (i = 0; i < count; i++)
946 dm_gpio_free(dev, &desc[i]);
947
948 return 0;
949 }
950
gpio_free_list_nodev(struct gpio_desc * desc,int count)951 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
952 {
953 return gpio_free_list(NULL, desc, count);
954 }
955
956 /* We need to renumber the GPIOs when any driver is probed/removed */
gpio_renumber(struct udevice * removed_dev)957 static int gpio_renumber(struct udevice *removed_dev)
958 {
959 struct gpio_dev_priv *uc_priv;
960 struct udevice *dev;
961 struct uclass *uc;
962 unsigned base;
963 int ret;
964
965 ret = uclass_get(UCLASS_GPIO, &uc);
966 if (ret)
967 return ret;
968
969 /* Ensure that we have a base for each bank */
970 base = 0;
971 uclass_foreach_dev(dev, uc) {
972 if (device_active(dev) && dev != removed_dev) {
973 uc_priv = dev_get_uclass_priv(dev);
974 uc_priv->gpio_base = base;
975 base += uc_priv->gpio_count;
976 }
977 }
978
979 return 0;
980 }
981
gpio_get_number(const struct gpio_desc * desc)982 int gpio_get_number(const struct gpio_desc *desc)
983 {
984 struct udevice *dev = desc->dev;
985 struct gpio_dev_priv *uc_priv;
986
987 if (!dev)
988 return -1;
989 uc_priv = dev->uclass_priv;
990
991 return uc_priv->gpio_base + desc->offset;
992 }
993
gpio_post_probe(struct udevice * dev)994 static int gpio_post_probe(struct udevice *dev)
995 {
996 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
997
998 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
999 if (!uc_priv->name)
1000 return -ENOMEM;
1001
1002 return gpio_renumber(NULL);
1003 }
1004
gpio_pre_remove(struct udevice * dev)1005 static int gpio_pre_remove(struct udevice *dev)
1006 {
1007 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1008 int i;
1009
1010 for (i = 0; i < uc_priv->gpio_count; i++) {
1011 if (uc_priv->name[i])
1012 free(uc_priv->name[i]);
1013 }
1014 free(uc_priv->name);
1015
1016 return gpio_renumber(dev);
1017 }
1018
gpio_dev_request_index(struct udevice * dev,const char * nodename,char * list_name,int index,int flags,int dtflags,struct gpio_desc * desc)1019 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1020 char *list_name, int index, int flags,
1021 int dtflags, struct gpio_desc *desc)
1022 {
1023 struct ofnode_phandle_args args;
1024
1025 args.node = ofnode_null();
1026 args.args_count = 2;
1027 args.args[0] = index;
1028 args.args[1] = dtflags;
1029
1030 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1031 flags, 0, dev);
1032 }
1033
gpio_post_bind(struct udevice * dev)1034 static int gpio_post_bind(struct udevice *dev)
1035 {
1036 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1037 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1038 static int reloc_done;
1039
1040 if (!reloc_done) {
1041 if (ops->request)
1042 ops->request += gd->reloc_off;
1043 if (ops->free)
1044 ops->free += gd->reloc_off;
1045 if (ops->direction_input)
1046 ops->direction_input += gd->reloc_off;
1047 if (ops->direction_output)
1048 ops->direction_output += gd->reloc_off;
1049 if (ops->get_value)
1050 ops->get_value += gd->reloc_off;
1051 if (ops->set_value)
1052 ops->set_value += gd->reloc_off;
1053 if (ops->get_open_drain)
1054 ops->get_open_drain += gd->reloc_off;
1055 if (ops->set_open_drain)
1056 ops->set_open_drain += gd->reloc_off;
1057 if (ops->get_function)
1058 ops->get_function += gd->reloc_off;
1059 if (ops->xlate)
1060 ops->xlate += gd->reloc_off;
1061
1062 reloc_done++;
1063 }
1064 #endif
1065
1066 if (CONFIG_IS_ENABLED(GPIO_HOG)) {
1067 struct udevice *child;
1068 ofnode node;
1069
1070 dev_for_each_subnode(node, dev) {
1071 if (ofnode_read_bool(node, "gpio-hog")) {
1072 const char *name = ofnode_get_name(node);
1073 int ret;
1074
1075 ret = device_bind_driver_to_node(dev,
1076 "gpio_hog",
1077 name, node,
1078 &child);
1079 if (ret)
1080 return ret;
1081 }
1082 }
1083 }
1084 return 0;
1085 }
1086
1087 UCLASS_DRIVER(gpio) = {
1088 .id = UCLASS_GPIO,
1089 .name = "gpio",
1090 .flags = DM_UC_FLAG_SEQ_ALIAS,
1091 .post_probe = gpio_post_probe,
1092 .post_bind = gpio_post_bind,
1093 .pre_remove = gpio_pre_remove,
1094 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
1095 };
1096