summaryrefslogtreecommitdiff
path: root/arch/arm/plat-mxc/cpuidle.c
blob: 40c192420f455c7cc4fcee44b9e5d137e8b6ff51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright 2011 Freescale Semiconductor, Inc.
 * Copyright 2011 Linaro Ltd.
 *
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/cpuidle.h>
#include <linux/err.h>
#include <asm/proc-fns.h>
#include <mach/cpuidle.h>

static int (*mach_cpuidle)(struct cpuidle_device *dev,
			       struct cpuidle_state *state);
static struct cpuidle_driver *imx_cpuidle_driver;
static struct cpuidle_device *device;

static int imx_enter_idle(struct cpuidle_device *dev,
			       struct cpuidle_state *state)
{
	struct timeval before, after;
	int idle_time;

	local_irq_disable();
	local_fiq_disable();

	do_gettimeofday(&before);

	mach_cpuidle(dev, state);

	do_gettimeofday(&after);

	local_fiq_enable();
	local_irq_enable();

	idle_time = (after.tv_sec - before.tv_sec) * USEC_PER_SEC +
		(after.tv_usec - before.tv_usec);

	return idle_time;
}

static DEFINE_PER_CPU(struct cpuidle_device, imx_cpuidle_device);

int __init imx_cpuidle_init(struct imx_cpuidle_data *cpuidle_data)
{
	int i, cpu_id;

	if (cpuidle_data == NULL) {
		pr_err("%s: cpuidle_data pointer NULL\n", __func__);
		return -EINVAL;
	}

	if (cpuidle_data->mach_cpuidle == NULL) {
		pr_err("%s: idle callback function NULL\n", __func__);
		return -EINVAL;
	}

	imx_cpuidle_driver = cpuidle_data->imx_cpuidle_driver;

	mach_cpuidle = cpuidle_data->mach_cpuidle;

	/* register imx_cpuidle driver */
	if (cpuidle_register_driver(imx_cpuidle_driver)) {
		pr_err("%s: Failed to register cpuidle driver\n", __func__);
		return -ENODEV;
	}

	/* if provided, initialize the mach level cpuidle functionality */
	if (cpuidle_data->mach_cpuidle_init) {
		if (cpuidle_data->mach_cpuidle_init(cpuidle_data)) {
			pr_err("%s: Failed to register cpuidle driver\n",
				 __func__);
			cpuidle_unregister_driver(imx_cpuidle_driver);
			return -ENODEV;
		}
	}

	/* initialize state data for each cpuidle_device(one per present cpu)*/
	for_each_cpu(cpu_id, cpu_present_mask) {

		device = &per_cpu(imx_cpuidle_device, cpu_id);
		device->cpu = cpu_id;

		device->state_count = min((unsigned int) CPUIDLE_STATE_MAX,
			cpuidle_data->num_states);

		device->prepare = cpuidle_data->prepare;

		for (i = 0; i < device->state_count; i++) {
			strlcpy(device->states[i].name,
				cpuidle_data->state_data[i].name,
				CPUIDLE_NAME_LEN);

			strlcpy(device->states[i].desc,
				cpuidle_data->state_data[i].desc,
				CPUIDLE_DESC_LEN);

			device->states[i].driver_data =
				(void *)cpuidle_data->
				state_data[i].mach_cpu_pwr_state;

			/*
			 * Because the imx_enter_idle function measures
			 * and returns a valid time for all imx SoCs,
			 * we always set this flag.
			 */
			device->states[i].flags = CPUIDLE_FLAG_TIME_VALID;

			device->states[i].exit_latency =
				cpuidle_data->state_data[i].exit_latency;

			device->states[i].power_usage =
				cpuidle_data->state_data[i].power_usage;

			device->states[i].target_residency =
				cpuidle_data->state_data[i].target_residency;

			device->states[i].enter = imx_enter_idle;
		}
	}

	return 0;
}

int __init imx_cpuidle_dev_init(void)
{
	int cpu_id;

	/*
	 * Register online cpus.  If maxcpus is specified as a boot
	 * argument and secondary cpus are brought online after boot,
	 * this function can be called again to register a cpuidle
	 * device for those secondary cpus.
	 */
	for_each_cpu(cpu_id, cpu_online_mask) {
		device = &per_cpu(imx_cpuidle_device, cpu_id);
		if (device == NULL) {
			pr_err("%s: Failed to register (No device)\n",
				__func__);
			return -ENODEV;
		}

		if (!device->registered)
			if (cpuidle_register_device(device)) {
				pr_err("%s: Failed to register\n", __func__);
				return -ENODEV;
			}
	}

	return 0;
}
late_initcall(imx_cpuidle_dev_init);