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>
17 #include <dm/device_compat.h>
18 #include <linux/bug.h>
19 #include <linux/ctype.h>
21 DECLARE_GLOBAL_DATA_PTR;
24 * gpio_desc_init() - Initialize the GPIO descriptor
26 * @desc: GPIO descriptor to initialize
28 * @offset: Offset of device GPIO
30 static void gpio_desc_init(struct gpio_desc *desc,
35 desc->offset = offset;
40 * gpio_to_device() - Convert global GPIO number to device, number
42 * Convert the GPIO number to an entry in the list of GPIOs
43 * or GPIO blocks registered with the GPIO controller. Returns
44 * entry on success, NULL on error.
46 * @gpio: The numeric representation of the GPIO
47 * @desc: Returns description (desc->flags will always be 0)
48 * @return 0 if found, -ENOENT if not found
50 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
52 struct gpio_dev_priv *uc_priv;
56 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
58 ret = uclass_next_device(&dev)) {
59 uc_priv = dev_get_uclass_priv(dev);
60 if (gpio >= uc_priv->gpio_base &&
61 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
62 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
68 return ret ? ret : -ENOENT;
71 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
73 struct gpio_dev_priv *uc_priv = NULL;
79 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
80 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
82 ret = uclass_next_device(&dev)) {
85 uc_priv = dev_get_uclass_priv(dev);
87 offset = numeric - uc_priv->gpio_base;
88 /* Allow GPIOs to be numbered from 0 */
89 if (offset < uc_priv->gpio_count)
93 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
95 if (!strncasecmp(name, uc_priv->bank_name, len)) {
96 if (!strict_strtoul(name + len, 10, &offset))
102 return ret ? ret : -EINVAL;
104 gpio_desc_init(desc, dev, offset);
109 int gpio_lookup_name(const char *name, struct udevice **devp,
110 unsigned int *offsetp, unsigned int *gpiop)
112 struct gpio_desc desc;
117 ret = dm_gpio_lookup_name(name, &desc);
124 *offsetp = desc.offset;
126 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
128 *gpiop = uc_priv->gpio_base + desc.offset;
134 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
135 struct ofnode_phandle_args *args)
137 if (args->args_count < 1)
140 desc->offset = args->args[0];
142 if (args->args_count < 2)
146 if (args->args[1] & GPIO_ACTIVE_LOW)
147 desc->flags |= GPIOD_ACTIVE_LOW;
150 * need to test 2 bits for gpio output binding:
151 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
152 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
154 if (args->args[1] & GPIO_SINGLE_ENDED) {
155 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
156 desc->flags |= GPIOD_OPEN_DRAIN;
158 desc->flags |= GPIOD_OPEN_SOURCE;
161 if (args->args[1] & GPIO_PULL_UP)
162 desc->flags |= GPIOD_PULL_UP;
164 if (args->args[1] & GPIO_PULL_DOWN)
165 desc->flags |= GPIOD_PULL_DOWN;
170 static int gpio_find_and_xlate(struct gpio_desc *desc,
171 struct ofnode_phandle_args *args)
173 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
176 return ops->xlate(desc->dev, desc, args);
178 return gpio_xlate_offs_flags(desc->dev, desc, args);
181 #if defined(CONFIG_GPIO_HOG)
183 struct gpio_hog_priv {
184 struct gpio_desc gpiod;
187 struct gpio_hog_data {
193 static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
195 struct gpio_hog_data *plat = dev_get_platdata(dev);
196 const char *nodename;
200 if (dev_read_bool(dev, "input")) {
201 plat->gpiod_flags = GPIOD_IS_IN;
202 } else if (dev_read_bool(dev, "output-high")) {
204 plat->gpiod_flags = GPIOD_IS_OUT;
205 } else if (dev_read_bool(dev, "output-low")) {
206 plat->gpiod_flags = GPIOD_IS_OUT;
208 printf("%s: missing gpio-hog state.\n", __func__);
211 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
213 printf("%s: wrong gpios property, 2 values needed %d\n",
217 nodename = dev_read_string(dev, "line-name");
219 device_set_name(dev, nodename);
224 static int gpio_hog_probe(struct udevice *dev)
226 struct gpio_hog_data *plat = dev_get_platdata(dev);
227 struct gpio_hog_priv *priv = dev_get_priv(dev);
230 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
231 plat->val[0], plat->gpiod_flags,
232 plat->val[1], &priv->gpiod);
234 debug("%s: node %s could not get gpio.\n", __func__,
239 if (plat->gpiod_flags == GPIOD_IS_OUT) {
240 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
242 debug("%s: node %s could not set gpio.\n", __func__,
251 int gpio_hog_probe_all(void)
257 for (uclass_first_device(UCLASS_NOP, &dev);
259 uclass_find_next_device(&dev)) {
260 if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
261 ret = device_probe(dev);
263 printf("Failed to probe device %s err: %d\n",
273 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
278 gpio_hog_probe_all();
279 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
280 struct gpio_hog_priv *priv = dev_get_priv(dev);
282 *desc = &priv->gpiod;
289 U_BOOT_DRIVER(gpio_hog) = {
292 .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
293 .probe = gpio_hog_probe,
294 .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
295 .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
298 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
304 int dm_gpio_request(struct gpio_desc *desc, const char *label)
306 struct udevice *dev = desc->dev;
307 struct gpio_dev_priv *uc_priv;
311 uc_priv = dev_get_uclass_priv(dev);
312 if (uc_priv->name[desc->offset])
317 if (gpio_get_ops(dev)->request) {
318 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
324 uc_priv->name[desc->offset] = str;
329 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
331 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
336 vscnprintf(buf, sizeof(buf), fmt, args);
338 return dm_gpio_request(desc, buf);
340 return dm_gpio_request(desc, fmt);
345 * gpio_request() - [COMPAT] Request GPIO
347 * label: Name for the requested GPIO
349 * The label is copied and allocated so the caller does not need to keep
350 * the pointer around.
352 * This function implements the API that's compatible with current
353 * GPIO API used in U-Boot. The request is forwarded to particular
354 * GPIO driver. Returns 0 on success, negative value on error.
356 int gpio_request(unsigned gpio, const char *label)
358 struct gpio_desc desc;
361 ret = gpio_to_device(gpio, &desc);
365 return dm_gpio_request(&desc, label);
369 * gpio_requestf() - [COMPAT] Request GPIO
371 * @fmt: Format string for the requested GPIO
372 * @...: Arguments for the printf() format string
374 * This function implements the API that's compatible with current
375 * GPIO API used in U-Boot. The request is forwarded to particular
376 * GPIO driver. Returns 0 on success, negative value on error.
378 int gpio_requestf(unsigned gpio, const char *fmt, ...)
380 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
385 vscnprintf(buf, sizeof(buf), fmt, args);
387 return gpio_request(gpio, buf);
389 return gpio_request(gpio, fmt);
393 int _dm_gpio_free(struct udevice *dev, uint offset)
395 struct gpio_dev_priv *uc_priv;
398 uc_priv = dev_get_uclass_priv(dev);
399 if (!uc_priv->name[offset])
401 if (gpio_get_ops(dev)->rfree) {
402 ret = gpio_get_ops(dev)->rfree(dev, offset);
407 free(uc_priv->name[offset]);
408 uc_priv->name[offset] = NULL;
414 * gpio_free() - [COMPAT] Relinquish GPIO
417 * This function implements the API that's compatible with current
418 * GPIO API used in U-Boot. The request is forwarded to particular
419 * GPIO driver. Returns 0 on success, negative value on error.
421 int gpio_free(unsigned gpio)
423 struct gpio_desc desc;
426 ret = gpio_to_device(gpio, &desc);
430 return _dm_gpio_free(desc.dev, desc.offset);
433 static int check_reserved(const struct gpio_desc *desc, const char *func)
435 struct gpio_dev_priv *uc_priv;
437 if (!dm_gpio_is_valid(desc))
440 uc_priv = dev_get_uclass_priv(desc->dev);
441 if (!uc_priv->name[desc->offset]) {
442 printf("%s: %s: error: gpio %s%d not reserved\n",
443 desc->dev->name, func,
444 uc_priv->bank_name ? uc_priv->bank_name : "",
453 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
456 * This function implements the API that's compatible with current
457 * GPIO API used in U-Boot. The request is forwarded to particular
458 * GPIO driver. Returns 0 on success, negative value on error.
460 int gpio_direction_input(unsigned gpio)
462 struct gpio_desc desc;
465 ret = gpio_to_device(gpio, &desc);
468 ret = check_reserved(&desc, "dir_input");
472 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
476 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
478 * value: Logical value to be set on the GPIO pin
480 * This function implements the API that's compatible with current
481 * GPIO API used in U-Boot. The request is forwarded to particular
482 * GPIO driver. Returns 0 on success, negative value on error.
484 int gpio_direction_output(unsigned gpio, int value)
486 struct gpio_desc desc;
489 ret = gpio_to_device(gpio, &desc);
492 ret = check_reserved(&desc, "dir_output");
496 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
500 static int _gpio_get_value(const struct gpio_desc *desc)
504 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
506 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
509 int dm_gpio_get_value(const struct gpio_desc *desc)
513 ret = check_reserved(desc, "get_value");
517 return _gpio_get_value(desc);
520 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
524 ret = check_reserved(desc, "set_value");
528 if (desc->flags & GPIOD_ACTIVE_LOW)
532 * Emulate open drain by not actively driving the line high or
533 * Emulate open source by not actively driving the line low
535 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
536 (desc->flags & GPIOD_OPEN_SOURCE && !value))
537 return gpio_get_ops(desc->dev)->direction_input(desc->dev,
539 else if (desc->flags & GPIOD_OPEN_DRAIN ||
540 desc->flags & GPIOD_OPEN_SOURCE)
541 return gpio_get_ops(desc->dev)->direction_output(desc->dev,
545 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
549 /* check dir flags invalid configuration */
550 static int check_dir_flags(ulong flags)
552 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
553 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
558 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
559 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
564 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
565 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
573 static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
575 struct udevice *dev = desc->dev;
576 struct dm_gpio_ops *ops = gpio_get_ops(dev);
577 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
580 ret = check_dir_flags(flags);
583 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
585 uc_priv->bank_name ? uc_priv->bank_name : "",
586 desc->offset, flags);
591 /* GPIOD_ are directly managed by driver in set_dir_flags*/
592 if (ops->set_dir_flags) {
593 ret = ops->set_dir_flags(dev, desc->offset, flags);
595 if (flags & GPIOD_IS_OUT) {
596 ret = ops->direction_output(dev, desc->offset,
597 GPIOD_FLAGS_OUTPUT(flags));
598 } else if (flags & GPIOD_IS_IN) {
599 ret = ops->direction_input(dev, desc->offset);
606 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
610 ret = check_reserved(desc, "set_dir_flags");
614 /* combine the requested flags (for IN/OUT) and the descriptor flags */
615 flags |= desc->flags;
616 ret = _dm_gpio_set_dir_flags(desc, flags);
618 /* update the descriptor flags */
625 int dm_gpio_set_dir(struct gpio_desc *desc)
629 ret = check_reserved(desc, "set_dir");
633 return _dm_gpio_set_dir_flags(desc, desc->flags);
636 int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags)
638 struct udevice *dev = desc->dev;
640 struct dm_gpio_ops *ops = gpio_get_ops(dev);
643 ret = check_reserved(desc, "get_dir_flags");
647 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
648 if (ops->get_dir_flags) {
649 ret = ops->get_dir_flags(dev, desc->offset, &dir_flags);
653 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
654 value = dir_flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
655 if (desc->flags & GPIOD_ACTIVE_LOW)
657 dir_flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
658 dir_flags |= (desc->flags & GPIOD_ACTIVE_LOW);
660 dir_flags |= GPIOD_IS_OUT_ACTIVE;
662 dir_flags = desc->flags;
663 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
664 dir_flags &= ~GPIOD_IS_OUT_ACTIVE;
665 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
666 dir_flags |= GPIOD_IS_OUT_ACTIVE;
674 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
677 * This function implements the API that's compatible with current
678 * GPIO API used in U-Boot. The request is forwarded to particular
679 * GPIO driver. Returns the value of the GPIO pin, or negative value
682 int gpio_get_value(unsigned gpio)
686 struct gpio_desc desc;
688 ret = gpio_to_device(gpio, &desc);
691 return dm_gpio_get_value(&desc);
695 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
697 * value: Logical value to be set on the GPIO pin.
699 * This function implements the API that's compatible with current
700 * GPIO API used in U-Boot. The request is forwarded to particular
701 * GPIO driver. Returns 0 on success, negative value on error.
703 int gpio_set_value(unsigned gpio, int value)
705 struct gpio_desc desc;
708 ret = gpio_to_device(gpio, &desc);
711 return dm_gpio_set_value(&desc, value);
714 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
716 struct gpio_dev_priv *priv;
718 /* Must be called on an active device */
719 priv = dev_get_uclass_priv(dev);
722 *bit_count = priv->gpio_count;
723 return priv->bank_name;
726 static const char * const gpio_function[GPIOF_COUNT] = {
734 static int get_function(struct udevice *dev, int offset, bool skip_unused,
737 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
738 struct dm_gpio_ops *ops = gpio_get_ops(dev);
740 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
741 if (!device_active(dev))
743 if (offset < 0 || offset >= uc_priv->gpio_count)
746 *namep = uc_priv->name[offset];
747 if (skip_unused && !uc_priv->name[offset])
749 if (ops->get_function) {
752 ret = ops->get_function(dev, offset);
755 if (ret >= ARRAY_SIZE(gpio_function))
760 return GPIOF_UNKNOWN;
763 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
765 return get_function(dev, offset, true, namep);
768 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
770 return get_function(dev, offset, false, namep);
773 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
775 struct dm_gpio_ops *ops = gpio_get_ops(dev);
776 struct gpio_dev_priv *priv;
782 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
785 priv = dev_get_uclass_priv(dev);
786 ret = gpio_get_raw_function(dev, offset, NULL);
790 len = snprintf(str, buffsize, "%s%d: %s",
791 priv->bank_name ? priv->bank_name : "",
792 offset, gpio_function[func]);
793 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
794 func == GPIOF_UNUSED) {
798 ret = ops->get_value(dev, offset);
801 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
802 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
812 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
817 for (i = 0; i < 32; i++) {
818 gpio = gpio_num_array[i];
821 ret = gpio_requestf(gpio, fmt, i);
824 ret = gpio_direction_input(gpio);
833 for (i--; i >= 0; i--)
834 gpio_free(gpio_num_array[i]);
840 * get a number comprised of multiple GPIO values. gpio_num_array points to
841 * the array of gpio pin numbers to scan, terminated by -1.
843 int gpio_get_values_as_int(const int *gpio_list)
846 unsigned bitmask = 1;
851 ((gpio = *gpio_list++) != -1)) {
852 ret = gpio_get_value(gpio);
863 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
865 unsigned bitmask = 1;
869 for (i = 0; i < count; i++) {
870 ret = dm_gpio_get_value(&desc_list[i]);
882 * gpio_request_tail: common work for requesting a gpio.
884 * ret: return value from previous work in function which calls
886 * This seems bogus (why calling this function instead not
887 * calling it and end caller function instead?).
888 * Because on error in caller function we want to set some
889 * default values in gpio desc and have a common error
890 * debug message, which provides this function.
891 * nodename: Name of node for which gpio gets requested
892 * used for gpio label name.
893 * args: pointer to output arguments structure
894 * list_name: Name of GPIO list
895 * used for gpio label name.
896 * index: gpio index in gpio list
897 * used for gpio label name.
898 * desc: pointer to gpio descriptor, filled from this
900 * flags: gpio flags to use.
901 * add_index: should index added to gpio label name
902 * gpio_dev: pointer to gpio device from which the gpio
903 * will be requested. If NULL try to get the
904 * gpio device with uclass_get_device_by_ofnode()
906 * return: In error case this function sets default values in
907 * gpio descriptor, also emmits a debug message.
908 * On success it returns 0 else the error code from
909 * function calls, or the error code passed through
910 * ret to this function.
913 static int gpio_request_tail(int ret, const char *nodename,
914 struct ofnode_phandle_args *args,
915 const char *list_name, int index,
916 struct gpio_desc *desc, int flags,
917 bool add_index, struct udevice *gpio_dev)
919 gpio_desc_init(desc, gpio_dev, 0);
924 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
927 debug("%s: uclass_get_device_by_ofnode failed\n",
932 ret = gpio_find_and_xlate(desc, args);
934 debug("%s: gpio_find_and_xlate failed\n", __func__);
937 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
938 nodename, list_name, index);
940 debug("%s: dm_gpio_requestf failed\n", __func__);
943 ret = dm_gpio_set_dir_flags(desc, flags);
945 debug("%s: dm_gpio_set_dir failed\n", __func__);
951 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
952 __func__, nodename, list_name, index, ret);
956 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
957 int index, struct gpio_desc *desc,
958 int flags, bool add_index)
960 struct ofnode_phandle_args args;
963 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
966 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
967 index, desc, flags, add_index, NULL);
970 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
971 struct gpio_desc *desc, int flags)
973 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
977 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
978 struct gpio_desc *desc, int flags)
980 struct ofnode_phandle_args args;
984 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
986 node = dev_ofnode(dev);
987 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
988 index, desc, flags, index > 0, NULL);
991 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
992 struct gpio_desc *desc, int max_count,
998 for (count = 0; count < max_count; count++) {
999 ret = _gpio_request_by_name_nodev(node, list_name, count,
1000 &desc[count], flags, true);
1007 /* We ran out of GPIOs in the list */
1011 gpio_free_list_nodev(desc, count - 1);
1016 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1017 struct gpio_desc *desc, int max_count,
1021 * This isn't ideal since we don't use dev->name in the debug()
1022 * calls in gpio_request_by_name(), but we can do this until
1023 * gpio_request_list_by_name_nodev() can be dropped.
1025 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1029 int gpio_get_list_count(struct udevice *dev, const char *list_name)
1033 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
1034 list_name, "#gpio-cells", 0, -1,
1037 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1038 __func__, dev->name, list_name, ret);
1044 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1046 /* For now, we don't do any checking of dev */
1047 return _dm_gpio_free(desc->dev, desc->offset);
1050 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1054 /* For now, we don't do any checking of dev */
1055 for (i = 0; i < count; i++)
1056 dm_gpio_free(dev, &desc[i]);
1061 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1063 return gpio_free_list(NULL, desc, count);
1066 /* We need to renumber the GPIOs when any driver is probed/removed */
1067 static int gpio_renumber(struct udevice *removed_dev)
1069 struct gpio_dev_priv *uc_priv;
1070 struct udevice *dev;
1075 ret = uclass_get(UCLASS_GPIO, &uc);
1079 /* Ensure that we have a base for each bank */
1081 uclass_foreach_dev(dev, uc) {
1082 if (device_active(dev) && dev != removed_dev) {
1083 uc_priv = dev_get_uclass_priv(dev);
1084 uc_priv->gpio_base = base;
1085 base += uc_priv->gpio_count;
1092 int gpio_get_number(const struct gpio_desc *desc)
1094 struct udevice *dev = desc->dev;
1095 struct gpio_dev_priv *uc_priv;
1099 uc_priv = dev->uclass_priv;
1101 return uc_priv->gpio_base + desc->offset;
1104 static int gpio_post_probe(struct udevice *dev)
1106 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1108 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1112 return gpio_renumber(NULL);
1115 static int gpio_pre_remove(struct udevice *dev)
1117 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1120 for (i = 0; i < uc_priv->gpio_count; i++) {
1121 if (uc_priv->name[i])
1122 free(uc_priv->name[i]);
1124 free(uc_priv->name);
1126 return gpio_renumber(dev);
1129 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1130 char *list_name, int index, int flags,
1131 int dtflags, struct gpio_desc *desc)
1133 struct ofnode_phandle_args args;
1135 args.node = ofnode_null();
1136 args.args_count = 2;
1137 args.args[0] = index;
1138 args.args[1] = dtflags;
1140 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1144 static int gpio_post_bind(struct udevice *dev)
1146 struct udevice *child;
1149 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1150 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1151 static int reloc_done;
1155 ops->request += gd->reloc_off;
1157 ops->rfree += gd->reloc_off;
1158 if (ops->direction_input)
1159 ops->direction_input += gd->reloc_off;
1160 if (ops->direction_output)
1161 ops->direction_output += gd->reloc_off;
1163 ops->get_value += gd->reloc_off;
1165 ops->set_value += gd->reloc_off;
1166 if (ops->get_function)
1167 ops->get_function += gd->reloc_off;
1169 ops->xlate += gd->reloc_off;
1170 if (ops->set_dir_flags)
1171 ops->set_dir_flags += gd->reloc_off;
1172 if (ops->get_dir_flags)
1173 ops->get_dir_flags += gd->reloc_off;
1179 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1180 dev_for_each_subnode(node, dev) {
1181 if (ofnode_read_bool(node, "gpio-hog")) {
1182 const char *name = ofnode_get_name(node);
1185 ret = device_bind_driver_to_node(dev,
1197 UCLASS_DRIVER(gpio) = {
1200 .flags = DM_UC_FLAG_SEQ_ALIAS,
1201 .post_probe = gpio_post_probe,
1202 .post_bind = gpio_post_bind,
1203 .pre_remove = gpio_pre_remove,
1204 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),