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, ...)
157 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
162 vscnprintf(buf, sizeof(buf), fmt, args);
164 return dm_gpio_request(desc, buf);
166 return dm_gpio_request(desc, fmt);
171 * gpio_request() - [COMPAT] Request GPIO
173 * label: Name for the requested GPIO
175 * The label is copied and allocated so the caller does not need to keep
176 * the pointer around.
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_request(unsigned gpio, const char *label)
184 struct gpio_desc desc;
187 ret = gpio_to_device(gpio, &desc);
191 return dm_gpio_request(&desc, label);
195 * gpio_requestf() - [COMPAT] Request GPIO
197 * @fmt: Format string for the requested GPIO
198 * @...: Arguments for the printf() format string
200 * This function implements the API that's compatible with current
201 * GPIO API used in U-Boot. The request is forwarded to particular
202 * GPIO driver. Returns 0 on success, negative value on error.
204 int gpio_requestf(unsigned gpio, const char *fmt, ...)
206 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
211 vscnprintf(buf, sizeof(buf), fmt, args);
213 return gpio_request(gpio, buf);
215 return gpio_request(gpio, fmt);
219 int _dm_gpio_free(struct udevice *dev, uint offset)
221 struct gpio_dev_priv *uc_priv;
224 uc_priv = dev_get_uclass_priv(dev);
225 if (!uc_priv->name[offset])
227 if (gpio_get_ops(dev)->free) {
228 ret = gpio_get_ops(dev)->free(dev, offset);
233 free(uc_priv->name[offset]);
234 uc_priv->name[offset] = NULL;
240 * gpio_free() - [COMPAT] Relinquish GPIO
243 * This function implements the API that's compatible with current
244 * GPIO API used in U-Boot. The request is forwarded to particular
245 * GPIO driver. Returns 0 on success, negative value on error.
247 int gpio_free(unsigned gpio)
249 struct gpio_desc desc;
252 ret = gpio_to_device(gpio, &desc);
256 return _dm_gpio_free(desc.dev, desc.offset);
259 static int check_reserved(struct gpio_desc *desc, const char *func)
261 struct gpio_dev_priv *uc_priv;
263 if (!dm_gpio_is_valid(desc))
266 uc_priv = dev_get_uclass_priv(desc->dev);
267 if (!uc_priv->name[desc->offset]) {
268 printf("%s: %s: error: gpio %s%d not reserved\n",
269 desc->dev->name, func,
270 uc_priv->bank_name ? uc_priv->bank_name : "",
279 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
282 * This function implements the API that's compatible with current
283 * GPIO API used in U-Boot. The request is forwarded to particular
284 * GPIO driver. Returns 0 on success, negative value on error.
286 int gpio_direction_input(unsigned gpio)
288 struct gpio_desc desc;
291 ret = gpio_to_device(gpio, &desc);
294 ret = check_reserved(&desc, "dir_input");
298 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
302 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
304 * value: Logical value to be set on the GPIO pin
306 * This function implements the API that's compatible with current
307 * GPIO API used in U-Boot. The request is forwarded to particular
308 * GPIO driver. Returns 0 on success, negative value on error.
310 int gpio_direction_output(unsigned gpio, int value)
312 struct gpio_desc desc;
315 ret = gpio_to_device(gpio, &desc);
318 ret = check_reserved(&desc, "dir_output");
322 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
326 int dm_gpio_get_value(struct gpio_desc *desc)
331 ret = check_reserved(desc, "get_value");
335 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
337 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
340 int dm_gpio_set_value(struct gpio_desc *desc, int value)
344 ret = check_reserved(desc, "set_value");
348 if (desc->flags & GPIOD_ACTIVE_LOW)
350 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
354 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
356 struct udevice *dev = desc->dev;
357 struct dm_gpio_ops *ops = gpio_get_ops(dev);
360 ret = check_reserved(desc, "set_dir");
364 if (flags & GPIOD_IS_OUT) {
365 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
367 if (flags & GPIOD_ACTIVE_LOW)
369 ret = ops->direction_output(dev, desc->offset, value);
370 } else if (flags & GPIOD_IS_IN) {
371 ret = ops->direction_input(dev, desc->offset);
376 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
384 int dm_gpio_set_dir(struct gpio_desc *desc)
386 return dm_gpio_set_dir_flags(desc, desc->flags);
390 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
393 * This function implements the API that's compatible with current
394 * GPIO API used in U-Boot. The request is forwarded to particular
395 * GPIO driver. Returns the value of the GPIO pin, or negative value
398 int gpio_get_value(unsigned gpio)
402 struct gpio_desc desc;
404 ret = gpio_to_device(gpio, &desc);
407 return dm_gpio_get_value(&desc);
411 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
413 * value: Logical value to be set on the GPIO pin.
415 * This function implements the API that's compatible with current
416 * GPIO API used in U-Boot. The request is forwarded to particular
417 * GPIO driver. Returns 0 on success, negative value on error.
419 int gpio_set_value(unsigned gpio, int value)
421 struct gpio_desc desc;
424 ret = gpio_to_device(gpio, &desc);
427 return dm_gpio_set_value(&desc, value);
430 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
432 struct gpio_dev_priv *priv;
434 /* Must be called on an active device */
435 priv = dev_get_uclass_priv(dev);
438 *bit_count = priv->gpio_count;
439 return priv->bank_name;
442 static const char * const gpio_function[GPIOF_COUNT] = {
450 int get_function(struct udevice *dev, int offset, bool skip_unused,
453 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
454 struct dm_gpio_ops *ops = gpio_get_ops(dev);
456 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
457 if (!device_active(dev))
459 if (offset < 0 || offset >= uc_priv->gpio_count)
462 *namep = uc_priv->name[offset];
463 if (skip_unused && !uc_priv->name[offset])
465 if (ops->get_function) {
468 ret = ops->get_function(dev, offset);
471 if (ret >= ARRAY_SIZE(gpio_function))
476 return GPIOF_UNKNOWN;
479 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
481 return get_function(dev, offset, true, namep);
484 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
486 return get_function(dev, offset, false, namep);
489 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
491 struct dm_gpio_ops *ops = gpio_get_ops(dev);
492 struct gpio_dev_priv *priv;
498 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
501 priv = dev_get_uclass_priv(dev);
502 ret = gpio_get_raw_function(dev, offset, NULL);
506 len = snprintf(str, buffsize, "%s%d: %s",
507 priv->bank_name ? priv->bank_name : "",
508 offset, gpio_function[func]);
509 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
510 func == GPIOF_UNUSED) {
514 ret = ops->get_value(dev, offset);
517 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
518 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
528 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
533 for (i = 0; i < 32; i++) {
534 gpio = gpio_num_array[i];
537 ret = gpio_requestf(gpio, fmt, i);
540 ret = gpio_direction_input(gpio);
549 for (i--; i >= 0; i--)
550 gpio_free(gpio_num_array[i]);
556 * get a number comprised of multiple GPIO values. gpio_num_array points to
557 * the array of gpio pin numbers to scan, terminated by -1.
559 int gpio_get_values_as_int(const int *gpio_list)
562 unsigned bitmask = 1;
567 ((gpio = *gpio_list++) != -1)) {
568 ret = gpio_get_value(gpio);
579 static int _gpio_request_by_name_nodev(const void *blob, int node,
580 const char *list_name, int index,
581 struct gpio_desc *desc, int flags,
584 struct fdtdec_phandle_args args;
589 ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
590 "#gpio-cells", 0, index, &args);
592 debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
596 ret = uclass_get_device_by_of_offset(UCLASS_GPIO, args.node,
599 debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
602 ret = gpio_find_and_xlate(desc, &args);
604 debug("%s: gpio_find_and_xlate failed\n", __func__);
607 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
608 fdt_get_name(blob, node, NULL),
611 debug("%s: dm_gpio_requestf failed\n", __func__);
614 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
616 debug("%s: dm_gpio_set_dir failed\n", __func__);
622 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
623 __func__, fdt_get_name(blob, node, NULL), list_name, index, ret);
627 int gpio_request_by_name_nodev(const void *blob, int node,
628 const char *list_name, int index,
629 struct gpio_desc *desc, int flags)
631 return _gpio_request_by_name_nodev(blob, node, list_name, index, desc,
635 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
636 struct gpio_desc *desc, int flags)
639 * This isn't ideal since we don't use dev->name in the debug()
640 * calls in gpio_request_by_name(), but we can do this until
641 * gpio_request_by_name_nodev() can be dropped.
643 return gpio_request_by_name_nodev(gd->fdt_blob, dev->of_offset,
644 list_name, index, desc, flags);
647 int gpio_request_list_by_name_nodev(const void *blob, int node,
648 const char *list_name,
649 struct gpio_desc *desc, int max_count,
655 for (count = 0; count < max_count; count++) {
656 ret = _gpio_request_by_name_nodev(blob, node, list_name, count,
657 &desc[count], flags, true);
664 /* We ran out of GPIOs in the list */
668 gpio_free_list_nodev(desc, count - 1);
673 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
674 struct gpio_desc *desc, int max_count,
678 * This isn't ideal since we don't use dev->name in the debug()
679 * calls in gpio_request_by_name(), but we can do this until
680 * gpio_request_list_by_name_nodev() can be dropped.
682 return gpio_request_list_by_name_nodev(gd->fdt_blob, dev->of_offset,
683 list_name, desc, max_count,
687 int gpio_get_list_count(struct udevice *dev, const char *list_name)
691 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
692 list_name, "#gpio-cells", 0, -1,
695 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
696 __func__, dev->name, list_name, ret);
702 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
704 /* For now, we don't do any checking of dev */
705 return _dm_gpio_free(desc->dev, desc->offset);
708 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
712 /* For now, we don't do any checking of dev */
713 for (i = 0; i < count; i++)
714 dm_gpio_free(dev, &desc[i]);
719 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
721 return gpio_free_list(NULL, desc, count);
724 /* We need to renumber the GPIOs when any driver is probed/removed */
725 static int gpio_renumber(struct udevice *removed_dev)
727 struct gpio_dev_priv *uc_priv;
733 ret = uclass_get(UCLASS_GPIO, &uc);
737 /* Ensure that we have a base for each bank */
739 uclass_foreach_dev(dev, uc) {
740 if (device_active(dev) && dev != removed_dev) {
741 uc_priv = dev_get_uclass_priv(dev);
742 uc_priv->gpio_base = base;
743 base += uc_priv->gpio_count;
750 int gpio_get_number(struct gpio_desc *desc)
752 struct udevice *dev = desc->dev;
753 struct gpio_dev_priv *uc_priv;
757 uc_priv = dev->uclass_priv;
759 return uc_priv->gpio_base + desc->offset;
762 static int gpio_post_probe(struct udevice *dev)
764 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
766 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
770 return gpio_renumber(NULL);
773 static int gpio_pre_remove(struct udevice *dev)
775 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
778 for (i = 0; i < uc_priv->gpio_count; i++) {
779 if (uc_priv->name[i])
780 free(uc_priv->name[i]);
784 return gpio_renumber(dev);
787 UCLASS_DRIVER(gpio) = {
790 .flags = DM_UC_FLAG_SEQ_ALIAS,
791 .post_probe = gpio_post_probe,
792 .pre_remove = gpio_pre_remove,
793 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),