sync hh.org
[hh.org.git] / drivers / soc / tsc2101.c
blobe09db83c7b52931ddbf6a69e8f0e54ab98972173
1 /*
2 * TI TSC2102 Common Code
4 * Copyright 2005 Openedhand Ltd.
6 * Author: Richard Purdie <richard@o-hand.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/init.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/soc/tsc2101.h>
22 #include "tsc2101.h"
24 extern void tsc2101_ts_setup(struct device *dev);
25 extern void tsc2101_ts_report(struct tsc2101_data *devdata, int x, int y, int p, int pendown);
27 static int tsc2101_regread(struct tsc2101_data *devdata, int regnum)
29 int reg;
30 devdata->platform->send(TSC2101_READ, regnum, &reg, 1);
31 return reg;
34 static void tsc2101_regwrite(struct tsc2101_data *devdata, int regnum, int value)
36 int reg=value;
37 devdata->platform->send(TSC2101_WRITE, regnum, &reg, 1);
38 return;
41 static int tsc2101_ts_penup(struct tsc2101_data *devdata)
43 return !( tsc2101_regread(devdata, TSC2101_REG_STATUS) & TSC2101_STATUS_DAVAIL);
46 #define TSC2101_ADC_DEFAULT (TSC2101_ADC_RES(TSC2101_ADC_RES_12BITP) | TSC2101_ADC_AVG(TSC2101_ADC_4AVG) | TSC2101_ADC_CL(TSC2101_ADC_CL_1MHZ_12BIT) | TSC2101_ADC_PV(TSC2101_ADC_PV_500us) | TSC2101_ADC_AVGFILT_MEAN)
48 static void tsc2101_readdata(struct tsc2101_data *devdata, struct tsc2101_ts_event *ts_data)
50 int z1,z2,fixadc=0;
51 u32 values[4],status;
53 status=tsc2101_regread(devdata, TSC2101_REG_STATUS);
55 if (status & (TSC2101_STATUS_XSTAT | TSC2101_STATUS_YSTAT | TSC2101_STATUS_Z1STAT
56 | TSC2101_STATUS_Z2STAT)) {
58 /* Read X, Y, Z1 and Z2 */
59 devdata->platform->send(TSC2101_READ, TSC2101_REG_X, &values[0], 4);
61 ts_data->x=values[0];
62 ts_data->y=values[1];
63 z1=values[2];
64 z2=values[3];
66 /* Calculate Pressure */
67 if ((z1 != 0) && (ts_data->x!=0) && (ts_data->y!=0))
68 ts_data->p = ((ts_data->x * (z2 -z1) / z1));
69 else
70 ts_data->p=0;
73 if (status & TSC2101_STATUS_BSTAT) {
74 devdata->platform->send(TSC2101_READ, TSC2101_REG_BAT, &values[0], 1);
75 devdata->miscdata.bat=values[0];
76 fixadc=1;
78 if (status & TSC2101_STATUS_AX1STAT) {
79 devdata->platform->send(TSC2101_READ, TSC2101_REG_AUX1, &values[0], 1);
80 devdata->miscdata.aux1=values[0];
81 fixadc=1;
83 if (status & TSC2101_STATUS_AX2STAT) {
84 devdata->platform->send(TSC2101_READ, TSC2101_REG_AUX2, &values[0], 1);
85 devdata->miscdata.aux2=values[0];
86 fixadc=1;
88 if (status & TSC2101_STATUS_T1STAT) {
89 devdata->platform->send(TSC2101_READ, TSC2101_REG_TEMP1, &values[0], 1);
90 devdata->miscdata.temp1=values[0];
91 fixadc=1;
93 if (status & TSC2101_STATUS_T2STAT) {
94 devdata->platform->send(TSC2101_READ, TSC2101_REG_TEMP2, &values[0], 1);
95 devdata->miscdata.temp2=values[0];
96 fixadc=1;
98 if (fixadc) {
99 /* Switch back to touchscreen autoscan */
100 tsc2101_regwrite(devdata, TSC2101_REG_ADC, TSC2101_ADC_DEFAULT | TSC2101_ADC_PSM | TSC2101_ADC_ADMODE(0x2));
104 static void tsc2101_ts_enable(struct tsc2101_data *devdata)
106 //tsc2101_regwrite(devdata, TSC2101_REG_RESETCTL, 0xbb00);
108 /* PINTDAV is data available only */
109 tsc2101_regwrite(devdata, TSC2101_REG_STATUS, 0x4000);
111 /* disable buffer mode */
112 tsc2101_regwrite(devdata, TSC2101_REG_BUFMODE, 0x0);
114 /* use internal reference, 100 usec power-up delay,
115 * power down between conversions, 1.25V internal reference */
116 tsc2101_regwrite(devdata, TSC2101_REG_REF, 0x16);
118 /* enable touch detection, 84usec precharge time, 32 usec sense time */
119 tsc2101_regwrite(devdata, TSC2101_REG_CONFIG, 0x08);
121 /* 3 msec conversion delays */
122 tsc2101_regwrite(devdata, TSC2101_REG_DELAY, 0x0900);
125 * TSC2101-controlled conversions
126 * 12-bit samples
127 * continuous X,Y,Z1,Z2 scan mode
128 * average (mean) 4 samples per coordinate
129 * 1 MHz internal conversion clock
130 * 500 usec panel voltage stabilization delay
132 tsc2101_regwrite(devdata, TSC2101_REG_ADC, TSC2101_ADC_DEFAULT | TSC2101_ADC_PSM | TSC2101_ADC_ADMODE(0x2));
134 return;
137 static void tsc2101_ts_disable(struct tsc2101_data *devdata)
139 /* stop conversions and power down */
140 tsc2101_regwrite(devdata, TSC2101_REG_ADC, 0x4000);
143 static void ts_interrupt_main(struct tsc2101_data *devdata, int isTimer, struct pt_regs *regs)
145 unsigned long flags;
146 struct tsc2101_ts_event ts_data;
148 spin_lock_irqsave(&devdata->lock, flags);
150 //if (!tsc2101_ts_penup(devdata)) {
151 if (devdata->platform->pendown()) {
152 devdata->pendown = 1;
153 tsc2101_readdata(devdata, &ts_data);
154 tsc2101_ts_report(devdata, ts_data.x, ts_data.y, ts_data.p, 1);
155 mod_timer(&(devdata->ts_timer), jiffies + HZ / 100);
156 } else if (devdata->pendown > 0 && devdata->pendown < 3) {
157 mod_timer(&(devdata->ts_timer), jiffies + HZ / 100);
158 devdata->pendown++;
159 } else {
160 if (devdata->pendown)
161 tsc2101_ts_report(devdata, 0, 0, 0, 0);
163 devdata->pendown = 0;
165 set_irq_type(devdata->platform->irq,IRQT_FALLING);
167 /* This must be checked after set_irq_type() to make sure no data was missed */
168 if (devdata->platform->pendown()) {
169 tsc2101_readdata(devdata, &ts_data);
170 mod_timer(&(devdata->ts_timer), jiffies + HZ / 100);
174 spin_unlock_irqrestore(&devdata->lock, flags);
178 static void tsc2101_timer(unsigned long data)
180 struct tsc2101_data *devdata = (struct tsc2101_data *) data;
182 ts_interrupt_main(devdata, 1, NULL);
185 static irqreturn_t tsc2101_handler(int irq, void *dev_id, struct pt_regs *regs)
187 struct tsc2101_data *devdata = dev_id;
189 set_irq_type(devdata->platform->irq,IRQT_NOEDGE);
190 ts_interrupt_main(devdata, 0, regs);
191 return IRQ_HANDLED;
195 static void tsc2101_get_miscdata(struct tsc2101_data *devdata)
197 static int i=0;
198 unsigned long flags;
200 if (devdata->pendown == 0) {
201 i++;
202 spin_lock_irqsave(&devdata->lock, flags);
203 if (i==1)
204 tsc2101_regwrite(devdata, TSC2101_REG_ADC, TSC2101_ADC_DEFAULT | TSC2101_ADC_ADMODE(0x6));
205 else if (i==2)
206 tsc2101_regwrite(devdata, TSC2101_REG_ADC, TSC2101_ADC_DEFAULT | TSC2101_ADC_ADMODE(0x7));
207 else if (i==3)
208 tsc2101_regwrite(devdata, TSC2101_REG_ADC, TSC2101_ADC_DEFAULT | TSC2101_ADC_ADMODE(0x8));
209 else if (i==4)
210 tsc2101_regwrite(devdata, TSC2101_REG_ADC, TSC2101_ADC_DEFAULT | TSC2101_ADC_ADMODE(0xa));
211 else if (i>=5) {
212 tsc2101_regwrite(devdata, TSC2101_REG_ADC, TSC2101_ADC_DEFAULT | TSC2101_ADC_ADMODE(0xc));
213 i=0;
215 spin_unlock_irqrestore(&devdata->lock, flags);
220 static void tsc2101_misc_timer(unsigned long data)
223 struct tsc2101_data *devdata = (struct tsc2101_data *) data;
224 tsc2101_get_miscdata(devdata);
225 mod_timer(&(devdata->misc_timer), jiffies + HZ);
228 void tsc2101_print_miscdata(struct device *dev)
230 struct tsc2101_data *devdata = dev_get_drvdata(dev);
232 printk(KERN_ERR "TSC2101 Bat: %04x\n",devdata->miscdata.bat);
233 printk(KERN_ERR "TSC2101 Aux1: %04x\n",devdata->miscdata.aux1);
234 printk(KERN_ERR "TSC2101 Aux2: %04x\n",devdata->miscdata.aux2);
235 printk(KERN_ERR "TSC2101 Temp1: %04x\n",devdata->miscdata.temp1);
236 printk(KERN_ERR "TSC2101 Temp2: %04x\n",devdata->miscdata.temp2);
239 static int tsc2101_suspend(struct device *dev, u32 state, u32 level)
241 struct tsc2101_data *devdata = dev_get_drvdata(dev);
243 if (level == SUSPEND_POWER_DOWN) {
244 tsc2101_ts_disable(devdata);
245 devdata->platform->suspend();
248 return 0;
251 static int tsc2101_resume(struct device *dev, u32 level)
253 struct tsc2101_data *devdata = dev_get_drvdata(dev);
255 if (level == RESUME_POWER_ON) {
256 devdata->platform->resume();
257 tsc2101_ts_enable(devdata);
260 return 0;
264 static int __init tsc2101_probe(struct device *dev)
266 struct tsc2101_data *devdata;
267 struct tsc2101_ts_event ts_data;
269 if (!(devdata = kcalloc(1, sizeof(struct tsc2101_data), GFP_KERNEL)))
270 return -ENOMEM;
272 dev_set_drvdata(dev,devdata);
273 spin_lock_init(&devdata->lock);
274 devdata->platform = dev->platform_data;
276 init_timer(&devdata->ts_timer);
277 devdata->ts_timer.data = (unsigned long) devdata;
278 devdata->ts_timer.function = tsc2101_timer;
280 init_timer(&devdata->misc_timer);
281 devdata->misc_timer.data = (unsigned long) devdata;
282 devdata->misc_timer.function = tsc2101_misc_timer;
284 /* request irq */
285 if (request_irq(devdata->platform->irq, tsc2101_handler, 0, "tsc2101", devdata)) {
286 printk(KERN_ERR "tsc2101: Could not allocate touchscreen IRQ!\n");
287 kfree(devdata);
288 return -EINVAL;
291 tsc2101_ts_setup(dev);
292 tsc2101_ts_enable(devdata);
294 set_irq_type(devdata->platform->irq,IRQT_FALLING);
295 /* Check there is no pending data */
296 tsc2101_readdata(devdata, &ts_data);
298 mod_timer(&(devdata->misc_timer), jiffies + HZ);
300 return 0;
304 static int __exit tsc2101_remove(struct device *dev)
306 struct tsc2101_data *devdata = dev_get_drvdata(dev);
308 free_irq(devdata->platform->irq, devdata);
309 del_timer_sync(&devdata->ts_timer);
310 del_timer_sync(&devdata->misc_timer);
311 input_unregister_device(&devdata->inputdevice);
312 tsc2101_ts_disable(devdata);
313 kfree(devdata);
314 return 0;
317 static struct device_driver tsc2101_driver = {
318 .name = "tsc2101",
319 .bus = &platform_bus_type,
320 .probe = tsc2101_probe,
321 .remove = __exit_p(tsc2101_remove),
322 .suspend = tsc2101_suspend,
323 .resume = tsc2101_resume,
326 static int __init tsc2101_init(void)
328 return driver_register(&tsc2101_driver);
331 static void __exit tsc2101_exit(void)
333 driver_unregister(&tsc2101_driver);
336 module_init(tsc2101_init);
337 module_exit(tsc2101_exit);
339 MODULE_LICENSE("GPL");