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/cpu.h>
14 #include <linux/module.h>
15 #include <linux/nvmem-consumer.h>
16 #include <linux/of.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_opp.h>
19 #include <linux/slab.h>
20 
21 #define MAX_NAME_LEN	7
22 
23 #define NVMEM_MASK	0x7
24 #define NVMEM_SHIFT	5
25 
26 static struct platform_device *cpufreq_dt_pdev, *sun50i_cpufreq_pdev;
27 
28 /**
29  * sun50i_cpufreq_get_efuse() - Determine speed grade from efuse value
30  * @versions: Set to the value parsed from efuse
31  *
32  * Returns 0 if success.
33  */
34 static int sun50i_cpufreq_get_efuse(u32 *versions)
35 {
36 	struct nvmem_cell *speedbin_nvmem;
37 	struct device_node *np;
38 	struct device *cpu_dev;
39 	u32 *speedbin, efuse_value;
40 	size_t len;
41 	int ret;
42 
43 	cpu_dev = get_cpu_device(0);
44 	if (!cpu_dev)
45 		return -ENODEV;
46 
47 	np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
48 	if (!np)
49 		return -ENOENT;
50 
51 	ret = of_device_is_compatible(np,
52 				      "allwinner,sun50i-h6-operating-points");
53 	if (!ret) {
54 		of_node_put(np);
55 		return -ENOENT;
56 	}
57 
58 	speedbin_nvmem = of_nvmem_cell_get(np, NULL);
59 	of_node_put(np);
60 	if (IS_ERR(speedbin_nvmem))
61 		return dev_err_probe(cpu_dev, PTR_ERR(speedbin_nvmem),
62 				     "Could not get nvmem cell\n");
63 
64 	speedbin = nvmem_cell_read(speedbin_nvmem, &len);
65 	nvmem_cell_put(speedbin_nvmem);
66 	if (IS_ERR(speedbin))
67 		return PTR_ERR(speedbin);
68 
69 	efuse_value = (*speedbin >> NVMEM_SHIFT) & NVMEM_MASK;
70 
71 	/*
72 	 * We treat unexpected efuse values as if the SoC was from
73 	 * the slowest bin. Expected efuse values are 1-3, slowest
74 	 * to fastest.
75 	 */
76 	if (efuse_value >= 1 && efuse_value <= 3)
77 		*versions = efuse_value - 1;
78 	else
79 		*versions = 0;
80 
81 	kfree(speedbin);
82 	return 0;
83 };
84 
85 static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
86 {
87 	int *opp_tokens;
88 	char name[MAX_NAME_LEN];
89 	unsigned int cpu;
90 	u32 speed = 0;
91 	int ret;
92 
93 	opp_tokens = kcalloc(num_possible_cpus(), sizeof(*opp_tokens),
94 			     GFP_KERNEL);
95 	if (!opp_tokens)
96 		return -ENOMEM;
97 
98 	ret = sun50i_cpufreq_get_efuse(&speed);
99 	if (ret) {
100 		kfree(opp_tokens);
101 		return ret;
102 	}
103 
104 	snprintf(name, MAX_NAME_LEN, "speed%d", speed);
105 
106 	for_each_possible_cpu(cpu) {
107 		struct device *cpu_dev = get_cpu_device(cpu);
108 
109 		if (!cpu_dev) {
110 			ret = -ENODEV;
111 			goto free_opp;
112 		}
113 
114 		opp_tokens[cpu] = dev_pm_opp_set_prop_name(cpu_dev, name);
115 		if (opp_tokens[cpu] < 0) {
116 			ret = opp_tokens[cpu];
117 			pr_err("Failed to set prop name\n");
118 			goto free_opp;
119 		}
120 	}
121 
122 	cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
123 							  NULL, 0);
124 	if (!IS_ERR(cpufreq_dt_pdev)) {
125 		platform_set_drvdata(pdev, opp_tokens);
126 		return 0;
127 	}
128 
129 	ret = PTR_ERR(cpufreq_dt_pdev);
130 	pr_err("Failed to register platform device\n");
131 
132 free_opp:
133 	for_each_possible_cpu(cpu)
134 		dev_pm_opp_put_prop_name(opp_tokens[cpu]);
135 	kfree(opp_tokens);
136 
137 	return ret;
138 }
139 
140 static int sun50i_cpufreq_nvmem_remove(struct platform_device *pdev)
141 {
142 	int *opp_tokens = platform_get_drvdata(pdev);
143 	unsigned int cpu;
144 
145 	platform_device_unregister(cpufreq_dt_pdev);
146 
147 	for_each_possible_cpu(cpu)
148 		dev_pm_opp_put_prop_name(opp_tokens[cpu]);
149 
150 	kfree(opp_tokens);
151 
152 	return 0;
153 }
154 
155 static struct platform_driver sun50i_cpufreq_driver = {
156 	.probe = sun50i_cpufreq_nvmem_probe,
157 	.remove = sun50i_cpufreq_nvmem_remove,
158 	.driver = {
159 		.name = "sun50i-cpufreq-nvmem",
160 	},
161 };
162 
163 static const struct of_device_id sun50i_cpufreq_match_list[] = {
164 	{ .compatible = "allwinner,sun50i-h6" },
165 	{}
166 };
167 MODULE_DEVICE_TABLE(of, sun50i_cpufreq_match_list);
168 
169 static const struct of_device_id *sun50i_cpufreq_match_node(void)
170 {
171 	const struct of_device_id *match;
172 	struct device_node *np;
173 
174 	np = of_find_node_by_path("/");
175 	match = of_match_node(sun50i_cpufreq_match_list, np);
176 	of_node_put(np);
177 
178 	return match;
179 }
180 
181 /*
182  * Since the driver depends on nvmem drivers, which may return EPROBE_DEFER,
183  * all the real activity is done in the probe, which may be defered as well.
184  * The init here is only registering the driver and the platform device.
185  */
186 static int __init sun50i_cpufreq_init(void)
187 {
188 	const struct of_device_id *match;
189 	int ret;
190 
191 	match = sun50i_cpufreq_match_node();
192 	if (!match)
193 		return -ENODEV;
194 
195 	ret = platform_driver_register(&sun50i_cpufreq_driver);
196 	if (unlikely(ret < 0))
197 		return ret;
198 
199 	sun50i_cpufreq_pdev =
200 		platform_device_register_simple("sun50i-cpufreq-nvmem",
201 						-1, NULL, 0);
202 	ret = PTR_ERR_OR_ZERO(sun50i_cpufreq_pdev);
203 	if (ret == 0)
204 		return 0;
205 
206 	platform_driver_unregister(&sun50i_cpufreq_driver);
207 	return ret;
208 }
209 module_init(sun50i_cpufreq_init);
210 
211 static void __exit sun50i_cpufreq_exit(void)
212 {
213 	platform_device_unregister(sun50i_cpufreq_pdev);
214 	platform_driver_unregister(&sun50i_cpufreq_driver);
215 }
216 module_exit(sun50i_cpufreq_exit);
217 
218 MODULE_DESCRIPTION("Sun50i-h6 cpufreq driver");
219 MODULE_LICENSE("GPL v2");
220