1 /*
2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #include <linux/kernel.h>
15 #include <linux/bitops.h>
16 #include <linux/regmap.h>
17 #include <linux/export.h>
18
19 #include "clk-regmap-mux.h"
20
to_clk_regmap_mux(struct clk_hw * hw)21 static inline struct clk_regmap_mux *to_clk_regmap_mux(struct clk_hw *hw)
22 {
23 return container_of(to_clk_regmap(hw), struct clk_regmap_mux, clkr);
24 }
25
mux_get_parent(struct clk_hw * hw)26 static u8 mux_get_parent(struct clk_hw *hw)
27 {
28 struct clk_regmap_mux *mux = to_clk_regmap_mux(hw);
29 struct clk_regmap *clkr = to_clk_regmap(hw);
30 unsigned int mask = GENMASK(mux->width - 1, 0);
31 unsigned int val;
32
33 regmap_read(clkr->regmap, mux->reg, &val);
34
35 val >>= mux->shift;
36 val &= mask;
37
38 if (mux->parent_map)
39 return qcom_find_src_index(hw, mux->parent_map, val);
40
41 return val;
42 }
43
mux_set_parent(struct clk_hw * hw,u8 index)44 static int mux_set_parent(struct clk_hw *hw, u8 index)
45 {
46 struct clk_regmap_mux *mux = to_clk_regmap_mux(hw);
47 struct clk_regmap *clkr = to_clk_regmap(hw);
48 unsigned int mask = GENMASK(mux->width + mux->shift - 1, mux->shift);
49 unsigned int val;
50
51 if (mux->parent_map)
52 index = mux->parent_map[index].cfg;
53
54 val = index;
55 val <<= mux->shift;
56
57 return regmap_update_bits(clkr->regmap, mux->reg, mask, val);
58 }
59
60 const struct clk_ops clk_regmap_mux_closest_ops = {
61 .get_parent = mux_get_parent,
62 .set_parent = mux_set_parent,
63 .determine_rate = __clk_mux_determine_rate_closest,
64 };
65 EXPORT_SYMBOL_GPL(clk_regmap_mux_closest_ops);
66