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