2024-01-05 15:59:41

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH v2 0/4] reset: gpio: ASoC: shared GPIO resets

Hi,

Changes in v2
=============
1. wsa884x.c: add missing return in wsa884x_get_reset(), correct comment.
2. qcom,wsa8840.yaml: fix oneOf syntax.
3. 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".
4. reset-gpio.c:
- Fix smatch warning on platdata evaluation.
- Parse GPIO args and store them in rc.of_args.

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]>

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/reset/Kconfig | 9 +
drivers/reset/Makefile | 1 +
drivers/reset/core.c | 176 ++++++++++++++++--
drivers/reset/reset-gpio.c | 121 ++++++++++++
include/linux/reset-controller.h | 4 +
sound/soc/codecs/wsa884x.c | 53 +++++-
8 files changed, 356 insertions(+), 24 deletions(-)
create mode 100644 drivers/reset/reset-gpio.c

--
2.34.1



2024-01-05 16:00:10

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH v2 1/4] 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: 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..cf0a867cbc5f
--- /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 void reset_gpio_of_args_put(void *data)
+{
+ of_node_put(data);
+}
+
+static int reset_gpio_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct device_node **platdata = dev_get_platdata(dev);
+ struct of_phandle_args gpio_args;
+ struct reset_gpio_priv *priv;
+ int ret;
+
+ if (!platdata || !*platdata)
+ return -EINVAL;
+
+ priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+
+ platform_set_drvdata(pdev, &priv->rc);
+ device_set_node(dev, of_fwnode_handle(*platdata));
+
+ 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");
+
+ ret = of_parse_phandle_with_args(*platdata, "reset-gpios",
+ "#gpio-cells", 0, &gpio_args);
+ if (ret)
+ return ret;
+
+ priv->rc.ops = &reset_gpio_ops;
+ priv->rc.owner = THIS_MODULE;
+ priv->rc.dev = dev;
+ priv->rc.nr_resets = 1;
+ priv->rc.of_node = gpio_args.np;
+ ret = devm_add_action_or_reset(dev, reset_gpio_of_args_put,
+ priv->rc.of_node);
+ if (ret)
+ return ret;
+
+ priv->rc.of_args = devm_kmemdup(dev, &gpio_args, sizeof(gpio_args),
+ GFP_KERNEL);
+ if (!priv->rc.of_args)
+ return -ENOMEM;
+
+ 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-05 16:00:17

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH v2 2/4] 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, which is missing but
there is a reset-gpios property, 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].

All newly registered "reset-gpio" platform devices will be stored on
their own list to avoid any duplicated devices. The key to find each of
such platform device is the entire Devicetree GPIO specifier: phandle to
GPIO controller, GPIO number and GPIO flags. If two devices have
conflicting "reset-gpios" property, e.g. with different ACTIVE_xxx
flags, this would spawn two separate "reset-gpio" devices, where the
second would fail probing on busy GPIO reques

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

diff --git a/drivers/reset/core.c b/drivers/reset/core.c
index 4d5a78d3c085..ec9b3ff419cf 100644
--- a/drivers/reset/core.c
+++ b/drivers/reset/core.c
@@ -13,6 +13,7 @@
#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 +24,10 @@ static LIST_HEAD(reset_controller_list);
static DEFINE_MUTEX(reset_lookup_mutex);
static LIST_HEAD(reset_lookup_list);

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

+/**
+ * struct reset_gpio_device - ad-hoc created reset-gpio device
+ * @of_args: phandle to the reset controller with all the args like GPIO number
+ * @list: list entry for the reset_lookup_list
+ */
+struct reset_gpio_device {
+ 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 +828,119 @@ 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])
+ break;
+
+ /* All args matched? */
+ if (i == a1->args_count)
+ return true;
+
+ return false;
+}
+
+/*
+ * @node: node of the device requesting reset
+ * @reset_args: phandle to the reset controller with all the args like GPIO number
+ */
+static int __reset_add_reset_gpio_device(struct device_node *node,
+ struct of_phandle_args *args)
+{
+ struct reset_gpio_device *rgpio_dev;
+ struct platform_device *pdev;
+ int ret;
+
+ lockdep_assert_not_held(&reset_list_mutex);
+
+ mutex_lock(&reset_gpio_device_mutex);
+
+ list_for_each_entry(rgpio_dev, &reset_gpio_device_list, list) {
+ if (args->np == rgpio_dev->of_args.np) {
+ if (__reset_gpios_args_match(args,
+ &rgpio_dev->of_args)) {
+ ret = 0;
+ goto out_unlock;
+ }
+ }
+ }
+
+ /* Not freed in normal path, persisent subsyst data */
+ rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL);
+ if (!rgpio_dev) {
+ ret = -ENOMEM;
+ goto out_unlock;
+ }
+
+ rgpio_dev->of_args = *args;
+ pdev = platform_device_register_data(NULL, "reset-gpio",
+ PLATFORM_DEVID_AUTO, &node,
+ sizeof(node));
+ ret = PTR_ERR_OR_ZERO(pdev);
+ if (!ret)
+ list_add(&rgpio_dev->list, &reset_gpio_device_list);
+ else
+ kfree(rgpio_dev);
+
+out_unlock:
+ mutex_unlock(&reset_gpio_device_mutex);
+
+ return ret;
+}
+
+static struct reset_controller_dev *__reset_find_rcdev(struct of_phandle_args *args,
+ bool gpio_fallback,
+ const void *cookie)
+{
+ 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)) {
+ /*
+ * Fake args (take first reset) and
+ * args_count (to matcg reset-gpio
+ * of_reset_n_cells) because reset-gpio
+ * has only one reset and does not care
+ * about reset of GPIO specifier.
+ */
+ args->args[0] = 0;
+ args->args_count = 1;
+ 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,21 +960,50 @@ __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;
}

+ mutex_lock(&reset_list_mutex);
+ rcdev = __reset_find_rcdev(&args, gpio_fallback, NULL);
+
if (!rcdev) {
- rstc = ERR_PTR(-EPROBE_DEFER);
- goto out;
+ if (gpio_fallback) {
+ /*
+ * Registering reset-gpio device might cause immediate
+ * bind, thus taking reset_list_mutex lock via
+ * reset_controller_register().
+ */
+ mutex_unlock(&reset_list_mutex);
+ ret = __reset_add_reset_gpio_device(node, &args);
+ mutex_lock(&reset_list_mutex);
+ if (ret) {
+ rstc = ERR_PTR(ret);
+ goto out;
+ }
+ /*
+ * Success: reset-gpio could probe immediately, so
+ * re-check the lookup.
+ */
+ rcdev = __reset_find_rcdev(&args, gpio_fallback, NULL);
+ if (!rcdev) {
+ rstc = ERR_PTR(-EPROBE_DEFER);
+ goto out;
+ }
+ /* Success, rcdev is valid thus do not bail out */
+ } else {
+ rstc = ERR_PTR(-EPROBE_DEFER);
+ goto out;
+ }
}

if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
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-05 16:01:52

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH v2 3/4] 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-05 16:02:20

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH v2 4/4] 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-05 16:40:14

by Biju Das

[permalink] [raw]
Subject: RE: [PATCH v2 1/4] reset: gpio: Add GPIO-based reset controller

Hi Krzysztof Kozlowski,

Thanks for the patch.

> -----Original Message-----
> From: Krzysztof Kozlowski <[email protected]>
> Sent: Friday, January 5, 2024 3:59 PM
> Subject: [PATCH v2 1/4] 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: 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..cf0a867cbc5f
> --- /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 void reset_gpio_of_args_put(void *data) {
> + of_node_put(data);
> +}
> +
> +static int reset_gpio_probe(struct platform_device *pdev) {
> + struct device *dev = &pdev->dev;
> + struct device_node **platdata = dev_get_platdata(dev);
> + struct of_phandle_args gpio_args;
> + struct reset_gpio_priv *priv;
> + int ret;
> +
> + if (!platdata || !*platdata)

Maybe, if (!(platdata && *platdata)) which reduces 1 inversion operation.

Cheers,
Biju

2024-01-06 15:29:00

by Krzysztof Kozlowski

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

On 05/01/2024 17:39, Biju Das wrote:
> Hi Krzysztof Kozlowski,
>
> Thanks for the patch.
>
>> -----Original Message-----
>> From: Krzysztof Kozlowski <[email protected]>
>> Sent: Friday, January 5, 2024 3:59 PM
>> Subject: [PATCH v2 1/4] 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: 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..cf0a867cbc5f
>> --- /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 void reset_gpio_of_args_put(void *data) {
>> + of_node_put(data);
>> +}
>> +
>> +static int reset_gpio_probe(struct platform_device *pdev) {
>> + struct device *dev = &pdev->dev;
>> + struct device_node **platdata = dev_get_platdata(dev);
>> + struct of_phandle_args gpio_args;
>> + struct reset_gpio_priv *priv;
>> + int ret;
>> +
>> + if (!platdata || !*platdata)
>
> Maybe, if (!(platdata && *platdata)) which reduces 1 inversion operation.

I would not call it easier to understand... To me !A and !*A are quite
obvious and easy to read instantly because !A is obvious: check if it is
not NULL. Therefore original check is obvious: is NULL or points to
NULL? Then exit.

Now your check is a bit more complicated. It is not even frequent code
pattern which my brain used to see. You want to check if both are not
NULL and then negate it, wait, no, opposite, check if they are something
and then negate? To me it is really opposite of readable code.

Best regards,
Krzysztof


2024-01-07 10:46:26

by Biju Das

[permalink] [raw]
Subject: RE: [PATCH v2 1/4] reset: gpio: Add GPIO-based reset controller

Hi Krzysztof Kozlowski,

> -----Original Message-----
> From: Krzysztof Kozlowski <[email protected]>
> Sent: Saturday, January 6, 2024 3:29 PM
> Subject: Re: [PATCH v2 1/4] reset: gpio: Add GPIO-based reset controller
>
> On 05/01/2024 17:39, Biju Das wrote:
> > Hi Krzysztof Kozlowski,
> >
> > Thanks for the patch.
> >
> >> -----Original Message-----
> >> From: Krzysztof Kozlowski <[email protected]>
> >> Sent: Friday, January 5, 2024 3:59 PM
> >> Subject: [PATCH v2 1/4] 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: 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..cf0a867cbc5f
> >> --- /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 void reset_gpio_of_args_put(void *data) {
> >> + of_node_put(data);
> >> +}
> >> +
> >> +static int reset_gpio_probe(struct platform_device *pdev) {
> >> + struct device *dev = &pdev->dev;
> >> + struct device_node **platdata = dev_get_platdata(dev);
> >> + struct of_phandle_args gpio_args;
> >> + struct reset_gpio_priv *priv;
> >> + int ret;
> >> +
> >> + if (!platdata || !*platdata)
> >
> > Maybe, if (!(platdata && *platdata)) which reduces 1 inversion
> operation.
>
> I would not call it easier to understand... To me !A and !*A are quite
> obvious and easy to read instantly because !A is obvious: check if it is
> not NULL. Therefore original check is obvious: is NULL or points to NULL?
> Then exit.
>
> Now your check is a bit more complicated. It is not even frequent code
> pattern which my brain used to see. You want to check if both are not NULL
> and then negate it, wait, no, opposite, check if they are something and
> then negate? To me it is really opposite of readable code.

I agree maybe it is not readable, even though it reduces 1 extra operation.

!(Valid pointer AND points to a non NULL value)
Then exit.

Cheers,
Biju

2024-01-07 11:57:15

by Krzysztof Kozlowski

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

On 07/01/2024 11:46, Biju Das wrote:
>>>> +
>>>> + if (!platdata || !*platdata)
>>>
>>> Maybe, if (!(platdata && *platdata)) which reduces 1 inversion
>> operation.
>>
>> I would not call it easier to understand... To me !A and !*A are quite
>> obvious and easy to read instantly because !A is obvious: check if it is
>> not NULL. Therefore original check is obvious: is NULL or points to NULL?
>> Then exit.
>>
>> Now your check is a bit more complicated. It is not even frequent code
>> pattern which my brain used to see. You want to check if both are not NULL
>> and then negate it, wait, no, opposite, check if they are something and
>> then negate? To me it is really opposite of readable code.
>
> I agree maybe it is not readable, even though it reduces 1 extra operation.
>

Number of operations does not matter. Code readability matters.
Compilers are nowadays smarter than us, so don't write code more
difficult to read just to optimize some instruction like this.

Best regards,
Krzysztof


2024-01-08 09:44:38

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v2 0/4] reset: gpio: ASoC: shared GPIO resets

On 07/01/2024 22:35, Chris Packham wrote:
> Hi Krzysztof,
>
> On 6/01/24 04:59, Krzysztof Kozlowski wrote:
>
> Hi,

You quoting got broken.

>
>
>
> I'll try and take these for a spin on my hardware. I think I'll need to update the pca954x mux driver along similar lines to your changes to the wsa884x. Do you happen to have an example of what the reset-controller usage looks like in a devicetree? I can probably figure it out based on the code but I figured I'd ask just in case you already had an example handy.

Just add "reset-gpios" property in the device node, not the bus.

Best regards,
Krzysztof


2024-01-08 12:18:04

by Philipp Zabel

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

On Fr, 2024-01-05 at 16:59 +0100, Krzysztof Kozlowski 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, which is missing but
^^^^^^^^^^^^^^^^
Nitpick: the "resets" property is missing, not the reset line.

"If Devicetree-based device requests a reset line, but there only is a
reset-gpios property instead of a "resets" property, ..." maybe?

> there is a reset-gpios property, 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].
>
> All newly registered "reset-gpio" platform devices will be stored on
> their own list to avoid any duplicated devices.

That's not strictly true. The reset_gpio_device_list only contains the
of_phandle_args for lookup.

> The key to find each of
> such platform device is the entire Devicetree GPIO specifier: phandle to
> GPIO controller, GPIO number and GPIO flags. If two devices have
> conflicting "reset-gpios" property, e.g. with different ACTIVE_xxx
> flags, this would spawn two separate "reset-gpio" devices, where the
> second would fail probing on busy GPIO reques

request.

Is that true? The code below looks like overwrites of_phandle_args so
that only one reset-gpio device is spawned for each gpio node.

> Link: https://lore.kernel.org/all/[email protected]/ [1]
> Cc: Bartosz Golaszewski <[email protected]>
> Cc: Sean Anderson <[email protected]>
> Signed-off-by: Krzysztof Kozlowski <[email protected]>
> ---
> drivers/reset/core.c | 176 ++++++++++++++++++++++++++++---
> include/linux/reset-controller.h | 4 +
> 2 files changed, 167 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/reset/core.c b/drivers/reset/core.c
> index 4d5a78d3c085..ec9b3ff419cf 100644
> --- a/drivers/reset/core.c
> +++ b/drivers/reset/core.c
> @@ -13,6 +13,7 @@
> #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 +24,10 @@ static LIST_HEAD(reset_controller_list);
> static DEFINE_MUTEX(reset_lookup_mutex);
> static LIST_HEAD(reset_lookup_list);
>
> +/* Protects reset_gpio_device_list */
> +static DEFINE_MUTEX(reset_gpio_device_mutex);
> +static LIST_HEAD(reset_gpio_device_list);

I would call this reset_gpio_lookup_list or
reset_gpio_phandle_args_list.

> +
> /**
> * struct reset_control - a reset control
> * @rcdev: a pointer to the reset controller device
> @@ -63,6 +68,16 @@ struct reset_control_array {
> struct reset_control *rstc[] __counted_by(num_rstcs);
> };
>
> +/**
> + * struct reset_gpio_device - ad-hoc created reset-gpio device
> + * @of_args: phandle to the reset controller with all the args like GPIO number
> + * @list: list entry for the reset_lookup_list
> + */
> +struct reset_gpio_device {

Similarly, I would call this reset_gpio_lookup or
reset_gpio_phandle_args.

> + 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 +828,119 @@ 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])
> + break;

Just return false in the loop and simplify the following to return
true.

> +
> + /* All args matched? */
> + if (i == a1->args_count)
> + return true;
> +
> + return false;
> +}
> +
> +/*
> + * @node: node of the device requesting reset
> + * @reset_args: phandle to the reset controller with all the args like GPIO number
> + */
> +static int __reset_add_reset_gpio_device(struct device_node *node,
> + struct of_phandle_args *args)
> +{
> + struct reset_gpio_device *rgpio_dev;
> + struct platform_device *pdev;
> + int ret;
> +
> + lockdep_assert_not_held(&reset_list_mutex);
> +
> + mutex_lock(&reset_gpio_device_mutex);
> +
> + list_for_each_entry(rgpio_dev, &reset_gpio_device_list, list) {
> + if (args->np == rgpio_dev->of_args.np) {
> + if (__reset_gpios_args_match(args,
> + &rgpio_dev->of_args)) {
> + ret = 0;
> + goto out_unlock;
> + }
> + }
> + }
> +
> + /* Not freed in normal path, persisent subsyst data */
> + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL);

Since this is persistent, instead of letting the reset-gpio driver call
of_parse_phandle_with_args() again, this could be passed in via
platform data. Is there a reason not to do that instead?

> + if (!rgpio_dev) {
> + ret = -ENOMEM;
> + goto out_unlock;
> + }
> +
> + rgpio_dev->of_args = *args;
> + pdev = platform_device_register_data(NULL, "reset-gpio",
> + PLATFORM_DEVID_AUTO, &node,
> + sizeof(node));
> + ret = PTR_ERR_OR_ZERO(pdev);
> + if (!ret)
> + list_add(&rgpio_dev->list, &reset_gpio_device_list);
> + else
> + kfree(rgpio_dev);
> +
> +out_unlock:
> + mutex_unlock(&reset_gpio_device_mutex);
> +
> + return ret;
> +}
> +
> +static struct reset_controller_dev *__reset_find_rcdev(struct of_phandle_args *args,
> + bool gpio_fallback,
> + const void *cookie)

Unused cookie.

> +{
> + 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)) {
> + /*
> + * Fake args (take first reset) and
> + * args_count (to matcg reset-gpio

match

> + * of_reset_n_cells) because reset-gpio
> + * has only one reset and does not care
> + * about reset of GPIO specifier.
> + */
> + args->args[0] = 0;
> + args->args_count = 1;

I'd expect args to be an input-only argument, but here its contents are
overwritten after a match. Why?

This has an effect in __of_reset_control_get(), that I find hard to
follow. See below.

> + 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,21 +960,50 @@ __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.
> + */

I don't understand this comment. The GPIO index should be checked as
part of __reset_gpios_args_match(), or should it not?

> + 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;

Is there a reason not just call __reset_add_reset_gpio_device() here?
With that, there should be no need to call __reset_find_rcdev() twice.

> }
>
> + mutex_lock(&reset_list_mutex);
> + rcdev = __reset_find_rcdev(&args, gpio_fallback, NULL);

This gets called with args as parsed. If there is a match, this will
overwrite args (in the gpio_fallback case) and return NULL.

> +
> if (!rcdev) {
> - rstc = ERR_PTR(-EPROBE_DEFER);
> - goto out;
> + if (gpio_fallback) {
> + /*
> + * Registering reset-gpio device might cause immediate
> + * bind, thus taking reset_list_mutex lock via
> + * reset_controller_register().
> + */
> + mutex_unlock(&reset_list_mutex);
> + ret = __reset_add_reset_gpio_device(node, &args);

So this will also be called with args as parsed.

> + mutex_lock(&reset_list_mutex);
> + if (ret) {
> + rstc = ERR_PTR(ret);
> + goto out;
> + }
> + /*
> + * Success: reset-gpio could probe immediately, so
> + * re-check the lookup.
> + */
> + rcdev = __reset_find_rcdev(&args, gpio_fallback, NULL);

And this will again be called with args as parsed and overwrite args
again.

> + if (!rcdev) {
> + rstc = ERR_PTR(-EPROBE_DEFER);
> + goto out;
> + }
> + /* Success, rcdev is valid thus do not bail out */
> + } else {
> + rstc = ERR_PTR(-EPROBE_DEFER);
> + goto out;
> + }
> }

So at this point args is overwritten in the gpio_fallback case. I would
find it much clearer to just overwrite args here and make the first
parameter to __reset_find_rcdev() const.


regards
Philipp

2024-01-08 12:22:14

by Philipp Zabel

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

On Fr, 2024-01-05 at 16:59 +0100, Krzysztof Kozlowski 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.

I don't know how evil it is to set a parent-less platform device's
of_node to another device's node, but I like the simplicity of a
single-GPIO reset controller driver more that I had expected.

[...]
> diff --git a/drivers/reset/reset-gpio.c b/drivers/reset/reset-gpio.c
> new file mode 100644
> index 000000000000..cf0a867cbc5f
> --- /dev/null
> +++ b/drivers/reset/reset-gpio.c
> @@ -0,0 +1,121 @@
[...]
> +static void reset_gpio_of_args_put(void *data)

This should probably be called reset_gpio_of_node_put().

> +{
> + of_node_put(data);
> +}
[...]

regards
Philipp

2024-01-09 10:59:33

by Krzysztof Kozlowski

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

On 08/01/2024 13:17, Philipp Zabel wrote:
> On Fr, 2024-01-05 at 16:59 +0100, Krzysztof Kozlowski 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, which is missing but
> ^^^^^^^^^^^^^^^^
> Nitpick: the "resets" property is missing, not the reset line.
>
> "If Devicetree-based device requests a reset line, but there only is a
> reset-gpios property instead of a "resets" property, ..." maybe?

Ack

>
>> there is a reset-gpios property, 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].
>>
>> All newly registered "reset-gpio" platform devices will be stored on
>> their own list to avoid any duplicated devices.
>
> That's not strictly true. The reset_gpio_device_list only contains the
> of_phandle_args for lookup.

Ack, I will re-phrase it.

>
>> The key to find each of
>> such platform device is the entire Devicetree GPIO specifier: phandle to
>> GPIO controller, GPIO number and GPIO flags. If two devices have
>> conflicting "reset-gpios" property, e.g. with different ACTIVE_xxx
>> flags, this would spawn two separate "reset-gpio" devices, where the
>> second would fail probing on busy GPIO reques
>
> request.

Ack

>
> Is that true?

It should be true and my tests confirmed it.

The code below looks like overwrites of_phandle_args so
> that only one reset-gpio device is spawned for each gpio node.

The code will iterate over list of of_node and of_phandle_args and
compare them with __reset_gpios_args_match(). If all match: do not
create new platform device.

If they do not match, e.g. ACTIVE_LOW -> ACTIVE_HIGH, create new
platform device. This will be the second device for the same GPIO.
Probing of that device in reset-gpio driver will fail:

[ 19.198775] reset-gpio reset-gpio.2.auto: error -EBUSY: Could not get
reset gpios

because GPIO is used by reset-gpio.1.auto already.

>
>> Link: https://lore.kernel.org/all/[email protected]/ [1]
>> Cc: Bartosz Golaszewski <[email protected]>
>> Cc: Sean Anderson <[email protected]>
>> Signed-off-by: Krzysztof Kozlowski <[email protected]>
>> ---
>> drivers/reset/core.c | 176 ++++++++++++++++++++++++++++---
>> include/linux/reset-controller.h | 4 +
>> 2 files changed, 167 insertions(+), 13 deletions(-)
>>
>> diff --git a/drivers/reset/core.c b/drivers/reset/core.c
>> index 4d5a78d3c085..ec9b3ff419cf 100644
>> --- a/drivers/reset/core.c
>> +++ b/drivers/reset/core.c
>> @@ -13,6 +13,7 @@
>> #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 +24,10 @@ static LIST_HEAD(reset_controller_list);
>> static DEFINE_MUTEX(reset_lookup_mutex);
>> static LIST_HEAD(reset_lookup_list);
>>
>> +/* Protects reset_gpio_device_list */
>> +static DEFINE_MUTEX(reset_gpio_device_mutex);
>> +static LIST_HEAD(reset_gpio_device_list);
>
> I would call this reset_gpio_lookup_list or
> reset_gpio_phandle_args_list.

Ack

>
>> +
>> /**
>> * struct reset_control - a reset control
>> * @rcdev: a pointer to the reset controller device
>> @@ -63,6 +68,16 @@ struct reset_control_array {
>> struct reset_control *rstc[] __counted_by(num_rstcs);
>> };
>>
>> +/**
>> + * struct reset_gpio_device - ad-hoc created reset-gpio device
>> + * @of_args: phandle to the reset controller with all the args like GPIO number
>> + * @list: list entry for the reset_lookup_list
>> + */
>> +struct reset_gpio_device {
>
> Similarly, I would call this reset_gpio_lookup or
> reset_gpio_phandle_args.

Ack

>
>> + 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 +828,119 @@ 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])
>> + break;
>
> Just return false in the loop and simplify the following to return
> true.

Ack
>
>> +
>> + /* All args matched? */
>> + if (i == a1->args_count)
>> + return true;
>> +
>> + return false;
>> +}
>> +
>> +/*
>> + * @node: node of the device requesting reset
>> + * @reset_args: phandle to the reset controller with all the args like GPIO number
>> + */
>> +static int __reset_add_reset_gpio_device(struct device_node *node,
>> + struct of_phandle_args *args)
>> +{
>> + struct reset_gpio_device *rgpio_dev;
>> + struct platform_device *pdev;
>> + int ret;
>> +
>> + lockdep_assert_not_held(&reset_list_mutex);
>> +
>> + mutex_lock(&reset_gpio_device_mutex);
>> +
>> + list_for_each_entry(rgpio_dev, &reset_gpio_device_list, list) {
>> + if (args->np == rgpio_dev->of_args.np) {
>> + if (__reset_gpios_args_match(args,
>> + &rgpio_dev->of_args)) {
>> + ret = 0;
>> + goto out_unlock;
>> + }
>> + }
>> + }
>> +
>> + /* Not freed in normal path, persisent subsyst data */
>> + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL);
>
> Since this is persistent, instead of letting the reset-gpio driver call
> of_parse_phandle_with_args() again, this could be passed in via
> platform data. Is there a reason not to do that instead?

We can pass it as read only platform data, but we cannot pass the
ownership. This is associated with registered platform device, not with
bound one device->driver.

Imagine case:
1. modprobe reset-gpio,
2. Driver is bound to the device,
3. unbind (echo > unbind)
4. rmmod
5. goto 1

>
>> + if (!rgpio_dev) {
>> + ret = -ENOMEM;
>> + goto out_unlock;
>> + }
>> +
>> + rgpio_dev->of_args = *args;
>> + pdev = platform_device_register_data(NULL, "reset-gpio",
>> + PLATFORM_DEVID_AUTO, &node,
>> + sizeof(node));
>> + ret = PTR_ERR_OR_ZERO(pdev);
>> + if (!ret)
>> + list_add(&rgpio_dev->list, &reset_gpio_device_list);
>> + else
>> + kfree(rgpio_dev);
>> +
>> +out_unlock:
>> + mutex_unlock(&reset_gpio_device_mutex);
>> +
>> + return ret;
>> +}
>> +
>> +static struct reset_controller_dev *__reset_find_rcdev(struct of_phandle_args *args,
>> + bool gpio_fallback,
>> + const void *cookie)
>
> Unused cookie.

Ack
>
>> +{
>> + 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)) {
>> + /*
>> + * Fake args (take first reset) and
>> + * args_count (to matcg reset-gpio
>
> match

Ack
>
>> + * of_reset_n_cells) because reset-gpio
>> + * has only one reset and does not care
>> + * about reset of GPIO specifier.
>> + */
>> + args->args[0] = 0;
>> + args->args_count = 1;
>
> I'd expect args to be an input-only argument, but here its contents are
> overwritten after a match. Why?
>
> This has an effect in __of_reset_control_get(), that I find hard to
> follow. See below.
>
>> + 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,21 +960,50 @@ __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.
>> + */
>
> I don't understand this comment. The GPIO index should be checked as
> part of __reset_gpios_args_match(), or should it not?

This and earlier comment are result of a bit hacky approach to the
problem: how to find reset controllers for that GPIO?

The point is that our reset gpio controller has only 1 reset, thus
of_reset_n_cells=1. However args_count from of_parse_handle is >0, which
later is compared in reset core:

https://elixir.bootlin.com/linux/latest/source/drivers/reset/core.c#L859

That part we need to match.

I could make the reset-gpio driver to have of_reset_n_cells=2, but what
would be the point? The rest of the cells are not really relevant,
because you cannot refer to this reset gpio controller with any other
arguments.

To remind: my solution spawns one reset-gpio controller for one GPIO.

>
>> + 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;
>
> Is there a reason not just call __reset_add_reset_gpio_device() here?
> With that, there should be no need to call __reset_find_rcdev() twice.

Hm, could be, although not sure if code would be simpler.

This entire function handles two cases:
1. Get normal reset controller ("resets" OF property),
2. If above fails, get reset-gpio controller ("reset-gpios" OF property)

Therefore the entire solution is following approach:
1. of_parse_phandle(resets)
1b. error? Then of_parse_phandle(reset-gpios)
2. Find reset-controller based on any of above phandles.
3. error? Check if we created reset-gpios platform device. If not:
create new reset-gpios platform device/
3b. Platform device could probe, so lookup again for reset controller or
defer probe.

What type of flow do you propose?

>
>> }
>>
>> + mutex_lock(&reset_list_mutex);
>> + rcdev = __reset_find_rcdev(&args, gpio_fallback, NULL);
>
> This gets called with args as parsed. If there is a match, this will
> overwrite args (in the gpio_fallback case) and return NULL.

Overwrite not complete. It will only overwrite args_count and return a
valid rcdev.
I do not see overwriting in case of returning NULL.

>
>> +
>> if (!rcdev) {
>> - rstc = ERR_PTR(-EPROBE_DEFER);
>> - goto out;
>> + if (gpio_fallback) {
>> + /*
>> + * Registering reset-gpio device might cause immediate
>> + * bind, thus taking reset_list_mutex lock via
>> + * reset_controller_register().
>> + */
>> + mutex_unlock(&reset_list_mutex);
>> + ret = __reset_add_reset_gpio_device(node, &args);
>
> So this will also be called with args as parsed.
>
>> + mutex_lock(&reset_list_mutex);
>> + if (ret) {
>> + rstc = ERR_PTR(ret);
>> + goto out;
>> + }
>> + /*
>> + * Success: reset-gpio could probe immediately, so
>> + * re-check the lookup.
>> + */
>> + rcdev = __reset_find_rcdev(&args, gpio_fallback, NULL);
>
> And this will again be called with args as parsed and overwrite args
> again.>
>> + if (!rcdev) {
>> + rstc = ERR_PTR(-EPROBE_DEFER);
>> + goto out;
>> + }
>> + /* Success, rcdev is valid thus do not bail out */
>> + } else {
>> + rstc = ERR_PTR(-EPROBE_DEFER);
>> + goto out;
>> + }
>> }
>
> So at this point args is overwritten in the gpio_fallback case. I would
> find it much clearer to just overwrite args here and make the first
> parameter to __reset_find_rcdev() const.

I think I get your point. Overwriting happens after we store the
original of_args, but the code is indeed not intuitive. I think I can
move it further, as you suggested.


Best regards,
Krzysztof


2024-01-09 11:00:35

by Krzysztof Kozlowski

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

On 08/01/2024 13:21, Philipp Zabel wrote:
> On Fr, 2024-01-05 at 16:59 +0100, Krzysztof Kozlowski 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.
>
> I don't know how evil it is to set a parent-less platform device's
> of_node to another device's node, but I like the simplicity of a
> single-GPIO reset controller driver more that I had expected.
>
> [...]
>> diff --git a/drivers/reset/reset-gpio.c b/drivers/reset/reset-gpio.c
>> new file mode 100644
>> index 000000000000..cf0a867cbc5f
>> --- /dev/null
>> +++ b/drivers/reset/reset-gpio.c
>> @@ -0,0 +1,121 @@
> [...]
>> +static void reset_gpio_of_args_put(void *data)
>
> This should probably be called reset_gpio_of_node_put().

Ack

Best regards,
Krzysztof


2024-01-09 11:59:24

by Philipp Zabel

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

On Di, 2024-01-09 at 11:59 +0100, Krzysztof Kozlowski wrote:
> On 08/01/2024 13:17, Philipp Zabel wrote:
> > On Fr, 2024-01-05 at 16:59 +0100, Krzysztof Kozlowski wrote:
> > Is that true?
>
> It should be true and my tests confirmed it.
>
> The code below looks like overwrites of_phandle_args so
> > that only one reset-gpio device is spawned for each gpio node.
>
> The code will iterate over list of of_node and of_phandle_args and
> compare them with __reset_gpios_args_match(). If all match: do not
> create new platform device.
>
> If they do not match, e.g. ACTIVE_LOW -> ACTIVE_HIGH, create new
> platform device. This will be the second device for the same GPIO.
> Probing of that device in reset-gpio driver will fail:
>
> [ 19.198775] reset-gpio reset-gpio.2.auto: error -EBUSY: Could not get
> reset gpios
>
> because GPIO is used by reset-gpio.1.auto already.

Thank you for the clarification.
I only understood later in the mail and didn't update this properly.

> > > + /* Not freed in normal path, persisent subsyst data */
> > > + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL);
> >
> > Since this is persistent, instead of letting the reset-gpio driver call
> > of_parse_phandle_with_args() again, this could be passed in via
> > platform data. Is there a reason not to do that instead?
>
> We can pass it as read only platform data, but we cannot pass the
> ownership. This is associated with registered platform device, not with
> bound one device->driver.
>
> Imagine case:
> 1. modprobe reset-gpio,
> 2. Driver is bound to the device,
> 3. unbind (echo > unbind)
> 4. rmmod
> 5. goto 1

Keeping ownership on the list is fine, the reset-gpio driver makes its
own copy of of_phandle_args anyway. I was just wondering whether it
could make this copy from platform data instead of from the
of_parse_phandle_with_args() return value.

[...]
> >
> > > @@ -839,21 +960,50 @@ __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.
> > > + */
> >
> > I don't understand this comment. The GPIO index should be checked as
> > part of __reset_gpios_args_match(), or should it not?
>
> This and earlier comment are result of a bit hacky approach to the
> problem: how to find reset controllers for that GPIO?
>
> The point is that our reset gpio controller has only 1 reset, thus
> of_reset_n_cells=1. However args_count from of_parse_handle is >0, which
> later is compared in reset core:
>
> https://elixir.bootlin.com/linux/latest/source/drivers/reset/core.c#L859
>
> That part we need to match.
>
> I could make the reset-gpio driver to have of_reset_n_cells=2, but what
> would be the point? The rest of the cells are not really relevant,
> because you cannot refer to this reset gpio controller with any other
> arguments.
>
> To remind: my solution spawns one reset-gpio controller for one GPIO.

Thank you. I think we could also just make that check

if (WARN_ON(!rcdev->of_args && ...))

instead and skip the of_xlate call in that case (or implement of_xlate
in the reset-gpio driver to make this more explicit).

> >
> > > + 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;
> >
> > Is there a reason not just call __reset_add_reset_gpio_device() here?
> > With that, there should be no need to call __reset_find_rcdev() twice.
>
> Hm, could be, although not sure if code would be simpler.
>
> This entire function handles two cases:
> 1. Get normal reset controller ("resets" OF property),
> 2. If above fails, get reset-gpio controller ("reset-gpios" OF property)
>
> Therefore the entire solution is following approach:
> 1. of_parse_phandle(resets)
> 1b. error? Then of_parse_phandle(reset-gpios)
> 2. Find reset-controller based on any of above phandles.
> 3. error? Check if we created reset-gpios platform device. If not:
> create new reset-gpios platform device/
> 3b. Platform device could probe, so lookup again for reset controller or
> defer probe.
>
> What type of flow do you propose?

I propose to reorder after parsing the phandles: check/create the gpio
platform device right after parsing the gpio handle. Only then lock
reset_list_mutex look for the rcdev.

1. of_parse_phandle(resets)
1b. error? Then of_parse_phandle(reset-gpios)
2b. gpio? Then check if we created reset-gpios platform device. If not:
create new reset-gpios platform device/, defer if probe failed
3. Lock reset_list_mutex, find reset-controller based on any of above
phandles.

>
> >
> > > }
> > >
> > > + mutex_lock(&reset_list_mutex);
> > > + rcdev = __reset_find_rcdev(&args, gpio_fallback, NULL);
> >
> > This gets called with args as parsed. If there is a match, this will
> > overwrite args (in the gpio_fallback case) and return NULL.
>
> Overwrite not complete. It will only overwrite args_count and return a
> valid rcdev.
> I do not see overwriting in case of returning NULL.

Sorry, I meant to write

"This gets called with args as parsed. If there is a match, this will
overwrite args (in the gpio_fallback case) _or_ return NULL."

at least at the end, when I understood the following.

> >
> > > +
> > > if (!rcdev) {

So in this non-NULL branch there was no overwriting.

> > > - rstc = ERR_PTR(-EPROBE_DEFER);
> > > - goto out;
> > > + if (gpio_fallback) {
> > > + /*
> > > + * Registering reset-gpio device might cause immediate
> > > + * bind, thus taking reset_list_mutex lock via
> > > + * reset_controller_register().
> > > + */
> > > + mutex_unlock(&reset_list_mutex);
> > > + ret = __reset_add_reset_gpio_device(node, &args);
> >
> > So this will also be called with args as parsed.
> >
> > > + mutex_lock(&reset_list_mutex);
> > > + if (ret) {
> > > + rstc = ERR_PTR(ret);
> > > + goto out;
> > > + }
> > > + /*
> > > + * Success: reset-gpio could probe immediately, so
> > > + * re-check the lookup.
> > > + */
> > > + rcdev = __reset_find_rcdev(&args, gpio_fallback, NULL);
> >
> > And this will again be called with args as parsed and overwrite args
> > again.>
> > > + if (!rcdev) {
> > > + rstc = ERR_PTR(-EPROBE_DEFER);
> > > + goto out;
> > > + }
> > > + /* Success, rcdev is valid thus do not bail out */
> > > + } else {
> > > + rstc = ERR_PTR(-EPROBE_DEFER);
> > > + goto out;
> > > + }
> > > }
> >
> > So at this point args is overwritten in the gpio_fallback case. I would
> > find it much clearer to just overwrite args here and make the first
> > parameter to __reset_find_rcdev() const.
>
> I think I get your point. Overwriting happens after we store the
> original of_args, but the code is indeed not intuitive. I think I can
> move it further, as you suggested.

Now I think we can skip the overwriting altogether and just adapt the
following of_reset_n_cells check ad of_xlate call as mentioned above.

regards
Philipp

2024-01-11 09:48:28

by Krzysztof Kozlowski

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

On 09/01/2024 12:58, Philipp Zabel wrote:
>>>> + /* Not freed in normal path, persisent subsyst data */
>>>> + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL);
>>>
>>> Since this is persistent, instead of letting the reset-gpio driver call
>>> of_parse_phandle_with_args() again, this could be passed in via
>>> platform data. Is there a reason not to do that instead?
>>
>> We can pass it as read only platform data, but we cannot pass the
>> ownership. This is associated with registered platform device, not with
>> bound one device->driver.
>>
>> Imagine case:
>> 1. modprobe reset-gpio,
>> 2. Driver is bound to the device,
>> 3. unbind (echo > unbind)
>> 4. rmmod
>> 5. goto 1
>
> Keeping ownership on the list is fine, the reset-gpio driver makes its
> own copy of of_phandle_args anyway. I was just wondering whether it
> could make this copy from platform data instead of from the
> of_parse_phandle_with_args() return value.

Looks like it could. This could save us few lines of code in
reset-gpio.c. I'll try it.

>
> [...]
>>>
>>>> @@ -839,21 +960,50 @@ __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.
>>>> + */
>>>
>>> I don't understand this comment. The GPIO index should be checked as
>>> part of __reset_gpios_args_match(), or should it not?
>>
>> This and earlier comment are result of a bit hacky approach to the
>> problem: how to find reset controllers for that GPIO?
>>
>> The point is that our reset gpio controller has only 1 reset, thus
>> of_reset_n_cells=1. However args_count from of_parse_handle is >0, which
>> later is compared in reset core:
>>
>> https://elixir.bootlin.com/linux/latest/source/drivers/reset/core.c#L859
>>
>> That part we need to match.
>>
>> I could make the reset-gpio driver to have of_reset_n_cells=2, but what
>> would be the point? The rest of the cells are not really relevant,
>> because you cannot refer to this reset gpio controller with any other
>> arguments.
>>
>> To remind: my solution spawns one reset-gpio controller for one GPIO.
>
> Thank you. I think we could also just make that check
>
> if (WARN_ON(!rcdev->of_args && ...))
>
> instead and skip the of_xlate call in that case (or implement of_xlate
> in the reset-gpio driver to make this more explicit).

Ack

>
>>>
>>>> + 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;
>>>
>>> Is there a reason not just call __reset_add_reset_gpio_device() here?
>>> With that, there should be no need to call __reset_find_rcdev() twice.
>>
>> Hm, could be, although not sure if code would be simpler.
>>
>> This entire function handles two cases:
>> 1. Get normal reset controller ("resets" OF property),
>> 2. If above fails, get reset-gpio controller ("reset-gpios" OF property)
>>
>> Therefore the entire solution is following approach:
>> 1. of_parse_phandle(resets)
>> 1b. error? Then of_parse_phandle(reset-gpios)
>> 2. Find reset-controller based on any of above phandles.
>> 3. error? Check if we created reset-gpios platform device. If not:
>> create new reset-gpios platform device/
>> 3b. Platform device could probe, so lookup again for reset controller or
>> defer probe.
>>
>> What type of flow do you propose?
>
> I propose to reorder after parsing the phandles: check/create the gpio
> platform device right after parsing the gpio handle. Only then lock
> reset_list_mutex look for the rcdev.
>
> 1. of_parse_phandle(resets)
> 1b. error? Then of_parse_phandle(reset-gpios)
> 2b. gpio? Then check if we created reset-gpios platform device. If not:
> create new reset-gpios platform device/, defer if probe failed
> 3. Lock reset_list_mutex, find reset-controller based on any of above
> phandles.

Could work, let me try. I have impression this was my first approach
which resulted in a bit more complicated code, but I don't remember the
details now.

>
>>
>>>
>>>> }
>>>>
>>>> + mutex_lock(&reset_list_mutex);
>>>> + rcdev = __reset_find_rcdev(&args, gpio_fallback, NULL);
>>>
>>> This gets called with args as parsed. If there is a match, this will
>>> overwrite args (in the gpio_fallback case) and return NULL.
>>
>> Overwrite not complete. It will only overwrite args_count and return a
>> valid rcdev.
>> I do not see overwriting in case of returning NULL.
>
> Sorry, I meant to write
>
> "This gets called with args as parsed. If there is a match, this will
> overwrite args (in the gpio_fallback case) _or_ return NULL."
>
> at least at the end, when I understood the following.
>
>>>
>>>> +
>>>> if (!rcdev) {
>
> So in this non-NULL branch there was no overwriting.
>
>>>> - rstc = ERR_PTR(-EPROBE_DEFER);
>>>> - goto out;
>>>> + if (gpio_fallback) {
>>>> + /*
>>>> + * Registering reset-gpio device might cause immediate
>>>> + * bind, thus taking reset_list_mutex lock via
>>>> + * reset_controller_register().
>>>> + */
>>>> + mutex_unlock(&reset_list_mutex);
>>>> + ret = __reset_add_reset_gpio_device(node, &args);
>>>
>>> So this will also be called with args as parsed.
>>>
>>>> + mutex_lock(&reset_list_mutex);
>>>> + if (ret) {
>>>> + rstc = ERR_PTR(ret);
>>>> + goto out;
>>>> + }
>>>> + /*
>>>> + * Success: reset-gpio could probe immediately, so
>>>> + * re-check the lookup.
>>>> + */
>>>> + rcdev = __reset_find_rcdev(&args, gpio_fallback, NULL);
>>>
>>> And this will again be called with args as parsed and overwrite args
>>> again.>
>>>> + if (!rcdev) {
>>>> + rstc = ERR_PTR(-EPROBE_DEFER);
>>>> + goto out;
>>>> + }
>>>> + /* Success, rcdev is valid thus do not bail out */
>>>> + } else {
>>>> + rstc = ERR_PTR(-EPROBE_DEFER);
>>>> + goto out;
>>>> + }
>>>> }
>>>
>>> So at this point args is overwritten in the gpio_fallback case. I would
>>> find it much clearer to just overwrite args here and make the first
>>> parameter to __reset_find_rcdev() const.
>>
>> I think I get your point. Overwriting happens after we store the
>> original of_args, but the code is indeed not intuitive. I think I can
>> move it further, as you suggested.
>
> Now I think we can skip the overwriting altogether and just adapt the
> following of_reset_n_cells check ad of_xlate call as mentioned above.


Yep!

Best regards,
Krzysztof