at commit c03c1a14
| 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 void gpio_unbind_unregister_notifier(void *data) |
| 570 | { |
| 571 | struct notifier_block *nb = data; |
| 572 | |
| 573 | blocking_notifier_chain_unregister(&gpio_unbind_notifier, nb); |
| 574 | } |
| 575 | |
| 576 | static int gpio_unbind_consumer_probe(struct platform_device *pdev) |
| 577 | { |
| 578 | struct gpio_unbind_consumer_drvdata *data; |
| 579 | struct device *dev = &pdev->dev; |
| 580 | int ret; |
| 581 | |
| 582 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 583 | if (!data) |
| 584 | return -ENOMEM; |
| 585 | |
| 586 | data->dev = dev; |
| 587 | |
| 588 | data->desc = devm_gpiod_get(dev, "foo", GPIOD_OUT_HIGH); |
| 589 | if (IS_ERR(data->desc)) |
| 590 | return PTR_ERR(data->desc); |
| 591 | |
| 592 | data->nb.notifier_call = gpio_unbind_notify; |
| 593 | ret = blocking_notifier_chain_register(&gpio_unbind_notifier, &data->nb); |
| 594 | if (ret) |
| 595 | return ret; |
| 596 | |
| 597 | ret = devm_add_action_or_reset(dev, gpio_unbind_unregister_notifier, &data->nb); |
| 598 | if (ret) |
| 599 | return ret; |
| 600 | |
| 601 | platform_set_drvdata(pdev, data); |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | static struct platform_driver gpio_unbind_consumer_driver = { |
| 607 | .probe = gpio_unbind_consumer_probe, |
| 608 | .driver = { |
| 609 | .name = GPIO_UNBIND_TEST_CONSUMER, |
| 610 | }, |
| 611 | }; |
| 612 | |
| 613 | static void gpio_unbind_with_consumers(struct kunit *test) |
| 614 | { |
| 615 | struct gpio_unbind_consumer_drvdata *cons_data; |
| 616 | struct platform_device_info pdevinfo; |
| 617 | struct property_entry properties[2]; |
| 618 | struct platform_device *prvd, *cons; |
| 619 | bool bound = false; |
| 620 | int ret; |
| 621 | |
| 622 | ret = kunit_platform_driver_register(test, &gpio_test_provider_driver); |
| 623 | KUNIT_ASSERT_EQ(test, ret, 0); |
| 624 | |
| 625 | ret = kunit_platform_driver_register(test, &gpio_unbind_consumer_driver); |
| 626 | KUNIT_ASSERT_EQ(test, ret, 0); |
| 627 | |
| 628 | pdevinfo = (struct platform_device_info){ |
| 629 | .name = GPIO_TEST_PROVIDER, |
| 630 | .id = PLATFORM_DEVID_NONE, |
| 631 | .swnode = &gpio_test_provider_swnode, |
| 632 | }; |
| 633 | |
| 634 | prvd = kunit_platform_device_register_full(test, &pdevinfo); |
| 635 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, prvd); |
| 636 | |
| 637 | properties[0] = PROPERTY_ENTRY_GPIO("foo-gpios", |
| 638 | &gpio_test_provider_swnode, |
| 639 | 0, GPIO_ACTIVE_HIGH); |
| 640 | properties[1] = (struct property_entry){ }; |
| 641 | |
| 642 | /* |
| 643 | * This test deliberately keeps the consumer bound while the provider |
| 644 | * is unregistered. fw_devlink would force-unbind the consumer before |
| 645 | * the provider so use the FWNODE_FLAG_LINKS_ADDED flag to opt out of |
| 646 | * it as a workaround. |
| 647 | */ |
| 648 | cons = kunit_platform_device_alloc(test, GPIO_UNBIND_TEST_CONSUMER, |
| 649 | PLATFORM_DEVID_NONE); |
| 650 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cons); |
| 651 | |
| 652 | ret = device_create_managed_software_node(&cons->dev, properties, NULL); |
| 653 | KUNIT_ASSERT_EQ(test, ret, 0); |
| 654 | |
| 655 | fwnode_set_flag(dev_fwnode(&cons->dev), FWNODE_FLAG_LINKS_ADDED); |
| 656 | |
| 657 | ret = kunit_platform_device_add(test, cons); |
| 658 | KUNIT_ASSERT_EQ(test, ret, 0); |
| 659 | |
| 660 | wait_for_device_probe(); |
| 661 | scoped_guard(device, &cons->dev) |
| 662 | bound = device_is_bound(&cons->dev); |
| 663 | |
| 664 | KUNIT_ASSERT_TRUE(test, bound); |
| 665 | |
| 666 | kunit_platform_device_unregister(test, prvd); |
| 667 | |
| 668 | ret = blocking_notifier_call_chain(&gpio_unbind_notifier, 0, &cons->dev); |
| 669 | KUNIT_ASSERT_EQ(test, ret, NOTIFY_OK); |
| 670 | |
| 671 | scoped_guard(device, &cons->dev) { |
| 672 | cons_data = platform_get_drvdata(cons); |
| 673 | ret = cons_data->set_retval; |
| 674 | } |
| 675 | |
| 676 | KUNIT_ASSERT_EQ(test, ret, -ENODEV); |
| 677 | } |
| 678 | |
| 679 | static struct kunit_case gpio_unbind_with_consumers_tests[] = { |
| 680 | KUNIT_CASE(gpio_unbind_with_consumers), |
| 681 | { } |
| 682 | }; |
| 683 | |
| 684 | static struct kunit_suite gpio_unbind_with_consumers_test_suite = { |
| 685 | .name = "gpio-unbind-with-consumers", |
| 686 | .test_cases = gpio_unbind_with_consumers_tests, |
| 687 | /* We need this here too to clean any left over links. */ |
| 688 | .init = gpio_swnode_probe_order_test_init, |
| 689 | }; |
| 690 | |
| 691 | /* |
| 692 | * GPIO line hogs are described by child software nodes of the provider |
| 693 | * carrying the "gpio-hog" property. They are picked up automatically when the |
| 694 | * gpiochip is registered. Each hog below sits on a distinct line of the |
| 695 | * provider. |
| 696 | */ |
| 697 | #define GPIO_HOG_OUTPUT_HIGH_OFFSET 0 |
| 698 | #define GPIO_HOG_OUTPUT_LOW_OFFSET 1 |
| 699 | #define GPIO_HOG_INPUT_OFFSET 2 |
| 700 | |
| 701 | static const u32 gpio_hog_output_high_gpios[] = { |
| 702 | GPIO_HOG_OUTPUT_HIGH_OFFSET, GPIO_ACTIVE_HIGH, |
| 703 | }; |
| 704 | |
| 705 | static const struct property_entry gpio_hog_output_high_properties[] = { |
| 706 | PROPERTY_ENTRY_U32_ARRAY("gpios", gpio_hog_output_high_gpios), |
| 707 | PROPERTY_ENTRY_STRING("line-name", "hog-output-high"), |
| 708 | PROPERTY_ENTRY_BOOL("output-high"), |
| 709 | PROPERTY_ENTRY_BOOL("gpio-hog"), |
| 710 | { } |
| 711 | }; |
| 712 | |
| 713 | static const struct software_node gpio_hog_output_high_swnode = |
| 714 | SOFTWARE_NODE("hog-output-high", gpio_hog_output_high_properties, |
| 715 | &gpio_test_provider_swnode); |
| 716 | |
| 717 | static const u32 gpio_hog_output_low_gpios[] = { |
| 718 | GPIO_HOG_OUTPUT_LOW_OFFSET, GPIO_ACTIVE_HIGH, |
| 719 | }; |
| 720 | |
| 721 | static const struct property_entry gpio_hog_output_low_properties[] = { |
| 722 | PROPERTY_ENTRY_U32_ARRAY("gpios", gpio_hog_output_low_gpios), |
| 723 | PROPERTY_ENTRY_STRING("line-name", "hog-output-low"), |
| 724 | PROPERTY_ENTRY_BOOL("output-low"), |
| 725 | PROPERTY_ENTRY_BOOL("gpio-hog"), |
| 726 | { } |
| 727 | }; |
| 728 | |
| 729 | static const struct software_node gpio_hog_output_low_swnode = |
| 730 | SOFTWARE_NODE("hog-output-low", gpio_hog_output_low_properties, |
| 731 | &gpio_test_provider_swnode); |
| 732 | |
| 733 | static const u32 gpio_hog_input_gpios[] = { |
| 734 | GPIO_HOG_INPUT_OFFSET, GPIO_ACTIVE_HIGH, |
| 735 | }; |
| 736 | |
| 737 | static const struct property_entry gpio_hog_input_properties[] = { |
| 738 | PROPERTY_ENTRY_U32_ARRAY("gpios", gpio_hog_input_gpios), |
| 739 | PROPERTY_ENTRY_STRING("line-name", "hog-input"), |
| 740 | PROPERTY_ENTRY_BOOL("input"), |
| 741 | PROPERTY_ENTRY_BOOL("gpio-hog"), |
| 742 | { } |
| 743 | }; |
| 744 | |
| 745 | static const struct software_node gpio_hog_input_swnode = |
| 746 | SOFTWARE_NODE("hog-input", gpio_hog_input_properties, |
| 747 | &gpio_test_provider_swnode); |
| 748 | |
| 749 | static const struct software_node *const gpio_hog_swnodes[] = { |
| 750 | &gpio_test_provider_swnode, |
| 751 | &gpio_hog_output_high_swnode, |
| 752 | &gpio_hog_output_low_swnode, |
| 753 | &gpio_hog_input_swnode, |
| 754 | NULL |
| 755 | }; |
| 756 | |
| 757 | /* |
| 758 | * Bring up the provider with a single hog child registered and verify both |
| 759 | * that the line was configured with the expected direction and that it is now |
| 760 | * exclusively owned (a consumer asking for the same line fails to bind). |
| 761 | * |
| 762 | * The provider node is referenced by the device through its fwnode rather than |
| 763 | * being handed to .swnode, so the device takes no software node reference of |
| 764 | * its own. Both the provider and the hog child are therefore test-managed and |
| 765 | * torn down (child first) once the test case completes. |
| 766 | */ |
| 767 | static void gpio_hog_assert(struct kunit *test, unsigned int offset, |
| 768 | int expected_direction) |
| 769 | { |
| 770 | struct gpio_swnode_consumer_pdata *pdata; |
| 771 | struct platform_device_info pdevinfo; |
| 772 | struct property_entry properties[2]; |
| 773 | struct platform_device *pdev; |
| 774 | struct fwnode_handle *fwnode; |
| 775 | struct gpio_device *gdev; |
| 776 | struct gpio_desc *desc; |
| 777 | bool bound = true; |
| 778 | int ret; |
| 779 | |
| 780 | fwnode = software_node_fwnode(&gpio_test_provider_swnode); |
| 781 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fwnode); |
| 782 | |
| 783 | pdevinfo = (struct platform_device_info){ |
| 784 | .name = GPIO_TEST_PROVIDER, |
| 785 | .id = PLATFORM_DEVID_NONE, |
| 786 | .fwnode = fwnode, |
| 787 | }; |
| 788 | |
| 789 | pdev = kunit_platform_device_register_full(test, &pdevinfo); |
| 790 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); |
| 791 | |
| 792 | wait_for_device_probe(); |
| 793 | |
| 794 | /* The hog must have configured the line with the expected direction. */ |
| 795 | gdev = gpio_device_find_by_label(GPIO_CONSUMER_NAME); |
| 796 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, gdev); |
| 797 | |
| 798 | ret = kunit_add_action_or_reset(test, gpio_device_put_wrapper, gdev); |
| 799 | KUNIT_ASSERT_EQ(test, ret, 0); |
| 800 | |
| 801 | desc = gpio_device_get_desc(gdev, offset); |
| 802 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc); |
| 803 | |
| 804 | ret = gpiod_get_direction(desc); |
| 805 | KUNIT_ASSERT_EQ(test, ret, expected_direction); |
| 806 | |
| 807 | /* A hogged line is owned exclusively, so a consumer must fail to bind. */ |
| 808 | properties[0] = PROPERTY_ENTRY_GPIO("foo-gpios", |
| 809 | &gpio_test_provider_swnode, |
| 810 | offset, GPIO_ACTIVE_HIGH); |
| 811 | properties[1] = (struct property_entry){ }; |
| 812 | |
| 813 | pdevinfo = (struct platform_device_info){ |
| 814 | .name = GPIO_SWNODE_TEST_CONSUMER, |
| 815 | .id = PLATFORM_DEVID_NONE, |
| 816 | .data = &gpio_swnode_pdata_template, |
| 817 | .size_data = sizeof(gpio_swnode_pdata_template), |
| 818 | .properties = properties, |
| 819 | }; |
| 820 | |
| 821 | pdev = kunit_platform_device_register_full(test, &pdevinfo); |
| 822 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); |
| 823 | |
| 824 | wait_for_device_probe(); |
| 825 | scoped_guard(device, &pdev->dev) |
| 826 | bound = device_is_bound(&pdev->dev); |
| 827 | |
| 828 | KUNIT_ASSERT_FALSE(test, bound); |
| 829 | |
| 830 | pdata = dev_get_platdata(&pdev->dev); |
| 831 | KUNIT_ASSERT_FALSE(test, pdata->gpio_ok); |
| 832 | KUNIT_ASSERT_EQ(test, pdata->errno, -EBUSY); |
| 833 | } |
| 834 | |
| 835 | static void gpio_hog_output_high(struct kunit *test) |
| 836 | { |
| 837 | gpio_hog_assert(test, GPIO_HOG_OUTPUT_HIGH_OFFSET, GPIO_LINE_DIRECTION_OUT); |
| 838 | } |
| 839 | |
| 840 | static void gpio_hog_output_low(struct kunit *test) |
| 841 | { |
| 842 | gpio_hog_assert(test, GPIO_HOG_OUTPUT_LOW_OFFSET, GPIO_LINE_DIRECTION_OUT); |
| 843 | } |
| 844 | |
| 845 | static void gpio_hog_input(struct kunit *test) |
| 846 | { |
| 847 | gpio_hog_assert(test, GPIO_HOG_INPUT_OFFSET, GPIO_LINE_DIRECTION_IN); |
| 848 | } |
| 849 | |
| 850 | static int gpio_hog_suite_init(struct kunit_suite *suite) |
| 851 | { |
| 852 | return software_node_register_node_group(gpio_hog_swnodes); |
| 853 | } |
| 854 | |
| 855 | static void gpio_hog_suite_exit(struct kunit_suite *suite) |
| 856 | { |
| 857 | software_node_unregister_node_group(gpio_hog_swnodes); |
| 858 | } |
| 859 | |
| 860 | static struct kunit_case gpio_swnode_hog_tests[] = { |
| 861 | KUNIT_CASE(gpio_hog_output_high), |
| 862 | KUNIT_CASE(gpio_hog_output_low), |
| 863 | KUNIT_CASE(gpio_hog_input), |
| 864 | { } |
| 865 | }; |
| 866 | |
| 867 | static struct kunit_suite gpio_swnode_hog_test_suite = { |
| 868 | .name = "gpio-swnode-hog", |
| 869 | .test_cases = gpio_swnode_hog_tests, |
| 870 | .suite_init = gpio_hog_suite_init, |
| 871 | .suite_exit = gpio_hog_suite_exit, |
| 872 | .init = gpio_swnode_register_drivers, |
| 873 | }; |
| 874 | |
| 875 | kunit_test_suites( |
| 876 | &gpio_swnode_lookup_test_suite, |
| 877 | &gpio_swnode_probe_order_test_suite, |
| 878 | &gpio_unbind_with_consumers_test_suite, |
| 879 | &gpio_swnode_hog_test_suite, |
| 880 | ); |
| 881 | |
| 882 | MODULE_DESCRIPTION("Test module for the GPIO subsystem"); |
| 883 | MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>"); |
| 884 | MODULE_LICENSE("GPL"); |