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> |
Paul Gortmaker | 066bcac | 2011-07-22 13:18:16 -0400 | [diff] [blame] | 7 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Greg Kroah-Hartman | 7c9503b | 2012-12-21 14:03:26 -0800 | [diff] [blame] | 26 | static int 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 | |
Greg Kroah-Hartman | 7c9503b | 2012-12-21 14:03:26 -0800 | [diff] [blame] | 36 | static int power_probe(struct platform_device *op) |
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]; |
Grant Likely | 1636f8a | 2010-06-18 11:09:58 -0600 | [diff] [blame] | 39 | unsigned int irq = op->archdata.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", |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 44 | op->dev.of_node->name, res->start); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 45 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 46 | if (has_button_interrupt(irq, op->dev.of_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 | 3628aa0 | 2011-03-30 17:37:56 -0700 | [diff] [blame] | 55 | static const struct of_device_id power_match[] = { |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 56 | { |
| 57 | .name = "power", |
| 58 | }, |
| 59 | {}, |
| 60 | }; |
| 61 | |
Grant Likely | 4ebb24f | 2011-02-22 20:01:33 -0700 | [diff] [blame] | 62 | static struct platform_driver power_driver = { |
David S. Miller | abbce6e | 2006-06-29 15:22:46 -0700 | [diff] [blame] | 63 | .probe = power_probe, |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 64 | .driver = { |
| 65 | .name = "power", |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 66 | .of_match_table = power_match, |
Stephen Rothwell | a2cd155 | 2007-10-10 23:27:34 -0700 | [diff] [blame] | 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 | { |
Grant Likely | 4ebb24f | 2011-02-22 20:01:33 -0700 | [diff] [blame] | 72 | return platform_driver_register(&power_driver); |
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); |