2 * drivers/spi/tsc2102.c
4 * TSC2102 interface driver.
6 * Copyright (c) 2005 Andrzej Zaborowski <balrog@zabor.org>
8 * This package is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This package is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this package; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/sched.h>
26 #include <linux/errno.h>
27 #include <linux/platform_device.h>
28 #include <linux/delay.h>
29 #include <linux/suspend.h>
30 #include <linux/interrupt.h>
31 #include <linux/clk.h>
32 #include <linux/spi/spi.h>
33 #include <linux/spi/tsc2102.h>
34 #include <linux/err.h>
35 #include <linux/hwmon.h>
36 #include <linux/hwmon-sysfs.h>
38 #include <asm/system.h>
41 #include <asm/mach-types.h>
42 #include <asm/hardware.h>
48 /* Bit field definitions for chip registers */
49 #define TSC2102_ADC_TS_CONTROL 0x8bf4
50 #define TSC2102_ADC_SCAN_CONTROL 0x2ff4
51 #define TSC2102_ADC_T1_CONTROL 0x2bf4
52 #define TSC2102_ADC_T2_CONTROL 0x33f4
53 #define TSC2102_ADC_DAV 0x4000
54 #define TSC2102_ADC_INT_REF 0x0016
55 #define TSC2102_ADC_EXT_REF 0x0002
56 #define TSC2102_CONFIG_TIMES 0x0008
57 #define TSC2102_RESET 0xbb00
58 #define TSC2102_ADC_PSTCM (1 << 15)
59 #define TSC2102_ADC_ADST (1 << 14)
60 #define TSC2102_TS_DAV 0x0780
61 #define TSC2102_PS_DAV 0x0078
62 #define TSC2102_T1_DAV 0x0004
63 #define TSC2102_T2_DAV 0x0002
64 #define TSC2102_DAC_ON 0x3ba0
65 #define TSC2102_DAC_OFF 0xafa0
66 #define TSC2102_FS44K (1 << 13)
67 #define TSC2102_PLL1_OFF 0x0000
68 #define TSC2102_PLL1_44K 0x811c
69 #define TSC2102_PLL1_48K 0x8120
70 #define TSC2102_PLL2_44K (5462 << 2)
71 #define TSC2102_PLL2_48K (1920 << 2)
72 #define TSC2102_SLVMS (1 << 11)
73 #define TSC2102_DEEMPF (1 << 0)
74 #define TSC2102_BASSBC (1 << 1)
75 #define TSC2102_KEYCLICK_OFF 0x0000
77 #define CS_CHANGE(val) 0
79 struct tsc2102_spi_req {
80 struct spi_device *dev;
83 struct spi_transfer *transfer;
84 struct spi_message message;
88 struct tsc2102_config *pdata;
89 spinlock_t lock, lock_sync;
92 int state; /* 0: TS, 1: Portscan, 2-3: Temps */
93 struct timer_list ts_timer; /* Busy-wait for PEN UP */
94 struct timer_list mode_timer; /* Change .state every some time */
97 uint16_t status, adc_status, adc_data[4];
98 tsc2102_touch_t touch_cb;
99 tsc2102_coords_t coords_cb;
100 tsc2102_ports_t ports_cb;
101 tsc2102_temp_t temp1_cb;
102 tsc2102_temp_t temp2_cb;
103 unsigned int ts_msecs; /* Interval for .ts_timer */
104 unsigned int mode_msecs; /* Interval for .mode_timer */
106 struct spi_device *spi;
107 struct spi_transfer *transfers;
108 struct tsc2102_spi_req req_adc;
109 struct tsc2102_spi_req req_status;
110 struct tsc2102_spi_req req_pressure;
111 struct tsc2102_spi_req req_stopadc;
112 struct tsc2102_spi_req req_mode;
114 int bat[2], aux[1], temp[2];
115 struct class_device *hwmondev;
118 static struct tsc2102_dev tsc;
120 module_param_named(touch_check_msecs, tsc.ts_msecs, uint, 0);
121 MODULE_PARM_DESC(touch_check_msecs, "Pen-up polling interval in msecs");
123 module_param_named(sensor_scan_msecs, tsc.mode_msecs, uint, 0);
124 MODULE_PARM_DESC(sensor_scan_msecs, "Temperature & battery scan interval");
126 void tsc2102_write_sync(int page, u8 address, u16 data)
128 static struct tsc2102_spi_req req;
129 static struct spi_transfer transfer[2];
132 spi_message_init(&req.message);
133 req.transfer = transfer;
136 req.command = (page << 11) | (address << 5);
137 req.transfer[0].tx_buf = &req.command;
138 req.transfer[0].rx_buf = 0;
139 req.transfer[0].len = 2;
140 spi_message_add_tail(&req.transfer[0], &req.message);
143 req.transfer[1].tx_buf = &data;
144 req.transfer[1].rx_buf = 0;
145 req.transfer[1].len = 2;
146 req.transfer[1].cs_change = CS_CHANGE(1);
147 spi_message_add_tail(&req.transfer[1], &req.message);
149 ret = spi_sync(tsc.spi, &req.message);
150 if (!ret && req.message.status)
151 ret = req.message.status;
154 printk(KERN_ERR "%s: error %i in SPI request\n",
158 void tsc2102_reads_sync(int page, u8 startaddress, u16 *data, int numregs)
160 static struct tsc2102_spi_req req;
161 static struct spi_transfer transfer[6];
164 BUG_ON(numregs + 1 > ARRAY_SIZE(transfer));
166 spi_message_init(&req.message);
167 req.transfer = transfer;
172 req.command = 0x8000 | (page << 11) | (startaddress << 5);
173 req.transfer[i].tx_buf = &req.command;
174 req.transfer[i].rx_buf = 0;
175 req.transfer[i].len = 2;
176 spi_message_add_tail(&req.transfer[i ++], &req.message);
179 while (j < numregs) {
180 req.transfer[i].tx_buf = 0;
181 req.transfer[i].rx_buf = &data[j ++];
182 req.transfer[i].len = 2;
183 req.transfer[i].cs_change = CS_CHANGE(j == numregs);
184 spi_message_add_tail(&req.transfer[i ++], &req.message);
187 ret = spi_sync(tsc.spi, &req.message);
188 if (!ret && req.message.status)
189 ret = req.message.status;
192 printk(KERN_ERR "%s: error %i in SPI request\n",
196 u16 tsc2102_read_sync(int page, u8 address)
199 tsc2102_reads_sync(page, address, &ret, 1);
203 static void tsc2102_write_async(
204 struct tsc2102_spi_req *spi, int page, u8 address, u16 data,
205 void (*complete)(struct tsc2102_dev *context))
209 spi_message_init(&spi->message);
210 spi->message.complete = (void (*)(void *)) complete;
211 spi->message.context = &tsc;
214 spi->command = (page << 11) | (address << 5);
215 spi->transfer[0].tx_buf = &spi->command;
216 spi->transfer[0].rx_buf = 0;
217 spi->transfer[0].len = 2;
218 spi_message_add_tail(&spi->transfer[0], &spi->message);
222 spi->transfer[1].tx_buf = &spi->data;
223 spi->transfer[1].rx_buf = 0;
224 spi->transfer[1].len = 2;
225 spi->transfer[1].cs_change = CS_CHANGE(1);
226 spi_message_add_tail(&spi->transfer[1], &spi->message);
228 ret = spi_async(spi->dev, &spi->message);
230 printk(KERN_ERR "%s: error %i in SPI request\n",
234 static void tsc2102_reads_async(struct tsc2102_spi_req *spi,
235 int page, u8 startaddress, u16 *data, int numregs,
236 void (*complete)(struct tsc2102_dev *context))
240 spi_message_init(&spi->message);
241 spi->message.complete = (void (*)(void *)) complete;
242 spi->message.context = &tsc;
247 spi->command = 0x8000 | (page << 11) | (startaddress << 5);
248 spi->transfer[i].tx_buf = &spi->command;
249 spi->transfer[i].rx_buf = 0;
250 spi->transfer[i].len = 2;
251 spi_message_add_tail(&spi->transfer[i ++], &spi->message);
254 while (j < numregs) {
255 spi->transfer[i].tx_buf = 0;
256 spi->transfer[i].rx_buf = &data[j ++];
257 spi->transfer[i].len = 2;
258 spi->transfer[i].cs_change = CS_CHANGE(j == numregs);
259 spi_message_add_tail(&spi->transfer[i ++], &spi->message);
262 ret = spi_async(spi->dev, &spi->message);
264 printk(KERN_ERR "%s: error %i in SPI request\n",
268 static void tsc2102_read_async(struct tsc2102_spi_req *spi,
269 int page, u8 address, u16 *ret,
270 void (*complete)(struct tsc2102_dev *context))
272 tsc2102_reads_async(spi, page, address, ret, 1, complete);
275 static void tsc2102_request_alloc(struct tsc2102_dev *dev,
276 struct tsc2102_spi_req *spi, int direction, int numregs,
277 struct spi_transfer **buffer)
281 if (direction == 1) /* Write */
286 spi->transfer = *buffer;
290 #define tsc2102_cb_register_func(cb, cb_t) \
291 int tsc2102_ ## cb(cb_t handler) \
293 spin_lock(&tsc.lock); \
295 /* Lock the module */ \
296 if (handler && !tsc.cb) \
297 if (!try_module_get(THIS_MODULE)) { \
298 printk(KERN_INFO "Failed to get TSC module\n"); \
300 if (!handler && tsc.cb) \
301 module_put(THIS_MODULE); \
305 spin_unlock(&tsc.lock); \
309 tsc2102_cb_register_func(touch_cb, tsc2102_touch_t)
310 tsc2102_cb_register_func(coords_cb, tsc2102_coords_t)
311 tsc2102_cb_register_func(ports_cb, tsc2102_ports_t)
312 tsc2102_cb_register_func(temp1_cb, tsc2102_temp_t)
313 tsc2102_cb_register_func(temp2_cb, tsc2102_temp_t)
316 static void tsc2102_print_dav(void)
318 u16 status = tsc2102_read_sync(TSC2102_TS_STATUS_CTRL);
320 printk("TSC2102: data in");
348 static void tsc2102_complete_dummy(struct tsc2102_dev *dev)
352 static inline void tsc2102_touchscreen_mode(struct tsc2102_dev *dev)
354 /* Scan X, Y, Z1, Z2, chip controlled, 12-bit, 16 samples, 500 usec */
355 tsc2102_write_async(&dev->req_mode,
356 TSC2102_TS_ADC_CTRL, TSC2102_ADC_TS_CONTROL,
357 tsc2102_complete_dummy);
360 static inline void tsc2102_portscan_mode(struct tsc2102_dev *dev)
362 /* Scan BAT1, BAT2, AUX, 12-bit, 16 samples, 500 usec */
363 tsc2102_write_async(&dev->req_mode,
364 TSC2102_TS_ADC_CTRL, TSC2102_ADC_SCAN_CONTROL,
365 tsc2102_complete_dummy);
368 static inline void tsc2102_temp1_mode(struct tsc2102_dev *dev)
370 /* Scan TEMP1, 12-bit, 16 samples, 500 usec */
371 tsc2102_write_async(&dev->req_mode,
372 TSC2102_TS_ADC_CTRL, TSC2102_ADC_T1_CONTROL,
373 tsc2102_complete_dummy);
376 static inline void tsc2102_temp2_mode(struct tsc2102_dev *dev)
378 /* Scan TEMP2, 12-bit, 16 samples, 500 usec */
379 tsc2102_write_async(&dev->req_mode,
380 TSC2102_TS_ADC_CTRL, TSC2102_ADC_T2_CONTROL,
381 tsc2102_complete_dummy);
384 static void tsc2102_mode(struct tsc2102_dev *dev)
386 switch (dev->state) {
388 tsc2102_touchscreen_mode(dev);
391 tsc2102_portscan_mode(dev);
394 tsc2102_temp1_mode(dev);
397 tsc2102_temp2_mode(dev);
401 tsc2102_touchscreen_mode(dev);
406 /* Lock is held when this is called. */
407 static void tsc2102_new_mode(struct tsc2102_dev *dev)
409 /* Abort current conversion if any */
410 tsc2102_write_async(&dev->req_stopadc,
411 TSC2102_TS_ADC_CTRL, TSC2102_ADC_ADST,
412 tsc2102_complete_dummy);
418 static void tsc2102_check_status(struct tsc2102_dev *dev);
420 /* TSC has new data for us availiable. */
421 static irqreturn_t tsc2102_handler(int irq, void *dev_id)
423 struct tsc2102_dev *dev = (struct tsc2102_dev *) dev_id;
424 spin_lock_irq(&dev->lock);
426 if (!dev->data_pending)
427 tsc2102_check_status(dev);
429 dev->data_pending ++;
431 spin_unlock_irq(&dev->lock);
435 static void tsc2102_data_report(struct tsc2102_dev *dev)
437 if (dev->status & TSC2102_TS_DAV) {
440 dev->adc_data[0], dev->adc_data[1],
441 dev->adc_data[2], dev->adc_data[3]);
444 if (dev->status & TSC2102_PS_DAV) {
446 dev->ports_cb(dev->adc_data[0],
447 dev->adc_data[1], dev->adc_data[2]);
448 dev->bat[0] = dev->adc_data[0];
449 dev->bat[1] = dev->adc_data[1];
450 dev->aux[0] = dev->adc_data[2];
453 if (dev->status & TSC2102_T1_DAV) {
455 dev->temp1_cb(*dev->adc_data);
456 dev->temp[0] = *dev->adc_data;
459 if (dev->status & TSC2102_T2_DAV) {
461 dev->temp2_cb(*dev->adc_data);
462 dev->temp[1] = *dev->adc_data;
465 spin_lock_irq(&dev->lock);
467 dev->data_pending --;
470 * This may happen if the registers were successfully read and a
471 * new conversion was started and completed by the TSC before the
472 * completion for SPI read was called.
474 if (dev->data_pending)
475 tsc2102_check_status(dev);
477 if (dev->status & (TSC2102_PS_DAV | TSC2102_T1_DAV | TSC2102_T2_DAV))
478 tsc2102_new_mode(dev);
480 spin_unlock_irq(&dev->lock);
483 static void tsc2102_status_report(struct tsc2102_dev *dev)
486 * Read all converted data from corresponding registers
487 * so that the ADC can move on to a new conversion.
489 if (dev->status & TSC2102_TS_DAV) {
490 tsc2102_reads_async(&dev->req_adc, TSC2102_TS_X,
491 dev->adc_data, 4, tsc2102_data_report);
497 mod_timer(&dev->ts_timer, jiffies +
498 msecs_to_jiffies(dev->ts_msecs));
502 if (dev->status & TSC2102_PS_DAV) {
503 tsc2102_reads_async(&dev->req_adc, TSC2102_TS_BAT1,
504 dev->adc_data, 3, tsc2102_data_report);
507 if (dev->status & TSC2102_T1_DAV) {
508 tsc2102_read_async(&dev->req_adc, TSC2102_TS_TEMP1,
509 dev->adc_data, tsc2102_data_report);
512 if (dev->status & TSC2102_T2_DAV) {
513 tsc2102_read_async(&dev->req_adc, TSC2102_TS_TEMP2,
514 dev->adc_data, tsc2102_data_report);
517 if (!(dev->status & (TSC2102_TS_DAV | TSC2102_PS_DAV |
518 TSC2102_T1_DAV | TSC2102_T2_DAV))) {
519 spin_lock_irq(&dev->lock);
520 dev->data_pending --;
521 spin_unlock_irq(&dev->lock);
523 WARN_ON(!dev->state);
527 static void tsc2102_check_status(struct tsc2102_dev *dev)
529 tsc2102_read_async(&dev->req_status, TSC2102_TS_STATUS_CTRL,
530 &dev->status, tsc2102_status_report);
533 static void tsc2102_mode_timer(unsigned long data)
535 struct tsc2102_dev *dev = (struct tsc2102_dev *) data;
536 spin_lock_irq(&dev->lock);
540 tsc2102_new_mode(dev);
542 mod_timer(&dev->mode_timer, jiffies +
543 msecs_to_jiffies(dev->mode_msecs));
544 spin_unlock_irq(&dev->lock);
548 * There are at least three ways to check for pen-up:
549 * - the PINT/DAV pin state,
550 * - reading PSTCM bit in ADC Control register (D15, offset 0x00),
551 * - reading ADST bit in ADC Control register (D14, offset 0x00),
552 * ADC idle would indicate no screen touch.
553 * Unfortunately none of them seems to be 100% accurate and you will
554 * find they are totally inconsistent, i.e. you get to see any arbitrary
555 * combination of values in these three bits. So we will busy-wait
556 * for a moment when all three indicate a pen-up, using a timer, before
557 * we report a pen-up.
559 static void tsc2102_pressure_report(struct tsc2102_dev *dev)
565 (dev->adc_status & TSC2102_ADC_PSTCM) ||
566 !(dev->adc_status & TSC2102_ADC_ADST)) {
567 mod_timer(&dev->ts_timer, jiffies +
568 msecs_to_jiffies(dev->ts_msecs));
576 static void tsc2102_pressure(unsigned long data)
578 struct tsc2102_dev *dev = (struct tsc2102_dev *) data;
580 BUG_ON(!dev->pendown);
582 tsc2102_read_async(&dev->req_pressure, TSC2102_TS_ADC_CTRL,
583 &dev->adc_status, tsc2102_pressure_report);
588 * Volume level values should be in the range [0, 127].
589 * Higher values mean lower volume.
591 void tsc2102_set_volume(uint8_t left_ch, uint8_t right_ch)
594 if (left_ch == 0x00 || left_ch == 0x7f) /* All 0's or all 1's */
596 if (right_ch == 0x00 || right_ch == 0x7f)
599 spin_lock(&tsc.lock_sync);
601 val = tsc2102_read_sync(TSC2102_DAC_GAIN_CTRL);
603 val &= 0x8080; /* Preserve mute-bits */
604 val |= (left_ch << 8) | right_ch;
606 tsc2102_write_sync(TSC2102_DAC_GAIN_CTRL, val);
608 spin_unlock(&tsc.lock_sync);
611 void tsc2102_set_mute(int left_ch, int right_ch)
614 spin_lock(&tsc.lock_sync);
616 val = tsc2102_read_sync(TSC2102_DAC_GAIN_CTRL);
618 val &= 0x7f7f; /* Preserve volume settings */
619 val |= (left_ch << 15) | (right_ch << 7);
621 tsc2102_write_sync(TSC2102_DAC_GAIN_CTRL, val);
623 spin_unlock(&tsc.lock_sync);
626 void tsc2102_get_mute(int *left_ch, int *right_ch)
629 spin_lock(&tsc.lock_sync);
631 val = tsc2102_read_sync(TSC2102_DAC_GAIN_CTRL);
633 spin_unlock(&tsc.lock_sync);
635 *left_ch = !!(val & (1 << 15));
636 *right_ch = !!(val & (1 << 7));
639 void tsc2102_set_deemphasis(int enable)
642 spin_lock(&tsc.lock_sync);
643 val = tsc2102_read_sync(TSC2102_DAC_POWER_CTRL);
646 val &= ~TSC2102_DEEMPF;
648 val |= TSC2102_DEEMPF;
650 tsc2102_write_sync(TSC2102_DAC_POWER_CTRL, val);
651 spin_unlock(&tsc.lock_sync);
654 void tsc2102_set_bassboost(int enable)
657 spin_lock(&tsc.lock_sync);
658 val = tsc2102_read_sync(TSC2102_DAC_POWER_CTRL);
661 val &= ~TSC2102_BASSBC;
663 val |= TSC2102_BASSBC;
665 tsc2102_write_sync(TSC2102_DAC_POWER_CTRL, val);
666 spin_unlock(&tsc.lock_sync);
669 /* {rate, dsor, fsref} */
670 static const struct tsc2102_rate_info_s tsc2102_rates[] = {
699 int tsc2102_set_rate(int rate)
704 for (i = 0; tsc2102_rates[i].sample_rate; i ++)
705 if (tsc2102_rates[i].sample_rate == rate)
707 if (tsc2102_rates[i].sample_rate == 0) {
708 printk(KERN_ERR "Unknown sampling rate %i.0 Hz\n", rate);
712 spin_lock(&tsc.lock_sync);
714 tsc2102_write_sync(TSC2102_AUDIO1_CTRL, tsc2102_rates[i].divisor);
716 val = tsc2102_read_sync(TSC2102_AUDIO3_CTRL);
718 if (tsc2102_rates[i].fs_44k) {
719 tsc2102_write_sync(TSC2102_AUDIO3_CTRL, val | TSC2102_FS44K);
720 /* Enable Phase-locked-loop, set up clock dividers */
721 tsc2102_write_sync(TSC2102_PLL1_CTRL, TSC2102_PLL1_44K);
722 tsc2102_write_sync(TSC2102_PLL2_CTRL, TSC2102_PLL2_44K);
724 tsc2102_write_sync(TSC2102_AUDIO3_CTRL, val & ~TSC2102_FS44K);
725 /* Enable Phase-locked-loop, set up clock dividers */
726 tsc2102_write_sync(TSC2102_PLL1_CTRL, TSC2102_PLL1_48K);
727 tsc2102_write_sync(TSC2102_PLL2_CTRL, TSC2102_PLL2_48K);
730 spin_unlock(&tsc.lock_sync);
735 * Perform basic set-up with default values and power the DAC on.
737 void tsc2102_dac_power(int state)
739 spin_lock(&tsc.lock_sync);
742 /* 16-bit words, DSP mode, sample at Fsref */
743 tsc2102_write_sync(TSC2102_AUDIO1_CTRL, 0x0100);
744 /* Keyclicks off, soft-stepping at normal rate */
745 tsc2102_write_sync(TSC2102_AUDIO2_CTRL, TSC2102_KEYCLICK_OFF);
746 /* 44.1 kHz Fsref, continuous transfer mode, master DAC */
747 tsc2102_write_sync(TSC2102_AUDIO3_CTRL, 0x2800);
748 /* Soft-stepping enabled */
749 tsc2102_write_sync(TSC2102_AUDIO4_CTRL, 0x0000);
751 /* PLL generates 44.1 kHz */
752 tsc2102_write_sync(TSC2102_PLL1_CTRL, TSC2102_PLL1_44K);
753 tsc2102_write_sync(TSC2102_PLL2_CTRL, TSC2102_PLL2_44K);
755 /* Codec & DAC power up, virtual ground disabled */
756 tsc2102_write_sync(TSC2102_DAC_POWER_CTRL, TSC2102_DAC_ON);
759 tsc2102_write_sync(TSC2102_AUDIO4_CTRL, TSC2102_KEYCLICK_OFF);
760 tsc2102_write_sync(TSC2102_PLL1_CTRL, TSC2102_PLL1_OFF);
763 spin_unlock(&tsc.lock_sync);
766 void tsc2102_set_i2s_master(int state)
769 spin_lock(&tsc.lock_sync);
771 val = tsc2102_read_sync(TSC2102_AUDIO3_CTRL);
774 tsc2102_write_sync(TSC2102_AUDIO3_CTRL, val | TSC2102_SLVMS);
776 tsc2102_write_sync(TSC2102_AUDIO3_CTRL, val & ~TSC2102_SLVMS);
778 spin_unlock(&tsc.lock_sync);
780 #endif /* CONFIG_SOUND */
782 static int tsc2102_configure(struct tsc2102_dev *dev)
785 tsc2102_write_sync(TSC2102_TS_RESET_CTRL, TSC2102_RESET);
787 /* Reference mode, 100 usec delay, 1.25 V reference */
788 if (dev->pdata->use_internal)
789 tsc2102_write_sync(TSC2102_TS_REF_CTRL, TSC2102_ADC_INT_REF);
791 tsc2102_write_sync(TSC2102_TS_REF_CTRL, TSC2102_ADC_EXT_REF);
793 /* 84 usec precharge time, 32 usec sense time */
794 tsc2102_write_sync(TSC2102_TS_CONFIG_CTRL, TSC2102_CONFIG_TIMES);
796 /* PINT/DAV acts as DAV */
797 tsc2102_write_sync(TSC2102_TS_STATUS_CTRL, TSC2102_ADC_DAV);
800 mod_timer(&dev->mode_timer, jiffies +
801 msecs_to_jiffies(dev->mode_msecs));
806 * Retrieves chip revision. Should be always 1.
808 int tsc2102_get_revision(void)
810 return tsc2102_read_sync(TSC2102_AUDIO3_CTRL) & 7;
814 * Emit a short keyclick typically in order to give feedback to
815 * user on specific events.
817 * amplitude must be between 0 (lowest) and 2 (highest).
818 * freq must be between 0 (corresponds to 62.5 Hz) and 7 (8 kHz).
819 * length should be between 2 and 32 periods.
821 * This function sleeps but doesn't sleep until the sound has
824 void tsc2102_keyclick(int amplitude, int freq, int length)
827 spin_lock(&tsc.lock_sync);
828 val = tsc2102_read_sync(TSC2102_AUDIO2_CTRL);
844 val |= (freq & 0x7) << 8;
846 /* Round to nearest supported length */
856 /* Enable keyclick */
859 tsc2102_write_sync(TSC2102_AUDIO2_CTRL, val);
860 spin_unlock(&tsc.lock_sync);
864 #define TSC2102_INPUT(devname, field) \
865 static ssize_t show_ ## devname(struct device *dev, \
866 struct device_attribute *devattr, char *buf) \
868 struct tsc2102_dev *devhwmon = dev_get_drvdata(dev); \
869 int value = devhwmon->field; \
870 return sprintf(buf, "%i\n", value); \
872 static DEVICE_ATTR(devname ## _input, S_IRUGO, show_ ## devname, NULL);
874 TSC2102_INPUT(in0, bat[0])
875 TSC2102_INPUT(in1, bat[1])
876 TSC2102_INPUT(in2, aux[0])
877 TSC2102_INPUT(in3, temp[0])
878 TSC2102_INPUT(in4, temp[1])
880 static ssize_t show_temp1(struct device *dev,
881 struct device_attribute *devattr, char *buf)
883 struct tsc2102_dev *devhwmon = dev_get_drvdata(dev);
887 t1 = devhwmon->temp[0];
888 t2 = devhwmon->temp[1];
891 * Use method #2 (differential) to calculate current temperature.
892 * The difference between TEMP2 and TEMP1 input values is
893 * multiplied by a constant to obtain current temperature.
894 * To find this constant we use the values measured at 25 C as
895 * thermometer calibration data.
897 * 298150 is 25 degrees Celcius represented in Kelvins and
898 * multiplied by 1000 for fixed point precision (273.15 + 25).
899 * 273150 is zero degrees Celcius.
901 diff = devhwmon->pdata->temp_at25c[1] - devhwmon->pdata->temp_at25c[0];
903 value = (t2 - t1) * 298150 / diff; /* This is in Kelvins now */
905 t1 = value - 273150; /* Celcius millidegree */
906 return sprintf(buf, "%i\n", t1);
908 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1, NULL);
909 #endif /* CONFIG_HWMON */
912 static void tsc2102_get_power_status(struct apm_power_info *info)
914 tsc.pdata->apm_report(info, tsc.bat);
923 tsc2102_suspend(struct spi_device *spi, pm_message_t state)
925 struct tsc2102_dev *dev = dev_get_drvdata(&spi->dev);
930 spin_lock(&dev->lock_sync);
932 del_timer(&dev->mode_timer);
933 del_timer(&dev->ts_timer);
935 if (dev->pendown && dev->touch_cb)
938 /* Abort current conversion and power down the ADC */
939 tsc2102_write_sync(TSC2102_TS_ADC_CTRL, TSC2102_ADC_ADST);
941 dev->spi->dev.power.power_state = state;
943 spin_unlock(&dev->lock_sync);
948 * Resume chip operation.
950 static int tsc2102_resume(struct spi_device *spi)
952 struct tsc2102_dev *dev = dev_get_drvdata(&spi->dev);
958 spin_lock(&dev->lock_sync);
960 dev->spi->dev.power.power_state = PMSG_ON;
965 err = tsc2102_configure(dev);
967 spin_unlock(&dev->lock_sync);
971 #define tsc2102_suspend NULL
972 #define tsc2102_resume NULL
975 static struct platform_device tsc2102_ts_device = {
976 .name = "tsc2102-ts",
980 static struct platform_device tsc2102_alsa_device = {
981 .name = "tsc2102-alsa",
985 static int tsc2102_probe(struct spi_device *spi)
987 struct tsc2102_config *pdata = spi->dev.platform_data;
988 struct spi_transfer *spi_buffer;
992 printk(KERN_ERR "TSC2102: Platform data not supplied\n");
997 printk(KERN_ERR "TSC2102: Invalid irq value\n");
1004 tsc.data_pending = 0;
1006 tsc.mode_msecs = 1000;
1009 /* Allocate enough struct spi_transfer's for all requests */
1010 spi_buffer = kzalloc(sizeof(struct spi_transfer) * 16, GFP_KERNEL);
1012 printk(KERN_ERR "TSC2102: No memory for SPI buffers\n");
1016 tsc.transfers = spi_buffer;
1017 tsc2102_request_alloc(&tsc, &tsc.req_adc, 0, 4, &spi_buffer);
1018 tsc2102_request_alloc(&tsc, &tsc.req_status, 0, 1, &spi_buffer);
1019 tsc2102_request_alloc(&tsc, &tsc.req_pressure, 0, 1, &spi_buffer);
1020 tsc2102_request_alloc(&tsc, &tsc.req_stopadc, 1, 1, &spi_buffer);
1021 tsc2102_request_alloc(&tsc, &tsc.req_mode, 1, 1, &spi_buffer);
1023 spin_lock_init(&tsc.lock);
1024 spin_lock(&tsc.lock_sync);
1026 /* Get the BCLK - assuming the rate is at 12000000 */
1027 tsc.bclk_ck = clk_get(0, "bclk");
1029 printk(KERN_ERR "Unable to get the clock BCLK\n");
1034 clk_enable(tsc.bclk_ck);
1036 if (request_irq(spi->irq, tsc2102_handler, IRQF_SAMPLE_RANDOM |
1037 IRQF_TRIGGER_FALLING, "tsc2102", &tsc)) {
1038 printk(KERN_ERR "Could not allocate touchscreen IRQ!\n");
1043 setup_timer(&tsc.ts_timer,
1044 tsc2102_pressure, (unsigned long) &tsc);
1045 setup_timer(&tsc.mode_timer,
1046 tsc2102_mode_timer, (unsigned long) &tsc);
1048 /* Set up the communication bus */
1049 dev_set_drvdata(&spi->dev, &tsc);
1050 spi->dev.power.power_state = PMSG_ON;
1051 spi->mode = SPI_MODE_1;
1052 spi->bits_per_word = 16;
1053 err = spi_setup(spi);
1057 /* Now try to detect the chip, make first contact */
1058 if (tsc2102_get_revision() != 0x1) {
1059 printk(KERN_ERR "No TI TSC2102 chip found!\n");
1063 err = tsc2102_configure(&tsc);
1067 /* Register devices controlled by TSC 2102 */
1068 tsc2102_ts_device.dev.platform_data = pdata;
1069 tsc2102_ts_device.dev.parent = &spi->dev;
1070 err = platform_device_register(&tsc2102_ts_device);
1074 tsc2102_alsa_device.dev.platform_data = pdata->alsa_config;
1075 tsc2102_alsa_device.dev.parent = &spi->dev;
1076 err = platform_device_register(&tsc2102_alsa_device);
1081 tsc.hwmondev = hwmon_device_register(&spi->dev);
1082 if (IS_ERR(tsc.hwmondev)) {
1083 printk(KERN_ERR "tsc2102_hwmon: Device registration failed\n");
1084 err = PTR_ERR(tsc.hwmondev);
1088 if (pdata->monitor & TSC_BAT1)
1089 err |= device_create_file(&spi->dev, &dev_attr_in0_input);
1090 if (pdata->monitor & TSC_BAT2)
1091 err |= device_create_file(&spi->dev, &dev_attr_in1_input);
1092 if (pdata->monitor & TSC_AUX)
1093 err |= device_create_file(&spi->dev, &dev_attr_in2_input);
1094 if (pdata->monitor & TSC_TEMP) {
1095 err |= device_create_file(&spi->dev, &dev_attr_temp1_input);
1096 err |= device_create_file(&spi->dev, &dev_attr_in3_input);
1097 err |= device_create_file(&spi->dev, &dev_attr_in4_input);
1101 printk(KERN_ERR "tsc2102_hwmon: Creating one or more "
1102 "attribute files failed\n");
1103 err = 0; /* Not fatal */
1107 if (pdata->apm_report)
1108 apm_get_power_status = tsc2102_get_power_status;
1115 platform_device_unregister(&tsc2102_alsa_device);
1117 platform_device_unregister(&tsc2102_ts_device);
1119 del_timer(&tsc.ts_timer);
1120 del_timer(&tsc.mode_timer);
1121 dev_set_drvdata(&spi->dev, NULL);
1123 clk_disable(tsc.bclk_ck);
1124 clk_put(tsc.bclk_ck);
1126 spin_unlock(&tsc.lock_sync);
1130 static int tsc2102_remove(struct spi_device *spi)
1132 struct tsc2102_dev *dev = dev_get_drvdata(&spi->dev);
1134 spin_lock(&dev->lock_sync);
1136 platform_device_unregister(&tsc2102_ts_device);
1137 platform_device_unregister(&tsc2102_alsa_device);
1139 dev_set_drvdata(&spi->dev, NULL);
1141 /* Release the BCLK */
1142 clk_disable(dev->bclk_ck);
1143 clk_put(dev->bclk_ck);
1145 del_timer(&tsc.mode_timer);
1146 del_timer(&tsc.ts_timer);
1148 kfree(tsc.transfers);
1151 hwmon_device_unregister(dev->hwmondev);
1155 apm_get_power_status = 0;
1158 spin_unlock(&dev->lock_sync);
1163 static struct spi_driver tsc2102_driver = {
1164 .probe = tsc2102_probe,
1165 .remove = tsc2102_remove,
1166 .suspend = tsc2102_suspend,
1167 .resume = tsc2102_resume,
1170 .owner = THIS_MODULE,
1171 .bus = &spi_bus_type,
1175 static char __initdata banner[] = KERN_INFO "TI TSC2102 driver initializing\n";
1177 static int __init tsc2102_init(void)
1180 return spi_register_driver(&tsc2102_driver);
1183 static void __exit tsc2102_exit(void)
1185 spi_unregister_driver(&tsc2102_driver);
1188 module_init(tsc2102_init);
1189 module_exit(tsc2102_exit);
1191 EXPORT_SYMBOL(tsc2102_read_sync);
1192 EXPORT_SYMBOL(tsc2102_reads_sync);
1193 EXPORT_SYMBOL(tsc2102_write_sync);
1194 EXPORT_SYMBOL(tsc2102_keyclick);
1196 MODULE_AUTHOR("Andrzej Zaborowski");
1197 MODULE_DESCRIPTION("Interface driver for TI TSC2102 chips.");
1198 MODULE_LICENSE("GPL");