clkdev.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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(cl->clk_hw, 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. static 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. void clkdev_add(struct clk_lookup *cl)
  192. {
  193. if (!cl->clk_hw)
  194. cl->clk_hw = __clk_get_hw(cl->clk);
  195. __clkdev_add(cl);
  196. }
  197. EXPORT_SYMBOL(clkdev_add);
  198. void clkdev_add_table(struct clk_lookup *cl, size_t num)
  199. {
  200. mutex_lock(&clocks_mutex);
  201. while (num--) {
  202. cl->clk_hw = __clk_get_hw(cl->clk);
  203. list_add_tail(&cl->node, &clocks);
  204. cl++;
  205. }
  206. mutex_unlock(&clocks_mutex);
  207. }
  208. #define MAX_DEV_ID 20
  209. #define MAX_CON_ID 16
  210. struct clk_lookup_alloc {
  211. struct clk_lookup cl;
  212. char dev_id[MAX_DEV_ID];
  213. char con_id[MAX_CON_ID];
  214. };
  215. static struct clk_lookup * __init_refok
  216. vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
  217. va_list ap)
  218. {
  219. struct clk_lookup_alloc *cla;
  220. cla = __clkdev_alloc(sizeof(*cla));
  221. if (!cla)
  222. return NULL;
  223. cla->cl.clk_hw = hw;
  224. if (con_id) {
  225. strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
  226. cla->cl.con_id = cla->con_id;
  227. }
  228. if (dev_fmt) {
  229. vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  230. cla->cl.dev_id = cla->dev_id;
  231. }
  232. return &cla->cl;
  233. }
  234. static struct clk_lookup *
  235. vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
  236. va_list ap)
  237. {
  238. struct clk_lookup *cl;
  239. cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
  240. if (cl)
  241. __clkdev_add(cl);
  242. return cl;
  243. }
  244. struct clk_lookup * __init_refok
  245. clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
  246. {
  247. struct clk_lookup *cl;
  248. va_list ap;
  249. va_start(ap, dev_fmt);
  250. cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap);
  251. va_end(ap);
  252. return cl;
  253. }
  254. EXPORT_SYMBOL(clkdev_alloc);
  255. /**
  256. * clkdev_create - allocate and add a clkdev lookup structure
  257. * @clk: struct clk to associate with all clk_lookups
  258. * @con_id: connection ID string on device
  259. * @dev_fmt: format string describing device name
  260. *
  261. * Returns a clk_lookup structure, which can be later unregistered and
  262. * freed.
  263. */
  264. struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
  265. const char *dev_fmt, ...)
  266. {
  267. struct clk_lookup *cl;
  268. va_list ap;
  269. va_start(ap, dev_fmt);
  270. cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
  271. va_end(ap);
  272. return cl;
  273. }
  274. EXPORT_SYMBOL_GPL(clkdev_create);
  275. int clk_add_alias(const char *alias, const char *alias_dev_name,
  276. const char *con_id, struct device *dev)
  277. {
  278. struct clk *r = clk_get(dev, con_id);
  279. struct clk_lookup *l;
  280. if (IS_ERR(r))
  281. return PTR_ERR(r);
  282. l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
  283. alias_dev_name);
  284. clk_put(r);
  285. return l ? 0 : -ENODEV;
  286. }
  287. EXPORT_SYMBOL(clk_add_alias);
  288. /*
  289. * clkdev_drop - remove a clock dynamically allocated
  290. */
  291. void clkdev_drop(struct clk_lookup *cl)
  292. {
  293. mutex_lock(&clocks_mutex);
  294. list_del(&cl->node);
  295. mutex_unlock(&clocks_mutex);
  296. kfree(cl);
  297. }
  298. EXPORT_SYMBOL(clkdev_drop);
  299. /**
  300. * clk_register_clkdev - register one clock lookup for a struct clk
  301. * @clk: struct clk to associate with all clk_lookups
  302. * @con_id: connection ID string on device
  303. * @dev_id: format string describing device name
  304. *
  305. * con_id or dev_id may be NULL as a wildcard, just as in the rest of
  306. * clkdev.
  307. *
  308. * To make things easier for mass registration, we detect error clks
  309. * from a previous clk_register() call, and return the error code for
  310. * those. This is to permit this function to be called immediately
  311. * after clk_register().
  312. */
  313. int clk_register_clkdev(struct clk *clk, const char *con_id,
  314. const char *dev_fmt, ...)
  315. {
  316. struct clk_lookup *cl;
  317. va_list ap;
  318. if (IS_ERR(clk))
  319. return PTR_ERR(clk);
  320. va_start(ap, dev_fmt);
  321. cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
  322. va_end(ap);
  323. return cl ? 0 : -ENOMEM;
  324. }
  325. EXPORT_SYMBOL(clk_register_clkdev);
  326. /**
  327. * clk_register_clkdevs - register a set of clk_lookup for a struct clk
  328. * @clk: struct clk to associate with all clk_lookups
  329. * @cl: array of clk_lookup structures with con_id and dev_id pre-initialized
  330. * @num: number of clk_lookup structures to register
  331. *
  332. * To make things easier for mass registration, we detect error clks
  333. * from a previous clk_register() call, and return the error code for
  334. * those. This is to permit this function to be called immediately
  335. * after clk_register().
  336. */
  337. int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num)
  338. {
  339. unsigned i;
  340. if (IS_ERR(clk))
  341. return PTR_ERR(clk);
  342. for (i = 0; i < num; i++, cl++) {
  343. cl->clk_hw = __clk_get_hw(clk);
  344. __clkdev_add(cl);
  345. }
  346. return 0;
  347. }
  348. EXPORT_SYMBOL(clk_register_clkdevs);