blob: 7559ad395a33d4bf293d53791899b5e270b25c56 [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. Millerc510b9b2008-08-30 01:18:56 -07003 * Copyright (C) 1999, 2007, 2008 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/interrupt.h>
David S. Miller90bf8112006-01-09 13:59:12 -080010#include <linux/pm.h>
David S. Millera3761782007-07-18 13:12:45 -070011#include <linux/reboot.h>
Stephen Rothwell764f2572008-08-07 15:33:36 -070012#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/auxio.h>
David S. Millerabbce6e2006-06-29 15:22:46 -070015#include <asm/prom.h>
David S. Millerabbce6e2006-06-29 15:22:46 -070016#include <asm/io.h>
David S. Miller22d6a1c2007-05-25 00:37:12 -070017#include <asm/sstate.h>
David S. Millerc3c25242008-02-19 20:39:18 -080018#include <asm/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/*
21 * sysctl - toggle power-off restriction for serial console
22 * systems in machine_power_off()
23 */
24int scons_pwroff = 1;
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static void __iomem *power_reg;
27
David S. Miller13077d82007-07-11 18:18:04 -070028static irqreturn_t power_handler(int irq, void *dev_id)
29{
David S. Millera3761782007-07-18 13:12:45 -070030 orderly_poweroff(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32 /* FIXME: Check registers for status... */
33 return IRQ_HANDLED;
34}
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static void (*poweroff_method)(void) = machine_alt_power_off;
37
38void machine_power_off(void)
39{
David S. Miller22d6a1c2007-05-25 00:37:12 -070040 sstate_poweroff();
David S. Millerc73fcc82007-07-20 16:59:26 -070041 if (strcmp(of_console_device->type, "serial") || scons_pwroff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (power_reg) {
43 /* Both register bits seem to have the
44 * same effect, so until I figure out
45 * what the difference is...
46 */
47 writel(AUXIO_PCIO_CPWR_OFF | AUXIO_PCIO_SPWR_OFF, power_reg);
David S. Miller13077d82007-07-11 18:18:04 -070048 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (poweroff_method != NULL) {
50 poweroff_method();
51 /* not reached */
52 }
David S. Miller13077d82007-07-11 18:18:04 -070053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
55 machine_halt();
56}
57
David S. Miller90bf8112006-01-09 13:59:12 -080058void (*pm_power_off)(void) = machine_power_off;
59EXPORT_SYMBOL(pm_power_off);
60
David S. Miller690c8fd2006-06-22 19:12:03 -070061static int __init has_button_interrupt(unsigned int irq, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
David S. Miller13077d82007-07-11 18:18:04 -070063 if (irq == 0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return 0;
David S. Miller690c8fd2006-06-22 19:12:03 -070065 if (!of_find_property(dp, "button", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 0;
67
68 return 1;
69}
70
David S. Millerabbce6e2006-06-29 15:22:46 -070071static int __devinit power_probe(struct of_device *op, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
David S. Millerabbce6e2006-06-29 15:22:46 -070073 struct resource *res = &op->resource[0];
74 unsigned int irq= op->irqs[0];
David S. Millera2bd4fd2006-06-23 01:44:10 -070075
David S. Millerabbce6e2006-06-29 15:22:46 -070076 power_reg = of_ioremap(res, 0, 0x4, "power");
77
David S. Miller2a263022007-07-18 21:18:50 -070078 printk(KERN_INFO "%s: Control reg at %lx\n",
David S. Millerabbce6e2006-06-29 15:22:46 -070079 op->node->name, res->start);
David S. Millera2bd4fd2006-06-23 01:44:10 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 poweroff_method = machine_halt; /* able to use the standard halt */
David S. Millera2bd4fd2006-06-23 01:44:10 -070082
David S. Millerabbce6e2006-06-29 15:22:46 -070083 if (has_button_interrupt(irq, op->node)) {
David S. Miller2256c132005-10-06 20:43:54 -070084 if (request_irq(irq,
David S. Miller00cde672006-06-29 14:39:11 -070085 power_handler, 0, "power", NULL) < 0)
David S. Miller13077d82007-07-11 18:18:04 -070086 printk(KERN_ERR "power: Cannot setup IRQ handler.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
David S. Millerabbce6e2006-06-29 15:22:46 -070088
89 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
David S. Millera2bd4fd2006-06-23 01:44:10 -070091
David S. Millerfd098312008-08-31 01:23:17 -070092static struct of_device_id __initdata power_match[] = {
David S. Millera2bd4fd2006-06-23 01:44:10 -070093 {
94 .name = "power",
95 },
96 {},
97};
98
David S. Millerabbce6e2006-06-29 15:22:46 -070099static struct of_platform_driver power_driver = {
David S. Millera2bd4fd2006-06-23 01:44:10 -0700100 .match_table = power_match,
David S. Millerabbce6e2006-06-29 15:22:46 -0700101 .probe = power_probe,
Stephen Rothwella2cd1552007-10-10 23:27:34 -0700102 .driver = {
103 .name = "power",
104 },
David S. Millera2bd4fd2006-06-23 01:44:10 -0700105};
106
David S. Millerc510b9b2008-08-30 01:18:56 -0700107static int __init power_init(void)
David S. Millera2bd4fd2006-06-23 01:44:10 -0700108{
David S. Millerc510b9b2008-08-30 01:18:56 -0700109 return of_register_driver(&power_driver, &of_platform_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700110}
David S. Millerc510b9b2008-08-30 01:18:56 -0700111
112device_initcall(power_init);