blob: 05f3d7876e448d8db78ecd16b07ee4f4fd671bb3 [file] [log] [blame]
Taniya Das2849dd82018-12-14 09:40:24 +05301// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4 */
5
6#include <linux/bitfield.h>
7#include <linux/cpufreq.h>
8#include <linux/init.h>
Sibi Sankar51c843c2020-06-22 13:46:48 +05309#include <linux/interconnect.h>
Thara Gopinath275157b2021-08-09 15:16:01 -040010#include <linux/interrupt.h>
Taniya Das2849dd82018-12-14 09:40:24 +053011#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/of_address.h>
14#include <linux/of_platform.h>
Taniya Das55538fb2019-01-31 23:02:50 +053015#include <linux/pm_opp.h>
Taniya Das2849dd82018-12-14 09:40:24 +053016#include <linux/slab.h>
Thara Gopinath275157b2021-08-09 15:16:01 -040017#include <linux/spinlock.h>
Taniya Das2849dd82018-12-14 09:40:24 +053018
19#define LUT_MAX_ENTRIES 40U
20#define LUT_SRC GENMASK(31, 30)
21#define LUT_L_VAL GENMASK(7, 0)
22#define LUT_CORE_COUNT GENMASK(18, 16)
Taniya Das55538fb2019-01-31 23:02:50 +053023#define LUT_VOLT GENMASK(11, 0)
Taniya Das2849dd82018-12-14 09:40:24 +053024#define CLK_HW_DIV 2
Sibi Sankar0eae1e32019-08-07 17:15:43 +053025#define LUT_TURBO_IND 1
Taniya Das2849dd82018-12-14 09:40:24 +053026
Thara Gopinath275157b2021-08-09 15:16:01 -040027#define HZ_PER_KHZ 1000
28
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +053029struct qcom_cpufreq_soc_data {
30 u32 reg_enable;
31 u32 reg_freq_lut;
32 u32 reg_volt_lut;
Thara Gopinath275157b2021-08-09 15:16:01 -040033 u32 reg_current_vote;
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +053034 u32 reg_perf_state;
35 u8 lut_row_size;
36};
37
38struct qcom_cpufreq_data {
39 void __iomem *base;
Shawn Guo67fc2092021-01-19 10:39:25 +080040 struct resource *res;
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +053041 const struct qcom_cpufreq_soc_data *soc_data;
Thara Gopinath275157b2021-08-09 15:16:01 -040042
43 /*
44 * Mutex to synchronize between de-init sequence and re-starting LMh
45 * polling/interrupts
46 */
47 struct mutex throttle_lock;
48 int throttle_irq;
Ard Biesheuvelbe6592e2021-11-11 17:48:06 +020049 char irq_name[15];
Thara Gopinath275157b2021-08-09 15:16:01 -040050 bool cancel_throttle;
51 struct delayed_work throttle_work;
52 struct cpufreq_policy *policy;
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +053053};
Taniya Das2849dd82018-12-14 09:40:24 +053054
55static unsigned long cpu_hw_rate, xo_rate;
Sibi Sankar51c843c2020-06-22 13:46:48 +053056static bool icc_scaling_enabled;
57
58static int qcom_cpufreq_set_bw(struct cpufreq_policy *policy,
59 unsigned long freq_khz)
60{
61 unsigned long freq_hz = freq_khz * 1000;
62 struct dev_pm_opp *opp;
63 struct device *dev;
64 int ret;
65
66 dev = get_cpu_device(policy->cpu);
67 if (!dev)
68 return -ENODEV;
69
70 opp = dev_pm_opp_find_freq_exact(dev, freq_hz, true);
71 if (IS_ERR(opp))
72 return PTR_ERR(opp);
73
Viresh Kumar8d251572021-01-21 15:27:55 +053074 ret = dev_pm_opp_set_opp(dev, opp);
Sibi Sankar51c843c2020-06-22 13:46:48 +053075 dev_pm_opp_put(opp);
76 return ret;
77}
78
79static int qcom_cpufreq_update_opp(struct device *cpu_dev,
80 unsigned long freq_khz,
81 unsigned long volt)
82{
83 unsigned long freq_hz = freq_khz * 1000;
84 int ret;
85
86 /* Skip voltage update if the opp table is not available */
87 if (!icc_scaling_enabled)
88 return dev_pm_opp_add(cpu_dev, freq_hz, volt);
89
90 ret = dev_pm_opp_adjust_voltage(cpu_dev, freq_hz, volt, volt, volt);
91 if (ret) {
92 dev_err(cpu_dev, "Voltage update failed freq=%ld\n", freq_khz);
93 return ret;
94 }
95
96 return dev_pm_opp_enable(cpu_dev, freq_hz);
97}
Taniya Das2849dd82018-12-14 09:40:24 +053098
99static int qcom_cpufreq_hw_target_index(struct cpufreq_policy *policy,
100 unsigned int index)
101{
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530102 struct qcom_cpufreq_data *data = policy->driver_data;
103 const struct qcom_cpufreq_soc_data *soc_data = data->soc_data;
Douglas RAILLARDada54f32019-08-08 14:18:57 +0100104 unsigned long freq = policy->freq_table[index].frequency;
Taniya Das2849dd82018-12-14 09:40:24 +0530105
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530106 writel_relaxed(index, data->base + soc_data->reg_perf_state);
Taniya Das2849dd82018-12-14 09:40:24 +0530107
Sibi Sankar51c843c2020-06-22 13:46:48 +0530108 if (icc_scaling_enabled)
109 qcom_cpufreq_set_bw(policy, freq);
110
Taniya Das2849dd82018-12-14 09:40:24 +0530111 return 0;
112}
113
114static unsigned int qcom_cpufreq_hw_get(unsigned int cpu)
115{
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530116 struct qcom_cpufreq_data *data;
117 const struct qcom_cpufreq_soc_data *soc_data;
Taniya Das2849dd82018-12-14 09:40:24 +0530118 struct cpufreq_policy *policy;
119 unsigned int index;
120
121 policy = cpufreq_cpu_get_raw(cpu);
122 if (!policy)
123 return 0;
124
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530125 data = policy->driver_data;
126 soc_data = data->soc_data;
Taniya Das2849dd82018-12-14 09:40:24 +0530127
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530128 index = readl_relaxed(data->base + soc_data->reg_perf_state);
Taniya Das2849dd82018-12-14 09:40:24 +0530129 index = min(index, LUT_MAX_ENTRIES - 1);
130
131 return policy->freq_table[index].frequency;
132}
133
134static unsigned int qcom_cpufreq_hw_fast_switch(struct cpufreq_policy *policy,
135 unsigned int target_freq)
136{
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530137 struct qcom_cpufreq_data *data = policy->driver_data;
138 const struct qcom_cpufreq_soc_data *soc_data = data->soc_data;
Viresh Kumar292072c2020-07-30 08:59:40 +0530139 unsigned int index;
Taniya Das2849dd82018-12-14 09:40:24 +0530140
141 index = policy->cached_resolved_idx;
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530142 writel_relaxed(index, data->base + soc_data->reg_perf_state);
Taniya Das2849dd82018-12-14 09:40:24 +0530143
Ionela Voinescu1a0419b2020-09-01 21:55:46 +0100144 return policy->freq_table[index].frequency;
Taniya Das2849dd82018-12-14 09:40:24 +0530145}
146
Taniya Das55538fb2019-01-31 23:02:50 +0530147static int qcom_cpufreq_hw_read_lut(struct device *cpu_dev,
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530148 struct cpufreq_policy *policy)
Taniya Das2849dd82018-12-14 09:40:24 +0530149{
Sibi Sankar0eae1e32019-08-07 17:15:43 +0530150 u32 data, src, lval, i, core_count, prev_freq = 0, freq;
Taniya Das55538fb2019-01-31 23:02:50 +0530151 u32 volt;
Taniya Das2849dd82018-12-14 09:40:24 +0530152 struct cpufreq_frequency_table *table;
Sibi Sankar51c843c2020-06-22 13:46:48 +0530153 struct dev_pm_opp *opp;
154 unsigned long rate;
155 int ret;
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530156 struct qcom_cpufreq_data *drv_data = policy->driver_data;
157 const struct qcom_cpufreq_soc_data *soc_data = drv_data->soc_data;
Taniya Das2849dd82018-12-14 09:40:24 +0530158
159 table = kcalloc(LUT_MAX_ENTRIES + 1, sizeof(*table), GFP_KERNEL);
160 if (!table)
161 return -ENOMEM;
162
Sibi Sankar51c843c2020-06-22 13:46:48 +0530163 ret = dev_pm_opp_of_add_table(cpu_dev);
164 if (!ret) {
165 /* Disable all opps and cross-validate against LUT later */
166 icc_scaling_enabled = true;
167 for (rate = 0; ; rate++) {
168 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &rate);
169 if (IS_ERR(opp))
170 break;
171
172 dev_pm_opp_put(opp);
173 dev_pm_opp_disable(cpu_dev, rate);
174 }
175 } else if (ret != -ENODEV) {
176 dev_err(cpu_dev, "Invalid opp table in device tree\n");
177 return ret;
178 } else {
Sibi Sankarafdb2192020-06-22 13:46:49 +0530179 policy->fast_switch_possible = true;
Sibi Sankar51c843c2020-06-22 13:46:48 +0530180 icc_scaling_enabled = false;
181 }
182
Taniya Das2849dd82018-12-14 09:40:24 +0530183 for (i = 0; i < LUT_MAX_ENTRIES; i++) {
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530184 data = readl_relaxed(drv_data->base + soc_data->reg_freq_lut +
185 i * soc_data->lut_row_size);
Taniya Das2849dd82018-12-14 09:40:24 +0530186 src = FIELD_GET(LUT_SRC, data);
187 lval = FIELD_GET(LUT_L_VAL, data);
188 core_count = FIELD_GET(LUT_CORE_COUNT, data);
189
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530190 data = readl_relaxed(drv_data->base + soc_data->reg_volt_lut +
191 i * soc_data->lut_row_size);
Taniya Das55538fb2019-01-31 23:02:50 +0530192 volt = FIELD_GET(LUT_VOLT, data) * 1000;
193
Taniya Das2849dd82018-12-14 09:40:24 +0530194 if (src)
195 freq = xo_rate * lval / 1000;
196 else
197 freq = cpu_hw_rate / 1000;
198
Sibi Sankar0eae1e32019-08-07 17:15:43 +0530199 if (freq != prev_freq && core_count != LUT_TURBO_IND) {
Matthias Kaehlckebc9b9c52020-09-15 10:10:54 -0700200 if (!qcom_cpufreq_update_opp(cpu_dev, freq, volt)) {
201 table[i].frequency = freq;
202 dev_dbg(cpu_dev, "index=%d freq=%d, core_count %d\n", i,
Taniya Das2849dd82018-12-14 09:40:24 +0530203 freq, core_count);
Matthias Kaehlckebc9b9c52020-09-15 10:10:54 -0700204 } else {
205 dev_warn(cpu_dev, "failed to update OPP for freq=%d\n", freq);
206 table[i].frequency = CPUFREQ_ENTRY_INVALID;
207 }
208
Sibi Sankar0eae1e32019-08-07 17:15:43 +0530209 } else if (core_count == LUT_TURBO_IND) {
Taniya Das55538fb2019-01-31 23:02:50 +0530210 table[i].frequency = CPUFREQ_ENTRY_INVALID;
Taniya Das2849dd82018-12-14 09:40:24 +0530211 }
212
213 /*
214 * Two of the same frequencies with the same core counts means
215 * end of table
216 */
Sibi Sankar0eae1e32019-08-07 17:15:43 +0530217 if (i > 0 && prev_freq == freq) {
Taniya Das2849dd82018-12-14 09:40:24 +0530218 struct cpufreq_frequency_table *prev = &table[i - 1];
219
220 /*
221 * Only treat the last frequency that might be a boost
222 * as the boost frequency
223 */
Sibi Sankar0eae1e32019-08-07 17:15:43 +0530224 if (prev->frequency == CPUFREQ_ENTRY_INVALID) {
Matthias Kaehlckebc9b9c52020-09-15 10:10:54 -0700225 if (!qcom_cpufreq_update_opp(cpu_dev, prev_freq, volt)) {
226 prev->frequency = prev_freq;
227 prev->flags = CPUFREQ_BOOST_FREQ;
228 } else {
229 dev_warn(cpu_dev, "failed to update OPP for freq=%d\n",
230 freq);
231 }
Taniya Das2849dd82018-12-14 09:40:24 +0530232 }
233
234 break;
235 }
236
Taniya Das2849dd82018-12-14 09:40:24 +0530237 prev_freq = freq;
238 }
239
240 table[i].frequency = CPUFREQ_TABLE_END;
241 policy->freq_table = table;
Taniya Das55538fb2019-01-31 23:02:50 +0530242 dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
Taniya Das2849dd82018-12-14 09:40:24 +0530243
244 return 0;
245}
246
247static void qcom_get_related_cpus(int index, struct cpumask *m)
248{
249 struct device_node *cpu_np;
250 struct of_phandle_args args;
251 int cpu, ret;
252
253 for_each_possible_cpu(cpu) {
254 cpu_np = of_cpu_device_node_get(cpu);
255 if (!cpu_np)
256 continue;
257
258 ret = of_parse_phandle_with_args(cpu_np, "qcom,freq-domain",
259 "#freq-domain-cells", 0,
260 &args);
261 of_node_put(cpu_np);
262 if (ret < 0)
263 continue;
264
265 if (index == args.args[0])
266 cpumask_set_cpu(cpu, m);
267 }
268}
269
Thara Gopinath275157b2021-08-09 15:16:01 -0400270static unsigned int qcom_lmh_get_throttle_freq(struct qcom_cpufreq_data *data)
271{
272 unsigned int val = readl_relaxed(data->base + data->soc_data->reg_current_vote);
273
274 return (val & 0x3FF) * 19200;
275}
276
277static void qcom_lmh_dcvs_notify(struct qcom_cpufreq_data *data)
278{
Thara Gopinath275157b2021-08-09 15:16:01 -0400279 struct cpufreq_policy *policy = data->policy;
280 int cpu = cpumask_first(policy->cpus);
281 struct device *dev = get_cpu_device(cpu);
Lukasz Luba0258cb12021-11-09 19:57:13 +0000282 unsigned long freq_hz, throttled_freq;
Thara Gopinath275157b2021-08-09 15:16:01 -0400283 struct dev_pm_opp *opp;
284 unsigned int freq;
285
286 /*
287 * Get the h/w throttled frequency, normalize it using the
288 * registered opp table and use it to calculate thermal pressure.
289 */
290 freq = qcom_lmh_get_throttle_freq(data);
291 freq_hz = freq * HZ_PER_KHZ;
292
293 opp = dev_pm_opp_find_freq_floor(dev, &freq_hz);
294 if (IS_ERR(opp) && PTR_ERR(opp) == -ERANGE)
295 dev_pm_opp_find_freq_ceil(dev, &freq_hz);
296
297 throttled_freq = freq_hz / HZ_PER_KHZ;
298
Lukasz Luba0258cb12021-11-09 19:57:13 +0000299 /* Update thermal pressure (the boost frequencies are accepted) */
300 arch_update_thermal_pressure(policy->related_cpus, throttled_freq);
Thara Gopinath275157b2021-08-09 15:16:01 -0400301
302 /*
303 * In the unlikely case policy is unregistered do not enable
304 * polling or h/w interrupt
305 */
306 mutex_lock(&data->throttle_lock);
307 if (data->cancel_throttle)
308 goto out;
309
310 /*
311 * If h/w throttled frequency is higher than what cpufreq has requested
312 * for, then stop polling and switch back to interrupt mechanism.
313 */
314 if (throttled_freq >= qcom_cpufreq_hw_get(cpu))
315 enable_irq(data->throttle_irq);
316 else
317 mod_delayed_work(system_highpri_wq, &data->throttle_work,
318 msecs_to_jiffies(10));
319
320out:
321 mutex_unlock(&data->throttle_lock);
322}
323
324static void qcom_lmh_dcvs_poll(struct work_struct *work)
325{
326 struct qcom_cpufreq_data *data;
327
328 data = container_of(work, struct qcom_cpufreq_data, throttle_work.work);
329 qcom_lmh_dcvs_notify(data);
330}
331
332static irqreturn_t qcom_lmh_dcvs_handle_irq(int irq, void *data)
333{
334 struct qcom_cpufreq_data *c_data = data;
335
336 /* Disable interrupt and enable polling */
337 disable_irq_nosync(c_data->throttle_irq);
Vladimir Zapolskiye0e27c32021-11-11 17:48:07 +0200338 schedule_delayed_work(&c_data->throttle_work, 0);
Thara Gopinath275157b2021-08-09 15:16:01 -0400339
Vladimir Zapolskiye0e27c32021-11-11 17:48:07 +0200340 return IRQ_HANDLED;
Thara Gopinath275157b2021-08-09 15:16:01 -0400341}
342
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530343static const struct qcom_cpufreq_soc_data qcom_soc_data = {
344 .reg_enable = 0x0,
345 .reg_freq_lut = 0x110,
346 .reg_volt_lut = 0x114,
Thara Gopinath275157b2021-08-09 15:16:01 -0400347 .reg_current_vote = 0x704,
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530348 .reg_perf_state = 0x920,
349 .lut_row_size = 32,
350};
351
Manivannan Sadhasivam49b59f42020-09-15 12:54:23 +0530352static const struct qcom_cpufreq_soc_data epss_soc_data = {
353 .reg_enable = 0x0,
354 .reg_freq_lut = 0x100,
355 .reg_volt_lut = 0x200,
356 .reg_perf_state = 0x320,
357 .lut_row_size = 4,
358};
359
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530360static const struct of_device_id qcom_cpufreq_hw_match[] = {
361 { .compatible = "qcom,cpufreq-hw", .data = &qcom_soc_data },
Manivannan Sadhasivam49b59f42020-09-15 12:54:23 +0530362 { .compatible = "qcom,cpufreq-epss", .data = &epss_soc_data },
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530363 {}
364};
365MODULE_DEVICE_TABLE(of, qcom_cpufreq_hw_match);
366
Thara Gopinath275157b2021-08-09 15:16:01 -0400367static int qcom_cpufreq_hw_lmh_init(struct cpufreq_policy *policy, int index)
368{
369 struct qcom_cpufreq_data *data = policy->driver_data;
370 struct platform_device *pdev = cpufreq_get_driver_data();
Thara Gopinath275157b2021-08-09 15:16:01 -0400371 int ret;
372
373 /*
374 * Look for LMh interrupt. If no interrupt line is specified /
375 * if there is an error, allow cpufreq to be enabled as usual.
376 */
Stephen Boyd8f5783a2021-11-16 18:03:46 -0800377 data->throttle_irq = platform_get_irq_optional(pdev, index);
378 if (data->throttle_irq == -ENXIO)
379 return 0;
380 if (data->throttle_irq < 0)
381 return data->throttle_irq;
Thara Gopinath275157b2021-08-09 15:16:01 -0400382
383 data->cancel_throttle = false;
384 data->policy = policy;
385
386 mutex_init(&data->throttle_lock);
387 INIT_DEFERRABLE_WORK(&data->throttle_work, qcom_lmh_dcvs_poll);
388
Ard Biesheuvelbe6592e2021-11-11 17:48:06 +0200389 snprintf(data->irq_name, sizeof(data->irq_name), "dcvsh-irq-%u", policy->cpu);
Thara Gopinath275157b2021-08-09 15:16:01 -0400390 ret = request_threaded_irq(data->throttle_irq, NULL, qcom_lmh_dcvs_handle_irq,
Ard Biesheuvelbe6592e2021-11-11 17:48:06 +0200391 IRQF_ONESHOT, data->irq_name, data);
Thara Gopinath275157b2021-08-09 15:16:01 -0400392 if (ret) {
Ard Biesheuvelbe6592e2021-11-11 17:48:06 +0200393 dev_err(&pdev->dev, "Error registering %s: %d\n", data->irq_name, ret);
Thara Gopinath275157b2021-08-09 15:16:01 -0400394 return 0;
395 }
396
Vladimir Zapolskiy3ed6dfb2021-11-11 17:48:08 +0200397 ret = irq_set_affinity_hint(data->throttle_irq, policy->cpus);
398 if (ret)
399 dev_err(&pdev->dev, "Failed to set CPU affinity of %s[%d]\n",
400 data->irq_name, data->throttle_irq);
401
Thara Gopinath275157b2021-08-09 15:16:01 -0400402 return 0;
403}
404
405static void qcom_cpufreq_hw_lmh_exit(struct qcom_cpufreq_data *data)
406{
407 if (data->throttle_irq <= 0)
408 return;
409
410 mutex_lock(&data->throttle_lock);
411 data->cancel_throttle = true;
412 mutex_unlock(&data->throttle_lock);
413
414 cancel_delayed_work_sync(&data->throttle_work);
415 free_irq(data->throttle_irq, data);
416}
417
Taniya Das2849dd82018-12-14 09:40:24 +0530418static int qcom_cpufreq_hw_cpu_init(struct cpufreq_policy *policy)
419{
Manivannan Sadhasivambd74e282020-09-08 13:27:12 +0530420 struct platform_device *pdev = cpufreq_get_driver_data();
421 struct device *dev = &pdev->dev;
Taniya Das2849dd82018-12-14 09:40:24 +0530422 struct of_phandle_args args;
423 struct device_node *cpu_np;
Taniya Das55538fb2019-01-31 23:02:50 +0530424 struct device *cpu_dev;
Shawn Guo67fc2092021-01-19 10:39:25 +0800425 struct resource *res;
Taniya Das2849dd82018-12-14 09:40:24 +0530426 void __iomem *base;
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530427 struct qcom_cpufreq_data *data;
Taniya Das2849dd82018-12-14 09:40:24 +0530428 int ret, index;
429
Taniya Das55538fb2019-01-31 23:02:50 +0530430 cpu_dev = get_cpu_device(policy->cpu);
431 if (!cpu_dev) {
432 pr_err("%s: failed to get cpu%d device\n", __func__,
433 policy->cpu);
434 return -ENODEV;
435 }
436
Taniya Das2849dd82018-12-14 09:40:24 +0530437 cpu_np = of_cpu_device_node_get(policy->cpu);
438 if (!cpu_np)
439 return -EINVAL;
440
441 ret = of_parse_phandle_with_args(cpu_np, "qcom,freq-domain",
442 "#freq-domain-cells", 0, &args);
443 of_node_put(cpu_np);
444 if (ret)
445 return ret;
446
447 index = args.args[0];
448
Shawn Guo67fc2092021-01-19 10:39:25 +0800449 res = platform_get_resource(pdev, IORESOURCE_MEM, index);
450 if (!res) {
451 dev_err(dev, "failed to get mem resource %d\n", index);
452 return -ENODEV;
453 }
Taniya Das2849dd82018-12-14 09:40:24 +0530454
Shawn Guo67fc2092021-01-19 10:39:25 +0800455 if (!request_mem_region(res->start, resource_size(res), res->name)) {
456 dev_err(dev, "failed to request resource %pR\n", res);
457 return -EBUSY;
458 }
459
460 base = ioremap(res->start, resource_size(res));
Wei Yongjun536eb972021-03-04 10:04:23 +0000461 if (!base) {
Shawn Guo67fc2092021-01-19 10:39:25 +0800462 dev_err(dev, "failed to map resource %pR\n", res);
Wei Yongjun536eb972021-03-04 10:04:23 +0000463 ret = -ENOMEM;
Shawn Guo67fc2092021-01-19 10:39:25 +0800464 goto release_region;
465 }
466
467 data = kzalloc(sizeof(*data), GFP_KERNEL);
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530468 if (!data) {
469 ret = -ENOMEM;
Shawn Guo67fc2092021-01-19 10:39:25 +0800470 goto unmap_base;
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530471 }
472
473 data->soc_data = of_device_get_match_data(&pdev->dev);
474 data->base = base;
Shawn Guo67fc2092021-01-19 10:39:25 +0800475 data->res = res;
Taniya Das2849dd82018-12-14 09:40:24 +0530476
477 /* HW should be in enabled state to proceed */
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530478 if (!(readl_relaxed(base + data->soc_data->reg_enable) & 0x1)) {
Taniya Das2849dd82018-12-14 09:40:24 +0530479 dev_err(dev, "Domain-%d cpufreq hardware not enabled\n", index);
480 ret = -ENODEV;
481 goto error;
482 }
483
484 qcom_get_related_cpus(index, policy->cpus);
485 if (!cpumask_weight(policy->cpus)) {
486 dev_err(dev, "Domain-%d failed to get related CPUs\n", index);
487 ret = -ENOENT;
488 goto error;
489 }
490
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530491 policy->driver_data = data;
Taniya Dasf0712ac2021-08-23 15:45:46 +0530492 policy->dvfs_possible_from_any_cpu = true;
Taniya Das2849dd82018-12-14 09:40:24 +0530493
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530494 ret = qcom_cpufreq_hw_read_lut(cpu_dev, policy);
Taniya Das2849dd82018-12-14 09:40:24 +0530495 if (ret) {
496 dev_err(dev, "Domain-%d failed to read LUT\n", index);
497 goto error;
498 }
499
Taniya Das55538fb2019-01-31 23:02:50 +0530500 ret = dev_pm_opp_get_opp_count(cpu_dev);
501 if (ret <= 0) {
502 dev_err(cpu_dev, "Failed to add OPPs\n");
503 ret = -ENODEV;
504 goto error;
505 }
506
Shawn Guo26699172021-01-13 14:52:41 +0800507 if (policy_has_boost_freq(policy)) {
508 ret = cpufreq_enable_boost_support();
509 if (ret)
510 dev_warn(cpu_dev, "failed to enable boost: %d\n", ret);
511 }
512
Thara Gopinath275157b2021-08-09 15:16:01 -0400513 ret = qcom_cpufreq_hw_lmh_init(policy, index);
514 if (ret)
515 goto error;
516
Taniya Das2849dd82018-12-14 09:40:24 +0530517 return 0;
518error:
Shawn Guo67fc2092021-01-19 10:39:25 +0800519 kfree(data);
520unmap_base:
Shawn Guo02fc4092021-02-28 09:33:19 +0800521 iounmap(base);
Shawn Guo67fc2092021-01-19 10:39:25 +0800522release_region:
523 release_mem_region(res->start, resource_size(res));
Taniya Das2849dd82018-12-14 09:40:24 +0530524 return ret;
525}
526
527static int qcom_cpufreq_hw_cpu_exit(struct cpufreq_policy *policy)
528{
Taniya Das55538fb2019-01-31 23:02:50 +0530529 struct device *cpu_dev = get_cpu_device(policy->cpu);
Manivannan Sadhasivamdcd1fd72020-09-15 12:54:22 +0530530 struct qcom_cpufreq_data *data = policy->driver_data;
Shawn Guo67fc2092021-01-19 10:39:25 +0800531 struct resource *res = data->res;
532 void __iomem *base = data->base;
Taniya Das2849dd82018-12-14 09:40:24 +0530533
Taniya Das55538fb2019-01-31 23:02:50 +0530534 dev_pm_opp_remove_all_dynamic(cpu_dev);
Sibi Sankar51c843c2020-06-22 13:46:48 +0530535 dev_pm_opp_of_cpumask_remove_table(policy->related_cpus);
Thara Gopinath275157b2021-08-09 15:16:01 -0400536 qcom_cpufreq_hw_lmh_exit(data);
Taniya Das2849dd82018-12-14 09:40:24 +0530537 kfree(policy->freq_table);
Shawn Guo67fc2092021-01-19 10:39:25 +0800538 kfree(data);
539 iounmap(base);
540 release_mem_region(res->start, resource_size(res));
Taniya Das2849dd82018-12-14 09:40:24 +0530541
542 return 0;
543}
544
545static struct freq_attr *qcom_cpufreq_hw_attr[] = {
546 &cpufreq_freq_attr_scaling_available_freqs,
547 &cpufreq_freq_attr_scaling_boost_freqs,
548 NULL
549};
550
551static struct cpufreq_driver cpufreq_qcom_hw_driver = {
Viresh Kumar5ae4a4b2021-02-02 10:25:11 +0530552 .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK |
Amit Kucheria4c5ff1c2019-01-29 10:25:09 +0530553 CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
554 CPUFREQ_IS_COOLING_DEV,
Taniya Das2849dd82018-12-14 09:40:24 +0530555 .verify = cpufreq_generic_frequency_table_verify,
556 .target_index = qcom_cpufreq_hw_target_index,
557 .get = qcom_cpufreq_hw_get,
558 .init = qcom_cpufreq_hw_cpu_init,
559 .exit = qcom_cpufreq_hw_cpu_exit,
Viresh Kumare96c2152021-08-10 12:24:36 +0530560 .register_em = cpufreq_register_em_with_opp,
Taniya Das2849dd82018-12-14 09:40:24 +0530561 .fast_switch = qcom_cpufreq_hw_fast_switch,
562 .name = "qcom-cpufreq-hw",
563 .attr = qcom_cpufreq_hw_attr,
564};
565
566static int qcom_cpufreq_hw_driver_probe(struct platform_device *pdev)
567{
Sibi Sankar51c843c2020-06-22 13:46:48 +0530568 struct device *cpu_dev;
Taniya Das2849dd82018-12-14 09:40:24 +0530569 struct clk *clk;
570 int ret;
571
572 clk = clk_get(&pdev->dev, "xo");
573 if (IS_ERR(clk))
574 return PTR_ERR(clk);
575
576 xo_rate = clk_get_rate(clk);
577 clk_put(clk);
578
579 clk = clk_get(&pdev->dev, "alternate");
580 if (IS_ERR(clk))
581 return PTR_ERR(clk);
582
583 cpu_hw_rate = clk_get_rate(clk) / CLK_HW_DIV;
584 clk_put(clk);
585
Manivannan Sadhasivambd74e282020-09-08 13:27:12 +0530586 cpufreq_qcom_hw_driver.driver_data = pdev;
Taniya Das2849dd82018-12-14 09:40:24 +0530587
Sibi Sankar51c843c2020-06-22 13:46:48 +0530588 /* Check for optional interconnect paths on CPU0 */
589 cpu_dev = get_cpu_device(0);
590 if (!cpu_dev)
591 return -EPROBE_DEFER;
592
593 ret = dev_pm_opp_of_find_icc_paths(cpu_dev, NULL);
594 if (ret)
595 return ret;
596
Taniya Das2849dd82018-12-14 09:40:24 +0530597 ret = cpufreq_register_driver(&cpufreq_qcom_hw_driver);
598 if (ret)
599 dev_err(&pdev->dev, "CPUFreq HW driver failed to register\n");
600 else
601 dev_dbg(&pdev->dev, "QCOM CPUFreq HW driver initialized\n");
602
603 return ret;
604}
605
606static int qcom_cpufreq_hw_driver_remove(struct platform_device *pdev)
607{
608 return cpufreq_unregister_driver(&cpufreq_qcom_hw_driver);
609}
610
Taniya Das2849dd82018-12-14 09:40:24 +0530611static struct platform_driver qcom_cpufreq_hw_driver = {
612 .probe = qcom_cpufreq_hw_driver_probe,
613 .remove = qcom_cpufreq_hw_driver_remove,
614 .driver = {
615 .name = "qcom-cpufreq-hw",
616 .of_match_table = qcom_cpufreq_hw_match,
617 },
618};
619
620static int __init qcom_cpufreq_hw_init(void)
621{
622 return platform_driver_register(&qcom_cpufreq_hw_driver);
623}
Amit Kucheria11ff4bd2019-10-21 17:45:15 +0530624postcore_initcall(qcom_cpufreq_hw_init);
Taniya Das2849dd82018-12-14 09:40:24 +0530625
626static void __exit qcom_cpufreq_hw_exit(void)
627{
628 platform_driver_unregister(&qcom_cpufreq_hw_driver);
629}
630module_exit(qcom_cpufreq_hw_exit);
631
632MODULE_DESCRIPTION("QCOM CPUFREQ HW Driver");
633MODULE_LICENSE("GPL v2");