clkdev.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 24
  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. struct va_format vaf;
  139. const char *failure;
  140. va_list ap_copy;
  141. size_t max_size;
  142. ssize_t res;
  143. cla = kzalloc(sizeof(*cla), GFP_KERNEL);
  144. if (!cla)
  145. return NULL;
  146. va_copy(ap_copy, ap);
  147. cla->cl.clk_hw = hw;
  148. if (con_id) {
  149. res = strscpy(cla->con_id, con_id, sizeof(cla->con_id));
  150. if (res < 0) {
  151. max_size = sizeof(cla->con_id);
  152. failure = "connection";
  153. goto fail;
  154. }
  155. cla->cl.con_id = cla->con_id;
  156. }
  157. if (dev_fmt) {
  158. res = vsnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  159. if (res >= sizeof(cla->dev_id)) {
  160. max_size = sizeof(cla->dev_id);
  161. failure = "device";
  162. goto fail;
  163. }
  164. cla->cl.dev_id = cla->dev_id;
  165. }
  166. va_end(ap_copy);
  167. return &cla->cl;
  168. fail:
  169. if (dev_fmt)
  170. vaf.fmt = dev_fmt;
  171. else
  172. vaf.fmt = "null-device";
  173. vaf.va = &ap_copy;
  174. pr_err("%pV:%s: %s ID is greater than %zu\n",
  175. &vaf, con_id, failure, max_size);
  176. va_end(ap_copy);
  177. /*
  178. * Don't fail in this case, but as the entry won't ever match just
  179. * fill it with something that also won't match.
  180. */
  181. strscpy(cla->con_id, "bad", sizeof(cla->con_id));
  182. strscpy(cla->dev_id, "bad", sizeof(cla->dev_id));
  183. return &cla->cl;
  184. }
  185. static struct clk_lookup *
  186. vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
  187. va_list ap)
  188. {
  189. struct clk_lookup *cl;
  190. cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
  191. if (cl)
  192. __clkdev_add(cl);
  193. return cl;
  194. }
  195. /**
  196. * clkdev_create - allocate and add a clkdev lookup structure
  197. * @clk: struct clk to associate with all clk_lookups
  198. * @con_id: connection ID string on device
  199. * @dev_fmt: format string describing device name
  200. *
  201. * Returns a clk_lookup structure, which can be later unregistered and
  202. * freed.
  203. */
  204. struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
  205. const char *dev_fmt, ...)
  206. {
  207. struct clk_lookup *cl;
  208. va_list ap;
  209. va_start(ap, dev_fmt);
  210. cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
  211. va_end(ap);
  212. return cl;
  213. }
  214. EXPORT_SYMBOL_GPL(clkdev_create);
  215. /**
  216. * clkdev_hw_create - allocate and add a clkdev lookup structure
  217. * @hw: struct clk_hw to associate with all clk_lookups
  218. * @con_id: connection ID string on device
  219. * @dev_fmt: format string describing device name
  220. *
  221. * Returns a clk_lookup structure, which can be later unregistered and
  222. * freed.
  223. */
  224. struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
  225. const char *dev_fmt, ...)
  226. {
  227. struct clk_lookup *cl;
  228. va_list ap;
  229. va_start(ap, dev_fmt);
  230. cl = vclkdev_create(hw, con_id, dev_fmt, ap);
  231. va_end(ap);
  232. return cl;
  233. }
  234. EXPORT_SYMBOL_GPL(clkdev_hw_create);
  235. int clk_add_alias(const char *alias, const char *alias_dev_name,
  236. const char *con_id, struct device *dev)
  237. {
  238. struct clk *r = clk_get(dev, con_id);
  239. struct clk_lookup *l;
  240. if (IS_ERR(r))
  241. return PTR_ERR(r);
  242. l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
  243. alias_dev_name);
  244. clk_put(r);
  245. return l ? 0 : -ENODEV;
  246. }
  247. EXPORT_SYMBOL(clk_add_alias);
  248. /*
  249. * clkdev_drop - remove a clock dynamically allocated
  250. */
  251. void clkdev_drop(struct clk_lookup *cl)
  252. {
  253. mutex_lock(&clocks_mutex);
  254. list_del(&cl->node);
  255. mutex_unlock(&clocks_mutex);
  256. kfree(cl);
  257. }
  258. EXPORT_SYMBOL(clkdev_drop);
  259. static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw,
  260. const char *con_id,
  261. const char *dev_id, ...)
  262. {
  263. struct clk_lookup *cl;
  264. va_list ap;
  265. va_start(ap, dev_id);
  266. cl = vclkdev_create(hw, con_id, dev_id, ap);
  267. va_end(ap);
  268. return cl;
  269. }
  270. static int do_clk_register_clkdev(struct clk_hw *hw,
  271. struct clk_lookup **cl, const char *con_id, const char *dev_id)
  272. {
  273. if (IS_ERR(hw))
  274. return PTR_ERR(hw);
  275. /*
  276. * Since dev_id can be NULL, and NULL is handled specially, we must
  277. * pass it as either a NULL format string, or with "%s".
  278. */
  279. if (dev_id)
  280. *cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
  281. else
  282. *cl = __clk_register_clkdev(hw, con_id, NULL);
  283. return *cl ? 0 : -ENOMEM;
  284. }
  285. /**
  286. * clk_register_clkdev - register one clock lookup for a struct clk
  287. * @clk: struct clk to associate with all clk_lookups
  288. * @con_id: connection ID string on device
  289. * @dev_id: string describing device name
  290. *
  291. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  292. * clkdev.
  293. *
  294. * To make things easier for mass registration, we detect error clks
  295. * from a previous clk_register() call, and return the error code for
  296. * those. This is to permit this function to be called immediately
  297. * after clk_register().
  298. */
  299. int clk_register_clkdev(struct clk *clk, const char *con_id,
  300. const char *dev_id)
  301. {
  302. struct clk_lookup *cl;
  303. if (IS_ERR(clk))
  304. return PTR_ERR(clk);
  305. return do_clk_register_clkdev(__clk_get_hw(clk), &cl, con_id,
  306. dev_id);
  307. }
  308. EXPORT_SYMBOL(clk_register_clkdev);
  309. /**
  310. * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
  311. * @hw: struct clk_hw to associate with all clk_lookups
  312. * @con_id: connection ID string on device
  313. * @dev_id: format string describing device name
  314. *
  315. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  316. * clkdev.
  317. *
  318. * To make things easier for mass registration, we detect error clk_hws
  319. * from a previous clk_hw_register_*() call, and return the error code for
  320. * those. This is to permit this function to be called immediately
  321. * after clk_hw_register_*().
  322. */
  323. int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
  324. const char *dev_id)
  325. {
  326. struct clk_lookup *cl;
  327. return do_clk_register_clkdev(hw, &cl, con_id, dev_id);
  328. }
  329. EXPORT_SYMBOL(clk_hw_register_clkdev);
  330. static void devm_clkdev_release(void *res)
  331. {
  332. clkdev_drop(res);
  333. }
  334. /**
  335. * devm_clk_hw_register_clkdev - managed clk lookup registration for clk_hw
  336. * @dev: device this lookup is bound
  337. * @hw: struct clk_hw to associate with all clk_lookups
  338. * @con_id: connection ID string on device
  339. * @dev_id: format string describing device name
  340. *
  341. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  342. * clkdev.
  343. *
  344. * To make things easier for mass registration, we detect error clk_hws
  345. * from a previous clk_hw_register_*() call, and return the error code for
  346. * those. This is to permit this function to be called immediately
  347. * after clk_hw_register_*().
  348. */
  349. int devm_clk_hw_register_clkdev(struct device *dev, struct clk_hw *hw,
  350. const char *con_id, const char *dev_id)
  351. {
  352. struct clk_lookup *cl;
  353. int rval;
  354. rval = do_clk_register_clkdev(hw, &cl, con_id, dev_id);
  355. if (rval)
  356. return rval;
  357. return devm_add_action_or_reset(dev, devm_clkdev_release, cl);
  358. }
  359. EXPORT_SYMBOL(devm_clk_hw_register_clkdev);