Input: synaptics_i2c_rmi: Add support for panels with has-no-relative-data property.
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / synaptics_i2c_rmi.c
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 (*power)(int on);
48         struct early_suspend early_suspend;
49 };
50
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);
54 #endif
55
56 static int synaptics_init_panel(struct synaptics_ts_data *ts)
57 {
58         int ret;
59
60         ret = i2c_smbus_write_byte_data(ts->client, 0xff, 0x10); /* page select = 0x10 */
61         if (ret < 0) {
62                 printk(KERN_ERR "i2c_smbus_write_byte_data failed for page select\n");
63                 goto err_page_select_failed;
64         }
65         ret = i2c_smbus_write_byte_data(ts->client, 0x41, 0x04); /* Set "No Clip Z" */
66         if (ret < 0)
67                 printk(KERN_ERR "i2c_smbus_write_byte_data failed for No Clip Z\n");
68
69 err_page_select_failed:
70         ret = i2c_smbus_write_byte_data(ts->client, 0xff, 0x04); /* page select = 0x04 */
71         if (ret < 0)
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 */
74         if (ret < 0)
75                 printk(KERN_ERR "synaptics_ts_resume: i2c_smbus_write_byte_data failed\n");
76         return ret;
77 }
78
79 static void synaptics_ts_work_func(struct work_struct *work)
80 {
81         int i;
82         int ret;
83         int bad_data = 0;
84         struct i2c_msg msg[2];
85         uint8_t start_reg;
86         uint8_t buf[15];
87         struct synaptics_ts_data *ts = container_of(work, struct synaptics_ts_data, work);
88         int buf_len = ts->has_relative_report ? 15 : 13;
89
90         msg[0].addr = ts->client->addr;
91         msg[0].flags = 0;
92         msg[0].len = 1;
93         msg[0].buf = &start_reg;
94         start_reg = 0x00;
95         msg[1].addr = ts->client->addr;
96         msg[1].flags = I2C_M_RD;
97         msg[1].len = buf_len;
98         msg[1].buf = buf;
99
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);
103                 if (ret < 0) {
104                         printk(KERN_ERR "synaptics_ts_work_func: i2c_transfer failed\n");
105                         bad_data = 1;
106                 } else {
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);
121                                 if (bad_data)
122                                         synaptics_init_panel(ts);
123                                 bad_data = 1;
124                                 continue;
125                         }
126                         bad_data = 0;
127                         if ((buf[buf_len - 1] & 1) == 0) {
128                                 /* printk("read %d coordinates\n", i); */
129                                 break;
130                         } else {
131                                 int pos[2][2];
132                                 int f, a;
133                                 int base;
134                                 /* int x = buf[3] | (uint16_t)(buf[2] & 0x1f) << 8; */
135                                 /* int y = buf[5] | (uint16_t)(buf[4] & 0x1f) << 8; */
136                                 int z = buf[1];
137                                 int w = buf[0] >> 4;
138                                 int finger = buf[0] & 7;
139
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; */
145
146                                 /* int dx = (int8_t)buf[12]; */
147                                 /* int dy = (int8_t)buf[13]; */
148                                 int finger2_pressed;
149
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, */
153                                 /*      dx, dy); */
154
155                                 base = 2;
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)
162                                                         p = ts->max[a] - p;
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])
168                                                                         p = ts->snap_up[a];
169                                                                 else
170                                                                         ts->snap_state[f][a] = 0;
171                                                         } else {
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]) {
176                                                                         p = ts->snap_up[a];
177                                                                         ts->snap_state[f][a] = 1;
178                                                                 }
179                                                         }
180                                                 }
181                                                 pos[f][a] = p;
182                                                 base += 2;
183                                                 flip_flag <<= 1;
184                                         }
185                                         base += 2;
186                                         if (ts->flags & SYNAPTICS_SWAP_XY)
187                                                 swap(pos[f][0], pos[f][1]);
188                                 }
189                                 if (z) {
190                                         input_report_abs(ts->input_dev, ABS_X, pos[0][0]);
191                                         input_report_abs(ts->input_dev, ABS_Y, pos[0][1]);
192                                 }
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]);
201                                 }
202                                 input_sync(ts->input_dev);
203                         }
204                 }
205         }
206         if (ts->use_irq)
207                 enable_irq(ts->client->irq);
208 }
209
210 static enum hrtimer_restart synaptics_ts_timer_func(struct hrtimer *timer)
211 {
212         struct synaptics_ts_data *ts = container_of(timer, struct synaptics_ts_data, timer);
213         /* printk("synaptics_ts_timer_func\n"); */
214
215         queue_work(synaptics_wq, &ts->work);
216
217         hrtimer_start(&ts->timer, ktime_set(0, 12500000), HRTIMER_MODE_REL);
218         return HRTIMER_NORESTART;
219 }
220
221 static irqreturn_t synaptics_ts_irq_handler(int irq, void *dev_id)
222 {
223         struct synaptics_ts_data *ts = dev_id;
224
225         /* printk("synaptics_ts_irq_handler\n"); */
226         disable_irq_nosync(ts->client->irq);
227         queue_work(synaptics_wq, &ts->work);
228         return IRQ_HANDLED;
229 }
230
231 static int synaptics_ts_probe(
232         struct i2c_client *client, const struct i2c_device_id *id)
233 {
234         struct synaptics_ts_data *ts;
235         uint8_t buf0[4];
236         uint8_t buf1[8];
237         struct i2c_msg msg[2];
238         int ret = 0;
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;
246         int snap_left_on;
247         int snap_left_off;
248         int snap_right_on;
249         int snap_right_off;
250         int snap_top_on;
251         int snap_top_off;
252         int snap_bottom_on;
253         int snap_bottom_off;
254         uint32_t panel_version;
255
256         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
257                 printk(KERN_ERR "synaptics_ts_probe: need I2C_FUNC_I2C\n");
258                 ret = -ENODEV;
259                 goto err_check_functionality_failed;
260         }
261
262         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
263         if (ts == NULL) {
264                 ret = -ENOMEM;
265                 goto err_alloc_data_failed;
266         }
267         INIT_WORK(&ts->work, synaptics_ts_work_func);
268         ts->client = client;
269         i2c_set_clientdata(client, ts);
270         pdata = client->dev.platform_data;
271         if (pdata)
272                 ts->power = pdata->power;
273         if (ts->power) {
274                 ret = ts->power(1);
275                 if (ret < 0) {
276                         printk(KERN_ERR "synaptics_ts_probe power on failed\n");
277                         goto err_power_failed;
278                 }
279         }
280
281         ret = i2c_smbus_write_byte_data(ts->client, 0xf4, 0x01); /* device command = reset */
282         if (ret < 0) {
283                 printk(KERN_ERR "i2c_smbus_write_byte_data failed\n");
284                 /* fail? */
285         }
286         {
287                 int retry = 10;
288                 while (retry-- > 0) {
289                         ret = i2c_smbus_read_byte_data(ts->client, 0xe4);
290                         if (ret >= 0)
291                                 break;
292                         msleep(100);
293                 }
294         }
295         if (ret < 0) {
296                 printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
297                 goto err_detect_failed;
298         }
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);
302         if (ret < 0) {
303                 printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
304                 goto err_detect_failed;
305         }
306         printk(KERN_INFO "synaptics_ts_probe: Product Minor Version %x\n", ret);
307         panel_version |= ret;
308
309         ret = i2c_smbus_read_byte_data(ts->client, 0xe3);
310         if (ret < 0) {
311                 printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
312                 goto err_detect_failed;
313         }
314         printk(KERN_INFO "synaptics_ts_probe: product property %x\n", ret);
315
316         if (pdata) {
317                 while (pdata->version > panel_version)
318                         pdata++;
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;
336         } else {
337                 inactive_area_left = 0;
338                 inactive_area_right = 0;
339                 inactive_area_top = 0;
340                 inactive_area_bottom = 0;
341                 snap_left_on = 0;
342                 snap_left_off = 0;
343                 snap_right_on = 0;
344                 snap_right_off = 0;
345                 snap_top_on = 0;
346                 snap_top_off = 0;
347                 snap_bottom_on = 0;
348                 snap_bottom_off = 0;
349                 fuzz_x = 0;
350                 fuzz_y = 0;
351                 fuzz_p = 0;
352                 fuzz_w = 0;
353         }
354
355         ret = i2c_smbus_read_byte_data(ts->client, 0xf0);
356         if (ret < 0) {
357                 printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
358                 goto err_detect_failed;
359         }
360         printk(KERN_INFO "synaptics_ts_probe: device control %x\n", ret);
361
362         ret = i2c_smbus_read_byte_data(ts->client, 0xf1);
363         if (ret < 0) {
364                 printk(KERN_ERR "i2c_smbus_read_byte_data failed\n");
365                 goto err_detect_failed;
366         }
367         printk(KERN_INFO "synaptics_ts_probe: interrupt enable %x\n", ret);
368
369         ret = i2c_smbus_write_byte_data(ts->client, 0xf1, 0); /* disable interrupt */
370         if (ret < 0) {
371                 printk(KERN_ERR "i2c_smbus_write_byte_data failed\n");
372                 goto err_detect_failed;
373         }
374
375         msg[0].addr = ts->client->addr;
376         msg[0].flags = 0;
377         msg[0].len = 1;
378         msg[0].buf = buf0;
379         buf0[0] = 0xe0;
380         msg[1].addr = ts->client->addr;
381         msg[1].flags = I2C_M_RD;
382         msg[1].len = 8;
383         msg[1].buf = buf1;
384         ret = i2c_transfer(ts->client->adapter, msg, 2);
385         if (ret < 0) {
386                 printk(KERN_ERR "i2c_transfer failed\n");
387                 goto err_detect_failed;
388         }
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]);
392
393         ret = i2c_smbus_write_byte_data(ts->client, 0xff, 0x10); /* page select = 0x10 */
394         if (ret < 0) {
395                 printk(KERN_ERR "i2c_smbus_write_byte_data failed for page select\n");
396                 goto err_detect_failed;
397         }
398         ret = i2c_smbus_read_word_data(ts->client, 0x02);
399         if (ret < 0) {
400                 printk(KERN_ERR "i2c_smbus_read_word_data failed\n");
401                 goto err_detect_failed;
402         }
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);
406         if (ret < 0) {
407                 printk(KERN_ERR "i2c_smbus_read_word_data failed\n");
408                 goto err_detect_failed;
409         }
410         ts->max[0] = max_x = (ret >> 8 & 0xff) | ((ret & 0x1f) << 8);
411         ret = i2c_smbus_read_word_data(ts->client, 0x06);
412         if (ret < 0) {
413                 printk(KERN_ERR "i2c_smbus_read_word_data failed\n");
414                 goto err_detect_failed;
415         }
416         ts->max[1] = max_y = (ret >> 8 & 0xff) | ((ret & 0x1f) << 8);
417         if (ts->flags & SYNAPTICS_SWAP_XY)
418                 swap(max_x, max_y);
419
420         ret = synaptics_init_panel(ts); /* will also switch back to page 0x04 */
421         if (ret < 0) {
422                 printk(KERN_ERR "synaptics_init_panel failed\n");
423                 goto err_detect_failed;
424         }
425
426         ts->input_dev = input_allocate_device();
427         if (ts->input_dev == NULL) {
428                 ret = -ENOMEM;
429                 printk(KERN_ERR "synaptics_ts_probe: Failed to allocate input device\n");
430                 goto err_input_dev_alloc_failed;
431         }
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);
479         if (ret) {
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;
482         }
483         if (client->irq) {
484                 ret = request_irq(client->irq, synaptics_ts_irq_handler, 0, client->name, ts);
485                 if (ret == 0) {
486                         ret = i2c_smbus_write_byte_data(ts->client, 0xf1, 0x01); /* enable abs int */
487                         if (ret)
488                                 free_irq(client->irq, ts);
489                 }
490                 if (ret == 0)
491                         ts->use_irq = 1;
492                 else
493                         dev_err(&client->dev, "request_irq failed\n");
494         }
495         if (!ts->use_irq) {
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);
499         }
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);
505 #endif
506
507         printk(KERN_INFO "synaptics_ts_probe: Start touchscreen %s in %s mode\n", ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
508
509         return 0;
510
511 err_input_register_device_failed:
512         input_free_device(ts->input_dev);
513
514 err_input_dev_alloc_failed:
515 err_detect_failed:
516 err_power_failed:
517         kfree(ts);
518 err_alloc_data_failed:
519 err_check_functionality_failed:
520         return ret;
521 }
522
523 static int synaptics_ts_remove(struct i2c_client *client)
524 {
525         struct synaptics_ts_data *ts = i2c_get_clientdata(client);
526         unregister_early_suspend(&ts->early_suspend);
527         if (ts->use_irq)
528                 free_irq(client->irq, ts);
529         else
530                 hrtimer_cancel(&ts->timer);
531         input_unregister_device(ts->input_dev);
532         kfree(ts);
533         return 0;
534 }
535
536 static int synaptics_ts_suspend(struct i2c_client *client, pm_message_t mesg)
537 {
538         int ret;
539         struct synaptics_ts_data *ts = i2c_get_clientdata(client);
540
541         if (ts->use_irq)
542                 disable_irq(client->irq);
543         else
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 */
549         if (ret < 0)
550                 printk(KERN_ERR "synaptics_ts_suspend: i2c_smbus_write_byte_data failed\n");
551
552         ret = i2c_smbus_write_byte_data(client, 0xf0, 0x86); /* deep sleep */
553         if (ret < 0)
554                 printk(KERN_ERR "synaptics_ts_suspend: i2c_smbus_write_byte_data failed\n");
555         if (ts->power) {
556                 ret = ts->power(0);
557                 if (ret < 0)
558                         printk(KERN_ERR "synaptics_ts_resume power off failed\n");
559         }
560         return 0;
561 }
562
563 static int synaptics_ts_resume(struct i2c_client *client)
564 {
565         int ret;
566         struct synaptics_ts_data *ts = i2c_get_clientdata(client);
567
568         if (ts->power) {
569                 ret = ts->power(1);
570                 if (ret < 0)
571                         printk(KERN_ERR "synaptics_ts_resume power on failed\n");
572         }
573
574         synaptics_init_panel(ts);
575
576         if (ts->use_irq)
577                 enable_irq(client->irq);
578
579         if (!ts->use_irq)
580                 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
581         else
582                 i2c_smbus_write_byte_data(ts->client, 0xf1, 0x01); /* enable abs int */
583
584         return 0;
585 }
586
587 #ifdef CONFIG_HAS_EARLYSUSPEND
588 static void synaptics_ts_early_suspend(struct early_suspend *h)
589 {
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);
593 }
594
595 static void synaptics_ts_late_resume(struct early_suspend *h)
596 {
597         struct synaptics_ts_data *ts;
598         ts = container_of(h, struct synaptics_ts_data, early_suspend);
599         synaptics_ts_resume(ts->client);
600 }
601 #endif
602
603 static const struct i2c_device_id synaptics_ts_id[] = {
604         { SYNAPTICS_I2C_RMI_NAME, 0 },
605         { }
606 };
607
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,
614 #endif
615         .id_table       = synaptics_ts_id,
616         .driver = {
617                 .name   = SYNAPTICS_I2C_RMI_NAME,
618         },
619 };
620
621 static int __devinit synaptics_ts_init(void)
622 {
623         synaptics_wq = create_singlethread_workqueue("synaptics_wq");
624         if (!synaptics_wq)
625                 return -ENOMEM;
626         return i2c_add_driver(&synaptics_ts_driver);
627 }
628
629 static void __exit synaptics_ts_exit(void)
630 {
631         i2c_del_driver(&synaptics_ts_driver);
632         if (synaptics_wq)
633                 destroy_workqueue(synaptics_wq);
634 }
635
636 module_init(synaptics_ts_init);
637 module_exit(synaptics_ts_exit);
638
639 MODULE_DESCRIPTION("Synaptics Touchscreen Driver");
640 MODULE_LICENSE("GPL");