Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / clk / uniphier / clk-uniphier-cpugear.c
bloba2f01a4da1276903b40098ec68213982ebd2b5ec
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2016 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5 */
7 #include <linux/clk-provider.h>
8 #include <linux/device.h>
9 #include <linux/regmap.h>
11 #include "clk-uniphier.h"
13 #define UNIPHIER_CLK_CPUGEAR_STAT 0 /* status */
14 #define UNIPHIER_CLK_CPUGEAR_SET 4 /* set */
15 #define UNIPHIER_CLK_CPUGEAR_UPD 8 /* update */
16 #define UNIPHIER_CLK_CPUGEAR_UPD_BIT BIT(0)
18 struct uniphier_clk_cpugear {
19 struct clk_hw hw;
20 struct regmap *regmap;
21 unsigned int regbase;
22 unsigned int mask;
25 #define to_uniphier_clk_cpugear(_hw) \
26 container_of(_hw, struct uniphier_clk_cpugear, hw)
28 static int uniphier_clk_cpugear_set_parent(struct clk_hw *hw, u8 index)
30 struct uniphier_clk_cpugear *gear = to_uniphier_clk_cpugear(hw);
31 int ret;
32 unsigned int val;
34 ret = regmap_write_bits(gear->regmap,
35 gear->regbase + UNIPHIER_CLK_CPUGEAR_SET,
36 gear->mask, index);
37 if (ret)
38 return ret;
40 ret = regmap_write_bits(gear->regmap,
41 gear->regbase + UNIPHIER_CLK_CPUGEAR_UPD,
42 UNIPHIER_CLK_CPUGEAR_UPD_BIT,
43 UNIPHIER_CLK_CPUGEAR_UPD_BIT);
44 if (ret)
45 return ret;
47 return regmap_read_poll_timeout(gear->regmap,
48 gear->regbase + UNIPHIER_CLK_CPUGEAR_UPD,
49 val, !(val & UNIPHIER_CLK_CPUGEAR_UPD_BIT),
50 0, 1);
53 static u8 uniphier_clk_cpugear_get_parent(struct clk_hw *hw)
55 struct uniphier_clk_cpugear *gear = to_uniphier_clk_cpugear(hw);
56 int num_parents = clk_hw_get_num_parents(hw);
57 int ret;
58 unsigned int val;
60 ret = regmap_read(gear->regmap,
61 gear->regbase + UNIPHIER_CLK_CPUGEAR_STAT, &val);
62 if (ret)
63 return ret;
65 val &= gear->mask;
67 return val < num_parents ? val : -EINVAL;
70 static const struct clk_ops uniphier_clk_cpugear_ops = {
71 .determine_rate = __clk_mux_determine_rate,
72 .set_parent = uniphier_clk_cpugear_set_parent,
73 .get_parent = uniphier_clk_cpugear_get_parent,
76 struct clk_hw *uniphier_clk_register_cpugear(struct device *dev,
77 struct regmap *regmap,
78 const char *name,
79 const struct uniphier_clk_cpugear_data *data)
81 struct uniphier_clk_cpugear *gear;
82 struct clk_init_data init;
83 int ret;
85 gear = devm_kzalloc(dev, sizeof(*gear), GFP_KERNEL);
86 if (!gear)
87 return ERR_PTR(-ENOMEM);
89 init.name = name;
90 init.ops = &uniphier_clk_cpugear_ops;
91 init.flags = CLK_SET_RATE_PARENT;
92 init.parent_names = data->parent_names;
93 init.num_parents = data->num_parents;
95 gear->regmap = regmap;
96 gear->regbase = data->regbase;
97 gear->mask = data->mask;
98 gear->hw.init = &init;
100 ret = devm_clk_hw_register(dev, &gear->hw);
101 if (ret)
102 return ERR_PTR(ret);
104 return &gear->hw;