2 * Copyright (c) 2013 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <dt-bindings/gpio/gpio.h>
14 #include <linux/bug.h>
15 #include <linux/ctype.h>
17 DECLARE_GLOBAL_DATA_PTR;
20 * gpio_to_device() - Convert global GPIO number to device, number
22 * Convert the GPIO number to an entry in the list of GPIOs
23 * or GPIO blocks registered with the GPIO controller. Returns
24 * entry on success, NULL on error.
26 * @gpio: The numeric representation of the GPIO
27 * @desc: Returns description (desc->flags will always be 0)
28 * @return 0 if found, -ENOENT if not found
30 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
32 struct gpio_dev_priv *uc_priv;
36 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
38 ret = uclass_next_device(&dev)) {
39 uc_priv = dev_get_uclass_priv(dev);
40 if (gpio >= uc_priv->gpio_base &&
41 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
43 desc->offset = gpio - uc_priv->gpio_base;
50 return ret ? ret : -ENOENT;
53 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
55 struct gpio_dev_priv *uc_priv = NULL;
61 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
62 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
64 ret = uclass_next_device(&dev)) {
67 uc_priv = dev_get_uclass_priv(dev);
69 offset = numeric - uc_priv->gpio_base;
70 /* Allow GPIOs to be numbered from 0 */
71 if (offset >= 0 && offset < uc_priv->gpio_count)
75 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
77 if (!strncasecmp(name, uc_priv->bank_name, len)) {
78 if (!strict_strtoul(name + len, 10, &offset))
84 return ret ? ret : -EINVAL;
87 desc->offset = offset;
92 int gpio_lookup_name(const char *name, struct udevice **devp,
93 unsigned int *offsetp, unsigned int *gpiop)
95 struct gpio_desc desc;
100 ret = dm_gpio_lookup_name(name, &desc);
107 *offsetp = desc.offset;
109 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
111 *gpiop = uc_priv->gpio_base + desc.offset;
117 int gpio_xlate_offs_flags(struct udevice *dev,
118 struct gpio_desc *desc,
119 struct fdtdec_phandle_args *args)
121 if (args->args_count < 1)
124 desc->offset = args->args[0];
126 if (args->args_count < 2)
129 if (args->args[1] & GPIO_ACTIVE_LOW)
130 desc->flags = GPIOD_ACTIVE_LOW;
135 static int gpio_find_and_xlate(struct gpio_desc *desc,
136 struct fdtdec_phandle_args *args)
138 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
141 return ops->xlate(desc->dev, desc, args);
143 return gpio_xlate_offs_flags(desc->dev, desc, args);
146 int dm_gpio_request(struct gpio_desc *desc, const char *label)
148 struct udevice *dev = desc->dev;
149 struct gpio_dev_priv *uc_priv;
153 uc_priv = dev_get_uclass_priv(dev);
154 if (uc_priv->name[desc->offset])
159 if (gpio_get_ops(dev)->request) {
160 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
166 uc_priv->name[desc->offset] = str;
171 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
173 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
178 vscnprintf(buf, sizeof(buf), fmt, args);
180 return dm_gpio_request(desc, buf);
182 return dm_gpio_request(desc, fmt);
187 * gpio_request() - [COMPAT] Request GPIO
189 * label: Name for the requested GPIO
191 * The label is copied and allocated so the caller does not need to keep
192 * the pointer around.
194 * This function implements the API that's compatible with current
195 * GPIO API used in U-Boot. The request is forwarded to particular
196 * GPIO driver. Returns 0 on success, negative value on error.
198 int gpio_request(unsigned gpio, const char *label)
200 struct gpio_desc desc;
203 ret = gpio_to_device(gpio, &desc);
207 return dm_gpio_request(&desc, label);
211 * gpio_requestf() - [COMPAT] Request GPIO
213 * @fmt: Format string for the requested GPIO
214 * @...: Arguments for the printf() format string
216 * This function implements the API that's compatible with current
217 * GPIO API used in U-Boot. The request is forwarded to particular
218 * GPIO driver. Returns 0 on success, negative value on error.
220 int gpio_requestf(unsigned gpio, const char *fmt, ...)
222 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
227 vscnprintf(buf, sizeof(buf), fmt, args);
229 return gpio_request(gpio, buf);
231 return gpio_request(gpio, fmt);
235 int _dm_gpio_free(struct udevice *dev, uint offset)
237 struct gpio_dev_priv *uc_priv;
240 uc_priv = dev_get_uclass_priv(dev);
241 if (!uc_priv->name[offset])
243 if (gpio_get_ops(dev)->free) {
244 ret = gpio_get_ops(dev)->free(dev, offset);
249 free(uc_priv->name[offset]);
250 uc_priv->name[offset] = NULL;
256 * gpio_free() - [COMPAT] Relinquish GPIO
259 * This function implements the API that's compatible with current
260 * GPIO API used in U-Boot. The request is forwarded to particular
261 * GPIO driver. Returns 0 on success, negative value on error.
263 int gpio_free(unsigned gpio)
265 struct gpio_desc desc;
268 ret = gpio_to_device(gpio, &desc);
272 return _dm_gpio_free(desc.dev, desc.offset);
275 static int check_reserved(const struct gpio_desc *desc, const char *func)
277 struct gpio_dev_priv *uc_priv;
279 if (!dm_gpio_is_valid(desc))
282 uc_priv = dev_get_uclass_priv(desc->dev);
283 if (!uc_priv->name[desc->offset]) {
284 printf("%s: %s: error: gpio %s%d not reserved\n",
285 desc->dev->name, func,
286 uc_priv->bank_name ? uc_priv->bank_name : "",
295 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
298 * This function implements the API that's compatible with current
299 * GPIO API used in U-Boot. The request is forwarded to particular
300 * GPIO driver. Returns 0 on success, negative value on error.
302 int gpio_direction_input(unsigned gpio)
304 struct gpio_desc desc;
307 ret = gpio_to_device(gpio, &desc);
310 ret = check_reserved(&desc, "dir_input");
314 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
318 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
320 * value: Logical value to be set on the GPIO pin
322 * This function implements the API that's compatible with current
323 * GPIO API used in U-Boot. The request is forwarded to particular
324 * GPIO driver. Returns 0 on success, negative value on error.
326 int gpio_direction_output(unsigned gpio, int value)
328 struct gpio_desc desc;
331 ret = gpio_to_device(gpio, &desc);
334 ret = check_reserved(&desc, "dir_output");
338 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
342 int dm_gpio_get_value(const struct gpio_desc *desc)
347 ret = check_reserved(desc, "get_value");
351 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
353 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
356 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
360 ret = check_reserved(desc, "set_value");
364 if (desc->flags & GPIOD_ACTIVE_LOW)
366 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
370 int dm_gpio_get_open_drain(struct gpio_desc *desc)
372 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
375 ret = check_reserved(desc, "get_open_drain");
379 if (ops->set_open_drain)
380 return ops->get_open_drain(desc->dev, desc->offset);
385 int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
387 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
390 ret = check_reserved(desc, "set_open_drain");
394 if (ops->set_open_drain)
395 ret = ops->set_open_drain(desc->dev, desc->offset, value);
397 return 0; /* feature not supported -> ignore setting */
402 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
404 struct udevice *dev = desc->dev;
405 struct dm_gpio_ops *ops = gpio_get_ops(dev);
408 ret = check_reserved(desc, "set_dir");
412 if (flags & GPIOD_IS_OUT) {
413 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
415 if (flags & GPIOD_ACTIVE_LOW)
417 ret = ops->direction_output(dev, desc->offset, value);
418 } else if (flags & GPIOD_IS_IN) {
419 ret = ops->direction_input(dev, desc->offset);
424 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
432 int dm_gpio_set_dir(struct gpio_desc *desc)
434 return dm_gpio_set_dir_flags(desc, desc->flags);
438 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
441 * This function implements the API that's compatible with current
442 * GPIO API used in U-Boot. The request is forwarded to particular
443 * GPIO driver. Returns the value of the GPIO pin, or negative value
446 int gpio_get_value(unsigned gpio)
450 struct gpio_desc desc;
452 ret = gpio_to_device(gpio, &desc);
455 return dm_gpio_get_value(&desc);
459 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
461 * value: Logical value to be set on the GPIO pin.
463 * This function implements the API that's compatible with current
464 * GPIO API used in U-Boot. The request is forwarded to particular
465 * GPIO driver. Returns 0 on success, negative value on error.
467 int gpio_set_value(unsigned gpio, int value)
469 struct gpio_desc desc;
472 ret = gpio_to_device(gpio, &desc);
475 return dm_gpio_set_value(&desc, value);
478 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
480 struct gpio_dev_priv *priv;
482 /* Must be called on an active device */
483 priv = dev_get_uclass_priv(dev);
486 *bit_count = priv->gpio_count;
487 return priv->bank_name;
490 static const char * const gpio_function[GPIOF_COUNT] = {
498 int get_function(struct udevice *dev, int offset, bool skip_unused,
501 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
502 struct dm_gpio_ops *ops = gpio_get_ops(dev);
504 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
505 if (!device_active(dev))
507 if (offset < 0 || offset >= uc_priv->gpio_count)
510 *namep = uc_priv->name[offset];
511 if (skip_unused && !uc_priv->name[offset])
513 if (ops->get_function) {
516 ret = ops->get_function(dev, offset);
519 if (ret >= ARRAY_SIZE(gpio_function))
524 return GPIOF_UNKNOWN;
527 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
529 return get_function(dev, offset, true, namep);
532 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
534 return get_function(dev, offset, false, namep);
537 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
539 struct dm_gpio_ops *ops = gpio_get_ops(dev);
540 struct gpio_dev_priv *priv;
546 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
549 priv = dev_get_uclass_priv(dev);
550 ret = gpio_get_raw_function(dev, offset, NULL);
554 len = snprintf(str, buffsize, "%s%d: %s",
555 priv->bank_name ? priv->bank_name : "",
556 offset, gpio_function[func]);
557 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
558 func == GPIOF_UNUSED) {
562 ret = ops->get_value(dev, offset);
565 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
566 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
576 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
581 for (i = 0; i < 32; i++) {
582 gpio = gpio_num_array[i];
585 ret = gpio_requestf(gpio, fmt, i);
588 ret = gpio_direction_input(gpio);
597 for (i--; i >= 0; i--)
598 gpio_free(gpio_num_array[i]);
604 * get a number comprised of multiple GPIO values. gpio_num_array points to
605 * the array of gpio pin numbers to scan, terminated by -1.
607 int gpio_get_values_as_int(const int *gpio_list)
610 unsigned bitmask = 1;
615 ((gpio = *gpio_list++) != -1)) {
616 ret = gpio_get_value(gpio);
627 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
629 unsigned bitmask = 1;
633 for (i = 0; i < count; i++) {
634 ret = dm_gpio_get_value(&desc_list[i]);
645 static int _gpio_request_by_name_nodev(const void *blob, int node,
646 const char *list_name, int index,
647 struct gpio_desc *desc, int flags,
650 struct fdtdec_phandle_args args;
656 ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
657 "#gpio-cells", 0, index, &args);
659 debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
663 ret = uclass_get_device_by_of_offset(UCLASS_GPIO, args.node,
666 debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
669 ret = gpio_find_and_xlate(desc, &args);
671 debug("%s: gpio_find_and_xlate failed\n", __func__);
674 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
675 fdt_get_name(blob, node, NULL),
678 debug("%s: dm_gpio_requestf failed\n", __func__);
681 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
683 debug("%s: dm_gpio_set_dir failed\n", __func__);
689 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
690 __func__, fdt_get_name(blob, node, NULL), list_name, index, ret);
694 int gpio_request_by_name_nodev(const void *blob, int node,
695 const char *list_name, int index,
696 struct gpio_desc *desc, int flags)
698 return _gpio_request_by_name_nodev(blob, node, list_name, index, desc,
702 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
703 struct gpio_desc *desc, int flags)
706 * This isn't ideal since we don't use dev->name in the debug()
707 * calls in gpio_request_by_name(), but we can do this until
708 * gpio_request_by_name_nodev() can be dropped.
710 return gpio_request_by_name_nodev(gd->fdt_blob, dev->of_offset,
711 list_name, index, desc, flags);
714 int gpio_request_list_by_name_nodev(const void *blob, int node,
715 const char *list_name,
716 struct gpio_desc *desc, int max_count,
722 for (count = 0; count < max_count; count++) {
723 ret = _gpio_request_by_name_nodev(blob, node, list_name, count,
724 &desc[count], flags, true);
731 /* We ran out of GPIOs in the list */
735 gpio_free_list_nodev(desc, count - 1);
740 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
741 struct gpio_desc *desc, int max_count,
745 * This isn't ideal since we don't use dev->name in the debug()
746 * calls in gpio_request_by_name(), but we can do this until
747 * gpio_request_list_by_name_nodev() can be dropped.
749 return gpio_request_list_by_name_nodev(gd->fdt_blob, dev->of_offset,
750 list_name, desc, max_count,
754 int gpio_get_list_count(struct udevice *dev, const char *list_name)
758 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
759 list_name, "#gpio-cells", 0, -1,
762 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
763 __func__, dev->name, list_name, ret);
769 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
771 /* For now, we don't do any checking of dev */
772 return _dm_gpio_free(desc->dev, desc->offset);
775 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
779 /* For now, we don't do any checking of dev */
780 for (i = 0; i < count; i++)
781 dm_gpio_free(dev, &desc[i]);
786 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
788 return gpio_free_list(NULL, desc, count);
791 /* We need to renumber the GPIOs when any driver is probed/removed */
792 static int gpio_renumber(struct udevice *removed_dev)
794 struct gpio_dev_priv *uc_priv;
800 ret = uclass_get(UCLASS_GPIO, &uc);
804 /* Ensure that we have a base for each bank */
806 uclass_foreach_dev(dev, uc) {
807 if (device_active(dev) && dev != removed_dev) {
808 uc_priv = dev_get_uclass_priv(dev);
809 uc_priv->gpio_base = base;
810 base += uc_priv->gpio_count;
817 int gpio_get_number(const struct gpio_desc *desc)
819 struct udevice *dev = desc->dev;
820 struct gpio_dev_priv *uc_priv;
824 uc_priv = dev->uclass_priv;
826 return uc_priv->gpio_base + desc->offset;
829 static int gpio_post_probe(struct udevice *dev)
831 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
833 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
837 return gpio_renumber(NULL);
840 static int gpio_pre_remove(struct udevice *dev)
842 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
845 for (i = 0; i < uc_priv->gpio_count; i++) {
846 if (uc_priv->name[i])
847 free(uc_priv->name[i]);
851 return gpio_renumber(dev);
854 UCLASS_DRIVER(gpio) = {
857 .flags = DM_UC_FLAG_SEQ_ALIAS,
858 .post_probe = gpio_post_probe,
859 .pre_remove = gpio_pre_remove,
860 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),