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