xref: /linux/drivers/leds/led-test.c (revision eb58933b78cdad9b879dc2dd248e7284341a1cfc)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2025 Google LLC
4  *
5  * Author: Lee Jones <lee@kernel.org>
6  */
7 
8 #include <kunit/device.h>
9 #include <kunit/test.h>
10 #include <linux/device.h>
11 #include <linux/leds.h>
12 
13 #define LED_TEST_POST_REG_BRIGHTNESS 10
14 
15 struct led_test_ddata {
16 	struct led_classdev cdev;
17 	struct device *dev;
18 };
19 
20 static enum led_brightness led_test_brightness_get(struct led_classdev *cdev)
21 {
22 	return LED_TEST_POST_REG_BRIGHTNESS;
23 }
24 
25 static void led_test_class_register(struct kunit *test)
26 {
27 	struct led_test_ddata *ddata = test->priv;
28 	struct led_classdev *cdev_clash, *cdev = &ddata->cdev;
29 	struct device *dev = ddata->dev;
30 	int ret;
31 
32 	/* Register a LED class device */
33 	cdev->name = "led-test";
34 	cdev->brightness_get = led_test_brightness_get;
35 	cdev->brightness = 0;
36 
37 	ret = devm_led_classdev_register(dev, cdev);
38 	KUNIT_ASSERT_EQ(test, ret, 0);
39 
40 	KUNIT_EXPECT_EQ(test, cdev->max_brightness, LED_FULL);
41 	KUNIT_EXPECT_EQ(test, cdev->brightness, LED_TEST_POST_REG_BRIGHTNESS);
42 	KUNIT_EXPECT_STREQ(test, cdev->dev->kobj.name, "led-test");
43 
44 	/* Register again with the same name - expect it to pass with the LED renamed */
45 	cdev_clash = devm_kmemdup(dev, cdev, sizeof(*cdev), GFP_KERNEL);
46 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cdev_clash);
47 
48 	ret = devm_led_classdev_register(dev, cdev_clash);
49 	KUNIT_ASSERT_EQ(test, ret, 0);
50 
51 	KUNIT_EXPECT_STREQ(test, cdev_clash->dev->kobj.name, "led-test_1");
52 	KUNIT_EXPECT_STREQ(test, cdev_clash->name, "led-test");
53 
54 	/* Enable name conflict rejection and register with the same name again - expect failure */
55 	cdev_clash->flags |= LED_REJECT_NAME_CONFLICT;
56 	ret = devm_led_classdev_register(dev, cdev_clash);
57 	KUNIT_EXPECT_EQ(test, ret, -EEXIST);
58 }
59 
60 static struct kunit_case led_test_cases[] = {
61 	KUNIT_CASE(led_test_class_register),
62 	{ }
63 };
64 
65 static int led_test_init(struct kunit *test)
66 {
67 	struct led_test_ddata *ddata;
68 	struct device *dev;
69 
70 	ddata = kunit_kzalloc(test, sizeof(*ddata), GFP_KERNEL);
71 	if (!ddata)
72 		return -ENOMEM;
73 
74 	test->priv = ddata;
75 
76 	dev = kunit_device_register(test, "led_test");
77 	if (IS_ERR(dev))
78 		return PTR_ERR(dev);
79 
80 	ddata->dev = get_device(dev);
81 
82 	return 0;
83 }
84 
85 static void led_test_exit(struct kunit *test)
86 {
87 	struct led_test_ddata *ddata = test->priv;
88 
89 	if (ddata && ddata->dev)
90 		put_device(ddata->dev);
91 }
92 
93 static struct kunit_suite led_test_suite = {
94 	.name = "led",
95 	.init = led_test_init,
96 	.exit = led_test_exit,
97 	.test_cases = led_test_cases,
98 };
99 kunit_test_suite(led_test_suite);
100 
101 MODULE_AUTHOR("Lee Jones <lee@kernel.org>");
102 MODULE_DESCRIPTION("KUnit tests for the LED framework");
103 MODULE_LICENSE("GPL");
104