blob: 4dbe1dae937c49e25e479f0ebbc89a80076dcb80 [file] [log] [blame]
Joseph Lo0b25e252012-10-31 17:41:15 +08001/*
2 * CPU idle driver for Tegra CPUs
3 *
4 * Copyright (c) 2010-2012, NVIDIA Corporation.
5 * Copyright (c) 2011 Google, Inc.
6 * Author: Colin Cross <ccross@android.com>
7 * Gary King <gking@nvidia.com>
8 *
9 * Rework for 3.3 by Peter De Schrijver <pdeschrijver@nvidia.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * more details.
20 */
21
Thierry Redinga0524ac2014-07-11 09:44:49 +020022#include <linux/clk/tegra.h>
Thomas Gleixnera0b41222015-04-03 02:32:14 +020023#include <linux/tick.h>
Joseph Lo0b25e252012-10-31 17:41:15 +080024#include <linux/cpuidle.h>
Joseph Lod457ef352012-10-31 17:41:17 +080025#include <linux/cpu_pm.h>
Thierry Redinga0524ac2014-07-11 09:44:49 +020026#include <linux/kernel.h>
27#include <linux/module.h>
Joseph Lo0b25e252012-10-31 17:41:15 +080028
29#include <asm/cpuidle.h>
Joseph Lod457ef352012-10-31 17:41:17 +080030#include <asm/smp_plat.h>
Thierry Redinga0524ac2014-07-11 09:44:49 +020031#include <asm/suspend.h>
Joseph Lod457ef352012-10-31 17:41:17 +080032
33#include "pm.h"
34#include "sleep.h"
35
36#ifdef CONFIG_PM_SLEEP
37static int tegra30_idle_lp2(struct cpuidle_device *dev,
38 struct cpuidle_driver *drv,
39 int index);
40#endif
Joseph Lo0b25e252012-10-31 17:41:15 +080041
42static struct cpuidle_driver tegra_idle_driver = {
43 .name = "tegra_idle",
44 .owner = THIS_MODULE,
Joseph Lod457ef352012-10-31 17:41:17 +080045#ifdef CONFIG_PM_SLEEP
46 .state_count = 2,
47#else
Joseph Lo0b25e252012-10-31 17:41:15 +080048 .state_count = 1,
Joseph Lod457ef352012-10-31 17:41:17 +080049#endif
Joseph Lo0b25e252012-10-31 17:41:15 +080050 .states = {
51 [0] = ARM_CPUIDLE_WFI_STATE_PWR(600),
Joseph Lod457ef352012-10-31 17:41:17 +080052#ifdef CONFIG_PM_SLEEP
53 [1] = {
54 .enter = tegra30_idle_lp2,
55 .exit_latency = 2000,
56 .target_residency = 2200,
57 .power_usage = 0,
Joseph Lod457ef352012-10-31 17:41:17 +080058 .name = "powered-down",
59 .desc = "CPU power gated",
60 },
61#endif
Joseph Lo0b25e252012-10-31 17:41:15 +080062 },
63};
64
Joseph Lod457ef352012-10-31 17:41:17 +080065#ifdef CONFIG_PM_SLEEP
Joseph Lod5529202012-10-31 17:41:21 +080066static bool tegra30_cpu_cluster_power_down(struct cpuidle_device *dev,
67 struct cpuidle_driver *drv,
68 int index)
69{
Joseph Lod5529202012-10-31 17:41:21 +080070 /* All CPUs entering LP2 is not working.
71 * Don't let CPU0 enter LP2 when any secondary CPU is online.
72 */
73 if (num_online_cpus() > 1 || !tegra_cpu_rail_off_ready()) {
74 cpu_do_idle();
75 return false;
76 }
77
Thomas Gleixnera0b41222015-04-03 02:32:14 +020078 tick_broadcast_enter();
Joseph Lod5529202012-10-31 17:41:21 +080079
Joseph Lo4d82d052013-04-02 01:20:50 +000080 tegra_idle_lp2_last();
Joseph Lod5529202012-10-31 17:41:21 +080081
Thomas Gleixnera0b41222015-04-03 02:32:14 +020082 tick_broadcast_exit();
Joseph Lod5529202012-10-31 17:41:21 +080083
84 return true;
85}
86
Joseph Lod457ef352012-10-31 17:41:17 +080087#ifdef CONFIG_SMP
88static bool tegra30_cpu_core_power_down(struct cpuidle_device *dev,
89 struct cpuidle_driver *drv,
90 int index)
91{
Thomas Gleixnera0b41222015-04-03 02:32:14 +020092 tick_broadcast_enter();
Joseph Lod457ef352012-10-31 17:41:17 +080093
94 smp_wmb();
95
Joseph Lod457ef352012-10-31 17:41:17 +080096 cpu_suspend(0, tegra30_sleep_cpu_secondary_finish);
97
Thomas Gleixnera0b41222015-04-03 02:32:14 +020098 tick_broadcast_exit();
Joseph Lod457ef352012-10-31 17:41:17 +080099
100 return true;
101}
102#else
103static inline bool tegra30_cpu_core_power_down(struct cpuidle_device *dev,
104 struct cpuidle_driver *drv,
105 int index)
106{
107 return true;
108}
109#endif
110
Joseph Lo8c627fa2013-01-04 17:32:21 +0800111static int tegra30_idle_lp2(struct cpuidle_device *dev,
112 struct cpuidle_driver *drv,
113 int index)
Joseph Lod457ef352012-10-31 17:41:17 +0800114{
Joseph Lod457ef352012-10-31 17:41:17 +0800115 bool entered_lp2 = false;
Joseph Lod5529202012-10-31 17:41:21 +0800116 bool last_cpu;
Joseph Lod457ef352012-10-31 17:41:17 +0800117
118 local_fiq_disable();
119
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800120 last_cpu = tegra_set_cpu_in_lp2();
Joseph Lod457ef352012-10-31 17:41:17 +0800121 cpu_pm_enter();
122
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800123 if (dev->cpu == 0) {
Joseph Lod5529202012-10-31 17:41:21 +0800124 if (last_cpu)
125 entered_lp2 = tegra30_cpu_cluster_power_down(dev, drv,
126 index);
127 else
128 cpu_do_idle();
129 } else {
Joseph Lod457ef352012-10-31 17:41:17 +0800130 entered_lp2 = tegra30_cpu_core_power_down(dev, drv, index);
Joseph Lod5529202012-10-31 17:41:21 +0800131 }
Joseph Lod457ef352012-10-31 17:41:17 +0800132
133 cpu_pm_exit();
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800134 tegra_clear_cpu_in_lp2();
Joseph Lod457ef352012-10-31 17:41:17 +0800135
136 local_fiq_enable();
137
138 smp_rmb();
139
140 return (entered_lp2) ? index : 0;
141}
142#endif
143
Joseph Lo0b25e252012-10-31 17:41:15 +0800144int __init tegra30_cpuidle_init(void)
145{
Daniel Lezcanof040c262013-04-23 08:54:41 +0000146 return cpuidle_register(&tegra_idle_driver, NULL);
Joseph Lo0b25e252012-10-31 17:41:15 +0800147}