mfd: cros_ec: add Kunit tests

./tools/testing/kunit/kunit.py run \
	--arch=x86_64 \
	--kconfig_add CONFIG_CHROME_PLATFORMS=y \
	--kconfig_add CONFIG_CROS_EC=y \
	--kconfig_add CONFIG_DEBUG_KERNEL=y \
	--kconfig_add CONFIG_DEBUG_INFO_DWARF5=y \
	--raw_output=all \
	--kernel_args "no_hash_pointers" \
	cros_ec_dev*
	#--kconfig_add CONFIG_MFD_CROS_EC_DEV=y \

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index bc8be2e..0fce6eb 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -2390,5 +2390,14 @@
 	  Additional drivers must be enabled in order to use the functionality
 	  of the device.
 
+# Kunit test cases
+config MFD_KUNIT_CROS_EC_DEV_TEST
+	tristate "Kunit tests for ChromeOS EC MFD" if !KUNIT_ALL_TESTS
+	depends on KUNIT && CROS_EC
+	default KUNIT_ALL_TESTS
+	select MFD_CROS_EC_DEV
+	help
+	  Kunit tests for ChromeOS EC MFD.
+
 endmenu
 endif
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index 02b651cd..3e13ffe 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -288,3 +288,6 @@
 
 obj-$(CONFIG_MFD_RSMU_I2C)	+= rsmu_i2c.o rsmu_core.o
 obj-$(CONFIG_MFD_RSMU_SPI)	+= rsmu_spi.o rsmu_core.o
+
+# Kunit test cases
+obj-$(CONFIG_MFD_KUNIT_CROS_EC_DEV_TEST) += cros_ec_dev_test.o
diff --git a/drivers/mfd/cros_ec_dev_test.c b/drivers/mfd/cros_ec_dev_test.c
new file mode 100644
index 0000000..82dd480
--- /dev/null
+++ b/drivers/mfd/cros_ec_dev_test.c
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Kunit tests for ChromeOS Embedded Controller
+ */
+
+#include <kunit/test.h>
+
+#include <linux/platform_data/cros_ec_proto.h>
+#include <linux/platform_device.h>
+
+struct cros_ec_dev_test_priv {
+	struct cros_ec_device ec_dev;
+};
+
+static struct cros_ec_platform ec_p = {
+	.ec_name = "fake",
+	.cmd_offset = 0,
+};
+
+static void cros_ec_dev_test_platform_data_constantly(struct kunit *test)
+{
+	struct cros_ec_dev_test_priv *priv = test->priv;
+	struct cros_ec_device *ec_dev = &priv->ec_dev;
+	struct platform_device *pdev1;
+
+	pdev1 = platform_device_register_data(ec_dev->dev, "cros-ec-dev",
+			PLATFORM_DEVID_AUTO, &ec_p, sizeof(struct cros_ec_platform));
+
+	KUNIT_ASSERT_STREQ(test, ec_p.ec_name, "fake");
+
+	platform_device_unregister(pdev1);
+}
+
+static void cros_ec_dev_test_release(struct device *dev)
+{
+}
+
+static int fake_pkt_xfer(struct cros_ec_device *ec, struct cros_ec_command *msg)
+{
+	if (msg->command == EC_CMD_GET_FEATURES) {
+		struct ec_response_get_features *features = (struct ec_response_get_features *) msg->data;
+		features->flags[0] = BIT(EC_FEATURE_FINGERPRINT);
+	}
+	return 0;
+}
+
+static int cros_ec_dev_test_init(struct kunit *test)
+{
+	struct cros_ec_dev_test_priv *priv;
+	struct cros_ec_device *ec_dev;
+
+	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+	test->priv = priv;
+
+	ec_dev = &priv->ec_dev;
+	ec_dev->dev = kunit_kzalloc(test, sizeof(*ec_dev->dev), GFP_KERNEL);
+	if (!ec_dev->dev)
+		return -ENOMEM;
+
+	ec_dev->dev->init_name = "fake_parent";
+	ec_dev->dev->release = cros_ec_dev_test_release;
+	ec_dev->pkt_xfer = fake_pkt_xfer;
+	ec_dev->proto_version = 3;
+	dev_set_drvdata(ec_dev->dev, ec_dev);
+
+	return device_register(ec_dev->dev);
+}
+
+static void cros_ec_dev_test_exit(struct kunit *test)
+{
+	struct cros_ec_dev_test_priv *priv = test->priv;
+	struct cros_ec_device *ec_dev = &priv->ec_dev;
+
+	put_device(ec_dev->dev);
+}
+
+static struct kunit_case cros_ec_dev_test_cases[] = {
+	KUNIT_CASE(cros_ec_dev_test_platform_data_constantly),
+	{}
+};
+
+static struct kunit_suite cros_ec_dev_test_suite = {
+	.name = "cros_ec_dev_test",
+	.init = cros_ec_dev_test_init,
+	.exit = cros_ec_dev_test_exit,
+	.test_cases = cros_ec_dev_test_cases,
+};
+kunit_test_suite(cros_ec_dev_test_suite);
+
+MODULE_DESCRIPTION("Kunit tests for ChromeOS Embedded Controller");
+MODULE_LICENSE("GPL");