9a6f051b382e1516e7f01cd205d05c9114abb91c
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-tegra / cpuidle.c
1 /*
2  * arch/arm/mach-tegra/cpuidle.c
3  *
4  * CPU idle driver for Tegra CPUs
5  *
6  * Copyright (c) 2010-2012, NVIDIA Corporation.
7  * Copyright (c) 2011 Google, Inc.
8  * Author: Colin Cross <ccross@android.com>
9  *         Gary King <gking@nvidia.com>
10  *
11  * Rework for 3.3 by Peter De Schrijver <pdeschrijver@nvidia.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
21  * more details.
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/cpu.h>
27 #include <linux/cpuidle.h>
28 #include <linux/hrtimer.h>
29
30 #include <asm/proc-fns.h>
31
32 static int tegra_idle_enter_lp3(struct cpuidle_device *dev,
33                                 struct cpuidle_driver *drv, int index);
34
35 struct cpuidle_driver tegra_idle_driver = {
36         .name = "tegra_idle",
37         .owner = THIS_MODULE,
38         .state_count = 1,
39         .states = {
40                 [0] = {
41                         .enter                  = tegra_idle_enter_lp3,
42                         .exit_latency           = 10,
43                         .target_residency       = 10,
44                         .power_usage            = 600,
45                         .flags                  = CPUIDLE_FLAG_TIME_VALID,
46                         .name                   = "LP3",
47                         .desc                   = "CPU flow-controlled",
48                 },
49         },
50 };
51
52 static DEFINE_PER_CPU(struct cpuidle_device, tegra_idle_device);
53
54 static int tegra_idle_enter_lp3(struct cpuidle_device *dev,
55         struct cpuidle_driver *drv, int index)
56 {
57         ktime_t enter, exit;
58         s64 us;
59
60         local_irq_disable();
61         local_fiq_disable();
62
63         enter = ktime_get();
64
65         cpu_do_idle();
66
67         exit = ktime_sub(ktime_get(), enter);
68         us = ktime_to_us(exit);
69
70         local_fiq_enable();
71         local_irq_enable();
72
73         dev->last_residency = us;
74
75         return index;
76 }
77
78 static int __init tegra_cpuidle_init(void)
79 {
80         int ret;
81         unsigned int cpu;
82         struct cpuidle_device *dev;
83         struct cpuidle_driver *drv = &tegra_idle_driver;
84
85         ret = cpuidle_register_driver(&tegra_idle_driver);
86         if (ret) {
87                 pr_err("CPUidle driver registration failed\n");
88                 return ret;
89         }
90
91         for_each_possible_cpu(cpu) {
92                 dev = &per_cpu(tegra_idle_device, cpu);
93                 dev->cpu = cpu;
94
95                 dev->state_count = drv->state_count;
96                 ret = cpuidle_register_device(dev);
97                 if (ret) {
98                         pr_err("CPU%u: CPUidle device registration failed\n",
99                                 cpu);
100                         return ret;
101                 }
102         }
103         return 0;
104 }
105 device_initcall(tegra_cpuidle_init);