xref: /openbmc/linux/drivers/clk/qcom/common.c (revision 781095f903f398148cd0b646d3984234a715f29e)
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_onecell_data data;
31 	struct clk *clks[];
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 int qcom_find_src_index(struct clk_hw *hw, const struct parent_map *map, u8 src)
50 {
51 	int i, num_parents = clk_hw_get_num_parents(hw);
52 
53 	for (i = 0; i < num_parents; i++)
54 		if (src == map[i].src)
55 			return i;
56 
57 	return -ENOENT;
58 }
59 EXPORT_SYMBOL_GPL(qcom_find_src_index);
60 
61 struct regmap *
62 qcom_cc_map(struct platform_device *pdev, const struct qcom_cc_desc *desc)
63 {
64 	void __iomem *base;
65 	struct resource *res;
66 	struct device *dev = &pdev->dev;
67 
68 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
69 	base = devm_ioremap_resource(dev, res);
70 	if (IS_ERR(base))
71 		return ERR_CAST(base);
72 
73 	return devm_regmap_init_mmio(dev, base, desc->config);
74 }
75 EXPORT_SYMBOL_GPL(qcom_cc_map);
76 
77 static void qcom_cc_del_clk_provider(void *data)
78 {
79 	of_clk_del_provider(data);
80 }
81 
82 static void qcom_cc_reset_unregister(void *data)
83 {
84 	reset_controller_unregister(data);
85 }
86 
87 static void qcom_cc_gdsc_unregister(void *data)
88 {
89 	gdsc_unregister(data);
90 }
91 
92 /*
93  * Backwards compatibility with old DTs. Register a pass-through factor 1/1
94  * clock to translate 'path' clk into 'name' clk and regsiter the 'path'
95  * clk as a fixed rate clock if it isn't present.
96  */
97 static int _qcom_cc_register_board_clk(struct device *dev, const char *path,
98 				       const char *name, unsigned long rate,
99 				       bool add_factor)
100 {
101 	struct device_node *node = NULL;
102 	struct device_node *clocks_node;
103 	struct clk_fixed_factor *factor;
104 	struct clk_fixed_rate *fixed;
105 	struct clk *clk;
106 	struct clk_init_data init_data = { };
107 
108 	clocks_node = of_find_node_by_path("/clocks");
109 	if (clocks_node)
110 		node = of_find_node_by_name(clocks_node, path);
111 	of_node_put(clocks_node);
112 
113 	if (!node) {
114 		fixed = devm_kzalloc(dev, sizeof(*fixed), GFP_KERNEL);
115 		if (!fixed)
116 			return -EINVAL;
117 
118 		fixed->fixed_rate = rate;
119 		fixed->hw.init = &init_data;
120 
121 		init_data.name = path;
122 		init_data.flags = CLK_IS_ROOT;
123 		init_data.ops = &clk_fixed_rate_ops;
124 
125 		clk = devm_clk_register(dev, &fixed->hw);
126 		if (IS_ERR(clk))
127 			return PTR_ERR(clk);
128 	}
129 	of_node_put(node);
130 
131 	if (add_factor) {
132 		factor = devm_kzalloc(dev, sizeof(*factor), GFP_KERNEL);
133 		if (!factor)
134 			return -EINVAL;
135 
136 		factor->mult = factor->div = 1;
137 		factor->hw.init = &init_data;
138 
139 		init_data.name = name;
140 		init_data.parent_names = &path;
141 		init_data.num_parents = 1;
142 		init_data.flags = 0;
143 		init_data.ops = &clk_fixed_factor_ops;
144 
145 		clk = devm_clk_register(dev, &factor->hw);
146 		if (IS_ERR(clk))
147 			return PTR_ERR(clk);
148 	}
149 
150 	return 0;
151 }
152 
153 int qcom_cc_register_board_clk(struct device *dev, const char *path,
154 			       const char *name, unsigned long rate)
155 {
156 	bool add_factor = true;
157 	struct device_node *node;
158 
159 	/* The RPM clock driver will add the factor clock if present */
160 	if (IS_ENABLED(CONFIG_QCOM_RPMCC)) {
161 		node = of_find_compatible_node(NULL, NULL, "qcom,rpmcc");
162 		if (of_device_is_available(node))
163 			add_factor = false;
164 		of_node_put(node);
165 	}
166 
167 	return _qcom_cc_register_board_clk(dev, path, name, rate, add_factor);
168 }
169 EXPORT_SYMBOL_GPL(qcom_cc_register_board_clk);
170 
171 int qcom_cc_register_sleep_clk(struct device *dev)
172 {
173 	return _qcom_cc_register_board_clk(dev, "sleep_clk", "sleep_clk_src",
174 					   32768, true);
175 }
176 EXPORT_SYMBOL_GPL(qcom_cc_register_sleep_clk);
177 
178 int qcom_cc_really_probe(struct platform_device *pdev,
179 			 const struct qcom_cc_desc *desc, struct regmap *regmap)
180 {
181 	int i, ret;
182 	struct device *dev = &pdev->dev;
183 	struct clk *clk;
184 	struct clk_onecell_data *data;
185 	struct clk **clks;
186 	struct qcom_reset_controller *reset;
187 	struct qcom_cc *cc;
188 	size_t num_clks = desc->num_clks;
189 	struct clk_regmap **rclks = desc->clks;
190 
191 	cc = devm_kzalloc(dev, sizeof(*cc) + sizeof(*clks) * num_clks,
192 			  GFP_KERNEL);
193 	if (!cc)
194 		return -ENOMEM;
195 
196 	clks = cc->clks;
197 	data = &cc->data;
198 	data->clks = clks;
199 	data->clk_num = num_clks;
200 
201 	for (i = 0; i < num_clks; i++) {
202 		if (!rclks[i]) {
203 			clks[i] = ERR_PTR(-ENOENT);
204 			continue;
205 		}
206 		clk = devm_clk_register_regmap(dev, rclks[i]);
207 		if (IS_ERR(clk))
208 			return PTR_ERR(clk);
209 		clks[i] = clk;
210 	}
211 
212 	ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get, data);
213 	if (ret)
214 		return ret;
215 
216 	devm_add_action(dev, qcom_cc_del_clk_provider, pdev->dev.of_node);
217 
218 	reset = &cc->reset;
219 	reset->rcdev.of_node = dev->of_node;
220 	reset->rcdev.ops = &qcom_reset_ops;
221 	reset->rcdev.owner = dev->driver->owner;
222 	reset->rcdev.nr_resets = desc->num_resets;
223 	reset->regmap = regmap;
224 	reset->reset_map = desc->resets;
225 
226 	ret = reset_controller_register(&reset->rcdev);
227 	if (ret)
228 		return ret;
229 
230 	devm_add_action(dev, qcom_cc_reset_unregister, &reset->rcdev);
231 
232 	if (desc->gdscs && desc->num_gdscs) {
233 		ret = gdsc_register(dev, desc->gdscs, desc->num_gdscs,
234 				    &reset->rcdev, regmap);
235 		if (ret)
236 			return ret;
237 	}
238 
239 	devm_add_action(dev, qcom_cc_gdsc_unregister, dev);
240 
241 
242 	return 0;
243 }
244 EXPORT_SYMBOL_GPL(qcom_cc_really_probe);
245 
246 int qcom_cc_probe(struct platform_device *pdev, const struct qcom_cc_desc *desc)
247 {
248 	struct regmap *regmap;
249 
250 	regmap = qcom_cc_map(pdev, desc);
251 	if (IS_ERR(regmap))
252 		return PTR_ERR(regmap);
253 
254 	return qcom_cc_really_probe(pdev, desc, regmap);
255 }
256 EXPORT_SYMBOL_GPL(qcom_cc_probe);
257 
258 MODULE_LICENSE("GPL v2");
259