1 // SPDX-License-Identifier: GPL-2.0-only
3 * Clkout driver for Rockchip RK808
5 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
7 * Author:Chris Zhong <zyw@rock-chips.com>
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>
17 struct regmap
*regmap
;
18 struct clk_hw clkout1_hw
;
19 struct clk_hw clkout2_hw
;
22 static unsigned long rk808_clkout_recalc_rate(struct clk_hw
*hw
,
23 unsigned long parent_rate
)
28 static int rk808_clkout2_enable(struct clk_hw
*hw
, bool enable
)
30 struct rk808_clkout
*rk808_clkout
= container_of(hw
,
34 return regmap_update_bits(rk808_clkout
->regmap
, RK808_CLK32OUT_REG
,
35 CLK32KOUT2_EN
, enable
? CLK32KOUT2_EN
: 0);
38 static int rk808_clkout2_prepare(struct clk_hw
*hw
)
40 return rk808_clkout2_enable(hw
, true);
43 static void rk808_clkout2_unprepare(struct clk_hw
*hw
)
45 rk808_clkout2_enable(hw
, false);
48 static int rk808_clkout2_is_prepared(struct clk_hw
*hw
)
50 struct rk808_clkout
*rk808_clkout
= container_of(hw
,
55 int ret
= regmap_read(rk808_clkout
->regmap
, RK808_CLK32OUT_REG
, &val
);
60 return (val
& CLK32KOUT2_EN
) ? 1 : 0;
63 static const struct clk_ops rk808_clkout1_ops
= {
64 .recalc_rate
= rk808_clkout_recalc_rate
,
67 static const struct clk_ops rk808_clkout2_ops
= {
68 .prepare
= rk808_clkout2_prepare
,
69 .unprepare
= rk808_clkout2_unprepare
,
70 .is_prepared
= rk808_clkout2_is_prepared
,
71 .recalc_rate
= rk808_clkout_recalc_rate
,
74 static struct clk_hw
*
75 of_clk_rk808_get(struct of_phandle_args
*clkspec
, void *data
)
77 struct rk808_clkout
*rk808_clkout
= data
;
78 unsigned int idx
= clkspec
->args
[0];
81 pr_err("%s: invalid index %u\n", __func__
, idx
);
82 return ERR_PTR(-EINVAL
);
85 return idx
? &rk808_clkout
->clkout2_hw
: &rk808_clkout
->clkout1_hw
;
88 static int rk817_clkout2_enable(struct clk_hw
*hw
, bool enable
)
90 struct rk808_clkout
*rk808_clkout
= container_of(hw
,
94 return regmap_update_bits(rk808_clkout
->regmap
, RK817_SYS_CFG(1),
96 enable
? RK817_CLK32KOUT2_EN
: 0);
99 static int rk817_clkout2_prepare(struct clk_hw
*hw
)
101 return rk817_clkout2_enable(hw
, true);
104 static void rk817_clkout2_unprepare(struct clk_hw
*hw
)
106 rk817_clkout2_enable(hw
, false);
109 static int rk817_clkout2_is_prepared(struct clk_hw
*hw
)
111 struct rk808_clkout
*rk808_clkout
= container_of(hw
,
116 int ret
= regmap_read(rk808_clkout
->regmap
, RK817_SYS_CFG(1), &val
);
121 return (val
& RK817_CLK32KOUT2_EN
) ? 1 : 0;
124 static const struct clk_ops rk817_clkout2_ops
= {
125 .prepare
= rk817_clkout2_prepare
,
126 .unprepare
= rk817_clkout2_unprepare
,
127 .is_prepared
= rk817_clkout2_is_prepared
,
128 .recalc_rate
= rk808_clkout_recalc_rate
,
131 static const struct clk_ops
*rkpmic_get_ops(long variant
)
136 return &rk817_clkout2_ops
;
138 * For the default case, it match the following PMIC type.
144 return &rk808_clkout2_ops
;
148 static int rk808_clkout_probe(struct platform_device
*pdev
)
150 struct rk808
*rk808
= dev_get_drvdata(pdev
->dev
.parent
);
151 struct device
*dev
= &pdev
->dev
;
152 struct clk_init_data init
= {};
153 struct rk808_clkout
*rk808_clkout
;
156 dev
->of_node
= pdev
->dev
.parent
->of_node
;
158 rk808_clkout
= devm_kzalloc(dev
,
159 sizeof(*rk808_clkout
), GFP_KERNEL
);
163 rk808_clkout
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
164 if (!rk808_clkout
->regmap
)
167 init
.parent_names
= NULL
;
168 init
.num_parents
= 0;
169 init
.name
= "rk808-clkout1";
170 init
.ops
= &rk808_clkout1_ops
;
171 rk808_clkout
->clkout1_hw
.init
= &init
;
173 /* optional override of the clockname */
174 of_property_read_string_index(dev
->of_node
, "clock-output-names",
177 ret
= devm_clk_hw_register(dev
, &rk808_clkout
->clkout1_hw
);
181 init
.name
= "rk808-clkout2";
182 init
.ops
= rkpmic_get_ops(rk808
->variant
);
183 rk808_clkout
->clkout2_hw
.init
= &init
;
185 /* optional override of the clockname */
186 of_property_read_string_index(dev
->of_node
, "clock-output-names",
189 ret
= devm_clk_hw_register(dev
, &rk808_clkout
->clkout2_hw
);
193 return devm_of_clk_add_hw_provider(&pdev
->dev
, of_clk_rk808_get
,
197 static struct platform_driver rk808_clkout_driver
= {
198 .probe
= rk808_clkout_probe
,
200 .name
= "rk808-clkout",
204 module_platform_driver(rk808_clkout_driver
);
206 MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
207 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
208 MODULE_LICENSE("GPL");
209 MODULE_ALIAS("platform:rk808-clkout");