2 * drivers/input/touchscreen/qtouch_obp_ts.c - driver for Quantum touch IC
4 * Copyright (C) 2009 Google, Inc.
5 * Copyright (C) 2009-2010 Motorola, Inc.
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.
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.
16 * Derived from the Motorola OBP touch driver.
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>
34 #define IGNORE_CHECKSUM_MISMATCH
37 struct qtm_obj_entry entry;
38 uint8_t report_id_min;
39 uint8_t report_id_max;
48 struct coordinate_map {
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;
69 struct qtm_object obj_tbl[QTM_OBP_MAX_OBJECT_NUM];
70 unsigned long obj_map[_BITMAP_LEN];
72 uint32_t last_keystate;
73 uint32_t eeprom_checksum;
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;
87 uint8_t xpos_rshift_lsb;
88 uint8_t ypos_rshift_lsb;
89 uint8_t xpos_lshift_msb;
90 uint8_t ypos_lshift_msb;
93 atomic_t process_open;
100 uint8_t org_i2c_addr;
102 /* Note: The message buffer is reused for reading different messages.
103 * MUST enforce that there is no concurrent access to msg_buf. */
107 struct regulator *regulator;
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);
115 static struct workqueue_struct *qtouch_ts_wq;
116 const struct firmware *fw_entry;
118 static uint32_t qtouch_tsdebug;
119 module_param_named(tsdebug, qtouch_tsdebug, uint, 0664);
121 static uint32_t qtouch_disable_touch;
122 module_param_named(disable_touch, qtouch_disable_touch, uint, 0644);
124 static irqreturn_t qtouch_ts_irq_handler(int irq, void *dev_id)
126 struct qtouch_ts_data *ts = dev_id;
128 disable_irq_nosync(ts->client->irq);
130 queue_work(qtouch_ts_wq, &ts->boot_work);
132 queue_work(qtouch_ts_wq, &ts->work);
137 static int qtouch_write(struct qtouch_ts_data *ts, void *buf, int buf_sz)
143 ret = i2c_master_send(ts->client, (char *)buf, buf_sz);
144 } while ((ret < buf_sz) && (--retries > 0));
147 pr_info("%s: Error while trying to write %d bytes\n", __func__,
149 else if (ret != buf_sz) {
150 pr_info("%s: Write %d bytes, expected %d\n", __func__,
157 static int qtouch_set_addr(struct qtouch_ts_data *ts, uint16_t addr)
161 /* Note: addr on the wire is LSB first */
162 ret = qtouch_write(ts, (char *)&addr, sizeof(uint16_t));
164 pr_info("%s: Can't send obp addr 0x%4x\n", __func__, addr);
166 return ret >= 0 ? 0 : ret;
169 static int qtouch_read(struct qtouch_ts_data *ts, void *buf, int buf_sz)
175 memset(buf, 0, buf_sz);
176 ret = i2c_master_recv(ts->client, (char *)buf, buf_sz);
177 } while ((ret < 0) && (--retries > 0));
180 pr_info("%s: Error while trying to read %d bytes\n", __func__,
182 else if (ret != buf_sz) {
183 pr_info("%s: Read %d bytes, expected %d\n", __func__,
188 return ret >= 0 ? 0 : ret;
191 static int qtouch_read_addr(struct qtouch_ts_data *ts, uint16_t addr,
192 void *buf, int buf_sz)
196 ret = qtouch_set_addr(ts, addr);
200 return qtouch_read(ts, buf, buf_sz);
203 static struct qtm_obj_message *qtouch_read_msg(struct qtouch_ts_data *ts)
207 ret = qtouch_read(ts, ts->msg_buf, ts->msg_size);
209 return (struct qtm_obj_message *)ts->msg_buf;
213 static int qtouch_write_addr(struct qtouch_ts_data *ts, uint16_t addr,
214 void *buf, int buf_sz)
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",
226 memcpy(write_buf, (void *)&addr, sizeof(addr));
227 memcpy((void *)write_buf + sizeof(addr), buf, buf_sz);
229 ret = qtouch_write(ts, write_buf, buf_sz + sizeof(addr));
234 pr_err("%s: Could not write %d bytes.\n", __func__, buf_sz);
240 static uint32_t crc24(uint32_t crc, uint8_t first_byte, uint8_t sec_byte)
242 static const uint32_t crcpoly = 0x80001b;
244 uint16_t data_word = 0;
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)
255 static uint32_t calc_csum(uint32_t curr_sum, void *_buf, int buf_sz)
266 curr_sum = crc24(curr_sum, *(buf + i), *(buf + i + 1));
270 curr_sum = crc24(curr_sum, *(buf + i), 0);
272 curr_sum = (curr_sum & 0x00FFFFFF);
277 static inline struct qtm_object *find_obj(struct qtouch_ts_data *ts, int id)
279 return &ts->obj_tbl[id];
282 static struct qtm_object *create_obj(struct qtouch_ts_data *ts,
283 struct qtm_obj_entry *entry)
285 struct qtm_object *obj;
287 obj = &ts->obj_tbl[entry->type];
288 memcpy(&obj->entry, entry, sizeof(*entry));
289 set_bit(entry->type, ts->obj_map);
294 static struct qtm_object *find_object_rid(struct qtouch_ts_data *ts, int rid)
298 for_each_set_bit(i, ts->obj_map, QTM_OBP_MAX_OBJECT_NUM) {
299 struct qtm_object *obj = &ts->obj_tbl[i];
301 if ((rid >= obj->report_id_min) && (rid <= obj->report_id_max))
308 static void qtouch_force_reset(struct qtouch_ts_data *ts, uint8_t sw_reset)
310 struct qtm_object *obj;
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);
322 obj->entry.addr + offsetof(struct qtm_gen_cmd_proc, reset);
323 /* Check to see if to reset into boot mode */
326 ret = qtouch_write_addr(ts, addr, &val, 1);
328 pr_err("%s: Unable to send the reset msg\n", __func__);
332 static int qtouch_force_calibration(struct qtouch_ts_data *ts)
334 struct qtm_object *obj;
339 pr_info("%s: Forcing calibration\n", __func__);
341 obj = find_obj(ts, QTM_OBJ_GEN_CMD_PROC);
343 addr = obj->entry.addr + offsetof(struct qtm_gen_cmd_proc, calibrate);
345 ret = qtouch_write_addr(ts, addr, &val, 1);
347 pr_err("%s: Unable to send the calibrate message\n", __func__);
352 #define min(a, b) (((a) < (b)) ? (a) : (b))
353 static int qtouch_power_config(struct qtouch_ts_data *ts, int on)
355 struct qtm_gen_power_cfg pwr_cfg;
356 struct qtm_object *obj;
359 /* go to standby mode */
360 pwr_cfg.idle_acq_int = 0;
361 pwr_cfg.active_acq_int = 0;
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;
367 pwr_cfg.active_idle_to = ts->pdata->power_cfg.active_idle_to;
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));
374 /* Apply the configuration provided in the platform_data to the hardware */
375 static int qtouch_hw_init(struct qtouch_ts_data *ts)
377 struct qtm_object *obj;
382 pr_info("%s: Doing hw init\n", __func__);
384 /* take the IC out of suspend */
385 qtouch_power_config(ts, 1);
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),
393 pr_err("%s: Can't write acquisition config\n", __func__);
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 */
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 */
410 ret = qtouch_write_addr(ts, obj->entry.addr, &cfg,
411 min(sizeof(cfg), obj->entry.size));
413 pr_err("%s: Can't write multi-touch config\n",
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],
428 memset(&cfg, 0, sizeof(cfg));
430 adj_addr = obj->entry.addr +
431 ((obj->entry.size + 1) * i);
432 ret = qtouch_write_addr(ts, adj_addr, &cfg,
436 pr_err("%s: Can't write keyarray config\n",
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),
451 pr_err("%s: Can't write signal filter config\n",
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),
465 pr_err("%s: Can't write linear table config\n",
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),
479 pr_err("%s: Can't write the comms configuration config\n",
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),
493 pr_err("%s: Can't write the GPIO PWM config\n",
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,
505 (ts->pdata->grip_face_suppression_cfg),
508 pr_err("%s: Can't write the grip face suppression config\n",
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),
522 pr_err("%s: Can't write the noise suppression config\n",
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),
536 pr_err("%s: Can't write the touch proximity config\n",
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),
550 pr_err("%s: Can't write the one touch gesture processor config\n",
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),
564 pr_err("%s: Can't write the self test config\n",
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),
578 pr_err("%s: Can't write the two touch gesture processor config\n",
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),
592 pr_err("%s: Can't write the capacitive touch engine config\n",
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,
604 (ts->pdata->noise1_suppression_cfg),
607 pr_err("%s: Can't write the noise suppression config\n",
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),
621 pr_err("%s: Can't write the grip suppression config\n",
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),
635 pr_err("%s: Can't write the palm suppression config\n",
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),
649 pr_err("%s: Can't write the Digitizer HID config\n",
655 ret = qtouch_force_calibration(ts);
657 pr_err("%s: Unable to recalibrate after reset\n", __func__);
661 /* Write the settings into nvram, if needed */
662 if (ts->pdata->flags & QTOUCH_CFG_BACKUPNV) {
666 obj = find_obj(ts, QTM_OBJ_GEN_CMD_PROC);
667 addr = obj->entry.addr + offsetof(struct qtm_gen_cmd_proc,
670 ret = qtouch_write_addr(ts, addr, &val, 1);
672 pr_err("%s: Can't backup nvram settings\n", __func__);
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 */
679 msleep(QTM_OBP_SLEEP_WAIT_FOR_BACKUP);
682 /* If debugging, read back and print all settings */
683 if (qtouch_tsdebug) {
692 msg = kmalloc(1024, GFP_KERNEL);
694 for (object = 7; object < QTM_OBP_MAX_OBJECT_NUM; object++) {
696 size = ts->obj_tbl[object].entry.size
697 * ts->obj_tbl[object].entry.num_inst;
699 data_buff = kmalloc(size, GFP_KERNEL);
700 if (data_buff == NULL) {
701 pr_err("%s: Object %d: Malloc failed\n",
707 ts->obj_tbl[object].entry.addr,
708 (void *)data_buff, size);
710 msg_location = sprintf(msg, "%s: Object %d:",
712 for (byte = 0; byte < size; byte++) {
713 msg_bytes = snprintf((msg + msg_location),
714 (1024 - msg_location),
716 *(data_buff + byte));
717 msg_location += msg_bytes;
718 if (msg_location >= 1024)
721 if (msg_location < 1024) {
722 pr_info("%s\n", msg);
724 pr_info("%s: Object %d: String overflow\n",
736 /* reset the address pointer */
737 ret = qtouch_set_addr(ts, ts->obj_tbl[QTM_OBJ_GEN_MSG_PROC].entry.addr);
739 pr_err("%s: Unable to reset address pointer after reset\n",
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,
751 struct qtm_cmd_proc_msg *msg = _msg;
754 uint32_t checksum = (msg->checksum[2] << 16)
755 | (msg->checksum[1] << 8) | msg->checksum[0];
757 if (msg->status & QTM_CMD_PROC_STATUS_RESET) {
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;
768 pr_info("%s:EEPROM checksum doesn't match 0x%x\n",
770 ret = qtouch_hw_init(ts);
772 pr_err("%s:Cannot init the touch IC\n",
778 pr_info("%s:EEPROM checksum matches\n", __func__);
780 pr_info("%s: Reset done.\n", __func__);
783 if (msg->status & QTM_CMD_PROC_STATUS_CAL)
784 pr_info("%s: Self-calibration started.\n", __func__);
786 if (msg->status & QTM_CMD_PROC_STATUS_OFL)
787 pr_err("%s: Acquisition cycle length overflow\n", __func__);
789 if (msg->status & QTM_CMD_PROC_STATUS_SIGERR)
790 pr_err("%s: Acquisition error\n", __func__);
792 if (msg->status & QTM_CMD_PROC_STATUS_CFGERR) {
793 pr_err("%s: Configuration error\n", __func__);
794 ret = qtouch_hw_init(ts);
796 pr_err("%s:Cannot init the touch IC\n", __func__);
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) {
804 pr_info("%s:EEPROM checksum is 0x%X cnt %i\n",
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;
814 ret = qtouch_hw_init(ts);
816 pr_err("%s:Cannot init the touch IC\n",
818 qtouch_force_reset(ts, 0);
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,
831 struct qtm_touch_multi_msg *msg = _msg;
840 finger = msg->report_id - obj->report_id_min;
841 if (finger >= ts->pdata->multi_touch_cfg.num_touch)
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);
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);
855 width = msg->touch_area;
856 pressure = msg->touch_amp;
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);
862 if (finger >= _NUM_FINGERS) {
863 pr_err("%s: Invalid finger number %dd\n", __func__, finger);
867 down = !(msg->status & (QTM_TOUCH_MULTI_STATUS_RELEASE |
868 QTM_TOUCH_MULTI_STATUS_SUPPRESS));
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;
875 /* The touch IC will not give back a pressure of zero
876 so send a 0 when a liftoff is produced */
878 ts->finger_data[finger].z_data = 0;
880 ts->finger_data[finger].z_data = pressure;
881 ts->finger_data[finger].down = down;
884 for (i = 0; i < ts->pdata->multi_touch_cfg.num_touch; i++) {
885 if (ts->finger_data[i].down == 0)
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,
899 input_mt_sync(ts->input_dev);
901 input_sync(ts->input_dev);
904 memset(&ts->finger_data[finger], 0,
905 sizeof(struct coordinate_map));
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)
915 struct qtm_touch_keyarray_msg *msg = _msg;
918 /* nothing changed.. odd. */
919 if (ts->last_keystate == msg->keystate)
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);
929 input_sync(ts->input_dev);
931 if (qtouch_tsdebug & 2)
932 pr_info("%s: key state changed 0x%08x -> 0x%08x\n", __func__,
933 ts->last_keystate, msg->keystate);
935 /* update our internal state */
936 ts->last_keystate = msg->keystate;
941 static int qtouch_handle_msg(struct qtouch_ts_data *ts, struct qtm_object *obj,
942 struct qtm_obj_message *msg)
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);
952 case QTM_OBJ_TOUCH_MULTI:
953 ret = do_touch_multi_msg(ts, obj, msg);
956 case QTM_OBJ_TOUCH_KEYARRAY:
957 ret = do_touch_keyarray_msg(ts, obj, msg);
961 /* probably not fatal? */
963 pr_info("%s: No handler defined for message from object "
964 "type %d, report_id %d\n", __func__, obj->entry.type,
971 static int qtouch_ts_prep_msg_proc(struct qtouch_ts_data *ts)
973 struct qtm_object *obj;
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__);
980 goto err_alloc_msg_buf;
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);
988 pr_err("%s: Can't to set addr to msg processor\n", __func__);
989 goto err_rst_addr_msg_proc;
994 err_rst_addr_msg_proc:
1002 int qtouch_input_open(struct input_dev *input)
1005 struct qtouch_ts_data *ts = input_get_drvdata(input);
1007 if (!atomic_xchg(&ts->process_open, 0))
1010 if (ts->touch_fw_image == NULL)
1011 goto finish_touch_upgrade;
1013 err = request_firmware(&fw_entry, ts->pdata->touch_fw_cfg.fw_name,
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,
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;
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;
1034 goto finish_touch_upgrade;
1036 goto reset_to_cleanup;
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;
1045 release_firmware(fw_entry);
1048 qtouch_force_reset(ts, 0);
1049 finish_touch_upgrade:
1054 static int qtouch_ts_register_input(struct qtouch_ts_data *ts)
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__);
1068 ts->input_dev->name = "qtouch-touchscreen";
1069 input_set_drvdata(ts->input_dev, ts);
1071 set_bit(EV_SYN, ts->input_dev->evbit);
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);
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);
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);
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,
1124 input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID,
1125 0, ts->pdata->multi_touch_cfg.num_touch, 1, 0);
1128 memset(&ts->finger_data[0], 0,
1129 (sizeof(struct coordinate_map) *
1132 ts->input_dev->open = qtouch_input_open;
1134 err = input_register_device(ts->input_dev);
1136 pr_err("%s: Cannot register input device \"%s\"\n", __func__,
1137 ts->input_dev->name);
1138 goto err_input_register_dev;
1142 err_input_register_dev:
1143 input_free_device(ts->input_dev);
1144 ts->input_dev = NULL;
1149 static int qtouch_process_info_block(struct qtouch_ts_data *ts)
1151 struct qtm_id_info qtm_info;
1152 uint32_t our_csum = 0x0;
1153 uint32_t their_csum;
1158 uint8_t *info_blk_buf, *info_blk_start;
1159 uint16_t info_blk_size;
1160 struct qtm_obj_entry entry;
1162 /* query the device and get the info block. */
1163 err = qtouch_read_addr(ts, QTM_OBP_ID_INFO_ADDR, &qtm_info,
1166 pr_err("%s: Cannot read info object block\n", __func__);
1167 goto err_read_info_block;
1170 pr_info("%s: Build version is 0x%x\n", __func__, qtm_info.version);
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);
1177 goto err_no_objects;
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);
1186 goto err_no_objects;
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);
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;
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;
1210 pr_info("%s: Reading addr: %i\n", __func__, addr);
1211 err = qtouch_read_addr(ts, addr, &entry, sizeof(entry));
1213 pr_err("%s: Can't read object (%d) entry.\n",
1216 goto err_read_entry;
1219 memcpy(info_blk_buf, (void *)&entry, sizeof(entry));
1220 info_blk_buf += sizeof(entry);
1221 addr += sizeof(entry);
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);
1230 if (entry.type >= QTM_OBP_MAX_OBJECT_NUM) {
1231 pr_warning("%s: Unknown object type (%d) encountered\n",
1232 __func__, entry.type);
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;
1243 ts->msg_size = entry.size - 1;
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;
1256 if (!ts->msg_size) {
1257 pr_err("%s: Message processing object not found. Bailing.\n",
1260 goto err_no_msg_proc;
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__);
1272 goto err_missing_objs;
1275 err = qtouch_read_addr(ts, addr, &their_csum, sizeof(their_csum));
1277 pr_err("%s: Unable to read remote checksum\n", __func__);
1279 goto err_no_checksum;
1282 our_csum = calc_csum(our_csum, info_blk_start, info_blk_size);
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
1289 goto err_bad_checksum;
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);
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);
1309 #ifndef IGNORE_CHECKSUM_MISMATCH
1316 kfree(info_blk_start);
1318 err_read_info_block:
1322 static int qtouch_ts_unregister_input(struct qtouch_ts_data *ts)
1324 input_unregister_device(ts->input_dev);
1325 ts->input_dev = NULL;
1329 static void qtouch_ts_boot_work_func(struct work_struct *work)
1332 struct qtouch_ts_data *ts = container_of(work,
1333 struct qtouch_ts_data,
1335 unsigned char boot_msg[3];
1337 if (ts->status == 0xff) {
1338 pr_err("%s: Entered in Wrong Mode\n", __func__);
1339 goto touch_to_normal_mode;
1342 err = qtouch_read(ts, &boot_msg, sizeof(boot_msg));
1344 pr_err("%s: Cannot read message\n", __func__);
1347 if (qtouch_tsdebug & 8)
1348 pr_err("%s: Message is 0x%X err is %i\n",
1349 __func__, boot_msg[0], err);
1351 if (boot_msg[0] == QTM_OBP_BOOT_CRC_CHECK) {
1352 if (qtouch_tsdebug & 8)
1353 pr_err("%s: CRC Check\n", __func__);
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);
1360 if (ts->fw_error_count > 3) {
1361 pr_err("%s: Resetting the IC fw upgrade failed\n",
1363 goto reset_touch_ic;
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;
1373 ts->fw_error_count++;
1374 pr_err("%s: Frame CRC check failed %i times\n",
1375 __func__, ts->fw_error_count);
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__);
1383 (ts->boot_pkt_size * 100) / ts->touch_fw_size;
1385 ts->boot_pkt_size += ts->current_pkt_sz;
1386 ts->fw_error_count = 0;
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__);
1393 goto touch_to_normal_mode;
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);
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;
1407 if (qtouch_tsdebug & 8)
1408 pr_err("%s: Size of the next packet is %i\n",
1409 __func__, ts->current_pkt_sz);
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",
1417 goto reset_touch_ic;
1420 pr_err("%s: Message is 0x%X is not handled\n",
1421 __func__, boot_msg[0]);
1425 enable_irq(ts->client->irq);
1429 qtouch_force_reset(ts, 0);
1430 touch_to_normal_mode:
1432 release_firmware(fw_entry);
1433 ts->client->addr = ts->org_i2c_addr;
1435 /* Wait for the IC to recover */
1436 msleep(QTM_OBP_SLEEP_WAIT_FOR_RESET);
1437 err = qtouch_process_info_block(ts);
1439 pr_err("%s:Cannot read info block %i\n", __func__, err);
1442 err = qtouch_ts_prep_msg_proc(ts);
1444 pr_err("%s: setting message proc failed %i\n", __func__, err);
1448 enable_irq(ts->client->irq);
1453 static void qtouch_ts_work_func(struct work_struct *work)
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;
1461 msg = qtouch_read_msg(ts);
1463 pr_err("%s: Cannot read message\n", __func__);
1467 obj = find_object_rid(ts, msg->report_id);
1469 pr_err("%s: Unknown object for report_id %d\n", __func__,
1474 ret = qtouch_handle_msg(ts, obj, msg);
1476 pr_err("%s: Unable to process message for obj %d, "
1477 "report_id %d\n", __func__, obj->entry.type,
1483 if (qtouch_disable_touch)
1484 pr_err("%s: Not enabling touch\n", __func__);
1486 enable_irq(ts->client->irq);
1489 static int qtouch_set_boot_mode(struct qtouch_ts_data *ts)
1491 unsigned char FWupdateInfo[3];
1495 err = qtouch_read(ts, FWupdateInfo, 3);
1497 pr_err("%s: Could not read back data\n", __func__);
1499 while ((FWupdateInfo[0] & QTM_OBP_BOOT_CMD_MASK) != QTM_OBP_BOOT_WAIT_FOR_DATA) {
1500 err = qtouch_read(ts, FWupdateInfo, 3);
1502 pr_err("%s: Could not read back data\n", __func__);
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);
1509 pr_err("%s: Could not write to BL %i\n",
1513 } else if (try_again > 10) {
1514 pr_err("%s: Cannot get into bootloader mode\n",
1519 msleep(QTM_OBP_SLEEP_WAIT_FOR_BOOT);
1526 static ssize_t qtouch_irq_status(struct device *dev,
1527 struct device_attribute *attr, char *buf)
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));
1535 static ssize_t qtouch_irq_enable(struct device *dev,
1536 struct device_attribute *attr,
1537 const char *buf, size_t size)
1539 struct i2c_client *client = container_of(dev,
1540 struct i2c_client, dev);
1541 struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1543 unsigned long value;
1544 struct qtm_obj_message *msg;
1549 err = strict_strtoul(buf, 10, &value);
1555 if (atomic_cmpxchg(&ts->irq_enabled, 1, 0)) {
1556 pr_info("touch irq disabled!\n");
1557 disable_irq_nosync(ts->client->irq);
1562 if (!atomic_cmpxchg(&ts->irq_enabled, 0, 1)) {
1563 pr_info("touch irq enabled!\n");
1564 msg = qtouch_read_msg(ts);
1566 pr_err("%s: Cannot read message\n", __func__);
1567 enable_irq(ts->client->irq);
1572 pr_info("qtouch_irq_enable failed -> irq_enabled = %d\n",
1573 atomic_read(&ts->irq_enabled));
1581 static DEVICE_ATTR(irq_enable, 0644, qtouch_irq_status, qtouch_irq_enable);
1583 static ssize_t qtouch_update_status(struct device *dev,
1584 struct device_attribute *attr, char *buf)
1586 struct i2c_client *client = container_of(dev,
1587 struct i2c_client, dev);
1588 struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1590 return sprintf(buf, "%u\n", ts->status);
1593 static DEVICE_ATTR(update_status, 0644, qtouch_update_status, NULL);
1595 static ssize_t qtouch_fw_version(struct device *dev,
1596 struct device_attribute *attr, char *buf)
1598 struct i2c_client *client = container_of(dev,
1599 struct i2c_client, dev);
1600 struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1602 return sprintf(buf, "0x%X%X\n", ts->fw_version, ts->build_version);
1605 static DEVICE_ATTR(fw_version, 0644, qtouch_fw_version, NULL);
1607 static int qtouch_ts_probe(struct i2c_client *client,
1608 const struct i2c_device_id *id)
1610 struct qtouch_ts_platform_data *pdata = client->dev.platform_data;
1611 struct qtouch_ts_data *ts;
1613 unsigned char boot_info;
1616 if (pdata == NULL) {
1617 pr_err("%s: platform data required\n", __func__);
1619 } else if (!client->irq) {
1620 pr_err("%s: polling mode currently not supported\n", __func__);
1622 } else if (!pdata->hw_reset) {
1623 pr_err("%s: Must supply a hw reset function\n", __func__);
1627 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1628 pr_err("%s: need I2C_FUNC_I2C\n", __func__);
1632 ts = kzalloc(sizeof(struct qtouch_ts_data), GFP_KERNEL);
1635 goto err_alloc_data_failed;
1638 ts->client = client;
1639 i2c_set_clientdata(client, ts);
1640 ts->checksum_cnt = 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;
1651 ts->touch_fw_size = 0;
1652 ts->touch_fw_image = NULL;
1653 ts->touch_fw = NULL;
1654 ts->base_fw_version = 0;
1656 ts->xpos_rshift_lsb = 6;
1657 ts->xpos_lshift_msb = 2;
1658 ts->ypos_rshift_lsb = 2;
1659 ts->ypos_lshift_msb = 2;
1661 if (ts->pdata->multi_touch_cfg.x_res > 1023) {
1662 ts->xpos_rshift_lsb = 4;
1663 ts->xpos_lshift_msb = 4;
1665 if (ts->pdata->multi_touch_cfg.y_res > 1023) {
1666 ts->ypos_rshift_lsb = 0;
1667 ts->ypos_lshift_msb = 4;
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);
1674 qtouch_force_reset(ts, 0);
1675 msleep(QTM_OBP_SLEEP_WAIT_FOR_HW_RESET);
1676 err = qtouch_process_info_block(ts);
1679 pr_info("%s: FW version is 0x%X Build 0x%X\n", __func__,
1680 ts->fw_version, ts->build_version);
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__);
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;
1692 pr_info("%s: Reflash not needed\n", __func__);
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);
1702 ts->org_i2c_addr = ts->client->addr;
1703 ts->client->addr = ts->pdata->boot_i2c_addr;
1705 pr_err("%s:FW 0x%X does not support boot mode\n",
1706 __func__, ts->fw_version);
1707 ts->touch_fw_image = NULL;
1711 pr_info("%s:Cannot read info block %i, checking for bootloader mode.\n", __func__, err);
1713 qtouch_force_reset(ts, 0);
1714 msleep(QTM_OBP_SLEEP_WAIT_FOR_HW_RESET);
1716 ts->org_i2c_addr = ts->client->addr;
1717 ts->client->addr = ts->pdata->boot_i2c_addr;
1719 err = qtouch_read(ts, &boot_info, 1);
1721 pr_err("%s:Read failed %d\n", __func__, err);
1723 pr_info("%s:Data read 0x%x\n", __func__, boot_info);
1725 while ((boot_info & QTM_OBP_BOOT_CMD_MASK) != QTM_OBP_BOOT_WAIT_ON_BOOT_CMD) {
1726 err = qtouch_read(ts, &boot_info, 1);
1728 pr_err("%s:Read failed %d\n", __func__, err);
1731 pr_info("%s:Data read 0x%x\n", __func__, boot_info);
1733 if (loop_count == 10) {
1740 boot_info &= QTM_OBP_BOOT_VERSION_MASK;
1741 pr_info("%s:Bootloader version %d\n", __func__, boot_info);
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;
1751 INIT_WORK(&ts->work, qtouch_ts_work_func);
1752 INIT_WORK(&ts->boot_work, qtouch_ts_boot_work_func);
1754 if (ts->touch_fw_image != NULL) {
1755 err = qtouch_set_boot_mode(ts);
1757 pr_err("%s: Failed setting IC in boot mode %i\n",
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) {
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);
1773 pr_err("%s: Failed reading info block %i\n",
1775 goto err_reading_info_block;
1777 goto err_boot_mode_failure;
1781 goto finish_touch_setup;
1785 /* If the update should fail the touch should still work */
1786 err_boot_mode_failure:
1788 err = qtouch_ts_prep_msg_proc(ts);
1790 pr_err("%s: setting message proc failed %i\n", __func__, err);
1791 goto err_set_msg_proc;
1795 err = qtouch_ts_register_input(ts);
1797 pr_err("%s: Registering input failed %i\n", __func__, err);
1798 goto err_input_register_dev;
1801 err = request_irq(ts->client->irq, qtouch_ts_irq_handler,
1802 IRQ_DISABLED | pdata->irqflags, "qtouch_ts_int", ts);
1804 pr_err("%s: request_irq (%d) failed\n", __func__,
1806 goto err_request_irq;
1808 pr_info("%s: request_irq [%d] success.\n", __func__,
1811 err = device_create_file(&ts->client->dev, &dev_attr_irq_enable);
1813 pr_err("%s:File device creation failed: %d\n", __func__, err);
1815 goto err_create_file_failed;
1818 err = device_create_file(&ts->client->dev, &dev_attr_update_status);
1820 pr_err("%s:File device creation failed: %d\n", __func__, err);
1822 goto err_create_update_status_failed;
1825 err = device_create_file(&ts->client->dev, &dev_attr_fw_version);
1827 pr_err("%s:File device creation failed: %d\n", __func__, err);
1829 goto err_create_fw_version_file_failed;
1832 ts->regulator = regulator_get(&ts->client->dev, "vio");
1833 if (!IS_ERR_OR_NULL(ts->regulator))
1834 regulator_enable(ts->regulator);
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);
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);
1852 qtouch_ts_unregister_input(ts);
1855 err_input_register_dev:
1856 err_reading_info_block:
1857 i2c_set_clientdata(client, NULL);
1860 err_alloc_data_failed:
1864 static int qtouch_ts_remove(struct i2c_client *client)
1866 struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1868 if (!IS_ERR_OR_NULL(ts->regulator))
1869 regulator_put(ts->regulator);
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);
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);
1883 static int qtouch_ts_suspend(struct i2c_client *client, pm_message_t mesg)
1885 struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1887 if (qtouch_tsdebug & 4)
1888 pr_info("%s: Suspending\n", __func__);
1890 if (!atomic_read(&ts->irq_enabled))
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);
1904 ret = qtouch_power_config(ts, 0);
1906 pr_err("%s: Cannot write power config\n", __func__);
1908 if (ts->pdata->hw_suspend)
1909 ts->pdata->hw_suspend(1);
1911 if (!IS_ERR_OR_NULL(ts->regulator))
1912 regulator_disable(ts->regulator);
1917 static int qtouch_ts_resume(struct i2c_client *client)
1919 struct qtouch_ts_data *ts = i2c_get_clientdata(client);
1922 struct qtm_object *obj;
1924 if (qtouch_tsdebug & 4)
1925 pr_info("%s: Resuming\n", __func__);
1927 if (!atomic_read(&ts->irq_enabled))
1933 if (!IS_ERR_OR_NULL(ts->regulator))
1934 regulator_enable(ts->regulator);
1936 if (ts->pdata->hw_suspend)
1937 ts->pdata->hw_suspend(0);
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)
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));
1953 input_sync(ts->input_dev);
1955 ret = qtouch_power_config(ts, 1);
1957 pr_err("%s: Cannot write power config\n", __func__);
1958 ts->enable_irq_flag = 0;
1961 ret = qtouch_force_calibration(ts);
1963 pr_err("%s: Unable to recalibrate after power config\n", __func__);
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);
1972 pr_err("%s: Can't to set addr to msg processor\n", __func__);
1973 ts->enable_irq_flag = 0;
1977 enable_irq(ts->client->irq);
1978 ts->enable_irq_flag = 1;
1982 #ifdef CONFIG_HAS_EARLYSUSPEND
1983 static void qtouch_ts_early_suspend(struct early_suspend *handler)
1985 struct qtouch_ts_data *ts;
1987 ts = container_of(handler, struct qtouch_ts_data, early_suspend);
1988 qtouch_ts_suspend(ts->client, PMSG_SUSPEND);
1991 static void qtouch_ts_late_resume(struct early_suspend *handler)
1993 struct qtouch_ts_data *ts;
1995 ts = container_of(handler, struct qtouch_ts_data, early_suspend);
1996 qtouch_ts_resume(ts->client);
2000 /******** init ********/
2001 static const struct i2c_device_id qtouch_ts_id[] = {
2002 { QTOUCH_TS_NAME, 0 },
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,
2013 .id_table = qtouch_ts_id,
2015 .name = QTOUCH_TS_NAME,
2016 .owner = THIS_MODULE,
2020 static int __devinit qtouch_ts_init(void)
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__);
2027 return i2c_add_driver(&qtouch_ts_driver);
2030 static void __exit qtouch_ts_exit(void)
2032 i2c_del_driver(&qtouch_ts_driver);
2034 destroy_workqueue(qtouch_ts_wq);
2037 module_init(qtouch_ts_init);
2038 module_exit(qtouch_ts_exit);
2040 MODULE_AUTHOR("Dima Zavin <dima@android.com>");
2041 MODULE_DESCRIPTION("Quantum OBP Touchscreen Driver");
2042 MODULE_LICENSE("GPL");