temp revert rk change
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / qtouch_obp_ts.c
1 /*
2  * drivers/input/touchscreen/qtouch_obp_ts.c - driver for Quantum touch IC
3  *
4  * Copyright (C) 2009 Google, Inc.
5  * Copyright (C) 2009-2010 Motorola, Inc.
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * Derived from the Motorola OBP touch driver.
17  *
18  */
19 #include <linux/delay.h>
20 #include <linux/device.h>
21 #include <linux/earlysuspend.h>
22 #include <linux/gpio.h>
23 #include <linux/i2c.h>
24 #include <linux/input.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/kernel.h>
28 #include <linux/platform_device.h>
29 #include <linux/qtouch_obp_ts.h>
30 #include <linux/slab.h>
31 #include <linux/firmware.h>
32 #include <linux/regulator/consumer.h>
33
34 #define IGNORE_CHECKSUM_MISMATCH
35
36 struct qtm_object {
37         struct qtm_obj_entry            entry;
38         uint8_t                         report_id_min;
39         uint8_t                         report_id_max;
40 };
41
42 struct axis_map {
43         int     key;
44         int     x;
45         int     y;
46 };
47
48 struct coordinate_map {
49         int x_data;
50         int y_data;
51         int z_data;
52         int w_data;
53         int vector;
54         int down;
55 };
56
57 #define _BITMAP_LEN                     BITS_TO_LONGS(QTM_OBP_MAX_OBJECT_NUM)
58 #define _NUM_FINGERS                    10
59 struct qtouch_ts_data {
60         struct i2c_client               *client;
61         struct input_dev                *input_dev;
62         struct work_struct              init_work;
63         struct work_struct              work;
64         struct work_struct              boot_work;
65         struct qtouch_ts_platform_data  *pdata;
66         struct coordinate_map           finger_data[_NUM_FINGERS];
67         struct early_suspend            early_suspend;
68
69         struct qtm_object               obj_tbl[QTM_OBP_MAX_OBJECT_NUM];
70         unsigned long                   obj_map[_BITMAP_LEN];
71
72         uint32_t                        last_keystate;
73         uint32_t                        eeprom_checksum;
74         uint8_t                         checksum_cnt;
75         int                             x_delta;
76         int                             y_delta;
77         uint8_t                         family_id;
78         uint8_t                         variant_id;
79         uint8_t                         fw_version;
80         uint8_t                         build_version;
81         uint8_t                         fw_error_count;
82         uint32_t                        touch_fw_size;
83         uint8_t                         *touch_fw_image;
84         uint8_t                         base_fw_version;
85         uint8_t                         *touch_fw;
86
87         uint8_t                         xpos_rshift_lsb;
88         uint8_t                         ypos_rshift_lsb;
89         uint8_t                         xpos_lshift_msb;
90         uint8_t                         ypos_lshift_msb;
91
92         atomic_t                        irq_enabled;
93         atomic_t                        process_open;
94         int                             enable_irq_flag;
95         int                             status;
96
97         uint8_t                         mode;
98         int                             boot_pkt_size;
99         int                             current_pkt_sz;
100         uint8_t                         org_i2c_addr;
101
102         /* Note: The message buffer is reused for reading different messages.
103          * MUST enforce that there is no concurrent access to msg_buf. */
104         uint8_t                         *msg_buf;
105         int                             msg_size;
106
107         struct regulator                *regulator;
108 };
109
110 #ifdef CONFIG_HAS_EARLYSUSPEND
111 static void qtouch_ts_early_suspend(struct early_suspend *handler);
112 static void qtouch_ts_late_resume(struct early_suspend *handler);
113 #endif
114
115 static struct workqueue_struct *qtouch_ts_wq;
116 const struct firmware *fw_entry;
117
118 static uint32_t qtouch_tsdebug;
119 module_param_named(tsdebug, qtouch_tsdebug, uint, 0664);
120
121 static uint32_t qtouch_disable_touch;
122 module_param_named(disable_touch, qtouch_disable_touch, uint, 0644);
123
124 static irqreturn_t qtouch_ts_irq_handler(int irq, void *dev_id)
125 {
126         struct qtouch_ts_data *ts = dev_id;
127
128         disable_irq_nosync(ts->client->irq);
129         if (ts->mode == 1)
130                 queue_work(qtouch_ts_wq, &ts->boot_work);
131         else
132                 queue_work(qtouch_ts_wq, &ts->work);
133
134         return IRQ_HANDLED;
135 }
136
137 static int qtouch_write(struct qtouch_ts_data *ts, void *buf, int buf_sz)
138 {
139         int retries = 10;
140         int ret;
141
142         do {
143                 ret = i2c_master_send(ts->client, (char *)buf, buf_sz);
144         } while ((ret < buf_sz) && (--retries > 0));
145
146         if (ret < 0)
147                 pr_info("%s: Error while trying to write %d bytes\n", __func__,
148                         buf_sz);
149         else if (ret != buf_sz) {
150                 pr_info("%s: Write %d bytes, expected %d\n", __func__,
151                         ret, buf_sz);
152                 ret = -EIO;
153         }
154         return ret;
155 }
156
157 static int qtouch_set_addr(struct qtouch_ts_data *ts, uint16_t addr)
158 {
159         int ret;
160
161         /* Note: addr on the wire is LSB first */
162         ret = qtouch_write(ts, (char *)&addr, sizeof(uint16_t));
163         if (ret < 0)
164                 pr_info("%s: Can't send obp addr 0x%4x\n", __func__, addr);
165
166         return ret >= 0 ? 0 : ret;
167 }
168
169 static int qtouch_read(struct qtouch_ts_data *ts, void *buf, int buf_sz)
170 {
171         int retries = 10;
172         int ret;
173
174         do {
175                 memset(buf, 0, buf_sz);
176                 ret = i2c_master_recv(ts->client, (char *)buf, buf_sz);
177         } while ((ret < 0) && (--retries > 0));
178
179         if (ret < 0)
180                 pr_info("%s: Error while trying to read %d bytes\n", __func__,
181                         buf_sz);
182         else if (ret != buf_sz) {
183                 pr_info("%s: Read %d bytes, expected %d\n", __func__,
184                         ret, buf_sz);
185                 ret = -EIO;
186         }
187
188         return ret >= 0 ? 0 : ret;
189 }
190
191 static int qtouch_read_addr(struct qtouch_ts_data *ts, uint16_t addr,
192                             void *buf, int buf_sz)
193 {
194         int ret;
195
196         ret = qtouch_set_addr(ts, addr);
197         if (ret != 0)
198                 return ret;
199
200         return qtouch_read(ts, buf, buf_sz);
201 }
202
203 static struct qtm_obj_message *qtouch_read_msg(struct qtouch_ts_data *ts)
204 {
205         int ret;
206
207         ret = qtouch_read(ts, ts->msg_buf, ts->msg_size);
208         if (!ret)
209                 return (struct qtm_obj_message *)ts->msg_buf;
210         return NULL;
211 }
212
213 static int qtouch_write_addr(struct qtouch_ts_data *ts, uint16_t addr,
214                              void *buf, int buf_sz)
215 {
216         int ret;
217         uint8_t *write_buf;
218
219         write_buf = kzalloc((buf_sz + sizeof(uint16_t)), GFP_KERNEL);
220         if (write_buf == NULL) {
221                 pr_err("%s: Can't allocate write buffer (%d)\n",
222                          __func__, buf_sz);
223                 return -ENOMEM;
224         }
225
226         memcpy(write_buf, (void *)&addr, sizeof(addr));
227         memcpy((void *)write_buf + sizeof(addr), buf, buf_sz);
228
229         ret = qtouch_write(ts, write_buf, buf_sz + sizeof(addr));
230
231         kfree(write_buf);
232
233         if (ret < 0) {
234                 pr_err("%s: Could not write %d bytes.\n", __func__, buf_sz);
235                 return ret;
236         }
237
238         return 0;
239 }
240 static uint32_t crc24(uint32_t crc, uint8_t first_byte, uint8_t sec_byte)
241 {
242         static const uint32_t crcpoly = 0x80001b;
243         uint32_t result = 0;
244         uint16_t data_word = 0;
245
246         data_word = (uint16_t)((uint16_t)(sec_byte << 8u) | first_byte);
247         result = ((crc<<1u) ^ (uint32_t)data_word);
248         /* If bit 25 is set, XOR result with crcpoly */
249         if (result & 0x1000000)
250                 result ^= crcpoly;
251
252         return result;
253 }
254
255 static uint32_t calc_csum(uint32_t curr_sum, void *_buf, int buf_sz)
256 {
257         uint8_t *buf = _buf;
258         int i = 0;
259         int odd = 0;
260
261         if (buf_sz % 2) {
262                 buf_sz -= 1;
263                 odd = 1;
264         }
265         while (i < buf_sz) {
266                 curr_sum = crc24(curr_sum, *(buf + i), *(buf + i + 1));
267                 i += 2;
268         }
269         if (odd)
270                 curr_sum = crc24(curr_sum, *(buf + i), 0);
271         /* Final Result */
272         curr_sum = (curr_sum & 0x00FFFFFF);
273
274         return curr_sum;
275 }
276
277 static inline struct qtm_object *find_obj(struct qtouch_ts_data *ts, int id)
278 {
279         return &ts->obj_tbl[id];
280 }
281
282 static struct qtm_object *create_obj(struct qtouch_ts_data *ts,
283                                      struct qtm_obj_entry *entry)
284 {
285         struct qtm_object *obj;
286
287         obj = &ts->obj_tbl[entry->type];
288         memcpy(&obj->entry, entry, sizeof(*entry));
289         set_bit(entry->type, ts->obj_map);
290
291         return obj;
292 }
293
294 static struct qtm_object *find_object_rid(struct qtouch_ts_data *ts, int rid)
295 {
296         int i;
297
298         for_each_set_bit(i, ts->obj_map, QTM_OBP_MAX_OBJECT_NUM) {
299                 struct qtm_object *obj = &ts->obj_tbl[i];
300
301                 if ((rid >= obj->report_id_min) && (rid <= obj->report_id_max))
302                         return obj;
303         }
304
305         return NULL;
306 }
307
308 static void qtouch_force_reset(struct qtouch_ts_data *ts, uint8_t sw_reset)
309 {
310         struct qtm_object *obj;
311         uint16_t addr;
312         uint8_t val = 1;
313         int ret;
314
315         if (ts->pdata->hw_reset && !sw_reset) {
316                 pr_info("%s: Forcing HW reset\n", __func__);
317                 ts->pdata->hw_reset();
318         } else if (sw_reset) {
319                 pr_info("%s: Forcing SW reset\n", __func__);
320                 obj = find_obj(ts, QTM_OBJ_GEN_CMD_PROC);
321                 addr =
322                     obj->entry.addr + offsetof(struct qtm_gen_cmd_proc, reset);
323                 /* Check to see if to reset into boot mode */
324                 if (sw_reset == 2)
325                         val = 0xa5;
326                 ret = qtouch_write_addr(ts, addr, &val, 1);
327                 if (ret)
328                         pr_err("%s: Unable to send the reset msg\n", __func__);
329         }
330 }
331
332 static int qtouch_force_calibration(struct qtouch_ts_data *ts)
333 {
334         struct qtm_object *obj;
335         uint16_t addr;
336         uint8_t val;
337         int ret;
338
339         pr_info("%s: Forcing calibration\n", __func__);
340
341         obj = find_obj(ts, QTM_OBJ_GEN_CMD_PROC);
342
343         addr = obj->entry.addr + offsetof(struct qtm_gen_cmd_proc, calibrate);
344         val = 1;
345         ret = qtouch_write_addr(ts, addr, &val, 1);
346         if (ret)
347                 pr_err("%s: Unable to send the calibrate message\n", __func__);
348         return ret;
349 }
350
351 #undef min
352 #define min(a, b) (((a) < (b)) ? (a) : (b))
353 static int qtouch_power_config(struct qtouch_ts_data *ts, int on)
354 {
355         struct qtm_gen_power_cfg pwr_cfg;
356         struct qtm_object *obj;
357
358         if (!on) {
359                 /* go to standby mode */
360                 pwr_cfg.idle_acq_int = 0;
361                 pwr_cfg.active_acq_int = 0;
362         } else {
363                 pwr_cfg.idle_acq_int = ts->pdata->power_cfg.idle_acq_int;
364                 pwr_cfg.active_acq_int = ts->pdata->power_cfg.active_acq_int;
365         }
366
367         pwr_cfg.active_idle_to = ts->pdata->power_cfg.active_idle_to;
368
369         obj = find_obj(ts, QTM_OBJ_GEN_PWR_CONF);
370         return qtouch_write_addr(ts, obj->entry.addr, &pwr_cfg,
371                                  min(sizeof(pwr_cfg), obj->entry.size));
372 }
373
374 /* Apply the configuration provided in the platform_data to the hardware */
375 static int qtouch_hw_init(struct qtouch_ts_data *ts)
376 {
377         struct qtm_object *obj;
378         int i;
379         int ret;
380         uint16_t adj_addr;
381
382         pr_info("%s: Doing hw init\n", __func__);
383
384         /* take the IC out of suspend */
385         qtouch_power_config(ts, 1);
386
387         /* configure the acquisition object. */
388         obj = find_obj(ts, QTM_OBJ_GEN_ACQUIRE_CONF);
389         ret = qtouch_write_addr(ts, obj->entry.addr, &ts->pdata->acquire_cfg,
390                                 min(sizeof(ts->pdata->acquire_cfg),
391                                     obj->entry.size));
392         if (ret != 0) {
393                 pr_err("%s: Can't write acquisition config\n", __func__);
394                 return ret;
395         }
396
397         /* The multitouch and keyarray objects have very similar memory
398          * layout, but are just different enough where we basically have to
399          * repeat the same code */
400
401         /* configure the multi-touch object. */
402         obj = find_obj(ts, QTM_OBJ_TOUCH_MULTI);
403         if (obj && obj->entry.num_inst > 0) {
404                 struct qtm_touch_multi_cfg cfg;
405                 memcpy(&cfg, &ts->pdata->multi_touch_cfg, sizeof(cfg));
406                 if (ts->pdata->flags & QTOUCH_USE_MULTITOUCH)
407                         cfg.ctrl |= (1 << 1) | (1 << 0); /* reporten | enable */
408                 else
409                         cfg.ctrl = 0;
410                 ret = qtouch_write_addr(ts, obj->entry.addr, &cfg,
411                                         min(sizeof(cfg), obj->entry.size));
412                 if (ret != 0) {
413                         pr_err("%s: Can't write multi-touch config\n",
414                                __func__);
415                         return ret;
416                 }
417         }
418
419         /* configure the key-array object. */
420         obj = find_obj(ts, QTM_OBJ_TOUCH_KEYARRAY);
421         if (obj && obj->entry.num_inst > 0) {
422                 struct qtm_touch_keyarray_cfg cfg;
423                 for (i = 0; i < obj->entry.num_inst; i++) {
424                         if (ts->pdata->flags & QTOUCH_USE_KEYARRAY) {
425                                 memcpy(&cfg, &ts->pdata->key_array.cfg[i],
426                                        sizeof(cfg));
427                         } else
428                                 memset(&cfg, 0, sizeof(cfg));
429
430                         adj_addr = obj->entry.addr +
431                                 ((obj->entry.size + 1) * i);
432                         ret = qtouch_write_addr(ts, adj_addr, &cfg,
433                                                 min(sizeof(cfg),
434                                                     obj->entry.size));
435                         if (ret != 0) {
436                                 pr_err("%s: Can't write keyarray config\n",
437                                            __func__);
438                                 return ret;
439                         }
440                 }
441         }
442
443         /* configure the signal filter */
444         obj = find_obj(ts, QTM_OBJ_PROCG_SIG_FILTER);
445         if (obj && obj->entry.num_inst > 0) {
446                 ret = qtouch_write_addr(ts, obj->entry.addr,
447                                         &ts->pdata->sig_filter_cfg,
448                                         min(sizeof(ts->pdata->sig_filter_cfg),
449                                             obj->entry.size));
450                 if (ret != 0) {
451                         pr_err("%s: Can't write signal filter config\n",
452                                __func__);
453                         return ret;
454                 }
455         }
456
457         /* configure the linearization table */
458         obj = find_obj(ts, QTM_OBJ_PROCI_LINEAR_TBL);
459         if (obj && obj->entry.num_inst > 0) {
460                 ret = qtouch_write_addr(ts, obj->entry.addr,
461                                         &ts->pdata->linear_tbl_cfg,
462                                         min(sizeof(ts->pdata->linear_tbl_cfg),
463                                             obj->entry.size));
464                 if (ret != 0) {
465                         pr_err("%s: Can't write linear table config\n",
466                                __func__);
467                         return ret;
468                 }
469         }
470
471         /* configure the comms configuration */
472         obj = find_obj(ts, QTM_OBJ_SPT_COM_CONFIG);
473         if (obj && obj->entry.num_inst > 0) {
474                 ret = qtouch_write_addr(ts, obj->entry.addr,
475                                         &ts->pdata->comms_config_cfg,
476                                         min(sizeof(ts->pdata->comms_config_cfg),
477                                             obj->entry.size));
478                 if (ret != 0) {
479                         pr_err("%s: Can't write the comms configuration config\n",
480                                __func__);
481                         return ret;
482                 }
483         }
484
485         /* configure the GPIO PWM support */
486         obj = find_obj(ts, QTM_OBJ_SPT_GPIO_PWM);
487         if (obj && obj->entry.num_inst > 0) {
488                 ret = qtouch_write_addr(ts, obj->entry.addr,
489                                         &ts->pdata->gpio_pwm_cfg,
490                                         min(sizeof(ts->pdata->gpio_pwm_cfg),
491                                             obj->entry.size));
492                 if (ret != 0) {
493                         pr_err("%s: Can't write the GPIO PWM config\n",
494                                __func__);
495                         return ret;
496                 }
497         }
498
499         /* configure the grip face suppression table */
500         obj = find_obj(ts, QTM_OBJ_PROCI_GRIPFACESUPPRESSION);
501         if (obj && obj->entry.num_inst > 0) {
502                 ret = qtouch_write_addr(ts, obj->entry.addr,
503                                         &ts->pdata->grip_face_suppression_cfg,
504                                         min(sizeof
505                                             (ts->pdata->grip_face_suppression_cfg),
506                                             obj->entry.size));
507                 if (ret != 0) {
508                         pr_err("%s: Can't write the grip face suppression config\n",
509                                __func__);
510                         return ret;
511                 }
512         }
513
514         /* configure noise suppression */
515         obj = find_obj(ts, QTM_OBJ_PROCG_NOISE_SUPPRESSION);
516         if (obj && obj->entry.num_inst > 0) {
517                 ret = qtouch_write_addr(ts, obj->entry.addr,
518                                         &ts->pdata->noise_suppression_cfg,
519                                         min(sizeof(ts->pdata->noise_suppression_cfg),
520                                             obj->entry.size));
521                 if (ret != 0) {
522                         pr_err("%s: Can't write the noise suppression config\n",
523                                __func__);
524                         return ret;
525                 }
526         }
527
528         /* configure the touch proximity sensor */
529         obj = find_obj(ts, QTM_OBJ_TOUCH_PROXIMITY);
530         if (obj && obj->entry.num_inst > 0) {
531                 ret = qtouch_write_addr(ts, obj->entry.addr,
532                                         &ts->pdata->touch_proximity_cfg,
533                                         min(sizeof(ts->pdata->touch_proximity_cfg),
534                                             obj->entry.size));
535                 if (ret != 0) {
536                         pr_err("%s: Can't write the touch proximity config\n",
537                                __func__);
538                         return ret;
539                 }
540         }
541
542         /* configure the one touch gesture processor */
543         obj = find_obj(ts, QTM_OBJ_PROCI_ONE_TOUCH_GESTURE_PROC);
544         if (obj && obj->entry.num_inst > 0) {
545                 ret = qtouch_write_addr(ts, obj->entry.addr,
546                                         &ts->pdata->one_touch_gesture_proc_cfg,
547                                         min(sizeof(ts->pdata->one_touch_gesture_proc_cfg),
548                                             obj->entry.size));
549                 if (ret != 0) {
550                         pr_err("%s: Can't write the one touch gesture processor config\n",
551                                __func__);
552                         return ret;
553                 }
554         }
555
556         /* configure self test */
557         obj = find_obj(ts, QTM_OBJ_SPT_SELF_TEST);
558         if (obj && obj->entry.num_inst > 0) {
559                 ret = qtouch_write_addr(ts, obj->entry.addr,
560                                         &ts->pdata->self_test_cfg,
561                                         min(sizeof(ts->pdata->self_test_cfg),
562                                             obj->entry.size));
563                 if (ret != 0) {
564                         pr_err("%s: Can't write the self test config\n",
565                                __func__);
566                         return ret;
567                 }
568         }
569
570         /* configure the two touch gesture processor */
571         obj = find_obj(ts, QTM_OBJ_PROCI_TWO_TOUCH_GESTURE_PROC);
572         if (obj && obj->entry.num_inst > 0) {
573                 ret = qtouch_write_addr(ts, obj->entry.addr,
574                                         &ts->pdata->two_touch_gesture_proc_cfg,
575                                         min(sizeof(ts->pdata->two_touch_gesture_proc_cfg),
576                                             obj->entry.size));
577                 if (ret != 0) {
578                         pr_err("%s: Can't write the two touch gesture processor config\n",
579                                __func__);
580                         return ret;
581                 }
582         }
583
584         /* configure the capacitive touch engine  */
585         obj = find_obj(ts, QTM_OBJ_SPT_CTE_CONFIG);
586         if (obj && obj->entry.num_inst > 0) {
587                 ret = qtouch_write_addr(ts, obj->entry.addr,
588                                         &ts->pdata->cte_config_cfg,
589                                         min(sizeof(ts->pdata->cte_config_cfg),
590                                             obj->entry.size));
591                 if (ret != 0) {
592                         pr_err("%s: Can't write the capacitive touch engine config\n",
593                                __func__);
594                         return ret;
595                 }
596         }
597
598         /* configure the noise suppression table */
599         obj = find_obj(ts, QTM_OBJ_NOISESUPPRESSION_1);
600         if (obj && obj->entry.num_inst > 0) {
601                 ret = qtouch_write_addr(ts, obj->entry.addr,
602                                         &ts->pdata->noise1_suppression_cfg,
603                                         min(sizeof
604                                             (ts->pdata->noise1_suppression_cfg),
605                                             obj->entry.size));
606                 if (ret != 0) {
607                         pr_err("%s: Can't write the noise suppression config\n",
608                                __func__);
609                         return ret;
610                 }
611         }
612
613         /* configure the grip suppression table */
614         obj = find_obj(ts, QTM_OBJ_PROCI_GRIPSUPPRESSION);
615         if (obj && obj->entry.num_inst > 0) {
616                 ret = qtouch_write_addr(ts, obj->entry.addr,
617                                         &ts->pdata->gripsuppression_t40_cfg,
618                                         min(sizeof(ts->pdata->gripsuppression_t40_cfg),
619                                             obj->entry.size));
620                 if (ret != 0) {
621                         pr_err("%s: Can't write the grip suppression config\n",
622                                __func__);
623                         return ret;
624                 }
625         }
626
627         /* configure the palm suppression table */
628         obj = find_obj(ts, QTM_OBJ_PROCI_PALMSUPPRESSION);
629         if (obj && obj->entry.num_inst > 0) {
630                 ret = qtouch_write_addr(ts, obj->entry.addr,
631                                         &ts->pdata->palm_suppression_cfg,
632                                         min(sizeof(ts->pdata->palm_suppression_cfg),
633                                             obj->entry.size));
634                 if (ret != 0) {
635                         pr_err("%s: Can't write the palm suppression config\n",
636                                __func__);
637                         return ret;
638                 }
639         }
640
641         /* configure the Digitizer HID config */
642         obj = find_obj(ts, QTM_OBJ_SPT_DIGITIZER);
643         if (obj && obj->entry.num_inst > 0) {
644                 ret = qtouch_write_addr(ts, obj->entry.addr,
645                                         &ts->pdata->spt_digitizer_cfg,
646                                         min(sizeof(ts->pdata->spt_digitizer_cfg),
647                                             obj->entry.size));
648                 if (ret != 0) {
649                         pr_err("%s: Can't write the Digitizer HID config\n",
650                                __func__);
651                         return ret;
652                 }
653         }
654
655         ret = qtouch_force_calibration(ts);
656         if (ret != 0) {
657                 pr_err("%s: Unable to recalibrate after reset\n", __func__);
658                 return ret;
659         }
660
661         /* Write the settings into nvram, if needed */
662         if (ts->pdata->flags & QTOUCH_CFG_BACKUPNV) {
663                 uint8_t val;
664                 uint16_t addr;
665
666                 obj = find_obj(ts, QTM_OBJ_GEN_CMD_PROC);
667                 addr = obj->entry.addr + offsetof(struct qtm_gen_cmd_proc,
668                                                   backupnv);
669                 val = 0x55;
670                 ret = qtouch_write_addr(ts, addr, &val, 1);
671                 if (ret != 0) {
672                         pr_err("%s: Can't backup nvram settings\n", __func__);
673                         return ret;
674                 }
675                 /* Since the IC does not indicate that has completed the
676                 backup place a hard wait here.  If we communicate with the
677                 IC during backup the EEPROM may be corrupted */
678
679                 msleep(QTM_OBP_SLEEP_WAIT_FOR_BACKUP);
680         }
681
682         /* If debugging, read back and print all settings */
683         if (qtouch_tsdebug) {
684                 int object;
685                 int size;
686                 uint8_t *data_buff;
687                 int byte;
688                 int msg_bytes;
689                 int msg_location;
690                 char *msg;
691
692                 msg = kmalloc(1024, GFP_KERNEL);
693                 if (msg != NULL) {
694                         for (object = 7; object < QTM_OBP_MAX_OBJECT_NUM; object++) {
695
696                                 size = ts->obj_tbl[object].entry.size
697                                        * ts->obj_tbl[object].entry.num_inst;
698                                 if (size != 0) {
699                                         data_buff = kmalloc(size, GFP_KERNEL);
700                                         if (data_buff == NULL) {
701                                                 pr_err("%s: Object %d: Malloc failed\n",
702                                                        __func__, object);
703                                                 continue;
704                                         }
705
706                                         qtouch_read_addr(ts,
707                                                          ts->obj_tbl[object].entry.addr,
708                                                          (void *)data_buff, size);
709
710                                         msg_location = sprintf(msg, "%s: Object %d:",
711                                                                __func__, object);
712                                         for (byte = 0; byte < size; byte++) {
713                                                 msg_bytes = snprintf((msg + msg_location),
714                                                                     (1024 - msg_location),
715                                                                     " 0x%02x",
716                                                                     *(data_buff + byte));
717                                                 msg_location += msg_bytes;
718                                                 if (msg_location >= 1024)
719                                                         break;
720                                         }
721                                         if (msg_location < 1024) {
722                                                 pr_info("%s\n", msg);
723                                         } else {
724                                                 pr_info("%s:  Object %d: String overflow\n",
725                                                         __func__, object);
726                                         }
727
728                                         kfree(data_buff);
729                                 }
730                         }
731
732                         kfree(msg);
733                 }
734         }
735
736         /* reset the address pointer */
737         ret = qtouch_set_addr(ts, ts->obj_tbl[QTM_OBJ_GEN_MSG_PROC].entry.addr);
738         if (ret != 0) {
739                 pr_err("%s: Unable to reset address pointer after reset\n",
740                        __func__);
741                 return ret;
742         }
743
744         return 0;
745 }
746
747 /* Handles a message from the command processor object. */
748 static int do_cmd_proc_msg(struct qtouch_ts_data *ts, struct qtm_object *obj,
749                            void *_msg)
750 {
751         struct qtm_cmd_proc_msg *msg = _msg;
752         int ret = 0;
753         int hw_reset = 0;
754         uint32_t checksum = (msg->checksum[2] << 16)
755                                 | (msg->checksum[1] << 8) | msg->checksum[0];
756
757         if (msg->status & QTM_CMD_PROC_STATUS_RESET) {
758                 if (qtouch_tsdebug)
759                         pr_info("%s:EEPROM checksum is 0x%X cnt %i\n",
760                                 __func__, checksum, ts->checksum_cnt);
761                 if (checksum != ts->eeprom_checksum) {
762                         if (ts->checksum_cnt > 2) {
763                                 /* Assume the checksum is what it is, cannot
764                                 disable the touch screen so set the checksum*/
765                                 ts->eeprom_checksum = checksum;
766                                 ts->checksum_cnt = 0;
767                         } else {
768                                 pr_info("%s:EEPROM checksum doesn't match 0x%x\n",
769                                         __func__, checksum);
770                                 ret = qtouch_hw_init(ts);
771                                 if (ret != 0)
772                                         pr_err("%s:Cannot init the touch IC\n",
773                                                    __func__);
774                                 hw_reset = 1;
775                                 ts->checksum_cnt++;
776                         }
777                 } else {
778                         pr_info("%s:EEPROM checksum matches\n", __func__);
779                 }
780                 pr_info("%s: Reset done.\n", __func__);
781         }
782
783         if (msg->status & QTM_CMD_PROC_STATUS_CAL)
784                 pr_info("%s: Self-calibration started.\n", __func__);
785
786         if (msg->status & QTM_CMD_PROC_STATUS_OFL)
787                 pr_err("%s: Acquisition cycle length overflow\n", __func__);
788
789         if (msg->status & QTM_CMD_PROC_STATUS_SIGERR)
790                 pr_err("%s: Acquisition error\n", __func__);
791
792         if (msg->status & QTM_CMD_PROC_STATUS_CFGERR) {
793                 pr_err("%s: Configuration error\n", __func__);
794                 ret = qtouch_hw_init(ts);
795                 if (ret != 0)
796                         pr_err("%s:Cannot init the touch IC\n", __func__);
797         }
798         /* Check the EEPROM checksum.  An ESD event may cause
799         the checksum to change during operation so we need to
800         reprogram the EEPROM and reset the IC */
801         if (ts->pdata->flags & QTOUCH_EEPROM_CHECKSUM) {
802                 if (checksum != ts->eeprom_checksum) {
803                         if (qtouch_tsdebug)
804                                 pr_info("%s:EEPROM checksum is 0x%X cnt %i\n",
805                                         __func__, checksum,
806                                         ts->checksum_cnt);
807                         if (ts->checksum_cnt > 2) {
808                                 /* Assume the checksum is what it is, cannot
809                                 disable the touch screen so set the checksum*/
810                                 ts->eeprom_checksum = checksum;
811                                 ts->checksum_cnt = 0;
812                         } else {
813                                 if (!hw_reset) {
814                                         ret = qtouch_hw_init(ts);
815                                         if (ret != 0)
816                                                 pr_err("%s:Cannot init the touch IC\n",
817                                                 __func__);
818                                         qtouch_force_reset(ts, 0);
819                                         ts->checksum_cnt++;
820                                 }
821                         }
822                 }
823         }
824         return ret;
825 }
826
827 /* Handles a message from a multi-touch object. */
828 static int do_touch_multi_msg(struct qtouch_ts_data *ts, struct qtm_object *obj,
829                               void *_msg)
830 {
831         struct qtm_touch_multi_msg *msg = _msg;
832         int i;
833         int x;
834         int y;
835         int pressure;
836         int width;
837         uint32_t finger;
838         int down;
839
840         finger = msg->report_id - obj->report_id_min;
841         if (finger >= ts->pdata->multi_touch_cfg.num_touch)
842                 return 0;
843
844         if (qtouch_tsdebug & 0x10)
845                 pr_info("%s: msgxpos_msb 0x%X msgypos_msb 0x%X msgxypos 0x%X \n",
846                         __func__, msg->xpos_msb, msg->ypos_msb, msg->xypos_lsb);
847
848         /* x/y are 10bit values(<1024), with bottom 2 bits inside the xypos_lsb */
849         /* x/y are 12bit values(>1023), with bottom 4 bits inside the xypos_lsb */
850         x = (msg->xpos_msb << ts->xpos_lshift_msb) |
851                 ((msg->xypos_lsb >> ts->xpos_rshift_lsb) & 0xf);
852         y = (msg->ypos_msb << ts->ypos_lshift_msb) |
853                 ((msg->xypos_lsb >> ts->ypos_rshift_lsb) & 0xf);
854
855         width = msg->touch_area;
856         pressure = msg->touch_amp;
857
858         if (qtouch_tsdebug & 2)
859                 pr_info("%s: stat=%02x, f=%d x=%d y=%d p=%d w=%d\n", __func__,
860                         msg->status, finger, x, y, pressure, width);
861
862         if (finger >= _NUM_FINGERS) {
863                 pr_err("%s: Invalid finger number %dd\n", __func__, finger);
864                 return 1;
865         }
866
867         down = !(msg->status & (QTM_TOUCH_MULTI_STATUS_RELEASE |
868                  QTM_TOUCH_MULTI_STATUS_SUPPRESS));
869
870         ts->finger_data[finger].x_data = x;
871         ts->finger_data[finger].y_data = y;
872         ts->finger_data[finger].w_data = width;
873         ts->finger_data[finger].vector = msg->touch_vect;
874
875         /* The touch IC will not give back a pressure of zero
876            so send a 0 when a liftoff is produced */
877         if (!down) {
878                 ts->finger_data[finger].z_data = 0;
879         } else {
880                 ts->finger_data[finger].z_data = pressure;
881                 ts->finger_data[finger].down = down;
882         }
883
884         for (i = 0; i < ts->pdata->multi_touch_cfg.num_touch; i++) {
885                 if (ts->finger_data[i].down == 0)
886                         continue;
887                 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR,
888                                  ts->finger_data[i].z_data);
889                 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR,
890                                  ts->finger_data[i].w_data);
891                 input_report_abs(ts->input_dev, ABS_MT_POSITION_X,
892                                  ts->finger_data[i].x_data);
893                 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y,
894                                  ts->finger_data[i].y_data);
895                 input_report_abs(ts->input_dev, ABS_MT_ORIENTATION,
896                                  ts->finger_data[i].vector);
897                 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID,
898                                  i);
899                 input_mt_sync(ts->input_dev);
900         }
901         input_sync(ts->input_dev);
902
903         if (!down) {
904                 memset(&ts->finger_data[finger], 0,
905                 sizeof(struct coordinate_map));
906         }
907
908         return 0;
909 }
910
911 /* Handles a message from a keyarray object. */
912 static int do_touch_keyarray_msg(struct qtouch_ts_data *ts,
913                                  struct qtm_object *obj, void *_msg)
914 {
915         struct qtm_touch_keyarray_msg *msg = _msg;
916         int i;
917
918         /* nothing changed.. odd. */
919         if (ts->last_keystate == msg->keystate)
920                 return 0;
921
922         for (i = 0; i < ts->pdata->key_array.num_keys; ++i) {
923                 struct qtouch_key *key = &ts->pdata->key_array.keys[i];
924                 uint32_t bit = 1 << (key->channel & 0x1f);
925                 if ((msg->keystate & bit) != (ts->last_keystate & bit))
926                         input_report_key(ts->input_dev, key->code,
927                                          msg->keystate & bit);
928         }
929         input_sync(ts->input_dev);
930
931         if (qtouch_tsdebug & 2)
932                 pr_info("%s: key state changed 0x%08x -> 0x%08x\n", __func__,
933                         ts->last_keystate, msg->keystate);
934
935         /* update our internal state */
936         ts->last_keystate = msg->keystate;
937
938         return 0;
939 }
940
941 static int qtouch_handle_msg(struct qtouch_ts_data *ts, struct qtm_object *obj,
942                              struct qtm_obj_message *msg)
943 {
944         int ret = 0;
945
946         /* These are all the known objects that we know how to handle. */
947         switch (obj->entry.type) {
948         case QTM_OBJ_GEN_CMD_PROC:
949                 ret = do_cmd_proc_msg(ts, obj, msg);
950                 break;
951
952         case QTM_OBJ_TOUCH_MULTI:
953                 ret = do_touch_multi_msg(ts, obj, msg);
954                 break;
955
956         case QTM_OBJ_TOUCH_KEYARRAY:
957                 ret = do_touch_keyarray_msg(ts, obj, msg);
958                 break;
959
960         default:
961                 /* probably not fatal? */
962                 ret = 0;
963                 pr_info("%s: No handler defined for message from object "
964                         "type %d, report_id %d\n", __func__, obj->entry.type,
965                         msg->report_id);
966         }
967
968         return ret;
969 }
970
971 static int qtouch_ts_prep_msg_proc(struct qtouch_ts_data *ts)
972 {
973         struct qtm_object *obj;
974         int err;
975
976         ts->msg_buf = kmalloc(ts->msg_size, GFP_KERNEL);
977         if (ts->msg_buf == NULL) {
978                 pr_err("%s: Cannot allocate msg_buf\n", __func__);
979                 err = -ENOMEM;
980                 goto err_alloc_msg_buf;
981         }
982
983         /* Point the address pointer to the message processor.
984          * Must do this before enabling interrupts */
985         obj = find_obj(ts, QTM_OBJ_GEN_MSG_PROC);
986         err = qtouch_set_addr(ts, obj->entry.addr);
987         if (err != 0) {
988                 pr_err("%s: Can't to set addr to msg processor\n", __func__);
989                 goto err_rst_addr_msg_proc;
990         }
991
992         return 0;
993
994 err_rst_addr_msg_proc:
995         if (ts->msg_buf)
996                 kfree(ts->msg_buf);
997 err_alloc_msg_buf:
998
999         return err;
1000 }
1001
1002 int qtouch_input_open(struct input_dev *input)
1003 {
1004         int err;
1005         struct qtouch_ts_data *ts = input_get_drvdata(input);
1006
1007         if (!atomic_xchg(&ts->process_open, 0))
1008                 return 0;
1009
1010         if (ts->touch_fw_image == NULL)
1011                 goto finish_touch_upgrade;
1012
1013         err = request_firmware(&fw_entry, ts->pdata->touch_fw_cfg.fw_name,
1014                                  &ts->client->dev);
1015
1016         if (err == 0) {
1017                 ts->touch_fw = (uint8_t *)fw_entry->data;
1018                 ts->touch_fw_size = fw_entry->size;
1019                 pr_info("firmware name: %s size: %d\n", ts->touch_fw_image,
1020                          ts->touch_fw_size);
1021
1022                 if ((ts->touch_fw_size != 0) && (ts->touch_fw != NULL)) {
1023                         /* Add 2 because the firmware packet size bytes
1024                         are not taken into account for the total size */
1025                         ts->boot_pkt_size = ((ts->touch_fw[0] << 8) |
1026                                 ts->touch_fw[1]) + 2;
1027
1028                         pr_info("%s: write first packet \n", __func__);
1029                         err = qtouch_write(ts, &ts->touch_fw[0], ts->boot_pkt_size);
1030                         if (err != ts->boot_pkt_size) {
1031                                 pr_err("%s: Could not write the first packet %i\n", __func__, err);
1032                                 goto reset_to_normal;
1033                         }
1034                         goto finish_touch_upgrade;
1035                 }
1036                 goto reset_to_cleanup;
1037         } else {
1038                 pr_err("%s: Firmware %s not available : %d\n",
1039                          __func__, ts->pdata->touch_fw_cfg.fw_name, err);
1040                 ts->touch_fw = NULL;
1041                 goto reset_to_normal;
1042         }
1043
1044 reset_to_cleanup:
1045         release_firmware(fw_entry);
1046 reset_to_normal:
1047         ts->status = 0xff;
1048         qtouch_force_reset(ts, 0);
1049 finish_touch_upgrade:
1050
1051         return 0;
1052 }
1053
1054 static int qtouch_ts_register_input(struct qtouch_ts_data *ts)
1055 {
1056         int err;
1057         int i;
1058
1059         if (ts->input_dev == NULL) {
1060                 ts->input_dev = input_allocate_device();
1061                 if (ts->input_dev == NULL) {
1062                         pr_err("%s: failed to alloc input device\n", __func__);
1063                         err = -ENOMEM;
1064                         return err;
1065                 }
1066         }
1067
1068         ts->input_dev->name = "qtouch-touchscreen";
1069         input_set_drvdata(ts->input_dev, ts);
1070
1071         set_bit(EV_SYN, ts->input_dev->evbit);
1072
1073         /* register the harwdare assisted virtual keys, if any */
1074         if (ts->pdata->flags & QTOUCH_USE_KEYARRAY) {
1075                 for (i = 0; i < ts->pdata->key_array.num_keys; ++i)
1076                         input_set_capability(ts->input_dev, EV_KEY,
1077                                              ts->pdata->key_array.keys[i].code);
1078         }
1079
1080         /* register the software virtual keys, if any are provided */
1081         for (i = 0; i < ts->pdata->vkeys.count; ++i)
1082                 input_set_capability(ts->input_dev, EV_KEY,
1083                                      ts->pdata->vkeys.keys[i].code);
1084
1085         if (ts->pdata->flags & QTOUCH_USE_MULTITOUCH) {
1086                 set_bit(EV_ABS, ts->input_dev->evbit);
1087                 /* Legacy support for testing only */
1088                 input_set_capability(ts->input_dev, EV_KEY, BTN_TOUCH);
1089                 input_set_capability(ts->input_dev, EV_KEY, BTN_2);
1090                 input_set_abs_params(ts->input_dev, ABS_X,
1091                                      ts->pdata->abs_min_x, ts->pdata->abs_max_x,
1092                                      ts->pdata->fuzz_x, 0);
1093                 input_set_abs_params(ts->input_dev, ABS_HAT0X,
1094                                      ts->pdata->abs_min_x, ts->pdata->abs_max_x,
1095                                      ts->pdata->fuzz_x, 0);
1096                 input_set_abs_params(ts->input_dev, ABS_Y,
1097                                      ts->pdata->abs_min_y, ts->pdata->abs_max_y,
1098                                      ts->pdata->fuzz_y, 0);
1099                 input_set_abs_params(ts->input_dev, ABS_HAT0Y,
1100                                      ts->pdata->abs_min_x, ts->pdata->abs_max_x,
1101                                      ts->pdata->fuzz_x, 0);
1102                 input_set_abs_params(ts->input_dev, ABS_PRESSURE,
1103                                      ts->pdata->abs_min_p, ts->pdata->abs_max_p,
1104                                      ts->pdata->fuzz_p, 0);
1105                 input_set_abs_params(ts->input_dev, ABS_TOOL_WIDTH,
1106                                      ts->pdata->abs_min_w, ts->pdata->abs_max_w,
1107                                      ts->pdata->fuzz_w, 0);
1108
1109                 /* multi touch */
1110                 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
1111                                      ts->pdata->abs_min_x, ts->pdata->abs_max_x,
1112                                      ts->pdata->fuzz_x, 0);
1113                 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
1114                                      ts->pdata->abs_min_y, ts->pdata->abs_max_y,
1115                                      ts->pdata->fuzz_y, 0);
1116                 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR,
1117                                      ts->pdata->abs_min_p, ts->pdata->abs_max_p,
1118                                      ts->pdata->fuzz_p, 0);
1119                 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR,
1120                                      ts->pdata->abs_min_w, ts->pdata->abs_max_w,
1121                                      ts->pdata->fuzz_w, 0);
1122                 input_set_abs_params(ts->input_dev, ABS_MT_ORIENTATION,
1123                                      0, 255, 0, 0);
1124                 input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID,
1125                                      0, ts->pdata->multi_touch_cfg.num_touch, 1, 0);
1126         }
1127
1128         memset(&ts->finger_data[0], 0,
1129                (sizeof(struct coordinate_map) *
1130                _NUM_FINGERS));
1131
1132         ts->input_dev->open = qtouch_input_open;
1133
1134         err = input_register_device(ts->input_dev);
1135         if (err != 0) {
1136                 pr_err("%s: Cannot register input device \"%s\"\n", __func__,
1137                        ts->input_dev->name);
1138                 goto err_input_register_dev;
1139         }
1140         return 0;
1141
1142 err_input_register_dev:
1143         input_free_device(ts->input_dev);
1144         ts->input_dev = NULL;
1145
1146         return err;
1147 }
1148
1149 static int qtouch_process_info_block(struct qtouch_ts_data *ts)
1150 {
1151         struct qtm_id_info qtm_info;
1152         uint32_t our_csum = 0x0;
1153         uint32_t their_csum;
1154         uint8_t report_id;
1155         uint16_t addr;
1156         int err;
1157         int i;
1158         uint8_t *info_blk_buf, *info_blk_start;
1159         uint16_t info_blk_size;
1160         struct qtm_obj_entry entry;
1161
1162         /* query the device and get the info block. */
1163         err = qtouch_read_addr(ts, QTM_OBP_ID_INFO_ADDR, &qtm_info,
1164                                sizeof(qtm_info));
1165         if (err != 0) {
1166                 pr_err("%s: Cannot read info object block\n", __func__);
1167                 goto err_read_info_block;
1168         }
1169
1170         pr_info("%s: Build version is 0x%x\n", __func__, qtm_info.version);
1171
1172         if (qtm_info.num_objs == 0) {
1173                 pr_err("%s: Device (0x%x/0x%x/0x%x/0x%x) does not export any "
1174                        "objects.\n", __func__, qtm_info.family_id,
1175                        qtm_info.variant_id, qtm_info.version, qtm_info.build);
1176                 err = -ENODEV;
1177                 goto err_no_objects;
1178         }
1179
1180         info_blk_size = sizeof(qtm_info) + qtm_info.num_objs * sizeof(entry);
1181         info_blk_buf = kzalloc(info_blk_size, GFP_KERNEL);
1182         if (info_blk_buf == NULL) {
1183                 pr_err("%s: Can't allocate write buffer (%d)\n",
1184                          __func__, info_blk_size);
1185                 err = -ENOMEM;
1186                 goto err_no_objects;
1187         }
1188         info_blk_start = info_blk_buf;
1189         memcpy(info_blk_buf, (void *)&qtm_info, sizeof(qtm_info));
1190         info_blk_buf += sizeof(qtm_info);
1191         addr = QTM_OBP_ID_INFO_ADDR + sizeof(qtm_info);
1192         report_id = 1;
1193
1194         /* Clear the object table */
1195         for (i = 0; i < QTM_OBP_MAX_OBJECT_NUM; ++i) {
1196                 ts->obj_tbl[i].entry.type = 0;
1197                 ts->obj_tbl[i].entry.addr = 0;
1198                 ts->obj_tbl[i].entry.size = 0;
1199                 ts->obj_tbl[i].entry.num_inst = 0;
1200                 ts->obj_tbl[i].entry.num_rids = 0;
1201                 ts->obj_tbl[i].report_id_min = 0;
1202                 ts->obj_tbl[i].report_id_max = 0;
1203         }
1204
1205         pr_info("%s: Num obj: %i addr: %i\n", __func__, qtm_info.num_objs, addr);
1206         /* read out the object entries table */
1207         for (i = 0; i < qtm_info.num_objs; ++i) {
1208                 struct qtm_object *obj;
1209
1210                 pr_info("%s: Reading addr: %i\n", __func__,  addr);
1211                 err = qtouch_read_addr(ts, addr, &entry, sizeof(entry));
1212                 if (err != 0) {
1213                         pr_err("%s: Can't read object (%d) entry.\n",
1214                                __func__, i);
1215                         err = -EIO;
1216                         goto err_read_entry;
1217                 }
1218
1219                 memcpy(info_blk_buf, (void *)&entry, sizeof(entry));
1220                 info_blk_buf += sizeof(entry);
1221                 addr += sizeof(entry);
1222
1223                 entry.size++;
1224                 entry.num_inst++;
1225
1226                 pr_info("%s: Object %d @ 0x%04x (%d) insts %d rep_ids %d\n",
1227                         __func__, entry.type, entry.addr, entry.size,
1228                         entry.num_inst, entry.num_rids);
1229
1230                 if (entry.type >= QTM_OBP_MAX_OBJECT_NUM) {
1231                         pr_warning("%s: Unknown object type (%d) encountered\n",
1232                                    __func__, entry.type);
1233                         /* Not fatal */
1234                         continue;
1235                 }
1236
1237                 /* save the message_procesor msg_size for easy reference. */
1238                 if (entry.type == QTM_OBJ_GEN_MSG_PROC) {
1239                         if (ts->pdata->flags & QTOUCH_USE_MSG_CRC) {
1240                                 ts->msg_size = entry.size;
1241                                 entry.addr |= QTOUCH_USE_MSG_CRC_MASK;
1242                         } else {
1243                                 ts->msg_size = entry.size - 1;
1244                         }
1245                 }
1246
1247                 obj = create_obj(ts, &entry);
1248                 /* set the report_id range that the object is responsible for */
1249                 if ((obj->entry.num_rids * obj->entry.num_inst) != 0) {
1250                         obj->report_id_min = report_id;
1251                         report_id += obj->entry.num_rids * obj->entry.num_inst;
1252                         obj->report_id_max = report_id - 1;
1253                 }
1254         }
1255
1256         if (!ts->msg_size) {
1257                 pr_err("%s: Message processing object not found. Bailing.\n",
1258                        __func__);
1259                 err = -ENODEV;
1260                 goto err_no_msg_proc;
1261         }
1262
1263         /* verify that some basic objects are present. These objects are
1264          * assumed to be present by the rest of the driver, so fail out now
1265          * if the firmware is busted. */
1266         if (!find_obj(ts, QTM_OBJ_GEN_PWR_CONF) ||
1267             !find_obj(ts, QTM_OBJ_GEN_ACQUIRE_CONF) ||
1268             !find_obj(ts, QTM_OBJ_GEN_MSG_PROC) ||
1269             !find_obj(ts, QTM_OBJ_GEN_CMD_PROC)) {
1270                 pr_err("%s: Required objects are missing\n", __func__);
1271                 err = -ENOENT;
1272                 goto err_missing_objs;
1273         }
1274
1275         err = qtouch_read_addr(ts, addr, &their_csum, sizeof(their_csum));
1276         if (err != 0) {
1277                 pr_err("%s: Unable to read remote checksum\n", __func__);
1278                 err = -ENODEV;
1279                 goto err_no_checksum;
1280         }
1281
1282         our_csum = calc_csum(our_csum, info_blk_start, info_blk_size);
1283
1284         if (our_csum != their_csum) {
1285                 pr_warning("%s: Checksum mismatch (0x%08x != 0x%08x)\n",
1286                            __func__, our_csum, their_csum);
1287 #ifndef IGNORE_CHECKSUM_MISMATCH
1288                 err = -ENODEV;
1289                 goto err_bad_checksum;
1290 #endif
1291         }
1292
1293         pr_info("%s: %s found.\n"
1294                 "  family 0x%x, variant 0x%x, ver 0x%x, build 0x%x\n"
1295                 "  matrix %dx%d, %d objects, info blk chksum 0x%x\n", __func__,
1296                 QTOUCH_TS_NAME, qtm_info.family_id, qtm_info.variant_id,
1297                 qtm_info.version, qtm_info.build, qtm_info.matrix_x_size,
1298                 qtm_info.matrix_y_size, qtm_info.num_objs, our_csum);
1299
1300         ts->eeprom_checksum = ts->pdata->nv_checksum;
1301         ts->family_id = qtm_info.family_id;
1302         ts->variant_id = qtm_info.variant_id;
1303         ts->fw_version = qtm_info.version;
1304         ts->build_version = qtm_info.build;
1305         kfree(info_blk_start);
1306
1307         return 0;
1308
1309 #ifndef IGNORE_CHECKSUM_MISMATCH
1310 err_bad_checksum:
1311 #endif
1312 err_no_checksum:
1313 err_missing_objs:
1314 err_no_msg_proc:
1315 err_read_entry:
1316         kfree(info_blk_start);
1317 err_no_objects:
1318 err_read_info_block:
1319         return err;
1320 }
1321
1322 static int qtouch_ts_unregister_input(struct qtouch_ts_data *ts)
1323 {
1324         input_unregister_device(ts->input_dev);
1325         ts->input_dev = NULL;
1326         return 0;
1327 }
1328
1329 static void qtouch_ts_boot_work_func(struct work_struct *work)
1330 {
1331         int err = 0;
1332         struct qtouch_ts_data *ts = container_of(work,
1333                                                  struct qtouch_ts_data,
1334                                                  boot_work);
1335         unsigned char boot_msg[3];
1336
1337         if (ts->status == 0xff) {
1338                 pr_err("%s: Entered in Wrong Mode\n", __func__);
1339                 goto touch_to_normal_mode;
1340         }
1341
1342         err = qtouch_read(ts, &boot_msg, sizeof(boot_msg));
1343         if (err) {
1344                 pr_err("%s: Cannot read message\n", __func__);
1345                 goto done;
1346         }
1347         if (qtouch_tsdebug & 8)
1348                 pr_err("%s: Message is 0x%X err is %i\n",
1349                        __func__, boot_msg[0], err);
1350
1351         if (boot_msg[0] == QTM_OBP_BOOT_CRC_CHECK) {
1352                 if (qtouch_tsdebug & 8)
1353                     pr_err("%s: CRC Check\n", __func__);
1354                 goto done;
1355         } else if (boot_msg[0] == QTM_OBP_BOOT_CRC_FAIL) {
1356                 if (qtouch_tsdebug & 8)
1357                         pr_err("%s: Boot size %i current pkt size %i\n",
1358                         __func__, ts->boot_pkt_size, ts->current_pkt_sz);
1359
1360                 if (ts->fw_error_count > 3) {
1361                         pr_err("%s: Resetting the IC fw upgrade failed\n",
1362                                 __func__);
1363                         goto reset_touch_ic;
1364                 } else {
1365                         /* If this is a failure on the first packet then
1366                         reset the boot packet size to 0 */
1367                         if (!ts->fw_error_count) {
1368                                 if (ts->current_pkt_sz == 0) {
1369                                         ts->current_pkt_sz = ts->boot_pkt_size;
1370                                         ts->boot_pkt_size -= ts->boot_pkt_size;
1371                                 }
1372                         }
1373                         ts->fw_error_count++;
1374                         pr_err("%s: Frame CRC check failed %i times\n",
1375                                 __func__, ts->fw_error_count);
1376                 }
1377                 goto done;
1378         } else if (boot_msg[0] == QTM_OBP_BOOT_CRC_PASSED) {
1379                 if (qtouch_tsdebug & 8)
1380                     pr_err("%s: Frame CRC check passed\n", __func__);
1381
1382                 ts->status =
1383                     (ts->boot_pkt_size * 100) / ts->touch_fw_size;
1384
1385                 ts->boot_pkt_size += ts->current_pkt_sz;
1386                 ts->fw_error_count = 0;
1387
1388                 /* Check to see if the update is done if it is
1389                    then register the touch with the system */
1390                 if (ts->boot_pkt_size == ts->touch_fw_size) {
1391                         pr_info("%s: Touch FW update done\n", __func__);
1392                         ts->status = 100;
1393                         goto touch_to_normal_mode;
1394                 }
1395                 goto done;
1396         } else if (boot_msg[0] & QTM_OBP_BOOT_WAIT_FOR_DATA) {
1397                 if (qtouch_tsdebug & 8)
1398                         pr_err("%s: Data sent so far %i\n",
1399                                 __func__, ts->boot_pkt_size);
1400
1401                 /* Don't change the packet size if there was a failure */
1402                 if (!ts->fw_error_count) {
1403                         ts->current_pkt_sz =
1404                             ((ts->touch_fw[ts->boot_pkt_size] << 8) |
1405                                 ts->touch_fw[ts->boot_pkt_size + 1]) + 2;
1406                 }
1407                 if (qtouch_tsdebug & 8)
1408                         pr_err("%s: Size of the next packet is %i\n",
1409                                 __func__, ts->current_pkt_sz);
1410
1411                 err = qtouch_write(ts, &ts->touch_fw[ts->boot_pkt_size],
1412                         ts->current_pkt_sz);
1413                 if (err != ts->current_pkt_sz) {
1414                         pr_err("%s: Could not write the packet %i\n",
1415                                 __func__, err);
1416                         ts->status = 0xff;
1417                         goto reset_touch_ic;
1418                 }
1419         } else {
1420                 pr_err("%s: Message is 0x%X is not handled\n",
1421                         __func__, boot_msg[0]);
1422         }
1423
1424 done:
1425         enable_irq(ts->client->irq);
1426         return;
1427
1428 reset_touch_ic:
1429         qtouch_force_reset(ts, 0);
1430 touch_to_normal_mode:
1431         if (ts->touch_fw)
1432                 release_firmware(fw_entry);
1433         ts->client->addr = ts->org_i2c_addr;
1434         ts->mode = 0;
1435         /* Wait for the IC to recover */
1436         msleep(QTM_OBP_SLEEP_WAIT_FOR_RESET);
1437         err = qtouch_process_info_block(ts);
1438         if (err != 0) {
1439                 pr_err("%s:Cannot read info block %i\n", __func__, err);
1440                 goto err_return;
1441         }
1442         err = qtouch_ts_prep_msg_proc(ts);
1443         if (err != 0) {
1444                 pr_err("%s: setting message proc failed %i\n", __func__, err);
1445                 goto err_return;
1446         }
1447
1448         enable_irq(ts->client->irq);
1449 err_return:
1450         return;
1451 }
1452
1453 static void qtouch_ts_work_func(struct work_struct *work)
1454 {
1455         struct qtouch_ts_data *ts =
1456                 container_of(work, struct qtouch_ts_data, work);
1457         struct qtm_obj_message *msg;
1458         struct qtm_object *obj;
1459         int ret;
1460
1461         msg = qtouch_read_msg(ts);
1462         if (msg == NULL) {
1463                 pr_err("%s: Cannot read message\n", __func__);
1464                 goto done;
1465         }
1466
1467         obj = find_object_rid(ts, msg->report_id);
1468         if (!obj) {
1469                 pr_err("%s: Unknown object for report_id %d\n", __func__,
1470                        msg->report_id);
1471                 goto done;
1472         }
1473
1474         ret = qtouch_handle_msg(ts, obj, msg);
1475         if (ret != 0) {
1476                 pr_err("%s: Unable to process message for obj %d, "
1477                        "report_id %d\n", __func__, obj->entry.type,
1478                        msg->report_id);
1479                 goto done;
1480         }
1481
1482 done:
1483         if (qtouch_disable_touch)
1484                 pr_err("%s: Not enabling touch\n", __func__);
1485         else
1486                 enable_irq(ts->client->irq);
1487 }
1488
1489 static int qtouch_set_boot_mode(struct qtouch_ts_data *ts)
1490 {
1491         unsigned char FWupdateInfo[3];
1492         int err;
1493         int try_again = 0;
1494
1495         err = qtouch_read(ts, FWupdateInfo, 3);
1496         if (err)
1497                 pr_err("%s: Could not read back data\n", __func__);
1498
1499         while ((FWupdateInfo[0] & QTM_OBP_BOOT_CMD_MASK) != QTM_OBP_BOOT_WAIT_FOR_DATA) {
1500                 err = qtouch_read(ts, FWupdateInfo, 3);
1501                 if (err)
1502                         pr_err("%s: Could not read back data\n", __func__);
1503
1504                 if ((FWupdateInfo[0] & QTM_OBP_BOOT_CMD_MASK) == QTM_OBP_BOOT_WAIT_ON_BOOT_CMD) {
1505                         FWupdateInfo[0] = 0xDC;
1506                         FWupdateInfo[1] = 0xAA;
1507                         err = qtouch_write(ts, FWupdateInfo, 2);
1508                         if (err != 2) {
1509                                 pr_err("%s: Could not write to BL %i\n",
1510                                        __func__, err);
1511                                 return -EIO;
1512                         }
1513                 } else if (try_again > 10) {
1514                                 pr_err("%s: Cannot get into bootloader mode\n",
1515                                         __func__);
1516                         return -ENODEV;
1517                 } else {
1518                         try_again++;
1519                         msleep(QTM_OBP_SLEEP_WAIT_FOR_BOOT);
1520                 }
1521         }
1522
1523         return err;
1524 }
1525
1526 static ssize_t qtouch_irq_status(struct device *dev,
1527                                  struct device_attribute *attr, char *buf)
1528 {
1529         struct i2c_client *client = container_of(dev,
1530                                                  struct i2c_client, dev);
1531         struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1532         return sprintf(buf, "%u\n", atomic_read(&ts->irq_enabled));
1533 }
1534
1535 static ssize_t qtouch_irq_enable(struct device *dev,
1536                                  struct device_attribute *attr,
1537                                  const char *buf, size_t size)
1538 {
1539         struct i2c_client *client = container_of(dev,
1540                                                  struct i2c_client, dev);
1541         struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1542         int err = 0;
1543         unsigned long value;
1544         struct qtm_obj_message *msg;
1545
1546         if (size > 2)
1547                 return -EINVAL;
1548
1549         err = strict_strtoul(buf, 10, &value);
1550         if (err != 0)
1551                 return err;
1552
1553         switch (value) {
1554         case 0:
1555                 if (atomic_cmpxchg(&ts->irq_enabled, 1, 0)) {
1556                         pr_info("touch irq disabled!\n");
1557                         disable_irq_nosync(ts->client->irq);
1558                 }
1559                 err = size;
1560                 break;
1561         case 1:
1562                 if (!atomic_cmpxchg(&ts->irq_enabled, 0, 1)) {
1563                         pr_info("touch irq enabled!\n");
1564                         msg = qtouch_read_msg(ts);
1565                         if (msg == NULL)
1566                                 pr_err("%s: Cannot read message\n", __func__);
1567                         enable_irq(ts->client->irq);
1568                 }
1569                 err = size;
1570                 break;
1571         default:
1572                 pr_info("qtouch_irq_enable failed -> irq_enabled = %d\n",
1573                 atomic_read(&ts->irq_enabled));
1574                 err = -EINVAL;
1575                 break;
1576         }
1577
1578         return err;
1579 }
1580
1581 static DEVICE_ATTR(irq_enable, 0644, qtouch_irq_status, qtouch_irq_enable);
1582
1583 static ssize_t qtouch_update_status(struct device *dev,
1584                                     struct device_attribute *attr, char *buf)
1585 {
1586         struct i2c_client *client = container_of(dev,
1587                                                  struct i2c_client, dev);
1588         struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1589
1590         return sprintf(buf, "%u\n", ts->status);
1591 }
1592
1593 static DEVICE_ATTR(update_status, 0644, qtouch_update_status, NULL);
1594
1595 static ssize_t qtouch_fw_version(struct device *dev,
1596                                  struct device_attribute *attr, char *buf)
1597 {
1598         struct i2c_client *client = container_of(dev,
1599                                                  struct i2c_client, dev);
1600         struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1601
1602         return sprintf(buf, "0x%X%X\n", ts->fw_version, ts->build_version);
1603 }
1604
1605 static DEVICE_ATTR(fw_version, 0644, qtouch_fw_version, NULL);
1606
1607 static int qtouch_ts_probe(struct i2c_client *client,
1608                            const struct i2c_device_id *id)
1609 {
1610         struct qtouch_ts_platform_data *pdata = client->dev.platform_data;
1611         struct qtouch_ts_data *ts;
1612         int err;
1613         unsigned char boot_info;
1614         int loop_count;
1615
1616         if (pdata == NULL) {
1617                 pr_err("%s: platform data required\n", __func__);
1618                 return -ENODEV;
1619         } else if (!client->irq) {
1620                 pr_err("%s: polling mode currently not supported\n", __func__);
1621                 return -ENODEV;
1622         } else if (!pdata->hw_reset) {
1623                 pr_err("%s: Must supply a hw reset function\n", __func__);
1624                 return -ENODEV;
1625         }
1626
1627         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1628                 pr_err("%s: need I2C_FUNC_I2C\n", __func__);
1629                 return -ENODEV;
1630         }
1631
1632         ts = kzalloc(sizeof(struct qtouch_ts_data), GFP_KERNEL);
1633         if (ts == NULL) {
1634                 err = -ENOMEM;
1635                 goto err_alloc_data_failed;
1636         }
1637         ts->pdata = pdata;
1638         ts->client = client;
1639         i2c_set_clientdata(client, ts);
1640         ts->checksum_cnt = 0;
1641         ts->fw_version = 0;
1642         ts->build_version = 0;
1643         ts->fw_error_count = 0;
1644         ts->current_pkt_sz = 0;
1645         ts->x_delta = ts->pdata->x_delta;
1646         ts->y_delta = ts->pdata->y_delta;
1647         atomic_set(&ts->irq_enabled, 1);
1648         atomic_set(&ts->process_open, 1);
1649         ts->enable_irq_flag = 1;
1650         ts->status = 0xfe;
1651         ts->touch_fw_size = 0;
1652         ts->touch_fw_image = NULL;
1653         ts->touch_fw = NULL;
1654         ts->base_fw_version = 0;
1655
1656         ts->xpos_rshift_lsb = 6;
1657         ts->xpos_lshift_msb = 2;
1658         ts->ypos_rshift_lsb = 2;
1659         ts->ypos_lshift_msb = 2;
1660
1661         if (ts->pdata->multi_touch_cfg.x_res > 1023) {
1662                 ts->xpos_rshift_lsb = 4;
1663                 ts->xpos_lshift_msb = 4;
1664         }
1665         if (ts->pdata->multi_touch_cfg.y_res > 1023) {
1666                 ts->ypos_rshift_lsb = 0;
1667                 ts->ypos_lshift_msb = 4;
1668         }
1669
1670         pr_info("%s: xpos_msb %d xpos_lsb %d ypos_msb %d ypos_lsb %d\n", __func__,
1671                         ts->xpos_lshift_msb, ts->xpos_rshift_lsb,
1672                         ts->ypos_lshift_msb, ts->ypos_rshift_lsb);
1673
1674         qtouch_force_reset(ts, 0);
1675         msleep(QTM_OBP_SLEEP_WAIT_FOR_HW_RESET);
1676         err = qtouch_process_info_block(ts);
1677
1678         if (err == 0) {
1679                 pr_info("%s: FW version is 0x%X Build 0x%X\n", __func__,
1680                            ts->fw_version, ts->build_version);
1681
1682                 if ((ts->family_id == ts->pdata->touch_fw_cfg.family_id)
1683                     && (ts->variant_id == ts->pdata->touch_fw_cfg.variant_id)) {
1684                         pr_info("%s: Chip type matched\n", __func__);
1685
1686                         if ((ts->fw_version != ts->pdata->touch_fw_cfg.fw_version)
1687                             || (ts->build_version != ts->pdata->touch_fw_cfg.fw_build)) {
1688                                 pr_info("%s: Reflash needed\n", __func__);
1689                                 ts->touch_fw_image = ts->pdata->touch_fw_cfg.fw_name;
1690                                 ts->base_fw_version = ts->pdata->touch_fw_cfg.base_fw_version;
1691                         } else {
1692                                 pr_info("%s: Reflash not needed\n", __func__);
1693                         }
1694                 }
1695
1696                 if (ts->touch_fw_image != NULL) {
1697                         /* Reset the chip into bootloader mode */
1698                         if (ts->fw_version >= ts->base_fw_version) {
1699                                 qtouch_force_reset(ts, 2);
1700                                 msleep(QTM_OBP_SLEEP_WAIT_FOR_HW_RESET);
1701
1702                                 ts->org_i2c_addr = ts->client->addr;
1703                                 ts->client->addr = ts->pdata->boot_i2c_addr;
1704                         } else {
1705                                 pr_err("%s:FW 0x%X does not support boot mode\n",
1706                                        __func__, ts->fw_version);
1707                                 ts->touch_fw_image = NULL;
1708                         }
1709                 }
1710         } else {
1711                 pr_info("%s:Cannot read info block %i, checking for bootloader mode.\n", __func__, err);
1712
1713                 qtouch_force_reset(ts, 0);
1714                 msleep(QTM_OBP_SLEEP_WAIT_FOR_HW_RESET);
1715
1716                 ts->org_i2c_addr = ts->client->addr;
1717                 ts->client->addr = ts->pdata->boot_i2c_addr;
1718
1719                 err = qtouch_read(ts, &boot_info, 1);
1720                 if (err) {
1721                         pr_err("%s:Read failed %d\n", __func__, err);
1722                 } else {
1723                         pr_info("%s:Data read 0x%x\n", __func__, boot_info);
1724                         loop_count = 0;
1725                         while ((boot_info & QTM_OBP_BOOT_CMD_MASK) != QTM_OBP_BOOT_WAIT_ON_BOOT_CMD) {
1726                                 err = qtouch_read(ts, &boot_info, 1);
1727                                 if (err) {
1728                                         pr_err("%s:Read failed %d\n", __func__, err);
1729                                         break;
1730                                 }
1731                                 pr_info("%s:Data read 0x%x\n", __func__, boot_info);
1732                                 loop_count++;
1733                                 if (loop_count == 10) {
1734                                         err = 1;
1735                                         break;
1736                                 }
1737                         }
1738                 }
1739                 if (!err) {
1740                         boot_info &= QTM_OBP_BOOT_VERSION_MASK;
1741                         pr_info("%s:Bootloader version %d\n", __func__, boot_info);
1742
1743                         if (boot_info == ts->pdata->touch_fw_cfg.boot_version) {
1744                                 pr_info("%s: Chip type matched\n", __func__);
1745                                 ts->touch_fw_image = ts->pdata->touch_fw_cfg.fw_name;
1746                                 ts->base_fw_version = ts->pdata->touch_fw_cfg.base_fw_version;
1747                         }
1748                 }
1749         }
1750
1751         INIT_WORK(&ts->work, qtouch_ts_work_func);
1752         INIT_WORK(&ts->boot_work, qtouch_ts_boot_work_func);
1753
1754         if (ts->touch_fw_image != NULL) {
1755                 err = qtouch_set_boot_mode(ts);
1756                 if (err < 0) {
1757                         pr_err("%s: Failed setting IC in boot mode %i\n",
1758                                __func__, err);
1759                         /* We must have been in boot mode to begin with
1760                         or the IC is not present so just exit out of probe */
1761                         if (ts->fw_version == 0) {
1762                                 ts->status = 0xfd;
1763                                 return err;
1764                         }
1765
1766                         ts->client->addr = ts->org_i2c_addr;
1767                         qtouch_force_reset(ts, 0);
1768                         msleep(QTM_OBP_SLEEP_WAIT_FOR_HW_RESET);
1769                         pr_err("%s: I2C address is 0x%X\n",
1770                                 __func__, ts->client->addr);
1771                         err = qtouch_process_info_block(ts);
1772                         if (err) {
1773                                 pr_err("%s: Failed reading info block %i\n",
1774                                        __func__, err);
1775                                 goto err_reading_info_block;
1776                         }
1777                         goto err_boot_mode_failure;
1778                 }
1779
1780                 ts->mode = 1;
1781                 goto finish_touch_setup;
1782
1783         }
1784
1785 /* If the update should fail the touch should still work */
1786 err_boot_mode_failure:
1787         ts->mode = 0;
1788         err = qtouch_ts_prep_msg_proc(ts);
1789         if (err != 0) {
1790                 pr_err("%s: setting message proc failed %i\n", __func__, err);
1791                 goto err_set_msg_proc;
1792         }
1793
1794 finish_touch_setup:
1795         err = qtouch_ts_register_input(ts);
1796         if (err != 0) {
1797                 pr_err("%s: Registering input failed %i\n", __func__, err);
1798                 goto err_input_register_dev;
1799         }
1800
1801         err = request_irq(ts->client->irq, qtouch_ts_irq_handler,
1802                           IRQ_DISABLED | pdata->irqflags, "qtouch_ts_int", ts);
1803         if (err != 0) {
1804                 pr_err("%s: request_irq (%d) failed\n", __func__,
1805                        ts->client->irq);
1806                 goto err_request_irq;
1807         }
1808         pr_info("%s: request_irq [%d] success.\n", __func__,
1809                        ts->client->irq);
1810
1811         err = device_create_file(&ts->client->dev, &dev_attr_irq_enable);
1812         if (err != 0) {
1813                 pr_err("%s:File device creation failed: %d\n", __func__, err);
1814                 err = -ENODEV;
1815                 goto err_create_file_failed;
1816         }
1817
1818         err = device_create_file(&ts->client->dev, &dev_attr_update_status);
1819         if (err != 0) {
1820                 pr_err("%s:File device creation failed: %d\n", __func__, err);
1821                 err = -ENODEV;
1822                 goto err_create_update_status_failed;
1823         }
1824
1825         err = device_create_file(&ts->client->dev, &dev_attr_fw_version);
1826         if (err != 0) {
1827                 pr_err("%s:File device creation failed: %d\n", __func__, err);
1828                 err = -ENODEV;
1829                 goto err_create_fw_version_file_failed;
1830         }
1831
1832         ts->regulator = regulator_get(&ts->client->dev, "vio");
1833         if (!IS_ERR_OR_NULL(ts->regulator))
1834                 regulator_enable(ts->regulator);
1835
1836 #ifdef CONFIG_HAS_EARLYSUSPEND
1837         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB - 1;
1838         ts->early_suspend.suspend = qtouch_ts_early_suspend;
1839         ts->early_suspend.resume = qtouch_ts_late_resume;
1840         register_early_suspend(&ts->early_suspend);
1841 #endif
1842
1843         return 0;
1844
1845 err_create_fw_version_file_failed:
1846         device_remove_file(&ts->client->dev, &dev_attr_update_status);
1847 err_create_update_status_failed:
1848         device_remove_file(&ts->client->dev, &dev_attr_irq_enable);
1849 err_create_file_failed:
1850         free_irq(ts->client->irq, ts);
1851 err_request_irq:
1852         qtouch_ts_unregister_input(ts);
1853
1854 err_set_msg_proc:
1855 err_input_register_dev:
1856 err_reading_info_block:
1857         i2c_set_clientdata(client, NULL);
1858         kfree(ts);
1859
1860 err_alloc_data_failed:
1861         return err;
1862 }
1863
1864 static int qtouch_ts_remove(struct i2c_client *client)
1865 {
1866         struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1867
1868         if (!IS_ERR_OR_NULL(ts->regulator))
1869                 regulator_put(ts->regulator);
1870
1871         device_remove_file(&ts->client->dev, &dev_attr_irq_enable);
1872         device_remove_file(&ts->client->dev, &dev_attr_update_status);
1873         device_remove_file(&ts->client->dev, &dev_attr_fw_version);
1874
1875         unregister_early_suspend(&ts->early_suspend);
1876         free_irq(ts->client->irq, ts);
1877         qtouch_ts_unregister_input(ts);
1878         i2c_set_clientdata(client, NULL);
1879         kfree(ts);
1880         return 0;
1881 }
1882
1883 static int qtouch_ts_suspend(struct i2c_client *client, pm_message_t mesg)
1884 {
1885         struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1886         int ret;
1887         if (qtouch_tsdebug & 4)
1888                 pr_info("%s: Suspending\n", __func__);
1889
1890         if (!atomic_read(&ts->irq_enabled))
1891                 return 0;
1892
1893         if (ts->mode == 1)
1894                 return -EBUSY;
1895
1896         if (ts->enable_irq_flag)
1897                 disable_irq_nosync(ts->client->irq);
1898         ret = cancel_work_sync(&ts->work);
1899         if (ret) { /* if work was pending disable-count is now 2 */
1900                 pr_info("%s: Pending work item\n", __func__);
1901                 enable_irq(ts->client->irq);
1902         }
1903
1904         ret = qtouch_power_config(ts, 0);
1905         if (ret < 0)
1906                 pr_err("%s: Cannot write power config\n", __func__);
1907
1908         if (ts->pdata->hw_suspend)
1909                 ts->pdata->hw_suspend(1);
1910
1911         if (!IS_ERR_OR_NULL(ts->regulator))
1912                 regulator_disable(ts->regulator);
1913
1914         return 0;
1915 }
1916
1917 static int qtouch_ts_resume(struct i2c_client *client)
1918 {
1919         struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1920         int ret;
1921         int i;
1922         struct qtm_object *obj;
1923
1924         if (qtouch_tsdebug & 4)
1925                 pr_info("%s: Resuming\n", __func__);
1926
1927         if (!atomic_read(&ts->irq_enabled))
1928                 return 0;
1929
1930         if (ts->mode == 1)
1931                 return -EBUSY;
1932
1933         if (!IS_ERR_OR_NULL(ts->regulator))
1934                 regulator_enable(ts->regulator);
1935
1936         if (ts->pdata->hw_suspend)
1937                 ts->pdata->hw_suspend(0);
1938
1939         /* If we were suspended while a touch was happening
1940            we need to tell the upper layers so they do not hang
1941            waiting on the liftoff that will not come. */
1942         for (i = 0; i < ts->pdata->multi_touch_cfg.num_touch; i++) {
1943                 if (qtouch_tsdebug & 4)
1944                         pr_info("%s: Finger %i down state %i\n",
1945                                 __func__, i, ts->finger_data[i].down);
1946                 if (ts->finger_data[i].down == 0)
1947                         continue;
1948                 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0);
1949                 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, i);
1950                 input_mt_sync(ts->input_dev);
1951                 memset(&ts->finger_data[i], 0, sizeof(struct coordinate_map));
1952         }
1953         input_sync(ts->input_dev);
1954
1955         ret = qtouch_power_config(ts, 1);
1956         if (ret < 0) {
1957                 pr_err("%s: Cannot write power config\n", __func__);
1958                 ts->enable_irq_flag = 0;
1959                 return -EIO;
1960         }
1961         ret = qtouch_force_calibration(ts);
1962         if (ret != 0) {
1963                 pr_err("%s: Unable to recalibrate after power config\n", __func__);
1964                 return ret;
1965         }
1966
1967         /* Point the address pointer to the message processor.
1968          * Must do this before enabling interrupts */
1969         obj = find_obj(ts, QTM_OBJ_GEN_MSG_PROC);
1970         ret = qtouch_set_addr(ts, obj->entry.addr);
1971         if (ret != 0) {
1972                 pr_err("%s: Can't to set addr to msg processor\n", __func__);
1973                 ts->enable_irq_flag = 0;
1974                 return -EIO;
1975         }
1976
1977         enable_irq(ts->client->irq);
1978         ts->enable_irq_flag = 1;
1979         return 0;
1980 }
1981
1982 #ifdef CONFIG_HAS_EARLYSUSPEND
1983 static void qtouch_ts_early_suspend(struct early_suspend *handler)
1984 {
1985         struct qtouch_ts_data *ts;
1986
1987         ts = container_of(handler, struct qtouch_ts_data, early_suspend);
1988         qtouch_ts_suspend(ts->client, PMSG_SUSPEND);
1989 }
1990
1991 static void qtouch_ts_late_resume(struct early_suspend *handler)
1992 {
1993         struct qtouch_ts_data *ts;
1994
1995         ts = container_of(handler, struct qtouch_ts_data, early_suspend);
1996         qtouch_ts_resume(ts->client);
1997 }
1998 #endif
1999
2000 /******** init ********/
2001 static const struct i2c_device_id qtouch_ts_id[] = {
2002         { QTOUCH_TS_NAME, 0 },
2003         { }
2004 };
2005
2006 static struct i2c_driver qtouch_ts_driver = {
2007         .probe          = qtouch_ts_probe,
2008         .remove         = qtouch_ts_remove,
2009 #ifndef CONFIG_HAS_EARLYSUSPEND
2010         .suspend        = qtouch_ts_suspend,
2011         .resume         = qtouch_ts_resume,
2012 #endif
2013         .id_table       = qtouch_ts_id,
2014         .driver = {
2015                 .name   = QTOUCH_TS_NAME,
2016                 .owner  = THIS_MODULE,
2017         },
2018 };
2019
2020 static int __devinit qtouch_ts_init(void)
2021 {
2022         qtouch_ts_wq = create_singlethread_workqueue("qtouch_obp_ts_wq");
2023         if (qtouch_ts_wq == NULL) {
2024                 pr_err("%s: No memory for qtouch_ts_wq\n", __func__);
2025                 return -ENOMEM;
2026         }
2027         return i2c_add_driver(&qtouch_ts_driver);
2028 }
2029
2030 static void __exit qtouch_ts_exit(void)
2031 {
2032         i2c_del_driver(&qtouch_ts_driver);
2033         if (qtouch_ts_wq)
2034                 destroy_workqueue(qtouch_ts_wq);
2035 }
2036
2037 module_init(qtouch_ts_init);
2038 module_exit(qtouch_ts_exit);
2039
2040 MODULE_AUTHOR("Dima Zavin <dima@android.com>");
2041 MODULE_DESCRIPTION("Quantum OBP Touchscreen Driver");
2042 MODULE_LICENSE("GPL");