clkdev.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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_by_clkspec(struct of_phandle_args *clkspec,
  29. const char *dev_id, const char *con_id)
  30. {
  31. struct clk *clk;
  32. if (!clkspec)
  33. return ERR_PTR(-EINVAL);
  34. of_clk_lock();
  35. clk = __of_clk_get_from_provider(clkspec, dev_id, con_id);
  36. of_clk_unlock();
  37. return clk;
  38. }
  39. /**
  40. * of_clk_get_by_clkspec() - Lookup a clock form a clock provider
  41. * @clkspec: pointer to a clock specifier data structure
  42. *
  43. * This function looks up a struct clk from the registered list of clock
  44. * providers, an input is a clock specifier data structure as returned
  45. * from the of_parse_phandle_with_args() function call.
  46. */
  47. struct clk *of_clk_get_by_clkspec(struct of_phandle_args *clkspec)
  48. {
  49. return __of_clk_get_by_clkspec(clkspec, NULL, __func__);
  50. }
  51. static struct clk *__of_clk_get(struct device_node *np, int index,
  52. const char *dev_id, const char *con_id)
  53. {
  54. struct of_phandle_args clkspec;
  55. struct clk *clk;
  56. int rc;
  57. if (index < 0)
  58. return ERR_PTR(-EINVAL);
  59. rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
  60. &clkspec);
  61. if (rc)
  62. return ERR_PTR(rc);
  63. clk = __of_clk_get_by_clkspec(&clkspec, dev_id, con_id);
  64. of_node_put(clkspec.np);
  65. return clk;
  66. }
  67. struct clk *of_clk_get(struct device_node *np, int index)
  68. {
  69. return __of_clk_get(np, index, np->full_name, NULL);
  70. }
  71. EXPORT_SYMBOL(of_clk_get);
  72. static struct clk *__of_clk_get_by_name(struct device_node *np,
  73. const char *dev_id,
  74. const char *name)
  75. {
  76. struct clk *clk = ERR_PTR(-ENOENT);
  77. /* Walk up the tree of devices looking for a clock that matches */
  78. while (np) {
  79. int index = 0;
  80. /*
  81. * For named clocks, first look up the name in the
  82. * "clock-names" property. If it cannot be found, then
  83. * index will be an error code, and of_clk_get() will fail.
  84. */
  85. if (name)
  86. index = of_property_match_string(np, "clock-names", name);
  87. clk = __of_clk_get(np, index, dev_id, name);
  88. if (!IS_ERR(clk)) {
  89. break;
  90. } else if (name && index >= 0) {
  91. if (PTR_ERR(clk) != -EPROBE_DEFER)
  92. pr_err("ERROR: could not get clock %s:%s(%i)\n",
  93. np->full_name, name ? name : "", index);
  94. return clk;
  95. }
  96. /*
  97. * No matching clock found on this node. If the parent node
  98. * has a "clock-ranges" property, then we can try one of its
  99. * clocks.
  100. */
  101. np = np->parent;
  102. if (np && !of_get_property(np, "clock-ranges", NULL))
  103. break;
  104. }
  105. return clk;
  106. }
  107. /**
  108. * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
  109. * @np: pointer to clock consumer node
  110. * @name: name of consumer's clock input, or NULL for the first clock reference
  111. *
  112. * This function parses the clocks and clock-names properties,
  113. * and uses them to look up the struct clk from the registered list of clock
  114. * providers.
  115. */
  116. struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
  117. {
  118. if (!np)
  119. return ERR_PTR(-ENOENT);
  120. return __of_clk_get_by_name(np, np->full_name, name);
  121. }
  122. EXPORT_SYMBOL(of_clk_get_by_name);
  123. #else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
  124. static struct clk *__of_clk_get_by_name(struct device_node *np,
  125. const char *dev_id,
  126. const char *name)
  127. {
  128. return ERR_PTR(-ENOENT);
  129. }
  130. #endif
  131. /*
  132. * Find the correct struct clk for the device and connection ID.
  133. * We do slightly fuzzy matching here:
  134. * An entry with a NULL ID is assumed to be a wildcard.
  135. * If an entry has a device ID, it must match
  136. * If an entry has a connection ID, it must match
  137. * Then we take the most specific entry - with the following
  138. * order of precedence: dev+con > dev only > con only.
  139. */
  140. static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
  141. {
  142. struct clk_lookup *p, *cl = NULL;
  143. int match, best_found = 0, best_possible = 0;
  144. if (dev_id)
  145. best_possible += 2;
  146. if (con_id)
  147. best_possible += 1;
  148. list_for_each_entry(p, &clocks, node) {
  149. match = 0;
  150. if (p->dev_id) {
  151. if (!dev_id || strcmp(p->dev_id, dev_id))
  152. continue;
  153. match += 2;
  154. }
  155. if (p->con_id) {
  156. if (!con_id || strcmp(p->con_id, con_id))
  157. continue;
  158. match += 1;
  159. }
  160. if (match > best_found) {
  161. cl = p;
  162. if (match != best_possible)
  163. best_found = match;
  164. else
  165. break;
  166. }
  167. }
  168. return cl;
  169. }
  170. struct clk *clk_get_sys(const char *dev_id, const char *con_id)
  171. {
  172. struct clk_lookup *cl;
  173. struct clk *clk = NULL;
  174. mutex_lock(&clocks_mutex);
  175. cl = clk_find(dev_id, con_id);
  176. if (!cl)
  177. goto out;
  178. clk = __clk_create_clk(__clk_get_hw(cl->clk), dev_id, con_id);
  179. if (IS_ERR(clk))
  180. goto out;
  181. if (!__clk_get(clk)) {
  182. __clk_free_clk(clk);
  183. cl = NULL;
  184. goto out;
  185. }
  186. out:
  187. mutex_unlock(&clocks_mutex);
  188. return cl ? clk : ERR_PTR(-ENOENT);
  189. }
  190. EXPORT_SYMBOL(clk_get_sys);
  191. struct clk *clk_get(struct device *dev, const char *con_id)
  192. {
  193. const char *dev_id = dev ? dev_name(dev) : NULL;
  194. struct clk *clk;
  195. if (dev) {
  196. clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
  197. if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
  198. return clk;
  199. }
  200. return clk_get_sys(dev_id, con_id);
  201. }
  202. EXPORT_SYMBOL(clk_get);
  203. void clk_put(struct clk *clk)
  204. {
  205. __clk_put(clk);
  206. }
  207. EXPORT_SYMBOL(clk_put);
  208. void clkdev_add(struct clk_lookup *cl)
  209. {
  210. mutex_lock(&clocks_mutex);
  211. list_add_tail(&cl->node, &clocks);
  212. mutex_unlock(&clocks_mutex);
  213. }
  214. EXPORT_SYMBOL(clkdev_add);
  215. void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
  216. {
  217. mutex_lock(&clocks_mutex);
  218. while (num--) {
  219. list_add_tail(&cl->node, &clocks);
  220. cl++;
  221. }
  222. mutex_unlock(&clocks_mutex);
  223. }
  224. #define MAX_DEV_ID 20
  225. #define MAX_CON_ID 16
  226. struct clk_lookup_alloc {
  227. struct clk_lookup cl;
  228. char dev_id[MAX_DEV_ID];
  229. char con_id[MAX_CON_ID];
  230. };
  231. static struct clk_lookup * __init_refok
  232. vclkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt,
  233. va_list ap)
  234. {
  235. struct clk_lookup_alloc *cla;
  236. cla = __clkdev_alloc(sizeof(*cla));
  237. if (!cla)
  238. return NULL;
  239. cla->cl.clk = clk;
  240. if (con_id) {
  241. strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
  242. cla->cl.con_id = cla->con_id;
  243. }
  244. if (dev_fmt) {
  245. vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  246. cla->cl.dev_id = cla->dev_id;
  247. }
  248. return &cla->cl;
  249. }
  250. struct clk_lookup * __init_refok
  251. clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
  252. {
  253. struct clk_lookup *cl;
  254. va_list ap;
  255. va_start(ap, dev_fmt);
  256. cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
  257. va_end(ap);
  258. return cl;
  259. }
  260. EXPORT_SYMBOL(clkdev_alloc);
  261. int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
  262. struct device *dev)
  263. {
  264. struct clk *r = clk_get(dev, id);
  265. struct clk_lookup *l;
  266. if (IS_ERR(r))
  267. return PTR_ERR(r);
  268. l = clkdev_alloc(r, alias, alias_dev_name);
  269. clk_put(r);
  270. if (!l)
  271. return -ENODEV;
  272. clkdev_add(l);
  273. return 0;
  274. }
  275. EXPORT_SYMBOL(clk_add_alias);
  276. /*
  277. * clkdev_drop - remove a clock dynamically allocated
  278. */
  279. void clkdev_drop(struct clk_lookup *cl)
  280. {
  281. mutex_lock(&clocks_mutex);
  282. list_del(&cl->node);
  283. mutex_unlock(&clocks_mutex);
  284. kfree(cl);
  285. }
  286. EXPORT_SYMBOL(clkdev_drop);
  287. /**
  288. * clk_register_clkdev - register one clock lookup for a struct clk
  289. * @clk: struct clk to associate with all clk_lookups
  290. * @con_id: connection ID string on device
  291. * @dev_id: format string describing device name
  292. *
  293. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  294. * clkdev.
  295. *
  296. * To make things easier for mass registration, we detect error clks
  297. * from a previous clk_register() call, and return the error code for
  298. * those. This is to permit this function to be called immediately
  299. * after clk_register().
  300. */
  301. int clk_register_clkdev(struct clk *clk, const char *con_id,
  302. const char *dev_fmt, ...)
  303. {
  304. struct clk_lookup *cl;
  305. va_list ap;
  306. if (IS_ERR(clk))
  307. return PTR_ERR(clk);
  308. va_start(ap, dev_fmt);
  309. cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
  310. va_end(ap);
  311. if (!cl)
  312. return -ENOMEM;
  313. clkdev_add(cl);
  314. return 0;
  315. }
  316. EXPORT_SYMBOL(clk_register_clkdev);
  317. /**
  318. * clk_register_clkdevs - register a set of clk_lookup for a struct clk
  319. * @clk: struct clk to associate with all clk_lookups
  320. * @cl: array of clk_lookup structures with con_id and dev_id pre-initialized
  321. * @num: number of clk_lookup structures to register
  322. *
  323. * To make things easier for mass registration, we detect error clks
  324. * from a previous clk_register() call, and return the error code for
  325. * those. This is to permit this function to be called immediately
  326. * after clk_register().
  327. */
  328. int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num)
  329. {
  330. unsigned i;
  331. if (IS_ERR(clk))
  332. return PTR_ERR(clk);
  333. for (i = 0; i < num; i++, cl++) {
  334. cl->clk = clk;
  335. clkdev_add(cl);
  336. }
  337. return 0;
  338. }
  339. EXPORT_SYMBOL(clk_register_clkdevs);