forked from cb22/macbook12-spi-driver
-
Notifications
You must be signed in to change notification settings - Fork 50
/
applespi.c
2635 lines (2170 loc) · 72.2 KB
/
applespi.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
/*
* MacBook (Pro) SPI keyboard and touchpad driver
*
* Copyright (c) 2015-2018 Federico Lorenzi
* Copyright (c) 2017-2018 Ronald Tschalär
*/
/*
* The keyboard and touchpad controller on the MacBookAir6, MacBookPro12,
* MacBook8 and newer can be driven either by USB or SPI. However the USB
* pins are only connected on the MacBookAir6 and 7 and the MacBookPro12.
* All others need this driver. The interface is selected using ACPI methods:
*
* * UIEN ("USB Interface Enable"): If invoked with argument 1, disables SPI
* and enables USB. If invoked with argument 0, disables USB.
* * UIST ("USB Interface Status"): Returns 1 if USB is enabled, 0 otherwise.
* * SIEN ("SPI Interface Enable"): If invoked with argument 1, disables USB
* and enables SPI. If invoked with argument 0, disables SPI.
* * SIST ("SPI Interface Status"): Returns 1 if SPI is enabled, 0 otherwise.
* * ISOL: Resets the four GPIO pins used for SPI. Intended to be invoked with
* argument 1, then once more with argument 0.
*
* UIEN and UIST are only provided on models where the USB pins are connected.
*
* SPI-based Protocol
* ------------------
*
* The device and driver exchange messages (struct message); each message is
* encapsulated in one or more packets (struct spi_packet). There are two types
* of exchanges: reads, and writes. A read is signaled by a GPE, upon which one
* message can be read from the device. A write exchange consists of writing a
* command message, immediately reading a short status packet, and then, upon
* receiving a GPE, reading the response message. Write exchanges cannot be
* interleaved, i.e. a new write exchange must not be started till the previous
* write exchange is complete. Whether a received message is part of a read or
* write exchange is indicated in the encapsulating packet's flags field.
*
* A single message may be too large to fit in a single packet (which has a
* fixed, 256-byte size). In that case it will be split over multiple,
* consecutive packets.
*/
#include <linux/acpi.h>
#include <linux/crc16.h>
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/efi.h>
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/jiffies.h>
#include <linux/ktime.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/spinlock.h>
#include <linux/spi/spi.h>
#include <linux/string.h>
#include <linux/trace_events.h>
#include <linux/version.h>
#include <linux/wait.h>
#include <asm/barrier.h>
#include <asm/unaligned.h>
#define CREATE_TRACE_POINTS
#include "applespi.h"
#include "applespi_trace.h"
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0)
#define PRE_SPI_PROPERTIES
#endif
#ifdef PRE_SPI_PROPERTIES
#include <linux/notifier.h>
#endif
#ifndef sizeof_field
#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
#endif
#ifndef struct_size
#define struct_size(p, member, n) (sizeof(*(p)) + n * sizeof(*(p)->member))
#endif
#ifndef wait_event_lock_irq_timeout
#define wait_event_lock_irq_timeout wait_event_interruptible_lock_irq_timeout
#endif
#define APPLESPI_PACKET_SIZE 256
#define APPLESPI_STATUS_SIZE 4
#define PACKET_TYPE_READ 0x20
#define PACKET_TYPE_WRITE 0x40
#define PACKET_DEV_KEYB 0x01
#define PACKET_DEV_TPAD 0x02
#define PACKET_DEV_INFO 0xd0
#define MAX_ROLLOVER 6
#define MAX_FINGERS 11
#define MAX_FINGER_ORIENTATION 16384
#define MAX_PKTS_PER_MSG 2
#define KBD_BL_LEVEL_MIN 32U
#define KBD_BL_LEVEL_MAX 255U
#define KBD_BL_LEVEL_SCALE 1000000U
#define KBD_BL_LEVEL_ADJ \
((KBD_BL_LEVEL_MAX - KBD_BL_LEVEL_MIN) * KBD_BL_LEVEL_SCALE / 255U)
#define EFI_BL_LEVEL_NAME L"KeyboardBacklightLevel"
#define EFI_BL_LEVEL_GUID EFI_GUID(0xa076d2af, 0x9678, 0x4386, 0x8b, 0x58, 0x1f, 0xc8, 0xef, 0x04, 0x16, 0x19)
#define APPLE_FLAG_FKEY 0x01
#define SPI_RW_CHG_DELAY_US 100 /* from experimentation, in µs */
#define SYNAPTICS_VENDOR_ID 0x06cb
static unsigned int fnmode = 1;
module_param(fnmode, uint, 0644);
MODULE_PARM_DESC(fnmode, "Mode of Fn key on Apple keyboards (0 = disabled, [1] = fkeyslast, 2 = fkeysfirst)");
static unsigned int fnremap;
module_param(fnremap, uint, 0644);
MODULE_PARM_DESC(fnremap, "Remap Fn key ([0] = no-remap; 1 = left-ctrl, 2 = left-shift, 3 = left-alt, 4 = left-meta, 6 = right-shift, 7 = right-alt, 8 = right-meta)");
static bool iso_layout;
module_param(iso_layout, bool, 0644);
MODULE_PARM_DESC(iso_layout, "Enable/Disable hardcoded ISO-layout of the keyboard. ([0] = disabled, 1 = enabled)");
static char touchpad_dimensions[40];
module_param_string(touchpad_dimensions, touchpad_dimensions,
sizeof(touchpad_dimensions), 0444);
MODULE_PARM_DESC(touchpad_dimensions, "The pixel dimensions of the touchpad, as XxY+W+H .");
static char* trace_event;
module_param(trace_event, charp, 0444);
MODULE_PARM_DESC(trace_event, "Enable early event tracing. It takes the form of a comma-separated list of events to enable.");
/**
* struct keyboard_protocol - keyboard message.
* message.type = 0x0110, message.length = 0x000a
*
* @unknown1: unknown
* @modifiers: bit-set of modifier/control keys pressed
* @unknown2: unknown
* @keys_pressed: the (non-modifier) keys currently pressed
* @fn_pressed: whether the fn key is currently pressed
* @crc16: crc over the whole message struct (message header +
* this struct) minus this @crc16 field
*/
struct keyboard_protocol {
u8 unknown1;
u8 modifiers;
u8 unknown2;
u8 keys_pressed[MAX_ROLLOVER];
u8 fn_pressed;
__le16 crc16;
};
/**
* struct tp_finger - single trackpad finger structure, le16-aligned
*
* @origin: zero when switching track finger
* @abs_x: absolute x coordinate
* @abs_y: absolute y coordinate
* @rel_x: relative x coordinate
* @rel_y: relative y coordinate
* @tool_major: tool area, major axis
* @tool_minor: tool area, minor axis
* @orientation: 16384 when point, else 15 bit angle
* @touch_major: touch area, major axis
* @touch_minor: touch area, minor axis
* @unused: zeros
* @pressure: pressure on forcetouch touchpad
* @multi: one finger: varies, more fingers: constant
* @crc16: on last finger: crc over the whole message struct
* (i.e. message header + this struct) minus the last
* @crc16 field; unknown on all other fingers.
*/
struct tp_finger {
__le16 origin;
__le16 abs_x;
__le16 abs_y;
__le16 rel_x;
__le16 rel_y;
__le16 tool_major;
__le16 tool_minor;
__le16 orientation;
__le16 touch_major;
__le16 touch_minor;
__le16 unused[2];
__le16 pressure;
__le16 multi;
__le16 crc16;
};
/**
* struct touchpad_protocol - touchpad message.
* message.type = 0x0210
*
* @unknown1: unknown
* @clicked: 1 if a button-click was detected, 0 otherwise
* @unknown2: unknown
* @number_of_fingers: the number of fingers being reported in @fingers
* @clicked2: same as @clicked
* @unknown3: unknown
* @fingers: the data for each finger
*/
struct touchpad_protocol {
u8 unknown1[1];
u8 clicked;
u8 unknown2[28];
u8 number_of_fingers;
u8 clicked2;
u8 unknown3[16];
struct tp_finger fingers[0];
};
/**
* struct command_protocol_tp_info - get touchpad info.
* message.type = 0x1020, message.length = 0x0000
*
* @crc16: crc over the whole message struct (message header +
* this struct) minus this @crc16 field
*/
struct command_protocol_tp_info {
__le16 crc16;
};
/**
* struct touchpad_info - touchpad info response.
* message.type = 0x1020, message.length = 0x006e
*
* @unknown1: unknown
* @model_flags: flags (vary by model number, but significance otherwise
* unknown)
* @model_no: the touchpad model number
* @unknown2: unknown
* @crc16: crc over the whole message struct (message header +
* this struct) minus this @crc16 field
*/
struct touchpad_info_protocol {
u8 unknown1[105];
u8 model_flags;
u8 model_no;
u8 unknown2[3];
__le16 crc16;
};
/**
* struct command_protocol_mt_init - initialize multitouch.
* message.type = 0x0252, message.length = 0x0002
*
* @cmd: value: 0x0102
* @crc16: crc over the whole message struct (message header +
* this struct) minus this @crc16 field
*/
struct command_protocol_mt_init {
__le16 cmd;
__le16 crc16;
};
/**
* struct command_protocol_capsl - toggle caps-lock led
* message.type = 0x0151, message.length = 0x0002
*
* @unknown: value: 0x01 (length?)
* @led: 0 off, 2 on
* @crc16: crc over the whole message struct (message header +
* this struct) minus this @crc16 field
*/
struct command_protocol_capsl {
u8 unknown;
u8 led;
__le16 crc16;
};
/**
* struct command_protocol_bl - set keyboard backlight brightness
* message.type = 0xB051, message.length = 0x0006
*
* @const1: value: 0x01B0
* @level: the brightness level to set
* @const2: value: 0x0001 (backlight off), 0x01F4 (backlight on)
* @crc16: crc over the whole message struct (message header +
* this struct) minus this @crc16 field
*/
struct command_protocol_bl {
__le16 const1;
__le16 level;
__le16 const2;
__le16 crc16;
};
/**
* struct message - a complete spi message.
*
* Each message begins with fixed header, followed by a message-type specific
* payload, and ends with a 16-bit crc. Because of the varying lengths of the
* payload, the crc is defined at the end of each payload struct, rather than
* in this struct.
*
* @type: the message type
* @zero: always 0
* @counter: incremented on each message, rolls over after 255; there is a
* separate counter for each message type.
* @rsp_buf_len:response buffer length (the exact nature of this field is quite
* speculative). On a request/write this is often the same as
* @length, though in some cases it has been seen to be much larger
* (e.g. 0x400); on a response/read this the same as on the
* request; for reads that are not responses it is 0.
* @length: length of the remainder of the data in the whole message
* structure (after re-assembly in case of being split over
* multiple spi-packets), minus the trailing crc. The total size
* of the message struct is therefore @length + 10.
*/
struct message {
__le16 type;
u8 zero;
u8 counter;
__le16 rsp_buf_len;
__le16 length;
union {
struct keyboard_protocol keyboard;
struct touchpad_protocol touchpad;
struct touchpad_info_protocol tp_info;
struct command_protocol_tp_info tp_info_command;
struct command_protocol_mt_init init_mt_command;
struct command_protocol_capsl capsl_command;
struct command_protocol_bl bl_command;
u8 data[0];
};
};
/* type + zero + counter + rsp_buf_len + length */
#define MSG_HEADER_SIZE 8
/**
* struct spi_packet - a complete spi packet; always 256 bytes. This carries
* the (parts of the) message in the data. But note that this does not
* necessarily contain a complete message, as in some cases (e.g. many
* fingers pressed) the message is split over multiple packets (see the
* @offset, @remaining, and @length fields). In general the data parts in
* spi_packet's are concatenated until @remaining is 0, and the result is an
* message.
*
* @flags: 0x40 = write (to device), 0x20 = read (from device); note that
* the response to a write still has 0x40.
* @device: 1 = keyboard, 2 = touchpad
* @offset: specifies the offset of this packet's data in the complete
* message; i.e. > 0 indicates this is a continuation packet (in
* the second packet for a message split over multiple packets
* this would then be the same as the @length in the first packet)
* @remaining: number of message bytes remaining in subsequents packets (in
* the first packet of a message split over two packets this would
* then be the same as the @length in the second packet)
* @length: length of the valid data in the @data in this packet
* @data: all or part of a message
* @crc16: crc over this whole structure minus this @crc16 field. This
* covers just this packet, even on multi-packet messages (in
* contrast to the crc in the message).
*/
struct spi_packet {
u8 flags;
u8 device;
__le16 offset;
__le16 remaining;
__le16 length;
u8 data[246];
__le16 crc16;
};
struct spi_settings {
#ifdef PRE_SPI_PROPERTIES
u64 spi_sclk_period; /* period in ns */
u64 spi_word_size; /* in number of bits */
u64 spi_bit_order; /* 1 = MSB_FIRST, 0 = LSB_FIRST */
u64 spi_spo; /* clock polarity: 0 = low, 1 = high */
u64 spi_sph; /* clock phase: 0 = first, 1 = second */
#endif
u64 spi_cs_delay; /* cs-to-clk delay in us */
u64 reset_a2r_usec; /* active-to-receive delay? */
u64 reset_rec_usec; /* ? (cur val: 10) */
};
/* this mimics struct drm_rect */
struct applespi_tp_info {
int x_min;
int y_min;
int x_max;
int y_max;
};
struct applespi_data {
struct spi_device *spi;
struct spi_settings spi_settings;
struct input_dev *keyboard_input_dev;
struct input_dev *touchpad_input_dev;
u8 *tx_buffer;
u8 *tx_status;
u8 *rx_buffer;
u8 *msg_buf;
unsigned int saved_msg_len;
struct applespi_tp_info tp_info;
u8 last_keys_pressed[MAX_ROLLOVER];
u8 last_keys_fn_pressed[MAX_ROLLOVER];
u8 last_fn_pressed;
struct input_mt_pos pos[MAX_FINGERS];
int slots[MAX_FINGERS];
int gpe;
acpi_handle sien;
acpi_handle sist;
struct spi_transfer dl_t;
struct spi_transfer rd_t;
struct spi_message rd_m;
struct spi_transfer ww_t;
struct spi_transfer wd_t;
struct spi_transfer wr_t;
struct spi_transfer st_t;
struct spi_message wr_m;
bool want_tp_info_cmd;
bool want_mt_init_cmd;
bool want_cl_led_on;
bool have_cl_led_on;
unsigned int want_bl_level;
unsigned int have_bl_level;
unsigned int cmd_msg_cntr;
/* lock to protect the above parameters and flags below */
spinlock_t cmd_msg_lock;
ktime_t cmd_msg_queued;
enum applespi_evt_type cmd_evt_type;
struct led_classdev backlight_info;
bool suspended;
bool drain;
wait_queue_head_t drain_complete;
bool read_active;
bool write_active;
struct applespi_complete_info {
void (*complete)(void *context);
struct applespi_data *applespi;
} spi_complete[2];
bool cancel_spi;
wait_queue_head_t tp_info_complete;
struct touchpad_info_protocol rcvd_tp_info;
struct dentry *debugfs_root;
bool debug_tp_dim;
char tp_dim_val[40];
int tp_dim_min_x;
int tp_dim_max_x;
int tp_dim_min_y;
int tp_dim_max_y;
};
static const unsigned char applespi_scancodes[] = {
0, 0, 0, 0,
KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J,
KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T,
KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z,
KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0,
KEY_ENTER, KEY_ESC, KEY_BACKSPACE, KEY_TAB, KEY_SPACE, KEY_MINUS,
KEY_EQUAL, KEY_LEFTBRACE, KEY_RIGHTBRACE, KEY_BACKSLASH, 0,
KEY_SEMICOLON, KEY_APOSTROPHE, KEY_GRAVE, KEY_COMMA, KEY_DOT, KEY_SLASH,
KEY_CAPSLOCK,
KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9,
KEY_F10, KEY_F11, KEY_F12, 0, 0, 0, 0, 0, 0, 0, 0, 0,
KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_102ND,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RO, 0, KEY_YEN, 0, 0, 0, 0, 0,
0, KEY_KATAKANAHIRAGANA, KEY_MUHENKAN
};
/*
* This must have exactly as many entries as there are bits in
* struct keyboard_protocol.modifiers .
*/
static const unsigned char applespi_controlcodes[] = {
KEY_LEFTCTRL,
KEY_LEFTSHIFT,
KEY_LEFTALT,
KEY_LEFTMETA,
0,
KEY_RIGHTSHIFT,
KEY_RIGHTALT,
KEY_RIGHTMETA
};
struct applespi_key_translation {
u16 from;
u16 to;
u8 flags;
};
static const struct applespi_key_translation applespi_fn_codes[] = {
{ KEY_BACKSPACE, KEY_DELETE },
{ KEY_ENTER, KEY_INSERT },
{ KEY_F1, KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
{ KEY_F2, KEY_BRIGHTNESSUP, APPLE_FLAG_FKEY },
{ KEY_F3, KEY_SCALE, APPLE_FLAG_FKEY },
{ KEY_F4, KEY_DASHBOARD, APPLE_FLAG_FKEY },
{ KEY_F5, KEY_KBDILLUMDOWN, APPLE_FLAG_FKEY },
{ KEY_F6, KEY_KBDILLUMUP, APPLE_FLAG_FKEY },
{ KEY_F7, KEY_PREVIOUSSONG, APPLE_FLAG_FKEY },
{ KEY_F8, KEY_PLAYPAUSE, APPLE_FLAG_FKEY },
{ KEY_F9, KEY_NEXTSONG, APPLE_FLAG_FKEY },
{ KEY_F10, KEY_MUTE, APPLE_FLAG_FKEY },
{ KEY_F11, KEY_VOLUMEDOWN, APPLE_FLAG_FKEY },
{ KEY_F12, KEY_VOLUMEUP, APPLE_FLAG_FKEY },
{ KEY_RIGHT, KEY_END },
{ KEY_LEFT, KEY_HOME },
{ KEY_DOWN, KEY_PAGEDOWN },
{ KEY_UP, KEY_PAGEUP },
{ }
};
static const struct applespi_key_translation apple_iso_keyboard[] = {
{ KEY_GRAVE, KEY_102ND },
{ KEY_102ND, KEY_GRAVE },
{ }
};
struct applespi_tp_model_info {
u16 model;
struct applespi_tp_info tp_info;
};
static const struct applespi_tp_model_info applespi_tp_models[] = {
{
.model = 0x04, /* MB8 MB9 MB10 */
.tp_info = { -5087, -182, 5579, 6089 },
},
{
.model = 0x05, /* MBP13,1 MBP13,2 MBP14,1 MBP14,2 */
.tp_info = { -6243, -170, 6749, 7685 },
},
{
.model = 0x06, /* MBP13,3 MBP14,3 */
.tp_info = { -7456, -163, 7976, 9283 },
},
{}
};
typedef void (*applespi_trace_fun)(enum applespi_evt_type,
enum applespi_pkt_type, u8 *, size_t);
static applespi_trace_fun applespi_get_trace_fun(enum applespi_evt_type type)
{
switch (type) {
case ET_CMD_TP_INI:
return trace_applespi_tp_ini_cmd;
case ET_CMD_BL:
return trace_applespi_backlight_cmd;
case ET_CMD_CL:
return trace_applespi_caps_lock_cmd;
case ET_RD_KEYB:
return trace_applespi_keyboard_data;
case ET_RD_TPAD:
return trace_applespi_touchpad_data;
case ET_RD_UNKN:
return trace_applespi_unknown_data;
default:
WARN_ONCE(1, "Unknown msg type %d", type);
return trace_applespi_unknown_data;
}
}
static void applespi_setup_read_txfrs(struct applespi_data *applespi)
{
struct spi_message *msg = &applespi->rd_m;
struct spi_transfer *dl_t = &applespi->dl_t;
struct spi_transfer *rd_t = &applespi->rd_t;
memset(dl_t, 0, sizeof(*dl_t));
memset(rd_t, 0, sizeof(*rd_t));
dl_t->delay_usecs = applespi->spi_settings.spi_cs_delay;
rd_t->rx_buf = applespi->rx_buffer;
rd_t->len = APPLESPI_PACKET_SIZE;
spi_message_init(msg);
spi_message_add_tail(dl_t, msg);
spi_message_add_tail(rd_t, msg);
}
static void applespi_setup_write_txfrs(struct applespi_data *applespi)
{
struct spi_message *msg = &applespi->wr_m;
struct spi_transfer *wt_t = &applespi->ww_t;
struct spi_transfer *dl_t = &applespi->wd_t;
struct spi_transfer *wr_t = &applespi->wr_t;
struct spi_transfer *st_t = &applespi->st_t;
memset(wt_t, 0, sizeof(*wt_t));
memset(dl_t, 0, sizeof(*dl_t));
memset(wr_t, 0, sizeof(*wr_t));
memset(st_t, 0, sizeof(*st_t));
/*
* All we need here is a delay at the beginning of the message before
* asserting cs. But the current spi API doesn't support this, so we
* end up with an extra unnecessary (but harmless) cs assertion and
* deassertion.
*/
wt_t->delay_usecs = SPI_RW_CHG_DELAY_US;
wt_t->cs_change = 1;
dl_t->delay_usecs = applespi->spi_settings.spi_cs_delay;
wr_t->tx_buf = applespi->tx_buffer;
wr_t->len = APPLESPI_PACKET_SIZE;
wr_t->delay_usecs = SPI_RW_CHG_DELAY_US;
st_t->rx_buf = applespi->tx_status;
st_t->len = APPLESPI_STATUS_SIZE;
spi_message_init(msg);
spi_message_add_tail(wt_t, msg);
spi_message_add_tail(dl_t, msg);
spi_message_add_tail(wr_t, msg);
spi_message_add_tail(st_t, msg);
}
static bool applespi_async_outstanding(struct applespi_data *applespi)
{
return applespi->spi_complete[0].complete ||
applespi->spi_complete[1].complete;
}
static void applespi_async_complete(void *context)
{
struct applespi_complete_info *info = context;
struct applespi_data *applespi = info->applespi;
void (*complete)(void *);
unsigned long flags;
spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
complete = info->complete;
info->complete = NULL;
spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
complete(applespi);
spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
if (applespi->cancel_spi && !applespi_async_outstanding(applespi))
wake_up_all(&applespi->drain_complete);
spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
}
static int applespi_async(struct applespi_data *applespi,
struct spi_message *message, void (*complete)(void *))
{
struct applespi_complete_info *info;
int sts;
if (applespi->cancel_spi) {
if (!applespi_async_outstanding(applespi))
wake_up_all(&applespi->drain_complete);
return -ESHUTDOWN;
}
/*
* There can only be at most 2 spi requests in flight, one for "reads"
* and one for "writes".
*/
if (!applespi->spi_complete[0].complete)
info = &applespi->spi_complete[0];
else
info = &applespi->spi_complete[1];
info->complete = complete;
info->applespi = applespi;
message->complete = applespi_async_complete;
message->context = info;
sts = spi_async(applespi->spi, message);
if (sts)
info->complete = NULL;
return sts;
}
static inline bool applespi_check_write_status(struct applespi_data *applespi,
int sts)
{
static u8 status_ok[] = { 0xac, 0x27, 0x68, 0xd5 };
if (sts < 0) {
dev_warn(&applespi->spi->dev, "Error writing to device: %d\n",
sts);
return false;
}
if (memcmp(applespi->tx_status, status_ok, APPLESPI_STATUS_SIZE)) {
dev_warn(&applespi->spi->dev, "Error writing to device: %*ph\n",
APPLESPI_STATUS_SIZE, applespi->tx_status);
return false;
}
return true;
}
#ifdef PRE_SPI_PROPERTIES
struct appleacpi_spi_registration_info {
struct class_interface cif;
struct acpi_device *adev;
struct spi_device *spi;
struct spi_master *spi_master;
struct delayed_work work;
struct notifier_block slave_notifier;
};
struct applespi_acpi_map_entry {
char *name;
size_t field_offset;
};
static const struct applespi_acpi_map_entry applespi_spi_settings_map[] = {
{ "spiSclkPeriod", offsetof(struct spi_settings, spi_sclk_period) },
{ "spiWordSize", offsetof(struct spi_settings, spi_word_size) },
{ "spiBitOrder", offsetof(struct spi_settings, spi_bit_order) },
{ "spiSPO", offsetof(struct spi_settings, spi_spo) },
{ "spiSPH", offsetof(struct spi_settings, spi_sph) },
{ "spiCSDelay", offsetof(struct spi_settings, spi_cs_delay) },
{ "resetA2RUsec", offsetof(struct spi_settings, reset_a2r_usec) },
{ "resetRecUsec", offsetof(struct spi_settings, reset_rec_usec) },
};
static u8 *acpi_dsm_uuid = "a0b5b7c6-1318-441c-b0c9-fe695eaf949b";
static int applespi_find_settings_field(const char *name)
{
int i;
for (i = 0; i < ARRAY_SIZE(applespi_spi_settings_map); i++) {
if (strcmp(applespi_spi_settings_map[i].name, name) == 0)
return applespi_spi_settings_map[i].field_offset;
}
return -1;
}
static int applespi_get_spi_settings(acpi_handle handle,
struct spi_settings *settings)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 13, 0)
guid_t guid, *uuid = &guid;
#else
u8 uuid[16];
#endif
union acpi_object *spi_info;
union acpi_object name;
union acpi_object value;
int i;
int field_off;
u64 *field;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 13, 0)
guid_parse(acpi_dsm_uuid, uuid);
#else
acpi_str_to_uuid(acpi_dsm_uuid, uuid);
#endif
spi_info = acpi_evaluate_dsm(handle, uuid, 1, 1, NULL);
if (!spi_info) {
pr_err("Failed to get SPI info from _DSM method\n");
return -ENODEV;
}
if (spi_info->type != ACPI_TYPE_PACKAGE) {
pr_err("Unexpected data returned from SPI _DSM method: type=%d\n",
spi_info->type);
ACPI_FREE(spi_info);
return -ENODEV;
}
/*
* The data is stored in pairs of items, first a string containing
* the name of the item, followed by an 8-byte buffer containing the
* value in little-endian.
*/
for (i = 0; i < spi_info->package.count - 1; i += 2) {
name = spi_info->package.elements[i];
value = spi_info->package.elements[i + 1];
if (!(name.type == ACPI_TYPE_STRING &&
value.type == ACPI_TYPE_BUFFER &&
value.buffer.length == 8)) {
pr_warn("Unexpected data returned from SPI _DSM method: name.type=%d, value.type=%d\n",
name.type, value.type);
continue;
}
field_off = applespi_find_settings_field(name.string.pointer);
if (field_off < 0) {
pr_debug("Skipping unknown SPI setting '%s'\n",
name.string.pointer);
continue;
}
field = (u64 *)((char *)settings + field_off);
*field = le64_to_cpu(*((__le64 *)value.buffer.pointer));
}
ACPI_FREE(spi_info);
return 0;
}
#else
static int applespi_get_spi_settings(struct applespi_data *applespi)
{
struct acpi_device *adev = ACPI_COMPANION(&applespi->spi->dev);
const union acpi_object *o;
struct spi_settings *settings = &applespi->spi_settings;
if (!acpi_dev_get_property(adev, "spiCSDelay", ACPI_TYPE_BUFFER, &o))
settings->spi_cs_delay = *(u64 *)o->buffer.pointer;
else
dev_warn(&applespi->spi->dev,
"Property spiCSDelay not found\n");
if (!acpi_dev_get_property(adev, "resetA2RUsec", ACPI_TYPE_BUFFER, &o))
settings->reset_a2r_usec = *(u64 *)o->buffer.pointer;
else
dev_warn(&applespi->spi->dev,
"Property resetA2RUsec not found\n");
if (!acpi_dev_get_property(adev, "resetRecUsec", ACPI_TYPE_BUFFER, &o))
settings->reset_rec_usec = *(u64 *)o->buffer.pointer;
else
dev_warn(&applespi->spi->dev,
"Property resetRecUsec not found\n");
dev_dbg(&applespi->spi->dev,
"SPI settings: spi_cs_delay=%llu reset_a2r_usec=%llu reset_rec_usec=%llu\n",
settings->spi_cs_delay, settings->reset_a2r_usec,
settings->reset_rec_usec);
return 0;
}
#endif
static int applespi_setup_spi(struct applespi_data *applespi
#ifdef PRE_SPI_PROPERTIES
, acpi_handle spi_handle
#endif
)
{
int sts;
#ifdef PRE_SPI_PROPERTIES
sts = applespi_get_spi_settings(spi_handle, &applespi->spi_settings);
#else
sts = applespi_get_spi_settings(applespi);
#endif
if (sts)
return sts;
spin_lock_init(&applespi->cmd_msg_lock);
init_waitqueue_head(&applespi->drain_complete);
init_waitqueue_head(&applespi->tp_info_complete);
return 0;
}
static int applespi_enable_spi(struct applespi_data *applespi)
{
acpi_status acpi_sts;
unsigned long long spi_status;
/* check if SPI is already enabled, so we can skip the delay below */
acpi_sts = acpi_evaluate_integer(applespi->sist, NULL, NULL,
&spi_status);
if (ACPI_SUCCESS(acpi_sts) && spi_status)
return 0;
/* SIEN(1) will enable SPI communication */
acpi_sts = acpi_execute_simple_method(applespi->sien, NULL, 1);
if (ACPI_FAILURE(acpi_sts)) {
dev_err(&applespi->spi->dev, "SIEN failed: %s\n",
acpi_format_exception(acpi_sts));
return -ENODEV;
}
/*
* Allow the SPI interface to come up before returning. Without this
* delay, the SPI commands to enable multitouch mode may not reach
* the trackpad controller, causing pointer movement to break upon
* resume from sleep.
*/
msleep(50);
return 0;
}
static int applespi_send_cmd_msg(struct applespi_data *applespi);
static void applespi_msg_complete(struct applespi_data *applespi,
bool is_write_msg, bool is_read_compl)
{
unsigned long flags;
spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
if (is_read_compl)
applespi->read_active = false;
if (is_write_msg)
applespi->write_active = false;
if (applespi->drain && !applespi->write_active)
wake_up_all(&applespi->drain_complete);
if (is_write_msg) {
applespi->cmd_msg_queued = 0;
applespi_send_cmd_msg(applespi);
}
spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
}
static void applespi_async_write_complete(void *context)
{
struct applespi_data *applespi = context;
enum applespi_evt_type evt_type = applespi->cmd_evt_type;
applespi_get_trace_fun(evt_type)(evt_type, PT_WRITE,
applespi->tx_buffer,
APPLESPI_PACKET_SIZE);
applespi_get_trace_fun(evt_type)(evt_type, PT_STATUS,
applespi->tx_status,
APPLESPI_STATUS_SIZE);
udelay(SPI_RW_CHG_DELAY_US);
if (!applespi_check_write_status(applespi, applespi->wr_m.status)) {
/*
* If we got an error, we presumably won't get the expected
* response message either.
*/
applespi_msg_complete(applespi, true, false);
}
}
static int applespi_send_cmd_msg(struct applespi_data *applespi)
{
u16 crc;
int sts;
struct spi_packet *packet = (struct spi_packet *)applespi->tx_buffer;
struct message *message = (struct message *)packet->data;
u16 msg_len;
u8 device;
/* check if draining */
if (applespi->drain)
return 0;
/* check whether send is in progress */
if (applespi->cmd_msg_queued) {
if (ktime_ms_delta(ktime_get(), applespi->cmd_msg_queued) < 1000)
return 0;
dev_warn(&applespi->spi->dev, "Command %d timed out\n",
applespi->cmd_evt_type);
applespi->cmd_msg_queued = 0;
applespi->write_active = false;
}
/* set up packet */
memset(packet, 0, APPLESPI_PACKET_SIZE);
/* are we processing init commands? */
if (applespi->want_tp_info_cmd) {
applespi->want_tp_info_cmd = false;
applespi->want_mt_init_cmd = true;
applespi->cmd_evt_type = ET_CMD_TP_INI;
/* build init command */
device = PACKET_DEV_INFO;