blob: 4f22feb93940b34f841bc602c58127a6b40d3fbd [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>
17
18#include "cpuidle.h"
19#include "pm.h"
20
21static DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuidle_device, msm_cpuidle_devs);
22static struct cpuidle_driver msm_cpuidle_driver = {
23 .name = "msm_idle",
24 .owner = THIS_MODULE,
25};
26
27#ifdef CONFIG_MSM_SLEEP_STATS
28static DEFINE_PER_CPU(struct atomic_notifier_head, msm_cpuidle_notifiers);
29
30int msm_cpuidle_register_notifier(unsigned int cpu, struct notifier_block *nb)
31{
32 struct atomic_notifier_head *head =
33 &per_cpu(msm_cpuidle_notifiers, cpu);
34
35 return atomic_notifier_chain_register(head, nb);
36}
37EXPORT_SYMBOL(msm_cpuidle_register_notifier);
38
39int msm_cpuidle_unregister_notifier(unsigned int cpu, struct notifier_block *nb)
40{
41 struct atomic_notifier_head *head =
42 &per_cpu(msm_cpuidle_notifiers, cpu);
43
44 return atomic_notifier_chain_unregister(head, nb);
45}
46EXPORT_SYMBOL(msm_cpuidle_unregister_notifier);
47#endif
48
49static int msm_cpuidle_enter(
50 struct cpuidle_device *dev, struct cpuidle_state *state)
51{
52 int ret;
53#ifdef CONFIG_MSM_SLEEP_STATS
54 struct atomic_notifier_head *head =
55 &__get_cpu_var(msm_cpuidle_notifiers);
56#endif
57
58 local_irq_disable();
59
60#ifdef CONFIG_MSM_SLEEP_STATS
61 atomic_notifier_call_chain(head, MSM_CPUIDLE_STATE_ENTER, NULL);
62#endif
63
64 ret = msm_pm_idle_enter((enum msm_pm_sleep_mode) (state->driver_data));
65
66#ifdef CONFIG_MSM_SLEEP_STATS
67 atomic_notifier_call_chain(head, MSM_CPUIDLE_STATE_EXIT, NULL);
68#endif
69
70 local_irq_enable();
71
72 return ret;
73}
74
75void __init msm_cpuidle_set_states(struct msm_cpuidle_state *states,
76 int nr_states, struct msm_pm_platform_data *pm_data)
77{
78 unsigned int cpu;
79
80 for_each_possible_cpu(cpu) {
81 struct cpuidle_device *dev = &per_cpu(msm_cpuidle_devs, cpu);
82 int i;
83
84 dev->cpu = cpu;
85 dev->prepare = msm_pm_idle_prepare;
86
87 for (i = 0; i < nr_states; i++) {
88 struct msm_cpuidle_state *cstate = &states[i];
89 struct cpuidle_state *state;
90 struct msm_pm_platform_data *pm_mode;
91
92 if (cstate->cpu != cpu)
93 continue;
94
95 state = &dev->states[cstate->state_nr];
96 pm_mode = &pm_data[MSM_PM_MODE(cpu, cstate->mode_nr)];
97
98 snprintf(state->name, CPUIDLE_NAME_LEN, cstate->name);
99 snprintf(state->desc, CPUIDLE_DESC_LEN, cstate->desc);
100 state->driver_data = (void *) cstate->mode_nr;
101 state->flags = CPUIDLE_FLAG_TIME_VALID;
102 state->exit_latency = pm_mode->latency;
103 state->power_usage = 0;
104 state->target_residency = pm_mode->residency;
105 state->enter = msm_cpuidle_enter;
106 }
107
108 for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
109 if (dev->states[i].enter == NULL)
110 break;
111 dev->state_count = i + 1;
112 }
113 }
114}
115
116int __init msm_cpuidle_init(void)
117{
118 unsigned int cpu;
119 int ret;
120
121 ret = cpuidle_register_driver(&msm_cpuidle_driver);
122 if (ret)
123 pr_err("%s: failed to register cpuidle driver: %d\n",
124 __func__, ret);
125
126 for_each_possible_cpu(cpu) {
127 struct cpuidle_device *dev = &per_cpu(msm_cpuidle_devs, cpu);
128
129 ret = cpuidle_register_device(dev);
130 if (ret) {
131 pr_err("%s: failed to register cpuidle device for "
132 "cpu %u: %d\n", __func__, cpu, ret);
133 return ret;
134 }
135 }
136
137 return 0;
138}
139
140static int __init msm_cpuidle_early_init(void)
141{
142#ifdef CONFIG_MSM_SLEEP_STATS
143 unsigned int cpu;
144
145 for_each_possible_cpu(cpu)
146 ATOMIC_INIT_NOTIFIER_HEAD(&per_cpu(msm_cpuidle_notifiers, cpu));
147#endif
148 return 0;
149}
150
151early_initcall(msm_cpuidle_early_init);