xref: /openbmc/linux/drivers/clk/x86/clk-cgu.c (revision d058fd9e8984cd9f18564f7fec38e07ce671c8b8)
1*d058fd9eSRahul Tanwar // SPDX-License-Identifier: GPL-2.0
2*d058fd9eSRahul Tanwar /*
3*d058fd9eSRahul Tanwar  * Copyright (C) 2020 Intel Corporation.
4*d058fd9eSRahul Tanwar  * Zhu YiXin <yixin.zhu@intel.com>
5*d058fd9eSRahul Tanwar  * Rahul Tanwar <rahul.tanwar@intel.com>
6*d058fd9eSRahul Tanwar  */
7*d058fd9eSRahul Tanwar #include <linux/clk-provider.h>
8*d058fd9eSRahul Tanwar #include <linux/device.h>
9*d058fd9eSRahul Tanwar #include <linux/of.h>
10*d058fd9eSRahul Tanwar 
11*d058fd9eSRahul Tanwar #include "clk-cgu.h"
12*d058fd9eSRahul Tanwar 
13*d058fd9eSRahul Tanwar #define GATE_HW_REG_STAT(reg)	((reg) + 0x0)
14*d058fd9eSRahul Tanwar #define GATE_HW_REG_EN(reg)	((reg) + 0x4)
15*d058fd9eSRahul Tanwar #define GATE_HW_REG_DIS(reg)	((reg) + 0x8)
16*d058fd9eSRahul Tanwar #define MAX_DDIV_REG	8
17*d058fd9eSRahul Tanwar #define MAX_DIVIDER_VAL 64
18*d058fd9eSRahul Tanwar 
19*d058fd9eSRahul Tanwar #define to_lgm_clk_mux(_hw) container_of(_hw, struct lgm_clk_mux, hw)
20*d058fd9eSRahul Tanwar #define to_lgm_clk_divider(_hw) container_of(_hw, struct lgm_clk_divider, hw)
21*d058fd9eSRahul Tanwar #define to_lgm_clk_gate(_hw) container_of(_hw, struct lgm_clk_gate, hw)
22*d058fd9eSRahul Tanwar #define to_lgm_clk_ddiv(_hw) container_of(_hw, struct lgm_clk_ddiv, hw)
23*d058fd9eSRahul Tanwar 
24*d058fd9eSRahul Tanwar static struct clk_hw *lgm_clk_register_fixed(struct lgm_clk_provider *ctx,
25*d058fd9eSRahul Tanwar 					     const struct lgm_clk_branch *list)
26*d058fd9eSRahul Tanwar {
27*d058fd9eSRahul Tanwar 	unsigned long flags;
28*d058fd9eSRahul Tanwar 
29*d058fd9eSRahul Tanwar 	if (list->div_flags & CLOCK_FLAG_VAL_INIT) {
30*d058fd9eSRahul Tanwar 		spin_lock_irqsave(&ctx->lock, flags);
31*d058fd9eSRahul Tanwar 		lgm_set_clk_val(ctx->membase, list->div_off, list->div_shift,
32*d058fd9eSRahul Tanwar 				list->div_width, list->div_val);
33*d058fd9eSRahul Tanwar 		spin_unlock_irqrestore(&ctx->lock, flags);
34*d058fd9eSRahul Tanwar 	}
35*d058fd9eSRahul Tanwar 
36*d058fd9eSRahul Tanwar 	return clk_hw_register_fixed_rate(NULL, list->name,
37*d058fd9eSRahul Tanwar 					  list->parent_data[0].name,
38*d058fd9eSRahul Tanwar 					  list->flags, list->mux_flags);
39*d058fd9eSRahul Tanwar }
40*d058fd9eSRahul Tanwar 
41*d058fd9eSRahul Tanwar static u8 lgm_clk_mux_get_parent(struct clk_hw *hw)
42*d058fd9eSRahul Tanwar {
43*d058fd9eSRahul Tanwar 	struct lgm_clk_mux *mux = to_lgm_clk_mux(hw);
44*d058fd9eSRahul Tanwar 	unsigned long flags;
45*d058fd9eSRahul Tanwar 	u32 val;
46*d058fd9eSRahul Tanwar 
47*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&mux->lock, flags);
48*d058fd9eSRahul Tanwar 	if (mux->flags & MUX_CLK_SW)
49*d058fd9eSRahul Tanwar 		val = mux->reg;
50*d058fd9eSRahul Tanwar 	else
51*d058fd9eSRahul Tanwar 		val = lgm_get_clk_val(mux->membase, mux->reg, mux->shift,
52*d058fd9eSRahul Tanwar 				      mux->width);
53*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&mux->lock, flags);
54*d058fd9eSRahul Tanwar 	return clk_mux_val_to_index(hw, NULL, mux->flags, val);
55*d058fd9eSRahul Tanwar }
56*d058fd9eSRahul Tanwar 
57*d058fd9eSRahul Tanwar static int lgm_clk_mux_set_parent(struct clk_hw *hw, u8 index)
58*d058fd9eSRahul Tanwar {
59*d058fd9eSRahul Tanwar 	struct lgm_clk_mux *mux = to_lgm_clk_mux(hw);
60*d058fd9eSRahul Tanwar 	unsigned long flags;
61*d058fd9eSRahul Tanwar 	u32 val;
62*d058fd9eSRahul Tanwar 
63*d058fd9eSRahul Tanwar 	val = clk_mux_index_to_val(NULL, mux->flags, index);
64*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&mux->lock, flags);
65*d058fd9eSRahul Tanwar 	if (mux->flags & MUX_CLK_SW)
66*d058fd9eSRahul Tanwar 		mux->reg = val;
67*d058fd9eSRahul Tanwar 	else
68*d058fd9eSRahul Tanwar 		lgm_set_clk_val(mux->membase, mux->reg, mux->shift,
69*d058fd9eSRahul Tanwar 				mux->width, val);
70*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&mux->lock, flags);
71*d058fd9eSRahul Tanwar 
72*d058fd9eSRahul Tanwar 	return 0;
73*d058fd9eSRahul Tanwar }
74*d058fd9eSRahul Tanwar 
75*d058fd9eSRahul Tanwar static int lgm_clk_mux_determine_rate(struct clk_hw *hw,
76*d058fd9eSRahul Tanwar 				      struct clk_rate_request *req)
77*d058fd9eSRahul Tanwar {
78*d058fd9eSRahul Tanwar 	struct lgm_clk_mux *mux = to_lgm_clk_mux(hw);
79*d058fd9eSRahul Tanwar 
80*d058fd9eSRahul Tanwar 	return clk_mux_determine_rate_flags(hw, req, mux->flags);
81*d058fd9eSRahul Tanwar }
82*d058fd9eSRahul Tanwar 
83*d058fd9eSRahul Tanwar static const struct clk_ops lgm_clk_mux_ops = {
84*d058fd9eSRahul Tanwar 	.get_parent = lgm_clk_mux_get_parent,
85*d058fd9eSRahul Tanwar 	.set_parent = lgm_clk_mux_set_parent,
86*d058fd9eSRahul Tanwar 	.determine_rate = lgm_clk_mux_determine_rate,
87*d058fd9eSRahul Tanwar };
88*d058fd9eSRahul Tanwar 
89*d058fd9eSRahul Tanwar static struct clk_hw *
90*d058fd9eSRahul Tanwar lgm_clk_register_mux(struct lgm_clk_provider *ctx,
91*d058fd9eSRahul Tanwar 		     const struct lgm_clk_branch *list)
92*d058fd9eSRahul Tanwar {
93*d058fd9eSRahul Tanwar 	unsigned long flags, cflags = list->mux_flags;
94*d058fd9eSRahul Tanwar 	struct device *dev = ctx->dev;
95*d058fd9eSRahul Tanwar 	u8 shift = list->mux_shift;
96*d058fd9eSRahul Tanwar 	u8 width = list->mux_width;
97*d058fd9eSRahul Tanwar 	struct clk_init_data init = {};
98*d058fd9eSRahul Tanwar 	struct lgm_clk_mux *mux;
99*d058fd9eSRahul Tanwar 	u32 reg = list->mux_off;
100*d058fd9eSRahul Tanwar 	struct clk_hw *hw;
101*d058fd9eSRahul Tanwar 	int ret;
102*d058fd9eSRahul Tanwar 
103*d058fd9eSRahul Tanwar 	mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
104*d058fd9eSRahul Tanwar 	if (!mux)
105*d058fd9eSRahul Tanwar 		return ERR_PTR(-ENOMEM);
106*d058fd9eSRahul Tanwar 
107*d058fd9eSRahul Tanwar 	init.name = list->name;
108*d058fd9eSRahul Tanwar 	init.ops = &lgm_clk_mux_ops;
109*d058fd9eSRahul Tanwar 	init.flags = list->flags;
110*d058fd9eSRahul Tanwar 	init.parent_data = list->parent_data;
111*d058fd9eSRahul Tanwar 	init.num_parents = list->num_parents;
112*d058fd9eSRahul Tanwar 
113*d058fd9eSRahul Tanwar 	mux->membase = ctx->membase;
114*d058fd9eSRahul Tanwar 	mux->lock = ctx->lock;
115*d058fd9eSRahul Tanwar 	mux->reg = reg;
116*d058fd9eSRahul Tanwar 	mux->shift = shift;
117*d058fd9eSRahul Tanwar 	mux->width = width;
118*d058fd9eSRahul Tanwar 	mux->flags = cflags;
119*d058fd9eSRahul Tanwar 	mux->hw.init = &init;
120*d058fd9eSRahul Tanwar 
121*d058fd9eSRahul Tanwar 	hw = &mux->hw;
122*d058fd9eSRahul Tanwar 	ret = clk_hw_register(dev, hw);
123*d058fd9eSRahul Tanwar 	if (ret)
124*d058fd9eSRahul Tanwar 		return ERR_PTR(ret);
125*d058fd9eSRahul Tanwar 
126*d058fd9eSRahul Tanwar 	if (cflags & CLOCK_FLAG_VAL_INIT) {
127*d058fd9eSRahul Tanwar 		spin_lock_irqsave(&mux->lock, flags);
128*d058fd9eSRahul Tanwar 		lgm_set_clk_val(mux->membase, reg, shift, width, list->mux_val);
129*d058fd9eSRahul Tanwar 		spin_unlock_irqrestore(&mux->lock, flags);
130*d058fd9eSRahul Tanwar 	}
131*d058fd9eSRahul Tanwar 
132*d058fd9eSRahul Tanwar 	return hw;
133*d058fd9eSRahul Tanwar }
134*d058fd9eSRahul Tanwar 
135*d058fd9eSRahul Tanwar static unsigned long
136*d058fd9eSRahul Tanwar lgm_clk_divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
137*d058fd9eSRahul Tanwar {
138*d058fd9eSRahul Tanwar 	struct lgm_clk_divider *divider = to_lgm_clk_divider(hw);
139*d058fd9eSRahul Tanwar 	unsigned long flags;
140*d058fd9eSRahul Tanwar 	unsigned int val;
141*d058fd9eSRahul Tanwar 
142*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&divider->lock, flags);
143*d058fd9eSRahul Tanwar 	val = lgm_get_clk_val(divider->membase, divider->reg,
144*d058fd9eSRahul Tanwar 			      divider->shift, divider->width);
145*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&divider->lock, flags);
146*d058fd9eSRahul Tanwar 
147*d058fd9eSRahul Tanwar 	return divider_recalc_rate(hw, parent_rate, val, divider->table,
148*d058fd9eSRahul Tanwar 				   divider->flags, divider->width);
149*d058fd9eSRahul Tanwar }
150*d058fd9eSRahul Tanwar 
151*d058fd9eSRahul Tanwar static long
152*d058fd9eSRahul Tanwar lgm_clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
153*d058fd9eSRahul Tanwar 			   unsigned long *prate)
154*d058fd9eSRahul Tanwar {
155*d058fd9eSRahul Tanwar 	struct lgm_clk_divider *divider = to_lgm_clk_divider(hw);
156*d058fd9eSRahul Tanwar 
157*d058fd9eSRahul Tanwar 	return divider_round_rate(hw, rate, prate, divider->table,
158*d058fd9eSRahul Tanwar 				  divider->width, divider->flags);
159*d058fd9eSRahul Tanwar }
160*d058fd9eSRahul Tanwar 
161*d058fd9eSRahul Tanwar static int
162*d058fd9eSRahul Tanwar lgm_clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
163*d058fd9eSRahul Tanwar 			 unsigned long prate)
164*d058fd9eSRahul Tanwar {
165*d058fd9eSRahul Tanwar 	struct lgm_clk_divider *divider = to_lgm_clk_divider(hw);
166*d058fd9eSRahul Tanwar 	unsigned long flags;
167*d058fd9eSRahul Tanwar 	int value;
168*d058fd9eSRahul Tanwar 
169*d058fd9eSRahul Tanwar 	value = divider_get_val(rate, prate, divider->table,
170*d058fd9eSRahul Tanwar 				divider->width, divider->flags);
171*d058fd9eSRahul Tanwar 	if (value < 0)
172*d058fd9eSRahul Tanwar 		return value;
173*d058fd9eSRahul Tanwar 
174*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&divider->lock, flags);
175*d058fd9eSRahul Tanwar 	lgm_set_clk_val(divider->membase, divider->reg,
176*d058fd9eSRahul Tanwar 			divider->shift, divider->width, value);
177*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&divider->lock, flags);
178*d058fd9eSRahul Tanwar 
179*d058fd9eSRahul Tanwar 	return 0;
180*d058fd9eSRahul Tanwar }
181*d058fd9eSRahul Tanwar 
182*d058fd9eSRahul Tanwar static int lgm_clk_divider_enable_disable(struct clk_hw *hw, int enable)
183*d058fd9eSRahul Tanwar {
184*d058fd9eSRahul Tanwar 	struct lgm_clk_divider *div = to_lgm_clk_divider(hw);
185*d058fd9eSRahul Tanwar 	unsigned long flags;
186*d058fd9eSRahul Tanwar 
187*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&div->lock, flags);
188*d058fd9eSRahul Tanwar 	lgm_set_clk_val(div->membase, div->reg, div->shift_gate,
189*d058fd9eSRahul Tanwar 			div->width_gate, enable);
190*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&div->lock, flags);
191*d058fd9eSRahul Tanwar 	return 0;
192*d058fd9eSRahul Tanwar }
193*d058fd9eSRahul Tanwar 
194*d058fd9eSRahul Tanwar static int lgm_clk_divider_enable(struct clk_hw *hw)
195*d058fd9eSRahul Tanwar {
196*d058fd9eSRahul Tanwar 	return lgm_clk_divider_enable_disable(hw, 1);
197*d058fd9eSRahul Tanwar }
198*d058fd9eSRahul Tanwar 
199*d058fd9eSRahul Tanwar static void lgm_clk_divider_disable(struct clk_hw *hw)
200*d058fd9eSRahul Tanwar {
201*d058fd9eSRahul Tanwar 	lgm_clk_divider_enable_disable(hw, 0);
202*d058fd9eSRahul Tanwar }
203*d058fd9eSRahul Tanwar 
204*d058fd9eSRahul Tanwar static const struct clk_ops lgm_clk_divider_ops = {
205*d058fd9eSRahul Tanwar 	.recalc_rate = lgm_clk_divider_recalc_rate,
206*d058fd9eSRahul Tanwar 	.round_rate = lgm_clk_divider_round_rate,
207*d058fd9eSRahul Tanwar 	.set_rate = lgm_clk_divider_set_rate,
208*d058fd9eSRahul Tanwar 	.enable = lgm_clk_divider_enable,
209*d058fd9eSRahul Tanwar 	.disable = lgm_clk_divider_disable,
210*d058fd9eSRahul Tanwar };
211*d058fd9eSRahul Tanwar 
212*d058fd9eSRahul Tanwar static struct clk_hw *
213*d058fd9eSRahul Tanwar lgm_clk_register_divider(struct lgm_clk_provider *ctx,
214*d058fd9eSRahul Tanwar 			 const struct lgm_clk_branch *list)
215*d058fd9eSRahul Tanwar {
216*d058fd9eSRahul Tanwar 	unsigned long flags, cflags = list->div_flags;
217*d058fd9eSRahul Tanwar 	struct device *dev = ctx->dev;
218*d058fd9eSRahul Tanwar 	struct lgm_clk_divider *div;
219*d058fd9eSRahul Tanwar 	struct clk_init_data init = {};
220*d058fd9eSRahul Tanwar 	u8 shift = list->div_shift;
221*d058fd9eSRahul Tanwar 	u8 width = list->div_width;
222*d058fd9eSRahul Tanwar 	u8 shift_gate = list->div_shift_gate;
223*d058fd9eSRahul Tanwar 	u8 width_gate = list->div_width_gate;
224*d058fd9eSRahul Tanwar 	u32 reg = list->div_off;
225*d058fd9eSRahul Tanwar 	struct clk_hw *hw;
226*d058fd9eSRahul Tanwar 	int ret;
227*d058fd9eSRahul Tanwar 
228*d058fd9eSRahul Tanwar 	div = devm_kzalloc(dev, sizeof(*div), GFP_KERNEL);
229*d058fd9eSRahul Tanwar 	if (!div)
230*d058fd9eSRahul Tanwar 		return ERR_PTR(-ENOMEM);
231*d058fd9eSRahul Tanwar 
232*d058fd9eSRahul Tanwar 	init.name = list->name;
233*d058fd9eSRahul Tanwar 	init.ops = &lgm_clk_divider_ops;
234*d058fd9eSRahul Tanwar 	init.flags = list->flags;
235*d058fd9eSRahul Tanwar 	init.parent_data = list->parent_data;
236*d058fd9eSRahul Tanwar 	init.num_parents = 1;
237*d058fd9eSRahul Tanwar 
238*d058fd9eSRahul Tanwar 	div->membase = ctx->membase;
239*d058fd9eSRahul Tanwar 	div->lock = ctx->lock;
240*d058fd9eSRahul Tanwar 	div->reg = reg;
241*d058fd9eSRahul Tanwar 	div->shift = shift;
242*d058fd9eSRahul Tanwar 	div->width = width;
243*d058fd9eSRahul Tanwar 	div->shift_gate	= shift_gate;
244*d058fd9eSRahul Tanwar 	div->width_gate	= width_gate;
245*d058fd9eSRahul Tanwar 	div->flags = cflags;
246*d058fd9eSRahul Tanwar 	div->table = list->div_table;
247*d058fd9eSRahul Tanwar 	div->hw.init = &init;
248*d058fd9eSRahul Tanwar 
249*d058fd9eSRahul Tanwar 	hw = &div->hw;
250*d058fd9eSRahul Tanwar 	ret = clk_hw_register(dev, hw);
251*d058fd9eSRahul Tanwar 	if (ret)
252*d058fd9eSRahul Tanwar 		return ERR_PTR(ret);
253*d058fd9eSRahul Tanwar 
254*d058fd9eSRahul Tanwar 	if (cflags & CLOCK_FLAG_VAL_INIT) {
255*d058fd9eSRahul Tanwar 		spin_lock_irqsave(&div->lock, flags);
256*d058fd9eSRahul Tanwar 		lgm_set_clk_val(div->membase, reg, shift, width, list->div_val);
257*d058fd9eSRahul Tanwar 		spin_unlock_irqrestore(&div->lock, flags);
258*d058fd9eSRahul Tanwar 	}
259*d058fd9eSRahul Tanwar 
260*d058fd9eSRahul Tanwar 	return hw;
261*d058fd9eSRahul Tanwar }
262*d058fd9eSRahul Tanwar 
263*d058fd9eSRahul Tanwar static struct clk_hw *
264*d058fd9eSRahul Tanwar lgm_clk_register_fixed_factor(struct lgm_clk_provider *ctx,
265*d058fd9eSRahul Tanwar 			      const struct lgm_clk_branch *list)
266*d058fd9eSRahul Tanwar {
267*d058fd9eSRahul Tanwar 	unsigned long flags;
268*d058fd9eSRahul Tanwar 	struct clk_hw *hw;
269*d058fd9eSRahul Tanwar 
270*d058fd9eSRahul Tanwar 	hw = clk_hw_register_fixed_factor(ctx->dev, list->name,
271*d058fd9eSRahul Tanwar 					  list->parent_data[0].name, list->flags,
272*d058fd9eSRahul Tanwar 					  list->mult, list->div);
273*d058fd9eSRahul Tanwar 	if (IS_ERR(hw))
274*d058fd9eSRahul Tanwar 		return ERR_CAST(hw);
275*d058fd9eSRahul Tanwar 
276*d058fd9eSRahul Tanwar 	if (list->div_flags & CLOCK_FLAG_VAL_INIT) {
277*d058fd9eSRahul Tanwar 		spin_lock_irqsave(&ctx->lock, flags);
278*d058fd9eSRahul Tanwar 		lgm_set_clk_val(ctx->membase, list->div_off, list->div_shift,
279*d058fd9eSRahul Tanwar 				list->div_width, list->div_val);
280*d058fd9eSRahul Tanwar 		spin_unlock_irqrestore(&ctx->lock, flags);
281*d058fd9eSRahul Tanwar 	}
282*d058fd9eSRahul Tanwar 
283*d058fd9eSRahul Tanwar 	return hw;
284*d058fd9eSRahul Tanwar }
285*d058fd9eSRahul Tanwar 
286*d058fd9eSRahul Tanwar static int lgm_clk_gate_enable(struct clk_hw *hw)
287*d058fd9eSRahul Tanwar {
288*d058fd9eSRahul Tanwar 	struct lgm_clk_gate *gate = to_lgm_clk_gate(hw);
289*d058fd9eSRahul Tanwar 	unsigned long flags;
290*d058fd9eSRahul Tanwar 	unsigned int reg;
291*d058fd9eSRahul Tanwar 
292*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&gate->lock, flags);
293*d058fd9eSRahul Tanwar 	reg = GATE_HW_REG_EN(gate->reg);
294*d058fd9eSRahul Tanwar 	lgm_set_clk_val(gate->membase, reg, gate->shift, 1, 1);
295*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&gate->lock, flags);
296*d058fd9eSRahul Tanwar 
297*d058fd9eSRahul Tanwar 	return 0;
298*d058fd9eSRahul Tanwar }
299*d058fd9eSRahul Tanwar 
300*d058fd9eSRahul Tanwar static void lgm_clk_gate_disable(struct clk_hw *hw)
301*d058fd9eSRahul Tanwar {
302*d058fd9eSRahul Tanwar 	struct lgm_clk_gate *gate = to_lgm_clk_gate(hw);
303*d058fd9eSRahul Tanwar 	unsigned long flags;
304*d058fd9eSRahul Tanwar 	unsigned int reg;
305*d058fd9eSRahul Tanwar 
306*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&gate->lock, flags);
307*d058fd9eSRahul Tanwar 	reg = GATE_HW_REG_DIS(gate->reg);
308*d058fd9eSRahul Tanwar 	lgm_set_clk_val(gate->membase, reg, gate->shift, 1, 1);
309*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&gate->lock, flags);
310*d058fd9eSRahul Tanwar }
311*d058fd9eSRahul Tanwar 
312*d058fd9eSRahul Tanwar static int lgm_clk_gate_is_enabled(struct clk_hw *hw)
313*d058fd9eSRahul Tanwar {
314*d058fd9eSRahul Tanwar 	struct lgm_clk_gate *gate = to_lgm_clk_gate(hw);
315*d058fd9eSRahul Tanwar 	unsigned int reg, ret;
316*d058fd9eSRahul Tanwar 	unsigned long flags;
317*d058fd9eSRahul Tanwar 
318*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&gate->lock, flags);
319*d058fd9eSRahul Tanwar 	reg = GATE_HW_REG_STAT(gate->reg);
320*d058fd9eSRahul Tanwar 	ret = lgm_get_clk_val(gate->membase, reg, gate->shift, 1);
321*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&gate->lock, flags);
322*d058fd9eSRahul Tanwar 
323*d058fd9eSRahul Tanwar 	return ret;
324*d058fd9eSRahul Tanwar }
325*d058fd9eSRahul Tanwar 
326*d058fd9eSRahul Tanwar static const struct clk_ops lgm_clk_gate_ops = {
327*d058fd9eSRahul Tanwar 	.enable = lgm_clk_gate_enable,
328*d058fd9eSRahul Tanwar 	.disable = lgm_clk_gate_disable,
329*d058fd9eSRahul Tanwar 	.is_enabled = lgm_clk_gate_is_enabled,
330*d058fd9eSRahul Tanwar };
331*d058fd9eSRahul Tanwar 
332*d058fd9eSRahul Tanwar static struct clk_hw *
333*d058fd9eSRahul Tanwar lgm_clk_register_gate(struct lgm_clk_provider *ctx,
334*d058fd9eSRahul Tanwar 		      const struct lgm_clk_branch *list)
335*d058fd9eSRahul Tanwar {
336*d058fd9eSRahul Tanwar 	unsigned long flags, cflags = list->gate_flags;
337*d058fd9eSRahul Tanwar 	const char *pname = list->parent_data[0].name;
338*d058fd9eSRahul Tanwar 	struct device *dev = ctx->dev;
339*d058fd9eSRahul Tanwar 	u8 shift = list->gate_shift;
340*d058fd9eSRahul Tanwar 	struct clk_init_data init = {};
341*d058fd9eSRahul Tanwar 	struct lgm_clk_gate *gate;
342*d058fd9eSRahul Tanwar 	u32 reg = list->gate_off;
343*d058fd9eSRahul Tanwar 	struct clk_hw *hw;
344*d058fd9eSRahul Tanwar 	int ret;
345*d058fd9eSRahul Tanwar 
346*d058fd9eSRahul Tanwar 	gate = devm_kzalloc(dev, sizeof(*gate), GFP_KERNEL);
347*d058fd9eSRahul Tanwar 	if (!gate)
348*d058fd9eSRahul Tanwar 		return ERR_PTR(-ENOMEM);
349*d058fd9eSRahul Tanwar 
350*d058fd9eSRahul Tanwar 	init.name = list->name;
351*d058fd9eSRahul Tanwar 	init.ops = &lgm_clk_gate_ops;
352*d058fd9eSRahul Tanwar 	init.flags = list->flags;
353*d058fd9eSRahul Tanwar 	init.parent_names = pname ? &pname : NULL;
354*d058fd9eSRahul Tanwar 	init.num_parents = pname ? 1 : 0;
355*d058fd9eSRahul Tanwar 
356*d058fd9eSRahul Tanwar 	gate->membase = ctx->membase;
357*d058fd9eSRahul Tanwar 	gate->lock = ctx->lock;
358*d058fd9eSRahul Tanwar 	gate->reg = reg;
359*d058fd9eSRahul Tanwar 	gate->shift = shift;
360*d058fd9eSRahul Tanwar 	gate->flags = cflags;
361*d058fd9eSRahul Tanwar 	gate->hw.init = &init;
362*d058fd9eSRahul Tanwar 
363*d058fd9eSRahul Tanwar 	hw = &gate->hw;
364*d058fd9eSRahul Tanwar 	ret = clk_hw_register(dev, hw);
365*d058fd9eSRahul Tanwar 	if (ret)
366*d058fd9eSRahul Tanwar 		return ERR_PTR(ret);
367*d058fd9eSRahul Tanwar 
368*d058fd9eSRahul Tanwar 	if (cflags & CLOCK_FLAG_VAL_INIT) {
369*d058fd9eSRahul Tanwar 		spin_lock_irqsave(&gate->lock, flags);
370*d058fd9eSRahul Tanwar 		lgm_set_clk_val(gate->membase, reg, shift, 1, list->gate_val);
371*d058fd9eSRahul Tanwar 		spin_unlock_irqrestore(&gate->lock, flags);
372*d058fd9eSRahul Tanwar 	}
373*d058fd9eSRahul Tanwar 
374*d058fd9eSRahul Tanwar 	return hw;
375*d058fd9eSRahul Tanwar }
376*d058fd9eSRahul Tanwar 
377*d058fd9eSRahul Tanwar int lgm_clk_register_branches(struct lgm_clk_provider *ctx,
378*d058fd9eSRahul Tanwar 			      const struct lgm_clk_branch *list,
379*d058fd9eSRahul Tanwar 			      unsigned int nr_clk)
380*d058fd9eSRahul Tanwar {
381*d058fd9eSRahul Tanwar 	struct clk_hw *hw;
382*d058fd9eSRahul Tanwar 	unsigned int idx;
383*d058fd9eSRahul Tanwar 
384*d058fd9eSRahul Tanwar 	for (idx = 0; idx < nr_clk; idx++, list++) {
385*d058fd9eSRahul Tanwar 		switch (list->type) {
386*d058fd9eSRahul Tanwar 		case CLK_TYPE_FIXED:
387*d058fd9eSRahul Tanwar 			hw = lgm_clk_register_fixed(ctx, list);
388*d058fd9eSRahul Tanwar 			break;
389*d058fd9eSRahul Tanwar 		case CLK_TYPE_MUX:
390*d058fd9eSRahul Tanwar 			hw = lgm_clk_register_mux(ctx, list);
391*d058fd9eSRahul Tanwar 			break;
392*d058fd9eSRahul Tanwar 		case CLK_TYPE_DIVIDER:
393*d058fd9eSRahul Tanwar 			hw = lgm_clk_register_divider(ctx, list);
394*d058fd9eSRahul Tanwar 			break;
395*d058fd9eSRahul Tanwar 		case CLK_TYPE_FIXED_FACTOR:
396*d058fd9eSRahul Tanwar 			hw = lgm_clk_register_fixed_factor(ctx, list);
397*d058fd9eSRahul Tanwar 			break;
398*d058fd9eSRahul Tanwar 		case CLK_TYPE_GATE:
399*d058fd9eSRahul Tanwar 			hw = lgm_clk_register_gate(ctx, list);
400*d058fd9eSRahul Tanwar 			break;
401*d058fd9eSRahul Tanwar 		default:
402*d058fd9eSRahul Tanwar 			dev_err(ctx->dev, "invalid clk type\n");
403*d058fd9eSRahul Tanwar 			return -EINVAL;
404*d058fd9eSRahul Tanwar 		}
405*d058fd9eSRahul Tanwar 
406*d058fd9eSRahul Tanwar 		if (IS_ERR(hw)) {
407*d058fd9eSRahul Tanwar 			dev_err(ctx->dev,
408*d058fd9eSRahul Tanwar 				"register clk: %s, type: %u failed!\n",
409*d058fd9eSRahul Tanwar 				list->name, list->type);
410*d058fd9eSRahul Tanwar 			return -EIO;
411*d058fd9eSRahul Tanwar 		}
412*d058fd9eSRahul Tanwar 		ctx->clk_data.hws[list->id] = hw;
413*d058fd9eSRahul Tanwar 	}
414*d058fd9eSRahul Tanwar 
415*d058fd9eSRahul Tanwar 	return 0;
416*d058fd9eSRahul Tanwar }
417*d058fd9eSRahul Tanwar 
418*d058fd9eSRahul Tanwar static unsigned long
419*d058fd9eSRahul Tanwar lgm_clk_ddiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
420*d058fd9eSRahul Tanwar {
421*d058fd9eSRahul Tanwar 	struct lgm_clk_ddiv *ddiv = to_lgm_clk_ddiv(hw);
422*d058fd9eSRahul Tanwar 	unsigned int div0, div1, exdiv;
423*d058fd9eSRahul Tanwar 	unsigned long flags;
424*d058fd9eSRahul Tanwar 	u64 prate;
425*d058fd9eSRahul Tanwar 
426*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&ddiv->lock, flags);
427*d058fd9eSRahul Tanwar 	div0 = lgm_get_clk_val(ddiv->membase, ddiv->reg,
428*d058fd9eSRahul Tanwar 			       ddiv->shift0, ddiv->width0) + 1;
429*d058fd9eSRahul Tanwar 	div1 = lgm_get_clk_val(ddiv->membase, ddiv->reg,
430*d058fd9eSRahul Tanwar 			       ddiv->shift1, ddiv->width1) + 1;
431*d058fd9eSRahul Tanwar 	exdiv = lgm_get_clk_val(ddiv->membase, ddiv->reg,
432*d058fd9eSRahul Tanwar 				ddiv->shift2, ddiv->width2);
433*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&ddiv->lock, flags);
434*d058fd9eSRahul Tanwar 
435*d058fd9eSRahul Tanwar 	prate = (u64)parent_rate;
436*d058fd9eSRahul Tanwar 	do_div(prate, div0);
437*d058fd9eSRahul Tanwar 	do_div(prate, div1);
438*d058fd9eSRahul Tanwar 
439*d058fd9eSRahul Tanwar 	if (exdiv) {
440*d058fd9eSRahul Tanwar 		do_div(prate, ddiv->div);
441*d058fd9eSRahul Tanwar 		prate *= ddiv->mult;
442*d058fd9eSRahul Tanwar 	}
443*d058fd9eSRahul Tanwar 
444*d058fd9eSRahul Tanwar 	return prate;
445*d058fd9eSRahul Tanwar }
446*d058fd9eSRahul Tanwar 
447*d058fd9eSRahul Tanwar static int lgm_clk_ddiv_enable(struct clk_hw *hw)
448*d058fd9eSRahul Tanwar {
449*d058fd9eSRahul Tanwar 	struct lgm_clk_ddiv *ddiv = to_lgm_clk_ddiv(hw);
450*d058fd9eSRahul Tanwar 	unsigned long flags;
451*d058fd9eSRahul Tanwar 
452*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&ddiv->lock, flags);
453*d058fd9eSRahul Tanwar 	lgm_set_clk_val(ddiv->membase, ddiv->reg, ddiv->shift_gate,
454*d058fd9eSRahul Tanwar 			ddiv->width_gate, 1);
455*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&ddiv->lock, flags);
456*d058fd9eSRahul Tanwar 	return 0;
457*d058fd9eSRahul Tanwar }
458*d058fd9eSRahul Tanwar 
459*d058fd9eSRahul Tanwar static void lgm_clk_ddiv_disable(struct clk_hw *hw)
460*d058fd9eSRahul Tanwar {
461*d058fd9eSRahul Tanwar 	struct lgm_clk_ddiv *ddiv = to_lgm_clk_ddiv(hw);
462*d058fd9eSRahul Tanwar 	unsigned long flags;
463*d058fd9eSRahul Tanwar 
464*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&ddiv->lock, flags);
465*d058fd9eSRahul Tanwar 	lgm_set_clk_val(ddiv->membase, ddiv->reg, ddiv->shift_gate,
466*d058fd9eSRahul Tanwar 			ddiv->width_gate, 0);
467*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&ddiv->lock, flags);
468*d058fd9eSRahul Tanwar }
469*d058fd9eSRahul Tanwar 
470*d058fd9eSRahul Tanwar static int
471*d058fd9eSRahul Tanwar lgm_clk_get_ddiv_val(u32 div, u32 *ddiv1, u32 *ddiv2)
472*d058fd9eSRahul Tanwar {
473*d058fd9eSRahul Tanwar 	u32 idx, temp;
474*d058fd9eSRahul Tanwar 
475*d058fd9eSRahul Tanwar 	*ddiv1 = 1;
476*d058fd9eSRahul Tanwar 	*ddiv2 = 1;
477*d058fd9eSRahul Tanwar 
478*d058fd9eSRahul Tanwar 	if (div > MAX_DIVIDER_VAL)
479*d058fd9eSRahul Tanwar 		div = MAX_DIVIDER_VAL;
480*d058fd9eSRahul Tanwar 
481*d058fd9eSRahul Tanwar 	if (div > 1) {
482*d058fd9eSRahul Tanwar 		for (idx = 2; idx <= MAX_DDIV_REG; idx++) {
483*d058fd9eSRahul Tanwar 			temp = DIV_ROUND_UP_ULL((u64)div, idx);
484*d058fd9eSRahul Tanwar 			if (div % idx == 0 && temp <= MAX_DDIV_REG)
485*d058fd9eSRahul Tanwar 				break;
486*d058fd9eSRahul Tanwar 		}
487*d058fd9eSRahul Tanwar 
488*d058fd9eSRahul Tanwar 		if (idx > MAX_DDIV_REG)
489*d058fd9eSRahul Tanwar 			return -EINVAL;
490*d058fd9eSRahul Tanwar 
491*d058fd9eSRahul Tanwar 		*ddiv1 = temp;
492*d058fd9eSRahul Tanwar 		*ddiv2 = idx;
493*d058fd9eSRahul Tanwar 	}
494*d058fd9eSRahul Tanwar 
495*d058fd9eSRahul Tanwar 	return 0;
496*d058fd9eSRahul Tanwar }
497*d058fd9eSRahul Tanwar 
498*d058fd9eSRahul Tanwar static int
499*d058fd9eSRahul Tanwar lgm_clk_ddiv_set_rate(struct clk_hw *hw, unsigned long rate,
500*d058fd9eSRahul Tanwar 		      unsigned long prate)
501*d058fd9eSRahul Tanwar {
502*d058fd9eSRahul Tanwar 	struct lgm_clk_ddiv *ddiv = to_lgm_clk_ddiv(hw);
503*d058fd9eSRahul Tanwar 	u32 div, ddiv1, ddiv2;
504*d058fd9eSRahul Tanwar 	unsigned long flags;
505*d058fd9eSRahul Tanwar 
506*d058fd9eSRahul Tanwar 	div = DIV_ROUND_CLOSEST_ULL((u64)prate, rate);
507*d058fd9eSRahul Tanwar 
508*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&ddiv->lock, flags);
509*d058fd9eSRahul Tanwar 	if (lgm_get_clk_val(ddiv->membase, ddiv->reg, ddiv->shift2, 1)) {
510*d058fd9eSRahul Tanwar 		div = DIV_ROUND_CLOSEST_ULL((u64)div, 5);
511*d058fd9eSRahul Tanwar 		div = div * 2;
512*d058fd9eSRahul Tanwar 	}
513*d058fd9eSRahul Tanwar 
514*d058fd9eSRahul Tanwar 	if (div <= 0) {
515*d058fd9eSRahul Tanwar 		spin_unlock_irqrestore(&ddiv->lock, flags);
516*d058fd9eSRahul Tanwar 		return -EINVAL;
517*d058fd9eSRahul Tanwar 	}
518*d058fd9eSRahul Tanwar 
519*d058fd9eSRahul Tanwar 	if (lgm_clk_get_ddiv_val(div, &ddiv1, &ddiv2)) {
520*d058fd9eSRahul Tanwar 		spin_unlock_irqrestore(&ddiv->lock, flags);
521*d058fd9eSRahul Tanwar 		return -EINVAL;
522*d058fd9eSRahul Tanwar 	}
523*d058fd9eSRahul Tanwar 
524*d058fd9eSRahul Tanwar 	lgm_set_clk_val(ddiv->membase, ddiv->reg, ddiv->shift0, ddiv->width0,
525*d058fd9eSRahul Tanwar 			ddiv1 - 1);
526*d058fd9eSRahul Tanwar 
527*d058fd9eSRahul Tanwar 	lgm_set_clk_val(ddiv->membase, ddiv->reg,  ddiv->shift1, ddiv->width1,
528*d058fd9eSRahul Tanwar 			ddiv2 - 1);
529*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&ddiv->lock, flags);
530*d058fd9eSRahul Tanwar 
531*d058fd9eSRahul Tanwar 	return 0;
532*d058fd9eSRahul Tanwar }
533*d058fd9eSRahul Tanwar 
534*d058fd9eSRahul Tanwar static long
535*d058fd9eSRahul Tanwar lgm_clk_ddiv_round_rate(struct clk_hw *hw, unsigned long rate,
536*d058fd9eSRahul Tanwar 			unsigned long *prate)
537*d058fd9eSRahul Tanwar {
538*d058fd9eSRahul Tanwar 	struct lgm_clk_ddiv *ddiv = to_lgm_clk_ddiv(hw);
539*d058fd9eSRahul Tanwar 	u32 div, ddiv1, ddiv2;
540*d058fd9eSRahul Tanwar 	unsigned long flags;
541*d058fd9eSRahul Tanwar 	u64 rate64 = rate;
542*d058fd9eSRahul Tanwar 
543*d058fd9eSRahul Tanwar 	div = DIV_ROUND_CLOSEST_ULL((u64)*prate, rate);
544*d058fd9eSRahul Tanwar 
545*d058fd9eSRahul Tanwar 	/* if predivide bit is enabled, modify div by factor of 2.5 */
546*d058fd9eSRahul Tanwar 	spin_lock_irqsave(&ddiv->lock, flags);
547*d058fd9eSRahul Tanwar 	if (lgm_get_clk_val(ddiv->membase, ddiv->reg, ddiv->shift2, 1)) {
548*d058fd9eSRahul Tanwar 		div = div * 2;
549*d058fd9eSRahul Tanwar 		div = DIV_ROUND_CLOSEST_ULL((u64)div, 5);
550*d058fd9eSRahul Tanwar 	}
551*d058fd9eSRahul Tanwar 
552*d058fd9eSRahul Tanwar 	if (div <= 0) {
553*d058fd9eSRahul Tanwar 		spin_unlock_irqrestore(&ddiv->lock, flags);
554*d058fd9eSRahul Tanwar 		return *prate;
555*d058fd9eSRahul Tanwar 	}
556*d058fd9eSRahul Tanwar 
557*d058fd9eSRahul Tanwar 	if (lgm_clk_get_ddiv_val(div, &ddiv1, &ddiv2) != 0) {
558*d058fd9eSRahul Tanwar 		if (lgm_clk_get_ddiv_val(div + 1, &ddiv1, &ddiv2) != 0) {
559*d058fd9eSRahul Tanwar 			spin_unlock_irqrestore(&ddiv->lock, flags);
560*d058fd9eSRahul Tanwar 			return -EINVAL;
561*d058fd9eSRahul Tanwar 		}
562*d058fd9eSRahul Tanwar 	}
563*d058fd9eSRahul Tanwar 
564*d058fd9eSRahul Tanwar 	rate64 = *prate;
565*d058fd9eSRahul Tanwar 	do_div(rate64, ddiv1);
566*d058fd9eSRahul Tanwar 	do_div(rate64, ddiv2);
567*d058fd9eSRahul Tanwar 
568*d058fd9eSRahul Tanwar 	/* if predivide bit is enabled, modify rounded rate by factor of 2.5 */
569*d058fd9eSRahul Tanwar 	if (lgm_get_clk_val(ddiv->membase, ddiv->reg, ddiv->shift2, 1)) {
570*d058fd9eSRahul Tanwar 		rate64 = rate64 * 2;
571*d058fd9eSRahul Tanwar 		rate64 = DIV_ROUND_CLOSEST_ULL(rate64, 5);
572*d058fd9eSRahul Tanwar 	}
573*d058fd9eSRahul Tanwar 	spin_unlock_irqrestore(&ddiv->lock, flags);
574*d058fd9eSRahul Tanwar 
575*d058fd9eSRahul Tanwar 	return rate64;
576*d058fd9eSRahul Tanwar }
577*d058fd9eSRahul Tanwar 
578*d058fd9eSRahul Tanwar static const struct clk_ops lgm_clk_ddiv_ops = {
579*d058fd9eSRahul Tanwar 	.recalc_rate = lgm_clk_ddiv_recalc_rate,
580*d058fd9eSRahul Tanwar 	.enable	= lgm_clk_ddiv_enable,
581*d058fd9eSRahul Tanwar 	.disable = lgm_clk_ddiv_disable,
582*d058fd9eSRahul Tanwar 	.set_rate = lgm_clk_ddiv_set_rate,
583*d058fd9eSRahul Tanwar 	.round_rate = lgm_clk_ddiv_round_rate,
584*d058fd9eSRahul Tanwar };
585*d058fd9eSRahul Tanwar 
586*d058fd9eSRahul Tanwar int lgm_clk_register_ddiv(struct lgm_clk_provider *ctx,
587*d058fd9eSRahul Tanwar 			  const struct lgm_clk_ddiv_data *list,
588*d058fd9eSRahul Tanwar 			  unsigned int nr_clk)
589*d058fd9eSRahul Tanwar {
590*d058fd9eSRahul Tanwar 	struct device *dev = ctx->dev;
591*d058fd9eSRahul Tanwar 	struct clk_init_data init = {};
592*d058fd9eSRahul Tanwar 	struct lgm_clk_ddiv *ddiv;
593*d058fd9eSRahul Tanwar 	struct clk_hw *hw;
594*d058fd9eSRahul Tanwar 	unsigned int idx;
595*d058fd9eSRahul Tanwar 	int ret;
596*d058fd9eSRahul Tanwar 
597*d058fd9eSRahul Tanwar 	for (idx = 0; idx < nr_clk; idx++, list++) {
598*d058fd9eSRahul Tanwar 		ddiv = NULL;
599*d058fd9eSRahul Tanwar 		ddiv = devm_kzalloc(dev, sizeof(*ddiv), GFP_KERNEL);
600*d058fd9eSRahul Tanwar 		if (!ddiv)
601*d058fd9eSRahul Tanwar 			return -ENOMEM;
602*d058fd9eSRahul Tanwar 
603*d058fd9eSRahul Tanwar 		memset(&init, 0, sizeof(init));
604*d058fd9eSRahul Tanwar 		init.name = list->name;
605*d058fd9eSRahul Tanwar 		init.ops = &lgm_clk_ddiv_ops;
606*d058fd9eSRahul Tanwar 		init.flags = list->flags;
607*d058fd9eSRahul Tanwar 		init.parent_data = list->parent_data;
608*d058fd9eSRahul Tanwar 		init.num_parents = 1;
609*d058fd9eSRahul Tanwar 
610*d058fd9eSRahul Tanwar 		ddiv->membase = ctx->membase;
611*d058fd9eSRahul Tanwar 		ddiv->lock = ctx->lock;
612*d058fd9eSRahul Tanwar 		ddiv->reg = list->reg;
613*d058fd9eSRahul Tanwar 		ddiv->shift0 = list->shift0;
614*d058fd9eSRahul Tanwar 		ddiv->width0 = list->width0;
615*d058fd9eSRahul Tanwar 		ddiv->shift1 = list->shift1;
616*d058fd9eSRahul Tanwar 		ddiv->width1 = list->width1;
617*d058fd9eSRahul Tanwar 		ddiv->shift_gate = list->shift_gate;
618*d058fd9eSRahul Tanwar 		ddiv->width_gate = list->width_gate;
619*d058fd9eSRahul Tanwar 		ddiv->shift2 = list->ex_shift;
620*d058fd9eSRahul Tanwar 		ddiv->width2 = list->ex_width;
621*d058fd9eSRahul Tanwar 		ddiv->flags = list->div_flags;
622*d058fd9eSRahul Tanwar 		ddiv->mult = 2;
623*d058fd9eSRahul Tanwar 		ddiv->div = 5;
624*d058fd9eSRahul Tanwar 		ddiv->hw.init = &init;
625*d058fd9eSRahul Tanwar 
626*d058fd9eSRahul Tanwar 		hw = &ddiv->hw;
627*d058fd9eSRahul Tanwar 		ret = clk_hw_register(dev, hw);
628*d058fd9eSRahul Tanwar 		if (ret) {
629*d058fd9eSRahul Tanwar 			dev_err(dev, "register clk: %s failed!\n", list->name);
630*d058fd9eSRahul Tanwar 			return ret;
631*d058fd9eSRahul Tanwar 		}
632*d058fd9eSRahul Tanwar 		ctx->clk_data.hws[list->id] = hw;
633*d058fd9eSRahul Tanwar 	}
634*d058fd9eSRahul Tanwar 
635*d058fd9eSRahul Tanwar 	return 0;
636*d058fd9eSRahul Tanwar }
637