clk-gate2.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
  3. * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Gated clock implementation
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/io.h>
  15. #include <linux/err.h>
  16. #include <linux/string.h>
  17. #include "clk.h"
  18. /**
  19. * DOC: basic gatable clock which can gate and ungate it's ouput
  20. *
  21. * Traits of this clock:
  22. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  23. * enable - clk_enable and clk_disable are functional & control gating
  24. * rate - inherits rate from parent. No clk_set_rate support
  25. * parent - fixed parent. No clk_set_parent support
  26. */
  27. #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
  28. static int clk_gate2_enable(struct clk_hw *hw)
  29. {
  30. struct clk_gate *gate = to_clk_gate(hw);
  31. u32 reg;
  32. unsigned long flags = 0;
  33. if (gate->lock)
  34. spin_lock_irqsave(gate->lock, flags);
  35. reg = readl(gate->reg);
  36. reg |= 3 << gate->bit_idx;
  37. writel(reg, gate->reg);
  38. if (gate->lock)
  39. spin_unlock_irqrestore(gate->lock, flags);
  40. return 0;
  41. }
  42. static void clk_gate2_disable(struct clk_hw *hw)
  43. {
  44. struct clk_gate *gate = to_clk_gate(hw);
  45. u32 reg;
  46. unsigned long flags = 0;
  47. if (gate->lock)
  48. spin_lock_irqsave(gate->lock, flags);
  49. reg = readl(gate->reg);
  50. reg &= ~(3 << gate->bit_idx);
  51. writel(reg, gate->reg);
  52. if (gate->lock)
  53. spin_unlock_irqrestore(gate->lock, flags);
  54. }
  55. static int clk_gate2_is_enabled(struct clk_hw *hw)
  56. {
  57. u32 reg;
  58. struct clk_gate *gate = to_clk_gate(hw);
  59. reg = readl(gate->reg);
  60. if (((reg >> gate->bit_idx) & 3) == 3)
  61. return 1;
  62. return 0;
  63. }
  64. static struct clk_ops clk_gate2_ops = {
  65. .enable = clk_gate2_enable,
  66. .disable = clk_gate2_disable,
  67. .is_enabled = clk_gate2_is_enabled,
  68. };
  69. struct clk *clk_register_gate2(struct device *dev, const char *name,
  70. const char *parent_name, unsigned long flags,
  71. void __iomem *reg, u8 bit_idx,
  72. u8 clk_gate2_flags, spinlock_t *lock)
  73. {
  74. struct clk_gate *gate;
  75. struct clk *clk;
  76. struct clk_init_data init;
  77. gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
  78. if (!gate)
  79. return ERR_PTR(-ENOMEM);
  80. /* struct clk_gate assignments */
  81. gate->reg = reg;
  82. gate->bit_idx = bit_idx;
  83. gate->flags = clk_gate2_flags;
  84. gate->lock = lock;
  85. init.name = name;
  86. init.ops = &clk_gate2_ops;
  87. init.flags = flags;
  88. init.parent_names = parent_name ? &parent_name : NULL;
  89. init.num_parents = parent_name ? 1 : 0;
  90. gate->hw.init = &init;
  91. clk = clk_register(dev, &gate->hw);
  92. if (IS_ERR(clk))
  93. kfree(gate);
  94. return clk;
  95. }