clk-lpcg-scu.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2018 NXP
  4. * Dong Aisheng <[email protected]>
  5. */
  6. #include <linux/bits.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/err.h>
  9. #include <linux/io.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include "clk-scu.h"
  13. static DEFINE_SPINLOCK(imx_lpcg_scu_lock);
  14. #define CLK_GATE_SCU_LPCG_MASK 0x3
  15. #define CLK_GATE_SCU_LPCG_HW_SEL BIT(0)
  16. #define CLK_GATE_SCU_LPCG_SW_SEL BIT(1)
  17. /*
  18. * struct clk_lpcg_scu - Description of LPCG clock
  19. *
  20. * @hw: clk_hw of this LPCG
  21. * @reg: register of this LPCG clock
  22. * @bit_idx: bit index of this LPCG clock
  23. * @hw_gate: HW auto gate enable
  24. *
  25. * This structure describes one LPCG clock
  26. */
  27. struct clk_lpcg_scu {
  28. struct clk_hw hw;
  29. void __iomem *reg;
  30. u8 bit_idx;
  31. bool hw_gate;
  32. /* for state save&restore */
  33. u32 state;
  34. };
  35. #define to_clk_lpcg_scu(_hw) container_of(_hw, struct clk_lpcg_scu, hw)
  36. static int clk_lpcg_scu_enable(struct clk_hw *hw)
  37. {
  38. struct clk_lpcg_scu *clk = to_clk_lpcg_scu(hw);
  39. unsigned long flags;
  40. u32 reg, val;
  41. spin_lock_irqsave(&imx_lpcg_scu_lock, flags);
  42. reg = readl_relaxed(clk->reg);
  43. reg &= ~(CLK_GATE_SCU_LPCG_MASK << clk->bit_idx);
  44. val = CLK_GATE_SCU_LPCG_SW_SEL;
  45. if (clk->hw_gate)
  46. val |= CLK_GATE_SCU_LPCG_HW_SEL;
  47. reg |= val << clk->bit_idx;
  48. writel(reg, clk->reg);
  49. spin_unlock_irqrestore(&imx_lpcg_scu_lock, flags);
  50. return 0;
  51. }
  52. static void clk_lpcg_scu_disable(struct clk_hw *hw)
  53. {
  54. struct clk_lpcg_scu *clk = to_clk_lpcg_scu(hw);
  55. unsigned long flags;
  56. u32 reg;
  57. spin_lock_irqsave(&imx_lpcg_scu_lock, flags);
  58. reg = readl_relaxed(clk->reg);
  59. reg &= ~(CLK_GATE_SCU_LPCG_MASK << clk->bit_idx);
  60. writel(reg, clk->reg);
  61. spin_unlock_irqrestore(&imx_lpcg_scu_lock, flags);
  62. }
  63. static const struct clk_ops clk_lpcg_scu_ops = {
  64. .enable = clk_lpcg_scu_enable,
  65. .disable = clk_lpcg_scu_disable,
  66. };
  67. struct clk_hw *__imx_clk_lpcg_scu(struct device *dev, const char *name,
  68. const char *parent_name, unsigned long flags,
  69. void __iomem *reg, u8 bit_idx, bool hw_gate)
  70. {
  71. struct clk_lpcg_scu *clk;
  72. struct clk_init_data init;
  73. struct clk_hw *hw;
  74. int ret;
  75. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  76. if (!clk)
  77. return ERR_PTR(-ENOMEM);
  78. clk->reg = reg;
  79. clk->bit_idx = bit_idx;
  80. clk->hw_gate = hw_gate;
  81. init.name = name;
  82. init.ops = &clk_lpcg_scu_ops;
  83. init.flags = CLK_SET_RATE_PARENT | flags;
  84. init.parent_names = parent_name ? &parent_name : NULL;
  85. init.num_parents = parent_name ? 1 : 0;
  86. clk->hw.init = &init;
  87. hw = &clk->hw;
  88. ret = clk_hw_register(dev, hw);
  89. if (ret) {
  90. kfree(clk);
  91. hw = ERR_PTR(ret);
  92. return hw;
  93. }
  94. if (dev)
  95. dev_set_drvdata(dev, clk);
  96. return hw;
  97. }
  98. void imx_clk_lpcg_scu_unregister(struct clk_hw *hw)
  99. {
  100. struct clk_lpcg_scu *clk = to_clk_lpcg_scu(hw);
  101. clk_hw_unregister(&clk->hw);
  102. kfree(clk);
  103. }
  104. static int __maybe_unused imx_clk_lpcg_scu_suspend(struct device *dev)
  105. {
  106. struct clk_lpcg_scu *clk = dev_get_drvdata(dev);
  107. clk->state = readl_relaxed(clk->reg);
  108. dev_dbg(dev, "save lpcg state 0x%x\n", clk->state);
  109. return 0;
  110. }
  111. static int __maybe_unused imx_clk_lpcg_scu_resume(struct device *dev)
  112. {
  113. struct clk_lpcg_scu *clk = dev_get_drvdata(dev);
  114. /*
  115. * FIXME: Sometimes writes don't work unless the CPU issues
  116. * them twice
  117. */
  118. writel(clk->state, clk->reg);
  119. writel(clk->state, clk->reg);
  120. dev_dbg(dev, "restore lpcg state 0x%x\n", clk->state);
  121. return 0;
  122. }
  123. const struct dev_pm_ops imx_clk_lpcg_scu_pm_ops = {
  124. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_clk_lpcg_scu_suspend,
  125. imx_clk_lpcg_scu_resume)
  126. };