2 * clearpad_tm1217.c - Touch Screen driver for Synaptics Clearpad
5 * Copyright (C) 2008 Intel Corp
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; ifnot, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * Questions/Comments/Bug fixes to Ramesh Agarwal (ramesh.agarwal@intel.com)
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/input.h>
29 #include <linux/interrupt.h>
31 #include <linux/i2c.h>
32 #include <linux/timer.h>
33 #include <linux/gpio.h>
34 #include <linux/hrtimer.h>
35 #include <linux/kthread.h>
36 #include <linux/delay.h>
37 #include <linux/slab.h>
38 #include "cp_tm1217.h"
40 #define CPTM1217_DEVICE_NAME "cptm1217"
41 #define CPTM1217_DRIVER_NAME CPTM1217_DEVICE_NAME
43 #define MAX_TOUCH_SUPPORTED 2
44 #define TOUCH_SUPPORTED 1
45 #define SAMPLING_FREQ 80 /* Frequency in HZ */
46 #define DELAY_BTWIN_SAMPLE (1000 / SAMPLING_FREQ)
47 #define WAIT_FOR_RESPONSE 5 /* 5msec just works */
48 #define MAX_RETRIES 5 /* As above */
49 #define INCREMENTAL_DELAY 5 /* As above */
51 /* Regster Definitions */
52 #define TMA1217_DEV_STATUS 0x13 /* Device Status */
53 #define TMA1217_INT_STATUS 0x14 /* Interrupt Status */
55 /* Controller can detect up to 2 possible finger touches.
56 * Each finger touch provides 12 bit X Y co-ordinates, the values are split
57 * across 2 registers, and an 8 bit Z value */
58 #define TMA1217_FINGER_STATE 0x18 /* Finger State */
59 #define TMA1217_FINGER1_X_HIGHER8 0x19 /* Higher 8 bit of X coordinate */
60 #define TMA1217_FINGER1_Y_HIGHER8 0x1A /* Higher 8 bit of Y coordinate */
61 #define TMA1217_FINGER1_XY_LOWER4 0x1B /* Lower 4 bits of X and Y */
62 #define TMA1217_FINGER1_Z_VALUE 0x1D /* 8 bit Z value for finger 1 */
63 #define TMA1217_FINGER2_X_HIGHER8 0x1E /* Higher 8 bit of X coordinate */
64 #define TMA1217_FINGER2_Y_HIGHER8 0x1F /* Higher 8 bit of Y coordinate */
65 #define TMA1217_FINGER2_XY_LOWER4 0x20 /* Lower 4 bits of X and Y */
66 #define TMA1217_FINGER2_Z_VALUE 0x22 /* 8 bit Z value for finger 2 */
67 #define TMA1217_DEVICE_CTRL 0x23 /* Device Control */
68 #define TMA1217_INTERRUPT_ENABLE 0x24 /* Interrupt Enable */
69 #define TMA1217_REPORT_MODE 0x2B /* Reporting Mode */
70 #define TMA1217_MAX_X_LOWER8 0x31 /* Bit 0-7 for Max X */
71 #define TMA1217_MAX_X_HIGHER4 0x32 /* Bit 8-11 for Max X */
72 #define TMA1217_MAX_Y_LOWER8 0x33 /* Bit 0-7 for Max Y */
73 #define TMA1217_MAX_Y_HIGHER4 0x34 /* Bit 8-11 for Max Y */
74 #define TMA1217_DEVICE_CMD_RESET 0x67 /* Device CMD reg for reset */
75 #define TMA1217_DEVICE_CMD_REZERO 0x69 /* Device CMD reg for rezero */
77 #define TMA1217_MANUFACTURER_ID 0x73 /* Manufacturer Id */
78 #define TMA1217_PRODUCT_FAMILY 0x75 /* Product Family */
79 #define TMA1217_FIRMWARE_REVISION 0x76 /* Firmware Revision */
80 #define TMA1217_SERIAL_NO_HIGH 0x7C /* Bit 8-15 of device serial no. */
81 #define TMA1217_SERIAL_NO_LOW 0x7D /* Bit 0-7 of device serial no. */
82 #define TMA1217_PRODUCT_ID_START 0x7E /* Start address for 10 byte ID */
83 #define TMA1217_DEVICE_CAPABILITY 0x8B /* Reporting capability */
87 * The touch position structure.
95 /* Device Specific info given by the controller */
101 /* Vendor related info given by the controller */
102 struct cp_vendor_info
{
110 * Private structure to store the device details
112 struct cp_tm1217_device
{
113 struct i2c_client
*client
;
115 struct cp_vendor_info vinfo
;
116 struct cp_dev_info dinfo
;
117 struct input_dev_info
{
120 struct input_dev
*input
;
121 struct touch_state touch
;
122 } cp_input_info
[MAX_TOUCH_SUPPORTED
];
125 struct mutex thread_mutex
;
131 /* The following functions are used to read/write registers on the device
132 * as per the RMI prorocol. Technically, a page select should be written
133 * before doing read/write but since the register offsets are below 0xFF
134 * we can use the default value of page which is 0x00
136 static int cp_tm1217_read(struct cp_tm1217_device
*ts
,
141 /* Send the address */
142 retval
= i2c_master_send(ts
->client
, &req
[0], 1);
144 dev_err(ts
->dev
, "cp_tm1217: I2C send failed\n");
147 msleep(WAIT_FOR_RESPONSE
);
148 for (i
= 0; i
< MAX_RETRIES
; i
++) {
149 retval
= i2c_master_recv(ts
->client
, &req
[1], size
);
150 if (retval
== size
) {
153 msleep(INCREMENTAL_DELAY
);
154 dev_dbg(ts
->dev
, "cp_tm1217: Retry count is %d\n", i
);
158 dev_err(ts
->dev
, "cp_tm1217: Read from device failed\n");
163 static int cp_tm1217_write(struct cp_tm1217_device
*ts
,
168 /* Send the address and the data to be written */
169 retval
= i2c_master_send(ts
->client
, &req
[0], size
+ 1);
170 if (retval
!= size
+ 1) {
171 dev_err(ts
->dev
, "cp_tm1217: I2C write failed: %d\n", retval
);
174 /* Wait for the write to complete. TBD why this is required */
175 msleep(WAIT_FOR_RESPONSE
);
180 static int cp_tm1217_mask_interrupt(struct cp_tm1217_device
*ts
)
185 req
[0] = TMA1217_INTERRUPT_ENABLE
;
187 retval
= cp_tm1217_write(ts
, req
, 1);
194 static int cp_tm1217_unmask_interrupt(struct cp_tm1217_device
*ts
)
199 req
[0] = TMA1217_INTERRUPT_ENABLE
;
201 retval
= cp_tm1217_write(ts
, req
, 1);
208 static void process_touch(struct cp_tm1217_device
*ts
, int index
)
211 struct input_dev_info
*input_info
=
212 (struct input_dev_info
*)&ts
->cp_input_info
[index
];
216 xy_data
[0] = TMA1217_FINGER1_X_HIGHER8
;
218 xy_data
[0] = TMA1217_FINGER2_X_HIGHER8
;
220 retval
= cp_tm1217_read(ts
, xy_data
, 5);
222 dev_err(ts
->dev
, "cp_tm1217: XY read from device failed\n");
226 /* Note: Currently not using the Z values but may be requried in
228 input_info
->touch
.x
= (xy_data
[1] << 4)
229 | (xy_data
[3] & 0x0F);
230 input_info
->touch
.y
= (xy_data
[2] << 4)
231 | ((xy_data
[3] & 0xF0) >> 4);
232 input_report_abs(input_info
->input
, ABS_X
, input_info
->touch
.x
);
233 input_report_abs(input_info
->input
, ABS_Y
, input_info
->touch
.y
);
234 input_sync(input_info
->input
);
237 static void cp_tm1217_get_data(struct cp_tm1217_device
*ts
)
240 int retval
, i
, finger_touched
= 0;
243 req
[0] = TMA1217_FINGER_STATE
;
244 retval
= cp_tm1217_read(ts
, req
, 1);
247 "cp_tm1217: Read from device failed\n");
251 /* Start sampling until the pressure is below
253 for (i
= 0; i
< TOUCH_SUPPORTED
; i
++) {
256 if (ts
->cp_input_info
[i
].touch
.button
== 0) {
257 /* send the button touch event */
259 ts
->cp_input_info
[i
].input
,
261 ts
->cp_input_info
[i
].touch
.button
= 1;
263 process_touch(ts
, i
);
265 if (ts
->cp_input_info
[i
].touch
.button
== 1) {
266 /* send the button release event */
268 ts
->cp_input_info
[i
].input
,
270 input_sync(ts
->cp_input_info
[i
].input
);
271 ts
->cp_input_info
[i
].touch
.button
= 0;
274 req
[1] = req
[1] >> 2;
276 msleep(DELAY_BTWIN_SAMPLE
);
277 } while (finger_touched
> 0);
280 static irqreturn_t
cp_tm1217_sample_thread(int irq
, void *handle
)
282 struct cp_tm1217_device
*ts
= (struct cp_tm1217_device
*) handle
;
286 /* Chedk if another thread is already running */
287 mutex_lock(&ts
->thread_mutex
);
288 if (ts
->thread_running
== 1) {
289 mutex_unlock(&ts
->thread_mutex
);
292 ts
->thread_running
= 1;
293 mutex_unlock(&ts
->thread_mutex
);
296 /* Mask the interrupts */
297 retval
= cp_tm1217_mask_interrupt(ts
);
299 /* Read the Interrupt Status register to find the cause of the
301 req
[0] = TMA1217_INT_STATUS
;
302 retval
= cp_tm1217_read(ts
, req
, 1);
309 cp_tm1217_get_data(ts
);
312 /* Unmask the interrupts before going to sleep */
313 retval
= cp_tm1217_unmask_interrupt(ts
);
315 mutex_lock(&ts
->thread_mutex
);
316 ts
->thread_running
= 0;
317 mutex_unlock(&ts
->thread_mutex
);
322 static int cp_tm1217_init_data(struct cp_tm1217_device
*ts
)
327 /* Read the vendor id/ fw revision etc. Ignoring return check as this
328 is non critical info */
329 req
[0] = TMA1217_MANUFACTURER_ID
;
330 retval
= cp_tm1217_read(ts
, req
, 1);
331 ts
->vinfo
.vendor_id
= req
[1];
333 req
[0] = TMA1217_PRODUCT_FAMILY
;
334 retval
= cp_tm1217_read(ts
, req
, 1);
335 ts
->vinfo
.product_family
= req
[1];
337 req
[0] = TMA1217_FIRMWARE_REVISION
;
338 retval
= cp_tm1217_read(ts
, req
, 1);
339 ts
->vinfo
.firmware_rev
= req
[1];
341 req
[0] = TMA1217_SERIAL_NO_HIGH
;
342 retval
= cp_tm1217_read(ts
, req
, 1);
343 ts
->vinfo
.serial_no
= (req
[1] << 8);
345 req
[0] = TMA1217_SERIAL_NO_LOW
;
346 retval
= cp_tm1217_read(ts
, req
, 1);
347 ts
->vinfo
.serial_no
= ts
->vinfo
.serial_no
| req
[1];
349 req
[0] = TMA1217_MAX_X_HIGHER4
;
350 retval
= cp_tm1217_read(ts
, req
, 1);
351 ts
->dinfo
.maxX
= (req
[1] & 0xF) << 8;
353 req
[0] = TMA1217_MAX_X_LOWER8
;
354 retval
= cp_tm1217_read(ts
, req
, 1);
355 ts
->dinfo
.maxX
= ts
->dinfo
.maxX
| req
[1];
357 req
[0] = TMA1217_MAX_Y_HIGHER4
;
358 retval
= cp_tm1217_read(ts
, req
, 1);
359 ts
->dinfo
.maxY
= (req
[1] & 0xF) << 8;
361 req
[0] = TMA1217_MAX_Y_LOWER8
;
362 retval
= cp_tm1217_read(ts
, req
, 1);
363 ts
->dinfo
.maxY
= ts
->dinfo
.maxY
| req
[1];
370 * Set up a GPIO for use as the interrupt. We can't simply do this at
371 * boot time because the GPIO drivers themselves may not be around at
372 * boot/firmware set up time to do the work. Instead defer it to driver
376 static int cp_tm1217_setup_gpio_irq(struct cp_tm1217_device
*ts
)
380 /* Hook up the irq handler */
381 retval
= gpio_request(ts
->gpio
, "cp_tm1217_touch");
383 dev_err(ts
->dev
, "cp_tm1217: GPIO request failed error %d\n",
388 retval
= gpio_direction_input(ts
->gpio
);
391 "cp_tm1217: GPIO direction configuration failed, error %d\n",
397 retval
= gpio_to_irq(ts
->gpio
);
400 "cp_tm1217: GPIO to IRQ failed, error %d\n", retval
);
404 "cp_tm1217: Got IRQ number is %d for GPIO %d\n",
409 static int cp_tm1217_probe(struct i2c_client
*client
,
410 const struct i2c_device_id
*id
)
412 struct cp_tm1217_device
*ts
;
413 struct input_dev
*input_dev
;
414 struct input_dev_info
*input_info
;
415 struct cp_tm1217_platform_data
*pdata
;
419 /* No pdata is fine - we then use "normal" IRQ mode */
421 pdata
= client
->dev
.platform_data
;
423 ts
= kzalloc(sizeof(struct cp_tm1217_device
), GFP_KERNEL
);
428 ts
->dev
= &client
->dev
;
429 i2c_set_clientdata(client
, ts
);
431 ts
->thread_running
= 0;
432 mutex_init(&ts
->thread_mutex
);
434 /* Reset the Controller */
435 req
[0] = TMA1217_DEVICE_CMD_RESET
;
437 retval
= cp_tm1217_write(ts
, req
, 1);
439 dev_err(ts
->dev
, "cp_tm1217: Controller reset failed\n");
444 /* Clear up the interrupt status from reset. */
445 req
[0] = TMA1217_INT_STATUS
;
446 retval
= cp_tm1217_read(ts
, req
, 1);
448 /* Mask all the interrupts */
449 retval
= cp_tm1217_mask_interrupt(ts
);
451 /* Read the controller information */
452 cp_tm1217_init_data(ts
);
454 /* The following code will register multiple event devices when
455 multi-pointer is enabled, the code has not been tested
457 for (i
= 0; i
< TOUCH_SUPPORTED
; i
++) {
458 input_dev
= input_allocate_device();
459 if (input_dev
== NULL
) {
463 input_info
= &ts
->cp_input_info
[i
];
464 snprintf(input_info
->name
, sizeof(input_info
->name
),
465 "cp_tm1217_touchscreen_%d", i
);
466 input_dev
->name
= input_info
->name
;
467 snprintf(input_info
->phys
, sizeof(input_info
->phys
),
468 "%s/input%d", dev_name(&client
->dev
), i
);
470 input_dev
->phys
= input_info
->phys
;
471 input_dev
->id
.bustype
= BUS_I2C
;
473 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
474 input_dev
->keybit
[BIT_WORD(BTN_TOUCH
)] = BIT_MASK(BTN_TOUCH
);
476 input_set_abs_params(input_dev
, ABS_X
, 0, ts
->dinfo
.maxX
, 0, 0);
477 input_set_abs_params(input_dev
, ABS_Y
, 0, ts
->dinfo
.maxY
, 0, 0);
479 retval
= input_register_device(input_dev
);
482 "Input dev registration failed for %s\n",
484 input_free_device(input_dev
);
487 input_info
->input
= input_dev
;
490 /* Setup the reporting mode to send an interrupt only when
491 finger arrives or departs. */
492 req
[0] = TMA1217_REPORT_MODE
;
494 retval
= cp_tm1217_write(ts
, req
, 1);
496 /* Setup the device to no sleep mode for now and make it configured */
497 req
[0] = TMA1217_DEVICE_CTRL
;
499 retval
= cp_tm1217_write(ts
, req
, 1);
501 /* Check for the status of the device */
502 req
[0] = TMA1217_DEV_STATUS
;
503 retval
= cp_tm1217_read(ts
, req
, 1);
506 "cp_tm1217: Device Status 0x%x != 0: config failed\n",
513 if (pdata
&& pdata
->gpio
) {
514 ts
->gpio
= pdata
->gpio
;
515 retval
= cp_tm1217_setup_gpio_irq(ts
);
517 retval
= client
->irq
;
520 dev_err(ts
->dev
, "cp_tm1217: GPIO request failed error %d\n",
525 client
->irq
= retval
;
528 retval
= request_threaded_irq(client
->irq
,
529 NULL
, cp_tm1217_sample_thread
,
530 IRQF_TRIGGER_FALLING
, "cp_tm1217_touch", ts
);
532 dev_err(ts
->dev
, "cp_tm1217: Request IRQ error %d\n", retval
);
536 /* Unmask the interrupts */
537 retval
= cp_tm1217_unmask_interrupt(ts
);
541 free_irq(client
->irq
, ts
);
546 /* Clean up before returning failure */
547 for (i
= 0; i
< TOUCH_SUPPORTED
; i
++) {
548 if (ts
->cp_input_info
[i
].input
)
549 input_unregister_device(ts
->cp_input_info
[i
].input
);
556 #ifdef CONFIG_PM_SLEEP
562 static int cp_tm1217_suspend(struct device
*dev
)
564 struct i2c_client
*client
= to_i2c_client(dev
);
565 struct cp_tm1217_device
*ts
= i2c_get_clientdata(client
);
569 /* Put the controller to sleep */
570 req
[0] = TMA1217_DEVICE_CTRL
;
571 retval
= cp_tm1217_read(ts
, req
, 1);
572 req
[1] = (req
[1] & 0xF8) | 0x1;
573 retval
= cp_tm1217_write(ts
, req
, 1);
575 if (device_may_wakeup(&client
->dev
))
576 enable_irq_wake(client
->irq
);
585 static int cp_tm1217_resume(struct device
*dev
)
587 struct i2c_client
*client
= to_i2c_client(dev
);
588 struct cp_tm1217_device
*ts
= i2c_get_clientdata(client
);
592 /* Take the controller out of sleep */
593 req
[0] = TMA1217_DEVICE_CTRL
;
594 retval
= cp_tm1217_read(ts
, req
, 1);
595 req
[1] = (req
[1] & 0xF8) | 0x4;
596 retval
= cp_tm1217_write(ts
, req
, 1);
598 /* Restore the register settings sinc the power to the
599 could have been cut off */
601 /* Setup the reporting mode to send an interrupt only when
602 finger arrives or departs. */
603 req
[0] = TMA1217_REPORT_MODE
;
605 retval
= cp_tm1217_write(ts
, req
, 1);
607 /* Setup the device to no sleep mode for now and make it configured */
608 req
[0] = TMA1217_DEVICE_CTRL
;
610 retval
= cp_tm1217_write(ts
, req
, 1);
612 /* Setup the interrupt mask */
613 retval
= cp_tm1217_unmask_interrupt(ts
);
615 if (device_may_wakeup(&client
->dev
))
616 disable_irq_wake(client
->irq
);
623 static SIMPLE_DEV_PM_OPS(cp_tm1217_pm_ops
, cp_tm1217_suspend
,
630 static int cp_tm1217_remove(struct i2c_client
*client
)
632 struct cp_tm1217_device
*ts
= i2c_get_clientdata(client
);
635 free_irq(client
->irq
, ts
);
638 for (i
= 0; i
< TOUCH_SUPPORTED
; i
++)
639 input_unregister_device(ts
->cp_input_info
[i
].input
);
644 static struct i2c_device_id cp_tm1217_idtable
[] = {
645 { CPTM1217_DEVICE_NAME
, 0 },
649 MODULE_DEVICE_TABLE(i2c
, cp_tm1217_idtable
);
651 static struct i2c_driver cp_tm1217_driver
= {
653 .owner
= THIS_MODULE
,
654 .name
= CPTM1217_DRIVER_NAME
,
655 .pm
= &cp_tm1217_pm_ops
,
657 .id_table
= cp_tm1217_idtable
,
658 .probe
= cp_tm1217_probe
,
659 .remove
= cp_tm1217_remove
,
662 module_i2c_driver(cp_tm1217_driver
);
664 MODULE_AUTHOR("Ramesh Agarwal <ramesh.agarwal@intel.com>");
665 MODULE_DESCRIPTION("Synaptics TM1217 TouchScreen Driver");
666 MODULE_LICENSE("GPL v2");