]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/cbus/retu-pwrbutton.c
Add Nokia CBUS support
[linux-2.6-omap-h63xx.git] / drivers / cbus / retu-pwrbutton.c
1 /**
2  * drivers/cbus/retu-pwrbutton.c
3  *
4  * Driver for sending retu power button event to input-layer
5  *
6  * Copyright (C) 2004 Nokia Corporation
7  *
8  * Written by Ari Saastamoinen <ari.saastamoinen@elektrobit.com>
9  *
10  * Contact Juha Yrjölä <juha.yrjola@nokia.com>
11  *
12  * This file is subject to the terms and conditions of the GNU General
13  * Public License. See the file "COPYING" in the main directory of this
14  * archive for more details.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/input.h>
31 #include <linux/timer.h>
32 #include <linux/jiffies.h>
33 #include <linux/bitops.h>
34
35 #include "retu.h"
36
37 #define RETU_STATUS_PWRONX      (1 << 5)
38
39 #define PWRBTN_DELAY            20
40 #define PWRBTN_UP               0
41 #define PWRBTN_PRESSED          1
42
43 static int pwrbtn_state;
44 static struct input_dev *pwrbtn_dev;
45 static struct timer_list pwrbtn_timer;
46
47 static void retubutton_timer_func(unsigned long arg)
48 {
49         int state;
50
51         if (retu_read_reg(RETU_REG_STATUS) & RETU_STATUS_PWRONX)
52                 state = PWRBTN_UP;
53         else
54                 state = PWRBTN_PRESSED;
55
56         if (pwrbtn_state != state) {
57                 input_report_key(pwrbtn_dev, KEY_POWER, state);
58                 pwrbtn_state = state;
59         }
60 }
61
62 /**
63  * Interrupt function is called whenever power button key is pressed
64  * or released.
65  */
66 static void retubutton_irq(unsigned long arg)
67 {
68         retu_ack_irq(RETU_INT_PWR);
69         mod_timer(&pwrbtn_timer, jiffies + msecs_to_jiffies(PWRBTN_DELAY));
70 }
71
72 /**
73  * Init function.
74  * Allocates interrupt for power button and registers itself to input layer.
75  */
76 static int __init retubutton_init(void)
77 {
78         int irq;
79
80         printk(KERN_INFO "Retu power button driver initialized\n");
81         irq = RETU_INT_PWR;
82
83         init_timer(&pwrbtn_timer);
84         pwrbtn_timer.function = retubutton_timer_func;
85
86         if (retu_request_irq(irq, &retubutton_irq, 0, "PwrOnX") < 0) {
87                 printk(KERN_ERR "%s@%s: Cannot allocate irq\n",
88                        __FUNCTION__, __FILE__);
89                 return -EBUSY;
90         }
91
92         pwrbtn_dev = input_allocate_device();
93         if (!pwrbtn_dev)
94                 return -ENOMEM;
95
96         pwrbtn_dev->evbit[0] = BIT_MASK(EV_KEY);
97         pwrbtn_dev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
98         pwrbtn_dev->name = "retu-pwrbutton";
99
100         return input_register_device(pwrbtn_dev);
101 }
102
103 /**
104  * Cleanup function which is called when driver is unloaded
105  */
106 static void __exit retubutton_exit(void)
107 {
108         retu_free_irq(RETU_INT_PWR);
109         del_timer_sync(&pwrbtn_timer);
110         input_unregister_device(pwrbtn_dev);
111 }
112
113 module_init(retubutton_init);
114 module_exit(retubutton_exit);
115
116 MODULE_DESCRIPTION("Retu Power Button");
117 MODULE_LICENSE("GPL");
118 MODULE_AUTHOR("Ari Saastamoinen");