From patchwork Mon Mar 27 22:21:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 667616 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18225C76195 for ; Mon, 27 Mar 2023 22:22:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230432AbjC0WWG (ORCPT ); Mon, 27 Mar 2023 18:22:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230412AbjC0WWG (ORCPT ); Mon, 27 Mar 2023 18:22:06 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9148B19AB; Mon, 27 Mar 2023 15:22:04 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 43834B819A5; Mon, 27 Mar 2023 22:22:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 624DDC4339C; Mon, 27 Mar 2023 22:22:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679955721; bh=36NmJOfvymqOUa86Xso6Y3noQoP8kgcFeJ65vzin3Zw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A07LNRrkcsI2/KLWfm9DfgHmIVnCrLxs45YeWwY9JyI55diZsluhyqXJiofBBLI57 aktf1oFcKlpX3nR6zKUa7dsvuujFKsbc2am0m0bsuNynboAvz9A2uF5Pgx4fPnCqhQ 5vDfm78cXE3aFkVxSGDm0YuC2mx+L8HHQ2Rl2XYQ4KLK3UkFKBDQ0HHOprF6hZw+kT 8rifNS8tCdUr7/rYxOvcuVPUnmzh9+AEJLym5SDcnSCzBplVH5WPHsyqZ09WDwvynG yoAPuEyjhkq9P1ZygfxcdTdTYWgDZG2M7Z4lqBsK+ICXLukiyXA83Pai+A4eLgSZab xV2GiLqGaXTUA== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, Maxime Ripard Subject: [PATCH v3 01/11] of: Add KUnit test to confirm DTB is loaded Date: Mon, 27 Mar 2023 15:21:49 -0700 Message-Id: <20230327222159.3509818-2-sboyd@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230327222159.3509818-1-sboyd@kernel.org> References: <20230327222159.3509818-1-sboyd@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Add a simple KUnit test that confirms a DTB has been loaded, i.e. there is a root node. Cc: Rob Herring Cc: Frank Rowand Signed-off-by: Stephen Boyd --- drivers/of/.kunitconfig | 3 +++ drivers/of/Kconfig | 9 +++++++++ drivers/of/Makefile | 2 ++ drivers/of/of_test.c | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 drivers/of/.kunitconfig create mode 100644 drivers/of/of_test.c diff --git a/drivers/of/.kunitconfig b/drivers/of/.kunitconfig new file mode 100644 index 000000000000..5a8fee11978c --- /dev/null +++ b/drivers/of/.kunitconfig @@ -0,0 +1,3 @@ +CONFIG_KUNIT=y +CONFIG_OF=y +CONFIG_OF_KUNIT_TEST=y diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig index 194e090ceee8..1b995cecf5be 100644 --- a/drivers/of/Kconfig +++ b/drivers/of/Kconfig @@ -37,6 +37,15 @@ config OF_UNITTEST If unsure, say N here. This option is not safe to enable. +config OF_KUNIT_TEST + tristate "Devicetree KUnit DTB Test" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + This option builds KUnit unit tests for device tree infrastructure. + + If unsure, say N here, but this option is safe to enable. + config OF_ALL_DTBS bool "Build all Device Tree Blobs" depends on COMPILE_TEST diff --git a/drivers/of/Makefile b/drivers/of/Makefile index cbae92c5ed02..b11a8adcb985 100644 --- a/drivers/of/Makefile +++ b/drivers/of/Makefile @@ -19,4 +19,6 @@ obj-y += kexec.o endif endif +obj-$(CONFIG_OF_KUNIT_TEST) += of_test.o + obj-$(CONFIG_OF_UNITTEST) += unittest-data/ diff --git a/drivers/of/of_test.c b/drivers/of/of_test.c new file mode 100644 index 000000000000..08b670aee083 --- /dev/null +++ b/drivers/of/of_test.c @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for OF APIs + */ +#include +#include + +#include + +/* + * Test that the root node / exists. + */ +static void dtb_root_node_exists(struct kunit *test) +{ + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, of_find_node_by_path("/")); +} + +static struct kunit_case dtb_test_cases[] = { + KUNIT_CASE(dtb_root_node_exists), + {} +}; + +/* + * Test suite to confirm a live DTB is loaded. + */ +static struct kunit_suite dtb_suite = { + .name = "dtb", + .test_cases = dtb_test_cases, +}; + +kunit_test_suites( + &dtb_suite, +); +MODULE_LICENSE("GPL"); From patchwork Mon Mar 27 22:21:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 667615 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74771C6FD1D for ; Mon, 27 Mar 2023 22:22:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231960AbjC0WWN (ORCPT ); Mon, 27 Mar 2023 18:22:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231437AbjC0WWG (ORCPT ); Mon, 27 Mar 2023 18:22:06 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA74735BD; Mon, 27 Mar 2023 15:22:05 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 8E926B8196F; Mon, 27 Mar 2023 22:22:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B6A99C433AA; Mon, 27 Mar 2023 22:22:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679955723; bh=EFxqrYszB/6SHqjnakwbj0FJzNzPv1cwpyJhojl6Aek=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=seEnY6KQl6OKggp/R3pZ6WZZsG51tUXc4GyxEX8f68nL6T6LEZev7olEPyETBQHW+ HmTUPMRQDmcOTBPMeH/jCQ9EVZ8b6o/xYUoHT0BkueWKZIhtZnJUrYfm2M7D+A5AWD PdOKcwdWZJfetSV00UO4H9rzkiFQX0ygbbDJfkNsr2XgpMNRiFbGiLZR0guhtMRVTP QuNqqy21MkK4Pug19K3e6QmxQhWULFgq4p+0aKPggxi0YnkxpKO69PnEKQ5LiB3tT/ gGYvnrsP9DlmAT9Sdg9IrQvZaa5FQKnhAyriruWsYpdrQVUTTySfF4N1iZOg+EiW6i scYvVgomQu85Q== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, Maxime Ripard Subject: [PATCH v3 03/11] dt-bindings: vendor-prefixes: Add "test" vendor for KUnit and friends Date: Mon, 27 Mar 2023 15:21:51 -0700 Message-Id: <20230327222159.3509818-4-sboyd@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230327222159.3509818-1-sboyd@kernel.org> References: <20230327222159.3509818-1-sboyd@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Add the vendor prefix "test" to reserve a vendor prefix for bindings that are purely for testing device tree code. This allows test code to write bindings that can be checked by the schema validator. Cc: Rob Herring Cc: Krzysztof Kozlowski Signed-off-by: Stephen Boyd --- Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Documentation/devicetree/bindings/vendor-prefixes.yaml b/Documentation/devicetree/bindings/vendor-prefixes.yaml index ed64e06ecca4..e50a78ec7344 100644 --- a/Documentation/devicetree/bindings/vendor-prefixes.yaml +++ b/Documentation/devicetree/bindings/vendor-prefixes.yaml @@ -1331,6 +1331,8 @@ patternProperties: description: Terasic Inc. "^tesla,.*": description: Tesla, Inc. + "^test,.*": + description: Reserved for use by tests. For example, KUnit. "^tfc,.*": description: Three Five Corp "^thead,.*": From patchwork Mon Mar 27 22:21:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 667612 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DF2AC77B74 for ; Mon, 27 Mar 2023 22:22:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232256AbjC0WWV (ORCPT ); Mon, 27 Mar 2023 18:22:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229512AbjC0WWM (ORCPT ); Mon, 27 Mar 2023 18:22:12 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4770F19AB; Mon, 27 Mar 2023 15:22:06 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BCE3661530; Mon, 27 Mar 2023 22:22:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A78EEC433EF; Mon, 27 Mar 2023 22:22:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679955725; bh=/DEIr7Sw4DkVY0Y+8LTV1B/3eOTXIenriA7lqf+vjdE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ccp6tZNpaVST7S09hkPaQmZWYbNypUO2C1rx9xj2/t4Gam6MjVj53v+TftwvFXdCS zcadXlZ20zcUCSj+leSlzWBx0yqZk96MVhdjDHgwP3FUOLDVnyOwSmrJPQKGzLYAuw /jMYlxCPl+X86AC/SHpXzQ63gyEdOm8I4Rqj07yA1Jd864u1AK/UWLwdzxjrvEYVAv yKvDmdkAsoqXVQ6rydBY04H1YCIg8mfcTq2SIpGR/2ZfOTt85yqswFzkhqGRIQIiUz oD++n+3Wc4iWDqRZk1PUxyfxtOFuRCpRzczGSJ9B/EME+IGO0/j3gTM9aatEfHR/Nz xapxE3NQaq2uw== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, Maxime Ripard Subject: [PATCH v3 06/11] platform: Add test managed platform_device/driver APIs Date: Mon, 27 Mar 2023 15:21:54 -0700 Message-Id: <20230327222159.3509818-7-sboyd@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230327222159.3509818-1-sboyd@kernel.org> References: <20230327222159.3509818-1-sboyd@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Introduce KUnit resource wrappers around platform_driver_register(), platform_device_alloc(), and platform_device_add() so that test authors can register platform drivers/devices from their tests and have the drivers/devices automatically be unregistered when the test is done. This makes test setup code simpler when a platform driver or platform device is needed. Add a few test cases at the same time to make sure the APIs work as intended. Cc: Brendan Higgins Cc: David Gow Cc: Greg Kroah-Hartman Cc: "Rafael J. Wysocki" Signed-off-by: Stephen Boyd --- Documentation/dev-tools/kunit/api/index.rst | 5 + .../dev-tools/kunit/api/platformdevice.rst | 10 + drivers/base/test/Makefile | 3 + drivers/base/test/platform_kunit-test.c | 140 ++++++++++++ drivers/base/test/platform_kunit.c | 215 ++++++++++++++++++ include/kunit/platform_device.h | 15 ++ 6 files changed, 388 insertions(+) create mode 100644 Documentation/dev-tools/kunit/api/platformdevice.rst create mode 100644 drivers/base/test/platform_kunit-test.c create mode 100644 drivers/base/test/platform_kunit.c create mode 100644 include/kunit/platform_device.h diff --git a/Documentation/dev-tools/kunit/api/index.rst b/Documentation/dev-tools/kunit/api/index.rst index 5eb5f76e532c..eb628cb2c72a 100644 --- a/Documentation/dev-tools/kunit/api/index.rst +++ b/Documentation/dev-tools/kunit/api/index.rst @@ -10,6 +10,7 @@ API Reference resource functionredirection of + platformdevice This page documents the KUnit kernel testing API. It is divided into the @@ -37,3 +38,7 @@ Documentation/dev-tools/kunit/api/of.rst - Documents the KUnit device tree (OF) API +Documentation/dev-tools/kunit/api/platformdevice.rst + + - Documents the KUnit platform device API + diff --git a/Documentation/dev-tools/kunit/api/platformdevice.rst b/Documentation/dev-tools/kunit/api/platformdevice.rst new file mode 100644 index 000000000000..b228fb6558c2 --- /dev/null +++ b/Documentation/dev-tools/kunit/api/platformdevice.rst @@ -0,0 +1,10 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=================== +Platform Device API +=================== + +The KUnit platform device API is used to test platform devices. + +.. kernel-doc:: drivers/base/test/platform_kunit.c + :export: diff --git a/drivers/base/test/Makefile b/drivers/base/test/Makefile index 7f76fee6f989..9edebaac4e75 100644 --- a/drivers/base/test/Makefile +++ b/drivers/base/test/Makefile @@ -3,3 +3,6 @@ obj-$(CONFIG_TEST_ASYNC_DRIVER_PROBE) += test_async_driver_probe.o obj-$(CONFIG_DRIVER_PE_KUNIT_TEST) += property-entry-test.o CFLAGS_property-entry-test.o += $(DISABLE_STRUCTLEAK_PLUGIN) + +obj-$(CONFIG_KUNIT) += platform_kunit.o +obj-$(CONFIG_KUNIT_TEST) += platform_kunit-test.o diff --git a/drivers/base/test/platform_kunit-test.c b/drivers/base/test/platform_kunit-test.c new file mode 100644 index 000000000000..36bf5e579c98 --- /dev/null +++ b/drivers/base/test/platform_kunit-test.c @@ -0,0 +1,140 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for platform driver infrastructure. + */ + +#include + +#include +#include + +static const char * const kunit_devname = "kunit-platform"; + +/* + * Test that platform_device_alloc_kunit() creates a platform device. + */ +static void platform_device_alloc_kunit_test(struct kunit *test) +{ + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, + platform_device_alloc_kunit(test, kunit_devname, 1)); +} + +/* + * Test that platform_device_add_kunit() registers a platform device on the + * platform bus with the proper name and id. + */ +static void platform_device_add_kunit_test(struct kunit *test) +{ + struct platform_device *pdev; + const char *name = kunit_devname; + const int id = -1; + + pdev = platform_device_alloc_kunit(test, name, id); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + + KUNIT_EXPECT_EQ(test, 0, platform_device_add_kunit(test, pdev)); + KUNIT_EXPECT_TRUE(test, dev_is_platform(&pdev->dev)); + KUNIT_EXPECT_STREQ(test, pdev->name, name); + KUNIT_EXPECT_EQ(test, pdev->id, id); +} + +/* + * Test that platform_device_add_kunit() called twice with the same device name + * and id fails the second time and properly cleans up. + */ +static void platform_device_add_kunit_2_test(struct kunit *test) +{ + struct platform_device *pdev; + const char *name = kunit_devname; + const int id = -1; + + pdev = platform_device_alloc_kunit(test, name, id); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + KUNIT_ASSERT_EQ(test, 0, platform_device_add_kunit(test, pdev)); + + pdev = platform_device_alloc_kunit(test, name, id); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + + KUNIT_EXPECT_NE(test, 0, platform_device_add_kunit(test, pdev)); +} + +/* + * Test suite for struct platform_device kunit APIs + */ +static struct kunit_case platform_device_kunit_test_cases[] = { + KUNIT_CASE(platform_device_alloc_kunit_test), + KUNIT_CASE(platform_device_add_kunit_test), + KUNIT_CASE(platform_device_add_kunit_2_test), + {} +}; + +static struct kunit_suite platform_device_kunit_suite = { + .name = "platform_device_kunit", + .test_cases = platform_device_kunit_test_cases, +}; + +struct kunit_platform_driver_test_context { + struct platform_driver pdrv; + const char *data; +}; + +static const char * const test_data = "test data"; + +static inline struct kunit_platform_driver_test_context * +to_test_context(struct platform_device *pdev) +{ + return container_of(to_platform_driver(pdev->dev.driver), + struct kunit_platform_driver_test_context, + pdrv); +} + +static int kunit_platform_driver_probe(struct platform_device *pdev) +{ + struct kunit_platform_driver_test_context *ctx; + + ctx = to_test_context(pdev); + ctx->data = test_data; + + return 0; +} + +/* Test that platform_driver_register_kunit() registers a driver that probes. */ +static void platform_driver_register_kunit_test(struct kunit *test) +{ + struct platform_device *pdev; + struct kunit_platform_driver_test_context *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + + pdev = platform_device_alloc_kunit(test, kunit_devname, -1); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + KUNIT_ASSERT_EQ(test, 0, platform_device_add_kunit(test, pdev)); + + ctx->pdrv.probe = kunit_platform_driver_probe; + ctx->pdrv.driver.name = kunit_devname; + ctx->pdrv.driver.owner = THIS_MODULE; + + KUNIT_EXPECT_EQ(test, 0, platform_driver_register_kunit(test, &ctx->pdrv)); + KUNIT_EXPECT_STREQ(test, ctx->data, test_data); +} + +static struct kunit_case platform_driver_kunit_test_cases[] = { + KUNIT_CASE(platform_driver_register_kunit_test), + {} +}; + +/* + * Test suite for struct platform_driver kunit APIs + */ +static struct kunit_suite platform_driver_kunit_suite = { + .name = "platform_driver_kunit", + .test_cases = platform_driver_kunit_test_cases, +}; + +kunit_test_suites( + &platform_device_kunit_suite, + &platform_driver_kunit_suite, +); + +MODULE_LICENSE("GPL"); diff --git a/drivers/base/test/platform_kunit.c b/drivers/base/test/platform_kunit.c new file mode 100644 index 000000000000..ed85ed98197a --- /dev/null +++ b/drivers/base/test/platform_kunit.c @@ -0,0 +1,215 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test managed platform driver + */ + +#include +#include + +#include +#include + +struct platform_device_alloc_params_kunit { + const char *name; + int id; +}; + +static int platform_device_alloc_kunit_init(struct kunit_resource *res, void *context) +{ + struct platform_device_alloc_params_kunit *params = context; + struct platform_device *pdev; + + pdev = platform_device_alloc(params->name, params->id); + if (!pdev) + return -ENOMEM; + + res->data = pdev; + + return 0; +} + +static void platform_device_alloc_kunit_exit(struct kunit_resource *res) +{ + struct platform_device *pdev = res->data; + + platform_device_put(pdev); +} + +/** + * platform_device_alloc_kunit() - Allocate a KUnit test managed platform device + * @test: test context + * @name: device name of platform device to alloc + * @id: identifier of platform device to alloc. + * + * Allocate a test managed platform device. The device is put when the test completes. + * + * Returns: Allocated platform device on success, NULL on failure. + */ +struct platform_device * +platform_device_alloc_kunit(struct kunit *test, const char *name, int id) +{ + struct platform_device_alloc_params_kunit params = { + .name = name, + .id = id, + }; + + return kunit_alloc_resource(test, + platform_device_alloc_kunit_init, + platform_device_alloc_kunit_exit, + GFP_KERNEL, ¶ms); +} +EXPORT_SYMBOL_GPL(platform_device_alloc_kunit); + +static void platform_device_add_kunit_exit(struct kunit_resource *res) +{ + struct platform_device *pdev = res->data; + + platform_device_unregister(pdev); +} + +static bool +platform_device_alloc_kunit_match(struct kunit *test, + struct kunit_resource *res, void *match_data) +{ + struct platform_device *pdev = match_data; + + return res->data == pdev; +} + +/** + * platform_device_add_kunit() - Register a KUnit test managed platform device + * @test: test context + * @pdev: platform device to add + * + * Register a test managed platform device. The device is unregistered when the + * test completes. + * + * Returns: 0 on success, negative errno on failure. + */ +int platform_device_add_kunit(struct kunit *test, struct platform_device *pdev) +{ + struct kunit_resource *res; + int ret; + + ret = platform_device_add(pdev); + if (ret) + return ret; + + res = kunit_find_resource(test, platform_device_alloc_kunit_match, pdev); + if (res) { + /* + * Transfer the reference count of the platform device if it was + * allocated with platform_device_alloc_kunit(). In that case, + * calling platform_device_put() leads to reference count + * underflow because platform_device_unregister() does it for + * us and we call platform_device_unregister() from + * platform_device_add_kunit_exit(). + * + * Usually callers transfer the refcount from + * platform_device_alloc() to platform_device_add() and simply + * call platform_device_unregister() when done, but with kunit + * we have to keep this straight by redirecting the free + * routine for the resource. + */ + res->free = platform_device_add_kunit_exit; + kunit_put_resource(res); + } else { + if (!kunit_alloc_resource(test, + NULL, + platform_device_add_kunit_exit, + GFP_KERNEL, pdev)) { + platform_device_unregister(pdev); + return -ENOMEM; + } + } + + return 0; +} +EXPORT_SYMBOL_GPL(platform_device_add_kunit); + +static void platform_driver_register_kunit_exit(struct kunit_resource *res) +{ + struct platform_driver *drv = res->data; + + platform_driver_unregister(drv); +} + +/** + * platform_driver_register_kunit() - Register a KUnit test managed platform driver + * @test: test context + * @drv: platform driver to register + * + * Register a test managed platform driver. This allows callers to embed the + * @drv in a container structure and use container_of() in the probe function + * to pass information to KUnit tests. It can be assumed that the driver has + * probed when this function returns. + * + * Example + * + * .. code-block:: c + * + * struct kunit_test_context { + * struct platform_driver pdrv; + * const char *data; + * }; + * + * static inline struct kunit_test_context * + * to_test_context(struct platform_device *pdev) + * { + * return container_of(to_platform_driver(pdev->dev.driver), + * struct kunit_test_context, + * pdrv); + * } + * + * static int kunit_platform_driver_probe(struct platform_device *pdev) + * { + * struct kunit_test_context *ctx; + * + * ctx = to_test_context(pdev); + * ctx->data = "test data"; + * + * return 0; + * } + * + * static void kunit_platform_driver_test(struct kunit *test) + * { + * struct kunit_test_context *ctx; + * + * ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + * + * ctx->pdrv.probe = kunit_platform_driver_probe; + * ctx->pdrv.driver.name = "kunit-platform"; + * ctx->pdrv.driver.owner = THIS_MODULE; + * + * KUNIT_EXPECT_EQ(test, 0, platform_driver_register_kunit(test, &ctx->pdrv)); + * KUNIT_EXPECT_STREQ(test, ctx->data, "test data"); + * } + * + * Returns: 0 on success, negative errno on failure. + */ +int platform_driver_register_kunit(struct kunit *test, + struct platform_driver *drv) +{ + int ret; + + ret = platform_driver_register(drv); + if (ret) + return ret; + + /* + * Wait for the driver to probe (or at least flush out of the deferred + * workqueue) + */ + wait_for_device_probe(); + + if (!kunit_alloc_resource(test, NULL, + platform_driver_register_kunit_exit, + GFP_KERNEL, drv)) { + platform_driver_unregister(drv); + return -ENOMEM; + } + + return 0; +} +EXPORT_SYMBOL_GPL(platform_driver_register_kunit); diff --git a/include/kunit/platform_device.h b/include/kunit/platform_device.h new file mode 100644 index 000000000000..28d28abf15a4 --- /dev/null +++ b/include/kunit/platform_device.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _KUNIT_PLATFORM_DRIVER_H +#define _KUNIT_PLATFORM_DRIVER_H + +struct kunit; +struct platform_device; +struct platform_driver; + +struct platform_device * +platform_device_alloc_kunit(struct kunit *test, const char *name, int id); +int platform_device_add_kunit(struct kunit *test, struct platform_device *pdev); + +int platform_driver_register_kunit(struct kunit *test, struct platform_driver *drv); + +#endif From patchwork Mon Mar 27 22:21:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 667614 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B6C7BC77B6E for ; Mon, 27 Mar 2023 22:22:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232126AbjC0WWQ (ORCPT ); Mon, 27 Mar 2023 18:22:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231820AbjC0WWH (ORCPT ); Mon, 27 Mar 2023 18:22:07 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5E6C3A82; Mon, 27 Mar 2023 15:22:06 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 61CE56153A; Mon, 27 Mar 2023 22:22:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 561C4C433D2; Mon, 27 Mar 2023 22:22:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679955725; bh=RvlyhYbptvBg73D2OHECbEteP6t+nqh7GcOpf9v1aEY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uTkAg0rxKzfKqWsxy/FOCMxmUQg3ulNMN7su2LSfBwnIWJH+cY+IuBsNdb48eKH+l klaU/1veZvXiRMRmE8Wo2gbKiR08CX0mwFF+xGTdTH/2R7z6Nwex+/fQLrp6x1mBck mllrbD5qEcHyiol48C94pNBlNwrvEV5PdsUo+ffPxQWH/7aTZyg6meW3ur4grkxVMD TUiW+jZCQWc/E1/KjsusQ/mmmlckkZYge9uAY1zVDnNStZKgOebs6KL8GNOsimVfEj NO4Vv9NhUBdneNnCZ2wYMxGxn1qleA9zlERC0S0dNKwdTrSoVRw6TzTdvmhDqXiAok BbWvzTPU2+fLQ== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, Maxime Ripard Subject: [PATCH v3 07/11] dt-bindings: kunit: Add fixed rate clk consumer test Date: Mon, 27 Mar 2023 15:21:55 -0700 Message-Id: <20230327222159.3509818-8-sboyd@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230327222159.3509818-1-sboyd@kernel.org> References: <20230327222159.3509818-1-sboyd@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Describe a binding for a device that consumes a fixed rate clk in DT so that a KUnit test can get the clk registered by of_fixed_clk_setup() and test that it is setup properly. Cc: Rob Herring Cc: Krzysztof Kozlowski Cc: Brendan Higgins Cc: David Gow Signed-off-by: Stephen Boyd --- .../bindings/test/test,clk-fixed-rate.yaml | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Documentation/devicetree/bindings/test/test,clk-fixed-rate.yaml diff --git a/Documentation/devicetree/bindings/test/test,clk-fixed-rate.yaml b/Documentation/devicetree/bindings/test/test,clk-fixed-rate.yaml new file mode 100644 index 000000000000..b9f58cba944c --- /dev/null +++ b/Documentation/devicetree/bindings/test/test,clk-fixed-rate.yaml @@ -0,0 +1,35 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/test/test,clk-fixed-rate.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: KUnit clk fixed rate test clk consumer + +maintainers: + - Stephen Boyd + +description: + A clk consumer of a fixed rate clk used to test the fixed rate clk + implementation. + +properties: + compatible: + const: test,clk-fixed-rate + + clocks: + maxItems: 1 + +required: + - compatible + - clocks + +additionalProperties: false + +examples: + - | + clock-consumer { + compatible = "test,clk-fixed-rate"; + clocks = <&fixed_clk>; + }; +... From patchwork Mon Mar 27 22:21:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 667613 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6B3EC77B72 for ; Mon, 27 Mar 2023 22:22:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232231AbjC0WWS (ORCPT ); Mon, 27 Mar 2023 18:22:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232060AbjC0WWP (ORCPT ); Mon, 27 Mar 2023 18:22:15 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 289723C22; Mon, 27 Mar 2023 15:22:08 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id AD1BD614BE; Mon, 27 Mar 2023 22:22:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9AB7CC433A1; Mon, 27 Mar 2023 22:22:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679955727; bh=lRMOO2E269k+cX4x6/50yH0eLshKrFNHYJq+jxmM/IE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mJxv4IHb09vIvbMHEhD4jTmo/jgb+KtRqOr4g9NZWjaPO0yCaKR2qL+HXiclsaP2h RlQRmNdm98z38ptG1HF1NFdBafcDJxVZoo3zarIJF1xouo40RnLEgWv9QqHT4TZlyJ llJrdbyVOxKgi6/jrwvZJHVbyITihDx8rDNxsAlQXQBO+Iyxs4Da3zsyGuY1js5/rz F+LLcTfvpHxhHIXkoHmuABZtug9S8xIzYTBCjx2XE89Xy5azBd3+cBc9qAjYqM0ajk 1EG/IVgyyhhMUIySrB4Sa1vOO7zrOs4JXorbBgVNlyIQp1v/uyhHjJYPQBRq7VzC/B +zdu595iRtYBw== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, Maxime Ripard Subject: [PATCH v3 09/11] clk: Add KUnit tests for clk fixed rate basic type Date: Mon, 27 Mar 2023 15:21:57 -0700 Message-Id: <20230327222159.3509818-10-sboyd@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230327222159.3509818-1-sboyd@kernel.org> References: <20230327222159.3509818-1-sboyd@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Test that the fixed rate basic type clk works as intended. Cc: Brendan Higgins Cc: David Gow Signed-off-by: Stephen Boyd --- drivers/clk/.kunitconfig | 3 + drivers/clk/Kconfig | 7 + drivers/clk/Makefile | 1 + drivers/clk/clk-fixed-rate_test.c | 374 +++++++++++++++++++++ drivers/clk/clk-fixed-rate_test.h | 8 + drivers/clk/kunit_clk_fixed_rate_test.dtso | 19 ++ 6 files changed, 412 insertions(+) create mode 100644 drivers/clk/clk-fixed-rate_test.c create mode 100644 drivers/clk/clk-fixed-rate_test.h create mode 100644 drivers/clk/kunit_clk_fixed_rate_test.dtso diff --git a/drivers/clk/.kunitconfig b/drivers/clk/.kunitconfig index 2fbeb71316f8..5faf67773b74 100644 --- a/drivers/clk/.kunitconfig +++ b/drivers/clk/.kunitconfig @@ -1,5 +1,8 @@ CONFIG_KUNIT=y +CONFIG_OF=y +CONFIG_OF_OVERLAY=y CONFIG_COMMON_CLK=y CONFIG_CLK_KUNIT_TEST=y +CONFIG_CLK_FIXED_RATE_KUNIT_TEST=y CONFIG_CLK_GATE_KUNIT_TEST=y CONFIG_UML_PCI_OVER_VIRTIO=n diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig index b6c5bf69a2b2..a992ca5e1efe 100644 --- a/drivers/clk/Kconfig +++ b/drivers/clk/Kconfig @@ -478,6 +478,13 @@ config CLK_KUNIT_TEST help Kunit tests for the common clock framework. +config CLK_FIXED_RATE_KUNIT_TEST + tristate "Basic fixed rate clk type KUnit test" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + KUnit tests for the basic fixed rate clk type. + config CLK_GATE_KUNIT_TEST tristate "Basic gate type Kunit test" if !KUNIT_ALL_TESTS depends on KUNIT diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile index 226b4b729703..4fb809f4bd95 100644 --- a/drivers/clk/Makefile +++ b/drivers/clk/Makefile @@ -6,6 +6,7 @@ obj-$(CONFIG_CLK_KUNIT_TEST) += clk_test.o obj-$(CONFIG_COMMON_CLK) += clk-divider.o obj-$(CONFIG_COMMON_CLK) += clk-fixed-factor.o obj-$(CONFIG_COMMON_CLK) += clk-fixed-rate.o +obj-$(CONFIG_CLK_FIXED_RATE_KUNIT_TEST) += clk-fixed-rate_test.o kunit_clk_fixed_rate_test.dtbo.o obj-$(CONFIG_COMMON_CLK) += clk-gate.o obj-$(CONFIG_CLK_GATE_KUNIT_TEST) += clk-gate_test.o obj-$(CONFIG_COMMON_CLK) += clk-multiplier.o diff --git a/drivers/clk/clk-fixed-rate_test.c b/drivers/clk/clk-fixed-rate_test.c new file mode 100644 index 000000000000..abe0c11ce9d3 --- /dev/null +++ b/drivers/clk/clk-fixed-rate_test.c @@ -0,0 +1,374 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for clk fixed rate basic type + */ +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "clk-fixed-rate_test.h" + +/** + * struct clk_hw_fixed_rate_kunit_params - Parameters to pass to __clk_hw_register_fixed_rate() + * @dev: device registering clk + * @np: device_node of device registering clk + * @name: name of clk + * @parent_name: parent name of clk + * @parent_hw: clk_hw pointer to parent of clk + * @parent_data: parent_data describing parent of clk + * @flags: clk framework flags + * @fixed_rate: frequency of clk + * @fixed_accuracy: accuracy of clk + * @clk_fixed_flags: fixed rate specific clk flags + */ +struct clk_hw_fixed_rate_kunit_params { + struct device *dev; + struct device_node *np; + const char *name; + const char *parent_name; + const struct clk_hw *parent_hw; + const struct clk_parent_data *parent_data; + unsigned long flags; + unsigned long fixed_rate; + unsigned long fixed_accuracy; + unsigned long clk_fixed_flags; +}; + +static int +clk_hw_register_fixed_rate_kunit_init(struct kunit_resource *res, void *context) +{ + struct clk_hw_fixed_rate_kunit_params *params = context; + struct clk_hw *hw; + + hw = __clk_hw_register_fixed_rate(params->dev, params->np, + params->name, + params->parent_name, + params->parent_hw, + params->parent_data, + params->flags, + params->fixed_rate, + params->fixed_accuracy, + params->clk_fixed_flags, + false); + if (IS_ERR(hw)) + return PTR_ERR(hw); + + res->data = hw; + + return 0; +} + +static void clk_hw_register_fixed_rate_kunit_exit(struct kunit_resource *res) +{ + struct clk_hw *hw = res->data; + + clk_hw_unregister_fixed_rate(hw); +} + +/** + * clk_hw_register_fixed_rate_kunit() - Test managed __clk_hw_register_fixed_rate() + * @test: The test context + * @params: Arguments to __clk_hw_register_fixed_rate() + * + * Returns: Registered fixed rate clk_hw or ERR_PTR on failure. + */ +static struct clk_hw * +clk_hw_register_fixed_rate_kunit(struct kunit *test, + struct clk_hw_fixed_rate_kunit_params *params) +{ + struct clk_hw *hw; + + hw = kunit_alloc_resource(test, + clk_hw_register_fixed_rate_kunit_init, + clk_hw_register_fixed_rate_kunit_exit, + GFP_KERNEL, params); + if (!hw) + return ERR_PTR(-EINVAL); + + return hw; +} + +/** + * clk_hw_unregister_fixed_rate_kunit() - Test managed clk_hw_unregister_fixed_rate() + * @test: The test context + * @hw: fixed rate clk to unregister upon test completion + * + * Automatically unregister @hw when @test is complete via + * clk_hw_unregister_fixed_rate(). + * + * Returns: 0 on success or negative errno on failure + */ +static int clk_hw_unregister_fixed_rate_kunit(struct kunit *test, struct clk_hw *hw) +{ + if (!kunit_alloc_resource(test, NULL, + clk_hw_register_fixed_rate_kunit_exit, + GFP_KERNEL, hw)) + return -ENOMEM; + + return 0; +} + +/* + * Test that clk_get_rate() on a fixed rate clk registered with + * clk_hw_register_fixed_rate() gets the proper frequency. + */ +static void clk_fixed_rate_rate_test(struct kunit *test) +{ + struct clk_hw *hw; + struct clk *clk; + const unsigned long fixed_rate = 230000; + + hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", NULL, 0, fixed_rate); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw); + KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw)); + + clk = clk_hw_get_clk_prepared_enabled_kunit(test, hw, __func__); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk); + + KUNIT_EXPECT_EQ(test, fixed_rate, clk_get_rate(clk)); +} + +/* + * Test that clk_get_accuracy() on a fixed rate clk registered via + * clk_hw_register_fixed_rate_with_accuracy() gets the proper accuracy. + */ +static void clk_fixed_rate_accuracy_test(struct kunit *test) +{ + struct clk_hw *hw; + struct clk *clk; + const unsigned long fixed_accuracy = 5000; + + hw = clk_hw_register_fixed_rate_with_accuracy(NULL, "test-fixed-rate", + NULL, 0, 0, + fixed_accuracy); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw); + KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw)); + + clk = clk_hw_get_clk_kunit(test, hw, __func__); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk); + + KUNIT_EXPECT_EQ(test, fixed_accuracy, clk_get_accuracy(clk)); +} + +/* Test suite for a fixed rate clk without any parent */ +static struct kunit_case clk_fixed_rate_test_cases[] = { + KUNIT_CASE(clk_fixed_rate_rate_test), + KUNIT_CASE(clk_fixed_rate_accuracy_test), + {} +}; + +static struct kunit_suite clk_fixed_rate_suite = { + .name = "clk_fixed_rate", + .test_cases = clk_fixed_rate_test_cases, +}; + +/* + * Test that clk_get_parent() on a fixed rate clk gets the proper parent. + */ +static void clk_fixed_rate_parent_test(struct kunit *test) +{ + struct clk_hw *hw, *parent_hw; + struct clk *expected_parent, *actual_parent; + struct clk *clk; + const char *parent_name = "test-fixed-rate-parent"; + struct clk_hw_fixed_rate_kunit_params parent_params = { + .name = parent_name, + }; + + parent_hw = clk_hw_register_fixed_rate_kunit(test, &parent_params); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw); + KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw)); + + expected_parent = clk_hw_get_clk_kunit(test, parent_hw, __func__); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent); + + hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", parent_name, 0, 0); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw); + KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw)); + + clk = clk_hw_get_clk_kunit(test, hw, __func__); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk); + + actual_parent = clk_get_parent(clk); + KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent)); +} + +/* + * Test that clk_get_rate() on a fixed rate clk ignores the parent rate. + */ +static void clk_fixed_rate_parent_rate_test(struct kunit *test) +{ + struct clk_hw *hw, *parent_hw; + struct clk *clk; + const unsigned long expected_rate = 1405; + const unsigned long parent_rate = 90402; + const char *parent_name = "test-fixed-rate-parent"; + struct clk_hw_fixed_rate_kunit_params parent_params = { + .name = parent_name, + .fixed_rate = parent_rate, + }; + + parent_hw = clk_hw_register_fixed_rate_kunit(test, &parent_params); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw); + KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw)); + + hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", parent_name, 0, + expected_rate); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw); + KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw)); + + clk = clk_hw_get_clk_prepared_enabled_kunit(test, hw, __func__); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk); + + KUNIT_EXPECT_EQ(test, expected_rate, clk_get_rate(clk)); +} + +/* + * Test that clk_get_accuracy() on a fixed rate clk ignores the parent accuracy. + */ +static void clk_fixed_rate_parent_accuracy_test(struct kunit *test) +{ + struct clk_hw *hw, *parent_hw; + struct clk *clk; + const unsigned long expected_accuracy = 900; + const unsigned long parent_accuracy = 24000; + const char *parent_name = "test-fixed-rate-parent"; + struct clk_hw_fixed_rate_kunit_params parent_params = { + .name = parent_name, + .fixed_accuracy = parent_accuracy, + }; + + parent_hw = clk_hw_register_fixed_rate_kunit(test, &parent_params); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw); + KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw)); + + hw = clk_hw_register_fixed_rate_with_accuracy(NULL, "test-fixed-rate", + parent_name, 0, 0, + expected_accuracy); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw); + KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw)); + + clk = clk_hw_get_clk_kunit(test, hw, __func__); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk); + + KUNIT_EXPECT_EQ(test, expected_accuracy, clk_get_accuracy(clk)); +} + +/* Test suite for a fixed rate clk with a parent */ +static struct kunit_case clk_fixed_rate_parent_test_cases[] = { + KUNIT_CASE(clk_fixed_rate_parent_test), + KUNIT_CASE(clk_fixed_rate_parent_rate_test), + KUNIT_CASE(clk_fixed_rate_parent_accuracy_test), + {} +}; + +static struct kunit_suite clk_fixed_rate_parent_suite = { + .name = "clk_fixed_rate_parent", + .test_cases = clk_fixed_rate_parent_test_cases, +}; + +struct clk_fixed_rate_of_test_context { + struct device *dev; + struct platform_driver pdrv; +}; + +static inline struct clk_fixed_rate_of_test_context * +pdev_to_clk_fixed_rate_of_test_context(struct platform_device *pdev) +{ + return container_of(to_platform_driver(pdev->dev.driver), + struct clk_fixed_rate_of_test_context, + pdrv); +} + +/* + * Test that of_fixed_clk_setup() registers a fixed rate clk with the proper + * rate. + */ +static void clk_fixed_rate_of_probe_test(struct kunit *test) +{ + struct clk_fixed_rate_of_test_context *ctx = test->priv; + struct device *dev = ctx->dev; + struct clk *clk; + + clk = clk_get_kunit(test, dev, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk); + + KUNIT_ASSERT_EQ(test, 0, clk_prepare_enable_kunit(test, clk)); + KUNIT_EXPECT_EQ(test, TEST_FIXED_FREQUENCY, clk_get_rate(clk)); +} + +/* + * Test that of_fixed_clk_setup() registers a fixed rate clk with the proper + * accuracy. + */ +static void clk_fixed_rate_of_accuracy_test(struct kunit *test) +{ + struct clk_fixed_rate_of_test_context *ctx = test->priv; + struct device *dev = ctx->dev; + struct clk *clk; + + clk = clk_get_kunit(test, dev, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk); + + KUNIT_EXPECT_EQ(test, TEST_FIXED_ACCURACY, clk_get_accuracy(clk)); +} + +static struct kunit_case clk_fixed_rate_of_cases[] = { + KUNIT_CASE(clk_fixed_rate_of_probe_test), + KUNIT_CASE(clk_fixed_rate_of_accuracy_test), + {} +}; + +static int clk_fixed_rate_of_test_probe(struct platform_device *pdev) +{ + struct clk_fixed_rate_of_test_context *ctx; + + ctx = pdev_to_clk_fixed_rate_of_test_context(pdev); + ctx->dev = &pdev->dev; + + return 0; +} + +static int clk_fixed_rate_of_init(struct kunit *test) +{ + struct clk_fixed_rate_of_test_context *ctx; + static const struct of_device_id match_table[] = { + { .compatible = "test,clk-fixed-rate" }, + { } + }; + + KUNIT_ASSERT_EQ(test, 0, + of_overlay_apply_kunit(test, kunit_clk_fixed_rate_test)); + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return -ENOMEM; + test->priv = ctx; + + ctx->pdrv.probe = clk_fixed_rate_of_test_probe; + ctx->pdrv.driver.of_match_table = match_table; + ctx->pdrv.driver.name = __func__; + ctx->pdrv.driver.owner = THIS_MODULE; + + return platform_driver_register_kunit(test, &ctx->pdrv); +} + +static struct kunit_suite clk_fixed_rate_of_suite = { + .name = "clk_fixed_rate_of", + .init = clk_fixed_rate_of_init, + .test_cases = clk_fixed_rate_of_cases, +}; + +kunit_test_suites( + &clk_fixed_rate_suite, + &clk_fixed_rate_of_suite, + &clk_fixed_rate_parent_suite, +); +MODULE_LICENSE("GPL"); diff --git a/drivers/clk/clk-fixed-rate_test.h b/drivers/clk/clk-fixed-rate_test.h new file mode 100644 index 000000000000..e0d28e5b6081 --- /dev/null +++ b/drivers/clk/clk-fixed-rate_test.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _CLK_FIXED_RATE_TEST_H +#define _CLK_FIXED_RATE_TEST_H + +#define TEST_FIXED_FREQUENCY 50000000 +#define TEST_FIXED_ACCURACY 300 + +#endif diff --git a/drivers/clk/kunit_clk_fixed_rate_test.dtso b/drivers/clk/kunit_clk_fixed_rate_test.dtso new file mode 100644 index 000000000000..10989b07e5b3 --- /dev/null +++ b/drivers/clk/kunit_clk_fixed_rate_test.dtso @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 +/dts-v1/; +/plugin/; + +#include "clk-fixed-rate_test.h" + +&{/} { + fixed_50MHz: kunit-clock { + compatible = "fixed-clock"; + #clock-cells = <0>; + clock-frequency = ; + clock-accuracy = ; + }; + + kunit-clock-consumer { + compatible = "test,clk-fixed-rate"; + clocks = <&fixed_50MHz>; + }; +}; From patchwork Mon Mar 27 22:21:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 667611 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53DB3C77B61 for ; Mon, 27 Mar 2023 22:22:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232400AbjC0WWa (ORCPT ); Mon, 27 Mar 2023 18:22:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232199AbjC0WWR (ORCPT ); Mon, 27 Mar 2023 18:22:17 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE82740DB; Mon, 27 Mar 2023 15:22:10 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 19A95B819A5; Mon, 27 Mar 2023 22:22:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 423F6C4339C; Mon, 27 Mar 2023 22:22:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679955727; bh=2Hped3KY6Uk2Oc/RHdUAUuvCu9fegBzwhk+dDaq1CSo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lAyNd0WoR1WcvvMf+8Wb4XJei0r0zHaAMVOg+st4WmC77BMhUQBudfLNQ9S0JKafa Y7uedc/ieLIIzblkbrx7ktwUd03utg9POvdqQGaX+sa+MeARGeEUtn7wT8oWlcGQvQ V9Mmh1TZrB0phjvNmYyuB+vrGOzjTvm87qFwtB1s7ASHY3lRy6WWow0GsftwGTd3UV RI/MRHDN3W+k6KlaQxsBlwqQ1slq4lSt+iVW22pW1uzTG9AQ2TGdH1MRF6x5tn0Gv5 KhKCL6qL/cpaWmgfqjb9YroXiVcBcpU5tzU6z7cunHwGOp8Yrp5lJ93uoCjbzsnJho Vl9UGj8/5g0qA== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, Maxime Ripard Subject: [PATCH v3 10/11] dt-bindings: clk: Add KUnit clk_parent_data test Date: Mon, 27 Mar 2023 15:21:58 -0700 Message-Id: <20230327222159.3509818-11-sboyd@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230327222159.3509818-1-sboyd@kernel.org> References: <20230327222159.3509818-1-sboyd@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Describe a binding for a device that provides and consumes clks in DT so that a KUnit test can register clks based on the device node and test clk_hw_register() with clk_parent_data. Cc: Rob Herring Cc: Krzysztof Kozlowski Cc: Brendan Higgins Cc: David Gow Signed-off-by: Stephen Boyd --- .../bindings/clock/test,clk-parent-data.yaml | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Documentation/devicetree/bindings/clock/test,clk-parent-data.yaml diff --git a/Documentation/devicetree/bindings/clock/test,clk-parent-data.yaml b/Documentation/devicetree/bindings/clock/test,clk-parent-data.yaml new file mode 100644 index 000000000000..a2f927526405 --- /dev/null +++ b/Documentation/devicetree/bindings/clock/test,clk-parent-data.yaml @@ -0,0 +1,47 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/clock/test,clk-parent-data.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Fake clk provider for clk_parent_data unit tests + +maintainers: + - Stephen Boyd + +description: + A clk provider to test the struct clk_parent_data implementation in the Linux + kernel. + +properties: + compatible: + const: test,clk-parent-data + + clocks: + items: + - description: Fixed parent + - description: 50 MHz fixed parent + + clock-names: + items: + - const: parent_fwname + - const: "50" + + "#clock-cells": + const: 1 + +required: + - compatible + - "#clock-cells" + +additionalProperties: false + +examples: + - | + clock-controller { + compatible = "test,clk-parent-data"; + #clock-cells = <1>; + clocks = <&fixed_parent>, <&fixed_50MHz>; + clock-names = "parent_fwname", "50"; + }; +...