1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2025 Linaro Ltd.
4 */
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include <linux/auxiliary_bus.h>
9 #include <linux/cleanup.h>
10 #include <linux/device.h>
11 #include <linux/fwnode.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/gpio/machine.h>
14 #include <linux/idr.h>
15 #include <linux/kref.h>
16 #include <linux/list.h>
17 #include <linux/lockdep.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/of.h>
21 #include <linux/overflow.h>
22 #include <linux/printk.h>
23 #include <linux/property.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26
27 #include "gpiolib.h"
28 #include "gpiolib-shared.h"
29
30 /* Represents a single reference to a GPIO pin. */
31 struct gpio_shared_ref {
32 struct list_head list;
33 /* Firmware node associated with this GPIO's consumer. */
34 struct fwnode_handle *fwnode;
35 /* GPIO flags this consumer uses for the request. */
36 enum gpiod_flags flags;
37 char *con_id;
38 int dev_id;
39 /* Protects the auxiliary device struct and the lookup table. */
40 struct mutex lock;
41 struct auxiliary_device adev;
42 struct gpiod_lookup_table *lookup;
43 };
44
45 /* Represents a single GPIO pin. */
46 struct gpio_shared_entry {
47 struct list_head list;
48 /* Firmware node associated with the GPIO controller. */
49 struct fwnode_handle *fwnode;
50 /* Hardware offset of the GPIO within its chip. */
51 unsigned int offset;
52 /* Index in the property value array. */
53 size_t index;
54 /* Synchronizes the modification of shared_desc. */
55 struct mutex lock;
56 struct gpio_shared_desc *shared_desc;
57 struct kref ref;
58 struct list_head refs;
59 };
60
61 static LIST_HEAD(gpio_shared_list);
62 static DEFINE_IDA(gpio_shared_ida);
63
64 #if IS_ENABLED(CONFIG_OF)
65 static struct gpio_shared_entry *
gpio_shared_find_entry(struct fwnode_handle * controller_node,unsigned int offset)66 gpio_shared_find_entry(struct fwnode_handle *controller_node,
67 unsigned int offset)
68 {
69 struct gpio_shared_entry *entry;
70
71 list_for_each_entry(entry, &gpio_shared_list, list) {
72 if (entry->fwnode == controller_node && entry->offset == offset)
73 return entry;
74 }
75
76 return NULL;
77 }
78
79 /* Handle all special nodes that we should ignore. */
gpio_shared_of_node_ignore(struct device_node * node)80 static bool gpio_shared_of_node_ignore(struct device_node *node)
81 {
82 /* Ignore disabled devices. */
83 if (!of_device_is_available(node))
84 return true;
85
86 /*
87 * __symbols__ is a special, internal node and should not be considered
88 * when scanning for shared GPIOs.
89 */
90 if (of_node_name_eq(node, "__symbols__"))
91 return true;
92
93 /*
94 * GPIO hogs have a "gpios" property which is not a phandle and can't
95 * possibly refer to a shared GPIO.
96 */
97 if (of_property_present(node, "gpio-hog"))
98 return true;
99
100 return false;
101 }
102
gpio_shared_of_traverse(struct device_node * curr)103 static int gpio_shared_of_traverse(struct device_node *curr)
104 {
105 struct gpio_shared_entry *entry;
106 size_t con_id_len, suffix_len;
107 struct fwnode_handle *fwnode;
108 struct of_phandle_args args;
109 struct property *prop;
110 unsigned int offset;
111 const char *suffix;
112 int ret, count, i;
113
114 if (gpio_shared_of_node_ignore(curr))
115 return 0;
116
117 for_each_property_of_node(curr, prop) {
118 /*
119 * The standard name for a GPIO property is "foo-gpios"
120 * or "foo-gpio". Some bindings also use "gpios" or "gpio".
121 * There are some legacy device-trees which have a different
122 * naming convention and for which we have rename quirks in
123 * place in gpiolib-of.c. I don't think any of them require
124 * support for shared GPIOs so for now let's just ignore
125 * them. We can always just export the quirk list and
126 * iterate over it here.
127 */
128 if (!strends(prop->name, "-gpios") &&
129 !strends(prop->name, "-gpio") &&
130 strcmp(prop->name, "gpios") != 0 &&
131 strcmp(prop->name, "gpio") != 0)
132 continue;
133
134 count = of_count_phandle_with_args(curr, prop->name,
135 "#gpio-cells");
136 if (count <= 0)
137 continue;
138
139 for (i = 0; i < count; i++) {
140 struct device_node *np __free(device_node) = NULL;
141
142 ret = of_parse_phandle_with_args(curr, prop->name,
143 "#gpio-cells", i,
144 &args);
145 if (ret)
146 continue;
147
148 np = args.np;
149
150 if (!of_property_present(np, "gpio-controller"))
151 continue;
152
153 /*
154 * We support 1, 2 and 3 cell GPIO bindings in the
155 * kernel currently. There's only one old MIPS dts that
156 * has a one-cell binding but there's no associated
157 * consumer so it may as well be an error. There don't
158 * seem to be any 3-cell users of non-exclusive GPIOs,
159 * so we can skip this as well. Let's occupy ourselves
160 * with the predominant 2-cell binding with the first
161 * cell indicating the hardware offset of the GPIO and
162 * the second defining the GPIO flags of the request.
163 */
164 if (args.args_count != 2)
165 continue;
166
167 fwnode = of_fwnode_handle(args.np);
168 offset = args.args[0];
169
170 entry = gpio_shared_find_entry(fwnode, offset);
171 if (!entry) {
172 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
173 if (!entry)
174 return -ENOMEM;
175
176 entry->fwnode = fwnode_handle_get(fwnode);
177 entry->offset = offset;
178 entry->index = count;
179 INIT_LIST_HEAD(&entry->refs);
180 mutex_init(&entry->lock);
181
182 list_add_tail(&entry->list, &gpio_shared_list);
183 }
184
185 struct gpio_shared_ref *ref __free(kfree) =
186 kzalloc(sizeof(*ref), GFP_KERNEL);
187 if (!ref)
188 return -ENOMEM;
189
190 ref->fwnode = fwnode_handle_get(of_fwnode_handle(curr));
191 ref->flags = args.args[1];
192 mutex_init(&ref->lock);
193
194 if (strends(prop->name, "gpios"))
195 suffix = "-gpios";
196 else if (strends(prop->name, "gpio"))
197 suffix = "-gpio";
198 else
199 suffix = NULL;
200 if (!suffix)
201 continue;
202
203 /* We only set con_id if there's actually one. */
204 if (strcmp(prop->name, "gpios") && strcmp(prop->name, "gpio")) {
205 ref->con_id = kstrdup(prop->name, GFP_KERNEL);
206 if (!ref->con_id)
207 return -ENOMEM;
208
209 con_id_len = strlen(ref->con_id);
210 suffix_len = strlen(suffix);
211
212 ref->con_id[con_id_len - suffix_len] = '\0';
213 }
214
215 ref->dev_id = ida_alloc(&gpio_shared_ida, GFP_KERNEL);
216 if (ref->dev_id < 0) {
217 kfree(ref->con_id);
218 return -ENOMEM;
219 }
220
221 if (!list_empty(&entry->refs))
222 pr_debug("GPIO %u at %s is shared by multiple firmware nodes\n",
223 entry->offset, fwnode_get_name(entry->fwnode));
224
225 list_add_tail(&no_free_ptr(ref)->list, &entry->refs);
226 }
227 }
228
229 for_each_child_of_node_scoped(curr, child) {
230 ret = gpio_shared_of_traverse(child);
231 if (ret)
232 return ret;
233 }
234
235 return 0;
236 }
237
gpio_shared_of_scan(void)238 static int gpio_shared_of_scan(void)
239 {
240 if (of_root)
241 return gpio_shared_of_traverse(of_root);
242
243 return 0;
244 }
245 #else
gpio_shared_of_scan(void)246 static int gpio_shared_of_scan(void)
247 {
248 return 0;
249 }
250 #endif /* CONFIG_OF */
251
gpio_shared_adev_release(struct device * dev)252 static void gpio_shared_adev_release(struct device *dev)
253 {
254
255 }
256
gpio_shared_make_adev(struct gpio_device * gdev,struct gpio_shared_entry * entry,struct gpio_shared_ref * ref)257 static int gpio_shared_make_adev(struct gpio_device *gdev,
258 struct gpio_shared_entry *entry,
259 struct gpio_shared_ref *ref)
260 {
261 struct auxiliary_device *adev = &ref->adev;
262 int ret;
263
264 guard(mutex)(&ref->lock);
265
266 memset(adev, 0, sizeof(*adev));
267
268 adev->id = ref->dev_id;
269 adev->name = "proxy";
270 adev->dev.parent = gdev->dev.parent;
271 adev->dev.platform_data = entry;
272 adev->dev.release = gpio_shared_adev_release;
273
274 ret = auxiliary_device_init(adev);
275 if (ret)
276 return ret;
277
278 ret = auxiliary_device_add(adev);
279 if (ret) {
280 auxiliary_device_uninit(adev);
281 return ret;
282 }
283
284 pr_debug("Created an auxiliary GPIO proxy %s for GPIO device %s\n",
285 dev_name(&adev->dev), gpio_device_get_label(gdev));
286
287 return 0;
288 }
289
290 #if IS_ENABLED(CONFIG_RESET_GPIO)
291 /*
292 * Special case: reset-gpio is an auxiliary device that's created dynamically
293 * and put in between the GPIO controller and consumers of shared GPIOs
294 * referred to by the "reset-gpios" property.
295 *
296 * If the supposed consumer of a shared GPIO didn't match any of the mappings
297 * we created when scanning the firmware nodes, it's still possible that it's
298 * the reset-gpio device which didn't exist at the time of the scan.
299 *
300 * This function verifies it an return true if it's the case.
301 */
gpio_shared_dev_is_reset_gpio(struct device * consumer,struct gpio_shared_entry * entry,struct gpio_shared_ref * ref)302 static bool gpio_shared_dev_is_reset_gpio(struct device *consumer,
303 struct gpio_shared_entry *entry,
304 struct gpio_shared_ref *ref)
305 {
306 struct fwnode_handle *reset_fwnode = dev_fwnode(consumer);
307 struct fwnode_reference_args ref_args, aux_args;
308 struct device *parent = consumer->parent;
309 bool match;
310 int ret;
311
312 /* The reset-gpio device must have a parent AND a firmware node. */
313 if (!parent || !reset_fwnode)
314 return false;
315
316 /*
317 * FIXME: use device_is_compatible() once the reset-gpio drivers gains
318 * a compatible string which it currently does not have.
319 */
320 if (!strstarts(dev_name(consumer), "reset.gpio."))
321 return false;
322
323 /*
324 * Parent of the reset-gpio auxiliary device is the GPIO chip whose
325 * fwnode we stored in the entry structure.
326 */
327 if (!device_match_fwnode(parent, entry->fwnode))
328 return false;
329
330 /*
331 * The device associated with the shared reference's firmware node is
332 * the consumer of the reset control exposed by the reset-gpio device.
333 * It must have a "reset-gpios" property that's referencing the entry's
334 * firmware node.
335 *
336 * The reference args must agree between the real consumer and the
337 * auxiliary reset-gpio device.
338 */
339 ret = fwnode_property_get_reference_args(ref->fwnode, "reset-gpios",
340 NULL, 2, 0, &ref_args);
341 if (ret)
342 return false;
343
344 ret = fwnode_property_get_reference_args(reset_fwnode, "reset-gpios",
345 NULL, 2, 0, &aux_args);
346 if (ret) {
347 fwnode_handle_put(ref_args.fwnode);
348 return false;
349 }
350
351 match = ((ref_args.fwnode == entry->fwnode) &&
352 (aux_args.fwnode == entry->fwnode) &&
353 (ref_args.args[0] == aux_args.args[0]));
354
355 fwnode_handle_put(ref_args.fwnode);
356 fwnode_handle_put(aux_args.fwnode);
357 return match;
358 }
359 #else
gpio_shared_dev_is_reset_gpio(struct device * consumer,struct gpio_shared_entry * entry,struct gpio_shared_ref * ref)360 static bool gpio_shared_dev_is_reset_gpio(struct device *consumer,
361 struct gpio_shared_entry *entry,
362 struct gpio_shared_ref *ref)
363 {
364 return false;
365 }
366 #endif /* CONFIG_RESET_GPIO */
367
gpio_shared_add_proxy_lookup(struct device * consumer,unsigned long lflags)368 int gpio_shared_add_proxy_lookup(struct device *consumer, unsigned long lflags)
369 {
370 const char *dev_id = dev_name(consumer);
371 struct gpio_shared_entry *entry;
372 struct gpio_shared_ref *ref;
373
374 struct gpiod_lookup_table *lookup __free(kfree) =
375 kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
376 if (!lookup)
377 return -ENOMEM;
378
379 list_for_each_entry(entry, &gpio_shared_list, list) {
380 list_for_each_entry(ref, &entry->refs, list) {
381 if (!device_match_fwnode(consumer, ref->fwnode) &&
382 !gpio_shared_dev_is_reset_gpio(consumer, entry, ref))
383 continue;
384
385 guard(mutex)(&ref->lock);
386
387 /* We've already done that on a previous request. */
388 if (ref->lookup)
389 return 0;
390
391 char *key __free(kfree) =
392 kasprintf(GFP_KERNEL,
393 KBUILD_MODNAME ".proxy.%u",
394 ref->adev.id);
395 if (!key)
396 return -ENOMEM;
397
398 pr_debug("Adding machine lookup entry for a shared GPIO for consumer %s, with key '%s' and con_id '%s'\n",
399 dev_id, key, ref->con_id ?: "none");
400
401 lookup->dev_id = dev_id;
402 lookup->table[0] = GPIO_LOOKUP(no_free_ptr(key), 0,
403 ref->con_id, lflags);
404
405 ref->lookup = no_free_ptr(lookup);
406 gpiod_add_lookup_table(ref->lookup);
407
408 return 0;
409 }
410 }
411
412 /* We warn here because this can only happen if the programmer borked. */
413 WARN_ON(1);
414 return -ENOENT;
415 }
416
gpio_shared_remove_adev(struct auxiliary_device * adev)417 static void gpio_shared_remove_adev(struct auxiliary_device *adev)
418 {
419 auxiliary_device_delete(adev);
420 auxiliary_device_uninit(adev);
421 }
422
gpio_device_setup_shared(struct gpio_device * gdev)423 int gpio_device_setup_shared(struct gpio_device *gdev)
424 {
425 struct gpio_shared_entry *entry;
426 struct gpio_shared_ref *ref;
427 unsigned long *flags;
428 int ret;
429
430 list_for_each_entry(entry, &gpio_shared_list, list) {
431 list_for_each_entry(ref, &entry->refs, list) {
432 if (gdev->dev.parent == &ref->adev.dev) {
433 /*
434 * This is a shared GPIO proxy. Mark its
435 * descriptor as such and return here.
436 */
437 __set_bit(GPIOD_FLAG_SHARED_PROXY,
438 &gdev->descs[0].flags);
439 return 0;
440 }
441 }
442 }
443
444 /*
445 * This is not a shared GPIO proxy but it still may be the device
446 * exposing shared pins. Find them and create the proxy devices.
447 */
448 list_for_each_entry(entry, &gpio_shared_list, list) {
449 if (!device_match_fwnode(&gdev->dev, entry->fwnode))
450 continue;
451
452 if (list_count_nodes(&entry->refs) <= 1)
453 continue;
454
455 flags = &gdev->descs[entry->offset].flags;
456
457 __set_bit(GPIOD_FLAG_SHARED, flags);
458 /*
459 * Shared GPIOs are not requested via the normal path. Make
460 * them inaccessible to anyone even before we register the
461 * chip.
462 */
463 __set_bit(GPIOD_FLAG_REQUESTED, flags);
464
465 pr_debug("GPIO %u owned by %s is shared by multiple consumers\n",
466 entry->offset, gpio_device_get_label(gdev));
467
468 list_for_each_entry(ref, &entry->refs, list) {
469 pr_debug("Setting up a shared GPIO entry for %s\n",
470 fwnode_get_name(ref->fwnode));
471
472 ret = gpio_shared_make_adev(gdev, entry, ref);
473 if (ret)
474 return ret;
475 }
476 }
477
478 return 0;
479 }
480
gpio_device_teardown_shared(struct gpio_device * gdev)481 void gpio_device_teardown_shared(struct gpio_device *gdev)
482 {
483 struct gpio_shared_entry *entry;
484 struct gpio_shared_ref *ref;
485
486 list_for_each_entry(entry, &gpio_shared_list, list) {
487 if (!device_match_fwnode(&gdev->dev, entry->fwnode))
488 continue;
489
490 /*
491 * For some reason if we call synchronize_srcu() in GPIO core,
492 * descent here and take this mutex and then recursively call
493 * synchronize_srcu() again from gpiochip_remove() (which is
494 * totally fine) called after gpio_shared_remove_adev(),
495 * lockdep prints a false positive deadlock splat. Disable
496 * lockdep here.
497 */
498 lockdep_off();
499 list_for_each_entry(ref, &entry->refs, list) {
500 guard(mutex)(&ref->lock);
501
502 if (ref->lookup) {
503 gpiod_remove_lookup_table(ref->lookup);
504 kfree(ref->lookup->table[0].key);
505 kfree(ref->lookup);
506 ref->lookup = NULL;
507 }
508
509 gpio_shared_remove_adev(&ref->adev);
510 }
511 lockdep_on();
512 }
513 }
514
gpio_shared_release(struct kref * kref)515 static void gpio_shared_release(struct kref *kref)
516 {
517 struct gpio_shared_entry *entry =
518 container_of(kref, struct gpio_shared_entry, ref);
519 struct gpio_shared_desc *shared_desc;
520
521 guard(mutex)(&entry->lock);
522
523 shared_desc = entry->shared_desc;
524 gpio_device_put(shared_desc->desc->gdev);
525 if (shared_desc->can_sleep)
526 mutex_destroy(&shared_desc->mutex);
527 kfree(shared_desc);
528 entry->shared_desc = NULL;
529 }
530
gpiod_shared_put(void * data)531 static void gpiod_shared_put(void *data)
532 {
533 struct gpio_shared_entry *entry = data;
534
535 kref_put(&entry->ref, gpio_shared_release);
536 }
537
538 static struct gpio_shared_desc *
gpiod_shared_desc_create(struct gpio_shared_entry * entry)539 gpiod_shared_desc_create(struct gpio_shared_entry *entry)
540 {
541 struct gpio_shared_desc *shared_desc;
542 struct gpio_device *gdev;
543
544 lockdep_assert_held(&entry->lock);
545
546 shared_desc = kzalloc(sizeof(*shared_desc), GFP_KERNEL);
547 if (!shared_desc)
548 return ERR_PTR(-ENOMEM);
549
550 gdev = gpio_device_find_by_fwnode(entry->fwnode);
551 if (!gdev) {
552 kfree(shared_desc);
553 return ERR_PTR(-EPROBE_DEFER);
554 }
555
556 shared_desc->desc = &gdev->descs[entry->offset];
557 shared_desc->can_sleep = gpiod_cansleep(shared_desc->desc);
558 if (shared_desc->can_sleep)
559 mutex_init(&shared_desc->mutex);
560 else
561 spin_lock_init(&shared_desc->spinlock);
562
563 return shared_desc;
564 }
565
devm_gpiod_shared_get(struct device * dev)566 struct gpio_shared_desc *devm_gpiod_shared_get(struct device *dev)
567 {
568 struct gpio_shared_desc *shared_desc;
569 struct gpio_shared_entry *entry;
570 int ret;
571
572 entry = dev_get_platdata(dev);
573 if (WARN_ON(!entry))
574 /* Programmer bug */
575 return ERR_PTR(-ENOENT);
576
577 scoped_guard(mutex, &entry->lock) {
578 if (entry->shared_desc) {
579 kref_get(&entry->ref);
580 shared_desc = entry->shared_desc;
581 } else {
582 shared_desc = gpiod_shared_desc_create(entry);
583 if (IS_ERR(shared_desc))
584 return ERR_CAST(shared_desc);
585
586 kref_init(&entry->ref);
587 entry->shared_desc = shared_desc;
588 }
589
590 pr_debug("Device %s acquired a reference to the shared GPIO %u owned by %s\n",
591 dev_name(dev), gpiod_hwgpio(shared_desc->desc),
592 gpio_device_get_label(shared_desc->desc->gdev));
593 }
594
595 ret = devm_add_action_or_reset(dev, gpiod_shared_put, entry);
596 if (ret)
597 return ERR_PTR(ret);
598
599 return shared_desc;
600 }
601 EXPORT_SYMBOL_GPL(devm_gpiod_shared_get);
602
gpio_shared_drop_ref(struct gpio_shared_ref * ref)603 static void gpio_shared_drop_ref(struct gpio_shared_ref *ref)
604 {
605 list_del(&ref->list);
606 mutex_destroy(&ref->lock);
607 kfree(ref->con_id);
608 ida_free(&gpio_shared_ida, ref->dev_id);
609 fwnode_handle_put(ref->fwnode);
610 kfree(ref);
611 }
612
gpio_shared_drop_entry(struct gpio_shared_entry * entry)613 static void gpio_shared_drop_entry(struct gpio_shared_entry *entry)
614 {
615 list_del(&entry->list);
616 mutex_destroy(&entry->lock);
617 fwnode_handle_put(entry->fwnode);
618 kfree(entry);
619 }
620
621 /*
622 * This is only called if gpio_shared_init() fails so it's in fact __init and
623 * not __exit.
624 */
gpio_shared_teardown(void)625 static void __init gpio_shared_teardown(void)
626 {
627 struct gpio_shared_entry *entry, *epos;
628 struct gpio_shared_ref *ref, *rpos;
629
630 list_for_each_entry_safe(entry, epos, &gpio_shared_list, list) {
631 list_for_each_entry_safe(ref, rpos, &entry->refs, list)
632 gpio_shared_drop_ref(ref);
633
634 gpio_shared_drop_entry(entry);
635 }
636 }
637
gpio_shared_free_exclusive(void)638 static void gpio_shared_free_exclusive(void)
639 {
640 struct gpio_shared_entry *entry, *epos;
641
642 list_for_each_entry_safe(entry, epos, &gpio_shared_list, list) {
643 if (list_count_nodes(&entry->refs) > 1)
644 continue;
645
646 gpio_shared_drop_ref(list_first_entry(&entry->refs,
647 struct gpio_shared_ref,
648 list));
649 gpio_shared_drop_entry(entry);
650 }
651 }
652
gpio_shared_init(void)653 static int __init gpio_shared_init(void)
654 {
655 int ret;
656
657 /* Right now, we only support OF-based systems. */
658 ret = gpio_shared_of_scan();
659 if (ret) {
660 gpio_shared_teardown();
661 pr_err("Failed to scan OF nodes for shared GPIOs: %d\n", ret);
662 return ret;
663 }
664
665 gpio_shared_free_exclusive();
666
667 pr_debug("Finished scanning firmware nodes for shared GPIOs\n");
668 return 0;
669 }
670 postcore_initcall(gpio_shared_init);
671