1 /* power.c: Power management driver.
3 * Copyright (C) 1999, 2007 David S. Miller (davem@davemloft.net)
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/sched.h>
10 #include <linux/signal.h>
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
14 #include <linux/syscalls.h>
16 #include <asm/system.h>
17 #include <asm/auxio.h>
19 #include <asm/of_device.h>
21 #include <asm/power.h>
22 #include <asm/sstate.h>
24 #include <linux/unistd.h>
27 * sysctl - toggle power-off restriction for serial console
28 * systems in machine_power_off()
32 static void __iomem *power_reg;
34 static DECLARE_WAIT_QUEUE_HEAD(powerd_wait);
35 static int button_pressed;
37 void wake_up_powerd(void)
39 if (button_pressed == 0) {
41 wake_up(&powerd_wait);
45 static irqreturn_t power_handler(int irq, void *dev_id)
49 /* FIXME: Check registers for status... */
53 extern void machine_halt(void);
54 extern void machine_alt_power_off(void);
55 static void (*poweroff_method)(void) = machine_alt_power_off;
57 void machine_power_off(void)
60 if (!serial_console || scons_pwroff) {
62 /* Both register bits seem to have the
63 * same effect, so until I figure out
64 * what the difference is...
66 writel(AUXIO_PCIO_CPWR_OFF | AUXIO_PCIO_SPWR_OFF, power_reg);
68 if (poweroff_method != NULL) {
77 void (*pm_power_off)(void) = machine_power_off;
78 EXPORT_SYMBOL(pm_power_off);
80 static int powerd(void *__unused)
82 static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
83 char *argv[] = { "/sbin/shutdown", "-h", "now", NULL };
84 DECLARE_WAITQUEUE(wait, current);
88 add_wait_queue(&powerd_wait, &wait);
91 set_task_state(current, TASK_INTERRUPTIBLE);
94 flush_signals(current);
97 __set_current_state(TASK_RUNNING);
98 remove_wait_queue(&powerd_wait, &wait);
100 /* Ok, down we go... */
102 if (kernel_execve("/sbin/shutdown", argv, envp) < 0) {
103 printk(KERN_ERR "powerd: shutdown execution failed\n");
109 int start_powerd(void)
113 err = kernel_thread(powerd, NULL, CLONE_FS);
115 printk(KERN_ERR "power: Failed to start power daemon.\n");
117 printk(KERN_INFO "power: powerd running.\n");
122 static int __init has_button_interrupt(unsigned int irq, struct device_node *dp)
124 if (irq == 0xffffffff)
126 if (!of_find_property(dp, "button", NULL))
132 static int __devinit power_probe(struct of_device *op, const struct of_device_id *match)
134 struct resource *res = &op->resource[0];
135 unsigned int irq= op->irqs[0];
137 power_reg = of_ioremap(res, 0, 0x4, "power");
139 printk("%s: Control reg at %lx ... ",
140 op->node->name, res->start);
142 poweroff_method = machine_halt; /* able to use the standard halt */
144 if (has_button_interrupt(irq, op->node)) {
145 if (start_powerd() < 0)
149 power_handler, 0, "power", NULL) < 0)
150 printk(KERN_ERR "power: Cannot setup IRQ handler.\n");
152 printk(KERN_INFO "power: Not using powerd.\n");
158 static struct of_device_id power_match[] = {
165 static struct of_platform_driver power_driver = {
167 .match_table = power_match,
168 .probe = power_probe,
171 void __init power_init(void)
173 of_register_driver(&power_driver, &of_bus_type);