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