]> 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
34 #include "retu.h"
35
36 #define RETU_STATUS_PWRONX      (1 << 5)
37
38 #define PWRBTN_DELAY            20
39 #define PWRBTN_UP               0
40 #define PWRBTN_PRESSED          1
41
42 static int pwrbtn_state;
43 static struct input_dev *pwrbtn_dev;
44 static struct timer_list pwrbtn_timer;
45
46 static void retubutton_timer_func(unsigned long arg)
47 {
48         int state;
49
50         if (retu_read_reg(RETU_REG_STATUS) & RETU_STATUS_PWRONX)
51                 state = PWRBTN_UP;
52         else
53                 state = PWRBTN_PRESSED;
54
55         if (pwrbtn_state != state) {
56                 input_report_key(pwrbtn_dev, KEY_POWER, state);
57                 pwrbtn_state = state;
58         }
59 }
60
61 /**
62  * Interrupt function is called whenever power button key is pressed
63  * or released.
64  */
65 static void retubutton_irq(unsigned long arg)
66 {
67         retu_ack_irq(RETU_INT_PWR);
68         mod_timer(&pwrbtn_timer, jiffies + msecs_to_jiffies(PWRBTN_DELAY));
69 }
70
71 /**
72  * Init function.
73  * Allocates interrupt for power button and registers itself to input layer.
74  */
75 static int __init retubutton_init(void)
76 {
77         int irq;
78
79         printk(KERN_INFO "Retu power button driver initialized\n");
80         irq = RETU_INT_PWR;
81
82         init_timer(&pwrbtn_timer);
83         pwrbtn_timer.function = retubutton_timer_func;
84
85         if (retu_request_irq(irq, &retubutton_irq, 0, "PwrOnX") < 0) {
86                 printk(KERN_ERR "%s@%s: Cannot allocate irq\n",
87                        __FUNCTION__, __FILE__);
88                 return -EBUSY;
89         }
90
91         pwrbtn_dev = input_allocate_device();
92         if (!pwrbtn_dev)
93                 return -ENOMEM;
94
95         pwrbtn_dev->evbit[0] = BIT(EV_KEY);
96         pwrbtn_dev->keybit[LONG(KEY_POWER)] = BIT(KEY_POWER);
97         pwrbtn_dev->name = "retu-pwrbutton";
98
99         input_register_device(pwrbtn_dev);
100
101         return 0;
102 }
103
104 /**
105  * Cleanup function which is called when driver is unloaded
106  */
107 static void __exit retubutton_exit(void)
108 {
109         retu_free_irq(RETU_INT_PWR);
110         del_timer_sync(&pwrbtn_timer);
111         input_unregister_device(pwrbtn_dev);
112 }
113
114 module_init(retubutton_init);
115 module_exit(retubutton_exit);
116
117 MODULE_DESCRIPTION("Retu Power Button");
118 MODULE_LICENSE("GPL");
119 MODULE_AUTHOR("Ari Saastamoinen");