2 * tsc210x.c - TSC2101/2102/... driver core
4 * Copyright (c) 2005-2007 Andrzej Zaborowski <balrog@zabor.org>
6 * This package is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This package is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this package; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/errno.h>
24 #include <linux/platform_device.h>
25 #include <linux/suspend.h>
26 #include <linux/interrupt.h>
27 #include <linux/clk.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
30 #include <linux/workqueue.h>
31 #include <linux/completion.h>
32 #include <linux/autoconf.h>
33 #include <linux/spi/spi.h>
34 #include <linux/spi/tsc210x.h>
37 /* NOTE: It should be straightforward to make this driver framework handle
38 * tsc2100 and tsc2111 chips, and maybe others too. The main differences
39 * are in the audio codec capabilities, but there are also some differences
40 * in how the various sensors (including touchscreen) are handled.
43 /* Bit field definitions for chip registers */
45 /* Scan X, Y, Z1, Z2, chip controlled, 12-bit, 16 samples, 500 usec */
46 #define TSC210X_ADC_TS_CONTROL 0x8bf4
47 /* Scan BAT1, BAT2, AUX1, AUX2, 12-bit, 16 samples, 500 usec */
48 #define TSC210X_ADC_SCAN_CONTROL 0x2ff4
49 /* Scan TEMP1, 12-bit, 16 samples, 500 usec */
50 #define TSC210X_ADC_T1_CONTROL 0x2bf4
51 /* Scan TEMP2, 12-bit, 16 samples, 500 usec */
52 #define TSC210X_ADC_T2_CONTROL 0x33f4
53 /* PINT/DAV acts as DAV */
54 #define TSC210X_ADC_DAV 0x4000
55 /* Internal reference, 100 usec delay, 1.25 V reference */
56 #define TSC210X_ADC_INT_REF 0x0016
57 /* External reference, 100 usec delay, 1.25 V reference */
58 #define TSC210X_ADC_EXT_REF 0x0002
59 /* 84 usec precharge time, 32 usec sense time */
60 #define TSC210X_CONFIG_TIMES 0x0008
61 /* The reset sequence */
62 #define TSC210X_RESET 0xbb00
64 #define TSC210X_ADC_PSTCM (1 << 15)
66 #define TSC210X_ADC_ADST (1 << 14)
67 /* (At least) One of X, Y, Z1, Z2 contains data */
68 #define TSC210X_TS_DAV 0x0780
69 /* (At least) One of BAT1, BAT2, AUX1, AUX2 contains data */
70 #define TSC210X_PS_DAV 0x0078
71 /* TEMP1 contains data */
72 #define TSC210X_T1_DAV 0x0004
73 /* TEMP2 contains data */
74 #define TSC210X_T2_DAV 0x0002
75 #define TSC2101_DAC_ON 0x0000
76 #define TSC2101_DAC_OFF 0xe7fc
77 #define TSC2102_DAC_ON 0x3ba0
78 #define TSC2102_DAC_OFF 0xafa0
79 #define TSC210X_FS44K (1 << 13)
80 #define TSC210X_PLL1_OFF 0x0000
81 #define TSC210X_PLL1_44K 0x811c
82 #define TSC210X_PLL1_48K 0x8120
83 #define TSC210X_PLL2_44K (5462 << 2)
84 #define TSC210X_PLL2_48K (1920 << 2)
85 #define TSC210X_SLVMS (1 << 11)
86 #define TSC210X_DEEMPF (1 << 0)
87 #define TSC2102_BASSBC (1 << 1)
88 #define TSC210X_KEYCLICK_OFF 0x0000
90 #define CS_CHANGE(val) 0
92 struct tsc210x_spi_req {
93 struct spi_device *dev;
96 struct spi_message message;
104 struct tsc210x_config *pdata;
107 struct workqueue_struct *queue;
108 struct delayed_work ts_worker; /* Poll-wait for PEN UP */
109 struct delayed_work sensor_worker; /* Scan the ADC inputs */
110 struct mutex queue_lock;
111 struct completion data_avail;
113 tsc210x_touch_t touch_cb;
116 tsc210x_coords_t coords_cb;
119 tsc210x_ports_t ports_cb;
122 tsc210x_temp_t temp1_cb;
125 tsc210x_temp_t temp2_cb;
128 struct spi_device *spi;
129 struct spi_transfer *transfers;
130 struct tsc210x_spi_req req_adc;
131 struct tsc210x_spi_req req_status;
132 struct tsc210x_spi_req req_mode;
133 struct tsc210x_spi_req req_stop;
136 int flushing; /* Queue flush in progress */
139 int bat[2], aux[2], temp[2];
143 unsigned int ts_msecs; /* Interval for .ts_timer */
144 unsigned int mode_msecs; /* Interval for .mode_timer */
147 module_param_named(touch_check_msecs, settings.ts_msecs, uint, 0);
148 MODULE_PARM_DESC(touch_check_msecs, "Pen-up polling interval in msecs");
150 module_param_named(sensor_scan_msecs, settings.mode_msecs, uint, 0);
151 MODULE_PARM_DESC(sensor_scan_msecs, "Temperature & battery scan interval");
153 int tsc210x_write_sync(struct tsc210x_dev *dev,
154 int page, u8 address, u16 data)
156 static struct tsc210x_spi_req req;
157 static struct spi_transfer transfer[2];
160 spi_message_init(&req.message);
163 req.command = (page << 11) | (address << 5);
164 transfer[0].tx_buf = &req.command;
165 transfer[0].rx_buf = NULL;
167 spi_message_add_tail(&transfer[0], &req.message);
170 transfer[1].tx_buf = &data;
171 transfer[1].rx_buf = NULL;
173 transfer[1].cs_change = CS_CHANGE(1);
174 spi_message_add_tail(&transfer[1], &req.message);
176 ret = spi_sync(dev->spi, &req.message);
177 if (!ret && req.message.status)
178 ret = req.message.status;
180 dev_dbg(&dev->spi->dev, "write_sync --> %d\n", ret);
184 EXPORT_SYMBOL(tsc210x_write_sync);
186 int tsc210x_reads_sync(struct tsc210x_dev *dev,
187 int page, u8 startaddress, u16 *data, int numregs)
189 static struct tsc210x_spi_req req;
190 static struct spi_transfer transfer[6];
193 if (numregs + 1 > ARRAY_SIZE(transfer))
196 spi_message_init(&req.message);
201 req.command = 0x8000 | (page << 11) | (startaddress << 5);
202 transfer[i].tx_buf = &req.command;
203 transfer[i].rx_buf = NULL;
205 spi_message_add_tail(&transfer[i ++], &req.message);
208 while (j < numregs) {
209 transfer[i].tx_buf = NULL;
210 transfer[i].rx_buf = &data[j ++];
212 transfer[i].cs_change = CS_CHANGE(j == numregs);
213 spi_message_add_tail(&transfer[i ++], &req.message);
216 ret = spi_sync(dev->spi, &req.message);
217 if (!ret && req.message.status)
218 ret = req.message.status;
220 dev_dbg(&dev->spi->dev, "reads_sync --> %d\n", ret);
224 EXPORT_SYMBOL(tsc210x_reads_sync);
226 int tsc210x_read_sync(struct tsc210x_dev *dev, int page, u8 address)
231 status = tsc210x_reads_sync(dev, page, address, &ret, 1);
232 return status ? : ret;
234 EXPORT_SYMBOL(tsc210x_read_sync);
237 static void tsc210x_submit_async(struct tsc210x_spi_req *spi)
241 ret = spi_async(spi->dev, &spi->message);
243 dev_dbg(&spi->dev->dev, "%s: error %i in SPI request\n",
247 static void tsc210x_request_alloc(struct tsc210x_dev *dev,
248 struct tsc210x_spi_req *spi, int direction,
249 int page, u8 startaddress, int numregs, u16 *data,
250 void (*complete)(struct tsc210x_dev *context),
251 struct spi_transfer **transfer)
255 if (direction == 1) /* Write */
260 spi_message_init(&spi->message);
261 spi->message.complete = (void (*)(void *)) complete;
262 spi->message.context = dev;
265 spi->command = (page << 11) | (startaddress << 5);
267 spi->command |= 1 << 15;
269 (*transfer)->tx_buf = &spi->command;
270 (*transfer)->rx_buf = NULL;
271 (*transfer)->len = 2;
272 spi_message_add_tail((*transfer) ++, &spi->message);
277 (*transfer)->tx_buf = &spi->data;
279 (*transfer)->rx_buf = data++;
280 (*transfer)->len = 2;
281 (*transfer)->cs_change = CS_CHANGE(numregs != 1);
282 spi_message_add_tail((*transfer) ++, &spi->message);
286 #define tsc210x_cb_register_func(cb, cb_t) \
287 int tsc210x_ ## cb(struct device *dev, cb_t handler, void *context) \
289 struct tsc210x_dev *tsc = dev_get_drvdata(dev); \
291 /* Lock the module */ \
292 if (handler && !tsc->cb) \
293 if (!try_module_get(THIS_MODULE)) { \
294 dev_err(dev, "Failed to get TSC module\n"); \
296 if (!handler && tsc->cb) \
297 module_put(THIS_MODULE); \
300 tsc->cb ## _ctx = context; \
303 EXPORT_SYMBOL(tsc210x_ ## cb);
305 tsc210x_cb_register_func(touch_cb, tsc210x_touch_t)
306 tsc210x_cb_register_func(coords_cb, tsc210x_coords_t)
307 tsc210x_cb_register_func(ports_cb, tsc210x_ports_t)
308 tsc210x_cb_register_func(temp1_cb, tsc210x_temp_t)
309 tsc210x_cb_register_func(temp2_cb, tsc210x_temp_t)
312 static void tsc210x_print_dav(struct tsc210x_dev *dev)
314 int status = tsc210x_read_sync(dev, TSC210X_TS_STATUS_CTRL);
317 dev_dbg(&dev->spi->dev, "status %d\n", status);
321 if (!(status & 0x0fff))
324 dev_dbg(&dev->spi->dev, "data in %s%s%s%s%s%s%s%s%s%s%s\n",
325 (status & 0x0400) ? " X" : "",
326 (status & 0x0200) ? " Y" : "",
327 (status & 0x0100) ? " Z1" : "",
328 (status & 0x0080) ? " Z2" : "",
329 (status & 0x0040) ? " BAT1" : "",
330 (status & 0x0020) ? " BAT2" : "",
331 (status & 0x0010) ? " AUX1" : "",
332 (status & 0x0008) ? " AUX2" : "",
333 (status & 0x0004) ? " TEMP1" : "",
334 (status & 0x0002) ? " TEMP2" : "",
335 (status & 0x0001) ? " KP" : "");
339 static void tsc210x_complete_dummy(struct tsc210x_dev *dev)
343 static inline void tsc210x_touchscreen_mode(struct tsc210x_dev *dev)
345 /* Scan X, Y, Z1, Z2, chip controlled, 12-bit, 16 samples, 500 usec */
346 dev->req_mode.data = TSC210X_ADC_TS_CONTROL;
347 tsc210x_submit_async(&dev->req_mode);
350 static inline void tsc210x_portscan_mode(struct tsc210x_dev *dev)
352 /* Scan BAT1, BAT2, AUX1, AUX2, 12-bit, 16 samples, 500 usec */
353 dev->req_mode.data = TSC210X_ADC_SCAN_CONTROL;
354 tsc210x_submit_async(&dev->req_mode);
357 static inline void tsc210x_temp1_mode(struct tsc210x_dev *dev)
359 /* Scan TEMP1, 12-bit, 16 samples, 500 usec */
360 dev->req_mode.data = TSC210X_ADC_T1_CONTROL;
361 tsc210x_submit_async(&dev->req_mode);
364 static inline void tsc210x_temp2_mode(struct tsc210x_dev *dev)
366 /* Scan TEMP2, 12-bit, 16 samples, 500 usec */
367 dev->req_mode.data = TSC210X_ADC_T2_CONTROL;
368 tsc210x_submit_async(&dev->req_mode);
371 /* Abort current conversion if any */
372 static void tsc210x_new_mode(struct tsc210x_dev *dev)
374 dev->req_stop.data = TSC210X_ADC_ADST;
375 tsc210x_submit_async(&dev->req_stop);
378 static void tsc210x_queue_scan(struct tsc210x_dev *dev)
380 if (dev->pdata->monitor)
381 if (!queue_delayed_work(dev->queue,
383 msecs_to_jiffies(settings.mode_msecs)))
384 dev_err(&dev->spi->dev,
385 "%s: can't queue measurements\n",
389 static void tsc210x_queue_penup(struct tsc210x_dev *dev)
391 if (!queue_delayed_work(dev->queue,
393 msecs_to_jiffies(settings.ts_msecs)))
394 dev_err(&dev->spi->dev,
395 "%s: can't queue pen-up poll\n",
399 static void tsc210x_status_report(struct tsc210x_dev *dev)
402 * Read all converted data from corresponding registers
403 * so that the ADC can move on to a new conversion.
405 if (dev->status & TSC210X_TS_DAV) {
406 if (!dev->pendown && !dev->flushing) {
409 dev->touch_cb(dev->touch_cb_ctx, 1);
411 tsc210x_queue_penup(dev);
414 tsc210x_submit_async(&dev->req_adc);
417 if (dev->status & (TSC210X_PS_DAV | TSC210X_T1_DAV | TSC210X_T2_DAV))
418 complete(&dev->data_avail);
421 static void tsc210x_data_report(struct tsc210x_dev *dev)
425 if (dev->status & TSC210X_PS_DAV) {
426 tsc210x_reads_sync(dev, TSC210X_TS_BAT1, adc_data, 4);
427 /* NOTE: reads_sync() could fail */
429 dev->bat[0] = adc_data[0];
430 dev->bat[1] = adc_data[1];
431 dev->aux[0] = adc_data[2];
432 dev->aux[1] = adc_data[3];
434 dev->ports_cb(dev->ports_cb_ctx, dev->bat, dev->aux);
437 if (dev->status & TSC210X_T1_DAV) {
438 dev->temp[0] = tsc210x_read_sync(dev, TSC210X_TS_TEMP1);
440 if (dev->temp[0] >= 0 && dev->temp1_cb)
441 dev->temp1_cb(dev->temp1_cb_ctx, dev->temp[0]);
444 if (dev->status & TSC210X_T2_DAV) {
445 dev->temp[1] = tsc210x_read_sync(dev, TSC210X_TS_TEMP2);
447 if (dev->temp[1] >= 0 && dev->temp2_cb)
448 dev->temp2_cb(dev->temp2_cb_ctx, dev->temp[1]);
452 static void tsc210x_coords_report(struct tsc210x_dev *dev)
455 dev->coords_cb(dev->coords_cb_ctx,
456 dev->adc_data[0], dev->adc_data[1],
457 dev->adc_data[2], dev->adc_data[3]);
461 * There are at least three ways to check for pen-up:
462 * - the PINT/DAV pin state,
463 * - reading PSTCM bit in ADC Control register (D15, offset 0x00),
464 * - reading ADST bit in ADC Control register (D14, offset 0x00),
465 * ADC idle would indicate no screen touch.
466 * Unfortunately none of them seems to be 100% accurate and you will
467 * find they are totally inconsistent, i.e. you get to see any arbitrary
468 * combination of values in these three bits. So we will busy-wait
469 * for a moment when the latter two indicate a pen-up, using a queue,
470 * before we report a pen-up.
472 static void tsc210x_pressure(struct work_struct *work)
474 struct tsc210x_dev *dev =
475 container_of(work, struct tsc210x_dev, ts_worker.work);
478 WARN_ON(!dev->pendown);
480 adc_status = tsc210x_read_sync(dev, TSC210X_TS_ADC_CTRL);
481 if (adc_status < 0) {
482 dev_dbg(&dev->spi->dev, "pressure, err %d\n", adc_status);
486 if ((adc_status & TSC210X_ADC_PSTCM) != 0
487 || !(adc_status & TSC210X_ADC_ADST))
488 tsc210x_queue_penup(dev);
492 dev->touch_cb(dev->touch_cb_ctx, 0);
496 static void tsc210x_wait_data(struct tsc210x_dev *dev)
498 wait_for_completion(&dev->data_avail);
500 tsc210x_data_report(dev);
503 static void tsc210x_input_scan(struct work_struct *work)
505 struct tsc210x_dev *dev = (struct tsc210x_dev *)
506 container_of(work, struct tsc210x_dev, sensor_worker.work);
508 tsc210x_new_mode(dev);
510 if (dev->pdata->monitor &
511 (TSC_BAT1 | TSC_BAT2 | TSC_AUX1 | TSC_AUX2)) {
512 tsc210x_portscan_mode(dev);
513 tsc210x_wait_data(dev);
516 if (dev->pdata->monitor & TSC_TEMP) {
517 tsc210x_temp1_mode(dev);
518 tsc210x_wait_data(dev);
520 tsc210x_temp2_mode(dev);
521 tsc210x_wait_data(dev);
524 tsc210x_touchscreen_mode(dev);
526 mutex_lock(&dev->queue_lock);
528 tsc210x_queue_scan(dev);
529 mutex_unlock(&dev->queue_lock);
532 /* ADC has finished a new conversion for us. */
533 static irqreturn_t tsc210x_handler(int irq, void *dev_id)
535 struct tsc210x_dev *dev = (struct tsc210x_dev *) dev_id;
537 /* See what data became available. */
538 tsc210x_submit_async(&dev->req_status);
543 #if defined(CONFIG_SOUND) || defined(CONFIG_SOUND_MODULE)
546 * FIXME the audio support shouldn't be included in upstream patches
547 * until it's ready. They might be better as utility functions linked
548 * with a chip-specific tsc21xx audio module ... e.g. chips with input
549 * channels need more, as will ones with multiple output channels and
550 * so on. Each of these functions should probably return a fault code,
551 * and will need to be exported so the sound drier can be modular.
555 * Volume level values should be in the range [0, 127].
556 * Higher values mean lower volume.
558 void tsc210x_set_dac_volume(struct device *dev, u8 left_ch, u8 right_ch)
560 struct tsc210x_dev *tsc = dev_get_drvdata(dev);
563 if (tsc->kind == tsc2102) {
564 /* All 0's or all 1's */
565 if (left_ch == 0x00 || left_ch == 0x7f)
567 if (right_ch == 0x00 || right_ch == 0x7f)
571 val = tsc210x_read_sync(tsc, TSC210X_DAC_GAIN_CTRL);
573 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
577 val &= 0x8080; /* Preserve mute-bits */
578 val |= (left_ch << 8) | right_ch;
580 tsc210x_write_sync(tsc, TSC210X_DAC_GAIN_CTRL, val);
581 /* NOTE: write_sync() could fail */
584 void tsc210x_set_dac_mute(struct device *dev, int left_ch, int right_ch)
586 struct tsc210x_dev *tsc = dev_get_drvdata(dev);
589 val = tsc210x_read_sync(tsc, TSC210X_DAC_GAIN_CTRL);
591 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
595 val &= 0x7f7f; /* Preserve volume settings */
596 val |= (left_ch << 15) | (right_ch << 7);
598 tsc210x_write_sync(tsc, TSC210X_DAC_GAIN_CTRL, val);
599 /* NOTE: write_sync() could fail */
602 void tsc210x_get_dac_mute(struct device *dev, int *left_ch, int *right_ch)
604 struct tsc210x_dev *tsc = dev_get_drvdata(dev);
607 val = tsc210x_read_sync(tsc, TSC210X_DAC_GAIN_CTRL);
609 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
613 *left_ch = !!(val & (1 << 15));
614 *right_ch = !!(val & (1 << 7));
617 void tsc210x_set_deemphasis(struct device *dev, int enable)
619 struct tsc210x_dev *tsc = dev_get_drvdata(dev);
622 val = tsc210x_read_sync(tsc, TSC210X_POWER_CTRL);
624 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
629 val &= ~TSC210X_DEEMPF;
631 val |= TSC210X_DEEMPF;
633 tsc210x_write_sync(tsc, TSC210X_POWER_CTRL, val);
634 /* NOTE: write_sync() could fail */
637 void tsc2102_set_bassboost(struct device *dev, int enable)
639 struct tsc210x_dev *tsc = dev_get_drvdata(dev);
642 val = tsc210x_read_sync(tsc, TSC210X_POWER_CTRL);
644 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
649 val &= ~TSC2102_BASSBC;
651 val |= TSC2102_BASSBC;
653 tsc210x_write_sync(tsc, TSC210X_POWER_CTRL, val);
654 /* NOTE: write_sync() could fail */
657 /* {rate, dsor, fsref} */
658 static const struct tsc210x_rate_info_s tsc2101_rates[] = {
687 /* {rate, dsor, fsref} */
688 static const struct tsc210x_rate_info_s tsc2102_rates[] = {
717 int tsc210x_set_rate(struct device *dev, int rate)
719 struct tsc210x_dev *tsc = dev_get_drvdata(dev);
722 const struct tsc210x_rate_info_s *rates;
724 if (tsc->kind == tsc2101)
725 rates = tsc2101_rates;
727 rates = tsc2102_rates;
729 for (i = 0; rates[i].sample_rate; i ++)
730 if (rates[i].sample_rate == rate)
732 if (rates[i].sample_rate == 0) {
733 dev_err(dev, "Unknown sampling rate %i.0 Hz\n", rate);
737 if (tsc->kind == tsc2101) {
738 val = tsc210x_read_sync(tsc, TSC210X_AUDIO1_CTRL);
740 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
743 val &= ~((7 << 3) | (7 << 0));
744 val |= rates[i].divisor << 3;
745 val |= rates[i].divisor << 0;
747 val = rates[i].divisor;
749 tsc210x_write_sync(tsc, TSC210X_AUDIO1_CTRL, val);
750 /* NOTE: write_sync() could fail */
752 val = tsc210x_read_sync(tsc, TSC210X_AUDIO3_CTRL);
754 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
758 if (tsc2102_rates[i].fs_44k) {
759 tsc210x_write_sync(tsc,
760 TSC210X_AUDIO3_CTRL, val | TSC210X_FS44K);
761 /* Enable Phase-locked-loop, set up clock dividers */
762 tsc210x_write_sync(tsc, TSC210X_PLL1_CTRL, TSC210X_PLL1_44K);
763 tsc210x_write_sync(tsc, TSC210X_PLL2_CTRL, TSC210X_PLL2_44K);
765 tsc210x_write_sync(tsc,
766 TSC210X_AUDIO3_CTRL, val & ~TSC210X_FS44K);
767 /* Enable Phase-locked-loop, set up clock dividers */
768 tsc210x_write_sync(tsc, TSC210X_PLL1_CTRL, TSC210X_PLL1_48K);
769 tsc210x_write_sync(tsc, TSC210X_PLL2_CTRL, TSC210X_PLL2_48K);
776 * Perform basic set-up with default values and power the DAC/ADC on.
778 void tsc210x_dac_power(struct device *dev, int on)
780 struct tsc210x_dev *tsc = dev_get_drvdata(dev);
782 /* NOTE: write_sync() could fail */
784 /* 16-bit words, DSP mode, sample at Fsref */
785 tsc210x_write_sync(tsc,
786 TSC210X_AUDIO1_CTRL, 0x0100);
787 /* Keyclicks off, soft-stepping at normal rate */
788 tsc210x_write_sync(tsc,
789 TSC210X_AUDIO2_CTRL, TSC210X_KEYCLICK_OFF);
790 /* 44.1 kHz Fsref, continuous transfer mode, master DAC */
791 tsc210x_write_sync(tsc,
792 TSC210X_AUDIO3_CTRL, 0x2000);
793 /* Soft-stepping enabled, 1 dB MIX AGC hysteresis */
794 tsc210x_write_sync(tsc,
795 TSC210X_AUDIO4_CTRL, 0x0000);
797 /* PLL generates 44.1 kHz */
798 tsc210x_write_sync(tsc,
799 TSC210X_PLL1_CTRL, TSC210X_PLL1_44K);
800 tsc210x_write_sync(tsc,
801 TSC210X_PLL2_CTRL, TSC210X_PLL2_44K);
803 /* Codec & DAC power up, virtual ground disabled */
804 tsc210x_write_sync(tsc,
805 TSC210X_POWER_CTRL, (tsc->kind == tsc2101) ?
806 TSC2101_DAC_ON : TSC2102_DAC_ON);
809 tsc210x_write_sync(tsc,
810 TSC210X_AUDIO2_CTRL, TSC210X_KEYCLICK_OFF);
811 tsc210x_write_sync(tsc,
812 TSC210X_PLL1_CTRL, TSC210X_PLL1_OFF);
814 tsc210x_write_sync(tsc,
815 TSC210X_POWER_CTRL, (tsc->kind == tsc2101) ?
816 TSC2102_DAC_OFF : TSC2102_DAC_OFF);
821 void tsc210x_set_i2s_master(struct device *dev, int state)
823 struct tsc210x_dev *tsc = dev_get_drvdata(dev);
826 val = tsc210x_read_sync(tsc, TSC210X_AUDIO3_CTRL);
828 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
832 /* NOTE: write_sync() could fail */
834 tsc210x_write_sync(tsc, TSC210X_AUDIO3_CTRL,
835 val | TSC210X_SLVMS);
837 tsc210x_write_sync(tsc, TSC210X_AUDIO3_CTRL,
838 val & ~TSC210X_SLVMS);
840 #endif /* CONFIG_SOUND */
842 static int tsc210x_configure(struct tsc210x_dev *dev)
844 /* NOTE: write_sync() could fail */
847 tsc210x_write_sync(dev, TSC210X_TS_RESET_CTRL, TSC210X_RESET);
850 if (dev->pdata->use_internal)
851 tsc210x_write_sync(dev,
852 TSC210X_TS_REF_CTRL, TSC210X_ADC_INT_REF);
854 tsc210x_write_sync(dev,
855 TSC210X_TS_REF_CTRL, TSC210X_ADC_EXT_REF);
857 /* Precharge and sense delays, pen touch detection on */
858 tsc210x_write_sync(dev, TSC210X_TS_CONFIG_CTRL, TSC210X_CONFIG_TIMES);
860 /* PINT/DAV acts as DAV */
861 tsc210x_write_sync(dev, TSC210X_TS_STATUS_CTRL, TSC210X_ADC_DAV);
863 tsc210x_queue_scan(dev);
867 void tsc210x_keyclick(struct tsc210x_dev *dev,
868 int amplitude, int freq, int length)
872 val = tsc210x_read_sync(dev, TSC210X_AUDIO2_CTRL);
874 dev_dbg(&dev->spi->dev, "%s, err %d\n",
893 val |= (freq & 0x7) << 8;
895 /* Round to nearest supported length */
896 if (dev->kind == tsc2101)
897 val = (min(length - 1, 31) >> 1) << 4;
909 /* Enable keyclick */
912 /* NOTE: write_sync() could fail */
913 tsc210x_write_sync(dev, TSC210X_AUDIO2_CTRL, val);
915 EXPORT_SYMBOL(tsc210x_keyclick);
922 tsc210x_suspend(struct spi_device *spi, pm_message_t state)
924 struct tsc210x_dev *dev = dev_get_drvdata(&spi->dev);
929 /* Stop the inputs scan loop */
930 mutex_lock(&dev->queue_lock);
932 cancel_delayed_work(&dev->sensor_worker);
933 mutex_unlock(&dev->queue_lock);
934 flush_workqueue(dev->queue);
936 /* Wait until pen-up happens */
938 flush_workqueue(dev->queue);
940 /* Abort current conversion and power down the ADC */
941 tsc210x_write_sync(dev, TSC210X_TS_ADC_CTRL, TSC210X_ADC_ADST);
942 /* NOTE: write_sync() could fail */
948 * Resume chip operation.
950 static int tsc210x_resume(struct spi_device *spi)
952 struct tsc210x_dev *dev = dev_get_drvdata(&spi->dev);
958 mutex_lock(&dev->queue_lock);
959 err = tsc210x_configure(dev);
962 mutex_unlock(&dev->queue_lock);
967 #define tsc210x_suspend NULL
968 #define tsc210x_resume NULL
971 /* REVISIT don't make these static */
972 static struct platform_device tsc210x_ts_device = {
973 .name = "tsc210x-ts",
977 static struct platform_device tsc210x_hwmon_device = {
978 .name = "tsc210x-hwmon",
982 static struct platform_device tsc210x_alsa_device = {
983 .name = "tsc210x-alsa",
987 static int tsc210x_probe(struct spi_device *spi, enum tsc_type type)
989 struct tsc210x_config *pdata = spi->dev.platform_data;
990 struct spi_transfer *spi_buffer;
991 struct tsc210x_dev *dev;
996 dev_dbg(&spi->dev, "Platform data not supplied\n");
1001 dev_dbg(&spi->dev, "Invalid irq value\n");
1005 dev = (struct tsc210x_dev *)
1006 kzalloc(sizeof(struct tsc210x_dev), GFP_KERNEL);
1008 dev_dbg(&spi->dev, "No memory\n");
1016 dev->queue = create_singlethread_workqueue(spi->dev.driver->name);
1018 dev_dbg(&spi->dev, "Can't make a workqueue\n");
1023 mutex_init(&dev->queue_lock);
1024 init_completion(&dev->data_avail);
1026 /* Allocate enough struct spi_transfer's for all requests */
1027 spi_buffer = kzalloc(sizeof(struct spi_transfer) * 16, GFP_KERNEL);
1029 dev_dbg(&spi->dev, "No memory for SPI buffers\n");
1034 dev->transfers = spi_buffer;
1035 tsc210x_request_alloc(dev, &dev->req_adc, 0,
1036 TSC210X_TS_X, 4, dev->adc_data,
1037 tsc210x_coords_report, &spi_buffer);
1038 tsc210x_request_alloc(dev, &dev->req_status, 0,
1039 TSC210X_TS_STATUS_CTRL, 1, &dev->status,
1040 tsc210x_status_report, &spi_buffer);
1041 tsc210x_request_alloc(dev, &dev->req_mode, 1,
1042 TSC210X_TS_ADC_CTRL, 1, NULL,
1043 tsc210x_complete_dummy, &spi_buffer);
1044 tsc210x_request_alloc(dev, &dev->req_stop, 1,
1045 TSC210X_TS_ADC_CTRL, 1, NULL,
1046 tsc210x_complete_dummy, &spi_buffer);
1050 dev->bclk_ck = clk_get(&spi->dev, pdata->bclk);
1051 if (IS_ERR(dev->bclk_ck)) {
1052 err = PTR_ERR(dev->bclk_ck);
1053 dev_dbg(&spi->dev, "Unable to get '%s': %i\n",
1058 clk_enable(dev->bclk_ck);
1061 INIT_DELAYED_WORK(&dev->ts_worker, tsc210x_pressure);
1062 INIT_DELAYED_WORK(&dev->sensor_worker, tsc210x_input_scan);
1064 /* Setup the communication bus */
1065 dev_set_drvdata(&spi->dev, dev);
1066 spi->mode = SPI_MODE_1;
1067 spi->bits_per_word = 16;
1068 err = spi_setup(spi);
1072 /* Now try to detect the chip, make first contact. These chips
1073 * don't self-identify, but we can expect that the status register
1074 * reports the ADC is idle and use that as a sanity check. (It'd
1075 * be even better if we did a soft reset first...)
1077 reg = tsc210x_read_sync(dev, TSC210X_TS_ADC_CTRL);
1080 dev_dbg(&dev->spi->dev, "adc_ctrl, err %d\n", err);
1083 if (!(reg & (1 << 14))) {
1085 dev_dbg(&dev->spi->dev, "adc_ctrl, busy? - %04x\n", reg);
1089 reg = tsc210x_read_sync(dev, TSC210X_AUDIO3_CTRL);
1092 dev_dbg(&dev->spi->dev, "revision, err %d\n", err);
1095 if (reg == 0xffff) {
1097 dev_dbg(&dev->spi->dev, "no device, err %d\n", err);
1100 dev_info(&spi->dev, "rev %d, irq %d\n", reg & 0x0007, spi->irq);
1102 err = tsc210x_configure(dev);
1106 /* We want no interrupts before configuration succeeds. */
1107 mutex_lock(&dev->queue_lock);
1110 if (request_irq(spi->irq, tsc210x_handler, IRQF_SAMPLE_RANDOM |
1111 IRQF_TRIGGER_FALLING, spi->dev.driver->name,
1113 dev_dbg(&spi->dev, "Could not allocate touchscreen IRQ!\n");
1118 /* Register subdevices controlled by the TSC 2101/2102 */
1119 tsc210x_ts_device.dev.platform_data = dev;
1120 tsc210x_ts_device.dev.parent = &spi->dev;
1121 err = platform_device_register(&tsc210x_ts_device);
1125 tsc210x_hwmon_device.dev.platform_data = pdata;
1126 tsc210x_hwmon_device.dev.parent = &spi->dev;
1127 err = platform_device_register(&tsc210x_hwmon_device);
1131 tsc210x_alsa_device.dev.platform_data = pdata->alsa_config;
1132 tsc210x_alsa_device.dev.parent = &spi->dev;
1133 err = platform_device_register(&tsc210x_alsa_device);
1138 mutex_unlock(&dev->queue_lock);
1142 platform_device_unregister(&tsc210x_hwmon_device);
1144 platform_device_unregister(&tsc210x_ts_device);
1146 mutex_unlock(&dev->queue_lock);
1148 dev_set_drvdata(&spi->dev, NULL);
1149 clk_disable(dev->bclk_ck);
1150 clk_put(dev->bclk_ck);
1152 kfree(dev->transfers);
1154 destroy_workqueue(dev->queue);
1160 static int tsc2101_probe(struct spi_device *spi)
1162 return tsc210x_probe(spi, tsc2101);
1165 static int tsc2102_probe(struct spi_device *spi)
1167 return tsc210x_probe(spi, tsc2102);
1170 static int tsc210x_remove(struct spi_device *spi)
1172 struct tsc210x_dev *dev = dev_get_drvdata(&spi->dev);
1174 /* Stop the inputs scan loop */
1175 mutex_lock(&dev->queue_lock);
1177 cancel_delayed_work(&dev->sensor_worker);
1178 mutex_unlock(&dev->queue_lock);
1179 flush_workqueue(dev->queue);
1181 /* Wait for pen-up */
1182 while (dev->pendown)
1183 flush_workqueue(dev->queue);
1185 /* Abort current conversion and power down the ADC */
1186 tsc210x_write_sync(dev, TSC210X_TS_ADC_CTRL, TSC210X_ADC_ADST);
1187 /* NOTE: write_sync() could fail */
1189 destroy_workqueue(dev->queue);
1191 platform_device_unregister(&tsc210x_ts_device);
1192 platform_device_unregister(&tsc210x_hwmon_device);
1193 platform_device_unregister(&tsc210x_alsa_device);
1195 dev_set_drvdata(&spi->dev, NULL);
1197 /* Release the BCLK */
1198 clk_disable(dev->bclk_ck);
1199 clk_put(dev->bclk_ck);
1201 kfree(dev->transfers);
1207 static struct spi_driver tsc2101_driver = {
1208 .probe = tsc2101_probe,
1209 .remove = tsc210x_remove,
1210 .suspend = tsc210x_suspend,
1211 .resume = tsc210x_resume,
1214 .owner = THIS_MODULE,
1215 .bus = &spi_bus_type,
1219 static struct spi_driver tsc2102_driver = {
1220 .probe = tsc2102_probe,
1221 .remove = tsc210x_remove,
1222 .suspend = tsc210x_suspend,
1223 .resume = tsc210x_resume,
1226 .owner = THIS_MODULE,
1227 .bus = &spi_bus_type,
1231 static char __initdata banner[] = KERN_INFO "TI TSC210x driver initializing\n";
1233 static int __init tsc210x_init(void)
1238 settings.ts_msecs = 20;
1239 settings.mode_msecs = 1000;
1241 err = spi_register_driver(&tsc2101_driver);
1245 err = spi_register_driver(&tsc2102_driver);
1247 spi_unregister_driver(&tsc2101_driver);
1251 module_init(tsc210x_init);
1253 static void __exit tsc210x_exit(void)
1255 spi_unregister_driver(&tsc2101_driver);
1256 spi_unregister_driver(&tsc2102_driver);
1258 module_exit(tsc210x_exit);
1260 MODULE_AUTHOR("Andrzej Zaborowski");
1261 MODULE_DESCRIPTION("Interface driver for TI TSC210x chips.");
1262 MODULE_LICENSE("GPL");