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