clkdev.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/clk/clkdev.c
  4. *
  5. * Copyright (C) 2008 Russell King.
  6. *
  7. * Helper for the clk API to assist looking up a struct clk.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/device.h>
  12. #include <linux/list.h>
  13. #include <linux/errno.h>
  14. #include <linux/err.h>
  15. #include <linux/string.h>
  16. #include <linux/mutex.h>
  17. #include <linux/clk.h>
  18. #include <linux/clkdev.h>
  19. #include <linux/clk-provider.h>
  20. #include <linux/of.h>
  21. #include "clk.h"
  22. static LIST_HEAD(clocks);
  23. static DEFINE_MUTEX(clocks_mutex);
  24. /*
  25. * Find the correct struct clk for the device and connection ID.
  26. * We do slightly fuzzy matching here:
  27. * An entry with a NULL ID is assumed to be a wildcard.
  28. * If an entry has a device ID, it must match
  29. * If an entry has a connection ID, it must match
  30. * Then we take the most specific entry - with the following
  31. * order of precedence: dev+con > dev only > con only.
  32. */
  33. static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
  34. {
  35. struct clk_lookup *p, *cl = NULL;
  36. int match, best_found = 0, best_possible = 0;
  37. if (dev_id)
  38. best_possible += 2;
  39. if (con_id)
  40. best_possible += 1;
  41. lockdep_assert_held(&clocks_mutex);
  42. list_for_each_entry(p, &clocks, node) {
  43. match = 0;
  44. if (p->dev_id) {
  45. if (!dev_id || strcmp(p->dev_id, dev_id))
  46. continue;
  47. match += 2;
  48. }
  49. if (p->con_id) {
  50. if (!con_id || strcmp(p->con_id, con_id))
  51. continue;
  52. match += 1;
  53. }
  54. if (match > best_found) {
  55. cl = p;
  56. if (match != best_possible)
  57. best_found = match;
  58. else
  59. break;
  60. }
  61. }
  62. return cl;
  63. }
  64. struct clk_hw *clk_find_hw(const char *dev_id, const char *con_id)
  65. {
  66. struct clk_lookup *cl;
  67. struct clk_hw *hw = ERR_PTR(-ENOENT);
  68. mutex_lock(&clocks_mutex);
  69. cl = clk_find(dev_id, con_id);
  70. if (cl)
  71. hw = cl->clk_hw;
  72. mutex_unlock(&clocks_mutex);
  73. return hw;
  74. }
  75. static struct clk *__clk_get_sys(struct device *dev, const char *dev_id,
  76. const char *con_id)
  77. {
  78. struct clk_hw *hw = clk_find_hw(dev_id, con_id);
  79. return clk_hw_create_clk(dev, hw, dev_id, con_id);
  80. }
  81. struct clk *clk_get_sys(const char *dev_id, const char *con_id)
  82. {
  83. return __clk_get_sys(NULL, dev_id, con_id);
  84. }
  85. EXPORT_SYMBOL(clk_get_sys);
  86. struct clk *clk_get(struct device *dev, const char *con_id)
  87. {
  88. const char *dev_id = dev ? dev_name(dev) : NULL;
  89. struct clk_hw *hw;
  90. if (dev && dev->of_node) {
  91. hw = of_clk_get_hw(dev->of_node, 0, con_id);
  92. if (!IS_ERR(hw) || PTR_ERR(hw) == -EPROBE_DEFER)
  93. return clk_hw_create_clk(dev, hw, dev_id, con_id);
  94. }
  95. return __clk_get_sys(dev, dev_id, con_id);
  96. }
  97. EXPORT_SYMBOL(clk_get);
  98. void clk_put(struct clk *clk)
  99. {
  100. __clk_put(clk);
  101. }
  102. EXPORT_SYMBOL(clk_put);
  103. static void __clkdev_add(struct clk_lookup *cl)
  104. {
  105. mutex_lock(&clocks_mutex);
  106. list_add_tail(&cl->node, &clocks);
  107. mutex_unlock(&clocks_mutex);
  108. }
  109. void clkdev_add(struct clk_lookup *cl)
  110. {
  111. if (!cl->clk_hw)
  112. cl->clk_hw = __clk_get_hw(cl->clk);
  113. __clkdev_add(cl);
  114. }
  115. EXPORT_SYMBOL(clkdev_add);
  116. void clkdev_add_table(struct clk_lookup *cl, size_t num)
  117. {
  118. mutex_lock(&clocks_mutex);
  119. while (num--) {
  120. cl->clk_hw = __clk_get_hw(cl->clk);
  121. list_add_tail(&cl->node, &clocks);
  122. cl++;
  123. }
  124. mutex_unlock(&clocks_mutex);
  125. }
  126. #define MAX_DEV_ID 20
  127. #define MAX_CON_ID 16
  128. struct clk_lookup_alloc {
  129. struct clk_lookup cl;
  130. char dev_id[MAX_DEV_ID];
  131. char con_id[MAX_CON_ID];
  132. };
  133. static struct clk_lookup * __ref
  134. vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
  135. va_list ap)
  136. {
  137. struct clk_lookup_alloc *cla;
  138. cla = kzalloc(sizeof(*cla), GFP_KERNEL);
  139. if (!cla)
  140. return NULL;
  141. cla->cl.clk_hw = hw;
  142. if (con_id) {
  143. strscpy(cla->con_id, con_id, sizeof(cla->con_id));
  144. cla->cl.con_id = cla->con_id;
  145. }
  146. if (dev_fmt) {
  147. vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  148. cla->cl.dev_id = cla->dev_id;
  149. }
  150. return &cla->cl;
  151. }
  152. static struct clk_lookup *
  153. vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
  154. va_list ap)
  155. {
  156. struct clk_lookup *cl;
  157. cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
  158. if (cl)
  159. __clkdev_add(cl);
  160. return cl;
  161. }
  162. /**
  163. * clkdev_create - allocate and add a clkdev lookup structure
  164. * @clk: struct clk to associate with all clk_lookups
  165. * @con_id: connection ID string on device
  166. * @dev_fmt: format string describing device name
  167. *
  168. * Returns a clk_lookup structure, which can be later unregistered and
  169. * freed.
  170. */
  171. struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
  172. const char *dev_fmt, ...)
  173. {
  174. struct clk_lookup *cl;
  175. va_list ap;
  176. va_start(ap, dev_fmt);
  177. cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
  178. va_end(ap);
  179. return cl;
  180. }
  181. EXPORT_SYMBOL_GPL(clkdev_create);
  182. /**
  183. * clkdev_hw_create - allocate and add a clkdev lookup structure
  184. * @hw: struct clk_hw to associate with all clk_lookups
  185. * @con_id: connection ID string on device
  186. * @dev_fmt: format string describing device name
  187. *
  188. * Returns a clk_lookup structure, which can be later unregistered and
  189. * freed.
  190. */
  191. struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
  192. const char *dev_fmt, ...)
  193. {
  194. struct clk_lookup *cl;
  195. va_list ap;
  196. va_start(ap, dev_fmt);
  197. cl = vclkdev_create(hw, con_id, dev_fmt, ap);
  198. va_end(ap);
  199. return cl;
  200. }
  201. EXPORT_SYMBOL_GPL(clkdev_hw_create);
  202. int clk_add_alias(const char *alias, const char *alias_dev_name,
  203. const char *con_id, struct device *dev)
  204. {
  205. struct clk *r = clk_get(dev, con_id);
  206. struct clk_lookup *l;
  207. if (IS_ERR(r))
  208. return PTR_ERR(r);
  209. l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
  210. alias_dev_name);
  211. clk_put(r);
  212. return l ? 0 : -ENODEV;
  213. }
  214. EXPORT_SYMBOL(clk_add_alias);
  215. /*
  216. * clkdev_drop - remove a clock dynamically allocated
  217. */
  218. void clkdev_drop(struct clk_lookup *cl)
  219. {
  220. mutex_lock(&clocks_mutex);
  221. list_del(&cl->node);
  222. mutex_unlock(&clocks_mutex);
  223. kfree(cl);
  224. }
  225. EXPORT_SYMBOL(clkdev_drop);
  226. static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw,
  227. const char *con_id,
  228. const char *dev_id, ...)
  229. {
  230. struct clk_lookup *cl;
  231. va_list ap;
  232. va_start(ap, dev_id);
  233. cl = vclkdev_create(hw, con_id, dev_id, ap);
  234. va_end(ap);
  235. return cl;
  236. }
  237. static int do_clk_register_clkdev(struct clk_hw *hw,
  238. struct clk_lookup **cl, const char *con_id, const char *dev_id)
  239. {
  240. if (IS_ERR(hw))
  241. return PTR_ERR(hw);
  242. /*
  243. * Since dev_id can be NULL, and NULL is handled specially, we must
  244. * pass it as either a NULL format string, or with "%s".
  245. */
  246. if (dev_id)
  247. *cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
  248. else
  249. *cl = __clk_register_clkdev(hw, con_id, NULL);
  250. return *cl ? 0 : -ENOMEM;
  251. }
  252. /**
  253. * clk_register_clkdev - register one clock lookup for a struct clk
  254. * @clk: struct clk to associate with all clk_lookups
  255. * @con_id: connection ID string on device
  256. * @dev_id: string describing device name
  257. *
  258. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  259. * clkdev.
  260. *
  261. * To make things easier for mass registration, we detect error clks
  262. * from a previous clk_register() call, and return the error code for
  263. * those. This is to permit this function to be called immediately
  264. * after clk_register().
  265. */
  266. int clk_register_clkdev(struct clk *clk, const char *con_id,
  267. const char *dev_id)
  268. {
  269. struct clk_lookup *cl;
  270. if (IS_ERR(clk))
  271. return PTR_ERR(clk);
  272. return do_clk_register_clkdev(__clk_get_hw(clk), &cl, con_id,
  273. dev_id);
  274. }
  275. EXPORT_SYMBOL(clk_register_clkdev);
  276. /**
  277. * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
  278. * @hw: struct clk_hw to associate with all clk_lookups
  279. * @con_id: connection ID string on device
  280. * @dev_id: format string describing device name
  281. *
  282. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  283. * clkdev.
  284. *
  285. * To make things easier for mass registration, we detect error clk_hws
  286. * from a previous clk_hw_register_*() call, and return the error code for
  287. * those. This is to permit this function to be called immediately
  288. * after clk_hw_register_*().
  289. */
  290. int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
  291. const char *dev_id)
  292. {
  293. struct clk_lookup *cl;
  294. return do_clk_register_clkdev(hw, &cl, con_id, dev_id);
  295. }
  296. EXPORT_SYMBOL(clk_hw_register_clkdev);
  297. static void devm_clkdev_release(void *res)
  298. {
  299. clkdev_drop(res);
  300. }
  301. /**
  302. * devm_clk_hw_register_clkdev - managed clk lookup registration for clk_hw
  303. * @dev: device this lookup is bound
  304. * @hw: struct clk_hw to associate with all clk_lookups
  305. * @con_id: connection ID string on device
  306. * @dev_id: format string describing device name
  307. *
  308. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  309. * clkdev.
  310. *
  311. * To make things easier for mass registration, we detect error clk_hws
  312. * from a previous clk_hw_register_*() call, and return the error code for
  313. * those. This is to permit this function to be called immediately
  314. * after clk_hw_register_*().
  315. */
  316. int devm_clk_hw_register_clkdev(struct device *dev, struct clk_hw *hw,
  317. const char *con_id, const char *dev_id)
  318. {
  319. struct clk_lookup *cl;
  320. int rval;
  321. rval = do_clk_register_clkdev(hw, &cl, con_id, dev_id);
  322. if (rval)
  323. return rval;
  324. return devm_add_action_or_reset(dev, devm_clkdev_release, cl);
  325. }
  326. EXPORT_SYMBOL(devm_clk_hw_register_clkdev);