summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/sitronix/sitronix_i2c.c
blob: 7b79aaf45c7adf6016a57627c737737e456ff087 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/* drivers/input/touchscreen/sis_i2c.c
 *
 * Copyright (C) 2009 SiS, Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/module.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/linkage.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/gpio.h>
#include <mach/wmt_iomux.h>
#include <mach/hardware.h>
#include <asm/uaccess.h>
#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
#endif
#include "sitronix_i2c.h"
#include "irq_gpio.h"

struct sitronix_data *pContext=NULL;
struct i2c_client *l_client=NULL;

#ifdef TOUCH_KEY 

#define MENU_IDX    0
#define HOME_IDX    1
#define BACK_IDX    2
#define SEARCH_IDX  3
#define NUM_KEYS    4

static int virtual_keys[NUM_KEYS] ={
        KEY_BACK,
        KEY_HOME,
        KEY_MENU,
        KEY_SEARCH
};
#endif

#define I2C_BUS1 1


#ifdef CONFIG_HAS_EARLYSUSPEND
static void sitronix_early_suspend(struct early_suspend *h);
static void sitronix_late_resume(struct early_suspend *h);
#endif

static int sitronix_read(struct sitronix_data *sitronix, u8 *rxdata, int length)
{
	int ret;
	struct i2c_msg msg[2];

    msg[0].addr = sitronix->addr;
	msg[0].flags = 0 | I2C_M_NOSTART;
	msg[0].len = 1;
	msg[0].buf = rxdata;
	
	msg[1].addr = sitronix->addr;
	msg[1].flags = I2C_M_RD;
	msg[1].len = length;
	msg[1].buf = rxdata;

	//ret = wmt_i2c_xfer_continue_if_4(msg, 2, I2C_BUS1);
	ret = i2c_transfer(l_client->adapter, msg, 2);
	if (ret <= 0)
		dbg_err("msg i2c read error: %d\n", ret);
	
	return ret;
}

#ifdef SITRONIX_DEBUG
static int sitronix_write(struct sitronix_data *sitronix, u8 *txdata, int length)
{
	int ret;
	struct i2c_msg msg[1];

    msg[0].addr = sitronix->addr;
	msg[0].flags = 0;
	msg[0].len = length;
	msg[0].buf = txdata;

	//ret = wmt_i2c_xfer_continue_if_4(msg, 1, I2C_BUS1);
	ret = i2c_transfer(l_client->adapter, msg, 1);
	if (ret <= 0)
		dbg_err("msg i2c read error: %d\n", ret);
	
	return ret;
}

static int sitronix_get_fw_revision(struct sitronix_data *sitronix)
{
	int ret = 0;
	uint8_t buffer[4]={FIRMWARE_REVISION_3,0};

	ret = sitronix_read(sitronix, buffer, 4);
	if (ret < 0){
		dbg_err("read fw revision error (%d)\n", ret);
		return ret;
	}
    
	memcpy(sitronix->fw_revision, buffer, 4);
	printk("Fw Revision (hex): %x%x%x%x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
	
	return 0;
}

static int sitronix_get_max_touches(struct sitronix_data *sitronix)
{
	int ret = 0;
	uint8_t buffer[1]={MAX_NUM_TOUCHES};

	ret = sitronix_read(sitronix, buffer, 1);
	if (ret < 0){
		dbg_err("read max touches error (%d)\n", ret);
		return ret;
	}
    
	sitronix->max_touches = buffer[0];
	printk("max touches = %d \n",sitronix->max_touches);
    
	return 0;
}

static int sitronix_get_protocol(struct sitronix_data *sitronix)
{
	int ret = 0;
	uint8_t buffer[1]={I2C_PROTOCOL};

	ret = sitronix_read(sitronix, buffer, 1);
	if (ret < 0){
		dbg_err("read i2c protocol error (%d)\n", ret);
		return ret;
	}
    
	sitronix->touch_protocol_type = buffer[0] & I2C_PROTOCOL_BMSK;
	sitronix->sensing_mode = (buffer[0] & (ONE_D_SENSING_CONTROL_BMSK << ONE_D_SENSING_CONTROL_SHFT)) >> ONE_D_SENSING_CONTROL_SHFT;
	printk("i2c protocol = %d ,sensing mode = %d \n", sitronix->touch_protocol_type, sitronix->sensing_mode);

	return 0;
}

static int sitronix_get_resolution(struct sitronix_data *sitronix)
{
	int ret = 0;
	uint8_t buffer[3]={XY_RESOLUTION_HIGH};

	ret = sitronix_read(sitronix, buffer, 3);
	if (ret < 0){
		dbg_err("read resolution error (%d)\n", ret);
		return ret;
	}
    
	sitronix->resolution_x = ((buffer[0] & (X_RES_H_BMSK << X_RES_H_SHFT)) << 4) | buffer[1];
	sitronix->resolution_y = ((buffer[0] & Y_RES_H_BMSK) << 8) | buffer[2];
	printk("Resolution: %d x %d\n", sitronix->resolution_x, sitronix->resolution_y);
	
	return 0;
}

static int sitronix_get_chip_id(struct sitronix_data *sitronix)
{
	int ret = 0;
	uint8_t buffer[3]={CHIP_ID};

	ret = sitronix_read(sitronix, buffer, 3);
	if (ret < 0){
		dbg_err("read Chip ID error (%d)\n", ret);
		return ret;
	}
    
	if(buffer[0] == 0){
		if(buffer[1] + buffer[2] > 32)
			sitronix->chip_id = 2;
		else
			sitronix->chip_id = 0;
	}else
		sitronix->chip_id = buffer[0];

    sitronix->Num_X = buffer[1];
	sitronix->Num_Y = buffer[2];
	printk("Chip ID = %d, Num_X = %d, Num_Y = %d\n", sitronix->chip_id, sitronix->Num_X, sitronix->Num_Y);

	return 0;
}

static int sitronix_get_device_status(struct sitronix_data *sitronix)
{
	int ret = 0;
	uint8_t buf[3]={FIRMWARE_VERSION,0};

	ret = sitronix_read(sitronix, buf, 3);
	if (ret < 0){
		dbg_err("read resolution error (%d)\n", ret);
		return ret;
	}
    
	printk("Firmware version:%02x, Status Reg:%02x,Ctrl Reg:%02x\n", buf[0], buf[1],buf[2]);
	return 0;

}

static int sitronix_get_device_info(struct sitronix_data *sitronix)
{
	int ret = 0;

    ret = sitronix_get_resolution(sitronix);
	if(ret < 0) return ret;
    
	ret = sitronix_get_chip_id(sitronix);
	if(ret < 0) return ret;
    
	ret = sitronix_get_fw_revision(sitronix);
	if(ret < 0) return ret;
    
	ret = sitronix_get_protocol(sitronix);
	if(ret < 0) return ret;
    
	ret = sitronix_get_max_touches(sitronix);
	if(ret < 0) return ret;
    
    ret = sitronix_get_device_status(sitronix);
    if(ret < 0) return ret;
    
	if((sitronix->fw_revision[0] == 0) && (sitronix->fw_revision[1] == 0)){
		if(sitronix->touch_protocol_type == SITRONIX_RESERVED_TYPE_0){
			sitronix->touch_protocol_type = SITRONIX_B_TYPE;
			printk("i2c protocol (revised) = %d \n", sitronix->touch_protocol_type);
		}
	}
    
	if(sitronix->touch_protocol_type == SITRONIX_A_TYPE)
		sitronix->pixel_length = PIXEL_DATA_LENGTH_A;
	else if(sitronix->touch_protocol_type == SITRONIX_B_TYPE){
		sitronix->pixel_length = PIXEL_DATA_LENGTH_B;
		sitronix->max_touches = 2;
		printk("max touches (revised) = %d \n", sitronix->max_touches);
	}

	return 0;
}
#endif

static void sitronix_read_work(struct work_struct *work)
{
	struct sitronix_data *sitronix= container_of(work, struct sitronix_data, read_work);
    int ret = -1;
	u8 buf[22] = {FINGERS,0};
	u8 i = 0, fingers = 0, tskey = 0;
	u16 px = 0, py = 0;
    u16 x = 0, y = 0;

    ret = sitronix_read(sitronix, buf,sizeof(buf));
    if(ret <= 0){
        dbg_err("get raw data failed!\n");
        goto err_exit;
    }
    
    fingers = buf[0]&0x0f;
    if( fingers ){
        /* Report co-ordinates to the multi-touch stack */
        for(i=0; i < fingers; i++){
            if(sitronix->swap){
                y = ((buf[i*4+2]<<4)&0x0700)|buf[i*4+3];
                x = ((buf[i*4+2]<<8)&0x0700)|buf[i*4+4];
            }else{
                x = ((buf[i*4+2]<<4)&0x0700)|buf[i*4+3];
                y = ((buf[i*4+2]<<8)&0x0700)|buf[i*4+4];
            }

            if(!(buf[i*4+2]&0x80)) continue; /*check valid bit */

            
            if(x > sitronix->xresl ) x = sitronix->xresl ;
            if(y > sitronix->yresl ) y = sitronix->yresl ;
            
            px = x;
            py = y;
            if(sitronix->xch) px = sitronix->xresl - x;
			if(sitronix->ych) py = sitronix->yresl - y;         

			if (sitronix->lcd_exchg) {
				int tmp;
				tmp = px;
				px = py;
				py = sitronix->xresl - tmp;
			}

			input_report_abs(sitronix->input_dev, ABS_MT_POSITION_X, px);
			input_report_abs(sitronix->input_dev, ABS_MT_POSITION_Y, py);
			input_report_abs(sitronix->input_dev, ABS_MT_TRACKING_ID, i+1);
			input_mt_sync(sitronix->input_dev);
            sitronix->penup = 0;
            if(sitronix->dbg) printk("F%d,raw data: x=%-4d, y=%-4d; report data: px=%-4d, py=%-4d\n", i, x, y, px, py);
    	}
    	input_sync(sitronix->input_dev);

    }
    else if(!sitronix->penup){
        dbg("pen up.\n");
        sitronix->penup = 1;
        input_mt_sync(sitronix->input_dev);
        input_sync(sitronix->input_dev);
    }

    /* virtual keys */
    tskey = buf[1];
    if(tskey){
        if(!sitronix->tkey_idx){
            sitronix->tkey_idx = tskey;
            input_report_key(sitronix->input_dev,virtual_keys[sitronix->tkey_idx>>1] , 1);
            input_sync(sitronix->input_dev);
            dbg("virtual key down, idx=%d\n",sitronix->tkey_idx);
        }
    }else{
        if(sitronix->tkey_idx){   
            dbg("virtual key up , idx=%d\n",sitronix->tkey_idx);
            input_report_key(sitronix->input_dev,virtual_keys[sitronix->tkey_idx>>1] , 0);
            input_sync(sitronix->input_dev);
            sitronix->tkey_idx = tskey;            
        }
    }

err_exit:
	wmt_enable_gpirq(sitronix->irqgpio);
    return;
}


static irqreturn_t sitronix_isr_handler(int irq, void *dev)
{
    	struct sitronix_data *sitronix = dev;

	if (wmt_is_tsint(sitronix->irqgpio))
	{
		wmt_clr_int(sitronix->irqgpio);
		if (wmt_is_tsirq_enable(sitronix->irqgpio))
		{
			wmt_disable_gpirq(sitronix->irqgpio);
			if(!sitronix->earlysus) queue_work(sitronix->workqueue, &sitronix->read_work);
		}
		return IRQ_HANDLED;
	}
	return IRQ_NONE;
}

static void sitronix_reset(struct sitronix_data *sitronix)
{

    gpio_set_value(sitronix->rstgpio, 1);
    mdelay(5);
    gpio_set_value(sitronix->rstgpio, 0);
    mdelay(5);
    gpio_set_value(sitronix->rstgpio, 1);
    mdelay(5);

    return;
}
    
static int sitronix_auto_clb(struct sitronix_data *sitronix)
{
    return 1;
}

#ifdef CONFIG_HAS_EARLYSUSPEND
static void sitronix_early_suspend(struct early_suspend *handler)
{
    struct sitronix_data *sitronix = container_of(handler, struct sitronix_data, early_suspend);
    sitronix->earlysus = 1;
	wmt_disable_gpirq(sitronix->irqgpio);
    return;
}

static void sitronix_late_resume(struct early_suspend *handler)
{
    struct sitronix_data *sitronix = container_of(handler, struct sitronix_data, early_suspend);
    
    sitronix->earlysus = 0;
    sitronix_reset(sitronix);
    msleep(200);

    wmt_set_gpirq(sitronix->irqgpio, IRQ_TYPE_EDGE_FALLING);
    wmt_enable_gpirq(sitronix->irqgpio);
    
    return;
}
#endif  //CONFIG_HAS_EARLYSUSPEND


static int sitronix_suspend(struct platform_device *pdev, pm_message_t state)
{
    	struct sitronix_data *sitronix = platform_get_drvdata(pdev);
    	sitronix->earlysus = 1;
	wmt_disable_gpirq(sitronix->irqgpio);
    	return 0;
}

static int sitronix_resume(struct platform_device *pdev)
{
    	struct sitronix_data *sitronix = platform_get_drvdata(pdev);
    
    	sitronix->earlysus = 0;
    	sitronix_reset(sitronix);
    	msleep(200);

    	wmt_set_gpirq(sitronix->irqgpio, IRQ_TYPE_EDGE_FALLING);
    	wmt_enable_gpirq(sitronix->irqgpio);
    	return 0;
}

static ssize_t cat_dbg(struct device *dev, struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "dbg \n");
}

static ssize_t echo_dbg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
    	struct sitronix_data *sitronix = pContext;

	sscanf(buf,"%d",&sitronix->dbg);
        
	return count;
}
static DEVICE_ATTR(dbg, S_IRUGO | S_IWUSR, cat_dbg, echo_dbg);

static ssize_t cat_clb(struct device *dev, struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "calibrate --echo 1 >clb \n");
}

static ssize_t echo_clb(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
	int cal ;
    	struct sitronix_data *sitronix = pContext;
    
    	sscanf(buf, "%d", &cal);
	if(cal){
        	if(sitronix_auto_clb(sitronix) <= 0) printk("Auto calibrate failed.\n");
    	}
        
	return count;
}
static DEVICE_ATTR(clb, S_IRUGO | S_IWUSR, cat_clb, echo_clb);

static struct attribute *sitronix_attributes[] = {
	&dev_attr_clb.attr,
    	&dev_attr_dbg.attr,
	NULL
};

static const struct attribute_group sitronix_group = {
	.attrs = sitronix_attributes,
};

static int sitronix_sysfs_create_group(struct sitronix_data *sitronix, const struct attribute_group *group)
{
    	int err;

    	sitronix->kobj = kobject_create_and_add("wmtts", NULL) ;
    	if(!sitronix->kobj){
        	dbg_err("kobj create failed.\n");
        	return -ENOMEM;
    	}	
    
    	/* Register sysfs hooks */
	err = sysfs_create_group(sitronix->kobj, group);
	if (err < 0){
        kobject_del(sitronix->kobj);
		dbg_err("Create sysfs group failed!\n");
		return -ENOMEM;
	}

    	return 0;
}

static void sitronix_sysfs_remove_group(struct sitronix_data *sitronix, const struct attribute_group *group)
{
    	sysfs_remove_group(sitronix->kobj, group);
    	kobject_del(sitronix->kobj);  
    	return;
}

static int sitronix_probe(struct platform_device *pdev)
{
	int i;
    	int err = 0;
	struct sitronix_data *sitronix = platform_get_drvdata(pdev);
    
	INIT_WORK(&sitronix->read_work, sitronix_read_work);
    	sitronix->workqueue = create_singlethread_workqueue(sitronix->name);
	if (!sitronix->workqueue) {
		err = -ESRCH;
		goto exit_create_singlethread;
	}
    
    	err = sitronix_sysfs_create_group(sitronix, &sitronix_group);
    	if(err < 0){
        	dbg("create sysfs group failed.\n");
        	goto exit_create_group;
    	}
    
	sitronix->input_dev = input_allocate_device();
	if (!sitronix->input_dev) {
		err = -ENOMEM;
		dbg("failed to allocate input device\n");
		goto exit_input_dev_alloc_failed;
	}
	
	sitronix->input_dev->name = sitronix->name;
	sitronix->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
	set_bit(INPUT_PROP_DIRECT, sitronix->input_dev->propbit);

	if (sitronix->lcd_exchg) {
		input_set_abs_params(sitronix->input_dev,
				ABS_MT_POSITION_X, 0, sitronix->yresl, 0, 0);
		input_set_abs_params(sitronix->input_dev,
				ABS_MT_POSITION_Y, 0, sitronix->xresl, 0, 0);
	} else {
		input_set_abs_params(sitronix->input_dev,
				ABS_MT_POSITION_X, 0, sitronix->xresl, 0, 0);
		input_set_abs_params(sitronix->input_dev,
				ABS_MT_POSITION_Y, 0, sitronix->yresl, 0, 0);
	}
    	input_set_abs_params(sitronix->input_dev,
                 ABS_MT_TRACKING_ID, 0, 20, 0, 0);
#ifdef TOUCH_KEY 	
	for (i = 0; i <NUM_KEYS; i++)
		set_bit(virtual_keys[i], sitronix->input_dev->keybit);

	sitronix->input_dev->keycode = virtual_keys;
	sitronix->input_dev->keycodesize = sizeof(unsigned int);
	sitronix->input_dev->keycodemax = NUM_KEYS;
#endif

	err = input_register_device(sitronix->input_dev);
	if (err) {
		dbg_err("sitronix_ts_probe: failed to register input device.\n");
		goto exit_input_register_device_failed;
	}
        
#ifdef CONFIG_HAS_EARLYSUSPEND
	sitronix->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
	sitronix->early_suspend.suspend = sitronix_early_suspend;
	sitronix->early_suspend.resume = sitronix_late_resume;
	register_early_suspend(&sitronix->early_suspend);
#endif
    
	if(request_irq(sitronix->irq, sitronix_isr_handler, IRQF_SHARED, sitronix->name, sitronix) < 0){
		dbg_err("Could not allocate irq for ts_sitronix !\n");
		err = -1;
		goto exit_register_irq;
	}	

    	wmt_set_gpirq(sitronix->irqgpio, IRQ_TYPE_EDGE_FALLING);
    	wmt_enable_gpirq(sitronix->irqgpio);
    	sitronix_reset(sitronix);
    	msleep(200);
#ifdef SITRONIX_DEBUG    
    	sitronix_get_device_info(sitronix);
#endif
    
    	return 0;
    
exit_register_irq:
#ifdef CONFIG_HAS_EARLYSUSPEND
	unregister_early_suspend(&sitronix->early_suspend);
#endif
exit_input_register_device_failed:
	input_free_device(sitronix->input_dev);
exit_input_dev_alloc_failed:
    	sitronix_sysfs_remove_group(sitronix, &sitronix_group);
exit_create_group:
	cancel_work_sync(&sitronix->read_work);
	destroy_workqueue(sitronix->workqueue);  
exit_create_singlethread:
    //kfree(sitronix);
	return err;
}

static int sitronix_remove(struct platform_device *pdev)
{
    	struct sitronix_data *sitronix = platform_get_drvdata(pdev);

    	cancel_work_sync(&sitronix->read_work);
    	flush_workqueue(sitronix->workqueue);
	destroy_workqueue(sitronix->workqueue);
    
    	free_irq(sitronix->irq, sitronix);
    	wmt_disable_gpirq(sitronix->irqgpio);
    
#ifdef CONFIG_HAS_EARLYSUSPEND
    	unregister_early_suspend(&sitronix->early_suspend);
#endif
	input_unregister_device(sitronix->input_dev);

    	sitronix_sysfs_remove_group(sitronix, &sitronix_group);
    //kfree(sitronix);
    
	dbg("remove...\n");
	return 0;
}

static void sitronix_release(struct device *device)
{
    	return;
}

static struct platform_device sitronix_device = {
	.name  	    = DEV_SITRONIX,
	.id       	= 0,
	.dev    	= {.release = sitronix_release},
};

static struct platform_driver sitronix_driver = {
	.driver = {
	           .name   	= DEV_SITRONIX,
		       .owner	= THIS_MODULE,
	 },
	.probe    = sitronix_probe,
	.remove   = sitronix_remove,
	.suspend  = sitronix_suspend,
	.resume   = sitronix_resume,
};

static int check_touch_env(struct sitronix_data *sitronix)
{
	int len = 96;
	int Enable;
    	char retval[96] = {0};
	char *p=NULL;
	int ret;

    // Get u-boot parameter
	if(wmt_getsyspara("wmt.io.touch", retval, &len)) return -EIO;

	sscanf(retval,"%d:",&Enable);
	//check touch enable
	if(Enable == 0) return -ENODEV;
	
	p = strchr(retval,':');
	p++;

    	if(strncmp(p,"st1536",6)) return -ENODEV;
    
	sitronix->name = DEV_SITRONIX;
    	sitronix->addr = SITRONIX_ADDR;
	p = strchr(p,':');
	p++;
	sscanf(p,"%d:%d:%d:%d:%d:%d:%d",
        &sitronix->xresl, &sitronix->yresl, &sitronix->irqgpio, &sitronix->rstgpio, &sitronix->swap, &sitronix->xch, &sitronix->ych);

    	sitronix->irq = IRQ_GPIO;
	printk("%s reslx=%d, resly=%d, irqgpio_num=%d, rstgpio_num=%d, XYswap=%d, Xdirch=%d, Ydirch=%d\n", sitronix->name, 
        sitronix->xresl, sitronix->yresl, sitronix->irqgpio, sitronix->rstgpio, sitronix->swap, sitronix->xch, sitronix->ych);
    
    	sitronix->penup = 1;
  
	memset(retval,0,sizeof(retval));
	ret = wmt_getsyspara("wmt.display.fb0", retval, &len);
	if (!ret) {
		int tmp[6];
		p = retval;
		sscanf(p, "%d:[%d:%d:%d:%d:%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
		if (tmp[4] > tmp[5])
			sitronix->lcd_exchg = 1;
	}

	return 0;
}

struct i2c_board_info ts_i2c_board_info = {
	.type          = DEV_SITRONIX,
	.flags         = 0x00,
	.addr          = SITRONIX_ADDR,
	.platform_data = NULL,
	.archdata      = NULL,
	.irq           = -1,
};

static int ts_i2c_register_device (void)
{
	struct i2c_board_info *ts_i2c_bi;
	struct i2c_adapter *adapter = NULL;
	//struct i2c_client *client   = NULL;

	ts_i2c_bi = &ts_i2c_board_info;
	adapter = i2c_get_adapter(I2C_BUS1);/*in bus 1*/

	if (NULL == adapter) {
		printk("can not get i2c adapter, client address error\n");
		return -1;
	}
	l_client = i2c_new_device(adapter, ts_i2c_bi);
	if (l_client == NULL) {
		printk("allocate i2c client failed\n");
		return -1;
	}
	i2c_put_adapter(adapter);
	return 0;
}

static void ts_i2c_unregister_device(void)
{
	if (l_client != NULL)
	{
		i2c_unregister_device(l_client);
		l_client = NULL;
	}
}


static int __init sitronix_init(void)
{
	int ret = -ENOMEM;
    	struct sitronix_data *sitronix=NULL;

	if (ts_i2c_register_device()<0)
	{
		dbg("Error to run ts_i2c_register_device()!\n");
		return -1;
	}

	sitronix = kzalloc(sizeof(struct sitronix_data), GFP_KERNEL);
    	if(!sitronix){
        	dbg_err("mem alloc failed.\n");
        	return -ENOMEM;
    	}
    
	pContext = sitronix;
	ret = check_touch_env(sitronix);
    	if(ret < 0)
        	goto exit_free_mem;
	
	ret = gpio_request(sitronix->irqgpio, "ts_irq");
	if (ret < 0) {
		printk("gpio(%d) touchscreen irq request fail\n", sitronix->irqgpio);
		goto exit_free_mem;
	}

	ret = gpio_request(sitronix->rstgpio, "ts_rst");
	if (ret < 0) {
		printk("gpio(%d) touchscreen reset request fail\n", sitronix->rstgpio);
		goto exit_free_irqgpio;
	}
	gpio_direction_output(sitronix->rstgpio, 1);


	ret = platform_device_register(&sitronix_device);
	if(ret){
		dbg_err("register platform drivver failed!\n");
		goto exit_free_gpio;
	}    
    	platform_set_drvdata(&sitronix_device, sitronix);
	
	ret = platform_driver_register(&sitronix_driver);
	if(ret){
		dbg_err("register platform device failed!\n");
		goto exit_unregister_pdev;
	}
	
	return ret;
	
exit_unregister_pdev:
	platform_device_unregister(&sitronix_device);
exit_free_gpio:

	gpio_free(sitronix->rstgpio);
exit_free_irqgpio:
	gpio_free(sitronix->irqgpio);

exit_free_mem:
    	kfree(sitronix);
    	pContext = NULL;
	ts_i2c_unregister_device();
	return ret;
}

static void sitronix_exit(void)
{
	struct sitronix_data *sitronix;

    	if(!pContext) return;

	sitronix = pContext;

	gpio_free(sitronix->irqgpio);
	gpio_free(sitronix->rstgpio);

    
	platform_driver_unregister(&sitronix_driver);
	platform_device_unregister(&sitronix_device);
    	kfree(pContext);
    
	ts_i2c_unregister_device();
	return;
}

late_initcall(sitronix_init);
module_exit(sitronix_exit);

MODULE_DESCRIPTION("Sitronix Multi-Touch Driver");
MODULE_LICENSE("GPL");