1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * MacBook (Pro) SPI keyboard and touchpad driver
4 *
5 * Copyright (c) 2015-2018 Federico Lorenzi
6 * Copyright (c) 2017-2018 Ronald Tschalär
7 */
8
9 /*
10 * The keyboard and touchpad controller on the MacBookAir6, MacBookPro12,
11 * MacBook8 and newer can be driven either by USB or SPI. However the USB
12 * pins are only connected on the MacBookAir6 and 7 and the MacBookPro12.
13 * All others need this driver. The interface is selected using ACPI methods:
14 *
15 * * UIEN ("USB Interface Enable"): If invoked with argument 1, disables SPI
16 * and enables USB. If invoked with argument 0, disables USB.
17 * * UIST ("USB Interface Status"): Returns 1 if USB is enabled, 0 otherwise.
18 * * SIEN ("SPI Interface Enable"): If invoked with argument 1, disables USB
19 * and enables SPI. If invoked with argument 0, disables SPI.
20 * * SIST ("SPI Interface Status"): Returns 1 if SPI is enabled, 0 otherwise.
21 * * ISOL: Resets the four GPIO pins used for SPI. Intended to be invoked with
22 * argument 1, then once more with argument 0.
23 *
24 * UIEN and UIST are only provided on models where the USB pins are connected.
25 *
26 * SPI-based Protocol
27 * ------------------
28 *
29 * The device and driver exchange messages (struct message); each message is
30 * encapsulated in one or more packets (struct spi_packet). There are two types
31 * of exchanges: reads, and writes. A read is signaled by a GPE, upon which one
32 * message can be read from the device. A write exchange consists of writing a
33 * command message, immediately reading a short status packet, and then, upon
34 * receiving a GPE, reading the response message. Write exchanges cannot be
35 * interleaved, i.e. a new write exchange must not be started till the previous
36 * write exchange is complete. Whether a received message is part of a read or
37 * write exchange is indicated in the encapsulating packet's flags field.
38 *
39 * A single message may be too large to fit in a single packet (which has a
40 * fixed, 256-byte size). In that case it will be split over multiple,
41 * consecutive packets.
42 */
43
44 #include <linux/acpi.h>
45 #include <linux/crc16.h>
46 #include <linux/debugfs.h>
47 #include <linux/delay.h>
48 #include <linux/efi.h>
49 #include <linux/input.h>
50 #include <linux/input/mt.h>
51 #include <linux/ktime.h>
52 #include <linux/leds.h>
53 #include <linux/module.h>
54 #include <linux/spinlock.h>
55 #include <linux/spi/spi.h>
56 #include <linux/wait.h>
57 #include <linux/workqueue.h>
58
59 #include <asm/barrier.h>
60 #include <linux/unaligned.h>
61
62 #define CREATE_TRACE_POINTS
63 #include "applespi.h"
64 #include "applespi_trace.h"
65
66 #define APPLESPI_PACKET_SIZE 256
67 #define APPLESPI_STATUS_SIZE 4
68
69 #define PACKET_TYPE_READ 0x20
70 #define PACKET_TYPE_WRITE 0x40
71 #define PACKET_DEV_KEYB 0x01
72 #define PACKET_DEV_TPAD 0x02
73 #define PACKET_DEV_INFO 0xd0
74
75 #define MAX_ROLLOVER 6
76
77 #define MAX_FINGERS 11
78 #define MAX_FINGER_ORIENTATION 16384
79 #define MAX_PKTS_PER_MSG 2
80
81 #define KBD_BL_LEVEL_MIN 32U
82 #define KBD_BL_LEVEL_MAX 255U
83 #define KBD_BL_LEVEL_SCALE 1000000U
84 #define KBD_BL_LEVEL_ADJ \
85 ((KBD_BL_LEVEL_MAX - KBD_BL_LEVEL_MIN) * KBD_BL_LEVEL_SCALE / 255U)
86
87 #define EFI_BL_LEVEL_NAME L"KeyboardBacklightLevel"
88 #define EFI_BL_LEVEL_GUID EFI_GUID(0xa076d2af, 0x9678, 0x4386, 0x8b, 0x58, 0x1f, 0xc8, 0xef, 0x04, 0x16, 0x19)
89
90 #define APPLE_FLAG_FKEY 0x01
91
92 #define SPI_RW_CHG_DELAY_US 100 /* from experimentation, in µs */
93
94 #define SYNAPTICS_VENDOR_ID 0x06cb
95
96 static unsigned int fnmode = 1;
97 module_param(fnmode, uint, 0644);
98 MODULE_PARM_DESC(fnmode, "Mode of Fn key on Apple keyboards (0 = disabled, [1] = fkeyslast, 2 = fkeysfirst)");
99
100 static unsigned int fnremap;
101 module_param(fnremap, uint, 0644);
102 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)");
103
104 static bool iso_layout;
105 module_param(iso_layout, bool, 0644);
106 MODULE_PARM_DESC(iso_layout, "Enable/Disable hardcoded ISO-layout of the keyboard. ([0] = disabled, 1 = enabled)");
107
108 static char touchpad_dimensions[40];
109 module_param_string(touchpad_dimensions, touchpad_dimensions,
110 sizeof(touchpad_dimensions), 0444);
111 MODULE_PARM_DESC(touchpad_dimensions, "The pixel dimensions of the touchpad, as XxY+W+H .");
112
113 /**
114 * struct keyboard_protocol - keyboard message.
115 * message.type = 0x0110, message.length = 0x000a
116 *
117 * @unknown1: unknown
118 * @modifiers: bit-set of modifier/control keys pressed
119 * @unknown2: unknown
120 * @keys_pressed: the (non-modifier) keys currently pressed
121 * @fn_pressed: whether the fn key is currently pressed
122 * @crc16: crc over the whole message struct (message header +
123 * this struct) minus this @crc16 field
124 */
125 struct keyboard_protocol {
126 u8 unknown1;
127 u8 modifiers;
128 u8 unknown2;
129 u8 keys_pressed[MAX_ROLLOVER];
130 u8 fn_pressed;
131 __le16 crc16;
132 };
133
134 /**
135 * struct tp_finger - single trackpad finger structure, le16-aligned
136 *
137 * @origin: zero when switching track finger
138 * @abs_x: absolute x coordinate
139 * @abs_y: absolute y coordinate
140 * @rel_x: relative x coordinate
141 * @rel_y: relative y coordinate
142 * @tool_major: tool area, major axis
143 * @tool_minor: tool area, minor axis
144 * @orientation: 16384 when point, else 15 bit angle
145 * @touch_major: touch area, major axis
146 * @touch_minor: touch area, minor axis
147 * @unused: zeros
148 * @pressure: pressure on forcetouch touchpad
149 * @multi: one finger: varies, more fingers: constant
150 * @crc16: on last finger: crc over the whole message struct
151 * (i.e. message header + this struct) minus the last
152 * @crc16 field; unknown on all other fingers.
153 */
154 struct tp_finger {
155 __le16 origin;
156 __le16 abs_x;
157 __le16 abs_y;
158 __le16 rel_x;
159 __le16 rel_y;
160 __le16 tool_major;
161 __le16 tool_minor;
162 __le16 orientation;
163 __le16 touch_major;
164 __le16 touch_minor;
165 __le16 unused[2];
166 __le16 pressure;
167 __le16 multi;
168 __le16 crc16;
169 };
170
171 /**
172 * struct touchpad_protocol - touchpad message.
173 * message.type = 0x0210
174 *
175 * @unknown1: unknown
176 * @clicked: 1 if a button-click was detected, 0 otherwise
177 * @unknown2: unknown
178 * @number_of_fingers: the number of fingers being reported in @fingers
179 * @clicked2: same as @clicked
180 * @unknown3: unknown
181 * @fingers: the data for each finger
182 */
183 struct touchpad_protocol {
184 u8 unknown1[1];
185 u8 clicked;
186 u8 unknown2[28];
187 u8 number_of_fingers;
188 u8 clicked2;
189 u8 unknown3[16];
190 struct tp_finger fingers[];
191 };
192
193 /**
194 * struct command_protocol_tp_info - get touchpad info.
195 * message.type = 0x1020, message.length = 0x0000
196 *
197 * @crc16: crc over the whole message struct (message header +
198 * this struct) minus this @crc16 field
199 */
200 struct command_protocol_tp_info {
201 __le16 crc16;
202 };
203
204 /**
205 * struct touchpad_info_protocol - touchpad info response.
206 * message.type = 0x1020, message.length = 0x006e
207 *
208 * @unknown1: unknown
209 * @model_flags: flags (vary by model number, but significance otherwise
210 * unknown)
211 * @model_no: the touchpad model number
212 * @unknown2: unknown
213 * @crc16: crc over the whole message struct (message header +
214 * this struct) minus this @crc16 field
215 */
216 struct touchpad_info_protocol {
217 u8 unknown1[105];
218 u8 model_flags;
219 u8 model_no;
220 u8 unknown2[3];
221 __le16 crc16;
222 };
223
224 /**
225 * struct command_protocol_mt_init - initialize multitouch.
226 * message.type = 0x0252, message.length = 0x0002
227 *
228 * @cmd: value: 0x0102
229 * @crc16: crc over the whole message struct (message header +
230 * this struct) minus this @crc16 field
231 */
232 struct command_protocol_mt_init {
233 __le16 cmd;
234 __le16 crc16;
235 };
236
237 /**
238 * struct command_protocol_capsl - toggle caps-lock led
239 * message.type = 0x0151, message.length = 0x0002
240 *
241 * @unknown: value: 0x01 (length?)
242 * @led: 0 off, 2 on
243 * @crc16: crc over the whole message struct (message header +
244 * this struct) minus this @crc16 field
245 */
246 struct command_protocol_capsl {
247 u8 unknown;
248 u8 led;
249 __le16 crc16;
250 };
251
252 /**
253 * struct command_protocol_bl - set keyboard backlight brightness
254 * message.type = 0xB051, message.length = 0x0006
255 *
256 * @const1: value: 0x01B0
257 * @level: the brightness level to set
258 * @const2: value: 0x0001 (backlight off), 0x01F4 (backlight on)
259 * @crc16: crc over the whole message struct (message header +
260 * this struct) minus this @crc16 field
261 */
262 struct command_protocol_bl {
263 __le16 const1;
264 __le16 level;
265 __le16 const2;
266 __le16 crc16;
267 };
268
269 /**
270 * struct message - a complete spi message.
271 *
272 * Each message begins with fixed header, followed by a message-type specific
273 * payload, and ends with a 16-bit crc. Because of the varying lengths of the
274 * payload, the crc is defined at the end of each payload struct, rather than
275 * in this struct.
276 *
277 * @type: the message type
278 * @zero: always 0
279 * @counter: incremented on each message, rolls over after 255; there is a
280 * separate counter for each message type.
281 * @rsp_buf_len:response buffer length (the exact nature of this field is quite
282 * speculative). On a request/write this is often the same as
283 * @length, though in some cases it has been seen to be much larger
284 * (e.g. 0x400); on a response/read this the same as on the
285 * request; for reads that are not responses it is 0.
286 * @length: length of the remainder of the data in the whole message
287 * structure (after re-assembly in case of being split over
288 * multiple spi-packets), minus the trailing crc. The total size
289 * of the message struct is therefore @length + 10.
290 *
291 * @keyboard: Keyboard message
292 * @touchpad: Touchpad message
293 * @tp_info: Touchpad info (response)
294 * @tp_info_command: Touchpad info (CRC)
295 * @init_mt_command: Initialise Multitouch
296 * @capsl_command: Toggle caps-lock LED
297 * @bl_command: Keyboard brightness
298 * @data: Buffer data
299 */
300 struct message {
301 __le16 type;
302 u8 zero;
303 u8 counter;
304 __le16 rsp_buf_len;
305 __le16 length;
306 union {
307 struct keyboard_protocol keyboard;
308 struct touchpad_protocol touchpad;
309 struct touchpad_info_protocol tp_info;
310 struct command_protocol_tp_info tp_info_command;
311 struct command_protocol_mt_init init_mt_command;
312 struct command_protocol_capsl capsl_command;
313 struct command_protocol_bl bl_command;
314 DECLARE_FLEX_ARRAY(u8, data);
315 };
316 };
317
318 /* type + zero + counter + rsp_buf_len + length */
319 #define MSG_HEADER_SIZE 8
320
321 /**
322 * struct spi_packet - a complete spi packet; always 256 bytes. This carries
323 * the (parts of the) message in the data. But note that this does not
324 * necessarily contain a complete message, as in some cases (e.g. many
325 * fingers pressed) the message is split over multiple packets (see the
326 * @offset, @remaining, and @length fields). In general the data parts in
327 * spi_packet's are concatenated until @remaining is 0, and the result is an
328 * message.
329 *
330 * @flags: 0x40 = write (to device), 0x20 = read (from device); note that
331 * the response to a write still has 0x40.
332 * @device: 1 = keyboard, 2 = touchpad
333 * @offset: specifies the offset of this packet's data in the complete
334 * message; i.e. > 0 indicates this is a continuation packet (in
335 * the second packet for a message split over multiple packets
336 * this would then be the same as the @length in the first packet)
337 * @remaining: number of message bytes remaining in subsequents packets (in
338 * the first packet of a message split over two packets this would
339 * then be the same as the @length in the second packet)
340 * @length: length of the valid data in the @data in this packet
341 * @data: all or part of a message
342 * @crc16: crc over this whole structure minus this @crc16 field. This
343 * covers just this packet, even on multi-packet messages (in
344 * contrast to the crc in the message).
345 */
346 struct spi_packet {
347 u8 flags;
348 u8 device;
349 __le16 offset;
350 __le16 remaining;
351 __le16 length;
352 u8 data[246];
353 __le16 crc16;
354 };
355
356 struct spi_settings {
357 u64 spi_cs_delay; /* cs-to-clk delay in us */
358 u64 reset_a2r_usec; /* active-to-receive delay? */
359 u64 reset_rec_usec; /* ? (cur val: 10) */
360 };
361
362 /* this mimics struct drm_rect */
363 struct applespi_tp_info {
364 int x_min;
365 int y_min;
366 int x_max;
367 int y_max;
368 };
369
370 struct applespi_data {
371 struct spi_device *spi;
372 struct spi_settings spi_settings;
373 struct input_dev *keyboard_input_dev;
374 struct input_dev *touchpad_input_dev;
375
376 u8 *tx_buffer;
377 u8 *tx_status;
378 u8 *rx_buffer;
379
380 u8 *msg_buf;
381 unsigned int saved_msg_len;
382
383 struct applespi_tp_info tp_info;
384
385 u8 last_keys_pressed[MAX_ROLLOVER];
386 u8 last_keys_fn_pressed[MAX_ROLLOVER];
387 u8 last_fn_pressed;
388 struct input_mt_pos pos[MAX_FINGERS];
389 int slots[MAX_FINGERS];
390 int gpe;
391 acpi_handle sien;
392 acpi_handle sist;
393
394 struct spi_transfer dl_t;
395 struct spi_transfer rd_t;
396 struct spi_message rd_m;
397
398 struct spi_transfer ww_t;
399 struct spi_transfer wd_t;
400 struct spi_transfer wr_t;
401 struct spi_transfer st_t;
402 struct spi_message wr_m;
403
404 bool want_tp_info_cmd;
405 bool want_mt_init_cmd;
406 bool want_cl_led_on;
407 bool have_cl_led_on;
408 unsigned int want_bl_level;
409 unsigned int have_bl_level;
410 unsigned int cmd_msg_cntr;
411 /* lock to protect the above parameters and flags below */
412 spinlock_t cmd_msg_lock;
413 ktime_t cmd_msg_queued;
414 enum applespi_evt_type cmd_evt_type;
415
416 struct led_classdev backlight_info;
417
418 bool suspended;
419 bool drain;
420 wait_queue_head_t drain_complete;
421 bool read_active;
422 bool write_active;
423
424 struct work_struct work;
425 struct touchpad_info_protocol rcvd_tp_info;
426
427 struct dentry *debugfs_root;
428 bool debug_tp_dim;
429 char tp_dim_val[40];
430 int tp_dim_min_x;
431 int tp_dim_max_x;
432 int tp_dim_min_y;
433 int tp_dim_max_y;
434 };
435
436 static const unsigned char applespi_scancodes[] = {
437 0, 0, 0, 0,
438 KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J,
439 KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T,
440 KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z,
441 KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0,
442 KEY_ENTER, KEY_ESC, KEY_BACKSPACE, KEY_TAB, KEY_SPACE, KEY_MINUS,
443 KEY_EQUAL, KEY_LEFTBRACE, KEY_RIGHTBRACE, KEY_BACKSLASH, 0,
444 KEY_SEMICOLON, KEY_APOSTROPHE, KEY_GRAVE, KEY_COMMA, KEY_DOT, KEY_SLASH,
445 KEY_CAPSLOCK,
446 KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9,
447 KEY_F10, KEY_F11, KEY_F12, 0, 0, 0, 0, 0, 0, 0, 0, 0,
448 KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP,
449 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_102ND,
450 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
451 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RO, 0, KEY_YEN, 0, 0, 0, 0, 0,
452 0, KEY_KATAKANAHIRAGANA, KEY_MUHENKAN
453 };
454
455 /*
456 * This must have exactly as many entries as there are bits in
457 * struct keyboard_protocol.modifiers .
458 */
459 static const unsigned char applespi_controlcodes[] = {
460 KEY_LEFTCTRL,
461 KEY_LEFTSHIFT,
462 KEY_LEFTALT,
463 KEY_LEFTMETA,
464 0,
465 KEY_RIGHTSHIFT,
466 KEY_RIGHTALT,
467 KEY_RIGHTMETA
468 };
469
470 struct applespi_key_translation {
471 u16 from;
472 u16 to;
473 u8 flags;
474 };
475
476 static const struct applespi_key_translation applespi_fn_codes[] = {
477 { KEY_BACKSPACE, KEY_DELETE },
478 { KEY_ENTER, KEY_INSERT },
479 { KEY_F1, KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
480 { KEY_F2, KEY_BRIGHTNESSUP, APPLE_FLAG_FKEY },
481 { KEY_F3, KEY_SCALE, APPLE_FLAG_FKEY },
482 { KEY_F4, KEY_DASHBOARD, APPLE_FLAG_FKEY },
483 { KEY_F5, KEY_KBDILLUMDOWN, APPLE_FLAG_FKEY },
484 { KEY_F6, KEY_KBDILLUMUP, APPLE_FLAG_FKEY },
485 { KEY_F7, KEY_PREVIOUSSONG, APPLE_FLAG_FKEY },
486 { KEY_F8, KEY_PLAYPAUSE, APPLE_FLAG_FKEY },
487 { KEY_F9, KEY_NEXTSONG, APPLE_FLAG_FKEY },
488 { KEY_F10, KEY_MUTE, APPLE_FLAG_FKEY },
489 { KEY_F11, KEY_VOLUMEDOWN, APPLE_FLAG_FKEY },
490 { KEY_F12, KEY_VOLUMEUP, APPLE_FLAG_FKEY },
491 { KEY_RIGHT, KEY_END },
492 { KEY_LEFT, KEY_HOME },
493 { KEY_DOWN, KEY_PAGEDOWN },
494 { KEY_UP, KEY_PAGEUP },
495 { }
496 };
497
498 static const struct applespi_key_translation apple_iso_keyboard[] = {
499 { KEY_GRAVE, KEY_102ND },
500 { KEY_102ND, KEY_GRAVE },
501 { }
502 };
503
504 struct applespi_tp_model_info {
505 u16 model;
506 struct applespi_tp_info tp_info;
507 };
508
509 static const struct applespi_tp_model_info applespi_tp_models[] = {
510 {
511 .model = 0x04, /* MB8 MB9 MB10 */
512 .tp_info = { -5087, -182, 5579, 6089 },
513 },
514 {
515 .model = 0x05, /* MBP13,1 MBP13,2 MBP14,1 MBP14,2 */
516 .tp_info = { -6243, -170, 6749, 7685 },
517 },
518 {
519 .model = 0x06, /* MBP13,3 MBP14,3 */
520 .tp_info = { -7456, -163, 7976, 9283 },
521 },
522 {}
523 };
524
525 typedef void (*applespi_trace_fun)(enum applespi_evt_type,
526 enum applespi_pkt_type, u8 *, size_t);
527
applespi_get_trace_fun(enum applespi_evt_type type)528 static applespi_trace_fun applespi_get_trace_fun(enum applespi_evt_type type)
529 {
530 switch (type) {
531 case ET_CMD_TP_INI:
532 return trace_applespi_tp_ini_cmd;
533 case ET_CMD_BL:
534 return trace_applespi_backlight_cmd;
535 case ET_CMD_CL:
536 return trace_applespi_caps_lock_cmd;
537 case ET_RD_KEYB:
538 return trace_applespi_keyboard_data;
539 case ET_RD_TPAD:
540 return trace_applespi_touchpad_data;
541 case ET_RD_UNKN:
542 return trace_applespi_unknown_data;
543 default:
544 WARN_ONCE(1, "Unknown msg type %d", type);
545 return trace_applespi_unknown_data;
546 }
547 }
548
applespi_setup_read_txfrs(struct applespi_data * applespi)549 static void applespi_setup_read_txfrs(struct applespi_data *applespi)
550 {
551 struct spi_message *msg = &applespi->rd_m;
552 struct spi_transfer *dl_t = &applespi->dl_t;
553 struct spi_transfer *rd_t = &applespi->rd_t;
554
555 memset(dl_t, 0, sizeof(*dl_t));
556 memset(rd_t, 0, sizeof(*rd_t));
557
558 dl_t->delay.value = applespi->spi_settings.spi_cs_delay;
559 dl_t->delay.unit = SPI_DELAY_UNIT_USECS;
560
561 rd_t->rx_buf = applespi->rx_buffer;
562 rd_t->len = APPLESPI_PACKET_SIZE;
563
564 spi_message_init(msg);
565 spi_message_add_tail(dl_t, msg);
566 spi_message_add_tail(rd_t, msg);
567 }
568
applespi_setup_write_txfrs(struct applespi_data * applespi)569 static void applespi_setup_write_txfrs(struct applespi_data *applespi)
570 {
571 struct spi_message *msg = &applespi->wr_m;
572 struct spi_transfer *wt_t = &applespi->ww_t;
573 struct spi_transfer *dl_t = &applespi->wd_t;
574 struct spi_transfer *wr_t = &applespi->wr_t;
575 struct spi_transfer *st_t = &applespi->st_t;
576
577 memset(wt_t, 0, sizeof(*wt_t));
578 memset(dl_t, 0, sizeof(*dl_t));
579 memset(wr_t, 0, sizeof(*wr_t));
580 memset(st_t, 0, sizeof(*st_t));
581
582 /*
583 * All we need here is a delay at the beginning of the message before
584 * asserting cs. But the current spi API doesn't support this, so we
585 * end up with an extra unnecessary (but harmless) cs assertion and
586 * deassertion.
587 */
588 wt_t->delay.value = SPI_RW_CHG_DELAY_US;
589 wt_t->delay.unit = SPI_DELAY_UNIT_USECS;
590 wt_t->cs_change = 1;
591
592 dl_t->delay.value = applespi->spi_settings.spi_cs_delay;
593 dl_t->delay.unit = SPI_DELAY_UNIT_USECS;
594
595 wr_t->tx_buf = applespi->tx_buffer;
596 wr_t->len = APPLESPI_PACKET_SIZE;
597 wr_t->delay.value = SPI_RW_CHG_DELAY_US;
598 wr_t->delay.unit = SPI_DELAY_UNIT_USECS;
599
600 st_t->rx_buf = applespi->tx_status;
601 st_t->len = APPLESPI_STATUS_SIZE;
602
603 spi_message_init(msg);
604 spi_message_add_tail(wt_t, msg);
605 spi_message_add_tail(dl_t, msg);
606 spi_message_add_tail(wr_t, msg);
607 spi_message_add_tail(st_t, msg);
608 }
609
applespi_async(struct applespi_data * applespi,struct spi_message * message,void (* complete)(void *))610 static int applespi_async(struct applespi_data *applespi,
611 struct spi_message *message, void (*complete)(void *))
612 {
613 message->complete = complete;
614 message->context = applespi;
615
616 return spi_async(applespi->spi, message);
617 }
618
applespi_check_write_status(struct applespi_data * applespi,int sts)619 static inline bool applespi_check_write_status(struct applespi_data *applespi,
620 int sts)
621 {
622 static u8 status_ok[] = { 0xac, 0x27, 0x68, 0xd5 };
623
624 if (sts < 0) {
625 dev_warn(&applespi->spi->dev, "Error writing to device: %d\n",
626 sts);
627 return false;
628 }
629
630 if (memcmp(applespi->tx_status, status_ok, APPLESPI_STATUS_SIZE)) {
631 dev_warn(&applespi->spi->dev, "Error writing to device: %*ph\n",
632 APPLESPI_STATUS_SIZE, applespi->tx_status);
633 return false;
634 }
635
636 return true;
637 }
638
applespi_get_spi_settings(struct applespi_data * applespi)639 static int applespi_get_spi_settings(struct applespi_data *applespi)
640 {
641 struct acpi_device *adev = ACPI_COMPANION(&applespi->spi->dev);
642 const union acpi_object *o;
643 struct spi_settings *settings = &applespi->spi_settings;
644
645 if (!acpi_dev_get_property(adev, "spiCSDelay", ACPI_TYPE_BUFFER, &o))
646 settings->spi_cs_delay = *(u64 *)o->buffer.pointer;
647 else
648 dev_warn(&applespi->spi->dev,
649 "Property spiCSDelay not found\n");
650
651 if (!acpi_dev_get_property(adev, "resetA2RUsec", ACPI_TYPE_BUFFER, &o))
652 settings->reset_a2r_usec = *(u64 *)o->buffer.pointer;
653 else
654 dev_warn(&applespi->spi->dev,
655 "Property resetA2RUsec not found\n");
656
657 if (!acpi_dev_get_property(adev, "resetRecUsec", ACPI_TYPE_BUFFER, &o))
658 settings->reset_rec_usec = *(u64 *)o->buffer.pointer;
659 else
660 dev_warn(&applespi->spi->dev,
661 "Property resetRecUsec not found\n");
662
663 dev_dbg(&applespi->spi->dev,
664 "SPI settings: spi_cs_delay=%llu reset_a2r_usec=%llu reset_rec_usec=%llu\n",
665 settings->spi_cs_delay, settings->reset_a2r_usec,
666 settings->reset_rec_usec);
667
668 return 0;
669 }
670
applespi_setup_spi(struct applespi_data * applespi)671 static int applespi_setup_spi(struct applespi_data *applespi)
672 {
673 int sts;
674
675 sts = applespi_get_spi_settings(applespi);
676 if (sts)
677 return sts;
678
679 spin_lock_init(&applespi->cmd_msg_lock);
680 init_waitqueue_head(&applespi->drain_complete);
681
682 return 0;
683 }
684
applespi_enable_spi(struct applespi_data * applespi)685 static int applespi_enable_spi(struct applespi_data *applespi)
686 {
687 acpi_status acpi_sts;
688 unsigned long long spi_status;
689
690 /* check if SPI is already enabled, so we can skip the delay below */
691 acpi_sts = acpi_evaluate_integer(applespi->sist, NULL, NULL,
692 &spi_status);
693 if (ACPI_SUCCESS(acpi_sts) && spi_status)
694 return 0;
695
696 /* SIEN(1) will enable SPI communication */
697 acpi_sts = acpi_execute_simple_method(applespi->sien, NULL, 1);
698 if (ACPI_FAILURE(acpi_sts)) {
699 dev_err(&applespi->spi->dev, "SIEN failed: %s\n",
700 acpi_format_exception(acpi_sts));
701 return -ENODEV;
702 }
703
704 /*
705 * Allow the SPI interface to come up before returning. Without this
706 * delay, the SPI commands to enable multitouch mode may not reach
707 * the trackpad controller, causing pointer movement to break upon
708 * resume from sleep.
709 */
710 msleep(50);
711
712 return 0;
713 }
714
715 static int applespi_send_cmd_msg(struct applespi_data *applespi);
716
applespi_msg_complete(struct applespi_data * applespi,bool is_write_msg,bool is_read_compl)717 static void applespi_msg_complete(struct applespi_data *applespi,
718 bool is_write_msg, bool is_read_compl)
719 {
720 guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
721
722 if (is_read_compl)
723 applespi->read_active = false;
724 if (is_write_msg)
725 applespi->write_active = false;
726
727 if (applespi->drain && !applespi->write_active)
728 wake_up_all(&applespi->drain_complete);
729
730 if (is_write_msg) {
731 applespi->cmd_msg_queued = 0;
732 applespi_send_cmd_msg(applespi);
733 }
734 }
735
applespi_async_write_complete(void * context)736 static void applespi_async_write_complete(void *context)
737 {
738 struct applespi_data *applespi = context;
739 enum applespi_evt_type evt_type = applespi->cmd_evt_type;
740
741 applespi_get_trace_fun(evt_type)(evt_type, PT_WRITE,
742 applespi->tx_buffer,
743 APPLESPI_PACKET_SIZE);
744 applespi_get_trace_fun(evt_type)(evt_type, PT_STATUS,
745 applespi->tx_status,
746 APPLESPI_STATUS_SIZE);
747
748 udelay(SPI_RW_CHG_DELAY_US);
749
750 if (!applespi_check_write_status(applespi, applespi->wr_m.status)) {
751 /*
752 * If we got an error, we presumably won't get the expected
753 * response message either.
754 */
755 applespi_msg_complete(applespi, true, false);
756 }
757 }
758
applespi_send_cmd_msg(struct applespi_data * applespi)759 static int applespi_send_cmd_msg(struct applespi_data *applespi)
760 {
761 u16 crc;
762 int sts;
763 struct spi_packet *packet = (struct spi_packet *)applespi->tx_buffer;
764 struct message *message = (struct message *)packet->data;
765 u16 msg_len;
766 u8 device;
767
768 /* check if draining */
769 if (applespi->drain)
770 return 0;
771
772 /* check whether send is in progress */
773 if (applespi->cmd_msg_queued) {
774 if (ktime_ms_delta(ktime_get(), applespi->cmd_msg_queued) < 1000)
775 return 0;
776
777 dev_warn(&applespi->spi->dev, "Command %d timed out\n",
778 applespi->cmd_evt_type);
779
780 applespi->cmd_msg_queued = 0;
781 applespi->write_active = false;
782 }
783
784 /* set up packet */
785 memset(packet, 0, APPLESPI_PACKET_SIZE);
786
787 /* are we processing init commands? */
788 if (applespi->want_tp_info_cmd) {
789 applespi->want_tp_info_cmd = false;
790 applespi->want_mt_init_cmd = true;
791 applespi->cmd_evt_type = ET_CMD_TP_INI;
792
793 /* build init command */
794 device = PACKET_DEV_INFO;
795
796 message->type = cpu_to_le16(0x1020);
797 msg_len = sizeof(message->tp_info_command);
798
799 message->zero = 0x02;
800 message->rsp_buf_len = cpu_to_le16(0x0200);
801
802 } else if (applespi->want_mt_init_cmd) {
803 applespi->want_mt_init_cmd = false;
804 applespi->cmd_evt_type = ET_CMD_TP_INI;
805
806 /* build init command */
807 device = PACKET_DEV_TPAD;
808
809 message->type = cpu_to_le16(0x0252);
810 msg_len = sizeof(message->init_mt_command);
811
812 message->init_mt_command.cmd = cpu_to_le16(0x0102);
813
814 /* do we need caps-lock command? */
815 } else if (applespi->want_cl_led_on != applespi->have_cl_led_on) {
816 applespi->have_cl_led_on = applespi->want_cl_led_on;
817 applespi->cmd_evt_type = ET_CMD_CL;
818
819 /* build led command */
820 device = PACKET_DEV_KEYB;
821
822 message->type = cpu_to_le16(0x0151);
823 msg_len = sizeof(message->capsl_command);
824
825 message->capsl_command.unknown = 0x01;
826 message->capsl_command.led = applespi->have_cl_led_on ? 2 : 0;
827
828 /* do we need backlight command? */
829 } else if (applespi->want_bl_level != applespi->have_bl_level) {
830 applespi->have_bl_level = applespi->want_bl_level;
831 applespi->cmd_evt_type = ET_CMD_BL;
832
833 /* build command buffer */
834 device = PACKET_DEV_KEYB;
835
836 message->type = cpu_to_le16(0xB051);
837 msg_len = sizeof(message->bl_command);
838
839 message->bl_command.const1 = cpu_to_le16(0x01B0);
840 message->bl_command.level =
841 cpu_to_le16(applespi->have_bl_level);
842
843 if (applespi->have_bl_level > 0)
844 message->bl_command.const2 = cpu_to_le16(0x01F4);
845 else
846 message->bl_command.const2 = cpu_to_le16(0x0001);
847
848 /* everything's up-to-date */
849 } else {
850 return 0;
851 }
852
853 /* finalize packet */
854 packet->flags = PACKET_TYPE_WRITE;
855 packet->device = device;
856 packet->length = cpu_to_le16(MSG_HEADER_SIZE + msg_len);
857
858 message->counter = applespi->cmd_msg_cntr++ % (U8_MAX + 1);
859
860 message->length = cpu_to_le16(msg_len - 2);
861 if (!message->rsp_buf_len)
862 message->rsp_buf_len = message->length;
863
864 crc = crc16(0, (u8 *)message, le16_to_cpu(packet->length) - 2);
865 put_unaligned_le16(crc, &message->data[msg_len - 2]);
866
867 crc = crc16(0, (u8 *)packet, sizeof(*packet) - 2);
868 packet->crc16 = cpu_to_le16(crc);
869
870 /* send command */
871 sts = applespi_async(applespi, &applespi->wr_m,
872 applespi_async_write_complete);
873 if (sts) {
874 dev_warn(&applespi->spi->dev,
875 "Error queueing async write to device: %d\n", sts);
876 return sts;
877 }
878
879 applespi->cmd_msg_queued = ktime_get_coarse();
880 applespi->write_active = true;
881
882 return 0;
883 }
884
applespi_init(struct applespi_data * applespi,bool is_resume)885 static void applespi_init(struct applespi_data *applespi, bool is_resume)
886 {
887 guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
888
889 if (is_resume)
890 applespi->want_mt_init_cmd = true;
891 else
892 applespi->want_tp_info_cmd = true;
893 applespi_send_cmd_msg(applespi);
894 }
895
applespi_set_capsl_led(struct applespi_data * applespi,bool capslock_on)896 static int applespi_set_capsl_led(struct applespi_data *applespi,
897 bool capslock_on)
898 {
899 guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
900
901 applespi->want_cl_led_on = capslock_on;
902 return applespi_send_cmd_msg(applespi);
903 }
904
applespi_set_bl_level(struct led_classdev * led_cdev,enum led_brightness value)905 static void applespi_set_bl_level(struct led_classdev *led_cdev,
906 enum led_brightness value)
907 {
908 struct applespi_data *applespi =
909 container_of(led_cdev, struct applespi_data, backlight_info);
910
911 guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
912
913 if (value == 0) {
914 applespi->want_bl_level = value;
915 } else {
916 /*
917 * The backlight does not turn on till level 32, so we scale
918 * the range here so that from a user's perspective it turns
919 * on at 1.
920 */
921 applespi->want_bl_level =
922 ((value * KBD_BL_LEVEL_ADJ) / KBD_BL_LEVEL_SCALE +
923 KBD_BL_LEVEL_MIN);
924 }
925
926 applespi_send_cmd_msg(applespi);
927 }
928
applespi_event(struct input_dev * dev,unsigned int type,unsigned int code,int value)929 static int applespi_event(struct input_dev *dev, unsigned int type,
930 unsigned int code, int value)
931 {
932 struct applespi_data *applespi = input_get_drvdata(dev);
933
934 switch (type) {
935 case EV_LED:
936 applespi_set_capsl_led(applespi, !!test_bit(LED_CAPSL, dev->led));
937 return 0;
938 }
939
940 return -EINVAL;
941 }
942
943 /* lifted from the BCM5974 driver and renamed from raw2int */
944 /* convert 16-bit little endian to signed integer */
le16_to_int(__le16 x)945 static inline int le16_to_int(__le16 x)
946 {
947 return (signed short)le16_to_cpu(x);
948 }
949
applespi_debug_update_dimensions(struct applespi_data * applespi,const struct tp_finger * f)950 static void applespi_debug_update_dimensions(struct applespi_data *applespi,
951 const struct tp_finger *f)
952 {
953 applespi->tp_dim_min_x = min(applespi->tp_dim_min_x,
954 le16_to_int(f->abs_x));
955 applespi->tp_dim_max_x = max(applespi->tp_dim_max_x,
956 le16_to_int(f->abs_x));
957 applespi->tp_dim_min_y = min(applespi->tp_dim_min_y,
958 le16_to_int(f->abs_y));
959 applespi->tp_dim_max_y = max(applespi->tp_dim_max_y,
960 le16_to_int(f->abs_y));
961 }
962
applespi_tp_dim_open(struct inode * inode,struct file * file)963 static int applespi_tp_dim_open(struct inode *inode, struct file *file)
964 {
965 struct applespi_data *applespi = inode->i_private;
966
967 file->private_data = applespi;
968
969 snprintf(applespi->tp_dim_val, sizeof(applespi->tp_dim_val),
970 "0x%.4x %dx%d+%u+%u\n",
971 applespi->touchpad_input_dev->id.product,
972 applespi->tp_dim_min_x, applespi->tp_dim_min_y,
973 applespi->tp_dim_max_x - applespi->tp_dim_min_x,
974 applespi->tp_dim_max_y - applespi->tp_dim_min_y);
975
976 return nonseekable_open(inode, file);
977 }
978
applespi_tp_dim_read(struct file * file,char __user * buf,size_t len,loff_t * off)979 static ssize_t applespi_tp_dim_read(struct file *file, char __user *buf,
980 size_t len, loff_t *off)
981 {
982 struct applespi_data *applespi = file->private_data;
983
984 return simple_read_from_buffer(buf, len, off, applespi->tp_dim_val,
985 strlen(applespi->tp_dim_val));
986 }
987
988 static const struct file_operations applespi_tp_dim_fops = {
989 .owner = THIS_MODULE,
990 .open = applespi_tp_dim_open,
991 .read = applespi_tp_dim_read,
992 };
993
report_finger_data(struct input_dev * input,int slot,const struct input_mt_pos * pos,const struct tp_finger * f)994 static void report_finger_data(struct input_dev *input, int slot,
995 const struct input_mt_pos *pos,
996 const struct tp_finger *f)
997 {
998 input_mt_slot(input, slot);
999 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
1000
1001 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
1002 le16_to_int(f->touch_major) << 1);
1003 input_report_abs(input, ABS_MT_TOUCH_MINOR,
1004 le16_to_int(f->touch_minor) << 1);
1005 input_report_abs(input, ABS_MT_WIDTH_MAJOR,
1006 le16_to_int(f->tool_major) << 1);
1007 input_report_abs(input, ABS_MT_WIDTH_MINOR,
1008 le16_to_int(f->tool_minor) << 1);
1009 input_report_abs(input, ABS_MT_ORIENTATION,
1010 MAX_FINGER_ORIENTATION - le16_to_int(f->orientation));
1011 input_report_abs(input, ABS_MT_POSITION_X, pos->x);
1012 input_report_abs(input, ABS_MT_POSITION_Y, pos->y);
1013 }
1014
report_tp_state(struct applespi_data * applespi,struct touchpad_protocol * t)1015 static void report_tp_state(struct applespi_data *applespi,
1016 struct touchpad_protocol *t)
1017 {
1018 const struct tp_finger *f;
1019 struct input_dev *input;
1020 const struct applespi_tp_info *tp_info = &applespi->tp_info;
1021 int i, n;
1022
1023 /* touchpad_input_dev is set async in worker */
1024 input = smp_load_acquire(&applespi->touchpad_input_dev);
1025 if (!input)
1026 return; /* touchpad isn't initialized yet */
1027
1028 n = 0;
1029
1030 for (i = 0; i < t->number_of_fingers; i++) {
1031 f = &t->fingers[i];
1032 if (le16_to_int(f->touch_major) == 0)
1033 continue;
1034 applespi->pos[n].x = le16_to_int(f->abs_x);
1035 applespi->pos[n].y = tp_info->y_min + tp_info->y_max -
1036 le16_to_int(f->abs_y);
1037 n++;
1038
1039 if (applespi->debug_tp_dim)
1040 applespi_debug_update_dimensions(applespi, f);
1041 }
1042
1043 input_mt_assign_slots(input, applespi->slots, applespi->pos, n, 0);
1044
1045 for (i = 0; i < n; i++)
1046 report_finger_data(input, applespi->slots[i],
1047 &applespi->pos[i], &t->fingers[i]);
1048
1049 input_mt_sync_frame(input);
1050 input_report_key(input, BTN_LEFT, t->clicked);
1051
1052 input_sync(input);
1053 }
1054
1055 static const struct applespi_key_translation *
applespi_find_translation(const struct applespi_key_translation * table,u16 key)1056 applespi_find_translation(const struct applespi_key_translation *table, u16 key)
1057 {
1058 const struct applespi_key_translation *trans;
1059
1060 for (trans = table; trans->from; trans++)
1061 if (trans->from == key)
1062 return trans;
1063
1064 return NULL;
1065 }
1066
applespi_translate_fn_key(unsigned int key,int fn_pressed)1067 static unsigned int applespi_translate_fn_key(unsigned int key, int fn_pressed)
1068 {
1069 const struct applespi_key_translation *trans;
1070 int do_translate;
1071
1072 trans = applespi_find_translation(applespi_fn_codes, key);
1073 if (trans) {
1074 if (trans->flags & APPLE_FLAG_FKEY)
1075 do_translate = (fnmode == 2 && fn_pressed) ||
1076 (fnmode == 1 && !fn_pressed);
1077 else
1078 do_translate = fn_pressed;
1079
1080 if (do_translate)
1081 key = trans->to;
1082 }
1083
1084 return key;
1085 }
1086
applespi_translate_iso_layout(unsigned int key)1087 static unsigned int applespi_translate_iso_layout(unsigned int key)
1088 {
1089 const struct applespi_key_translation *trans;
1090
1091 trans = applespi_find_translation(apple_iso_keyboard, key);
1092 if (trans)
1093 key = trans->to;
1094
1095 return key;
1096 }
1097
applespi_code_to_key(u8 code,int fn_pressed)1098 static unsigned int applespi_code_to_key(u8 code, int fn_pressed)
1099 {
1100 unsigned int key = applespi_scancodes[code];
1101
1102 if (fnmode)
1103 key = applespi_translate_fn_key(key, fn_pressed);
1104 if (iso_layout)
1105 key = applespi_translate_iso_layout(key);
1106 return key;
1107 }
1108
1109 static void
applespi_remap_fn_key(struct keyboard_protocol * keyboard_protocol)1110 applespi_remap_fn_key(struct keyboard_protocol *keyboard_protocol)
1111 {
1112 unsigned char tmp;
1113 u8 bit = BIT((fnremap - 1) & 0x07);
1114
1115 if (!fnremap || fnremap > ARRAY_SIZE(applespi_controlcodes) ||
1116 !applespi_controlcodes[fnremap - 1])
1117 return;
1118
1119 tmp = keyboard_protocol->fn_pressed;
1120 keyboard_protocol->fn_pressed = !!(keyboard_protocol->modifiers & bit);
1121 if (tmp)
1122 keyboard_protocol->modifiers |= bit;
1123 else
1124 keyboard_protocol->modifiers &= ~bit;
1125 }
1126
1127 static void
applespi_handle_keyboard_event(struct applespi_data * applespi,struct keyboard_protocol * keyboard_protocol)1128 applespi_handle_keyboard_event(struct applespi_data *applespi,
1129 struct keyboard_protocol *keyboard_protocol)
1130 {
1131 unsigned int key;
1132 int i;
1133
1134 compiletime_assert(ARRAY_SIZE(applespi_controlcodes) ==
1135 sizeof_field(struct keyboard_protocol, modifiers) * 8,
1136 "applespi_controlcodes has wrong number of entries");
1137
1138 /* check for rollover overflow, which is signalled by all keys == 1 */
1139 if (!memchr_inv(keyboard_protocol->keys_pressed, 1, MAX_ROLLOVER))
1140 return;
1141
1142 /* remap fn key if desired */
1143 applespi_remap_fn_key(keyboard_protocol);
1144
1145 /* check released keys */
1146 for (i = 0; i < MAX_ROLLOVER; i++) {
1147 if (memchr(keyboard_protocol->keys_pressed,
1148 applespi->last_keys_pressed[i], MAX_ROLLOVER))
1149 continue; /* key is still pressed */
1150
1151 key = applespi_code_to_key(applespi->last_keys_pressed[i],
1152 applespi->last_keys_fn_pressed[i]);
1153 input_report_key(applespi->keyboard_input_dev, key, 0);
1154 applespi->last_keys_fn_pressed[i] = 0;
1155 }
1156
1157 /* check pressed keys */
1158 for (i = 0; i < MAX_ROLLOVER; i++) {
1159 if (keyboard_protocol->keys_pressed[i] <
1160 ARRAY_SIZE(applespi_scancodes) &&
1161 keyboard_protocol->keys_pressed[i] > 0) {
1162 key = applespi_code_to_key(
1163 keyboard_protocol->keys_pressed[i],
1164 keyboard_protocol->fn_pressed);
1165 input_report_key(applespi->keyboard_input_dev, key, 1);
1166 applespi->last_keys_fn_pressed[i] =
1167 keyboard_protocol->fn_pressed;
1168 }
1169 }
1170
1171 /* check control keys */
1172 for (i = 0; i < ARRAY_SIZE(applespi_controlcodes); i++) {
1173 if (keyboard_protocol->modifiers & BIT(i))
1174 input_report_key(applespi->keyboard_input_dev,
1175 applespi_controlcodes[i], 1);
1176 else
1177 input_report_key(applespi->keyboard_input_dev,
1178 applespi_controlcodes[i], 0);
1179 }
1180
1181 /* check function key */
1182 if (keyboard_protocol->fn_pressed && !applespi->last_fn_pressed)
1183 input_report_key(applespi->keyboard_input_dev, KEY_FN, 1);
1184 else if (!keyboard_protocol->fn_pressed && applespi->last_fn_pressed)
1185 input_report_key(applespi->keyboard_input_dev, KEY_FN, 0);
1186 applespi->last_fn_pressed = keyboard_protocol->fn_pressed;
1187
1188 /* done */
1189 input_sync(applespi->keyboard_input_dev);
1190 memcpy(&applespi->last_keys_pressed, keyboard_protocol->keys_pressed,
1191 sizeof(applespi->last_keys_pressed));
1192 }
1193
applespi_find_touchpad_info(u8 model)1194 static const struct applespi_tp_info *applespi_find_touchpad_info(u8 model)
1195 {
1196 const struct applespi_tp_model_info *info;
1197
1198 for (info = applespi_tp_models; info->model; info++) {
1199 if (info->model == model)
1200 return &info->tp_info;
1201 }
1202
1203 return NULL;
1204 }
1205
1206 static int
applespi_register_touchpad_device(struct applespi_data * applespi,struct touchpad_info_protocol * rcvd_tp_info)1207 applespi_register_touchpad_device(struct applespi_data *applespi,
1208 struct touchpad_info_protocol *rcvd_tp_info)
1209 {
1210 const struct applespi_tp_info *tp_info;
1211 struct input_dev *touchpad_input_dev;
1212 int sts;
1213
1214 /* set up touchpad dimensions */
1215 tp_info = applespi_find_touchpad_info(rcvd_tp_info->model_no);
1216 if (!tp_info) {
1217 dev_warn(&applespi->spi->dev,
1218 "Unknown touchpad model %x - falling back to MB8 touchpad\n",
1219 rcvd_tp_info->model_no);
1220 tp_info = &applespi_tp_models[0].tp_info;
1221 }
1222
1223 applespi->tp_info = *tp_info;
1224
1225 if (touchpad_dimensions[0]) {
1226 int x, y, w, h;
1227
1228 sts = sscanf(touchpad_dimensions, "%dx%d+%u+%u", &x, &y, &w, &h);
1229 if (sts == 4) {
1230 dev_info(&applespi->spi->dev,
1231 "Overriding touchpad dimensions from module param\n");
1232 applespi->tp_info.x_min = x;
1233 applespi->tp_info.y_min = y;
1234 applespi->tp_info.x_max = x + w;
1235 applespi->tp_info.y_max = y + h;
1236 } else {
1237 dev_warn(&applespi->spi->dev,
1238 "Invalid touchpad dimensions '%s': must be in the form XxY+W+H\n",
1239 touchpad_dimensions);
1240 touchpad_dimensions[0] = '\0';
1241 }
1242 }
1243 if (!touchpad_dimensions[0]) {
1244 snprintf(touchpad_dimensions, sizeof(touchpad_dimensions),
1245 "%dx%d+%u+%u",
1246 applespi->tp_info.x_min,
1247 applespi->tp_info.y_min,
1248 applespi->tp_info.x_max - applespi->tp_info.x_min,
1249 applespi->tp_info.y_max - applespi->tp_info.y_min);
1250 }
1251
1252 /* create touchpad input device */
1253 touchpad_input_dev = devm_input_allocate_device(&applespi->spi->dev);
1254 if (!touchpad_input_dev) {
1255 dev_err(&applespi->spi->dev,
1256 "Failed to allocate touchpad input device\n");
1257 return -ENOMEM;
1258 }
1259
1260 touchpad_input_dev->name = "Apple SPI Touchpad";
1261 touchpad_input_dev->phys = "applespi/input1";
1262 touchpad_input_dev->dev.parent = &applespi->spi->dev;
1263 touchpad_input_dev->id.bustype = BUS_SPI;
1264 touchpad_input_dev->id.vendor = SYNAPTICS_VENDOR_ID;
1265 touchpad_input_dev->id.product =
1266 rcvd_tp_info->model_no << 8 | rcvd_tp_info->model_flags;
1267
1268 /* basic properties */
1269 input_set_capability(touchpad_input_dev, EV_REL, REL_X);
1270 input_set_capability(touchpad_input_dev, EV_REL, REL_Y);
1271
1272 __set_bit(INPUT_PROP_POINTER, touchpad_input_dev->propbit);
1273 __set_bit(INPUT_PROP_BUTTONPAD, touchpad_input_dev->propbit);
1274
1275 /* finger touch area */
1276 input_set_abs_params(touchpad_input_dev, ABS_MT_TOUCH_MAJOR,
1277 0, 5000, 0, 0);
1278 input_set_abs_params(touchpad_input_dev, ABS_MT_TOUCH_MINOR,
1279 0, 5000, 0, 0);
1280
1281 /* finger approach area */
1282 input_set_abs_params(touchpad_input_dev, ABS_MT_WIDTH_MAJOR,
1283 0, 5000, 0, 0);
1284 input_set_abs_params(touchpad_input_dev, ABS_MT_WIDTH_MINOR,
1285 0, 5000, 0, 0);
1286
1287 /* finger orientation */
1288 input_set_abs_params(touchpad_input_dev, ABS_MT_ORIENTATION,
1289 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION,
1290 0, 0);
1291
1292 /* finger position */
1293 input_set_abs_params(touchpad_input_dev, ABS_MT_POSITION_X,
1294 applespi->tp_info.x_min, applespi->tp_info.x_max,
1295 0, 0);
1296 input_set_abs_params(touchpad_input_dev, ABS_MT_POSITION_Y,
1297 applespi->tp_info.y_min, applespi->tp_info.y_max,
1298 0, 0);
1299
1300 /* touchpad button */
1301 input_set_capability(touchpad_input_dev, EV_KEY, BTN_LEFT);
1302
1303 /* multitouch */
1304 sts = input_mt_init_slots(touchpad_input_dev, MAX_FINGERS,
1305 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
1306 INPUT_MT_TRACK);
1307 if (sts) {
1308 dev_err(&applespi->spi->dev,
1309 "failed to initialize slots: %d", sts);
1310 return sts;
1311 }
1312
1313 /* register input device */
1314 sts = input_register_device(touchpad_input_dev);
1315 if (sts) {
1316 dev_err(&applespi->spi->dev,
1317 "Unable to register touchpad input device (%d)\n", sts);
1318 return sts;
1319 }
1320
1321 /* touchpad_input_dev is read async in spi callback */
1322 smp_store_release(&applespi->touchpad_input_dev, touchpad_input_dev);
1323
1324 return 0;
1325 }
1326
applespi_worker(struct work_struct * work)1327 static void applespi_worker(struct work_struct *work)
1328 {
1329 struct applespi_data *applespi =
1330 container_of(work, struct applespi_data, work);
1331
1332 applespi_register_touchpad_device(applespi, &applespi->rcvd_tp_info);
1333 }
1334
applespi_handle_cmd_response(struct applespi_data * applespi,struct spi_packet * packet,struct message * message)1335 static void applespi_handle_cmd_response(struct applespi_data *applespi,
1336 struct spi_packet *packet,
1337 struct message *message)
1338 {
1339 if (packet->device == PACKET_DEV_INFO &&
1340 le16_to_cpu(message->type) == 0x1020) {
1341 /*
1342 * We're not allowed to sleep here, but registering an input
1343 * device can sleep.
1344 */
1345 applespi->rcvd_tp_info = message->tp_info;
1346 schedule_work(&applespi->work);
1347 return;
1348 }
1349
1350 if (le16_to_cpu(message->length) != 0x0000) {
1351 dev_warn_ratelimited(&applespi->spi->dev,
1352 "Received unexpected write response: length=%x\n",
1353 le16_to_cpu(message->length));
1354 return;
1355 }
1356
1357 if (packet->device == PACKET_DEV_TPAD &&
1358 le16_to_cpu(message->type) == 0x0252 &&
1359 le16_to_cpu(message->rsp_buf_len) == 0x0002)
1360 dev_info(&applespi->spi->dev, "modeswitch done.\n");
1361 }
1362
applespi_verify_crc(struct applespi_data * applespi,u8 * buffer,size_t buflen)1363 static bool applespi_verify_crc(struct applespi_data *applespi, u8 *buffer,
1364 size_t buflen)
1365 {
1366 u16 crc;
1367
1368 crc = crc16(0, buffer, buflen);
1369 if (crc) {
1370 dev_warn_ratelimited(&applespi->spi->dev,
1371 "Received corrupted packet (crc mismatch)\n");
1372 trace_applespi_bad_crc(ET_RD_CRC, READ, buffer, buflen);
1373
1374 return false;
1375 }
1376
1377 return true;
1378 }
1379
applespi_debug_print_read_packet(struct applespi_data * applespi,struct spi_packet * packet)1380 static void applespi_debug_print_read_packet(struct applespi_data *applespi,
1381 struct spi_packet *packet)
1382 {
1383 unsigned int evt_type;
1384
1385 if (packet->flags == PACKET_TYPE_READ &&
1386 packet->device == PACKET_DEV_KEYB)
1387 evt_type = ET_RD_KEYB;
1388 else if (packet->flags == PACKET_TYPE_READ &&
1389 packet->device == PACKET_DEV_TPAD)
1390 evt_type = ET_RD_TPAD;
1391 else if (packet->flags == PACKET_TYPE_WRITE)
1392 evt_type = applespi->cmd_evt_type;
1393 else
1394 evt_type = ET_RD_UNKN;
1395
1396 applespi_get_trace_fun(evt_type)(evt_type, PT_READ, applespi->rx_buffer,
1397 APPLESPI_PACKET_SIZE);
1398 }
1399
applespi_got_data(struct applespi_data * applespi)1400 static void applespi_got_data(struct applespi_data *applespi)
1401 {
1402 struct spi_packet *packet;
1403 struct message *message;
1404 unsigned int msg_len;
1405 unsigned int off;
1406 unsigned int rem;
1407 unsigned int len;
1408
1409 /* process packet header */
1410 if (!applespi_verify_crc(applespi, applespi->rx_buffer,
1411 APPLESPI_PACKET_SIZE)) {
1412 guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
1413
1414 if (applespi->drain) {
1415 applespi->read_active = false;
1416 applespi->write_active = false;
1417
1418 wake_up_all(&applespi->drain_complete);
1419 }
1420
1421 return;
1422 }
1423
1424 packet = (struct spi_packet *)applespi->rx_buffer;
1425
1426 applespi_debug_print_read_packet(applespi, packet);
1427
1428 off = le16_to_cpu(packet->offset);
1429 rem = le16_to_cpu(packet->remaining);
1430 len = le16_to_cpu(packet->length);
1431
1432 if (len > sizeof(packet->data)) {
1433 dev_warn_ratelimited(&applespi->spi->dev,
1434 "Received corrupted packet (invalid packet length %u)\n",
1435 len);
1436 goto msg_complete;
1437 }
1438
1439 /* handle multi-packet messages */
1440 if (rem > 0 || off > 0) {
1441 if (off != applespi->saved_msg_len) {
1442 dev_warn_ratelimited(&applespi->spi->dev,
1443 "Received unexpected offset (got %u, expected %u)\n",
1444 off, applespi->saved_msg_len);
1445 goto msg_complete;
1446 }
1447
1448 if (off + rem > MAX_PKTS_PER_MSG * APPLESPI_PACKET_SIZE) {
1449 dev_warn_ratelimited(&applespi->spi->dev,
1450 "Received message too large (size %u)\n",
1451 off + rem);
1452 goto msg_complete;
1453 }
1454
1455 if (off + len > MAX_PKTS_PER_MSG * APPLESPI_PACKET_SIZE) {
1456 dev_warn_ratelimited(&applespi->spi->dev,
1457 "Received message too large (size %u)\n",
1458 off + len);
1459 goto msg_complete;
1460 }
1461
1462 memcpy(applespi->msg_buf + off, &packet->data, len);
1463 applespi->saved_msg_len += len;
1464
1465 if (rem > 0)
1466 return;
1467
1468 message = (struct message *)applespi->msg_buf;
1469 msg_len = applespi->saved_msg_len;
1470 } else {
1471 message = (struct message *)&packet->data;
1472 msg_len = len;
1473 }
1474
1475 /* got complete message - verify */
1476 if (!applespi_verify_crc(applespi, (u8 *)message, msg_len))
1477 goto msg_complete;
1478
1479 if (le16_to_cpu(message->length) != msg_len - MSG_HEADER_SIZE - 2) {
1480 dev_warn_ratelimited(&applespi->spi->dev,
1481 "Received corrupted packet (invalid message length %u - expected %u)\n",
1482 le16_to_cpu(message->length),
1483 msg_len - MSG_HEADER_SIZE - 2);
1484 goto msg_complete;
1485 }
1486
1487 /* handle message */
1488 if (packet->flags == PACKET_TYPE_READ &&
1489 packet->device == PACKET_DEV_KEYB) {
1490 applespi_handle_keyboard_event(applespi, &message->keyboard);
1491
1492 } else if (packet->flags == PACKET_TYPE_READ &&
1493 packet->device == PACKET_DEV_TPAD) {
1494 struct touchpad_protocol *tp;
1495 size_t tp_len;
1496
1497 tp = &message->touchpad;
1498 tp_len = struct_size(tp, fingers, tp->number_of_fingers);
1499
1500 if (le16_to_cpu(message->length) + 2 != tp_len) {
1501 dev_warn_ratelimited(&applespi->spi->dev,
1502 "Received corrupted packet (invalid message length %u - num-fingers %u, tp-len %zu)\n",
1503 le16_to_cpu(message->length),
1504 tp->number_of_fingers, tp_len);
1505 goto msg_complete;
1506 }
1507
1508 if (tp->number_of_fingers > MAX_FINGERS) {
1509 dev_warn_ratelimited(&applespi->spi->dev,
1510 "Number of reported fingers (%u) exceeds max (%u))\n",
1511 tp->number_of_fingers,
1512 MAX_FINGERS);
1513 tp->number_of_fingers = MAX_FINGERS;
1514 }
1515
1516 report_tp_state(applespi, tp);
1517
1518 } else if (packet->flags == PACKET_TYPE_WRITE) {
1519 applespi_handle_cmd_response(applespi, packet, message);
1520 }
1521
1522 msg_complete:
1523 applespi->saved_msg_len = 0;
1524
1525 applespi_msg_complete(applespi, packet->flags == PACKET_TYPE_WRITE,
1526 true);
1527 }
1528
applespi_async_read_complete(void * context)1529 static void applespi_async_read_complete(void *context)
1530 {
1531 struct applespi_data *applespi = context;
1532
1533 if (applespi->rd_m.status < 0) {
1534 dev_warn(&applespi->spi->dev, "Error reading from device: %d\n",
1535 applespi->rd_m.status);
1536 /*
1537 * We don't actually know if this was a pure read, or a response
1538 * to a write. But this is a rare error condition that should
1539 * never occur, so clearing both flags to avoid deadlock.
1540 */
1541 applespi_msg_complete(applespi, true, true);
1542 } else {
1543 applespi_got_data(applespi);
1544 }
1545
1546 acpi_finish_gpe(NULL, applespi->gpe);
1547 }
1548
applespi_notify(acpi_handle gpe_device,u32 gpe,void * context)1549 static u32 applespi_notify(acpi_handle gpe_device, u32 gpe, void *context)
1550 {
1551 struct applespi_data *applespi = context;
1552 int sts;
1553
1554 trace_applespi_irq_received(ET_RD_IRQ, PT_READ);
1555
1556 guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
1557
1558 if (!applespi->suspended) {
1559 sts = applespi_async(applespi, &applespi->rd_m,
1560 applespi_async_read_complete);
1561 if (sts)
1562 dev_warn(&applespi->spi->dev,
1563 "Error queueing async read to device: %d\n",
1564 sts);
1565 else
1566 applespi->read_active = true;
1567 }
1568
1569 return ACPI_INTERRUPT_HANDLED;
1570 }
1571
applespi_get_saved_bl_level(struct applespi_data * applespi)1572 static int applespi_get_saved_bl_level(struct applespi_data *applespi)
1573 {
1574 efi_status_t sts = EFI_NOT_FOUND;
1575 u16 efi_data = 0;
1576 unsigned long efi_data_len = sizeof(efi_data);
1577
1578 if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
1579 sts = efi.get_variable(EFI_BL_LEVEL_NAME, &EFI_BL_LEVEL_GUID,
1580 NULL, &efi_data_len, &efi_data);
1581 if (sts != EFI_SUCCESS && sts != EFI_NOT_FOUND)
1582 dev_warn(&applespi->spi->dev,
1583 "Error getting backlight level from EFI vars: 0x%lx\n",
1584 sts);
1585
1586 return sts != EFI_SUCCESS ? -ENODEV : efi_data;
1587 }
1588
applespi_save_bl_level(struct applespi_data * applespi,unsigned int level)1589 static void applespi_save_bl_level(struct applespi_data *applespi,
1590 unsigned int level)
1591 {
1592 efi_status_t sts = EFI_UNSUPPORTED;
1593 u32 efi_attr;
1594 u16 efi_data;
1595
1596 efi_data = (u16)level;
1597 efi_attr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |
1598 EFI_VARIABLE_RUNTIME_ACCESS;
1599
1600 if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE))
1601 sts = efi.set_variable(EFI_BL_LEVEL_NAME, &EFI_BL_LEVEL_GUID,
1602 efi_attr, sizeof(efi_data), &efi_data);
1603 if (sts != EFI_SUCCESS)
1604 dev_warn(&applespi->spi->dev,
1605 "Error saving backlight level to EFI vars: 0x%lx\n", sts);
1606 }
1607
applespi_probe(struct spi_device * spi)1608 static int applespi_probe(struct spi_device *spi)
1609 {
1610 struct applespi_data *applespi;
1611 acpi_handle spi_handle = ACPI_HANDLE(&spi->dev);
1612 acpi_status acpi_sts;
1613 int sts, i;
1614 unsigned long long gpe, usb_status;
1615
1616 /* check if the USB interface is present and enabled already */
1617 acpi_sts = acpi_evaluate_integer(spi_handle, "UIST", NULL, &usb_status);
1618 if (ACPI_SUCCESS(acpi_sts) && usb_status) {
1619 /* let the USB driver take over instead */
1620 dev_info(&spi->dev, "USB interface already enabled\n");
1621 return -ENODEV;
1622 }
1623
1624 /* allocate driver data */
1625 applespi = devm_kzalloc(&spi->dev, sizeof(*applespi), GFP_KERNEL);
1626 if (!applespi)
1627 return -ENOMEM;
1628
1629 applespi->spi = spi;
1630
1631 INIT_WORK(&applespi->work, applespi_worker);
1632
1633 /* store the driver data */
1634 spi_set_drvdata(spi, applespi);
1635
1636 /* create our buffers */
1637 applespi->tx_buffer = devm_kmalloc(&spi->dev, APPLESPI_PACKET_SIZE,
1638 GFP_KERNEL);
1639 applespi->tx_status = devm_kmalloc(&spi->dev, APPLESPI_STATUS_SIZE,
1640 GFP_KERNEL);
1641 applespi->rx_buffer = devm_kmalloc(&spi->dev, APPLESPI_PACKET_SIZE,
1642 GFP_KERNEL);
1643 applespi->msg_buf = devm_kmalloc_array(&spi->dev, MAX_PKTS_PER_MSG,
1644 APPLESPI_PACKET_SIZE,
1645 GFP_KERNEL);
1646
1647 if (!applespi->tx_buffer || !applespi->tx_status ||
1648 !applespi->rx_buffer || !applespi->msg_buf)
1649 return -ENOMEM;
1650
1651 /* set up our spi messages */
1652 applespi_setup_read_txfrs(applespi);
1653 applespi_setup_write_txfrs(applespi);
1654
1655 /* cache ACPI method handles */
1656 acpi_sts = acpi_get_handle(spi_handle, "SIEN", &applespi->sien);
1657 if (ACPI_FAILURE(acpi_sts)) {
1658 dev_err(&applespi->spi->dev,
1659 "Failed to get SIEN ACPI method handle: %s\n",
1660 acpi_format_exception(acpi_sts));
1661 return -ENODEV;
1662 }
1663
1664 acpi_sts = acpi_get_handle(spi_handle, "SIST", &applespi->sist);
1665 if (ACPI_FAILURE(acpi_sts)) {
1666 dev_err(&applespi->spi->dev,
1667 "Failed to get SIST ACPI method handle: %s\n",
1668 acpi_format_exception(acpi_sts));
1669 return -ENODEV;
1670 }
1671
1672 /* switch on the SPI interface */
1673 sts = applespi_setup_spi(applespi);
1674 if (sts)
1675 return sts;
1676
1677 sts = applespi_enable_spi(applespi);
1678 if (sts)
1679 return sts;
1680
1681 /* setup the keyboard input dev */
1682 applespi->keyboard_input_dev = devm_input_allocate_device(&spi->dev);
1683
1684 if (!applespi->keyboard_input_dev)
1685 return -ENOMEM;
1686
1687 applespi->keyboard_input_dev->name = "Apple SPI Keyboard";
1688 applespi->keyboard_input_dev->phys = "applespi/input0";
1689 applespi->keyboard_input_dev->dev.parent = &spi->dev;
1690 applespi->keyboard_input_dev->id.bustype = BUS_SPI;
1691
1692 applespi->keyboard_input_dev->evbit[0] =
1693 BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) | BIT_MASK(EV_REP);
1694 applespi->keyboard_input_dev->ledbit[0] = BIT_MASK(LED_CAPSL);
1695
1696 input_set_drvdata(applespi->keyboard_input_dev, applespi);
1697 applespi->keyboard_input_dev->event = applespi_event;
1698
1699 for (i = 0; i < ARRAY_SIZE(applespi_scancodes); i++)
1700 if (applespi_scancodes[i])
1701 input_set_capability(applespi->keyboard_input_dev,
1702 EV_KEY, applespi_scancodes[i]);
1703
1704 for (i = 0; i < ARRAY_SIZE(applespi_controlcodes); i++)
1705 if (applespi_controlcodes[i])
1706 input_set_capability(applespi->keyboard_input_dev,
1707 EV_KEY, applespi_controlcodes[i]);
1708
1709 for (i = 0; i < ARRAY_SIZE(applespi_fn_codes); i++)
1710 if (applespi_fn_codes[i].to)
1711 input_set_capability(applespi->keyboard_input_dev,
1712 EV_KEY, applespi_fn_codes[i].to);
1713
1714 input_set_capability(applespi->keyboard_input_dev, EV_KEY, KEY_FN);
1715
1716 sts = input_register_device(applespi->keyboard_input_dev);
1717 if (sts) {
1718 dev_err(&applespi->spi->dev,
1719 "Unable to register keyboard input device (%d)\n", sts);
1720 return -ENODEV;
1721 }
1722
1723 /*
1724 * The applespi device doesn't send interrupts normally (as is described
1725 * in its DSDT), but rather seems to use ACPI GPEs.
1726 */
1727 acpi_sts = acpi_evaluate_integer(spi_handle, "_GPE", NULL, &gpe);
1728 if (ACPI_FAILURE(acpi_sts)) {
1729 dev_err(&applespi->spi->dev,
1730 "Failed to obtain GPE for SPI slave device: %s\n",
1731 acpi_format_exception(acpi_sts));
1732 return -ENODEV;
1733 }
1734 applespi->gpe = (int)gpe;
1735
1736 acpi_sts = acpi_install_gpe_handler(NULL, applespi->gpe,
1737 ACPI_GPE_LEVEL_TRIGGERED,
1738 applespi_notify, applespi);
1739 if (ACPI_FAILURE(acpi_sts)) {
1740 dev_err(&applespi->spi->dev,
1741 "Failed to install GPE handler for GPE %d: %s\n",
1742 applespi->gpe, acpi_format_exception(acpi_sts));
1743 return -ENODEV;
1744 }
1745
1746 applespi->suspended = false;
1747
1748 acpi_sts = acpi_enable_gpe(NULL, applespi->gpe);
1749 if (ACPI_FAILURE(acpi_sts)) {
1750 dev_err(&applespi->spi->dev,
1751 "Failed to enable GPE handler for GPE %d: %s\n",
1752 applespi->gpe, acpi_format_exception(acpi_sts));
1753 acpi_remove_gpe_handler(NULL, applespi->gpe, applespi_notify);
1754 return -ENODEV;
1755 }
1756
1757 /* trigger touchpad setup */
1758 applespi_init(applespi, false);
1759
1760 /*
1761 * By default this device is not enabled for wakeup; but USB keyboards
1762 * generally are, so the expectation is that by default the keyboard
1763 * will wake the system.
1764 */
1765 device_wakeup_enable(&spi->dev);
1766
1767 /* set up keyboard-backlight */
1768 sts = applespi_get_saved_bl_level(applespi);
1769 if (sts >= 0)
1770 applespi_set_bl_level(&applespi->backlight_info, sts);
1771
1772 applespi->backlight_info.name = "spi::kbd_backlight";
1773 applespi->backlight_info.default_trigger = "kbd-backlight";
1774 applespi->backlight_info.brightness_set = applespi_set_bl_level;
1775
1776 sts = devm_led_classdev_register(&spi->dev, &applespi->backlight_info);
1777 if (sts)
1778 dev_warn(&applespi->spi->dev,
1779 "Unable to register keyboard backlight class dev (%d)\n",
1780 sts);
1781
1782 /* set up debugfs entries for touchpad dimensions logging */
1783 applespi->debugfs_root = debugfs_create_dir("applespi", NULL);
1784
1785 debugfs_create_bool("enable_tp_dim", 0600, applespi->debugfs_root,
1786 &applespi->debug_tp_dim);
1787
1788 debugfs_create_file("tp_dim", 0400, applespi->debugfs_root, applespi,
1789 &applespi_tp_dim_fops);
1790
1791 return 0;
1792 }
1793
applespi_drain_writes(struct applespi_data * applespi)1794 static void applespi_drain_writes(struct applespi_data *applespi)
1795 {
1796 guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
1797
1798 applespi->drain = true;
1799 wait_event_lock_irq(applespi->drain_complete, !applespi->write_active,
1800 applespi->cmd_msg_lock);
1801 }
1802
applespi_drain_reads(struct applespi_data * applespi)1803 static void applespi_drain_reads(struct applespi_data *applespi)
1804 {
1805 guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
1806
1807 wait_event_lock_irq(applespi->drain_complete, !applespi->read_active,
1808 applespi->cmd_msg_lock);
1809
1810 applespi->suspended = true;
1811 }
1812
applespi_remove(struct spi_device * spi)1813 static void applespi_remove(struct spi_device *spi)
1814 {
1815 struct applespi_data *applespi = spi_get_drvdata(spi);
1816
1817 applespi_drain_writes(applespi);
1818
1819 acpi_disable_gpe(NULL, applespi->gpe);
1820 acpi_remove_gpe_handler(NULL, applespi->gpe, applespi_notify);
1821 device_wakeup_disable(&spi->dev);
1822
1823 applespi_drain_reads(applespi);
1824
1825 debugfs_remove_recursive(applespi->debugfs_root);
1826 }
1827
applespi_shutdown(struct spi_device * spi)1828 static void applespi_shutdown(struct spi_device *spi)
1829 {
1830 struct applespi_data *applespi = spi_get_drvdata(spi);
1831
1832 applespi_save_bl_level(applespi, applespi->have_bl_level);
1833 }
1834
applespi_poweroff_late(struct device * dev)1835 static int applespi_poweroff_late(struct device *dev)
1836 {
1837 struct spi_device *spi = to_spi_device(dev);
1838 struct applespi_data *applespi = spi_get_drvdata(spi);
1839
1840 applespi_save_bl_level(applespi, applespi->have_bl_level);
1841
1842 return 0;
1843 }
1844
applespi_suspend(struct device * dev)1845 static int applespi_suspend(struct device *dev)
1846 {
1847 struct spi_device *spi = to_spi_device(dev);
1848 struct applespi_data *applespi = spi_get_drvdata(spi);
1849 acpi_status acpi_sts;
1850 int sts;
1851
1852 /* turn off caps-lock - it'll stay on otherwise */
1853 sts = applespi_set_capsl_led(applespi, false);
1854 if (sts)
1855 dev_warn(&applespi->spi->dev,
1856 "Failed to turn off caps-lock led (%d)\n", sts);
1857
1858 applespi_drain_writes(applespi);
1859
1860 /* disable the interrupt */
1861 acpi_sts = acpi_disable_gpe(NULL, applespi->gpe);
1862 if (ACPI_FAILURE(acpi_sts))
1863 dev_err(&applespi->spi->dev,
1864 "Failed to disable GPE handler for GPE %d: %s\n",
1865 applespi->gpe, acpi_format_exception(acpi_sts));
1866
1867 applespi_drain_reads(applespi);
1868
1869 return 0;
1870 }
1871
applespi_resume(struct device * dev)1872 static int applespi_resume(struct device *dev)
1873 {
1874 struct spi_device *spi = to_spi_device(dev);
1875 struct applespi_data *applespi = spi_get_drvdata(spi);
1876 acpi_status acpi_sts;
1877
1878 /* ensure our flags and state reflect a newly resumed device */
1879 scoped_guard(spinlock_irqsave, &applespi->cmd_msg_lock) {
1880 applespi->drain = false;
1881 applespi->have_cl_led_on = false;
1882 applespi->have_bl_level = 0;
1883 applespi->cmd_msg_queued = 0;
1884 applespi->read_active = false;
1885 applespi->write_active = false;
1886
1887 applespi->suspended = false;
1888 }
1889
1890 /* switch on the SPI interface */
1891 applespi_enable_spi(applespi);
1892
1893 /* re-enable the interrupt */
1894 acpi_sts = acpi_enable_gpe(NULL, applespi->gpe);
1895 if (ACPI_FAILURE(acpi_sts))
1896 dev_err(&applespi->spi->dev,
1897 "Failed to re-enable GPE handler for GPE %d: %s\n",
1898 applespi->gpe, acpi_format_exception(acpi_sts));
1899
1900 /* switch the touchpad into multitouch mode */
1901 applespi_init(applespi, true);
1902
1903 return 0;
1904 }
1905
1906 static const struct acpi_device_id applespi_acpi_match[] = {
1907 { "APP000D", 0 },
1908 { }
1909 };
1910 MODULE_DEVICE_TABLE(acpi, applespi_acpi_match);
1911
1912 static const struct dev_pm_ops applespi_pm_ops = {
1913 SYSTEM_SLEEP_PM_OPS(applespi_suspend, applespi_resume)
1914 .poweroff_late = pm_sleep_ptr(applespi_poweroff_late),
1915 };
1916
1917 static struct spi_driver applespi_driver = {
1918 .driver = {
1919 .name = "applespi",
1920 .acpi_match_table = applespi_acpi_match,
1921 .pm = pm_sleep_ptr(&applespi_pm_ops),
1922 },
1923 .probe = applespi_probe,
1924 .remove = applespi_remove,
1925 .shutdown = applespi_shutdown,
1926 };
1927
1928 module_spi_driver(applespi_driver)
1929
1930 MODULE_LICENSE("GPL v2");
1931 MODULE_DESCRIPTION("MacBook(Pro) SPI Keyboard/Touchpad driver");
1932 MODULE_AUTHOR("Federico Lorenzi");
1933 MODULE_AUTHOR("Ronald Tschalär");
1934