1 /* drivers/input/keyboard/synaptics_i2c_rmi.c
3 * Copyright (C) 2007 Google, Inc.
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.
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.
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>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/synaptics_i2c_rmi.h>
28 static struct workqueue_struct *synaptics_wq;
30 struct synaptics_ts_data {
32 struct i2c_client *client;
33 struct input_dev *input_dev;
35 bool has_relative_report;
37 struct work_struct work;
48 struct early_suspend early_suspend;
51 #ifdef CONFIG_HAS_EARLYSUSPEND
52 static void synaptics_ts_early_suspend(struct early_suspend *h);
53 static void synaptics_ts_late_resume(struct early_suspend *h);
56 static int synaptics_init_panel(struct synaptics_ts_data *ts)
60 ret = i2c_smbus_write_byte_data(ts->client, 0xff, 0x10); /* page select = 0x10 */
62 printk(KERN_ERR "i2c_smbus_write_byte_data failed for page select\n");
63 goto err_page_select_failed;
65 ret = i2c_smbus_write_byte_data(ts->client, 0x41, 0x04); /* Set "No Clip Z" */
67 printk(KERN_ERR "i2c_smbus_write_byte_data failed for No Clip Z\n");
69 err_page_select_failed:
70 ret = i2c_smbus_write_byte_data(ts->client, 0xff, 0x04); /* page select = 0x04 */
72 printk(KERN_ERR "i2c_smbus_write_byte_data failed for page select\n");
73 ret = i2c_smbus_write_byte_data(ts->client, 0xf0, 0x81); /* normal operation, 80 reports per second */
75 printk(KERN_ERR "synaptics_ts_resume: i2c_smbus_write_byte_data failed\n");
79 static void synaptics_ts_work_func(struct work_struct *work)
84 struct i2c_msg msg[2];
87 struct synaptics_ts_data *ts = container_of(work, struct synaptics_ts_data, work);
88 int buf_len = ts->has_relative_report ? 15 : 13;
90 msg[0].addr = ts->client->addr;
93 msg[0].buf = &start_reg;
95 msg[1].addr = ts->client->addr;
96 msg[1].flags = I2C_M_RD;
100 /* printk("synaptics_ts_work_func\n"); */
101 for (i = 0; i < ((ts->use_irq && !bad_data) ? 1 : 10); i++) {
102 ret = i2c_transfer(ts->client->adapter, msg, 2);
104 printk(KERN_ERR "synaptics_ts_work_func: i2c_transfer failed\n");
107 /* printk("synaptics_ts_work_func: %x %x %x %x %x %x" */
108 /* " %x %x %x %x %x %x %x %x %x, ret %d\n", */
109 /* buf[0], buf[1], buf[2], buf[3], */
110 /* buf[4], buf[5], buf[6], buf[7], */
111 /* buf[8], buf[9], buf[10], buf[11], */
112 /* buf[12], buf[13], buf[14], ret); */
113 if ((buf[buf_len - 1] & 0xc0) != 0x40) {
114 printk(KERN_WARNING "synaptics_ts_work_func:"
115 " bad read %x %x %x %x %x %x %x %x %x"
116 " %x %x %x %x %x %x, ret %d\n",
117 buf[0], buf[1], buf[2], buf[3],
118 buf[4], buf[5], buf[6], buf[7],
119 buf[8], buf[9], buf[10], buf[11],
120 buf[12], buf[13], buf[14], ret);
122 synaptics_init_panel(ts);
127 if ((buf[buf_len - 1] & 1) == 0) {
128 /* printk("read %d coordinates\n", i); */
134 /* int x = buf[3] | (uint16_t)(buf[2] & 0x1f) << 8; */
135 /* int y = buf[5] | (uint16_t)(buf[4] & 0x1f) << 8; */
138 int finger = buf[0] & 7;
140 /* int x2 = buf[3+6] | (uint16_t)(buf[2+6] & 0x1f) << 8; */
141 /* int y2 = buf[5+6] | (uint16_t)(buf[4+6] & 0x1f) << 8; */
142 /* int z2 = buf[1+6]; */
143 /* int w2 = buf[0+6] >> 4; */
144 /* int finger2 = buf[0+6] & 7; */
146 /* int dx = (int8_t)buf[12]; */
147 /* int dy = (int8_t)buf[13]; */
150 /* 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", */
151 /* x, y, z, w, finger, */
152 /* x2, y2, z2, w2, finger2, */
156 for (f = 0; f < 2; f++) {
157 uint32_t flip_flag = SYNAPTICS_FLIP_X;
158 for (a = 0; a < 2; a++) {
159 int p = buf[base + 1];
160 p |= (uint16_t)(buf[base] & 0x1f) << 8;
161 if (ts->flags & flip_flag)
163 if (ts->flags & SYNAPTICS_SNAP_TO_INACTIVE_EDGE) {
164 if (ts->snap_state[f][a]) {
165 if (p <= ts->snap_down_off[a])
166 p = ts->snap_down[a];
167 else if (p >= ts->snap_up_off[a])
170 ts->snap_state[f][a] = 0;
172 if (p <= ts->snap_down_on[a]) {
173 p = ts->snap_down[a];
174 ts->snap_state[f][a] = 1;
175 } else if (p >= ts->snap_up_on[a]) {
177 ts->snap_state[f][a] = 1;
186 if (ts->flags & SYNAPTICS_SWAP_XY)
187 swap(pos[f][0], pos[f][1]);
190 input_report_abs(ts->input_dev, ABS_X, pos[0][0]);
191 input_report_abs(ts->input_dev, ABS_Y, pos[0][1]);
193 input_report_abs(ts->input_dev, ABS_PRESSURE, z);
194 input_report_abs(ts->input_dev, ABS_TOOL_WIDTH, w);
195 input_report_key(ts->input_dev, BTN_TOUCH, finger);
196 finger2_pressed = finger > 1 && finger != 7;
197 input_report_key(ts->input_dev, BTN_2, finger2_pressed);
198 if (finger2_pressed) {
199 input_report_abs(ts->input_dev, ABS_HAT0X, pos[1][0]);
200 input_report_abs(ts->input_dev, ABS_HAT0Y, pos[1][1]);
202 input_sync(ts->input_dev);
207 enable_irq(ts->client->irq);
210 static enum hrtimer_restart synaptics_ts_timer_func(struct hrtimer *timer)
212 struct synaptics_ts_data *ts = container_of(timer, struct synaptics_ts_data, timer);
213 /* printk("synaptics_ts_timer_func\n"); */
215 queue_work(synaptics_wq, &ts->work);
217 hrtimer_start(&ts->timer, ktime_set(0, 12500000), HRTIMER_MODE_REL);
218 return HRTIMER_NORESTART;
221 static irqreturn_t synaptics_ts_irq_handler(int irq, void *dev_id)
223 struct synaptics_ts_data *ts = dev_id;
225 /* printk("synaptics_ts_irq_handler\n"); */
226 disable_irq_nosync(ts->client->irq);
227 queue_work(synaptics_wq, &ts->work);
231 static int synaptics_ts_probe(
232 struct i2c_client *client, const struct i2c_device_id *id)
234 struct synaptics_ts_data *ts;
237 struct i2c_msg msg[2];
239 uint16_t max_x, max_y;
240 int fuzz_x, fuzz_y, fuzz_p, fuzz_w;
241 struct synaptics_i2c_rmi_platform_data *pdata;
242 int inactive_area_left;
243 int inactive_area_right;
244 int inactive_area_top;
245 int inactive_area_bottom;
254 uint32_t panel_version;
256 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
257 printk(KERN_ERR "synaptics_ts_probe: need I2C_FUNC_I2C\n");
259 goto err_check_functionality_failed;
262 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
265 goto err_alloc_data_failed;
267 INIT_WORK(&ts->work, synaptics_ts_work_func);
269 i2c_set_clientdata(client, ts);
270 pdata = client->dev.platform_data;
272 ts->power = pdata->power;
276 printk(KERN_ERR "synaptics_ts_probe power on failed\n");
277 goto err_power_failed;
281 ret = i2c_smbus_write_byte_data(ts->client, 0xf4, 0x01); /* device command = reset */
283 printk(KERN_ERR "i2c_smbus_write_byte_data failed\n");
288 while (retry-- > 0) {
289 ret = i2c_smbus_read_byte_data(ts->client, 0xe4);
296 printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
297 goto err_detect_failed;
299 printk(KERN_INFO "synaptics_ts_probe: Product Major Version %x\n", ret);
300 panel_version = ret << 8;
301 ret = i2c_smbus_read_byte_data(ts->client, 0xe5);
303 printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
304 goto err_detect_failed;
306 printk(KERN_INFO "synaptics_ts_probe: Product Minor Version %x\n", ret);
307 panel_version |= ret;
309 ret = i2c_smbus_read_byte_data(ts->client, 0xe3);
311 printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
312 goto err_detect_failed;
314 printk(KERN_INFO "synaptics_ts_probe: product property %x\n", ret);
317 while (pdata->version > panel_version)
319 ts->flags = pdata->flags;
320 inactive_area_left = pdata->inactive_left;
321 inactive_area_right = pdata->inactive_right;
322 inactive_area_top = pdata->inactive_top;
323 inactive_area_bottom = pdata->inactive_bottom;
324 snap_left_on = pdata->snap_left_on;
325 snap_left_off = pdata->snap_left_off;
326 snap_right_on = pdata->snap_right_on;
327 snap_right_off = pdata->snap_right_off;
328 snap_top_on = pdata->snap_top_on;
329 snap_top_off = pdata->snap_top_off;
330 snap_bottom_on = pdata->snap_bottom_on;
331 snap_bottom_off = pdata->snap_bottom_off;
332 fuzz_x = pdata->fuzz_x;
333 fuzz_y = pdata->fuzz_y;
334 fuzz_p = pdata->fuzz_p;
335 fuzz_w = pdata->fuzz_w;
337 inactive_area_left = 0;
338 inactive_area_right = 0;
339 inactive_area_top = 0;
340 inactive_area_bottom = 0;
355 ret = i2c_smbus_read_byte_data(ts->client, 0xf0);
357 printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
358 goto err_detect_failed;
360 printk(KERN_INFO "synaptics_ts_probe: device control %x\n", ret);
362 ret = i2c_smbus_read_byte_data(ts->client, 0xf1);
364 printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
365 goto err_detect_failed;
367 printk(KERN_INFO "synaptics_ts_probe: interrupt enable %x\n", ret);
369 ret = i2c_smbus_write_byte_data(ts->client, 0xf1, 0); /* disable interrupt */
371 printk(KERN_ERR "i2c_smbus_write_byte_data failed\n");
372 goto err_detect_failed;
375 msg[0].addr = ts->client->addr;
380 msg[1].addr = ts->client->addr;
381 msg[1].flags = I2C_M_RD;
384 ret = i2c_transfer(ts->client->adapter, msg, 2);
386 printk(KERN_ERR "i2c_transfer failed\n");
387 goto err_detect_failed;
389 printk(KERN_INFO "synaptics_ts_probe: 0xe0: %x %x %x %x %x %x %x %x\n",
390 buf1[0], buf1[1], buf1[2], buf1[3],
391 buf1[4], buf1[5], buf1[6], buf1[7]);
393 ret = i2c_smbus_write_byte_data(ts->client, 0xff, 0x10); /* page select = 0x10 */
395 printk(KERN_ERR "i2c_smbus_write_byte_data failed for page select\n");
396 goto err_detect_failed;
398 ret = i2c_smbus_read_word_data(ts->client, 0x02);
400 printk(KERN_ERR "i2c_smbus_read_word_data failed\n");
401 goto err_detect_failed;
403 ts->has_relative_report = !(ret & 0x100);
404 printk(KERN_INFO "synaptics_ts_probe: Sensor properties %x\n", ret);
405 ret = i2c_smbus_read_word_data(ts->client, 0x04);
407 printk(KERN_ERR "i2c_smbus_read_word_data failed\n");
408 goto err_detect_failed;
410 ts->max[0] = max_x = (ret >> 8 & 0xff) | ((ret & 0x1f) << 8);
411 ret = i2c_smbus_read_word_data(ts->client, 0x06);
413 printk(KERN_ERR "i2c_smbus_read_word_data failed\n");
414 goto err_detect_failed;
416 ts->max[1] = max_y = (ret >> 8 & 0xff) | ((ret & 0x1f) << 8);
417 if (ts->flags & SYNAPTICS_SWAP_XY)
420 ret = synaptics_init_panel(ts); /* will also switch back to page 0x04 */
422 printk(KERN_ERR "synaptics_init_panel failed\n");
423 goto err_detect_failed;
426 ts->input_dev = input_allocate_device();
427 if (ts->input_dev == NULL) {
429 printk(KERN_ERR "synaptics_ts_probe: Failed to allocate input device\n");
430 goto err_input_dev_alloc_failed;
432 ts->input_dev->name = "synaptics-rmi-touchscreen";
433 set_bit(EV_SYN, ts->input_dev->evbit);
434 set_bit(EV_KEY, ts->input_dev->evbit);
435 set_bit(BTN_TOUCH, ts->input_dev->keybit);
436 set_bit(BTN_2, ts->input_dev->keybit);
437 set_bit(EV_ABS, ts->input_dev->evbit);
438 inactive_area_left = inactive_area_left * max_x / 0x10000;
439 inactive_area_right = inactive_area_right * max_x / 0x10000;
440 inactive_area_top = inactive_area_top * max_y / 0x10000;
441 inactive_area_bottom = inactive_area_bottom * max_y / 0x10000;
442 snap_left_on = snap_left_on * max_x / 0x10000;
443 snap_left_off = snap_left_off * max_x / 0x10000;
444 snap_right_on = snap_right_on * max_x / 0x10000;
445 snap_right_off = snap_right_off * max_x / 0x10000;
446 snap_top_on = snap_top_on * max_y / 0x10000;
447 snap_top_off = snap_top_off * max_y / 0x10000;
448 snap_bottom_on = snap_bottom_on * max_y / 0x10000;
449 snap_bottom_off = snap_bottom_off * max_y / 0x10000;
450 fuzz_x = fuzz_x * max_x / 0x10000;
451 fuzz_y = fuzz_y * max_y / 0x10000;
452 ts->snap_down[!!(ts->flags & SYNAPTICS_SWAP_XY)] = -inactive_area_left;
453 ts->snap_up[!!(ts->flags & SYNAPTICS_SWAP_XY)] = max_x + inactive_area_right;
454 ts->snap_down[!(ts->flags & SYNAPTICS_SWAP_XY)] = -inactive_area_top;
455 ts->snap_up[!(ts->flags & SYNAPTICS_SWAP_XY)] = max_y + inactive_area_bottom;
456 ts->snap_down_on[!!(ts->flags & SYNAPTICS_SWAP_XY)] = snap_left_on;
457 ts->snap_down_off[!!(ts->flags & SYNAPTICS_SWAP_XY)] = snap_left_off;
458 ts->snap_up_on[!!(ts->flags & SYNAPTICS_SWAP_XY)] = max_x - snap_right_on;
459 ts->snap_up_off[!!(ts->flags & SYNAPTICS_SWAP_XY)] = max_x - snap_right_off;
460 ts->snap_down_on[!(ts->flags & SYNAPTICS_SWAP_XY)] = snap_top_on;
461 ts->snap_down_off[!(ts->flags & SYNAPTICS_SWAP_XY)] = snap_top_off;
462 ts->snap_up_on[!(ts->flags & SYNAPTICS_SWAP_XY)] = max_y - snap_bottom_on;
463 ts->snap_up_off[!(ts->flags & SYNAPTICS_SWAP_XY)] = max_y - snap_bottom_off;
464 printk(KERN_INFO "synaptics_ts_probe: max_x %d, max_y %d\n", max_x, max_y);
465 printk(KERN_INFO "synaptics_ts_probe: inactive_x %d %d, inactive_y %d %d\n",
466 inactive_area_left, inactive_area_right,
467 inactive_area_top, inactive_area_bottom);
468 printk(KERN_INFO "synaptics_ts_probe: snap_x %d-%d %d-%d, snap_y %d-%d %d-%d\n",
469 snap_left_on, snap_left_off, snap_right_on, snap_right_off,
470 snap_top_on, snap_top_off, snap_bottom_on, snap_bottom_off);
471 input_set_abs_params(ts->input_dev, ABS_X, -inactive_area_left, max_x + inactive_area_right, fuzz_x, 0);
472 input_set_abs_params(ts->input_dev, ABS_Y, -inactive_area_top, max_y + inactive_area_bottom, fuzz_y, 0);
473 input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 255, fuzz_p, 0);
474 input_set_abs_params(ts->input_dev, ABS_TOOL_WIDTH, 0, 15, fuzz_w, 0);
475 input_set_abs_params(ts->input_dev, ABS_HAT0X, -inactive_area_left, max_x + inactive_area_right, fuzz_x, 0);
476 input_set_abs_params(ts->input_dev, ABS_HAT0Y, -inactive_area_top, max_y + inactive_area_bottom, fuzz_y, 0);
477 /* ts->input_dev->name = ts->keypad_info->name; */
478 ret = input_register_device(ts->input_dev);
480 printk(KERN_ERR "synaptics_ts_probe: Unable to register %s input device\n", ts->input_dev->name);
481 goto err_input_register_device_failed;
484 ret = request_irq(client->irq, synaptics_ts_irq_handler, 0, client->name, ts);
486 ret = i2c_smbus_write_byte_data(ts->client, 0xf1, 0x01); /* enable abs int */
488 free_irq(client->irq, ts);
493 dev_err(&client->dev, "request_irq failed\n");
496 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
497 ts->timer.function = synaptics_ts_timer_func;
498 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
500 #ifdef CONFIG_HAS_EARLYSUSPEND
501 ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
502 ts->early_suspend.suspend = synaptics_ts_early_suspend;
503 ts->early_suspend.resume = synaptics_ts_late_resume;
504 register_early_suspend(&ts->early_suspend);
507 printk(KERN_INFO "synaptics_ts_probe: Start touchscreen %s in %s mode\n", ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
511 err_input_register_device_failed:
512 input_free_device(ts->input_dev);
514 err_input_dev_alloc_failed:
518 err_alloc_data_failed:
519 err_check_functionality_failed:
523 static int synaptics_ts_remove(struct i2c_client *client)
525 struct synaptics_ts_data *ts = i2c_get_clientdata(client);
526 unregister_early_suspend(&ts->early_suspend);
528 free_irq(client->irq, ts);
530 hrtimer_cancel(&ts->timer);
531 input_unregister_device(ts->input_dev);
536 static int synaptics_ts_suspend(struct i2c_client *client, pm_message_t mesg)
539 struct synaptics_ts_data *ts = i2c_get_clientdata(client);
542 disable_irq(client->irq);
544 hrtimer_cancel(&ts->timer);
545 ret = cancel_work_sync(&ts->work);
546 if (ret && ts->use_irq) /* if work was pending disable-count is now 2 */
547 enable_irq(client->irq);
548 ret = i2c_smbus_write_byte_data(ts->client, 0xf1, 0); /* disable interrupt */
550 printk(KERN_ERR "synaptics_ts_suspend: i2c_smbus_write_byte_data failed\n");
552 ret = i2c_smbus_write_byte_data(client, 0xf0, 0x86); /* deep sleep */
554 printk(KERN_ERR "synaptics_ts_suspend: i2c_smbus_write_byte_data failed\n");
558 printk(KERN_ERR "synaptics_ts_resume power off failed\n");
563 static int synaptics_ts_resume(struct i2c_client *client)
566 struct synaptics_ts_data *ts = i2c_get_clientdata(client);
571 printk(KERN_ERR "synaptics_ts_resume power on failed\n");
574 synaptics_init_panel(ts);
577 enable_irq(client->irq);
580 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
582 i2c_smbus_write_byte_data(ts->client, 0xf1, 0x01); /* enable abs int */
587 #ifdef CONFIG_HAS_EARLYSUSPEND
588 static void synaptics_ts_early_suspend(struct early_suspend *h)
590 struct synaptics_ts_data *ts;
591 ts = container_of(h, struct synaptics_ts_data, early_suspend);
592 synaptics_ts_suspend(ts->client, PMSG_SUSPEND);
595 static void synaptics_ts_late_resume(struct early_suspend *h)
597 struct synaptics_ts_data *ts;
598 ts = container_of(h, struct synaptics_ts_data, early_suspend);
599 synaptics_ts_resume(ts->client);
603 static const struct i2c_device_id synaptics_ts_id[] = {
604 { SYNAPTICS_I2C_RMI_NAME, 0 },
608 static struct i2c_driver synaptics_ts_driver = {
609 .probe = synaptics_ts_probe,
610 .remove = synaptics_ts_remove,
611 #ifndef CONFIG_HAS_EARLYSUSPEND
612 .suspend = synaptics_ts_suspend,
613 .resume = synaptics_ts_resume,
615 .id_table = synaptics_ts_id,
617 .name = SYNAPTICS_I2C_RMI_NAME,
621 static int __devinit synaptics_ts_init(void)
623 synaptics_wq = create_singlethread_workqueue("synaptics_wq");
626 return i2c_add_driver(&synaptics_ts_driver);
629 static void __exit synaptics_ts_exit(void)
631 i2c_del_driver(&synaptics_ts_driver);
633 destroy_workqueue(synaptics_wq);
636 module_init(synaptics_ts_init);
637 module_exit(synaptics_ts_exit);
639 MODULE_DESCRIPTION("Synaptics Touchscreen Driver");
640 MODULE_LICENSE("GPL");