-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnt36xxx.c
3232 lines (2784 loc) · 82.7 KB
/
nt36xxx.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
/*
* Copyright (C) 2010 - 2018 Novatek, Inc.
* Copyright (C) 2021 XiaoMi, Inc.
*
* $Revision: 32206 $
* $Date: 2018-08-10 19:23:04 +0800 (週五, 10 八月 2018) $
*
* 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.
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/gpio.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
#include <linux/uaccess.h>
#include <linux/input/mt.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/debugfs.h>
#include "../spi-xiaomi-tp.h"
#include <drm/drm_notifier_mi.h>
#include <linux/init.h>
#include <linux/notifier.h>
#include <linux/fb.h>
#if defined(CONFIG_HAS_EARLYSUSPEND)
#include <linux/earlysuspend.h>
#endif
#include "nt36xxx.h"
#ifndef NVT_SAVE_TESTDATA_IN_FILE
#include "nt36xxx_mp_ctrlram.h"
#endif
#if NVT_TOUCH_ESD_PROTECT
#include <linux/jiffies.h>
#endif /* #if NVT_TOUCH_ESD_PROTECT */
#ifdef CONFIG_TOUCHSCREEN_XIAOMI_TOUCHFEATURE
#include "../xiaomi/xiaomi_touch.h"
#endif
#if NVT_TOUCH_ESD_PROTECT
static struct delayed_work nvt_esd_check_work;
static struct workqueue_struct *nvt_esd_check_wq;
static unsigned long irq_timer = 0;
uint8_t esd_check = false;
uint8_t esd_retry = 0;
#endif /* #if NVT_TOUCH_ESD_PROTECT */
#if NVT_TOUCH_EXT_PROC
extern int32_t nvt_extra_proc_init(void);
extern void nvt_extra_proc_deinit(void);
#endif
#if NVT_TOUCH_MP
extern int32_t nvt_mp_proc_init(void);
extern void nvt_mp_proc_deinit(void);
#endif
struct nvt_ts_data *ts;
#if BOOT_UPDATE_FIRMWARE
static struct workqueue_struct *nvt_fwu_wq;
static struct workqueue_struct *nvt_lockdown_wq;
extern void Boot_Update_Firmware(struct work_struct *work);
#endif
#ifdef MI_DRM_NOTIFIER
static int nvt_drm_notifier_callback(struct notifier_block *self, unsigned long event, void *data);
#else
static int nvt_fb_notifier_callback(struct notifier_block *self, unsigned long event, void *data);
#endif
#if defined(CONFIG_HAS_EARLYSUSPEND)
static void nvt_ts_early_suspend(struct early_suspend *h);
static void nvt_ts_late_resume(struct early_suspend *h);
#endif
static int32_t nvt_ts_suspend(struct device *dev);
static int32_t nvt_ts_resume(struct device *dev);
extern int dsi_panel_lockdown_info_read(unsigned char *plockdowninfo);
extern void dsi_panel_doubleclick_enable(bool on);
static int32_t nvt_check_palm(uint8_t input_id, uint8_t *data);
extern void touch_irq_boost(void);
extern void lpm_disable_for_input(bool on);
uint32_t ENG_RST_ADDR = 0x7FFF80;
uint32_t SWRST_N8_ADDR = 0; /* read from dtsi */
uint32_t SPI_RD_FAST_ADDR = 0; /* read from dtsi */
#ifdef CONFIG_MTK_SPI
const struct mt_chip_conf spi_ctrdata = {
.setuptime = 25,
.holdtime = 25,
.high_time = 5, /* 10MHz (SPI_SPEED=100M / (high_time+low_time(10ns)))*/
.low_time = 5,
.cs_idletime = 2,
.ulthgh_thrsh = 0,
.cpol = 0,
.cpha = 0,
.rx_mlsb = 1,
.tx_mlsb = 1,
.tx_endian = 0,
.rx_endian = 0,
.com_mod = DMA_TRANSFER,
.pause = 0,
.finish_intr = 1,
.deassert = 0,
.ulthigh = 0,
.tckdly = 0,
};
#endif /*endif CONFIG_MTK_SPI*/
#ifdef CONFIG_SPI_MT65XX
const struct mtk_chip_config spi_ctrdata = {
.rx_mlsb = 1,
.tx_mlsb = 1,
.cs_pol = 0,
};
#endif /*endif CONFIG_SPI_MT65XX*/
static ssize_t nvt_cg_color_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%c\n", ts->lockdown_info[2]);
}
static ssize_t nvt_cg_maker_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%c\n", ts->lockdown_info[6]);
}
static ssize_t nvt_display_maker_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%c\n", ts->lockdown_info[1]);
}
static DEVICE_ATTR(cg_color, (S_IRUGO), nvt_cg_color_show, NULL);
static DEVICE_ATTR(cg_maker, (S_IRUGO), nvt_cg_maker_show, NULL);
static DEVICE_ATTR(display_maker, (S_IRUGO), nvt_display_maker_show, NULL);
struct attribute *nvt_panel_attr[] = {
&dev_attr_cg_color.attr,
&dev_attr_cg_maker.attr,
&dev_attr_display_maker.attr,
NULL,
};
static uint8_t bTouchIsAwake = 0;
/*******************************************************
Description:
Novatek touchscreen irq enable/disable function.
return:
n.a.
*******************************************************/
static void nvt_irq_enable(bool enable)
{
struct irq_desc *desc;
if (enable) {
if (!ts->irq_enabled) {
enable_irq(ts->client->irq);
ts->irq_enabled = true;
}
} else {
if (ts->irq_enabled) {
disable_irq(ts->client->irq);
ts->irq_enabled = false;
}
}
desc = irq_to_desc(ts->client->irq);
NVT_LOG("enable=%d, desc->depth=%d\n", enable, desc->depth);
}
/*******************************************************
Description:
Novatek touchscreen spi read/write core function.
return:
Executive outcomes. 0---succeed.
*******************************************************/
static inline int32_t spi_read_write(struct spi_device *client, uint8_t *buf, size_t len , NVT_SPI_RW rw)
{
struct spi_message m;
struct spi_transfer t = {
.len = len,
};
memcpy(ts->xbuf, buf, len);
switch (rw) {
case NVTREAD:
t.tx_buf = ts->xbuf;
t.rx_buf = ts->rbuf;
t.len = (len + DUMMY_BYTES);
break;
case NVTWRITE:
t.tx_buf = ts->xbuf;
break;
}
spi_message_init(&m);
spi_message_add_tail(&t, &m);
return spi_sync(client, &m);
}
/*******************************************************
Description:
Novatek touchscreen spi read function.
return:
Executive outcomes. 2---succeed. -5---I/O error
*******************************************************/
int32_t CTP_SPI_READ(struct spi_device *client, uint8_t *buf, uint16_t len)
{
int32_t ret = -1;
int32_t retries = 0;
mutex_lock(&ts->xbuf_lock);
buf[0] = SPI_READ_MASK(buf[0]);
while (retries < 5) {
ret = spi_read_write(client, buf, len, NVTREAD);
if (ret == 0) break;
retries++;
}
if (unlikely(retries == 5)) {
NVT_ERR("read error, ret=%d\n", ret);
ret = -EIO;
} else {
memcpy((buf+1), (ts->rbuf+2), (len-1));
}
mutex_unlock(&ts->xbuf_lock);
return ret;
}
/*******************************************************
Description:
Novatek touchscreen spi write function.
return:
Executive outcomes. 1---succeed. -5---I/O error
*******************************************************/
int32_t CTP_SPI_WRITE(struct spi_device *client, uint8_t *buf, uint16_t len)
{
int32_t ret = -1;
int32_t retries = 0;
mutex_lock(&ts->xbuf_lock);
buf[0] = SPI_WRITE_MASK(buf[0]);
while (retries < 5) {
ret = spi_read_write(client, buf, len, NVTWRITE);
if (ret == 0) break;
retries++;
}
if (unlikely(retries == 5)) {
NVT_ERR("error, ret=%d\n", ret);
ret = -EIO;
}
mutex_unlock(&ts->xbuf_lock);
return ret;
}
/*******************************************************
Description:
Novatek touchscreen set index/page/addr address.
return:
Executive outcomes. 0---succeed. -5---access fail.
*******************************************************/
int32_t nvt_set_page(uint32_t addr)
{
uint8_t buf[4] = {0};
buf[0] = 0xFF; /* set index/page/addr command */
buf[1] = (addr >> 15) & 0xFF;
buf[2] = (addr >> 7) & 0xFF;
return CTP_SPI_WRITE(ts->client, buf, 3);
}
/*******************************************************
Description:
Novatek touchscreen write data to specify address.
return:
Executive outcomes. 0---succeed. -5---access fail.
*******************************************************/
int32_t nvt_write_addr(uint32_t addr, uint8_t data)
{
int32_t ret = 0;
uint8_t buf[4] = {0};
NVT_LOG("nvt_write_addr enter\n");
/* ---set xdata index--- */
buf[0] = 0xFF; /* set index/page/addr command */
buf[1] = (addr >> 15) & 0xFF;
buf[2] = (addr >> 7) & 0xFF;
ret = CTP_SPI_WRITE(ts->client, buf, 3);
if (ret) {
NVT_ERR("set page 0x%06X failed, ret = %d\n", addr, ret);
return ret;
}
/* ---write data to index--- */
buf[0] = addr & (0x7F);
buf[1] = data;
ret = CTP_SPI_WRITE(ts->client, buf, 2);
if (ret) {
NVT_ERR("write data to 0x%06X failed, ret = %d\n", addr, ret);
return ret;
}
return ret;
}
/*******************************************************
Description:
Novatek touchscreen enable hw bld crc function.
return:
N/A.
*******************************************************/
void nvt_bld_crc_enable(void)
{
uint8_t buf[2] = {0};
/* ---set xdata index to BLD_CRC_EN_ADDR--- */
nvt_set_page(ts->mmap->BLD_CRC_EN_ADDR);
/* ---read data from index--- */
buf[0] = ts->mmap->BLD_CRC_EN_ADDR & (0x7F);
buf[1] = 0xFF;
CTP_SPI_READ(ts->client, buf, 2);
/* ---write data to index--- */
buf[0] = ts->mmap->BLD_CRC_EN_ADDR & (0x7F);
buf[1] = buf[1] | (0x01 << 7);
CTP_SPI_WRITE(ts->client, buf, 2);
}
/*******************************************************
Description:
Novatek touchscreen clear status & enable fw crc function.
return:
N/A.
*******************************************************/
void nvt_fw_crc_enable(void)
{
uint8_t buf[2] = {0};
/* ---set xdata index to EVENT BUF ADDR--- */
nvt_set_page(ts->mmap->EVENT_BUF_ADDR);
/* ---clear fw reset status--- */
buf[0] = EVENT_MAP_RESET_COMPLETE & (0x7F);
buf[1] = 0x00;
CTP_SPI_WRITE(ts->client, buf, 2);
/* ---enable fw crc--- */
buf[0] = EVENT_MAP_HOST_CMD & (0x7F);
buf[1] = 0xAE; /* enable fw crc command */
CTP_SPI_WRITE(ts->client, buf, 2);
}
/*******************************************************
Description:
Novatek touchscreen set boot ready function.
return:
N/A.
*******************************************************/
void nvt_boot_ready(void)
{
/* ---write BOOT_RDY status cmds--- */
nvt_write_addr(ts->mmap->BOOT_RDY_ADDR, 1);
mdelay(5);
if (!ts->hw_crc) {
/* ---write BOOT_RDY status cmds--- */
nvt_write_addr(ts->mmap->BOOT_RDY_ADDR, 0);
/* ---write POR_CD cmds--- */
nvt_write_addr(ts->mmap->POR_CD_ADDR, 0xA0);
}
}
/*******************************************************
Description:
Novatek touchscreen eng reset cmd
function.
return:
n.a.
*******************************************************/
void nvt_eng_reset(void)
{
/* ---eng reset cmds to ENG_RST_ADDR--- */
NVT_LOG("%s \n", __func__);
nvt_write_addr(ENG_RST_ADDR, 0x5A);
NVT_LOG("%s leave\n", __func__);
mdelay(1); /* wait tMCU_Idle2TP_REX_Hi after TP_RST */
}
/*******************************************************
Description:
Novatek touchscreen reset MCU
function.
return:
n.a.
*******************************************************/
void nvt_sw_reset(void)
{
/* ---software reset cmds to SWRST_N8_ADDR--- */
nvt_write_addr(SWRST_N8_ADDR, 0x55);
msleep(10);
}
/*******************************************************
Description:
Novatek touchscreen reset MCU then into idle mode
function.
return:
n.a.
*******************************************************/
void nvt_sw_reset_idle(void)
{
/* ---MCU idle cmds to SWRST_N8_ADDR--- */
nvt_write_addr(SWRST_N8_ADDR, 0xAA);
msleep(15);
}
/*******************************************************
Description:
Novatek touchscreen reset MCU (boot) function.
return:
n.a.
*******************************************************/
void nvt_bootloader_reset(void)
{
/* ---reset cmds to SWRST_N8_ADDR--- */
nvt_write_addr(SWRST_N8_ADDR, 0x69);
mdelay(5); /* wait tBRST2FR after Bootload RST */
if (SPI_RD_FAST_ADDR) {
/* disable SPI_RD_FAST */
nvt_write_addr(SPI_RD_FAST_ADDR, 0x00);
}
}
/*******************************************************
Description:
Novatek touchscreen clear FW status function.
return:
Executive outcomes. 0---succeed. -1---fail.
*******************************************************/
int32_t nvt_clear_fw_status(void)
{
uint8_t buf[8] = {0};
int32_t i = 0;
const int32_t retry = 20;
for (i = 0; i < retry; i++) {
/* ---set xdata index to EVENT BUF ADDR--- */
nvt_set_page(ts->mmap->EVENT_BUF_ADDR | EVENT_MAP_HANDSHAKING_or_SUB_CMD_BYTE);
/* ---clear fw status--- */
buf[0] = EVENT_MAP_HANDSHAKING_or_SUB_CMD_BYTE;
buf[1] = 0x00;
CTP_SPI_WRITE(ts->client, buf, 2);
/* ---read fw status--- */
buf[0] = EVENT_MAP_HANDSHAKING_or_SUB_CMD_BYTE;
buf[1] = 0xFF;
CTP_SPI_READ(ts->client, buf, 2);
if (buf[1] == 0x00)
break;
usleep_range(10000, 10000);
}
if (i >= retry) {
NVT_ERR("failed, i=%d, buf[1]=0x%02X\n", i, buf[1]);
return -1;
} else {
return 0;
}
}
/*******************************************************
Description:
Novatek touchscreen check FW status function.
return:
Executive outcomes. 0---succeed. -1---failed.
*******************************************************/
int32_t nvt_check_fw_status(void)
{
uint8_t buf[8] = {0};
int32_t i = 0;
const int32_t retry = 50;
for (i = 0; i < retry; i++) {
/* ---set xdata index to EVENT BUF ADDR--- */
nvt_set_page(ts->mmap->EVENT_BUF_ADDR | EVENT_MAP_HANDSHAKING_or_SUB_CMD_BYTE);
/* ---read fw status--- */
buf[0] = EVENT_MAP_HANDSHAKING_or_SUB_CMD_BYTE;
buf[1] = 0x00;
CTP_SPI_READ(ts->client, buf, 2);
if ((buf[1] & 0xF0) == 0xA0)
break;
usleep_range(10000, 10000);
}
if (i >= retry) {
NVT_ERR("failed, i=%d, buf[1]=0x%02X\n", i, buf[1]);
return -1;
} else {
return 0;
}
}
/*******************************************************
Description:
Novatek touchscreen check FW reset state function.
return:
Executive outcomes. 0---succeed. -1---failed.
*******************************************************/
int32_t nvt_check_fw_reset_state(RST_COMPLETE_STATE check_reset_state)
{
uint8_t buf[8] = {0};
int32_t ret = 0;
int32_t retry = 0;
int32_t retry_max = (check_reset_state == RESET_STATE_INIT) ? 10 : 50;
/* ---set xdata index to EVENT BUF ADDR--- */
nvt_set_page(ts->mmap->EVENT_BUF_ADDR | EVENT_MAP_RESET_COMPLETE);
while (1) {
/* ---read reset state--- */
buf[0] = EVENT_MAP_RESET_COMPLETE;
buf[1] = 0x00;
CTP_SPI_READ(ts->client, buf, 6);
if ((buf[1] >= check_reset_state) && (buf[1] <= RESET_STATE_MAX)) {
ret = 0;
break;
}
retry++;
if(unlikely(retry > retry_max)) {
NVT_ERR("error, retry=%d, buf[1]=0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X\n",
retry, buf[1], buf[2], buf[3], buf[4], buf[5]);
ret = -1;
break;
}
usleep_range(10000, 10000);
}
return ret;
}
/*******************************************************
Description:
Novatek touchscreen get novatek project id information
function.
return:
Executive outcomes. 0---success. -1---fail.
*******************************************************/
int32_t nvt_read_pid(void)
{
uint8_t buf[3] = {0};
int32_t ret = 0;
/* ---set xdata index to EVENT BUF ADDR--- */
nvt_set_page(ts->mmap->EVENT_BUF_ADDR | EVENT_MAP_PROJECTID);
/* ---read project id--- */
buf[0] = EVENT_MAP_PROJECTID;
buf[1] = 0x00;
buf[2] = 0x00;
CTP_SPI_READ(ts->client, buf, 3);
ts->nvt_pid = (buf[2] << 8) + buf[1];
/* ---set xdata index to EVENT BUF ADDR--- */
nvt_set_page(ts->mmap->EVENT_BUF_ADDR);
NVT_LOG("PID=%04X\n", ts->nvt_pid);
return ret;
}
/*******************************************************
Description:
Novatek touchscreen get firmware related information
function.
return:
Executive outcomes. 0---success. -1---fail.
*******************************************************/
int32_t nvt_get_fw_info(void)
{
uint8_t buf[64] = {0};
uint32_t retry_count = 0;
int32_t ret = 0;
#if 0
/* READ CHIP ID */
/* ---set xdata index to 0x1F600-- */
nvt_set_page(0x1F600);
buf[0] = 0x4E;
buf[1] = 0x00;
buf[2] = 0x00;
buf[3] = 0x00;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x00;
CTP_SPI_READ(ts->client, buf, 7);
NVT_LOG("buf[1]=0x%02X, buf[2]=0x%02X, buf[3]=0x%02X, buf[4]=0x%02X, buf[5]=0x%02X, buf[6]=0x%02X\n",
buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
memset(buf, 0, 64);
#endif
info_retry:
/* ---set xdata index to EVENT BUF ADDR--- */
nvt_set_page(ts->mmap->EVENT_BUF_ADDR | EVENT_MAP_FWINFO);
/* ---read fw info--- */
buf[0] = EVENT_MAP_FWINFO;
CTP_SPI_READ(ts->client, buf, 17);
ts->fw_ver = buf[1];
ts->x_num = buf[3];
ts->y_num = buf[4];
ts->abs_x_max = (uint16_t)((buf[5] << 8) | buf[6]);
ts->abs_y_max = (uint16_t)((buf[7] << 8) | buf[8]);
ts->max_button_num = buf[11];
/* ---clear x_num, y_num if fw info is broken--- */
if ((buf[1] + buf[2]) != 0xFF) {
NVT_ERR("FW info is broken! fw_ver=0x%02X, ~fw_ver=0x%02X\n", buf[1], buf[2]);
ts->fw_ver = 0;
ts->x_num = 18;
ts->y_num = 32;
ts->abs_x_max = TOUCH_DEFAULT_MAX_WIDTH;
ts->abs_y_max = TOUCH_DEFAULT_MAX_HEIGHT;
ts->max_button_num = TOUCH_KEY_NUM;
if(retry_count < 3) {
retry_count++;
NVT_ERR("retry_count=%d\n", retry_count);
goto info_retry;
} else {
NVT_ERR("Set default fw_ver=%d, x_num=%d, y_num=%d, "
"abs_x_max=%d, abs_y_max=%d, max_button_num=%d!\n",
ts->fw_ver, ts->x_num, ts->y_num,
ts->abs_x_max, ts->abs_y_max, ts->max_button_num);
ret = -1;
}
} else {
ret = 0;
}
NVT_LOG("FW type is 0x%02X, fw_ver=%d\n", buf[14], ts->fw_ver);
/* ---Get Novatek PID--- */
nvt_read_pid();
return ret;
}
/*******************************************************
Create Device Node (Proc Entry)
*******************************************************/
#if NVT_TOUCH_PROC
static struct proc_dir_entry *NVT_proc_entry;
#define DEVICE_NAME "NVTSPI"
/*******************************************************
Description:
Novatek touchscreen /proc/NVTSPI read function.
return:
Executive outcomes. 2---succeed. -5,-14---failed.
*******************************************************/
static ssize_t nvt_flash_read(struct file *file, char __user *buff, size_t count, loff_t *offp)
{
uint8_t *str = NULL;
int32_t ret = 0;
int32_t retries = 0;
int8_t spi_wr = 0;
uint8_t *buf;
if (count > NVT_TRANSFER_LEN) {
NVT_ERR("invalid transfer len!\n");
return -EFAULT;
}
/* allocate buffer for spi transfer */
str = (uint8_t *)kzalloc((count), GFP_KERNEL);
if(str == NULL) {
NVT_ERR("kzalloc for buf failed!\n");
ret = -ENOMEM;
goto kzalloc_failed;
}
buf = (uint8_t *)kzalloc((count), GFP_KERNEL | GFP_DMA);
if(buf == NULL) {
NVT_ERR("kzalloc for buf failed!\n");
ret = -ENOMEM;
kfree(str);
str = NULL;
goto kzalloc_failed;
}
if (copy_from_user(str, buff, count)) {
NVT_ERR("copy from user error\n");
ret = -EFAULT;
goto out;
}
#if NVT_TOUCH_ESD_PROTECT
/*
* stop esd check work to avoid case that 0x77 report righ after here to enable esd check again
* finally lead to trigger esd recovery bootloader reset
*/
cancel_delayed_work_sync(&nvt_esd_check_work);
nvt_esd_check_enable(false);
#endif /* #if NVT_TOUCH_ESD_PROTECT */
spi_wr = str[0] >> 7;
memcpy(buf, str+2, ((str[0] & 0x7F) << 8) | str[1]);
if (spi_wr == NVTWRITE) { /* SPI write */
while (retries < 20) {
ret = CTP_SPI_WRITE(ts->client, buf, ((str[0] & 0x7F) << 8) | str[1]);
if (!ret)
break;
else
NVT_ERR("error, retries=%d, ret=%d\n", retries, ret);
retries++;
}
if (unlikely(retries == 20)) {
NVT_ERR("error, ret = %d\n", ret);
ret = -EIO;
goto out;
}
} else if (spi_wr == NVTREAD) { /* SPI read */
while (retries < 20) {
ret = CTP_SPI_READ(ts->client, buf, ((str[0] & 0x7F) << 8) | str[1]);
if (!ret)
break;
else
NVT_ERR("error, retries=%d, ret=%d\n", retries, ret);
retries++;
}
memcpy(str+2, buf, ((str[0] & 0x7F) << 8) | str[1]);
/* copy buff to user if spi transfer */
if (retries < 20) {
if (copy_to_user(buff, str, count)) {
ret = -EFAULT;
goto out;
}
}
if (unlikely(retries == 20)) {
NVT_ERR("error, ret = %d\n", ret);
ret = -EIO;
goto out;
}
} else {
NVT_ERR("Call error, str[0]=%d\n", str[0]);
ret = -EFAULT;
goto out;
}
out:
kfree(str);
kfree(buf);
kzalloc_failed:
return ret;
}
/*******************************************************
Description:
Novatek touchscreen /proc/NVTSPI open function.
return:
Executive outcomes. 0---succeed. -12---failed.
*******************************************************/
static int32_t nvt_flash_open(struct inode *inode, struct file *file)
{
struct nvt_flash_data *dev;
dev = kmalloc(sizeof(struct nvt_flash_data), GFP_KERNEL);
if (dev == NULL) {
NVT_ERR("Failed to allocate memory for nvt flash data\n");
return -ENOMEM;
}
rwlock_init(&dev->lock);
file->private_data = dev;
return 0;
}
/*******************************************************
Description:
Novatek touchscreen /proc/NVTSPI close function.
return:
Executive outcomes. 0---succeed.
*******************************************************/
static int32_t nvt_flash_close(struct inode *inode, struct file *file)
{
struct nvt_flash_data *dev = file->private_data;
if (dev)
kfree(dev);
return 0;
}
static const struct file_operations nvt_flash_fops = {
.owner = THIS_MODULE,
.open = nvt_flash_open,
.release = nvt_flash_close,
.read = nvt_flash_read,
};
/*******************************************************
Description:
Novatek touchscreen /proc/NVTSPI initial function.
return:
Executive outcomes. 0---succeed. -12---failed.
*******************************************************/
static int32_t nvt_flash_proc_init(void)
{
NVT_proc_entry = proc_create(DEVICE_NAME, 0444, NULL,&nvt_flash_fops);
if (NVT_proc_entry == NULL) {
NVT_ERR("Failed!\n");
return -ENOMEM;
} else {
NVT_LOG("Succeeded!\n");
}
NVT_LOG("============================================================\n");
NVT_LOG("Create /proc/%s\n", DEVICE_NAME);
NVT_LOG("============================================================\n");
return 0;
}
/*******************************************************
Description:
Novatek touchscreen /proc/NVTSPI deinitial function.
return:
n.a.
*******************************************************/
static void nvt_flash_proc_deinit(void)
{
if (NVT_proc_entry != NULL) {
remove_proc_entry(DEVICE_NAME, NULL);
NVT_proc_entry = NULL;
NVT_LOG("Removed /proc/%s\n", DEVICE_NAME);
}
}
#endif
#if WAKEUP_GESTURE
#define GESTURE_WORD_C 12
#define GESTURE_WORD_W 13
#define GESTURE_WORD_V 14
#define GESTURE_DOUBLE_CLICK 15
#define GESTURE_WORD_Z 16
#define GESTURE_WORD_M 17
#define GESTURE_WORD_O 18
#define GESTURE_WORD_e 19
#define GESTURE_WORD_S 20
#define GESTURE_SLIDE_UP 21
#define GESTURE_SLIDE_DOWN 22
#define GESTURE_SLIDE_LEFT 23
#define GESTURE_SLIDE_RIGHT 24
/* customized gesture id */
#define DATA_PROTOCOL 30
/* function page definition */
#define FUNCPAGE_GESTURE 1
/*******************************************************
Description:
Novatek touchscreen wake up gesture key report function.
return:
n.a.
*******************************************************/
void nvt_ts_wakeup_gesture_report(uint8_t gesture_id, uint8_t *data)
{
uint8_t func_type = data[2];
uint8_t func_id = data[3];
/* support fw specifal data protocol */
if ((gesture_id == DATA_PROTOCOL) && (func_type == FUNCPAGE_GESTURE)) {
gesture_id = func_id;
} else if (gesture_id > DATA_PROTOCOL) {
NVT_ERR("gesture_id %d is invalid, func_type=%d, func_id=%d\n", gesture_id, func_type, func_id);
return;
}
NVT_LOG("gesture_id = %d\n", gesture_id);
switch (gesture_id) {
case GESTURE_DOUBLE_CLICK:
NVT_LOG("Gesture : Double Click.\n");
input_report_key(ts->input_dev, KEY_WAKEUP, 1);
input_sync(ts->input_dev);
input_report_key(ts->input_dev, KEY_WAKEUP, 0);
input_sync(ts->input_dev);
break;
default:
break;
}
}
#endif
/*******************************************************
Description:
Novatek touchscreen parse device tree function.
return:
n.a.
*******************************************************/
#ifdef CONFIG_OF
static int32_t nvt_parse_dt(struct device *dev)
{
struct nvt_config_info *config_info;
struct device_node *temp, *np = dev->of_node;
int32_t ret = 0;
uint32_t temp_val;
#if NVT_TOUCH_SUPPORT_HW_RST
ts->reset_gpio = of_get_named_gpio_flags(np, "novatek,reset-gpio", 0, &ts->reset_flags);
NVT_LOG("novatek,reset-gpio=%d\n", ts->reset_gpio);
#endif
ts->irq_gpio = of_get_named_gpio_flags(np, "novatek,irq-gpio", 0, &ts->irq_flags);
NVT_LOG("novatek,irq-gpio=%d\n", ts->irq_gpio);
ret = of_property_read_u32(np, "novatek,swrst-n8-addr", &SWRST_N8_ADDR);
if (ret) {
NVT_ERR("error reading novatek,swrst-n8-addr. ret=%d\n", ret);
return ret;
} else {
NVT_LOG("SWRST_N8_ADDR=0x%06X\n", SWRST_N8_ADDR);
}
ret = of_property_read_u32(np, "novatek,spi-rd-fast-addr", &SPI_RD_FAST_ADDR);
if (ret) {
NVT_ERR("not support novatek,spi-rd-fast-addr\n");