Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * clearpad_tm1217.c - Touch Screen driver for Synaptics Clearpad |
| 3 | * TM1217 controller |
| 4 | * |
| 5 | * Copyright (C) 2008 Intel Corp |
| 6 | * |
| 7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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. |
| 21 | * |
| 22 | * Questions/Comments/Bug fixes to Ramesh Agarwal (ramesh.agarwal@intel.com) |
| 23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 | */ |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/input.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/io.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" |
| 39 | |
| 40 | #define CPTM1217_DEVICE_NAME "cptm1217" |
| 41 | #define CPTM1217_DRIVER_NAME CPTM1217_DEVICE_NAME |
| 42 | |
| 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 */ |
| 50 | |
| 51 | /* Regster Definitions */ |
| 52 | #define TMA1217_DEV_STATUS 0x13 /* Device Status */ |
| 53 | #define TMA1217_INT_STATUS 0x14 /* Interrupt Status */ |
| 54 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 55 | /* Controller can detect up to 2 possible finger touches. |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 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 */ |
| 76 | |
| 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 */ |
| 84 | |
| 85 | |
| 86 | /* |
| 87 | * The touch position structure. |
| 88 | */ |
| 89 | struct touch_state { |
| 90 | int x; |
| 91 | int y; |
| 92 | bool button; |
| 93 | }; |
| 94 | |
| 95 | /* Device Specific info given by the controller */ |
| 96 | struct cp_dev_info { |
| 97 | u16 maxX; |
| 98 | u16 maxY; |
| 99 | }; |
| 100 | |
| 101 | /* Vendor related info given by the controller */ |
| 102 | struct cp_vendor_info { |
| 103 | u8 vendor_id; |
| 104 | u8 product_family; |
| 105 | u8 firmware_rev; |
| 106 | u16 serial_no; |
| 107 | }; |
| 108 | |
| 109 | /* |
| 110 | * Private structure to store the device details |
| 111 | */ |
| 112 | struct cp_tm1217_device { |
| 113 | struct i2c_client *client; |
| 114 | struct device *dev; |
| 115 | struct cp_vendor_info vinfo; |
| 116 | struct cp_dev_info dinfo; |
| 117 | struct input_dev_info { |
| 118 | char phys[32]; |
| 119 | char name[128]; |
| 120 | struct input_dev *input; |
| 121 | struct touch_state touch; |
| 122 | } cp_input_info[MAX_TOUCH_SUPPORTED]; |
| 123 | |
| 124 | int thread_running; |
| 125 | struct mutex thread_mutex; |
| 126 | |
| 127 | int gpio; |
| 128 | }; |
| 129 | |
| 130 | |
| 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 |
| 135 | */ |
| 136 | static int cp_tm1217_read(struct cp_tm1217_device *ts, |
| 137 | u8 *req, int size) |
| 138 | { |
| 139 | int i, retval; |
| 140 | |
| 141 | /* Send the address */ |
| 142 | retval = i2c_master_send(ts->client, &req[0], 1); |
| 143 | if (retval != 1) { |
| 144 | dev_err(ts->dev, "cp_tm1217: I2C send failed\n"); |
| 145 | return retval; |
| 146 | } |
| 147 | msleep(WAIT_FOR_RESPONSE); |
| 148 | for (i = 0; i < MAX_RETRIES; i++) { |
| 149 | retval = i2c_master_recv(ts->client, &req[1], size); |
Evgeny Budilovsky | 55a4ea7 | 2014-06-20 11:22:14 +0300 | [diff] [blame] | 150 | if (retval == size) |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 151 | break; |
Evgeny Budilovsky | 55a4ea7 | 2014-06-20 11:22:14 +0300 | [diff] [blame] | 152 | |
| 153 | msleep(INCREMENTAL_DELAY); |
| 154 | dev_dbg(ts->dev, "cp_tm1217: Retry count is %d\n", i); |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 155 | } |
| 156 | if (retval != size) |
| 157 | dev_err(ts->dev, "cp_tm1217: Read from device failed\n"); |
| 158 | |
| 159 | return retval; |
| 160 | } |
| 161 | |
| 162 | static int cp_tm1217_write(struct cp_tm1217_device *ts, |
| 163 | u8 *req, int size) |
| 164 | { |
| 165 | int retval; |
| 166 | |
| 167 | /* Send the address and the data to be written */ |
| 168 | retval = i2c_master_send(ts->client, &req[0], size + 1); |
| 169 | if (retval != size + 1) { |
| 170 | dev_err(ts->dev, "cp_tm1217: I2C write failed: %d\n", retval); |
| 171 | return retval; |
| 172 | } |
| 173 | /* Wait for the write to complete. TBD why this is required */ |
| 174 | msleep(WAIT_FOR_RESPONSE); |
| 175 | |
| 176 | return size; |
| 177 | } |
| 178 | |
| 179 | static int cp_tm1217_mask_interrupt(struct cp_tm1217_device *ts) |
| 180 | { |
| 181 | u8 req[2]; |
| 182 | int retval; |
| 183 | |
| 184 | req[0] = TMA1217_INTERRUPT_ENABLE; |
| 185 | req[1] = 0x0; |
| 186 | retval = cp_tm1217_write(ts, req, 1); |
| 187 | if (retval != 1) |
| 188 | return -EIO; |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int cp_tm1217_unmask_interrupt(struct cp_tm1217_device *ts) |
| 194 | { |
| 195 | u8 req[2]; |
| 196 | int retval; |
| 197 | |
| 198 | req[0] = TMA1217_INTERRUPT_ENABLE; |
| 199 | req[1] = 0xa; |
| 200 | retval = cp_tm1217_write(ts, req, 1); |
| 201 | if (retval != 1) |
| 202 | return -EIO; |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static void process_touch(struct cp_tm1217_device *ts, int index) |
| 208 | { |
| 209 | int retval; |
| 210 | struct input_dev_info *input_info = |
| 211 | (struct input_dev_info *)&ts->cp_input_info[index]; |
| 212 | u8 xy_data[6]; |
| 213 | |
| 214 | if (index == 0) |
| 215 | xy_data[0] = TMA1217_FINGER1_X_HIGHER8; |
| 216 | else |
| 217 | xy_data[0] = TMA1217_FINGER2_X_HIGHER8; |
| 218 | |
| 219 | retval = cp_tm1217_read(ts, xy_data, 5); |
| 220 | if (retval < 5) { |
| 221 | dev_err(ts->dev, "cp_tm1217: XY read from device failed\n"); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | /* Note: Currently not using the Z values but may be requried in |
| 226 | the future. */ |
| 227 | input_info->touch.x = (xy_data[1] << 4) |
| 228 | | (xy_data[3] & 0x0F); |
| 229 | input_info->touch.y = (xy_data[2] << 4) |
| 230 | | ((xy_data[3] & 0xF0) >> 4); |
| 231 | input_report_abs(input_info->input, ABS_X, input_info->touch.x); |
| 232 | input_report_abs(input_info->input, ABS_Y, input_info->touch.y); |
| 233 | input_sync(input_info->input); |
| 234 | } |
| 235 | |
| 236 | static void cp_tm1217_get_data(struct cp_tm1217_device *ts) |
| 237 | { |
| 238 | u8 req[2]; |
| 239 | int retval, i, finger_touched = 0; |
| 240 | |
| 241 | do { |
| 242 | req[0] = TMA1217_FINGER_STATE; |
| 243 | retval = cp_tm1217_read(ts, req, 1); |
| 244 | if (retval != 1) { |
| 245 | dev_err(ts->dev, |
| 246 | "cp_tm1217: Read from device failed\n"); |
| 247 | continue; |
| 248 | } |
| 249 | finger_touched = 0; |
| 250 | /* Start sampling until the pressure is below |
| 251 | threshold */ |
| 252 | for (i = 0; i < TOUCH_SUPPORTED; i++) { |
| 253 | if (req[1] & 0x3) { |
| 254 | finger_touched++; |
| 255 | if (ts->cp_input_info[i].touch.button == 0) { |
| 256 | /* send the button touch event */ |
| 257 | input_report_key( |
| 258 | ts->cp_input_info[i].input, |
| 259 | BTN_TOUCH, 1); |
| 260 | ts->cp_input_info[i].touch.button = 1; |
| 261 | } |
| 262 | process_touch(ts, i); |
| 263 | } else { |
| 264 | if (ts->cp_input_info[i].touch.button == 1) { |
| 265 | /* send the button release event */ |
| 266 | input_report_key( |
| 267 | ts->cp_input_info[i].input, |
| 268 | BTN_TOUCH, 0); |
| 269 | input_sync(ts->cp_input_info[i].input); |
| 270 | ts->cp_input_info[i].touch.button = 0; |
| 271 | } |
| 272 | } |
| 273 | req[1] = req[1] >> 2; |
| 274 | } |
| 275 | msleep(DELAY_BTWIN_SAMPLE); |
| 276 | } while (finger_touched > 0); |
| 277 | } |
| 278 | |
| 279 | static irqreturn_t cp_tm1217_sample_thread(int irq, void *handle) |
| 280 | { |
| 281 | struct cp_tm1217_device *ts = (struct cp_tm1217_device *) handle; |
| 282 | u8 req[2]; |
| 283 | int retval; |
| 284 | |
| 285 | /* Chedk if another thread is already running */ |
| 286 | mutex_lock(&ts->thread_mutex); |
| 287 | if (ts->thread_running == 1) { |
| 288 | mutex_unlock(&ts->thread_mutex); |
| 289 | return IRQ_HANDLED; |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Evgeny Budilovsky | 55a4ea7 | 2014-06-20 11:22:14 +0300 | [diff] [blame] | 292 | ts->thread_running = 1; |
| 293 | mutex_unlock(&ts->thread_mutex); |
| 294 | |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 295 | /* Mask the interrupts */ |
| 296 | retval = cp_tm1217_mask_interrupt(ts); |
| 297 | |
| 298 | /* Read the Interrupt Status register to find the cause of the |
| 299 | Interrupt */ |
| 300 | req[0] = TMA1217_INT_STATUS; |
| 301 | retval = cp_tm1217_read(ts, req, 1); |
| 302 | if (retval != 1) |
| 303 | goto exit_thread; |
| 304 | |
| 305 | if (!(req[1] & 0x8)) |
| 306 | goto exit_thread; |
| 307 | |
| 308 | cp_tm1217_get_data(ts); |
| 309 | |
| 310 | exit_thread: |
| 311 | /* Unmask the interrupts before going to sleep */ |
| 312 | retval = cp_tm1217_unmask_interrupt(ts); |
| 313 | |
| 314 | mutex_lock(&ts->thread_mutex); |
| 315 | ts->thread_running = 0; |
| 316 | mutex_unlock(&ts->thread_mutex); |
| 317 | |
| 318 | return IRQ_HANDLED; |
| 319 | } |
| 320 | |
| 321 | static int cp_tm1217_init_data(struct cp_tm1217_device *ts) |
| 322 | { |
| 323 | int retval; |
| 324 | u8 req[2]; |
| 325 | |
| 326 | /* Read the vendor id/ fw revision etc. Ignoring return check as this |
| 327 | is non critical info */ |
| 328 | req[0] = TMA1217_MANUFACTURER_ID; |
| 329 | retval = cp_tm1217_read(ts, req, 1); |
| 330 | ts->vinfo.vendor_id = req[1]; |
| 331 | |
| 332 | req[0] = TMA1217_PRODUCT_FAMILY; |
| 333 | retval = cp_tm1217_read(ts, req, 1); |
| 334 | ts->vinfo.product_family = req[1]; |
| 335 | |
| 336 | req[0] = TMA1217_FIRMWARE_REVISION; |
| 337 | retval = cp_tm1217_read(ts, req, 1); |
| 338 | ts->vinfo.firmware_rev = req[1]; |
| 339 | |
| 340 | req[0] = TMA1217_SERIAL_NO_HIGH; |
| 341 | retval = cp_tm1217_read(ts, req, 1); |
| 342 | ts->vinfo.serial_no = (req[1] << 8); |
| 343 | |
| 344 | req[0] = TMA1217_SERIAL_NO_LOW; |
| 345 | retval = cp_tm1217_read(ts, req, 1); |
| 346 | ts->vinfo.serial_no = ts->vinfo.serial_no | req[1]; |
| 347 | |
| 348 | req[0] = TMA1217_MAX_X_HIGHER4; |
| 349 | retval = cp_tm1217_read(ts, req, 1); |
| 350 | ts->dinfo.maxX = (req[1] & 0xF) << 8; |
| 351 | |
| 352 | req[0] = TMA1217_MAX_X_LOWER8; |
| 353 | retval = cp_tm1217_read(ts, req, 1); |
| 354 | ts->dinfo.maxX = ts->dinfo.maxX | req[1]; |
| 355 | |
| 356 | req[0] = TMA1217_MAX_Y_HIGHER4; |
| 357 | retval = cp_tm1217_read(ts, req, 1); |
| 358 | ts->dinfo.maxY = (req[1] & 0xF) << 8; |
| 359 | |
| 360 | req[0] = TMA1217_MAX_Y_LOWER8; |
| 361 | retval = cp_tm1217_read(ts, req, 1); |
| 362 | ts->dinfo.maxY = ts->dinfo.maxY | req[1]; |
| 363 | |
| 364 | return 0; |
| 365 | |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Set up a GPIO for use as the interrupt. We can't simply do this at |
| 370 | * boot time because the GPIO drivers themselves may not be around at |
| 371 | * boot/firmware set up time to do the work. Instead defer it to driver |
| 372 | * detection. |
| 373 | */ |
| 374 | |
| 375 | static int cp_tm1217_setup_gpio_irq(struct cp_tm1217_device *ts) |
| 376 | { |
| 377 | int retval; |
| 378 | |
| 379 | /* Hook up the irq handler */ |
| 380 | retval = gpio_request(ts->gpio, "cp_tm1217_touch"); |
| 381 | if (retval < 0) { |
| 382 | dev_err(ts->dev, "cp_tm1217: GPIO request failed error %d\n", |
| 383 | retval); |
| 384 | return retval; |
| 385 | } |
| 386 | |
| 387 | retval = gpio_direction_input(ts->gpio); |
| 388 | if (retval < 0) { |
| 389 | dev_err(ts->dev, |
| 390 | "cp_tm1217: GPIO direction configuration failed, error %d\n", |
| 391 | retval); |
| 392 | gpio_free(ts->gpio); |
| 393 | return retval; |
| 394 | } |
| 395 | |
| 396 | retval = gpio_to_irq(ts->gpio); |
| 397 | if (retval < 0) { |
Adnan Ali | 1caba56 | 2012-05-28 16:07:49 +0100 | [diff] [blame] | 398 | dev_err(ts->dev, |
| 399 | "cp_tm1217: GPIO to IRQ failed, error %d\n", retval); |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 400 | gpio_free(ts->gpio); |
| 401 | } |
| 402 | dev_dbg(ts->dev, |
| 403 | "cp_tm1217: Got IRQ number is %d for GPIO %d\n", |
| 404 | retval, ts->gpio); |
| 405 | return retval; |
| 406 | } |
| 407 | |
| 408 | static int cp_tm1217_probe(struct i2c_client *client, |
| 409 | const struct i2c_device_id *id) |
| 410 | { |
| 411 | struct cp_tm1217_device *ts; |
| 412 | struct input_dev *input_dev; |
| 413 | struct input_dev_info *input_info; |
| 414 | struct cp_tm1217_platform_data *pdata; |
| 415 | u8 req[2]; |
| 416 | int i, retval; |
| 417 | |
| 418 | /* No pdata is fine - we then use "normal" IRQ mode */ |
| 419 | |
| 420 | pdata = client->dev.platform_data; |
| 421 | |
| 422 | ts = kzalloc(sizeof(struct cp_tm1217_device), GFP_KERNEL); |
Joe Perches | 78110bb | 2013-02-11 09:41:29 -0800 | [diff] [blame] | 423 | if (!ts) |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 424 | return -ENOMEM; |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 425 | |
| 426 | ts->client = client; |
| 427 | ts->dev = &client->dev; |
| 428 | i2c_set_clientdata(client, ts); |
| 429 | |
| 430 | ts->thread_running = 0; |
| 431 | mutex_init(&ts->thread_mutex); |
| 432 | |
| 433 | /* Reset the Controller */ |
| 434 | req[0] = TMA1217_DEVICE_CMD_RESET; |
| 435 | req[1] = 0x1; |
| 436 | retval = cp_tm1217_write(ts, req, 1); |
| 437 | if (retval != 1) { |
| 438 | dev_err(ts->dev, "cp_tm1217: Controller reset failed\n"); |
| 439 | kfree(ts); |
| 440 | return -EIO; |
| 441 | } |
| 442 | |
| 443 | /* Clear up the interrupt status from reset. */ |
| 444 | req[0] = TMA1217_INT_STATUS; |
| 445 | retval = cp_tm1217_read(ts, req, 1); |
| 446 | |
| 447 | /* Mask all the interrupts */ |
| 448 | retval = cp_tm1217_mask_interrupt(ts); |
| 449 | |
| 450 | /* Read the controller information */ |
| 451 | cp_tm1217_init_data(ts); |
| 452 | |
| 453 | /* The following code will register multiple event devices when |
| 454 | multi-pointer is enabled, the code has not been tested |
| 455 | with MPX */ |
| 456 | for (i = 0; i < TOUCH_SUPPORTED; i++) { |
| 457 | input_dev = input_allocate_device(); |
| 458 | if (input_dev == NULL) { |
Julia Lawall | 311fda8 | 2011-05-18 21:20:43 +0200 | [diff] [blame] | 459 | retval = -ENOMEM; |
| 460 | goto fail; |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 461 | } |
| 462 | input_info = &ts->cp_input_info[i]; |
| 463 | snprintf(input_info->name, sizeof(input_info->name), |
| 464 | "cp_tm1217_touchscreen_%d", i); |
| 465 | input_dev->name = input_info->name; |
| 466 | snprintf(input_info->phys, sizeof(input_info->phys), |
| 467 | "%s/input%d", dev_name(&client->dev), i); |
| 468 | |
| 469 | input_dev->phys = input_info->phys; |
| 470 | input_dev->id.bustype = BUS_I2C; |
| 471 | |
| 472 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 473 | input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
| 474 | |
| 475 | input_set_abs_params(input_dev, ABS_X, 0, ts->dinfo.maxX, 0, 0); |
| 476 | input_set_abs_params(input_dev, ABS_Y, 0, ts->dinfo.maxY, 0, 0); |
| 477 | |
| 478 | retval = input_register_device(input_dev); |
| 479 | if (retval) { |
| 480 | dev_err(ts->dev, |
| 481 | "Input dev registration failed for %s\n", |
| 482 | input_dev->name); |
Julia Lawall | 311fda8 | 2011-05-18 21:20:43 +0200 | [diff] [blame] | 483 | input_free_device(input_dev); |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 484 | goto fail; |
| 485 | } |
| 486 | input_info->input = input_dev; |
| 487 | } |
| 488 | |
| 489 | /* Setup the reporting mode to send an interrupt only when |
| 490 | finger arrives or departs. */ |
| 491 | req[0] = TMA1217_REPORT_MODE; |
| 492 | req[1] = 0x02; |
| 493 | retval = cp_tm1217_write(ts, req, 1); |
| 494 | |
| 495 | /* Setup the device to no sleep mode for now and make it configured */ |
| 496 | req[0] = TMA1217_DEVICE_CTRL; |
| 497 | req[1] = 0x84; |
| 498 | retval = cp_tm1217_write(ts, req, 1); |
| 499 | |
| 500 | /* Check for the status of the device */ |
| 501 | req[0] = TMA1217_DEV_STATUS; |
| 502 | retval = cp_tm1217_read(ts, req, 1); |
| 503 | if (req[1] != 0) { |
| 504 | dev_err(ts->dev, |
| 505 | "cp_tm1217: Device Status 0x%x != 0: config failed\n", |
| 506 | req[1]); |
| 507 | |
| 508 | retval = -EIO; |
| 509 | goto fail; |
| 510 | } |
| 511 | |
| 512 | if (pdata && pdata->gpio) { |
| 513 | ts->gpio = pdata->gpio; |
| 514 | retval = cp_tm1217_setup_gpio_irq(ts); |
| 515 | } else |
| 516 | retval = client->irq; |
| 517 | |
| 518 | if (retval < 0) { |
| 519 | dev_err(ts->dev, "cp_tm1217: GPIO request failed error %d\n", |
| 520 | retval); |
| 521 | goto fail; |
| 522 | } |
| 523 | |
| 524 | client->irq = retval; |
| 525 | |
| 526 | |
| 527 | retval = request_threaded_irq(client->irq, |
| 528 | NULL, cp_tm1217_sample_thread, |
| 529 | IRQF_TRIGGER_FALLING, "cp_tm1217_touch", ts); |
| 530 | if (retval < 0) { |
| 531 | dev_err(ts->dev, "cp_tm1217: Request IRQ error %d\n", retval); |
| 532 | goto fail_gpio; |
| 533 | } |
| 534 | |
| 535 | /* Unmask the interrupts */ |
| 536 | retval = cp_tm1217_unmask_interrupt(ts); |
| 537 | if (retval == 0) |
| 538 | return 0; |
| 539 | |
| 540 | free_irq(client->irq, ts); |
| 541 | fail_gpio: |
| 542 | if (ts->gpio) |
| 543 | gpio_free(ts->gpio); |
| 544 | fail: |
| 545 | /* Clean up before returning failure */ |
| 546 | for (i = 0; i < TOUCH_SUPPORTED; i++) { |
Wei Yongjun | d1d2f83 | 2013-04-24 10:40:00 +0800 | [diff] [blame] | 547 | if (ts->cp_input_info[i].input) |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 548 | input_unregister_device(ts->cp_input_info[i].input); |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 549 | } |
| 550 | kfree(ts); |
| 551 | return retval; |
| 552 | |
| 553 | } |
| 554 | |
Lars-Peter Clausen | bc73488 | 2013-04-08 09:56:07 +0200 | [diff] [blame] | 555 | #ifdef CONFIG_PM_SLEEP |
| 556 | |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 557 | /* |
| 558 | * cp_tm1217 suspend |
| 559 | * |
| 560 | */ |
Lars-Peter Clausen | bc73488 | 2013-04-08 09:56:07 +0200 | [diff] [blame] | 561 | static int cp_tm1217_suspend(struct device *dev) |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 562 | { |
Lars-Peter Clausen | bc73488 | 2013-04-08 09:56:07 +0200 | [diff] [blame] | 563 | struct i2c_client *client = to_i2c_client(dev); |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 564 | struct cp_tm1217_device *ts = i2c_get_clientdata(client); |
| 565 | u8 req[2]; |
| 566 | int retval; |
| 567 | |
| 568 | /* Put the controller to sleep */ |
| 569 | req[0] = TMA1217_DEVICE_CTRL; |
| 570 | retval = cp_tm1217_read(ts, req, 1); |
| 571 | req[1] = (req[1] & 0xF8) | 0x1; |
| 572 | retval = cp_tm1217_write(ts, req, 1); |
| 573 | |
| 574 | if (device_may_wakeup(&client->dev)) |
| 575 | enable_irq_wake(client->irq); |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * cp_tm1217_resume |
| 582 | * |
| 583 | */ |
Lars-Peter Clausen | bc73488 | 2013-04-08 09:56:07 +0200 | [diff] [blame] | 584 | static int cp_tm1217_resume(struct device *dev) |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 585 | { |
Lars-Peter Clausen | bc73488 | 2013-04-08 09:56:07 +0200 | [diff] [blame] | 586 | struct i2c_client *client = to_i2c_client(dev); |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 587 | struct cp_tm1217_device *ts = i2c_get_clientdata(client); |
| 588 | u8 req[2]; |
| 589 | int retval; |
| 590 | |
| 591 | /* Take the controller out of sleep */ |
| 592 | req[0] = TMA1217_DEVICE_CTRL; |
| 593 | retval = cp_tm1217_read(ts, req, 1); |
| 594 | req[1] = (req[1] & 0xF8) | 0x4; |
| 595 | retval = cp_tm1217_write(ts, req, 1); |
| 596 | |
| 597 | /* Restore the register settings sinc the power to the |
| 598 | could have been cut off */ |
| 599 | |
| 600 | /* Setup the reporting mode to send an interrupt only when |
| 601 | finger arrives or departs. */ |
| 602 | req[0] = TMA1217_REPORT_MODE; |
| 603 | req[1] = 0x02; |
| 604 | retval = cp_tm1217_write(ts, req, 1); |
| 605 | |
| 606 | /* Setup the device to no sleep mode for now and make it configured */ |
| 607 | req[0] = TMA1217_DEVICE_CTRL; |
| 608 | req[1] = 0x84; |
| 609 | retval = cp_tm1217_write(ts, req, 1); |
| 610 | |
| 611 | /* Setup the interrupt mask */ |
| 612 | retval = cp_tm1217_unmask_interrupt(ts); |
| 613 | |
| 614 | if (device_may_wakeup(&client->dev)) |
| 615 | disable_irq_wake(client->irq); |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
Lars-Peter Clausen | bc73488 | 2013-04-08 09:56:07 +0200 | [diff] [blame] | 620 | #endif |
| 621 | |
| 622 | static SIMPLE_DEV_PM_OPS(cp_tm1217_pm_ops, cp_tm1217_suspend, |
| 623 | cp_tm1217_resume); |
| 624 | |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 625 | /* |
| 626 | * cp_tm1217_remove |
| 627 | * |
| 628 | */ |
| 629 | static int cp_tm1217_remove(struct i2c_client *client) |
| 630 | { |
| 631 | struct cp_tm1217_device *ts = i2c_get_clientdata(client); |
| 632 | int i; |
| 633 | |
| 634 | free_irq(client->irq, ts); |
| 635 | if (ts->gpio) |
| 636 | gpio_free(ts->gpio); |
| 637 | for (i = 0; i < TOUCH_SUPPORTED; i++) |
| 638 | input_unregister_device(ts->cp_input_info[i].input); |
| 639 | kfree(ts); |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | static struct i2c_device_id cp_tm1217_idtable[] = { |
| 644 | { CPTM1217_DEVICE_NAME, 0 }, |
| 645 | { } |
| 646 | }; |
| 647 | |
| 648 | MODULE_DEVICE_TABLE(i2c, cp_tm1217_idtable); |
| 649 | |
| 650 | static struct i2c_driver cp_tm1217_driver = { |
| 651 | .driver = { |
| 652 | .owner = THIS_MODULE, |
| 653 | .name = CPTM1217_DRIVER_NAME, |
Lars-Peter Clausen | bc73488 | 2013-04-08 09:56:07 +0200 | [diff] [blame] | 654 | .pm = &cp_tm1217_pm_ops, |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 655 | }, |
| 656 | .id_table = cp_tm1217_idtable, |
| 657 | .probe = cp_tm1217_probe, |
| 658 | .remove = cp_tm1217_remove, |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 659 | }; |
| 660 | |
Devendra Naga | c993e43 | 2012-08-26 02:35:47 +0530 | [diff] [blame] | 661 | module_i2c_driver(cp_tm1217_driver); |
Ramesh Agarwal | 78fd115 | 2010-10-22 14:00:20 +0100 | [diff] [blame] | 662 | |
| 663 | MODULE_AUTHOR("Ramesh Agarwal <ramesh.agarwal@intel.com>"); |
| 664 | MODULE_DESCRIPTION("Synaptics TM1217 TouchScreen Driver"); |
| 665 | MODULE_LICENSE("GPL v2"); |