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 if (args->args_count < 1)
195 desc->offset = args->args[0];
197 if (args->args_count < 2)
201 if (args->args[1] & GPIO_ACTIVE_LOW)
202 desc->flags |= GPIOD_ACTIVE_LOW;
205 * need to test 2 bits for gpio output binding:
206 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
207 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
209 if (args->args[1] & GPIO_SINGLE_ENDED) {
210 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
211 desc->flags |= GPIOD_OPEN_DRAIN;
213 desc->flags |= GPIOD_OPEN_SOURCE;
216 if (args->args[1] & GPIO_PULL_UP)
217 desc->flags |= GPIOD_PULL_UP;
219 if (args->args[1] & GPIO_PULL_DOWN)
220 desc->flags |= GPIOD_PULL_DOWN;
225 static int gpio_find_and_xlate(struct gpio_desc *desc,
226 struct ofnode_phandle_args *args)
228 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
231 return ops->xlate(desc->dev, desc, args);
233 return gpio_xlate_offs_flags(desc->dev, desc, args);
236 #if CONFIG_IS_ENABLED(GPIO_HOG)
238 struct gpio_hog_priv {
239 struct gpio_desc gpiod;
242 struct gpio_hog_data {
248 static int gpio_hog_of_to_plat(struct udevice *dev)
250 struct gpio_hog_data *plat = dev_get_plat(dev);
251 const char *nodename;
255 if (dev_read_bool(dev, "input")) {
256 plat->gpiod_flags = GPIOD_IS_IN;
257 } else if (dev_read_bool(dev, "output-high")) {
259 plat->gpiod_flags = GPIOD_IS_OUT;
260 } else if (dev_read_bool(dev, "output-low")) {
261 plat->gpiod_flags = GPIOD_IS_OUT;
263 printf("%s: missing gpio-hog state.\n", __func__);
266 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
268 printf("%s: wrong gpios property, 2 values needed %d\n",
272 nodename = dev_read_string(dev, "line-name");
274 device_set_name(dev, nodename);
279 static int gpio_hog_probe(struct udevice *dev)
281 struct gpio_hog_data *plat = dev_get_plat(dev);
282 struct gpio_hog_priv *priv = dev_get_priv(dev);
285 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
286 plat->val[0], plat->gpiod_flags,
287 plat->val[1], &priv->gpiod);
289 debug("%s: node %s could not get gpio.\n", __func__,
294 if (plat->gpiod_flags == GPIOD_IS_OUT) {
295 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
297 debug("%s: node %s could not set gpio.\n", __func__,
306 int gpio_hog_probe_all(void)
312 for (uclass_first_device(UCLASS_NOP, &dev);
314 uclass_find_next_device(&dev)) {
315 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
316 ret = device_probe(dev);
318 printf("Failed to probe device %s err: %d\n",
328 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
333 gpio_hog_probe_all();
334 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
335 struct gpio_hog_priv *priv = dev_get_priv(dev);
337 *desc = &priv->gpiod;
344 U_BOOT_DRIVER(gpio_hog) = {
347 .of_to_plat = gpio_hog_of_to_plat,
348 .probe = gpio_hog_probe,
349 .priv_auto = sizeof(struct gpio_hog_priv),
350 .plat_auto = sizeof(struct gpio_hog_data),
353 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
359 int dm_gpio_request(struct gpio_desc *desc, const char *label)
361 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
362 struct udevice *dev = desc->dev;
363 struct gpio_dev_priv *uc_priv;
367 uc_priv = dev_get_uclass_priv(dev);
368 if (uc_priv->name[desc->offset])
374 ret = ops->request(dev, desc->offset, label);
380 uc_priv->name[desc->offset] = str;
385 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
387 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
392 vscnprintf(buf, sizeof(buf), fmt, args);
394 return dm_gpio_request(desc, buf);
396 return dm_gpio_request(desc, fmt);
401 * gpio_request() - [COMPAT] Request GPIO
403 * label: Name for the requested GPIO
405 * The label is copied and allocated so the caller does not need to keep
406 * the pointer around.
408 * This function implements the API that's compatible with current
409 * GPIO API used in U-Boot. The request is forwarded to particular
410 * GPIO driver. Returns 0 on success, negative value on error.
412 int gpio_request(unsigned gpio, const char *label)
414 struct gpio_desc desc;
417 ret = gpio_to_device(gpio, &desc);
421 return dm_gpio_request(&desc, label);
425 * gpio_requestf() - [COMPAT] Request GPIO
427 * @fmt: Format string for the requested GPIO
428 * @...: Arguments for the printf() format string
430 * This function implements the API that's compatible with current
431 * GPIO API used in U-Boot. The request is forwarded to particular
432 * GPIO driver. Returns 0 on success, negative value on error.
434 int gpio_requestf(unsigned gpio, const char *fmt, ...)
436 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
441 vscnprintf(buf, sizeof(buf), fmt, args);
443 return gpio_request(gpio, buf);
445 return gpio_request(gpio, fmt);
449 int _dm_gpio_free(struct udevice *dev, uint offset)
451 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
452 struct gpio_dev_priv *uc_priv;
455 uc_priv = dev_get_uclass_priv(dev);
456 if (!uc_priv->name[offset])
459 ret = ops->rfree(dev, offset);
464 free(uc_priv->name[offset]);
465 uc_priv->name[offset] = NULL;
471 * gpio_free() - [COMPAT] Relinquish GPIO
474 * This function implements the API that's compatible with current
475 * GPIO API used in U-Boot. The request is forwarded to particular
476 * GPIO driver. Returns 0 on success, negative value on error.
478 int gpio_free(unsigned gpio)
480 struct gpio_desc desc;
483 ret = gpio_to_device(gpio, &desc);
487 return _dm_gpio_free(desc.dev, desc.offset);
490 static int check_reserved(const struct gpio_desc *desc, const char *func)
492 struct gpio_dev_priv *uc_priv;
494 if (!dm_gpio_is_valid(desc))
497 uc_priv = dev_get_uclass_priv(desc->dev);
498 if (!uc_priv->name[desc->offset]) {
499 printf("%s: %s: error: gpio %s%d not reserved\n",
500 desc->dev->name, func,
501 uc_priv->bank_name ? uc_priv->bank_name : "",
510 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
513 * This function implements the API that's compatible with current
514 * GPIO API used in U-Boot. The request is forwarded to particular
515 * GPIO driver. Returns 0 on success, negative value on error.
517 int gpio_direction_input(unsigned gpio)
519 struct gpio_desc desc;
522 ret = gpio_to_device(gpio, &desc);
526 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
530 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
532 * value: Logical value to be set on the GPIO pin
534 * This function implements the API that's compatible with current
535 * GPIO API used in U-Boot. The request is forwarded to particular
536 * GPIO driver. Returns 0 on success, negative value on error.
538 int gpio_direction_output(unsigned gpio, int value)
540 struct gpio_desc desc;
544 ret = gpio_to_device(gpio, &desc);
548 flags = GPIOD_IS_OUT;
550 flags |= GPIOD_IS_OUT_ACTIVE;
551 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
554 static int _gpio_get_value(const struct gpio_desc *desc)
556 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
559 value = ops->get_value(desc->dev, desc->offset);
561 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
564 int dm_gpio_get_value(const struct gpio_desc *desc)
568 ret = check_reserved(desc, "get_value");
572 return _gpio_get_value(desc);
575 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
577 const struct dm_gpio_ops *ops;
580 ret = check_reserved(desc, "set_value");
584 if (desc->flags & GPIOD_ACTIVE_LOW)
587 /* GPIOD_ are directly managed by driver in set_flags */
588 ops = gpio_get_ops(desc->dev);
589 if (ops->set_flags) {
590 ulong flags = desc->flags;
593 flags |= GPIOD_IS_OUT_ACTIVE;
595 flags &= ~GPIOD_IS_OUT_ACTIVE;
596 return ops->set_flags(desc->dev, desc->offset, flags);
600 * Emulate open drain by not actively driving the line high or
601 * Emulate open source by not actively driving the line low
603 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
604 (desc->flags & GPIOD_OPEN_SOURCE && !value))
605 return ops->direction_input(desc->dev, desc->offset);
606 else if (desc->flags & GPIOD_OPEN_DRAIN ||
607 desc->flags & GPIOD_OPEN_SOURCE)
608 return ops->direction_output(desc->dev, desc->offset, value);
610 ret = ops->set_value(desc->dev, desc->offset, value);
617 /* check dir flags invalid configuration */
618 static int check_dir_flags(ulong flags)
620 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
621 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
626 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
627 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
632 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
633 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
642 * _dm_gpio_set_flags() - Send flags to the driver
644 * This uses the best available method to send the given flags to the driver.
645 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
646 * of GPIOD_IS_OUT_ACTIVE.
648 * @desc: GPIO description
649 * @flags: flags value to set
650 * @return 0 if OK, -ve on error
652 static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
654 struct udevice *dev = desc->dev;
655 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
656 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
659 ret = check_dir_flags(flags);
662 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
664 uc_priv->bank_name ? uc_priv->bank_name : "",
665 desc->offset, flags);
670 /* If active low, invert the output state */
671 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
672 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
673 flags ^= GPIOD_IS_OUT_ACTIVE;
675 /* GPIOD_ are directly managed by driver in set_flags */
676 if (ops->set_flags) {
677 ret = ops->set_flags(dev, desc->offset, flags);
679 if (flags & GPIOD_IS_OUT) {
680 bool value = flags & GPIOD_IS_OUT_ACTIVE;
682 ret = ops->direction_output(dev, desc->offset, value);
683 } else if (flags & GPIOD_IS_IN) {
684 ret = ops->direction_input(dev, desc->offset);
691 int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
696 ret = check_reserved(desc, "set_dir_flags");
700 flags = (desc->flags & ~clr) | set;
702 ret = _dm_gpio_set_flags(desc, flags);
706 /* save the flags also in descriptor */
712 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
714 /* combine the requested flags (for IN/OUT) and the descriptor flags */
715 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
718 int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
724 for (i = 0; i < count; i++) {
725 ret = dm_gpio_clrset_flags(&desc[i], clr, set);
733 int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
735 struct udevice *dev = desc->dev;
737 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
740 ret = check_reserved(desc, "get_flags");
744 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
745 if (ops->get_flags) {
746 ret = ops->get_flags(dev, desc->offset, &flags);
750 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
751 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
752 if (desc->flags & GPIOD_ACTIVE_LOW)
754 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
755 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
757 flags |= GPIOD_IS_OUT_ACTIVE;
760 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
761 flags &= ~GPIOD_IS_OUT_ACTIVE;
762 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
763 flags |= GPIOD_IS_OUT_ACTIVE;
771 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
774 * This function implements the API that's compatible with current
775 * GPIO API used in U-Boot. The request is forwarded to particular
776 * GPIO driver. Returns the value of the GPIO pin, or negative value
779 int gpio_get_value(unsigned gpio)
783 struct gpio_desc desc;
785 ret = gpio_to_device(gpio, &desc);
788 return dm_gpio_get_value(&desc);
792 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
794 * value: Logical value to be set on the GPIO pin.
796 * This function implements the API that's compatible with current
797 * GPIO API used in U-Boot. The request is forwarded to particular
798 * GPIO driver. Returns 0 on success, negative value on error.
800 int gpio_set_value(unsigned gpio, int value)
802 struct gpio_desc desc;
805 ret = gpio_to_device(gpio, &desc);
808 return dm_gpio_set_value(&desc, value);
811 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
813 struct gpio_dev_priv *priv;
815 /* Must be called on an active device */
816 priv = dev_get_uclass_priv(dev);
819 *bit_count = priv->gpio_count;
820 return priv->bank_name;
823 static const char * const gpio_function[GPIOF_COUNT] = {
831 static int get_function(struct udevice *dev, int offset, bool skip_unused,
834 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
835 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
837 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
838 if (!device_active(dev))
840 if (offset < 0 || offset >= uc_priv->gpio_count)
843 *namep = uc_priv->name[offset];
844 if (skip_unused && !uc_priv->name[offset])
846 if (ops->get_function) {
849 ret = ops->get_function(dev, offset);
852 if (ret >= ARRAY_SIZE(gpio_function))
857 return GPIOF_UNKNOWN;
860 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
862 return get_function(dev, offset, true, namep);
865 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
867 return get_function(dev, offset, false, namep);
870 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
872 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
873 struct gpio_dev_priv *priv;
879 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
882 priv = dev_get_uclass_priv(dev);
883 ret = gpio_get_raw_function(dev, offset, NULL);
887 len = snprintf(str, buffsize, "%s%d: %s",
888 priv->bank_name ? priv->bank_name : "",
889 offset, gpio_function[func]);
890 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
891 func == GPIOF_UNUSED) {
895 ret = ops->get_value(dev, offset);
898 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
899 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
909 #if CONFIG_IS_ENABLED(ACPIGEN)
910 int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
912 const struct dm_gpio_ops *ops;
914 memset(gpio, '\0', sizeof(*gpio));
915 if (!dm_gpio_is_valid(desc)) {
916 /* Indicate that the GPIO is not valid */
922 ops = gpio_get_ops(desc->dev);
926 return ops->get_acpi(desc, gpio);
930 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
935 for (i = 0; i < 32; i++) {
936 gpio = gpio_num_array[i];
939 ret = gpio_requestf(gpio, fmt, i);
942 ret = gpio_direction_input(gpio);
951 for (i--; i >= 0; i--)
952 gpio_free(gpio_num_array[i]);
958 * get a number comprised of multiple GPIO values. gpio_num_array points to
959 * the array of gpio pin numbers to scan, terminated by -1.
961 int gpio_get_values_as_int(const int *gpio_list)
964 unsigned bitmask = 1;
969 ((gpio = *gpio_list++) != -1)) {
970 ret = gpio_get_value(gpio);
981 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
983 unsigned bitmask = 1;
987 for (i = 0; i < count; i++) {
988 ret = dm_gpio_get_value(&desc_list[i]);
999 int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
1002 static const char tristate[] = "01z";
1009 int vals[NUM_OPTIONS];
1015 * Limit to 19 digits which should be plenty. This avoids overflow of a
1020 for (i = 0; i < NUM_OPTIONS; i++) {
1021 uint flags = GPIOD_IS_IN;
1023 flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
1024 ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
1027 return log_msg_ret("pu", ret);
1029 /* Give the lines time to settle */
1032 ret = dm_gpio_get_values_as_int(desc_list, count);
1034 return log_msg_ret("get1", ret);
1038 log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
1039 for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
1040 uint pd = vals[PULLDOWN] & mask ? 1 : 0;
1041 uint pu = vals[PULLUP] & mask ? 1 : 0;
1045 * Get value with internal pulldown active. If this is 1 then
1046 * there is a stronger external pullup, which we call 1. If not
1049 * If the values differ then the pin is floating so we call
1056 log_debug("%c ", tristate[digit]);
1057 vector = 3 * vector + digit;
1059 log_debug("vector=%d\n", vector);
1065 * gpio_request_tail: common work for requesting a gpio.
1067 * ret: return value from previous work in function which calls
1069 * This seems bogus (why calling this function instead not
1070 * calling it and end caller function instead?).
1071 * Because on error in caller function we want to set some
1072 * default values in gpio desc and have a common error
1073 * debug message, which provides this function.
1074 * nodename: Name of node for which gpio gets requested
1075 * used for gpio label name.
1076 * args: pointer to output arguments structure
1077 * list_name: Name of GPIO list
1078 * used for gpio label name.
1079 * index: gpio index in gpio list
1080 * used for gpio label name.
1081 * desc: pointer to gpio descriptor, filled from this
1083 * flags: gpio flags to use.
1084 * add_index: should index added to gpio label name
1085 * gpio_dev: pointer to gpio device from which the gpio
1086 * will be requested. If NULL try to get the
1087 * gpio device with uclass_get_device_by_ofnode()
1089 * return: In error case this function sets default values in
1090 * gpio descriptor, also emmits a debug message.
1091 * On success it returns 0 else the error code from
1092 * function calls, or the error code passed through
1093 * ret to this function.
1096 static int gpio_request_tail(int ret, const char *nodename,
1097 struct ofnode_phandle_args *args,
1098 const char *list_name, int index,
1099 struct gpio_desc *desc, int flags,
1100 bool add_index, struct udevice *gpio_dev)
1102 gpio_desc_init(desc, gpio_dev, 0);
1107 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1110 debug("%s: uclass_get_device_by_ofnode failed\n",
1115 ret = gpio_find_and_xlate(desc, args);
1117 debug("%s: gpio_find_and_xlate failed\n", __func__);
1120 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
1121 nodename, list_name, index);
1123 debug("%s: dm_gpio_requestf failed\n", __func__);
1127 /* Keep any direction flags provided by the devicetree */
1128 ret = dm_gpio_set_dir_flags(desc,
1129 flags | (desc->flags & GPIOD_MASK_DIR));
1131 debug("%s: dm_gpio_set_dir failed\n", __func__);
1137 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
1138 __func__, nodename, list_name, index, ret);
1142 #if CONFIG_IS_ENABLED(OF_REAL)
1143 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1144 int index, struct gpio_desc *desc,
1145 int flags, bool add_index)
1147 struct ofnode_phandle_args args;
1150 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1153 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1154 index, desc, flags, add_index, NULL);
1157 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
1158 struct gpio_desc *desc, int flags)
1160 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1164 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
1165 struct gpio_desc *desc, int flags)
1167 struct ofnode_phandle_args args;
1171 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1173 node = dev_ofnode(dev);
1174 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1175 index, desc, flags, index > 0, NULL);
1178 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1179 struct gpio_desc *desc, int max_count,
1185 for (count = 0; count < max_count; count++) {
1186 ret = _gpio_request_by_name_nodev(node, list_name, count,
1187 &desc[count], flags, true);
1194 /* We ran out of GPIOs in the list */
1198 gpio_free_list_nodev(desc, count - 1);
1203 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1204 struct gpio_desc *desc, int max_count,
1208 * This isn't ideal since we don't use dev->name in the debug()
1209 * calls in gpio_request_by_name(), but we can do this until
1210 * gpio_request_list_by_name_nodev() can be dropped.
1212 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1216 int gpio_get_list_count(struct udevice *dev, const char *list_name)
1220 ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells",
1223 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1224 __func__, dev->name, list_name, ret);
1229 #endif /* OF_PLATDATA */
1231 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1232 int gpio_request_by_phandle(struct udevice *dev,
1233 const struct phandle_2_arg *cells,
1234 struct gpio_desc *desc, int flags)
1236 struct ofnode_phandle_args args;
1237 struct udevice *gpio_dev;
1238 const int index = 0;
1241 ret = device_get_by_ofplat_idx(cells->idx, &gpio_dev);
1244 args.args[0] = cells->arg[0];
1245 args.args[1] = cells->arg[1];
1247 return gpio_request_tail(ret, NULL, &args, NULL, index, desc, flags,
1248 index > 0, gpio_dev);
1252 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1254 /* For now, we don't do any checking of dev */
1255 return _dm_gpio_free(desc->dev, desc->offset);
1258 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1262 /* For now, we don't do any checking of dev */
1263 for (i = 0; i < count; i++)
1264 dm_gpio_free(dev, &desc[i]);
1269 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1271 return gpio_free_list(NULL, desc, count);
1274 /* We need to renumber the GPIOs when any driver is probed/removed */
1275 static int gpio_renumber(struct udevice *removed_dev)
1277 struct gpio_dev_priv *uc_priv;
1278 struct udevice *dev;
1283 ret = uclass_get(UCLASS_GPIO, &uc);
1287 /* Ensure that we have a base for each bank */
1289 uclass_foreach_dev(dev, uc) {
1290 if (device_active(dev) && dev != removed_dev) {
1291 uc_priv = dev_get_uclass_priv(dev);
1292 uc_priv->gpio_base = base;
1293 base += uc_priv->gpio_count;
1300 int gpio_get_number(const struct gpio_desc *desc)
1302 struct udevice *dev = desc->dev;
1303 struct gpio_dev_priv *uc_priv;
1307 uc_priv = dev_get_uclass_priv(dev);
1309 return uc_priv->gpio_base + desc->offset;
1312 static int gpio_post_probe(struct udevice *dev)
1314 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1316 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1320 return gpio_renumber(NULL);
1323 static int gpio_pre_remove(struct udevice *dev)
1325 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1328 for (i = 0; i < uc_priv->gpio_count; i++) {
1329 if (uc_priv->name[i])
1330 free(uc_priv->name[i]);
1332 free(uc_priv->name);
1334 return gpio_renumber(dev);
1337 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1338 char *list_name, int index, int flags,
1339 int dtflags, struct gpio_desc *desc)
1341 struct ofnode_phandle_args args;
1343 args.node = ofnode_null();
1344 args.args_count = 2;
1345 args.args[0] = index;
1346 args.args[1] = dtflags;
1348 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1352 static void devm_gpiod_release(struct udevice *dev, void *res)
1354 dm_gpio_free(dev, res);
1357 static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1362 struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1363 unsigned int index, int flags)
1366 struct gpio_desc *desc;
1368 static const char suffix[] = "-gpios";
1370 propname = malloc(strlen(id) + sizeof(suffix));
1376 strcpy(propname, id);
1377 strcat(propname, suffix);
1379 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1381 if (unlikely(!desc)) {
1386 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1395 devres_add(dev, desc);
1400 struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1405 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1413 void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1417 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1421 static int gpio_post_bind(struct udevice *dev)
1423 struct udevice *child;
1426 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1427 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1428 static int reloc_done;
1432 ops->request += gd->reloc_off;
1434 ops->rfree += gd->reloc_off;
1435 if (ops->direction_input)
1436 ops->direction_input += gd->reloc_off;
1437 if (ops->direction_output)
1438 ops->direction_output += gd->reloc_off;
1440 ops->get_value += gd->reloc_off;
1442 ops->set_value += gd->reloc_off;
1443 if (ops->get_function)
1444 ops->get_function += gd->reloc_off;
1446 ops->xlate += gd->reloc_off;
1448 ops->set_flags += gd->reloc_off;
1450 ops->get_flags += gd->reloc_off;
1456 if (CONFIG_IS_ENABLED(OF_REAL) && IS_ENABLED(CONFIG_GPIO_HOG)) {
1457 dev_for_each_subnode(node, dev) {
1458 if (ofnode_read_bool(node, "gpio-hog")) {
1459 const char *name = ofnode_get_name(node);
1462 ret = device_bind_driver_to_node(dev,
1474 UCLASS_DRIVER(gpio) = {
1477 .flags = DM_UC_FLAG_SEQ_ALIAS,
1478 .post_probe = gpio_post_probe,
1479 .post_bind = gpio_post_bind,
1480 .pre_remove = gpio_pre_remove,
1481 .per_device_auto = sizeof(struct gpio_dev_priv),