Full support for Ginger Console
[linux-ginger.git] / drivers / input / touchscreen / tsc2004.c
blob0bba2e670cf6264d326dcf6a812367aca4ff6a4a
1 /*
2 * drivers/input/touchscreen/tsc2004.c
4 * Copyright (C) 2009 Texas Instruments Inc
5 * Author: Vaibhav Hiremath <hvaibhav@ti.com>
7 * Using code from:
8 * - tsc2007.c
10 * This package is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/input.h>
28 #include <linux/interrupt.h>
29 #include <linux/i2c.h>
30 #include <linux/i2c/tsc2004.h>
33 #define TS_POLL_DELAY 1 /* ms delay between samples */
34 #define TS_POLL_PERIOD 1 /* ms delay between samples */
36 /* Control byte 0 */
37 #define TSC2004_CMD0(addr, pnd, rw) ((addr<<3)|(pnd<<1)|rw)
38 /* Control byte 1 */
39 #define TSC2004_CMD1(cmd, mode, rst) ((1<<7)|(cmd<<4)|(mode<<2)|(rst<<1))
41 /* Command Bits */
42 #define READ_REG 1
43 #define WRITE_REG 0
44 #define SWRST_TRUE 1
45 #define SWRST_FALSE 0
46 #define PND0_TRUE 1
47 #define PND0_FALSE 0
49 /* Converter function mapping */
50 enum convertor_function {
51 MEAS_X_Y_Z1_Z2, /* Measure X,Y,z1 and Z2: 0x0 */
52 MEAS_X_Y, /* Measure X and Y only: 0x1 */
53 MEAS_X, /* Measure X only: 0x2 */
54 MEAS_Y, /* Measure Y only: 0x3 */
55 MEAS_Z1_Z2, /* Measure Z1 and Z2 only: 0x4 */
56 MEAS_AUX, /* Measure Auxillary input: 0x5 */
57 MEAS_TEMP1, /* Measure Temparature1: 0x6 */
58 MEAS_TEMP2, /* Measure Temparature2: 0x7 */
59 MEAS_AUX_CONT, /* Continuously measure Auxillary input: 0x8 */
60 X_DRV_TEST, /* X-Axis drivers tested 0x9 */
61 Y_DRV_TEST, /* Y-Axis drivers tested 0xA */
62 /*Command Reserved*/
63 SHORT_CKT_TST = 0xC, /* Short circuit test: 0xC */
64 XP_XN_DRV_STAT, /* X+,Y- drivers status: 0xD */
65 YP_YN_DRV_STAT, /* X+,Y- drivers status: 0xE */
66 YP_XN_DRV_STAT /* Y+,X- drivers status: 0xF */
69 /* Register address mapping */
70 enum register_address {
71 X_REG, /* X register: 0x0 */
72 Y_REG, /* Y register: 0x1 */
73 Z1_REG, /* Z1 register: 0x2 */
74 Z2_REG, /* Z2 register: 0x3 */
75 AUX_REG, /* AUX register: 0x4 */
76 TEMP1_REG, /* Temp1 register: 0x5 */
77 TEMP2_REG, /* Temp2 register: 0x6 */
78 STAT_REG, /* Status Register: 0x7 */
79 AUX_HGH_TH_REG, /* AUX high threshold register: 0x8 */
80 AUX_LOW_TH_REG, /* AUX low threshold register: 0x9 */
81 TMP_HGH_TH_REG, /* Temp high threshold register:0xA */
82 TMP_LOW_TH_REG, /* Temp low threshold register: 0xB */
83 CFR0_REG, /* Configuration register 0: 0xC */
84 CFR1_REG, /* Configuration register 1: 0xD */
85 CFR2_REG, /* Configuration register 2: 0xE */
86 CONV_FN_SEL_STAT /* Convertor function select register: 0xF */
89 /* Supported Resolution modes */
90 enum resolution_mode {
91 MODE_10BIT, /* 10 bit resolution */
92 MODE_12BIT /* 12 bit resolution */
95 /* Configuraton register bit fields */
96 /* CFR0 */
97 #define PEN_STS_CTRL_MODE (1<<15)
98 #define ADC_STS (1<<14)
99 #define RES_CTRL (1<<13)
100 #define ADC_CLK_4MHZ (0<<11)
101 #define ADC_CLK_2MHZ (1<<11)
102 #define ADC_CLK_1MHZ (2<<11)
103 #define PANEL_VLTG_STB_TIME_0US (0<<8)
104 #define PANEL_VLTG_STB_TIME_100US (1<<8)
105 #define PANEL_VLTG_STB_TIME_500US (2<<8)
106 #define PANEL_VLTG_STB_TIME_1MS (3<<8)
107 #define PANEL_VLTG_STB_TIME_5MS (4<<8)
108 #define PANEL_VLTG_STB_TIME_10MS (5<<8)
109 #define PANEL_VLTG_STB_TIME_50MS (6<<8)
110 #define PANEL_VLTG_STB_TIME_100MS (7<<8)
112 /* CFR2 */
113 #define PINTS1 (1<<15)
114 #define PINTS0 (1<<14)
115 #define MEDIAN_VAL_FLTR_SIZE_1 (0<<12)
116 #define MEDIAN_VAL_FLTR_SIZE_3 (1<<12)
117 #define MEDIAN_VAL_FLTR_SIZE_7 (2<<12)
118 #define MEDIAN_VAL_FLTR_SIZE_15 (3<<12)
119 #define AVRG_VAL_FLTR_SIZE_1 (0<<10)
120 #define AVRG_VAL_FLTR_SIZE_3_4 (1<<10)
121 #define AVRG_VAL_FLTR_SIZE_7_8 (2<<10)
122 #define AVRG_VAL_FLTR_SIZE_16 (3<<10)
123 #define MAV_FLTR_EN_X (1<<4)
124 #define MAV_FLTR_EN_Y (1<<3)
125 #define MAV_FLTR_EN_Z (1<<2)
127 #define MAX_12BIT ((1 << 12) - 1)
128 #define MEAS_MASK 0xFFF
130 struct ts_event {
131 u16 x;
132 u16 y;
133 u16 z1, z2;
136 struct tsc2004 {
137 struct input_dev *input;
138 char phys[32];
139 struct delayed_work work;
141 struct i2c_client *client;
143 u16 model;
144 u16 x_plate_ohms;
146 bool pendown;
147 int irq;
149 int (*get_pendown_state)(void);
150 void (*clear_penirq)(void);
153 static inline int tsc2004_read_word_data(struct tsc2004 *tsc, u8 cmd)
155 s32 data;
156 u16 val;
158 data = i2c_smbus_read_word_data(tsc->client, cmd);
159 if (data < 0) {
160 dev_err(&tsc->client->dev, "i2c io (read) error: %d\n", data);
161 return data;
164 /* The protocol and raw data format from i2c interface:
165 * S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
166 * Where DataLow has [D11-D4], DataHigh has [D3-D0 << 4 | Dummy 4bit].
168 val = swab16(data) >> 4;
170 dev_dbg(&tsc->client->dev, "data: 0x%x, val: 0x%x\n", data, val);
172 return val;
175 static inline int tsc2004_write_word_data(struct tsc2004 *tsc, u8 cmd, u16 data)
177 u16 val;
179 val = swab16(data);
180 return i2c_smbus_write_word_data(tsc->client, cmd, val);
183 static inline int tsc2004_write_cmd(struct tsc2004 *tsc, u8 value)
185 return i2c_smbus_write_byte(tsc->client, value);
188 static int tsc2004_prepare_for_reading(struct tsc2004 *ts)
190 int err;
191 int cmd, data;
193 /* Reset the TSC, configure for 12 bit */
194 cmd = TSC2004_CMD1(MEAS_X_Y_Z1_Z2, MODE_12BIT, SWRST_TRUE);
195 err = tsc2004_write_cmd(ts, cmd);
196 if (err < 0)
197 return err;
199 /* Enable interrupt for PENIRQ and DAV */
200 cmd = TSC2004_CMD0(CFR2_REG, PND0_FALSE, WRITE_REG);
201 data = PINTS1 | PINTS0 | MEDIAN_VAL_FLTR_SIZE_15 |
202 AVRG_VAL_FLTR_SIZE_7_8 | MAV_FLTR_EN_X | MAV_FLTR_EN_Y |
203 MAV_FLTR_EN_Z;
204 err = tsc2004_write_word_data(ts, cmd, data);
205 if (err < 0)
206 return err;
208 /* Configure the TSC in TSMode 1 */
209 cmd = TSC2004_CMD0(CFR0_REG, PND0_FALSE, WRITE_REG);
210 data = PEN_STS_CTRL_MODE | ADC_CLK_2MHZ | PANEL_VLTG_STB_TIME_1MS;
211 err = tsc2004_write_word_data(ts, cmd, data);
212 if (err < 0)
213 return err;
215 /* Enable x, y, z1 and z2 conversion functions */
216 cmd = TSC2004_CMD1(MEAS_X_Y_Z1_Z2, MODE_12BIT, SWRST_FALSE);
217 err = tsc2004_write_cmd(ts, cmd);
218 if (err < 0)
219 return err;
221 return 0;
224 static void tsc2004_read_values(struct tsc2004 *tsc, struct ts_event *tc)
226 int cmd;
228 /* Read X Measurement */
229 cmd = TSC2004_CMD0(X_REG, PND0_FALSE, READ_REG);
230 tc->x = tsc2004_read_word_data(tsc, cmd);
232 /* Read Y Measurement */
233 cmd = TSC2004_CMD0(Y_REG, PND0_FALSE, READ_REG);
234 tc->y = tsc2004_read_word_data(tsc, cmd);
236 /* Read Z1 Measurement */
237 cmd = TSC2004_CMD0(Z1_REG, PND0_FALSE, READ_REG);
238 tc->z1 = tsc2004_read_word_data(tsc, cmd);
240 /* Read Z2 Measurement */
241 cmd = TSC2004_CMD0(Z2_REG, PND0_FALSE, READ_REG);
242 tc->z2 = tsc2004_read_word_data(tsc, cmd);
245 tc->x &= MEAS_MASK;
246 tc->y &= MEAS_MASK;
247 tc->z1 &= MEAS_MASK;
248 tc->z2 &= MEAS_MASK;
250 /* Prepare for touch readings */
251 if (tsc2004_prepare_for_reading(tsc) < 0)
252 dev_dbg(&tsc->client->dev, "Failed to prepare TSC for next"
253 "reading\n");
256 static u32 tsc2004_calculate_pressure(struct tsc2004 *tsc, struct ts_event *tc)
258 u32 rt = 0;
260 /* range filtering */
261 if (tc->x == MAX_12BIT)
262 tc->x = 0;
264 if (likely(tc->x && tc->z1)) {
265 /* compute touch pressure resistance using equation #1 */
266 rt = tc->z2 - tc->z1;
267 rt *= tc->x;
268 rt *= tsc->x_plate_ohms;
269 rt /= tc->z1;
270 rt = (rt + 2047) >> 12;
273 return rt;
276 static void tsc2004_send_up_event(struct tsc2004 *tsc)
278 struct input_dev *input = tsc->input;
280 dev_dbg(&tsc->client->dev, "UP\n");
282 input_report_key(input, BTN_TOUCH, 0);
283 input_report_abs(input, ABS_PRESSURE, 0);
284 input_sync(input);
287 static void tsc2004_work(struct work_struct *work)
289 struct tsc2004 *ts =
290 container_of(to_delayed_work(work), struct tsc2004, work);
291 struct ts_event tc;
292 u32 rt;
295 * NOTE: We can't rely on the pressure to determine the pen down
296 * state, even though this controller has a pressure sensor.
297 * The pressure value can fluctuate for quite a while after
298 * lifting the pen and in some cases may not even settle at the
299 * expected value.
301 * The only safe way to check for the pen up condition is in the
302 * work function by reading the pen signal state (it's a GPIO
303 * and IRQ). Unfortunately such callback is not always available,
304 * in that case we have rely on the pressure anyway.
306 if (ts->get_pendown_state) {
307 if (unlikely(!ts->get_pendown_state())) {
308 tsc2004_send_up_event(ts);
309 ts->pendown = false;
310 goto out;
313 dev_dbg(&ts->client->dev, "pen is still down\n");
316 tsc2004_read_values(ts, &tc);
318 rt = tsc2004_calculate_pressure(ts, &tc);
319 if (rt > MAX_12BIT) {
321 * Sample found inconsistent by debouncing or pressure is
322 * beyond the maximum. Don't report it to user space,
323 * repeat at least once more the measurement.
325 dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt);
326 goto out;
330 if (rt) {
331 struct input_dev *input = ts->input;
333 if (!ts->pendown) {
334 dev_dbg(&ts->client->dev, "DOWN\n");
336 input_report_key(input, BTN_TOUCH, 1);
337 ts->pendown = true;
340 input_report_abs(input, ABS_X, tc.x);
341 input_report_abs(input, ABS_Y, tc.y);
342 input_report_abs(input, ABS_PRESSURE, rt);
344 input_sync(input);
346 dev_dbg(&ts->client->dev, "point(%4d,%4d), pressure (%4u)\n",
347 tc.x, tc.y, rt);
349 } else if (!ts->get_pendown_state && ts->pendown) {
351 * We don't have callback to check pendown state, so we
352 * have to assume that since pressure reported is 0 the
353 * pen was lifted up.
355 tsc2004_send_up_event(ts);
356 ts->pendown = false;
359 out:
360 if (ts->pendown)
361 schedule_delayed_work(&ts->work,
362 msecs_to_jiffies(TS_POLL_PERIOD));
363 else
364 enable_irq(ts->irq);
367 static irqreturn_t tsc2004_irq(int irq, void *handle)
369 struct tsc2004 *ts = handle;
371 if (!ts->get_pendown_state || likely(ts->get_pendown_state())) {
372 disable_irq_nosync(ts->irq);
373 schedule_delayed_work(&ts->work,
374 msecs_to_jiffies(TS_POLL_DELAY));
377 if (ts->clear_penirq)
378 ts->clear_penirq();
380 return IRQ_HANDLED;
383 static void tsc2004_free_irq(struct tsc2004 *ts)
385 free_irq(ts->irq, ts);
386 if (cancel_delayed_work_sync(&ts->work)) {
388 * Work was pending, therefore we need to enable
389 * IRQ here to balance the disable_irq() done in the
390 * interrupt handler.
392 enable_irq(ts->irq);
396 static int __devinit tsc2004_probe(struct i2c_client *client,
397 const struct i2c_device_id *id)
399 struct tsc2004 *ts;
400 struct tsc2004_platform_data *pdata = pdata = client->dev.platform_data;
401 struct input_dev *input_dev;
402 int err;
404 if (!pdata) {
405 dev_err(&client->dev, "platform data is required!\n");
406 return -EINVAL;
409 if (!i2c_check_functionality(client->adapter,
410 I2C_FUNC_SMBUS_READ_WORD_DATA))
411 return -EIO;
413 ts = kzalloc(sizeof(struct tsc2004), GFP_KERNEL);
414 input_dev = input_allocate_device();
415 if (!ts || !input_dev) {
416 err = -ENOMEM;
417 goto err_free_mem;
420 ts->client = client;
421 ts->irq = client->irq;
422 ts->input = input_dev;
423 INIT_DELAYED_WORK(&ts->work, tsc2004_work);
425 ts->model = pdata->model;
426 ts->x_plate_ohms = pdata->x_plate_ohms;
427 ts->get_pendown_state = pdata->get_pendown_state;
428 ts->clear_penirq = pdata->clear_penirq;
430 snprintf(ts->phys, sizeof(ts->phys),
431 "%s/input0", dev_name(&client->dev));
433 input_dev->name = "TSC2004 Touchscreen";
434 input_dev->phys = ts->phys;
435 input_dev->id.bustype = BUS_I2C;
437 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
438 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
440 input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
441 input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
442 input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
444 if (pdata->init_platform_hw)
445 pdata->init_platform_hw();
447 err = request_irq(ts->irq, tsc2004_irq, IRQF_TRIGGER_FALLING,
448 client->dev.driver->name, ts);
449 if (err < 0) {
450 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
451 goto err_free_mem;
454 /* Prepare for touch readings */
455 err = tsc2004_prepare_for_reading(ts);
456 if (err < 0)
457 goto err_free_irq;
459 err = input_register_device(input_dev);
460 if (err)
461 goto err_free_irq;
463 i2c_set_clientdata(client, ts);
465 return 0;
467 err_free_irq:
468 tsc2004_free_irq(ts);
469 if (pdata->exit_platform_hw)
470 pdata->exit_platform_hw();
471 err_free_mem:
472 input_free_device(input_dev);
473 kfree(ts);
474 return err;
477 static int __devexit tsc2004_remove(struct i2c_client *client)
479 struct tsc2004 *ts = i2c_get_clientdata(client);
480 struct tsc2004_platform_data *pdata = client->dev.platform_data;
482 tsc2004_free_irq(ts);
484 if (pdata->exit_platform_hw)
485 pdata->exit_platform_hw();
487 input_unregister_device(ts->input);
488 kfree(ts);
490 return 0;
493 static struct i2c_device_id tsc2004_idtable[] = {
494 { "tsc2004", 0 },
498 MODULE_DEVICE_TABLE(i2c, tsc2004_idtable);
500 static struct i2c_driver tsc2004_driver = {
501 .driver = {
502 .owner = THIS_MODULE,
503 .name = "tsc2004"
505 .id_table = tsc2004_idtable,
506 .probe = tsc2004_probe,
507 .remove = __devexit_p(tsc2004_remove),
510 static int __init tsc2004_init(void)
512 return i2c_add_driver(&tsc2004_driver);
515 static void __exit tsc2004_exit(void)
517 i2c_del_driver(&tsc2004_driver);
520 module_init(tsc2004_init);
521 module_exit(tsc2004_exit);
523 MODULE_AUTHOR("Vaibhav Hiremath <hvaibhav@ti.com>");
524 MODULE_DESCRIPTION("TSC2004 TouchScreen Driver");
525 MODULE_LICENSE("GPL");