spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / drivers / input / misc / mc13783-pwrbutton.c
blob8428f1e8e83ed4ef7c3d98d7e5ad7db7e920a7a5
1 /**
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>
33 struct mc13783_pwrb {
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)
39 int flags;
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;
59 int val;
61 mc13xxx_irq_ack(priv->mc13783, irq);
62 mc13xxx_reg_read(priv->mc13783, MC13783_REG_INTERRUPT_SENSE_1, &val);
64 switch (irq) {
65 case MC13783_IRQ_ONOFD1:
66 val = val & MC13783_IRQSENSE1_ONOFD1S ? 1 : 0;
67 if (priv->flags & MC13783_PWRB_B1_POL_INVERT)
68 val ^= 1;
69 input_report_key(priv->pwr, priv->keymap[0], val);
70 break;
72 case MC13783_IRQ_ONOFD2:
73 val = val & MC13783_IRQSENSE1_ONOFD2S ? 1 : 0;
74 if (priv->flags & MC13783_PWRB_B2_POL_INVERT)
75 val ^= 1;
76 input_report_key(priv->pwr, priv->keymap[1], val);
77 break;
79 case MC13783_IRQ_ONOFD3:
80 val = val & MC13783_IRQSENSE1_ONOFD3S ? 1 : 0;
81 if (priv->flags & MC13783_PWRB_B3_POL_INVERT)
82 val ^= 1;
83 input_report_key(priv->pwr, priv->keymap[2], val);
84 break;
87 input_sync(priv->pwr);
89 return IRQ_HANDLED;
92 static int __devinit 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;
98 int err = 0;
99 int reg = 0;
101 pdata = dev_get_platdata(&pdev->dev);
102 if (!pdata) {
103 dev_err(&pdev->dev, "missing platform data\n");
104 return -ENODEV;
107 pwr = input_allocate_device();
108 if (!pwr) {
109 dev_dbg(&pdev->dev, "Can't allocate power button\n");
110 return -ENOMEM;
113 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
114 if (!priv) {
115 err = -ENOMEM;
116 dev_dbg(&pdev->dev, "Can't allocate power button\n");
117 goto free_input_dev;
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;
124 priv->pwr = pwr;
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);
142 if (err) {
143 dev_dbg(&pdev->dev, "Can't request irq\n");
144 goto free_priv;
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);
161 if (err) {
162 dev_dbg(&pdev->dev, "Can't request irq\n");
163 goto free_irq_b1;
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);
180 if (err) {
181 dev_dbg(&pdev->dev, "Can't request irq: %d\n", err);
182 goto free_irq_b2;
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);
200 if (err) {
201 dev_dbg(&pdev->dev, "Can't register power button: %d\n", err);
202 goto free_irq;
205 platform_set_drvdata(pdev, priv);
207 return 0;
209 free_irq:
210 mc13xxx_lock(mc13783);
212 if (pdata->b3on_flags & MC13783_BUTTON_ENABLE)
213 mc13xxx_irq_free(mc13783, MC13783_IRQ_ONOFD3, priv);
215 free_irq_b2:
216 if (pdata->b2on_flags & MC13783_BUTTON_ENABLE)
217 mc13xxx_irq_free(mc13783, MC13783_IRQ_ONOFD2, priv);
219 free_irq_b1:
220 if (pdata->b1on_flags & MC13783_BUTTON_ENABLE)
221 mc13xxx_irq_free(mc13783, MC13783_IRQ_ONOFD1, priv);
223 free_priv:
224 mc13xxx_unlock(mc13783);
225 kfree(priv);
227 free_input_dev:
228 input_free_device(pwr);
230 return err;
233 static int __devexit 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);
252 kfree(priv);
253 platform_set_drvdata(pdev, NULL);
255 return 0;
258 static struct platform_driver mc13783_pwrbutton_driver = {
259 .probe = mc13783_pwrbutton_probe,
260 .remove = __devexit_p(mc13783_pwrbutton_remove),
261 .driver = {
262 .name = "mc13783-pwrbutton",
263 .owner = THIS_MODULE,
267 module_platform_driver(mc13783_pwrbutton_driver);
269 MODULE_ALIAS("platform:mc13783-pwrbutton");
270 MODULE_DESCRIPTION("MC13783 Power Button");
271 MODULE_LICENSE("GPL v2");
272 MODULE_AUTHOR("Philippe Retornaz");