1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
8 #include <dm/device-internal.h>
10 #include <dm/uclass-internal.h>
11 #include <dt-bindings/gpio/gpio.h>
16 #include <linux/bug.h>
17 #include <linux/ctype.h>
19 DECLARE_GLOBAL_DATA_PTR;
22 * gpio_to_device() - Convert global GPIO number to device, number
24 * Convert the GPIO number to an entry in the list of GPIOs
25 * or GPIO blocks registered with the GPIO controller. Returns
26 * entry on success, NULL on error.
28 * @gpio: The numeric representation of the GPIO
29 * @desc: Returns description (desc->flags will always be 0)
30 * @return 0 if found, -ENOENT if not found
32 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
34 struct gpio_dev_priv *uc_priv;
38 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
40 ret = uclass_next_device(&dev)) {
41 uc_priv = dev_get_uclass_priv(dev);
42 if (gpio >= uc_priv->gpio_base &&
43 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
45 desc->offset = gpio - uc_priv->gpio_base;
52 return ret ? ret : -ENOENT;
55 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
57 struct gpio_dev_priv *uc_priv = NULL;
63 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
64 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
66 ret = uclass_next_device(&dev)) {
69 uc_priv = dev_get_uclass_priv(dev);
71 offset = numeric - uc_priv->gpio_base;
72 /* Allow GPIOs to be numbered from 0 */
73 if (offset < uc_priv->gpio_count)
77 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
79 if (!strncasecmp(name, uc_priv->bank_name, len)) {
80 if (!strict_strtoul(name + len, 10, &offset))
86 return ret ? ret : -EINVAL;
89 desc->offset = offset;
94 int gpio_lookup_name(const char *name, struct udevice **devp,
95 unsigned int *offsetp, unsigned int *gpiop)
97 struct gpio_desc desc;
102 ret = dm_gpio_lookup_name(name, &desc);
109 *offsetp = desc.offset;
111 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
113 *gpiop = uc_priv->gpio_base + desc.offset;
119 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
120 struct ofnode_phandle_args *args)
122 if (args->args_count < 1)
125 desc->offset = args->args[0];
127 if (args->args_count < 2)
130 if (args->args[1] & GPIO_ACTIVE_LOW)
131 desc->flags = GPIOD_ACTIVE_LOW;
136 static int gpio_find_and_xlate(struct gpio_desc *desc,
137 struct ofnode_phandle_args *args)
139 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
142 return ops->xlate(desc->dev, desc, args);
144 return gpio_xlate_offs_flags(desc->dev, desc, args);
147 #if defined(CONFIG_DM_GPIO_HOG)
149 struct gpio_hog_priv {
150 struct gpio_desc gpiod;
153 struct gpio_hog_data {
159 static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
161 struct gpio_hog_data *plat = dev_get_platdata(dev);
162 const char *nodename;
166 if (dev_read_bool(dev, "input")) {
167 plat->gpiod_flags = GPIOD_IS_IN;
168 } else if (dev_read_bool(dev, "output-high")) {
170 plat->gpiod_flags = GPIOD_IS_OUT;
171 } else if (dev_read_bool(dev, "output-low")) {
172 plat->gpiod_flags = GPIOD_IS_OUT;
174 printf("%s: missing gpio-hog state.\n", __func__);
177 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
179 printf("%s: wrong gpios property, 2 values needed %d\n",
183 nodename = dev_read_string(dev, "line-name");
185 nodename = dev_read_name(dev);
186 device_set_name(dev, nodename);
191 static int gpio_hog_probe(struct udevice *dev)
193 struct gpio_hog_data *plat = dev_get_platdata(dev);
194 struct gpio_hog_priv *priv = dev_get_priv(dev);
197 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
198 plat->val[0], plat->gpiod_flags,
199 plat->val[1], &priv->gpiod);
201 debug("%s: node %s could not get gpio.\n", __func__,
205 dm_gpio_set_dir(&priv->gpiod);
206 if (plat->gpiod_flags == GPIOD_IS_OUT)
207 dm_gpio_set_value(&priv->gpiod, plat->value);
212 int gpio_hog_probe_all(void)
217 for (uclass_first_device(UCLASS_NOP, &dev);
219 uclass_find_next_device(&dev)) {
220 if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
221 ret = device_probe(dev);
230 struct gpio_desc *gpio_hog_lookup_name(const char *name)
234 gpio_hog_probe_all();
235 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
236 struct gpio_hog_priv *priv = dev_get_priv(dev);
244 U_BOOT_DRIVER(gpio_hog) = {
247 .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
248 .probe = gpio_hog_probe,
249 .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
250 .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
253 struct gpio_desc *gpio_hog_lookup_name(const char *name)
259 int dm_gpio_request(struct gpio_desc *desc, const char *label)
261 struct udevice *dev = desc->dev;
262 struct gpio_dev_priv *uc_priv;
266 uc_priv = dev_get_uclass_priv(dev);
267 if (uc_priv->name[desc->offset])
272 if (gpio_get_ops(dev)->request) {
273 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
279 uc_priv->name[desc->offset] = str;
284 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
286 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
291 vscnprintf(buf, sizeof(buf), fmt, args);
293 return dm_gpio_request(desc, buf);
295 return dm_gpio_request(desc, fmt);
300 * gpio_request() - [COMPAT] Request GPIO
302 * label: Name for the requested GPIO
304 * The label is copied and allocated so the caller does not need to keep
305 * the pointer around.
307 * This function implements the API that's compatible with current
308 * GPIO API used in U-Boot. The request is forwarded to particular
309 * GPIO driver. Returns 0 on success, negative value on error.
311 int gpio_request(unsigned gpio, const char *label)
313 struct gpio_desc desc;
316 ret = gpio_to_device(gpio, &desc);
320 return dm_gpio_request(&desc, label);
324 * gpio_requestf() - [COMPAT] Request GPIO
326 * @fmt: Format string for the requested GPIO
327 * @...: Arguments for the printf() format string
329 * This function implements the API that's compatible with current
330 * GPIO API used in U-Boot. The request is forwarded to particular
331 * GPIO driver. Returns 0 on success, negative value on error.
333 int gpio_requestf(unsigned gpio, const char *fmt, ...)
335 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
340 vscnprintf(buf, sizeof(buf), fmt, args);
342 return gpio_request(gpio, buf);
344 return gpio_request(gpio, fmt);
348 int _dm_gpio_free(struct udevice *dev, uint offset)
350 struct gpio_dev_priv *uc_priv;
353 uc_priv = dev_get_uclass_priv(dev);
354 if (!uc_priv->name[offset])
356 if (gpio_get_ops(dev)->free) {
357 ret = gpio_get_ops(dev)->free(dev, offset);
362 free(uc_priv->name[offset]);
363 uc_priv->name[offset] = NULL;
369 * gpio_free() - [COMPAT] Relinquish GPIO
372 * This function implements the API that's compatible with current
373 * GPIO API used in U-Boot. The request is forwarded to particular
374 * GPIO driver. Returns 0 on success, negative value on error.
376 int gpio_free(unsigned gpio)
378 struct gpio_desc desc;
381 ret = gpio_to_device(gpio, &desc);
385 return _dm_gpio_free(desc.dev, desc.offset);
388 static int check_reserved(const struct gpio_desc *desc, const char *func)
390 struct gpio_dev_priv *uc_priv;
392 if (!dm_gpio_is_valid(desc))
395 uc_priv = dev_get_uclass_priv(desc->dev);
396 if (!uc_priv->name[desc->offset]) {
397 printf("%s: %s: error: gpio %s%d not reserved\n",
398 desc->dev->name, func,
399 uc_priv->bank_name ? uc_priv->bank_name : "",
408 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
411 * This function implements the API that's compatible with current
412 * GPIO API used in U-Boot. The request is forwarded to particular
413 * GPIO driver. Returns 0 on success, negative value on error.
415 int gpio_direction_input(unsigned gpio)
417 struct gpio_desc desc;
420 ret = gpio_to_device(gpio, &desc);
423 ret = check_reserved(&desc, "dir_input");
427 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
431 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
433 * value: Logical value to be set on the GPIO pin
435 * This function implements the API that's compatible with current
436 * GPIO API used in U-Boot. The request is forwarded to particular
437 * GPIO driver. Returns 0 on success, negative value on error.
439 int gpio_direction_output(unsigned gpio, int value)
441 struct gpio_desc desc;
444 ret = gpio_to_device(gpio, &desc);
447 ret = check_reserved(&desc, "dir_output");
451 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
455 int dm_gpio_get_value(const struct gpio_desc *desc)
460 ret = check_reserved(desc, "get_value");
464 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
466 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
469 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
473 ret = check_reserved(desc, "set_value");
477 if (desc->flags & GPIOD_ACTIVE_LOW)
479 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
483 int dm_gpio_get_open_drain(struct gpio_desc *desc)
485 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
488 ret = check_reserved(desc, "get_open_drain");
492 if (ops->set_open_drain)
493 return ops->get_open_drain(desc->dev, desc->offset);
498 int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
500 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
503 ret = check_reserved(desc, "set_open_drain");
507 if (ops->set_open_drain)
508 ret = ops->set_open_drain(desc->dev, desc->offset, value);
510 return 0; /* feature not supported -> ignore setting */
515 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
517 struct udevice *dev = desc->dev;
518 struct dm_gpio_ops *ops = gpio_get_ops(dev);
521 ret = check_reserved(desc, "set_dir");
525 if (flags & GPIOD_IS_OUT) {
526 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
528 if (flags & GPIOD_ACTIVE_LOW)
530 ret = ops->direction_output(dev, desc->offset, value);
531 } else if (flags & GPIOD_IS_IN) {
532 ret = ops->direction_input(dev, desc->offset);
537 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
545 int dm_gpio_set_dir(struct gpio_desc *desc)
547 return dm_gpio_set_dir_flags(desc, desc->flags);
551 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
554 * This function implements the API that's compatible with current
555 * GPIO API used in U-Boot. The request is forwarded to particular
556 * GPIO driver. Returns the value of the GPIO pin, or negative value
559 int gpio_get_value(unsigned gpio)
563 struct gpio_desc desc;
565 ret = gpio_to_device(gpio, &desc);
568 return dm_gpio_get_value(&desc);
572 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
574 * value: Logical value to be set on the GPIO pin.
576 * This function implements the API that's compatible with current
577 * GPIO API used in U-Boot. The request is forwarded to particular
578 * GPIO driver. Returns 0 on success, negative value on error.
580 int gpio_set_value(unsigned gpio, int value)
582 struct gpio_desc desc;
585 ret = gpio_to_device(gpio, &desc);
588 return dm_gpio_set_value(&desc, value);
591 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
593 struct gpio_dev_priv *priv;
595 /* Must be called on an active device */
596 priv = dev_get_uclass_priv(dev);
599 *bit_count = priv->gpio_count;
600 return priv->bank_name;
603 static const char * const gpio_function[GPIOF_COUNT] = {
611 static int get_function(struct udevice *dev, int offset, bool skip_unused,
614 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
615 struct dm_gpio_ops *ops = gpio_get_ops(dev);
617 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
618 if (!device_active(dev))
620 if (offset < 0 || offset >= uc_priv->gpio_count)
623 *namep = uc_priv->name[offset];
624 if (skip_unused && !uc_priv->name[offset])
626 if (ops->get_function) {
629 ret = ops->get_function(dev, offset);
632 if (ret >= ARRAY_SIZE(gpio_function))
637 return GPIOF_UNKNOWN;
640 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
642 return get_function(dev, offset, true, namep);
645 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
647 return get_function(dev, offset, false, namep);
650 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
652 struct dm_gpio_ops *ops = gpio_get_ops(dev);
653 struct gpio_dev_priv *priv;
659 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
662 priv = dev_get_uclass_priv(dev);
663 ret = gpio_get_raw_function(dev, offset, NULL);
667 len = snprintf(str, buffsize, "%s%d: %s",
668 priv->bank_name ? priv->bank_name : "",
669 offset, gpio_function[func]);
670 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
671 func == GPIOF_UNUSED) {
675 ret = ops->get_value(dev, offset);
678 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
679 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
689 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
694 for (i = 0; i < 32; i++) {
695 gpio = gpio_num_array[i];
698 ret = gpio_requestf(gpio, fmt, i);
701 ret = gpio_direction_input(gpio);
710 for (i--; i >= 0; i--)
711 gpio_free(gpio_num_array[i]);
717 * get a number comprised of multiple GPIO values. gpio_num_array points to
718 * the array of gpio pin numbers to scan, terminated by -1.
720 int gpio_get_values_as_int(const int *gpio_list)
723 unsigned bitmask = 1;
728 ((gpio = *gpio_list++) != -1)) {
729 ret = gpio_get_value(gpio);
740 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
742 unsigned bitmask = 1;
746 for (i = 0; i < count; i++) {
747 ret = dm_gpio_get_value(&desc_list[i]);
758 static int gpio_request_tail(int ret, const char *nodename,
759 struct ofnode_phandle_args *args,
760 const char *list_name, int index,
761 struct gpio_desc *desc, int flags,
762 bool add_index, struct udevice *dev)
771 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
774 debug("%s: uclass_get_device_by_ofnode failed\n", __func__);
778 ret = gpio_find_and_xlate(desc, args);
780 debug("%s: gpio_find_and_xlate failed\n", __func__);
783 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
784 nodename, list_name, index);
786 debug("%s: dm_gpio_requestf failed\n", __func__);
789 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
791 debug("%s: dm_gpio_set_dir failed\n", __func__);
797 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
798 __func__, nodename, list_name, index, ret);
802 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
803 int index, struct gpio_desc *desc,
804 int flags, bool add_index)
806 struct ofnode_phandle_args args;
809 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
812 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
813 index, desc, flags, add_index, NULL);
816 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
817 struct gpio_desc *desc, int flags)
819 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
823 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
824 struct gpio_desc *desc, int flags)
826 struct ofnode_phandle_args args;
830 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
832 node = dev_ofnode(dev);
833 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
834 index, desc, flags, index > 0, NULL);
837 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
838 struct gpio_desc *desc, int max_count,
844 for (count = 0; count < max_count; count++) {
845 ret = _gpio_request_by_name_nodev(node, list_name, count,
846 &desc[count], flags, true);
853 /* We ran out of GPIOs in the list */
857 gpio_free_list_nodev(desc, count - 1);
862 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
863 struct gpio_desc *desc, int max_count,
867 * This isn't ideal since we don't use dev->name in the debug()
868 * calls in gpio_request_by_name(), but we can do this until
869 * gpio_request_list_by_name_nodev() can be dropped.
871 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
875 int gpio_get_list_count(struct udevice *dev, const char *list_name)
879 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
880 list_name, "#gpio-cells", 0, -1,
883 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
884 __func__, dev->name, list_name, ret);
890 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
892 /* For now, we don't do any checking of dev */
893 return _dm_gpio_free(desc->dev, desc->offset);
896 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
900 /* For now, we don't do any checking of dev */
901 for (i = 0; i < count; i++)
902 dm_gpio_free(dev, &desc[i]);
907 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
909 return gpio_free_list(NULL, desc, count);
912 /* We need to renumber the GPIOs when any driver is probed/removed */
913 static int gpio_renumber(struct udevice *removed_dev)
915 struct gpio_dev_priv *uc_priv;
921 ret = uclass_get(UCLASS_GPIO, &uc);
925 /* Ensure that we have a base for each bank */
927 uclass_foreach_dev(dev, uc) {
928 if (device_active(dev) && dev != removed_dev) {
929 uc_priv = dev_get_uclass_priv(dev);
930 uc_priv->gpio_base = base;
931 base += uc_priv->gpio_count;
938 int gpio_get_number(const struct gpio_desc *desc)
940 struct udevice *dev = desc->dev;
941 struct gpio_dev_priv *uc_priv;
945 uc_priv = dev->uclass_priv;
947 return uc_priv->gpio_base + desc->offset;
950 static int gpio_post_probe(struct udevice *dev)
952 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
954 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
958 return gpio_renumber(NULL);
961 static int gpio_pre_remove(struct udevice *dev)
963 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
966 for (i = 0; i < uc_priv->gpio_count; i++) {
967 if (uc_priv->name[i])
968 free(uc_priv->name[i]);
972 return gpio_renumber(dev);
975 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
976 char *list_name, int index, int flags,
977 int dtflags, struct gpio_desc *desc)
979 struct ofnode_phandle_args args;
981 args.node = ofnode_null();
983 args.args[0] = index;
984 args.args[1] = dtflags;
986 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
990 static int gpio_post_bind(struct udevice *dev)
992 #if defined(CONFIG_DM_GPIO_HOG)
993 struct udevice *child;
997 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
998 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
999 static int reloc_done;
1003 ops->request += gd->reloc_off;
1005 ops->free += gd->reloc_off;
1006 if (ops->direction_input)
1007 ops->direction_input += gd->reloc_off;
1008 if (ops->direction_output)
1009 ops->direction_output += gd->reloc_off;
1011 ops->get_value += gd->reloc_off;
1013 ops->set_value += gd->reloc_off;
1014 if (ops->get_open_drain)
1015 ops->get_open_drain += gd->reloc_off;
1016 if (ops->set_open_drain)
1017 ops->set_open_drain += gd->reloc_off;
1018 if (ops->get_function)
1019 ops->get_function += gd->reloc_off;
1021 ops->xlate += gd->reloc_off;
1027 #if defined(CONFIG_DM_GPIO_HOG)
1028 dev_for_each_subnode(node, dev) {
1029 if (ofnode_read_bool(node, "gpio-hog")) {
1030 const char *name = ofnode_get_name(node);
1032 device_bind_driver_to_node(dev, "gpio_hog", name,
1040 UCLASS_DRIVER(gpio) = {
1043 .flags = DM_UC_FLAG_SEQ_ALIAS,
1044 .post_probe = gpio_post_probe,
1045 .post_bind = gpio_post_bind,
1046 .pre_remove = gpio_pre_remove,
1047 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),