1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
6 #define LOG_CATEGORY UCLASS_GPIO
11 #include <dm/devres.h>
12 #include <dm/device_compat.h>
13 #include <dm/device-internal.h>
15 #include <dm/uclass-internal.h>
16 #include <dt-bindings/gpio/gpio.h>
20 #include <acpi/acpi_device.h>
21 #include <asm/global_data.h>
23 #include <dm/device_compat.h>
24 #include <linux/bug.h>
25 #include <linux/ctype.h>
26 #include <linux/delay.h>
28 DECLARE_GLOBAL_DATA_PTR;
31 * gpio_desc_init() - Initialize the GPIO descriptor
33 * @desc: GPIO descriptor to initialize
35 * @offset: Offset of device GPIO
37 static void gpio_desc_init(struct gpio_desc *desc,
42 desc->offset = offset;
47 * gpio_to_device() - Convert global GPIO number to device, number
49 * Convert the GPIO number to an entry in the list of GPIOs
50 * or GPIO blocks registered with the GPIO controller. Returns
51 * entry on success, NULL on error.
53 * @gpio: The numeric representation of the GPIO
54 * @desc: Returns description (desc->flags will always be 0)
55 * @return 0 if found, -ENOENT if not found
57 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
59 struct gpio_dev_priv *uc_priv;
63 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
65 ret = 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);
75 return ret ? ret : -ENOENT;
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)
98 for (i = 0; i < uc_priv->gpio_count; i++) {
99 if (!uc_priv->name[i])
101 if (!strncmp(name, uc_priv->name[i], len)) {
110 dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
117 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
119 struct gpio_dev_priv *uc_priv = NULL;
125 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
126 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
128 ret = uclass_next_device(&dev)) {
131 uc_priv = dev_get_uclass_priv(dev);
133 offset = numeric - uc_priv->gpio_base;
134 /* Allow GPIOs to be numbered from 0 */
135 if (offset < uc_priv->gpio_count)
139 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
141 if (!strncasecmp(name, uc_priv->bank_name, len)) {
142 if (!strict_strtoul(name + len, 10, &offset))
147 * if we did not found a gpio through its bank
148 * name, we search for a valid gpio label.
150 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
155 return ret ? ret : -EINVAL;
157 gpio_desc_init(desc, dev, offset);
162 int gpio_lookup_name(const char *name, struct udevice **devp,
163 unsigned int *offsetp, unsigned int *gpiop)
165 struct gpio_desc desc;
170 ret = dm_gpio_lookup_name(name, &desc);
177 *offsetp = desc.offset;
179 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
181 *gpiop = uc_priv->gpio_base + desc.offset;
187 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
188 struct ofnode_phandle_args *args)
190 if (args->args_count < 1)
193 desc->offset = args->args[0];
195 if (args->args_count < 2)
199 if (args->args[1] & GPIO_ACTIVE_LOW)
200 desc->flags |= GPIOD_ACTIVE_LOW;
203 * need to test 2 bits for gpio output binding:
204 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
205 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
207 if (args->args[1] & GPIO_SINGLE_ENDED) {
208 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
209 desc->flags |= GPIOD_OPEN_DRAIN;
211 desc->flags |= GPIOD_OPEN_SOURCE;
214 if (args->args[1] & GPIO_PULL_UP)
215 desc->flags |= GPIOD_PULL_UP;
217 if (args->args[1] & GPIO_PULL_DOWN)
218 desc->flags |= GPIOD_PULL_DOWN;
223 static int gpio_find_and_xlate(struct gpio_desc *desc,
224 struct ofnode_phandle_args *args)
226 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
229 return ops->xlate(desc->dev, desc, args);
231 return gpio_xlate_offs_flags(desc->dev, desc, args);
234 #if defined(CONFIG_GPIO_HOG)
236 struct gpio_hog_priv {
237 struct gpio_desc gpiod;
240 struct gpio_hog_data {
246 static int gpio_hog_of_to_plat(struct udevice *dev)
248 struct gpio_hog_data *plat = dev_get_plat(dev);
249 const char *nodename;
253 if (dev_read_bool(dev, "input")) {
254 plat->gpiod_flags = GPIOD_IS_IN;
255 } else if (dev_read_bool(dev, "output-high")) {
257 plat->gpiod_flags = GPIOD_IS_OUT;
258 } else if (dev_read_bool(dev, "output-low")) {
259 plat->gpiod_flags = GPIOD_IS_OUT;
261 printf("%s: missing gpio-hog state.\n", __func__);
264 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
266 printf("%s: wrong gpios property, 2 values needed %d\n",
270 nodename = dev_read_string(dev, "line-name");
272 device_set_name(dev, nodename);
277 static int gpio_hog_probe(struct udevice *dev)
279 struct gpio_hog_data *plat = dev_get_plat(dev);
280 struct gpio_hog_priv *priv = dev_get_priv(dev);
283 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
284 plat->val[0], plat->gpiod_flags,
285 plat->val[1], &priv->gpiod);
287 debug("%s: node %s could not get gpio.\n", __func__,
292 if (plat->gpiod_flags == GPIOD_IS_OUT) {
293 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
295 debug("%s: node %s could not set gpio.\n", __func__,
304 int gpio_hog_probe_all(void)
310 for (uclass_first_device(UCLASS_NOP, &dev);
312 uclass_find_next_device(&dev)) {
313 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
314 ret = device_probe(dev);
316 printf("Failed to probe device %s err: %d\n",
326 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
331 gpio_hog_probe_all();
332 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
333 struct gpio_hog_priv *priv = dev_get_priv(dev);
335 *desc = &priv->gpiod;
342 U_BOOT_DRIVER(gpio_hog) = {
345 .of_to_plat = gpio_hog_of_to_plat,
346 .probe = gpio_hog_probe,
347 .priv_auto = sizeof(struct gpio_hog_priv),
348 .plat_auto = sizeof(struct gpio_hog_data),
351 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
357 int dm_gpio_request(struct gpio_desc *desc, const char *label)
359 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
360 struct udevice *dev = desc->dev;
361 struct gpio_dev_priv *uc_priv;
365 uc_priv = dev_get_uclass_priv(dev);
366 if (uc_priv->name[desc->offset])
372 ret = ops->request(dev, desc->offset, label);
378 uc_priv->name[desc->offset] = str;
383 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
385 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
390 vscnprintf(buf, sizeof(buf), fmt, args);
392 return dm_gpio_request(desc, buf);
394 return dm_gpio_request(desc, fmt);
399 * gpio_request() - [COMPAT] Request GPIO
401 * label: Name for the requested GPIO
403 * The label is copied and allocated so the caller does not need to keep
404 * the pointer around.
406 * This function implements the API that's compatible with current
407 * GPIO API used in U-Boot. The request is forwarded to particular
408 * GPIO driver. Returns 0 on success, negative value on error.
410 int gpio_request(unsigned gpio, const char *label)
412 struct gpio_desc desc;
415 ret = gpio_to_device(gpio, &desc);
419 return dm_gpio_request(&desc, label);
423 * gpio_requestf() - [COMPAT] Request GPIO
425 * @fmt: Format string for the requested GPIO
426 * @...: Arguments for the printf() format string
428 * This function implements the API that's compatible with current
429 * GPIO API used in U-Boot. The request is forwarded to particular
430 * GPIO driver. Returns 0 on success, negative value on error.
432 int gpio_requestf(unsigned gpio, const char *fmt, ...)
434 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
439 vscnprintf(buf, sizeof(buf), fmt, args);
441 return gpio_request(gpio, buf);
443 return gpio_request(gpio, fmt);
447 int _dm_gpio_free(struct udevice *dev, uint offset)
449 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
450 struct gpio_dev_priv *uc_priv;
453 uc_priv = dev_get_uclass_priv(dev);
454 if (!uc_priv->name[offset])
457 ret = ops->rfree(dev, offset);
462 free(uc_priv->name[offset]);
463 uc_priv->name[offset] = NULL;
469 * gpio_free() - [COMPAT] Relinquish GPIO
472 * This function implements the API that's compatible with current
473 * GPIO API used in U-Boot. The request is forwarded to particular
474 * GPIO driver. Returns 0 on success, negative value on error.
476 int gpio_free(unsigned gpio)
478 struct gpio_desc desc;
481 ret = gpio_to_device(gpio, &desc);
485 return _dm_gpio_free(desc.dev, desc.offset);
488 static int check_reserved(const struct gpio_desc *desc, const char *func)
490 struct gpio_dev_priv *uc_priv;
492 if (!dm_gpio_is_valid(desc))
495 uc_priv = dev_get_uclass_priv(desc->dev);
496 if (!uc_priv->name[desc->offset]) {
497 printf("%s: %s: error: gpio %s%d not reserved\n",
498 desc->dev->name, func,
499 uc_priv->bank_name ? uc_priv->bank_name : "",
508 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
511 * This function implements the API that's compatible with current
512 * GPIO API used in U-Boot. The request is forwarded to particular
513 * GPIO driver. Returns 0 on success, negative value on error.
515 int gpio_direction_input(unsigned gpio)
517 struct gpio_desc desc;
520 ret = gpio_to_device(gpio, &desc);
524 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
528 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
530 * value: Logical value to be set on the GPIO pin
532 * This function implements the API that's compatible with current
533 * GPIO API used in U-Boot. The request is forwarded to particular
534 * GPIO driver. Returns 0 on success, negative value on error.
536 int gpio_direction_output(unsigned gpio, int value)
538 struct gpio_desc desc;
542 ret = gpio_to_device(gpio, &desc);
546 flags = GPIOD_IS_OUT;
548 flags |= GPIOD_IS_OUT_ACTIVE;
549 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
552 static int _gpio_get_value(const struct gpio_desc *desc)
554 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
557 value = ops->get_value(desc->dev, desc->offset);
559 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
562 int dm_gpio_get_value(const struct gpio_desc *desc)
566 ret = check_reserved(desc, "get_value");
570 return _gpio_get_value(desc);
573 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
575 const struct dm_gpio_ops *ops;
578 ret = check_reserved(desc, "set_value");
582 if (desc->flags & GPIOD_ACTIVE_LOW)
585 /* GPIOD_ are directly managed by driver in set_flags */
586 ops = gpio_get_ops(desc->dev);
587 if (ops->set_flags) {
588 ulong flags = desc->flags;
591 flags |= GPIOD_IS_OUT_ACTIVE;
593 flags &= ~GPIOD_IS_OUT_ACTIVE;
594 return ops->set_flags(desc->dev, desc->offset, flags);
598 * Emulate open drain by not actively driving the line high or
599 * Emulate open source by not actively driving the line low
601 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
602 (desc->flags & GPIOD_OPEN_SOURCE && !value))
603 return ops->direction_input(desc->dev, desc->offset);
604 else if (desc->flags & GPIOD_OPEN_DRAIN ||
605 desc->flags & GPIOD_OPEN_SOURCE)
606 return ops->direction_output(desc->dev, desc->offset, value);
608 ret = ops->set_value(desc->dev, desc->offset, value);
615 /* check dir flags invalid configuration */
616 static int check_dir_flags(ulong flags)
618 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
619 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
624 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
625 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
630 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
631 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
640 * _dm_gpio_set_flags() - Send flags to the driver
642 * This uses the best available method to send the given flags to the driver.
643 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
644 * of GPIOD_IS_OUT_ACTIVE.
646 * @desc: GPIO description
647 * @flags: flags value to set
648 * @return 0 if OK, -ve on error
650 static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
652 struct udevice *dev = desc->dev;
653 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
654 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
657 ret = check_dir_flags(flags);
660 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
662 uc_priv->bank_name ? uc_priv->bank_name : "",
663 desc->offset, flags);
668 /* If active low, invert the output state */
669 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
670 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
671 flags ^= GPIOD_IS_OUT_ACTIVE;
673 /* GPIOD_ are directly managed by driver in set_flags */
674 if (ops->set_flags) {
675 ret = ops->set_flags(dev, desc->offset, flags);
677 if (flags & GPIOD_IS_OUT) {
678 bool value = flags & GPIOD_IS_OUT_ACTIVE;
680 ret = ops->direction_output(dev, desc->offset, value);
681 } else if (flags & GPIOD_IS_IN) {
682 ret = ops->direction_input(dev, desc->offset);
689 int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
694 ret = check_reserved(desc, "set_dir_flags");
698 flags = (desc->flags & ~clr) | set;
700 ret = _dm_gpio_set_flags(desc, flags);
704 /* save the flags also in descriptor */
710 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
712 /* combine the requested flags (for IN/OUT) and the descriptor flags */
713 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
716 int dm_gpio_set_dir(struct gpio_desc *desc)
720 ret = check_reserved(desc, "set_dir");
724 return _dm_gpio_set_flags(desc, desc->flags);
727 int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
733 for (i = 0; i < count; i++) {
734 ret = dm_gpio_clrset_flags(&desc[i], clr, set);
742 int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
744 struct udevice *dev = desc->dev;
746 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
749 ret = check_reserved(desc, "get_flags");
753 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
754 if (ops->get_flags) {
755 ret = ops->get_flags(dev, desc->offset, &flags);
759 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
760 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
761 if (desc->flags & GPIOD_ACTIVE_LOW)
763 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
764 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
766 flags |= GPIOD_IS_OUT_ACTIVE;
769 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
770 flags &= ~GPIOD_IS_OUT_ACTIVE;
771 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
772 flags |= GPIOD_IS_OUT_ACTIVE;
780 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
783 * This function implements the API that's compatible with current
784 * GPIO API used in U-Boot. The request is forwarded to particular
785 * GPIO driver. Returns the value of the GPIO pin, or negative value
788 int gpio_get_value(unsigned gpio)
792 struct gpio_desc desc;
794 ret = gpio_to_device(gpio, &desc);
797 return dm_gpio_get_value(&desc);
801 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
803 * value: Logical value to be set on the GPIO pin.
805 * This function implements the API that's compatible with current
806 * GPIO API used in U-Boot. The request is forwarded to particular
807 * GPIO driver. Returns 0 on success, negative value on error.
809 int gpio_set_value(unsigned gpio, int value)
811 struct gpio_desc desc;
814 ret = gpio_to_device(gpio, &desc);
817 return dm_gpio_set_value(&desc, value);
820 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
822 struct gpio_dev_priv *priv;
824 /* Must be called on an active device */
825 priv = dev_get_uclass_priv(dev);
828 *bit_count = priv->gpio_count;
829 return priv->bank_name;
832 static const char * const gpio_function[GPIOF_COUNT] = {
840 static int get_function(struct udevice *dev, int offset, bool skip_unused,
843 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
844 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
846 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
847 if (!device_active(dev))
849 if (offset < 0 || offset >= uc_priv->gpio_count)
852 *namep = uc_priv->name[offset];
853 if (skip_unused && !uc_priv->name[offset])
855 if (ops->get_function) {
858 ret = ops->get_function(dev, offset);
861 if (ret >= ARRAY_SIZE(gpio_function))
866 return GPIOF_UNKNOWN;
869 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
871 return get_function(dev, offset, true, namep);
874 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
876 return get_function(dev, offset, false, namep);
879 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
881 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
882 struct gpio_dev_priv *priv;
888 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
891 priv = dev_get_uclass_priv(dev);
892 ret = gpio_get_raw_function(dev, offset, NULL);
896 len = snprintf(str, buffsize, "%s%d: %s",
897 priv->bank_name ? priv->bank_name : "",
898 offset, gpio_function[func]);
899 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
900 func == GPIOF_UNUSED) {
904 ret = ops->get_value(dev, offset);
907 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
908 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
918 #if CONFIG_IS_ENABLED(ACPIGEN)
919 int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
921 const struct dm_gpio_ops *ops;
923 memset(gpio, '\0', sizeof(*gpio));
924 if (!dm_gpio_is_valid(desc)) {
925 /* Indicate that the GPIO is not valid */
931 ops = gpio_get_ops(desc->dev);
935 return ops->get_acpi(desc, gpio);
939 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
944 for (i = 0; i < 32; i++) {
945 gpio = gpio_num_array[i];
948 ret = gpio_requestf(gpio, fmt, i);
951 ret = gpio_direction_input(gpio);
960 for (i--; i >= 0; i--)
961 gpio_free(gpio_num_array[i]);
967 * get a number comprised of multiple GPIO values. gpio_num_array points to
968 * the array of gpio pin numbers to scan, terminated by -1.
970 int gpio_get_values_as_int(const int *gpio_list)
973 unsigned bitmask = 1;
978 ((gpio = *gpio_list++) != -1)) {
979 ret = gpio_get_value(gpio);
990 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
992 unsigned bitmask = 1;
996 for (i = 0; i < count; i++) {
997 ret = dm_gpio_get_value(&desc_list[i]);
1008 int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
1011 static const char tristate[] = "01z";
1018 int vals[NUM_OPTIONS];
1024 * Limit to 19 digits which should be plenty. This avoids overflow of a
1029 for (i = 0; i < NUM_OPTIONS; i++) {
1030 uint flags = GPIOD_IS_IN;
1032 flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
1033 ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
1036 return log_msg_ret("pu", ret);
1038 /* Give the lines time to settle */
1041 ret = dm_gpio_get_values_as_int(desc_list, count);
1043 return log_msg_ret("get1", ret);
1047 log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
1048 for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
1049 uint pd = vals[PULLDOWN] & mask ? 1 : 0;
1050 uint pu = vals[PULLUP] & mask ? 1 : 0;
1054 * Get value with internal pulldown active. If this is 1 then
1055 * there is a stronger external pullup, which we call 1. If not
1058 * If the values differ then the pin is floating so we call
1065 log_debug("%c ", tristate[digit]);
1066 vector = 3 * vector + digit;
1068 log_debug("vector=%d\n", vector);
1074 * gpio_request_tail: common work for requesting a gpio.
1076 * ret: return value from previous work in function which calls
1078 * This seems bogus (why calling this function instead not
1079 * calling it and end caller function instead?).
1080 * Because on error in caller function we want to set some
1081 * default values in gpio desc and have a common error
1082 * debug message, which provides this function.
1083 * nodename: Name of node for which gpio gets requested
1084 * used for gpio label name.
1085 * args: pointer to output arguments structure
1086 * list_name: Name of GPIO list
1087 * used for gpio label name.
1088 * index: gpio index in gpio list
1089 * used for gpio label name.
1090 * desc: pointer to gpio descriptor, filled from this
1092 * flags: gpio flags to use.
1093 * add_index: should index added to gpio label name
1094 * gpio_dev: pointer to gpio device from which the gpio
1095 * will be requested. If NULL try to get the
1096 * gpio device with uclass_get_device_by_ofnode()
1098 * return: In error case this function sets default values in
1099 * gpio descriptor, also emmits a debug message.
1100 * On success it returns 0 else the error code from
1101 * function calls, or the error code passed through
1102 * ret to this function.
1105 static int gpio_request_tail(int ret, const char *nodename,
1106 struct ofnode_phandle_args *args,
1107 const char *list_name, int index,
1108 struct gpio_desc *desc, int flags,
1109 bool add_index, struct udevice *gpio_dev)
1111 gpio_desc_init(desc, gpio_dev, 0);
1116 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1119 debug("%s: uclass_get_device_by_ofnode failed\n",
1124 ret = gpio_find_and_xlate(desc, args);
1126 debug("%s: gpio_find_and_xlate failed\n", __func__);
1129 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
1130 nodename, list_name, index);
1132 debug("%s: dm_gpio_requestf failed\n", __func__);
1136 /* Keep any direction flags provided by the devicetree */
1137 ret = dm_gpio_set_dir_flags(desc,
1138 flags | (desc->flags & GPIOD_MASK_DIR));
1140 debug("%s: dm_gpio_set_dir failed\n", __func__);
1146 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
1147 __func__, nodename, list_name, index, ret);
1151 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1152 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1153 int index, struct gpio_desc *desc,
1154 int flags, bool add_index)
1156 struct ofnode_phandle_args args;
1159 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1162 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1163 index, desc, flags, add_index, NULL);
1166 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
1167 struct gpio_desc *desc, int flags)
1169 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1173 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
1174 struct gpio_desc *desc, int flags)
1176 struct ofnode_phandle_args args;
1180 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1182 node = dev_ofnode(dev);
1183 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1184 index, desc, flags, index > 0, NULL);
1187 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1188 struct gpio_desc *desc, int max_count,
1194 for (count = 0; count < max_count; count++) {
1195 ret = _gpio_request_by_name_nodev(node, list_name, count,
1196 &desc[count], flags, true);
1203 /* We ran out of GPIOs in the list */
1207 gpio_free_list_nodev(desc, count - 1);
1212 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1213 struct gpio_desc *desc, int max_count,
1217 * This isn't ideal since we don't use dev->name in the debug()
1218 * calls in gpio_request_by_name(), but we can do this until
1219 * gpio_request_list_by_name_nodev() can be dropped.
1221 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1225 int gpio_get_list_count(struct udevice *dev, const char *list_name)
1229 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, -1,
1232 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1233 __func__, dev->name, list_name, ret);
1238 #endif /* OF_PLATDATA */
1240 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1242 /* For now, we don't do any checking of dev */
1243 return _dm_gpio_free(desc->dev, desc->offset);
1246 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1250 /* For now, we don't do any checking of dev */
1251 for (i = 0; i < count; i++)
1252 dm_gpio_free(dev, &desc[i]);
1257 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1259 return gpio_free_list(NULL, desc, count);
1262 /* We need to renumber the GPIOs when any driver is probed/removed */
1263 static int gpio_renumber(struct udevice *removed_dev)
1265 struct gpio_dev_priv *uc_priv;
1266 struct udevice *dev;
1271 ret = uclass_get(UCLASS_GPIO, &uc);
1275 /* Ensure that we have a base for each bank */
1277 uclass_foreach_dev(dev, uc) {
1278 if (device_active(dev) && dev != removed_dev) {
1279 uc_priv = dev_get_uclass_priv(dev);
1280 uc_priv->gpio_base = base;
1281 base += uc_priv->gpio_count;
1288 int gpio_get_number(const struct gpio_desc *desc)
1290 struct udevice *dev = desc->dev;
1291 struct gpio_dev_priv *uc_priv;
1295 uc_priv = dev_get_uclass_priv(dev);
1297 return uc_priv->gpio_base + desc->offset;
1300 static int gpio_post_probe(struct udevice *dev)
1302 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1304 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1308 return gpio_renumber(NULL);
1311 static int gpio_pre_remove(struct udevice *dev)
1313 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1316 for (i = 0; i < uc_priv->gpio_count; i++) {
1317 if (uc_priv->name[i])
1318 free(uc_priv->name[i]);
1320 free(uc_priv->name);
1322 return gpio_renumber(dev);
1325 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1326 char *list_name, int index, int flags,
1327 int dtflags, struct gpio_desc *desc)
1329 struct ofnode_phandle_args args;
1331 args.node = ofnode_null();
1332 args.args_count = 2;
1333 args.args[0] = index;
1334 args.args[1] = dtflags;
1336 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1340 static void devm_gpiod_release(struct udevice *dev, void *res)
1342 dm_gpio_free(dev, res);
1345 static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1350 struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1351 unsigned int index, int flags)
1354 struct gpio_desc *desc;
1356 static const char suffix[] = "-gpios";
1358 propname = malloc(strlen(id) + sizeof(suffix));
1364 strcpy(propname, id);
1365 strcat(propname, suffix);
1367 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1369 if (unlikely(!desc)) {
1374 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1383 devres_add(dev, desc);
1388 struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1393 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1401 void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1405 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1409 static int gpio_post_bind(struct udevice *dev)
1411 struct udevice *child;
1414 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1415 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1416 static int reloc_done;
1420 ops->request += gd->reloc_off;
1422 ops->rfree += gd->reloc_off;
1423 if (ops->direction_input)
1424 ops->direction_input += gd->reloc_off;
1425 if (ops->direction_output)
1426 ops->direction_output += gd->reloc_off;
1428 ops->get_value += gd->reloc_off;
1430 ops->set_value += gd->reloc_off;
1431 if (ops->get_function)
1432 ops->get_function += gd->reloc_off;
1434 ops->xlate += gd->reloc_off;
1436 ops->set_flags += gd->reloc_off;
1438 ops->get_flags += gd->reloc_off;
1444 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1445 dev_for_each_subnode(node, dev) {
1446 if (ofnode_read_bool(node, "gpio-hog")) {
1447 const char *name = ofnode_get_name(node);
1450 ret = device_bind_driver_to_node(dev,
1462 UCLASS_DRIVER(gpio) = {
1465 .flags = DM_UC_FLAG_SEQ_ALIAS,
1466 .post_probe = gpio_post_probe,
1467 .post_bind = gpio_post_bind,
1468 .pre_remove = gpio_pre_remove,
1469 .per_device_auto = sizeof(struct gpio_dev_priv),