clkdev.c 7.5 KB

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