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