blob: dd64dcf89c74c26a3d93924e2906a631757c292b [file] [log] [blame]
Ilia Lin46e28562018-05-30 05:39:28 +03001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4 */
5
6/*
7 * In Certain QCOM SoCs like apq8096 and msm8996 that have KRYO processors,
8 * the CPU frequency subset and voltage value of each OPP varies
9 * based on the silicon variant in use. Qualcomm Process Voltage Scaling Tables
10 * defines the voltage and frequency value based on the msm-id in SMEM
11 * and speedbin blown in the efuse combination.
12 * The qcom-cpufreq-kryo driver reads the msm-id and efuse value from the SoC
13 * to provide the OPP framework with required information.
14 * This is used to determine the voltage and frequency value for each OPP of
15 * operating-points-v2 table when it is parsed by the OPP framework.
16 */
17
18#include <linux/cpu.h>
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/nvmem-consumer.h>
24#include <linux/of.h>
25#include <linux/platform_device.h>
26#include <linux/pm_opp.h>
27#include <linux/slab.h>
28#include <linux/soc/qcom/smem.h>
29
30#define MSM_ID_SMEM 137
31
32enum _msm_id {
33 MSM8996V3 = 0xF6ul,
34 APQ8096V3 = 0x123ul,
35 MSM8996SG = 0x131ul,
36 APQ8096SG = 0x138ul,
37};
38
39enum _msm8996_version {
40 MSM8996_V3,
41 MSM8996_SG,
42 NUM_OF_MSM8996_VERSIONS,
43};
44
Yangtao Li50c0b122019-02-04 01:13:10 -050045static struct platform_device *cpufreq_dt_pdev, *kryo_cpufreq_pdev;
Ilia Lin5ad73462018-06-17 22:01:46 +020046
Nathan Chancellord51aea12018-09-19 17:22:21 -070047static enum _msm8996_version qcom_cpufreq_kryo_get_msm_id(void)
Ilia Lin46e28562018-05-30 05:39:28 +030048{
49 size_t len;
50 u32 *msm_id;
51 enum _msm8996_version version;
52
53 msm_id = qcom_smem_get(QCOM_SMEM_HOST_ANY, MSM_ID_SMEM, &len);
54 if (IS_ERR(msm_id))
55 return NUM_OF_MSM8996_VERSIONS;
56
57 /* The first 4 bytes are format, next to them is the actual msm-id */
58 msm_id++;
59
60 switch ((enum _msm_id)*msm_id) {
61 case MSM8996V3:
62 case APQ8096V3:
63 version = MSM8996_V3;
64 break;
65 case MSM8996SG:
66 case APQ8096SG:
67 version = MSM8996_SG;
68 break;
69 default:
70 version = NUM_OF_MSM8996_VERSIONS;
71 }
72
73 return version;
74}
75
76static int qcom_cpufreq_kryo_probe(struct platform_device *pdev)
77{
Viresh Kumar03349062019-02-20 16:41:18 +053078 struct opp_table **opp_tables;
Ilia Lin46e28562018-05-30 05:39:28 +030079 enum _msm8996_version msm8996_version;
80 struct nvmem_cell *speedbin_nvmem;
81 struct device_node *np;
82 struct device *cpu_dev;
83 unsigned cpu;
84 u8 *speedbin;
85 u32 versions;
86 size_t len;
87 int ret;
88
89 cpu_dev = get_cpu_device(0);
Dan Carpenter1dd20582018-06-21 11:06:41 +030090 if (!cpu_dev)
91 return -ENODEV;
Ilia Lin46e28562018-05-30 05:39:28 +030092
93 msm8996_version = qcom_cpufreq_kryo_get_msm_id();
94 if (NUM_OF_MSM8996_VERSIONS == msm8996_version) {
95 dev_err(cpu_dev, "Not Snapdragon 820/821!");
96 return -ENODEV;
97 }
98
99 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
Dan Carpenter1dd20582018-06-21 11:06:41 +0300100 if (!np)
101 return -ENOENT;
Ilia Lin46e28562018-05-30 05:39:28 +0300102
103 ret = of_device_is_compatible(np, "operating-points-v2-kryo-cpu");
104 if (!ret) {
105 of_node_put(np);
106 return -ENOENT;
107 }
108
109 speedbin_nvmem = of_nvmem_cell_get(np, NULL);
110 of_node_put(np);
111 if (IS_ERR(speedbin_nvmem)) {
Niklas Casself54ab692018-07-17 22:48:21 +0200112 if (PTR_ERR(speedbin_nvmem) != -EPROBE_DEFER)
113 dev_err(cpu_dev, "Could not get nvmem cell: %ld\n",
114 PTR_ERR(speedbin_nvmem));
Ilia Lin46e28562018-05-30 05:39:28 +0300115 return PTR_ERR(speedbin_nvmem);
116 }
117
118 speedbin = nvmem_cell_read(speedbin_nvmem, &len);
119 nvmem_cell_put(speedbin_nvmem);
Ilia Linee3dbcf2018-06-17 21:58:42 +0200120 if (IS_ERR(speedbin))
121 return PTR_ERR(speedbin);
Ilia Lin46e28562018-05-30 05:39:28 +0300122
123 switch (msm8996_version) {
124 case MSM8996_V3:
125 versions = 1 << (unsigned int)(*speedbin);
126 break;
127 case MSM8996_SG:
128 versions = 1 << ((unsigned int)(*speedbin) + 4);
129 break;
130 default:
131 BUG();
132 break;
133 }
Ilia Linee3dbcf2018-06-17 21:58:42 +0200134 kfree(speedbin);
Ilia Lin46e28562018-05-30 05:39:28 +0300135
Viresh Kumar03349062019-02-20 16:41:18 +0530136 opp_tables = kcalloc(num_possible_cpus(), sizeof(*opp_tables), GFP_KERNEL);
137 if (!opp_tables)
138 return -ENOMEM;
139
Ilia Lin46e28562018-05-30 05:39:28 +0300140 for_each_possible_cpu(cpu) {
141 cpu_dev = get_cpu_device(cpu);
142 if (NULL == cpu_dev) {
143 ret = -ENODEV;
144 goto free_opp;
145 }
146
147 opp_tables[cpu] = dev_pm_opp_set_supported_hw(cpu_dev,
148 &versions, 1);
149 if (IS_ERR(opp_tables[cpu])) {
150 ret = PTR_ERR(opp_tables[cpu]);
151 dev_err(cpu_dev, "Failed to set supported hardware\n");
152 goto free_opp;
153 }
154 }
155
156 cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
157 NULL, 0);
Viresh Kumar03349062019-02-20 16:41:18 +0530158 if (!IS_ERR(cpufreq_dt_pdev)) {
159 platform_set_drvdata(pdev, opp_tables);
Ilia Lin46e28562018-05-30 05:39:28 +0300160 return 0;
Viresh Kumar03349062019-02-20 16:41:18 +0530161 }
Ilia Lin46e28562018-05-30 05:39:28 +0300162
163 ret = PTR_ERR(cpufreq_dt_pdev);
164 dev_err(cpu_dev, "Failed to register platform device\n");
165
166free_opp:
167 for_each_possible_cpu(cpu) {
168 if (IS_ERR_OR_NULL(opp_tables[cpu]))
169 break;
170 dev_pm_opp_put_supported_hw(opp_tables[cpu]);
171 }
Viresh Kumar03349062019-02-20 16:41:18 +0530172 kfree(opp_tables);
Ilia Lin46e28562018-05-30 05:39:28 +0300173
174 return ret;
175}
176
Ilia Lin5ad73462018-06-17 22:01:46 +0200177static int qcom_cpufreq_kryo_remove(struct platform_device *pdev)
178{
Viresh Kumar03349062019-02-20 16:41:18 +0530179 struct opp_table **opp_tables = platform_get_drvdata(pdev);
180 unsigned int cpu;
181
Ilia Lin5ad73462018-06-17 22:01:46 +0200182 platform_device_unregister(cpufreq_dt_pdev);
Viresh Kumar03349062019-02-20 16:41:18 +0530183
184 for_each_possible_cpu(cpu)
185 dev_pm_opp_put_supported_hw(opp_tables[cpu]);
186
187 kfree(opp_tables);
188
Ilia Lin5ad73462018-06-17 22:01:46 +0200189 return 0;
190}
191
Ilia Lin46e28562018-05-30 05:39:28 +0300192static struct platform_driver qcom_cpufreq_kryo_driver = {
193 .probe = qcom_cpufreq_kryo_probe,
Ilia Lin5ad73462018-06-17 22:01:46 +0200194 .remove = qcom_cpufreq_kryo_remove,
Ilia Lin46e28562018-05-30 05:39:28 +0300195 .driver = {
196 .name = "qcom-cpufreq-kryo",
197 },
198};
199
200static const struct of_device_id qcom_cpufreq_kryo_match_list[] __initconst = {
201 { .compatible = "qcom,apq8096", },
202 { .compatible = "qcom,msm8996", },
YueHaibingbafaf052018-07-23 20:34:29 +0800203 {}
Ilia Lin46e28562018-05-30 05:39:28 +0300204};
205
206/*
207 * Since the driver depends on smem and nvmem drivers, which may
208 * return EPROBE_DEFER, all the real activity is done in the probe,
209 * which may be defered as well. The init here is only registering
210 * the driver and the platform device.
211 */
212static int __init qcom_cpufreq_kryo_init(void)
213{
214 struct device_node *np = of_find_node_by_path("/");
215 const struct of_device_id *match;
216 int ret;
217
218 if (!np)
219 return -ENODEV;
220
221 match = of_match_node(qcom_cpufreq_kryo_match_list, np);
222 of_node_put(np);
223 if (!match)
224 return -ENODEV;
225
226 ret = platform_driver_register(&qcom_cpufreq_kryo_driver);
227 if (unlikely(ret < 0))
228 return ret;
229
Ilia Lin5ad73462018-06-17 22:01:46 +0200230 kryo_cpufreq_pdev = platform_device_register_simple(
231 "qcom-cpufreq-kryo", -1, NULL, 0);
232 ret = PTR_ERR_OR_ZERO(kryo_cpufreq_pdev);
Ilia Lin46e28562018-05-30 05:39:28 +0300233 if (0 == ret)
234 return 0;
235
236 platform_driver_unregister(&qcom_cpufreq_kryo_driver);
237 return ret;
238}
239module_init(qcom_cpufreq_kryo_init);
240
Nathan Chancellord51aea12018-09-19 17:22:21 -0700241static void __exit qcom_cpufreq_kryo_exit(void)
Ilia Lin5ad73462018-06-17 22:01:46 +0200242{
243 platform_device_unregister(kryo_cpufreq_pdev);
244 platform_driver_unregister(&qcom_cpufreq_kryo_driver);
245}
246module_exit(qcom_cpufreq_kryo_exit);
247
Ilia Lin46e28562018-05-30 05:39:28 +0300248MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Kryo CPUfreq driver");
249MODULE_LICENSE("GPL v2");