xref: /openbmc/linux/drivers/clk/qcom/common.c (revision 4da722ca)
1 /*
2  * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <linux/export.h>
15 #include <linux/module.h>
16 #include <linux/regmap.h>
17 #include <linux/platform_device.h>
18 #include <linux/clk-provider.h>
19 #include <linux/reset-controller.h>
20 #include <linux/of.h>
21 
22 #include "common.h"
23 #include "clk-rcg.h"
24 #include "clk-regmap.h"
25 #include "reset.h"
26 #include "gdsc.h"
27 
28 struct qcom_cc {
29 	struct qcom_reset_controller reset;
30 	struct clk_regmap **rclks;
31 	size_t num_rclks;
32 };
33 
34 const
35 struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, unsigned long rate)
36 {
37 	if (!f)
38 		return NULL;
39 
40 	for (; f->freq; f++)
41 		if (rate <= f->freq)
42 			return f;
43 
44 	/* Default to our fastest rate */
45 	return f - 1;
46 }
47 EXPORT_SYMBOL_GPL(qcom_find_freq);
48 
49 const struct freq_tbl *qcom_find_freq_floor(const struct freq_tbl *f,
50 					    unsigned long rate)
51 {
52 	const struct freq_tbl *best = NULL;
53 
54 	for ( ; f->freq; f++) {
55 		if (rate >= f->freq)
56 			best = f;
57 		else
58 			break;
59 	}
60 
61 	return best;
62 }
63 EXPORT_SYMBOL_GPL(qcom_find_freq_floor);
64 
65 int qcom_find_src_index(struct clk_hw *hw, const struct parent_map *map, u8 src)
66 {
67 	int i, num_parents = clk_hw_get_num_parents(hw);
68 
69 	for (i = 0; i < num_parents; i++)
70 		if (src == map[i].src)
71 			return i;
72 
73 	return -ENOENT;
74 }
75 EXPORT_SYMBOL_GPL(qcom_find_src_index);
76 
77 struct regmap *
78 qcom_cc_map(struct platform_device *pdev, const struct qcom_cc_desc *desc)
79 {
80 	void __iomem *base;
81 	struct resource *res;
82 	struct device *dev = &pdev->dev;
83 
84 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
85 	base = devm_ioremap_resource(dev, res);
86 	if (IS_ERR(base))
87 		return ERR_CAST(base);
88 
89 	return devm_regmap_init_mmio(dev, base, desc->config);
90 }
91 EXPORT_SYMBOL_GPL(qcom_cc_map);
92 
93 void
94 qcom_pll_set_fsm_mode(struct regmap *map, u32 reg, u8 bias_count, u8 lock_count)
95 {
96 	u32 val;
97 	u32 mask;
98 
99 	/* De-assert reset to FSM */
100 	regmap_update_bits(map, reg, PLL_VOTE_FSM_RESET, 0);
101 
102 	/* Program bias count and lock count */
103 	val = bias_count << PLL_BIAS_COUNT_SHIFT |
104 		lock_count << PLL_LOCK_COUNT_SHIFT;
105 	mask = PLL_BIAS_COUNT_MASK << PLL_BIAS_COUNT_SHIFT;
106 	mask |= PLL_LOCK_COUNT_MASK << PLL_LOCK_COUNT_SHIFT;
107 	regmap_update_bits(map, reg, mask, val);
108 
109 	/* Enable PLL FSM voting */
110 	regmap_update_bits(map, reg, PLL_VOTE_FSM_ENA, PLL_VOTE_FSM_ENA);
111 }
112 EXPORT_SYMBOL_GPL(qcom_pll_set_fsm_mode);
113 
114 static void qcom_cc_del_clk_provider(void *data)
115 {
116 	of_clk_del_provider(data);
117 }
118 
119 static void qcom_cc_reset_unregister(void *data)
120 {
121 	reset_controller_unregister(data);
122 }
123 
124 static void qcom_cc_gdsc_unregister(void *data)
125 {
126 	gdsc_unregister(data);
127 }
128 
129 /*
130  * Backwards compatibility with old DTs. Register a pass-through factor 1/1
131  * clock to translate 'path' clk into 'name' clk and register the 'path'
132  * clk as a fixed rate clock if it isn't present.
133  */
134 static int _qcom_cc_register_board_clk(struct device *dev, const char *path,
135 				       const char *name, unsigned long rate,
136 				       bool add_factor)
137 {
138 	struct device_node *node = NULL;
139 	struct device_node *clocks_node;
140 	struct clk_fixed_factor *factor;
141 	struct clk_fixed_rate *fixed;
142 	struct clk_init_data init_data = { };
143 	int ret;
144 
145 	clocks_node = of_find_node_by_path("/clocks");
146 	if (clocks_node)
147 		node = of_find_node_by_name(clocks_node, path);
148 
149 	if (!node) {
150 		fixed = devm_kzalloc(dev, sizeof(*fixed), GFP_KERNEL);
151 		if (!fixed)
152 			return -EINVAL;
153 
154 		fixed->fixed_rate = rate;
155 		fixed->hw.init = &init_data;
156 
157 		init_data.name = path;
158 		init_data.ops = &clk_fixed_rate_ops;
159 
160 		ret = devm_clk_hw_register(dev, &fixed->hw);
161 		if (ret)
162 			return ret;
163 	}
164 	of_node_put(node);
165 
166 	if (add_factor) {
167 		factor = devm_kzalloc(dev, sizeof(*factor), GFP_KERNEL);
168 		if (!factor)
169 			return -EINVAL;
170 
171 		factor->mult = factor->div = 1;
172 		factor->hw.init = &init_data;
173 
174 		init_data.name = name;
175 		init_data.parent_names = &path;
176 		init_data.num_parents = 1;
177 		init_data.flags = 0;
178 		init_data.ops = &clk_fixed_factor_ops;
179 
180 		ret = devm_clk_hw_register(dev, &factor->hw);
181 		if (ret)
182 			return ret;
183 	}
184 
185 	return 0;
186 }
187 
188 int qcom_cc_register_board_clk(struct device *dev, const char *path,
189 			       const char *name, unsigned long rate)
190 {
191 	bool add_factor = true;
192 
193 	/*
194 	 * TODO: The RPM clock driver currently does not support the xo clock.
195 	 * When xo is added to the RPM clock driver, we should change this
196 	 * function to skip registration of xo factor clocks.
197 	 */
198 
199 	return _qcom_cc_register_board_clk(dev, path, name, rate, add_factor);
200 }
201 EXPORT_SYMBOL_GPL(qcom_cc_register_board_clk);
202 
203 int qcom_cc_register_sleep_clk(struct device *dev)
204 {
205 	return _qcom_cc_register_board_clk(dev, "sleep_clk", "sleep_clk_src",
206 					   32768, true);
207 }
208 EXPORT_SYMBOL_GPL(qcom_cc_register_sleep_clk);
209 
210 static struct clk_hw *qcom_cc_clk_hw_get(struct of_phandle_args *clkspec,
211 					 void *data)
212 {
213 	struct qcom_cc *cc = data;
214 	unsigned int idx = clkspec->args[0];
215 
216 	if (idx >= cc->num_rclks) {
217 		pr_err("%s: invalid index %u\n", __func__, idx);
218 		return ERR_PTR(-EINVAL);
219 	}
220 
221 	return cc->rclks[idx] ? &cc->rclks[idx]->hw : ERR_PTR(-ENOENT);
222 }
223 
224 int qcom_cc_really_probe(struct platform_device *pdev,
225 			 const struct qcom_cc_desc *desc, struct regmap *regmap)
226 {
227 	int i, ret;
228 	struct device *dev = &pdev->dev;
229 	struct qcom_reset_controller *reset;
230 	struct qcom_cc *cc;
231 	struct gdsc_desc *scd;
232 	size_t num_clks = desc->num_clks;
233 	struct clk_regmap **rclks = desc->clks;
234 
235 	cc = devm_kzalloc(dev, sizeof(*cc), GFP_KERNEL);
236 	if (!cc)
237 		return -ENOMEM;
238 
239 	cc->rclks = rclks;
240 	cc->num_rclks = num_clks;
241 
242 	for (i = 0; i < num_clks; i++) {
243 		if (!rclks[i])
244 			continue;
245 
246 		ret = devm_clk_register_regmap(dev, rclks[i]);
247 		if (ret)
248 			return ret;
249 	}
250 
251 	ret = of_clk_add_hw_provider(dev->of_node, qcom_cc_clk_hw_get, cc);
252 	if (ret)
253 		return ret;
254 
255 	ret = devm_add_action_or_reset(dev, qcom_cc_del_clk_provider,
256 				       pdev->dev.of_node);
257 
258 	if (ret)
259 		return ret;
260 
261 	reset = &cc->reset;
262 	reset->rcdev.of_node = dev->of_node;
263 	reset->rcdev.ops = &qcom_reset_ops;
264 	reset->rcdev.owner = dev->driver->owner;
265 	reset->rcdev.nr_resets = desc->num_resets;
266 	reset->regmap = regmap;
267 	reset->reset_map = desc->resets;
268 
269 	ret = reset_controller_register(&reset->rcdev);
270 	if (ret)
271 		return ret;
272 
273 	ret = devm_add_action_or_reset(dev, qcom_cc_reset_unregister,
274 				       &reset->rcdev);
275 
276 	if (ret)
277 		return ret;
278 
279 	if (desc->gdscs && desc->num_gdscs) {
280 		scd = devm_kzalloc(dev, sizeof(*scd), GFP_KERNEL);
281 		if (!scd)
282 			return -ENOMEM;
283 		scd->dev = dev;
284 		scd->scs = desc->gdscs;
285 		scd->num = desc->num_gdscs;
286 		ret = gdsc_register(scd, &reset->rcdev, regmap);
287 		if (ret)
288 			return ret;
289 		ret = devm_add_action_or_reset(dev, qcom_cc_gdsc_unregister,
290 					       scd);
291 		if (ret)
292 			return ret;
293 	}
294 
295 	return 0;
296 }
297 EXPORT_SYMBOL_GPL(qcom_cc_really_probe);
298 
299 int qcom_cc_probe(struct platform_device *pdev, const struct qcom_cc_desc *desc)
300 {
301 	struct regmap *regmap;
302 
303 	regmap = qcom_cc_map(pdev, desc);
304 	if (IS_ERR(regmap))
305 		return PTR_ERR(regmap);
306 
307 	return qcom_cc_really_probe(pdev, desc, regmap);
308 }
309 EXPORT_SYMBOL_GPL(qcom_cc_probe);
310 
311 MODULE_LICENSE("GPL v2");
312