omap24xx: Refresh 3.1 patches
[openwrt.git] / target / linux / omap24xx / patches-3.1 / 315-n800-touchscreen-and-keypad-drivers.patch
1 From 63e56392b9024aceb610d7b4e1979e2d2cebd217 Mon Sep 17 00:00:00 2001
2 From: Marat Radchenko <marat@slonopotamus.org>
3 Date: Tue, 18 Oct 2011 21:48:23 +0400
4 Subject: [PATCH 1/2] TSC2301 driver (touchscreen and keypad)
5
6 This patch adds support for TSC2301 touchscreen/keypad/audio chip found on Nokia N800.
7 Touchscreen and keypad are fully functional, audio part only provides power management.
8 ---
9  drivers/input/keyboard/Kconfig         |    6 +
10  drivers/input/keyboard/Makefile        |    1 +
11  drivers/input/keyboard/tsc2301_kp.c    |  475 +++++++++++++++
12  drivers/input/touchscreen/Kconfig      |    6 +
13  drivers/input/touchscreen/Makefile     |    1 +
14  drivers/input/touchscreen/tsc2301_ts.c |  676 +++++++++++++++++++++
15  drivers/spi/Kconfig                    |   22 +
16  drivers/spi/Makefile                   |    3 +
17  drivers/spi/tsc2301-core.c             |  297 ++++++++++
18  drivers/spi/tsc2301-mixer.c            | 1003 ++++++++++++++++++++++++++++++++
19  include/linux/spi/tsc2301.h            |  208 +++++++
20  11 files changed, 2698 insertions(+), 0 deletions(-)
21  create mode 100644 drivers/input/keyboard/tsc2301_kp.c
22  create mode 100644 drivers/input/touchscreen/tsc2301_ts.c
23  create mode 100644 drivers/spi/tsc2301-core.c
24  create mode 100644 drivers/spi/tsc2301-mixer.c
25  create mode 100644 include/linux/spi/tsc2301.h
26
27 Index: linux-3.1/drivers/input/keyboard/Kconfig
28 ===================================================================
29 --- linux-3.1.orig/drivers/input/keyboard/Kconfig       2011-10-30 00:48:29.357056963 +0200
30 +++ linux-3.1/drivers/input/keyboard/Kconfig    2011-10-30 00:48:48.989043470 +0200
31 @@ -530,6 +530,12 @@ config KEYBOARD_TNETV107X
32           To compile this driver as a module, choose M here: the
33           module will be called tnetv107x-keypad.
34  
35 +config KEYBOARD_TSC2301
36 +       tristate "TSC2301 keypad support"
37 +       depends on SPI_TSC2301
38 +       help
39 +         Say Y here for if you are using the keypad features of TSC2301.
40 +
41  config KEYBOARD_TWL4030
42         tristate "TI TWL4030/TWL5030/TPS659x0 keypad support"
43         depends on TWL4030_CORE
44 Index: linux-3.1/drivers/input/keyboard/Makefile
45 ===================================================================
46 --- linux-3.1.orig/drivers/input/keyboard/Makefile      2011-10-30 00:48:29.365056957 +0200
47 +++ linux-3.1/drivers/input/keyboard/Makefile   2011-10-30 00:48:48.989043470 +0200
48 @@ -48,6 +48,7 @@ obj-$(CONFIG_KEYBOARD_SUNKBD)         += sunkbd
49  obj-$(CONFIG_KEYBOARD_TC3589X)         += tc3589x-keypad.o
50  obj-$(CONFIG_KEYBOARD_TEGRA)           += tegra-kbc.o
51  obj-$(CONFIG_KEYBOARD_TNETV107X)       += tnetv107x-keypad.o
52 +obj-$(CONFIG_KEYBOARD_TSC2301)         += tsc2301_kp.o
53  obj-$(CONFIG_KEYBOARD_TWL4030)         += twl4030_keypad.o
54  obj-$(CONFIG_KEYBOARD_XTKBD)           += xtkbd.o
55  obj-$(CONFIG_KEYBOARD_W90P910)         += w90p910_keypad.o
56 Index: linux-3.1/drivers/input/keyboard/tsc2301_kp.c
57 ===================================================================
58 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
59 +++ linux-3.1/drivers/input/keyboard/tsc2301_kp.c       2011-10-30 00:48:48.989043470 +0200
60 @@ -0,0 +1,475 @@
61 +/*
62 + * TSC2301 keypad driver
63 + *
64 + * Copyright (C) 2005-2006 Nokia Corporation
65 + *
66 + * Written by Jarkko Oikarinen
67 + * Rewritten by Juha Yrjola <juha.yrjola@nokia.com>
68 + *
69 + * This program is free software; you can redistribute it and/or modify
70 + * it under the terms of the GNU General Public License as published by
71 + * the Free Software Foundation; either version 2 of the License, or
72 + * (at your option) any later version.
73 + *
74 + * This program is distributed in the hope that it will be useful,
75 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
76 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
77 + * GNU General Public License for more details.
78 + *
79 + * You should have received a copy of the GNU General Public License
80 + * along with this program; if not, write to the Free Software
81 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
82 + *
83 + */
84 +
85 +#include <linux/kernel.h>
86 +#include <linux/module.h>
87 +#include <linux/input.h>
88 +#include <linux/interrupt.h>
89 +#include <linux/irq.h>
90 +#include <linux/delay.h>
91 +#include <linux/spi/spi.h>
92 +
93 +#include <linux/spi/tsc2301.h>
94 +
95 +#define TSC2301_KEYBOARD_PRODUCT_ID      0x0051
96 +#define TSC2301_KEYBOARD_PRODUCT_VERSION 0x0001
97 +#define TSC2301_DEBOUNCE_TIME_2MS        0x0000
98 +#define TSC2301_DEBOUNCE_TIME_10MS       0x0800
99 +#define TSC2301_DEBOUNCE_TIME_20MS       0x1000
100 +#define TSC2301_DEBOUNCE_TIME_50MS       0x1800
101 +#define TSC2301_DEBOUNCE_TIME_60MS       0x2000
102 +#define TSC2301_DEBOUNCE_TIME_80MS       0x2800
103 +#define TSC2301_DEBOUNCE_TIME_100MS      0x3000
104 +#define TSC2301_DEBOUNCE_TIME_120MS      0x3800
105 +
106 +#define TSC2301_DEBOUNCE_TIME          TSC2301_DEBOUNCE_TIME_20MS
107 +
108 +#define TSC2301_RELEASE_TIMEOUT                50
109 +
110 +struct tsc2301_kp {
111 +       struct input_dev        *idev;
112 +       char                    phys[32];
113 +       spinlock_t              lock;
114 +       struct mutex            mutex;
115 +       struct timer_list       timer;
116 +       u16                     keys_pressed;
117 +       unsigned                pending:1;
118 +       unsigned                user_disabled:1;
119 +       unsigned                disable_depth;
120 +
121 +       struct spi_transfer     read_xfer[4];
122 +       struct spi_message      read_msg;
123 +
124 +       u16                     data;
125 +       u16                     mask;
126 +
127 +       int                     irq;
128 +       s16                     keymap[16];
129 +};
130 +
131 +static inline int tsc2301_kp_disabled(struct tsc2301 *tsc)
132 +{
133 +       return tsc->kp->disable_depth != 0;
134 +}
135 +
136 +static void tsc2301_kp_send_key_events(struct tsc2301 *tsc,
137 +                                      u16 prev_state,
138 +                                      u16 new_state)
139 +{
140 +       struct tsc2301_kp *kp = tsc->kp;
141 +       u16 common, released, pressed;
142 +       int i;
143 +
144 +       common = prev_state & new_state;
145 +       released = common ^ prev_state;
146 +       pressed = common ^ new_state;
147 +       if (!released && !pressed)
148 +               return;
149 +       for (i = 0; i < 16 && (released || pressed); i++) {
150 +               if (released & 1) {
151 +                       dev_dbg(&tsc->spi->dev, "key %d released\n", i);
152 +                       input_report_key(kp->idev, kp->keymap[i], 0);
153 +               }
154 +               released >>= 1;
155 +               if (pressed & 1) {
156 +                       dev_dbg(&tsc->spi->dev, "key %d pressed\n", i);
157 +                       input_report_key(kp->idev, kp->keymap[i], 1);
158 +               }
159 +               pressed >>= 1;
160 +       }
161 +       input_sync(kp->idev);
162 +}
163 +
164 +static inline void _filter_out(struct tsc2301 *tsc, u16 prev_state,
165 +                              u16 *new_state, int row1, int row2, u8 rect_pat)
166 +{
167 +       u16 mask;
168 +
169 +       mask = (rect_pat << (row1 * 4)) | (rect_pat << (row2 * 4));
170 +       mask &= ~prev_state;
171 +       *new_state &= ~mask;
172 +       dev_dbg(&tsc->spi->dev, "filtering ghost keys %02x\n", mask);
173 +}
174 +
175 +static void tsc2301_filter_ghost_keys(struct tsc2301 *tsc, u16 prev_state,
176 +                                     u16 *new_state)
177 +{
178 +       int row1, row2;
179 +       u16 key_map;
180 +       u16 row1_map;
181 +       static const u8 rect_pat[] = {
182 +               0x3, 0x5, 0x9, 0x6, 0xa, 0xc, 0,
183 +       };
184 +
185 +       key_map = *new_state;
186 +       for (row1 = 0; row1 < 4; row1++) {
187 +               row1_map = (key_map >> (row1 * 4)) & 0xf;
188 +               if (!row1_map)
189 +                       continue;
190 +               for (row2 = row1 + 1; row2 < 4; row2++) {
191 +                       u16 rect_map = (key_map >> (row2 * 4)) & 0xf;
192 +                       const u8 *rp;
193 +
194 +                       rect_map &= row1_map;
195 +                       if (!rect_map)
196 +                               continue;
197 +                       for (rp = rect_pat; *rp; rp++)
198 +                               if ((rect_map & *rp) == *rp)
199 +                                       _filter_out(tsc, prev_state, new_state,
200 +                                                   row1, row2, *rp);
201 +               }
202 +       }
203 +}
204 +
205 +static void tsc2301_kp_timer(unsigned long arg)
206 +{
207 +       struct tsc2301 *tsc = (void *) arg;
208 +       struct tsc2301_kp *kp = tsc->kp;
209 +       unsigned long flags;
210 +
211 +       tsc2301_kp_send_key_events(tsc, kp->keys_pressed, 0);
212 +       spin_lock_irqsave(&kp->lock, flags);
213 +       kp->keys_pressed = 0;
214 +       spin_unlock_irqrestore(&kp->lock, flags);
215 +}
216 +
217 +static void tsc2301_kp_rx(void *arg)
218 +{
219 +       struct tsc2301 *tsc = arg;
220 +       struct tsc2301_kp *kp = tsc->kp;
221 +       unsigned long flags;
222 +       u16 kp_data;
223 +
224 +       kp_data = kp->data;
225 +       dev_dbg(&tsc->spi->dev, "KP data %04x\n", kp_data);
226 +
227 +       tsc2301_filter_ghost_keys(tsc, kp->keys_pressed, &kp_data);
228 +       tsc2301_kp_send_key_events(tsc, kp->keys_pressed, kp_data);
229 +       spin_lock_irqsave(&kp->lock, flags);
230 +       kp->keys_pressed = kp_data;
231 +       kp->pending = 0;
232 +       spin_unlock_irqrestore(&kp->lock, flags);
233 +}
234 +
235 +static irqreturn_t tsc2301_kp_irq_handler(int irq, void *dev_id)
236 +{
237 +       struct tsc2301 *tsc = dev_id;
238 +       struct tsc2301_kp *kp = tsc->kp;
239 +       unsigned long flags;
240 +       int r;
241 +
242 +       spin_lock_irqsave(&kp->lock, flags);
243 +       if (tsc2301_kp_disabled(tsc)) {
244 +               spin_unlock_irqrestore(&kp->lock, flags);
245 +               return IRQ_HANDLED;
246 +       }
247 +       kp->pending = 1;
248 +       spin_unlock_irqrestore(&kp->lock, flags);
249 +       mod_timer(&kp->timer,
250 +                jiffies + msecs_to_jiffies(TSC2301_RELEASE_TIMEOUT));
251 +       r = spi_async(tsc->spi, &tsc->kp->read_msg);
252 +       if (r)
253 +               dev_err(&tsc->spi->dev, "kp: spi_async() failed");
254 +       return IRQ_HANDLED;
255 +}
256 +
257 +static void tsc2301_kp_start_scan(struct tsc2301 *tsc)
258 +{
259 +       tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, tsc->kp->mask);
260 +       tsc2301_write_reg(tsc, TSC2301_REG_KEY, TSC2301_DEBOUNCE_TIME);
261 +}
262 +
263 +static void tsc2301_kp_stop_scan(struct tsc2301 *tsc)
264 +{
265 +       tsc2301_write_reg(tsc, TSC2301_REG_KEY, 1 << 14);
266 +}
267 +
268 +/* Must be called with the mutex held */
269 +static void tsc2301_kp_enable(struct tsc2301 *tsc)
270 +{
271 +       struct tsc2301_kp *kp = tsc->kp;
272 +       unsigned long flags;
273 +
274 +       spin_lock_irqsave(&kp->lock, flags);
275 +       BUG_ON(!tsc2301_kp_disabled(tsc));
276 +       if (--kp->disable_depth != 0) {
277 +               spin_unlock_irqrestore(&kp->lock, flags);
278 +               return;
279 +       }
280 +       spin_unlock_irqrestore(&kp->lock, flags);
281 +
282 +       irq_set_irq_type(kp->irq, IRQ_TYPE_EDGE_FALLING);
283 +       tsc2301_kp_start_scan(tsc);
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 +       disable_irq_nosync(kp->irq);
299 +       irq_set_irq_type(kp->irq, IRQ_TYPE_NONE);
300 +       spin_unlock_irqrestore(&kp->lock, flags);
301 +
302 +       while (kp->pending) {
303 +               msleep(1);
304 +       }
305 +
306 +       tsc2301_kp_stop_scan(tsc);
307 +out:
308 +       if (!release_keys)
309 +               del_timer(&kp->timer); /* let timeout release keys */
310 +
311 +       return 0;
312 +}
313 +
314 +/* The following workaround is needed for a HW bug triggered by the
315 + * following:
316 + * 1. keep any key pressed
317 + * 2. disable keypad
318 + * 3. release all keys
319 + * 4. reenable keypad
320 + * 5. disable touch screen controller
321 + *
322 + * After this the keypad scanner will get stuck in busy state and won't
323 + * report any interrupts for further keypresses. One way to recover is to
324 + * restart the keypad scanner whenever we enable / disable the
325 + * touchscreen controller.
326 + */
327 +void tsc2301_kp_restart(struct tsc2301 *tsc)
328 +{
329 +       if (!tsc2301_kp_disabled(tsc)) {
330 +               tsc2301_kp_start_scan(tsc);
331 +       }
332 +}
333 +
334 +static ssize_t tsc2301_kp_disable_show(struct device *dev,
335 +                                      struct device_attribute *attr, char *buf)
336 +{
337 +       struct tsc2301          *tsc = dev_get_drvdata(dev);
338 +
339 +       return sprintf(buf, "%u\n", tsc2301_kp_disabled(tsc) ? 1 : 0);
340 +}
341 +
342 +static ssize_t tsc2301_kp_disable_store(struct device *dev,
343 +                                       struct device_attribute *attr,
344 +                                       const char *buf, size_t count)
345 +{
346 +       struct tsc2301          *tsc = dev_get_drvdata(dev);
347 +       struct tsc2301_kp       *kp = tsc->kp;
348 +       char *endp;
349 +       int i;
350 +
351 +       i = simple_strtoul(buf, &endp, 10);
352 +       i = i ? 1 : 0;
353 +
354 +       mutex_lock(&kp->mutex);
355 +       if (i == kp->user_disabled) {
356 +               mutex_unlock(&kp->mutex);
357 +               return count;
358 +       }
359 +       kp->user_disabled = i;
360 +
361 +       if (i)
362 +               tsc2301_kp_disable(tsc, 1);
363 +       else
364 +               tsc2301_kp_enable(tsc);
365 +       mutex_unlock(&kp->mutex);
366 +
367 +       return count;
368 +}
369 +
370 +static DEVICE_ATTR(disable_kp, 0664, tsc2301_kp_disable_show,
371 +                  tsc2301_kp_disable_store);
372 +
373 +static const u16 tsc2301_kp_read_data = 0x8000 | TSC2301_REG_KPDATA;
374 +
375 +static void tsc2301_kp_setup_spi_xfer(struct tsc2301 *tsc)
376 +{
377 +       struct tsc2301_kp *kp = tsc->kp;
378 +       struct spi_message *m = &kp->read_msg;
379 +       struct spi_transfer *x = &kp->read_xfer[0];
380 +
381 +       spi_message_init(&kp->read_msg);
382 +
383 +       x->tx_buf = &tsc2301_kp_read_data;
384 +       x->len = 2;
385 +       spi_message_add_tail(x, m);
386 +       x++;
387 +
388 +       x->rx_buf = &kp->data;
389 +       x->len = 2;
390 +       spi_message_add_tail(x, m);
391 +
392 +       m->complete = tsc2301_kp_rx;
393 +       m->context = tsc;
394 +}
395 +
396 +#ifdef CONFIG_PM
397 +int tsc2301_kp_suspend(struct tsc2301 *tsc)
398 +{
399 +       struct tsc2301_kp *kp = tsc->kp;
400 +
401 +       mutex_lock(&kp->mutex);
402 +       tsc2301_kp_disable(tsc, 1);
403 +       mutex_unlock(&kp->mutex);
404 +       return 0;
405 +}
406 +
407 +void tsc2301_kp_resume(struct tsc2301 *tsc)
408 +{
409 +       struct tsc2301_kp *kp = tsc->kp;
410 +
411 +       mutex_lock(&kp->mutex);
412 +       tsc2301_kp_enable(tsc);
413 +       mutex_unlock(&kp->mutex);
414 +}
415 +#endif
416 +
417 +int __devinit tsc2301_kp_init(struct tsc2301 *tsc,
418 +                             struct tsc2301_platform_data *pdata)
419 +{
420 +       struct input_dev *idev;
421 +       struct tsc2301_kp *kp;
422 +       int r, i;
423 +       u16 mask;
424 +
425 +       if (pdata->keyb_int < 0) {
426 +               dev_err(&tsc->spi->dev, "need kbirq");
427 +               return -EINVAL;
428 +       }
429 +
430 +       kp = kzalloc(sizeof(*kp), GFP_KERNEL);
431 +       if (kp == NULL)
432 +               return -ENOMEM;
433 +       tsc->kp = kp;
434 +
435 +       kp->irq = pdata->keyb_int;
436 +       spin_lock_init(&kp->lock);
437 +       mutex_init(&kp->mutex);
438 +
439 +       init_timer(&kp->timer);
440 +       kp->timer.data = (unsigned long) tsc;
441 +       kp->timer.function = tsc2301_kp_timer;
442 +
443 +       idev = input_allocate_device();
444 +       if (idev == NULL) {
445 +               r = -ENOMEM;
446 +               goto err1;
447 +       }
448 +       if (pdata->keyb_name)
449 +               idev->name = pdata->keyb_name;
450 +       else
451 +               idev->name = "TSC2301 keypad";
452 +       snprintf(kp->phys, sizeof(kp->phys), "%s/input-kp", dev_name(&tsc->spi->dev));
453 +       idev->phys = kp->phys;
454 +
455 +       mask = 0;
456 +       idev->evbit[0] = BIT(EV_KEY);
457 +       for (i = 0; i < 16; i++) {
458 +               if (pdata->keymap[i] > 0) {
459 +                       set_bit(pdata->keymap[i], idev->keybit);
460 +                       kp->keymap[i] = pdata->keymap[i];
461 +               } else {
462 +                       kp->keymap[i] = -1;
463 +                       mask |= 1 << i;
464 +               }
465 +       }
466 +
467 +       if (pdata->kp_rep)
468 +               set_bit(EV_REP, idev->evbit);
469 +
470 +       kp->idev = idev;
471 +
472 +       tsc2301_kp_setup_spi_xfer(tsc);
473 +
474 +       r = device_create_file(&tsc->spi->dev, &dev_attr_disable_kp);
475 +       if (r < 0)
476 +               goto err2;
477 +
478 +       tsc2301_kp_start_scan(tsc);
479 +
480 +       /* IRQ mode 0 is faulty, it can cause the KBIRQ to get stuck.
481 +        * Mode 2 deasserts the IRQ at:
482 +        * - HW or SW reset
483 +        * - Setting SCS flag in REG_KEY register
484 +        * - Releasing all keys
485 +        * - Reading the REG_KPDATA
486 +        */
487 +       tsc2301_write_kbc(tsc, 2);
488 +
489 +       tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, mask);
490 +       kp->mask = mask;
491 +
492 +       irq_set_irq_type(kp->irq, IRQ_TYPE_EDGE_FALLING);
493 +
494 +       r = request_irq(kp->irq, tsc2301_kp_irq_handler, IRQF_SAMPLE_RANDOM,
495 +                       "tsc2301-kp", tsc);
496 +       if (r < 0) {
497 +               dev_err(&tsc->spi->dev, "unable to get kbirq IRQ");
498 +               goto err3;
499 +       }
500 +       irq_set_irq_wake(kp->irq, 1);
501 +
502 +       /* We need to read the register once..? */
503 +       tsc2301_read_reg(tsc, TSC2301_REG_KPDATA);
504 +
505 +       r = input_register_device(idev);
506 +       if (r < 0) {
507 +               dev_err(&tsc->spi->dev, "can't register keypad device\n");
508 +               goto err4;
509 +       }
510 +
511 +       return 0;
512 +
513 +err4:
514 +       free_irq(kp->irq, tsc);
515 +err3:
516 +       tsc2301_kp_stop_scan(tsc);
517 +       device_remove_file(&tsc->spi->dev, &dev_attr_disable_kp);
518 +err2:
519 +       input_free_device(kp->idev);
520 +err1:
521 +       kfree(kp);
522 +       return r;
523 +}
524 +
525 +void __devexit tsc2301_kp_exit(struct tsc2301 *tsc)
526 +{
527 +       struct tsc2301_kp *kp = tsc->kp;
528 +
529 +       tsc2301_kp_disable(tsc, 1);
530 +       input_unregister_device(kp->idev);
531 +       free_irq(kp->irq, tsc);
532 +       device_remove_file(&tsc->spi->dev, &dev_attr_disable_kp);
533 +
534 +       kfree(kp);
535 +}
536 Index: linux-3.1/drivers/input/touchscreen/Kconfig
537 ===================================================================
538 --- linux-3.1.orig/drivers/input/touchscreen/Kconfig    2011-10-30 00:48:29.341056972 +0200
539 +++ linux-3.1/drivers/input/touchscreen/Kconfig 2011-10-30 00:48:48.989043470 +0200
540 @@ -673,6 +673,12 @@ config TOUCHSCREEN_TSC2007
541           To compile this driver as a module, choose M here: the
542           module will be called tsc2007.
543  
544 +config TOUCHSCREEN_TSC2301
545 +       tristate "TSC2301 touchscreen support"
546 +       depends on SPI_TSC2301
547 +       help
548 +         Say Y here for if you are using the touchscreen features of TSC2301.
549 +
550  config TOUCHSCREEN_W90X900
551         tristate "W90P910 touchscreen driver"
552         depends on HAVE_CLK
553 Index: linux-3.1/drivers/input/touchscreen/Makefile
554 ===================================================================
555 --- linux-3.1.orig/drivers/input/touchscreen/Makefile   2011-10-30 00:48:29.349056969 +0200
556 +++ linux-3.1/drivers/input/touchscreen/Makefile        2011-10-30 00:48:48.989043470 +0200
557 @@ -48,6 +48,7 @@ obj-$(CONFIG_TOUCHSCREEN_TOUCHRIGHT)  +=
558  obj-$(CONFIG_TOUCHSCREEN_TOUCHWIN)     += touchwin.o
559  obj-$(CONFIG_TOUCHSCREEN_TSC2005)      += tsc2005.o
560  obj-$(CONFIG_TOUCHSCREEN_TSC2007)      += tsc2007.o
561 +obj-$(CONFIG_TOUCHSCREEN_TSC2301)      += tsc2301_ts.o
562  obj-$(CONFIG_TOUCHSCREEN_UCB1400)      += ucb1400_ts.o
563  obj-$(CONFIG_TOUCHSCREEN_WACOM_W8001)  += wacom_w8001.o
564  obj-$(CONFIG_TOUCHSCREEN_WM831X)       += wm831x-ts.o
565 Index: linux-3.1/drivers/input/touchscreen/tsc2301_ts.c
566 ===================================================================
567 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
568 +++ linux-3.1/drivers/input/touchscreen/tsc2301_ts.c    2011-10-30 00:48:48.989043470 +0200
569 @@ -0,0 +1,676 @@
570 +/*
571 + * TSC2301 touchscreen driver
572 + *
573 + * Copyright (C) 2005-2008 Nokia Corporation
574 + *
575 + * Written by Jarkko Oikarinen, Imre Deak and Juha Yrjola
576 + *
577 + * This program is free software; you can redistribute it and/or modify
578 + * it under the terms of the GNU General Public License as published by
579 + * the Free Software Foundation; either version 2 of the License, or
580 + * (at your option) any later version.
581 + *
582 + * This program is distributed in the hope that it will be useful,
583 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
584 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
585 + * GNU General Public License for more details.
586 + *
587 + * You should have received a copy of the GNU General Public License
588 + * along with this program; if not, write to the Free Software
589 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
590 + *
591 + */
592 +
593 +#include <linux/kernel.h>
594 +#include <linux/module.h>
595 +#include <linux/input.h>
596 +#include <linux/interrupt.h>
597 +#include <linux/delay.h>
598 +#include <linux/spi/spi.h>
599 +
600 +#include <linux/spi/tsc2301.h>
601 +
602 +/**
603 + * The touchscreen interface operates as follows:
604 + *
605 + * Initialize:
606 + *    Request access to GPIO103 (DAV)
607 + *    tsc2301_ts_irq_handler will trigger when DAV line goes down
608 + *
609 + *  1) Pen is pressed against touchscreeen
610 + *  2) TSC2301 performs AD conversion
611 + *  3) After the conversion is done TSC2301 drives DAV line down
612 + *  4) GPIO IRQ is received and tsc2301_ts_irq_handler is called
613 + *  5) tsc2301_ts_irq_handler queues up an spi transfer to fetch
614 + *     the x, y, z1, z2 values
615 + *  6) SPI framework calls tsc2301_ts_rx after the coordinates are read
616 + *  7) When the penup_timer expires, there have not been DAV interrupts
617 + *     during the last 20ms which means the pen has been lifted.
618 + */
619 +
620 +
621 +#define TSC2301_TOUCHSCREEN_PRODUCT_ID                 0x0052
622 +#define TSC2301_TOUCHSCREEN_PRODUCT_VERSION            0x0001
623 +
624 +#define TSC2301_TS_PENUP_TIME                          20
625 +
626 +#define TSC2301_ADCREG_CONVERSION_CTRL_BY_TSC2301      0x8000
627 +#define TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST         0x0000
628 +
629 +#define TSC2301_ADCREG_FUNCTION_NONE                   0x0000
630 +#define TSC2301_ADCREG_FUNCTION_XY                     0x0400
631 +#define TSC2301_ADCREG_FUNCTION_XYZ                    0x0800
632 +#define TSC2301_ADCREG_FUNCTION_X                      0x0C00
633 +#define TSC2301_ADCREG_FUNCTION_Y                      0x1000
634 +#define TSC2301_ADCREG_FUNCTION_Z                      0x1400
635 +#define TSC2301_ADCREG_FUNCTION_DAT1                   0x1800
636 +#define TSC2301_ADCREG_FUNCTION_DAT2                   0x1C00
637 +#define TSC2301_ADCREG_FUNCTION_AUX1                   0x2000
638 +#define TSC2301_ADCREG_FUNCTION_AUX2                   0x2400
639 +#define TSC2301_ADCREG_FUNCTION_TEMP                   0x2800
640 +
641 +#define TSC2301_ADCREG_RESOLUTION_8BIT                 0x0100
642 +#define TSC2301_ADCREG_RESOLUTION_10BIT                        0x0200
643 +#define TSC2301_ADCREG_RESOLUTION_12BIT                        0x0300
644 +
645 +#define TSC2301_ADCREG_AVERAGING_NONE                  0x0000
646 +#define TSC2301_ADCREG_AVERAGING_4AVG                  0x0040
647 +#define TSC2301_ADCREG_AVERAGING_8AVG                  0x0080
648 +#define TSC2301_ADCREG_AVERAGING_16AVG                 0x00C0
649 +
650 +#define TSC2301_ADCREG_CLOCK_8MHZ                      0x0000
651 +#define TSC2301_ADCREG_CLOCK_4MHZ                      0x0010
652 +#define TSC2301_ADCREG_CLOCK_2MHZ                      0x0020
653 +#define TSC2301_ADCREG_CLOCK_1MHZ                      0x0030
654 +
655 +#define TSC2301_ADCREG_VOLTAGE_STAB_0US                        0x0000
656 +#define TSC2301_ADCREG_VOLTAGE_STAB_100US              0x0002
657 +#define TSC2301_ADCREG_VOLTAGE_STAB_500US              0x0004
658 +#define TSC2301_ADCREG_VOLTAGE_STAB_1MS                        0x0006
659 +#define TSC2301_ADCREG_VOLTAGE_STAB_5MS                        0x0008
660 +#define TSC2301_ADCREG_VOLTAGE_STAB_10MS               0x000A
661 +#define TSC2301_ADCREG_VOLTAGE_STAB_50MS               0x000C
662 +#define TSC2301_ADCREG_VOLTAGE_STAB_100MS              0x000E
663 +
664 +#define TSC2301_ADCREG_STOP_CONVERSION                 0x4000
665 +
666 +#define MAX_12BIT                                      ((1 << 12) - 1)
667 +
668 +#define TS_RECT_SIZE                                   8
669 +#define TSF_MIN_Z1                                     100
670 +#define TSF_MAX_Z2                                     4000
671 +
672 +#define TSF_SAMPLES                                    4
673 +
674 +struct ts_filter {
675 +       int                     sample_cnt;
676 +
677 +       int                     avg_x;
678 +       int                     avg_y;
679 +       int                     avg_z1;
680 +       int                     avg_z2;
681 +};
682 +
683 +struct ts_coords {
684 +       u16                     x;
685 +       u16                     y;
686 +       u16                     z1;
687 +       u16                     z2;
688 +};
689 +
690 +struct tsc2301_ts {
691 +       struct input_dev        *idev;
692 +       char                    phys[32];
693 +       struct timer_list       penup_timer;
694 +       struct mutex            mutex;
695 +
696 +       struct spi_transfer     read_xfer[2];
697 +       struct spi_message      read_msg;
698 +       struct ts_coords        *coords;
699 +
700 +       struct ts_filter        filter;
701 +
702 +       int                     hw_avg_max;
703 +       u16                     x;
704 +       u16                     y;
705 +       u16                     p;
706 +
707 +       u16                     x_plate_ohm;
708 +       int                     stab_time;
709 +       int                     max_pressure;
710 +       int                     touch_pressure;
711 +
712 +       u8                      event_sent;
713 +       u8                      pen_down;
714 +       u8                      disabled;
715 +       u8                      disable_depth;
716 +
717 +       int                     hw_flags;
718 +       int                     irq;
719 +};
720 +
721 +
722 +static const u16 tsc2301_ts_read_data = 0x8000 | TSC2301_REG_X;
723 +
724 +static int tsc2301_ts_check_config(struct tsc2301_ts *ts, int *hw_flags)
725 +{
726 +       int flags;
727 +
728 +       flags = 0;
729 +       switch (ts->hw_avg_max) {
730 +       case 0:
731 +               flags |= TSC2301_ADCREG_AVERAGING_NONE;
732 +               break;
733 +       case 4:
734 +               flags |= TSC2301_ADCREG_AVERAGING_4AVG;
735 +               break;
736 +       case 8:
737 +               flags |= TSC2301_ADCREG_AVERAGING_8AVG;
738 +               break;
739 +       case 16:
740 +               flags |= TSC2301_ADCREG_AVERAGING_16AVG;
741 +               break;
742 +       default:
743 +               return -EINVAL;
744 +       }
745 +
746 +       switch (ts->stab_time) {
747 +       case 0:
748 +               flags |= TSC2301_ADCREG_VOLTAGE_STAB_0US;
749 +               break;
750 +       case 100:
751 +               flags |= TSC2301_ADCREG_VOLTAGE_STAB_100US;
752 +               break;
753 +       case 500:
754 +               flags |= TSC2301_ADCREG_VOLTAGE_STAB_500US;
755 +               break;
756 +       case 1000:
757 +               flags |= TSC2301_ADCREG_VOLTAGE_STAB_1MS;
758 +               break;
759 +       case 5000:
760 +               flags |= TSC2301_ADCREG_VOLTAGE_STAB_5MS;
761 +               break;
762 +       case 10000:
763 +               flags |= TSC2301_ADCREG_VOLTAGE_STAB_10MS;
764 +               break;
765 +       case 50000:
766 +               flags |= TSC2301_ADCREG_VOLTAGE_STAB_50MS;
767 +               break;
768 +       case 100000:
769 +               flags |= TSC2301_ADCREG_VOLTAGE_STAB_100MS;
770 +               break;
771 +       default:
772 +               return -EINVAL;
773 +       }
774 +
775 +       *hw_flags = flags;
776 +       return 0;
777 +}
778 +
779 +/*
780 + * This odd three-time initialization is to work around a bug in TSC2301.
781 + * See TSC2301 errata for details.
782 + */
783 +static int tsc2301_ts_configure(struct tsc2301 *tsc, int flags)
784 +{
785 +       struct spi_transfer xfer[5];
786 +       struct spi_transfer *x;
787 +       struct spi_message m;
788 +       int i;
789 +       u16 val1, val2, val3;
790 +       u16 data[10];
791 +
792 +       val1 = TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST |
793 +               TSC2301_ADCREG_STOP_CONVERSION |
794 +               TSC2301_ADCREG_FUNCTION_NONE |
795 +               TSC2301_ADCREG_RESOLUTION_12BIT |
796 +               TSC2301_ADCREG_AVERAGING_NONE |
797 +               TSC2301_ADCREG_CLOCK_2MHZ |
798 +               TSC2301_ADCREG_VOLTAGE_STAB_100MS;
799 +
800 +       val2 = TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST |
801 +               TSC2301_ADCREG_FUNCTION_XYZ |
802 +               TSC2301_ADCREG_RESOLUTION_12BIT |
803 +               TSC2301_ADCREG_AVERAGING_16AVG |
804 +               TSC2301_ADCREG_CLOCK_1MHZ |
805 +               TSC2301_ADCREG_VOLTAGE_STAB_100MS;
806 +
807 +       /* Averaging and voltage stabilization settings in flags */
808 +       val3 = TSC2301_ADCREG_CONVERSION_CTRL_BY_TSC2301 |
809 +               TSC2301_ADCREG_FUNCTION_XYZ |
810 +               TSC2301_ADCREG_RESOLUTION_12BIT |
811 +               TSC2301_ADCREG_CLOCK_2MHZ |
812 +               flags;
813 +
814 +       /* Now we prepare the command for transferring */
815 +       data[0] = TSC2301_REG_ADC;
816 +       data[1] = val1;
817 +       data[2] = TSC2301_REG_ADC;
818 +       data[3] = val2;
819 +       data[4] = TSC2301_REG_ADC;
820 +       data[5] = val3;
821 +       data[6] = TSC2301_REG_REF;
822 +       data[7] = 1 << 4 | 1 << 2 | 1; /* intref, 100uS settl, 2.5V ref */
823 +       data[8] = TSC2301_REG_CONFIG;
824 +       data[9] = 3 << 3 | 2 << 0; /* 340uS pre-chrg, 544us delay */
825 +
826 +       spi_message_init(&m);
827 +       m.spi = tsc->spi;
828 +
829 +       memset(xfer, 0, sizeof(xfer));
830 +       x = &xfer[0];
831 +
832 +       for (i = 0; i < 10; i += 2) {
833 +               x->tx_buf = &data[i];
834 +               x->len = 4;
835 +               if (i != 8)
836 +                       x->cs_change = 1;
837 +               spi_message_add_tail(x, &m);
838 +               x++;
839 +       }
840 +       spi_sync(m.spi, &m);
841 +
842 +       return 0;
843 +}
844 +
845 +static void tsc2301_ts_start_scan(struct tsc2301 *tsc)
846 +{
847 +       tsc2301_ts_configure(tsc, tsc->ts->hw_flags);
848 +       tsc2301_kp_restart(tsc);
849 +}
850 +
851 +static void tsc2301_ts_stop_scan(struct tsc2301 *tsc)
852 +{
853 +       tsc2301_write_reg(tsc, TSC2301_REG_ADC, TSC2301_ADCREG_STOP_CONVERSION);
854 +       tsc2301_kp_restart(tsc);
855 +}
856 +
857 +static void update_pen_state(struct tsc2301_ts *ts, int x, int y, int pressure)
858 +{
859 +       if (pressure) {
860 +               input_report_abs(ts->idev, ABS_X, x);
861 +               input_report_abs(ts->idev, ABS_Y, y);
862 +               input_report_abs(ts->idev, ABS_PRESSURE, pressure);
863 +               if (!ts->pen_down)
864 +                       input_report_key(ts->idev, BTN_TOUCH, 1);
865 +               ts->pen_down = 1;
866 +       } else {
867 +               input_report_abs(ts->idev, ABS_PRESSURE, 0);
868 +               if (ts->pen_down)
869 +                       input_report_key(ts->idev, BTN_TOUCH, 0);
870 +               ts->pen_down = 0;
871 +       }
872 +
873 +       input_sync(ts->idev);
874 +
875 +#ifdef VERBOSE
876 +       dev_dbg(&tsc->spi->dev, "x %4d y %4d p %4d\n", x, y, pressure);
877 +#endif
878 +}
879 +
880 +static int filter(struct tsc2301_ts *ts, int x, int y, int z1, int z2)
881 +{
882 +       int inside_rect, pressure_limit, Rt;
883 +       struct ts_filter *tsf = &ts->filter;
884 +
885 +       /* validate pressure and position */
886 +       if (x > MAX_12BIT || y > MAX_12BIT)
887 +               return 0;
888 +
889 +       /* skip coords if the pressure-components are out of range */
890 +       if (z1 < TSF_MIN_Z1 || z2 > TSF_MAX_Z2)
891 +               return 0;
892 +
893 +       /* Use the x,y,z1,z2 directly on the first "pen down" event */
894 +       if (ts->event_sent) {
895 +               tsf->avg_x  += x;
896 +               tsf->avg_y  += y;
897 +               tsf->avg_z1 += z1;
898 +               tsf->avg_z2 += z2;
899 +
900 +               if (++tsf->sample_cnt < TSF_SAMPLES)
901 +                       return 0;
902 +               x = tsf->avg_x / TSF_SAMPLES;
903 +               y = tsf->avg_y / TSF_SAMPLES;
904 +               z1 = tsf->avg_z1 / TSF_SAMPLES;
905 +               z2 = tsf->avg_z2 / TSF_SAMPLES;
906 +       }
907 +       tsf->sample_cnt = 0;
908 +       tsf->avg_x  = 0;
909 +       tsf->avg_y  = 0;
910 +       tsf->avg_z1 = 0;
911 +       tsf->avg_z2 = 0;
912 +
913 +       pressure_limit = ts->event_sent? ts->max_pressure: ts->touch_pressure;
914 +
915 +       /* z1 is always at least 100: */
916 +       Rt = x * (z2 - z1) / z1;
917 +       Rt = Rt * ts->x_plate_ohm / 4096;
918 +       if (Rt > pressure_limit)
919 +               return 0;
920 +
921 +       /* discard the event if it still is within the previous rect - unless
922 +        * if the pressure is harder, but then use previous x,y position */
923 +       inside_rect = (
924 +           x > (int)ts->x - TS_RECT_SIZE && x < (int)ts->x + TS_RECT_SIZE &&
925 +           y > (int)ts->y - TS_RECT_SIZE && y < (int)ts->y + TS_RECT_SIZE);
926 +
927 +       if (!ts->event_sent || !inside_rect) {
928 +               ts->x = x;
929 +               ts->y = y;
930 +               ts->p = Rt;
931 +               return 1;
932 +       } else if (Rt < ts->p) {
933 +               ts->p = Rt;
934 +               return 1;
935 +       }
936 +       return 0;
937 +}
938 +
939 +/*
940 + * This procedure is called by the SPI framework after the coordinates
941 + * have been read from TSC2301
942 + */
943 +static void tsc2301_ts_rx(void *arg)
944 +{
945 +       struct tsc2301 *tsc = arg;
946 +       struct tsc2301_ts *ts = tsc->ts;
947 +       int send_event;
948 +       int x, y, z1, z2;
949 +
950 +       x  = ts->coords->x;
951 +       y  = ts->coords->y;
952 +       z1 = ts->coords->z1;
953 +       z2 = ts->coords->z2;
954 +
955 +       send_event = filter(ts, x, y, z1, z2);
956 +       if (send_event) {
957 +               update_pen_state(ts, ts->x, ts->y, ts->p);
958 +               ts->event_sent = 1;
959 +       }
960 +
961 +       mod_timer(&ts->penup_timer,
962 +                 jiffies + msecs_to_jiffies(TSC2301_TS_PENUP_TIME));
963 +}
964 +
965 +/*
966 + * Timer is called TSC2301_TS_PENUP_TIME after pen is up
967 + */
968 +static void tsc2301_ts_timer_handler(unsigned long data)
969 +{
970 +       struct tsc2301 *tsc = (struct tsc2301 *)data;
971 +       struct tsc2301_ts *ts = tsc->ts;
972 +
973 +       if (ts->event_sent) {
974 +               ts->event_sent = 0;
975 +               update_pen_state(ts, 0, 0, 0);
976 +       }
977 +}
978 +
979 +/*
980 + * This interrupt is called when pen is down and coordinates are
981 + * available. That is indicated by a falling edge on DEV line.
982 + */
983 +static irqreturn_t tsc2301_ts_irq_handler(int irq, void *dev_id)
984 +{
985 +       struct tsc2301 *tsc = dev_id;
986 +       struct tsc2301_ts *ts = tsc->ts;
987 +       int r;
988 +
989 +       r = spi_async(tsc->spi, &ts->read_msg);
990 +       if (r)
991 +               dev_err(&tsc->spi->dev, "ts: spi_async() failed");
992 +
993 +       mod_timer(&ts->penup_timer,
994 +                 jiffies + msecs_to_jiffies(TSC2301_TS_PENUP_TIME));
995 +
996 +       return IRQ_HANDLED;
997 +}
998 +
999 +static void tsc2301_ts_disable(struct tsc2301 *tsc)
1000 +{
1001 +       struct tsc2301_ts *ts = tsc->ts;
1002 +
1003 +       if (ts->disable_depth++ != 0)
1004 +               return;
1005 +
1006 +       disable_irq(ts->irq);
1007 +
1008 +       /* wait until penup timer expire normally */
1009 +       do {
1010 +               msleep(1);
1011 +       } while (ts->event_sent);
1012 +
1013 +       tsc2301_ts_stop_scan(tsc);
1014 +}
1015 +
1016 +static void tsc2301_ts_enable(struct tsc2301 *tsc)
1017 +{
1018 +       struct tsc2301_ts *ts = tsc->ts;
1019 +
1020 +       if (--ts->disable_depth != 0)
1021 +               return;
1022 +
1023 +       enable_irq(ts->irq);
1024 +
1025 +       tsc2301_ts_start_scan(tsc);
1026 +}
1027 +
1028 +#ifdef CONFIG_PM
1029 +int tsc2301_ts_suspend(struct tsc2301 *tsc)
1030 +{
1031 +       struct tsc2301_ts *ts = tsc->ts;
1032 +
1033 +       mutex_lock(&ts->mutex);
1034 +       tsc2301_ts_disable(tsc);
1035 +       mutex_unlock(&ts->mutex);
1036 +
1037 +       return 0;
1038 +}
1039 +
1040 +void tsc2301_ts_resume(struct tsc2301 *tsc)
1041 +{
1042 +       struct tsc2301_ts *ts = tsc->ts;
1043 +
1044 +       mutex_lock(&ts->mutex);
1045 +       tsc2301_ts_enable(tsc);
1046 +       mutex_unlock(&ts->mutex);
1047 +}
1048 +#endif
1049 +
1050 +static void tsc2301_ts_setup_spi_xfer(struct tsc2301 *tsc)
1051 +{
1052 +       struct tsc2301_ts *ts = tsc->ts;
1053 +       struct spi_message *m = &ts->read_msg;
1054 +       struct spi_transfer *x = &ts->read_xfer[0];
1055 +
1056 +       spi_message_init(m);
1057 +
1058 +       x->tx_buf = &tsc2301_ts_read_data;
1059 +       x->len = 2;
1060 +       spi_message_add_tail(x, m);
1061 +
1062 +       x++;
1063 +       x->rx_buf = ts->coords;
1064 +       x->len = 8;
1065 +       spi_message_add_tail(x, m);
1066 +
1067 +       m->complete = tsc2301_ts_rx;
1068 +       m->context = tsc;
1069 +}
1070 +
1071 +static ssize_t tsc2301_ts_pen_down_show(struct device *dev,
1072 +                                       struct device_attribute *attr,
1073 +                                       char *buf)
1074 +{
1075 +       struct tsc2301 *tsc = dev_get_drvdata(dev);
1076 +
1077 +       return sprintf(buf, "%u\n", tsc->ts->pen_down);
1078 +}
1079 +
1080 +static DEVICE_ATTR(pen_down, S_IRUGO, tsc2301_ts_pen_down_show, NULL);
1081 +
1082 +static ssize_t tsc2301_ts_disable_show(struct device *dev,
1083 +                                      struct device_attribute *attr, char *buf)
1084 +{
1085 +       struct tsc2301          *tsc = dev_get_drvdata(dev);
1086 +       struct tsc2301_ts       *ts = tsc->ts;
1087 +
1088 +       return sprintf(buf, "%u\n", ts->disabled);
1089 +}
1090 +
1091 +static ssize_t tsc2301_ts_disable_store(struct device *dev,
1092 +                                       struct device_attribute *attr,
1093 +                                       const char *buf, size_t count)
1094 +{
1095 +       struct tsc2301          *tsc = dev_get_drvdata(dev);
1096 +       struct tsc2301_ts       *ts = tsc->ts;
1097 +       char *endp;
1098 +       int i;
1099 +
1100 +       i = simple_strtoul(buf, &endp, 10);
1101 +       i = i ? 1 : 0;
1102 +       mutex_lock(&ts->mutex);
1103 +       if (i == ts->disabled) goto out;
1104 +       ts->disabled = i;
1105 +
1106 +       if (i)
1107 +               tsc2301_ts_disable(tsc);
1108 +       else
1109 +               tsc2301_ts_enable(tsc);
1110 +out:
1111 +       mutex_unlock(&ts->mutex);
1112 +       return count;
1113 +}
1114 +
1115 +static DEVICE_ATTR(disable_ts, 0664, tsc2301_ts_disable_show,
1116 +                  tsc2301_ts_disable_store);
1117 +
1118 +int __devinit tsc2301_ts_init(struct tsc2301 *tsc,
1119 +                             struct tsc2301_platform_data *pdata)
1120 +{
1121 +       struct tsc2301_ts *ts;
1122 +       struct input_dev *idev;
1123 +       int r;
1124 +       int x_max, y_max;
1125 +       int x_fudge, y_fudge, p_fudge;
1126 +
1127 +       if (pdata->dav_int <= 0) {
1128 +               dev_err(&tsc->spi->dev, "need DAV IRQ");
1129 +               return -EINVAL;
1130 +       }
1131 +
1132 +       ts = kzalloc(sizeof(*ts), GFP_KERNEL);
1133 +       if (ts == NULL)
1134 +               return -ENOMEM;
1135 +       tsc->ts = ts;
1136 +
1137 +       ts->coords = kzalloc(sizeof(*ts->coords), GFP_KERNEL);
1138 +       if (ts->coords == NULL) {
1139 +               kfree(ts);
1140 +               return -ENOMEM;
1141 +       }
1142 +
1143 +       ts->irq = pdata->dav_int;
1144 +
1145 +       init_timer(&ts->penup_timer);
1146 +       setup_timer(&ts->penup_timer, tsc2301_ts_timer_handler,
1147 +                       (unsigned long)tsc);
1148 +
1149 +       mutex_init(&ts->mutex);
1150 +
1151 +       ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
1152 +       ts->hw_avg_max  = pdata->ts_hw_avg;
1153 +       ts->max_pressure = pdata->ts_max_pressure ? : MAX_12BIT;
1154 +       ts->touch_pressure = pdata->ts_touch_pressure ? : ts->max_pressure;
1155 +       ts->stab_time   = pdata->ts_stab_time;
1156 +
1157 +       x_max           = pdata->ts_x_max ? : 4096;
1158 +       y_max           = pdata->ts_y_max ? : 4096;
1159 +       x_fudge         = pdata->ts_x_fudge ? : 4;
1160 +       y_fudge         = pdata->ts_y_fudge ? : 8;
1161 +       p_fudge         = pdata->ts_pressure_fudge ? : 2;
1162 +
1163 +       if ((r = tsc2301_ts_check_config(ts, &ts->hw_flags))) {
1164 +               dev_err(&tsc->spi->dev, "invalid configuration\n");
1165 +               goto err2;
1166 +       }
1167 +
1168 +       idev = input_allocate_device();
1169 +       if (idev == NULL) {
1170 +               r = -ENOMEM;
1171 +               goto err2;
1172 +       }
1173 +       idev->name = "TSC2301 touchscreen";
1174 +       snprintf(ts->phys, sizeof(ts->phys),
1175 +                "%s/input-ts", dev_name(&tsc->spi->dev));
1176 +       idev->phys = ts->phys;
1177 +       idev->dev.parent = &tsc->spi->dev;
1178 +
1179 +       idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
1180 +       idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
1181 +       ts->idev = idev;
1182 +
1183 +       tsc2301_ts_setup_spi_xfer(tsc);
1184 +
1185 +       /* These parameters should perhaps be configurable? */
1186 +       input_set_abs_params(idev, ABS_X, 0, x_max, x_fudge, 0);
1187 +       input_set_abs_params(idev, ABS_Y, 0, y_max, y_fudge, 0);
1188 +       input_set_abs_params(idev, ABS_PRESSURE, 0, ts->max_pressure,
1189 +                            p_fudge, 0);
1190 +
1191 +       tsc2301_ts_start_scan(tsc);
1192 +
1193 +       r = request_irq(ts->irq, tsc2301_ts_irq_handler,
1194 +                       IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
1195 +                       "tsc2301-ts", tsc);
1196 +       if (r < 0) {
1197 +               dev_err(&tsc->spi->dev, "unable to get DAV IRQ");
1198 +               goto err3;
1199 +       }
1200 +       irq_set_irq_wake(ts->irq, 1);
1201 +
1202 +       if (device_create_file(&tsc->spi->dev, &dev_attr_pen_down) < 0)
1203 +               goto err4;
1204 +       if (device_create_file(&tsc->spi->dev, &dev_attr_disable_ts) < 0)
1205 +               goto err5;
1206 +
1207 +       r = input_register_device(idev);
1208 +       if (r < 0) {
1209 +               dev_err(&tsc->spi->dev, "can't register touchscreen device\n");
1210 +               goto err6;
1211 +       }
1212 +
1213 +       return 0;
1214 +err6:
1215 +       device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
1216 +err5:
1217 +       device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
1218 +err4:
1219 +       free_irq(ts->irq, tsc);
1220 +err3:
1221 +       tsc2301_ts_stop_scan(tsc);
1222 +       input_free_device(idev);
1223 +err2:
1224 +       kfree(ts->coords);
1225 +       kfree(ts);
1226 +       return r;
1227 +}
1228 +
1229 +void __devexit tsc2301_ts_exit(struct tsc2301 *tsc)
1230 +{
1231 +       struct tsc2301_ts *ts = tsc->ts;
1232 +
1233 +       tsc2301_ts_disable(tsc);
1234 +
1235 +       device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
1236 +       device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
1237 +
1238 +       free_irq(ts->irq, tsc);
1239 +       input_unregister_device(ts->idev);
1240 +
1241 +       kfree(ts->coords);
1242 +       kfree(ts);
1243 +}
1244 +MODULE_AUTHOR("Jarkko Oikarinen <jarkko.oikarinen@nokia.com>");
1245 +MODULE_LICENSE("GPL");
1246 Index: linux-3.1/drivers/spi/Kconfig
1247 ===================================================================
1248 --- linux-3.1.orig/drivers/spi/Kconfig  2011-10-30 00:48:29.373056952 +0200
1249 +++ linux-3.1/drivers/spi/Kconfig       2011-10-30 00:48:48.989043470 +0200
1250 @@ -435,6 +435,28 @@ config SPI_TLE62X0
1251           sysfs interface, with each line presented as a kind of GPIO
1252           exposing both switch control and diagnostic feedback.
1253  
1254 +config SPI_TSC2301
1255 +       tristate "TSC2301 driver"
1256 +       depends on SPI_MASTER
1257 +       help
1258 +         Say Y here if you have a TSC2301 chip connected to an SPI
1259 +         bus on your board.
1260 +
1261 +         The TSC2301 is a highly integrated PDA analog interface circuit.
1262 +         It contains a complete 12-bit A/D resistive touch screen
1263 +         converter (ADC) including drivers, touch pressure measurement
1264 +         capability, keypad controller, and 8-bit D/A converter (DAC) output
1265 +         for LCD contrast control.
1266 +
1267 +         To compile this driver as a module, choose M here: the
1268 +         module will be called tsc2301.
1269 +
1270 +config SPI_TSC2301_AUDIO
1271 +       boolean "TSC2301 audio support"
1272 +       depends on SPI_TSC2301 && SND
1273 +       help
1274 +         Say Y here for if you are using the audio features of TSC2301.
1275 +
1276  #
1277  # Add new SPI protocol masters in alphabetical order above this line
1278  #
1279 Index: linux-3.1/drivers/spi/Makefile
1280 ===================================================================
1281 --- linux-3.1.orig/drivers/spi/Makefile 2011-10-30 00:48:29.385056944 +0200
1282 +++ linux-3.1/drivers/spi/Makefile      2011-10-30 00:48:48.989043470 +0200
1283 @@ -59,4 +59,6 @@ obj-$(CONFIG_SPI_TLE62X0)             += spi-tle62x
1284  obj-$(CONFIG_SPI_TOPCLIFF_PCH)         += spi-topcliff-pch.o
1285  obj-$(CONFIG_SPI_TXX9)                 += spi-txx9.o
1286  obj-$(CONFIG_SPI_XILINX)               += spi-xilinx.o
1287 -
1288 +obj-$(CONFIG_SPI_TSC2301)              += tsc2301.o
1289 +tsc2301-objs                           := tsc2301-core.o
1290 +tsc2301-$(CONFIG_SPI_TSC2301_AUDIO)    += tsc2301-mixer.o
1291 Index: linux-3.1/drivers/spi/tsc2301-core.c
1292 ===================================================================
1293 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1294 +++ linux-3.1/drivers/spi/tsc2301-core.c        2011-10-30 00:48:48.989043470 +0200
1295 @@ -0,0 +1,297 @@
1296 +/*
1297 + * TSC2301 driver
1298 + *
1299 + * Copyright (C) 2005, 2006 Nokia Corporation
1300 + *
1301 + * This program is free software; you can redistribute it and/or modify
1302 + * it under the terms of the GNU General Public License as published by
1303 + * the Free Software Foundation; either version 2 of the License, or
1304 + * (at your option) any later version.
1305 + *
1306 + * This program is distributed in the hope that it will be useful,
1307 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1308 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1309 + * GNU General Public License for more details.
1310 + *
1311 + * You should have received a copy of the GNU General Public License
1312 + * along with this program; if not, write to the Free Software
1313 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1314 + *
1315 + */
1316 +
1317 +#include <linux/module.h>
1318 +#include <linux/device.h>
1319 +#include <linux/delay.h>
1320 +#include <linux/gpio.h>
1321 +#include <linux/spi/spi.h>
1322 +#include <linux/spi/tsc2301.h>
1323 +
1324 +u16 tsc2301_read_reg(struct tsc2301 *tsc, int reg)
1325 +{
1326 +       struct spi_transfer t[2];
1327 +       struct spi_message m;
1328 +       u16 data = 0, cmd;
1329 +
1330 +       cmd = reg;
1331 +       cmd |= 0x8000;
1332 +
1333 +       memset(t, 0, sizeof(t));
1334 +       spi_message_init(&m);
1335 +       m.spi = tsc->spi;
1336 +
1337 +       t[0].tx_buf = &cmd;
1338 +       t[0].rx_buf = NULL;
1339 +       t[0].len = 2;
1340 +       spi_message_add_tail(&t[0], &m);
1341 +
1342 +       t[1].tx_buf = NULL;
1343 +       t[1].rx_buf = &data;
1344 +       t[1].len = 2;
1345 +       spi_message_add_tail(&t[1], &m);
1346 +
1347 +       spi_sync(m.spi, &m);
1348 +
1349 +       return data;
1350 +}
1351 +
1352 +void tsc2301_write_reg(struct tsc2301 *tsc, int reg, u16 val)
1353 +{
1354 +       struct spi_transfer t;
1355 +       struct spi_message m;
1356 +       u16 data[2];
1357 +
1358 +       /* Now we prepare the command for transferring */
1359 +       data[0] = reg;
1360 +       data[1] = val;
1361 +
1362 +       spi_message_init(&m);
1363 +       m.spi = tsc->spi;
1364 +
1365 +       memset(&t, 0, sizeof(t));
1366 +       t.tx_buf = data;
1367 +       t.rx_buf = NULL;
1368 +       t.len = 4;
1369 +       spi_message_add_tail(&t, &m);
1370 +
1371 +       spi_sync(m.spi, &m);
1372 +}
1373 +
1374 +void tsc2301_write_kbc(struct tsc2301 *tsc, int val)
1375 +{
1376 +       u16 w;
1377 +
1378 +       w = tsc->config2_shadow;
1379 +       w &= ~(0x03 << 14);
1380 +       w |= (val & 0x03) << 14;
1381 +       tsc2301_write_reg(tsc, TSC2301_REG_CONFIG2, w);
1382 +       tsc->config2_shadow = w;
1383 +}
1384 +
1385 +void tsc2301_write_pll(struct tsc2301 *tsc,
1386 +                      int pll_n, int pll_a, int pll_pdc, int pct_e, int pll_o)
1387 +{
1388 +       u16 w;
1389 +
1390 +       w = tsc->config2_shadow;
1391 +       w &= ~0x3fff;
1392 +       w |= (pll_n & 0x0f) | ((pll_a & 0x0f) << 4) | ((pll_pdc & 0x0f) << 8);
1393 +       w |= pct_e ? (1 << 12) : 0;
1394 +       w |= pll_o ? (1 << 13) : 0;
1395 +       tsc2301_write_reg(tsc, TSC2301_REG_CONFIG2, w);
1396 +       tsc->config2_shadow = w;
1397 +}
1398 +
1399 +void tsc2301_read_buf(struct tsc2301 *tsc, int reg, u16 *rx_buf, int len)
1400 +{
1401 +       struct spi_transfer t[2];
1402 +       struct spi_message m;
1403 +       u16 cmd, i;
1404 +
1405 +       cmd = reg;
1406 +       cmd |= 0x8000;
1407 +
1408 +       spi_message_init(&m);
1409 +       m.spi = tsc->spi;
1410 +
1411 +       memset(t, 0, sizeof(t));
1412 +       t[0].tx_buf = &cmd;
1413 +       t[0].rx_buf = NULL;
1414 +       t[0].len = 2;
1415 +       spi_message_add_tail(&t[0], &m);
1416 +
1417 +       t[1].tx_buf = NULL;
1418 +       t[1].rx_buf = rx_buf;
1419 +       t[1].len = 2 * len;
1420 +       spi_message_add_tail(&t[1], &m);
1421 +
1422 +       spi_sync(m.spi, &m);
1423 +
1424 +       for (i = 0; i < len; i++)
1425 +               printk(KERN_DEBUG "rx_buf[%d]: %04x\n", i, rx_buf[i]);
1426 +}
1427 +
1428 +static int __devinit tsc2301_probe(struct spi_device *spi)
1429 +{
1430 +       struct tsc2301                  *tsc;
1431 +       struct tsc2301_platform_data    *pdata = spi->dev.platform_data;
1432 +       int r;
1433 +       u16 w;
1434 +
1435 +       if (!pdata) {
1436 +               dev_dbg(&spi->dev, "no platform data?\n");
1437 +               return -ENODEV;
1438 +       }
1439 +
1440 +       tsc = kzalloc(sizeof(*tsc), GFP_KERNEL);
1441 +       if (tsc == NULL)
1442 +               return -ENOMEM;
1443 +
1444 +       dev_set_drvdata(&spi->dev, tsc);
1445 +       tsc->spi = spi;
1446 +
1447 +       tsc->enable_clock = pdata->enable_clock;
1448 +       tsc->disable_clock = pdata->disable_clock;
1449 +
1450 +       if (pdata->reset_gpio >= 0) {
1451 +               tsc->reset_gpio = pdata->reset_gpio;
1452 +               r = gpio_request(tsc->reset_gpio, "TSC2301 reset");
1453 +               if (r < 0)
1454 +                       goto err1;
1455 +               gpio_direction_output(tsc->reset_gpio, 1);
1456 +               mdelay(1);
1457 +               gpio_set_value(tsc->reset_gpio, 0);
1458 +       } else
1459 +               tsc->reset_gpio = -1;
1460 +
1461 +       spi->mode = SPI_MODE_1;
1462 +       spi->bits_per_word = 16;
1463 +       /* The max speed might've been defined by the board-specific
1464 +        * struct */
1465 +       if (!spi->max_speed_hz)
1466 +               spi->max_speed_hz = TSC2301_HZ;
1467 +       spi_setup(spi);
1468 +
1469 +       /* Soft reset */
1470 +       tsc2301_write_reg(tsc, TSC2301_REG_RESET, 0xbb00);
1471 +       msleep(1);
1472 +
1473 +       w = tsc2301_read_reg(tsc, TSC2301_REG_ADC);
1474 +       if (!(w & (1 << 14))) {
1475 +               dev_err(&spi->dev, "invalid ADC reg value: %04x\n", w);
1476 +               r = -ENODEV;
1477 +               goto err1;
1478 +       }
1479 +
1480 +       w = tsc2301_read_reg(tsc, TSC2301_REG_DAC);
1481 +       if (!(w & (1 << 15))) {
1482 +               dev_err(&spi->dev, "invalid DAC reg value: %04x\n", w);
1483 +               r = -ENODEV;
1484 +               goto err1;
1485 +       }
1486 +
1487 +       /* Stop keypad scanning */
1488 +       tsc2301_write_reg(tsc, TSC2301_REG_KEY, 0x4000);
1489 +
1490 +       /* We have to cache this for read-modify-write, since we can't
1491 +        * read back BIT15 */
1492 +       w = tsc2301_read_reg(tsc, TSC2301_REG_CONFIG2);
1493 +       /* By default BIT15 is set */
1494 +       w |= 1 << 15;
1495 +       tsc->config2_shadow = w;
1496 +
1497 +       r = tsc2301_kp_init(tsc, pdata);
1498 +       if (r)
1499 +               goto err1;
1500 +       r = tsc2301_ts_init(tsc, pdata);
1501 +       if (r)
1502 +               goto err2;
1503 +       r = tsc2301_mixer_init(tsc, pdata);
1504 +       if (r)
1505 +               goto err3;
1506 +       return 0;
1507 +
1508 +err3:
1509 +       tsc2301_ts_exit(tsc);
1510 +err2:
1511 +       tsc2301_kp_exit(tsc);
1512 +err1:
1513 +       kfree(tsc);
1514 +       return r;
1515 +}
1516 +
1517 +static int __devexit tsc2301_remove(struct spi_device *spi)
1518 +{
1519 +       struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
1520 +
1521 +       tsc2301_mixer_exit(tsc);
1522 +       tsc2301_ts_exit(tsc);
1523 +       tsc2301_kp_exit(tsc);
1524 +       if (tsc->reset_gpio >= 0)
1525 +               gpio_free(tsc->reset_gpio);
1526 +       kfree(tsc);
1527 +
1528 +       return 0;
1529 +}
1530 +
1531 +#ifdef CONFIG_PM
1532 +static int tsc2301_suspend(struct spi_device *spi, pm_message_t mesg)
1533 +{
1534 +       struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
1535 +       int r;
1536 +
1537 +       if ((r = tsc2301_mixer_suspend(tsc)) < 0)
1538 +               return r;
1539 +       if ((r = tsc2301_kp_suspend(tsc)) < 0)
1540 +               goto err1;
1541 +       if ((r = tsc2301_ts_suspend(tsc)) < 0)
1542 +               goto err2;
1543 +
1544 +       return 0;
1545 +err2:
1546 +       tsc2301_kp_resume(tsc);
1547 +err1:
1548 +       tsc2301_mixer_resume(tsc);
1549 +       return r;
1550 +}
1551 +
1552 +static int tsc2301_resume(struct spi_device *spi)
1553 +{
1554 +       struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
1555 +
1556 +       tsc2301_ts_resume(tsc);
1557 +       tsc2301_kp_resume(tsc);
1558 +       tsc2301_mixer_resume(tsc);
1559 +       return 0;
1560 +}
1561 +#endif
1562 +
1563 +static struct spi_driver tsc2301_driver = {
1564 +       .driver = {
1565 +                  .name = "tsc2301",
1566 +                  .bus = &spi_bus_type,
1567 +                  .owner = THIS_MODULE,
1568 +       },
1569 +#ifdef CONFIG_PM
1570 +       .suspend = tsc2301_suspend,
1571 +       .resume = tsc2301_resume,
1572 +#endif
1573 +       .probe = tsc2301_probe,
1574 +       .remove = __devexit_p(tsc2301_remove),
1575 +};
1576 +
1577 +static int __init tsc2301_init(void)
1578 +{
1579 +       printk("TSC2301 driver initializing\n");
1580 +
1581 +       return spi_register_driver(&tsc2301_driver);
1582 +}
1583 +module_init(tsc2301_init);
1584 +
1585 +static void __exit tsc2301_exit(void)
1586 +{
1587 +       spi_unregister_driver(&tsc2301_driver);
1588 +}
1589 +module_exit(tsc2301_exit);
1590 +
1591 +MODULE_AUTHOR("Juha Yrjölä <juha.yrjola@nokia.com>");
1592 +MODULE_LICENSE("GPL");
1593 Index: linux-3.1/drivers/spi/tsc2301-mixer.c
1594 ===================================================================
1595 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1596 +++ linux-3.1/drivers/spi/tsc2301-mixer.c       2011-10-30 00:48:48.989043470 +0200
1597 @@ -0,0 +1,1003 @@
1598 +/*
1599 + * ALSA Mixer implementation for TSC2301
1600 + *
1601 + * Copyright (C) 2006 Nokia Corporation.
1602 + *
1603 + * Contact: Jarkko Nikula <jarkko.nikula@nokia.com>
1604 + *          Juha Yrjola
1605 + *
1606 + * Some notes about TSC2301:
1607 + * - PLL will stop when DAC and ADC's are powered down.
1608 + * - Touchscreen will stop working when audio part is powered up and if audio
1609 + *   MCLK is stopped. Problem is avoided if audio is powered down before
1610 + *   stopping MCLK.
1611 + * - Audio DAC or audio outputs will activate only after 100 msec from the
1612 + *   chip power-up. Reason seems to be VCM since there is no this delay if the
1613 + *   chip and VCM (bit AVPD on PD/MISC) were not powered down. The chip will
1614 + *   consume about 1 mA if all other audio blocks are powered down except the
1615 + *   chip itself and VCM. Full power down consumes only about few uA.
1616 + * - Power-down transition could happen earliest about 100 msec after the chip
1617 + *   power-up. Otherwise power-down will fail if there is no that 100 msec
1618 + *   on time before it. It's not obvious why is that since chip reports
1619 + *   power-up to be completed and also PLL output on GPIO_0 is active in few
1620 + *   milliseconds.
1621 + *
1622 + * This program is free software; you can redistribute it and/or
1623 + * modify it under the terms of the GNU General Public License
1624 + * version 2 as published by the Free Software Foundation.
1625 + *
1626 + * This program is distributed in the hope that it will be useful, but
1627 + * WITHOUT ANY WARRANTY; without even the implied warranty of
1628 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1629 + * General Public License for more details.
1630 + *
1631 + * You should have received a copy of the GNU General Public License
1632 + * along with this program; if not, write to the Free Software
1633 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
1634 + * 02110-1301 USA
1635 + *
1636 + */
1637 +
1638 +#include <linux/module.h>
1639 +#include <linux/device.h>
1640 +#include <linux/errno.h>
1641 +#include <linux/delay.h>
1642 +#include <linux/slab.h>
1643 +#include <linux/spi/spi.h>
1644 +#include <linux/spi/tsc2301.h>
1645 +
1646 +#include <sound/core.h>
1647 +#include <sound/control.h>
1648 +
1649 +/* shadow register indexes */
1650 +enum {
1651 +       /* audio control and volume registers */
1652 +       AUDCNTL_INDEX,
1653 +       ADCVOL_INDEX,
1654 +       DACVOL_INDEX,
1655 +       BPVOL_INDEX,
1656 +       /* keyclick control register (not needed here) */
1657 +       /* audio power control register */
1658 +       PD_MISC_INDEX,
1659 +       /* TSC2301 GPIO control register */
1660 +       GPIO_INDEX,
1661 +
1662 +       SHADOW_REG_COUNT,
1663 +};
1664 +
1665 +/* structure for driver private data */
1666 +struct tsc2301_mixer {
1667 +       struct tsc2301 *tsc;
1668 +       struct mutex mutex;
1669 +
1670 +       /* shadow registers holding TSC2301 audio registers. Used to hold
1671 +        * their states during the sleep and also to reduce communication with
1672 +        * the chip since get callback functions could get register values
1673 +        * directly from these shadow registers without needing to read them
1674 +        * from the chip */
1675 +       u16 shadow_regs[SHADOW_REG_COUNT];
1676 +
1677 +       /* audio controller driver usage of the ADC and DAC */
1678 +       unsigned adc_enabled:1, dac_enabled:1;
1679 +       unsigned pll_output:1;
1680 +       unsigned mclk_enabled;
1681 +
1682 +       /* latest audio power-up timestamp */
1683 +       unsigned long pu_jiffies;
1684 +
1685 +       /* these are used when upper layer(s) are going to power-down TSC2301
1686 +        * before 100 msec is passed from power-up */
1687 +       struct delayed_work delayed_power_down;
1688 +       unsigned delayed_pd_active:1;
1689 +
1690 +       int (* platform_init)(struct device *);
1691 +       void (* platform_cleanup)(struct device *);
1692 +
1693 +       struct tsc2301_mixer_gpio *mixer_gpios;
1694 +       int n_mixer_gpios;
1695 +};
1696 +
1697 +#define TSC2301_DAC_DELAY              msecs_to_jiffies(100)
1698 +#define TSC2301_MIN_PU_PERIOD          msecs_to_jiffies(100)
1699 +
1700 +#define TSC2301_REG_TO_PVAL(reg)       \
1701 +       (TSC2301_REG_TO_PAGE(reg) << 6 | TSC2301_REG_TO_ADDR(reg))
1702 +#define  TSC2301_PVAL_TO_REG(v)                \
1703 +       (TSC2301_REG((((v) >> 6) & 3),((v) & 0x1f)))
1704 +
1705 +#define TSC2301_VOLUME_MASK            0x7f
1706 +#define TSC2301_MIN_ADCVOL             6
1707 +#define TSC2301_MIN_DACVOL             0
1708 +#define TSC2301_MIN_BPVOL              31
1709 +#define TSC2301_MUTE_LEFT_SHIFT                15
1710 +#define TSC2301_VOL_LEFT_SHIFT         8
1711 +#define TSC2301_MUTE_RIGHT_SHIFT       7
1712 +#define TSC2301_VOL_RIGHT_SHIFT                0
1713 +
1714 +#define TSC2301_INM_MASK               3
1715 +#define TSC2301_INML_SHIFT             12
1716 +#define TSC2301_INMR_SHIFT             10
1717 +
1718 +#define TSC2301_MICG_MASK              3
1719 +#define TSC2301_MICG_MIN               1 /* values 0 & 1 both mean 0 dB */
1720 +#define TSC2301_MICG_SHIFT             8
1721 +
1722 +#define TSC2301_REG_AUDCNTL_MCLK(v)    (((v) & 3) << 6)
1723 +#define TSC2301_REG_AUDCNTL_I2SFS(v)   (((v) & 0xf) << 2)
1724 +#define TSC2301_REG_AUDCNTL_I2SFM(v)   (((v) & 3) << 0)
1725 +
1726 +#define TSC2301_SINGLE(xname, xindex, reg, shadow_index, shift, mask, min) \
1727 +{\
1728 +       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1729 +       .name = xname, \
1730 +       .index = xindex, \
1731 +       .info = snd_tsc2301_info_single, \
1732 +       .get = snd_tsc2301_get_single, \
1733 +       .put = snd_tsc2301_put_single, \
1734 +       .private_value = TSC2301_REG_TO_PVAL(reg) | \
1735 +               (shadow_index << 8) | (shift << 16) | (mask << 24) | \
1736 +               (min << 28) \
1737 +}
1738 +#define TSC2301_SINGLE_MINVAL(v)       (((v) >> 28) & 15)
1739 +#define TSC2301_SINGLE_SHIFT(v)                (((v) >> 16) & 15)
1740 +#define TSC2301_SINGLE_MASK(v)         (((v) >> 24) & 15)
1741 +
1742 +#define TSC2301_DOUBLE(xname, xindex, reg, shadow_index, ls, rs, mask, min) \
1743 +{\
1744 +       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1745 +       .name = xname, \
1746 +       .index = xindex, \
1747 +       .info = snd_tsc2301_info_double, \
1748 +       .get = snd_tsc2301_get_double, \
1749 +       .put = snd_tsc2301_put_double, \
1750 +       .private_value = TSC2301_REG_TO_PVAL(reg) | \
1751 +               (shadow_index << 8) | (min << 11) | \
1752 +               (ls << 16) | (rs << 20) | (mask << 24) \
1753 +}
1754 +#define TSC2301_DOUBLE_MINVAL(v)       (((v) >> 11) & 0x1f)
1755 +#define TSC2301_DOUBLE_LEFT_SHIFT(v)   (((v) >> 16) & 15)
1756 +#define TSC2301_DOUBLE_RIGHT_SHIFT(v)  (((v) >> 20) & 15)
1757 +#define TSC2301_DOUBLE_MASK(v)         (((v) >> 24) & TSC2301_VOLUME_MASK)
1758 +
1759 +#define TSC2301_MUX(xname, xindex, reg, shadow_index, ls, rs, mask) \
1760 +{\
1761 +       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1762 +       .name = xname, \
1763 +       .index = xindex, \
1764 +       .info = snd_tsc2301_info_mux, \
1765 +       .get = snd_tsc2301_get_mux, \
1766 +       .put = snd_tsc2301_put_mux, \
1767 +       .private_value = TSC2301_REG_TO_PVAL(reg) | \
1768 +               (shadow_index << 8) | (ls << 16) | (rs << 20) | (mask << 24) \
1769 +}
1770 +#define TSC2301_MUX_LEFT_SHIFT(v)      (((v) >> 16) & 15)
1771 +#define TSC2301_MUX_RIGHT_SHIFT(v)     (((v) >> 20) & 15)
1772 +#define TSC2301_MUX_MASK(v)            (((v) >> 24) & TSC2301_VOLUME_MASK)
1773 +
1774 +#define TSC2301_BOOL(xname, xindex, reg, shadow_index, shift, invert, state) \
1775 +{ \
1776 +       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1777 +       .name = xname, \
1778 +       .index = xindex, \
1779 +       .info = snd_tsc2301_info_bool, \
1780 +       .get = snd_tsc2301_get_bool, \
1781 +       .put = snd_tsc2301_put_bool, \
1782 +       .private_value = TSC2301_REG_TO_PVAL(reg) | \
1783 +               (shadow_index << 8) | (shift << 16) | \
1784 +               (invert << 24) | (state << 25) \
1785 +}
1786 +#define TSC2301_BOOL_SHIFT(v)          (((v) >> 16) & 7)
1787 +#define TSC2301_BOOL_INVERT(v)         (((v) >> 24) & 1)
1788 +#define TSC2301_BOOL_STATE(v)          (((v) >> 25) & 1)
1789 +
1790 +#define TSC2301_SHADOW_INDEX(v)                (((v) >> 8) & 7)
1791 +
1792 +/*
1793 + * Power-down handler for additional GPIO mixer controls. GPIO state of GPIO
1794 + * controls whose power-down flag is enabled are set to their false/deactivate
1795 + * state
1796 + *
1797 + * Must be called tsc->mixer->mutex locked
1798 + */
1799 +static void tsc2301_gpio_power_down(struct tsc2301 *tsc)
1800 +{
1801 +       struct tsc2301_mixer *mix = tsc->mixer;
1802 +       u16 temp;
1803 +       int i;
1804 +
1805 +       temp = mix->shadow_regs[GPIO_INDEX];
1806 +       for (i = 0; i < mix->n_mixer_gpios; i++) {
1807 +               const struct tsc2301_mixer_gpio *mg;
1808 +
1809 +               mg = mix->mixer_gpios + i;
1810 +               if (mg->deactivate_on_pd) {
1811 +                       int gpio = mg->gpio;
1812 +
1813 +                       temp &= ~(1 << gpio);
1814 +                       temp |= mg->inverted << gpio;
1815 +               }
1816 +       }
1817 +       tsc2301_write_reg(tsc, TSC2301_REG_GPIO, temp);
1818 +}
1819 +
1820 +/*
1821 + * Powers down/up audio blocks which are muted or become unused.
1822 + * shadow_index >= 0, changes power state of single audio block
1823 + * shadow_index < 0, changes power state of all blocks
1824 + *
1825 + * Must be called tsc->mixer->mutex locked
1826 + */
1827 +#define TSC2301_MUTE_MASK \
1828 +       ((1 << TSC2301_MUTE_LEFT_SHIFT) | (1 << TSC2301_MUTE_RIGHT_SHIFT))
1829 +static void tsc2301_power_ctrl(struct tsc2301 *tsc, int shadow_index,
1830 +                              int poll_pdsts)
1831 +{
1832 +       struct tsc2301_mixer *mix = tsc->mixer;
1833 +       u16 pd_ctrl, pd_ctrl_old, w;
1834 +       unsigned long timeout;
1835 +       int power_up = 0;
1836 +
1837 +       if (mix->delayed_pd_active) {
1838 +               mix->delayed_pd_active = 0;
1839 +               mix->mclk_enabled--;
1840 +               cancel_delayed_work(&mix->delayed_power_down);
1841 +       }
1842 +
1843 +       pd_ctrl = pd_ctrl_old = mix->shadow_regs[PD_MISC_INDEX];
1844 +       /* power control helper based on used space mixer selections. See
1845 +        * actual power control decisions below */
1846 +       if (shadow_index < 0 || shadow_index == ADCVOL_INDEX) {
1847 +               /* ADC left and right power down control */
1848 +               if (mix->shadow_regs[ADCVOL_INDEX] &
1849 +                   (1 << TSC2301_MUTE_LEFT_SHIFT))
1850 +                       /* left ADC muted. Power down the left ADC */
1851 +                       pd_ctrl |= TSC2301_REG_PD_MISC_ADPDL;
1852 +               else
1853 +                       pd_ctrl &= ~TSC2301_REG_PD_MISC_ADPDL;
1854 +               if (mix->shadow_regs[ADCVOL_INDEX] &
1855 +                   (1 << TSC2301_MUTE_LEFT_SHIFT))
1856 +                       /* right ADC muted. Power down the right ADC */
1857 +                       pd_ctrl |= TSC2301_REG_PD_MISC_ADPDR;
1858 +               else
1859 +                       pd_ctrl &= ~TSC2301_REG_PD_MISC_ADPDR;
1860 +       }
1861 +       if (shadow_index < 0 || shadow_index == DACVOL_INDEX) {
1862 +               /* DAC power down control */
1863 +               if ((mix->shadow_regs[DACVOL_INDEX] &
1864 +                    TSC2301_MUTE_MASK) == TSC2301_MUTE_MASK)
1865 +                       /* both DACs muted. Power down the DAC */
1866 +                       pd_ctrl |= TSC2301_REG_PD_MISC_DAPD;
1867 +               else
1868 +                       pd_ctrl &= ~TSC2301_REG_PD_MISC_DAPD;
1869 +       }
1870 +       if (shadow_index < 0 || shadow_index == BPVOL_INDEX) {
1871 +               /* line bypass power down control */
1872 +               if ((mix->shadow_regs[BPVOL_INDEX] &
1873 +                    TSC2301_MUTE_MASK) == TSC2301_MUTE_MASK)
1874 +                       /* both line bypasses muted. Power down the bypass
1875 +                        * path */
1876 +                       pd_ctrl |= TSC2301_REG_PD_MISC_ABPD;
1877 +               else
1878 +                       pd_ctrl &= ~TSC2301_REG_PD_MISC_ABPD;
1879 +       }
1880 +       if (shadow_index < 0 || shadow_index == AUDCNTL_INDEX) {
1881 +               /* mic bias power down control */
1882 +               if ((mix->shadow_regs[AUDCNTL_INDEX] &
1883 +                    (3 << TSC2301_INML_SHIFT)) &&
1884 +                   (mix->shadow_regs[AUDCNTL_INDEX] &
1885 +                    (3 << TSC2301_INMR_SHIFT)))
1886 +                       /* both ADC channels use other than mic input. Power
1887 +                        * down the mic bias output */
1888 +                       pd_ctrl |= TSC2301_REG_PD_MISC_MIBPD;
1889 +               else
1890 +                       pd_ctrl &= ~TSC2301_REG_PD_MISC_MIBPD;
1891 +       }
1892 +
1893 +       /* power control decisions based on codec usage and user space mixer
1894 +        * selections detected above */
1895 +       pd_ctrl &= ~TSC2301_REG_PD_MISC_APD; /* audio not powered down */
1896 +       if (mix->mclk_enabled) {
1897 +               if (!mix->adc_enabled) {
1898 +                       /* ADC not used, power down both ADC's and mic bias
1899 +                        * output independently of user space mixer
1900 +                        * selections */
1901 +                       pd_ctrl |= TSC2301_REG_PD_MISC_ADPDL;
1902 +                       pd_ctrl |= TSC2301_REG_PD_MISC_ADPDR;
1903 +                       pd_ctrl |= TSC2301_REG_PD_MISC_MIBPD;
1904 +               }
1905 +               if (!mix->dac_enabled) {
1906 +                       /* DAC not used, power down DAC independently of user
1907 +                        * space mixer selections */
1908 +                       pd_ctrl |= TSC2301_REG_PD_MISC_DAPD;
1909 +               }
1910 +
1911 +               if (mix->pll_output) {
1912 +                       /* GPIO_0 is configured as PLL output so audio
1913 +                        * controller is expecting clock from TSC2301. Either
1914 +                        * ADC or DAC must be active in order to keep PLL on */
1915 +                       if ((pd_ctrl & TSC2301_REG_PD_MISC_ADPDL) &&
1916 +                           (pd_ctrl & TSC2301_REG_PD_MISC_ADPDR) &&
1917 +                           (pd_ctrl & TSC2301_REG_PD_MISC_DAPD)) {
1918 +                               /* neither ADC or DAC used. Force ADC on in
1919 +                                * order to keep PLL active */
1920 +                               pd_ctrl &= ~(TSC2301_REG_PD_MISC_ADPDL |
1921 +                                            TSC2301_REG_PD_MISC_ADPDR);
1922 +                       }
1923 +               }
1924 +       } else {
1925 +               /* audio input clock is not enabled so power down DAC and ADC
1926 +                * in order to shutdown PLL and to keep touchscreen and keypad
1927 +                * parts working. Touchscreen and keypad use audio clock when
1928 +                * PLL is on and internal clock otherwise */
1929 +               pd_ctrl |= TSC2301_REG_PD_MISC_DAPD |
1930 +                          TSC2301_REG_PD_MISC_ADPDL |
1931 +                          TSC2301_REG_PD_MISC_ADPDR;
1932 +       }
1933 +
1934 +       if ((pd_ctrl & TSC2301_REG_PD_MISC_ADPDL) &&
1935 +           (pd_ctrl & TSC2301_REG_PD_MISC_ADPDR) &&
1936 +           (pd_ctrl & TSC2301_REG_PD_MISC_DAPD) &&
1937 +           (pd_ctrl & TSC2301_REG_PD_MISC_ABPD)) {
1938 +               /* all ADC, DAC and line bypass path unused. Power down the
1939 +                * whole audio part of the TSC2301 */
1940 +               pd_ctrl |= TSC2301_REG_PD_MISC_APD;
1941 +       }
1942 +
1943 +       if (pd_ctrl == pd_ctrl_old)
1944 +               return;
1945 +
1946 +       /* power down control changed. Update into TSC2301 */
1947 +       if ((pd_ctrl ^ pd_ctrl_old) & TSC2301_REG_PD_MISC_APD) {
1948 +               /* whole audio power state changed. Update GPIO states */
1949 +               if (pd_ctrl & TSC2301_REG_PD_MISC_APD) {
1950 +                       /* power down GPIO controls before powering down
1951 +                        * the codec */
1952 +                       tsc2301_gpio_power_down(tsc);
1953 +                       /* we must really ensure that codec has been on no less
1954 +                        * than 100 msec before doing power-down */
1955 +                       timeout = mix->pu_jiffies + TSC2301_MIN_PU_PERIOD -
1956 +                                 jiffies;
1957 +                       if (timeout <= TSC2301_MIN_PU_PERIOD) {
1958 +                               mix->delayed_pd_active = 1;
1959 +                               mix->mclk_enabled++;
1960 +                               schedule_delayed_work(&mix->delayed_power_down,
1961 +                                                     timeout + 1);
1962 +                               return;
1963 +                       }
1964 +               } else
1965 +                       /* restore GPIOs after codec is powered up */
1966 +                       power_up = 1;
1967 +       }
1968 +       mix->shadow_regs[PD_MISC_INDEX] = pd_ctrl;
1969 +       tsc2301_write_reg(tsc, TSC2301_REG_PD_MISC, pd_ctrl);
1970 +       if (power_up)
1971 +               mix->pu_jiffies = jiffies;
1972 +       if (!poll_pdsts) {
1973 +               if (power_up)
1974 +                       tsc2301_write_reg(tsc, TSC2301_REG_GPIO,
1975 +                                         mix->shadow_regs[GPIO_INDEX]);
1976 +               return;
1977 +       }
1978 +
1979 +       /* wait until power-up/-down is completed */
1980 +       timeout = jiffies + msecs_to_jiffies(100);
1981 +       w = 0;
1982 +       do {
1983 +               if (time_after(jiffies, timeout)) {
1984 +                       /* Print a warning only if the I2S clock is not
1985 +                        * present / out of sync. This can happen during
1986 +                        * init time, when that clock will be turned on
1987 +                        * by another driver like in the OMAP EAC with
1988 +                        * external clock case.
1989 +                        */
1990 +                       if (w & TSC2301_REG_PD_MISC_OTSYN) {
1991 +                               dev_warn(&tsc->spi->dev,
1992 +                                  "I2S clock not in sync or off.\n");
1993 +                       } else {
1994 +                               dev_err(&tsc->spi->dev,
1995 +                                  "power-up/-down timed out "
1996 +                                  "(0x%04x, 0x%04x -> 0x%04x)\n",
1997 +                                  w, pd_ctrl_old, pd_ctrl);
1998 +                       }
1999 +                       goto out;
2000 +               }
2001 +               w = tsc2301_read_reg(tsc, TSC2301_REG_PD_MISC);
2002 +       } while (!(w & TSC2301_REG_PD_MISC_PDSTS));
2003 +
2004 +out:
2005 +       if (((pd_ctrl ^ pd_ctrl_old) & TSC2301_REG_PD_MISC_DAPD) &&
2006 +           !(pd_ctrl & TSC2301_REG_PD_MISC_DAPD)) {
2007 +               /* DAC powered up now. Ensure that DAC and audio outputs are
2008 +                * activated. They are up 100 msec after the chip power-up
2009 +                * command */
2010 +               timeout = mix->pu_jiffies + TSC2301_DAC_DELAY - jiffies;
2011 +               if (timeout <= TSC2301_DAC_DELAY)
2012 +                       schedule_timeout_interruptible(timeout);
2013 +               /* FIXME: This is lazy. We restore GPIOs only after activating
2014 +                * the DAC. It would be better to do some kind of delayed GPIO
2015 +                * restore. That ensures that we restore them also if only ADC
2016 +                * path is activated. But this is required only if there is
2017 +                * some input amplifier, bias control, etc. and their power
2018 +                * state is under TSC GPIO control */
2019 +               tsc2301_write_reg(tsc, TSC2301_REG_GPIO,
2020 +                                 mix->shadow_regs[GPIO_INDEX]);
2021 +       }
2022 +}
2023 +
2024 +static int snd_tsc2301_info_single(struct snd_kcontrol *kcontrol,
2025 +                                  struct snd_ctl_elem_info *uinfo)
2026 +{
2027 +       int mask = TSC2301_SINGLE_MASK(kcontrol->private_value);
2028 +       int minval = TSC2301_SINGLE_MINVAL(kcontrol->private_value);
2029 +
2030 +       uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2031 +       uinfo->count = 1;
2032 +       uinfo->value.integer.min = minval;
2033 +       uinfo->value.integer.max = mask;
2034 +
2035 +       return 0;
2036 +}
2037 +
2038 +static int snd_tsc2301_get_single(struct snd_kcontrol *kcontrol,
2039 +                                 struct snd_ctl_elem_value *ucontrol)
2040 +{
2041 +       struct tsc2301 *tsc = kcontrol->private_data;
2042 +       unsigned long priv = kcontrol->private_value;
2043 +       int mask = TSC2301_SINGLE_MASK(priv);
2044 +       int shift = TSC2301_SINGLE_SHIFT(priv);
2045 +       int shadow_index = TSC2301_SHADOW_INDEX(priv);
2046 +       u16 shadow_reg;
2047 +
2048 +       shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2049 +
2050 +       ucontrol->value.integer.value[0] = (shadow_reg >> shift) & mask;
2051 +
2052 +       return 0;
2053 +}
2054 +
2055 +static int snd_tsc2301_put_single(struct snd_kcontrol *kcontrol,
2056 +                                 struct snd_ctl_elem_value *ucontrol)
2057 +{
2058 +       struct tsc2301 *tsc = kcontrol->private_data;
2059 +       unsigned long priv = kcontrol->private_value;
2060 +       int mask = TSC2301_SINGLE_MASK(priv);
2061 +       int shadow_index = TSC2301_SHADOW_INDEX(priv);
2062 +       u16 shadow_reg, shadow_reg_old;
2063 +       int shift = TSC2301_SINGLE_SHIFT(priv);
2064 +       int reg = TSC2301_PVAL_TO_REG(priv);
2065 +       int changed;
2066 +
2067 +       mutex_lock(&tsc->mixer->mutex);
2068 +       shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2069 +
2070 +       /* zero bits to be modified */
2071 +       shadow_reg &= ~(mask << shift);
2072 +       /* modify with new value */
2073 +       shadow_reg |= ((ucontrol->value.integer.value[0] & mask) << shift);
2074 +
2075 +       changed = (shadow_reg != shadow_reg_old);
2076 +       tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2077 +
2078 +       /* update into TSC2301 if necessary */
2079 +       if (changed)
2080 +               tsc2301_write_reg(tsc, reg, shadow_reg);
2081 +       mutex_unlock(&tsc->mixer->mutex);
2082 +
2083 +       return changed;
2084 +}
2085 +
2086 +static int snd_tsc2301_info_double(struct snd_kcontrol *kcontrol,
2087 +                                  struct snd_ctl_elem_info *uinfo)
2088 +{
2089 +       /* mask == 1 : Switch
2090 +        * mask > 1 : Max volume */
2091 +       int mask = TSC2301_DOUBLE_MASK(kcontrol->private_value);
2092 +       int minval = TSC2301_DOUBLE_MINVAL(kcontrol->private_value);
2093 +
2094 +       uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN :
2095 +               SNDRV_CTL_ELEM_TYPE_INTEGER;
2096 +       uinfo->count = 2;
2097 +       uinfo->value.integer.min = minval;
2098 +       uinfo->value.integer.max = mask;
2099 +
2100 +       return 0;
2101 +}
2102 +
2103 +static int snd_tsc2301_get_double(struct snd_kcontrol *kcontrol,
2104 +                                 struct snd_ctl_elem_value *ucontrol)
2105 +{
2106 +       struct tsc2301 *tsc = kcontrol->private_data;
2107 +       unsigned long priv = kcontrol->private_value;
2108 +       /* mask == 1 : Switch
2109 +        * mask > 1 : Volume */
2110 +       int mask = TSC2301_DOUBLE_MASK(priv);
2111 +       int ls = TSC2301_DOUBLE_LEFT_SHIFT(priv);
2112 +       int rs = TSC2301_DOUBLE_RIGHT_SHIFT(priv);
2113 +       int shadow_index = TSC2301_SHADOW_INDEX(priv);
2114 +       u16 shadow_reg;
2115 +
2116 +       shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2117 +
2118 +       /* invert mute bits for the switches */
2119 +       if (mask == 1)
2120 +               shadow_reg = ~shadow_reg;
2121 +
2122 +       ucontrol->value.integer.value[0] = (shadow_reg >> ls) & mask;
2123 +       ucontrol->value.integer.value[1] = (shadow_reg >> rs) & mask;
2124 +
2125 +       return 0;
2126 +}
2127 +
2128 +static int snd_tsc2301_put_double(struct snd_kcontrol *kcontrol,
2129 +                                 struct snd_ctl_elem_value *ucontrol)
2130 +{
2131 +       struct tsc2301 *tsc = kcontrol->private_data;
2132 +       unsigned long priv = kcontrol->private_value;
2133 +       /* mask == 1 : Switch
2134 +        * mask > 1 : Volume */
2135 +       int mask = TSC2301_DOUBLE_MASK(priv);
2136 +       int shadow_index = TSC2301_SHADOW_INDEX(priv);
2137 +       u16 shadow_reg, shadow_reg_old;
2138 +       int ls = TSC2301_DOUBLE_LEFT_SHIFT(priv);
2139 +       int rs = TSC2301_DOUBLE_RIGHT_SHIFT(priv);
2140 +       int reg = TSC2301_PVAL_TO_REG(priv);
2141 +       int changed;
2142 +
2143 +       mutex_lock(&tsc->mixer->mutex);
2144 +       shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2145 +
2146 +       /* zero bits to be modified */
2147 +       shadow_reg &= ~((mask << ls) | (mask << rs));
2148 +       /* modify with new value */
2149 +       if (mask == 1) {
2150 +               /* switch. Invert switch values for the mute bits */
2151 +               shadow_reg |=
2152 +                       ((~ucontrol->value.integer.value[0] & mask) << ls) |
2153 +                       ((~ucontrol->value.integer.value[1] & mask) << rs);
2154 +       } else {
2155 +               /* volume */
2156 +               shadow_reg |=
2157 +                       (ucontrol->value.integer.value[0] << ls) |
2158 +                       (ucontrol->value.integer.value[1] << rs);
2159 +       }
2160 +
2161 +       changed = (shadow_reg != shadow_reg_old);
2162 +       tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2163 +
2164 +       /* update into TSC2301 if necessary */
2165 +       if (changed)
2166 +               tsc2301_write_reg(tsc, reg, shadow_reg);
2167 +
2168 +       if (mask == 1)
2169 +               /* check is need to power down/up audio blocks in case of
2170 +                * muted state change */
2171 +               tsc2301_power_ctrl(tsc, shadow_index, 0);
2172 +       mutex_unlock(&tsc->mixer->mutex);
2173 +
2174 +       return changed;
2175 +}
2176 +
2177 +static int snd_tsc2301_info_mux(struct snd_kcontrol *kcontrol,
2178 +                               struct snd_ctl_elem_info *uinfo)
2179 +{
2180 +       static char *texts[4] = {"Mic", "Line", "Line swapped", "Line mono"};
2181 +
2182 +       uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2183 +       uinfo->count = 2;
2184 +       uinfo->value.enumerated.items = 4;
2185 +       if (uinfo->value.enumerated.item > 3)
2186 +               uinfo->value.enumerated.item = 3;
2187 +       strcpy(uinfo->value.enumerated.name,
2188 +              texts[uinfo->value.enumerated.item]);
2189 +
2190 +       return 0;
2191 +}
2192 +
2193 +static int snd_tsc2301_get_mux(struct snd_kcontrol *kcontrol,
2194 +                              struct snd_ctl_elem_value *ucontrol)
2195 +{
2196 +       struct tsc2301 *tsc = kcontrol->private_data;
2197 +       unsigned long priv = kcontrol->private_value;
2198 +       int mask = TSC2301_MUX_MASK(priv);
2199 +       int ls = TSC2301_MUX_LEFT_SHIFT(priv);
2200 +       int rs = TSC2301_MUX_RIGHT_SHIFT(priv);
2201 +       int shadow_index = TSC2301_SHADOW_INDEX(priv);
2202 +       u16 shadow_reg;
2203 +
2204 +       shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2205 +       ucontrol->value.enumerated.item[0] = (shadow_reg >> ls) & mask;
2206 +       ucontrol->value.enumerated.item[1] = (shadow_reg >> rs) & mask;
2207 +
2208 +       return 0;
2209 +}
2210 +
2211 +static int snd_tsc2301_put_mux(struct snd_kcontrol *kcontrol,
2212 +                              struct snd_ctl_elem_value *ucontrol)
2213 +{
2214 +       struct tsc2301 *tsc = kcontrol->private_data;
2215 +       unsigned long priv = kcontrol->private_value;
2216 +       int mask = TSC2301_MUX_MASK(priv);
2217 +       int shadow_index = TSC2301_SHADOW_INDEX(priv);
2218 +       u16 shadow_reg, shadow_reg_old;
2219 +       int ls = TSC2301_MUX_LEFT_SHIFT(priv);
2220 +       int rs = TSC2301_MUX_RIGHT_SHIFT(priv);
2221 +       int reg = TSC2301_PVAL_TO_REG(priv);
2222 +       int changed;
2223 +
2224 +       mutex_lock(&tsc->mixer->mutex);
2225 +       shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2226 +
2227 +       /* zero bits to be modified */
2228 +       shadow_reg &= ~((mask << ls) | (mask << rs));
2229 +       /* modify with new value */
2230 +       shadow_reg |= (ucontrol->value.enumerated.item[0] << ls);
2231 +       shadow_reg |= (ucontrol->value.enumerated.item[1] << rs);
2232 +
2233 +       changed = (shadow_reg != shadow_reg_old);
2234 +
2235 +       /* update into TSC2301 if necessary */
2236 +       if (changed) {
2237 +               tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2238 +               tsc2301_write_reg(tsc, reg, shadow_reg);
2239 +       }
2240 +
2241 +       /* check is need to power up/down audio blocks in case of ADC input
2242 +        * change */
2243 +       tsc2301_power_ctrl(tsc, shadow_index, 0);
2244 +       mutex_unlock(&tsc->mixer->mutex);
2245 +
2246 +       return changed;
2247 +}
2248 +
2249 +static int snd_tsc2301_info_bool(struct snd_kcontrol *kcontrol,
2250 +                                struct snd_ctl_elem_info *uinfo)
2251 +{
2252 +       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2253 +       uinfo->count = 1;
2254 +       uinfo->value.integer.min = 0;
2255 +       uinfo->value.integer.max = 1;
2256 +
2257 +       return 0;
2258 +}
2259 +
2260 +static int snd_tsc2301_get_bool(struct snd_kcontrol *kcontrol,
2261 +                               struct snd_ctl_elem_value *ucontrol)
2262 +{
2263 +       struct tsc2301 *tsc = kcontrol->private_data;
2264 +       unsigned long priv = kcontrol->private_value;
2265 +       int shadow_index = TSC2301_SHADOW_INDEX(priv);
2266 +       int shift = TSC2301_BOOL_SHIFT(priv);
2267 +       int invert = TSC2301_BOOL_INVERT(priv);
2268 +       u16 shadow_reg;
2269 +
2270 +       shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2271 +       ucontrol->value.integer.value[0] =
2272 +               invert ^ ((shadow_reg >> shift) & 1);
2273 +
2274 +       return 0;
2275 +}
2276 +
2277 +static int snd_tsc2301_put_bool(struct snd_kcontrol *kcontrol,
2278 +                               struct snd_ctl_elem_value *ucontrol)
2279 +{
2280 +       struct tsc2301 *tsc = kcontrol->private_data;
2281 +       unsigned long priv = kcontrol->private_value;
2282 +       int shadow_index = TSC2301_SHADOW_INDEX(priv);
2283 +       int shift = TSC2301_BOOL_SHIFT(priv);
2284 +       int invert = TSC2301_BOOL_INVERT(priv);
2285 +       int reg = TSC2301_PVAL_TO_REG(priv);
2286 +       u16 shadow_reg, shadow_reg_old;
2287 +       int changed;
2288 +
2289 +       mutex_lock(&tsc->mixer->mutex);
2290 +       shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2291 +
2292 +       /* zero bit to be modified */
2293 +       shadow_reg &= ~(1 << shift);
2294 +       /* modify with new value */
2295 +       shadow_reg |=
2296 +               (invert ^ (ucontrol->value.integer.value[0] & 1)) << shift;
2297 +
2298 +       changed = (shadow_reg != shadow_reg_old);
2299 +
2300 +       /* update into TSC2301 if necessary */
2301 +       if (changed) {
2302 +               tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2303 +               if ((shadow_index == GPIO_INDEX) &&
2304 +                   (tsc->mixer->shadow_regs[PD_MISC_INDEX] &
2305 +                    TSC2301_REG_PD_MISC_APD)) {
2306 +                       /* changing GPIO control and audio is powered down.
2307 +                        * Update GPIO states according to their power-down
2308 +                        * flag */
2309 +                       tsc2301_gpio_power_down(tsc);
2310 +               } else
2311 +                       tsc2301_write_reg(tsc, reg, shadow_reg);
2312 +       }
2313 +       mutex_unlock(&tsc->mixer->mutex);
2314 +
2315 +       return changed;
2316 +}
2317 +
2318 +/* TSC2301 internal mixer controls */
2319 +static struct snd_kcontrol_new snd_tsc2301_controls[] = {
2320 +       /* stereo ADC input switches and volumes */
2321 +       TSC2301_DOUBLE("Capture Switch", 0,
2322 +               TSC2301_REG_ADCVOL, ADCVOL_INDEX,
2323 +               TSC2301_MUTE_LEFT_SHIFT, TSC2301_MUTE_RIGHT_SHIFT,
2324 +               1, 0),
2325 +       TSC2301_DOUBLE("Capture Volume", 0,
2326 +               TSC2301_REG_ADCVOL, ADCVOL_INDEX,
2327 +               TSC2301_VOL_LEFT_SHIFT, TSC2301_VOL_RIGHT_SHIFT,
2328 +               TSC2301_VOLUME_MASK, TSC2301_MIN_ADCVOL),
2329 +
2330 +       /* stereo DAC output switches and volumes */
2331 +       TSC2301_DOUBLE("PCM Playback Switch", 0,
2332 +               TSC2301_REG_DACVOL, DACVOL_INDEX,
2333 +               TSC2301_MUTE_LEFT_SHIFT, TSC2301_MUTE_RIGHT_SHIFT,
2334 +               1, 0),
2335 +       TSC2301_DOUBLE("PCM Playback Volume", 0,
2336 +               TSC2301_REG_DACVOL, DACVOL_INDEX,
2337 +               TSC2301_VOL_LEFT_SHIFT, TSC2301_VOL_RIGHT_SHIFT,
2338 +               TSC2301_VOLUME_MASK, TSC2301_MIN_DACVOL),
2339 +
2340 +       /* stereo line input bypass switches and volumes */
2341 +       TSC2301_DOUBLE("Line Playback Switch", 0,
2342 +               TSC2301_REG_BPVOL, BPVOL_INDEX,
2343 +               TSC2301_MUTE_LEFT_SHIFT, TSC2301_MUTE_RIGHT_SHIFT,
2344 +               1, 0),
2345 +       TSC2301_DOUBLE("Line Playback Volume", 0,
2346 +               TSC2301_REG_BPVOL, BPVOL_INDEX,
2347 +               TSC2301_VOL_LEFT_SHIFT, TSC2301_VOL_RIGHT_SHIFT,
2348 +               TSC2301_VOLUME_MASK, TSC2301_MIN_BPVOL),
2349 +
2350 +       /* mono microphone input gain */
2351 +       TSC2301_SINGLE("Mic Boost", 0,
2352 +               TSC2301_REG_AUDCNTL, AUDCNTL_INDEX,
2353 +               TSC2301_MICG_SHIFT,
2354 +               TSC2301_MICG_MASK, TSC2301_MICG_MIN),
2355 +
2356 +       /* ADC input sources. Both channels could be selected separately */
2357 +       TSC2301_MUX("Capture Source", 0,
2358 +               TSC2301_REG_AUDCNTL, AUDCNTL_INDEX,
2359 +               TSC2301_INML_SHIFT, TSC2301_INMR_SHIFT,
2360 +               TSC2301_INM_MASK),
2361 +};
2362 +
2363 +/* must be called tsc->mixer->mutex locked */
2364 +static void tsc2301_flush_shadow_regs(struct tsc2301 *tsc)
2365 +{
2366 +       int i, page, addr;
2367 +       u16 temp;
2368 +
2369 +       page = TSC2301_REG_TO_PAGE(TSC2301_REG_AUDCNTL);
2370 +       addr = TSC2301_REG_TO_ADDR(TSC2301_REG_AUDCNTL);
2371 +
2372 +       for (i = 0; i < 4; i++) {
2373 +               temp = tsc->mixer->shadow_regs[i];
2374 +               tsc2301_write_reg(tsc, TSC2301_REG(page, addr + i), temp);
2375 +       }
2376 +       temp = tsc->mixer->shadow_regs[GPIO_INDEX];
2377 +       tsc2301_write_reg(tsc, TSC2301_REG_GPIO, temp);
2378 +
2379 +       /* Update power state of all audio blocks depending are they
2380 +        * muted or unused. */
2381 +       tsc2301_power_ctrl(tsc, -1, 0);
2382 +}
2383 +
2384 +#ifdef CONFIG_PM
2385 +int tsc2301_mixer_suspend(struct tsc2301 *tsc)
2386 +{
2387 +       /* power down entire audio section inside TSC2301 in case the
2388 +        * chip is still powered during the system sleep. However this driver
2389 +        * doesn't require that chip is powered because registers are restored
2390 +        * in function tsc2301_mixer_resume */
2391 +       mutex_lock(&tsc->mixer->mutex);
2392 +       tsc2301_gpio_power_down(tsc);
2393 +       tsc->mixer->shadow_regs[PD_MISC_INDEX] |= TSC2301_REG_PD_MISC_APD;
2394 +       tsc2301_write_reg(tsc, TSC2301_REG_PD_MISC,
2395 +                         tsc->mixer->shadow_regs[PD_MISC_INDEX]);
2396 +       mutex_unlock(&tsc->mixer->mutex);
2397 +       return 0;
2398 +}
2399 +
2400 +void tsc2301_mixer_resume(struct tsc2301 *tsc)
2401 +{
2402 +       /* power up the TSC2301 audio section and restore registers */
2403 +       mutex_lock(&tsc->mixer->mutex);
2404 +       tsc->mixer->shadow_regs[PD_MISC_INDEX] &= ~TSC2301_REG_PD_MISC_APD;
2405 +       tsc2301_flush_shadow_regs(tsc);
2406 +       mutex_unlock(&tsc->mixer->mutex);
2407 +}
2408 +#endif
2409 +
2410 +void tsc2301_mixer_enable_mclk(struct device *dev)
2411 +{
2412 +       struct tsc2301 *tsc = dev_get_drvdata(dev);
2413 +       struct tsc2301_mixer *mix = tsc->mixer;
2414 +
2415 +       mutex_lock(&mix->mutex);
2416 +       if (!mix->mclk_enabled++ && tsc->enable_clock != NULL) {
2417 +               tsc->enable_clock(dev);
2418 +       }
2419 +       tsc2301_power_ctrl(tsc, -1, 1);
2420 +       mutex_unlock(&mix->mutex);
2421 +}
2422 +
2423 +void tsc2301_mixer_disable_mclk(struct device *dev)
2424 +{
2425 +       struct tsc2301 *tsc = dev_get_drvdata(dev);
2426 +       struct tsc2301_mixer *mix = tsc->mixer;
2427 +
2428 +       mutex_lock(&mix->mutex);
2429 +       mix->mclk_enabled--;
2430 +       tsc2301_power_ctrl(tsc, -1, 1);
2431 +       if (!mix->mclk_enabled && tsc->disable_clock != NULL) {
2432 +               tsc->disable_clock(dev);
2433 +       }
2434 +       mutex_unlock(&mix->mutex);
2435 +}
2436 +
2437 +static void tsc2301_mixer_delayed_power_down(struct work_struct *work)
2438 +{
2439 +       struct tsc2301_mixer *mix = container_of(work, struct tsc2301_mixer,
2440 +                                                delayed_power_down.work);
2441 +       struct tsc2301 *tsc = mix->tsc;
2442 +
2443 +       mutex_lock(&mix->mutex);
2444 +       if (!mix->delayed_pd_active) {
2445 +               mutex_unlock(&mix->mutex);
2446 +               return;
2447 +       }
2448 +       mix->delayed_pd_active = 0;
2449 +       mutex_unlock(&mix->mutex);
2450 +       tsc2301_mixer_disable_mclk(&tsc->spi->dev);
2451 +}
2452 +
2453 +/*
2454 + * Allows audio controller driver to notify its usage of ADC and DAC
2455 + */
2456 +void tsc2301_mixer_set_power(struct device *dev, int dac, int adc)
2457 +{
2458 +       struct tsc2301 *tsc = dev_get_drvdata(dev);
2459 +
2460 +       mutex_lock(&tsc->mixer->mutex);
2461 +       tsc->mixer->adc_enabled = adc;
2462 +       tsc->mixer->dac_enabled = dac;
2463 +
2464 +       /* update power state of all audio blocks */
2465 +       tsc2301_power_ctrl(tsc, -1, 1);
2466 +       mutex_unlock(&tsc->mixer->mutex);
2467 +}
2468 +
2469 +/*
2470 + * Registers TSC2301 ALSA Mixer controls for the given sound card
2471 + */
2472 +int tsc2301_mixer_register_controls(struct device *dev, struct snd_card *card)
2473 +{
2474 +       struct tsc2301 *tsc = dev_get_drvdata(dev);
2475 +       struct tsc2301_mixer *mix = tsc->mixer;
2476 +       int i, err;
2477 +
2478 +       /* Register ALSA mixer controls */
2479 +       for (i = 0; i < ARRAY_SIZE(snd_tsc2301_controls); i++) {
2480 +               err = snd_ctl_add(card,
2481 +                                 snd_ctl_new1(&snd_tsc2301_controls[i], tsc));
2482 +               if (err < 0)
2483 +                       return err;
2484 +       }
2485 +
2486 +       if (!mix->n_mixer_gpios)
2487 +               return 0;
2488 +
2489 +       /* Register additional GPIO controls if defined */
2490 +       for (i = 0; i < mix->n_mixer_gpios; i++) {
2491 +               const struct tsc2301_mixer_gpio *mg = mix->mixer_gpios + i;
2492 +               struct snd_kcontrol *ctrlp;
2493 +               struct snd_kcontrol_new ctrl =
2494 +                       TSC2301_BOOL((char *)mg->name, 0,
2495 +                                    TSC2301_REG_GPIO, GPIO_INDEX,
2496 +                                    mg->gpio, mg->inverted, mg->def_enable);
2497 +
2498 +               ctrlp = snd_ctl_new1(&ctrl, tsc);
2499 +               err = snd_ctl_add(card, ctrlp);
2500 +               if (err < 0)
2501 +                       return err;
2502 +       }
2503 +
2504 +       return 0;
2505 +}
2506 +
2507 +int tsc2301_mixer_init(struct tsc2301 *tsc,
2508 +                      struct tsc2301_platform_data *pdata)
2509 +{
2510 +       struct tsc2301_mixer *mix;
2511 +       int err = 0;
2512 +       u16 w;
2513 +
2514 +       mix = kzalloc(sizeof(*mix), GFP_KERNEL);
2515 +       if (mix == NULL)
2516 +               return -ENOMEM;
2517 +       tsc->mixer = mix;
2518 +
2519 +       mix->tsc = tsc;
2520 +       mutex_init(&mix->mutex);
2521 +       mix->platform_init = pdata->codec_init;
2522 +       mix->platform_cleanup = pdata->codec_cleanup;
2523 +       mix->pll_output = pdata->pll_output;
2524 +
2525 +       INIT_DELAYED_WORK(&mix->delayed_power_down,
2526 +                         tsc2301_mixer_delayed_power_down);
2527 +
2528 +       /* initialize shadow register default values */
2529 +       w = 0xc000;
2530 +       w |= (pdata->mclk_ratio << 6) | (pdata->i2s_sample_rate << 2);
2531 +       w |= pdata->i2s_format;
2532 +       mix->shadow_regs[AUDCNTL_INDEX] = w;
2533 +       mix->shadow_regs[ADCVOL_INDEX] = 0xd7d7;
2534 +       mix->shadow_regs[DACVOL_INDEX] = 0xffff;
2535 +       mix->shadow_regs[BPVOL_INDEX] = 0xe7e7;
2536 +       mix->shadow_regs[PD_MISC_INDEX] = pdata->power_down_blocks;
2537 +
2538 +       /* if extra mixer controls configured, then configure associated
2539 +        * GPIOs as output and drive their default state */
2540 +       if (pdata->n_mixer_gpios) {
2541 +               int i;
2542 +
2543 +               w = 0;
2544 +               for (i = 0; i < pdata->n_mixer_gpios; i++) {
2545 +                       const struct tsc2301_mixer_gpio *mg;
2546 +                       int gpio;
2547 +
2548 +                       mg = pdata->mixer_gpios + i;
2549 +                       gpio = mg->gpio;
2550 +                       w |= (1 << gpio) << 8;
2551 +                       w |= (mg->inverted ^ mg->def_enable) << gpio;
2552 +               }
2553 +               mix->shadow_regs[GPIO_INDEX] = w;
2554 +
2555 +               mix->mixer_gpios = kmalloc(sizeof(*pdata->mixer_gpios) *
2556 +                                          pdata->n_mixer_gpios,
2557 +                                          GFP_KERNEL);
2558 +               if (mix->mixer_gpios == NULL) {
2559 +                       err = -ENOMEM;
2560 +                       goto err1;
2561 +               }
2562 +               memcpy(mix->mixer_gpios, pdata->mixer_gpios,
2563 +                      sizeof(*pdata->mixer_gpios) * pdata->n_mixer_gpios);
2564 +               mix->n_mixer_gpios = pdata->n_mixer_gpios;
2565 +       }
2566 +
2567 +       /* PLL control */
2568 +       tsc2301_write_pll(tsc, pdata->pll_n, pdata->pll_a, pdata->pll_pdc,
2569 +                         0, mix->pll_output ? 0 : 1);
2570 +
2571 +       tsc2301_flush_shadow_regs(tsc);
2572 +
2573 +       if (mix->platform_init != NULL) {
2574 +               err = mix->platform_init(&tsc->spi->dev);
2575 +               if (err < 0)
2576 +                       goto err2;
2577 +       }
2578 +
2579 +       return 0;
2580 +err2:
2581 +       if (mix->mixer_gpios != NULL)
2582 +               kfree(mix->mixer_gpios);
2583 +err1:
2584 +       kfree(mix);
2585 +       return err;
2586 +}
2587 +
2588 +void tsc2301_mixer_exit(struct tsc2301 *tsc)
2589 +{
2590 +       struct tsc2301_mixer *mixer = tsc->mixer;
2591 +
2592 +       if (mixer->platform_cleanup != NULL)
2593 +               mixer->platform_cleanup(&tsc->spi->dev);
2594 +
2595 +       if (mixer->mixer_gpios != NULL)
2596 +               kfree(mixer->mixer_gpios);
2597 +}
2598 +
2599 +MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
2600 +MODULE_LICENSE("GPL");
2601 Index: linux-3.1/include/linux/spi/tsc2301.h
2602 ===================================================================
2603 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
2604 +++ linux-3.1/include/linux/spi/tsc2301.h       2011-10-30 00:48:48.989043470 +0200
2605 @@ -0,0 +1,208 @@
2606 +#ifndef _LINUX_SPI_TSC2301_H
2607 +#define _LINUX_SPI_TSC2301_H
2608 +
2609 +#include <linux/types.h>
2610 +#include <linux/timer.h>
2611 +
2612 +struct tsc2301_platform_data {
2613 +       /*
2614 +        * Keypad
2615 +        */
2616 +       s16     reset_gpio;
2617 +       s16     keyb_int;
2618 +       s16     keymap[16];     /* Set a key to a negative value if not used */
2619 +       unsigned kp_rep:1;      /* Enable keypad repeating */
2620 +       char    *keyb_name;     /* Keyboard device name */
2621 +
2622 +       /*
2623 +        * Touchscreen
2624 +        */
2625 +       s16     dav_int;
2626 +       u16     ts_x_plate_ohm;
2627 +       u32     ts_stab_time;   /* voltage settling time */
2628 +       u8      ts_hw_avg;      /* HW assiseted averaging. Can be
2629 +                                  0, 4, 8, 16 samples per reading */
2630 +       u32     ts_max_pressure;/* Samples with bigger pressure value will
2631 +                                  be ignored, since the corresponding X, Y
2632 +                                  values are unreliable */
2633 +       u32     ts_touch_pressure;      /* Pressure limit until we report a
2634 +                                          touch event. After that we switch
2635 +                                          to ts_max_pressure. */
2636 +       u32     ts_pressure_fudge;
2637 +       u32     ts_x_max;
2638 +       u32     ts_x_fudge;
2639 +       u32     ts_y_max;
2640 +       u32     ts_y_fudge;
2641 +
2642 +       /*
2643 +        * Audio
2644 +        */
2645 +       unsigned        pll_pdc:4;
2646 +       unsigned        pll_a:4;
2647 +       unsigned        pll_n:4;
2648 +       unsigned        pll_output:1; /* Output PLL on GPIO_0 */
2649 +
2650 +       unsigned        mclk_ratio:2;
2651 +       unsigned        i2s_sample_rate:4;
2652 +       unsigned        i2s_format:2;
2653 +       /* Mask for audio blocks to be powered down */
2654 +       u16             power_down_blocks;
2655 +
2656 +       /* Called after codec has been initialized, can be NULL */
2657 +       int (* codec_init)(struct device *tsc2301_dev);
2658 +       /* Called when codec is being removed, can be NULL */
2659 +       void (* codec_cleanup)(struct device *tsc2301_dev);
2660 +       int     (*enable_clock)(struct device *dev);
2661 +       void    (*disable_clock)(struct device *dev);
2662 +
2663 +       const struct tsc2301_mixer_gpio {
2664 +               const char      *name;
2665 +               unsigned        gpio:4;
2666 +               unsigned        inverted:1;
2667 +               unsigned        def_enable:1; /* enable by default */
2668 +               unsigned        deactivate_on_pd:1; /* power-down flag */
2669 +       } *mixer_gpios;
2670 +       int     n_mixer_gpios;
2671 +};
2672 +
2673 +struct tsc2301_kp;
2674 +struct tsc2301_ts;
2675 +struct tsc2301_mixer;
2676 +
2677 +struct tsc2301 {
2678 +       struct spi_device       *spi;
2679 +
2680 +       s16                     reset_gpio;
2681 +       u16                     config2_shadow;
2682 +
2683 +        struct tsc2301_kp      *kp;
2684 +       struct tsc2301_ts       *ts;
2685 +       struct tsc2301_mixer    *mixer;
2686 +
2687 +       int                     (*enable_clock)(struct device *dev);
2688 +       void                    (*disable_clock)(struct device *dev);
2689 +};
2690 +
2691 +
2692 +#define TSC2301_HZ     33000000
2693 +
2694 +#define TSC2301_REG(page, addr)  (((page) << 11) | ((addr) << 5))
2695 +#define TSC2301_REG_TO_PAGE(reg) (((reg) >> 11) & 0x03)
2696 +#define TSC2301_REG_TO_ADDR(reg) (((reg) >> 5)  & 0x1f)
2697 +
2698 +#define TSC2301_REG_X          TSC2301_REG(0, 0)
2699 +#define TSC2301_REG_Y          TSC2301_REG(0, 1)
2700 +#define TSC2301_REG_Z1         TSC2301_REG(0, 2)
2701 +#define TSC2301_REG_Z2         TSC2301_REG(0, 3)
2702 +#define TSC2301_REG_KPDATA     TSC2301_REG(0, 4)
2703 +#define TSC2301_REG_ADC                TSC2301_REG(1, 0)
2704 +#define TSC2301_REG_KEY                TSC2301_REG(1, 1)
2705 +#define TSC2301_REG_DAC                TSC2301_REG(1, 2)
2706 +#define TSC2301_REG_REF                TSC2301_REG(1, 3)
2707 +#define TSC2301_REG_RESET      TSC2301_REG(1, 4)
2708 +#define TSC2301_REG_CONFIG     TSC2301_REG(1, 5)
2709 +#define TSC2301_REG_CONFIG2    TSC2301_REG(1, 6)
2710 +#define TSC2301_REG_KPMASK     TSC2301_REG(1, 16)
2711 +#define TSC2301_REG_AUDCNTL    TSC2301_REG(2, 0)
2712 +#define TSC2301_REG_ADCVOL     TSC2301_REG(2, 1)
2713 +#define TSC2301_REG_DACVOL     TSC2301_REG(2, 2)
2714 +#define TSC2301_REG_BPVOL      TSC2301_REG(2, 3)
2715 +#define TSC2301_REG_KEYCTL     TSC2301_REG(2, 4)
2716 +#define TSC2301_REG_PD_MISC    TSC2301_REG(2, 5)
2717 +#define TSC2301_REG_GPIO       TSC2301_REG(2, 6)
2718 +#define TSC2301_REG_ADCLKCFG   TSC2301_REG(2, 27)
2719 +
2720 +#define TSC2301_REG_PD_MISC_APD                (1 << 15)
2721 +#define TSC2301_REG_PD_MISC_AVPD       (1 << 14)
2722 +#define TSC2301_REG_PD_MISC_ABPD       (1 << 13)
2723 +#define TSC2301_REG_PD_MISC_HAPD       (1 << 12)
2724 +#define TSC2301_REG_PD_MISC_MOPD       (1 << 11)
2725 +#define TSC2301_REG_PD_MISC_DAPD       (1 << 10)
2726 +#define TSC2301_REG_PD_MISC_ADPDL      (1 << 9)
2727 +#define TSC2301_REG_PD_MISC_ADPDR      (1 << 8)
2728 +#define TSC2301_REG_PD_MISC_PDSTS      (1 << 7)
2729 +#define TSC2301_REG_PD_MISC_MIBPD      (1 << 6)
2730 +#define TSC2301_REG_PD_MISC_OTSYN      (1 << 2)
2731 +
2732 +/* I2S sample rate */
2733 +#define TSC2301_I2S_SR_48000   0x00
2734 +#define TSC2301_I2S_SR_44100   0x01
2735 +#define TSC2301_I2S_SR_32000   0x02
2736 +#define TSC2301_I2S_SR_24000   0x03
2737 +#define TSC2301_I2S_SR_22050   0x04
2738 +#define TSC2301_I2S_SR_16000   0x05
2739 +#define TSC2301_I2S_SR_12000   0x06
2740 +#define TSC2301_I2S_SR_11050   0x07
2741 +#define TSC2301_I2S_SR_8000    0x08
2742 +
2743 +/* 16-bit, MSB-first. DAC Right-Justified, ADC Left-Justified */
2744 +#define TSC2301_I2S_FORMAT0    0x00
2745 +/* 20-bit, MSB-first. DAC Right-Justified, ADC Left-Justified */
2746 +#define TSC2301_I2S_FORMAT1    0x01
2747 +/* 20-bit, MSB-first. DAC Left-Justified, ADC Left-Justified */
2748 +#define TSC2301_I2S_FORMAT2    0x02
2749 +/* 20-bit, MSB-first */
2750 +#define TSC2301_I2S_FORMAT3    0x03
2751 +
2752 +/* Master Clock Ratio */
2753 +#define TSC2301_MCLK_256xFS    0x00 /* default */
2754 +#define TSC2301_MCLK_384xFS    0x01
2755 +#define TSC2301_MCLK_512xFS    0x02
2756 +
2757 +
2758 +extern u16 tsc2301_read_reg(struct tsc2301 *tsc, int reg);
2759 +extern void tsc2301_write_reg(struct tsc2301 *tsc, int reg, u16 val);
2760 +extern void tsc2301_write_kbc(struct tsc2301 *tsc, int val);
2761 +extern void tsc2301_write_pll(struct tsc2301 *tsc, int pll_n, int pll_a,
2762 +                             int pll_pdc, int pct_e, int pll_o);
2763 +extern void tsc2301_read_buf(struct tsc2301 *tsc, int reg, u16 *buf, int len);
2764 +
2765 +#define TSC2301_DECL_MOD(module)                                       \
2766 +extern int  tsc2301_##module##_init(struct tsc2301 *tsc,               \
2767 +                          struct tsc2301_platform_data *pdata);        \
2768 +extern void tsc2301_##module##_exit(struct tsc2301 *tsc);              \
2769 +extern int  tsc2301_##module##_suspend(struct tsc2301 *tsc);           \
2770 +extern void tsc2301_##module##_resume(struct tsc2301 *tsc);
2771 +
2772 +#define TSC2301_DECL_EMPTY_MOD(module)                                 \
2773 +static inline int tsc2301_##module##_init(struct tsc2301 *tsc,         \
2774 +                          struct tsc2301_platform_data *pdata)         \
2775 +{                                                                      \
2776 +       return 0;                                                       \
2777 +}                                                                      \
2778 +static inline void tsc2301_##module##_exit(struct tsc2301 *tsc) {}     \
2779 +static inline int  tsc2301_##module##_suspend(struct tsc2301 *tsc)     \
2780 +{                                                                      \
2781 +       return 0;                                                       \
2782 +}                                                                      \
2783 +static inline void tsc2301_##module##_resume(struct tsc2301 *tsc) {}
2784 +
2785 +#ifdef CONFIG_KEYBOARD_TSC2301
2786 +TSC2301_DECL_MOD(kp)
2787 +void tsc2301_kp_restart(struct tsc2301 *tsc);
2788 +#else
2789 +TSC2301_DECL_EMPTY_MOD(kp)
2790 +static inline void tsc2301_kp_restart(struct tsc2301 *tsc) {}
2791 +#endif
2792 +
2793 +#ifdef CONFIG_TOUCHSCREEN_TSC2301
2794 +TSC2301_DECL_MOD(ts)
2795 +#else
2796 +TSC2301_DECL_EMPTY_MOD(ts)
2797 +#endif
2798 +
2799 +#ifdef CONFIG_SPI_TSC2301_AUDIO
2800 +TSC2301_DECL_MOD(mixer)
2801 +extern void tsc2301_mixer_set_power(struct device *tsc_dev, int dac, int adc);
2802 +
2803 +struct snd_card;
2804 +extern int tsc2301_mixer_register_controls(struct device *tsc_dev,
2805 +                                          struct snd_card *card);
2806 +#else
2807 +TSC2301_DECL_EMPTY_MOD(mixer)
2808 +#endif
2809 +
2810 +extern void tsc2301_mixer_enable_mclk(struct device *tsc_dev);
2811 +extern void tsc2301_mixer_disable_mclk(struct device *tsc_dev);
2812 +
2813 +#endif