clkdev.c 7.4 KB

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