[PATCH v6 0/2] gpio: gpio-cascade: add generic GPIO cascade

From: Mauri Sandberg
Date: Tue Oct 19 2021 - 09:00:48 EST


Hello all,

It's been a while and here comes the sixth version of the patches.

What was done:
- adjust module dependecies and what it selects in Kconfig
- use managed device resource call when adding gpio chip
- remove redundant code
- cosmetic changes

Thanks,
Mauri

rangediff v5 -> v6:
1: a1bb23f56378 ! 1: 87348f391671 dt-bindings: gpio-cascade: add documentation
@@ Commit message
the cascaded line.

Signed-off-by: Mauri Sandberg <maukka@xxxxxxxxxxxx>
+ Reviewed-by: Rob Herring <robh@xxxxxxxxxx>
---
+ v5 -> v6:
+ - added Reviewed-by tag by Rob
v4 -> v5:
- renamed gpio-mux-input -> gpio-cascade
- changed vague term 'pin' to 'upstream line'
2: e0be9df088a9 ! 2: a8e608181bc7 gpio: gpio-cascade: add generic GPIO cascade
@@ Metadata
## Commit message ##
gpio: gpio-cascade: add generic GPIO cascade

- Adds support for a building cascades of GPIO lines. That is, it allows
+ Adds support for building cascades of GPIO lines. That is, it allows
setups when there is one upstream line and multiple cascaded lines, out
of which one can be chosen at a time. The status of the upstream line
- can be conveyd to the selected cascaded line or, vice versa, the status
+ can be conveyed to the selected cascaded line or, vice versa, the status
of the cascaded line can be conveyed to the upstream line.

- A gpio-mux is being used to select, which cascaded GPIO line is being
+ A multiplexer is being used to select, which cascaded GPIO line is being
used at any given time.

At the moment only input direction is supported. In future it should be
@@ Commit message

Signed-off-by: Mauri Sandberg <maukka@xxxxxxxxxxxx>
---
+ v5 -> v6:
+ - In Kconfig, remove dependency to OF_GPIO and select only MULTIPLEXER
+ - refactor code preferring one-liners
+ - clean up prints, removing them from success-path.
+ - don't explicitly set gpio_chip.of_node as it's done in the GPIO library
+ - use devm_gpiochip_add_data instead of gpiochip_add
v4 -> v5:
- renamed gpio-mux-input -> gpio-cascade. refactored code accordingly
here and there and changed to use new bindings and compatible string
@@ drivers/gpio/Kconfig: config GPIO_VIRTIO
+
+config GPIO_CASCADE
+ tristate "General GPIO cascade"
-+ depends on OF_GPIO
+ select MULTIPLEXER
-+ select MUX_GPIO
+ help
+ Say yes here to enable support for generic GPIO cascade.
+
@@ drivers/gpio/gpio-cascade.c (new)
+
+static int gpio_cascade_get_value(struct gpio_chip *gc, unsigned int offset)
+{
-+ struct gpio_cascade *cas;
++ struct gpio_cascade *cas = chip_to_cascade(gc);
+ int ret;
+
-+ cas = chip_to_cascade(gc);
+ ret = mux_control_select(cas->mux_control, offset);
+ if (ret)
+ return ret;
@@ drivers/gpio/gpio-cascade.c (new)
+
+static int gpio_cascade_probe(struct platform_device *pdev)
+{
-+ struct device_node *np = pdev->dev.of_node;
+ struct device *dev = &pdev->dev;
+ struct gpio_cascade *cas;
+ struct mux_control *mc;
@@ drivers/gpio/gpio-cascade.c (new)
+ struct gpio_chip *gc;
+ int err;
+
-+ cas = devm_kzalloc(dev, sizeof(struct gpio_cascade), GFP_KERNEL);
-+ if (cas == NULL)
++ cas = devm_kzalloc(dev, sizeof(*cas), GFP_KERNEL);
++ if (!cas)
+ return -ENOMEM;
+
+ mc = devm_mux_control_get(dev, NULL);
-+ if (IS_ERR(mc)) {
-+ err = (int) PTR_ERR(mc);
-+ if (err != -EPROBE_DEFER)
-+ dev_err(dev, "unable to get mux-control: %d\n", err);
-+ return err;
-+ }
++ if (IS_ERR(mc))
++ return dev_err_probe(dev,
++ PTR_ERR(mc),
++ "unable to get mux-control\n");
+
+ cas->mux_control = mc;
+ upstream = devm_gpiod_get(dev, "upstream", GPIOD_IN);
+ if (IS_ERR(upstream)) {
-+ err = (int) PTR_ERR(upstream);
-+ dev_err(dev, "unable to claim upstream GPIO line: %d\n", err);
-+ return err;
++ dev_err(dev, "unable to claim upstream GPIO line\n");
++ return -ENODEV;
+ }
+
+ cas->upstream_line = upstream;
@@ drivers/gpio/gpio-cascade.c (new)
+ gc->label = dev_name(cas->parent);
+ gc->parent = cas->parent;
+ gc->owner = THIS_MODULE;
-+ gc->of_node = np;
+
-+ err = gpiochip_add(&cas->gpio_chip);
++ err = devm_gpiochip_add_data(dev, &cas->gpio_chip, NULL);
+ if (err) {
-+ dev_err(dev, "unable to add gpio chip, err=%d\n", err);
++ dev_err(dev, "unable to add gpio chip\n");
+ return err;
+ }
+
+ platform_set_drvdata(pdev, cas);
-+ dev_info(dev, "registered %u cascaded GPIO lines\n", gc->ngpio);
+ return 0;
+}
+
+static const struct of_device_id gpio_cascade_id[] = {
-+ {
-+ .compatible = "gpio-cascade",
-+ .data = NULL,
-+ },
++ { .compatible = "gpio-cascade" },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, gpio_cascade_id);

Mauri Sandberg (2):
dt-bindings: gpio-cascade: add documentation
gpio: gpio-cascade: add generic GPIO cascade

.../bindings/gpio/gpio-cascade.yaml | 103 ++++++++++++++
drivers/gpio/Kconfig | 12 ++
drivers/gpio/Makefile | 1 +
drivers/gpio/gpio-cascade.c | 129 ++++++++++++++++++
4 files changed, 245 insertions(+)
create mode 100644 Documentation/devicetree/bindings/gpio/gpio-cascade.yaml
create mode 100644 drivers/gpio/gpio-cascade.c


base-commit: f4a20dfac88c06c9b529a41ff4cf9acba8f3fdff
--
2.25.1