diff --git a/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml index b6c9e727e5a67..821d54f68d590 100644 --- a/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml +++ b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml @@ -241,6 +241,26 @@ properties: Name of the touchscreen firmware that the host sends to the coprocessor when the interface is first opened. + apple,power-method: + $ref: /schemas/types.yaml#/definitions/uint32 + enum: [1, 2] + default: 1 + description: + The power sequencing protocol of the coprocessor firmware, as + described above. Power method 2 for kiwi carries its own reset + and power lines instead of the shared AFE reset. + + apple,reset-gpios: + maxItems: 1 + description: + The touchscreen's own reset line, when it does not share the + AFE reset of the parent node. + + apple,power-gpios: + maxItems: 1 + description: + The touchscreen's optional supply enable line. + mtp: type: object additionalProperties: false diff --git a/arch/arm64/boot/dts/apple/t6050-k114c.dts b/arch/arm64/boot/dts/apple/t6050-k114c.dts index 13ea865ba58e1..414dc0c81fc48 100644 --- a/arch/arm64/boot/dts/apple/t6050-k114c.dts +++ b/arch/arm64/boot/dts/apple/t6050-k114c.dts @@ -581,3 +581,44 @@ <&ps_atc2_cio_usb>, <&ps_atc2_cio_auss>, <&ps_atc2_pcie>; apple,pcie-tunnel = <&acio2_pcie_tunnel>; }; +/* The touchscreen (kiwi). Its reset and supply are NUB GPIO pins 25 and 26 (ADT + * function-reset and function-power), not the shared multi-touch AFE reset. + * The NUB GPIO block powers the touch controller; upstream wires its interrupts. + * the reset, as for the AFE one [INFERENCE]. + */ +&ps_nub_gpio { + status = "okay"; +}; + +&nub_gpio0 { + status = "okay"; +}; + +&kiwi { + firmware-name = "apple/tpkiwifw-k114c.bin"; + apple,power-method = <2>; + apple,reset-gpios = <&nub_gpio0 25 GPIO_ACTIVE_LOW>; + apple,power-gpios = <&nub_gpio0 26 GPIO_ACTIVE_HIGH>; + status = "okay"; +}; + +/* + * The kiwi 24 MHz clock pad (NUB pin 22) and display_sync pad (AP pin 11) are pad + * functions, not host GPIOs: function 1 on each. + */ +&pinctrl_ap { + kiwi_dsync_pins: kiwi-dsync-pins { + pinmux = <0x1000b>; /* APPLE_PINMUX(11, 1) */ + }; +}; + +&nub_gpio0 { + kiwi_clk24_pins: kiwi-clk24-pins { + pinmux = <0x10016>; /* APPLE_PINMUX(22, 1) */ + }; +}; + +&mtp_hid { + pinctrl-0 = <&kiwi_clk24_pins>, <&kiwi_dsync_pins>; + pinctrl-names = "default"; +}; diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 6f956d056b87c..bface8b26a144 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -156,6 +156,17 @@ config HID_APPLE_MTP_HAPTIC This driver can also be built as a module. If so, it will be called hid-apple-mtp-haptic. +config HID_APPLE_MTP_KIWI + tristate "Apple MTP touch screen (kiwi) support" + depends on HID + help + Support for the touch screen of Apple Silicon laptops that have one, + such as the MacBook Pro with a touch display. The driver turns the raw + capacitance image of the touch interface into multi-touch contacts. + + This driver can also be built as a module. If so, it will be called + hid-apple-mtp-kiwi. + config HID_APPLEIR tristate "Apple infrared receiver" depends on (USB_HID) diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index 3efe8ee1caddb..ba4942205143d 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -30,6 +30,7 @@ obj-$(CONFIG_HID_ALPS) += hid-alps.o obj-$(CONFIG_HID_ACRUX) += hid-axff.o obj-$(CONFIG_HID_APPLE) += hid-apple.o obj-$(CONFIG_HID_APPLE_MTP_HAPTIC) += hid-apple-mtp-haptic.o +obj-$(CONFIG_HID_APPLE_MTP_KIWI) += hid-apple-mtp-kiwi.o obj-$(CONFIG_HID_APPLEIR) += hid-appleir.o obj-$(CONFIG_HID_APPLETB_BL) += hid-appletb-bl.o obj-$(CONFIG_HID_APPLETB_KBD) += hid-appletb-kbd.o diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid.c b/drivers/hid/dockchannel-hid/dockchannel-hid.c index 9e0877ca2d496..55a66bb066efa 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid.c @@ -29,6 +29,9 @@ #define COMMAND_TIMEOUT_MS 1000 #define START_TIMEOUT_MS 2000 +/* The MTP bootloader writes the 1.3 MB kiwi image over the touch IC SPI: seconds, not 1 s. */ +#define KIWI_CMD_TIMEOUT_MS 10000 +#define KIWI_START_TIMEOUT_MS 10000 #define MAX_INTERFACES 16 @@ -180,8 +183,15 @@ struct dchid_iface { size_t hid_desc_len; struct gpio_desc *gpio; - char gpio_name[MAX_GPIO_NAME]; - int gpio_id; + /* The k114c MTP announces several GPIOs per interface (kiwi: + * clock_24MHz_enable, display_sync, power, reset). Track them all; + * gpio keeps the legacy single-GPIO semantics for existing paths. */ +#define MAX_IFACE_GPIOS 4 + struct gpio_desc *gpios[MAX_IFACE_GPIOS]; + char gpio_names[MAX_IFACE_GPIOS][MAX_GPIO_NAME]; + int gpio_ids[MAX_IFACE_GPIOS]; + int gpio_count; + bool gpios_requested; bool gpio_announced; struct mutex out_mutex; @@ -195,6 +205,11 @@ struct dchid_iface { u32 keyboard_layout_id; u32 power_method; bool firmware_managed_reset; + + /* Power method 2: the line that resets this interface, and its optional supply. */ + struct gpio_desc *reset; + struct gpio_desc *power; + struct gpio_desc *clock; }; struct dockchannel_hid { @@ -203,6 +218,8 @@ struct dockchannel_hid { struct device_link *helper_link; struct apple_smc *afe_smc; bool stopping; + /* The kiwi firmware load is running: comm commands get the long timeout */ + bool kiwi_loading; bool inherited_mtp; /* Between the system sleep suspend and resume callbacks */ bool suspended; @@ -253,6 +270,7 @@ dchid_get_interface(struct dockchannel_hid *dchid, int index, const char *name) u32 power_method = DCHID_POWER_METHOD_1; bool firmware_managed_reset = false; struct device_node *of_node = NULL; + struct gpio_desc *reset = NULL, *power = NULL, *clock = NULL; struct dchid_iface *iface; if (READ_ONCE(dchid->stopping)) @@ -294,11 +312,44 @@ dchid_get_interface(struct dockchannel_hid *dchid, int index, const char *name) power_method, name); goto err_put_node; } - if (power_method == DCHID_POWER_METHOD_2 && - !firmware_managed_reset && !dchid->afe_reset) { - dev_err(dchid->dev, "Power method 2 for %s needs the AFE reset line\n", - name); - goto err_put_node; + if (power_method == DCHID_POWER_METHOD_2 && !firmware_managed_reset) { + struct fwnode_handle *fwnode = of_fwnode_handle(of_node); + + /* An interface with its own reset line (kiwi) does not share the AFE one. */ + reset = devm_fwnode_gpiod_get(dchid->dev, fwnode, "apple,reset", + GPIOD_OUT_LOW, "dchid-reset"); + if (IS_ERR(reset)) { + if (PTR_ERR(reset) != -ENOENT) { + dev_err(dchid->dev, "Reset line of %s: %pe\n", name, reset); + goto err_put_node; + } + reset = dchid->afe_reset; + } + if (!reset) { + dev_err(dchid->dev, "Power method 2 for %s needs a reset line\n", + name); + goto err_put_node; + } + + power = devm_fwnode_gpiod_get(dchid->dev, fwnode, "apple,power", + GPIOD_OUT_HIGH, "dchid-power"); + if (IS_ERR(power)) { + if (PTR_ERR(power) != -ENOENT) { + dev_err(dchid->dev, "Power line of %s: %pe\n", name, power); + goto err_put_node; + } + power = NULL; + } + + clock = devm_fwnode_gpiod_get(dchid->dev, fwnode, "apple,clock", + GPIOD_OUT_HIGH, "dchid-clock"); + if (IS_ERR(clock)) { + if (PTR_ERR(clock) != -ENOENT) { + dev_err(dchid->dev, "Clock line of %s: %pe\n", name, clock); + goto err_put_node; + } + clock = NULL; + } } } @@ -314,6 +365,9 @@ dchid_get_interface(struct dockchannel_hid *dchid, int index, const char *name) iface->of_node = of_node; iface->power_method = power_method; iface->firmware_managed_reset = firmware_managed_reset; + iface->reset = reset; + iface->power = power; + iface->clock = clock; iface->out_report = -1; init_completion(&iface->out_complete); init_completion(&iface->ready); @@ -443,7 +497,10 @@ static int dchid_cmd(struct dchid_iface *iface, u32 type, u32 req, if (ret < 0) goto done; - if (!wait_for_completion_timeout(&iface->out_complete, msecs_to_jiffies(COMMAND_TIMEOUT_MS))) { + if (!wait_for_completion_timeout(&iface->out_complete, + msecs_to_jiffies(READ_ONCE(iface->dchid->kiwi_loading) ? + KIWI_CMD_TIMEOUT_MS : + COMMAND_TIMEOUT_MS))) { dev_err(iface->dchid->dev, "output report 0x%x to iface %d (%s) timed out\n", report_id, iface->index, iface->name); ret = -ETIMEDOUT; @@ -571,11 +628,23 @@ static int dchid_register_ring(struct dockchannel_hid *dchid) return ret; } +/* Drive an interface's own power, clock and reset lines to the OFF or ON state. */ +static int dchid_set_own_lines(struct dchid_iface *iface, u8 state) +{ + bool on = state == DCHID_POWER_STATE_ON; + + if (on && iface->power) + gpiod_set_value_cansleep(iface->power, on); + if (iface->clock) + gpiod_set_value_cansleep(iface->clock, on); + return gpiod_set_value_cansleep(iface->reset, !on); +} + /* - * Power method 2: announce the transition, drive the AFE reset line to the - * new state and confirm the transition. The reset is asserted for a power-off - * and held for 50 ms, and released for a power-on and given 50 ms to settle - * before the transition is confirmed. + * Power method 2: announce the transition, drive the interface's reset line + * to the new state and confirm the transition. The reset is asserted for a + * power-off and held for 50 ms, and released for a power-on before the + * transition is confirmed. */ static int dchid_pm2_set_power(struct dchid_iface *iface, u8 state) { @@ -595,21 +664,19 @@ static int dchid_pm2_set_power(struct dchid_iface *iface, u8 state) } else { bool off = state == DCHID_POWER_STATE_OFF; - ret = gpiod_set_value_cansleep(dchid->afe_reset, off); + ret = dchid_set_own_lines(iface, state); if (ret < 0) { - dev_err(dchid->dev, "%s: failed to %s the AFE reset: %d\n", + dev_err(dchid->dev, "%s: failed to %s the reset line: %d\n", iface->name, off ? "assert" : "release", ret); return ret; } - dev_dbg(dchid->dev, "%s: AFE reset %s\n", iface->name, - off ? "asserted" : "released"); /* - * The sequence validated on J716s (M5 Pro, MTP firmware - * 6320.86.1, "Touch MT ready" on 3 of 3 boots) also waits - * 50 ms after the release before HasChanged, as the - * firmware-managed path above does. + * The kiwi sequence was verified with a settle after power-off + * only; the shared AFE reset (multi-touch, sequence validated + * on J716s) settles after every transition. */ - fsleep(50 * USEC_PER_MSEC); + if (off || iface->reset == dchid->afe_reset) + fsleep(50 * USEC_PER_MSEC); } dchid_pm2_command(iface->index, state, true, msg); @@ -705,7 +772,7 @@ static int dchid_get_firmware(struct dchid_iface *iface, void **firmware, size_t static int dchid_request_gpio(struct dchid_iface *iface) { char prop_name[MAX_GPIO_NAME + 16]; - int ret; + int ret, g; if (iface->firmware_managed_reset) return -EINVAL; @@ -715,30 +782,45 @@ static int dchid_request_gpio(struct dchid_iface *iface) return -EOPNOTSUPP; } - if (iface->gpio) + if (iface->gpio_count && iface->gpios_requested) return 0; - - dev_dbg(iface->dchid->dev, "Requesting GPIO %s#%d: %s\n", - iface->name, iface->gpio_id, iface->gpio_name); - - /* A power method 2 interface holds its reset line from probe on. */ - if (iface->power_method == DCHID_POWER_METHOD_2 && - !strcmp(iface->gpio_name, "afe-reset")) { - iface->gpio = iface->dchid->afe_reset; + if (!iface->gpio_count && iface->gpio) return 0; - } - snprintf(prop_name, sizeof(prop_name), "apple,%s", iface->gpio_name); + for (g = 0; g < iface->gpio_count; g++) { + const char *name = iface->gpio_names[g]; - iface->gpio = devm_gpiod_get_index(iface->dchid->dev, prop_name, 0, GPIOD_OUT_LOW); + dev_dbg(iface->dchid->dev, "Requesting GPIO %s#%d: %s\n", + iface->name, iface->gpio_ids[g], name); - if (IS_ERR(iface->gpio)) { - ret = PTR_ERR(iface->gpio); - iface->gpio = NULL; - dev_err(iface->dchid->dev, "Failed to request GPIO %s-gpios: %d\n", - prop_name, ret); - return ret; + /* A power method 2 multi-touch holds its AFE reset line from + * probe on; the pm2 transitions drive it, not this path. */ + if (iface->power_method == DCHID_POWER_METHOD_2 && + !strcmp(name, "afe-reset")) { + iface->gpios[g] = iface->dchid->afe_reset; + if (g == 0) + iface->gpio = iface->dchid->afe_reset; + continue; + } + + snprintf(prop_name, sizeof(prop_name), "apple,%s", name); + iface->gpios[g] = devm_gpiod_get_index(iface->dchid->dev, prop_name, + 0, GPIOD_OUT_LOW); + if (IS_ERR(iface->gpios[g])) { + ret = PTR_ERR(iface->gpios[g]); + iface->gpios[g] = NULL; + /* An announced line the DT does not wire (a pad function): its commands are refused. */ + if (ret == -ENOENT) + continue; + dev_err(iface->dchid->dev, + "Failed to request GPIO %s-gpios: %d\n", + prop_name, ret); + return ret; + } + if (g == 0) + iface->gpio = iface->gpios[g]; } + iface->gpios_requested = true; return 0; } @@ -748,6 +830,12 @@ static int dchid_start_interface(struct dchid_iface *iface) void *fw; size_t size; int ret; + /* Only a kiwi with its own reset line takes the long firmware timeout and the power + * cycle: without one it would pulse the AFE reset that the trackpad shares. */ + bool kiwi_own = !strcmp(iface->name, "kiwi") && + iface->power_method == DCHID_POWER_METHOD_2 && + !iface->firmware_managed_reset && + iface->reset != iface->dchid->afe_reset; if (iface->starting) { dev_warn(iface->dchid->dev, "Interface %s is already starting\n", iface->name); @@ -773,13 +861,26 @@ static int dchid_start_interface(struct dchid_iface *iface) goto err; } - /* Only multi-touch has firmware */ + /* Only multi-touch and kiwi have firmware */ if (fw && size) { + if (kiwi_own) + WRITE_ONCE(iface->dchid->kiwi_loading, true); + + /* The kiwi chip needs a full power cycle with a reset pulse + * before its firmware goes out (its reset line is active low + * in the device tree); the sequence is verified on hardware. */ + if (kiwi_own) { + dchid_set_own_lines(iface, DCHID_POWER_STATE_OFF); + fsleep(50 * USEC_PER_MSEC); + dchid_set_own_lines(iface, DCHID_POWER_STATE_ON); + fsleep(50 * USEC_PER_MSEC); + } /* Send firmware to the device */ dev_dbg(iface->dchid->dev, "Sending firmware for %s\n", iface->name); /* Retained MTP must see AFE reset throughout firmware upload. */ if (iface->dchid->inherited_mtp && + !kiwi_own && iface->power_method == DCHID_POWER_METHOD_2) { if (iface->firmware_managed_reset) { ret = -EINVAL; @@ -799,8 +900,9 @@ static int dchid_start_interface(struct dchid_iface *iface) /* After loading firmware, multi-touch needs a reset */ dev_dbg(iface->dchid->dev, "Resetting %s\n", iface->name); if (iface->power_method == DCHID_POWER_METHOD_2) { - /* The retained session is already off while reset is asserted. */ - if (!iface->dchid->inherited_mtp) { + /* The retained session is already off while reset is asserted; + * kiwi always power-cycles its own lines. */ + if (!iface->dchid->inherited_mtp || kiwi_own) { ret = dchid_pm2_set_power(iface, DCHID_POWER_STATE_OFF); if (ret < 0) goto err; @@ -816,9 +918,13 @@ static int dchid_start_interface(struct dchid_iface *iface) } } + if (kiwi_own) + WRITE_ONCE(iface->dchid->kiwi_loading, false); return 0; err: + if (kiwi_own) + WRITE_ONCE(iface->dchid->kiwi_loading, false); iface->starting = false; return ret; } @@ -859,7 +965,10 @@ static int dchid_open(struct hid_device *hdev) if (ret < 0) return ret; - if (!wait_for_completion_timeout(&iface->ready, msecs_to_jiffies(START_TIMEOUT_MS))) { + if (!wait_for_completion_timeout(&iface->ready, + msecs_to_jiffies(!strcmp(iface->name, "kiwi") ? + KIWI_START_TIMEOUT_MS : + START_TIMEOUT_MS))) { dev_err(iface->dchid->dev, "iface %s start timed out\n", iface->name); return -ETIMEDOUT; } @@ -1049,6 +1158,51 @@ static int dchid_create_interface(struct dchid_iface *iface) return queue_work(iface->dchid->new_iface_wq, &iface->create_work); } +/* hid-core rejects any Report Size above this (see hid_parser_global()). */ +#define DCHID_MAX_REPORT_SIZE 16384 + +/* + * The T6050 multi-touch firmware declares its raw touch report as one item of + * 0x80000 bits. The magicmouse driver reads those reports in raw_event() and + * never looks at the field layout, so clamp every Report Size item to what + * hid-core accepts instead of failing the whole interface. + */ +static void dchid_clamp_report_sizes(struct dchid_iface *iface, u8 *desc, size_t len) +{ + size_t i = 0; + + while (i < len) { + u8 prefix = desc[i]; + size_t n = prefix & 3; + u32 val = 0; + size_t j; + + if (prefix == 0xfe) { + if (i + 1 >= len) + return; + i += 3 + desc[i + 1]; + continue; + } + if (n == 3) + n = 4; + if (i + 1 + n > len) + return; + + if ((prefix & 0xfc) == 0x74 && n >= 2) { + for (j = 0; j < n; j++) + val |= (u32)desc[i + 1 + j] << (8 * j); + if (val > DCHID_MAX_REPORT_SIZE) { + dev_info(iface->dchid->dev, + "%s: clamping Report Size %u to %u\n", + iface->name, val, DCHID_MAX_REPORT_SIZE); + for (j = 0; j < n; j++) + desc[i + 1 + j] = (DCHID_MAX_REPORT_SIZE >> (8 * j)) & 0xff; + } + } + i += 1 + n; + } +} + static void dchid_handle_descriptor(struct dchid_iface *iface, void *hid_desc, size_t desc_len) { if (iface->hid) { @@ -1062,6 +1216,7 @@ static void dchid_handle_descriptor(struct dchid_iface *iface, void *hid_desc, s return; iface->hid_desc_len = desc_len; + dchid_clamp_report_sizes(iface, iface->hid_desc, desc_len); } static void dchid_handle_ready(struct dockchannel_hid *dchid, void *data, size_t length) @@ -1160,14 +1315,16 @@ static void dchid_handle_init(struct dockchannel_hid *dchid, void *data, size_t break; } - if (iface->gpio_announced) { + if (iface->gpio_count >= MAX_IFACE_GPIOS) { dev_err(dchid->dev, - "Cannot request more than one GPIO per interface!\n"); + "Too many GPIO requests for %s\n", iface->name); break; } - strscpy(iface->gpio_name, req->name, MAX_GPIO_NAME); - iface->gpio_id = req->id; + strscpy(iface->gpio_names[iface->gpio_count], req->name, + MAX_GPIO_NAME); + iface->gpio_ids[iface->gpio_count] = req->id; + iface->gpio_count++; iface->gpio_announced = true; break; } @@ -1214,6 +1371,9 @@ static void dchid_handle_init(struct dockchannel_hid *dchid, void *data, size_t static void dchid_handle_gpio(struct dockchannel_hid *dchid, void *data, size_t length) { + struct gpio_desc *cmd_gpio = NULL; + bool found = false; + unsigned int g; struct dchid_gpio_cmd *cmd = data; struct dchid_iface *iface; u32 retcode = 0xe000f00d; /* Give it a random Apple-style error code */ @@ -1236,24 +1396,36 @@ static void dchid_handle_gpio(struct dockchannel_hid *dchid, void *data, size_t if (dchid_request_gpio(iface) < 0) goto err; - if (!iface->gpio || cmd->gpio != iface->gpio_id) { + for (g = 0; g < iface->gpio_count; g++) { + if (iface->gpio_ids[g] == cmd->gpio) { + cmd_gpio = iface->gpios[g]; + found = true; + break; + } + } + if (!found) { dev_err(dchid->dev, "Got GPIO command for bad GPIO %s#%d\n", iface->name, cmd->gpio); goto err; } + if (!cmd_gpio) { + dev_dbg(dchid->dev, "GPIO %s#%d is not wired in the DT: refused\n", + iface->name, cmd->gpio); + goto err; + } dev_dbg(dchid->dev, "GPIO command: %s#%d: %d\n", iface->name, cmd->gpio, cmd->cmd); switch (cmd->cmd) { case 3: /* Pulse. */ - gpiod_set_value_cansleep(iface->gpio, 1); + gpiod_set_value_cansleep(cmd_gpio, 1); msleep(10); /* Random guess... */ - gpiod_set_value_cansleep(iface->gpio, 0); + gpiod_set_value_cansleep(cmd_gpio, 0); retcode = 0; break; default: - dev_err(dchid->dev, "Unknown GPIO command %d\n", cmd->cmd ); + dev_err(dchid->dev, "Unknown GPIO command %d\n", cmd->cmd); break; } diff --git a/drivers/hid/hid-apple-mtp-kiwi.c b/drivers/hid/hid-apple-mtp-kiwi.c new file mode 100644 index 0000000000000..3994d773822b3 --- /dev/null +++ b/drivers/hid/hid-apple-mtp-kiwi.c @@ -0,0 +1,370 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Apple MTP touch-screen (kiwi) interface. + * + * The kiwi interface of the dockchannel MTP carries its touches as a raw + * report pipe. No other HID driver may claim it: the Apple wildcard entries of + * hid-apple and hid-magicmouse make hid-generic stand off from every + * BUS_HOST Apple device, and both reject the interface in probe. This driver + * claims it by name, keeps the pipe on hidraw, and registers a direct + * multi-touch input device. The interface is started (firmware, reset) when + * the first user opens hidraw or the input device. + * + * The 29-byte descriptor declares one opaque Digitizer/TouchScreen input blob + * (16384 bits), so the finger layout is not described by HID. Observed layout + * of the 17861-byte report (see the pull request description): + * + * 0..60 header. Byte 0 is 0x5a, bytes 1..6 are 01 02 00 20 18 00, + * bytes 7..10 are a u32 LE microsecond timer (8333 us per frame) + * 61..8916 54 rows x 82 columns of u16 LE raw capacitance, row major, + * row 0 at the bottom edge of the panel, + * idle level about 2500 (noise about 25 to 50 counts). A finger + * lowers the cells under it by 600..1300 counts. One cell is + * about 36.9 px on the 3024x1986 panel in both directions. + * 8917.. further sections that are not decoded yet + * + * Every 2 s four whole-image shifts arrive (about +658, +902, -445 and -126 + * counts, slightly different per row). Frames shifted by more than 200 counts + * are skipped and a per-row mean is removed from the rest. The raw image has no + * baseline, so the driver keeps one per cell, picks local maxima of + * (baseline - raw), and centroids 3x3. + */ + +#include +#include +#include +#include +#include + +#include "hid-ids.h" + +#define KIWI_HDR_LEN 61 +#define KIWI_COLS 82 +#define KIWI_ROWS 54 +#define KIWI_CELLS (KIWI_COLS * KIWI_ROWS) +#define KIWI_MIN_FRAME (KIWI_HDR_LEN + 2 * KIWI_CELLS) +#define KIWI_MAGIC 0x5a +#define KIWI_MAX_SLOTS 10 +/* eDP-1 panel of the k114c, 3024x1986. */ +#define KIWI_MAX_X 3023 +#define KIWI_MAX_Y 1985 +/* Panel is about 10 units per mm. */ +#define KIWI_RES 10 +#define KIWI_FP 256 +#define KIWI_GLITCH 200 /* mean shift of the 2 s reference frame is about 900 */ +#define KIWI_GLITCH_MAX 10 /* skipped frames in a row before the baseline is retaken */ +#define KIWI_MISS_MAX 2 +#define KIWI_MATCH_D2 (4 * KIWI_FP * 4 * KIWI_FP) + +#define KIWI_THRESHOLD 250 /* counts below baseline that make a touch */ + +struct kiwi_contact { + bool on; + int miss; + int x, y; /* 1/KIWI_FP cell, centre of cell 0 is KIWI_FP / 2 */ + int size; +}; + +struct kiwi { + struct input_dev *input; + bool have_base; + int bad; + s16 sig[KIWI_CELLS]; /* baseline minus raw, positive under a finger */ + s32 base[KIWI_CELLS]; + s32 fp[KIWI_CELLS]; + struct kiwi_contact cont[KIWI_MAX_SLOTS]; +}; + +static bool kiwi_match(struct hid_device *hdev, bool ignore_special_drivers) +{ + return hdev->bus == BUS_HOST && !strcmp(hdev->name, "Apple MTP kiwi"); +} + +/* + * Fixed point: base and fp are in 1/256 count. fp is baseline minus raw with the + * frame-wide shift removed, positive under a finger. + * Returns false for a frame that must be skipped (big reference frame). + * Besides the two big reference frames (+658 and +902 counts) every 2 s there is a + * -445 and a -126 count frame: the whole image moves, so remove the mean. + */ +static bool kiwi_load(struct kiwi *k, const u8 *data) +{ + s64 sum = 0; + s32 common; + int i, r, c; + + if (!k->have_base) { + for (i = 0; i < KIWI_CELLS; i++) + k->base[i] = get_unaligned_le16(data + KIWI_HDR_LEN + 2 * i) << 8; + k->have_base = true; + k->bad = 0; + return false; + } + for (i = 0; i < KIWI_CELLS; i++) { + k->fp[i] = k->base[i] - (get_unaligned_le16(data + KIWI_HDR_LEN + 2 * i) << 8); + sum += k->fp[i]; + } + common = div_s64(sum, KIWI_CELLS); + if (abs(common >> 8) > KIWI_GLITCH) { + if (++k->bad >= KIWI_GLITCH_MAX) + k->have_base = false; + return false; + } + k->bad = 0; + /* the shift differs a little from row to row, so remove it per row */ + for (r = 0; r < KIWI_ROWS; r++) { + s64 row = 0; + + for (c = 0; c < KIWI_COLS; c++) + row += k->fp[r * KIWI_COLS + c]; + common = div_s64(row, KIWI_COLS); + for (c = 0; c < KIWI_COLS; c++) { + i = r * KIWI_COLS + c; + k->fp[i] -= common; + k->sig[i] = clamp(k->fp[i] >> 8, -32768, 32767); + } + } + return true; +} + +/* Track the baseline: fast up when a finger left, slow drift otherwise. */ +static void kiwi_adapt(struct kiwi *k) +{ + int i; + + for (i = 0; i < KIWI_CELLS; i++) { + int s = k->sig[i]; + + if (s < -100) + k->base[i] -= k->fp[i] >> 3; + else if (s < 100) + k->base[i] -= k->fp[i] >> 10; + } +} + +static bool kiwi_is_peak(struct kiwi *k, int r, int c) +{ + int s = k->sig[r * KIWI_COLS + c]; + int dr, dc; + + for (dr = -1; dr <= 1; dr++) { + for (dc = -1; dc <= 1; dc++) { + int rr = r + dr, cc = c + dc, n; + + if ((!dr && !dc) || rr < 0 || rr >= KIWI_ROWS || cc < 0 || cc >= KIWI_COLS) + continue; + n = k->sig[rr * KIWI_COLS + cc]; + /* earlier in raster order must be strictly lower: one peak per plateau */ + if (((dr < 0) || (!dr && dc < 0)) ? n >= s : n > s) + return false; + } + } + return true; +} + +static void kiwi_centroid(struct kiwi *k, int r, int c, struct kiwi_contact *out) +{ + s64 w = 0, wx = 0, wy = 0; + int dr, dc, cells = 0; + + for (dr = -1; dr <= 1; dr++) { + for (dc = -1; dc <= 1; dc++) { + int rr = r + dr, cc = c + dc, s; + + if (rr < 0 || rr >= KIWI_ROWS || cc < 0 || cc >= KIWI_COLS) + continue; + s = k->sig[rr * KIWI_COLS + cc]; + if (s <= KIWI_THRESHOLD / 4) + continue; + w += s; + wx += (s64)s * cc; + wy += (s64)s * rr; + cells++; + } + } + out->x = (int)div64_s64(wx * KIWI_FP, w) + KIWI_FP / 2; + out->y = (int)div64_s64(wy * KIWI_FP, w) + KIWI_FP / 2; + out->size = cells; + out->on = true; + out->miss = 0; +} + +static int kiwi_find(struct kiwi *k, const struct kiwi_contact *c, u32 *claimed) +{ + int best = -1, best_d2 = KIWI_MATCH_D2, j; + + for (j = 0; j < KIWI_MAX_SLOTS; j++) { + int dx = k->cont[j].x - c->x, dy = k->cont[j].y - c->y, d2; + + if (!k->cont[j].on || (*claimed & BIT(j))) + continue; + d2 = dx * dx + dy * dy; + if (d2 < best_d2) { + best_d2 = d2; + best = j; + } + } + return best; +} + +static void kiwi_report(struct kiwi *k) +{ + struct input_dev *input = k->input; + int j; + + for (j = 0; j < KIWI_MAX_SLOTS; j++) { + struct kiwi_contact *c = &k->cont[j]; + int x, y; + + input_mt_slot(input, j); + input_mt_report_slot_state(input, MT_TOOL_FINGER, c->on); + if (!c->on) + continue; + x = div_s64((s64)c->x * (KIWI_MAX_X + 1), KIWI_COLS * KIWI_FP); + y = div_s64((s64)c->y * (KIWI_MAX_Y + 1), KIWI_ROWS * KIWI_FP); + /* Row 0 is the bottom edge of the panel. */ + x = clamp(x, 0, KIWI_MAX_X); + y = clamp(KIWI_MAX_Y - y, 0, KIWI_MAX_Y); + input_report_abs(input, ABS_MT_POSITION_X, x); + input_report_abs(input, ABS_MT_POSITION_Y, y); + input_report_abs(input, ABS_MT_TOUCH_MAJOR, c->size * 16); + } + input_mt_sync_frame(input); + input_sync(input); +} + +static void kiwi_track(struct kiwi *k, struct kiwi_contact *found, int n) +{ + u32 claimed = 0; + int i, j; + + for (i = 0; i < n; i++) { + j = kiwi_find(k, &found[i], &claimed); + if (j < 0) + continue; + claimed |= BIT(j); + k->cont[j] = found[i]; + found[i].on = false; /* consumed */ + } + for (j = 0; j < KIWI_MAX_SLOTS; j++) { + if (k->cont[j].on && !(claimed & BIT(j)) && ++k->cont[j].miss > KIWI_MISS_MAX) + k->cont[j].on = false; + } + for (i = 0; i < n; i++) { + if (!found[i].on) + continue; + for (j = 0; j < KIWI_MAX_SLOTS && k->cont[j].on; j++) + ; + if (j < KIWI_MAX_SLOTS) + k->cont[j] = found[i]; + } +} + +static void kiwi_frame(struct kiwi *k, const u8 *data) +{ + struct kiwi_contact found[KIWI_MAX_SLOTS]; + int r, c, n = 0; + + if (!kiwi_load(k, data)) + return; + for (r = 0; r < KIWI_ROWS && n < KIWI_MAX_SLOTS; r++) { + for (c = 0; c < KIWI_COLS && n < KIWI_MAX_SLOTS; c++) { + if (k->sig[r * KIWI_COLS + c] >= KIWI_THRESHOLD && kiwi_is_peak(k, r, c)) + kiwi_centroid(k, r, c, &found[n++]); + } + } + kiwi_adapt(k); + kiwi_track(k, found, n); + kiwi_report(k); +} + +static int kiwi_raw_event(struct hid_device *hdev, struct hid_report *report, + u8 *data, int size) +{ + struct kiwi *k = hid_get_drvdata(hdev); + + if (size >= KIWI_MIN_FRAME && data[0] == KIWI_MAGIC) + kiwi_frame(k, data); + return 0; +} + +static int kiwi_input_open(struct input_dev *input) +{ + struct kiwi *k = input_get_drvdata(input); + + k->have_base = false; + return hid_hw_open(to_hid_device(input->dev.parent)); +} + +static void kiwi_input_close(struct input_dev *input) +{ + hid_hw_close(to_hid_device(input->dev.parent)); +} + +static struct input_dev *kiwi_alloc_input(struct hid_device *hdev, struct kiwi *k) +{ + struct input_dev *input = devm_input_allocate_device(&hdev->dev); + + if (!input) + return NULL; + input->name = "Apple MTP touch screen"; + input->id.bustype = hdev->bus; + input->id.vendor = hdev->vendor; + input->id.product = hdev->product; + input->phys = hdev->phys; + input->dev.parent = &hdev->dev; + input->open = kiwi_input_open; + input->close = kiwi_input_close; + input_set_drvdata(input, k); + __set_bit(INPUT_PROP_DIRECT, input->propbit); + input_set_abs_params(input, ABS_MT_POSITION_X, 0, KIWI_MAX_X, 0, 0); + input_set_abs_params(input, ABS_MT_POSITION_Y, 0, KIWI_MAX_Y, 0, 0); + input_abs_set_res(input, ABS_MT_POSITION_X, KIWI_RES); + input_abs_set_res(input, ABS_MT_POSITION_Y, KIWI_RES); + input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); + if (input_mt_init_slots(input, KIWI_MAX_SLOTS, INPUT_MT_DIRECT)) + return NULL; + return input; +} + +static int kiwi_probe(struct hid_device *hdev, const struct hid_device_id *id) +{ + struct kiwi *kiwi; + int ret; + + kiwi = devm_kzalloc(&hdev->dev, sizeof(*kiwi), GFP_KERNEL); + if (!kiwi) + return -ENOMEM; + hid_set_drvdata(hdev, kiwi); + + ret = hid_parse(hdev); + if (ret) + return ret; + + kiwi->input = kiwi_alloc_input(hdev, kiwi); + if (!kiwi->input) + return -ENOMEM; + ret = input_register_device(kiwi->input); + if (ret) + return ret; + + return hid_hw_start(hdev, HID_CONNECT_HIDRAW); +} + +static const struct hid_device_id kiwi_devices[] = { + { HID_DEVICE(BUS_HOST, HID_GROUP_ANY, HOST_VENDOR_ID_APPLE, HID_ANY_ID) }, + { } +}; +MODULE_DEVICE_TABLE(hid, kiwi_devices); + +static struct hid_driver kiwi_driver = { + .name = "hid-apple-mtp-kiwi", + .id_table = kiwi_devices, + .match = kiwi_match, + .probe = kiwi_probe, + .raw_event = kiwi_raw_event, +}; +module_hid_driver(kiwi_driver); + +MODULE_DESCRIPTION("Apple MTP touch-screen (kiwi) interface"); +MODULE_LICENSE("GPL"); diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c index cc079c42c5803..6d5a0cb02dfa7 100644 --- a/drivers/hid/hid-magicmouse.c +++ b/drivers/hid/hid-magicmouse.c @@ -1586,6 +1586,12 @@ static int magicmouse_setup_input_mtp(struct input_dev *input, int mt_flags = 0; struct magicmouse_sc *msc = hid_get_drvdata(hdev); + /* + * The K114c descriptor puts a Digitizer-page field under a Mouse + * application. hid-input takes that for a pen and sets DIRECT, which + * makes udev call the pad a touchscreen. This is an indirect touchpad. + */ + __clear_bit(INPUT_PROP_DIRECT, input->propbit); __set_bit(INPUT_PROP_BUTTONPAD, input->propbit); /* * An MTP trackpad is a pointer, never a direct-touch surface. The K116s @@ -1750,6 +1756,15 @@ static int magicmouse_input_mapping(struct hid_device *hdev, if (!msc->input) msc->input = hi->input; + /* The dockchannel MTP descriptors (K114c and later) open with a GD + * Mouse collection by design; macOS consumes both halves. Mouse + * usages must not reach the input: libinput refuses a mouse+MT + * hybrid. The keyboard interface (GD Keyboard) is unaffected, and + * kiwi never probes here (type guard in magicmouse_probe). */ + if (hdev->bus == BUS_HOST && !strncmp(hdev->name, "Apple MTP", 9) && + field->application == HID_GD_MOUSE) + return -1; + /* Magic Trackpad does not give relative data after switching to MT */ if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD || hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 || @@ -2921,6 +2936,46 @@ static const __u8 *magicmouse_report_fixup(struct hid_device *hdev, __u8 *rdesc, rdesc[3] = 0x02; } + /* The K114c dockchannel MTP declares its input as a raw buffer pipe: + * REPORT_SIZE 0x80000 bits (64 KiB), one field, no report id. macOS + * reads variable-length reports against it. hid core refuses any + * REPORT_SIZE above 16384 bits and aborts the whole parse, so no + * report ever registers and every finger report is dropped before + * hidraw. Clamp the item to the hid core maximum; real reports are + * far shorter and hid core accepts reports shorter than declared. */ + if (hdev->bus == BUS_HOST && !strncmp(hdev->name, "Apple MTP", 9)) { + unsigned int i = 0; + + while (i < *rsize) { + __u8 b = rdesc[i]; + unsigned int len; + static const unsigned int lens[4] = { 0, 1, 2, 4 }; + + if ((b >> 2 & 3) == 3) { /* long item */ + i += 3 + rdesc[i + 1]; + continue; + } + len = lens[b & 3]; + if ((b >> 2 & 3) == 1 && (b >> 4) == 0x7 && len) { + __u32 v = 0; + unsigned int k; + + for (k = 0; k < len; k++) + v |= (__u32)rdesc[i + 1 + k] << (8 * k); + if (v > 16384) { + rdesc[i + 1] = 0x00; + rdesc[i + 2] = 0x40; + rdesc[i + 3] = 0x00; + rdesc[i + 4] = 0x00; + hid_info(hdev, + "clamped MTP REPORT_SIZE %u bits to 16384\n", + v); + } + } + i += 1 + len; + } + } + return rdesc; } diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index 2f0c25e38c67f..d075383523757 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -61,7 +61,7 @@ struct apple_gpio_pinctrl { #define REG_GPIO(x) (4 * (x)) #define REG_GPIOx_DATA BIT(0) -#define REG_GPIOx_MODE GENMASK(3, 1) +#define REG_GPIOx_MODE GENMASK(4, 1) #define REG_GPIOx_OUT 1 #define REG_GPIOx_IN_IRQ_HI 2 #define REG_GPIOx_IN_IRQ_LO 3 diff --git a/drivers/soc/apple/mailbox.c b/drivers/soc/apple/mailbox.c index 689d79858ab2b..d9b8ad400c635 100644 --- a/drivers/soc/apple/mailbox.c +++ b/drivers/soc/apple/mailbox.c @@ -227,6 +227,8 @@ struct apple_mbox_hw { unsigned int num_ascwrap_v6_roles; /* T8152 GPU ASCWrap v8: wrapper + iop-vbar + post-office window + PSD. */ bool is_ascwrap_v8; + /* The mailbox interrupt never reaches the AIC: receive by polling. */ + bool rx_poll_only; /* Status word counts RX occupancy and free TX slots; paired transfers. */ bool counted_fifo; bool ap_initializes_mailboxes; @@ -279,7 +281,7 @@ static struct apple_mbox_priv *apple_mbox_to_priv(struct apple_mbox *mbox) /* Only providers allocated by apple_mbox_probe() have a receive poller. */ static bool apple_mbox_has_rx_poller(const struct apple_mbox *mbox) { - return mbox->hw->is_ascwrap_v8; + return mbox->hw->is_ascwrap_v8 || mbox->hw->rx_poll_only; } /* Both ASCWrap generations share the consumer lifecycle API below. */ @@ -1526,6 +1528,10 @@ static int apple_mbox_probe(struct platform_device *pdev) return -EINVAL; if (mbox->hw->counted_fifo && !IS_ENABLED(CONFIG_ARM64)) return -EOPNOTSUPP; + if (mbox->hw->rx_poll_only) { + priv->rx_poll_only = true; + INIT_DELAYED_WORK(&priv->rx_poll_work, apple_mbox_v8_rx_poll_work); + } if (mbox->hw->is_ascwrap_v6) { /* @@ -1916,6 +1922,7 @@ static const struct apple_mbox_hw apple_mbox_t6050_ascwrap_v6_hw = { .i2a_recv0 = APPLE_ASC_MBOX_I2A_RECV0, .i2a_recv1 = APPLE_ASC_MBOX_I2A_RECV1, .has_irq_controls = false, + .rx_poll_only = true, }; static const struct apple_mbox_hw apple_mbox_t6050_j775d_ascwrap_v6_hw = { @@ -1932,6 +1939,7 @@ static const struct apple_mbox_hw apple_mbox_t6050_j775d_ascwrap_v6_hw = { .i2a_recv0 = APPLE_ASC_MBOX_I2A_RECV0, .i2a_recv1 = APPLE_ASC_MBOX_I2A_RECV1, .has_irq_controls = false, + .rx_poll_only = true, }; /*