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