1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
8 #include <dm/device_compat.h>
9 #include <dm/device-internal.h>
11 #include <dm/uclass-internal.h>
12 #include <dt-bindings/gpio/gpio.h>
17 #include <linux/bug.h>
18 #include <linux/ctype.h>
20 DECLARE_GLOBAL_DATA_PTR;
23 * gpio_desc_init() - Initialize the GPIO descriptor
25 * @desc: GPIO descriptor to initialize
27 * @offset: Offset of device GPIO
29 static void gpio_desc_init(struct gpio_desc *desc,
34 desc->offset = offset;
39 * gpio_to_device() - Convert global GPIO number to device, number
41 * Convert the GPIO number to an entry in the list of GPIOs
42 * or GPIO blocks registered with the GPIO controller. Returns
43 * entry on success, NULL on error.
45 * @gpio: The numeric representation of the GPIO
46 * @desc: Returns description (desc->flags will always be 0)
47 * @return 0 if found, -ENOENT if not found
49 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
51 struct gpio_dev_priv *uc_priv;
55 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
57 ret = uclass_next_device(&dev)) {
58 uc_priv = dev_get_uclass_priv(dev);
59 if (gpio >= uc_priv->gpio_base &&
60 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
61 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
67 return ret ? ret : -ENOENT;
70 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
72 struct gpio_dev_priv *uc_priv = NULL;
78 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
79 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
81 ret = uclass_next_device(&dev)) {
84 uc_priv = dev_get_uclass_priv(dev);
86 offset = numeric - uc_priv->gpio_base;
87 /* Allow GPIOs to be numbered from 0 */
88 if (offset < uc_priv->gpio_count)
92 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
94 if (!strncasecmp(name, uc_priv->bank_name, len)) {
95 if (!strict_strtoul(name + len, 10, &offset))
101 return ret ? ret : -EINVAL;
103 gpio_desc_init(desc, dev, offset);
108 int gpio_lookup_name(const char *name, struct udevice **devp,
109 unsigned int *offsetp, unsigned int *gpiop)
111 struct gpio_desc desc;
116 ret = dm_gpio_lookup_name(name, &desc);
123 *offsetp = desc.offset;
125 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
127 *gpiop = uc_priv->gpio_base + desc.offset;
133 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
134 struct ofnode_phandle_args *args)
136 if (args->args_count < 1)
139 desc->offset = args->args[0];
141 if (args->args_count < 2)
145 if (args->args[1] & GPIO_ACTIVE_LOW)
146 desc->flags |= GPIOD_ACTIVE_LOW;
149 * need to test 2 bits for gpio output binding:
150 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
151 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
153 if (args->args[1] & GPIO_SINGLE_ENDED) {
154 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
155 desc->flags |= GPIOD_OPEN_DRAIN;
157 desc->flags |= GPIOD_OPEN_SOURCE;
160 if (args->args[1] & GPIO_PULL_UP)
161 desc->flags |= GPIOD_PULL_UP;
163 if (args->args[1] & GPIO_PULL_DOWN)
164 desc->flags |= GPIOD_PULL_DOWN;
169 static int gpio_find_and_xlate(struct gpio_desc *desc,
170 struct ofnode_phandle_args *args)
172 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
175 return ops->xlate(desc->dev, desc, args);
177 return gpio_xlate_offs_flags(desc->dev, desc, args);
180 #if defined(CONFIG_GPIO_HOG)
182 struct gpio_hog_priv {
183 struct gpio_desc gpiod;
186 struct gpio_hog_data {
192 static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
194 struct gpio_hog_data *plat = dev_get_platdata(dev);
195 const char *nodename;
199 if (dev_read_bool(dev, "input")) {
200 plat->gpiod_flags = GPIOD_IS_IN;
201 } else if (dev_read_bool(dev, "output-high")) {
203 plat->gpiod_flags = GPIOD_IS_OUT;
204 } else if (dev_read_bool(dev, "output-low")) {
205 plat->gpiod_flags = GPIOD_IS_OUT;
207 printf("%s: missing gpio-hog state.\n", __func__);
210 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
212 printf("%s: wrong gpios property, 2 values needed %d\n",
216 nodename = dev_read_string(dev, "line-name");
218 device_set_name(dev, nodename);
223 static int gpio_hog_probe(struct udevice *dev)
225 struct gpio_hog_data *plat = dev_get_platdata(dev);
226 struct gpio_hog_priv *priv = dev_get_priv(dev);
229 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
230 plat->val[0], plat->gpiod_flags,
231 plat->val[1], &priv->gpiod);
233 debug("%s: node %s could not get gpio.\n", __func__,
238 if (plat->gpiod_flags == GPIOD_IS_OUT) {
239 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
241 debug("%s: node %s could not set gpio.\n", __func__,
250 int gpio_hog_probe_all(void)
256 for (uclass_first_device(UCLASS_NOP, &dev);
258 uclass_find_next_device(&dev)) {
259 if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
260 ret = device_probe(dev);
262 printf("Failed to probe device %s err: %d\n",
272 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
277 gpio_hog_probe_all();
278 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
279 struct gpio_hog_priv *priv = dev_get_priv(dev);
281 *desc = &priv->gpiod;
288 U_BOOT_DRIVER(gpio_hog) = {
291 .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
292 .probe = gpio_hog_probe,
293 .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
294 .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
297 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
303 int dm_gpio_request(struct gpio_desc *desc, const char *label)
305 struct udevice *dev = desc->dev;
306 struct gpio_dev_priv *uc_priv;
310 uc_priv = dev_get_uclass_priv(dev);
311 if (uc_priv->name[desc->offset])
316 if (gpio_get_ops(dev)->request) {
317 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
323 uc_priv->name[desc->offset] = str;
328 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
330 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
335 vscnprintf(buf, sizeof(buf), fmt, args);
337 return dm_gpio_request(desc, buf);
339 return dm_gpio_request(desc, fmt);
344 * gpio_request() - [COMPAT] Request GPIO
346 * label: Name for the requested GPIO
348 * The label is copied and allocated so the caller does not need to keep
349 * the pointer around.
351 * This function implements the API that's compatible with current
352 * GPIO API used in U-Boot. The request is forwarded to particular
353 * GPIO driver. Returns 0 on success, negative value on error.
355 int gpio_request(unsigned gpio, const char *label)
357 struct gpio_desc desc;
360 ret = gpio_to_device(gpio, &desc);
364 return dm_gpio_request(&desc, label);
368 * gpio_requestf() - [COMPAT] Request GPIO
370 * @fmt: Format string for the requested GPIO
371 * @...: Arguments for the printf() format string
373 * This function implements the API that's compatible with current
374 * GPIO API used in U-Boot. The request is forwarded to particular
375 * GPIO driver. Returns 0 on success, negative value on error.
377 int gpio_requestf(unsigned gpio, const char *fmt, ...)
379 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
384 vscnprintf(buf, sizeof(buf), fmt, args);
386 return gpio_request(gpio, buf);
388 return gpio_request(gpio, fmt);
392 int _dm_gpio_free(struct udevice *dev, uint offset)
394 struct gpio_dev_priv *uc_priv;
397 uc_priv = dev_get_uclass_priv(dev);
398 if (!uc_priv->name[offset])
400 if (gpio_get_ops(dev)->rfree) {
401 ret = gpio_get_ops(dev)->rfree(dev, offset);
406 free(uc_priv->name[offset]);
407 uc_priv->name[offset] = NULL;
413 * gpio_free() - [COMPAT] Relinquish GPIO
416 * This function implements the API that's compatible with current
417 * GPIO API used in U-Boot. The request is forwarded to particular
418 * GPIO driver. Returns 0 on success, negative value on error.
420 int gpio_free(unsigned gpio)
422 struct gpio_desc desc;
425 ret = gpio_to_device(gpio, &desc);
429 return _dm_gpio_free(desc.dev, desc.offset);
432 static int check_reserved(const struct gpio_desc *desc, const char *func)
434 struct gpio_dev_priv *uc_priv;
436 if (!dm_gpio_is_valid(desc))
439 uc_priv = dev_get_uclass_priv(desc->dev);
440 if (!uc_priv->name[desc->offset]) {
441 printf("%s: %s: error: gpio %s%d not reserved\n",
442 desc->dev->name, func,
443 uc_priv->bank_name ? uc_priv->bank_name : "",
452 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
455 * This function implements the API that's compatible with current
456 * GPIO API used in U-Boot. The request is forwarded to particular
457 * GPIO driver. Returns 0 on success, negative value on error.
459 int gpio_direction_input(unsigned gpio)
461 struct gpio_desc desc;
464 ret = gpio_to_device(gpio, &desc);
467 ret = check_reserved(&desc, "dir_input");
471 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
475 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
477 * value: Logical value to be set on the GPIO pin
479 * This function implements the API that's compatible with current
480 * GPIO API used in U-Boot. The request is forwarded to particular
481 * GPIO driver. Returns 0 on success, negative value on error.
483 int gpio_direction_output(unsigned gpio, int value)
485 struct gpio_desc desc;
488 ret = gpio_to_device(gpio, &desc);
491 ret = check_reserved(&desc, "dir_output");
495 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
499 static int _gpio_get_value(const struct gpio_desc *desc)
503 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
505 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
508 int dm_gpio_get_value(const struct gpio_desc *desc)
512 ret = check_reserved(desc, "get_value");
516 return _gpio_get_value(desc);
519 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
523 ret = check_reserved(desc, "set_value");
527 if (desc->flags & GPIOD_ACTIVE_LOW)
529 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
533 /* check dir flags invalid configuration */
534 static int check_dir_flags(ulong flags)
536 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
537 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
542 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
543 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
548 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
549 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
557 static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
559 struct udevice *dev = desc->dev;
560 struct dm_gpio_ops *ops = gpio_get_ops(dev);
561 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
564 ret = check_dir_flags(flags);
567 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
569 uc_priv->bank_name ? uc_priv->bank_name : "",
570 desc->offset, flags);
575 if (flags & GPIOD_IS_OUT) {
576 ret = ops->direction_output(dev, desc->offset,
577 GPIOD_FLAGS_OUTPUT(flags));
578 } else if (flags & GPIOD_IS_IN) {
579 ret = ops->direction_input(dev, desc->offset);
585 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
589 ret = check_reserved(desc, "set_dir_flags");
593 /* combine the requested flags (for IN/OUT) and the descriptor flags */
594 flags |= desc->flags;
595 ret = _dm_gpio_set_dir_flags(desc, flags);
597 /* update the descriptor flags */
604 int dm_gpio_set_dir(struct gpio_desc *desc)
608 ret = check_reserved(desc, "set_dir");
612 return _dm_gpio_set_dir_flags(desc, desc->flags);
615 int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags)
620 ret = check_reserved(desc, "get_dir_flags");
624 dir_flags = desc->flags;
625 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
626 dir_flags &= ~GPIOD_IS_OUT_ACTIVE;
627 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
628 dir_flags |= GPIOD_IS_OUT_ACTIVE;
636 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
639 * This function implements the API that's compatible with current
640 * GPIO API used in U-Boot. The request is forwarded to particular
641 * GPIO driver. Returns the value of the GPIO pin, or negative value
644 int gpio_get_value(unsigned gpio)
648 struct gpio_desc desc;
650 ret = gpio_to_device(gpio, &desc);
653 return dm_gpio_get_value(&desc);
657 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
659 * value: Logical value to be set on the GPIO pin.
661 * This function implements the API that's compatible with current
662 * GPIO API used in U-Boot. The request is forwarded to particular
663 * GPIO driver. Returns 0 on success, negative value on error.
665 int gpio_set_value(unsigned gpio, int value)
667 struct gpio_desc desc;
670 ret = gpio_to_device(gpio, &desc);
673 return dm_gpio_set_value(&desc, value);
676 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
678 struct gpio_dev_priv *priv;
680 /* Must be called on an active device */
681 priv = dev_get_uclass_priv(dev);
684 *bit_count = priv->gpio_count;
685 return priv->bank_name;
688 static const char * const gpio_function[GPIOF_COUNT] = {
696 static int get_function(struct udevice *dev, int offset, bool skip_unused,
699 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
700 struct dm_gpio_ops *ops = gpio_get_ops(dev);
702 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
703 if (!device_active(dev))
705 if (offset < 0 || offset >= uc_priv->gpio_count)
708 *namep = uc_priv->name[offset];
709 if (skip_unused && !uc_priv->name[offset])
711 if (ops->get_function) {
714 ret = ops->get_function(dev, offset);
717 if (ret >= ARRAY_SIZE(gpio_function))
722 return GPIOF_UNKNOWN;
725 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
727 return get_function(dev, offset, true, namep);
730 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
732 return get_function(dev, offset, false, namep);
735 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
737 struct dm_gpio_ops *ops = gpio_get_ops(dev);
738 struct gpio_dev_priv *priv;
744 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
747 priv = dev_get_uclass_priv(dev);
748 ret = gpio_get_raw_function(dev, offset, NULL);
752 len = snprintf(str, buffsize, "%s%d: %s",
753 priv->bank_name ? priv->bank_name : "",
754 offset, gpio_function[func]);
755 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
756 func == GPIOF_UNUSED) {
760 ret = ops->get_value(dev, offset);
763 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
764 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
774 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
779 for (i = 0; i < 32; i++) {
780 gpio = gpio_num_array[i];
783 ret = gpio_requestf(gpio, fmt, i);
786 ret = gpio_direction_input(gpio);
795 for (i--; i >= 0; i--)
796 gpio_free(gpio_num_array[i]);
802 * get a number comprised of multiple GPIO values. gpio_num_array points to
803 * the array of gpio pin numbers to scan, terminated by -1.
805 int gpio_get_values_as_int(const int *gpio_list)
808 unsigned bitmask = 1;
813 ((gpio = *gpio_list++) != -1)) {
814 ret = gpio_get_value(gpio);
825 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
827 unsigned bitmask = 1;
831 for (i = 0; i < count; i++) {
832 ret = dm_gpio_get_value(&desc_list[i]);
844 * gpio_request_tail: common work for requesting a gpio.
846 * ret: return value from previous work in function which calls
848 * This seems bogus (why calling this function instead not
849 * calling it and end caller function instead?).
850 * Because on error in caller function we want to set some
851 * default values in gpio desc and have a common error
852 * debug message, which provides this function.
853 * nodename: Name of node for which gpio gets requested
854 * used for gpio label name.
855 * args: pointer to output arguments structure
856 * list_name: Name of GPIO list
857 * used for gpio label name.
858 * index: gpio index in gpio list
859 * used for gpio label name.
860 * desc: pointer to gpio descriptor, filled from this
862 * flags: gpio flags to use.
863 * add_index: should index added to gpio label name
864 * gpio_dev: pointer to gpio device from which the gpio
865 * will be requested. If NULL try to get the
866 * gpio device with uclass_get_device_by_ofnode()
868 * return: In error case this function sets default values in
869 * gpio descriptor, also emmits a debug message.
870 * On success it returns 0 else the error code from
871 * function calls, or the error code passed through
872 * ret to this function.
875 static int gpio_request_tail(int ret, const char *nodename,
876 struct ofnode_phandle_args *args,
877 const char *list_name, int index,
878 struct gpio_desc *desc, int flags,
879 bool add_index, struct udevice *gpio_dev)
881 gpio_desc_init(desc, gpio_dev, 0);
886 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
889 debug("%s: uclass_get_device_by_ofnode failed\n",
894 ret = gpio_find_and_xlate(desc, args);
896 debug("%s: gpio_find_and_xlate failed\n", __func__);
899 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
900 nodename, list_name, index);
902 debug("%s: dm_gpio_requestf failed\n", __func__);
905 ret = dm_gpio_set_dir_flags(desc, flags);
907 debug("%s: dm_gpio_set_dir failed\n", __func__);
913 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
914 __func__, nodename, list_name, index, ret);
918 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
919 int index, struct gpio_desc *desc,
920 int flags, bool add_index)
922 struct ofnode_phandle_args args;
925 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
928 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
929 index, desc, flags, add_index, NULL);
932 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
933 struct gpio_desc *desc, int flags)
935 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
939 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
940 struct gpio_desc *desc, int flags)
942 struct ofnode_phandle_args args;
946 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
948 node = dev_ofnode(dev);
949 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
950 index, desc, flags, index > 0, NULL);
953 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
954 struct gpio_desc *desc, int max_count,
960 for (count = 0; count < max_count; count++) {
961 ret = _gpio_request_by_name_nodev(node, list_name, count,
962 &desc[count], flags, true);
969 /* We ran out of GPIOs in the list */
973 gpio_free_list_nodev(desc, count - 1);
978 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
979 struct gpio_desc *desc, int max_count,
983 * This isn't ideal since we don't use dev->name in the debug()
984 * calls in gpio_request_by_name(), but we can do this until
985 * gpio_request_list_by_name_nodev() can be dropped.
987 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
991 int gpio_get_list_count(struct udevice *dev, const char *list_name)
995 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
996 list_name, "#gpio-cells", 0, -1,
999 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1000 __func__, dev->name, list_name, ret);
1006 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1008 /* For now, we don't do any checking of dev */
1009 return _dm_gpio_free(desc->dev, desc->offset);
1012 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1016 /* For now, we don't do any checking of dev */
1017 for (i = 0; i < count; i++)
1018 dm_gpio_free(dev, &desc[i]);
1023 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1025 return gpio_free_list(NULL, desc, count);
1028 /* We need to renumber the GPIOs when any driver is probed/removed */
1029 static int gpio_renumber(struct udevice *removed_dev)
1031 struct gpio_dev_priv *uc_priv;
1032 struct udevice *dev;
1037 ret = uclass_get(UCLASS_GPIO, &uc);
1041 /* Ensure that we have a base for each bank */
1043 uclass_foreach_dev(dev, uc) {
1044 if (device_active(dev) && dev != removed_dev) {
1045 uc_priv = dev_get_uclass_priv(dev);
1046 uc_priv->gpio_base = base;
1047 base += uc_priv->gpio_count;
1054 int gpio_get_number(const struct gpio_desc *desc)
1056 struct udevice *dev = desc->dev;
1057 struct gpio_dev_priv *uc_priv;
1061 uc_priv = dev->uclass_priv;
1063 return uc_priv->gpio_base + desc->offset;
1066 static int gpio_post_probe(struct udevice *dev)
1068 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1070 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1074 return gpio_renumber(NULL);
1077 static int gpio_pre_remove(struct udevice *dev)
1079 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1082 for (i = 0; i < uc_priv->gpio_count; i++) {
1083 if (uc_priv->name[i])
1084 free(uc_priv->name[i]);
1086 free(uc_priv->name);
1088 return gpio_renumber(dev);
1091 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1092 char *list_name, int index, int flags,
1093 int dtflags, struct gpio_desc *desc)
1095 struct ofnode_phandle_args args;
1097 args.node = ofnode_null();
1098 args.args_count = 2;
1099 args.args[0] = index;
1100 args.args[1] = dtflags;
1102 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1106 static int gpio_post_bind(struct udevice *dev)
1108 struct udevice *child;
1111 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1112 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1113 static int reloc_done;
1117 ops->request += gd->reloc_off;
1119 ops->rfree += gd->reloc_off;
1120 if (ops->direction_input)
1121 ops->direction_input += gd->reloc_off;
1122 if (ops->direction_output)
1123 ops->direction_output += gd->reloc_off;
1125 ops->get_value += gd->reloc_off;
1127 ops->set_value += gd->reloc_off;
1128 if (ops->get_function)
1129 ops->get_function += gd->reloc_off;
1131 ops->xlate += gd->reloc_off;
1137 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1138 dev_for_each_subnode(node, dev) {
1139 if (ofnode_read_bool(node, "gpio-hog")) {
1140 const char *name = ofnode_get_name(node);
1143 ret = device_bind_driver_to_node(dev,
1155 UCLASS_DRIVER(gpio) = {
1158 .flags = DM_UC_FLAG_SEQ_ALIAS,
1159 .post_probe = gpio_post_probe,
1160 .post_bind = gpio_post_bind,
1161 .pre_remove = gpio_pre_remove,
1162 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),