forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 59
/
atmel_mxt_ts.c
3775 lines (3091 loc) · 87.7 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-2014 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/acpi.h>
#include <linux/dmi.h>
#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/platform_data/atmel_mxt_ts.h>
#include <linux/input/mt.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/slab.h>
#include <asm/unaligned.h>
#include <linux/regulator/consumer.h>
#include <linux/gpio.h>
#include <linux/workqueue.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 255
/* 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_PROCI_ACTIVE_STYLUS_T63 63
#define MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71 71
#define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
#define MXT_PROCI_ACTIVESTYLUS_T107 107
/* 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 BIT(7)
#define MXT_T6_STATUS_OFL BIT(6)
#define MXT_T6_STATUS_SIGERR BIT(5)
#define MXT_T6_STATUS_CAL BIT(4)
#define MXT_T6_STATUS_CFGERR BIT(3)
#define MXT_T6_STATUS_COMSERR BIT(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_CTRL 0
#define MXT_T9_ORIENT 9
#define MXT_T9_RANGE 18
/* MXT_TOUCH_MULTI_T9 status */
#define MXT_T9_UNGRIP BIT(0)
#define MXT_T9_SUPPRESS BIT(1)
#define MXT_T9_AMP BIT(2)
#define MXT_T9_VECTOR BIT(3)
#define MXT_T9_MOVE BIT(4)
#define MXT_T9_RELEASE BIT(5)
#define MXT_T9_PRESS BIT(6)
#define MXT_T9_DETECT BIT(7)
struct t9_range {
u16 x;
u16 y;
} __packed;
/* MXT_TOUCH_MULTI_T9 orient */
#define MXT_T9_ORIENT_SWITCH BIT(0)
/* MXT_SPT_COMMSCONFIG_T18 */
#define MXT_COMMS_CTRL 0
#define MXT_COMMS_CMD 1
#define MXT_COMMS_RETRIGEN BIT(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 BIT(0)
/* T63 Stylus */
#define MXT_T63_STYLUS_PRESS BIT(0)
#define MXT_T63_STYLUS_RELEASE BIT(1)
#define MXT_T63_STYLUS_MOVE BIT(2)
#define MXT_T63_STYLUS_SUPPRESS BIT(3)
#define MXT_T63_STYLUS_DETECT BIT(4)
#define MXT_T63_STYLUS_TIP BIT(5)
#define MXT_T63_STYLUS_ERASER BIT(6)
#define MXT_T63_STYLUS_BARREL BIT(7)
#define MXT_T63_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 BIT(5)
#define MXT_T100_TCHAUX_VECT BIT(0)
#define MXT_T100_TCHAUX_AMPL BIT(1)
#define MXT_T100_TCHAUX_AREA BIT(2)
#define MXT_T100_DETECT BIT(7)
#define MXT_T100_TYPE_MASK 0x70
enum t100_type {
MXT_T100_TYPE_FINGER = 1,
MXT_T100_TYPE_PASSIVE_STYLUS = 2,
MXT_T100_TYPE_ACTIVE_STYLUS = 3,
MXT_T100_TYPE_HOVERING_FINGER = 4,
MXT_T100_TYPE_GLOVE = 5,
MXT_T100_TYPE_LARGE_TOUCH = 6,
};
#define MXT_DISTANCE_ACTIVE_TOUCH 0
#define MXT_DISTANCE_HOVERING 1
#define MXT_TOUCH_MAJOR_DEFAULT 1
#define MXT_PRESSURE_DEFAULT 1
/* Gen2 Active Stylus */
#define MXT_T107_STYLUS_STYAUX 42
#define MXT_T107_STYLUS_STYAUX_PRESSURE BIT(0)
#define MXT_T107_STYLUS_STYAUX_PEAK BIT(4)
#define MXT_T107_STYLUS_HOVER BIT(0)
#define MXT_T107_STYLUS_TIPSWITCH BIT(1)
#define MXT_T107_STYLUS_BUTTON0 BIT(2)
#define MXT_T107_STYLUS_BUTTON1 BIT(3)
/* 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 3000 /* msec */
#define MXT_FW_CHG_TIMEOUT 300 /* msec */
#define MXT_WAKEUP_TIME 25 /* msec */
#define MXT_REGULATOR_DELAY 150 /* msec */
#define MXT_CHG_DELAY 100 /* msec */
#define MXT_POWERON_DELAY 150 /* msec */
#define MXT_BOOTLOADER_WAIT 36E5 /* 1 minute */
/* 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 BIT(5)
#define MXT_BOOT_ID_MASK 0x1f
/* Touchscreen absolute values */
#define MXT_MAX_AREA 0xff
#define MXT_PIXELS_PER_MM 20
#define DEBUG_MSG_MAX 200
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;
/* Firmware frame structure */
struct mxt_fw_frame {
__be16 size;
u8 data[];
};
/* Firmware update context */
struct mxt_flash {
struct mxt_data *data;
const struct firmware *fw;
struct mxt_fw_frame *frame;
loff_t pos;
size_t frame_size;
unsigned int count;
unsigned int retry;
u8 previous;
struct completion flash_completion;
struct delayed_work work;
};
/* Each client has this additional data */
struct mxt_data {
struct i2c_client *client;
struct input_dev *input_dev;
char phys[64]; /* device physical location */
const 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;
bool debug_v2_enabled;
u8 *debug_msg_data;
u16 debug_msg_count;
struct bin_attribute debug_msg_attr;
struct mutex debug_msg_lock;
u8 max_reportid;
u32 config_crc;
u32 info_crc;
u8 bootloader_addr;
u8 *msg_buf;
u8 t6_status;
bool update_input;
u8 last_message_count;
u8 num_touchids;
u8 multitouch;
struct t7_config t7_cfg;
u8 num_stylusids;
unsigned long t15_keystatus;
u8 stylus_aux_pressure;
u8 stylus_aux_peak;
bool use_retrigen_workaround;
struct regulator *reg_vdd;
struct regulator *reg_avdd;
char *fw_name;
char *cfg_name;
struct mxt_flash *flash;
/* Cached parameters from object table */
u16 T5_address;
u8 T5_msg_size;
u8 T6_reportid;
u16 T6_address;
u16 T7_address;
u16 T71_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;
u16 T107_address;
/* for reset handling */
struct completion reset_completion;
/* for config update handling */
struct completion crc_completion;
/* for power up handling */
struct completion chg_completion;
/* Indicates whether device is in suspend */
bool suspended;
/* Indicates whether device is updating configuration */
bool updating_config;
};
static size_t mxt_obj_size(const struct mxt_object *obj)
{
return obj->size_minus_one + 1;
}
static 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:
case MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71:
return true;
default:
return false;
}
}
static void mxt_dump_message(struct mxt_data *data, u8 *message)
{
dev_dbg(&data->client->dev, "MXT MSG: %*ph\n",
data->T5_msg_size, message);
}
static void mxt_debug_msg_enable(struct mxt_data *data)
{
struct device *dev = &data->client->dev;
if (data->debug_v2_enabled)
return;
mutex_lock(&data->debug_msg_lock);
data->debug_msg_data = kcalloc(DEBUG_MSG_MAX,
data->T5_msg_size, GFP_KERNEL);
if (!data->debug_msg_data)
return;
data->debug_v2_enabled = true;
mutex_unlock(&data->debug_msg_lock);
dev_info(dev, "Enabled message output\n");
}
static void mxt_debug_msg_disable(struct mxt_data *data)
{
struct device *dev = &data->client->dev;
if (!data->debug_v2_enabled)
return;
dev_info(dev, "disabling message output\n");
data->debug_v2_enabled = false;
mutex_lock(&data->debug_msg_lock);
kfree(data->debug_msg_data);
data->debug_msg_data = NULL;
data->debug_msg_count = 0;
mutex_unlock(&data->debug_msg_lock);
dev_info(dev, "Disabled message output\n");
}
static void mxt_debug_msg_add(struct mxt_data *data, u8 *msg)
{
struct device *dev = &data->client->dev;
mutex_lock(&data->debug_msg_lock);
if (!data->debug_msg_data) {
dev_err(dev, "No buffer!\n");
return;
}
if (data->debug_msg_count < DEBUG_MSG_MAX) {
memcpy(data->debug_msg_data +
data->debug_msg_count * data->T5_msg_size,
msg,
data->T5_msg_size);
data->debug_msg_count++;
} else {
dev_dbg(dev, "Discarding %u messages\n", data->debug_msg_count);
data->debug_msg_count = 0;
}
mutex_unlock(&data->debug_msg_lock);
sysfs_notify(&data->client->dev.kobj, NULL, "debug_notify");
}
static ssize_t mxt_debug_msg_write(struct file *filp, struct kobject *kobj,
struct bin_attribute *bin_attr, char *buf, loff_t off,
size_t count)
{
return -EIO;
}
static ssize_t mxt_debug_msg_read(struct file *filp, struct kobject *kobj,
struct bin_attribute *bin_attr, char *buf, loff_t off, size_t bytes)
{
struct device *dev = container_of(kobj, struct device, kobj);
struct mxt_data *data = dev_get_drvdata(dev);
int count;
size_t bytes_read;
if (!data->debug_msg_data) {
dev_err(dev, "No buffer!\n");
return 0;
}
count = bytes / data->T5_msg_size;
if (count > DEBUG_MSG_MAX)
count = DEBUG_MSG_MAX;
mutex_lock(&data->debug_msg_lock);
if (count > data->debug_msg_count)
count = data->debug_msg_count;
bytes_read = count * data->T5_msg_size;
memcpy(buf, data->debug_msg_data, bytes_read);
data->debug_msg_count = 0;
mutex_unlock(&data->debug_msg_lock);
return bytes_read;
}
static int mxt_debug_msg_init(struct mxt_data *data)
{
sysfs_bin_attr_init(&data->debug_msg_attr);
data->debug_msg_attr.attr.name = "debug_msg";
data->debug_msg_attr.attr.mode = 0666;
data->debug_msg_attr.read = mxt_debug_msg_read;
data->debug_msg_attr.write = mxt_debug_msg_write;
data->debug_msg_attr.size = data->T5_msg_size * DEBUG_MSG_MAX;
if (sysfs_create_bin_file(&data->client->dev.kobj,
&data->debug_msg_attr) < 0) {
dev_err(&data->client->dev, "Failed to create %s\n",
data->debug_msg_attr.attr.name);
return -EINVAL;
}
return 0;
}
static void mxt_debug_msg_remove(struct mxt_data *data)
{
if (data->debug_msg_attr.attr.name)
sysfs_remove_bin_file(&data->client->dev.kobj,
&data->debug_msg_attr);
}
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) {
return ret;
} 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, "%s: i2c recv failed (%d)\n",
__func__, 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, "%s: i2c send failed (%d)\n",
__func__, ret);
}
return ret;
}
static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry)
{
u8 appmode = data->client->addr;
u8 bootloader;
u8 family_id = data->info ? data->info->family_id : 0;
switch (appmode) {
case 0x4a:
case 0x4b:
/* Chips after 1664S use different scheme */
if (retry || 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, bool alt_address)
{
struct device *dev = &data->client->dev;
int error;
u8 buf[3];
bool crc_failure, extended_id;
error = mxt_lookup_bootloader_address(data, alt_address);
if (error)
return error;
/* Check bootloader status and version information */
error = mxt_bootloader_read(data, buf, sizeof(buf));
if (error)
return error;
crc_failure = (buf[0] & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL;
extended_id = buf[0] & MXT_BOOT_EXTENDED_ID;
dev_info(dev, "Found bootloader addr:%02x ID:%u%s%u%s\n",
data->bootloader_addr,
extended_id ? (buf[1] & MXT_BOOT_ID_MASK) : buf[0],
extended_id ? " version:" : "",
extended_id ? buf[2] : 0,
crc_failure ? ", APP_CRC_FAIL" : "");
return 0;
}
static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock);
static int mxt_write_firmware_frame(struct mxt_data *data, struct mxt_flash *f)
{
f->frame = (struct mxt_fw_frame *)(f->fw->data + f->pos);
/* Take account of CRC bytes */
f->frame_size = __be16_to_cpu(f->frame->size) + 2U;
/* Write one frame to device */
return mxt_bootloader_write(data, f->fw->data + f->pos,
f->frame_size);
}
static int mxt_check_bootloader(struct mxt_data *data)
{
struct device *dev = &data->client->dev;
struct mxt_flash *f = data->flash;
u8 state;
int ret;
/* Handle interrupt after download/flash process */
if (f->pos >= f->fw->size) {
complete(&f->flash_completion);
return 0;
}
ret = mxt_bootloader_read(data, &state, 1);
if (ret)
return ret;
/* Remove don't care bits */
if (state & ~MXT_BOOT_STATUS_MASK)
state &= ~MXT_BOOT_STATUS_MASK;
switch (state) {
case MXT_WAITING_BOOTLOAD_CMD:
dev_info(dev, "Unlocking bootloader\n");
ret = mxt_send_bootloader_cmd(data, true);
if (ret)
return ret;
break;
case MXT_WAITING_FRAME_DATA:
if ((f->previous != MXT_WAITING_BOOTLOAD_CMD)
&& (f->previous != MXT_FRAME_CRC_PASS)
&& (f->previous != MXT_FRAME_CRC_FAIL))
goto unexpected;
ret = mxt_write_firmware_frame(data, f);
if (ret)
return ret;
break;
case MXT_FRAME_CRC_CHECK:
if (f->previous != MXT_WAITING_FRAME_DATA)
goto unexpected;
break;
case MXT_FRAME_CRC_PASS:
if (f->previous != MXT_FRAME_CRC_CHECK)
goto unexpected;
/* Next frame */
f->retry = 0;
f->pos += f->frame_size;
f->count++;
if (f->pos >= f->fw->size)
dev_info(dev, "Sent %u frames, %zu bytes\n",
f->count, f->fw->size);
else if (f->count % 50 == 0)
dev_dbg(dev, "Sent %u frames, %lld/%zu bytes\n",
f->count, f->pos, f->fw->size);
break;
case MXT_FRAME_CRC_FAIL:
if (f->retry > 20) {
dev_err(dev, "Retry count exceeded\n");
return -EIO;
}
/* Back off by 20ms per retry */
dev_dbg(dev, "Bootloader frame CRC failure\n");
f->retry++;
msleep(f->retry * 20);
break;
default:
return -EINVAL;
}
f->previous = state;
/* Poll after 0.1s if no interrupt received */
schedule_delayed_work(&f->work, HZ / 10);
return 0;
unexpected:
dev_err(dev, "Unexpected state transition\n");
return -EINVAL;
}
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) {
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;
}
} else {
ret = 0;
}
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 reset */
if (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 int mxt_write_object(struct mxt_data *data,
u8 type, u8 offset, u8 val)
{
struct mxt_object *object;
u16 reg;
object = mxt_get_object(data, type);
if (!object || offset >= mxt_obj_size(object))
return -EINVAL;
reg = object->start_address;
return mxt_write_reg(data->client, reg + offset, val);
}
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;
int i;
for (i = 0; i < pdata->t19_num_keys; i++) {
if (pdata->t19_keymap[i] == KEY_RESERVED)
continue;
/* Active-low switch */
input_report_key(input, pdata->t19_keymap[i],
!(message[1] & BIT(i)));
}
}
static void mxt_input_sync(struct mxt_data *data)
{
if (data->input_dev) {
input_mt_report_pointer_emulation(data->input_dev,
data->pdata->t19_num_keys);
input_sync(data->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;
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' : '.',