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