2 * Copyright (c) 2013 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/ctype.h>
15 DECLARE_GLOBAL_DATA_PTR
;
18 * gpio_to_device() - Convert global GPIO number to device, number
20 * Convert the GPIO number to an entry in the list of GPIOs
21 * or GPIO blocks registered with the GPIO controller. Returns
22 * entry on success, NULL on error.
24 * @gpio: The numeric representation of the GPIO
25 * @desc: Returns description (desc->flags will always be 0)
26 * @return 0 if found, -ENOENT if not found
28 static int gpio_to_device(unsigned int gpio
, struct gpio_desc
*desc
)
30 struct gpio_dev_priv
*uc_priv
;
34 for (ret
= uclass_first_device(UCLASS_GPIO
, &dev
);
36 ret
= uclass_next_device(&dev
)) {
37 uc_priv
= dev_get_uclass_priv(dev
);
38 if (gpio
>= uc_priv
->gpio_base
&&
39 gpio
< uc_priv
->gpio_base
+ uc_priv
->gpio_count
) {
41 desc
->offset
= gpio
- uc_priv
->gpio_base
;
48 return ret
? ret
: -ENOENT
;
51 int dm_gpio_lookup_name(const char *name
, struct gpio_desc
*desc
)
53 struct gpio_dev_priv
*uc_priv
= NULL
;
59 numeric
= isdigit(*name
) ? simple_strtoul(name
, NULL
, 10) : -1;
60 for (ret
= uclass_first_device(UCLASS_GPIO
, &dev
);
62 ret
= uclass_next_device(&dev
)) {
65 uc_priv
= dev_get_uclass_priv(dev
);
67 offset
= numeric
- uc_priv
->gpio_base
;
68 /* Allow GPIOs to be numbered from 0 */
69 if (offset
>= 0 && offset
< uc_priv
->gpio_count
)
73 len
= uc_priv
->bank_name
? strlen(uc_priv
->bank_name
) : 0;
75 if (!strncasecmp(name
, uc_priv
->bank_name
, len
)) {
76 if (!strict_strtoul(name
+ len
, 10, &offset
))
82 return ret
? ret
: -EINVAL
;
85 desc
->offset
= offset
;
90 int gpio_lookup_name(const char *name
, struct udevice
**devp
,
91 unsigned int *offsetp
, unsigned int *gpiop
)
93 struct gpio_desc desc
;
98 ret
= dm_gpio_lookup_name(name
, &desc
);
105 *offsetp
= desc
.offset
;
107 struct gpio_dev_priv
*uc_priv
= dev_get_uclass_priv(desc
.dev
);
109 *gpiop
= uc_priv
->gpio_base
+ desc
.offset
;
115 static int gpio_find_and_xlate(struct gpio_desc
*desc
,
116 struct fdtdec_phandle_args
*args
)
118 struct dm_gpio_ops
*ops
= gpio_get_ops(desc
->dev
);
120 /* Use the first argument as the offset by default */
121 if (args
->args_count
> 0)
122 desc
->offset
= args
->args
[0];
127 return ops
->xlate
? ops
->xlate(desc
->dev
, desc
, args
) : 0;
130 int dm_gpio_request(struct gpio_desc
*desc
, const char *label
)
132 struct udevice
*dev
= desc
->dev
;
133 struct gpio_dev_priv
*uc_priv
;
137 uc_priv
= dev_get_uclass_priv(dev
);
138 if (uc_priv
->name
[desc
->offset
])
143 if (gpio_get_ops(dev
)->request
) {
144 ret
= gpio_get_ops(dev
)->request(dev
, desc
->offset
, label
);
150 uc_priv
->name
[desc
->offset
] = str
;
155 static int dm_gpio_requestf(struct gpio_desc
*desc
, const char *fmt
, ...)
161 vscnprintf(buf
, sizeof(buf
), fmt
, args
);
163 return dm_gpio_request(desc
, buf
);
167 * gpio_request() - [COMPAT] Request GPIO
169 * label: Name for the requested GPIO
171 * The label is copied and allocated so the caller does not need to keep
172 * the pointer around.
174 * This function implements the API that's compatible with current
175 * GPIO API used in U-Boot. The request is forwarded to particular
176 * GPIO driver. Returns 0 on success, negative value on error.
178 int gpio_request(unsigned gpio
, const char *label
)
180 struct gpio_desc desc
;
183 ret
= gpio_to_device(gpio
, &desc
);
187 return dm_gpio_request(&desc
, label
);
191 * gpio_requestf() - [COMPAT] Request GPIO
193 * @fmt: Format string for the requested GPIO
194 * @...: Arguments for the printf() format string
196 * This function implements the API that's compatible with current
197 * GPIO API used in U-Boot. The request is forwarded to particular
198 * GPIO driver. Returns 0 on success, negative value on error.
200 int gpio_requestf(unsigned gpio
, const char *fmt
, ...)
206 vscnprintf(buf
, sizeof(buf
), fmt
, args
);
208 return gpio_request(gpio
, buf
);
211 int _dm_gpio_free(struct udevice
*dev
, uint offset
)
213 struct gpio_dev_priv
*uc_priv
;
216 uc_priv
= dev_get_uclass_priv(dev
);
217 if (!uc_priv
->name
[offset
])
219 if (gpio_get_ops(dev
)->free
) {
220 ret
= gpio_get_ops(dev
)->free(dev
, offset
);
225 free(uc_priv
->name
[offset
]);
226 uc_priv
->name
[offset
] = NULL
;
232 * gpio_free() - [COMPAT] Relinquish GPIO
235 * This function implements the API that's compatible with current
236 * GPIO API used in U-Boot. The request is forwarded to particular
237 * GPIO driver. Returns 0 on success, negative value on error.
239 int gpio_free(unsigned gpio
)
241 struct gpio_desc desc
;
244 ret
= gpio_to_device(gpio
, &desc
);
248 return _dm_gpio_free(desc
.dev
, desc
.offset
);
251 static int check_reserved(struct gpio_desc
*desc
, const char *func
)
253 struct gpio_dev_priv
*uc_priv
= dev_get_uclass_priv(desc
->dev
);
255 if (!uc_priv
->name
[desc
->offset
]) {
256 printf("%s: %s: error: gpio %s%d not reserved\n",
257 desc
->dev
->name
, func
,
258 uc_priv
->bank_name
? uc_priv
->bank_name
: "",
267 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
270 * This function implements the API that's compatible with current
271 * GPIO API used in U-Boot. The request is forwarded to particular
272 * GPIO driver. Returns 0 on success, negative value on error.
274 int gpio_direction_input(unsigned gpio
)
276 struct gpio_desc desc
;
279 ret
= gpio_to_device(gpio
, &desc
);
282 ret
= check_reserved(&desc
, "dir_input");
286 return gpio_get_ops(desc
.dev
)->direction_input(desc
.dev
, desc
.offset
);
290 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
292 * value: Logical value to be set on the GPIO pin
294 * This function implements the API that's compatible with current
295 * GPIO API used in U-Boot. The request is forwarded to particular
296 * GPIO driver. Returns 0 on success, negative value on error.
298 int gpio_direction_output(unsigned gpio
, int value
)
300 struct gpio_desc desc
;
303 ret
= gpio_to_device(gpio
, &desc
);
306 ret
= check_reserved(&desc
, "dir_output");
310 return gpio_get_ops(desc
.dev
)->direction_output(desc
.dev
,
314 int dm_gpio_get_value(struct gpio_desc
*desc
)
319 ret
= check_reserved(desc
, "get_value");
323 value
= gpio_get_ops(desc
->dev
)->get_value(desc
->dev
, desc
->offset
);
325 return desc
->flags
& GPIOD_ACTIVE_LOW
? !value
: value
;
328 int dm_gpio_set_value(struct gpio_desc
*desc
, int value
)
332 ret
= check_reserved(desc
, "set_value");
336 if (desc
->flags
& GPIOD_ACTIVE_LOW
)
338 gpio_get_ops(desc
->dev
)->set_value(desc
->dev
, desc
->offset
, value
);
342 int dm_gpio_set_dir_flags(struct gpio_desc
*desc
, ulong flags
)
344 struct udevice
*dev
= desc
->dev
;
345 struct dm_gpio_ops
*ops
= gpio_get_ops(dev
);
348 ret
= check_reserved(desc
, "set_dir");
352 if (flags
& GPIOD_IS_OUT
) {
353 int value
= flags
& GPIOD_IS_OUT_ACTIVE
? 1 : 0;
355 if (flags
& GPIOD_ACTIVE_LOW
)
357 ret
= ops
->direction_output(dev
, desc
->offset
, value
);
358 } else if (flags
& GPIOD_IS_IN
) {
359 ret
= ops
->direction_input(dev
, desc
->offset
);
364 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
372 int dm_gpio_set_dir(struct gpio_desc
*desc
)
374 return dm_gpio_set_dir_flags(desc
, desc
->flags
);
378 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
381 * This function implements the API that's compatible with current
382 * GPIO API used in U-Boot. The request is forwarded to particular
383 * GPIO driver. Returns the value of the GPIO pin, or negative value
386 int gpio_get_value(unsigned gpio
)
390 struct gpio_desc desc
;
392 ret
= gpio_to_device(gpio
, &desc
);
395 return dm_gpio_get_value(&desc
);
399 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
401 * value: Logical value to be set on the GPIO pin.
403 * This function implements the API that's compatible with current
404 * GPIO API used in U-Boot. The request is forwarded to particular
405 * GPIO driver. Returns 0 on success, negative value on error.
407 int gpio_set_value(unsigned gpio
, int value
)
409 struct gpio_desc desc
;
412 ret
= gpio_to_device(gpio
, &desc
);
415 return dm_gpio_set_value(&desc
, value
);
418 const char *gpio_get_bank_info(struct udevice
*dev
, int *bit_count
)
420 struct gpio_dev_priv
*priv
;
422 /* Must be called on an active device */
423 priv
= dev_get_uclass_priv(dev
);
426 *bit_count
= priv
->gpio_count
;
427 return priv
->bank_name
;
430 static const char * const gpio_function
[GPIOF_COUNT
] = {
438 int get_function(struct udevice
*dev
, int offset
, bool skip_unused
,
441 struct gpio_dev_priv
*uc_priv
= dev_get_uclass_priv(dev
);
442 struct dm_gpio_ops
*ops
= gpio_get_ops(dev
);
444 BUILD_BUG_ON(GPIOF_COUNT
!= ARRAY_SIZE(gpio_function
));
445 if (!device_active(dev
))
447 if (offset
< 0 || offset
>= uc_priv
->gpio_count
)
450 *namep
= uc_priv
->name
[offset
];
451 if (skip_unused
&& !uc_priv
->name
[offset
])
453 if (ops
->get_function
) {
456 ret
= ops
->get_function(dev
, offset
);
459 if (ret
>= ARRAY_SIZE(gpio_function
))
464 return GPIOF_UNKNOWN
;
467 int gpio_get_function(struct udevice
*dev
, int offset
, const char **namep
)
469 return get_function(dev
, offset
, true, namep
);
472 int gpio_get_raw_function(struct udevice
*dev
, int offset
, const char **namep
)
474 return get_function(dev
, offset
, false, namep
);
477 int gpio_get_status(struct udevice
*dev
, int offset
, char *buf
, int buffsize
)
479 struct dm_gpio_ops
*ops
= gpio_get_ops(dev
);
480 struct gpio_dev_priv
*priv
;
486 BUILD_BUG_ON(GPIOF_COUNT
!= ARRAY_SIZE(gpio_function
));
489 priv
= dev_get_uclass_priv(dev
);
490 ret
= gpio_get_raw_function(dev
, offset
, NULL
);
494 len
= snprintf(str
, buffsize
, "%s%d: %s",
495 priv
->bank_name
? priv
->bank_name
: "",
496 offset
, gpio_function
[func
]);
497 if (func
== GPIOF_INPUT
|| func
== GPIOF_OUTPUT
||
498 func
== GPIOF_UNUSED
) {
502 ret
= ops
->get_value(dev
, offset
);
505 used
= gpio_get_function(dev
, offset
, &label
) != GPIOF_UNUSED
;
506 snprintf(str
+ len
, buffsize
- len
, ": %d [%c]%s%s",
516 int gpio_claim_vector(const int *gpio_num_array
, const char *fmt
)
521 for (i
= 0; i
< 32; i
++) {
522 gpio
= gpio_num_array
[i
];
525 ret
= gpio_requestf(gpio
, fmt
, i
);
528 ret
= gpio_direction_input(gpio
);
537 for (i
--; i
>= 0; i
--)
538 gpio_free(gpio_num_array
[i
]);
544 * get a number comprised of multiple GPIO values. gpio_num_array points to
545 * the array of gpio pin numbers to scan, terminated by -1.
547 int gpio_get_values_as_int(const int *gpio_list
)
550 unsigned bitmask
= 1;
555 ((gpio
= *gpio_list
++) != -1)) {
556 ret
= gpio_get_value(gpio
);
567 static int _gpio_request_by_name_nodev(const void *blob
, int node
,
568 const char *list_name
, int index
,
569 struct gpio_desc
*desc
, int flags
,
572 struct fdtdec_phandle_args args
;
577 ret
= fdtdec_parse_phandle_with_args(blob
, node
, list_name
,
578 "#gpio-cells", 0, index
, &args
);
580 debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__
);
584 ret
= uclass_get_device_by_of_offset(UCLASS_GPIO
, args
.node
,
587 debug("%s: uclass_get_device_by_of_offset failed\n", __func__
);
590 ret
= gpio_find_and_xlate(desc
, &args
);
592 debug("%s: gpio_find_and_xlate failed\n", __func__
);
595 ret
= dm_gpio_requestf(desc
, add_index
? "%s.%s%d" : "%s.%s",
596 fdt_get_name(blob
, node
, NULL
),
599 debug("%s: dm_gpio_requestf failed\n", __func__
);
602 ret
= dm_gpio_set_dir_flags(desc
, flags
| desc
->flags
);
604 debug("%s: dm_gpio_set_dir failed\n", __func__
);
610 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
611 __func__
, fdt_get_name(blob
, node
, NULL
), list_name
, index
, ret
);
615 int gpio_request_by_name_nodev(const void *blob
, int node
,
616 const char *list_name
, int index
,
617 struct gpio_desc
*desc
, int flags
)
619 return _gpio_request_by_name_nodev(blob
, node
, list_name
, index
, desc
,
623 int gpio_request_by_name(struct udevice
*dev
, const char *list_name
, int index
,
624 struct gpio_desc
*desc
, int flags
)
627 * This isn't ideal since we don't use dev->name in the debug()
628 * calls in gpio_request_by_name(), but we can do this until
629 * gpio_request_by_name_nodev() can be dropped.
631 return gpio_request_by_name_nodev(gd
->fdt_blob
, dev
->of_offset
,
632 list_name
, index
, desc
, flags
);
635 int gpio_request_list_by_name_nodev(const void *blob
, int node
,
636 const char *list_name
,
637 struct gpio_desc
*desc
, int max_count
,
643 for (count
= 0; count
< max_count
; count
++) {
644 ret
= _gpio_request_by_name_nodev(blob
, node
, list_name
, count
,
645 &desc
[count
], flags
, true);
652 /* We ran out of GPIOs in the list */
656 gpio_free_list_nodev(desc
, count
- 1);
661 int gpio_request_list_by_name(struct udevice
*dev
, const char *list_name
,
662 struct gpio_desc
*desc
, int max_count
,
666 * This isn't ideal since we don't use dev->name in the debug()
667 * calls in gpio_request_by_name(), but we can do this until
668 * gpio_request_list_by_name_nodev() can be dropped.
670 return gpio_request_list_by_name_nodev(gd
->fdt_blob
, dev
->of_offset
,
671 list_name
, desc
, max_count
,
675 int gpio_get_list_count(struct udevice
*dev
, const char *list_name
)
679 ret
= fdtdec_parse_phandle_with_args(gd
->fdt_blob
, dev
->of_offset
,
680 list_name
, "#gpio-cells", 0, -1,
683 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
684 __func__
, dev
->name
, list_name
, ret
);
690 int dm_gpio_free(struct udevice
*dev
, struct gpio_desc
*desc
)
692 /* For now, we don't do any checking of dev */
693 return _dm_gpio_free(desc
->dev
, desc
->offset
);
696 int gpio_free_list(struct udevice
*dev
, struct gpio_desc
*desc
, int count
)
700 /* For now, we don't do any checking of dev */
701 for (i
= 0; i
< count
; i
++)
702 dm_gpio_free(dev
, &desc
[i
]);
707 int gpio_free_list_nodev(struct gpio_desc
*desc
, int count
)
709 return gpio_free_list(NULL
, desc
, count
);
712 /* We need to renumber the GPIOs when any driver is probed/removed */
713 static int gpio_renumber(struct udevice
*removed_dev
)
715 struct gpio_dev_priv
*uc_priv
;
721 ret
= uclass_get(UCLASS_GPIO
, &uc
);
725 /* Ensure that we have a base for each bank */
727 uclass_foreach_dev(dev
, uc
) {
728 if (device_active(dev
) && dev
!= removed_dev
) {
729 uc_priv
= dev_get_uclass_priv(dev
);
730 uc_priv
->gpio_base
= base
;
731 base
+= uc_priv
->gpio_count
;
738 int gpio_get_number(struct gpio_desc
*desc
)
740 struct udevice
*dev
= desc
->dev
;
741 struct gpio_dev_priv
*uc_priv
;
745 uc_priv
= dev
->uclass_priv
;
747 return uc_priv
->gpio_base
+ desc
->offset
;
750 static int gpio_post_probe(struct udevice
*dev
)
752 struct gpio_dev_priv
*uc_priv
= dev_get_uclass_priv(dev
);
754 uc_priv
->name
= calloc(uc_priv
->gpio_count
, sizeof(char *));
758 return gpio_renumber(NULL
);
761 static int gpio_pre_remove(struct udevice
*dev
)
763 struct gpio_dev_priv
*uc_priv
= dev_get_uclass_priv(dev
);
766 for (i
= 0; i
< uc_priv
->gpio_count
; i
++) {
767 if (uc_priv
->name
[i
])
768 free(uc_priv
->name
[i
]);
772 return gpio_renumber(dev
);
775 UCLASS_DRIVER(gpio
) = {
778 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
779 .post_probe
= gpio_post_probe
,
780 .pre_remove
= gpio_pre_remove
,
781 .per_device_auto_alloc_size
= sizeof(struct gpio_dev_priv
),