2 * drivers/input/touchscreen/ft5x0x_ts.c
4 * FocalTech ft5x0x TouchScreen driver.
6 * Copyright (c) 2010 Focal tech Ltd.
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 * note: only support mulititouch Wenfs 2010-10-01
21 #include <linux/input.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/platform_device.h>
27 #include <linux/spi/spi.h>
28 #include <linux/slab.h>
29 #include <linux/fcntl.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/timer.h>
33 #include <linux/jiffies.h>
34 #include <linux/miscdevice.h>
35 #include <linux/types.h>
37 #include <linux/delay.h>
38 #include <linux/ioport.h>
39 #include <linux/input-polldev.h>
40 #include <linux/i2c.h>
41 #include <linux/workqueue.h>
42 #ifdef CONFIG_ANDROID_POWER
43 #include <linux/android_power.h>
45 #include <mach/hardware.h>
46 #include <asm/setup.h>
47 #include <asm/mach-types.h>
48 #include <asm/mach/arch.h>
49 #include <asm/mach/map.h>
50 #include <asm/mach/flash.h>
51 #include <asm/hardware/gic.h>
52 #include <mach/irqs.h>
53 #include <mach/board.h>
54 #include <mach/gpio.h>
55 #include <mach/sram.h>
56 #ifdef CONFIG_HAS_EARLYSUSPEND
57 #include <linux/earlysuspend.h>
59 #include <linux/input/mt.h>
63 #define fts_dbg(dev, format, arg...) \
64 dev_printk(KERN_INFO , dev , format , ## arg)
66 #define fts_dbg(dev, format, arg...)
69 #define FTS_NAME "ft5x0x_ts"
70 #define FTS_I2C_RATE (400*1000)
72 #define SCREEN_MAX_Y 800
73 #define SCREEN_MAX_X 480
76 #define FTS_REPORT_RATE 0x0c //0x06 // report_rate = (FTS_REPORT_RATE)*(10Hz)
77 #define FTS_VALID_THRES 0x0a // valid_tresshold = 0x0a * 4
79 #define FTS_REG_THRES 0x80 /* Thresshold, the threshold be low, the sensitivy will be high */
80 #define FTS_REG_REPORT_RATE 0x88 /* **************report rate, in unit of 10Hz **************/
81 #define FTS_REG_PMODE 0xA5 /* Power Consume Mode 0 -- active, 1 -- monitor, 3 -- sleep */
82 #define FS_REG_FIRMID 0xA6 /* ***************firmware version **********************/
83 #define FS_REG_NOISE_MODE 0xb2 /* to enable or disable power noise, 1 -- enable, 0 -- disable */
87 FTS_PMODE_ACTIVE = 0x00,
103 u16 down_count[MAX_POINT];
105 struct workqueue_struct *freezable_work;
106 struct delayed_work work;
108 struct i2c_client *client;
110 struct input_dev *input_dev;
111 int (*platform_sleep)(void);
112 int (*platform_wakeup)(void);
113 int (*platform_init_hw)(void);
114 void (*platform_deinit_hw)(void);
115 #ifdef CONFIG_HAS_EARLYSUSPEND
116 struct early_suspend suspend;
120 static int fts_i2c_read(struct fts_data *ts, const char reg, char *buf, int len)
124 ret = i2c_master_reg8_recv(ts->client, reg, buf, len, FTS_I2C_RATE);
127 fts_dbg(ts->dev, "%s error, reg: 0x%x\n", __func__, reg);
129 ret = (ret < 0)?ret:0;
133 static int fts_i2c_write(struct fts_data *ts, const char reg, const char *buf, int len)
137 ret = i2c_master_reg8_send(ts->client, reg, buf, len , FTS_I2C_RATE);
140 fts_dbg(ts->dev, "%s error, reg: 0x%x\n", __func__, reg);
142 ret = (ret < 0)?ret:0;
146 #define FTS_PACKET_LENGTH 128
149 #include "ft_app_5306.i"
166 static int fts_ctpm_read(struct fts_data *ts, char *buf, int len)
170 ret = i2c_master_normal_recv(ts->client, buf, len , FTS_I2C_RATE);
173 fts_dbg(ts->dev, "%s error\n", __func__);
175 ret = (ret < 0)?ret:0;
179 static int fts_ctpm_write(struct fts_data *ts, char *buf, int len)
183 ret = i2c_master_normal_send(ts->client, buf, len , FTS_I2C_RATE);
186 fts_dbg(ts->dev, "%s error\n", __func__);
188 ret = (ret < 0)?ret:0;
193 E_UPGRADE_ERR_TYPE fts_ctpm_fw_upgrade(struct fts_data *ts, u8* pbt_buf, int dw_lenth)
196 int retry = 10, ret = 0, tmp;
197 int i, j, i_is_new_protocol = 0, packet_number;
198 u8 packet_buf[FTS_PACKET_LENGTH + 6];
202 fts_i2c_write(ts, 0xfc, val, 1);
205 fts_i2c_write(ts, 0xfc, val, 1);
206 fts_dbg(ts->dev, "[TSP] Step 1: Reset CTPM\n");
208 fts_dbg(ts->dev, "[TSP] Step 2:enter new update mode\n");
213 ret = fts_ctpm_write(ts, val, 2);
214 }while(retry-- && (ret < 0));
217 i_is_new_protocol = 1;
223 fts_ctpm_write(ts, val, 4);
225 fts_ctpm_read(ts, val, 2);
226 fts_dbg(ts->dev, "[TSP] Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",val[0],val[1]);
227 if(val[0] != 0x79 || val[1] != 0x03)
230 if(i_is_new_protocol)
238 fts_ctpm_write(ts, val, 1);
241 fts_dbg(ts->dev,"[TSP] Step 4: erase. \n");
243 dw_lenth = dw_lenth - 8;
244 packet_number = (dw_lenth) / FTS_PACKET_LENGTH;
245 fts_dbg(ts->dev,"[TSP] Step 5: start upgrade, packet_number = %d\n", packet_number);
247 packet_buf[0] = 0xbf;
248 packet_buf[1] = 0x00;
250 for(i = 0; i < packet_number; i++){
251 tmp = i * FTS_PACKET_LENGTH;
252 packet_buf[2] = (u8)(tmp >> 8);
253 packet_buf[3] = (u8)tmp ;
254 tmp = FTS_PACKET_LENGTH;
255 packet_buf[4] = (u8)(tmp >> 8);
256 packet_buf[5] = (u8)(tmp);
258 for(j = 0; j < FTS_PACKET_LENGTH; j++){
259 packet_buf[6+j] = pbt_buf[i*FTS_PACKET_LENGTH + j];
260 bt_ecc ^= packet_buf[6+j];
262 fts_ctpm_write(ts, packet_buf, FTS_PACKET_LENGTH + 6);
263 mdelay(FTS_PACKET_LENGTH/6 + 1);
265 if ((i * FTS_PACKET_LENGTH % 1024) == 0)
267 fts_dbg(ts->dev, "[TSP] upgrade the 0x%x th byte.\n", ((unsigned int)i) * FTS_PACKET_LENGTH);
271 if ((dw_lenth) % FTS_PACKET_LENGTH > 0)
273 tmp = packet_number * FTS_PACKET_LENGTH;
274 packet_buf[2] = (u8)(tmp>>8);
275 packet_buf[3] = (u8)tmp;
277 tmp = (dw_lenth) % FTS_PACKET_LENGTH;
278 packet_buf[4] = (u8)(tmp>>8);
279 packet_buf[5] = (u8)tmp;
283 packet_buf[6+i] = pbt_buf[packet_number*FTS_PACKET_LENGTH + i];
284 bt_ecc ^= packet_buf[6+i];
287 fts_ctpm_write(ts, packet_buf, tmp + 6);
291 for (i = 0; i<6; i++)
294 packet_buf[2] = (u8)(tmp>>8);
295 packet_buf[3] = (u8)tmp;
296 packet_buf[4] = (u8)(1>>8);
297 packet_buf[5] = (u8)1;
298 packet_buf[6] = pbt_buf[dw_lenth + i];
299 bt_ecc ^= packet_buf[6];
301 fts_ctpm_write(ts, packet_buf,7);
309 fts_ctpm_write(ts, val, 1);
311 fts_ctpm_read(ts, val, 1);
312 fts_dbg(ts->dev,"[TSP] Step 6: ecc read 0x%x, new firmware 0x%x. \n", val[0], bt_ecc);
321 fts_ctpm_write(ts, val, 1);
324 fts_dbg(ts->dev, "[TSP] Step 1: Reset new FM\n");
325 fts_i2c_read(ts, 0xfc, val, 1);
328 fts_i2c_write(ts, 0xfc, val, 1);
329 mdelay(2500);//2500ms
331 fts_i2c_read(ts, 0xfc, val, 1);
333 }while(retry-- && val[0] != 1);
336 return -ERR_RESET_FM;
339 fts_dbg(ts->dev, "[TSP] %s ok\n", __func__);
342 int fts_ctpm_fw_upgrade_with_i_file(struct fts_data *ts)
348 ret = fts_ctpm_fw_upgrade(ts, pbt_buf, sizeof(CTPM_FW));
352 unsigned char fts_ctpm_get_upg_ver(void)
356 ui_sz = sizeof(CTPM_FW);
359 return CTPM_FW[ui_sz - 2];
365 static int fts_update_config(struct fts_data *ts)
367 char old_ver, new_ver;
370 ret = fts_i2c_read(ts, FS_REG_FIRMID, &old_ver, 1);
372 dev_err(ts->dev, "%s: i2c read version error\n", __func__);
376 if(fts_ctpm_get_upg_ver() != old_ver){
378 ret = fts_ctpm_fw_upgrade_with_i_file(ts);
380 dev_err(ts->dev, "%s: failed to ugrade\n", __func__);
384 fts_i2c_read(ts, FS_REG_FIRMID, &new_ver, 1);
385 dev_info(ts->dev, "Update from old version[0x%2x] to new version[0x%2x]\n", old_ver, new_ver);
391 static void fts_work(struct work_struct *work)
395 struct fts_data *ts = container_of(work, struct fts_data, work.work);
397 buf[0] = FTS_REPORT_RATE;
398 ret = fts_i2c_write(ts, FTS_REG_REPORT_RATE, &buf[0], 1);
400 ret = fts_i2c_read(ts, FTS_REG_REPORT_RATE, &buf[0], 1);
402 dev_err(ts->dev, "%s: i2c(r/w) error, reg: 0x%x\n", __func__, FTS_REG_REPORT_RATE);
406 buf[1] = FTS_VALID_THRES;
407 ret = fts_i2c_write(ts, FTS_REG_THRES, &buf[1], 1);
409 ret = fts_i2c_read(ts, FTS_REG_THRES, &buf[1], 1);
411 dev_err(ts->dev, "%s: i2c(r/w), reg: 0x%x\n", __func__, FTS_REG_THRES);
414 if(buf[0] == FTS_REPORT_RATE && buf[1] == FTS_VALID_THRES){
415 dev_info(ts->dev, "%s: report rate: %dHz, valide thres: %d\n", __func__, buf[0]*10, buf[1]*4);
420 queue_delayed_work(ts->freezable_work, &ts->work, msecs_to_jiffies(1000));
425 static void fts_report_value(struct fts_data *ts)
427 char buf[6*MAX_POINT+1], reg;
428 struct fts_event events[MAX_POINT], ev;
432 ret = fts_i2c_read(ts, reg, buf, 6*MAX_POINT+1);
435 dev_err(ts->dev, "fts_i2c_write error:%d!\n",ret);
438 memset(events, 0, sizeof(struct fts_event) * MAX_POINT);
439 ts->touch_point = buf[2]&0x07;
441 if(ts->touch_point == 0){
442 for(i = 0; i < MAX_POINT; i++){
443 if(ts->flags[i] != 0){
444 fts_dbg(ts->dev, "Point UP: id: %d, down_count: %d\n",i, ts->down_count[i]);
446 ts->down_count[i] = 0;
447 input_mt_slot(ts->input_dev, i);
448 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, false);
451 input_sync(ts->input_dev);
455 for(i = 0; i < ts->touch_point; i++){
457 ev.id = (s16)(buf[off+2] & 0xF0)>>4;
458 if(unlikely((ev.id >= MAX_POINT) || (ev.id < 0))){
459 dev_err(ts->dev, "read touch_id[%d] error\n", ev.id);
462 ev.x = ((buf[off+0] & 0x0F)<<8) | buf[off+1];
463 ev.y = ((buf[off+2] & 0x0F)<<8) | buf[off+3];
464 if(unlikely(ev.x > SCREEN_MAX_X || ev.y > (SCREEN_MAX_Y + 60))){
465 dev_err(ts->dev, "read pos[x:%d, y:%d] error\n", ev.x, ev.y);
468 ev.flag = (buf[off+0] & 0xc0) >> 6;
470 events[ev.id].x = ev.x;
471 events[ev.id].y = ev.y;
472 events[ev.id].flag = ev.flag;
473 events[ev.id].pressure = 200;
475 fts_dbg(ts->dev, "get event: id: %d, x: %d, y: %d, flag:%d\n",ev.id, ev.x, ev.y, ev.flag);
477 for(i = 0; i < MAX_POINT; i++){
478 if((events[i].flag == 0) && (ts->flags[i] != 0)){
479 fts_dbg(ts->dev, "Point UP: id: %d, x: %d, y: %d, down_count: %d\n",
480 i, events[i].x, events[i].y, ts->down_count[i]);
481 ts->down_count[i] = 0;
482 input_mt_slot(ts->input_dev, i);
483 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, false);
484 }else if(events[i].flag != 0){
485 fts_dbg(ts->dev, "Point DOWN: id: %d, x: %d, y: %d\n",i, events[i].x, events[i].y);
487 input_mt_slot(ts->input_dev, i);
488 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
489 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1);
490 //input_report_abs(data->input_dev, ABS_MT_PRESSURE, event.pressure);
491 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, events[i].x);
492 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, events[i].y);
494 ts->flags[i] = events[i].flag;
496 input_sync(ts->input_dev);
500 static irqreturn_t fts_interrupt(int irq, void *dev_id)
502 struct fts_data *ts = dev_id;
504 disable_irq_nosync(ts->irq);
505 fts_report_value(ts);
509 #ifdef CONFIG_HAS_EARLYSUSPEND
510 void fts_early_suspend(struct early_suspend *h)
512 struct fts_data *ts = container_of(h, struct fts_data, suspend);
514 char buf = FTS_PMODE_HIBERNATE;
517 disable_irq(ts->irq);
518 cancel_delayed_work(&ts->work);
519 ret = fts_i2c_write(ts, FTS_REG_PMODE, &buf, 1);
522 dev_err(ts->dev, "%s: i2c_write error, reg = 0x%x\n", __func__, FTS_REG_PMODE);
523 if(ts->platform_sleep)
524 ts->platform_sleep();
527 void fts_early_resume(struct early_suspend *h)
529 struct fts_data *ts = container_of(h, struct fts_data, suspend);
530 if(ts->platform_wakeup)
531 ts->platform_wakeup();
533 queue_delayed_work(ts->freezable_work, &ts->work, msecs_to_jiffies(1000));
539 static struct i2c_device_id fts_idtable[] = {
544 static int fts_probe(struct i2c_client *client ,const struct i2c_device_id *id)
547 struct ft5x0x_platform_data *pdata = client->dev.platform_data;
550 int retrys = 5, ret = 0;
553 dev_err(&client->dev, "no platform data\n");
557 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)){
558 dev_err(&client->dev, "Must have I2C_FUNC_I2C.\n");
563 dev_err(&client->dev, "no irq.\n");
567 ts = kzalloc(sizeof(struct fts_data), GFP_KERNEL);
569 dev_err(&client->dev, "No memory for fts\n");
573 ts->dev = &client->dev;
574 ts->irq = gpio_to_irq(client->irq);
575 ts->platform_wakeup = pdata->ft5x0x_platform_wakeup;
576 ts->platform_sleep = pdata->ft5x0x_platform_sleep;
577 ts->platform_init_hw = pdata->init_platform_hw;
578 ts->platform_deinit_hw = pdata->exit_platform_hw;
580 i2c_set_clientdata(client, ts);
582 if (pdata->init_platform_hw)
583 pdata->init_platform_hw();
585 buf[0] = FTS_PMODE_MONITOR;
587 ret = fts_i2c_write(ts, FTS_REG_PMODE, buf, 1);
593 dev_err(ts->dev, "fts_i2c_write error, reg: 0x%x\n", FTS_REG_PMODE);
597 ts->input_dev = input_allocate_device();
598 if (!ts->input_dev) {
600 dev_err(ts->dev, "failed to allocate input device\n");
601 goto err_input_allocate_device;
604 __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
605 __set_bit(EV_ABS, ts->input_dev->evbit);
607 input_mt_init_slots(ts->input_dev, MAX_POINT);
608 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
609 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, SCREEN_MAX_X, 0, 0);
610 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, SCREEN_MAX_Y, 0, 0);
612 ts->input_dev->name = "ft5x0x_ts-touchscreen"; //dev_name(&client->dev)
613 ret = input_register_device(ts->input_dev);
615 dev_err(ts->dev,"failed to register input device: \n");
616 goto err_input_register_device;
619 ret = fts_update_config(ts);
621 dev_err(ts->dev, "failed to fts_update_config\n");
622 goto err_fts_update_config;
625 #ifdef CONFIG_HAS_EARLYSUSPEND
626 ts->suspend.suspend =fts_early_suspend;
627 ts->suspend.resume =fts_early_resume;
628 ts->suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;;
629 register_early_suspend(&ts->suspend);
632 ts->freezable_work = system_freezable_wq;
633 INIT_DELAYED_WORK(&ts->work, fts_work);
635 buf[0] = FTS_REPORT_RATE;
636 ret = fts_i2c_write(ts, FTS_REG_REPORT_RATE, &buf[0], 1);
638 ret = fts_i2c_read(ts, FTS_REG_REPORT_RATE, &buf[0], 1);
640 dev_err(&client->dev, "fts_i2c_read error, reg: 0x%x\n", FTS_REG_REPORT_RATE);
641 goto err_request_irq;
644 buf[1] = FTS_VALID_THRES;
645 ret = fts_i2c_write(ts, FTS_REG_THRES, &buf[1], 1);
647 ret = fts_i2c_read(ts, FTS_REG_THRES, &buf[1], 1);
649 dev_err(&client->dev, "fts_i2c_read error, reg: 0x%x\n", FTS_REG_THRES);
650 goto err_request_irq;
652 ret = request_threaded_irq(ts->irq, NULL, fts_interrupt, IRQF_TRIGGER_FALLING, client->dev.driver->name, ts);
654 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
655 goto err_request_irq;
658 dev_info(ts->dev, "%s ok, i2c addr: 0x%x, report rate: %dHz, active thres: %d\n",
659 __func__, ts->client->addr, buf[0]*10, buf[1]*4);
664 #ifdef CONFIG_HAS_EARLYSUSPEND
665 unregister_early_suspend(&ts->suspend);
667 err_fts_update_config:
668 input_unregister_device(ts->input_dev);
669 err_input_register_device:
670 input_free_device(ts->input_dev);
671 err_input_allocate_device:
673 i2c_set_clientdata(client, NULL);
675 if (pdata->exit_platform_hw)
676 pdata->exit_platform_hw();
680 static int __devexit fts_remove(struct i2c_client *client)
682 struct fts_data *ts = i2c_get_clientdata(client);
684 free_irq(ts->irq,ts);
685 #ifdef CONFIG_HAS_EARLYSUSPEND
686 unregister_early_suspend(&ts->suspend);
688 input_unregister_device(ts->input_dev);
689 input_free_device(ts->input_dev);
690 i2c_set_clientdata(client, NULL);
692 if(ts->platform_deinit_hw)
693 ts->platform_deinit_hw();
696 MODULE_DEVICE_TABLE(i2c, fts_idtable);
698 static struct i2c_driver fts_driver = {
700 .owner = THIS_MODULE,
703 .id_table = fts_idtable,
705 .remove = __devexit_p(fts_remove),
708 static int __init fts_ts_init(void)
710 return i2c_add_driver(&fts_driver);
713 static void __exit fts_ts_exit(void)
715 i2c_del_driver(&fts_driver);
718 module_init(fts_ts_init);
719 module_exit(fts_ts_exit);
721 MODULE_AUTHOR("<kfx@rock-chips.com>");
722 MODULE_DESCRIPTION("FocalTech ft5x0x TouchScreen driver");