0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/clk.h>
0009 #include <linux/clk-provider.h>
0010 #include <linux/regmap.h>
0011
0012 #include "mux.h"
0013
0014 u8 sprd_mux_helper_get_parent(const struct sprd_clk_common *common,
0015 const struct sprd_mux_ssel *mux)
0016 {
0017 unsigned int reg;
0018 u8 parent;
0019 int num_parents;
0020 int i;
0021
0022 regmap_read(common->regmap, common->reg, ®);
0023 parent = reg >> mux->shift;
0024 parent &= (1 << mux->width) - 1;
0025
0026 if (!mux->table)
0027 return parent;
0028
0029 num_parents = clk_hw_get_num_parents(&common->hw);
0030
0031 for (i = 0; i < num_parents - 1; i++)
0032 if (parent >= mux->table[i] && parent < mux->table[i + 1])
0033 return i;
0034
0035 return num_parents - 1;
0036 }
0037 EXPORT_SYMBOL_GPL(sprd_mux_helper_get_parent);
0038
0039 static u8 sprd_mux_get_parent(struct clk_hw *hw)
0040 {
0041 struct sprd_mux *cm = hw_to_sprd_mux(hw);
0042
0043 return sprd_mux_helper_get_parent(&cm->common, &cm->mux);
0044 }
0045
0046 int sprd_mux_helper_set_parent(const struct sprd_clk_common *common,
0047 const struct sprd_mux_ssel *mux,
0048 u8 index)
0049 {
0050 unsigned int reg;
0051
0052 if (mux->table)
0053 index = mux->table[index];
0054
0055 regmap_read(common->regmap, common->reg, ®);
0056 reg &= ~GENMASK(mux->width + mux->shift - 1, mux->shift);
0057 regmap_write(common->regmap, common->reg,
0058 reg | (index << mux->shift));
0059
0060 return 0;
0061 }
0062 EXPORT_SYMBOL_GPL(sprd_mux_helper_set_parent);
0063
0064 static int sprd_mux_set_parent(struct clk_hw *hw, u8 index)
0065 {
0066 struct sprd_mux *cm = hw_to_sprd_mux(hw);
0067
0068 return sprd_mux_helper_set_parent(&cm->common, &cm->mux, index);
0069 }
0070
0071 const struct clk_ops sprd_mux_ops = {
0072 .get_parent = sprd_mux_get_parent,
0073 .set_parent = sprd_mux_set_parent,
0074 .determine_rate = __clk_mux_determine_rate,
0075 };
0076 EXPORT_SYMBOL_GPL(sprd_mux_ops);