2 * Alchemy Development Board example suspend userspace interface.
4 * (c) 2008 Manuel Lauss <mano@roarinelk.homelinux.net>
7 #include <linux/init.h>
8 #include <linux/kobject.h>
9 #include <linux/suspend.h>
10 #include <linux/sysfs.h>
11 #include <asm/mach-au1x00/au1000.h>
14 * Generic suspend userspace interface for Alchemy development boards.
15 * This code exports a few sysfs nodes under /sys/power/db1x/ which
16 * can be used by userspace to en/disable all au1x-provided wakeup
17 * sources and configure the timeout after which the the TOYMATCH2 irq
18 * is to trigger a wakeup.
22 static unsigned long db1x_pm_sleep_secs;
23 static unsigned long db1x_pm_wakemsk;
24 static unsigned long db1x_pm_last_wakesrc;
26 static int db1x_pm_enter(suspend_state_t state)
28 /* enable GPIO based wakeup */
29 au_writel(1, SYS_PININPUTEN);
31 /* clear and setup wake cause and source */
32 au_writel(0, SYS_WAKEMSK);
34 au_writel(0, SYS_WAKESRC);
37 au_writel(db1x_pm_wakemsk, SYS_WAKEMSK);
40 /* setup 1Hz-timer-based wakeup: wait for reg access */
41 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20)
44 au_writel(au_readl(SYS_TOYREAD) + db1x_pm_sleep_secs, SYS_TOYMATCH2);
47 /* wait for value to really hit the register */
48 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20)
51 /* ...and now the sandman can come! */
57 static int db1x_pm_begin(suspend_state_t state)
59 if (!db1x_pm_wakemsk) {
60 printk(KERN_ERR "db1x: no wakeup source activated!\n");
67 static void db1x_pm_end(void)
69 /* read and store wakeup source, the clear the register. To
70 * be able to clear it, WAKEMSK must be cleared first.
72 db1x_pm_last_wakesrc = au_readl(SYS_WAKESRC);
74 au_writel(0, SYS_WAKEMSK);
75 au_writel(0, SYS_WAKESRC);
80 static struct platform_suspend_ops db1x_pm_ops = {
81 .valid = suspend_valid_only_mem,
82 .begin = db1x_pm_begin,
83 .enter = db1x_pm_enter,
87 #define ATTRCMP(x) (0 == strcmp(attr->attr.name, #x))
89 static ssize_t db1x_pmattr_show(struct kobject *kobj,
90 struct kobj_attribute *attr,
95 if (ATTRCMP(timer_timeout))
96 return sprintf(buf, "%lu\n", db1x_pm_sleep_secs);
98 else if (ATTRCMP(timer))
99 return sprintf(buf, "%u\n",
100 !!(db1x_pm_wakemsk & SYS_WAKEMSK_M2));
102 else if (ATTRCMP(wakesrc))
103 return sprintf(buf, "%lu\n", db1x_pm_last_wakesrc);
105 else if (ATTRCMP(gpio0) || ATTRCMP(gpio1) || ATTRCMP(gpio2) ||
106 ATTRCMP(gpio3) || ATTRCMP(gpio4) || ATTRCMP(gpio5) ||
107 ATTRCMP(gpio6) || ATTRCMP(gpio7)) {
108 idx = (attr->attr.name)[4] - '0';
109 return sprintf(buf, "%d\n",
110 !!(db1x_pm_wakemsk & SYS_WAKEMSK_GPIO(idx)));
112 } else if (ATTRCMP(wakemsk)) {
113 return sprintf(buf, "%08lx\n", db1x_pm_wakemsk);
119 static ssize_t db1x_pmattr_store(struct kobject *kobj,
120 struct kobj_attribute *attr,
127 if (ATTRCMP(timer_timeout)) {
128 tmp = strict_strtoul(instr, 0, &l);
132 db1x_pm_sleep_secs = l;
134 } else if (ATTRCMP(timer)) {
136 db1x_pm_wakemsk |= SYS_WAKEMSK_M2;
138 db1x_pm_wakemsk &= ~SYS_WAKEMSK_M2;
140 } else if (ATTRCMP(gpio0) || ATTRCMP(gpio1) || ATTRCMP(gpio2) ||
141 ATTRCMP(gpio3) || ATTRCMP(gpio4) || ATTRCMP(gpio5) ||
142 ATTRCMP(gpio6) || ATTRCMP(gpio7)) {
143 tmp = (attr->attr.name)[4] - '0';
144 if (instr[0] != '0') {
145 db1x_pm_wakemsk |= SYS_WAKEMSK_GPIO(tmp);
147 db1x_pm_wakemsk &= ~SYS_WAKEMSK_GPIO(tmp);
150 } else if (ATTRCMP(wakemsk)) {
151 tmp = strict_strtoul(instr, 0, &l);
155 db1x_pm_wakemsk = l & 0x0000003f;
164 static struct kobj_attribute x##_attribute = \
165 __ATTR(x, 0664, db1x_pmattr_show, \
168 ATTR(gpio0) /* GPIO-based wakeup enable */
176 ATTR(timer) /* TOYMATCH2-based wakeup enable */
177 ATTR(timer_timeout) /* timer-based wakeup timeout value, in seconds */
178 ATTR(wakesrc) /* contents of SYS_WAKESRC after last wakeup */
179 ATTR(wakemsk) /* direct access to SYS_WAKEMSK */
181 #define ATTR_LIST(x) & x ## _attribute.attr
182 static struct attribute *db1x_pmattrs[] = {
192 ATTR_LIST(timer_timeout),
195 NULL, /* terminator */
198 static struct attribute_group db1x_pmattr_group = {
200 .attrs = db1x_pmattrs,
204 * Initialize suspend interface
206 static int __init pm_init(void)
208 /* init TOY to tick at 1Hz if not already done. No need to wait
209 * for confirmation since there's plenty of time from here to
210 * the next suspend cycle.
212 if (au_readl(SYS_TOYTRIM) != 32767) {
213 au_writel(32767, SYS_TOYTRIM);
217 db1x_pm_last_wakesrc = au_readl(SYS_WAKESRC);
219 au_writel(0, SYS_WAKESRC);
221 au_writel(0, SYS_WAKEMSK);
224 suspend_set_ops(&db1x_pm_ops);
226 return sysfs_create_group(power_kobj, &db1x_pmattr_group);
229 late_initcall(pm_init);