1 /*
2 * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
3 *
4 * Base on code in drivers/clk/clk-mux.c.
5 * See clk-mux.c for further copyright information.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18 #include "clk-regmap.h"
19
20 #define to_clk_regmap_mux(_hw) container_of(_hw, struct clk_regmap_mux, hw)
21
clk_regmap_mux_get_parent(struct clk_hw * hw)22 static u8 clk_regmap_mux_get_parent(struct clk_hw *hw)
23 {
24 struct clk_regmap_mux *mux = to_clk_regmap_mux(hw);
25 u8 index;
26 u32 val;
27
28 regmap_read(mux->regmap, mux->reg, &val);
29
30 index = val >> mux->shift;
31 index &= mux->mask;
32
33 return index;
34 }
35
clk_regmap_mux_set_parent(struct clk_hw * hw,u8 index)36 static int clk_regmap_mux_set_parent(struct clk_hw *hw, u8 index)
37 {
38 struct clk_regmap_mux *mux = to_clk_regmap_mux(hw);
39
40 return regmap_write(mux->regmap, mux->reg, (index << mux->shift) |
41 (mux->mask << (mux->shift + 16)));
42 }
43
44 const struct clk_ops clk_regmap_mux_ops = {
45 .set_parent = clk_regmap_mux_set_parent,
46 .get_parent = clk_regmap_mux_get_parent,
47 .determine_rate = __clk_mux_determine_rate,
48 };
49 EXPORT_SYMBOL_GPL(clk_regmap_mux_ops);
50
51 struct clk *
devm_clk_regmap_register_mux(struct device * dev,const char * name,const char * const * parent_names,u8 num_parents,struct regmap * regmap,u32 reg,u8 shift,u8 width,unsigned long flags)52 devm_clk_regmap_register_mux(struct device *dev, const char *name,
53 const char * const *parent_names, u8 num_parents,
54 struct regmap *regmap, u32 reg, u8 shift, u8 width,
55 unsigned long flags)
56 {
57 struct clk_regmap_mux *mux;
58 struct clk_init_data init = {};
59
60 mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
61 if (!mux)
62 return ERR_PTR(-ENOMEM);
63
64 init.name = name;
65 init.ops = &clk_regmap_mux_ops;
66 init.flags = flags;
67 init.parent_names = parent_names;
68 init.num_parents = num_parents;
69
70 mux->dev = dev;
71 mux->regmap = regmap;
72 mux->reg = reg;
73 mux->shift = shift;
74 mux->mask = BIT(width) - 1;
75 mux->hw.init = &init;
76
77 return devm_clk_register(dev, &mux->hw);
78 }
79 EXPORT_SYMBOL_GPL(devm_clk_regmap_register_mux);
80
81 MODULE_LICENSE("GPL");
82