• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* drivers/input/keyboard/synaptics_i2c_rmi.c
2  *
3  * Copyright (C) 2007 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/earlysuspend.h>
19 #include <linux/hrtimer.h>
20 #include <linux/i2c.h>
21 #include <linux/input.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/synaptics_i2c_rmi.h>
27 
28 static struct workqueue_struct *synaptics_wq;
29 
30 struct synaptics_ts_data {
31 	uint16_t addr;
32 	struct i2c_client *client;
33 	struct input_dev *input_dev;
34 	int use_irq;
35 	bool has_relative_report;
36 	struct hrtimer timer;
37 	struct work_struct  work;
38 	uint16_t max[2];
39 	int snap_state[2][2];
40 	int snap_down_on[2];
41 	int snap_down_off[2];
42 	int snap_up_on[2];
43 	int snap_up_off[2];
44 	int snap_down[2];
45 	int snap_up[2];
46 	uint32_t flags;
47 	int reported_finger_count;
48 	int8_t sensitivity_adjust;
49 	int (*power)(int on);
50 	struct early_suspend early_suspend;
51 };
52 
53 #ifdef CONFIG_HAS_EARLYSUSPEND
54 static void synaptics_ts_early_suspend(struct early_suspend *h);
55 static void synaptics_ts_late_resume(struct early_suspend *h);
56 #endif
57 
synaptics_init_panel(struct synaptics_ts_data * ts)58 static int synaptics_init_panel(struct synaptics_ts_data *ts)
59 {
60 	int ret;
61 
62 	ret = i2c_smbus_write_byte_data(ts->client, 0xff, 0x10); /* page select = 0x10 */
63 	if (ret < 0) {
64 		printk(KERN_ERR "i2c_smbus_write_byte_data failed for page select\n");
65 		goto err_page_select_failed;
66 	}
67 	ret = i2c_smbus_write_byte_data(ts->client, 0x41, 0x04); /* Set "No Clip Z" */
68 	if (ret < 0)
69 		printk(KERN_ERR "i2c_smbus_write_byte_data failed for No Clip Z\n");
70 
71 	ret = i2c_smbus_write_byte_data(ts->client, 0x44,
72 					ts->sensitivity_adjust);
73 	if (ret < 0)
74 		pr_err("synaptics_ts: failed to set Sensitivity Adjust\n");
75 
76 err_page_select_failed:
77 	ret = i2c_smbus_write_byte_data(ts->client, 0xff, 0x04); /* page select = 0x04 */
78 	if (ret < 0)
79 		printk(KERN_ERR "i2c_smbus_write_byte_data failed for page select\n");
80 	ret = i2c_smbus_write_byte_data(ts->client, 0xf0, 0x81); /* normal operation, 80 reports per second */
81 	if (ret < 0)
82 		printk(KERN_ERR "synaptics_ts_resume: i2c_smbus_write_byte_data failed\n");
83 	return ret;
84 }
85 
synaptics_ts_work_func(struct work_struct * work)86 static void synaptics_ts_work_func(struct work_struct *work)
87 {
88 	int i;
89 	int ret;
90 	int bad_data = 0;
91 	struct i2c_msg msg[2];
92 	uint8_t start_reg;
93 	uint8_t buf[15];
94 	struct synaptics_ts_data *ts = container_of(work, struct synaptics_ts_data, work);
95 	int buf_len = ts->has_relative_report ? 15 : 13;
96 
97 	msg[0].addr = ts->client->addr;
98 	msg[0].flags = 0;
99 	msg[0].len = 1;
100 	msg[0].buf = &start_reg;
101 	start_reg = 0x00;
102 	msg[1].addr = ts->client->addr;
103 	msg[1].flags = I2C_M_RD;
104 	msg[1].len = buf_len;
105 	msg[1].buf = buf;
106 
107 	/* printk("synaptics_ts_work_func\n"); */
108 	for (i = 0; i < ((ts->use_irq && !bad_data) ? 1 : 10); i++) {
109 		ret = i2c_transfer(ts->client->adapter, msg, 2);
110 		if (ret < 0) {
111 			printk(KERN_ERR "synaptics_ts_work_func: i2c_transfer failed\n");
112 			bad_data = 1;
113 		} else {
114 			/* printk("synaptics_ts_work_func: %x %x %x %x %x %x" */
115 			/*        " %x %x %x %x %x %x %x %x %x, ret %d\n", */
116 			/*        buf[0], buf[1], buf[2], buf[3], */
117 			/*        buf[4], buf[5], buf[6], buf[7], */
118 			/*        buf[8], buf[9], buf[10], buf[11], */
119 			/*        buf[12], buf[13], buf[14], ret); */
120 			if ((buf[buf_len - 1] & 0xc0) != 0x40) {
121 				printk(KERN_WARNING "synaptics_ts_work_func:"
122 				       " bad read %x %x %x %x %x %x %x %x %x"
123 				       " %x %x %x %x %x %x, ret %d\n",
124 				       buf[0], buf[1], buf[2], buf[3],
125 				       buf[4], buf[5], buf[6], buf[7],
126 				       buf[8], buf[9], buf[10], buf[11],
127 				       buf[12], buf[13], buf[14], ret);
128 				if (bad_data)
129 					synaptics_init_panel(ts);
130 				bad_data = 1;
131 				continue;
132 			}
133 			bad_data = 0;
134 			if ((buf[buf_len - 1] & 1) == 0) {
135 				/* printk("read %d coordinates\n", i); */
136 				break;
137 			} else {
138 				int pos[2][2];
139 				int f, a;
140 				int base;
141 				/* int x = buf[3] | (uint16_t)(buf[2] & 0x1f) << 8; */
142 				/* int y = buf[5] | (uint16_t)(buf[4] & 0x1f) << 8; */
143 				int z = buf[1];
144 				int w = buf[0] >> 4;
145 				int finger = buf[0] & 7;
146 
147 				/* int x2 = buf[3+6] | (uint16_t)(buf[2+6] & 0x1f) << 8; */
148 				/* int y2 = buf[5+6] | (uint16_t)(buf[4+6] & 0x1f) << 8; */
149 				/* int z2 = buf[1+6]; */
150 				/* int w2 = buf[0+6] >> 4; */
151 				/* int finger2 = buf[0+6] & 7; */
152 
153 				/* int dx = (int8_t)buf[12]; */
154 				/* int dy = (int8_t)buf[13]; */
155 				int finger2_pressed;
156 
157 				/* printk("x %4d, y %4d, z %3d, w %2d, F %d, 2nd: x %4d, y %4d, z %3d, w %2d, F %d, dx %4d, dy %4d\n", */
158 				/*	x, y, z, w, finger, */
159 				/*	x2, y2, z2, w2, finger2, */
160 				/*	dx, dy); */
161 
162 				base = 2;
163 				for (f = 0; f < 2; f++) {
164 					uint32_t flip_flag = SYNAPTICS_FLIP_X;
165 					for (a = 0; a < 2; a++) {
166 						int p = buf[base + 1];
167 						p |= (uint16_t)(buf[base] & 0x1f) << 8;
168 						if (ts->flags & flip_flag)
169 							p = ts->max[a] - p;
170 						if (ts->flags & SYNAPTICS_SNAP_TO_INACTIVE_EDGE) {
171 							if (ts->snap_state[f][a]) {
172 								if (p <= ts->snap_down_off[a])
173 									p = ts->snap_down[a];
174 								else if (p >= ts->snap_up_off[a])
175 									p = ts->snap_up[a];
176 								else
177 									ts->snap_state[f][a] = 0;
178 							} else {
179 								if (p <= ts->snap_down_on[a]) {
180 									p = ts->snap_down[a];
181 									ts->snap_state[f][a] = 1;
182 								} else if (p >= ts->snap_up_on[a]) {
183 									p = ts->snap_up[a];
184 									ts->snap_state[f][a] = 1;
185 								}
186 							}
187 						}
188 						pos[f][a] = p;
189 						base += 2;
190 						flip_flag <<= 1;
191 					}
192 					base += 2;
193 					if (ts->flags & SYNAPTICS_SWAP_XY)
194 						swap(pos[f][0], pos[f][1]);
195 				}
196 				if (z) {
197 					input_report_abs(ts->input_dev, ABS_X, pos[0][0]);
198 					input_report_abs(ts->input_dev, ABS_Y, pos[0][1]);
199 				}
200 				input_report_abs(ts->input_dev, ABS_PRESSURE, z);
201 				input_report_abs(ts->input_dev, ABS_TOOL_WIDTH, w);
202 				input_report_key(ts->input_dev, BTN_TOUCH, finger);
203 				finger2_pressed = finger > 1 && finger != 7;
204 				input_report_key(ts->input_dev, BTN_2, finger2_pressed);
205 				if (finger2_pressed) {
206 					input_report_abs(ts->input_dev, ABS_HAT0X, pos[1][0]);
207 					input_report_abs(ts->input_dev, ABS_HAT0Y, pos[1][1]);
208 				}
209 
210 				if (!finger)
211 					z = 0;
212 				input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, z);
213 				input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
214 				input_report_abs(ts->input_dev, ABS_MT_POSITION_X, pos[0][0]);
215 				input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, pos[0][1]);
216 				input_mt_sync(ts->input_dev);
217 				if (finger2_pressed) {
218 					input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, z);
219 					input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
220 					input_report_abs(ts->input_dev, ABS_MT_POSITION_X, pos[1][0]);
221 					input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, pos[1][1]);
222 					input_mt_sync(ts->input_dev);
223 				} else if (ts->reported_finger_count > 1) {
224 					input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0);
225 					input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0);
226 					input_mt_sync(ts->input_dev);
227 				}
228 				ts->reported_finger_count = finger;
229 				input_sync(ts->input_dev);
230 			}
231 		}
232 	}
233 	if (ts->use_irq)
234 		enable_irq(ts->client->irq);
235 }
236 
synaptics_ts_timer_func(struct hrtimer * timer)237 static enum hrtimer_restart synaptics_ts_timer_func(struct hrtimer *timer)
238 {
239 	struct synaptics_ts_data *ts = container_of(timer, struct synaptics_ts_data, timer);
240 	/* printk("synaptics_ts_timer_func\n"); */
241 
242 	queue_work(synaptics_wq, &ts->work);
243 
244 	hrtimer_start(&ts->timer, ktime_set(0, 12500000), HRTIMER_MODE_REL);
245 	return HRTIMER_NORESTART;
246 }
247 
synaptics_ts_irq_handler(int irq,void * dev_id)248 static irqreturn_t synaptics_ts_irq_handler(int irq, void *dev_id)
249 {
250 	struct synaptics_ts_data *ts = dev_id;
251 
252 	/* printk("synaptics_ts_irq_handler\n"); */
253 	disable_irq_nosync(ts->client->irq);
254 	queue_work(synaptics_wq, &ts->work);
255 	return IRQ_HANDLED;
256 }
257 
synaptics_ts_probe(struct i2c_client * client,const struct i2c_device_id * id)258 static int synaptics_ts_probe(
259 	struct i2c_client *client, const struct i2c_device_id *id)
260 {
261 	struct synaptics_ts_data *ts;
262 	uint8_t buf0[4];
263 	uint8_t buf1[8];
264 	struct i2c_msg msg[2];
265 	int ret = 0;
266 	uint16_t max_x, max_y;
267 	int fuzz_x, fuzz_y, fuzz_p, fuzz_w;
268 	struct synaptics_i2c_rmi_platform_data *pdata;
269 	unsigned long irqflags;
270 	int inactive_area_left;
271 	int inactive_area_right;
272 	int inactive_area_top;
273 	int inactive_area_bottom;
274 	int snap_left_on;
275 	int snap_left_off;
276 	int snap_right_on;
277 	int snap_right_off;
278 	int snap_top_on;
279 	int snap_top_off;
280 	int snap_bottom_on;
281 	int snap_bottom_off;
282 	uint32_t panel_version;
283 
284 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
285 		printk(KERN_ERR "synaptics_ts_probe: need I2C_FUNC_I2C\n");
286 		ret = -ENODEV;
287 		goto err_check_functionality_failed;
288 	}
289 
290 	ts = kzalloc(sizeof(*ts), GFP_KERNEL);
291 	if (ts == NULL) {
292 		ret = -ENOMEM;
293 		goto err_alloc_data_failed;
294 	}
295 	INIT_WORK(&ts->work, synaptics_ts_work_func);
296 	ts->client = client;
297 	i2c_set_clientdata(client, ts);
298 	pdata = client->dev.platform_data;
299 	if (pdata)
300 		ts->power = pdata->power;
301 	if (ts->power) {
302 		ret = ts->power(1);
303 		if (ret < 0) {
304 			printk(KERN_ERR "synaptics_ts_probe power on failed\n");
305 			goto err_power_failed;
306 		}
307 	}
308 
309 	ret = i2c_smbus_write_byte_data(ts->client, 0xf4, 0x01); /* device command = reset */
310 	if (ret < 0) {
311 		printk(KERN_ERR "i2c_smbus_write_byte_data failed\n");
312 		/* fail? */
313 	}
314 	{
315 		int retry = 10;
316 		while (retry-- > 0) {
317 			ret = i2c_smbus_read_byte_data(ts->client, 0xe4);
318 			if (ret >= 0)
319 				break;
320 			msleep(100);
321 		}
322 	}
323 	if (ret < 0) {
324 		printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
325 		goto err_detect_failed;
326 	}
327 	printk(KERN_INFO "synaptics_ts_probe: Product Major Version %x\n", ret);
328 	panel_version = ret << 8;
329 	ret = i2c_smbus_read_byte_data(ts->client, 0xe5);
330 	if (ret < 0) {
331 		printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
332 		goto err_detect_failed;
333 	}
334 	printk(KERN_INFO "synaptics_ts_probe: Product Minor Version %x\n", ret);
335 	panel_version |= ret;
336 
337 	ret = i2c_smbus_read_byte_data(ts->client, 0xe3);
338 	if (ret < 0) {
339 		printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
340 		goto err_detect_failed;
341 	}
342 	printk(KERN_INFO "synaptics_ts_probe: product property %x\n", ret);
343 
344 	if (pdata) {
345 		while (pdata->version > panel_version)
346 			pdata++;
347 		ts->flags = pdata->flags;
348 		ts->sensitivity_adjust = pdata->sensitivity_adjust;
349 		irqflags = pdata->irqflags;
350 		inactive_area_left = pdata->inactive_left;
351 		inactive_area_right = pdata->inactive_right;
352 		inactive_area_top = pdata->inactive_top;
353 		inactive_area_bottom = pdata->inactive_bottom;
354 		snap_left_on = pdata->snap_left_on;
355 		snap_left_off = pdata->snap_left_off;
356 		snap_right_on = pdata->snap_right_on;
357 		snap_right_off = pdata->snap_right_off;
358 		snap_top_on = pdata->snap_top_on;
359 		snap_top_off = pdata->snap_top_off;
360 		snap_bottom_on = pdata->snap_bottom_on;
361 		snap_bottom_off = pdata->snap_bottom_off;
362 		fuzz_x = pdata->fuzz_x;
363 		fuzz_y = pdata->fuzz_y;
364 		fuzz_p = pdata->fuzz_p;
365 		fuzz_w = pdata->fuzz_w;
366 	} else {
367 		irqflags = 0;
368 		inactive_area_left = 0;
369 		inactive_area_right = 0;
370 		inactive_area_top = 0;
371 		inactive_area_bottom = 0;
372 		snap_left_on = 0;
373 		snap_left_off = 0;
374 		snap_right_on = 0;
375 		snap_right_off = 0;
376 		snap_top_on = 0;
377 		snap_top_off = 0;
378 		snap_bottom_on = 0;
379 		snap_bottom_off = 0;
380 		fuzz_x = 0;
381 		fuzz_y = 0;
382 		fuzz_p = 0;
383 		fuzz_w = 0;
384 	}
385 
386 	ret = i2c_smbus_read_byte_data(ts->client, 0xf0);
387 	if (ret < 0) {
388 		printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
389 		goto err_detect_failed;
390 	}
391 	printk(KERN_INFO "synaptics_ts_probe: device control %x\n", ret);
392 
393 	ret = i2c_smbus_read_byte_data(ts->client, 0xf1);
394 	if (ret < 0) {
395 		printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
396 		goto err_detect_failed;
397 	}
398 	printk(KERN_INFO "synaptics_ts_probe: interrupt enable %x\n", ret);
399 
400 	ret = i2c_smbus_write_byte_data(ts->client, 0xf1, 0); /* disable interrupt */
401 	if (ret < 0) {
402 		printk(KERN_ERR "i2c_smbus_write_byte_data failed\n");
403 		goto err_detect_failed;
404 	}
405 
406 	msg[0].addr = ts->client->addr;
407 	msg[0].flags = 0;
408 	msg[0].len = 1;
409 	msg[0].buf = buf0;
410 	buf0[0] = 0xe0;
411 	msg[1].addr = ts->client->addr;
412 	msg[1].flags = I2C_M_RD;
413 	msg[1].len = 8;
414 	msg[1].buf = buf1;
415 	ret = i2c_transfer(ts->client->adapter, msg, 2);
416 	if (ret < 0) {
417 		printk(KERN_ERR "i2c_transfer failed\n");
418 		goto err_detect_failed;
419 	}
420 	printk(KERN_INFO "synaptics_ts_probe: 0xe0: %x %x %x %x %x %x %x %x\n",
421 	       buf1[0], buf1[1], buf1[2], buf1[3],
422 	       buf1[4], buf1[5], buf1[6], buf1[7]);
423 
424 	ret = i2c_smbus_write_byte_data(ts->client, 0xff, 0x10); /* page select = 0x10 */
425 	if (ret < 0) {
426 		printk(KERN_ERR "i2c_smbus_write_byte_data failed for page select\n");
427 		goto err_detect_failed;
428 	}
429 	ret = i2c_smbus_read_word_data(ts->client, 0x02);
430 	if (ret < 0) {
431 		printk(KERN_ERR "i2c_smbus_read_word_data failed\n");
432 		goto err_detect_failed;
433 	}
434 	ts->has_relative_report = !(ret & 0x100);
435 	printk(KERN_INFO "synaptics_ts_probe: Sensor properties %x\n", ret);
436 	ret = i2c_smbus_read_word_data(ts->client, 0x04);
437 	if (ret < 0) {
438 		printk(KERN_ERR "i2c_smbus_read_word_data failed\n");
439 		goto err_detect_failed;
440 	}
441 	ts->max[0] = max_x = (ret >> 8 & 0xff) | ((ret & 0x1f) << 8);
442 	ret = i2c_smbus_read_word_data(ts->client, 0x06);
443 	if (ret < 0) {
444 		printk(KERN_ERR "i2c_smbus_read_word_data failed\n");
445 		goto err_detect_failed;
446 	}
447 	ts->max[1] = max_y = (ret >> 8 & 0xff) | ((ret & 0x1f) << 8);
448 	if (ts->flags & SYNAPTICS_SWAP_XY)
449 		swap(max_x, max_y);
450 
451 	ret = synaptics_init_panel(ts); /* will also switch back to page 0x04 */
452 	if (ret < 0) {
453 		printk(KERN_ERR "synaptics_init_panel failed\n");
454 		goto err_detect_failed;
455 	}
456 
457 	ts->input_dev = input_allocate_device();
458 	if (ts->input_dev == NULL) {
459 		ret = -ENOMEM;
460 		printk(KERN_ERR "synaptics_ts_probe: Failed to allocate input device\n");
461 		goto err_input_dev_alloc_failed;
462 	}
463 	ts->input_dev->name = "synaptics-rmi-touchscreen";
464 	set_bit(EV_SYN, ts->input_dev->evbit);
465 	set_bit(EV_KEY, ts->input_dev->evbit);
466 	set_bit(BTN_TOUCH, ts->input_dev->keybit);
467 	set_bit(BTN_2, ts->input_dev->keybit);
468 	set_bit(EV_ABS, ts->input_dev->evbit);
469 	inactive_area_left = inactive_area_left * max_x / 0x10000;
470 	inactive_area_right = inactive_area_right * max_x / 0x10000;
471 	inactive_area_top = inactive_area_top * max_y / 0x10000;
472 	inactive_area_bottom = inactive_area_bottom * max_y / 0x10000;
473 	snap_left_on = snap_left_on * max_x / 0x10000;
474 	snap_left_off = snap_left_off * max_x / 0x10000;
475 	snap_right_on = snap_right_on * max_x / 0x10000;
476 	snap_right_off = snap_right_off * max_x / 0x10000;
477 	snap_top_on = snap_top_on * max_y / 0x10000;
478 	snap_top_off = snap_top_off * max_y / 0x10000;
479 	snap_bottom_on = snap_bottom_on * max_y / 0x10000;
480 	snap_bottom_off = snap_bottom_off * max_y / 0x10000;
481 	fuzz_x = fuzz_x * max_x / 0x10000;
482 	fuzz_y = fuzz_y * max_y / 0x10000;
483 	ts->snap_down[!!(ts->flags & SYNAPTICS_SWAP_XY)] = -inactive_area_left;
484 	ts->snap_up[!!(ts->flags & SYNAPTICS_SWAP_XY)] = max_x + inactive_area_right;
485 	ts->snap_down[!(ts->flags & SYNAPTICS_SWAP_XY)] = -inactive_area_top;
486 	ts->snap_up[!(ts->flags & SYNAPTICS_SWAP_XY)] = max_y + inactive_area_bottom;
487 	ts->snap_down_on[!!(ts->flags & SYNAPTICS_SWAP_XY)] = snap_left_on;
488 	ts->snap_down_off[!!(ts->flags & SYNAPTICS_SWAP_XY)] = snap_left_off;
489 	ts->snap_up_on[!!(ts->flags & SYNAPTICS_SWAP_XY)] = max_x - snap_right_on;
490 	ts->snap_up_off[!!(ts->flags & SYNAPTICS_SWAP_XY)] = max_x - snap_right_off;
491 	ts->snap_down_on[!(ts->flags & SYNAPTICS_SWAP_XY)] = snap_top_on;
492 	ts->snap_down_off[!(ts->flags & SYNAPTICS_SWAP_XY)] = snap_top_off;
493 	ts->snap_up_on[!(ts->flags & SYNAPTICS_SWAP_XY)] = max_y - snap_bottom_on;
494 	ts->snap_up_off[!(ts->flags & SYNAPTICS_SWAP_XY)] = max_y - snap_bottom_off;
495 	printk(KERN_INFO "synaptics_ts_probe: max_x %d, max_y %d\n", max_x, max_y);
496 	printk(KERN_INFO "synaptics_ts_probe: inactive_x %d %d, inactive_y %d %d\n",
497 	       inactive_area_left, inactive_area_right,
498 	       inactive_area_top, inactive_area_bottom);
499 	printk(KERN_INFO "synaptics_ts_probe: snap_x %d-%d %d-%d, snap_y %d-%d %d-%d\n",
500 	       snap_left_on, snap_left_off, snap_right_on, snap_right_off,
501 	       snap_top_on, snap_top_off, snap_bottom_on, snap_bottom_off);
502 	input_set_abs_params(ts->input_dev, ABS_X, -inactive_area_left, max_x + inactive_area_right, fuzz_x, 0);
503 	input_set_abs_params(ts->input_dev, ABS_Y, -inactive_area_top, max_y + inactive_area_bottom, fuzz_y, 0);
504 	input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 255, fuzz_p, 0);
505 	input_set_abs_params(ts->input_dev, ABS_TOOL_WIDTH, 0, 15, fuzz_w, 0);
506 	input_set_abs_params(ts->input_dev, ABS_HAT0X, -inactive_area_left, max_x + inactive_area_right, fuzz_x, 0);
507 	input_set_abs_params(ts->input_dev, ABS_HAT0Y, -inactive_area_top, max_y + inactive_area_bottom, fuzz_y, 0);
508 	input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, -inactive_area_left, max_x + inactive_area_right, fuzz_x, 0);
509 	input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, -inactive_area_top, max_y + inactive_area_bottom, fuzz_y, 0);
510 	input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, fuzz_p, 0);
511 	input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 15, fuzz_w, 0);
512 	/* ts->input_dev->name = ts->keypad_info->name; */
513 	ret = input_register_device(ts->input_dev);
514 	if (ret) {
515 		printk(KERN_ERR "synaptics_ts_probe: Unable to register %s input device\n", ts->input_dev->name);
516 		goto err_input_register_device_failed;
517 	}
518 	if (client->irq) {
519 		ret = request_irq(client->irq, synaptics_ts_irq_handler, irqflags, client->name, ts);
520 		if (ret == 0) {
521 			ret = i2c_smbus_write_byte_data(ts->client, 0xf1, 0x01); /* enable abs int */
522 			if (ret)
523 				free_irq(client->irq, ts);
524 		}
525 		if (ret == 0)
526 			ts->use_irq = 1;
527 		else
528 			dev_err(&client->dev, "request_irq failed\n");
529 	}
530 	if (!ts->use_irq) {
531 		hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
532 		ts->timer.function = synaptics_ts_timer_func;
533 		hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
534 	}
535 #ifdef CONFIG_HAS_EARLYSUSPEND
536 	ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
537 	ts->early_suspend.suspend = synaptics_ts_early_suspend;
538 	ts->early_suspend.resume = synaptics_ts_late_resume;
539 	register_early_suspend(&ts->early_suspend);
540 #endif
541 
542 	printk(KERN_INFO "synaptics_ts_probe: Start touchscreen %s in %s mode\n", ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
543 
544 	return 0;
545 
546 err_input_register_device_failed:
547 	input_free_device(ts->input_dev);
548 
549 err_input_dev_alloc_failed:
550 err_detect_failed:
551 err_power_failed:
552 	kfree(ts);
553 err_alloc_data_failed:
554 err_check_functionality_failed:
555 	return ret;
556 }
557 
synaptics_ts_remove(struct i2c_client * client)558 static int synaptics_ts_remove(struct i2c_client *client)
559 {
560 	struct synaptics_ts_data *ts = i2c_get_clientdata(client);
561 	unregister_early_suspend(&ts->early_suspend);
562 	if (ts->use_irq)
563 		free_irq(client->irq, ts);
564 	else
565 		hrtimer_cancel(&ts->timer);
566 	input_unregister_device(ts->input_dev);
567 	kfree(ts);
568 	return 0;
569 }
570 
synaptics_ts_suspend(struct i2c_client * client,pm_message_t mesg)571 static int synaptics_ts_suspend(struct i2c_client *client, pm_message_t mesg)
572 {
573 	int ret;
574 	struct synaptics_ts_data *ts = i2c_get_clientdata(client);
575 
576 	if (ts->use_irq)
577 		disable_irq(client->irq);
578 	else
579 		hrtimer_cancel(&ts->timer);
580 	ret = cancel_work_sync(&ts->work);
581 	if (ret && ts->use_irq) /* if work was pending disable-count is now 2 */
582 		enable_irq(client->irq);
583 	ret = i2c_smbus_write_byte_data(ts->client, 0xf1, 0); /* disable interrupt */
584 	if (ret < 0)
585 		printk(KERN_ERR "synaptics_ts_suspend: i2c_smbus_write_byte_data failed\n");
586 
587 	ret = i2c_smbus_write_byte_data(client, 0xf0, 0x86); /* deep sleep */
588 	if (ret < 0)
589 		printk(KERN_ERR "synaptics_ts_suspend: i2c_smbus_write_byte_data failed\n");
590 	if (ts->power) {
591 		ret = ts->power(0);
592 		if (ret < 0)
593 			printk(KERN_ERR "synaptics_ts_resume power off failed\n");
594 	}
595 	return 0;
596 }
597 
synaptics_ts_resume(struct i2c_client * client)598 static int synaptics_ts_resume(struct i2c_client *client)
599 {
600 	int ret;
601 	struct synaptics_ts_data *ts = i2c_get_clientdata(client);
602 
603 	if (ts->power) {
604 		ret = ts->power(1);
605 		if (ret < 0)
606 			printk(KERN_ERR "synaptics_ts_resume power on failed\n");
607 	}
608 
609 	synaptics_init_panel(ts);
610 
611 	if (ts->use_irq)
612 		enable_irq(client->irq);
613 
614 	if (!ts->use_irq)
615 		hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
616 	else
617 		i2c_smbus_write_byte_data(ts->client, 0xf1, 0x01); /* enable abs int */
618 
619 	return 0;
620 }
621 
622 #ifdef CONFIG_HAS_EARLYSUSPEND
synaptics_ts_early_suspend(struct early_suspend * h)623 static void synaptics_ts_early_suspend(struct early_suspend *h)
624 {
625 	struct synaptics_ts_data *ts;
626 	ts = container_of(h, struct synaptics_ts_data, early_suspend);
627 	synaptics_ts_suspend(ts->client, PMSG_SUSPEND);
628 }
629 
synaptics_ts_late_resume(struct early_suspend * h)630 static void synaptics_ts_late_resume(struct early_suspend *h)
631 {
632 	struct synaptics_ts_data *ts;
633 	ts = container_of(h, struct synaptics_ts_data, early_suspend);
634 	synaptics_ts_resume(ts->client);
635 }
636 #endif
637 
638 static const struct i2c_device_id synaptics_ts_id[] = {
639 	{ SYNAPTICS_I2C_RMI_NAME, 0 },
640 	{ }
641 };
642 
643 static struct i2c_driver synaptics_ts_driver = {
644 	.probe		= synaptics_ts_probe,
645 	.remove		= synaptics_ts_remove,
646 #ifndef CONFIG_HAS_EARLYSUSPEND
647 	.suspend	= synaptics_ts_suspend,
648 	.resume		= synaptics_ts_resume,
649 #endif
650 	.id_table	= synaptics_ts_id,
651 	.driver = {
652 		.name	= SYNAPTICS_I2C_RMI_NAME,
653 	},
654 };
655 
synaptics_ts_init(void)656 static int __devinit synaptics_ts_init(void)
657 {
658 	synaptics_wq = create_singlethread_workqueue("synaptics_wq");
659 	if (!synaptics_wq)
660 		return -ENOMEM;
661 	return i2c_add_driver(&synaptics_ts_driver);
662 }
663 
synaptics_ts_exit(void)664 static void __exit synaptics_ts_exit(void)
665 {
666 	i2c_del_driver(&synaptics_ts_driver);
667 	if (synaptics_wq)
668 		destroy_workqueue(synaptics_wq);
669 }
670 
671 module_init(synaptics_ts_init);
672 module_exit(synaptics_ts_exit);
673 
674 MODULE_DESCRIPTION("Synaptics Touchscreen Driver");
675 MODULE_LICENSE("GPL");
676