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 | a376178 | 2007-07-18 13:12:45 -0700 | [diff] [blame] | 10 | #include <linux/reboot.h> |
Stephen Rothwell | 764f257 | 2008-08-07 15:33:36 -0700 | [diff] [blame] | 11 | #include <linux/of_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 13 | #include <asm/prom.h> |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 14 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | static void __iomem *power_reg; |
| 17 | |
David S. Miller | 13077d8 | 2007-07-11 18:18:04 -0700 | [diff] [blame] | 18 | static irqreturn_t power_handler(int irq, void *dev_id) |
| 19 | { |
David S. Miller | a376178 | 2007-07-18 13:12:45 -0700 | [diff] [blame] | 20 | orderly_poweroff(true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | /* FIXME: Check registers for status... */ |
| 23 | return IRQ_HANDLED; |
| 24 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
David S. Miller | 19ab6db | 2009-04-07 00:47:44 -0700 | [diff] [blame] | 26 | static int __devinit has_button_interrupt(unsigned int irq, struct device_node *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
David S. Miller | 13077d8 | 2007-07-11 18:18:04 -0700 | [diff] [blame] | 28 | if (irq == 0xffffffff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | return 0; |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 30 | if (!of_find_property(dp, "button", NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | return 0; |
| 32 | |
| 33 | return 1; |
| 34 | } |
| 35 | |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 36 | 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] | 37 | { |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 38 | struct resource *res = &op->resource[0]; |
| 39 | unsigned int irq= op->irqs[0]; |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 40 | |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 41 | power_reg = of_ioremap(res, 0, 0x4, "power"); |
| 42 | |
Sam Ravnborg | 9018113 | 2009-01-06 13:19:28 -0800 | [diff] [blame] | 43 | printk(KERN_INFO "%s: Control reg at %llx\n", |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 44 | op->node->name, res->start); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 45 | |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 46 | if (has_button_interrupt(irq, op->node)) { |
David S. Miller | 2256c13 | 2005-10-06 20:43:54 -0700 | [diff] [blame] | 47 | if (request_irq(irq, |
David S. Miller | 00cde67 | 2006-06-29 14:39:11 -0700 | [diff] [blame] | 48 | power_handler, 0, "power", NULL) < 0) |
David S. Miller | 13077d8 | 2007-07-11 18:18:04 -0700 | [diff] [blame] | 49 | printk(KERN_ERR "power: Cannot setup IRQ handler.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 51 | |
| 52 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 54 | |
David S. Miller | fd09831 | 2008-08-31 01:23:17 -0700 | [diff] [blame] | 55 | static struct of_device_id __initdata power_match[] = { |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 56 | { |
| 57 | .name = "power", |
| 58 | }, |
| 59 | {}, |
| 60 | }; |
| 61 | |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 62 | static struct of_platform_driver power_driver = { |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 63 | .match_table = power_match, |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 64 | .probe = power_probe, |
Stephen Rothwell | a2cd155 | 2007-10-10 23:27:34 -0700 | [diff] [blame] | 65 | .driver = { |
| 66 | .name = "power", |
| 67 | }, |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
David S. Miller | c510b9b | 2008-08-30 01:18:56 -0700 | [diff] [blame] | 70 | static int __init power_init(void) |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 71 | { |
David S. Miller | c510b9b | 2008-08-30 01:18:56 -0700 | [diff] [blame] | 72 | return of_register_driver(&power_driver, &of_platform_bus_type); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 73 | } |
David S. Miller | c510b9b | 2008-08-30 01:18:56 -0700 | [diff] [blame] | 74 | |
| 75 | device_initcall(power_init); |