Merge current mainline tree into linux-omap tree
[pandora-kernel.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/interrupt.h>
30 #include <linux/i2c/twl4030.h>
31
32 #define PWR_ISR1 0
33 #define PWR_IMR1 1
34 #define PWR_PWRON_IRQ (1<<0)
35
36 #define PWR_EDR1 5
37 #define PWR_PWRON_RISING (1<<1)
38 #define PWR_PWRON_FALLING  (1<<0)
39 #define PWR_PWRON_BOTH (PWR_PWRON_RISING | PWR_PWRON_FALLING)
40
41 #define PWR_SIH_CTRL 7
42
43 #define STS_HW_CONDITIONS 0xf
44
45 static struct input_dev *powerbutton_dev;
46
47 static irqreturn_t powerbutton_irq(int irq, void *dev_id)
48 {
49         int err;
50         u8 value;
51
52         err = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &value,
53                                   STS_HW_CONDITIONS);
54         if (!err)  {
55                 input_report_key(powerbutton_dev, KEY_POWER,
56                                  value & PWR_PWRON_IRQ);
57         } else {
58                 pr_err("twl4030: i2c error %d while reading TWL4030"
59                         " PM_MASTER STS_HW_CONDITIONS register\n", err);
60         }
61
62         return IRQ_HANDLED;
63 }
64
65 static int __init twl4030_pwrbutton_init(void)
66 {
67         int err = 0;
68         u8 value;
69
70         if (request_irq(TWL4030_PWRIRQ_PWRBTN, powerbutton_irq, 0,
71                         "PwrButton", NULL) < 0) {
72                 printk(KERN_ERR "Unable to allocate IRQ for power button\n");
73                 err = -EBUSY;
74                 goto out;
75         }
76
77         powerbutton_dev = input_allocate_device();
78         if (!powerbutton_dev) {
79                 printk(KERN_ERR
80                         "Unable to allocate input device for power button\n");
81                 err = -ENOMEM;
82                 goto free_irq_and_out;
83         }
84
85         powerbutton_dev->evbit[0] = BIT_MASK(EV_KEY);
86         powerbutton_dev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
87         powerbutton_dev->name = "triton2-pwrbutton";
88
89         err = input_register_device(powerbutton_dev);
90         if (err) {
91                 input_free_device(powerbutton_dev);
92                 goto free_irq_and_out;
93         }
94
95         err = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &value, PWR_IMR1);
96         if (err) {
97                 printk(KERN_WARNING "I2C error %d while reading TWL4030"
98                                         " INT PWR_IMR1 register\n", err);
99
100                 goto free_input_dev;
101         }
102
103         err = twl4030_i2c_write_u8(TWL4030_MODULE_INT,
104                                    value & (~PWR_PWRON_IRQ), PWR_IMR1);
105         if (err) {
106                 printk(KERN_WARNING "I2C error %d while writing TWL4030"
107                                     " INT PWR_IMR1 register\n", err);
108                 goto free_input_dev;
109         }
110
111         err = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &value, PWR_EDR1);
112         if (err) {
113                 printk(KERN_WARNING "I2C error %d while reading TWL4030"
114                                         " INT PWR_EDR1 register\n", err);
115                 goto free_input_dev;
116         }
117
118         err = twl4030_i2c_write_u8(TWL4030_MODULE_INT,
119                                    value | PWR_PWRON_BOTH, PWR_EDR1);
120
121         if (err) {
122                 printk(KERN_WARNING "I2C error %d while writing TWL4030"
123                                         " INT PWR_EDR1 register\n", err);
124                 goto free_input_dev;
125         }
126
127         printk(KERN_INFO "triton2 power button driver initialized\n");
128
129         return 0;
130
131
132 free_input_dev:
133         input_unregister_device(powerbutton_dev);
134 free_irq_and_out:
135         free_irq(TWL4030_PWRIRQ_PWRBTN, NULL);
136 out:
137         return err;
138 }
139
140 static void __exit twl4030_pwrbutton_exit(void)
141 {
142         free_irq(TWL4030_PWRIRQ_PWRBTN, NULL);
143         input_unregister_device(powerbutton_dev);
144         input_free_device(powerbutton_dev);
145
146 }
147
148 module_init(twl4030_pwrbutton_init);
149 module_exit(twl4030_pwrbutton_exit);
150
151 MODULE_ALIAS("i2c:twl4030-pwrbutton");
152 MODULE_DESCRIPTION("Triton2 Power Button");
153 MODULE_LICENSE("GPL");
154 MODULE_AUTHOR("Peter De Schrijver");
155