aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/touchscreen/wacom/wacom_i2c_flash.c
blob: cf92018d620a747f4ad5c1759cc0a3782cbf4b95 (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
/*
 *  wacom_i2c_flash.c - Wacom G5 Digitizer Controller (I2C bus)
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/wacom_i2c.h>
#include "wacom_i2c_flash.h"

#define ERR_HEX 0x056
#define RETRY_TRANSFER 5
unsigned char checksum_data[4];

int calc_checksum(unsigned char *flash_data)
{
	unsigned long i;

	if (ums_binary)
		return 0;

	for (i = 0; i < 4; i++)
		checksum_data[i] = 0;

	for (i = 0x0000; i < 0xC000; i += 4) {
		checksum_data[0] ^= flash_data[i];
		checksum_data[1] ^= flash_data[i+1];
		checksum_data[2] ^= flash_data[i+2];
		checksum_data[3] ^= flash_data[i+3];
	}

	printk(KERN_DEBUG
		"[E-PEN] %s : %02x, %02x, %02x, %02x\n",
		__func__, checksum_data[0], checksum_data[1],
		checksum_data[2], checksum_data[3]);

	for (i = 0; i < 4; i++) {
		if (checksum_data[i] != Firmware_checksum[i+1])
			return -ERR_HEX;
	}

	return 0;
}

int wacom_i2c_flash_chksum(struct wacom_i2c *wac_i2c, unsigned char *flash_data,
			   unsigned long *max_address)
{
	unsigned long i;
	unsigned long chksum = 0;

	for (i = 0x0000; i <= *max_address; i++)
		chksum += flash_data[i];

	chksum &= 0xFFFF;

	return (int)chksum;
}

int wacom_i2c_flash_cmd(struct wacom_i2c *wac_i2c)
{

	int ret, len, i;
	u8 buf[10], flashq;
	
#if defined(CONFIG_MACH_KONA)
	buf[0] = 0x0d;
	buf[1] = FLASH_START0;
	buf[2] = FLASH_START1;
	buf[3] = FLASH_START2;
	buf[4] = FLASH_START3;
	buf[5] = FLASH_START4;
	buf[6] = FLASH_START5;
	buf[7] = 0x0d;

	printk(KERN_DEBUG "[E-PEN][jjals] w9002 running!!\n");

	len = 8;
	ret = i2c_master_send(wac_i2c->client, buf, len);

	if (ret < 0) {
		printk(KERN_ERR
			"Sending flash command failed\n");
		return -1;
	}
#else

	buf[0] = 0x0d;
	buf[1] = FLASH_START0;
	buf[2] = FLASH_START1;
	buf[3] = FLASH_START2;
	buf[4] = FLASH_START3;
	buf[5] = FLASH_START4;
	buf[6] = FLASH_START5;
	buf[7] = 0x0d;
	flashq = 0xE0;
	len = 1;

	ret = wacom_i2c_send(wac_i2c, &flashq, len, true);
	if (ret >= 0) {

		i = 0;
		do {
			msleep(1);
			ret = wacom_i2c_recv(wac_i2c,
						&flashq, len, true);
			i++;

			if (i > RETRY)
				return -1;
		} while (flashq == 0xff);
	} else {
		msleep(1);
		len = 8;
		ret = wacom_i2c_send(wac_i2c, buf, len, false);
		if (ret < 0) {
			printk(KERN_ERR
			       "[E-PEN]: Sending flash command failed\n");
			return -1;
		}
		printk(KERN_DEBUG "[E-PEN]: flash send?:%d\n", ret);
		msleep(270);
	}
#endif

	return 0;
}

int wacom_i2c_flash_query(struct wacom_i2c *wac_i2c, u8 query, u8 recvdQuery)
{
	int ret, len, i;
	u8 flashq;

	flashq = query;
	len = 1;

	ret = wacom_i2c_send(wac_i2c, &flashq, len, true);
	if (ret < 0) {
		printk(KERN_ERR "[E-PEN]: query unsent:%d\n", ret);
		return -1;
	}

	/*sleep*/
	msleep(10);
	i = 0;
	do {
		msleep(1);
		flashq = query;
		ret = wacom_i2c_recv(wac_i2c,
						&flashq, len, true);
		i++;

		if (i > RETRY)
			return -1;
		printk(KERN_DEBUG "[E-PEN]: ret:%d flashq:%x\n", ret, flashq);
	} while (recvdQuery == 0xff && flashq != recvdQuery);
	printk(KERN_DEBUG "[E-PEN]: query:%x\n", flashq);

	return flashq;
}

int wacom_i2c_flash_end(struct wacom_i2c *wac_i2c)
{
	int ret;

	/* 2012/07/04 Evaluation for 0x80 and 0xA0 added by Wacom*/
	do {
		ret = wacom_i2c_flash_query(wac_i2c, FLASH_END, FLASH_END);
		if (ret == -1)
			return ERR_FAILED_EXIT;
	} while (ret == 0xA0 || ret != 0x80);
	/* 2012/07/04 Evaluation for 0x80 and 0xA0 added by Wacom*/

	/*2012/07/05
	below added for giving firmware enough time to change to user mode*/
	msleep(1000);

	printk(KERN_DEBUG "[E-PEN] Digitizer activated\n");
	wac_i2c->boot_mode = false;
	return 0;
}

int wacom_i2c_flash_enter(struct wacom_i2c *wac_i2c)
{
	if (wacom_i2c_flash_query(wac_i2c, FLASH_QUERY, FLASH_ACK) == -1)
		return ERR_NOT_FLASH;
	
        wac_i2c->boot_mode = true;

	return 0;
}

int wacom_i2c_flash_BLVer(struct wacom_i2c *wac_i2c)
{
	int ret = 0;
	ret = wacom_i2c_flash_query(wac_i2c, FLASH_BLVER, 0x40);
	if (ret == -1)
		return ERR_UNSENT;

	return ret;
}

int wacom_i2c_flash_mcuId(struct wacom_i2c *wac_i2c)
{
	int ret = 0;

	ret = wacom_i2c_flash_query(wac_i2c, FLASH_MPU, 0x26);
	if (ret == -1)
		return ERR_UNSENT;

	return ret;
}

int wacom_i2c_flash_erase(struct wacom_i2c *wac_i2c, u8 cmd_erase,
			  u8 cmd_block, u8 endBlock)
{
	int len, ret, i, j;
	u8 buf[3], sum, block, flashq;
	unsigned long swtich_slot_time;

	ret = 0;

	for (i = cmd_block; i >= endBlock; i--) {
		block = i;
		block |= 0x80;

		sum = cmd_erase;
		sum += block;
		sum = ~sum + 1;

		buf[0] = cmd_erase;
		buf[1] = block;
		buf[2] = sum;

		len = 3;
		ret = wacom_i2c_send(wac_i2c, buf, len, true);
		if (ret < 0) {
			printk(KERN_ERR "[E-PEN]: Erase failed\n");
			return -1;
		}

		len = 1;
		flashq = 0;
		j = 0;

		do {
			/*sleep */
			msleep(100);
			ret = wacom_i2c_recv(wac_i2c,
						&flashq, len, true);
			j++;

			if (j > RETRY || flashq == 0x84 || flashq == 0x88
			    || flashq == 0x8A || flashq == 0x90) {
				/*
				   0xff:No data
				   0x84:Erase failure
				   0x88:Erase time parameter error
				   0x8A:Write time parameter error
				   0x90:Checksum error
				 */
				printk(KERN_ERR "[E-PEN]: Error:%x\n", flashq);
				return -1;
			}

		/* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
		} while (flashq == 0xff || flashq != 0x06);
		/* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/

		if (printk_timed_ratelimit(&swtich_slot_time, 5000))
			printk(KERN_DEBUG "[E-PEN]: Erasing at %d, ", i);
		/*sleep */
		msleep(1);
	}
	printk(KERN_DEBUG "[E-PEN] Erasing done\n");
	return ret;
}

int wacom_i2c_flash_write(struct wacom_i2c *wac_i2c, unsigned long startAddr,
			  u8 size, unsigned long maxAddr)
{
	unsigned long ulAddr;
	int ret, len, i, j, k;
	char sum;
	u8 buf[WRITE_BUFF], bank;
	unsigned long swtich_slot_time;

	ret = len = i = 0;
	bank = BANK;

	for (ulAddr = startAddr; ulAddr <= maxAddr; ulAddr += BLOCK_SIZE_W) {

		sum = 0;
		buf[0] = FLASH_WRITE;
		buf[1] = (u8) (ulAddr & 0xff);
		buf[2] = (u8) ((ulAddr & 0xff00) >> 8);
		buf[3] = size;
		buf[4] = bank;
#ifdef CONFIG_MACH_T0
		/*Pass Garbage*/
		for (i = 0; i < BLOCK_SIZE_W; i++) {
			if (Binary[ulAddr+i] != 0xff)
				break;
		}
		if (i == BLOCK_SIZE_W) {
			printk(KERN_DEBUG"[E-PEN] Pass ulAddr %u\n",
				(unsigned int)ulAddr);
			continue;
		}
#endif

		for (i = 0; i < 5; i++)
			sum += buf[i];

		sum = ~sum + 1;
		buf[5] = sum;

		len = 6;

		/* 2012/07/18
		* if the returned data is not equal to the length of the bytes
		* that is supposed to send/receive, return it as fail
		*/
		for (k = 0; k < RETRY_TRANSFER; k++) {
			ret = wacom_i2c_send(wac_i2c, buf, len, true);
			if (ret == len)
				break;
			if (ret < 0 || k == (RETRY_TRANSFER - 1)) {
				printk(KERN_ERR "[E-PEN]: Write process aborted\n");
				return ERR_FAILED_ENTER;
			}
		}
		/*2012/07/18*/

		msleep(10);
		len = 1;
		j = 0;
		do {
			msleep(5);
			ret = wacom_i2c_recv(wac_i2c,
						buf, len, true);
			j++;

			if (j > RETRY || buf[0] == 0x90) {
				/*0xff:No data 0x90:Checksum error */
				printk(KERN_ERR "[E-PEN] Error: %x , 0x%lx(%d)\n",
					buf[0], ulAddr, __LINE__);
				return -1;
			}

		/* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
		} while (buf[0] == 0xff || buf[0] != 0x06);
		/* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/

		msleep(1);

		sum = 0;
		for (i = 0; i < BLOCK_SIZE_W; i++) {
			buf[i] = Binary[ulAddr + i];
			sum += Binary[ulAddr + i];
		}
		sum = ~sum + 1;
		buf[BLOCK_SIZE_W] = sum;
		len = BLOCK_SIZE_W + 1;

		/* 2012/07/18
		* if the returned data is not equal to the length of the bytes
		* that is supposed to send/receive, return it as fail
		*/
		for (k = 0; k < RETRY_TRANSFER; k++) {
			ret = wacom_i2c_send(wac_i2c, buf, len, true);
			if (ret == len)
				break;
			if (ret < 0 || k == (RETRY_TRANSFER - 1)) {
				printk(KERN_ERR "[E-PEN]: Write process aborted\n");
				return ERR_FAILED_ENTER;
			}
		}
		/*2012/07/18*/

		msleep(50);
		len = 1;
		j = 0;

		do {
			msleep(10);
			ret = wacom_i2c_recv(wac_i2c,
						buf, len, true);
			j++;

			if (j > RETRY || buf[0] == 0x82 || buf[0] == 0x90) {
				/*
				   0xff:No data
				   0x82:Write error
				   0x90:Checksum error
				 */
				printk(KERN_ERR "[E-PEN] Error: %x , 0x%lx(%d)\n",
					buf[0], ulAddr, __LINE__);
				return -1;
			}

		/* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
		} while (buf[0] == 0xff || buf[0] != 0x06);
		/* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/

		if (printk_timed_ratelimit(&swtich_slot_time, 5000))
			printk(KERN_DEBUG "[E-PEN]: Written on:0x%lx", ulAddr);
		msleep(1);
	}
	printk(KERN_DEBUG "\nWriting done\n");

	return 0;
}

int wacom_i2c_flash_verify(struct wacom_i2c *wac_i2c, unsigned long startAddr,
			   u8 size, unsigned long maxAddr)
{
	unsigned long ulAddr;
	int ret, len, i, j, k;
	char sum;
	u8 buf[WRITE_BUFF], bank;
	unsigned long swtich_slot_time;

	ret = len = i = 0;
	bank = BANK;

	for (ulAddr = startAddr; ulAddr <= maxAddr; ulAddr += BLOCK_SIZE_W) {

		sum = 0;
		buf[0] = FLASH_VERIFY;
		buf[1] = (u8) (ulAddr & 0xff);
		buf[2] = (u8) ((ulAddr & 0xff00) >> 8);
		buf[3] = size;
		buf[4] = bank;

		for (i = 0; i < 5; i++)
			sum += buf[i];
		sum = ~sum + 1;
		buf[5] = sum;

		len = 6;
		j = 0;
		/*sleep */

		/* 2012/07/18
		* if the returned data is not equal to the length of the bytes
		* that is supposed to send/receive, return it as fail
		*/
		for (k = 0; k < RETRY_TRANSFER; k++) {
			ret = wacom_i2c_send(wac_i2c, buf, len, true);
			if (ret == len)
				break;
			if (ret < 0 || k == (RETRY_TRANSFER - 1)) {
				printk(KERN_ERR "[E-PEN]: Write process aborted\n");
				return ERR_FAILED_ENTER;
			}
		}
		/*2012/07/18*/

		len = 1;

		do {
			msleep(1);
			ret = wacom_i2c_recv(wac_i2c,
						buf, len, true);
			j++;
			if (j > RETRY || buf[0] == 0x90) {
				/*0xff:No data 0x90:Checksum error */
				printk(KERN_ERR "[E-PEN] Error: %x , 0x%lx(%d)\n",
					buf[0], ulAddr, __LINE__);
				return -1;
			}
		/* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
		} while (buf[0] == 0xff || buf[0] != 0x06);
		/* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/

		msleep(1);
		sum = 0;
		for (i = 0; i < BLOCK_SIZE_W; i++) {
			buf[i] = Binary[ulAddr + i];
			sum += Binary[ulAddr + i];
		}
		sum = ~sum + 1;
		buf[BLOCK_SIZE_W] = sum;
		len = BLOCK_SIZE_W + 1;

		/* 2012/07/18
		* if the returned data is not equal to the length of the bytes
		* that is supposed to send/receive, return it as fail
		*/
		for (k = 0; k < RETRY_TRANSFER; k++) {
			ret = wacom_i2c_send(wac_i2c, buf, len, true);
			if (ret == len)
				break;
			if (ret < 0 || k == (RETRY_TRANSFER - 1)) {
				printk(KERN_ERR "[E-PEN]: Write process aborted\n");
				return ERR_FAILED_ENTER;
			}
		}
		/*2012/07/18*/

		len = 1;
		j = 0;
		do {
			msleep(2);
			ret = wacom_i2c_recv(wac_i2c,
						buf, len, true);
			j++;

			if (j > RETRY || buf[0] == 0x81 || buf[0] == 0x90) {
				/*
				   0xff:No data
				   0x82:Write error
				   0x90:Checksum error
				 */
				printk(KERN_ERR "[E-PEN] Error: %x , 0x%lx(%d)\n",
					buf[0], ulAddr, __LINE__);
				return -1;
			}

		/* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
		} while (buf[0] == 0xff || buf[0] != 0x06);
		/* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/

		if (printk_timed_ratelimit(&swtich_slot_time, 5000))
			printk(KERN_DEBUG "[E-PEN]: Verified:0x%lx", ulAddr);
		msleep(1);
	}

#if defined(CONFIG_MACH_P4NOTE)
	if (calc_checksum(Binary)) {
		printk(KERN_DEBUG
			"[E-PEN] check sum not matched\n");
		return -ERR_HEX;
	}
#endif

	printk("\n[E-PEN]: Verifying done\n");

	return 0;
}

int wacom_i2c_flash(struct wacom_i2c *wac_i2c)
{
	bool fw_update_enable = false;
	int ret = 0, blver = 0, mcu = 0;
	u32 max_addr = 0, cmd_addr = 0;

	if (Binary == NULL) {
		printk(KERN_ERR"[E-PEN] Data is NULL. Exit.\n");
		return -1;
	}

#if defined(CONFIG_MACH_P4NOTE)
	if (calc_checksum(Binary)) {
		printk(KERN_DEBUG
			"[E-PEN] check sum not matched\n");
		return -ERR_HEX;
	}
#endif

	wake_lock(&wac_i2c->wakelock);

	ret = wacom_i2c_flash_cmd(wac_i2c);
	if (ret < 0)
		goto fw_update_error;
	msleep(10);

	ret = wacom_i2c_flash_enter(wac_i2c);
	printk(KERN_DEBUG "[E-PEN]: flashEnter:%d\n", ret);
	msleep(10);

	blver = wacom_i2c_flash_BLVer(wac_i2c);
	printk(KERN_DEBUG "[E-PEN]: blver:%d\n", blver);
	msleep(10);

	mcu = wacom_i2c_flash_mcuId(wac_i2c);
	printk(KERN_DEBUG "[E-PEN]: mcu:%x\n", mcu);
	if (Mpu_type != mcu) {
		printk(KERN_DEBUG "[E-PEN]: mcu:%x\n", mcu);
		ret = -ENXIO;
		goto mcu_type_error;
	}
	msleep(1);

	switch (mcu) {
	case MPUVER_W8501:
		printk(KERN_DEBUG "[E-PEN]: flashing for w8501 started\n");
		max_addr = MAX_ADDR_W8501;
		cmd_addr = MAX_BLOCK_W8501;
		fw_update_enable = true;
		break;

	case MPUVER_514:
		printk(KERN_DEBUG "[E-PEN]: Flashing for 514 started\n");
		max_addr = MAX_ADDR_514;
		cmd_addr = MAX_BLOCK_514;
		fw_update_enable = true;
		break;

	case MPUVER_505:
		printk(KERN_DEBUG "[E-PEN]: Flashing for 505 started\n");
		max_addr = MAX_ADDR_505;
		cmd_addr = MAX_BLOCK_505;
		fw_update_enable = true;
		break;

	default:
		printk(KERN_DEBUG "[E-PEN]: default called\n");
		break;
	}

	if (fw_update_enable) {
		bool valid_hex = false;
		int cnt = 0;
		/*2012/07/04: below modified by Wacom*/
		/*If wacom_i2c_flash_verify returns -ERR_HEX, */
		/*please redo whole process of flashing from  */
		/*wacom_i2c_flash_erase                       */
		do {
			ret = wacom_i2c_flash_erase(wac_i2c, FLASH_ERASE,
				    cmd_addr, END_BLOCK);
			if (ret < 0) {
				printk(KERN_ERR "[E-PEN] error - erase\n");
				continue;
			}
			msleep(20);

			ret = wacom_i2c_flash_write(wac_i2c, START_ADDR,
				    NUM_BLOCK_2WRITE, max_addr);
			if (ret < 0) {
				printk(KERN_ERR "[E-PEN] error - writing\n");
				continue;
			}
			msleep(20);

			ret = wacom_i2c_flash_verify(wac_i2c, START_ADDR,
				     NUM_BLOCK_2WRITE, max_addr);

			if (ret == -ERR_HEX)
				printk(KERN_DEBUG "[E-PEN]: firmware is not valied\n");
			else if (ret < 0) {
				printk(KERN_ERR "[E-PEN] error - verifying\n");
				continue;
			} else
				valid_hex = true;
		} while ((!valid_hex) && (cnt < 10));
		/*2012/07/04: Wacom*/

		printk(KERN_DEBUG "[E-PEN]: Firmware successfully updated\n");
	}
	msleep(1);

mcu_type_error:
	wacom_i2c_flash_end(wac_i2c);
	if (ret < 0)
		printk(KERN_ERR "[E-PEN] error - wacom_i2c_flash_end\n");

fw_update_error:
	wake_unlock(&wac_i2c->wakelock);
	return ret;
}