David S. Miller | 13077d8 | 2007-07-11 18:18:04 -0700 | [diff] [blame] | 1 | /* power.c: Power management driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David S. Miller | c510b9b | 2008-08-30 01:18:56 -0700 | [diff] [blame] | 3 | * Copyright (C) 1999, 2007, 2008 David S. Miller (davem@davemloft.net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/kernel.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/interrupt.h> |
David S. Miller | 90bf811 | 2006-01-09 13:59:12 -0800 | [diff] [blame] | 10 | #include <linux/pm.h> |
David S. Miller | a376178 | 2007-07-18 13:12:45 -0700 | [diff] [blame] | 11 | #include <linux/reboot.h> |
Stephen Rothwell | 764f257 | 2008-08-07 15:33:36 -0700 | [diff] [blame] | 12 | #include <linux/of_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <asm/auxio.h> |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 15 | #include <asm/prom.h> |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 16 | #include <asm/io.h> |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 17 | #include <asm/sstate.h> |
David S. Miller | c3c2524 | 2008-02-19 20:39:18 -0800 | [diff] [blame] | 18 | #include <asm/reboot.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | /* |
| 21 | * sysctl - toggle power-off restriction for serial console |
| 22 | * systems in machine_power_off() |
| 23 | */ |
| 24 | int scons_pwroff = 1; |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | static void __iomem *power_reg; |
| 27 | |
David S. Miller | 13077d8 | 2007-07-11 18:18:04 -0700 | [diff] [blame] | 28 | static irqreturn_t power_handler(int irq, void *dev_id) |
| 29 | { |
David S. Miller | a376178 | 2007-07-18 13:12:45 -0700 | [diff] [blame] | 30 | orderly_poweroff(true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | /* FIXME: Check registers for status... */ |
| 33 | return IRQ_HANDLED; |
| 34 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static void (*poweroff_method)(void) = machine_alt_power_off; |
| 37 | |
| 38 | void machine_power_off(void) |
| 39 | { |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 40 | sstate_poweroff(); |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 41 | if (strcmp(of_console_device->type, "serial") || scons_pwroff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | 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. Miller | 13077d8 | 2007-07-11 18:18:04 -0700 | [diff] [blame] | 48 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | if (poweroff_method != NULL) { |
| 50 | poweroff_method(); |
| 51 | /* not reached */ |
| 52 | } |
David S. Miller | 13077d8 | 2007-07-11 18:18:04 -0700 | [diff] [blame] | 53 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | machine_halt(); |
| 56 | } |
| 57 | |
David S. Miller | 90bf811 | 2006-01-09 13:59:12 -0800 | [diff] [blame] | 58 | void (*pm_power_off)(void) = machine_power_off; |
| 59 | EXPORT_SYMBOL(pm_power_off); |
| 60 | |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 61 | static int __init has_button_interrupt(unsigned int irq, struct device_node *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
David S. Miller | 13077d8 | 2007-07-11 18:18:04 -0700 | [diff] [blame] | 63 | if (irq == 0xffffffff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return 0; |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 65 | if (!of_find_property(dp, "button", NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | return 0; |
| 67 | |
| 68 | return 1; |
| 69 | } |
| 70 | |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 71 | static int __devinit power_probe(struct of_device *op, const struct of_device_id *match) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 73 | struct resource *res = &op->resource[0]; |
| 74 | unsigned int irq= op->irqs[0]; |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 75 | |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 76 | power_reg = of_ioremap(res, 0, 0x4, "power"); |
| 77 | |
David S. Miller | 2a26302 | 2007-07-18 21:18:50 -0700 | [diff] [blame] | 78 | printk(KERN_INFO "%s: Control reg at %lx\n", |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 79 | op->node->name, res->start); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | poweroff_method = machine_halt; /* able to use the standard halt */ |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 82 | |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 83 | if (has_button_interrupt(irq, op->node)) { |
David S. Miller | 2256c13 | 2005-10-06 20:43:54 -0700 | [diff] [blame] | 84 | if (request_irq(irq, |
David S. Miller | 00cde67 | 2006-06-29 14:39:11 -0700 | [diff] [blame] | 85 | power_handler, 0, "power", NULL) < 0) |
David S. Miller | 13077d8 | 2007-07-11 18:18:04 -0700 | [diff] [blame] | 86 | printk(KERN_ERR "power: Cannot setup IRQ handler.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 88 | |
| 89 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 91 | |
David S. Miller | fd09831 | 2008-08-31 01:23:17 -0700 | [diff] [blame^] | 92 | static struct of_device_id __initdata power_match[] = { |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 93 | { |
| 94 | .name = "power", |
| 95 | }, |
| 96 | {}, |
| 97 | }; |
| 98 | |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 99 | static struct of_platform_driver power_driver = { |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 100 | .match_table = power_match, |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 101 | .probe = power_probe, |
Stephen Rothwell | a2cd155 | 2007-10-10 23:27:34 -0700 | [diff] [blame] | 102 | .driver = { |
| 103 | .name = "power", |
| 104 | }, |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
David S. Miller | c510b9b | 2008-08-30 01:18:56 -0700 | [diff] [blame] | 107 | static int __init power_init(void) |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 108 | { |
David S. Miller | c510b9b | 2008-08-30 01:18:56 -0700 | [diff] [blame] | 109 | return of_register_driver(&power_driver, &of_platform_bus_type); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 110 | } |
David S. Miller | c510b9b | 2008-08-30 01:18:56 -0700 | [diff] [blame] | 111 | |
| 112 | device_initcall(power_init); |