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_lookup_name(const char *name, struct gpio_desc **desc)
319 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
320 struct gpio_hog_priv *priv = dev_get_priv(dev);
322 *desc = &priv->gpiod;
329 U_BOOT_DRIVER(gpio_hog) = {
332 .of_to_plat = gpio_hog_of_to_plat,
333 .probe = gpio_hog_probe,
334 .priv_auto = sizeof(struct gpio_hog_priv),
335 .plat_auto = sizeof(struct gpio_hog_data),
338 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
344 int dm_gpio_request(struct gpio_desc *desc, const char *label)
346 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
347 struct udevice *dev = desc->dev;
348 struct gpio_dev_priv *uc_priv;
352 uc_priv = dev_get_uclass_priv(dev);
353 if (uc_priv->name[desc->offset])
359 ret = ops->request(dev, desc->offset, label);
365 uc_priv->name[desc->offset] = str;
370 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
372 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
377 vscnprintf(buf, sizeof(buf), fmt, args);
379 return dm_gpio_request(desc, buf);
381 return dm_gpio_request(desc, fmt);
386 * gpio_request() - [COMPAT] Request GPIO
388 * label: Name for the requested GPIO
390 * The label is copied and allocated so the caller does not need to keep
391 * the pointer around.
393 * This function implements the API that's compatible with current
394 * GPIO API used in U-Boot. The request is forwarded to particular
395 * GPIO driver. Returns 0 on success, negative value on error.
397 int gpio_request(unsigned gpio, const char *label)
399 struct gpio_desc desc;
402 ret = gpio_to_device(gpio, &desc);
406 return dm_gpio_request(&desc, label);
410 * gpio_requestf() - [COMPAT] Request GPIO
412 * @fmt: Format string for the requested GPIO
413 * @...: Arguments for the printf() format string
415 * This function implements the API that's compatible with current
416 * GPIO API used in U-Boot. The request is forwarded to particular
417 * GPIO driver. Returns 0 on success, negative value on error.
419 int gpio_requestf(unsigned gpio, const char *fmt, ...)
421 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
426 vscnprintf(buf, sizeof(buf), fmt, args);
428 return gpio_request(gpio, buf);
430 return gpio_request(gpio, fmt);
434 int _dm_gpio_free(struct udevice *dev, uint offset)
436 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
437 struct gpio_dev_priv *uc_priv;
440 uc_priv = dev_get_uclass_priv(dev);
441 if (!uc_priv->name[offset])
444 ret = ops->rfree(dev, offset);
449 free(uc_priv->name[offset]);
450 uc_priv->name[offset] = NULL;
456 * gpio_free() - [COMPAT] Relinquish GPIO
459 * This function implements the API that's compatible with current
460 * GPIO API used in U-Boot. The request is forwarded to particular
461 * GPIO driver. Returns 0 on success, negative value on error.
463 int gpio_free(unsigned gpio)
465 struct gpio_desc desc;
468 ret = gpio_to_device(gpio, &desc);
472 return _dm_gpio_free(desc.dev, desc.offset);
475 static int check_reserved(const struct gpio_desc *desc, const char *func)
477 struct gpio_dev_priv *uc_priv;
479 if (!dm_gpio_is_valid(desc))
482 uc_priv = dev_get_uclass_priv(desc->dev);
483 if (!uc_priv->name[desc->offset]) {
484 printf("%s: %s: error: gpio %s%d not reserved\n",
485 desc->dev->name, func,
486 uc_priv->bank_name ? uc_priv->bank_name : "",
495 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
498 * This function implements the API that's compatible with current
499 * GPIO API used in U-Boot. The request is forwarded to particular
500 * GPIO driver. Returns 0 on success, negative value on error.
502 int gpio_direction_input(unsigned gpio)
504 struct gpio_desc desc;
507 ret = gpio_to_device(gpio, &desc);
511 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
515 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
517 * value: Logical value to be set on the GPIO pin
519 * This function implements the API that's compatible with current
520 * GPIO API used in U-Boot. The request is forwarded to particular
521 * GPIO driver. Returns 0 on success, negative value on error.
523 int gpio_direction_output(unsigned gpio, int value)
525 struct gpio_desc desc;
529 ret = gpio_to_device(gpio, &desc);
533 flags = GPIOD_IS_OUT;
535 flags |= GPIOD_IS_OUT_ACTIVE;
536 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
539 static int _gpio_get_value(const struct gpio_desc *desc)
541 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
544 value = ops->get_value(desc->dev, desc->offset);
546 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
549 int dm_gpio_get_value(const struct gpio_desc *desc)
553 ret = check_reserved(desc, "get_value");
557 return _gpio_get_value(desc);
560 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
562 const struct dm_gpio_ops *ops;
565 ret = check_reserved(desc, "set_value");
569 if (desc->flags & GPIOD_ACTIVE_LOW)
572 /* GPIOD_ are directly managed by driver in set_flags */
573 ops = gpio_get_ops(desc->dev);
574 if (ops->set_flags) {
575 ulong flags = desc->flags;
578 flags |= GPIOD_IS_OUT_ACTIVE;
580 flags &= ~GPIOD_IS_OUT_ACTIVE;
581 return ops->set_flags(desc->dev, desc->offset, flags);
585 * Emulate open drain by not actively driving the line high or
586 * Emulate open source by not actively driving the line low
588 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
589 (desc->flags & GPIOD_OPEN_SOURCE && !value))
590 return ops->direction_input(desc->dev, desc->offset);
591 else if (desc->flags & GPIOD_OPEN_DRAIN ||
592 desc->flags & GPIOD_OPEN_SOURCE)
593 return ops->direction_output(desc->dev, desc->offset, value);
595 ret = ops->set_value(desc->dev, desc->offset, value);
602 /* check dir flags invalid configuration */
603 static int check_dir_flags(ulong flags)
605 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
606 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
611 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
612 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
617 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
618 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
627 * _dm_gpio_set_flags() - Send flags to the driver
629 * This uses the best available method to send the given flags to the driver.
630 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
631 * of GPIOD_IS_OUT_ACTIVE.
633 * @desc: GPIO description
634 * @flags: flags value to set
635 * Return: 0 if OK, -ve on error
637 static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
639 struct udevice *dev = desc->dev;
640 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
641 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
644 ret = check_dir_flags(flags);
647 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
649 uc_priv->bank_name ? uc_priv->bank_name : "",
650 desc->offset, flags);
655 /* If active low, invert the output state */
656 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
657 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
658 flags ^= GPIOD_IS_OUT_ACTIVE;
660 /* GPIOD_ are directly managed by driver in set_flags */
661 if (ops->set_flags) {
662 ret = ops->set_flags(dev, desc->offset, flags);
664 if (flags & GPIOD_IS_OUT) {
665 bool value = flags & GPIOD_IS_OUT_ACTIVE;
667 ret = ops->direction_output(dev, desc->offset, value);
668 } else if (flags & GPIOD_IS_IN) {
669 ret = ops->direction_input(dev, desc->offset);
676 int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
681 ret = check_reserved(desc, "set_dir_flags");
685 flags = (desc->flags & ~clr) | set;
687 ret = _dm_gpio_set_flags(desc, flags);
691 /* save the flags also in descriptor */
697 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
699 /* combine the requested flags (for IN/OUT) and the descriptor flags */
700 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
703 int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
709 for (i = 0; i < count; i++) {
710 ret = dm_gpio_clrset_flags(&desc[i], clr, set);
718 int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
720 struct udevice *dev = desc->dev;
722 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
725 ret = check_reserved(desc, "get_flags");
729 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
730 if (ops->get_flags) {
731 ret = ops->get_flags(dev, desc->offset, &flags);
735 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
736 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
737 if (desc->flags & GPIOD_ACTIVE_LOW)
739 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
740 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
742 flags |= GPIOD_IS_OUT_ACTIVE;
745 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
746 flags &= ~GPIOD_IS_OUT_ACTIVE;
747 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
748 flags |= GPIOD_IS_OUT_ACTIVE;
756 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
759 * This function implements the API that's compatible with current
760 * GPIO API used in U-Boot. The request is forwarded to particular
761 * GPIO driver. Returns the value of the GPIO pin, or negative value
764 int gpio_get_value(unsigned gpio)
768 struct gpio_desc desc;
770 ret = gpio_to_device(gpio, &desc);
773 return dm_gpio_get_value(&desc);
777 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
779 * value: Logical value to be set on the GPIO pin.
781 * This function implements the API that's compatible with current
782 * GPIO API used in U-Boot. The request is forwarded to particular
783 * GPIO driver. Returns 0 on success, negative value on error.
785 int gpio_set_value(unsigned gpio, int value)
787 struct gpio_desc desc;
790 ret = gpio_to_device(gpio, &desc);
793 return dm_gpio_set_value(&desc, value);
796 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
798 struct gpio_dev_priv *priv;
800 /* Must be called on an active device */
801 priv = dev_get_uclass_priv(dev);
804 *bit_count = priv->gpio_count;
805 return priv->bank_name;
808 static const char * const gpio_function[GPIOF_COUNT] = {
816 static int get_function(struct udevice *dev, int offset, bool skip_unused,
819 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
820 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
822 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
823 if (!device_active(dev))
825 if (offset < 0 || offset >= uc_priv->gpio_count)
828 *namep = uc_priv->name[offset];
829 if (skip_unused && !uc_priv->name[offset])
831 if (ops->get_function) {
834 ret = ops->get_function(dev, offset);
837 if (ret >= ARRAY_SIZE(gpio_function))
842 return GPIOF_UNKNOWN;
845 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
847 return get_function(dev, offset, true, namep);
850 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
852 return get_function(dev, offset, false, namep);
855 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
857 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
858 struct gpio_dev_priv *priv;
866 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
869 priv = dev_get_uclass_priv(dev);
870 ret = gpio_get_raw_function(dev, offset, &label);
874 len = snprintf(str, buffsize, "%s%d: %s",
875 priv->bank_name ? priv->bank_name : "",
876 offset, gpio_function[func]);
880 snprintf(str + len, buffsize - len, " %s", label ? label : "");
885 ret = ops->get_value(dev, offset);
888 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
889 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
900 #if CONFIG_IS_ENABLED(ACPIGEN)
901 int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
903 const struct dm_gpio_ops *ops;
905 memset(gpio, '\0', sizeof(*gpio));
906 if (!dm_gpio_is_valid(desc)) {
907 /* Indicate that the GPIO is not valid */
913 ops = gpio_get_ops(desc->dev);
917 return ops->get_acpi(desc, gpio);
921 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
926 for (i = 0; i < 32; i++) {
927 gpio = gpio_num_array[i];
930 ret = gpio_requestf(gpio, fmt, i);
933 ret = gpio_direction_input(gpio);
942 for (i--; i >= 0; i--)
943 gpio_free(gpio_num_array[i]);
949 * get a number comprised of multiple GPIO values. gpio_num_array points to
950 * the array of gpio pin numbers to scan, terminated by -1.
952 int gpio_get_values_as_int(const int *gpio_list)
955 unsigned bitmask = 1;
960 ((gpio = *gpio_list++) != -1)) {
961 ret = gpio_get_value(gpio);
972 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
974 unsigned bitmask = 1;
978 for (i = 0; i < count; i++) {
979 ret = dm_gpio_get_value(&desc_list[i]);
990 int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
993 static const char tristate[] = "01z";
1000 int vals[NUM_OPTIONS];
1006 * Limit to 19 digits which should be plenty. This avoids overflow of a
1011 for (i = 0; i < NUM_OPTIONS; i++) {
1012 uint flags = GPIOD_IS_IN;
1014 flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
1015 ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
1018 return log_msg_ret("pu", ret);
1020 /* Give the lines time to settle */
1023 ret = dm_gpio_get_values_as_int(desc_list, count);
1025 return log_msg_ret("get1", ret);
1029 log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
1030 for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
1031 uint pd = vals[PULLDOWN] & mask ? 1 : 0;
1032 uint pu = vals[PULLUP] & mask ? 1 : 0;
1036 * Get value with internal pulldown active. If this is 1 then
1037 * there is a stronger external pullup, which we call 1. If not
1040 * If the values differ then the pin is floating so we call
1047 log_debug("%c ", tristate[digit]);
1048 vector = 3 * vector + digit;
1050 log_debug("vector=%d\n", vector);
1056 * gpio_request_tail: common work for requesting a gpio.
1058 * ret: return value from previous work in function which calls
1060 * This seems bogus (why calling this function instead not
1061 * calling it and end caller function instead?).
1062 * Because on error in caller function we want to set some
1063 * default values in gpio desc and have a common error
1064 * debug message, which provides this function.
1065 * nodename: Name of node for which gpio gets requested
1066 * used for gpio label name.
1067 * args: pointer to output arguments structure
1068 * list_name: Name of GPIO list
1069 * used for gpio label name.
1070 * index: gpio index in gpio list
1071 * used for gpio label name.
1072 * desc: pointer to gpio descriptor, filled from this
1074 * flags: gpio flags to use.
1075 * add_index: should index added to gpio label name
1076 * gpio_dev: pointer to gpio device from which the gpio
1077 * will be requested. If NULL try to get the
1078 * gpio device with uclass_get_device_by_ofnode()
1080 * return: In error case this function sets default values in
1081 * gpio descriptor, also emmits a debug message.
1082 * On success it returns 0 else the error code from
1083 * function calls, or the error code passed through
1084 * ret to this function.
1087 static int gpio_request_tail(int ret, const char *nodename,
1088 struct ofnode_phandle_args *args,
1089 const char *list_name, int index,
1090 struct gpio_desc *desc, int flags,
1091 bool add_index, struct udevice *gpio_dev)
1093 gpio_desc_init(desc, gpio_dev, 0);
1098 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1101 debug("%s: uclass_get_device_by_ofnode failed\n",
1106 ret = gpio_find_and_xlate(desc, args);
1108 debug("%s: gpio_find_and_xlate failed\n", __func__);
1111 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
1112 nodename, list_name, index);
1114 debug("%s: dm_gpio_requestf failed\n", __func__);
1118 /* Keep any direction flags provided by the devicetree */
1119 ret = dm_gpio_set_dir_flags(desc,
1120 flags | (desc->flags & GPIOD_MASK_DIR));
1122 debug("%s: dm_gpio_set_dir failed\n", __func__);
1128 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
1129 __func__, nodename, list_name, index, ret);
1133 #if CONFIG_IS_ENABLED(OF_REAL)
1134 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1135 int index, struct gpio_desc *desc,
1136 int flags, bool add_index)
1138 struct ofnode_phandle_args args;
1141 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1144 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1145 index, desc, flags, add_index, NULL);
1148 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
1149 struct gpio_desc *desc, int flags)
1151 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1155 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
1156 struct gpio_desc *desc, int flags)
1158 struct ofnode_phandle_args args;
1162 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1164 node = dev_ofnode(dev);
1165 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1166 index, desc, flags, index > 0, NULL);
1169 int gpio_request_by_line_name(struct udevice *dev, const char *line_name,
1170 struct gpio_desc *desc, int flags)
1175 uclass_foreach_dev_probe(UCLASS_GPIO, dev)
1176 if (!gpio_request_by_line_name(dev, line_name, desc, flags))
1181 ret = dev_read_stringlist_search(dev, "gpio-line-names", line_name);
1189 ret = dm_gpio_request(desc, line_name);
1191 debug("%s: dm_gpio_requestf failed\n", __func__);
1195 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
1197 debug("%s: dm_gpio_set_dir failed\n", __func__);
1202 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1203 struct gpio_desc *desc, int max_count,
1209 for (count = 0; count < max_count; count++) {
1210 ret = _gpio_request_by_name_nodev(node, list_name, count,
1211 &desc[count], flags, true);
1218 /* We ran out of GPIOs in the list */
1222 gpio_free_list_nodev(desc, count);
1227 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1228 struct gpio_desc *desc, int max_count,
1232 * This isn't ideal since we don't use dev->name in the debug()
1233 * calls in gpio_request_by_name(), but we can do this until
1234 * gpio_request_list_by_name_nodev() can be dropped.
1236 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1240 int gpio_get_list_count(struct udevice *dev, const char *list_name)
1244 ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells",
1247 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1248 __func__, dev->name, list_name, ret);
1253 #endif /* OF_PLATDATA */
1255 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1256 int gpio_request_by_phandle(struct udevice *dev,
1257 const struct phandle_2_arg *cells,
1258 struct gpio_desc *desc, int flags)
1260 struct ofnode_phandle_args args;
1261 struct udevice *gpio_dev;
1262 const int index = 0;
1265 ret = device_get_by_ofplat_idx(cells->idx, &gpio_dev);
1268 args.args[0] = cells->arg[0];
1269 args.args[1] = cells->arg[1];
1271 return gpio_request_tail(ret, NULL, &args, NULL, index, desc, flags,
1272 index > 0, gpio_dev);
1276 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1278 /* For now, we don't do any checking of dev */
1279 return _dm_gpio_free(desc->dev, desc->offset);
1282 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1286 /* For now, we don't do any checking of dev */
1287 for (i = 0; i < count; i++)
1288 dm_gpio_free(dev, &desc[i]);
1293 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1295 return gpio_free_list(NULL, desc, count);
1298 /* We need to renumber the GPIOs when any driver is probed/removed */
1299 static int gpio_renumber(struct udevice *removed_dev)
1301 struct gpio_dev_priv *uc_priv;
1302 struct udevice *dev;
1307 ret = uclass_get(UCLASS_GPIO, &uc);
1311 /* Ensure that we have a base for each bank */
1313 uclass_foreach_dev(dev, uc) {
1314 if (device_active(dev) && dev != removed_dev) {
1315 uc_priv = dev_get_uclass_priv(dev);
1316 uc_priv->gpio_base = base;
1317 base += uc_priv->gpio_count;
1324 int gpio_get_number(const struct gpio_desc *desc)
1326 struct udevice *dev = desc->dev;
1327 struct gpio_dev_priv *uc_priv;
1331 uc_priv = dev_get_uclass_priv(dev);
1333 return uc_priv->gpio_base + desc->offset;
1336 static int gpio_post_probe(struct udevice *dev)
1338 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1340 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1344 return gpio_renumber(NULL);
1347 static int gpio_pre_remove(struct udevice *dev)
1349 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1352 for (i = 0; i < uc_priv->gpio_count; i++) {
1353 if (uc_priv->name[i])
1354 free(uc_priv->name[i]);
1356 free(uc_priv->name);
1358 return gpio_renumber(dev);
1361 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1362 char *list_name, int index, int flags,
1363 int dtflags, struct gpio_desc *desc)
1365 struct ofnode_phandle_args args;
1367 args.node = ofnode_null();
1368 args.args_count = 2;
1369 args.args[0] = index;
1370 args.args[1] = dtflags;
1372 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1376 static void devm_gpiod_release(struct udevice *dev, void *res)
1378 dm_gpio_free(dev, res);
1381 static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1386 struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1387 unsigned int index, int flags)
1390 struct gpio_desc *desc;
1392 static const char suffix[] = "-gpios";
1394 propname = malloc(strlen(id) + sizeof(suffix));
1400 strcpy(propname, id);
1401 strcat(propname, suffix);
1403 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1405 if (unlikely(!desc)) {
1410 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1419 devres_add(dev, desc);
1424 struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1429 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1437 void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1441 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1445 static int gpio_post_bind(struct udevice *dev)
1447 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1448 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1449 static int reloc_done;
1453 ops->request += gd->reloc_off;
1455 ops->rfree += gd->reloc_off;
1456 if (ops->direction_input)
1457 ops->direction_input += gd->reloc_off;
1458 if (ops->direction_output)
1459 ops->direction_output += gd->reloc_off;
1461 ops->get_value += gd->reloc_off;
1463 ops->set_value += gd->reloc_off;
1464 if (ops->get_function)
1465 ops->get_function += gd->reloc_off;
1467 ops->xlate += gd->reloc_off;
1469 ops->set_flags += gd->reloc_off;
1471 ops->get_flags += gd->reloc_off;
1477 if (CONFIG_IS_ENABLED(GPIO_HOG)) {
1478 struct udevice *child;
1481 dev_for_each_subnode(node, dev) {
1482 if (ofnode_read_bool(node, "gpio-hog")) {
1483 const char *name = ofnode_get_name(node);
1486 ret = device_bind_driver_to_node(dev,
1494 * Make sure gpio-hogs are probed after bind
1495 * since hogs can be essential to the hardware
1498 dev_or_flags(child, DM_FLAG_PROBE_AFTER_BIND);
1506 UCLASS_DRIVER(gpio) = {
1509 .flags = DM_UC_FLAG_SEQ_ALIAS,
1510 .post_probe = gpio_post_probe,
1511 .post_bind = gpio_post_bind,
1512 .pre_remove = gpio_pre_remove,
1513 .per_device_auto = sizeof(struct gpio_dev_priv),