2 * Rockchip CPU AVS support.
4 * Copyright (c) 2016 Rockchip Electronics Co. Ltd.
5 * Author: Finley Xiao <finley.xiao@rock-chips.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/cpu.h>
20 #include <linux/cpufreq.h>
21 #include <linux/cpumask.h>
22 #include <linux/err.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/nvmem-consumer.h>
28 #include <linux/platform_device.h>
29 #include <linux/pm_opp.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
33 #define MAX_CLUSTERS 2
34 #define LEAKAGE_TABLE_END ~1
35 #define INVALID_VALUE 0xff
37 struct leakage_volt_table {
46 unsigned char leakage;
47 unsigned int min_freq;
48 unsigned int min_volt;
49 struct leakage_volt_table *table;
50 unsigned int adjust_done;
53 struct rockchip_cpu_avs {
54 unsigned int num_clusters;
55 struct cluster_info *cluster;
56 struct notifier_block cpufreq_notify;
57 unsigned int check_done[MAX_CLUSTERS];
58 struct cpumask allowed_cpus[MAX_CLUSTERS];
61 #define notifier_to_avs(_n) container_of(_n, struct rockchip_cpu_avs, \
64 static void rockchip_leakage_adjust_table(struct device *cpu_dev,
65 struct cpufreq_frequency_table *table,
66 struct cluster_info *cluster)
68 struct cpufreq_frequency_table *pos;
69 struct dev_pm_opp *opp;
70 unsigned long adjust_volt;
72 if (!cluster->offset_volt)
75 cpufreq_for_each_valid_entry(pos, table) {
76 if (pos->frequency < cluster->min_freq)
81 opp = dev_pm_opp_find_freq_exact(cpu_dev, pos->frequency * 1000,
88 adjust_volt = dev_pm_opp_get_voltage(opp) +
93 if (adjust_volt < cluster->min_volt)
96 dev_pm_opp_adjust_voltage(cpu_dev, pos->frequency * 1000,
101 static int rockchip_cpu_avs_notifier(struct notifier_block *nb,
102 unsigned long event, void *data)
104 struct rockchip_cpu_avs *avs = notifier_to_avs(nb);
105 struct cpufreq_policy *policy = data;
106 struct cluster_info *cluster;
107 struct device *cpu_dev;
108 unsigned long cur_freq;
109 int i, id, cpu = policy->cpu;
112 if (event != CPUFREQ_CREATE_POLICY && event != CPUFREQ_START)
115 for (id = 0; id < MAX_CLUSTERS; id++) {
116 if (cpumask_test_cpu(cpu, &avs->allowed_cpus[id]))
118 if (cpumask_empty(&avs->allowed_cpus[id])) {
119 cpumask_copy(&avs->allowed_cpus[id],
120 policy->related_cpus);
125 if (id == MAX_CLUSTERS) {
126 pr_err("cpu%d invalid cluster id\n", cpu);
130 cpu_dev = get_cpu_device(cpu);
132 pr_err("cpu%d failed to get device\n", cpu);
136 if (avs->num_clusters == 0)
139 for (i = 0; i < avs->num_clusters; i++) {
140 if (avs->cluster[i].id == id)
143 if (i == avs->num_clusters)
146 cluster = &avs->cluster[i];
148 if (!policy->freq_table) {
149 pr_err("cpu%d freq table not found\n", cpu);
154 * First time, CPUFREQ_CREATE_POLICY, adjust talbe
155 * Second time, CPUFREQ_START, no need to adjust again
156 * Later, cpu down and up, CPUFREQ_START, adjust table
158 if (event == CPUFREQ_CREATE_POLICY) {
159 rockchip_leakage_adjust_table(cpu_dev, policy->freq_table,
161 cluster->adjust_done = 1;
162 } else if (event == CPUFREQ_START) {
163 if (cluster->adjust_done == 1)
164 cluster->adjust_done = 0;
166 rockchip_leakage_adjust_table(cpu_dev,
172 if (avs->check_done[id] == 0) {
173 ret = dev_pm_opp_check_initial_rate(cpu_dev, &cur_freq);
175 policy->cur = cur_freq / 1000;
176 avs->check_done[id] = 1;
184 static int rockchip_get_leakage_volt_table(struct device_node *np,
185 struct leakage_volt_table **table)
187 struct leakage_volt_table *volt_table;
188 const struct property *prop;
191 prop = of_find_property(np, "leakage-adjust-volt", NULL);
193 pr_err("failed to find property leakage-adjust-volt\n");
197 pr_err("property leakage-adjust-volt is NULL\n");
201 count = of_property_count_u32_elems(np, "leakage-adjust-volt");
203 pr_err("Invalid property (%d)\n", count);
207 pr_err("Invalid number of elements in property (%d)\n", count);
211 volt_table = kzalloc(sizeof(*volt_table) * (count / 3 + 1), GFP_KERNEL);
215 for (i = 0; i < count / 3; i++) {
216 of_property_read_u32_index(np, "leakage-adjust-volt", 3 * i,
218 of_property_read_u32_index(np, "leakage-adjust-volt", 3 * i + 1,
220 of_property_read_u32_index(np, "leakage-adjust-volt", 3 * i + 2,
221 (u32 *)&volt_table[i].volt);
223 volt_table[i].min = 0;
224 volt_table[i].max = 0;
225 volt_table[i].volt = LEAKAGE_TABLE_END;
232 static int rockchip_get_leakage(struct device_node *np, unsigned char *leakage)
234 struct nvmem_cell *cell;
238 cell = of_nvmem_cell_get(np, "cpu_leakage");
240 pr_err("avs failed to get cpu_leakage cell\n");
241 return PTR_ERR(cell);
244 buf = (unsigned char *)nvmem_cell_read(cell, &len);
246 nvmem_cell_put(cell);
251 if (buf[0] == INVALID_VALUE)
260 static int rockchip_get_offset_volt(unsigned char leakage,
261 struct leakage_volt_table *table, int *volt)
268 for (i = 0; table[i].volt != LEAKAGE_TABLE_END; i++) {
269 if (leakage >= (unsigned char)table[i].min)
276 *volt = table[j].volt;
281 static int rockchip_of_parse_cpu_avs(struct device_node *np,
282 struct cluster_info *cluster)
286 ret = of_property_read_u32(np, "cluster-id", &cluster->id);
288 pr_err("prop cluster-id missing\n");
291 if (cluster->id >= MAX_CLUSTERS) {
292 pr_err("prop cluster-id invalid\n");
296 ret = of_property_read_u32(np, "min-freq", &cluster->min_freq);
298 pr_err("prop min_freq missing\n");
302 ret = of_property_read_u32(np, "min-volt", &cluster->min_volt);
304 pr_err("prop min_volt missing\n");
308 ret = rockchip_get_leakage_volt_table(np, &cluster->table);
310 pr_err("prop leakage-adjust-volt invalid\n");
314 ret = rockchip_get_leakage(np, &cluster->leakage);
316 pr_err("get leakage invalid\n");
320 ret = rockchip_get_offset_volt(cluster->leakage, cluster->table,
321 &cluster->offset_volt);
323 pr_err("get offset volt err\n");
327 pr_info("cluster%d leakage=%d adjust_volt=%d\n", cluster->id,
328 cluster->leakage, cluster->offset_volt);
333 static int rockchip_cpu_avs_probe(struct platform_device *pdev)
335 struct device *dev = &pdev->dev;
336 struct device_node *np, *node;
337 struct rockchip_cpu_avs *avs;
338 int ret, num_clusters = 0, i = 0;
340 avs = devm_kzalloc(dev, sizeof(*avs), GFP_KERNEL);
344 np = of_find_node_by_name(NULL, "cpu-avs");
346 pr_info("unable to find cpu-avs node\n");
350 if (!of_device_is_available(np)) {
351 pr_info("cpu-avs node disabled\n");
355 for_each_available_child_of_node(np, node)
359 pr_info("cpu-avs child node disabled\n");
363 avs->cluster = devm_kzalloc(dev, sizeof(*avs->cluster) * num_clusters,
368 avs->num_clusters = num_clusters;
370 for_each_available_child_of_node(np, node) {
371 ret = rockchip_of_parse_cpu_avs(node, &avs->cluster[i++]);
373 devm_kfree(dev, avs->cluster);
374 avs->num_clusters = 0;
383 avs->cpufreq_notify.notifier_call = rockchip_cpu_avs_notifier;
385 return cpufreq_register_notifier(&avs->cpufreq_notify,
386 CPUFREQ_POLICY_NOTIFIER);
389 static struct platform_driver rockchip_cpu_avs_platdrv = {
391 .name = "rockchip-cpu-avs",
393 .probe = rockchip_cpu_avs_probe,
396 static int __init rockchip_cpu_avs_init(void)
398 struct platform_device *pdev;
401 ret = platform_driver_register(&rockchip_cpu_avs_platdrv);
406 * Since there's no place to hold device registration code and no
407 * device tree based way to match cpu-avs driver yet, both the driver
408 * and the device registration codes are put here to handle defer
411 pdev = platform_device_register_simple("rockchip-cpu-avs", -1, NULL, 0);
413 pr_err("failed to register rockchip-cpu-avs platform device\n");
414 return PTR_ERR(pdev);
420 module_init(rockchip_cpu_avs_init);
422 MODULE_DESCRIPTION("Rockchip CPU AVS driver");
423 MODULE_AUTHOR("Finley Xiao <finley.xiao@rock-chips.com>");
424 MODULE_LICENSE("GPL v2");