clk: introduce clk_is_match

Some drivers compare struct clk pointers as a means of knowing
if the two pointers reference the same clock hardware. This behavior is
dubious (drivers must not dereference struct clk), but did not cause any
regressions until the per-user struct clk patch was merged. Now the test
for matching clk's will always fail with per-user struct clk's.

clk_is_match is introduced to fix the regression and prevent drivers
from comparing the pointers manually.

Fixes: 035a61c314eb ("clk: Make clk API return per-user struct clk instances")
Cc: Russell King <linux@arm.linux.org.uk>
Cc: Shawn Guo <shawn.guo@linaro.org>
Cc: Tomeu Vizoso <tomeu.vizoso@collabora.com>
Signed-off-by: Michael Turquette <mturquette@linaro.org>
[arnd@arndb.de: Fix COMMON_CLK=N && HAS_CLK=Y config]
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
[sboyd@codeaurora.org: const arguments to clk_is_match() and
remove unnecessary ternary operation]
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>

authored by Michael Turquette and committed by Stephen Boyd 3d3801ef f55ac065

+44
+26
drivers/clk/clk.c
··· 2170 } 2171 2172 /** 2173 * __clk_init - initialize the data structures in a struct clk 2174 * @dev: device initializing this clk, placeholder for now 2175 * @clk: clk being initialized
··· 2170 } 2171 2172 /** 2173 + * clk_is_match - check if two clk's point to the same hardware clock 2174 + * @p: clk compared against q 2175 + * @q: clk compared against p 2176 + * 2177 + * Returns true if the two struct clk pointers both point to the same hardware 2178 + * clock node. Put differently, returns true if struct clk *p and struct clk *q 2179 + * share the same struct clk_core object. 2180 + * 2181 + * Returns false otherwise. Note that two NULL clks are treated as matching. 2182 + */ 2183 + bool clk_is_match(const struct clk *p, const struct clk *q) 2184 + { 2185 + /* trivial case: identical struct clk's or both NULL */ 2186 + if (p == q) 2187 + return true; 2188 + 2189 + /* true if clk->core pointers match. Avoid derefing garbage */ 2190 + if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q)) 2191 + if (p->core == q->core) 2192 + return true; 2193 + 2194 + return false; 2195 + } 2196 + EXPORT_SYMBOL_GPL(clk_is_match); 2197 + 2198 + /** 2199 * __clk_init - initialize the data structures in a struct clk 2200 * @dev: device initializing this clk, placeholder for now 2201 * @clk: clk being initialized
+18
include/linux/clk.h
··· 125 */ 126 int clk_get_phase(struct clk *clk); 127 128 #else 129 130 static inline long clk_get_accuracy(struct clk *clk) ··· 153 static inline long clk_get_phase(struct clk *clk) 154 { 155 return -ENOTSUPP; 156 } 157 158 #endif
··· 125 */ 126 int clk_get_phase(struct clk *clk); 127 128 + /** 129 + * clk_is_match - check if two clk's point to the same hardware clock 130 + * @p: clk compared against q 131 + * @q: clk compared against p 132 + * 133 + * Returns true if the two struct clk pointers both point to the same hardware 134 + * clock node. Put differently, returns true if struct clk *p and struct clk *q 135 + * share the same struct clk_core object. 136 + * 137 + * Returns false otherwise. Note that two NULL clks are treated as matching. 138 + */ 139 + bool clk_is_match(const struct clk *p, const struct clk *q); 140 + 141 #else 142 143 static inline long clk_get_accuracy(struct clk *clk) ··· 140 static inline long clk_get_phase(struct clk *clk) 141 { 142 return -ENOTSUPP; 143 + } 144 + 145 + static inline bool clk_is_match(const struct clk *p, const struct clk *q) 146 + { 147 + return p == q; 148 } 149 150 #endif