clkdev.c 8.1 KB

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