xref: /linux/drivers/gpio/gpiolib-kunit.c (revision 7711f4417f1870e4bfbee1a7d501f789255bc7aa)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) Qualcomm Technologies, Inc. and/or its subsidiaries
4  */
5 
6 #include <linux/cleanup.h>
7 #include <linux/err.h>
8 #include <linux/fwnode.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/gpio/machine.h>
12 #include <linux/gpio/property.h>
13 #include <linux/notifier.h>
14 #include <linux/platform_device.h>
15 #include <linux/property.h>
16 #include <linux/types.h>
17 
18 #include <kunit/fwnode.h>
19 #include <kunit/platform_device.h>
20 #include <kunit/test.h>
21 
22 #define GPIO_TEST_PROVIDER		"gpio-test-provider"
23 #define GPIO_SWNODE_TEST_CONSUMER	"gpio-swnode-test-consumer"
24 #define GPIO_PROBE_ORDER_TEST_CONSUMER	"gpio-probe-order-test-consumer"
25 #define GPIO_PROBE_DEFER_TEST_CONSUMER	"gpio-probe-defer-test-consumer"
26 #define GPIO_UNBIND_TEST_CONSUMER	"gpio-unbind-test-consumer"
27 #define GPIO_CONSUMER_NAME		"gpio-swnode-consumer-test-device"
28 
29 #define GPIO_TEST_PROVIDER_NGPIO	4
30 
31 /*
32  * The test provider tracks per-line direction and value so that lines can be
33  * driven as both inputs and outputs - this is needed to exercise input as well
34  * as output GPIO hogs.
35  */
36 struct gpio_test_provider_data {
37 	DECLARE_BITMAP(is_output, GPIO_TEST_PROVIDER_NGPIO);
38 	DECLARE_BITMAP(values, GPIO_TEST_PROVIDER_NGPIO);
39 };
40 
41 static int gpio_test_provider_get_direction(struct gpio_chip *gc, unsigned int offset)
42 {
43 	struct gpio_test_provider_data *data = gpiochip_get_data(gc);
44 
45 	return test_bit(offset, data->is_output) ?
46 		GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
47 }
48 
49 static int gpio_test_provider_direction_input(struct gpio_chip *gc, unsigned int offset)
50 {
51 	struct gpio_test_provider_data *data = gpiochip_get_data(gc);
52 
53 	clear_bit(offset, data->is_output);
54 
55 	return 0;
56 }
57 
58 static int gpio_test_provider_direction_output(struct gpio_chip *gc, unsigned int offset,
59 					       int value)
60 {
61 	struct gpio_test_provider_data *data = gpiochip_get_data(gc);
62 
63 	set_bit(offset, data->is_output);
64 	__assign_bit(offset, data->values, value);
65 
66 	return 0;
67 }
68 
69 static int gpio_test_provider_get(struct gpio_chip *gc, unsigned int offset)
70 {
71 	struct gpio_test_provider_data *data = gpiochip_get_data(gc);
72 
73 	return test_bit(offset, data->values);
74 }
75 
76 static int gpio_test_provider_set(struct gpio_chip *gc, unsigned int offset, int value)
77 {
78 	struct gpio_test_provider_data *data = gpiochip_get_data(gc);
79 
80 	__assign_bit(offset, data->values, value);
81 
82 	return 0;
83 }
84 
85 static int gpio_test_provider_probe(struct platform_device *pdev)
86 {
87 	struct gpio_test_provider_data *data;
88 	struct device *dev = &pdev->dev;
89 	struct gpio_chip *gc;
90 
91 	gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
92 	if (!gc)
93 		return -ENOMEM;
94 
95 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
96 	if (!data)
97 		return -ENOMEM;
98 
99 	/* Lines start as outputs to preserve the default for lookup tests. */
100 	bitmap_fill(data->is_output, GPIO_TEST_PROVIDER_NGPIO);
101 
102 	gc->base = -1;
103 	gc->ngpio = GPIO_TEST_PROVIDER_NGPIO;
104 	gc->label = GPIO_CONSUMER_NAME;
105 	gc->parent = dev;
106 	gc->owner = THIS_MODULE;
107 
108 	gc->get_direction = gpio_test_provider_get_direction;
109 	gc->direction_input = gpio_test_provider_direction_input;
110 	gc->direction_output = gpio_test_provider_direction_output;
111 	gc->get = gpio_test_provider_get;
112 	gc->set = gpio_test_provider_set;
113 
114 	return devm_gpiochip_add_data(dev, gc, data);
115 }
116 
117 static struct platform_driver gpio_test_provider_driver = {
118 	.probe = gpio_test_provider_probe,
119 	.driver = {
120 		.name = GPIO_TEST_PROVIDER,
121 	},
122 };
123 
124 static const struct software_node gpio_test_provider_swnode = {
125 	.name = "gpio-test-provider-primary",
126 };
127 
128 struct gpio_swnode_consumer_pdata {
129 	bool gpio_ok;
130 	int errno;
131 };
132 
133 static const struct gpio_swnode_consumer_pdata gpio_swnode_pdata_template = {
134 	.gpio_ok = false,
135 	.errno = 0,
136 };
137 
138 static int gpio_swnode_consumer_probe(struct platform_device *pdev)
139 {
140 	struct device *dev = &pdev->dev;
141 	struct gpio_swnode_consumer_pdata *pdata = dev_get_platdata(dev);
142 	struct gpio_desc *desc;
143 
144 	desc = devm_gpiod_get(dev, "foo", GPIOD_OUT_HIGH);
145 	if (IS_ERR(desc)) {
146 		pdata->errno = PTR_ERR(desc);
147 		return PTR_ERR(desc);
148 	}
149 
150 	pdata->gpio_ok = true;
151 
152 	return 0;
153 }
154 
155 static struct platform_driver gpio_swnode_consumer_driver = {
156 	.probe = gpio_swnode_consumer_probe,
157 	.driver = {
158 		.name = GPIO_SWNODE_TEST_CONSUMER,
159 	},
160 };
161 
162 static int gpio_swnode_register_drivers(struct kunit *test)
163 {
164 	int ret;
165 
166 	ret = kunit_platform_driver_register(test, &gpio_test_provider_driver);
167 	KUNIT_ASSERT_EQ(test, ret, 0);
168 
169 	ret = kunit_platform_driver_register(test, &gpio_swnode_consumer_driver);
170 	KUNIT_ASSERT_EQ(test, ret, 0);
171 
172 	return 0;
173 }
174 
175 static void gpio_swnode_lookup_by_primary(struct kunit *test)
176 {
177 	struct gpio_swnode_consumer_pdata *pdata;
178 	struct platform_device_info pdevinfo;
179 	struct property_entry properties[2];
180 	struct platform_device *pdev;
181 	bool bound = false;
182 
183 	pdevinfo = (struct platform_device_info){
184 		.name = GPIO_TEST_PROVIDER,
185 		.id = PLATFORM_DEVID_NONE,
186 		.swnode = &gpio_test_provider_swnode,
187 	};
188 
189 	pdev = kunit_platform_device_register_full(test, &pdevinfo);
190 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
191 
192 	properties[0] = PROPERTY_ENTRY_GPIO("foo-gpios",
193 					    &gpio_test_provider_swnode,
194 					    0, GPIO_ACTIVE_HIGH);
195 	properties[1] = (struct property_entry){ };
196 
197 	pdevinfo = (struct platform_device_info){
198 		.name = GPIO_SWNODE_TEST_CONSUMER,
199 		.id = PLATFORM_DEVID_NONE,
200 		.data = &gpio_swnode_pdata_template,
201 		.size_data = sizeof(gpio_swnode_pdata_template),
202 		.properties = properties,
203 	};
204 
205 	pdev = kunit_platform_device_register_full(test, &pdevinfo);
206 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
207 
208 	wait_for_device_probe();
209 	scoped_guard(device, &pdev->dev)
210 		bound = device_is_bound(&pdev->dev);
211 
212 	KUNIT_ASSERT_TRUE(test, bound);
213 
214 	pdata = dev_get_platdata(&pdev->dev);
215 	KUNIT_ASSERT_TRUE(test, pdata->gpio_ok);
216 }
217 
218 static void gpio_swnode_lookup_by_secondary(struct kunit *test)
219 {
220 	struct gpio_swnode_consumer_pdata *pdata;
221 	struct platform_device_info pdevinfo;
222 	struct property_entry properties[2];
223 	struct fwnode_handle *primary;
224 	struct platform_device *pdev;
225 	bool bound = false;
226 
227 	/*
228 	 * Can't live on the stack as it will still get referenced in cleanup
229 	 * path after this function returns.
230 	 */
231 	primary = kunit_kzalloc(test, sizeof(*primary), GFP_KERNEL);
232 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, primary);
233 
234 	fwnode_init(primary, NULL);
235 
236 	pdevinfo = (struct platform_device_info){
237 		.name = GPIO_TEST_PROVIDER,
238 		.id = PLATFORM_DEVID_NONE,
239 		.fwnode = primary,
240 		.swnode = &gpio_test_provider_swnode,
241 	};
242 
243 	pdev = kunit_platform_device_register_full(test, &pdevinfo);
244 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
245 
246 	properties[0] = PROPERTY_ENTRY_GPIO("foo-gpios",
247 					    &gpio_test_provider_swnode,
248 					    0, GPIO_ACTIVE_HIGH);
249 	properties[1] = (struct property_entry){ };
250 
251 	pdevinfo = (struct platform_device_info){
252 		.name = GPIO_SWNODE_TEST_CONSUMER,
253 		.id = PLATFORM_DEVID_NONE,
254 		.data = &gpio_swnode_pdata_template,
255 		.size_data = sizeof(gpio_swnode_pdata_template),
256 		.properties = properties,
257 	};
258 
259 	pdev = kunit_platform_device_register_full(test, &pdevinfo);
260 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
261 
262 	wait_for_device_probe();
263 	scoped_guard(device, &pdev->dev)
264 		bound = device_is_bound(&pdev->dev);
265 
266 	KUNIT_ASSERT_TRUE(test, bound);
267 
268 	pdata = dev_get_platdata(&pdev->dev);
269 	KUNIT_ASSERT_TRUE(test, pdata->gpio_ok);
270 }
271 
272 static struct kunit_case gpio_swnode_lookup_tests[] = {
273 	KUNIT_CASE(gpio_swnode_lookup_by_primary),
274 	KUNIT_CASE(gpio_swnode_lookup_by_secondary),
275 	{ }
276 };
277 
278 static struct kunit_suite gpio_swnode_lookup_test_suite = {
279 	.name = "gpio-swnode-lookup",
280 	.test_cases = gpio_swnode_lookup_tests,
281 	.init = gpio_swnode_register_drivers,
282 };
283 
284 static void gpio_swnode_unregister_swnode(void *data)
285 {
286 	software_node_unregister(data);
287 }
288 
289 struct gpio_probe_order_pdata {
290 	unsigned int probe_count;
291 	bool gpio_ok;
292 };
293 
294 static const struct gpio_probe_order_pdata gpio_probe_order_pdata_template = {
295 	.probe_count = 0,
296 	.gpio_ok = false,
297 };
298 
299 static int gpio_probe_order_consumer_probe(struct platform_device *pdev)
300 {
301 	struct device *dev = &pdev->dev;
302 	struct gpio_probe_order_pdata *pdata = dev_get_platdata(dev);
303 	struct gpio_desc *desc;
304 
305 	pdata->probe_count++;
306 
307 	desc = devm_gpiod_get(dev, "foo", GPIOD_OUT_HIGH);
308 	if (IS_ERR(desc))
309 		return PTR_ERR(desc);
310 
311 	pdata->gpio_ok = true;
312 
313 	return 0;
314 }
315 
316 static struct platform_driver gpio_probe_order_consumer_driver = {
317 	.probe = gpio_probe_order_consumer_probe,
318 	.driver = {
319 		.name = GPIO_PROBE_ORDER_TEST_CONSUMER,
320 	},
321 };
322 
323 /*
324  * Verify that fw_devlink orders the probe of a GPIO consumer after its
325  * provider. The consumer references the provider through a software node and
326  * is registered first. fw_devlink must defer it before its driver's probe()
327  * is ever entered, so the consumer probes exactly once - only after the
328  * provider is added and bound.
329  */
330 static void gpio_swnode_probe_order(struct kunit *test)
331 {
332 	struct property_entry properties[2] = { };
333 	struct gpio_probe_order_pdata *pdata;
334 	struct platform_device_info pdevinfo;
335 	struct platform_device *prvd, *cons;
336 	bool bound = false;
337 	int ret;
338 
339 	ret = kunit_platform_driver_register(test, &gpio_test_provider_driver);
340 	KUNIT_ASSERT_EQ(test, ret, 0);
341 
342 	ret = kunit_platform_driver_register(test, &gpio_probe_order_consumer_driver);
343 	KUNIT_ASSERT_EQ(test, ret, 0);
344 
345 	ret = software_node_register(&gpio_test_provider_swnode);
346 	KUNIT_ASSERT_EQ(test, ret, 0);
347 
348 	ret = kunit_add_action_or_reset(test, gpio_swnode_unregister_swnode,
349 					(void *)&gpio_test_provider_swnode);
350 	KUNIT_ASSERT_EQ(test, ret, 0);
351 
352 	properties[0] = PROPERTY_ENTRY_GPIO("foo-gpios",
353 					    &gpio_test_provider_swnode,
354 					    0, GPIO_ACTIVE_HIGH);
355 
356 	pdevinfo = (struct platform_device_info){
357 		.name = GPIO_PROBE_ORDER_TEST_CONSUMER,
358 		.id = PLATFORM_DEVID_NONE,
359 		.data = &gpio_probe_order_pdata_template,
360 		.size_data = sizeof(gpio_probe_order_pdata_template),
361 		.properties = properties,
362 	};
363 
364 	cons = kunit_platform_device_register_full(test, &pdevinfo);
365 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cons);
366 
367 	wait_for_device_probe();
368 	scoped_guard(device, &cons->dev)
369 		bound = device_is_bound(&cons->dev);
370 
371 	KUNIT_ASSERT_FALSE(test, bound);
372 
373 	pdata = dev_get_platdata(&cons->dev);
374 	KUNIT_ASSERT_EQ(test, pdata->probe_count, 0);
375 	KUNIT_ASSERT_FALSE(test, pdata->gpio_ok);
376 
377 	pdevinfo = (struct platform_device_info){
378 		.name = GPIO_TEST_PROVIDER,
379 		.id = PLATFORM_DEVID_NONE,
380 		.swnode = &gpio_test_provider_swnode,
381 	};
382 
383 	prvd = kunit_platform_device_register_full(test, &pdevinfo);
384 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, prvd);
385 
386 	wait_for_device_probe();
387 
388 	scoped_guard(device, &prvd->dev)
389 		bound = device_is_bound(&prvd->dev);
390 	KUNIT_ASSERT_TRUE(test, bound);
391 
392 	scoped_guard(device, &cons->dev)
393 		bound = device_is_bound(&cons->dev);
394 	KUNIT_ASSERT_TRUE(test, bound);
395 
396 	pdata = dev_get_platdata(&cons->dev);
397 	KUNIT_ASSERT_EQ(test, pdata->probe_count, 1);
398 	KUNIT_ASSERT_TRUE(test, pdata->gpio_ok);
399 }
400 
401 struct gpio_probe_defer_pdata {
402 	unsigned int probe_count;
403 	int gpio_err;
404 };
405 
406 static const struct gpio_probe_defer_pdata gpio_probe_defer_pdata_template = {
407 	.probe_count = 0,
408 	.gpio_err = 0,
409 };
410 
411 static int gpio_probe_defer_consumer_probe(struct platform_device *pdev)
412 {
413 	struct device *dev = &pdev->dev;
414 	struct gpio_probe_defer_pdata *pdata = dev_get_platdata(dev);
415 	struct gpio_desc *desc;
416 
417 	pdata->probe_count++;
418 
419 	desc = devm_gpiod_get(dev, "foo", GPIOD_OUT_HIGH);
420 	if (IS_ERR(desc)) {
421 		pdata->gpio_err = PTR_ERR(desc);
422 		return pdata->gpio_err;
423 	}
424 
425 	pdata->gpio_err = 0;
426 
427 	return 0;
428 }
429 
430 static struct platform_driver gpio_probe_defer_consumer_driver = {
431 	.probe = gpio_probe_defer_consumer_probe,
432 	.driver = {
433 		.name = GPIO_PROBE_DEFER_TEST_CONSUMER,
434 	},
435 };
436 
437 /*
438  * Verify that a GPIO consumer referencing a provider whose software node is
439  * not registered yet, defers its probe instead of failing.
440  *
441  * The provider software node is deliberately left unregistered when the
442  * consumer is added. fw_devlink cannot resolve the reference, so it creates no
443  * supplier link and does not order the consumer - the consumer's probe() runs
444  * and reaches devm_gpiod_get(). The swnode GPIO lookup returns -ENOTCONN for a
445  * reference to an unregistered node, which gpiolib maps to -EPROBE_DEFER. Once
446  * the provider software node and device appear, the deferred consumer probes
447  * again and binds.
448  */
449 static void gpio_swnode_probe_defer_on_unregistered(struct kunit *test)
450 {
451 	struct property_entry properties[2] = { };
452 	struct gpio_probe_defer_pdata *pdata;
453 	struct platform_device_info pdevinfo;
454 	struct platform_device *prvd, *cons;
455 	struct fwnode_handle *fwnode;
456 	bool bound = false;
457 	int ret;
458 
459 	ret = kunit_platform_driver_register(test, &gpio_test_provider_driver);
460 	KUNIT_ASSERT_EQ(test, ret, 0);
461 
462 	ret = kunit_platform_driver_register(test, &gpio_probe_defer_consumer_driver);
463 	KUNIT_ASSERT_EQ(test, ret, 0);
464 
465 	properties[0] = PROPERTY_ENTRY_GPIO("foo-gpios",
466 					    &gpio_test_provider_swnode,
467 					    0, GPIO_ACTIVE_HIGH);
468 
469 	pdevinfo = (struct platform_device_info){
470 		.name = GPIO_PROBE_DEFER_TEST_CONSUMER,
471 		.id = PLATFORM_DEVID_NONE,
472 		.data = &gpio_probe_defer_pdata_template,
473 		.size_data = sizeof(gpio_probe_defer_pdata_template),
474 		.properties = properties,
475 	};
476 
477 	cons = kunit_platform_device_register_full(test, &pdevinfo);
478 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cons);
479 
480 	wait_for_device_probe();
481 	scoped_guard(device, &cons->dev)
482 		bound = device_is_bound(&cons->dev);
483 
484 	KUNIT_ASSERT_FALSE(test, bound);
485 
486 	pdata = dev_get_platdata(&cons->dev);
487 	KUNIT_ASSERT_GT(test, pdata->probe_count, 0);
488 	KUNIT_ASSERT_EQ(test, pdata->gpio_err, -EPROBE_DEFER);
489 
490 	fwnode = kunit_software_node_register(test, &gpio_test_provider_swnode);
491 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
492 
493 	pdevinfo = (struct platform_device_info){
494 		.name = GPIO_TEST_PROVIDER,
495 		.id = PLATFORM_DEVID_NONE,
496 		.swnode = &gpio_test_provider_swnode,
497 	};
498 
499 	prvd = kunit_platform_device_register_full(test, &pdevinfo);
500 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, prvd);
501 
502 	wait_for_device_probe();
503 
504 	scoped_guard(device, &prvd->dev)
505 		bound = device_is_bound(&prvd->dev);
506 	KUNIT_ASSERT_TRUE(test, bound);
507 
508 	scoped_guard(device, &cons->dev)
509 		bound = device_is_bound(&cons->dev);
510 	KUNIT_ASSERT_TRUE(test, bound);
511 
512 	pdata = dev_get_platdata(&cons->dev);
513 	KUNIT_ASSERT_EQ(test, pdata->gpio_err, 0);
514 
515 	/* Tear down the consumer before the provider to free the GPIO. */
516 	kunit_platform_device_unregister(test, cons);
517 }
518 
519 static int gpio_swnode_probe_order_test_init(struct kunit *test)
520 {
521 	/*
522 	 * A prior test may have left a managed device link teardown queued on
523 	 * the device_link_mq. Flush it so that software_node_register()
524 	 * doesn't spuriously see the node as registered and fail with -EEXIST.
525 	 */
526 	device_link_wait_removal();
527 
528 	return 0;
529 }
530 
531 static struct kunit_case gpio_swnode_probe_order_tests[] = {
532 	KUNIT_CASE(gpio_swnode_probe_order),
533 	KUNIT_CASE(gpio_swnode_probe_defer_on_unregistered),
534 	{ }
535 };
536 
537 static struct kunit_suite gpio_swnode_probe_order_test_suite = {
538 	.name = "gpio-swnode-probe-order",
539 	.test_cases = gpio_swnode_probe_order_tests,
540 	.init = gpio_swnode_probe_order_test_init,
541 };
542 
543 static BLOCKING_NOTIFIER_HEAD(gpio_unbind_notifier);
544 
545 struct gpio_unbind_consumer_drvdata {
546 	struct device *dev;
547 	struct gpio_desc *desc;
548 	struct notifier_block nb;
549 	int set_retval;
550 };
551 
552 static int gpio_unbind_notify(struct notifier_block *nb, unsigned long action,
553 			      void *data)
554 {
555 	struct gpio_unbind_consumer_drvdata *drvdata =
556 		container_of(nb, struct gpio_unbind_consumer_drvdata, nb);
557 	struct device *dev = data;
558 
559 	if (dev != drvdata->dev)
560 		return NOTIFY_DONE;
561 
562 	drvdata->set_retval = gpiod_set_value_cansleep(drvdata->desc, 0);
563 
564 	return NOTIFY_OK;
565 }
566 
567 static void gpio_unbind_unregister_notifier(void *data)
568 {
569 	struct notifier_block *nb = data;
570 
571 	blocking_notifier_chain_unregister(&gpio_unbind_notifier, nb);
572 }
573 
574 static int gpio_unbind_consumer_probe(struct platform_device *pdev)
575 {
576 	struct gpio_unbind_consumer_drvdata *data;
577 	struct device *dev = &pdev->dev;
578 	int ret;
579 
580 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
581 	if (!data)
582 		return -ENOMEM;
583 
584 	data->dev = dev;
585 
586 	data->desc = devm_gpiod_get(dev, "foo", GPIOD_OUT_HIGH);
587 	if (IS_ERR(data->desc))
588 		return PTR_ERR(data->desc);
589 
590 	data->nb.notifier_call = gpio_unbind_notify;
591 	ret = blocking_notifier_chain_register(&gpio_unbind_notifier, &data->nb);
592 	if (ret)
593 		return ret;
594 
595 	ret = devm_add_action_or_reset(dev, gpio_unbind_unregister_notifier, &data->nb);
596 	if (ret)
597 		return ret;
598 
599 	platform_set_drvdata(pdev, data);
600 
601 	return 0;
602 }
603 
604 static struct platform_driver gpio_unbind_consumer_driver = {
605 	.probe = gpio_unbind_consumer_probe,
606 	.driver = {
607 		.name = GPIO_UNBIND_TEST_CONSUMER,
608 	},
609 };
610 
611 static void gpio_unbind_with_consumers(struct kunit *test)
612 {
613 	struct gpio_unbind_consumer_drvdata *cons_data;
614 	struct platform_device_info pdevinfo;
615 	struct property_entry properties[2];
616 	struct platform_device *prvd, *cons;
617 	bool bound = false;
618 	int ret;
619 
620 	ret = kunit_platform_driver_register(test, &gpio_test_provider_driver);
621 	KUNIT_ASSERT_EQ(test, ret, 0);
622 
623 	ret = kunit_platform_driver_register(test, &gpio_unbind_consumer_driver);
624 	KUNIT_ASSERT_EQ(test, ret, 0);
625 
626 	pdevinfo = (struct platform_device_info){
627 		.name = GPIO_TEST_PROVIDER,
628 		.id = PLATFORM_DEVID_NONE,
629 		.swnode = &gpio_test_provider_swnode,
630 	};
631 
632 	prvd = kunit_platform_device_register_full(test, &pdevinfo);
633 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, prvd);
634 
635 	properties[0] = PROPERTY_ENTRY_GPIO("foo-gpios",
636 					    &gpio_test_provider_swnode,
637 					    0, GPIO_ACTIVE_HIGH);
638 	properties[1] = (struct property_entry){ };
639 
640 	/*
641 	 * This test deliberately keeps the consumer bound while the provider
642 	 * is unregistered. fw_devlink would force-unbind the consumer before
643 	 * the provider so use the FWNODE_FLAG_LINKS_ADDED flag to opt out of
644 	 * it as a workaround.
645 	 */
646 	cons = kunit_platform_device_alloc(test, GPIO_UNBIND_TEST_CONSUMER,
647 					   PLATFORM_DEVID_NONE);
648 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cons);
649 
650 	ret = device_create_managed_software_node(&cons->dev, properties, NULL);
651 	KUNIT_ASSERT_EQ(test, ret, 0);
652 
653 	fwnode_set_flag(dev_fwnode(&cons->dev), FWNODE_FLAG_LINKS_ADDED);
654 
655 	ret = kunit_platform_device_add(test, cons);
656 	KUNIT_ASSERT_EQ(test, ret, 0);
657 
658 	wait_for_device_probe();
659 	scoped_guard(device, &cons->dev)
660 		bound = device_is_bound(&cons->dev);
661 
662 	KUNIT_ASSERT_TRUE(test, bound);
663 
664 	kunit_platform_device_unregister(test, prvd);
665 
666 	ret = blocking_notifier_call_chain(&gpio_unbind_notifier, 0, &cons->dev);
667 	KUNIT_ASSERT_EQ(test, ret, NOTIFY_OK);
668 
669 	scoped_guard(device, &cons->dev) {
670 		cons_data = platform_get_drvdata(cons);
671 		ret = cons_data->set_retval;
672 	}
673 
674 	KUNIT_ASSERT_EQ(test, ret, -ENODEV);
675 }
676 
677 static struct kunit_case gpio_unbind_with_consumers_tests[] = {
678 	KUNIT_CASE(gpio_unbind_with_consumers),
679 	{ }
680 };
681 
682 static struct kunit_suite gpio_unbind_with_consumers_test_suite = {
683 	.name = "gpio-unbind-with-consumers",
684 	.test_cases = gpio_unbind_with_consumers_tests,
685 	/* We need this here too to clean any left over links. */
686 	.init = gpio_swnode_probe_order_test_init,
687 };
688 
689 /*
690  * GPIO line hogs are described by child software nodes of the provider
691  * carrying the "gpio-hog" property. They are picked up automatically when the
692  * gpiochip is registered. Each hog below sits on a distinct line of the
693  * provider.
694  */
695 #define GPIO_HOG_OUTPUT_HIGH_OFFSET	0
696 #define GPIO_HOG_OUTPUT_LOW_OFFSET	1
697 #define GPIO_HOG_INPUT_OFFSET		2
698 
699 static const u32 gpio_hog_output_high_gpios[] = {
700 	GPIO_HOG_OUTPUT_HIGH_OFFSET, GPIO_ACTIVE_HIGH,
701 };
702 
703 static const struct property_entry gpio_hog_output_high_properties[] = {
704 	PROPERTY_ENTRY_U32_ARRAY("gpios", gpio_hog_output_high_gpios),
705 	PROPERTY_ENTRY_STRING("line-name", "hog-output-high"),
706 	PROPERTY_ENTRY_BOOL("output-high"),
707 	PROPERTY_ENTRY_BOOL("gpio-hog"),
708 	{ }
709 };
710 
711 static const struct software_node gpio_hog_output_high_swnode =
712 	SOFTWARE_NODE("hog-output-high", gpio_hog_output_high_properties,
713 		      &gpio_test_provider_swnode);
714 
715 static const u32 gpio_hog_output_low_gpios[] = {
716 	GPIO_HOG_OUTPUT_LOW_OFFSET, GPIO_ACTIVE_HIGH,
717 };
718 
719 static const struct property_entry gpio_hog_output_low_properties[] = {
720 	PROPERTY_ENTRY_U32_ARRAY("gpios", gpio_hog_output_low_gpios),
721 	PROPERTY_ENTRY_STRING("line-name", "hog-output-low"),
722 	PROPERTY_ENTRY_BOOL("output-low"),
723 	PROPERTY_ENTRY_BOOL("gpio-hog"),
724 	{ }
725 };
726 
727 static const struct software_node gpio_hog_output_low_swnode =
728 	SOFTWARE_NODE("hog-output-low", gpio_hog_output_low_properties,
729 		      &gpio_test_provider_swnode);
730 
731 static const u32 gpio_hog_input_gpios[] = {
732 	GPIO_HOG_INPUT_OFFSET, GPIO_ACTIVE_HIGH,
733 };
734 
735 static const struct property_entry gpio_hog_input_properties[] = {
736 	PROPERTY_ENTRY_U32_ARRAY("gpios", gpio_hog_input_gpios),
737 	PROPERTY_ENTRY_STRING("line-name", "hog-input"),
738 	PROPERTY_ENTRY_BOOL("input"),
739 	PROPERTY_ENTRY_BOOL("gpio-hog"),
740 	{ }
741 };
742 
743 static const struct software_node gpio_hog_input_swnode =
744 	SOFTWARE_NODE("hog-input", gpio_hog_input_properties,
745 		      &gpio_test_provider_swnode);
746 
747 static const struct software_node *const gpio_hog_swnodes[] = {
748 	&gpio_test_provider_swnode,
749 	&gpio_hog_output_high_swnode,
750 	&gpio_hog_output_low_swnode,
751 	&gpio_hog_input_swnode,
752 	NULL
753 };
754 
755 /*
756  * Bring up the provider with a single hog child registered and verify both
757  * that the line was configured with the expected direction and that it is now
758  * exclusively owned (a consumer asking for the same line fails to bind).
759  *
760  * The provider node is referenced by the device through its fwnode rather than
761  * being handed to .swnode, so the device takes no software node reference of
762  * its own. Both the provider and the hog child are therefore test-managed and
763  * torn down (child first) once the test case completes.
764  */
765 static void gpio_hog_assert(struct kunit *test, unsigned int offset,
766 			    int expected_direction)
767 {
768 	struct gpio_swnode_consumer_pdata *pdata;
769 	struct platform_device_info pdevinfo;
770 	struct property_entry properties[2];
771 	struct platform_device *pdev;
772 	struct fwnode_handle *fwnode;
773 	struct gpio_desc *desc;
774 	bool bound = true;
775 	int ret;
776 
777 	fwnode = software_node_fwnode(&gpio_test_provider_swnode);
778 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode);
779 
780 	pdevinfo = (struct platform_device_info){
781 		.name = GPIO_TEST_PROVIDER,
782 		.id = PLATFORM_DEVID_NONE,
783 		.fwnode = fwnode,
784 	};
785 
786 	pdev = kunit_platform_device_register_full(test, &pdevinfo);
787 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
788 
789 	wait_for_device_probe();
790 
791 	/* The hog must have configured the line with the expected direction. */
792 	struct gpio_device *gdev __free(gpio_device_put) =
793 		gpio_device_find_by_label(GPIO_CONSUMER_NAME);
794 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, gdev);
795 
796 	desc = gpio_device_get_desc(gdev, offset);
797 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc);
798 
799 	ret = gpiod_get_direction(desc);
800 	KUNIT_ASSERT_EQ(test, ret, expected_direction);
801 
802 	/* A hogged line is owned exclusively, so a consumer must fail to bind. */
803 	properties[0] = PROPERTY_ENTRY_GPIO("foo-gpios",
804 					    &gpio_test_provider_swnode,
805 					    offset, GPIO_ACTIVE_HIGH);
806 	properties[1] = (struct property_entry){ };
807 
808 	pdevinfo = (struct platform_device_info){
809 		.name = GPIO_SWNODE_TEST_CONSUMER,
810 		.id = PLATFORM_DEVID_NONE,
811 		.data = &gpio_swnode_pdata_template,
812 		.size_data = sizeof(gpio_swnode_pdata_template),
813 		.properties = properties,
814 	};
815 
816 	pdev = kunit_platform_device_register_full(test, &pdevinfo);
817 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
818 
819 	wait_for_device_probe();
820 	scoped_guard(device, &pdev->dev)
821 		bound = device_is_bound(&pdev->dev);
822 
823 	KUNIT_ASSERT_FALSE(test, bound);
824 
825 	pdata = dev_get_platdata(&pdev->dev);
826 	KUNIT_ASSERT_FALSE(test, pdata->gpio_ok);
827 	KUNIT_ASSERT_EQ(test, pdata->errno, -EBUSY);
828 }
829 
830 static void gpio_hog_output_high(struct kunit *test)
831 {
832 	gpio_hog_assert(test, GPIO_HOG_OUTPUT_HIGH_OFFSET, GPIO_LINE_DIRECTION_OUT);
833 }
834 
835 static void gpio_hog_output_low(struct kunit *test)
836 {
837 	gpio_hog_assert(test, GPIO_HOG_OUTPUT_LOW_OFFSET, GPIO_LINE_DIRECTION_OUT);
838 }
839 
840 static void gpio_hog_input(struct kunit *test)
841 {
842 	gpio_hog_assert(test, GPIO_HOG_INPUT_OFFSET, GPIO_LINE_DIRECTION_IN);
843 }
844 
845 static int gpio_hog_suite_init(struct kunit_suite *suite)
846 {
847 	return software_node_register_node_group(gpio_hog_swnodes);
848 }
849 
850 static void gpio_hog_suite_exit(struct kunit_suite *suite)
851 {
852 	software_node_unregister_node_group(gpio_hog_swnodes);
853 }
854 
855 static struct kunit_case gpio_swnode_hog_tests[] = {
856 	KUNIT_CASE(gpio_hog_output_high),
857 	KUNIT_CASE(gpio_hog_output_low),
858 	KUNIT_CASE(gpio_hog_input),
859 	{ }
860 };
861 
862 static struct kunit_suite gpio_swnode_hog_test_suite = {
863 	.name = "gpio-swnode-hog",
864 	.test_cases = gpio_swnode_hog_tests,
865 	.suite_init = gpio_hog_suite_init,
866 	.suite_exit = gpio_hog_suite_exit,
867 	.init = gpio_swnode_register_drivers,
868 };
869 
870 kunit_test_suites(
871 	&gpio_swnode_lookup_test_suite,
872 	&gpio_swnode_probe_order_test_suite,
873 	&gpio_unbind_with_consumers_test_suite,
874 	&gpio_swnode_hog_test_suite,
875 );
876 
877 MODULE_DESCRIPTION("Test module for the GPIO subsystem");
878 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>");
879 MODULE_LICENSE("GPL");
880