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