clkdev.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * arch/arm/common/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/slab.h>
  22. #include <asm/clkdev.h>
  23. #include <mach/clkdev.h>
  24. static LIST_HEAD(clocks);
  25. static DEFINE_MUTEX(clocks_mutex);
  26. /*
  27. * Find the correct struct clk for the device and connection ID.
  28. * We do slightly fuzzy matching here:
  29. * An entry with a NULL ID is assumed to be a wildcard.
  30. * If an entry has a device ID, it must match
  31. * If an entry has a connection ID, it must match
  32. * Then we take the most specific entry - with the following
  33. * order of precedence: dev+con > dev only > con only.
  34. */
  35. static struct clk *clk_find(const char *dev_id, const char *con_id)
  36. {
  37. struct clk_lookup *p;
  38. struct clk *clk = NULL;
  39. int match, best = 0;
  40. list_for_each_entry(p, &clocks, node) {
  41. match = 0;
  42. if (p->dev_id) {
  43. if (!dev_id || strcmp(p->dev_id, dev_id))
  44. continue;
  45. match += 2;
  46. }
  47. if (p->con_id) {
  48. if (!con_id || strcmp(p->con_id, con_id))
  49. continue;
  50. match += 1;
  51. }
  52. if (match > best) {
  53. clk = p->clk;
  54. if (match != 3)
  55. best = match;
  56. else
  57. break;
  58. }
  59. }
  60. return clk;
  61. }
  62. struct clk *clk_get_sys(const char *dev_id, const char *con_id)
  63. {
  64. struct clk *clk;
  65. mutex_lock(&clocks_mutex);
  66. clk = clk_find(dev_id, con_id);
  67. if (clk && !__clk_get(clk))
  68. clk = NULL;
  69. mutex_unlock(&clocks_mutex);
  70. return clk ? clk : ERR_PTR(-ENOENT);
  71. }
  72. EXPORT_SYMBOL(clk_get_sys);
  73. struct clk *clk_get(struct device *dev, const char *con_id)
  74. {
  75. const char *dev_id = dev ? dev_name(dev) : NULL;
  76. return clk_get_sys(dev_id, con_id);
  77. }
  78. EXPORT_SYMBOL(clk_get);
  79. void clk_put(struct clk *clk)
  80. {
  81. __clk_put(clk);
  82. }
  83. EXPORT_SYMBOL(clk_put);
  84. void clkdev_add(struct clk_lookup *cl)
  85. {
  86. mutex_lock(&clocks_mutex);
  87. list_add_tail(&cl->node, &clocks);
  88. mutex_unlock(&clocks_mutex);
  89. }
  90. EXPORT_SYMBOL(clkdev_add);
  91. void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
  92. {
  93. mutex_lock(&clocks_mutex);
  94. while (num--) {
  95. list_add_tail(&cl->node, &clocks);
  96. cl++;
  97. }
  98. mutex_unlock(&clocks_mutex);
  99. }
  100. #define MAX_DEV_ID 20
  101. #define MAX_CON_ID 16
  102. struct clk_lookup_alloc {
  103. struct clk_lookup cl;
  104. char dev_id[MAX_DEV_ID];
  105. char con_id[MAX_CON_ID];
  106. };
  107. struct clk_lookup *clkdev_alloc(struct clk *clk, const char *con_id,
  108. const char *dev_fmt, ...)
  109. {
  110. struct clk_lookup_alloc *cla;
  111. cla = kzalloc(sizeof(*cla), GFP_KERNEL);
  112. if (!cla)
  113. return NULL;
  114. cla->cl.clk = clk;
  115. if (con_id) {
  116. strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
  117. cla->cl.con_id = cla->con_id;
  118. }
  119. if (dev_fmt) {
  120. va_list ap;
  121. va_start(ap, dev_fmt);
  122. vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  123. cla->cl.dev_id = cla->dev_id;
  124. va_end(ap);
  125. }
  126. return &cla->cl;
  127. }
  128. EXPORT_SYMBOL(clkdev_alloc);
  129. int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
  130. struct device *dev)
  131. {
  132. struct clk *r = clk_get(dev, id);
  133. struct clk_lookup *l;
  134. if (IS_ERR(r))
  135. return PTR_ERR(r);
  136. l = clkdev_alloc(r, alias, alias_dev_name);
  137. clk_put(r);
  138. if (!l)
  139. return -ENODEV;
  140. clkdev_add(l);
  141. return 0;
  142. }
  143. EXPORT_SYMBOL(clk_add_alias);
  144. /*
  145. * clkdev_drop - remove a clock dynamically allocated
  146. */
  147. void clkdev_drop(struct clk_lookup *cl)
  148. {
  149. mutex_lock(&clocks_mutex);
  150. list_del(&cl->node);
  151. mutex_unlock(&clocks_mutex);
  152. kfree(cl);
  153. }
  154. EXPORT_SYMBOL(clkdev_drop);