clkgate-separated.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Hisilicon clock separated gate driver
  3. *
  4. * Copyright (c) 2012-2013 Hisilicon Limited.
  5. * Copyright (c) 2012-2013 Linaro Limited.
  6. *
  7. * Author: Haojian Zhuang <haojian.zhuang@linaro.org>
  8. * Xin Li <li.xin@linaro.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/clk-provider.h>
  27. #include <linux/io.h>
  28. #include <linux/slab.h>
  29. #include "clk.h"
  30. /* clock separated gate register offset */
  31. #define CLKGATE_SEPERATED_ENABLE 0x0
  32. #define CLKGATE_SEPERATED_DISABLE 0x4
  33. #define CLKGATE_SEPERATED_STATUS 0x8
  34. struct clkgate_separated {
  35. struct clk_hw hw;
  36. void __iomem *enable; /* enable register */
  37. u8 bit_idx; /* bits in enable/disable register */
  38. u8 flags;
  39. spinlock_t *lock;
  40. };
  41. static int clkgate_separated_enable(struct clk_hw *hw)
  42. {
  43. struct clkgate_separated *sclk;
  44. unsigned long flags = 0;
  45. u32 reg;
  46. sclk = container_of(hw, struct clkgate_separated, hw);
  47. if (sclk->lock)
  48. spin_lock_irqsave(sclk->lock, flags);
  49. reg = BIT(sclk->bit_idx);
  50. writel_relaxed(reg, sclk->enable);
  51. readl_relaxed(sclk->enable + CLKGATE_SEPERATED_STATUS);
  52. if (sclk->lock)
  53. spin_unlock_irqrestore(sclk->lock, flags);
  54. return 0;
  55. }
  56. static void clkgate_separated_disable(struct clk_hw *hw)
  57. {
  58. struct clkgate_separated *sclk;
  59. unsigned long flags = 0;
  60. u32 reg;
  61. sclk = container_of(hw, struct clkgate_separated, hw);
  62. if (sclk->lock)
  63. spin_lock_irqsave(sclk->lock, flags);
  64. reg = BIT(sclk->bit_idx);
  65. writel_relaxed(reg, sclk->enable + CLKGATE_SEPERATED_DISABLE);
  66. readl_relaxed(sclk->enable + CLKGATE_SEPERATED_STATUS);
  67. if (sclk->lock)
  68. spin_unlock_irqrestore(sclk->lock, flags);
  69. }
  70. static int clkgate_separated_is_enabled(struct clk_hw *hw)
  71. {
  72. struct clkgate_separated *sclk;
  73. u32 reg;
  74. sclk = container_of(hw, struct clkgate_separated, hw);
  75. reg = readl_relaxed(sclk->enable + CLKGATE_SEPERATED_STATUS);
  76. reg &= BIT(sclk->bit_idx);
  77. return reg ? 1 : 0;
  78. }
  79. static struct clk_ops clkgate_separated_ops = {
  80. .enable = clkgate_separated_enable,
  81. .disable = clkgate_separated_disable,
  82. .is_enabled = clkgate_separated_is_enabled,
  83. };
  84. struct clk *hisi_register_clkgate_sep(struct device *dev, const char *name,
  85. const char *parent_name,
  86. unsigned long flags,
  87. void __iomem *reg, u8 bit_idx,
  88. u8 clk_gate_flags, spinlock_t *lock)
  89. {
  90. struct clkgate_separated *sclk;
  91. struct clk *clk;
  92. struct clk_init_data init;
  93. sclk = kzalloc(sizeof(*sclk), GFP_KERNEL);
  94. if (!sclk) {
  95. pr_err("%s: fail to allocate separated gated clk\n", __func__);
  96. return ERR_PTR(-ENOMEM);
  97. }
  98. init.name = name;
  99. init.ops = &clkgate_separated_ops;
  100. init.flags = flags | CLK_IS_BASIC;
  101. init.parent_names = (parent_name ? &parent_name : NULL);
  102. init.num_parents = (parent_name ? 1 : 0);
  103. sclk->enable = reg + CLKGATE_SEPERATED_ENABLE;
  104. sclk->bit_idx = bit_idx;
  105. sclk->flags = clk_gate_flags;
  106. sclk->hw.init = &init;
  107. clk = clk_register(dev, &sclk->hw);
  108. if (IS_ERR(clk))
  109. kfree(sclk);
  110. return clk;
  111. }