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