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))
148 * if we did not found a gpio through its bank
149 * name, we search for a valid gpio label.
151 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
156 return ret ? ret : -EINVAL;
158 gpio_desc_init(desc, dev, offset);
163 int gpio_lookup_name(const char *name, struct udevice **devp,
164 unsigned int *offsetp, unsigned int *gpiop)
166 struct gpio_desc desc;
171 ret = dm_gpio_lookup_name(name, &desc);
178 *offsetp = desc.offset;
180 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
182 *gpiop = uc_priv->gpio_base + desc.offset;
188 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
189 struct ofnode_phandle_args *args)
191 if (args->args_count < 1)
194 desc->offset = args->args[0];
196 if (args->args_count < 2)
200 if (args->args[1] & GPIO_ACTIVE_LOW)
201 desc->flags |= GPIOD_ACTIVE_LOW;
204 * need to test 2 bits for gpio output binding:
205 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
206 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
208 if (args->args[1] & GPIO_SINGLE_ENDED) {
209 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
210 desc->flags |= GPIOD_OPEN_DRAIN;
212 desc->flags |= GPIOD_OPEN_SOURCE;
215 if (args->args[1] & GPIO_PULL_UP)
216 desc->flags |= GPIOD_PULL_UP;
218 if (args->args[1] & GPIO_PULL_DOWN)
219 desc->flags |= GPIOD_PULL_DOWN;
224 static int gpio_find_and_xlate(struct gpio_desc *desc,
225 struct ofnode_phandle_args *args)
227 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
230 return ops->xlate(desc->dev, desc, args);
232 return gpio_xlate_offs_flags(desc->dev, desc, args);
235 #if CONFIG_IS_ENABLED(GPIO_HOG)
237 struct gpio_hog_priv {
238 struct gpio_desc gpiod;
241 struct gpio_hog_data {
247 static int gpio_hog_of_to_plat(struct udevice *dev)
249 struct gpio_hog_data *plat = dev_get_plat(dev);
250 const char *nodename;
254 if (dev_read_bool(dev, "input")) {
255 plat->gpiod_flags = GPIOD_IS_IN;
256 } else if (dev_read_bool(dev, "output-high")) {
258 plat->gpiod_flags = GPIOD_IS_OUT;
259 } else if (dev_read_bool(dev, "output-low")) {
260 plat->gpiod_flags = GPIOD_IS_OUT;
262 printf("%s: missing gpio-hog state.\n", __func__);
265 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
267 printf("%s: wrong gpios property, 2 values needed %d\n",
271 nodename = dev_read_string(dev, "line-name");
273 device_set_name(dev, nodename);
278 static int gpio_hog_probe(struct udevice *dev)
280 struct gpio_hog_data *plat = dev_get_plat(dev);
281 struct gpio_hog_priv *priv = dev_get_priv(dev);
284 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
285 plat->val[0], plat->gpiod_flags,
286 plat->val[1], &priv->gpiod);
288 debug("%s: node %s could not get gpio.\n", __func__,
293 if (plat->gpiod_flags == GPIOD_IS_OUT) {
294 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
296 debug("%s: node %s could not set gpio.\n", __func__,
305 int gpio_hog_probe_all(void)
311 for (uclass_first_device(UCLASS_NOP, &dev);
313 uclass_find_next_device(&dev)) {
314 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
315 ret = device_probe(dev);
317 printf("Failed to probe device %s err: %d\n",
327 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
332 gpio_hog_probe_all();
333 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
334 struct gpio_hog_priv *priv = dev_get_priv(dev);
336 *desc = &priv->gpiod;
343 U_BOOT_DRIVER(gpio_hog) = {
346 .of_to_plat = gpio_hog_of_to_plat,
347 .probe = gpio_hog_probe,
348 .priv_auto = sizeof(struct gpio_hog_priv),
349 .plat_auto = sizeof(struct gpio_hog_data),
352 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
358 int dm_gpio_request(struct gpio_desc *desc, const char *label)
360 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
361 struct udevice *dev = desc->dev;
362 struct gpio_dev_priv *uc_priv;
366 uc_priv = dev_get_uclass_priv(dev);
367 if (uc_priv->name[desc->offset])
373 ret = ops->request(dev, desc->offset, label);
379 uc_priv->name[desc->offset] = str;
384 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
386 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
391 vscnprintf(buf, sizeof(buf), fmt, args);
393 return dm_gpio_request(desc, buf);
395 return dm_gpio_request(desc, fmt);
400 * gpio_request() - [COMPAT] Request GPIO
402 * label: Name for the requested GPIO
404 * The label is copied and allocated so the caller does not need to keep
405 * the pointer around.
407 * This function implements the API that's compatible with current
408 * GPIO API used in U-Boot. The request is forwarded to particular
409 * GPIO driver. Returns 0 on success, negative value on error.
411 int gpio_request(unsigned gpio, const char *label)
413 struct gpio_desc desc;
416 ret = gpio_to_device(gpio, &desc);
420 return dm_gpio_request(&desc, label);
424 * gpio_requestf() - [COMPAT] Request GPIO
426 * @fmt: Format string for the requested GPIO
427 * @...: Arguments for the printf() format string
429 * This function implements the API that's compatible with current
430 * GPIO API used in U-Boot. The request is forwarded to particular
431 * GPIO driver. Returns 0 on success, negative value on error.
433 int gpio_requestf(unsigned gpio, const char *fmt, ...)
435 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
440 vscnprintf(buf, sizeof(buf), fmt, args);
442 return gpio_request(gpio, buf);
444 return gpio_request(gpio, fmt);
448 int _dm_gpio_free(struct udevice *dev, uint offset)
450 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
451 struct gpio_dev_priv *uc_priv;
454 uc_priv = dev_get_uclass_priv(dev);
455 if (!uc_priv->name[offset])
458 ret = ops->rfree(dev, offset);
463 free(uc_priv->name[offset]);
464 uc_priv->name[offset] = NULL;
470 * gpio_free() - [COMPAT] Relinquish GPIO
473 * This function implements the API that's compatible with current
474 * GPIO API used in U-Boot. The request is forwarded to particular
475 * GPIO driver. Returns 0 on success, negative value on error.
477 int gpio_free(unsigned gpio)
479 struct gpio_desc desc;
482 ret = gpio_to_device(gpio, &desc);
486 return _dm_gpio_free(desc.dev, desc.offset);
489 static int check_reserved(const struct gpio_desc *desc, const char *func)
491 struct gpio_dev_priv *uc_priv;
493 if (!dm_gpio_is_valid(desc))
496 uc_priv = dev_get_uclass_priv(desc->dev);
497 if (!uc_priv->name[desc->offset]) {
498 printf("%s: %s: error: gpio %s%d not reserved\n",
499 desc->dev->name, func,
500 uc_priv->bank_name ? uc_priv->bank_name : "",
509 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
512 * This function implements the API that's compatible with current
513 * GPIO API used in U-Boot. The request is forwarded to particular
514 * GPIO driver. Returns 0 on success, negative value on error.
516 int gpio_direction_input(unsigned gpio)
518 struct gpio_desc desc;
521 ret = gpio_to_device(gpio, &desc);
525 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
529 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
531 * value: Logical value to be set on the GPIO pin
533 * This function implements the API that's compatible with current
534 * GPIO API used in U-Boot. The request is forwarded to particular
535 * GPIO driver. Returns 0 on success, negative value on error.
537 int gpio_direction_output(unsigned gpio, int value)
539 struct gpio_desc desc;
543 ret = gpio_to_device(gpio, &desc);
547 flags = GPIOD_IS_OUT;
549 flags |= GPIOD_IS_OUT_ACTIVE;
550 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
553 static int _gpio_get_value(const struct gpio_desc *desc)
555 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
558 value = ops->get_value(desc->dev, desc->offset);
560 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
563 int dm_gpio_get_value(const struct gpio_desc *desc)
567 ret = check_reserved(desc, "get_value");
571 return _gpio_get_value(desc);
574 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
576 const struct dm_gpio_ops *ops;
579 ret = check_reserved(desc, "set_value");
583 if (desc->flags & GPIOD_ACTIVE_LOW)
586 /* GPIOD_ are directly managed by driver in set_flags */
587 ops = gpio_get_ops(desc->dev);
588 if (ops->set_flags) {
589 ulong flags = desc->flags;
592 flags |= GPIOD_IS_OUT_ACTIVE;
594 flags &= ~GPIOD_IS_OUT_ACTIVE;
595 return ops->set_flags(desc->dev, desc->offset, flags);
599 * Emulate open drain by not actively driving the line high or
600 * Emulate open source by not actively driving the line low
602 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
603 (desc->flags & GPIOD_OPEN_SOURCE && !value))
604 return ops->direction_input(desc->dev, desc->offset);
605 else if (desc->flags & GPIOD_OPEN_DRAIN ||
606 desc->flags & GPIOD_OPEN_SOURCE)
607 return ops->direction_output(desc->dev, desc->offset, value);
609 ret = ops->set_value(desc->dev, desc->offset, value);
616 /* check dir flags invalid configuration */
617 static int check_dir_flags(ulong flags)
619 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
620 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
625 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
626 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
631 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
632 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
641 * _dm_gpio_set_flags() - Send flags to the driver
643 * This uses the best available method to send the given flags to the driver.
644 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
645 * of GPIOD_IS_OUT_ACTIVE.
647 * @desc: GPIO description
648 * @flags: flags value to set
649 * @return 0 if OK, -ve on error
651 static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
653 struct udevice *dev = desc->dev;
654 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
655 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
658 ret = check_dir_flags(flags);
661 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
663 uc_priv->bank_name ? uc_priv->bank_name : "",
664 desc->offset, flags);
669 /* If active low, invert the output state */
670 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
671 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
672 flags ^= GPIOD_IS_OUT_ACTIVE;
674 /* GPIOD_ are directly managed by driver in set_flags */
675 if (ops->set_flags) {
676 ret = ops->set_flags(dev, desc->offset, flags);
678 if (flags & GPIOD_IS_OUT) {
679 bool value = flags & GPIOD_IS_OUT_ACTIVE;
681 ret = ops->direction_output(dev, desc->offset, value);
682 } else if (flags & GPIOD_IS_IN) {
683 ret = ops->direction_input(dev, desc->offset);
690 int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
695 ret = check_reserved(desc, "set_dir_flags");
699 flags = (desc->flags & ~clr) | set;
701 ret = _dm_gpio_set_flags(desc, flags);
705 /* save the flags also in descriptor */
711 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
713 /* combine the requested flags (for IN/OUT) and the descriptor flags */
714 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
717 int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
723 for (i = 0; i < count; i++) {
724 ret = dm_gpio_clrset_flags(&desc[i], clr, set);
732 int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
734 struct udevice *dev = desc->dev;
736 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
739 ret = check_reserved(desc, "get_flags");
743 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
744 if (ops->get_flags) {
745 ret = ops->get_flags(dev, desc->offset, &flags);
749 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
750 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
751 if (desc->flags & GPIOD_ACTIVE_LOW)
753 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
754 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
756 flags |= GPIOD_IS_OUT_ACTIVE;
759 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
760 flags &= ~GPIOD_IS_OUT_ACTIVE;
761 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
762 flags |= GPIOD_IS_OUT_ACTIVE;
770 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
773 * This function implements the API that's compatible with current
774 * GPIO API used in U-Boot. The request is forwarded to particular
775 * GPIO driver. Returns the value of the GPIO pin, or negative value
778 int gpio_get_value(unsigned gpio)
782 struct gpio_desc desc;
784 ret = gpio_to_device(gpio, &desc);
787 return dm_gpio_get_value(&desc);
791 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
793 * value: Logical value to be set on the GPIO pin.
795 * This function implements the API that's compatible with current
796 * GPIO API used in U-Boot. The request is forwarded to particular
797 * GPIO driver. Returns 0 on success, negative value on error.
799 int gpio_set_value(unsigned gpio, int value)
801 struct gpio_desc desc;
804 ret = gpio_to_device(gpio, &desc);
807 return dm_gpio_set_value(&desc, value);
810 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
812 struct gpio_dev_priv *priv;
814 /* Must be called on an active device */
815 priv = dev_get_uclass_priv(dev);
818 *bit_count = priv->gpio_count;
819 return priv->bank_name;
822 static const char * const gpio_function[GPIOF_COUNT] = {
830 static int get_function(struct udevice *dev, int offset, bool skip_unused,
833 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
834 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
836 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
837 if (!device_active(dev))
839 if (offset < 0 || offset >= uc_priv->gpio_count)
842 *namep = uc_priv->name[offset];
843 if (skip_unused && !uc_priv->name[offset])
845 if (ops->get_function) {
848 ret = ops->get_function(dev, offset);
851 if (ret >= ARRAY_SIZE(gpio_function))
856 return GPIOF_UNKNOWN;
859 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
861 return get_function(dev, offset, true, namep);
864 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
866 return get_function(dev, offset, false, namep);
869 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
871 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
872 struct gpio_dev_priv *priv;
878 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
881 priv = dev_get_uclass_priv(dev);
882 ret = gpio_get_raw_function(dev, offset, NULL);
886 len = snprintf(str, buffsize, "%s%d: %s",
887 priv->bank_name ? priv->bank_name : "",
888 offset, gpio_function[func]);
889 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
890 func == GPIOF_UNUSED) {
894 ret = ops->get_value(dev, offset);
897 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
898 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
908 #if CONFIG_IS_ENABLED(ACPIGEN)
909 int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
911 const struct dm_gpio_ops *ops;
913 memset(gpio, '\0', sizeof(*gpio));
914 if (!dm_gpio_is_valid(desc)) {
915 /* Indicate that the GPIO is not valid */
921 ops = gpio_get_ops(desc->dev);
925 return ops->get_acpi(desc, gpio);
929 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
934 for (i = 0; i < 32; i++) {
935 gpio = gpio_num_array[i];
938 ret = gpio_requestf(gpio, fmt, i);
941 ret = gpio_direction_input(gpio);
950 for (i--; i >= 0; i--)
951 gpio_free(gpio_num_array[i]);
957 * get a number comprised of multiple GPIO values. gpio_num_array points to
958 * the array of gpio pin numbers to scan, terminated by -1.
960 int gpio_get_values_as_int(const int *gpio_list)
963 unsigned bitmask = 1;
968 ((gpio = *gpio_list++) != -1)) {
969 ret = gpio_get_value(gpio);
980 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
982 unsigned bitmask = 1;
986 for (i = 0; i < count; i++) {
987 ret = dm_gpio_get_value(&desc_list[i]);
998 int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
1001 static const char tristate[] = "01z";
1008 int vals[NUM_OPTIONS];
1014 * Limit to 19 digits which should be plenty. This avoids overflow of a
1019 for (i = 0; i < NUM_OPTIONS; i++) {
1020 uint flags = GPIOD_IS_IN;
1022 flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
1023 ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
1026 return log_msg_ret("pu", ret);
1028 /* Give the lines time to settle */
1031 ret = dm_gpio_get_values_as_int(desc_list, count);
1033 return log_msg_ret("get1", ret);
1037 log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
1038 for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
1039 uint pd = vals[PULLDOWN] & mask ? 1 : 0;
1040 uint pu = vals[PULLUP] & mask ? 1 : 0;
1044 * Get value with internal pulldown active. If this is 1 then
1045 * there is a stronger external pullup, which we call 1. If not
1048 * If the values differ then the pin is floating so we call
1055 log_debug("%c ", tristate[digit]);
1056 vector = 3 * vector + digit;
1058 log_debug("vector=%d\n", vector);
1064 * gpio_request_tail: common work for requesting a gpio.
1066 * ret: return value from previous work in function which calls
1068 * This seems bogus (why calling this function instead not
1069 * calling it and end caller function instead?).
1070 * Because on error in caller function we want to set some
1071 * default values in gpio desc and have a common error
1072 * debug message, which provides this function.
1073 * nodename: Name of node for which gpio gets requested
1074 * used for gpio label name.
1075 * args: pointer to output arguments structure
1076 * list_name: Name of GPIO list
1077 * used for gpio label name.
1078 * index: gpio index in gpio list
1079 * used for gpio label name.
1080 * desc: pointer to gpio descriptor, filled from this
1082 * flags: gpio flags to use.
1083 * add_index: should index added to gpio label name
1084 * gpio_dev: pointer to gpio device from which the gpio
1085 * will be requested. If NULL try to get the
1086 * gpio device with uclass_get_device_by_ofnode()
1088 * return: In error case this function sets default values in
1089 * gpio descriptor, also emmits a debug message.
1090 * On success it returns 0 else the error code from
1091 * function calls, or the error code passed through
1092 * ret to this function.
1095 static int gpio_request_tail(int ret, const char *nodename,
1096 struct ofnode_phandle_args *args,
1097 const char *list_name, int index,
1098 struct gpio_desc *desc, int flags,
1099 bool add_index, struct udevice *gpio_dev)
1101 gpio_desc_init(desc, gpio_dev, 0);
1106 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1109 debug("%s: uclass_get_device_by_ofnode failed\n",
1114 ret = gpio_find_and_xlate(desc, args);
1116 debug("%s: gpio_find_and_xlate failed\n", __func__);
1119 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
1120 nodename, list_name, index);
1122 debug("%s: dm_gpio_requestf failed\n", __func__);
1126 /* Keep any direction flags provided by the devicetree */
1127 ret = dm_gpio_set_dir_flags(desc,
1128 flags | (desc->flags & GPIOD_MASK_DIR));
1130 debug("%s: dm_gpio_set_dir failed\n", __func__);
1136 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
1137 __func__, nodename, list_name, index, ret);
1141 #if CONFIG_IS_ENABLED(OF_REAL)
1142 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1143 int index, struct gpio_desc *desc,
1144 int flags, bool add_index)
1146 struct ofnode_phandle_args args;
1149 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1152 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1153 index, desc, flags, add_index, NULL);
1156 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
1157 struct gpio_desc *desc, int flags)
1159 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1163 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
1164 struct gpio_desc *desc, int flags)
1166 struct ofnode_phandle_args args;
1170 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1172 node = dev_ofnode(dev);
1173 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1174 index, desc, flags, index > 0, NULL);
1177 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1178 struct gpio_desc *desc, int max_count,
1184 for (count = 0; count < max_count; count++) {
1185 ret = _gpio_request_by_name_nodev(node, list_name, count,
1186 &desc[count], flags, true);
1193 /* We ran out of GPIOs in the list */
1197 gpio_free_list_nodev(desc, count - 1);
1202 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1203 struct gpio_desc *desc, int max_count,
1207 * This isn't ideal since we don't use dev->name in the debug()
1208 * calls in gpio_request_by_name(), but we can do this until
1209 * gpio_request_list_by_name_nodev() can be dropped.
1211 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1215 int gpio_get_list_count(struct udevice *dev, const char *list_name)
1219 ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells",
1222 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1223 __func__, dev->name, list_name, ret);
1228 #endif /* OF_PLATDATA */
1230 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1231 int gpio_request_by_phandle(struct udevice *dev,
1232 const struct phandle_2_arg *cells,
1233 struct gpio_desc *desc, int flags)
1235 struct ofnode_phandle_args args;
1236 struct udevice *gpio_dev;
1237 const int index = 0;
1240 ret = device_get_by_ofplat_idx(cells->idx, &gpio_dev);
1243 args.args[0] = cells->arg[0];
1244 args.args[1] = cells->arg[1];
1246 return gpio_request_tail(ret, NULL, &args, NULL, index, desc, flags,
1247 index > 0, gpio_dev);
1251 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1253 /* For now, we don't do any checking of dev */
1254 return _dm_gpio_free(desc->dev, desc->offset);
1257 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1261 /* For now, we don't do any checking of dev */
1262 for (i = 0; i < count; i++)
1263 dm_gpio_free(dev, &desc[i]);
1268 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1270 return gpio_free_list(NULL, desc, count);
1273 /* We need to renumber the GPIOs when any driver is probed/removed */
1274 static int gpio_renumber(struct udevice *removed_dev)
1276 struct gpio_dev_priv *uc_priv;
1277 struct udevice *dev;
1282 ret = uclass_get(UCLASS_GPIO, &uc);
1286 /* Ensure that we have a base for each bank */
1288 uclass_foreach_dev(dev, uc) {
1289 if (device_active(dev) && dev != removed_dev) {
1290 uc_priv = dev_get_uclass_priv(dev);
1291 uc_priv->gpio_base = base;
1292 base += uc_priv->gpio_count;
1299 int gpio_get_number(const struct gpio_desc *desc)
1301 struct udevice *dev = desc->dev;
1302 struct gpio_dev_priv *uc_priv;
1306 uc_priv = dev_get_uclass_priv(dev);
1308 return uc_priv->gpio_base + desc->offset;
1311 static int gpio_post_probe(struct udevice *dev)
1313 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1315 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1319 return gpio_renumber(NULL);
1322 static int gpio_pre_remove(struct udevice *dev)
1324 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1327 for (i = 0; i < uc_priv->gpio_count; i++) {
1328 if (uc_priv->name[i])
1329 free(uc_priv->name[i]);
1331 free(uc_priv->name);
1333 return gpio_renumber(dev);
1336 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1337 char *list_name, int index, int flags,
1338 int dtflags, struct gpio_desc *desc)
1340 struct ofnode_phandle_args args;
1342 args.node = ofnode_null();
1343 args.args_count = 2;
1344 args.args[0] = index;
1345 args.args[1] = dtflags;
1347 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1351 static void devm_gpiod_release(struct udevice *dev, void *res)
1353 dm_gpio_free(dev, res);
1356 static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1361 struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1362 unsigned int index, int flags)
1365 struct gpio_desc *desc;
1367 static const char suffix[] = "-gpios";
1369 propname = malloc(strlen(id) + sizeof(suffix));
1375 strcpy(propname, id);
1376 strcat(propname, suffix);
1378 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1380 if (unlikely(!desc)) {
1385 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1394 devres_add(dev, desc);
1399 struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1404 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1412 void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1416 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1420 static int gpio_post_bind(struct udevice *dev)
1422 struct udevice *child;
1425 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1426 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1427 static int reloc_done;
1431 ops->request += gd->reloc_off;
1433 ops->rfree += gd->reloc_off;
1434 if (ops->direction_input)
1435 ops->direction_input += gd->reloc_off;
1436 if (ops->direction_output)
1437 ops->direction_output += gd->reloc_off;
1439 ops->get_value += gd->reloc_off;
1441 ops->set_value += gd->reloc_off;
1442 if (ops->get_function)
1443 ops->get_function += gd->reloc_off;
1445 ops->xlate += gd->reloc_off;
1447 ops->set_flags += gd->reloc_off;
1449 ops->get_flags += gd->reloc_off;
1455 if (CONFIG_IS_ENABLED(OF_REAL) && IS_ENABLED(CONFIG_GPIO_HOG)) {
1456 dev_for_each_subnode(node, dev) {
1457 if (ofnode_read_bool(node, "gpio-hog")) {
1458 const char *name = ofnode_get_name(node);
1461 ret = device_bind_driver_to_node(dev,
1473 UCLASS_DRIVER(gpio) = {
1476 .flags = DM_UC_FLAG_SEQ_ALIAS,
1477 .post_probe = gpio_post_probe,
1478 .post_bind = gpio_post_bind,
1479 .pre_remove = gpio_pre_remove,
1480 .per_device_auto = sizeof(struct gpio_dev_priv),