blob: 6738955af37f8b963e71188a541b861ebefa93e0 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/cpuidle.h>
Ashwin Chaugule464983a2011-11-21 14:51:51 -050017#include <linux/cpu_pm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070018
19#include "cpuidle.h"
20#include "pm.h"
21
22static DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuidle_device, msm_cpuidle_devs);
23static struct cpuidle_driver msm_cpuidle_driver = {
24 .name = "msm_idle",
25 .owner = THIS_MODULE,
26};
27
28#ifdef CONFIG_MSM_SLEEP_STATS
29static DEFINE_PER_CPU(struct atomic_notifier_head, msm_cpuidle_notifiers);
30
31int msm_cpuidle_register_notifier(unsigned int cpu, struct notifier_block *nb)
32{
33 struct atomic_notifier_head *head =
34 &per_cpu(msm_cpuidle_notifiers, cpu);
35
36 return atomic_notifier_chain_register(head, nb);
37}
38EXPORT_SYMBOL(msm_cpuidle_register_notifier);
39
40int msm_cpuidle_unregister_notifier(unsigned int cpu, struct notifier_block *nb)
41{
42 struct atomic_notifier_head *head =
43 &per_cpu(msm_cpuidle_notifiers, cpu);
44
45 return atomic_notifier_chain_unregister(head, nb);
46}
47EXPORT_SYMBOL(msm_cpuidle_unregister_notifier);
48#endif
49
50static int msm_cpuidle_enter(
51 struct cpuidle_device *dev, struct cpuidle_state *state)
52{
53 int ret;
54#ifdef CONFIG_MSM_SLEEP_STATS
55 struct atomic_notifier_head *head =
56 &__get_cpu_var(msm_cpuidle_notifiers);
57#endif
58
59 local_irq_disable();
60
61#ifdef CONFIG_MSM_SLEEP_STATS
62 atomic_notifier_call_chain(head, MSM_CPUIDLE_STATE_ENTER, NULL);
63#endif
64
Ashwin Chaugule464983a2011-11-21 14:51:51 -050065#ifdef CONFIG_CPU_PM
66 cpu_pm_enter();
67#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068 ret = msm_pm_idle_enter((enum msm_pm_sleep_mode) (state->driver_data));
69
Ashwin Chaugule464983a2011-11-21 14:51:51 -050070#ifdef CONFIG_CPU_PM
71 cpu_pm_exit();
72#endif
73
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074#ifdef CONFIG_MSM_SLEEP_STATS
75 atomic_notifier_call_chain(head, MSM_CPUIDLE_STATE_EXIT, NULL);
76#endif
77
78 local_irq_enable();
79
80 return ret;
81}
82
83void __init msm_cpuidle_set_states(struct msm_cpuidle_state *states,
84 int nr_states, struct msm_pm_platform_data *pm_data)
85{
86 unsigned int cpu;
87
88 for_each_possible_cpu(cpu) {
89 struct cpuidle_device *dev = &per_cpu(msm_cpuidle_devs, cpu);
90 int i;
91
92 dev->cpu = cpu;
93 dev->prepare = msm_pm_idle_prepare;
94
95 for (i = 0; i < nr_states; i++) {
96 struct msm_cpuidle_state *cstate = &states[i];
97 struct cpuidle_state *state;
98 struct msm_pm_platform_data *pm_mode;
99
100 if (cstate->cpu != cpu)
101 continue;
102
103 state = &dev->states[cstate->state_nr];
104 pm_mode = &pm_data[MSM_PM_MODE(cpu, cstate->mode_nr)];
105
106 snprintf(state->name, CPUIDLE_NAME_LEN, cstate->name);
107 snprintf(state->desc, CPUIDLE_DESC_LEN, cstate->desc);
108 state->driver_data = (void *) cstate->mode_nr;
109 state->flags = CPUIDLE_FLAG_TIME_VALID;
110 state->exit_latency = pm_mode->latency;
111 state->power_usage = 0;
112 state->target_residency = pm_mode->residency;
113 state->enter = msm_cpuidle_enter;
114 }
115
116 for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
117 if (dev->states[i].enter == NULL)
118 break;
119 dev->state_count = i + 1;
120 }
121 }
122}
123
124int __init msm_cpuidle_init(void)
125{
126 unsigned int cpu;
127 int ret;
128
129 ret = cpuidle_register_driver(&msm_cpuidle_driver);
130 if (ret)
131 pr_err("%s: failed to register cpuidle driver: %d\n",
132 __func__, ret);
133
134 for_each_possible_cpu(cpu) {
135 struct cpuidle_device *dev = &per_cpu(msm_cpuidle_devs, cpu);
136
137 ret = cpuidle_register_device(dev);
138 if (ret) {
139 pr_err("%s: failed to register cpuidle device for "
140 "cpu %u: %d\n", __func__, cpu, ret);
141 return ret;
142 }
143 }
144
145 return 0;
146}
147
148static int __init msm_cpuidle_early_init(void)
149{
150#ifdef CONFIG_MSM_SLEEP_STATS
151 unsigned int cpu;
152
153 for_each_possible_cpu(cpu)
154 ATOMIC_INIT_NOTIFIER_HEAD(&per_cpu(msm_cpuidle_notifiers, cpu));
155#endif
156 return 0;
157}
158
159early_initcall(msm_cpuidle_early_init);