Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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