1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
8 #include <dt-bindings/gpio/gpio.h>
13 #include <linux/bug.h>
14 #include <linux/ctype.h>
16 DECLARE_GLOBAL_DATA_PTR;
19 * gpio_to_device() - Convert global GPIO number to device, number
21 * Convert the GPIO number to an entry in the list of GPIOs
22 * or GPIO blocks registered with the GPIO controller. Returns
23 * entry on success, NULL on error.
25 * @gpio: The numeric representation of the GPIO
26 * @desc: Returns description (desc->flags will always be 0)
27 * @return 0 if found, -ENOENT if not found
29 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
31 struct gpio_dev_priv *uc_priv;
35 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
37 ret = uclass_next_device(&dev)) {
38 uc_priv = dev_get_uclass_priv(dev);
39 if (gpio >= uc_priv->gpio_base &&
40 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
42 desc->offset = gpio - uc_priv->gpio_base;
49 return ret ? ret : -ENOENT;
52 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
54 struct gpio_dev_priv *uc_priv = NULL;
60 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
61 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
63 ret = uclass_next_device(&dev)) {
66 uc_priv = dev_get_uclass_priv(dev);
68 offset = numeric - uc_priv->gpio_base;
69 /* Allow GPIOs to be numbered from 0 */
70 if (offset < uc_priv->gpio_count)
74 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
76 if (!strncasecmp(name, uc_priv->bank_name, len)) {
77 if (!strict_strtoul(name + len, 10, &offset))
83 return ret ? ret : -EINVAL;
86 desc->offset = offset;
91 int gpio_lookup_name(const char *name, struct udevice **devp,
92 unsigned int *offsetp, unsigned int *gpiop)
94 struct gpio_desc desc;
99 ret = dm_gpio_lookup_name(name, &desc);
106 *offsetp = desc.offset;
108 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
110 *gpiop = uc_priv->gpio_base + desc.offset;
116 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
117 struct ofnode_phandle_args *args)
119 if (args->args_count < 1)
122 desc->offset = args->args[0];
124 if (args->args_count < 2)
127 if (args->args[1] & GPIO_ACTIVE_LOW)
128 desc->flags = GPIOD_ACTIVE_LOW;
133 static int gpio_find_and_xlate(struct gpio_desc *desc,
134 struct ofnode_phandle_args *args)
136 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
139 return ops->xlate(desc->dev, desc, args);
141 return gpio_xlate_offs_flags(desc->dev, desc, args);
144 int dm_gpio_request(struct gpio_desc *desc, const char *label)
146 struct udevice *dev = desc->dev;
147 struct gpio_dev_priv *uc_priv;
151 uc_priv = dev_get_uclass_priv(dev);
152 if (uc_priv->name[desc->offset])
157 if (gpio_get_ops(dev)->request) {
158 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
164 uc_priv->name[desc->offset] = str;
169 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
171 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
176 vscnprintf(buf, sizeof(buf), fmt, args);
178 return dm_gpio_request(desc, buf);
180 return dm_gpio_request(desc, fmt);
185 * gpio_request() - [COMPAT] Request GPIO
187 * label: Name for the requested GPIO
189 * The label is copied and allocated so the caller does not need to keep
190 * the pointer around.
192 * This function implements the API that's compatible with current
193 * GPIO API used in U-Boot. The request is forwarded to particular
194 * GPIO driver. Returns 0 on success, negative value on error.
196 int gpio_request(unsigned gpio, const char *label)
198 struct gpio_desc desc;
201 ret = gpio_to_device(gpio, &desc);
205 return dm_gpio_request(&desc, label);
209 * gpio_requestf() - [COMPAT] Request GPIO
211 * @fmt: Format string for the requested GPIO
212 * @...: Arguments for the printf() format string
214 * This function implements the API that's compatible with current
215 * GPIO API used in U-Boot. The request is forwarded to particular
216 * GPIO driver. Returns 0 on success, negative value on error.
218 int gpio_requestf(unsigned gpio, const char *fmt, ...)
220 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
225 vscnprintf(buf, sizeof(buf), fmt, args);
227 return gpio_request(gpio, buf);
229 return gpio_request(gpio, fmt);
233 int _dm_gpio_free(struct udevice *dev, uint offset)
235 struct gpio_dev_priv *uc_priv;
238 uc_priv = dev_get_uclass_priv(dev);
239 if (!uc_priv->name[offset])
241 if (gpio_get_ops(dev)->free) {
242 ret = gpio_get_ops(dev)->free(dev, offset);
247 free(uc_priv->name[offset]);
248 uc_priv->name[offset] = NULL;
254 * gpio_free() - [COMPAT] Relinquish GPIO
257 * This function implements the API that's compatible with current
258 * GPIO API used in U-Boot. The request is forwarded to particular
259 * GPIO driver. Returns 0 on success, negative value on error.
261 int gpio_free(unsigned gpio)
263 struct gpio_desc desc;
266 ret = gpio_to_device(gpio, &desc);
270 return _dm_gpio_free(desc.dev, desc.offset);
273 static int check_reserved(const struct gpio_desc *desc, const char *func)
275 struct gpio_dev_priv *uc_priv;
277 if (!dm_gpio_is_valid(desc))
280 uc_priv = dev_get_uclass_priv(desc->dev);
281 if (!uc_priv->name[desc->offset]) {
282 printf("%s: %s: error: gpio %s%d not reserved\n",
283 desc->dev->name, func,
284 uc_priv->bank_name ? uc_priv->bank_name : "",
293 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
296 * This function implements the API that's compatible with current
297 * GPIO API used in U-Boot. The request is forwarded to particular
298 * GPIO driver. Returns 0 on success, negative value on error.
300 int gpio_direction_input(unsigned gpio)
302 struct gpio_desc desc;
305 ret = gpio_to_device(gpio, &desc);
308 ret = check_reserved(&desc, "dir_input");
312 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
316 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
318 * value: Logical value to be set on the GPIO pin
320 * This function implements the API that's compatible with current
321 * GPIO API used in U-Boot. The request is forwarded to particular
322 * GPIO driver. Returns 0 on success, negative value on error.
324 int gpio_direction_output(unsigned gpio, int value)
326 struct gpio_desc desc;
329 ret = gpio_to_device(gpio, &desc);
332 ret = check_reserved(&desc, "dir_output");
336 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
340 int dm_gpio_get_value(const struct gpio_desc *desc)
345 ret = check_reserved(desc, "get_value");
349 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
351 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
354 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
358 ret = check_reserved(desc, "set_value");
362 if (desc->flags & GPIOD_ACTIVE_LOW)
364 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
368 int dm_gpio_get_open_drain(struct gpio_desc *desc)
370 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
373 ret = check_reserved(desc, "get_open_drain");
377 if (ops->set_open_drain)
378 return ops->get_open_drain(desc->dev, desc->offset);
383 int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
385 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
388 ret = check_reserved(desc, "set_open_drain");
392 if (ops->set_open_drain)
393 ret = ops->set_open_drain(desc->dev, desc->offset, value);
395 return 0; /* feature not supported -> ignore setting */
400 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
402 struct udevice *dev = desc->dev;
403 struct dm_gpio_ops *ops = gpio_get_ops(dev);
406 ret = check_reserved(desc, "set_dir");
410 if (flags & GPIOD_IS_OUT) {
411 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
413 if (flags & GPIOD_ACTIVE_LOW)
415 ret = ops->direction_output(dev, desc->offset, value);
416 } else if (flags & GPIOD_IS_IN) {
417 ret = ops->direction_input(dev, desc->offset);
422 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
430 int dm_gpio_set_dir(struct gpio_desc *desc)
432 return dm_gpio_set_dir_flags(desc, desc->flags);
436 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
439 * This function implements the API that's compatible with current
440 * GPIO API used in U-Boot. The request is forwarded to particular
441 * GPIO driver. Returns the value of the GPIO pin, or negative value
444 int gpio_get_value(unsigned gpio)
448 struct gpio_desc desc;
450 ret = gpio_to_device(gpio, &desc);
453 return dm_gpio_get_value(&desc);
457 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
459 * value: Logical value to be set on the GPIO pin.
461 * This function implements the API that's compatible with current
462 * GPIO API used in U-Boot. The request is forwarded to particular
463 * GPIO driver. Returns 0 on success, negative value on error.
465 int gpio_set_value(unsigned gpio, int value)
467 struct gpio_desc desc;
470 ret = gpio_to_device(gpio, &desc);
473 return dm_gpio_set_value(&desc, value);
476 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
478 struct gpio_dev_priv *priv;
480 /* Must be called on an active device */
481 priv = dev_get_uclass_priv(dev);
484 *bit_count = priv->gpio_count;
485 return priv->bank_name;
488 static const char * const gpio_function[GPIOF_COUNT] = {
496 static int get_function(struct udevice *dev, int offset, bool skip_unused,
499 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
500 struct dm_gpio_ops *ops = gpio_get_ops(dev);
502 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
503 if (!device_active(dev))
505 if (offset < 0 || offset >= uc_priv->gpio_count)
508 *namep = uc_priv->name[offset];
509 if (skip_unused && !uc_priv->name[offset])
511 if (ops->get_function) {
514 ret = ops->get_function(dev, offset);
517 if (ret >= ARRAY_SIZE(gpio_function))
522 return GPIOF_UNKNOWN;
525 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
527 return get_function(dev, offset, true, namep);
530 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
532 return get_function(dev, offset, false, namep);
535 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
537 struct dm_gpio_ops *ops = gpio_get_ops(dev);
538 struct gpio_dev_priv *priv;
544 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
547 priv = dev_get_uclass_priv(dev);
548 ret = gpio_get_raw_function(dev, offset, NULL);
552 len = snprintf(str, buffsize, "%s%d: %s",
553 priv->bank_name ? priv->bank_name : "",
554 offset, gpio_function[func]);
555 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
556 func == GPIOF_UNUSED) {
560 ret = ops->get_value(dev, offset);
563 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
564 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
574 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
579 for (i = 0; i < 32; i++) {
580 gpio = gpio_num_array[i];
583 ret = gpio_requestf(gpio, fmt, i);
586 ret = gpio_direction_input(gpio);
595 for (i--; i >= 0; i--)
596 gpio_free(gpio_num_array[i]);
602 * get a number comprised of multiple GPIO values. gpio_num_array points to
603 * the array of gpio pin numbers to scan, terminated by -1.
605 int gpio_get_values_as_int(const int *gpio_list)
608 unsigned bitmask = 1;
613 ((gpio = *gpio_list++) != -1)) {
614 ret = gpio_get_value(gpio);
625 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
627 unsigned bitmask = 1;
631 for (i = 0; i < count; i++) {
632 ret = dm_gpio_get_value(&desc_list[i]);
643 static int gpio_request_tail(int ret, ofnode node,
644 struct ofnode_phandle_args *args,
645 const char *list_name, int index,
646 struct gpio_desc *desc, int flags, bool add_index)
654 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
657 debug("%s: uclass_get_device_by_ofnode failed\n", __func__);
660 ret = gpio_find_and_xlate(desc, args);
662 debug("%s: gpio_find_and_xlate failed\n", __func__);
665 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
666 ofnode_get_name(node),
669 debug("%s: dm_gpio_requestf failed\n", __func__);
672 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
674 debug("%s: dm_gpio_set_dir failed\n", __func__);
680 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
681 __func__, ofnode_get_name(node), list_name, index, ret);
685 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
686 int index, struct gpio_desc *desc,
687 int flags, bool add_index)
689 struct ofnode_phandle_args args;
692 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
695 return gpio_request_tail(ret, node, &args, list_name, index, desc,
699 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
700 struct gpio_desc *desc, int flags)
702 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
706 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
707 struct gpio_desc *desc, int flags)
709 struct ofnode_phandle_args args;
712 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
715 return gpio_request_tail(ret, dev_ofnode(dev), &args, list_name,
716 index, desc, flags, index > 0);
719 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
720 struct gpio_desc *desc, int max_count,
726 for (count = 0; count < max_count; count++) {
727 ret = _gpio_request_by_name_nodev(node, list_name, count,
728 &desc[count], flags, true);
735 /* We ran out of GPIOs in the list */
739 gpio_free_list_nodev(desc, count - 1);
744 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
745 struct gpio_desc *desc, int max_count,
749 * This isn't ideal since we don't use dev->name in the debug()
750 * calls in gpio_request_by_name(), but we can do this until
751 * gpio_request_list_by_name_nodev() can be dropped.
753 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
757 int gpio_get_list_count(struct udevice *dev, const char *list_name)
761 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
762 list_name, "#gpio-cells", 0, -1,
765 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
766 __func__, dev->name, list_name, ret);
772 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
774 /* For now, we don't do any checking of dev */
775 return _dm_gpio_free(desc->dev, desc->offset);
778 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
782 /* For now, we don't do any checking of dev */
783 for (i = 0; i < count; i++)
784 dm_gpio_free(dev, &desc[i]);
789 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
791 return gpio_free_list(NULL, desc, count);
794 /* We need to renumber the GPIOs when any driver is probed/removed */
795 static int gpio_renumber(struct udevice *removed_dev)
797 struct gpio_dev_priv *uc_priv;
803 ret = uclass_get(UCLASS_GPIO, &uc);
807 /* Ensure that we have a base for each bank */
809 uclass_foreach_dev(dev, uc) {
810 if (device_active(dev) && dev != removed_dev) {
811 uc_priv = dev_get_uclass_priv(dev);
812 uc_priv->gpio_base = base;
813 base += uc_priv->gpio_count;
820 int gpio_get_number(const struct gpio_desc *desc)
822 struct udevice *dev = desc->dev;
823 struct gpio_dev_priv *uc_priv;
827 uc_priv = dev->uclass_priv;
829 return uc_priv->gpio_base + desc->offset;
832 static int gpio_post_probe(struct udevice *dev)
834 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
836 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
840 return gpio_renumber(NULL);
843 static int gpio_pre_remove(struct udevice *dev)
845 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
848 for (i = 0; i < uc_priv->gpio_count; i++) {
849 if (uc_priv->name[i])
850 free(uc_priv->name[i]);
854 return gpio_renumber(dev);
857 static int gpio_post_bind(struct udevice *dev)
859 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
860 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
861 static int reloc_done;
865 ops->request += gd->reloc_off;
867 ops->free += gd->reloc_off;
868 if (ops->direction_input)
869 ops->direction_input += gd->reloc_off;
870 if (ops->direction_output)
871 ops->direction_output += gd->reloc_off;
873 ops->get_value += gd->reloc_off;
875 ops->set_value += gd->reloc_off;
876 if (ops->get_open_drain)
877 ops->get_open_drain += gd->reloc_off;
878 if (ops->set_open_drain)
879 ops->set_open_drain += gd->reloc_off;
880 if (ops->get_function)
881 ops->get_function += gd->reloc_off;
883 ops->xlate += gd->reloc_off;
891 UCLASS_DRIVER(gpio) = {
894 .flags = DM_UC_FLAG_SEQ_ALIAS,
895 .post_probe = gpio_post_probe,
896 .post_bind = gpio_post_bind,
897 .pre_remove = gpio_pre_remove,
898 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),