]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030-pwrbutton.c
I2C: TWL4030: Add power button and power IRQ support
[linux-2.6-omap-h63xx.git] / drivers / i2c / chips / twl4030-pwrbutton.c
1 /**
2  * drivers/i2c/chips/twl4030-pwrbutton.c
3  *
4  * Driver for sending triton2 power button event to input-layer
5  *
6  * Copyright (C) 2008 Nokia Corporation
7  *
8  * Written by Peter De Schrijver <peter.de-schrijver@nokia.com>
9  *
10  * This file is subject to the terms and conditions of the GNU General
11  * Public License. See the file "COPYING" in the main directory of this
12  * archive for more details.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/input.h>
29 #include <linux/timer.h>
30 #include <linux/jiffies.h>
31 #include <linux/kthread.h>
32 #include <linux/interrupt.h>
33 #include <linux/i2c/twl4030.h>
34
35 #define PWR_ISR1 0
36 #define PWR_IMR1 1
37 #define PWR_PWRON_IRQ (1<<0)
38
39 #define PWR_EDR1 5
40 #define PWR_PWRON_RISING (1<<1)
41 #define PWR_PWRON_FALLING  (1<<0)
42 #define PWR_PWRON_BOTH (PWR_PWRON_RISING | PWR_PWRON_FALLING)
43
44 #define PWR_SIH_CTRL 7
45
46 #define STS_HW_CONDITIONS 0xf
47
48 static struct input_dev *powerbutton_dev;
49
50 /*
51  * Note : the following function runs in kernel thread context
52  * with IRQs enabled
53  */
54
55 static irqreturn_t powerbutton_irq(int irq, void *dev_id)
56 {
57         int err;
58         u8 value;
59
60         err = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &value,
61                                   STS_HW_CONDITIONS);
62         if (!err)  {
63                 input_report_key(powerbutton_dev, KEY_POWER,
64                                  value & PWR_PWRON_IRQ);
65         } else {
66                 printk(KERN_WARNING "I2C error %d while reading TWL4030"
67                         "PM_MASTER STS_HW_CONDITIONS register\n", err);
68         }
69
70         return IRQ_HANDLED;
71 }
72
73 static int __init twl4030_pwrbutton_init(void)
74 {
75         int err = 0;
76         u8 value;
77
78         if (request_irq(TWL4030_PWRIRQ_PWRBTN, powerbutton_irq, 0,
79                         "PwrButton", NULL) < 0) {
80                 printk(KERN_ERR "Unable to allocate IRQ for power button\n");
81                 err = -EBUSY;
82                 goto out;
83         }
84
85         powerbutton_dev = input_allocate_device();
86         if (!powerbutton_dev) {
87                 printk(KERN_ERR
88                         "Unable to allocate input device for power button\n");
89                 err = -ENOMEM;
90                 goto free_irq_and_out;
91         }
92
93         powerbutton_dev->evbit[0] = BIT_MASK(EV_KEY);
94         powerbutton_dev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
95         powerbutton_dev->name = "triton2-pwrbutton";
96
97         err = input_register_device(powerbutton_dev);
98         if (err)
99                 goto free_input_dev;
100
101         err = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &value, PWR_IMR1);
102         if (err) {
103                 printk(KERN_WARNING "I2C error %d while reading TWL4030"
104                                         "INT PWR_IMR1 register\n", err);
105
106                 goto free_input_dev;
107         }
108
109         err = twl4030_i2c_write_u8(TWL4030_MODULE_INT,
110                                    value & (~PWR_PWRON_IRQ), PWR_IMR1);
111         if (err) {
112                 printk(KERN_WARNING "I2C error %d while writing TWL4030"
113                                     "INT PWR_IMR1 register\n", err);
114                 goto free_input_dev;
115         }
116
117         err = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &value, PWR_EDR1);
118         if (err) {
119                 printk(KERN_WARNING "I2C error %d while reading TWL4030"
120                                         "INT PWR_EDR1 register\n", err);
121                 goto free_input_dev;
122         }
123
124         err = twl4030_i2c_write_u8(TWL4030_MODULE_INT,
125                                    value | PWR_PWRON_BOTH , PWR_EDR1);
126
127         if (err) {
128                 printk(KERN_WARNING "I2C error %d while writing TWL4030"
129                                         "INT PWR_EDR1 register\n", err);
130                 goto free_input_dev;
131         }
132
133         printk(KERN_INFO "triton2 power button driver initialized\n");
134
135         return 0;
136
137
138 free_input_dev:
139         input_free_device(powerbutton_dev);
140 free_irq_and_out:
141         free_irq(TWL4030_PWRIRQ_PWRBTN, NULL);
142 out:
143         return err;
144 }
145
146 static void __exit twl4030_pwrbutton_exit(void)
147 {
148         free_irq(TWL4030_PWRIRQ_PWRBTN, NULL);
149         input_unregister_device(powerbutton_dev);
150         input_free_device(powerbutton_dev);
151
152 }
153
154 module_init(twl4030_pwrbutton_init);
155 module_exit(twl4030_pwrbutton_exit);
156
157 MODULE_DESCRIPTION("Triton2 Power Button");
158 MODULE_LICENSE("GPL");
159 MODULE_AUTHOR("Peter De Schrijver");
160