xref: /linux/arch/sh/kernel/cpu/shmobile/cpuidle.c (revision 068df0f34e81bc06c5eb5012ec2eda25624e87aa)
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 <linux/export.h>
18 #include <asm/suspend.h>
19 #include <asm/uaccess.h>
20 #include <asm/hwblk.h>
21 
22 static unsigned long cpuidle_mode[] = {
23 	SUSP_SH_SLEEP, /* regular sleep mode */
24 	SUSP_SH_SLEEP | SUSP_SH_SF, /* sleep mode + self refresh */
25 	SUSP_SH_STANDBY | SUSP_SH_SF, /* software standby mode + self refresh */
26 };
27 
28 static int cpuidle_sleep_enter(struct cpuidle_device *dev,
29 				struct cpuidle_driver *drv,
30 				int index)
31 {
32 	unsigned long allowed_mode = arch_hwblk_sleep_mode();
33 	ktime_t before, after;
34 	int requested_state = index;
35 	int allowed_state;
36 	int k;
37 
38 	/* convert allowed mode to allowed state */
39 	for (k = ARRAY_SIZE(cpuidle_mode) - 1; k > 0; k--)
40 		if (cpuidle_mode[k] == allowed_mode)
41 			break;
42 
43 	allowed_state = k;
44 
45 	/* take the following into account for sleep mode selection:
46 	 * - allowed_state: best mode allowed by hardware (clock deps)
47 	 * - requested_state: best mode allowed by software (latencies)
48 	 */
49 	k = min_t(int, allowed_state, requested_state);
50 
51 	before = ktime_get();
52 	sh_mobile_call_standby(cpuidle_mode[k]);
53 	after = ktime_get();
54 
55 	dev->last_residency = (int)ktime_to_ns(ktime_sub(after, before)) >> 10;
56 
57 	return k;
58 }
59 
60 static struct cpuidle_device cpuidle_dev;
61 static struct cpuidle_driver cpuidle_driver = {
62 	.name =		"sh_idle",
63 	.owner =	THIS_MODULE,
64 };
65 
66 void sh_mobile_setup_cpuidle(void)
67 {
68 	struct cpuidle_device *dev = &cpuidle_dev;
69 	struct cpuidle_driver *drv = &cpuidle_driver;
70 	struct cpuidle_state *state;
71 	int i;
72 
73 
74 	for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
75 		drv->states[i].name[0] = '\0';
76 		drv->states[i].desc[0] = '\0';
77 	}
78 
79 	i = CPUIDLE_DRIVER_STATE_START;
80 
81 	state = &drv->states[i++];
82 	snprintf(state->name, CPUIDLE_NAME_LEN, "C1");
83 	strncpy(state->desc, "SuperH Sleep Mode", CPUIDLE_DESC_LEN);
84 	state->exit_latency = 1;
85 	state->target_residency = 1 * 2;
86 	state->power_usage = 3;
87 	state->flags = 0;
88 	state->flags |= CPUIDLE_FLAG_TIME_VALID;
89 	state->enter = cpuidle_sleep_enter;
90 
91 	drv->safe_state_index = i-1;
92 
93 	if (sh_mobile_sleep_supported & SUSP_SH_SF) {
94 		state = &drv->states[i++];
95 		snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
96 		strncpy(state->desc, "SuperH Sleep Mode [SF]",
97 			CPUIDLE_DESC_LEN);
98 		state->exit_latency = 100;
99 		state->target_residency = 1 * 2;
100 		state->power_usage = 1;
101 		state->flags = 0;
102 		state->flags |= CPUIDLE_FLAG_TIME_VALID;
103 		state->enter = cpuidle_sleep_enter;
104 	}
105 
106 	if (sh_mobile_sleep_supported & SUSP_SH_STANDBY) {
107 		state = &drv->states[i++];
108 		snprintf(state->name, CPUIDLE_NAME_LEN, "C3");
109 		strncpy(state->desc, "SuperH Mobile Standby Mode [SF]",
110 			CPUIDLE_DESC_LEN);
111 		state->exit_latency = 2300;
112 		state->target_residency = 1 * 2;
113 		state->power_usage = 1;
114 		state->flags = 0;
115 		state->flags |= CPUIDLE_FLAG_TIME_VALID;
116 		state->enter = cpuidle_sleep_enter;
117 	}
118 
119 	drv->state_count = i;
120 	dev->state_count = i;
121 
122 	cpuidle_register_driver(&cpuidle_driver);
123 
124 	cpuidle_register_device(dev);
125 }
126