/
joycon-serdev.c
3743 lines (3279 loc) · 113 KB
/
joycon-serdev.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
// SPDX-License-Identifier: GPL-2.0+
/*
* serial driver for Nintendo Switch Joy-Cons
*
* Copyright (c) 2019-2020 Daniel J. Ogorchock <djogorchock@gmail.com>
* Copyright (c) 2021-2023 CTCaer <ctcaer@gmail.com>
*
* The following resources/projects were referenced for this driver:
* https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering
* https://gitlab.com/pjranki/joycon-linux-kernel (Peter Rankin)
* https://github.com/FrotBot/SwitchProConLinuxUSB
* https://github.com/MTCKC/ProconXInput
* https://github.com/Davidobot/BetterJoyForCemu
* hid-wiimote kernel hid driver
* hid-logitech-hidpp driver
* hid-sony driver
*
* This driver supports the Nintendo Switch Joy-Cons and Lite gamepad over uart.
*
* Much of this driver is ported from the hid-nintendo usb/bt joy-con driver.
*
* Large portions are based on Max Thomas' joycon.c serdev driver:
* Copyright (c) 2018 Max Thomas
*
*/
#include <asm/unaligned.h>
#include <linux/clk.h>
#include <linux/crc8.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/gpio.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/jiffies.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/power_supply.h>
#include <linux/regulator/consumer.h>
#include <linux/serdev.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/tty.h>
/*
* Reference the url below for the following protocol defines:
* https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering
*/
#define DETECT_PIN_SETTLE_TIME_MS 32
/* UART header commands */
static const u8 JC_CMD_EXTSEND = 0x91;
static const u8 JC_CMD_EXTRET = 0x92;
static const u8 JC_CMD_INITRET = 0x94;
static const u8 JC_CMD_HANDSHAKE = 0xA5;
static const u8 JC_CMD_HORIINPUTREPORT = 0x9A;
/* Used in handshake */
static const u8 JC_INIT_MAC = 0x01;
static const u8 JC_INIT_HID_CONNECT = 0x10;
static const u8 JC_INIT_HID_DISCONNECT = 0x11;
static const u8 JC_INIT_SET_HID_RATE = 0x12;
static const u8 JC_INIT_SET_BAUDRATE = 0x20;
/* Input Reports */
static const u8 JC_INPUT_EXT_ACK = 0x1F;
static const u8 JC_INPUT_BUTTON_EVENT = 0x3F;
static const u8 JC_INPUT_SUBCMD_REPLY = 0x21;
static const u8 JC_INPUT_IMU_DATA = 0x30;
static const u8 JC_INPUT_MCU_DATA = 0x31;
static const u8 JC_INPUT_USB_RESPONSE = 0x81;
/* Output Reports */
static const u8 JC_OUTPUT_RUMBLE_AND_SUBCMD = 0x01;
static const u8 JC_OUTPUT_FW_UPDATE_PKT = 0x03;
static const u8 JC_OUTPUT_RUMBLE_ONLY = 0x10;
static const u8 JC_OUTPUT_MCU_DATA = 0x11;
/* Subcommand IDs */
static const u8 JC_SUBCMD_STATE = 0x00;
static const u8 JC_SUBCMD_MANUAL_BT_PAIRING = 0x01;
static const u8 JC_SUBCMD_REQ_DEV_INFO = 0x02;
static const u8 JC_SUBCMD_SET_REPORT_MODE = 0x03;
static const u8 JC_SUBCMD_TRIGGERS_ELAPSED = 0x04;
static const u8 JC_SUBCMD_GET_PAGE_LIST_STATE = 0x05;
static const u8 JC_SUBCMD_SET_HCI_STATE = 0x06;
static const u8 JC_SUBCMD_RESET_PAIRING_INFO = 0x07;
static const u8 JC_SUBCMD_LOW_POWER_MODE = 0x08;
static const u8 JC_SUBCMD_SPI_FLASH_READ = 0x10;
static const u8 JC_SUBCMD_SPI_FLASH_WRITE = 0x11;
static const u8 JC_SUBCMD_REQUEST_REPORT = 0x1F;
static const u8 JC_SUBCMD_RESET_MCU = 0x20;
static const u8 JC_SUBCMD_SET_MCU_CONFIG = 0x21;
static const u8 JC_SUBCMD_SET_MCU_STATE = 0x22;
static const u8 JC_SUBCMD_SET_PLAYER_LIGHTS = 0x30;
static const u8 JC_SUBCMD_GET_PLAYER_LIGHTS = 0x31;
static const u8 JC_SUBCMD_SET_HOME_LIGHT = 0x38;
static const u8 JC_SUBCMD_ENABLE_IMU = 0x40;
static const u8 JC_SUBCMD_SET_IMU_SENSITIVITY = 0x41;
static const u8 JC_SUBCMD_WRITE_IMU_REG = 0x42;
static const u8 JC_SUBCMD_READ_IMU_REG = 0x43;
static const u8 JC_SUBCMD_ENABLE_VIBRATION = 0x48;
static const u8 JC_SUBCMD_GET_REGULATED_VOLTAGE = 0x50;
/* Sio uart header commands */
static const u8 JC_SIO_OUTPUT_RPT = 0x91;
static const u8 JC_SIO_INPUT_RPT = 0x92;
static const u8 JC_SIO_CMD_ACK = 0x80;
static const u8 JC_SIO_STATUS_OK = 0x00;
/* Sio CMDs */
static const u8 JC_SIO_CMD_INIT = 0x01;
static const u8 JC_SIO_CMD_VER_RPT = 0x03;
static const u8 JC_SIO_CMD_INPUTREPORT = 0x41;
static const u8 JC_SIO_CMD_IAP_VER = 0x42;
/* Magic value denoting presence of user calibration */
static const u16 JC_CAL_USR_MAGIC_0 = 0xB2;
static const u16 JC_CAL_USR_MAGIC_1 = 0xA1;
static const u8 JC_CAL_USR_MAGIC_SIZE = 2;
/* SPI storage addresses of user calibration data */
static const u16 JC_CAL_USR_LEFT_MAGIC_ADDR = 0x8010;
static const u16 JC_CAL_USR_LEFT_DATA_ADDR = 0x8012;
static const u16 JC_CAL_USR_LEFT_DATA_END = 0x801A;
static const u16 JC_CAL_USR_RIGHT_MAGIC_ADDR = 0x801B;
static const u16 JC_CAL_USR_RIGHT_DATA_ADDR = 0x801D;
#define JC_CAL_STICK_DATA_SIZE \
(JC_CAL_USR_LEFT_DATA_END - JC_CAL_USR_LEFT_DATA_ADDR + 1)
/* SPI storage addresses of factory calibration data */
static const u16 JC_CAL_FCT_DATA_LEFT_ADDR = 0x603d;
static const u16 JC_CAL_FCT_DATA_RIGHT_ADDR = 0x6046;
/* The raw analog joystick values will be mapped in terms of this magnitude */
static const u16 JC_MAX_STICK_MAG = 32767;
static const u16 JC_STICK_FUZZ = 250;
static const u16 JC_STICK_FLAT = 1500;
/* Under most circumstances IMU reports are pushed every 15ms; use as default */
static const u16 JC_IMU_DFLT_AVG_DELTA_MS = 15;
/* How many samples to sum before calculating average IMU report delta */
static const u16 JC_IMU_SAMPLES_PER_DELTA_AVG = 300;
/* Controls how many dropped IMU packets at once trigger a warning message */
static const u16 JC_IMU_DROPPED_PKT_WARNING = 3;
/*
* The controller's accelerometer has a sensor resolution of 16bits and is
* configured with a range of +-8000 milliGs. Therefore, the resolution can be
* calculated thus: (2^16-1)/(8000 * 2) = 4.096 digits per milliG
* Resolution per G (rather than per millliG): 4.096 * 1000 = 4096 digits per G
* Alternatively: 1/4096 = .0002441 Gs per digit
*/
static const s32 JC_IMU_MAX_ACCEL_MAG = 32767;
static const u16 JC_IMU_ACCEL_RES_PER_G = 4096;
static const u16 JC_IMU_ACCEL_FUZZ = 10;
static const u16 JC_IMU_ACCEL_FLAT /*= 0*/;
/*
* The controller's gyroscope has a sensor resolution of 16bits and is
* configured with a range of +-2000 degrees/second.
* Digits per dps: (2^16 -1)/(2000*2) = 16.38375
* dps per digit: 16.38375E-1 = .0610
*
* STMicro recommends in the datasheet to add 15% to the dps/digit. This allows
* the full sensitivity range to be saturated without clipping. This yields more
* accurate results, so it's the technique this driver uses.
* dps per digit (corrected): .0610 * 1.15 = .0702
* digits per dps (corrected): .0702E-1 = 14.247
*
* Now, 14.247 truncating to 14 loses a lot of precision, so we rescale the
* min/max range by 1000.
*/
static const s32 JC_IMU_PREC_RANGE_SCALE = 1000;
/* Note: change mag and res_per_dps if prec_range_scale is ever altered */
static const s32 JC_IMU_MAX_GYRO_MAG = 32767000; /* (2^16-1)*1000 */
static const u16 JC_IMU_GYRO_RES_PER_DPS = 14247; /* (14.247*1000) */
static const u16 JC_IMU_GYRO_FUZZ = 10;
static const u16 JC_IMU_GYRO_FLAT /*= 0*/;
/* Frequency/amplitude defines and tables for rumble */
static const u32 JC_RUMBLE_MASK = 0xFF80FE00;
static const u32 JC_RUMBLE_ZERO_AMP = 0x40000000;
#define JC_VIB_RPT_GET_BUF_STS_RIGHT(val) ((val) & 7)
#define JC_VIB_RPT_GET_BUF_STS_LEFT(val) (((val) >> 4) & 7)
struct joycon_rumble_freq_data {
u16 high;
u8 low;
u16 freq; /* Hz*/
};
struct joycon_rumble_amp_data {
u8 high;
u16 low;
u16 amp;
};
/*
* These tables are from
* https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/rumble_data_table.md
*/
static const struct joycon_rumble_freq_data joycon_rumble_frequencies[] = {
/* high, low, freq */
{ 0x0000, 0x01, 41 }, { 0x0000, 0x02, 42 }, { 0x0000, 0x03, 43 },
{ 0x0000, 0x04, 44 }, { 0x0000, 0x05, 45 }, { 0x0000, 0x06, 46 },
{ 0x0000, 0x07, 47 }, { 0x0000, 0x08, 48 }, { 0x0000, 0x09, 49 },
{ 0x0000, 0x0A, 50 }, { 0x0000, 0x0B, 51 }, { 0x0000, 0x0C, 52 },
{ 0x0000, 0x0D, 53 }, { 0x0000, 0x0E, 54 }, { 0x0000, 0x0F, 55 },
{ 0x0000, 0x10, 57 }, { 0x0000, 0x11, 58 }, { 0x0000, 0x12, 59 },
{ 0x0000, 0x13, 60 }, { 0x0000, 0x14, 62 }, { 0x0000, 0x15, 63 },
{ 0x0000, 0x16, 64 }, { 0x0000, 0x17, 66 }, { 0x0000, 0x18, 67 },
{ 0x0000, 0x19, 69 }, { 0x0000, 0x1A, 70 }, { 0x0000, 0x1B, 72 },
{ 0x0000, 0x1C, 73 }, { 0x0000, 0x1D, 75 }, { 0x0000, 0x1e, 77 },
{ 0x0000, 0x1f, 78 }, { 0x0000, 0x20, 80 }, { 0x0400, 0x21, 82 },
{ 0x0800, 0x22, 84 }, { 0x0c00, 0x23, 85 }, { 0x1000, 0x24, 87 },
{ 0x1400, 0x25, 89 }, { 0x1800, 0x26, 91 }, { 0x1c00, 0x27, 93 },
{ 0x2000, 0x28, 95 }, { 0x2400, 0x29, 97 }, { 0x2800, 0x2a, 99 },
{ 0x2c00, 0x2b, 102 }, { 0x3000, 0x2c, 104 }, { 0x3400, 0x2d, 106 },
{ 0x3800, 0x2e, 108 }, { 0x3c00, 0x2f, 111 }, { 0x4000, 0x30, 113 },
{ 0x4400, 0x31, 116 }, { 0x4800, 0x32, 118 }, { 0x4c00, 0x33, 121 },
{ 0x5000, 0x34, 123 }, { 0x5400, 0x35, 126 }, { 0x5800, 0x36, 129 },
{ 0x5c00, 0x37, 132 }, { 0x6000, 0x38, 135 }, { 0x6400, 0x39, 137 },
{ 0x6800, 0x3a, 141 }, { 0x6c00, 0x3b, 144 }, { 0x7000, 0x3c, 147 },
{ 0x7400, 0x3d, 150 }, { 0x7800, 0x3e, 153 }, { 0x7c00, 0x3f, 157 },
{ 0x8000, 0x40, 160 }, { 0x8400, 0x41, 164 }, { 0x8800, 0x42, 167 },
{ 0x8c00, 0x43, 171 }, { 0x9000, 0x44, 174 }, { 0x9400, 0x45, 178 },
{ 0x9800, 0x46, 182 }, { 0x9c00, 0x47, 186 }, { 0xa000, 0x48, 190 },
{ 0xa400, 0x49, 194 }, { 0xa800, 0x4a, 199 }, { 0xac00, 0x4b, 203 },
{ 0xb000, 0x4c, 207 }, { 0xb400, 0x4d, 212 }, { 0xb800, 0x4e, 217 },
{ 0xbc00, 0x4f, 221 }, { 0xc000, 0x50, 226 }, { 0xc400, 0x51, 231 },
{ 0xc800, 0x52, 236 }, { 0xcc00, 0x53, 241 }, { 0xd000, 0x54, 247 },
{ 0xd400, 0x55, 252 }, { 0xd800, 0x56, 258 }, { 0xdc00, 0x57, 263 },
{ 0xe000, 0x58, 269 }, { 0xe400, 0x59, 275 }, { 0xe800, 0x5a, 281 },
{ 0xec00, 0x5b, 287 }, { 0xf000, 0x5c, 293 }, { 0xf400, 0x5d, 300 },
{ 0xf800, 0x5e, 306 }, { 0xfc00, 0x5f, 313 }, { 0x0001, 0x60, 320 },
{ 0x0401, 0x61, 327 }, { 0x0801, 0x62, 334 }, { 0x0c01, 0x63, 341 },
{ 0x1001, 0x64, 349 }, { 0x1401, 0x65, 357 }, { 0x1801, 0x66, 364 },
{ 0x1c01, 0x67, 372 }, { 0x2001, 0x68, 381 }, { 0x2401, 0x69, 389 },
{ 0x2801, 0x6a, 397 }, { 0x2c01, 0x6b, 406 }, { 0x3001, 0x6c, 415 },
{ 0x3401, 0x6d, 424 }, { 0x3801, 0x6e, 433 }, { 0x3c01, 0x6f, 443 },
{ 0x4001, 0x70, 453 }, { 0x4401, 0x71, 462 }, { 0x4801, 0x72, 473 },
{ 0x4c01, 0x73, 483 }, { 0x5001, 0x74, 494 }, { 0x5401, 0x75, 504 },
{ 0x5801, 0x76, 515 }, { 0x5c01, 0x77, 527 }, { 0x6001, 0x78, 538 },
{ 0x6401, 0x79, 550 }, { 0x6801, 0x7a, 562 }, { 0x6c01, 0x7b, 574 },
{ 0x7001, 0x7c, 587 }, { 0x7401, 0x7d, 600 }, { 0x7801, 0x7e, 613 },
{ 0x7c01, 0x7f, 626 }, { 0x8001, 0x00, 640 }, { 0x8401, 0x00, 654 },
{ 0x8801, 0x00, 668 }, { 0x8c01, 0x00, 683 }, { 0x9001, 0x00, 698 },
{ 0x9401, 0x00, 713 }, { 0x9801, 0x00, 729 }, { 0x9c01, 0x00, 745 },
{ 0xa001, 0x00, 761 }, { 0xa401, 0x00, 778 }, { 0xa801, 0x00, 795 },
{ 0xac01, 0x00, 812 }, { 0xb001, 0x00, 830 }, { 0xb401, 0x00, 848 },
{ 0xb801, 0x00, 867 }, { 0xbc01, 0x00, 886 }, { 0xc001, 0x00, 905 },
{ 0xc401, 0x00, 925 }, { 0xc801, 0x00, 945 }, { 0xcc01, 0x00, 966 },
{ 0xd001, 0x00, 987 }, { 0xd401, 0x00, 1009 }, { 0xd801, 0x00, 1031 },
{ 0xdc01, 0x00, 1053 }, { 0xe001, 0x00, 1076 }, { 0xe401, 0x00, 1100 },
{ 0xe801, 0x00, 1124 }, { 0xec01, 0x00, 1149 }, { 0xf001, 0x00, 1174 },
{ 0xf401, 0x00, 1199 }, { 0xf801, 0x00, 1226 }, { 0xfc01, 0x00, 1253 }
};
#define JOYCON_RUMBLE_AMP_COEFF0 50
#define JOYCON_RUMBLE_AMP_COEFF1 15
#define JOYCON_RUMBLE_AMP_COEFF2 30
#define JOYCON_RUMBLE_AMP_COEFF3 10
#define JOYCON_RUMBLE_MAX_AMP (1003) /* 100.3% */
static const struct joycon_rumble_amp_data joycon_rumble_amplitudes[] = {
/* high, low, amp */
{ 0x00, 0x0040, 0 },
{ 0x02, 0x8040, 10 }, { 0x04, 0x0041, 12 }, { 0x06, 0x8041, 14 },
{ 0x08, 0x0042, 17 }, { 0x0a, 0x8042, 20 }, { 0x0c, 0x0043, 24 },
{ 0x0e, 0x8043, 28 }, { 0x10, 0x0044, 33 }, { 0x12, 0x8044, 40 },
{ 0x14, 0x0045, 47 }, { 0x16, 0x8045, 56 }, { 0x18, 0x0046, 67 },
{ 0x1a, 0x8046, 80 }, { 0x1c, 0x0047, 95 }, { 0x1e, 0x8047, 112 },
{ 0x20, 0x0048, 117 }, { 0x22, 0x8048, 123 }, { 0x24, 0x0049, 128 },
{ 0x26, 0x8049, 134 }, { 0x28, 0x004a, 140 }, { 0x2a, 0x804a, 146 },
{ 0x2c, 0x004b, 152 }, { 0x2e, 0x804b, 159 }, { 0x30, 0x004c, 166 },
{ 0x32, 0x804c, 173 }, { 0x34, 0x004d, 181 }, { 0x36, 0x804d, 189 },
{ 0x38, 0x004e, 198 }, { 0x3a, 0x804e, 206 }, { 0x3c, 0x004f, 215 },
{ 0x3e, 0x804f, 225 }, { 0x40, 0x0050, 230 }, { 0x42, 0x8050, 235 },
{ 0x44, 0x0051, 240 }, { 0x46, 0x8051, 245 }, { 0x48, 0x0052, 251 },
{ 0x4a, 0x8052, 256 }, { 0x4c, 0x0053, 262 }, { 0x4e, 0x8053, 268 },
{ 0x50, 0x0054, 273 }, { 0x52, 0x8054, 279 }, { 0x54, 0x0055, 286 },
{ 0x56, 0x8055, 292 }, { 0x58, 0x0056, 298 }, { 0x5a, 0x8056, 305 },
{ 0x5c, 0x0057, 311 }, { 0x5e, 0x8057, 318 }, { 0x60, 0x0058, 325 },
{ 0x62, 0x8058, 332 }, { 0x64, 0x0059, 340 }, { 0x66, 0x8059, 347 },
{ 0x68, 0x005a, 355 }, { 0x6a, 0x805a, 362 }, { 0x6c, 0x005b, 370 },
{ 0x6e, 0x805b, 378 }, { 0x70, 0x005c, 387 }, { 0x72, 0x805c, 395 },
{ 0x74, 0x005d, 404 }, { 0x76, 0x805d, 413 }, { 0x78, 0x005e, 422 },
{ 0x7a, 0x805e, 431 }, { 0x7c, 0x005f, 440 }, { 0x7e, 0x805f, 450 },
{ 0x80, 0x0060, 460 }, { 0x82, 0x8060, 470 }, { 0x84, 0x0061, 480 },
{ 0x86, 0x8061, 491 }, { 0x88, 0x0062, 501 }, { 0x8a, 0x8062, 512 },
{ 0x8c, 0x0063, 524 }, { 0x8e, 0x8063, 535 }, { 0x90, 0x0064, 547 },
{ 0x92, 0x8064, 559 }, { 0x94, 0x0065, 571 }, { 0x96, 0x8065, 584 },
{ 0x98, 0x0066, 596 }, { 0x9a, 0x8066, 609 }, { 0x9c, 0x0067, 623 },
{ 0x9e, 0x8067, 636 }, { 0xa0, 0x0068, 650 }, { 0xa2, 0x8068, 665 },
{ 0xa4, 0x0069, 679 }, { 0xa6, 0x8069, 694 }, { 0xa8, 0x006a, 709 },
{ 0xaa, 0x806a, 725 }, { 0xac, 0x006b, 741 }, { 0xae, 0x806b, 757 },
{ 0xb0, 0x006c, 773 }, { 0xb2, 0x806c, 790 }, { 0xb4, 0x006d, 808 },
{ 0xb6, 0x806d, 825 }, { 0xb8, 0x006e, 843 }, { 0xba, 0x806e, 862 },
{ 0xbc, 0x006f, 881 }, { 0xbe, 0x806f, 900 }, { 0xc0, 0x0070, 920 },
{ 0xc2, 0x8070, 940 }, { 0xc4, 0x0071, 960 }, { 0xc6, 0x8071, 981 },
{ 0xc8, 0x0072, JOYCON_RUMBLE_MAX_AMP }
};
struct joycon_stick_cal {
s32 max;
s32 min;
s32 center;
};
struct joycon_imu_cal {
s16 offset[3];
s16 scale[3];
};
/* States for controller state machine */
enum joycon_ctlr_state {
JOYCON_CTLR_STATE_INIT,
JOYCON_CTLR_STATE_READ,
};
enum joycon_ctlr_type {
JOYCON_TYPE_UNKNOWN,
JOYCON_TYPE_LEFT,
JOYCON_TYPE_RIGHT,
JOYCON_TYPE_SIO
};
/*
* All the controller's button values are stored in a u32.
* They can be accessed with bitwise ANDs.
*/
static const u32 JC_BTN_Y = BIT(0);
static const u32 JC_BTN_X = BIT(1);
static const u32 JC_BTN_B = BIT(2);
static const u32 JC_BTN_A = BIT(3);
static const u32 JC_BTN_SR_R = BIT(4);
static const u32 JC_BTN_SL_R = BIT(5);
static const u32 JC_BTN_R = BIT(6);
static const u32 JC_BTN_ZR = BIT(7);
static const u32 JC_BTN_MINUS = BIT(8);
static const u32 JC_BTN_PLUS = BIT(9);
static const u32 JC_BTN_RSTICK = BIT(10);
static const u32 JC_BTN_LSTICK = BIT(11);
static const u32 JC_BTN_HOME = BIT(12);
static const u32 JC_BTN_CAP = BIT(13); /* Capture button */
static const u32 JC_BTN_DOWN = BIT(16);
static const u32 JC_BTN_UP = BIT(17);
static const u32 JC_BTN_RIGHT = BIT(18);
static const u32 JC_BTN_LEFT = BIT(19);
static const u32 JC_BTN_SR_L = BIT(20);
static const u32 JC_BTN_SL_L = BIT(21);
static const u32 JC_BTN_L = BIT(22);
static const u32 JC_BTN_ZL = BIT(23);
static const u8 JC_CRC8_POLY = 0x8D;
static const u8 JC_CRC8_INIT = 0x00;
enum joycon_msg_type {
JOYCON_MSG_TYPE_NONE,
JOYCON_MSG_TYPE_SUBCMD,
JOYCON_MSG_TYPE_UART_CMD,
};
static const u8 JC_UART_MAGIC_TX_0 = 0x19;
static const u8 JC_UART_MAGIC_TX_1 = 0x01;
static const u8 JC_UART_MAGIC_TX_2 = 0x03;
static const u8 JC_UART_MAGIC_RX_0 = 0x19;
static const u8 JC_UART_MAGIC_RX_1 = 0x81;
static const u8 JC_UART_MAGIC_RX_2 = 0x03;
struct joycon_uart_packet {
u8 magic[3];
u8 size;
u8 pad;
u8 command;
u8 header_data[5];
u8 crc;
u8 data[0]; /* Length will vary by packet type */
} __packed;
struct joycon_subcmd_request {
u8 output_id; /* Must be 0x01 for subcommand, 0x10 for rumble only */
u8 packet_num; /* Incremented every send */
u8 rumble_data[8];
u8 subcmd_id;
u8 data[]; /* Length depends on the subcommand */
} __packed;
struct joycon_subcmd_reply {
u8 ack; /* MSB 1 for ACK, 0 for NACK */
u8 id; /* id of requested subcmd */
u8 data[0]; /* Will be at most 35 bytes */
} __packed;
struct joycon_input_report {
u8 id;
u8 timer;
u8 bat_con; /* Battery and connection info */
u8 button_status[3];
u8 left_stick[3];
u8 right_stick[3];
u8 vibrator_report;
/*
* If support for firmware updates, gyroscope data, and/or NFC/IR
* are added in the future, this can be swapped for a union.
*/
struct joycon_subcmd_reply reply;
} __packed;
struct sio_uart_packet {
u8 cmd;
u8 subcmd;
u16 payload_len;
u8 data[2];
u8 crc_payload;
u8 crc_hdr;
u8 payload[];
} __packed;
struct joycon_imu_data {
s16 accel_x;
s16 accel_y;
s16 accel_z;
s16 gyro_x;
s16 gyro_y;
s16 gyro_z;
} __packed;
struct sio_input_report
{
u8 type;
u8 pkt_id;
u8 unk;
u8 button_status[3];
u8 left_stick[3];
u8 right_stick[3];
u8 sixaxis_report_num; /* Max 15. Each report is 800 us? */
union {
struct joycon_imu_data sixaxis[15];
/* IMU input reports contain 3 samples */
u8 imu_raw_bytes[sizeof(struct joycon_imu_data) * 15];
};
} __packed;
struct joycon_led_queue_item {
struct led_classdev *led;
enum led_brightness brightness;
};
#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)
#define JC_MAX_UART_PKT_SIZE (sizeof(struct joycon_uart_packet) + JC_MAX_RESP_SIZE)
#define JC_NUM_LEDS 4
#define JC_RUMBLE_DATA_SIZE 8
#define JC_RUMBLE_QUEUE_SIZE 8
#define JC_LED_QUEUE_SIZE 20
#define JC_SIO_MAX_RESP_SIZE (sizeof(struct sio_input_report) + 15)
#define JC_SIO_MAX_UART_PKT_SIZE (sizeof(struct sio_uart_packet) + JC_SIO_MAX_RESP_SIZE)
static const u16 JC_RUMBLE_DFLT_LOW_FREQ = 160;
static const u16 JC_RUMBLE_DFLT_HIGH_FREQ = 320;
static const u16 JC_RUMBLE_PERIOD_MS = 50;
static const unsigned int JC_UART_BAUD_LOW = 1000000;
static const unsigned int JC_UART_BAUD_HIGH = 3000000;
/* Each physical controller is associated with a joycon_ctlr struct */
struct joycon_ctlr {
struct serdev_device *sdev;
struct input_dev *input;
char *led_names[JC_NUM_LEDS];
struct led_classdev leds[JC_NUM_LEDS]; /* Player leds */
char *home_led_name;
struct led_classdev home_led;
enum joycon_ctlr_state ctlr_state;
enum joycon_ctlr_type ctlr_type;
struct mutex init_mutex;
spinlock_t lock;
u8 mac_addr[6];
char *mac_addr_str;
bool suspending;
/* Lite gamepad */
bool is_sio;
int sio_home_gpio;
int sio_por_gpio;
int sio_rst_gpio;
struct clk *sio_clk;
/* Third party protocol */
bool is_hori;
/* Used for crc8 computation */
u8 joycon_crc_table[CRC8_TABLE_SIZE];
/* Used for processing led brightness sets */
struct joycon_led_queue_item led_queue[JC_LED_QUEUE_SIZE];
int led_queue_head;
int led_queue_tail;
struct work_struct led_worker;
/* The following members are used for synchronous sends/receives */
enum joycon_msg_type msg_type;
u8 subcmd_num;
struct mutex output_mutex;
u8 input_buf[JC_SIO_MAX_RESP_SIZE];
wait_queue_head_t wait;
bool received_resp;
u8 subcmd_ack_match;
u8 uart_cmd_match;
bool received_input_report;
bool ctlr_removed;
/* Buffering partially received uart packets */
u8 partial_pkt[JC_SIO_MAX_UART_PKT_SIZE];
size_t partial_pkt_len;
/* Joy-con detection */
struct workqueue_struct *detection_queue;
struct delayed_work detection_worker;
int detection_irq_retries;
int detect_en_gpio;
bool detect_en_req;
int detect_gpio;
int detect_irq;
/* Joy-con input polling */
struct workqueue_struct *input_queue;
struct delayed_work input_worker;
unsigned int last_input_report_msecs;
/* Factory calibration data */
struct joycon_stick_cal stick_cal_x;
struct joycon_stick_cal stick_cal_y;
struct joycon_stick_cal right_stick_cal_x;
struct joycon_stick_cal right_stick_cal_y;
struct joycon_imu_cal accel_cal;
struct joycon_imu_cal gyro_cal;
/* Prevents needlessly recalculating these divisors every sample */
s32 imu_cal_accel_divisor[3];
s32 imu_cal_gyro_divisor[3];
/* Power supply data */
struct power_supply *battery;
char *battery_desc_name;
struct power_supply_desc battery_desc;
u8 battery_capacity;
bool battery_charging;
bool host_powered;
struct regulator *charger_reg;
/* Rumble */
u8 rumble_data[JC_RUMBLE_QUEUE_SIZE][JC_RUMBLE_DATA_SIZE];
int rumble_queue_head;
int rumble_queue_tail;
struct workqueue_struct *rumble_queue;
struct delayed_work rumble_worker;
unsigned int rumble_msecs;
u16 rumble_ll_freq;
u16 rumble_lh_freq;
u16 rumble_rl_freq;
u16 rumble_rh_freq;
u32 rumble_buf_avail;
bool rumble_keep_alive;
/* Imu */
struct input_dev *imu_input;
bool imu_first_packet_received; /* Helps in initiating timestamp */
unsigned int imu_timestamp_us; /* Timestamp we report to userspace */
unsigned int imu_last_pkt_ms; /* Used to calc imu report delta */
/* The following are used to track the average imu report time delta */
unsigned int imu_delta_samples_count;
unsigned int imu_delta_samples_sum;
unsigned int imu_avg_delta_ms;
};
static int joycon_serdev_send(struct joycon_ctlr *ctlr, u8 *data,
size_t len, u32 timeout)
{
int ret;
ret = serdev_device_write(ctlr->sdev, data, len, timeout);
if (ret < 0) {
dev_err(&ctlr->sdev->dev,
"Failed to send serial data; ret=%d\n", ret);
return ret;
}
return 0;
}
static int joycon_serdev_send_sync(struct joycon_ctlr *ctlr, u8 *data,
size_t len, u32 timeout)
{
int ret;
int tries = 2;
/*
* The controller occasionally seems to drop subcommands. In testing,
* doing one retry after a timeout appears to always work.
*/
while (tries--) {
/*
* If we are in the proper reporting mode, wait for an input
* report prior to sending the subcommand. This improves
* reliability considerably.
*/
if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) {
unsigned long flags;
spin_lock_irqsave(&ctlr->lock, flags);
ctlr->received_input_report = false;
spin_unlock_irqrestore(&ctlr->lock, flags);
ret = wait_event_timeout(ctlr->wait,
ctlr->received_input_report,
HZ / 4);
spin_lock_irqsave(&ctlr->lock, flags);
if (!ret && !ctlr->ctlr_removed) {
dev_warn(&ctlr->sdev->dev,
"Timeout waiting for input report\n");
ret = -ETIMEDOUT;
spin_unlock_irqrestore(&ctlr->lock, flags);
goto err;
}
spin_unlock_irqrestore(&ctlr->lock, flags);
}
ret = joycon_serdev_send(ctlr, data, len, timeout);
if (ret) {
memset(ctlr->input_buf, 0, JC_SIO_MAX_RESP_SIZE);
return ret;
}
ret = wait_event_timeout(ctlr->wait, ctlr->received_resp,
timeout);
if (!ret) {
if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) {
dev_info(&ctlr->sdev->dev,
"Synchronous send/receive timed out\n");
}
if (tries) {
dev_dbg(&ctlr->sdev->dev,
"Retrying sync send after timeout\n");
}
memset(ctlr->input_buf, 0, JC_SIO_MAX_RESP_SIZE);
ret = -ETIMEDOUT;
} else {
ret = 0;
break;
}
}
err:
ctlr->received_resp = false;
return ret;
}
static int joycon_send_packet(struct joycon_ctlr *ctlr, u8 command,
u8 *hdata, size_t hdata_size,
u8 *data, size_t data_size, u32 timeout,
bool sync)
{
struct joycon_uart_packet *packet;
struct device *dev = &ctlr->sdev->dev;
int ret = 0;
packet = kzalloc(sizeof(*packet) + data_size, GFP_KERNEL);
if (!packet)
return -ENOMEM;
packet->magic[0] = JC_UART_MAGIC_TX_0;
packet->magic[1] = JC_UART_MAGIC_TX_1;
packet->magic[2] = JC_UART_MAGIC_TX_2;
packet->size = 7 + data_size;
packet->command = command;
if (hdata) {
if (hdata_size > sizeof(packet->header_data)) {
dev_err(dev, "Header data size too large\n");
ret = -EINVAL;
goto err;
}
memcpy(packet->header_data, hdata, hdata_size);
}
if (command == JC_CMD_HANDSHAKE) {
packet->crc = JC_CRC8_INIT;
} else {
packet->crc = crc8(ctlr->joycon_crc_table, &packet->pad, sizeof(packet->pad) + sizeof(packet->command) + sizeof(packet->header_data), JC_CRC8_INIT);
}
if (data)
memcpy(packet->data, data, data_size);
if (sync)
ret = joycon_serdev_send_sync(ctlr, (u8 *)packet,
sizeof(*packet) + data_size,
timeout);
else
ret = joycon_serdev_send(ctlr, (u8 *)packet,
sizeof(*packet) + data_size, timeout);
if (ret && ctlr->ctlr_state == JOYCON_CTLR_STATE_READ)
dev_err(dev, "Failed sending uart packet; ret=%d\n", ret);
err:
kfree(packet);
return ret;
}
static int sio_send_packet(struct joycon_ctlr *ctlr, u8 command,
u8 *hdata, size_t hdata_size,
u8 *data, size_t data_size, u32 timeout,
bool sync)
{
struct sio_uart_packet *packet;
struct device *dev = &ctlr->sdev->dev;
int ret = 0;
packet = kzalloc(sizeof(*packet) + data_size, GFP_KERNEL);
if (!packet)
return -ENOMEM;
packet->cmd = JC_SIO_OUTPUT_RPT;
packet->subcmd = command;
packet->payload_len = data_size;
if (hdata) {
if (hdata_size > sizeof(packet->data)) {
dev_err(dev, "Header data size too large\n");
ret = -EINVAL;
goto err;
}
memcpy(packet->data, hdata, hdata_size);
}
if (data_size)
packet->crc_payload = crc8(ctlr->joycon_crc_table, data, data_size, JC_CRC8_INIT);
packet->crc_hdr = crc8(ctlr->joycon_crc_table, (u8 *)packet, sizeof(*packet) - 1, JC_CRC8_INIT);
if (data)
memcpy(packet->data, data, data_size);
if (sync)
ret = joycon_serdev_send_sync(ctlr, (u8 *)packet,
sizeof(*packet) + data_size,
timeout);
else
ret = joycon_serdev_send(ctlr, (u8 *)packet,
sizeof(*packet) + data_size, timeout);
if (ret && ctlr->ctlr_state == JOYCON_CTLR_STATE_READ)
dev_err(dev, "Failed sending uart packet; ret=%d\n", ret);
err:
kfree(packet);
return ret;
}
/* Caller must set ctlr->uart_cmd_match prior to calling */
static int joycon_send_command(struct joycon_ctlr *ctlr, u8 command,
u8 *data, size_t data_len, u32 timeout)
{
struct device *dev = &ctlr->sdev->dev;
int ret;
ctlr->msg_type = JOYCON_MSG_TYPE_UART_CMD;
ret = joycon_send_packet(ctlr, command, data, data_len, NULL, 0,
timeout, true);
if (ret < 0)
dev_dbg(dev, "Send command failed; ret=%d\n", ret);
return ret;
}
static int joycon_send_subcmd(struct joycon_ctlr *ctlr,
struct joycon_subcmd_request *subcmd,
size_t data_len, u32 timeout)
{
int ret;
unsigned long flags;
__be16 subcmd_size;
struct device *dev = &ctlr->sdev->dev;
spin_lock_irqsave(&ctlr->lock, flags);
memcpy(subcmd->rumble_data, ctlr->rumble_data[ctlr->rumble_queue_tail],
JC_RUMBLE_DATA_SIZE);
spin_unlock_irqrestore(&ctlr->lock, flags);
subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD;
subcmd->packet_num = ctlr->subcmd_num;
if (++ctlr->subcmd_num > 0xF)
ctlr->subcmd_num = 0;
ctlr->subcmd_ack_match = subcmd->subcmd_id;
ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD;
subcmd_size = cpu_to_be16(sizeof(*subcmd) + data_len);
ret = joycon_send_packet(ctlr, JC_CMD_EXTRET,
(u8 *)&subcmd_size, sizeof(subcmd_size),
(u8 *)subcmd, sizeof(*subcmd) + data_len,
timeout, true);
if (ret < 0)
dev_dbg(dev, "Send subcommand failed; ret=%d\n", ret);
return ret;
}
static int joycon_set_hci_state(struct joycon_ctlr *ctlr, u8 state)
{
struct joycon_subcmd_request *req;
u8 buffer[sizeof(*req) + 1] = { 0 };
req = (struct joycon_subcmd_request *)buffer;
req->subcmd_id = JC_SUBCMD_SET_HCI_STATE;
req->data[0] = state;
dev_dbg(&ctlr->sdev->dev, "Setting hci state = %u\n", state);
/* We won't get a reply. Just use a short timeout. */
return joycon_send_subcmd(ctlr, req, 1, HZ/10);
}
static int joycon_get_ctlr_info(struct joycon_ctlr *ctlr)
{
struct joycon_subcmd_request req = { 0 };
struct device *dev = &ctlr->sdev->dev;
struct joycon_input_report *report;
int ret;
req.subcmd_id = JC_SUBCMD_REQ_DEV_INFO;
ret = joycon_send_subcmd(ctlr, &req, 0, HZ);
if (ret) {
dev_err(dev, "Failed to get Joy-con info; ret=%d\n", ret);
return ret;
}
report = (struct joycon_input_report *)ctlr->input_buf;
switch (report->reply.data[2]) {
case 1:
ctlr->ctlr_type = JOYCON_TYPE_LEFT;
dev_info(dev, "Detected left joy-con\n");
break;
case 2:
ctlr->ctlr_type = JOYCON_TYPE_RIGHT;
dev_info(dev, "Detected right joy-con\n");
break;
default:
dev_err(dev, "Invalid joy-con type = %u\n",
report->reply.data[2]);
return -EINVAL;
}
return 0;
}
/* Supply nibbles for flash and on. Ones correspond to active */
static int joycon_set_player_leds(struct joycon_ctlr *ctlr, u8 flash, u8 on)
{
struct joycon_subcmd_request *req;
u8 buffer[sizeof(*req) + 1] = { 0 };
req = (struct joycon_subcmd_request *)buffer;
req->subcmd_id = JC_SUBCMD_SET_PLAYER_LIGHTS;
req->data[0] = (flash << 4) | on;
dev_dbg(&ctlr->sdev->dev, "Setting player leds\n");
return joycon_send_subcmd(ctlr, req, 1, HZ/4);
}
/* The following was copied from the hid core's implementation */
static u32 field_extract(u8 *report, unsigned offset, int n)
{
unsigned int idx = offset / 8;
unsigned int bit_nr = 0;
unsigned int bit_shift = offset % 8;
int bits_to_copy = 8 - bit_shift;
u32 value = 0;
u32 mask = n < 32 ? (1U << n) - 1 : ~0U;
while (n > 0) {
value |= ((u32)report[idx] >> bit_shift) << bit_nr;
n -= bits_to_copy;
bit_nr += bits_to_copy;
bits_to_copy = 8;
bit_shift = 0;
idx++;
}
return value & mask;
}
static int joycon_request_spi_flash_read(struct joycon_ctlr *ctlr,
u32 start_addr, u8 size, u8 **reply)
{
struct joycon_subcmd_request *req;
struct joycon_input_report *report;
u8 buffer[sizeof(*req) + 5] = { 0 };
struct device *dev = &ctlr->sdev->dev;
u8 *data;
int ret;
if (!reply)
return -EINVAL;
req = (struct joycon_subcmd_request *)buffer;
req->subcmd_id = JC_SUBCMD_SPI_FLASH_READ;
data = req->data;
data[0] = 0xFF & start_addr;
data[1] = 0xFF & (start_addr >> 8);
data[2] = 0xFF & (start_addr >> 16);
data[3] = 0xFF & (start_addr >> 24);
data[4] = size;
dev_dbg(dev, "Requesting SPI flash data\n");
ret = joycon_send_subcmd(ctlr, req, 5, HZ);
if (ret) {
dev_err(dev, "Failed reading SPI flash; ret=%d\n", ret);
} else {
report = (struct joycon_input_report *)ctlr->input_buf;
/* The read data starts at the 6th byte */
*reply = &report->reply.data[5];
}
return ret;
}
/*
* User calibration's presence is denoted with a magic byte preceding it.
* returns 0 if magic val is present, 1 if not present, < 0 on error
*/
static int joycon_check_for_cal_magic(struct joycon_ctlr *ctlr, u32 flash_addr)
{
int ret;
u8 *reply;
ret = joycon_request_spi_flash_read(ctlr, flash_addr,
JC_CAL_USR_MAGIC_SIZE, &reply);
if (ret)
return ret;
return reply[0] != JC_CAL_USR_MAGIC_0 || reply[1] != JC_CAL_USR_MAGIC_1;
}
static int joycon_read_stick_calibration(struct joycon_ctlr *ctlr, u16 cal_addr,
struct joycon_stick_cal *cal_x,
struct joycon_stick_cal *cal_y,
bool left_stick)
{
s32 x_max_above;
s32 x_min_below;
s32 y_max_above;
s32 y_min_below;
u8 *raw_cal;
int ret;
ret = joycon_request_spi_flash_read(ctlr, cal_addr,
JC_CAL_STICK_DATA_SIZE, &raw_cal);
if (ret)
return ret;
/* Stick calibration parsing: note the order differs based on stick */
if (left_stick) {
x_max_above = field_extract((raw_cal + 0), 0, 12);
y_max_above = field_extract((raw_cal + 1), 4, 12);
cal_x->center = field_extract((raw_cal + 3), 0, 12);
cal_y->center = field_extract((raw_cal + 4), 4, 12);
x_min_below = field_extract((raw_cal + 6), 0, 12);
y_min_below = field_extract((raw_cal + 7), 4, 12);
} else {
cal_x->center = field_extract((raw_cal + 0), 0, 12);
cal_y->center = field_extract((raw_cal + 1), 4, 12);
x_min_below = field_extract((raw_cal + 3), 0, 12);
y_min_below = field_extract((raw_cal + 4), 4, 12);
x_max_above = field_extract((raw_cal + 6), 0, 12);
y_max_above = field_extract((raw_cal + 7), 4, 12);
}
cal_x->max = cal_x->center + x_max_above;
cal_x->min = cal_x->center - x_min_below;
cal_y->max = cal_y->center + y_max_above;
cal_y->min = cal_y->center - y_min_below;
return 0;
}
static const u16 DFLT_STICK_CAL_CEN = 2048;
static const u16 DFLT_STICK_CAL_MAX = 3072;
static const u16 DFLT_STICK_CAL_MIN = 1024;
static const u16 HORI_STICK_CAL_CEN = 2048;
static const u16 HORI_STICK_CAL_MAX = 4095;
static const u16 HORI_STICK_CAL_MIN = 0;
static int joycon_request_calibration(struct joycon_ctlr *ctlr)
{
u16 stick_addr = JC_CAL_FCT_DATA_LEFT_ADDR;
struct device *dev = &ctlr->sdev->dev;
enum joycon_ctlr_type type = ctlr->ctlr_type;
int ret;