1 /*
2  *
3  * FocalTech TouchScreen driver.
4  *
5  * Copyright (c) 2012-2018, FocalTech Systems, Ltd., all rights reserved.
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17 
18 /*******************************************************************************
19 * Included header files
20 *******************************************************************************/
21 #include "../focaltech_test.h"
22 
23 /*******************************************************************************
24 * Private constant and macro definitions using #define
25 *******************************************************************************/
26 #define REG_MS_SELECT           0x26
27 #define REG_CH_X_MASTER         0x50
28 #define REG_CH_Y_MASTER         0x51
29 #define REG_CH_X_SLAVE          0x52
30 #define REG_CH_Y_SLAVE          0x53
31 #define REG_FW_INFO_CNT         0x17
32 #define I2C_ADDR_M              0
33 #define I2C_ADDR_S              12
34 #define REG_FW_INFO_ADDR        0x81
35 #define REG_FW_INFO_LEN         32
36 #define MAX_ADC_VALUE                   4015
37 #define FACTORY_NOISE_MODE_REG          0x5E
38 
39 /*******************************************************************************
40 * Private enumerations, structures and unions using typedef
41 *******************************************************************************/
42 enum M_S_TYPE {
43     CHIP_AS_SLAVE = 0,
44     CHIP_AS_MASTER = 1,
45     SINGLE_CHIP = 3,
46 };
47 
48 enum CASCADE_DIRECTION {
49     CASCADE_LEFT_RIGHT = 0,
50     CASCADE_UP_DOWN    = 1,
51 };
52 
53 /*
54  * m_s_sel    - master/slave information
55  * m_i2c_addr - master ic I2C address
56  * s_i2c_addr - slave ic I2C address
57  * m_tx       - master IC tx number
58  * m_rx       - master IC rx number
59  * s_tx       - slave IC tx number
60  * s_rx       - slave IC rx number
61  */
62 struct ft8201_info {
63     union m_s_sel {
64         struct bits {
65             u8 type         : 6;
66             u8 direction    : 1;
67             u8 s0_as_slave  : 1;
68         } bits;
69         u8 byte_val;
70     } m_s_sel;
71     u8 m_i2c_addr;
72     u8 s_i2c_addr;
73     u8 m_tx;
74     u8 m_rx;
75     u8 s_tx;
76     u8 s_rx;
77     u8  current_slave_addr;
78 };
79 
80 /*******************************************************************************
81 * Static function prototypes
82 *******************************************************************************/
fts_array_copy(int * dest,const int * src,int len)83 static void fts_array_copy(int *dest, const int *src, int len)
84 {
85     int i = 0;
86 
87     for (i = 0; i < len; i++) {
88         dest[i] = src[i];
89     }
90 }
91 
work_as_master(struct ft8201_info * info)92 static void work_as_master(struct ft8201_info *info)
93 {
94     if (fts_data->client->addr != info->m_i2c_addr) {
95         FTS_TEST_DBG("change i2c addr to master(0x%x)\n", info->m_i2c_addr);
96         fts_data->client->addr = info->m_i2c_addr;
97     }
98 }
99 
work_as_slave(struct ft8201_info * info)100 static void work_as_slave(struct ft8201_info *info)
101 {
102     if (fts_data->client->addr != info->s_i2c_addr) {
103         FTS_TEST_DBG("change i2c addr to slave(0x%x)\n", info->s_i2c_addr);
104         fts_data->client->addr = info->s_i2c_addr;
105     }
106 }
107 
ft8201_write_reg(struct ft8201_info * info,u8 reg_addr,u8 reg_val)108 static int ft8201_write_reg(struct ft8201_info *info, u8 reg_addr, u8 reg_val)
109 {
110     int ret = 0;
111 
112     /* write master reg */
113     work_as_master(info);
114     ret = fts_test_write_reg(reg_addr, reg_val);
115     if (ret) {
116         FTS_TEST_SAVE_ERR("write master reg fail\n");
117         return ret;
118     }
119 
120     /* write slave reg */
121     work_as_slave(info);
122     ret = fts_test_write_reg(reg_addr, reg_val);
123     if (ret) {
124         FTS_TEST_SAVE_ERR("write slave reg fail\n");
125         work_as_master(info);
126         return ret;
127     }
128     work_as_master(info);
129 
130     return 0;
131 }
132 
integrate_data(struct ft8201_info * info,int * m_buf,int * s_buf,int * data)133 static void integrate_data(struct ft8201_info *info, int *m_buf, int *s_buf, int *data)
134 {
135     int i = 0;
136     int *s0_buf;
137     int *s1_buf;
138     int s0_ch = 0;
139     int s0_tx = 0;
140     int s0_rx = 0;
141     int s1_ch = 0;
142     int s1_rx = 0;
143     int row = 0;
144     int s0_row = 0;
145     int s1_row = 0;
146 
147     FTS_TEST_FUNC_ENTER();
148 
149     if (false == info->m_s_sel.bits.s0_as_slave) {
150         s0_buf = m_buf;
151         s0_tx = info->m_tx;
152         s0_rx = info->m_rx;
153         s0_ch = info->m_tx * info->m_rx;
154         s1_buf = s_buf;
155         s1_rx = info->s_rx;
156         s1_ch = info->s_tx * info->s_rx;
157     } else {
158         s0_buf = s_buf;
159         s0_tx = info->s_tx;
160         s0_rx = info->s_rx;
161         s0_ch = info->s_tx * info->s_rx;
162         s1_buf = m_buf;
163         s1_rx = info->m_rx;
164         s1_ch = info->m_tx * info->m_rx;
165     }
166 
167     FTS_TEST_DBG("%d %d %d %d %d", s0_tx, s0_rx, s0_ch, s1_rx, s1_ch);
168     if (CASCADE_LEFT_RIGHT == info->m_s_sel.bits.direction) {
169         /* cascade direction : left to right */
170         for (i = 0; i < s0_tx; i++) {
171             row = i * (s0_rx + s1_rx);
172             s0_row = i * s0_rx;
173             s1_row = i * s1_rx;
174 
175             fts_array_copy(data + row, s0_buf + s0_row, s0_rx);
176             fts_array_copy(data + row + s0_rx, s1_buf + s1_row, s1_rx);
177         }
178 
179     } else {
180         /* cascade direction : up to down */
181         fts_array_copy(data, s0_buf, s0_ch);
182         fts_array_copy(data + s0_ch, s1_buf, s1_ch);
183     }
184 
185     /* key */
186     fts_array_copy(data + s0_ch + s1_ch, s0_buf + s0_ch, 6);
187     fts_array_copy(data + s0_ch + s1_ch + 6, s1_buf + s1_ch, 6);
188 
189     FTS_TEST_FUNC_EXIT();
190 }
191 
check_ic_info_validity(struct ft8201_info * info)192 static int check_ic_info_validity(struct ft8201_info *info)
193 {
194     /* IC type */
195     if ((info->m_s_sel.bits.type != CHIP_AS_SLAVE)
196         && (info->m_s_sel.bits.type != CHIP_AS_MASTER)) {
197         FTS_TEST_SAVE_ERR("IC cascade type(%d) fail\n", info->m_s_sel.bits.type);
198         return -EINVAL;
199     }
200 
201     /* I2C addr */
202     if ((0 == info->m_i2c_addr) || (0 == info->s_i2c_addr)) {
203         FTS_TEST_SAVE_ERR("i2c addr of master(0x%x)/slave(0x%x) fail\n",
204                           info->m_i2c_addr, info->s_i2c_addr);
205         return -EINVAL;
206     }
207 
208     /* tx/rx */
209     if ((0 == info->m_tx) || (info->m_tx > TX_NUM_MAX)) {
210         FTS_TEST_SAVE_ERR("master tx(%d) fail\n", info->m_tx);
211         return -EINVAL;
212     }
213 
214     if ((0 == info->m_rx) || (info->m_rx > TX_NUM_MAX)) {
215         FTS_TEST_SAVE_ERR("master rx(%d) fail\n", info->m_rx);
216         return -EINVAL;
217     }
218 
219     if ((0 == info->s_tx) || (info->s_tx > TX_NUM_MAX)) {
220         FTS_TEST_SAVE_ERR("slave tx(%d) fail\n", info->s_tx);
221         return -EINVAL;
222     }
223 
224     if ((0 == info->s_rx) || (info->s_rx > TX_NUM_MAX)) {
225         FTS_TEST_SAVE_ERR("slave rx(%d) fail\n", info->s_rx);
226         return -EINVAL;
227     }
228 
229     return 0;
230 }
231 
get_chip_information(struct ft8201_info * info)232 static int get_chip_information(struct ft8201_info *info)
233 {
234     int ret = 0;
235     u8 value[REG_FW_INFO_LEN] = { 0 };
236     u8 cmd = 0;
237 
238     ret = fts_test_read_reg(REG_MS_SELECT, &value[0]);
239     if (ret) {
240         FTS_TEST_SAVE_ERR("read m/s select info fail\n");
241         return ret;
242     }
243     info->m_s_sel.byte_val = value[0];
244 
245     ret = fts_test_read_reg(REG_CH_X_MASTER, &value[0]);
246     if (ret) {
247         FTS_TEST_SAVE_ERR("read ch_x_m fail\n");
248         return ret;
249     }
250     info->m_tx = value[0];
251 
252     ret = fts_test_read_reg(REG_CH_Y_MASTER, &value[0]);
253     if (ret) {
254         FTS_TEST_SAVE_ERR("read ch_y_m fail\n");
255         return ret;
256     }
257     info->m_rx = value[0];
258 
259     ret = fts_test_read_reg(REG_CH_X_SLAVE, &value[0]);
260     if (ret) {
261         FTS_TEST_SAVE_ERR("read ch_x_s fail\n");
262         return ret;
263     }
264     info->s_tx = value[0];
265 
266     ret = fts_test_read_reg(REG_CH_Y_SLAVE, &value[0]);
267     if (ret) {
268         FTS_TEST_SAVE_ERR("read ch_y_s fail\n");
269         return ret;
270     }
271     info->s_rx = value[0];
272 
273     ret = fts_test_write_reg(REG_FW_INFO_CNT, 0);
274     if (ret) {
275         FTS_TEST_SAVE_ERR("write fw into cnt fail\n");
276         return ret;
277     }
278     cmd = REG_FW_INFO_ADDR;
279     ret = fts_test_read(cmd, &value[0], REG_FW_INFO_LEN);
280     if (ret) {
281         FTS_TEST_SAVE_ERR("read fw info fail\n");
282         return ret;
283     }
284 
285     if ((value[I2C_ADDR_M] + value[I2C_ADDR_M + 1]) == 0xFF) {
286         info->m_i2c_addr = value[I2C_ADDR_M] >> 1;
287     }
288 
289     if ((value[I2C_ADDR_S] + value[I2C_ADDR_S + 1]) == 0xFF) {
290         info->s_i2c_addr = value[I2C_ADDR_S] >> 1;
291     }
292 
293     FTS_TEST_DBG("%s=%d,%s=%d,%s=%d,%s=0x%x,%s=0x%x,%s=%d,%s=%d,%s=%d,%s=%d\n",
294                  "type", info->m_s_sel.bits.type,
295                  "direction", info->m_s_sel.bits.direction,
296                  "s0_as_slave", info->m_s_sel.bits.s0_as_slave,
297                  "m_i2c_addr", info->m_i2c_addr,
298                  "s_i2c_addr", info->s_i2c_addr,
299                  "m_tx", info->m_tx,
300                  "m_rx", info->m_rx,
301                  "s_tx", info->s_tx,
302                  "s_rx", info->s_rx
303                 );
304 
305     ret = check_ic_info_validity(info);
306     if (ret) {
307         FTS_TEST_SAVE_ERR("ic information invalid\n");
308         return ret;
309     }
310 
311     return 0;
312 }
313 
ft8201_test_init(struct ft8201_info * info)314 static int ft8201_test_init(struct ft8201_info *info)
315 {
316     int ret = 0;
317 
318     /* initialize info */
319     memset(info, 0, sizeof(struct ft8201_info));
320 
321     /* enter factory mode */
322     ret = enter_factory_mode();
323     if (ret < 0) {
324         FTS_TEST_SAVE_ERR("enter factory mode fail, can't get tx/rx num\n");
325         return ret;
326     }
327 
328     /* get chip info */
329     ret = get_chip_information(info);
330     if (ret < 0) {
331         FTS_TEST_SAVE_ERR("get chip information fail\n");
332         return ret;
333     }
334 
335     return 0;
336 }
337 
ft8201_chip_clb(struct ft8201_info * info)338 static u8 ft8201_chip_clb(struct ft8201_info *info)
339 {
340     int ret = 0;
341 
342     FTS_TEST_FUNC_ENTER();
343     /* master clb */
344     work_as_master(info);
345     ret = chip_clb();
346     if (ret) {
347         FTS_TEST_SAVE_ERR("master clb fail\n");
348         return ret;
349     }
350 
351     /* slave clb */
352     work_as_slave(info);
353     ret = chip_clb();
354     if (ret) {
355         FTS_TEST_SAVE_ERR("master clb fail\n");
356         work_as_master(info);
357         return ret;
358     }
359     work_as_master(info);
360 
361     FTS_TEST_FUNC_EXIT();
362     return 0;
363 }
364 
ft8201_get_tx_rx_cb(struct ft8201_info * info,u8 start_node,int read_num,int * read_buffer)365 static int ft8201_get_tx_rx_cb(struct ft8201_info *info, u8 start_node, int read_num, int *read_buffer)
366 {
367     int ret = 0;
368     int *buffer_master = NULL;
369     int *buffer_slave = NULL;
370     int master_tx = info->m_tx;
371     int master_rx = info->m_rx;
372     int slave_tx = info->s_tx;
373     int slave_rx = info->s_rx;
374 
375     FTS_TEST_FUNC_ENTER();
376 
377     buffer_master = fts_malloc((master_tx + 1) * master_rx * sizeof(int));
378     if (NULL == buffer_master) {
379         FTS_TEST_SAVE_ERR("%s:master buf malloc fail\n", __func__);
380         ret = -ENOMEM;
381         goto GET_CB_ERR;
382     }
383 
384     buffer_slave = fts_malloc((slave_tx + 1) * slave_rx * sizeof(int));
385     if (NULL == buffer_slave) {
386         FTS_TEST_SAVE_ERR("%s:slave buf malloc fail\n", __func__);
387         ret = -ENOMEM;
388         goto GET_CB_ERR;
389     }
390 
391     /* master cb */
392     work_as_master(info);
393     ret = get_cb_incell(0, master_tx * master_rx  + 6, buffer_master);
394     if (ret ) {
395         FTS_TEST_SAVE_ERR("master clb fail\n");
396         goto GET_CB_ERR;
397     }
398 
399     /* slave cb */
400     work_as_slave(info);
401     ret = get_cb_incell(0, slave_tx * slave_rx + 6, buffer_slave);
402     if (ret ) {
403         FTS_TEST_SAVE_ERR("slave clb fail\n");
404         work_as_master(info);
405         goto GET_CB_ERR;
406     }
407     work_as_master(info);
408 
409     integrate_data(info, buffer_master, buffer_slave, read_buffer);
410 
411 GET_CB_ERR:
412     fts_free(buffer_master);
413     fts_free(buffer_slave);
414 
415     FTS_TEST_FUNC_EXIT();
416     return ret;
417 }
418 
read_adc_data(u8 retval,int byte_num,int * adc_buf)419 static int read_adc_data(u8 retval, int byte_num, int *adc_buf)
420 {
421     int ret = 0;
422     int times = 0;
423     u8 short_state = 0;
424 
425     FTS_TEST_FUNC_ENTER();
426 
427     for (times = 0; times < FACTORY_TEST_RETRY; times++) {
428         ret = fts_test_read_reg(FACTORY_REG_SHORT_TEST_STATE, &short_state);
429         if ((0 == ret) && (retval == short_state))
430             break;
431         else
432             FTS_TEST_DBG("reg%x=%x,retry:%d",
433                          FACTORY_REG_SHORT_TEST_STATE, short_state, times);
434 
435         sys_delay(FACTORY_TEST_RETRY_DELAY);
436     }
437     if (times >= FACTORY_TEST_RETRY) {
438         FTS_TEST_SAVE_ERR("short test timeout, ADC data not OK\n");
439         ret = -EIO;
440         goto ADC_ERROR;
441     }
442 
443     ret = read_mass_data(FACTORY_REG_SHORT_ADDR, byte_num, adc_buf);
444     if (ret) {
445         FTS_TEST_SAVE_ERR("get short(adc) data fail\n");
446     }
447 
448 ADC_ERROR:
449     FTS_TEST_FUNC_EXIT();
450     return ret;
451 }
452 
ft8201_weakshort_get_adcdata(struct ft8201_info * info,int * rbuf)453 static u8 ft8201_weakshort_get_adcdata(struct ft8201_info *info, int *rbuf)
454 {
455     int ret = 0;
456     int master_adc_num = 0;
457     int slave_adc_num = 0;
458     int *buffer_master = NULL;
459     int *buffer_slave = NULL;
460     int master_tx = info->m_tx;
461     int master_rx = info->m_rx;
462     int slave_tx = info->s_tx;
463     int slave_rx = info->s_rx;
464     int ch_num = 0;
465 
466     FTS_TEST_FUNC_ENTER();
467 
468     buffer_master = fts_malloc((master_tx + 1) * master_rx * sizeof(int));
469     if (NULL == buffer_master) {
470         FTS_TEST_SAVE_ERR("%s:master buf malloc fail\n", __func__);
471         ret = -ENOMEM;
472         goto ADC_ERROR;
473     }
474 
475     buffer_slave = fts_malloc((slave_tx + 1) * slave_rx * sizeof(int));
476     if (NULL == buffer_slave) {
477         FTS_TEST_SAVE_ERR("%s:slave buf malloc fail\n", __func__);
478         ret = -ENOMEM;
479         goto ADC_ERROR;
480     }
481 
482     /* Start ADC sample */
483     ch_num = master_tx + master_rx;
484     ret = fts_test_write_reg(FACTORY_REG_SHORT_TEST_EN, 0x01);
485     if (ret) {
486         FTS_TEST_SAVE_ERR("start short test fail\n");
487         goto ADC_ERROR;
488     }
489     sys_delay(ch_num * FACTORY_TEST_DELAY);
490 
491     /* read master adc data */
492     master_adc_num = (master_tx * master_rx + 6) * 2;
493     work_as_master(info);
494     ret = read_adc_data(TEST_RETVAL_00, master_adc_num, buffer_master);
495     if (ret) {
496         FTS_TEST_SAVE_ERR("read master adc data fail\n");
497         goto ADC_ERROR;
498     }
499 
500     /* read slave adc data */
501     slave_adc_num = (slave_tx * slave_rx + 6) * 2;
502     work_as_slave(info);
503     ret = read_adc_data(TEST_RETVAL_00, slave_adc_num, buffer_slave);
504     if (ret) {
505         FTS_TEST_SAVE_ERR("read master adc data fail\n");
506         work_as_master(info);
507         goto ADC_ERROR;
508     }
509     work_as_master(info);
510 
511     /* data integration */
512     integrate_data(info, buffer_master, buffer_slave, rbuf);
513 
514 ADC_ERROR:
515     fts_free(buffer_master);
516     fts_free(buffer_slave);
517 
518     FTS_TEST_FUNC_EXIT();
519     return ret;
520 }
521 
ft8201_short_test(struct ft8201_info * info,struct fts_test * tdata,bool * test_result)522 static int ft8201_short_test(struct ft8201_info *info, struct fts_test *tdata, bool *test_result)
523 {
524     int ret = 0;
525     bool tmp_result = true;
526     int *adcdata = NULL;
527     int tmp_adc = 0;
528     int i = 0;
529     struct incell_threshold *thr = &tdata->ic.incell.thr;
530 
531     FTS_TEST_FUNC_ENTER();
532     FTS_TEST_SAVE_INFO("\n============ Test Item: short test\n");
533     memset(tdata->buffer, 0, tdata->buffer_length);
534     adcdata = tdata->buffer;
535 
536     ret = enter_factory_mode();
537     if (ret) {
538         FTS_TEST_SAVE_ERR("//Failed to Enter factory mode.ret=%d\n", ret);
539         goto test_err;
540     }
541 
542     ret = ft8201_weakshort_get_adcdata(info, adcdata);
543     if (ret) {
544         FTS_TEST_SAVE_ERR("//Failed to get AdcData. ret=%d\n", ret);
545         goto test_err;
546     }
547 
548     /* change adc to resistance */
549     for (i = 0; i < tdata->node.node_num; ++i) {
550         tmp_adc = adcdata[i];
551         /* avoid calculating the value of the resistance is too large, limiting the size of the ADC value */
552         if (tmp_adc > MAX_ADC_VALUE)
553             tmp_adc = MAX_ADC_VALUE;
554         adcdata[i] = (tmp_adc * 100) / (4095 - tmp_adc);
555     }
556 
557     /* save */
558     show_data(adcdata, true);
559     save_data_csv(adcdata, "Short Circuit Test", \
560                   CODE_SHORT_TEST, false, true);
561 
562     /* compare */
563     tmp_result = compare_data(adcdata, thr->basic.short_res_min, TEST_SHORT_RES_MAX, thr->basic.short_res_vk_min, TEST_SHORT_RES_MAX, true);
564 
565     ret = 0;
566 test_err:
567     if (tmp_result) {
568         *test_result = true;
569         FTS_TEST_SAVE_INFO("\n------ Short Circuit Test PASS\n");
570     } else {
571         *test_result = false;
572         FTS_TEST_SAVE_INFO("\n------ Short Circuit Test NG\n");
573     }
574     FTS_TEST_FUNC_EXIT();
575     return ret;
576 }
577 
ft8201_open_test(struct ft8201_info * info,struct fts_test * tdata,bool * test_result)578 static int ft8201_open_test(struct ft8201_info *info, struct fts_test *tdata, bool *test_result)
579 {
580     int ret = 0;
581     bool tmp_result = false;
582     u8 reg20_val = 0;
583     u8 reg86_val = 0;
584     u8 tmp_val = 0;
585     int min = 0;
586     int max = 0;
587     int *opendata = NULL;
588     int byte_num = 0;
589     struct incell_threshold *thr = &tdata->ic.incell.thr;
590 
591 
592     FTS_TEST_FUNC_ENTER();
593     FTS_TEST_SAVE_INFO("\n============ Test Item: Open Test\n");
594     memset(tdata->buffer, 0, tdata->buffer_length);
595     opendata = tdata->buffer;
596 
597     ret = enter_factory_mode();
598     if (ret) {
599         FTS_TEST_SAVE_ERR("Enter Factory Failed\n");
600         goto test_err;
601     }
602 
603     ret = fts_test_read_reg(FACTORY_REG_OPEN_REG86, &reg86_val);
604     if (ret < 0) {
605         FTS_TEST_SAVE_ERR("read 0x86 fail\n");
606         goto test_err;
607     }
608 
609     ret = fts_test_read_reg(FACTORY_REG_OPEN_REG20, &reg20_val);
610     if (ret < 0) {
611         FTS_TEST_SAVE_ERR("read 0x20 fail\n");
612         goto test_err;
613     }
614 
615 
616     /* set open mode */
617     ret = ft8201_write_reg(info, FACTORY_REG_OPEN_REG86, 0x01);
618     if (ret < 0) {
619         FTS_TEST_SAVE_ERR("write 0x86 fail\n");
620         goto restore_reg;
621     }
622 
623     /* set Bit4~Bit5 of reg0x20 is set to 2b'10 (Source to GND) */
624     tmp_val = reg20_val | (1 << 5);
625     tmp_val &= ~(1 << 4);
626     ret = ft8201_write_reg(info, FACTORY_REG_OPEN_REG20, tmp_val);
627     if (ret) {
628         FTS_TEST_SAVE_ERR("Failed to Read or Write Reg\n");
629         goto restore_reg;
630     }
631 
632     /* wait fw state update before clb */
633     ret = wait_state_update(TEST_RETVAL_00);
634     if (ret < 0) {
635         FTS_TEST_SAVE_ERR("wait state update fail\n");
636         goto restore_reg;
637     }
638 
639     ret = ft8201_chip_clb(info);
640     if (ret) {
641         FTS_TEST_SAVE_ERR("auto clb fail\n");
642         goto restore_reg;
643     }
644 
645     /* get cb data */
646     byte_num = tdata->node.tx_num * tdata->node.rx_num;
647     ret = ft8201_get_tx_rx_cb(info, 0, byte_num, opendata);
648     if (ret < 0) {
649         FTS_TEST_SAVE_ERR("get cb fail\n");
650         goto restore_reg;
651     }
652 
653     /* show open data */
654     show_data(opendata, false);
655     /* save data */
656     save_data_csv(opendata, "Open Test", \
657                   CODE_OPEN_TEST, false, false);
658 
659     /* compare */
660     min = thr->basic.open_cb_min;
661     max = 256;
662     FTS_TEST_DBG("open %d %d\n", min, opendata[0]);
663     tmp_result = compare_data(opendata, min, max, 0, 0, false);
664 
665     //ret = 0;
666 
667 restore_reg:
668     /* restore */
669     ret = ft8201_write_reg(info, FACTORY_REG_OPEN_REG86, reg86_val);
670     if (ret < 0) {
671         FTS_TEST_SAVE_ERR("restore reg86 fail\n");
672     }
673 
674     ret = ft8201_write_reg(info, FACTORY_REG_OPEN_REG20, reg20_val);
675     if (ret < 0) {
676         FTS_TEST_SAVE_ERR("restore reg20 fail\n");
677     }
678 
679     ret = wait_state_update(TEST_RETVAL_00);
680     if (ret < 0) {
681         FTS_TEST_SAVE_ERR("wait state update fail\n");
682     }
683 
684     ret = ft8201_chip_clb(info);
685     if (ret < 0) {
686         FTS_TEST_SAVE_ERR("auto clb fail\n");
687     }
688 
689 test_err:
690     if (tmp_result) {
691         *test_result = true;
692         FTS_TEST_SAVE_INFO("\n------ Open Test PASS\n");
693     } else {
694         *test_result = false;
695         FTS_TEST_SAVE_INFO("\n------ Open Test NG\n");
696     }
697     FTS_TEST_FUNC_EXIT();
698     return ret;
699 }
700 
701 
ft8201_cb_test(struct ft8201_info * info,struct fts_test * tdata,bool * test_result)702 static int ft8201_cb_test(struct ft8201_info *info, struct fts_test *tdata, bool *test_result)
703 {
704     bool tmp_result = false;
705     int ret = 0;
706     bool key_check = false;
707     int byte_num = 0;
708     int *cbdata = NULL;
709     struct incell_threshold *thr = &tdata->ic.incell.thr;
710 
711     FTS_TEST_FUNC_ENTER();
712     FTS_TEST_SAVE_INFO("\n============ Test Item: CB Test\n");
713     memset(tdata->buffer, 0, tdata->buffer_length);
714     cbdata = tdata->buffer;
715 
716     if (!thr->cb_min || !thr->cb_max) {
717         FTS_TEST_SAVE_ERR("cb_min/max is null\n");
718         ret = -EINVAL;
719         goto test_err;
720     }
721 
722     ret = enter_factory_mode();
723     if (ret) {
724         FTS_TEST_SAVE_ERR("// Failed to Enter factory mode.ret:%d\n", ret);
725         goto test_err;
726     }
727 
728     ret = ft8201_chip_clb(info);
729     if (ret) {
730         FTS_TEST_SAVE_ERR("//========= auto clb Failed\n");
731         goto test_err;
732     }
733 
734     byte_num = tdata->node.node_num;
735     ret = ft8201_get_tx_rx_cb(info, 0, byte_num, cbdata);
736     if (ret < 0) {
737         FTS_TEST_SAVE_ERR("get cb fail\n");
738         goto test_err;
739     }
740 
741     key_check = thr->basic.cb_vkey_check;
742 
743     show_data(cbdata, key_check);
744     save_data_csv(cbdata, "CB Test", \
745                   CODE_CB_TEST, false, key_check);
746     /* compare */
747     tmp_result = compare_array(cbdata, thr->cb_min, thr->cb_max, key_check);
748 
749     ret = 0;
750 
751 test_err:
752     if (tmp_result) {
753         *test_result = true;
754         FTS_TEST_SAVE_INFO("\n------ CB Test PASS\n");
755     } else {
756         *test_result = false;
757         FTS_TEST_SAVE_INFO("\n------ CB Test NG\n");
758     }
759     FTS_TEST_FUNC_EXIT();
760     return ret;
761 
762 }
763 
ft8201_rawdata_test(struct ft8201_info * info,struct fts_test * tdata,bool * test_result)764 static int ft8201_rawdata_test(struct ft8201_info *info, struct fts_test *tdata, bool *test_result)
765 {
766     int ret = 0;
767     bool tmp_result = false;
768     int i = 0;
769     bool key_check = true;
770     int *rawdata = NULL;
771     struct incell_threshold *thr = &tdata->ic.incell.thr;
772 
773     FTS_TEST_FUNC_ENTER();
774     FTS_TEST_SAVE_INFO("\n============ Test Item: RawData Test\n");
775     memset(tdata->buffer, 0, tdata->buffer_length);
776     rawdata = tdata->buffer;
777 
778     if (!thr->rawdata_min || !thr->rawdata_max) {
779         FTS_TEST_SAVE_ERR("rawdata_min/max is null\n");
780         ret = -EINVAL;
781         goto test_err;
782     }
783 
784     ret = enter_factory_mode();
785     if (ret < 0) {
786         FTS_TEST_SAVE_ERR("enter factory mode fail,ret=%d\n", ret);
787         goto test_err;
788     }
789 
790     /* read rawdata */
791     for (i = 0 ; i < 3; i++) {
792         ret = get_rawdata(rawdata);
793     }
794     if (ret < 0) {
795         FTS_TEST_SAVE_ERR("get RawData fail,ret=%d\n", ret);
796         goto test_err;
797     }
798 
799     /* save */
800     show_data(rawdata, key_check);
801     save_data_csv(rawdata, "RawData Test", \
802                   CODE_RAWDATA_TEST, false, key_check);
803     /* compare */
804     tmp_result = compare_array(rawdata,
805                                thr->rawdata_min,
806                                thr->rawdata_max,
807                                key_check);
808 
809 
810 test_err:
811     if (tmp_result) {
812         *test_result = true;
813         FTS_TEST_SAVE_INFO("\n------ RawData Test PASS\n");
814     } else {
815         *test_result = false;
816         FTS_TEST_SAVE_INFO("\n------ RawData Test NG\n");
817     }
818     FTS_TEST_FUNC_EXIT();
819     return ret;
820 
821 }
822 
ft8201_lcdnoise_test(struct ft8201_info * info,struct fts_test * tdata,bool * test_result)823 static int ft8201_lcdnoise_test(struct ft8201_info *info, struct fts_test *tdata, bool *test_result)
824 {
825     int ret = 0;
826     bool tmp_result = false;
827     int frame_num = 0;
828     int i = 0;
829     int max = 0;
830     int max_vk = 0;
831     int byte_num  = 0;
832     u8 old_mode = 0;
833     u8 reg_value = 0;
834     u8 status = 0;
835     int *lcdnoise = NULL;
836     struct incell_threshold *thr = &tdata->ic.incell.thr;
837 
838     FTS_TEST_FUNC_ENTER();
839     FTS_TEST_SAVE_INFO("\n============ Test Item: LCD Noise Test\n");
840     memset(tdata->buffer, 0, tdata->buffer_length);
841     lcdnoise = tdata->buffer;
842 
843     ret = enter_factory_mode();
844     if (ret < 0) {
845         FTS_TEST_SAVE_ERR("enter factory mode fail,ret=%d\n", ret);
846         goto test_err;
847     }
848 
849 
850     ret = fts_test_read_reg(FACTORY_REG_DATA_SELECT, &old_mode);
851     if (ret < 0) {
852         FTS_TEST_SAVE_ERR("read reg06 fail\n");
853         goto test_err;
854     }
855 
856     ret = fts_test_read_reg(FACTORY_NOISE_MODE_REG, &reg_value);
857     if (ret < 0) {
858         FTS_TEST_SAVE_ERR("read reg5e fail\n");
859         goto test_err;
860     }
861 
862     ret = ft8201_write_reg(info, FACTORY_REG_DATA_SELECT, 0x64);
863     if (ret < 0) {
864         FTS_TEST_SAVE_ERR("write 0x64 to reg5e fail\n");
865         goto restore_reg;
866     }
867 
868     ret = ft8201_write_reg(info, FACTORY_REG_DATA_SELECT, 0x01);
869     if (ret < 0) {
870         FTS_TEST_SAVE_ERR("write 1 to reg06 fail\n");
871         goto restore_reg;
872     }
873 
874     frame_num = thr->basic.lcdnoise_frame;
875     ret = ft8201_write_reg(info, FACTORY_REG_LCD_NOISE_FRAME, frame_num & 0xff);
876     if (ret < 0) {
877         FTS_TEST_SAVE_INFO("write frame num fail\n");
878         goto restore_reg;
879     }
880     ret = ft8201_write_reg(info, FACTORY_REG_LCD_NOISE_FRAME + 1, (frame_num >> 8) & 0xff);
881     if (ret < 0) {
882         FTS_TEST_SAVE_INFO("write frame num fail\n");
883         goto restore_reg;
884     }
885 
886     /* read noise data */
887     ret = fts_test_write_reg(FACTORY_REG_LINE_ADDR, 0xAD);
888     if (ret < 0) {
889         FTS_TEST_SAVE_ERR("write 0xAD to reg01 fail\n");
890         goto restore_reg;
891     }
892 
893     /* start test */
894     ret = ft8201_write_reg(info, FACTORY_REG_LCD_NOISE_START, 0x01);
895     if (ret < 0) {
896         FTS_TEST_SAVE_INFO("start lcdnoise test fail\n");
897         goto restore_reg;
898     }
899     sys_delay(frame_num * FACTORY_TEST_DELAY / 2);
900     for (i = 0; i < FACTORY_TEST_RETRY; i++) {
901         status = 0xFF;
902         ret = fts_test_read_reg(FACTORY_REG_LCD_NOISE_START, &status );
903         if ((ret >= 0) && (0x00 == status)) {
904             break;
905         } else {
906             FTS_TEST_DBG("reg%x=%x,retry:%d\n", FACTORY_REG_LCD_NOISE_START, status, i);
907         }
908         sys_delay(FACTORY_TEST_RETRY_DELAY);
909     }
910     if (i >= FACTORY_TEST_RETRY) {
911         FTS_TEST_SAVE_ERR("lcdnoise test timeout\n");
912         //ret = -ENODATA;
913         goto restore_reg;
914     }
915 
916     byte_num = tdata->node.node_num * 2;
917     ret = read_mass_data(FACTORY_REG_RAWDATA_ADDR, byte_num, lcdnoise);
918     if (ret < 0) {
919         FTS_TEST_SAVE_ERR("read rawdata fail\n");
920         goto restore_reg;
921     }
922 
923     /* save */
924     show_data(lcdnoise, true);
925     save_data_csv(lcdnoise, "LCD Noise Test", \
926                   CODE_LCD_NOISE_TEST, false, true);
927 
928     /* compare */
929     max = thr->basic.lcdnoise_coefficient * tdata->va_touch_thr * 32 / 100;
930     max_vk = thr->basic.lcdnoise_coefficient_vkey * tdata->vk_touch_thr * 32 / 100;
931     tmp_result = compare_data(lcdnoise, 0, max, 0, max_vk, true);
932 
933     //ret = 0;
934 
935 restore_reg:
936     ret = fts_test_write_reg(FACTORY_REG_DATA_SELECT, old_mode);
937     if (ret < 0) {
938         FTS_TEST_SAVE_ERR("restore reg06 fail\n");
939     }
940     ret = fts_test_write_reg(FACTORY_NOISE_MODE_REG, reg_value);
941     if (ret < 0) {
942         FTS_TEST_SAVE_ERR("restore reg5e fail\n");
943     }
944     ret = fts_test_write_reg(FACTORY_REG_LCD_NOISE_START, 0x00);
945     if (ret < 0) {
946         FTS_TEST_SAVE_ERR("restore reg11 fail\n");
947     }
948 
949 test_err:
950     if (tmp_result) {
951         *test_result = true;
952         FTS_TEST_SAVE_INFO("\n------ LCD Noise Test PASS\n");
953     } else {
954         *test_result = false;
955         FTS_TEST_SAVE_INFO("\n------ LCD Noise Test NG\n");
956     }
957     FTS_TEST_FUNC_EXIT();
958     return ret;
959 
960 }
961 
start_test_ft8201(void)962 static int start_test_ft8201(void)
963 {
964     int ret = 0;
965     struct fts_test *tdata = fts_ftest;
966     struct incell_testitem *test_item = &tdata->ic.incell.u.item;
967     bool temp_result = false;
968     bool test_result = true;
969     struct ft8201_info info;
970 
971     FTS_TEST_FUNC_ENTER();
972     FTS_TEST_INFO("test item:0x%x", fts_ftest->ic.incell.u.tmp);
973 
974     if (!tdata || !tdata->testresult || !tdata->buffer) {
975         FTS_TEST_ERROR("tdata is null");
976         return -EINVAL;
977     }
978 
979     ret = ft8201_test_init(&info);
980     if (ret) {
981         FTS_TEST_SAVE_ERR("test init fail\n");
982         return ret;
983     }
984 
985     /* short test */
986     if (true == test_item->short_test) {
987         ret = ft8201_short_test(&info, tdata, &temp_result);
988         if ((ret < 0) || (false == temp_result)) {
989             test_result = false;
990         }
991     }
992 
993     /* open test */
994     if (true == test_item->open_test) {
995         ret = ft8201_open_test(&info, tdata, &temp_result);
996         if ((ret < 0) || (false == temp_result)) {
997             test_result = false;
998         }
999     }
1000 
1001     /* cb test */
1002     if (true == test_item->cb_test) {
1003         ret = ft8201_cb_test(&info, tdata, &temp_result);
1004         if ((ret < 0) || (false == temp_result)) {
1005             test_result = false;
1006         }
1007     }
1008 
1009     /* rawdata test */
1010     if (true == test_item->rawdata_test) {
1011         ret = ft8201_rawdata_test(&info, tdata, &temp_result);
1012         if ((ret < 0) || (false == temp_result)) {
1013             test_result = false;
1014         }
1015     }
1016 
1017     /* lcd noise test */
1018     if (true == test_item->lcdnoise_test) {
1019         ret = ft8201_lcdnoise_test(&info, tdata, &temp_result);
1020         if ((ret < 0) || (false == temp_result)) {
1021             test_result = false;
1022         }
1023     }
1024 
1025     return test_result;
1026 }
1027 
1028 struct test_funcs test_func_ft8201 = {
1029     .ctype = {0x10},
1030     .hwtype = IC_HW_INCELL,
1031     .key_num_total = 12,
1032     .start_test = start_test_ft8201,
1033 };
1034 
1035