1 // SPDX-License-Identifier: GPL-2.0
3 * Allwinner CPUFreq nvmem based driver
5 * The sun50i-cpufreq-nvmem driver reads the efuse value from the SoC to
6 * provide the OPP framework with required information.
8 * Copyright (C) 2019 Yangtao Li <tiny.windzz@gmail.com>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/cpu.h>
14 #include <linux/module.h>
15 #include <linux/nvmem-consumer.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_opp.h>
19 #include <linux/slab.h>
21 #define MAX_NAME_LEN 7
23 #define NVMEM_MASK 0x7
26 static struct platform_device *cpufreq_dt_pdev, *sun50i_cpufreq_pdev;
29 * sun50i_cpufreq_get_efuse() - Determine speed grade from efuse value
30 * @versions: Set to the value parsed from efuse
32 * Returns 0 if success.
34 static int sun50i_cpufreq_get_efuse(u32 *versions)
36 struct nvmem_cell *speedbin_nvmem;
37 struct device_node *np;
38 struct device *cpu_dev;
39 u32 *speedbin, efuse_value;
43 cpu_dev = get_cpu_device(0);
47 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
51 ret = of_device_is_compatible(np,
52 "allwinner,sun50i-h6-operating-points");
58 speedbin_nvmem = of_nvmem_cell_get(np, NULL);
60 if (IS_ERR(speedbin_nvmem))
61 return dev_err_probe(cpu_dev, PTR_ERR(speedbin_nvmem),
62 "Could not get nvmem cell\n");
64 speedbin = nvmem_cell_read(speedbin_nvmem, &len);
65 nvmem_cell_put(speedbin_nvmem);
67 return PTR_ERR(speedbin);
69 efuse_value = (*speedbin >> NVMEM_SHIFT) & NVMEM_MASK;
72 * We treat unexpected efuse values as if the SoC was from
73 * the slowest bin. Expected efuse values are 1-3, slowest
76 if (efuse_value >= 1 && efuse_value <= 3)
77 *versions = efuse_value - 1;
85 static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
88 char name[MAX_NAME_LEN];
93 opp_tokens = kcalloc(num_possible_cpus(), sizeof(*opp_tokens),
98 ret = sun50i_cpufreq_get_efuse(&speed);
104 snprintf(name, MAX_NAME_LEN, "speed%d", speed);
106 for_each_possible_cpu(cpu) {
107 struct device *cpu_dev = get_cpu_device(cpu);
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");
122 cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
124 if (!IS_ERR(cpufreq_dt_pdev)) {
125 platform_set_drvdata(pdev, opp_tokens);
129 ret = PTR_ERR(cpufreq_dt_pdev);
130 pr_err("Failed to register platform device\n");
133 for_each_possible_cpu(cpu)
134 dev_pm_opp_put_prop_name(opp_tokens[cpu]);
140 static int sun50i_cpufreq_nvmem_remove(struct platform_device *pdev)
142 int *opp_tokens = platform_get_drvdata(pdev);
145 platform_device_unregister(cpufreq_dt_pdev);
147 for_each_possible_cpu(cpu)
148 dev_pm_opp_put_prop_name(opp_tokens[cpu]);
155 static struct platform_driver sun50i_cpufreq_driver = {
156 .probe = sun50i_cpufreq_nvmem_probe,
157 .remove = sun50i_cpufreq_nvmem_remove,
159 .name = "sun50i-cpufreq-nvmem",
163 static const struct of_device_id sun50i_cpufreq_match_list[] = {
164 { .compatible = "allwinner,sun50i-h6" },
167 MODULE_DEVICE_TABLE(of, sun50i_cpufreq_match_list);
169 static const struct of_device_id *sun50i_cpufreq_match_node(void)
171 const struct of_device_id *match;
172 struct device_node *np;
174 np = of_find_node_by_path("/");
175 match = of_match_node(sun50i_cpufreq_match_list, np);
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.
186 static int __init sun50i_cpufreq_init(void)
188 const struct of_device_id *match;
191 match = sun50i_cpufreq_match_node();
195 ret = platform_driver_register(&sun50i_cpufreq_driver);
196 if (unlikely(ret < 0))
199 sun50i_cpufreq_pdev =
200 platform_device_register_simple("sun50i-cpufreq-nvmem",
202 ret = PTR_ERR_OR_ZERO(sun50i_cpufreq_pdev);
206 platform_driver_unregister(&sun50i_cpufreq_driver);
209 module_init(sun50i_cpufreq_init);
211 static void __exit sun50i_cpufreq_exit(void)
213 platform_device_unregister(sun50i_cpufreq_pdev);
214 platform_driver_unregister(&sun50i_cpufreq_driver);
216 module_exit(sun50i_cpufreq_exit);
218 MODULE_DESCRIPTION("Sun50i-h6 cpufreq driver");
219 MODULE_LICENSE("GPL v2");