]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/keyboard/tsc2301_kp.c
tsc2301 - correct the handling of the keyb_int interrupt
[linux-2.6-omap-h63xx.git] / drivers / input / keyboard / tsc2301_kp.c
1 /*
2  * TSC2301 keypad driver
3  *
4  * Copyright (C) 2005-2006 Nokia Corporation
5  *
6  * Written by Jarkko Oikarinen
7  * Rewritten by Juha Yrjola <juha.yrjola@nokia.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
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
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/input.h>
28 #include <linux/interrupt.h>
29 #include <linux/irq.h>
30 #include <linux/delay.h>
31 #include <linux/spi/spi.h>
32
33 #ifdef CONFIG_ARCH_OMAP
34 #include <asm/arch/gpio.h>
35 #endif
36
37 #include <linux/spi/tsc2301.h>
38
39 #define TSC2301_KEYBOARD_PRODUCT_ID      0x0051
40 #define TSC2301_KEYBOARD_PRODUCT_VERSION 0x0001
41 #define TSC2301_DEBOUNCE_TIME_2MS        0x0000
42 #define TSC2301_DEBOUNCE_TIME_10MS       0x0800
43 #define TSC2301_DEBOUNCE_TIME_20MS       0x1000
44 #define TSC2301_DEBOUNCE_TIME_50MS       0x1800
45 #define TSC2301_DEBOUNCE_TIME_60MS       0x2000
46 #define TSC2301_DEBOUNCE_TIME_80MS       0x2800
47 #define TSC2301_DEBOUNCE_TIME_100MS      0x3000
48 #define TSC2301_DEBOUNCE_TIME_120MS      0x3800
49
50 #define TSC2301_DEBOUNCE_TIME           TSC2301_DEBOUNCE_TIME_20MS
51
52 #define TSC2301_RELEASE_TIMEOUT         50
53
54 struct tsc2301_kp {
55         struct input_dev        *idev;
56         char                    phys[32];
57         spinlock_t              lock;
58         struct mutex            mutex;
59         struct timer_list       timer;
60         u16                     keys_pressed;
61         unsigned                pending:1;
62         unsigned                user_disabled:1;
63         unsigned                disable_depth;
64
65         struct spi_transfer     read_xfer[4];
66         struct spi_message      read_msg;
67
68         u16                     data;
69         u16                     mask;
70
71         int                     irq;
72         s16                     keymap[16];
73 };
74
75 static inline int tsc2301_kp_disabled(struct tsc2301 *tsc)
76 {
77         return tsc->kp->disable_depth != 0;
78 }
79
80 static void tsc2301_kp_send_key_events(struct tsc2301 *tsc,
81                                        u16 prev_state,
82                                        u16 new_state)
83 {
84         struct tsc2301_kp *kp = tsc->kp;
85         u16 common, released, pressed;
86         int i;
87
88         common = prev_state & new_state;
89         released = common ^ prev_state;
90         pressed = common ^ new_state;
91         if (!released && !pressed)
92                 return;
93         for (i = 0; i < 16 && (released || pressed); i++) {
94                 if (released & 1) {
95                         dev_dbg(&tsc->spi->dev, "key %d released\n", i);
96                         input_report_key(kp->idev, kp->keymap[i], 0);
97                 }
98                 released >>= 1;
99                 if (pressed & 1) {
100                         dev_dbg(&tsc->spi->dev, "key %d pressed\n", i);
101                         input_report_key(kp->idev, kp->keymap[i], 1);
102                 }
103                 pressed >>= 1;
104         }
105         input_sync(kp->idev);
106 }
107
108 static inline void _filter_out(struct tsc2301 *tsc, u16 prev_state,
109                                u16 *new_state, int row1, int row2, u8 rect_pat)
110 {
111         u16 mask;
112
113         mask = (rect_pat << (row1 * 4)) | (rect_pat << (row2 * 4));
114         mask &= ~prev_state;
115         *new_state &= ~mask;
116         dev_dbg(&tsc->spi->dev, "filtering ghost keys %02x\n", mask);
117 }
118
119 static void tsc2301_filter_ghost_keys(struct tsc2301 *tsc, u16 prev_state,
120                                       u16 *new_state)
121 {
122         int row1, row2;
123         u16 key_map;
124         u16 row1_map;
125         static const u8 rect_pat[] = {
126                 0x3, 0x5, 0x9, 0x6, 0xa, 0xc, 0,
127         };
128
129         key_map = *new_state;
130         for (row1 = 0; row1 < 4; row1++) {
131                 row1_map = (key_map >> (row1 * 4)) & 0xf;
132                 if (!row1_map)
133                         continue;
134                 for (row2 = row1 + 1; row2 < 4; row2++) {
135                         u16 rect_map = (key_map >> (row2 * 4)) & 0xf;
136                         const u8 *rp;
137
138                         rect_map &= row1_map;
139                         if (!rect_map)
140                                 continue;
141                         for (rp = rect_pat; *rp; rp++)
142                                 if ((rect_map & *rp) == *rp)
143                                         _filter_out(tsc, prev_state, new_state,
144                                                     row1, row2, *rp);
145                 }
146         }
147 }
148
149 static void tsc2301_kp_timer(unsigned long arg)
150 {
151         struct tsc2301 *tsc = (void *) arg;
152         struct tsc2301_kp *kp = tsc->kp;
153         unsigned long flags;
154
155         tsc2301_kp_send_key_events(tsc, kp->keys_pressed, 0);
156         spin_lock_irqsave(&kp->lock, flags);
157         kp->keys_pressed = 0;
158         spin_unlock_irqrestore(&kp->lock, flags);
159 }
160
161 static void tsc2301_kp_rx(void *arg)
162 {
163         struct tsc2301 *tsc = arg;
164         struct tsc2301_kp *kp = tsc->kp;
165         unsigned long flags;
166         u16 kp_data;
167
168         kp_data = kp->data;
169         dev_dbg(&tsc->spi->dev, "KP data %04x\n", kp_data);
170
171         tsc2301_filter_ghost_keys(tsc, kp->keys_pressed, &kp_data);
172         tsc2301_kp_send_key_events(tsc, kp->keys_pressed, kp_data);
173         spin_lock_irqsave(&kp->lock, flags);
174         kp->keys_pressed = kp_data;
175         kp->pending = 0;
176         spin_unlock_irqrestore(&kp->lock, flags);
177 }
178
179 static irqreturn_t tsc2301_kp_irq_handler(int irq, void *dev_id)
180 {
181         struct tsc2301 *tsc = dev_id;
182         struct tsc2301_kp *kp = tsc->kp;
183         unsigned long flags;
184         int r;
185
186         spin_lock_irqsave(&kp->lock, flags);
187         if (tsc2301_kp_disabled(tsc)) {
188                 spin_unlock_irqrestore(&kp->lock, flags);
189                 return IRQ_HANDLED;
190         }
191         kp->pending = 1;
192         spin_unlock_irqrestore(&kp->lock, flags);
193         mod_timer(&kp->timer,
194                  jiffies + msecs_to_jiffies(TSC2301_RELEASE_TIMEOUT));
195         r = spi_async(tsc->spi, &tsc->kp->read_msg);
196         if (r)
197                 dev_err(&tsc->spi->dev, "kp: spi_async() failed");
198         return IRQ_HANDLED;
199 }
200
201 static void tsc2301_kp_start_scan(struct tsc2301 *tsc)
202 {
203         tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, tsc->kp->mask);
204         tsc2301_write_reg(tsc, TSC2301_REG_KEY, TSC2301_DEBOUNCE_TIME);
205 }
206
207 static void tsc2301_kp_stop_scan(struct tsc2301 *tsc)
208 {
209         tsc2301_write_reg(tsc, TSC2301_REG_KEY, 1 << 14);
210 }
211
212 /* Must be called with the mutex held */
213 static void tsc2301_kp_enable(struct tsc2301 *tsc)
214 {
215         struct tsc2301_kp *kp = tsc->kp;
216         unsigned long flags;
217
218         spin_lock_irqsave(&kp->lock, flags);
219         BUG_ON(!tsc2301_kp_disabled(tsc));
220         if (--kp->disable_depth != 0) {
221                 spin_unlock_irqrestore(&kp->lock, flags);
222                 return;
223         }
224         spin_unlock_irqrestore(&kp->lock, flags);
225
226         set_irq_type(kp->irq, IRQT_FALLING);
227         tsc2301_kp_start_scan(tsc);
228         enable_irq(kp->irq);
229 }
230
231 /* Must be called with the mutex held */
232 static int tsc2301_kp_disable(struct tsc2301 *tsc, int release_keys)
233 {
234         struct tsc2301_kp *kp = tsc->kp;
235         unsigned long flags;
236
237         spin_lock_irqsave(&kp->lock, flags);
238         if (kp->disable_depth++ != 0) {
239                 spin_unlock_irqrestore(&kp->lock, flags);
240                 goto out;
241         }
242         disable_irq_nosync(kp->irq);
243         set_irq_type(kp->irq, IRQT_NOEDGE);
244         spin_unlock_irqrestore(&kp->lock, flags);
245
246         while (kp->pending) {
247                 msleep(1);
248         }
249
250         tsc2301_kp_stop_scan(tsc);
251 out:
252         if (!release_keys)
253                 del_timer(&kp->timer); /* let timeout release keys */
254
255         return 0;
256 }
257
258 /* The following workaround is needed for a HW bug triggered by the
259  * following:
260  * 1. keep any key pressed
261  * 2. disable keypad
262  * 3. release all keys
263  * 4. reenable keypad
264  * 5. disable touch screen controller
265  *
266  * After this the keypad scanner will get stuck in busy state and won't
267  * report any interrupts for further keypresses. One way to recover is to
268  * restart the keypad scanner whenever we enable / disable the
269  * touchscreen controller.
270  */
271 void tsc2301_kp_restart(struct tsc2301 *tsc)
272 {
273         struct tsc2301_kp *kp = tsc->kp;
274
275         mutex_lock(&kp->mutex);
276         if (!kp->user_disabled) {
277                 tsc2301_kp_disable(tsc, 0);
278                 tsc2301_kp_enable(tsc);
279         }
280         mutex_unlock(&kp->mutex);
281 }
282
283 static ssize_t tsc2301_kp_disable_show(struct device *dev,
284                                        struct device_attribute *attr, char *buf)
285 {
286         struct tsc2301          *tsc = dev_get_drvdata(dev);
287
288         return sprintf(buf, "%u\n", tsc2301_kp_disabled(tsc) ? 1 : 0);
289 }
290
291 static ssize_t tsc2301_kp_disable_store(struct device *dev,
292                                         struct device_attribute *attr,
293                                         const char *buf, size_t count)
294 {
295         struct tsc2301          *tsc = dev_get_drvdata(dev);
296         struct tsc2301_kp       *kp = tsc->kp;
297         char *endp;
298         int i;
299
300         i = simple_strtoul(buf, &endp, 10);
301         i = i ? 1 : 0;
302
303         mutex_lock(&kp->mutex);
304         if (i == kp->user_disabled) {
305                 mutex_unlock(&kp->mutex);
306                 return count;
307         }
308         kp->user_disabled = i;
309
310         if (i)
311                 tsc2301_kp_disable(tsc, 1);
312         else
313                 tsc2301_kp_enable(tsc);
314         mutex_unlock(&kp->mutex);
315
316         return count;
317 }
318
319 static DEVICE_ATTR(disable_kp, 0664, tsc2301_kp_disable_show,
320                    tsc2301_kp_disable_store);
321
322 static const u16 tsc2301_kp_read_data = 0x8000 | TSC2301_REG_KPDATA;
323
324 static void tsc2301_kp_setup_spi_xfer(struct tsc2301 *tsc)
325 {
326         struct tsc2301_kp *kp = tsc->kp;
327         struct spi_message *m = &kp->read_msg;
328         struct spi_transfer *x = &kp->read_xfer[0];
329
330         spi_message_init(&kp->read_msg);
331
332         x->tx_buf = &tsc2301_kp_read_data;
333         x->len = 2;
334         spi_message_add_tail(x, m);
335         x++;
336
337         x->rx_buf = &kp->data;
338         x->len = 2;
339         spi_message_add_tail(x, m);
340
341         m->complete = tsc2301_kp_rx;
342         m->context = tsc;
343 }
344
345 #ifdef CONFIG_PM
346 int tsc2301_kp_suspend(struct tsc2301 *tsc)
347 {
348         struct tsc2301_kp *kp = tsc->kp;
349
350         mutex_lock(&kp->mutex);
351         tsc2301_kp_disable(tsc, 1);
352         mutex_unlock(&kp->mutex);
353         return 0;
354 }
355
356 void tsc2301_kp_resume(struct tsc2301 *tsc)
357 {
358         struct tsc2301_kp *kp = tsc->kp;
359
360         mutex_lock(&kp->mutex);
361         tsc2301_kp_enable(tsc);
362         mutex_unlock(&kp->mutex);
363 }
364 #endif
365
366 int __devinit tsc2301_kp_init(struct tsc2301 *tsc,
367                               struct tsc2301_platform_data *pdata)
368 {
369         struct input_dev *idev;
370         struct tsc2301_kp *kp;
371         int r, i;
372         u16 mask;
373
374         if (pdata->keyb_int < 0) {
375                 dev_err(&tsc->spi->dev, "need kbirq");
376                 return -EINVAL;
377         }
378
379         kp = kzalloc(sizeof(*kp), GFP_KERNEL);
380         if (kp == NULL)
381                 return -ENOMEM;
382         tsc->kp = kp;
383
384         kp->irq = pdata->keyb_int;
385         spin_lock_init(&kp->lock);
386         mutex_init(&kp->mutex);
387
388         init_timer(&kp->timer);
389         kp->timer.data = (unsigned long) tsc;
390         kp->timer.function = tsc2301_kp_timer;
391
392         idev = input_allocate_device();
393         if (idev == NULL) {
394                 r = -ENOMEM;
395                 goto err1;
396         }
397         idev->name = "TSC2301 keypad";
398         snprintf(kp->phys, sizeof(kp->phys), "%s/input-kp", tsc->spi->dev.bus_id);
399         idev->phys = kp->phys;
400
401         mask = 0;
402         idev->evbit[0] = BIT(EV_KEY);
403         for (i = 0; i < 16; i++) {
404                 if (pdata->keymap[i] > 0) {
405                         set_bit(pdata->keymap[i], idev->keybit);
406                         kp->keymap[i] = pdata->keymap[i];
407                 } else {
408                         kp->keymap[i] = -1;
409                         mask |= 1 << i;
410                 }
411         }
412
413         if (pdata->kp_rep)
414                 set_bit(EV_REP, idev->evbit);
415
416         kp->idev = idev;
417
418         tsc2301_kp_setup_spi_xfer(tsc);
419
420         r = device_create_file(&tsc->spi->dev, &dev_attr_disable_kp);
421         if (r < 0)
422                 goto err2;
423
424         tsc2301_kp_start_scan(tsc);
425
426         /* IRQ mode 0 is faulty, it can cause the KBIRQ to get stuck.
427          * Mode 2 deasserts the IRQ at:
428          * - HW or SW reset
429          * - Setting SCS flag in REG_KEY register
430          * - Releasing all keys
431          * - Reading the REG_KPDATA
432          */
433         tsc2301_write_kbc(tsc, 2);
434
435         tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, mask);
436         kp->mask = mask;
437
438         set_irq_type(kp->irq, IRQT_FALLING);
439
440         r = request_irq(kp->irq, tsc2301_kp_irq_handler, IRQF_SAMPLE_RANDOM,
441                         "tsc2301-kp", tsc);
442         if (r < 0) {
443                 dev_err(&tsc->spi->dev, "unable to get kbirq IRQ");
444                 goto err3;
445         }
446         set_irq_wake(kp->irq, 1);
447
448         /* We need to read the register once..? */
449         tsc2301_read_reg(tsc, TSC2301_REG_KPDATA);
450
451         r = input_register_device(idev);
452         if (r < 0) {
453                 dev_err(&tsc->spi->dev, "can't register keypad device\n");
454                 goto err4;
455         }
456
457         return 0;
458
459 err4:
460         free_irq(kp->irq, tsc);
461 err3:
462         tsc2301_kp_stop_scan(tsc);
463         device_remove_file(&tsc->spi->dev, &dev_attr_disable_kp);
464 err2:
465         input_free_device(kp->idev);
466 err1:
467         kfree(kp);
468         return r;
469 }
470
471 void __devexit tsc2301_kp_exit(struct tsc2301 *tsc)
472 {
473         struct tsc2301_kp *kp = tsc->kp;
474
475         tsc2301_kp_disable(tsc, 1);
476         input_unregister_device(kp->idev);
477         free_irq(kp->irq, tsc);
478         device_remove_file(&tsc->spi->dev, &dev_attr_disable_kp);
479
480         kfree(kp);
481 }