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