ARM: tegra12: set CPU rate to 2.2GHz for sku 0x87
[linux-3.10.git] / arch / arm / mach-tegra / tegra3_throttle.c
1 /*
2  * arch/arm/mach-tegra/tegra3_throttle.c
3  *
4  * Copyright (c) 2011-2013, NVIDIA CORPORATION. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/cpufreq.h>
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/err.h>
25 #include <linux/clk.h>
26 #include <linux/debugfs.h>
27 #include <linux/seq_file.h>
28 #include <linux/uaccess.h>
29 #include <linux/thermal.h>
30 #include <linux/module.h>
31 #include <mach/thermal.h>
32
33 #include "clock.h"
34 #include "cpu-tegra.h"
35
36 /* cpu_throttle_lock is tegra_cpu_lock from cpu-tegra.c */
37 static struct mutex *cpu_throttle_lock;
38 static DEFINE_MUTEX(bthrot_list_lock);
39 static LIST_HEAD(bthrot_list);
40 static int num_throt;
41 static struct cpufreq_frequency_table *cpu_freq_table;
42 static unsigned long cpu_throttle_lowest_speed;
43 static unsigned long cpu_cap_freq;
44
45 static struct {
46         const char *cap_name;
47         struct clk *cap_clk;
48         unsigned long cap_freq;
49 } cap_freqs_table[] = {
50 #ifdef CONFIG_ARCH_TEGRA_12x_SOC
51         { .cap_name = "cap.throttle.gbus" },
52 #endif
53 #ifdef CONFIG_TEGRA_GPU_DVFS
54         { .cap_name = "cap.throttle.c2bus" },
55         { .cap_name = "cap.throttle.c3bus" },
56 #else
57         { .cap_name = "cap.throttle.cbus" },
58 #endif
59         { .cap_name = "cap.throttle.sclk" },
60         { .cap_name = "cap.throttle.emc" },
61 };
62
63 static bool tegra_throttle_init_failed;
64
65 #define CAP_TBL_CAP_NAME(index) (cap_freqs_table[index].cap_name)
66 #define CAP_TBL_CAP_CLK(index)  (cap_freqs_table[index].cap_clk)
67 #define CAP_TBL_CAP_FREQ(index) (cap_freqs_table[index].cap_freq)
68
69 #ifndef CONFIG_TEGRA_THERMAL_THROTTLE_EXACT_FREQ
70 static unsigned long clip_to_table(unsigned long cpu_freq)
71 {
72         int i;
73
74         if (IS_ERR_OR_NULL(cpu_freq_table))
75                 return -EINVAL;
76
77         for (i = 0; cpu_freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
78                 if (cpu_freq_table[i].frequency > cpu_freq)
79                         break;
80         }
81         i = (i == 0) ? 0 : i-1;
82         return cpu_freq_table[i].frequency;
83 }
84 #else
85 static unsigned long clip_to_table(unsigned long cpu_freq)
86 {
87         return cpu_freq;
88 }
89 #endif /* CONFIG_TEGRA_THERMAL_THROTTLE_EXACT_FREQ */
90
91 unsigned long tegra_throttle_governor_speed(unsigned long requested_speed)
92 {
93         if (cpu_cap_freq == NO_CAP ||
94                         cpu_cap_freq == 0)
95                 return requested_speed;
96         return min(requested_speed, cpu_cap_freq);
97 }
98
99 bool tegra_is_throttling(int *count)
100 {
101         struct balanced_throttle *bthrot;
102         bool is_throttling = false;
103         int lcount = 0;
104
105         mutex_lock(&bthrot_list_lock);
106         list_for_each_entry(bthrot, &bthrot_list, node) {
107                 if (bthrot->cur_state)
108                         is_throttling = true;
109                 lcount += bthrot->throttle_count;
110         }
111         mutex_unlock(&bthrot_list_lock);
112
113         if (count)
114                 *count = lcount;
115         return is_throttling;
116 }
117
118 static int
119 tegra_throttle_get_max_state(struct thermal_cooling_device *cdev,
120                                 unsigned long *max_state)
121 {
122         struct balanced_throttle *bthrot = cdev->devdata;
123
124         *max_state = bthrot->throt_tab_size;
125
126         return 0;
127 }
128
129 static int
130 tegra_throttle_get_cur_state(struct thermal_cooling_device *cdev,
131                                 unsigned long *cur_state)
132 {
133         struct balanced_throttle *bthrot = cdev->devdata;
134
135         *cur_state = bthrot->cur_state;
136
137         return 0;
138 }
139
140 static void tegra_throttle_set_cap_clk(struct throttle_table *throt_tab,
141                                         int cap_clk_index)
142 {
143         unsigned long cap_rate, clk_rate;
144
145         if (tegra_throttle_init_failed)
146                 return;
147
148         cap_rate = throt_tab->cap_freqs[cap_clk_index];
149
150         if (cap_rate == NO_CAP)
151                 clk_rate = clk_get_max_rate(CAP_TBL_CAP_CLK(cap_clk_index-1));
152         else
153                 clk_rate = cap_rate * 1000UL;
154
155         if (CAP_TBL_CAP_FREQ(cap_clk_index-1) != clk_rate) {
156                 clk_set_rate(CAP_TBL_CAP_CLK(cap_clk_index-1), clk_rate);
157                 CAP_TBL_CAP_FREQ(cap_clk_index-1) = clk_rate;
158         }
159 }
160
161 static void
162 tegra_throttle_cap_freqs_update(struct throttle_table *throt_tab,
163                                 int direction)
164 {
165         int i;
166         int num_of_cap_clocks = ARRAY_SIZE(cap_freqs_table);
167
168         if (direction == 1) { /* performance up : throttle less */
169                 for (i = num_of_cap_clocks; i > 0; i--)
170                         tegra_throttle_set_cap_clk(throt_tab, i);
171         } else { /* performance down : throotle more */
172                 for (i = 1; i <= num_of_cap_clocks; i++)
173                         tegra_throttle_set_cap_clk(throt_tab, i);
174         }
175 }
176
177 static int
178 tegra_throttle_set_cur_state(struct thermal_cooling_device *cdev,
179                                 unsigned long cur_state)
180 {
181         struct balanced_throttle *bthrot = cdev->devdata;
182         int direction;
183         int i;
184         int num_of_cap_clocks = ARRAY_SIZE(cap_freqs_table);
185         unsigned long bthrot_speed;
186         struct throttle_table *throt_entry;
187         struct throttle_table cur_throt_freq;
188
189         if (cpu_freq_table == NULL)
190                 return 0;
191
192         if (bthrot->cur_state == cur_state)
193                 return 0;
194
195         if (bthrot->cur_state == 0 && cur_state)
196                 bthrot->throttle_count++;
197
198         direction = bthrot->cur_state >= cur_state;
199         bthrot->cur_state = cur_state;
200
201         for (i = 0; i <= num_of_cap_clocks; i++)
202                 cur_throt_freq.cap_freqs[i] = NO_CAP;
203
204         mutex_lock(&bthrot_list_lock);
205         list_for_each_entry(bthrot, &bthrot_list, node) {
206                 if (bthrot->cur_state) {
207                         throt_entry = &bthrot->throt_tab[bthrot->cur_state-1];
208                         for (i = 0; i <= num_of_cap_clocks; i++) {
209                                 cur_throt_freq.cap_freqs[i] = min(
210                                                 cur_throt_freq.cap_freqs[i],
211                                                 throt_entry->cap_freqs[i]);
212                         }
213                 }
214         }
215
216         tegra_throttle_cap_freqs_update(&cur_throt_freq, direction);
217
218         bthrot_speed = cur_throt_freq.cap_freqs[0];
219         if (bthrot_speed == CPU_THROT_LOW)
220                 bthrot_speed = cpu_throttle_lowest_speed;
221         else
222                 bthrot_speed = clip_to_table(bthrot_speed);
223
224         cpu_cap_freq = bthrot_speed;
225         tegra_cpu_set_speed_cap(NULL);
226         mutex_unlock(&bthrot_list_lock);
227
228         return 0;
229 }
230
231 static struct thermal_cooling_device_ops tegra_throttle_cooling_ops = {
232         .get_max_state = tegra_throttle_get_max_state,
233         .get_cur_state = tegra_throttle_get_cur_state,
234         .set_cur_state = tegra_throttle_set_cur_state,
235 };
236
237 #ifdef CONFIG_DEBUG_FS
238 static int table_show(struct seq_file *s, void *data)
239 {
240         struct balanced_throttle *bthrot = s->private;
241         int i, j;
242
243         for (i = 0; i < bthrot->throt_tab_size; i++) {
244                 /* CPU FREQ */
245                 seq_printf(s, "[%d] = %7lu",
246                         i, bthrot->throt_tab[i].cap_freqs[0]);
247
248                 /* OTHER DVFS MODULE FREQS */
249                 for (j = 1; j <= ARRAY_SIZE(cap_freqs_table); j++)
250                         seq_printf(s, " %7lu",
251                                 bthrot->throt_tab[i].cap_freqs[j]);
252                 seq_printf(s, "\n");
253         }
254
255         return 0;
256 }
257
258 static int table_open(struct inode *inode, struct file *file)
259 {
260         return single_open(file, table_show, inode->i_private);
261 }
262
263 static ssize_t table_write(struct file *file,
264         const char __user *userbuf, size_t count, loff_t *ppos)
265 {
266         struct balanced_throttle *bthrot =
267                         ((struct seq_file *)(file->private_data))->private;
268         char buf[80], temp_buf[10], *cur_pos;
269         int table_idx, i;
270         unsigned long cap_rate;
271
272         if (sizeof(buf) <= count)
273                 return -EINVAL;
274
275         if (copy_from_user(buf, userbuf, count))
276                 return -EFAULT;
277
278         /* terminate buffer and trim - white spaces may be appended
279          *  at the end when invoked from shell command line */
280         buf[count] = '\0';
281         strim(buf);
282         cur_pos = buf;
283
284         /* get table index */
285         if (sscanf(cur_pos, "[%d] = ", &table_idx) != 1)
286                 return -EINVAL;
287         sscanf(cur_pos, "[%s] = ", temp_buf);
288         cur_pos += strlen(temp_buf) + 4;
289         if ((table_idx < 0) || (table_idx >= bthrot->throt_tab_size))
290                 return -EINVAL;
291
292         /* CPU FREQ and DVFS FREQS == DVFS FREQS + 1(cpu) */
293         for (i = 0; i < ARRAY_SIZE(cap_freqs_table) + 1; i++) {
294                 if (sscanf(cur_pos, "%lu", &cap_rate) != 1)
295                         return -EINVAL;
296                 sscanf(cur_pos, "%s", temp_buf);
297                 cur_pos += strlen(temp_buf) + 1;
298
299                 bthrot->throt_tab[table_idx].cap_freqs[i] = cap_rate;
300         }
301
302         return count;
303 }
304
305 static const struct file_operations table_fops = {
306         .open           = table_open,
307         .read           = seq_read,
308         .write          = table_write,
309         .llseek         = seq_lseek,
310         .release        = single_release,
311 };
312
313 static struct dentry *throttle_debugfs_root;
314 #endif /* CONFIG_DEBUG_FS */
315
316
317 struct thermal_cooling_device *balanced_throttle_register(
318                 struct balanced_throttle *bthrot,
319                 char *type)
320 {
321 #ifdef CONFIG_DEBUG_FS
322         char name[32];
323 #endif
324         mutex_lock(&bthrot_list_lock);
325         num_throt++;
326         list_add(&bthrot->node, &bthrot_list);
327         mutex_unlock(&bthrot_list_lock);
328
329         bthrot->cdev = thermal_cooling_device_register(
330                                                 type,
331                                                 bthrot,
332                                                 &tegra_throttle_cooling_ops);
333
334         if (IS_ERR(bthrot->cdev)) {
335                 bthrot->cdev = NULL;
336                 return ERR_PTR(-ENODEV);
337         }
338
339 #ifdef CONFIG_DEBUG_FS
340         sprintf(name, "throttle_table%d", num_throt);
341         debugfs_create_file(name,0644, throttle_debugfs_root,
342                                 bthrot, &table_fops);
343 #endif
344
345         return bthrot->cdev;
346 }
347
348 int __init tegra_throttle_init(struct mutex *cpu_lock)
349 {
350         int i;
351         struct clk *c;
352         struct tegra_cpufreq_table_data *table_data =
353                 tegra_cpufreq_table_get();
354
355         if (IS_ERR_OR_NULL(table_data))
356                 return -EINVAL;
357
358         cpu_freq_table = table_data->freq_table;
359         cpu_throttle_lowest_speed =
360                 cpu_freq_table[table_data->throttle_lowest_index].frequency;
361
362         cpu_throttle_lock = cpu_lock;
363 #ifdef CONFIG_DEBUG_FS
364         throttle_debugfs_root = debugfs_create_dir("tegra_throttle", 0);
365 #endif
366
367         for (i = 0; i < ARRAY_SIZE(cap_freqs_table); i++) {
368                 c = tegra_get_clock_by_name(CAP_TBL_CAP_NAME(i));
369                 if (!c) {
370                         pr_err("tegra_throttle: cannot get clock %s\n",
371                                 CAP_TBL_CAP_NAME(i));
372                         tegra_throttle_init_failed = true;
373                         continue;
374                 }
375
376                 CAP_TBL_CAP_CLK(i) = c;
377                 CAP_TBL_CAP_FREQ(i) = clk_get_max_rate(c);
378         }
379         pr_info("tegra_throttle : init %s\n",
380                 tegra_throttle_init_failed ? "FAILED" : "passed");
381         return 0;
382 }
383
384 void tegra_throttle_exit(void)
385 {
386 #ifdef CONFIG_DEBUG_FS
387         debugfs_remove_recursive(throttle_debugfs_root);
388 #endif
389 }
390