import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / input / touchscreen / mediatek / mms244 / mms200_ts.c
1 /*
2 * Touchscreen driver for Melfas MMS-200 series
3 *
4 * Copyright (C) 2013 Melfas Inc.
5 * Author: DVK team <dvk@melfas.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/i2c.h>
19 #include <linux/input.h>
20 #include <linux/slab.h>
21 #include <linux/gpio.h>
22 #include <linux/sched.h>
23 #include <linux/kthread.h>
24 #include <linux/bitops.h>
25 #include <linux/kernel.h>
26 #include <linux/byteorder/generic.h>
27 #ifdef CONFIG_HAS_EARLYSUSPEND
28 #include <linux/earlysuspend.h>
29 #endif
30 #include <linux/interrupt.h>
31 #include <linux/time.h>
32 #include <linux/rtpm_prio.h>
33 #include <linux/proc_fs.h>
34 #include <linux/jiffies.h>
35 #include <linux/firmware.h>
36 #include <linux/earlysuspend.h>
37 #include <linux/irq.h>
38 #include <linux/input/mt.h>
39 #include <linux/cdev.h>
40 #include <linux/fs.h>
41 #include <linux/completion.h>
42 #include <linux/init.h>
43
44 #include <asm/uaccess.h>
45 #include <cust_eint.h>
46 #include <asm/unaligned.h>
47 #include <mach/eint.h>
48 #include <mach/mt_typedefs.h>
49 #include <mach/mt_pm_ldo.h>
50 #include <linux/types.h>
51 #include <linux/dma-mapping.h>
52
53 #ifdef MT6589
54 #include <mach/mt_boot.h>
55 #endif
56
57 #ifdef MT6592
58 #include <mach/mt_boot.h>
59 #endif
60
61 #include "tpd.h"
62 #include "mms200_ts.h"
63
64 #define TPD_POWER_SOURCE_CUSTOM MT6323_POWER_LDO_VGP1
65
66 #define TP_DEV_NAME "mms200"
67 #define I2C_RETRY_CNT 5 //Fixed value
68 #define DOWNLOAD_RETRY_CNT 5 //Fixed value
69 #define MELFAS_DOWNLOAD 1 //Fixed value
70
71 #define PRESS_KEY 1 //Fixed value
72 #define RELEASE_KEY 0 //Fixed value
73
74 /*
75 MMS244 RMI register map
76 */
77 #define TS_READ_LEN_ADDR 0x0F
78 #define TS_READ_START_ADDR 0x10
79 #define TS_READ_REGS_LEN 66
80 #define TS_WRITE_REGS_LEN 16
81 #define TS_TSP_REV_ADDR 0xc0
82 #define TS_HARDWARE_REV_ADDR 0xc1
83 #define TS_COMPATIBILITY_GRU_ADDR 0xc2
84 #define TS_FIRMWARE_REV_ADDR 0xc3
85 #define TS_CHIP_INFO_ADDR 0xc4
86 #define TS_MANUFACTURER_INFO_ADDR 0xc5
87
88 /*
89 MMS244 FACTORY
90 */
91 #define TS_TYPE_TRULY 0x02
92 #define TS_TYPE_BIEL 0x10
93 #define TS_TYPE_UNKNOWN 0xff
94
95 /*
96 MMS244 VERSION
97 */
98 #define MELFAS_HW_REVISON 0x01
99 #define MELFAS_TURLY_H1_FW_VERSION 0x04
100 #define MELFAS_TURLY_H2_FW_VERSION 0xf8
101 #define MELFAS_TURLY_H3_FW_VERSION 0x12
102 #define MELFAS_BIEL_H1_FW_VERSION 0x06
103 #define MELFAS_BIEL_H2_FW_VERSION 0x09
104
105 #define TS_READ_HW_VER_ADDR 0xF1 //Model Dependent
106 #define TS_READ_SW_VER_ADDR 0xF5 //Model Dependent
107
108 #define MELFAS_MAX_TRANSACTION_LENGTH 66
109 #define MELFAS_MAX_I2C_TRANSFER_SIZE 7
110 #define MELFAS_I2C_DEVICE_ADDRESS_LEN 1
111 #define MELFAS_I2C_MASTER_CLOCK 100
112 #define MELFAS_I2C_ADDRESS 0x20
113
114 /*
115 MMS244 PROC LIST
116 */
117 #define MELFAS_ESD_ERROR 0x0F
118 #define TS_MAX_TOUCH 10
119 #define MMS244_CHIP 0x14
120
121 static int melfas_tpd_flag = 0;
122 static u8 * DMAbuffer_va = NULL;
123 static dma_addr_t DMAbuffer_pa = NULL;
124 static int interrupt_count = 0;
125
126 static u8 tp_factory_id_cmd = TS_TYPE_UNKNOWN;
127 static u8 hardware_ver = 0;
128
129 typedef struct muti_touch_info
130 {
131 uint16_t pos_x;
132 uint16_t pos_y;
133 uint16_t area;
134 uint16_t pressure;
135 unsigned int pressed;
136 bool updated;
137 };
138 static struct muti_touch_info g_Mtouch_info[TS_MAX_TOUCH];
139
140 //updated
141 enum tp_finger_status {
142 FINGER_UNKNOWN,
143 FINGER_DOWN,
144 FINGER_UP
145 };
146
147 static int finger_status[TS_MAX_TOUCH] = {FINGER_UNKNOWN};
148
149 #define TPD_HAVE_BUTTON
150 #ifdef TPD_HAVE_BUTTON
151 #define TPD_KEY_COUNT 3
152 #define TPD_KEYS {KEY_BACK, KEY_HOMEPAGE ,KEY_MENU}
153 #define TPD_KEYS_DIM {{200,1980,100,80},\
154 {500,1980,100,80},\
155 {800,1980,100,80}}
156 static int tpd_keys_local[TPD_KEY_COUNT] = TPD_KEYS;
157 static int tpd_keys_dim_local[TPD_KEY_COUNT][4] = TPD_KEYS_DIM;
158 #endif
159
160
161 static DECLARE_WAIT_QUEUE_HEAD(melfas_waiter);
162 //static DEFINE_MUTEX(melfas_tp_mutex);
163
164 struct i2c_client *melfas_i2c_client = NULL;
165
166 static const struct i2c_device_id melfas_tpd_id[] = {{TP_DEV_NAME,0},{}};
167 static struct i2c_board_info __initdata melfas_i2c_tpd={ I2C_BOARD_INFO(TP_DEV_NAME, MELFAS_I2C_ADDRESS)};
168
169 static int melfas_tpd_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
170 static int melfas_tpd_i2c_remove(struct i2c_client *client);
171 static int melfas_tpd_i2c_detect(struct i2c_client *client, struct i2c_board_info *info);
172 extern int isc_fw_download(struct i2c_client *client, const U8 *data, size_t len);
173 static int melfas_i2c_read(struct i2c_client *client, U16 addr, U16 len, U8 *rxbuf);
174 int melfas_i2c_DMAread(struct i2c_client *client, U16 addr, U16 len, U8 *rxbuf);
175 int melfas_i2c_DMAwrite(struct i2c_client *client, U16 addr, U16 len, U8 *txbuf);
176
177 static void melfas_ts_release_all_finger(void);
178
179
180 extern struct tpd_device *tpd;
181
182 static struct i2c_driver melfas_tpd_i2c_driver =
183 {
184 .probe = melfas_tpd_i2c_probe,
185 .remove = melfas_tpd_i2c_remove,
186 .detect = melfas_tpd_i2c_detect,
187 .driver.name = "mtk-tpd",
188 .id_table = melfas_tpd_id,
189 #ifndef CONFIG_HAS_EARLYSUSPEND
190 .suspend = melfas_ts_suspend,
191 .resume = melfas_ts_resume,
192 #endif
193
194 };
195
196 /*
197 PR 460136,turly and biel touchpanel compatibility,Firmware update series
198 Firmware definition
199 B:biel T:turly
200 R:hardware version
201 V:firmware version
202 */
203 #if 1
204 const u8 MELFAS_biel_h1_binary[] = {
205 #include "MCH_TDIABLOX_B_R01_V06.mfsb.h"
206 };
207 const u8 MELFAS_biel_h2_binary[] = {
208 //#include "MCH_TDIABLOX_B_R02_V07.mfsb.h"
209 #include "MCH_TDIABLOX_B_R02_V09.mfsb.h"
210 };
211 const u8 MELFAS_turly_h1_binary[] = {
212 #include "MCH_TDIABLOX_R01_V04.mfsb.h"
213 };
214 const u8 MELFAS_turly_h2_binary[] = {
215 #include "MCH_TDIABLOX_T_R02_VF8.mfsb.h"
216 };
217 const u8 MELFAS_turly_h3_binary[] = {
218 #include "MCH_TDIABLOX_T_R03_V12.mfsb.h"
219 };
220
221 #endif
222
223
224 #define TP_SYSFS_SUPPORT
225 #ifdef TP_SYSFS_SUPPORT
226 static ssize_t TP_value_show(struct kobject *kobj, struct kobj_attribute *attr,
227 char *buf)
228 {
229 char *s = buf;
230 int ret;
231 u8 val[7];
232
233 ret = melfas_i2c_read(melfas_i2c_client, 0xc1, 1, &val[0]);
234 ret = melfas_i2c_read(melfas_i2c_client, 0xc2, 1, &val[1]);
235 ret = melfas_i2c_read(melfas_i2c_client, 0xc3, 1, &val[2]);
236 ret = melfas_i2c_read(melfas_i2c_client, 0xc4, 1, &val[3]);
237 ret = melfas_i2c_read(melfas_i2c_client, 0xc5, 1, &val[4]);
238 ret = melfas_i2c_read(melfas_i2c_client, 0xc0, 1, &val[5]);
239
240 s += sprintf(s, "TP, mms244_ts, hardware version: %x \n", val[0]);
241 s += sprintf(s, "TP, mms244_ts, 0xC2: %x \n", val[1]);
242 s += sprintf(s, "TP, mms244_ts, firmware version: %x \n", val[2]);
243 s += sprintf(s, "TP, mms244_ts, 0xC4: %x \n", val[3]);
244 s += sprintf(s, "TP, mms244_ts, manufacture info: %x \n", val[4]);
245 s += sprintf(s, "TP, mms244_ts, 0xC0: %x \n", val[5]);
246 return (s - buf);
247 }
248
249 kal_uint8 TPD_DBG = 0;
250 static ssize_t TP_value_store(struct kobject *kobj, struct kobj_attribute *attr,
251 const char *buf, size_t n)
252 {
253 char *s = buf;
254 int save;
255 ssize_t ret_val;
256 char cmd_str[16] = {0};
257 unsigned int val;
258 sscanf(buf, "%s %d", (char *)&cmd_str, &val);
259
260 if(strcmp(cmd_str, "DEBUG") == 0) {
261 if (val == 1)
262 TPD_DBG = 1;
263 else
264 TPD_DBG = 0;
265 }
266
267 ret_val = (s - buf);
268 return ret_val;
269 }
270
271 static ssize_t debug_touch_show(struct device *dev,
272 struct device_attribute *attr, const char *buf)
273 {
274 char *s = buf;
275
276 s += sprintf(s, "TP finger status:%d %d %d %d %d \n",
277 finger_status[0],finger_status[1],finger_status[2],
278 finger_status[3],finger_status[4]);
279 s += sprintf(s, "TP finger status:%d %d %d %d %d \n",
280 finger_status[5],finger_status[6],finger_status[7],
281 finger_status[8],finger_status[9]);
282 return (s - buf);
283 }
284
285 static ssize_t debug_touch_store(struct kobject *kobj,
286 struct kobj_attribute *attr, const char *buf, size_t n)
287 {
288 int save;
289
290 if (sscanf(buf, "%d", &save)==0) {
291 printk(KERN_ERR "%s -- invalid save string '%s'...\n",
292 __func__, buf);
293 return -EINVAL;
294 }
295
296 return n;
297 }
298
299 static DEVICE_ATTR(debug_touch, 0664, debug_touch_show, debug_touch_store);
300 /*
301 PR 457838 series,cts permission issue
302 */
303 static DEVICE_ATTR(TP_DEBUG, 0644, TP_value_show, TP_value_store);
304
305 static struct attribute *TP_sysfs_attrs[] = {
306 &dev_attr_TP_DEBUG.attr,
307 &dev_attr_debug_touch.attr,
308 NULL,
309 };
310 static struct attribute_group TP_attr_group = {
311 .attrs = TP_sysfs_attrs,
312 };
313
314 //add sysfs
315 struct kobject *TP_ctrl_kobj;
316 static int TP_sysfs_init(void)
317 {
318 TP_ctrl_kobj = kobject_create_and_add("TP", NULL);
319 if (!TP_ctrl_kobj)
320 return -ENOMEM;
321
322 return sysfs_create_group(TP_ctrl_kobj, &TP_attr_group);
323 }
324 //remove sysfs
325 static void TP_sysfs_exit(void)
326 {
327 sysfs_remove_group(TP_ctrl_kobj, &TP_attr_group);
328 kobject_put(TP_ctrl_kobj);
329 }
330
331
332 #endif
333
334 void touchkey_handler(u8 key, bool on)
335 {
336 int i;
337 switch(key)
338 {
339 case 1:
340 TPD_DMESG("MMS_TOUCH_KEY_EVENT BACK, %d \n",on);
341 input_report_key(tpd->dev, KEY_BACK, on);
342 break;
343
344 case 2:
345 TPD_DMESG("MMS_TOUCH_KEY_EVENT HOME, %d \n",on);
346 input_report_key(tpd->dev, KEY_HOMEPAGE, on);
347 break;
348
349 case 3:
350 TPD_DMESG("MMS_TOUCH_KEY_EVENT MENU, %d \n",on);
351 input_report_key(tpd->dev, KEY_MENU, on);
352 break;
353
354 default:
355 break;
356 }
357 }
358
359
360 /*
361 esd performance revise
362 if some esd error happened,then we reset the tp
363 chip factory think 50 ms dalay is acceptable
364 */
365 static void esd_rest_tp(void)
366 {
367 //TPD_DEBUG("==========tp have inter esd =============\n");
368 TPD_DMESG("[melfas_tpd] %s\n", __func__);
369 mt_set_gpio_mode(GPIO_CTP_RST_PIN, GPIO_CTP_RST_PIN_M_GPIO);
370 mt_set_gpio_dir(GPIO_CTP_RST_PIN, GPIO_DIR_OUT);
371 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
372 msleep(50);
373 mt_set_gpio_mode(GPIO_CTP_RST_PIN, GPIO_CTP_RST_PIN_M_GPIO);
374 mt_set_gpio_dir(GPIO_CTP_RST_PIN, GPIO_DIR_OUT);
375 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
376 }
377
378 static int melfas_touch_event_handler(void *unused)
379 {
380 #if 1
381 u8 buf[TS_READ_REGS_LEN] = { 0 };
382 int i, read_num, fingerID, Touch_Type = 0, touchState = 0;//, keyID = 0;
383 int id = 0;
384
385 struct sched_param param = { .sched_priority = RTPM_PRIO_TPD };
386
387 sched_setscheduler(current, SCHED_RR, &param);
388
389 do {
390 //mt65xx_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
391 set_current_state(TASK_INTERRUPTIBLE);
392 wait_event_interruptible(melfas_waiter, melfas_tpd_flag != 0);
393
394 melfas_tpd_flag = 0;
395 set_current_state(TASK_RUNNING);
396
397 melfas_i2c_read(melfas_i2c_client, TS_READ_LEN_ADDR, 1, buf);
398 read_num = buf[0];
399 //TPD_DMESG("melfas,read_num = %d\n",read_num);
400 //TPD_DMESG("melfas,interrupt_count = %d \n",interrupt_count);
401 if(read_num) {
402 /*
403 int siganl rest issue,filter some error packets
404 */
405 if (((read_num % 6)!= 0) || (read_num > 60)) {
406 //mt65xx_eint_mask(CUST_EINT_TOUCH_PANEL_NUM);
407 TPD_DMESG("illegal read_num : %d\n", read_num);
408 melfas_ts_release_all_finger();
409 esd_rest_tp();
410 continue;
411 }
412
413 if (read_num <= 8)
414 melfas_i2c_read(melfas_i2c_client, TS_READ_START_ADDR, read_num, buf);
415 else if (read_num > 8)
416 melfas_i2c_DMAread(melfas_i2c_client, TS_READ_START_ADDR, read_num, buf);
417
418 /*esd error*/
419 if(buf[0] == MELFAS_ESD_ERROR) {
420 //mt65xx_eint_mask(CUST_EINT_TOUCH_PANEL_NUM);
421 TPD_DMESG("ESD error!\n");
422 melfas_ts_release_all_finger();
423 esd_rest_tp();
424 continue;
425 }
426
427 for (i = 0; i < read_num; i = i + FINGER_EVENT_SZ) {
428 Touch_Type = (buf[i] >> 5) & 0x03;
429 //TPD_DMESG("%s : touch type = %d, buf[i] = %x \n",__FUNCTION__,Touch_Type, buf[i]);
430 /* touch type is panel */
431 if (Touch_Type == MMS_TOUCH_KEY_EVENT) {
432 touchkey_handler((buf[i] & 0x0f),(bool)(buf[i] & 0x80));
433 }
434 else {
435 fingerID = (buf[i] & 0x0F) - 1;
436 touchState = ((buf[i] & 0x80) >> 7);
437
438 if ((fingerID < 0) || (fingerID >TS_MAX_TOUCH )) {
439 TPD_DMESG("illegal finger id (id: %d)\n", fingerID);
440 continue;
441 }
442 if (!touchState && !g_Mtouch_info[fingerID].pressed) {
443 TPD_DMESG("Wrong touch release (id: %d)\n", fingerID);
444 continue;
445 }
446
447 g_Mtouch_info[fingerID].pos_x = (uint16_t)(buf[i + 2] |
448 ((buf[i + 1] & 0xf) << 8));
449 g_Mtouch_info[fingerID].pos_y = (uint16_t)(buf[i + 3] |
450 (((buf[i + 1] >> 4 ) & 0xf) << 8));
451 g_Mtouch_info[fingerID].area = buf[i + 4];
452 if (touchState)
453 g_Mtouch_info[fingerID].pressure = buf[i + 5];
454 else
455 g_Mtouch_info[fingerID].pressure = 0;
456 //g_Mtouch_info[fingerID].pressed = pressed;
457 g_Mtouch_info[fingerID].pressed = touchState;
458 g_Mtouch_info[fingerID].updated = true;
459
460 /*TPD_DMESG("[MMS200]: Touch ID: %d, State : %d, x: %d, y: %d, z: %d w: %d\n",
461 fingerID,
462 touchState,
463 g_Mtouch_info[i].pos_x,
464 g_Mtouch_info[i].pos_y,
465 g_Mtouch_info[i].pressure,
466 g_Mtouch_info[i].area);*/
467 }
468
469 }
470
471 for (id = 0; id < TS_MAX_TOUCH; id++) {
472 if (!g_Mtouch_info[id].updated)
473 continue;
474 g_Mtouch_info[id].updated = false;
475
476 if (g_Mtouch_info[id].pressed) {
477 input_mt_slot(tpd->dev, id);
478 input_report_key(tpd->dev, BTN_TOUCH, 1);
479 input_report_abs(tpd->dev, ABS_MT_TRACKING_ID, id+2);
480
481 input_report_abs(tpd->dev, ABS_MT_TOUCH_MAJOR,
482 g_Mtouch_info[id].area);
483 input_report_abs(tpd->dev, ABS_MT_POSITION_X,
484 g_Mtouch_info[id].pos_x);
485 input_report_abs(tpd->dev, ABS_MT_POSITION_Y,
486 g_Mtouch_info[id].pos_y);
487 finger_status[id] = FINGER_DOWN;
488 }
489 else if (!g_Mtouch_info[id].pressed) {
490 input_mt_slot(tpd->dev, id);
491 input_report_abs(tpd->dev, ABS_MT_TRACKING_ID, -1);
492 finger_status[id] = FINGER_UP;
493 }
494
495 }
496
497 if ( (tpd != NULL) && (tpd->dev != NULL) )
498 input_sync(tpd->dev);
499 }
500
501 /*
502 the delay advised by yida.yu
503 mdelay(8);
504 */
505 //mt65xx_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
506 } while ( !kthread_should_stop() );
507
508 return 0;
509 #endif
510 }
511
512 static void melfas_i2c_tpd_eint_interrupt_handler(void)
513 {
514 //TPD_DMESG_PRINT_INT;
515 melfas_tpd_flag=1;
516 interrupt_count++;
517 wake_up_interruptible(&melfas_waiter);
518 }
519
520 int melfas_i2c_write_bytes( struct i2c_client *client, U16 addr, int len, U32 *txbuf )
521 {
522 u8 buffer[MELFAS_MAX_TRANSACTION_LENGTH]={0};
523 u16 left = len;
524 u8 offset = 0;
525 u8 retry = 0;
526
527 struct i2c_msg msg =
528 {
529 .addr = ((client->addr&I2C_MASK_FLAG )|(I2C_ENEXT_FLAG )),
530 .flags = 0,
531 .buf = buffer,
532 .timing = MELFAS_I2C_MASTER_CLOCK,
533 };
534
535
536 if ( txbuf == NULL )
537 return -1;
538
539 //TPD_DMESG("i2c_write_bytes to device %02X address %04X len %d\n", client->addr, addr, len );
540
541 while ( left > 0 )
542 {
543 retry = 0;
544
545 buffer[0] = (u8)addr+offset;
546
547 if ( left > MELFAS_MAX_I2C_TRANSFER_SIZE )
548 {
549 memcpy( &buffer[MELFAS_I2C_DEVICE_ADDRESS_LEN], &txbuf[offset], MELFAS_MAX_I2C_TRANSFER_SIZE );
550 msg.len = MELFAS_MAX_TRANSACTION_LENGTH;
551 left -= MELFAS_MAX_I2C_TRANSFER_SIZE;
552 offset += MELFAS_MAX_I2C_TRANSFER_SIZE;
553 }
554 else
555 {
556 memcpy( &buffer[MELFAS_I2C_DEVICE_ADDRESS_LEN], &txbuf[offset], left );
557 msg.len = left + MELFAS_I2C_DEVICE_ADDRESS_LEN;
558 left = 0;
559 }
560
561 //TPD_DMESG("byte left %d offset %d\n", left, offset );
562
563 while ( i2c_transfer( client->adapter, &msg, 1 ) != 1 )
564 {
565 retry++;
566
567 if ( retry == I2C_RETRY_CNT )
568 {
569 TPD_DMESG("I2C write 0x%X%X length=%d failed\n", buffer[0], buffer[1], len);
570 return -1;
571 }
572 else {
573 //TPD_DMESG("I2C write retry %d addr 0x%X%X\n", retry, buffer[0], buffer[1]);
574 }
575 }
576 }
577
578 return 0;
579 }
580
581
582 static int melfas_i2c_read(struct i2c_client *client, U16 addr, U16 len, U8 *rxbuf)
583 {
584 u8 buffer[MELFAS_I2C_DEVICE_ADDRESS_LEN]={0};
585 u8 retry;
586 u16 left = len;
587 u8 offset = 0;
588
589 struct i2c_msg msg[2] =
590 {
591 {
592 .addr = ((client->addr&I2C_MASK_FLAG )|(I2C_ENEXT_FLAG )),
593 .flags = 0,
594 .buf = buffer,
595 .len = MELFAS_I2C_DEVICE_ADDRESS_LEN,
596 .timing = MELFAS_I2C_MASTER_CLOCK
597 },
598 {
599 .addr = ((client->addr&I2C_MASK_FLAG )|(I2C_ENEXT_FLAG )),
600 .flags = I2C_M_RD,
601 .timing = MELFAS_I2C_MASTER_CLOCK
602 },
603 };
604
605 if ( rxbuf == NULL )
606 return -1;
607
608 //TPD_DMESG("i2c_read_bytes to device %02X address %04X len %d\n", client->addr, addr, len );
609
610 while ( left > 0 )
611 {
612 buffer[0] = (u8)addr+offset;
613
614 msg[1].buf = &rxbuf[offset];
615
616 if ( left > MELFAS_MAX_TRANSACTION_LENGTH )
617 {
618 msg[1].len = MELFAS_MAX_TRANSACTION_LENGTH;
619 left -= MELFAS_MAX_TRANSACTION_LENGTH;
620 offset += MELFAS_MAX_TRANSACTION_LENGTH;
621 }
622 else
623 {
624 msg[1].len = left;
625 left = 0;
626 }
627
628 retry = 0;
629
630 while ( i2c_transfer( client->adapter, &msg[0], 2 ) != 2 )
631 {
632 retry++;
633
634 if ( retry == I2C_RETRY_CNT )
635 {
636 TPD_DMESG("I2C read 0x%X length=%d failed\n", addr + offset, len);
637 TPD_DMESG("I2C read 0x%X length=%d failed\n", addr + offset, len);
638 return -1;
639 }
640 }
641 }
642
643 return 0;
644 }
645
646 int melfas_i2c_DMAread(struct i2c_client *client, U16 addr, U16 len, U8 *rxbuf)
647 {
648 int retry,i;
649
650 struct i2c_msg msg[] = {
651 {
652 .addr = client->addr & I2C_MASK_FLAG | I2C_ENEXT_FLAG,
653 .flags = 0,
654 .len = 1,
655 .buf = &addr,
656 },
657 {
658 .addr = client->addr & I2C_MASK_FLAG | I2C_ENEXT_FLAG | I2C_DMA_FLAG,
659 .flags = I2C_M_RD,
660 .len = len,
661 .buf = DMAbuffer_pa,
662 }
663 };
664 for (retry = 0; retry < I2C_RETRY_CNT; retry++) {
665 if (i2c_transfer(client->adapter, msg, 2) == 2)
666 break;
667 mdelay(5);
668 TPD_DMESG("Line %d, i2c_transfer error, retry = %d\n", __LINE__, retry);
669 }
670 if (retry == I2C_RETRY_CNT) {
671 printk(KERN_ERR "i2c_read_block retry over %d\n",I2C_RETRY_CNT);
672 return -EIO;
673 }
674
675 for(i=0;i<len;i++)
676 rxbuf[i]=DMAbuffer_va[i];
677
678 return 0;
679 }
680 int melfas_i2c_DMA_RW_isc(struct i2c_client *client,
681 int rw,
682 U8 *rxbuf, U16 rlen,
683 U8 *txbuf, U16 tlen)
684 {
685 int retry,i;
686
687 struct i2c_msg msg[] = {
688 {
689 .addr = client->addr & I2C_MASK_FLAG | I2C_ENEXT_FLAG | I2C_DMA_FLAG,
690 .flags = 0,
691 .buf = DMAbuffer_pa,
692 }
693 };
694 /*
695 rw = 1 //write
696 rw = 2 //read
697 */
698 if (rw == 1)//write
699 {
700 for(i=0;i<tlen;i++)
701 DMAbuffer_va[i] = txbuf[i];
702 msg[0].flags = 0;
703 msg[0].len = tlen;
704 for (retry = 0; retry < I2C_RETRY_CNT; retry++)
705 {
706 if (i2c_transfer(client->adapter, msg, 1) == 1)
707 break;
708 mdelay(5);
709 TPD_DMESG("Line %d, i2c_transfer error, retry = %d\n", __LINE__, retry);
710 }
711 if (retry == I2C_RETRY_CNT) {
712 printk(KERN_ERR "i2c_read_block retry over %d\n",I2C_RETRY_CNT);
713 return -EIO;
714 }
715 }
716 else if (rw == 2)//read
717 {
718 //write from TX
719 for(i=0;i<tlen;i++)
720 DMAbuffer_va[i] = txbuf[i];
721 msg[0].flags = 0;
722 msg[0].len = tlen;
723 for (retry = 0; retry < I2C_RETRY_CNT; retry++)
724 {
725 if (i2c_transfer(client->adapter, msg, 1) == 1)
726 break;
727 mdelay(5);
728 TPD_DMESG("Line %d, i2c_transfer error, retry = %d\n", __LINE__, retry);
729 }
730 if (retry == I2C_RETRY_CNT) {
731 printk(KERN_ERR "i2c_read_block retry over %d\n",I2C_RETRY_CNT);
732 return -EIO;
733 }
734 //read into RX
735 msg[0].flags = I2C_M_RD;
736 msg[0].len = rlen;
737 for (retry = 0; retry < I2C_RETRY_CNT; retry++)
738 {
739 if (i2c_transfer(client->adapter, msg, 1) == 1)
740 break;
741 mdelay(5);
742 TPD_DMESG("Line %d, i2c_transfer error, retry = %d\n", __LINE__, retry);
743 }
744 if (retry == I2C_RETRY_CNT) {
745 printk(KERN_ERR "i2c_read_block retry over %d\n",I2C_RETRY_CNT);
746 return -EIO;
747 }
748
749 for(i=0;i<rlen;i++)
750 rxbuf[i] = DMAbuffer_va[i];
751 }
752
753 return 0;
754 }
755
756 /*
757 compatibility with all hardware ver for T&B
758 Turly firmware history
759 H1:04
760 H2:06,e6-f8
761 H3:08-09
762 Biel firmware history
763 H1:04-06
764 H2:07
765 */
766 int melfas_check_firmware(struct i2c_client *client)
767 {
768 int ret = 0;
769 //uint8_t i = 0;
770 u8 val[7];
771
772 TPD_DMESG("[melfas_tpd]: i2c addr is 0x%02x",client->addr);
773 ret = melfas_i2c_read(client, 0xc3, 1, &val[0]);
774 ret = melfas_i2c_read(client, 0xc4, 1, &val[1]);
775 ret = melfas_i2c_read(client, 0xc0, 1, &val[3]);
776 ret = melfas_i2c_read(client, 0xc2, 1, &val[5]);
777 ret = melfas_i2c_read(client, 0xc1, 1, &val[4]);
778 if (ret >= 0) {
779 hardware_ver = val[4];
780 } else {
781 goto out;
782 }
783
784 ret = melfas_i2c_read(client, 0xc5, 1, &val[2]);
785 if (ret >= 0)
786 {
787 tp_factory_id_cmd = val[2];
788 TPD_DMESG("[melfas_tpd]: 0Xc3[0x%02x],0Xc4[0x%02x],0Xc5[0x%02x]",
789 val[0], val[1],val[2]);
790 if (tp_factory_id_cmd == TS_TYPE_TRULY) {
791 if (hardware_ver == 1) {
792 if (val[0] < MELFAS_TURLY_H1_FW_VERSION)
793 ret = 1;
794 else
795 ret = 0;
796 } else if (hardware_ver == 2) {
797 if (val[0] < MELFAS_TURLY_H2_FW_VERSION)
798 ret = 1;
799 else
800 ret = 0;
801 } else if (hardware_ver == 3) {
802 if (val[0] < MELFAS_TURLY_H3_FW_VERSION)
803 ret = 1;
804 else
805 ret = 0;
806 }
807 } else if (tp_factory_id_cmd == TS_TYPE_BIEL) {
808 if (hardware_ver == 1) {
809 if (val[0] < MELFAS_BIEL_H1_FW_VERSION)
810 ret = 1;
811 else
812 ret = 0;
813 } else if (hardware_ver == 2) {
814 if (val[0] < MELFAS_BIEL_H2_FW_VERSION)
815 ret = 1;
816 else
817 ret = 0;
818 }
819 }
820
821 TPD_DMESG("[melfas_tpd]: MMS_FW_VERSION is 0x%02x, IC version is 0x%02x\n",
822 MELFAS_TURLY_H2_FW_VERSION,val[0]);
823 goto out;
824 }
825 else if (ret < 0)
826 {
827 TPD_DMESG("[melfas_tpd] %s,%d: i2c read fail[%d] \n", __FUNCTION__, __LINE__, ret);
828 goto out;
829 }
830
831 out:
832 return ret;
833 }
834
835 extern void mms_fw_update_controller(const struct firmware *fw,
836 struct i2c_client *client);
837 struct firmware fw_info_turly_h3 =
838 {
839 .size = sizeof(MELFAS_turly_h3_binary),
840 .data = &MELFAS_turly_h3_binary[0],
841 };
842
843 struct firmware fw_info_turly_h2 =
844 {
845 .size = sizeof(MELFAS_turly_h2_binary),
846 .data = &MELFAS_turly_h2_binary[0],
847 };
848
849 struct firmware fw_info_turly_h1 =
850 {
851 .size = sizeof(MELFAS_turly_h1_binary),
852 .data = &MELFAS_turly_h1_binary[0],
853 };
854
855 struct firmware fw_info_biel_h1 =
856 {
857 .size = sizeof(MELFAS_biel_h1_binary),
858 .data = &MELFAS_biel_h1_binary[0],
859 };
860
861 struct firmware fw_info_biel_h2 =
862 {
863 .size = sizeof(MELFAS_biel_h2_binary),
864 .data = &MELFAS_biel_h2_binary[0],
865 };
866
867 static int melfas_firmware_update(struct i2c_client *client)
868 {
869 int ret = 0;
870 ret = melfas_check_firmware(client);
871 if (ret != 0) {
872 #if MELFAS_DOWNLOAD
873 int ver;
874 //MELFAS_binary = kstrdup(fw_name,GFP_KERNEL);
875 //ret = request_firmware_nowait(THIS_MODULE, true, fw_name, &client->dev,
876 // GFP_KERNEL, client, mms_fw_update_controller);
877 if (tp_factory_id_cmd == TS_TYPE_TRULY) {
878 if (hardware_ver == 1) {
879 TPD_DMESG("[melfas_tpd] MELFAS_bin_len = %x\n",
880 fw_info_turly_h1.size);
881 TPD_DMESG("[melfas_tpd] MELFAS_bin = %x, addr = %p\n",
882 fw_info_turly_h1.data[0],
883 fw_info_turly_h1.data);
884 mms_fw_update_controller(&fw_info_turly_h1,client);
885 } else if (hardware_ver == 2) {
886 TPD_DMESG("[melfas_tpd] MELFAS_bin_len = %x\n",
887 fw_info_turly_h2.size);
888 TPD_DMESG("[melfas_tpd] MELFAS_bin = %x, addr = %p\n",
889 fw_info_turly_h2.data[0],
890 fw_info_turly_h2.data);
891 mms_fw_update_controller(&fw_info_turly_h2,client);
892 } else if (hardware_ver == 3) {
893 TPD_DMESG("[melfas_tpd] MELFAS_bin_len = %x\n",
894 fw_info_turly_h3.size);
895 TPD_DMESG("[melfas_tpd] MELFAS_bin = %x, addr = %p\n",
896 fw_info_turly_h3.data[0],
897 fw_info_turly_h3.data);
898 mms_fw_update_controller(&fw_info_turly_h3,client);
899 }
900 } else if (tp_factory_id_cmd == TS_TYPE_BIEL) {
901 if (hardware_ver == 1) {
902 TPD_DMESG("[melfas_tpd] MELFAS_bin_len = %x\n",
903 fw_info_biel_h1.size);
904 TPD_DMESG("[melfas_tpd] MELFAS_bin = %x, addr = %p\n",
905 fw_info_biel_h1.data[0],fw_info_biel_h1.data);
906 mms_fw_update_controller(&fw_info_biel_h1,client);
907 } else if (hardware_ver == 2) {
908 TPD_DMESG("[melfas_tpd] MELFAS_bin_len = %x\n",
909 fw_info_biel_h2.size);
910 TPD_DMESG("[melfas_tpd] MELFAS_bin = %x, addr = %p\n",
911 fw_info_biel_h2.data[0],fw_info_biel_h2.data);
912 mms_fw_update_controller(&fw_info_biel_h2,client);
913 }
914 }
915 #endif
916 }
917 return ret;
918 }
919
920 void mms_reboot(void)
921 {
922 TPD_DMESG("mms_reboot\n");
923 hwPowerDown(TPD_POWER_SOURCE_CUSTOM, "TP");
924 //init GPIO pin
925 //init CE //GPIO_CTP_RST_PIN, is CE
926 mt_set_gpio_mode(GPIO_CTP_RST_PIN, GPIO_CTP_RST_PIN_M_GPIO);
927 mt_set_gpio_dir(GPIO_CTP_RST_PIN, GPIO_DIR_OUT);
928 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
929 //init EINT, mask CTP EINT //GPIO_CTP_EINT_PIN, is RSTB
930 mt_eint_mask(CUST_EINT_TOUCH_PANEL_NUM);
931 mt_set_gpio_mode(GPIO_CTP_EINT_PIN, GPIO_CTP_EINT_PIN_M_EINT);
932 mt_set_gpio_dir(GPIO_CTP_EINT_PIN, GPIO_DIR_IN);
933 mt_set_gpio_pull_enable(GPIO_CTP_EINT_PIN, GPIO_PULL_ENABLE);
934 mt_set_gpio_pull_select(GPIO_CTP_EINT_PIN, GPIO_PULL_DOWN);
935 msleep(10); //dummy delay here
936
937 //turn on VDD33, LDP_VGP4
938 hwPowerOn(TPD_POWER_SOURCE_CUSTOM, VOL_3300, "TP");
939 msleep(20); //tce, min is 0, max is ?
940
941 //set CE to HIGH
942 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
943 msleep(50); //tpor, min is 1, max is 5
944
945 //set RSTB to HIGH
946 mt_set_gpio_pull_select(GPIO_CTP_EINT_PIN, GPIO_PULL_UP);
947 msleep(50);//t boot_core, typicl is 20, max is 25ms
948 msleep(300);
949 }
950
951
952 struct completion mms200_init_done;
953
954 static int melfas_tpd_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
955 {
956 int err = 0;
957 u8 chip_info = 0;
958 int i_ret = 0;
959 int try_count = 0;
960
961 struct task_struct *thread = NULL;
962
963 TPD_DMESG("[melfas_tpd] %s\n", __func__);
964
965 //init_completion(&mms200_init_done);
966
967 #if 1//def TP_SYSFS_SUPPORT
968 //TP sysfs debug support
969 TP_sysfs_init();
970 #endif
971
972 //TP DMA support
973 if(DMAbuffer_va == NULL)
974 DMAbuffer_va = (u8 *)dma_alloc_coherent(NULL, 4096,&DMAbuffer_pa, GFP_KERNEL);
975
976 TPD_DMESG("dma_alloc_coherent va = 0x%p, pa = 0x%08x \n",DMAbuffer_va,DMAbuffer_pa);
977 if(!DMAbuffer_va)
978 {
979 TPD_DMESG("Allocate DMA I2C Buffer failed!\n");
980 return -1;
981 }
982
983 reset_proc:
984 mms_reboot();
985
986 melfas_i2c_client = client;
987
988 if (melfas_firmware_update(client) < 0)
989 {
990 //if firmware update failed, reset IC
991 //goto reset_proc;
992 }
993
994 thread = kthread_run(melfas_touch_event_handler, 0, TPD_DEVICE);
995
996 if (IS_ERR(thread))
997 {
998 err = PTR_ERR(thread);
999 TPD_DMESG(TPD_DEVICE "[melfas_tpd] failed to create kernel thread: %d\n", err);
1000 }
1001 mt_eint_set_sens(CUST_EINT_TOUCH_PANEL_NUM, 0);
1002 //mt65xx_eint_set_sens(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_SENSITIVE);
1003 mt_eint_set_hw_debounce(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_DEBOUNCE_CN);
1004 //mt_eint_registration(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_DEBOUNCE_EN, CUST_EINT_POLARITY_LOW, melfas_i2c_tpd_eint_interrupt_handler, 1);
1005 mt_eint_registration(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINTF_TRIGGER_FALLING, melfas_i2c_tpd_eint_interrupt_handler, 1);
1006 mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
1007
1008 mdelay(5);
1009
1010 /*try 3 times handshake*/
1011 do {
1012 try_count++;
1013 i_ret = melfas_i2c_read(melfas_i2c_client, MMS_CHIP_INFO, 1, &chip_info);
1014 if (i_ret < 0)
1015 mms_reboot();
1016 } while( (i_ret < 0) && (try_count <= 3) );
1017 TPD_DMESG("[melfas_tpd] communication times is %d\n", try_count);
1018 if (i_ret < 0)
1019 return -1;
1020 if (chip_info == MMS244_CHIP)
1021 TPD_DMESG("[melfas_tpd] MMS-244 was probed successfully \n");
1022 /*
1023 sometimes tp do not work,so add rst operation
1024 */
1025 #if 1
1026 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
1027 msleep(20);
1028 //power on
1029 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
1030 msleep(200);
1031 //irq unmask
1032 mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
1033 #endif
1034 tpd_load_status = 1;
1035 return 0;
1036
1037 }
1038
1039 static int melfas_tpd_i2c_remove(struct i2c_client *client)
1040 {
1041 TPD_DMESG("[melfas_tpd] %s\n", __func__);
1042 if(DMAbuffer_va) {
1043 dma_free_coherent(NULL, 4096, DMAbuffer_va, DMAbuffer_pa);
1044 DMAbuffer_va = NULL;
1045 DMAbuffer_pa = 0;
1046 }
1047 input_mt_destroy_slots(tpd->dev);
1048 TP_sysfs_exit();
1049 return 0;
1050 }
1051
1052 static int melfas_tpd_i2c_detect(struct i2c_client *client,
1053 struct i2c_board_info *info)
1054 {
1055 TPD_DMESG("[melfas_tpd] %s\n", __func__);
1056 strcpy(info->type, "mtk-tpd");
1057 return 0;
1058 }
1059 static int melfas_tpd_local_init(void)
1060 {
1061
1062 TPD_DMESG("[melfas_tpd] end %s, %d\n", __FUNCTION__, __LINE__);
1063 if(i2c_add_driver(&melfas_tpd_i2c_driver)!= 0) {
1064 TPD_DMESG("[melfas_tpd] unable to add i2c driver.\n");
1065 return -1;
1066 }
1067 if(tpd_load_status == 0) {
1068 TPD_DMESG("[melfas_tpd] add error touch panel driver.\n");
1069 i2c_del_driver(&melfas_tpd_i2c_driver);
1070 return -1;
1071 }
1072 #ifdef TPD_HAVE_BUTTON
1073 tpd_button_setting(TPD_KEY_COUNT, tpd_keys_local, tpd_keys_dim_local);
1074 #endif
1075
1076 input_mt_init_slots(tpd->dev, TS_MAX_TOUCH, 1);
1077 tpd_type_cap = 1;
1078
1079 return 0;
1080 }
1081
1082
1083 #ifdef SLOT_TYPE
1084 static void melfas_ts_release_all_finger(void)
1085 {
1086 int i;
1087 TPD_DMESG("[melfas_tpd] %s\n", __func__);
1088 for (i = 0; i < TS_MAX_TOUCH; i++)
1089 {
1090 input_mt_slot(tpd->dev, i);
1091 input_mt_report_slot_state(tpd->dev, MT_TOOL_FINGER, false);
1092 }
1093 input_sync(tpd->dev);
1094 }
1095 #else
1096 /*
1097 TP freeze workaround
1098 */
1099 static void special_ts_release_all_finger(void)
1100 {
1101 unsigned char i;
1102 TPD_DMESG("[melfas_tpd] %s\n", __func__);
1103 for(i=0; i< TS_MAX_TOUCH; i++){
1104 input_mt_slot(tpd->dev, i);
1105 input_report_abs(tpd->dev, ABS_MT_TRACKING_ID, -1);
1106 }
1107 input_sync(tpd->dev);
1108
1109 for(i=0; i< TS_MAX_TOUCH; i++){
1110 input_mt_slot(tpd->dev, i);
1111 input_report_key(tpd->dev, BTN_TOUCH, 1);
1112 input_report_abs(tpd->dev, ABS_MT_TRACKING_ID, i+2);
1113
1114 input_report_abs(tpd->dev, ABS_MT_TOUCH_MAJOR,
1115 20);
1116 input_report_abs(tpd->dev, ABS_MT_POSITION_X,
1117 1100);
1118 input_report_abs(tpd->dev, ABS_MT_POSITION_Y,
1119 0);
1120 }
1121 input_sync(tpd->dev);
1122
1123 for(i=0; i< TS_MAX_TOUCH; i++){
1124 input_mt_slot(tpd->dev, i);
1125 input_report_abs(tpd->dev, ABS_MT_TRACKING_ID, -1);
1126 }
1127 input_sync(tpd->dev);
1128 }
1129 static void melfas_ts_release_all_finger(void)
1130 {
1131 unsigned char i;
1132 TPD_DMESG("[melfas_tpd] %s\n", __func__);
1133 for(i=0; i< TS_MAX_TOUCH; i++){
1134 input_mt_slot(tpd->dev, i);
1135 input_report_abs(tpd->dev, ABS_MT_TRACKING_ID, -1);
1136 }
1137 input_sync(tpd->dev);
1138 }
1139 #endif
1140
1141 /*
1142 add power on/off operation
1143 */
1144 #ifdef CONFIG_HAS_EARLYSUSPEND
1145 static void melfas_tpd_early_suspend(struct early_suspend *h)
1146 {
1147 TPD_DMESG("[melfas_tpd] %s\n", __func__);
1148 //mutex_lock(&melfas_tp_mutex);
1149 //irq mask
1150 mt_eint_mask(CUST_EINT_TOUCH_PANEL_NUM);
1151 //power down
1152 /*hwPowerDown(TPD_POWER_SOURCE_CUSTOM, "TP");
1153 msleep(10);*/
1154 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
1155 msleep(20);
1156 special_ts_release_all_finger();
1157 //mutex_unlock(&melfas_tp_mutex);
1158 }
1159
1160 static void melfas_tpd_late_resume(struct early_suspend *h)
1161 {
1162 //int ret;
1163 //struct melfas_ts_data *ts = i2c_get_clientdata(client);
1164 //mutex_lock(&melfas_tp_mutex);
1165 TPD_DMESG("[melfas_tpd] %s\n", __func__);
1166 /*hwPowerOn(TPD_POWER_SOURCE_CUSTOM, VOL_3300, "TP");
1167 msleep(10);*/
1168 //power on
1169 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
1170 msleep(200);
1171 //irq unmask
1172 mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
1173 //mutex_unlock(&melfas_tp_mutex);
1174 }
1175 #endif
1176
1177 static struct tpd_driver_t melfas_tpd_device_driver =
1178 {
1179 .tpd_device_name = "melfas_mms200",
1180 .tpd_local_init = melfas_tpd_local_init,
1181 #ifdef CONFIG_HAS_EARLYSUSPEND
1182 .suspend = melfas_tpd_early_suspend,
1183 .resume = melfas_tpd_late_resume,
1184 #endif
1185 #ifdef TPD_HAVE_BUTTON
1186 .tpd_have_button = 1,
1187 #else
1188 .tpd_have_button = 0,
1189 #endif
1190 };
1191
1192 /* called when loaded into kernel */
1193 static int __init melfas_tpd_driver_init(void)
1194 {
1195 TPD_DMESG("[melfas_tpd] %s\n", __func__);
1196
1197 i2c_register_board_info(0, &melfas_i2c_tpd, 1);
1198 if ( tpd_driver_add(&melfas_tpd_device_driver) < 0)
1199 TPD_DMESG("[melfas_tpd] add generic driver failed\n");
1200
1201 return 0;
1202 }
1203
1204 /* should never be called */
1205 static void __exit melfas_tpd_driver_exit(void)
1206 {
1207 TPD_DMESG("[melfas_tpd] %s\n", __func__);
1208 tpd_driver_remove(&melfas_tpd_device_driver);
1209 }
1210
1211
1212 module_init(melfas_tpd_driver_init);
1213 module_exit(melfas_tpd_driver_exit);
1214