OMAP3: PM: Added DVFS OPP locking interface for VDD1 and VDD2
[linux-ginger.git] / arch / arm / mach-omap2 / pm.c
blob64d077e7a8696ed764a92d3959de16a4d65ff89d
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/omapdev.h>
35 #include <plat/resource.h>
37 #include "prm-regbits-34xx.h"
38 #include "pm.h"
40 atomic_t sleep_block = ATOMIC_INIT(0);
41 static int vdd1_locked;
42 static int vdd2_locked;
44 static ssize_t idle_show(struct kobject *, struct kobj_attribute *, char *);
45 static ssize_t idle_store(struct kobject *k, struct kobj_attribute *,
46 const char *buf, size_t n);
48 #ifdef CONFIG_OMAP_PM_SRF
49 static ssize_t vdd_opp_show(struct kobject *, struct kobj_attribute *, char *);
50 static ssize_t vdd_opp_store(struct kobject *k, struct kobj_attribute *,
51 const char *buf, size_t n);
52 static struct kobj_attribute vdd1_opp_attr =
53 __ATTR(vdd1_opp, 0644, vdd_opp_show, vdd_opp_store);
55 static struct kobj_attribute vdd2_opp_attr =
56 __ATTR(vdd2_opp, 0644, vdd_opp_show, vdd_opp_store);
57 static struct kobj_attribute vdd1_lock_attr =
58 __ATTR(vdd1_lock, 0644, vdd_opp_show, vdd_opp_store);
59 static struct kobj_attribute vdd2_lock_attr =
60 __ATTR(vdd2_lock, 0644, vdd_opp_show, vdd_opp_store);
62 #endif
64 static ssize_t idle_show(struct kobject *kobj, struct kobj_attribute *attr,
65 char *buf)
67 return -EINVAL;
70 static ssize_t idle_store(struct kobject *kobj, struct kobj_attribute *attr,
71 const char *buf, size_t n)
73 unsigned short value;
75 if (sscanf(buf, "%hu", &value) != 1 ||
76 (value != 0 && value != 1)) {
77 printk(KERN_ERR "idle_store: Invalid value\n");
78 return -EINVAL;
81 return n;
84 #ifdef CONFIG_OMAP_PM_SRF
85 static ssize_t vdd_opp_show(struct kobject *kobj, struct kobj_attribute *attr,
86 char *buf)
88 if (attr == &vdd1_opp_attr)
89 return sprintf(buf, "%hu\n", resource_get_level("vdd1_opp"));
90 else if (attr == &vdd2_opp_attr)
91 return sprintf(buf, "%hu\n", resource_get_level("vdd2_opp"));
92 else if (attr == &vdd1_lock_attr)
93 return sprintf(buf, "%hu\n", resource_get_opp_lock(VDD1_OPP));
94 else if (attr == &vdd2_lock_attr)
95 return sprintf(buf, "%hu\n", resource_get_opp_lock(VDD2_OPP));
96 else
97 return -EINVAL;
100 static ssize_t vdd_opp_store(struct kobject *kobj, struct kobj_attribute *attr,
101 const char *buf, size_t n)
103 unsigned short value;
104 int flags = 0;
106 if (sscanf(buf, "%hu", &value) != 1)
107 return -EINVAL;
109 /* Check locks */
110 if (attr == &vdd1_lock_attr) {
111 flags = OPP_IGNORE_LOCK;
112 attr = &vdd1_opp_attr;
113 if (vdd1_locked && value == 0) {
114 resource_unlock_opp(VDD1_OPP);
115 vdd1_locked = 0;
116 return n;
118 if (vdd1_locked == 0 && value != 0) {
119 resource_lock_opp(VDD1_OPP);
120 vdd1_locked = 1;
122 } else if (attr == &vdd2_lock_attr) {
123 flags = OPP_IGNORE_LOCK;
124 attr = &vdd2_opp_attr;
125 if (vdd2_locked && value == 0) {
126 resource_unlock_opp(VDD2_OPP);
127 vdd2_locked = 0;
128 return n;
130 if (vdd2_locked == 0 && value != 0) {
131 resource_lock_opp(VDD2_OPP);
132 vdd2_locked = 1;
136 if (attr == &vdd1_opp_attr) {
137 if (value < 1 || value > 5) {
138 printk(KERN_ERR "vdd_opp_store: Invalid value\n");
139 return -EINVAL;
141 resource_set_opp_level(VDD1_OPP, value, flags);
142 } else if (attr == &vdd2_opp_attr) {
143 if (value < 2 || value > 3) {
144 printk(KERN_ERR "vdd_opp_store: Invalid value\n");
145 return -EINVAL;
147 resource_set_opp_level(VDD2_OPP, value, flags);
148 } else {
149 return -EINVAL;
151 return n;
153 #endif
155 void omap2_block_sleep(void)
157 atomic_inc(&sleep_block);
160 void omap2_allow_sleep(void)
162 int i;
164 i = atomic_dec_return(&sleep_block);
165 BUG_ON(i < 0);
168 unsigned get_last_off_on_transaction_id(struct device *dev)
170 struct platform_device *pdev = to_platform_device(dev);
171 struct omapdev *odev = omapdev_find_pdev(pdev);
172 struct powerdomain *pwrdm;
174 if (odev) {
175 pwrdm = omapdev_get_pwrdm(odev);
176 if (pwrdm)
177 return pwrdm->state_counter[0];
180 return 0;
183 static int __init omap_pm_init(void)
185 int error = -1;
187 #ifdef CONFIG_OMAP_PM_SRF
188 error = sysfs_create_file(power_kobj,
189 &vdd1_opp_attr.attr);
190 if (error) {
191 printk(KERN_ERR "sysfs_create_file failed: %d\n", error);
192 return error;
194 error = sysfs_create_file(power_kobj,
195 &vdd2_opp_attr.attr);
196 if (error) {
197 printk(KERN_ERR "sysfs_create_file failed: %d\n", error);
198 return error;
201 error = sysfs_create_file(power_kobj, &vdd1_lock_attr.attr);
202 if (error) {
203 printk(KERN_ERR "sysfs_create_file failed: %d\n", error);
204 return error;
207 error = sysfs_create_file(power_kobj, &vdd2_lock_attr.attr);
208 if (error) {
209 printk(KERN_ERR "sysfs_create_file failed: %d\n", error);
210 return error;
212 #endif
214 return error;
216 late_initcall(omap_pm_init);