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)
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.
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
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.
35 +config KEYBOARD_TSC2301
36 + tristate "TSC2301 keypad support"
37 + depends on SPI_TSC2301
39 + Say Y here for if you are using the keypad features of TSC2301.
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
62 + * TSC2301 keypad driver
64 + * Copyright (C) 2005-2006 Nokia Corporation
66 + * Written by Jarkko Oikarinen
67 + * Rewritten by Juha Yrjola <juha.yrjola@nokia.com>
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.
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.
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
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>
93 +#include <linux/spi/tsc2301.h>
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
106 +#define TSC2301_DEBOUNCE_TIME TSC2301_DEBOUNCE_TIME_20MS
108 +#define TSC2301_RELEASE_TIMEOUT 50
111 + struct input_dev *idev;
114 + struct mutex mutex;
115 + struct timer_list timer;
117 + unsigned pending:1;
118 + unsigned user_disabled:1;
119 + unsigned disable_depth;
121 + struct spi_transfer read_xfer[4];
122 + struct spi_message read_msg;
131 +static inline int tsc2301_kp_disabled(struct tsc2301 *tsc)
133 + return tsc->kp->disable_depth != 0;
136 +static void tsc2301_kp_send_key_events(struct tsc2301 *tsc,
140 + struct tsc2301_kp *kp = tsc->kp;
141 + u16 common, released, pressed;
144 + common = prev_state & new_state;
145 + released = common ^ prev_state;
146 + pressed = common ^ new_state;
147 + if (!released && !pressed)
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);
156 + dev_dbg(&tsc->spi->dev, "key %d pressed\n", i);
157 + input_report_key(kp->idev, kp->keymap[i], 1);
161 + input_sync(kp->idev);
164 +static inline void _filter_out(struct tsc2301 *tsc, u16 prev_state,
165 + u16 *new_state, int row1, int row2, u8 rect_pat)
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);
175 +static void tsc2301_filter_ghost_keys(struct tsc2301 *tsc, u16 prev_state,
181 + static const u8 rect_pat[] = {
182 + 0x3, 0x5, 0x9, 0x6, 0xa, 0xc, 0,
185 + key_map = *new_state;
186 + for (row1 = 0; row1 < 4; row1++) {
187 + row1_map = (key_map >> (row1 * 4)) & 0xf;
190 + for (row2 = row1 + 1; row2 < 4; row2++) {
191 + u16 rect_map = (key_map >> (row2 * 4)) & 0xf;
194 + rect_map &= row1_map;
197 + for (rp = rect_pat; *rp; rp++)
198 + if ((rect_map & *rp) == *rp)
199 + _filter_out(tsc, prev_state, new_state,
205 +static void tsc2301_kp_timer(unsigned long arg)
207 + struct tsc2301 *tsc = (void *) arg;
208 + struct tsc2301_kp *kp = tsc->kp;
209 + unsigned long flags;
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);
217 +static void tsc2301_kp_rx(void *arg)
219 + struct tsc2301 *tsc = arg;
220 + struct tsc2301_kp *kp = tsc->kp;
221 + unsigned long flags;
224 + kp_data = kp->data;
225 + dev_dbg(&tsc->spi->dev, "KP data %04x\n", kp_data);
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;
232 + spin_unlock_irqrestore(&kp->lock, flags);
235 +static irqreturn_t tsc2301_kp_irq_handler(int irq, void *dev_id)
237 + struct tsc2301 *tsc = dev_id;
238 + struct tsc2301_kp *kp = tsc->kp;
239 + unsigned long flags;
242 + spin_lock_irqsave(&kp->lock, flags);
243 + if (tsc2301_kp_disabled(tsc)) {
244 + spin_unlock_irqrestore(&kp->lock, flags);
245 + return IRQ_HANDLED;
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);
253 + dev_err(&tsc->spi->dev, "kp: spi_async() failed");
254 + return IRQ_HANDLED;
257 +static void tsc2301_kp_start_scan(struct tsc2301 *tsc)
259 + tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, tsc->kp->mask);
260 + tsc2301_write_reg(tsc, TSC2301_REG_KEY, TSC2301_DEBOUNCE_TIME);
263 +static void tsc2301_kp_stop_scan(struct tsc2301 *tsc)
265 + tsc2301_write_reg(tsc, TSC2301_REG_KEY, 1 << 14);
268 +/* Must be called with the mutex held */
269 +static void tsc2301_kp_enable(struct tsc2301 *tsc)
271 + struct tsc2301_kp *kp = tsc->kp;
272 + unsigned long flags;
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);
280 + spin_unlock_irqrestore(&kp->lock, flags);
282 + irq_set_irq_type(kp->irq, IRQ_TYPE_EDGE_FALLING);
283 + tsc2301_kp_start_scan(tsc);
284 + enable_irq(kp->irq);
287 +/* Must be called with the mutex held */
288 +static int tsc2301_kp_disable(struct tsc2301 *tsc, int release_keys)
290 + struct tsc2301_kp *kp = tsc->kp;
291 + unsigned long flags;
293 + spin_lock_irqsave(&kp->lock, flags);
294 + if (kp->disable_depth++ != 0) {
295 + spin_unlock_irqrestore(&kp->lock, flags);
298 + disable_irq_nosync(kp->irq);
299 + irq_set_irq_type(kp->irq, IRQ_TYPE_NONE);
300 + spin_unlock_irqrestore(&kp->lock, flags);
302 + while (kp->pending) {
306 + tsc2301_kp_stop_scan(tsc);
309 + del_timer(&kp->timer); /* let timeout release keys */
314 +/* The following workaround is needed for a HW bug triggered by the
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
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.
327 +void tsc2301_kp_restart(struct tsc2301 *tsc)
329 + if (!tsc2301_kp_disabled(tsc)) {
330 + tsc2301_kp_start_scan(tsc);
334 +static ssize_t tsc2301_kp_disable_show(struct device *dev,
335 + struct device_attribute *attr, char *buf)
337 + struct tsc2301 *tsc = dev_get_drvdata(dev);
339 + return sprintf(buf, "%u\n", tsc2301_kp_disabled(tsc) ? 1 : 0);
342 +static ssize_t tsc2301_kp_disable_store(struct device *dev,
343 + struct device_attribute *attr,
344 + const char *buf, size_t count)
346 + struct tsc2301 *tsc = dev_get_drvdata(dev);
347 + struct tsc2301_kp *kp = tsc->kp;
351 + i = simple_strtoul(buf, &endp, 10);
354 + mutex_lock(&kp->mutex);
355 + if (i == kp->user_disabled) {
356 + mutex_unlock(&kp->mutex);
359 + kp->user_disabled = i;
362 + tsc2301_kp_disable(tsc, 1);
364 + tsc2301_kp_enable(tsc);
365 + mutex_unlock(&kp->mutex);
370 +static DEVICE_ATTR(disable_kp, 0664, tsc2301_kp_disable_show,
371 + tsc2301_kp_disable_store);
373 +static const u16 tsc2301_kp_read_data = 0x8000 | TSC2301_REG_KPDATA;
375 +static void tsc2301_kp_setup_spi_xfer(struct tsc2301 *tsc)
377 + struct tsc2301_kp *kp = tsc->kp;
378 + struct spi_message *m = &kp->read_msg;
379 + struct spi_transfer *x = &kp->read_xfer[0];
381 + spi_message_init(&kp->read_msg);
383 + x->tx_buf = &tsc2301_kp_read_data;
385 + spi_message_add_tail(x, m);
388 + x->rx_buf = &kp->data;
390 + spi_message_add_tail(x, m);
392 + m->complete = tsc2301_kp_rx;
397 +int tsc2301_kp_suspend(struct tsc2301 *tsc)
399 + struct tsc2301_kp *kp = tsc->kp;
401 + mutex_lock(&kp->mutex);
402 + tsc2301_kp_disable(tsc, 1);
403 + mutex_unlock(&kp->mutex);
407 +void tsc2301_kp_resume(struct tsc2301 *tsc)
409 + struct tsc2301_kp *kp = tsc->kp;
411 + mutex_lock(&kp->mutex);
412 + tsc2301_kp_enable(tsc);
413 + mutex_unlock(&kp->mutex);
417 +int __devinit tsc2301_kp_init(struct tsc2301 *tsc,
418 + struct tsc2301_platform_data *pdata)
420 + struct input_dev *idev;
421 + struct tsc2301_kp *kp;
425 + if (pdata->keyb_int < 0) {
426 + dev_err(&tsc->spi->dev, "need kbirq");
430 + kp = kzalloc(sizeof(*kp), GFP_KERNEL);
435 + kp->irq = pdata->keyb_int;
436 + spin_lock_init(&kp->lock);
437 + mutex_init(&kp->mutex);
439 + init_timer(&kp->timer);
440 + kp->timer.data = (unsigned long) tsc;
441 + kp->timer.function = tsc2301_kp_timer;
443 + idev = input_allocate_device();
444 + if (idev == NULL) {
448 + if (pdata->keyb_name)
449 + idev->name = pdata->keyb_name;
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;
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];
462 + kp->keymap[i] = -1;
468 + set_bit(EV_REP, idev->evbit);
472 + tsc2301_kp_setup_spi_xfer(tsc);
474 + r = device_create_file(&tsc->spi->dev, &dev_attr_disable_kp);
478 + tsc2301_kp_start_scan(tsc);
480 + /* IRQ mode 0 is faulty, it can cause the KBIRQ to get stuck.
481 + * Mode 2 deasserts the IRQ at:
483 + * - Setting SCS flag in REG_KEY register
484 + * - Releasing all keys
485 + * - Reading the REG_KPDATA
487 + tsc2301_write_kbc(tsc, 2);
489 + tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, mask);
492 + irq_set_irq_type(kp->irq, IRQ_TYPE_EDGE_FALLING);
494 + r = request_irq(kp->irq, tsc2301_kp_irq_handler, IRQF_SAMPLE_RANDOM,
495 + "tsc2301-kp", tsc);
497 + dev_err(&tsc->spi->dev, "unable to get kbirq IRQ");
500 + irq_set_irq_wake(kp->irq, 1);
502 + /* We need to read the register once..? */
503 + tsc2301_read_reg(tsc, TSC2301_REG_KPDATA);
505 + r = input_register_device(idev);
507 + dev_err(&tsc->spi->dev, "can't register keypad device\n");
514 + free_irq(kp->irq, tsc);
516 + tsc2301_kp_stop_scan(tsc);
517 + device_remove_file(&tsc->spi->dev, &dev_attr_disable_kp);
519 + input_free_device(kp->idev);
525 +void __devexit tsc2301_kp_exit(struct tsc2301 *tsc)
527 + struct tsc2301_kp *kp = tsc->kp;
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);
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.
544 +config TOUCHSCREEN_TSC2301
545 + tristate "TSC2301 touchscreen support"
546 + depends on SPI_TSC2301
548 + Say Y here for if you are using the touchscreen features of TSC2301.
550 config TOUCHSCREEN_W90X900
551 tristate "W90P910 touchscreen driver"
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
571 + * TSC2301 touchscreen driver
573 + * Copyright (C) 2005-2008 Nokia Corporation
575 + * Written by Jarkko Oikarinen, Imre Deak and Juha Yrjola
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.
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.
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
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>
600 +#include <linux/spi/tsc2301.h>
603 + * The touchscreen interface operates as follows:
606 + * Request access to GPIO103 (DAV)
607 + * tsc2301_ts_irq_handler will trigger when DAV line goes down
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.
621 +#define TSC2301_TOUCHSCREEN_PRODUCT_ID 0x0052
622 +#define TSC2301_TOUCHSCREEN_PRODUCT_VERSION 0x0001
624 +#define TSC2301_TS_PENUP_TIME 20
626 +#define TSC2301_ADCREG_CONVERSION_CTRL_BY_TSC2301 0x8000
627 +#define TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST 0x0000
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
641 +#define TSC2301_ADCREG_RESOLUTION_8BIT 0x0100
642 +#define TSC2301_ADCREG_RESOLUTION_10BIT 0x0200
643 +#define TSC2301_ADCREG_RESOLUTION_12BIT 0x0300
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
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
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
664 +#define TSC2301_ADCREG_STOP_CONVERSION 0x4000
666 +#define MAX_12BIT ((1 << 12) - 1)
668 +#define TS_RECT_SIZE 8
669 +#define TSF_MIN_Z1 100
670 +#define TSF_MAX_Z2 4000
672 +#define TSF_SAMPLES 4
691 + struct input_dev *idev;
693 + struct timer_list penup_timer;
694 + struct mutex mutex;
696 + struct spi_transfer read_xfer[2];
697 + struct spi_message read_msg;
698 + struct ts_coords *coords;
700 + struct ts_filter filter;
710 + int touch_pressure;
722 +static const u16 tsc2301_ts_read_data = 0x8000 | TSC2301_REG_X;
724 +static int tsc2301_ts_check_config(struct tsc2301_ts *ts, int *hw_flags)
729 + switch (ts->hw_avg_max) {
731 + flags |= TSC2301_ADCREG_AVERAGING_NONE;
734 + flags |= TSC2301_ADCREG_AVERAGING_4AVG;
737 + flags |= TSC2301_ADCREG_AVERAGING_8AVG;
740 + flags |= TSC2301_ADCREG_AVERAGING_16AVG;
746 + switch (ts->stab_time) {
748 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_0US;
751 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_100US;
754 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_500US;
757 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_1MS;
760 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_5MS;
763 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_10MS;
766 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_50MS;
769 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_100MS;
780 + * This odd three-time initialization is to work around a bug in TSC2301.
781 + * See TSC2301 errata for details.
783 +static int tsc2301_ts_configure(struct tsc2301 *tsc, int flags)
785 + struct spi_transfer xfer[5];
786 + struct spi_transfer *x;
787 + struct spi_message m;
789 + u16 val1, val2, val3;
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;
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;
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 |
814 + /* Now we prepare the command for transferring */
815 + data[0] = TSC2301_REG_ADC;
817 + data[2] = TSC2301_REG_ADC;
819 + data[4] = TSC2301_REG_ADC;
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 */
826 + spi_message_init(&m);
829 + memset(xfer, 0, sizeof(xfer));
832 + for (i = 0; i < 10; i += 2) {
833 + x->tx_buf = &data[i];
837 + spi_message_add_tail(x, &m);
840 + spi_sync(m.spi, &m);
845 +static void tsc2301_ts_start_scan(struct tsc2301 *tsc)
847 + tsc2301_ts_configure(tsc, tsc->ts->hw_flags);
848 + tsc2301_kp_restart(tsc);
851 +static void tsc2301_ts_stop_scan(struct tsc2301 *tsc)
853 + tsc2301_write_reg(tsc, TSC2301_REG_ADC, TSC2301_ADCREG_STOP_CONVERSION);
854 + tsc2301_kp_restart(tsc);
857 +static void update_pen_state(struct tsc2301_ts *ts, int x, int y, int 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);
864 + input_report_key(ts->idev, BTN_TOUCH, 1);
867 + input_report_abs(ts->idev, ABS_PRESSURE, 0);
869 + input_report_key(ts->idev, BTN_TOUCH, 0);
873 + input_sync(ts->idev);
876 + dev_dbg(&tsc->spi->dev, "x %4d y %4d p %4d\n", x, y, pressure);
880 +static int filter(struct tsc2301_ts *ts, int x, int y, int z1, int z2)
882 + int inside_rect, pressure_limit, Rt;
883 + struct ts_filter *tsf = &ts->filter;
885 + /* validate pressure and position */
886 + if (x > MAX_12BIT || y > MAX_12BIT)
889 + /* skip coords if the pressure-components are out of range */
890 + if (z1 < TSF_MIN_Z1 || z2 > TSF_MAX_Z2)
893 + /* Use the x,y,z1,z2 directly on the first "pen down" event */
894 + if (ts->event_sent) {
900 + if (++tsf->sample_cnt < TSF_SAMPLES)
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;
907 + tsf->sample_cnt = 0;
913 + pressure_limit = ts->event_sent? ts->max_pressure: ts->touch_pressure;
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)
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 */
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);
927 + if (!ts->event_sent || !inside_rect) {
932 + } else if (Rt < ts->p) {
940 + * This procedure is called by the SPI framework after the coordinates
941 + * have been read from TSC2301
943 +static void tsc2301_ts_rx(void *arg)
945 + struct tsc2301 *tsc = arg;
946 + struct tsc2301_ts *ts = tsc->ts;
952 + z1 = ts->coords->z1;
953 + z2 = ts->coords->z2;
955 + send_event = filter(ts, x, y, z1, z2);
957 + update_pen_state(ts, ts->x, ts->y, ts->p);
958 + ts->event_sent = 1;
961 + mod_timer(&ts->penup_timer,
962 + jiffies + msecs_to_jiffies(TSC2301_TS_PENUP_TIME));
966 + * Timer is called TSC2301_TS_PENUP_TIME after pen is up
968 +static void tsc2301_ts_timer_handler(unsigned long data)
970 + struct tsc2301 *tsc = (struct tsc2301 *)data;
971 + struct tsc2301_ts *ts = tsc->ts;
973 + if (ts->event_sent) {
974 + ts->event_sent = 0;
975 + update_pen_state(ts, 0, 0, 0);
980 + * This interrupt is called when pen is down and coordinates are
981 + * available. That is indicated by a falling edge on DEV line.
983 +static irqreturn_t tsc2301_ts_irq_handler(int irq, void *dev_id)
985 + struct tsc2301 *tsc = dev_id;
986 + struct tsc2301_ts *ts = tsc->ts;
989 + r = spi_async(tsc->spi, &ts->read_msg);
991 + dev_err(&tsc->spi->dev, "ts: spi_async() failed");
993 + mod_timer(&ts->penup_timer,
994 + jiffies + msecs_to_jiffies(TSC2301_TS_PENUP_TIME));
996 + return IRQ_HANDLED;
999 +static void tsc2301_ts_disable(struct tsc2301 *tsc)
1001 + struct tsc2301_ts *ts = tsc->ts;
1003 + if (ts->disable_depth++ != 0)
1006 + disable_irq(ts->irq);
1008 + /* wait until penup timer expire normally */
1011 + } while (ts->event_sent);
1013 + tsc2301_ts_stop_scan(tsc);
1016 +static void tsc2301_ts_enable(struct tsc2301 *tsc)
1018 + struct tsc2301_ts *ts = tsc->ts;
1020 + if (--ts->disable_depth != 0)
1023 + enable_irq(ts->irq);
1025 + tsc2301_ts_start_scan(tsc);
1029 +int tsc2301_ts_suspend(struct tsc2301 *tsc)
1031 + struct tsc2301_ts *ts = tsc->ts;
1033 + mutex_lock(&ts->mutex);
1034 + tsc2301_ts_disable(tsc);
1035 + mutex_unlock(&ts->mutex);
1040 +void tsc2301_ts_resume(struct tsc2301 *tsc)
1042 + struct tsc2301_ts *ts = tsc->ts;
1044 + mutex_lock(&ts->mutex);
1045 + tsc2301_ts_enable(tsc);
1046 + mutex_unlock(&ts->mutex);
1050 +static void tsc2301_ts_setup_spi_xfer(struct tsc2301 *tsc)
1052 + struct tsc2301_ts *ts = tsc->ts;
1053 + struct spi_message *m = &ts->read_msg;
1054 + struct spi_transfer *x = &ts->read_xfer[0];
1056 + spi_message_init(m);
1058 + x->tx_buf = &tsc2301_ts_read_data;
1060 + spi_message_add_tail(x, m);
1063 + x->rx_buf = ts->coords;
1065 + spi_message_add_tail(x, m);
1067 + m->complete = tsc2301_ts_rx;
1071 +static ssize_t tsc2301_ts_pen_down_show(struct device *dev,
1072 + struct device_attribute *attr,
1075 + struct tsc2301 *tsc = dev_get_drvdata(dev);
1077 + return sprintf(buf, "%u\n", tsc->ts->pen_down);
1080 +static DEVICE_ATTR(pen_down, S_IRUGO, tsc2301_ts_pen_down_show, NULL);
1082 +static ssize_t tsc2301_ts_disable_show(struct device *dev,
1083 + struct device_attribute *attr, char *buf)
1085 + struct tsc2301 *tsc = dev_get_drvdata(dev);
1086 + struct tsc2301_ts *ts = tsc->ts;
1088 + return sprintf(buf, "%u\n", ts->disabled);
1091 +static ssize_t tsc2301_ts_disable_store(struct device *dev,
1092 + struct device_attribute *attr,
1093 + const char *buf, size_t count)
1095 + struct tsc2301 *tsc = dev_get_drvdata(dev);
1096 + struct tsc2301_ts *ts = tsc->ts;
1100 + i = simple_strtoul(buf, &endp, 10);
1102 + mutex_lock(&ts->mutex);
1103 + if (i == ts->disabled) goto out;
1107 + tsc2301_ts_disable(tsc);
1109 + tsc2301_ts_enable(tsc);
1111 + mutex_unlock(&ts->mutex);
1115 +static DEVICE_ATTR(disable_ts, 0664, tsc2301_ts_disable_show,
1116 + tsc2301_ts_disable_store);
1118 +int __devinit tsc2301_ts_init(struct tsc2301 *tsc,
1119 + struct tsc2301_platform_data *pdata)
1121 + struct tsc2301_ts *ts;
1122 + struct input_dev *idev;
1125 + int x_fudge, y_fudge, p_fudge;
1127 + if (pdata->dav_int <= 0) {
1128 + dev_err(&tsc->spi->dev, "need DAV IRQ");
1132 + ts = kzalloc(sizeof(*ts), GFP_KERNEL);
1137 + ts->coords = kzalloc(sizeof(*ts->coords), GFP_KERNEL);
1138 + if (ts->coords == NULL) {
1143 + ts->irq = pdata->dav_int;
1145 + init_timer(&ts->penup_timer);
1146 + setup_timer(&ts->penup_timer, tsc2301_ts_timer_handler,
1147 + (unsigned long)tsc);
1149 + mutex_init(&ts->mutex);
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;
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;
1163 + if ((r = tsc2301_ts_check_config(ts, &ts->hw_flags))) {
1164 + dev_err(&tsc->spi->dev, "invalid configuration\n");
1168 + idev = input_allocate_device();
1169 + if (idev == NULL) {
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;
1179 + idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
1180 + idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
1183 + tsc2301_ts_setup_spi_xfer(tsc);
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,
1191 + tsc2301_ts_start_scan(tsc);
1193 + r = request_irq(ts->irq, tsc2301_ts_irq_handler,
1194 + IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
1195 + "tsc2301-ts", tsc);
1197 + dev_err(&tsc->spi->dev, "unable to get DAV IRQ");
1200 + irq_set_irq_wake(ts->irq, 1);
1202 + if (device_create_file(&tsc->spi->dev, &dev_attr_pen_down) < 0)
1204 + if (device_create_file(&tsc->spi->dev, &dev_attr_disable_ts) < 0)
1207 + r = input_register_device(idev);
1209 + dev_err(&tsc->spi->dev, "can't register touchscreen device\n");
1215 + device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
1217 + device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
1219 + free_irq(ts->irq, tsc);
1221 + tsc2301_ts_stop_scan(tsc);
1222 + input_free_device(idev);
1224 + kfree(ts->coords);
1229 +void __devexit tsc2301_ts_exit(struct tsc2301 *tsc)
1231 + struct tsc2301_ts *ts = tsc->ts;
1233 + tsc2301_ts_disable(tsc);
1235 + device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
1236 + device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
1238 + free_irq(ts->irq, tsc);
1239 + input_unregister_device(ts->idev);
1241 + kfree(ts->coords);
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.
1255 + tristate "TSC2301 driver"
1256 + depends on SPI_MASTER
1258 + Say Y here if you have a TSC2301 chip connected to an SPI
1259 + bus on your board.
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.
1267 + To compile this driver as a module, choose M here: the
1268 + module will be called tsc2301.
1270 +config SPI_TSC2301_AUDIO
1271 + boolean "TSC2301 audio support"
1272 + depends on SPI_TSC2301 && SND
1274 + Say Y here for if you are using the audio features of TSC2301.
1277 # Add new SPI protocol masters in alphabetical order above this line
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
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
1299 + * Copyright (C) 2005, 2006 Nokia Corporation
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.
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.
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
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>
1324 +u16 tsc2301_read_reg(struct tsc2301 *tsc, int reg)
1326 + struct spi_transfer t[2];
1327 + struct spi_message m;
1328 + u16 data = 0, cmd;
1333 + memset(t, 0, sizeof(t));
1334 + spi_message_init(&m);
1337 + t[0].tx_buf = &cmd;
1338 + t[0].rx_buf = NULL;
1340 + spi_message_add_tail(&t[0], &m);
1342 + t[1].tx_buf = NULL;
1343 + t[1].rx_buf = &data;
1345 + spi_message_add_tail(&t[1], &m);
1347 + spi_sync(m.spi, &m);
1352 +void tsc2301_write_reg(struct tsc2301 *tsc, int reg, u16 val)
1354 + struct spi_transfer t;
1355 + struct spi_message m;
1358 + /* Now we prepare the command for transferring */
1362 + spi_message_init(&m);
1365 + memset(&t, 0, sizeof(t));
1369 + spi_message_add_tail(&t, &m);
1371 + spi_sync(m.spi, &m);
1374 +void tsc2301_write_kbc(struct tsc2301 *tsc, int val)
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;
1385 +void tsc2301_write_pll(struct tsc2301 *tsc,
1386 + int pll_n, int pll_a, int pll_pdc, int pct_e, int pll_o)
1390 + w = tsc->config2_shadow;
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;
1399 +void tsc2301_read_buf(struct tsc2301 *tsc, int reg, u16 *rx_buf, int len)
1401 + struct spi_transfer t[2];
1402 + struct spi_message m;
1408 + spi_message_init(&m);
1411 + memset(t, 0, sizeof(t));
1412 + t[0].tx_buf = &cmd;
1413 + t[0].rx_buf = NULL;
1415 + spi_message_add_tail(&t[0], &m);
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);
1422 + spi_sync(m.spi, &m);
1424 + for (i = 0; i < len; i++)
1425 + printk(KERN_DEBUG "rx_buf[%d]: %04x\n", i, rx_buf[i]);
1428 +static int __devinit tsc2301_probe(struct spi_device *spi)
1430 + struct tsc2301 *tsc;
1431 + struct tsc2301_platform_data *pdata = spi->dev.platform_data;
1436 + dev_dbg(&spi->dev, "no platform data?\n");
1440 + tsc = kzalloc(sizeof(*tsc), GFP_KERNEL);
1444 + dev_set_drvdata(&spi->dev, tsc);
1447 + tsc->enable_clock = pdata->enable_clock;
1448 + tsc->disable_clock = pdata->disable_clock;
1450 + if (pdata->reset_gpio >= 0) {
1451 + tsc->reset_gpio = pdata->reset_gpio;
1452 + r = gpio_request(tsc->reset_gpio, "TSC2301 reset");
1455 + gpio_direction_output(tsc->reset_gpio, 1);
1457 + gpio_set_value(tsc->reset_gpio, 0);
1459 + tsc->reset_gpio = -1;
1461 + spi->mode = SPI_MODE_1;
1462 + spi->bits_per_word = 16;
1463 + /* The max speed might've been defined by the board-specific
1465 + if (!spi->max_speed_hz)
1466 + spi->max_speed_hz = TSC2301_HZ;
1470 + tsc2301_write_reg(tsc, TSC2301_REG_RESET, 0xbb00);
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);
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);
1487 + /* Stop keypad scanning */
1488 + tsc2301_write_reg(tsc, TSC2301_REG_KEY, 0x4000);
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 */
1495 + tsc->config2_shadow = w;
1497 + r = tsc2301_kp_init(tsc, pdata);
1500 + r = tsc2301_ts_init(tsc, pdata);
1503 + r = tsc2301_mixer_init(tsc, pdata);
1509 + tsc2301_ts_exit(tsc);
1511 + tsc2301_kp_exit(tsc);
1517 +static int __devexit tsc2301_remove(struct spi_device *spi)
1519 + struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
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);
1532 +static int tsc2301_suspend(struct spi_device *spi, pm_message_t mesg)
1534 + struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
1537 + if ((r = tsc2301_mixer_suspend(tsc)) < 0)
1539 + if ((r = tsc2301_kp_suspend(tsc)) < 0)
1541 + if ((r = tsc2301_ts_suspend(tsc)) < 0)
1546 + tsc2301_kp_resume(tsc);
1548 + tsc2301_mixer_resume(tsc);
1552 +static int tsc2301_resume(struct spi_device *spi)
1554 + struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
1556 + tsc2301_ts_resume(tsc);
1557 + tsc2301_kp_resume(tsc);
1558 + tsc2301_mixer_resume(tsc);
1563 +static struct spi_driver tsc2301_driver = {
1565 + .name = "tsc2301",
1566 + .bus = &spi_bus_type,
1567 + .owner = THIS_MODULE,
1570 + .suspend = tsc2301_suspend,
1571 + .resume = tsc2301_resume,
1573 + .probe = tsc2301_probe,
1574 + .remove = __devexit_p(tsc2301_remove),
1577 +static int __init tsc2301_init(void)
1579 + printk("TSC2301 driver initializing\n");
1581 + return spi_register_driver(&tsc2301_driver);
1583 +module_init(tsc2301_init);
1585 +static void __exit tsc2301_exit(void)
1587 + spi_unregister_driver(&tsc2301_driver);
1589 +module_exit(tsc2301_exit);
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
1599 + * ALSA Mixer implementation for TSC2301
1601 + * Copyright (C) 2006 Nokia Corporation.
1603 + * Contact: Jarkko Nikula <jarkko.nikula@nokia.com>
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
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
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.
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.
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
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>
1646 +#include <sound/core.h>
1647 +#include <sound/control.h>
1649 +/* shadow register indexes */
1651 + /* audio control and volume registers */
1656 + /* keyclick control register (not needed here) */
1657 + /* audio power control register */
1659 + /* TSC2301 GPIO control register */
1665 +/* structure for driver private data */
1666 +struct tsc2301_mixer {
1667 + struct tsc2301 *tsc;
1668 + struct mutex mutex;
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];
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;
1682 + /* latest audio power-up timestamp */
1683 + unsigned long pu_jiffies;
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;
1690 + int (* platform_init)(struct device *);
1691 + void (* platform_cleanup)(struct device *);
1693 + struct tsc2301_mixer_gpio *mixer_gpios;
1694 + int n_mixer_gpios;
1697 +#define TSC2301_DAC_DELAY msecs_to_jiffies(100)
1698 +#define TSC2301_MIN_PU_PERIOD msecs_to_jiffies(100)
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)))
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
1714 +#define TSC2301_INM_MASK 3
1715 +#define TSC2301_INML_SHIFT 12
1716 +#define TSC2301_INMR_SHIFT 10
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
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)
1726 +#define TSC2301_SINGLE(xname, xindex, reg, shadow_index, shift, mask, min) \
1728 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
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) | \
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)
1742 +#define TSC2301_DOUBLE(xname, xindex, reg, shadow_index, ls, rs, mask, min) \
1744 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
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) \
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)
1759 +#define TSC2301_MUX(xname, xindex, reg, shadow_index, ls, rs, mask) \
1761 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
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) \
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)
1774 +#define TSC2301_BOOL(xname, xindex, reg, shadow_index, shift, invert, state) \
1776 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
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) \
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)
1790 +#define TSC2301_SHADOW_INDEX(v) (((v) >> 8) & 7)
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
1797 + * Must be called tsc->mixer->mutex locked
1799 +static void tsc2301_gpio_power_down(struct tsc2301 *tsc)
1801 + struct tsc2301_mixer *mix = tsc->mixer;
1805 + temp = mix->shadow_regs[GPIO_INDEX];
1806 + for (i = 0; i < mix->n_mixer_gpios; i++) {
1807 + const struct tsc2301_mixer_gpio *mg;
1809 + mg = mix->mixer_gpios + i;
1810 + if (mg->deactivate_on_pd) {
1811 + int gpio = mg->gpio;
1813 + temp &= ~(1 << gpio);
1814 + temp |= mg->inverted << gpio;
1817 + tsc2301_write_reg(tsc, TSC2301_REG_GPIO, temp);
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
1825 + * Must be called tsc->mixer->mutex locked
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,
1832 + struct tsc2301_mixer *mix = tsc->mixer;
1833 + u16 pd_ctrl, pd_ctrl_old, w;
1834 + unsigned long timeout;
1837 + if (mix->delayed_pd_active) {
1838 + mix->delayed_pd_active = 0;
1839 + mix->mclk_enabled--;
1840 + cancel_delayed_work(&mix->delayed_power_down);
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;
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;
1859 + pd_ctrl &= ~TSC2301_REG_PD_MISC_ADPDR;
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;
1868 + pd_ctrl &= ~TSC2301_REG_PD_MISC_DAPD;
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
1876 + pd_ctrl |= TSC2301_REG_PD_MISC_ABPD;
1878 + pd_ctrl &= ~TSC2301_REG_PD_MISC_ABPD;
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;
1890 + pd_ctrl &= ~TSC2301_REG_PD_MISC_MIBPD;
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
1901 + pd_ctrl |= TSC2301_REG_PD_MISC_ADPDL;
1902 + pd_ctrl |= TSC2301_REG_PD_MISC_ADPDR;
1903 + pd_ctrl |= TSC2301_REG_PD_MISC_MIBPD;
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;
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);
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;
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;
1943 + if (pd_ctrl == pd_ctrl_old)
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
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 -
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,
1965 + /* restore GPIOs after codec is powered up */
1968 + mix->shadow_regs[PD_MISC_INDEX] = pd_ctrl;
1969 + tsc2301_write_reg(tsc, TSC2301_REG_PD_MISC, pd_ctrl);
1971 + mix->pu_jiffies = jiffies;
1972 + if (!poll_pdsts) {
1974 + tsc2301_write_reg(tsc, TSC2301_REG_GPIO,
1975 + mix->shadow_regs[GPIO_INDEX]);
1979 + /* wait until power-up/-down is completed */
1980 + timeout = jiffies + msecs_to_jiffies(100);
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.
1990 + if (w & TSC2301_REG_PD_MISC_OTSYN) {
1991 + dev_warn(&tsc->spi->dev,
1992 + "I2S clock not in sync or off.\n");
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);
2001 + w = tsc2301_read_reg(tsc, TSC2301_REG_PD_MISC);
2002 + } while (!(w & TSC2301_REG_PD_MISC_PDSTS));
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
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]);
2024 +static int snd_tsc2301_info_single(struct snd_kcontrol *kcontrol,
2025 + struct snd_ctl_elem_info *uinfo)
2027 + int mask = TSC2301_SINGLE_MASK(kcontrol->private_value);
2028 + int minval = TSC2301_SINGLE_MINVAL(kcontrol->private_value);
2030 + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2032 + uinfo->value.integer.min = minval;
2033 + uinfo->value.integer.max = mask;
2038 +static int snd_tsc2301_get_single(struct snd_kcontrol *kcontrol,
2039 + struct snd_ctl_elem_value *ucontrol)
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);
2048 + shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2050 + ucontrol->value.integer.value[0] = (shadow_reg >> shift) & mask;
2055 +static int snd_tsc2301_put_single(struct snd_kcontrol *kcontrol,
2056 + struct snd_ctl_elem_value *ucontrol)
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);
2067 + mutex_lock(&tsc->mixer->mutex);
2068 + shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
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);
2075 + changed = (shadow_reg != shadow_reg_old);
2076 + tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2078 + /* update into TSC2301 if necessary */
2080 + tsc2301_write_reg(tsc, reg, shadow_reg);
2081 + mutex_unlock(&tsc->mixer->mutex);
2086 +static int snd_tsc2301_info_double(struct snd_kcontrol *kcontrol,
2087 + struct snd_ctl_elem_info *uinfo)
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);
2094 + uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN :
2095 + SNDRV_CTL_ELEM_TYPE_INTEGER;
2097 + uinfo->value.integer.min = minval;
2098 + uinfo->value.integer.max = mask;
2103 +static int snd_tsc2301_get_double(struct snd_kcontrol *kcontrol,
2104 + struct snd_ctl_elem_value *ucontrol)
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);
2116 + shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2118 + /* invert mute bits for the switches */
2120 + shadow_reg = ~shadow_reg;
2122 + ucontrol->value.integer.value[0] = (shadow_reg >> ls) & mask;
2123 + ucontrol->value.integer.value[1] = (shadow_reg >> rs) & mask;
2128 +static int snd_tsc2301_put_double(struct snd_kcontrol *kcontrol,
2129 + struct snd_ctl_elem_value *ucontrol)
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);
2143 + mutex_lock(&tsc->mixer->mutex);
2144 + shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2146 + /* zero bits to be modified */
2147 + shadow_reg &= ~((mask << ls) | (mask << rs));
2148 + /* modify with new value */
2150 + /* switch. Invert switch values for the mute bits */
2152 + ((~ucontrol->value.integer.value[0] & mask) << ls) |
2153 + ((~ucontrol->value.integer.value[1] & mask) << rs);
2157 + (ucontrol->value.integer.value[0] << ls) |
2158 + (ucontrol->value.integer.value[1] << rs);
2161 + changed = (shadow_reg != shadow_reg_old);
2162 + tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2164 + /* update into TSC2301 if necessary */
2166 + tsc2301_write_reg(tsc, reg, shadow_reg);
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);
2177 +static int snd_tsc2301_info_mux(struct snd_kcontrol *kcontrol,
2178 + struct snd_ctl_elem_info *uinfo)
2180 + static char *texts[4] = {"Mic", "Line", "Line swapped", "Line mono"};
2182 + uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
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]);
2193 +static int snd_tsc2301_get_mux(struct snd_kcontrol *kcontrol,
2194 + struct snd_ctl_elem_value *ucontrol)
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);
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;
2211 +static int snd_tsc2301_put_mux(struct snd_kcontrol *kcontrol,
2212 + struct snd_ctl_elem_value *ucontrol)
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);
2224 + mutex_lock(&tsc->mixer->mutex);
2225 + shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
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);
2233 + changed = (shadow_reg != shadow_reg_old);
2235 + /* update into TSC2301 if necessary */
2237 + tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2238 + tsc2301_write_reg(tsc, reg, shadow_reg);
2241 + /* check is need to power up/down audio blocks in case of ADC input
2243 + tsc2301_power_ctrl(tsc, shadow_index, 0);
2244 + mutex_unlock(&tsc->mixer->mutex);
2249 +static int snd_tsc2301_info_bool(struct snd_kcontrol *kcontrol,
2250 + struct snd_ctl_elem_info *uinfo)
2252 + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2254 + uinfo->value.integer.min = 0;
2255 + uinfo->value.integer.max = 1;
2260 +static int snd_tsc2301_get_bool(struct snd_kcontrol *kcontrol,
2261 + struct snd_ctl_elem_value *ucontrol)
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);
2270 + shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2271 + ucontrol->value.integer.value[0] =
2272 + invert ^ ((shadow_reg >> shift) & 1);
2277 +static int snd_tsc2301_put_bool(struct snd_kcontrol *kcontrol,
2278 + struct snd_ctl_elem_value *ucontrol)
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;
2289 + mutex_lock(&tsc->mixer->mutex);
2290 + shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2292 + /* zero bit to be modified */
2293 + shadow_reg &= ~(1 << shift);
2294 + /* modify with new value */
2296 + (invert ^ (ucontrol->value.integer.value[0] & 1)) << shift;
2298 + changed = (shadow_reg != shadow_reg_old);
2300 + /* update into TSC2301 if necessary */
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
2309 + tsc2301_gpio_power_down(tsc);
2311 + tsc2301_write_reg(tsc, reg, shadow_reg);
2313 + mutex_unlock(&tsc->mixer->mutex);
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,
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),
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,
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),
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,
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),
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),
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),
2363 +/* must be called tsc->mixer->mutex locked */
2364 +static void tsc2301_flush_shadow_regs(struct tsc2301 *tsc)
2366 + int i, page, addr;
2369 + page = TSC2301_REG_TO_PAGE(TSC2301_REG_AUDCNTL);
2370 + addr = TSC2301_REG_TO_ADDR(TSC2301_REG_AUDCNTL);
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);
2376 + temp = tsc->mixer->shadow_regs[GPIO_INDEX];
2377 + tsc2301_write_reg(tsc, TSC2301_REG_GPIO, temp);
2379 + /* Update power state of all audio blocks depending are they
2380 + * muted or unused. */
2381 + tsc2301_power_ctrl(tsc, -1, 0);
2385 +int tsc2301_mixer_suspend(struct tsc2301 *tsc)
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);
2400 +void tsc2301_mixer_resume(struct tsc2301 *tsc)
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);
2410 +void tsc2301_mixer_enable_mclk(struct device *dev)
2412 + struct tsc2301 *tsc = dev_get_drvdata(dev);
2413 + struct tsc2301_mixer *mix = tsc->mixer;
2415 + mutex_lock(&mix->mutex);
2416 + if (!mix->mclk_enabled++ && tsc->enable_clock != NULL) {
2417 + tsc->enable_clock(dev);
2419 + tsc2301_power_ctrl(tsc, -1, 1);
2420 + mutex_unlock(&mix->mutex);
2423 +void tsc2301_mixer_disable_mclk(struct device *dev)
2425 + struct tsc2301 *tsc = dev_get_drvdata(dev);
2426 + struct tsc2301_mixer *mix = tsc->mixer;
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);
2434 + mutex_unlock(&mix->mutex);
2437 +static void tsc2301_mixer_delayed_power_down(struct work_struct *work)
2439 + struct tsc2301_mixer *mix = container_of(work, struct tsc2301_mixer,
2440 + delayed_power_down.work);
2441 + struct tsc2301 *tsc = mix->tsc;
2443 + mutex_lock(&mix->mutex);
2444 + if (!mix->delayed_pd_active) {
2445 + mutex_unlock(&mix->mutex);
2448 + mix->delayed_pd_active = 0;
2449 + mutex_unlock(&mix->mutex);
2450 + tsc2301_mixer_disable_mclk(&tsc->spi->dev);
2454 + * Allows audio controller driver to notify its usage of ADC and DAC
2456 +void tsc2301_mixer_set_power(struct device *dev, int dac, int adc)
2458 + struct tsc2301 *tsc = dev_get_drvdata(dev);
2460 + mutex_lock(&tsc->mixer->mutex);
2461 + tsc->mixer->adc_enabled = adc;
2462 + tsc->mixer->dac_enabled = dac;
2464 + /* update power state of all audio blocks */
2465 + tsc2301_power_ctrl(tsc, -1, 1);
2466 + mutex_unlock(&tsc->mixer->mutex);
2470 + * Registers TSC2301 ALSA Mixer controls for the given sound card
2472 +int tsc2301_mixer_register_controls(struct device *dev, struct snd_card *card)
2474 + struct tsc2301 *tsc = dev_get_drvdata(dev);
2475 + struct tsc2301_mixer *mix = tsc->mixer;
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));
2486 + if (!mix->n_mixer_gpios)
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);
2498 + ctrlp = snd_ctl_new1(&ctrl, tsc);
2499 + err = snd_ctl_add(card, ctrlp);
2507 +int tsc2301_mixer_init(struct tsc2301 *tsc,
2508 + struct tsc2301_platform_data *pdata)
2510 + struct tsc2301_mixer *mix;
2514 + mix = kzalloc(sizeof(*mix), GFP_KERNEL);
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;
2525 + INIT_DELAYED_WORK(&mix->delayed_power_down,
2526 + tsc2301_mixer_delayed_power_down);
2528 + /* initialize shadow register default values */
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;
2538 + /* if extra mixer controls configured, then configure associated
2539 + * GPIOs as output and drive their default state */
2540 + if (pdata->n_mixer_gpios) {
2544 + for (i = 0; i < pdata->n_mixer_gpios; i++) {
2545 + const struct tsc2301_mixer_gpio *mg;
2548 + mg = pdata->mixer_gpios + i;
2550 + w |= (1 << gpio) << 8;
2551 + w |= (mg->inverted ^ mg->def_enable) << gpio;
2553 + mix->shadow_regs[GPIO_INDEX] = w;
2555 + mix->mixer_gpios = kmalloc(sizeof(*pdata->mixer_gpios) *
2556 + pdata->n_mixer_gpios,
2558 + if (mix->mixer_gpios == NULL) {
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;
2568 + tsc2301_write_pll(tsc, pdata->pll_n, pdata->pll_a, pdata->pll_pdc,
2569 + 0, mix->pll_output ? 0 : 1);
2571 + tsc2301_flush_shadow_regs(tsc);
2573 + if (mix->platform_init != NULL) {
2574 + err = mix->platform_init(&tsc->spi->dev);
2581 + if (mix->mixer_gpios != NULL)
2582 + kfree(mix->mixer_gpios);
2588 +void tsc2301_mixer_exit(struct tsc2301 *tsc)
2590 + struct tsc2301_mixer *mixer = tsc->mixer;
2592 + if (mixer->platform_cleanup != NULL)
2593 + mixer->platform_cleanup(&tsc->spi->dev);
2595 + if (mixer->mixer_gpios != NULL)
2596 + kfree(mixer->mixer_gpios);
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
2606 +#ifndef _LINUX_SPI_TSC2301_H
2607 +#define _LINUX_SPI_TSC2301_H
2609 +#include <linux/types.h>
2610 +#include <linux/timer.h>
2612 +struct tsc2301_platform_data {
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 */
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;
2645 + unsigned pll_pdc:4;
2648 + unsigned pll_output:1; /* Output PLL on GPIO_0 */
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;
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);
2663 + const struct tsc2301_mixer_gpio {
2666 + unsigned inverted:1;
2667 + unsigned def_enable:1; /* enable by default */
2668 + unsigned deactivate_on_pd:1; /* power-down flag */
2670 + int n_mixer_gpios;
2675 +struct tsc2301_mixer;
2678 + struct spi_device *spi;
2681 + u16 config2_shadow;
2683 + struct tsc2301_kp *kp;
2684 + struct tsc2301_ts *ts;
2685 + struct tsc2301_mixer *mixer;
2687 + int (*enable_clock)(struct device *dev);
2688 + void (*disable_clock)(struct device *dev);
2692 +#define TSC2301_HZ 33000000
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)
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)
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)
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
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
2752 +/* Master Clock Ratio */
2753 +#define TSC2301_MCLK_256xFS 0x00 /* default */
2754 +#define TSC2301_MCLK_384xFS 0x01
2755 +#define TSC2301_MCLK_512xFS 0x02
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);
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);
2772 +#define TSC2301_DECL_EMPTY_MOD(module) \
2773 +static inline int tsc2301_##module##_init(struct tsc2301 *tsc, \
2774 + struct tsc2301_platform_data *pdata) \
2778 +static inline void tsc2301_##module##_exit(struct tsc2301 *tsc) {} \
2779 +static inline int tsc2301_##module##_suspend(struct tsc2301 *tsc) \
2783 +static inline void tsc2301_##module##_resume(struct tsc2301 *tsc) {}
2785 +#ifdef CONFIG_KEYBOARD_TSC2301
2786 +TSC2301_DECL_MOD(kp)
2787 +void tsc2301_kp_restart(struct tsc2301 *tsc);
2789 +TSC2301_DECL_EMPTY_MOD(kp)
2790 +static inline void tsc2301_kp_restart(struct tsc2301 *tsc) {}
2793 +#ifdef CONFIG_TOUCHSCREEN_TSC2301
2794 +TSC2301_DECL_MOD(ts)
2796 +TSC2301_DECL_EMPTY_MOD(ts)
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);
2804 +extern int tsc2301_mixer_register_controls(struct device *tsc_dev,
2805 + struct snd_card *card);
2807 +TSC2301_DECL_EMPTY_MOD(mixer)
2810 +extern void tsc2301_mixer_enable_mclk(struct device *tsc_dev);
2811 +extern void tsc2301_mixer_disable_mclk(struct device *tsc_dev);