Joseph Lo | 0b25e25 | 2012-10-31 17:41:15 +0800 | [diff] [blame] | 1 | /* |
| 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 Reding | a0524ac | 2014-07-11 09:44:49 +0200 | [diff] [blame] | 22 | #include <linux/clk/tegra.h> |
| 23 | #include <linux/clockchips.h> |
Joseph Lo | 0b25e25 | 2012-10-31 17:41:15 +0800 | [diff] [blame] | 24 | #include <linux/cpuidle.h> |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 25 | #include <linux/cpu_pm.h> |
Thierry Reding | a0524ac | 2014-07-11 09:44:49 +0200 | [diff] [blame] | 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
Joseph Lo | 0b25e25 | 2012-10-31 17:41:15 +0800 | [diff] [blame] | 28 | |
| 29 | #include <asm/cpuidle.h> |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 30 | #include <asm/proc-fns.h> |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 31 | #include <asm/smp_plat.h> |
Thierry Reding | a0524ac | 2014-07-11 09:44:49 +0200 | [diff] [blame] | 32 | #include <asm/suspend.h> |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 33 | |
| 34 | #include "pm.h" |
| 35 | #include "sleep.h" |
| 36 | |
| 37 | #ifdef CONFIG_PM_SLEEP |
| 38 | static int tegra30_idle_lp2(struct cpuidle_device *dev, |
| 39 | struct cpuidle_driver *drv, |
| 40 | int index); |
| 41 | #endif |
Joseph Lo | 0b25e25 | 2012-10-31 17:41:15 +0800 | [diff] [blame] | 42 | |
| 43 | static struct cpuidle_driver tegra_idle_driver = { |
| 44 | .name = "tegra_idle", |
| 45 | .owner = THIS_MODULE, |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 46 | #ifdef CONFIG_PM_SLEEP |
| 47 | .state_count = 2, |
| 48 | #else |
Joseph Lo | 0b25e25 | 2012-10-31 17:41:15 +0800 | [diff] [blame] | 49 | .state_count = 1, |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 50 | #endif |
Joseph Lo | 0b25e25 | 2012-10-31 17:41:15 +0800 | [diff] [blame] | 51 | .states = { |
| 52 | [0] = ARM_CPUIDLE_WFI_STATE_PWR(600), |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 53 | #ifdef CONFIG_PM_SLEEP |
| 54 | [1] = { |
| 55 | .enter = tegra30_idle_lp2, |
| 56 | .exit_latency = 2000, |
| 57 | .target_residency = 2200, |
| 58 | .power_usage = 0, |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 59 | .name = "powered-down", |
| 60 | .desc = "CPU power gated", |
| 61 | }, |
| 62 | #endif |
Joseph Lo | 0b25e25 | 2012-10-31 17:41:15 +0800 | [diff] [blame] | 63 | }, |
| 64 | }; |
| 65 | |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 66 | #ifdef CONFIG_PM_SLEEP |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 67 | static bool tegra30_cpu_cluster_power_down(struct cpuidle_device *dev, |
| 68 | struct cpuidle_driver *drv, |
| 69 | int index) |
| 70 | { |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 71 | /* All CPUs entering LP2 is not working. |
| 72 | * Don't let CPU0 enter LP2 when any secondary CPU is online. |
| 73 | */ |
| 74 | if (num_online_cpus() > 1 || !tegra_cpu_rail_off_ready()) { |
| 75 | cpu_do_idle(); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu); |
| 80 | |
Joseph Lo | 4d82d05 | 2013-04-02 01:20:50 +0000 | [diff] [blame] | 81 | tegra_idle_lp2_last(); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 82 | |
| 83 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu); |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 88 | #ifdef CONFIG_SMP |
| 89 | static bool tegra30_cpu_core_power_down(struct cpuidle_device *dev, |
| 90 | struct cpuidle_driver *drv, |
| 91 | int index) |
| 92 | { |
| 93 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu); |
| 94 | |
| 95 | smp_wmb(); |
| 96 | |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 97 | cpu_suspend(0, tegra30_sleep_cpu_secondary_finish); |
| 98 | |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 99 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu); |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | #else |
| 104 | static inline bool tegra30_cpu_core_power_down(struct cpuidle_device *dev, |
| 105 | struct cpuidle_driver *drv, |
| 106 | int index) |
| 107 | { |
| 108 | return true; |
| 109 | } |
| 110 | #endif |
| 111 | |
Joseph Lo | 8c627fa | 2013-01-04 17:32:21 +0800 | [diff] [blame] | 112 | static int tegra30_idle_lp2(struct cpuidle_device *dev, |
| 113 | struct cpuidle_driver *drv, |
| 114 | int index) |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 115 | { |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 116 | bool entered_lp2 = false; |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 117 | bool last_cpu; |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 118 | |
| 119 | local_fiq_disable(); |
| 120 | |
Joseph Lo | 8f6a0b6 | 2013-06-04 18:47:35 +0800 | [diff] [blame] | 121 | last_cpu = tegra_set_cpu_in_lp2(); |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 122 | cpu_pm_enter(); |
| 123 | |
Joseph Lo | 8f6a0b6 | 2013-06-04 18:47:35 +0800 | [diff] [blame] | 124 | if (dev->cpu == 0) { |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 125 | if (last_cpu) |
| 126 | entered_lp2 = tegra30_cpu_cluster_power_down(dev, drv, |
| 127 | index); |
| 128 | else |
| 129 | cpu_do_idle(); |
| 130 | } else { |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 131 | entered_lp2 = tegra30_cpu_core_power_down(dev, drv, index); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 132 | } |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 133 | |
| 134 | cpu_pm_exit(); |
Joseph Lo | 8f6a0b6 | 2013-06-04 18:47:35 +0800 | [diff] [blame] | 135 | tegra_clear_cpu_in_lp2(); |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 136 | |
| 137 | local_fiq_enable(); |
| 138 | |
| 139 | smp_rmb(); |
| 140 | |
| 141 | return (entered_lp2) ? index : 0; |
| 142 | } |
| 143 | #endif |
| 144 | |
Joseph Lo | 0b25e25 | 2012-10-31 17:41:15 +0800 | [diff] [blame] | 145 | int __init tegra30_cpuidle_init(void) |
| 146 | { |
Daniel Lezcano | f040c26 | 2013-04-23 08:54:41 +0000 | [diff] [blame] | 147 | return cpuidle_register(&tegra_idle_driver, NULL); |
Joseph Lo | 0b25e25 | 2012-10-31 17:41:15 +0800 | [diff] [blame] | 148 | } |