clkdev.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 = 0;
  37. list_for_each_entry(p, &clocks, node) {
  38. match = 0;
  39. if (p->dev_id) {
  40. if (!dev_id || strcmp(p->dev_id, dev_id))
  41. continue;
  42. match += 2;
  43. }
  44. if (p->con_id) {
  45. if (!con_id || strcmp(p->con_id, con_id))
  46. continue;
  47. match += 1;
  48. }
  49. if (match > best) {
  50. cl = p;
  51. if (match != 3)
  52. best = match;
  53. else
  54. break;
  55. }
  56. }
  57. return cl;
  58. }
  59. struct clk *clk_get_sys(const char *dev_id, const char *con_id)
  60. {
  61. struct clk_lookup *cl;
  62. mutex_lock(&clocks_mutex);
  63. cl = clk_find(dev_id, con_id);
  64. if (cl && !__clk_get(cl->clk))
  65. cl = NULL;
  66. mutex_unlock(&clocks_mutex);
  67. return cl ? cl->clk : ERR_PTR(-ENOENT);
  68. }
  69. EXPORT_SYMBOL(clk_get_sys);
  70. struct clk *clk_get(struct device *dev, const char *con_id)
  71. {
  72. const char *dev_id = dev ? dev_name(dev) : NULL;
  73. return clk_get_sys(dev_id, con_id);
  74. }
  75. EXPORT_SYMBOL(clk_get);
  76. void clk_put(struct clk *clk)
  77. {
  78. __clk_put(clk);
  79. }
  80. EXPORT_SYMBOL(clk_put);
  81. void clkdev_add(struct clk_lookup *cl)
  82. {
  83. mutex_lock(&clocks_mutex);
  84. list_add_tail(&cl->node, &clocks);
  85. mutex_unlock(&clocks_mutex);
  86. }
  87. EXPORT_SYMBOL(clkdev_add);
  88. void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
  89. {
  90. mutex_lock(&clocks_mutex);
  91. while (num--) {
  92. list_add_tail(&cl->node, &clocks);
  93. cl++;
  94. }
  95. mutex_unlock(&clocks_mutex);
  96. }
  97. #define MAX_DEV_ID 20
  98. #define MAX_CON_ID 16
  99. struct clk_lookup_alloc {
  100. struct clk_lookup cl;
  101. char dev_id[MAX_DEV_ID];
  102. char con_id[MAX_CON_ID];
  103. };
  104. struct clk_lookup * __init_refok
  105. clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
  106. {
  107. struct clk_lookup_alloc *cla;
  108. cla = __clkdev_alloc(sizeof(*cla));
  109. if (!cla)
  110. return NULL;
  111. cla->cl.clk = clk;
  112. if (con_id) {
  113. strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
  114. cla->cl.con_id = cla->con_id;
  115. }
  116. if (dev_fmt) {
  117. va_list ap;
  118. va_start(ap, dev_fmt);
  119. vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  120. cla->cl.dev_id = cla->dev_id;
  121. va_end(ap);
  122. }
  123. return &cla->cl;
  124. }
  125. EXPORT_SYMBOL(clkdev_alloc);
  126. int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
  127. struct device *dev)
  128. {
  129. struct clk *r = clk_get(dev, id);
  130. struct clk_lookup *l;
  131. if (IS_ERR(r))
  132. return PTR_ERR(r);
  133. l = clkdev_alloc(r, alias, alias_dev_name);
  134. clk_put(r);
  135. if (!l)
  136. return -ENODEV;
  137. clkdev_add(l);
  138. return 0;
  139. }
  140. EXPORT_SYMBOL(clk_add_alias);
  141. /*
  142. * clkdev_drop - remove a clock dynamically allocated
  143. */
  144. void clkdev_drop(struct clk_lookup *cl)
  145. {
  146. mutex_lock(&clocks_mutex);
  147. list_del(&cl->node);
  148. mutex_unlock(&clocks_mutex);
  149. kfree(cl);
  150. }
  151. EXPORT_SYMBOL(clkdev_drop);