clkdev.c 8.3 KB

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