clkgate-separated.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/clkdev.h>
  28. #include <linux/io.h>
  29. #include <linux/slab.h>
  30. #include <linux/clk.h>
  31. #include "clk.h"
  32. /* clock separated gate register offset */
  33. #define CLKGATE_SEPERATED_ENABLE 0x0
  34. #define CLKGATE_SEPERATED_DISABLE 0x4
  35. #define CLKGATE_SEPERATED_STATUS 0x8
  36. struct clkgate_separated {
  37. struct clk_hw hw;
  38. void __iomem *enable; /* enable register */
  39. u8 bit_idx; /* bits in enable/disable register */
  40. u8 flags;
  41. spinlock_t *lock;
  42. };
  43. static int clkgate_separated_enable(struct clk_hw *hw)
  44. {
  45. struct clkgate_separated *sclk;
  46. unsigned long flags = 0;
  47. u32 reg;
  48. sclk = container_of(hw, struct clkgate_separated, hw);
  49. if (sclk->lock)
  50. spin_lock_irqsave(sclk->lock, flags);
  51. reg = BIT(sclk->bit_idx);
  52. writel_relaxed(reg, sclk->enable);
  53. readl_relaxed(sclk->enable + CLKGATE_SEPERATED_STATUS);
  54. if (sclk->lock)
  55. spin_unlock_irqrestore(sclk->lock, flags);
  56. return 0;
  57. }
  58. static void clkgate_separated_disable(struct clk_hw *hw)
  59. {
  60. struct clkgate_separated *sclk;
  61. unsigned long flags = 0;
  62. u32 reg;
  63. sclk = container_of(hw, struct clkgate_separated, hw);
  64. if (sclk->lock)
  65. spin_lock_irqsave(sclk->lock, flags);
  66. reg = BIT(sclk->bit_idx);
  67. writel_relaxed(reg, sclk->enable + CLKGATE_SEPERATED_DISABLE);
  68. readl_relaxed(sclk->enable + CLKGATE_SEPERATED_STATUS);
  69. if (sclk->lock)
  70. spin_unlock_irqrestore(sclk->lock, flags);
  71. }
  72. static int clkgate_separated_is_enabled(struct clk_hw *hw)
  73. {
  74. struct clkgate_separated *sclk;
  75. u32 reg;
  76. sclk = container_of(hw, struct clkgate_separated, hw);
  77. reg = readl_relaxed(sclk->enable + CLKGATE_SEPERATED_STATUS);
  78. reg &= BIT(sclk->bit_idx);
  79. return reg ? 1 : 0;
  80. }
  81. static struct clk_ops clkgate_separated_ops = {
  82. .enable = clkgate_separated_enable,
  83. .disable = clkgate_separated_disable,
  84. .is_enabled = clkgate_separated_is_enabled,
  85. };
  86. struct clk *hisi_register_clkgate_sep(struct device *dev, const char *name,
  87. const char *parent_name,
  88. unsigned long flags,
  89. void __iomem *reg, u8 bit_idx,
  90. u8 clk_gate_flags, spinlock_t *lock)
  91. {
  92. struct clkgate_separated *sclk;
  93. struct clk *clk;
  94. struct clk_init_data init;
  95. sclk = kzalloc(sizeof(*sclk), GFP_KERNEL);
  96. if (!sclk) {
  97. pr_err("%s: fail to allocate separated gated clk\n", __func__);
  98. return ERR_PTR(-ENOMEM);
  99. }
  100. init.name = name;
  101. init.ops = &clkgate_separated_ops;
  102. init.flags = flags | CLK_IS_BASIC;
  103. init.parent_names = (parent_name ? &parent_name : NULL);
  104. init.num_parents = (parent_name ? 1 : 0);
  105. sclk->enable = reg + CLKGATE_SEPERATED_ENABLE;
  106. sclk->bit_idx = bit_idx;
  107. sclk->flags = clk_gate_flags;
  108. sclk->hw.init = &init;
  109. clk = clk_register(dev, &sclk->hw);
  110. if (IS_ERR(clk))
  111. kfree(sclk);
  112. return clk;
  113. }