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);
586 #if defined(CONFIG_SND_OMAP_TSC2102) || defined(CONFIG_SND_OMAP_TSC2102_MODULE)
589 * Volume level values should be in the range [0, 127].
590 * Higher values mean lower volume.
592 void tsc2102_set_volume(uint8_t left_ch, uint8_t right_ch)
595 if (left_ch == 0x00 || left_ch == 0x7f) /* All 0's or all 1's */
597 if (right_ch == 0x00 || right_ch == 0x7f)
600 spin_lock(&tsc.lock_sync);
602 val = tsc2102_read_sync(TSC2102_DAC_GAIN_CTRL);
604 val &= 0x8080; /* Preserve mute-bits */
605 val |= (left_ch << 8) | right_ch;
607 tsc2102_write_sync(TSC2102_DAC_GAIN_CTRL, val);
609 spin_unlock(&tsc.lock_sync);
611 EXPORT_SYMBOL_GPL(tsc2102_set_volume);
613 void tsc2102_set_mute(int left_ch, int right_ch)
616 spin_lock(&tsc.lock_sync);
618 val = tsc2102_read_sync(TSC2102_DAC_GAIN_CTRL);
620 val &= 0x7f7f; /* Preserve volume settings */
621 val |= (left_ch << 15) | (right_ch << 7);
623 tsc2102_write_sync(TSC2102_DAC_GAIN_CTRL, val);
625 spin_unlock(&tsc.lock_sync);
627 EXPORT_SYMBOL_GPL(tsc2102_set_mute);
629 void tsc2102_get_mute(int *left_ch, int *right_ch)
632 spin_lock(&tsc.lock_sync);
634 val = tsc2102_read_sync(TSC2102_DAC_GAIN_CTRL);
636 spin_unlock(&tsc.lock_sync);
638 *left_ch = !!(val & (1 << 15));
639 *right_ch = !!(val & (1 << 7));
642 void tsc2102_set_deemphasis(int enable)
645 spin_lock(&tsc.lock_sync);
646 val = tsc2102_read_sync(TSC2102_DAC_POWER_CTRL);
649 val &= ~TSC2102_DEEMPF;
651 val |= TSC2102_DEEMPF;
653 tsc2102_write_sync(TSC2102_DAC_POWER_CTRL, val);
654 spin_unlock(&tsc.lock_sync);
656 EXPORT_SYMBOL_GPL(tsc2102_set_deemphasis);
658 void tsc2102_set_bassboost(int enable)
661 spin_lock(&tsc.lock_sync);
662 val = tsc2102_read_sync(TSC2102_DAC_POWER_CTRL);
665 val &= ~TSC2102_BASSBC;
667 val |= TSC2102_BASSBC;
669 tsc2102_write_sync(TSC2102_DAC_POWER_CTRL, val);
670 spin_unlock(&tsc.lock_sync);
672 EXPORT_SYMBOL_GPL(tsc2102_set_bassboost);
674 /* {rate, dsor, fsref} */
675 static const struct tsc2102_rate_info_s tsc2102_rates[] = {
704 int tsc2102_set_rate(int rate)
709 for (i = 0; tsc2102_rates[i].sample_rate; i ++)
710 if (tsc2102_rates[i].sample_rate == rate)
712 if (tsc2102_rates[i].sample_rate == 0) {
713 printk(KERN_ERR "Unknown sampling rate %i.0 Hz\n", rate);
717 spin_lock(&tsc.lock_sync);
719 tsc2102_write_sync(TSC2102_AUDIO1_CTRL, tsc2102_rates[i].divisor);
721 val = tsc2102_read_sync(TSC2102_AUDIO3_CTRL);
723 if (tsc2102_rates[i].fs_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_44K);
727 tsc2102_write_sync(TSC2102_PLL2_CTRL, TSC2102_PLL2_44K);
729 tsc2102_write_sync(TSC2102_AUDIO3_CTRL, val & ~TSC2102_FS44K);
730 /* Enable Phase-locked-loop, set up clock dividers */
731 tsc2102_write_sync(TSC2102_PLL1_CTRL, TSC2102_PLL1_48K);
732 tsc2102_write_sync(TSC2102_PLL2_CTRL, TSC2102_PLL2_48K);
735 spin_unlock(&tsc.lock_sync);
738 EXPORT_SYMBOL(tsc2102_set_rate);
741 * Perform basic set-up with default values and power the DAC on.
743 void tsc2102_dac_power(int state)
745 spin_lock(&tsc.lock_sync);
748 /* 16-bit words, DSP mode, sample at Fsref */
749 tsc2102_write_sync(TSC2102_AUDIO1_CTRL, 0x0100);
750 /* Keyclicks off, soft-stepping at normal rate */
751 tsc2102_write_sync(TSC2102_AUDIO2_CTRL, TSC2102_KEYCLICK_OFF);
752 /* 44.1 kHz Fsref, continuous transfer mode, master DAC */
753 tsc2102_write_sync(TSC2102_AUDIO3_CTRL, 0x2800);
754 /* Soft-stepping enabled */
755 tsc2102_write_sync(TSC2102_AUDIO4_CTRL, 0x0000);
757 /* PLL generates 44.1 kHz */
758 tsc2102_write_sync(TSC2102_PLL1_CTRL, TSC2102_PLL1_44K);
759 tsc2102_write_sync(TSC2102_PLL2_CTRL, TSC2102_PLL2_44K);
761 /* Codec & DAC power up, virtual ground disabled */
762 tsc2102_write_sync(TSC2102_DAC_POWER_CTRL, TSC2102_DAC_ON);
765 tsc2102_write_sync(TSC2102_AUDIO4_CTRL, TSC2102_KEYCLICK_OFF);
766 tsc2102_write_sync(TSC2102_PLL1_CTRL, TSC2102_PLL1_OFF);
769 spin_unlock(&tsc.lock_sync);
771 EXPORT_SYMBOL_GPL(tsc2102_dac_power);
773 void tsc2102_set_i2s_master(int state)
776 spin_lock(&tsc.lock_sync);
778 val = tsc2102_read_sync(TSC2102_AUDIO3_CTRL);
781 tsc2102_write_sync(TSC2102_AUDIO3_CTRL, val | TSC2102_SLVMS);
783 tsc2102_write_sync(TSC2102_AUDIO3_CTRL, val & ~TSC2102_SLVMS);
785 spin_unlock(&tsc.lock_sync);
787 EXPORT_SYMBOL_GPL(tsc2102_set_i2s_master);
789 #endif /* CONFIG_SND_OMAP_TSC2101 */
791 static int tsc2102_configure(struct tsc2102_dev *dev)
794 tsc2102_write_sync(TSC2102_TS_RESET_CTRL, TSC2102_RESET);
796 /* Reference mode, 100 usec delay, 1.25 V reference */
797 if (dev->pdata->use_internal)
798 tsc2102_write_sync(TSC2102_TS_REF_CTRL, TSC2102_ADC_INT_REF);
800 tsc2102_write_sync(TSC2102_TS_REF_CTRL, TSC2102_ADC_EXT_REF);
802 /* 84 usec precharge time, 32 usec sense time */
803 tsc2102_write_sync(TSC2102_TS_CONFIG_CTRL, TSC2102_CONFIG_TIMES);
805 /* PINT/DAV acts as DAV */
806 tsc2102_write_sync(TSC2102_TS_STATUS_CTRL, TSC2102_ADC_DAV);
809 mod_timer(&dev->mode_timer, jiffies +
810 msecs_to_jiffies(dev->mode_msecs));
815 * Retrieves chip revision. Should be always 1.
817 int tsc2102_get_revision(void)
819 return tsc2102_read_sync(TSC2102_AUDIO3_CTRL) & 7;
823 * Emit a short keyclick typically in order to give feedback to
824 * user on specific events.
826 * amplitude must be between 0 (lowest) and 2 (highest).
827 * freq must be between 0 (corresponds to 62.5 Hz) and 7 (8 kHz).
828 * length should be between 2 and 32 periods.
830 * This function sleeps but doesn't sleep until the sound has
833 void tsc2102_keyclick(int amplitude, int freq, int length)
836 spin_lock(&tsc.lock_sync);
837 val = tsc2102_read_sync(TSC2102_AUDIO2_CTRL);
853 val |= (freq & 0x7) << 8;
855 /* Round to nearest supported length */
865 /* Enable keyclick */
868 tsc2102_write_sync(TSC2102_AUDIO2_CTRL, val);
869 spin_unlock(&tsc.lock_sync);
873 #define TSC2102_INPUT(devname, field) \
874 static ssize_t show_ ## devname(struct device *dev, \
875 struct device_attribute *devattr, char *buf) \
877 struct tsc2102_dev *devhwmon = dev_get_drvdata(dev); \
878 int value = devhwmon->field; \
879 return sprintf(buf, "%i\n", value); \
881 static DEVICE_ATTR(devname ## _input, S_IRUGO, show_ ## devname, NULL);
883 TSC2102_INPUT(in0, bat[0])
884 TSC2102_INPUT(in1, bat[1])
885 TSC2102_INPUT(in2, aux[0])
886 TSC2102_INPUT(in3, temp[0])
887 TSC2102_INPUT(in4, temp[1])
889 static ssize_t show_temp1(struct device *dev,
890 struct device_attribute *devattr, char *buf)
892 struct tsc2102_dev *devhwmon = dev_get_drvdata(dev);
896 t1 = devhwmon->temp[0];
897 t2 = devhwmon->temp[1];
900 * Use method #2 (differential) to calculate current temperature.
901 * The difference between TEMP2 and TEMP1 input values is
902 * multiplied by a constant to obtain current temperature.
903 * To find this constant we use the values measured at 25 C as
904 * thermometer calibration data.
906 * 298150 is 25 degrees Celcius represented in Kelvins and
907 * multiplied by 1000 for fixed point precision (273.15 + 25).
908 * 273150 is zero degrees Celcius.
910 diff = devhwmon->pdata->temp_at25c[1] - devhwmon->pdata->temp_at25c[0];
912 value = (t2 - t1) * 298150 / diff; /* This is in Kelvins now */
914 t1 = value - 273150; /* Celcius millidegree */
915 return sprintf(buf, "%i\n", t1);
917 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1, NULL);
918 #endif /* CONFIG_HWMON */
921 static void tsc2102_get_power_status(struct apm_power_info *info)
923 tsc.pdata->apm_report(info, tsc.bat);
932 tsc2102_suspend(struct spi_device *spi, pm_message_t state)
934 struct tsc2102_dev *dev = dev_get_drvdata(&spi->dev);
939 spin_lock(&dev->lock_sync);
941 del_timer(&dev->mode_timer);
942 del_timer(&dev->ts_timer);
944 if (dev->pendown && dev->touch_cb)
947 /* Abort current conversion and power down the ADC */
948 tsc2102_write_sync(TSC2102_TS_ADC_CTRL, TSC2102_ADC_ADST);
950 dev->spi->dev.power.power_state = state;
952 spin_unlock(&dev->lock_sync);
957 * Resume chip operation.
959 static int tsc2102_resume(struct spi_device *spi)
961 struct tsc2102_dev *dev = dev_get_drvdata(&spi->dev);
967 spin_lock(&dev->lock_sync);
969 dev->spi->dev.power.power_state = PMSG_ON;
974 err = tsc2102_configure(dev);
976 spin_unlock(&dev->lock_sync);
980 #define tsc2102_suspend NULL
981 #define tsc2102_resume NULL
984 static struct platform_device tsc2102_ts_device = {
985 .name = "tsc2102-ts",
989 static struct platform_device tsc2102_alsa_device = {
990 .name = "tsc2102-alsa",
994 static int tsc2102_probe(struct spi_device *spi)
996 struct tsc2102_config *pdata = spi->dev.platform_data;
997 struct spi_transfer *spi_buffer;
1001 printk(KERN_ERR "TSC2102: Platform data not supplied\n");
1006 printk(KERN_ERR "TSC2102: Invalid irq value\n");
1013 tsc.data_pending = 0;
1015 tsc.mode_msecs = 1000;
1018 /* Allocate enough struct spi_transfer's for all requests */
1019 spi_buffer = kzalloc(sizeof(struct spi_transfer) * 16, GFP_KERNEL);
1021 printk(KERN_ERR "TSC2102: No memory for SPI buffers\n");
1025 tsc.transfers = spi_buffer;
1026 tsc2102_request_alloc(&tsc, &tsc.req_adc, 0, 4, &spi_buffer);
1027 tsc2102_request_alloc(&tsc, &tsc.req_status, 0, 1, &spi_buffer);
1028 tsc2102_request_alloc(&tsc, &tsc.req_pressure, 0, 1, &spi_buffer);
1029 tsc2102_request_alloc(&tsc, &tsc.req_stopadc, 1, 1, &spi_buffer);
1030 tsc2102_request_alloc(&tsc, &tsc.req_mode, 1, 1, &spi_buffer);
1032 spin_lock_init(&tsc.lock);
1033 spin_lock(&tsc.lock_sync);
1035 /* Get the BCLK - assuming the rate is at 12000000 */
1036 tsc.bclk_ck = clk_get(0, "bclk");
1038 printk(KERN_ERR "Unable to get the clock BCLK\n");
1043 clk_enable(tsc.bclk_ck);
1045 if (request_irq(spi->irq, tsc2102_handler, IRQF_SAMPLE_RANDOM |
1046 IRQF_TRIGGER_FALLING, "tsc2102", &tsc)) {
1047 printk(KERN_ERR "Could not allocate touchscreen IRQ!\n");
1052 setup_timer(&tsc.ts_timer,
1053 tsc2102_pressure, (unsigned long) &tsc);
1054 setup_timer(&tsc.mode_timer,
1055 tsc2102_mode_timer, (unsigned long) &tsc);
1057 /* Set up the communication bus */
1058 dev_set_drvdata(&spi->dev, &tsc);
1059 spi->dev.power.power_state = PMSG_ON;
1060 spi->mode = SPI_MODE_1;
1061 spi->bits_per_word = 16;
1062 err = spi_setup(spi);
1066 /* Now try to detect the chip, make first contact */
1067 if (tsc2102_get_revision() != 0x1) {
1068 printk(KERN_ERR "No TI TSC2102 chip found!\n");
1072 err = tsc2102_configure(&tsc);
1076 /* Register devices controlled by TSC 2102 */
1077 tsc2102_ts_device.dev.platform_data = pdata;
1078 tsc2102_ts_device.dev.parent = &spi->dev;
1079 err = platform_device_register(&tsc2102_ts_device);
1083 tsc2102_alsa_device.dev.platform_data = pdata->alsa_config;
1084 tsc2102_alsa_device.dev.parent = &spi->dev;
1085 err = platform_device_register(&tsc2102_alsa_device);
1090 tsc.hwmondev = hwmon_device_register(&spi->dev);
1091 if (IS_ERR(tsc.hwmondev)) {
1092 printk(KERN_ERR "tsc2102_hwmon: Device registration failed\n");
1093 err = PTR_ERR(tsc.hwmondev);
1097 if (pdata->monitor & TSC_BAT1)
1098 err |= device_create_file(&spi->dev, &dev_attr_in0_input);
1099 if (pdata->monitor & TSC_BAT2)
1100 err |= device_create_file(&spi->dev, &dev_attr_in1_input);
1101 if (pdata->monitor & TSC_AUX)
1102 err |= device_create_file(&spi->dev, &dev_attr_in2_input);
1103 if (pdata->monitor & TSC_TEMP) {
1104 err |= device_create_file(&spi->dev, &dev_attr_temp1_input);
1105 err |= device_create_file(&spi->dev, &dev_attr_in3_input);
1106 err |= device_create_file(&spi->dev, &dev_attr_in4_input);
1110 printk(KERN_ERR "tsc2102_hwmon: Creating one or more "
1111 "attribute files failed\n");
1112 err = 0; /* Not fatal */
1116 if (pdata->apm_report)
1117 apm_get_power_status = tsc2102_get_power_status;
1124 platform_device_unregister(&tsc2102_alsa_device);
1126 platform_device_unregister(&tsc2102_ts_device);
1128 del_timer(&tsc.ts_timer);
1129 del_timer(&tsc.mode_timer);
1130 dev_set_drvdata(&spi->dev, NULL);
1132 clk_disable(tsc.bclk_ck);
1133 clk_put(tsc.bclk_ck);
1135 spin_unlock(&tsc.lock_sync);
1139 static int tsc2102_remove(struct spi_device *spi)
1141 struct tsc2102_dev *dev = dev_get_drvdata(&spi->dev);
1143 spin_lock(&dev->lock_sync);
1145 platform_device_unregister(&tsc2102_ts_device);
1146 platform_device_unregister(&tsc2102_alsa_device);
1148 dev_set_drvdata(&spi->dev, NULL);
1150 /* Release the BCLK */
1151 clk_disable(dev->bclk_ck);
1152 clk_put(dev->bclk_ck);
1154 del_timer(&tsc.mode_timer);
1155 del_timer(&tsc.ts_timer);
1157 kfree(tsc.transfers);
1160 hwmon_device_unregister(dev->hwmondev);
1164 apm_get_power_status = 0;
1167 spin_unlock(&dev->lock_sync);
1172 static struct spi_driver tsc2102_driver = {
1173 .probe = tsc2102_probe,
1174 .remove = tsc2102_remove,
1175 .suspend = tsc2102_suspend,
1176 .resume = tsc2102_resume,
1179 .owner = THIS_MODULE,
1180 .bus = &spi_bus_type,
1184 static char __initdata banner[] = KERN_INFO "TI TSC2102 driver initializing\n";
1186 static int __init tsc2102_init(void)
1189 return spi_register_driver(&tsc2102_driver);
1192 static void __exit tsc2102_exit(void)
1194 spi_unregister_driver(&tsc2102_driver);
1197 module_init(tsc2102_init);
1198 module_exit(tsc2102_exit);
1200 EXPORT_SYMBOL(tsc2102_read_sync);
1201 EXPORT_SYMBOL(tsc2102_reads_sync);
1202 EXPORT_SYMBOL(tsc2102_write_sync);
1203 EXPORT_SYMBOL(tsc2102_keyclick);
1205 MODULE_AUTHOR("Andrzej Zaborowski");
1206 MODULE_DESCRIPTION("Interface driver for TI TSC2102 chips.");
1207 MODULE_LICENSE("GPL");