2024-01-12 16:36:37

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH v3 0/5] reset: gpio: ASoC: shared GPIO resets

Hi,

Changes in v3
=============
1. reset-gpio.c:
- Add reset_gpio_of_xlate (Philipp).
- reset_gpio_of_args_put->reset_gpio_of_node_put (Philipp).
- Expect via platdata of_phandle_args.
- Do not call device_set_node() to attach itself to reset consumer
(the final device). This was questionable idea in the first place.
Bartosz suggested to use GPIO_LOOKUP to solve this.

2. reset/core.c, implement Philipp's feedback. That was a lot:
- Commit msg fixes.
- Add new platform_device earlier, when reset core found "reset-gpios" but
not "resets".
- Do not overwrite of_phandle_args.
- Expect matching .of_reset_n_cells.
- Pass of_phandle_args as platdata to reset-gpio.
- Rename reset_gpio_device->reset_gpio_lookup and others. Fix few comments
and code cleanup pointed on review.
- From Bartosz:
Use GPIO_LOOKUP and a lot of cleanup.h in __reset_add_reset_gpio_lookup().

3. Include here Chris' patch: "i2c: muxes: pca954x: Allow sharing reset GPIO".

Changes in v2
=============
1. wsa884x.c: add missing return in wsa884x_get_reset(), correct comment.
2. qcom,wsa8840.yaml: fix oneOf syntax.
3. reset-gpio.c:
- Fix smatch warning on platdata evaluation.
- Parse GPIO args and store them in rc.of_args.
4. reset/core.c:
- Revise approach based on Bartosz comments: parse the reset-gpios phandle
with arguments, do not use deprecated API and do not rely on gpio_desc
pointer.
- Create a list of instantiated platform devices to avoid any duplicates.
- After creating reset-gpio platform device, try to get new reset controller
or return EPROBE_DEFER.
- Drop the "cookie" member and add new "of_args" to "struct
reset_controller_dev".

Description
===========

We have at least few cases where hardware engineers decided to use one
powerdown/shutdown/reset GPIO line for multiple devices:

1. WSA884x (this and previous patch):
https://lore.kernel.org/all/[email protected]/
2. https://lore.kernel.org/all/[email protected]/
3. https://lore.kernel.org/lkml/[email protected]/
4. https://lore.kernel.org/all/[email protected]/
5. https://social.treehouse.systems/@marcan/111268780311634160

I try to solve my case, hopefuly Chris' (2), partially Sean's (4) and maybe
Hectors (5), using Rob's suggestion:

https://lore.kernel.org/all/[email protected]/

Best regards,
Krzysztof

Cc: Chris Packham <[email protected]>
Cc: Bartosz Golaszewski <[email protected]>
Cc: Sean Anderson <[email protected]>

Chris Packham (1):
i2c: muxes: pca954x: Allow sharing reset GPIO

Krzysztof Kozlowski (4):
reset: gpio: Add GPIO-based reset controller
reset: Instantiate reset GPIO controller for shared reset-gpios
ASoC: dt-bindings: qcom,wsa8840: Add reset-gpios for shared line
ASoC: codecs: wsa884x: Allow sharing reset GPIO

.../bindings/sound/qcom,wsa8840.yaml | 11 +-
MAINTAINERS | 5 +
drivers/i2c/muxes/i2c-mux-pca954x.c | 46 +++-
drivers/reset/Kconfig | 9 +
drivers/reset/Makefile | 1 +
drivers/reset/core.c | 227 ++++++++++++++++--
drivers/reset/reset-gpio.c | 121 ++++++++++
include/linux/reset-controller.h | 4 +
sound/soc/codecs/wsa884x.c | 53 +++-
9 files changed, 444 insertions(+), 33 deletions(-)
create mode 100644 drivers/reset/reset-gpio.c

--
2.34.1



2024-01-12 16:36:55

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH v3 1/5] reset: gpio: Add GPIO-based reset controller

Add a simple driver to control GPIO-based resets using the reset
controller API for the cases when the GPIOs are shared and reset should
be coordinated. The driver is expected to be used by reset core
framework for ad-hoc reset controllers.

Cc: Bartosz Golaszewski <[email protected]>
Cc: Chris Packham <[email protected]>
Cc: Sean Anderson <[email protected]>
Signed-off-by: Krzysztof Kozlowski <[email protected]>
---
MAINTAINERS | 5 ++
drivers/reset/Kconfig | 9 +++
drivers/reset/Makefile | 1 +
drivers/reset/reset-gpio.c | 121 +++++++++++++++++++++++++++++++++++++
4 files changed, 136 insertions(+)
create mode 100644 drivers/reset/reset-gpio.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 7fe27cd60e1b..a0fbd4814bc7 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8866,6 +8866,11 @@ F: Documentation/i2c/muxes/i2c-mux-gpio.rst
F: drivers/i2c/muxes/i2c-mux-gpio.c
F: include/linux/platform_data/i2c-mux-gpio.h

+GENERIC GPIO RESET DRIVER
+M: Krzysztof Kozlowski <[email protected]>
+S: Maintained
+F: drivers/reset/reset-gpio.c
+
GENERIC HDLC (WAN) DRIVERS
M: Krzysztof Halasa <[email protected]>
S: Maintained
diff --git a/drivers/reset/Kconfig b/drivers/reset/Kconfig
index ccd59ddd7610..bb1b5a326eb7 100644
--- a/drivers/reset/Kconfig
+++ b/drivers/reset/Kconfig
@@ -66,6 +66,15 @@ config RESET_BRCMSTB_RESCAL
This enables the RESCAL reset controller for SATA, PCIe0, or PCIe1 on
BCM7216.

+config RESET_GPIO
+ tristate "GPIO reset controller"
+ help
+ This enables a generic reset controller for resets attached via
+ GPIOs. Typically for OF platforms this driver expects "reset-gpios"
+ property.
+
+ If compiled as module, it will be called reset-gpio.
+
config RESET_HSDK
bool "Synopsys HSDK Reset Driver"
depends on HAS_IOMEM
diff --git a/drivers/reset/Makefile b/drivers/reset/Makefile
index 8270da8a4baa..fd8b49fa46fc 100644
--- a/drivers/reset/Makefile
+++ b/drivers/reset/Makefile
@@ -11,6 +11,7 @@ obj-$(CONFIG_RESET_BCM6345) += reset-bcm6345.o
obj-$(CONFIG_RESET_BERLIN) += reset-berlin.o
obj-$(CONFIG_RESET_BRCMSTB) += reset-brcmstb.o
obj-$(CONFIG_RESET_BRCMSTB_RESCAL) += reset-brcmstb-rescal.o
+obj-$(CONFIG_RESET_GPIO) += reset-gpio.o
obj-$(CONFIG_RESET_HSDK) += reset-hsdk.o
obj-$(CONFIG_RESET_IMX7) += reset-imx7.o
obj-$(CONFIG_RESET_INTEL_GW) += reset-intel-gw.o
diff --git a/drivers/reset/reset-gpio.c b/drivers/reset/reset-gpio.c
new file mode 100644
index 000000000000..0fe482740f1b
--- /dev/null
+++ b/drivers/reset/reset-gpio.c
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/gpio/consumer.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/reset-controller.h>
+
+struct reset_gpio_priv {
+ struct reset_controller_dev rc;
+ struct gpio_desc *reset;
+};
+
+static inline struct reset_gpio_priv
+*rc_to_reset_gpio(struct reset_controller_dev *rc)
+{
+ return container_of(rc, struct reset_gpio_priv, rc);
+}
+
+static int reset_gpio_assert(struct reset_controller_dev *rc, unsigned long id)
+{
+ struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
+
+ gpiod_set_value_cansleep(priv->reset, 1);
+
+ return 0;
+}
+
+static int reset_gpio_deassert(struct reset_controller_dev *rc,
+ unsigned long id)
+{
+ struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
+
+ gpiod_set_value_cansleep(priv->reset, 0);
+
+ return 0;
+}
+
+static int reset_gpio_status(struct reset_controller_dev *rc, unsigned long id)
+{
+ struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
+
+ return gpiod_get_value_cansleep(priv->reset);
+}
+
+static const struct reset_control_ops reset_gpio_ops = {
+ .assert = reset_gpio_assert,
+ .deassert = reset_gpio_deassert,
+ .status = reset_gpio_status,
+};
+
+static int reset_gpio_of_xlate(struct reset_controller_dev *rcdev,
+ const struct of_phandle_args *reset_spec)
+{
+ return reset_spec->args[0];
+}
+
+static void reset_gpio_of_node_put(void *data)
+{
+ of_node_put(data);
+}
+
+static int reset_gpio_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct of_phandle_args *platdata = dev_get_platdata(dev);
+ struct reset_gpio_priv *priv;
+ int ret;
+
+ if (!platdata)
+ return -EINVAL;
+
+ priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+
+ platform_set_drvdata(pdev, &priv->rc);
+
+ /* Relies on GPIO_LOOKUP */
+ priv->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
+ if (IS_ERR(priv->reset))
+ return dev_err_probe(dev, PTR_ERR(priv->reset),
+ "Could not get reset gpios\n");
+
+ priv->rc.ops = &reset_gpio_ops;
+ priv->rc.owner = THIS_MODULE;
+ priv->rc.dev = dev;
+ priv->rc.of_args = platdata;
+ priv->rc.of_node = of_node_get(platdata->np);
+ ret = devm_add_action_or_reset(dev, reset_gpio_of_node_put,
+ priv->rc.of_node);
+ if (ret)
+ return ret;
+
+ /* Cells to match GPIO specifier, but it's not really used */
+ priv->rc.of_reset_n_cells = 2;
+ priv->rc.of_xlate = reset_gpio_of_xlate;
+ priv->rc.nr_resets = 1;
+
+ return devm_reset_controller_register(dev, &priv->rc);
+}
+
+static const struct platform_device_id reset_gpio_ids[] = {
+ { .name = "reset-gpio", },
+ {}
+};
+MODULE_DEVICE_TABLE(platform, reset_gpio_ids);
+
+static struct platform_driver reset_gpio_driver = {
+ .probe = reset_gpio_probe,
+ .id_table = reset_gpio_ids,
+ .driver = {
+ .name = "reset-gpio",
+ },
+};
+module_platform_driver(reset_gpio_driver);
+
+MODULE_AUTHOR("Krzysztof Kozlowski <[email protected]>");
+MODULE_DESCRIPTION("Generic GPIO reset driver");
+MODULE_LICENSE("GPL");
--
2.34.1


2024-01-12 16:37:10

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH v3 2/5] reset: Instantiate reset GPIO controller for shared reset-gpios

Devices sharing a reset GPIO could use the reset framework for
coordinated handling of that shared GPIO line. We have several cases of
such needs, at least for Devicetree-based platforms.

If Devicetree-based device requests a reset line, while "resets"
Devicetree property is missing but there is a "reset-gpios" one,
instantiate a new "reset-gpio" platform device which will handle such
reset line. This allows seamless handling of such shared reset-gpios
without need of changing Devicetree binding [1].

To avoid creating multiple "reset-gpio" platform devices, store the
Devicetree "reset-gpios" GPIO specifiers used for new devices on a
linked list. Later such Devicetree GPIO specifier (phandle to GPIO
controller, GPIO number and GPIO flags) is used to check if reset
controller for given GPIO was already registered.

If two devices have conflicting "reset-gpios" property, e.g. with
different ACTIVE_xxx flags, this would allow to spawn two separate
"reset-gpio" devices, where the second would fail probing on busy GPIO
request.

Link: https://lore.kernel.org/all/[email protected]/ [1]
Cc: Bartosz Golaszewski <[email protected]>
Cc: Chris Packham <[email protected]>
Cc: Sean Anderson <[email protected]>
Signed-off-by: Krzysztof Kozlowski <[email protected]>
---
drivers/reset/core.c | 227 +++++++++++++++++++++++++++++--
include/linux/reset-controller.h | 4 +
2 files changed, 217 insertions(+), 14 deletions(-)

diff --git a/drivers/reset/core.c b/drivers/reset/core.c
index 4d5a78d3c085..86e33a703ad2 100644
--- a/drivers/reset/core.c
+++ b/drivers/reset/core.c
@@ -10,9 +10,13 @@
#include <linux/export.h>
#include <linux/kernel.h>
#include <linux/kref.h>
+#include <linux/gpio/driver.h>
+#include <linux/gpio/machine.h>
+#include <linux/idr.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/acpi.h>
+#include <linux/platform_device.h>
#include <linux/reset.h>
#include <linux/reset-controller.h>
#include <linux/slab.h>
@@ -23,6 +27,11 @@ static LIST_HEAD(reset_controller_list);
static DEFINE_MUTEX(reset_lookup_mutex);
static LIST_HEAD(reset_lookup_list);

+/* Protects reset_gpio_lookup_list */
+static DEFINE_MUTEX(reset_gpio_lookup_mutex);
+static LIST_HEAD(reset_gpio_lookup_list);
+static DEFINE_IDA(reset_gpio_ida);
+
/**
* struct reset_control - a reset control
* @rcdev: a pointer to the reset controller device
@@ -63,6 +72,16 @@ struct reset_control_array {
struct reset_control *rstc[] __counted_by(num_rstcs);
};

+/**
+ * struct reset_gpio_lookup - lookup key for ad-hoc created reset-gpio devices
+ * @of_args: phandle to the reset controller with all the args like GPIO number
+ * @list: list entry for the reset_gpio_lookup_list
+ */
+struct reset_gpio_lookup {
+ struct of_phandle_args of_args;
+ struct list_head list;
+};
+
static const char *rcdev_name(struct reset_controller_dev *rcdev)
{
if (rcdev->dev)
@@ -813,13 +832,183 @@ static void __reset_control_put_internal(struct reset_control *rstc)
kref_put(&rstc->refcnt, __reset_control_release);
}

+static bool __reset_gpios_args_match(const struct of_phandle_args *a1,
+ const struct of_phandle_args *a2)
+{
+ unsigned int i;
+
+ if (!a2)
+ return false;
+
+ if (a1->args_count != a2->args_count)
+ return false;
+
+ for (i = 0; i < a1->args_count; i++)
+ if (a1->args[i] != a2->args[i])
+ return false;
+
+ return true;
+}
+
+static int __reset_add_reset_gpio_lookup(int id, struct device_node *np,
+ unsigned int gpio,
+ unsigned int of_flags)
+{
+ struct gpiod_lookup_table *lookup __free(kfree) = NULL;
+ struct gpio_device *gdev __free(gpio_device_put) = NULL;
+ char *label __free(kfree) = NULL;
+ unsigned int lookup_flags;
+
+ /*
+ * Later we map GPIO flags between OF and Linux, however not all
+ * constants from include/dt-bindings/gpio/gpio.h and
+ * include/linux/gpio/machine.h match each other.
+ */
+ if (of_flags > GPIO_ACTIVE_LOW) {
+ pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n",
+ of_flags, gpio);
+ return -EINVAL;
+ }
+
+ gdev = gpio_device_find_by_fwnode(of_fwnode_handle(np));
+ if (!gdev)
+ return -EPROBE_DEFER;
+
+ label = kstrdup(gpio_device_get_label(gdev), GFP_KERNEL);
+ if (!label)
+ return -EINVAL;
+
+ /* Size: one lookup entry plus sentinel */
+ lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
+ if (!lookup)
+ return -ENOMEM;
+
+ lookup->dev_id = kasprintf(GFP_KERNEL, "reset-gpio.%d", id);
+ if (!lookup->dev_id)
+ return -ENOMEM;
+
+ lookup_flags = GPIO_PERSISTENT;
+ lookup_flags |= of_flags & GPIO_ACTIVE_LOW;
+ lookup->table[0] = GPIO_LOOKUP(no_free_ptr(label), gpio, "reset",
+ lookup_flags);
+
+ gpiod_add_lookup_table(no_free_ptr(lookup));
+
+ return 0;
+}
+
+/*
+ * @reset_args: phandle to the GPIO provider with all the args like GPIO number
+ */
+static int __reset_add_reset_gpio_device(const struct of_phandle_args *args)
+{
+ struct reset_gpio_lookup *rgpio_dev;
+ struct platform_device *pdev;
+ int id, ret;
+
+ /*
+ * Registering reset-gpio device might cause immediate
+ * bind, resulting in its probe() registering new reset controller thus
+ * taking reset_list_mutex lock via reset_controller_register().
+ */
+ lockdep_assert_not_held(&reset_list_mutex);
+
+ mutex_lock(&reset_gpio_lookup_mutex);
+
+ list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) {
+ if (args->np == rgpio_dev->of_args.np) {
+ if (__reset_gpios_args_match(args, &rgpio_dev->of_args))
+ goto out; /* Already on the list, done */
+ }
+ }
+
+ id = ida_alloc(&reset_gpio_ida, GFP_KERNEL);
+ if (id < 0) {
+ ret = id;
+ goto err_unlock;
+ }
+
+ /*
+ * Not freed in normal path, persisent subsystem data (which is assumed
+ * also in the reset-gpio driver).
+ */
+ rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL);
+ if (!rgpio_dev) {
+ ret = -ENOMEM;
+ goto err_ida_free;
+ }
+
+ ret = __reset_add_reset_gpio_lookup(id, args->np, args->args[0],
+ args->args[1]);
+ if (ret < 0)
+ goto err_kfree;
+
+ rgpio_dev->of_args = *args;
+ /*
+ * We keep the device_node reference, but of_args.np is put at the end
+ * of __of_reset_control_get(), so get it one more time.
+ * Hold reference as long as rgpio_dev memory is valid.
+ */
+ of_node_get(rgpio_dev->of_args.np);
+ pdev = platform_device_register_data(NULL, "reset-gpio", id,
+ &rgpio_dev->of_args,
+ sizeof(rgpio_dev->of_args));
+ ret = PTR_ERR_OR_ZERO(pdev);
+ if (ret)
+ goto err_put;
+
+ list_add(&rgpio_dev->list, &reset_gpio_lookup_list);
+
+out:
+ mutex_unlock(&reset_gpio_lookup_mutex);
+
+ return 0;
+
+err_put:
+ of_node_put(rgpio_dev->of_args.np);
+err_kfree:
+ kfree(rgpio_dev);
+err_ida_free:
+ ida_free(&reset_gpio_ida, id);
+err_unlock:
+ mutex_unlock(&reset_gpio_lookup_mutex);
+
+ return ret;
+}
+
+static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args,
+ bool gpio_fallback)
+{
+ struct reset_controller_dev *r, *rcdev;
+
+ lockdep_assert_held(&reset_list_mutex);
+
+ rcdev = NULL;
+ list_for_each_entry(r, &reset_controller_list, list) {
+ if (args->np == r->of_node) {
+ if (gpio_fallback) {
+ if (__reset_gpios_args_match(args, r->of_args)) {
+ rcdev = r;
+ break;
+ }
+ } else {
+ rcdev = r;
+ break;
+ }
+ }
+ }
+
+ return rcdev;
+}
+
struct reset_control *
__of_reset_control_get(struct device_node *node, const char *id, int index,
bool shared, bool optional, bool acquired)
{
+ struct of_phandle_args args = {0};
+ bool gpio_fallback = false;
struct reset_control *rstc;
- struct reset_controller_dev *r, *rcdev;
- struct of_phandle_args args;
+ struct reset_controller_dev *rcdev;
int rstc_id;
int ret;

@@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index,
index, &args);
if (ret == -EINVAL)
return ERR_PTR(ret);
- if (ret)
- return optional ? NULL : ERR_PTR(ret);
+ if (ret) {
+ /*
+ * There can be only one reset-gpio for regular devices, so
+ * don't bother with GPIO index.
+ */
+ ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells",
+ 0, &args);
+ if (ret)
+ return optional ? NULL : ERR_PTR(ret);

- mutex_lock(&reset_list_mutex);
- rcdev = NULL;
- list_for_each_entry(r, &reset_controller_list, list) {
- if (args.np == r->of_node) {
- rcdev = r;
- break;
+ gpio_fallback = true;
+
+ ret = __reset_add_reset_gpio_device(&args);
+ if (ret) {
+ rstc = ERR_PTR(ret);
+ goto out_put;
}
}

+ mutex_lock(&reset_list_mutex);
+ rcdev = __reset_find_rcdev(&args, gpio_fallback);
if (!rcdev) {
rstc = ERR_PTR(-EPROBE_DEFER);
- goto out;
+ goto out_unlock;
}

if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
rstc = ERR_PTR(-EINVAL);
- goto out;
+ goto out_unlock;
}

rstc_id = rcdev->of_xlate(rcdev, &args);
if (rstc_id < 0) {
rstc = ERR_PTR(rstc_id);
- goto out;
+ goto out_unlock;
}

/* reset_list_mutex also protects the rcdev's reset_control list */
rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired);

-out:
+out_unlock:
mutex_unlock(&reset_list_mutex);
+out_put:
of_node_put(args.np);

return rstc;
diff --git a/include/linux/reset-controller.h b/include/linux/reset-controller.h
index 0fa4f60e1186..e064473215de 100644
--- a/include/linux/reset-controller.h
+++ b/include/linux/reset-controller.h
@@ -61,6 +61,9 @@ struct reset_control_lookup {
* @dev: corresponding driver model device struct
* @of_node: corresponding device tree node as phandle target
* @of_reset_n_cells: number of cells in reset line specifiers
+ * TODO: of_args have of_node, so we have here duplication
+ * @of_args: for reset-gpios controllers: corresponding phandle args with GPIO
+ * number complementing of_node
* @of_xlate: translation function to translate from specifier as found in the
* device tree to id as given to the reset control ops, defaults
* to :c:func:`of_reset_simple_xlate`.
@@ -74,6 +77,7 @@ struct reset_controller_dev {
struct device *dev;
struct device_node *of_node;
int of_reset_n_cells;
+ const struct of_phandle_args *of_args;
int (*of_xlate)(struct reset_controller_dev *rcdev,
const struct of_phandle_args *reset_spec);
unsigned int nr_resets;
--
2.34.1


2024-01-12 16:37:20

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH v3 3/5] ASoC: dt-bindings: qcom,wsa8840: Add reset-gpios for shared line

On newer Qualcomm platforms, like X1E80100-CRD, the WSA884x speakers
share SD_N GPIOs between two speakers, thus a coordinated assertion is
needed. Linux supports handling shared GPIO lines through "reset-gpios"
property, thus allow specifying either powerdown or reset GPIOs (these
are the same).

Cc: Bartosz Golaszewski <[email protected]>
Cc: Sean Anderson <[email protected]>
Signed-off-by: Krzysztof Kozlowski <[email protected]>

---

If previous patches are fine, then this commit is independent and could
be taken via ASoC.
---
.../devicetree/bindings/sound/qcom,wsa8840.yaml | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/sound/qcom,wsa8840.yaml b/Documentation/devicetree/bindings/sound/qcom,wsa8840.yaml
index d717017b0fdb..22798d22d981 100644
--- a/Documentation/devicetree/bindings/sound/qcom,wsa8840.yaml
+++ b/Documentation/devicetree/bindings/sound/qcom,wsa8840.yaml
@@ -28,6 +28,10 @@ properties:
description: Powerdown/Shutdown line to use (pin SD_N)
maxItems: 1

+ reset-gpios:
+ description: Powerdown/Shutdown line to use (pin SD_N)
+ maxItems: 1
+
'#sound-dai-cells':
const: 0

@@ -37,11 +41,16 @@ properties:
required:
- compatible
- reg
- - powerdown-gpios
- '#sound-dai-cells'
- vdd-1p8-supply
- vdd-io-supply

+oneOf:
+ - required:
+ - powerdown-gpios
+ - required:
+ - reset-gpios
+
unevaluatedProperties: false

examples:
--
2.34.1


2024-01-12 16:37:56

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH v3 4/5] ASoC: codecs: wsa884x: Allow sharing reset GPIO

On some boards with multiple WSA8840/WSA8845 speakers, the reset
(shutdown) GPIO is shared between two speakers. Use the reset
controller framework and its "reset-gpio" driver to handle this case.
This allows bring-up and proper handling of all WSA884x speakers on
X1E80100-CRD board.

Cc: Bartosz Golaszewski <[email protected]>
Cc: Sean Anderson <[email protected]>
Signed-off-by: Krzysztof Kozlowski <[email protected]>

---

If previous patches are fine, then this commit is independent and could
be taken via ASoC.
---
sound/soc/codecs/wsa884x.c | 53 +++++++++++++++++++++++++++++++-------
1 file changed, 43 insertions(+), 10 deletions(-)

diff --git a/sound/soc/codecs/wsa884x.c b/sound/soc/codecs/wsa884x.c
index f2653df84e4a..a9767ef0e39d 100644
--- a/sound/soc/codecs/wsa884x.c
+++ b/sound/soc/codecs/wsa884x.c
@@ -13,6 +13,7 @@
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
+#include <linux/reset.h>
#include <linux/slab.h>
#include <linux/soundwire/sdw.h>
#include <linux/soundwire/sdw_registers.h>
@@ -699,6 +700,7 @@ struct wsa884x_priv {
struct sdw_stream_runtime *sruntime;
struct sdw_port_config port_config[WSA884X_MAX_SWR_PORTS];
struct gpio_desc *sd_n;
+ struct reset_control *sd_reset;
bool port_prepared[WSA884X_MAX_SWR_PORTS];
bool port_enable[WSA884X_MAX_SWR_PORTS];
unsigned int variant;
@@ -1799,9 +1801,22 @@ static struct snd_soc_dai_driver wsa884x_dais[] = {
},
};

-static void wsa884x_gpio_powerdown(void *data)
+static void wsa884x_reset_powerdown(void *data)
{
- gpiod_direction_output(data, 1);
+ struct wsa884x_priv *wsa884x = data;
+
+ if (wsa884x->sd_reset)
+ reset_control_assert(wsa884x->sd_reset);
+ else
+ gpiod_direction_output(wsa884x->sd_n, 1);
+}
+
+static void wsa884x_reset_deassert(struct wsa884x_priv *wsa884x)
+{
+ if (wsa884x->sd_reset)
+ reset_control_deassert(wsa884x->sd_reset);
+ else
+ gpiod_direction_output(wsa884x->sd_n, 0);
}

static void wsa884x_regulator_disable(void *data)
@@ -1809,6 +1824,27 @@ static void wsa884x_regulator_disable(void *data)
regulator_bulk_disable(WSA884X_SUPPLIES_NUM, data);
}

+static int wsa884x_get_reset(struct device *dev, struct wsa884x_priv *wsa884x)
+{
+ wsa884x->sd_reset = devm_reset_control_get_optional_shared(dev, NULL);
+ if (IS_ERR(wsa884x->sd_reset))
+ return dev_err_probe(dev, PTR_ERR(wsa884x->sd_reset),
+ "Failed to get reset\n");
+ else if (wsa884x->sd_reset)
+ return 0;
+ /*
+ * else: NULL, so use the backwards compatible way for powerdown-gpios,
+ * which does not handle sharing GPIO properly.
+ */
+ wsa884x->sd_n = devm_gpiod_get_optional(dev, "powerdown",
+ GPIOD_OUT_HIGH);
+ if (IS_ERR(wsa884x->sd_n))
+ return dev_err_probe(dev, PTR_ERR(wsa884x->sd_n),
+ "Shutdown Control GPIO not found\n");
+
+ return 0;
+}
+
static int wsa884x_probe(struct sdw_slave *pdev,
const struct sdw_device_id *id)
{
@@ -1838,11 +1874,9 @@ static int wsa884x_probe(struct sdw_slave *pdev,
if (ret)
return ret;

- wsa884x->sd_n = devm_gpiod_get_optional(dev, "powerdown",
- GPIOD_OUT_HIGH);
- if (IS_ERR(wsa884x->sd_n))
- return dev_err_probe(dev, PTR_ERR(wsa884x->sd_n),
- "Shutdown Control GPIO not found\n");
+ ret = wsa884x_get_reset(dev, wsa884x);
+ if (ret)
+ return ret;

dev_set_drvdata(dev, wsa884x);
wsa884x->slave = pdev;
@@ -1858,9 +1892,8 @@ static int wsa884x_probe(struct sdw_slave *pdev,
pdev->prop.sink_dpn_prop = wsa884x_sink_dpn_prop;
pdev->prop.scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;

- /* Bring out of reset */
- gpiod_direction_output(wsa884x->sd_n, 0);
- ret = devm_add_action_or_reset(dev, wsa884x_gpio_powerdown, wsa884x->sd_n);
+ wsa884x_reset_deassert(wsa884x);
+ ret = devm_add_action_or_reset(dev, wsa884x_reset_powerdown, wsa884x);
if (ret)
return ret;

--
2.34.1


2024-01-12 16:38:08

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH v3 5/5] i2c: muxes: pca954x: Allow sharing reset GPIO

From: Chris Packham <[email protected]>

Some hardware designs with multiple PCA954x devices use a reset GPIO
connected to all the muxes. Support this configuration by making use of
the reset controller framework which can deal with the shared reset
GPIOs. Fall back to the old GPIO descriptor method if the reset
controller framework is not enabled.

Signed-off-by: Chris Packham <[email protected]>
Acked-by: Peter Rosin <[email protected]>
Reviewed-by: Krzysztof Kozlowski <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Krzysztof Kozlowski <[email protected]>

---

If previous patches are fine, then this commit is independent and could
be taken via I2C.

Cc: Chris Packham <[email protected]>
Cc: Bartosz Golaszewski <[email protected]>
Cc: Sean Anderson <[email protected]>
---
drivers/i2c/muxes/i2c-mux-pca954x.c | 46 ++++++++++++++++++++++++-----
1 file changed, 38 insertions(+), 8 deletions(-)

diff --git a/drivers/i2c/muxes/i2c-mux-pca954x.c b/drivers/i2c/muxes/i2c-mux-pca954x.c
index 2219062104fb..1702e8d49b91 100644
--- a/drivers/i2c/muxes/i2c-mux-pca954x.c
+++ b/drivers/i2c/muxes/i2c-mux-pca954x.c
@@ -49,6 +49,7 @@
#include <linux/pm.h>
#include <linux/property.h>
#include <linux/regulator/consumer.h>
+#include <linux/reset.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <dt-bindings/mux/mux.h>
@@ -102,6 +103,9 @@ struct pca954x {
unsigned int irq_mask;
raw_spinlock_t lock;
struct regulator *supply;
+
+ struct gpio_desc *reset_gpio;
+ struct reset_control *reset_cont;
};

/* Provide specs for the MAX735x, PCA954x and PCA984x types we know about */
@@ -477,6 +481,35 @@ static int pca954x_init(struct i2c_client *client, struct pca954x *data)
return ret;
}

+static int pca954x_get_reset(struct device *dev, struct pca954x *data)
+{
+ data->reset_cont = devm_reset_control_get_optional_shared(dev, NULL);
+ if (IS_ERR(data->reset_cont))
+ return dev_err_probe(dev, PTR_ERR(data->reset_cont),
+ "Failed to get reset\n");
+ else if (data->reset_cont)
+ return 0;
+
+ /*
+ * fallback to legacy reset-gpios
+ */
+ data->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
+ if (IS_ERR(data->reset_gpio)) {
+ return dev_err_probe(dev, PTR_ERR(data->reset_gpio),
+ "Failed to get reset gpio");
+ }
+
+ return 0;
+}
+
+static void pca954x_reset_deassert(struct pca954x *data)
+{
+ if (data->reset_cont)
+ reset_control_deassert(data->reset_cont);
+ else
+ gpiod_set_value_cansleep(data->reset_gpio, 0);
+}
+
/*
* I2C init/probing/exit functions
*/
@@ -485,7 +518,6 @@ static int pca954x_probe(struct i2c_client *client)
const struct i2c_device_id *id = i2c_client_get_device_id(client);
struct i2c_adapter *adap = client->adapter;
struct device *dev = &client->dev;
- struct gpio_desc *gpio;
struct i2c_mux_core *muxc;
struct pca954x *data;
int num;
@@ -513,15 +545,13 @@ static int pca954x_probe(struct i2c_client *client)
return dev_err_probe(dev, ret,
"Failed to enable vdd supply\n");

- /* Reset the mux if a reset GPIO is specified. */
- gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
- if (IS_ERR(gpio)) {
- ret = PTR_ERR(gpio);
+ ret = pca954x_get_reset(dev, data);
+ if (ret)
goto fail_cleanup;
- }
- if (gpio) {
+
+ if (data->reset_cont || data->reset_gpio) {
udelay(1);
- gpiod_set_value_cansleep(gpio, 0);
+ pca954x_reset_deassert(data);
/* Give the chip some time to recover. */
udelay(1);
}
--
2.34.1


2024-01-15 14:50:56

by Bartosz Golaszewski

[permalink] [raw]
Subject: Re: [PATCH v3 1/5] reset: gpio: Add GPIO-based reset controller

On Fri, Jan 12, 2024 at 5:36 PM Krzysztof Kozlowski
<[email protected]> wrote:
>
> Add a simple driver to control GPIO-based resets using the reset
> controller API for the cases when the GPIOs are shared and reset should
> be coordinated. The driver is expected to be used by reset core
> framework for ad-hoc reset controllers.
>
> Cc: Bartosz Golaszewski <[email protected]>
> Cc: Chris Packham <[email protected]>
> Cc: Sean Anderson <[email protected]>
> Signed-off-by: Krzysztof Kozlowski <[email protected]>
> ---
> MAINTAINERS | 5 ++
> drivers/reset/Kconfig | 9 +++
> drivers/reset/Makefile | 1 +
> drivers/reset/reset-gpio.c | 121 +++++++++++++++++++++++++++++++++++++
> 4 files changed, 136 insertions(+)
> create mode 100644 drivers/reset/reset-gpio.c
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 7fe27cd60e1b..a0fbd4814bc7 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -8866,6 +8866,11 @@ F: Documentation/i2c/muxes/i2c-mux-gpio.rst
> F: drivers/i2c/muxes/i2c-mux-gpio.c
> F: include/linux/platform_data/i2c-mux-gpio.h
>
> +GENERIC GPIO RESET DRIVER
> +M: Krzysztof Kozlowski <[email protected]>
> +S: Maintained
> +F: drivers/reset/reset-gpio.c
> +
> GENERIC HDLC (WAN) DRIVERS
> M: Krzysztof Halasa <[email protected]>
> S: Maintained
> diff --git a/drivers/reset/Kconfig b/drivers/reset/Kconfig
> index ccd59ddd7610..bb1b5a326eb7 100644
> --- a/drivers/reset/Kconfig
> +++ b/drivers/reset/Kconfig
> @@ -66,6 +66,15 @@ config RESET_BRCMSTB_RESCAL
> This enables the RESCAL reset controller for SATA, PCIe0, or PCIe1 on
> BCM7216.
>
> +config RESET_GPIO
> + tristate "GPIO reset controller"
> + help
> + This enables a generic reset controller for resets attached via
> + GPIOs. Typically for OF platforms this driver expects "reset-gpios"
> + property.
> +
> + If compiled as module, it will be called reset-gpio.
> +
> config RESET_HSDK
> bool "Synopsys HSDK Reset Driver"
> depends on HAS_IOMEM
> diff --git a/drivers/reset/Makefile b/drivers/reset/Makefile
> index 8270da8a4baa..fd8b49fa46fc 100644
> --- a/drivers/reset/Makefile
> +++ b/drivers/reset/Makefile
> @@ -11,6 +11,7 @@ obj-$(CONFIG_RESET_BCM6345) += reset-bcm6345.o
> obj-$(CONFIG_RESET_BERLIN) += reset-berlin.o
> obj-$(CONFIG_RESET_BRCMSTB) += reset-brcmstb.o
> obj-$(CONFIG_RESET_BRCMSTB_RESCAL) += reset-brcmstb-rescal.o
> +obj-$(CONFIG_RESET_GPIO) += reset-gpio.o
> obj-$(CONFIG_RESET_HSDK) += reset-hsdk.o
> obj-$(CONFIG_RESET_IMX7) += reset-imx7.o
> obj-$(CONFIG_RESET_INTEL_GW) += reset-intel-gw.o
> diff --git a/drivers/reset/reset-gpio.c b/drivers/reset/reset-gpio.c
> new file mode 100644
> index 000000000000..0fe482740f1b
> --- /dev/null
> +++ b/drivers/reset/reset-gpio.c
> @@ -0,0 +1,121 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <linux/gpio/consumer.h>
> +#include <linux/mod_devicetable.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +#include <linux/reset-controller.h>
> +
> +struct reset_gpio_priv {
> + struct reset_controller_dev rc;
> + struct gpio_desc *reset;
> +};
> +
> +static inline struct reset_gpio_priv
> +*rc_to_reset_gpio(struct reset_controller_dev *rc)
> +{
> + return container_of(rc, struct reset_gpio_priv, rc);
> +}
> +
> +static int reset_gpio_assert(struct reset_controller_dev *rc, unsigned long id)
> +{
> + struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
> +
> + gpiod_set_value_cansleep(priv->reset, 1);
> +
> + return 0;
> +}
> +
> +static int reset_gpio_deassert(struct reset_controller_dev *rc,
> + unsigned long id)
> +{
> + struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
> +
> + gpiod_set_value_cansleep(priv->reset, 0);
> +
> + return 0;
> +}
> +
> +static int reset_gpio_status(struct reset_controller_dev *rc, unsigned long id)
> +{
> + struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
> +
> + return gpiod_get_value_cansleep(priv->reset);
> +}
> +
> +static const struct reset_control_ops reset_gpio_ops = {
> + .assert = reset_gpio_assert,
> + .deassert = reset_gpio_deassert,
> + .status = reset_gpio_status,
> +};
> +
> +static int reset_gpio_of_xlate(struct reset_controller_dev *rcdev,
> + const struct of_phandle_args *reset_spec)
> +{
> + return reset_spec->args[0];
> +}
> +
> +static void reset_gpio_of_node_put(void *data)
> +{
> + of_node_put(data);
> +}
> +
> +static int reset_gpio_probe(struct platform_device *pdev)
> +{
> + struct device *dev = &pdev->dev;
> + struct of_phandle_args *platdata = dev_get_platdata(dev);
> + struct reset_gpio_priv *priv;
> + int ret;
> +
> + if (!platdata)
> + return -EINVAL;
> +
> + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> + if (!priv)
> + return -ENOMEM;
> +
> + platform_set_drvdata(pdev, &priv->rc);
> +
> + /* Relies on GPIO_LOOKUP */

That doesn't mean anything. The GPIO abstraction is there to hide
*how* the GPIO is assigned to the device. This relies on regular GPIO
infrastructure common to all drivers. If at some point this gets an OF
compatible or is instantiated from ACPI or otherwise, the same code
will still work and not rely on "GPIO_LOOKUP").

> + priv->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
> + if (IS_ERR(priv->reset))
> + return dev_err_probe(dev, PTR_ERR(priv->reset),
> + "Could not get reset gpios\n");
> +
> + priv->rc.ops = &reset_gpio_ops;
> + priv->rc.owner = THIS_MODULE;
> + priv->rc.dev = dev;
> + priv->rc.of_args = platdata;
> + priv->rc.of_node = of_node_get(platdata->np);
> + ret = devm_add_action_or_reset(dev, reset_gpio_of_node_put,
> + priv->rc.of_node);
> + if (ret)
> + return ret;
> +
> + /* Cells to match GPIO specifier, but it's not really used */
> + priv->rc.of_reset_n_cells = 2;
> + priv->rc.of_xlate = reset_gpio_of_xlate;
> + priv->rc.nr_resets = 1;
> +
> + return devm_reset_controller_register(dev, &priv->rc);
> +}
> +
> +static const struct platform_device_id reset_gpio_ids[] = {
> + { .name = "reset-gpio", },
> + {}
> +};
> +MODULE_DEVICE_TABLE(platform, reset_gpio_ids);
> +
> +static struct platform_driver reset_gpio_driver = {
> + .probe = reset_gpio_probe,
> + .id_table = reset_gpio_ids,
> + .driver = {
> + .name = "reset-gpio",
> + },
> +};
> +module_platform_driver(reset_gpio_driver);
> +
> +MODULE_AUTHOR("Krzysztof Kozlowski <[email protected]>");
> +MODULE_DESCRIPTION("Generic GPIO reset driver");
> +MODULE_LICENSE("GPL");
> --
> 2.34.1
>

With the above dropped:

Reviewed-by: Bartosz Golaszewski <[email protected]>

2024-01-15 16:07:55

by Bartosz Golaszewski

[permalink] [raw]
Subject: Re: [PATCH v3 2/5] reset: Instantiate reset GPIO controller for shared reset-gpios

On Fri, Jan 12, 2024 at 5:36 PM Krzysztof Kozlowski
<[email protected]> wrote:
>
> Devices sharing a reset GPIO could use the reset framework for
> coordinated handling of that shared GPIO line. We have several cases of
> such needs, at least for Devicetree-based platforms.
>
> If Devicetree-based device requests a reset line, while "resets"
> Devicetree property is missing but there is a "reset-gpios" one,
> instantiate a new "reset-gpio" platform device which will handle such
> reset line. This allows seamless handling of such shared reset-gpios
> without need of changing Devicetree binding [1].
>
> To avoid creating multiple "reset-gpio" platform devices, store the
> Devicetree "reset-gpios" GPIO specifiers used for new devices on a
> linked list. Later such Devicetree GPIO specifier (phandle to GPIO
> controller, GPIO number and GPIO flags) is used to check if reset
> controller for given GPIO was already registered.
>
> If two devices have conflicting "reset-gpios" property, e.g. with
> different ACTIVE_xxx flags, this would allow to spawn two separate
> "reset-gpio" devices, where the second would fail probing on busy GPIO
> request.
>
> Link: https://lore.kernel.org/all/[email protected]/ [1]
> Cc: Bartosz Golaszewski <[email protected]>
> Cc: Chris Packham <[email protected]>
> Cc: Sean Anderson <[email protected]>
> Signed-off-by: Krzysztof Kozlowski <[email protected]>
> ---
> drivers/reset/core.c | 227 +++++++++++++++++++++++++++++--
> include/linux/reset-controller.h | 4 +
> 2 files changed, 217 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/reset/core.c b/drivers/reset/core.c
> index 4d5a78d3c085..86e33a703ad2 100644
> --- a/drivers/reset/core.c
> +++ b/drivers/reset/core.c
> @@ -10,9 +10,13 @@
> #include <linux/export.h>
> #include <linux/kernel.h>
> #include <linux/kref.h>
> +#include <linux/gpio/driver.h>
> +#include <linux/gpio/machine.h>
> +#include <linux/idr.h>
> #include <linux/module.h>
> #include <linux/of.h>
> #include <linux/acpi.h>
> +#include <linux/platform_device.h>
> #include <linux/reset.h>
> #include <linux/reset-controller.h>
> #include <linux/slab.h>
> @@ -23,6 +27,11 @@ static LIST_HEAD(reset_controller_list);
> static DEFINE_MUTEX(reset_lookup_mutex);
> static LIST_HEAD(reset_lookup_list);
>
> +/* Protects reset_gpio_lookup_list */
> +static DEFINE_MUTEX(reset_gpio_lookup_mutex);
> +static LIST_HEAD(reset_gpio_lookup_list);
> +static DEFINE_IDA(reset_gpio_ida);
> +
> /**
> * struct reset_control - a reset control
> * @rcdev: a pointer to the reset controller device
> @@ -63,6 +72,16 @@ struct reset_control_array {
> struct reset_control *rstc[] __counted_by(num_rstcs);
> };
>
> +/**
> + * struct reset_gpio_lookup - lookup key for ad-hoc created reset-gpio devices
> + * @of_args: phandle to the reset controller with all the args like GPIO number
> + * @list: list entry for the reset_gpio_lookup_list
> + */
> +struct reset_gpio_lookup {
> + struct of_phandle_args of_args;
> + struct list_head list;
> +};
> +
> static const char *rcdev_name(struct reset_controller_dev *rcdev)
> {
> if (rcdev->dev)
> @@ -813,13 +832,183 @@ static void __reset_control_put_internal(struct reset_control *rstc)
> kref_put(&rstc->refcnt, __reset_control_release);
> }
>
> +static bool __reset_gpios_args_match(const struct of_phandle_args *a1,
> + const struct of_phandle_args *a2)
> +{
> + unsigned int i;
> +
> + if (!a2)
> + return false;
> +
> + if (a1->args_count != a2->args_count)
> + return false;
> +
> + for (i = 0; i < a1->args_count; i++)
> + if (a1->args[i] != a2->args[i])
> + return false;
> +
> + return true;
> +}
> +
> +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np,
> + unsigned int gpio,
> + unsigned int of_flags)
> +{
> + struct gpiod_lookup_table *lookup __free(kfree) = NULL;
> + struct gpio_device *gdev __free(gpio_device_put) = NULL;
> + char *label __free(kfree) = NULL;

I got yelled at by Linus Torvalds personally for doing it like this. I
know this is a common pattern in code using GLib but Linus wants auto
variables to be initialized where they're declared...

> + unsigned int lookup_flags;
> +
> + /*
> + * Later we map GPIO flags between OF and Linux, however not all
> + * constants from include/dt-bindings/gpio/gpio.h and
> + * include/linux/gpio/machine.h match each other.
> + */
> + if (of_flags > GPIO_ACTIVE_LOW) {
> + pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n",
> + of_flags, gpio);
> + return -EINVAL;
> + }
> +
> + gdev = gpio_device_find_by_fwnode(of_fwnode_handle(np));

.. so this should become:

struct gpio_device *gdev __free(gpio_device_put) = gpio_device_find(...)

and same for the rest.

Don't get me wrong, I love cleanup.h but there's a (unofficial for
now) coding style.

> + if (!gdev)
> + return -EPROBE_DEFER;
> +
> + label = kstrdup(gpio_device_get_label(gdev), GFP_KERNEL);
> + if (!label)
> + return -EINVAL;
> +
> + /* Size: one lookup entry plus sentinel */
> + lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
> + if (!lookup)
> + return -ENOMEM;
> +
> + lookup->dev_id = kasprintf(GFP_KERNEL, "reset-gpio.%d", id);
> + if (!lookup->dev_id)
> + return -ENOMEM;
> +
> + lookup_flags = GPIO_PERSISTENT;
> + lookup_flags |= of_flags & GPIO_ACTIVE_LOW;
> + lookup->table[0] = GPIO_LOOKUP(no_free_ptr(label), gpio, "reset",
> + lookup_flags);
> +
> + gpiod_add_lookup_table(no_free_ptr(lookup));

You told me that this doesn't need to be removed or ever freed but a
comment on that would be in order.

> +
> + return 0;
> +}
> +
> +/*
> + * @reset_args: phandle to the GPIO provider with all the args like GPIO number
> + */
> +static int __reset_add_reset_gpio_device(const struct of_phandle_args *args)
> +{
> + struct reset_gpio_lookup *rgpio_dev;
> + struct platform_device *pdev;
> + int id, ret;
> +
> + /*
> + * Registering reset-gpio device might cause immediate
> + * bind, resulting in its probe() registering new reset controller thus
> + * taking reset_list_mutex lock via reset_controller_register().
> + */
> + lockdep_assert_not_held(&reset_list_mutex);

So how does dumping the stack help here exactly?

> +
> + mutex_lock(&reset_gpio_lookup_mutex);
> +
> + list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) {
> + if (args->np == rgpio_dev->of_args.np) {
> + if (__reset_gpios_args_match(args, &rgpio_dev->of_args))
> + goto out; /* Already on the list, done */
> + }
> + }
> +
> + id = ida_alloc(&reset_gpio_ida, GFP_KERNEL);
> + if (id < 0) {
> + ret = id;
> + goto err_unlock;
> + }
> +
> + /*
> + * Not freed in normal path, persisent subsystem data (which is assumed
> + * also in the reset-gpio driver).
> + */
> + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL);
> + if (!rgpio_dev) {
> + ret = -ENOMEM;
> + goto err_ida_free;
> + }
> +
> + ret = __reset_add_reset_gpio_lookup(id, args->np, args->args[0],
> + args->args[1]);
> + if (ret < 0)
> + goto err_kfree;
> +
> + rgpio_dev->of_args = *args;
> + /*
> + * We keep the device_node reference, but of_args.np is put at the end
> + * of __of_reset_control_get(), so get it one more time.
> + * Hold reference as long as rgpio_dev memory is valid.
> + */
> + of_node_get(rgpio_dev->of_args.np);
> + pdev = platform_device_register_data(NULL, "reset-gpio", id,
> + &rgpio_dev->of_args,
> + sizeof(rgpio_dev->of_args));
> + ret = PTR_ERR_OR_ZERO(pdev);
> + if (ret)
> + goto err_put;
> +
> + list_add(&rgpio_dev->list, &reset_gpio_lookup_list);
> +
> +out:
> + mutex_unlock(&reset_gpio_lookup_mutex);
> +
> + return 0;
> +
> +err_put:
> + of_node_put(rgpio_dev->of_args.np);
> +err_kfree:
> + kfree(rgpio_dev);
> +err_ida_free:
> + ida_free(&reset_gpio_ida, id);
> +err_unlock:
> + mutex_unlock(&reset_gpio_lookup_mutex);
> +

You're already using cleanup helpers above, why not here too? Would
make this function much more readable and allow you to drop all but
the ida_free() here. Possibly you'd need to define the __free()
callback for of_node_put() though.

> + return ret;
> +}
> +
> +static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args,
> + bool gpio_fallback)
> +{
> + struct reset_controller_dev *r, *rcdev;
> +
> + lockdep_assert_held(&reset_list_mutex);
> +
> + rcdev = NULL;
> + list_for_each_entry(r, &reset_controller_list, list) {
> + if (args->np == r->of_node) {
> + if (gpio_fallback) {
> + if (__reset_gpios_args_match(args, r->of_args)) {
> + rcdev = r;
> + break;
> + }
> + } else {
> + rcdev = r;
> + break;
> + }
> + }
> + }
> +
> + return rcdev;
> +}
> +
> struct reset_control *
> __of_reset_control_get(struct device_node *node, const char *id, int index,
> bool shared, bool optional, bool acquired)
> {
> + struct of_phandle_args args = {0};
> + bool gpio_fallback = false;
> struct reset_control *rstc;
> - struct reset_controller_dev *r, *rcdev;
> - struct of_phandle_args args;
> + struct reset_controller_dev *rcdev;
> int rstc_id;
> int ret;
>
> @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index,
> index, &args);
> if (ret == -EINVAL)
> return ERR_PTR(ret);
> - if (ret)
> - return optional ? NULL : ERR_PTR(ret);
> + if (ret) {
> + /*
> + * There can be only one reset-gpio for regular devices, so
> + * don't bother with GPIO index.
> + */
> + ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells",
> + 0, &args);
> + if (ret)
> + return optional ? NULL : ERR_PTR(ret);
>
> - mutex_lock(&reset_list_mutex);
> - rcdev = NULL;
> - list_for_each_entry(r, &reset_controller_list, list) {
> - if (args.np == r->of_node) {
> - rcdev = r;
> - break;
> + gpio_fallback = true;
> +
> + ret = __reset_add_reset_gpio_device(&args);
> + if (ret) {
> + rstc = ERR_PTR(ret);
> + goto out_put;
> }
> }
>
> + mutex_lock(&reset_list_mutex);
> + rcdev = __reset_find_rcdev(&args, gpio_fallback);
> if (!rcdev) {
> rstc = ERR_PTR(-EPROBE_DEFER);
> - goto out;
> + goto out_unlock;
> }
>
> if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
> rstc = ERR_PTR(-EINVAL);
> - goto out;
> + goto out_unlock;
> }
>
> rstc_id = rcdev->of_xlate(rcdev, &args);
> if (rstc_id < 0) {
> rstc = ERR_PTR(rstc_id);
> - goto out;
> + goto out_unlock;
> }
>
> /* reset_list_mutex also protects the rcdev's reset_control list */
> rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired);
>
> -out:
> +out_unlock:
> mutex_unlock(&reset_list_mutex);
> +out_put:
> of_node_put(args.np);

I suggest reworking this to use cleanup.h as well.

Bartosz

>
> return rstc;
> diff --git a/include/linux/reset-controller.h b/include/linux/reset-controller.h
> index 0fa4f60e1186..e064473215de 100644
> --- a/include/linux/reset-controller.h
> +++ b/include/linux/reset-controller.h
> @@ -61,6 +61,9 @@ struct reset_control_lookup {
> * @dev: corresponding driver model device struct
> * @of_node: corresponding device tree node as phandle target
> * @of_reset_n_cells: number of cells in reset line specifiers
> + * TODO: of_args have of_node, so we have here duplication
> + * @of_args: for reset-gpios controllers: corresponding phandle args with GPIO
> + * number complementing of_node
> * @of_xlate: translation function to translate from specifier as found in the
> * device tree to id as given to the reset control ops, defaults
> * to :c:func:`of_reset_simple_xlate`.
> @@ -74,6 +77,7 @@ struct reset_controller_dev {
> struct device *dev;
> struct device_node *of_node;
> int of_reset_n_cells;
> + const struct of_phandle_args *of_args;
> int (*of_xlate)(struct reset_controller_dev *rcdev,
> const struct of_phandle_args *reset_spec);
> unsigned int nr_resets;
> --
> 2.34.1
>

2024-01-15 16:13:39

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v3 2/5] reset: Instantiate reset GPIO controller for shared reset-gpios

On 15/01/2024 17:06, Bartosz Golaszewski wrote:
>> +
>> +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np,
>> + unsigned int gpio,
>> + unsigned int of_flags)
>> +{
>> + struct gpiod_lookup_table *lookup __free(kfree) = NULL;
>> + struct gpio_device *gdev __free(gpio_device_put) = NULL;
>> + char *label __free(kfree) = NULL;
>
> I got yelled at by Linus Torvalds personally for doing it like this. I
> know this is a common pattern in code using GLib but Linus wants auto
> variables to be initialized where they're declared...

Declaration is here. Initialization is here. Therefore this is
initialized where it is declared. What's more it is initialized to a
valid value, because __free() accepts NULLs.

>
>> + unsigned int lookup_flags;
>> +
>> + /*
>> + * Later we map GPIO flags between OF and Linux, however not all
>> + * constants from include/dt-bindings/gpio/gpio.h and
>> + * include/linux/gpio/machine.h match each other.
>> + */
>> + if (of_flags > GPIO_ACTIVE_LOW) {
>> + pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n",
>> + of_flags, gpio);
>> + return -EINVAL;
>> + }
>> +
>> + gdev = gpio_device_find_by_fwnode(of_fwnode_handle(np));
>
> ... so this should become:
>
> struct gpio_device *gdev __free(gpio_device_put) = gpio_device_find(...)
>
> and same for the rest.
>
> Don't get me wrong, I love cleanup.h but there's a (unofficial for
> now) coding style.

So you just want to declare it not in top-part of the function but just
before first use?

>
>> + if (!gdev)
>> + return -EPROBE_DEFER;
>> +
>> + label = kstrdup(gpio_device_get_label(gdev), GFP_KERNEL);
>> + if (!label)
>> + return -EINVAL;
>> +
>> + /* Size: one lookup entry plus sentinel */
>> + lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
>> + if (!lookup)
>> + return -ENOMEM;
>> +
>> + lookup->dev_id = kasprintf(GFP_KERNEL, "reset-gpio.%d", id);
>> + if (!lookup->dev_id)
>> + return -ENOMEM;
>> +
>> + lookup_flags = GPIO_PERSISTENT;
>> + lookup_flags |= of_flags & GPIO_ACTIVE_LOW;
>> + lookup->table[0] = GPIO_LOOKUP(no_free_ptr(label), gpio, "reset",
>> + lookup_flags);
>> +
>> + gpiod_add_lookup_table(no_free_ptr(lookup));
>
> You told me that this doesn't need to be removed or ever freed but a
> comment on that would be in order.

Sure, code further comments on this but I can make it explicit here as well.

>
>> +
>> + return 0;
>> +}
>> +
>> +/*
>> + * @reset_args: phandle to the GPIO provider with all the args like GPIO number
>> + */
>> +static int __reset_add_reset_gpio_device(const struct of_phandle_args *args)
>> +{
>> + struct reset_gpio_lookup *rgpio_dev;
>> + struct platform_device *pdev;
>> + int id, ret;
>> +
>> + /*
>> + * Registering reset-gpio device might cause immediate
>> + * bind, resulting in its probe() registering new reset controller thus
>> + * taking reset_list_mutex lock via reset_controller_register().
>> + */
>> + lockdep_assert_not_held(&reset_list_mutex);
>
> So how does dumping the stack help here exactly?

This is self-documenting code. dumping stack does not matter, the point
is that future developers should see this lockdep before they start
playing with new locks.

>
>> +
>> + mutex_lock(&reset_gpio_lookup_mutex);
>> +
>> + list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) {
>> + if (args->np == rgpio_dev->of_args.np) {
>> + if (__reset_gpios_args_match(args, &rgpio_dev->of_args))
>> + goto out; /* Already on the list, done */
>> + }
>> + }
>> +
>> + id = ida_alloc(&reset_gpio_ida, GFP_KERNEL);
>> + if (id < 0) {
>> + ret = id;
>> + goto err_unlock;
>> + }
>> +
>> + /*
>> + * Not freed in normal path, persisent subsystem data (which is assumed
>> + * also in the reset-gpio driver).
>> + */
>> + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL);
>> + if (!rgpio_dev) {
>> + ret = -ENOMEM;
>> + goto err_ida_free;
>> + }
>> +
>> + ret = __reset_add_reset_gpio_lookup(id, args->np, args->args[0],
>> + args->args[1]);
>> + if (ret < 0)
>> + goto err_kfree;
>> +
>> + rgpio_dev->of_args = *args;
>> + /*
>> + * We keep the device_node reference, but of_args.np is put at the end
>> + * of __of_reset_control_get(), so get it one more time.
>> + * Hold reference as long as rgpio_dev memory is valid.
>> + */
>> + of_node_get(rgpio_dev->of_args.np);
>> + pdev = platform_device_register_data(NULL, "reset-gpio", id,
>> + &rgpio_dev->of_args,
>> + sizeof(rgpio_dev->of_args));
>> + ret = PTR_ERR_OR_ZERO(pdev);
>> + if (ret)
>> + goto err_put;
>> +
>> + list_add(&rgpio_dev->list, &reset_gpio_lookup_list);
>> +
>> +out:
>> + mutex_unlock(&reset_gpio_lookup_mutex);
>> +
>> + return 0;
>> +
>> +err_put:
>> + of_node_put(rgpio_dev->of_args.np);
>> +err_kfree:
>> + kfree(rgpio_dev);
>> +err_ida_free:
>> + ida_free(&reset_gpio_ida, id);
>> +err_unlock:
>> + mutex_unlock(&reset_gpio_lookup_mutex);
>> +
>
> You're already using cleanup helpers above, why not here too? Would
> make this function much more readable and allow you to drop all but

Not sure how much it would be cleaner considering that these are not
free on success.

> the ida_free() here. Possibly you'd need to define the __free()
> callback for of_node_put() though.
>
>> + return ret;
>> +}
>> +
>> +static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args,
>> + bool gpio_fallback)
>> +{
>> + struct reset_controller_dev *r, *rcdev;
>> +
>> + lockdep_assert_held(&reset_list_mutex);
>> +
>> + rcdev = NULL;
>> + list_for_each_entry(r, &reset_controller_list, list) {
>> + if (args->np == r->of_node) {
>> + if (gpio_fallback) {
>> + if (__reset_gpios_args_match(args, r->of_args)) {
>> + rcdev = r;
>> + break;
>> + }
>> + } else {
>> + rcdev = r;
>> + break;
>> + }
>> + }
>> + }
>> +
>> + return rcdev;
>> +}
>> +
>> struct reset_control *
>> __of_reset_control_get(struct device_node *node, const char *id, int index,
>> bool shared, bool optional, bool acquired)
>> {
>> + struct of_phandle_args args = {0};
>> + bool gpio_fallback = false;
>> struct reset_control *rstc;
>> - struct reset_controller_dev *r, *rcdev;
>> - struct of_phandle_args args;
>> + struct reset_controller_dev *rcdev;
>> int rstc_id;
>> int ret;
>>
>> @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index,
>> index, &args);
>> if (ret == -EINVAL)
>> return ERR_PTR(ret);
>> - if (ret)
>> - return optional ? NULL : ERR_PTR(ret);
>> + if (ret) {
>> + /*
>> + * There can be only one reset-gpio for regular devices, so
>> + * don't bother with GPIO index.
>> + */
>> + ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells",
>> + 0, &args);
>> + if (ret)
>> + return optional ? NULL : ERR_PTR(ret);
>>
>> - mutex_lock(&reset_list_mutex);
>> - rcdev = NULL;
>> - list_for_each_entry(r, &reset_controller_list, list) {
>> - if (args.np == r->of_node) {
>> - rcdev = r;
>> - break;
>> + gpio_fallback = true;
>> +
>> + ret = __reset_add_reset_gpio_device(&args);
>> + if (ret) {
>> + rstc = ERR_PTR(ret);
>> + goto out_put;
>> }
>> }
>>
>> + mutex_lock(&reset_list_mutex);
>> + rcdev = __reset_find_rcdev(&args, gpio_fallback);
>> if (!rcdev) {
>> rstc = ERR_PTR(-EPROBE_DEFER);
>> - goto out;
>> + goto out_unlock;
>> }
>>
>> if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
>> rstc = ERR_PTR(-EINVAL);
>> - goto out;
>> + goto out_unlock;
>> }
>>
>> rstc_id = rcdev->of_xlate(rcdev, &args);
>> if (rstc_id < 0) {
>> rstc = ERR_PTR(rstc_id);
>> - goto out;
>> + goto out_unlock;
>> }
>>
>> /* reset_list_mutex also protects the rcdev's reset_control list */
>> rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired);
>>
>> -out:
>> +out_unlock:
>> mutex_unlock(&reset_list_mutex);
>> +out_put:
>> of_node_put(args.np);
>
> I suggest reworking this to use cleanup.h as well.

It's independent task. This is an existing code and any refactoring to
cleanup or not is independent thing.

Best regards,
Krzysztof


2024-01-15 16:56:34

by Philipp Zabel

[permalink] [raw]
Subject: Re: [PATCH v3 2/5] reset: Instantiate reset GPIO controller for shared reset-gpios

On Fr, 2024-01-12 at 17:36 +0100, Krzysztof Kozlowski wrote:
[...]
> diff --git a/drivers/reset/core.c b/drivers/reset/core.c
> index 4d5a78d3c085..86e33a703ad2 100644
> --- a/drivers/reset/core.c
> +++ b/drivers/reset/core.c
[...]
> @@ -813,13 +832,183 @@ static void __reset_control_put_internal(struct reset_control *rstc)
> kref_put(&rstc->refcnt, __reset_control_release);
> }
>
> +static bool __reset_gpios_args_match(const struct of_phandle_args *a1,
> + const struct of_phandle_args *a2)
> +{
> + unsigned int i;
> +
> + if (!a2)
> + return false;
> +
> + if (a1->args_count != a2->args_count)
> + return false;
> +
> + for (i = 0; i < a1->args_count; i++)
> + if (a1->args[i] != a2->args[i])
> + return false;
> +
> + return true;
> +}

How about making this

return a2 &&
a1->np == a2->np &&
a1->args_count == a2->args_count &&
!memcmp(a1->args, a2->args, sizeof(a1->args[0]) * a1->args_count);

?

There's similar code in include/linux/cpufreq.h, maybe this could later
be lifted into a common of_phandle_args_equal().

> +
> +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np,
> + unsigned int gpio,
> + unsigned int of_flags)
> +{
> + struct gpiod_lookup_table *lookup __free(kfree) = NULL;
> + struct gpio_device *gdev __free(gpio_device_put) = NULL;
> + char *label __free(kfree) = NULL;
> + unsigned int lookup_flags;
> +
> + /*
> + * Later we map GPIO flags between OF and Linux, however not all
> + * constants from include/dt-bindings/gpio/gpio.h and
> + * include/linux/gpio/machine.h match each other.
> + */
> + if (of_flags > GPIO_ACTIVE_LOW) {
> + pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n",
> + of_flags, gpio);
> + return -EINVAL;
> + }
> +
> + gdev = gpio_device_find_by_fwnode(of_fwnode_handle(np));
> + if (!gdev)
> + return -EPROBE_DEFER;
> +
> + label = kstrdup(gpio_device_get_label(gdev), GFP_KERNEL);
> + if (!label)
> + return -EINVAL;

The kstrdup() failure looks like it should be -ENOMEM to me.
I'd check the gpio_device_get_label(gdev) return value separately.

Is this going to be in v6.8-rc1, or does using gpio_device_get_label()
introduce a dependency?

> +
> + /* Size: one lookup entry plus sentinel */
> + lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
> + if (!lookup)
> + return -ENOMEM;
> +
> + lookup->dev_id = kasprintf(GFP_KERNEL, "reset-gpio.%d", id);
> + if (!lookup->dev_id)
> + return -ENOMEM;
> +
> + lookup_flags = GPIO_PERSISTENT;
> + lookup_flags |= of_flags & GPIO_ACTIVE_LOW;
> + lookup->table[0] = GPIO_LOOKUP(no_free_ptr(label), gpio, "reset",
> + lookup_flags);
> +
> + gpiod_add_lookup_table(no_free_ptr(lookup));
> +
> + return 0;
> +}
> +
> +/*
> + * @reset_args: phandle to the GPIO provider with all the args like GPIO number

s/reset_//

> + */
> +static int __reset_add_reset_gpio_device(const struct of_phandle_args *args)
> +{
> + struct reset_gpio_lookup *rgpio_dev;
> + struct platform_device *pdev;
> + int id, ret;
> +
> + /*
> + * Registering reset-gpio device might cause immediate
> + * bind, resulting in its probe() registering new reset controller thus
> + * taking reset_list_mutex lock via reset_controller_register().
> + */
> + lockdep_assert_not_held(&reset_list_mutex);
> +
> + mutex_lock(&reset_gpio_lookup_mutex);
> +
> + list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) {
> + if (args->np == rgpio_dev->of_args.np) {
> + if (__reset_gpios_args_match(args, &rgpio_dev->of_args))
> + goto out; /* Already on the list, done */
> + }
> + }
> +
> + id = ida_alloc(&reset_gpio_ida, GFP_KERNEL);
> + if (id < 0) {
> + ret = id;
> + goto err_unlock;
> + }
> +
> + /*
> + * Not freed in normal path, persisent subsystem data (which is assumed
> + * also in the reset-gpio driver).
> + */
> + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL);
> + if (!rgpio_dev) {
> + ret = -ENOMEM;
> + goto err_ida_free;
> + }
> +
> + ret = __reset_add_reset_gpio_lookup(id, args->np, args->args[0],
> + args->args[1]);
> + if (ret < 0)
> + goto err_kfree;
> +
> + rgpio_dev->of_args = *args;
> + /*
> + * We keep the device_node reference, but of_args.np is put at the end
> + * of __of_reset_control_get(), so get it one more time.
> + * Hold reference as long as rgpio_dev memory is valid.
> + */
> + of_node_get(rgpio_dev->of_args.np);
> + pdev = platform_device_register_data(NULL, "reset-gpio", id,
> + &rgpio_dev->of_args,
> + sizeof(rgpio_dev->of_args));
> + ret = PTR_ERR_OR_ZERO(pdev);
> + if (ret)
> + goto err_put;
> +
> + list_add(&rgpio_dev->list, &reset_gpio_lookup_list);
> +
> +out:
> + mutex_unlock(&reset_gpio_lookup_mutex);
> +
> + return 0;
> +
> +err_put:
> + of_node_put(rgpio_dev->of_args.np);
> +err_kfree:
> + kfree(rgpio_dev);
> +err_ida_free:
> + ida_free(&reset_gpio_ida, id);
> +err_unlock:
> + mutex_unlock(&reset_gpio_lookup_mutex);
> +
> + return ret;
> +}
> +
> +static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args,
> + bool gpio_fallback)
> +{
> + struct reset_controller_dev *r, *rcdev;

Now that this is moved into a function, there's no need for the r,
rcdev split anymore. Just return a match when found, and NULL at the
end:

struct reset_controller_dev *rcdev;

> +
> + lockdep_assert_held(&reset_list_mutex);
> +
> + rcdev = NULL;
> + list_for_each_entry(r, &reset_controller_list, list) {

list_for_each_entry(rcdev, &reset_controller_list, list) {

> + if (args->np == r->of_node) {
> + if (gpio_fallback) {
> + if (__reset_gpios_args_match(args, r->of_args)) {
> + rcdev = r;
> + break;

return rcdev;

> + }
> + } else {
> + rcdev = r;
> + break;
> + }
> + }

With the np check moved into __reset_gpios_args_match() above, the
whole loop could be turned into:

if (gpio_fallback) {
if (__reset_gpios_args_match(args, rcdev->of_args))
return rcdev;
} else {
if (args->np == rcdev->of_node)
return rcdev;
}

Explicitly checking against rcdev->of_args->np instead of rcdev-
>of_node in gpio_fallback mode could avoid false positives in case
anybody ever creates a combined GPIO and reset controller device and
then uses its GPIOs to drive a shared reset line..

> + }
> +
> + return rcdev;

return NULL;

> +}
>
> struct reset_control *
> __of_reset_control_get(struct device_node *node, const char *id, int index,
> bool shared, bool optional, bool acquired)
> {
> + struct of_phandle_args args = {0};

Is this still needed?

> + bool gpio_fallback = false;
> struct reset_control *rstc;
> - struct reset_controller_dev *r, *rcdev;
> - struct of_phandle_args args;
> + struct reset_controller_dev *rcdev;
> int rstc_id;
> int ret;
>
> @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index,
> index, &args);
> if (ret == -EINVAL)
> return ERR_PTR(ret);
> - if (ret)
> - return optional ? NULL : ERR_PTR(ret);
> + if (ret) {
> + /*
> + * There can be only one reset-gpio for regular devices, so
> + * don't bother with GPIO index.
> + */
> + ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells",
> + 0, &args);
> + if (ret)
> + return optional ? NULL : ERR_PTR(ret);
>
> - mutex_lock(&reset_list_mutex);
> - rcdev = NULL;
> - list_for_each_entry(r, &reset_controller_list, list) {
> - if (args.np == r->of_node) {
> - rcdev = r;
> - break;
> + gpio_fallback = true;
> +
> + ret = __reset_add_reset_gpio_device(&args);
> + if (ret) {
> + rstc = ERR_PTR(ret);
> + goto out_put;
> }
> }
>
> + mutex_lock(&reset_list_mutex);
> + rcdev = __reset_find_rcdev(&args, gpio_fallback);
> if (!rcdev) {
> rstc = ERR_PTR(-EPROBE_DEFER);
> - goto out;
> + goto out_unlock;
> }
>
> if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {

Nice. I like that the __of_reset_control_get() changes are much less
invasive now.

> rstc = ERR_PTR(-EINVAL);
> - goto out;
> + goto out_unlock;
> }
>
> rstc_id = rcdev->of_xlate(rcdev, &args);
> if (rstc_id < 0) {
> rstc = ERR_PTR(rstc_id);
> - goto out;
> + goto out_unlock;
> }
>
> /* reset_list_mutex also protects the rcdev's reset_control list */
> rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired);
>
> -out:
> +out_unlock:
> mutex_unlock(&reset_list_mutex);
> +out_put:
> of_node_put(args.np);
>
> return rstc;
> diff --git a/include/linux/reset-controller.h b/include/linux/reset-controller.h
> index 0fa4f60e1186..e064473215de 100644
> --- a/include/linux/reset-controller.h
> +++ b/include/linux/reset-controller.h
> @@ -61,6 +61,9 @@ struct reset_control_lookup {
> * @dev: corresponding driver model device struct
> * @of_node: corresponding device tree node as phandle target
> * @of_reset_n_cells: number of cells in reset line specifiers
> + * TODO: of_args have of_node, so we have here duplication

Any plans what to do about this? With the above changes we could
mandate that either of_node or of_args should be set, never both.

> + * @of_args: for reset-gpios controllers: corresponding phandle args with GPIO
> + * number complementing of_node

regards
Philipp

2024-01-15 17:32:40

by Philipp Zabel

[permalink] [raw]
Subject: Re: [PATCH v3 2/5] reset: Instantiate reset GPIO controller for shared reset-gpios

On Mo, 2024-01-15 at 17:13 +0100, Krzysztof Kozlowski wrote:
> On 15/01/2024 17:06, Bartosz Golaszewski wrote:
> > > +
> > > +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np,
> > > + unsigned int gpio,
> > > + unsigned int of_flags)
> > > +{
> > > + struct gpiod_lookup_table *lookup __free(kfree) = NULL;
> > > + struct gpio_device *gdev __free(gpio_device_put) = NULL;
> > > + char *label __free(kfree) = NULL;
> >
> > I got yelled at by Linus Torvalds personally for doing it like this. I
> > know this is a common pattern in code using GLib but Linus wants auto
> > variables to be initialized where they're declared...
>
> Declaration is here. Initialization is here. Therefore this is
> initialized where it is declared. What's more it is initialized to a
> valid value, because __free() accepts NULLs.
[...]
> > ... so this should become:
> >
> > struct gpio_device *gdev __free(gpio_device_put) = gpio_device_find(...)
> >
> > and same for the rest.
> >
> > Don't get me wrong, I love cleanup.h but there's a (unofficial for
> > now) coding style.
>
> So you just want to declare it not in top-part of the function but just
> before first use?

IIUC, Linus wants exactly this:

https://lore.kernel.org/all/CAHk-=wgRHiV5VSxtfXA4S6aLUmcQYEuB67u3BJPJPtuESs1JyA@mail.gmail.com/

[...]
> >
> > > + goto out_unlock;
> > > }
> > >
> > > rstc_id = rcdev->of_xlate(rcdev, &args);
> > > if (rstc_id < 0) {
> > > rstc = ERR_PTR(rstc_id);
> > > - goto out;
> > > + goto out_unlock;
> > > }
> > >
> > > /* reset_list_mutex also protects the rcdev's reset_control list */
> > > rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired);
> > >
> > > -out:
> > > +out_unlock:
> > > mutex_unlock(&reset_list_mutex);
> > > +out_put:
> > > of_node_put(args.np);
> >
> > I suggest reworking this to use cleanup.h as well.
>
> It's independent task. This is an existing code and any refactoring to
> cleanup or not is independent thing.

Seconded. Separate cleanup very welcome, but this series is about
adding functionality.

regards
Philipp

2024-01-16 15:19:17

by Philipp Zabel

[permalink] [raw]
Subject: Re: [PATCH v3 5/5] i2c: muxes: pca954x: Allow sharing reset GPIO

On Fr, 2024-01-12 at 17:36 +0100, Krzysztof Kozlowski wrote:
> From: Chris Packham <[email protected]>
>
> Some hardware designs with multiple PCA954x devices use a reset GPIO
> connected to all the muxes. Support this configuration by making use of
> the reset controller framework which can deal with the shared reset
> GPIOs. Fall back to the old GPIO descriptor method if the reset
> controller framework is not enabled.
>
> Signed-off-by: Chris Packham <[email protected]>
> Acked-by: Peter Rosin <[email protected]>
> Reviewed-by: Krzysztof Kozlowski <[email protected]>
> Link: https://lore.kernel.org/r/[email protected]
> Signed-off-by: Krzysztof Kozlowski <[email protected]>
>
> ---
>
> If previous patches are fine, then this commit is independent and could
> be taken via I2C.
>
> Cc: Chris Packham <[email protected]>
> Cc: Bartosz Golaszewski <[email protected]>
> Cc: Sean Anderson <[email protected]>
> ---
> drivers/i2c/muxes/i2c-mux-pca954x.c | 46 ++++++++++++++++++++++++-----
> 1 file changed, 38 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/i2c/muxes/i2c-mux-pca954x.c b/drivers/i2c/muxes/i2c-mux-pca954x.c
> index 2219062104fb..1702e8d49b91 100644
> --- a/drivers/i2c/muxes/i2c-mux-pca954x.c
> +++ b/drivers/i2c/muxes/i2c-mux-pca954x.c
> @@ -49,6 +49,7 @@
> #include <linux/pm.h>
> #include <linux/property.h>
> #include <linux/regulator/consumer.h>
> +#include <linux/reset.h>
> #include <linux/slab.h>
> #include <linux/spinlock.h>
> #include <dt-bindings/mux/mux.h>
> @@ -102,6 +103,9 @@ struct pca954x {
> unsigned int irq_mask;
> raw_spinlock_t lock;
> struct regulator *supply;
> +
> + struct gpio_desc *reset_gpio;
> + struct reset_control *reset_cont;
> };
>
> /* Provide specs for the MAX735x, PCA954x and PCA984x types we know about */
> @@ -477,6 +481,35 @@ static int pca954x_init(struct i2c_client *client, struct pca954x *data)
> return ret;
> }
>
> +static int pca954x_get_reset(struct device *dev, struct pca954x *data)
> +{
> + data->reset_cont = devm_reset_control_get_optional_shared(dev, NULL);
> + if (IS_ERR(data->reset_cont))
> + return dev_err_probe(dev, PTR_ERR(data->reset_cont),
> + "Failed to get reset\n");
> + else if (data->reset_cont)
> + return 0;
> +
> + /*
> + * fallback to legacy reset-gpios
> + */

devm_reset_control_get_optional_shared() won't return NULL if the
"reset-gpios" property is found in the device tree, so the GPIO
fallback is dead code.

> + data->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
> + if (IS_ERR(data->reset_gpio)) {
> + return dev_err_probe(dev, PTR_ERR(data->reset_gpio),
> + "Failed to get reset gpio");
> + }
> +
> + return 0;
> +}
> +

regards
Philipp

2024-01-16 18:27:34

by Rob Herring (Arm)

[permalink] [raw]
Subject: Re: [PATCH v3 3/5] ASoC: dt-bindings: qcom,wsa8840: Add reset-gpios for shared line


On Fri, 12 Jan 2024 17:36:06 +0100, Krzysztof Kozlowski wrote:
> On newer Qualcomm platforms, like X1E80100-CRD, the WSA884x speakers
> share SD_N GPIOs between two speakers, thus a coordinated assertion is
> needed. Linux supports handling shared GPIO lines through "reset-gpios"
> property, thus allow specifying either powerdown or reset GPIOs (these
> are the same).
>
> Cc: Bartosz Golaszewski <[email protected]>
> Cc: Sean Anderson <[email protected]>
> Signed-off-by: Krzysztof Kozlowski <[email protected]>
>
> ---
>
> If previous patches are fine, then this commit is independent and could
> be taken via ASoC.
> ---
> .../devicetree/bindings/sound/qcom,wsa8840.yaml | 11 ++++++++++-
> 1 file changed, 10 insertions(+), 1 deletion(-)
>

Acked-by: Rob Herring <[email protected]>


2024-01-16 21:23:01

by Chris Packham

[permalink] [raw]
Subject: Re: [PATCH v3 5/5] i2c: muxes: pca954x: Allow sharing reset GPIO


On 17/01/24 04:18, Philipp Zabel wrote:
> On Fr, 2024-01-12 at 17:36 +0100, Krzysztof Kozlowski wrote:
>> From: Chris Packham <[email protected]>
>>
>> Some hardware designs with multiple PCA954x devices use a reset GPIO
>> connected to all the muxes. Support this configuration by making use of
>> the reset controller framework which can deal with the shared reset
>> GPIOs. Fall back to the old GPIO descriptor method if the reset
>> controller framework is not enabled.
>>
>> Signed-off-by: Chris Packham <[email protected]>
>> Acked-by: Peter Rosin <[email protected]>
>> Reviewed-by: Krzysztof Kozlowski <[email protected]>
>> Link: https://scanmail.trustwave.com/?c=20988&d=8p6m5Tfi2yYJWYV9xYGcYnz7UYxB6WTGTPkmGu7b8A&u=https%3a%2f%2flore%2ekernel%2eorg%2fr%2f20240108041913%2e7078-1-chris%2epackham%40alliedtelesis%2eco%2enz
>> Signed-off-by: Krzysztof Kozlowski <[email protected]>
>>
>> ---
>>
>> If previous patches are fine, then this commit is independent and could
>> be taken via I2C.
>>
>> Cc: Chris Packham <[email protected]>
>> Cc: Bartosz Golaszewski <[email protected]>
>> Cc: Sean Anderson <[email protected]>
>> ---
>> drivers/i2c/muxes/i2c-mux-pca954x.c | 46 ++++++++++++++++++++++++-----
>> 1 file changed, 38 insertions(+), 8 deletions(-)
>>
>> diff --git a/drivers/i2c/muxes/i2c-mux-pca954x.c b/drivers/i2c/muxes/i2c-mux-pca954x.c
>> index 2219062104fb..1702e8d49b91 100644
>> --- a/drivers/i2c/muxes/i2c-mux-pca954x.c
>> +++ b/drivers/i2c/muxes/i2c-mux-pca954x.c
>> @@ -49,6 +49,7 @@
>> #include <linux/pm.h>
>> #include <linux/property.h>
>> #include <linux/regulator/consumer.h>
>> +#include <linux/reset.h>
>> #include <linux/slab.h>
>> #include <linux/spinlock.h>
>> #include <dt-bindings/mux/mux.h>
>> @@ -102,6 +103,9 @@ struct pca954x {
>> unsigned int irq_mask;
>> raw_spinlock_t lock;
>> struct regulator *supply;
>> +
>> + struct gpio_desc *reset_gpio;
>> + struct reset_control *reset_cont;
>> };
>>
>> /* Provide specs for the MAX735x, PCA954x and PCA984x types we know about */
>> @@ -477,6 +481,35 @@ static int pca954x_init(struct i2c_client *client, struct pca954x *data)
>> return ret;
>> }
>>
>> +static int pca954x_get_reset(struct device *dev, struct pca954x *data)
>> +{
>> + data->reset_cont = devm_reset_control_get_optional_shared(dev, NULL);
>> + if (IS_ERR(data->reset_cont))
>> + return dev_err_probe(dev, PTR_ERR(data->reset_cont),
>> + "Failed to get reset\n");
>> + else if (data->reset_cont)
>> + return 0;
>> +
>> + /*
>> + * fallback to legacy reset-gpios
>> + */
> devm_reset_control_get_optional_shared() won't return NULL if the
> "reset-gpios" property is found in the device tree, so the GPIO
> fallback is dead code.

Hmm, I was attempting to handle the case where CONFIG_RESET_GPIO wasn't
set or the reset core wasn't enabled. It doesn't appear that the latter
is even possible so no need to worry about that. For the former it looks
like we'd get -EPROBE_DEFER. I could change to check for that or just
remove the GPIO fallback entirely. Any preference?

>
>> + data->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
>> + if (IS_ERR(data->reset_gpio)) {
>> + return dev_err_probe(dev, PTR_ERR(data->reset_gpio),
>> + "Failed to get reset gpio");
>> + }
>> +
>> + return 0;
>> +}
>> +
> regards
> Philipp

2024-01-17 11:16:45

by Philipp Zabel

[permalink] [raw]
Subject: Re: [PATCH v3 5/5] i2c: muxes: pca954x: Allow sharing reset GPIO

On Di, 2024-01-16 at 19:58 +0000, Chris Packham wrote:
> On 17/01/24 04:18, Philipp Zabel wrote:
> > On Fr, 2024-01-12 at 17:36 +0100, Krzysztof Kozlowski wrote:
> > > From: Chris Packham <[email protected]>
> > >
> > > Some hardware designs with multiple PCA954x devices use a reset GPIO
> > > connected to all the muxes. Support this configuration by making use of
> > > the reset controller framework which can deal with the shared reset
> > > GPIOs. Fall back to the old GPIO descriptor method if the reset
> > > controller framework is not enabled.
> > >
> > > Signed-off-by: Chris Packham <[email protected]>
> > > Acked-by: Peter Rosin <[email protected]>
> > > Reviewed-by: Krzysztof Kozlowski <[email protected]>
> > > Link: https://scanmail.trustwave.com/?c=20988&d=8p6m5Tfi2yYJWYV9xYGcYnz7UYxB6WTGTPkmGu7b8A&u=https%3a%2f%2flore%2ekernel%2eorg%2fr%2f20240108041913%2e7078-1-chris%2epackham%40alliedtelesis%2eco%2enz
> > > Signed-off-by: Krzysztof Kozlowski <[email protected]>
> > >
> > > ---
> > >
> > > If previous patches are fine, then this commit is independent and could
> > > be taken via I2C.
> > >
> > > Cc: Chris Packham <[email protected]>
> > > Cc: Bartosz Golaszewski <[email protected]>
> > > Cc: Sean Anderson <[email protected]>
> > > ---
> > > drivers/i2c/muxes/i2c-mux-pca954x.c | 46 ++++++++++++++++++++++++-----
> > > 1 file changed, 38 insertions(+), 8 deletions(-)
> > >
> > > diff --git a/drivers/i2c/muxes/i2c-mux-pca954x.c b/drivers/i2c/muxes/i2c-mux-pca954x.c
> > > index 2219062104fb..1702e8d49b91 100644
> > > --- a/drivers/i2c/muxes/i2c-mux-pca954x.c
> > > +++ b/drivers/i2c/muxes/i2c-mux-pca954x.c
> > > @@ -49,6 +49,7 @@
> > > #include <linux/pm.h>
> > > #include <linux/property.h>
> > > #include <linux/regulator/consumer.h>
> > > +#include <linux/reset.h>
> > > #include <linux/slab.h>
> > > #include <linux/spinlock.h>
> > > #include <dt-bindings/mux/mux.h>
> > > @@ -102,6 +103,9 @@ struct pca954x {
> > > unsigned int irq_mask;
> > > raw_spinlock_t lock;
> > > struct regulator *supply;
> > > +
> > > + struct gpio_desc *reset_gpio;
> > > + struct reset_control *reset_cont;
> > > };
> > >
> > > /* Provide specs for the MAX735x, PCA954x and PCA984x types we know about */
> > > @@ -477,6 +481,35 @@ static int pca954x_init(struct i2c_client *client, struct pca954x *data)
> > > return ret;
> > > }
> > >
> > > +static int pca954x_get_reset(struct device *dev, struct pca954x *data)
> > > +{
> > > + data->reset_cont = devm_reset_control_get_optional_shared(dev, NULL);
> > > + if (IS_ERR(data->reset_cont))
> > > + return dev_err_probe(dev, PTR_ERR(data->reset_cont),
> > > + "Failed to get reset\n");
> > > + else if (data->reset_cont)
> > > + return 0;
> > > +
> > > + /*
> > > + * fallback to legacy reset-gpios
> > > + */
> > devm_reset_control_get_optional_shared() won't return NULL if the
> > "reset-gpios" property is found in the device tree, so the GPIO
> > fallback is dead code.
>
> Hmm, I was attempting to handle the case where CONFIG_RESET_GPIO wasn't
> set [...]
> [...] it looks like we'd get -EPROBE_DEFER. I could change to check
> for that or just remove the GPIO fallback entirely. Any preference?

I hadn't considered this.

If CONFIG_RESET_GPIO=n, devm_reset_control_get_optional_shared()
probably shouldn't return -EPROBE_DEFER. If we change that, the GPIO
fallback here can stay as is.

The alternative would be to drop the fallback and select RESET_GPIO.
Using -EPROBE_DEFER for fallback detection is no good, as there could
be a valid probe deferral if reset-gpio is compiled as a module that
will be loaded later.

regards
Philipp

2024-01-17 11:27:21

by Philipp Zabel

[permalink] [raw]
Subject: Re: [PATCH v3 2/5] reset: Instantiate reset GPIO controller for shared reset-gpios

On Fr, 2024-01-12 at 17:36 +0100, Krzysztof Kozlowski wrote:
[...]
> struct reset_control *
> __of_reset_control_get(struct device_node *node, const char *id, int index,
> bool shared, bool optional, bool acquired)
> {
> + struct of_phandle_args args = {0};
> + bool gpio_fallback = false;
> struct reset_control *rstc;
> - struct reset_controller_dev *r, *rcdev;
> - struct of_phandle_args args;
> + struct reset_controller_dev *rcdev;
> int rstc_id;
> int ret;
>
> @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index,
> index, &args);
> if (ret == -EINVAL)
> return ERR_PTR(ret);
> - if (ret)
> - return optional ? NULL : ERR_PTR(ret);
> + if (ret) {

I think this should continue to return optional ? NULL : ERR_PTR(ret)
if !IS_ENABLED(CONFIG_RESET_GPIO), for example by just skipping the
of_parse_phandle_with_args(). That should allow the GPIO fallback in
patch 5 to work as expected.

> + /*
> + * There can be only one reset-gpio for regular devices, so
> + * don't bother with GPIO index.
> + */
> + ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells",
> + 0, &args);
> + if (ret)
> + return optional ? NULL : ERR_PTR(ret);
>
> - mutex_lock(&reset_list_mutex);
> - rcdev = NULL;
> - list_for_each_entry(r, &reset_controller_list, list) {
> - if (args.np == r->of_node) {
> - rcdev = r;
> - break;
> + gpio_fallback = true;
> +
> + ret = __reset_add_reset_gpio_device(&args);
> + if (ret) {
> + rstc = ERR_PTR(ret);
> + goto out_put;
> }
> }

regards
Philipp

2024-01-17 19:57:00

by Chris Packham

[permalink] [raw]
Subject: Re: [PATCH v3 5/5] i2c: muxes: pca954x: Allow sharing reset GPIO


On 18/01/24 00:16, Philipp Zabel wrote:
> On Di, 2024-01-16 at 19:58 +0000, Chris Packham wrote:
>> On 17/01/24 04:18, Philipp Zabel wrote:
>>> On Fr, 2024-01-12 at 17:36 +0100, Krzysztof Kozlowski wrote:
>>>> From: Chris Packham <[email protected]>
>>>>
>>>> Some hardware designs with multiple PCA954x devices use a reset GPIO
>>>> connected to all the muxes. Support this configuration by making use of
>>>> the reset controller framework which can deal with the shared reset
>>>> GPIOs. Fall back to the old GPIO descriptor method if the reset
>>>> controller framework is not enabled.
>>>>
>>>> Signed-off-by: Chris Packham <[email protected]>
>>>> Acked-by: Peter Rosin <[email protected]>
>>>> Reviewed-by: Krzysztof Kozlowski <[email protected]>
>>>> Link: https://scanmail.trustwave.com/?c=20988&d=_ban5W3xRzKSimJ9ijTJ74p10otfq8ZONfjVnBr6Fw&u=https%3a%2f%2flore%2ekernel%2eorg%2fr%2f20240108041913%2e7078-1-chris%2epackham%40alliedtelesis%2eco%2enz
>>>> Signed-off-by: Krzysztof Kozlowski <[email protected]>
>>>>
>>>> ---
>>>>
>>>> If previous patches are fine, then this commit is independent and could
>>>> be taken via I2C.
>>>>
>>>> Cc: Chris Packham <[email protected]>
>>>> Cc: Bartosz Golaszewski <[email protected]>
>>>> Cc: Sean Anderson <[email protected]>
>>>> ---
>>>> drivers/i2c/muxes/i2c-mux-pca954x.c | 46 ++++++++++++++++++++++++-----
>>>> 1 file changed, 38 insertions(+), 8 deletions(-)
>>>>
>>>> diff --git a/drivers/i2c/muxes/i2c-mux-pca954x.c b/drivers/i2c/muxes/i2c-mux-pca954x.c
>>>> index 2219062104fb..1702e8d49b91 100644
>>>> --- a/drivers/i2c/muxes/i2c-mux-pca954x.c
>>>> +++ b/drivers/i2c/muxes/i2c-mux-pca954x.c
>>>> @@ -49,6 +49,7 @@
>>>> #include <linux/pm.h>
>>>> #include <linux/property.h>
>>>> #include <linux/regulator/consumer.h>
>>>> +#include <linux/reset.h>
>>>> #include <linux/slab.h>
>>>> #include <linux/spinlock.h>
>>>> #include <dt-bindings/mux/mux.h>
>>>> @@ -102,6 +103,9 @@ struct pca954x {
>>>> unsigned int irq_mask;
>>>> raw_spinlock_t lock;
>>>> struct regulator *supply;
>>>> +
>>>> + struct gpio_desc *reset_gpio;
>>>> + struct reset_control *reset_cont;
>>>> };
>>>>
>>>> /* Provide specs for the MAX735x, PCA954x and PCA984x types we know about */
>>>> @@ -477,6 +481,35 @@ static int pca954x_init(struct i2c_client *client, struct pca954x *data)
>>>> return ret;
>>>> }
>>>>
>>>> +static int pca954x_get_reset(struct device *dev, struct pca954x *data)
>>>> +{
>>>> + data->reset_cont = devm_reset_control_get_optional_shared(dev, NULL);
>>>> + if (IS_ERR(data->reset_cont))
>>>> + return dev_err_probe(dev, PTR_ERR(data->reset_cont),
>>>> + "Failed to get reset\n");
>>>> + else if (data->reset_cont)
>>>> + return 0;
>>>> +
>>>> + /*
>>>> + * fallback to legacy reset-gpios
>>>> + */
>>> devm_reset_control_get_optional_shared() won't return NULL if the
>>> "reset-gpios" property is found in the device tree, so the GPIO
>>> fallback is dead code.
>> Hmm, I was attempting to handle the case where CONFIG_RESET_GPIO wasn't
>> set [...]
>> [...] it looks like we'd get -EPROBE_DEFER. I could change to check
>> for that or just remove the GPIO fallback entirely. Any preference?
> I hadn't considered this.
>
> If CONFIG_RESET_GPIO=n, devm_reset_control_get_optional_shared()
> probably shouldn't return -EPROBE_DEFER. If we change that, the GPIO
> fallback here can stay as is.
>
> The alternative would be to drop the fallback and select RESET_GPIO.
> Using -EPROBE_DEFER for fallback detection is no good, as there could
> be a valid probe deferral if reset-gpio is compiled as a module that
> will be loaded later.
I did consider adding `select RESET_GPIO` (or maybe just `imply
RESET_GPIO`) initially but decided on the fallback as a way of avoiding
surprises for existing users. I'll see if anyone else has a different
suggestion but assuming nothing else changes I'll work with Krzystof to
get an updated patch for this series.

2024-01-22 13:03:40

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v3 2/5] reset: Instantiate reset GPIO controller for shared reset-gpios

On 15/01/2024 18:32, Philipp Zabel wrote:
> On Mo, 2024-01-15 at 17:13 +0100, Krzysztof Kozlowski wrote:
>> On 15/01/2024 17:06, Bartosz Golaszewski wrote:
>>>> +
>>>> +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np,
>>>> + unsigned int gpio,
>>>> + unsigned int of_flags)
>>>> +{
>>>> + struct gpiod_lookup_table *lookup __free(kfree) = NULL;
>>>> + struct gpio_device *gdev __free(gpio_device_put) = NULL;
>>>> + char *label __free(kfree) = NULL;
>>>
>>> I got yelled at by Linus Torvalds personally for doing it like this. I
>>> know this is a common pattern in code using GLib but Linus wants auto
>>> variables to be initialized where they're declared...
>>
>> Declaration is here. Initialization is here. Therefore this is
>> initialized where it is declared. What's more it is initialized to a
>> valid value, because __free() accepts NULLs.
> [...]
>>> ... so this should become:
>>>
>>> struct gpio_device *gdev __free(gpio_device_put) = gpio_device_find(...)
>>>
>>> and same for the rest.
>>>
>>> Don't get me wrong, I love cleanup.h but there's a (unofficial for
>>> now) coding style.
>>
>> So you just want to declare it not in top-part of the function but just
>> before first use?
>
> IIUC, Linus wants exactly this:
>
> https://lore.kernel.org/all/CAHk-=wgRHiV5VSxtfXA4S6aLUmcQYEuB67u3BJPJPtuESs1JyA@mail.gmail.com/

Sure.



Best regards,
Krzysztof


2024-01-22 13:13:27

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v3 2/5] reset: Instantiate reset GPIO controller for shared reset-gpios

On 15/01/2024 17:55, Philipp Zabel wrote:
> On Fr, 2024-01-12 at 17:36 +0100, Krzysztof Kozlowski wrote:
>>
>> +static bool __reset_gpios_args_match(const struct of_phandle_args *a1,
>> + const struct of_phandle_args *a2)
>> +{
>> + unsigned int i;
>> +
>> + if (!a2)
>> + return false;
>> +
>> + if (a1->args_count != a2->args_count)
>> + return false;
>> +
>> + for (i = 0; i < a1->args_count; i++)
>> + if (a1->args[i] != a2->args[i])
>> + return false;
>> +
>> + return true;
>> +}
>
> How about making this
>
> return a2 &&
> a1->np == a2->np &&
> a1->args_count == a2->args_count &&
> !memcmp(a1->args, a2->args, sizeof(a1->args[0]) * a1->args_count);
>
> ?
>
> There's similar code in include/linux/cpufreq.h, maybe this could later
> be lifted into a common of_phandle_args_equal().

I'll make a helper because such long return is also not the fastest to
parse by brain.

>
>> +
>> +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np,
>> + unsigned int gpio,
>> + unsigned int of_flags)
>> +{
>> + struct gpiod_lookup_table *lookup __free(kfree) = NULL;
>> + struct gpio_device *gdev __free(gpio_device_put) = NULL;
>> + char *label __free(kfree) = NULL;
>> + unsigned int lookup_flags;
>> +
>> + /*
>> + * Later we map GPIO flags between OF and Linux, however not all
>> + * constants from include/dt-bindings/gpio/gpio.h and
>> + * include/linux/gpio/machine.h match each other.
>> + */
>> + if (of_flags > GPIO_ACTIVE_LOW) {
>> + pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n",
>> + of_flags, gpio);
>> + return -EINVAL;
>> + }
>> +
>> + gdev = gpio_device_find_by_fwnode(of_fwnode_handle(np));
>> + if (!gdev)
>> + return -EPROBE_DEFER;
>> +
>> + label = kstrdup(gpio_device_get_label(gdev), GFP_KERNEL);
>> + if (!label)
>> + return -EINVAL;
>
> The kstrdup() failure looks like it should be -ENOMEM to me.
> I'd check the gpio_device_get_label(gdev) return value separately.

OK, makes sense. One more local variable will be needed for that.

>
> Is this going to be in v6.8-rc1, or does using gpio_device_get_label()
> introduce a dependency?

We were already in the merge window, so no problem here.
gpio_device_get_label() is in v6.8-rc1.


>
>> +
>> + /* Size: one lookup entry plus sentinel */
>> + lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
>> + if (!lookup)
>> + return -ENOMEM;
>> +
>> + lookup->dev_id = kasprintf(GFP_KERNEL, "reset-gpio.%d", id);
>> + if (!lookup->dev_id)
>> + return -ENOMEM;
>> +
>> + lookup_flags = GPIO_PERSISTENT;
>> + lookup_flags |= of_flags & GPIO_ACTIVE_LOW;
>> + lookup->table[0] = GPIO_LOOKUP(no_free_ptr(label), gpio, "reset",
>> + lookup_flags);
>> +
>> + gpiod_add_lookup_table(no_free_ptr(lookup));
>> +
>> + return 0;
>> +}
>> +
>> +/*
>> + * @reset_args: phandle to the GPIO provider with all the args like GPIO number
>
> s/reset_//

ack

>
>> + */
>> +static int __reset_add_reset_gpio_device(const struct of_phandle_args *args)
>> +{
>> + struct reset_gpio_lookup *rgpio_dev;
>> + struct platform_device *pdev;
>> + int id, ret;
>> +
>> + /*
>> + * Registering reset-gpio device might cause immediate
>> + * bind, resulting in its probe() registering new reset controller thus
>> + * taking reset_list_mutex lock via reset_controller_register().
>> + */
>> + lockdep_assert_not_held(&reset_list_mutex);
>> +
>> + mutex_lock(&reset_gpio_lookup_mutex);
>> +
>> + list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) {
>> + if (args->np == rgpio_dev->of_args.np) {
>> + if (__reset_gpios_args_match(args, &rgpio_dev->of_args))
>> + goto out; /* Already on the list, done */
>> + }
>> + }
>> +
>> + id = ida_alloc(&reset_gpio_ida, GFP_KERNEL);
>> + if (id < 0) {
>> + ret = id;
>> + goto err_unlock;
>> + }
>> +
>> + /*
>> + * Not freed in normal path, persisent subsystem data (which is assumed
>> + * also in the reset-gpio driver).
>> + */
>> + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL);
>> + if (!rgpio_dev) {
>> + ret = -ENOMEM;
>> + goto err_ida_free;
>> + }
>> +
>> + ret = __reset_add_reset_gpio_lookup(id, args->np, args->args[0],
>> + args->args[1]);
>> + if (ret < 0)
>> + goto err_kfree;
>> +
>> + rgpio_dev->of_args = *args;
>> + /*
>> + * We keep the device_node reference, but of_args.np is put at the end
>> + * of __of_reset_control_get(), so get it one more time.
>> + * Hold reference as long as rgpio_dev memory is valid.
>> + */
>> + of_node_get(rgpio_dev->of_args.np);
>> + pdev = platform_device_register_data(NULL, "reset-gpio", id,
>> + &rgpio_dev->of_args,
>> + sizeof(rgpio_dev->of_args));
>> + ret = PTR_ERR_OR_ZERO(pdev);
>> + if (ret)
>> + goto err_put;
>> +
>> + list_add(&rgpio_dev->list, &reset_gpio_lookup_list);
>> +
>> +out:
>> + mutex_unlock(&reset_gpio_lookup_mutex);
>> +
>> + return 0;
>> +
>> +err_put:
>> + of_node_put(rgpio_dev->of_args.np);
>> +err_kfree:
>> + kfree(rgpio_dev);
>> +err_ida_free:
>> + ida_free(&reset_gpio_ida, id);
>> +err_unlock:
>> + mutex_unlock(&reset_gpio_lookup_mutex);
>> +
>> + return ret;
>> +}
>> +
>> +static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args,
>> + bool gpio_fallback)
>> +{
>> + struct reset_controller_dev *r, *rcdev;
>
> Now that this is moved into a function, there's no need for the r,
> rcdev split anymore. Just return a match when found, and NULL at the
> end:
>
> struct reset_controller_dev *rcdev;

Indeed, thanks.

>
>> +
>> + lockdep_assert_held(&reset_list_mutex);
>> +
>> + rcdev = NULL;
>> + list_for_each_entry(r, &reset_controller_list, list) {
>
> list_for_each_entry(rcdev, &reset_controller_list, list) {
>
>> + if (args->np == r->of_node) {
>> + if (gpio_fallback) {
>> + if (__reset_gpios_args_match(args, r->of_args)) {
>> + rcdev = r;
>> + break;
>
> return rcdev;
>
>> + }
>> + } else {
>> + rcdev = r;
>> + break;
>> + }
>> + }
>
> With the np check moved into __reset_gpios_args_match() above, the
> whole loop could be turned into:
>
> if (gpio_fallback) {
> if (__reset_gpios_args_match(args, rcdev->of_args))
> return rcdev;
> } else {
> if (args->np == rcdev->of_node)
> return rcdev;
> }
>
> Explicitly checking against rcdev->of_args->np instead of rcdev-
>> of_node in gpio_fallback mode could avoid false positives in case
> anybody ever creates a combined GPIO and reset controller device and
> then uses its GPIOs to drive a shared reset line..

ack

>
>> + }
>> +
>> + return rcdev;
>
> return NULL;

ack

>
>> +}
>>
>> struct reset_control *
>> __of_reset_control_get(struct device_node *node, const char *id, int index,
>> bool shared, bool optional, bool acquired)
>> {
>> + struct of_phandle_args args = {0};
>
> Is this still needed?

I will double check.

>
>> + bool gpio_fallback = false;
>> struct reset_control *rstc;
>> - struct reset_controller_dev *r, *rcdev;
>> - struct of_phandle_args args;
>> + struct reset_controller_dev *rcdev;
>> int rstc_id;
>> int ret;
>>
>> @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index,
>> index, &args);
>> if (ret == -EINVAL)
>> return ERR_PTR(ret);
>> - if (ret)
>> - return optional ? NULL : ERR_PTR(ret);
>> + if (ret) {
>> + /*
>> + * There can be only one reset-gpio for regular devices, so
>> + * don't bother with GPIO index.
>> + */
>> + ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells",
>> + 0, &args);
>> + if (ret)
>> + return optional ? NULL : ERR_PTR(ret);
>>
>> - mutex_lock(&reset_list_mutex);
>> - rcdev = NULL;
>> - list_for_each_entry(r, &reset_controller_list, list) {
>> - if (args.np == r->of_node) {
>> - rcdev = r;
>> - break;
>> + gpio_fallback = true;
>> +
>> + ret = __reset_add_reset_gpio_device(&args);
>> + if (ret) {
>> + rstc = ERR_PTR(ret);
>> + goto out_put;
>> }
>> }
>>
>> + mutex_lock(&reset_list_mutex);
>> + rcdev = __reset_find_rcdev(&args, gpio_fallback);
>> if (!rcdev) {
>> rstc = ERR_PTR(-EPROBE_DEFER);
>> - goto out;
>> + goto out_unlock;
>> }
>>
>> if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
>
> Nice. I like that the __of_reset_control_get() changes are much less
> invasive now.
>
>> rstc = ERR_PTR(-EINVAL);
>> - goto out;
>> + goto out_unlock;
>> }
>>
>> rstc_id = rcdev->of_xlate(rcdev, &args);
>> if (rstc_id < 0) {
>> rstc = ERR_PTR(rstc_id);
>> - goto out;
>> + goto out_unlock;
>> }
>>
>> /* reset_list_mutex also protects the rcdev's reset_control list */
>> rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired);
>>
>> -out:
>> +out_unlock:
>> mutex_unlock(&reset_list_mutex);
>> +out_put:
>> of_node_put(args.np);
>>
>> return rstc;
>> diff --git a/include/linux/reset-controller.h b/include/linux/reset-controller.h
>> index 0fa4f60e1186..e064473215de 100644
>> --- a/include/linux/reset-controller.h
>> +++ b/include/linux/reset-controller.h
>> @@ -61,6 +61,9 @@ struct reset_control_lookup {
>> * @dev: corresponding driver model device struct
>> * @of_node: corresponding device tree node as phandle target
>> * @of_reset_n_cells: number of cells in reset line specifiers
>> + * TODO: of_args have of_node, so we have here duplication
>
> Any plans what to do about this? With the above changes we could
> mandate that either of_node or of_args should be set, never both.

Yes, makes sense. We could also drop of_node, but the code won't be more
readable.

Best regards,
Krzysztof


2024-01-22 13:47:46

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v3 2/5] reset: Instantiate reset GPIO controller for shared reset-gpios

On 17/01/2024 12:26, Philipp Zabel wrote:
> On Fr, 2024-01-12 at 17:36 +0100, Krzysztof Kozlowski wrote:
> [...]
>> struct reset_control *
>> __of_reset_control_get(struct device_node *node, const char *id, int index,
>> bool shared, bool optional, bool acquired)
>> {
>> + struct of_phandle_args args = {0};
>> + bool gpio_fallback = false;
>> struct reset_control *rstc;
>> - struct reset_controller_dev *r, *rcdev;
>> - struct of_phandle_args args;
>> + struct reset_controller_dev *rcdev;
>> int rstc_id;
>> int ret;
>>
>> @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index,
>> index, &args);
>> if (ret == -EINVAL)
>> return ERR_PTR(ret);
>> - if (ret)
>> - return optional ? NULL : ERR_PTR(ret);
>> + if (ret) {
>
> I think this should continue to return optional ? NULL : ERR_PTR(ret)
> if !IS_ENABLED(CONFIG_RESET_GPIO), for example by just skipping the
> of_parse_phandle_with_args(). That should allow the GPIO fallback in
> patch 5 to work as expected.
>

ack

Best regards,
Krzysztof