All of lore.kernel.org
 help / color / mirror / Atom feed
From: Maxime Ripard <maxime@cerno.tech>
To: Michael Turquette <mturquette@baylibre.com>,
	 Stephen Boyd <sboyd@kernel.org>
Cc: "Alexandre Belloni" <alexandre.belloni@bootlin.com>,
	"Peng Fan" <peng.fan@nxp.com>,
	"Geert Uytterhoeven" <geert+renesas@glider.be>,
	"Sekhar Nori" <nsekhar@ti.com>,
	"Alexandre Torgue" <alexandre.torgue@foss.st.com>,
	dri-devel@lists.freedesktop.org,
	"Jaroslav Kysela" <perex@perex.cz>,
	"Paul Cercueil" <paul@crapouillou.net>,
	"Max Filippov" <jcmvbkbc@gmail.com>,
	"Prashant Gaikwad" <pgaikwad@nvidia.com>,
	linux-phy@lists.infradead.org, linux-clk@vger.kernel.org,
	"Abel Vesa" <abelvesa@kernel.org>,
	"Kishon Vijay Abraham I" <kishon@kernel.org>,
	"Samuel Holland" <samuel@sholland.org>,
	"Chunyan Zhang" <zhang.lyra@gmail.com>,
	"Takashi Iwai" <tiwai@suse.com>, "Vinod Koul" <vkoul@kernel.org>,
	"Jernej Skrabec" <jernej.skrabec@gmail.com>,
	"Jonathan Hunter" <jonathanh@nvidia.com>,
	"Chen-Yu Tsai" <wens@csie.org>,
	"NXP Linux Team" <linux-imx@nxp.com>,
	"Chen-Yu Tsai" <wenst@chromium.org>,
	"Orson Zhai" <orsonzhai@gmail.com>,
	"Ulf Hansson" <ulf.hansson@linaro.org>,
	linux-mips@vger.kernel.org,
	"Luca Ceresoli" <luca.ceresoli@bootlin.com>,
	linux-sunxi@lists.linux.dev,
	"Maxime Coquelin" <mcoquelin.stm32@gmail.com>,
	linux-rtc@vger.kernel.org,
	"Charles Keepax" <ckeepax@opensource.cirrus.com>,
	"David Lechner" <david@lechnology.com>,
	"Manivannan Sadhasivam" <mani@kernel.org>,
	"Sascha Hauer" <s.hauer@pengutronix.de>,
	"Nicolas Ferre" <nicolas.ferre@microchip.com>,
	linux-actions@lists.infradead.org,
	"Markus Schneider-Pargmann" <msp@baylibre.com>,
	"Richard Fitzgerald" <rf@opensource.cirrus.com>,
	"Mark Brown" <broonie@kernel.org>,
	"Maxime Ripard" <maxime@cerno.tech>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	linux-tegra@vger.kernel.org,
	"Mikko Perttunen" <mperttunen@nvidia.com>,
	"Pengutronix Kernel Team" <kernel@pengutronix.de>,
	linux-arm-kernel@lists.infradead.org,
	"AngeloGioacchino Del Regno"
	<angelogioacchino.delregno@collabora.com>,
	"Alessandro Zummo" <a.zummo@towertech.it>,
	patches@opensource.cirrus.com,
	"Peter De Schrijver" <pdeschrijver@nvidia.com>,
	linux-stm32@st-md-mailman.stormreply.com,
	"Liam Girdwood" <lgirdwood@gmail.com>,
	"Claudiu Beznea" <claudiu.beznea@microchip.com>,
	linux-renesas-soc@vger.kernel.org,
	"Dinh Nguyen" <dinguyen@kernel.org>,
	"Miles Chen" <miles.chen@mediatek.com>,
	"Thierry Reding" <thierry.reding@gmail.com>,
	"Shawn Guo" <shawnguo@kernel.org>,
	"Andreas Färber" <afaerber@suse.de>
Subject: [PATCH v4 04/68] clk: Introduce clk_hw_determine_rate_no_reparent()
Date: Fri, 05 May 2023 13:25:06 +0200	[thread overview]
Message-ID: <20221018-clk-range-checks-fixes-v4-4-971d5077e7d2@cerno.tech> (raw)
In-Reply-To: <20221018-clk-range-checks-fixes-v4-0-971d5077e7d2@cerno.tech>

From: Stephen Boyd <sboyd@kernel.org>

Some clock drivers do not want to allow any reparenting on a given
clock, but usually do so by not providing any determine_rate
implementation.

Whenever we call clk_round_rate() or clk_set_rate(), this leads to
clk_core_can_round() returning false and thus the rest of the function
either forwarding the rate request to its current parent if
CLK_SET_RATE_PARENT is set, or just returning the current clock rate.

This behaviour happens implicitly, and as we move forward to making a
determine_rate implementation required for muxes, we need some way to
explicitly opt-in for that behaviour.

Fortunately, this is exactly what the clk_core_determine_rate_no_reparent()
function is doing, so we can simply make it available to drivers.

Cc: Abel Vesa <abelvesa@kernel.org>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
Cc: Alexandre Torgue <alexandre.torgue@foss.st.com>
Cc: "Andreas Färber" <afaerber@suse.de>
Cc: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Charles Keepax <ckeepax@opensource.cirrus.com>
Cc: Chen-Yu Tsai <wens@csie.org>
Cc: Chen-Yu Tsai <wenst@chromium.org>
Cc: Chunyan Zhang <zhang.lyra@gmail.com>
Cc: Claudiu Beznea <claudiu.beznea@microchip.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: David Airlie <airlied@gmail.com>
Cc: David Lechner <david@lechnology.com>
Cc: Dinh Nguyen <dinguyen@kernel.org>
Cc: Fabio Estevam <festevam@gmail.com>
Cc: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: Jaroslav Kysela <perex@perex.cz>
Cc: Jernej Skrabec <jernej.skrabec@gmail.com>
Cc: Jonathan Hunter <jonathanh@nvidia.com>
Cc: Kishon Vijay Abraham I <kishon@kernel.org>
Cc: Liam Girdwood <lgirdwood@gmail.com>
Cc: Linus Walleij <linus.walleij@linaro.org>
Cc: Luca Ceresoli <luca.ceresoli@bootlin.com>
Cc: Manivannan Sadhasivam <mani@kernel.org>
Cc: Mark Brown <broonie@kernel.org>
Cc: Markus Schneider-Pargmann <msp@baylibre.com>
Cc: Max Filippov <jcmvbkbc@gmail.com>
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Cc: Mikko Perttunen <mperttunen@nvidia.com>
Cc: Miles Chen <miles.chen@mediatek.com>
Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
Cc: Orson Zhai <orsonzhai@gmail.com>
Cc: Paul Cercueil <paul@crapouillou.net>
Cc: Peng Fan <peng.fan@nxp.com>
Cc: Peter De Schrijver <pdeschrijver@nvidia.com>
Cc: Prashant Gaikwad <pgaikwad@nvidia.com>
Cc: Richard Fitzgerald <rf@opensource.cirrus.com>
Cc: Samuel Holland <samuel@sholland.org>
Cc: Sascha Hauer <s.hauer@pengutronix.de>
Cc: Sekhar Nori <nsekhar@ti.com>
Cc: Shawn Guo <shawnguo@kernel.org>
Cc: Takashi Iwai <tiwai@suse.com>
Cc: Thierry Reding <thierry.reding@gmail.com>
Cc: Ulf Hansson <ulf.hansson@linaro.org>
Cc: Vinod Koul <vkoul@kernel.org>
Cc: dri-devel@lists.freedesktop.org
Cc: linux-actions@lists.infradead.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-mips@vger.kernel.org
Cc: linux-phy@lists.infradead.org
Cc: linux-renesas-soc@vger.kernel.org
Cc: linux-rtc@vger.kernel.org
Cc: linux-stm32@st-md-mailman.stormreply.com
Cc: linux-sunxi@lists.linux.dev
Cc: linux-tegra@vger.kernel.org
Cc: NXP Linux Team <linux-imx@nxp.com>
Cc: patches@opensource.cirrus.com
Cc: Pengutronix Kernel Team <kernel@pengutronix.de>
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: Maxime Ripard <maxime@cerno.tech>
---
 drivers/clk/clk.c            |  18 +++++
 drivers/clk/clk_test.c       | 152 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/clk-provider.h |   2 +
 3 files changed, 172 insertions(+)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index f57f821a5e5a..5365595433c8 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -783,6 +783,24 @@ int __clk_mux_determine_rate_closest(struct clk_hw *hw,
 }
 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
 
+/*
+ * clk_hw_determine_rate_no_reparent - clk_ops::determine_rate implementation for a clk that doesn't reparent
+ * @hw: mux type clk to determine rate on
+ * @req: rate request, also used to return preferred frequency
+ *
+ * Helper for finding best parent rate to provide a given frequency.
+ * This can be used directly as a determine_rate callback (e.g. for a
+ * mux), or from a more complex clock that may combine a mux with other
+ * operations.
+ *
+ * Returns: 0 on success, -EERROR value on error
+ */
+int clk_hw_determine_rate_no_reparent(struct clk_hw *hw,
+				      struct clk_rate_request *req)
+{
+	return clk_core_determine_rate_no_reparent(hw, req);
+}
+
 /***        clk api        ***/
 
 static void clk_core_rate_unprotect(struct clk_core *core)
diff --git a/drivers/clk/clk_test.c b/drivers/clk/clk_test.c
index 2cb51153750d..b3ed3b0e4c31 100644
--- a/drivers/clk/clk_test.c
+++ b/drivers/clk/clk_test.c
@@ -141,6 +141,12 @@ static const struct clk_ops clk_multiple_parents_mux_ops = {
 	.determine_rate = __clk_mux_determine_rate_closest,
 };
 
+static const struct clk_ops clk_multiple_parents_no_reparent_mux_ops = {
+	.determine_rate = clk_hw_determine_rate_no_reparent,
+	.get_parent = clk_multiple_parents_mux_get_parent,
+	.set_parent = clk_multiple_parents_mux_set_parent,
+};
+
 static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
 {
 	struct clk_dummy_context *ctx;
@@ -2395,10 +2401,156 @@ static struct kunit_suite clk_mux_notifier_test_suite = {
 	.test_cases = clk_mux_notifier_test_cases,
 };
 
+static int
+clk_mux_no_reparent_test_init(struct kunit *test)
+{
+	struct clk_multiple_parent_ctx *ctx;
+	const char *parents[2] = { "parent-0", "parent-1"};
+	int ret;
+
+	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+	if (!ctx)
+		return -ENOMEM;
+	test->priv = ctx;
+
+	ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
+							    &clk_dummy_rate_ops,
+							    0);
+	ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
+	ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
+	if (ret)
+		return ret;
+
+	ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
+							    &clk_dummy_rate_ops,
+							    0);
+	ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
+	ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
+	if (ret)
+		return ret;
+
+	ctx->current_parent = 0;
+	ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
+					   &clk_multiple_parents_no_reparent_mux_ops,
+					   0);
+	ret = clk_hw_register(NULL, &ctx->hw);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static void
+clk_mux_no_reparent_test_exit(struct kunit *test)
+{
+	struct clk_multiple_parent_ctx *ctx = test->priv;
+
+	clk_hw_unregister(&ctx->hw);
+	clk_hw_unregister(&ctx->parents_ctx[0].hw);
+	clk_hw_unregister(&ctx->parents_ctx[1].hw);
+}
+
+/*
+ * Test that if the we have a mux that cannot change parent and we call
+ * clk_round_rate() on it with a rate that should cause it to change
+ * parent, it won't.
+ */
+static void clk_mux_no_reparent_round_rate(struct kunit *test)
+{
+	struct clk_multiple_parent_ctx *ctx = test->priv;
+	struct clk_hw *hw = &ctx->hw;
+	struct clk *clk = clk_hw_get_clk(hw, NULL);
+	struct clk *other_parent, *parent;
+	unsigned long other_parent_rate;
+	unsigned long parent_rate;
+	long rounded_rate;
+
+	parent = clk_get_parent(clk);
+	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
+
+	parent_rate = clk_get_rate(parent);
+	KUNIT_ASSERT_GT(test, parent_rate, 0);
+
+	other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
+	KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
+
+	other_parent_rate = clk_get_rate(other_parent);
+	KUNIT_ASSERT_GT(test, other_parent_rate, 0);
+	clk_put(other_parent);
+
+	rounded_rate = clk_round_rate(clk, other_parent_rate);
+	KUNIT_ASSERT_GT(test, rounded_rate, 0);
+	KUNIT_EXPECT_EQ(test, rounded_rate, parent_rate);
+
+	clk_put(clk);
+}
+
+/*
+ * Test that if the we have a mux that cannot change parent and we call
+ * clk_set_rate() on it with a rate that should cause it to change
+ * parent, it won't.
+ */
+static void clk_mux_no_reparent_set_rate(struct kunit *test)
+{
+	struct clk_multiple_parent_ctx *ctx = test->priv;
+	struct clk_hw *hw = &ctx->hw;
+	struct clk *clk = clk_hw_get_clk(hw, NULL);
+	struct clk *other_parent, *parent;
+	unsigned long other_parent_rate;
+	unsigned long parent_rate;
+	unsigned long rate;
+	int ret;
+
+	parent = clk_get_parent(clk);
+	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
+
+	parent_rate = clk_get_rate(parent);
+	KUNIT_ASSERT_GT(test, parent_rate, 0);
+
+	other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
+	KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
+
+	other_parent_rate = clk_get_rate(other_parent);
+	KUNIT_ASSERT_GT(test, other_parent_rate, 0);
+	clk_put(other_parent);
+
+	ret = clk_set_rate(clk, other_parent_rate);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	rate = clk_get_rate(clk);
+	KUNIT_ASSERT_GT(test, rate, 0);
+	KUNIT_EXPECT_EQ(test, rate, parent_rate);
+
+	clk_put(clk);
+}
+
+static struct kunit_case clk_mux_no_reparent_test_cases[] = {
+	KUNIT_CASE(clk_mux_no_reparent_round_rate),
+	KUNIT_CASE(clk_mux_no_reparent_set_rate),
+	{}
+};
+
+/*
+ * Test suite for a clock mux that isn't allowed to change parent, using
+ * the clk_hw_determine_rate_no_reparent() helper.
+ *
+ * These tests exercise that helper, and the proper selection of
+ * rates and parents.
+ */
+static struct kunit_suite clk_mux_no_reparent_test_suite = {
+	.name = "clk-mux-no-reparent",
+	.init = clk_mux_no_reparent_test_init,
+	.exit = clk_mux_no_reparent_test_exit,
+	.test_cases = clk_mux_no_reparent_test_cases,
+};
+
 kunit_test_suites(
 	&clk_leaf_mux_set_rate_parent_test_suite,
 	&clk_test_suite,
 	&clk_multiple_parents_mux_test_suite,
+	&clk_mux_no_reparent_test_suite,
 	&clk_mux_notifier_test_suite,
 	&clk_orphan_transparent_multiple_parent_mux_test_suite,
 	&clk_orphan_transparent_single_parent_test_suite,
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 28ff6f1a6ada..f8f220fb5dab 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -1333,6 +1333,8 @@ int __clk_mux_determine_rate_closest(struct clk_hw *hw,
 int clk_mux_determine_rate_flags(struct clk_hw *hw,
 				 struct clk_rate_request *req,
 				 unsigned long flags);
+int clk_hw_determine_rate_no_reparent(struct clk_hw *hw,
+				      struct clk_rate_request *req);
 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent);
 void clk_hw_get_rate_range(struct clk_hw *hw, unsigned long *min_rate,
 			   unsigned long *max_rate);

-- 
2.40.0


WARNING: multiple messages have this Message-ID (diff)
From: Maxime Ripard <maxime@cerno.tech>
To: Michael Turquette <mturquette@baylibre.com>,
	 Stephen Boyd <sboyd@kernel.org>
Cc: linux-clk@vger.kernel.org, "Maxime Ripard" <maxime@cerno.tech>,
	"Abel Vesa" <abelvesa@kernel.org>,
	"Alessandro Zummo" <a.zummo@towertech.it>,
	"Alexandre Belloni" <alexandre.belloni@bootlin.com>,
	"Alexandre Torgue" <alexandre.torgue@foss.st.com>,
	"Andreas Färber" <afaerber@suse.de>,
	"AngeloGioacchino Del Regno"
	<angelogioacchino.delregno@collabora.com>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Charles Keepax" <ckeepax@opensource.cirrus.com>,
	"Chen-Yu Tsai" <wens@csie.org>,
	"Chen-Yu Tsai" <wenst@chromium.org>,
	"Chunyan Zhang" <zhang.lyra@gmail.com>,
	"Claudiu Beznea" <claudiu.beznea@microchip.com>,
	"Daniel Vetter" <daniel@ffwll.ch>,
	"David Airlie" <airlied@gmail.com>,
	"David Lechner" <david@lechnology.com>,
	"Dinh Nguyen" <dinguyen@kernel.org>,
	"Fabio Estevam" <festevam@gmail.com>,
	"Geert Uytterhoeven" <geert+renesas@glider.be>,
	"Jaroslav Kysela" <perex@perex.cz>,
	"Jernej Skrabec" <jernej.skrabec@gmail.com>,
	"Jonathan Hunter" <jonathanh@nvidia.com>,
	"Kishon Vijay Abraham I" <kishon@kernel.org>,
	"Liam Girdwood" <lgirdwood@gmail.com>,
	"Linus Walleij" <linus.walleij@linaro.org>,
	"Luca Ceresoli" <luca.ceresoli@bootlin.com>,
	"Manivannan Sadhasivam" <mani@kernel.org>,
	"Mark Brown" <broonie@kernel.org>,
	"Markus Schneider-Pargmann" <msp@baylibre.com>,
	"Max Filippov" <jcmvbkbc@gmail.com>,
	"Maxime Coquelin" <mcoquelin.stm32@gmail.com>,
	"Mikko Perttunen" <mperttunen@nvidia.com>,
	"Miles Chen" <miles.chen@mediatek.com>,
	"Nicolas Ferre" <nicolas.ferre@microchip.com>,
	"Orson Zhai" <orsonzhai@gmail.com>,
	"Paul Cercueil" <paul@crapouillou.net>,
	"Peng Fan" <peng.fan@nxp.com>,
	"Peter De Schrijver" <pdeschrijver@nvidia.com>,
	"Prashant Gaikwad" <pgaikwad@nvidia.com>,
	"Richard Fitzgerald" <rf@opensource.cirrus.com>,
	"Samuel Holland" <samuel@sholland.org>,
	"Sascha Hauer" <s.hauer@pengutronix.de>,
	"Sekhar Nori" <nsekhar@ti.com>, "Shawn Guo" <shawnguo@kernel.org>,
	"Takashi Iwai" <tiwai@suse.com>,
	"Thierry Reding" <thierry.reding@gmail.com>,
	"Ulf Hansson" <ulf.hansson@linaro.org>,
	"Vinod Koul" <vkoul@kernel.org>,
	dri-devel@lists.freedesktop.org,
	linux-actions@lists.infradead.org,
	linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org,
	linux-phy@lists.infradead.org, linux-renesas-soc@vger.kernel.org,
	linux-rtc@vger.kernel.org,
	linux-stm32@st-md-mailman.stormreply.com,
	linux-sunxi@lists.linux.dev, linux-tegra@vger.kernel.org,
	"NXP Linux Team" <linux-imx@nxp.com>,
	patches@opensource.cirrus.com,
	"Pengutronix Kernel Team" <kernel@pengutronix.de>
Subject: [PATCH v4 04/68] clk: Introduce clk_hw_determine_rate_no_reparent()
Date: Fri, 05 May 2023 13:25:06 +0200	[thread overview]
Message-ID: <20221018-clk-range-checks-fixes-v4-4-971d5077e7d2@cerno.tech> (raw)
In-Reply-To: <20221018-clk-range-checks-fixes-v4-0-971d5077e7d2@cerno.tech>

From: Stephen Boyd <sboyd@kernel.org>

Some clock drivers do not want to allow any reparenting on a given
clock, but usually do so by not providing any determine_rate
implementation.

Whenever we call clk_round_rate() or clk_set_rate(), this leads to
clk_core_can_round() returning false and thus the rest of the function
either forwarding the rate request to its current parent if
CLK_SET_RATE_PARENT is set, or just returning the current clock rate.

This behaviour happens implicitly, and as we move forward to making a
determine_rate implementation required for muxes, we need some way to
explicitly opt-in for that behaviour.

Fortunately, this is exactly what the clk_core_determine_rate_no_reparent()
function is doing, so we can simply make it available to drivers.

Cc: Abel Vesa <abelvesa@kernel.org>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
Cc: Alexandre Torgue <alexandre.torgue@foss.st.com>
Cc: "Andreas Färber" <afaerber@suse.de>
Cc: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Charles Keepax <ckeepax@opensource.cirrus.com>
Cc: Chen-Yu Tsai <wens@csie.org>
Cc: Chen-Yu Tsai <wenst@chromium.org>
Cc: Chunyan Zhang <zhang.lyra@gmail.com>
Cc: Claudiu Beznea <claudiu.beznea@microchip.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: David Airlie <airlied@gmail.com>
Cc: David Lechner <david@lechnology.com>
Cc: Dinh Nguyen <dinguyen@kernel.org>
Cc: Fabio Estevam <festevam@gmail.com>
Cc: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: Jaroslav Kysela <perex@perex.cz>
Cc: Jernej Skrabec <jernej.skrabec@gmail.com>
Cc: Jonathan Hunter <jonathanh@nvidia.com>
Cc: Kishon Vijay Abraham I <kishon@kernel.org>
Cc: Liam Girdwood <lgirdwood@gmail.com>
Cc: Linus Walleij <linus.walleij@linaro.org>
Cc: Luca Ceresoli <luca.ceresoli@bootlin.com>
Cc: Manivannan Sadhasivam <mani@kernel.org>
Cc: Mark Brown <broonie@kernel.org>
Cc: Markus Schneider-Pargmann <msp@baylibre.com>
Cc: Max Filippov <jcmvbkbc@gmail.com>
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Cc: Mikko Perttunen <mperttunen@nvidia.com>
Cc: Miles Chen <miles.chen@mediatek.com>
Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
Cc: Orson Zhai <orsonzhai@gmail.com>
Cc: Paul Cercueil <paul@crapouillou.net>
Cc: Peng Fan <peng.fan@nxp.com>
Cc: Peter De Schrijver <pdeschrijver@nvidia.com>
Cc: Prashant Gaikwad <pgaikwad@nvidia.com>
Cc: Richard Fitzgerald <rf@opensource.cirrus.com>
Cc: Samuel Holland <samuel@sholland.org>
Cc: Sascha Hauer <s.hauer@pengutronix.de>
Cc: Sekhar Nori <nsekhar@ti.com>
Cc: Shawn Guo <shawnguo@kernel.org>
Cc: Takashi Iwai <tiwai@suse.com>
Cc: Thierry Reding <thierry.reding@gmail.com>
Cc: Ulf Hansson <ulf.hansson@linaro.org>
Cc: Vinod Koul <vkoul@kernel.org>
Cc: dri-devel@lists.freedesktop.org
Cc: linux-actions@lists.infradead.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-mips@vger.kernel.org
Cc: linux-phy@lists.infradead.org
Cc: linux-renesas-soc@vger.kernel.org
Cc: linux-rtc@vger.kernel.org
Cc: linux-stm32@st-md-mailman.stormreply.com
Cc: linux-sunxi@lists.linux.dev
Cc: linux-tegra@vger.kernel.org
Cc: NXP Linux Team <linux-imx@nxp.com>
Cc: patches@opensource.cirrus.com
Cc: Pengutronix Kernel Team <kernel@pengutronix.de>
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: Maxime Ripard <maxime@cerno.tech>
---
 drivers/clk/clk.c            |  18 +++++
 drivers/clk/clk_test.c       | 152 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/clk-provider.h |   2 +
 3 files changed, 172 insertions(+)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index f57f821a5e5a..5365595433c8 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -783,6 +783,24 @@ int __clk_mux_determine_rate_closest(struct clk_hw *hw,
 }
 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
 
+/*
+ * clk_hw_determine_rate_no_reparent - clk_ops::determine_rate implementation for a clk that doesn't reparent
+ * @hw: mux type clk to determine rate on
+ * @req: rate request, also used to return preferred frequency
+ *
+ * Helper for finding best parent rate to provide a given frequency.
+ * This can be used directly as a determine_rate callback (e.g. for a
+ * mux), or from a more complex clock that may combine a mux with other
+ * operations.
+ *
+ * Returns: 0 on success, -EERROR value on error
+ */
+int clk_hw_determine_rate_no_reparent(struct clk_hw *hw,
+				      struct clk_rate_request *req)
+{
+	return clk_core_determine_rate_no_reparent(hw, req);
+}
+
 /***        clk api        ***/
 
 static void clk_core_rate_unprotect(struct clk_core *core)
diff --git a/drivers/clk/clk_test.c b/drivers/clk/clk_test.c
index 2cb51153750d..b3ed3b0e4c31 100644
--- a/drivers/clk/clk_test.c
+++ b/drivers/clk/clk_test.c
@@ -141,6 +141,12 @@ static const struct clk_ops clk_multiple_parents_mux_ops = {
 	.determine_rate = __clk_mux_determine_rate_closest,
 };
 
+static const struct clk_ops clk_multiple_parents_no_reparent_mux_ops = {
+	.determine_rate = clk_hw_determine_rate_no_reparent,
+	.get_parent = clk_multiple_parents_mux_get_parent,
+	.set_parent = clk_multiple_parents_mux_set_parent,
+};
+
 static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
 {
 	struct clk_dummy_context *ctx;
@@ -2395,10 +2401,156 @@ static struct kunit_suite clk_mux_notifier_test_suite = {
 	.test_cases = clk_mux_notifier_test_cases,
 };
 
+static int
+clk_mux_no_reparent_test_init(struct kunit *test)
+{
+	struct clk_multiple_parent_ctx *ctx;
+	const char *parents[2] = { "parent-0", "parent-1"};
+	int ret;
+
+	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+	if (!ctx)
+		return -ENOMEM;
+	test->priv = ctx;
+
+	ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
+							    &clk_dummy_rate_ops,
+							    0);
+	ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
+	ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
+	if (ret)
+		return ret;
+
+	ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
+							    &clk_dummy_rate_ops,
+							    0);
+	ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
+	ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
+	if (ret)
+		return ret;
+
+	ctx->current_parent = 0;
+	ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
+					   &clk_multiple_parents_no_reparent_mux_ops,
+					   0);
+	ret = clk_hw_register(NULL, &ctx->hw);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static void
+clk_mux_no_reparent_test_exit(struct kunit *test)
+{
+	struct clk_multiple_parent_ctx *ctx = test->priv;
+
+	clk_hw_unregister(&ctx->hw);
+	clk_hw_unregister(&ctx->parents_ctx[0].hw);
+	clk_hw_unregister(&ctx->parents_ctx[1].hw);
+}
+
+/*
+ * Test that if the we have a mux that cannot change parent and we call
+ * clk_round_rate() on it with a rate that should cause it to change
+ * parent, it won't.
+ */
+static void clk_mux_no_reparent_round_rate(struct kunit *test)
+{
+	struct clk_multiple_parent_ctx *ctx = test->priv;
+	struct clk_hw *hw = &ctx->hw;
+	struct clk *clk = clk_hw_get_clk(hw, NULL);
+	struct clk *other_parent, *parent;
+	unsigned long other_parent_rate;
+	unsigned long parent_rate;
+	long rounded_rate;
+
+	parent = clk_get_parent(clk);
+	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
+
+	parent_rate = clk_get_rate(parent);
+	KUNIT_ASSERT_GT(test, parent_rate, 0);
+
+	other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
+	KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
+
+	other_parent_rate = clk_get_rate(other_parent);
+	KUNIT_ASSERT_GT(test, other_parent_rate, 0);
+	clk_put(other_parent);
+
+	rounded_rate = clk_round_rate(clk, other_parent_rate);
+	KUNIT_ASSERT_GT(test, rounded_rate, 0);
+	KUNIT_EXPECT_EQ(test, rounded_rate, parent_rate);
+
+	clk_put(clk);
+}
+
+/*
+ * Test that if the we have a mux that cannot change parent and we call
+ * clk_set_rate() on it with a rate that should cause it to change
+ * parent, it won't.
+ */
+static void clk_mux_no_reparent_set_rate(struct kunit *test)
+{
+	struct clk_multiple_parent_ctx *ctx = test->priv;
+	struct clk_hw *hw = &ctx->hw;
+	struct clk *clk = clk_hw_get_clk(hw, NULL);
+	struct clk *other_parent, *parent;
+	unsigned long other_parent_rate;
+	unsigned long parent_rate;
+	unsigned long rate;
+	int ret;
+
+	parent = clk_get_parent(clk);
+	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
+
+	parent_rate = clk_get_rate(parent);
+	KUNIT_ASSERT_GT(test, parent_rate, 0);
+
+	other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
+	KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
+
+	other_parent_rate = clk_get_rate(other_parent);
+	KUNIT_ASSERT_GT(test, other_parent_rate, 0);
+	clk_put(other_parent);
+
+	ret = clk_set_rate(clk, other_parent_rate);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	rate = clk_get_rate(clk);
+	KUNIT_ASSERT_GT(test, rate, 0);
+	KUNIT_EXPECT_EQ(test, rate, parent_rate);
+
+	clk_put(clk);
+}
+
+static struct kunit_case clk_mux_no_reparent_test_cases[] = {
+	KUNIT_CASE(clk_mux_no_reparent_round_rate),
+	KUNIT_CASE(clk_mux_no_reparent_set_rate),
+	{}
+};
+
+/*
+ * Test suite for a clock mux that isn't allowed to change parent, using
+ * the clk_hw_determine_rate_no_reparent() helper.
+ *
+ * These tests exercise that helper, and the proper selection of
+ * rates and parents.
+ */
+static struct kunit_suite clk_mux_no_reparent_test_suite = {
+	.name = "clk-mux-no-reparent",
+	.init = clk_mux_no_reparent_test_init,
+	.exit = clk_mux_no_reparent_test_exit,
+	.test_cases = clk_mux_no_reparent_test_cases,
+};
+
 kunit_test_suites(
 	&clk_leaf_mux_set_rate_parent_test_suite,
 	&clk_test_suite,
 	&clk_multiple_parents_mux_test_suite,
+	&clk_mux_no_reparent_test_suite,
 	&clk_mux_notifier_test_suite,
 	&clk_orphan_transparent_multiple_parent_mux_test_suite,
 	&clk_orphan_transparent_single_parent_test_suite,
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 28ff6f1a6ada..f8f220fb5dab 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -1333,6 +1333,8 @@ int __clk_mux_determine_rate_closest(struct clk_hw *hw,
 int clk_mux_determine_rate_flags(struct clk_hw *hw,
 				 struct clk_rate_request *req,
 				 unsigned long flags);
+int clk_hw_determine_rate_no_reparent(struct clk_hw *hw,
+				      struct clk_rate_request *req);
 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent);
 void clk_hw_get_rate_range(struct clk_hw *hw, unsigned long *min_rate,
 			   unsigned long *max_rate);

-- 
2.40.0


-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

WARNING: multiple messages have this Message-ID (diff)
From: Maxime Ripard <maxime@cerno.tech>
To: Michael Turquette <mturquette@baylibre.com>,
	 Stephen Boyd <sboyd@kernel.org>
Cc: linux-clk@vger.kernel.org, "Maxime Ripard" <maxime@cerno.tech>,
	"Abel Vesa" <abelvesa@kernel.org>,
	"Alessandro Zummo" <a.zummo@towertech.it>,
	"Alexandre Belloni" <alexandre.belloni@bootlin.com>,
	"Alexandre Torgue" <alexandre.torgue@foss.st.com>,
	"Andreas Färber" <afaerber@suse.de>,
	"AngeloGioacchino Del Regno"
	<angelogioacchino.delregno@collabora.com>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Charles Keepax" <ckeepax@opensource.cirrus.com>,
	"Chen-Yu Tsai" <wens@csie.org>,
	"Chen-Yu Tsai" <wenst@chromium.org>,
	"Chunyan Zhang" <zhang.lyra@gmail.com>,
	"Claudiu Beznea" <claudiu.beznea@microchip.com>,
	"Daniel Vetter" <daniel@ffwll.ch>,
	"David Airlie" <airlied@gmail.com>,
	"David Lechner" <david@lechnology.com>,
	"Dinh Nguyen" <dinguyen@kernel.org>,
	"Fabio Estevam" <festevam@gmail.com>,
	"Geert Uytterhoeven" <geert+renesas@glider.be>,
	"Jaroslav Kysela" <perex@perex.cz>,
	"Jernej Skrabec" <jernej.skrabec@gmail.com>,
	"Jonathan Hunter" <jonathanh@nvidia.com>,
	"Kishon Vijay Abraham I" <kishon@kernel.org>,
	"Liam Girdwood" <lgirdwood@gmail.com>,
	"Linus Walleij" <linus.walleij@linaro.org>,
	"Luca Ceresoli" <luca.ceresoli@bootlin.com>,
	"Manivannan Sadhasivam" <mani@kernel.org>,
	"Mark Brown" <broonie@kernel.org>,
	"Markus Schneider-Pargmann" <msp@baylibre.com>,
	"Max Filippov" <jcmvbkbc@gmail.com>,
	"Maxime Coquelin" <mcoquelin.stm32@gmail.com>,
	"Mikko Perttunen" <mperttunen@nvidia.com>,
	"Miles Chen" <miles.chen@mediatek.com>,
	"Nicolas Ferre" <nicolas.ferre@microchip.com>,
	"Orson Zhai" <orsonzhai@gmail.com>,
	"Paul Cercueil" <paul@crapouillou.net>,
	"Peng Fan" <peng.fan@nxp.com>,
	"Peter De Schrijver" <pdeschrijver@nvidia.com>,
	"Prashant Gaikwad" <pgaikwad@nvidia.com>,
	"Richard Fitzgerald" <rf@opensource.cirrus.com>,
	"Samuel Holland" <samuel@sholland.org>,
	"Sascha Hauer" <s.hauer@pengutronix.de>,
	"Sekhar Nori" <nsekhar@ti.com>, "Shawn Guo" <shawnguo@kernel.org>,
	"Takashi Iwai" <tiwai@suse.com>,
	"Thierry Reding" <thierry.reding@gmail.com>,
	"Ulf Hansson" <ulf.hansson@linaro.org>,
	"Vinod Koul" <vkoul@kernel.org>,
	dri-devel@lists.freedesktop.org,
	linux-actions@lists.infradead.org,
	linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org,
	linux-phy@lists.infradead.org, linux-renesas-soc@vger.kernel.org,
	linux-rtc@vger.kernel.org,
	linux-stm32@st-md-mailman.stormreply.com,
	linux-sunxi@lists.linux.dev, linux-tegra@vger.kernel.org,
	"NXP Linux Team" <linux-imx@nxp.com>,
	patches@opensource.cirrus.com,
	"Pengutronix Kernel Team" <kernel@pengutronix.de>
Subject: [PATCH v4 04/68] clk: Introduce clk_hw_determine_rate_no_reparent()
Date: Fri, 05 May 2023 13:25:06 +0200	[thread overview]
Message-ID: <20221018-clk-range-checks-fixes-v4-4-971d5077e7d2@cerno.tech> (raw)
In-Reply-To: <20221018-clk-range-checks-fixes-v4-0-971d5077e7d2@cerno.tech>

From: Stephen Boyd <sboyd@kernel.org>

Some clock drivers do not want to allow any reparenting on a given
clock, but usually do so by not providing any determine_rate
implementation.

Whenever we call clk_round_rate() or clk_set_rate(), this leads to
clk_core_can_round() returning false and thus the rest of the function
either forwarding the rate request to its current parent if
CLK_SET_RATE_PARENT is set, or just returning the current clock rate.

This behaviour happens implicitly, and as we move forward to making a
determine_rate implementation required for muxes, we need some way to
explicitly opt-in for that behaviour.

Fortunately, this is exactly what the clk_core_determine_rate_no_reparent()
function is doing, so we can simply make it available to drivers.

Cc: Abel Vesa <abelvesa@kernel.org>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
Cc: Alexandre Torgue <alexandre.torgue@foss.st.com>
Cc: "Andreas Färber" <afaerber@suse.de>
Cc: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Charles Keepax <ckeepax@opensource.cirrus.com>
Cc: Chen-Yu Tsai <wens@csie.org>
Cc: Chen-Yu Tsai <wenst@chromium.org>
Cc: Chunyan Zhang <zhang.lyra@gmail.com>
Cc: Claudiu Beznea <claudiu.beznea@microchip.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: David Airlie <airlied@gmail.com>
Cc: David Lechner <david@lechnology.com>
Cc: Dinh Nguyen <dinguyen@kernel.org>
Cc: Fabio Estevam <festevam@gmail.com>
Cc: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: Jaroslav Kysela <perex@perex.cz>
Cc: Jernej Skrabec <jernej.skrabec@gmail.com>
Cc: Jonathan Hunter <jonathanh@nvidia.com>
Cc: Kishon Vijay Abraham I <kishon@kernel.org>
Cc: Liam Girdwood <lgirdwood@gmail.com>
Cc: Linus Walleij <linus.walleij@linaro.org>
Cc: Luca Ceresoli <luca.ceresoli@bootlin.com>
Cc: Manivannan Sadhasivam <mani@kernel.org>
Cc: Mark Brown <broonie@kernel.org>
Cc: Markus Schneider-Pargmann <msp@baylibre.com>
Cc: Max Filippov <jcmvbkbc@gmail.com>
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Cc: Mikko Perttunen <mperttunen@nvidia.com>
Cc: Miles Chen <miles.chen@mediatek.com>
Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
Cc: Orson Zhai <orsonzhai@gmail.com>
Cc: Paul Cercueil <paul@crapouillou.net>
Cc: Peng Fan <peng.fan@nxp.com>
Cc: Peter De Schrijver <pdeschrijver@nvidia.com>
Cc: Prashant Gaikwad <pgaikwad@nvidia.com>
Cc: Richard Fitzgerald <rf@opensource.cirrus.com>
Cc: Samuel Holland <samuel@sholland.org>
Cc: Sascha Hauer <s.hauer@pengutronix.de>
Cc: Sekhar Nori <nsekhar@ti.com>
Cc: Shawn Guo <shawnguo@kernel.org>
Cc: Takashi Iwai <tiwai@suse.com>
Cc: Thierry Reding <thierry.reding@gmail.com>
Cc: Ulf Hansson <ulf.hansson@linaro.org>
Cc: Vinod Koul <vkoul@kernel.org>
Cc: dri-devel@lists.freedesktop.org
Cc: linux-actions@lists.infradead.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-mips@vger.kernel.org
Cc: linux-phy@lists.infradead.org
Cc: linux-renesas-soc@vger.kernel.org
Cc: linux-rtc@vger.kernel.org
Cc: linux-stm32@st-md-mailman.stormreply.com
Cc: linux-sunxi@lists.linux.dev
Cc: linux-tegra@vger.kernel.org
Cc: NXP Linux Team <linux-imx@nxp.com>
Cc: patches@opensource.cirrus.com
Cc: Pengutronix Kernel Team <kernel@pengutronix.de>
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: Maxime Ripard <maxime@cerno.tech>
---
 drivers/clk/clk.c            |  18 +++++
 drivers/clk/clk_test.c       | 152 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/clk-provider.h |   2 +
 3 files changed, 172 insertions(+)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index f57f821a5e5a..5365595433c8 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -783,6 +783,24 @@ int __clk_mux_determine_rate_closest(struct clk_hw *hw,
 }
 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
 
+/*
+ * clk_hw_determine_rate_no_reparent - clk_ops::determine_rate implementation for a clk that doesn't reparent
+ * @hw: mux type clk to determine rate on
+ * @req: rate request, also used to return preferred frequency
+ *
+ * Helper for finding best parent rate to provide a given frequency.
+ * This can be used directly as a determine_rate callback (e.g. for a
+ * mux), or from a more complex clock that may combine a mux with other
+ * operations.
+ *
+ * Returns: 0 on success, -EERROR value on error
+ */
+int clk_hw_determine_rate_no_reparent(struct clk_hw *hw,
+				      struct clk_rate_request *req)
+{
+	return clk_core_determine_rate_no_reparent(hw, req);
+}
+
 /***        clk api        ***/
 
 static void clk_core_rate_unprotect(struct clk_core *core)
diff --git a/drivers/clk/clk_test.c b/drivers/clk/clk_test.c
index 2cb51153750d..b3ed3b0e4c31 100644
--- a/drivers/clk/clk_test.c
+++ b/drivers/clk/clk_test.c
@@ -141,6 +141,12 @@ static const struct clk_ops clk_multiple_parents_mux_ops = {
 	.determine_rate = __clk_mux_determine_rate_closest,
 };
 
+static const struct clk_ops clk_multiple_parents_no_reparent_mux_ops = {
+	.determine_rate = clk_hw_determine_rate_no_reparent,
+	.get_parent = clk_multiple_parents_mux_get_parent,
+	.set_parent = clk_multiple_parents_mux_set_parent,
+};
+
 static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
 {
 	struct clk_dummy_context *ctx;
@@ -2395,10 +2401,156 @@ static struct kunit_suite clk_mux_notifier_test_suite = {
 	.test_cases = clk_mux_notifier_test_cases,
 };
 
+static int
+clk_mux_no_reparent_test_init(struct kunit *test)
+{
+	struct clk_multiple_parent_ctx *ctx;
+	const char *parents[2] = { "parent-0", "parent-1"};
+	int ret;
+
+	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+	if (!ctx)
+		return -ENOMEM;
+	test->priv = ctx;
+
+	ctx->parents_ctx[0].hw.init = CLK_HW_INIT_NO_PARENT("parent-0",
+							    &clk_dummy_rate_ops,
+							    0);
+	ctx->parents_ctx[0].rate = DUMMY_CLOCK_RATE_1;
+	ret = clk_hw_register(NULL, &ctx->parents_ctx[0].hw);
+	if (ret)
+		return ret;
+
+	ctx->parents_ctx[1].hw.init = CLK_HW_INIT_NO_PARENT("parent-1",
+							    &clk_dummy_rate_ops,
+							    0);
+	ctx->parents_ctx[1].rate = DUMMY_CLOCK_RATE_2;
+	ret = clk_hw_register(NULL, &ctx->parents_ctx[1].hw);
+	if (ret)
+		return ret;
+
+	ctx->current_parent = 0;
+	ctx->hw.init = CLK_HW_INIT_PARENTS("test-mux", parents,
+					   &clk_multiple_parents_no_reparent_mux_ops,
+					   0);
+	ret = clk_hw_register(NULL, &ctx->hw);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static void
+clk_mux_no_reparent_test_exit(struct kunit *test)
+{
+	struct clk_multiple_parent_ctx *ctx = test->priv;
+
+	clk_hw_unregister(&ctx->hw);
+	clk_hw_unregister(&ctx->parents_ctx[0].hw);
+	clk_hw_unregister(&ctx->parents_ctx[1].hw);
+}
+
+/*
+ * Test that if the we have a mux that cannot change parent and we call
+ * clk_round_rate() on it with a rate that should cause it to change
+ * parent, it won't.
+ */
+static void clk_mux_no_reparent_round_rate(struct kunit *test)
+{
+	struct clk_multiple_parent_ctx *ctx = test->priv;
+	struct clk_hw *hw = &ctx->hw;
+	struct clk *clk = clk_hw_get_clk(hw, NULL);
+	struct clk *other_parent, *parent;
+	unsigned long other_parent_rate;
+	unsigned long parent_rate;
+	long rounded_rate;
+
+	parent = clk_get_parent(clk);
+	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
+
+	parent_rate = clk_get_rate(parent);
+	KUNIT_ASSERT_GT(test, parent_rate, 0);
+
+	other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
+	KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
+
+	other_parent_rate = clk_get_rate(other_parent);
+	KUNIT_ASSERT_GT(test, other_parent_rate, 0);
+	clk_put(other_parent);
+
+	rounded_rate = clk_round_rate(clk, other_parent_rate);
+	KUNIT_ASSERT_GT(test, rounded_rate, 0);
+	KUNIT_EXPECT_EQ(test, rounded_rate, parent_rate);
+
+	clk_put(clk);
+}
+
+/*
+ * Test that if the we have a mux that cannot change parent and we call
+ * clk_set_rate() on it with a rate that should cause it to change
+ * parent, it won't.
+ */
+static void clk_mux_no_reparent_set_rate(struct kunit *test)
+{
+	struct clk_multiple_parent_ctx *ctx = test->priv;
+	struct clk_hw *hw = &ctx->hw;
+	struct clk *clk = clk_hw_get_clk(hw, NULL);
+	struct clk *other_parent, *parent;
+	unsigned long other_parent_rate;
+	unsigned long parent_rate;
+	unsigned long rate;
+	int ret;
+
+	parent = clk_get_parent(clk);
+	KUNIT_ASSERT_PTR_NE(test, parent, NULL);
+
+	parent_rate = clk_get_rate(parent);
+	KUNIT_ASSERT_GT(test, parent_rate, 0);
+
+	other_parent = clk_hw_get_clk(&ctx->parents_ctx[1].hw, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other_parent);
+	KUNIT_ASSERT_FALSE(test, clk_is_match(parent, other_parent));
+
+	other_parent_rate = clk_get_rate(other_parent);
+	KUNIT_ASSERT_GT(test, other_parent_rate, 0);
+	clk_put(other_parent);
+
+	ret = clk_set_rate(clk, other_parent_rate);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	rate = clk_get_rate(clk);
+	KUNIT_ASSERT_GT(test, rate, 0);
+	KUNIT_EXPECT_EQ(test, rate, parent_rate);
+
+	clk_put(clk);
+}
+
+static struct kunit_case clk_mux_no_reparent_test_cases[] = {
+	KUNIT_CASE(clk_mux_no_reparent_round_rate),
+	KUNIT_CASE(clk_mux_no_reparent_set_rate),
+	{}
+};
+
+/*
+ * Test suite for a clock mux that isn't allowed to change parent, using
+ * the clk_hw_determine_rate_no_reparent() helper.
+ *
+ * These tests exercise that helper, and the proper selection of
+ * rates and parents.
+ */
+static struct kunit_suite clk_mux_no_reparent_test_suite = {
+	.name = "clk-mux-no-reparent",
+	.init = clk_mux_no_reparent_test_init,
+	.exit = clk_mux_no_reparent_test_exit,
+	.test_cases = clk_mux_no_reparent_test_cases,
+};
+
 kunit_test_suites(
 	&clk_leaf_mux_set_rate_parent_test_suite,
 	&clk_test_suite,
 	&clk_multiple_parents_mux_test_suite,
+	&clk_mux_no_reparent_test_suite,
 	&clk_mux_notifier_test_suite,
 	&clk_orphan_transparent_multiple_parent_mux_test_suite,
 	&clk_orphan_transparent_single_parent_test_suite,
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 28ff6f1a6ada..f8f220fb5dab 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -1333,6 +1333,8 @@ int __clk_mux_determine_rate_closest(struct clk_hw *hw,
 int clk_mux_determine_rate_flags(struct clk_hw *hw,
 				 struct clk_rate_request *req,
 				 unsigned long flags);
+int clk_hw_determine_rate_no_reparent(struct clk_hw *hw,
+				      struct clk_rate_request *req);
 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent);
 void clk_hw_get_rate_range(struct clk_hw *hw, unsigned long *min_rate,
 			   unsigned long *max_rate);

-- 
2.40.0


  parent reply	other threads:[~2023-05-05 11:26 UTC|newest]

Thread overview: 152+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-05 11:25 [PATCH v4 00/68] clk: Make determine_rate mandatory for muxes Maxime Ripard
2023-05-05 11:25 ` Maxime Ripard
2023-05-05 11:25 ` Maxime Ripard
2023-05-05 11:25 ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 01/68] clk: Export clk_hw_forward_rate_request() Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 02/68] clk: test: Fix type sign of rounded rate variables Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 03/68] clk: Move no reparent case into a separate function Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
     [not found]   ` <CGME20230613111502eucas1p2644889c9de1abfe1a14a3b549772f247@eucas1p2.samsung.com>
2023-06-13 11:15     ` Marek Szyprowski
2023-06-13 11:15       ` Marek Szyprowski
2023-06-13 11:15       ` Marek Szyprowski
2023-06-13 11:15       ` Marek Szyprowski
     [not found]       ` <CGME20230613121511eucas1p2595e0de21fadbafc1f6ffdc5636b9271@eucas1p2.samsung.com>
2023-06-13 12:15         ` Marek Szyprowski
2023-06-13 12:15           ` Marek Szyprowski
2023-06-13 12:15           ` Marek Szyprowski
2023-06-13 12:15           ` Marek Szyprowski
2023-06-13 12:29           ` Maxime Ripard
2023-06-13 12:29             ` Maxime Ripard
2023-06-13 12:29             ` Maxime Ripard
2023-06-13 12:29             ` Maxime Ripard
2023-05-05 11:25 ` Maxime Ripard [this message]
2023-05-05 11:25   ` [PATCH v4 04/68] clk: Introduce clk_hw_determine_rate_no_reparent() Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 17:15   ` kernel test robot
2023-05-05 17:15     ` kernel test robot
2023-05-05 17:15     ` kernel test robot
2023-05-05 11:25 ` [PATCH v4 05/68] clk: lan966x: Remove unused round_rate hook Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 06/68] clk: nodrv: Add a determine_rate hook Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 07/68] clk: test: " Maxime Ripard
2023-06-09  1:41   ` Stephen Boyd
2023-06-13  8:21     ` Maxime Ripard
2023-06-13 18:39       ` Stephen Boyd
2023-06-19 13:20         ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 08/68] clk: actions: composite: Add a determine_rate hook for pass clk Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 09/68] clk: at91: main: Add a determine_rate hook Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 10/68] clk: at91: sckc: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 11/68] clk: berlin: div: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 12/68] clk: cdce706: " Maxime Ripard
2023-05-05 21:00   ` kernel test robot
2023-05-05 11:25 ` [PATCH v4 13/68] clk: k210: pll: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 14/68] clk: k210: aclk: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 15/68] clk: k210: mux: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 16/68] clk: lmk04832: clkout: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 17/68] clk: lochnagar: " Maxime Ripard
2023-05-05 17:46   ` kernel test robot
2023-05-05 18:47   ` kernel test robot
2023-05-05 11:25 ` [PATCH v4 18/68] clk: qoriq: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 19/68] clk: si5341: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 20/68] clk: stm32f4: mux: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 21/68] clk: vc5: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 22/68] clk: vc5: clkout: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 23/68] clk: wm831x: " Maxime Ripard
2023-05-05 18:06   ` kernel test robot
2023-05-05 11:25 ` [PATCH v4 24/68] clk: davinci: da8xx-cfgchip: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 25/68] " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 26/68] clk: imx: busy: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 27/68] clk: imx: fixup-mux: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 28/68] clk: imx: scu: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-06  0:37   ` kernel test robot
2023-05-06  0:37     ` kernel test robot
2023-05-05 11:25 ` [PATCH v4 29/68] clk: mediatek: cpumux: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-08  2:36   ` Chen-Yu Tsai
2023-05-08  2:36     ` Chen-Yu Tsai
2023-05-05 11:25 ` [PATCH v4 30/68] clk: pxa: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 31/68] clk: renesas: r9a06g032: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 32/68] clk: socfpga: gate: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 33/68] clk: stm32: core: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 34/68] clk: tegra: bpmp: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 35/68] clk: tegra: super: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 36/68] clk: tegra: periph: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 37/68] clk: ux500: prcmu: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 38/68] clk: ux500: sysctrl: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 39/68] clk: versatile: sp810: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:30   ` Linus Walleij
2023-05-05 11:30     ` Linus Walleij
2023-05-05 19:04     ` Pawel Moll
2023-05-05 19:04       ` Pawel Moll
2023-05-05 11:25 ` [PATCH v4 40/68] drm/tegra: sor: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 41/68] phy: cadence: sierra: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 42/68] phy: cadence: torrent: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 43/68] phy: ti: am654-serdes: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 44/68] phy: ti: j721e-wiz: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 45/68] rtc: sun6i: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 18:46   ` Jernej Škrabec
2023-05-05 18:46     ` Jernej Škrabec
2023-05-05 11:25 ` [PATCH v4 46/68] ASoC: tlv320aic32x4: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 47/68] clk: actions: composite: div: Switch to determine_rate Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 48/68] clk: actions: composite: fact: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 49/68] clk: at91: smd: " Maxime Ripard
2023-05-05 11:25   ` Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 50/68] clk: axi-clkgen: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 51/68] clk: cdce706: divider: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 52/68] clk: cdce706: clkout: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 53/68] clk: si5341: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 54/68] clk: si5351: pll: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 55/68] clk: si5351: msynth: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 56/68] clk: si5351: clkout: " Maxime Ripard
2023-05-05 11:25 ` [PATCH v4 57/68] clk: da8xx: clk48: " Maxime Ripard
2023-05-05 11:26 ` [PATCH v4 58/68] clk: imx: scu: " Maxime Ripard
2023-05-05 11:26   ` Maxime Ripard
2023-05-05 11:26 ` [PATCH v4 59/68] clk: ingenic: cgu: " Maxime Ripard
2023-05-05 11:26 ` [PATCH v4 60/68] clk: ingenic: tcu: " Maxime Ripard
2023-05-05 11:26 ` [PATCH v4 61/68] clk: sprd: composite: " Maxime Ripard
2023-06-13 17:11   ` Harshit Mogalapalli
2023-06-13 19:21     ` Stephen Boyd
2023-06-13 19:45       ` Harshit Mogalapalli
2023-05-05 11:26 ` [PATCH v4 62/68] clk: st: flexgen: " Maxime Ripard
2023-05-05 11:26 ` [PATCH v4 63/68] clk: stm32: composite: " Maxime Ripard
2023-05-05 11:26   ` Maxime Ripard
2023-05-05 11:26 ` [PATCH v4 64/68] clk: tegra: periph: " Maxime Ripard
2023-05-05 11:26 ` [PATCH v4 65/68] clk: tegra: super: " Maxime Ripard
2023-06-18 23:38   ` Dmitry Osipenko
2023-06-19  7:26     ` Maxime Ripard
2023-06-20 19:09       ` Stephen Boyd
2023-06-21 15:35         ` Thierry Reding
2023-06-22 11:24           ` Maxime Ripard
2023-06-23 14:51             ` Thierry Reding
2023-06-23 15:02               ` Maxime Ripard
2023-06-22 11:32           ` Dmitry Osipenko
2023-06-30  4:57           ` Stephen Boyd
2023-05-05 11:26 ` [PATCH v4 66/68] ASoC: tlv320aic32x4: pll: " Maxime Ripard
2023-05-05 11:26   ` Maxime Ripard
2023-05-05 11:26 ` [PATCH v4 67/68] ASoC: tlv320aic32x4: div: " Maxime Ripard
2023-05-05 11:26   ` Maxime Ripard
2023-05-05 11:26 ` [PATCH v4 68/68] clk: Forbid to register a mux without determine_rate Maxime Ripard
2023-05-05 11:26   ` Maxime Ripard
2023-05-05 11:26   ` Maxime Ripard
2023-06-09  1:49 ` [PATCH v4 00/68] clk: Make determine_rate mandatory for muxes Stephen Boyd
2023-06-09  1:49   ` Stephen Boyd
2023-06-09  1:49   ` Stephen Boyd

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221018-clk-range-checks-fixes-v4-4-971d5077e7d2@cerno.tech \
    --to=maxime@cerno.tech \
    --cc=a.zummo@towertech.it \
    --cc=abelvesa@kernel.org \
    --cc=afaerber@suse.de \
    --cc=alexandre.belloni@bootlin.com \
    --cc=alexandre.torgue@foss.st.com \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=broonie@kernel.org \
    --cc=ckeepax@opensource.cirrus.com \
    --cc=claudiu.beznea@microchip.com \
    --cc=david@lechnology.com \
    --cc=dinguyen@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=geert+renesas@glider.be \
    --cc=jcmvbkbc@gmail.com \
    --cc=jernej.skrabec@gmail.com \
    --cc=jonathanh@nvidia.com \
    --cc=kernel@pengutronix.de \
    --cc=kishon@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-actions@lists.infradead.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-imx@nxp.com \
    --cc=linux-mips@vger.kernel.org \
    --cc=linux-phy@lists.infradead.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=linux-sunxi@lists.linux.dev \
    --cc=linux-tegra@vger.kernel.org \
    --cc=luca.ceresoli@bootlin.com \
    --cc=mani@kernel.org \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=miles.chen@mediatek.com \
    --cc=mperttunen@nvidia.com \
    --cc=msp@baylibre.com \
    --cc=mturquette@baylibre.com \
    --cc=nicolas.ferre@microchip.com \
    --cc=nsekhar@ti.com \
    --cc=orsonzhai@gmail.com \
    --cc=patches@opensource.cirrus.com \
    --cc=paul@crapouillou.net \
    --cc=pdeschrijver@nvidia.com \
    --cc=peng.fan@nxp.com \
    --cc=perex@perex.cz \
    --cc=pgaikwad@nvidia.com \
    --cc=rf@opensource.cirrus.com \
    --cc=s.hauer@pengutronix.de \
    --cc=samuel@sholland.org \
    --cc=sboyd@kernel.org \
    --cc=shawnguo@kernel.org \
    --cc=thierry.reding@gmail.com \
    --cc=tiwai@suse.com \
    --cc=ulf.hansson@linaro.org \
    --cc=vkoul@kernel.org \
    --cc=wens@csie.org \
    --cc=wenst@chromium.org \
    --cc=zhang.lyra@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.