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