xref: /linux/drivers/cpufreq/sun50i-cpufreq-nvmem.c (revision 2697b79a469b68e3ad3640f55284359c1396278d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Allwinner CPUFreq nvmem based driver
4  *
5  * The sun50i-cpufreq-nvmem driver reads the efuse value from the SoC to
6  * provide the OPP framework with required information.
7  *
8  * Copyright (C) 2019 Yangtao Li <tiny.windzz@gmail.com>
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/arm-smccc.h>
14 #include <linux/cpu.h>
15 #include <linux/module.h>
16 #include <linux/nvmem-consumer.h>
17 #include <linux/of.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_opp.h>
20 #include <linux/slab.h>
21 
22 #define NVMEM_MASK	0x7
23 #define NVMEM_SHIFT	5
24 
25 static struct platform_device *cpufreq_dt_pdev, *sun50i_cpufreq_pdev;
26 
27 struct sunxi_cpufreq_data {
28 	u32 (*efuse_xlate)(u32 speedbin);
29 };
30 
31 static u32 sun50i_h6_efuse_xlate(u32 speedbin)
32 {
33 	u32 efuse_value;
34 
35 	efuse_value = (speedbin >> NVMEM_SHIFT) & NVMEM_MASK;
36 
37 	/*
38 	 * We treat unexpected efuse values as if the SoC was from
39 	 * the slowest bin. Expected efuse values are 1-3, slowest
40 	 * to fastest.
41 	 */
42 	if (efuse_value >= 1 && efuse_value <= 3)
43 		return efuse_value - 1;
44 	else
45 		return 0;
46 }
47 
48 static int get_soc_id_revision(void)
49 {
50 #ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY
51 	return arm_smccc_get_soc_id_revision();
52 #else
53 	return SMCCC_RET_NOT_SUPPORTED;
54 #endif
55 }
56 
57 /*
58  * Judging by the OPP tables in the vendor BSP, the quality order of the
59  * returned speedbin index is 4 -> 0/2 -> 3 -> 1, from worst to best.
60  * 0 and 2 seem identical from the OPP tables' point of view.
61  */
62 static u32 sun50i_h616_efuse_xlate(u32 speedbin)
63 {
64 	int ver_bits = get_soc_id_revision();
65 	u32 value = 0;
66 
67 	switch (speedbin & 0xffff) {
68 	case 0x2000:
69 		value = 0;
70 		break;
71 	case 0x2400:
72 	case 0x7400:
73 	case 0x2c00:
74 	case 0x7c00:
75 		if (ver_bits != SMCCC_RET_NOT_SUPPORTED && ver_bits <= 1) {
76 			/* ic version A/B */
77 			value = 1;
78 		} else {
79 			/* ic version C and later version */
80 			value = 2;
81 		}
82 		break;
83 	case 0x5000:
84 	case 0x5400:
85 	case 0x6000:
86 		value = 3;
87 		break;
88 	case 0x5c00:
89 		value = 4;
90 		break;
91 	case 0x5d00:
92 		value = 0;
93 		break;
94 	default:
95 		pr_warn("sun50i-cpufreq-nvmem: unknown speed bin 0x%x, using default bin 0\n",
96 			speedbin & 0xffff);
97 		value = 0;
98 		break;
99 	}
100 
101 	return value;
102 }
103 
104 static struct sunxi_cpufreq_data sun50i_h6_cpufreq_data = {
105 	.efuse_xlate = sun50i_h6_efuse_xlate,
106 };
107 
108 static struct sunxi_cpufreq_data sun50i_h616_cpufreq_data = {
109 	.efuse_xlate = sun50i_h616_efuse_xlate,
110 };
111 
112 static const struct of_device_id cpu_opp_match_list[] = {
113 	{ .compatible = "allwinner,sun50i-h6-operating-points",
114 	  .data = &sun50i_h6_cpufreq_data,
115 	},
116 	{ .compatible = "allwinner,sun50i-h616-operating-points",
117 	  .data = &sun50i_h616_cpufreq_data,
118 	},
119 	{}
120 };
121 
122 /**
123  * dt_has_supported_hw() - Check if any OPPs use opp-supported-hw
124  *
125  * If we ask the cpufreq framework to use the opp-supported-hw feature, it
126  * will ignore every OPP node without that DT property. If none of the OPPs
127  * have it, the driver will fail probing, due to the lack of OPPs.
128  *
129  * Returns true if we have at least one OPP with the opp-supported-hw property.
130  */
131 static bool dt_has_supported_hw(void)
132 {
133 	bool has_opp_supported_hw = false;
134 	struct device_node *np, *opp;
135 	struct device *cpu_dev;
136 
137 	cpu_dev = get_cpu_device(0);
138 	if (!cpu_dev)
139 		return false;
140 
141 	np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
142 	if (!np)
143 		return false;
144 
145 	for_each_child_of_node(np, opp) {
146 		if (of_find_property(opp, "opp-supported-hw", NULL)) {
147 			has_opp_supported_hw = true;
148 			break;
149 		}
150 	}
151 
152 	of_node_put(np);
153 
154 	return has_opp_supported_hw;
155 }
156 
157 /**
158  * sun50i_cpufreq_get_efuse() - Determine speed grade from efuse value
159  *
160  * Returns non-negative speed bin index on success, a negative error
161  * value otherwise.
162  */
163 static int sun50i_cpufreq_get_efuse(void)
164 {
165 	const struct sunxi_cpufreq_data *opp_data;
166 	struct nvmem_cell *speedbin_nvmem;
167 	const struct of_device_id *match;
168 	struct device_node *np;
169 	struct device *cpu_dev;
170 	u32 *speedbin;
171 	int ret;
172 
173 	cpu_dev = get_cpu_device(0);
174 	if (!cpu_dev)
175 		return -ENODEV;
176 
177 	np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
178 	if (!np)
179 		return -ENOENT;
180 
181 	match = of_match_node(cpu_opp_match_list, np);
182 	if (!match) {
183 		of_node_put(np);
184 		return -ENOENT;
185 	}
186 	opp_data = match->data;
187 
188 	speedbin_nvmem = of_nvmem_cell_get(np, NULL);
189 	of_node_put(np);
190 	if (IS_ERR(speedbin_nvmem))
191 		return dev_err_probe(cpu_dev, PTR_ERR(speedbin_nvmem),
192 				     "Could not get nvmem cell\n");
193 
194 	speedbin = nvmem_cell_read(speedbin_nvmem, NULL);
195 	nvmem_cell_put(speedbin_nvmem);
196 	if (IS_ERR(speedbin))
197 		return PTR_ERR(speedbin);
198 
199 	ret = opp_data->efuse_xlate(*speedbin);
200 
201 	kfree(speedbin);
202 
203 	return ret;
204 };
205 
206 static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
207 {
208 	int *opp_tokens;
209 	char name[] = "speedXXXXXXXXXXX"; /* Integers can take 11 chars max */
210 	unsigned int cpu, supported_hw;
211 	struct dev_pm_opp_config config = {};
212 	int speed;
213 	int ret;
214 
215 	opp_tokens = kcalloc(num_possible_cpus(), sizeof(*opp_tokens),
216 			     GFP_KERNEL);
217 	if (!opp_tokens)
218 		return -ENOMEM;
219 
220 	speed = sun50i_cpufreq_get_efuse();
221 	if (speed < 0) {
222 		kfree(opp_tokens);
223 		return speed;
224 	}
225 
226 	/*
227 	 * We need at least one OPP with the "opp-supported-hw" property,
228 	 * or else the upper layers will ignore every OPP and will bail out.
229 	 */
230 	if (dt_has_supported_hw()) {
231 		supported_hw = 1U << speed;
232 		config.supported_hw = &supported_hw;
233 		config.supported_hw_count = 1;
234 	}
235 
236 	snprintf(name, sizeof(name), "speed%d", speed);
237 	config.prop_name = name;
238 
239 	for_each_possible_cpu(cpu) {
240 		struct device *cpu_dev = get_cpu_device(cpu);
241 
242 		if (!cpu_dev) {
243 			ret = -ENODEV;
244 			goto free_opp;
245 		}
246 
247 		ret = dev_pm_opp_set_config(cpu_dev, &config);
248 		if (ret < 0)
249 			goto free_opp;
250 
251 		opp_tokens[cpu] = ret;
252 	}
253 
254 	cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
255 							  NULL, 0);
256 	if (!IS_ERR(cpufreq_dt_pdev)) {
257 		platform_set_drvdata(pdev, opp_tokens);
258 		return 0;
259 	}
260 
261 	ret = PTR_ERR(cpufreq_dt_pdev);
262 	pr_err("Failed to register platform device\n");
263 
264 free_opp:
265 	for_each_possible_cpu(cpu)
266 		dev_pm_opp_clear_config(opp_tokens[cpu]);
267 	kfree(opp_tokens);
268 
269 	return ret;
270 }
271 
272 static void sun50i_cpufreq_nvmem_remove(struct platform_device *pdev)
273 {
274 	int *opp_tokens = platform_get_drvdata(pdev);
275 	unsigned int cpu;
276 
277 	platform_device_unregister(cpufreq_dt_pdev);
278 
279 	for_each_possible_cpu(cpu)
280 		dev_pm_opp_clear_config(opp_tokens[cpu]);
281 
282 	kfree(opp_tokens);
283 }
284 
285 static struct platform_driver sun50i_cpufreq_driver = {
286 	.probe = sun50i_cpufreq_nvmem_probe,
287 	.remove_new = sun50i_cpufreq_nvmem_remove,
288 	.driver = {
289 		.name = "sun50i-cpufreq-nvmem",
290 	},
291 };
292 
293 static const struct of_device_id sun50i_cpufreq_match_list[] = {
294 	{ .compatible = "allwinner,sun50i-h6" },
295 	{ .compatible = "allwinner,sun50i-h616" },
296 	{ .compatible = "allwinner,sun50i-h618" },
297 	{ .compatible = "allwinner,sun50i-h700" },
298 	{}
299 };
300 MODULE_DEVICE_TABLE(of, sun50i_cpufreq_match_list);
301 
302 static const struct of_device_id *sun50i_cpufreq_match_node(void)
303 {
304 	const struct of_device_id *match;
305 	struct device_node *np;
306 
307 	np = of_find_node_by_path("/");
308 	match = of_match_node(sun50i_cpufreq_match_list, np);
309 	of_node_put(np);
310 
311 	return match;
312 }
313 
314 /*
315  * Since the driver depends on nvmem drivers, which may return EPROBE_DEFER,
316  * all the real activity is done in the probe, which may be defered as well.
317  * The init here is only registering the driver and the platform device.
318  */
319 static int __init sun50i_cpufreq_init(void)
320 {
321 	const struct of_device_id *match;
322 	int ret;
323 
324 	match = sun50i_cpufreq_match_node();
325 	if (!match)
326 		return -ENODEV;
327 
328 	ret = platform_driver_register(&sun50i_cpufreq_driver);
329 	if (unlikely(ret < 0))
330 		return ret;
331 
332 	sun50i_cpufreq_pdev =
333 		platform_device_register_simple("sun50i-cpufreq-nvmem",
334 						-1, NULL, 0);
335 	ret = PTR_ERR_OR_ZERO(sun50i_cpufreq_pdev);
336 	if (ret == 0)
337 		return 0;
338 
339 	platform_driver_unregister(&sun50i_cpufreq_driver);
340 	return ret;
341 }
342 module_init(sun50i_cpufreq_init);
343 
344 static void __exit sun50i_cpufreq_exit(void)
345 {
346 	platform_device_unregister(sun50i_cpufreq_pdev);
347 	platform_driver_unregister(&sun50i_cpufreq_driver);
348 }
349 module_exit(sun50i_cpufreq_exit);
350 
351 MODULE_DESCRIPTION("Sun50i-h6 cpufreq driver");
352 MODULE_LICENSE("GPL v2");
353