]> git.ipfire.org Git - thirdparty/u-boot.git/blame - drivers/gpio/gpio-uclass.c
SPDX: Convert all of our single license tags to Linux Kernel style
[thirdparty/u-boot.git] / drivers / gpio / gpio-uclass.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
96495d90
SG
2/*
3 * Copyright (c) 2013 Google, Inc
96495d90
SG
4 */
5
6#include <common.h>
7#include <dm.h>
6c880b77 8#include <dt-bindings/gpio/gpio.h>
96495d90 9#include <errno.h>
0dac4d51 10#include <fdtdec.h>
b892d127 11#include <malloc.h>
96495d90 12#include <asm/gpio.h>
84b8bf6d 13#include <linux/bug.h>
fe1ef503 14#include <linux/ctype.h>
96495d90 15
3669e0e7
SG
16DECLARE_GLOBAL_DATA_PTR;
17
96495d90
SG
18/**
19 * gpio_to_device() - Convert global GPIO number to device, number
96495d90
SG
20 *
21 * Convert the GPIO number to an entry in the list of GPIOs
22 * or GPIO blocks registered with the GPIO controller. Returns
23 * entry on success, NULL on error.
ae7123f8
SG
24 *
25 * @gpio: The numeric representation of the GPIO
26 * @desc: Returns description (desc->flags will always be 0)
27 * @return 0 if found, -ENOENT if not found
96495d90 28 */
ae7123f8 29static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
96495d90
SG
30{
31 struct gpio_dev_priv *uc_priv;
54c5d08a 32 struct udevice *dev;
96495d90
SG
33 int ret;
34
35 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
36 dev;
37 ret = uclass_next_device(&dev)) {
e564f054 38 uc_priv = dev_get_uclass_priv(dev);
96495d90
SG
39 if (gpio >= uc_priv->gpio_base &&
40 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
ae7123f8
SG
41 desc->dev = dev;
42 desc->offset = gpio - uc_priv->gpio_base;
43 desc->flags = 0;
96495d90
SG
44 return 0;
45 }
46 }
47
48 /* No such GPIO */
ae7123f8 49 return ret ? ret : -ENOENT;
96495d90
SG
50}
51
32ec1598 52int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
96495d90 53{
fe1ef503 54 struct gpio_dev_priv *uc_priv = NULL;
54c5d08a 55 struct udevice *dev;
fe1ef503
SG
56 ulong offset;
57 int numeric;
96495d90
SG
58 int ret;
59
fe1ef503 60 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
96495d90
SG
61 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
62 dev;
63 ret = uclass_next_device(&dev)) {
96495d90
SG
64 int len;
65
e564f054 66 uc_priv = dev_get_uclass_priv(dev);
fe1ef503
SG
67 if (numeric != -1) {
68 offset = numeric - uc_priv->gpio_base;
69 /* Allow GPIOs to be numbered from 0 */
75897910 70 if (offset < uc_priv->gpio_count)
fe1ef503
SG
71 break;
72 }
73
96495d90
SG
74 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
75
939cda5b 76 if (!strncasecmp(name, uc_priv->bank_name, len)) {
fe1ef503
SG
77 if (!strict_strtoul(name + len, 10, &offset))
78 break;
96495d90
SG
79 }
80 }
81
fe1ef503
SG
82 if (!dev)
83 return ret ? ret : -EINVAL;
84
32ec1598
SG
85 desc->dev = dev;
86 desc->offset = offset;
87
88 return 0;
89}
90
91int gpio_lookup_name(const char *name, struct udevice **devp,
92 unsigned int *offsetp, unsigned int *gpiop)
93{
94 struct gpio_desc desc;
95 int ret;
96
fe1ef503 97 if (devp)
32ec1598
SG
98 *devp = NULL;
99 ret = dm_gpio_lookup_name(name, &desc);
100 if (ret)
101 return ret;
102
103 if (devp)
104 *devp = desc.dev;
fe1ef503 105 if (offsetp)
32ec1598
SG
106 *offsetp = desc.offset;
107 if (gpiop) {
108 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
109
110 *gpiop = uc_priv->gpio_base + desc.offset;
111 }
fe1ef503
SG
112
113 return 0;
96495d90
SG
114}
115
3a57123e
SG
116int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
117 struct ofnode_phandle_args *args)
6c880b77
EN
118{
119 if (args->args_count < 1)
120 return -EINVAL;
121
122 desc->offset = args->args[0];
123
124 if (args->args_count < 2)
125 return 0;
126
127 if (args->args[1] & GPIO_ACTIVE_LOW)
128 desc->flags = GPIOD_ACTIVE_LOW;
129
130 return 0;
131}
132
3669e0e7 133static int gpio_find_and_xlate(struct gpio_desc *desc,
3a57123e 134 struct ofnode_phandle_args *args)
0dac4d51
SG
135{
136 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
137
6c880b77
EN
138 if (ops->xlate)
139 return ops->xlate(desc->dev, desc, args);
0dac4d51 140 else
6c880b77 141 return gpio_xlate_offs_flags(desc->dev, desc, args);
0dac4d51
SG
142}
143
efa677fb 144int dm_gpio_request(struct gpio_desc *desc, const char *label)
ae7123f8
SG
145{
146 struct udevice *dev = desc->dev;
147 struct gpio_dev_priv *uc_priv;
148 char *str;
149 int ret;
150
e564f054 151 uc_priv = dev_get_uclass_priv(dev);
ae7123f8
SG
152 if (uc_priv->name[desc->offset])
153 return -EBUSY;
154 str = strdup(label);
155 if (!str)
156 return -ENOMEM;
157 if (gpio_get_ops(dev)->request) {
158 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
159 if (ret) {
160 free(str);
161 return ret;
162 }
163 }
164 uc_priv->name[desc->offset] = str;
165
166 return 0;
167}
168
3669e0e7
SG
169static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
170{
4dc5259a 171#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
3669e0e7
SG
172 va_list args;
173 char buf[40];
174
175 va_start(args, fmt);
176 vscnprintf(buf, sizeof(buf), fmt, args);
177 va_end(args);
178 return dm_gpio_request(desc, buf);
4dc5259a
SG
179#else
180 return dm_gpio_request(desc, fmt);
181#endif
3669e0e7
SG
182}
183
96495d90
SG
184/**
185 * gpio_request() - [COMPAT] Request GPIO
186 * gpio: GPIO number
187 * label: Name for the requested GPIO
188 *
b892d127
SG
189 * The label is copied and allocated so the caller does not need to keep
190 * the pointer around.
191 *
96495d90
SG
192 * This function implements the API that's compatible with current
193 * GPIO API used in U-Boot. The request is forwarded to particular
194 * GPIO driver. Returns 0 on success, negative value on error.
195 */
196int gpio_request(unsigned gpio, const char *label)
197{
ae7123f8 198 struct gpio_desc desc;
96495d90
SG
199 int ret;
200
ae7123f8 201 ret = gpio_to_device(gpio, &desc);
96495d90
SG
202 if (ret)
203 return ret;
204
ae7123f8 205 return dm_gpio_request(&desc, label);
96495d90
SG
206}
207
d44f597b
SG
208/**
209 * gpio_requestf() - [COMPAT] Request GPIO
210 * @gpio: GPIO number
211 * @fmt: Format string for the requested GPIO
212 * @...: Arguments for the printf() format string
213 *
214 * This function implements the API that's compatible with current
215 * GPIO API used in U-Boot. The request is forwarded to particular
216 * GPIO driver. Returns 0 on success, negative value on error.
217 */
218int gpio_requestf(unsigned gpio, const char *fmt, ...)
219{
4dc5259a 220#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
d44f597b
SG
221 va_list args;
222 char buf[40];
223
224 va_start(args, fmt);
225 vscnprintf(buf, sizeof(buf), fmt, args);
226 va_end(args);
227 return gpio_request(gpio, buf);
4dc5259a
SG
228#else
229 return gpio_request(gpio, fmt);
230#endif
d44f597b
SG
231}
232
ae7123f8 233int _dm_gpio_free(struct udevice *dev, uint offset)
96495d90 234{
b892d127 235 struct gpio_dev_priv *uc_priv;
96495d90
SG
236 int ret;
237
e564f054 238 uc_priv = dev_get_uclass_priv(dev);
b892d127
SG
239 if (!uc_priv->name[offset])
240 return -ENXIO;
241 if (gpio_get_ops(dev)->free) {
242 ret = gpio_get_ops(dev)->free(dev, offset);
243 if (ret)
244 return ret;
245 }
246
247 free(uc_priv->name[offset]);
248 uc_priv->name[offset] = NULL;
249
250 return 0;
251}
252
ae7123f8
SG
253/**
254 * gpio_free() - [COMPAT] Relinquish GPIO
255 * gpio: GPIO number
256 *
257 * This function implements the API that's compatible with current
258 * GPIO API used in U-Boot. The request is forwarded to particular
259 * GPIO driver. Returns 0 on success, negative value on error.
260 */
261int gpio_free(unsigned gpio)
b892d127 262{
ae7123f8
SG
263 struct gpio_desc desc;
264 int ret;
265
266 ret = gpio_to_device(gpio, &desc);
267 if (ret)
268 return ret;
269
270 return _dm_gpio_free(desc.dev, desc.offset);
271}
b892d127 272
17c43f1a 273static int check_reserved(const struct gpio_desc *desc, const char *func)
ae7123f8 274{
eca48665
SG
275 struct gpio_dev_priv *uc_priv;
276
277 if (!dm_gpio_is_valid(desc))
278 return -ENOENT;
ae7123f8 279
eca48665 280 uc_priv = dev_get_uclass_priv(desc->dev);
ae7123f8 281 if (!uc_priv->name[desc->offset]) {
b892d127 282 printf("%s: %s: error: gpio %s%d not reserved\n",
ae7123f8
SG
283 desc->dev->name, func,
284 uc_priv->bank_name ? uc_priv->bank_name : "",
285 desc->offset);
b892d127
SG
286 return -EBUSY;
287 }
288
289 return 0;
96495d90
SG
290}
291
292/**
293 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
294 * gpio: GPIO number
295 *
296 * This function implements the API that's compatible with current
297 * GPIO API used in U-Boot. The request is forwarded to particular
298 * GPIO driver. Returns 0 on success, negative value on error.
299 */
300int gpio_direction_input(unsigned gpio)
301{
ae7123f8 302 struct gpio_desc desc;
96495d90
SG
303 int ret;
304
ae7123f8
SG
305 ret = gpio_to_device(gpio, &desc);
306 if (ret)
307 return ret;
308 ret = check_reserved(&desc, "dir_input");
96495d90
SG
309 if (ret)
310 return ret;
311
ae7123f8 312 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
96495d90
SG
313}
314
315/**
316 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
317 * gpio: GPIO number
318 * value: Logical value to be set on the GPIO pin
319 *
320 * This function implements the API that's compatible with current
321 * GPIO API used in U-Boot. The request is forwarded to particular
322 * GPIO driver. Returns 0 on success, negative value on error.
323 */
324int gpio_direction_output(unsigned gpio, int value)
325{
ae7123f8 326 struct gpio_desc desc;
96495d90
SG
327 int ret;
328
ae7123f8
SG
329 ret = gpio_to_device(gpio, &desc);
330 if (ret)
331 return ret;
332 ret = check_reserved(&desc, "dir_output");
333 if (ret)
334 return ret;
335
336 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
337 desc.offset, value);
338}
339
17c43f1a 340int dm_gpio_get_value(const struct gpio_desc *desc)
ae7123f8
SG
341{
342 int value;
343 int ret;
344
345 ret = check_reserved(desc, "get_value");
96495d90
SG
346 if (ret)
347 return ret;
348
ae7123f8
SG
349 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
350
351 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
352}
353
17c43f1a 354int dm_gpio_set_value(const struct gpio_desc *desc, int value)
ae7123f8
SG
355{
356 int ret;
357
358 ret = check_reserved(desc, "set_value");
359 if (ret)
360 return ret;
361
362 if (desc->flags & GPIOD_ACTIVE_LOW)
363 value = !value;
364 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
365 return 0;
366}
367
53ecdfb9 368int dm_gpio_get_open_drain(struct gpio_desc *desc)
369{
370 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
371 int ret;
372
373 ret = check_reserved(desc, "get_open_drain");
374 if (ret)
375 return ret;
376
377 if (ops->set_open_drain)
378 return ops->get_open_drain(desc->dev, desc->offset);
379 else
380 return -ENOSYS;
381}
382
383int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
384{
385 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
386 int ret;
387
388 ret = check_reserved(desc, "set_open_drain");
389 if (ret)
390 return ret;
391
392 if (ops->set_open_drain)
393 ret = ops->set_open_drain(desc->dev, desc->offset, value);
394 else
395 return 0; /* feature not supported -> ignore setting */
396
397 return ret;
398}
399
ae7123f8
SG
400int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
401{
402 struct udevice *dev = desc->dev;
403 struct dm_gpio_ops *ops = gpio_get_ops(dev);
404 int ret;
405
406 ret = check_reserved(desc, "set_dir");
407 if (ret)
408 return ret;
409
410 if (flags & GPIOD_IS_OUT) {
411 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
412
413 if (flags & GPIOD_ACTIVE_LOW)
414 value = !value;
415 ret = ops->direction_output(dev, desc->offset, value);
416 } else if (flags & GPIOD_IS_IN) {
417 ret = ops->direction_input(dev, desc->offset);
418 }
419 if (ret)
420 return ret;
421 /*
422 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
423 * futures
424 */
425 desc->flags = flags;
426
427 return 0;
428}
429
430int dm_gpio_set_dir(struct gpio_desc *desc)
431{
432 return dm_gpio_set_dir_flags(desc, desc->flags);
96495d90
SG
433}
434
435/**
436 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
437 * gpio: GPIO number
438 *
439 * This function implements the API that's compatible with current
440 * GPIO API used in U-Boot. The request is forwarded to particular
441 * GPIO driver. Returns the value of the GPIO pin, or negative value
442 * on error.
443 */
444int gpio_get_value(unsigned gpio)
445{
96495d90
SG
446 int ret;
447
ae7123f8
SG
448 struct gpio_desc desc;
449
450 ret = gpio_to_device(gpio, &desc);
96495d90
SG
451 if (ret)
452 return ret;
ae7123f8 453 return dm_gpio_get_value(&desc);
96495d90
SG
454}
455
456/**
457 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
458 * gpio: GPIO number
459 * value: Logical value to be set on the GPIO pin.
460 *
461 * This function implements the API that's compatible with current
462 * GPIO API used in U-Boot. The request is forwarded to particular
463 * GPIO driver. Returns 0 on success, negative value on error.
464 */
465int gpio_set_value(unsigned gpio, int value)
466{
ae7123f8 467 struct gpio_desc desc;
96495d90
SG
468 int ret;
469
ae7123f8 470 ret = gpio_to_device(gpio, &desc);
96495d90
SG
471 if (ret)
472 return ret;
ae7123f8 473 return dm_gpio_set_value(&desc, value);
96495d90
SG
474}
475
54c5d08a 476const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
96495d90
SG
477{
478 struct gpio_dev_priv *priv;
479
480 /* Must be called on an active device */
e564f054 481 priv = dev_get_uclass_priv(dev);
96495d90
SG
482 assert(priv);
483
484 *bit_count = priv->gpio_count;
485 return priv->bank_name;
486}
487
6449a506
SG
488static const char * const gpio_function[GPIOF_COUNT] = {
489 "input",
490 "output",
491 "unused",
492 "unknown",
493 "func",
494};
495
fb07f97d
MY
496static int get_function(struct udevice *dev, int offset, bool skip_unused,
497 const char **namep)
6449a506 498{
e564f054 499 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
6449a506
SG
500 struct dm_gpio_ops *ops = gpio_get_ops(dev);
501
502 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
503 if (!device_active(dev))
504 return -ENODEV;
505 if (offset < 0 || offset >= uc_priv->gpio_count)
506 return -EINVAL;
507 if (namep)
508 *namep = uc_priv->name[offset];
509 if (skip_unused && !uc_priv->name[offset])
510 return GPIOF_UNUSED;
511 if (ops->get_function) {
512 int ret;
513
514 ret = ops->get_function(dev, offset);
515 if (ret < 0)
516 return ret;
517 if (ret >= ARRAY_SIZE(gpio_function))
518 return -ENODATA;
519 return ret;
520 }
521
522 return GPIOF_UNKNOWN;
523}
524
525int gpio_get_function(struct udevice *dev, int offset, const char **namep)
526{
527 return get_function(dev, offset, true, namep);
528}
529
530int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
531{
532 return get_function(dev, offset, false, namep);
533}
534
0757535a
SG
535int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
536{
537 struct dm_gpio_ops *ops = gpio_get_ops(dev);
538 struct gpio_dev_priv *priv;
539 char *str = buf;
540 int func;
541 int ret;
542 int len;
543
544 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
545
546 *buf = 0;
e564f054 547 priv = dev_get_uclass_priv(dev);
0757535a
SG
548 ret = gpio_get_raw_function(dev, offset, NULL);
549 if (ret < 0)
550 return ret;
551 func = ret;
552 len = snprintf(str, buffsize, "%s%d: %s",
553 priv->bank_name ? priv->bank_name : "",
554 offset, gpio_function[func]);
555 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
556 func == GPIOF_UNUSED) {
557 const char *label;
558 bool used;
559
560 ret = ops->get_value(dev, offset);
561 if (ret < 0)
562 return ret;
563 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
564 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
565 ret,
566 used ? 'x' : ' ',
567 used ? " " : "",
568 label ? label : "");
569 }
570
571 return 0;
572}
573
962f5caf
SG
574int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
575{
576 int i, ret;
577 int gpio;
578
579 for (i = 0; i < 32; i++) {
580 gpio = gpio_num_array[i];
581 if (gpio == -1)
582 break;
583 ret = gpio_requestf(gpio, fmt, i);
584 if (ret)
585 goto err;
586 ret = gpio_direction_input(gpio);
587 if (ret) {
588 gpio_free(gpio);
589 goto err;
590 }
591 }
592
593 return 0;
594err:
595 for (i--; i >= 0; i--)
596 gpio_free(gpio_num_array[i]);
597
598 return ret;
599}
600
e5901c94
SG
601/*
602 * get a number comprised of multiple GPIO values. gpio_num_array points to
603 * the array of gpio pin numbers to scan, terminated by -1.
604 */
962f5caf 605int gpio_get_values_as_int(const int *gpio_list)
e5901c94
SG
606{
607 int gpio;
608 unsigned bitmask = 1;
609 unsigned vector = 0;
962f5caf 610 int ret;
e5901c94
SG
611
612 while (bitmask &&
962f5caf
SG
613 ((gpio = *gpio_list++) != -1)) {
614 ret = gpio_get_value(gpio);
615 if (ret < 0)
616 return ret;
617 else if (ret)
e5901c94
SG
618 vector |= bitmask;
619 bitmask <<= 1;
620 }
962f5caf 621
e5901c94
SG
622 return vector;
623}
624
17c43f1a 625int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
bbf24780
SG
626{
627 unsigned bitmask = 1;
628 unsigned vector = 0;
629 int ret, i;
630
631 for (i = 0; i < count; i++) {
632 ret = dm_gpio_get_value(&desc_list[i]);
633 if (ret < 0)
634 return ret;
635 else if (ret)
636 vector |= bitmask;
637 bitmask <<= 1;
638 }
639
640 return vector;
641}
642
3a57123e
SG
643static int gpio_request_tail(int ret, ofnode node,
644 struct ofnode_phandle_args *args,
645 const char *list_name, int index,
646 struct gpio_desc *desc, int flags, bool add_index)
3669e0e7 647{
3669e0e7
SG
648 desc->dev = NULL;
649 desc->offset = 0;
6c880b77 650 desc->flags = 0;
3a57123e 651 if (ret)
3669e0e7 652 goto err;
3669e0e7 653
3a57123e
SG
654 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
655 &desc->dev);
3669e0e7 656 if (ret) {
b053dd7c 657 debug("%s: uclass_get_device_by_ofnode failed\n", __func__);
3669e0e7
SG
658 goto err;
659 }
3a57123e 660 ret = gpio_find_and_xlate(desc, args);
3669e0e7
SG
661 if (ret) {
662 debug("%s: gpio_find_and_xlate failed\n", __func__);
663 goto err;
664 }
665 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
3a57123e 666 ofnode_get_name(node),
3669e0e7
SG
667 list_name, index);
668 if (ret) {
669 debug("%s: dm_gpio_requestf failed\n", __func__);
670 goto err;
671 }
672 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
673 if (ret) {
674 debug("%s: dm_gpio_set_dir failed\n", __func__);
675 goto err;
676 }
677
678 return 0;
679err:
680 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
3a57123e 681 __func__, ofnode_get_name(node), list_name, index, ret);
3669e0e7
SG
682 return ret;
683}
684
150c5afe
SG
685static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
686 int index, struct gpio_desc *desc,
687 int flags, bool add_index)
3a57123e
SG
688{
689 struct ofnode_phandle_args args;
690 int ret;
691
150c5afe
SG
692 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
693 index, &args);
3a57123e 694
150c5afe
SG
695 return gpio_request_tail(ret, node, &args, list_name, index, desc,
696 flags, add_index);
3a57123e
SG
697}
698
150c5afe 699int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
3669e0e7
SG
700 struct gpio_desc *desc, int flags)
701{
150c5afe
SG
702 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
703 index > 0);
3669e0e7
SG
704}
705
150c5afe 706int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
3669e0e7
SG
707 struct gpio_desc *desc, int flags)
708{
150c5afe
SG
709 struct ofnode_phandle_args args;
710 int ret;
711
712 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
713 index, &args);
714
715 return gpio_request_tail(ret, dev_ofnode(dev), &args, list_name,
716 index, desc, flags, index > 0);
3669e0e7
SG
717}
718
150c5afe 719int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
3669e0e7
SG
720 struct gpio_desc *desc, int max_count,
721 int flags)
722{
723 int count;
724 int ret;
725
2984e7a1 726 for (count = 0; count < max_count; count++) {
150c5afe 727 ret = _gpio_request_by_name_nodev(node, list_name, count,
3669e0e7
SG
728 &desc[count], flags, true);
729 if (ret == -ENOENT)
730 break;
731 else if (ret)
732 goto err;
733 }
734
735 /* We ran out of GPIOs in the list */
736 return count;
737
738err:
739 gpio_free_list_nodev(desc, count - 1);
740
741 return ret;
742}
743
744int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
745 struct gpio_desc *desc, int max_count,
746 int flags)
747{
748 /*
749 * This isn't ideal since we don't use dev->name in the debug()
750 * calls in gpio_request_by_name(), but we can do this until
751 * gpio_request_list_by_name_nodev() can be dropped.
752 */
150c5afe
SG
753 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
754 max_count, flags);
3669e0e7
SG
755}
756
757int gpio_get_list_count(struct udevice *dev, const char *list_name)
758{
759 int ret;
760
e160f7d4 761 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
3669e0e7
SG
762 list_name, "#gpio-cells", 0, -1,
763 NULL);
764 if (ret) {
765 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
766 __func__, dev->name, list_name, ret);
767 }
768
769 return ret;
770}
771
772int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
773{
774 /* For now, we don't do any checking of dev */
775 return _dm_gpio_free(desc->dev, desc->offset);
776}
777
778int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
779{
780 int i;
781
782 /* For now, we don't do any checking of dev */
783 for (i = 0; i < count; i++)
784 dm_gpio_free(dev, &desc[i]);
785
786 return 0;
787}
788
789int gpio_free_list_nodev(struct gpio_desc *desc, int count)
790{
791 return gpio_free_list(NULL, desc, count);
792}
793
96495d90 794/* We need to renumber the GPIOs when any driver is probed/removed */
b892d127 795static int gpio_renumber(struct udevice *removed_dev)
96495d90
SG
796{
797 struct gpio_dev_priv *uc_priv;
54c5d08a 798 struct udevice *dev;
96495d90
SG
799 struct uclass *uc;
800 unsigned base;
801 int ret;
802
803 ret = uclass_get(UCLASS_GPIO, &uc);
804 if (ret)
805 return ret;
806
807 /* Ensure that we have a base for each bank */
808 base = 0;
809 uclass_foreach_dev(dev, uc) {
b892d127 810 if (device_active(dev) && dev != removed_dev) {
e564f054 811 uc_priv = dev_get_uclass_priv(dev);
96495d90
SG
812 uc_priv->gpio_base = base;
813 base += uc_priv->gpio_count;
814 }
815 }
816
817 return 0;
818}
819
17c43f1a 820int gpio_get_number(const struct gpio_desc *desc)
56a71f89
SG
821{
822 struct udevice *dev = desc->dev;
823 struct gpio_dev_priv *uc_priv;
824
825 if (!dev)
826 return -1;
827 uc_priv = dev->uclass_priv;
828
829 return uc_priv->gpio_base + desc->offset;
830}
831
54c5d08a 832static int gpio_post_probe(struct udevice *dev)
96495d90 833{
e564f054 834 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
b892d127
SG
835
836 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
837 if (!uc_priv->name)
838 return -ENOMEM;
839
840 return gpio_renumber(NULL);
96495d90
SG
841}
842
54c5d08a 843static int gpio_pre_remove(struct udevice *dev)
96495d90 844{
e564f054 845 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
b892d127
SG
846 int i;
847
848 for (i = 0; i < uc_priv->gpio_count; i++) {
849 if (uc_priv->name[i])
850 free(uc_priv->name[i]);
851 }
852 free(uc_priv->name);
853
854 return gpio_renumber(dev);
96495d90
SG
855}
856
857UCLASS_DRIVER(gpio) = {
858 .id = UCLASS_GPIO,
859 .name = "gpio",
ae89bb0d 860 .flags = DM_UC_FLAG_SEQ_ALIAS,
96495d90
SG
861 .post_probe = gpio_post_probe,
862 .pre_remove = gpio_pre_remove,
863 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
864};