xref: /openbmc/linux/arch/sh/kernel/cpu/shmobile/cpuidle.c (revision 0a4f841e9c473be84a1ed0c14f65058c29238ce1)
17426394fSMagnus Damm /*
27426394fSMagnus Damm  * arch/sh/kernel/cpu/shmobile/cpuidle.c
37426394fSMagnus Damm  *
47426394fSMagnus Damm  * Cpuidle support code for SuperH Mobile
57426394fSMagnus Damm  *
67426394fSMagnus Damm  *  Copyright (C) 2009 Magnus Damm
77426394fSMagnus Damm  *
87426394fSMagnus Damm  * This file is subject to the terms and conditions of the GNU General Public
97426394fSMagnus Damm  * License.  See the file "COPYING" in the main directory of this archive
107426394fSMagnus Damm  * for more details.
117426394fSMagnus Damm  */
127426394fSMagnus Damm #include <linux/init.h>
137426394fSMagnus Damm #include <linux/kernel.h>
147426394fSMagnus Damm #include <linux/io.h>
157426394fSMagnus Damm #include <linux/suspend.h>
167426394fSMagnus Damm #include <linux/cpuidle.h>
17f7be3455SPaul Gortmaker #include <linux/export.h>
187426394fSMagnus Damm #include <asm/suspend.h>
197426394fSMagnus Damm #include <asm/uaccess.h>
207426394fSMagnus Damm 
217426394fSMagnus Damm static unsigned long cpuidle_mode[] = {
227426394fSMagnus Damm 	SUSP_SH_SLEEP, /* regular sleep mode */
237426394fSMagnus Damm 	SUSP_SH_SLEEP | SUSP_SH_SF, /* sleep mode + self refresh */
2463cd91dfSMagnus Damm 	SUSP_SH_STANDBY | SUSP_SH_SF, /* software standby mode + self refresh */
257426394fSMagnus Damm };
267426394fSMagnus Damm 
277426394fSMagnus Damm static int cpuidle_sleep_enter(struct cpuidle_device *dev,
2846bcfad7SDeepthi Dharwar 				struct cpuidle_driver *drv,
29e978aa7dSDeepthi Dharwar 				int index)
307426394fSMagnus Damm {
314be88734SPaul Mundt 	unsigned long allowed_mode = SUSP_SH_SLEEP;
32e978aa7dSDeepthi Dharwar 	int requested_state = index;
337426394fSMagnus Damm 	int allowed_state;
347426394fSMagnus Damm 	int k;
357426394fSMagnus Damm 
367426394fSMagnus Damm 	/* convert allowed mode to allowed state */
377426394fSMagnus Damm 	for (k = ARRAY_SIZE(cpuidle_mode) - 1; k > 0; k--)
387426394fSMagnus Damm 		if (cpuidle_mode[k] == allowed_mode)
397426394fSMagnus Damm 			break;
407426394fSMagnus Damm 
417426394fSMagnus Damm 	allowed_state = k;
427426394fSMagnus Damm 
437426394fSMagnus Damm 	/* take the following into account for sleep mode selection:
447426394fSMagnus Damm 	 * - allowed_state: best mode allowed by hardware (clock deps)
457426394fSMagnus Damm 	 * - requested_state: best mode allowed by software (latencies)
467426394fSMagnus Damm 	 */
477426394fSMagnus Damm 	k = min_t(int, allowed_state, requested_state);
487426394fSMagnus Damm 
497426394fSMagnus Damm 	sh_mobile_call_standby(cpuidle_mode[k]);
50e978aa7dSDeepthi Dharwar 
51e978aa7dSDeepthi Dharwar 	return k;
527426394fSMagnus Damm }
537426394fSMagnus Damm 
547426394fSMagnus Damm static struct cpuidle_device cpuidle_dev;
557426394fSMagnus Damm static struct cpuidle_driver cpuidle_driver = {
567426394fSMagnus Damm 	.name   = "sh_idle",
577426394fSMagnus Damm 	.owner  = THIS_MODULE,
585c48c873SRobert Lee 	.en_core_tk_irqen = 1,
59*0a4f841eSDaniel Lezcano 	.states = {
60*0a4f841eSDaniel Lezcano 		{
61*0a4f841eSDaniel Lezcano 			.exit_latency = 1,
62*0a4f841eSDaniel Lezcano 			.target_residency = 1 * 2,
63*0a4f841eSDaniel Lezcano 			.power_usage = 3,
64*0a4f841eSDaniel Lezcano 			.flags = CPUIDLE_FLAG_TIME_VALID,
65*0a4f841eSDaniel Lezcano 			.enter = cpuidle_sleep_enter,
66*0a4f841eSDaniel Lezcano 			.name = "C1",
67*0a4f841eSDaniel Lezcano 			.desc = "SuperH Sleep Mode",
68*0a4f841eSDaniel Lezcano 		},
69*0a4f841eSDaniel Lezcano 		{
70*0a4f841eSDaniel Lezcano 			.exit_latency = 100,
71*0a4f841eSDaniel Lezcano 			.target_residency = 1 * 2,
72*0a4f841eSDaniel Lezcano 			.power_usage = 1,
73*0a4f841eSDaniel Lezcano 			.flags = CPUIDLE_FLAG_TIME_VALID,
74*0a4f841eSDaniel Lezcano 			.enter = cpuidle_sleep_enter,
75*0a4f841eSDaniel Lezcano 			.name = "C2",
76*0a4f841eSDaniel Lezcano 			.desc = "SuperH Sleep Mode [SF]",
77*0a4f841eSDaniel Lezcano 			.disabled = true,
78*0a4f841eSDaniel Lezcano 		},
79*0a4f841eSDaniel Lezcano 		{
80*0a4f841eSDaniel Lezcano 			.exit_latency = 2300,
81*0a4f841eSDaniel Lezcano 			.target_residency = 1 * 2,
82*0a4f841eSDaniel Lezcano 			.power_usage = 1,
83*0a4f841eSDaniel Lezcano 			.flags = CPUIDLE_FLAG_TIME_VALID,
84*0a4f841eSDaniel Lezcano 			.enter = cpuidle_sleep_enter,
85*0a4f841eSDaniel Lezcano 			.name = "C3",
86*0a4f841eSDaniel Lezcano 			.desc = "SuperH Mobile Standby Mode [SF]",
87*0a4f841eSDaniel Lezcano 			.disabled = true,
88*0a4f841eSDaniel Lezcano 		},
89*0a4f841eSDaniel Lezcano 	},
90*0a4f841eSDaniel Lezcano 	.safe_state_index = 0,
91*0a4f841eSDaniel Lezcano 	.state_count = 3,
927426394fSMagnus Damm };
937426394fSMagnus Damm 
947426394fSMagnus Damm void sh_mobile_setup_cpuidle(void)
957426394fSMagnus Damm {
96*0a4f841eSDaniel Lezcano 	if (sh_mobile_sleep_supported & SUSP_SH_SF)
97*0a4f841eSDaniel Lezcano 		cpuidle_driver.states[1].disabled = false;
987426394fSMagnus Damm 
99*0a4f841eSDaniel Lezcano 	if (sh_mobile_sleep_supported & SUSP_SH_STANDBY)
100*0a4f841eSDaniel Lezcano 		cpuidle_driver.states[2].disabled = false;
1017426394fSMagnus Damm 
102*0a4f841eSDaniel Lezcano 	if (!cpuidle_register_driver(&cpuidle_driver))
103*0a4f841eSDaniel Lezcano 		cpuidle_register_device(&cpuidle_dev);
1047426394fSMagnus Damm }
105