OMAP3: PM: move context-loss counting into OMAP PM
[linux-ginger.git] / arch / arm / mach-omap2 / pm.c
blobdbe1187cd3ff5bdf9a93ed1d5dd8a9c03f99ac5e
1 /*
2 * linux/arch/arm/mach-omap2/pm.c
4 * OMAP Power Management Common Routines
6 * Copyright (C) 2005 Texas Instruments, Inc.
7 * Copyright (C) 2006-2008 Nokia Corporation
9 * Written by:
10 * Richard Woodruff <r-woodruff2@ti.com>
11 * Tony Lindgren
12 * Juha Yrjola
13 * Amit Kucheria <amit.kucheria@nokia.com>
14 * Igor Stoppa <igor.stoppa@nokia.com>
15 * Jouni Hogander
17 * Based on pm.c for omap1
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 2 as
21 * published by the Free Software Foundation.
24 #include <linux/suspend.h>
25 #include <linux/time.h>
26 #include <linux/platform_device.h>
27 #include <linux/device.h>
29 #include <plat/cpu.h>
30 #include <asm/mach/time.h>
31 #include <asm/atomic.h>
33 #include <plat/powerdomain.h>
34 #include <plat/resource.h>
35 #include <plat/omap34xx.h>
37 #include "prm-regbits-34xx.h"
38 #include "pm.h"
40 atomic_t sleep_block = ATOMIC_INIT(0);
42 static ssize_t idle_show(struct kobject *, struct kobj_attribute *, char *);
43 static ssize_t idle_store(struct kobject *k, struct kobj_attribute *,
44 const char *buf, size_t n);
46 #ifdef CONFIG_OMAP_PM_SRF
47 static ssize_t vdd_opp_show(struct kobject *, struct kobj_attribute *, char *);
48 static ssize_t vdd_opp_store(struct kobject *k, struct kobj_attribute *,
49 const char *buf, size_t n);
50 static struct kobj_attribute vdd1_opp_attr =
51 __ATTR(vdd1_opp, 0644, vdd_opp_show, vdd_opp_store);
53 static struct kobj_attribute vdd2_opp_attr =
54 __ATTR(vdd2_opp, 0644, vdd_opp_show, vdd_opp_store);
55 static struct kobj_attribute vdd1_lock_attr =
56 __ATTR(vdd1_lock, 0644, vdd_opp_show, vdd_opp_store);
57 static struct kobj_attribute vdd2_lock_attr =
58 __ATTR(vdd2_lock, 0644, vdd_opp_show, vdd_opp_store);
60 #endif
62 static ssize_t idle_show(struct kobject *kobj, struct kobj_attribute *attr,
63 char *buf)
65 return -EINVAL;
68 static ssize_t idle_store(struct kobject *kobj, struct kobj_attribute *attr,
69 const char *buf, size_t n)
71 unsigned short value;
73 if (sscanf(buf, "%hu", &value) != 1 ||
74 (value != 0 && value != 1)) {
75 printk(KERN_ERR "idle_store: Invalid value\n");
76 return -EINVAL;
79 return n;
82 #ifdef CONFIG_OMAP_PM_SRF
83 #include <plat/omap34xx.h>
84 static int vdd1_locked;
85 static int vdd2_locked;
87 static ssize_t vdd_opp_show(struct kobject *kobj, struct kobj_attribute *attr,
88 char *buf)
90 if (attr == &vdd1_opp_attr)
91 return sprintf(buf, "%hu\n", resource_get_level("vdd1_opp"));
92 else if (attr == &vdd2_opp_attr)
93 return sprintf(buf, "%hu\n", resource_get_level("vdd2_opp"));
94 else if (attr == &vdd1_lock_attr)
95 return sprintf(buf, "%hu\n", resource_get_opp_lock(VDD1_OPP));
96 else if (attr == &vdd2_lock_attr)
97 return sprintf(buf, "%hu\n", resource_get_opp_lock(VDD2_OPP));
98 else
99 return -EINVAL;
102 static ssize_t vdd_opp_store(struct kobject *kobj, struct kobj_attribute *attr,
103 const char *buf, size_t n)
105 unsigned short value;
106 int flags = 0;
108 if (sscanf(buf, "%hu", &value) != 1)
109 return -EINVAL;
111 /* Check locks */
112 if (attr == &vdd1_lock_attr) {
113 flags = OPP_IGNORE_LOCK;
114 attr = &vdd1_opp_attr;
115 if (vdd1_locked && value == 0) {
116 resource_unlock_opp(VDD1_OPP);
117 resource_refresh();
118 vdd1_locked = 0;
119 return n;
121 if (vdd1_locked == 0 && value != 0) {
122 resource_lock_opp(VDD1_OPP);
123 vdd1_locked = 1;
125 } else if (attr == &vdd2_lock_attr) {
126 flags = OPP_IGNORE_LOCK;
127 attr = &vdd2_opp_attr;
128 if (vdd2_locked && value == 0) {
129 resource_unlock_opp(VDD2_OPP);
130 resource_refresh();
131 vdd2_locked = 0;
132 return n;
134 if (vdd2_locked == 0 && value != 0) {
135 resource_lock_opp(VDD2_OPP);
136 vdd2_locked = 1;
140 if (attr == &vdd1_opp_attr) {
141 if (value < 1 || value > 5) {
142 printk(KERN_ERR "vdd_opp_store: Invalid value\n");
143 return -EINVAL;
145 resource_set_opp_level(VDD1_OPP, value, flags);
146 } else if (attr == &vdd2_opp_attr) {
147 if (value < 1 || value > 3) {
148 printk(KERN_ERR "vdd_opp_store: Invalid value\n");
149 return -EINVAL;
151 resource_set_opp_level(VDD2_OPP, value, flags);
152 } else {
153 return -EINVAL;
155 return n;
157 #endif
159 void omap2_block_sleep(void)
161 atomic_inc(&sleep_block);
164 void omap2_allow_sleep(void)
166 int i;
168 i = atomic_dec_return(&sleep_block);
169 BUG_ON(i < 0);
172 static int __init omap_pm_init(void)
174 int error = -1;
176 #ifdef CONFIG_OMAP_PM_SRF
177 error = sysfs_create_file(power_kobj,
178 &vdd1_opp_attr.attr);
179 if (error) {
180 printk(KERN_ERR "sysfs_create_file failed: %d\n", error);
181 return error;
183 error = sysfs_create_file(power_kobj,
184 &vdd2_opp_attr.attr);
185 if (error) {
186 printk(KERN_ERR "sysfs_create_file failed: %d\n", error);
187 return error;
190 error = sysfs_create_file(power_kobj, &vdd1_lock_attr.attr);
191 if (error) {
192 printk(KERN_ERR "sysfs_create_file failed: %d\n", error);
193 return error;
196 error = sysfs_create_file(power_kobj, &vdd2_lock_attr.attr);
197 if (error) {
198 printk(KERN_ERR "sysfs_create_file failed: %d\n", error);
199 return error;
201 #endif
203 return error;
205 late_initcall(omap_pm_init);