clkdev.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. pr_err("ERROR: could not get clock %s:%s(%i)\n",
  90. np->full_name, name ? name : "", index);
  91. return clk;
  92. }
  93. /*
  94. * No matching clock found on this node. If the parent node
  95. * has a "clock-ranges" property, then we can try one of its
  96. * clocks.
  97. */
  98. np = np->parent;
  99. if (np && !of_get_property(np, "clock-ranges", NULL))
  100. break;
  101. }
  102. return clk;
  103. }
  104. EXPORT_SYMBOL(of_clk_get_by_name);
  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. mutex_lock(&clocks_mutex);
  149. cl = clk_find(dev_id, con_id);
  150. if (cl && !__clk_get(cl->clk))
  151. cl = NULL;
  152. mutex_unlock(&clocks_mutex);
  153. return cl ? cl->clk : ERR_PTR(-ENOENT);
  154. }
  155. EXPORT_SYMBOL(clk_get_sys);
  156. struct clk *clk_get(struct device *dev, const char *con_id)
  157. {
  158. const char *dev_id = dev ? dev_name(dev) : NULL;
  159. struct clk *clk;
  160. if (dev) {
  161. clk = of_clk_get_by_name(dev->of_node, con_id);
  162. if (!IS_ERR(clk))
  163. return clk;
  164. if (PTR_ERR(clk) == -EPROBE_DEFER)
  165. return clk;
  166. }
  167. return clk_get_sys(dev_id, con_id);
  168. }
  169. EXPORT_SYMBOL(clk_get);
  170. void clk_put(struct clk *clk)
  171. {
  172. __clk_put(clk);
  173. }
  174. EXPORT_SYMBOL(clk_put);
  175. void clkdev_add(struct clk_lookup *cl)
  176. {
  177. mutex_lock(&clocks_mutex);
  178. list_add_tail(&cl->node, &clocks);
  179. mutex_unlock(&clocks_mutex);
  180. }
  181. EXPORT_SYMBOL(clkdev_add);
  182. void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
  183. {
  184. mutex_lock(&clocks_mutex);
  185. while (num--) {
  186. list_add_tail(&cl->node, &clocks);
  187. cl++;
  188. }
  189. mutex_unlock(&clocks_mutex);
  190. }
  191. #define MAX_DEV_ID 20
  192. #define MAX_CON_ID 16
  193. struct clk_lookup_alloc {
  194. struct clk_lookup cl;
  195. char dev_id[MAX_DEV_ID];
  196. char con_id[MAX_CON_ID];
  197. };
  198. static struct clk_lookup * __init_refok
  199. vclkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt,
  200. va_list ap)
  201. {
  202. struct clk_lookup_alloc *cla;
  203. cla = __clkdev_alloc(sizeof(*cla));
  204. if (!cla)
  205. return NULL;
  206. cla->cl.clk = clk;
  207. if (con_id) {
  208. strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
  209. cla->cl.con_id = cla->con_id;
  210. }
  211. if (dev_fmt) {
  212. vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  213. cla->cl.dev_id = cla->dev_id;
  214. }
  215. return &cla->cl;
  216. }
  217. struct clk_lookup * __init_refok
  218. clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
  219. {
  220. struct clk_lookup *cl;
  221. va_list ap;
  222. va_start(ap, dev_fmt);
  223. cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
  224. va_end(ap);
  225. return cl;
  226. }
  227. EXPORT_SYMBOL(clkdev_alloc);
  228. int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
  229. struct device *dev)
  230. {
  231. struct clk *r = clk_get(dev, id);
  232. struct clk_lookup *l;
  233. if (IS_ERR(r))
  234. return PTR_ERR(r);
  235. l = clkdev_alloc(r, alias, alias_dev_name);
  236. clk_put(r);
  237. if (!l)
  238. return -ENODEV;
  239. clkdev_add(l);
  240. return 0;
  241. }
  242. EXPORT_SYMBOL(clk_add_alias);
  243. /*
  244. * clkdev_drop - remove a clock dynamically allocated
  245. */
  246. void clkdev_drop(struct clk_lookup *cl)
  247. {
  248. mutex_lock(&clocks_mutex);
  249. list_del(&cl->node);
  250. mutex_unlock(&clocks_mutex);
  251. kfree(cl);
  252. }
  253. EXPORT_SYMBOL(clkdev_drop);
  254. /**
  255. * clk_register_clkdev - register one clock lookup for a struct clk
  256. * @clk: struct clk to associate with all clk_lookups
  257. * @con_id: connection ID string on device
  258. * @dev_id: format string describing device name
  259. *
  260. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  261. * clkdev.
  262. *
  263. * To make things easier for mass registration, we detect error clks
  264. * from a previous clk_register() call, and return the error code for
  265. * those. This is to permit this function to be called immediately
  266. * after clk_register().
  267. */
  268. int clk_register_clkdev(struct clk *clk, const char *con_id,
  269. const char *dev_fmt, ...)
  270. {
  271. struct clk_lookup *cl;
  272. va_list ap;
  273. if (IS_ERR(clk))
  274. return PTR_ERR(clk);
  275. va_start(ap, dev_fmt);
  276. cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
  277. va_end(ap);
  278. if (!cl)
  279. return -ENOMEM;
  280. clkdev_add(cl);
  281. return 0;
  282. }
  283. /**
  284. * clk_register_clkdevs - register a set of clk_lookup for a struct clk
  285. * @clk: struct clk to associate with all clk_lookups
  286. * @cl: array of clk_lookup structures with con_id and dev_id pre-initialized
  287. * @num: number of clk_lookup structures to register
  288. *
  289. * To make things easier for mass registration, we detect error clks
  290. * from a previous clk_register() call, and return the error code for
  291. * those. This is to permit this function to be called immediately
  292. * after clk_register().
  293. */
  294. int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num)
  295. {
  296. unsigned i;
  297. if (IS_ERR(clk))
  298. return PTR_ERR(clk);
  299. for (i = 0; i < num; i++, cl++) {
  300. cl->clk = clk;
  301. clkdev_add(cl);
  302. }
  303. return 0;
  304. }
  305. EXPORT_SYMBOL(clk_register_clkdevs);