blob: eae8ca2a6ba5a1abc5a4172fff285b90ddaaf2f8 [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>
David S. Millera3761782007-07-18 13:12:45 -070015#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/auxio.h>
David S. Millerabbce6e2006-06-29 15:22:46 -070019#include <asm/prom.h>
20#include <asm/of_device.h>
21#include <asm/io.h>
David S. Miller22d6a1c2007-05-25 00:37:12 -070022#include <asm/sstate.h>
David S. Millerc3c25242008-02-19 20:39:18 -080023#include <asm/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/unistd.h>
26
27/*
28 * sysctl - toggle power-off restriction for serial console
29 * systems in machine_power_off()
30 */
31int scons_pwroff = 1;
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static void __iomem *power_reg;
34
David S. Miller13077d82007-07-11 18:18:04 -070035static irqreturn_t power_handler(int irq, void *dev_id)
36{
David S. Millera3761782007-07-18 13:12:45 -070037 orderly_poweroff(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 /* FIXME: Check registers for status... */
40 return IRQ_HANDLED;
41}
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static void (*poweroff_method)(void) = machine_alt_power_off;
44
45void machine_power_off(void)
46{
David S. Miller22d6a1c2007-05-25 00:37:12 -070047 sstate_poweroff();
David S. Millerc73fcc82007-07-20 16:59:26 -070048 if (strcmp(of_console_device->type, "serial") || scons_pwroff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (power_reg) {
50 /* Both register bits seem to have the
51 * same effect, so until I figure out
52 * what the difference is...
53 */
54 writel(AUXIO_PCIO_CPWR_OFF | AUXIO_PCIO_SPWR_OFF, power_reg);
David S. Miller13077d82007-07-11 18:18:04 -070055 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (poweroff_method != NULL) {
57 poweroff_method();
58 /* not reached */
59 }
David S. Miller13077d82007-07-11 18:18:04 -070060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
62 machine_halt();
63}
64
David S. Miller90bf8112006-01-09 13:59:12 -080065void (*pm_power_off)(void) = machine_power_off;
66EXPORT_SYMBOL(pm_power_off);
67
David S. Miller690c8fd2006-06-22 19:12:03 -070068static int __init has_button_interrupt(unsigned int irq, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
David S. Miller13077d82007-07-11 18:18:04 -070070 if (irq == 0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return 0;
David S. Miller690c8fd2006-06-22 19:12:03 -070072 if (!of_find_property(dp, "button", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return 0;
74
75 return 1;
76}
77
David S. Millerabbce6e2006-06-29 15:22:46 -070078static int __devinit power_probe(struct of_device *op, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
David S. Millerabbce6e2006-06-29 15:22:46 -070080 struct resource *res = &op->resource[0];
81 unsigned int irq= op->irqs[0];
David S. Millera2bd4fd2006-06-23 01:44:10 -070082
David S. Millerabbce6e2006-06-29 15:22:46 -070083 power_reg = of_ioremap(res, 0, 0x4, "power");
84
David S. Miller2a263022007-07-18 21:18:50 -070085 printk(KERN_INFO "%s: Control reg at %lx\n",
David S. Millerabbce6e2006-06-29 15:22:46 -070086 op->node->name, res->start);
David S. Millera2bd4fd2006-06-23 01:44:10 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 poweroff_method = machine_halt; /* able to use the standard halt */
David S. Millera2bd4fd2006-06-23 01:44:10 -070089
David S. Millerabbce6e2006-06-29 15:22:46 -070090 if (has_button_interrupt(irq, op->node)) {
David S. Miller2256c132005-10-06 20:43:54 -070091 if (request_irq(irq,
David S. Miller00cde672006-06-29 14:39:11 -070092 power_handler, 0, "power", NULL) < 0)
David S. Miller13077d82007-07-11 18:18:04 -070093 printk(KERN_ERR "power: Cannot setup IRQ handler.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
David S. Millerabbce6e2006-06-29 15:22:46 -070095
96 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
David S. Millera2bd4fd2006-06-23 01:44:10 -070098
99static struct of_device_id power_match[] = {
100 {
101 .name = "power",
102 },
103 {},
104};
105
David S. Millerabbce6e2006-06-29 15:22:46 -0700106static struct of_platform_driver power_driver = {
David S. Millera2bd4fd2006-06-23 01:44:10 -0700107 .match_table = power_match,
David S. Millerabbce6e2006-06-29 15:22:46 -0700108 .probe = power_probe,
Stephen Rothwella2cd1552007-10-10 23:27:34 -0700109 .driver = {
110 .name = "power",
111 },
David S. Millera2bd4fd2006-06-23 01:44:10 -0700112};
113
114void __init power_init(void)
115{
Stephen Rothwell37b77542007-04-30 17:43:56 +1000116 of_register_driver(&power_driver, &of_platform_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700117 return;
118}