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 --- a/drivers/input/keyboard/Kconfig
28 +++ b/drivers/input/keyboard/Kconfig
29 @@ -530,6 +530,12 @@ config KEYBOARD_TNETV107X
30 To compile this driver as a module, choose M here: the
31 module will be called tnetv107x-keypad.
33 +config KEYBOARD_TSC2301
34 + tristate "TSC2301 keypad support"
35 + depends on SPI_TSC2301
37 + Say Y here for if you are using the keypad features of TSC2301.
39 config KEYBOARD_TWL4030
40 tristate "TI TWL4030/TWL5030/TPS659x0 keypad support"
41 depends on TWL4030_CORE
42 --- a/drivers/input/keyboard/Makefile
43 +++ b/drivers/input/keyboard/Makefile
44 @@ -48,6 +48,7 @@ obj-$(CONFIG_KEYBOARD_SUNKBD) += sunkbd
45 obj-$(CONFIG_KEYBOARD_TC3589X) += tc3589x-keypad.o
46 obj-$(CONFIG_KEYBOARD_TEGRA) += tegra-kbc.o
47 obj-$(CONFIG_KEYBOARD_TNETV107X) += tnetv107x-keypad.o
48 +obj-$(CONFIG_KEYBOARD_TSC2301) += tsc2301_kp.o
49 obj-$(CONFIG_KEYBOARD_TWL4030) += twl4030_keypad.o
50 obj-$(CONFIG_KEYBOARD_XTKBD) += xtkbd.o
51 obj-$(CONFIG_KEYBOARD_W90P910) += w90p910_keypad.o
53 +++ b/drivers/input/keyboard/tsc2301_kp.c
56 + * TSC2301 keypad driver
58 + * Copyright (C) 2005-2006 Nokia Corporation
60 + * Written by Jarkko Oikarinen
61 + * Rewritten by Juha Yrjola <juha.yrjola@nokia.com>
63 + * This program is free software; you can redistribute it and/or modify
64 + * it under the terms of the GNU General Public License as published by
65 + * the Free Software Foundation; either version 2 of the License, or
66 + * (at your option) any later version.
68 + * This program is distributed in the hope that it will be useful,
69 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
70 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
71 + * GNU General Public License for more details.
73 + * You should have received a copy of the GNU General Public License
74 + * along with this program; if not, write to the Free Software
75 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
79 +#include <linux/kernel.h>
80 +#include <linux/module.h>
81 +#include <linux/input.h>
82 +#include <linux/interrupt.h>
83 +#include <linux/irq.h>
84 +#include <linux/delay.h>
85 +#include <linux/spi/spi.h>
87 +#include <linux/spi/tsc2301.h>
89 +#define TSC2301_KEYBOARD_PRODUCT_ID 0x0051
90 +#define TSC2301_KEYBOARD_PRODUCT_VERSION 0x0001
91 +#define TSC2301_DEBOUNCE_TIME_2MS 0x0000
92 +#define TSC2301_DEBOUNCE_TIME_10MS 0x0800
93 +#define TSC2301_DEBOUNCE_TIME_20MS 0x1000
94 +#define TSC2301_DEBOUNCE_TIME_50MS 0x1800
95 +#define TSC2301_DEBOUNCE_TIME_60MS 0x2000
96 +#define TSC2301_DEBOUNCE_TIME_80MS 0x2800
97 +#define TSC2301_DEBOUNCE_TIME_100MS 0x3000
98 +#define TSC2301_DEBOUNCE_TIME_120MS 0x3800
100 +#define TSC2301_DEBOUNCE_TIME TSC2301_DEBOUNCE_TIME_20MS
102 +#define TSC2301_RELEASE_TIMEOUT 50
105 + struct input_dev *idev;
108 + struct mutex mutex;
109 + struct timer_list timer;
111 + unsigned pending:1;
112 + unsigned user_disabled:1;
113 + unsigned disable_depth;
115 + struct spi_transfer read_xfer[4];
116 + struct spi_message read_msg;
125 +static inline int tsc2301_kp_disabled(struct tsc2301 *tsc)
127 + return tsc->kp->disable_depth != 0;
130 +static void tsc2301_kp_send_key_events(struct tsc2301 *tsc,
134 + struct tsc2301_kp *kp = tsc->kp;
135 + u16 common, released, pressed;
138 + common = prev_state & new_state;
139 + released = common ^ prev_state;
140 + pressed = common ^ new_state;
141 + if (!released && !pressed)
143 + for (i = 0; i < 16 && (released || pressed); i++) {
144 + if (released & 1) {
145 + dev_dbg(&tsc->spi->dev, "key %d released\n", i);
146 + input_report_key(kp->idev, kp->keymap[i], 0);
150 + dev_dbg(&tsc->spi->dev, "key %d pressed\n", i);
151 + input_report_key(kp->idev, kp->keymap[i], 1);
155 + input_sync(kp->idev);
158 +static inline void _filter_out(struct tsc2301 *tsc, u16 prev_state,
159 + u16 *new_state, int row1, int row2, u8 rect_pat)
163 + mask = (rect_pat << (row1 * 4)) | (rect_pat << (row2 * 4));
164 + mask &= ~prev_state;
165 + *new_state &= ~mask;
166 + dev_dbg(&tsc->spi->dev, "filtering ghost keys %02x\n", mask);
169 +static void tsc2301_filter_ghost_keys(struct tsc2301 *tsc, u16 prev_state,
175 + static const u8 rect_pat[] = {
176 + 0x3, 0x5, 0x9, 0x6, 0xa, 0xc, 0,
179 + key_map = *new_state;
180 + for (row1 = 0; row1 < 4; row1++) {
181 + row1_map = (key_map >> (row1 * 4)) & 0xf;
184 + for (row2 = row1 + 1; row2 < 4; row2++) {
185 + u16 rect_map = (key_map >> (row2 * 4)) & 0xf;
188 + rect_map &= row1_map;
191 + for (rp = rect_pat; *rp; rp++)
192 + if ((rect_map & *rp) == *rp)
193 + _filter_out(tsc, prev_state, new_state,
199 +static void tsc2301_kp_timer(unsigned long arg)
201 + struct tsc2301 *tsc = (void *) arg;
202 + struct tsc2301_kp *kp = tsc->kp;
203 + unsigned long flags;
205 + tsc2301_kp_send_key_events(tsc, kp->keys_pressed, 0);
206 + spin_lock_irqsave(&kp->lock, flags);
207 + kp->keys_pressed = 0;
208 + spin_unlock_irqrestore(&kp->lock, flags);
211 +static void tsc2301_kp_rx(void *arg)
213 + struct tsc2301 *tsc = arg;
214 + struct tsc2301_kp *kp = tsc->kp;
215 + unsigned long flags;
218 + kp_data = kp->data;
219 + dev_dbg(&tsc->spi->dev, "KP data %04x\n", kp_data);
221 + tsc2301_filter_ghost_keys(tsc, kp->keys_pressed, &kp_data);
222 + tsc2301_kp_send_key_events(tsc, kp->keys_pressed, kp_data);
223 + spin_lock_irqsave(&kp->lock, flags);
224 + kp->keys_pressed = kp_data;
226 + spin_unlock_irqrestore(&kp->lock, flags);
229 +static irqreturn_t tsc2301_kp_irq_handler(int irq, void *dev_id)
231 + struct tsc2301 *tsc = dev_id;
232 + struct tsc2301_kp *kp = tsc->kp;
233 + unsigned long flags;
236 + spin_lock_irqsave(&kp->lock, flags);
237 + if (tsc2301_kp_disabled(tsc)) {
238 + spin_unlock_irqrestore(&kp->lock, flags);
239 + return IRQ_HANDLED;
242 + spin_unlock_irqrestore(&kp->lock, flags);
243 + mod_timer(&kp->timer,
244 + jiffies + msecs_to_jiffies(TSC2301_RELEASE_TIMEOUT));
245 + r = spi_async(tsc->spi, &tsc->kp->read_msg);
247 + dev_err(&tsc->spi->dev, "kp: spi_async() failed");
248 + return IRQ_HANDLED;
251 +static void tsc2301_kp_start_scan(struct tsc2301 *tsc)
253 + tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, tsc->kp->mask);
254 + tsc2301_write_reg(tsc, TSC2301_REG_KEY, TSC2301_DEBOUNCE_TIME);
257 +static void tsc2301_kp_stop_scan(struct tsc2301 *tsc)
259 + tsc2301_write_reg(tsc, TSC2301_REG_KEY, 1 << 14);
262 +/* Must be called with the mutex held */
263 +static void tsc2301_kp_enable(struct tsc2301 *tsc)
265 + struct tsc2301_kp *kp = tsc->kp;
266 + unsigned long flags;
268 + spin_lock_irqsave(&kp->lock, flags);
269 + BUG_ON(!tsc2301_kp_disabled(tsc));
270 + if (--kp->disable_depth != 0) {
271 + spin_unlock_irqrestore(&kp->lock, flags);
274 + spin_unlock_irqrestore(&kp->lock, flags);
276 + irq_set_irq_type(kp->irq, IRQ_TYPE_EDGE_FALLING);
277 + tsc2301_kp_start_scan(tsc);
278 + enable_irq(kp->irq);
281 +/* Must be called with the mutex held */
282 +static int tsc2301_kp_disable(struct tsc2301 *tsc, int release_keys)
284 + struct tsc2301_kp *kp = tsc->kp;
285 + unsigned long flags;
287 + spin_lock_irqsave(&kp->lock, flags);
288 + if (kp->disable_depth++ != 0) {
289 + spin_unlock_irqrestore(&kp->lock, flags);
292 + disable_irq_nosync(kp->irq);
293 + irq_set_irq_type(kp->irq, IRQ_TYPE_NONE);
294 + spin_unlock_irqrestore(&kp->lock, flags);
296 + while (kp->pending) {
300 + tsc2301_kp_stop_scan(tsc);
303 + del_timer(&kp->timer); /* let timeout release keys */
308 +/* The following workaround is needed for a HW bug triggered by the
310 + * 1. keep any key pressed
311 + * 2. disable keypad
312 + * 3. release all keys
313 + * 4. reenable keypad
314 + * 5. disable touch screen controller
316 + * After this the keypad scanner will get stuck in busy state and won't
317 + * report any interrupts for further keypresses. One way to recover is to
318 + * restart the keypad scanner whenever we enable / disable the
319 + * touchscreen controller.
321 +void tsc2301_kp_restart(struct tsc2301 *tsc)
323 + if (!tsc2301_kp_disabled(tsc)) {
324 + tsc2301_kp_start_scan(tsc);
328 +static ssize_t tsc2301_kp_disable_show(struct device *dev,
329 + struct device_attribute *attr, char *buf)
331 + struct tsc2301 *tsc = dev_get_drvdata(dev);
333 + return sprintf(buf, "%u\n", tsc2301_kp_disabled(tsc) ? 1 : 0);
336 +static ssize_t tsc2301_kp_disable_store(struct device *dev,
337 + struct device_attribute *attr,
338 + const char *buf, size_t count)
340 + struct tsc2301 *tsc = dev_get_drvdata(dev);
341 + struct tsc2301_kp *kp = tsc->kp;
345 + i = simple_strtoul(buf, &endp, 10);
348 + mutex_lock(&kp->mutex);
349 + if (i == kp->user_disabled) {
350 + mutex_unlock(&kp->mutex);
353 + kp->user_disabled = i;
356 + tsc2301_kp_disable(tsc, 1);
358 + tsc2301_kp_enable(tsc);
359 + mutex_unlock(&kp->mutex);
364 +static DEVICE_ATTR(disable_kp, 0664, tsc2301_kp_disable_show,
365 + tsc2301_kp_disable_store);
367 +static const u16 tsc2301_kp_read_data = 0x8000 | TSC2301_REG_KPDATA;
369 +static void tsc2301_kp_setup_spi_xfer(struct tsc2301 *tsc)
371 + struct tsc2301_kp *kp = tsc->kp;
372 + struct spi_message *m = &kp->read_msg;
373 + struct spi_transfer *x = &kp->read_xfer[0];
375 + spi_message_init(&kp->read_msg);
377 + x->tx_buf = &tsc2301_kp_read_data;
379 + spi_message_add_tail(x, m);
382 + x->rx_buf = &kp->data;
384 + spi_message_add_tail(x, m);
386 + m->complete = tsc2301_kp_rx;
391 +int tsc2301_kp_suspend(struct tsc2301 *tsc)
393 + struct tsc2301_kp *kp = tsc->kp;
395 + mutex_lock(&kp->mutex);
396 + tsc2301_kp_disable(tsc, 1);
397 + mutex_unlock(&kp->mutex);
401 +void tsc2301_kp_resume(struct tsc2301 *tsc)
403 + struct tsc2301_kp *kp = tsc->kp;
405 + mutex_lock(&kp->mutex);
406 + tsc2301_kp_enable(tsc);
407 + mutex_unlock(&kp->mutex);
411 +int __devinit tsc2301_kp_init(struct tsc2301 *tsc,
412 + struct tsc2301_platform_data *pdata)
414 + struct input_dev *idev;
415 + struct tsc2301_kp *kp;
419 + if (pdata->keyb_int < 0) {
420 + dev_err(&tsc->spi->dev, "need kbirq");
424 + kp = kzalloc(sizeof(*kp), GFP_KERNEL);
429 + kp->irq = pdata->keyb_int;
430 + spin_lock_init(&kp->lock);
431 + mutex_init(&kp->mutex);
433 + init_timer(&kp->timer);
434 + kp->timer.data = (unsigned long) tsc;
435 + kp->timer.function = tsc2301_kp_timer;
437 + idev = input_allocate_device();
438 + if (idev == NULL) {
442 + if (pdata->keyb_name)
443 + idev->name = pdata->keyb_name;
445 + idev->name = "TSC2301 keypad";
446 + snprintf(kp->phys, sizeof(kp->phys), "%s/input-kp", dev_name(&tsc->spi->dev));
447 + idev->phys = kp->phys;
450 + idev->evbit[0] = BIT(EV_KEY);
451 + for (i = 0; i < 16; i++) {
452 + if (pdata->keymap[i] > 0) {
453 + set_bit(pdata->keymap[i], idev->keybit);
454 + kp->keymap[i] = pdata->keymap[i];
456 + kp->keymap[i] = -1;
462 + set_bit(EV_REP, idev->evbit);
466 + tsc2301_kp_setup_spi_xfer(tsc);
468 + r = device_create_file(&tsc->spi->dev, &dev_attr_disable_kp);
472 + tsc2301_kp_start_scan(tsc);
474 + /* IRQ mode 0 is faulty, it can cause the KBIRQ to get stuck.
475 + * Mode 2 deasserts the IRQ at:
477 + * - Setting SCS flag in REG_KEY register
478 + * - Releasing all keys
479 + * - Reading the REG_KPDATA
481 + tsc2301_write_kbc(tsc, 2);
483 + tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, mask);
486 + irq_set_irq_type(kp->irq, IRQ_TYPE_EDGE_FALLING);
488 + r = request_irq(kp->irq, tsc2301_kp_irq_handler, IRQF_SAMPLE_RANDOM,
489 + "tsc2301-kp", tsc);
491 + dev_err(&tsc->spi->dev, "unable to get kbirq IRQ");
494 + irq_set_irq_wake(kp->irq, 1);
496 + /* We need to read the register once..? */
497 + tsc2301_read_reg(tsc, TSC2301_REG_KPDATA);
499 + r = input_register_device(idev);
501 + dev_err(&tsc->spi->dev, "can't register keypad device\n");
508 + free_irq(kp->irq, tsc);
510 + tsc2301_kp_stop_scan(tsc);
511 + device_remove_file(&tsc->spi->dev, &dev_attr_disable_kp);
513 + input_free_device(kp->idev);
519 +void __devexit tsc2301_kp_exit(struct tsc2301 *tsc)
521 + struct tsc2301_kp *kp = tsc->kp;
523 + tsc2301_kp_disable(tsc, 1);
524 + input_unregister_device(kp->idev);
525 + free_irq(kp->irq, tsc);
526 + device_remove_file(&tsc->spi->dev, &dev_attr_disable_kp);
530 --- a/drivers/input/touchscreen/Kconfig
531 +++ b/drivers/input/touchscreen/Kconfig
532 @@ -673,6 +673,12 @@ config TOUCHSCREEN_TSC2007
533 To compile this driver as a module, choose M here: the
534 module will be called tsc2007.
536 +config TOUCHSCREEN_TSC2301
537 + tristate "TSC2301 touchscreen support"
538 + depends on SPI_TSC2301
540 + Say Y here for if you are using the touchscreen features of TSC2301.
542 config TOUCHSCREEN_W90X900
543 tristate "W90P910 touchscreen driver"
545 --- a/drivers/input/touchscreen/Makefile
546 +++ b/drivers/input/touchscreen/Makefile
547 @@ -48,6 +48,7 @@ obj-$(CONFIG_TOUCHSCREEN_TOUCHRIGHT) +=
548 obj-$(CONFIG_TOUCHSCREEN_TOUCHWIN) += touchwin.o
549 obj-$(CONFIG_TOUCHSCREEN_TSC2005) += tsc2005.o
550 obj-$(CONFIG_TOUCHSCREEN_TSC2007) += tsc2007.o
551 +obj-$(CONFIG_TOUCHSCREEN_TSC2301) += tsc2301_ts.o
552 obj-$(CONFIG_TOUCHSCREEN_UCB1400) += ucb1400_ts.o
553 obj-$(CONFIG_TOUCHSCREEN_WACOM_W8001) += wacom_w8001.o
554 obj-$(CONFIG_TOUCHSCREEN_WM831X) += wm831x-ts.o
556 +++ b/drivers/input/touchscreen/tsc2301_ts.c
559 + * TSC2301 touchscreen driver
561 + * Copyright (C) 2005-2008 Nokia Corporation
563 + * Written by Jarkko Oikarinen, Imre Deak and Juha Yrjola
565 + * This program is free software; you can redistribute it and/or modify
566 + * it under the terms of the GNU General Public License as published by
567 + * the Free Software Foundation; either version 2 of the License, or
568 + * (at your option) any later version.
570 + * This program is distributed in the hope that it will be useful,
571 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
572 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
573 + * GNU General Public License for more details.
575 + * You should have received a copy of the GNU General Public License
576 + * along with this program; if not, write to the Free Software
577 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
581 +#include <linux/kernel.h>
582 +#include <linux/module.h>
583 +#include <linux/input.h>
584 +#include <linux/interrupt.h>
585 +#include <linux/delay.h>
586 +#include <linux/spi/spi.h>
588 +#include <linux/spi/tsc2301.h>
591 + * The touchscreen interface operates as follows:
594 + * Request access to GPIO103 (DAV)
595 + * tsc2301_ts_irq_handler will trigger when DAV line goes down
597 + * 1) Pen is pressed against touchscreeen
598 + * 2) TSC2301 performs AD conversion
599 + * 3) After the conversion is done TSC2301 drives DAV line down
600 + * 4) GPIO IRQ is received and tsc2301_ts_irq_handler is called
601 + * 5) tsc2301_ts_irq_handler queues up an spi transfer to fetch
602 + * the x, y, z1, z2 values
603 + * 6) SPI framework calls tsc2301_ts_rx after the coordinates are read
604 + * 7) When the penup_timer expires, there have not been DAV interrupts
605 + * during the last 20ms which means the pen has been lifted.
609 +#define TSC2301_TOUCHSCREEN_PRODUCT_ID 0x0052
610 +#define TSC2301_TOUCHSCREEN_PRODUCT_VERSION 0x0001
612 +#define TSC2301_TS_PENUP_TIME 20
614 +#define TSC2301_ADCREG_CONVERSION_CTRL_BY_TSC2301 0x8000
615 +#define TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST 0x0000
617 +#define TSC2301_ADCREG_FUNCTION_NONE 0x0000
618 +#define TSC2301_ADCREG_FUNCTION_XY 0x0400
619 +#define TSC2301_ADCREG_FUNCTION_XYZ 0x0800
620 +#define TSC2301_ADCREG_FUNCTION_X 0x0C00
621 +#define TSC2301_ADCREG_FUNCTION_Y 0x1000
622 +#define TSC2301_ADCREG_FUNCTION_Z 0x1400
623 +#define TSC2301_ADCREG_FUNCTION_DAT1 0x1800
624 +#define TSC2301_ADCREG_FUNCTION_DAT2 0x1C00
625 +#define TSC2301_ADCREG_FUNCTION_AUX1 0x2000
626 +#define TSC2301_ADCREG_FUNCTION_AUX2 0x2400
627 +#define TSC2301_ADCREG_FUNCTION_TEMP 0x2800
629 +#define TSC2301_ADCREG_RESOLUTION_8BIT 0x0100
630 +#define TSC2301_ADCREG_RESOLUTION_10BIT 0x0200
631 +#define TSC2301_ADCREG_RESOLUTION_12BIT 0x0300
633 +#define TSC2301_ADCREG_AVERAGING_NONE 0x0000
634 +#define TSC2301_ADCREG_AVERAGING_4AVG 0x0040
635 +#define TSC2301_ADCREG_AVERAGING_8AVG 0x0080
636 +#define TSC2301_ADCREG_AVERAGING_16AVG 0x00C0
638 +#define TSC2301_ADCREG_CLOCK_8MHZ 0x0000
639 +#define TSC2301_ADCREG_CLOCK_4MHZ 0x0010
640 +#define TSC2301_ADCREG_CLOCK_2MHZ 0x0020
641 +#define TSC2301_ADCREG_CLOCK_1MHZ 0x0030
643 +#define TSC2301_ADCREG_VOLTAGE_STAB_0US 0x0000
644 +#define TSC2301_ADCREG_VOLTAGE_STAB_100US 0x0002
645 +#define TSC2301_ADCREG_VOLTAGE_STAB_500US 0x0004
646 +#define TSC2301_ADCREG_VOLTAGE_STAB_1MS 0x0006
647 +#define TSC2301_ADCREG_VOLTAGE_STAB_5MS 0x0008
648 +#define TSC2301_ADCREG_VOLTAGE_STAB_10MS 0x000A
649 +#define TSC2301_ADCREG_VOLTAGE_STAB_50MS 0x000C
650 +#define TSC2301_ADCREG_VOLTAGE_STAB_100MS 0x000E
652 +#define TSC2301_ADCREG_STOP_CONVERSION 0x4000
654 +#define MAX_12BIT ((1 << 12) - 1)
656 +#define TS_RECT_SIZE 8
657 +#define TSF_MIN_Z1 100
658 +#define TSF_MAX_Z2 4000
660 +#define TSF_SAMPLES 4
679 + struct input_dev *idev;
681 + struct timer_list penup_timer;
682 + struct mutex mutex;
684 + struct spi_transfer read_xfer[2];
685 + struct spi_message read_msg;
686 + struct ts_coords *coords;
688 + struct ts_filter filter;
698 + int touch_pressure;
710 +static const u16 tsc2301_ts_read_data = 0x8000 | TSC2301_REG_X;
712 +static int tsc2301_ts_check_config(struct tsc2301_ts *ts, int *hw_flags)
717 + switch (ts->hw_avg_max) {
719 + flags |= TSC2301_ADCREG_AVERAGING_NONE;
722 + flags |= TSC2301_ADCREG_AVERAGING_4AVG;
725 + flags |= TSC2301_ADCREG_AVERAGING_8AVG;
728 + flags |= TSC2301_ADCREG_AVERAGING_16AVG;
734 + switch (ts->stab_time) {
736 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_0US;
739 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_100US;
742 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_500US;
745 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_1MS;
748 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_5MS;
751 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_10MS;
754 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_50MS;
757 + flags |= TSC2301_ADCREG_VOLTAGE_STAB_100MS;
768 + * This odd three-time initialization is to work around a bug in TSC2301.
769 + * See TSC2301 errata for details.
771 +static int tsc2301_ts_configure(struct tsc2301 *tsc, int flags)
773 + struct spi_transfer xfer[5];
774 + struct spi_transfer *x;
775 + struct spi_message m;
777 + u16 val1, val2, val3;
780 + val1 = TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST |
781 + TSC2301_ADCREG_STOP_CONVERSION |
782 + TSC2301_ADCREG_FUNCTION_NONE |
783 + TSC2301_ADCREG_RESOLUTION_12BIT |
784 + TSC2301_ADCREG_AVERAGING_NONE |
785 + TSC2301_ADCREG_CLOCK_2MHZ |
786 + TSC2301_ADCREG_VOLTAGE_STAB_100MS;
788 + val2 = TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST |
789 + TSC2301_ADCREG_FUNCTION_XYZ |
790 + TSC2301_ADCREG_RESOLUTION_12BIT |
791 + TSC2301_ADCREG_AVERAGING_16AVG |
792 + TSC2301_ADCREG_CLOCK_1MHZ |
793 + TSC2301_ADCREG_VOLTAGE_STAB_100MS;
795 + /* Averaging and voltage stabilization settings in flags */
796 + val3 = TSC2301_ADCREG_CONVERSION_CTRL_BY_TSC2301 |
797 + TSC2301_ADCREG_FUNCTION_XYZ |
798 + TSC2301_ADCREG_RESOLUTION_12BIT |
799 + TSC2301_ADCREG_CLOCK_2MHZ |
802 + /* Now we prepare the command for transferring */
803 + data[0] = TSC2301_REG_ADC;
805 + data[2] = TSC2301_REG_ADC;
807 + data[4] = TSC2301_REG_ADC;
809 + data[6] = TSC2301_REG_REF;
810 + data[7] = 1 << 4 | 1 << 2 | 1; /* intref, 100uS settl, 2.5V ref */
811 + data[8] = TSC2301_REG_CONFIG;
812 + data[9] = 3 << 3 | 2 << 0; /* 340uS pre-chrg, 544us delay */
814 + spi_message_init(&m);
817 + memset(xfer, 0, sizeof(xfer));
820 + for (i = 0; i < 10; i += 2) {
821 + x->tx_buf = &data[i];
825 + spi_message_add_tail(x, &m);
828 + spi_sync(m.spi, &m);
833 +static void tsc2301_ts_start_scan(struct tsc2301 *tsc)
835 + tsc2301_ts_configure(tsc, tsc->ts->hw_flags);
836 + tsc2301_kp_restart(tsc);
839 +static void tsc2301_ts_stop_scan(struct tsc2301 *tsc)
841 + tsc2301_write_reg(tsc, TSC2301_REG_ADC, TSC2301_ADCREG_STOP_CONVERSION);
842 + tsc2301_kp_restart(tsc);
845 +static void update_pen_state(struct tsc2301_ts *ts, int x, int y, int pressure)
848 + input_report_abs(ts->idev, ABS_X, x);
849 + input_report_abs(ts->idev, ABS_Y, y);
850 + input_report_abs(ts->idev, ABS_PRESSURE, pressure);
852 + input_report_key(ts->idev, BTN_TOUCH, 1);
855 + input_report_abs(ts->idev, ABS_PRESSURE, 0);
857 + input_report_key(ts->idev, BTN_TOUCH, 0);
861 + input_sync(ts->idev);
864 + dev_dbg(&tsc->spi->dev, "x %4d y %4d p %4d\n", x, y, pressure);
868 +static int filter(struct tsc2301_ts *ts, int x, int y, int z1, int z2)
870 + int inside_rect, pressure_limit, Rt;
871 + struct ts_filter *tsf = &ts->filter;
873 + /* validate pressure and position */
874 + if (x > MAX_12BIT || y > MAX_12BIT)
877 + /* skip coords if the pressure-components are out of range */
878 + if (z1 < TSF_MIN_Z1 || z2 > TSF_MAX_Z2)
881 + /* Use the x,y,z1,z2 directly on the first "pen down" event */
882 + if (ts->event_sent) {
888 + if (++tsf->sample_cnt < TSF_SAMPLES)
890 + x = tsf->avg_x / TSF_SAMPLES;
891 + y = tsf->avg_y / TSF_SAMPLES;
892 + z1 = tsf->avg_z1 / TSF_SAMPLES;
893 + z2 = tsf->avg_z2 / TSF_SAMPLES;
895 + tsf->sample_cnt = 0;
901 + pressure_limit = ts->event_sent? ts->max_pressure: ts->touch_pressure;
903 + /* z1 is always at least 100: */
904 + Rt = x * (z2 - z1) / z1;
905 + Rt = Rt * ts->x_plate_ohm / 4096;
906 + if (Rt > pressure_limit)
909 + /* discard the event if it still is within the previous rect - unless
910 + * if the pressure is harder, but then use previous x,y position */
912 + x > (int)ts->x - TS_RECT_SIZE && x < (int)ts->x + TS_RECT_SIZE &&
913 + y > (int)ts->y - TS_RECT_SIZE && y < (int)ts->y + TS_RECT_SIZE);
915 + if (!ts->event_sent || !inside_rect) {
920 + } else if (Rt < ts->p) {
928 + * This procedure is called by the SPI framework after the coordinates
929 + * have been read from TSC2301
931 +static void tsc2301_ts_rx(void *arg)
933 + struct tsc2301 *tsc = arg;
934 + struct tsc2301_ts *ts = tsc->ts;
940 + z1 = ts->coords->z1;
941 + z2 = ts->coords->z2;
943 + send_event = filter(ts, x, y, z1, z2);
945 + update_pen_state(ts, ts->x, ts->y, ts->p);
946 + ts->event_sent = 1;
949 + mod_timer(&ts->penup_timer,
950 + jiffies + msecs_to_jiffies(TSC2301_TS_PENUP_TIME));
954 + * Timer is called TSC2301_TS_PENUP_TIME after pen is up
956 +static void tsc2301_ts_timer_handler(unsigned long data)
958 + struct tsc2301 *tsc = (struct tsc2301 *)data;
959 + struct tsc2301_ts *ts = tsc->ts;
961 + if (ts->event_sent) {
962 + ts->event_sent = 0;
963 + update_pen_state(ts, 0, 0, 0);
968 + * This interrupt is called when pen is down and coordinates are
969 + * available. That is indicated by a falling edge on DEV line.
971 +static irqreturn_t tsc2301_ts_irq_handler(int irq, void *dev_id)
973 + struct tsc2301 *tsc = dev_id;
974 + struct tsc2301_ts *ts = tsc->ts;
977 + r = spi_async(tsc->spi, &ts->read_msg);
979 + dev_err(&tsc->spi->dev, "ts: spi_async() failed");
981 + mod_timer(&ts->penup_timer,
982 + jiffies + msecs_to_jiffies(TSC2301_TS_PENUP_TIME));
984 + return IRQ_HANDLED;
987 +static void tsc2301_ts_disable(struct tsc2301 *tsc)
989 + struct tsc2301_ts *ts = tsc->ts;
991 + if (ts->disable_depth++ != 0)
994 + disable_irq(ts->irq);
996 + /* wait until penup timer expire normally */
999 + } while (ts->event_sent);
1001 + tsc2301_ts_stop_scan(tsc);
1004 +static void tsc2301_ts_enable(struct tsc2301 *tsc)
1006 + struct tsc2301_ts *ts = tsc->ts;
1008 + if (--ts->disable_depth != 0)
1011 + enable_irq(ts->irq);
1013 + tsc2301_ts_start_scan(tsc);
1017 +int tsc2301_ts_suspend(struct tsc2301 *tsc)
1019 + struct tsc2301_ts *ts = tsc->ts;
1021 + mutex_lock(&ts->mutex);
1022 + tsc2301_ts_disable(tsc);
1023 + mutex_unlock(&ts->mutex);
1028 +void tsc2301_ts_resume(struct tsc2301 *tsc)
1030 + struct tsc2301_ts *ts = tsc->ts;
1032 + mutex_lock(&ts->mutex);
1033 + tsc2301_ts_enable(tsc);
1034 + mutex_unlock(&ts->mutex);
1038 +static void tsc2301_ts_setup_spi_xfer(struct tsc2301 *tsc)
1040 + struct tsc2301_ts *ts = tsc->ts;
1041 + struct spi_message *m = &ts->read_msg;
1042 + struct spi_transfer *x = &ts->read_xfer[0];
1044 + spi_message_init(m);
1046 + x->tx_buf = &tsc2301_ts_read_data;
1048 + spi_message_add_tail(x, m);
1051 + x->rx_buf = ts->coords;
1053 + spi_message_add_tail(x, m);
1055 + m->complete = tsc2301_ts_rx;
1059 +static ssize_t tsc2301_ts_pen_down_show(struct device *dev,
1060 + struct device_attribute *attr,
1063 + struct tsc2301 *tsc = dev_get_drvdata(dev);
1065 + return sprintf(buf, "%u\n", tsc->ts->pen_down);
1068 +static DEVICE_ATTR(pen_down, S_IRUGO, tsc2301_ts_pen_down_show, NULL);
1070 +static ssize_t tsc2301_ts_disable_show(struct device *dev,
1071 + struct device_attribute *attr, char *buf)
1073 + struct tsc2301 *tsc = dev_get_drvdata(dev);
1074 + struct tsc2301_ts *ts = tsc->ts;
1076 + return sprintf(buf, "%u\n", ts->disabled);
1079 +static ssize_t tsc2301_ts_disable_store(struct device *dev,
1080 + struct device_attribute *attr,
1081 + const char *buf, size_t count)
1083 + struct tsc2301 *tsc = dev_get_drvdata(dev);
1084 + struct tsc2301_ts *ts = tsc->ts;
1088 + i = simple_strtoul(buf, &endp, 10);
1090 + mutex_lock(&ts->mutex);
1091 + if (i == ts->disabled) goto out;
1095 + tsc2301_ts_disable(tsc);
1097 + tsc2301_ts_enable(tsc);
1099 + mutex_unlock(&ts->mutex);
1103 +static DEVICE_ATTR(disable_ts, 0664, tsc2301_ts_disable_show,
1104 + tsc2301_ts_disable_store);
1106 +int __devinit tsc2301_ts_init(struct tsc2301 *tsc,
1107 + struct tsc2301_platform_data *pdata)
1109 + struct tsc2301_ts *ts;
1110 + struct input_dev *idev;
1113 + int x_fudge, y_fudge, p_fudge;
1115 + if (pdata->dav_int <= 0) {
1116 + dev_err(&tsc->spi->dev, "need DAV IRQ");
1120 + ts = kzalloc(sizeof(*ts), GFP_KERNEL);
1125 + ts->coords = kzalloc(sizeof(*ts->coords), GFP_KERNEL);
1126 + if (ts->coords == NULL) {
1131 + ts->irq = pdata->dav_int;
1133 + init_timer(&ts->penup_timer);
1134 + setup_timer(&ts->penup_timer, tsc2301_ts_timer_handler,
1135 + (unsigned long)tsc);
1137 + mutex_init(&ts->mutex);
1139 + ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
1140 + ts->hw_avg_max = pdata->ts_hw_avg;
1141 + ts->max_pressure = pdata->ts_max_pressure ? : MAX_12BIT;
1142 + ts->touch_pressure = pdata->ts_touch_pressure ? : ts->max_pressure;
1143 + ts->stab_time = pdata->ts_stab_time;
1145 + x_max = pdata->ts_x_max ? : 4096;
1146 + y_max = pdata->ts_y_max ? : 4096;
1147 + x_fudge = pdata->ts_x_fudge ? : 4;
1148 + y_fudge = pdata->ts_y_fudge ? : 8;
1149 + p_fudge = pdata->ts_pressure_fudge ? : 2;
1151 + if ((r = tsc2301_ts_check_config(ts, &ts->hw_flags))) {
1152 + dev_err(&tsc->spi->dev, "invalid configuration\n");
1156 + idev = input_allocate_device();
1157 + if (idev == NULL) {
1161 + idev->name = "TSC2301 touchscreen";
1162 + snprintf(ts->phys, sizeof(ts->phys),
1163 + "%s/input-ts", dev_name(&tsc->spi->dev));
1164 + idev->phys = ts->phys;
1165 + idev->dev.parent = &tsc->spi->dev;
1167 + idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
1168 + idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
1171 + tsc2301_ts_setup_spi_xfer(tsc);
1173 + /* These parameters should perhaps be configurable? */
1174 + input_set_abs_params(idev, ABS_X, 0, x_max, x_fudge, 0);
1175 + input_set_abs_params(idev, ABS_Y, 0, y_max, y_fudge, 0);
1176 + input_set_abs_params(idev, ABS_PRESSURE, 0, ts->max_pressure,
1179 + tsc2301_ts_start_scan(tsc);
1181 + r = request_irq(ts->irq, tsc2301_ts_irq_handler,
1182 + IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
1183 + "tsc2301-ts", tsc);
1185 + dev_err(&tsc->spi->dev, "unable to get DAV IRQ");
1188 + irq_set_irq_wake(ts->irq, 1);
1190 + if (device_create_file(&tsc->spi->dev, &dev_attr_pen_down) < 0)
1192 + if (device_create_file(&tsc->spi->dev, &dev_attr_disable_ts) < 0)
1195 + r = input_register_device(idev);
1197 + dev_err(&tsc->spi->dev, "can't register touchscreen device\n");
1203 + device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
1205 + device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
1207 + free_irq(ts->irq, tsc);
1209 + tsc2301_ts_stop_scan(tsc);
1210 + input_free_device(idev);
1212 + kfree(ts->coords);
1217 +void __devexit tsc2301_ts_exit(struct tsc2301 *tsc)
1219 + struct tsc2301_ts *ts = tsc->ts;
1221 + tsc2301_ts_disable(tsc);
1223 + device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
1224 + device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
1226 + free_irq(ts->irq, tsc);
1227 + input_unregister_device(ts->idev);
1229 + kfree(ts->coords);
1232 +MODULE_AUTHOR("Jarkko Oikarinen <jarkko.oikarinen@nokia.com>");
1233 +MODULE_LICENSE("GPL");
1234 --- a/drivers/spi/Kconfig
1235 +++ b/drivers/spi/Kconfig
1236 @@ -435,6 +435,28 @@ config SPI_TLE62X0
1237 sysfs interface, with each line presented as a kind of GPIO
1238 exposing both switch control and diagnostic feedback.
1241 + tristate "TSC2301 driver"
1242 + depends on SPI_MASTER
1244 + Say Y here if you have a TSC2301 chip connected to an SPI
1245 + bus on your board.
1247 + The TSC2301 is a highly integrated PDA analog interface circuit.
1248 + It contains a complete 12-bit A/D resistive touch screen
1249 + converter (ADC) including drivers, touch pressure measurement
1250 + capability, keypad controller, and 8-bit D/A converter (DAC) output
1251 + for LCD contrast control.
1253 + To compile this driver as a module, choose M here: the
1254 + module will be called tsc2301.
1256 +config SPI_TSC2301_AUDIO
1257 + boolean "TSC2301 audio support"
1258 + depends on SPI_TSC2301 && SND
1260 + Say Y here for if you are using the audio features of TSC2301.
1263 # Add new SPI protocol masters in alphabetical order above this line
1265 --- a/drivers/spi/Makefile
1266 +++ b/drivers/spi/Makefile
1267 @@ -59,4 +59,6 @@ obj-$(CONFIG_SPI_TLE62X0) += spi-tle62x
1268 obj-$(CONFIG_SPI_TOPCLIFF_PCH) += spi-topcliff-pch.o
1269 obj-$(CONFIG_SPI_TXX9) += spi-txx9.o
1270 obj-$(CONFIG_SPI_XILINX) += spi-xilinx.o
1272 +obj-$(CONFIG_SPI_TSC2301) += tsc2301.o
1273 +tsc2301-objs := tsc2301-core.o
1274 +tsc2301-$(CONFIG_SPI_TSC2301_AUDIO) += tsc2301-mixer.o
1276 +++ b/drivers/spi/tsc2301-core.c
1281 + * Copyright (C) 2005, 2006 Nokia Corporation
1283 + * This program is free software; you can redistribute it and/or modify
1284 + * it under the terms of the GNU General Public License as published by
1285 + * the Free Software Foundation; either version 2 of the License, or
1286 + * (at your option) any later version.
1288 + * This program is distributed in the hope that it will be useful,
1289 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1290 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1291 + * GNU General Public License for more details.
1293 + * You should have received a copy of the GNU General Public License
1294 + * along with this program; if not, write to the Free Software
1295 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1299 +#include <linux/module.h>
1300 +#include <linux/device.h>
1301 +#include <linux/delay.h>
1302 +#include <linux/gpio.h>
1303 +#include <linux/spi/spi.h>
1304 +#include <linux/spi/tsc2301.h>
1306 +u16 tsc2301_read_reg(struct tsc2301 *tsc, int reg)
1308 + struct spi_transfer t[2];
1309 + struct spi_message m;
1310 + u16 data = 0, cmd;
1315 + memset(t, 0, sizeof(t));
1316 + spi_message_init(&m);
1319 + t[0].tx_buf = &cmd;
1320 + t[0].rx_buf = NULL;
1322 + spi_message_add_tail(&t[0], &m);
1324 + t[1].tx_buf = NULL;
1325 + t[1].rx_buf = &data;
1327 + spi_message_add_tail(&t[1], &m);
1329 + spi_sync(m.spi, &m);
1334 +void tsc2301_write_reg(struct tsc2301 *tsc, int reg, u16 val)
1336 + struct spi_transfer t;
1337 + struct spi_message m;
1340 + /* Now we prepare the command for transferring */
1344 + spi_message_init(&m);
1347 + memset(&t, 0, sizeof(t));
1351 + spi_message_add_tail(&t, &m);
1353 + spi_sync(m.spi, &m);
1356 +void tsc2301_write_kbc(struct tsc2301 *tsc, int val)
1360 + w = tsc->config2_shadow;
1361 + w &= ~(0x03 << 14);
1362 + w |= (val & 0x03) << 14;
1363 + tsc2301_write_reg(tsc, TSC2301_REG_CONFIG2, w);
1364 + tsc->config2_shadow = w;
1367 +void tsc2301_write_pll(struct tsc2301 *tsc,
1368 + int pll_n, int pll_a, int pll_pdc, int pct_e, int pll_o)
1372 + w = tsc->config2_shadow;
1374 + w |= (pll_n & 0x0f) | ((pll_a & 0x0f) << 4) | ((pll_pdc & 0x0f) << 8);
1375 + w |= pct_e ? (1 << 12) : 0;
1376 + w |= pll_o ? (1 << 13) : 0;
1377 + tsc2301_write_reg(tsc, TSC2301_REG_CONFIG2, w);
1378 + tsc->config2_shadow = w;
1381 +void tsc2301_read_buf(struct tsc2301 *tsc, int reg, u16 *rx_buf, int len)
1383 + struct spi_transfer t[2];
1384 + struct spi_message m;
1390 + spi_message_init(&m);
1393 + memset(t, 0, sizeof(t));
1394 + t[0].tx_buf = &cmd;
1395 + t[0].rx_buf = NULL;
1397 + spi_message_add_tail(&t[0], &m);
1399 + t[1].tx_buf = NULL;
1400 + t[1].rx_buf = rx_buf;
1401 + t[1].len = 2 * len;
1402 + spi_message_add_tail(&t[1], &m);
1404 + spi_sync(m.spi, &m);
1406 + for (i = 0; i < len; i++)
1407 + printk(KERN_DEBUG "rx_buf[%d]: %04x\n", i, rx_buf[i]);
1410 +static int __devinit tsc2301_probe(struct spi_device *spi)
1412 + struct tsc2301 *tsc;
1413 + struct tsc2301_platform_data *pdata = spi->dev.platform_data;
1418 + dev_dbg(&spi->dev, "no platform data?\n");
1422 + tsc = kzalloc(sizeof(*tsc), GFP_KERNEL);
1426 + dev_set_drvdata(&spi->dev, tsc);
1429 + tsc->enable_clock = pdata->enable_clock;
1430 + tsc->disable_clock = pdata->disable_clock;
1432 + if (pdata->reset_gpio >= 0) {
1433 + tsc->reset_gpio = pdata->reset_gpio;
1434 + r = gpio_request(tsc->reset_gpio, "TSC2301 reset");
1437 + gpio_direction_output(tsc->reset_gpio, 1);
1439 + gpio_set_value(tsc->reset_gpio, 0);
1441 + tsc->reset_gpio = -1;
1443 + spi->mode = SPI_MODE_1;
1444 + spi->bits_per_word = 16;
1445 + /* The max speed might've been defined by the board-specific
1447 + if (!spi->max_speed_hz)
1448 + spi->max_speed_hz = TSC2301_HZ;
1452 + tsc2301_write_reg(tsc, TSC2301_REG_RESET, 0xbb00);
1455 + w = tsc2301_read_reg(tsc, TSC2301_REG_ADC);
1456 + if (!(w & (1 << 14))) {
1457 + dev_err(&spi->dev, "invalid ADC reg value: %04x\n", w);
1462 + w = tsc2301_read_reg(tsc, TSC2301_REG_DAC);
1463 + if (!(w & (1 << 15))) {
1464 + dev_err(&spi->dev, "invalid DAC reg value: %04x\n", w);
1469 + /* Stop keypad scanning */
1470 + tsc2301_write_reg(tsc, TSC2301_REG_KEY, 0x4000);
1472 + /* We have to cache this for read-modify-write, since we can't
1473 + * read back BIT15 */
1474 + w = tsc2301_read_reg(tsc, TSC2301_REG_CONFIG2);
1475 + /* By default BIT15 is set */
1477 + tsc->config2_shadow = w;
1479 + r = tsc2301_kp_init(tsc, pdata);
1482 + r = tsc2301_ts_init(tsc, pdata);
1485 + r = tsc2301_mixer_init(tsc, pdata);
1491 + tsc2301_ts_exit(tsc);
1493 + tsc2301_kp_exit(tsc);
1499 +static int __devexit tsc2301_remove(struct spi_device *spi)
1501 + struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
1503 + tsc2301_mixer_exit(tsc);
1504 + tsc2301_ts_exit(tsc);
1505 + tsc2301_kp_exit(tsc);
1506 + if (tsc->reset_gpio >= 0)
1507 + gpio_free(tsc->reset_gpio);
1514 +static int tsc2301_suspend(struct spi_device *spi, pm_message_t mesg)
1516 + struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
1519 + if ((r = tsc2301_mixer_suspend(tsc)) < 0)
1521 + if ((r = tsc2301_kp_suspend(tsc)) < 0)
1523 + if ((r = tsc2301_ts_suspend(tsc)) < 0)
1528 + tsc2301_kp_resume(tsc);
1530 + tsc2301_mixer_resume(tsc);
1534 +static int tsc2301_resume(struct spi_device *spi)
1536 + struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
1538 + tsc2301_ts_resume(tsc);
1539 + tsc2301_kp_resume(tsc);
1540 + tsc2301_mixer_resume(tsc);
1545 +static struct spi_driver tsc2301_driver = {
1547 + .name = "tsc2301",
1548 + .bus = &spi_bus_type,
1549 + .owner = THIS_MODULE,
1552 + .suspend = tsc2301_suspend,
1553 + .resume = tsc2301_resume,
1555 + .probe = tsc2301_probe,
1556 + .remove = __devexit_p(tsc2301_remove),
1559 +static int __init tsc2301_init(void)
1561 + printk("TSC2301 driver initializing\n");
1563 + return spi_register_driver(&tsc2301_driver);
1565 +module_init(tsc2301_init);
1567 +static void __exit tsc2301_exit(void)
1569 + spi_unregister_driver(&tsc2301_driver);
1571 +module_exit(tsc2301_exit);
1573 +MODULE_AUTHOR("Juha Yrjölä <juha.yrjola@nokia.com>");
1574 +MODULE_LICENSE("GPL");
1576 +++ b/drivers/spi/tsc2301-mixer.c
1579 + * ALSA Mixer implementation for TSC2301
1581 + * Copyright (C) 2006 Nokia Corporation.
1583 + * Contact: Jarkko Nikula <jarkko.nikula@nokia.com>
1586 + * Some notes about TSC2301:
1587 + * - PLL will stop when DAC and ADC's are powered down.
1588 + * - Touchscreen will stop working when audio part is powered up and if audio
1589 + * MCLK is stopped. Problem is avoided if audio is powered down before
1591 + * - Audio DAC or audio outputs will activate only after 100 msec from the
1592 + * chip power-up. Reason seems to be VCM since there is no this delay if the
1593 + * chip and VCM (bit AVPD on PD/MISC) were not powered down. The chip will
1594 + * consume about 1 mA if all other audio blocks are powered down except the
1595 + * chip itself and VCM. Full power down consumes only about few uA.
1596 + * - Power-down transition could happen earliest about 100 msec after the chip
1597 + * power-up. Otherwise power-down will fail if there is no that 100 msec
1598 + * on time before it. It's not obvious why is that since chip reports
1599 + * power-up to be completed and also PLL output on GPIO_0 is active in few
1602 + * This program is free software; you can redistribute it and/or
1603 + * modify it under the terms of the GNU General Public License
1604 + * version 2 as published by the Free Software Foundation.
1606 + * This program is distributed in the hope that it will be useful, but
1607 + * WITHOUT ANY WARRANTY; without even the implied warranty of
1608 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1609 + * General Public License for more details.
1611 + * You should have received a copy of the GNU General Public License
1612 + * along with this program; if not, write to the Free Software
1613 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
1618 +#include <linux/module.h>
1619 +#include <linux/device.h>
1620 +#include <linux/errno.h>
1621 +#include <linux/delay.h>
1622 +#include <linux/slab.h>
1623 +#include <linux/spi/spi.h>
1624 +#include <linux/spi/tsc2301.h>
1626 +#include <sound/core.h>
1627 +#include <sound/control.h>
1629 +/* shadow register indexes */
1631 + /* audio control and volume registers */
1636 + /* keyclick control register (not needed here) */
1637 + /* audio power control register */
1639 + /* TSC2301 GPIO control register */
1645 +/* structure for driver private data */
1646 +struct tsc2301_mixer {
1647 + struct tsc2301 *tsc;
1648 + struct mutex mutex;
1650 + /* shadow registers holding TSC2301 audio registers. Used to hold
1651 + * their states during the sleep and also to reduce communication with
1652 + * the chip since get callback functions could get register values
1653 + * directly from these shadow registers without needing to read them
1654 + * from the chip */
1655 + u16 shadow_regs[SHADOW_REG_COUNT];
1657 + /* audio controller driver usage of the ADC and DAC */
1658 + unsigned adc_enabled:1, dac_enabled:1;
1659 + unsigned pll_output:1;
1660 + unsigned mclk_enabled;
1662 + /* latest audio power-up timestamp */
1663 + unsigned long pu_jiffies;
1665 + /* these are used when upper layer(s) are going to power-down TSC2301
1666 + * before 100 msec is passed from power-up */
1667 + struct delayed_work delayed_power_down;
1668 + unsigned delayed_pd_active:1;
1670 + int (* platform_init)(struct device *);
1671 + void (* platform_cleanup)(struct device *);
1673 + struct tsc2301_mixer_gpio *mixer_gpios;
1674 + int n_mixer_gpios;
1677 +#define TSC2301_DAC_DELAY msecs_to_jiffies(100)
1678 +#define TSC2301_MIN_PU_PERIOD msecs_to_jiffies(100)
1680 +#define TSC2301_REG_TO_PVAL(reg) \
1681 + (TSC2301_REG_TO_PAGE(reg) << 6 | TSC2301_REG_TO_ADDR(reg))
1682 +#define TSC2301_PVAL_TO_REG(v) \
1683 + (TSC2301_REG((((v) >> 6) & 3),((v) & 0x1f)))
1685 +#define TSC2301_VOLUME_MASK 0x7f
1686 +#define TSC2301_MIN_ADCVOL 6
1687 +#define TSC2301_MIN_DACVOL 0
1688 +#define TSC2301_MIN_BPVOL 31
1689 +#define TSC2301_MUTE_LEFT_SHIFT 15
1690 +#define TSC2301_VOL_LEFT_SHIFT 8
1691 +#define TSC2301_MUTE_RIGHT_SHIFT 7
1692 +#define TSC2301_VOL_RIGHT_SHIFT 0
1694 +#define TSC2301_INM_MASK 3
1695 +#define TSC2301_INML_SHIFT 12
1696 +#define TSC2301_INMR_SHIFT 10
1698 +#define TSC2301_MICG_MASK 3
1699 +#define TSC2301_MICG_MIN 1 /* values 0 & 1 both mean 0 dB */
1700 +#define TSC2301_MICG_SHIFT 8
1702 +#define TSC2301_REG_AUDCNTL_MCLK(v) (((v) & 3) << 6)
1703 +#define TSC2301_REG_AUDCNTL_I2SFS(v) (((v) & 0xf) << 2)
1704 +#define TSC2301_REG_AUDCNTL_I2SFM(v) (((v) & 3) << 0)
1706 +#define TSC2301_SINGLE(xname, xindex, reg, shadow_index, shift, mask, min) \
1708 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1710 + .index = xindex, \
1711 + .info = snd_tsc2301_info_single, \
1712 + .get = snd_tsc2301_get_single, \
1713 + .put = snd_tsc2301_put_single, \
1714 + .private_value = TSC2301_REG_TO_PVAL(reg) | \
1715 + (shadow_index << 8) | (shift << 16) | (mask << 24) | \
1718 +#define TSC2301_SINGLE_MINVAL(v) (((v) >> 28) & 15)
1719 +#define TSC2301_SINGLE_SHIFT(v) (((v) >> 16) & 15)
1720 +#define TSC2301_SINGLE_MASK(v) (((v) >> 24) & 15)
1722 +#define TSC2301_DOUBLE(xname, xindex, reg, shadow_index, ls, rs, mask, min) \
1724 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1726 + .index = xindex, \
1727 + .info = snd_tsc2301_info_double, \
1728 + .get = snd_tsc2301_get_double, \
1729 + .put = snd_tsc2301_put_double, \
1730 + .private_value = TSC2301_REG_TO_PVAL(reg) | \
1731 + (shadow_index << 8) | (min << 11) | \
1732 + (ls << 16) | (rs << 20) | (mask << 24) \
1734 +#define TSC2301_DOUBLE_MINVAL(v) (((v) >> 11) & 0x1f)
1735 +#define TSC2301_DOUBLE_LEFT_SHIFT(v) (((v) >> 16) & 15)
1736 +#define TSC2301_DOUBLE_RIGHT_SHIFT(v) (((v) >> 20) & 15)
1737 +#define TSC2301_DOUBLE_MASK(v) (((v) >> 24) & TSC2301_VOLUME_MASK)
1739 +#define TSC2301_MUX(xname, xindex, reg, shadow_index, ls, rs, mask) \
1741 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1743 + .index = xindex, \
1744 + .info = snd_tsc2301_info_mux, \
1745 + .get = snd_tsc2301_get_mux, \
1746 + .put = snd_tsc2301_put_mux, \
1747 + .private_value = TSC2301_REG_TO_PVAL(reg) | \
1748 + (shadow_index << 8) | (ls << 16) | (rs << 20) | (mask << 24) \
1750 +#define TSC2301_MUX_LEFT_SHIFT(v) (((v) >> 16) & 15)
1751 +#define TSC2301_MUX_RIGHT_SHIFT(v) (((v) >> 20) & 15)
1752 +#define TSC2301_MUX_MASK(v) (((v) >> 24) & TSC2301_VOLUME_MASK)
1754 +#define TSC2301_BOOL(xname, xindex, reg, shadow_index, shift, invert, state) \
1756 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1758 + .index = xindex, \
1759 + .info = snd_tsc2301_info_bool, \
1760 + .get = snd_tsc2301_get_bool, \
1761 + .put = snd_tsc2301_put_bool, \
1762 + .private_value = TSC2301_REG_TO_PVAL(reg) | \
1763 + (shadow_index << 8) | (shift << 16) | \
1764 + (invert << 24) | (state << 25) \
1766 +#define TSC2301_BOOL_SHIFT(v) (((v) >> 16) & 7)
1767 +#define TSC2301_BOOL_INVERT(v) (((v) >> 24) & 1)
1768 +#define TSC2301_BOOL_STATE(v) (((v) >> 25) & 1)
1770 +#define TSC2301_SHADOW_INDEX(v) (((v) >> 8) & 7)
1773 + * Power-down handler for additional GPIO mixer controls. GPIO state of GPIO
1774 + * controls whose power-down flag is enabled are set to their false/deactivate
1777 + * Must be called tsc->mixer->mutex locked
1779 +static void tsc2301_gpio_power_down(struct tsc2301 *tsc)
1781 + struct tsc2301_mixer *mix = tsc->mixer;
1785 + temp = mix->shadow_regs[GPIO_INDEX];
1786 + for (i = 0; i < mix->n_mixer_gpios; i++) {
1787 + const struct tsc2301_mixer_gpio *mg;
1789 + mg = mix->mixer_gpios + i;
1790 + if (mg->deactivate_on_pd) {
1791 + int gpio = mg->gpio;
1793 + temp &= ~(1 << gpio);
1794 + temp |= mg->inverted << gpio;
1797 + tsc2301_write_reg(tsc, TSC2301_REG_GPIO, temp);
1801 + * Powers down/up audio blocks which are muted or become unused.
1802 + * shadow_index >= 0, changes power state of single audio block
1803 + * shadow_index < 0, changes power state of all blocks
1805 + * Must be called tsc->mixer->mutex locked
1807 +#define TSC2301_MUTE_MASK \
1808 + ((1 << TSC2301_MUTE_LEFT_SHIFT) | (1 << TSC2301_MUTE_RIGHT_SHIFT))
1809 +static void tsc2301_power_ctrl(struct tsc2301 *tsc, int shadow_index,
1812 + struct tsc2301_mixer *mix = tsc->mixer;
1813 + u16 pd_ctrl, pd_ctrl_old, w;
1814 + unsigned long timeout;
1817 + if (mix->delayed_pd_active) {
1818 + mix->delayed_pd_active = 0;
1819 + mix->mclk_enabled--;
1820 + cancel_delayed_work(&mix->delayed_power_down);
1823 + pd_ctrl = pd_ctrl_old = mix->shadow_regs[PD_MISC_INDEX];
1824 + /* power control helper based on used space mixer selections. See
1825 + * actual power control decisions below */
1826 + if (shadow_index < 0 || shadow_index == ADCVOL_INDEX) {
1827 + /* ADC left and right power down control */
1828 + if (mix->shadow_regs[ADCVOL_INDEX] &
1829 + (1 << TSC2301_MUTE_LEFT_SHIFT))
1830 + /* left ADC muted. Power down the left ADC */
1831 + pd_ctrl |= TSC2301_REG_PD_MISC_ADPDL;
1833 + pd_ctrl &= ~TSC2301_REG_PD_MISC_ADPDL;
1834 + if (mix->shadow_regs[ADCVOL_INDEX] &
1835 + (1 << TSC2301_MUTE_LEFT_SHIFT))
1836 + /* right ADC muted. Power down the right ADC */
1837 + pd_ctrl |= TSC2301_REG_PD_MISC_ADPDR;
1839 + pd_ctrl &= ~TSC2301_REG_PD_MISC_ADPDR;
1841 + if (shadow_index < 0 || shadow_index == DACVOL_INDEX) {
1842 + /* DAC power down control */
1843 + if ((mix->shadow_regs[DACVOL_INDEX] &
1844 + TSC2301_MUTE_MASK) == TSC2301_MUTE_MASK)
1845 + /* both DACs muted. Power down the DAC */
1846 + pd_ctrl |= TSC2301_REG_PD_MISC_DAPD;
1848 + pd_ctrl &= ~TSC2301_REG_PD_MISC_DAPD;
1850 + if (shadow_index < 0 || shadow_index == BPVOL_INDEX) {
1851 + /* line bypass power down control */
1852 + if ((mix->shadow_regs[BPVOL_INDEX] &
1853 + TSC2301_MUTE_MASK) == TSC2301_MUTE_MASK)
1854 + /* both line bypasses muted. Power down the bypass
1856 + pd_ctrl |= TSC2301_REG_PD_MISC_ABPD;
1858 + pd_ctrl &= ~TSC2301_REG_PD_MISC_ABPD;
1860 + if (shadow_index < 0 || shadow_index == AUDCNTL_INDEX) {
1861 + /* mic bias power down control */
1862 + if ((mix->shadow_regs[AUDCNTL_INDEX] &
1863 + (3 << TSC2301_INML_SHIFT)) &&
1864 + (mix->shadow_regs[AUDCNTL_INDEX] &
1865 + (3 << TSC2301_INMR_SHIFT)))
1866 + /* both ADC channels use other than mic input. Power
1867 + * down the mic bias output */
1868 + pd_ctrl |= TSC2301_REG_PD_MISC_MIBPD;
1870 + pd_ctrl &= ~TSC2301_REG_PD_MISC_MIBPD;
1873 + /* power control decisions based on codec usage and user space mixer
1874 + * selections detected above */
1875 + pd_ctrl &= ~TSC2301_REG_PD_MISC_APD; /* audio not powered down */
1876 + if (mix->mclk_enabled) {
1877 + if (!mix->adc_enabled) {
1878 + /* ADC not used, power down both ADC's and mic bias
1879 + * output independently of user space mixer
1881 + pd_ctrl |= TSC2301_REG_PD_MISC_ADPDL;
1882 + pd_ctrl |= TSC2301_REG_PD_MISC_ADPDR;
1883 + pd_ctrl |= TSC2301_REG_PD_MISC_MIBPD;
1885 + if (!mix->dac_enabled) {
1886 + /* DAC not used, power down DAC independently of user
1887 + * space mixer selections */
1888 + pd_ctrl |= TSC2301_REG_PD_MISC_DAPD;
1891 + if (mix->pll_output) {
1892 + /* GPIO_0 is configured as PLL output so audio
1893 + * controller is expecting clock from TSC2301. Either
1894 + * ADC or DAC must be active in order to keep PLL on */
1895 + if ((pd_ctrl & TSC2301_REG_PD_MISC_ADPDL) &&
1896 + (pd_ctrl & TSC2301_REG_PD_MISC_ADPDR) &&
1897 + (pd_ctrl & TSC2301_REG_PD_MISC_DAPD)) {
1898 + /* neither ADC or DAC used. Force ADC on in
1899 + * order to keep PLL active */
1900 + pd_ctrl &= ~(TSC2301_REG_PD_MISC_ADPDL |
1901 + TSC2301_REG_PD_MISC_ADPDR);
1905 + /* audio input clock is not enabled so power down DAC and ADC
1906 + * in order to shutdown PLL and to keep touchscreen and keypad
1907 + * parts working. Touchscreen and keypad use audio clock when
1908 + * PLL is on and internal clock otherwise */
1909 + pd_ctrl |= TSC2301_REG_PD_MISC_DAPD |
1910 + TSC2301_REG_PD_MISC_ADPDL |
1911 + TSC2301_REG_PD_MISC_ADPDR;
1914 + if ((pd_ctrl & TSC2301_REG_PD_MISC_ADPDL) &&
1915 + (pd_ctrl & TSC2301_REG_PD_MISC_ADPDR) &&
1916 + (pd_ctrl & TSC2301_REG_PD_MISC_DAPD) &&
1917 + (pd_ctrl & TSC2301_REG_PD_MISC_ABPD)) {
1918 + /* all ADC, DAC and line bypass path unused. Power down the
1919 + * whole audio part of the TSC2301 */
1920 + pd_ctrl |= TSC2301_REG_PD_MISC_APD;
1923 + if (pd_ctrl == pd_ctrl_old)
1926 + /* power down control changed. Update into TSC2301 */
1927 + if ((pd_ctrl ^ pd_ctrl_old) & TSC2301_REG_PD_MISC_APD) {
1928 + /* whole audio power state changed. Update GPIO states */
1929 + if (pd_ctrl & TSC2301_REG_PD_MISC_APD) {
1930 + /* power down GPIO controls before powering down
1932 + tsc2301_gpio_power_down(tsc);
1933 + /* we must really ensure that codec has been on no less
1934 + * than 100 msec before doing power-down */
1935 + timeout = mix->pu_jiffies + TSC2301_MIN_PU_PERIOD -
1937 + if (timeout <= TSC2301_MIN_PU_PERIOD) {
1938 + mix->delayed_pd_active = 1;
1939 + mix->mclk_enabled++;
1940 + schedule_delayed_work(&mix->delayed_power_down,
1945 + /* restore GPIOs after codec is powered up */
1948 + mix->shadow_regs[PD_MISC_INDEX] = pd_ctrl;
1949 + tsc2301_write_reg(tsc, TSC2301_REG_PD_MISC, pd_ctrl);
1951 + mix->pu_jiffies = jiffies;
1952 + if (!poll_pdsts) {
1954 + tsc2301_write_reg(tsc, TSC2301_REG_GPIO,
1955 + mix->shadow_regs[GPIO_INDEX]);
1959 + /* wait until power-up/-down is completed */
1960 + timeout = jiffies + msecs_to_jiffies(100);
1963 + if (time_after(jiffies, timeout)) {
1964 + /* Print a warning only if the I2S clock is not
1965 + * present / out of sync. This can happen during
1966 + * init time, when that clock will be turned on
1967 + * by another driver like in the OMAP EAC with
1968 + * external clock case.
1970 + if (w & TSC2301_REG_PD_MISC_OTSYN) {
1971 + dev_warn(&tsc->spi->dev,
1972 + "I2S clock not in sync or off.\n");
1974 + dev_err(&tsc->spi->dev,
1975 + "power-up/-down timed out "
1976 + "(0x%04x, 0x%04x -> 0x%04x)\n",
1977 + w, pd_ctrl_old, pd_ctrl);
1981 + w = tsc2301_read_reg(tsc, TSC2301_REG_PD_MISC);
1982 + } while (!(w & TSC2301_REG_PD_MISC_PDSTS));
1985 + if (((pd_ctrl ^ pd_ctrl_old) & TSC2301_REG_PD_MISC_DAPD) &&
1986 + !(pd_ctrl & TSC2301_REG_PD_MISC_DAPD)) {
1987 + /* DAC powered up now. Ensure that DAC and audio outputs are
1988 + * activated. They are up 100 msec after the chip power-up
1990 + timeout = mix->pu_jiffies + TSC2301_DAC_DELAY - jiffies;
1991 + if (timeout <= TSC2301_DAC_DELAY)
1992 + schedule_timeout_interruptible(timeout);
1993 + /* FIXME: This is lazy. We restore GPIOs only after activating
1994 + * the DAC. It would be better to do some kind of delayed GPIO
1995 + * restore. That ensures that we restore them also if only ADC
1996 + * path is activated. But this is required only if there is
1997 + * some input amplifier, bias control, etc. and their power
1998 + * state is under TSC GPIO control */
1999 + tsc2301_write_reg(tsc, TSC2301_REG_GPIO,
2000 + mix->shadow_regs[GPIO_INDEX]);
2004 +static int snd_tsc2301_info_single(struct snd_kcontrol *kcontrol,
2005 + struct snd_ctl_elem_info *uinfo)
2007 + int mask = TSC2301_SINGLE_MASK(kcontrol->private_value);
2008 + int minval = TSC2301_SINGLE_MINVAL(kcontrol->private_value);
2010 + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2012 + uinfo->value.integer.min = minval;
2013 + uinfo->value.integer.max = mask;
2018 +static int snd_tsc2301_get_single(struct snd_kcontrol *kcontrol,
2019 + struct snd_ctl_elem_value *ucontrol)
2021 + struct tsc2301 *tsc = kcontrol->private_data;
2022 + unsigned long priv = kcontrol->private_value;
2023 + int mask = TSC2301_SINGLE_MASK(priv);
2024 + int shift = TSC2301_SINGLE_SHIFT(priv);
2025 + int shadow_index = TSC2301_SHADOW_INDEX(priv);
2028 + shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2030 + ucontrol->value.integer.value[0] = (shadow_reg >> shift) & mask;
2035 +static int snd_tsc2301_put_single(struct snd_kcontrol *kcontrol,
2036 + struct snd_ctl_elem_value *ucontrol)
2038 + struct tsc2301 *tsc = kcontrol->private_data;
2039 + unsigned long priv = kcontrol->private_value;
2040 + int mask = TSC2301_SINGLE_MASK(priv);
2041 + int shadow_index = TSC2301_SHADOW_INDEX(priv);
2042 + u16 shadow_reg, shadow_reg_old;
2043 + int shift = TSC2301_SINGLE_SHIFT(priv);
2044 + int reg = TSC2301_PVAL_TO_REG(priv);
2047 + mutex_lock(&tsc->mixer->mutex);
2048 + shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2050 + /* zero bits to be modified */
2051 + shadow_reg &= ~(mask << shift);
2052 + /* modify with new value */
2053 + shadow_reg |= ((ucontrol->value.integer.value[0] & mask) << shift);
2055 + changed = (shadow_reg != shadow_reg_old);
2056 + tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2058 + /* update into TSC2301 if necessary */
2060 + tsc2301_write_reg(tsc, reg, shadow_reg);
2061 + mutex_unlock(&tsc->mixer->mutex);
2066 +static int snd_tsc2301_info_double(struct snd_kcontrol *kcontrol,
2067 + struct snd_ctl_elem_info *uinfo)
2069 + /* mask == 1 : Switch
2070 + * mask > 1 : Max volume */
2071 + int mask = TSC2301_DOUBLE_MASK(kcontrol->private_value);
2072 + int minval = TSC2301_DOUBLE_MINVAL(kcontrol->private_value);
2074 + uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN :
2075 + SNDRV_CTL_ELEM_TYPE_INTEGER;
2077 + uinfo->value.integer.min = minval;
2078 + uinfo->value.integer.max = mask;
2083 +static int snd_tsc2301_get_double(struct snd_kcontrol *kcontrol,
2084 + struct snd_ctl_elem_value *ucontrol)
2086 + struct tsc2301 *tsc = kcontrol->private_data;
2087 + unsigned long priv = kcontrol->private_value;
2088 + /* mask == 1 : Switch
2089 + * mask > 1 : Volume */
2090 + int mask = TSC2301_DOUBLE_MASK(priv);
2091 + int ls = TSC2301_DOUBLE_LEFT_SHIFT(priv);
2092 + int rs = TSC2301_DOUBLE_RIGHT_SHIFT(priv);
2093 + int shadow_index = TSC2301_SHADOW_INDEX(priv);
2096 + shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2098 + /* invert mute bits for the switches */
2100 + shadow_reg = ~shadow_reg;
2102 + ucontrol->value.integer.value[0] = (shadow_reg >> ls) & mask;
2103 + ucontrol->value.integer.value[1] = (shadow_reg >> rs) & mask;
2108 +static int snd_tsc2301_put_double(struct snd_kcontrol *kcontrol,
2109 + struct snd_ctl_elem_value *ucontrol)
2111 + struct tsc2301 *tsc = kcontrol->private_data;
2112 + unsigned long priv = kcontrol->private_value;
2113 + /* mask == 1 : Switch
2114 + * mask > 1 : Volume */
2115 + int mask = TSC2301_DOUBLE_MASK(priv);
2116 + int shadow_index = TSC2301_SHADOW_INDEX(priv);
2117 + u16 shadow_reg, shadow_reg_old;
2118 + int ls = TSC2301_DOUBLE_LEFT_SHIFT(priv);
2119 + int rs = TSC2301_DOUBLE_RIGHT_SHIFT(priv);
2120 + int reg = TSC2301_PVAL_TO_REG(priv);
2123 + mutex_lock(&tsc->mixer->mutex);
2124 + shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2126 + /* zero bits to be modified */
2127 + shadow_reg &= ~((mask << ls) | (mask << rs));
2128 + /* modify with new value */
2130 + /* switch. Invert switch values for the mute bits */
2132 + ((~ucontrol->value.integer.value[0] & mask) << ls) |
2133 + ((~ucontrol->value.integer.value[1] & mask) << rs);
2137 + (ucontrol->value.integer.value[0] << ls) |
2138 + (ucontrol->value.integer.value[1] << rs);
2141 + changed = (shadow_reg != shadow_reg_old);
2142 + tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2144 + /* update into TSC2301 if necessary */
2146 + tsc2301_write_reg(tsc, reg, shadow_reg);
2149 + /* check is need to power down/up audio blocks in case of
2150 + * muted state change */
2151 + tsc2301_power_ctrl(tsc, shadow_index, 0);
2152 + mutex_unlock(&tsc->mixer->mutex);
2157 +static int snd_tsc2301_info_mux(struct snd_kcontrol *kcontrol,
2158 + struct snd_ctl_elem_info *uinfo)
2160 + static char *texts[4] = {"Mic", "Line", "Line swapped", "Line mono"};
2162 + uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2164 + uinfo->value.enumerated.items = 4;
2165 + if (uinfo->value.enumerated.item > 3)
2166 + uinfo->value.enumerated.item = 3;
2167 + strcpy(uinfo->value.enumerated.name,
2168 + texts[uinfo->value.enumerated.item]);
2173 +static int snd_tsc2301_get_mux(struct snd_kcontrol *kcontrol,
2174 + struct snd_ctl_elem_value *ucontrol)
2176 + struct tsc2301 *tsc = kcontrol->private_data;
2177 + unsigned long priv = kcontrol->private_value;
2178 + int mask = TSC2301_MUX_MASK(priv);
2179 + int ls = TSC2301_MUX_LEFT_SHIFT(priv);
2180 + int rs = TSC2301_MUX_RIGHT_SHIFT(priv);
2181 + int shadow_index = TSC2301_SHADOW_INDEX(priv);
2184 + shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2185 + ucontrol->value.enumerated.item[0] = (shadow_reg >> ls) & mask;
2186 + ucontrol->value.enumerated.item[1] = (shadow_reg >> rs) & mask;
2191 +static int snd_tsc2301_put_mux(struct snd_kcontrol *kcontrol,
2192 + struct snd_ctl_elem_value *ucontrol)
2194 + struct tsc2301 *tsc = kcontrol->private_data;
2195 + unsigned long priv = kcontrol->private_value;
2196 + int mask = TSC2301_MUX_MASK(priv);
2197 + int shadow_index = TSC2301_SHADOW_INDEX(priv);
2198 + u16 shadow_reg, shadow_reg_old;
2199 + int ls = TSC2301_MUX_LEFT_SHIFT(priv);
2200 + int rs = TSC2301_MUX_RIGHT_SHIFT(priv);
2201 + int reg = TSC2301_PVAL_TO_REG(priv);
2204 + mutex_lock(&tsc->mixer->mutex);
2205 + shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2207 + /* zero bits to be modified */
2208 + shadow_reg &= ~((mask << ls) | (mask << rs));
2209 + /* modify with new value */
2210 + shadow_reg |= (ucontrol->value.enumerated.item[0] << ls);
2211 + shadow_reg |= (ucontrol->value.enumerated.item[1] << rs);
2213 + changed = (shadow_reg != shadow_reg_old);
2215 + /* update into TSC2301 if necessary */
2217 + tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2218 + tsc2301_write_reg(tsc, reg, shadow_reg);
2221 + /* check is need to power up/down audio blocks in case of ADC input
2223 + tsc2301_power_ctrl(tsc, shadow_index, 0);
2224 + mutex_unlock(&tsc->mixer->mutex);
2229 +static int snd_tsc2301_info_bool(struct snd_kcontrol *kcontrol,
2230 + struct snd_ctl_elem_info *uinfo)
2232 + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2234 + uinfo->value.integer.min = 0;
2235 + uinfo->value.integer.max = 1;
2240 +static int snd_tsc2301_get_bool(struct snd_kcontrol *kcontrol,
2241 + struct snd_ctl_elem_value *ucontrol)
2243 + struct tsc2301 *tsc = kcontrol->private_data;
2244 + unsigned long priv = kcontrol->private_value;
2245 + int shadow_index = TSC2301_SHADOW_INDEX(priv);
2246 + int shift = TSC2301_BOOL_SHIFT(priv);
2247 + int invert = TSC2301_BOOL_INVERT(priv);
2250 + shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2251 + ucontrol->value.integer.value[0] =
2252 + invert ^ ((shadow_reg >> shift) & 1);
2257 +static int snd_tsc2301_put_bool(struct snd_kcontrol *kcontrol,
2258 + struct snd_ctl_elem_value *ucontrol)
2260 + struct tsc2301 *tsc = kcontrol->private_data;
2261 + unsigned long priv = kcontrol->private_value;
2262 + int shadow_index = TSC2301_SHADOW_INDEX(priv);
2263 + int shift = TSC2301_BOOL_SHIFT(priv);
2264 + int invert = TSC2301_BOOL_INVERT(priv);
2265 + int reg = TSC2301_PVAL_TO_REG(priv);
2266 + u16 shadow_reg, shadow_reg_old;
2269 + mutex_lock(&tsc->mixer->mutex);
2270 + shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2272 + /* zero bit to be modified */
2273 + shadow_reg &= ~(1 << shift);
2274 + /* modify with new value */
2276 + (invert ^ (ucontrol->value.integer.value[0] & 1)) << shift;
2278 + changed = (shadow_reg != shadow_reg_old);
2280 + /* update into TSC2301 if necessary */
2282 + tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2283 + if ((shadow_index == GPIO_INDEX) &&
2284 + (tsc->mixer->shadow_regs[PD_MISC_INDEX] &
2285 + TSC2301_REG_PD_MISC_APD)) {
2286 + /* changing GPIO control and audio is powered down.
2287 + * Update GPIO states according to their power-down
2289 + tsc2301_gpio_power_down(tsc);
2291 + tsc2301_write_reg(tsc, reg, shadow_reg);
2293 + mutex_unlock(&tsc->mixer->mutex);
2298 +/* TSC2301 internal mixer controls */
2299 +static struct snd_kcontrol_new snd_tsc2301_controls[] = {
2300 + /* stereo ADC input switches and volumes */
2301 + TSC2301_DOUBLE("Capture Switch", 0,
2302 + TSC2301_REG_ADCVOL, ADCVOL_INDEX,
2303 + TSC2301_MUTE_LEFT_SHIFT, TSC2301_MUTE_RIGHT_SHIFT,
2305 + TSC2301_DOUBLE("Capture Volume", 0,
2306 + TSC2301_REG_ADCVOL, ADCVOL_INDEX,
2307 + TSC2301_VOL_LEFT_SHIFT, TSC2301_VOL_RIGHT_SHIFT,
2308 + TSC2301_VOLUME_MASK, TSC2301_MIN_ADCVOL),
2310 + /* stereo DAC output switches and volumes */
2311 + TSC2301_DOUBLE("PCM Playback Switch", 0,
2312 + TSC2301_REG_DACVOL, DACVOL_INDEX,
2313 + TSC2301_MUTE_LEFT_SHIFT, TSC2301_MUTE_RIGHT_SHIFT,
2315 + TSC2301_DOUBLE("PCM Playback Volume", 0,
2316 + TSC2301_REG_DACVOL, DACVOL_INDEX,
2317 + TSC2301_VOL_LEFT_SHIFT, TSC2301_VOL_RIGHT_SHIFT,
2318 + TSC2301_VOLUME_MASK, TSC2301_MIN_DACVOL),
2320 + /* stereo line input bypass switches and volumes */
2321 + TSC2301_DOUBLE("Line Playback Switch", 0,
2322 + TSC2301_REG_BPVOL, BPVOL_INDEX,
2323 + TSC2301_MUTE_LEFT_SHIFT, TSC2301_MUTE_RIGHT_SHIFT,
2325 + TSC2301_DOUBLE("Line Playback Volume", 0,
2326 + TSC2301_REG_BPVOL, BPVOL_INDEX,
2327 + TSC2301_VOL_LEFT_SHIFT, TSC2301_VOL_RIGHT_SHIFT,
2328 + TSC2301_VOLUME_MASK, TSC2301_MIN_BPVOL),
2330 + /* mono microphone input gain */
2331 + TSC2301_SINGLE("Mic Boost", 0,
2332 + TSC2301_REG_AUDCNTL, AUDCNTL_INDEX,
2333 + TSC2301_MICG_SHIFT,
2334 + TSC2301_MICG_MASK, TSC2301_MICG_MIN),
2336 + /* ADC input sources. Both channels could be selected separately */
2337 + TSC2301_MUX("Capture Source", 0,
2338 + TSC2301_REG_AUDCNTL, AUDCNTL_INDEX,
2339 + TSC2301_INML_SHIFT, TSC2301_INMR_SHIFT,
2340 + TSC2301_INM_MASK),
2343 +/* must be called tsc->mixer->mutex locked */
2344 +static void tsc2301_flush_shadow_regs(struct tsc2301 *tsc)
2346 + int i, page, addr;
2349 + page = TSC2301_REG_TO_PAGE(TSC2301_REG_AUDCNTL);
2350 + addr = TSC2301_REG_TO_ADDR(TSC2301_REG_AUDCNTL);
2352 + for (i = 0; i < 4; i++) {
2353 + temp = tsc->mixer->shadow_regs[i];
2354 + tsc2301_write_reg(tsc, TSC2301_REG(page, addr + i), temp);
2356 + temp = tsc->mixer->shadow_regs[GPIO_INDEX];
2357 + tsc2301_write_reg(tsc, TSC2301_REG_GPIO, temp);
2359 + /* Update power state of all audio blocks depending are they
2360 + * muted or unused. */
2361 + tsc2301_power_ctrl(tsc, -1, 0);
2365 +int tsc2301_mixer_suspend(struct tsc2301 *tsc)
2367 + /* power down entire audio section inside TSC2301 in case the
2368 + * chip is still powered during the system sleep. However this driver
2369 + * doesn't require that chip is powered because registers are restored
2370 + * in function tsc2301_mixer_resume */
2371 + mutex_lock(&tsc->mixer->mutex);
2372 + tsc2301_gpio_power_down(tsc);
2373 + tsc->mixer->shadow_regs[PD_MISC_INDEX] |= TSC2301_REG_PD_MISC_APD;
2374 + tsc2301_write_reg(tsc, TSC2301_REG_PD_MISC,
2375 + tsc->mixer->shadow_regs[PD_MISC_INDEX]);
2376 + mutex_unlock(&tsc->mixer->mutex);
2380 +void tsc2301_mixer_resume(struct tsc2301 *tsc)
2382 + /* power up the TSC2301 audio section and restore registers */
2383 + mutex_lock(&tsc->mixer->mutex);
2384 + tsc->mixer->shadow_regs[PD_MISC_INDEX] &= ~TSC2301_REG_PD_MISC_APD;
2385 + tsc2301_flush_shadow_regs(tsc);
2386 + mutex_unlock(&tsc->mixer->mutex);
2390 +void tsc2301_mixer_enable_mclk(struct device *dev)
2392 + struct tsc2301 *tsc = dev_get_drvdata(dev);
2393 + struct tsc2301_mixer *mix = tsc->mixer;
2395 + mutex_lock(&mix->mutex);
2396 + if (!mix->mclk_enabled++ && tsc->enable_clock != NULL) {
2397 + tsc->enable_clock(dev);
2399 + tsc2301_power_ctrl(tsc, -1, 1);
2400 + mutex_unlock(&mix->mutex);
2403 +void tsc2301_mixer_disable_mclk(struct device *dev)
2405 + struct tsc2301 *tsc = dev_get_drvdata(dev);
2406 + struct tsc2301_mixer *mix = tsc->mixer;
2408 + mutex_lock(&mix->mutex);
2409 + mix->mclk_enabled--;
2410 + tsc2301_power_ctrl(tsc, -1, 1);
2411 + if (!mix->mclk_enabled && tsc->disable_clock != NULL) {
2412 + tsc->disable_clock(dev);
2414 + mutex_unlock(&mix->mutex);
2417 +static void tsc2301_mixer_delayed_power_down(struct work_struct *work)
2419 + struct tsc2301_mixer *mix = container_of(work, struct tsc2301_mixer,
2420 + delayed_power_down.work);
2421 + struct tsc2301 *tsc = mix->tsc;
2423 + mutex_lock(&mix->mutex);
2424 + if (!mix->delayed_pd_active) {
2425 + mutex_unlock(&mix->mutex);
2428 + mix->delayed_pd_active = 0;
2429 + mutex_unlock(&mix->mutex);
2430 + tsc2301_mixer_disable_mclk(&tsc->spi->dev);
2434 + * Allows audio controller driver to notify its usage of ADC and DAC
2436 +void tsc2301_mixer_set_power(struct device *dev, int dac, int adc)
2438 + struct tsc2301 *tsc = dev_get_drvdata(dev);
2440 + mutex_lock(&tsc->mixer->mutex);
2441 + tsc->mixer->adc_enabled = adc;
2442 + tsc->mixer->dac_enabled = dac;
2444 + /* update power state of all audio blocks */
2445 + tsc2301_power_ctrl(tsc, -1, 1);
2446 + mutex_unlock(&tsc->mixer->mutex);
2450 + * Registers TSC2301 ALSA Mixer controls for the given sound card
2452 +int tsc2301_mixer_register_controls(struct device *dev, struct snd_card *card)
2454 + struct tsc2301 *tsc = dev_get_drvdata(dev);
2455 + struct tsc2301_mixer *mix = tsc->mixer;
2458 + /* Register ALSA mixer controls */
2459 + for (i = 0; i < ARRAY_SIZE(snd_tsc2301_controls); i++) {
2460 + err = snd_ctl_add(card,
2461 + snd_ctl_new1(&snd_tsc2301_controls[i], tsc));
2466 + if (!mix->n_mixer_gpios)
2469 + /* Register additional GPIO controls if defined */
2470 + for (i = 0; i < mix->n_mixer_gpios; i++) {
2471 + const struct tsc2301_mixer_gpio *mg = mix->mixer_gpios + i;
2472 + struct snd_kcontrol *ctrlp;
2473 + struct snd_kcontrol_new ctrl =
2474 + TSC2301_BOOL((char *)mg->name, 0,
2475 + TSC2301_REG_GPIO, GPIO_INDEX,
2476 + mg->gpio, mg->inverted, mg->def_enable);
2478 + ctrlp = snd_ctl_new1(&ctrl, tsc);
2479 + err = snd_ctl_add(card, ctrlp);
2487 +int tsc2301_mixer_init(struct tsc2301 *tsc,
2488 + struct tsc2301_platform_data *pdata)
2490 + struct tsc2301_mixer *mix;
2494 + mix = kzalloc(sizeof(*mix), GFP_KERNEL);
2500 + mutex_init(&mix->mutex);
2501 + mix->platform_init = pdata->codec_init;
2502 + mix->platform_cleanup = pdata->codec_cleanup;
2503 + mix->pll_output = pdata->pll_output;
2505 + INIT_DELAYED_WORK(&mix->delayed_power_down,
2506 + tsc2301_mixer_delayed_power_down);
2508 + /* initialize shadow register default values */
2510 + w |= (pdata->mclk_ratio << 6) | (pdata->i2s_sample_rate << 2);
2511 + w |= pdata->i2s_format;
2512 + mix->shadow_regs[AUDCNTL_INDEX] = w;
2513 + mix->shadow_regs[ADCVOL_INDEX] = 0xd7d7;
2514 + mix->shadow_regs[DACVOL_INDEX] = 0xffff;
2515 + mix->shadow_regs[BPVOL_INDEX] = 0xe7e7;
2516 + mix->shadow_regs[PD_MISC_INDEX] = pdata->power_down_blocks;
2518 + /* if extra mixer controls configured, then configure associated
2519 + * GPIOs as output and drive their default state */
2520 + if (pdata->n_mixer_gpios) {
2524 + for (i = 0; i < pdata->n_mixer_gpios; i++) {
2525 + const struct tsc2301_mixer_gpio *mg;
2528 + mg = pdata->mixer_gpios + i;
2530 + w |= (1 << gpio) << 8;
2531 + w |= (mg->inverted ^ mg->def_enable) << gpio;
2533 + mix->shadow_regs[GPIO_INDEX] = w;
2535 + mix->mixer_gpios = kmalloc(sizeof(*pdata->mixer_gpios) *
2536 + pdata->n_mixer_gpios,
2538 + if (mix->mixer_gpios == NULL) {
2542 + memcpy(mix->mixer_gpios, pdata->mixer_gpios,
2543 + sizeof(*pdata->mixer_gpios) * pdata->n_mixer_gpios);
2544 + mix->n_mixer_gpios = pdata->n_mixer_gpios;
2548 + tsc2301_write_pll(tsc, pdata->pll_n, pdata->pll_a, pdata->pll_pdc,
2549 + 0, mix->pll_output ? 0 : 1);
2551 + tsc2301_flush_shadow_regs(tsc);
2553 + if (mix->platform_init != NULL) {
2554 + err = mix->platform_init(&tsc->spi->dev);
2561 + if (mix->mixer_gpios != NULL)
2562 + kfree(mix->mixer_gpios);
2568 +void tsc2301_mixer_exit(struct tsc2301 *tsc)
2570 + struct tsc2301_mixer *mixer = tsc->mixer;
2572 + if (mixer->platform_cleanup != NULL)
2573 + mixer->platform_cleanup(&tsc->spi->dev);
2575 + if (mixer->mixer_gpios != NULL)
2576 + kfree(mixer->mixer_gpios);
2579 +MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
2580 +MODULE_LICENSE("GPL");
2582 +++ b/include/linux/spi/tsc2301.h
2584 +#ifndef _LINUX_SPI_TSC2301_H
2585 +#define _LINUX_SPI_TSC2301_H
2587 +#include <linux/types.h>
2588 +#include <linux/timer.h>
2590 +struct tsc2301_platform_data {
2596 + s16 keymap[16]; /* Set a key to a negative value if not used */
2597 + unsigned kp_rep:1; /* Enable keypad repeating */
2598 + char *keyb_name; /* Keyboard device name */
2604 + u16 ts_x_plate_ohm;
2605 + u32 ts_stab_time; /* voltage settling time */
2606 + u8 ts_hw_avg; /* HW assiseted averaging. Can be
2607 + 0, 4, 8, 16 samples per reading */
2608 + u32 ts_max_pressure;/* Samples with bigger pressure value will
2609 + be ignored, since the corresponding X, Y
2610 + values are unreliable */
2611 + u32 ts_touch_pressure; /* Pressure limit until we report a
2612 + touch event. After that we switch
2613 + to ts_max_pressure. */
2614 + u32 ts_pressure_fudge;
2623 + unsigned pll_pdc:4;
2626 + unsigned pll_output:1; /* Output PLL on GPIO_0 */
2628 + unsigned mclk_ratio:2;
2629 + unsigned i2s_sample_rate:4;
2630 + unsigned i2s_format:2;
2631 + /* Mask for audio blocks to be powered down */
2632 + u16 power_down_blocks;
2634 + /* Called after codec has been initialized, can be NULL */
2635 + int (* codec_init)(struct device *tsc2301_dev);
2636 + /* Called when codec is being removed, can be NULL */
2637 + void (* codec_cleanup)(struct device *tsc2301_dev);
2638 + int (*enable_clock)(struct device *dev);
2639 + void (*disable_clock)(struct device *dev);
2641 + const struct tsc2301_mixer_gpio {
2644 + unsigned inverted:1;
2645 + unsigned def_enable:1; /* enable by default */
2646 + unsigned deactivate_on_pd:1; /* power-down flag */
2648 + int n_mixer_gpios;
2653 +struct tsc2301_mixer;
2656 + struct spi_device *spi;
2659 + u16 config2_shadow;
2661 + struct tsc2301_kp *kp;
2662 + struct tsc2301_ts *ts;
2663 + struct tsc2301_mixer *mixer;
2665 + int (*enable_clock)(struct device *dev);
2666 + void (*disable_clock)(struct device *dev);
2670 +#define TSC2301_HZ 33000000
2672 +#define TSC2301_REG(page, addr) (((page) << 11) | ((addr) << 5))
2673 +#define TSC2301_REG_TO_PAGE(reg) (((reg) >> 11) & 0x03)
2674 +#define TSC2301_REG_TO_ADDR(reg) (((reg) >> 5) & 0x1f)
2676 +#define TSC2301_REG_X TSC2301_REG(0, 0)
2677 +#define TSC2301_REG_Y TSC2301_REG(0, 1)
2678 +#define TSC2301_REG_Z1 TSC2301_REG(0, 2)
2679 +#define TSC2301_REG_Z2 TSC2301_REG(0, 3)
2680 +#define TSC2301_REG_KPDATA TSC2301_REG(0, 4)
2681 +#define TSC2301_REG_ADC TSC2301_REG(1, 0)
2682 +#define TSC2301_REG_KEY TSC2301_REG(1, 1)
2683 +#define TSC2301_REG_DAC TSC2301_REG(1, 2)
2684 +#define TSC2301_REG_REF TSC2301_REG(1, 3)
2685 +#define TSC2301_REG_RESET TSC2301_REG(1, 4)
2686 +#define TSC2301_REG_CONFIG TSC2301_REG(1, 5)
2687 +#define TSC2301_REG_CONFIG2 TSC2301_REG(1, 6)
2688 +#define TSC2301_REG_KPMASK TSC2301_REG(1, 16)
2689 +#define TSC2301_REG_AUDCNTL TSC2301_REG(2, 0)
2690 +#define TSC2301_REG_ADCVOL TSC2301_REG(2, 1)
2691 +#define TSC2301_REG_DACVOL TSC2301_REG(2, 2)
2692 +#define TSC2301_REG_BPVOL TSC2301_REG(2, 3)
2693 +#define TSC2301_REG_KEYCTL TSC2301_REG(2, 4)
2694 +#define TSC2301_REG_PD_MISC TSC2301_REG(2, 5)
2695 +#define TSC2301_REG_GPIO TSC2301_REG(2, 6)
2696 +#define TSC2301_REG_ADCLKCFG TSC2301_REG(2, 27)
2698 +#define TSC2301_REG_PD_MISC_APD (1 << 15)
2699 +#define TSC2301_REG_PD_MISC_AVPD (1 << 14)
2700 +#define TSC2301_REG_PD_MISC_ABPD (1 << 13)
2701 +#define TSC2301_REG_PD_MISC_HAPD (1 << 12)
2702 +#define TSC2301_REG_PD_MISC_MOPD (1 << 11)
2703 +#define TSC2301_REG_PD_MISC_DAPD (1 << 10)
2704 +#define TSC2301_REG_PD_MISC_ADPDL (1 << 9)
2705 +#define TSC2301_REG_PD_MISC_ADPDR (1 << 8)
2706 +#define TSC2301_REG_PD_MISC_PDSTS (1 << 7)
2707 +#define TSC2301_REG_PD_MISC_MIBPD (1 << 6)
2708 +#define TSC2301_REG_PD_MISC_OTSYN (1 << 2)
2710 +/* I2S sample rate */
2711 +#define TSC2301_I2S_SR_48000 0x00
2712 +#define TSC2301_I2S_SR_44100 0x01
2713 +#define TSC2301_I2S_SR_32000 0x02
2714 +#define TSC2301_I2S_SR_24000 0x03
2715 +#define TSC2301_I2S_SR_22050 0x04
2716 +#define TSC2301_I2S_SR_16000 0x05
2717 +#define TSC2301_I2S_SR_12000 0x06
2718 +#define TSC2301_I2S_SR_11050 0x07
2719 +#define TSC2301_I2S_SR_8000 0x08
2721 +/* 16-bit, MSB-first. DAC Right-Justified, ADC Left-Justified */
2722 +#define TSC2301_I2S_FORMAT0 0x00
2723 +/* 20-bit, MSB-first. DAC Right-Justified, ADC Left-Justified */
2724 +#define TSC2301_I2S_FORMAT1 0x01
2725 +/* 20-bit, MSB-first. DAC Left-Justified, ADC Left-Justified */
2726 +#define TSC2301_I2S_FORMAT2 0x02
2727 +/* 20-bit, MSB-first */
2728 +#define TSC2301_I2S_FORMAT3 0x03
2730 +/* Master Clock Ratio */
2731 +#define TSC2301_MCLK_256xFS 0x00 /* default */
2732 +#define TSC2301_MCLK_384xFS 0x01
2733 +#define TSC2301_MCLK_512xFS 0x02
2736 +extern u16 tsc2301_read_reg(struct tsc2301 *tsc, int reg);
2737 +extern void tsc2301_write_reg(struct tsc2301 *tsc, int reg, u16 val);
2738 +extern void tsc2301_write_kbc(struct tsc2301 *tsc, int val);
2739 +extern void tsc2301_write_pll(struct tsc2301 *tsc, int pll_n, int pll_a,
2740 + int pll_pdc, int pct_e, int pll_o);
2741 +extern void tsc2301_read_buf(struct tsc2301 *tsc, int reg, u16 *buf, int len);
2743 +#define TSC2301_DECL_MOD(module) \
2744 +extern int tsc2301_##module##_init(struct tsc2301 *tsc, \
2745 + struct tsc2301_platform_data *pdata); \
2746 +extern void tsc2301_##module##_exit(struct tsc2301 *tsc); \
2747 +extern int tsc2301_##module##_suspend(struct tsc2301 *tsc); \
2748 +extern void tsc2301_##module##_resume(struct tsc2301 *tsc);
2750 +#define TSC2301_DECL_EMPTY_MOD(module) \
2751 +static inline int tsc2301_##module##_init(struct tsc2301 *tsc, \
2752 + struct tsc2301_platform_data *pdata) \
2756 +static inline void tsc2301_##module##_exit(struct tsc2301 *tsc) {} \
2757 +static inline int tsc2301_##module##_suspend(struct tsc2301 *tsc) \
2761 +static inline void tsc2301_##module##_resume(struct tsc2301 *tsc) {}
2763 +#ifdef CONFIG_KEYBOARD_TSC2301
2764 +TSC2301_DECL_MOD(kp)
2765 +void tsc2301_kp_restart(struct tsc2301 *tsc);
2767 +TSC2301_DECL_EMPTY_MOD(kp)
2768 +static inline void tsc2301_kp_restart(struct tsc2301 *tsc) {}
2771 +#ifdef CONFIG_TOUCHSCREEN_TSC2301
2772 +TSC2301_DECL_MOD(ts)
2774 +TSC2301_DECL_EMPTY_MOD(ts)
2777 +#ifdef CONFIG_SPI_TSC2301_AUDIO
2778 +TSC2301_DECL_MOD(mixer)
2779 +extern void tsc2301_mixer_set_power(struct device *tsc_dev, int dac, int adc);
2782 +extern int tsc2301_mixer_register_controls(struct device *tsc_dev,
2783 + struct snd_card *card);
2785 +TSC2301_DECL_EMPTY_MOD(mixer)
2788 +extern void tsc2301_mixer_enable_mclk(struct device *tsc_dev);
2789 +extern void tsc2301_mixer_disable_mclk(struct device *tsc_dev);