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;
52 struct rockchip_cpu_avs {
53 unsigned int num_clusters;
54 struct cluster_info *cluster;
55 struct notifier_block cpufreq_notify;
56 unsigned int check_done[MAX_CLUSTERS];
57 struct cpumask allowed_cpus[MAX_CLUSTERS];
60 #define notifier_to_avs(_n) container_of(_n, struct rockchip_cpu_avs, \
63 static void rockchip_leakage_adjust_table(struct device *cpu_dev,
64 struct cpufreq_frequency_table *table,
65 struct cluster_info *cluster)
67 struct cpufreq_frequency_table *pos;
68 struct dev_pm_opp *opp;
69 unsigned long adjust_volt;
71 if (!cluster->offset_volt)
74 cpufreq_for_each_valid_entry(pos, table) {
75 if (pos->frequency < cluster->min_freq)
80 opp = dev_pm_opp_find_freq_exact(cpu_dev, pos->frequency * 1000,
87 adjust_volt = dev_pm_opp_get_voltage(opp) +
92 if (adjust_volt < cluster->min_volt)
95 dev_pm_opp_adjust_voltage(cpu_dev, pos->frequency * 1000,
100 static int rockchip_cpu_avs_notifier(struct notifier_block *nb,
101 unsigned long event, void *data)
103 struct rockchip_cpu_avs *avs = notifier_to_avs(nb);
104 struct cpufreq_policy *policy = data;
105 struct cluster_info *cluster;
106 struct device *cpu_dev;
107 unsigned long cur_freq;
108 int i, id, cpu = policy->cpu;
111 if (event == CPUFREQ_CREATE_POLICY) {
112 for (id = 0; id < MAX_CLUSTERS; id++) {
113 if (cpumask_test_cpu(policy->cpu,
114 &avs->allowed_cpus[id]))
116 if (cpumask_empty(&avs->allowed_cpus[id])) {
117 cpumask_copy(&avs->allowed_cpus[id],
118 policy->related_cpus);
125 if (event != CPUFREQ_START)
128 for (id = 0; id < MAX_CLUSTERS; id++) {
129 if (cpumask_test_cpu(cpu, &avs->allowed_cpus[id]))
132 if (id == MAX_CLUSTERS) {
133 pr_err("cpu%d invalid cluster id\n", cpu);
137 cpu_dev = get_cpu_device(cpu);
139 pr_err("cpu%d failed to get device\n", cpu);
143 if (avs->num_clusters == 0)
146 for (i = 0; i < avs->num_clusters; i++) {
147 if (avs->cluster[i].id == id)
150 if (i == avs->num_clusters)
153 cluster = &avs->cluster[i];
155 if (!policy->freq_table) {
156 pr_err("cpu%d freq table not found\n", cpu);
160 rockchip_leakage_adjust_table(cpu_dev, policy->freq_table, cluster);
163 if (avs->check_done[id] == 0) {
164 ret = dev_pm_opp_check_initial_rate(cpu_dev, &cur_freq);
166 policy->cur = cur_freq / 1000;
167 avs->check_done[id] = 1;
175 static int rockchip_get_leakage_volt_table(struct device_node *np,
176 struct leakage_volt_table **table)
178 struct leakage_volt_table *volt_table;
179 const struct property *prop;
182 prop = of_find_property(np, "leakage-adjust-volt", NULL);
184 pr_err("failed to find property leakage-adjust-volt\n");
188 pr_err("property leakage-adjust-volt is NULL\n");
192 count = of_property_count_u32_elems(np, "leakage-adjust-volt");
194 pr_err("Invalid property (%d)\n", count);
198 pr_err("Invalid number of elements in property (%d)\n", count);
202 volt_table = kzalloc(sizeof(*volt_table) * (count / 3 + 1), GFP_KERNEL);
206 for (i = 0; i < count / 3; i++) {
207 of_property_read_u32_index(np, "leakage-adjust-volt", 3 * i,
209 of_property_read_u32_index(np, "leakage-adjust-volt", 3 * i + 1,
211 of_property_read_u32_index(np, "leakage-adjust-volt", 3 * i + 2,
212 (u32 *)&volt_table[i].volt);
214 volt_table[i].min = 0;
215 volt_table[i].max = 0;
216 volt_table[i].volt = LEAKAGE_TABLE_END;
223 static int rockchip_get_leakage(struct device_node *np, unsigned char *leakage)
225 struct nvmem_cell *cell;
229 cell = of_nvmem_cell_get(np, "cpu_leakage");
231 pr_err("avs failed to get cpu_leakage cell\n");
232 return PTR_ERR(cell);
235 buf = (unsigned char *)nvmem_cell_read(cell, &len);
237 nvmem_cell_put(cell);
242 if (buf[0] == INVALID_VALUE)
251 static int rockchip_get_offset_volt(unsigned char leakage,
252 struct leakage_volt_table *table, int *volt)
259 for (i = 0; table[i].volt != LEAKAGE_TABLE_END; i++) {
260 if (leakage >= (unsigned char)table[i].min)
267 *volt = table[j].volt;
272 static int rockchip_of_parse_cpu_avs(struct device_node *np,
273 struct cluster_info *cluster)
277 ret = of_property_read_u32(np, "cluster-id", &cluster->id);
279 pr_err("prop cluster-id missing\n");
282 if (cluster->id >= MAX_CLUSTERS) {
283 pr_err("prop cluster-id invalid\n");
287 ret = of_property_read_u32(np, "min-freq", &cluster->min_freq);
289 pr_err("prop min_freq missing\n");
293 ret = of_property_read_u32(np, "min-volt", &cluster->min_volt);
295 pr_err("prop min_volt missing\n");
299 ret = rockchip_get_leakage_volt_table(np, &cluster->table);
301 pr_err("prop leakage-adjust-volt invalid\n");
305 ret = rockchip_get_leakage(np, &cluster->leakage);
307 pr_err("get leakage invalid\n");
311 ret = rockchip_get_offset_volt(cluster->leakage, cluster->table,
312 &cluster->offset_volt);
314 pr_err("get offset volt err\n");
318 pr_info("cluster%d leakage=%d adjust_volt=%d\n", cluster->id,
319 cluster->leakage, cluster->offset_volt);
324 static int rockchip_cpu_avs_probe(struct platform_device *pdev)
326 struct device *dev = &pdev->dev;
327 struct device_node *np, *node;
328 struct rockchip_cpu_avs *avs;
329 int ret, num_clusters = 0, i = 0;
331 avs = devm_kzalloc(dev, sizeof(*avs), GFP_KERNEL);
335 np = of_find_node_by_name(NULL, "cpu-avs");
337 pr_info("unable to find cpu-avs node\n");
341 if (!of_device_is_available(np)) {
342 pr_info("cpu-avs node disabled\n");
346 for_each_available_child_of_node(np, node)
350 pr_info("cpu-avs child node disabled\n");
354 avs->cluster = devm_kzalloc(dev, sizeof(*avs->cluster) * num_clusters,
359 avs->num_clusters = num_clusters;
361 for_each_available_child_of_node(np, node) {
362 ret = rockchip_of_parse_cpu_avs(node, &avs->cluster[i++]);
364 devm_kfree(dev, avs->cluster);
365 avs->num_clusters = 0;
374 avs->cpufreq_notify.notifier_call = rockchip_cpu_avs_notifier;
376 return cpufreq_register_notifier(&avs->cpufreq_notify,
377 CPUFREQ_POLICY_NOTIFIER);
380 static struct platform_driver rockchip_cpu_avs_platdrv = {
382 .name = "rockchip-cpu-avs",
384 .probe = rockchip_cpu_avs_probe,
387 static int __init rockchip_cpu_avs_init(void)
389 struct platform_device *pdev;
392 ret = platform_driver_register(&rockchip_cpu_avs_platdrv);
397 * Since there's no place to hold device registration code and no
398 * device tree based way to match cpu-avs driver yet, both the driver
399 * and the device registration codes are put here to handle defer
402 pdev = platform_device_register_simple("rockchip-cpu-avs", -1, NULL, 0);
404 pr_err("failed to register rockchip-cpu-avs platform device\n");
405 return PTR_ERR(pdev);
411 module_init(rockchip_cpu_avs_init);
413 MODULE_DESCRIPTION("Rockchip CPU AVS driver");
414 MODULE_AUTHOR("Finley Xiao <finley.xiao@rock-chips.com>");
415 MODULE_LICENSE("GPL v2");