Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / clk / clk-rk808.c
blob32f833d732ed5655fdcd7419ea626cc2fa04654d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Clkout driver for Rockchip RK808
5 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
7 * Author:Chris Zhong <zyw@rock-chips.com>
8 */
10 #include <linux/clk-provider.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
14 #include <linux/mfd/rk808.h>
15 #include <linux/i2c.h>
17 struct rk808_clkout {
18 struct rk808 *rk808;
19 struct clk_hw clkout1_hw;
20 struct clk_hw clkout2_hw;
23 static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw,
24 unsigned long parent_rate)
26 return 32768;
29 static int rk808_clkout2_enable(struct clk_hw *hw, bool enable)
31 struct rk808_clkout *rk808_clkout = container_of(hw,
32 struct rk808_clkout,
33 clkout2_hw);
34 struct rk808 *rk808 = rk808_clkout->rk808;
36 return regmap_update_bits(rk808->regmap, RK808_CLK32OUT_REG,
37 CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0);
40 static int rk808_clkout2_prepare(struct clk_hw *hw)
42 return rk808_clkout2_enable(hw, true);
45 static void rk808_clkout2_unprepare(struct clk_hw *hw)
47 rk808_clkout2_enable(hw, false);
50 static int rk808_clkout2_is_prepared(struct clk_hw *hw)
52 struct rk808_clkout *rk808_clkout = container_of(hw,
53 struct rk808_clkout,
54 clkout2_hw);
55 struct rk808 *rk808 = rk808_clkout->rk808;
56 uint32_t val;
58 int ret = regmap_read(rk808->regmap, RK808_CLK32OUT_REG, &val);
60 if (ret < 0)
61 return ret;
63 return (val & CLK32KOUT2_EN) ? 1 : 0;
66 static const struct clk_ops rk808_clkout1_ops = {
67 .recalc_rate = rk808_clkout_recalc_rate,
70 static const struct clk_ops rk808_clkout2_ops = {
71 .prepare = rk808_clkout2_prepare,
72 .unprepare = rk808_clkout2_unprepare,
73 .is_prepared = rk808_clkout2_is_prepared,
74 .recalc_rate = rk808_clkout_recalc_rate,
77 static struct clk_hw *
78 of_clk_rk808_get(struct of_phandle_args *clkspec, void *data)
80 struct rk808_clkout *rk808_clkout = data;
81 unsigned int idx = clkspec->args[0];
83 if (idx >= 2) {
84 pr_err("%s: invalid index %u\n", __func__, idx);
85 return ERR_PTR(-EINVAL);
88 return idx ? &rk808_clkout->clkout2_hw : &rk808_clkout->clkout1_hw;
91 static int rk817_clkout2_enable(struct clk_hw *hw, bool enable)
93 struct rk808_clkout *rk808_clkout = container_of(hw,
94 struct rk808_clkout,
95 clkout2_hw);
96 struct rk808 *rk808 = rk808_clkout->rk808;
98 return regmap_update_bits(rk808->regmap, RK817_SYS_CFG(1),
99 RK817_CLK32KOUT2_EN,
100 enable ? RK817_CLK32KOUT2_EN : 0);
103 static int rk817_clkout2_prepare(struct clk_hw *hw)
105 return rk817_clkout2_enable(hw, true);
108 static void rk817_clkout2_unprepare(struct clk_hw *hw)
110 rk817_clkout2_enable(hw, false);
113 static int rk817_clkout2_is_prepared(struct clk_hw *hw)
115 struct rk808_clkout *rk808_clkout = container_of(hw,
116 struct rk808_clkout,
117 clkout2_hw);
118 struct rk808 *rk808 = rk808_clkout->rk808;
119 unsigned int val;
121 int ret = regmap_read(rk808->regmap, RK817_SYS_CFG(1), &val);
123 if (ret < 0)
124 return 0;
126 return (val & RK817_CLK32KOUT2_EN) ? 1 : 0;
129 static const struct clk_ops rk817_clkout2_ops = {
130 .prepare = rk817_clkout2_prepare,
131 .unprepare = rk817_clkout2_unprepare,
132 .is_prepared = rk817_clkout2_is_prepared,
133 .recalc_rate = rk808_clkout_recalc_rate,
136 static const struct clk_ops *rkpmic_get_ops(long variant)
138 switch (variant) {
139 case RK809_ID:
140 case RK817_ID:
141 return &rk817_clkout2_ops;
143 * For the default case, it match the following PMIC type.
144 * RK805_ID
145 * RK808_ID
146 * RK818_ID
148 default:
149 return &rk808_clkout2_ops;
153 static int rk808_clkout_probe(struct platform_device *pdev)
155 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
156 struct i2c_client *client = rk808->i2c;
157 struct device_node *node = client->dev.of_node;
158 struct clk_init_data init = {};
159 struct rk808_clkout *rk808_clkout;
160 int ret;
162 rk808_clkout = devm_kzalloc(&client->dev,
163 sizeof(*rk808_clkout), GFP_KERNEL);
164 if (!rk808_clkout)
165 return -ENOMEM;
167 rk808_clkout->rk808 = rk808;
169 init.parent_names = NULL;
170 init.num_parents = 0;
171 init.name = "rk808-clkout1";
172 init.ops = &rk808_clkout1_ops;
173 rk808_clkout->clkout1_hw.init = &init;
175 /* optional override of the clockname */
176 of_property_read_string_index(node, "clock-output-names",
177 0, &init.name);
179 ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout1_hw);
180 if (ret)
181 return ret;
183 init.name = "rk808-clkout2";
184 init.ops = rkpmic_get_ops(rk808->variant);
185 rk808_clkout->clkout2_hw.init = &init;
187 /* optional override of the clockname */
188 of_property_read_string_index(node, "clock-output-names",
189 1, &init.name);
191 ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout2_hw);
192 if (ret)
193 return ret;
195 return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_rk808_get,
196 rk808_clkout);
199 static struct platform_driver rk808_clkout_driver = {
200 .probe = rk808_clkout_probe,
201 .driver = {
202 .name = "rk808-clkout",
206 module_platform_driver(rk808_clkout_driver);
208 MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
209 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
210 MODULE_LICENSE("GPL");
211 MODULE_ALIAS("platform:rk808-clkout");