summaryrefslogtreecommitdiff
path: root/arch/arm/mach-mx3/dpm.c
blob: 6cacc7ea0f230771bb224c151079e83ef0921e2c (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* REVISIT Doxygen fixups */
/*
 * DPM support for Freescale i.MX31
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Copyright (C) 2002, 2004 MontaVista Software <source@mvista.com>.
 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
 *
 * Based on code by Matthew Locke, Dmitry Chigirev, and Bishop Brock.
 */

/*!
 * @file mach-mx3/dpm.c
 *
 * @brief This file provides DPM support hooks for the Freescale i.MX31
 *
 * @ingroup DPM_MX31
 */

#include <linux/dpm.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kmod.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/device.h>
#include <linux/pm.h>
#include <linux/delay.h>
#include <linux/clk.h>

#include <asm/hardirq.h>
#include <asm/page.h>
#include <asm/processor.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/arch/hardware.h>
#include <asm/arch/mxc_pm.h>

static unsigned saved_cpu_freq;
static unsigned long saved_loops_per_jiffy;
static unsigned int curr_mode = DPM_MODE_RUN;

static struct clk *cpu_clk;
static struct clk *ahb_clk;
static struct clk *ipg_clk;

extern void (*pm_idle) (void);

static int mxc_dpm_set_opt(struct dpm_opt *cur, struct dpm_opt *new)
{
	struct dpm_md_opt *md_cur, *md_new;
	unsigned long flags;

	md_cur = &cur->md_opt;
	md_new = &new->md_opt;

	if (md_new->cpu == -1)
		md_new->cpu = md_cur->cpu;
	if (md_new->ahb == -1)
		md_new->ahb = md_cur->ahb;
	if (md_new->ip == -1)
		md_new->ip = md_cur->ip;
	if (md_new->mode == -1)
		md_new->mode = md_cur->mode;

	if (md_new->cpu == 0 || md_new->mode == DPM_MODE_SLEEP) {

#ifdef CONFIG_PM
		pm_suspend(PM_SUSPEND_MEM);

		/* Here when we wake up.  Recursive call to switch back to
		 * to task state.
		 */
		dpm_set_os(DPM_TASK_STATE);
#endif
		return 0;
	}

	if (md_new->mode == DPM_MODE_WAIT) {
#ifdef CONFIG_PM
		pm_suspend(PM_SUSPEND_STANDBY);

		/* Here when we wake up.  Recursive call to switch back to
		 * to task state.
		 */
		dpm_set_os(DPM_TASK_STATE);
#endif
		return 0;
	}

	if (md_new->mode == DPM_MODE_STOP) {
#ifdef CONFIG_PM
		pm_suspend(PM_SUSPEND_STOP);

		/* Here when we wake up.  Recursive call to switch back to
		 * to task state.
		 */
		dpm_set_os(DPM_TASK_STATE);
#endif
		return 0;
	}

	local_irq_save(flags);

	if (md_new->cpu) {
		loops_per_jiffy =
		    dpm_compute_lpj(saved_loops_per_jiffy, saved_cpu_freq,
				    md_new->cpu / 1000);
	}
#ifdef CONFIG_PM
	mxc_pm_dvfs(md_new->cpu, md_new->ahb, md_new->ip);
	curr_mode = md_new->mode;
#endif

	local_irq_restore(flags);
	return 0;
}

static int mxc_dpm_init_opt(struct dpm_opt *opt)
{
	int core = opt->pp[DPM_CORE_FREQ];
	int ahb = opt->pp[DPM_AHB_FREQ];
	int ip = opt->pp[DPM_IP_FREQ];
	int mode = opt->pp[DPM_MODE];
	struct dpm_md_opt *md_opt = &opt->md_opt;

	if (mode != DPM_MODE_SLEEP &&
	    mode != DPM_MODE_RUN &&
	    mode != DPM_MODE_WAIT && mode != DPM_MODE_STOP && mode != -1) {
		printk(KERN_ERR "MXC DPM Error: "
		       "MCU mode is out of range. "
		       "Possible settings: 0 - sleep,  1 - run, 2 - wait, 3 - stop\n");
		return -EINVAL;
	}

	if (mode == DPM_MODE_SLEEP || core == 0) {
		md_opt->cpu = 0;
		md_opt->ahb = 0;
		md_opt->ip = 0;
		md_opt->mode = mode;
		return 0;
	}

	if (ahb != -1 && (ahb > AHB_MAX || ahb < AHB_MIN)) {
		printk(KERN_ERR "MXC DPM Error: "
		       "AHB frequency setting %u is out of range for opt "
		       "named %s. Possible settings: %d .. %d.\n",
		       ahb, opt->name, AHB_MIN, AHB_MAX);
		return -EINVAL;
	}

	if (ahb != -1 && ahb % AHB_MIN != 0) {
		printk(KERN_ERR "MXC DPM Error: "
		       "AHB frequency setting %u is out of range for opt "
		       "named %s. AHB frequency should be multiple of %d.\n",
		       ahb, opt->name, AHB_MIN);
		return -EINVAL;
	}

	if (ip != -1 && (ip > IPG_MAX || ip < IPG_MIN)) {
		printk(KERN_ERR "MXC DPM Error: "
		       "IPG frequency setting %u is out of range for opt "
		       "named %s. Possible settings: %d .. %d.\n",
		       ip, opt->name, IPG_MIN, IPG_MAX);
		return -EINVAL;
	}

	if (ip != -1 && ip % IPG_MIN != 0) {
		printk(KERN_ERR "MXC DPM Error: "
		       "IP frequency setting %u is out of range for opt "
		       "named %s. IP frequency should be multiple of %d.\n",
		       ip, opt->name, IPG_MIN);
		return -EINVAL;
	}

	if (core != -1 && (core > ARM_MAX || core < ARM_MIN) && core != 0) {
		printk(KERN_ERR "MXC DPM Error: "
		       "ARM frequency setting %u is out of range for opt "
		       "named %s. Possible settings: %d .. %d.\n",
		       core, opt->name, ARM_MIN, ARM_MAX);
		return -EINVAL;
	}

	if (core != -1 && core % ARM_MIN != 0) {
		printk(KERN_ERR "MXC DPM Error: "
		       "ARM frequency setting %u is out of range for opt "
		       "named %s. ARM frequency should be multiple of %d.\n",
		       core, opt->name, ARM_MIN);
		return -EINVAL;
	}

	md_opt->cpu = core;
	md_opt->ahb = ahb;
	md_opt->ip = ip;
	md_opt->mode = mode;
	return 0;
}

/*!
 * Fully determine the current machine-dependent operating point, and fill in a
 * structure presented by the caller. This should only be called when the
 * dpm_sem is held. This call can return an error if the system is currently at
 * an operating point that could not be constructed by dpm_md_init_opt().
 */
static int mxc_dpm_get_opt(struct dpm_opt *opt)
{
	struct dpm_md_opt *md_opt;

	md_opt = &opt->md_opt;
	md_opt->cpu = clk_get_rate(cpu_clk);
	md_opt->ahb = clk_get_rate(ahb_clk);
	md_opt->ip = clk_get_rate(ipg_clk);
	md_opt->mode = curr_mode;

	return 0;
}

/****************************************************************************
 * Machine-dependent /proc/driver/dpm/md entries
 ****************************************************************************/

static inline int p5d(char *buf, unsigned mhz)
{
	return sprintf(buf, "%5d", mhz);	/* Round */
}

int dpm_proc_print_opt(char *buf, struct dpm_opt *opt)
{
	int len = 0;
	struct dpm_md_opt *md_opt;

	md_opt = &opt->md_opt;

	len += sprintf(buf + len, "%12s %9lu", opt->name, opt->stats.count);
	len += sprintf(buf + len, "\t%d\t%d\t%d\n",
		       md_opt->cpu, md_opt->ahb, md_opt->ip);
	/* Add MODE string later */
	return len;
}

int
read_proc_dpm_md_opts(char *page, char **start, off_t offset,
		      int count, int *eof, void *data)
{
	int len = 0;
	int limit = offset + count;
	struct dpm_opt *opt;
	struct list_head *opt_list;

	/* FIXME: For now we assume that the complete table,
	 * formatted, fits within one page */
	if (offset >= PAGE_SIZE)
		return 0;

	if (dpm_lock_interruptible())
		return -ERESTARTSYS;

	if (!dpm_initialized)
		len += sprintf(page + len, "DPM is not initialized\n");
	else if (!dpm_enabled)
		len += sprintf(page + len, "DPM is disabled\n");
	else {
		len += sprintf(page + len,
			       "The active DPM policy is \"%s\"\n",
			       dpm_active_policy->name);
		len += sprintf(page + len,
			       "The current operating point is \"%s\"\n",
			       dpm_active_opt->name);
	}

	if (dpm_initialized) {
		len += sprintf(page + len,
			       "Table of all defined operating points, "
			       "frequencies in MHz:\n");

		len += sprintf(page + len,
			       " Name           Count  DPLL  CPU  TC  PER  DSP  DSPMMU   LCD\n");

		list_for_each(opt_list, &dpm_opts) {
			opt = list_entry(opt_list, struct dpm_opt, list);
			if (len >= PAGE_SIZE)
				BUG();
			if (len >= limit)
				break;
			len += dpm_proc_print_opt(page + len, opt);
		}
	}
	dpm_unlock();
	*eof = 1;
	if (offset >= len)
		return 0;
	*start = page + offset;
	return min(count, len - (int)offset);
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 *
 * /proc/driver/dpm/md/cmd (Write-only)
 *
 *  This is a catch-all, simple command processor for the MX31 DPM
 *  implementation. These commands are for experimentation and development
 *  _only_, and may leave the system in an unstable state.
 *
 *  No commands defined now.
 *
 ****************************************************************************/

int
write_proc_dpm_md_cmd(struct file *file, const char *buffer,
		      unsigned long count, void *data)
{
	char *buf, *tok, *s;
	static const char *whitespace = " \t\r\n";
	int ret = 0;

	if (current->uid != 0)
		return -EACCES;
	if (count == 0)
		return 0;
	if (!(buf = kmalloc(count + 1, GFP_KERNEL)))
		return -ENOMEM;
	if (copy_from_user(buf, buffer, count)) {
		kfree(buf);
		return -EFAULT;
	}
	buf[count] = '\0';
	s = buf + strspn(buf, whitespace);
	tok = strsep(&s, whitespace);

	if (strcmp(tok, "define-me") == 0) {
		;
	} else {
		ret = -EINVAL;
	}
	kfree(buf);
	if (ret == 0)
		return count;
	else
		return ret;
}

/****************************************************************************
 *  DPM Idle Handler
 ****************************************************************************/

static void (*orig_idle) (void);

static void mxc_dpm_idle(void)
{
	extern void default_idle(void);

	if (orig_idle)
		orig_idle();
	else
		default_idle();
}

/****************************************************************************
 * Initialization/Exit
 ****************************************************************************/

static void mxc_dpm_startup(void)
{
	if (!saved_loops_per_jiffy) {
		saved_loops_per_jiffy = loops_per_jiffy;
		saved_cpu_freq = clk_get_rate(cpu_clk) / 1000;
	}
	orig_idle = pm_idle;
	pm_idle = dpm_idle;
}

static void mxc_dpm_cleanup(void)
{
	pm_idle = orig_idle;
}

static int __init mxc_dpm_init(void)
{
	printk(KERN_INFO "Freescale i.MX31 Dynamic Power Management.\n");

	cpu_clk = clk_get(NULL, "cpu_clk");
	ahb_clk = clk_get(NULL, "ahb_clk");
	ipg_clk = clk_get(NULL, "ipg_clk");

	dpm_md.init_opt = mxc_dpm_init_opt;
	dpm_md.set_opt = mxc_dpm_set_opt;
	dpm_md.get_opt = mxc_dpm_get_opt;
	dpm_md.check_constraint = dpm_default_check_constraint;
	dpm_md.idle = mxc_dpm_idle;
	dpm_md.startup = mxc_dpm_startup;
	dpm_md.cleanup = mxc_dpm_cleanup;

	return 0;
}

__initcall(mxc_dpm_init);