-
Notifications
You must be signed in to change notification settings - Fork 27
/
wacom_i2c.c
3410 lines (2818 loc) · 89.3 KB
/
wacom_i2c.c
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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* wacom_i2c.c - Wacom 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/kernel.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/firmware.h>
#include <linux/regulator/consumer.h>
#include <linux/of_gpio.h>
#include <linux/spu-verify.h>
#include "wacom.h"
#define WACOM_FW_SIZE 131072
#define WACOM_I2C_RETRY 3
#define WACOM_PATH_EXTERNAL_FW "/sdcard/FIRMWARE/WACOM/wacom.bin"
#define WACOM_PATH_EXTERNAL_FW_SIGNED "/sdcard/FIRMWARE/WACOM/wacom_signed.bin"
#define WACOM_PATH_SPU_FW_SIGNED "/spu/WACOM/ffu_wacom.bin"
#define WACOM_INVALID_IRQ_COUNT 2
int set_spen_mode(int mode)
{
return 0;
}
struct wacom_i2c *wacom_get_drv_data(void *data)
{
static void *drv_data;
if (unlikely(data))
drv_data = data;
return (struct wacom_i2c *)drv_data;
}
int coordc;
void forced_release(struct wacom_i2c *wac_i2c)
{
input_info(true, &wac_i2c->client->dev, "%s\n", __func__);
input_report_abs(wac_i2c->input_dev, ABS_X, 0);
input_report_abs(wac_i2c->input_dev, ABS_Y, 0);
input_report_abs(wac_i2c->input_dev, ABS_PRESSURE, 0);
input_report_key(wac_i2c->input_dev, BTN_STYLUS, 0);
input_report_key(wac_i2c->input_dev, BTN_TOUCH, 0);
/* prevent invalid operation of input booster */
input_sync(wac_i2c->input_dev);
input_report_abs(wac_i2c->input_dev, ABS_DISTANCE, 0);
input_report_key(wac_i2c->input_dev, BTN_TOOL_RUBBER, 0);
input_report_key(wac_i2c->input_dev, BTN_TOOL_PEN, 0);
input_sync(wac_i2c->input_dev);
wac_i2c->pen_prox = 0;
wac_i2c->pen_pressed = 0;
wac_i2c->side_pressed = 0;
wac_i2c->mcount = 0;
wac_i2c->virtual_tracking = EPEN_POS_NONE;
}
int wacom_i2c_send(struct wacom_i2c *wac_i2c,
const char *buf, int count, bool mode)
{
struct i2c_client *client = mode ? wac_i2c->client_boot : wac_i2c->client;
int retry = WACOM_I2C_RETRY;
int ret;
u8 *buff;
buff = kzalloc(count, GFP_KERNEL);
if (!buff)
return -ENOMEM;
memcpy(buff, buf, count);
do {
if (!wac_i2c->power_enable) {
input_err(true, &wac_i2c->client->dev, "%s: Power status off\n",
__func__);
ret = -EIO;
goto out;
}
ret = i2c_master_send(client, buff, count);
if (ret == count)
break;
if (retry < WACOM_I2C_RETRY) {
input_err(true, &wac_i2c->client->dev, "%s: I2C retry(%d)\n",
__func__, WACOM_I2C_RETRY - retry);
wac_i2c->i2c_fail_count++;
}
} while (--retry);
out:
kfree(buff);
return ret;
}
int wacom_i2c_recv(struct wacom_i2c *wac_i2c, char *buf, int count, bool mode)
{
struct i2c_client *client = mode ? wac_i2c->client_boot : wac_i2c->client;
int retry = WACOM_I2C_RETRY;
int ret;
u8 *buff;
buff = kzalloc(count, GFP_KERNEL);
if (!buff)
return -ENOMEM;
do {
if (!wac_i2c->power_enable) {
input_err(true, &wac_i2c->client->dev, "%s: Power status off\n",
__func__);
ret = -EIO;
goto out;
}
ret = i2c_master_recv(client, buff, count);
if (ret == count)
break;
if (retry < WACOM_I2C_RETRY) {
input_err(true, &wac_i2c->client->dev, "%s: I2C retry(%d)\n",
__func__, WACOM_I2C_RETRY - retry);
wac_i2c->i2c_fail_count++;
}
} while (--retry);
memcpy(buf, buff, count);
out:
kfree(buff);
return ret;
}
int wacom_checksum(struct wacom_i2c *wac_i2c)
{
struct i2c_client *client = wac_i2c->client;
int ret = 0, retry = 10;
int i = 0;
u8 buf[5] = { 0, };
while (retry--) {
buf[0] = COM_CHECKSUM;
ret = wacom_i2c_send(wac_i2c, &buf[0], 1,
WACOM_I2C_MODE_NORMAL);
if (ret < 0) {
input_err(true, &client->dev, "i2c fail, retry, %d\n",
__LINE__);
continue;
}
msleep(200);
ret = wacom_i2c_recv(wac_i2c, buf, 5, WACOM_I2C_MODE_NORMAL);
if (ret < 0) {
input_err(true, &client->dev, "i2c fail, retry, %d\n",
__LINE__);
continue;
}
if (buf[0] == 0x1f)
break;
input_info(true, &client->dev, "buf[0]: 0x%x, checksum retry\n",
buf[0]);
}
if (ret >= 0) {
input_info(true, &client->dev,
"received checksum %x, %x, %x, %x, %x\n", buf[0],
buf[1], buf[2], buf[3], buf[4]);
}
for (i = 0; i < 5; ++i) {
if (buf[i] != wac_i2c->fw_chksum[i]) {
input_info(true, &client->dev,
"checksum fail %dth %x %x\n", i, buf[i],
wac_i2c->fw_chksum[i]);
break;
}
}
wac_i2c->checksum_result = (i == 5);
return wac_i2c->checksum_result;
}
int wacom_i2c_query(struct wacom_i2c *wac_i2c)
{
struct wacom_g5_platform_data *pdata = wac_i2c->pdata;
struct i2c_client *client = wac_i2c->client;
u8 data[COM_QUERY_BUFFER] = { 0, };
u8 *query = data + COM_QUERY_POS;
int read_size = COM_QUERY_BUFFER;
int ret;
int i;
int max_x, max_y, pressure;
for (i = 0; i < RETRY_COUNT; i++) {
ret = wacom_i2c_recv(wac_i2c, data, read_size,
WACOM_I2C_MODE_NORMAL);
if (ret < 0) {
input_err(true, &client->dev, "%s: failed to recv\n",
__func__);
continue;
}
input_info(true, &client->dev,
"%s: %dth ret of wacom query=%d\n", __func__, i,
ret);
if (read_size != ret) {
input_err(true, &client->dev,
"%s: read size error %d of %d\n", __func__,
ret, read_size);
continue;
}
if (query[EPEN_REG_HEADER] == 0x0f) {
wac_i2c->fw_ver_ic =
((u16)query[EPEN_REG_FWVER1] << 8) +
query[EPEN_REG_FWVER2];
break;
}
}
input_info(true, &client->dev,
"%X, %X, %X, %X, %X, %X, %X, %X, %X, %X, %X, %X, %X, %X, %X\n",
query[0], query[1], query[2], query[3], query[4], query[5],
query[6], query[7], query[8], query[9], query[10], query[11],
query[12], query[13], query[14]);
if (ret < 0) {
input_err(true, &client->dev, "%s: failed to read query\n",
__func__);
wac_i2c->fw_ver_ic = 0;
wac_i2c->query_status = false;
return ret;
}
wac_i2c->query_status = true;
max_x = ((u16)query[EPEN_REG_X1] << 8) + query[EPEN_REG_X2];
max_y = ((u16)query[EPEN_REG_Y1] << 8) + query[EPEN_REG_Y2];
pressure = ((u16)query[EPEN_REG_PRESSURE1] << 8) + query[EPEN_REG_PRESSURE2];
if (!pdata->use_dt_coord) {
pdata->max_x = max_x;
pdata->max_y = max_y;
}
pdata->max_pressure = pressure;
pdata->max_x_tilt = query[EPEN_REG_TILT_X];
pdata->max_y_tilt = query[EPEN_REG_TILT_Y];
pdata->max_height = query[EPEN_REG_HEIGHT];
pdata->ic_type = query[EPEN_REG_MPUVER];
input_info(true, &client->dev, "use_dt_coord=%d, max_x=%d max_y=%d, max_pressure=%d\n",
pdata->use_dt_coord, pdata->max_x, pdata->max_y, pdata->max_pressure);
input_info(true, &client->dev, "fw_ver_ic=0x%X\n", wac_i2c->fw_ver_ic);
input_info(true, &client->dev, "mpu %#x, bl %#x, tx %d, ty %d, h %d\n",
query[EPEN_REG_MPUVER], query[EPEN_REG_BLVER], query[EPEN_REG_TILT_X],
query[EPEN_REG_TILT_Y], query[EPEN_REG_HEIGHT]);
return 0;
}
int wacom_i2c_set_sense_mode(struct wacom_i2c *wac_i2c)
{
struct i2c_client *client = wac_i2c->client;
int retval;
char data[4] = { 0, 0, 0, 0 };
input_info(true, &wac_i2c->client->dev, "%s cmd mod(%d)\n", __func__,
wac_i2c->wcharging_mode);
if (wac_i2c->wcharging_mode == 1)
data[0] = COM_LOW_SENSE_MODE;
else if (wac_i2c->wcharging_mode == 3)
data[0] = COM_LOW_SENSE_MODE2;
else {
/* it must be 0 */
data[0] = COM_NORMAL_SENSE_MODE;
}
retval = wacom_i2c_send(wac_i2c, &data[0], 1, WACOM_I2C_MODE_NORMAL);
if (retval != 1) {
input_err(true, &client->dev,
"%s: failed to send wacom i2c mode, %d\n", __func__,
retval);
return retval;
}
msleep(60);
data[0] = COM_SAMPLERATE_STOP;
retval = wacom_i2c_send(wac_i2c, &data[0], 1, WACOM_I2C_MODE_NORMAL);
if (retval != 1) {
input_err(true, &client->dev,
"%s: failed to read wacom i2c send1, %d\n", __func__,
retval);
return retval;
}
wac_i2c->samplerate_state = false;
msleep(60);
#if 0 /* temp block not to receive gabage irq by cmd */
data[1] = COM_REQUEST_SENSE_MODE;
retval = wacom_i2c_send(wac_i2c, &data[1], 1, WACOM_I2C_MODE_NORMAL);
if (retval != 1) {
input_err(true, &client->dev,
"%s: failed to read wacom i2c send2, %d\n", __func__,
retval);
return retval;
}
msleep(60);
retval = wacom_i2c_recv(wac_i2c, &data[2], 2, WACOM_I2C_MODE_NORMAL);
if (retval != 2) {
input_err(true, &client->dev,
"%s: failed to read wacom i2c recv, %d\n", __func__,
retval);
return retval;
}
input_info(true, &client->dev, "%s: mode:%X, %X\n", __func__, data[2],
data[3]);
data[0] = COM_SAMPLERATE_STOP;
retval = wacom_i2c_send(wac_i2c, &data[0], 1, WACOM_I2C_MODE_NORMAL);
if (retval != 1) {
input_err(true, &client->dev,
"%s: failed to read wacom i2c send3, %d\n", __func__,
retval);
return retval;
}
msleep(60);
#endif
data[0] = COM_SAMPLERATE_START;
retval = wacom_i2c_send(wac_i2c, &data[0], 1, WACOM_I2C_MODE_NORMAL);
if (retval != 1) {
input_err(true, &client->dev,
"%s: failed to read wacom i2c send4, %d\n", __func__,
retval);
return retval;
}
wac_i2c->samplerate_state = true;
msleep(60);
return data[3];
}
void wacom_select_survey_mode(struct wacom_i2c *wac_i2c, bool enable)
{
struct i2c_client *client = wac_i2c->client;
mutex_lock(&wac_i2c->mode_lock);
if (enable) {
if (wac_i2c->epen_blocked ||
(wac_i2c->battery_saving_mode && !(wac_i2c->function_result & EPEN_EVENT_PEN_OUT))) {
if (wac_i2c->pdata->use_garage) {
input_info(true, &client->dev,
"%s: %s & garage on. garage only mode\n",
__func__,
wac_i2c->epen_blocked ? "epen blocked" : "ps on & pen in");
wacom_i2c_set_survey_mode(wac_i2c,
EPEN_SURVEY_MODE_GARAGE_ONLY);
} else {
input_info(true, &client->dev,
"%s: %s & garage off. power off\n", __func__,
wac_i2c->epen_blocked ? "epen blocked" : "ps on & pen in");
wacom_enable_irq(wac_i2c, false);
wacom_enable_pdct_irq(wac_i2c, false);
wacom_power(wac_i2c, false);
wac_i2c->survey_mode = EPEN_SURVEY_MODE_NONE;
wac_i2c->function_result &= ~EPEN_EVENT_SURVEY;
}
} else if (wac_i2c->survey_mode) {
input_info(true, &client->dev, "%s: exit aop mode\n",
__func__);
wacom_i2c_set_survey_mode(wac_i2c,
EPEN_SURVEY_MODE_NONE);
} else {
input_info(true, &client->dev, "%s: power on\n",
__func__);
wacom_power(wac_i2c, true);
msleep(100);
wacom_enable_irq(wac_i2c, true);
wacom_enable_pdct_irq(wac_i2c, true);
}
} else {
if (wac_i2c->epen_blocked ||
(wac_i2c->battery_saving_mode && !(wac_i2c->function_result & EPEN_EVENT_PEN_OUT))) {
if (wac_i2c->pdata->use_garage) {
input_info(true, &client->dev,
"%s: %s & garage on. garage only mode\n",
__func__,
wac_i2c->epen_blocked ? "epen blocked" : "ps on & pen in");
wacom_i2c_set_survey_mode(wac_i2c,
EPEN_SURVEY_MODE_GARAGE_ONLY);
} else {
input_info(true, &client->dev,
"%s: %s & garage off. power off\n", __func__,
wac_i2c->epen_blocked ? "epen blocked" : "ps on & pen in");
wacom_enable_irq(wac_i2c, false);
wacom_enable_pdct_irq(wac_i2c, false);
wacom_power(wac_i2c, false);
wac_i2c->survey_mode = EPEN_SURVEY_MODE_NONE;
wac_i2c->function_result &= ~EPEN_EVENT_SURVEY;
}
} else if (!(wac_i2c->function_set & EPEN_SETMODE_AOP)) {
if (wac_i2c->pdata->use_garage) {
input_info(true, &client->dev,
"%s: aop off & garage on. garage only mode\n",
__func__);
wacom_i2c_set_survey_mode(wac_i2c,
EPEN_SURVEY_MODE_GARAGE_ONLY);
} else {
input_info(true, &client->dev,
"%s: aop off & garage off. power off\n",
__func__);
wacom_enable_irq(wac_i2c, false);
wacom_enable_pdct_irq(wac_i2c, false);
wacom_power(wac_i2c, false);
wac_i2c->survey_mode = EPEN_SURVEY_MODE_NONE;
wac_i2c->function_result &= ~EPEN_EVENT_SURVEY;
}
} else {
/* aop on => (aod : screen off memo = 1:1 or 1:0 or 0:1)
* double tab & hover + button event will be occurred,
* but some of them will be skipped at reporting by mode
*/
input_info(true, &client->dev,
"%s: aop on. aop mode\n", __func__);
if (!wac_i2c->power_enable) {
input_info(true, &client->dev, "%s: power on\n",
__func__);
wacom_power(wac_i2c, true);
msleep(100);
wacom_enable_irq(wac_i2c, true);
wacom_enable_pdct_irq(wac_i2c, true);
}
wacom_i2c_set_survey_mode(wac_i2c,
EPEN_SURVEY_MODE_GARAGE_AOP);
}
}
if (wac_i2c->power_enable) {
input_info(true, &client->dev, "%s: screen %s, survey mode:%d, result:%d\n",
__func__, enable ? "on" : "off",
wac_i2c->survey_mode,
wac_i2c->function_result & EPEN_EVENT_SURVEY);
if ((wac_i2c->function_result & EPEN_EVENT_SURVEY) != wac_i2c->survey_mode) {
input_err(true, &client->dev, "%s: survey mode cmd failed\n",
__func__);
wacom_i2c_set_survey_mode(wac_i2c,
wac_i2c->survey_mode);
}
}
mutex_unlock(&wac_i2c->mode_lock);
}
int wacom_i2c_set_survey_mode(struct wacom_i2c *wac_i2c, int mode)
{
struct i2c_client *client = wac_i2c->client;
int retval;
char data[4] = { 0, 0, 0, 0 };
switch (mode) {
case EPEN_SURVEY_MODE_NONE:
data[0] = COM_SURVEYEXIT;
break;
case EPEN_SURVEY_MODE_GARAGE_ONLY:
if (!wac_i2c->pdata->use_garage) {
input_err(true, &client->dev,
"%s: garage mode is not supported\n", __func__);
return -EPERM;
}
data[0] = COM_SURVEY_TARGET_GARAGEONLY;
break;
case EPEN_SURVEY_MODE_GARAGE_AOP:
/* octa(R7) : only use COM_SURVEYSCAN, yocta : need to use COM_SURVEYSYNCSCAN register */
// if ((wac_i2c->function_set & EPEN_SETMODE_AOP_OPTION_AOD_LCD_ON)
// == EPEN_SETMODE_AOP_OPTION_AOD_LCD_ON)
// data[0] = COM_SURVEYSYNCSCAN;
// else
data[0] = COM_SURVEYSCAN;
break;
default:
input_err(true, &client->dev,
"%s: wrong param %d\n", __func__, mode);
return -EINVAL;
}
wac_i2c->survey_mode = mode;
input_info(true, &client->dev, "%s: ps %s & mode : %d cmd(0x%2X)\n", __func__,
wac_i2c->battery_saving_mode ? "on" : "off", mode, data[0]);
retval = wacom_i2c_send(wac_i2c, &data[0], 1, WACOM_I2C_MODE_NORMAL);
if (retval != 1) {
input_err(true, &client->dev, "%s: failed to send data(%02x %d)\n",
__func__, data[0], retval);
wac_i2c->reset_flag = true;
return retval;
}
wac_i2c->check_survey_mode = mode;
if (mode)
msleep(300);
wac_i2c->reset_flag = false;
wac_i2c->function_result &= ~EPEN_EVENT_SURVEY;
wac_i2c->function_result |= mode;
return 0;
}
void forced_release_fullscan(struct wacom_i2c *wac_i2c)
{
input_info(true, &wac_i2c->client->dev, "%s: full scan OUT\n", __func__);
cancel_delayed_work(&wac_i2c->gxscan_work);
cancel_delayed_work(&wac_i2c->fullscan_work);
wac_i2c->tsp_noise_mode = set_spen_mode(EPEN_GLOBAL_SCAN_MODE);
wac_i2c->fullscan_mode = false;
}
int wacom_get_scan_mode(struct wacom_i2c *wac_i2c)
{
struct i2c_client *client = wac_i2c->client;
int retval;
char data[COM_COORD_NUM + 1] = { 0, };
int i, retry = 0, temp = 0, ret = 0;
input_info(true, &wac_i2c->client->dev, "%s\n", __func__);
data[0] = COM_SAMPLERATE_STOP;
retval = wacom_i2c_send(wac_i2c, &data[0], 1, WACOM_I2C_MODE_NORMAL);
if (retval != 1) {
input_err(true, &client->dev,
"%s: failed to read wacom i2c send (stop), %d\n",
__func__, retval);
return -EIO;
}
wac_i2c->samplerate_state = false;
msleep(30);
retry = 5;
while (retry--) {
if (wacom_get_irq_state(wac_i2c) > 0) {
u8 data[COM_COORD_NUM + 1] = { 0, };
retval = wacom_i2c_recv(wac_i2c, data, COM_COORD_NUM + 1,
WACOM_I2C_MODE_NORMAL);
if (retval < 0) {
input_err(true, &client->dev,
"%s: failed to receive\n", __func__);
} else {
input_err(true, &client->dev,
"%s: ignore gabage data\n", __func__);
}
} else {
break;
}
usleep_range(4500, 5500);
}
retry = 3;
while (retry--) {
data[0] = COM_REQUESTSCANMODE;
retval = wacom_i2c_send(wac_i2c, &data[0], 1, WACOM_I2C_MODE_NORMAL);
if (retval != 1) {
input_err(true, &client->dev,
"%s: failed to read wacom i2c send (request data), %d\n",
__func__, retval);
usleep_range(4500, 5500);
continue;
}
msleep(55);
retval = wacom_i2c_recv(wac_i2c, data, COM_COORD_NUM + 1,
WACOM_I2C_MODE_NORMAL);
if (retval < 0) {
input_err(true, &client->dev,
"%s: failed to read wacom i2c send survey, %d\n",
__func__, retval);
usleep_range(4500, 5500);
continue;
}
temp = 0;
for (i = 0; i < COM_COORD_NUM; i++)
temp += data[i];
input_info(true, &client->dev,
"%x %x %x %x, %x %x %x %x, %x %x %x %x, %x %x\n",
data[0], data[1], data[2], data[3], data[4], data[5],
data[6], data[7], data[8], data[9], data[10],
data[11], data[12], data[13]);
if (temp == 0) { /* unlock */
ret = EPEN_GLOBAL_SCAN_MODE;
break;
} else if (data[12] == 0x01) { /* send noise mode to tsp */
/* skip high noise mode after crown, ret = EPEN_HIGH_NOISE_MODE; */
ret = -EPERM;
if (!wac_i2c->screen_on && !(temp - data[12]))
ret = -EPERM;
break;
}
msleep(10);
}
input_info(true, &client->dev,
"data[0] = %x, data[12] = %x, retry(%d) ret(%d)\n", data[0],
data[12], 3 - retry, ret);
data[0] = COM_SAMPLERATE_133;
retval = wacom_i2c_send(wac_i2c, &data[0], 1, WACOM_I2C_MODE_NORMAL);
if (retval != 1) {
input_err(true, &client->dev,
"%s: failed to read wacom i2c send (start), %d\n",
__func__, retval);
return -EIO;
}
wac_i2c->samplerate_state = true;
return ret;
}
bool wacom_get_status_data(struct wacom_i2c *wac_i2c, char *data)
{
struct i2c_client *client = wac_i2c->client;
int retval;
int retry = 5;
bool tsp = false, rdy = false;
input_info(true, &wac_i2c->client->dev, "%s\n", __func__);
while (retry--) {
retval = wacom_i2c_recv(wac_i2c, data, COM_COORD_NUM + 1,
WACOM_I2C_MODE_NORMAL);
if (retval >= 0)
break;
input_err(true, &client->dev, "%s: failed to read i2c retry(%d)\n",
__func__, retry);
}
if (retval < 0) {
input_err(true, &client->dev, "%s: failed to read status data, %d\n",
__func__, retval);
return false;
}
if (coordc < 100)
coordc++;
rdy = data[0] & 0x80;
tsp = data[12] & 0x01;
if (wac_i2c->pdata->table_swap && data[0] == TABLE_SWAP_DATA &&
data[2] == 0 && data[3] == 0 && data[4] == 0) {
wac_i2c->dp_connect_state = data[1];
input_info(true, &client->dev,
"%s: usb typec DP %sconnected\n",
__func__, wac_i2c->dp_connect_state ? "" : "dis");
return false;
}
/* Noise status : 11 11 = High noise, 22 22 = Low noise */
if (data[0] == 0x0F) {
input_info(true, &client->dev,
"%x %x %x %x, %x %x %x %x, %x %x %x %x, %x %x\n",
data[0], data[1], data[2], data[3], data[4], data[5],
data[6], data[7], data[8], data[9], data[10],
data[11], data[12], data[13]);
if ((data[10] == WACOM_NOISE_HIGH) && (data[11] == WACOM_NOISE_HIGH)) {
input_err(true, &wac_i2c->client->dev, "11 11 high-noise mode\n");
wac_i2c->wacom_noise_state = WACOM_NOISE_HIGH;
wac_i2c->tsp_noise_mode = set_spen_mode(EPEN_HIGH_NOISE_MODE);
} else if ((data[10] == WACOM_NOISE_LOW) && (data[11] == WACOM_NOISE_LOW)) {
input_err(true, &wac_i2c->client->dev, "22 22 low-noise mode\n");
wac_i2c->wacom_noise_state = WACOM_NOISE_LOW;
wac_i2c->tsp_noise_mode = set_spen_mode(EPEN_GLOBAL_SCAN_MODE);
}
return false;
}
/* AOP status : BB BB = Button+Hover, DD DD = Double Tab gesture */
if ((data[10] == AOP_BUTTON_HOVER) || (data[10] == AOP_DOUBLE_TAB)) {
if (data[10] != data[11]) {
input_err(true, &client->dev,
"%s: invalid status data\n", __func__);
return false;
}
input_info(true, &client->dev, "%s: data[10] = %x, data[11] = %x\n",
__func__, data[10], data[11]);
return true;
}
if (tsp && !wac_i2c->fullscan_mode) {
input_info(true, &client->dev,
"%x %x %x %x, %x %x %x %x, %x %x %x %x, %x %x coordc(%d)\n",
data[0], data[1], data[2], data[3], data[4], data[5],
data[6], data[7], data[8], data[9], data[10],
data[11], data[12], data[13], coordc);
if (data[10] == HSYNC_COUNTER_UMAGIC && data[11] == HSYNC_COUNTER_LMAGIC) {
input_info(true, &client->dev, "x scan IN, rdy(%d) tsp(%d)\n",
rdy, tsp);
}
wac_i2c->fullscan_mode = true;
if (wac_i2c->wacom_noise_state != WACOM_NOISE_HIGH)
wac_i2c->tsp_noise_mode = set_spen_mode(EPEN_LOCAL_SCAN_MODE);
else
input_info(true, &client->dev, "high noise mode, skip tsp (F3 1)\n");
coordc = 0;
cancel_delayed_work(&wac_i2c->gxscan_work);
schedule_delayed_work(&wac_i2c->gxscan_work,
msecs_to_jiffies(1500));
}
if (!tsp) {
input_info(true, &client->dev, "full scan out (%02X %02X)\n", data[10], data[11]);
if (wac_i2c->wacom_noise_state != WACOM_NOISE_HIGH)
wac_i2c->tsp_noise_mode = set_spen_mode(EPEN_GLOBAL_SCAN_MODE);
else
input_info(true, &client->dev, "high noise mode, skip tsp (F3 0)\n");
cancel_delayed_work(&wac_i2c->gxscan_work);
wac_i2c->fullscan_mode = false;
}
return false;
}
void wacom_gxscan_work(struct work_struct *work)
{
struct wacom_i2c *wac_i2c =
container_of(work, struct wacom_i2c, gxscan_work.work);
int ret;
input_info(true, &wac_i2c->client->dev,
"gxscan_work irq-cnt(%d)\n", coordc);
if (coordc <= WACOM_INVALID_IRQ_COUNT) {
wacom_enable_irq(wac_i2c, false);
ret = wacom_get_scan_mode(wac_i2c);
if (ret < 0) {
input_info(true, &wac_i2c->client->dev, "work - stay scan mode\n");
} else if (ret == EPEN_GLOBAL_SCAN_MODE) {
if (wac_i2c->wacom_noise_state != WACOM_NOISE_HIGH) {
input_info(true, &wac_i2c->client->dev, "work - full scan OUT\n");
wac_i2c->tsp_noise_mode = set_spen_mode(EPEN_GLOBAL_SCAN_MODE);
wac_i2c->fullscan_mode = false;
} else {
input_info(true, &wac_i2c->client->dev, "high noise mode, skip tsp (F3 0)\n");
}
} else if (ret == EPEN_HIGH_NOISE_MODE) {
if (wac_i2c->wacom_noise_state != WACOM_NOISE_HIGH) {
input_info(true, &wac_i2c->client->dev, "work - wacom noise mode\n");
wac_i2c->tsp_noise_mode = set_spen_mode(EPEN_HIGH_NOISE_MODE);
} else {
input_info(true, &wac_i2c->client->dev, "high noise mode, skip tsp (F3 2)\n");
}
} else {
input_err(true, &wac_i2c->client->dev, "unexpected status\n");
}
wacom_enable_irq(wac_i2c, true);
}
}
void wacom_fullscan_work(struct work_struct *work)
{
struct wacom_i2c *wac_i2c =
container_of(work, struct wacom_i2c, fullscan_work.work);
int ret;
char data[4] = { 0, 0, 0, 0 };
input_info(true, &wac_i2c->client->dev,
"fullscan_work fullscan_mode(%d)\n", wac_i2c->fullscan_mode);
if (wac_i2c->fullscan_mode) {
input_info(true, &wac_i2c->client->dev, "reset dsp\n");
data[0] = COM_RESET_DSP;
ret = wacom_i2c_send(wac_i2c, &data[0], 1, WACOM_I2C_MODE_NORMAL);
if (ret != 1) {
input_err(true, &wac_i2c->client->dev,
"%s: failed to send wacom i2c mode, %d\n", __func__,
ret);
}
wac_i2c->tsp_noise_mode = set_spen_mode(EPEN_GLOBAL_SCAN_MODE);
wac_i2c->fullscan_mode = false;
/* we expect dsp_reset cmd will recover scan mdoe and send fullscan out cmd
wac_i2c->tsp_noise_mode = set_spen_mode(EPEN_GLOBAL_SCAN_MODE);
wac_i2c->fullscan_mode = false;
wacom_i2c_set_survey_mode(wac_i2c,
EPEN_SURVEY_MODE_GARAGE_ONLY);
msleep(30);
wacom_i2c_set_survey_mode(wac_i2c,
EPEN_SURVEY_MODE_NONE);
*/
}
}
#define EPEN_LOCATION_DETECT_SIZE 6
void epen_location_detect(struct wacom_i2c *wac_i2c, char *loc, int x, int y)
{
int i;
int max_x = wac_i2c->pdata->max_x;
int max_y = wac_i2c->pdata->max_y;
if (wac_i2c->pdata->xy_switch) {
max_x = wac_i2c->pdata->max_y;
max_y = wac_i2c->pdata->max_x;
}
if (wac_i2c->pdata->area_indicator == 0){
/* approximately value */
wac_i2c->pdata->area_indicator = max_y * 4 / 100;
wac_i2c->pdata->area_navigation = max_y * 6 / 100;
wac_i2c->pdata->area_edge = max_y * 3 / 100;
input_info(true, &wac_i2c->client->dev,
"MAX XY(%d/%d), area_edge %d, area_indicator %d, area_navigation %d\n",
max_x, max_y, wac_i2c->pdata->area_edge,
wac_i2c->pdata->area_indicator, wac_i2c->pdata->area_navigation);
}
for (i = 0; i < EPEN_LOCATION_DETECT_SIZE; ++i)
loc[i] = 0;
if (x < wac_i2c->pdata->area_edge)
strcat(loc, "E.");
else if (x < (max_x - wac_i2c->pdata->area_edge))
strcat(loc, "C.");
else
strcat(loc, "e.");
if (y < wac_i2c->pdata->area_indicator)
strcat(loc, "S");
else if (y < (max_y - wac_i2c->pdata->area_navigation))
strcat(loc, "C");
else
strcat(loc, "N");
}
int wacom_i2c_coord(struct wacom_i2c *wac_i2c)
{
struct wacom_g5_platform_data *pdata = wac_i2c->pdata;
struct i2c_client *client = wac_i2c->client;
u8 data[COM_COORD_NUM + 1] = { 0, };
bool prox = false;
bool rdy = false;
int ret = 0, tsp = 0;
int stylus;
s16 x, y, pressure;
s16 tmp;
u8 height = 0;
s8 tilt_x = 0;
s8 tilt_y = 0;
s8 retry = 3;
char location[EPEN_LOCATION_DETECT_SIZE] = { 0, };
while (retry--) {
ret = wacom_i2c_recv(wac_i2c, data, COM_COORD_NUM + 1,
WACOM_I2C_MODE_NORMAL);
if (ret >= 0)
break;
input_err(true, &client->dev,
"%s failed to read i2c.retry %d.L%d\n", __func__,
retry, __LINE__);
}
if (ret < 0) {
input_err(true, &client->dev, "i2c err, exit %s\n", __func__);
forced_release(wac_i2c);
wac_i2c->reset_flag = true;
if (work_busy(&wac_i2c->resume_work.work) == 0) {
input_err(true, &client->dev,
"%s schedule resume work\n", __func__);
cancel_delayed_work(&wac_i2c->resume_work);
schedule_delayed_work(&wac_i2c->resume_work,
msecs_to_jiffies(EPEN_RESUME_DELAY));
} else {
input_err(true, &client->dev,
"%s resume work is busy\n", __func__);
}
return 0;
}
#ifndef CONFIG_SAMSUNG_PRODUCT_SHIP
#if 0
input_info(true, &client->dev,
"%x %x %x %x, %x %x %x %x, %x %x %x %x, %x %x\n",
data[0], data[1], data[2], data[3], data[4], data[5],
data[6], data[7], data[8], data[9], data[10], data[11],
data[12], data[13]);
#endif
#endif
rdy = data[0] & 0x80;
x = ((u16) data[1] << 8) + (u16) data[2];
y = ((u16) data[3] << 8) + (u16) data[4];
pressure = ((u16) data[5] << 8) + (u16) data[6];
height = (u8)data[7];
tilt_x = (s8) data[8];
tilt_y = (s8) data[9];
/* origin */
x = x - pdata->origin[0];
y = y - pdata->origin[1];
/* change axis from wacom to lcd */
if (pdata->x_invert) {
x = pdata->max_x - x;
tilt_x = -tilt_x;
}
if (pdata->y_invert) {
y = pdata->max_y - y;
tilt_y = -tilt_y;
}
if (pdata->xy_switch) {
tmp = x;
x = y;
y = tmp;
tmp = tilt_x;
tilt_x = tilt_y;
tilt_y = tmp;