clkgate-separated.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hisilicon clock separated gate driver
  4. *
  5. * Copyright (c) 2012-2013 Hisilicon Limited.
  6. * Copyright (c) 2012-2013 Linaro Limited.
  7. *
  8. * Author: Haojian Zhuang <[email protected]>
  9. * Xin Li <[email protected]>
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/io.h>
  14. #include <linux/slab.h>
  15. #include "clk.h"
  16. /* clock separated gate register offset */
  17. #define CLKGATE_SEPERATED_ENABLE 0x0
  18. #define CLKGATE_SEPERATED_DISABLE 0x4
  19. #define CLKGATE_SEPERATED_STATUS 0x8
  20. struct clkgate_separated {
  21. struct clk_hw hw;
  22. void __iomem *enable; /* enable register */
  23. u8 bit_idx; /* bits in enable/disable register */
  24. u8 flags;
  25. spinlock_t *lock;
  26. };
  27. static int clkgate_separated_enable(struct clk_hw *hw)
  28. {
  29. struct clkgate_separated *sclk;
  30. unsigned long flags = 0;
  31. u32 reg;
  32. sclk = container_of(hw, struct clkgate_separated, hw);
  33. if (sclk->lock)
  34. spin_lock_irqsave(sclk->lock, flags);
  35. reg = BIT(sclk->bit_idx);
  36. writel_relaxed(reg, sclk->enable);
  37. readl_relaxed(sclk->enable + CLKGATE_SEPERATED_STATUS);
  38. if (sclk->lock)
  39. spin_unlock_irqrestore(sclk->lock, flags);
  40. return 0;
  41. }
  42. static void clkgate_separated_disable(struct clk_hw *hw)
  43. {
  44. struct clkgate_separated *sclk;
  45. unsigned long flags = 0;
  46. u32 reg;
  47. sclk = container_of(hw, struct clkgate_separated, hw);
  48. if (sclk->lock)
  49. spin_lock_irqsave(sclk->lock, flags);
  50. reg = BIT(sclk->bit_idx);
  51. writel_relaxed(reg, sclk->enable + CLKGATE_SEPERATED_DISABLE);
  52. readl_relaxed(sclk->enable + CLKGATE_SEPERATED_STATUS);
  53. if (sclk->lock)
  54. spin_unlock_irqrestore(sclk->lock, flags);
  55. }
  56. static int clkgate_separated_is_enabled(struct clk_hw *hw)
  57. {
  58. struct clkgate_separated *sclk;
  59. u32 reg;
  60. sclk = container_of(hw, struct clkgate_separated, hw);
  61. reg = readl_relaxed(sclk->enable + CLKGATE_SEPERATED_STATUS);
  62. reg &= BIT(sclk->bit_idx);
  63. return reg ? 1 : 0;
  64. }
  65. static const struct clk_ops clkgate_separated_ops = {
  66. .enable = clkgate_separated_enable,
  67. .disable = clkgate_separated_disable,
  68. .is_enabled = clkgate_separated_is_enabled,
  69. };
  70. struct clk *hisi_register_clkgate_sep(struct device *dev, const char *name,
  71. const char *parent_name,
  72. unsigned long flags,
  73. void __iomem *reg, u8 bit_idx,
  74. u8 clk_gate_flags, spinlock_t *lock)
  75. {
  76. struct clkgate_separated *sclk;
  77. struct clk *clk;
  78. struct clk_init_data init;
  79. sclk = kzalloc(sizeof(*sclk), GFP_KERNEL);
  80. if (!sclk)
  81. return ERR_PTR(-ENOMEM);
  82. init.name = name;
  83. init.ops = &clkgate_separated_ops;
  84. init.flags = flags;
  85. init.parent_names = (parent_name ? &parent_name : NULL);
  86. init.num_parents = (parent_name ? 1 : 0);
  87. sclk->enable = reg + CLKGATE_SEPERATED_ENABLE;
  88. sclk->bit_idx = bit_idx;
  89. sclk->flags = clk_gate_flags;
  90. sclk->hw.init = &init;
  91. sclk->lock = lock;
  92. clk = clk_register(dev, &sclk->hw);
  93. if (IS_ERR(clk))
  94. kfree(sclk);
  95. return clk;
  96. }