@@ -84,6 +84,8 @@ struct clk {
unsigned long min_rate;
unsigned long max_rate;
struct hlist_node clks_node;
+ unsigned int enable_count;
+ unsigned int prepare_count;
};
/*** locking ***/
@@ -600,6 +602,9 @@ void clk_unprepare(struct clk *clk)
return;
clk_prepare_lock();
+ if (WARN_ON(clk->prepare_count == 0))
+ return;
+ clk->prepare_count--;
clk_core_unprepare(clk->core);
clk_prepare_unlock();
}
@@ -657,6 +662,7 @@ int clk_prepare(struct clk *clk)
return 0;
clk_prepare_lock();
+ clk->prepare_count++;
ret = clk_core_prepare(clk->core);
clk_prepare_unlock();
@@ -707,6 +713,9 @@ void clk_disable(struct clk *clk)
return;
flags = clk_enable_lock();
+ if (WARN_ON(clk->enable_count == 0))
+ return;
+ clk->enable_count--;
clk_core_disable(clk->core);
clk_enable_unlock(flags);
}
@@ -769,6 +778,7 @@ int clk_enable(struct clk *clk)
return 0;
flags = clk_enable_lock();
+ clk->enable_count++;
ret = clk_core_enable(clk->core);
clk_enable_unlock(flags);
--
1.9.1
From ace76f6ed634a69c499f8440a98d4b5a54d78368 Mon Sep 17 00:00:00 2001
From: Michael Turquette <mturquette@baylibre.com>
Date: Thu, 30 Jul 2015 12:52:26 -0700
Subject: [PATCH 2/2] clk: clk_put WARNs if user has not disabled clk
From the clk_put kerneldoc in include/linux/clk.h:
"""
Note: drivers must ensure that all clk_enable calls made on this clock
source are balanced by clk_disable calls prior to calling this function.
"""
The common clock framework implementation of the clk.h api has per-user
reference counts for calls to clk_prepare and clk_disable. As such it
can enforce the requirement to properly call clk_disable and
clk_unprepare before calling clk_put.
Because this requirement is probably violated in many places, this patch
starts with a simple warning. Once offending code has been fixed this
check could additionally release the reference counts automatically.
Signed-off-by: Michael Turquette <mturquette@baylibre.com>
---
drivers/clk/clk.c | 8 ++++++++
1 file changed, 8 insertions(+)
@@ -2764,6 +2764,14 @@ void __clk_put(struct clk *clk)
clk->max_rate < clk->core->req_rate)
clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
+ /*
+ * before calling clk_put, all calls to clk_prepare and clk_enable from
+ * a given user must be balanced with calls to clk_disable and
+ * clk_unprepare by that same user
+ */
+ WARN_ON(clk->prepare_count);
+ WARN_ON(clk->enable_count);
+
owner = clk->core->owner;
kref_put(&clk->core->ref, __clk_release);