2d70637850027eaa930d439ed108f1e7460ca6e5
[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         policy->min = policy->cpuinfo.min_freq;
87         policy->max = policy->cpuinfo.max_freq;
88         policy->cur = dbx500_cpufreq_getspeed(policy->cpu);
89         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
90
91         /*
92          * FIXME : Need to take time measurement across the target()
93          *         function with no/some/all drivers in the notification
94          *         list.
95          */
96         policy->cpuinfo.transition_latency = 20 * 1000; /* in ns */
97
98         /* policy sharing between dual CPUs */
99         cpumask_setall(policy->cpus);
100
101         return 0;
102 }
103
104 static struct cpufreq_driver dbx500_cpufreq_driver = {
105         .flags  = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS,
106         .verify = cpufreq_generic_frequency_table_verify,
107         .target = dbx500_cpufreq_target,
108         .get    = dbx500_cpufreq_getspeed,
109         .init   = dbx500_cpufreq_init,
110         .name   = "DBX500",
111         .attr   = cpufreq_generic_attr,
112 };
113
114 static int dbx500_cpufreq_probe(struct platform_device *pdev)
115 {
116         int i = 0;
117
118         freq_table = dev_get_platdata(&pdev->dev);
119         if (!freq_table) {
120                 pr_err("dbx500-cpufreq: Failed to fetch cpufreq table\n");
121                 return -ENODEV;
122         }
123
124         armss_clk = clk_get(&pdev->dev, "armss");
125         if (IS_ERR(armss_clk)) {
126                 pr_err("dbx500-cpufreq: Failed to get armss clk\n");
127                 return PTR_ERR(armss_clk);
128         }
129
130         pr_info("dbx500-cpufreq: Available frequencies:\n");
131         while (freq_table[i].frequency != CPUFREQ_TABLE_END) {
132                 pr_info("  %d Mhz\n", freq_table[i].frequency/1000);
133                 i++;
134         }
135
136         return cpufreq_register_driver(&dbx500_cpufreq_driver);
137 }
138
139 static struct platform_driver dbx500_cpufreq_plat_driver = {
140         .driver = {
141                 .name = "cpufreq-ux500",
142                 .owner = THIS_MODULE,
143         },
144         .probe = dbx500_cpufreq_probe,
145 };
146
147 static int __init dbx500_cpufreq_register(void)
148 {
149         return platform_driver_register(&dbx500_cpufreq_plat_driver);
150 }
151 device_initcall(dbx500_cpufreq_register);
152
153 MODULE_LICENSE("GPL v2");
154 MODULE_DESCRIPTION("cpufreq driver for DBX500");