1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
10 #include <dm/device_compat.h>
11 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.h>
14 #include <dt-bindings/gpio/gpio.h>
18 #include <acpi/acpi_device.h>
19 #include <asm/global_data.h>
21 #include <dm/device_compat.h>
22 #include <linux/bug.h>
23 #include <linux/ctype.h>
25 DECLARE_GLOBAL_DATA_PTR;
28 * gpio_desc_init() - Initialize the GPIO descriptor
30 * @desc: GPIO descriptor to initialize
32 * @offset: Offset of device GPIO
34 static void gpio_desc_init(struct gpio_desc *desc,
39 desc->offset = offset;
44 * gpio_to_device() - Convert global GPIO number to device, number
46 * Convert the GPIO number to an entry in the list of GPIOs
47 * or GPIO blocks registered with the GPIO controller. Returns
48 * entry on success, NULL on error.
50 * @gpio: The numeric representation of the GPIO
51 * @desc: Returns description (desc->flags will always be 0)
52 * @return 0 if found, -ENOENT if not found
54 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
56 struct gpio_dev_priv *uc_priv;
60 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
62 ret = uclass_next_device(&dev)) {
63 uc_priv = dev_get_uclass_priv(dev);
64 if (gpio >= uc_priv->gpio_base &&
65 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
66 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
72 return ret ? ret : -ENOENT;
75 #if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
77 * dm_gpio_lookup_label() - look for name in gpio device
79 * search in uc_priv, if there is a gpio with labelname same
82 * @name: name which is searched
83 * @uc_priv: gpio_dev_priv pointer.
84 * @offset: gpio offset within the device
85 * @return: 0 if found, -ENOENT if not.
87 static int dm_gpio_lookup_label(const char *name,
88 struct gpio_dev_priv *uc_priv, ulong *offset)
95 for (i = 0; i < uc_priv->gpio_count; i++) {
96 if (!uc_priv->name[i])
98 if (!strncmp(name, uc_priv->name[i], len)) {
107 dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
114 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
116 struct gpio_dev_priv *uc_priv = NULL;
122 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
123 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
125 ret = uclass_next_device(&dev)) {
128 uc_priv = dev_get_uclass_priv(dev);
130 offset = numeric - uc_priv->gpio_base;
131 /* Allow GPIOs to be numbered from 0 */
132 if (offset < uc_priv->gpio_count)
136 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
138 if (!strncasecmp(name, uc_priv->bank_name, len)) {
139 if (!strict_strtoul(name + len, 10, &offset))
144 * if we did not found a gpio through its bank
145 * name, we search for a valid gpio label.
147 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
152 return ret ? ret : -EINVAL;
154 gpio_desc_init(desc, dev, offset);
159 int gpio_lookup_name(const char *name, struct udevice **devp,
160 unsigned int *offsetp, unsigned int *gpiop)
162 struct gpio_desc desc;
167 ret = dm_gpio_lookup_name(name, &desc);
174 *offsetp = desc.offset;
176 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
178 *gpiop = uc_priv->gpio_base + desc.offset;
184 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
185 struct ofnode_phandle_args *args)
187 if (args->args_count < 1)
190 desc->offset = args->args[0];
192 if (args->args_count < 2)
196 if (args->args[1] & GPIO_ACTIVE_LOW)
197 desc->flags |= GPIOD_ACTIVE_LOW;
200 * need to test 2 bits for gpio output binding:
201 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
202 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
204 if (args->args[1] & GPIO_SINGLE_ENDED) {
205 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
206 desc->flags |= GPIOD_OPEN_DRAIN;
208 desc->flags |= GPIOD_OPEN_SOURCE;
211 if (args->args[1] & GPIO_PULL_UP)
212 desc->flags |= GPIOD_PULL_UP;
214 if (args->args[1] & GPIO_PULL_DOWN)
215 desc->flags |= GPIOD_PULL_DOWN;
220 static int gpio_find_and_xlate(struct gpio_desc *desc,
221 struct ofnode_phandle_args *args)
223 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
226 return ops->xlate(desc->dev, desc, args);
228 return gpio_xlate_offs_flags(desc->dev, desc, args);
231 #if defined(CONFIG_GPIO_HOG)
233 struct gpio_hog_priv {
234 struct gpio_desc gpiod;
237 struct gpio_hog_data {
243 static int gpio_hog_of_to_plat(struct udevice *dev)
245 struct gpio_hog_data *plat = dev_get_plat(dev);
246 const char *nodename;
250 if (dev_read_bool(dev, "input")) {
251 plat->gpiod_flags = GPIOD_IS_IN;
252 } else if (dev_read_bool(dev, "output-high")) {
254 plat->gpiod_flags = GPIOD_IS_OUT;
255 } else if (dev_read_bool(dev, "output-low")) {
256 plat->gpiod_flags = GPIOD_IS_OUT;
258 printf("%s: missing gpio-hog state.\n", __func__);
261 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
263 printf("%s: wrong gpios property, 2 values needed %d\n",
267 nodename = dev_read_string(dev, "line-name");
269 device_set_name(dev, nodename);
274 static int gpio_hog_probe(struct udevice *dev)
276 struct gpio_hog_data *plat = dev_get_plat(dev);
277 struct gpio_hog_priv *priv = dev_get_priv(dev);
280 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
281 plat->val[0], plat->gpiod_flags,
282 plat->val[1], &priv->gpiod);
284 debug("%s: node %s could not get gpio.\n", __func__,
289 if (plat->gpiod_flags == GPIOD_IS_OUT) {
290 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
292 debug("%s: node %s could not set gpio.\n", __func__,
301 int gpio_hog_probe_all(void)
307 for (uclass_first_device(UCLASS_NOP, &dev);
309 uclass_find_next_device(&dev)) {
310 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
311 ret = device_probe(dev);
313 printf("Failed to probe device %s err: %d\n",
323 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
328 gpio_hog_probe_all();
329 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
330 struct gpio_hog_priv *priv = dev_get_priv(dev);
332 *desc = &priv->gpiod;
339 U_BOOT_DRIVER(gpio_hog) = {
342 .of_to_plat = gpio_hog_of_to_plat,
343 .probe = gpio_hog_probe,
344 .priv_auto = sizeof(struct gpio_hog_priv),
345 .plat_auto = sizeof(struct gpio_hog_data),
348 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
354 int dm_gpio_request(struct gpio_desc *desc, const char *label)
356 struct udevice *dev = desc->dev;
357 struct gpio_dev_priv *uc_priv;
361 uc_priv = dev_get_uclass_priv(dev);
362 if (uc_priv->name[desc->offset])
367 if (gpio_get_ops(dev)->request) {
368 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
374 uc_priv->name[desc->offset] = str;
379 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
381 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
386 vscnprintf(buf, sizeof(buf), fmt, args);
388 return dm_gpio_request(desc, buf);
390 return dm_gpio_request(desc, fmt);
395 * gpio_request() - [COMPAT] Request GPIO
397 * label: Name for the requested GPIO
399 * The label is copied and allocated so the caller does not need to keep
400 * the pointer around.
402 * This function implements the API that's compatible with current
403 * GPIO API used in U-Boot. The request is forwarded to particular
404 * GPIO driver. Returns 0 on success, negative value on error.
406 int gpio_request(unsigned gpio, const char *label)
408 struct gpio_desc desc;
411 ret = gpio_to_device(gpio, &desc);
415 return dm_gpio_request(&desc, label);
419 * gpio_requestf() - [COMPAT] Request GPIO
421 * @fmt: Format string for the requested GPIO
422 * @...: Arguments for the printf() format string
424 * This function implements the API that's compatible with current
425 * GPIO API used in U-Boot. The request is forwarded to particular
426 * GPIO driver. Returns 0 on success, negative value on error.
428 int gpio_requestf(unsigned gpio, const char *fmt, ...)
430 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
435 vscnprintf(buf, sizeof(buf), fmt, args);
437 return gpio_request(gpio, buf);
439 return gpio_request(gpio, fmt);
443 int _dm_gpio_free(struct udevice *dev, uint offset)
445 struct gpio_dev_priv *uc_priv;
448 uc_priv = dev_get_uclass_priv(dev);
449 if (!uc_priv->name[offset])
451 if (gpio_get_ops(dev)->rfree) {
452 ret = gpio_get_ops(dev)->rfree(dev, offset);
457 free(uc_priv->name[offset]);
458 uc_priv->name[offset] = NULL;
464 * gpio_free() - [COMPAT] Relinquish GPIO
467 * This function implements the API that's compatible with current
468 * GPIO API used in U-Boot. The request is forwarded to particular
469 * GPIO driver. Returns 0 on success, negative value on error.
471 int gpio_free(unsigned gpio)
473 struct gpio_desc desc;
476 ret = gpio_to_device(gpio, &desc);
480 return _dm_gpio_free(desc.dev, desc.offset);
483 static int check_reserved(const struct gpio_desc *desc, const char *func)
485 struct gpio_dev_priv *uc_priv;
487 if (!dm_gpio_is_valid(desc))
490 uc_priv = dev_get_uclass_priv(desc->dev);
491 if (!uc_priv->name[desc->offset]) {
492 printf("%s: %s: error: gpio %s%d not reserved\n",
493 desc->dev->name, func,
494 uc_priv->bank_name ? uc_priv->bank_name : "",
503 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
506 * This function implements the API that's compatible with current
507 * GPIO API used in U-Boot. The request is forwarded to particular
508 * GPIO driver. Returns 0 on success, negative value on error.
510 int gpio_direction_input(unsigned gpio)
512 struct gpio_desc desc;
515 ret = gpio_to_device(gpio, &desc);
518 ret = check_reserved(&desc, "dir_input");
522 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
526 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
528 * value: Logical value to be set on the GPIO pin
530 * This function implements the API that's compatible with current
531 * GPIO API used in U-Boot. The request is forwarded to particular
532 * GPIO driver. Returns 0 on success, negative value on error.
534 int gpio_direction_output(unsigned gpio, int value)
536 struct gpio_desc desc;
539 ret = gpio_to_device(gpio, &desc);
542 ret = check_reserved(&desc, "dir_output");
546 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
550 static int _gpio_get_value(const struct gpio_desc *desc)
554 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
556 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
559 int dm_gpio_get_value(const struct gpio_desc *desc)
563 ret = check_reserved(desc, "get_value");
567 return _gpio_get_value(desc);
570 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
572 const struct dm_gpio_ops *ops;
575 ret = check_reserved(desc, "set_value");
579 if (desc->flags & GPIOD_ACTIVE_LOW)
582 /* GPIOD_ are directly managed by driver in set_flags */
583 ops = gpio_get_ops(desc->dev);
584 if (ops->set_flags) {
585 ulong flags = desc->flags;
588 flags |= GPIOD_IS_OUT_ACTIVE;
590 flags &= ~GPIOD_IS_OUT_ACTIVE;
591 return ops->set_flags(desc->dev, desc->offset, flags);
595 * Emulate open drain by not actively driving the line high or
596 * Emulate open source by not actively driving the line low
598 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
599 (desc->flags & GPIOD_OPEN_SOURCE && !value))
600 return ops->direction_input(desc->dev, desc->offset);
601 else if (desc->flags & GPIOD_OPEN_DRAIN ||
602 desc->flags & GPIOD_OPEN_SOURCE)
603 return ops->direction_output(desc->dev, desc->offset, value);
605 ret = ops->set_value(desc->dev, desc->offset, value);
612 /* check dir flags invalid configuration */
613 static int check_dir_flags(ulong flags)
615 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
616 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
621 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
622 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
627 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
628 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
637 * _dm_gpio_set_flags() - Send flags to the driver
639 * This uses the best available method to send the given flags to the driver.
640 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
641 * of GPIOD_IS_OUT_ACTIVE.
643 * @desc: GPIO description
644 * @flags: flags value to set
645 * @return 0 if OK, -ve on error
647 static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
649 struct udevice *dev = desc->dev;
650 struct dm_gpio_ops *ops = gpio_get_ops(dev);
651 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
654 ret = check_dir_flags(flags);
657 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
659 uc_priv->bank_name ? uc_priv->bank_name : "",
660 desc->offset, flags);
665 /* If active low, invert the output state */
666 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
667 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
668 flags ^= GPIOD_IS_OUT_ACTIVE;
670 /* GPIOD_ are directly managed by driver in set_flags */
671 if (ops->set_flags) {
672 ret = ops->set_flags(dev, desc->offset, flags);
674 if (flags & GPIOD_IS_OUT) {
675 bool value = flags & GPIOD_IS_OUT_ACTIVE;
677 ret = ops->direction_output(dev, desc->offset, value);
678 } else if (flags & GPIOD_IS_IN) {
679 ret = ops->direction_input(dev, desc->offset);
686 int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
691 ret = check_reserved(desc, "set_dir_flags");
695 flags = (desc->flags & ~clr) | set;
697 ret = _dm_gpio_set_flags(desc, flags);
701 /* save the flags also in descriptor */
707 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
709 /* combine the requested flags (for IN/OUT) and the descriptor flags */
710 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
713 int dm_gpio_set_dir(struct gpio_desc *desc)
717 ret = check_reserved(desc, "set_dir");
721 return _dm_gpio_set_flags(desc, desc->flags);
724 int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
726 struct udevice *dev = desc->dev;
728 struct dm_gpio_ops *ops = gpio_get_ops(dev);
731 ret = check_reserved(desc, "get_flags");
735 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
736 if (ops->get_flags) {
737 ret = ops->get_flags(dev, desc->offset, &flags);
741 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
742 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
743 if (desc->flags & GPIOD_ACTIVE_LOW)
745 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
746 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
748 flags |= GPIOD_IS_OUT_ACTIVE;
751 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
752 flags &= ~GPIOD_IS_OUT_ACTIVE;
753 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
754 flags |= GPIOD_IS_OUT_ACTIVE;
762 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
765 * This function implements the API that's compatible with current
766 * GPIO API used in U-Boot. The request is forwarded to particular
767 * GPIO driver. Returns the value of the GPIO pin, or negative value
770 int gpio_get_value(unsigned gpio)
774 struct gpio_desc desc;
776 ret = gpio_to_device(gpio, &desc);
779 return dm_gpio_get_value(&desc);
783 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
785 * value: Logical value to be set on the GPIO pin.
787 * This function implements the API that's compatible with current
788 * GPIO API used in U-Boot. The request is forwarded to particular
789 * GPIO driver. Returns 0 on success, negative value on error.
791 int gpio_set_value(unsigned gpio, int value)
793 struct gpio_desc desc;
796 ret = gpio_to_device(gpio, &desc);
799 return dm_gpio_set_value(&desc, value);
802 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
804 struct gpio_dev_priv *priv;
806 /* Must be called on an active device */
807 priv = dev_get_uclass_priv(dev);
810 *bit_count = priv->gpio_count;
811 return priv->bank_name;
814 static const char * const gpio_function[GPIOF_COUNT] = {
822 static int get_function(struct udevice *dev, int offset, bool skip_unused,
825 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
826 struct dm_gpio_ops *ops = gpio_get_ops(dev);
828 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
829 if (!device_active(dev))
831 if (offset < 0 || offset >= uc_priv->gpio_count)
834 *namep = uc_priv->name[offset];
835 if (skip_unused && !uc_priv->name[offset])
837 if (ops->get_function) {
840 ret = ops->get_function(dev, offset);
843 if (ret >= ARRAY_SIZE(gpio_function))
848 return GPIOF_UNKNOWN;
851 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
853 return get_function(dev, offset, true, namep);
856 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
858 return get_function(dev, offset, false, namep);
861 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
863 struct dm_gpio_ops *ops = gpio_get_ops(dev);
864 struct gpio_dev_priv *priv;
870 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
873 priv = dev_get_uclass_priv(dev);
874 ret = gpio_get_raw_function(dev, offset, NULL);
878 len = snprintf(str, buffsize, "%s%d: %s",
879 priv->bank_name ? priv->bank_name : "",
880 offset, gpio_function[func]);
881 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
882 func == GPIOF_UNUSED) {
886 ret = ops->get_value(dev, offset);
889 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
890 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
900 #if CONFIG_IS_ENABLED(ACPIGEN)
901 int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
903 struct dm_gpio_ops *ops;
905 memset(gpio, '\0', sizeof(*gpio));
906 if (!dm_gpio_is_valid(desc)) {
907 /* Indicate that the GPIO is not valid */
913 ops = gpio_get_ops(desc->dev);
917 return ops->get_acpi(desc, gpio);
921 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
926 for (i = 0; i < 32; i++) {
927 gpio = gpio_num_array[i];
930 ret = gpio_requestf(gpio, fmt, i);
933 ret = gpio_direction_input(gpio);
942 for (i--; i >= 0; i--)
943 gpio_free(gpio_num_array[i]);
949 * get a number comprised of multiple GPIO values. gpio_num_array points to
950 * the array of gpio pin numbers to scan, terminated by -1.
952 int gpio_get_values_as_int(const int *gpio_list)
955 unsigned bitmask = 1;
960 ((gpio = *gpio_list++) != -1)) {
961 ret = gpio_get_value(gpio);
972 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
974 unsigned bitmask = 1;
978 for (i = 0; i < count; i++) {
979 ret = dm_gpio_get_value(&desc_list[i]);
991 * gpio_request_tail: common work for requesting a gpio.
993 * ret: return value from previous work in function which calls
995 * This seems bogus (why calling this function instead not
996 * calling it and end caller function instead?).
997 * Because on error in caller function we want to set some
998 * default values in gpio desc and have a common error
999 * debug message, which provides this function.
1000 * nodename: Name of node for which gpio gets requested
1001 * used for gpio label name.
1002 * args: pointer to output arguments structure
1003 * list_name: Name of GPIO list
1004 * used for gpio label name.
1005 * index: gpio index in gpio list
1006 * used for gpio label name.
1007 * desc: pointer to gpio descriptor, filled from this
1009 * flags: gpio flags to use.
1010 * add_index: should index added to gpio label name
1011 * gpio_dev: pointer to gpio device from which the gpio
1012 * will be requested. If NULL try to get the
1013 * gpio device with uclass_get_device_by_ofnode()
1015 * return: In error case this function sets default values in
1016 * gpio descriptor, also emmits a debug message.
1017 * On success it returns 0 else the error code from
1018 * function calls, or the error code passed through
1019 * ret to this function.
1022 static int gpio_request_tail(int ret, const char *nodename,
1023 struct ofnode_phandle_args *args,
1024 const char *list_name, int index,
1025 struct gpio_desc *desc, int flags,
1026 bool add_index, struct udevice *gpio_dev)
1028 gpio_desc_init(desc, gpio_dev, 0);
1033 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1036 debug("%s: uclass_get_device_by_ofnode failed\n",
1041 ret = gpio_find_and_xlate(desc, args);
1043 debug("%s: gpio_find_and_xlate failed\n", __func__);
1046 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
1047 nodename, list_name, index);
1049 debug("%s: dm_gpio_requestf failed\n", __func__);
1053 /* Keep any direction flags provided by the devicetree */
1054 ret = dm_gpio_set_dir_flags(desc,
1055 flags | (desc->flags & GPIOD_MASK_DIR));
1057 debug("%s: dm_gpio_set_dir failed\n", __func__);
1063 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
1064 __func__, nodename, list_name, index, ret);
1068 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1069 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1070 int index, struct gpio_desc *desc,
1071 int flags, bool add_index)
1073 struct ofnode_phandle_args args;
1076 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1079 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1080 index, desc, flags, add_index, NULL);
1083 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
1084 struct gpio_desc *desc, int flags)
1086 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1090 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
1091 struct gpio_desc *desc, int flags)
1093 struct ofnode_phandle_args args;
1097 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1099 node = dev_ofnode(dev);
1100 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1101 index, desc, flags, index > 0, NULL);
1104 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1105 struct gpio_desc *desc, int max_count,
1111 for (count = 0; count < max_count; count++) {
1112 ret = _gpio_request_by_name_nodev(node, list_name, count,
1113 &desc[count], flags, true);
1120 /* We ran out of GPIOs in the list */
1124 gpio_free_list_nodev(desc, count - 1);
1129 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1130 struct gpio_desc *desc, int max_count,
1134 * This isn't ideal since we don't use dev->name in the debug()
1135 * calls in gpio_request_by_name(), but we can do this until
1136 * gpio_request_list_by_name_nodev() can be dropped.
1138 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1142 int gpio_get_list_count(struct udevice *dev, const char *list_name)
1146 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, -1,
1149 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1150 __func__, dev->name, list_name, ret);
1155 #endif /* OF_PLATDATA */
1157 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1159 /* For now, we don't do any checking of dev */
1160 return _dm_gpio_free(desc->dev, desc->offset);
1163 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1167 /* For now, we don't do any checking of dev */
1168 for (i = 0; i < count; i++)
1169 dm_gpio_free(dev, &desc[i]);
1174 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1176 return gpio_free_list(NULL, desc, count);
1179 /* We need to renumber the GPIOs when any driver is probed/removed */
1180 static int gpio_renumber(struct udevice *removed_dev)
1182 struct gpio_dev_priv *uc_priv;
1183 struct udevice *dev;
1188 ret = uclass_get(UCLASS_GPIO, &uc);
1192 /* Ensure that we have a base for each bank */
1194 uclass_foreach_dev(dev, uc) {
1195 if (device_active(dev) && dev != removed_dev) {
1196 uc_priv = dev_get_uclass_priv(dev);
1197 uc_priv->gpio_base = base;
1198 base += uc_priv->gpio_count;
1205 int gpio_get_number(const struct gpio_desc *desc)
1207 struct udevice *dev = desc->dev;
1208 struct gpio_dev_priv *uc_priv;
1212 uc_priv = dev_get_uclass_priv(dev);
1214 return uc_priv->gpio_base + desc->offset;
1217 static int gpio_post_probe(struct udevice *dev)
1219 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1221 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1225 return gpio_renumber(NULL);
1228 static int gpio_pre_remove(struct udevice *dev)
1230 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1233 for (i = 0; i < uc_priv->gpio_count; i++) {
1234 if (uc_priv->name[i])
1235 free(uc_priv->name[i]);
1237 free(uc_priv->name);
1239 return gpio_renumber(dev);
1242 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1243 char *list_name, int index, int flags,
1244 int dtflags, struct gpio_desc *desc)
1246 struct ofnode_phandle_args args;
1248 args.node = ofnode_null();
1249 args.args_count = 2;
1250 args.args[0] = index;
1251 args.args[1] = dtflags;
1253 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1257 static void devm_gpiod_release(struct udevice *dev, void *res)
1259 dm_gpio_free(dev, res);
1262 static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1267 struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1268 unsigned int index, int flags)
1271 struct gpio_desc *desc;
1273 static const char suffix[] = "-gpios";
1275 propname = malloc(strlen(id) + sizeof(suffix));
1281 strcpy(propname, id);
1282 strcat(propname, suffix);
1284 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1286 if (unlikely(!desc)) {
1291 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1300 devres_add(dev, desc);
1305 struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1310 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1318 void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1322 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1326 static int gpio_post_bind(struct udevice *dev)
1328 struct udevice *child;
1331 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1332 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1333 static int reloc_done;
1337 ops->request += gd->reloc_off;
1339 ops->rfree += gd->reloc_off;
1340 if (ops->direction_input)
1341 ops->direction_input += gd->reloc_off;
1342 if (ops->direction_output)
1343 ops->direction_output += gd->reloc_off;
1345 ops->get_value += gd->reloc_off;
1347 ops->set_value += gd->reloc_off;
1348 if (ops->get_function)
1349 ops->get_function += gd->reloc_off;
1351 ops->xlate += gd->reloc_off;
1353 ops->set_flags += gd->reloc_off;
1355 ops->get_flags += gd->reloc_off;
1361 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1362 dev_for_each_subnode(node, dev) {
1363 if (ofnode_read_bool(node, "gpio-hog")) {
1364 const char *name = ofnode_get_name(node);
1367 ret = device_bind_driver_to_node(dev,
1379 UCLASS_DRIVER(gpio) = {
1382 .flags = DM_UC_FLAG_SEQ_ALIAS,
1383 .post_probe = gpio_post_probe,
1384 .post_bind = gpio_post_bind,
1385 .pre_remove = gpio_pre_remove,
1386 .per_device_auto = sizeof(struct gpio_dev_priv),