summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/ct36x/ct363.c
blob: 2060667c0cdce70135b98822577a9c84871a6311 (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
/* SPDX-License-Identifier: GPL-2.0 */
#define CT363_POINT_NUM		10

struct ct363_finger_data {
	unsigned char	xhi;			// X coordinate Hi
	unsigned char	yhi;			// Y coordinate Hi
	unsigned char	ylo : 4;		// Y coordinate Lo
	unsigned char	xlo : 4;		// X coordinate Lo
	unsigned char   status : 3;             // Action information, 1: Down; 2: Move; 3: Up
	unsigned char   id : 5;                 // ID information, from 1 to CFG_MAX_POINT_NUM
	unsigned char	area;			// Touch area
	unsigned char	pressure;		// Touch Pressure
};


struct ct363_priv{
	int press;
	int release;
	int x, y;
	union{
		struct ct363_finger_data pts[CT363_POINT_NUM];
		char buf[CT363_POINT_NUM * sizeof(struct ct363_finger_data)];
	};
};

static int ct363_init_hw(struct ct36x_data *ts)
{
/*
	int ret = 0;
	ret = gpio_request(ts->rst_io.gpio, "ct363_rst");
	if(ret < 0){
		dev_err(ts->dev, "Failed to request rst gpio\n");
		return ret;
	}

	ret = gpio_request(ts->irq_io.gpio, "ct363_irq");
	if(ret < 0){
		gpio_free(ts->rst_io.gpio);
		dev_err(ts->dev, "Failed to request irq gpio\n");
		return ret;
	}*/
	gpio_direction_input(ts->irq_io.gpio);
	//gpio_pull_updown(ts->irq_io.gpio, 1);
	//gpio_set_value(ts->irq_io.gpio,1);
	gpio_direction_output(ts->rst_io.gpio, ts->rst_io.active_low);

	return 0;
}
static void ct363_deinit_hw(struct ct36x_data *ts)
{
	gpio_free(ts->rst_io.gpio);
	gpio_free(ts->irq_io.gpio);
}

static void ct363_reset_hw(struct ct36x_data *ts)
{
	gpio_direction_output(ts->rst_io.gpio, ts->rst_io.active_low);
	msleep(50);
	gpio_set_value(ts->rst_io.gpio, !ts->rst_io.active_low);
	msleep(50);
	gpio_set_value(ts->rst_io.gpio, ts->rst_io.active_low);
	msleep(500);
}

static int ct363_init(struct ct36x_data *ts)
{
	int ret = 0, fwchksum, binchksum, updcnt = 5;
	struct ct363_priv *ct363 = NULL;
	
	ret = ct363_init_hw(ts);
	if(ret < 0)
		return ret;

	 /* Hardware reset */
	ct363_reset_hw(ts);
	// Get binary Checksum
	binchksum = ct36x_chip_get_binchksum();
	ct36x_dbg(ts, "CT363 init: binchksum = %d\n", binchksum);

	ret = ct36x_chip_get_fwchksum(ts);
	if(ret < 0){
		dev_err(ts->dev, "CT36X chip: Failed to get fwchksum\n");
		return ret;
	}
	fwchksum = ret;
	ct36x_dbg(ts, "CT363 init: fwchksum = %d\n", fwchksum);
	while(binchksum != fwchksum && updcnt--) {
		/* Update Firmware */
		ret = ct36x_chip_go_bootloader(ts);
		if(ret < 0){
			dev_err(ts->dev, "CT36X chip: Failed to go bootloader\n");
			return ret;
		}
		 /* Hardware reset */
		ct363_reset_hw(ts);

		ret = ct36x_chip_get_fwchksum(ts);
		if(ret < 0){
			dev_err(ts->dev, "CT36X chip: Failed to get fwchksum\n");
			return ret;
		}
		fwchksum = ret;
		ct36x_dbg(ts, "CT363 update FW: fwchksum = %d\n", fwchksum);
	}
	if(binchksum != fwchksum){
		dev_err(ts->dev, "Fail to update FW\n");
		return -ENODEV;
	}

	/* Hardware reset */
	ct363_reset_hw(ts);
	msleep(5);

	ts->point_num = CT363_POINT_NUM;
	
	ct363 = kzalloc(sizeof(struct ct363_priv), GFP_KERNEL);
	if(!ct363){
		dev_err(ts->dev, "No memory for ct36x");
		return -ENOMEM;
	}
	ts->priv = ct363;

	return 0;
}

static void ct363_deinit(struct ct36x_data *ts)
{
	struct ct363_priv *ct363 = ts->priv;

	ct363_deinit_hw(ts);
	kfree(ct363);
	ts->priv = NULL;

	return;
}

static int ct363_first_init_flag = 1;
static int ct363_suspend(struct ct36x_data *ts)
{
	int ret = 0;

	ret = ct36x_chip_go_sleep(ts);
	ct363_first_init_flag=0;
	if(ret < 0)
		dev_warn(ts->dev, "CT363 chip: failed to go to sleep\n");
	return ret;
}

static int ct363_resume(struct ct36x_data *ts)
{
	int i;

	/* Hardware reset */
	if(ct363_first_init_flag)
		ct363_reset_hw(ts);
	else
	{
	//	gpio_direction_output(ts->rst_io.gpio, ts->rst_io.active_low);
	//	msleep(50);
		gpio_set_value(ts->rst_io.gpio, !ts->rst_io.active_low);
		msleep(50);
		gpio_set_value(ts->rst_io.gpio, ts->rst_io.active_low);
		msleep(50);
	}
	for(i = 0; i < ts->point_num; i++){
		input_mt_slot(ts->input, i);
		input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
	}
	input_sync(ts->input);

	return 0;
}

static void ct363_report(struct ct36x_data *ts)
{
	int t ,m;
	int i, ret = 0;
	int sync = 0, x, y;
	int len = sizeof(struct ct363_finger_data) * ts->point_num;
	struct ct363_priv *ct363 = ts->priv;

	ret = ct36x_read(ts, ct363->buf, len);
	if(ret < 0){
		dev_warn(ts->dev, "Failed to read finger data\n");
		return;
	}

       if(ct36x_dbg_level==2)
       for(t=0;t< ts->point_num;t++){
            ct36x_dbg(ts, "CT363buf[%d]: ", t);
            for(m=0;m<6;m++){
                    ct36x_dbg(ts, " 0x%x  %x  %x  %x  %x  %x  %x  %x ",ct363->pts[t].xhi,
                                                                                               ct363->pts[t].yhi,
                                                                                               ct363->pts[t].ylo,
                                                                                               ct363->pts[t].xlo,
                                                                                               ct363->pts[t].status,
                                                                                               ct363->pts[t].id,
                                                                                               ct363->pts[t].area,
                                                                                               ct363->pts[t].pressure);
             }
             ct36x_dbg(ts, " \n ");
       }
       
	ct363->press = 0;
	for(i = 0; i < ts->point_num; i++){
		if((ct363->pts[i].xhi != 0xFF && ct363->pts[i].yhi != 0xFF) &&
			(ct363->pts[i].status == 1 || ct363->pts[i].status == 2)){
			x = (ct363->pts[i].xhi<<4)|(ct363->pts[i].xlo&0xF);
			y = (ct363->pts[i].yhi<<4)|(ct363->pts[i].ylo&0xF);

			ct363->x = ts->orientation[0] * x + ts->orientation[1] * y;
			ct363->y = ts->orientation[2] * x + ts->orientation[3] * y;

                   if( (ct363->x > ts->x_max) || (ct363->y > ts->y_max) || (ct363->x < 0) || (ct363->y < 0) ){
                          continue ;
                    }
                   if(flag_ct36x_model==363){
			ct363->y = ts->y_max - ct363->y;      //add for p977 
		   }       
			input_mt_slot(ts->input, ct363->pts[i].id - 1);
			input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
			input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, 1);
			input_report_abs(ts->input, ABS_MT_POSITION_X, ct363->x);
			input_report_abs(ts->input, ABS_MT_POSITION_Y, ct363->y);
			input_report_abs(ts->input, ABS_MT_PRESSURE, ct363->pts[i].pressure);
			ct36x_dbg(ts, "CT363 report value: id: %d,  x: %d, y:%d\n",ct363->pts[i].id - 1, ct363->x, ct363->y);

			sync = 1;
			ct363->press |= 0x01 << (ct363->pts[i].id - 1);
		}
	}
	ct363->release &= ct363->release ^ ct363->press;
	for(i = 0; i < ts->point_num; i++){
		if ( ct363->release & (0x01<<i) ) {
			input_mt_slot(ts->input, i);
			input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
			ct36x_dbg(ts, "CT363 release\n");
			sync = 1;
		}
	}
	ct363->release = ct363->press;

	if(sync)
		input_sync(ts->input);

	return;
}
struct ct36x_ops ct363_ops = {
	.init = ct363_init,
	.deinit = ct363_deinit,
	.suspend = ct363_suspend,
	.resume = ct363_resume,
	.report = ct363_report,
};