input: touchscreen: add touch screen of gslx680 for rk3399-firefly-edp
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / synaptics_i2c_rmi4.c
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 <linux/earlysuspend.h>
34
35 #include <linux/workqueue.h>
36 #include <linux/delay.h>
37 #include <linux/input/mt.h>
38
39 #include <mach/gpio.h>
40 #include <mach/board.h>
41
42 #include "synaptics_i2c_rmi4.h"
43
44 /* TODO: for multiple device support will need a per-device mutex */
45 #define DRIVER_NAME "synaptics_rmi4_i2c"
46
47 #define MAX_ERROR_REPORT        6
48 #define MAX_TOUCH_MAJOR         15
49 #define MAX_RETRY_COUNT         5
50 #define STD_QUERY_LEN           21
51 #define PAGE_LEN                2
52 #define DATA_BUF_LEN            32
53 #define BUF_LEN                 37
54 #define QUERY_LEN               9
55 #define DATA_LEN                12
56 #define HAS_TAP                 0x01
57 #define HAS_PALMDETECT          0x01
58 #define HAS_ROTATE              0x02
59 #define HAS_TAPANDHOLD          0x02
60 #define HAS_DOUBLETAP           0x04
61 #define HAS_EARLYTAP            0x08
62 #define HAS_RELEASE             0x08
63 #define HAS_FLICK               0x10
64 #define HAS_PRESS               0x20
65 #define HAS_PINCH               0x40
66
67 #define MASK_16BIT              0xFFFF
68 #define MASK_8BIT               0xFF
69 #define MASK_7BIT               0x7F
70 #define MASK_5BIT               0x1F
71 #define MASK_4BIT               0x0F
72 #define MASK_3BIT               0x07
73 #define MASK_2BIT               0x03
74 /* cwz: change 0x08 -> 0x04 for RMI version 4.03 */
75 #if 0
76 #define TOUCHPAD_CTRL_INTR      0x8
77 #else
78 #define TOUCHPAD_CTRL_INTR      0x4     //rmi4_acgzx modified
79 #endif
80 #define PDT_START_SCAN_LOCATION (0x00E9)
81 #define PDT_END_SCAN_LOCATION   (0x000A)
82 #define PDT_ENTRY_SIZE          (0x0006)
83
84 #define RMI4_NUMBER_OF_MAX_FINGERS              (8)
85 #define SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM        (0x11)
86 #define SYNAPTICS_RMI4_DEVICE_CONTROL_FUNC_NUM  (0x01)
87
88 #define SYNA_SUPPORT_POINT_MAX  (5)
89 #define SYNA_FINGER_MAX         (5)
90
91 #define SYNA_I2C_BUS_IO         1 /* cwz: 1 for i2c io poart. */
92 #define RMI4_MULTI_TOUCH        1 /* cwz: 1 for kernel new report mode */
93
94 #define RMI4_I2C_SPEED          (400*1000) /* cwz: i2c io speed */
95
96 #define SYNA_WIDTH_DELTA        (2)             /* rmi4_acgzx: test empirical value */
97
98 #define SYNA_AIXS_REVISE_FORCE  (0)
99 #if SYNA_AIXS_REVISE_FORCE
100 #define SYNA_AIXS_X_DELTA       (0x40)  //rmi4_acgzx: revise synaptics s7010 demo tp axis x for kindle
101 #define SYNA_WIDTH_DELTA_FOR_X_MAX      (6)
102 #else
103 #define SYNA_AIXS_X_DELTA       (0)
104 #define SYNA_WIDTH_DELTA_FOR_X_MAX      (0)
105 #endif
106
107 static struct i2c_client *syna_client;
108
109 #ifdef CONFIG_HAS_EARLYSUSPEND
110 static void synaptics_rmi4_early_suspend(struct early_suspend *h);
111 static void synaptics_rmi4_late_resume(struct early_suspend *h);
112 #endif
113
114 #if 1
115 #define rmi4_debug(level, ...) \
116         do { \
117                         printk(KERN_ERR __VA_ARGS__); \
118         } while (0)
119 #else
120 #define rmi4_debug(level, ...)
121 #endif
122
123 #if (RMI4_MULTI_TOUCH != 0)
124 enum synaptics_rmi4_touch_status {
125         SYNA_TOUCH_ORIGINAL = 0,
126         SYNA_TOUCH_PRESS,
127         SYNA_TOUCH_RELEASE
128 };
129 #endif
130
131 /**
132  * struct synaptics_rmi4_fn_desc - contains the function descriptor information
133  * @query_base_addr: base address for query
134  * @cmd_base_addr: base address for command
135  * @ctrl_base_addr: base address for control
136  * @data_base_addr: base address for data
137  * @intr_src_count: count for the interrupt source
138  * @fn_number: function number
139  *
140  * This structure is used to gives the function descriptor information
141  * of the particular functionality.
142  */
143 struct synaptics_rmi4_fn_desc {
144         unsigned char   query_base_addr;
145         unsigned char   cmd_base_addr;
146         unsigned char   ctrl_base_addr;
147         unsigned char   data_base_addr;
148         unsigned char   intr_src_count;
149         unsigned char   fn_number;
150 };
151
152 /**
153  * struct synaptics_rmi4_fn - contains the function information
154  * @fn_number: function number
155  * @num_of_data_sources: number of data sources
156  * @num_of_data_points: number of fingers touched
157  * @size_of_data_register_block: data register block size
158  * @index_to_intr_reg: index for interrupt register
159  * @intr_mask: interrupt mask value
160  * @fn_desc: variable for function descriptor structure
161  * @link: linked list for function descriptors
162  *
163  * This structure gives information about the number of data sources and
164  * the number of data registers associated with the function.
165  */
166 struct synaptics_rmi4_fn {
167         unsigned char           fn_number;
168         unsigned char           num_of_data_sources;
169         unsigned char           num_of_data_points;
170         unsigned char           size_of_data_register_block;
171         unsigned char           index_to_intr_reg;
172         unsigned char           intr_mask;
173         struct synaptics_rmi4_fn_desc   fn_desc;
174         struct list_head        link;
175 };
176
177 /**
178  * struct synaptics_rmi4_device_info - contains the rmi4 device information
179  * @version_major: protocol major version number
180  * @version_minor: protocol minor version number
181  * @manufacturer_id: manufacturer identification byte
182  * @product_props: product properties information
183  * @product_info: product info array
184  * @date_code: device manufacture date
185  * @tester_id: tester id array
186  * @serial_number: serial number for that device
187  * @product_id_string: product id for the device
188  * @support_fn_list: linked list for device information
189  *
190  * This structure gives information about the number of data sources and
191  * the number of data registers associated with the function.
192  */
193 struct synaptics_rmi4_device_info {
194         unsigned int            version_major;
195         unsigned int            version_minor;
196         unsigned char           manufacturer_id;
197         unsigned char           product_props;
198         unsigned char           product_info[2];
199         unsigned char           date_code[3];
200         unsigned short          tester_id;
201         unsigned short          serial_number;
202         unsigned char           product_id_string[11];
203         struct list_head        support_fn_list;
204 };
205
206 /**
207  * struct synaptics_rmi4_data - contains the rmi4 device data
208  * @rmi4_mod_info: structure variable for rmi4 device info
209  * @input_dev: pointer for input device
210  * @i2c_client: pointer for i2c client
211  * @board: constant pointer for touch platform data
212  * @fn_list_mutex: mutex for function list
213  * @rmi4_page_mutex: mutex for rmi4 page
214  * @current_page: variable for integer
215  * @number_of_interrupt_register: interrupt registers count
216  * @fn01_ctrl_base_addr: control base address for fn01
217  * @fn01_query_base_addr: query base address for fn01
218  * @fn01_data_base_addr: data base address for fn01
219  * @sensor_max_x: sensor maximum x value
220  * @sensor_max_y: sensor maximum y value
221  * @regulator: pointer to the regulator structure
222  * @wait: wait queue structure variable
223  * @touch_stopped: flag to stop the thread function
224  *
225  * This structure gives the device data information.
226  */
227 struct synaptics_rmi4_data {
228         struct synaptics_rmi4_device_info rmi4_mod_info;
229         struct input_dev        *input_dev;
230         struct i2c_client       *i2c_client;
231         const struct synaptics_rmi4_platform_data *board;
232         struct mutex            fn_list_mutex;
233         struct mutex            rmi4_page_mutex;
234         int                     current_page;
235         unsigned int            number_of_interrupt_register;
236         unsigned short          fn01_ctrl_base_addr;
237         unsigned short          fn01_query_base_addr;
238         unsigned short          fn01_data_base_addr;
239         int                     sensor_max_x;
240         int                     sensor_max_y;
241         struct regulator        *regulator;
242         wait_queue_head_t       wait;
243         bool                    touch_stopped;
244
245         struct mutex            rmi4_wq_mutex;
246         struct work_struct work;
247         struct workqueue_struct *rmi4_wq;
248         struct early_suspend m_early_suspend;
249 };
250
251 #if (SYNA_I2C_BUS_IO != 0)
252 static int synaptics_rmi4_read_block(struct i2c_client *client,
253                    u16 addr,
254                    u16 length,
255                    u8 *value)
256 {
257         struct i2c_adapter *adapter = client->adapter;
258         struct i2c_msg msg[2];
259         __le16  le_addr;
260         int ret;
261
262         le_addr = cpu_to_le16(addr);
263
264         msg[0].scl_rate = RMI4_I2C_SPEED;
265         msg[0].addr  = client->addr;
266         msg[0].flags = 0x00;
267         msg[0].len   = 1;
268         msg[0].buf   = (u8*) &le_addr;
269
270         msg[1].scl_rate = RMI4_I2C_SPEED;
271         msg[1].addr  = client->addr;
272         msg[1].flags = I2C_M_RD;
273         msg[1].len   = length;
274         msg[1].buf   = (u8*) value;
275
276         ret = i2c_transfer(adapter, msg, 2);
277
278         return (ret == 2)? length : ret;
279 }
280 #endif
281
282 /**
283  * synaptics_rmi4_set_page() - sets the page
284  * @pdata: pointer to synaptics_rmi4_data structure
285  * @address: set the address of the page
286  *
287  * This function is used to set the page and returns integer.
288  */
289 static int synaptics_rmi4_set_page(struct synaptics_rmi4_data *pdata,
290                                         unsigned int address)
291 {
292         unsigned char   txbuf[PAGE_LEN];
293         int             retval;
294         unsigned int    page;
295         struct i2c_client *i2c = pdata->i2c_client;
296
297         page    = ((address >> 8) & MASK_8BIT);
298         if (page != pdata->current_page) {
299                 txbuf[0]        = MASK_8BIT;
300                 txbuf[1]        = page;
301                 retval  = i2c_master_send(i2c, txbuf, PAGE_LEN);
302                 if (retval != PAGE_LEN)
303                         dev_err(&i2c->dev, "%s:failed:%d\n", __func__, retval);
304                 else
305                         pdata->current_page = page;
306         } else
307                 retval = PAGE_LEN;
308         return retval;
309 }
310 /**
311  * synaptics_rmi4_i2c_block_read() - read the block of data
312  * @pdata: pointer to synaptics_rmi4_data structure
313  * @address: read the block of data from this offset
314  * @valp: pointer to a buffer containing the data to be read
315  * @size: number of bytes to read
316  *
317  * This function is to read the block of data and returns integer.
318  */
319 static int synaptics_rmi4_i2c_block_read(struct synaptics_rmi4_data *pdata,
320                                                 unsigned short address,
321                                                 unsigned char *valp, int size)
322 {
323         int retval = 0;
324         int retry_count = 0;
325         int index;
326         struct i2c_client *i2c = pdata->i2c_client;
327
328         mutex_lock(&(pdata->rmi4_page_mutex));
329         retval = synaptics_rmi4_set_page(pdata, address);
330         if (retval != PAGE_LEN)
331                 goto exit;
332         index = address & MASK_8BIT;
333 retry:
334 #if (SYNA_I2C_BUS_IO != 0)
335         retval = synaptics_rmi4_read_block(i2c, index, size, valp);
336 #else
337         retval = i2c_smbus_read_i2c_block_data(i2c, index, size, valp);
338 #endif
339         if (retval != size) {
340                 if (++retry_count == MAX_RETRY_COUNT)
341                         dev_err(&i2c->dev,
342                                 "%s:address 0x%04x size %d failed:%d\n",
343                                         __func__, address, size, retval);
344                 else {
345                         synaptics_rmi4_set_page(pdata, address);
346                         goto retry;
347                 }
348         }
349 exit:
350         mutex_unlock(&(pdata->rmi4_page_mutex));
351         return retval;
352 }
353
354 /**
355  * synaptics_rmi4_i2c_byte_write() - write the single byte data
356  * @pdata: pointer to synaptics_rmi4_data structure
357  * @address: write the block of data from this offset
358  * @data: data to be write
359  *
360  * This function is to write the single byte data and returns integer.
361  */
362 static int synaptics_rmi4_i2c_byte_write(struct synaptics_rmi4_data *pdata,
363                                                 unsigned short address,
364                                                 unsigned char data)
365 {
366         unsigned char txbuf[2];
367         int retval = 0;
368         struct i2c_client *i2c = pdata->i2c_client;
369
370         /* Can't have anyone else changing the page behind our backs */
371         mutex_lock(&(pdata->rmi4_page_mutex));
372
373         retval = synaptics_rmi4_set_page(pdata, address);
374         if (retval != PAGE_LEN)
375                 goto exit;
376         txbuf[0]        = address & MASK_8BIT;
377         txbuf[1]        = data;
378         retval          = i2c_master_send(pdata->i2c_client, txbuf, 2);
379         /* Add in retry on writes only in certain error return values */
380         if (retval != 2) {
381                 dev_err(&i2c->dev, "%s:failed:%d\n", __func__, retval);
382                 retval = -EIO;
383         } else
384                 retval = 1;
385 exit:
386         mutex_unlock(&(pdata->rmi4_page_mutex));
387         return retval;
388 }
389 //hhb@rock-chips.com
390 static int synaptics_rmi4_i2c_block_write(struct synaptics_rmi4_data *pdata,
391                                                 unsigned short address, unsigned char *data, unsigned char size)
392 {
393         unsigned char txbuf[8];
394         int retval = 0;
395         struct i2c_client *i2c = pdata->i2c_client;
396
397         /* Can't have anyone else changing the page behind our backs */
398         mutex_lock(&(pdata->rmi4_page_mutex));
399
400         retval = synaptics_rmi4_set_page(pdata, address);
401         if (retval != PAGE_LEN)
402                 goto exit;
403         txbuf[0] = address & MASK_8BIT;
404         memcpy(txbuf + 1, data, size);
405         retval = i2c_master_send(pdata->i2c_client, txbuf, size + 1);
406         /* Add in retry on writes only in certain error return values */
407         if (retval != 2) {
408                 dev_err(&i2c->dev, "%s:failed:%d\n", __func__, retval);
409                 retval = -EIO;
410         } else
411                 retval = 1;
412 exit:
413         mutex_unlock(&(pdata->rmi4_page_mutex));
414         return retval;
415 }
416
417
418 /**
419  * synpatics_rmi4_touchpad_report() - reports for the rmi4 touchpad device
420  * @pdata: pointer to synaptics_rmi4_data structure
421  * @rfi: pointer to synaptics_rmi4_fn structure
422  *
423  * This function calls to reports for the rmi4 touchpad device
424  */
425 static int synpatics_rmi4_touchpad_report(struct synaptics_rmi4_data *pdata,
426                                                 struct synaptics_rmi4_fn *rfi)
427 {
428         /* number of touch points - fingers down in this case */
429         int     touch_count = 0;
430         int     finger;
431         int     fingers_supported;
432         int     finger_registers;
433         int     reg;
434         int     finger_shift;
435         int     finger_status;
436         int     retval;
437         unsigned short  data_base_addr;
438         unsigned short  data_offset;
439         unsigned char   data_reg_blk_size;
440         unsigned char   values[2];
441         unsigned char   data[DATA_LEN];
442         int     x[RMI4_NUMBER_OF_MAX_FINGERS];
443         int     y[RMI4_NUMBER_OF_MAX_FINGERS];
444         int     wx[RMI4_NUMBER_OF_MAX_FINGERS];
445         int     wy[RMI4_NUMBER_OF_MAX_FINGERS];
446         struct  i2c_client *client = pdata->i2c_client;
447
448 #if (RMI4_MULTI_TOUCH != 0)
449         volatile enum synaptics_rmi4_touch_status cur_touch_status[SYNA_SUPPORT_POINT_MAX];
450         static volatile enum synaptics_rmi4_touch_status pre_touch_status[SYNA_SUPPORT_POINT_MAX];
451 #endif
452
453         /* get 2D sensor finger data */
454         /*
455          * First get the finger status field - the size of the finger status
456          * field is determined by the number of finger supporte - 2 bits per
457          * finger, so the number of registers to read is:
458          * registerCount = ceil(numberOfFingers/4).
459          * Read the required number of registers and check each 2 bit field to
460          * determine if a finger is down:
461          *      00 = finger not present,
462          *      01 = finger present and data accurate,
463          *      10 = finger present but data may not be accurate,
464          *      11 = reserved for product use.
465          */
466         fingers_supported       = rfi->num_of_data_points;
467         finger_registers        = (fingers_supported + 3)/4;
468         data_base_addr          = rfi->fn_desc.data_base_addr;
469         retval = synaptics_rmi4_i2c_block_read(pdata, data_base_addr, values,
470                                                         finger_registers);
471         if (retval != finger_registers) {
472                 dev_err(&client->dev, "%s:read status registers failed\n",
473                                                                 __func__);
474                 return 0;
475         }
476         /*
477          * For each finger present, read the proper number of registers
478          * to get absolute data.
479          */
480         data_reg_blk_size = rfi->size_of_data_register_block;
481
482         for (finger = 0; finger < fingers_supported; finger++) {
483                 //mutex_lock(&pdata->rmi4_wq_mutex);
484                 cur_touch_status[finger] = SYNA_TOUCH_ORIGINAL;
485                 //mutex_unlock(&pdata->rmi4_wq_mutex);
486         }
487
488         for (finger = 0; finger < fingers_supported; finger++) {
489                 /* determine which data byte the finger status is in */
490                 reg = finger/4;
491                 /* bit shift to get finger's status */
492                 finger_shift    = (finger % 4) * 2;
493                 finger_status   = (values[reg] >> finger_shift) & 3;
494                 /*
495                  * if finger status indicates a finger is present then
496                  * read the finger data and report it
497                  */
498                 if (finger_status == 1 || finger_status == 2) {
499                         rmi4_debug(DEBUG_TRACE, "---> finger[%d] press ??\n", finger);
500
501                         //mutex_lock(&pdata->rmi4_wq_mutex);
502                         cur_touch_status[finger] = SYNA_TOUCH_PRESS;
503                         //mutex_unlock(&pdata->rmi4_wq_mutex);
504
505                         /* Read the finger data */
506                         data_offset = data_base_addr +
507                                         ((finger * data_reg_blk_size) +
508                                         finger_registers);
509                         retval = synaptics_rmi4_i2c_block_read(pdata,
510                                                 data_offset, data, data_reg_blk_size);
511                         if (retval != data_reg_blk_size) {
512                                 printk(KERN_ERR "%s:read data failed\n", __func__);
513                                 //mutex_lock(&pdata->rmi4_wq_mutex);
514                                 cur_touch_status[finger] = SYNA_TOUCH_ORIGINAL;
515                                 //mutex_unlock(&pdata->rmi4_wq_mutex);
516                                 return 0;
517                         } else {
518                                 x[finger]       =
519                                         (data[0] << 4) | (data[2] & MASK_4BIT);
520                                 y[finger]       =
521                                         (data[1] << 4) |
522                                         ((data[2] >> 4) & MASK_4BIT);
523                                 wy[finger]      =
524                                                 (data[3] >> 4) & MASK_4BIT;
525                                 wx[finger]      =
526                                                 (data[3] & MASK_4BIT);
527
528                                 #if 0
529                                 if (x[finger] >= pdata->sensor_max_x)
530                                         rmi4_debug(DEBUG_TRACE, "x[%d] = 0x%x, y[%d] = 0x%x\n"
531                                                         "wx[%d] = 0x%x, wy[%d] = 0x%x\n"
532                                                         "sensor_max_x = 0x%x, sensor_max_y = 0x%x\n",
533                                                         finger, x[finger], finger, y[finger],
534                                                         finger, wx[finger], finger, wy[finger],
535                                                         pdata->sensor_max_x, pdata->sensor_max_y
536                                                 );
537                                 #endif
538
539                                 #if SYNA_AIXS_REVISE_FORCE
540                                 {
541                                         int axis_bak;
542
543                                         axis_bak = x[finger];
544
545                                         /*rmi4_acgzx: because these s7010 tp is assembling for a short time, sensor and lines isn't matching.
546                                          * force revise axis x and special handing.
547                                          */
548                                         x[finger] -= SYNA_AIXS_X_DELTA;
549                                         x[finger] = (x[finger] < 0) ? 0 : x[finger];
550
551                                         //rmi4_acgzx: special handing for sensor_max_x.
552                                         if (axis_bak >= pdata->sensor_max_x && wx[finger] <= SYNA_WIDTH_DELTA_FOR_X_MAX)
553                                                 x[finger] += SYNA_AIXS_X_DELTA;
554                                 }
555                                 #endif
556
557                                 /* rmi4_acgzx: do not support touch key, ignore outside touch */
558                                 if (x[finger] >= (pdata->sensor_max_x - SYNA_AIXS_X_DELTA) && wx[finger] <= SYNA_WIDTH_DELTA) {
559                                         //mutex_lock(&pdata->rmi4_wq_mutex);
560                                         cur_touch_status[finger] = SYNA_TOUCH_RELEASE;
561                                         //mutex_unlock(&pdata->rmi4_wq_mutex);
562                                         rmi4_debug(DEBUG_TRACE, "touch point beyond lcd display area.\n");
563                                 }
564
565                                 if (pdata->board->x_flip)
566                                         x[finger] =     pdata->sensor_max_x - x[finger];
567                                 if (pdata->board->y_flip)
568                                         y[finger] =     pdata->sensor_max_y - y[finger];
569
570                         }
571                         /* rmi4_acgzx: touch press,  touch_count increase */
572                         if (SYNA_TOUCH_PRESS == cur_touch_status[finger])
573                                 touch_count++;
574                 }
575                 else {
576                         //mutex_lock(&pdata->rmi4_wq_mutex);
577                         cur_touch_status[finger] = SYNA_TOUCH_RELEASE;
578                         //mutex_unlock(&pdata->rmi4_wq_mutex);
579                 }
580         }
581
582         for (finger = 0; finger < fingers_supported; finger++) {
583                 if (SYNA_TOUCH_PRESS == cur_touch_status[finger]) {
584                         rmi4_debug(DEBUG_TRACE, "====> finger[%d] press!\n", finger);
585                         /* number of active touch points */
586                         input_mt_slot(pdata->input_dev, finger);
587                         input_mt_report_slot_state(pdata->input_dev, MT_TOOL_FINGER, true);
588                         input_report_abs(pdata->input_dev, ABS_MT_TRACKING_ID, finger);
589                         input_report_abs(pdata->input_dev, ABS_MT_TOUCH_MAJOR, MAX_TOUCH_MAJOR);
590                         input_report_abs(pdata->input_dev, ABS_MT_POSITION_X, x[finger]);
591                         input_report_abs(pdata->input_dev, ABS_MT_POSITION_Y, y[finger]);
592                         //mutex_lock(&pdata->rmi4_wq_mutex);
593                         pre_touch_status[finger] = cur_touch_status[finger];
594                         //mutex_unlock(&pdata->rmi4_wq_mutex);
595                 }
596         }
597
598         for (finger = 0; finger < fingers_supported; finger++) {
599                 if (SYNA_TOUCH_RELEASE == cur_touch_status[finger] &&
600                         SYNA_TOUCH_PRESS == pre_touch_status[finger]) {
601                         rmi4_debug(DEBUG_TRACE, "====> finger[%d] release!\n", finger);
602                         /* number of active touch points */
603                         input_mt_slot(pdata->input_dev, finger);
604                         input_mt_report_slot_state(pdata->input_dev, MT_TOOL_FINGER, false);
605                         input_report_abs(pdata->input_dev, ABS_MT_TRACKING_ID, -1);
606                         //mutex_lock(&pdata->rmi4_wq_mutex);
607                         pre_touch_status[finger] = cur_touch_status[finger];
608                         //mutex_unlock(&pdata->rmi4_wq_mutex);
609                 }
610         }
611
612         /* sync after groups of events */
613         input_sync(pdata->input_dev);
614         /* return the number of touch points */
615         return touch_count;
616 }
617
618 /**
619  * synaptics_rmi4_report_device() - reports the rmi4 device
620  * @pdata: pointer to synaptics_rmi4_data structure
621  * @rfi: pointer to synaptics_rmi4_fn
622  *
623  * This function is used to call the report function of the rmi4 device.
624  */
625 static int synaptics_rmi4_report_device(struct synaptics_rmi4_data *pdata,
626                                         struct synaptics_rmi4_fn *rfi)
627 {
628         int touch = 0;
629         struct  i2c_client *client = pdata->i2c_client;
630         static int num_error_reports;
631         if (rfi->fn_number != SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM) {
632                 rmi4_debug(DEBUG_TRACE, "====> %s fn_number no equal enter!\n", __func__);
633                 num_error_reports++;
634                 if (num_error_reports < MAX_ERROR_REPORT)
635                         dev_err(&client->dev, "%s:report not supported\n",
636                                                                 __func__);
637         } else
638                 touch = synpatics_rmi4_touchpad_report(pdata, rfi);
639         return touch;
640 }
641 /**
642  * synaptics_rmi4_sensor_report() - reports to input subsystem
643  * @pdata: pointer to synaptics_rmi4_data structure
644  *
645  * This function is used to reads in all data sources and reports
646  * them to the input subsystem.
647  */
648 static int synaptics_rmi4_sensor_report(struct synaptics_rmi4_data *pdata)
649 {
650         unsigned char   intr_status[4];
651         /* number of touch points - fingers or buttons */
652         int touch = 0;
653         unsigned int retval;
654         struct synaptics_rmi4_fn                *rfi;
655         struct synaptics_rmi4_device_info       *rmi;
656         struct  i2c_client *client = pdata->i2c_client;
657
658         /*
659          * Get the interrupt status from the function $01
660          * control register+1 to find which source(s) were interrupting
661          * so we can read the data from the source(s) (2D sensor, buttons..)
662          */
663         retval = synaptics_rmi4_i2c_block_read(pdata,
664                                         pdata->fn01_data_base_addr + 1,
665                                         intr_status,
666                                         pdata->number_of_interrupt_register);
667         if (retval != pdata->number_of_interrupt_register) {
668                 dev_err(&client->dev,
669                                 "could not read interrupt status registers\n");
670                 return 0;
671         }
672         /*
673          * check each function that has data sources and if the interrupt for
674          * that triggered then call that RMI4 functions report() function to
675          * gather data and report it to the input subsystem
676          */
677         rmi = &(pdata->rmi4_mod_info);
678         list_for_each_entry(rfi, &rmi->support_fn_list, link) {
679                 if (rfi->num_of_data_sources) {
680                         if (intr_status[rfi->index_to_intr_reg] &
681                                                         rfi->intr_mask)
682                                 touch = synaptics_rmi4_report_device(pdata,
683                                                                         rfi);
684                 }
685         }
686         /* return the number of touch points */
687         return touch;
688 }
689
690 static void synaptics_rmi4_work_func(struct work_struct *work)
691 {
692         struct synaptics_rmi4_data *rmi4_data = container_of(work, struct synaptics_rmi4_data, work);
693         struct i2c_client *client = rmi4_data->i2c_client;
694         unsigned char intr_status;
695
696         /* rmi4_acgzx note: important!! disable tp interrupt avoid lost attn */
697         synaptics_rmi4_i2c_byte_write(rmi4_data, rmi4_data->fn01_data_base_addr + 1, 0x00);
698
699         synaptics_rmi4_sensor_report(rmi4_data);
700
701         enable_irq(client->irq);
702
703         /* rmi4_acgzx note: important!! enable tp interrupt again*/
704         synaptics_rmi4_i2c_byte_write(rmi4_data, rmi4_data->fn01_data_base_addr + 1, 0x0f);
705
706         /* rmi4_acgzx note: may be cause lose attn, gpio irq is low, that is small probability in rk30 platform.
707           * need read int status register again just in case.
708           */
709         if (0 == gpio_get_value(client->irq)) {
710                 rmi4_debug(DEBUG_TRACE, "----> %s: irq change low value enter! <----\n", __func__);
711                 synaptics_rmi4_i2c_block_read(rmi4_data,
712                                 rmi4_data->fn01_data_base_addr + 1,
713                                 &intr_status,
714                                 rmi4_data->number_of_interrupt_register);
715
716         }
717
718         //rmi4_debug(DEBUG_TRACE, "%s: exit!\n", __func__);     //rmi4_acgzx
719 }
720
721 /**
722  * synaptics_rmi4_irq() - thread function for rmi4 attention line
723  * @irq: irq value
724  * @data: void pointer
725  *
726  * This function is interrupt thread function. It just notifies the
727  * application layer that attention is required.
728  */
729 static irqreturn_t synaptics_rmi4_irq(int irq, void *data)
730 {
731         struct synaptics_rmi4_data *pdata = data;
732
733         //rmi4_debug(DEBUG_TRACE, "%s: enter!\n", __func__);    //rmi4_acgzx
734
735         disable_irq_nosync(irq);
736         queue_work(pdata->rmi4_wq, &pdata->work);
737
738         return IRQ_HANDLED;
739 }
740
741 /**
742  * synpatics_rmi4_touchpad_detect() - detects the rmi4 touchpad device
743  * @pdata: pointer to synaptics_rmi4_data structure
744  * @rfi: pointer to synaptics_rmi4_fn structure
745  * @fd: pointer to synaptics_rmi4_fn_desc structure
746  * @interruptcount: count the number of interrupts
747  *
748  * This function calls to detects the rmi4 touchpad device
749  */
750 static int synpatics_rmi4_touchpad_detect(struct synaptics_rmi4_data *pdata,
751                                         struct synaptics_rmi4_fn *rfi,
752                                         struct synaptics_rmi4_fn_desc *fd,
753                                         unsigned int interruptcount)
754 {
755         unsigned char   queries[QUERY_LEN];
756         unsigned short  intr_offset;
757         unsigned char   abs_data_size;
758         unsigned char   abs_data_blk_size;
759         unsigned char   egr_0, egr_1;
760         unsigned int    all_data_blk_size;
761         int     has_pinch, has_flick, has_tap;
762         int     has_tapandhold, has_doubletap;
763         int     has_earlytap, has_press;
764         int     has_palmdetect, has_rotate;
765         int     has_rel;
766         int     i;
767         int     retval;
768         struct  i2c_client *client = pdata->i2c_client;
769
770         rfi->fn_desc.query_base_addr    = fd->query_base_addr;
771         rfi->fn_desc.data_base_addr     = fd->data_base_addr;
772         rfi->fn_desc.intr_src_count     = fd->intr_src_count;
773         rfi->fn_desc.fn_number          = fd->fn_number;
774         rfi->fn_number                  = fd->fn_number;
775         rfi->num_of_data_sources        = fd->intr_src_count;
776         rfi->fn_desc.ctrl_base_addr     = fd->ctrl_base_addr;
777         rfi->fn_desc.cmd_base_addr      = fd->cmd_base_addr;
778
779         /*
780          * need to get number of fingers supported, data size, etc.
781          * to be used when getting data since the number of registers to
782          * read depends on the number of fingers supported and data size.
783          */
784         retval = synaptics_rmi4_i2c_block_read(pdata, fd->query_base_addr,
785                                                         queries,
786                                                         sizeof(queries));
787         if (retval != sizeof(queries)) {
788                 dev_err(&client->dev, "%s:read function query registers\n",
789                                                         __func__);
790                 return retval;
791         }
792         /*
793          * 2D data sources have only 3 bits for the number of fingers
794          * supported - so the encoding is a bit weird.
795          */
796         if ((queries[1] & MASK_3BIT) <= 4)
797                 /* add 1 since zero based */
798                 rfi->num_of_data_points = (queries[1] & MASK_3BIT) + 1;
799         else {
800                 /*
801                  * a value of 5 is up to 10 fingers - 6 and 7 are reserved
802                  * (shouldn't get these i int retval;n a normal 2D source).
803                  */
804                 if ((queries[1] & MASK_3BIT) == 5)
805                         rfi->num_of_data_points = 10;
806         }
807         /* Need to get interrupt info for handling interrupts */
808         rfi->index_to_intr_reg = (interruptcount + 7)/8;
809         if (rfi->index_to_intr_reg != 0)
810                 rfi->index_to_intr_reg -= 1;
811         /*
812          * loop through interrupts for each source in fn $11
813          * and or in a bit to the interrupt mask for each.
814          */
815         intr_offset = interruptcount % 8;
816         rfi->intr_mask = 0;
817         for (i = intr_offset;
818                 i < ((fd->intr_src_count & MASK_3BIT) + intr_offset); i++)
819                 rfi->intr_mask |= 1 << i;
820
821         /* Size of just the absolute data for one finger */
822         abs_data_size   = queries[5] & MASK_2BIT;
823         /* One each for X and Y, one for LSB for X & Y, one for W, one for Z */
824         abs_data_blk_size = 3 + (2 * (abs_data_size == 0 ? 1 : 0));
825         rfi->size_of_data_register_block = abs_data_blk_size;
826
827         /*
828          * need to determine the size of data to read - this depends on
829          * conditions such as whether Relative data is reported and if Gesture
830          * data is reported.
831          */
832         egr_0 = queries[7];
833         egr_1 = queries[8];
834
835         /*
836          * Get info about what EGR data is supported, whether it has
837          * Relative data supported, etc.
838          */
839         has_pinch       = egr_0 & HAS_PINCH;
840         has_flick       = egr_0 & HAS_FLICK;
841         has_tap         = egr_0 & HAS_TAP;
842         has_earlytap    = egr_0 & HAS_EARLYTAP;
843         has_press       = egr_0 & HAS_PRESS;
844         has_rotate      = egr_1 & HAS_ROTATE;
845         has_rel         = queries[1] & HAS_RELEASE;
846         has_tapandhold  = egr_0 & HAS_TAPANDHOLD;
847         has_doubletap   = egr_0 & HAS_DOUBLETAP;
848         has_palmdetect  = egr_1 & HAS_PALMDETECT;
849
850         /*
851          * Size of all data including finger status, absolute data for each
852          * finger, relative data and EGR data
853          */
854         all_data_blk_size =
855                 /* finger status, four fingers per register */
856                 ((rfi->num_of_data_points + 3) / 4) +
857                 /* absolute data, per finger times number of fingers */
858                 (abs_data_blk_size * rfi->num_of_data_points) +
859                 /*
860                  * two relative registers (if relative is being reported)
861                  */
862                 2 * has_rel +
863                 /*
864                  * F11_2D_data8 is only present if the egr_0
865                  * register is non-zero.
866                  */
867                 !!(egr_0) +
868                 /*
869                  * F11_2D_data9 is only present if either egr_0 or
870                  * egr_1 registers are non-zero.
871                  */
872                 (egr_0 || egr_1) +
873                 /*
874                  * F11_2D_data10 is only present if EGR_PINCH or EGR_FLICK of
875                  * egr_0 reports as 1.
876                  */
877                 !!(has_pinch | has_flick) +
878                 /*
879                  * F11_2D_data11 and F11_2D_data12 are only present if
880                  * EGR_FLICK of egr_0 reports as 1.
881                  */
882                 2 * !!(has_flick);
883         return retval;
884 }
885
886 /**
887  * synpatics_rmi4_touchpad_config() - confiures the rmi4 touchpad device
888  * @pdata: pointer to synaptics_rmi4_data structure
889  * @rfi: pointer to synaptics_rmi4_fn structure
890  *
891  * This function calls to confiures the rmi4 touchpad device
892  */
893 int synpatics_rmi4_touchpad_config(struct synaptics_rmi4_data *pdata,
894                                                 struct synaptics_rmi4_fn *rfi)
895 {
896         /*
897          * For the data source - print info and do any
898          * source specific configuration.
899          */
900         unsigned char data[BUF_LEN];
901         int retval = 0;
902         struct  i2c_client *client = pdata->i2c_client;
903
904         /* Get and print some info about the data source... */
905         /* To Query 2D devices we need to read from the address obtained
906          * from the function descriptor stored in the RMI function info.
907          */
908         retval = synaptics_rmi4_i2c_block_read(pdata,
909                                                 rfi->fn_desc.query_base_addr,
910                                                 data, QUERY_LEN);
911         if (retval != QUERY_LEN)
912                 dev_err(&client->dev, "%s:read query registers failed\n",
913                                                                 __func__);
914         else {
915
916                 //hhb@rock-chips.com  add virtual_keys function
917                 if (pdata->board->virtual_keys == true) {
918
919                         int w, h;
920                         char buf[2];
921
922                         pdata->sensor_max_x = pdata->board->lcd_width;
923                         pdata->sensor_max_y = pdata->board->lcd_height;
924
925                         w = pdata->board->lcd_width + pdata->board->w_delta;
926                         h = pdata->board->lcd_height + pdata->board->h_delta;
927
928 #if 0
929                         buf[0] = w & MASK_8BIT;
930                         buf[1] = (w >> 8) & MASK_4BIT;
931                         synaptics_rmi4_i2c_block_write(pdata, rfi->fn_desc.ctrl_base_addr + 6, buf, 2);
932                         buf[0] = h & MASK_8BIT;
933                         buf[1] = (h >> 8) & MASK_4BIT;
934                         synaptics_rmi4_i2c_block_write(pdata, rfi->fn_desc.ctrl_base_addr + 8, buf, 2);
935 #else
936                         synaptics_rmi4_i2c_byte_write(pdata, rfi->fn_desc.ctrl_base_addr + 6, w & MASK_8BIT);
937                         synaptics_rmi4_i2c_byte_write(pdata, rfi->fn_desc.ctrl_base_addr + 7, (w >> 8) & MASK_4BIT);
938
939                         synaptics_rmi4_i2c_byte_write(pdata, rfi->fn_desc.ctrl_base_addr + 8, h & MASK_8BIT);
940                         synaptics_rmi4_i2c_byte_write(pdata, rfi->fn_desc.ctrl_base_addr + 9, (h >> 8) & MASK_4BIT);
941 #endif
942
943                         retval = synaptics_rmi4_i2c_block_read(pdata,
944                                                         rfi->fn_desc.ctrl_base_addr,
945                                                         data, DATA_BUF_LEN);
946                         if (retval != DATA_BUF_LEN) {
947                                 dev_err(&client->dev,
948                                         "%s:read control registers failed\n",
949                                                                         __func__);
950                                 return retval;
951                         }
952
953                         w = ((data[6] & MASK_8BIT) << 0) |
954                                                         ((data[7] & MASK_4BIT) << 8);
955                         h = ((data[8] & MASK_8BIT) << 0) |
956                                                         ((data[9] & MASK_4BIT) << 8);
957
958                         if(pdata->board->lcd_width != w || pdata->board->lcd_height != h){
959                                 printk("set tp x:%d, y:%d, but read x:%d, y:%d\n", pdata->board->lcd_width, pdata->board->lcd_height, w, h);
960                         }
961
962                 } else {
963
964                         retval = synaptics_rmi4_i2c_block_read(pdata,
965                                                         rfi->fn_desc.ctrl_base_addr,
966                                                         data, DATA_BUF_LEN);
967                         if (retval != DATA_BUF_LEN) {
968                                 dev_err(&client->dev,
969                                         "%s:read control registers failed\n",
970                                                                         __func__);
971                                 return retval;
972                         }
973
974                         /* Store these for use later*/
975
976                         pdata->sensor_max_x = ((data[6] & MASK_8BIT) << 0) |
977                                                         ((data[7] & MASK_4BIT) << 8);
978                         pdata->sensor_max_y = ((data[8] & MASK_8BIT) << 0) |
979                                                         ((data[9] & MASK_4BIT) << 8);
980                 }
981
982                 rmi4_debug(DEBUG_TRACE, "sensor_max_x = 0x%x, sensor_max_y = 0x%x.\n",
983                                         pdata->sensor_max_x, pdata->sensor_max_y);
984         }
985         return retval;
986 }
987
988 /**
989  * synaptics_rmi4_i2c_query_device() - query the rmi4 device
990  * @pdata: pointer to synaptics_rmi4_data structure
991  *
992  * This function is used to query the rmi4 device.
993  */
994 static int synaptics_rmi4_i2c_query_device(struct synaptics_rmi4_data *pdata)
995 {
996         int i;
997         int retval;
998         unsigned char std_queries[STD_QUERY_LEN];
999         unsigned char intr_count = 0;
1000         int data_sources = 0;
1001         unsigned int ctrl_offset;
1002         struct synaptics_rmi4_fn *rfi;
1003         struct synaptics_rmi4_fn_desc   rmi_fd;
1004         struct synaptics_rmi4_device_info *rmi;
1005         struct  i2c_client *client = pdata->i2c_client;
1006
1007         /*
1008          * init the physical drivers RMI module
1009          * info list of functions
1010          */
1011         INIT_LIST_HEAD(&pdata->rmi4_mod_info.support_fn_list);
1012
1013         /*
1014          * Read the Page Descriptor Table to determine what functions
1015          * are present
1016          */
1017         for (i = PDT_START_SCAN_LOCATION; i > PDT_END_SCAN_LOCATION;
1018                                                 i -= PDT_ENTRY_SIZE) {
1019                 retval = synaptics_rmi4_i2c_block_read(pdata, i,
1020                                                 (unsigned char *)&rmi_fd,
1021                                                 sizeof(rmi_fd));
1022                 if (retval != sizeof(rmi_fd)) {
1023                         /* failed to read next PDT entry */
1024                         dev_err(&client->dev, "%s: read error\n", __func__);
1025                         return -EIO;
1026                 }
1027
1028                 rmi4_debug(DEBUG_TRACE, "----> query_base_addr = 0x%x\n"
1029                                         "cmd_base_addr = 0x%x\n"
1030                                         "ctrl_base_addr = 0x%x\n"
1031                                         "data_base_addr = 0x%x\n"
1032                                         "intr_src_count = 0x%x\n"
1033                                         "fn_number = 0x%x\n",
1034                                         rmi_fd.query_base_addr,
1035                                         rmi_fd.cmd_base_addr,
1036                                         rmi_fd.ctrl_base_addr,
1037                                         rmi_fd.data_base_addr,
1038                                         rmi_fd.intr_src_count,
1039                                         rmi_fd.fn_number
1040                 );
1041
1042                 rfi = NULL;
1043                 if (rmi_fd.fn_number) {
1044                         switch (rmi_fd.fn_number & MASK_8BIT) {
1045                         case SYNAPTICS_RMI4_DEVICE_CONTROL_FUNC_NUM:
1046                                 pdata->fn01_query_base_addr =
1047                                                 rmi_fd.query_base_addr;
1048                                 pdata->fn01_ctrl_base_addr =
1049                                                 rmi_fd.ctrl_base_addr;
1050                                 pdata->fn01_data_base_addr =
1051                                                 rmi_fd.data_base_addr;
1052
1053                                 rmi4_debug(DEBUG_TRACE, "----> fn01_query_base_addr = 0x%x\n,"
1054                                                         "fn01_ctrl_base_addr = 0x%x\n,"
1055                                                         "fn01_data_base_addr = 0x%x\n,",
1056                                                         pdata->fn01_query_base_addr,
1057                                                         pdata->fn01_ctrl_base_addr,
1058                                                         pdata->fn01_data_base_addr
1059                                 );
1060                                 break;
1061                         case SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM:
1062                                 if (rmi_fd.intr_src_count) {
1063                                         rfi = kmalloc(sizeof(*rfi),
1064                                                                 GFP_KERNEL);
1065                                         if (!rfi) {
1066                                                 dev_err(&client->dev,
1067                                                         "%s:kmalloc failed\n",
1068                                                                 __func__);
1069                                                         return -ENOMEM;
1070                                         }
1071                                         retval = synpatics_rmi4_touchpad_detect
1072                                                                 (pdata, rfi,
1073                                                                 &rmi_fd,
1074                                                                 intr_count);
1075                                         if (retval < 0) {
1076                                                 kfree(rfi);
1077                                                 return retval;
1078                                         }
1079                                 }
1080                                 break;
1081                         }
1082                         /* interrupt count for next iteration */
1083                         intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
1084                         /*
1085                          * We only want to add functions to the list
1086                          * that have data associated with them.
1087                          */
1088                         if (rfi && rmi_fd.intr_src_count) {
1089                                 /* link this function info to the RMI module */
1090                                 mutex_lock(&(pdata->fn_list_mutex));
1091                                 list_add_tail(&rfi->link,
1092                                         &pdata->rmi4_mod_info.support_fn_list);
1093                                 mutex_unlock(&(pdata->fn_list_mutex));
1094                         }
1095                 } else {
1096                         /*
1097                          * A zero in the function number
1098                          * signals the end of the PDT
1099                          */
1100                         dev_dbg(&client->dev,
1101                                 "%s:end of PDT\n", __func__);
1102                         break;
1103                 }
1104         }
1105         /*
1106          * calculate the interrupt register count - used in the
1107          * ISR to read the correct number of interrupt registers
1108          */
1109         pdata->number_of_interrupt_register = (intr_count + 7) / 8;
1110         /*
1111          * Function $01 will be used to query the product properties,
1112          * and product ID  so we had to read the PDT above first to get
1113          * the Fn $01 query address and prior to filling in the product
1114          * info. NOTE: Even an unflashed device will still have FN $01.
1115          */
1116
1117         /* Load up the standard queries and get the RMI4 module info */
1118         retval = synaptics_rmi4_i2c_block_read(pdata,
1119                                         pdata->fn01_query_base_addr,
1120                                         std_queries,
1121                                         sizeof(std_queries));
1122         if (retval != sizeof(std_queries)) {
1123                 dev_err(&client->dev, "%s:Failed reading queries\n",
1124                                                         __func__);
1125                  return -EIO;
1126         }
1127
1128         /* Currently supported RMI version is 4.0 */
1129         pdata->rmi4_mod_info.version_major      = 4;
1130         pdata->rmi4_mod_info.version_minor      = 0;
1131         /*
1132          * get manufacturer id, product_props, product info,
1133          * date code, tester id, serial num and product id (name)
1134          */
1135         pdata->rmi4_mod_info.manufacturer_id    = std_queries[0];
1136         pdata->rmi4_mod_info.product_props      = std_queries[1];
1137         pdata->rmi4_mod_info.product_info[0]    = std_queries[2];
1138         pdata->rmi4_mod_info.product_info[1]    = std_queries[3];
1139         /* year - 2001-2032 */
1140         pdata->rmi4_mod_info.date_code[0]       = std_queries[4] & MASK_5BIT;
1141         /* month - 1-12 */
1142         pdata->rmi4_mod_info.date_code[1]       = std_queries[5] & MASK_4BIT;
1143         /* day - 1-31 */
1144         pdata->rmi4_mod_info.date_code[2]       = std_queries[6] & MASK_5BIT;
1145         pdata->rmi4_mod_info.tester_id = ((std_queries[7] & MASK_7BIT) << 8) |
1146                                                 (std_queries[8] & MASK_7BIT);
1147         pdata->rmi4_mod_info.serial_number =
1148                 ((std_queries[9] & MASK_7BIT) << 8) |
1149                                 (std_queries[10] & MASK_7BIT);
1150         memcpy(pdata->rmi4_mod_info.product_id_string, &std_queries[11], 10);
1151
1152         rmi4_debug(DEBUG_TRACE, "version_major = 0x%x, version_minor = 0x%x.\n"
1153                         "manufacturer_id = 0x%x, product_props = 0x%x.\n"
1154                         "product_info[1] = 0x%x, product_info[0] = 0x%x.\n"
1155                         "date_code[2] = 0x%x, date_code[1] = 0x%x, date_code[0] = 0x%x.\n"
1156                         "tester_id = 0x%x, serial_number = 0x%x.\n"
1157                         "product_id_string = %s.\n",
1158                         pdata->rmi4_mod_info.version_major, pdata->rmi4_mod_info.version_minor,
1159                         pdata->rmi4_mod_info.manufacturer_id, pdata->rmi4_mod_info.product_props,
1160                         pdata->rmi4_mod_info.product_info[1], pdata->rmi4_mod_info.product_info[0],
1161                         pdata->rmi4_mod_info.date_code[2], pdata->rmi4_mod_info.date_code[1], pdata->rmi4_mod_info.date_code[0],
1162                         pdata->rmi4_mod_info.tester_id, pdata->rmi4_mod_info.serial_number,
1163                         pdata->rmi4_mod_info.product_id_string
1164         );
1165
1166         /* Check if this is a Synaptics device - report if not. */
1167         if (pdata->rmi4_mod_info.manufacturer_id != 1)
1168                 dev_err(&client->dev, "%s: non-Synaptics mfg id:%d\n",
1169                         __func__, pdata->rmi4_mod_info.manufacturer_id);
1170
1171         list_for_each_entry(rfi, &pdata->rmi4_mod_info.support_fn_list, link)
1172                 data_sources += rfi->num_of_data_sources;
1173         if (data_sources) {
1174                 rmi = &(pdata->rmi4_mod_info);
1175                 list_for_each_entry(rfi, &rmi->support_fn_list, link) {
1176                         if (rfi->num_of_data_sources) {
1177                                 if (rfi->fn_number ==
1178                                         SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM) {
1179                                         retval = synpatics_rmi4_touchpad_config
1180                                                                 (pdata, rfi);
1181                                         if (retval < 0)
1182                                                 return retval;
1183                                 } else
1184                                         dev_err(&client->dev,
1185                                                 "%s:fn_number not supported\n",
1186                                                                 __func__);
1187                                 /*
1188                                  * Turn on interrupts for this
1189                                  * function's data sources.
1190                                  */
1191                                 ctrl_offset = pdata->fn01_ctrl_base_addr + 1 +
1192                                                         rfi->index_to_intr_reg;
1193
1194                                 rmi4_debug(DEBUG_TRACE, "----> fn01_ctrl_base_addr = 0x%x, intr_mask = 0x%x\n"
1195                                         "index_to_intr_reg = 0x%x, ctrl_offset = 0x%x.\n",
1196                                         pdata->fn01_ctrl_base_addr, rfi->intr_mask,
1197                                         rfi->index_to_intr_reg, ctrl_offset
1198                                 );
1199
1200                                 retval = synaptics_rmi4_i2c_byte_write(pdata,
1201                                                         ctrl_offset,
1202                                                         rfi->intr_mask);
1203                                 if (retval < 0)
1204                                         return retval;
1205                         }
1206                 }
1207         }
1208         return 0;
1209 }
1210
1211
1212 /**
1213  * synaptics_rmi4_probe() - Initialze the i2c-client touchscreen driver
1214  * @i2c: i2c client structure pointer
1215  * @id:i2c device id pointer
1216  *
1217  * This function will allocate and initialize the instance
1218  * data and request the irq and set the instance data as the clients
1219  * platform data then register the physical driver which will do a scan of
1220  * the rmi4 Physical Device Table and enumerate any rmi4 functions that
1221  * have data sources associated with them.
1222  */
1223 static int __devinit synaptics_rmi4_probe
1224         (struct i2c_client *client, const struct i2c_device_id *dev_id)
1225 {
1226         int retval;
1227         unsigned char intr_status[4];
1228         struct synaptics_rmi4_data *rmi4_data;
1229         struct synaptics_rmi4_platform_data *platformdata =
1230                                                 client->dev.platform_data;
1231
1232 #if (SYNA_I2C_BUS_IO != 0)
1233         if (!i2c_check_functionality(client->adapter,
1234                                         I2C_FUNC_I2C)) {
1235                 dev_err(&client->dev, "i2c smbus byte data not supported\n");
1236                 return -EIO;
1237         }
1238 #else
1239         if (!i2c_check_functionality(client->adapter,
1240                                         I2C_FUNC_SMBUS_BYTE_DATA)) {
1241                 dev_err(&client->dev, "i2c smbus byte data not supported\n");
1242                 return -EIO;
1243         }
1244 #endif
1245
1246         if (!platformdata) {
1247                 dev_err(&client->dev, "%s: no platform data\n", __func__);
1248                 return -EINVAL;
1249         }
1250
1251         //if (platformdata->init_hw)
1252         //      platformdata->init_hw();
1253
1254         platformdata->irq_number = gpio_to_irq(client->irq);
1255         //platformdata->x_flip = false;                // hhb
1256         //platformdata->y_flip = false;
1257         //platformdata->irq_type = IRQF_TRIGGER_FALLING;
1258         //platformdata->regulator_en = false;
1259
1260         rmi4_debug(DEBUG_TRACE, "irq_number = %d, client->irq = %d\n",
1261                 platformdata->irq_number, client->irq);
1262
1263         /* Allocate and initialize the instance data for this client */
1264         rmi4_data = kzalloc(sizeof(struct synaptics_rmi4_data) * 2,
1265                                                         GFP_KERNEL);
1266         if (!rmi4_data) {
1267                 dev_err(&client->dev, "%s: no memory allocated\n", __func__);
1268                 return -ENOMEM;
1269         }
1270
1271         rmi4_data->input_dev = input_allocate_device();
1272         if (rmi4_data->input_dev == NULL) {
1273                 dev_err(&client->dev, "%s:input device alloc failed\n",
1274                                                 __func__);
1275                 retval = -ENOMEM;
1276                 goto err_input;
1277         }
1278
1279         if (platformdata->regulator_en) {
1280                 rmi4_data->regulator = regulator_get(&client->dev, "vdd");
1281                 if (IS_ERR(rmi4_data->regulator)) {
1282                         dev_err(&client->dev, "%s:get regulator failed\n",
1283                                                                 __func__);
1284                         retval = PTR_ERR(rmi4_data->regulator);
1285                         goto err_regulator;
1286                 }
1287                 regulator_enable(rmi4_data->regulator);
1288         }
1289
1290         init_waitqueue_head(&rmi4_data->wait);
1291         /*
1292          * Copy i2c_client pointer into RTID's i2c_client pointer for
1293          * later use in rmi4_read, rmi4_write, etc.
1294          */
1295         rmi4_data->i2c_client           = client;
1296         /* So we set the page correctly the first time */
1297         rmi4_data->current_page         = MASK_16BIT;
1298         rmi4_data->board                = platformdata;
1299         rmi4_data->touch_stopped        = false;
1300
1301         /* init the mutexes for maintain the lists */
1302         mutex_init(&(rmi4_data->fn_list_mutex));
1303         mutex_init(&(rmi4_data->rmi4_page_mutex));
1304
1305         mutex_init(&(rmi4_data->rmi4_wq_mutex));
1306
1307         /*
1308          * Register physical driver - this will call the detect function that
1309          * will then scan the device and determine the supported
1310          * rmi4 functions.
1311          */
1312         retval = synaptics_rmi4_i2c_query_device(rmi4_data);
1313         if (retval) {
1314                 dev_err(&client->dev, "%s: rmi4 query device failed\n",
1315                                                         __func__);
1316                 goto err_query_dev;
1317         }
1318
1319         /* Store the instance data in the i2c_client */
1320         i2c_set_clientdata(client, rmi4_data);
1321
1322         /*initialize the input device parameters */
1323         rmi4_data->input_dev->name      = DRIVER_NAME;
1324         rmi4_data->input_dev->phys      = "Synaptics_Clearpad";
1325         rmi4_data->input_dev->id.bustype = BUS_I2C;
1326         rmi4_data->input_dev->dev.parent = &client->dev;
1327         input_set_drvdata(rmi4_data->input_dev, rmi4_data);
1328
1329         /* Initialize the function handlers for rmi4 */
1330
1331         __set_bit(INPUT_PROP_DIRECT, rmi4_data->input_dev->propbit);
1332         __set_bit(EV_ABS, rmi4_data->input_dev->evbit);
1333
1334         input_mt_init_slots(rmi4_data->input_dev, SYNA_SUPPORT_POINT_MAX);
1335
1336         input_set_abs_params(rmi4_data->input_dev, ABS_MT_POSITION_X, 0,
1337                                         rmi4_data->sensor_max_x, 0, 0);
1338         input_set_abs_params(rmi4_data->input_dev, ABS_MT_POSITION_Y, 0,
1339                                         rmi4_data->sensor_max_y, 0, 0);
1340         input_set_abs_params(rmi4_data->input_dev, ABS_MT_TOUCH_MAJOR, 0,
1341                                                 MAX_TOUCH_MAJOR, 0, 0);
1342
1343         /* Clear interrupts */
1344         synaptics_rmi4_i2c_block_read(rmi4_data,
1345                         rmi4_data->fn01_data_base_addr + 1, intr_status,
1346                                 rmi4_data->number_of_interrupt_register);
1347
1348         rmi4_debug(DEBUG_TRACE, "intr_status: %d %d %d %d, number_of_interrupt_register = %d\n",
1349                 intr_status[0], intr_status[1], intr_status[2], intr_status[3],
1350                 rmi4_data->number_of_interrupt_register);
1351
1352         rmi4_data->rmi4_wq = create_workqueue("rmi4_wq");
1353         rmi4_debug(DEBUG_TRACE, "----> create_workqueue start!\n");
1354
1355         INIT_WORK(&rmi4_data->work, synaptics_rmi4_work_func);
1356
1357         retval = request_threaded_irq(platformdata->irq_number, NULL,
1358                                         synaptics_rmi4_irq,
1359                                         platformdata->irq_type,
1360                                         DRIVER_NAME, rmi4_data);
1361         if (retval) {
1362                 dev_err(&client->dev, "%s:Unable to get attn irq %d\n",
1363                                 __func__, platformdata->irq_number);
1364                 goto err_query_dev;
1365         }
1366
1367         retval = input_register_device(rmi4_data->input_dev);
1368         if (retval) {
1369                 dev_err(&client->dev, "%s:input register failed\n", __func__);
1370                 goto err_free_irq;
1371         }
1372
1373 #ifdef CONFIG_HAS_EARLYSUSPEND
1374         syna_client = client;
1375
1376     rmi4_data->m_early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 1;
1377     rmi4_data->m_early_suspend.suspend = synaptics_rmi4_early_suspend;
1378     rmi4_data->m_early_suspend.resume = synaptics_rmi4_late_resume;
1379     register_early_suspend(&rmi4_data->m_early_suspend);
1380 #else
1381         rmi4_data->m_early_suspend.level = -1;
1382     rmi4_data->m_early_suspend.suspend = NULL;
1383     rmi4_data->m_early_suspend.resume = NULL;
1384 #endif
1385
1386         return retval;
1387
1388 err_free_irq:
1389         free_irq(platformdata->irq_number, rmi4_data);
1390 err_query_dev:
1391         if (platformdata->regulator_en) {
1392                 regulator_disable(rmi4_data->regulator);
1393                 regulator_put(rmi4_data->regulator);
1394         }
1395 err_regulator:
1396         input_free_device(rmi4_data->input_dev);
1397         rmi4_data->input_dev = NULL;
1398 err_input:
1399         kfree(rmi4_data);
1400
1401         return retval;
1402 }
1403 /**
1404  * synaptics_rmi4_remove() - Removes the i2c-client touchscreen driver
1405  * @client: i2c client structure pointer
1406  *
1407  * This function uses to remove the i2c-client
1408  * touchscreen driver and returns integer.
1409  */
1410 static int __devexit synaptics_rmi4_remove(struct i2c_client *client)
1411 {
1412         struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
1413         const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board;
1414
1415 #ifdef CONFIG_HAS_EARLYSUSPEND
1416         if (rmi4_data->m_early_suspend.suspend && rmi4_data->m_early_suspend.resume)
1417                 unregister_early_suspend(&rmi4_data->m_early_suspend);
1418 #endif
1419
1420         rmi4_data->touch_stopped = true;
1421         wake_up(&rmi4_data->wait);
1422         free_irq(pdata->irq_number, rmi4_data);
1423         input_unregister_device(rmi4_data->input_dev);
1424         if (pdata->regulator_en) {
1425                 regulator_disable(rmi4_data->regulator);
1426                 regulator_put(rmi4_data->regulator);
1427         }
1428         kfree(rmi4_data);
1429
1430         return 0;
1431 }
1432
1433 #ifdef CONFIG_HAS_EARLYSUSPEND
1434 static void synaptics_rmi4_early_suspend(struct early_suspend *h)
1435 {
1436         struct i2c_client *client = syna_client;
1437         struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
1438
1439         rmi4_debug(DEBUG_TRACE, "%s: enter!\n", __func__);      //rmi4_acgzx
1440
1441         disable_irq(client->irq);
1442         synaptics_rmi4_i2c_byte_write(rmi4_data, rmi4_data->fn01_data_base_addr + 1, 0x00);
1443
1444 }
1445 static void synaptics_rmi4_late_resume(struct early_suspend *h)
1446 {
1447         struct i2c_client *client = syna_client;
1448         struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
1449         int retval;
1450         unsigned char intr_status;
1451
1452         rmi4_debug(DEBUG_TRACE, "%s: enter!\n", __func__);      //rmi4_acgzx
1453
1454         enable_irq(client->irq);
1455
1456         retval = synaptics_rmi4_i2c_block_read(rmi4_data,
1457                                 rmi4_data->fn01_data_base_addr + 1,
1458                                 &intr_status,
1459                                 rmi4_data->number_of_interrupt_register);
1460         if (retval < 0)
1461                 return;
1462
1463         synaptics_rmi4_i2c_byte_write(rmi4_data, rmi4_data->fn01_data_base_addr + 1, 0x0f);
1464
1465 }
1466 #endif
1467
1468 #ifdef CONFIG_PM
1469 /**
1470  * synaptics_rmi4_suspend() - suspend the touch screen controller
1471  * @dev: pointer to device structure
1472  *
1473  * This function is used to suspend the
1474  * touch panel controller and returns integer
1475  */
1476 static int synaptics_rmi4_suspend(struct device *dev)
1477 {
1478         /* Touch sleep mode */
1479         int retval;
1480         unsigned char intr_status;
1481         struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
1482         const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board;
1483
1484         rmi4_debug(DEBUG_TRACE, "%s: enter!\n", __func__);      //rmi4_acgzx
1485
1486         rmi4_data->touch_stopped = true;
1487         disable_irq(pdata->irq_number);
1488
1489         retval = synaptics_rmi4_i2c_block_read(rmi4_data,
1490                                 rmi4_data->fn01_data_base_addr + 1,
1491                                 &intr_status,
1492                                 rmi4_data->number_of_interrupt_register);
1493         if (retval < 0)
1494                 return retval;
1495
1496         retval = synaptics_rmi4_i2c_byte_write(rmi4_data,
1497                                         rmi4_data->fn01_ctrl_base_addr + 1,
1498                                         (intr_status & ~TOUCHPAD_CTRL_INTR));
1499         if (retval < 0)
1500                 return retval;
1501
1502         if (pdata->regulator_en)
1503                 regulator_disable(rmi4_data->regulator);
1504
1505         return 0;
1506 }
1507 /**
1508  * synaptics_rmi4_resume() - resume the touch screen controller
1509  * @dev: pointer to device structure
1510  *
1511  * This function is used to resume the touch panel
1512  * controller and returns integer.
1513  */
1514 static int synaptics_rmi4_resume(struct device *dev)
1515 {
1516         int retval;
1517         unsigned char intr_status;
1518         struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
1519         const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board;
1520
1521         rmi4_debug(DEBUG_TRACE, "%s: enter!\n", __func__);      //rmi4_acgzx
1522
1523         if (pdata->regulator_en)
1524                 regulator_enable(rmi4_data->regulator);
1525
1526         enable_irq(pdata->irq_number);
1527         rmi4_data->touch_stopped = false;
1528
1529         retval = synaptics_rmi4_i2c_block_read(rmi4_data,
1530                                 rmi4_data->fn01_data_base_addr + 1,
1531                                 &intr_status,
1532                                 rmi4_data->number_of_interrupt_register);
1533         if (retval < 0)
1534                 return retval;
1535
1536         retval = synaptics_rmi4_i2c_byte_write(rmi4_data,
1537                                         rmi4_data->fn01_ctrl_base_addr + 1,
1538                                         (intr_status | TOUCHPAD_CTRL_INTR));
1539         if (retval < 0)
1540                 return retval;
1541
1542         return 0;
1543 }
1544
1545 static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = {
1546         .suspend = synaptics_rmi4_suspend,
1547         .resume  = synaptics_rmi4_resume,
1548 };
1549 #endif
1550
1551 static const struct i2c_device_id synaptics_rmi4_id_table[] = {
1552         { DRIVER_NAME, 0 },
1553         { },
1554 };
1555 MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table);
1556
1557 static struct i2c_driver synaptics_rmi4_driver = {
1558         .driver = {
1559                 .name   =       DRIVER_NAME,
1560                 .owner  =       THIS_MODULE,
1561 #if (0 == CONFIG_HAS_EARLYSUSPEND)              //rmi4_acgzx
1562 #ifdef CONFIG_PM
1563                 .pm     =       &synaptics_rmi4_dev_pm_ops,
1564 #endif
1565 #endif
1566         },
1567         .probe          =       synaptics_rmi4_probe,
1568         .remove         =       __devexit_p(synaptics_rmi4_remove),
1569         .id_table       =       synaptics_rmi4_id_table,
1570 };
1571 /**
1572  * synaptics_rmi4_init() - Initialize the touchscreen driver
1573  *
1574  * This function uses to initializes the synaptics
1575  * touchscreen driver and returns integer.
1576  */
1577 static int __init synaptics_rmi4_init(void)
1578 {
1579         return i2c_add_driver(&synaptics_rmi4_driver);
1580 }
1581 /**
1582  * synaptics_rmi4_exit() - De-initialize the touchscreen driver
1583  *
1584  * This function uses to de-initialize the synaptics
1585  * touchscreen driver and returns none.
1586  */
1587 static void __exit synaptics_rmi4_exit(void)
1588 {
1589         i2c_del_driver(&synaptics_rmi4_driver);
1590 }
1591
1592
1593 module_init(synaptics_rmi4_init);
1594 module_exit(synaptics_rmi4_exit);
1595
1596 MODULE_LICENSE("GPL v2");
1597 MODULE_AUTHOR("naveen.gaddipati@stericsson.com, js.ha@stericsson.com");
1598 MODULE_DESCRIPTION("synaptics rmi4 i2c touch Driver");
1599 MODULE_ALIAS("i2c:synaptics_rmi4_ts");
1600
1601
1602
1603