2 * Copyright (C) 2011 Philippe Rétornaz
4 * Based on twl4030-pwrbutton driver by:
5 * Peter De Schrijver <peter.de-schrijver@nokia.com>
6 * Felipe Balbi <felipe.balbi@nokia.com>
8 * This file is subject to the terms and conditions of the GNU General
9 * Public License. See the file "COPYING" in the main directory of this
10 * archive for more details.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/input.h>
27 #include <linux/interrupt.h>
28 #include <linux/platform_device.h>
29 #include <linux/mfd/mc13783.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
34 struct input_dev
*pwr
;
35 struct mc13xxx
*mc13783
;
36 #define MC13783_PWRB_B1_POL_INVERT (1 << 0)
37 #define MC13783_PWRB_B2_POL_INVERT (1 << 1)
38 #define MC13783_PWRB_B3_POL_INVERT (1 << 2)
40 unsigned short keymap
[3];
43 #define MC13783_REG_INTERRUPT_SENSE_1 5
44 #define MC13783_IRQSENSE1_ONOFD1S (1 << 3)
45 #define MC13783_IRQSENSE1_ONOFD2S (1 << 4)
46 #define MC13783_IRQSENSE1_ONOFD3S (1 << 5)
48 #define MC13783_REG_POWER_CONTROL_2 15
49 #define MC13783_POWER_CONTROL_2_ON1BDBNC 4
50 #define MC13783_POWER_CONTROL_2_ON2BDBNC 6
51 #define MC13783_POWER_CONTROL_2_ON3BDBNC 8
52 #define MC13783_POWER_CONTROL_2_ON1BRSTEN (1 << 1)
53 #define MC13783_POWER_CONTROL_2_ON2BRSTEN (1 << 2)
54 #define MC13783_POWER_CONTROL_2_ON3BRSTEN (1 << 3)
56 static irqreturn_t
button_irq(int irq
, void *_priv
)
58 struct mc13783_pwrb
*priv
= _priv
;
61 mc13xxx_irq_ack(priv
->mc13783
, irq
);
62 mc13xxx_reg_read(priv
->mc13783
, MC13783_REG_INTERRUPT_SENSE_1
, &val
);
65 case MC13783_IRQ_ONOFD1
:
66 val
= val
& MC13783_IRQSENSE1_ONOFD1S
? 1 : 0;
67 if (priv
->flags
& MC13783_PWRB_B1_POL_INVERT
)
69 input_report_key(priv
->pwr
, priv
->keymap
[0], val
);
72 case MC13783_IRQ_ONOFD2
:
73 val
= val
& MC13783_IRQSENSE1_ONOFD2S
? 1 : 0;
74 if (priv
->flags
& MC13783_PWRB_B2_POL_INVERT
)
76 input_report_key(priv
->pwr
, priv
->keymap
[1], val
);
79 case MC13783_IRQ_ONOFD3
:
80 val
= val
& MC13783_IRQSENSE1_ONOFD3S
? 1 : 0;
81 if (priv
->flags
& MC13783_PWRB_B3_POL_INVERT
)
83 input_report_key(priv
->pwr
, priv
->keymap
[2], val
);
87 input_sync(priv
->pwr
);
92 static int mc13783_pwrbutton_probe(struct platform_device
*pdev
)
94 const struct mc13xxx_buttons_platform_data
*pdata
;
95 struct mc13xxx
*mc13783
= dev_get_drvdata(pdev
->dev
.parent
);
96 struct input_dev
*pwr
;
97 struct mc13783_pwrb
*priv
;
101 pdata
= dev_get_platdata(&pdev
->dev
);
103 dev_err(&pdev
->dev
, "missing platform data\n");
107 pwr
= input_allocate_device();
109 dev_dbg(&pdev
->dev
, "Can't allocate power button\n");
113 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
116 dev_dbg(&pdev
->dev
, "Can't allocate power button\n");
120 reg
|= (pdata
->b1on_flags
& 0x3) << MC13783_POWER_CONTROL_2_ON1BDBNC
;
121 reg
|= (pdata
->b2on_flags
& 0x3) << MC13783_POWER_CONTROL_2_ON2BDBNC
;
122 reg
|= (pdata
->b3on_flags
& 0x3) << MC13783_POWER_CONTROL_2_ON3BDBNC
;
125 priv
->mc13783
= mc13783
;
127 mc13xxx_lock(mc13783
);
129 if (pdata
->b1on_flags
& MC13783_BUTTON_ENABLE
) {
130 priv
->keymap
[0] = pdata
->b1on_key
;
131 if (pdata
->b1on_key
!= KEY_RESERVED
)
132 __set_bit(pdata
->b1on_key
, pwr
->keybit
);
134 if (pdata
->b1on_flags
& MC13783_BUTTON_POL_INVERT
)
135 priv
->flags
|= MC13783_PWRB_B1_POL_INVERT
;
137 if (pdata
->b1on_flags
& MC13783_BUTTON_RESET_EN
)
138 reg
|= MC13783_POWER_CONTROL_2_ON1BRSTEN
;
140 err
= mc13xxx_irq_request(mc13783
, MC13783_IRQ_ONOFD1
,
141 button_irq
, "b1on", priv
);
143 dev_dbg(&pdev
->dev
, "Can't request irq\n");
148 if (pdata
->b2on_flags
& MC13783_BUTTON_ENABLE
) {
149 priv
->keymap
[1] = pdata
->b2on_key
;
150 if (pdata
->b2on_key
!= KEY_RESERVED
)
151 __set_bit(pdata
->b2on_key
, pwr
->keybit
);
153 if (pdata
->b2on_flags
& MC13783_BUTTON_POL_INVERT
)
154 priv
->flags
|= MC13783_PWRB_B2_POL_INVERT
;
156 if (pdata
->b2on_flags
& MC13783_BUTTON_RESET_EN
)
157 reg
|= MC13783_POWER_CONTROL_2_ON2BRSTEN
;
159 err
= mc13xxx_irq_request(mc13783
, MC13783_IRQ_ONOFD2
,
160 button_irq
, "b2on", priv
);
162 dev_dbg(&pdev
->dev
, "Can't request irq\n");
167 if (pdata
->b3on_flags
& MC13783_BUTTON_ENABLE
) {
168 priv
->keymap
[2] = pdata
->b3on_key
;
169 if (pdata
->b3on_key
!= KEY_RESERVED
)
170 __set_bit(pdata
->b3on_key
, pwr
->keybit
);
172 if (pdata
->b3on_flags
& MC13783_BUTTON_POL_INVERT
)
173 priv
->flags
|= MC13783_PWRB_B3_POL_INVERT
;
175 if (pdata
->b3on_flags
& MC13783_BUTTON_RESET_EN
)
176 reg
|= MC13783_POWER_CONTROL_2_ON3BRSTEN
;
178 err
= mc13xxx_irq_request(mc13783
, MC13783_IRQ_ONOFD3
,
179 button_irq
, "b3on", priv
);
181 dev_dbg(&pdev
->dev
, "Can't request irq: %d\n", err
);
186 mc13xxx_reg_rmw(mc13783
, MC13783_REG_POWER_CONTROL_2
, 0x3FE, reg
);
188 mc13xxx_unlock(mc13783
);
190 pwr
->name
= "mc13783_pwrbutton";
191 pwr
->phys
= "mc13783_pwrbutton/input0";
192 pwr
->dev
.parent
= &pdev
->dev
;
194 pwr
->keycode
= priv
->keymap
;
195 pwr
->keycodemax
= ARRAY_SIZE(priv
->keymap
);
196 pwr
->keycodesize
= sizeof(priv
->keymap
[0]);
197 __set_bit(EV_KEY
, pwr
->evbit
);
199 err
= input_register_device(pwr
);
201 dev_dbg(&pdev
->dev
, "Can't register power button: %d\n", err
);
205 platform_set_drvdata(pdev
, priv
);
210 mc13xxx_lock(mc13783
);
212 if (pdata
->b3on_flags
& MC13783_BUTTON_ENABLE
)
213 mc13xxx_irq_free(mc13783
, MC13783_IRQ_ONOFD3
, priv
);
216 if (pdata
->b2on_flags
& MC13783_BUTTON_ENABLE
)
217 mc13xxx_irq_free(mc13783
, MC13783_IRQ_ONOFD2
, priv
);
220 if (pdata
->b1on_flags
& MC13783_BUTTON_ENABLE
)
221 mc13xxx_irq_free(mc13783
, MC13783_IRQ_ONOFD1
, priv
);
224 mc13xxx_unlock(mc13783
);
228 input_free_device(pwr
);
233 static int mc13783_pwrbutton_remove(struct platform_device
*pdev
)
235 struct mc13783_pwrb
*priv
= platform_get_drvdata(pdev
);
236 const struct mc13xxx_buttons_platform_data
*pdata
;
238 pdata
= dev_get_platdata(&pdev
->dev
);
240 mc13xxx_lock(priv
->mc13783
);
242 if (pdata
->b3on_flags
& MC13783_BUTTON_ENABLE
)
243 mc13xxx_irq_free(priv
->mc13783
, MC13783_IRQ_ONOFD3
, priv
);
244 if (pdata
->b2on_flags
& MC13783_BUTTON_ENABLE
)
245 mc13xxx_irq_free(priv
->mc13783
, MC13783_IRQ_ONOFD2
, priv
);
246 if (pdata
->b1on_flags
& MC13783_BUTTON_ENABLE
)
247 mc13xxx_irq_free(priv
->mc13783
, MC13783_IRQ_ONOFD1
, priv
);
249 mc13xxx_unlock(priv
->mc13783
);
251 input_unregister_device(priv
->pwr
);
257 static struct platform_driver mc13783_pwrbutton_driver
= {
258 .probe
= mc13783_pwrbutton_probe
,
259 .remove
= mc13783_pwrbutton_remove
,
261 .name
= "mc13783-pwrbutton",
262 .owner
= THIS_MODULE
,
266 module_platform_driver(mc13783_pwrbutton_driver
);
268 MODULE_ALIAS("platform:mc13783-pwrbutton");
269 MODULE_DESCRIPTION("MC13783 Power Button");
270 MODULE_LICENSE("GPL v2");
271 MODULE_AUTHOR("Philippe Retornaz");