xref: /linux/drivers/clk/clk-rk808.c (revision 2025cf9e193de05b0654570dd639acb49ebd3adf)
1*2025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2038b892aSChris Zhong /*
3038b892aSChris Zhong  * Clkout driver for Rockchip RK808
4038b892aSChris Zhong  *
5038b892aSChris Zhong  * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
6038b892aSChris Zhong  *
7038b892aSChris Zhong  * Author:Chris Zhong <zyw@rock-chips.com>
8038b892aSChris Zhong  */
9038b892aSChris Zhong 
10038b892aSChris Zhong #include <linux/clk-provider.h>
11038b892aSChris Zhong #include <linux/module.h>
12038b892aSChris Zhong #include <linux/slab.h>
13038b892aSChris Zhong #include <linux/platform_device.h>
14038b892aSChris Zhong #include <linux/mfd/rk808.h>
15038b892aSChris Zhong #include <linux/i2c.h>
16038b892aSChris Zhong 
17038b892aSChris Zhong struct rk808_clkout {
18038b892aSChris Zhong 	struct rk808 *rk808;
19038b892aSChris Zhong 	struct clk_hw		clkout1_hw;
20038b892aSChris Zhong 	struct clk_hw		clkout2_hw;
21038b892aSChris Zhong };
22038b892aSChris Zhong 
23038b892aSChris Zhong static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw,
24038b892aSChris Zhong 					      unsigned long parent_rate)
25038b892aSChris Zhong {
26038b892aSChris Zhong 	return 32768;
27038b892aSChris Zhong }
28038b892aSChris Zhong 
29038b892aSChris Zhong static int rk808_clkout2_enable(struct clk_hw *hw, bool enable)
30038b892aSChris Zhong {
31038b892aSChris Zhong 	struct rk808_clkout *rk808_clkout = container_of(hw,
32038b892aSChris Zhong 							 struct rk808_clkout,
33038b892aSChris Zhong 							 clkout2_hw);
34038b892aSChris Zhong 	struct rk808 *rk808 = rk808_clkout->rk808;
35038b892aSChris Zhong 
36038b892aSChris Zhong 	return regmap_update_bits(rk808->regmap, RK808_CLK32OUT_REG,
37038b892aSChris Zhong 				  CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0);
38038b892aSChris Zhong }
39038b892aSChris Zhong 
40038b892aSChris Zhong static int rk808_clkout2_prepare(struct clk_hw *hw)
41038b892aSChris Zhong {
42038b892aSChris Zhong 	return rk808_clkout2_enable(hw, true);
43038b892aSChris Zhong }
44038b892aSChris Zhong 
45038b892aSChris Zhong static void rk808_clkout2_unprepare(struct clk_hw *hw)
46038b892aSChris Zhong {
47038b892aSChris Zhong 	rk808_clkout2_enable(hw, false);
48038b892aSChris Zhong }
49038b892aSChris Zhong 
50038b892aSChris Zhong static int rk808_clkout2_is_prepared(struct clk_hw *hw)
51038b892aSChris Zhong {
52038b892aSChris Zhong 	struct rk808_clkout *rk808_clkout = container_of(hw,
53038b892aSChris Zhong 							 struct rk808_clkout,
54038b892aSChris Zhong 							 clkout2_hw);
55038b892aSChris Zhong 	struct rk808 *rk808 = rk808_clkout->rk808;
56038b892aSChris Zhong 	uint32_t val;
57038b892aSChris Zhong 
58038b892aSChris Zhong 	int ret = regmap_read(rk808->regmap, RK808_CLK32OUT_REG, &val);
59038b892aSChris Zhong 
60038b892aSChris Zhong 	if (ret < 0)
61038b892aSChris Zhong 		return ret;
62038b892aSChris Zhong 
63038b892aSChris Zhong 	return (val & CLK32KOUT2_EN) ? 1 : 0;
64038b892aSChris Zhong }
65038b892aSChris Zhong 
66038b892aSChris Zhong static const struct clk_ops rk808_clkout1_ops = {
67038b892aSChris Zhong 	.recalc_rate = rk808_clkout_recalc_rate,
68038b892aSChris Zhong };
69038b892aSChris Zhong 
70038b892aSChris Zhong static const struct clk_ops rk808_clkout2_ops = {
71038b892aSChris Zhong 	.prepare = rk808_clkout2_prepare,
72038b892aSChris Zhong 	.unprepare = rk808_clkout2_unprepare,
73038b892aSChris Zhong 	.is_prepared = rk808_clkout2_is_prepared,
74038b892aSChris Zhong 	.recalc_rate = rk808_clkout_recalc_rate,
75038b892aSChris Zhong };
76038b892aSChris Zhong 
77a8b6e85dSStephen Boyd static struct clk_hw *
78a8b6e85dSStephen Boyd of_clk_rk808_get(struct of_phandle_args *clkspec, void *data)
79a8b6e85dSStephen Boyd {
80a8b6e85dSStephen Boyd 	struct rk808_clkout *rk808_clkout = data;
81a8b6e85dSStephen Boyd 	unsigned int idx = clkspec->args[0];
82a8b6e85dSStephen Boyd 
83a8b6e85dSStephen Boyd 	if (idx >= 2) {
84a8b6e85dSStephen Boyd 		pr_err("%s: invalid index %u\n", __func__, idx);
85a8b6e85dSStephen Boyd 		return ERR_PTR(-EINVAL);
86a8b6e85dSStephen Boyd 	}
87a8b6e85dSStephen Boyd 
88a8b6e85dSStephen Boyd 	return idx ? &rk808_clkout->clkout2_hw : &rk808_clkout->clkout1_hw;
89a8b6e85dSStephen Boyd }
90a8b6e85dSStephen Boyd 
91038b892aSChris Zhong static int rk808_clkout_probe(struct platform_device *pdev)
92038b892aSChris Zhong {
93038b892aSChris Zhong 	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
94038b892aSChris Zhong 	struct i2c_client *client = rk808->i2c;
95038b892aSChris Zhong 	struct device_node *node = client->dev.of_node;
96038b892aSChris Zhong 	struct clk_init_data init = {};
97038b892aSChris Zhong 	struct rk808_clkout *rk808_clkout;
98a8b6e85dSStephen Boyd 	int ret;
99038b892aSChris Zhong 
100038b892aSChris Zhong 	rk808_clkout = devm_kzalloc(&client->dev,
101038b892aSChris Zhong 				    sizeof(*rk808_clkout), GFP_KERNEL);
102038b892aSChris Zhong 	if (!rk808_clkout)
103038b892aSChris Zhong 		return -ENOMEM;
104038b892aSChris Zhong 
105038b892aSChris Zhong 	rk808_clkout->rk808 = rk808;
106038b892aSChris Zhong 
107038b892aSChris Zhong 	init.parent_names = NULL;
108038b892aSChris Zhong 	init.num_parents = 0;
109038b892aSChris Zhong 	init.name = "rk808-clkout1";
110038b892aSChris Zhong 	init.ops = &rk808_clkout1_ops;
111038b892aSChris Zhong 	rk808_clkout->clkout1_hw.init = &init;
112038b892aSChris Zhong 
113038b892aSChris Zhong 	/* optional override of the clockname */
114038b892aSChris Zhong 	of_property_read_string_index(node, "clock-output-names",
115038b892aSChris Zhong 				      0, &init.name);
116038b892aSChris Zhong 
117a8b6e85dSStephen Boyd 	ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout1_hw);
118a8b6e85dSStephen Boyd 	if (ret)
119a8b6e85dSStephen Boyd 		return ret;
120038b892aSChris Zhong 
121038b892aSChris Zhong 	init.name = "rk808-clkout2";
122038b892aSChris Zhong 	init.ops = &rk808_clkout2_ops;
123038b892aSChris Zhong 	rk808_clkout->clkout2_hw.init = &init;
124038b892aSChris Zhong 
125038b892aSChris Zhong 	/* optional override of the clockname */
126038b892aSChris Zhong 	of_property_read_string_index(node, "clock-output-names",
127038b892aSChris Zhong 				      1, &init.name);
128038b892aSChris Zhong 
129a8b6e85dSStephen Boyd 	ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout2_hw);
130a8b6e85dSStephen Boyd 	if (ret)
131a8b6e85dSStephen Boyd 		return ret;
132038b892aSChris Zhong 
13325224667SMatti Vaittinen 	return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_rk808_get,
13425224667SMatti Vaittinen 					   rk808_clkout);
135038b892aSChris Zhong }
136038b892aSChris Zhong 
137038b892aSChris Zhong static struct platform_driver rk808_clkout_driver = {
138038b892aSChris Zhong 	.probe = rk808_clkout_probe,
139038b892aSChris Zhong 	.driver		= {
140038b892aSChris Zhong 		.name	= "rk808-clkout",
141038b892aSChris Zhong 	},
142038b892aSChris Zhong };
143038b892aSChris Zhong 
144038b892aSChris Zhong module_platform_driver(rk808_clkout_driver);
145038b892aSChris Zhong 
146038b892aSChris Zhong MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
147038b892aSChris Zhong MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
148038b892aSChris Zhong MODULE_LICENSE("GPL");
149038b892aSChris Zhong MODULE_ALIAS("platform:rk808-clkout");
150