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 < 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, struct gpio_desc *desc,
118 struct ofnode_phandle_args *args)
120 if (args->args_count < 1)
123 desc->offset = args->args[0];
125 if (args->args_count < 2)
128 if (args->args[1] & GPIO_ACTIVE_LOW)
129 desc->flags = GPIOD_ACTIVE_LOW;
134 static int gpio_find_and_xlate(struct gpio_desc *desc,
135 struct ofnode_phandle_args *args)
137 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
140 return ops->xlate(desc->dev, desc, args);
142 return gpio_xlate_offs_flags(desc->dev, desc, args);
145 int dm_gpio_request(struct gpio_desc *desc, const char *label)
147 struct udevice *dev = desc->dev;
148 struct gpio_dev_priv *uc_priv;
152 uc_priv = dev_get_uclass_priv(dev);
153 if (uc_priv->name[desc->offset])
158 if (gpio_get_ops(dev)->request) {
159 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
165 uc_priv->name[desc->offset] = str;
170 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
172 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
177 vscnprintf(buf, sizeof(buf), fmt, args);
179 return dm_gpio_request(desc, buf);
181 return dm_gpio_request(desc, fmt);
186 * gpio_request() - [COMPAT] Request GPIO
188 * label: Name for the requested GPIO
190 * The label is copied and allocated so the caller does not need to keep
191 * the pointer around.
193 * This function implements the API that's compatible with current
194 * GPIO API used in U-Boot. The request is forwarded to particular
195 * GPIO driver. Returns 0 on success, negative value on error.
197 int gpio_request(unsigned gpio, const char *label)
199 struct gpio_desc desc;
202 ret = gpio_to_device(gpio, &desc);
206 return dm_gpio_request(&desc, label);
210 * gpio_requestf() - [COMPAT] Request GPIO
212 * @fmt: Format string for the requested GPIO
213 * @...: Arguments for the printf() format string
215 * This function implements the API that's compatible with current
216 * GPIO API used in U-Boot. The request is forwarded to particular
217 * GPIO driver. Returns 0 on success, negative value on error.
219 int gpio_requestf(unsigned gpio, const char *fmt, ...)
221 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
226 vscnprintf(buf, sizeof(buf), fmt, args);
228 return gpio_request(gpio, buf);
230 return gpio_request(gpio, fmt);
234 int _dm_gpio_free(struct udevice *dev, uint offset)
236 struct gpio_dev_priv *uc_priv;
239 uc_priv = dev_get_uclass_priv(dev);
240 if (!uc_priv->name[offset])
242 if (gpio_get_ops(dev)->free) {
243 ret = gpio_get_ops(dev)->free(dev, offset);
248 free(uc_priv->name[offset]);
249 uc_priv->name[offset] = NULL;
255 * gpio_free() - [COMPAT] Relinquish GPIO
258 * This function implements the API that's compatible with current
259 * GPIO API used in U-Boot. The request is forwarded to particular
260 * GPIO driver. Returns 0 on success, negative value on error.
262 int gpio_free(unsigned gpio)
264 struct gpio_desc desc;
267 ret = gpio_to_device(gpio, &desc);
271 return _dm_gpio_free(desc.dev, desc.offset);
274 static int check_reserved(const struct gpio_desc *desc, const char *func)
276 struct gpio_dev_priv *uc_priv;
278 if (!dm_gpio_is_valid(desc))
281 uc_priv = dev_get_uclass_priv(desc->dev);
282 if (!uc_priv->name[desc->offset]) {
283 printf("%s: %s: error: gpio %s%d not reserved\n",
284 desc->dev->name, func,
285 uc_priv->bank_name ? uc_priv->bank_name : "",
294 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
297 * This function implements the API that's compatible with current
298 * GPIO API used in U-Boot. The request is forwarded to particular
299 * GPIO driver. Returns 0 on success, negative value on error.
301 int gpio_direction_input(unsigned gpio)
303 struct gpio_desc desc;
306 ret = gpio_to_device(gpio, &desc);
309 ret = check_reserved(&desc, "dir_input");
313 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
317 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
319 * value: Logical value to be set on the GPIO pin
321 * This function implements the API that's compatible with current
322 * GPIO API used in U-Boot. The request is forwarded to particular
323 * GPIO driver. Returns 0 on success, negative value on error.
325 int gpio_direction_output(unsigned gpio, int value)
327 struct gpio_desc desc;
330 ret = gpio_to_device(gpio, &desc);
333 ret = check_reserved(&desc, "dir_output");
337 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
341 int dm_gpio_get_value(const struct gpio_desc *desc)
346 ret = check_reserved(desc, "get_value");
350 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
352 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
355 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
359 ret = check_reserved(desc, "set_value");
363 if (desc->flags & GPIOD_ACTIVE_LOW)
365 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
369 int dm_gpio_get_open_drain(struct gpio_desc *desc)
371 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
374 ret = check_reserved(desc, "get_open_drain");
378 if (ops->set_open_drain)
379 return ops->get_open_drain(desc->dev, desc->offset);
384 int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
386 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
389 ret = check_reserved(desc, "set_open_drain");
393 if (ops->set_open_drain)
394 ret = ops->set_open_drain(desc->dev, desc->offset, value);
396 return 0; /* feature not supported -> ignore setting */
401 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
403 struct udevice *dev = desc->dev;
404 struct dm_gpio_ops *ops = gpio_get_ops(dev);
407 ret = check_reserved(desc, "set_dir");
411 if (flags & GPIOD_IS_OUT) {
412 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
414 if (flags & GPIOD_ACTIVE_LOW)
416 ret = ops->direction_output(dev, desc->offset, value);
417 } else if (flags & GPIOD_IS_IN) {
418 ret = ops->direction_input(dev, desc->offset);
423 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
431 int dm_gpio_set_dir(struct gpio_desc *desc)
433 return dm_gpio_set_dir_flags(desc, desc->flags);
437 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
440 * This function implements the API that's compatible with current
441 * GPIO API used in U-Boot. The request is forwarded to particular
442 * GPIO driver. Returns the value of the GPIO pin, or negative value
445 int gpio_get_value(unsigned gpio)
449 struct gpio_desc desc;
451 ret = gpio_to_device(gpio, &desc);
454 return dm_gpio_get_value(&desc);
458 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
460 * value: Logical value to be set on the GPIO pin.
462 * This function implements the API that's compatible with current
463 * GPIO API used in U-Boot. The request is forwarded to particular
464 * GPIO driver. Returns 0 on success, negative value on error.
466 int gpio_set_value(unsigned gpio, int value)
468 struct gpio_desc desc;
471 ret = gpio_to_device(gpio, &desc);
474 return dm_gpio_set_value(&desc, value);
477 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
479 struct gpio_dev_priv *priv;
481 /* Must be called on an active device */
482 priv = dev_get_uclass_priv(dev);
485 *bit_count = priv->gpio_count;
486 return priv->bank_name;
489 static const char * const gpio_function[GPIOF_COUNT] = {
497 int get_function(struct udevice *dev, int offset, bool skip_unused,
500 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
501 struct dm_gpio_ops *ops = gpio_get_ops(dev);
503 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
504 if (!device_active(dev))
506 if (offset < 0 || offset >= uc_priv->gpio_count)
509 *namep = uc_priv->name[offset];
510 if (skip_unused && !uc_priv->name[offset])
512 if (ops->get_function) {
515 ret = ops->get_function(dev, offset);
518 if (ret >= ARRAY_SIZE(gpio_function))
523 return GPIOF_UNKNOWN;
526 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
528 return get_function(dev, offset, true, namep);
531 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
533 return get_function(dev, offset, false, namep);
536 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
538 struct dm_gpio_ops *ops = gpio_get_ops(dev);
539 struct gpio_dev_priv *priv;
545 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
548 priv = dev_get_uclass_priv(dev);
549 ret = gpio_get_raw_function(dev, offset, NULL);
553 len = snprintf(str, buffsize, "%s%d: %s",
554 priv->bank_name ? priv->bank_name : "",
555 offset, gpio_function[func]);
556 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
557 func == GPIOF_UNUSED) {
561 ret = ops->get_value(dev, offset);
564 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
565 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
575 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
580 for (i = 0; i < 32; i++) {
581 gpio = gpio_num_array[i];
584 ret = gpio_requestf(gpio, fmt, i);
587 ret = gpio_direction_input(gpio);
596 for (i--; i >= 0; i--)
597 gpio_free(gpio_num_array[i]);
603 * get a number comprised of multiple GPIO values. gpio_num_array points to
604 * the array of gpio pin numbers to scan, terminated by -1.
606 int gpio_get_values_as_int(const int *gpio_list)
609 unsigned bitmask = 1;
614 ((gpio = *gpio_list++) != -1)) {
615 ret = gpio_get_value(gpio);
626 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
628 unsigned bitmask = 1;
632 for (i = 0; i < count; i++) {
633 ret = dm_gpio_get_value(&desc_list[i]);
644 static int gpio_request_tail(int ret, ofnode node,
645 struct ofnode_phandle_args *args,
646 const char *list_name, int index,
647 struct gpio_desc *desc, int flags, bool add_index)
655 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
658 debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
661 ret = gpio_find_and_xlate(desc, args);
663 debug("%s: gpio_find_and_xlate failed\n", __func__);
666 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
667 ofnode_get_name(node),
670 debug("%s: dm_gpio_requestf failed\n", __func__);
673 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
675 debug("%s: dm_gpio_set_dir failed\n", __func__);
681 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
682 __func__, ofnode_get_name(node), list_name, index, ret);
686 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
687 int index, struct gpio_desc *desc,
688 int flags, bool add_index)
690 struct ofnode_phandle_args args;
693 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
696 return gpio_request_tail(ret, node, &args, list_name, index, desc,
700 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
701 struct gpio_desc *desc, int flags)
703 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
707 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
708 struct gpio_desc *desc, int flags)
710 struct ofnode_phandle_args args;
713 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
716 return gpio_request_tail(ret, dev_ofnode(dev), &args, list_name,
717 index, desc, flags, index > 0);
720 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
721 struct gpio_desc *desc, int max_count,
727 for (count = 0; count < max_count; count++) {
728 ret = _gpio_request_by_name_nodev(node, list_name, count,
729 &desc[count], flags, true);
736 /* We ran out of GPIOs in the list */
740 gpio_free_list_nodev(desc, count - 1);
745 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
746 struct gpio_desc *desc, int max_count,
750 * This isn't ideal since we don't use dev->name in the debug()
751 * calls in gpio_request_by_name(), but we can do this until
752 * gpio_request_list_by_name_nodev() can be dropped.
754 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
758 int gpio_get_list_count(struct udevice *dev, const char *list_name)
762 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
763 list_name, "#gpio-cells", 0, -1,
766 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
767 __func__, dev->name, list_name, ret);
773 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
775 /* For now, we don't do any checking of dev */
776 return _dm_gpio_free(desc->dev, desc->offset);
779 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
783 /* For now, we don't do any checking of dev */
784 for (i = 0; i < count; i++)
785 dm_gpio_free(dev, &desc[i]);
790 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
792 return gpio_free_list(NULL, desc, count);
795 /* We need to renumber the GPIOs when any driver is probed/removed */
796 static int gpio_renumber(struct udevice *removed_dev)
798 struct gpio_dev_priv *uc_priv;
804 ret = uclass_get(UCLASS_GPIO, &uc);
808 /* Ensure that we have a base for each bank */
810 uclass_foreach_dev(dev, uc) {
811 if (device_active(dev) && dev != removed_dev) {
812 uc_priv = dev_get_uclass_priv(dev);
813 uc_priv->gpio_base = base;
814 base += uc_priv->gpio_count;
821 int gpio_get_number(const struct gpio_desc *desc)
823 struct udevice *dev = desc->dev;
824 struct gpio_dev_priv *uc_priv;
828 uc_priv = dev->uclass_priv;
830 return uc_priv->gpio_base + desc->offset;
833 static int gpio_post_probe(struct udevice *dev)
835 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
837 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
841 return gpio_renumber(NULL);
844 static int gpio_pre_remove(struct udevice *dev)
846 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
849 for (i = 0; i < uc_priv->gpio_count; i++) {
850 if (uc_priv->name[i])
851 free(uc_priv->name[i]);
855 return gpio_renumber(dev);
858 UCLASS_DRIVER(gpio) = {
861 .flags = DM_UC_FLAG_SEQ_ALIAS,
862 .post_probe = gpio_post_probe,
863 .pre_remove = gpio_pre_remove,
864 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),