2 * Copyright (c) 2013 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/bug.h>
14 #include <linux/ctype.h>
16 DECLARE_GLOBAL_DATA_PTR
;
19 * gpio_to_device() - Convert global GPIO number to device, number
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.
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
29 static int gpio_to_device(unsigned int gpio
, struct gpio_desc
*desc
)
31 struct gpio_dev_priv
*uc_priv
;
35 for (ret
= uclass_first_device(UCLASS_GPIO
, &dev
);
37 ret
= uclass_next_device(&dev
)) {
38 uc_priv
= dev_get_uclass_priv(dev
);
39 if (gpio
>= uc_priv
->gpio_base
&&
40 gpio
< uc_priv
->gpio_base
+ uc_priv
->gpio_count
) {
42 desc
->offset
= gpio
- uc_priv
->gpio_base
;
49 return ret
? ret
: -ENOENT
;
52 int dm_gpio_lookup_name(const char *name
, struct gpio_desc
*desc
)
54 struct gpio_dev_priv
*uc_priv
= NULL
;
60 numeric
= isdigit(*name
) ? simple_strtoul(name
, NULL
, 10) : -1;
61 for (ret
= uclass_first_device(UCLASS_GPIO
, &dev
);
63 ret
= uclass_next_device(&dev
)) {
66 uc_priv
= dev_get_uclass_priv(dev
);
68 offset
= numeric
- uc_priv
->gpio_base
;
69 /* Allow GPIOs to be numbered from 0 */
70 if (offset
>= 0 && offset
< uc_priv
->gpio_count
)
74 len
= uc_priv
->bank_name
? strlen(uc_priv
->bank_name
) : 0;
76 if (!strncasecmp(name
, uc_priv
->bank_name
, len
)) {
77 if (!strict_strtoul(name
+ len
, 10, &offset
))
83 return ret
? ret
: -EINVAL
;
86 desc
->offset
= offset
;
91 int gpio_lookup_name(const char *name
, struct udevice
**devp
,
92 unsigned int *offsetp
, unsigned int *gpiop
)
94 struct gpio_desc desc
;
99 ret
= dm_gpio_lookup_name(name
, &desc
);
106 *offsetp
= desc
.offset
;
108 struct gpio_dev_priv
*uc_priv
= dev_get_uclass_priv(desc
.dev
);
110 *gpiop
= uc_priv
->gpio_base
+ desc
.offset
;
116 static int gpio_find_and_xlate(struct gpio_desc
*desc
,
117 struct fdtdec_phandle_args
*args
)
119 struct dm_gpio_ops
*ops
= gpio_get_ops(desc
->dev
);
121 /* Use the first argument as the offset by default */
122 if (args
->args_count
> 0)
123 desc
->offset
= args
->args
[0];
128 return ops
->xlate
? ops
->xlate(desc
->dev
, desc
, args
) : 0;
131 int dm_gpio_request(struct gpio_desc
*desc
, const char *label
)
133 struct udevice
*dev
= desc
->dev
;
134 struct gpio_dev_priv
*uc_priv
;
138 uc_priv
= dev_get_uclass_priv(dev
);
139 if (uc_priv
->name
[desc
->offset
])
144 if (gpio_get_ops(dev
)->request
) {
145 ret
= gpio_get_ops(dev
)->request(dev
, desc
->offset
, label
);
151 uc_priv
->name
[desc
->offset
] = str
;
156 static int dm_gpio_requestf(struct gpio_desc
*desc
, const char *fmt
, ...)
158 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
163 vscnprintf(buf
, sizeof(buf
), fmt
, args
);
165 return dm_gpio_request(desc
, buf
);
167 return dm_gpio_request(desc
, fmt
);
172 * gpio_request() - [COMPAT] Request GPIO
174 * label: Name for the requested GPIO
176 * The label is copied and allocated so the caller does not need to keep
177 * the pointer around.
179 * This function implements the API that's compatible with current
180 * GPIO API used in U-Boot. The request is forwarded to particular
181 * GPIO driver. Returns 0 on success, negative value on error.
183 int gpio_request(unsigned gpio
, const char *label
)
185 struct gpio_desc desc
;
188 ret
= gpio_to_device(gpio
, &desc
);
192 return dm_gpio_request(&desc
, label
);
196 * gpio_requestf() - [COMPAT] Request GPIO
198 * @fmt: Format string for the requested GPIO
199 * @...: Arguments for the printf() format string
201 * This function implements the API that's compatible with current
202 * GPIO API used in U-Boot. The request is forwarded to particular
203 * GPIO driver. Returns 0 on success, negative value on error.
205 int gpio_requestf(unsigned gpio
, const char *fmt
, ...)
207 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
212 vscnprintf(buf
, sizeof(buf
), fmt
, args
);
214 return gpio_request(gpio
, buf
);
216 return gpio_request(gpio
, fmt
);
220 int _dm_gpio_free(struct udevice
*dev
, uint offset
)
222 struct gpio_dev_priv
*uc_priv
;
225 uc_priv
= dev_get_uclass_priv(dev
);
226 if (!uc_priv
->name
[offset
])
228 if (gpio_get_ops(dev
)->free
) {
229 ret
= gpio_get_ops(dev
)->free(dev
, offset
);
234 free(uc_priv
->name
[offset
]);
235 uc_priv
->name
[offset
] = NULL
;
241 * gpio_free() - [COMPAT] Relinquish GPIO
244 * This function implements the API that's compatible with current
245 * GPIO API used in U-Boot. The request is forwarded to particular
246 * GPIO driver. Returns 0 on success, negative value on error.
248 int gpio_free(unsigned gpio
)
250 struct gpio_desc desc
;
253 ret
= gpio_to_device(gpio
, &desc
);
257 return _dm_gpio_free(desc
.dev
, desc
.offset
);
260 static int check_reserved(const struct gpio_desc
*desc
, const char *func
)
262 struct gpio_dev_priv
*uc_priv
;
264 if (!dm_gpio_is_valid(desc
))
267 uc_priv
= dev_get_uclass_priv(desc
->dev
);
268 if (!uc_priv
->name
[desc
->offset
]) {
269 printf("%s: %s: error: gpio %s%d not reserved\n",
270 desc
->dev
->name
, func
,
271 uc_priv
->bank_name
? uc_priv
->bank_name
: "",
280 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
283 * This function implements the API that's compatible with current
284 * GPIO API used in U-Boot. The request is forwarded to particular
285 * GPIO driver. Returns 0 on success, negative value on error.
287 int gpio_direction_input(unsigned gpio
)
289 struct gpio_desc desc
;
292 ret
= gpio_to_device(gpio
, &desc
);
295 ret
= check_reserved(&desc
, "dir_input");
299 return gpio_get_ops(desc
.dev
)->direction_input(desc
.dev
, desc
.offset
);
303 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
305 * value: Logical value to be set on the GPIO pin
307 * This function implements the API that's compatible with current
308 * GPIO API used in U-Boot. The request is forwarded to particular
309 * GPIO driver. Returns 0 on success, negative value on error.
311 int gpio_direction_output(unsigned gpio
, int value
)
313 struct gpio_desc desc
;
316 ret
= gpio_to_device(gpio
, &desc
);
319 ret
= check_reserved(&desc
, "dir_output");
323 return gpio_get_ops(desc
.dev
)->direction_output(desc
.dev
,
327 int dm_gpio_get_value(const struct gpio_desc
*desc
)
332 ret
= check_reserved(desc
, "get_value");
336 value
= gpio_get_ops(desc
->dev
)->get_value(desc
->dev
, desc
->offset
);
338 return desc
->flags
& GPIOD_ACTIVE_LOW
? !value
: value
;
341 int dm_gpio_set_value(const struct gpio_desc
*desc
, int value
)
345 ret
= check_reserved(desc
, "set_value");
349 if (desc
->flags
& GPIOD_ACTIVE_LOW
)
351 gpio_get_ops(desc
->dev
)->set_value(desc
->dev
, desc
->offset
, value
);
355 int dm_gpio_set_dir_flags(struct gpio_desc
*desc
, ulong flags
)
357 struct udevice
*dev
= desc
->dev
;
358 struct dm_gpio_ops
*ops
= gpio_get_ops(dev
);
361 ret
= check_reserved(desc
, "set_dir");
365 if (flags
& GPIOD_IS_OUT
) {
366 int value
= flags
& GPIOD_IS_OUT_ACTIVE
? 1 : 0;
368 if (flags
& GPIOD_ACTIVE_LOW
)
370 ret
= ops
->direction_output(dev
, desc
->offset
, value
);
371 } else if (flags
& GPIOD_IS_IN
) {
372 ret
= ops
->direction_input(dev
, desc
->offset
);
377 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
385 int dm_gpio_set_dir(struct gpio_desc
*desc
)
387 return dm_gpio_set_dir_flags(desc
, desc
->flags
);
391 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
394 * This function implements the API that's compatible with current
395 * GPIO API used in U-Boot. The request is forwarded to particular
396 * GPIO driver. Returns the value of the GPIO pin, or negative value
399 int gpio_get_value(unsigned gpio
)
403 struct gpio_desc desc
;
405 ret
= gpio_to_device(gpio
, &desc
);
408 return dm_gpio_get_value(&desc
);
412 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
414 * value: Logical value to be set on the GPIO pin.
416 * This function implements the API that's compatible with current
417 * GPIO API used in U-Boot. The request is forwarded to particular
418 * GPIO driver. Returns 0 on success, negative value on error.
420 int gpio_set_value(unsigned gpio
, int value
)
422 struct gpio_desc desc
;
425 ret
= gpio_to_device(gpio
, &desc
);
428 return dm_gpio_set_value(&desc
, value
);
431 const char *gpio_get_bank_info(struct udevice
*dev
, int *bit_count
)
433 struct gpio_dev_priv
*priv
;
435 /* Must be called on an active device */
436 priv
= dev_get_uclass_priv(dev
);
439 *bit_count
= priv
->gpio_count
;
440 return priv
->bank_name
;
443 static const char * const gpio_function
[GPIOF_COUNT
] = {
451 int get_function(struct udevice
*dev
, int offset
, bool skip_unused
,
454 struct gpio_dev_priv
*uc_priv
= dev_get_uclass_priv(dev
);
455 struct dm_gpio_ops
*ops
= gpio_get_ops(dev
);
457 BUILD_BUG_ON(GPIOF_COUNT
!= ARRAY_SIZE(gpio_function
));
458 if (!device_active(dev
))
460 if (offset
< 0 || offset
>= uc_priv
->gpio_count
)
463 *namep
= uc_priv
->name
[offset
];
464 if (skip_unused
&& !uc_priv
->name
[offset
])
466 if (ops
->get_function
) {
469 ret
= ops
->get_function(dev
, offset
);
472 if (ret
>= ARRAY_SIZE(gpio_function
))
477 return GPIOF_UNKNOWN
;
480 int gpio_get_function(struct udevice
*dev
, int offset
, const char **namep
)
482 return get_function(dev
, offset
, true, namep
);
485 int gpio_get_raw_function(struct udevice
*dev
, int offset
, const char **namep
)
487 return get_function(dev
, offset
, false, namep
);
490 int gpio_get_status(struct udevice
*dev
, int offset
, char *buf
, int buffsize
)
492 struct dm_gpio_ops
*ops
= gpio_get_ops(dev
);
493 struct gpio_dev_priv
*priv
;
499 BUILD_BUG_ON(GPIOF_COUNT
!= ARRAY_SIZE(gpio_function
));
502 priv
= dev_get_uclass_priv(dev
);
503 ret
= gpio_get_raw_function(dev
, offset
, NULL
);
507 len
= snprintf(str
, buffsize
, "%s%d: %s",
508 priv
->bank_name
? priv
->bank_name
: "",
509 offset
, gpio_function
[func
]);
510 if (func
== GPIOF_INPUT
|| func
== GPIOF_OUTPUT
||
511 func
== GPIOF_UNUSED
) {
515 ret
= ops
->get_value(dev
, offset
);
518 used
= gpio_get_function(dev
, offset
, &label
) != GPIOF_UNUSED
;
519 snprintf(str
+ len
, buffsize
- len
, ": %d [%c]%s%s",
529 int gpio_claim_vector(const int *gpio_num_array
, const char *fmt
)
534 for (i
= 0; i
< 32; i
++) {
535 gpio
= gpio_num_array
[i
];
538 ret
= gpio_requestf(gpio
, fmt
, i
);
541 ret
= gpio_direction_input(gpio
);
550 for (i
--; i
>= 0; i
--)
551 gpio_free(gpio_num_array
[i
]);
557 * get a number comprised of multiple GPIO values. gpio_num_array points to
558 * the array of gpio pin numbers to scan, terminated by -1.
560 int gpio_get_values_as_int(const int *gpio_list
)
563 unsigned bitmask
= 1;
568 ((gpio
= *gpio_list
++) != -1)) {
569 ret
= gpio_get_value(gpio
);
580 int dm_gpio_get_values_as_int(const struct gpio_desc
*desc_list
, int count
)
582 unsigned bitmask
= 1;
586 for (i
= 0; i
< count
; i
++) {
587 ret
= dm_gpio_get_value(&desc_list
[i
]);
598 static int _gpio_request_by_name_nodev(const void *blob
, int node
,
599 const char *list_name
, int index
,
600 struct gpio_desc
*desc
, int flags
,
603 struct fdtdec_phandle_args args
;
608 ret
= fdtdec_parse_phandle_with_args(blob
, node
, list_name
,
609 "#gpio-cells", 0, index
, &args
);
611 debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__
);
615 ret
= uclass_get_device_by_of_offset(UCLASS_GPIO
, args
.node
,
618 debug("%s: uclass_get_device_by_of_offset failed\n", __func__
);
621 ret
= gpio_find_and_xlate(desc
, &args
);
623 debug("%s: gpio_find_and_xlate failed\n", __func__
);
626 ret
= dm_gpio_requestf(desc
, add_index
? "%s.%s%d" : "%s.%s",
627 fdt_get_name(blob
, node
, NULL
),
630 debug("%s: dm_gpio_requestf failed\n", __func__
);
633 ret
= dm_gpio_set_dir_flags(desc
, flags
| desc
->flags
);
635 debug("%s: dm_gpio_set_dir failed\n", __func__
);
641 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
642 __func__
, fdt_get_name(blob
, node
, NULL
), list_name
, index
, ret
);
646 int gpio_request_by_name_nodev(const void *blob
, int node
,
647 const char *list_name
, int index
,
648 struct gpio_desc
*desc
, int flags
)
650 return _gpio_request_by_name_nodev(blob
, node
, list_name
, index
, desc
,
654 int gpio_request_by_name(struct udevice
*dev
, const char *list_name
, int index
,
655 struct gpio_desc
*desc
, int flags
)
658 * This isn't ideal since we don't use dev->name in the debug()
659 * calls in gpio_request_by_name(), but we can do this until
660 * gpio_request_by_name_nodev() can be dropped.
662 return gpio_request_by_name_nodev(gd
->fdt_blob
, dev
->of_offset
,
663 list_name
, index
, desc
, flags
);
666 int gpio_request_list_by_name_nodev(const void *blob
, int node
,
667 const char *list_name
,
668 struct gpio_desc
*desc
, int max_count
,
674 for (count
= 0; count
< max_count
; count
++) {
675 ret
= _gpio_request_by_name_nodev(blob
, node
, list_name
, count
,
676 &desc
[count
], flags
, true);
683 /* We ran out of GPIOs in the list */
687 gpio_free_list_nodev(desc
, count
- 1);
692 int gpio_request_list_by_name(struct udevice
*dev
, const char *list_name
,
693 struct gpio_desc
*desc
, int max_count
,
697 * This isn't ideal since we don't use dev->name in the debug()
698 * calls in gpio_request_by_name(), but we can do this until
699 * gpio_request_list_by_name_nodev() can be dropped.
701 return gpio_request_list_by_name_nodev(gd
->fdt_blob
, dev
->of_offset
,
702 list_name
, desc
, max_count
,
706 int gpio_get_list_count(struct udevice
*dev
, const char *list_name
)
710 ret
= fdtdec_parse_phandle_with_args(gd
->fdt_blob
, dev
->of_offset
,
711 list_name
, "#gpio-cells", 0, -1,
714 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
715 __func__
, dev
->name
, list_name
, ret
);
721 int dm_gpio_free(struct udevice
*dev
, struct gpio_desc
*desc
)
723 /* For now, we don't do any checking of dev */
724 return _dm_gpio_free(desc
->dev
, desc
->offset
);
727 int gpio_free_list(struct udevice
*dev
, struct gpio_desc
*desc
, int count
)
731 /* For now, we don't do any checking of dev */
732 for (i
= 0; i
< count
; i
++)
733 dm_gpio_free(dev
, &desc
[i
]);
738 int gpio_free_list_nodev(struct gpio_desc
*desc
, int count
)
740 return gpio_free_list(NULL
, desc
, count
);
743 /* We need to renumber the GPIOs when any driver is probed/removed */
744 static int gpio_renumber(struct udevice
*removed_dev
)
746 struct gpio_dev_priv
*uc_priv
;
752 ret
= uclass_get(UCLASS_GPIO
, &uc
);
756 /* Ensure that we have a base for each bank */
758 uclass_foreach_dev(dev
, uc
) {
759 if (device_active(dev
) && dev
!= removed_dev
) {
760 uc_priv
= dev_get_uclass_priv(dev
);
761 uc_priv
->gpio_base
= base
;
762 base
+= uc_priv
->gpio_count
;
769 int gpio_get_number(const struct gpio_desc
*desc
)
771 struct udevice
*dev
= desc
->dev
;
772 struct gpio_dev_priv
*uc_priv
;
776 uc_priv
= dev
->uclass_priv
;
778 return uc_priv
->gpio_base
+ desc
->offset
;
781 static int gpio_post_probe(struct udevice
*dev
)
783 struct gpio_dev_priv
*uc_priv
= dev_get_uclass_priv(dev
);
785 uc_priv
->name
= calloc(uc_priv
->gpio_count
, sizeof(char *));
789 return gpio_renumber(NULL
);
792 static int gpio_pre_remove(struct udevice
*dev
)
794 struct gpio_dev_priv
*uc_priv
= dev_get_uclass_priv(dev
);
797 for (i
= 0; i
< uc_priv
->gpio_count
; i
++) {
798 if (uc_priv
->name
[i
])
799 free(uc_priv
->name
[i
]);
803 return gpio_renumber(dev
);
806 UCLASS_DRIVER(gpio
) = {
809 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
810 .post_probe
= gpio_post_probe
,
811 .pre_remove
= gpio_pre_remove
,
812 .per_device_auto_alloc_size
= sizeof(struct gpio_dev_priv
),