clkdev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * drivers/clk/clkdev.c
  3. *
  4. * Copyright (C) 2008 Russell King.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Helper for the clk API to assist looking up a struct clk.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/device.h>
  15. #include <linux/list.h>
  16. #include <linux/errno.h>
  17. #include <linux/err.h>
  18. #include <linux/string.h>
  19. #include <linux/mutex.h>
  20. #include <linux/clk.h>
  21. #include <linux/clkdev.h>
  22. #include <linux/clk-provider.h>
  23. #include <linux/of.h>
  24. #include "clk.h"
  25. static LIST_HEAD(clocks);
  26. static DEFINE_MUTEX(clocks_mutex);
  27. #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
  28. static struct clk *__of_clk_get(struct device_node *np, int index,
  29. const char *dev_id, const char *con_id)
  30. {
  31. struct of_phandle_args clkspec;
  32. struct clk *clk;
  33. int rc;
  34. rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
  35. &clkspec);
  36. if (rc)
  37. return ERR_PTR(rc);
  38. clk = __of_clk_get_from_provider(&clkspec, dev_id, con_id);
  39. of_node_put(clkspec.np);
  40. return clk;
  41. }
  42. struct clk *of_clk_get(struct device_node *np, int index)
  43. {
  44. return __of_clk_get(np, index, np->full_name, NULL);
  45. }
  46. EXPORT_SYMBOL(of_clk_get);
  47. static struct clk *__of_clk_get_by_name(struct device_node *np,
  48. const char *dev_id,
  49. const char *name)
  50. {
  51. struct clk *clk = ERR_PTR(-ENOENT);
  52. /* Walk up the tree of devices looking for a clock that matches */
  53. while (np) {
  54. int index = 0;
  55. /*
  56. * For named clocks, first look up the name in the
  57. * "clock-names" property. If it cannot be found, then
  58. * index will be an error code, and of_clk_get() will fail.
  59. */
  60. if (name)
  61. index = of_property_match_string(np, "clock-names", name);
  62. clk = __of_clk_get(np, index, dev_id, name);
  63. if (!IS_ERR(clk)) {
  64. break;
  65. } else if (name && index >= 0) {
  66. if (PTR_ERR(clk) != -EPROBE_DEFER)
  67. pr_err("ERROR: could not get clock %pOF:%s(%i)\n",
  68. np, name ? name : "", index);
  69. return clk;
  70. }
  71. /*
  72. * No matching clock found on this node. If the parent node
  73. * has a "clock-ranges" property, then we can try one of its
  74. * clocks.
  75. */
  76. np = np->parent;
  77. if (np && !of_get_property(np, "clock-ranges", NULL))
  78. break;
  79. }
  80. return clk;
  81. }
  82. /**
  83. * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
  84. * @np: pointer to clock consumer node
  85. * @name: name of consumer's clock input, or NULL for the first clock reference
  86. *
  87. * This function parses the clocks and clock-names properties,
  88. * and uses them to look up the struct clk from the registered list of clock
  89. * providers.
  90. */
  91. struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
  92. {
  93. if (!np)
  94. return ERR_PTR(-ENOENT);
  95. return __of_clk_get_by_name(np, np->full_name, name);
  96. }
  97. EXPORT_SYMBOL(of_clk_get_by_name);
  98. #else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
  99. static struct clk *__of_clk_get_by_name(struct device_node *np,
  100. const char *dev_id,
  101. const char *name)
  102. {
  103. return ERR_PTR(-ENOENT);
  104. }
  105. #endif
  106. /*
  107. * Find the correct struct clk for the device and connection ID.
  108. * We do slightly fuzzy matching here:
  109. * An entry with a NULL ID is assumed to be a wildcard.
  110. * If an entry has a device ID, it must match
  111. * If an entry has a connection ID, it must match
  112. * Then we take the most specific entry - with the following
  113. * order of precedence: dev+con > dev only > con only.
  114. */
  115. static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
  116. {
  117. struct clk_lookup *p, *cl = NULL;
  118. int match, best_found = 0, best_possible = 0;
  119. if (dev_id)
  120. best_possible += 2;
  121. if (con_id)
  122. best_possible += 1;
  123. list_for_each_entry(p, &clocks, node) {
  124. match = 0;
  125. if (p->dev_id) {
  126. if (!dev_id || strcmp(p->dev_id, dev_id))
  127. continue;
  128. match += 2;
  129. }
  130. if (p->con_id) {
  131. if (!con_id || strcmp(p->con_id, con_id))
  132. continue;
  133. match += 1;
  134. }
  135. if (match > best_found) {
  136. cl = p;
  137. if (match != best_possible)
  138. best_found = match;
  139. else
  140. break;
  141. }
  142. }
  143. return cl;
  144. }
  145. struct clk *clk_get_sys(const char *dev_id, const char *con_id)
  146. {
  147. struct clk_lookup *cl;
  148. struct clk *clk = NULL;
  149. mutex_lock(&clocks_mutex);
  150. cl = clk_find(dev_id, con_id);
  151. if (!cl)
  152. goto out;
  153. clk = __clk_create_clk(cl->clk_hw, dev_id, con_id);
  154. if (IS_ERR(clk))
  155. goto out;
  156. if (!__clk_get(clk)) {
  157. __clk_free_clk(clk);
  158. cl = NULL;
  159. goto out;
  160. }
  161. out:
  162. mutex_unlock(&clocks_mutex);
  163. return cl ? clk : ERR_PTR(-ENOENT);
  164. }
  165. EXPORT_SYMBOL(clk_get_sys);
  166. struct clk *clk_get(struct device *dev, const char *con_id)
  167. {
  168. const char *dev_id = dev ? dev_name(dev) : NULL;
  169. struct clk *clk;
  170. if (dev && dev->of_node) {
  171. clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
  172. if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
  173. return clk;
  174. }
  175. return clk_get_sys(dev_id, con_id);
  176. }
  177. EXPORT_SYMBOL(clk_get);
  178. void clk_put(struct clk *clk)
  179. {
  180. __clk_put(clk);
  181. }
  182. EXPORT_SYMBOL(clk_put);
  183. static void __clkdev_add(struct clk_lookup *cl)
  184. {
  185. mutex_lock(&clocks_mutex);
  186. list_add_tail(&cl->node, &clocks);
  187. mutex_unlock(&clocks_mutex);
  188. }
  189. void clkdev_add(struct clk_lookup *cl)
  190. {
  191. if (!cl->clk_hw)
  192. cl->clk_hw = __clk_get_hw(cl->clk);
  193. __clkdev_add(cl);
  194. }
  195. EXPORT_SYMBOL(clkdev_add);
  196. void clkdev_add_table(struct clk_lookup *cl, size_t num)
  197. {
  198. mutex_lock(&clocks_mutex);
  199. while (num--) {
  200. cl->clk_hw = __clk_get_hw(cl->clk);
  201. list_add_tail(&cl->node, &clocks);
  202. cl++;
  203. }
  204. mutex_unlock(&clocks_mutex);
  205. }
  206. #define MAX_DEV_ID 20
  207. #define MAX_CON_ID 16
  208. struct clk_lookup_alloc {
  209. struct clk_lookup cl;
  210. char dev_id[MAX_DEV_ID];
  211. char con_id[MAX_CON_ID];
  212. };
  213. static struct clk_lookup * __ref
  214. vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
  215. va_list ap)
  216. {
  217. struct clk_lookup_alloc *cla;
  218. cla = kzalloc(sizeof(*cla), GFP_KERNEL);
  219. if (!cla)
  220. return NULL;
  221. cla->cl.clk_hw = hw;
  222. if (con_id) {
  223. strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
  224. cla->cl.con_id = cla->con_id;
  225. }
  226. if (dev_fmt) {
  227. vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  228. cla->cl.dev_id = cla->dev_id;
  229. }
  230. return &cla->cl;
  231. }
  232. static struct clk_lookup *
  233. vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
  234. va_list ap)
  235. {
  236. struct clk_lookup *cl;
  237. cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
  238. if (cl)
  239. __clkdev_add(cl);
  240. return cl;
  241. }
  242. struct clk_lookup * __ref
  243. clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
  244. {
  245. struct clk_lookup *cl;
  246. va_list ap;
  247. va_start(ap, dev_fmt);
  248. cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap);
  249. va_end(ap);
  250. return cl;
  251. }
  252. EXPORT_SYMBOL(clkdev_alloc);
  253. struct clk_lookup *
  254. clkdev_hw_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, ...)
  255. {
  256. struct clk_lookup *cl;
  257. va_list ap;
  258. va_start(ap, dev_fmt);
  259. cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
  260. va_end(ap);
  261. return cl;
  262. }
  263. EXPORT_SYMBOL(clkdev_hw_alloc);
  264. /**
  265. * clkdev_create - allocate and add a clkdev lookup structure
  266. * @clk: struct clk to associate with all clk_lookups
  267. * @con_id: connection ID string on device
  268. * @dev_fmt: format string describing device name
  269. *
  270. * Returns a clk_lookup structure, which can be later unregistered and
  271. * freed.
  272. */
  273. struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
  274. const char *dev_fmt, ...)
  275. {
  276. struct clk_lookup *cl;
  277. va_list ap;
  278. va_start(ap, dev_fmt);
  279. cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
  280. va_end(ap);
  281. return cl;
  282. }
  283. EXPORT_SYMBOL_GPL(clkdev_create);
  284. /**
  285. * clkdev_hw_create - allocate and add a clkdev lookup structure
  286. * @hw: struct clk_hw to associate with all clk_lookups
  287. * @con_id: connection ID string on device
  288. * @dev_fmt: format string describing device name
  289. *
  290. * Returns a clk_lookup structure, which can be later unregistered and
  291. * freed.
  292. */
  293. struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
  294. const char *dev_fmt, ...)
  295. {
  296. struct clk_lookup *cl;
  297. va_list ap;
  298. va_start(ap, dev_fmt);
  299. cl = vclkdev_create(hw, con_id, dev_fmt, ap);
  300. va_end(ap);
  301. return cl;
  302. }
  303. EXPORT_SYMBOL_GPL(clkdev_hw_create);
  304. int clk_add_alias(const char *alias, const char *alias_dev_name,
  305. const char *con_id, struct device *dev)
  306. {
  307. struct clk *r = clk_get(dev, con_id);
  308. struct clk_lookup *l;
  309. if (IS_ERR(r))
  310. return PTR_ERR(r);
  311. l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
  312. alias_dev_name);
  313. clk_put(r);
  314. return l ? 0 : -ENODEV;
  315. }
  316. EXPORT_SYMBOL(clk_add_alias);
  317. /*
  318. * clkdev_drop - remove a clock dynamically allocated
  319. */
  320. void clkdev_drop(struct clk_lookup *cl)
  321. {
  322. mutex_lock(&clocks_mutex);
  323. list_del(&cl->node);
  324. mutex_unlock(&clocks_mutex);
  325. kfree(cl);
  326. }
  327. EXPORT_SYMBOL(clkdev_drop);
  328. static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw,
  329. const char *con_id,
  330. const char *dev_id, ...)
  331. {
  332. struct clk_lookup *cl;
  333. va_list ap;
  334. va_start(ap, dev_id);
  335. cl = vclkdev_create(hw, con_id, dev_id, ap);
  336. va_end(ap);
  337. return cl;
  338. }
  339. /**
  340. * clk_register_clkdev - register one clock lookup for a struct clk
  341. * @clk: struct clk to associate with all clk_lookups
  342. * @con_id: connection ID string on device
  343. * @dev_id: string describing device name
  344. *
  345. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  346. * clkdev.
  347. *
  348. * To make things easier for mass registration, we detect error clks
  349. * from a previous clk_register() call, and return the error code for
  350. * those. This is to permit this function to be called immediately
  351. * after clk_register().
  352. */
  353. int clk_register_clkdev(struct clk *clk, const char *con_id,
  354. const char *dev_id)
  355. {
  356. struct clk_lookup *cl;
  357. if (IS_ERR(clk))
  358. return PTR_ERR(clk);
  359. /*
  360. * Since dev_id can be NULL, and NULL is handled specially, we must
  361. * pass it as either a NULL format string, or with "%s".
  362. */
  363. if (dev_id)
  364. cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, "%s",
  365. dev_id);
  366. else
  367. cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, NULL);
  368. return cl ? 0 : -ENOMEM;
  369. }
  370. EXPORT_SYMBOL(clk_register_clkdev);
  371. /**
  372. * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
  373. * @hw: struct clk_hw to associate with all clk_lookups
  374. * @con_id: connection ID string on device
  375. * @dev_id: format string describing device name
  376. *
  377. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  378. * clkdev.
  379. *
  380. * To make things easier for mass registration, we detect error clk_hws
  381. * from a previous clk_hw_register_*() call, and return the error code for
  382. * those. This is to permit this function to be called immediately
  383. * after clk_hw_register_*().
  384. */
  385. int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
  386. const char *dev_id)
  387. {
  388. struct clk_lookup *cl;
  389. if (IS_ERR(hw))
  390. return PTR_ERR(hw);
  391. /*
  392. * Since dev_id can be NULL, and NULL is handled specially, we must
  393. * pass it as either a NULL format string, or with "%s".
  394. */
  395. if (dev_id)
  396. cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
  397. else
  398. cl = __clk_register_clkdev(hw, con_id, NULL);
  399. return cl ? 0 : -ENOMEM;
  400. }
  401. EXPORT_SYMBOL(clk_hw_register_clkdev);