1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2013, NVIDIA Corporation. All rights reserved.
4  */
5 
6 #include <asm/firmware.h>
7 #include <linux/tick.h>
8 #include <linux/cpuidle.h>
9 #include <linux/cpu_pm.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 
13 #include <linux/firmware/trusted_foundations.h>
14 
15 #include <asm/cpuidle.h>
16 #include <asm/smp_plat.h>
17 #include <asm/suspend.h>
18 #include <asm/psci.h>
19 
20 #include "cpuidle.h"
21 #include "pm.h"
22 #include "sleep.h"
23 
24 #ifdef CONFIG_PM_SLEEP
25 #define TEGRA114_MAX_STATES 2
26 #else
27 #define TEGRA114_MAX_STATES 1
28 #endif
29 
30 #ifdef CONFIG_PM_SLEEP
tegra114_idle_power_down(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)31 static int tegra114_idle_power_down(struct cpuidle_device *dev,
32 				    struct cpuidle_driver *drv,
33 				    int index)
34 {
35 	local_fiq_disable();
36 
37 	tegra_set_cpu_in_lp2();
38 	cpu_pm_enter();
39 
40 	call_firmware_op(prepare_idle, TF_PM_MODE_LP2_NOFLUSH_L2);
41 
42 	/* Do suspend by ourselves if the firmware does not implement it */
43 	if (call_firmware_op(do_idle, 0) == -ENOSYS)
44 		cpu_suspend(0, tegra30_sleep_cpu_secondary_finish);
45 
46 	cpu_pm_exit();
47 	tegra_clear_cpu_in_lp2();
48 
49 	local_fiq_enable();
50 
51 	return index;
52 }
53 
tegra114_idle_enter_s2idle(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)54 static void tegra114_idle_enter_s2idle(struct cpuidle_device *dev,
55 				       struct cpuidle_driver *drv,
56 				       int index)
57 {
58        tegra114_idle_power_down(dev, drv, index);
59 }
60 #endif
61 
62 static struct cpuidle_driver tegra_idle_driver = {
63 	.name = "tegra_idle",
64 	.owner = THIS_MODULE,
65 	.state_count = TEGRA114_MAX_STATES,
66 	.states = {
67 		[0] = ARM_CPUIDLE_WFI_STATE_PWR(600),
68 #ifdef CONFIG_PM_SLEEP
69 		[1] = {
70 			.enter			= tegra114_idle_power_down,
71 			.enter_s2idle		= tegra114_idle_enter_s2idle,
72 			.exit_latency		= 500,
73 			.target_residency	= 1000,
74 			.flags			= CPUIDLE_FLAG_TIMER_STOP,
75 			.power_usage		= 0,
76 			.name			= "powered-down",
77 			.desc			= "CPU power gated",
78 		},
79 #endif
80 	},
81 };
82 
tegra114_cpuidle_init(void)83 int __init tegra114_cpuidle_init(void)
84 {
85 	if (!psci_smp_available())
86 		return cpuidle_register(&tegra_idle_driver, NULL);
87 
88 	return 0;
89 }
90