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>
12 #include <asm/mach-au1x00/gpio-au1000.h>
13 #include <asm/mach-db1x00/bcsr.h>
16 * Generic suspend userspace interface for Alchemy development boards.
17 * This code exports a few sysfs nodes under /sys/power/db1x/ which
18 * can be used by userspace to en/disable all au1x-provided wakeup
19 * sources and configure the timeout after which the the TOYMATCH2 irq
20 * is to trigger a wakeup.
24 static unsigned long db1x_pm_sleep_secs
;
25 static unsigned long db1x_pm_wakemsk
;
26 static unsigned long db1x_pm_last_wakesrc
;
28 static int db1x_pm_enter(suspend_state_t state
)
30 unsigned short bcsrs
[16];
34 hasint
= bcsr_read(BCSR_WHOAMI
);
35 hasint
= BCSR_WHOAMI_BOARD(hasint
) >= BCSR_WHOAMI_DB1200
;
36 j
= (hasint
) ? BCSR_MASKSET
: BCSR_SYSTEM
;
38 for (i
= BCSR_STATUS
; i
<= j
; i
++)
39 bcsrs
[i
] = bcsr_read(i
);
41 /* shut off hexleds */
42 bcsr_write(BCSR_HEXCLEAR
, 3);
44 /* enable GPIO based wakeup */
45 alchemy_gpio1_input_enable();
47 /* clear and setup wake cause and source */
48 alchemy_wrsys(0, AU1000_SYS_WAKEMSK
);
49 alchemy_wrsys(0, AU1000_SYS_WAKESRC
);
51 alchemy_wrsys(db1x_pm_wakemsk
, AU1000_SYS_WAKEMSK
);
53 /* setup 1Hz-timer-based wakeup: wait for reg access */
54 while (alchemy_rdsys(AU1000_SYS_CNTRCTRL
) & SYS_CNTRL_M20
)
57 alchemy_wrsys(alchemy_rdsys(AU1000_SYS_TOYREAD
) + db1x_pm_sleep_secs
,
58 AU1000_SYS_TOYMATCH2
);
60 /* wait for value to really hit the register */
61 while (alchemy_rdsys(AU1000_SYS_CNTRCTRL
) & SYS_CNTRL_M20
)
64 /* ...and now the sandman can come! */
68 /* restore CPLD regs */
69 for (i
= BCSR_STATUS
; i
<= BCSR_SYSTEM
; i
++)
70 bcsr_write(i
, bcsrs
[i
]);
72 /* restore CPLD int registers */
74 bcsr_write(BCSR_INTCLR
, 0xffff);
75 bcsr_write(BCSR_MASKCLR
, 0xffff);
76 bcsr_write(BCSR_INTSTAT
, 0xffff);
77 bcsr_write(BCSR_INTSET
, bcsrs
[BCSR_INTSET
]);
78 bcsr_write(BCSR_MASKSET
, bcsrs
[BCSR_MASKSET
]);
81 /* light up hexleds */
82 bcsr_write(BCSR_HEXCLEAR
, 0);
87 static int db1x_pm_begin(suspend_state_t state
)
89 if (!db1x_pm_wakemsk
) {
90 printk(KERN_ERR
"db1x: no wakeup source activated!\n");
97 static void db1x_pm_end(void)
99 /* read and store wakeup source, the clear the register. To
100 * be able to clear it, WAKEMSK must be cleared first.
102 db1x_pm_last_wakesrc
= alchemy_rdsys(AU1000_SYS_WAKESRC
);
104 alchemy_wrsys(0, AU1000_SYS_WAKEMSK
);
105 alchemy_wrsys(0, AU1000_SYS_WAKESRC
);
108 static const struct platform_suspend_ops db1x_pm_ops
= {
109 .valid
= suspend_valid_only_mem
,
110 .begin
= db1x_pm_begin
,
111 .enter
= db1x_pm_enter
,
115 #define ATTRCMP(x) (0 == strcmp(attr->attr.name, #x))
117 static ssize_t
db1x_pmattr_show(struct kobject
*kobj
,
118 struct kobj_attribute
*attr
,
123 if (ATTRCMP(timer_timeout
))
124 return sprintf(buf
, "%lu\n", db1x_pm_sleep_secs
);
126 else if (ATTRCMP(timer
))
127 return sprintf(buf
, "%u\n",
128 !!(db1x_pm_wakemsk
& SYS_WAKEMSK_M2
));
130 else if (ATTRCMP(wakesrc
))
131 return sprintf(buf
, "%lu\n", db1x_pm_last_wakesrc
);
133 else if (ATTRCMP(gpio0
) || ATTRCMP(gpio1
) || ATTRCMP(gpio2
) ||
134 ATTRCMP(gpio3
) || ATTRCMP(gpio4
) || ATTRCMP(gpio5
) ||
135 ATTRCMP(gpio6
) || ATTRCMP(gpio7
)) {
136 idx
= (attr
->attr
.name
)[4] - '0';
137 return sprintf(buf
, "%d\n",
138 !!(db1x_pm_wakemsk
& SYS_WAKEMSK_GPIO(idx
)));
140 } else if (ATTRCMP(wakemsk
)) {
141 return sprintf(buf
, "%08lx\n", db1x_pm_wakemsk
);
147 static ssize_t
db1x_pmattr_store(struct kobject
*kobj
,
148 struct kobj_attribute
*attr
,
155 if (ATTRCMP(timer_timeout
)) {
156 tmp
= kstrtoul(instr
, 0, &l
);
160 db1x_pm_sleep_secs
= l
;
162 } else if (ATTRCMP(timer
)) {
164 db1x_pm_wakemsk
|= SYS_WAKEMSK_M2
;
166 db1x_pm_wakemsk
&= ~SYS_WAKEMSK_M2
;
168 } else if (ATTRCMP(gpio0
) || ATTRCMP(gpio1
) || ATTRCMP(gpio2
) ||
169 ATTRCMP(gpio3
) || ATTRCMP(gpio4
) || ATTRCMP(gpio5
) ||
170 ATTRCMP(gpio6
) || ATTRCMP(gpio7
)) {
171 tmp
= (attr
->attr
.name
)[4] - '0';
172 if (instr
[0] != '0') {
173 db1x_pm_wakemsk
|= SYS_WAKEMSK_GPIO(tmp
);
175 db1x_pm_wakemsk
&= ~SYS_WAKEMSK_GPIO(tmp
);
178 } else if (ATTRCMP(wakemsk
)) {
179 tmp
= kstrtoul(instr
, 0, &l
);
183 db1x_pm_wakemsk
= l
& 0x0000003f;
192 static struct kobj_attribute x##_attribute = \
193 __ATTR(x, 0664, db1x_pmattr_show, \
196 ATTR(gpio0
) /* GPIO-based wakeup enable */
204 ATTR(timer
) /* TOYMATCH2-based wakeup enable */
205 ATTR(timer_timeout
) /* timer-based wakeup timeout value, in seconds */
206 ATTR(wakesrc
) /* contents of SYS_WAKESRC after last wakeup */
207 ATTR(wakemsk
) /* direct access to SYS_WAKEMSK */
209 #define ATTR_LIST(x) & x ## _attribute.attr
210 static struct attribute
*db1x_pmattrs
[] = {
220 ATTR_LIST(timer_timeout
),
223 NULL
, /* terminator */
226 static struct attribute_group db1x_pmattr_group
= {
228 .attrs
= db1x_pmattrs
,
232 * Initialize suspend interface
234 static int __init
pm_init(void)
236 /* init TOY to tick at 1Hz if not already done. No need to wait
237 * for confirmation since there's plenty of time from here to
238 * the next suspend cycle.
240 if (alchemy_rdsys(AU1000_SYS_TOYTRIM
) != 32767)
241 alchemy_wrsys(32767, AU1000_SYS_TOYTRIM
);
243 db1x_pm_last_wakesrc
= alchemy_rdsys(AU1000_SYS_WAKESRC
);
245 alchemy_wrsys(0, AU1000_SYS_WAKESRC
);
246 alchemy_wrsys(0, AU1000_SYS_WAKEMSK
);
248 suspend_set_ops(&db1x_pm_ops
);
250 return sysfs_create_group(power_kobj
, &db1x_pmattr_group
);
253 late_initcall(pm_init
);