clkdev.c 3.4 KB

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