1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
9 #include <dm/device-internal.h>
11 #include <dm/uclass-internal.h>
12 #include <dt-bindings/gpio/gpio.h>
16 #include <acpi/acpi_device.h>
18 #include <dm/device_compat.h>
19 #include <linux/bug.h>
20 #include <linux/ctype.h>
22 DECLARE_GLOBAL_DATA_PTR;
25 * gpio_desc_init() - Initialize the GPIO descriptor
27 * @desc: GPIO descriptor to initialize
29 * @offset: Offset of device GPIO
31 static void gpio_desc_init(struct gpio_desc *desc,
36 desc->offset = offset;
41 * gpio_to_device() - Convert global GPIO number to device, number
43 * Convert the GPIO number to an entry in the list of GPIOs
44 * or GPIO blocks registered with the GPIO controller. Returns
45 * entry on success, NULL on error.
47 * @gpio: The numeric representation of the GPIO
48 * @desc: Returns description (desc->flags will always be 0)
49 * @return 0 if found, -ENOENT if not found
51 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
53 struct gpio_dev_priv *uc_priv;
57 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
59 ret = uclass_next_device(&dev)) {
60 uc_priv = dev_get_uclass_priv(dev);
61 if (gpio >= uc_priv->gpio_base &&
62 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
63 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
69 return ret ? ret : -ENOENT;
72 #if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
74 * dm_gpio_lookup_label() - look for name in gpio device
76 * search in uc_priv, if there is a gpio with labelname same
79 * @name: name which is searched
80 * @uc_priv: gpio_dev_priv pointer.
81 * @offset: gpio offset within the device
82 * @return: 0 if found, -ENOENT if not.
84 static int dm_gpio_lookup_label(const char *name,
85 struct gpio_dev_priv *uc_priv, ulong *offset)
92 for (i = 0; i < uc_priv->gpio_count; i++) {
93 if (!uc_priv->name[i])
95 if (!strncmp(name, uc_priv->name[i], len)) {
104 dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
111 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
113 struct gpio_dev_priv *uc_priv = NULL;
119 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
120 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
122 ret = uclass_next_device(&dev)) {
125 uc_priv = dev_get_uclass_priv(dev);
127 offset = numeric - uc_priv->gpio_base;
128 /* Allow GPIOs to be numbered from 0 */
129 if (offset < uc_priv->gpio_count)
133 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
135 if (!strncasecmp(name, uc_priv->bank_name, len)) {
136 if (!strict_strtoul(name + len, 10, &offset))
141 * if we did not found a gpio through its bank
142 * name, we search for a valid gpio label.
144 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
149 return ret ? ret : -EINVAL;
151 gpio_desc_init(desc, dev, offset);
156 int gpio_lookup_name(const char *name, struct udevice **devp,
157 unsigned int *offsetp, unsigned int *gpiop)
159 struct gpio_desc desc;
164 ret = dm_gpio_lookup_name(name, &desc);
171 *offsetp = desc.offset;
173 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
175 *gpiop = uc_priv->gpio_base + desc.offset;
181 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
182 struct ofnode_phandle_args *args)
184 if (args->args_count < 1)
187 desc->offset = args->args[0];
189 if (args->args_count < 2)
193 if (args->args[1] & GPIO_ACTIVE_LOW)
194 desc->flags |= GPIOD_ACTIVE_LOW;
197 * need to test 2 bits for gpio output binding:
198 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
199 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
201 if (args->args[1] & GPIO_SINGLE_ENDED) {
202 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
203 desc->flags |= GPIOD_OPEN_DRAIN;
205 desc->flags |= GPIOD_OPEN_SOURCE;
208 if (args->args[1] & GPIO_PULL_UP)
209 desc->flags |= GPIOD_PULL_UP;
211 if (args->args[1] & GPIO_PULL_DOWN)
212 desc->flags |= GPIOD_PULL_DOWN;
217 static int gpio_find_and_xlate(struct gpio_desc *desc,
218 struct ofnode_phandle_args *args)
220 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
223 return ops->xlate(desc->dev, desc, args);
225 return gpio_xlate_offs_flags(desc->dev, desc, args);
228 #if defined(CONFIG_GPIO_HOG)
230 struct gpio_hog_priv {
231 struct gpio_desc gpiod;
234 struct gpio_hog_data {
240 static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
242 struct gpio_hog_data *plat = dev_get_platdata(dev);
243 const char *nodename;
247 if (dev_read_bool(dev, "input")) {
248 plat->gpiod_flags = GPIOD_IS_IN;
249 } else if (dev_read_bool(dev, "output-high")) {
251 plat->gpiod_flags = GPIOD_IS_OUT;
252 } else if (dev_read_bool(dev, "output-low")) {
253 plat->gpiod_flags = GPIOD_IS_OUT;
255 printf("%s: missing gpio-hog state.\n", __func__);
258 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
260 printf("%s: wrong gpios property, 2 values needed %d\n",
264 nodename = dev_read_string(dev, "line-name");
266 device_set_name(dev, nodename);
271 static int gpio_hog_probe(struct udevice *dev)
273 struct gpio_hog_data *plat = dev_get_platdata(dev);
274 struct gpio_hog_priv *priv = dev_get_priv(dev);
277 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
278 plat->val[0], plat->gpiod_flags,
279 plat->val[1], &priv->gpiod);
281 debug("%s: node %s could not get gpio.\n", __func__,
286 if (plat->gpiod_flags == GPIOD_IS_OUT) {
287 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
289 debug("%s: node %s could not set gpio.\n", __func__,
298 int gpio_hog_probe_all(void)
304 for (uclass_first_device(UCLASS_NOP, &dev);
306 uclass_find_next_device(&dev)) {
307 if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
308 ret = device_probe(dev);
310 printf("Failed to probe device %s err: %d\n",
320 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
325 gpio_hog_probe_all();
326 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
327 struct gpio_hog_priv *priv = dev_get_priv(dev);
329 *desc = &priv->gpiod;
336 U_BOOT_DRIVER(gpio_hog) = {
339 .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
340 .probe = gpio_hog_probe,
341 .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
342 .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
345 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
351 int dm_gpio_request(struct gpio_desc *desc, const char *label)
353 struct udevice *dev = desc->dev;
354 struct gpio_dev_priv *uc_priv;
358 uc_priv = dev_get_uclass_priv(dev);
359 if (uc_priv->name[desc->offset])
364 if (gpio_get_ops(dev)->request) {
365 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
371 uc_priv->name[desc->offset] = str;
376 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
378 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
383 vscnprintf(buf, sizeof(buf), fmt, args);
385 return dm_gpio_request(desc, buf);
387 return dm_gpio_request(desc, fmt);
392 * gpio_request() - [COMPAT] Request GPIO
394 * label: Name for the requested GPIO
396 * The label is copied and allocated so the caller does not need to keep
397 * the pointer around.
399 * This function implements the API that's compatible with current
400 * GPIO API used in U-Boot. The request is forwarded to particular
401 * GPIO driver. Returns 0 on success, negative value on error.
403 int gpio_request(unsigned gpio, const char *label)
405 struct gpio_desc desc;
408 ret = gpio_to_device(gpio, &desc);
412 return dm_gpio_request(&desc, label);
416 * gpio_requestf() - [COMPAT] Request GPIO
418 * @fmt: Format string for the requested GPIO
419 * @...: Arguments for the printf() format string
421 * This function implements the API that's compatible with current
422 * GPIO API used in U-Boot. The request is forwarded to particular
423 * GPIO driver. Returns 0 on success, negative value on error.
425 int gpio_requestf(unsigned gpio, const char *fmt, ...)
427 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
432 vscnprintf(buf, sizeof(buf), fmt, args);
434 return gpio_request(gpio, buf);
436 return gpio_request(gpio, fmt);
440 int _dm_gpio_free(struct udevice *dev, uint offset)
442 struct gpio_dev_priv *uc_priv;
445 uc_priv = dev_get_uclass_priv(dev);
446 if (!uc_priv->name[offset])
448 if (gpio_get_ops(dev)->rfree) {
449 ret = gpio_get_ops(dev)->rfree(dev, offset);
454 free(uc_priv->name[offset]);
455 uc_priv->name[offset] = NULL;
461 * gpio_free() - [COMPAT] Relinquish GPIO
464 * This function implements the API that's compatible with current
465 * GPIO API used in U-Boot. The request is forwarded to particular
466 * GPIO driver. Returns 0 on success, negative value on error.
468 int gpio_free(unsigned gpio)
470 struct gpio_desc desc;
473 ret = gpio_to_device(gpio, &desc);
477 return _dm_gpio_free(desc.dev, desc.offset);
480 static int check_reserved(const struct gpio_desc *desc, const char *func)
482 struct gpio_dev_priv *uc_priv;
484 if (!dm_gpio_is_valid(desc))
487 uc_priv = dev_get_uclass_priv(desc->dev);
488 if (!uc_priv->name[desc->offset]) {
489 printf("%s: %s: error: gpio %s%d not reserved\n",
490 desc->dev->name, func,
491 uc_priv->bank_name ? uc_priv->bank_name : "",
500 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
503 * This function implements the API that's compatible with current
504 * GPIO API used in U-Boot. The request is forwarded to particular
505 * GPIO driver. Returns 0 on success, negative value on error.
507 int gpio_direction_input(unsigned gpio)
509 struct gpio_desc desc;
512 ret = gpio_to_device(gpio, &desc);
515 ret = check_reserved(&desc, "dir_input");
519 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
523 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
525 * value: Logical value to be set on the GPIO pin
527 * This function implements the API that's compatible with current
528 * GPIO API used in U-Boot. The request is forwarded to particular
529 * GPIO driver. Returns 0 on success, negative value on error.
531 int gpio_direction_output(unsigned gpio, int value)
533 struct gpio_desc desc;
536 ret = gpio_to_device(gpio, &desc);
539 ret = check_reserved(&desc, "dir_output");
543 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
547 static int _gpio_get_value(const struct gpio_desc *desc)
551 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
553 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
556 int dm_gpio_get_value(const struct gpio_desc *desc)
560 ret = check_reserved(desc, "get_value");
564 return _gpio_get_value(desc);
567 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
571 ret = check_reserved(desc, "set_value");
575 if (desc->flags & GPIOD_ACTIVE_LOW)
579 * Emulate open drain by not actively driving the line high or
580 * Emulate open source by not actively driving the line low
582 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
583 (desc->flags & GPIOD_OPEN_SOURCE && !value))
584 return gpio_get_ops(desc->dev)->direction_input(desc->dev,
586 else if (desc->flags & GPIOD_OPEN_DRAIN ||
587 desc->flags & GPIOD_OPEN_SOURCE)
588 return gpio_get_ops(desc->dev)->direction_output(desc->dev,
592 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
596 /* check dir flags invalid configuration */
597 static int check_dir_flags(ulong flags)
599 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
600 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
605 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
606 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
611 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
612 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
620 static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
622 struct udevice *dev = desc->dev;
623 struct dm_gpio_ops *ops = gpio_get_ops(dev);
624 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
627 ret = check_dir_flags(flags);
630 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
632 uc_priv->bank_name ? uc_priv->bank_name : "",
633 desc->offset, flags);
638 /* GPIOD_ are directly managed by driver in set_dir_flags*/
639 if (ops->set_dir_flags) {
640 ret = ops->set_dir_flags(dev, desc->offset, flags);
642 if (flags & GPIOD_IS_OUT) {
643 ret = ops->direction_output(dev, desc->offset,
644 GPIOD_FLAGS_OUTPUT(flags));
645 } else if (flags & GPIOD_IS_IN) {
646 ret = ops->direction_input(dev, desc->offset);
650 /* save the flags also in descriptor */
657 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
661 ret = check_reserved(desc, "set_dir_flags");
665 /* combine the requested flags (for IN/OUT) and the descriptor flags */
666 flags |= desc->flags;
667 ret = _dm_gpio_set_dir_flags(desc, flags);
672 int dm_gpio_set_dir(struct gpio_desc *desc)
676 ret = check_reserved(desc, "set_dir");
680 return _dm_gpio_set_dir_flags(desc, desc->flags);
683 int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags)
685 struct udevice *dev = desc->dev;
687 struct dm_gpio_ops *ops = gpio_get_ops(dev);
690 ret = check_reserved(desc, "get_dir_flags");
694 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
695 if (ops->get_dir_flags) {
696 ret = ops->get_dir_flags(dev, desc->offset, &dir_flags);
700 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
701 value = dir_flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
702 if (desc->flags & GPIOD_ACTIVE_LOW)
704 dir_flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
705 dir_flags |= (desc->flags & GPIOD_ACTIVE_LOW);
707 dir_flags |= GPIOD_IS_OUT_ACTIVE;
709 dir_flags = desc->flags;
710 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
711 dir_flags &= ~GPIOD_IS_OUT_ACTIVE;
712 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
713 dir_flags |= GPIOD_IS_OUT_ACTIVE;
721 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
724 * This function implements the API that's compatible with current
725 * GPIO API used in U-Boot. The request is forwarded to particular
726 * GPIO driver. Returns the value of the GPIO pin, or negative value
729 int gpio_get_value(unsigned gpio)
733 struct gpio_desc desc;
735 ret = gpio_to_device(gpio, &desc);
738 return dm_gpio_get_value(&desc);
742 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
744 * value: Logical value to be set on the GPIO pin.
746 * This function implements the API that's compatible with current
747 * GPIO API used in U-Boot. The request is forwarded to particular
748 * GPIO driver. Returns 0 on success, negative value on error.
750 int gpio_set_value(unsigned gpio, int value)
752 struct gpio_desc desc;
755 ret = gpio_to_device(gpio, &desc);
758 return dm_gpio_set_value(&desc, value);
761 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
763 struct gpio_dev_priv *priv;
765 /* Must be called on an active device */
766 priv = dev_get_uclass_priv(dev);
769 *bit_count = priv->gpio_count;
770 return priv->bank_name;
773 static const char * const gpio_function[GPIOF_COUNT] = {
781 static int get_function(struct udevice *dev, int offset, bool skip_unused,
784 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
785 struct dm_gpio_ops *ops = gpio_get_ops(dev);
787 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
788 if (!device_active(dev))
790 if (offset < 0 || offset >= uc_priv->gpio_count)
793 *namep = uc_priv->name[offset];
794 if (skip_unused && !uc_priv->name[offset])
796 if (ops->get_function) {
799 ret = ops->get_function(dev, offset);
802 if (ret >= ARRAY_SIZE(gpio_function))
807 return GPIOF_UNKNOWN;
810 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
812 return get_function(dev, offset, true, namep);
815 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
817 return get_function(dev, offset, false, namep);
820 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
822 struct dm_gpio_ops *ops = gpio_get_ops(dev);
823 struct gpio_dev_priv *priv;
829 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
832 priv = dev_get_uclass_priv(dev);
833 ret = gpio_get_raw_function(dev, offset, NULL);
837 len = snprintf(str, buffsize, "%s%d: %s",
838 priv->bank_name ? priv->bank_name : "",
839 offset, gpio_function[func]);
840 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
841 func == GPIOF_UNUSED) {
845 ret = ops->get_value(dev, offset);
848 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
849 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
859 #if CONFIG_IS_ENABLED(ACPIGEN)
860 int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
862 struct dm_gpio_ops *ops;
864 memset(gpio, '\0', sizeof(*gpio));
865 if (!dm_gpio_is_valid(desc)) {
866 /* Indicate that the GPIO is not valid */
872 ops = gpio_get_ops(desc->dev);
876 return ops->get_acpi(desc, gpio);
880 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
885 for (i = 0; i < 32; i++) {
886 gpio = gpio_num_array[i];
889 ret = gpio_requestf(gpio, fmt, i);
892 ret = gpio_direction_input(gpio);
901 for (i--; i >= 0; i--)
902 gpio_free(gpio_num_array[i]);
908 * get a number comprised of multiple GPIO values. gpio_num_array points to
909 * the array of gpio pin numbers to scan, terminated by -1.
911 int gpio_get_values_as_int(const int *gpio_list)
914 unsigned bitmask = 1;
919 ((gpio = *gpio_list++) != -1)) {
920 ret = gpio_get_value(gpio);
931 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
933 unsigned bitmask = 1;
937 for (i = 0; i < count; i++) {
938 ret = dm_gpio_get_value(&desc_list[i]);
950 * gpio_request_tail: common work for requesting a gpio.
952 * ret: return value from previous work in function which calls
954 * This seems bogus (why calling this function instead not
955 * calling it and end caller function instead?).
956 * Because on error in caller function we want to set some
957 * default values in gpio desc and have a common error
958 * debug message, which provides this function.
959 * nodename: Name of node for which gpio gets requested
960 * used for gpio label name.
961 * args: pointer to output arguments structure
962 * list_name: Name of GPIO list
963 * used for gpio label name.
964 * index: gpio index in gpio list
965 * used for gpio label name.
966 * desc: pointer to gpio descriptor, filled from this
968 * flags: gpio flags to use.
969 * add_index: should index added to gpio label name
970 * gpio_dev: pointer to gpio device from which the gpio
971 * will be requested. If NULL try to get the
972 * gpio device with uclass_get_device_by_ofnode()
974 * return: In error case this function sets default values in
975 * gpio descriptor, also emmits a debug message.
976 * On success it returns 0 else the error code from
977 * function calls, or the error code passed through
978 * ret to this function.
981 static int gpio_request_tail(int ret, const char *nodename,
982 struct ofnode_phandle_args *args,
983 const char *list_name, int index,
984 struct gpio_desc *desc, int flags,
985 bool add_index, struct udevice *gpio_dev)
987 gpio_desc_init(desc, gpio_dev, 0);
992 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
995 debug("%s: uclass_get_device_by_ofnode failed\n",
1000 ret = gpio_find_and_xlate(desc, args);
1002 debug("%s: gpio_find_and_xlate failed\n", __func__);
1005 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
1006 nodename, list_name, index);
1008 debug("%s: dm_gpio_requestf failed\n", __func__);
1011 ret = dm_gpio_set_dir_flags(desc, flags);
1013 debug("%s: dm_gpio_set_dir failed\n", __func__);
1019 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
1020 __func__, nodename, list_name, index, ret);
1024 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1025 int index, struct gpio_desc *desc,
1026 int flags, bool add_index)
1028 struct ofnode_phandle_args args;
1031 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1034 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1035 index, desc, flags, add_index, NULL);
1038 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
1039 struct gpio_desc *desc, int flags)
1041 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1045 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
1046 struct gpio_desc *desc, int flags)
1048 struct ofnode_phandle_args args;
1052 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1054 node = dev_ofnode(dev);
1055 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1056 index, desc, flags, index > 0, NULL);
1059 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1060 struct gpio_desc *desc, int max_count,
1066 for (count = 0; count < max_count; count++) {
1067 ret = _gpio_request_by_name_nodev(node, list_name, count,
1068 &desc[count], flags, true);
1075 /* We ran out of GPIOs in the list */
1079 gpio_free_list_nodev(desc, count - 1);
1084 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1085 struct gpio_desc *desc, int max_count,
1089 * This isn't ideal since we don't use dev->name in the debug()
1090 * calls in gpio_request_by_name(), but we can do this until
1091 * gpio_request_list_by_name_nodev() can be dropped.
1093 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1097 int gpio_get_list_count(struct udevice *dev, const char *list_name)
1101 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
1102 list_name, "#gpio-cells", 0, -1,
1105 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1106 __func__, dev->name, list_name, ret);
1112 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1114 /* For now, we don't do any checking of dev */
1115 return _dm_gpio_free(desc->dev, desc->offset);
1118 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1122 /* For now, we don't do any checking of dev */
1123 for (i = 0; i < count; i++)
1124 dm_gpio_free(dev, &desc[i]);
1129 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1131 return gpio_free_list(NULL, desc, count);
1134 /* We need to renumber the GPIOs when any driver is probed/removed */
1135 static int gpio_renumber(struct udevice *removed_dev)
1137 struct gpio_dev_priv *uc_priv;
1138 struct udevice *dev;
1143 ret = uclass_get(UCLASS_GPIO, &uc);
1147 /* Ensure that we have a base for each bank */
1149 uclass_foreach_dev(dev, uc) {
1150 if (device_active(dev) && dev != removed_dev) {
1151 uc_priv = dev_get_uclass_priv(dev);
1152 uc_priv->gpio_base = base;
1153 base += uc_priv->gpio_count;
1160 int gpio_get_number(const struct gpio_desc *desc)
1162 struct udevice *dev = desc->dev;
1163 struct gpio_dev_priv *uc_priv;
1167 uc_priv = dev->uclass_priv;
1169 return uc_priv->gpio_base + desc->offset;
1172 static int gpio_post_probe(struct udevice *dev)
1174 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1176 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1180 return gpio_renumber(NULL);
1183 static int gpio_pre_remove(struct udevice *dev)
1185 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1188 for (i = 0; i < uc_priv->gpio_count; i++) {
1189 if (uc_priv->name[i])
1190 free(uc_priv->name[i]);
1192 free(uc_priv->name);
1194 return gpio_renumber(dev);
1197 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1198 char *list_name, int index, int flags,
1199 int dtflags, struct gpio_desc *desc)
1201 struct ofnode_phandle_args args;
1203 args.node = ofnode_null();
1204 args.args_count = 2;
1205 args.args[0] = index;
1206 args.args[1] = dtflags;
1208 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1212 static int gpio_post_bind(struct udevice *dev)
1214 struct udevice *child;
1217 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1218 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1219 static int reloc_done;
1223 ops->request += gd->reloc_off;
1225 ops->rfree += gd->reloc_off;
1226 if (ops->direction_input)
1227 ops->direction_input += gd->reloc_off;
1228 if (ops->direction_output)
1229 ops->direction_output += gd->reloc_off;
1231 ops->get_value += gd->reloc_off;
1233 ops->set_value += gd->reloc_off;
1234 if (ops->get_function)
1235 ops->get_function += gd->reloc_off;
1237 ops->xlate += gd->reloc_off;
1238 if (ops->set_dir_flags)
1239 ops->set_dir_flags += gd->reloc_off;
1240 if (ops->get_dir_flags)
1241 ops->get_dir_flags += gd->reloc_off;
1247 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1248 dev_for_each_subnode(node, dev) {
1249 if (ofnode_read_bool(node, "gpio-hog")) {
1250 const char *name = ofnode_get_name(node);
1253 ret = device_bind_driver_to_node(dev,
1265 UCLASS_DRIVER(gpio) = {
1268 .flags = DM_UC_FLAG_SEQ_ALIAS,
1269 .post_probe = gpio_post_probe,
1270 .post_bind = gpio_post_bind,
1271 .pre_remove = gpio_pre_remove,
1272 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),