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];
59 #define notifier_to_avs(_n) container_of(_n, struct rockchip_cpu_avs, \
62 static void rockchip_leakage_adjust_table(struct device *cpu_dev,
63 struct cpufreq_frequency_table *table,
64 struct cluster_info *cluster)
66 struct cpufreq_frequency_table *pos;
67 struct dev_pm_opp *opp;
68 unsigned long adjust_volt;
70 if (!cluster->offset_volt)
73 cpufreq_for_each_valid_entry(pos, table) {
74 if (pos->frequency < cluster->min_freq)
79 opp = dev_pm_opp_find_freq_exact(cpu_dev, pos->frequency * 1000,
86 adjust_volt = dev_pm_opp_get_voltage(opp) +
91 if (adjust_volt < cluster->min_volt)
94 dev_pm_opp_adjust_voltage(cpu_dev, pos->frequency * 1000,
99 static int rockchip_cpu_avs_notifier(struct notifier_block *nb,
100 unsigned long event, void *data)
102 struct rockchip_cpu_avs *avs = notifier_to_avs(nb);
103 struct cpufreq_policy *policy = data;
104 struct cluster_info *cluster;
105 struct device *cpu_dev;
106 unsigned long cur_freq;
107 int i, id, cpu = policy->cpu;
110 if (event != CPUFREQ_START)
113 id = topology_physical_package_id(cpu);
114 if (id < 0 || id >= MAX_CLUSTERS) {
115 pr_err("cpu%d invalid cluster id\n", cpu);
119 cpu_dev = get_cpu_device(cpu);
121 pr_err("cpu%d failed to get device\n", cpu);
125 if (avs->num_clusters == 0)
128 for (i = 0; i < avs->num_clusters; i++) {
129 if (avs->cluster[i].id == id)
132 if (i == avs->num_clusters)
135 cluster = &avs->cluster[i];
137 if (!policy->freq_table) {
138 pr_err("cpu%d freq table not found\n", cpu);
142 rockchip_leakage_adjust_table(cpu_dev, policy->freq_table, cluster);
145 if (avs->check_done[id] == 0) {
146 ret = dev_pm_opp_check_initial_rate(cpu_dev, &cur_freq);
148 policy->cur = cur_freq / 1000;
149 avs->check_done[id] = 1;
157 static int rockchip_get_leakage_volt_table(struct device_node *np,
158 struct leakage_volt_table **table)
160 struct leakage_volt_table *volt_table;
161 const struct property *prop;
164 prop = of_find_property(np, "leakage-adjust-volt", NULL);
166 pr_err("failed to find property leakage-adjust-volt\n");
170 pr_err("property leakage-adjust-volt is NULL\n");
174 count = of_property_count_u32_elems(np, "leakage-adjust-volt");
176 pr_err("Invalid property (%d)\n", count);
180 pr_err("Invalid number of elements in property (%d)\n", count);
184 volt_table = kzalloc(sizeof(*volt_table) * (count / 3 + 1), GFP_KERNEL);
188 for (i = 0; i < count / 3; i++) {
189 of_property_read_u32_index(np, "leakage-adjust-volt", 3 * i,
191 of_property_read_u32_index(np, "leakage-adjust-volt", 3 * i + 1,
193 of_property_read_u32_index(np, "leakage-adjust-volt", 3 * i + 2,
194 (u32 *)&volt_table[i].volt);
196 volt_table[i].min = 0;
197 volt_table[i].max = 0;
198 volt_table[i].volt = LEAKAGE_TABLE_END;
205 static int rockchip_get_leakage(struct device_node *np, unsigned char *leakage)
207 struct nvmem_cell *cell;
211 cell = of_nvmem_cell_get(np, "cpu_leakage");
213 pr_err("avs failed to get cpu_leakage cell\n");
214 return PTR_ERR(cell);
217 buf = (unsigned char *)nvmem_cell_read(cell, &len);
219 nvmem_cell_put(cell);
224 if (buf[0] == INVALID_VALUE)
233 static int rockchip_get_offset_volt(unsigned char leakage,
234 struct leakage_volt_table *table, int *volt)
241 for (i = 0; table[i].volt != LEAKAGE_TABLE_END; i++) {
242 if (leakage >= (unsigned char)table[i].min)
249 *volt = table[j].volt;
254 static int rockchip_of_parse_cpu_avs(struct device_node *np,
255 struct cluster_info *cluster)
259 ret = of_property_read_u32(np, "cluster-id", &cluster->id);
261 pr_err("prop cluster-id missing\n");
264 if (cluster->id >= MAX_CLUSTERS) {
265 pr_err("prop cluster-id invalid\n");
269 ret = of_property_read_u32(np, "min-freq", &cluster->min_freq);
271 pr_err("prop min_freq missing\n");
275 ret = of_property_read_u32(np, "min-volt", &cluster->min_volt);
277 pr_err("prop min_volt missing\n");
281 ret = rockchip_get_leakage_volt_table(np, &cluster->table);
283 pr_err("prop leakage-adjust-volt invalid\n");
287 ret = rockchip_get_leakage(np, &cluster->leakage);
289 pr_err("get leakage invalid\n");
293 ret = rockchip_get_offset_volt(cluster->leakage, cluster->table,
294 &cluster->offset_volt);
296 pr_err("get offset volt err\n");
300 pr_info("cluster%d leakage=%d adjust_volt=%d\n", cluster->id,
301 cluster->leakage, cluster->offset_volt);
306 static int rockchip_cpu_avs_probe(struct platform_device *pdev)
308 struct device *dev = &pdev->dev;
309 struct device_node *np, *node;
310 struct rockchip_cpu_avs *avs;
311 int ret, num_clusters = 0, i = 0;
313 avs = devm_kzalloc(dev, sizeof(*avs), GFP_KERNEL);
317 np = of_find_node_by_name(NULL, "cpu-avs");
319 pr_info("unable to find cpu-avs node\n");
323 if (!of_device_is_available(np)) {
324 pr_info("cpu-avs node disabled\n");
328 for_each_available_child_of_node(np, node)
332 pr_info("cpu-avs child node disabled\n");
336 avs->cluster = devm_kzalloc(dev, sizeof(*avs->cluster) * num_clusters,
341 avs->num_clusters = num_clusters;
343 for_each_available_child_of_node(np, node) {
344 ret = rockchip_of_parse_cpu_avs(node, &avs->cluster[i++]);
346 devm_kfree(dev, avs->cluster);
347 avs->num_clusters = 0;
356 avs->cpufreq_notify.notifier_call = rockchip_cpu_avs_notifier;
358 return cpufreq_register_notifier(&avs->cpufreq_notify,
359 CPUFREQ_POLICY_NOTIFIER);
362 static struct platform_driver rockchip_cpu_avs_platdrv = {
364 .name = "rockchip-cpu-avs",
366 .probe = rockchip_cpu_avs_probe,
369 static int __init rockchip_cpu_avs_init(void)
371 struct platform_device *pdev;
374 ret = platform_driver_register(&rockchip_cpu_avs_platdrv);
379 * Since there's no place to hold device registration code and no
380 * device tree based way to match cpu-avs driver yet, both the driver
381 * and the device registration codes are put here to handle defer
384 pdev = platform_device_register_simple("rockchip-cpu-avs", -1, NULL, 0);
386 pr_err("failed to register rockchip-cpu-avs platform device\n");
387 return PTR_ERR(pdev);
393 module_init(rockchip_cpu_avs_init);
395 MODULE_DESCRIPTION("Rockchip CPU AVS driver");
396 MODULE_AUTHOR("Finley Xiao <finley.xiao@rock-chips.com>");
397 MODULE_LICENSE("GPL v2");