xref: /linux/drivers/clk/clk-rk808.c (revision a8b6e85db6a65132ebc85e38b6391669b37b36f8)
1038b892aSChris Zhong /*
2038b892aSChris Zhong  * Clkout driver for Rockchip RK808
3038b892aSChris Zhong  *
4038b892aSChris Zhong  * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
5038b892aSChris Zhong  *
6038b892aSChris Zhong  * Author:Chris Zhong <zyw@rock-chips.com>
7038b892aSChris Zhong  *
8038b892aSChris Zhong  * This program is free software; you can redistribute it and/or modify it
9038b892aSChris Zhong  * under the terms and conditions of the GNU General Public License,
10038b892aSChris Zhong  * version 2, as published by the Free Software Foundation.
11038b892aSChris Zhong  *
12038b892aSChris Zhong  * This program is distributed in the hope it will be useful, but WITHOUT
13038b892aSChris Zhong  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14038b892aSChris Zhong  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15038b892aSChris Zhong  * more details.
16038b892aSChris Zhong  */
17038b892aSChris Zhong 
18038b892aSChris Zhong #include <linux/clk-provider.h>
19038b892aSChris Zhong #include <linux/module.h>
20038b892aSChris Zhong #include <linux/slab.h>
21038b892aSChris Zhong #include <linux/platform_device.h>
22038b892aSChris Zhong #include <linux/mfd/rk808.h>
23038b892aSChris Zhong #include <linux/i2c.h>
24038b892aSChris Zhong 
25038b892aSChris Zhong struct rk808_clkout {
26038b892aSChris Zhong 	struct rk808 *rk808;
27038b892aSChris Zhong 	struct clk_hw		clkout1_hw;
28038b892aSChris Zhong 	struct clk_hw		clkout2_hw;
29038b892aSChris Zhong };
30038b892aSChris Zhong 
31038b892aSChris Zhong static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw,
32038b892aSChris Zhong 					      unsigned long parent_rate)
33038b892aSChris Zhong {
34038b892aSChris Zhong 	return 32768;
35038b892aSChris Zhong }
36038b892aSChris Zhong 
37038b892aSChris Zhong static int rk808_clkout2_enable(struct clk_hw *hw, bool enable)
38038b892aSChris Zhong {
39038b892aSChris Zhong 	struct rk808_clkout *rk808_clkout = container_of(hw,
40038b892aSChris Zhong 							 struct rk808_clkout,
41038b892aSChris Zhong 							 clkout2_hw);
42038b892aSChris Zhong 	struct rk808 *rk808 = rk808_clkout->rk808;
43038b892aSChris Zhong 
44038b892aSChris Zhong 	return regmap_update_bits(rk808->regmap, RK808_CLK32OUT_REG,
45038b892aSChris Zhong 				  CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0);
46038b892aSChris Zhong }
47038b892aSChris Zhong 
48038b892aSChris Zhong static int rk808_clkout2_prepare(struct clk_hw *hw)
49038b892aSChris Zhong {
50038b892aSChris Zhong 	return rk808_clkout2_enable(hw, true);
51038b892aSChris Zhong }
52038b892aSChris Zhong 
53038b892aSChris Zhong static void rk808_clkout2_unprepare(struct clk_hw *hw)
54038b892aSChris Zhong {
55038b892aSChris Zhong 	rk808_clkout2_enable(hw, false);
56038b892aSChris Zhong }
57038b892aSChris Zhong 
58038b892aSChris Zhong static int rk808_clkout2_is_prepared(struct clk_hw *hw)
59038b892aSChris Zhong {
60038b892aSChris Zhong 	struct rk808_clkout *rk808_clkout = container_of(hw,
61038b892aSChris Zhong 							 struct rk808_clkout,
62038b892aSChris Zhong 							 clkout2_hw);
63038b892aSChris Zhong 	struct rk808 *rk808 = rk808_clkout->rk808;
64038b892aSChris Zhong 	uint32_t val;
65038b892aSChris Zhong 
66038b892aSChris Zhong 	int ret = regmap_read(rk808->regmap, RK808_CLK32OUT_REG, &val);
67038b892aSChris Zhong 
68038b892aSChris Zhong 	if (ret < 0)
69038b892aSChris Zhong 		return ret;
70038b892aSChris Zhong 
71038b892aSChris Zhong 	return (val & CLK32KOUT2_EN) ? 1 : 0;
72038b892aSChris Zhong }
73038b892aSChris Zhong 
74038b892aSChris Zhong static const struct clk_ops rk808_clkout1_ops = {
75038b892aSChris Zhong 	.recalc_rate = rk808_clkout_recalc_rate,
76038b892aSChris Zhong };
77038b892aSChris Zhong 
78038b892aSChris Zhong static const struct clk_ops rk808_clkout2_ops = {
79038b892aSChris Zhong 	.prepare = rk808_clkout2_prepare,
80038b892aSChris Zhong 	.unprepare = rk808_clkout2_unprepare,
81038b892aSChris Zhong 	.is_prepared = rk808_clkout2_is_prepared,
82038b892aSChris Zhong 	.recalc_rate = rk808_clkout_recalc_rate,
83038b892aSChris Zhong };
84038b892aSChris Zhong 
85*a8b6e85dSStephen Boyd static struct clk_hw *
86*a8b6e85dSStephen Boyd of_clk_rk808_get(struct of_phandle_args *clkspec, void *data)
87*a8b6e85dSStephen Boyd {
88*a8b6e85dSStephen Boyd 	struct rk808_clkout *rk808_clkout = data;
89*a8b6e85dSStephen Boyd 	unsigned int idx = clkspec->args[0];
90*a8b6e85dSStephen Boyd 
91*a8b6e85dSStephen Boyd 	if (idx >= 2) {
92*a8b6e85dSStephen Boyd 		pr_err("%s: invalid index %u\n", __func__, idx);
93*a8b6e85dSStephen Boyd 		return ERR_PTR(-EINVAL);
94*a8b6e85dSStephen Boyd 	}
95*a8b6e85dSStephen Boyd 
96*a8b6e85dSStephen Boyd 	return idx ? &rk808_clkout->clkout2_hw : &rk808_clkout->clkout1_hw;
97*a8b6e85dSStephen Boyd }
98*a8b6e85dSStephen Boyd 
99038b892aSChris Zhong static int rk808_clkout_probe(struct platform_device *pdev)
100038b892aSChris Zhong {
101038b892aSChris Zhong 	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
102038b892aSChris Zhong 	struct i2c_client *client = rk808->i2c;
103038b892aSChris Zhong 	struct device_node *node = client->dev.of_node;
104038b892aSChris Zhong 	struct clk_init_data init = {};
105038b892aSChris Zhong 	struct rk808_clkout *rk808_clkout;
106*a8b6e85dSStephen Boyd 	int ret;
107038b892aSChris Zhong 
108038b892aSChris Zhong 	rk808_clkout = devm_kzalloc(&client->dev,
109038b892aSChris Zhong 				    sizeof(*rk808_clkout), GFP_KERNEL);
110038b892aSChris Zhong 	if (!rk808_clkout)
111038b892aSChris Zhong 		return -ENOMEM;
112038b892aSChris Zhong 
113038b892aSChris Zhong 	rk808_clkout->rk808 = rk808;
114038b892aSChris Zhong 
115038b892aSChris Zhong 	init.parent_names = NULL;
116038b892aSChris Zhong 	init.num_parents = 0;
117038b892aSChris Zhong 	init.name = "rk808-clkout1";
118038b892aSChris Zhong 	init.ops = &rk808_clkout1_ops;
119038b892aSChris Zhong 	rk808_clkout->clkout1_hw.init = &init;
120038b892aSChris Zhong 
121038b892aSChris Zhong 	/* optional override of the clockname */
122038b892aSChris Zhong 	of_property_read_string_index(node, "clock-output-names",
123038b892aSChris Zhong 				      0, &init.name);
124038b892aSChris Zhong 
125*a8b6e85dSStephen Boyd 	ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout1_hw);
126*a8b6e85dSStephen Boyd 	if (ret)
127*a8b6e85dSStephen Boyd 		return ret;
128038b892aSChris Zhong 
129038b892aSChris Zhong 	init.name = "rk808-clkout2";
130038b892aSChris Zhong 	init.ops = &rk808_clkout2_ops;
131038b892aSChris Zhong 	rk808_clkout->clkout2_hw.init = &init;
132038b892aSChris Zhong 
133038b892aSChris Zhong 	/* optional override of the clockname */
134038b892aSChris Zhong 	of_property_read_string_index(node, "clock-output-names",
135038b892aSChris Zhong 				      1, &init.name);
136038b892aSChris Zhong 
137*a8b6e85dSStephen Boyd 	ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout2_hw);
138*a8b6e85dSStephen Boyd 	if (ret)
139*a8b6e85dSStephen Boyd 		return ret;
140038b892aSChris Zhong 
141*a8b6e85dSStephen Boyd 	return of_clk_add_hw_provider(node, of_clk_rk808_get, &rk808_clkout);
142038b892aSChris Zhong }
143038b892aSChris Zhong 
144038b892aSChris Zhong static int rk808_clkout_remove(struct platform_device *pdev)
145038b892aSChris Zhong {
146038b892aSChris Zhong 	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
147038b892aSChris Zhong 	struct i2c_client *client = rk808->i2c;
148038b892aSChris Zhong 	struct device_node *node = client->dev.of_node;
149038b892aSChris Zhong 
150038b892aSChris Zhong 	of_clk_del_provider(node);
151038b892aSChris Zhong 
152038b892aSChris Zhong 	return 0;
153038b892aSChris Zhong }
154038b892aSChris Zhong 
155038b892aSChris Zhong static struct platform_driver rk808_clkout_driver = {
156038b892aSChris Zhong 	.probe = rk808_clkout_probe,
157038b892aSChris Zhong 	.remove = rk808_clkout_remove,
158038b892aSChris Zhong 	.driver		= {
159038b892aSChris Zhong 		.name	= "rk808-clkout",
160038b892aSChris Zhong 	},
161038b892aSChris Zhong };
162038b892aSChris Zhong 
163038b892aSChris Zhong module_platform_driver(rk808_clkout_driver);
164038b892aSChris Zhong 
165038b892aSChris Zhong MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
166038b892aSChris Zhong MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
167038b892aSChris Zhong MODULE_LICENSE("GPL");
168038b892aSChris Zhong MODULE_ALIAS("platform:rk808-clkout");
169