[go: nahoru, domu]

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 */
86struct 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 */
109struct 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 */
136struct 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 */
171struct 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 */
198static 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 */
228static 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;
242retry:
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	}
254exit:
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 */
267static 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;
290exit:
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 */
302static 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 */
412static 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
419	if (rfi->fn_number != SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM) {
420		num_error_reports++;
421		if (num_error_reports < MAX_ERROR_REPORT)
422			dev_err(&client->dev, "%s:report not supported\n",
423								__func__);
424	} else
425		touch = synpatics_rmi4_touchpad_report(pdata, rfi);
426	return touch;
427}
428/**
429 * synaptics_rmi4_sensor_report() - reports to input subsystem
430 * @pdata: pointer to synaptics_rmi4_data structure
431 *
432 * This function is used to reads in all data sources and reports
433 * them to the input subsystem.
434 */
435static int synaptics_rmi4_sensor_report(struct synaptics_rmi4_data *pdata)
436{
437	unsigned char	intr_status[4];
438	/* number of touch points - fingers or buttons */
439	int touch = 0;
440	unsigned int retval;
441	struct synaptics_rmi4_fn		*rfi;
442	struct synaptics_rmi4_device_info	*rmi;
443	struct	i2c_client *client = pdata->i2c_client;
444
445	/*
446	 * Get the interrupt status from the function $01
447	 * control register+1 to find which source(s) were interrupting
448	 * so we can read the data from the source(s) (2D sensor, buttons..)
449	 */
450	retval = synaptics_rmi4_i2c_block_read(pdata,
451					pdata->fn01_data_base_addr + 1,
452					intr_status,
453					pdata->number_of_interrupt_register);
454	if (retval != pdata->number_of_interrupt_register) {
455		dev_err(&client->dev,
456				"could not read interrupt status registers\n");
457		return 0;
458	}
459	/*
460	 * check each function that has data sources and if the interrupt for
461	 * that triggered then call that RMI4 functions report() function to
462	 * gather data and report it to the input subsystem
463	 */
464	rmi = &(pdata->rmi4_mod_info);
465	list_for_each_entry(rfi, &rmi->support_fn_list, link) {
466		if (rfi->num_of_data_sources) {
467			if (intr_status[rfi->index_to_intr_reg] &
468							rfi->intr_mask)
469				touch = synaptics_rmi4_report_device(pdata,
470									rfi);
471		}
472	}
473	/* return the number of touch points */
474	return touch;
475}
476
477/**
478 * synaptics_rmi4_irq() - thread function for rmi4 attention line
479 * @irq: irq value
480 * @data: void pointer
481 *
482 * This function is interrupt thread function. It just notifies the
483 * application layer that attention is required.
484 */
485static irqreturn_t synaptics_rmi4_irq(int irq, void *data)
486{
487	struct synaptics_rmi4_data *pdata = data;
488	int touch_count;
489
490	do {
491		touch_count = synaptics_rmi4_sensor_report(pdata);
492		if (touch_count)
493			wait_event_timeout(pdata->wait, pdata->touch_stopped,
494							msecs_to_jiffies(1));
495		else
496			break;
497	} while (!pdata->touch_stopped);
498	return IRQ_HANDLED;
499}
500
501/**
502 * synpatics_rmi4_touchpad_detect() - detects the rmi4 touchpad device
503 * @pdata: pointer to synaptics_rmi4_data structure
504 * @rfi: pointer to synaptics_rmi4_fn structure
505 * @fd: pointer to synaptics_rmi4_fn_desc structure
506 * @interruptcount: count the number of interrupts
507 *
508 * This function calls to detects the rmi4 touchpad device
509 */
510static int synpatics_rmi4_touchpad_detect(struct synaptics_rmi4_data *pdata,
511					struct synaptics_rmi4_fn *rfi,
512					struct synaptics_rmi4_fn_desc *fd,
513					unsigned int interruptcount)
514{
515	unsigned char	queries[QUERY_LEN];
516	unsigned short	intr_offset;
517	unsigned char	abs_data_size;
518	unsigned char	abs_data_blk_size;
519	unsigned char	egr_0, egr_1;
520	unsigned int	all_data_blk_size;
521	int	has_pinch, has_flick, has_tap;
522	int	has_tapandhold, has_doubletap;
523	int	has_earlytap, has_press;
524	int	has_palmdetect, has_rotate;
525	int	has_rel;
526	int	i;
527	int	retval;
528	struct	i2c_client *client = pdata->i2c_client;
529
530	rfi->fn_desc.query_base_addr	= fd->query_base_addr;
531	rfi->fn_desc.data_base_addr	= fd->data_base_addr;
532	rfi->fn_desc.intr_src_count	= fd->intr_src_count;
533	rfi->fn_desc.fn_number		= fd->fn_number;
534	rfi->fn_number			= fd->fn_number;
535	rfi->num_of_data_sources	= fd->intr_src_count;
536	rfi->fn_desc.ctrl_base_addr	= fd->ctrl_base_addr;
537	rfi->fn_desc.cmd_base_addr	= fd->cmd_base_addr;
538
539	/*
540	 * need to get number of fingers supported, data size, etc.
541	 * to be used when getting data since the number of registers to
542	 * read depends on the number of fingers supported and data size.
543	 */
544	retval = synaptics_rmi4_i2c_block_read(pdata, fd->query_base_addr,
545							queries,
546							sizeof(queries));
547	if (retval != sizeof(queries)) {
548		dev_err(&client->dev, "%s:read function query registers\n",
549							__func__);
550		return retval;
551	}
552	/*
553	 * 2D data sources have only 3 bits for the number of fingers
554	 * supported - so the encoding is a bit weird.
555	 */
556	if ((queries[1] & MASK_3BIT) <= 4)
557		/* add 1 since zero based */
558		rfi->num_of_data_points = (queries[1] & MASK_3BIT) + 1;
559	else {
560		/*
561		 * a value of 5 is up to 10 fingers - 6 and 7 are reserved
562		 * (shouldn't get these i int retval;n a normal 2D source).
563		 */
564		if ((queries[1] & MASK_3BIT) == 5)
565			rfi->num_of_data_points = 10;
566	}
567	pdata->fingers_supported = rfi->num_of_data_points;
568	/* Need to get interrupt info for handling interrupts */
569	rfi->index_to_intr_reg = (interruptcount + 7)/8;
570	if (rfi->index_to_intr_reg != 0)
571		rfi->index_to_intr_reg -= 1;
572	/*
573	 * loop through interrupts for each source in fn $11
574	 * and or in a bit to the interrupt mask for each.
575	 */
576	intr_offset = interruptcount % 8;
577	rfi->intr_mask = 0;
578	for (i = intr_offset;
579		i < ((fd->intr_src_count & MASK_3BIT) + intr_offset); i++)
580		rfi->intr_mask |= 1 << i;
581
582	/* Size of just the absolute data for one finger */
583	abs_data_size	= queries[5] & MASK_2BIT;
584	/* One each for X and Y, one for LSB for X & Y, one for W, one for Z */
585	abs_data_blk_size = 3 + (2 * (abs_data_size == 0 ? 1 : 0));
586	rfi->size_of_data_register_block = abs_data_blk_size;
587
588	/*
589	 * need to determine the size of data to read - this depends on
590	 * conditions such as whether Relative data is reported and if Gesture
591	 * data is reported.
592	 */
593	egr_0 = queries[7];
594	egr_1 = queries[8];
595
596	/*
597	 * Get info about what EGR data is supported, whether it has
598	 * Relative data supported, etc.
599	 */
600	has_pinch	= egr_0 & HAS_PINCH;
601	has_flick	= egr_0 & HAS_FLICK;
602	has_tap		= egr_0 & HAS_TAP;
603	has_earlytap	= egr_0 & HAS_EARLYTAP;
604	has_press	= egr_0 & HAS_PRESS;
605	has_rotate	= egr_1 & HAS_ROTATE;
606	has_rel		= queries[1] & HAS_RELEASE;
607	has_tapandhold	= egr_0 & HAS_TAPANDHOLD;
608	has_doubletap	= egr_0 & HAS_DOUBLETAP;
609	has_palmdetect	= egr_1 & HAS_PALMDETECT;
610
611	/*
612	 * Size of all data including finger status, absolute data for each
613	 * finger, relative data and EGR data
614	 */
615	all_data_blk_size =
616		/* finger status, four fingers per register */
617		((rfi->num_of_data_points + 3) / 4) +
618		/* absolute data, per finger times number of fingers */
619		(abs_data_blk_size * rfi->num_of_data_points) +
620		/*
621		 * two relative registers (if relative is being reported)
622		 */
623		2 * has_rel +
624		/*
625		 * F11_2D_data8 is only present if the egr_0
626		 * register is non-zero.
627		 */
628		!!(egr_0) +
629		/*
630		 * F11_2D_data9 is only present if either egr_0 or
631		 * egr_1 registers are non-zero.
632		 */
633		(egr_0 || egr_1) +
634		/*
635		 * F11_2D_data10 is only present if EGR_PINCH or EGR_FLICK of
636		 * egr_0 reports as 1.
637		 */
638		!!(has_pinch | has_flick) +
639		/*
640		 * F11_2D_data11 and F11_2D_data12 are only present if
641		 * EGR_FLICK of egr_0 reports as 1.
642		 */
643		2 * !!(has_flick);
644	return retval;
645}
646
647/**
648 * synaptics_rmi4_touchpad_config() - configures the rmi4 touchpad device
649 * @pdata: pointer to synaptics_rmi4_data structure
650 * @rfi: pointer to synaptics_rmi4_fn structure
651 *
652 * This function calls to configures the rmi4 touchpad device
653 */
654static int synaptics_rmi4_touchpad_config(struct synaptics_rmi4_data *pdata,
655						struct synaptics_rmi4_fn *rfi)
656{
657	/*
658	 * For the data source - print info and do any
659	 * source specific configuration.
660	 */
661	unsigned char data[BUF_LEN];
662	int retval = 0;
663	struct	i2c_client *client = pdata->i2c_client;
664
665	/* Get and print some info about the data source... */
666	/* To Query 2D devices we need to read from the address obtained
667	 * from the function descriptor stored in the RMI function info.
668	 */
669	retval = synaptics_rmi4_i2c_block_read(pdata,
670						rfi->fn_desc.query_base_addr,
671						data, QUERY_LEN);
672	if (retval != QUERY_LEN)
673		dev_err(&client->dev, "%s:read query registers failed\n",
674								__func__);
675	else {
676		retval = synaptics_rmi4_i2c_block_read(pdata,
677						rfi->fn_desc.ctrl_base_addr,
678						data, DATA_BUF_LEN);
679		if (retval != DATA_BUF_LEN) {
680			dev_err(&client->dev,
681				"%s:read control registers failed\n",
682								__func__);
683			return retval;
684		}
685		/* Store these for use later*/
686		pdata->sensor_max_x = ((data[6] & MASK_8BIT) << 0) |
687						((data[7] & MASK_4BIT) << 8);
688		pdata->sensor_max_y = ((data[8] & MASK_5BIT) << 0) |
689						((data[9] & MASK_4BIT) << 8);
690	}
691	return retval;
692}
693
694/**
695 * synaptics_rmi4_i2c_query_device() - query the rmi4 device
696 * @pdata: pointer to synaptics_rmi4_data structure
697 *
698 * This function is used to query the rmi4 device.
699 */
700static int synaptics_rmi4_i2c_query_device(struct synaptics_rmi4_data *pdata)
701{
702	int i;
703	int retval;
704	unsigned char std_queries[STD_QUERY_LEN];
705	unsigned char intr_count = 0;
706	int data_sources = 0;
707	unsigned int ctrl_offset;
708	struct synaptics_rmi4_fn *rfi;
709	struct synaptics_rmi4_fn_desc	rmi_fd;
710	struct synaptics_rmi4_device_info *rmi;
711	struct	i2c_client *client = pdata->i2c_client;
712
713	/*
714	 * init the physical drivers RMI module
715	 * info list of functions
716	 */
717	INIT_LIST_HEAD(&pdata->rmi4_mod_info.support_fn_list);
718
719	/*
720	 * Read the Page Descriptor Table to determine what functions
721	 * are present
722	 */
723	for (i = PDT_START_SCAN_LOCATION; i > PDT_END_SCAN_LOCATION;
724						i -= PDT_ENTRY_SIZE) {
725		retval = synaptics_rmi4_i2c_block_read(pdata, i,
726						(unsigned char *)&rmi_fd,
727						sizeof(rmi_fd));
728		if (retval != sizeof(rmi_fd)) {
729			/* failed to read next PDT entry */
730			dev_err(&client->dev, "%s: read error\n", __func__);
731			return -EIO;
732		}
733		rfi = NULL;
734		if (rmi_fd.fn_number) {
735			switch (rmi_fd.fn_number & MASK_8BIT) {
736			case SYNAPTICS_RMI4_DEVICE_CONTROL_FUNC_NUM:
737				pdata->fn01_query_base_addr =
738						rmi_fd.query_base_addr;
739				pdata->fn01_ctrl_base_addr =
740						rmi_fd.ctrl_base_addr;
741				pdata->fn01_data_base_addr =
742						rmi_fd.data_base_addr;
743				break;
744			case SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM:
745				if (rmi_fd.intr_src_count) {
746					rfi = kmalloc(sizeof(*rfi),
747						      GFP_KERNEL);
748					if (!rfi)
749						return -ENOMEM;
750					retval = synpatics_rmi4_touchpad_detect
751								(pdata,	rfi,
752								&rmi_fd,
753								intr_count);
754					if (retval < 0) {
755						kfree(rfi);
756						return retval;
757					}
758				}
759				break;
760			}
761			/* interrupt count for next iteration */
762			intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
763			/*
764			 * We only want to add functions to the list
765			 * that have data associated with them.
766			 */
767			if (rfi && rmi_fd.intr_src_count) {
768				/* link this function info to the RMI module */
769				mutex_lock(&(pdata->fn_list_mutex));
770				list_add_tail(&rfi->link,
771					&pdata->rmi4_mod_info.support_fn_list);
772				mutex_unlock(&(pdata->fn_list_mutex));
773			}
774		} else {
775			/*
776			 * A zero in the function number
777			 * signals the end of the PDT
778			 */
779			dev_dbg(&client->dev,
780				"%s:end of PDT\n", __func__);
781			break;
782		}
783	}
784	/*
785	 * calculate the interrupt register count - used in the
786	 * ISR to read the correct number of interrupt registers
787	 */
788	pdata->number_of_interrupt_register = (intr_count + 7) / 8;
789	/*
790	 * Function $01 will be used to query the product properties,
791	 * and product ID  so we had to read the PDT above first to get
792	 * the Fn $01 query address and prior to filling in the product
793	 * info. NOTE: Even an unflashed device will still have FN $01.
794	 */
795
796	/* Load up the standard queries and get the RMI4 module info */
797	retval = synaptics_rmi4_i2c_block_read(pdata,
798					pdata->fn01_query_base_addr,
799					std_queries,
800					sizeof(std_queries));
801	if (retval != sizeof(std_queries)) {
802		dev_err(&client->dev, "%s:Failed reading queries\n",
803							__func__);
804		 return -EIO;
805	}
806
807	/* Currently supported RMI version is 4.0 */
808	pdata->rmi4_mod_info.version_major	= 4;
809	pdata->rmi4_mod_info.version_minor	= 0;
810	/*
811	 * get manufacturer id, product_props, product info,
812	 * date code, tester id, serial num and product id (name)
813	 */
814	pdata->rmi4_mod_info.manufacturer_id	= std_queries[0];
815	pdata->rmi4_mod_info.product_props	= std_queries[1];
816	pdata->rmi4_mod_info.product_info[0]	= std_queries[2];
817	pdata->rmi4_mod_info.product_info[1]	= std_queries[3];
818	/* year - 2001-2032 */
819	pdata->rmi4_mod_info.date_code[0]	= std_queries[4] & MASK_5BIT;
820	/* month - 1-12 */
821	pdata->rmi4_mod_info.date_code[1]	= std_queries[5] & MASK_4BIT;
822	/* day - 1-31 */
823	pdata->rmi4_mod_info.date_code[2]	= std_queries[6] & MASK_5BIT;
824	pdata->rmi4_mod_info.tester_id = ((std_queries[7] & MASK_7BIT) << 8) |
825						(std_queries[8] & MASK_7BIT);
826	pdata->rmi4_mod_info.serial_number =
827		((std_queries[9] & MASK_7BIT) << 8) |
828				(std_queries[10] & MASK_7BIT);
829	memcpy(pdata->rmi4_mod_info.product_id_string, &std_queries[11], 10);
830
831	/* Check if this is a Synaptics device - report if not. */
832	if (pdata->rmi4_mod_info.manufacturer_id != 1)
833		dev_err(&client->dev, "%s: non-Synaptics mfg id:%d\n",
834			__func__, pdata->rmi4_mod_info.manufacturer_id);
835
836	list_for_each_entry(rfi, &pdata->rmi4_mod_info.support_fn_list, link)
837		data_sources += rfi->num_of_data_sources;
838	if (data_sources) {
839		rmi = &(pdata->rmi4_mod_info);
840		list_for_each_entry(rfi, &rmi->support_fn_list, link) {
841			if (rfi->num_of_data_sources) {
842				if (rfi->fn_number ==
843					SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM) {
844					retval = synaptics_rmi4_touchpad_config
845								(pdata, rfi);
846					if (retval < 0)
847						return retval;
848				} else
849					dev_err(&client->dev,
850						"%s:fn_number not supported\n",
851								__func__);
852				/*
853				 * Turn on interrupts for this
854				 * function's data sources.
855				 */
856				ctrl_offset = pdata->fn01_ctrl_base_addr + 1 +
857							rfi->index_to_intr_reg;
858				retval = synaptics_rmi4_i2c_byte_write(pdata,
859							ctrl_offset,
860							rfi->intr_mask);
861				if (retval < 0)
862					return retval;
863			}
864		}
865	}
866	return 0;
867}
868
869/*
870 * Descriptor structure.
871 * Describes the number of i2c devices on the bus that speak RMI.
872 */
873static struct synaptics_rmi4_platform_data synaptics_rmi4_platformdata = {
874	.irq_type       = (IRQF_TRIGGER_FALLING | IRQF_SHARED),
875	.x_flip		= false,
876	.y_flip		= true,
877};
878
879/**
880 * synaptics_rmi4_probe() - Initialze the i2c-client touchscreen driver
881 * @i2c: i2c client structure pointer
882 * @id:i2c device id pointer
883 *
884 * This function will allocate and initialize the instance
885 * data and request the irq and set the instance data as the clients
886 * platform data then register the physical driver which will do a scan of
887 * the rmi4 Physical Device Table and enumerate any rmi4 functions that
888 * have data sources associated with them.
889 */
890static int synaptics_rmi4_probe
891	(struct i2c_client *client, const struct i2c_device_id *dev_id)
892{
893	int retval;
894	unsigned char intr_status[4];
895	struct synaptics_rmi4_data *rmi4_data;
896	const struct synaptics_rmi4_platform_data *platformdata =
897						client->dev.platform_data;
898
899	if (!i2c_check_functionality(client->adapter,
900					I2C_FUNC_SMBUS_BYTE_DATA)) {
901		dev_err(&client->dev, "i2c smbus byte data not supported\n");
902		return -EIO;
903	}
904
905	if (!platformdata)
906		platformdata = &synaptics_rmi4_platformdata;
907
908	/* Allocate and initialize the instance data for this client */
909	rmi4_data = kcalloc(2, sizeof(struct synaptics_rmi4_data),
910			    GFP_KERNEL);
911	if (!rmi4_data)
912		return -ENOMEM;
913
914	rmi4_data->input_dev = input_allocate_device();
915	if (rmi4_data->input_dev == NULL) {
916		retval = -ENOMEM;
917		goto err_input;
918	}
919
920	rmi4_data->regulator = regulator_get(&client->dev, "vdd");
921	if (IS_ERR(rmi4_data->regulator)) {
922		dev_err(&client->dev, "%s:get regulator failed\n",
923							__func__);
924		retval = PTR_ERR(rmi4_data->regulator);
925		goto err_get_regulator;
926	}
927	retval = regulator_enable(rmi4_data->regulator);
928	if (retval < 0) {
929		dev_err(&client->dev, "%s:regulator enable failed\n",
930							__func__);
931		goto err_regulator_enable;
932	}
933	init_waitqueue_head(&rmi4_data->wait);
934	/*
935	 * Copy i2c_client pointer into RTID's i2c_client pointer for
936	 * later use in rmi4_read, rmi4_write, etc.
937	 */
938	rmi4_data->i2c_client		= client;
939	/* So we set the page correctly the first time */
940	rmi4_data->current_page		= MASK_16BIT;
941	rmi4_data->board		= platformdata;
942	rmi4_data->touch_stopped	= false;
943
944	/* init the mutexes for maintain the lists */
945	mutex_init(&(rmi4_data->fn_list_mutex));
946	mutex_init(&(rmi4_data->rmi4_page_mutex));
947
948	/*
949	 * Register physical driver - this will call the detect function that
950	 * will then scan the device and determine the supported
951	 * rmi4 functions.
952	 */
953	retval = synaptics_rmi4_i2c_query_device(rmi4_data);
954	if (retval) {
955		dev_err(&client->dev, "%s: rmi4 query device failed\n",
956							__func__);
957		goto err_query_dev;
958	}
959
960	/* Store the instance data in the i2c_client */
961	i2c_set_clientdata(client, rmi4_data);
962
963	/*initialize the input device parameters */
964	rmi4_data->input_dev->name	= DRIVER_NAME;
965	rmi4_data->input_dev->phys	= "Synaptics_Clearpad";
966	rmi4_data->input_dev->id.bustype = BUS_I2C;
967	rmi4_data->input_dev->dev.parent = &client->dev;
968	input_set_drvdata(rmi4_data->input_dev, rmi4_data);
969
970	/* Initialize the function handlers for rmi4 */
971	set_bit(EV_SYN, rmi4_data->input_dev->evbit);
972	set_bit(EV_KEY, rmi4_data->input_dev->evbit);
973	set_bit(EV_ABS, rmi4_data->input_dev->evbit);
974
975	input_set_abs_params(rmi4_data->input_dev, ABS_MT_POSITION_X, 0,
976					rmi4_data->sensor_max_x, 0, 0);
977	input_set_abs_params(rmi4_data->input_dev, ABS_MT_POSITION_Y, 0,
978					rmi4_data->sensor_max_y, 0, 0);
979	input_set_abs_params(rmi4_data->input_dev, ABS_MT_TOUCH_MAJOR, 0,
980						MAX_TOUCH_MAJOR, 0, 0);
981	input_mt_init_slots(rmi4_data->input_dev,
982				rmi4_data->fingers_supported, 0);
983
984	/* Clear interrupts */
985	synaptics_rmi4_i2c_block_read(rmi4_data,
986			rmi4_data->fn01_data_base_addr + 1, intr_status,
987				rmi4_data->number_of_interrupt_register);
988	retval = request_threaded_irq(client->irq, NULL,
989					synaptics_rmi4_irq,
990					platformdata->irq_type,
991					DRIVER_NAME, rmi4_data);
992	if (retval) {
993		dev_err(&client->dev, "%s:Unable to get attn irq %d\n",
994				__func__, client->irq);
995		goto err_query_dev;
996	}
997
998	retval = input_register_device(rmi4_data->input_dev);
999	if (retval) {
1000		dev_err(&client->dev, "%s:input register failed\n", __func__);
1001		goto err_free_irq;
1002	}
1003
1004	return retval;
1005
1006err_free_irq:
1007	free_irq(client->irq, rmi4_data);
1008err_query_dev:
1009	regulator_disable(rmi4_data->regulator);
1010err_regulator_enable:
1011	regulator_put(rmi4_data->regulator);
1012err_get_regulator:
1013	input_free_device(rmi4_data->input_dev);
1014	rmi4_data->input_dev = NULL;
1015err_input:
1016	kfree(rmi4_data);
1017
1018	return retval;
1019}
1020/**
1021 * synaptics_rmi4_remove() - Removes the i2c-client touchscreen driver
1022 * @client: i2c client structure pointer
1023 *
1024 * This function uses to remove the i2c-client
1025 * touchscreen driver and returns integer.
1026 */
1027static int synaptics_rmi4_remove(struct i2c_client *client)
1028{
1029	struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
1030
1031	rmi4_data->touch_stopped = true;
1032	wake_up(&rmi4_data->wait);
1033	free_irq(client->irq, rmi4_data);
1034	input_unregister_device(rmi4_data->input_dev);
1035	regulator_disable(rmi4_data->regulator);
1036	regulator_put(rmi4_data->regulator);
1037	kfree(rmi4_data);
1038
1039	return 0;
1040}
1041
1042#ifdef CONFIG_PM
1043/**
1044 * synaptics_rmi4_suspend() - suspend the touch screen controller
1045 * @dev: pointer to device structure
1046 *
1047 * This function is used to suspend the
1048 * touch panel controller and returns integer
1049 */
1050static int synaptics_rmi4_suspend(struct device *dev)
1051{
1052	/* Touch sleep mode */
1053	int retval;
1054	unsigned char intr_status;
1055	struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
1056
1057	rmi4_data->touch_stopped = true;
1058	disable_irq(rmi4_data->i2c_client->irq);
1059
1060	retval = synaptics_rmi4_i2c_block_read(rmi4_data,
1061				rmi4_data->fn01_data_base_addr + 1,
1062				&intr_status,
1063				rmi4_data->number_of_interrupt_register);
1064	if (retval < 0)
1065		return retval;
1066
1067	retval = synaptics_rmi4_i2c_byte_write(rmi4_data,
1068					rmi4_data->fn01_ctrl_base_addr + 1,
1069					(intr_status & ~TOUCHPAD_CTRL_INTR));
1070	if (retval < 0)
1071		return retval;
1072
1073	regulator_disable(rmi4_data->regulator);
1074
1075	return 0;
1076}
1077/**
1078 * synaptics_rmi4_resume() - resume the touch screen controller
1079 * @dev: pointer to device structure
1080 *
1081 * This function is used to resume the touch panel
1082 * controller and returns integer.
1083 */
1084static int synaptics_rmi4_resume(struct device *dev)
1085{
1086	int retval;
1087	unsigned char intr_status;
1088	struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
1089
1090	retval = regulator_enable(rmi4_data->regulator);
1091	if (retval) {
1092		dev_err(dev, "Regulator enable failed (%d)\n", retval);
1093		return retval;
1094	}
1095
1096	enable_irq(rmi4_data->i2c_client->irq);
1097	rmi4_data->touch_stopped = false;
1098
1099	retval = synaptics_rmi4_i2c_block_read(rmi4_data,
1100				rmi4_data->fn01_data_base_addr + 1,
1101				&intr_status,
1102				rmi4_data->number_of_interrupt_register);
1103	if (retval < 0)
1104		return retval;
1105
1106	retval = synaptics_rmi4_i2c_byte_write(rmi4_data,
1107					rmi4_data->fn01_ctrl_base_addr + 1,
1108					(intr_status | TOUCHPAD_CTRL_INTR));
1109	if (retval < 0)
1110		return retval;
1111
1112	return 0;
1113}
1114
1115static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = {
1116	.suspend = synaptics_rmi4_suspend,
1117	.resume  = synaptics_rmi4_resume,
1118};
1119#endif
1120
1121static const struct i2c_device_id synaptics_rmi4_id_table[] = {
1122	{ DRIVER_NAME, 0 },
1123	{ },
1124};
1125MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table);
1126
1127static struct i2c_driver synaptics_rmi4_driver = {
1128	.driver = {
1129		.name	=	DRIVER_NAME,
1130		.owner	=	THIS_MODULE,
1131#ifdef CONFIG_PM
1132		.pm	=	&synaptics_rmi4_dev_pm_ops,
1133#endif
1134	},
1135	.probe		=	synaptics_rmi4_probe,
1136	.remove		=	synaptics_rmi4_remove,
1137	.id_table	=	synaptics_rmi4_id_table,
1138};
1139
1140module_i2c_driver(synaptics_rmi4_driver);
1141
1142MODULE_LICENSE("GPL v2");
1143MODULE_AUTHOR("naveen.gaddipati@stericsson.com, js.ha@stericsson.com");
1144MODULE_DESCRIPTION("synaptics rmi4 i2c touch Driver");
1145MODULE_ALIAS("i2c:synaptics_rmi4_ts");
1146