xref: /openbmc/linux/drivers/clk/clk-rk808.c (revision bc5aa3a0)
1 /*
2  * Clkout driver for Rockchip RK808
3  *
4  * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
5  *
6  * Author:Chris Zhong <zyw@rock-chips.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  */
17 
18 #include <linux/clk-provider.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/platform_device.h>
22 #include <linux/mfd/rk808.h>
23 #include <linux/i2c.h>
24 
25 #define RK808_NR_OUTPUT 2
26 
27 struct rk808_clkout {
28 	struct rk808 *rk808;
29 	struct clk_onecell_data clk_data;
30 	struct clk_hw		clkout1_hw;
31 	struct clk_hw		clkout2_hw;
32 };
33 
34 static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw,
35 					      unsigned long parent_rate)
36 {
37 	return 32768;
38 }
39 
40 static int rk808_clkout2_enable(struct clk_hw *hw, bool enable)
41 {
42 	struct rk808_clkout *rk808_clkout = container_of(hw,
43 							 struct rk808_clkout,
44 							 clkout2_hw);
45 	struct rk808 *rk808 = rk808_clkout->rk808;
46 
47 	return regmap_update_bits(rk808->regmap, RK808_CLK32OUT_REG,
48 				  CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0);
49 }
50 
51 static int rk808_clkout2_prepare(struct clk_hw *hw)
52 {
53 	return rk808_clkout2_enable(hw, true);
54 }
55 
56 static void rk808_clkout2_unprepare(struct clk_hw *hw)
57 {
58 	rk808_clkout2_enable(hw, false);
59 }
60 
61 static int rk808_clkout2_is_prepared(struct clk_hw *hw)
62 {
63 	struct rk808_clkout *rk808_clkout = container_of(hw,
64 							 struct rk808_clkout,
65 							 clkout2_hw);
66 	struct rk808 *rk808 = rk808_clkout->rk808;
67 	uint32_t val;
68 
69 	int ret = regmap_read(rk808->regmap, RK808_CLK32OUT_REG, &val);
70 
71 	if (ret < 0)
72 		return ret;
73 
74 	return (val & CLK32KOUT2_EN) ? 1 : 0;
75 }
76 
77 static const struct clk_ops rk808_clkout1_ops = {
78 	.recalc_rate = rk808_clkout_recalc_rate,
79 };
80 
81 static const struct clk_ops rk808_clkout2_ops = {
82 	.prepare = rk808_clkout2_prepare,
83 	.unprepare = rk808_clkout2_unprepare,
84 	.is_prepared = rk808_clkout2_is_prepared,
85 	.recalc_rate = rk808_clkout_recalc_rate,
86 };
87 
88 static int rk808_clkout_probe(struct platform_device *pdev)
89 {
90 	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
91 	struct i2c_client *client = rk808->i2c;
92 	struct device_node *node = client->dev.of_node;
93 	struct clk_init_data init = {};
94 	struct clk **clk_table;
95 	struct rk808_clkout *rk808_clkout;
96 
97 	rk808_clkout = devm_kzalloc(&client->dev,
98 				    sizeof(*rk808_clkout), GFP_KERNEL);
99 	if (!rk808_clkout)
100 		return -ENOMEM;
101 
102 	rk808_clkout->rk808 = rk808;
103 
104 	clk_table = devm_kcalloc(&client->dev, RK808_NR_OUTPUT,
105 				 sizeof(struct clk *), GFP_KERNEL);
106 	if (!clk_table)
107 		return -ENOMEM;
108 
109 	init.parent_names = NULL;
110 	init.num_parents = 0;
111 	init.name = "rk808-clkout1";
112 	init.ops = &rk808_clkout1_ops;
113 	rk808_clkout->clkout1_hw.init = &init;
114 
115 	/* optional override of the clockname */
116 	of_property_read_string_index(node, "clock-output-names",
117 				      0, &init.name);
118 
119 	clk_table[0] = devm_clk_register(&client->dev,
120 					 &rk808_clkout->clkout1_hw);
121 	if (IS_ERR(clk_table[0]))
122 		return PTR_ERR(clk_table[0]);
123 
124 	init.name = "rk808-clkout2";
125 	init.ops = &rk808_clkout2_ops;
126 	rk808_clkout->clkout2_hw.init = &init;
127 
128 	/* optional override of the clockname */
129 	of_property_read_string_index(node, "clock-output-names",
130 				      1, &init.name);
131 
132 	clk_table[1] = devm_clk_register(&client->dev,
133 					 &rk808_clkout->clkout2_hw);
134 	if (IS_ERR(clk_table[1]))
135 		return PTR_ERR(clk_table[1]);
136 
137 	rk808_clkout->clk_data.clks = clk_table;
138 	rk808_clkout->clk_data.clk_num = RK808_NR_OUTPUT;
139 
140 	return of_clk_add_provider(node, of_clk_src_onecell_get,
141 				   &rk808_clkout->clk_data);
142 }
143 
144 static int rk808_clkout_remove(struct platform_device *pdev)
145 {
146 	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
147 	struct i2c_client *client = rk808->i2c;
148 	struct device_node *node = client->dev.of_node;
149 
150 	of_clk_del_provider(node);
151 
152 	return 0;
153 }
154 
155 static struct platform_driver rk808_clkout_driver = {
156 	.probe = rk808_clkout_probe,
157 	.remove = rk808_clkout_remove,
158 	.driver		= {
159 		.name	= "rk808-clkout",
160 	},
161 };
162 
163 module_platform_driver(rk808_clkout_driver);
164 
165 MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
166 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
167 MODULE_LICENSE("GPL");
168 MODULE_ALIAS("platform:rk808-clkout");
169