2 * drivers/input/touchscreen/gt801_ts.c
4 * Copyright (C) 2010 ROCKCHIP, Inc.
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/earlysuspend.h>
19 #include <linux/hrtimer.h>
20 #include <linux/i2c.h>
21 #include <linux/input.h>
22 #include <linux/input/mt.h>
23 #include <linux/interrupt.h>
25 #include <linux/slab.h>
26 #include <linux/gpio.h>
27 #include <mach/iomux.h>
28 #include <mach/board.h>
29 #include <linux/platform_device.h>
33 #define MYCT360_DEBUG 0
36 #define ct360printk(msg...) printk(msg);
38 #define ct360printk(msg...)
42 #define yj_printk(msg...) printk(msg);
44 #define yj_printk(msg...)
48 #define boot_printk(msg...) printk(msg);
50 #define boot_printk(msg...)
54 #define myct360printk(msg...) printk(msg);
56 #define myct360printk(msg...)
59 #define IOMUX_NAME_SIZE 48
62 ptxh = 0, ptxl = 1, ptyh = 2, ptyl = 3, ptpressure = 4,
65 TOUCH_UP = 0, TOUCH_DOWN = 1,
69 #define TOUCH_NUMBER 10
70 #define TOUCH_REG_NUM 6
72 #define ct360_TS_NAME "ct3610_ts"
74 struct ct360_ts_data {
77 bool swap_xy; //define?
79 struct i2c_client *client;
80 struct input_dev *input_dev;
81 struct workqueue_struct *ct360_wq;
82 struct work_struct work;
83 struct early_suspend early_suspend;
85 /*tochscreen private data*/
86 static int touch_state[TOUCH_NUMBER] = {TOUCH_UP,TOUCH_UP};
88 #ifdef CONFIG_HAS_EARLYSUSPEND
89 static void ct360_ts_early_suspend(struct early_suspend *h);
90 static void ct360_ts_late_resume(struct early_suspend *h);
93 /*read the ct360 register ,used i2c bus*/
94 static int ct360_read_regs(struct i2c_client *client, u8 buf[], unsigned len)
97 ret =i2c_master_normal_recv(client, buf, len, 400*1000);
99 printk("ct360_ts_work_func:i2c_transfer fail =%d\n",ret);
102 /* set the ct360 registe,used i2c bus*/
103 static int ct360_write_regs(struct i2c_client *client, u8 reg, u8 const buf[], unsigned short len)
106 ret = i2c_master_reg8_send(client,reg, buf, len, 100*1000);
108 printk("ct360_ts_work_func:i2c_transfer fail =%d\n",ret);
113 char const Binary_Data[32768]=
115 //#include "CT365RC972030D_V39120329A_waterproof_1.dat"
116 #include "CT36X_JS_DS_973H_LX20x30_V18120810W.txt"
119 char CTP_BootLoader(struct ct360_ts_data *ts)
123 unsigned int version = 0;
125 char I2C_Buf[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
126 unsigned int Flash_Address = 0 ;
127 char CheckSum[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // 128/8 = 16 times
129 //--------------------------------------
130 // Step1 --> initial BootLoader
131 // Note. 0x7F -> 0x00 -> 0xA5 ;
132 //--------------------------------------
136 ts->client->addr = 0x7F;
137 ct360_write_regs(ts->client,0x00,&I2C_Buf[1], 1); // Host issue 0xA5 Command to CT365
138 mdelay(3); // Delay 1 ms
141 //------------------------------
142 // Reset I2C Offset address
143 // Note. 0x7F -> 0x00
144 //------------------------------
146 i2c_master_normal_send(ts->client,I2C_Buf, 1,100*1000); // Reset CT365 I2C Offset address
147 udelay(500); // Delay 500 us
149 //------------------------------
150 // Read I2C Bus status
151 //------------------------------
152 i2c_master_normal_recv(ts->client,&value, 1,100*1000);
154 boot_printk("%s......0...\n",__FUNCTION__);
155 // if return "AAH" then going next step
159 boot_printk("%s......1...\n",__FUNCTION__);
163 I2C_Buf[1] = 0x99; // Generate check sum command
164 I2C_Buf[2] = (char)(0x0044 >> 8); // define a flash address for CT365 to generate check sum
165 I2C_Buf[3] = (char)(0x0044 & 0xFF); //
166 I2C_Buf[4] = 0x08; // Define a data length for CT365 to generate check sum
168 i2c_master_normal_send(ts->client,I2C_Buf, 5,100*1000); // Write Genertate check sum command to CT365
170 mdelay(1); // Delay 1 ms
172 i2c_master_reg8_recv(ts->client,0x00, I2C_Buf,13, 100*1000); // Read check sum and flash data from CT365
173 if (!(I2C_Buf[5] != 'V') || (I2C_Buf[9] != 'T'))
179 I2C_Buf[1] = 0x99; // Generate check sum command
180 I2C_Buf[2] = (char)(0x00a4 >> 8); // define a flash address for CT365 to generate check sum
181 I2C_Buf[3] = (char)(0x00a4 & 0xFF); //
182 I2C_Buf[4] = 0x08; // Define a data length for CT365 to generate check sum
184 i2c_master_normal_send(ts->client,I2C_Buf, 5,100*1000); // Write Genertate check sum command to CT365
186 mdelay(1); // Delay 1 ms
188 i2c_master_reg8_recv(ts->client,0x00, I2C_Buf,13, 100*1000); // Read check sum and flash data from CT365
189 if (!(I2C_Buf[5] != 'V') || (I2C_Buf[9] != 'T'))
196 //------------------------------
197 // Reset I2C Offset address
198 // Note. 0x7F -> 0x00
199 //------------------------------
203 i2c_master_normal_send(ts->client,I2C_Buf, 2,100*1000); // Host issue 0xA5 Command to CT365
207 i2c_master_normal_send(ts->client,I2C_Buf, 1,100*1000); // Reset CT365 I2C Offset address
209 //-----------------------------------------------------
210 // Step 2 : Erase 32K flash memory via Mass Erase (33H)
211 // 0x7F --> 0x00 --> 0x33 --> 0x00 ;
212 //-----------------------------------------------------
213 I2C_Buf [0] = 0x00; // Offset address
214 I2C_Buf [1] = 0x33; // Mass Erase command
217 i2c_master_normal_send(ts->client,I2C_Buf,3,100*1000);
218 mdelay(10); // Delay 10 mS
221 //------------------------------
222 // Reset I2C Offset address
223 // Note. 0x7F -> 0x00
224 //------------------------------
226 i2c_master_normal_send(ts->client,I2C_Buf, 1,100*1000); // Reset CT365 I2C Offset address
227 udelay(500); // Delay 500 us
230 //------------------------------
231 // Read I2C Bus status
232 //------------------------------
233 i2c_master_normal_recv(ts->client,&value, 1,100*1000);
235 // if return "AAH" then going next step
239 boot_printk("%s......2...\n",__FUNCTION__);
241 //----------------------------------------
242 // Step3. Host write 128 bytes to CT365
243 // Step4. Host read checksum to verify ;
244 // Write/Read for 256 times ( 32k Bytes )
245 //----------------------------------------
247 for ( j = 0 ; j < 256 ; j++ ) // 32k/128 = 256 times
249 Flash_Address = 128*j ; // 0 ~ 127 ; 128 ~ 255 ;
251 for ( i = 0 ; i < 16 ; i++ ) // 128/8 = 16 times for One Row program
253 // Step 3 : write binary data to CT365
254 I2C_Buf[0] = 0x00; // Offset address
255 I2C_Buf[1] = 0x55; // Flash write command
256 I2C_Buf[2] = (char)(Flash_Address >> 8); // Flash address [15:8]
257 I2C_Buf[3] = (char)(Flash_Address & 0xFF); // Flash address [7:0]
258 I2C_Buf[4] = 0x08; // Data Length
259 I2C_Buf[6] = Binary_Data[Flash_Address + 0]; // Binary data 1
260 I2C_Buf[7] = Binary_Data[Flash_Address + 1]; // Binary data 2
261 I2C_Buf[8] = Binary_Data[Flash_Address + 2]; // Binary data 3
262 I2C_Buf[9] = Binary_Data[Flash_Address + 3]; // Binary data 4
263 I2C_Buf[10] = Binary_Data[Flash_Address + 4]; // Binary data 5
264 I2C_Buf[11] = Binary_Data[Flash_Address + 5]; // Binary data 6
265 I2C_Buf[12] = Binary_Data[Flash_Address + 6]; // Binary data 7
266 I2C_Buf[13] = Binary_Data[Flash_Address + 7]; // Binary data 8
268 // Calculate a check sum by Host controller.
269 // Checksum = / (FLASH_ADRH+FLASH_ADRL+LENGTH+
270 // Binary_Data1+Binary_Data2+Binary_Data3+Binary_Data4+
271 // Binary_Data5+Binary_Data6+Binary_Data7+Binary_Data8) + 1
272 CheckSum[i] = ~(I2C_Buf[2] + I2C_Buf[3] + I2C_Buf[4] + I2C_Buf[6] + I2C_Buf[7] +
273 I2C_Buf[8] + I2C_Buf[9] + I2C_Buf[10] + I2C_Buf[11] + I2C_Buf[12] +
276 I2C_Buf[5] = CheckSum[i]; // Load check sum to I2C Buffer
278 i2c_master_normal_send(ts->client,I2C_Buf, 14,100*1000); // Host write I2C_Buf[0¡K12] to CT365.
280 mdelay(1); // 8 Bytes program --> Need 1 ms delay time
282 Flash_Address += 8 ; // Increase Flash Address. 8 bytes for 1 time
286 mdelay(20); // Each Row command --> Need 20 ms delay time
288 Flash_Address = 128*j ; // 0 ~ 127 ; 128 ~ 255 ;
290 // Step4. Verify process
291 for ( i = 0 ; i < 16 ; i++ ) // 128/8 = 16 times for One Row program
293 //Step 4 : Force CT365 generate check sum for host to compare data.
294 //Prepare get check sum from CT365
296 I2C_Buf[1] = 0x99; // Generate check sum command
297 I2C_Buf[2] = (char)(Flash_Address >> 8); // define a flash address for NT1100x to generate check sum
298 I2C_Buf[3] = (char)(Flash_Address & 0xFF); //
299 I2C_Buf[4] = 0x08; // Define a data length for CT36x to generate check sum
301 i2c_master_normal_send(ts->client,I2C_Buf, 5,100*1000); // Write Genertate check sum command to CT365
303 mdelay(1); // Delay 1 ms
305 i2c_master_reg8_recv(ts->client,0x00,I2C_Buf, 13,100*1000); // Read check sum and flash data from CT365
307 // Compare host check sum with CT365 check sum(I2C_Buf[4])
308 if ( I2C_Buf[4] != CheckSum[i] )
310 boot_printk("%s......3...\n",__FUNCTION__);
314 Flash_Address += 8; // Increase Flash Address.
320 ts->client->addr = 0x01;
321 boot_printk("%s......4...\n",__FUNCTION__);
326 static void ct360_ts_work_func(struct work_struct *work)
331 int i,ret,syn_flag=0;
333 unsigned char buf[TOUCH_REG_NUM*TOUCH_NUMBER+1] = {0};
335 int point_num,point_pressure;
337 struct ct360_ts_data *ts = container_of(work,struct ct360_ts_data,work);
339 ret= ct360_read_regs(ts->client,buf,60);//only one data represent the current touch num
341 printk("%s:i2c_transfer fail =%d\n",__FUNCTION__,toatl_num);
346 for (i=0; i<60; i+=TOUCH_REG_NUM)
348 if (((buf[i+1] << 4)|(buf[i+3] >> 4)) != 0x0fff)
350 x = ((buf[i+1] << 4)|(buf[i+3] >> 4));
351 y = ((buf[i+2] << 4)|(buf[i+3]&0x0F));
352 point_status=buf[i]&0x07;
353 point_num = buf[i]>>3;
354 point_pressure = (buf[i+5]*4)>=255? 255 : (buf[i+5]*4);
356 if((point_status == 1) || (point_status == 2)){
357 input_mt_slot(ts->input_dev, point_num-1);
358 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, point_num-1);
359 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
360 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
361 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, point_pressure); //Finger Size
362 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, point_pressure); //Touch Size
363 //input_mt_sync(ts->input_dev);
365 touch_state[point_num-1] = TOUCH_DOWN;
367 else if(point_status == 3){
368 input_mt_slot(ts->input_dev, point_num-1);
369 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, -1);
370 touch_state[point_num-1] = TOUCH_UP;
377 input_sync(ts->input_dev);
385 static irqreturn_t ct360_ts_irq_handler(int irq, void *dev_id)
387 struct ct360_ts_data *ts = dev_id;
389 disable_irq_nosync(ts->irq);
390 queue_work(ts->ct360_wq, &ts->work);
395 static int ct360_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
397 struct ct360_ts_data *ts;
398 struct ct360_platform_data *pdata = client->dev.platform_data;
400 char loader_buf[2] = {0x3f,0xff};
402 char boot_loader[2] = {0};
403 printk("%s \n",__FUNCTION__);
406 dev_err(&client->dev, "empty platform_data\n");
407 goto err_check_functionality_failed;
409 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
410 printk(KERN_ERR "ct360_ts_probe: need I2C_FUNC_I2C\n");
412 goto err_check_functionality_failed;
415 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
418 goto err_alloc_data_failed;
421 ts->ct360_wq = create_singlethread_workqueue("ct360_wq");
423 printk(KERN_ERR"%s: create workqueue failed\n", __func__);
425 goto err_input_dev_alloc_failed;
428 INIT_WORK(&ts->work, ct360_ts_work_func);
431 i2c_set_clientdata(client, ts);
444 //¼Ó40msÑÓʱ£¬·ñÔò¶ÁÈ¡³ö´í¡£¡£
446 ret=ct360_write_regs(client,0xfF, loader_buf, 2);
448 printk("\n--%s--Set Register values error !!!\n",__FUNCTION__);
452 printk("%s...........%d\n",__FUNCTION__,boot_buf);
453 ret = i2c_master_normal_send(client,boot_loader,1,100*1000);
455 printk("ct360_ts_probe:sdf i2c_transfer fail =%d\n",ret);
457 printk("%s.............ok\n",__FUNCTION__);
460 ret = ct360_read_regs(client,&boot_buf,1);
461 printk("%s....3......%x\n",__FUNCTION__,boot_buf);
464 printk("ct360_ts_probe:i2c_transfer fail =%d\n",ret);
466 printk("%s.............boot_buf=%d\n",__FUNCTION__,boot_buf);
468 if (Binary_Data[32756] != boot_buf)
470 printk("start Bootloader ...........boot_Buf=%x.....%d......%x..........TP \n\n",boot_buf,(Binary_Data[16372]-boot_buf),Binary_Data[16372]);
471 ret = CTP_BootLoader(ts);
473 printk("TP Bootloader success\n");
475 printk("TP Bootloader failed ret=%d\n",ret);
476 printk("stop Bootloader.................................TP \n\n");
480 printk("Don't need bootloader.skip it %x \n",Binary_Data[16372]);
491 ts->client->addr = 0x01;
493 /* allocate input device */
494 ts->input_dev = input_allocate_device();
495 if (ts->input_dev == NULL) {
497 printk(KERN_ERR "%s: Failed to allocate input device\n",__FUNCTION__);
498 goto err_input_dev_alloc_failed;
501 ts->x_max = pdata->x_max;
502 ts->y_max = pdata->y_max;
504 ts->input_dev->name = ct360_TS_NAME;
505 ts->input_dev->dev.parent = &client->dev;
507 //set_bit(EV_SYN, ts->input_dev->evbit);
508 //set_bit(BTN_TOUCH, ts->input_dev->keybit);
509 set_bit(EV_ABS, ts->input_dev->evbit);
510 set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
511 //set_bit(BTN_2, ts->input_dev->keybit);
513 input_mt_init_slots(ts->input_dev, TOUCH_NUMBER);
514 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, ts->x_max, 0, 0);
515 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, ts->y_max, 0, 0);
516 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); //Finger Size
517 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0); //Touch Size
518 input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID, 0, 10, 0, 0); //Touch Size
520 ret = input_register_device(ts->input_dev);
522 printk(KERN_ERR "%s: Unable to register %s input device\n", __FUNCTION__,ts->input_dev->name);
523 goto err_input_register_device_failed;
526 ts->irq = gpio_to_irq(client->irq);
527 ret = request_irq(ts->irq, ct360_ts_irq_handler, IRQF_TRIGGER_FALLING, client->name, ts);
529 printk("!!! ct360 request_irq failed\n");
530 goto err_input_register_device_failed;
533 #ifdef CONFIG_HAS_EARLYSUSPEND
534 ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
535 ts->early_suspend.suspend = ct360_ts_early_suspend;
536 ts->early_suspend.resume = ct360_ts_late_resume;
537 register_early_suspend(&ts->early_suspend);
540 printk("%s: probe ok!!\n", __FUNCTION__);
544 err_input_register_device_failed:
545 input_free_device(ts->input_dev);
546 err_input_dev_alloc_failed:
548 err_alloc_data_failed:
549 err_check_functionality_failed:
554 static int ct360_ts_remove(struct i2c_client *client)
556 struct ct360_ts_data *ts = i2c_get_clientdata(client);
557 unregister_early_suspend(&ts->early_suspend);
558 free_irq(ts->irq, ts);
559 input_unregister_device(ts->input_dev);
561 destroy_workqueue(ts->ct360_wq);
567 static int ct360_ts_suspend(struct i2c_client *client, pm_message_t mesg)
569 struct ct360_ts_data *ts = i2c_get_clientdata(client);
572 char buf[3] = {0xff,0x8f,0xff};
574 //disable_irq(ts->irq);
575 free_irq(ts->irq,ts);
576 cancel_work_sync(&ts->work);
577 flush_work(&ts->work);
579 ret = i2c_master_normal_send(client, buf, 3, 400*1000);
581 printk("ct360_ts_suspend:i2c_transfer fail 1=%d\n",ret);
586 ret = i2c_master_normal_send(client, buf, 2, 400*1000);
588 printk("ct360_ts_suspend:i2c_transfer fail 2=%d\n",ret);
591 for (i =0; i<10; i++)
593 if (touch_state[i] == TOUCH_DOWN)
595 input_mt_slot(ts->input_dev, i);
596 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, -1);
597 touch_state[i] = TOUCH_UP;
603 static int ct360_ts_resume(struct i2c_client *client)
605 struct ct360_ts_data *ts = i2c_get_clientdata(client);
606 struct ct360_platform_data *pdata = client->dev.platform_data;
610 { pdata->shutdown(1);
617 printk("ct360 TS Resume\n");
619 for(i=0; i<10; i++) {
621 input_mt_slot(ts->input_dev, i);
622 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, false);
626 for(i=0; i<10; i++) {
628 input_mt_slot(ts->input_dev, i);
629 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, false);
633 input_sync(ts->input_dev);
635 ret = request_irq(ts->irq, ct360_ts_irq_handler, IRQF_TRIGGER_FALLING, client->name, ts);
637 printk("!!! ct360 request_irq failed\n");
642 #ifdef CONFIG_HAS_EARLYSUSPEND
643 static void ct360_ts_early_suspend(struct early_suspend *h)
645 struct ct360_ts_data *ts;
646 ts = container_of(h, struct ct360_ts_data, early_suspend);
647 ct360_ts_suspend(ts->client, PMSG_SUSPEND);
650 static void ct360_ts_late_resume(struct early_suspend *h)
652 struct ct360_ts_data *ts;
653 ts = container_of(h, struct ct360_ts_data, early_suspend);
654 ct360_ts_resume(ts->client);
658 static const struct i2c_device_id ct360_ts_id[] = {
659 { ct360_TS_NAME, 0 },
663 static struct i2c_driver ct360_ts_driver = {
664 .probe = ct360_ts_probe,
665 .remove = ct360_ts_remove,
666 #ifndef CONFIG_HAS_EARLYSUSPEND
667 .suspend = ct360_ts_suspend,
668 .resume = ct360_ts_resume,
670 .id_table = ct360_ts_id,
672 .name = ct360_TS_NAME,
676 static int __devinit ct360_ts_init(void)
678 printk("%s\n",__FUNCTION__);
680 return i2c_add_driver(&ct360_ts_driver);
683 static void __exit ct360_ts_exit(void)
685 printk("%s\n",__FUNCTION__);
686 i2c_del_driver(&ct360_ts_driver);
689 module_init(ct360_ts_init);
690 module_exit(ct360_ts_exit);
692 MODULE_DESCRIPTION("ct360 Touchscreen Driver");
693 MODULE_LICENSE("GPL");