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->uclass_priv;
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 gpio_lookup_name(const char *name, struct udevice **devp,
52 unsigned int *offsetp, unsigned int *gpiop)
54 struct gpio_dev_priv *uc_priv = NULL;
62 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
63 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
65 ret = uclass_next_device(&dev)) {
68 uc_priv = dev->uclass_priv;
70 offset = numeric - uc_priv->gpio_base;
71 /* Allow GPIOs to be numbered from 0 */
72 if (offset >= 0 && offset < uc_priv->gpio_count)
76 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
78 if (!strncasecmp(name, uc_priv->bank_name, len)) {
79 if (!strict_strtoul(name + len, 10, &offset))
85 return ret ? ret : -EINVAL;
92 *gpiop = uc_priv->gpio_base + offset;
97 static int gpio_find_and_xlate(struct gpio_desc *desc,
98 struct fdtdec_phandle_args *args)
100 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
102 /* Use the first argument as the offset by default */
103 if (args->args_count > 0)
104 desc->offset = args->args[0];
109 return ops->xlate ? ops->xlate(desc->dev, desc, args) : 0;
112 static int dm_gpio_request(struct gpio_desc *desc, const char *label)
114 struct udevice *dev = desc->dev;
115 struct gpio_dev_priv *uc_priv;
119 uc_priv = dev->uclass_priv;
120 if (uc_priv->name[desc->offset])
125 if (gpio_get_ops(dev)->request) {
126 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
132 uc_priv->name[desc->offset] = str;
137 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
143 vscnprintf(buf, sizeof(buf), fmt, args);
145 return dm_gpio_request(desc, buf);
149 * gpio_request() - [COMPAT] Request GPIO
151 * label: Name for the requested GPIO
153 * The label is copied and allocated so the caller does not need to keep
154 * the pointer around.
156 * This function implements the API that's compatible with current
157 * GPIO API used in U-Boot. The request is forwarded to particular
158 * GPIO driver. Returns 0 on success, negative value on error.
160 int gpio_request(unsigned gpio, const char *label)
162 struct gpio_desc desc;
165 ret = gpio_to_device(gpio, &desc);
169 return dm_gpio_request(&desc, label);
173 * gpio_requestf() - [COMPAT] Request GPIO
175 * @fmt: Format string for the requested GPIO
176 * @...: Arguments for the printf() format string
178 * This function implements the API that's compatible with current
179 * GPIO API used in U-Boot. The request is forwarded to particular
180 * GPIO driver. Returns 0 on success, negative value on error.
182 int gpio_requestf(unsigned gpio, const char *fmt, ...)
188 vscnprintf(buf, sizeof(buf), fmt, args);
190 return gpio_request(gpio, buf);
193 int _dm_gpio_free(struct udevice *dev, uint offset)
195 struct gpio_dev_priv *uc_priv;
198 uc_priv = dev->uclass_priv;
199 if (!uc_priv->name[offset])
201 if (gpio_get_ops(dev)->free) {
202 ret = gpio_get_ops(dev)->free(dev, offset);
207 free(uc_priv->name[offset]);
208 uc_priv->name[offset] = NULL;
214 * gpio_free() - [COMPAT] Relinquish GPIO
217 * This function implements the API that's compatible with current
218 * GPIO API used in U-Boot. The request is forwarded to particular
219 * GPIO driver. Returns 0 on success, negative value on error.
221 int gpio_free(unsigned gpio)
223 struct gpio_desc desc;
226 ret = gpio_to_device(gpio, &desc);
230 return _dm_gpio_free(desc.dev, desc.offset);
233 static int check_reserved(struct gpio_desc *desc, const char *func)
235 struct gpio_dev_priv *uc_priv = desc->dev->uclass_priv;
237 if (!uc_priv->name[desc->offset]) {
238 printf("%s: %s: error: gpio %s%d not reserved\n",
239 desc->dev->name, func,
240 uc_priv->bank_name ? uc_priv->bank_name : "",
249 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
252 * This function implements the API that's compatible with current
253 * GPIO API used in U-Boot. The request is forwarded to particular
254 * GPIO driver. Returns 0 on success, negative value on error.
256 int gpio_direction_input(unsigned gpio)
258 struct gpio_desc desc;
261 ret = gpio_to_device(gpio, &desc);
264 ret = check_reserved(&desc, "dir_input");
268 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
272 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
274 * value: Logical value to be set on the GPIO pin
276 * This function implements the API that's compatible with current
277 * GPIO API used in U-Boot. The request is forwarded to particular
278 * GPIO driver. Returns 0 on success, negative value on error.
280 int gpio_direction_output(unsigned gpio, int value)
282 struct gpio_desc desc;
285 ret = gpio_to_device(gpio, &desc);
288 ret = check_reserved(&desc, "dir_output");
292 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
296 int dm_gpio_get_value(struct gpio_desc *desc)
301 ret = check_reserved(desc, "get_value");
305 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
307 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
310 int dm_gpio_set_value(struct gpio_desc *desc, int value)
314 ret = check_reserved(desc, "set_value");
318 if (desc->flags & GPIOD_ACTIVE_LOW)
320 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
324 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
326 struct udevice *dev = desc->dev;
327 struct dm_gpio_ops *ops = gpio_get_ops(dev);
330 ret = check_reserved(desc, "set_dir");
334 if (flags & GPIOD_IS_OUT) {
335 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
337 if (flags & GPIOD_ACTIVE_LOW)
339 ret = ops->direction_output(dev, desc->offset, value);
340 } else if (flags & GPIOD_IS_IN) {
341 ret = ops->direction_input(dev, desc->offset);
346 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
354 int dm_gpio_set_dir(struct gpio_desc *desc)
356 return dm_gpio_set_dir_flags(desc, desc->flags);
360 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
363 * This function implements the API that's compatible with current
364 * GPIO API used in U-Boot. The request is forwarded to particular
365 * GPIO driver. Returns the value of the GPIO pin, or negative value
368 int gpio_get_value(unsigned gpio)
372 struct gpio_desc desc;
374 ret = gpio_to_device(gpio, &desc);
377 return dm_gpio_get_value(&desc);
381 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
383 * value: Logical value to be set on the GPIO pin.
385 * This function implements the API that's compatible with current
386 * GPIO API used in U-Boot. The request is forwarded to particular
387 * GPIO driver. Returns 0 on success, negative value on error.
389 int gpio_set_value(unsigned gpio, int value)
391 struct gpio_desc desc;
394 ret = gpio_to_device(gpio, &desc);
397 return dm_gpio_set_value(&desc, value);
400 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
402 struct gpio_dev_priv *priv;
404 /* Must be called on an active device */
405 priv = dev->uclass_priv;
408 *bit_count = priv->gpio_count;
409 return priv->bank_name;
412 static const char * const gpio_function[GPIOF_COUNT] = {
420 int get_function(struct udevice *dev, int offset, bool skip_unused,
423 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
424 struct dm_gpio_ops *ops = gpio_get_ops(dev);
426 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
427 if (!device_active(dev))
429 if (offset < 0 || offset >= uc_priv->gpio_count)
432 *namep = uc_priv->name[offset];
433 if (skip_unused && !uc_priv->name[offset])
435 if (ops->get_function) {
438 ret = ops->get_function(dev, offset);
441 if (ret >= ARRAY_SIZE(gpio_function))
446 return GPIOF_UNKNOWN;
449 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
451 return get_function(dev, offset, true, namep);
454 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
456 return get_function(dev, offset, false, namep);
459 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
461 struct dm_gpio_ops *ops = gpio_get_ops(dev);
462 struct gpio_dev_priv *priv;
468 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
471 priv = dev->uclass_priv;
472 ret = gpio_get_raw_function(dev, offset, NULL);
476 len = snprintf(str, buffsize, "%s%d: %s",
477 priv->bank_name ? priv->bank_name : "",
478 offset, gpio_function[func]);
479 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
480 func == GPIOF_UNUSED) {
484 ret = ops->get_value(dev, offset);
487 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
488 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
499 * get a number comprised of multiple GPIO values. gpio_num_array points to
500 * the array of gpio pin numbers to scan, terminated by -1.
502 unsigned gpio_get_values_as_int(const int *gpio_num_array)
505 unsigned bitmask = 1;
509 ((gpio = *gpio_num_array++) != -1)) {
510 if (gpio_get_value(gpio))
517 static int _gpio_request_by_name_nodev(const void *blob, int node,
518 const char *list_name, int index,
519 struct gpio_desc *desc, int flags,
522 struct fdtdec_phandle_args args;
527 ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
528 "#gpio-cells", 0, index, &args);
530 debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
534 ret = uclass_get_device_by_of_offset(UCLASS_GPIO, args.node,
537 debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
540 ret = gpio_find_and_xlate(desc, &args);
542 debug("%s: gpio_find_and_xlate failed\n", __func__);
545 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
546 fdt_get_name(blob, node, NULL),
549 debug("%s: dm_gpio_requestf failed\n", __func__);
552 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
554 debug("%s: dm_gpio_set_dir failed\n", __func__);
560 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
561 __func__, fdt_get_name(blob, node, NULL), list_name, index, ret);
565 int gpio_request_by_name_nodev(const void *blob, int node,
566 const char *list_name, int index,
567 struct gpio_desc *desc, int flags)
569 return _gpio_request_by_name_nodev(blob, node, list_name, index, desc,
573 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
574 struct gpio_desc *desc, int flags)
577 * This isn't ideal since we don't use dev->name in the debug()
578 * calls in gpio_request_by_name(), but we can do this until
579 * gpio_request_by_name_nodev() can be dropped.
581 return gpio_request_by_name_nodev(gd->fdt_blob, dev->of_offset,
582 list_name, index, desc, flags);
585 int gpio_request_list_by_name_nodev(const void *blob, int node,
586 const char *list_name,
587 struct gpio_desc *desc, int max_count,
593 for (count = 0; ; count++) {
594 if (count >= max_count) {
598 ret = _gpio_request_by_name_nodev(blob, node, list_name, count,
599 &desc[count], flags, true);
606 /* We ran out of GPIOs in the list */
610 gpio_free_list_nodev(desc, count - 1);
615 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
616 struct gpio_desc *desc, int max_count,
620 * This isn't ideal since we don't use dev->name in the debug()
621 * calls in gpio_request_by_name(), but we can do this until
622 * gpio_request_list_by_name_nodev() can be dropped.
624 return gpio_request_list_by_name_nodev(gd->fdt_blob, dev->of_offset,
625 list_name, desc, max_count,
629 int gpio_get_list_count(struct udevice *dev, const char *list_name)
633 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
634 list_name, "#gpio-cells", 0, -1,
637 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
638 __func__, dev->name, list_name, ret);
644 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
646 /* For now, we don't do any checking of dev */
647 return _dm_gpio_free(desc->dev, desc->offset);
650 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
654 /* For now, we don't do any checking of dev */
655 for (i = 0; i < count; i++)
656 dm_gpio_free(dev, &desc[i]);
661 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
663 return gpio_free_list(NULL, desc, count);
666 /* We need to renumber the GPIOs when any driver is probed/removed */
667 static int gpio_renumber(struct udevice *removed_dev)
669 struct gpio_dev_priv *uc_priv;
675 ret = uclass_get(UCLASS_GPIO, &uc);
679 /* Ensure that we have a base for each bank */
681 uclass_foreach_dev(dev, uc) {
682 if (device_active(dev) && dev != removed_dev) {
683 uc_priv = dev->uclass_priv;
684 uc_priv->gpio_base = base;
685 base += uc_priv->gpio_count;
692 static int gpio_post_probe(struct udevice *dev)
694 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
696 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
700 return gpio_renumber(NULL);
703 static int gpio_pre_remove(struct udevice *dev)
705 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
708 for (i = 0; i < uc_priv->gpio_count; i++) {
709 if (uc_priv->name[i])
710 free(uc_priv->name[i]);
714 return gpio_renumber(dev);
717 UCLASS_DRIVER(gpio) = {
720 .post_probe = gpio_post_probe,
721 .pre_remove = gpio_pre_remove,
722 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),