cpufreq: dbx500: don't initialize part of policy set by core
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / cpufreq / dbx500-cpufreq.c
1 /*
2  * Copyright (C) STMicroelectronics 2009
3  * Copyright (C) ST-Ericsson SA 2010-2012
4  *
5  * License Terms: GNU General Public License v2
6  * Author: Sundar Iyer <sundar.iyer@stericsson.com>
7  * Author: Martin Persson <martin.persson@stericsson.com>
8  * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
9  */
10
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/cpufreq.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/clk.h>
18
19 static struct cpufreq_frequency_table *freq_table;
20 static struct clk *armss_clk;
21
22 static int dbx500_cpufreq_target(struct cpufreq_policy *policy,
23                                 unsigned int target_freq,
24                                 unsigned int relation)
25 {
26         struct cpufreq_freqs freqs;
27         unsigned int idx;
28         int ret;
29
30         /* Lookup the next frequency */
31         if (cpufreq_frequency_table_target(policy, freq_table, target_freq,
32                                         relation, &idx))
33                 return -EINVAL;
34
35         freqs.old = policy->cur;
36         freqs.new = freq_table[idx].frequency;
37
38         if (freqs.old == freqs.new)
39                 return 0;
40
41         /* pre-change notification */
42         cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
43
44         /* update armss clk frequency */
45         ret = clk_set_rate(armss_clk, freqs.new * 1000);
46
47         if (ret) {
48                 pr_err("dbx500-cpufreq: Failed to set armss_clk to %d Hz: error %d\n",
49                        freqs.new * 1000, ret);
50                 freqs.new = freqs.old;
51         }
52
53         /* post change notification */
54         cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
55
56         return ret;
57 }
58
59 static unsigned int dbx500_cpufreq_getspeed(unsigned int cpu)
60 {
61         int i = 0;
62         unsigned long freq = clk_get_rate(armss_clk) / 1000;
63
64         /* The value is rounded to closest frequency in the defined table. */
65         while (freq_table[i + 1].frequency != CPUFREQ_TABLE_END) {
66                 if (freq < freq_table[i].frequency +
67                    (freq_table[i + 1].frequency - freq_table[i].frequency) / 2)
68                         return freq_table[i].frequency;
69                 i++;
70         }
71
72         return freq_table[i].frequency;
73 }
74
75 static int dbx500_cpufreq_init(struct cpufreq_policy *policy)
76 {
77         int res;
78
79         /* get policy fields based on the table */
80         res = cpufreq_table_validate_and_show(policy, freq_table);
81         if (res) {
82                 pr_err("dbx500-cpufreq: Failed to read policy table\n");
83                 return res;
84         }
85
86         /*
87          * FIXME : Need to take time measurement across the target()
88          *         function with no/some/all drivers in the notification
89          *         list.
90          */
91         policy->cpuinfo.transition_latency = 20 * 1000; /* in ns */
92
93         /* policy sharing between dual CPUs */
94         cpumask_setall(policy->cpus);
95
96         return 0;
97 }
98
99 static struct cpufreq_driver dbx500_cpufreq_driver = {
100         .flags  = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS,
101         .verify = cpufreq_generic_frequency_table_verify,
102         .target = dbx500_cpufreq_target,
103         .get    = dbx500_cpufreq_getspeed,
104         .init   = dbx500_cpufreq_init,
105         .name   = "DBX500",
106         .attr   = cpufreq_generic_attr,
107 };
108
109 static int dbx500_cpufreq_probe(struct platform_device *pdev)
110 {
111         int i = 0;
112
113         freq_table = dev_get_platdata(&pdev->dev);
114         if (!freq_table) {
115                 pr_err("dbx500-cpufreq: Failed to fetch cpufreq table\n");
116                 return -ENODEV;
117         }
118
119         armss_clk = clk_get(&pdev->dev, "armss");
120         if (IS_ERR(armss_clk)) {
121                 pr_err("dbx500-cpufreq: Failed to get armss clk\n");
122                 return PTR_ERR(armss_clk);
123         }
124
125         pr_info("dbx500-cpufreq: Available frequencies:\n");
126         while (freq_table[i].frequency != CPUFREQ_TABLE_END) {
127                 pr_info("  %d Mhz\n", freq_table[i].frequency/1000);
128                 i++;
129         }
130
131         return cpufreq_register_driver(&dbx500_cpufreq_driver);
132 }
133
134 static struct platform_driver dbx500_cpufreq_plat_driver = {
135         .driver = {
136                 .name = "cpufreq-ux500",
137                 .owner = THIS_MODULE,
138         },
139         .probe = dbx500_cpufreq_probe,
140 };
141
142 static int __init dbx500_cpufreq_register(void)
143 {
144         return platform_driver_register(&dbx500_cpufreq_plat_driver);
145 }
146 device_initcall(dbx500_cpufreq_register);
147
148 MODULE_LICENSE("GPL v2");
149 MODULE_DESCRIPTION("cpufreq driver for DBX500");