Merge branch 'staging-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[pandora-kernel.git] / drivers / staging / ste_rmi4 / 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 "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 funtion 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  */
85 struct 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 funtion 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  */
108 struct 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  */
135 struct 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 funtion 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  */
169 struct 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  */
195 static 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  */
225 static 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;
239 retry:
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         }
251 exit:
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  */
264 static 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 certian 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;
287 exit:
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  */
299 static 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  */
424 static 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  */
446 static 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  */
496 static 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  */
520 static 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 wierd.
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  */
663 int 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  */
709 static 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                                                 return retval;
769                                 }
770                                 break;
771                         }
772                         /* interrupt count for next iteration */
773                         intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
774                         /*
775                          * We only want to add functions to the list
776                          * that have data associated with them.
777                          */
778                         if (rfi && rmi_fd.intr_src_count) {
779                                 /* link this function info to the RMI module */
780                                 mutex_lock(&(pdata->fn_list_mutex));
781                                 list_add_tail(&rfi->link,
782                                         &pdata->rmi4_mod_info.support_fn_list);
783                                 mutex_unlock(&(pdata->fn_list_mutex));
784                         }
785                 } else {
786                         /*
787                          * A zero in the function number
788                          * signals the end of the PDT
789                          */
790                         dev_dbg(&client->dev,
791                                 "%s:end of PDT\n", __func__);
792                         break;
793                 }
794         }
795         /*
796          * calculate the interrupt register count - used in the
797          * ISR to read the correct number of interrupt registers
798          */
799         pdata->number_of_interrupt_register = (intr_count + 7) / 8;
800         /*
801          * Function $01 will be used to query the product properties,
802          * and product ID  so we had to read the PDT above first to get
803          * the Fn $01 query address and prior to filling in the product
804          * info. NOTE: Even an unflashed device will still have FN $01.
805          */
806
807         /* Load up the standard queries and get the RMI4 module info */
808         retval = synaptics_rmi4_i2c_block_read(pdata,
809                                         pdata->fn01_query_base_addr,
810                                         std_queries,
811                                         sizeof(std_queries));
812         if (retval != sizeof(std_queries)) {
813                 dev_err(&client->dev, "%s:Failed reading queries\n",
814                                                         __func__);
815                  return -EIO;
816         }
817
818         /* Currently supported RMI version is 4.0 */
819         pdata->rmi4_mod_info.version_major      = 4;
820         pdata->rmi4_mod_info.version_minor      = 0;
821         /*
822          * get manufacturer id, product_props, product info,
823          * date code, tester id, serial num and product id (name)
824          */
825         pdata->rmi4_mod_info.manufacturer_id    = std_queries[0];
826         pdata->rmi4_mod_info.product_props      = std_queries[1];
827         pdata->rmi4_mod_info.product_info[0]    = std_queries[2];
828         pdata->rmi4_mod_info.product_info[1]    = std_queries[3];
829         /* year - 2001-2032 */
830         pdata->rmi4_mod_info.date_code[0]       = std_queries[4] & MASK_5BIT;
831         /* month - 1-12 */
832         pdata->rmi4_mod_info.date_code[1]       = std_queries[5] & MASK_4BIT;
833         /* day - 1-31 */
834         pdata->rmi4_mod_info.date_code[2]       = std_queries[6] & MASK_5BIT;
835         pdata->rmi4_mod_info.tester_id = ((std_queries[7] & MASK_7BIT) << 8) |
836                                                 (std_queries[8] & MASK_7BIT);
837         pdata->rmi4_mod_info.serial_number =
838                 ((std_queries[9] & MASK_7BIT) << 8) |
839                                 (std_queries[10] & MASK_7BIT);
840         memcpy(pdata->rmi4_mod_info.product_id_string, &std_queries[11], 10);
841
842         /* Check if this is a Synaptics device - report if not. */
843         if (pdata->rmi4_mod_info.manufacturer_id != 1)
844                 dev_err(&client->dev, "%s: non-Synaptics mfg id:%d\n",
845                         __func__, pdata->rmi4_mod_info.manufacturer_id);
846
847         list_for_each_entry(rfi, &pdata->rmi4_mod_info.support_fn_list, link)
848                 data_sources += rfi->num_of_data_sources;
849         if (data_sources) {
850                 rmi = &(pdata->rmi4_mod_info);
851                 list_for_each_entry(rfi, &rmi->support_fn_list, link) {
852                         if (rfi->num_of_data_sources) {
853                                 if (rfi->fn_number ==
854                                         SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM) {
855                                         retval = synpatics_rmi4_touchpad_config
856                                                                 (pdata, rfi);
857                                         if (retval < 0)
858                                                 return retval;
859                                 } else
860                                         dev_err(&client->dev,
861                                                 "%s:fn_number not supported\n",
862                                                                 __func__);
863                                 /*
864                                  * Turn on interrupts for this
865                                  * function's data sources.
866                                  */
867                                 ctrl_offset = pdata->fn01_ctrl_base_addr + 1 +
868                                                         rfi->index_to_intr_reg;
869                                 retval = synaptics_rmi4_i2c_byte_write(pdata,
870                                                         ctrl_offset,
871                                                         rfi->intr_mask);
872                                 if (retval < 0)
873                                         return retval;
874                         }
875                 }
876         }
877         return 0;
878 }
879
880 /**
881  * synaptics_rmi4_probe() - Initialze the i2c-client touchscreen driver
882  * @i2c: i2c client structure pointer
883  * @id:i2c device id pointer
884  *
885  * This function will allocate and initialize the instance
886  * data and request the irq and set the instance data as the clients
887  * platform data then register the physical driver which will do a scan of
888  * the rmi4 Physical Device Table and enumerate any rmi4 functions that
889  * have data sources associated with them.
890  */
891 static int __devinit synaptics_rmi4_probe
892         (struct i2c_client *client, const struct i2c_device_id *dev_id)
893 {
894         int retval;
895         unsigned char intr_status[4];
896         struct synaptics_rmi4_data *rmi4_data;
897         const struct synaptics_rmi4_platform_data *platformdata =
898                                                 client->dev.platform_data;
899
900         if (!i2c_check_functionality(client->adapter,
901                                         I2C_FUNC_SMBUS_BYTE_DATA)) {
902                 dev_err(&client->dev, "i2c smbus byte data not supported\n");
903                 return -EIO;
904         }
905
906         if (!platformdata) {
907                 dev_err(&client->dev, "%s: no platform data\n", __func__);
908                 return -EINVAL;
909         }
910
911         /* Allocate and initialize the instance data for this client */
912         rmi4_data = kzalloc(sizeof(struct synaptics_rmi4_data) * 2,
913                                                         GFP_KERNEL);
914         if (!rmi4_data) {
915                 dev_err(&client->dev, "%s: no memory allocated\n", __func__);
916                 return -ENOMEM;
917         }
918
919         rmi4_data->input_dev = input_allocate_device();
920         if (rmi4_data->input_dev == NULL) {
921                 dev_err(&client->dev, "%s:input device alloc failed\n",
922                                                 __func__);
923                 retval = -ENOMEM;
924                 goto err_input;
925         }
926
927         dev_set_name(&client->dev, platformdata->name);
928
929         if (platformdata->regulator_en) {
930                 rmi4_data->regulator = regulator_get(&client->dev, "v-touch");
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         retval = input_register_device(rmi4_data->input_dev);
990         if (retval) {
991                 dev_err(&client->dev, "%s:input register failed\n", __func__);
992                 goto err_input_register;
993         }
994
995         /* Clear interrupts */
996         synaptics_rmi4_i2c_block_read(rmi4_data,
997                         rmi4_data->fn01_data_base_addr + 1, intr_status,
998                                 rmi4_data->number_of_interrupt_register);
999         retval = request_threaded_irq(platformdata->irq_number, NULL,
1000                                         synaptics_rmi4_irq,
1001                                         platformdata->irq_type,
1002                                         platformdata->name, rmi4_data);
1003         if (retval) {
1004                 dev_err(&client->dev, "%s:Unable to get attn irq %d\n",
1005                                 __func__, platformdata->irq_number);
1006                 goto err_request_irq;
1007         }
1008
1009         return retval;
1010
1011 err_request_irq:
1012         free_irq(platformdata->irq_number, rmi4_data);
1013         input_unregister_device(rmi4_data->input_dev);
1014 err_input_register:
1015         i2c_set_clientdata(client, NULL);
1016 err_query_dev:
1017         if (platformdata->regulator_en) {
1018                 regulator_disable(rmi4_data->regulator);
1019                 regulator_put(rmi4_data->regulator);
1020         }
1021 err_regulator:
1022         input_free_device(rmi4_data->input_dev);
1023         rmi4_data->input_dev = NULL;
1024 err_input:
1025         kfree(rmi4_data);
1026
1027         return retval;
1028 }
1029 /**
1030  * synaptics_rmi4_remove() - Removes the i2c-client touchscreen driver
1031  * @client: i2c client structure pointer
1032  *
1033  * This funtion uses to remove the i2c-client
1034  * touchscreen driver and returns integer.
1035  */
1036 static int __devexit synaptics_rmi4_remove(struct i2c_client *client)
1037 {
1038         struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
1039         const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board;
1040
1041         rmi4_data->touch_stopped = true;
1042         wake_up(&rmi4_data->wait);
1043         free_irq(pdata->irq_number, rmi4_data);
1044         input_unregister_device(rmi4_data->input_dev);
1045         if (pdata->regulator_en) {
1046                 regulator_disable(rmi4_data->regulator);
1047                 regulator_put(rmi4_data->regulator);
1048         }
1049         kfree(rmi4_data);
1050
1051         return 0;
1052 }
1053
1054 #ifdef CONFIG_PM
1055 /**
1056  * synaptics_rmi4_suspend() - suspend the touch screen controller
1057  * @dev: pointer to device structure
1058  *
1059  * This funtion is used to suspend the
1060  * touch panel controller and returns integer
1061  */
1062 static int synaptics_rmi4_suspend(struct device *dev)
1063 {
1064         /* Touch sleep mode */
1065         int retval;
1066         unsigned char intr_status;
1067         struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
1068         const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board;
1069
1070         rmi4_data->touch_stopped = true;
1071         disable_irq(pdata->irq_number);
1072
1073         retval = synaptics_rmi4_i2c_block_read(rmi4_data,
1074                                 rmi4_data->fn01_data_base_addr + 1,
1075                                 &intr_status,
1076                                 rmi4_data->number_of_interrupt_register);
1077         if (retval < 0)
1078                 return retval;
1079
1080         retval = synaptics_rmi4_i2c_byte_write(rmi4_data,
1081                                         rmi4_data->fn01_ctrl_base_addr + 1,
1082                                         (intr_status & ~TOUCHPAD_CTRL_INTR));
1083         if (retval < 0)
1084                 return retval;
1085
1086         if (pdata->regulator_en)
1087                 regulator_disable(rmi4_data->regulator);
1088
1089         return 0;
1090 }
1091 /**
1092  * synaptics_rmi4_resume() - resume the touch screen controller
1093  * @dev: pointer to device structure
1094  *
1095  * This funtion is used to resume the touch panel
1096  * controller and returns integer.
1097  */
1098 static int synaptics_rmi4_resume(struct device *dev)
1099 {
1100         int retval;
1101         unsigned char intr_status;
1102         struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
1103         const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board;
1104
1105         if (pdata->regulator_en)
1106                 regulator_enable(rmi4_data->regulator);
1107
1108         enable_irq(pdata->irq_number);
1109         rmi4_data->touch_stopped = false;
1110
1111         retval = synaptics_rmi4_i2c_block_read(rmi4_data,
1112                                 rmi4_data->fn01_data_base_addr + 1,
1113                                 &intr_status,
1114                                 rmi4_data->number_of_interrupt_register);
1115         if (retval < 0)
1116                 return retval;
1117
1118         retval = synaptics_rmi4_i2c_byte_write(rmi4_data,
1119                                         rmi4_data->fn01_ctrl_base_addr + 1,
1120                                         (intr_status | TOUCHPAD_CTRL_INTR));
1121         if (retval < 0)
1122                 return retval;
1123
1124         return 0;
1125 }
1126
1127 static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = {
1128         .suspend = synaptics_rmi4_suspend,
1129         .resume  = synaptics_rmi4_resume,
1130 };
1131 #endif
1132
1133 static const struct i2c_device_id synaptics_rmi4_id_table[] = {
1134         { DRIVER_NAME, 0 },
1135         { },
1136 };
1137 MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table);
1138
1139 static struct i2c_driver synaptics_rmi4_driver = {
1140         .driver = {
1141                 .name   =       DRIVER_NAME,
1142                 .owner  =       THIS_MODULE,
1143 #ifdef CONFIG_PM
1144                 .pm     =       &synaptics_rmi4_dev_pm_ops,
1145 #endif
1146         },
1147         .probe          =       synaptics_rmi4_probe,
1148         .remove         =       __devexit_p(synaptics_rmi4_remove),
1149         .id_table       =       synaptics_rmi4_id_table,
1150 };
1151 /**
1152  * synaptics_rmi4_init() - Initialize the touchscreen driver
1153  *
1154  * This funtion uses to initializes the synaptics
1155  * touchscreen driver and returns integer.
1156  */
1157 static int __init synaptics_rmi4_init(void)
1158 {
1159         return i2c_add_driver(&synaptics_rmi4_driver);
1160 }
1161 /**
1162  * synaptics_rmi4_exit() - De-initialize the touchscreen driver
1163  *
1164  * This funtion uses to de-initialize the synaptics
1165  * touchscreen driver and returns none.
1166  */
1167 static void __exit synaptics_rmi4_exit(void)
1168 {
1169         i2c_del_driver(&synaptics_rmi4_driver);
1170 }
1171
1172
1173 module_init(synaptics_rmi4_init);
1174 module_exit(synaptics_rmi4_exit);
1175
1176 MODULE_LICENSE("GPL v2");
1177 MODULE_AUTHOR("naveen.gaddipati@stericsson.com, js.ha@stericsson.com");
1178 MODULE_DESCRIPTION("synaptics rmi4 i2c touch Driver");
1179 MODULE_ALIAS("i2c:synaptics_rmi4_ts");