2 * Copyright (C) 2013 Freescale Semiconductor, Inc.
4 * The code contained herein is licensed under the GNU General Public
5 * License. You may obtain a copy of the GNU General Public License
6 * Version 2 or later at the following locations:
8 * http://www.opensource.org/licenses/gpl-license.html
9 * http://www.gnu.org/copyleft/gpl.html
12 #include <linux/clk-provider.h>
13 #include <linux/err.h>
15 #include <linux/slab.h>
18 #define div_mask(d) ((1 << (d->width)) - 1)
21 * struct clk_fixup_div - imx integer fixup divider clock
22 * @divider: the parent class
23 * @ops: pointer to clk_ops of parent class
24 * @fixup: a hook to fixup the write value
26 * The imx fixup divider clock is a subclass of basic clk_divider
27 * with an addtional fixup hook.
29 struct clk_fixup_div
{
30 struct clk_divider divider
;
31 const struct clk_ops
*ops
;
32 void (*fixup
)(u32
*val
);
35 static inline struct clk_fixup_div
*to_clk_fixup_div(struct clk_hw
*hw
)
37 struct clk_divider
*divider
= to_clk_divider(hw
);
39 return container_of(divider
, struct clk_fixup_div
, divider
);
42 static unsigned long clk_fixup_div_recalc_rate(struct clk_hw
*hw
,
43 unsigned long parent_rate
)
45 struct clk_fixup_div
*fixup_div
= to_clk_fixup_div(hw
);
47 return fixup_div
->ops
->recalc_rate(&fixup_div
->divider
.hw
, parent_rate
);
50 static long clk_fixup_div_round_rate(struct clk_hw
*hw
, unsigned long rate
,
53 struct clk_fixup_div
*fixup_div
= to_clk_fixup_div(hw
);
55 return fixup_div
->ops
->round_rate(&fixup_div
->divider
.hw
, rate
, prate
);
58 static int clk_fixup_div_set_rate(struct clk_hw
*hw
, unsigned long rate
,
59 unsigned long parent_rate
)
61 struct clk_fixup_div
*fixup_div
= to_clk_fixup_div(hw
);
62 struct clk_divider
*div
= to_clk_divider(hw
);
63 unsigned int divider
, value
;
64 unsigned long flags
= 0;
67 divider
= parent_rate
/ rate
;
69 /* Zero based divider */
72 if (value
> div_mask(div
))
73 value
= div_mask(div
);
75 spin_lock_irqsave(div
->lock
, flags
);
77 val
= readl(div
->reg
);
78 val
&= ~(div_mask(div
) << div
->shift
);
79 val
|= value
<< div
->shift
;
80 fixup_div
->fixup(&val
);
81 writel(val
, div
->reg
);
83 spin_unlock_irqrestore(div
->lock
, flags
);
88 static const struct clk_ops clk_fixup_div_ops
= {
89 .recalc_rate
= clk_fixup_div_recalc_rate
,
90 .round_rate
= clk_fixup_div_round_rate
,
91 .set_rate
= clk_fixup_div_set_rate
,
94 struct clk
*imx_clk_fixup_divider(const char *name
, const char *parent
,
95 void __iomem
*reg
, u8 shift
, u8 width
,
96 void (*fixup
)(u32
*val
))
98 struct clk_fixup_div
*fixup_div
;
100 struct clk_init_data init
;
103 return ERR_PTR(-EINVAL
);
105 fixup_div
= kzalloc(sizeof(*fixup_div
), GFP_KERNEL
);
107 return ERR_PTR(-ENOMEM
);
110 init
.ops
= &clk_fixup_div_ops
;
111 init
.flags
= CLK_SET_RATE_PARENT
;
112 init
.parent_names
= parent
? &parent
: NULL
;
113 init
.num_parents
= parent
? 1 : 0;
115 fixup_div
->divider
.reg
= reg
;
116 fixup_div
->divider
.shift
= shift
;
117 fixup_div
->divider
.width
= width
;
118 fixup_div
->divider
.lock
= &imx_ccm_lock
;
119 fixup_div
->divider
.hw
.init
= &init
;
120 fixup_div
->ops
= &clk_divider_ops
;
121 fixup_div
->fixup
= fixup
;
123 clk
= clk_register(NULL
, &fixup_div
->divider
.hw
);