clkdev.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. static LIST_HEAD(clocks);
  23. static DEFINE_MUTEX(clocks_mutex);
  24. /*
  25. * Find the correct struct clk for the device and connection ID.
  26. * We do slightly fuzzy matching here:
  27. * An entry with a NULL ID is assumed to be a wildcard.
  28. * If an entry has a device ID, it must match
  29. * If an entry has a connection ID, it must match
  30. * Then we take the most specific entry - with the following
  31. * order of precedence: dev+con > dev only > con only.
  32. */
  33. static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
  34. {
  35. struct clk_lookup *p, *cl = NULL;
  36. int match, best_found = 0, best_possible = 0;
  37. if (dev_id)
  38. best_possible += 2;
  39. if (con_id)
  40. best_possible += 1;
  41. list_for_each_entry(p, &clocks, node) {
  42. match = 0;
  43. if (p->dev_id) {
  44. if (!dev_id || strcmp(p->dev_id, dev_id))
  45. continue;
  46. match += 2;
  47. }
  48. if (p->con_id) {
  49. if (!con_id || strcmp(p->con_id, con_id))
  50. continue;
  51. match += 1;
  52. }
  53. if (match > best_found) {
  54. cl = p;
  55. if (match != best_possible)
  56. best_found = match;
  57. else
  58. break;
  59. }
  60. }
  61. return cl;
  62. }
  63. struct clk *clk_get_sys(const char *dev_id, const char *con_id)
  64. {
  65. struct clk_lookup *cl;
  66. mutex_lock(&clocks_mutex);
  67. cl = clk_find(dev_id, con_id);
  68. if (cl && !__clk_get(cl->clk))
  69. cl = NULL;
  70. mutex_unlock(&clocks_mutex);
  71. return cl ? cl->clk : ERR_PTR(-ENOENT);
  72. }
  73. EXPORT_SYMBOL(clk_get_sys);
  74. struct clk *clk_get(struct device *dev, const char *con_id)
  75. {
  76. const char *dev_id = dev ? dev_name(dev) : NULL;
  77. return clk_get_sys(dev_id, con_id);
  78. }
  79. EXPORT_SYMBOL(clk_get);
  80. void clk_put(struct clk *clk)
  81. {
  82. __clk_put(clk);
  83. }
  84. EXPORT_SYMBOL(clk_put);
  85. static void devm_clk_release(struct device *dev, void *res)
  86. {
  87. clk_put(*(struct clk **)res);
  88. }
  89. struct clk *devm_clk_get(struct device *dev, const char *id)
  90. {
  91. struct clk **ptr, *clk;
  92. ptr = devres_alloc(devm_clk_release, sizeof(*ptr), GFP_KERNEL);
  93. if (!ptr)
  94. return ERR_PTR(-ENOMEM);
  95. clk = clk_get(dev, id);
  96. if (!IS_ERR(clk)) {
  97. *ptr = clk;
  98. devres_add(dev, ptr);
  99. } else {
  100. devres_free(ptr);
  101. }
  102. return clk;
  103. }
  104. EXPORT_SYMBOL(devm_clk_get);
  105. static int devm_clk_match(struct device *dev, void *res, void *data)
  106. {
  107. struct clk **c = res;
  108. if (!c || !*c) {
  109. WARN_ON(!c || !*c);
  110. return 0;
  111. }
  112. return *c == data;
  113. }
  114. void devm_clk_put(struct device *dev, struct clk *clk)
  115. {
  116. int ret;
  117. ret = devres_destroy(dev, devm_clk_release, devm_clk_match, clk);
  118. WARN_ON(ret);
  119. }
  120. EXPORT_SYMBOL(devm_clk_put);
  121. void clkdev_add(struct clk_lookup *cl)
  122. {
  123. mutex_lock(&clocks_mutex);
  124. list_add_tail(&cl->node, &clocks);
  125. mutex_unlock(&clocks_mutex);
  126. }
  127. EXPORT_SYMBOL(clkdev_add);
  128. void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
  129. {
  130. mutex_lock(&clocks_mutex);
  131. while (num--) {
  132. list_add_tail(&cl->node, &clocks);
  133. cl++;
  134. }
  135. mutex_unlock(&clocks_mutex);
  136. }
  137. #define MAX_DEV_ID 20
  138. #define MAX_CON_ID 16
  139. struct clk_lookup_alloc {
  140. struct clk_lookup cl;
  141. char dev_id[MAX_DEV_ID];
  142. char con_id[MAX_CON_ID];
  143. };
  144. static struct clk_lookup * __init_refok
  145. vclkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt,
  146. va_list ap)
  147. {
  148. struct clk_lookup_alloc *cla;
  149. cla = __clkdev_alloc(sizeof(*cla));
  150. if (!cla)
  151. return NULL;
  152. cla->cl.clk = clk;
  153. if (con_id) {
  154. strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
  155. cla->cl.con_id = cla->con_id;
  156. }
  157. if (dev_fmt) {
  158. vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  159. cla->cl.dev_id = cla->dev_id;
  160. }
  161. return &cla->cl;
  162. }
  163. struct clk_lookup * __init_refok
  164. clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
  165. {
  166. struct clk_lookup *cl;
  167. va_list ap;
  168. va_start(ap, dev_fmt);
  169. cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
  170. va_end(ap);
  171. return cl;
  172. }
  173. EXPORT_SYMBOL(clkdev_alloc);
  174. int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
  175. struct device *dev)
  176. {
  177. struct clk *r = clk_get(dev, id);
  178. struct clk_lookup *l;
  179. if (IS_ERR(r))
  180. return PTR_ERR(r);
  181. l = clkdev_alloc(r, alias, alias_dev_name);
  182. clk_put(r);
  183. if (!l)
  184. return -ENODEV;
  185. clkdev_add(l);
  186. return 0;
  187. }
  188. EXPORT_SYMBOL(clk_add_alias);
  189. /*
  190. * clkdev_drop - remove a clock dynamically allocated
  191. */
  192. void clkdev_drop(struct clk_lookup *cl)
  193. {
  194. mutex_lock(&clocks_mutex);
  195. list_del(&cl->node);
  196. mutex_unlock(&clocks_mutex);
  197. kfree(cl);
  198. }
  199. EXPORT_SYMBOL(clkdev_drop);
  200. /**
  201. * clk_register_clkdev - register one clock lookup for a struct clk
  202. * @clk: struct clk to associate with all clk_lookups
  203. * @con_id: connection ID string on device
  204. * @dev_id: format string describing device name
  205. *
  206. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  207. * clkdev.
  208. *
  209. * To make things easier for mass registration, we detect error clks
  210. * from a previous clk_register() call, and return the error code for
  211. * those. This is to permit this function to be called immediately
  212. * after clk_register().
  213. */
  214. int clk_register_clkdev(struct clk *clk, const char *con_id,
  215. const char *dev_fmt, ...)
  216. {
  217. struct clk_lookup *cl;
  218. va_list ap;
  219. if (IS_ERR(clk))
  220. return PTR_ERR(clk);
  221. va_start(ap, dev_fmt);
  222. cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
  223. va_end(ap);
  224. if (!cl)
  225. return -ENOMEM;
  226. clkdev_add(cl);
  227. return 0;
  228. }
  229. /**
  230. * clk_register_clkdevs - register a set of clk_lookup for a struct clk
  231. * @clk: struct clk to associate with all clk_lookups
  232. * @cl: array of clk_lookup structures with con_id and dev_id pre-initialized
  233. * @num: number of clk_lookup structures to register
  234. *
  235. * To make things easier for mass registration, we detect error clks
  236. * from a previous clk_register() call, and return the error code for
  237. * those. This is to permit this function to be called immediately
  238. * after clk_register().
  239. */
  240. int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num)
  241. {
  242. unsigned i;
  243. if (IS_ERR(clk))
  244. return PTR_ERR(clk);
  245. for (i = 0; i < num; i++, cl++) {
  246. cl->clk = clk;
  247. clkdev_add(cl);
  248. }
  249. return 0;
  250. }
  251. EXPORT_SYMBOL(clk_register_clkdevs);