1 // SPDX-License-Identifier: GPL-2.0+
3 * Software Node helpers for the GPIO API
5 * Copyright 2022 Google LLC
8 #include <linux/errno.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/kernel.h>
12 #include <linux/printk.h>
13 #include <linux/property.h>
14 #include <linux/string.h>
17 #include "gpiolib-swnode.h"
19 static void swnode_format_propname(const char *con_id, char *propname,
23 * Note we do not need to try both -gpios and -gpio suffixes,
24 * as, unlike OF and ACPI, we can fix software nodes to conform
25 * to the proper binding.
28 snprintf(propname, max_size, "%s-gpios", con_id);
30 strscpy(propname, "gpios", max_size);
33 static int swnode_gpiochip_match_name(struct gpio_chip *chip, void *data)
35 return !strcmp(chip->label, data);
38 static struct gpio_chip *swnode_get_chip(struct fwnode_handle *fwnode)
40 const struct software_node *chip_node;
41 struct gpio_chip *chip;
43 chip_node = to_software_node(fwnode);
44 if (!chip_node || !chip_node->name)
45 return ERR_PTR(-EINVAL);
47 chip = gpiochip_find((void *)chip_node->name, swnode_gpiochip_match_name);
48 return chip ?: ERR_PTR(-EPROBE_DEFER);
51 struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
52 const char *con_id, unsigned int idx,
55 const struct software_node *swnode;
56 struct fwnode_reference_args args;
57 struct gpio_chip *chip;
58 struct gpio_desc *desc;
59 char propname[32]; /* 32 is max size of property name */
62 swnode = to_software_node(fwnode);
64 return ERR_PTR(-EINVAL);
66 swnode_format_propname(con_id, propname, sizeof(propname));
69 * We expect all swnode-described GPIOs have GPIO number and
70 * polarity arguments, hence nargs is set to 2.
72 error = fwnode_property_get_reference_args(fwnode, propname, NULL, 2, idx, &args);
74 pr_debug("%s: can't parse '%s' property of node '%pfwP[%d]'\n",
75 __func__, propname, fwnode, idx);
76 return ERR_PTR(error);
79 chip = swnode_get_chip(args.fwnode);
80 fwnode_handle_put(args.fwnode);
82 return ERR_CAST(chip);
84 desc = gpiochip_get_desc(chip, args.args[0]);
85 *flags = args.args[1]; /* We expect native GPIO flags */
87 pr_debug("%s: parsed '%s' property of node '%pfwP[%d]' - status (%d)\n",
88 __func__, propname, fwnode, idx, PTR_ERR_OR_ZERO(desc));
94 * swnode_gpio_count - count the GPIOs associated with a device / function
95 * @fwnode: firmware node of the GPIO consumer, can be %NULL for
97 * @con_id: function within the GPIO consumer
100 * The number of GPIOs associated with a device / function or %-ENOENT,
101 * if no GPIO has been assigned to the requested function.
103 int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id)
105 struct fwnode_reference_args args;
109 swnode_format_propname(con_id, propname, sizeof(propname));
112 * This is not very efficient, but GPIO lists usually have only
116 while (fwnode_property_get_reference_args(fwnode, propname, NULL, 0,
117 count, &args) == 0) {
118 fwnode_handle_put(args.fwnode);
122 return count ?: -ENOENT;