1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2025 Linaro Ltd.
4 */
5
6 #include <linux/auxiliary_bus.h>
7 #include <linux/cleanup.h>
8 #include <linux/device.h>
9 #include <linux/err.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/string_choices.h>
15 #include <linux/types.h>
16
17 #include "gpiolib-shared.h"
18
19 struct gpio_shared_proxy_data {
20 struct gpio_chip gc;
21 struct gpio_shared_desc *shared_desc;
22 struct device *dev;
23 bool voted_high;
24 };
25
26 static int
gpio_shared_proxy_set_unlocked(struct gpio_shared_proxy_data * proxy,int (* set_func)(struct gpio_desc * desc,int value),int value)27 gpio_shared_proxy_set_unlocked(struct gpio_shared_proxy_data *proxy,
28 int (*set_func)(struct gpio_desc *desc, int value),
29 int value)
30 {
31 struct gpio_shared_desc *shared_desc = proxy->shared_desc;
32 struct gpio_desc *desc = shared_desc->desc;
33 int ret = 0;
34
35 gpio_shared_lockdep_assert(shared_desc);
36
37 if (value) {
38 /* User wants to set value to high. */
39 if (proxy->voted_high)
40 /* Already voted for high, nothing to do. */
41 goto out;
42
43 /* Haven't voted for high yet. */
44 if (!shared_desc->highcnt) {
45 /*
46 * Current value is low, need to actually set value
47 * to high.
48 */
49 ret = set_func(desc, 1);
50 if (ret)
51 goto out;
52 }
53
54 shared_desc->highcnt++;
55 proxy->voted_high = true;
56
57 goto out;
58 }
59
60 /* Desired value is low. */
61 if (!proxy->voted_high)
62 /* We didn't vote for high, nothing to do. */
63 goto out;
64
65 /* We previously voted for high. */
66 if (shared_desc->highcnt == 1) {
67 /* This is the last remaining vote for high, set value to low. */
68 ret = set_func(desc, 0);
69 if (ret)
70 goto out;
71 }
72
73 shared_desc->highcnt--;
74 proxy->voted_high = false;
75
76 out:
77 if (shared_desc->highcnt)
78 dev_dbg(proxy->dev,
79 "Voted for value '%s', effective value is 'high', number of votes for 'high': %u\n",
80 str_high_low(value), shared_desc->highcnt);
81 else
82 dev_dbg(proxy->dev, "Voted for value 'low', effective value is 'low'\n");
83
84 return ret;
85 }
86
gpio_shared_proxy_request(struct gpio_chip * gc,unsigned int offset)87 static int gpio_shared_proxy_request(struct gpio_chip *gc, unsigned int offset)
88 {
89 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
90 struct gpio_shared_desc *shared_desc = proxy->shared_desc;
91
92 guard(gpio_shared_desc_lock)(shared_desc);
93
94 proxy->shared_desc->usecnt++;
95
96 dev_dbg(proxy->dev, "Shared GPIO requested, number of users: %u\n",
97 proxy->shared_desc->usecnt);
98
99 return 0;
100 }
101
gpio_shared_proxy_free(struct gpio_chip * gc,unsigned int offset)102 static void gpio_shared_proxy_free(struct gpio_chip *gc, unsigned int offset)
103 {
104 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
105 struct gpio_shared_desc *shared_desc = proxy->shared_desc;
106 int ret;
107
108 guard(gpio_shared_desc_lock)(shared_desc);
109
110 if (proxy->voted_high) {
111 ret = gpio_shared_proxy_set_unlocked(proxy,
112 shared_desc->can_sleep ? gpiod_set_value_cansleep : gpiod_set_value, 0);
113 if (ret)
114 dev_err(proxy->dev,
115 "Failed to unset the shared GPIO value on release: %d\n", ret);
116 }
117
118 proxy->shared_desc->usecnt--;
119
120 dev_dbg(proxy->dev, "Shared GPIO freed, number of users: %u\n",
121 proxy->shared_desc->usecnt);
122 }
123
gpio_shared_proxy_set_config(struct gpio_chip * gc,unsigned int offset,unsigned long cfg)124 static int gpio_shared_proxy_set_config(struct gpio_chip *gc,
125 unsigned int offset, unsigned long cfg)
126 {
127 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
128 struct gpio_shared_desc *shared_desc = proxy->shared_desc;
129 struct gpio_desc *desc = shared_desc->desc;
130 int ret;
131
132 guard(gpio_shared_desc_lock)(shared_desc);
133
134 if (shared_desc->usecnt > 1) {
135 if (shared_desc->cfg != cfg) {
136 dev_dbg(proxy->dev,
137 "Shared GPIO's configuration already set, accepting changes but users may conflict!!\n");
138 } else {
139 dev_dbg(proxy->dev, "Equal config requested, nothing to do\n");
140 return 0;
141 }
142 }
143
144 ret = gpiod_set_config(desc, cfg);
145 if (ret && ret != -ENOTSUPP)
146 return ret;
147
148 shared_desc->cfg = cfg;
149 return 0;
150 }
151
gpio_shared_proxy_direction_input(struct gpio_chip * gc,unsigned int offset)152 static int gpio_shared_proxy_direction_input(struct gpio_chip *gc,
153 unsigned int offset)
154 {
155 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
156 struct gpio_shared_desc *shared_desc = proxy->shared_desc;
157 struct gpio_desc *desc = shared_desc->desc;
158 int dir;
159
160 guard(gpio_shared_desc_lock)(shared_desc);
161
162 if (shared_desc->usecnt == 1) {
163 dev_dbg(proxy->dev,
164 "Only one user of this shared GPIO, allowing to set direction to input\n");
165
166 return gpiod_direction_input(desc);
167 }
168
169 dir = gpiod_get_direction(desc);
170 if (dir < 0)
171 return dir;
172
173 if (dir == GPIO_LINE_DIRECTION_OUT) {
174 dev_dbg(proxy->dev,
175 "Shared GPIO's direction already set to output, refusing to change\n");
176 return -EPERM;
177 }
178
179 return 0;
180 }
181
gpio_shared_proxy_direction_output(struct gpio_chip * gc,unsigned int offset,int value)182 static int gpio_shared_proxy_direction_output(struct gpio_chip *gc,
183 unsigned int offset, int value)
184 {
185 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
186 struct gpio_shared_desc *shared_desc = proxy->shared_desc;
187 struct gpio_desc *desc = shared_desc->desc;
188 int ret, dir;
189
190 guard(gpio_shared_desc_lock)(shared_desc);
191
192 if (shared_desc->usecnt == 1) {
193 dev_dbg(proxy->dev,
194 "Only one user of this shared GPIO, allowing to set direction to output with value '%s'\n",
195 str_high_low(value));
196
197 ret = gpiod_direction_output(desc, value);
198 if (ret)
199 return ret;
200
201 if (value) {
202 proxy->voted_high = true;
203 shared_desc->highcnt = 1;
204 } else {
205 proxy->voted_high = false;
206 shared_desc->highcnt = 0;
207 }
208
209 return 0;
210 }
211
212 dir = gpiod_get_direction(desc);
213 if (dir < 0)
214 return dir;
215
216 if (dir == GPIO_LINE_DIRECTION_IN) {
217 dev_dbg(proxy->dev,
218 "Shared GPIO's direction already set to input, refusing to change\n");
219 return -EPERM;
220 }
221
222 return gpio_shared_proxy_set_unlocked(proxy, gpiod_direction_output, value);
223 }
224
gpio_shared_proxy_get(struct gpio_chip * gc,unsigned int offset)225 static int gpio_shared_proxy_get(struct gpio_chip *gc, unsigned int offset)
226 {
227 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
228
229 return gpiod_get_value(proxy->shared_desc->desc);
230 }
231
gpio_shared_proxy_get_cansleep(struct gpio_chip * gc,unsigned int offset)232 static int gpio_shared_proxy_get_cansleep(struct gpio_chip *gc,
233 unsigned int offset)
234 {
235 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
236
237 return gpiod_get_value_cansleep(proxy->shared_desc->desc);
238 }
239
gpio_shared_proxy_do_set(struct gpio_shared_proxy_data * proxy,int (* set_func)(struct gpio_desc * desc,int value),int value)240 static int gpio_shared_proxy_do_set(struct gpio_shared_proxy_data *proxy,
241 int (*set_func)(struct gpio_desc *desc, int value),
242 int value)
243 {
244 guard(gpio_shared_desc_lock)(proxy->shared_desc);
245
246 return gpio_shared_proxy_set_unlocked(proxy, set_func, value);
247 }
248
gpio_shared_proxy_set(struct gpio_chip * gc,unsigned int offset,int value)249 static int gpio_shared_proxy_set(struct gpio_chip *gc, unsigned int offset,
250 int value)
251 {
252 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
253
254 return gpio_shared_proxy_do_set(proxy, gpiod_set_value, value);
255 }
256
gpio_shared_proxy_set_cansleep(struct gpio_chip * gc,unsigned int offset,int value)257 static int gpio_shared_proxy_set_cansleep(struct gpio_chip *gc,
258 unsigned int offset, int value)
259 {
260 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
261
262 return gpio_shared_proxy_do_set(proxy, gpiod_set_value_cansleep, value);
263 }
264
gpio_shared_proxy_get_direction(struct gpio_chip * gc,unsigned int offset)265 static int gpio_shared_proxy_get_direction(struct gpio_chip *gc,
266 unsigned int offset)
267 {
268 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
269
270 return gpiod_get_direction(proxy->shared_desc->desc);
271 }
272
gpio_shared_proxy_to_irq(struct gpio_chip * gc,unsigned int offset)273 static int gpio_shared_proxy_to_irq(struct gpio_chip *gc, unsigned int offset)
274 {
275 struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
276
277 return gpiod_to_irq(proxy->shared_desc->desc);
278 }
279
gpio_shared_proxy_probe(struct auxiliary_device * adev,const struct auxiliary_device_id * id)280 static int gpio_shared_proxy_probe(struct auxiliary_device *adev,
281 const struct auxiliary_device_id *id)
282 {
283 struct gpio_shared_proxy_data *proxy;
284 struct gpio_shared_desc *shared_desc;
285 struct device *dev = &adev->dev;
286 struct gpio_chip *gc;
287
288 shared_desc = devm_gpiod_shared_get(dev);
289 if (IS_ERR(shared_desc))
290 return PTR_ERR(shared_desc);
291
292 proxy = devm_kzalloc(dev, sizeof(*proxy), GFP_KERNEL);
293 if (!proxy)
294 return -ENOMEM;
295
296 proxy->shared_desc = shared_desc;
297 proxy->dev = dev;
298
299 gc = &proxy->gc;
300 gc->base = -1;
301 gc->ngpio = 1;
302 gc->label = dev_name(dev);
303 gc->parent = dev;
304 gc->owner = THIS_MODULE;
305 gc->can_sleep = shared_desc->can_sleep;
306
307 gc->request = gpio_shared_proxy_request;
308 gc->free = gpio_shared_proxy_free;
309 gc->set_config = gpio_shared_proxy_set_config;
310 gc->direction_input = gpio_shared_proxy_direction_input;
311 gc->direction_output = gpio_shared_proxy_direction_output;
312 if (gc->can_sleep) {
313 gc->set = gpio_shared_proxy_set_cansleep;
314 gc->get = gpio_shared_proxy_get_cansleep;
315 } else {
316 gc->set = gpio_shared_proxy_set;
317 gc->get = gpio_shared_proxy_get;
318 }
319 gc->get_direction = gpio_shared_proxy_get_direction;
320 gc->to_irq = gpio_shared_proxy_to_irq;
321
322 return devm_gpiochip_add_data(dev, &proxy->gc, proxy);
323 }
324
325 static const struct auxiliary_device_id gpio_shared_proxy_id_table[] = {
326 { .name = "gpiolib_shared.proxy" },
327 {},
328 };
329 MODULE_DEVICE_TABLE(auxiliary, gpio_shared_proxy_id_table);
330
331 static struct auxiliary_driver gpio_shared_proxy_driver = {
332 .driver = {
333 .name = "gpio-shared-proxy",
334 .suppress_bind_attrs = true,
335 },
336 .probe = gpio_shared_proxy_probe,
337 .id_table = gpio_shared_proxy_id_table,
338 };
339 module_auxiliary_driver(gpio_shared_proxy_driver);
340
341 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
342 MODULE_DESCRIPTION("Shared GPIO mux driver.");
343 MODULE_LICENSE("GPL");
344