clock.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * linux/arch/arm/mach-sa1100/clock.c
  3. */
  4. #include <linux/module.h>
  5. #include <linux/kernel.h>
  6. #include <linux/list.h>
  7. #include <linux/errno.h>
  8. #include <linux/err.h>
  9. #include <linux/string.h>
  10. #include <linux/clk.h>
  11. #include <linux/spinlock.h>
  12. #include <asm/arch/pxa-regs.h>
  13. #include <asm/hardware.h>
  14. #include <asm/semaphore.h>
  15. struct clk {
  16. struct list_head node;
  17. unsigned long rate;
  18. struct module *owner;
  19. const char *name;
  20. unsigned int enabled;
  21. void (*enable)(void);
  22. void (*disable)(void);
  23. };
  24. static LIST_HEAD(clocks);
  25. static DECLARE_MUTEX(clocks_sem);
  26. static DEFINE_SPINLOCK(clocks_lock);
  27. struct clk *clk_get(struct device *dev, const char *id)
  28. {
  29. struct clk *p, *clk = ERR_PTR(-ENOENT);
  30. down(&clocks_sem);
  31. list_for_each_entry(p, &clocks, node) {
  32. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  33. clk = p;
  34. break;
  35. }
  36. }
  37. up(&clocks_sem);
  38. return clk;
  39. }
  40. EXPORT_SYMBOL(clk_get);
  41. void clk_put(struct clk *clk)
  42. {
  43. module_put(clk->owner);
  44. }
  45. EXPORT_SYMBOL(clk_put);
  46. int clk_enable(struct clk *clk)
  47. {
  48. unsigned long flags;
  49. spin_lock_irqsave(&clocks_lock, flags);
  50. if (clk->enabled++ == 0)
  51. clk->enable();
  52. spin_unlock_irqrestore(&clocks_lock, flags);
  53. return 0;
  54. }
  55. EXPORT_SYMBOL(clk_enable);
  56. void clk_disable(struct clk *clk)
  57. {
  58. unsigned long flags;
  59. WARN_ON(clk->enabled == 0);
  60. spin_lock_irqsave(&clocks_lock, flags);
  61. if (--clk->enabled == 0)
  62. clk->disable();
  63. spin_unlock_irqrestore(&clocks_lock, flags);
  64. }
  65. EXPORT_SYMBOL(clk_disable);
  66. unsigned long clk_get_rate(struct clk *clk)
  67. {
  68. return clk->rate;
  69. }
  70. EXPORT_SYMBOL(clk_get_rate);
  71. static void clk_gpio27_enable(void)
  72. {
  73. pxa_gpio_mode(GPIO11_3_6MHz_MD);
  74. }
  75. static void clk_gpio27_disable(void)
  76. {
  77. }
  78. static struct clk clk_gpio27 = {
  79. .name = "GPIO27_CLK",
  80. .rate = 3686400,
  81. .enable = clk_gpio27_enable,
  82. .disable = clk_gpio27_disable,
  83. };
  84. int clk_register(struct clk *clk)
  85. {
  86. down(&clocks_sem);
  87. list_add(&clk->node, &clocks);
  88. up(&clocks_sem);
  89. return 0;
  90. }
  91. EXPORT_SYMBOL(clk_register);
  92. void clk_unregister(struct clk *clk)
  93. {
  94. down(&clocks_sem);
  95. list_del(&clk->node);
  96. up(&clocks_sem);
  97. }
  98. EXPORT_SYMBOL(clk_unregister);
  99. static int __init clk_init(void)
  100. {
  101. clk_register(&clk_gpio27);
  102. return 0;
  103. }
  104. arch_initcall(clk_init);