ARM: tegra12: set CPU rate to 2.2GHz for sku 0x87
[linux-3.10.git] / arch / arm / mach-tegra / edp_core.c
1 /*
2  * arch/arm/mach-tegra/edp_core.c
3  *
4  * Copyright (C) 2012-2013, NVIDIA Corporation. All rights reserved.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/clk.h>
21 #include <linux/kobject.h>
22 #include <linux/err.h>
23 #include <linux/suspend.h>
24 #include <linux/tegra-soc.h>
25
26 #include <mach/edp.h>
27 #include <mach/thermal.h>
28
29 #include "clock.h"
30 #include "common.h"
31
32 static DEFINE_MUTEX(core_edp_lock);
33
34 static struct tegra_core_edp_limits core_edp_limits;
35 static const struct tegra_core_edp_limits *limits;
36
37 static bool core_edp_disabled;
38
39 static bool core_edp_scpu_state;
40 static int core_edp_profile;
41 static int core_edp_modules_state;
42 static int core_edp_thermal_idx;
43 static int core_edp_suspended_profile = CORE_EDP_PROFILES_NUM;
44
45 static const char *profile_names[CORE_EDP_PROFILES_NUM] = {
46         [CORE_EDP_PROFILE_BALANCED]  = "profile_balanced",
47         [CORE_EDP_PROFILE_FAVOR_GPU] = "profile_favor_gpu",
48         [CORE_EDP_PROFILE_FAVOR_EMC] = "profile_favor_emc",
49 };
50
51 static unsigned long *get_cap_rates(bool scpu_state, int profile,
52                                     int m_state, int t_idx)
53 {
54         unsigned long *cap_rates = scpu_state ?
55                 limits->cap_rates_scpu_on : limits->cap_rates_scpu_off;
56
57         cap_rates += ((profile * limits->core_modules_states + m_state) *
58                       limits->temperature_ranges + t_idx) *
59                       limits->cap_clocks_num;
60
61         return cap_rates;
62 }
63
64 static unsigned long *get_current_cap_rates(void)
65 {
66         return get_cap_rates(core_edp_scpu_state, core_edp_profile,
67                              core_edp_modules_state, core_edp_thermal_idx);
68 }
69
70 static int set_max_rates(void)
71 {
72         int i, ret;
73
74         if (core_edp_disabled)
75                 return 0;
76
77         for (i = 0; i < limits->cap_clocks_num; i++) {
78                 struct clk *c = limits->cap_clocks[i];
79                 ret = clk_set_rate(c, clk_get_max_rate(c));
80                 if (ret) {
81                         pr_err("%s: Failed to set %s max rate %lu\n",
82                                __func__, c->name, clk_get_max_rate(c));
83                         return ret;
84                 }
85         }
86         return 0;
87 }
88
89 static int set_cap_rates(unsigned long *new_rates)
90 {
91         int i, ret;
92
93         if (core_edp_disabled)
94                 return 0;
95
96         for (i = 0; i < limits->cap_clocks_num; i++) {
97                 struct clk *c = limits->cap_clocks[i];
98                 ret = clk_set_rate(c, new_rates[i]);
99                 if (ret) {
100                         pr_err("%s: Failed to set %s rate %lu\n",
101                                __func__, c->name, new_rates[i]);
102                         return ret;
103                 }
104         }
105         return 0;
106 }
107
108 static int update_cap_rates(unsigned long *new_rates, unsigned long *old_rates)
109 {
110         int i, ret;
111
112         if (core_edp_disabled)
113                 return 0;
114
115         /* 1st lower caps */
116         for (i = 0; i < limits->cap_clocks_num; i++) {
117                 if (new_rates[i] < old_rates[i]) {
118                         struct clk *c = limits->cap_clocks[i];
119                         ret = clk_set_rate(c, new_rates[i]);
120                         if (ret) {
121                                 pr_err("%s: Failed to set %s rate %lu\n",
122                                        __func__, c->name, new_rates[i]);
123                                 return ret;
124                         }
125
126                 }
127         }
128
129         /* then increase caps */
130         for (i = 0; i < limits->cap_clocks_num; i++) {
131                 if (new_rates[i] > old_rates[i]) {
132                         struct clk *c = limits->cap_clocks[i];
133                         ret = clk_set_rate(c, new_rates[i]);
134                         if (ret) {
135                                 pr_err("%s: Failed to set %s rate %lu\n",
136                                        __func__, c->name, new_rates[i]);
137                                 return ret;
138                         }
139
140                 }
141         }
142         return 0;
143 }
144
145 static int __init start_core_edp(void)
146 {
147         int ret;
148
149         /*
150          * Default state:
151          * always boot G-cluster (no cpu on core rail),
152          * non-throttled EMC profile
153          * all core modules that affect EDP are On
154          * unknown temperature - assume maximum (WC)
155          */
156         core_edp_scpu_state = false;
157         core_edp_profile = CORE_EDP_PROFILE_FAVOR_EMC;
158         core_edp_modules_state = 0;
159         core_edp_thermal_idx = limits->temperature_ranges - 1;
160
161         ret = set_cap_rates(get_current_cap_rates());
162         if (ret)
163                 return ret;
164
165         return 0;
166 }
167
168 void __init tegra_init_core_edp_limits(unsigned int regulator_mA)
169 {
170         int i;
171         unsigned long *cap_rates;
172         u32 tegra_chip_id;
173
174         tegra_chip_id = tegra_get_chip_id();
175         switch (tegra_chip_id) {
176         case TEGRA_CHIPID_TEGRA11:
177                 if (tegra11x_select_core_edp_table(
178                         regulator_mA, &core_edp_limits))
179                         return;
180                 break;
181         case TEGRA_CHIPID_TEGRA14:
182                 if (tegra14x_select_core_edp_table(
183                         regulator_mA, &core_edp_limits))
184                         return;
185                 break;
186         default:
187                 pr_err("%s: core edp is not supported on chip ID %d\n",
188                        __func__, tegra_chip_id);
189                 return;
190         }
191
192         limits = &core_edp_limits;
193
194         if (start_core_edp()) {
195                 WARN(1, "Core EDP failed to set initiali limits");
196                 return;
197         }
198
199         cap_rates = get_current_cap_rates();
200         pr_info("Core EDP limits are initialized at:\n");
201         for (i = 0; i < limits->cap_clocks_num; i++)
202                 pr_info("    %10s: %lu\n",
203                         limits->cap_clocks[i]->name, cap_rates[i]);
204 }
205
206 /* core edp cpu state update */
207 int tegra_core_edp_cpu_state_update(bool scpu_state)
208 {
209         int ret = 0;
210         unsigned long *old_cap_rates;
211         unsigned long *new_cap_rates;
212
213         if (!limits) {
214                 core_edp_scpu_state = scpu_state;
215                 return 0;
216         }
217
218         mutex_lock(&core_edp_lock);
219
220         if (core_edp_scpu_state != scpu_state) {
221                 old_cap_rates = get_current_cap_rates();
222                 new_cap_rates = get_cap_rates(scpu_state, core_edp_profile,
223                                 core_edp_modules_state, core_edp_thermal_idx);
224                 ret = update_cap_rates(new_cap_rates, old_cap_rates);
225                 if (ret)
226                         update_cap_rates(old_cap_rates, new_cap_rates);
227                 else
228                         core_edp_scpu_state = scpu_state;
229         }
230         mutex_unlock(&core_edp_lock);
231
232         return ret;
233 }
234
235 /* core edp profiles update */
236 static int  _profile_update(int profile)
237 {
238         int ret = 0;
239         unsigned long *old_cap_rates;
240         unsigned long *new_cap_rates;
241
242         if (core_edp_profile != profile) {
243                 old_cap_rates = get_current_cap_rates();
244                 new_cap_rates = get_cap_rates(core_edp_scpu_state, profile,
245                                 core_edp_modules_state, core_edp_thermal_idx);
246                 ret = update_cap_rates(new_cap_rates, old_cap_rates);
247                 if (ret)
248                         update_cap_rates(old_cap_rates, new_cap_rates);
249                 else
250                         core_edp_profile = profile;
251         }
252
253         return ret;
254 }
255
256 static int profile_update(int profile)
257 {
258         int ret = 0;
259
260         if (!limits) {
261                 core_edp_profile = profile;
262                 return 0;
263         }
264
265         mutex_lock(&core_edp_lock);
266         if (core_edp_suspended_profile == CORE_EDP_PROFILES_NUM)
267                 ret = _profile_update(profile);
268         mutex_unlock(&core_edp_lock);
269         return ret;
270 }
271
272 static ssize_t
273 core_edp_profile_show(struct kobject *kobj, struct kobj_attribute *attr,
274                       char *buf)
275 {
276         return sprintf(buf, "%s\n", profile_names[core_edp_profile]);
277 }
278 static ssize_t
279 core_edp_profile_store(struct kobject *kobj, struct kobj_attribute *attr,
280                        const char *buf, size_t count)
281 {
282         int i, ret;
283         size_t l;
284         const char *name;
285
286         for (i = 0; i < ARRAY_SIZE(profile_names); i++) {
287                 name = profile_names[i];
288                 l = strlen(name);
289                 if ((l < count) && (strncmp(buf, name, l) == 0))
290                         break;
291         }
292         if (i == ARRAY_SIZE(profile_names))
293                 return -ENOENT;
294
295         ret = profile_update(i);
296         if (ret)
297                 return ret;
298
299         return count;
300 }
301
302 static ssize_t
303 available_profiles_show(struct kobject *kobj, struct kobj_attribute *attr,
304                       char *buf)
305 {
306         int i;
307         ssize_t n = 0;
308         const char *name;
309
310         for (i = 0; i < ARRAY_SIZE(profile_names); i++) {
311                 name = profile_names[i];
312                 if ((n + strlen(name) + 2) > PAGE_SIZE)
313                         break;
314                 n += sprintf(&buf[n], "%s\n", name);
315         }
316
317         return n;
318 }
319
320 static struct kobj_attribute profile_attribute =
321         __ATTR(profile, 0644, core_edp_profile_show, core_edp_profile_store);
322 static struct kobj_attribute available_profiles_attribute =
323         __ATTR_RO(available_profiles);
324
325 const struct attribute *core_edp_attributes[] = {
326         &profile_attribute.attr,
327         &available_profiles_attribute.attr,
328         NULL,
329 };
330
331 static struct kobject *core_edp_kobj;
332
333 /* core edp temperature update */
334 static int core_edp_get_cdev_max_state(struct thermal_cooling_device *cdev,
335                                        unsigned long *max_state)
336 {
337         *max_state = limits ? limits->temperature_ranges - 1 : 0;
338         return 0;
339 }
340
341 static int core_edp_get_cdev_cur_state(struct thermal_cooling_device *cdev,
342                                        unsigned long *cur_state)
343 {
344         *cur_state = core_edp_thermal_idx;
345         return 0;
346 }
347
348 static int core_edp_set_cdev_state(struct thermal_cooling_device *cdev,
349                                    unsigned long cur_state)
350 {
351         int ret = 0;
352         unsigned long *old_cap_rates;
353         unsigned long *new_cap_rates;
354
355         if (!limits) {
356                 core_edp_thermal_idx = cur_state;
357                 return 0;
358         }
359
360         mutex_lock(&core_edp_lock);
361
362         if (core_edp_thermal_idx != cur_state) {
363                 old_cap_rates = get_current_cap_rates();
364                 new_cap_rates = get_cap_rates(
365                         core_edp_scpu_state, core_edp_profile,
366                         core_edp_modules_state, cur_state);
367                 ret = update_cap_rates(new_cap_rates, old_cap_rates);
368                 /*
369                  * Unlike updating other state variables, temperature change
370                  * must be always "accepted" (it's already happened) - just
371                  * re-try one more time in case of error
372                  */
373                 if (ret)
374                         update_cap_rates(new_cap_rates, old_cap_rates);
375
376                 core_edp_thermal_idx = cur_state;
377         }
378         mutex_unlock(&core_edp_lock);
379
380         return 0;
381 }
382
383 static struct thermal_cooling_device_ops core_edp_cooling_ops = {
384         .get_max_state = core_edp_get_cdev_max_state,
385         .get_cur_state = core_edp_get_cdev_cur_state,
386         .set_cur_state = core_edp_set_cdev_state,
387 };
388
389 static struct tegra_cooling_device core_edp_cdev;
390
391 struct tegra_cooling_device *tegra_core_edp_get_cdev(void)
392 {
393         if (!limits)
394                 return NULL;
395
396         if (!core_edp_cdev.cdev_type) {
397                 core_edp_cdev.cdev_type = "core_edp";
398                 core_edp_cdev.trip_temperatures = limits->temperatures;
399                 core_edp_cdev.trip_temperatures_num =
400                         limits->temperature_ranges-1;
401         }
402         return &core_edp_cdev;
403 }
404
405 /*
406  * Since EMC rate on suspend exit is set to boot configuration with no regards
407  * to EDP constraints, force profile_favor_emc on suspend entry, and restore
408  * suspended profile after resume. This guarantees that other clocks (GPU)
409  * are throttled enough to prevent regulator over-current.
410  */
411 static int core_edp_pm_notify(struct notifier_block *nb, unsigned long event,
412         void *dummy)
413 {
414         int ret = 0;
415         if (!limits)
416                 return NOTIFY_OK;
417
418         mutex_lock(&core_edp_lock);
419         if (event == PM_SUSPEND_PREPARE) {
420                 core_edp_suspended_profile = core_edp_profile;
421                 ret = _profile_update(CORE_EDP_PROFILE_FAVOR_EMC);
422                 if (ret)
423                         pr_err("Core EDP suspend: failed to set %s\n",
424                                profile_names[CORE_EDP_PROFILE_FAVOR_EMC]);
425                 else
426                         pr_info("Core EDP suspend: set %s\n",
427                                 profile_names[CORE_EDP_PROFILE_FAVOR_EMC]);
428         } else if (event == PM_POST_SUSPEND) {
429                 ret = _profile_update(core_edp_suspended_profile);
430                 if (ret)
431                         pr_err("Core EDP resume: failed to restore %s\n",
432                                profile_names[core_edp_suspended_profile]);
433                 else
434                         pr_info("Core EDP resume: restored %s\n",
435                                 profile_names[core_edp_suspended_profile]);
436                 core_edp_suspended_profile = CORE_EDP_PROFILES_NUM;
437                 ret = 0; /* don't stop resume */
438         }
439         mutex_unlock(&core_edp_lock);
440         return notifier_from_errno(ret);
441 }
442
443 static struct notifier_block core_edp_pm_notifier = {
444         .notifier_call = core_edp_pm_notify,
445 };
446
447 /* initialize update interfaces */
448 static int __init tegra_core_edp_late_init(void)
449 {
450         if (!limits)
451                 return 0;
452
453         /* continue on error - initialized at max temperature, anyway */
454         if (IS_ERR_OR_NULL(thermal_cooling_device_register(
455                 core_edp_cdev.cdev_type, NULL, &core_edp_cooling_ops)))
456                 pr_err("%s: failed to register edp cooling device\n", __func__);
457
458         /* exit on error - prevent changing profile_favor_emc  */
459         if (register_pm_notifier(&core_edp_pm_notifier)) {
460                 pr_err("%s: failed to register edp pm notifier\n", __func__);
461                 return 0;
462         }
463
464         core_edp_kobj = kobject_create_and_add("tegra_core_edp", kernel_kobj);
465         if (!core_edp_kobj) {
466                 pr_err("%s: failed to create edp sysfs object\n", __func__);
467                 return 0;
468         }
469
470         if (sysfs_create_files(core_edp_kobj, core_edp_attributes)) {
471                 pr_err("%s: failed to create edp profile sysfs interface\n",
472                        __func__);
473                 return 0;
474         }
475         pr_info("Core EDP sysfs interface is initialized\n");
476
477         return 0;
478 }
479 late_initcall(tegra_core_edp_late_init);
480
481 #ifdef CONFIG_DEBUG_FS
482
483 static int edp_table_show(struct seq_file *s, void *data)
484 {
485         int i, j, k, l;
486         unsigned long *cap_rates;
487
488         seq_printf(s, "VDD_CORE EDP TABLE (cap rates in kHz)\n");
489
490         seq_printf(s, "%10s", " Temp.");
491         for (l = 0; l < limits->cap_clocks_num; l++)
492                 seq_printf(s, "%10s", limits->cap_clocks[l]->name);
493         seq_printf(s, "\n");
494         for (l = 0; l < 10+10*limits->cap_clocks_num; l++)
495                 seq_printf(s, "-");
496         seq_printf(s, "\n");
497
498         seq_printf(s, "SCPU ON\n");
499         for (i = 0; i < CORE_EDP_PROFILES_NUM; i++) {
500                 seq_printf(s, "%-19s%d\n", profile_names[i], i);
501                 for (j = 0; j < limits->core_modules_states; j++) {
502                         seq_printf(s, "%-19s%d\n", "modules_state", j);
503                         for (k = 0; k < limits->temperature_ranges; k++) {
504                                 seq_printf(s, "%8dC:", limits->temperatures[k]);
505                                 cap_rates = get_cap_rates(true, i, j, k);
506                                 for (l = 0; l < limits->cap_clocks_num; l++)
507                                         seq_printf(s, "%10lu",
508                                                    cap_rates[l]/1000);
509                                 seq_printf(s, "\n");
510                         }
511                 }
512         }
513
514         seq_printf(s, "SCPU OFF\n");
515         for (i = 0; i < CORE_EDP_PROFILES_NUM; i++) {
516                 seq_printf(s, "%-19s%d\n", profile_names[i], i);
517                 for (j = 0; j < limits->core_modules_states; j++) {
518                         seq_printf(s, "%-19s%d\n", "modules_state", j);
519                         for (k = 0; k < limits->temperature_ranges; k++) {
520                                 seq_printf(s, "%8dC:", limits->temperatures[k]);
521                                 cap_rates = get_cap_rates(false, i, j, k);
522                                 for (l = 0; l < limits->cap_clocks_num; l++)
523                                         seq_printf(s, "%10lu",
524                                                    cap_rates[l]/1000);
525                                 seq_printf(s, "\n");
526                         }
527                 }
528         }
529
530         return 0;
531 }
532 static int edp_table_open(struct inode *inode, struct file *file)
533 {
534         return single_open(file, edp_table_show, inode->i_private);
535 }
536 static const struct file_operations edp_table_fops = {
537         .open           = edp_table_open,
538         .read           = seq_read,
539         .llseek         = seq_lseek,
540         .release        = single_release,
541 };
542
543 static int profile_show(struct seq_file *s, void *data)
544 {
545         seq_printf(s, "%s\n", profile_names[core_edp_profile]);
546         return 0;
547 }
548 static int profile_open(struct inode *inode, struct file *file)
549 {
550         return single_open(file, profile_show, inode->i_private);
551 }
552 static const struct file_operations profile_fops = {
553         .open           = profile_open,
554         .read           = seq_read,
555         .llseek         = seq_lseek,
556         .release        = single_release,
557 };
558
559 static int range_show(struct seq_file *s, void *data)
560 {
561         seq_printf(s, "%d\n", limits->temperatures[core_edp_thermal_idx]);
562         return 0;
563 }
564 static int range_open(struct inode *inode, struct file *file)
565 {
566         return single_open(file, range_show, inode->i_private);
567 }
568 static const struct file_operations range_fops = {
569         .open           = range_open,
570         .read           = seq_read,
571         .llseek         = seq_lseek,
572         .release        = single_release,
573 };
574
575 static int rates_show(struct seq_file *s, void *data)
576 {
577         int i;
578         unsigned long *cap_rates;
579
580         mutex_lock(&core_edp_lock);
581         cap_rates = get_current_cap_rates();
582         mutex_unlock(&core_edp_lock);
583
584         for (i = 0; i < limits->cap_clocks_num; i++)
585                 seq_printf(s, "%-10srate (kHz): %lu\n",
586                            limits->cap_clocks[i]->name, cap_rates[i] / 1000);
587         return 0;
588 }
589 static int rates_open(struct inode *inode, struct file *file)
590 {
591         return single_open(file, rates_show, inode->i_private);
592 }
593 static const struct file_operations rates_fops = {
594         .open           = rates_open,
595         .read           = seq_read,
596         .llseek         = seq_lseek,
597         .release        = single_release,
598 };
599
600 static int disable_edp_get(void *data, u64 *val)
601 {
602         *val = core_edp_disabled;
603         return 0;
604 }
605 static int disable_edp_set(void *data, u64 val)
606 {
607         int ret;
608         unsigned long *cap_rates;
609         bool disable = val ? true : false;
610
611         if (!limits) {
612                 core_edp_disabled = disable;
613                 return 0;
614         }
615
616         mutex_lock(&core_edp_lock);
617
618         if (core_edp_disabled != disable) {
619                 if (disable) {
620                         ret = set_max_rates();
621                         core_edp_disabled = true;
622                 } else {
623                         core_edp_disabled = false;
624                         cap_rates = get_current_cap_rates();
625                         ret = set_cap_rates(cap_rates);
626                 }
627                 pr_info("Core EDP %s%s\n", disable ? "disabled" : "enabled",
628                         ret ? " with incomplete limits" : "");
629         }
630         mutex_unlock(&core_edp_lock);
631         return 0;
632 }
633 DEFINE_SIMPLE_ATTRIBUTE(disable_edp_fops,
634                         disable_edp_get, disable_edp_set, "%llu\n");
635
636 int __init tegra_core_edp_debugfs_init(struct dentry *edp_dir)
637 {
638         struct dentry *dir, *d;
639
640         if (!limits)
641                 return 0;
642
643         dir = debugfs_create_dir("vdd_core", edp_dir);
644         if (!dir)
645                 return -ENOMEM;
646
647         d = debugfs_create_file("edp", S_IRUGO, dir, NULL, &edp_table_fops);
648         if (!d)
649                 goto err_out;
650
651         d = debugfs_create_bool("scpu_state", S_IRUGO, dir,
652                                 (u32 *)&core_edp_scpu_state);
653         if (!d)
654                 goto err_out;
655
656         d = debugfs_create_file("profile", S_IRUGO, dir, NULL, &profile_fops);
657         if (!d)
658                 goto err_out;
659
660         d = debugfs_create_u32("modules_state", S_IRUGO, dir,
661                                (u32 *)&core_edp_modules_state);
662         if (!d)
663                 goto err_out;
664
665         d = debugfs_create_file("therm_range", S_IRUGO, dir, NULL, &range_fops);
666         if (!d)
667                 goto err_out;
668
669         d = debugfs_create_file("rates", S_IRUGO, dir, NULL, &rates_fops);
670         if (!d)
671                 goto err_out;
672
673         d = debugfs_create_file("disable_edp", S_IRUGO | S_IWUSR, dir, NULL,
674                                 &disable_edp_fops);
675         if (!d)
676                 goto err_out;
677         return 0;
678
679 err_out:
680         debugfs_remove_recursive(dir);
681         return -ENOMEM;
682
683
684 }
685
686 #endif