1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
10 #include <dm/device_compat.h>
11 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.h>
14 #include <dt-bindings/gpio/gpio.h>
18 #include <acpi/acpi_device.h>
20 #include <dm/device_compat.h>
21 #include <linux/bug.h>
22 #include <linux/ctype.h>
24 DECLARE_GLOBAL_DATA_PTR;
27 * gpio_desc_init() - Initialize the GPIO descriptor
29 * @desc: GPIO descriptor to initialize
31 * @offset: Offset of device GPIO
33 static void gpio_desc_init(struct gpio_desc *desc,
38 desc->offset = offset;
43 * gpio_to_device() - Convert global GPIO number to device, number
45 * Convert the GPIO number to an entry in the list of GPIOs
46 * or GPIO blocks registered with the GPIO controller. Returns
47 * entry on success, NULL on error.
49 * @gpio: The numeric representation of the GPIO
50 * @desc: Returns description (desc->flags will always be 0)
51 * @return 0 if found, -ENOENT if not found
53 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
55 struct gpio_dev_priv *uc_priv;
59 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
61 ret = uclass_next_device(&dev)) {
62 uc_priv = dev_get_uclass_priv(dev);
63 if (gpio >= uc_priv->gpio_base &&
64 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
65 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
71 return ret ? ret : -ENOENT;
74 #if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
76 * dm_gpio_lookup_label() - look for name in gpio device
78 * search in uc_priv, if there is a gpio with labelname same
81 * @name: name which is searched
82 * @uc_priv: gpio_dev_priv pointer.
83 * @offset: gpio offset within the device
84 * @return: 0 if found, -ENOENT if not.
86 static int dm_gpio_lookup_label(const char *name,
87 struct gpio_dev_priv *uc_priv, ulong *offset)
94 for (i = 0; i < uc_priv->gpio_count; i++) {
95 if (!uc_priv->name[i])
97 if (!strncmp(name, uc_priv->name[i], len)) {
106 dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
113 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
115 struct gpio_dev_priv *uc_priv = NULL;
121 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
122 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
124 ret = uclass_next_device(&dev)) {
127 uc_priv = dev_get_uclass_priv(dev);
129 offset = numeric - uc_priv->gpio_base;
130 /* Allow GPIOs to be numbered from 0 */
131 if (offset < uc_priv->gpio_count)
135 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
137 if (!strncasecmp(name, uc_priv->bank_name, len)) {
138 if (!strict_strtoul(name + len, 10, &offset))
143 * if we did not found a gpio through its bank
144 * name, we search for a valid gpio label.
146 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
151 return ret ? ret : -EINVAL;
153 gpio_desc_init(desc, dev, offset);
158 int gpio_lookup_name(const char *name, struct udevice **devp,
159 unsigned int *offsetp, unsigned int *gpiop)
161 struct gpio_desc desc;
166 ret = dm_gpio_lookup_name(name, &desc);
173 *offsetp = desc.offset;
175 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
177 *gpiop = uc_priv->gpio_base + desc.offset;
183 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
184 struct ofnode_phandle_args *args)
186 if (args->args_count < 1)
189 desc->offset = args->args[0];
191 if (args->args_count < 2)
195 if (args->args[1] & GPIO_ACTIVE_LOW)
196 desc->flags |= GPIOD_ACTIVE_LOW;
199 * need to test 2 bits for gpio output binding:
200 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
201 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
203 if (args->args[1] & GPIO_SINGLE_ENDED) {
204 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
205 desc->flags |= GPIOD_OPEN_DRAIN;
207 desc->flags |= GPIOD_OPEN_SOURCE;
210 if (args->args[1] & GPIO_PULL_UP)
211 desc->flags |= GPIOD_PULL_UP;
213 if (args->args[1] & GPIO_PULL_DOWN)
214 desc->flags |= GPIOD_PULL_DOWN;
219 static int gpio_find_and_xlate(struct gpio_desc *desc,
220 struct ofnode_phandle_args *args)
222 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
225 return ops->xlate(desc->dev, desc, args);
227 return gpio_xlate_offs_flags(desc->dev, desc, args);
230 #if defined(CONFIG_GPIO_HOG)
232 struct gpio_hog_priv {
233 struct gpio_desc gpiod;
236 struct gpio_hog_data {
242 static int gpio_hog_of_to_plat(struct udevice *dev)
244 struct gpio_hog_data *plat = dev_get_plat(dev);
245 const char *nodename;
249 if (dev_read_bool(dev, "input")) {
250 plat->gpiod_flags = GPIOD_IS_IN;
251 } else if (dev_read_bool(dev, "output-high")) {
253 plat->gpiod_flags = GPIOD_IS_OUT;
254 } else if (dev_read_bool(dev, "output-low")) {
255 plat->gpiod_flags = GPIOD_IS_OUT;
257 printf("%s: missing gpio-hog state.\n", __func__);
260 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
262 printf("%s: wrong gpios property, 2 values needed %d\n",
266 nodename = dev_read_string(dev, "line-name");
268 device_set_name(dev, nodename);
273 static int gpio_hog_probe(struct udevice *dev)
275 struct gpio_hog_data *plat = dev_get_plat(dev);
276 struct gpio_hog_priv *priv = dev_get_priv(dev);
279 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
280 plat->val[0], plat->gpiod_flags,
281 plat->val[1], &priv->gpiod);
283 debug("%s: node %s could not get gpio.\n", __func__,
288 if (plat->gpiod_flags == GPIOD_IS_OUT) {
289 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
291 debug("%s: node %s could not set gpio.\n", __func__,
300 int gpio_hog_probe_all(void)
306 for (uclass_first_device(UCLASS_NOP, &dev);
308 uclass_find_next_device(&dev)) {
309 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
310 ret = device_probe(dev);
312 printf("Failed to probe device %s err: %d\n",
322 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
327 gpio_hog_probe_all();
328 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
329 struct gpio_hog_priv *priv = dev_get_priv(dev);
331 *desc = &priv->gpiod;
338 U_BOOT_DRIVER(gpio_hog) = {
341 .of_to_plat = gpio_hog_of_to_plat,
342 .probe = gpio_hog_probe,
343 .priv_auto = sizeof(struct gpio_hog_priv),
344 .plat_auto = sizeof(struct gpio_hog_data),
347 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
353 int dm_gpio_request(struct gpio_desc *desc, const char *label)
355 struct udevice *dev = desc->dev;
356 struct gpio_dev_priv *uc_priv;
360 uc_priv = dev_get_uclass_priv(dev);
361 if (uc_priv->name[desc->offset])
366 if (gpio_get_ops(dev)->request) {
367 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
373 uc_priv->name[desc->offset] = str;
378 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
380 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
385 vscnprintf(buf, sizeof(buf), fmt, args);
387 return dm_gpio_request(desc, buf);
389 return dm_gpio_request(desc, fmt);
394 * gpio_request() - [COMPAT] Request GPIO
396 * label: Name for the requested GPIO
398 * The label is copied and allocated so the caller does not need to keep
399 * the pointer around.
401 * This function implements the API that's compatible with current
402 * GPIO API used in U-Boot. The request is forwarded to particular
403 * GPIO driver. Returns 0 on success, negative value on error.
405 int gpio_request(unsigned gpio, const char *label)
407 struct gpio_desc desc;
410 ret = gpio_to_device(gpio, &desc);
414 return dm_gpio_request(&desc, label);
418 * gpio_requestf() - [COMPAT] Request GPIO
420 * @fmt: Format string for the requested GPIO
421 * @...: Arguments for the printf() format string
423 * This function implements the API that's compatible with current
424 * GPIO API used in U-Boot. The request is forwarded to particular
425 * GPIO driver. Returns 0 on success, negative value on error.
427 int gpio_requestf(unsigned gpio, const char *fmt, ...)
429 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
434 vscnprintf(buf, sizeof(buf), fmt, args);
436 return gpio_request(gpio, buf);
438 return gpio_request(gpio, fmt);
442 int _dm_gpio_free(struct udevice *dev, uint offset)
444 struct gpio_dev_priv *uc_priv;
447 uc_priv = dev_get_uclass_priv(dev);
448 if (!uc_priv->name[offset])
450 if (gpio_get_ops(dev)->rfree) {
451 ret = gpio_get_ops(dev)->rfree(dev, offset);
456 free(uc_priv->name[offset]);
457 uc_priv->name[offset] = NULL;
463 * gpio_free() - [COMPAT] Relinquish GPIO
466 * This function implements the API that's compatible with current
467 * GPIO API used in U-Boot. The request is forwarded to particular
468 * GPIO driver. Returns 0 on success, negative value on error.
470 int gpio_free(unsigned gpio)
472 struct gpio_desc desc;
475 ret = gpio_to_device(gpio, &desc);
479 return _dm_gpio_free(desc.dev, desc.offset);
482 static int check_reserved(const struct gpio_desc *desc, const char *func)
484 struct gpio_dev_priv *uc_priv;
486 if (!dm_gpio_is_valid(desc))
489 uc_priv = dev_get_uclass_priv(desc->dev);
490 if (!uc_priv->name[desc->offset]) {
491 printf("%s: %s: error: gpio %s%d not reserved\n",
492 desc->dev->name, func,
493 uc_priv->bank_name ? uc_priv->bank_name : "",
502 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
505 * This function implements the API that's compatible with current
506 * GPIO API used in U-Boot. The request is forwarded to particular
507 * GPIO driver. Returns 0 on success, negative value on error.
509 int gpio_direction_input(unsigned gpio)
511 struct gpio_desc desc;
514 ret = gpio_to_device(gpio, &desc);
517 ret = check_reserved(&desc, "dir_input");
521 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
525 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
527 * value: Logical value to be set on the GPIO pin
529 * This function implements the API that's compatible with current
530 * GPIO API used in U-Boot. The request is forwarded to particular
531 * GPIO driver. Returns 0 on success, negative value on error.
533 int gpio_direction_output(unsigned gpio, int value)
535 struct gpio_desc desc;
538 ret = gpio_to_device(gpio, &desc);
541 ret = check_reserved(&desc, "dir_output");
545 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
549 static int _gpio_get_value(const struct gpio_desc *desc)
553 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
555 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
558 int dm_gpio_get_value(const struct gpio_desc *desc)
562 ret = check_reserved(desc, "get_value");
566 return _gpio_get_value(desc);
569 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
573 ret = check_reserved(desc, "set_value");
577 if (desc->flags & GPIOD_ACTIVE_LOW)
581 * Emulate open drain by not actively driving the line high or
582 * Emulate open source by not actively driving the line low
584 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
585 (desc->flags & GPIOD_OPEN_SOURCE && !value))
586 return gpio_get_ops(desc->dev)->direction_input(desc->dev,
588 else if (desc->flags & GPIOD_OPEN_DRAIN ||
589 desc->flags & GPIOD_OPEN_SOURCE)
590 return gpio_get_ops(desc->dev)->direction_output(desc->dev,
594 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
598 /* check dir flags invalid configuration */
599 static int check_dir_flags(ulong flags)
601 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
602 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
607 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
608 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
613 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
614 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
622 static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
624 struct udevice *dev = desc->dev;
625 struct dm_gpio_ops *ops = gpio_get_ops(dev);
626 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
629 ret = check_dir_flags(flags);
632 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
634 uc_priv->bank_name ? uc_priv->bank_name : "",
635 desc->offset, flags);
640 /* GPIOD_ are directly managed by driver in set_dir_flags*/
641 if (ops->set_dir_flags) {
642 ret = ops->set_dir_flags(dev, desc->offset, flags);
644 if (flags & GPIOD_IS_OUT) {
645 ret = ops->direction_output(dev, desc->offset,
646 GPIOD_FLAGS_OUTPUT(flags));
647 } else if (flags & GPIOD_IS_IN) {
648 ret = ops->direction_input(dev, desc->offset);
652 /* save the flags also in descriptor */
659 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
663 ret = check_reserved(desc, "set_dir_flags");
667 /* combine the requested flags (for IN/OUT) and the descriptor flags */
668 flags |= desc->flags;
669 ret = _dm_gpio_set_dir_flags(desc, flags);
674 int dm_gpio_set_dir(struct gpio_desc *desc)
678 ret = check_reserved(desc, "set_dir");
682 return _dm_gpio_set_dir_flags(desc, desc->flags);
685 int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags)
687 struct udevice *dev = desc->dev;
689 struct dm_gpio_ops *ops = gpio_get_ops(dev);
692 ret = check_reserved(desc, "get_dir_flags");
696 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
697 if (ops->get_dir_flags) {
698 ret = ops->get_dir_flags(dev, desc->offset, &dir_flags);
702 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
703 value = dir_flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
704 if (desc->flags & GPIOD_ACTIVE_LOW)
706 dir_flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
707 dir_flags |= (desc->flags & GPIOD_ACTIVE_LOW);
709 dir_flags |= GPIOD_IS_OUT_ACTIVE;
711 dir_flags = desc->flags;
712 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
713 dir_flags &= ~GPIOD_IS_OUT_ACTIVE;
714 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
715 dir_flags |= GPIOD_IS_OUT_ACTIVE;
723 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
726 * This function implements the API that's compatible with current
727 * GPIO API used in U-Boot. The request is forwarded to particular
728 * GPIO driver. Returns the value of the GPIO pin, or negative value
731 int gpio_get_value(unsigned gpio)
735 struct gpio_desc desc;
737 ret = gpio_to_device(gpio, &desc);
740 return dm_gpio_get_value(&desc);
744 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
746 * value: Logical value to be set on the GPIO pin.
748 * This function implements the API that's compatible with current
749 * GPIO API used in U-Boot. The request is forwarded to particular
750 * GPIO driver. Returns 0 on success, negative value on error.
752 int gpio_set_value(unsigned gpio, int value)
754 struct gpio_desc desc;
757 ret = gpio_to_device(gpio, &desc);
760 return dm_gpio_set_value(&desc, value);
763 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
765 struct gpio_dev_priv *priv;
767 /* Must be called on an active device */
768 priv = dev_get_uclass_priv(dev);
771 *bit_count = priv->gpio_count;
772 return priv->bank_name;
775 static const char * const gpio_function[GPIOF_COUNT] = {
783 static int get_function(struct udevice *dev, int offset, bool skip_unused,
786 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
787 struct dm_gpio_ops *ops = gpio_get_ops(dev);
789 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
790 if (!device_active(dev))
792 if (offset < 0 || offset >= uc_priv->gpio_count)
795 *namep = uc_priv->name[offset];
796 if (skip_unused && !uc_priv->name[offset])
798 if (ops->get_function) {
801 ret = ops->get_function(dev, offset);
804 if (ret >= ARRAY_SIZE(gpio_function))
809 return GPIOF_UNKNOWN;
812 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
814 return get_function(dev, offset, true, namep);
817 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
819 return get_function(dev, offset, false, namep);
822 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
824 struct dm_gpio_ops *ops = gpio_get_ops(dev);
825 struct gpio_dev_priv *priv;
831 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
834 priv = dev_get_uclass_priv(dev);
835 ret = gpio_get_raw_function(dev, offset, NULL);
839 len = snprintf(str, buffsize, "%s%d: %s",
840 priv->bank_name ? priv->bank_name : "",
841 offset, gpio_function[func]);
842 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
843 func == GPIOF_UNUSED) {
847 ret = ops->get_value(dev, offset);
850 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
851 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
861 #if CONFIG_IS_ENABLED(ACPIGEN)
862 int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
864 struct dm_gpio_ops *ops;
866 memset(gpio, '\0', sizeof(*gpio));
867 if (!dm_gpio_is_valid(desc)) {
868 /* Indicate that the GPIO is not valid */
874 ops = gpio_get_ops(desc->dev);
878 return ops->get_acpi(desc, gpio);
882 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
887 for (i = 0; i < 32; i++) {
888 gpio = gpio_num_array[i];
891 ret = gpio_requestf(gpio, fmt, i);
894 ret = gpio_direction_input(gpio);
903 for (i--; i >= 0; i--)
904 gpio_free(gpio_num_array[i]);
910 * get a number comprised of multiple GPIO values. gpio_num_array points to
911 * the array of gpio pin numbers to scan, terminated by -1.
913 int gpio_get_values_as_int(const int *gpio_list)
916 unsigned bitmask = 1;
921 ((gpio = *gpio_list++) != -1)) {
922 ret = gpio_get_value(gpio);
933 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
935 unsigned bitmask = 1;
939 for (i = 0; i < count; i++) {
940 ret = dm_gpio_get_value(&desc_list[i]);
952 * gpio_request_tail: common work for requesting a gpio.
954 * ret: return value from previous work in function which calls
956 * This seems bogus (why calling this function instead not
957 * calling it and end caller function instead?).
958 * Because on error in caller function we want to set some
959 * default values in gpio desc and have a common error
960 * debug message, which provides this function.
961 * nodename: Name of node for which gpio gets requested
962 * used for gpio label name.
963 * args: pointer to output arguments structure
964 * list_name: Name of GPIO list
965 * used for gpio label name.
966 * index: gpio index in gpio list
967 * used for gpio label name.
968 * desc: pointer to gpio descriptor, filled from this
970 * flags: gpio flags to use.
971 * add_index: should index added to gpio label name
972 * gpio_dev: pointer to gpio device from which the gpio
973 * will be requested. If NULL try to get the
974 * gpio device with uclass_get_device_by_ofnode()
976 * return: In error case this function sets default values in
977 * gpio descriptor, also emmits a debug message.
978 * On success it returns 0 else the error code from
979 * function calls, or the error code passed through
980 * ret to this function.
983 static int gpio_request_tail(int ret, const char *nodename,
984 struct ofnode_phandle_args *args,
985 const char *list_name, int index,
986 struct gpio_desc *desc, int flags,
987 bool add_index, struct udevice *gpio_dev)
989 gpio_desc_init(desc, gpio_dev, 0);
994 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
997 debug("%s: uclass_get_device_by_ofnode failed\n",
1002 ret = gpio_find_and_xlate(desc, args);
1004 debug("%s: gpio_find_and_xlate failed\n", __func__);
1007 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
1008 nodename, list_name, index);
1010 debug("%s: dm_gpio_requestf failed\n", __func__);
1013 ret = dm_gpio_set_dir_flags(desc, flags);
1015 debug("%s: dm_gpio_set_dir failed\n", __func__);
1021 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
1022 __func__, nodename, list_name, index, ret);
1026 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1027 int index, struct gpio_desc *desc,
1028 int flags, bool add_index)
1030 struct ofnode_phandle_args args;
1033 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1036 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1037 index, desc, flags, add_index, NULL);
1040 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
1041 struct gpio_desc *desc, int flags)
1043 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1047 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
1048 struct gpio_desc *desc, int flags)
1050 struct ofnode_phandle_args args;
1054 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1056 node = dev_ofnode(dev);
1057 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1058 index, desc, flags, index > 0, NULL);
1061 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1062 struct gpio_desc *desc, int max_count,
1068 for (count = 0; count < max_count; count++) {
1069 ret = _gpio_request_by_name_nodev(node, list_name, count,
1070 &desc[count], flags, true);
1077 /* We ran out of GPIOs in the list */
1081 gpio_free_list_nodev(desc, count - 1);
1086 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1087 struct gpio_desc *desc, int max_count,
1091 * This isn't ideal since we don't use dev->name in the debug()
1092 * calls in gpio_request_by_name(), but we can do this until
1093 * gpio_request_list_by_name_nodev() can be dropped.
1095 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1099 int gpio_get_list_count(struct udevice *dev, const char *list_name)
1103 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, -1,
1106 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1107 __func__, dev->name, list_name, ret);
1113 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1115 /* For now, we don't do any checking of dev */
1116 return _dm_gpio_free(desc->dev, desc->offset);
1119 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1123 /* For now, we don't do any checking of dev */
1124 for (i = 0; i < count; i++)
1125 dm_gpio_free(dev, &desc[i]);
1130 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1132 return gpio_free_list(NULL, desc, count);
1135 /* We need to renumber the GPIOs when any driver is probed/removed */
1136 static int gpio_renumber(struct udevice *removed_dev)
1138 struct gpio_dev_priv *uc_priv;
1139 struct udevice *dev;
1144 ret = uclass_get(UCLASS_GPIO, &uc);
1148 /* Ensure that we have a base for each bank */
1150 uclass_foreach_dev(dev, uc) {
1151 if (device_active(dev) && dev != removed_dev) {
1152 uc_priv = dev_get_uclass_priv(dev);
1153 uc_priv->gpio_base = base;
1154 base += uc_priv->gpio_count;
1161 int gpio_get_number(const struct gpio_desc *desc)
1163 struct udevice *dev = desc->dev;
1164 struct gpio_dev_priv *uc_priv;
1168 uc_priv = dev_get_uclass_priv(dev);
1170 return uc_priv->gpio_base + desc->offset;
1173 static int gpio_post_probe(struct udevice *dev)
1175 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1177 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1181 return gpio_renumber(NULL);
1184 static int gpio_pre_remove(struct udevice *dev)
1186 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1189 for (i = 0; i < uc_priv->gpio_count; i++) {
1190 if (uc_priv->name[i])
1191 free(uc_priv->name[i]);
1193 free(uc_priv->name);
1195 return gpio_renumber(dev);
1198 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1199 char *list_name, int index, int flags,
1200 int dtflags, struct gpio_desc *desc)
1202 struct ofnode_phandle_args args;
1204 args.node = ofnode_null();
1205 args.args_count = 2;
1206 args.args[0] = index;
1207 args.args[1] = dtflags;
1209 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1213 static void devm_gpiod_release(struct udevice *dev, void *res)
1215 dm_gpio_free(dev, res);
1218 static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1223 struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1224 unsigned int index, int flags)
1227 struct gpio_desc *desc;
1229 static const char suffix[] = "-gpios";
1231 propname = malloc(strlen(id) + sizeof(suffix));
1237 strcpy(propname, id);
1238 strcat(propname, suffix);
1240 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1242 if (unlikely(!desc)) {
1247 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1256 devres_add(dev, desc);
1261 struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1266 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1274 void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1278 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1282 static int gpio_post_bind(struct udevice *dev)
1284 struct udevice *child;
1287 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1288 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1289 static int reloc_done;
1293 ops->request += gd->reloc_off;
1295 ops->rfree += gd->reloc_off;
1296 if (ops->direction_input)
1297 ops->direction_input += gd->reloc_off;
1298 if (ops->direction_output)
1299 ops->direction_output += gd->reloc_off;
1301 ops->get_value += gd->reloc_off;
1303 ops->set_value += gd->reloc_off;
1304 if (ops->get_function)
1305 ops->get_function += gd->reloc_off;
1307 ops->xlate += gd->reloc_off;
1308 if (ops->set_dir_flags)
1309 ops->set_dir_flags += gd->reloc_off;
1310 if (ops->get_dir_flags)
1311 ops->get_dir_flags += gd->reloc_off;
1317 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1318 dev_for_each_subnode(node, dev) {
1319 if (ofnode_read_bool(node, "gpio-hog")) {
1320 const char *name = ofnode_get_name(node);
1323 ret = device_bind_driver_to_node(dev,
1335 UCLASS_DRIVER(gpio) = {
1338 .flags = DM_UC_FLAG_SEQ_ALIAS,
1339 .post_probe = gpio_post_probe,
1340 .post_bind = gpio_post_bind,
1341 .pre_remove = gpio_pre_remove,
1342 .per_device_auto = sizeof(struct gpio_dev_priv),