xref: /linux/drivers/gpio/gpio-shared-proxy.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
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/lockdep.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/string_choices.h>
16 #include <linux/types.h>
17 
18 #include "gpiolib-shared.h"
19 
20 struct gpio_shared_proxy_data {
21 	struct gpio_chip gc;
22 	struct gpio_shared_desc *shared_desc;
23 	struct device *dev;
24 	bool configured;
25 	bool voted_change;
26 };
27 
28 /*
29  * A proxy may (re)initialize the line - set its direction and reset the
30  * default value and the vote tally - only while no other proxy has set the
31  * direction or voted for a value. Requesting the line is not enough: consumers
32  * may request concurrently, so counting requests lets every one of them
33  * conclude that it is not first.
34  */
35 static bool gpio_shared_proxy_may_init(struct gpio_shared_proxy_data *proxy)
36 {
37 	unsigned int cfgcnt = proxy->shared_desc->cfgcnt;
38 
39 	lockdep_assert_held(&proxy->shared_desc->mutex);
40 
41 	return !cfgcnt || (cfgcnt == 1 && proxy->configured);
42 }
43 
44 static void gpio_shared_proxy_mark_configured(struct gpio_shared_proxy_data *proxy)
45 {
46 	lockdep_assert_held(&proxy->shared_desc->mutex);
47 
48 	if (proxy->configured)
49 		return;
50 
51 	proxy->configured = true;
52 	proxy->shared_desc->cfgcnt++;
53 }
54 
55 static int
56 gpio_shared_proxy_set_unlocked(struct gpio_shared_proxy_data *proxy, int value)
57 {
58 	struct gpio_shared_desc *shared_desc = proxy->shared_desc;
59 	struct gpio_desc *desc = shared_desc->desc;
60 	int ret = 0;
61 
62 	lockdep_assert_held(&shared_desc->mutex);
63 
64 	if (value != shared_desc->def_val) {
65 	       /* User wants to vote for a value change. */
66 		if (proxy->voted_change)
67 			/* Already voted for a change, nothing to do. */
68 			goto out;
69 
70 		/* Haven't voted for a value change yet. */
71 		if (!shared_desc->votecnt) {
72 			/*
73 			 * Current value is default, need to actually set value
74 			 * to the opposite.
75 			 */
76 			ret = gpiod_set_value_cansleep(desc, value);
77 			if (ret)
78 				goto out;
79 		}
80 
81 		shared_desc->votecnt++;
82 		proxy->voted_change = true;
83 
84 		goto out;
85 	}
86 
87 	/* Desired value is the default. */
88 	if (!proxy->voted_change) {
89 		/*
90 		 * With no votes outstanding the line should be at the
91 		 * default, but a GPIOD_ASIS line may have been left at the
92 		 * opposite value. Drive it there.
93 		 */
94 		if (!shared_desc->votecnt)
95 			ret = gpiod_set_value_cansleep(desc, shared_desc->def_val);
96 		goto out;
97 	}
98 
99 	/* We previously voted for change. */
100 	if (shared_desc->votecnt == 1) {
101 		/* This is the last remaining vote for change, set value to default. */
102 		ret = gpiod_set_value_cansleep(desc, shared_desc->def_val);
103 		if (ret)
104 			goto out;
105 	}
106 
107 	shared_desc->votecnt--;
108 	proxy->voted_change = false;
109 
110 out:
111 	if (shared_desc->votecnt)
112 		dev_dbg(proxy->dev,
113 			"Voted for value '%s', effective value is '%s', number of votes: %u\n",
114 			str_high_low(value), str_high_low(!shared_desc->def_val),
115 			shared_desc->votecnt);
116 	else
117 		dev_dbg(proxy->dev, "Voted for value '%s', effective value is '%s'\n",
118 			str_high_low(value), str_high_low(shared_desc->def_val));
119 
120 	return ret;
121 }
122 
123 static int gpio_shared_proxy_request(struct gpio_chip *gc, unsigned int offset)
124 {
125 	struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
126 	struct gpio_shared_desc *shared_desc = proxy->shared_desc;
127 
128 	guard(mutex)(&shared_desc->mutex);
129 
130 	proxy->shared_desc->usecnt++;
131 
132 	dev_dbg(proxy->dev, "Shared GPIO requested, number of users: %u\n",
133 		proxy->shared_desc->usecnt);
134 
135 	return 0;
136 }
137 
138 static void gpio_shared_proxy_free(struct gpio_chip *gc, unsigned int offset)
139 {
140 	struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
141 	struct gpio_shared_desc *shared_desc = proxy->shared_desc;
142 	int ret;
143 
144 	guard(mutex)(&shared_desc->mutex);
145 
146 	if (proxy->voted_change) {
147 		ret = gpio_shared_proxy_set_unlocked(proxy, shared_desc->def_val);
148 		if (ret) {
149 			dev_err(proxy->dev,
150 				"Failed to unset the shared GPIO value on release: %d\n", ret);
151 			/* The consumer is going away regardless, drop its vote */
152 			shared_desc->votecnt--;
153 			proxy->voted_change = false;
154 		}
155 	}
156 
157 	proxy->shared_desc->usecnt--;
158 
159 	if (proxy->configured) {
160 		proxy->configured = false;
161 		shared_desc->cfgcnt--;
162 	}
163 
164 	dev_dbg(proxy->dev, "Shared GPIO freed, number of users: %u\n",
165 		proxy->shared_desc->usecnt);
166 }
167 
168 static int gpio_shared_proxy_set_config(struct gpio_chip *gc,
169 					unsigned int offset, unsigned long cfg)
170 {
171 	struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
172 	struct gpio_shared_desc *shared_desc = proxy->shared_desc;
173 	struct gpio_desc *desc = shared_desc->desc;
174 	int ret;
175 
176 	guard(mutex)(&shared_desc->mutex);
177 
178 	if (!gpio_shared_proxy_may_init(proxy)) {
179 		if (shared_desc->cfg != cfg) {
180 			dev_dbg(proxy->dev,
181 				"Shared GPIO's configuration already set, accepting changes but users may conflict!!\n");
182 		} else {
183 			dev_dbg(proxy->dev, "Equal config requested, nothing to do\n");
184 			return 0;
185 		}
186 	}
187 
188 	ret = gpiod_set_config(desc, cfg);
189 	if (ret && ret != -ENOTSUPP)
190 		return ret;
191 
192 	shared_desc->cfg = cfg;
193 	return 0;
194 }
195 
196 static int gpio_shared_proxy_direction_input(struct gpio_chip *gc,
197 					     unsigned int offset)
198 {
199 	struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
200 	struct gpio_shared_desc *shared_desc = proxy->shared_desc;
201 	struct gpio_desc *desc = shared_desc->desc;
202 	int ret, dir;
203 
204 	guard(mutex)(&shared_desc->mutex);
205 
206 	if (gpio_shared_proxy_may_init(proxy)) {
207 		dev_dbg(proxy->dev,
208 			"No other user configured this shared GPIO, allowing to set direction to input\n");
209 
210 		ret = gpiod_direction_input(desc);
211 		if (ret)
212 			return ret;
213 
214 		/* An input carries no value, drop any vote this proxy cast */
215 		shared_desc->votecnt = 0;
216 		proxy->voted_change = false;
217 		gpio_shared_proxy_mark_configured(proxy);
218 
219 		return 0;
220 	}
221 
222 	dir = gpiod_get_direction(desc);
223 	if (dir < 0)
224 		return dir;
225 
226 	if (dir == GPIO_LINE_DIRECTION_OUT) {
227 		dev_dbg(proxy->dev,
228 			"Shared GPIO's direction already set to output, refusing to change\n");
229 		return -EPERM;
230 	}
231 
232 	gpio_shared_proxy_mark_configured(proxy);
233 
234 	return 0;
235 }
236 
237 static int gpio_shared_proxy_direction_output(struct gpio_chip *gc,
238 					      unsigned int offset, int value)
239 {
240 	struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
241 	struct gpio_shared_desc *shared_desc = proxy->shared_desc;
242 	struct gpio_desc *desc = shared_desc->desc;
243 	int ret, dir;
244 
245 	guard(mutex)(&shared_desc->mutex);
246 
247 	if (gpio_shared_proxy_may_init(proxy)) {
248 		dev_dbg(proxy->dev,
249 			"No other user configured this shared GPIO, allowing to set direction to output with value '%s'\n",
250 			str_high_low(value));
251 
252 		ret = gpiod_direction_output(desc, value);
253 		if (ret)
254 			return ret;
255 
256 		/* Only this proxy can have voted, so the tally is ours to reset */
257 		shared_desc->def_val = value;
258 		shared_desc->votecnt = 0;
259 		proxy->voted_change = false;
260 		gpio_shared_proxy_mark_configured(proxy);
261 
262 		return 0;
263 	}
264 
265 	dir = gpiod_get_direction(desc);
266 	if (dir < 0)
267 		return dir;
268 
269 	if (dir == GPIO_LINE_DIRECTION_IN) {
270 		dev_dbg(proxy->dev,
271 			"Shared GPIO's direction already set to input, refusing to change\n");
272 		return -EPERM;
273 	}
274 
275 	ret = gpio_shared_proxy_set_unlocked(proxy, value);
276 	if (ret)
277 		return ret;
278 
279 	gpio_shared_proxy_mark_configured(proxy);
280 
281 	return 0;
282 }
283 
284 static int gpio_shared_proxy_get_cansleep(struct gpio_chip *gc,
285 					  unsigned int offset)
286 {
287 	struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
288 
289 	return gpiod_get_value_cansleep(proxy->shared_desc->desc);
290 }
291 
292 static int gpio_shared_proxy_set_cansleep(struct gpio_chip *gc,
293 					  unsigned int offset, int value)
294 {
295 	struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
296 	int ret;
297 
298 	guard(mutex)(&proxy->shared_desc->mutex);
299 
300 	/*
301 	 * A GPIOD_ASIS consumer may vote without ever setting the direction;
302 	 * count it as configured so a later user can't reset its vote.
303 	 */
304 	ret = gpio_shared_proxy_set_unlocked(proxy, value);
305 	if (ret)
306 		return ret;
307 
308 	gpio_shared_proxy_mark_configured(proxy);
309 
310 	return 0;
311 }
312 
313 static int gpio_shared_proxy_get_direction(struct gpio_chip *gc,
314 					   unsigned int offset)
315 {
316 	struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
317 
318 	return gpiod_get_direction(proxy->shared_desc->desc);
319 }
320 
321 static int gpio_shared_proxy_to_irq(struct gpio_chip *gc, unsigned int offset)
322 {
323 	struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
324 
325 	return gpiod_to_irq(proxy->shared_desc->desc);
326 }
327 
328 static int gpio_shared_proxy_probe(struct auxiliary_device *adev,
329 				   const struct auxiliary_device_id *id)
330 {
331 	struct gpio_shared_proxy_data *proxy;
332 	struct gpio_shared_desc *shared_desc;
333 	struct device *dev = &adev->dev;
334 	struct gpio_chip *gc;
335 
336 	shared_desc = devm_gpiod_shared_get(dev);
337 	if (IS_ERR(shared_desc))
338 		return PTR_ERR(shared_desc);
339 
340 	proxy = devm_kzalloc_obj(dev, *proxy);
341 	if (!proxy)
342 		return -ENOMEM;
343 
344 	proxy->shared_desc = shared_desc;
345 	proxy->dev = dev;
346 
347 	gc = &proxy->gc;
348 	gc->base = -1;
349 	gc->ngpio = 1;
350 	gc->label = dev_name(dev);
351 	gc->parent = dev;
352 	gc->owner = THIS_MODULE;
353 	/*
354 	 * Under the descriptor mutex the proxy may call
355 	 * gpiod_set_config()/gpiod_direction_*(), which can reach pinctrl
356 	 * paths that take a mutex (e.g. gpiod_set_config() ->
357 	 * gpiochip_generic_config() -> pinctrl_gpio_set_config()), independent
358 	 * of the underlying chip's can_sleep. So the descriptor lock must be a
359 	 * mutex and the proxy gpiochip is therefore always sleeping; drive the
360 	 * underlying GPIO through the cansleep value accessors, which are valid
361 	 * for both sleeping and non-sleeping chips.
362 	 */
363 	gc->can_sleep = true;
364 
365 	gc->request = gpio_shared_proxy_request;
366 	gc->free = gpio_shared_proxy_free;
367 	gc->set_config = gpio_shared_proxy_set_config;
368 	gc->direction_input = gpio_shared_proxy_direction_input;
369 	gc->direction_output = gpio_shared_proxy_direction_output;
370 	gc->set = gpio_shared_proxy_set_cansleep;
371 	gc->get = gpio_shared_proxy_get_cansleep;
372 	gc->get_direction = gpio_shared_proxy_get_direction;
373 	gc->to_irq = gpio_shared_proxy_to_irq;
374 
375 	return devm_gpiochip_add_data(dev, &proxy->gc, proxy);
376 }
377 
378 static const struct auxiliary_device_id gpio_shared_proxy_id_table[] = {
379 	{ .name = "gpiolib_shared.proxy" },
380 	{},
381 };
382 MODULE_DEVICE_TABLE(auxiliary, gpio_shared_proxy_id_table);
383 
384 static struct auxiliary_driver gpio_shared_proxy_driver = {
385 	.driver = {
386 		.name = "gpio-shared-proxy",
387 		.suppress_bind_attrs = true,
388 	},
389 	.probe = gpio_shared_proxy_probe,
390 	.id_table = gpio_shared_proxy_id_table,
391 };
392 module_auxiliary_driver(gpio_shared_proxy_driver);
393 
394 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
395 MODULE_DESCRIPTION("Shared GPIO mux driver.");
396 MODULE_LICENSE("GPL");
397