clkdev.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <asm/clkdev.h>
  21. #include <mach/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 precidence: dev+con > dev only > con only.
  32. */
  33. static struct clk *clk_find(const char *dev_id, const char *con_id)
  34. {
  35. struct clk_lookup *p;
  36. struct clk *clk = NULL;
  37. int match, best = 0;
  38. list_for_each_entry(p, &clocks, node) {
  39. match = 0;
  40. if (p->dev_id) {
  41. if (!dev_id || strcmp(p->dev_id, dev_id))
  42. continue;
  43. match += 2;
  44. }
  45. if (p->con_id) {
  46. if (!con_id || strcmp(p->con_id, con_id))
  47. continue;
  48. match += 1;
  49. }
  50. if (match == 0)
  51. continue;
  52. if (match > best) {
  53. clk = p->clk;
  54. best = match;
  55. }
  56. }
  57. return clk;
  58. }
  59. struct clk *clk_get(struct device *dev, const char *con_id)
  60. {
  61. const char *dev_id = dev ? dev_name(dev) : NULL;
  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);
  71. void clk_put(struct clk *clk)
  72. {
  73. __clk_put(clk);
  74. }
  75. EXPORT_SYMBOL(clk_put);
  76. void clkdev_add(struct clk_lookup *cl)
  77. {
  78. mutex_lock(&clocks_mutex);
  79. list_add_tail(&cl->node, &clocks);
  80. mutex_unlock(&clocks_mutex);
  81. }
  82. EXPORT_SYMBOL(clkdev_add);
  83. #define MAX_DEV_ID 20
  84. #define MAX_CON_ID 16
  85. struct clk_lookup_alloc {
  86. struct clk_lookup cl;
  87. char dev_id[MAX_DEV_ID];
  88. char con_id[MAX_CON_ID];
  89. };
  90. struct clk_lookup *clkdev_alloc(struct clk *clk, const char *con_id,
  91. const char *dev_fmt, ...)
  92. {
  93. struct clk_lookup_alloc *cla;
  94. cla = kzalloc(sizeof(*cla), GFP_KERNEL);
  95. if (!cla)
  96. return NULL;
  97. cla->cl.clk = clk;
  98. if (con_id) {
  99. strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
  100. cla->cl.con_id = cla->con_id;
  101. }
  102. if (dev_fmt) {
  103. va_list ap;
  104. va_start(ap, dev_fmt);
  105. vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  106. cla->cl.dev_id = cla->dev_id;
  107. va_end(ap);
  108. }
  109. return &cla->cl;
  110. }
  111. EXPORT_SYMBOL(clkdev_alloc);
  112. /*
  113. * clkdev_drop - remove a clock dynamically allocated
  114. */
  115. void clkdev_drop(struct clk_lookup *cl)
  116. {
  117. mutex_lock(&clocks_mutex);
  118. list_del(&cl->node);
  119. mutex_unlock(&clocks_mutex);
  120. kfree(cl);
  121. }
  122. EXPORT_SYMBOL(clkdev_drop);