From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Linus Walleij <linus.walleij@linaro.org>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Mika Westerberg <mika.westerberg@linux.intel.com>,
linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org,
Bartosz Golaszewski <bgolaszewski@baylibre.com>
Subject: [PATCH 11/11] gpiolib: add support for software nodes
Date: Wed, 11 Sep 2019 00:52:15 -0700 [thread overview]
Message-ID: <20190911075215.78047-12-dmitry.torokhov@gmail.com> (raw)
In-Reply-To: <20190911075215.78047-1-dmitry.torokhov@gmail.com>
Now that static device properties understand notion of child nodes and
references, let's teach gpiolib to handle them:
- GPIOs are represented as a references to software nodes representing
gpiochip
- references must have 2 arguments - GPIO number within the chip and
GPIO flags (GPIO_ACTIVE_LOW/GPIO_ACTIVE_HIGH, etc).
- name of the software node representing gpiochip must match label of
the gpiochip, as we use it to locate gpiochip structure at runtime.
const struct software_node gpio_bank_b_node = {
.name = "B",
};
const struct property_entry simone_key_enter_props[] __initconst = {
PROPERTY_ENTRY_U32("linux,code", KEY_ENTER),
PROPERTY_ENTRY_STRING("label", "enter"),
PROPERTY_ENTRY_REF("gpios", &gpio_bank_b_node, 123, GPIO_ACTIVE_LOW),
{ }
};
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/gpio/Makefile | 1 +
drivers/gpio/gpiolib-swnode.c | 92 +++++++++++++++++++++++++++++++++++
drivers/gpio/gpiolib-swnode.h | 13 +++++
drivers/gpio/gpiolib.c | 31 ++++++++++--
4 files changed, 133 insertions(+), 4 deletions(-)
create mode 100644 drivers/gpio/gpiolib-swnode.c
create mode 100644 drivers/gpio/gpiolib-swnode.h
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index d2fd19c15bae..41869ba725e2 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_GPIOLIB) += gpiolib.o
obj-$(CONFIG_GPIOLIB) += gpiolib-devres.o
obj-$(CONFIG_GPIOLIB) += gpiolib-legacy.o
obj-$(CONFIG_GPIOLIB) += gpiolib-devprop.o
+obj-$(CONFIG_GPIOLIB) += gpiolib-swnode.o
obj-$(CONFIG_OF_GPIO) += gpiolib-of.o
obj-$(CONFIG_GPIO_SYSFS) += gpiolib-sysfs.o
obj-$(CONFIG_GPIO_ACPI) += gpiolib-acpi.o
diff --git a/drivers/gpio/gpiolib-swnode.c b/drivers/gpio/gpiolib-swnode.c
new file mode 100644
index 000000000000..a20d73cc9a3f
--- /dev/null
+++ b/drivers/gpio/gpiolib-swnode.c
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Software Node helpers for the GPIO API
+ *
+ * Copyright 2019 Google LLC
+ */
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/gpio/consumer.h>
+
+#include "gpiolib.h"
+#include "gpiolib-swnode.h"
+
+static int swnode_gpiochip_match_name(struct gpio_chip *chip, void *data)
+{
+ return !strcmp(chip->label, data);
+}
+
+struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
+ const char *con_id, unsigned int idx,
+ unsigned long *flags)
+{
+ const struct software_node *chip_node;
+ const struct software_node *swnode;
+ struct fwnode_reference_args args;
+ struct gpio_chip *chip;
+ char prop_name[32]; /* 32 is max size of property name */
+ int error;
+
+ swnode = to_software_node(fwnode);
+ if (!swnode)
+ return ERR_PTR(-EINVAL);
+
+ /*
+ * Note we do not need to try both -gpios and -gpio suffixes,
+ * as, unlike OF and ACPI, we can fix software nodes to conform
+ * to the proper binding.
+ */
+ if (con_id)
+ snprintf(prop_name, sizeof(prop_name), "%s-gpios", con_id);
+ else
+ stracpy(prop_name, "gpios");
+
+ /*
+ * We expect all swnode-described GPIOs have GPIO number and
+ * polarity arguments, hence nargs is set to 2.
+ */
+ error = fwnode_property_get_reference_args(fwnode, prop_name, NULL,
+ 2, idx, &args);
+ if (error) {
+ pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
+ __func__, prop_name, swnode->name ?: "unnamed", idx);
+ return ERR_PTR(error);
+ }
+
+ chip_node = to_software_node(args.fwnode);
+ if (!chip_node || !chip_node->name)
+ return ERR_PTR(-EINVAL);
+
+ chip = gpiochip_find((void *)chip_node->name,
+ swnode_gpiochip_match_name);
+ if (!chip)
+ return ERR_PTR(-EPROBE_DEFER);
+
+ /* We expect native GPIO flags */
+ *flags = args.args[1];
+
+ return gpiochip_get_desc(chip, args.args[0]);
+}
+
+int swnode_gpio_count(struct fwnode_handle *fwnode, const char *con_id)
+{
+ struct fwnode_reference_args args;
+ char prop_name[32];
+ int count;
+
+ if (con_id)
+ snprintf(prop_name, sizeof(prop_name), "%s-gpios", con_id);
+ else
+ stracpy(prop_name, "gpios");
+
+ /*
+ * This is not very efficient, but GPIO lists usually have only
+ * 1 or 2 entries.
+ */
+ count = 0;
+ while (fwnode_property_get_reference_args(fwnode, prop_name, NULL,
+ 0, count, &args) == 0)
+ count++;
+
+ return count ? count : -ENOENT;
+}
diff --git a/drivers/gpio/gpiolib-swnode.h b/drivers/gpio/gpiolib-swnode.h
new file mode 100644
index 000000000000..afd51c9b6e34
--- /dev/null
+++ b/drivers/gpio/gpiolib-swnode.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef GPIOLIB_SWNODE_H
+#define GPIOLIB_SWNODE_H
+
+struct fwnode_handle;
+
+struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
+ const char *con_id, unsigned int idx,
+ unsigned long *flags);
+int swnode_gpio_count(struct fwnode_handle *fwnode, const char *con_id);
+
+#endif /* GPIOLIB_SWNODE_H */
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 6534dcd6e406..0f41b4b9b8ba 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -31,6 +31,7 @@
#include "gpiolib.h"
#include "gpiolib-of.h"
#include "gpiolib-acpi.h"
+#include "gpiolib-swnode.h"
#define CREATE_TRACE_POINTS
#include <trace/events/gpio.h>
@@ -4335,6 +4336,16 @@ static struct gpio_desc *fwnode_locate_gpiod(struct fwnode_handle *fwnode,
desc = acpi_find_gpio_fallback(ACPI_COMPANION(dev),
con_id, idx,
flags, lookupflags);
+ } else if (is_software_node(fwnode)) {
+ dev_dbg(dev, "using software node for GPIO lookup\n");
+ desc = swnode_find_gpio(fwnode, con_id, idx, lookupflags);
+ }
+
+ if (IS_ERR(desc) &&
+ !IS_ERR_OR_NULL(fwnode) &&
+ is_software_node(fwnode->secondary)) {
+ desc = swnode_find_gpio(fwnode->secondary,
+ con_id, idx, lookupflags);
}
return desc;
@@ -4397,12 +4408,24 @@ EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
*/
int gpiod_count(struct device *dev, const char *con_id)
{
+ struct fwnode_handle *fwnode;
int count = -ENOENT;
- if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
- count = of_gpio_get_count(dev, con_id);
- else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
- count = acpi_gpio_count(dev, con_id);
+ if (dev) {
+ fwnode = dev_fwnode(dev);
+ if (is_of_node(fwnode))
+ count = of_gpio_get_count(dev, con_id);
+ else if (is_acpi_device_node(fwnode))
+ count = acpi_gpio_count(dev, con_id);
+ else if (is_software_node(fwnode))
+ count = swnode_gpio_count(fwnode, con_id);
+
+ if (count < 0 &&
+ !IS_ERR_OR_NULL(fwnode) &&
+ is_software_node(fwnode->secondary)) {
+ count = swnode_gpio_count(fwnode, con_id);
+ }
+ }
if (count < 0)
count = platform_gpio_count(dev, con_id);
--
2.23.0.162.g0b9fbb3734-goog
next prev parent reply other threads:[~2019-09-11 7:52 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-11 7:52 [PATCH 00/11] Add support for software nodes to gpiolib Dmitry Torokhov
2019-09-11 7:52 ` [PATCH 01/11] gpiolib: of: add a fallback for wlf,reset GPIO name Dmitry Torokhov
2019-09-12 9:30 ` Linus Walleij
2019-09-11 7:52 ` [PATCH 02/11] gpiolib: introduce devm_fwnode_gpiod_get_index() Dmitry Torokhov
2019-09-11 17:01 ` Andy Shevchenko
2019-09-12 9:35 ` Linus Walleij
2019-09-12 9:48 ` Linus Walleij
2019-09-13 18:14 ` Dmitry Torokhov
2019-09-11 7:52 ` [PATCH 03/11] gpiolib: introduce fwnode_gpiod_get_index() Dmitry Torokhov
2019-09-12 9:50 ` Linus Walleij
2019-09-11 7:52 ` [PATCH 04/11] net: phylink: switch to using fwnode_gpiod_get_index() Dmitry Torokhov
2019-09-11 9:25 ` Andy Shevchenko
2019-09-11 9:39 ` Russell King - ARM Linux admin
2019-09-11 9:46 ` Andy Shevchenko
2019-09-11 9:49 ` Russell King - ARM Linux admin
2019-09-11 9:55 ` Dmitry Torokhov
2019-09-11 10:10 ` Russell King - ARM Linux admin
2019-09-11 16:52 ` Andy Shevchenko
2019-09-11 9:51 ` Dmitry Torokhov
2019-09-12 9:41 ` Linus Walleij
2019-09-12 13:44 ` Andy Shevchenko
2019-09-12 13:52 ` Russell King - ARM Linux admin
2019-09-11 7:52 ` [PATCH 05/11] net: mdio: " Dmitry Torokhov
2019-09-11 7:52 ` [PATCH 06/11] drm/bridge: ti-tfp410: " Dmitry Torokhov
2019-09-20 23:12 ` Laurent Pinchart
2019-09-23 15:03 ` Heikki Krogerus
2019-09-23 16:00 ` Laurent Pinchart
2019-09-11 7:52 ` [PATCH 07/11] gpliolib: make fwnode_get_named_gpiod() static Dmitry Torokhov
2019-09-11 7:52 ` [PATCH 08/11] gpiolib: of: tease apart of_find_gpio() Dmitry Torokhov
2019-09-11 17:10 ` Andy Shevchenko
2019-09-11 7:52 ` [PATCH 09/11] gpiolib: of: tease apart acpi_find_gpio() Dmitry Torokhov
2019-09-11 17:12 ` Andy Shevchenko
2019-09-11 7:52 ` [PATCH 10/11] gpiolib: consolidate fwnode GPIO lookups Dmitry Torokhov
2019-09-11 7:52 ` Dmitry Torokhov [this message]
2019-09-11 17:13 ` [PATCH 00/11] Add support for software nodes to gpiolib Andy Shevchenko
2019-09-12 9:55 ` Linus Walleij
2019-09-17 0:22 ` Dmitry Torokhov
2019-09-30 22:44 ` Dmitry Torokhov
2019-10-04 21:44 ` Linus Walleij
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190911075215.78047-12-dmitry.torokhov@gmail.com \
--to=dmitry.torokhov@gmail.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=bgolaszewski@baylibre.com \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mika.westerberg@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).