1/*
2 * arch/arm/mach-ixp4xx/dsmg600-power.c
3 *
4 * DSM-G600 Power/Reset driver
5 * Author: Michael Westerhof <mwester@dls.net>
6 *
7 * Based on nslu2-power.c
8 *  Copyright (C) 2005 Tower Technologies
9 *  Author: Alessandro Zummo <a.zummo@towertech.it>
10 *
11 * which was based on nslu2-io.c
12 *  Copyright (C) 2004 Karen Spearel
13 *
14 * Maintainers: http://www.nslu2-linux.org/
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
19 *
20 */
21
22#include <linux/module.h>
23#include <linux/reboot.h>
24#include <linux/interrupt.h>
25#include <linux/irq.h>
26#include <linux/jiffies.h>
27#include <linux/timer.h>
28
29#include <asm/mach-types.h>
30
31extern void ctrl_alt_del(void);
32
33/* This is used to make sure the power-button pusher is serious.  The button
34 * must be held until the value of this counter reaches zero.
35 */
36static volatile int power_button_countdown;
37
38/* Must hold the button down for at least this many counts to be processed */
39#define PBUTTON_HOLDDOWN_COUNT 4 /* 2 secs */
40
41static void dsmg600_power_handler(unsigned long data);
42static DEFINE_TIMER(dsmg600_power_timer, dsmg600_power_handler, 0, 0);
43
44static void dsmg600_power_handler(unsigned long data)
45{
46	/* This routine is called twice per second to check the
47	 * state of the power button.
48	 */
49
50	if (*IXP4XX_GPIO_GPINR & DSMG600_PB_BM) {
51
52		/* IO Pin is 1 (button pushed) */
53		if (power_button_countdown == 0) {
54			/* Signal init to do the ctrlaltdel action, this will bypass
55			 * init if it hasn't started and do a kernel_restart.
56			 */
57			ctrl_alt_del();
58
59			/* Change the state of the power LED to "blink" */
60			gpio_line_set(DSMG600_LED_PWR_GPIO, IXP4XX_GPIO_LOW);
61		}
62		power_button_countdown--;
63
64	} else {
65		power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
66	}
67
68	mod_timer(&dsmg600_power_timer, jiffies + msecs_to_jiffies(500));
69}
70
71static irqreturn_t dsmg600_reset_handler(int irq, void *dev_id)
72{
73	/* This is the paper-clip reset, it shuts the machine down directly. */
74	machine_power_off();
75
76	return IRQ_HANDLED;
77}
78
79static int __init dsmg600_power_init(void)
80{
81	if (!(machine_is_dsmg600()))
82		return 0;
83
84	if (request_irq(DSMG600_RB_IRQ, &dsmg600_reset_handler,
85		IRQF_DISABLED | IRQF_TRIGGER_LOW, "DSM-G600 reset button",
86		NULL) < 0) {
87
88		printk(KERN_DEBUG "Reset Button IRQ %d not available\n",
89			DSMG600_RB_IRQ);
90
91		return -EIO;
92	}
93
94	/* The power button on the D-Link DSM-G600 is on GPIO 15, but
95	 * it cannot handle interrupts on that GPIO line.  So we'll
96	 * have to poll it with a kernel timer.
97	 */
98
99	/* Make sure that the power button GPIO is set up as an input */
100	gpio_line_config(DSMG600_PB_GPIO, IXP4XX_GPIO_IN);
101
102	/* Set the initial value for the power button IRQ handler */
103	power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
104
105	mod_timer(&dsmg600_power_timer, jiffies + msecs_to_jiffies(500));
106
107	return 0;
108}
109
110static void __exit dsmg600_power_exit(void)
111{
112	if (!(machine_is_dsmg600()))
113		return;
114
115	del_timer_sync(&dsmg600_power_timer);
116
117	free_irq(DSMG600_RB_IRQ, NULL);
118}
119
120module_init(dsmg600_power_init);
121module_exit(dsmg600_power_exit);
122
123MODULE_AUTHOR("Michael Westerhof <mwester@dls.net>");
124MODULE_DESCRIPTION("DSM-G600 Power/Reset driver");
125MODULE_LICENSE("GPL");
126