1 // SPDX-License-Identifier: GPL-2.0-or-later
6 #include <linux/slab.h>
7 #include <linux/clk-provider.h>
9 #include <linux/spinlock.h>
10 #include <linux/kernel.h>
13 struct rockchip_inv_clock {
21 #define to_inv_clock(_hw) container_of(_hw, struct rockchip_inv_clock, hw)
23 #define INVERTER_MASK 0x1
25 static int rockchip_inv_get_phase(struct clk_hw *hw)
27 struct rockchip_inv_clock *inv_clock = to_inv_clock(hw);
30 val = readl(inv_clock->reg) >> inv_clock->shift;
35 static int rockchip_inv_set_phase(struct clk_hw *hw, int degrees)
37 struct rockchip_inv_clock *inv_clock = to_inv_clock(hw);
40 if (degrees % 180 == 0) {
43 pr_err("%s: unsupported phase %d for %s\n",
44 __func__, degrees, clk_hw_get_name(hw));
48 if (inv_clock->flags & ROCKCHIP_INVERTER_HIWORD_MASK) {
49 writel(HIWORD_UPDATE(val, INVERTER_MASK, inv_clock->shift),
55 spin_lock_irqsave(inv_clock->lock, flags);
57 reg = readl(inv_clock->reg);
58 reg &= ~BIT(inv_clock->shift);
60 writel(reg, inv_clock->reg);
62 spin_unlock_irqrestore(inv_clock->lock, flags);
68 static const struct clk_ops rockchip_inv_clk_ops = {
69 .get_phase = rockchip_inv_get_phase,
70 .set_phase = rockchip_inv_set_phase,
73 struct clk *rockchip_clk_register_inverter(const char *name,
74 const char *const *parent_names, u8 num_parents,
75 void __iomem *reg, int shift, int flags,
78 struct clk_init_data init;
79 struct rockchip_inv_clock *inv_clock;
82 inv_clock = kmalloc(sizeof(*inv_clock), GFP_KERNEL);
84 return ERR_PTR(-ENOMEM);
87 init.num_parents = num_parents;
88 init.flags = CLK_SET_RATE_PARENT;
89 init.parent_names = parent_names;
90 init.ops = &rockchip_inv_clk_ops;
92 inv_clock->hw.init = &init;
94 inv_clock->shift = shift;
95 inv_clock->flags = flags;
96 inv_clock->lock = lock;
98 clk = clk_register(NULL, &inv_clock->hw);