1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2012 Freescale Semiconductor, Inc.
6 #include <linux/clk-provider.h>
9 #include <linux/slab.h>
13 * struct clk_frac - mxs fractional divider clock
14 * @hw: clk_hw for the fractional divider clock
15 * @reg: register address
16 * @shift: the divider bit shift
17 * @width: the divider bit width
18 * @busy: busy bit shift
20 * The clock is an adjustable fractional divider with a busy bit to wait
21 * when the divider is adjusted.
31 #define to_clk_frac(_hw) container_of(_hw, struct clk_frac, hw)
33 static unsigned long clk_frac_recalc_rate(struct clk_hw
*hw
,
34 unsigned long parent_rate
)
36 struct clk_frac
*frac
= to_clk_frac(hw
);
40 div
= readl_relaxed(frac
->reg
) >> frac
->shift
;
41 div
&= (1 << frac
->width
) - 1;
43 tmp_rate
= (u64
)parent_rate
* div
;
44 return tmp_rate
>> frac
->width
;
47 static long clk_frac_round_rate(struct clk_hw
*hw
, unsigned long rate
,
50 struct clk_frac
*frac
= to_clk_frac(hw
);
51 unsigned long parent_rate
= *prate
;
53 u64 tmp
, tmp_rate
, result
;
55 if (rate
> parent_rate
)
60 do_div(tmp
, parent_rate
);
66 tmp_rate
= (u64
)parent_rate
* div
;
67 result
= tmp_rate
>> frac
->width
;
68 if ((result
<< frac
->width
) < tmp_rate
)
73 static int clk_frac_set_rate(struct clk_hw
*hw
, unsigned long rate
,
74 unsigned long parent_rate
)
76 struct clk_frac
*frac
= to_clk_frac(hw
);
81 if (rate
> parent_rate
)
86 do_div(tmp
, parent_rate
);
92 spin_lock_irqsave(&mxs_lock
, flags
);
94 val
= readl_relaxed(frac
->reg
);
95 val
&= ~(((1 << frac
->width
) - 1) << frac
->shift
);
96 val
|= div
<< frac
->shift
;
97 writel_relaxed(val
, frac
->reg
);
99 spin_unlock_irqrestore(&mxs_lock
, flags
);
101 return mxs_clk_wait(frac
->reg
, frac
->busy
);
104 static const struct clk_ops clk_frac_ops
= {
105 .recalc_rate
= clk_frac_recalc_rate
,
106 .round_rate
= clk_frac_round_rate
,
107 .set_rate
= clk_frac_set_rate
,
110 struct clk
*mxs_clk_frac(const char *name
, const char *parent_name
,
111 void __iomem
*reg
, u8 shift
, u8 width
, u8 busy
)
113 struct clk_frac
*frac
;
115 struct clk_init_data init
;
117 frac
= kzalloc(sizeof(*frac
), GFP_KERNEL
);
119 return ERR_PTR(-ENOMEM
);
122 init
.ops
= &clk_frac_ops
;
123 init
.flags
= CLK_SET_RATE_PARENT
;
124 init
.parent_names
= (parent_name
? &parent_name
: NULL
);
125 init
.num_parents
= (parent_name
? 1 : 0);
131 frac
->hw
.init
= &init
;
133 clk
= clk_register(NULL
, &frac
->hw
);