Root/target/linux/omap24xx/patches-3.1/315-n800-touchscreen-and-keypad-drivers.patch

1From 63e56392b9024aceb610d7b4e1979e2d2cebd217 Mon Sep 17 00:00:00 2001
2From: Marat Radchenko <marat@slonopotamus.org>
3Date: Tue, 18 Oct 2011 21:48:23 +0400
4Subject: [PATCH 1/2] TSC2301 driver (touchscreen and keypad)
5
6This patch adds support for TSC2301 touchscreen/keypad/audio chip found on Nokia N800.
7Touchscreen and keypad are fully functional, audio part only provides power management.
8---
9 drivers/input/keyboard/Kconfig | 6 +
10 drivers/input/keyboard/Makefile | 1 +
11 drivers/input/keyboard/tsc2301_kp.c | 475 +++++++++++++++
12 drivers/input/touchscreen/Kconfig | 6 +
13 drivers/input/touchscreen/Makefile | 1 +
14 drivers/input/touchscreen/tsc2301_ts.c | 676 +++++++++++++++++++++
15 drivers/spi/Kconfig | 22 +
16 drivers/spi/Makefile | 3 +
17 drivers/spi/tsc2301-core.c | 297 ++++++++++
18 drivers/spi/tsc2301-mixer.c | 1003 ++++++++++++++++++++++++++++++++
19 include/linux/spi/tsc2301.h | 208 +++++++
20 11 files changed, 2698 insertions(+), 0 deletions(-)
21 create mode 100644 drivers/input/keyboard/tsc2301_kp.c
22 create mode 100644 drivers/input/touchscreen/tsc2301_ts.c
23 create mode 100644 drivers/spi/tsc2301-core.c
24 create mode 100644 drivers/spi/tsc2301-mixer.c
25 create mode 100644 include/linux/spi/tsc2301.h
26
27--- 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.
32 
33+config KEYBOARD_TSC2301
34+ tristate "TSC2301 keypad support"
35+ depends on SPI_TSC2301
36+ help
37+ Say Y here for if you are using the keypad features of TSC2301.
38+
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
52--- /dev/null
53+++ b/drivers/input/keyboard/tsc2301_kp.c
54@@ -0,0 +1,475 @@
55+/*
56+ * TSC2301 keypad driver
57+ *
58+ * Copyright (C) 2005-2006 Nokia Corporation
59+ *
60+ * Written by Jarkko Oikarinen
61+ * Rewritten by Juha Yrjola <juha.yrjola@nokia.com>
62+ *
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.
67+ *
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.
72+ *
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
76+ *
77+ */
78+
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>
86+
87+#include <linux/spi/tsc2301.h>
88+
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
99+
100+#define TSC2301_DEBOUNCE_TIME TSC2301_DEBOUNCE_TIME_20MS
101+
102+#define TSC2301_RELEASE_TIMEOUT 50
103+
104+struct tsc2301_kp {
105+ struct input_dev *idev;
106+ char phys[32];
107+ spinlock_t lock;
108+ struct mutex mutex;
109+ struct timer_list timer;
110+ u16 keys_pressed;
111+ unsigned pending:1;
112+ unsigned user_disabled:1;
113+ unsigned disable_depth;
114+
115+ struct spi_transfer read_xfer[4];
116+ struct spi_message read_msg;
117+
118+ u16 data;
119+ u16 mask;
120+
121+ int irq;
122+ s16 keymap[16];
123+};
124+
125+static inline int tsc2301_kp_disabled(struct tsc2301 *tsc)
126+{
127+ return tsc->kp->disable_depth != 0;
128+}
129+
130+static void tsc2301_kp_send_key_events(struct tsc2301 *tsc,
131+ u16 prev_state,
132+ u16 new_state)
133+{
134+ struct tsc2301_kp *kp = tsc->kp;
135+ u16 common, released, pressed;
136+ int i;
137+
138+ common = prev_state & new_state;
139+ released = common ^ prev_state;
140+ pressed = common ^ new_state;
141+ if (!released && !pressed)
142+ return;
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);
147+ }
148+ released >>= 1;
149+ if (pressed & 1) {
150+ dev_dbg(&tsc->spi->dev, "key %d pressed\n", i);
151+ input_report_key(kp->idev, kp->keymap[i], 1);
152+ }
153+ pressed >>= 1;
154+ }
155+ input_sync(kp->idev);
156+}
157+
158+static inline void _filter_out(struct tsc2301 *tsc, u16 prev_state,
159+ u16 *new_state, int row1, int row2, u8 rect_pat)
160+{
161+ u16 mask;
162+
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);
167+}
168+
169+static void tsc2301_filter_ghost_keys(struct tsc2301 *tsc, u16 prev_state,
170+ u16 *new_state)
171+{
172+ int row1, row2;
173+ u16 key_map;
174+ u16 row1_map;
175+ static const u8 rect_pat[] = {
176+ 0x3, 0x5, 0x9, 0x6, 0xa, 0xc, 0,
177+ };
178+
179+ key_map = *new_state;
180+ for (row1 = 0; row1 < 4; row1++) {
181+ row1_map = (key_map >> (row1 * 4)) & 0xf;
182+ if (!row1_map)
183+ continue;
184+ for (row2 = row1 + 1; row2 < 4; row2++) {
185+ u16 rect_map = (key_map >> (row2 * 4)) & 0xf;
186+ const u8 *rp;
187+
188+ rect_map &= row1_map;
189+ if (!rect_map)
190+ continue;
191+ for (rp = rect_pat; *rp; rp++)
192+ if ((rect_map & *rp) == *rp)
193+ _filter_out(tsc, prev_state, new_state,
194+ row1, row2, *rp);
195+ }
196+ }
197+}
198+
199+static void tsc2301_kp_timer(unsigned long arg)
200+{
201+ struct tsc2301 *tsc = (void *) arg;
202+ struct tsc2301_kp *kp = tsc->kp;
203+ unsigned long flags;
204+
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);
209+}
210+
211+static void tsc2301_kp_rx(void *arg)
212+{
213+ struct tsc2301 *tsc = arg;
214+ struct tsc2301_kp *kp = tsc->kp;
215+ unsigned long flags;
216+ u16 kp_data;
217+
218+ kp_data = kp->data;
219+ dev_dbg(&tsc->spi->dev, "KP data %04x\n", kp_data);
220+
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;
225+ kp->pending = 0;
226+ spin_unlock_irqrestore(&kp->lock, flags);
227+}
228+
229+static irqreturn_t tsc2301_kp_irq_handler(int irq, void *dev_id)
230+{
231+ struct tsc2301 *tsc = dev_id;
232+ struct tsc2301_kp *kp = tsc->kp;
233+ unsigned long flags;
234+ int r;
235+
236+ spin_lock_irqsave(&kp->lock, flags);
237+ if (tsc2301_kp_disabled(tsc)) {
238+ spin_unlock_irqrestore(&kp->lock, flags);
239+ return IRQ_HANDLED;
240+ }
241+ kp->pending = 1;
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);
246+ if (r)
247+ dev_err(&tsc->spi->dev, "kp: spi_async() failed");
248+ return IRQ_HANDLED;
249+}
250+
251+static void tsc2301_kp_start_scan(struct tsc2301 *tsc)
252+{
253+ tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, tsc->kp->mask);
254+ tsc2301_write_reg(tsc, TSC2301_REG_KEY, TSC2301_DEBOUNCE_TIME);
255+}
256+
257+static void tsc2301_kp_stop_scan(struct tsc2301 *tsc)
258+{
259+ tsc2301_write_reg(tsc, TSC2301_REG_KEY, 1 << 14);
260+}
261+
262+/* Must be called with the mutex held */
263+static void tsc2301_kp_enable(struct tsc2301 *tsc)
264+{
265+ struct tsc2301_kp *kp = tsc->kp;
266+ unsigned long flags;
267+
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);
272+ return;
273+ }
274+ spin_unlock_irqrestore(&kp->lock, flags);
275+
276+ irq_set_irq_type(kp->irq, IRQ_TYPE_EDGE_FALLING);
277+ tsc2301_kp_start_scan(tsc);
278+ enable_irq(kp->irq);
279+}
280+
281+/* Must be called with the mutex held */
282+static int tsc2301_kp_disable(struct tsc2301 *tsc, int release_keys)
283+{
284+ struct tsc2301_kp *kp = tsc->kp;
285+ unsigned long flags;
286+
287+ spin_lock_irqsave(&kp->lock, flags);
288+ if (kp->disable_depth++ != 0) {
289+ spin_unlock_irqrestore(&kp->lock, flags);
290+ goto out;
291+ }
292+ disable_irq_nosync(kp->irq);
293+ irq_set_irq_type(kp->irq, IRQ_TYPE_NONE);
294+ spin_unlock_irqrestore(&kp->lock, flags);
295+
296+ while (kp->pending) {
297+ msleep(1);
298+ }
299+
300+ tsc2301_kp_stop_scan(tsc);
301+out:
302+ if (!release_keys)
303+ del_timer(&kp->timer); /* let timeout release keys */
304+
305+ return 0;
306+}
307+
308+/* The following workaround is needed for a HW bug triggered by the
309+ * following:
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
315+ *
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.
320+ */
321+void tsc2301_kp_restart(struct tsc2301 *tsc)
322+{
323+ if (!tsc2301_kp_disabled(tsc)) {
324+ tsc2301_kp_start_scan(tsc);
325+ }
326+}
327+
328+static ssize_t tsc2301_kp_disable_show(struct device *dev,
329+ struct device_attribute *attr, char *buf)
330+{
331+ struct tsc2301 *tsc = dev_get_drvdata(dev);
332+
333+ return sprintf(buf, "%u\n", tsc2301_kp_disabled(tsc) ? 1 : 0);
334+}
335+
336+static ssize_t tsc2301_kp_disable_store(struct device *dev,
337+ struct device_attribute *attr,
338+ const char *buf, size_t count)
339+{
340+ struct tsc2301 *tsc = dev_get_drvdata(dev);
341+ struct tsc2301_kp *kp = tsc->kp;
342+ char *endp;
343+ int i;
344+
345+ i = simple_strtoul(buf, &endp, 10);
346+ i = i ? 1 : 0;
347+
348+ mutex_lock(&kp->mutex);
349+ if (i == kp->user_disabled) {
350+ mutex_unlock(&kp->mutex);
351+ return count;
352+ }
353+ kp->user_disabled = i;
354+
355+ if (i)
356+ tsc2301_kp_disable(tsc, 1);
357+ else
358+ tsc2301_kp_enable(tsc);
359+ mutex_unlock(&kp->mutex);
360+
361+ return count;
362+}
363+
364+static DEVICE_ATTR(disable_kp, 0664, tsc2301_kp_disable_show,
365+ tsc2301_kp_disable_store);
366+
367+static const u16 tsc2301_kp_read_data = 0x8000 | TSC2301_REG_KPDATA;
368+
369+static void tsc2301_kp_setup_spi_xfer(struct tsc2301 *tsc)
370+{
371+ struct tsc2301_kp *kp = tsc->kp;
372+ struct spi_message *m = &kp->read_msg;
373+ struct spi_transfer *x = &kp->read_xfer[0];
374+
375+ spi_message_init(&kp->read_msg);
376+
377+ x->tx_buf = &tsc2301_kp_read_data;
378+ x->len = 2;
379+ spi_message_add_tail(x, m);
380+ x++;
381+
382+ x->rx_buf = &kp->data;
383+ x->len = 2;
384+ spi_message_add_tail(x, m);
385+
386+ m->complete = tsc2301_kp_rx;
387+ m->context = tsc;
388+}
389+
390+#ifdef CONFIG_PM
391+int tsc2301_kp_suspend(struct tsc2301 *tsc)
392+{
393+ struct tsc2301_kp *kp = tsc->kp;
394+
395+ mutex_lock(&kp->mutex);
396+ tsc2301_kp_disable(tsc, 1);
397+ mutex_unlock(&kp->mutex);
398+ return 0;
399+}
400+
401+void tsc2301_kp_resume(struct tsc2301 *tsc)
402+{
403+ struct tsc2301_kp *kp = tsc->kp;
404+
405+ mutex_lock(&kp->mutex);
406+ tsc2301_kp_enable(tsc);
407+ mutex_unlock(&kp->mutex);
408+}
409+#endif
410+
411+int __devinit tsc2301_kp_init(struct tsc2301 *tsc,
412+ struct tsc2301_platform_data *pdata)
413+{
414+ struct input_dev *idev;
415+ struct tsc2301_kp *kp;
416+ int r, i;
417+ u16 mask;
418+
419+ if (pdata->keyb_int < 0) {
420+ dev_err(&tsc->spi->dev, "need kbirq");
421+ return -EINVAL;
422+ }
423+
424+ kp = kzalloc(sizeof(*kp), GFP_KERNEL);
425+ if (kp == NULL)
426+ return -ENOMEM;
427+ tsc->kp = kp;
428+
429+ kp->irq = pdata->keyb_int;
430+ spin_lock_init(&kp->lock);
431+ mutex_init(&kp->mutex);
432+
433+ init_timer(&kp->timer);
434+ kp->timer.data = (unsigned long) tsc;
435+ kp->timer.function = tsc2301_kp_timer;
436+
437+ idev = input_allocate_device();
438+ if (idev == NULL) {
439+ r = -ENOMEM;
440+ goto err1;
441+ }
442+ if (pdata->keyb_name)
443+ idev->name = pdata->keyb_name;
444+ else
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;
448+
449+ mask = 0;
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];
455+ } else {
456+ kp->keymap[i] = -1;
457+ mask |= 1 << i;
458+ }
459+ }
460+
461+ if (pdata->kp_rep)
462+ set_bit(EV_REP, idev->evbit);
463+
464+ kp->idev = idev;
465+
466+ tsc2301_kp_setup_spi_xfer(tsc);
467+
468+ r = device_create_file(&tsc->spi->dev, &dev_attr_disable_kp);
469+ if (r < 0)
470+ goto err2;
471+
472+ tsc2301_kp_start_scan(tsc);
473+
474+ /* IRQ mode 0 is faulty, it can cause the KBIRQ to get stuck.
475+ * Mode 2 deasserts the IRQ at:
476+ * - HW or SW reset
477+ * - Setting SCS flag in REG_KEY register
478+ * - Releasing all keys
479+ * - Reading the REG_KPDATA
480+ */
481+ tsc2301_write_kbc(tsc, 2);
482+
483+ tsc2301_write_reg(tsc, TSC2301_REG_KPMASK, mask);
484+ kp->mask = mask;
485+
486+ irq_set_irq_type(kp->irq, IRQ_TYPE_EDGE_FALLING);
487+
488+ r = request_irq(kp->irq, tsc2301_kp_irq_handler, IRQF_SAMPLE_RANDOM,
489+ "tsc2301-kp", tsc);
490+ if (r < 0) {
491+ dev_err(&tsc->spi->dev, "unable to get kbirq IRQ");
492+ goto err3;
493+ }
494+ irq_set_irq_wake(kp->irq, 1);
495+
496+ /* We need to read the register once..? */
497+ tsc2301_read_reg(tsc, TSC2301_REG_KPDATA);
498+
499+ r = input_register_device(idev);
500+ if (r < 0) {
501+ dev_err(&tsc->spi->dev, "can't register keypad device\n");
502+ goto err4;
503+ }
504+
505+ return 0;
506+
507+err4:
508+ free_irq(kp->irq, tsc);
509+err3:
510+ tsc2301_kp_stop_scan(tsc);
511+ device_remove_file(&tsc->spi->dev, &dev_attr_disable_kp);
512+err2:
513+ input_free_device(kp->idev);
514+err1:
515+ kfree(kp);
516+ return r;
517+}
518+
519+void __devexit tsc2301_kp_exit(struct tsc2301 *tsc)
520+{
521+ struct tsc2301_kp *kp = tsc->kp;
522+
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);
527+
528+ kfree(kp);
529+}
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.
535 
536+config TOUCHSCREEN_TSC2301
537+ tristate "TSC2301 touchscreen support"
538+ depends on SPI_TSC2301
539+ help
540+ Say Y here for if you are using the touchscreen features of TSC2301.
541+
542 config TOUCHSCREEN_W90X900
543     tristate "W90P910 touchscreen driver"
544     depends on HAVE_CLK
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
555--- /dev/null
556+++ b/drivers/input/touchscreen/tsc2301_ts.c
557@@ -0,0 +1,676 @@
558+/*
559+ * TSC2301 touchscreen driver
560+ *
561+ * Copyright (C) 2005-2008 Nokia Corporation
562+ *
563+ * Written by Jarkko Oikarinen, Imre Deak and Juha Yrjola
564+ *
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.
569+ *
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.
574+ *
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
578+ *
579+ */
580+
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>
587+
588+#include <linux/spi/tsc2301.h>
589+
590+/**
591+ * The touchscreen interface operates as follows:
592+ *
593+ * Initialize:
594+ * Request access to GPIO103 (DAV)
595+ * tsc2301_ts_irq_handler will trigger when DAV line goes down
596+ *
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.
606+ */
607+
608+
609+#define TSC2301_TOUCHSCREEN_PRODUCT_ID 0x0052
610+#define TSC2301_TOUCHSCREEN_PRODUCT_VERSION 0x0001
611+
612+#define TSC2301_TS_PENUP_TIME 20
613+
614+#define TSC2301_ADCREG_CONVERSION_CTRL_BY_TSC2301 0x8000
615+#define TSC2301_ADCREG_CONVERSION_CTRL_BY_HOST 0x0000
616+
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
628+
629+#define TSC2301_ADCREG_RESOLUTION_8BIT 0x0100
630+#define TSC2301_ADCREG_RESOLUTION_10BIT 0x0200
631+#define TSC2301_ADCREG_RESOLUTION_12BIT 0x0300
632+
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
637+
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
642+
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
651+
652+#define TSC2301_ADCREG_STOP_CONVERSION 0x4000
653+
654+#define MAX_12BIT ((1 << 12) - 1)
655+
656+#define TS_RECT_SIZE 8
657+#define TSF_MIN_Z1 100
658+#define TSF_MAX_Z2 4000
659+
660+#define TSF_SAMPLES 4
661+
662+struct ts_filter {
663+ int sample_cnt;
664+
665+ int avg_x;
666+ int avg_y;
667+ int avg_z1;
668+ int avg_z2;
669+};
670+
671+struct ts_coords {
672+ u16 x;
673+ u16 y;
674+ u16 z1;
675+ u16 z2;
676+};
677+
678+struct tsc2301_ts {
679+ struct input_dev *idev;
680+ char phys[32];
681+ struct timer_list penup_timer;
682+ struct mutex mutex;
683+
684+ struct spi_transfer read_xfer[2];
685+ struct spi_message read_msg;
686+ struct ts_coords *coords;
687+
688+ struct ts_filter filter;
689+
690+ int hw_avg_max;
691+ u16 x;
692+ u16 y;
693+ u16 p;
694+
695+ u16 x_plate_ohm;
696+ int stab_time;
697+ int max_pressure;
698+ int touch_pressure;
699+
700+ u8 event_sent;
701+ u8 pen_down;
702+ u8 disabled;
703+ u8 disable_depth;
704+
705+ int hw_flags;
706+ int irq;
707+};
708+
709+
710+static const u16 tsc2301_ts_read_data = 0x8000 | TSC2301_REG_X;
711+
712+static int tsc2301_ts_check_config(struct tsc2301_ts *ts, int *hw_flags)
713+{
714+ int flags;
715+
716+ flags = 0;
717+ switch (ts->hw_avg_max) {
718+ case 0:
719+ flags |= TSC2301_ADCREG_AVERAGING_NONE;
720+ break;
721+ case 4:
722+ flags |= TSC2301_ADCREG_AVERAGING_4AVG;
723+ break;
724+ case 8:
725+ flags |= TSC2301_ADCREG_AVERAGING_8AVG;
726+ break;
727+ case 16:
728+ flags |= TSC2301_ADCREG_AVERAGING_16AVG;
729+ break;
730+ default:
731+ return -EINVAL;
732+ }
733+
734+ switch (ts->stab_time) {
735+ case 0:
736+ flags |= TSC2301_ADCREG_VOLTAGE_STAB_0US;
737+ break;
738+ case 100:
739+ flags |= TSC2301_ADCREG_VOLTAGE_STAB_100US;
740+ break;
741+ case 500:
742+ flags |= TSC2301_ADCREG_VOLTAGE_STAB_500US;
743+ break;
744+ case 1000:
745+ flags |= TSC2301_ADCREG_VOLTAGE_STAB_1MS;
746+ break;
747+ case 5000:
748+ flags |= TSC2301_ADCREG_VOLTAGE_STAB_5MS;
749+ break;
750+ case 10000:
751+ flags |= TSC2301_ADCREG_VOLTAGE_STAB_10MS;
752+ break;
753+ case 50000:
754+ flags |= TSC2301_ADCREG_VOLTAGE_STAB_50MS;
755+ break;
756+ case 100000:
757+ flags |= TSC2301_ADCREG_VOLTAGE_STAB_100MS;
758+ break;
759+ default:
760+ return -EINVAL;
761+ }
762+
763+ *hw_flags = flags;
764+ return 0;
765+}
766+
767+/*
768+ * This odd three-time initialization is to work around a bug in TSC2301.
769+ * See TSC2301 errata for details.
770+ */
771+static int tsc2301_ts_configure(struct tsc2301 *tsc, int flags)
772+{
773+ struct spi_transfer xfer[5];
774+ struct spi_transfer *x;
775+ struct spi_message m;
776+ int i;
777+ u16 val1, val2, val3;
778+ u16 data[10];
779+
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;
787+
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;
794+
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 |
800+ flags;
801+
802+ /* Now we prepare the command for transferring */
803+ data[0] = TSC2301_REG_ADC;
804+ data[1] = val1;
805+ data[2] = TSC2301_REG_ADC;
806+ data[3] = val2;
807+ data[4] = TSC2301_REG_ADC;
808+ data[5] = val3;
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 */
813+
814+ spi_message_init(&m);
815+ m.spi = tsc->spi;
816+
817+ memset(xfer, 0, sizeof(xfer));
818+ x = &xfer[0];
819+
820+ for (i = 0; i < 10; i += 2) {
821+ x->tx_buf = &data[i];
822+ x->len = 4;
823+ if (i != 8)
824+ x->cs_change = 1;
825+ spi_message_add_tail(x, &m);
826+ x++;
827+ }
828+ spi_sync(m.spi, &m);
829+
830+ return 0;
831+}
832+
833+static void tsc2301_ts_start_scan(struct tsc2301 *tsc)
834+{
835+ tsc2301_ts_configure(tsc, tsc->ts->hw_flags);
836+ tsc2301_kp_restart(tsc);
837+}
838+
839+static void tsc2301_ts_stop_scan(struct tsc2301 *tsc)
840+{
841+ tsc2301_write_reg(tsc, TSC2301_REG_ADC, TSC2301_ADCREG_STOP_CONVERSION);
842+ tsc2301_kp_restart(tsc);
843+}
844+
845+static void update_pen_state(struct tsc2301_ts *ts, int x, int y, int pressure)
846+{
847+ if (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);
851+ if (!ts->pen_down)
852+ input_report_key(ts->idev, BTN_TOUCH, 1);
853+ ts->pen_down = 1;
854+ } else {
855+ input_report_abs(ts->idev, ABS_PRESSURE, 0);
856+ if (ts->pen_down)
857+ input_report_key(ts->idev, BTN_TOUCH, 0);
858+ ts->pen_down = 0;
859+ }
860+
861+ input_sync(ts->idev);
862+
863+#ifdef VERBOSE
864+ dev_dbg(&tsc->spi->dev, "x %4d y %4d p %4d\n", x, y, pressure);
865+#endif
866+}
867+
868+static int filter(struct tsc2301_ts *ts, int x, int y, int z1, int z2)
869+{
870+ int inside_rect, pressure_limit, Rt;
871+ struct ts_filter *tsf = &ts->filter;
872+
873+ /* validate pressure and position */
874+ if (x > MAX_12BIT || y > MAX_12BIT)
875+ return 0;
876+
877+ /* skip coords if the pressure-components are out of range */
878+ if (z1 < TSF_MIN_Z1 || z2 > TSF_MAX_Z2)
879+ return 0;
880+
881+ /* Use the x,y,z1,z2 directly on the first "pen down" event */
882+ if (ts->event_sent) {
883+ tsf->avg_x += x;
884+ tsf->avg_y += y;
885+ tsf->avg_z1 += z1;
886+ tsf->avg_z2 += z2;
887+
888+ if (++tsf->sample_cnt < TSF_SAMPLES)
889+ return 0;
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;
894+ }
895+ tsf->sample_cnt = 0;
896+ tsf->avg_x = 0;
897+ tsf->avg_y = 0;
898+ tsf->avg_z1 = 0;
899+ tsf->avg_z2 = 0;
900+
901+ pressure_limit = ts->event_sent? ts->max_pressure: ts->touch_pressure;
902+
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)
907+ return 0;
908+
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 */
911+ inside_rect = (
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);
914+
915+ if (!ts->event_sent || !inside_rect) {
916+ ts->x = x;
917+ ts->y = y;
918+ ts->p = Rt;
919+ return 1;
920+ } else if (Rt < ts->p) {
921+ ts->p = Rt;
922+ return 1;
923+ }
924+ return 0;
925+}
926+
927+/*
928+ * This procedure is called by the SPI framework after the coordinates
929+ * have been read from TSC2301
930+ */
931+static void tsc2301_ts_rx(void *arg)
932+{
933+ struct tsc2301 *tsc = arg;
934+ struct tsc2301_ts *ts = tsc->ts;
935+ int send_event;
936+ int x, y, z1, z2;
937+
938+ x = ts->coords->x;
939+ y = ts->coords->y;
940+ z1 = ts->coords->z1;
941+ z2 = ts->coords->z2;
942+
943+ send_event = filter(ts, x, y, z1, z2);
944+ if (send_event) {
945+ update_pen_state(ts, ts->x, ts->y, ts->p);
946+ ts->event_sent = 1;
947+ }
948+
949+ mod_timer(&ts->penup_timer,
950+ jiffies + msecs_to_jiffies(TSC2301_TS_PENUP_TIME));
951+}
952+
953+/*
954+ * Timer is called TSC2301_TS_PENUP_TIME after pen is up
955+ */
956+static void tsc2301_ts_timer_handler(unsigned long data)
957+{
958+ struct tsc2301 *tsc = (struct tsc2301 *)data;
959+ struct tsc2301_ts *ts = tsc->ts;
960+
961+ if (ts->event_sent) {
962+ ts->event_sent = 0;
963+ update_pen_state(ts, 0, 0, 0);
964+ }
965+}
966+
967+/*
968+ * This interrupt is called when pen is down and coordinates are
969+ * available. That is indicated by a falling edge on DEV line.
970+ */
971+static irqreturn_t tsc2301_ts_irq_handler(int irq, void *dev_id)
972+{
973+ struct tsc2301 *tsc = dev_id;
974+ struct tsc2301_ts *ts = tsc->ts;
975+ int r;
976+
977+ r = spi_async(tsc->spi, &ts->read_msg);
978+ if (r)
979+ dev_err(&tsc->spi->dev, "ts: spi_async() failed");
980+
981+ mod_timer(&ts->penup_timer,
982+ jiffies + msecs_to_jiffies(TSC2301_TS_PENUP_TIME));
983+
984+ return IRQ_HANDLED;
985+}
986+
987+static void tsc2301_ts_disable(struct tsc2301 *tsc)
988+{
989+ struct tsc2301_ts *ts = tsc->ts;
990+
991+ if (ts->disable_depth++ != 0)
992+ return;
993+
994+ disable_irq(ts->irq);
995+
996+ /* wait until penup timer expire normally */
997+ do {
998+ msleep(1);
999+ } while (ts->event_sent);
1000+
1001+ tsc2301_ts_stop_scan(tsc);
1002+}
1003+
1004+static void tsc2301_ts_enable(struct tsc2301 *tsc)
1005+{
1006+ struct tsc2301_ts *ts = tsc->ts;
1007+
1008+ if (--ts->disable_depth != 0)
1009+ return;
1010+
1011+ enable_irq(ts->irq);
1012+
1013+ tsc2301_ts_start_scan(tsc);
1014+}
1015+
1016+#ifdef CONFIG_PM
1017+int tsc2301_ts_suspend(struct tsc2301 *tsc)
1018+{
1019+ struct tsc2301_ts *ts = tsc->ts;
1020+
1021+ mutex_lock(&ts->mutex);
1022+ tsc2301_ts_disable(tsc);
1023+ mutex_unlock(&ts->mutex);
1024+
1025+ return 0;
1026+}
1027+
1028+void tsc2301_ts_resume(struct tsc2301 *tsc)
1029+{
1030+ struct tsc2301_ts *ts = tsc->ts;
1031+
1032+ mutex_lock(&ts->mutex);
1033+ tsc2301_ts_enable(tsc);
1034+ mutex_unlock(&ts->mutex);
1035+}
1036+#endif
1037+
1038+static void tsc2301_ts_setup_spi_xfer(struct tsc2301 *tsc)
1039+{
1040+ struct tsc2301_ts *ts = tsc->ts;
1041+ struct spi_message *m = &ts->read_msg;
1042+ struct spi_transfer *x = &ts->read_xfer[0];
1043+
1044+ spi_message_init(m);
1045+
1046+ x->tx_buf = &tsc2301_ts_read_data;
1047+ x->len = 2;
1048+ spi_message_add_tail(x, m);
1049+
1050+ x++;
1051+ x->rx_buf = ts->coords;
1052+ x->len = 8;
1053+ spi_message_add_tail(x, m);
1054+
1055+ m->complete = tsc2301_ts_rx;
1056+ m->context = tsc;
1057+}
1058+
1059+static ssize_t tsc2301_ts_pen_down_show(struct device *dev,
1060+ struct device_attribute *attr,
1061+ char *buf)
1062+{
1063+ struct tsc2301 *tsc = dev_get_drvdata(dev);
1064+
1065+ return sprintf(buf, "%u\n", tsc->ts->pen_down);
1066+}
1067+
1068+static DEVICE_ATTR(pen_down, S_IRUGO, tsc2301_ts_pen_down_show, NULL);
1069+
1070+static ssize_t tsc2301_ts_disable_show(struct device *dev,
1071+ struct device_attribute *attr, char *buf)
1072+{
1073+ struct tsc2301 *tsc = dev_get_drvdata(dev);
1074+ struct tsc2301_ts *ts = tsc->ts;
1075+
1076+ return sprintf(buf, "%u\n", ts->disabled);
1077+}
1078+
1079+static ssize_t tsc2301_ts_disable_store(struct device *dev,
1080+ struct device_attribute *attr,
1081+ const char *buf, size_t count)
1082+{
1083+ struct tsc2301 *tsc = dev_get_drvdata(dev);
1084+ struct tsc2301_ts *ts = tsc->ts;
1085+ char *endp;
1086+ int i;
1087+
1088+ i = simple_strtoul(buf, &endp, 10);
1089+ i = i ? 1 : 0;
1090+ mutex_lock(&ts->mutex);
1091+ if (i == ts->disabled) goto out;
1092+ ts->disabled = i;
1093+
1094+ if (i)
1095+ tsc2301_ts_disable(tsc);
1096+ else
1097+ tsc2301_ts_enable(tsc);
1098+out:
1099+ mutex_unlock(&ts->mutex);
1100+ return count;
1101+}
1102+
1103+static DEVICE_ATTR(disable_ts, 0664, tsc2301_ts_disable_show,
1104+ tsc2301_ts_disable_store);
1105+
1106+int __devinit tsc2301_ts_init(struct tsc2301 *tsc,
1107+ struct tsc2301_platform_data *pdata)
1108+{
1109+ struct tsc2301_ts *ts;
1110+ struct input_dev *idev;
1111+ int r;
1112+ int x_max, y_max;
1113+ int x_fudge, y_fudge, p_fudge;
1114+
1115+ if (pdata->dav_int <= 0) {
1116+ dev_err(&tsc->spi->dev, "need DAV IRQ");
1117+ return -EINVAL;
1118+ }
1119+
1120+ ts = kzalloc(sizeof(*ts), GFP_KERNEL);
1121+ if (ts == NULL)
1122+ return -ENOMEM;
1123+ tsc->ts = ts;
1124+
1125+ ts->coords = kzalloc(sizeof(*ts->coords), GFP_KERNEL);
1126+ if (ts->coords == NULL) {
1127+ kfree(ts);
1128+ return -ENOMEM;
1129+ }
1130+
1131+ ts->irq = pdata->dav_int;
1132+
1133+ init_timer(&ts->penup_timer);
1134+ setup_timer(&ts->penup_timer, tsc2301_ts_timer_handler,
1135+ (unsigned long)tsc);
1136+
1137+ mutex_init(&ts->mutex);
1138+
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;
1144+
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;
1150+
1151+ if ((r = tsc2301_ts_check_config(ts, &ts->hw_flags))) {
1152+ dev_err(&tsc->spi->dev, "invalid configuration\n");
1153+ goto err2;
1154+ }
1155+
1156+ idev = input_allocate_device();
1157+ if (idev == NULL) {
1158+ r = -ENOMEM;
1159+ goto err2;
1160+ }
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;
1166+
1167+ idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
1168+ idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
1169+ ts->idev = idev;
1170+
1171+ tsc2301_ts_setup_spi_xfer(tsc);
1172+
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,
1177+ p_fudge, 0);
1178+
1179+ tsc2301_ts_start_scan(tsc);
1180+
1181+ r = request_irq(ts->irq, tsc2301_ts_irq_handler,
1182+ IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
1183+ "tsc2301-ts", tsc);
1184+ if (r < 0) {
1185+ dev_err(&tsc->spi->dev, "unable to get DAV IRQ");
1186+ goto err3;
1187+ }
1188+ irq_set_irq_wake(ts->irq, 1);
1189+
1190+ if (device_create_file(&tsc->spi->dev, &dev_attr_pen_down) < 0)
1191+ goto err4;
1192+ if (device_create_file(&tsc->spi->dev, &dev_attr_disable_ts) < 0)
1193+ goto err5;
1194+
1195+ r = input_register_device(idev);
1196+ if (r < 0) {
1197+ dev_err(&tsc->spi->dev, "can't register touchscreen device\n");
1198+ goto err6;
1199+ }
1200+
1201+ return 0;
1202+err6:
1203+ device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
1204+err5:
1205+ device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
1206+err4:
1207+ free_irq(ts->irq, tsc);
1208+err3:
1209+ tsc2301_ts_stop_scan(tsc);
1210+ input_free_device(idev);
1211+err2:
1212+ kfree(ts->coords);
1213+ kfree(ts);
1214+ return r;
1215+}
1216+
1217+void __devexit tsc2301_ts_exit(struct tsc2301 *tsc)
1218+{
1219+ struct tsc2301_ts *ts = tsc->ts;
1220+
1221+ tsc2301_ts_disable(tsc);
1222+
1223+ device_remove_file(&tsc->spi->dev, &dev_attr_disable_ts);
1224+ device_remove_file(&tsc->spi->dev, &dev_attr_pen_down);
1225+
1226+ free_irq(ts->irq, tsc);
1227+ input_unregister_device(ts->idev);
1228+
1229+ kfree(ts->coords);
1230+ kfree(ts);
1231+}
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.
1239 
1240+config SPI_TSC2301
1241+ tristate "TSC2301 driver"
1242+ depends on SPI_MASTER
1243+ help
1244+ Say Y here if you have a TSC2301 chip connected to an SPI
1245+ bus on your board.
1246+
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.
1252+
1253+ To compile this driver as a module, choose M here: the
1254+ module will be called tsc2301.
1255+
1256+config SPI_TSC2301_AUDIO
1257+ boolean "TSC2301 audio support"
1258+ depends on SPI_TSC2301 && SND
1259+ help
1260+ Say Y here for if you are using the audio features of TSC2301.
1261+
1262 #
1263 # Add new SPI protocol masters in alphabetical order above this line
1264 #
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
1271-
1272+obj-$(CONFIG_SPI_TSC2301) += tsc2301.o
1273+tsc2301-objs := tsc2301-core.o
1274+tsc2301-$(CONFIG_SPI_TSC2301_AUDIO) += tsc2301-mixer.o
1275--- /dev/null
1276+++ b/drivers/spi/tsc2301-core.c
1277@@ -0,0 +1,297 @@
1278+/*
1279+ * TSC2301 driver
1280+ *
1281+ * Copyright (C) 2005, 2006 Nokia Corporation
1282+ *
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.
1287+ *
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.
1292+ *
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
1296+ *
1297+ */
1298+
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>
1305+
1306+u16 tsc2301_read_reg(struct tsc2301 *tsc, int reg)
1307+{
1308+ struct spi_transfer t[2];
1309+ struct spi_message m;
1310+ u16 data = 0, cmd;
1311+
1312+ cmd = reg;
1313+ cmd |= 0x8000;
1314+
1315+ memset(t, 0, sizeof(t));
1316+ spi_message_init(&m);
1317+ m.spi = tsc->spi;
1318+
1319+ t[0].tx_buf = &cmd;
1320+ t[0].rx_buf = NULL;
1321+ t[0].len = 2;
1322+ spi_message_add_tail(&t[0], &m);
1323+
1324+ t[1].tx_buf = NULL;
1325+ t[1].rx_buf = &data;
1326+ t[1].len = 2;
1327+ spi_message_add_tail(&t[1], &m);
1328+
1329+ spi_sync(m.spi, &m);
1330+
1331+ return data;
1332+}
1333+
1334+void tsc2301_write_reg(struct tsc2301 *tsc, int reg, u16 val)
1335+{
1336+ struct spi_transfer t;
1337+ struct spi_message m;
1338+ u16 data[2];
1339+
1340+ /* Now we prepare the command for transferring */
1341+ data[0] = reg;
1342+ data[1] = val;
1343+
1344+ spi_message_init(&m);
1345+ m.spi = tsc->spi;
1346+
1347+ memset(&t, 0, sizeof(t));
1348+ t.tx_buf = data;
1349+ t.rx_buf = NULL;
1350+ t.len = 4;
1351+ spi_message_add_tail(&t, &m);
1352+
1353+ spi_sync(m.spi, &m);
1354+}
1355+
1356+void tsc2301_write_kbc(struct tsc2301 *tsc, int val)
1357+{
1358+ u16 w;
1359+
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;
1365+}
1366+
1367+void tsc2301_write_pll(struct tsc2301 *tsc,
1368+ int pll_n, int pll_a, int pll_pdc, int pct_e, int pll_o)
1369+{
1370+ u16 w;
1371+
1372+ w = tsc->config2_shadow;
1373+ w &= ~0x3fff;
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;
1379+}
1380+
1381+void tsc2301_read_buf(struct tsc2301 *tsc, int reg, u16 *rx_buf, int len)
1382+{
1383+ struct spi_transfer t[2];
1384+ struct spi_message m;
1385+ u16 cmd, i;
1386+
1387+ cmd = reg;
1388+ cmd |= 0x8000;
1389+
1390+ spi_message_init(&m);
1391+ m.spi = tsc->spi;
1392+
1393+ memset(t, 0, sizeof(t));
1394+ t[0].tx_buf = &cmd;
1395+ t[0].rx_buf = NULL;
1396+ t[0].len = 2;
1397+ spi_message_add_tail(&t[0], &m);
1398+
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);
1403+
1404+ spi_sync(m.spi, &m);
1405+
1406+ for (i = 0; i < len; i++)
1407+ printk(KERN_DEBUG "rx_buf[%d]: %04x\n", i, rx_buf[i]);
1408+}
1409+
1410+static int __devinit tsc2301_probe(struct spi_device *spi)
1411+{
1412+ struct tsc2301 *tsc;
1413+ struct tsc2301_platform_data *pdata = spi->dev.platform_data;
1414+ int r;
1415+ u16 w;
1416+
1417+ if (!pdata) {
1418+ dev_dbg(&spi->dev, "no platform data?\n");
1419+ return -ENODEV;
1420+ }
1421+
1422+ tsc = kzalloc(sizeof(*tsc), GFP_KERNEL);
1423+ if (tsc == NULL)
1424+ return -ENOMEM;
1425+
1426+ dev_set_drvdata(&spi->dev, tsc);
1427+ tsc->spi = spi;
1428+
1429+ tsc->enable_clock = pdata->enable_clock;
1430+ tsc->disable_clock = pdata->disable_clock;
1431+
1432+ if (pdata->reset_gpio >= 0) {
1433+ tsc->reset_gpio = pdata->reset_gpio;
1434+ r = gpio_request(tsc->reset_gpio, "TSC2301 reset");
1435+ if (r < 0)
1436+ goto err1;
1437+ gpio_direction_output(tsc->reset_gpio, 1);
1438+ mdelay(1);
1439+ gpio_set_value(tsc->reset_gpio, 0);
1440+ } else
1441+ tsc->reset_gpio = -1;
1442+
1443+ spi->mode = SPI_MODE_1;
1444+ spi->bits_per_word = 16;
1445+ /* The max speed might've been defined by the board-specific
1446+ * struct */
1447+ if (!spi->max_speed_hz)
1448+ spi->max_speed_hz = TSC2301_HZ;
1449+ spi_setup(spi);
1450+
1451+ /* Soft reset */
1452+ tsc2301_write_reg(tsc, TSC2301_REG_RESET, 0xbb00);
1453+ msleep(1);
1454+
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);
1458+ r = -ENODEV;
1459+ goto err1;
1460+ }
1461+
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);
1465+ r = -ENODEV;
1466+ goto err1;
1467+ }
1468+
1469+ /* Stop keypad scanning */
1470+ tsc2301_write_reg(tsc, TSC2301_REG_KEY, 0x4000);
1471+
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 */
1476+ w |= 1 << 15;
1477+ tsc->config2_shadow = w;
1478+
1479+ r = tsc2301_kp_init(tsc, pdata);
1480+ if (r)
1481+ goto err1;
1482+ r = tsc2301_ts_init(tsc, pdata);
1483+ if (r)
1484+ goto err2;
1485+ r = tsc2301_mixer_init(tsc, pdata);
1486+ if (r)
1487+ goto err3;
1488+ return 0;
1489+
1490+err3:
1491+ tsc2301_ts_exit(tsc);
1492+err2:
1493+ tsc2301_kp_exit(tsc);
1494+err1:
1495+ kfree(tsc);
1496+ return r;
1497+}
1498+
1499+static int __devexit tsc2301_remove(struct spi_device *spi)
1500+{
1501+ struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
1502+
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);
1508+ kfree(tsc);
1509+
1510+ return 0;
1511+}
1512+
1513+#ifdef CONFIG_PM
1514+static int tsc2301_suspend(struct spi_device *spi, pm_message_t mesg)
1515+{
1516+ struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
1517+ int r;
1518+
1519+ if ((r = tsc2301_mixer_suspend(tsc)) < 0)
1520+ return r;
1521+ if ((r = tsc2301_kp_suspend(tsc)) < 0)
1522+ goto err1;
1523+ if ((r = tsc2301_ts_suspend(tsc)) < 0)
1524+ goto err2;
1525+
1526+ return 0;
1527+err2:
1528+ tsc2301_kp_resume(tsc);
1529+err1:
1530+ tsc2301_mixer_resume(tsc);
1531+ return r;
1532+}
1533+
1534+static int tsc2301_resume(struct spi_device *spi)
1535+{
1536+ struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
1537+
1538+ tsc2301_ts_resume(tsc);
1539+ tsc2301_kp_resume(tsc);
1540+ tsc2301_mixer_resume(tsc);
1541+ return 0;
1542+}
1543+#endif
1544+
1545+static struct spi_driver tsc2301_driver = {
1546+ .driver = {
1547+ .name = "tsc2301",
1548+ .bus = &spi_bus_type,
1549+ .owner = THIS_MODULE,
1550+ },
1551+#ifdef CONFIG_PM
1552+ .suspend = tsc2301_suspend,
1553+ .resume = tsc2301_resume,
1554+#endif
1555+ .probe = tsc2301_probe,
1556+ .remove = __devexit_p(tsc2301_remove),
1557+};
1558+
1559+static int __init tsc2301_init(void)
1560+{
1561+ printk("TSC2301 driver initializing\n");
1562+
1563+ return spi_register_driver(&tsc2301_driver);
1564+}
1565+module_init(tsc2301_init);
1566+
1567+static void __exit tsc2301_exit(void)
1568+{
1569+ spi_unregister_driver(&tsc2301_driver);
1570+}
1571+module_exit(tsc2301_exit);
1572+
1573+MODULE_AUTHOR("Juha Yrjölä <juha.yrjola@nokia.com>");
1574+MODULE_LICENSE("GPL");
1575--- /dev/null
1576+++ b/drivers/spi/tsc2301-mixer.c
1577@@ -0,0 +1,1003 @@
1578+/*
1579+ * ALSA Mixer implementation for TSC2301
1580+ *
1581+ * Copyright (C) 2006 Nokia Corporation.
1582+ *
1583+ * Contact: Jarkko Nikula <jarkko.nikula@nokia.com>
1584+ * Juha Yrjola
1585+ *
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
1590+ * stopping MCLK.
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
1600+ * milliseconds.
1601+ *
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.
1605+ *
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.
1610+ *
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
1614+ * 02110-1301 USA
1615+ *
1616+ */
1617+
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>
1625+
1626+#include <sound/core.h>
1627+#include <sound/control.h>
1628+
1629+/* shadow register indexes */
1630+enum {
1631+ /* audio control and volume registers */
1632+ AUDCNTL_INDEX,
1633+ ADCVOL_INDEX,
1634+ DACVOL_INDEX,
1635+ BPVOL_INDEX,
1636+ /* keyclick control register (not needed here) */
1637+ /* audio power control register */
1638+ PD_MISC_INDEX,
1639+ /* TSC2301 GPIO control register */
1640+ GPIO_INDEX,
1641+
1642+ SHADOW_REG_COUNT,
1643+};
1644+
1645+/* structure for driver private data */
1646+struct tsc2301_mixer {
1647+ struct tsc2301 *tsc;
1648+ struct mutex mutex;
1649+
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];
1656+
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;
1661+
1662+ /* latest audio power-up timestamp */
1663+ unsigned long pu_jiffies;
1664+
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;
1669+
1670+ int (* platform_init)(struct device *);
1671+ void (* platform_cleanup)(struct device *);
1672+
1673+ struct tsc2301_mixer_gpio *mixer_gpios;
1674+ int n_mixer_gpios;
1675+};
1676+
1677+#define TSC2301_DAC_DELAY msecs_to_jiffies(100)
1678+#define TSC2301_MIN_PU_PERIOD msecs_to_jiffies(100)
1679+
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)))
1684+
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
1693+
1694+#define TSC2301_INM_MASK 3
1695+#define TSC2301_INML_SHIFT 12
1696+#define TSC2301_INMR_SHIFT 10
1697+
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
1701+
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)
1705+
1706+#define TSC2301_SINGLE(xname, xindex, reg, shadow_index, shift, mask, min) \
1707+{\
1708+ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1709+ .name = xname, \
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) | \
1716+ (min << 28) \
1717+}
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)
1721+
1722+#define TSC2301_DOUBLE(xname, xindex, reg, shadow_index, ls, rs, mask, min) \
1723+{\
1724+ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1725+ .name = xname, \
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) \
1733+}
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)
1738+
1739+#define TSC2301_MUX(xname, xindex, reg, shadow_index, ls, rs, mask) \
1740+{\
1741+ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1742+ .name = xname, \
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) \
1749+}
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)
1753+
1754+#define TSC2301_BOOL(xname, xindex, reg, shadow_index, shift, invert, state) \
1755+{ \
1756+ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1757+ .name = xname, \
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) \
1765+}
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)
1769+
1770+#define TSC2301_SHADOW_INDEX(v) (((v) >> 8) & 7)
1771+
1772+/*
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
1775+ * state
1776+ *
1777+ * Must be called tsc->mixer->mutex locked
1778+ */
1779+static void tsc2301_gpio_power_down(struct tsc2301 *tsc)
1780+{
1781+ struct tsc2301_mixer *mix = tsc->mixer;
1782+ u16 temp;
1783+ int i;
1784+
1785+ temp = mix->shadow_regs[GPIO_INDEX];
1786+ for (i = 0; i < mix->n_mixer_gpios; i++) {
1787+ const struct tsc2301_mixer_gpio *mg;
1788+
1789+ mg = mix->mixer_gpios + i;
1790+ if (mg->deactivate_on_pd) {
1791+ int gpio = mg->gpio;
1792+
1793+ temp &= ~(1 << gpio);
1794+ temp |= mg->inverted << gpio;
1795+ }
1796+ }
1797+ tsc2301_write_reg(tsc, TSC2301_REG_GPIO, temp);
1798+}
1799+
1800+/*
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
1804+ *
1805+ * Must be called tsc->mixer->mutex locked
1806+ */
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,
1810+ int poll_pdsts)
1811+{
1812+ struct tsc2301_mixer *mix = tsc->mixer;
1813+ u16 pd_ctrl, pd_ctrl_old, w;
1814+ unsigned long timeout;
1815+ int power_up = 0;
1816+
1817+ if (mix->delayed_pd_active) {
1818+ mix->delayed_pd_active = 0;
1819+ mix->mclk_enabled--;
1820+ cancel_delayed_work(&mix->delayed_power_down);
1821+ }
1822+
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;
1832+ else
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;
1838+ else
1839+ pd_ctrl &= ~TSC2301_REG_PD_MISC_ADPDR;
1840+ }
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;
1847+ else
1848+ pd_ctrl &= ~TSC2301_REG_PD_MISC_DAPD;
1849+ }
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
1855+ * path */
1856+ pd_ctrl |= TSC2301_REG_PD_MISC_ABPD;
1857+ else
1858+ pd_ctrl &= ~TSC2301_REG_PD_MISC_ABPD;
1859+ }
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;
1869+ else
1870+ pd_ctrl &= ~TSC2301_REG_PD_MISC_MIBPD;
1871+ }
1872+
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
1880+ * selections */
1881+ pd_ctrl |= TSC2301_REG_PD_MISC_ADPDL;
1882+ pd_ctrl |= TSC2301_REG_PD_MISC_ADPDR;
1883+ pd_ctrl |= TSC2301_REG_PD_MISC_MIBPD;
1884+ }
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;
1889+ }
1890+
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);
1902+ }
1903+ }
1904+ } else {
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;
1912+ }
1913+
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;
1921+ }
1922+
1923+ if (pd_ctrl == pd_ctrl_old)
1924+ return;
1925+
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
1931+ * the codec */
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 -
1936+ jiffies;
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,
1941+ timeout + 1);
1942+ return;
1943+ }
1944+ } else
1945+ /* restore GPIOs after codec is powered up */
1946+ power_up = 1;
1947+ }
1948+ mix->shadow_regs[PD_MISC_INDEX] = pd_ctrl;
1949+ tsc2301_write_reg(tsc, TSC2301_REG_PD_MISC, pd_ctrl);
1950+ if (power_up)
1951+ mix->pu_jiffies = jiffies;
1952+ if (!poll_pdsts) {
1953+ if (power_up)
1954+ tsc2301_write_reg(tsc, TSC2301_REG_GPIO,
1955+ mix->shadow_regs[GPIO_INDEX]);
1956+ return;
1957+ }
1958+
1959+ /* wait until power-up/-down is completed */
1960+ timeout = jiffies + msecs_to_jiffies(100);
1961+ w = 0;
1962+ do {
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.
1969+ */
1970+ if (w & TSC2301_REG_PD_MISC_OTSYN) {
1971+ dev_warn(&tsc->spi->dev,
1972+ "I2S clock not in sync or off.\n");
1973+ } else {
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);
1978+ }
1979+ goto out;
1980+ }
1981+ w = tsc2301_read_reg(tsc, TSC2301_REG_PD_MISC);
1982+ } while (!(w & TSC2301_REG_PD_MISC_PDSTS));
1983+
1984+out:
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
1989+ * command */
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]);
2001+ }
2002+}
2003+
2004+static int snd_tsc2301_info_single(struct snd_kcontrol *kcontrol,
2005+ struct snd_ctl_elem_info *uinfo)
2006+{
2007+ int mask = TSC2301_SINGLE_MASK(kcontrol->private_value);
2008+ int minval = TSC2301_SINGLE_MINVAL(kcontrol->private_value);
2009+
2010+ uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2011+ uinfo->count = 1;
2012+ uinfo->value.integer.min = minval;
2013+ uinfo->value.integer.max = mask;
2014+
2015+ return 0;
2016+}
2017+
2018+static int snd_tsc2301_get_single(struct snd_kcontrol *kcontrol,
2019+ struct snd_ctl_elem_value *ucontrol)
2020+{
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);
2026+ u16 shadow_reg;
2027+
2028+ shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2029+
2030+ ucontrol->value.integer.value[0] = (shadow_reg >> shift) & mask;
2031+
2032+ return 0;
2033+}
2034+
2035+static int snd_tsc2301_put_single(struct snd_kcontrol *kcontrol,
2036+ struct snd_ctl_elem_value *ucontrol)
2037+{
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);
2045+ int changed;
2046+
2047+ mutex_lock(&tsc->mixer->mutex);
2048+ shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2049+
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);
2054+
2055+ changed = (shadow_reg != shadow_reg_old);
2056+ tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2057+
2058+ /* update into TSC2301 if necessary */
2059+ if (changed)
2060+ tsc2301_write_reg(tsc, reg, shadow_reg);
2061+ mutex_unlock(&tsc->mixer->mutex);
2062+
2063+ return changed;
2064+}
2065+
2066+static int snd_tsc2301_info_double(struct snd_kcontrol *kcontrol,
2067+ struct snd_ctl_elem_info *uinfo)
2068+{
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);
2073+
2074+ uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN :
2075+ SNDRV_CTL_ELEM_TYPE_INTEGER;
2076+ uinfo->count = 2;
2077+ uinfo->value.integer.min = minval;
2078+ uinfo->value.integer.max = mask;
2079+
2080+ return 0;
2081+}
2082+
2083+static int snd_tsc2301_get_double(struct snd_kcontrol *kcontrol,
2084+ struct snd_ctl_elem_value *ucontrol)
2085+{
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);
2094+ u16 shadow_reg;
2095+
2096+ shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2097+
2098+ /* invert mute bits for the switches */
2099+ if (mask == 1)
2100+ shadow_reg = ~shadow_reg;
2101+
2102+ ucontrol->value.integer.value[0] = (shadow_reg >> ls) & mask;
2103+ ucontrol->value.integer.value[1] = (shadow_reg >> rs) & mask;
2104+
2105+ return 0;
2106+}
2107+
2108+static int snd_tsc2301_put_double(struct snd_kcontrol *kcontrol,
2109+ struct snd_ctl_elem_value *ucontrol)
2110+{
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);
2121+ int changed;
2122+
2123+ mutex_lock(&tsc->mixer->mutex);
2124+ shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2125+
2126+ /* zero bits to be modified */
2127+ shadow_reg &= ~((mask << ls) | (mask << rs));
2128+ /* modify with new value */
2129+ if (mask == 1) {
2130+ /* switch. Invert switch values for the mute bits */
2131+ shadow_reg |=
2132+ ((~ucontrol->value.integer.value[0] & mask) << ls) |
2133+ ((~ucontrol->value.integer.value[1] & mask) << rs);
2134+ } else {
2135+ /* volume */
2136+ shadow_reg |=
2137+ (ucontrol->value.integer.value[0] << ls) |
2138+ (ucontrol->value.integer.value[1] << rs);
2139+ }
2140+
2141+ changed = (shadow_reg != shadow_reg_old);
2142+ tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2143+
2144+ /* update into TSC2301 if necessary */
2145+ if (changed)
2146+ tsc2301_write_reg(tsc, reg, shadow_reg);
2147+
2148+ if (mask == 1)
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);
2153+
2154+ return changed;
2155+}
2156+
2157+static int snd_tsc2301_info_mux(struct snd_kcontrol *kcontrol,
2158+ struct snd_ctl_elem_info *uinfo)
2159+{
2160+ static char *texts[4] = {"Mic", "Line", "Line swapped", "Line mono"};
2161+
2162+ uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2163+ uinfo->count = 2;
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]);
2169+
2170+ return 0;
2171+}
2172+
2173+static int snd_tsc2301_get_mux(struct snd_kcontrol *kcontrol,
2174+ struct snd_ctl_elem_value *ucontrol)
2175+{
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);
2182+ u16 shadow_reg;
2183+
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;
2187+
2188+ return 0;
2189+}
2190+
2191+static int snd_tsc2301_put_mux(struct snd_kcontrol *kcontrol,
2192+ struct snd_ctl_elem_value *ucontrol)
2193+{
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);
2202+ int changed;
2203+
2204+ mutex_lock(&tsc->mixer->mutex);
2205+ shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2206+
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);
2212+
2213+ changed = (shadow_reg != shadow_reg_old);
2214+
2215+ /* update into TSC2301 if necessary */
2216+ if (changed) {
2217+ tsc->mixer->shadow_regs[shadow_index] = shadow_reg;
2218+ tsc2301_write_reg(tsc, reg, shadow_reg);
2219+ }
2220+
2221+ /* check is need to power up/down audio blocks in case of ADC input
2222+ * change */
2223+ tsc2301_power_ctrl(tsc, shadow_index, 0);
2224+ mutex_unlock(&tsc->mixer->mutex);
2225+
2226+ return changed;
2227+}
2228+
2229+static int snd_tsc2301_info_bool(struct snd_kcontrol *kcontrol,
2230+ struct snd_ctl_elem_info *uinfo)
2231+{
2232+ uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2233+ uinfo->count = 1;
2234+ uinfo->value.integer.min = 0;
2235+ uinfo->value.integer.max = 1;
2236+
2237+ return 0;
2238+}
2239+
2240+static int snd_tsc2301_get_bool(struct snd_kcontrol *kcontrol,
2241+ struct snd_ctl_elem_value *ucontrol)
2242+{
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);
2248+ u16 shadow_reg;
2249+
2250+ shadow_reg = tsc->mixer->shadow_regs[shadow_index];
2251+ ucontrol->value.integer.value[0] =
2252+ invert ^ ((shadow_reg >> shift) & 1);
2253+
2254+ return 0;
2255+}
2256+
2257+static int snd_tsc2301_put_bool(struct snd_kcontrol *kcontrol,
2258+ struct snd_ctl_elem_value *ucontrol)
2259+{
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;
2267+ int changed;
2268+
2269+ mutex_lock(&tsc->mixer->mutex);
2270+ shadow_reg = shadow_reg_old = tsc->mixer->shadow_regs[shadow_index];
2271+
2272+ /* zero bit to be modified */
2273+ shadow_reg &= ~(1 << shift);
2274+ /* modify with new value */
2275+ shadow_reg |=
2276+ (invert ^ (ucontrol->value.integer.value[0] & 1)) << shift;
2277+
2278+ changed = (shadow_reg != shadow_reg_old);
2279+
2280+ /* update into TSC2301 if necessary */
2281+ if (changed) {
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
2288+ * flag */
2289+ tsc2301_gpio_power_down(tsc);
2290+ } else
2291+ tsc2301_write_reg(tsc, reg, shadow_reg);
2292+ }
2293+ mutex_unlock(&tsc->mixer->mutex);
2294+
2295+ return changed;
2296+}
2297+
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,
2304+ 1, 0),
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),
2309+
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,
2314+ 1, 0),
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),
2319+
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,
2324+ 1, 0),
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),
2329+
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),
2335+
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),
2341+};
2342+
2343+/* must be called tsc->mixer->mutex locked */
2344+static void tsc2301_flush_shadow_regs(struct tsc2301 *tsc)
2345+{
2346+ int i, page, addr;
2347+ u16 temp;
2348+
2349+ page = TSC2301_REG_TO_PAGE(TSC2301_REG_AUDCNTL);
2350+ addr = TSC2301_REG_TO_ADDR(TSC2301_REG_AUDCNTL);
2351+
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);
2355+ }
2356+ temp = tsc->mixer->shadow_regs[GPIO_INDEX];
2357+ tsc2301_write_reg(tsc, TSC2301_REG_GPIO, temp);
2358+
2359+ /* Update power state of all audio blocks depending are they
2360+ * muted or unused. */
2361+ tsc2301_power_ctrl(tsc, -1, 0);
2362+}
2363+
2364+#ifdef CONFIG_PM
2365+int tsc2301_mixer_suspend(struct tsc2301 *tsc)
2366+{
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);
2377+ return 0;
2378+}
2379+
2380+void tsc2301_mixer_resume(struct tsc2301 *tsc)
2381+{
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);
2387+}
2388+#endif
2389+
2390+void tsc2301_mixer_enable_mclk(struct device *dev)
2391+{
2392+ struct tsc2301 *tsc = dev_get_drvdata(dev);
2393+ struct tsc2301_mixer *mix = tsc->mixer;
2394+
2395+ mutex_lock(&mix->mutex);
2396+ if (!mix->mclk_enabled++ && tsc->enable_clock != NULL) {
2397+ tsc->enable_clock(dev);
2398+ }
2399+ tsc2301_power_ctrl(tsc, -1, 1);
2400+ mutex_unlock(&mix->mutex);
2401+}
2402+
2403+void tsc2301_mixer_disable_mclk(struct device *dev)
2404+{
2405+ struct tsc2301 *tsc = dev_get_drvdata(dev);
2406+ struct tsc2301_mixer *mix = tsc->mixer;
2407+
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);
2413+ }
2414+ mutex_unlock(&mix->mutex);
2415+}
2416+
2417+static void tsc2301_mixer_delayed_power_down(struct work_struct *work)
2418+{
2419+ struct tsc2301_mixer *mix = container_of(work, struct tsc2301_mixer,
2420+ delayed_power_down.work);
2421+ struct tsc2301 *tsc = mix->tsc;
2422+
2423+ mutex_lock(&mix->mutex);
2424+ if (!mix->delayed_pd_active) {
2425+ mutex_unlock(&mix->mutex);
2426+ return;
2427+ }
2428+ mix->delayed_pd_active = 0;
2429+ mutex_unlock(&mix->mutex);
2430+ tsc2301_mixer_disable_mclk(&tsc->spi->dev);
2431+}
2432+
2433+/*
2434+ * Allows audio controller driver to notify its usage of ADC and DAC
2435+ */
2436+void tsc2301_mixer_set_power(struct device *dev, int dac, int adc)
2437+{
2438+ struct tsc2301 *tsc = dev_get_drvdata(dev);
2439+
2440+ mutex_lock(&tsc->mixer->mutex);
2441+ tsc->mixer->adc_enabled = adc;
2442+ tsc->mixer->dac_enabled = dac;
2443+
2444+ /* update power state of all audio blocks */
2445+ tsc2301_power_ctrl(tsc, -1, 1);
2446+ mutex_unlock(&tsc->mixer->mutex);
2447+}
2448+
2449+/*
2450+ * Registers TSC2301 ALSA Mixer controls for the given sound card
2451+ */
2452+int tsc2301_mixer_register_controls(struct device *dev, struct snd_card *card)
2453+{
2454+ struct tsc2301 *tsc = dev_get_drvdata(dev);
2455+ struct tsc2301_mixer *mix = tsc->mixer;
2456+ int i, err;
2457+
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));
2462+ if (err < 0)
2463+ return err;
2464+ }
2465+
2466+ if (!mix->n_mixer_gpios)
2467+ return 0;
2468+
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);
2477+
2478+ ctrlp = snd_ctl_new1(&ctrl, tsc);
2479+ err = snd_ctl_add(card, ctrlp);
2480+ if (err < 0)
2481+ return err;
2482+ }
2483+
2484+ return 0;
2485+}
2486+
2487+int tsc2301_mixer_init(struct tsc2301 *tsc,
2488+ struct tsc2301_platform_data *pdata)
2489+{
2490+ struct tsc2301_mixer *mix;
2491+ int err = 0;
2492+ u16 w;
2493+
2494+ mix = kzalloc(sizeof(*mix), GFP_KERNEL);
2495+ if (mix == NULL)
2496+ return -ENOMEM;
2497+ tsc->mixer = mix;
2498+
2499+ mix->tsc = tsc;
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;
2504+
2505+ INIT_DELAYED_WORK(&mix->delayed_power_down,
2506+ tsc2301_mixer_delayed_power_down);
2507+
2508+ /* initialize shadow register default values */
2509+ w = 0xc000;
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;
2517+
2518+ /* if extra mixer controls configured, then configure associated
2519+ * GPIOs as output and drive their default state */
2520+ if (pdata->n_mixer_gpios) {
2521+ int i;
2522+
2523+ w = 0;
2524+ for (i = 0; i < pdata->n_mixer_gpios; i++) {
2525+ const struct tsc2301_mixer_gpio *mg;
2526+ int gpio;
2527+
2528+ mg = pdata->mixer_gpios + i;
2529+ gpio = mg->gpio;
2530+ w |= (1 << gpio) << 8;
2531+ w |= (mg->inverted ^ mg->def_enable) << gpio;
2532+ }
2533+ mix->shadow_regs[GPIO_INDEX] = w;
2534+
2535+ mix->mixer_gpios = kmalloc(sizeof(*pdata->mixer_gpios) *
2536+ pdata->n_mixer_gpios,
2537+ GFP_KERNEL);
2538+ if (mix->mixer_gpios == NULL) {
2539+ err = -ENOMEM;
2540+ goto err1;
2541+ }
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;
2545+ }
2546+
2547+ /* PLL control */
2548+ tsc2301_write_pll(tsc, pdata->pll_n, pdata->pll_a, pdata->pll_pdc,
2549+ 0, mix->pll_output ? 0 : 1);
2550+
2551+ tsc2301_flush_shadow_regs(tsc);
2552+
2553+ if (mix->platform_init != NULL) {
2554+ err = mix->platform_init(&tsc->spi->dev);
2555+ if (err < 0)
2556+ goto err2;
2557+ }
2558+
2559+ return 0;
2560+err2:
2561+ if (mix->mixer_gpios != NULL)
2562+ kfree(mix->mixer_gpios);
2563+err1:
2564+ kfree(mix);
2565+ return err;
2566+}
2567+
2568+void tsc2301_mixer_exit(struct tsc2301 *tsc)
2569+{
2570+ struct tsc2301_mixer *mixer = tsc->mixer;
2571+
2572+ if (mixer->platform_cleanup != NULL)
2573+ mixer->platform_cleanup(&tsc->spi->dev);
2574+
2575+ if (mixer->mixer_gpios != NULL)
2576+ kfree(mixer->mixer_gpios);
2577+}
2578+
2579+MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
2580+MODULE_LICENSE("GPL");
2581--- /dev/null
2582+++ b/include/linux/spi/tsc2301.h
2583@@ -0,0 +1,208 @@
2584+#ifndef _LINUX_SPI_TSC2301_H
2585+#define _LINUX_SPI_TSC2301_H
2586+
2587+#include <linux/types.h>
2588+#include <linux/timer.h>
2589+
2590+struct tsc2301_platform_data {
2591+ /*
2592+ * Keypad
2593+ */
2594+ s16 reset_gpio;
2595+ s16 keyb_int;
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 */
2599+
2600+ /*
2601+ * Touchscreen
2602+ */
2603+ s16 dav_int;
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;
2615+ u32 ts_x_max;
2616+ u32 ts_x_fudge;
2617+ u32 ts_y_max;
2618+ u32 ts_y_fudge;
2619+
2620+ /*
2621+ * Audio
2622+ */
2623+ unsigned pll_pdc:4;
2624+ unsigned pll_a:4;
2625+ unsigned pll_n:4;
2626+ unsigned pll_output:1; /* Output PLL on GPIO_0 */
2627+
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;
2633+
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);
2640+
2641+ const struct tsc2301_mixer_gpio {
2642+ const char *name;
2643+ unsigned gpio:4;
2644+ unsigned inverted:1;
2645+ unsigned def_enable:1; /* enable by default */
2646+ unsigned deactivate_on_pd:1; /* power-down flag */
2647+ } *mixer_gpios;
2648+ int n_mixer_gpios;
2649+};
2650+
2651+struct tsc2301_kp;
2652+struct tsc2301_ts;
2653+struct tsc2301_mixer;
2654+
2655+struct tsc2301 {
2656+ struct spi_device *spi;
2657+
2658+ s16 reset_gpio;
2659+ u16 config2_shadow;
2660+
2661+ struct tsc2301_kp *kp;
2662+ struct tsc2301_ts *ts;
2663+ struct tsc2301_mixer *mixer;
2664+
2665+ int (*enable_clock)(struct device *dev);
2666+ void (*disable_clock)(struct device *dev);
2667+};
2668+
2669+
2670+#define TSC2301_HZ 33000000
2671+
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)
2675+
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)
2697+
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)
2709+
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
2720+
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
2729+
2730+/* Master Clock Ratio */
2731+#define TSC2301_MCLK_256xFS 0x00 /* default */
2732+#define TSC2301_MCLK_384xFS 0x01
2733+#define TSC2301_MCLK_512xFS 0x02
2734+
2735+
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);
2742+
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);
2749+
2750+#define TSC2301_DECL_EMPTY_MOD(module) \
2751+static inline int tsc2301_##module##_init(struct tsc2301 *tsc, \
2752+ struct tsc2301_platform_data *pdata) \
2753+{ \
2754+ return 0; \
2755+} \
2756+static inline void tsc2301_##module##_exit(struct tsc2301 *tsc) {} \
2757+static inline int tsc2301_##module##_suspend(struct tsc2301 *tsc) \
2758+{ \
2759+ return 0; \
2760+} \
2761+static inline void tsc2301_##module##_resume(struct tsc2301 *tsc) {}
2762+
2763+#ifdef CONFIG_KEYBOARD_TSC2301
2764+TSC2301_DECL_MOD(kp)
2765+void tsc2301_kp_restart(struct tsc2301 *tsc);
2766+#else
2767+TSC2301_DECL_EMPTY_MOD(kp)
2768+static inline void tsc2301_kp_restart(struct tsc2301 *tsc) {}
2769+#endif
2770+
2771+#ifdef CONFIG_TOUCHSCREEN_TSC2301
2772+TSC2301_DECL_MOD(ts)
2773+#else
2774+TSC2301_DECL_EMPTY_MOD(ts)
2775+#endif
2776+
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);
2780+
2781+struct snd_card;
2782+extern int tsc2301_mixer_register_controls(struct device *tsc_dev,
2783+ struct snd_card *card);
2784+#else
2785+TSC2301_DECL_EMPTY_MOD(mixer)
2786+#endif
2787+
2788+extern void tsc2301_mixer_enable_mclk(struct device *tsc_dev);
2789+extern void tsc2301_mixer_disable_mclk(struct device *tsc_dev);
2790+
2791+#endif
2792

Archive Download this file



interactive