clkdev.c 8.2 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/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. static void devm_clk_release(struct device *dev, void *res)
  153. {
  154. clk_put(*(struct clk **)res);
  155. }
  156. struct clk *devm_clk_get(struct device *dev, const char *id)
  157. {
  158. struct clk **ptr, *clk;
  159. ptr = devres_alloc(devm_clk_release, sizeof(*ptr), GFP_KERNEL);
  160. if (!ptr)
  161. return ERR_PTR(-ENOMEM);
  162. clk = clk_get(dev, id);
  163. if (!IS_ERR(clk)) {
  164. *ptr = clk;
  165. devres_add(dev, ptr);
  166. } else {
  167. devres_free(ptr);
  168. }
  169. return clk;
  170. }
  171. EXPORT_SYMBOL(devm_clk_get);
  172. static int devm_clk_match(struct device *dev, void *res, void *data)
  173. {
  174. struct clk **c = res;
  175. if (!c || !*c) {
  176. WARN_ON(!c || !*c);
  177. return 0;
  178. }
  179. return *c == data;
  180. }
  181. void devm_clk_put(struct device *dev, struct clk *clk)
  182. {
  183. int ret;
  184. ret = devres_destroy(dev, devm_clk_release, devm_clk_match, clk);
  185. WARN_ON(ret);
  186. }
  187. EXPORT_SYMBOL(devm_clk_put);
  188. void clkdev_add(struct clk_lookup *cl)
  189. {
  190. mutex_lock(&clocks_mutex);
  191. list_add_tail(&cl->node, &clocks);
  192. mutex_unlock(&clocks_mutex);
  193. }
  194. EXPORT_SYMBOL(clkdev_add);
  195. void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
  196. {
  197. mutex_lock(&clocks_mutex);
  198. while (num--) {
  199. list_add_tail(&cl->node, &clocks);
  200. cl++;
  201. }
  202. mutex_unlock(&clocks_mutex);
  203. }
  204. #define MAX_DEV_ID 20
  205. #define MAX_CON_ID 16
  206. struct clk_lookup_alloc {
  207. struct clk_lookup cl;
  208. char dev_id[MAX_DEV_ID];
  209. char con_id[MAX_CON_ID];
  210. };
  211. static struct clk_lookup * __init_refok
  212. vclkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt,
  213. va_list ap)
  214. {
  215. struct clk_lookup_alloc *cla;
  216. cla = __clkdev_alloc(sizeof(*cla));
  217. if (!cla)
  218. return NULL;
  219. cla->cl.clk = clk;
  220. if (con_id) {
  221. strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
  222. cla->cl.con_id = cla->con_id;
  223. }
  224. if (dev_fmt) {
  225. vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  226. cla->cl.dev_id = cla->dev_id;
  227. }
  228. return &cla->cl;
  229. }
  230. struct clk_lookup * __init_refok
  231. clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
  232. {
  233. struct clk_lookup *cl;
  234. va_list ap;
  235. va_start(ap, dev_fmt);
  236. cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
  237. va_end(ap);
  238. return cl;
  239. }
  240. EXPORT_SYMBOL(clkdev_alloc);
  241. int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
  242. struct device *dev)
  243. {
  244. struct clk *r = clk_get(dev, id);
  245. struct clk_lookup *l;
  246. if (IS_ERR(r))
  247. return PTR_ERR(r);
  248. l = clkdev_alloc(r, alias, alias_dev_name);
  249. clk_put(r);
  250. if (!l)
  251. return -ENODEV;
  252. clkdev_add(l);
  253. return 0;
  254. }
  255. EXPORT_SYMBOL(clk_add_alias);
  256. /*
  257. * clkdev_drop - remove a clock dynamically allocated
  258. */
  259. void clkdev_drop(struct clk_lookup *cl)
  260. {
  261. mutex_lock(&clocks_mutex);
  262. list_del(&cl->node);
  263. mutex_unlock(&clocks_mutex);
  264. kfree(cl);
  265. }
  266. EXPORT_SYMBOL(clkdev_drop);
  267. /**
  268. * clk_register_clkdev - register one clock lookup for a struct clk
  269. * @clk: struct clk to associate with all clk_lookups
  270. * @con_id: connection ID string on device
  271. * @dev_id: format string describing device name
  272. *
  273. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  274. * clkdev.
  275. *
  276. * To make things easier for mass registration, we detect error clks
  277. * from a previous clk_register() call, and return the error code for
  278. * those. This is to permit this function to be called immediately
  279. * after clk_register().
  280. */
  281. int clk_register_clkdev(struct clk *clk, const char *con_id,
  282. const char *dev_fmt, ...)
  283. {
  284. struct clk_lookup *cl;
  285. va_list ap;
  286. if (IS_ERR(clk))
  287. return PTR_ERR(clk);
  288. va_start(ap, dev_fmt);
  289. cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
  290. va_end(ap);
  291. if (!cl)
  292. return -ENOMEM;
  293. clkdev_add(cl);
  294. return 0;
  295. }
  296. /**
  297. * clk_register_clkdevs - register a set of clk_lookup for a struct clk
  298. * @clk: struct clk to associate with all clk_lookups
  299. * @cl: array of clk_lookup structures with con_id and dev_id pre-initialized
  300. * @num: number of clk_lookup structures to register
  301. *
  302. * To make things easier for mass registration, we detect error clks
  303. * from a previous clk_register() call, and return the error code for
  304. * those. This is to permit this function to be called immediately
  305. * after clk_register().
  306. */
  307. int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num)
  308. {
  309. unsigned i;
  310. if (IS_ERR(clk))
  311. return PTR_ERR(clk);
  312. for (i = 0; i < num; i++, cl++) {
  313. cl->clk = clk;
  314. clkdev_add(cl);
  315. }
  316. return 0;
  317. }
  318. EXPORT_SYMBOL(clk_register_clkdevs);