clkdev.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. static struct clk *clk_find(const char *dev_id, const char *con_id)
  25. {
  26. struct clk_lookup *p;
  27. struct clk *clk = NULL;
  28. int match, best = 0;
  29. list_for_each_entry(p, &clocks, node) {
  30. if ((p->dev_id && !dev_id) || (p->con_id && !con_id))
  31. continue;
  32. match = 0;
  33. if (p->dev_id)
  34. match += 2 * (strcmp(p->dev_id, dev_id) == 0);
  35. if (p->con_id)
  36. match += 1 * (strcmp(p->con_id, con_id) == 0);
  37. if (match == 0)
  38. continue;
  39. if (match > best) {
  40. clk = p->clk;
  41. best = match;
  42. }
  43. }
  44. return clk;
  45. }
  46. struct clk *clk_get(struct device *dev, const char *con_id)
  47. {
  48. const char *dev_id = dev ? dev_name(dev) : NULL;
  49. struct clk *clk;
  50. mutex_lock(&clocks_mutex);
  51. clk = clk_find(dev_id, con_id);
  52. if (clk && !__clk_get(clk))
  53. clk = NULL;
  54. mutex_unlock(&clocks_mutex);
  55. return clk ? clk : ERR_PTR(-ENOENT);
  56. }
  57. EXPORT_SYMBOL(clk_get);
  58. void clk_put(struct clk *clk)
  59. {
  60. __clk_put(clk);
  61. }
  62. EXPORT_SYMBOL(clk_put);
  63. void clkdev_add(struct clk_lookup *cl)
  64. {
  65. mutex_lock(&clocks_mutex);
  66. list_add_tail(&cl->node, &clocks);
  67. mutex_unlock(&clocks_mutex);
  68. }
  69. EXPORT_SYMBOL(clkdev_add);
  70. #define MAX_DEV_ID 20
  71. #define MAX_CON_ID 16
  72. struct clk_lookup_alloc {
  73. struct clk_lookup cl;
  74. char dev_id[MAX_DEV_ID];
  75. char con_id[MAX_CON_ID];
  76. };
  77. struct clk_lookup *clkdev_alloc(struct clk *clk, const char *con_id,
  78. const char *dev_fmt, ...)
  79. {
  80. struct clk_lookup_alloc *cla;
  81. cla = kzalloc(sizeof(*cla), GFP_KERNEL);
  82. if (!cla)
  83. return NULL;
  84. cla->cl.clk = clk;
  85. if (con_id) {
  86. strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
  87. cla->cl.con_id = cla->con_id;
  88. }
  89. if (dev_fmt) {
  90. va_list ap;
  91. va_start(ap, dev_fmt);
  92. vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
  93. cla->cl.dev_id = cla->dev_id;
  94. va_end(ap);
  95. }
  96. return &cla->cl;
  97. }
  98. EXPORT_SYMBOL(clkdev_alloc);
  99. /*
  100. * clkdev_drop - remove a clock dynamically allocated
  101. */
  102. void clkdev_drop(struct clk_lookup *cl)
  103. {
  104. mutex_lock(&clocks_mutex);
  105. list_del(&cl->node);
  106. mutex_unlock(&clocks_mutex);
  107. kfree(cl);
  108. }
  109. EXPORT_SYMBOL(clkdev_drop);