2 * Copyright (C) 2010,Imagis Technology Co. Ltd. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/version.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/input.h>
20 #include <linux/interrupt.h>
21 #include <linux/i2c.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
25 #include <linux/platform_device.h>
26 #include <linux/device.h>
27 #include <linux/mutex.h>
28 #include <mach/gpio.h>
29 #include <linux/input/mt.h>
32 #include "ist30xx_update.h"
33 #include "ist30xx_tracking.h"
36 #include <linux/of_gpio.h>
37 #include <linux/of_device.h>
41 #include "ist30xx_misc.h"
44 #include "ist30xx_cmcs.h"
47 #include "ist30xx_sec.h"
48 extern int sec_touch_sysfs(struct ist30xx_data *data);
49 extern int sec_fac_cmd_init(struct ist30xx_data *data);
52 #define MAX_ERR_CNT (100)
55 int ist30xx_key_code[] = { 0, KEY_MENU, KEY_BACK };
58 DEFINE_MUTEX(ist30xx_mutex);
60 volatile bool ist30xx_irq_working = false;
61 static bool ist30xx_initialized = 0;
62 struct ist30xx_data *ts_data;
63 static struct delayed_work work_reset_check;
64 #if IST30XX_EVENT_MODE
65 static struct delayed_work work_noise_protect, work_debug_algorithm;
68 static int ist30xx_power_status = -1;
69 static int ist30xx_ta_status = -1;
70 static int ist30xx_noise_mode = -1;
72 #if IST30XX_INTERNAL_BIN && IST30XX_UPDATE_BY_WORKQUEUE
73 static struct delayed_work work_fw_update;
76 int ist30xx_report_rate = -1;
77 int ist30xx_idle_rate = -1;
79 u32 event_ms = 0, timer_ms = 0;
80 u32 ist30xx_scan_count = 0;
81 int ist30xx_scan_retry = 0;
83 #if IST30XX_EVENT_MODE
84 static struct timer_list event_timer;
85 static struct timespec t_current; // ns
86 int timer_period_ms = 500; // 0.5sec
87 #define EVENT_TIMER_INTERVAL (HZ * timer_period_ms / 1000)
88 #endif // IST30XX_EVENT_MODE
90 char *ist_vdd_name = "vddsim2";
93 extern TSP_INFO ist30xx_tsp_info;
94 extern TKEY_INFO ist30xx_tkey_info;
98 int ist30xx_dbg_level = IST30XX_DEBUG_LEVEL;
99 void tsp_printk(int level, const char *fmt, ...)
101 struct va_format vaf;
105 if (ist30xx_dbg_level < level)
113 r = printk("%s %pV", IST30XX_DEBUG_TAG, &vaf);
118 long get_milli_second(void)
120 #if IST30XX_EVENT_MODE
121 ktime_get_ts(&t_current);
123 return t_current.tv_sec * 1000 + t_current.tv_nsec / 1000000;
126 #endif // IST30XX_EVENT_MODE
129 int ist30xx_intr_wait(long ms)
131 long start_ms = get_milli_second();
135 if (!ist30xx_irq_working)
138 curr_ms = get_milli_second();
139 if ((curr_ms < 0) || (start_ms < 0) || (curr_ms - start_ms > ms)) {
140 tsp_info("%s() timeout(%dms)\n", __func__, ms);
149 void ist30xx_disable_irq(struct ist30xx_data *data)
151 if (likely(data->irq_enabled)) {
152 ist30xx_tracking(TRACK_INTR_DISABLE);
153 disable_irq(data->client->irq);
154 data->irq_enabled = 0;
155 data->status.event_mode = false;
159 void ist30xx_enable_irq(struct ist30xx_data *data)
161 if (likely(!data->irq_enabled)) {
162 ist30xx_tracking(TRACK_INTR_ENABLE);
163 enable_irq(data->client->irq);
165 data->irq_enabled = 1;
166 data->status.event_mode = true;
170 int ist30xx_max_error_cnt = MAX_ERR_CNT;
171 int ist30xx_error_cnt = 0;
172 void ist30xx_scheduled_reset(void)
174 if (likely(ist30xx_initialized))
175 schedule_delayed_work(&work_reset_check, 0);
178 static void ist30xx_request_reset(void)
181 if (unlikely(ist30xx_error_cnt >= ist30xx_max_error_cnt)) {
182 tsp_info("%s()\n", __func__);
183 ist30xx_scheduled_reset();
184 ist30xx_error_cnt = 0;
188 #define NOISE_MODE_TA (0)
189 #define NOISE_MODE_CALL (1)
190 #define NOISE_MODE_COVER (2)
191 #define NOISE_MODE_POWER (8)
192 void ist30xx_start(struct ist30xx_data *data)
194 if (ist30xx_initialized) {
195 #if IST30XX_EVENT_MODE
196 ist30xx_scan_count = 0;
197 ist30xx_scan_retry = 0;
198 mod_timer(&event_timer, get_jiffies_64() + EVENT_TIMER_INTERVAL * 2);
202 ist30xx_tracking(ist30xx_ta_status ?
203 TRACK_CMD_TACON : TRACK_CMD_TADISCON);
205 ist30xx_noise_mode &= ~(1 << NOISE_MODE_TA);
206 ist30xx_noise_mode |= (ist30xx_ta_status << NOISE_MODE_TA);
208 ist30xx_noise_mode &= ~(1 << NOISE_MODE_POWER);
209 ist30xx_noise_mode |= (ist30xx_power_status << NOISE_MODE_POWER);
211 ist30xx_noise_mode |= (TSP_LOCAL_CODE << 16);
212 ist30xx_write_cmd(data->client, CMD_SET_NOISE_MODE, ist30xx_noise_mode);
214 tsp_info("%s(), local : %d, mode : 0x%x\n", __func__,
215 (ist30xx_noise_mode >> 16) & 0xFFFF, ist30xx_noise_mode & 0xFFFF);
217 if (ist30xx_report_rate >= 0) {
218 ist30xx_write_cmd(data->client, CMD_SET_REPORT_RATE,
219 ist30xx_report_rate);
220 tsp_info(" reporting rate : %dus\n", ist30xx_report_rate);
223 if (ist30xx_idle_rate >= 0) {
224 ist30xx_write_cmd(data->client, CMD_SET_IDLE_TIME, ist30xx_idle_rate);
225 tsp_info(" idle rate : %dus\n", ist30xx_idle_rate);
228 ist30xx_cmd_start_scan(data->client);
232 int ist30xx_get_ver_info(struct ist30xx_data *data)
236 data->fw.prev_core_ver = data->fw.core_ver;
237 data->fw.prev_param_ver = data->fw.param_ver;
238 data->fw.core_ver = data->fw.param_ver = 0;
240 ret = ist30xx_read_cmd(data->client, CMD_GET_FW_VER, &data->fw.core_ver);
244 ret = ist30xx_read_cmd(data->client, CMD_GET_PARAM_VER,
245 &data->fw.param_ver);
249 ret = ist30xx_read_cmd(data->client, CMD_GET_SUB_VER, &data->fw.sub_ver);
253 tsp_info("IC version read core: %x, param: %x, sub: %x\n",
254 data->fw.core_ver, data->fw.param_ver, data->fw.sub_ver);
260 void ist30xx_print_info(void)
262 TSP_INFO *tsp = &ist30xx_tsp_info;
263 TKEY_INFO *tkey = &ist30xx_tkey_info;
265 tsp_info("*** TSP/TKEY info ***\n");
266 tsp_info("tscn finger num : %d\n", tsp->finger_num);
267 tsp_info("tscn dir swap: %d, flip x: %d, y: %d\n",
268 tsp->dir.swap_xy, tsp->dir.flip_x, tsp->dir.flip_y);
269 tsp_info("tscn ch_num tx: %d, rx: %d\n",
270 tsp->ch_num.tx, tsp->ch_num.rx);
271 tsp_info("tscn width: %d, height: %d\n",
272 tsp->width, tsp->height);
273 tsp_info("tkey enable: %d, key num: %d, axis rx: %d \n",
274 tkey->enable, tkey->key_num, tkey->axis_rx);
275 tsp_info("tkey ch_num[0] %d, [1] %d, [2] %d, [3] %d, [4] %d\n",
276 tkey->ch_num[0], tkey->ch_num[1], tkey->ch_num[2],
277 tkey->ch_num[3], tkey->ch_num[4]);
281 #define CALIB_MSG_MASK (0xF0000FFF)
282 #define CALIB_MSG_VALID (0x80000CAB)
283 #define TRACKING_INTR_VALID (0x127EA597)
284 u32 tracking_intr_value = TRACKING_INTR_VALID;
285 int ist30xx_get_info(struct ist30xx_data *data)
291 mutex_lock(&ist30xx_mutex);
292 ist30xx_disable_irq(data);
294 #if IST30XX_INTERNAL_BIN
296 #if IST30XX_UPDATE_BY_WORKQUEUE
297 ist30xx_get_update_info(data, data->fw.buf, data->fw.buf_size);
299 ist30xx_get_tsp_info(data);
300 ist30xx_get_tkey_info(data);
301 #endif // IST30XX_DEBUG
304 ret = ist30xx_get_ver_info(data);
309 ret = ist30xx_tsp_update_info();
313 ret = ist30xx_tkey_update_info();
316 #endif // IST30XX_DEBUG
317 #endif // IST30XX_INTERNAL_BIN
320 ist30xx_print_info();
321 data->max_fingers = ist30xx_tsp_info.finger_num;
322 data->max_keys = ist30xx_tkey_info.key_num;
323 #endif // IST30XX_DEBUG
325 ret = ist30xx_read_cmd(ts_data->client, CMD_GET_CALIB_RESULT, &calib_msg);
326 if (likely(ret == 0)) {
327 tsp_info("calib status: 0x%08x\n", calib_msg);
328 ms = get_milli_second();
329 ist30xx_put_track_ms(ms);
330 ist30xx_put_track(&tracking_intr_value, 1);
331 ist30xx_put_track(&calib_msg, 1);
332 if ((calib_msg & CALIB_MSG_MASK) != CALIB_MSG_VALID ||
333 CALIB_TO_STATUS(calib_msg) > 0) {
334 ist30xx_calibrate(IST30XX_FW_UPDATE_RETRY);
335 ist30xx_reset(false);
339 #if (IST30XX_EVENT_MODE && IST30XX_CHECK_CALIB)
340 if (likely(!data->status.update)) {
341 ret = ist30xx_cmd_check_calib(data->client);
343 data->status.calib = 1;
344 data->status.calib_msg = 0;
345 event_ms = (u32)get_milli_second();
346 data->status.event_mode = true;
350 ist30xx_start(ts_data);
353 #if !(IST30XX_INTERNAL_BIN)
356 if (likely(ret == 0))
357 ist30xx_enable_irq(data);
358 mutex_unlock(&ist30xx_mutex);
363 #define PRESS_MSG_MASK (0x01)
364 #define MULTI_MSG_MASK (0x02)
365 #define PRESS_MSG_KEY (0x6)
366 #define TOUCH_DOWN_MESSAGE ("p")
367 #define TOUCH_UP_MESSAGE ("r")
368 #define TOUCH_MOVE_MESSAGE (" ")
369 bool tsp_touched[IST30XX_MAX_MT_FINGERS] = { false, };
370 bool tkey_pressed[IST30XX_MAX_KEYS] = { false, };
371 void print_tsp_event(finger_info *finger)
373 int idx = finger->bit_field.id - 1;
376 press = PRESSED_FINGER(ts_data->t_status, finger->bit_field.id);
379 if (tsp_touched[idx] == false) { // touch down
380 tsp_notc("%s%d (%d, %d)\n",
381 TOUCH_DOWN_MESSAGE, finger->bit_field.id,
382 finger->bit_field.x, finger->bit_field.y);
383 tsp_touched[idx] = true;
384 } else { // touch move
385 tsp_debug("%s%d (%d, %d)\n",
386 TOUCH_MOVE_MESSAGE, finger->bit_field.id,
387 finger->bit_field.x, finger->bit_field.y);
390 if (tsp_touched[idx] == true) { // touch up
391 tsp_notc("%s%d\n", TOUCH_UP_MESSAGE, finger->bit_field.id);
392 tsp_touched[idx] = false;
397 void print_tkey_event(int id)
400 bool press = PRESSED_KEY(ts_data->t_status, id);
403 if (tkey_pressed[idx] == false) { // tkey down
404 tsp_notc("k %s%d\n", TOUCH_DOWN_MESSAGE, id);
405 tkey_pressed[idx] = true;
408 if (tkey_pressed[idx] == true) { // tkey up
409 tsp_notc("k %s%d\n", TOUCH_UP_MESSAGE, id);
410 tkey_pressed[idx] = false;
415 static void release_finger(int id)
417 input_mt_slot(ts_data->input_dev, id - 1);
418 input_mt_report_slot_state(ts_data->input_dev, MT_TOOL_FINGER, false);
420 ist30xx_tracking(TRACK_POS_FINGER + id);
421 tsp_info("%s() %d\n", __func__, id);
423 tsp_touched[id - 1] = false;
425 input_sync(ts_data->input_dev);
428 #define CANCEL_KEY (0xFF)
429 #define RELEASE_KEY (0)
430 static void release_key(int id, u8 key_status)
432 input_report_key(ts_data->input_dev, ist30xx_key_code[id], key_status);
434 ist30xx_tracking(TRACK_POS_KEY + id);
435 tsp_info("%s() key%d, status: %d\n", __func__, id, key_status);
437 tkey_pressed[id - 1] = false;
439 input_sync(ts_data->input_dev);
442 static void clear_input_data(struct ist30xx_data *data)
447 status = PARSE_FINGER_STATUS(data->t_status);
456 status = PARSE_KEY_STATUS(data->t_status);
459 release_key(id, RELEASE_KEY);
466 static int check_report_fingers(struct ist30xx_data *data, int finger_counts)
469 finger_info *fingers = (finger_info *)data->fingers;
471 /* current finger info */
472 for (i = 0; i < finger_counts; i++) {
473 if (unlikely((fingers[i].bit_field.x > IST30XX_MAX_X) ||
474 (fingers[i].bit_field.y > IST30XX_MAX_Y))) {
475 tsp_warn("Invalid touch data - %d: %d(%d, %d), 0x%08x\n", i,
476 fingers[i].bit_field.id,
477 fingers[i].bit_field.x,
478 fingers[i].bit_field.y,
479 fingers[i].full_field);
481 fingers[i].bit_field.id = 0;
482 ist30xx_tracking(TRACK_POS_UNKNOWN);
490 static int check_valid_coord(u32 *msg, int cnt)
494 u8 chksum1 = msg[0] >> 24;
497 msg[0] &= 0x00FFFFFF;
499 cnt *= IST30XX_DATA_LEN;
504 if (chksum1 != chksum2) {
505 tsp_err("intr chksum: %02x, %02x\n", chksum1, chksum2);
509 return (chksum1 == chksum2) ? 0 : -EPERM;
512 static void report_input_data(struct ist30xx_data *data, int finger_counts,
517 finger_info *fingers = (finger_info *)data->fingers;
521 status = PARSE_FINGER_STATUS(data->t_status);
522 for (id = 0; id < IST30XX_MAX_MT_FINGERS; id++) {
523 press = (status & (1 << id)) ? true : false;
525 input_mt_slot(data->input_dev, id);
526 input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, press);
528 fingers[idx].bit_field.id = id + 1;
529 print_tsp_event(&fingers[idx]);
534 input_report_abs(data->input_dev, ABS_MT_POSITION_X,
535 fingers[idx].bit_field.x);
536 input_report_abs(data->input_dev, ABS_MT_POSITION_Y,
537 fingers[idx].bit_field.y);
538 input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR,
539 fingers[idx].bit_field.area);
544 status = PARSE_KEY_STATUS(data->t_status);
545 for (id = 0; id < data->max_keys; id++) {
546 press = (status & (1 << id)) ? true : false;
548 input_report_key(data->input_dev, ist30xx_key_code[id + 1], press);
550 print_tkey_event(id + 1);
552 #endif // IST30XX_USE_KEY
554 ist30xx_error_cnt = 0;
555 ist30xx_scan_retry = 0;
557 input_sync(data->input_dev);
561 * CMD : CMD_GET_COORD
563 * 1st [31:24] [23:21] [20:16] [15:12] [11:10] [9:0]
564 * Checksum KeyCnt KeyStatus FingerCnt Rsvd. FingerStatus
565 * 2nd [31:28] [27:24] [23:12] [11:0]
568 u32 intr_debug_addr, intr_debug2_addr, intr_debug3_addr = 0;
569 u32 intr_debug_size, intr_debug2_size, intr_debug3_size = 0;
570 static irqreturn_t ist30xx_irq_thread(int irq, void *ptr)
573 int key_cnt, finger_cnt, read_cnt;
574 struct ist30xx_data *data = ts_data;
576 u32 msg[IST30XX_MAX_MT_FINGERS + offset];
579 ist30xx_irq_working = true;
581 if (unlikely(!data->irq_enabled))
584 ms = get_milli_second();
586 memset(msg, 0, sizeof(msg));
588 ret = ist30xx_get_position(data->client, msg, 1);
592 tsp_verb("intr msg: 0x%08x\n", *msg);
594 if (unlikely(*msg == 0xE11CE970)) // TSP IC Exception
597 if (unlikely(*msg == 0 || *msg == 0xFFFFFFFF)) // Unknown CMD
601 ist30xx_put_track_ms(event_ms);
602 ist30xx_put_track(&tracking_intr_value, 1);
603 ist30xx_put_track(msg, 1);
605 if (unlikely((*msg & CALIB_MSG_MASK) == CALIB_MSG_VALID)) {
606 data->status.calib_msg = *msg;
607 tsp_info("calib status: 0x%08x\n", data->status.calib_msg);
611 } else if (*msg == IST30XX_CHKIC_END) {
612 data->status.chkic_msg = *msg;
613 tsp_info("IC check status: 0x%08x\n", data->status.chkic_msg);
619 #if IST30XX_ENABLE_GESTURE
620 ret = PARSE_GESTURE_MESSAGE(*msg);
621 if (unlikely(ret > 0)) {
622 tsp_info("Gesture ID: %d (0x%08x)\n", ret, *msg);
623 ist30xx_gesture_cmd(data, ret);
629 memset(data->fingers, 0, sizeof(data->fingers));
631 /* Unknown interrupt data for extend coordinate */
632 if (unlikely(!CHECK_INTR_STATUS(*msg)))
635 data->t_status = *msg;
636 key_cnt = PARSE_KEY_CNT(data->t_status);
637 finger_cnt = PARSE_FINGER_CNT(data->t_status);
638 read_cnt = finger_cnt;
640 if (unlikely((finger_cnt > data->max_fingers) ||
641 (key_cnt > data->max_keys))) {
642 tsp_warn("Invalid touch count - finger: %d(%d), key: %d(%d)\n",
643 finger_cnt, data->max_fingers, key_cnt, data->max_keys);
649 ret = ist30xx_get_position(data->client, &msg[offset], read_cnt);
653 for (i = 0; i < read_cnt; i++)
654 data->fingers[i].full_field = msg[i + offset];
656 for (i = 0; i < read_cnt; i++) {
657 ret = ist30xx_get_position(data->client, &msg[i + offset], 1);
661 data->fingers[i].full_field = msg[i + offset];
663 #endif // I2C_BURST_MODE
665 ist30xx_put_track(msg + offset, read_cnt);
666 for (i = 0; i < read_cnt; i++)
667 tsp_verb("intr msg(%d): 0x%08x\n", i + offset, msg[i + offset]);
670 ret = check_valid_coord(&msg[0], read_cnt + 1);
671 if (unlikely(ret < 0))
674 if (unlikely(check_report_fingers(data, finger_cnt)))
677 report_input_data(data, finger_cnt, key_cnt);
679 if (intr_debug2_addr > 0 && intr_debug2_size > 0) {
680 tsp_notc("Intr_debug2 (addr: 0x%08x)\n", intr_debug2_addr);
681 for (i = 0; i < intr_debug2_size; i++) {
682 ist30xx_read_buf(data->client,
683 intr_debug2_addr + IST30XX_DATA_LEN * i, &msg[i], 1);
685 tsp_notc("\t%08x\n", msg[i]);
687 ist30xx_put_track(msg, intr_debug2_size);
690 if (intr_debug3_addr > 0 && intr_debug3_size > 0) {
691 tsp_notc("Intr_debug3 (addr: 0x%08x)\n", intr_debug3_addr);
692 for (i = 0; i < intr_debug3_size; i++) {
693 ist30xx_read_buf(data->client,
694 intr_debug3_addr + IST30XX_DATA_LEN * i, &msg[i], 1);
696 tsp_notc("\t%08x\n", msg[i]);
698 ist30xx_put_track(msg, intr_debug3_size);
704 tsp_err("intr msg: 0x%08x, ret: %d\n", msg[0], ret);
705 ist30xx_request_reset();
707 ist30xx_irq_working = false;
708 event_ms = (u32)get_milli_second();
712 tsp_err("Occured IC exception\n");
713 ist30xx_scheduled_reset();
714 ist30xx_irq_working = false;
715 event_ms = (u32)get_milli_second();
719 #ifdef CONFIG_HAS_EARLYSUSPEND
720 #define ist30xx_suspend NULL
721 #define ist30xx_resume NULL
722 static void ist30xx_early_suspend(struct early_suspend *h)
724 struct ist30xx_data *data = container_of(h, struct ist30xx_data,
727 mutex_lock(&ist30xx_mutex);
728 ist30xx_disable_irq(data);
729 del_timer(&event_timer);
730 cancel_delayed_work_sync(&work_noise_protect);
731 cancel_delayed_work_sync(&work_reset_check);
732 cancel_delayed_work_sync(&work_debug_algorithm);
733 ist30xx_internal_suspend(data);
734 clear_input_data(data);
735 mutex_unlock(&ist30xx_mutex);
737 static void ist30xx_late_resume(struct early_suspend *h)
739 struct ist30xx_data *data = container_of(h, struct ist30xx_data,
742 ist30xx_power_status = 1;
744 mutex_lock(&ist30xx_mutex);
745 ist30xx_internal_resume(data);
747 ist30xx_enable_irq(data);
748 mutex_unlock(&ist30xx_mutex);
751 static int ist30xx_suspend(struct device *dev)
753 struct i2c_client *client = to_i2c_client(dev);
754 struct ist30xx_data *data = i2c_get_clientdata(client);
756 mutex_lock(&ist30xx_mutex);
757 ist30xx_disable_irq(data);
758 del_timer(&event_timer);
759 cancel_delayed_work_sync(&work_noise_protect);
760 cancel_delayed_work_sync(&work_reset_check);
761 cancel_delayed_work_sync(&work_debug_algorithm);
762 ist30xx_internal_suspend(data);
763 clear_input_data(data);
764 mutex_unlock(&ist30xx_mutex);
768 static int ist30xx_resume(struct device *dev)
770 struct i2c_client *client = to_i2c_client(dev);
771 struct ist30xx_data *data = i2c_get_clientdata(client);
773 ist30xx_power_status = 1;
775 mutex_lock(&ist30xx_mutex);
776 ist30xx_internal_resume(data);
778 ist30xx_enable_irq(data);
779 mutex_unlock(&ist30xx_mutex);
783 #endif // CONFIG_HAS_EARLYSUSPEND
785 static void ist30xx_shutdown(struct i2c_client *client)
787 struct ist30xx_data *data = i2c_get_clientdata(client);
789 if(ist30xx_initialized)
791 mutex_lock(&ist30xx_mutex);
792 ist30xx_disable_irq(data);
793 ist30xx_internal_suspend(data);
794 mutex_unlock(&ist30xx_mutex);
796 del_timer_sync(&event_timer);
801 void ist30xx_set_ta_mode(bool charging)
803 tsp_info("%s(), charging = %d\n", __func__, charging);
805 if (unlikely(charging == ist30xx_ta_status))
808 if (unlikely(ist30xx_noise_mode == -1)) {
809 ist30xx_ta_status = charging ? 1 : 0;
813 ist30xx_ta_status = charging ? 1 : 0;
815 ist30xx_scheduled_reset();
817 EXPORT_SYMBOL(ist30xx_set_ta_mode);
819 void ist30xx_set_call_mode(int mode)
821 tsp_info("%s(), mode = %d\n", __func__, mode);
823 if (unlikely(mode == ((ist30xx_noise_mode >> NOISE_MODE_CALL) & 1)))
826 ist30xx_noise_mode &= ~(1 << NOISE_MODE_CALL);
828 ist30xx_noise_mode |= (1 << NOISE_MODE_CALL);
830 ist30xx_scheduled_reset();
832 EXPORT_SYMBOL(ist30xx_set_call_mode);
834 void ist30xx_set_cover_mode(int mode)
836 tsp_info("%s(), mode = %d\n", __func__, mode);
838 if (unlikely(mode == ((ist30xx_noise_mode >> NOISE_MODE_COVER) & 1)))
841 ist30xx_noise_mode &= ~(1 << NOISE_MODE_COVER);
843 ist30xx_noise_mode |= (1 << NOISE_MODE_COVER);
845 ist30xx_scheduled_reset();
847 EXPORT_SYMBOL(ist30xx_set_cover_mode);
849 void charger_enable(int enable)
851 bool charging = enable ? true : false;
853 ist30xx_set_ta_mode(charging);
856 static void reset_work_func(struct work_struct *work)
858 if (unlikely((ts_data == NULL) || (ts_data->client == NULL)))
861 tsp_info("Request reset function\n");
863 if (likely((ist30xx_initialized == 1) && (ts_data->status.power == 1) &&
864 (ts_data->status.update != 1) && (ts_data->status.calib != 1))) {
865 mutex_lock(&ist30xx_mutex);
866 ist30xx_disable_irq(ts_data);
867 clear_input_data(ts_data);
868 ist30xx_reset(false);
869 ist30xx_start(ts_data);
870 ist30xx_enable_irq(ts_data);
871 mutex_unlock(&ist30xx_mutex);
875 #if IST30XX_INTERNAL_BIN && IST30XX_UPDATE_BY_WORKQUEUE
876 static void fw_update_func(struct work_struct *work)
878 if (unlikely((ts_data == NULL) || (ts_data->client == NULL)))
881 tsp_info("FW update function\n");
883 if (likely(ist30xx_auto_bin_update(ts_data)))
884 ist30xx_disable_irq(ts_data);
886 #endif // IST30XX_INTERNAL_BIN && IST30XX_UPDATE_BY_WORKQUEUE
889 #if IST30XX_EVENT_MODE
890 u32 ist30xx_max_scan_retry = 2;
891 u32 ist30xx_algr_addr = 0, ist30xx_algr_size = 0;
893 #define SCAN_STATUS_MAGIC (0x3C000000)
894 #define SCAN_STATUS_MASK (0xFF000000)
895 #define FINGER_CNT_MASK (0x00F00000)
896 #define SCAN_CNT_MASK (0x000FFFFF)
897 #define GET_FINGER_CNT(n) ((n & FINGER_CNT_MASK) >> 20)
898 #define GET_SCAN_CNT(n) (n & SCAN_CNT_MASK)
899 static void noise_work_func(struct work_struct *work)
901 #if IST30XX_NOISE_MODE
905 ret = ist30xx_read_cmd(ts_data->client, IST30XXB_MEM_COUNT, &scan_status);
907 tsp_warn("Mem scan count read fail!\n");
911 ist30xx_put_track_ms(timer_ms);
912 ist30xx_put_track(&scan_status, 1);
914 tsp_verb("scan status: 0x%x\n", scan_status);
916 /* Check valid scan count */
917 if (unlikely((scan_status & SCAN_STATUS_MASK) != SCAN_STATUS_MAGIC)) {
918 tsp_warn("Scan status is not corrected! (0x%08x)\n", scan_status);
922 /* Status of IC is idle */
923 if (GET_FINGER_CNT(scan_status) == 0) {
924 if ((PARSE_FINGER_CNT(ts_data->t_status) > 0) ||
925 (PARSE_KEY_CNT(ts_data->t_status) > 0))
926 clear_input_data(ts_data);
929 scan_status &= SCAN_CNT_MASK;
931 /* Status of IC is lock-up */
932 if (unlikely(scan_status == ist30xx_scan_count)) {
933 tsp_warn("TSP IC is not responded! (0x%08x)\n", scan_status);
936 ist30xx_scan_retry = 0;
939 ist30xx_scan_count = scan_status;
944 ist30xx_scan_retry++;
945 tsp_warn("Retry scan status!(%d)\n", ist30xx_scan_retry);
947 if (unlikely(ist30xx_scan_retry == ist30xx_max_scan_retry)) {
948 ist30xx_scheduled_reset();
949 ist30xx_scan_retry = 0;
951 #endif // IST30XX_NOISE_MODE
954 static void debug_work_func(struct work_struct *work)
956 #if IST30XX_ALGORITHM_MODE
961 buf32 = kzalloc(ist30xx_algr_size, GFP_KERNEL);
963 for (i = 0; i < ist30xx_algr_size; i++) {
964 ret = ist30xx_read_buf(ts_data->client,
965 ist30xx_algr_addr + IST30XX_DATA_LEN * i, &buf32[i], 1);
967 tsp_warn("Algorithm mem addr read fail!\n");
972 ist30xx_put_track(buf32, ist30xx_algr_size);
974 tsp_debug(" 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
975 buf32[0], buf32[1], buf32[2], buf32[3], buf32[4]);
979 void timer_handler(unsigned long data)
981 struct ist30xx_status *status = &ts_data->status;
983 if (ist30xx_irq_working)
986 if (status->event_mode) {
987 if (likely((status->power == 1) && (status->update != 1))) {
988 timer_ms = (u32)get_milli_second();
989 if (unlikely(status->calib == 1)) { // Check calibration
990 if ((status->calib_msg & CALIB_MSG_MASK) == CALIB_MSG_VALID) {
991 tsp_info("Calibration check OK!!\n");
992 schedule_delayed_work(&work_reset_check, 0);
994 } else if (timer_ms - event_ms >= 3000) { // 3second
995 tsp_info("calibration timeout over 3sec\n");
996 schedule_delayed_work(&work_reset_check, 0);
999 } else if (likely(status->noise_mode)) {
1000 if (timer_ms - event_ms > 100) // 100ms after last interrupt
1001 schedule_delayed_work(&work_noise_protect, 0);
1004 #if IST30XX_ALGORITHM_MODE
1005 if ((ist30xx_algr_addr >= IST30XXB_ACCESS_ADDR) &&
1006 (ist30xx_algr_size > 0)) {
1007 if (timer_ms - event_ms > 100) // 100ms after last interrupt
1008 schedule_delayed_work(&work_debug_algorithm, 0);
1015 mod_timer(&event_timer, get_jiffies_64() + EVENT_TIMER_INTERVAL);
1017 #endif // IST30XX_EVENT_MODE
1019 static int ist30xx_probe(struct i2c_client *client,
1020 const struct i2c_device_id *id)
1024 struct ist30xx_data *data;
1025 struct input_dev *input_dev;
1028 tsp_info("##### IST30XX(Ver%d) Initialize #####\n", IST30XX_DD_VERSION);
1030 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1031 printk(KERN_INFO "i2c_check_functionality error\n");
1034 tsp_info("%s(), the i2c addr=0x%x\n", __func__, client->addr);
1036 data = kzalloc(sizeof(*data), GFP_KERNEL);
1041 struct device_node *np = client->dev.of_node;
1043 tsp_info("the client->dev.of_node=0x%x\n", client->dev.of_node);
1045 if (client->dev.of_node){
1046 ist_gpio = of_get_gpio(np, 0);
1047 of_property_read_string(np, "tsp_pwr_name", &ist_vdd_name);
1048 tsp_info("gpio id=%d,power name:%s\n", ist_gpio,ist_vdd_name);
1052 input_dev = input_allocate_device();
1053 if (unlikely(!input_dev)) {
1055 tsp_err("%s(), input_allocate_device failed (%d)\n",
1062 ist_vdd_name = "vddsim2";
1065 /* configure touchscreen interrupt gpio */
1066 ret = gpio_request(ist_gpio, "ist30xx_irq_gpio");
1067 printk("[TSP]request gpio id = %d\n", ist_gpio);
1069 tsp_err("unable to request gpio.(%s)\r\n",input_dev->name);
1073 client->irq = gpio_to_irq(ist_gpio);
1074 printk("[TSP] client->irq : %d\n", client->irq);
1076 data->max_fingers = data->max_keys = IST30XX_MAX_MT_FINGERS;
1077 data->irq_enabled = 1;
1078 data->client = client;
1079 data->input_dev = input_dev;
1080 i2c_set_clientdata(client, data);
1082 input_mt_init_slots(input_dev, IST30XX_MAX_MT_FINGERS, 0);
1084 input_dev->name = "ist30xx_ts_input";
1085 input_dev->id.bustype = BUS_I2C;
1086 input_dev->dev.parent = &client->dev;
1088 set_bit(EV_ABS, input_dev->evbit);
1089 set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
1091 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, IST30XX_MAX_X, 0, 0);
1092 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, IST30XX_MAX_Y, 0, 0);
1093 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, IST30XX_MAX_W, 0, 0);
1098 set_bit(EV_KEY, input_dev->evbit);
1099 set_bit(EV_SYN, input_dev->evbit);
1100 for (i = 1; i < ARRAY_SIZE(ist30xx_key_code); i++)
1101 set_bit(ist30xx_key_code[i], input_dev->keybit);
1105 input_set_drvdata(input_dev, data);
1106 ret = input_register_device(input_dev);
1107 if (unlikely(ret)) {
1108 input_free_device(input_dev);
1112 #ifdef CONFIG_HAS_EARLYSUSPEND
1113 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
1114 data->early_suspend.suspend = ist30xx_early_suspend;
1115 data->early_suspend.resume = ist30xx_late_resume;
1116 register_early_suspend(&data->early_suspend);
1121 ret = ist30xx_init_system();
1122 if (unlikely(ret)) {
1123 tsp_err("chip initialization failed\n");
1127 ret = ist30xx_init_update_sysfs();
1132 ret = ist30xx_init_misc_sysfs();
1137 #if IST30XX_CMCS_TEST
1138 ret = ist30xx_init_cmcs_sysfs();
1143 #if SEC_FACTORY_MODE
1144 ret = sec_fac_cmd_init(data);
1147 ret = sec_touch_sysfs(data);
1152 #if IST30XX_TRACKING_MODE
1153 ret = ist30xx_init_tracking_sysfs();
1158 ret = request_threaded_irq(client->irq, NULL, ist30xx_irq_thread,
1159 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "ist30xx_ts", data);
1163 ist30xx_disable_irq(data);
1165 while ((data->chip_id != IST30XXB_CHIP_ID) &&
1166 (data->chip_id != IST3038_CHIP_ID)) {
1167 ret = ist30xx_read_cmd(data->client, IST30XXB_REG_CHIPID,
1169 if (unlikely(ret)) {
1170 tsp_info("tspid: %x\n", data->chip_id);
1171 ist30xx_reset(false);
1174 if (data->chip_id == 0x3000B)
1175 data->chip_id = IST30XXB_CHIP_ID;
1177 if (data->chip_id == 0x3038B)
1178 data->chip_id = IST3038_CHIP_ID;
1185 while (retry-- > 0) {
1186 ret = ist30xx_read_cmd(data->client, IST30XXB_REG_TSPTYPE,
1189 tsp_info("tsptype: %x\n", data->tsp_type);
1190 data->tsp_type = IST30XXB_PARSE_TSPTYPE(data->tsp_type);
1192 if (likely(ret == 0))
1195 if (unlikely(retry == 0))
1199 tsp_info("TSP IC: %x, TSP Vendor: %x\n", data->chip_id, data->tsp_type);
1201 data->status.event_mode = false;
1203 #if IST30XX_INTERNAL_BIN
1204 #if IST30XX_UPDATE_BY_WORKQUEUE
1205 INIT_DELAYED_WORK(&work_fw_update, fw_update_func);
1206 schedule_delayed_work(&work_fw_update, IST30XX_UPDATE_DELAY);
1208 ret = ist30xx_auto_bin_update(data);
1209 if (unlikely(ret != 0))
1212 #endif // IST30XX_INTERNAL_BIN
1214 if (ist30xx_ta_status < 0)
1215 ist30xx_ta_status = 0;
1217 if (ist30xx_power_status < 0)
1218 ist30xx_power_status = 0;
1220 if (ist30xx_noise_mode < 0)
1221 ist30xx_noise_mode = 0;
1223 ret = ist30xx_get_info(data);
1224 tsp_info("Get info: %s\n", (ret == 0 ? "success" : "fail"));
1226 INIT_DELAYED_WORK(&work_reset_check, reset_work_func);
1227 #if IST30XX_EVENT_MODE
1228 INIT_DELAYED_WORK(&work_noise_protect, noise_work_func);
1229 INIT_DELAYED_WORK(&work_debug_algorithm, debug_work_func);
1232 #if IST30XX_EVENT_MODE
1233 init_timer(&event_timer);
1234 event_timer.function = timer_handler;
1235 event_timer.expires = jiffies_64 + (EVENT_TIMER_INTERVAL);
1236 mod_timer(&event_timer, get_jiffies_64() + EVENT_TIMER_INTERVAL);
1239 ist30xx_initialized = 1;
1240 tsp_info("Probe end\n");
1245 tsp_info("ChipID: %x\n", data->chip_id);
1246 ist30xx_disable_irq(data);
1247 free_irq(client->irq, data);
1249 data->status.event_mode = false;
1250 tsp_err("Error, ist30xx init driver\n");
1251 ist30xx_power_off();
1252 #ifdef CONFIG_HAS_EARLYSUSPEND
1253 unregister_early_suspend(&data->early_suspend);
1255 input_unregister_device(input_dev);
1260 tsp_err("Error, ist30xx mem free\n");
1266 static int __exit ist30xx_remove(struct i2c_client *client)
1268 struct ist30xx_data *data = i2c_get_clientdata(client);
1270 #ifdef CONFIG_HAS_EARLYSUSPEND
1271 unregister_early_suspend(&data->early_suspend);
1274 ist30xx_disable_irq(data);
1275 free_irq(client->irq, data);
1276 ist30xx_power_off();
1278 input_unregister_device(data->input_dev);
1285 static struct i2c_device_id ist30xx_idtable[] = {
1286 { IST30XX_DEV_NAME, 0 },
1289 MODULE_DEVICE_TABLE(i2c, ist30xx_idtable);
1291 #ifdef CONFIG_HAS_EARLYSUSPEND
1292 static const struct dev_pm_ops ist30xx_pm_ops = {
1293 .suspend = ist30xx_suspend,
1294 .resume = ist30xx_resume,
1299 static const struct of_device_id ist30xxb_ts_of_match[] = {
1300 { .compatible = "Imagis,IST30XX", },
1305 static struct i2c_driver ist30xx_i2c_driver = {
1306 .id_table = ist30xx_idtable,
1307 .probe = ist30xx_probe,
1308 .remove = __exit_p(ist30xx_remove),
1309 .shutdown = ist30xx_shutdown,
1311 .owner = THIS_MODULE,
1312 .name = IST30XX_DEV_NAME,
1313 #ifdef CONFIG_HAS_EARLYSUSPEND
1314 .pm = &ist30xx_pm_ops,
1318 .of_match_table = ist30xxb_ts_of_match,
1323 static int __init ist30xx_init(void)
1325 tsp_info("Imagis init called\n");
1326 return i2c_add_driver(&ist30xx_i2c_driver);
1330 static void __exit ist30xx_exit(void)
1332 i2c_del_driver(&ist30xx_i2c_driver);
1335 module_init(ist30xx_init);
1336 module_exit(ist30xx_exit);
1338 MODULE_DESCRIPTION("Imagis IST30XX touch driver");
1339 MODULE_LICENSE("GPL");