[SPARC/64] Rename some functions like PowerPC
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / sparc64 / kernel / power.c
CommitLineData
13077d80 1/* power.c: Power management driver.
1da177e4 2 *
13077d80 3 * Copyright (C) 1999, 2007 David S. Miller (davem@davemloft.net)
1da177e4
LT
4 */
5
1da177e4
LT
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>
90bf8116 13#include <linux/pm.h>
67608567 14#include <linux/syscalls.h>
1da177e4
LT
15
16#include <asm/system.h>
1da177e4 17#include <asm/auxio.h>
abbce6e2
DM
18#include <asm/prom.h>
19#include <asm/of_device.h>
20#include <asm/io.h>
13077d80 21#include <asm/power.h>
22d6a1cb 22#include <asm/sstate.h>
1da177e4 23
1da177e4
LT
24#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
1da177e4
LT
32static void __iomem *power_reg;
33
34static DECLARE_WAIT_QUEUE_HEAD(powerd_wait);
35static int button_pressed;
36
13077d80 37void wake_up_powerd(void)
1da177e4
LT
38{
39 if (button_pressed == 0) {
40 button_pressed = 1;
41 wake_up(&powerd_wait);
42 }
13077d80
DM
43}
44
45static irqreturn_t power_handler(int irq, void *dev_id)
46{
47 wake_up_powerd();
1da177e4
LT
48
49 /* FIXME: Check registers for status... */
50 return IRQ_HANDLED;
51}
1da177e4
LT
52
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{
22d6a1cb 59 sstate_poweroff();
1da177e4 60 if (!serial_console || scons_pwroff) {
1da177e4
LT
61 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);
13077d80 67 } else {
1da177e4
LT
68 if (poweroff_method != NULL) {
69 poweroff_method();
70 /* not reached */
71 }
13077d80 72 }
1da177e4
LT
73 }
74 machine_halt();
75}
76
90bf8116
DM
77void (*pm_power_off)(void) = machine_power_off;
78EXPORT_SYMBOL(pm_power_off);
79
1da177e4
LT
80static 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);
13077d80 89
1da177e4
LT
90 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;
67608567 102 if (kernel_execve("/sbin/shutdown", argv, envp) < 0) {
13077d80
DM
103 printk(KERN_ERR "powerd: shutdown execution failed\n");
104 machine_power_off();
1da177e4
LT
105 }
106 return 0;
107}
108
13077d80
DM
109int 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
690c8fd3 122static int __init has_button_interrupt(unsigned int irq, struct device_node *dp)
1da177e4 123{
13077d80 124 if (irq == 0xffffffff)
1da177e4 125 return 0;
690c8fd3 126 if (!of_find_property(dp, "button", NULL))
1da177e4
LT
127 return 0;
128
129 return 1;
130}
131
abbce6e2 132static int __devinit power_probe(struct of_device *op, const struct of_device_id *match)
1da177e4 133{
abbce6e2
DM
134 struct resource *res = &op->resource[0];
135 unsigned int irq= op->irqs[0];
a2bd4fd1 136
abbce6e2
DM
137 power_reg = of_ioremap(res, 0, 0x4, "power");
138
139 printk("%s: Control reg at %lx ... ",
140 op->node->name, res->start);
a2bd4fd1 141
1da177e4 142 poweroff_method = machine_halt; /* able to use the standard halt */
a2bd4fd1 143
abbce6e2 144 if (has_button_interrupt(irq, op->node)) {
13077d80 145 if (start_powerd() < 0)
abbce6e2 146 return 0;
1da177e4 147
2256c13b 148 if (request_irq(irq,
00cde674 149 power_handler, 0, "power", NULL) < 0)
13077d80 150 printk(KERN_ERR "power: Cannot setup IRQ handler.\n");
1da177e4 151 } else {
13077d80 152 printk(KERN_INFO "power: Not using powerd.\n");
1da177e4 153 }
abbce6e2
DM
154
155 return 0;
1da177e4 156}
a2bd4fd1
DM
157
158static struct of_device_id power_match[] = {
159 {
160 .name = "power",
161 },
162 {},
163};
164
abbce6e2 165static struct of_platform_driver power_driver = {
a2bd4fd1
DM
166 .name = "power",
167 .match_table = power_match,
abbce6e2 168 .probe = power_probe,
a2bd4fd1
DM
169};
170
171void __init power_init(void)
172{
37b7754a 173 of_register_driver(&power_driver, &of_platform_bus_type);
a2bd4fd1
DM
174 return;
175}