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