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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Raydium touchscreen I2C driver.
*
* Copyright (C) 2012-2014, Raydium Semiconductor Corporation.
*
* Raydium reserves the right to make changes without further notice
* to the materials described herein. Raydium does not assume any
* liability arising out of the application described herein.
*
* Contact Raydium Semiconductor Corporation at www.rad-ic.com
*/
#include <linux/acpi.h>
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/pm_wakeirq.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/unaligned.h>
/* Slave I2C mode */
#define RM_BOOT_BLDR 0x02
#define RM_BOOT_MAIN 0x03
/* I2C bootoloader commands */
#define RM_CMD_BOOT_PAGE_WRT 0x0B /* send bl page write */
#define RM_CMD_BOOT_WRT 0x11 /* send bl write */
#define RM_CMD_BOOT_ACK 0x22 /* send ack*/
#define RM_CMD_BOOT_CHK 0x33 /* send data check */
#define RM_CMD_BOOT_READ 0x44 /* send wait bl data ready*/
#define RM_BOOT_RDY 0xFF /* bl data ready */
#define RM_BOOT_CMD_READHWID 0x0E /* read hwid */
/* I2C main commands */
#define RM_CMD_QUERY_BANK 0x2B
#define RM_CMD_DATA_BANK 0x4D
#define RM_CMD_ENTER_SLEEP 0x4E
#define RM_CMD_BANK_SWITCH 0xAA
#define RM_RESET_MSG_ADDR 0x40000004
#define RM_MAX_READ_SIZE 56
#define RM_PACKET_CRC_SIZE 2
/* Touch relative info */
#define RM_MAX_RETRIES 3
#define RM_RETRY_DELAY_MS 20
#define RM_MAX_TOUCH_NUM 10
#define RM_BOOT_DELAY_MS 100
/* Offsets in contact data */
#define RM_CONTACT_STATE_POS 0
#define RM_CONTACT_X_POS 1
#define RM_CONTACT_Y_POS 3
#define RM_CONTACT_PRESSURE_POS 5
#define RM_CONTACT_WIDTH_X_POS 6
#define RM_CONTACT_WIDTH_Y_POS 7
/* Bootloader relative info */
#define RM_BL_WRT_CMD_SIZE 3 /* bl flash wrt cmd size */
#define RM_BL_WRT_PKG_SIZE 32 /* bl wrt pkg size */
#define RM_BL_WRT_LEN (RM_BL_WRT_PKG_SIZE + RM_BL_WRT_CMD_SIZE)
#define RM_FW_PAGE_SIZE 128
#define RM_MAX_FW_RETRIES 30
#define RM_MAX_FW_SIZE 0xD000
#define RM_POWERON_DELAY_USEC 500
#define RM_RESET_DELAY_MSEC 50
enum raydium_bl_cmd {
BL_HEADER = 0,
BL_PAGE_STR,
BL_PKG_IDX,
BL_DATA_STR,
};
enum raydium_bl_ack {
RAYDIUM_ACK_NULL = 0,
RAYDIUM_WAIT_READY,
RAYDIUM_PATH_READY,
};
enum raydium_boot_mode {
RAYDIUM_TS_MAIN = 0,
RAYDIUM_TS_BLDR,
};
/* Response to RM_CMD_DATA_BANK request */
struct raydium_data_info {
__le32 data_bank_addr;
u8 pkg_size;
u8 tp_info_size;
};
struct raydium_info {
__le32 hw_ver; /*device version */
u8 main_ver;
u8 sub_ver;
__le16 ft_ver; /* test version */
u8 x_num;
u8 y_num;
__le16 x_max;
__le16 y_max;
u8 x_res; /* units/mm */
u8 y_res; /* units/mm */
};
/* struct raydium_data - represents state of Raydium touchscreen device */
struct raydium_data {
struct i2c_client *client;
struct input_dev *input;
struct regulator *avdd;
struct regulator *vccio;
struct gpio_desc *reset_gpio;
struct raydium_info info;
struct mutex sysfs_mutex;
u8 *report_data;
u32 data_bank_addr;
u8 report_size;
u8 contact_size;
u8 pkg_size;
enum raydium_boot_mode boot_mode;
};
/*
* Header to be sent for RM_CMD_BANK_SWITCH command. This is used by
* raydium_i2c_{read|send} below.
*/
struct __packed raydium_bank_switch_header {
u8 cmd;
__be32 be_addr;
};
static int raydium_i2c_xfer(struct i2c_client *client, u32 addr,
struct i2c_msg *xfer, size_t xfer_count)
{
int ret;
/*
* If address is greater than 255, then RM_CMD_BANK_SWITCH needs to be
* sent first. Else, skip the header i.e. xfer[0].
*/
int xfer_start_idx = (addr > 0xff) ? 0 : 1;
xfer_count -= xfer_start_idx;
ret = i2c_transfer(client->adapter, &xfer[xfer_start_idx], xfer_count);
if (likely(ret == xfer_count))
return 0;
return ret < 0 ? ret : -EIO;
}
static int raydium_i2c_send(struct i2c_client *client,
u32 addr, const void *data, size_t len)
{
int tries = 0;
int error;
u8 *tx_buf;
u8 reg_addr = addr & 0xff;
tx_buf = kmalloc(len + 1, GFP_KERNEL);
if (!tx_buf)
return -ENOMEM;
tx_buf[0] = reg_addr;
memcpy(tx_buf + 1, data, len);
do {
struct raydium_bank_switch_header header = {
.cmd = RM_CMD_BANK_SWITCH,
.be_addr = cpu_to_be32(addr),
};
/*
* Perform as a single i2c_transfer transaction to ensure that
* no other I2C transactions are initiated on the bus to any
* other device in between. Initiating transacations to other
* devices after RM_CMD_BANK_SWITCH is sent is known to cause
* issues. This is also why regmap infrastructure cannot be used
* for this driver. Regmap handles page(bank) switch and reads
* as separate i2c_transfer() operations. This can result in
* problems if the Raydium device is on a shared I2C bus.
*/
struct i2c_msg xfer[] = {
{
.addr = client->addr,
.len = sizeof(header),
.buf = (u8 *)&header,
},
{
.addr = client->addr,
.len = len + 1,
.buf = tx_buf,
},
};
error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
if (likely(!error))
goto out;
msleep(RM_RETRY_DELAY_MS);
} while (++tries < RM_MAX_RETRIES);
dev_err(&client->dev, "%s failed: %d\n", __func__, error);
out:
kfree(tx_buf);
return error;
}
static int raydium_i2c_read(struct i2c_client *client,
u32 addr, void *data, size_t len)
{
int error;
while (len) {
u8 reg_addr = addr & 0xff;
struct raydium_bank_switch_header header = {
.cmd = RM_CMD_BANK_SWITCH,
.be_addr = cpu_to_be32(addr),
};
size_t xfer_len = min_t(size_t, len, RM_MAX_READ_SIZE);
/*
* Perform as a single i2c_transfer transaction to ensure that
* no other I2C transactions are initiated on the bus to any
* other device in between. Initiating transacations to other
* devices after RM_CMD_BANK_SWITCH is sent is known to cause
* issues. This is also why regmap infrastructure cannot be used
* for this driver. Regmap handles page(bank) switch and writes
* as separate i2c_transfer() operations. This can result in
* problems if the Raydium device is on a shared I2C bus.
*/
struct i2c_msg xfer[] = {
{
.addr = client->addr,
.len = sizeof(header),
.buf = (u8 *)&header,
},
{
.addr = client->addr,
.len = 1,
.buf = ®_addr,
},
{
.addr = client->addr,
.len = xfer_len,
.buf = data,
.flags = I2C_M_RD,
}
};
error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
if (unlikely(error))
return error;
len -= xfer_len;
data += xfer_len;
addr += xfer_len;
}
return 0;
}
static int raydium_i2c_sw_reset(struct i2c_client *client)
{
const u8 soft_rst_cmd = 0x01;
int error;
error = raydium_i2c_send(client, RM_RESET_MSG_ADDR, &soft_rst_cmd,
sizeof(soft_rst_cmd));
if (error) {
dev_err(&client->dev, "software reset failed: %d\n", error);
return error;
}
msleep(RM_RESET_DELAY_MSEC);
return 0;
}
static int raydium_i2c_query_ts_bootloader_info(struct raydium_data *ts)
{
struct i2c_client *client = ts->client;
static const u8 get_hwid[] = { RM_BOOT_CMD_READHWID,
0x10, 0xc0, 0x01, 0x00, 0x04, 0x00 };
u8 rbuf[5] = { 0 };
u32 hw_ver;
int error;
error = raydium_i2c_send(client, RM_CMD_BOOT_WRT,
get_hwid, sizeof(get_hwid));
if (error) {
dev_err(&client->dev, "WRT HWID command failed: %d\n", error);
return error;
}
error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, rbuf, 1);
if (error) {
dev_err(&client->dev, "Ack HWID command failed: %d\n", error);
return error;
}
error = raydium_i2c_read(client, RM_CMD_BOOT_CHK, rbuf, sizeof(rbuf));
if (error) {
dev_err(&client->dev, "Read HWID command failed: %d (%4ph)\n",
error, rbuf + 1);
hw_ver = 0xffffffffUL;
} else {
hw_ver = get_unaligned_be32(rbuf + 1);
}
ts->info.hw_ver = cpu_to_le32(hw_ver);
ts->info.main_ver = 0xff;
ts->info.sub_ver = 0xff;
return error;
}
static int raydium_i2c_query_ts_info(struct raydium_data *ts)
{
struct i2c_client *client = ts->client;
struct raydium_data_info data_info;
__le32 query_bank_addr;
int error, retry_cnt;
for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
error = raydium_i2c_read(client, RM_CMD_DATA_BANK,
&data_info, sizeof(data_info));
if (error)
continue;
/*
* Warn user if we already allocated memory for reports and
* then the size changed (due to firmware update?) and keep
* old size instead.
*/
if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
dev_warn(&client->dev,
"report size changes, was: %d, new: %d\n",
ts->pkg_size, data_info.pkg_size);
} else {
ts->pkg_size = data_info.pkg_size;
ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE;
}
ts->contact_size = data_info.tp_info_size;
ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
dev_dbg(&client->dev,
"data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
ts->data_bank_addr, ts->report_size, ts->contact_size);
error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
&query_bank_addr,
sizeof(query_bank_addr));
if (error)
continue;
error = raydium_i2c_read(client, le32_to_cpu(query_bank_addr),
&ts->info, sizeof(ts->info));
if (error)
continue;
return 0;
}
dev_err(&client->dev, "failed to query device parameters: %d\n", error);
return error;
}
static int raydium_i2c_check_fw_status(struct raydium_data *ts)
{
struct i2c_client *client = ts->client;
static const u8 bl_ack = 0x62;
static const u8 main_ack = 0x66;
u8 buf[4];
int error;
error = raydium_i2c_read(client, RM_CMD_BOOT_READ, buf, sizeof(buf));
if (!error) {
if (buf[0] == bl_ack)
ts->boot_mode = RAYDIUM_TS_BLDR;
else if (buf[0] == main_ack)
ts->boot_mode = RAYDIUM_TS_MAIN;
return 0;
}
return error;
}
static int raydium_i2c_initialize(struct raydium_data *ts)
{
struct i2c_client *client = ts->client;
int error, retry_cnt;
for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
/* Wait for Hello packet */
msleep(RM_BOOT_DELAY_MS);
error = raydium_i2c_check_fw_status(ts);
if (error) {
dev_err(&client->dev,
"failed to read 'hello' packet: %d\n", error);
continue;
}
if (ts->boot_mode == RAYDIUM_TS_BLDR ||
ts->boot_mode == RAYDIUM_TS_MAIN) {
break;
}
}
if (error)
ts->boot_mode = RAYDIUM_TS_BLDR;
if (ts->boot_mode == RAYDIUM_TS_BLDR)
raydium_i2c_query_ts_bootloader_info(ts);
else
raydium_i2c_query_ts_info(ts);
return error;
}
static int raydium_i2c_bl_chk_state(struct i2c_client *client,
enum raydium_bl_ack state)
{
static const u8 ack_ok[] = { 0xFF, 0x39, 0x30, 0x30, 0x54 };
u8 rbuf[sizeof(ack_ok)];
u8 retry;
int error;
for (retry = 0; retry < RM_MAX_FW_RETRIES; retry++) {
switch (state) {
case RAYDIUM_ACK_NULL:
return 0;
case RAYDIUM_WAIT_READY:
error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
&rbuf[0], 1);
if (!error && rbuf[0] == RM_BOOT_RDY)
return 0;
break;
case RAYDIUM_PATH_READY:
error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
rbuf, sizeof(rbuf));
if (!error && !memcmp(rbuf, ack_ok, sizeof(ack_ok)))
return 0;
break;
default:
dev_err(&client->dev, "%s: invalid target state %d\n",
__func__, state);
return -EINVAL;
}
msleep(20);
}
return -ETIMEDOUT;
}
static int raydium_i2c_write_object(struct i2c_client *client,
const void *data, size_t len,
enum raydium_bl_ack state)
{
int error;
static const u8 cmd[] = { 0xFF, 0x39 };
error = raydium_i2c_send(client, RM_CMD_BOOT_WRT, data, len);
if (error) {
dev_err(&client->dev, "WRT obj command failed: %d\n",
error);
return error;
}
error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, cmd, sizeof(cmd));
if (error) {
dev_err(&client->dev, "Ack obj command failed: %d\n", error);
return error;
}
error = raydium_i2c_bl_chk_state(client, state);
if (error) {
dev_err(&client->dev, "BL check state failed: %d\n", error);
return error;
}
return 0;
}
static int raydium_i2c_boot_trigger(struct i2c_client *client)
{
static const u8 cmd[7][6] = {
{ 0x08, 0x0C, 0x09, 0x00, 0x50, 0xD7 },
{ 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
{ 0x08, 0x04, 0x09, 0x00, 0x50, 0x00 },
{ 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
{ 0x08, 0x0C, 0x09, 0x00, 0x50, 0x00 },
{ 0x06, 0x01, 0x00, 0x00, 0x00, 0x00 },
{ 0x02, 0xA2, 0x00, 0x00, 0x00, 0x00 },
};
int i;
int error;
for (i = 0; i < 7; i++) {
error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
RAYDIUM_WAIT_READY);
if (error) {
dev_err(&client->dev,
"boot trigger failed at step %d: %d\n",
i, error);
return error;
}
}
return 0;
}
static int raydium_i2c_fw_trigger(struct i2c_client *client)
{
static const u8 cmd[5][11] = {
{ 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0xD7, 0, 0, 0 },
{ 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
{ 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
{ 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
{ 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
};
int i;
int error;
for (i = 0; i < 5; i++) {
error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
RAYDIUM_ACK_NULL);
if (error) {
dev_err(&client->dev,
"fw trigger failed at step %d: %d\n",
i, error);
return error;
}
}
return 0;
}
static int raydium_i2c_check_path(struct i2c_client *client)
{
static const u8 cmd[] = { 0x09, 0x00, 0x09, 0x00, 0x50, 0x10, 0x00 };
int error;
error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
RAYDIUM_PATH_READY);
if (error) {
dev_err(&client->dev, "check path command failed: %d\n", error);
return error;
}
return 0;
}
static int raydium_i2c_enter_bl(struct i2c_client *client)
{
static const u8 cal_cmd[] = { 0x00, 0x01, 0x52 };
int error;
error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
RAYDIUM_ACK_NULL);
if (error) {
dev_err(&client->dev, "enter bl command failed: %d\n", error);
return error;
}
msleep(RM_BOOT_DELAY_MS);
return 0;
}
static int raydium_i2c_leave_bl(struct i2c_client *client)
{
static const u8 leave_cmd[] = { 0x05, 0x00 };
int error;
error = raydium_i2c_write_object(client, leave_cmd, sizeof(leave_cmd),
RAYDIUM_ACK_NULL);
if (error) {
dev_err(&client->dev, "leave bl command failed: %d\n", error);
return error;
}
msleep(RM_BOOT_DELAY_MS);
return 0;
}
static int raydium_i2c_write_checksum(struct i2c_client *client,
size_t length, u16 checksum)
{
u8 checksum_cmd[] = { 0x00, 0x05, 0x6D, 0x00, 0x00, 0x00, 0x00 };
int error;
put_unaligned_le16(length, &checksum_cmd[3]);
put_unaligned_le16(checksum, &checksum_cmd[5]);
error = raydium_i2c_write_object(client,
checksum_cmd, sizeof(checksum_cmd),
RAYDIUM_ACK_NULL);
if (error) {
dev_err(&client->dev, "failed to write checksum: %d\n",
error);
return error;
}
return 0;
}
static int raydium_i2c_disable_watch_dog(struct i2c_client *client)
{
static const u8 cmd[] = { 0x0A, 0xAA };
int error;
error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
RAYDIUM_WAIT_READY);
if (error) {
dev_err(&client->dev, "disable watchdog command failed: %d\n",
error);
return error;
}
return 0;
}
static int raydium_i2c_fw_write_page(struct i2c_client *client,
u16 page_idx, const void *data, size_t len)
{
u8 buf[RM_BL_WRT_LEN];
size_t xfer_len;
int error;
int i;
BUILD_BUG_ON((RM_FW_PAGE_SIZE % RM_BL_WRT_PKG_SIZE) != 0);
for (i = 0; i < RM_FW_PAGE_SIZE / RM_BL_WRT_PKG_SIZE; i++) {
buf[BL_HEADER] = RM_CMD_BOOT_PAGE_WRT;
buf[BL_PAGE_STR] = page_idx ? 0xff : 0;
buf[BL_PKG_IDX] = i + 1;
xfer_len = min_t(size_t, len, RM_BL_WRT_PKG_SIZE);
memcpy(&buf[BL_DATA_STR], data, xfer_len);
if (len < RM_BL_WRT_PKG_SIZE)
memset(&buf[BL_DATA_STR + xfer_len], 0xff,
RM_BL_WRT_PKG_SIZE - xfer_len);
error = raydium_i2c_write_object(client, buf, RM_BL_WRT_LEN,
RAYDIUM_WAIT_READY);
if (error) {
dev_err(&client->dev,
"page write command failed for page %d, chunk %d: %d\n",
page_idx, i, error);
return error;
}
data += xfer_len;
len -= xfer_len;
}
return error;
}
static u16 raydium_calc_chksum(const u8 *buf, u16 len)
{
u16 checksum = 0;
u16 i;
for (i = 0; i < len; i++)
checksum += buf[i];
return checksum;
}
static int raydium_i2c_do_update_firmware(struct raydium_data *ts,
const struct firmware *fw)
{
struct i2c_client *client = ts->client;
const void *data;
size_t data_len;
size_t len;
int page_nr;
int i;
int error;
u16 fw_checksum;
if (fw->size == 0 || fw->size > RM_MAX_FW_SIZE) {
dev_err(&client->dev, "Invalid firmware length\n");
return -EINVAL;
}
error = raydium_i2c_check_fw_status(ts);
if (error) {
dev_err(&client->dev, "Unable to access IC %d\n", error);
return error;
}
if (ts->boot_mode == RAYDIUM_TS_MAIN) {
for (i = 0; i < RM_MAX_RETRIES; i++) {
error = raydium_i2c_enter_bl(client);
if (!error) {
error = raydium_i2c_check_fw_status(ts);
if (error) {
dev_err(&client->dev,
"unable to access IC: %d\n",
error);
return error;
}
if (ts->boot_mode == RAYDIUM_TS_BLDR)
break;
}
}
if (ts->boot_mode == RAYDIUM_TS_MAIN) {
dev_err(&client->dev,
"failed to jump to boot loader: %d\n",
error);
return -EIO;
}
}
error = raydium_i2c_disable_watch_dog(client);
if (error)
return error;
error = raydium_i2c_check_path(client);
if (error)
return error;
error = raydium_i2c_boot_trigger(client);
if (error) {
dev_err(&client->dev, "send boot trigger fail: %d\n", error);
return error;
}
msleep(RM_BOOT_DELAY_MS);
data = fw->data;
data_len = fw->size;
page_nr = 0;
while (data_len) {
len = min_t(size_t, data_len, RM_FW_PAGE_SIZE);
error = raydium_i2c_fw_write_page(client, page_nr++, data, len);
if (error)
return error;
msleep(20);
data += len;
data_len -= len;
}
error = raydium_i2c_leave_bl(client);
if (error) {
dev_err(&client->dev,
"failed to leave boot loader: %d\n", error);
return error;
}
dev_dbg(&client->dev, "left boot loader mode\n");
msleep(RM_BOOT_DELAY_MS);
error = raydium_i2c_check_fw_status(ts);
if (error) {
dev_err(&client->dev,
"failed to check fw status after write: %d\n",
error);
return error;
}
if (ts->boot_mode != RAYDIUM_TS_MAIN) {
dev_err(&client->dev,
"failed to switch to main fw after writing firmware: %d\n",
error);
return -EINVAL;
}
error = raydium_i2c_fw_trigger(client);
if (error) {
dev_err(&client->dev, "failed to trigger fw: %d\n", error);
return error;
}
fw_checksum = raydium_calc_chksum(fw->data, fw->size);
error = raydium_i2c_write_checksum(client, fw->size, fw_checksum);
if (error)
return error;
return 0;
}
static int raydium_i2c_fw_update(struct raydium_data *ts)
{
struct i2c_client *client = ts->client;
const struct firmware *fw = NULL;
char *fw_file;
int error;
fw_file = kasprintf(GFP_KERNEL, "raydium_%#04x.fw",
le32_to_cpu(ts->info.hw_ver));
if (!fw_file)
return -ENOMEM;
dev_dbg(&client->dev, "firmware name: %s\n", fw_file);
error = request_firmware(&fw, fw_file, &client->dev);
if (error) {
dev_err(&client->dev, "Unable to open firmware %s\n", fw_file);
goto out_free_fw_file;
}
disable_irq(client->irq);
error = raydium_i2c_do_update_firmware(ts, fw);
if (error) {
dev_err(&client->dev, "firmware update failed: %d\n", error);
ts->boot_mode = RAYDIUM_TS_BLDR;
goto out_enable_irq;
}
error = raydium_i2c_initialize(ts);
if (error) {
dev_err(&client->dev,
"failed to initialize device after firmware update: %d\n",
error);
ts->boot_mode = RAYDIUM_TS_BLDR;
goto out_enable_irq;
}
ts->boot_mode = RAYDIUM_TS_MAIN;
out_enable_irq:
enable_irq(client->irq);
msleep(100);
release_firmware(fw);
out_free_fw_file:
kfree(fw_file);
return error;
}
static void raydium_mt_event(struct raydium_data *ts)
{
int i;
for (i = 0; i < ts->report_size / ts->contact_size; i++) {
u8 *contact = &ts->report_data[ts->contact_size * i];
bool state = contact[RM_CONTACT_STATE_POS];
u8 wx, wy;
input_mt_slot(ts->input, i);
input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, state);
if (!state)
continue;
input_report_abs(ts->input, ABS_MT_POSITION_X,
get_unaligned_le16(&contact[RM_CONTACT_X_POS]));
input_report_abs(ts->input, ABS_MT_POSITION_Y,
get_unaligned_le16(&contact[RM_CONTACT_Y_POS]));
input_report_abs(ts->input, ABS_MT_PRESSURE,
contact[RM_CONTACT_PRESSURE_POS]);
wx = contact[RM_CONTACT_WIDTH_X_POS];
wy = contact[RM_CONTACT_WIDTH_Y_POS];
input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, max(wx, wy));
input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, min(wx, wy));
}
input_mt_sync_frame(ts->input);
input_sync(ts->input);
}
static irqreturn_t raydium_i2c_irq(int irq, void *_dev)
{
struct raydium_data *ts = _dev;
int error;
u16 fw_crc;
u16 calc_crc;
if (ts->boot_mode != RAYDIUM_TS_MAIN)
goto out;
error = raydium_i2c_read(ts->client, ts->data_bank_addr,
ts->report_data, ts->pkg_size);
if (error)
goto out;
fw_crc = get_unaligned_le16(&ts->report_data[ts->report_size]);
calc_crc = raydium_calc_chksum(ts->report_data, ts->report_size);
if (unlikely(fw_crc != calc_crc)) {
dev_warn(&ts->client->dev,
"%s: invalid packet crc %#04x vs %#04x\n",
__func__, calc_crc, fw_crc);
goto out;
}
raydium_mt_event(ts);
out:
return IRQ_HANDLED;
}
static ssize_t raydium_i2c_fw_ver_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
struct raydium_data *ts = i2c_get_clientdata(client);
return sprintf(buf, "%d.%d\n", ts->info.main_ver, ts->info.sub_ver);
}
static ssize_t raydium_i2c_hw_ver_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
struct raydium_data *ts = i2c_get_clientdata(client);
return sprintf(buf, "%#04x\n", le32_to_cpu(ts->info.hw_ver));
}
static ssize_t raydium_i2c_boot_mode_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
struct raydium_data *ts = i2c_get_clientdata(client);
return sprintf(buf, "%s\n",
ts->boot_mode == RAYDIUM_TS_MAIN ?
"Normal" : "Recovery");
}
static ssize_t raydium_i2c_update_fw_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct i2c_client *client = to_i2c_client(dev);
struct raydium_data *ts = i2c_get_clientdata(client);
int error;
error = mutex_lock_interruptible(&ts->sysfs_mutex);
if (error)
return error;
error = raydium_i2c_fw_update(ts);
mutex_unlock(&ts->sysfs_mutex);
return error ?: count;
}
static ssize_t raydium_i2c_calibrate_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct i2c_client *client = to_i2c_client(dev);
struct raydium_data *ts = i2c_get_clientdata(client);
static const u8 cal_cmd[] = { 0x00, 0x01, 0x9E };
int error;
error = mutex_lock_interruptible(&ts->sysfs_mutex);
if (error)
return error;
error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
RAYDIUM_WAIT_READY);
if (error)
dev_err(&client->dev, "calibrate command failed: %d\n", error);
mutex_unlock(&ts->sysfs_mutex);
return error ?: count;
}
static DEVICE_ATTR(fw_version, S_IRUGO, raydium_i2c_fw_ver_show, NULL);
static DEVICE_ATTR(hw_version, S_IRUGO, raydium_i2c_hw_ver_show, NULL);
static DEVICE_ATTR(boot_mode, S_IRUGO, raydium_i2c_boot_mode_show, NULL);
static DEVICE_ATTR(update_fw, S_IWUSR, NULL, raydium_i2c_update_fw_store);
static DEVICE_ATTR(calibrate, S_IWUSR, NULL, raydium_i2c_calibrate_store);
static struct attribute *raydium_i2c_attrs[] = {
&dev_attr_update_fw.attr,
&dev_attr_boot_mode.attr,
&dev_attr_fw_version.attr,
&dev_attr_hw_version.attr,
&dev_attr_calibrate.attr,
NULL
};
ATTRIBUTE_GROUPS(raydium_i2c);
static int raydium_i2c_power_on(struct raydium_data *ts)
{
int error;
if (!ts->reset_gpio)
return 0;
gpiod_set_value_cansleep(ts->reset_gpio, 1);
error = regulator_enable(ts->avdd);
if (error) {
dev_err(&ts->client->dev,
"failed to enable avdd regulator: %d\n", error);
goto release_reset_gpio;
}
error = regulator_enable(ts->vccio);
if (error) {
regulator_disable(ts->avdd);
dev_err(&ts->client->dev,
"failed to enable vccio regulator: %d\n", error);
goto release_reset_gpio;
}
udelay(RM_POWERON_DELAY_USEC);
release_reset_gpio:
gpiod_set_value_cansleep(ts->reset_gpio, 0);
if (error)
return error;
msleep(RM_RESET_DELAY_MSEC);
return 0;
}
static void raydium_i2c_power_off(void *_data)
{
struct raydium_data *ts = _data;
if (ts->reset_gpio) {
gpiod_set_value_cansleep(ts->reset_gpio, 1);
regulator_disable(ts->vccio);
regulator_disable(ts->avdd);
}
}
static int raydium_i2c_probe(struct i2c_client *client)
{
union i2c_smbus_data dummy;
struct raydium_data *ts;
int error;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
dev_err(&client->dev,
"i2c check functionality error (need I2C_FUNC_I2C)\n");
return -ENXIO;
}
ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
if (!ts)
return -ENOMEM;
mutex_init(&ts->sysfs_mutex);
ts->client = client;
i2c_set_clientdata(client, ts);
ts->avdd = devm_regulator_get(&client->dev, "avdd");
if (IS_ERR(ts->avdd))
return dev_err_probe(&client->dev, PTR_ERR(ts->avdd),
"Failed to get 'avdd' regulator\n");
ts->vccio = devm_regulator_get(&client->dev, "vccio");
if (IS_ERR(ts->vccio))
return dev_err_probe(&client->dev, PTR_ERR(ts->vccio),
"Failed to get 'vccio' regulator\n");
ts->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
GPIOD_OUT_LOW);
if (IS_ERR(ts->reset_gpio))
return dev_err_probe(&client->dev, PTR_ERR(ts->reset_gpio),
"Failed to get reset gpio\n");
error = raydium_i2c_power_on(ts);
if (error)
return error;
error = devm_add_action_or_reset(&client->dev,
raydium_i2c_power_off, ts);
if (error) {
dev_err(&client->dev,
"failed to install power off action: %d\n", error);
return error;
}
/* Make sure there is something at this address */
if (i2c_smbus_xfer(client->adapter, client->addr, 0,
I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
dev_err(&client->dev, "nothing at this address\n");
return -ENXIO;
}
error = raydium_i2c_initialize(ts);
if (error) {
dev_err(&client->dev, "failed to initialize: %d\n", error);
return error;
}
ts->report_data = devm_kmalloc(&client->dev,
ts->pkg_size, GFP_KERNEL);
if (!ts->report_data)
return -ENOMEM;
ts->input = devm_input_allocate_device(&client->dev);
if (!ts->input) {
dev_err(&client->dev, "Failed to allocate input device\n");
return -ENOMEM;
}
ts->input->name = "Raydium Touchscreen";
ts->input->id.bustype = BUS_I2C;
input_set_abs_params(ts->input, ABS_MT_POSITION_X,
0, le16_to_cpu(ts->info.x_max), 0, 0);
input_set_abs_params(ts->input, ABS_MT_POSITION_Y,
0, le16_to_cpu(ts->info.y_max), 0, 0);
input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->info.x_res);
input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->info.y_res);
input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
error = input_mt_init_slots(ts->input, RM_MAX_TOUCH_NUM,
INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
if (error) {
dev_err(&client->dev,
"failed to initialize MT slots: %d\n", error);
return error;
}
error = input_register_device(ts->input);
if (error) {
dev_err(&client->dev,
"unable to register input device: %d\n", error);
return error;
}
error = devm_request_threaded_irq(&client->dev, client->irq,
NULL, raydium_i2c_irq,
IRQF_ONESHOT, client->name, ts);
if (error) {
dev_err(&client->dev, "Failed to register interrupt\n");
return error;
}
return 0;
}
static void raydium_enter_sleep(struct i2c_client *client)
{
static const u8 sleep_cmd[] = { 0x5A, 0xff, 0x00, 0x0f };
int error;
error = raydium_i2c_send(client, RM_CMD_ENTER_SLEEP,
sleep_cmd, sizeof(sleep_cmd));
if (error)
dev_err(&client->dev,
"sleep command failed: %d\n", error);
}
static int raydium_i2c_suspend(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);
struct raydium_data *ts = i2c_get_clientdata(client);
/* Sleep is not available in BLDR recovery mode */
if (ts->boot_mode != RAYDIUM_TS_MAIN)
return -EBUSY;
disable_irq(client->irq);
if (device_may_wakeup(dev)) {
raydium_enter_sleep(client);
} else {
raydium_i2c_power_off(ts);
}
return 0;
}
static int raydium_i2c_resume(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);
struct raydium_data *ts = i2c_get_clientdata(client);
if (device_may_wakeup(dev)) {
raydium_i2c_sw_reset(client);
} else {
raydium_i2c_power_on(ts);
raydium_i2c_initialize(ts);
}
enable_irq(client->irq);
return 0;
}
static DEFINE_SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops,
raydium_i2c_suspend, raydium_i2c_resume);
static const struct i2c_device_id raydium_i2c_id[] = {
{ "raydium_i2c" },
{ "rm32380" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(i2c, raydium_i2c_id);
#ifdef CONFIG_ACPI
static const struct acpi_device_id raydium_acpi_id[] = {
{ "RAYD0001", 0 },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(acpi, raydium_acpi_id);
#endif
#ifdef CONFIG_OF
static const struct of_device_id raydium_of_match[] = {
{ .compatible = "raydium,rm32380", },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, raydium_of_match);
#endif
static struct i2c_driver raydium_i2c_driver = {
.probe = raydium_i2c_probe,
.id_table = raydium_i2c_id,
.driver = {
.name = "raydium_ts",
.dev_groups = raydium_i2c_groups,
.pm = pm_sleep_ptr(&raydium_i2c_pm_ops),
.acpi_match_table = ACPI_PTR(raydium_acpi_id),
.of_match_table = of_match_ptr(raydium_of_match),
},
};
module_i2c_driver(raydium_i2c_driver);
MODULE_AUTHOR("Raydium");
MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
MODULE_LICENSE("GPL v2");
|