ath25: switch default kernel to 5.15
[openwrt/openwrt.git] / target / linux / generic / pending-5.10 / 801-gpio-gpio-cascade-add-generic-GPIO-cascade.patch
1 From fc23ea48ba52c24f201fe5ca0132ee1a3de5a70a Mon Sep 17 00:00:00 2001
2 From: Mauri Sandberg <maukka@ext.kapsi.fi>
3 Date: Thu, 25 Mar 2021 11:48:05 +0200
4 Subject: [PATCH 2/2] gpio: gpio-cascade: add generic GPIO cascade
5
6 Adds support for building cascades of GPIO lines. That is, it allows
7 setups when there is one upstream line and multiple cascaded lines, out
8 of which one can be chosen at a time. The status of the upstream line
9 can be conveyed to the selected cascaded line or, vice versa, the status
10 of the cascaded line can be conveyed to the upstream line.
11
12 A multiplexer is being used to select, which cascaded GPIO line is being
13 used at any given time.
14
15 At the moment only input direction is supported. In future it should be
16 possible to add support for output direction, too.
17
18 Signed-off-by: Mauri Sandberg <maukka@ext.kapsi.fi>
19 Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
20 Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
21 ---
22 v7 -> v8:
23 - rearrange members in struct gpio_cascade
24 - cosmetic changes in file header and in one function declaration
25 - added Reviewed-by tags by Linus and Andy
26 v6 -> v7:
27 - In Kconfig add info about module name
28 - adhere to new convention that allows lines longer than 80 chars
29 - use dev_probe_err with upstream gpio line too
30 - refactor for cleaner exit of probe function.
31 v5 -> v6:
32 - In Kconfig, remove dependency to OF_GPIO and select only MULTIPLEXER
33 - refactor code preferring one-liners
34 - clean up prints, removing them from success-path.
35 - don't explicitly set gpio_chip.of_node as it's done in the GPIO library
36 - use devm_gpiochip_add_data instead of gpiochip_add
37 v4 -> v5:
38 - renamed gpio-mux-input -> gpio-cascade. refactored code accordingly
39 here and there and changed to use new bindings and compatible string
40 - ambigious and vague 'pin' was rename to 'upstream_line'
41 - dropped Tested-by and Reviewed-by due to changes in bindings
42 - dropped Reported-by suggested by an automatic bot as it was not really
43 appropriate to begin with
44 - functionally it's the same as v4
45 v3 -> v4:
46 - Changed author email
47 - Included Tested-by and Reviewed-by from Drew
48 v2 -> v3:
49 - use managed device resources
50 - update Kconfig description
51 v1 -> v2:
52 - removed .owner from platform_driver as per test bot's instruction
53 - added MODULE_AUTHOR, MODULE_DESCRIPTION, MODULE_LICENSE
54 - added gpio_mux_input_get_direction as it's recommended for all chips
55 - removed because this is input only chip: gpio_mux_input_set_value
56 - removed because they are not needed for input/output only chips:
57 gpio_mux_input_direction_input
58 gpio_mux_input_direction_output
59 - fixed typo in an error message
60 - added info message about successful registration
61 - removed can_sleep flag as this does not sleep while getting GPIO value
62 like I2C or SPI do
63 - Updated description in Kconfig
64 ---
65 drivers/gpio/Kconfig | 15 +++++
66 drivers/gpio/Makefile | 1 +
67 drivers/gpio/gpio-cascade.c | 117 ++++++++++++++++++++++++++++++++++++
68 3 files changed, 133 insertions(+)
69 create mode 100644 drivers/gpio/gpio-cascade.c
70
71 --- a/drivers/gpio/Kconfig
72 +++ b/drivers/gpio/Kconfig
73 @@ -1617,4 +1617,19 @@ config GPIO_MOCKUP
74 tools/testing/selftests/gpio/gpio-mockup.sh. Reference the usage in
75 it.
76
77 +comment "Other GPIO expanders"
78 +
79 +config GPIO_CASCADE
80 + tristate "General GPIO cascade"
81 + select MULTIPLEXER
82 + help
83 + Say yes here to enable support for generic GPIO cascade.
84 +
85 + This allows building one-to-many cascades of GPIO lines using
86 + different types of multiplexers readily available. At the
87 + moment only input lines are supported.
88 +
89 + To build the driver as a module choose 'm' and the resulting module
90 + will be called 'gpio-cascade'.
91 +
92 endif
93 --- a/drivers/gpio/Makefile
94 +++ b/drivers/gpio/Makefile
95 @@ -44,6 +44,7 @@ obj-$(CONFIG_GPIO_BD9571MWV) += gpio-bd
96 obj-$(CONFIG_GPIO_BRCMSTB) += gpio-brcmstb.o
97 obj-$(CONFIG_GPIO_BT8XX) += gpio-bt8xx.o
98 obj-$(CONFIG_GPIO_CADENCE) += gpio-cadence.o
99 +obj-$(CONFIG_GPIO_CASCADE) += gpio-cascade.o
100 obj-$(CONFIG_GPIO_CLPS711X) += gpio-clps711x.o
101 obj-$(CONFIG_GPIO_SNPS_CREG) += gpio-creg-snps.o
102 obj-$(CONFIG_GPIO_CRYSTAL_COVE) += gpio-crystalcove.o
103 --- /dev/null
104 +++ b/drivers/gpio/gpio-cascade.c
105 @@ -0,0 +1,117 @@
106 +// SPDX-License-Identifier: GPL-2.0-only
107 +/*
108 + * A generic GPIO cascade driver
109 + *
110 + * Copyright (C) 2021 Mauri Sandberg <maukka@ext.kapsi.fi>
111 + *
112 + * This allows building cascades of GPIO lines in a manner illustrated
113 + * below:
114 + *
115 + * /|---- Cascaded GPIO line 0
116 + * Upstream | |---- Cascaded GPIO line 1
117 + * GPIO line ----+ | .
118 + * | | .
119 + * \|---- Cascaded GPIO line n
120 + *
121 + * A multiplexer is being used to select, which cascaded line is being
122 + * addressed at any given time.
123 + *
124 + * At the moment only input mode is supported due to lack of means for
125 + * testing output functionality. At least theoretically output should be
126 + * possible with open drain constructions.
127 + */
128 +
129 +#include <linux/module.h>
130 +#include <linux/slab.h>
131 +#include <linux/platform_device.h>
132 +#include <linux/mux/consumer.h>
133 +
134 +#include <linux/gpio/consumer.h>
135 +#include <linux/gpio/driver.h>
136 +
137 +struct gpio_cascade {
138 + struct gpio_chip gpio_chip;
139 + struct device *parent;
140 + struct mux_control *mux_control;
141 + struct gpio_desc *upstream_line;
142 +};
143 +
144 +static struct gpio_cascade *chip_to_cascade(struct gpio_chip *gc)
145 +{
146 + return container_of(gc, struct gpio_cascade, gpio_chip);
147 +}
148 +
149 +static int gpio_cascade_get_direction(struct gpio_chip *gc, unsigned int offset)
150 +{
151 + return GPIO_LINE_DIRECTION_IN;
152 +}
153 +
154 +static int gpio_cascade_get_value(struct gpio_chip *gc, unsigned int offset)
155 +{
156 + struct gpio_cascade *cas = chip_to_cascade(gc);
157 + int ret;
158 +
159 + ret = mux_control_select(cas->mux_control, offset);
160 + if (ret)
161 + return ret;
162 +
163 + ret = gpiod_get_value(cas->upstream_line);
164 + mux_control_deselect(cas->mux_control);
165 + return ret;
166 +}
167 +
168 +static int gpio_cascade_probe(struct platform_device *pdev)
169 +{
170 + struct device *dev = &pdev->dev;
171 + struct gpio_cascade *cas;
172 + struct mux_control *mc;
173 + struct gpio_desc *upstream;
174 + struct gpio_chip *gc;
175 +
176 + cas = devm_kzalloc(dev, sizeof(*cas), GFP_KERNEL);
177 + if (!cas)
178 + return -ENOMEM;
179 +
180 + mc = devm_mux_control_get(dev, NULL);
181 + if (IS_ERR(mc))
182 + return dev_err_probe(dev, PTR_ERR(mc), "unable to get mux-control\n");
183 +
184 + cas->mux_control = mc;
185 + upstream = devm_gpiod_get(dev, "upstream", GPIOD_IN);
186 + if (IS_ERR(upstream))
187 + return dev_err_probe(dev, PTR_ERR(upstream), "unable to claim upstream GPIO line\n");
188 +
189 + cas->upstream_line = upstream;
190 + cas->parent = dev;
191 +
192 + gc = &cas->gpio_chip;
193 + gc->get = gpio_cascade_get_value;
194 + gc->get_direction = gpio_cascade_get_direction;
195 + gc->base = -1;
196 + gc->ngpio = mux_control_states(mc);
197 + gc->label = dev_name(cas->parent);
198 + gc->parent = cas->parent;
199 + gc->owner = THIS_MODULE;
200 +
201 + platform_set_drvdata(pdev, cas);
202 + return devm_gpiochip_add_data(dev, &cas->gpio_chip, NULL);
203 +}
204 +
205 +static const struct of_device_id gpio_cascade_id[] = {
206 + { .compatible = "gpio-cascade" },
207 + { /* sentinel */ }
208 +};
209 +MODULE_DEVICE_TABLE(of, gpio_cascade_id);
210 +
211 +static struct platform_driver gpio_cascade_driver = {
212 + .driver = {
213 + .name = "gpio-cascade",
214 + .of_match_table = gpio_cascade_id,
215 + },
216 + .probe = gpio_cascade_probe,
217 +};
218 +module_platform_driver(gpio_cascade_driver);
219 +
220 +MODULE_AUTHOR("Mauri Sandberg <maukka@ext.kapsi.fi>");
221 +MODULE_DESCRIPTION("Generic GPIO cascade");
222 +MODULE_LICENSE("GPL");