xref: /linux/arch/sh/kernel/cpu/shmobile/cpuidle.c (revision 2277ab4a1df50e05bc732fe9488d4e902bb8399a)
1 /*
2  * arch/sh/kernel/cpu/shmobile/cpuidle.c
3  *
4  * Cpuidle support code for SuperH Mobile
5  *
6  *  Copyright (C) 2009 Magnus Damm
7  *
8  * This file is subject to the terms and conditions of the GNU General Public
9  * License.  See the file "COPYING" in the main directory of this archive
10  * for more details.
11  */
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/io.h>
15 #include <linux/suspend.h>
16 #include <linux/cpuidle.h>
17 #include <asm/suspend.h>
18 #include <asm/uaccess.h>
19 #include <asm/hwblk.h>
20 
21 static unsigned long cpuidle_mode[] = {
22 	SUSP_SH_SLEEP, /* regular sleep mode */
23 	SUSP_SH_SLEEP | SUSP_SH_SF, /* sleep mode + self refresh */
24 };
25 
26 static int cpuidle_sleep_enter(struct cpuidle_device *dev,
27 			       struct cpuidle_state *state)
28 {
29 	unsigned long allowed_mode = arch_hwblk_sleep_mode();
30 	ktime_t before, after;
31 	int requested_state = state - &dev->states[0];
32 	int allowed_state;
33 	int k;
34 
35 	/* convert allowed mode to allowed state */
36 	for (k = ARRAY_SIZE(cpuidle_mode) - 1; k > 0; k--)
37 		if (cpuidle_mode[k] == allowed_mode)
38 			break;
39 
40 	allowed_state = k;
41 
42 	/* take the following into account for sleep mode selection:
43 	 * - allowed_state: best mode allowed by hardware (clock deps)
44 	 * - requested_state: best mode allowed by software (latencies)
45 	 */
46 	k = min_t(int, allowed_state, requested_state);
47 
48 	dev->last_state = &dev->states[k];
49 	before = ktime_get();
50 	sh_mobile_call_standby(cpuidle_mode[k]);
51 	after = ktime_get();
52 	return ktime_to_ns(ktime_sub(after, before)) >> 10;
53 }
54 
55 static struct cpuidle_device cpuidle_dev;
56 static struct cpuidle_driver cpuidle_driver = {
57 	.name =		"sh_idle",
58 	.owner =	THIS_MODULE,
59 };
60 
61 void sh_mobile_setup_cpuidle(void)
62 {
63 	struct cpuidle_device *dev = &cpuidle_dev;
64 	struct cpuidle_state *state;
65 	int i;
66 
67 	cpuidle_register_driver(&cpuidle_driver);
68 
69 	for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
70 		dev->states[i].name[0] = '\0';
71 		dev->states[i].desc[0] = '\0';
72 	}
73 
74 	i = CPUIDLE_DRIVER_STATE_START;
75 
76 	state = &dev->states[i++];
77 	snprintf(state->name, CPUIDLE_NAME_LEN, "C0");
78 	strncpy(state->desc, "SuperH Sleep Mode", CPUIDLE_DESC_LEN);
79 	state->exit_latency = 1;
80 	state->target_residency = 1 * 2;
81 	state->power_usage = 3;
82 	state->flags = 0;
83 	state->flags |= CPUIDLE_FLAG_SHALLOW;
84 	state->flags |= CPUIDLE_FLAG_TIME_VALID;
85 	state->enter = cpuidle_sleep_enter;
86 
87 	dev->safe_state = state;
88 
89 	state = &dev->states[i++];
90 	snprintf(state->name, CPUIDLE_NAME_LEN, "C1");
91 	strncpy(state->desc, "SuperH Sleep Mode [SF]", CPUIDLE_DESC_LEN);
92 	state->exit_latency = 100;
93 	state->target_residency = 1 * 2;
94 	state->power_usage = 1;
95 	state->flags = 0;
96 	state->flags |= CPUIDLE_FLAG_TIME_VALID;
97 	state->enter = cpuidle_sleep_enter;
98 
99 	dev->state_count = i;
100 
101 	cpuidle_register_device(dev);
102 }
103