forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 59
/
atmel_mxt_ts.c
3094 lines (2552 loc) · 72.8 KB
/
atmel_mxt_ts.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
/*
* Atmel maXTouch Touchscreen driver
*
* Copyright (C) 2010 Samsung Electronics Co.Ltd
* Copyright (C) 2011-2012 Atmel Corporation
* Copyright (C) 2012 Google, Inc.
*
* Author: Joonyoung Shim <[email protected]>
*
* 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.
*
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/i2c.h>
#include <linux/i2c/atmel_mxt_ts.h>
#include <linux/input/mt.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/regulator/consumer.h>
#include <linux/gpio.h>
/* Configuration file */
#define MXT_CFG_MAGIC "OBP_RAW V1"
/* Registers */
#define MXT_OBJECT_START 0x07
#define MXT_OBJECT_SIZE 6
#define MXT_INFO_CHECKSUM_SIZE 3
#define MXT_MAX_BLOCK_WRITE 256
/* Object types */
#define MXT_DEBUG_DIAGNOSTIC_T37 37
#define MXT_GEN_MESSAGE_T5 5
#define MXT_GEN_COMMAND_T6 6
#define MXT_GEN_POWER_T7 7
#define MXT_GEN_ACQUIRE_T8 8
#define MXT_GEN_DATASOURCE_T53 53
#define MXT_TOUCH_MULTI_T9 9
#define MXT_TOUCH_KEYARRAY_T15 15
#define MXT_TOUCH_PROXIMITY_T23 23
#define MXT_TOUCH_PROXKEY_T52 52
#define MXT_PROCI_GRIPFACE_T20 20
#define MXT_PROCG_NOISE_T22 22
#define MXT_PROCI_ONETOUCH_T24 24
#define MXT_PROCI_TWOTOUCH_T27 27
#define MXT_PROCI_GRIP_T40 40
#define MXT_PROCI_PALM_T41 41
#define MXT_PROCI_TOUCHSUPPRESSION_T42 42
#define MXT_PROCI_STYLUS_T47 47
#define MXT_PROCG_NOISESUPPRESSION_T48 48
#define MXT_SPT_COMMSCONFIG_T18 18
#define MXT_SPT_GPIOPWM_T19 19
#define MXT_SPT_SELFTEST_T25 25
#define MXT_SPT_CTECONFIG_T28 28
#define MXT_SPT_USERDATA_T38 38
#define MXT_SPT_DIGITIZER_T43 43
#define MXT_SPT_MESSAGECOUNT_T44 44
#define MXT_SPT_CTECONFIG_T46 46
#define MXT_SPT_NOISESUPPRESSION_T48 48
#define MXT_PROCI_ACTIVE_STYLUS_T63 63
#define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
/* MXT_GEN_MESSAGE_T5 object */
#define MXT_RPTID_NOMSG 0xff
/* MXT_GEN_COMMAND_T6 field */
#define MXT_COMMAND_RESET 0
#define MXT_COMMAND_BACKUPNV 1
#define MXT_COMMAND_CALIBRATE 2
#define MXT_COMMAND_REPORTALL 3
#define MXT_COMMAND_DIAGNOSTIC 5
/* Define for T6 status byte */
#define MXT_T6_STATUS_RESET (1 << 7)
#define MXT_T6_STATUS_OFL (1 << 6)
#define MXT_T6_STATUS_SIGERR (1 << 5)
#define MXT_T6_STATUS_CAL (1 << 4)
#define MXT_T6_STATUS_CFGERR (1 << 3)
#define MXT_T6_STATUS_COMSERR (1 << 2)
/* MXT_GEN_POWER_T7 field */
struct t7_config {
u8 idle;
u8 active;
} __packed;
#define MXT_POWER_CFG_RUN 0
#define MXT_POWER_CFG_DEEPSLEEP 1
/* MXT_TOUCH_MULTI_T9 field */
#define MXT_T9_ORIENT 9
#define MXT_T9_RANGE 18
/* MXT_TOUCH_MULTI_T9 status */
#define MXT_T9_UNGRIP (1 << 0)
#define MXT_T9_SUPPRESS (1 << 1)
#define MXT_T9_AMP (1 << 2)
#define MXT_T9_VECTOR (1 << 3)
#define MXT_T9_MOVE (1 << 4)
#define MXT_T9_RELEASE (1 << 5)
#define MXT_T9_PRESS (1 << 6)
#define MXT_T9_DETECT (1 << 7)
struct t9_range {
u16 x;
u16 y;
} __packed;
/* MXT_TOUCH_MULTI_T9 orient */
#define MXT_T9_ORIENT_SWITCH (1 << 0)
/* MXT_SPT_COMMSCONFIG_T18 */
#define MXT_COMMS_CTRL 0
#define MXT_COMMS_CMD 1
#define MXT_COMMS_RETRIGEN (1 << 6)
/* Define for MXT_GEN_COMMAND_T6 */
#define MXT_BOOT_VALUE 0xa5
#define MXT_RESET_VALUE 0x01
#define MXT_BACKUP_VALUE 0x55
/* Define for MXT_PROCI_TOUCHSUPPRESSION_T42 */
#define MXT_T42_MSG_TCHSUP (1 << 0)
/* T47 Stylus */
#define MXT_TOUCH_MAJOR_T47_STYLUS 1
/* T63 Stylus */
#define MXT_STYLUS_PRESS (1 << 0)
#define MXT_STYLUS_RELEASE (1 << 1)
#define MXT_STYLUS_MOVE (1 << 2)
#define MXT_STYLUS_SUPPRESS (1 << 3)
#define MXT_STYLUS_DETECT (1 << 4)
#define MXT_STYLUS_TIP (1 << 5)
#define MXT_STYLUS_ERASER (1 << 6)
#define MXT_STYLUS_BARREL (1 << 7)
#define MXT_STYLUS_PRESSURE_MASK 0x3F
/* T100 Multiple Touch Touchscreen */
#define MXT_T100_CTRL 0
#define MXT_T100_CFG1 1
#define MXT_T100_TCHAUX 3
#define MXT_T100_XRANGE 13
#define MXT_T100_YRANGE 24
#define MXT_T100_CFG_SWITCHXY (1 << 5)
#define MXT_T100_TCHAUX_VECT (1 << 0)
#define MXT_T100_TCHAUX_AMPL (1 << 1)
#define MXT_T100_TCHAUX_AREA (1 << 2)
#define MXT_T100_DETECT (1 << 7)
#define MXT_T100_TYPE_MASK 0x70
#define MXT_T100_TYPE_STYLUS 0x20
/* Delay times */
#define MXT_BACKUP_TIME 50 /* msec */
#define MXT_RESET_TIME 200 /* msec */
#define MXT_RESET_TIMEOUT 3000 /* msec */
#define MXT_CRC_TIMEOUT 1000 /* msec */
#define MXT_FW_RESET_TIME 1000 /* msec */
#define MXT_FW_CHG_TIMEOUT 300 /* msec */
#define MXT_WAKEUP_TIME 25 /* msec */
#define MXT_REGULATOR_DELAY 150 /* msec */
#define MXT_POWERON_DELAY 150 /* msec */
/* Command to unlock bootloader */
#define MXT_UNLOCK_CMD_MSB 0xaa
#define MXT_UNLOCK_CMD_LSB 0xdc
/* Bootloader mode status */
#define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
#define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
#define MXT_FRAME_CRC_CHECK 0x02
#define MXT_FRAME_CRC_FAIL 0x03
#define MXT_FRAME_CRC_PASS 0x04
#define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
#define MXT_BOOT_STATUS_MASK 0x3f
#define MXT_BOOT_EXTENDED_ID (1 << 5)
#define MXT_BOOT_ID_MASK 0x1f
/* Touchscreen absolute values */
#define MXT_MAX_AREA 0xff
#define MXT_PIXELS_PER_MM 20
struct mxt_info {
u8 family_id;
u8 variant_id;
u8 version;
u8 build;
u8 matrix_xsize;
u8 matrix_ysize;
u8 object_num;
};
struct mxt_object {
u8 type;
u16 start_address;
u8 size_minus_one;
u8 instances_minus_one;
u8 num_report_ids;
} __packed;
/* Each client has this additional data */
struct mxt_data {
struct i2c_client *client;
struct input_dev *input_dev;
char phys[64]; /* device physical location */
struct mxt_platform_data *pdata;
struct mxt_object *object_table;
struct mxt_info *info;
void *raw_info_block;
unsigned int irq;
unsigned int max_x;
unsigned int max_y;
bool in_bootloader;
u16 mem_size;
u8 t100_aux_ampl;
u8 t100_aux_area;
u8 t100_aux_vect;
struct bin_attribute mem_access_attr;
bool debug_enabled;
u8 max_reportid;
u32 config_crc;
u32 info_crc;
u8 bootloader_addr;
struct t7_config t7_cfg;
u8 *msg_buf;
u8 t6_status;
bool update_input;
u8 last_message_count;
u8 num_touchids;
u8 num_stylusids;
unsigned long t15_keystatus;
bool use_retrigen_workaround;
bool use_regulator;
struct regulator *reg_vdd;
struct regulator *reg_avdd;
char *fw_name;
char *cfg_name;
/* Cached parameters from object table */
u16 T5_address;
u8 T5_msg_size;
u8 T6_reportid;
u16 T6_address;
u16 T7_address;
u8 T9_reportid_min;
u8 T9_reportid_max;
u8 T15_reportid_min;
u8 T15_reportid_max;
u16 T18_address;
u8 T19_reportid;
u8 T42_reportid_min;
u8 T42_reportid_max;
u16 T44_address;
u8 T48_reportid;
u8 T63_reportid_min;
u8 T63_reportid_max;
u8 T100_reportid_min;
u8 T100_reportid_max;
/* for fw update in bootloader */
struct completion bl_completion;
/* for reset handling */
struct completion reset_completion;
/* for reset handling */
struct completion crc_completion;
/* Enable reporting of input events */
bool enable_reporting;
/* Indicates whether device is in suspend */
bool suspended;
};
static inline size_t mxt_obj_size(const struct mxt_object *obj)
{
return obj->size_minus_one + 1;
}
static inline size_t mxt_obj_instances(const struct mxt_object *obj)
{
return obj->instances_minus_one + 1;
}
static bool mxt_object_readable(unsigned int type)
{
switch (type) {
case MXT_GEN_COMMAND_T6:
case MXT_GEN_POWER_T7:
case MXT_GEN_ACQUIRE_T8:
case MXT_GEN_DATASOURCE_T53:
case MXT_TOUCH_MULTI_T9:
case MXT_TOUCH_KEYARRAY_T15:
case MXT_TOUCH_PROXIMITY_T23:
case MXT_TOUCH_PROXKEY_T52:
case MXT_PROCI_GRIPFACE_T20:
case MXT_PROCG_NOISE_T22:
case MXT_PROCI_ONETOUCH_T24:
case MXT_PROCI_TWOTOUCH_T27:
case MXT_PROCI_GRIP_T40:
case MXT_PROCI_PALM_T41:
case MXT_PROCI_TOUCHSUPPRESSION_T42:
case MXT_PROCI_STYLUS_T47:
case MXT_PROCG_NOISESUPPRESSION_T48:
case MXT_SPT_COMMSCONFIG_T18:
case MXT_SPT_GPIOPWM_T19:
case MXT_SPT_SELFTEST_T25:
case MXT_SPT_CTECONFIG_T28:
case MXT_SPT_USERDATA_T38:
case MXT_SPT_DIGITIZER_T43:
case MXT_SPT_CTECONFIG_T46:
return true;
default:
return false;
}
}
static void mxt_dump_message(struct mxt_data *data, u8 *message)
{
print_hex_dump(KERN_DEBUG, "MXT MSG:", DUMP_PREFIX_NONE, 16, 1,
message, data->T5_msg_size, false);
}
static int mxt_wait_for_completion(struct mxt_data *data,
struct completion *comp, unsigned int timeout_ms)
{
struct device *dev = &data->client->dev;
unsigned long timeout = msecs_to_jiffies(timeout_ms);
long ret;
ret = wait_for_completion_interruptible_timeout(comp, timeout);
if (ret < 0) {
dev_err(dev, "Wait for completion interrupted.\n");
return -EINTR;
} else if (ret == 0) {
dev_err(dev, "Wait for completion timed out.\n");
return -ETIMEDOUT;
}
return 0;
}
static int mxt_bootloader_read(struct mxt_data *data,
u8 *val, unsigned int count)
{
int ret;
struct i2c_msg msg;
msg.addr = data->bootloader_addr;
msg.flags = data->client->flags & I2C_M_TEN;
msg.flags |= I2C_M_RD;
msg.len = count;
msg.buf = val;
ret = i2c_transfer(data->client->adapter, &msg, 1);
if (ret == 1) {
ret = 0;
} else {
ret = (ret < 0) ? ret : -EIO;
dev_err(&data->client->dev, "i2c recv failed (%d)\n", ret);
}
return ret;
}
static int mxt_bootloader_write(struct mxt_data *data,
const u8 * const val, unsigned int count)
{
int ret;
struct i2c_msg msg;
msg.addr = data->bootloader_addr;
msg.flags = data->client->flags & I2C_M_TEN;
msg.len = count;
msg.buf = (u8 *)val;
ret = i2c_transfer(data->client->adapter, &msg, 1);
if (ret == 1) {
ret = 0;
} else {
ret = (ret < 0) ? ret : -EIO;
dev_err(&data->client->dev, "i2c send failed (%d)\n", ret);
}
return ret;
}
static int mxt_lookup_bootloader_address(struct mxt_data *data, u8 retry)
{
u8 appmode = data->client->addr;
u8 bootloader;
u8 family_id = 0;
if (data->info)
family_id = data->info->family_id;
switch (appmode) {
case 0x4a:
case 0x4b:
/* Chips after 1664S use different scheme */
if ((retry % 2) || family_id >= 0xa2) {
bootloader = appmode - 0x24;
break;
}
/* Fall through for normal case */
case 0x4c:
case 0x4d:
case 0x5a:
case 0x5b:
bootloader = appmode - 0x26;
break;
default:
dev_err(&data->client->dev,
"Appmode i2c address 0x%02x not found\n",
appmode);
return -EINVAL;
}
data->bootloader_addr = bootloader;
return 0;
}
static int mxt_probe_bootloader(struct mxt_data *data, u8 retry)
{
struct device *dev = &data->client->dev;
int ret;
u8 val;
bool crc_failure;
ret = mxt_lookup_bootloader_address(data, retry);
if (ret)
return ret;
ret = mxt_bootloader_read(data, &val, 1);
if (ret)
return ret;
/* Check app crc fail mode */
crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL;
dev_err(dev, "Detected bootloader, status:%02X%s\n",
val, crc_failure ? ", APP_CRC_FAIL" : "");
return 0;
}
static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
{
struct device *dev = &data->client->dev;
u8 buf[3];
if (val & MXT_BOOT_EXTENDED_ID) {
if (mxt_bootloader_read(data, &buf[0], 3) != 0) {
dev_err(dev, "%s: i2c failure\n", __func__);
return -EIO;
}
dev_info(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]);
return buf[0];
} else {
dev_info(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
return val;
}
}
static int mxt_check_bootloader(struct mxt_data *data, unsigned int state)
{
struct device *dev = &data->client->dev;
u8 val;
int ret;
recheck:
if (state != MXT_WAITING_BOOTLOAD_CMD) {
/*
* In application update mode, the interrupt
* line signals state transitions. We must wait for the
* CHG assertion before reading the status byte.
* Once the status byte has been read, the line is deasserted.
*/
ret = mxt_wait_for_completion(data, &data->bl_completion,
MXT_FW_CHG_TIMEOUT);
if (ret) {
/*
* TODO: handle -EINTR better by terminating fw update
* process before returning to userspace by writing
* length 0x000 to device (iff we are in
* WAITING_FRAME_DATA state).
*/
dev_err(dev, "Update wait error %d\n", ret);
return ret;
}
}
ret = mxt_bootloader_read(data, &val, 1);
if (ret)
return ret;
if (state == MXT_WAITING_BOOTLOAD_CMD)
val = mxt_get_bootloader_version(data, val);
switch (state) {
case MXT_WAITING_BOOTLOAD_CMD:
case MXT_WAITING_FRAME_DATA:
case MXT_APP_CRC_FAIL:
val &= ~MXT_BOOT_STATUS_MASK;
break;
case MXT_FRAME_CRC_PASS:
if (val == MXT_FRAME_CRC_CHECK) {
goto recheck;
} else if (val == MXT_FRAME_CRC_FAIL) {
dev_err(dev, "Bootloader CRC fail\n");
return -EINVAL;
}
break;
default:
return -EINVAL;
}
if (val != state) {
dev_err(dev, "Invalid bootloader state %02X != %02X\n",
val, state);
return -EINVAL;
}
return 0;
}
static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock)
{
int ret;
u8 buf[2];
if (unlock) {
buf[0] = MXT_UNLOCK_CMD_LSB;
buf[1] = MXT_UNLOCK_CMD_MSB;
} else {
buf[0] = 0x01;
buf[1] = 0x01;
}
ret = mxt_bootloader_write(data, buf, 2);
if (ret)
return ret;
return 0;
}
static int __mxt_read_reg(struct i2c_client *client,
u16 reg, u16 len, void *val)
{
struct i2c_msg xfer[2];
u8 buf[2];
int ret;
bool retry = false;
buf[0] = reg & 0xff;
buf[1] = (reg >> 8) & 0xff;
/* Write register */
xfer[0].addr = client->addr;
xfer[0].flags = 0;
xfer[0].len = 2;
xfer[0].buf = buf;
/* Read data */
xfer[1].addr = client->addr;
xfer[1].flags = I2C_M_RD;
xfer[1].len = len;
xfer[1].buf = val;
retry_read:
ret = i2c_transfer(client->adapter, xfer, ARRAY_SIZE(xfer));
if (ret != ARRAY_SIZE(xfer)) {
if (!retry) {
dev_dbg(&client->dev, "%s: i2c retry\n", __func__);
msleep(MXT_WAKEUP_TIME);
retry = true;
goto retry_read;
} else {
dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
__func__, ret);
return -EIO;
}
}
return 0;
}
static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
const void *val)
{
u8 *buf;
size_t count;
int ret;
bool retry = false;
count = len + 2;
buf = kmalloc(count, GFP_KERNEL);
if (!buf)
return -ENOMEM;
buf[0] = reg & 0xff;
buf[1] = (reg >> 8) & 0xff;
memcpy(&buf[2], val, len);
retry_write:
ret = i2c_master_send(client, buf, count);
if (ret == count) {
ret = 0;
} else {
if (!retry) {
dev_dbg(&client->dev, "%s: i2c retry\n", __func__);
msleep(MXT_WAKEUP_TIME);
retry = true;
goto retry_write;
} else {
dev_err(&client->dev, "%s: i2c send failed (%d)\n",
__func__, ret);
ret = -EIO;
}
}
kfree(buf);
return ret;
}
static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
{
return __mxt_write_reg(client, reg, 1, &val);
}
static struct mxt_object *
mxt_get_object(struct mxt_data *data, u8 type)
{
struct mxt_object *object;
int i;
for (i = 0; i < data->info->object_num; i++) {
object = data->object_table + i;
if (object->type == type)
return object;
}
dev_warn(&data->client->dev, "Invalid object type T%u\n", type);
return NULL;
}
static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg)
{
struct device *dev = &data->client->dev;
u8 status = msg[1];
u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16);
if (crc != data->config_crc) {
data->config_crc = crc;
dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc);
complete(&data->crc_completion);
}
/* Detect transition out of reset */
if ((data->t6_status & MXT_T6_STATUS_RESET) &&
!(status & MXT_T6_STATUS_RESET))
complete(&data->reset_completion);
/* Output debug if status has changed */
if (status != data->t6_status)
dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
status,
(status == 0) ? " OK" : "",
(status & MXT_T6_STATUS_RESET) ? " RESET" : "",
(status & MXT_T6_STATUS_OFL) ? " OFL" : "",
(status & MXT_T6_STATUS_SIGERR) ? " SIGERR" : "",
(status & MXT_T6_STATUS_CAL) ? " CAL" : "",
(status & MXT_T6_STATUS_CFGERR) ? " CFGERR" : "",
(status & MXT_T6_STATUS_COMSERR) ? " COMSERR" : "");
/* Save current status */
data->t6_status = status;
}
static void mxt_input_button(struct mxt_data *data, u8 *message)
{
struct input_dev *input = data->input_dev;
const struct mxt_platform_data *pdata = data->pdata;
bool button;
int i;
/* do not report events if input device not yet registered */
if (!data->enable_reporting)
return;
/* Active-low switch */
for (i = 0; i < pdata->t19_num_keys; i++) {
if (pdata->t19_keymap[i] == KEY_RESERVED)
continue;
button = !(message[1] & (1 << i));
input_report_key(input, pdata->t19_keymap[i], button);
}
}
static void mxt_input_sync(struct input_dev *input_dev)
{
input_mt_report_pointer_emulation(input_dev, false);
input_sync(input_dev);
}
static void mxt_proc_t9_message(struct mxt_data *data, u8 *message)
{
struct device *dev = &data->client->dev;
struct input_dev *input_dev = data->input_dev;
int id;
u8 status;
int x;
int y;
int area;
int amplitude;
u8 vector;
int tool;
/* do not report events if input device not yet registered */
if (!data->enable_reporting)
return;
id = message[0] - data->T9_reportid_min;
status = message[1];
x = (message[2] << 4) | ((message[4] >> 4) & 0xf);
y = (message[3] << 4) | ((message[4] & 0xf));
/* Handle 10/12 bit switching */
if (data->max_x < 1024)
x >>= 2;
if (data->max_y < 1024)
y >>= 2;
area = message[5];
amplitude = message[6];
vector = message[7];
dev_dbg(dev,
"[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u vector: %02X\n",
id,
(status & MXT_T9_DETECT) ? 'D' : '.',
(status & MXT_T9_PRESS) ? 'P' : '.',
(status & MXT_T9_RELEASE) ? 'R' : '.',
(status & MXT_T9_MOVE) ? 'M' : '.',
(status & MXT_T9_VECTOR) ? 'V' : '.',
(status & MXT_T9_AMP) ? 'A' : '.',
(status & MXT_T9_SUPPRESS) ? 'S' : '.',
(status & MXT_T9_UNGRIP) ? 'U' : '.',
x, y, area, amplitude, vector);
input_mt_slot(input_dev, id);
if (status & MXT_T9_DETECT) {
/* Multiple bits may be set if the host is slow to read the
* status messages, indicating all the events that have
* happened */
if (status & MXT_T9_RELEASE) {
input_mt_report_slot_state(input_dev,
MT_TOOL_FINGER, 0);
mxt_input_sync(input_dev);
}
/* A reported size of zero indicates that the reported touch
* is a stylus from a linked Stylus T47 object. */
if (area == 0) {
area = MXT_TOUCH_MAJOR_T47_STYLUS;
tool = MT_TOOL_PEN;
} else {
tool = MT_TOOL_FINGER;
}
/* Touch active */
input_mt_report_slot_state(input_dev, tool, 1);
input_report_abs(input_dev, ABS_MT_POSITION_X, x);
input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
input_report_abs(input_dev, ABS_MT_ORIENTATION, vector);
} else {
/* Touch no longer active, close out slot */
input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
}
data->update_input = true;
}
static void mxt_proc_t100_message(struct mxt_data *data, u8 *message)
{
struct device *dev = &data->client->dev;
struct input_dev *input_dev = data->input_dev;
int id;
u8 status;
int x;
int y;
int tool;
/* do not report events if input device not yet registered */
if (!data->enable_reporting)
return;
id = message[0] - data->T100_reportid_min - 2;
/* ignore SCRSTATUS events */
if (id < 0)
return;
status = message[1];
x = (message[3] << 8) | message[2];
y = (message[5] << 8) | message[4];
dev_dbg(dev,
"[%u] status:%02X x:%u y:%u area:%02X amp:%02X vec:%02X\n",
id,
status,
x, y,
(data->t100_aux_area) ? message[data->t100_aux_area] : 0,
(data->t100_aux_ampl) ? message[data->t100_aux_ampl] : 0,
(data->t100_aux_vect) ? message[data->t100_aux_vect] : 0);
input_mt_slot(input_dev, id);
if (status & MXT_T100_DETECT) {
/* A reported size of zero indicates that the reported touch
* is a stylus from a linked Stylus T47 object. */
if ((status & MXT_T100_TYPE_MASK) == MXT_T100_TYPE_STYLUS)
tool = MT_TOOL_PEN;
else
tool = MT_TOOL_FINGER;
/* Touch active */
input_mt_report_slot_state(input_dev, tool, 1);
input_report_abs(input_dev, ABS_MT_POSITION_X, x);
input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
if (data->t100_aux_ampl)
input_report_abs(input_dev, ABS_MT_PRESSURE,
message[data->t100_aux_ampl]);
if (data->t100_aux_area) {
if (tool == MT_TOOL_PEN)
input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
MXT_TOUCH_MAJOR_T47_STYLUS);
else
input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
message[data->t100_aux_area]);
}
if (data->t100_aux_vect)
input_report_abs(input_dev, ABS_MT_ORIENTATION,
message[data->t100_aux_vect]);
} else {
/* Touch no longer active, close out slot */
input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
}
data->update_input = true;
}
static void mxt_proc_t15_messages(struct mxt_data *data, u8 *msg)
{
struct input_dev *input_dev = data->input_dev;
struct device *dev = &data->client->dev;
int key;
bool curr_state, new_state;
bool sync = false;
unsigned long keystates = le32_to_cpu(msg[2]);
/* do not report events if input device not yet registered */
if (!data->enable_reporting)
return;
for (key = 0; key < data->pdata->t15_num_keys; key++) {
curr_state = test_bit(key, &data->t15_keystatus);
new_state = test_bit(key, &keystates);
if (!curr_state && new_state) {
dev_dbg(dev, "T15 key press: %u\n", key);
__set_bit(key, &data->t15_keystatus);
input_event(input_dev, EV_KEY,
data->pdata->t15_keymap[key], 1);
sync = true;
} else if (curr_state && !new_state) {
dev_dbg(dev, "T15 key release: %u\n", key);
__clear_bit(key, &data->t15_keystatus);
input_event(input_dev, EV_KEY,
data->pdata->t15_keymap[key], 0);
sync = true;
}
}
if (sync)
input_sync(input_dev);
}
static void mxt_proc_t42_messages(struct mxt_data *data, u8 *msg)
{
struct device *dev = &data->client->dev;
u8 status = msg[1];
if (status & MXT_T42_MSG_TCHSUP)
dev_info(dev, "T42 suppress\n");
else
dev_info(dev, "T42 normal\n");
}
static int mxt_proc_t48_messages(struct mxt_data *data, u8 *msg)
{
struct device *dev = &data->client->dev;
u8 status, state;
status = msg[1];
state = msg[4];
dev_dbg(dev, "T48 state %d status %02X %s%s%s%s%s\n",
state,
status,
(status & 0x01) ? "FREQCHG " : "",
(status & 0x02) ? "APXCHG " : "",
(status & 0x04) ? "ALGOERR " : "",
(status & 0x10) ? "STATCHG " : "",
(status & 0x20) ? "NLVLCHG " : "");
return 0;
}
static void mxt_proc_t63_messages(struct mxt_data *data, u8 *msg)
{
struct device *dev = &data->client->dev;
struct input_dev *input_dev = data->input_dev;
u8 id;
u16 x, y;
u8 pressure;
/* do not report events if input device not yet registered */
if (!data->enable_reporting)
return;
/* stylus slots come after touch slots */
id = data->num_touchids + (msg[0] - data->T63_reportid_min);
if (id < 0 || id > (data->num_touchids + data->num_stylusids)) {
dev_err(dev, "invalid stylus id %d, max slot is %d\n",
id, data->num_stylusids);
return;
}
x = msg[3] | (msg[4] << 8);
y = msg[5] | (msg[6] << 8);
pressure = msg[7] & MXT_STYLUS_PRESSURE_MASK;
dev_dbg(dev,
"[%d] %c%c%c%c x: %d y: %d pressure: %d stylus:%c%c%c%c\n",
id,
(msg[1] & MXT_STYLUS_SUPPRESS) ? 'S' : '.',
(msg[1] & MXT_STYLUS_MOVE) ? 'M' : '.',
(msg[1] & MXT_STYLUS_RELEASE) ? 'R' : '.',
(msg[1] & MXT_STYLUS_PRESS) ? 'P' : '.',
x, y, pressure,
(msg[2] & MXT_STYLUS_BARREL) ? 'B' : '.',
(msg[2] & MXT_STYLUS_ERASER) ? 'E' : '.',
(msg[2] & MXT_STYLUS_TIP) ? 'T' : '.',
(msg[2] & MXT_STYLUS_DETECT) ? 'D' : '.');
input_mt_slot(input_dev, id);
if (msg[2] & MXT_STYLUS_DETECT) {
input_mt_report_slot_state(input_dev, MT_TOOL_PEN, 1);
input_report_abs(input_dev, ABS_MT_POSITION_X, x);
input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
} else {
input_mt_report_slot_state(input_dev, MT_TOOL_PEN, 0);
}
input_report_key(input_dev, BTN_STYLUS, (msg[2] & MXT_STYLUS_ERASER));
input_report_key(input_dev, BTN_STYLUS2, (msg[2] & MXT_STYLUS_BARREL));
mxt_input_sync(input_dev);
}