[PATCH 1/2] clk: Add generic sync_state callback for disabling unused clocks

From: Abel Vesa
Date: Mon Oct 24 2022 - 16:18:43 EST


There are unused clocks that need to remain untouched by clk_disable_unused,
and most likely could be disabled later on sync_state. So provide a generic
sync_state callback for the clock providers that register such clocks.
Then, use the same mechanism as clk_disable_unused from that generic
callback, but pass the device to make sure only the clocks belonging to
the current clock provider get disabled, if unused. Also, during the
default clk_disable_unused, if the driver that registered the clock has
the generic clk_sync_state_disable_unused callback set for sync_state,
skip disabling its clocks.

Signed-off-by: Abel Vesa <abel.vesa@xxxxxxxxxx>
---

Here is the link to the RFC:

https://lore.kernel.org/all/20220929151047.wom3m2ydgxme5nhh@xxxxxxxxxxx/

Changes since RFC:
* Added from_sync_state local variable, as Bjorn suggested
* Dropped the addition extra condition for the CLK_IGNORE_UNUSED
* Changed the comments above the sync_state checking
* Moved back the clk_ignore_unused check to clk_disable_unused_subtree
function, as Bjorn suggested

drivers/clk/clk.c | 55 ++++++++++++++++++++++++++++++------
include/linux/clk-provider.h | 1 +
2 files changed, 47 insertions(+), 9 deletions(-)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index c3c3f8c07258..acf5139e16d8 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -1292,14 +1292,27 @@ static void clk_core_disable_unprepare(struct clk_core *core)
clk_core_unprepare_lock(core);
}

-static void __init clk_unprepare_unused_subtree(struct clk_core *core)
+static void clk_unprepare_unused_subtree(struct clk_core *core,
+ struct device *dev)
{
+ bool from_sync_state = !!dev;
struct clk_core *child;

lockdep_assert_held(&prepare_lock);

hlist_for_each_entry(child, &core->children, child_node)
- clk_unprepare_unused_subtree(child);
+ clk_unprepare_unused_subtree(child, dev);
+
+ if (from_sync_state && core->dev != dev)
+ return;
+
+ /*
+ * clock will be unprepared on sync_state,
+ * so leave as is for now
+ */
+ if (!from_sync_state && dev_has_sync_state(core->dev) &&
+ core->dev->driver->sync_state == clk_sync_state_disable_unused)
+ return;

if (core->prepare_count)
return;
@@ -1322,15 +1335,28 @@ static void __init clk_unprepare_unused_subtree(struct clk_core *core)
clk_pm_runtime_put(core);
}

-static void __init clk_disable_unused_subtree(struct clk_core *core)
+static void clk_disable_unused_subtree(struct clk_core *core,
+ struct device *dev)
{
+ bool from_sync_state = !!dev;
struct clk_core *child;
unsigned long flags;

lockdep_assert_held(&prepare_lock);

hlist_for_each_entry(child, &core->children, child_node)
- clk_disable_unused_subtree(child);
+ clk_disable_unused_subtree(child, dev);
+
+ if (from_sync_state && core->dev != dev)
+ return;
+
+ /*
+ * clock will be disabled on sync_state,
+ * so leave as is for now
+ */
+ if (!from_sync_state &&
+ core->dev->driver->sync_state == clk_sync_state_disable_unused)
+ return;

if (core->flags & CLK_OPS_PARENT_ENABLE)
clk_core_prepare_enable(core->parent);
@@ -1376,7 +1402,7 @@ static int __init clk_ignore_unused_setup(char *__unused)
}
__setup("clk_ignore_unused", clk_ignore_unused_setup);

-static int __init clk_disable_unused(void)
+static void __clk_disable_unused(struct device *dev)
{
struct clk_core *core;

@@ -1388,23 +1414,34 @@ static int __init clk_disable_unused(void)
clk_prepare_lock();

hlist_for_each_entry(core, &clk_root_list, child_node)
- clk_disable_unused_subtree(core);
+ clk_disable_unused_subtree(core, dev);

hlist_for_each_entry(core, &clk_orphan_list, child_node)
- clk_disable_unused_subtree(core);
+ clk_disable_unused_subtree(core, dev);

hlist_for_each_entry(core, &clk_root_list, child_node)
- clk_unprepare_unused_subtree(core);
+ clk_unprepare_unused_subtree(core, dev);

hlist_for_each_entry(core, &clk_orphan_list, child_node)
- clk_unprepare_unused_subtree(core);
+ clk_unprepare_unused_subtree(core, dev);

clk_prepare_unlock();
+}
+
+static int __init clk_disable_unused(void)
+{
+ __clk_disable_unused(NULL);

return 0;
}
late_initcall_sync(clk_disable_unused);

+void clk_sync_state_disable_unused(struct device *dev)
+{
+ __clk_disable_unused(dev);
+}
+EXPORT_SYMBOL_GPL(clk_sync_state_disable_unused);
+
static int clk_core_determine_round_nolock(struct clk_core *core,
struct clk_rate_request *req)
{
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 267cd06b54a0..06a8622f90cf 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -718,6 +718,7 @@ struct clk *clk_register_divider_table(struct device *dev, const char *name,
void __iomem *reg, u8 shift, u8 width,
u8 clk_divider_flags, const struct clk_div_table *table,
spinlock_t *lock);
+void clk_sync_state_disable_unused(struct device *dev);
/**
* clk_register_divider - register a divider clock with the clock framework
* @dev: device registering this clock
--
2.34.1