2 * arch/sh/kernel/cpu/shmobile/cpuidle.c
4 * Cpuidle support code for SuperH Mobile
6 * Copyright (C) 2009 Magnus Damm
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
12 #include <linux/init.h>
13 #include <linux/kernel.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>
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 */
28 static int cpuidle_sleep_enter(struct cpuidle_device *dev,
29 struct cpuidle_driver *drv,
32 unsigned long allowed_mode = arch_hwblk_sleep_mode();
33 ktime_t before, after;
34 int requested_state = index;
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)
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)
49 k = min_t(int, allowed_state, requested_state);
52 sh_mobile_call_standby(cpuidle_mode[k]);
55 dev->last_residency = (int)ktime_to_ns(ktime_sub(after, before)) >> 10;
60 static struct cpuidle_device cpuidle_dev;
61 static struct cpuidle_driver cpuidle_driver = {
66 void sh_mobile_setup_cpuidle(void)
68 struct cpuidle_device *dev = &cpuidle_dev;
69 struct cpuidle_driver *drv = &cpuidle_driver;
70 struct cpuidle_state *state;
74 for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
75 drv->states[i].name[0] = '\0';
76 drv->states[i].desc[0] = '\0';
79 i = CPUIDLE_DRIVER_STATE_START;
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;
88 state->flags |= CPUIDLE_FLAG_TIME_VALID;
89 state->enter = cpuidle_sleep_enter;
91 drv->safe_state_index = i-1;
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]",
98 state->exit_latency = 100;
99 state->target_residency = 1 * 2;
100 state->power_usage = 1;
102 state->flags |= CPUIDLE_FLAG_TIME_VALID;
103 state->enter = cpuidle_sleep_enter;
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]",
111 state->exit_latency = 2300;
112 state->target_residency = 1 * 2;
113 state->power_usage = 1;
115 state->flags |= CPUIDLE_FLAG_TIME_VALID;
116 state->enter = cpuidle_sleep_enter;
119 drv->state_count = i;
120 dev->state_count = i;
122 cpuidle_register_driver(&cpuidle_driver);
124 cpuidle_register_device(dev);