clkdev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. strlcpy(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. struct clk_lookup * __ref
  163. clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
  164. {
  165. struct clk_lookup *cl;
  166. va_list ap;
  167. va_start(ap, dev_fmt);
  168. cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap);
  169. va_end(ap);
  170. return cl;
  171. }
  172. EXPORT_SYMBOL(clkdev_alloc);
  173. struct clk_lookup *
  174. clkdev_hw_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, ...)
  175. {
  176. struct clk_lookup *cl;
  177. va_list ap;
  178. va_start(ap, dev_fmt);
  179. cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
  180. va_end(ap);
  181. return cl;
  182. }
  183. EXPORT_SYMBOL(clkdev_hw_alloc);
  184. /**
  185. * clkdev_create - allocate and add a clkdev lookup structure
  186. * @clk: struct clk to associate with all clk_lookups
  187. * @con_id: connection ID string on device
  188. * @dev_fmt: format string describing device name
  189. *
  190. * Returns a clk_lookup structure, which can be later unregistered and
  191. * freed.
  192. */
  193. struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
  194. const char *dev_fmt, ...)
  195. {
  196. struct clk_lookup *cl;
  197. va_list ap;
  198. va_start(ap, dev_fmt);
  199. cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
  200. va_end(ap);
  201. return cl;
  202. }
  203. EXPORT_SYMBOL_GPL(clkdev_create);
  204. /**
  205. * clkdev_hw_create - allocate and add a clkdev lookup structure
  206. * @hw: struct clk_hw to associate with all clk_lookups
  207. * @con_id: connection ID string on device
  208. * @dev_fmt: format string describing device name
  209. *
  210. * Returns a clk_lookup structure, which can be later unregistered and
  211. * freed.
  212. */
  213. struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
  214. const char *dev_fmt, ...)
  215. {
  216. struct clk_lookup *cl;
  217. va_list ap;
  218. va_start(ap, dev_fmt);
  219. cl = vclkdev_create(hw, con_id, dev_fmt, ap);
  220. va_end(ap);
  221. return cl;
  222. }
  223. EXPORT_SYMBOL_GPL(clkdev_hw_create);
  224. int clk_add_alias(const char *alias, const char *alias_dev_name,
  225. const char *con_id, struct device *dev)
  226. {
  227. struct clk *r = clk_get(dev, con_id);
  228. struct clk_lookup *l;
  229. if (IS_ERR(r))
  230. return PTR_ERR(r);
  231. l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
  232. alias_dev_name);
  233. clk_put(r);
  234. return l ? 0 : -ENODEV;
  235. }
  236. EXPORT_SYMBOL(clk_add_alias);
  237. /*
  238. * clkdev_drop - remove a clock dynamically allocated
  239. */
  240. void clkdev_drop(struct clk_lookup *cl)
  241. {
  242. mutex_lock(&clocks_mutex);
  243. list_del(&cl->node);
  244. mutex_unlock(&clocks_mutex);
  245. kfree(cl);
  246. }
  247. EXPORT_SYMBOL(clkdev_drop);
  248. static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw,
  249. const char *con_id,
  250. const char *dev_id, ...)
  251. {
  252. struct clk_lookup *cl;
  253. va_list ap;
  254. va_start(ap, dev_id);
  255. cl = vclkdev_create(hw, con_id, dev_id, ap);
  256. va_end(ap);
  257. return cl;
  258. }
  259. static int do_clk_register_clkdev(struct clk_hw *hw,
  260. struct clk_lookup **cl, const char *con_id, const char *dev_id)
  261. {
  262. if (IS_ERR(hw))
  263. return PTR_ERR(hw);
  264. /*
  265. * Since dev_id can be NULL, and NULL is handled specially, we must
  266. * pass it as either a NULL format string, or with "%s".
  267. */
  268. if (dev_id)
  269. *cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
  270. else
  271. *cl = __clk_register_clkdev(hw, con_id, NULL);
  272. return *cl ? 0 : -ENOMEM;
  273. }
  274. /**
  275. * clk_register_clkdev - register one clock lookup for a struct clk
  276. * @clk: struct clk to associate with all clk_lookups
  277. * @con_id: connection ID string on device
  278. * @dev_id: string describing device name
  279. *
  280. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  281. * clkdev.
  282. *
  283. * To make things easier for mass registration, we detect error clks
  284. * from a previous clk_register() call, and return the error code for
  285. * those. This is to permit this function to be called immediately
  286. * after clk_register().
  287. */
  288. int clk_register_clkdev(struct clk *clk, const char *con_id,
  289. const char *dev_id)
  290. {
  291. struct clk_lookup *cl;
  292. if (IS_ERR(clk))
  293. return PTR_ERR(clk);
  294. return do_clk_register_clkdev(__clk_get_hw(clk), &cl, con_id,
  295. dev_id);
  296. }
  297. EXPORT_SYMBOL(clk_register_clkdev);
  298. /**
  299. * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
  300. * @hw: struct clk_hw to associate with all clk_lookups
  301. * @con_id: connection ID string on device
  302. * @dev_id: format string describing device name
  303. *
  304. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  305. * clkdev.
  306. *
  307. * To make things easier for mass registration, we detect error clk_hws
  308. * from a previous clk_hw_register_*() call, and return the error code for
  309. * those. This is to permit this function to be called immediately
  310. * after clk_hw_register_*().
  311. */
  312. int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
  313. const char *dev_id)
  314. {
  315. struct clk_lookup *cl;
  316. return do_clk_register_clkdev(hw, &cl, con_id, dev_id);
  317. }
  318. EXPORT_SYMBOL(clk_hw_register_clkdev);
  319. static void devm_clkdev_release(struct device *dev, void *res)
  320. {
  321. clkdev_drop(*(struct clk_lookup **)res);
  322. }
  323. static int devm_clk_match_clkdev(struct device *dev, void *res, void *data)
  324. {
  325. struct clk_lookup **l = res;
  326. return *l == data;
  327. }
  328. /**
  329. * devm_clk_release_clkdev - Resource managed clkdev lookup release
  330. * @dev: device this lookup is bound
  331. * @con_id: connection ID string on device
  332. * @dev_id: format string describing device name
  333. *
  334. * Drop the clkdev lookup created with devm_clk_hw_register_clkdev.
  335. * Normally this function will not need to be called and the resource
  336. * management code will ensure that the resource is freed.
  337. */
  338. void devm_clk_release_clkdev(struct device *dev, const char *con_id,
  339. const char *dev_id)
  340. {
  341. struct clk_lookup *cl;
  342. int rval;
  343. mutex_lock(&clocks_mutex);
  344. cl = clk_find(dev_id, con_id);
  345. mutex_unlock(&clocks_mutex);
  346. WARN_ON(!cl);
  347. rval = devres_release(dev, devm_clkdev_release,
  348. devm_clk_match_clkdev, cl);
  349. WARN_ON(rval);
  350. }
  351. EXPORT_SYMBOL(devm_clk_release_clkdev);
  352. /**
  353. * devm_clk_hw_register_clkdev - managed clk lookup registration for clk_hw
  354. * @dev: device this lookup is bound
  355. * @hw: struct clk_hw to associate with all clk_lookups
  356. * @con_id: connection ID string on device
  357. * @dev_id: format string describing device name
  358. *
  359. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  360. * clkdev.
  361. *
  362. * To make things easier for mass registration, we detect error clk_hws
  363. * from a previous clk_hw_register_*() call, and return the error code for
  364. * those. This is to permit this function to be called immediately
  365. * after clk_hw_register_*().
  366. */
  367. int devm_clk_hw_register_clkdev(struct device *dev, struct clk_hw *hw,
  368. const char *con_id, const char *dev_id)
  369. {
  370. int rval = -ENOMEM;
  371. struct clk_lookup **cl;
  372. cl = devres_alloc(devm_clkdev_release, sizeof(*cl), GFP_KERNEL);
  373. if (cl) {
  374. rval = do_clk_register_clkdev(hw, cl, con_id, dev_id);
  375. if (!rval)
  376. devres_add(dev, cl);
  377. else
  378. devres_free(cl);
  379. }
  380. return rval;
  381. }
  382. EXPORT_SYMBOL(devm_clk_hw_register_clkdev);