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
|
//[*]--------------------------------------------------------------------------------------------------[*]
//
//
//
// I2C Touchscreen driver
// 2012.01.17
//
//
//[*]--------------------------------------------------------------------------------------------------[*]
#include <linux/device.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/hrtimer.h>
#include <asm/unaligned.h>
#include <linux/firmware.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
//[*]--------------------------------------------------------------------------------------------------[*]
#if defined(CONFIG_HAS_EARLYSUSPEND)
#include <linux/wakelock.h>
#include <linux/earlysuspend.h>
#include <linux/suspend.h>
#endif
//[*]--------------------------------------------------------------------------------------------------[*]
#include <linux/input/touch-pdata.h>
#include <linux/input/odroidq-touch.h>
#include "touch.h"
#include "odroidq-config.h"
//[*]--------------------------------------------------------------------------------------------------[*]
//#define DEBUG_TOUCH
#define DEBUG_TOUCH_KEY
//[*]--------------------------------------------------------------------------------------------------[*]
// function prototype define
//[*]--------------------------------------------------------------------------------------------------[*]
// Touch data processing function
//[*]--------------------------------------------------------------------------------------------------[*]
static void odroidq_sw_config (struct touch *ts);
static void odroidq_fifo_clear (struct touch *ts);
void odroidq_work (struct touch *ts);
int odroidq_calibration (struct touch *ts);
int odroidq_i2c_read (struct i2c_client *client, unsigned char *cmd, unsigned int cmd_len, unsigned char *data, unsigned int len);
void odroidq_enable (struct touch *ts);
void odroidq_disable (struct touch *ts);
int odroidq_early_probe (struct touch *ts);
int odroidq_probe (struct touch *ts);
#ifdef CONFIG_HAS_EARLYSUSPEND
void odroidq_suspend (struct early_suspend *h);
void odroidq_resume (struct early_suspend *h);
#endif
//[*]--------------------------------------------------------------------------------------------------[*]
//
// i2c control function
//
//[*]--------------------------------------------------------------------------------------------------[*]
int odroidq_i2c_read(struct i2c_client *client, unsigned char *cmd, unsigned int cmd_len, unsigned char *data, unsigned int len)
{
struct i2c_msg msg[2];
int ret;
unsigned char temp[10];
unsigned char i;
if((len == 0) || (data == NULL)) {
dev_err(&client->dev, "I2C read error: Null pointer or length == 0\n");
return -1;
}
memset(msg, 0x00, sizeof(msg));
msg[0].addr = client->addr;
msg[0].flags = 0;
msg[0].len = cmd_len;
msg[0].buf = cmd;
msg[1].addr = client->addr;
msg[1].flags = I2C_M_RD;
msg[1].len = len;
msg[1].buf = temp;
if ((ret = i2c_transfer(client->adapter, msg, 2)) != 2) {
dev_err(&client->dev, "I2C read error: (%d) reg: 0x%X len: %d\n", ret, cmd[0], len);
return -EIO;
}
if(len) {
for(i = 0; i < len; i++) data[i] = temp[len - i -1];
}
return len;
}
//[*]--------------------------------------------------------------------------------------------------[*]
int odroidq_calibration (struct touch *ts)
{
if(ts->pdata->reset_gpio) gpio_set_value(ts->pdata->reset_gpio, ts->pdata->reset_level); mdelay(10);
if(ts->pdata->reset_gpio) gpio_set_value(ts->pdata->reset_gpio, ts->pdata->reset_level ? 0 : 1); mdelay(10);
odroidq_sw_config(ts); odroidq_fifo_clear(ts);
return 0;
}
//[*]--------------------------------------------------------------------------------------------------[*]
//
// Touch data processing function
//
//[*]--------------------------------------------------------------------------------------------------[*]
static void odroidq_sw_config (struct touch *ts)
{
unsigned char i, wdata[2], cmd;
for(i = 0; i < sizeof(Config) / sizeof(Config[0]); i++)
{
wdata[0] = Config[i].Data1; wdata[1] = Config[i].Data2;
cmd = Config[i].Reg;
if(Config[i].No == 0xFF) mdelay(cmd);
else {
ts->pdata->i2c_write(ts->client, (unsigned char *)&cmd, sizeof(cmd), &wdata[0], Config[i].No);
}
}
mdelay(10);
}
//[*]--------------------------------------------------------------------------------------------------[*]
static void odroidq_fifo_clear (struct touch *ts)
{
unsigned char wdata = 0x01, cmd = EVENT_FIFO_SCLR;
ts->pdata->i2c_write(ts->client, (unsigned char *)&cmd, sizeof(cmd), (unsigned char *)&wdata, sizeof(wdata)); // clear Event FiFo
}
//[*]--------------------------------------------------------------------------------------------------[*]
static unsigned char odroidq_id_tracking (struct touch *ts, unsigned char find_id)
{
unsigned char i, find_slot = 0xFF;
for(i = 0; i < ts->pdata->max_fingers; i++) {
if(ts->finger[i].id == find_id) find_slot = i;
if((ts->finger[i].event == TS_EVENT_UNKNOWN) && (find_slot == 0xFF)) find_slot = i;
}
return find_slot;
}
//[*]--------------------------------------------------------------------------------------------------[*]
#if defined(SOFT_AVR_FILTER_ENABLE)
typedef struct soft_filter__t {
unsigned int x[SOFT_AVR_COUNT];
unsigned int y[SOFT_AVR_COUNT];
unsigned int pos, cnt;
} __attribute__ ((packed)) soft_filter_t;
static soft_filter_t *pSoftFilter;
static unsigned char odroidq_soft_avr_filter(struct touch *ts, event_stack_u *event_stack, unsigned char find_slot)
{
unsigned int i, x, y;
if(ts->finger[find_slot].event != TS_EVENT_MOVE) {
pSoftFilter[find_slot].pos = 0; pSoftFilter[find_slot].cnt = 0;
return true;
}
x = (((event_stack->bits.msb_x << 8) & 0xF00) | event_stack->bits.lsb_x);
y = (((event_stack->bits.msb_y << 8) & 0xF00) | event_stack->bits.lsb_y);
if(y > 15) y -= 15;
else y = 1;
ts->finger[find_slot].id = event_stack->bits.number + 1;
ts->finger[find_slot].pressure =
(unsigned int)(event_stack->bits.pressure);
if(pSoftFilter[find_slot].cnt == 1) {
if((abs(pSoftFilter[find_slot].x[0] - x) < SOFT_AVR_MOVE_TOL_X) &&
(abs(pSoftFilter[find_slot].y[0] - y) < SOFT_AVR_MOVE_TOL_Y)) return false;
}
if(event_stack->bits.speed < SOFT_AVR_ENABLE_SPEED) {
pSoftFilter[find_slot].x[pSoftFilter[find_slot].pos] = x;
pSoftFilter[find_slot].y[pSoftFilter[find_slot].pos] = y;
pSoftFilter[find_slot].pos++; pSoftFilter[find_slot].pos %= SOFT_AVR_COUNT;
if(pSoftFilter[find_slot].cnt < SOFT_AVR_COUNT) pSoftFilter[find_slot].cnt++;
for(i = 0, x = 0, y = 0; i < pSoftFilter[find_slot].cnt; i++) {
x += pSoftFilter[find_slot].x[i]; y += pSoftFilter[find_slot].y[i];
}
ts->finger[find_slot].x = x / pSoftFilter[find_slot].cnt;
ts->finger[find_slot].y = y / pSoftFilter[find_slot].cnt;
}
else {
pSoftFilter[find_slot].pos = 0; pSoftFilter[find_slot].cnt = 0;
ts->finger[find_slot].x = x; ts->finger[find_slot].y = y;
}
return true;
}
#endif // #if defined(SOFT_AVR_FILTER_ENABLE)
//[*]--------------------------------------------------------------------------------------------------[*]
void odroidq_work (struct touch *ts)
{
status_u status;
button_u button;
event_stack_u event_stack;
unsigned char cmd, find_slot;
cmd = TOUCH_STATUS;
if(ts->pdata->i2c_read(ts->client, (unsigned char *)&cmd, sizeof(cmd), (unsigned char *)&status.byte[0], sizeof(status_u)) < 0) return;
if(ts->pdata->keycode) {
cmd = BUTTON_STATUS;
if(ts->pdata->i2c_read(ts->client, (unsigned char *)&cmd, sizeof(cmd), (unsigned char *)&button.ubyte, sizeof(button_u)) < 0) return;
ts->pdata->key_report(ts, button.ubyte);
}
if(status.bits.fifo_overflow || status.bits.large_object || status.bits.abnomal_status) {
printk("[Error Status] fifo_overflow(%d), large_object(%d), abnomal_status(%d)\n" , status.bits.fifo_overflow
, status.bits.large_object
, status.bits.abnomal_status);
// Error reconfig
ts->pdata->disable(ts); ts->pdata->enable(ts);
return;
}
do {
cmd = EVENT_STACK;
ts->pdata->i2c_read(ts->client, (unsigned char *)&cmd, sizeof(cmd), (unsigned char *)&event_stack.byte[0], sizeof(event_stack_u));
if(status.bits.fifo_valid) {
if((find_slot = odroidq_id_tracking(ts, event_stack.bits.number + 1)) == 0xFF) {
printk("ERROR(%s) : Empty slot not found", __func__); continue;
}
if((event_stack.bits.event != EVENT_UNKNOWN) && (event_stack.bits.number < ts->pdata->max_fingers)) {
if((event_stack.bits.event == EVENT_PRESS) || (event_stack.bits.event == EVENT_MOVE)) {
ts->finger[find_slot].status = true;
ts->finger[find_slot].event = TS_EVENT_MOVE;
#if defined(SOFT_AVR_FILTER_ENABLE)
ts->finger[find_slot].status = odroidq_soft_avr_filter(ts, &event_stack, find_slot);
#else
ts->finger[find_slot].id = event_stack.bits.number + 1;
ts->finger[find_slot].x =
(unsigned int)(((event_stack.bits.msb_x << 8) & 0xF00) | event_stack.bits.lsb_x);
ts->finger[find_slot].y =
(unsigned int)(((event_stack.bits.msb_y << 8) & 0xF00) | event_stack.bits.lsb_y);
if(ts->finger[find_slot].y > 15) ts->finger[find_slot].y -= 15;
else ts->finger[find_slot].y = 1;
ts->finger[find_slot].pressure =
(unsigned int)(event_stack.bits.pressure);
#endif
}
else {
if(ts->finger[find_slot].event == TS_EVENT_MOVE) {
ts->finger[find_slot].status = true;
ts->finger[find_slot].event = TS_EVENT_RELEASE;
}
else {
ts->finger[find_slot].status = false;
ts->finger[find_slot].event = TS_EVENT_UNKNOWN;
}
#if defined(SOFT_AVR_FILTER_ENABLE)
odroidq_soft_avr_filter(ts, &event_stack, find_slot);
#endif
}
ts->pdata->report(ts);
}
}
} while(!gpio_get_value(ts->pdata->irq_gpio));
}
//[*]--------------------------------------------------------------------------------------------------[*]
void odroidq_enable (struct touch *ts)
{
if(ts->disabled) {
odroidq_calibration(ts);
enable_irq(ts->irq); ts->disabled = false;
}
}
//[*]--------------------------------------------------------------------------------------------------[*]
void odroidq_disable (struct touch *ts)
{
if(!ts->disabled) {
disable_irq(ts->irq); ts->disabled = true;
if(ts->pdata->event_clear) ts->pdata->event_clear(ts);
if(ts->pdata->reset_gpio) gpio_set_value(ts->pdata->reset_gpio, ts->pdata->reset_level);
}
}
//[*]--------------------------------------------------------------------------------------------------[*]
int odroidq_early_probe (struct touch *ts)
{
#if defined(SOFT_AVR_FILTER_ENABLE)
if(ts->pdata->max_fingers) {
if(!(pSoftFilter = kzalloc(sizeof(soft_filter_t) * ts->pdata->max_fingers, GFP_KERNEL))) {
printk("touch soft-filter struct malloc error!\n"); return -ENOMEM;
}
}
#endif
odroidq_calibration(ts);
return 0;
}
//[*]--------------------------------------------------------------------------------------------------[*]
int odroidq_probe (struct touch *ts)
{
int ret;
unsigned short rd;
unsigned char cmd;
// Get Touch screen information
cmd = DEVICE_ID; rd = 0;
ret = ts->pdata->i2c_read(ts->client, (unsigned char *)&cmd, sizeof(cmd), (unsigned char *)&rd, sizeof(rd));
#if defined(DEBUG_TOUCH) // 0x2533
printk("DEVICE ID = 0x%04X\n", rd);
#endif
cmd = VERSION_ID; rd = 0;
ret = ts->pdata->i2c_read(ts->client, (unsigned char *)&cmd, sizeof(cmd), (unsigned char *)&rd, sizeof(rd));
#if defined(DEBUG_TOUCH)
printk("VERSION = 0x%04X\n", rd);
#endif
ts->fw_version = ((rd >> 8) & 0xFF) * 100 + (rd & 0xFF);
return ret;
}
//[*]--------------------------------------------------------------------------------------------------[*]
//[*]--------------------------------------------------------------------------------------------------[*]
MODULE_AUTHOR("HardKernel Co., Ltd.");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Touchscreen Driver");
//[*]--------------------------------------------------------------------------------------------------[*]
//[*]--------------------------------------------------------------------------------------------------[*]
|