0001
0002
0003
0004
0005
0006
0007
0008
0009 #define pr_fmt(fmt) "clk-frac-synth: " fmt
0010
0011 #include <linux/clk-provider.h>
0012 #include <linux/slab.h>
0013 #include <linux/io.h>
0014 #include <linux/err.h>
0015 #include "clk.h"
0016
0017 #define DIV_FACTOR_MASK 0x1FFFF
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 #define to_clk_frac(_hw) container_of(_hw, struct clk_frac, hw)
0040
0041 static unsigned long frac_calc_rate(struct clk_hw *hw, unsigned long prate,
0042 int index)
0043 {
0044 struct clk_frac *frac = to_clk_frac(hw);
0045 struct frac_rate_tbl *rtbl = frac->rtbl;
0046
0047 prate /= 10000;
0048 prate <<= 14;
0049 prate /= (2 * rtbl[index].div);
0050 prate *= 10000;
0051
0052 return prate;
0053 }
0054
0055 static long clk_frac_round_rate(struct clk_hw *hw, unsigned long drate,
0056 unsigned long *prate)
0057 {
0058 struct clk_frac *frac = to_clk_frac(hw);
0059 int unused;
0060
0061 return clk_round_rate_index(hw, drate, *prate, frac_calc_rate,
0062 frac->rtbl_cnt, &unused);
0063 }
0064
0065 static unsigned long clk_frac_recalc_rate(struct clk_hw *hw,
0066 unsigned long parent_rate)
0067 {
0068 struct clk_frac *frac = to_clk_frac(hw);
0069 unsigned long flags = 0;
0070 unsigned int div = 1, val;
0071
0072 if (frac->lock)
0073 spin_lock_irqsave(frac->lock, flags);
0074
0075 val = readl_relaxed(frac->reg);
0076
0077 if (frac->lock)
0078 spin_unlock_irqrestore(frac->lock, flags);
0079
0080 div = val & DIV_FACTOR_MASK;
0081
0082 if (!div)
0083 return 0;
0084
0085 parent_rate = parent_rate / 10000;
0086
0087 parent_rate = (parent_rate << 14) / (2 * div);
0088 return parent_rate * 10000;
0089 }
0090
0091
0092 static int clk_frac_set_rate(struct clk_hw *hw, unsigned long drate,
0093 unsigned long prate)
0094 {
0095 struct clk_frac *frac = to_clk_frac(hw);
0096 struct frac_rate_tbl *rtbl = frac->rtbl;
0097 unsigned long flags = 0, val;
0098 int i;
0099
0100 clk_round_rate_index(hw, drate, prate, frac_calc_rate, frac->rtbl_cnt,
0101 &i);
0102
0103 if (frac->lock)
0104 spin_lock_irqsave(frac->lock, flags);
0105
0106 val = readl_relaxed(frac->reg) & ~DIV_FACTOR_MASK;
0107 val |= rtbl[i].div & DIV_FACTOR_MASK;
0108 writel_relaxed(val, frac->reg);
0109
0110 if (frac->lock)
0111 spin_unlock_irqrestore(frac->lock, flags);
0112
0113 return 0;
0114 }
0115
0116 static const struct clk_ops clk_frac_ops = {
0117 .recalc_rate = clk_frac_recalc_rate,
0118 .round_rate = clk_frac_round_rate,
0119 .set_rate = clk_frac_set_rate,
0120 };
0121
0122 struct clk *clk_register_frac(const char *name, const char *parent_name,
0123 unsigned long flags, void __iomem *reg,
0124 struct frac_rate_tbl *rtbl, u8 rtbl_cnt, spinlock_t *lock)
0125 {
0126 struct clk_init_data init;
0127 struct clk_frac *frac;
0128 struct clk *clk;
0129
0130 if (!name || !parent_name || !reg || !rtbl || !rtbl_cnt) {
0131 pr_err("Invalid arguments passed\n");
0132 return ERR_PTR(-EINVAL);
0133 }
0134
0135 frac = kzalloc(sizeof(*frac), GFP_KERNEL);
0136 if (!frac)
0137 return ERR_PTR(-ENOMEM);
0138
0139
0140 frac->reg = reg;
0141 frac->rtbl = rtbl;
0142 frac->rtbl_cnt = rtbl_cnt;
0143 frac->lock = lock;
0144 frac->hw.init = &init;
0145
0146 init.name = name;
0147 init.ops = &clk_frac_ops;
0148 init.flags = flags;
0149 init.parent_names = &parent_name;
0150 init.num_parents = 1;
0151
0152 clk = clk_register(NULL, &frac->hw);
0153 if (!IS_ERR_OR_NULL(clk))
0154 return clk;
0155
0156 pr_err("clk register failed\n");
0157 kfree(frac);
0158
0159 return NULL;
0160 }