hc
2023-03-21 4b55d97acc464242bcd6a8ae77b8ff37c22dec58
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
82
/*
 * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
 *
 * Base on code in drivers/clk/clk-gate.c.
 * See clk-gate.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_gate(_hw)    container_of(_hw, struct clk_regmap_gate, hw)
 
static int clk_regmap_gate_prepare(struct clk_hw *hw)
{
   struct clk_regmap_gate *gate = to_clk_regmap_gate(hw);
 
   return regmap_write(gate->regmap, gate->reg,
               0 | BIT(gate->shift + 16));
}
 
static void clk_regmap_gate_unprepare(struct clk_hw *hw)
{
   struct clk_regmap_gate *gate = to_clk_regmap_gate(hw);
 
   regmap_write(gate->regmap, gate->reg,
            BIT(gate->shift) | BIT(gate->shift + 16));
}
 
static int clk_regmap_gate_is_prepared(struct clk_hw *hw)
{
   struct clk_regmap_gate *gate = to_clk_regmap_gate(hw);
   u32 val;
 
   regmap_read(gate->regmap, gate->reg, &val);
 
   return !(val & BIT(gate->shift));
}
 
const struct clk_ops clk_regmap_gate_ops = {
   .prepare = clk_regmap_gate_prepare,
   .unprepare = clk_regmap_gate_unprepare,
   .is_prepared = clk_regmap_gate_is_prepared,
};
EXPORT_SYMBOL_GPL(clk_regmap_gate_ops);
 
struct clk *
devm_clk_regmap_register_gate(struct device *dev, const char *name,
                 const char *parent_name,
                 struct regmap *regmap, u32 reg, u8 shift,
                 unsigned long flags)
{
   struct clk_regmap_gate *gate;
   struct clk_init_data init = {};
 
   gate = devm_kzalloc(dev, sizeof(*gate), GFP_KERNEL);
   if (!gate)
       return ERR_PTR(-ENOMEM);
 
   init.name = name;
   init.ops = &clk_regmap_gate_ops;
   init.flags = flags;
   init.parent_names = (parent_name ? &parent_name : NULL);
   init.num_parents = (parent_name ? 1 : 0);
 
   gate->dev = dev;
   gate->regmap = regmap;
   gate->reg = reg;
   gate->shift = shift;
   gate->hw.init = &init;
 
   return devm_clk_register(dev, &gate->hw);
}
EXPORT_SYMBOL_GPL(devm_clk_regmap_register_gate);