blob: 1836cb965ff80d407afd03463575872bd57c7015 [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>
Paul Gortmaker066bcac2011-07-22 13:18:16 -04007#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/interrupt.h>
David S. Millera3761782007-07-18 13:12:45 -070010#include <linux/reboot.h>
Stephen Rothwell764f2572008-08-07 15:33:36 -070011#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
David S. Millerabbce6e2006-06-29 15:22:46 -070013#include <asm/prom.h>
David S. Millerabbce6e2006-06-29 15:22:46 -070014#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016static void __iomem *power_reg;
17
David S. Miller13077d82007-07-11 18:18:04 -070018static irqreturn_t power_handler(int irq, void *dev_id)
19{
David S. Millera3761782007-07-18 13:12:45 -070020 orderly_poweroff(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22 /* FIXME: Check registers for status... */
23 return IRQ_HANDLED;
24}
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -080026static int has_button_interrupt(unsigned int irq, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
David S. Miller13077d82007-07-11 18:18:04 -070028 if (irq == 0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 return 0;
David S. Miller690c8fd2006-06-22 19:12:03 -070030 if (!of_find_property(dp, "button", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return 0;
32
33 return 1;
34}
35
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -080036static int power_probe(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
David S. Millerabbce6e2006-06-29 15:22:46 -070038 struct resource *res = &op->resource[0];
Grant Likely1636f8a2010-06-18 11:09:58 -060039 unsigned int irq = op->archdata.irqs[0];
David S. Millera2bd4fd2006-06-23 01:44:10 -070040
David S. Millerabbce6e2006-06-29 15:22:46 -070041 power_reg = of_ioremap(res, 0, 0x4, "power");
42
Sam Ravnborg90181132009-01-06 13:19:28 -080043 printk(KERN_INFO "%s: Control reg at %llx\n",
Grant Likely61c7a082010-04-13 16:12:29 -070044 op->dev.of_node->name, res->start);
David S. Millera2bd4fd2006-06-23 01:44:10 -070045
Grant Likely61c7a082010-04-13 16:12:29 -070046 if (has_button_interrupt(irq, op->dev.of_node)) {
David S. Miller2256c132005-10-06 20:43:54 -070047 if (request_irq(irq,
David S. Miller00cde672006-06-29 14:39:11 -070048 power_handler, 0, "power", NULL) < 0)
David S. Miller13077d82007-07-11 18:18:04 -070049 printk(KERN_ERR "power: Cannot setup IRQ handler.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
David S. Millerabbce6e2006-06-29 15:22:46 -070051
52 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
David S. Millera2bd4fd2006-06-23 01:44:10 -070054
David S. Miller3628aa02011-03-30 17:37:56 -070055static const struct of_device_id power_match[] = {
David S. Millera2bd4fd2006-06-23 01:44:10 -070056 {
57 .name = "power",
58 },
59 {},
60};
61
Grant Likely4ebb24f2011-02-22 20:01:33 -070062static struct platform_driver power_driver = {
David S. Millerabbce6e2006-06-29 15:22:46 -070063 .probe = power_probe,
Grant Likely40182942010-04-13 16:13:02 -070064 .driver = {
65 .name = "power",
Grant Likely40182942010-04-13 16:13:02 -070066 .of_match_table = power_match,
Stephen Rothwella2cd1552007-10-10 23:27:34 -070067 },
David S. Millera2bd4fd2006-06-23 01:44:10 -070068};
69
David S. Millerc510b9b2008-08-30 01:18:56 -070070static int __init power_init(void)
David S. Millera2bd4fd2006-06-23 01:44:10 -070071{
Grant Likely4ebb24f2011-02-22 20:01:33 -070072 return platform_driver_register(&power_driver);
David S. Millera2bd4fd2006-06-23 01:44:10 -070073}
David S. Millerc510b9b2008-08-30 01:18:56 -070074
75device_initcall(power_init);