]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/keyboard/tsc2301_kp.c
da14be50d498f6b965e6072cd91a2f152db75446
[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_POLL_TIME               30
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         u16                     state;
68         u16                     data;
69         u16                     mask;
70
71         int                     irq;
72         s16                     keymap[16];
73
74         int                     (*get_keyb_irq_state)(struct device *dev);
75 };
76
77 static inline int tsc2301_kp_disabled(struct tsc2301 *tsc)
78 {
79         return tsc->kp->disable_depth != 0;
80 }
81
82 static inline void tsc2301_kp_set_keypressed_state(struct tsc2301 *tsc)
83 {
84         struct tsc2301_kp *kp = tsc->kp;
85
86         /* kp->state is updated only if we don't have the callback */
87         if (kp->get_keyb_irq_state != NULL) {
88                 if (kp->get_keyb_irq_state(&tsc->spi->dev))
89                         kp->state = 1 << 15;
90                 else
91                         kp->state = 0;
92         }
93 }
94
95 static inline int tsc2301_kp_key_is_pressed(struct tsc2301 *tsc)
96 {
97         return tsc->kp->state & (1 << 15);
98 }
99
100 static void tsc2301_kp_send_key_events(struct tsc2301 *tsc,
101                                        u16 prev_state,
102                                        u16 new_state)
103 {
104         struct tsc2301_kp *kp = tsc->kp;
105         u16 common, released, pressed;
106         int i;
107
108         common = prev_state & new_state;
109         released = common ^ prev_state;
110         pressed = common ^ new_state;
111         if (!released && !pressed)
112                 return;
113         for (i = 0; i < 16 && (released || pressed); i++) {
114                 if (released & 1) {
115                         dev_dbg(&tsc->spi->dev, "key %d released\n", i);
116                         input_report_key(kp->idev, kp->keymap[i], 0);
117                 }
118                 released >>= 1;
119                 if (pressed & 1) {
120                         dev_dbg(&tsc->spi->dev, "key %d pressed\n", i);
121                         input_report_key(kp->idev, kp->keymap[i], 1);
122                 }
123                 pressed >>= 1;
124         }
125         input_sync(kp->idev);
126 }
127
128 static inline void tsc2301_kp_release_all_keys(struct tsc2301 *tsc)
129 {
130         struct tsc2301_kp *kp = tsc->kp;
131         unsigned long flags;
132         int keys_pressed;
133
134         spin_lock_irqsave(&kp->lock, flags);
135         keys_pressed = kp->keys_pressed;
136         kp->keys_pressed = 0;
137         spin_unlock_irqrestore(&kp->lock, flags);
138         if (keys_pressed)
139                 tsc2301_kp_send_key_events(tsc, keys_pressed, 0);
140 }
141
142 static inline void _filter_out(struct tsc2301 *tsc, u16 prev_state,
143                                u16 *new_state, int row1, int row2, u8 rect_pat)
144 {
145         u16 mask;
146
147         mask = (rect_pat << (row1 * 4)) | (rect_pat << (row2 * 4));
148         mask &= ~prev_state;
149         *new_state &= ~mask;
150         dev_dbg(&tsc->spi->dev, "filtering ghost keys %02x\n", mask);
151 }
152
153 static void tsc2301_filter_ghost_keys(struct tsc2301 *tsc, u16 prev_state,
154                                       u16 *new_state)
155 {
156         int row1, row2;
157         u16 key_map;
158         u16 row1_map;
159         static const u8 rect_pat[] = {
160                 0x3, 0x5, 0x9, 0x6, 0xa, 0xc, 0,
161         };
162
163         key_map = *new_state;
164         for (row1 = 0; row1 < 4; row1++) {
165                 row1_map = (key_map >> (row1 * 4)) & 0xf;
166                 if (!row1_map)
167                         continue;
168                 for (row2 = row1 + 1; row2 < 4; row2++) {
169                         u16 rect_map = (key_map >> (row2 * 4)) & 0xf;
170                         const u8 *rp;
171
172                         rect_map &= row1_map;
173                         if (!rect_map)
174                                 continue;
175                         for (rp = rect_pat; *rp; rp++)
176                                 if ((rect_map & *rp) == *rp)
177                                         _filter_out(tsc, prev_state, new_state,
178                                                     row1, row2, *rp);
179                 }
180         }
181 }
182
183 static void tsc2301_kp_timer(unsigned long arg)
184 {
185         struct tsc2301 *tsc = (void *) arg;
186         int r;
187
188         /* This needs to be done early enough, since reading the key data
189          * register clears the IRQ line, which may be used to determine
190          * the key pressed state.
191          */
192         tsc2301_kp_set_keypressed_state(tsc);
193         r = spi_async(tsc->spi, &tsc->kp->read_msg);
194         if (unlikely(r < 0 && printk_ratelimit()))
195                 dev_err(&tsc->spi->dev, "kp: spi_async() failed");
196 }
197
198 static void tsc2301_kp_rx(void *arg)
199 {
200         struct tsc2301 *tsc = arg;
201         struct tsc2301_kp *kp = tsc->kp;
202         unsigned long flags;
203         int key_pressed;
204         u16 kp_data;
205
206         kp_data = kp->data;
207         key_pressed = tsc2301_kp_key_is_pressed(tsc);
208         dev_dbg(&tsc->spi->dev, "KP data %04x (%s)\n",
209                 kp_data, key_pressed ? "pressed" : "not pressed");
210         if (!key_pressed)
211                 kp_data = 0;
212         else
213                 tsc2301_filter_ghost_keys(tsc, kp->keys_pressed, &kp_data);
214         tsc2301_kp_send_key_events(tsc, kp->keys_pressed, kp_data);
215         kp->keys_pressed = kp_data;
216         spin_lock_irqsave(&kp->lock, flags);
217         if (likely(!tsc2301_kp_disabled(tsc))) {
218                 if (likely(key_pressed))
219                         mod_timer(&kp->timer,
220                                  jiffies + msecs_to_jiffies(TSC2301_POLL_TIME));
221                 else {
222                         kp->pending = 0;
223                         enable_irq(kp->irq);
224                 }
225         } else
226                 kp->pending = 0;
227         spin_unlock_irqrestore(&kp->lock, flags);
228 }
229
230 static irqreturn_t tsc2301_kp_irq_handler(int irq, void *dev_id)
231 {
232         struct tsc2301 *tsc = dev_id;
233         struct tsc2301_kp *kp = tsc->kp;
234         unsigned long flags;
235
236         spin_lock_irqsave(&kp->lock, flags);
237         BUG_ON(kp->pending);
238         if (tsc2301_kp_disabled(tsc)) {
239                 spin_unlock_irqrestore(&kp->lock, flags);
240                 return IRQ_HANDLED;
241         }
242         kp->pending = 1;
243         disable_irq_nosync(irq);
244         spin_unlock_irqrestore(&kp->lock, flags);
245         tsc2301_kp_timer((unsigned long) tsc);
246         return IRQ_HANDLED;
247 }
248
249 static void tsc2301_kp_start_scan(struct tsc2301 *tsc)
250 {
251         tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, tsc->kp->mask);
252         tsc2301_write_reg(tsc, TSC2301_REG_KEY, TSC2301_DEBOUNCE_TIME);
253 }
254
255 static void tsc2301_kp_stop_scan(struct tsc2301 *tsc)
256 {
257         tsc2301_write_reg(tsc, TSC2301_REG_KEY, 1 << 14);
258         tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, 0xffff);
259 }
260
261 /* Must be called with the mutex held */
262 static void tsc2301_kp_enable(struct tsc2301 *tsc)
263 {
264         struct tsc2301_kp *kp = tsc->kp;
265         unsigned long flags;
266
267         spin_lock_irqsave(&kp->lock, flags);
268         BUG_ON(!tsc2301_kp_disabled(tsc));
269         if (--kp->disable_depth != 0) {
270                 spin_unlock_irqrestore(&kp->lock, flags);
271                 return;
272         }
273         if (kp->keys_pressed)
274                 kp->pending = 1;
275         spin_unlock_irqrestore(&kp->lock, flags);
276
277         set_irq_type(kp->irq, IRQT_FALLING);
278         tsc2301_kp_start_scan(tsc);
279         if (kp->pending)
280                 /* continue an interrupted polling */
281                 mod_timer(&kp->timer,
282                           jiffies + msecs_to_jiffies(TSC2301_POLL_TIME));
283         else
284                 enable_irq(kp->irq);
285 }
286
287 /* Must be called with the mutex held */
288 static int tsc2301_kp_disable(struct tsc2301 *tsc, int release_keys)
289 {
290         struct tsc2301_kp *kp = tsc->kp;
291         unsigned long flags;
292
293         spin_lock_irqsave(&kp->lock, flags);
294         if (kp->disable_depth++ != 0) {
295                 spin_unlock_irqrestore(&kp->lock, flags);
296                 goto out;
297         }
298         if (!kp->pending)
299                 disable_irq_nosync(kp->irq);
300
301         while (kp->pending) {
302                 spin_unlock_irqrestore(&kp->lock, flags);
303                 msleep(1);
304                 spin_lock_irqsave(&kp->lock, flags);
305         }
306         spin_unlock_irqrestore(&kp->lock, flags);
307
308         tsc2301_kp_stop_scan(tsc);
309         set_irq_type(kp->irq, IRQT_NOEDGE);
310 out:
311         if (release_keys)
312                 tsc2301_kp_release_all_keys(tsc);
313
314         return 0;
315 }
316
317 /* The following workaround is needed for a HW bug triggered by the
318  * following:
319  * 1. keep any key pressed
320  * 2. disable keypad
321  * 3. release all keys
322  * 4. reenable keypad
323  * 5. disable touch screen controller
324  *
325  * After this the keypad scanner will get stuck in busy state and won't
326  * report any interrupts for further keypresses. One way to recover is to
327  * restart the keypad scanner whenever we enable / disable the
328  * touchscreen controller.
329  */
330 void tsc2301_kp_restart(struct tsc2301 *tsc)
331 {
332         struct tsc2301_kp *kp = tsc->kp;
333
334         mutex_lock(&kp->mutex);
335         if (!kp->user_disabled) {
336                 tsc2301_kp_disable(tsc, 0);
337                 tsc2301_kp_enable(tsc);
338         }
339         mutex_unlock(&kp->mutex);
340 }
341
342 static ssize_t tsc2301_kp_disable_show(struct device *dev,
343                                        struct device_attribute *attr, char *buf)
344 {
345         struct tsc2301          *tsc = dev_get_drvdata(dev);
346
347         return sprintf(buf, "%u\n", tsc2301_kp_disabled(tsc) ? 1 : 0);
348 }
349
350 static ssize_t tsc2301_kp_disable_store(struct device *dev,
351                                         struct device_attribute *attr,
352                                         const char *buf, size_t count)
353 {
354         struct tsc2301          *tsc = dev_get_drvdata(dev);
355         struct tsc2301_kp       *kp = tsc->kp;
356         char *endp;
357         int i;
358
359         i = simple_strtoul(buf, &endp, 10);
360         i = i ? 1 : 0;
361
362         mutex_lock(&kp->mutex);
363         if (i == kp->user_disabled) {
364                 mutex_unlock(&kp->mutex);
365                 return count;
366         }
367         kp->user_disabled = i;
368
369         if (i)
370                 tsc2301_kp_disable(tsc, 1);
371         else
372                 tsc2301_kp_enable(tsc);
373         mutex_unlock(&kp->mutex);
374
375         return count;
376 }
377
378 static DEVICE_ATTR(disable_kp, 0664, tsc2301_kp_disable_show,
379                    tsc2301_kp_disable_store);
380
381 static const u16 tsc2301_kp_read_data = 0x8000 | TSC2301_REG_KPDATA;
382 static const u16 tsc2301_kp_read_state = 0x8000 | TSC2301_REG_KEY;
383
384 static void tsc2301_kp_setup_spi_xfer(struct tsc2301 *tsc)
385 {
386         struct tsc2301_kp *kp = tsc->kp;
387         struct spi_message *m = &kp->read_msg;
388         struct spi_transfer *x = &kp->read_xfer[0];
389
390         spi_message_init(&kp->read_msg);
391
392         if (kp->get_keyb_irq_state == NULL) {
393                 /* No platform specific way for determining the keypress
394                  * state, so we'll need an extra status register read.
395                  */
396                 x->tx_buf = &tsc2301_kp_read_state;
397                 x->len = 2;
398                 spi_message_add_tail(x, m);
399                 x++;
400
401                 x->rx_buf = &kp->state;
402                 x->len = 2;
403                 x->cs_change = 1;
404                 spi_message_add_tail(x, m);
405                 x++;
406         }
407
408         x->tx_buf = &tsc2301_kp_read_data;
409         x->len = 2;
410         spi_message_add_tail(x, m);
411         x++;
412
413         x->rx_buf = &kp->data;
414         x->len = 2;
415         spi_message_add_tail(x, m);
416
417         m->complete = tsc2301_kp_rx;
418         m->context = tsc;
419 }
420
421 #ifdef CONFIG_PM
422 int tsc2301_kp_suspend(struct tsc2301 *tsc)
423 {
424         struct tsc2301_kp *kp = tsc->kp;
425
426         mutex_lock(&kp->mutex);
427         tsc2301_kp_disable(tsc, 1);
428         mutex_unlock(&kp->mutex);
429         return 0;
430 }
431
432 void tsc2301_kp_resume(struct tsc2301 *tsc)
433 {
434         struct tsc2301_kp *kp = tsc->kp;
435
436         mutex_lock(&kp->mutex);
437         tsc2301_kp_enable(tsc);
438         mutex_unlock(&kp->mutex);
439 }
440 #endif
441
442 int __devinit tsc2301_kp_init(struct tsc2301 *tsc,
443                               struct tsc2301_platform_data *pdata)
444 {
445         struct input_dev *idev;
446         struct tsc2301_kp *kp;
447         int r, i;
448         u16 mask;
449
450         if (pdata->keyb_int < 0) {
451                 dev_err(&tsc->spi->dev, "need kbirq");
452                 return -EINVAL;
453         }
454
455         kp = kzalloc(sizeof(*kp), GFP_KERNEL);
456         if (kp == NULL)
457                 return -ENOMEM;
458         tsc->kp = kp;
459
460         kp->irq = pdata->keyb_int;
461         spin_lock_init(&kp->lock);
462         mutex_init(&kp->mutex);
463
464         init_timer(&kp->timer);
465         kp->timer.data = (unsigned long) tsc;
466         kp->timer.function = tsc2301_kp_timer;
467
468         kp->get_keyb_irq_state = pdata->get_keyb_irq_state;
469
470         idev = input_allocate_device();
471         if (idev == NULL) {
472                 r = -ENOMEM;
473                 goto err1;
474         }
475         idev->name = "TSC2301 keypad";
476         snprintf(kp->phys, sizeof(kp->phys), "%s/input-kp", tsc->spi->dev.bus_id);
477         idev->phys = kp->phys;
478
479         mask = 0;
480         idev->evbit[0] = BIT(EV_KEY);
481         for (i = 0; i < 16; i++) {
482                 if (pdata->keymap[i] > 0) {
483                         set_bit(pdata->keymap[i], idev->keybit);
484                         kp->keymap[i] = pdata->keymap[i];
485                 } else {
486                         kp->keymap[i] = -1;
487                         mask |= 1 << i;
488                 }
489         }
490
491         if (pdata->kp_rep)
492                 set_bit(EV_REP, idev->evbit);
493
494         kp->idev = idev;
495
496         tsc2301_kp_setup_spi_xfer(tsc);
497
498         r = device_create_file(&tsc->spi->dev, &dev_attr_disable_kp);
499         if (r < 0)
500                 goto err2;
501
502         tsc2301_kp_start_scan(tsc);
503
504         /* IRQ mode 0 is faulty, it can cause the KBIRQ to get stuck.
505          * Mode 2 deasserts the IRQ at:
506          * - HW or SW reset
507          * - Setting SCS flag in REG_KEY register
508          * - Releasing all keys
509          * - Reading the REG_KPDATA
510          */
511         tsc2301_write_kbc(tsc, 2);
512
513         tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, mask);
514         kp->mask = mask;
515
516         set_irq_type(kp->irq, IRQT_FALLING);
517
518         r = request_irq(kp->irq, tsc2301_kp_irq_handler, SA_SAMPLE_RANDOM,
519                         "tsc2301-kp", tsc);
520         if (r < 0) {
521                 dev_err(&tsc->spi->dev, "unable to get kbirq IRQ");
522                 goto err3;
523         }
524         set_irq_wake(kp->irq, 1);
525
526         /* We need to read the register once..? */
527         tsc2301_read_reg(tsc, TSC2301_REG_KPDATA);
528
529         r = input_register_device(idev);
530         if (r < 0) {
531                 dev_err(&tsc->spi->dev, "can't register keypad device\n");
532                 goto err4;
533         }
534
535         return 0;
536
537 err4:
538         free_irq(kp->irq, tsc);
539 err3:
540         tsc2301_kp_stop_scan(tsc);
541         device_remove_file(&tsc->spi->dev, &dev_attr_disable_kp);
542 err2:
543         input_free_device(kp->idev);
544 err1:
545         kfree(kp);
546         return r;
547 }
548
549 void __devexit tsc2301_kp_exit(struct tsc2301 *tsc)
550 {
551         struct tsc2301_kp *kp = tsc->kp;
552
553         tsc2301_kp_disable(tsc, 1);
554         input_unregister_device(kp->idev);
555         free_irq(kp->irq, tsc);
556         device_remove_file(&tsc->spi->dev, &dev_attr_disable_kp);
557
558         kfree(kp);
559 }