clkdev.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * arch/sh/kernel/clkdev.c
  3. *
  4. * Cloned from arch/arm/common/clkdev.c:
  5. *
  6. * Copyright (C) 2008 Russell King.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Helper for the clk API to assist looking up a struct clk.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/device.h>
  17. #include <linux/list.h>
  18. #include <linux/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/string.h>
  21. #include <linux/mutex.h>
  22. #include <linux/clk.h>
  23. #include <linux/slab.h>
  24. #include <linux/bootmem.h>
  25. #include <linux/mm.h>
  26. #include <asm/clock.h>
  27. #include <asm/clkdev.h>
  28. static LIST_HEAD(clocks);
  29. static DEFINE_MUTEX(clocks_mutex);
  30. /*
  31. * Find the correct struct clk for the device and connection ID.
  32. * We do slightly fuzzy matching here:
  33. * An entry with a NULL ID is assumed to be a wildcard.
  34. * If an entry has a device ID, it must match
  35. * If an entry has a connection ID, it must match
  36. * Then we take the most specific entry - with the following
  37. * order of precedence: dev+con > dev only > con only.
  38. */
  39. static struct clk *clk_find(const char *dev_id, const char *con_id)
  40. {
  41. struct clk_lookup *p;
  42. struct clk *clk = NULL;
  43. int match, best = 0;
  44. list_for_each_entry(p, &clocks, node) {
  45. match = 0;
  46. if (p->dev_id) {
  47. if (!dev_id || strcmp(p->dev_id, dev_id))
  48. continue;
  49. match += 2;
  50. }
  51. if (p->con_id) {
  52. if (!con_id || strcmp(p->con_id, con_id))
  53. continue;
  54. match += 1;
  55. }
  56. if (match == 0)
  57. continue;
  58. if (match > best) {
  59. clk = p->clk;
  60. best = match;
  61. }
  62. }
  63. return clk;
  64. }
  65. struct clk *clk_get_sys(const char *dev_id, const char *con_id)
  66. {
  67. struct clk *clk;
  68. mutex_lock(&clocks_mutex);
  69. clk = clk_find(dev_id, con_id);
  70. mutex_unlock(&clocks_mutex);
  71. return clk ? clk : ERR_PTR(-ENOENT);
  72. }
  73. EXPORT_SYMBOL(clk_get_sys);
  74. void clkdev_add(struct clk_lookup *cl)
  75. {
  76. mutex_lock(&clocks_mutex);
  77. list_add_tail(&cl->node, &clocks);
  78. mutex_unlock(&clocks_mutex);
  79. }
  80. EXPORT_SYMBOL(clkdev_add);
  81. void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
  82. {
  83. mutex_lock(&clocks_mutex);
  84. while (num--) {
  85. list_add_tail(&cl->node, &clocks);
  86. cl++;
  87. }
  88. mutex_unlock(&clocks_mutex);
  89. }
  90. #define MAX_DEV_ID 20
  91. #define MAX_CON_ID 16
  92. struct clk_lookup_alloc {
  93. struct clk_lookup cl;
  94. char dev_id[MAX_DEV_ID];
  95. char con_id[MAX_CON_ID];
  96. };
  97. struct clk_lookup * __init_refok
  98. clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
  99. {
  100. struct clk_lookup_alloc *cla;
  101. if (!slab_is_available())
  102. cla = alloc_bootmem_low_pages(sizeof(*cla));
  103. else
  104. cla = kzalloc(sizeof(*cla), GFP_KERNEL);
  105. if (!cla)
  106. return NULL;
  107. cla->cl.clk = clk;
  108. if (con_id) {
  109. strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
  110. cla->cl.con_id = cla->con_id;
  111. }
  112. if (dev_fmt) {
  113. va_list ap;
  114. va_start(ap, dev_fmt);
  115. vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  116. cla->cl.dev_id = cla->dev_id;
  117. va_end(ap);
  118. }
  119. return &cla->cl;
  120. }
  121. EXPORT_SYMBOL(clkdev_alloc);
  122. int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
  123. struct device *dev)
  124. {
  125. struct clk *r = clk_get(dev, id);
  126. struct clk_lookup *l;
  127. if (IS_ERR(r))
  128. return PTR_ERR(r);
  129. l = clkdev_alloc(r, alias, alias_dev_name);
  130. clk_put(r);
  131. if (!l)
  132. return -ENODEV;
  133. clkdev_add(l);
  134. return 0;
  135. }
  136. EXPORT_SYMBOL(clk_add_alias);
  137. /*
  138. * clkdev_drop - remove a clock dynamically allocated
  139. */
  140. void clkdev_drop(struct clk_lookup *cl)
  141. {
  142. mutex_lock(&clocks_mutex);
  143. list_del(&cl->node);
  144. mutex_unlock(&clocks_mutex);
  145. kfree(cl);
  146. }
  147. EXPORT_SYMBOL(clkdev_drop);