Re: [PATCH V6 1/9] clk: imx: add gatable clock divider support

2018-12-03 Thread Stephen Boyd
Quoting A.s. Dong (2018-11-14 05:01:35)
> For dividers with zero indicating clock is disabled, instead of giving a
> warning each time like "clkx: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not
> set" in exist code, we'd like to introduce enable/disable function for it.
> e.g.
> 000b - Clock disabled
> 001b - Divide by 1
> 010b - Divide by 2
> ...
> 
> Set rate when the clk is disabled will cache the rate request and only
> when the clk is enabled will the driver actually program the hardware to
> have the requested divider value. Similarly, when the clk is disabled we'll
> write a 0 there, but when the clk is enabled we'll restore whatever rate
> (divider) was chosen last.
> 
> It does mean that recalc rate will be sort of odd, because when the clk is
> off it will return 0, and when the clk is on it will return the right rate.
> So to make things work, we'll need to return the cached rate in recalc rate
> when the clk is off and read the hardware when the clk is on.
> 
> NOTE for the default off divider, the recalc rate will still return 0 as
> there's still no proper preset rate. Enable such divider will give user
> a reminder error message.
> 
> Cc: Stephen Boyd 
> Cc: Michael Turquette 
> Cc: Shawn Guo 
> Signed-off-by: Dong Aisheng 
> 
> ---

Applied to clk-next



[PATCH V6 1/9] clk: imx: add gatable clock divider support

2018-11-14 Thread A.s. Dong
For dividers with zero indicating clock is disabled, instead of giving a
warning each time like "clkx: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not
set" in exist code, we'd like to introduce enable/disable function for it.
e.g.
000b - Clock disabled
001b - Divide by 1
010b - Divide by 2
...

Set rate when the clk is disabled will cache the rate request and only
when the clk is enabled will the driver actually program the hardware to
have the requested divider value. Similarly, when the clk is disabled we'll
write a 0 there, but when the clk is enabled we'll restore whatever rate
(divider) was chosen last.

It does mean that recalc rate will be sort of odd, because when the clk is
off it will return 0, and when the clk is on it will return the right rate.
So to make things work, we'll need to return the cached rate in recalc rate
when the clk is off and read the hardware when the clk is on.

NOTE for the default off divider, the recalc rate will still return 0 as
there's still no proper preset rate. Enable such divider will give user
a reminder error message.

Cc: Stephen Boyd 
Cc: Michael Turquette 
Cc: Shawn Guo 
Signed-off-by: Dong Aisheng 

---
ChangeLog:
v5->v6:
 * move gatable divider code into imx specific clock folder as suggested
   by Michael. Patch title also updated accordingly.
v4->v5:
 * no changes
v3->v4:
 * no changes
v2->v3:
 * split normal and gate ops
 * fix the possible racy
v1->v2:
 * add enable/disable for the type of CLK_DIVIDER_ZERO_GATE dividers
---
 drivers/clk/imx/Makefile   |   1 +
 drivers/clk/imx/clk-divider-gate.c | 219 +
 drivers/clk/imx/clk.h  |   4 +
 3 files changed, 224 insertions(+)
 create mode 100644 drivers/clk/imx/clk-divider-gate.c

diff --git a/drivers/clk/imx/Makefile b/drivers/clk/imx/Makefile
index 8c3baa7..077e732 100644
--- a/drivers/clk/imx/Makefile
+++ b/drivers/clk/imx/Makefile
@@ -4,6 +4,7 @@ obj-y += \
clk.o \
clk-busy.o \
clk-cpu.o \
+   clk-divider-gate.o \
clk-fixup-div.o \
clk-fixup-mux.o \
clk-gate-exclusive.o \
diff --git a/drivers/clk/imx/clk-divider-gate.c 
b/drivers/clk/imx/clk-divider-gate.c
new file mode 100644
index 000..b48fba2
--- /dev/null
+++ b/drivers/clk/imx/clk-divider-gate.c
@@ -0,0 +1,219 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2018 NXP.
+ *   Dong Aisheng 
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+struct clk_divider_gate {
+   struct clk_divider divider;
+   u32 cached_val;
+};
+
+static inline struct clk_divider_gate *to_clk_divider_gate(struct clk_hw *hw)
+{
+   struct clk_divider *div = to_clk_divider(hw);
+
+   return container_of(div, struct clk_divider_gate, divider);
+}
+
+static unsigned long clk_divider_gate_recalc_rate_ro(struct clk_hw *hw,
+unsigned long parent_rate)
+{
+   struct clk_divider *div = to_clk_divider(hw);
+   unsigned int val;
+
+   val = clk_readl(div->reg) >> div->shift;
+   val &= clk_div_mask(div->width);
+   if (!val)
+   return 0;
+
+   return divider_recalc_rate(hw, parent_rate, val, div->table,
+  div->flags, div->width);
+}
+
+static unsigned long clk_divider_gate_recalc_rate(struct clk_hw *hw,
+ unsigned long parent_rate)
+{
+   struct clk_divider_gate *div_gate = to_clk_divider_gate(hw);
+   struct clk_divider *div = to_clk_divider(hw);
+   unsigned long flags = 0;
+   unsigned int val;
+
+   spin_lock_irqsave(div->lock, flags);
+
+   if (!clk_hw_is_enabled(hw)) {
+   val = div_gate->cached_val;
+   } else {
+   val = clk_readl(div->reg) >> div->shift;
+   val &= clk_div_mask(div->width);
+   }
+
+   spin_unlock_irqrestore(div->lock, flags);
+
+   if (!val)
+   return 0;
+
+   return divider_recalc_rate(hw, parent_rate, val, div->table,
+  div->flags, div->width);
+}
+
+static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
+  unsigned long *prate)
+{
+   return clk_divider_ops.round_rate(hw, rate, prate);
+}
+
+static int clk_divider_gate_set_rate(struct clk_hw *hw, unsigned long rate,
+   unsigned long parent_rate)
+{
+   struct clk_divider_gate *div_gate = to_clk_divider_gate(hw);
+   struct clk_divider *div = to_clk_divider(hw);
+   unsigned long flags = 0;
+   int value;
+   u32 val;
+
+   value = divider_get_val(rate, parent_rate, div->table,
+   div->width, div->flags);
+   if (value < 0)
+   return value;
+
+   spin_lock_irqsave(div->lock, flags);
+
+   if (clk_hw_is_enabled(hw)) {
+   val = clk_readl(div->reg);
+   val &= ~(clk_div_mask(div->width) << div->shift);
+