Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / staging / ste_rmi4 / synaptics_i2c_rmi4.c
blob28b393071026318eea30591a81e7d66ce250ded7
1 /**
3 * Synaptics Register Mapped Interface (RMI4) I2C Physical Layer Driver.
4 * Copyright (c) 2007-2010, Synaptics Incorporated
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 */
11 * This file is licensed under the GPL2 license.
13 *#############################################################################
14 * GPL
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.
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.
25 *#############################################################################
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"
37 /* TODO: for multiple device support will need a per-device mutex */
38 #define DRIVER_NAME "synaptics_rmi4_i2c"
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
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)
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
83 * This structure is used to gives the function descriptor information
84 * of the particular functionality.
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;
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
106 * This structure gives information about the number of data sources and
107 * the number of data registers associated with the function.
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;
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
133 * This structure gives information about the number of data sources and
134 * the number of data registers associated with the function.
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;
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
169 * This structure gives the device data information.
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;
192 * synaptics_rmi4_set_page() - sets the page
193 * @pdata: pointer to synaptics_rmi4_data structure
194 * @address: set the address of the page
196 * This function is used to set the page and returns integer.
198 static int synaptics_rmi4_set_page(struct synaptics_rmi4_data *pdata,
199 unsigned int address)
201 unsigned char txbuf[PAGE_LEN];
202 int retval;
203 unsigned int page;
204 struct i2c_client *i2c = pdata->i2c_client;
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;
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
226 * This function is to read the block of data and returns integer.
228 static int synaptics_rmi4_i2c_block_read(struct synaptics_rmi4_data *pdata,
229 unsigned short address,
230 unsigned char *valp, int size)
232 int retval = 0;
233 int retry_count = 0;
234 int index;
235 struct i2c_client *i2c = pdata->i2c_client;
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;
254 exit:
255 mutex_unlock(&(pdata->rmi4_page_mutex));
256 return retval;
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
265 * This function is to write the single byte data and returns integer.
267 static int synaptics_rmi4_i2c_byte_write(struct synaptics_rmi4_data *pdata,
268 unsigned short address,
269 unsigned char data)
271 unsigned char txbuf[2];
272 int retval = 0;
273 struct i2c_client *i2c = pdata->i2c_client;
275 /* Can't have anyone else changing the page behind our backs */
276 mutex_lock(&(pdata->rmi4_page_mutex));
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;
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
300 * This function calls to reports for the rmi4 touchpad device
302 static int synpatics_rmi4_touchpad_report(struct synaptics_rmi4_data *pdata,
303 struct synaptics_rmi4_fn *rfi)
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;
324 /* get 2D sensor finger data */
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.
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;
347 * For each finger present, read the proper number of registers
348 * to get absolute data.
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;
358 * if finger status indicates a finger is present then
359 * read the finger data and report it
361 input_mt_slot(input_dev, finger);
362 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
363 finger_status != 0);
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;
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);
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;
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);
393 /* number of active touch points */
394 touch_count++;
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;
406 * synaptics_rmi4_report_device() - reports the rmi4 device
407 * @pdata: pointer to synaptics_rmi4_data structure
408 * @rfi: pointer to synaptics_rmi4_fn
410 * This function is used to call the report function of the rmi4 device.
412 static int synaptics_rmi4_report_device(struct synaptics_rmi4_data *pdata,
413 struct synaptics_rmi4_fn *rfi)
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;
428 * synaptics_rmi4_sensor_report() - reports to input subsystem
429 * @pdata: pointer to synaptics_rmi4_data structure
431 * This function is used to reads in all data sources and reports
432 * them to the input subsystem.
434 static int synaptics_rmi4_sensor_report(struct synaptics_rmi4_data *pdata)
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;
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..)
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;
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
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);
472 /* return the number of touch points */
473 return touch;
477 * synaptics_rmi4_irq() - thread function for rmi4 attention line
478 * @irq: irq value
479 * @data: void pointer
481 * This function is interrupt thread function. It just notifies the
482 * application layer that attention is required.
484 static irqreturn_t synaptics_rmi4_irq(int irq, void *data)
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;
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
506 * This function calls to detects the rmi4 touchpad device
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)
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;
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;
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.
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;
551 * 2D data sources have only 3 bits for the number of fingers
552 * supported - so the encoding is a bit weird.
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 {
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).
562 if ((queries[1] & MASK_3BIT) == 5)
563 rfi->num_of_data_points = 10;
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;
571 * loop through interrupts for each source in fn $11
572 * and or in a bit to the interrupt mask for each.
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;
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;
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.
591 egr_0 = queries[7];
592 egr_1 = queries[8];
595 * Get info about what EGR data is supported, whether it has
596 * Relative data supported, etc.
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;
610 * Size of all data including finger status, absolute data for each
611 * finger, relative data and EGR data
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) +
619 * two relative registers (if relative is being reported)
621 2 * has_rel +
623 * F11_2D_data8 is only present if the egr_0
624 * register is non-zero.
626 !!(egr_0) +
628 * F11_2D_data9 is only present if either egr_0 or
629 * egr_1 registers are non-zero.
631 (egr_0 || egr_1) +
633 * F11_2D_data10 is only present if EGR_PINCH or EGR_FLICK of
634 * egr_0 reports as 1.
636 !!(has_pinch | has_flick) +
638 * F11_2D_data11 and F11_2D_data12 are only present if
639 * EGR_FLICK of egr_0 reports as 1.
641 2 * !!(has_flick);
642 return retval;
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
650 * This function calls to configures the rmi4 touchpad device
652 static int synaptics_rmi4_touchpad_config(struct synaptics_rmi4_data *pdata,
653 struct synaptics_rmi4_fn *rfi)
656 * For the data source - print info and do any
657 * source specific configuration.
659 unsigned char data[BUF_LEN];
660 int retval = 0;
661 struct i2c_client *client = pdata->i2c_client;
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.
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;
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);
689 return retval;
693 * synaptics_rmi4_i2c_query_device() - query the rmi4 device
694 * @pdata: pointer to synaptics_rmi4_data structure
696 * This function is used to query the rmi4 device.
698 static int synaptics_rmi4_i2c_query_device(struct synaptics_rmi4_data *pdata)
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;
712 * init the physical drivers RMI module
713 * info list of functions
715 INIT_LIST_HEAD(&pdata->rmi4_mod_info.support_fn_list);
718 * Read the Page Descriptor Table to determine what functions
719 * are present
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;
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;
757 break;
759 /* interrupt count for next iteration */
760 intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
762 * We only want to add functions to the list
763 * that have data associated with them.
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));
772 } else {
774 * A zero in the function number
775 * signals the end of the PDT
777 dev_dbg(&client->dev,
778 "%s:end of PDT\n", __func__);
779 break;
783 * calculate the interrupt register count - used in the
784 * ISR to read the correct number of interrupt registers
786 pdata->number_of_interrupt_register = (intr_count + 7) / 8;
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.
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;
805 /* Currently supported RMI version is 4.0 */
806 pdata->rmi4_mod_info.version_major = 4;
807 pdata->rmi4_mod_info.version_minor = 0;
809 * get manufacturer id, product_props, product info,
810 * date code, tester id, serial num and product id (name)
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);
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);
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__);
851 * Turn on interrupts for this
852 * function's data sources.
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;
864 return 0;
868 * Descriptor structure.
869 * Describes the number of i2c devices on the bus that speak RMI.
871 static struct synaptics_rmi4_platform_data synaptics_rmi4_platformdata = {
872 .irq_type = (IRQF_TRIGGER_FALLING | IRQF_SHARED),
873 .x_flip = false,
874 .y_flip = true,
878 * synaptics_rmi4_probe() - Initialze the i2c-client touchscreen driver
879 * @i2c: i2c client structure pointer
880 * @id:i2c device id pointer
882 * This function will allocate and initialize the instance
883 * data and request the irq and set the instance data as the clients
884 * platform data then register the physical driver which will do a scan of
885 * the rmi4 Physical Device Table and enumerate any rmi4 functions that
886 * have data sources associated with them.
888 static int synaptics_rmi4_probe
889 (struct i2c_client *client, const struct i2c_device_id *dev_id)
891 int retval;
892 unsigned char intr_status[4];
893 struct synaptics_rmi4_data *rmi4_data;
894 const struct synaptics_rmi4_platform_data *platformdata =
895 client->dev.platform_data;
897 if (!i2c_check_functionality(client->adapter,
898 I2C_FUNC_SMBUS_BYTE_DATA)) {
899 dev_err(&client->dev, "i2c smbus byte data not supported\n");
900 return -EIO;
903 if (!platformdata)
904 platformdata = &synaptics_rmi4_platformdata;
906 /* Allocate and initialize the instance data for this client */
907 rmi4_data = kcalloc(2, sizeof(struct synaptics_rmi4_data),
908 GFP_KERNEL);
909 if (!rmi4_data)
910 return -ENOMEM;
912 rmi4_data->input_dev = input_allocate_device();
913 if (rmi4_data->input_dev == NULL) {
914 retval = -ENOMEM;
915 goto err_input;
918 rmi4_data->regulator = regulator_get(&client->dev, "vdd");
919 if (IS_ERR(rmi4_data->regulator)) {
920 dev_err(&client->dev, "%s:get regulator failed\n",
921 __func__);
922 retval = PTR_ERR(rmi4_data->regulator);
923 goto err_get_regulator;
925 retval = regulator_enable(rmi4_data->regulator);
926 if (retval < 0) {
927 dev_err(&client->dev, "%s:regulator enable failed\n",
928 __func__);
929 goto err_regulator_enable;
931 init_waitqueue_head(&rmi4_data->wait);
933 * Copy i2c_client pointer into RTID's i2c_client pointer for
934 * later use in rmi4_read, rmi4_write, etc.
936 rmi4_data->i2c_client = client;
937 /* So we set the page correctly the first time */
938 rmi4_data->current_page = MASK_16BIT;
939 rmi4_data->board = platformdata;
940 rmi4_data->touch_stopped = false;
942 /* init the mutexes for maintain the lists */
943 mutex_init(&(rmi4_data->fn_list_mutex));
944 mutex_init(&(rmi4_data->rmi4_page_mutex));
947 * Register physical driver - this will call the detect function that
948 * will then scan the device and determine the supported
949 * rmi4 functions.
951 retval = synaptics_rmi4_i2c_query_device(rmi4_data);
952 if (retval) {
953 dev_err(&client->dev, "%s: rmi4 query device failed\n",
954 __func__);
955 goto err_query_dev;
958 /* Store the instance data in the i2c_client */
959 i2c_set_clientdata(client, rmi4_data);
961 /*initialize the input device parameters */
962 rmi4_data->input_dev->name = DRIVER_NAME;
963 rmi4_data->input_dev->phys = "Synaptics_Clearpad";
964 rmi4_data->input_dev->id.bustype = BUS_I2C;
965 rmi4_data->input_dev->dev.parent = &client->dev;
966 input_set_drvdata(rmi4_data->input_dev, rmi4_data);
968 /* Initialize the function handlers for rmi4 */
969 set_bit(EV_SYN, rmi4_data->input_dev->evbit);
970 set_bit(EV_KEY, rmi4_data->input_dev->evbit);
971 set_bit(EV_ABS, rmi4_data->input_dev->evbit);
973 input_set_abs_params(rmi4_data->input_dev, ABS_MT_POSITION_X, 0,
974 rmi4_data->sensor_max_x, 0, 0);
975 input_set_abs_params(rmi4_data->input_dev, ABS_MT_POSITION_Y, 0,
976 rmi4_data->sensor_max_y, 0, 0);
977 input_set_abs_params(rmi4_data->input_dev, ABS_MT_TOUCH_MAJOR, 0,
978 MAX_TOUCH_MAJOR, 0, 0);
979 input_mt_init_slots(rmi4_data->input_dev,
980 rmi4_data->fingers_supported, 0);
982 /* Clear interrupts */
983 synaptics_rmi4_i2c_block_read(rmi4_data,
984 rmi4_data->fn01_data_base_addr + 1, intr_status,
985 rmi4_data->number_of_interrupt_register);
986 retval = request_threaded_irq(client->irq, NULL,
987 synaptics_rmi4_irq,
988 platformdata->irq_type,
989 DRIVER_NAME, rmi4_data);
990 if (retval) {
991 dev_err(&client->dev, "%s:Unable to get attn irq %d\n",
992 __func__, client->irq);
993 goto err_query_dev;
996 retval = input_register_device(rmi4_data->input_dev);
997 if (retval) {
998 dev_err(&client->dev, "%s:input register failed\n", __func__);
999 goto err_free_irq;
1002 return retval;
1004 err_free_irq:
1005 free_irq(client->irq, rmi4_data);
1006 err_query_dev:
1007 regulator_disable(rmi4_data->regulator);
1008 err_regulator_enable:
1009 regulator_put(rmi4_data->regulator);
1010 err_get_regulator:
1011 input_free_device(rmi4_data->input_dev);
1012 rmi4_data->input_dev = NULL;
1013 err_input:
1014 kfree(rmi4_data);
1016 return retval;
1019 * synaptics_rmi4_remove() - Removes the i2c-client touchscreen driver
1020 * @client: i2c client structure pointer
1022 * This function uses to remove the i2c-client
1023 * touchscreen driver and returns integer.
1025 static int synaptics_rmi4_remove(struct i2c_client *client)
1027 struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
1029 rmi4_data->touch_stopped = true;
1030 wake_up(&rmi4_data->wait);
1031 free_irq(client->irq, rmi4_data);
1032 input_unregister_device(rmi4_data->input_dev);
1033 regulator_disable(rmi4_data->regulator);
1034 regulator_put(rmi4_data->regulator);
1035 kfree(rmi4_data);
1037 return 0;
1040 #ifdef CONFIG_PM
1042 * synaptics_rmi4_suspend() - suspend the touch screen controller
1043 * @dev: pointer to device structure
1045 * This function is used to suspend the
1046 * touch panel controller and returns integer
1048 static int synaptics_rmi4_suspend(struct device *dev)
1050 /* Touch sleep mode */
1051 int retval;
1052 unsigned char intr_status;
1053 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
1055 rmi4_data->touch_stopped = true;
1056 disable_irq(rmi4_data->i2c_client->irq);
1058 retval = synaptics_rmi4_i2c_block_read(rmi4_data,
1059 rmi4_data->fn01_data_base_addr + 1,
1060 &intr_status,
1061 rmi4_data->number_of_interrupt_register);
1062 if (retval < 0)
1063 return retval;
1065 retval = synaptics_rmi4_i2c_byte_write(rmi4_data,
1066 rmi4_data->fn01_ctrl_base_addr + 1,
1067 (intr_status & ~TOUCHPAD_CTRL_INTR));
1068 if (retval < 0)
1069 return retval;
1071 regulator_disable(rmi4_data->regulator);
1073 return 0;
1076 * synaptics_rmi4_resume() - resume the touch screen controller
1077 * @dev: pointer to device structure
1079 * This function is used to resume the touch panel
1080 * controller and returns integer.
1082 static int synaptics_rmi4_resume(struct device *dev)
1084 int retval;
1085 unsigned char intr_status;
1086 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
1088 retval = regulator_enable(rmi4_data->regulator);
1089 if (retval) {
1090 dev_err(dev, "Regulator enable failed (%d)\n", retval);
1091 return retval;
1094 enable_irq(rmi4_data->i2c_client->irq);
1095 rmi4_data->touch_stopped = false;
1097 retval = synaptics_rmi4_i2c_block_read(rmi4_data,
1098 rmi4_data->fn01_data_base_addr + 1,
1099 &intr_status,
1100 rmi4_data->number_of_interrupt_register);
1101 if (retval < 0)
1102 return retval;
1104 retval = synaptics_rmi4_i2c_byte_write(rmi4_data,
1105 rmi4_data->fn01_ctrl_base_addr + 1,
1106 (intr_status | TOUCHPAD_CTRL_INTR));
1107 if (retval < 0)
1108 return retval;
1110 return 0;
1113 static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = {
1114 .suspend = synaptics_rmi4_suspend,
1115 .resume = synaptics_rmi4_resume,
1117 #endif
1119 static const struct i2c_device_id synaptics_rmi4_id_table[] = {
1120 { DRIVER_NAME, 0 },
1121 { },
1123 MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table);
1125 static struct i2c_driver synaptics_rmi4_driver = {
1126 .driver = {
1127 .name = DRIVER_NAME,
1128 .owner = THIS_MODULE,
1129 #ifdef CONFIG_PM
1130 .pm = &synaptics_rmi4_dev_pm_ops,
1131 #endif
1133 .probe = synaptics_rmi4_probe,
1134 .remove = synaptics_rmi4_remove,
1135 .id_table = synaptics_rmi4_id_table,
1138 module_i2c_driver(synaptics_rmi4_driver);
1140 MODULE_LICENSE("GPL v2");
1141 MODULE_AUTHOR("naveen.gaddipati@stericsson.com, js.ha@stericsson.com");
1142 MODULE_DESCRIPTION("synaptics rmi4 i2c touch Driver");
1143 MODULE_ALIAS("i2c:synaptics_rmi4_ts");