2 * Copyright 2012 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.h>
13 #include <linux/clk-provider.h>
14 #include <linux/err.h>
16 #include <linux/slab.h>
20 * struct clk_frac - mxs fractional divider clock
21 * @hw: clk_hw for the fractional divider clock
22 * @reg: register address
23 * @shift: the divider bit shift
24 * @width: the divider bit width
25 * @busy: busy bit shift
27 * The clock is an adjustable fractional divider with a busy bit to wait
28 * when the divider is adjusted.
38 #define to_clk_frac(_hw) container_of(_hw, struct clk_frac, hw)
40 static unsigned long clk_frac_recalc_rate(struct clk_hw *hw,
41 unsigned long parent_rate)
43 struct clk_frac *frac = to_clk_frac(hw);
46 div = readl_relaxed(frac->reg) >> frac->shift;
47 div &= (1 << frac->width) - 1;
49 return (parent_rate >> frac->width) * div;
52 static long clk_frac_round_rate(struct clk_hw *hw, unsigned long rate,
55 struct clk_frac *frac = to_clk_frac(hw);
56 unsigned long parent_rate = *prate;
60 if (rate > parent_rate)
65 do_div(tmp, parent_rate);
71 return (parent_rate >> frac->width) * div;
74 static int clk_frac_set_rate(struct clk_hw *hw, unsigned long rate,
75 unsigned long parent_rate)
77 struct clk_frac *frac = to_clk_frac(hw);
82 if (rate > parent_rate)
87 do_div(tmp, parent_rate);
93 spin_lock_irqsave(&mxs_lock, flags);
95 val = readl_relaxed(frac->reg);
96 val &= ~(((1 << frac->width) - 1) << frac->shift);
97 val |= div << frac->shift;
98 writel_relaxed(val, frac->reg);
100 spin_unlock_irqrestore(&mxs_lock, flags);
102 return mxs_clk_wait(frac->reg, frac->busy);
105 static struct clk_ops clk_frac_ops = {
106 .recalc_rate = clk_frac_recalc_rate,
107 .round_rate = clk_frac_round_rate,
108 .set_rate = clk_frac_set_rate,
111 struct clk *mxs_clk_frac(const char *name, const char *parent_name,
112 void __iomem *reg, u8 shift, u8 width, u8 busy)
114 struct clk_frac *frac;
116 struct clk_init_data init;
118 frac = kzalloc(sizeof(*frac), GFP_KERNEL);
120 return ERR_PTR(-ENOMEM);
123 init.ops = &clk_frac_ops;
124 init.flags = CLK_SET_RATE_PARENT;
125 init.parent_names = (parent_name ? &parent_name: NULL);
126 init.num_parents = (parent_name ? 1 : 0);
132 frac->hw.init = &init;
134 clk = clk_register(NULL, &frac->hw);