2 * mmp gate clock operation source file
4 * Copyright (C) 2014 Marvell
5 * Chao Xie <chao.xie@marvell.com>
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
12 #include <linux/clk-provider.h>
13 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/delay.h>
21 * Some clocks will have mutiple bits to enable the clocks, and
22 * the bits to disable the clock is not same as enabling bits.
25 #define to_clk_mmp_gate(hw) container_of(hw, struct mmp_clk_gate, hw)
27 static int mmp_clk_gate_enable(struct clk_hw
*hw
)
29 struct mmp_clk_gate
*gate
= to_clk_mmp_gate(hw
);
30 struct clk
*clk
= hw
->clk
;
31 unsigned long flags
= 0;
36 spin_lock_irqsave(gate
->lock
, flags
);
38 tmp
= readl(gate
->reg
);
40 tmp
|= gate
->val_enable
;
41 writel(tmp
, gate
->reg
);
44 spin_unlock_irqrestore(gate
->lock
, flags
);
46 if (gate
->flags
& MMP_CLK_GATE_NEED_DELAY
) {
47 rate
= __clk_get_rate(clk
);
48 /* Need delay 2 cycles. */
55 static void mmp_clk_gate_disable(struct clk_hw
*hw
)
57 struct mmp_clk_gate
*gate
= to_clk_mmp_gate(hw
);
58 unsigned long flags
= 0;
62 spin_lock_irqsave(gate
->lock
, flags
);
64 tmp
= readl(gate
->reg
);
66 tmp
|= gate
->val_disable
;
67 writel(tmp
, gate
->reg
);
70 spin_unlock_irqrestore(gate
->lock
, flags
);
73 static int mmp_clk_gate_is_enabled(struct clk_hw
*hw
)
75 struct mmp_clk_gate
*gate
= to_clk_mmp_gate(hw
);
76 unsigned long flags
= 0;
80 spin_lock_irqsave(gate
->lock
, flags
);
82 tmp
= readl(gate
->reg
);
85 spin_unlock_irqrestore(gate
->lock
, flags
);
87 return (tmp
& gate
->mask
) == gate
->val_enable
;
90 const struct clk_ops mmp_clk_gate_ops
= {
91 .enable
= mmp_clk_gate_enable
,
92 .disable
= mmp_clk_gate_disable
,
93 .is_enabled
= mmp_clk_gate_is_enabled
,
96 struct clk
*mmp_clk_register_gate(struct device
*dev
, const char *name
,
97 const char *parent_name
, unsigned long flags
,
98 void __iomem
*reg
, u32 mask
, u32 val_enable
, u32 val_disable
,
99 unsigned int gate_flags
, spinlock_t
*lock
)
101 struct mmp_clk_gate
*gate
;
103 struct clk_init_data init
;
105 /* allocate the gate */
106 gate
= kzalloc(sizeof(*gate
), GFP_KERNEL
);
108 pr_err("%s:%s could not allocate gate clk\n", __func__
, name
);
109 return ERR_PTR(-ENOMEM
);
113 init
.ops
= &mmp_clk_gate_ops
;
114 init
.flags
= flags
| CLK_IS_BASIC
;
115 init
.parent_names
= (parent_name
? &parent_name
: NULL
);
116 init
.num_parents
= (parent_name
? 1 : 0);
118 /* struct clk_gate assignments */
121 gate
->val_enable
= val_enable
;
122 gate
->val_disable
= val_disable
;
123 gate
->flags
= gate_flags
;
125 gate
->hw
.init
= &init
;
127 clk
= clk_register(dev
, &gate
->hw
);