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
|
/*
**************************************************************************
** STMicroelectronics **
**************************************************************************
** marco.cali@st.com **
**************************************************************************
* *
* I2C/SPI Communication *
* *
**************************************************************************
**************************************************************************
*/
#include "ftsSoftware.h"
#include "ftsCrossCompile.h"
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/platform_device.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <stdarg.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/serio.h>
#include <linux/init.h>
#include <linux/pm.h>
#include <linux/delay.h>
#include <linux/ctype.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/power_supply.h>
#include <linux/firmware.h>
#include <linux/regulator/consumer.h>
#include <linux/of_gpio.h>
/* #include <linux/sec_sysfs.h> */
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <linux/spi/spidev.h>
static struct i2c_client *client;
static u16 I2CSAD;
#include "ftsError.h"
#include "ftsHardware.h"
#include "ftsIO.h"
#include "ftsTool.h"
static char tag[8] = "[ FTS ]\0";
int openChannel(struct i2c_client *clt)
{
client = clt;
I2CSAD = clt->addr;
logError(1, "%s openChannel: SAD: %02X\n", tag, I2CSAD);
return OK;
}
struct device *getDev(void)
{
if (client != NULL)
return &(client->dev);
else
return NULL;
}
struct i2c_client *getClient(void)
{
if (client != NULL)
return client;
else
return NULL;
}
int fts_readCmd(u8 *cmd, int cmdLength, u8 *outBuf, int byteToRead)
{
int ret = -1;
int retry = 0;
struct i2c_msg I2CMsg[2];
/* write msg */
I2CMsg[0].addr = (__u16)I2CSAD;
I2CMsg[0].flags = (__u16)0;
I2CMsg[0].len = (__u16)cmdLength;
I2CMsg[0].buf = (__u8 *)cmd;
/* read msg */
I2CMsg[1].addr = (__u16)I2CSAD;
I2CMsg[1].flags = I2C_M_RD;
I2CMsg[1].len = byteToRead;
I2CMsg[1].buf = (__u8 *)outBuf;
if (client == NULL)
return ERROR_I2C_O;
while (retry < I2C_RETRY && ret < OK) {
ret = i2c_transfer(client->adapter, I2CMsg, 2);
if (ret >= OK)
break;
retry++;
msleep(I2C_WAIT_BEFORE_RETRY);
}
if (ret < 0) {
logError(1, "%s fts_readCmd: ERROR %02X\n", tag, ERROR_I2C_R);
return ERROR_I2C_R;
}
return OK;
}
int fts_writeCmd(u8 *cmd, int cmdLength)
{
int ret = -1;
int retry = 0;
struct i2c_msg I2CMsg[2];
I2CMsg[0].addr = (__u16)I2CSAD;
I2CMsg[0].flags = (__u16)0;
I2CMsg[0].len = (__u16)cmdLength;
I2CMsg[0].buf = (__u8 *)cmd;
if (client == NULL)
return ERROR_I2C_O;
while (retry < I2C_RETRY && ret < OK) {
ret = i2c_transfer(client->adapter, I2CMsg, 1);
if (ret >= OK)
break;
retry++;
msleep(I2C_WAIT_BEFORE_RETRY);
/* logError(1, "%s fts_writeCmd: attempt %d\n", tag, retry); */
}
if (ret < 0) {
logError(1, "%s fts_writeCmd: ERROR %02X\n", tag, ERROR_I2C_W);
return ERROR_I2C_W;
}
return OK;
}
int fts_writeFwCmd(u8 *cmd, int cmdLength)
{
int ret = -1;
int ret2 = -1;
int retry = 0;
struct i2c_msg I2CMsg[2];
I2CMsg[0].addr = (__u16)I2CSAD;
I2CMsg[0].flags = (__u16)0;
I2CMsg[0].len = (__u16)cmdLength;
I2CMsg[0].buf = (__u8 *)cmd;
if (client == NULL)
return ERROR_I2C_O;
while (retry < I2C_RETRY && (ret < OK || ret2 < OK)) {
ret = i2c_transfer(client->adapter, I2CMsg, 1);
retry++;
if (ret >= 0) {
ret2 = checkEcho(cmd, cmdLength);
break;
}
msleep(I2C_WAIT_BEFORE_RETRY);
/* logError(1, "%s fts_writeCmd: attempt %d\n", tag, retry); */
}
if (ret < 0) {
logError(1, "%s fts_writeFwCmd: ERROR %02X\n", tag, ERROR_I2C_W);
return ERROR_I2C_W;
}
if (ret2 < OK) {
logError(1, "%s fts_writeFwCmd: check echo ERROR %02X\n", tag, ret2);
return (ret|ERROR_I2C_W);
}
return OK;
}
int writeReadCmd(u8 *writeCmd1, int writeCmdLength, u8 *readCmd1,
int readCmdLength, u8 *outBuf, int byteToRead)
{
int ret = -1;
int retry = 0;
struct i2c_msg I2CMsg[3];
/* write msg */
I2CMsg[0].addr = (__u16)I2CSAD;
I2CMsg[0].flags = (__u16)0;
I2CMsg[0].len = (__u16)writeCmdLength;
I2CMsg[0].buf = (__u8 *)writeCmd1;
/* write msg */
I2CMsg[1].addr = (__u16)I2CSAD;
I2CMsg[1].flags = (__u16)0;
I2CMsg[1].len = (__u16)readCmdLength;
I2CMsg[1].buf = (__u8 *)readCmd1;
/* read msg */
I2CMsg[2].addr = (__u16)I2CSAD;
I2CMsg[2].flags = I2C_M_RD;
I2CMsg[2].len = byteToRead;
I2CMsg[2].buf = (__u8 *)outBuf;
if (client == NULL)
return ERROR_I2C_O;
while (retry < I2C_RETRY && ret < OK) {
ret = i2c_transfer(client->adapter, I2CMsg, 3);
if (ret >= OK)
break;
retry++;
msleep(I2C_WAIT_BEFORE_RETRY);
}
if (ret < 0) {
logError(1, "%s writeReadCmd: ERROR %02X\n", tag, ERROR_I2C_WR);
return ERROR_I2C_WR;
}
return OK;
}
int readCmdU16(u8 cmd, u16 address, u8 *outBuf, int byteToRead, int hasDummyByte)
{
int remaining = byteToRead;
int toRead = 0;
u8 rCmd[3] = { cmd, 0x00, 0x00 };
u8 *buff = (u8 *)kmalloc((READ_CHUNK + 1)*sizeof(u8), GFP_KERNEL);
if (buff == NULL) {
logError(1, "%s readCmdU16: ERROR %02X\n", tag, ERROR_ALLOC);
return ERROR_ALLOC;
}
while (remaining > 0) {
if (remaining >= READ_CHUNK) {
toRead = READ_CHUNK;
remaining -= READ_CHUNK;
} else {
toRead = remaining;
remaining = 0;
}
rCmd[1] = (u8)((address & 0xFF00) >> 8);
rCmd[2] = (u8)(address & 0xFF);
if (hasDummyByte) {
if (fts_readCmd(rCmd, 3, buff, toRead + 1) < 0) {
logError(1, "%s readCmdU16: ERROR %02X\n", tag, ERROR_I2C_R);
return ERROR_I2C_R;
}
memcpy(outBuf, buff + 1, toRead);
} else {
if (fts_readCmd(rCmd, 3, buff, toRead) < 0)
return ERROR_I2C_R;
memcpy(outBuf, buff, toRead);
}
address += toRead;
outBuf += toRead;
}
kfree(buff);
return OK;
}
int writeCmdU16(u8 WriteCmd, u16 address, u8 *dataToWrite, int byteToWrite)
{
int remaining = byteToWrite;
int toWrite = 0;
u8 *buff = (u8 *)kmalloc((WRITE_CHUNK + 3)*sizeof(u8), GFP_KERNEL);
if (buff == NULL) {
logError(1, "%s writeCmdU16: ERROR %02X\n", tag, ERROR_ALLOC);
return ERROR_ALLOC;
}
buff[0] = WriteCmd;
while (remaining > 0) {
if (remaining >= WRITE_CHUNK) {
toWrite = WRITE_CHUNK;
remaining -= WRITE_CHUNK;
} else {
toWrite = remaining;
remaining = 0;
}
buff[1] = (u8)((address & 0xFF00) >> 8);
buff[2] = (u8)(address & 0xFF);
memcpy(buff + 3, dataToWrite, toWrite);
if (fts_writeCmd(buff, 3 + toWrite) < 0) {
logError(1, "%s writeCmdU16: ERROR %02\n", tag, ERROR_I2C_W);
return ERROR_I2C_W;
}
address += toWrite;
dataToWrite += toWrite;
}
return OK;
}
int writeCmdU32(u8 writeCmd1, u8 writeCmd2, u32 address, u8 *dataToWrite, int byteToWrite)
{
int remaining = byteToWrite;
int toWrite = 0;
u8 buff1[3] = { writeCmd1, 0x00, 0x00 };
u8 *buff2 = (u8 *)kmalloc((WRITE_CHUNK + 3)*sizeof(u8), GFP_KERNEL);
if (buff2 == NULL) {
logError(1, "%s writeCmdU32: ERROR %02X\n", tag, ERROR_ALLOC);
return ERROR_ALLOC;
}
buff2[0] = writeCmd2;
while (remaining > 0) {
if (remaining >= WRITE_CHUNK) {
toWrite = WRITE_CHUNK;
remaining -= WRITE_CHUNK;
} else {
toWrite = remaining;
remaining = 0;
}
buff1[1] = (u8)((address & 0xFF000000) >> 24);
buff1[2] = (u8)((address & 0x00FF0000) >> 16);
buff2[1] = (u8)((address & 0x0000FF00) >> 8);
buff2[2] = (u8)(address & 0xFF);
memcpy(buff2 + 3, dataToWrite, toWrite);
if (fts_writeCmd(buff1, 3) < 0) {
logError(1, "%s writeCmdU32: ERROR %02X\n", tag, ERROR_I2C_W);
return ERROR_I2C_W;
}
if (fts_writeCmd(buff2, 3 + toWrite) < 0) {
logError(1, "%s writeCmdU32: ERROR %02X\n", tag, ERROR_I2C_W);
return ERROR_I2C_W;
}
address += toWrite;
dataToWrite += toWrite;
}
return OK;
}
int writeReadCmdU32(u8 wCmd, u8 rCmd, u32 address, u8 *outBuf, int byteToRead, int hasDummyByte)
{
int remaining = byteToRead;
int toRead = 0;
u8 reaCmd[3];
u8 wriCmd[3];
u8 *buff = (u8 *)kmalloc((READ_CHUNK + 1)*sizeof(u8), GFP_KERNEL);
if (buff == NULL) {
logError(1, "%s writereadCmd32: ERROR %02X\n", tag, ERROR_ALLOC);
return ERROR_ALLOC;
}
reaCmd[0] = rCmd;
wriCmd[0] = wCmd;
while (remaining > 0) {
if (remaining >= READ_CHUNK) {
toRead = READ_CHUNK;
remaining -= READ_CHUNK;
} else {
toRead = remaining;
remaining = 0;
}
wriCmd[1] = (u8)((address & 0xFF000000) >> 24);
wriCmd[2] = (u8)((address & 0x00FF0000) >> 16);
reaCmd[1] = (u8)((address & 0x0000FF00) >> 8);
reaCmd[2] = (u8)(address & 0x000000FF);
if (hasDummyByte) {
if (writeReadCmd(wriCmd, 3, reaCmd, 3, buff, toRead + 1) < 0) {
logError(1, "%s writeCmdU32: ERROR %02X\n", tag, ERROR_I2C_WR);
return ERROR_I2C_WR;
}
memcpy(outBuf, buff + 1, toRead);
} else {
if (writeReadCmd(wriCmd, 3, reaCmd, 3, buff, toRead) < 0)
return ERROR_I2C_WR;
memcpy(outBuf, buff, toRead);
}
address += toRead;
outBuf += toRead;
}
return OK;
}
|