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;
63 for (uclass_first_device(UCLASS_GPIO, &dev);
65 uclass_next_device(&dev)) {
66 uc_priv = dev_get_uclass_priv(dev);
67 if (gpio >= uc_priv->gpio_base &&
68 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
69 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
78 #if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
80 * dm_gpio_lookup_label() - look for name in gpio device
82 * search in uc_priv, if there is a gpio with labelname same
85 * @name: name which is searched
86 * @uc_priv: gpio_dev_priv pointer.
87 * @offset: gpio offset within the device
88 * @return: 0 if found, -ENOENT if not.
90 static int dm_gpio_lookup_label(const char *name,
91 struct gpio_dev_priv *uc_priv, ulong *offset)
96 for (i = 0; i < uc_priv->gpio_count; i++) {
97 if (!uc_priv->name[i])
99 if (!strcmp(name, uc_priv->name[i])) {
108 dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
115 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
117 struct gpio_dev_priv *uc_priv = NULL;
122 numeric = isdigit(*name) ? dectoul(name, NULL) : -1;
123 for (uclass_first_device(UCLASS_GPIO, &dev);
125 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))
140 if (offset < uc_priv->gpio_count)
145 * if we did not found a gpio through its bank
146 * name, we search for a valid gpio label.
148 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
155 gpio_desc_init(desc, dev, offset);
160 int gpio_lookup_name(const char *name, struct udevice **devp,
161 unsigned int *offsetp, unsigned int *gpiop)
163 struct gpio_desc desc;
168 ret = dm_gpio_lookup_name(name, &desc);
175 *offsetp = desc.offset;
177 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
179 *gpiop = uc_priv->gpio_base + desc.offset;
185 unsigned long gpio_flags_xlate(uint32_t arg)
187 unsigned long flags = 0;
189 if (arg & GPIO_ACTIVE_LOW)
190 flags |= GPIOD_ACTIVE_LOW;
193 * need to test 2 bits for gpio output binding:
194 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
195 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
197 if (arg & GPIO_SINGLE_ENDED) {
198 if (arg & GPIO_LINE_OPEN_DRAIN)
199 flags |= GPIOD_OPEN_DRAIN;
201 flags |= GPIOD_OPEN_SOURCE;
204 if (arg & GPIO_PULL_UP)
205 flags |= GPIOD_PULL_UP;
207 if (arg & GPIO_PULL_DOWN)
208 flags |= GPIOD_PULL_DOWN;
213 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
214 struct ofnode_phandle_args *args)
216 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
218 if (args->args_count < 1)
221 desc->offset = args->args[0];
222 if (desc->offset >= uc_priv->gpio_count)
225 if (args->args_count < 2)
228 desc->flags = gpio_flags_xlate(args->args[1]);
233 static int gpio_find_and_xlate(struct gpio_desc *desc,
234 struct ofnode_phandle_args *args)
236 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
239 return ops->xlate(desc->dev, desc, args);
241 return gpio_xlate_offs_flags(desc->dev, desc, args);
244 #if CONFIG_IS_ENABLED(GPIO_HOG)
246 struct gpio_hog_priv {
247 struct gpio_desc gpiod;
250 struct gpio_hog_data {
256 static int gpio_hog_of_to_plat(struct udevice *dev)
258 struct gpio_hog_data *plat = dev_get_plat(dev);
259 const char *nodename;
263 if (dev_read_bool(dev, "input")) {
264 plat->gpiod_flags = GPIOD_IS_IN;
265 } else if (dev_read_bool(dev, "output-high")) {
267 plat->gpiod_flags = GPIOD_IS_OUT;
268 } else if (dev_read_bool(dev, "output-low")) {
269 plat->gpiod_flags = GPIOD_IS_OUT;
271 printf("%s: missing gpio-hog state.\n", __func__);
274 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
276 printf("%s: wrong gpios property, 2 values needed %d\n",
280 nodename = dev_read_string(dev, "line-name");
282 device_set_name(dev, nodename);
287 static int gpio_hog_probe(struct udevice *dev)
289 struct gpio_hog_data *plat = dev_get_plat(dev);
290 struct gpio_hog_priv *priv = dev_get_priv(dev);
293 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
294 plat->val[0], plat->gpiod_flags,
295 plat->val[1], &priv->gpiod);
297 debug("%s: node %s could not get gpio.\n", __func__,
302 if (plat->gpiod_flags == GPIOD_IS_OUT) {
303 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
305 debug("%s: node %s could not set gpio.\n", __func__,
314 int gpio_hog_probe_all(void)
320 for (uclass_first_device(UCLASS_NOP, &dev);
322 uclass_find_next_device(&dev)) {
323 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
324 ret = device_probe(dev);
326 printf("Failed to probe device %s err: %d\n",
336 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
341 gpio_hog_probe_all();
342 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
343 struct gpio_hog_priv *priv = dev_get_priv(dev);
345 *desc = &priv->gpiod;
352 U_BOOT_DRIVER(gpio_hog) = {
355 .of_to_plat = gpio_hog_of_to_plat,
356 .probe = gpio_hog_probe,
357 .priv_auto = sizeof(struct gpio_hog_priv),
358 .plat_auto = sizeof(struct gpio_hog_data),
361 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
367 int dm_gpio_request(struct gpio_desc *desc, const char *label)
369 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
370 struct udevice *dev = desc->dev;
371 struct gpio_dev_priv *uc_priv;
375 uc_priv = dev_get_uclass_priv(dev);
376 if (uc_priv->name[desc->offset])
382 ret = ops->request(dev, desc->offset, label);
388 uc_priv->name[desc->offset] = str;
393 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
395 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
400 vscnprintf(buf, sizeof(buf), fmt, args);
402 return dm_gpio_request(desc, buf);
404 return dm_gpio_request(desc, fmt);
409 * gpio_request() - [COMPAT] Request GPIO
411 * label: Name for the requested GPIO
413 * The label is copied and allocated so the caller does not need to keep
414 * the pointer around.
416 * This function implements the API that's compatible with current
417 * GPIO API used in U-Boot. The request is forwarded to particular
418 * GPIO driver. Returns 0 on success, negative value on error.
420 int gpio_request(unsigned gpio, const char *label)
422 struct gpio_desc desc;
425 ret = gpio_to_device(gpio, &desc);
429 return dm_gpio_request(&desc, label);
433 * gpio_requestf() - [COMPAT] Request GPIO
435 * @fmt: Format string for the requested GPIO
436 * @...: Arguments for the printf() format string
438 * This function implements the API that's compatible with current
439 * GPIO API used in U-Boot. The request is forwarded to particular
440 * GPIO driver. Returns 0 on success, negative value on error.
442 int gpio_requestf(unsigned gpio, const char *fmt, ...)
444 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
449 vscnprintf(buf, sizeof(buf), fmt, args);
451 return gpio_request(gpio, buf);
453 return gpio_request(gpio, fmt);
457 int _dm_gpio_free(struct udevice *dev, uint offset)
459 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
460 struct gpio_dev_priv *uc_priv;
463 uc_priv = dev_get_uclass_priv(dev);
464 if (!uc_priv->name[offset])
467 ret = ops->rfree(dev, offset);
472 free(uc_priv->name[offset]);
473 uc_priv->name[offset] = NULL;
479 * gpio_free() - [COMPAT] Relinquish GPIO
482 * This function implements the API that's compatible with current
483 * GPIO API used in U-Boot. The request is forwarded to particular
484 * GPIO driver. Returns 0 on success, negative value on error.
486 int gpio_free(unsigned gpio)
488 struct gpio_desc desc;
491 ret = gpio_to_device(gpio, &desc);
495 return _dm_gpio_free(desc.dev, desc.offset);
498 static int check_reserved(const struct gpio_desc *desc, const char *func)
500 struct gpio_dev_priv *uc_priv;
502 if (!dm_gpio_is_valid(desc))
505 uc_priv = dev_get_uclass_priv(desc->dev);
506 if (!uc_priv->name[desc->offset]) {
507 printf("%s: %s: error: gpio %s%d not reserved\n",
508 desc->dev->name, func,
509 uc_priv->bank_name ? uc_priv->bank_name : "",
518 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
521 * This function implements the API that's compatible with current
522 * GPIO API used in U-Boot. The request is forwarded to particular
523 * GPIO driver. Returns 0 on success, negative value on error.
525 int gpio_direction_input(unsigned gpio)
527 struct gpio_desc desc;
530 ret = gpio_to_device(gpio, &desc);
534 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
538 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
540 * value: Logical value to be set on the GPIO pin
542 * This function implements the API that's compatible with current
543 * GPIO API used in U-Boot. The request is forwarded to particular
544 * GPIO driver. Returns 0 on success, negative value on error.
546 int gpio_direction_output(unsigned gpio, int value)
548 struct gpio_desc desc;
552 ret = gpio_to_device(gpio, &desc);
556 flags = GPIOD_IS_OUT;
558 flags |= GPIOD_IS_OUT_ACTIVE;
559 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
562 static int _gpio_get_value(const struct gpio_desc *desc)
564 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
567 value = ops->get_value(desc->dev, desc->offset);
569 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
572 int dm_gpio_get_value(const struct gpio_desc *desc)
576 ret = check_reserved(desc, "get_value");
580 return _gpio_get_value(desc);
583 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
585 const struct dm_gpio_ops *ops;
588 ret = check_reserved(desc, "set_value");
592 if (desc->flags & GPIOD_ACTIVE_LOW)
595 /* GPIOD_ are directly managed by driver in set_flags */
596 ops = gpio_get_ops(desc->dev);
597 if (ops->set_flags) {
598 ulong flags = desc->flags;
601 flags |= GPIOD_IS_OUT_ACTIVE;
603 flags &= ~GPIOD_IS_OUT_ACTIVE;
604 return ops->set_flags(desc->dev, desc->offset, flags);
608 * Emulate open drain by not actively driving the line high or
609 * Emulate open source by not actively driving the line low
611 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
612 (desc->flags & GPIOD_OPEN_SOURCE && !value))
613 return ops->direction_input(desc->dev, desc->offset);
614 else if (desc->flags & GPIOD_OPEN_DRAIN ||
615 desc->flags & GPIOD_OPEN_SOURCE)
616 return ops->direction_output(desc->dev, desc->offset, value);
618 ret = ops->set_value(desc->dev, desc->offset, value);
625 /* check dir flags invalid configuration */
626 static int check_dir_flags(ulong flags)
628 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
629 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
634 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
635 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
640 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
641 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
650 * _dm_gpio_set_flags() - Send flags to the driver
652 * This uses the best available method to send the given flags to the driver.
653 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
654 * of GPIOD_IS_OUT_ACTIVE.
656 * @desc: GPIO description
657 * @flags: flags value to set
658 * Return: 0 if OK, -ve on error
660 static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
662 struct udevice *dev = desc->dev;
663 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
664 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
667 ret = check_dir_flags(flags);
670 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
672 uc_priv->bank_name ? uc_priv->bank_name : "",
673 desc->offset, flags);
678 /* If active low, invert the output state */
679 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
680 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
681 flags ^= GPIOD_IS_OUT_ACTIVE;
683 /* GPIOD_ are directly managed by driver in set_flags */
684 if (ops->set_flags) {
685 ret = ops->set_flags(dev, desc->offset, flags);
687 if (flags & GPIOD_IS_OUT) {
688 bool value = flags & GPIOD_IS_OUT_ACTIVE;
690 ret = ops->direction_output(dev, desc->offset, value);
691 } else if (flags & GPIOD_IS_IN) {
692 ret = ops->direction_input(dev, desc->offset);
699 int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
704 ret = check_reserved(desc, "set_dir_flags");
708 flags = (desc->flags & ~clr) | set;
710 ret = _dm_gpio_set_flags(desc, flags);
714 /* save the flags also in descriptor */
720 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
722 /* combine the requested flags (for IN/OUT) and the descriptor flags */
723 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
726 int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
732 for (i = 0; i < count; i++) {
733 ret = dm_gpio_clrset_flags(&desc[i], clr, set);
741 int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
743 struct udevice *dev = desc->dev;
745 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
748 ret = check_reserved(desc, "get_flags");
752 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
753 if (ops->get_flags) {
754 ret = ops->get_flags(dev, desc->offset, &flags);
758 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
759 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
760 if (desc->flags & GPIOD_ACTIVE_LOW)
762 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
763 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
765 flags |= GPIOD_IS_OUT_ACTIVE;
768 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
769 flags &= ~GPIOD_IS_OUT_ACTIVE;
770 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
771 flags |= GPIOD_IS_OUT_ACTIVE;
779 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
782 * This function implements the API that's compatible with current
783 * GPIO API used in U-Boot. The request is forwarded to particular
784 * GPIO driver. Returns the value of the GPIO pin, or negative value
787 int gpio_get_value(unsigned gpio)
791 struct gpio_desc desc;
793 ret = gpio_to_device(gpio, &desc);
796 return dm_gpio_get_value(&desc);
800 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
802 * value: Logical value to be set on the GPIO pin.
804 * This function implements the API that's compatible with current
805 * GPIO API used in U-Boot. The request is forwarded to particular
806 * GPIO driver. Returns 0 on success, negative value on error.
808 int gpio_set_value(unsigned gpio, int value)
810 struct gpio_desc desc;
813 ret = gpio_to_device(gpio, &desc);
816 return dm_gpio_set_value(&desc, value);
819 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
821 struct gpio_dev_priv *priv;
823 /* Must be called on an active device */
824 priv = dev_get_uclass_priv(dev);
827 *bit_count = priv->gpio_count;
828 return priv->bank_name;
831 static const char * const gpio_function[GPIOF_COUNT] = {
839 static int get_function(struct udevice *dev, int offset, bool skip_unused,
842 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
843 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
845 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
846 if (!device_active(dev))
848 if (offset < 0 || offset >= uc_priv->gpio_count)
851 *namep = uc_priv->name[offset];
852 if (skip_unused && !uc_priv->name[offset])
854 if (ops->get_function) {
857 ret = ops->get_function(dev, offset);
860 if (ret >= ARRAY_SIZE(gpio_function))
865 return GPIOF_UNKNOWN;
868 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
870 return get_function(dev, offset, true, namep);
873 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
875 return get_function(dev, offset, false, namep);
878 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
880 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
881 struct gpio_dev_priv *priv;
889 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
892 priv = dev_get_uclass_priv(dev);
893 ret = gpio_get_raw_function(dev, offset, &label);
897 len = snprintf(str, buffsize, "%s%d: %s",
898 priv->bank_name ? priv->bank_name : "",
899 offset, gpio_function[func]);
903 snprintf(str + len, buffsize - len, " %s", label ? label : "");
908 ret = ops->get_value(dev, offset);
911 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
912 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
923 #if CONFIG_IS_ENABLED(ACPIGEN)
924 int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
926 const struct dm_gpio_ops *ops;
928 memset(gpio, '\0', sizeof(*gpio));
929 if (!dm_gpio_is_valid(desc)) {
930 /* Indicate that the GPIO is not valid */
936 ops = gpio_get_ops(desc->dev);
940 return ops->get_acpi(desc, gpio);
944 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
949 for (i = 0; i < 32; i++) {
950 gpio = gpio_num_array[i];
953 ret = gpio_requestf(gpio, fmt, i);
956 ret = gpio_direction_input(gpio);
965 for (i--; i >= 0; i--)
966 gpio_free(gpio_num_array[i]);
972 * get a number comprised of multiple GPIO values. gpio_num_array points to
973 * the array of gpio pin numbers to scan, terminated by -1.
975 int gpio_get_values_as_int(const int *gpio_list)
978 unsigned bitmask = 1;
983 ((gpio = *gpio_list++) != -1)) {
984 ret = gpio_get_value(gpio);
995 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
997 unsigned bitmask = 1;
1001 for (i = 0; i < count; i++) {
1002 ret = dm_gpio_get_value(&desc_list[i]);
1013 int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
1016 static const char tristate[] = "01z";
1023 int vals[NUM_OPTIONS];
1029 * Limit to 19 digits which should be plenty. This avoids overflow of a
1034 for (i = 0; i < NUM_OPTIONS; i++) {
1035 uint flags = GPIOD_IS_IN;
1037 flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
1038 ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
1041 return log_msg_ret("pu", ret);
1043 /* Give the lines time to settle */
1046 ret = dm_gpio_get_values_as_int(desc_list, count);
1048 return log_msg_ret("get1", ret);
1052 log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
1053 for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
1054 uint pd = vals[PULLDOWN] & mask ? 1 : 0;
1055 uint pu = vals[PULLUP] & mask ? 1 : 0;
1059 * Get value with internal pulldown active. If this is 1 then
1060 * there is a stronger external pullup, which we call 1. If not
1063 * If the values differ then the pin is floating so we call
1070 log_debug("%c ", tristate[digit]);
1071 vector = 3 * vector + digit;
1073 log_debug("vector=%d\n", vector);
1079 * gpio_request_tail: common work for requesting a gpio.
1081 * ret: return value from previous work in function which calls
1083 * This seems bogus (why calling this function instead not
1084 * calling it and end caller function instead?).
1085 * Because on error in caller function we want to set some
1086 * default values in gpio desc and have a common error
1087 * debug message, which provides this function.
1088 * nodename: Name of node for which gpio gets requested
1089 * used for gpio label name.
1090 * args: pointer to output arguments structure
1091 * list_name: Name of GPIO list
1092 * used for gpio label name.
1093 * index: gpio index in gpio list
1094 * used for gpio label name.
1095 * desc: pointer to gpio descriptor, filled from this
1097 * flags: gpio flags to use.
1098 * add_index: should index added to gpio label name
1099 * gpio_dev: pointer to gpio device from which the gpio
1100 * will be requested. If NULL try to get the
1101 * gpio device with uclass_get_device_by_ofnode()
1103 * return: In error case this function sets default values in
1104 * gpio descriptor, also emmits a debug message.
1105 * On success it returns 0 else the error code from
1106 * function calls, or the error code passed through
1107 * ret to this function.
1110 static int gpio_request_tail(int ret, const char *nodename,
1111 struct ofnode_phandle_args *args,
1112 const char *list_name, int index,
1113 struct gpio_desc *desc, int flags,
1114 bool add_index, struct udevice *gpio_dev)
1116 gpio_desc_init(desc, gpio_dev, 0);
1121 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1124 debug("%s: uclass_get_device_by_ofnode failed\n",
1129 ret = gpio_find_and_xlate(desc, args);
1131 debug("%s: gpio_find_and_xlate failed\n", __func__);
1134 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
1135 nodename, list_name, index);
1137 debug("%s: dm_gpio_requestf failed\n", __func__);
1141 /* Keep any direction flags provided by the devicetree */
1142 ret = dm_gpio_set_dir_flags(desc,
1143 flags | (desc->flags & GPIOD_MASK_DIR));
1145 debug("%s: dm_gpio_set_dir failed\n", __func__);
1151 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
1152 __func__, nodename, list_name, index, ret);
1156 #if CONFIG_IS_ENABLED(OF_REAL)
1157 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1158 int index, struct gpio_desc *desc,
1159 int flags, bool add_index)
1161 struct ofnode_phandle_args args;
1164 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1167 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1168 index, desc, flags, add_index, NULL);
1171 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
1172 struct gpio_desc *desc, int flags)
1174 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1178 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
1179 struct gpio_desc *desc, int flags)
1181 struct ofnode_phandle_args args;
1185 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1187 node = dev_ofnode(dev);
1188 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1189 index, desc, flags, index > 0, NULL);
1192 int gpio_request_by_line_name(struct udevice *dev, const char *line_name,
1193 struct gpio_desc *desc, int flags)
1197 ret = dev_read_stringlist_search(dev, "gpio-line-names", line_name);
1205 ret = dm_gpio_request(desc, line_name);
1207 debug("%s: dm_gpio_requestf failed\n", __func__);
1211 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
1213 debug("%s: dm_gpio_set_dir failed\n", __func__);
1218 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1219 struct gpio_desc *desc, int max_count,
1225 for (count = 0; count < max_count; count++) {
1226 ret = _gpio_request_by_name_nodev(node, list_name, count,
1227 &desc[count], flags, true);
1234 /* We ran out of GPIOs in the list */
1238 gpio_free_list_nodev(desc, count - 1);
1243 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1244 struct gpio_desc *desc, int max_count,
1248 * This isn't ideal since we don't use dev->name in the debug()
1249 * calls in gpio_request_by_name(), but we can do this until
1250 * gpio_request_list_by_name_nodev() can be dropped.
1252 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1256 int gpio_get_list_count(struct udevice *dev, const char *list_name)
1260 ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells",
1263 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1264 __func__, dev->name, list_name, ret);
1269 #endif /* OF_PLATDATA */
1271 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1272 int gpio_request_by_phandle(struct udevice *dev,
1273 const struct phandle_2_arg *cells,
1274 struct gpio_desc *desc, int flags)
1276 struct ofnode_phandle_args args;
1277 struct udevice *gpio_dev;
1278 const int index = 0;
1281 ret = device_get_by_ofplat_idx(cells->idx, &gpio_dev);
1284 args.args[0] = cells->arg[0];
1285 args.args[1] = cells->arg[1];
1287 return gpio_request_tail(ret, NULL, &args, NULL, index, desc, flags,
1288 index > 0, gpio_dev);
1292 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1294 /* For now, we don't do any checking of dev */
1295 return _dm_gpio_free(desc->dev, desc->offset);
1298 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1302 /* For now, we don't do any checking of dev */
1303 for (i = 0; i < count; i++)
1304 dm_gpio_free(dev, &desc[i]);
1309 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1311 return gpio_free_list(NULL, desc, count);
1314 /* We need to renumber the GPIOs when any driver is probed/removed */
1315 static int gpio_renumber(struct udevice *removed_dev)
1317 struct gpio_dev_priv *uc_priv;
1318 struct udevice *dev;
1323 ret = uclass_get(UCLASS_GPIO, &uc);
1327 /* Ensure that we have a base for each bank */
1329 uclass_foreach_dev(dev, uc) {
1330 if (device_active(dev) && dev != removed_dev) {
1331 uc_priv = dev_get_uclass_priv(dev);
1332 uc_priv->gpio_base = base;
1333 base += uc_priv->gpio_count;
1340 int gpio_get_number(const struct gpio_desc *desc)
1342 struct udevice *dev = desc->dev;
1343 struct gpio_dev_priv *uc_priv;
1347 uc_priv = dev_get_uclass_priv(dev);
1349 return uc_priv->gpio_base + desc->offset;
1352 static int gpio_post_probe(struct udevice *dev)
1354 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1356 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1360 return gpio_renumber(NULL);
1363 static int gpio_pre_remove(struct udevice *dev)
1365 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1368 for (i = 0; i < uc_priv->gpio_count; i++) {
1369 if (uc_priv->name[i])
1370 free(uc_priv->name[i]);
1372 free(uc_priv->name);
1374 return gpio_renumber(dev);
1377 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1378 char *list_name, int index, int flags,
1379 int dtflags, struct gpio_desc *desc)
1381 struct ofnode_phandle_args args;
1383 args.node = ofnode_null();
1384 args.args_count = 2;
1385 args.args[0] = index;
1386 args.args[1] = dtflags;
1388 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1392 static void devm_gpiod_release(struct udevice *dev, void *res)
1394 dm_gpio_free(dev, res);
1397 static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1402 struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1403 unsigned int index, int flags)
1406 struct gpio_desc *desc;
1408 static const char suffix[] = "-gpios";
1410 propname = malloc(strlen(id) + sizeof(suffix));
1416 strcpy(propname, id);
1417 strcat(propname, suffix);
1419 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1421 if (unlikely(!desc)) {
1426 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1435 devres_add(dev, desc);
1440 struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1445 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1453 void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1457 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1461 static int gpio_post_bind(struct udevice *dev)
1463 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1464 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1465 static int reloc_done;
1469 ops->request += gd->reloc_off;
1471 ops->rfree += gd->reloc_off;
1472 if (ops->direction_input)
1473 ops->direction_input += gd->reloc_off;
1474 if (ops->direction_output)
1475 ops->direction_output += gd->reloc_off;
1477 ops->get_value += gd->reloc_off;
1479 ops->set_value += gd->reloc_off;
1480 if (ops->get_function)
1481 ops->get_function += gd->reloc_off;
1483 ops->xlate += gd->reloc_off;
1485 ops->set_flags += gd->reloc_off;
1487 ops->get_flags += gd->reloc_off;
1493 if (CONFIG_IS_ENABLED(GPIO_HOG)) {
1494 struct udevice *child;
1497 dev_for_each_subnode(node, dev) {
1498 if (ofnode_read_bool(node, "gpio-hog")) {
1499 const char *name = ofnode_get_name(node);
1502 ret = device_bind_driver_to_node(dev,
1514 UCLASS_DRIVER(gpio) = {
1517 .flags = DM_UC_FLAG_SEQ_ALIAS,
1518 .post_probe = gpio_post_probe,
1519 .post_bind = gpio_post_bind,
1520 .pre_remove = gpio_pre_remove,
1521 .per_device_auto = sizeof(struct gpio_dev_priv),