blob: 8dd4294ad21ec0bcf0b3345d53f920efe999703c [file] [log] [blame]
David S. Miller13077d82007-07-11 18:18:04 -07001/* power.c: Power management driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David S. Miller13077d82007-07-11 18:18:04 -07003 * Copyright (C) 1999, 2007 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#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>
David S. Miller90bf8112006-01-09 13:59:12 -080013#include <linux/pm.h>
Arnd Bergmann67608562006-10-02 02:18:26 -070014#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/auxio.h>
David S. Millerabbce6e2006-06-29 15:22:46 -070018#include <asm/prom.h>
19#include <asm/of_device.h>
20#include <asm/io.h>
David S. Miller13077d82007-07-11 18:18:04 -070021#include <asm/power.h>
David S. Miller22d6a1c2007-05-25 00:37:12 -070022#include <asm/sstate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/unistd.h>
25
26/*
27 * sysctl - toggle power-off restriction for serial console
28 * systems in machine_power_off()
29 */
30int scons_pwroff = 1;
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static void __iomem *power_reg;
33
34static DECLARE_WAIT_QUEUE_HEAD(powerd_wait);
35static int button_pressed;
36
David S. Miller13077d82007-07-11 18:18:04 -070037void wake_up_powerd(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 if (button_pressed == 0) {
40 button_pressed = 1;
41 wake_up(&powerd_wait);
42 }
David S. Miller13077d82007-07-11 18:18:04 -070043}
44
45static irqreturn_t power_handler(int irq, void *dev_id)
46{
47 wake_up_powerd();
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 /* FIXME: Check registers for status... */
50 return IRQ_HANDLED;
51}
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53extern void machine_halt(void);
54extern void machine_alt_power_off(void);
55static void (*poweroff_method)(void) = machine_alt_power_off;
56
57void machine_power_off(void)
58{
David S. Miller22d6a1c2007-05-25 00:37:12 -070059 sstate_poweroff();
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (!serial_console || scons_pwroff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (power_reg) {
62 /* Both register bits seem to have the
63 * same effect, so until I figure out
64 * what the difference is...
65 */
66 writel(AUXIO_PCIO_CPWR_OFF | AUXIO_PCIO_SPWR_OFF, power_reg);
David S. Miller13077d82007-07-11 18:18:04 -070067 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (poweroff_method != NULL) {
69 poweroff_method();
70 /* not reached */
71 }
David S. Miller13077d82007-07-11 18:18:04 -070072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74 machine_halt();
75}
76
David S. Miller90bf8112006-01-09 13:59:12 -080077void (*pm_power_off)(void) = machine_power_off;
78EXPORT_SYMBOL(pm_power_off);
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int powerd(void *__unused)
81{
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);
85
86 daemonize("powerd");
87
88 add_wait_queue(&powerd_wait, &wait);
David S. Miller13077d82007-07-11 18:18:04 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 for (;;) {
91 set_task_state(current, TASK_INTERRUPTIBLE);
92 if (button_pressed)
93 break;
94 flush_signals(current);
95 schedule();
96 }
97 __set_current_state(TASK_RUNNING);
98 remove_wait_queue(&powerd_wait, &wait);
99
100 /* Ok, down we go... */
101 button_pressed = 0;
Arnd Bergmann67608562006-10-02 02:18:26 -0700102 if (kernel_execve("/sbin/shutdown", argv, envp) < 0) {
David S. Miller13077d82007-07-11 18:18:04 -0700103 printk(KERN_ERR "powerd: shutdown execution failed\n");
104 machine_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106 return 0;
107}
108
David S. Miller13077d82007-07-11 18:18:04 -0700109int start_powerd(void)
110{
111 int err;
112
113 err = kernel_thread(powerd, NULL, CLONE_FS);
114 if (err < 0)
115 printk(KERN_ERR "power: Failed to start power daemon.\n");
116 else
117 printk(KERN_INFO "power: powerd running.\n");
118
119 return err;
120}
121
David S. Miller690c8fd2006-06-22 19:12:03 -0700122static int __init has_button_interrupt(unsigned int irq, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
David S. Miller13077d82007-07-11 18:18:04 -0700124 if (irq == 0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 0;
David S. Miller690c8fd2006-06-22 19:12:03 -0700126 if (!of_find_property(dp, "button", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return 0;
128
129 return 1;
130}
131
David S. Millerabbce6e2006-06-29 15:22:46 -0700132static int __devinit power_probe(struct of_device *op, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
David S. Millerabbce6e2006-06-29 15:22:46 -0700134 struct resource *res = &op->resource[0];
135 unsigned int irq= op->irqs[0];
David S. Millera2bd4fd2006-06-23 01:44:10 -0700136
David S. Millerabbce6e2006-06-29 15:22:46 -0700137 power_reg = of_ioremap(res, 0, 0x4, "power");
138
139 printk("%s: Control reg at %lx ... ",
140 op->node->name, res->start);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 poweroff_method = machine_halt; /* able to use the standard halt */
David S. Millera2bd4fd2006-06-23 01:44:10 -0700143
David S. Millerabbce6e2006-06-29 15:22:46 -0700144 if (has_button_interrupt(irq, op->node)) {
David S. Miller13077d82007-07-11 18:18:04 -0700145 if (start_powerd() < 0)
David S. Millerabbce6e2006-06-29 15:22:46 -0700146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
David S. Miller2256c132005-10-06 20:43:54 -0700148 if (request_irq(irq,
David S. Miller00cde672006-06-29 14:39:11 -0700149 power_handler, 0, "power", NULL) < 0)
David S. Miller13077d82007-07-11 18:18:04 -0700150 printk(KERN_ERR "power: Cannot setup IRQ handler.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 } else {
David S. Miller13077d82007-07-11 18:18:04 -0700152 printk(KERN_INFO "power: Not using powerd.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
David S. Millerabbce6e2006-06-29 15:22:46 -0700154
155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
David S. Millera2bd4fd2006-06-23 01:44:10 -0700157
158static struct of_device_id power_match[] = {
159 {
160 .name = "power",
161 },
162 {},
163};
164
David S. Millerabbce6e2006-06-29 15:22:46 -0700165static struct of_platform_driver power_driver = {
David S. Millera2bd4fd2006-06-23 01:44:10 -0700166 .name = "power",
167 .match_table = power_match,
David S. Millerabbce6e2006-06-29 15:22:46 -0700168 .probe = power_probe,
David S. Millera2bd4fd2006-06-23 01:44:10 -0700169};
170
171void __init power_init(void)
172{
David S. Millerabbce6e2006-06-29 15:22:46 -0700173 of_register_driver(&power_driver, &of_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700174 return;
175}