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;
255 if (!dm_gpio_is_valid(desc))
258 uc_priv = dev_get_uclass_priv(desc->dev);
259 if (!uc_priv->name[desc->offset]) {
260 printf("%s: %s: error: gpio %s%d not reserved\n",
261 desc->dev->name, func,
262 uc_priv->bank_name ? uc_priv->bank_name : "",
271 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
274 * This function implements the API that's compatible with current
275 * GPIO API used in U-Boot. The request is forwarded to particular
276 * GPIO driver. Returns 0 on success, negative value on error.
278 int gpio_direction_input(unsigned gpio)
280 struct gpio_desc desc;
283 ret = gpio_to_device(gpio, &desc);
286 ret = check_reserved(&desc, "dir_input");
290 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
294 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
296 * value: Logical value to be set on the GPIO pin
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_output(unsigned gpio, int value)
304 struct gpio_desc desc;
307 ret = gpio_to_device(gpio, &desc);
310 ret = check_reserved(&desc, "dir_output");
314 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
318 int dm_gpio_get_value(struct gpio_desc *desc)
323 ret = check_reserved(desc, "get_value");
327 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
329 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
332 int dm_gpio_set_value(struct gpio_desc *desc, int value)
336 ret = check_reserved(desc, "set_value");
340 if (desc->flags & GPIOD_ACTIVE_LOW)
342 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
346 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
348 struct udevice *dev = desc->dev;
349 struct dm_gpio_ops *ops = gpio_get_ops(dev);
352 ret = check_reserved(desc, "set_dir");
356 if (flags & GPIOD_IS_OUT) {
357 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
359 if (flags & GPIOD_ACTIVE_LOW)
361 ret = ops->direction_output(dev, desc->offset, value);
362 } else if (flags & GPIOD_IS_IN) {
363 ret = ops->direction_input(dev, desc->offset);
368 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
376 int dm_gpio_set_dir(struct gpio_desc *desc)
378 return dm_gpio_set_dir_flags(desc, desc->flags);
382 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
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 the value of the GPIO pin, or negative value
390 int gpio_get_value(unsigned gpio)
394 struct gpio_desc desc;
396 ret = gpio_to_device(gpio, &desc);
399 return dm_gpio_get_value(&desc);
403 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
405 * value: Logical value to be set on the GPIO pin.
407 * This function implements the API that's compatible with current
408 * GPIO API used in U-Boot. The request is forwarded to particular
409 * GPIO driver. Returns 0 on success, negative value on error.
411 int gpio_set_value(unsigned gpio, int value)
413 struct gpio_desc desc;
416 ret = gpio_to_device(gpio, &desc);
419 return dm_gpio_set_value(&desc, value);
422 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
424 struct gpio_dev_priv *priv;
426 /* Must be called on an active device */
427 priv = dev_get_uclass_priv(dev);
430 *bit_count = priv->gpio_count;
431 return priv->bank_name;
434 static const char * const gpio_function[GPIOF_COUNT] = {
442 int get_function(struct udevice *dev, int offset, bool skip_unused,
445 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
446 struct dm_gpio_ops *ops = gpio_get_ops(dev);
448 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
449 if (!device_active(dev))
451 if (offset < 0 || offset >= uc_priv->gpio_count)
454 *namep = uc_priv->name[offset];
455 if (skip_unused && !uc_priv->name[offset])
457 if (ops->get_function) {
460 ret = ops->get_function(dev, offset);
463 if (ret >= ARRAY_SIZE(gpio_function))
468 return GPIOF_UNKNOWN;
471 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
473 return get_function(dev, offset, true, namep);
476 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
478 return get_function(dev, offset, false, namep);
481 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
483 struct dm_gpio_ops *ops = gpio_get_ops(dev);
484 struct gpio_dev_priv *priv;
490 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
493 priv = dev_get_uclass_priv(dev);
494 ret = gpio_get_raw_function(dev, offset, NULL);
498 len = snprintf(str, buffsize, "%s%d: %s",
499 priv->bank_name ? priv->bank_name : "",
500 offset, gpio_function[func]);
501 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
502 func == GPIOF_UNUSED) {
506 ret = ops->get_value(dev, offset);
509 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
510 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
520 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
525 for (i = 0; i < 32; i++) {
526 gpio = gpio_num_array[i];
529 ret = gpio_requestf(gpio, fmt, i);
532 ret = gpio_direction_input(gpio);
541 for (i--; i >= 0; i--)
542 gpio_free(gpio_num_array[i]);
548 * get a number comprised of multiple GPIO values. gpio_num_array points to
549 * the array of gpio pin numbers to scan, terminated by -1.
551 int gpio_get_values_as_int(const int *gpio_list)
554 unsigned bitmask = 1;
559 ((gpio = *gpio_list++) != -1)) {
560 ret = gpio_get_value(gpio);
571 static int _gpio_request_by_name_nodev(const void *blob, int node,
572 const char *list_name, int index,
573 struct gpio_desc *desc, int flags,
576 struct fdtdec_phandle_args args;
581 ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
582 "#gpio-cells", 0, index, &args);
584 debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
588 ret = uclass_get_device_by_of_offset(UCLASS_GPIO, args.node,
591 debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
594 ret = gpio_find_and_xlate(desc, &args);
596 debug("%s: gpio_find_and_xlate failed\n", __func__);
599 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
600 fdt_get_name(blob, node, NULL),
603 debug("%s: dm_gpio_requestf failed\n", __func__);
606 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
608 debug("%s: dm_gpio_set_dir failed\n", __func__);
614 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
615 __func__, fdt_get_name(blob, node, NULL), list_name, index, ret);
619 int gpio_request_by_name_nodev(const void *blob, int node,
620 const char *list_name, int index,
621 struct gpio_desc *desc, int flags)
623 return _gpio_request_by_name_nodev(blob, node, list_name, index, desc,
627 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
628 struct gpio_desc *desc, int flags)
631 * This isn't ideal since we don't use dev->name in the debug()
632 * calls in gpio_request_by_name(), but we can do this until
633 * gpio_request_by_name_nodev() can be dropped.
635 return gpio_request_by_name_nodev(gd->fdt_blob, dev->of_offset,
636 list_name, index, desc, flags);
639 int gpio_request_list_by_name_nodev(const void *blob, int node,
640 const char *list_name,
641 struct gpio_desc *desc, int max_count,
647 for (count = 0; count < max_count; count++) {
648 ret = _gpio_request_by_name_nodev(blob, node, list_name, count,
649 &desc[count], flags, true);
656 /* We ran out of GPIOs in the list */
660 gpio_free_list_nodev(desc, count - 1);
665 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
666 struct gpio_desc *desc, int max_count,
670 * This isn't ideal since we don't use dev->name in the debug()
671 * calls in gpio_request_by_name(), but we can do this until
672 * gpio_request_list_by_name_nodev() can be dropped.
674 return gpio_request_list_by_name_nodev(gd->fdt_blob, dev->of_offset,
675 list_name, desc, max_count,
679 int gpio_get_list_count(struct udevice *dev, const char *list_name)
683 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
684 list_name, "#gpio-cells", 0, -1,
687 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
688 __func__, dev->name, list_name, ret);
694 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
696 /* For now, we don't do any checking of dev */
697 return _dm_gpio_free(desc->dev, desc->offset);
700 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
704 /* For now, we don't do any checking of dev */
705 for (i = 0; i < count; i++)
706 dm_gpio_free(dev, &desc[i]);
711 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
713 return gpio_free_list(NULL, desc, count);
716 /* We need to renumber the GPIOs when any driver is probed/removed */
717 static int gpio_renumber(struct udevice *removed_dev)
719 struct gpio_dev_priv *uc_priv;
725 ret = uclass_get(UCLASS_GPIO, &uc);
729 /* Ensure that we have a base for each bank */
731 uclass_foreach_dev(dev, uc) {
732 if (device_active(dev) && dev != removed_dev) {
733 uc_priv = dev_get_uclass_priv(dev);
734 uc_priv->gpio_base = base;
735 base += uc_priv->gpio_count;
742 int gpio_get_number(struct gpio_desc *desc)
744 struct udevice *dev = desc->dev;
745 struct gpio_dev_priv *uc_priv;
749 uc_priv = dev->uclass_priv;
751 return uc_priv->gpio_base + desc->offset;
754 static int gpio_post_probe(struct udevice *dev)
756 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
758 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
762 return gpio_renumber(NULL);
765 static int gpio_pre_remove(struct udevice *dev)
767 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
770 for (i = 0; i < uc_priv->gpio_count; i++) {
771 if (uc_priv->name[i])
772 free(uc_priv->name[i]);
776 return gpio_renumber(dev);
779 UCLASS_DRIVER(gpio) = {
782 .flags = DM_UC_FLAG_SEQ_ALIAS,
783 .post_probe = gpio_post_probe,
784 .pre_remove = gpio_pre_remove,
785 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),