synaptics_i2c_rmi4.c revision 25985edcedea6396277003854657b5f3cb31a628
1/** 2 * 3 * Synaptics Register Mapped Interface (RMI4) I2C Physical Layer Driver. 4 * Copyright (c) 2007-2010, Synaptics Incorporated 5 * 6 * Author: Js HA <js.ha@stericsson.com> for ST-Ericsson 7 * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson 8 * Copyright 2010 (c) ST-Ericsson AB 9 */ 10/* 11 * This file is licensed under the GPL2 license. 12 * 13 *############################################################################# 14 * GPL 15 * 16 * This program is free software; you can redistribute it and/or modify it 17 * under the terms of the GNU General Public License version 2 as published 18 * by the Free Software Foundation. 19 * 20 * This program is distributed in the hope that it will be useful, but 21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 23 * for more details. 24 * 25 *############################################################################# 26 */ 27 28#include <linux/input.h> 29#include <linux/slab.h> 30#include <linux/i2c.h> 31#include <linux/interrupt.h> 32#include <linux/regulator/consumer.h> 33#include "synaptics_i2c_rmi4.h" 34 35/* TODO: for multiple device support will need a per-device mutex */ 36#define DRIVER_NAME "synaptics_rmi4_i2c" 37 38#define MAX_ERROR_REPORT 6 39#define MAX_TOUCH_MAJOR 15 40#define MAX_RETRY_COUNT 5 41#define STD_QUERY_LEN 21 42#define PAGE_LEN 2 43#define DATA_BUF_LEN 32 44#define BUF_LEN 37 45#define QUERY_LEN 9 46#define DATA_LEN 12 47#define HAS_TAP 0x01 48#define HAS_PALMDETECT 0x01 49#define HAS_ROTATE 0x02 50#define HAS_TAPANDHOLD 0x02 51#define HAS_DOUBLETAP 0x04 52#define HAS_EARLYTAP 0x08 53#define HAS_RELEASE 0x08 54#define HAS_FLICK 0x10 55#define HAS_PRESS 0x20 56#define HAS_PINCH 0x40 57 58#define MASK_16BIT 0xFFFF 59#define MASK_8BIT 0xFF 60#define MASK_7BIT 0x7F 61#define MASK_5BIT 0x1F 62#define MASK_4BIT 0x0F 63#define MASK_3BIT 0x07 64#define MASK_2BIT 0x03 65#define TOUCHPAD_CTRL_INTR 0x8 66#define PDT_START_SCAN_LOCATION (0x00E9) 67#define PDT_END_SCAN_LOCATION (0x000A) 68#define PDT_ENTRY_SIZE (0x0006) 69#define RMI4_NUMBER_OF_MAX_FINGERS (8) 70#define SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM (0x11) 71#define SYNAPTICS_RMI4_DEVICE_CONTROL_FUNC_NUM (0x01) 72 73/** 74 * struct synaptics_rmi4_fn_desc - contains the function descriptor information 75 * @query_base_addr: base address for query 76 * @cmd_base_addr: base address for command 77 * @ctrl_base_addr: base address for control 78 * @data_base_addr: base address for data 79 * @intr_src_count: count for the interrupt source 80 * @fn_number: function number 81 * 82 * This structure is used to gives the function descriptor information 83 * of the particular functionality. 84 */ 85struct synaptics_rmi4_fn_desc { 86 unsigned char query_base_addr; 87 unsigned char cmd_base_addr; 88 unsigned char ctrl_base_addr; 89 unsigned char data_base_addr; 90 unsigned char intr_src_count; 91 unsigned char fn_number; 92}; 93 94/** 95 * struct synaptics_rmi4_fn - contains the function information 96 * @fn_number: function number 97 * @num_of_data_sources: number of data sources 98 * @num_of_data_points: number of fingers touched 99 * @size_of_data_register_block: data register block size 100 * @index_to_intr_reg: index for interrupt register 101 * @intr_mask: interrupt mask value 102 * @fn_desc: variable for function descriptor structure 103 * @link: linked list for function descriptors 104 * 105 * This structure gives information about the number of data sources and 106 * the number of data registers associated with the function. 107 */ 108struct synaptics_rmi4_fn { 109 unsigned char fn_number; 110 unsigned char num_of_data_sources; 111 unsigned char num_of_data_points; 112 unsigned char size_of_data_register_block; 113 unsigned char index_to_intr_reg; 114 unsigned char intr_mask; 115 struct synaptics_rmi4_fn_desc fn_desc; 116 struct list_head link; 117}; 118 119/** 120 * struct synaptics_rmi4_device_info - contains the rmi4 device information 121 * @version_major: protocol major version number 122 * @version_minor: protocol minor version number 123 * @manufacturer_id: manufacturer identification byte 124 * @product_props: product properties information 125 * @product_info: product info array 126 * @date_code: device manufacture date 127 * @tester_id: tester id array 128 * @serial_number: serial number for that device 129 * @product_id_string: product id for the device 130 * @support_fn_list: linked list for device information 131 * 132 * This structure gives information about the number of data sources and 133 * the number of data registers associated with the function. 134 */ 135struct synaptics_rmi4_device_info { 136 unsigned int version_major; 137 unsigned int version_minor; 138 unsigned char manufacturer_id; 139 unsigned char product_props; 140 unsigned char product_info[2]; 141 unsigned char date_code[3]; 142 unsigned short tester_id; 143 unsigned short serial_number; 144 unsigned char product_id_string[11]; 145 struct list_head support_fn_list; 146}; 147 148/** 149 * struct synaptics_rmi4_data - contains the rmi4 device data 150 * @rmi4_mod_info: structure variable for rmi4 device info 151 * @input_dev: pointer for input device 152 * @i2c_client: pointer for i2c client 153 * @board: constant pointer for touch platform data 154 * @fn_list_mutex: mutex for function list 155 * @rmi4_page_mutex: mutex for rmi4 page 156 * @current_page: variable for integer 157 * @number_of_interrupt_register: interrupt registers count 158 * @fn01_ctrl_base_addr: control base address for fn01 159 * @fn01_query_base_addr: query base address for fn01 160 * @fn01_data_base_addr: data base address for fn01 161 * @sensor_max_x: sensor maximum x value 162 * @sensor_max_y: sensor maximum y value 163 * @regulator: pointer to the regulator structure 164 * @wait: wait queue structure variable 165 * @touch_stopped: flag to stop the thread function 166 * 167 * This structure gives the device data information. 168 */ 169struct synaptics_rmi4_data { 170 struct synaptics_rmi4_device_info rmi4_mod_info; 171 struct input_dev *input_dev; 172 struct i2c_client *i2c_client; 173 const struct synaptics_rmi4_platform_data *board; 174 struct mutex fn_list_mutex; 175 struct mutex rmi4_page_mutex; 176 int current_page; 177 unsigned int number_of_interrupt_register; 178 unsigned short fn01_ctrl_base_addr; 179 unsigned short fn01_query_base_addr; 180 unsigned short fn01_data_base_addr; 181 int sensor_max_x; 182 int sensor_max_y; 183 struct regulator *regulator; 184 wait_queue_head_t wait; 185 bool touch_stopped; 186}; 187 188/** 189 * synaptics_rmi4_set_page() - sets the page 190 * @pdata: pointer to synaptics_rmi4_data structure 191 * @address: set the address of the page 192 * 193 * This function is used to set the page and returns integer. 194 */ 195static int synaptics_rmi4_set_page(struct synaptics_rmi4_data *pdata, 196 unsigned int address) 197{ 198 unsigned char txbuf[PAGE_LEN]; 199 int retval; 200 unsigned int page; 201 struct i2c_client *i2c = pdata->i2c_client; 202 203 page = ((address >> 8) & MASK_8BIT); 204 if (page != pdata->current_page) { 205 txbuf[0] = MASK_8BIT; 206 txbuf[1] = page; 207 retval = i2c_master_send(i2c, txbuf, PAGE_LEN); 208 if (retval != PAGE_LEN) 209 dev_err(&i2c->dev, "%s:failed:%d\n", __func__, retval); 210 else 211 pdata->current_page = page; 212 } else 213 retval = PAGE_LEN; 214 return retval; 215} 216/** 217 * synaptics_rmi4_i2c_block_read() - read the block of data 218 * @pdata: pointer to synaptics_rmi4_data structure 219 * @address: read the block of data from this offset 220 * @valp: pointer to a buffer containing the data to be read 221 * @size: number of bytes to read 222 * 223 * This function is to read the block of data and returns integer. 224 */ 225static int synaptics_rmi4_i2c_block_read(struct synaptics_rmi4_data *pdata, 226 unsigned short address, 227 unsigned char *valp, int size) 228{ 229 int retval = 0; 230 int retry_count = 0; 231 int index; 232 struct i2c_client *i2c = pdata->i2c_client; 233 234 mutex_lock(&(pdata->rmi4_page_mutex)); 235 retval = synaptics_rmi4_set_page(pdata, address); 236 if (retval != PAGE_LEN) 237 goto exit; 238 index = address & MASK_8BIT; 239retry: 240 retval = i2c_smbus_read_i2c_block_data(i2c, index, size, valp); 241 if (retval != size) { 242 if (++retry_count == MAX_RETRY_COUNT) 243 dev_err(&i2c->dev, 244 "%s:address 0x%04x size %d failed:%d\n", 245 __func__, address, size, retval); 246 else { 247 synaptics_rmi4_set_page(pdata, address); 248 goto retry; 249 } 250 } 251exit: 252 mutex_unlock(&(pdata->rmi4_page_mutex)); 253 return retval; 254} 255 256/** 257 * synaptics_rmi4_i2c_byte_write() - write the single byte data 258 * @pdata: pointer to synaptics_rmi4_data structure 259 * @address: write the block of data from this offset 260 * @data: data to be write 261 * 262 * This function is to write the single byte data and returns integer. 263 */ 264static int synaptics_rmi4_i2c_byte_write(struct synaptics_rmi4_data *pdata, 265 unsigned short address, 266 unsigned char data) 267{ 268 unsigned char txbuf[2]; 269 int retval = 0; 270 struct i2c_client *i2c = pdata->i2c_client; 271 272 /* Can't have anyone else changing the page behind our backs */ 273 mutex_lock(&(pdata->rmi4_page_mutex)); 274 275 retval = synaptics_rmi4_set_page(pdata, address); 276 if (retval != PAGE_LEN) 277 goto exit; 278 txbuf[0] = address & MASK_8BIT; 279 txbuf[1] = data; 280 retval = i2c_master_send(pdata->i2c_client, txbuf, 2); 281 /* Add in retry on writes only in certain error return values */ 282 if (retval != 2) { 283 dev_err(&i2c->dev, "%s:failed:%d\n", __func__, retval); 284 retval = -EIO; 285 } else 286 retval = 1; 287exit: 288 mutex_unlock(&(pdata->rmi4_page_mutex)); 289 return retval; 290} 291 292/** 293 * synpatics_rmi4_touchpad_report() - reports for the rmi4 touchpad device 294 * @pdata: pointer to synaptics_rmi4_data structure 295 * @rfi: pointer to synaptics_rmi4_fn structure 296 * 297 * This function calls to reports for the rmi4 touchpad device 298 */ 299static int synpatics_rmi4_touchpad_report(struct synaptics_rmi4_data *pdata, 300 struct synaptics_rmi4_fn *rfi) 301{ 302 /* number of touch points - fingers down in this case */ 303 int touch_count = 0; 304 int finger; 305 int fingers_supported; 306 int finger_registers; 307 int reg; 308 int finger_shift; 309 int finger_status; 310 int retval; 311 unsigned short data_base_addr; 312 unsigned short data_offset; 313 unsigned char data_reg_blk_size; 314 unsigned char values[2]; 315 unsigned char data[DATA_LEN]; 316 int x[RMI4_NUMBER_OF_MAX_FINGERS]; 317 int y[RMI4_NUMBER_OF_MAX_FINGERS]; 318 int wx[RMI4_NUMBER_OF_MAX_FINGERS]; 319 int wy[RMI4_NUMBER_OF_MAX_FINGERS]; 320 struct i2c_client *client = pdata->i2c_client; 321 322 /* get 2D sensor finger data */ 323 /* 324 * First get the finger status field - the size of the finger status 325 * field is determined by the number of finger supporte - 2 bits per 326 * finger, so the number of registers to read is: 327 * registerCount = ceil(numberOfFingers/4). 328 * Read the required number of registers and check each 2 bit field to 329 * determine if a finger is down: 330 * 00 = finger not present, 331 * 01 = finger present and data accurate, 332 * 10 = finger present but data may not be accurate, 333 * 11 = reserved for product use. 334 */ 335 fingers_supported = rfi->num_of_data_points; 336 finger_registers = (fingers_supported + 3)/4; 337 data_base_addr = rfi->fn_desc.data_base_addr; 338 retval = synaptics_rmi4_i2c_block_read(pdata, data_base_addr, values, 339 finger_registers); 340 if (retval != finger_registers) { 341 dev_err(&client->dev, "%s:read status registers failed\n", 342 __func__); 343 return 0; 344 } 345 /* 346 * For each finger present, read the proper number of registers 347 * to get absolute data. 348 */ 349 data_reg_blk_size = rfi->size_of_data_register_block; 350 for (finger = 0; finger < fingers_supported; finger++) { 351 /* determine which data byte the finger status is in */ 352 reg = finger/4; 353 /* bit shift to get finger's status */ 354 finger_shift = (finger % 4) * 2; 355 finger_status = (values[reg] >> finger_shift) & 3; 356 /* 357 * if finger status indicates a finger is present then 358 * read the finger data and report it 359 */ 360 if (finger_status == 1 || finger_status == 2) { 361 /* Read the finger data */ 362 data_offset = data_base_addr + 363 ((finger * data_reg_blk_size) + 364 finger_registers); 365 retval = synaptics_rmi4_i2c_block_read(pdata, 366 data_offset, data, 367 data_reg_blk_size); 368 if (retval != data_reg_blk_size) { 369 printk(KERN_ERR "%s:read data failed\n", 370 __func__); 371 return 0; 372 } else { 373 x[touch_count] = 374 (data[0] << 4) | (data[2] & MASK_4BIT); 375 y[touch_count] = 376 (data[1] << 4) | 377 ((data[2] >> 4) & MASK_4BIT); 378 wy[touch_count] = 379 (data[3] >> 4) & MASK_4BIT; 380 wx[touch_count] = 381 (data[3] & MASK_4BIT); 382 383 if (pdata->board->x_flip) 384 x[touch_count] = 385 pdata->sensor_max_x - 386 x[touch_count]; 387 if (pdata->board->y_flip) 388 y[touch_count] = 389 pdata->sensor_max_y - 390 y[touch_count]; 391 } 392 /* number of active touch points */ 393 touch_count++; 394 } 395 } 396 397 /* report to input subsystem */ 398 if (touch_count) { 399 for (finger = 0; finger < touch_count; finger++) { 400 input_report_abs(pdata->input_dev, ABS_MT_TOUCH_MAJOR, 401 max(wx[finger] , wy[finger])); 402 input_report_abs(pdata->input_dev, ABS_MT_POSITION_X, 403 x[finger]); 404 input_report_abs(pdata->input_dev, ABS_MT_POSITION_Y, 405 y[finger]); 406 input_mt_sync(pdata->input_dev); 407 } 408 } else 409 input_mt_sync(pdata->input_dev); 410 411 /* sync after groups of events */ 412 input_sync(pdata->input_dev); 413 /* return the number of touch points */ 414 return touch_count; 415} 416 417/** 418 * synaptics_rmi4_report_device() - reports the rmi4 device 419 * @pdata: pointer to synaptics_rmi4_data structure 420 * @rfi: pointer to synaptics_rmi4_fn 421 * 422 * This function is used to call the report function of the rmi4 device. 423 */ 424static int synaptics_rmi4_report_device(struct synaptics_rmi4_data *pdata, 425 struct synaptics_rmi4_fn *rfi) 426{ 427 int touch = 0; 428 struct i2c_client *client = pdata->i2c_client; 429 static int num_error_reports; 430 if (rfi->fn_number != SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM) { 431 num_error_reports++; 432 if (num_error_reports < MAX_ERROR_REPORT) 433 dev_err(&client->dev, "%s:report not supported\n", 434 __func__); 435 } else 436 touch = synpatics_rmi4_touchpad_report(pdata, rfi); 437 return touch; 438} 439/** 440 * synaptics_rmi4_sensor_report() - reports to input subsystem 441 * @pdata: pointer to synaptics_rmi4_data structure 442 * 443 * This function is used to reads in all data sources and reports 444 * them to the input subsystem. 445 */ 446static int synaptics_rmi4_sensor_report(struct synaptics_rmi4_data *pdata) 447{ 448 unsigned char intr_status[4]; 449 /* number of touch points - fingers or buttons */ 450 int touch = 0; 451 unsigned int retval; 452 struct synaptics_rmi4_fn *rfi; 453 struct synaptics_rmi4_device_info *rmi; 454 struct i2c_client *client = pdata->i2c_client; 455 456 /* 457 * Get the interrupt status from the function $01 458 * control register+1 to find which source(s) were interrupting 459 * so we can read the data from the source(s) (2D sensor, buttons..) 460 */ 461 retval = synaptics_rmi4_i2c_block_read(pdata, 462 pdata->fn01_data_base_addr + 1, 463 intr_status, 464 pdata->number_of_interrupt_register); 465 if (retval != pdata->number_of_interrupt_register) { 466 dev_err(&client->dev, 467 "could not read interrupt status registers\n"); 468 return 0; 469 } 470 /* 471 * check each function that has data sources and if the interrupt for 472 * that triggered then call that RMI4 functions report() function to 473 * gather data and report it to the input subsystem 474 */ 475 rmi = &(pdata->rmi4_mod_info); 476 list_for_each_entry(rfi, &rmi->support_fn_list, link) { 477 if (rfi->num_of_data_sources) { 478 if (intr_status[rfi->index_to_intr_reg] & 479 rfi->intr_mask) 480 touch = synaptics_rmi4_report_device(pdata, 481 rfi); 482 } 483 } 484 /* return the number of touch points */ 485 return touch; 486} 487 488/** 489 * synaptics_rmi4_irq() - thread function for rmi4 attention line 490 * @irq: irq value 491 * @data: void pointer 492 * 493 * This function is interrupt thread function. It just notifies the 494 * application layer that attention is required. 495 */ 496static irqreturn_t synaptics_rmi4_irq(int irq, void *data) 497{ 498 struct synaptics_rmi4_data *pdata = data; 499 int touch_count; 500 do { 501 touch_count = synaptics_rmi4_sensor_report(pdata); 502 if (touch_count) 503 wait_event_timeout(pdata->wait, pdata->touch_stopped, 504 msecs_to_jiffies(1)); 505 else 506 break; 507 } while (!pdata->touch_stopped); 508 return IRQ_HANDLED; 509} 510 511/** 512 * synpatics_rmi4_touchpad_detect() - detects the rmi4 touchpad device 513 * @pdata: pointer to synaptics_rmi4_data structure 514 * @rfi: pointer to synaptics_rmi4_fn structure 515 * @fd: pointer to synaptics_rmi4_fn_desc structure 516 * @interruptcount: count the number of interrupts 517 * 518 * This function calls to detects the rmi4 touchpad device 519 */ 520static int synpatics_rmi4_touchpad_detect(struct synaptics_rmi4_data *pdata, 521 struct synaptics_rmi4_fn *rfi, 522 struct synaptics_rmi4_fn_desc *fd, 523 unsigned int interruptcount) 524{ 525 unsigned char queries[QUERY_LEN]; 526 unsigned short intr_offset; 527 unsigned char abs_data_size; 528 unsigned char abs_data_blk_size; 529 unsigned char egr_0, egr_1; 530 unsigned int all_data_blk_size; 531 int has_pinch, has_flick, has_tap; 532 int has_tapandhold, has_doubletap; 533 int has_earlytap, has_press; 534 int has_palmdetect, has_rotate; 535 int has_rel; 536 int i; 537 int retval; 538 struct i2c_client *client = pdata->i2c_client; 539 540 rfi->fn_desc.query_base_addr = fd->query_base_addr; 541 rfi->fn_desc.data_base_addr = fd->data_base_addr; 542 rfi->fn_desc.intr_src_count = fd->intr_src_count; 543 rfi->fn_desc.fn_number = fd->fn_number; 544 rfi->fn_number = fd->fn_number; 545 rfi->num_of_data_sources = fd->intr_src_count; 546 rfi->fn_desc.ctrl_base_addr = fd->ctrl_base_addr; 547 rfi->fn_desc.cmd_base_addr = fd->cmd_base_addr; 548 549 /* 550 * need to get number of fingers supported, data size, etc. 551 * to be used when getting data since the number of registers to 552 * read depends on the number of fingers supported and data size. 553 */ 554 retval = synaptics_rmi4_i2c_block_read(pdata, fd->query_base_addr, 555 queries, 556 sizeof(queries)); 557 if (retval != sizeof(queries)) { 558 dev_err(&client->dev, "%s:read function query registers\n", 559 __func__); 560 return retval; 561 } 562 /* 563 * 2D data sources have only 3 bits for the number of fingers 564 * supported - so the encoding is a bit weird. 565 */ 566 if ((queries[1] & MASK_3BIT) <= 4) 567 /* add 1 since zero based */ 568 rfi->num_of_data_points = (queries[1] & MASK_3BIT) + 1; 569 else { 570 /* 571 * a value of 5 is up to 10 fingers - 6 and 7 are reserved 572 * (shouldn't get these i int retval;n a normal 2D source). 573 */ 574 if ((queries[1] & MASK_3BIT) == 5) 575 rfi->num_of_data_points = 10; 576 } 577 /* Need to get interrupt info for handling interrupts */ 578 rfi->index_to_intr_reg = (interruptcount + 7)/8; 579 if (rfi->index_to_intr_reg != 0) 580 rfi->index_to_intr_reg -= 1; 581 /* 582 * loop through interrupts for each source in fn $11 583 * and or in a bit to the interrupt mask for each. 584 */ 585 intr_offset = interruptcount % 8; 586 rfi->intr_mask = 0; 587 for (i = intr_offset; 588 i < ((fd->intr_src_count & MASK_3BIT) + intr_offset); i++) 589 rfi->intr_mask |= 1 << i; 590 591 /* Size of just the absolute data for one finger */ 592 abs_data_size = queries[5] & MASK_2BIT; 593 /* One each for X and Y, one for LSB for X & Y, one for W, one for Z */ 594 abs_data_blk_size = 3 + (2 * (abs_data_size == 0 ? 1 : 0)); 595 rfi->size_of_data_register_block = abs_data_blk_size; 596 597 /* 598 * need to determine the size of data to read - this depends on 599 * conditions such as whether Relative data is reported and if Gesture 600 * data is reported. 601 */ 602 egr_0 = queries[7]; 603 egr_1 = queries[8]; 604 605 /* 606 * Get info about what EGR data is supported, whether it has 607 * Relative data supported, etc. 608 */ 609 has_pinch = egr_0 & HAS_PINCH; 610 has_flick = egr_0 & HAS_FLICK; 611 has_tap = egr_0 & HAS_TAP; 612 has_earlytap = egr_0 & HAS_EARLYTAP; 613 has_press = egr_0 & HAS_PRESS; 614 has_rotate = egr_1 & HAS_ROTATE; 615 has_rel = queries[1] & HAS_RELEASE; 616 has_tapandhold = egr_0 & HAS_TAPANDHOLD; 617 has_doubletap = egr_0 & HAS_DOUBLETAP; 618 has_palmdetect = egr_1 & HAS_PALMDETECT; 619 620 /* 621 * Size of all data including finger status, absolute data for each 622 * finger, relative data and EGR data 623 */ 624 all_data_blk_size = 625 /* finger status, four fingers per register */ 626 ((rfi->num_of_data_points + 3) / 4) + 627 /* absolute data, per finger times number of fingers */ 628 (abs_data_blk_size * rfi->num_of_data_points) + 629 /* 630 * two relative registers (if relative is being reported) 631 */ 632 2 * has_rel + 633 /* 634 * F11_2D_data8 is only present if the egr_0 635 * register is non-zero. 636 */ 637 !!(egr_0) + 638 /* 639 * F11_2D_data9 is only present if either egr_0 or 640 * egr_1 registers are non-zero. 641 */ 642 (egr_0 || egr_1) + 643 /* 644 * F11_2D_data10 is only present if EGR_PINCH or EGR_FLICK of 645 * egr_0 reports as 1. 646 */ 647 !!(has_pinch | has_flick) + 648 /* 649 * F11_2D_data11 and F11_2D_data12 are only present if 650 * EGR_FLICK of egr_0 reports as 1. 651 */ 652 2 * !!(has_flick); 653 return retval; 654} 655 656/** 657 * synpatics_rmi4_touchpad_config() - confiures the rmi4 touchpad device 658 * @pdata: pointer to synaptics_rmi4_data structure 659 * @rfi: pointer to synaptics_rmi4_fn structure 660 * 661 * This function calls to confiures the rmi4 touchpad device 662 */ 663int synpatics_rmi4_touchpad_config(struct synaptics_rmi4_data *pdata, 664 struct synaptics_rmi4_fn *rfi) 665{ 666 /* 667 * For the data source - print info and do any 668 * source specific configuration. 669 */ 670 unsigned char data[BUF_LEN]; 671 int retval = 0; 672 struct i2c_client *client = pdata->i2c_client; 673 674 /* Get and print some info about the data source... */ 675 /* To Query 2D devices we need to read from the address obtained 676 * from the function descriptor stored in the RMI function info. 677 */ 678 retval = synaptics_rmi4_i2c_block_read(pdata, 679 rfi->fn_desc.query_base_addr, 680 data, QUERY_LEN); 681 if (retval != QUERY_LEN) 682 dev_err(&client->dev, "%s:read query registers failed\n", 683 __func__); 684 else { 685 retval = synaptics_rmi4_i2c_block_read(pdata, 686 rfi->fn_desc.ctrl_base_addr, 687 data, DATA_BUF_LEN); 688 if (retval != DATA_BUF_LEN) { 689 dev_err(&client->dev, 690 "%s:read control registers failed\n", 691 __func__); 692 return retval; 693 } 694 /* Store these for use later*/ 695 pdata->sensor_max_x = ((data[6] & MASK_8BIT) << 0) | 696 ((data[7] & MASK_4BIT) << 8); 697 pdata->sensor_max_y = ((data[8] & MASK_5BIT) << 0) | 698 ((data[9] & MASK_4BIT) << 8); 699 } 700 return retval; 701} 702 703/** 704 * synaptics_rmi4_i2c_query_device() - query the rmi4 device 705 * @pdata: pointer to synaptics_rmi4_data structure 706 * 707 * This function is used to query the rmi4 device. 708 */ 709static int synaptics_rmi4_i2c_query_device(struct synaptics_rmi4_data *pdata) 710{ 711 int i; 712 int retval; 713 unsigned char std_queries[STD_QUERY_LEN]; 714 unsigned char intr_count = 0; 715 int data_sources = 0; 716 unsigned int ctrl_offset; 717 struct synaptics_rmi4_fn *rfi; 718 struct synaptics_rmi4_fn_desc rmi_fd; 719 struct synaptics_rmi4_device_info *rmi; 720 struct i2c_client *client = pdata->i2c_client; 721 722 /* 723 * init the physical drivers RMI module 724 * info list of functions 725 */ 726 INIT_LIST_HEAD(&pdata->rmi4_mod_info.support_fn_list); 727 728 /* 729 * Read the Page Descriptor Table to determine what functions 730 * are present 731 */ 732 for (i = PDT_START_SCAN_LOCATION; i > PDT_END_SCAN_LOCATION; 733 i -= PDT_ENTRY_SIZE) { 734 retval = synaptics_rmi4_i2c_block_read(pdata, i, 735 (unsigned char *)&rmi_fd, 736 sizeof(rmi_fd)); 737 if (retval != sizeof(rmi_fd)) { 738 /* failed to read next PDT entry */ 739 dev_err(&client->dev, "%s: read error\n", __func__); 740 return -EIO; 741 } 742 rfi = NULL; 743 if (rmi_fd.fn_number) { 744 switch (rmi_fd.fn_number & MASK_8BIT) { 745 case SYNAPTICS_RMI4_DEVICE_CONTROL_FUNC_NUM: 746 pdata->fn01_query_base_addr = 747 rmi_fd.query_base_addr; 748 pdata->fn01_ctrl_base_addr = 749 rmi_fd.ctrl_base_addr; 750 pdata->fn01_data_base_addr = 751 rmi_fd.data_base_addr; 752 break; 753 case SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM: 754 if (rmi_fd.intr_src_count) { 755 rfi = kmalloc(sizeof(*rfi), 756 GFP_KERNEL); 757 if (!rfi) { 758 dev_err(&client->dev, 759 "%s:kmalloc failed\n", 760 __func__); 761 return -ENOMEM; 762 } 763 retval = synpatics_rmi4_touchpad_detect 764 (pdata, rfi, 765 &rmi_fd, 766 intr_count); 767 if (retval < 0) { 768 kfree(rfi); 769 return retval; 770 } 771 } 772 break; 773 } 774 /* interrupt count for next iteration */ 775 intr_count += (rmi_fd.intr_src_count & MASK_3BIT); 776 /* 777 * We only want to add functions to the list 778 * that have data associated with them. 779 */ 780 if (rfi && rmi_fd.intr_src_count) { 781 /* link this function info to the RMI module */ 782 mutex_lock(&(pdata->fn_list_mutex)); 783 list_add_tail(&rfi->link, 784 &pdata->rmi4_mod_info.support_fn_list); 785 mutex_unlock(&(pdata->fn_list_mutex)); 786 } 787 } else { 788 /* 789 * A zero in the function number 790 * signals the end of the PDT 791 */ 792 dev_dbg(&client->dev, 793 "%s:end of PDT\n", __func__); 794 break; 795 } 796 } 797 /* 798 * calculate the interrupt register count - used in the 799 * ISR to read the correct number of interrupt registers 800 */ 801 pdata->number_of_interrupt_register = (intr_count + 7) / 8; 802 /* 803 * Function $01 will be used to query the product properties, 804 * and product ID so we had to read the PDT above first to get 805 * the Fn $01 query address and prior to filling in the product 806 * info. NOTE: Even an unflashed device will still have FN $01. 807 */ 808 809 /* Load up the standard queries and get the RMI4 module info */ 810 retval = synaptics_rmi4_i2c_block_read(pdata, 811 pdata->fn01_query_base_addr, 812 std_queries, 813 sizeof(std_queries)); 814 if (retval != sizeof(std_queries)) { 815 dev_err(&client->dev, "%s:Failed reading queries\n", 816 __func__); 817 return -EIO; 818 } 819 820 /* Currently supported RMI version is 4.0 */ 821 pdata->rmi4_mod_info.version_major = 4; 822 pdata->rmi4_mod_info.version_minor = 0; 823 /* 824 * get manufacturer id, product_props, product info, 825 * date code, tester id, serial num and product id (name) 826 */ 827 pdata->rmi4_mod_info.manufacturer_id = std_queries[0]; 828 pdata->rmi4_mod_info.product_props = std_queries[1]; 829 pdata->rmi4_mod_info.product_info[0] = std_queries[2]; 830 pdata->rmi4_mod_info.product_info[1] = std_queries[3]; 831 /* year - 2001-2032 */ 832 pdata->rmi4_mod_info.date_code[0] = std_queries[4] & MASK_5BIT; 833 /* month - 1-12 */ 834 pdata->rmi4_mod_info.date_code[1] = std_queries[5] & MASK_4BIT; 835 /* day - 1-31 */ 836 pdata->rmi4_mod_info.date_code[2] = std_queries[6] & MASK_5BIT; 837 pdata->rmi4_mod_info.tester_id = ((std_queries[7] & MASK_7BIT) << 8) | 838 (std_queries[8] & MASK_7BIT); 839 pdata->rmi4_mod_info.serial_number = 840 ((std_queries[9] & MASK_7BIT) << 8) | 841 (std_queries[10] & MASK_7BIT); 842 memcpy(pdata->rmi4_mod_info.product_id_string, &std_queries[11], 10); 843 844 /* Check if this is a Synaptics device - report if not. */ 845 if (pdata->rmi4_mod_info.manufacturer_id != 1) 846 dev_err(&client->dev, "%s: non-Synaptics mfg id:%d\n", 847 __func__, pdata->rmi4_mod_info.manufacturer_id); 848 849 list_for_each_entry(rfi, &pdata->rmi4_mod_info.support_fn_list, link) 850 data_sources += rfi->num_of_data_sources; 851 if (data_sources) { 852 rmi = &(pdata->rmi4_mod_info); 853 list_for_each_entry(rfi, &rmi->support_fn_list, link) { 854 if (rfi->num_of_data_sources) { 855 if (rfi->fn_number == 856 SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM) { 857 retval = synpatics_rmi4_touchpad_config 858 (pdata, rfi); 859 if (retval < 0) 860 return retval; 861 } else 862 dev_err(&client->dev, 863 "%s:fn_number not supported\n", 864 __func__); 865 /* 866 * Turn on interrupts for this 867 * function's data sources. 868 */ 869 ctrl_offset = pdata->fn01_ctrl_base_addr + 1 + 870 rfi->index_to_intr_reg; 871 retval = synaptics_rmi4_i2c_byte_write(pdata, 872 ctrl_offset, 873 rfi->intr_mask); 874 if (retval < 0) 875 return retval; 876 } 877 } 878 } 879 return 0; 880} 881 882/** 883 * synaptics_rmi4_probe() - Initialze the i2c-client touchscreen driver 884 * @i2c: i2c client structure pointer 885 * @id:i2c device id pointer 886 * 887 * This function will allocate and initialize the instance 888 * data and request the irq and set the instance data as the clients 889 * platform data then register the physical driver which will do a scan of 890 * the rmi4 Physical Device Table and enumerate any rmi4 functions that 891 * have data sources associated with them. 892 */ 893static int __devinit synaptics_rmi4_probe 894 (struct i2c_client *client, const struct i2c_device_id *dev_id) 895{ 896 int retval; 897 unsigned char intr_status[4]; 898 struct synaptics_rmi4_data *rmi4_data; 899 const struct synaptics_rmi4_platform_data *platformdata = 900 client->dev.platform_data; 901 902 if (!i2c_check_functionality(client->adapter, 903 I2C_FUNC_SMBUS_BYTE_DATA)) { 904 dev_err(&client->dev, "i2c smbus byte data not supported\n"); 905 return -EIO; 906 } 907 908 if (!platformdata) { 909 dev_err(&client->dev, "%s: no platform data\n", __func__); 910 return -EINVAL; 911 } 912 913 /* Allocate and initialize the instance data for this client */ 914 rmi4_data = kzalloc(sizeof(struct synaptics_rmi4_data) * 2, 915 GFP_KERNEL); 916 if (!rmi4_data) { 917 dev_err(&client->dev, "%s: no memory allocated\n", __func__); 918 return -ENOMEM; 919 } 920 921 rmi4_data->input_dev = input_allocate_device(); 922 if (rmi4_data->input_dev == NULL) { 923 dev_err(&client->dev, "%s:input device alloc failed\n", 924 __func__); 925 retval = -ENOMEM; 926 goto err_input; 927 } 928 929 if (platformdata->regulator_en) { 930 rmi4_data->regulator = regulator_get(&client->dev, "vdd"); 931 if (IS_ERR(rmi4_data->regulator)) { 932 dev_err(&client->dev, "%s:get regulator failed\n", 933 __func__); 934 retval = PTR_ERR(rmi4_data->regulator); 935 goto err_regulator; 936 } 937 regulator_enable(rmi4_data->regulator); 938 } 939 940 init_waitqueue_head(&rmi4_data->wait); 941 /* 942 * Copy i2c_client pointer into RTID's i2c_client pointer for 943 * later use in rmi4_read, rmi4_write, etc. 944 */ 945 rmi4_data->i2c_client = client; 946 /* So we set the page correctly the first time */ 947 rmi4_data->current_page = MASK_16BIT; 948 rmi4_data->board = platformdata; 949 rmi4_data->touch_stopped = false; 950 951 /* init the mutexes for maintain the lists */ 952 mutex_init(&(rmi4_data->fn_list_mutex)); 953 mutex_init(&(rmi4_data->rmi4_page_mutex)); 954 955 /* 956 * Register physical driver - this will call the detect function that 957 * will then scan the device and determine the supported 958 * rmi4 functions. 959 */ 960 retval = synaptics_rmi4_i2c_query_device(rmi4_data); 961 if (retval) { 962 dev_err(&client->dev, "%s: rmi4 query device failed\n", 963 __func__); 964 goto err_query_dev; 965 } 966 967 /* Store the instance data in the i2c_client */ 968 i2c_set_clientdata(client, rmi4_data); 969 970 /*initialize the input device parameters */ 971 rmi4_data->input_dev->name = DRIVER_NAME; 972 rmi4_data->input_dev->phys = "Synaptics_Clearpad"; 973 rmi4_data->input_dev->id.bustype = BUS_I2C; 974 rmi4_data->input_dev->dev.parent = &client->dev; 975 input_set_drvdata(rmi4_data->input_dev, rmi4_data); 976 977 /* Initialize the function handlers for rmi4 */ 978 set_bit(EV_SYN, rmi4_data->input_dev->evbit); 979 set_bit(EV_KEY, rmi4_data->input_dev->evbit); 980 set_bit(EV_ABS, rmi4_data->input_dev->evbit); 981 982 input_set_abs_params(rmi4_data->input_dev, ABS_MT_POSITION_X, 0, 983 rmi4_data->sensor_max_x, 0, 0); 984 input_set_abs_params(rmi4_data->input_dev, ABS_MT_POSITION_Y, 0, 985 rmi4_data->sensor_max_y, 0, 0); 986 input_set_abs_params(rmi4_data->input_dev, ABS_MT_TOUCH_MAJOR, 0, 987 MAX_TOUCH_MAJOR, 0, 0); 988 989 /* Clear interrupts */ 990 synaptics_rmi4_i2c_block_read(rmi4_data, 991 rmi4_data->fn01_data_base_addr + 1, intr_status, 992 rmi4_data->number_of_interrupt_register); 993 retval = request_threaded_irq(platformdata->irq_number, NULL, 994 synaptics_rmi4_irq, 995 platformdata->irq_type, 996 DRIVER_NAME, rmi4_data); 997 if (retval) { 998 dev_err(&client->dev, "%s:Unable to get attn irq %d\n", 999 __func__, platformdata->irq_number); 1000 goto err_query_dev; 1001 } 1002 1003 retval = input_register_device(rmi4_data->input_dev); 1004 if (retval) { 1005 dev_err(&client->dev, "%s:input register failed\n", __func__); 1006 goto err_free_irq; 1007 } 1008 1009 return retval; 1010 1011err_free_irq: 1012 free_irq(platformdata->irq_number, rmi4_data); 1013err_query_dev: 1014 if (platformdata->regulator_en) { 1015 regulator_disable(rmi4_data->regulator); 1016 regulator_put(rmi4_data->regulator); 1017 } 1018err_regulator: 1019 input_free_device(rmi4_data->input_dev); 1020 rmi4_data->input_dev = NULL; 1021err_input: 1022 kfree(rmi4_data); 1023 1024 return retval; 1025} 1026/** 1027 * synaptics_rmi4_remove() - Removes the i2c-client touchscreen driver 1028 * @client: i2c client structure pointer 1029 * 1030 * This function uses to remove the i2c-client 1031 * touchscreen driver and returns integer. 1032 */ 1033static int __devexit synaptics_rmi4_remove(struct i2c_client *client) 1034{ 1035 struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client); 1036 const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board; 1037 1038 rmi4_data->touch_stopped = true; 1039 wake_up(&rmi4_data->wait); 1040 free_irq(pdata->irq_number, rmi4_data); 1041 input_unregister_device(rmi4_data->input_dev); 1042 if (pdata->regulator_en) { 1043 regulator_disable(rmi4_data->regulator); 1044 regulator_put(rmi4_data->regulator); 1045 } 1046 kfree(rmi4_data); 1047 1048 return 0; 1049} 1050 1051#ifdef CONFIG_PM 1052/** 1053 * synaptics_rmi4_suspend() - suspend the touch screen controller 1054 * @dev: pointer to device structure 1055 * 1056 * This function is used to suspend the 1057 * touch panel controller and returns integer 1058 */ 1059static int synaptics_rmi4_suspend(struct device *dev) 1060{ 1061 /* Touch sleep mode */ 1062 int retval; 1063 unsigned char intr_status; 1064 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev); 1065 const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board; 1066 1067 rmi4_data->touch_stopped = true; 1068 disable_irq(pdata->irq_number); 1069 1070 retval = synaptics_rmi4_i2c_block_read(rmi4_data, 1071 rmi4_data->fn01_data_base_addr + 1, 1072 &intr_status, 1073 rmi4_data->number_of_interrupt_register); 1074 if (retval < 0) 1075 return retval; 1076 1077 retval = synaptics_rmi4_i2c_byte_write(rmi4_data, 1078 rmi4_data->fn01_ctrl_base_addr + 1, 1079 (intr_status & ~TOUCHPAD_CTRL_INTR)); 1080 if (retval < 0) 1081 return retval; 1082 1083 if (pdata->regulator_en) 1084 regulator_disable(rmi4_data->regulator); 1085 1086 return 0; 1087} 1088/** 1089 * synaptics_rmi4_resume() - resume the touch screen controller 1090 * @dev: pointer to device structure 1091 * 1092 * This function is used to resume the touch panel 1093 * controller and returns integer. 1094 */ 1095static int synaptics_rmi4_resume(struct device *dev) 1096{ 1097 int retval; 1098 unsigned char intr_status; 1099 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev); 1100 const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board; 1101 1102 if (pdata->regulator_en) 1103 regulator_enable(rmi4_data->regulator); 1104 1105 enable_irq(pdata->irq_number); 1106 rmi4_data->touch_stopped = false; 1107 1108 retval = synaptics_rmi4_i2c_block_read(rmi4_data, 1109 rmi4_data->fn01_data_base_addr + 1, 1110 &intr_status, 1111 rmi4_data->number_of_interrupt_register); 1112 if (retval < 0) 1113 return retval; 1114 1115 retval = synaptics_rmi4_i2c_byte_write(rmi4_data, 1116 rmi4_data->fn01_ctrl_base_addr + 1, 1117 (intr_status | TOUCHPAD_CTRL_INTR)); 1118 if (retval < 0) 1119 return retval; 1120 1121 return 0; 1122} 1123 1124static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = { 1125 .suspend = synaptics_rmi4_suspend, 1126 .resume = synaptics_rmi4_resume, 1127}; 1128#endif 1129 1130static const struct i2c_device_id synaptics_rmi4_id_table[] = { 1131 { DRIVER_NAME, 0 }, 1132 { }, 1133}; 1134MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table); 1135 1136static struct i2c_driver synaptics_rmi4_driver = { 1137 .driver = { 1138 .name = DRIVER_NAME, 1139 .owner = THIS_MODULE, 1140#ifdef CONFIG_PM 1141 .pm = &synaptics_rmi4_dev_pm_ops, 1142#endif 1143 }, 1144 .probe = synaptics_rmi4_probe, 1145 .remove = __devexit_p(synaptics_rmi4_remove), 1146 .id_table = synaptics_rmi4_id_table, 1147}; 1148/** 1149 * synaptics_rmi4_init() - Initialize the touchscreen driver 1150 * 1151 * This function uses to initializes the synaptics 1152 * touchscreen driver and returns integer. 1153 */ 1154static int __init synaptics_rmi4_init(void) 1155{ 1156 return i2c_add_driver(&synaptics_rmi4_driver); 1157} 1158/** 1159 * synaptics_rmi4_exit() - De-initialize the touchscreen driver 1160 * 1161 * This function uses to de-initialize the synaptics 1162 * touchscreen driver and returns none. 1163 */ 1164static void __exit synaptics_rmi4_exit(void) 1165{ 1166 i2c_del_driver(&synaptics_rmi4_driver); 1167} 1168 1169 1170module_init(synaptics_rmi4_init); 1171module_exit(synaptics_rmi4_exit); 1172 1173MODULE_LICENSE("GPL v2"); 1174MODULE_AUTHOR("naveen.gaddipati@stericsson.com, js.ha@stericsson.com"); 1175MODULE_DESCRIPTION("synaptics rmi4 i2c touch Driver"); 1176MODULE_ALIAS("i2c:synaptics_rmi4_ts"); 1177