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 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
190 struct ofnode_phandle_args *args)
192 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
194 if (args->args_count < 1)
197 desc->offset = args->args[0];
198 if (desc->offset >= uc_priv->gpio_count)
201 if (args->args_count < 2)
205 if (args->args[1] & GPIO_ACTIVE_LOW)
206 desc->flags |= GPIOD_ACTIVE_LOW;
209 * need to test 2 bits for gpio output binding:
210 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
211 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
213 if (args->args[1] & GPIO_SINGLE_ENDED) {
214 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
215 desc->flags |= GPIOD_OPEN_DRAIN;
217 desc->flags |= GPIOD_OPEN_SOURCE;
220 if (args->args[1] & GPIO_PULL_UP)
221 desc->flags |= GPIOD_PULL_UP;
223 if (args->args[1] & GPIO_PULL_DOWN)
224 desc->flags |= GPIOD_PULL_DOWN;
229 static int gpio_find_and_xlate(struct gpio_desc *desc,
230 struct ofnode_phandle_args *args)
232 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
235 return ops->xlate(desc->dev, desc, args);
237 return gpio_xlate_offs_flags(desc->dev, desc, args);
240 #if CONFIG_IS_ENABLED(GPIO_HOG)
242 struct gpio_hog_priv {
243 struct gpio_desc gpiod;
246 struct gpio_hog_data {
252 static int gpio_hog_of_to_plat(struct udevice *dev)
254 struct gpio_hog_data *plat = dev_get_plat(dev);
255 const char *nodename;
259 if (dev_read_bool(dev, "input")) {
260 plat->gpiod_flags = GPIOD_IS_IN;
261 } else if (dev_read_bool(dev, "output-high")) {
263 plat->gpiod_flags = GPIOD_IS_OUT;
264 } else if (dev_read_bool(dev, "output-low")) {
265 plat->gpiod_flags = GPIOD_IS_OUT;
267 printf("%s: missing gpio-hog state.\n", __func__);
270 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
272 printf("%s: wrong gpios property, 2 values needed %d\n",
276 nodename = dev_read_string(dev, "line-name");
278 device_set_name(dev, nodename);
283 static int gpio_hog_probe(struct udevice *dev)
285 struct gpio_hog_data *plat = dev_get_plat(dev);
286 struct gpio_hog_priv *priv = dev_get_priv(dev);
289 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
290 plat->val[0], plat->gpiod_flags,
291 plat->val[1], &priv->gpiod);
293 debug("%s: node %s could not get gpio.\n", __func__,
298 if (plat->gpiod_flags == GPIOD_IS_OUT) {
299 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
301 debug("%s: node %s could not set gpio.\n", __func__,
310 int gpio_hog_probe_all(void)
316 for (uclass_first_device(UCLASS_NOP, &dev);
318 uclass_find_next_device(&dev)) {
319 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
320 ret = device_probe(dev);
322 printf("Failed to probe device %s err: %d\n",
332 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
337 gpio_hog_probe_all();
338 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
339 struct gpio_hog_priv *priv = dev_get_priv(dev);
341 *desc = &priv->gpiod;
348 U_BOOT_DRIVER(gpio_hog) = {
351 .of_to_plat = gpio_hog_of_to_plat,
352 .probe = gpio_hog_probe,
353 .priv_auto = sizeof(struct gpio_hog_priv),
354 .plat_auto = sizeof(struct gpio_hog_data),
357 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
363 int dm_gpio_request(struct gpio_desc *desc, const char *label)
365 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
366 struct udevice *dev = desc->dev;
367 struct gpio_dev_priv *uc_priv;
371 uc_priv = dev_get_uclass_priv(dev);
372 if (uc_priv->name[desc->offset])
378 ret = ops->request(dev, desc->offset, label);
384 uc_priv->name[desc->offset] = str;
389 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
391 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
396 vscnprintf(buf, sizeof(buf), fmt, args);
398 return dm_gpio_request(desc, buf);
400 return dm_gpio_request(desc, fmt);
405 * gpio_request() - [COMPAT] Request GPIO
407 * label: Name for the requested GPIO
409 * The label is copied and allocated so the caller does not need to keep
410 * the pointer around.
412 * This function implements the API that's compatible with current
413 * GPIO API used in U-Boot. The request is forwarded to particular
414 * GPIO driver. Returns 0 on success, negative value on error.
416 int gpio_request(unsigned gpio, const char *label)
418 struct gpio_desc desc;
421 ret = gpio_to_device(gpio, &desc);
425 return dm_gpio_request(&desc, label);
429 * gpio_requestf() - [COMPAT] Request GPIO
431 * @fmt: Format string for the requested GPIO
432 * @...: Arguments for the printf() format string
434 * This function implements the API that's compatible with current
435 * GPIO API used in U-Boot. The request is forwarded to particular
436 * GPIO driver. Returns 0 on success, negative value on error.
438 int gpio_requestf(unsigned gpio, const char *fmt, ...)
440 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
445 vscnprintf(buf, sizeof(buf), fmt, args);
447 return gpio_request(gpio, buf);
449 return gpio_request(gpio, fmt);
453 int _dm_gpio_free(struct udevice *dev, uint offset)
455 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
456 struct gpio_dev_priv *uc_priv;
459 uc_priv = dev_get_uclass_priv(dev);
460 if (!uc_priv->name[offset])
463 ret = ops->rfree(dev, offset);
468 free(uc_priv->name[offset]);
469 uc_priv->name[offset] = NULL;
475 * gpio_free() - [COMPAT] Relinquish GPIO
478 * This function implements the API that's compatible with current
479 * GPIO API used in U-Boot. The request is forwarded to particular
480 * GPIO driver. Returns 0 on success, negative value on error.
482 int gpio_free(unsigned gpio)
484 struct gpio_desc desc;
487 ret = gpio_to_device(gpio, &desc);
491 return _dm_gpio_free(desc.dev, desc.offset);
494 static int check_reserved(const struct gpio_desc *desc, const char *func)
496 struct gpio_dev_priv *uc_priv;
498 if (!dm_gpio_is_valid(desc))
501 uc_priv = dev_get_uclass_priv(desc->dev);
502 if (!uc_priv->name[desc->offset]) {
503 printf("%s: %s: error: gpio %s%d not reserved\n",
504 desc->dev->name, func,
505 uc_priv->bank_name ? uc_priv->bank_name : "",
514 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
517 * This function implements the API that's compatible with current
518 * GPIO API used in U-Boot. The request is forwarded to particular
519 * GPIO driver. Returns 0 on success, negative value on error.
521 int gpio_direction_input(unsigned gpio)
523 struct gpio_desc desc;
526 ret = gpio_to_device(gpio, &desc);
530 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
534 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
536 * value: Logical value to be set on the GPIO pin
538 * This function implements the API that's compatible with current
539 * GPIO API used in U-Boot. The request is forwarded to particular
540 * GPIO driver. Returns 0 on success, negative value on error.
542 int gpio_direction_output(unsigned gpio, int value)
544 struct gpio_desc desc;
548 ret = gpio_to_device(gpio, &desc);
552 flags = GPIOD_IS_OUT;
554 flags |= GPIOD_IS_OUT_ACTIVE;
555 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
558 static int _gpio_get_value(const struct gpio_desc *desc)
560 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
563 value = ops->get_value(desc->dev, desc->offset);
565 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
568 int dm_gpio_get_value(const struct gpio_desc *desc)
572 ret = check_reserved(desc, "get_value");
576 return _gpio_get_value(desc);
579 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
581 const struct dm_gpio_ops *ops;
584 ret = check_reserved(desc, "set_value");
588 if (desc->flags & GPIOD_ACTIVE_LOW)
591 /* GPIOD_ are directly managed by driver in set_flags */
592 ops = gpio_get_ops(desc->dev);
593 if (ops->set_flags) {
594 ulong flags = desc->flags;
597 flags |= GPIOD_IS_OUT_ACTIVE;
599 flags &= ~GPIOD_IS_OUT_ACTIVE;
600 return ops->set_flags(desc->dev, desc->offset, flags);
604 * Emulate open drain by not actively driving the line high or
605 * Emulate open source by not actively driving the line low
607 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
608 (desc->flags & GPIOD_OPEN_SOURCE && !value))
609 return ops->direction_input(desc->dev, desc->offset);
610 else if (desc->flags & GPIOD_OPEN_DRAIN ||
611 desc->flags & GPIOD_OPEN_SOURCE)
612 return ops->direction_output(desc->dev, desc->offset, value);
614 ret = ops->set_value(desc->dev, desc->offset, value);
621 /* check dir flags invalid configuration */
622 static int check_dir_flags(ulong flags)
624 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
625 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
630 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
631 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
636 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
637 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
646 * _dm_gpio_set_flags() - Send flags to the driver
648 * This uses the best available method to send the given flags to the driver.
649 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
650 * of GPIOD_IS_OUT_ACTIVE.
652 * @desc: GPIO description
653 * @flags: flags value to set
654 * @return 0 if OK, -ve on error
656 static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
658 struct udevice *dev = desc->dev;
659 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
660 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
663 ret = check_dir_flags(flags);
666 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
668 uc_priv->bank_name ? uc_priv->bank_name : "",
669 desc->offset, flags);
674 /* If active low, invert the output state */
675 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
676 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
677 flags ^= GPIOD_IS_OUT_ACTIVE;
679 /* GPIOD_ are directly managed by driver in set_flags */
680 if (ops->set_flags) {
681 ret = ops->set_flags(dev, desc->offset, flags);
683 if (flags & GPIOD_IS_OUT) {
684 bool value = flags & GPIOD_IS_OUT_ACTIVE;
686 ret = ops->direction_output(dev, desc->offset, value);
687 } else if (flags & GPIOD_IS_IN) {
688 ret = ops->direction_input(dev, desc->offset);
695 int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
700 ret = check_reserved(desc, "set_dir_flags");
704 flags = (desc->flags & ~clr) | set;
706 ret = _dm_gpio_set_flags(desc, flags);
710 /* save the flags also in descriptor */
716 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
718 /* combine the requested flags (for IN/OUT) and the descriptor flags */
719 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
722 int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
728 for (i = 0; i < count; i++) {
729 ret = dm_gpio_clrset_flags(&desc[i], clr, set);
737 int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
739 struct udevice *dev = desc->dev;
741 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
744 ret = check_reserved(desc, "get_flags");
748 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
749 if (ops->get_flags) {
750 ret = ops->get_flags(dev, desc->offset, &flags);
754 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
755 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
756 if (desc->flags & GPIOD_ACTIVE_LOW)
758 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
759 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
761 flags |= GPIOD_IS_OUT_ACTIVE;
764 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
765 flags &= ~GPIOD_IS_OUT_ACTIVE;
766 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
767 flags |= GPIOD_IS_OUT_ACTIVE;
775 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
778 * This function implements the API that's compatible with current
779 * GPIO API used in U-Boot. The request is forwarded to particular
780 * GPIO driver. Returns the value of the GPIO pin, or negative value
783 int gpio_get_value(unsigned gpio)
787 struct gpio_desc desc;
789 ret = gpio_to_device(gpio, &desc);
792 return dm_gpio_get_value(&desc);
796 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
798 * value: Logical value to be set on the GPIO pin.
800 * This function implements the API that's compatible with current
801 * GPIO API used in U-Boot. The request is forwarded to particular
802 * GPIO driver. Returns 0 on success, negative value on error.
804 int gpio_set_value(unsigned gpio, int value)
806 struct gpio_desc desc;
809 ret = gpio_to_device(gpio, &desc);
812 return dm_gpio_set_value(&desc, value);
815 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
817 struct gpio_dev_priv *priv;
819 /* Must be called on an active device */
820 priv = dev_get_uclass_priv(dev);
823 *bit_count = priv->gpio_count;
824 return priv->bank_name;
827 static const char * const gpio_function[GPIOF_COUNT] = {
835 static int get_function(struct udevice *dev, int offset, bool skip_unused,
838 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
839 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
841 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
842 if (!device_active(dev))
844 if (offset < 0 || offset >= uc_priv->gpio_count)
847 *namep = uc_priv->name[offset];
848 if (skip_unused && !uc_priv->name[offset])
850 if (ops->get_function) {
853 ret = ops->get_function(dev, offset);
856 if (ret >= ARRAY_SIZE(gpio_function))
861 return GPIOF_UNKNOWN;
864 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
866 return get_function(dev, offset, true, namep);
869 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
871 return get_function(dev, offset, false, namep);
874 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
876 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
877 struct gpio_dev_priv *priv;
883 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
886 priv = dev_get_uclass_priv(dev);
887 ret = gpio_get_raw_function(dev, offset, NULL);
891 len = snprintf(str, buffsize, "%s%d: %s",
892 priv->bank_name ? priv->bank_name : "",
893 offset, gpio_function[func]);
894 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
895 func == GPIOF_UNUSED) {
899 ret = ops->get_value(dev, offset);
902 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
903 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
913 #if CONFIG_IS_ENABLED(ACPIGEN)
914 int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
916 const struct dm_gpio_ops *ops;
918 memset(gpio, '\0', sizeof(*gpio));
919 if (!dm_gpio_is_valid(desc)) {
920 /* Indicate that the GPIO is not valid */
926 ops = gpio_get_ops(desc->dev);
930 return ops->get_acpi(desc, gpio);
934 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
939 for (i = 0; i < 32; i++) {
940 gpio = gpio_num_array[i];
943 ret = gpio_requestf(gpio, fmt, i);
946 ret = gpio_direction_input(gpio);
955 for (i--; i >= 0; i--)
956 gpio_free(gpio_num_array[i]);
962 * get a number comprised of multiple GPIO values. gpio_num_array points to
963 * the array of gpio pin numbers to scan, terminated by -1.
965 int gpio_get_values_as_int(const int *gpio_list)
968 unsigned bitmask = 1;
973 ((gpio = *gpio_list++) != -1)) {
974 ret = gpio_get_value(gpio);
985 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
987 unsigned bitmask = 1;
991 for (i = 0; i < count; i++) {
992 ret = dm_gpio_get_value(&desc_list[i]);
1003 int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
1006 static const char tristate[] = "01z";
1013 int vals[NUM_OPTIONS];
1019 * Limit to 19 digits which should be plenty. This avoids overflow of a
1024 for (i = 0; i < NUM_OPTIONS; i++) {
1025 uint flags = GPIOD_IS_IN;
1027 flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
1028 ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
1031 return log_msg_ret("pu", ret);
1033 /* Give the lines time to settle */
1036 ret = dm_gpio_get_values_as_int(desc_list, count);
1038 return log_msg_ret("get1", ret);
1042 log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
1043 for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
1044 uint pd = vals[PULLDOWN] & mask ? 1 : 0;
1045 uint pu = vals[PULLUP] & mask ? 1 : 0;
1049 * Get value with internal pulldown active. If this is 1 then
1050 * there is a stronger external pullup, which we call 1. If not
1053 * If the values differ then the pin is floating so we call
1060 log_debug("%c ", tristate[digit]);
1061 vector = 3 * vector + digit;
1063 log_debug("vector=%d\n", vector);
1069 * gpio_request_tail: common work for requesting a gpio.
1071 * ret: return value from previous work in function which calls
1073 * This seems bogus (why calling this function instead not
1074 * calling it and end caller function instead?).
1075 * Because on error in caller function we want to set some
1076 * default values in gpio desc and have a common error
1077 * debug message, which provides this function.
1078 * nodename: Name of node for which gpio gets requested
1079 * used for gpio label name.
1080 * args: pointer to output arguments structure
1081 * list_name: Name of GPIO list
1082 * used for gpio label name.
1083 * index: gpio index in gpio list
1084 * used for gpio label name.
1085 * desc: pointer to gpio descriptor, filled from this
1087 * flags: gpio flags to use.
1088 * add_index: should index added to gpio label name
1089 * gpio_dev: pointer to gpio device from which the gpio
1090 * will be requested. If NULL try to get the
1091 * gpio device with uclass_get_device_by_ofnode()
1093 * return: In error case this function sets default values in
1094 * gpio descriptor, also emmits a debug message.
1095 * On success it returns 0 else the error code from
1096 * function calls, or the error code passed through
1097 * ret to this function.
1100 static int gpio_request_tail(int ret, const char *nodename,
1101 struct ofnode_phandle_args *args,
1102 const char *list_name, int index,
1103 struct gpio_desc *desc, int flags,
1104 bool add_index, struct udevice *gpio_dev)
1106 gpio_desc_init(desc, gpio_dev, 0);
1111 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1114 debug("%s: uclass_get_device_by_ofnode failed\n",
1119 ret = gpio_find_and_xlate(desc, args);
1121 debug("%s: gpio_find_and_xlate failed\n", __func__);
1124 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
1125 nodename, list_name, index);
1127 debug("%s: dm_gpio_requestf failed\n", __func__);
1131 /* Keep any direction flags provided by the devicetree */
1132 ret = dm_gpio_set_dir_flags(desc,
1133 flags | (desc->flags & GPIOD_MASK_DIR));
1135 debug("%s: dm_gpio_set_dir failed\n", __func__);
1141 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
1142 __func__, nodename, list_name, index, ret);
1146 #if CONFIG_IS_ENABLED(OF_REAL)
1147 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1148 int index, struct gpio_desc *desc,
1149 int flags, bool add_index)
1151 struct ofnode_phandle_args args;
1154 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1157 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1158 index, desc, flags, add_index, NULL);
1161 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
1162 struct gpio_desc *desc, int flags)
1164 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1168 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
1169 struct gpio_desc *desc, int flags)
1171 struct ofnode_phandle_args args;
1175 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1177 node = dev_ofnode(dev);
1178 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1179 index, desc, flags, index > 0, NULL);
1182 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1183 struct gpio_desc *desc, int max_count,
1189 for (count = 0; count < max_count; count++) {
1190 ret = _gpio_request_by_name_nodev(node, list_name, count,
1191 &desc[count], flags, true);
1198 /* We ran out of GPIOs in the list */
1202 gpio_free_list_nodev(desc, count - 1);
1207 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1208 struct gpio_desc *desc, int max_count,
1212 * This isn't ideal since we don't use dev->name in the debug()
1213 * calls in gpio_request_by_name(), but we can do this until
1214 * gpio_request_list_by_name_nodev() can be dropped.
1216 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1220 int gpio_get_list_count(struct udevice *dev, const char *list_name)
1224 ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells",
1227 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1228 __func__, dev->name, list_name, ret);
1233 #endif /* OF_PLATDATA */
1235 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1236 int gpio_request_by_phandle(struct udevice *dev,
1237 const struct phandle_2_arg *cells,
1238 struct gpio_desc *desc, int flags)
1240 struct ofnode_phandle_args args;
1241 struct udevice *gpio_dev;
1242 const int index = 0;
1245 ret = device_get_by_ofplat_idx(cells->idx, &gpio_dev);
1248 args.args[0] = cells->arg[0];
1249 args.args[1] = cells->arg[1];
1251 return gpio_request_tail(ret, NULL, &args, NULL, index, desc, flags,
1252 index > 0, gpio_dev);
1256 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1258 /* For now, we don't do any checking of dev */
1259 return _dm_gpio_free(desc->dev, desc->offset);
1262 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1266 /* For now, we don't do any checking of dev */
1267 for (i = 0; i < count; i++)
1268 dm_gpio_free(dev, &desc[i]);
1273 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1275 return gpio_free_list(NULL, desc, count);
1278 /* We need to renumber the GPIOs when any driver is probed/removed */
1279 static int gpio_renumber(struct udevice *removed_dev)
1281 struct gpio_dev_priv *uc_priv;
1282 struct udevice *dev;
1287 ret = uclass_get(UCLASS_GPIO, &uc);
1291 /* Ensure that we have a base for each bank */
1293 uclass_foreach_dev(dev, uc) {
1294 if (device_active(dev) && dev != removed_dev) {
1295 uc_priv = dev_get_uclass_priv(dev);
1296 uc_priv->gpio_base = base;
1297 base += uc_priv->gpio_count;
1304 int gpio_get_number(const struct gpio_desc *desc)
1306 struct udevice *dev = desc->dev;
1307 struct gpio_dev_priv *uc_priv;
1311 uc_priv = dev_get_uclass_priv(dev);
1313 return uc_priv->gpio_base + desc->offset;
1316 static int gpio_post_probe(struct udevice *dev)
1318 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1320 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1324 return gpio_renumber(NULL);
1327 static int gpio_pre_remove(struct udevice *dev)
1329 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1332 for (i = 0; i < uc_priv->gpio_count; i++) {
1333 if (uc_priv->name[i])
1334 free(uc_priv->name[i]);
1336 free(uc_priv->name);
1338 return gpio_renumber(dev);
1341 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1342 char *list_name, int index, int flags,
1343 int dtflags, struct gpio_desc *desc)
1345 struct ofnode_phandle_args args;
1347 args.node = ofnode_null();
1348 args.args_count = 2;
1349 args.args[0] = index;
1350 args.args[1] = dtflags;
1352 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1356 static void devm_gpiod_release(struct udevice *dev, void *res)
1358 dm_gpio_free(dev, res);
1361 static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1366 struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1367 unsigned int index, int flags)
1370 struct gpio_desc *desc;
1372 static const char suffix[] = "-gpios";
1374 propname = malloc(strlen(id) + sizeof(suffix));
1380 strcpy(propname, id);
1381 strcat(propname, suffix);
1383 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1385 if (unlikely(!desc)) {
1390 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1399 devres_add(dev, desc);
1404 struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1409 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1417 void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1421 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1425 static int gpio_post_bind(struct udevice *dev)
1427 struct udevice *child;
1430 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1431 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1432 static int reloc_done;
1436 ops->request += gd->reloc_off;
1438 ops->rfree += gd->reloc_off;
1439 if (ops->direction_input)
1440 ops->direction_input += gd->reloc_off;
1441 if (ops->direction_output)
1442 ops->direction_output += gd->reloc_off;
1444 ops->get_value += gd->reloc_off;
1446 ops->set_value += gd->reloc_off;
1447 if (ops->get_function)
1448 ops->get_function += gd->reloc_off;
1450 ops->xlate += gd->reloc_off;
1452 ops->set_flags += gd->reloc_off;
1454 ops->get_flags += gd->reloc_off;
1460 if (CONFIG_IS_ENABLED(OF_REAL) && IS_ENABLED(CONFIG_GPIO_HOG)) {
1461 dev_for_each_subnode(node, dev) {
1462 if (ofnode_read_bool(node, "gpio-hog")) {
1463 const char *name = ofnode_get_name(node);
1466 ret = device_bind_driver_to_node(dev,
1478 UCLASS_DRIVER(gpio) = {
1481 .flags = DM_UC_FLAG_SEQ_ALIAS,
1482 .post_probe = gpio_post_probe,
1483 .post_bind = gpio_post_bind,
1484 .pre_remove = gpio_pre_remove,
1485 .per_device_auto = sizeof(struct gpio_dev_priv),