1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
6 #define LOG_CATEGORY UCLASS_GPIO
10 #include <dt-structs.h>
12 #include <dm/devres.h>
13 #include <dm/device_compat.h>
14 #include <dm/device-internal.h>
16 #include <dm/uclass-internal.h>
17 #include <dt-bindings/gpio/gpio.h>
21 #include <acpi/acpi_device.h>
22 #include <asm/global_data.h>
24 #include <dm/device_compat.h>
25 #include <linux/bug.h>
26 #include <linux/ctype.h>
27 #include <linux/delay.h>
29 DECLARE_GLOBAL_DATA_PTR;
32 * gpio_desc_init() - Initialize the GPIO descriptor
34 * @desc: GPIO descriptor to initialize
36 * @offset: Offset of device GPIO
38 static void gpio_desc_init(struct gpio_desc *desc,
43 desc->offset = offset;
48 * gpio_to_device() - Convert global GPIO number to device, number
50 * Convert the GPIO number to an entry in the list of GPIOs
51 * or GPIO blocks registered with the GPIO controller. Returns
52 * entry on success, NULL on error.
54 * @gpio: The numeric representation of the GPIO
55 * @desc: Returns description (desc->flags will always be 0)
56 * @return 0 if found, -ENOENT if not found
58 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
60 struct gpio_dev_priv *uc_priv;
64 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
66 ret = uclass_next_device(&dev)) {
67 uc_priv = dev_get_uclass_priv(dev);
68 if (gpio >= uc_priv->gpio_base &&
69 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
70 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
76 return ret ? ret : -ENOENT;
79 #if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
81 * dm_gpio_lookup_label() - look for name in gpio device
83 * search in uc_priv, if there is a gpio with labelname same
86 * @name: name which is searched
87 * @uc_priv: gpio_dev_priv pointer.
88 * @offset: gpio offset within the device
89 * @return: 0 if found, -ENOENT if not.
91 static int dm_gpio_lookup_label(const char *name,
92 struct gpio_dev_priv *uc_priv, ulong *offset)
99 for (i = 0; i < uc_priv->gpio_count; i++) {
100 if (!uc_priv->name[i])
102 if (!strncmp(name, uc_priv->name[i], len)) {
111 dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
118 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
120 struct gpio_dev_priv *uc_priv = NULL;
126 numeric = isdigit(*name) ? dectoul(name, NULL) : -1;
127 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
129 ret = uclass_next_device(&dev)) {
132 uc_priv = dev_get_uclass_priv(dev);
134 offset = numeric - uc_priv->gpio_base;
135 /* Allow GPIOs to be numbered from 0 */
136 if (offset < uc_priv->gpio_count)
140 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
142 if (!strncasecmp(name, uc_priv->bank_name, len)) {
143 if (!strict_strtoul(name + len, 10, &offset))
144 if (offset < uc_priv->gpio_count)
149 * if we did not found a gpio through its bank
150 * name, we search for a valid gpio label.
152 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
157 return ret ? ret : -EINVAL;
159 gpio_desc_init(desc, dev, offset);
164 int gpio_lookup_name(const char *name, struct udevice **devp,
165 unsigned int *offsetp, unsigned int *gpiop)
167 struct gpio_desc desc;
172 ret = dm_gpio_lookup_name(name, &desc);
179 *offsetp = desc.offset;
181 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
183 *gpiop = uc_priv->gpio_base + desc.offset;
189 unsigned long gpio_flags_xlate(uint32_t arg)
191 unsigned long flags = 0;
193 if (arg & GPIO_ACTIVE_LOW)
194 flags |= GPIOD_ACTIVE_LOW;
197 * need to test 2 bits for gpio output binding:
198 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
199 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
201 if (arg & GPIO_SINGLE_ENDED) {
202 if (arg & GPIO_LINE_OPEN_DRAIN)
203 flags |= GPIOD_OPEN_DRAIN;
205 flags |= GPIOD_OPEN_SOURCE;
208 if (arg & GPIO_PULL_UP)
209 flags |= GPIOD_PULL_UP;
211 if (arg & GPIO_PULL_DOWN)
212 flags |= GPIOD_PULL_DOWN;
217 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
218 struct ofnode_phandle_args *args)
220 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
222 if (args->args_count < 1)
225 desc->offset = args->args[0];
226 if (desc->offset >= uc_priv->gpio_count)
229 if (args->args_count < 2)
232 desc->flags = gpio_flags_xlate(args->args[1]);
237 static int gpio_find_and_xlate(struct gpio_desc *desc,
238 struct ofnode_phandle_args *args)
240 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
243 return ops->xlate(desc->dev, desc, args);
245 return gpio_xlate_offs_flags(desc->dev, desc, args);
248 #if CONFIG_IS_ENABLED(GPIO_HOG)
250 struct gpio_hog_priv {
251 struct gpio_desc gpiod;
254 struct gpio_hog_data {
260 static int gpio_hog_of_to_plat(struct udevice *dev)
262 struct gpio_hog_data *plat = dev_get_plat(dev);
263 const char *nodename;
267 if (dev_read_bool(dev, "input")) {
268 plat->gpiod_flags = GPIOD_IS_IN;
269 } else if (dev_read_bool(dev, "output-high")) {
271 plat->gpiod_flags = GPIOD_IS_OUT;
272 } else if (dev_read_bool(dev, "output-low")) {
273 plat->gpiod_flags = GPIOD_IS_OUT;
275 printf("%s: missing gpio-hog state.\n", __func__);
278 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
280 printf("%s: wrong gpios property, 2 values needed %d\n",
284 nodename = dev_read_string(dev, "line-name");
286 device_set_name(dev, nodename);
291 static int gpio_hog_probe(struct udevice *dev)
293 struct gpio_hog_data *plat = dev_get_plat(dev);
294 struct gpio_hog_priv *priv = dev_get_priv(dev);
297 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
298 plat->val[0], plat->gpiod_flags,
299 plat->val[1], &priv->gpiod);
301 debug("%s: node %s could not get gpio.\n", __func__,
306 if (plat->gpiod_flags == GPIOD_IS_OUT) {
307 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
309 debug("%s: node %s could not set gpio.\n", __func__,
318 int gpio_hog_probe_all(void)
324 for (uclass_first_device(UCLASS_NOP, &dev);
326 uclass_find_next_device(&dev)) {
327 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
328 ret = device_probe(dev);
330 printf("Failed to probe device %s err: %d\n",
340 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
345 gpio_hog_probe_all();
346 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
347 struct gpio_hog_priv *priv = dev_get_priv(dev);
349 *desc = &priv->gpiod;
356 U_BOOT_DRIVER(gpio_hog) = {
359 .of_to_plat = gpio_hog_of_to_plat,
360 .probe = gpio_hog_probe,
361 .priv_auto = sizeof(struct gpio_hog_priv),
362 .plat_auto = sizeof(struct gpio_hog_data),
365 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
371 int dm_gpio_request(struct gpio_desc *desc, const char *label)
373 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
374 struct udevice *dev = desc->dev;
375 struct gpio_dev_priv *uc_priv;
379 uc_priv = dev_get_uclass_priv(dev);
380 if (uc_priv->name[desc->offset])
386 ret = ops->request(dev, desc->offset, label);
392 uc_priv->name[desc->offset] = str;
397 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
399 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
404 vscnprintf(buf, sizeof(buf), fmt, args);
406 return dm_gpio_request(desc, buf);
408 return dm_gpio_request(desc, fmt);
413 * gpio_request() - [COMPAT] Request GPIO
415 * label: Name for the requested GPIO
417 * The label is copied and allocated so the caller does not need to keep
418 * the pointer around.
420 * This function implements the API that's compatible with current
421 * GPIO API used in U-Boot. The request is forwarded to particular
422 * GPIO driver. Returns 0 on success, negative value on error.
424 int gpio_request(unsigned gpio, const char *label)
426 struct gpio_desc desc;
429 ret = gpio_to_device(gpio, &desc);
433 return dm_gpio_request(&desc, label);
437 * gpio_requestf() - [COMPAT] Request GPIO
439 * @fmt: Format string for the requested GPIO
440 * @...: Arguments for the printf() format string
442 * This function implements the API that's compatible with current
443 * GPIO API used in U-Boot. The request is forwarded to particular
444 * GPIO driver. Returns 0 on success, negative value on error.
446 int gpio_requestf(unsigned gpio, const char *fmt, ...)
448 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
453 vscnprintf(buf, sizeof(buf), fmt, args);
455 return gpio_request(gpio, buf);
457 return gpio_request(gpio, fmt);
461 int _dm_gpio_free(struct udevice *dev, uint offset)
463 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
464 struct gpio_dev_priv *uc_priv;
467 uc_priv = dev_get_uclass_priv(dev);
468 if (!uc_priv->name[offset])
471 ret = ops->rfree(dev, offset);
476 free(uc_priv->name[offset]);
477 uc_priv->name[offset] = NULL;
483 * gpio_free() - [COMPAT] Relinquish GPIO
486 * This function implements the API that's compatible with current
487 * GPIO API used in U-Boot. The request is forwarded to particular
488 * GPIO driver. Returns 0 on success, negative value on error.
490 int gpio_free(unsigned gpio)
492 struct gpio_desc desc;
495 ret = gpio_to_device(gpio, &desc);
499 return _dm_gpio_free(desc.dev, desc.offset);
502 static int check_reserved(const struct gpio_desc *desc, const char *func)
504 struct gpio_dev_priv *uc_priv;
506 if (!dm_gpio_is_valid(desc))
509 uc_priv = dev_get_uclass_priv(desc->dev);
510 if (!uc_priv->name[desc->offset]) {
511 printf("%s: %s: error: gpio %s%d not reserved\n",
512 desc->dev->name, func,
513 uc_priv->bank_name ? uc_priv->bank_name : "",
522 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
525 * This function implements the API that's compatible with current
526 * GPIO API used in U-Boot. The request is forwarded to particular
527 * GPIO driver. Returns 0 on success, negative value on error.
529 int gpio_direction_input(unsigned gpio)
531 struct gpio_desc desc;
534 ret = gpio_to_device(gpio, &desc);
538 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
542 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
544 * value: Logical value to be set on the GPIO pin
546 * This function implements the API that's compatible with current
547 * GPIO API used in U-Boot. The request is forwarded to particular
548 * GPIO driver. Returns 0 on success, negative value on error.
550 int gpio_direction_output(unsigned gpio, int value)
552 struct gpio_desc desc;
556 ret = gpio_to_device(gpio, &desc);
560 flags = GPIOD_IS_OUT;
562 flags |= GPIOD_IS_OUT_ACTIVE;
563 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
566 static int _gpio_get_value(const struct gpio_desc *desc)
568 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
571 value = ops->get_value(desc->dev, desc->offset);
573 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
576 int dm_gpio_get_value(const struct gpio_desc *desc)
580 ret = check_reserved(desc, "get_value");
584 return _gpio_get_value(desc);
587 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
589 const struct dm_gpio_ops *ops;
592 ret = check_reserved(desc, "set_value");
596 if (desc->flags & GPIOD_ACTIVE_LOW)
599 /* GPIOD_ are directly managed by driver in set_flags */
600 ops = gpio_get_ops(desc->dev);
601 if (ops->set_flags) {
602 ulong flags = desc->flags;
605 flags |= GPIOD_IS_OUT_ACTIVE;
607 flags &= ~GPIOD_IS_OUT_ACTIVE;
608 return ops->set_flags(desc->dev, desc->offset, flags);
612 * Emulate open drain by not actively driving the line high or
613 * Emulate open source by not actively driving the line low
615 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
616 (desc->flags & GPIOD_OPEN_SOURCE && !value))
617 return ops->direction_input(desc->dev, desc->offset);
618 else if (desc->flags & GPIOD_OPEN_DRAIN ||
619 desc->flags & GPIOD_OPEN_SOURCE)
620 return ops->direction_output(desc->dev, desc->offset, value);
622 ret = ops->set_value(desc->dev, desc->offset, value);
629 /* check dir flags invalid configuration */
630 static int check_dir_flags(ulong flags)
632 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
633 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
638 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
639 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
644 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
645 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
654 * _dm_gpio_set_flags() - Send flags to the driver
656 * This uses the best available method to send the given flags to the driver.
657 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
658 * of GPIOD_IS_OUT_ACTIVE.
660 * @desc: GPIO description
661 * @flags: flags value to set
662 * @return 0 if OK, -ve on error
664 static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
666 struct udevice *dev = desc->dev;
667 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
668 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
671 ret = check_dir_flags(flags);
674 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
676 uc_priv->bank_name ? uc_priv->bank_name : "",
677 desc->offset, flags);
682 /* If active low, invert the output state */
683 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
684 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
685 flags ^= GPIOD_IS_OUT_ACTIVE;
687 /* GPIOD_ are directly managed by driver in set_flags */
688 if (ops->set_flags) {
689 ret = ops->set_flags(dev, desc->offset, flags);
691 if (flags & GPIOD_IS_OUT) {
692 bool value = flags & GPIOD_IS_OUT_ACTIVE;
694 ret = ops->direction_output(dev, desc->offset, value);
695 } else if (flags & GPIOD_IS_IN) {
696 ret = ops->direction_input(dev, desc->offset);
703 int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
708 ret = check_reserved(desc, "set_dir_flags");
712 flags = (desc->flags & ~clr) | set;
714 ret = _dm_gpio_set_flags(desc, flags);
718 /* save the flags also in descriptor */
724 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
726 /* combine the requested flags (for IN/OUT) and the descriptor flags */
727 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
730 int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
736 for (i = 0; i < count; i++) {
737 ret = dm_gpio_clrset_flags(&desc[i], clr, set);
745 int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
747 struct udevice *dev = desc->dev;
749 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
752 ret = check_reserved(desc, "get_flags");
756 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
757 if (ops->get_flags) {
758 ret = ops->get_flags(dev, desc->offset, &flags);
762 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
763 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
764 if (desc->flags & GPIOD_ACTIVE_LOW)
766 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
767 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
769 flags |= GPIOD_IS_OUT_ACTIVE;
772 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
773 flags &= ~GPIOD_IS_OUT_ACTIVE;
774 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
775 flags |= GPIOD_IS_OUT_ACTIVE;
783 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
786 * This function implements the API that's compatible with current
787 * GPIO API used in U-Boot. The request is forwarded to particular
788 * GPIO driver. Returns the value of the GPIO pin, or negative value
791 int gpio_get_value(unsigned gpio)
795 struct gpio_desc desc;
797 ret = gpio_to_device(gpio, &desc);
800 return dm_gpio_get_value(&desc);
804 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
806 * value: Logical value to be set on the GPIO pin.
808 * This function implements the API that's compatible with current
809 * GPIO API used in U-Boot. The request is forwarded to particular
810 * GPIO driver. Returns 0 on success, negative value on error.
812 int gpio_set_value(unsigned gpio, int value)
814 struct gpio_desc desc;
817 ret = gpio_to_device(gpio, &desc);
820 return dm_gpio_set_value(&desc, value);
823 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
825 struct gpio_dev_priv *priv;
827 /* Must be called on an active device */
828 priv = dev_get_uclass_priv(dev);
831 *bit_count = priv->gpio_count;
832 return priv->bank_name;
835 static const char * const gpio_function[GPIOF_COUNT] = {
843 static int get_function(struct udevice *dev, int offset, bool skip_unused,
846 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
847 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
849 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
850 if (!device_active(dev))
852 if (offset < 0 || offset >= uc_priv->gpio_count)
855 *namep = uc_priv->name[offset];
856 if (skip_unused && !uc_priv->name[offset])
858 if (ops->get_function) {
861 ret = ops->get_function(dev, offset);
864 if (ret >= ARRAY_SIZE(gpio_function))
869 return GPIOF_UNKNOWN;
872 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
874 return get_function(dev, offset, true, namep);
877 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
879 return get_function(dev, offset, false, namep);
882 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
884 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
885 struct gpio_dev_priv *priv;
891 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
894 priv = dev_get_uclass_priv(dev);
895 ret = gpio_get_raw_function(dev, offset, NULL);
899 len = snprintf(str, buffsize, "%s%d: %s",
900 priv->bank_name ? priv->bank_name : "",
901 offset, gpio_function[func]);
902 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
903 func == GPIOF_UNUSED) {
907 ret = ops->get_value(dev, offset);
910 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
911 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
921 #if CONFIG_IS_ENABLED(ACPIGEN)
922 int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
924 const struct dm_gpio_ops *ops;
926 memset(gpio, '\0', sizeof(*gpio));
927 if (!dm_gpio_is_valid(desc)) {
928 /* Indicate that the GPIO is not valid */
934 ops = gpio_get_ops(desc->dev);
938 return ops->get_acpi(desc, gpio);
942 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
947 for (i = 0; i < 32; i++) {
948 gpio = gpio_num_array[i];
951 ret = gpio_requestf(gpio, fmt, i);
954 ret = gpio_direction_input(gpio);
963 for (i--; i >= 0; i--)
964 gpio_free(gpio_num_array[i]);
970 * get a number comprised of multiple GPIO values. gpio_num_array points to
971 * the array of gpio pin numbers to scan, terminated by -1.
973 int gpio_get_values_as_int(const int *gpio_list)
976 unsigned bitmask = 1;
981 ((gpio = *gpio_list++) != -1)) {
982 ret = gpio_get_value(gpio);
993 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
995 unsigned bitmask = 1;
999 for (i = 0; i < count; i++) {
1000 ret = dm_gpio_get_value(&desc_list[i]);
1011 int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
1014 static const char tristate[] = "01z";
1021 int vals[NUM_OPTIONS];
1027 * Limit to 19 digits which should be plenty. This avoids overflow of a
1032 for (i = 0; i < NUM_OPTIONS; i++) {
1033 uint flags = GPIOD_IS_IN;
1035 flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
1036 ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
1039 return log_msg_ret("pu", ret);
1041 /* Give the lines time to settle */
1044 ret = dm_gpio_get_values_as_int(desc_list, count);
1046 return log_msg_ret("get1", ret);
1050 log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
1051 for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
1052 uint pd = vals[PULLDOWN] & mask ? 1 : 0;
1053 uint pu = vals[PULLUP] & mask ? 1 : 0;
1057 * Get value with internal pulldown active. If this is 1 then
1058 * there is a stronger external pullup, which we call 1. If not
1061 * If the values differ then the pin is floating so we call
1068 log_debug("%c ", tristate[digit]);
1069 vector = 3 * vector + digit;
1071 log_debug("vector=%d\n", vector);
1077 * gpio_request_tail: common work for requesting a gpio.
1079 * ret: return value from previous work in function which calls
1081 * This seems bogus (why calling this function instead not
1082 * calling it and end caller function instead?).
1083 * Because on error in caller function we want to set some
1084 * default values in gpio desc and have a common error
1085 * debug message, which provides this function.
1086 * nodename: Name of node for which gpio gets requested
1087 * used for gpio label name.
1088 * args: pointer to output arguments structure
1089 * list_name: Name of GPIO list
1090 * used for gpio label name.
1091 * index: gpio index in gpio list
1092 * used for gpio label name.
1093 * desc: pointer to gpio descriptor, filled from this
1095 * flags: gpio flags to use.
1096 * add_index: should index added to gpio label name
1097 * gpio_dev: pointer to gpio device from which the gpio
1098 * will be requested. If NULL try to get the
1099 * gpio device with uclass_get_device_by_ofnode()
1101 * return: In error case this function sets default values in
1102 * gpio descriptor, also emmits a debug message.
1103 * On success it returns 0 else the error code from
1104 * function calls, or the error code passed through
1105 * ret to this function.
1108 static int gpio_request_tail(int ret, const char *nodename,
1109 struct ofnode_phandle_args *args,
1110 const char *list_name, int index,
1111 struct gpio_desc *desc, int flags,
1112 bool add_index, struct udevice *gpio_dev)
1114 gpio_desc_init(desc, gpio_dev, 0);
1119 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1122 debug("%s: uclass_get_device_by_ofnode failed\n",
1127 ret = gpio_find_and_xlate(desc, args);
1129 debug("%s: gpio_find_and_xlate failed\n", __func__);
1132 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
1133 nodename, list_name, index);
1135 debug("%s: dm_gpio_requestf failed\n", __func__);
1139 /* Keep any direction flags provided by the devicetree */
1140 ret = dm_gpio_set_dir_flags(desc,
1141 flags | (desc->flags & GPIOD_MASK_DIR));
1143 debug("%s: dm_gpio_set_dir failed\n", __func__);
1149 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
1150 __func__, nodename, list_name, index, ret);
1154 #if CONFIG_IS_ENABLED(OF_REAL)
1155 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1156 int index, struct gpio_desc *desc,
1157 int flags, bool add_index)
1159 struct ofnode_phandle_args args;
1162 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1165 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1166 index, desc, flags, add_index, NULL);
1169 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
1170 struct gpio_desc *desc, int flags)
1172 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1176 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
1177 struct gpio_desc *desc, int flags)
1179 struct ofnode_phandle_args args;
1183 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1185 node = dev_ofnode(dev);
1186 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1187 index, desc, flags, index > 0, NULL);
1190 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1191 struct gpio_desc *desc, int max_count,
1197 for (count = 0; count < max_count; count++) {
1198 ret = _gpio_request_by_name_nodev(node, list_name, count,
1199 &desc[count], flags, true);
1206 /* We ran out of GPIOs in the list */
1210 gpio_free_list_nodev(desc, count - 1);
1215 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1216 struct gpio_desc *desc, int max_count,
1220 * This isn't ideal since we don't use dev->name in the debug()
1221 * calls in gpio_request_by_name(), but we can do this until
1222 * gpio_request_list_by_name_nodev() can be dropped.
1224 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1228 int gpio_get_list_count(struct udevice *dev, const char *list_name)
1232 ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells",
1235 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1236 __func__, dev->name, list_name, ret);
1241 #endif /* OF_PLATDATA */
1243 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1244 int gpio_request_by_phandle(struct udevice *dev,
1245 const struct phandle_2_arg *cells,
1246 struct gpio_desc *desc, int flags)
1248 struct ofnode_phandle_args args;
1249 struct udevice *gpio_dev;
1250 const int index = 0;
1253 ret = device_get_by_ofplat_idx(cells->idx, &gpio_dev);
1256 args.args[0] = cells->arg[0];
1257 args.args[1] = cells->arg[1];
1259 return gpio_request_tail(ret, NULL, &args, NULL, index, desc, flags,
1260 index > 0, gpio_dev);
1264 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1266 /* For now, we don't do any checking of dev */
1267 return _dm_gpio_free(desc->dev, desc->offset);
1270 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1274 /* For now, we don't do any checking of dev */
1275 for (i = 0; i < count; i++)
1276 dm_gpio_free(dev, &desc[i]);
1281 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1283 return gpio_free_list(NULL, desc, count);
1286 /* We need to renumber the GPIOs when any driver is probed/removed */
1287 static int gpio_renumber(struct udevice *removed_dev)
1289 struct gpio_dev_priv *uc_priv;
1290 struct udevice *dev;
1295 ret = uclass_get(UCLASS_GPIO, &uc);
1299 /* Ensure that we have a base for each bank */
1301 uclass_foreach_dev(dev, uc) {
1302 if (device_active(dev) && dev != removed_dev) {
1303 uc_priv = dev_get_uclass_priv(dev);
1304 uc_priv->gpio_base = base;
1305 base += uc_priv->gpio_count;
1312 int gpio_get_number(const struct gpio_desc *desc)
1314 struct udevice *dev = desc->dev;
1315 struct gpio_dev_priv *uc_priv;
1319 uc_priv = dev_get_uclass_priv(dev);
1321 return uc_priv->gpio_base + desc->offset;
1324 static int gpio_post_probe(struct udevice *dev)
1326 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1328 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1332 return gpio_renumber(NULL);
1335 static int gpio_pre_remove(struct udevice *dev)
1337 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1340 for (i = 0; i < uc_priv->gpio_count; i++) {
1341 if (uc_priv->name[i])
1342 free(uc_priv->name[i]);
1344 free(uc_priv->name);
1346 return gpio_renumber(dev);
1349 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1350 char *list_name, int index, int flags,
1351 int dtflags, struct gpio_desc *desc)
1353 struct ofnode_phandle_args args;
1355 args.node = ofnode_null();
1356 args.args_count = 2;
1357 args.args[0] = index;
1358 args.args[1] = dtflags;
1360 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1364 static void devm_gpiod_release(struct udevice *dev, void *res)
1366 dm_gpio_free(dev, res);
1369 static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1374 struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1375 unsigned int index, int flags)
1378 struct gpio_desc *desc;
1380 static const char suffix[] = "-gpios";
1382 propname = malloc(strlen(id) + sizeof(suffix));
1388 strcpy(propname, id);
1389 strcat(propname, suffix);
1391 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1393 if (unlikely(!desc)) {
1398 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1407 devres_add(dev, desc);
1412 struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1417 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1425 void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1429 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1433 static int gpio_post_bind(struct udevice *dev)
1435 struct udevice *child;
1438 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1439 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1440 static int reloc_done;
1444 ops->request += gd->reloc_off;
1446 ops->rfree += gd->reloc_off;
1447 if (ops->direction_input)
1448 ops->direction_input += gd->reloc_off;
1449 if (ops->direction_output)
1450 ops->direction_output += gd->reloc_off;
1452 ops->get_value += gd->reloc_off;
1454 ops->set_value += gd->reloc_off;
1455 if (ops->get_function)
1456 ops->get_function += gd->reloc_off;
1458 ops->xlate += gd->reloc_off;
1460 ops->set_flags += gd->reloc_off;
1462 ops->get_flags += gd->reloc_off;
1468 if (CONFIG_IS_ENABLED(OF_REAL) && IS_ENABLED(CONFIG_GPIO_HOG)) {
1469 dev_for_each_subnode(node, dev) {
1470 if (ofnode_read_bool(node, "gpio-hog")) {
1471 const char *name = ofnode_get_name(node);
1474 ret = device_bind_driver_to_node(dev,
1486 UCLASS_DRIVER(gpio) = {
1489 .flags = DM_UC_FLAG_SEQ_ALIAS,
1490 .post_probe = gpio_post_probe,
1491 .post_bind = gpio_post_bind,
1492 .pre_remove = gpio_pre_remove,
1493 .per_device_auto = sizeof(struct gpio_dev_priv),