diff --git a/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml index 0be283d25641f..07a0b8d1173ce 100644 --- a/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml +++ b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml @@ -92,6 +92,14 @@ properties: description: Reset line of the STM interface, pulsed on the coprocessor's request. + apple,afe-smc: + $ref: /schemas/types.yaml#/definitions/phandle + description: + The SMC whose gp1c key powers the trackpad analog front end. With it + the host drives the key low to power the multi-touch interface on + and high to power it off, between the coprocessor's power transition + notifications. + apple,no-stm: type: boolean description: @@ -198,6 +206,15 @@ required: additionalProperties: false allOf: + - if: + properties: + compatible: + not: + contains: + const: apple,t6041-dockchannel-hid + then: + properties: + apple,afe-smc: false - if: properties: multi-touch: diff --git a/arch/arm64/boot/dts/apple/t6040-j616s.dts b/arch/arm64/boot/dts/apple/t6040-j616s.dts index d24ff77472e6c..a683e8da10c78 100644 --- a/arch/arm64/boot/dts/apple/t6040-j616s.dts +++ b/arch/arm64/boot/dts/apple/t6040-j616s.dts @@ -288,6 +288,9 @@ }; &mtp_hid { + /* SMC key gp1c powers the trackpad analog front end. */ + apple,afe-smc = <&smc>; + multi-touch { firmware-name = "apple/tpmtfw-j616s.bin"; }; diff --git a/drivers/hid/dockchannel-hid/Kconfig b/drivers/hid/dockchannel-hid/Kconfig index 593cbeb58416a..53ddaaac9057e 100644 --- a/drivers/hid/dockchannel-hid/Kconfig +++ b/drivers/hid/dockchannel-hid/Kconfig @@ -5,6 +5,7 @@ menu "DockChannel HID support" config HID_DOCKCHANNEL tristate "HID over DockChannel transport layer for Apple Silicon SoCs" depends on APPLE_DOCKCHANNEL && APPLE_RTKIT_HELPER && INPUT && OF && HID + depends on MFD_MACSMC || !MFD_MACSMC help Say Y here if you use an M2 or later Apple Silicon based laptop. The keyboard and touchpad are HID based devices connected via the diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c b/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c index 56efa764aaec6..64fde481641cd 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c @@ -196,6 +196,66 @@ static void dchid_unaligned_checksum_test(struct kunit *test) } } +/* A ring whose next frame straddles the end of the data area. */ +static void *dchid_test_ring(struct kunit *test, u32 consumer, u32 producer) +{ + struct dockchannel_hid *dchid = ((struct dchid_iface *)test->priv)->dchid; + u32 *indices; + + dchid->ring = kunit_kzalloc(test, DCHID_RING_SIZE, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, dchid->ring); + indices = dchid->ring; + indices[0] = producer; + indices[1] = consumer; + dchid->ring_read = consumer; + dchid->ring_registered = true; + dchid->use_ring = true; + return dchid->ring + DCHID_RING_HEADER_SIZE; +} + +static void dchid_ring_wrap_test(struct kunit *test) +{ + struct dchid_iface *iface = test->priv; + const u32 capacity = DCHID_RING_SIZE - DCHID_RING_HEADER_SIZE; + const u32 consumer = capacity - 12; + struct { + struct dchid_hdr hdr; + struct dchid_subhdr sub; + u8 id; + u8 pad[3]; + __le32 checksum; + } __packed frame = { + .hdr = { .hdr_len = sizeof(struct dchid_hdr), .channel = DCHID_CHANNEL_CMD, + .length = sizeof(struct dchid_subhdr) + 4, .seq = 7, .iface = 1 }, + .sub = { .flags = 0x80, .length = 1 }, + .id = 0x10, + }; + u8 *data; + + /* A complete, checksummed acknowledgment of the pending command. */ + frame.checksum = cpu_to_le32(U32_MAX - dchid_checksum(&frame, sizeof(frame) - 4)); + data = dchid_test_ring(test, consumer, sizeof(frame) - 12); + memcpy(data + consumer, &frame, 12); + memcpy(data, (u8 *)&frame + 12, sizeof(frame) - 12); + + KUNIT_EXPECT_EQ(test, dchid_drain_ring(iface->dchid), 0); + /* The frame arrived whole: its acknowledgment matched the pending command. */ + KUNIT_EXPECT_TRUE(test, completion_done(&iface->out_complete)); + KUNIT_EXPECT_EQ(test, iface->dchid->ring_read, sizeof(frame) - 12); + KUNIT_EXPECT_EQ(test, ((u32 *)iface->dchid->ring)[1], sizeof(frame) - 12); +} + +static void dchid_ring_bad_header_test(struct kunit *test) +{ + struct dchid_iface *iface = test->priv; + struct dchid_hdr hdr = { .hdr_len = sizeof(hdr) + 1, .length = 4 }; + u8 *data = dchid_test_ring(test, 0, 16); + + memcpy(data, &hdr, sizeof(hdr)); + KUNIT_EXPECT_EQ(test, dchid_drain_ring(iface->dchid), -EPROTO); + KUNIT_EXPECT_EQ(test, iface->dchid->ring_read, 0U); +} + static struct kunit_case dchid_parser_cases[] = { KUNIT_CASE(dchid_unaligned_checksum_test), KUNIT_CASE(dchid_short_ack_test), @@ -206,6 +266,8 @@ static struct kunit_case dchid_parser_cases[] = { KUNIT_CASE(dchid_short_input_test), KUNIT_CASE(dchid_init_descriptor_test), KUNIT_CASE(dchid_packet_checksum_test), + KUNIT_CASE(dchid_ring_wrap_test), + KUNIT_CASE(dchid_ring_bad_header_test), {} }; diff --git a/drivers/hid/dockchannel-hid/dockchannel-hid.c b/drivers/hid/dockchannel-hid/dockchannel-hid.c index a0af190a9b50a..61792a1c3dc4d 100644 --- a/drivers/hid/dockchannel-hid/dockchannel-hid.c +++ b/drivers/hid/dockchannel-hid/dockchannel-hid.c @@ -12,8 +12,12 @@ #include #include #include +#include +#include #include +#include #include +#include #include #include #include @@ -27,9 +31,12 @@ #define MAX_INTERFACES 16 -/* Data + checksum */ -#define MAX_PKT_SIZE (0xffff + 4) +/* Complete frame, including the largest aligned body and its checksum. */ +#define MAX_PKT_SIZE (8 + 0xfffc + 4) +#define DCHID_RING_SIZE SZ_2M +#define DCHID_RING_HEADER_SIZE 8 +#define DCHID_CHANNEL_NOTIFY 0x00 #define DCHID_CHANNEL_CMD 0x11 #define DCHID_CHANNEL_REPORT 0x12 @@ -76,6 +83,7 @@ struct dchid_init_hdr { #define INIT_TERMINATOR 2 #define INIT_PRODUCT_NAME 7 +#define CMD_REGISTER_RING 0x91 #define CMD_SEND_FIRMWARE 0x95 #define CMD_ENABLE_INTERFACE 0xb4 #define CMD_ACK_GPIO_CMD 0xa1 @@ -107,6 +115,10 @@ struct dchid_gpio_ack { u8 cmd[]; } __packed; +/* Multi-touch report 0x75: byte 23 bit 0 is the firmware's button state. */ +#define MT_REPORT_ID 0x75 +#define MT_REPORT_BUTTON 23 + #define STM_REPORT_ID 0x10 #define STM_REPORT_SERIAL 0x11 #define STM_REPORT_KEYBTYPE 0x14 @@ -185,12 +197,22 @@ struct dockchannel_hid { struct device *dev; struct dockchannel *dc; struct device_link *helper_link; + struct apple_smc *afe_smc; bool stopping; bool inherited_mtp; + /* Between the system sleep suspend and resume callbacks */ + bool suspended; /* Held from probe on when the multi-touch interface uses power method 2 */ struct gpio_desc *afe_reset; + struct mutex ring_mutex; + bool use_ring; + bool ring_registered; + int ring_error; + void *ring; + dma_addr_t ring_dma; + u32 ring_read; bool id_ready; struct dchid_stm_id device_id; char serial[64]; @@ -430,9 +452,85 @@ static int dchid_enable_interface(struct dchid_iface *iface) static int dchid_reset_interface(struct dchid_iface *iface, int state) { u8 msg[DCHID_PM1_CMD_SIZE]; + u8 msg2[DCHID_PM2_CMD_SIZE]; + bool afe = iface->dchid->afe_smc && iface->index == 1; + int ret; - dchid_pm1_command(iface->index, state, msg); - return dchid_comm_cmd(iface->dchid, msg, sizeof(msg)); + if (!iface->dchid->use_ring) { + dchid_pm1_command(iface->index, state, msg); + return dchid_comm_cmd(iface->dchid, msg, sizeof(msg)); + } + + /* + * T8132 boot capture: state 0 then 2, each with byte 4 equal to 0 + * then 1. This is only the observed provisioning sequence, not a + * contract for suspend, wake, or arbitrary interface power changes. + */ + dchid_pm2_command(iface->index, state, false, msg2); + ret = dchid_comm_cmd(iface->dchid, msg2, sizeof(msg2)); + if (ret < 0) + return ret; + +#if IS_REACHABLE(CONFIG_MFD_MACSMC) + if (afe) { + /* + * J616s macOS power sequence: gp1c high + 50 ms when turning + * off, low with no added delay when turning on. These are + * pKW4 function payloads, not gpio-macsmc CMD_OUTPUT values. + * The host operation sits between WillChange and HasChanged. + */ + __le32 value = cpu_to_le32(0x00010000 | (state == 0)); + + ret = apple_smc_write(iface->dchid->afe_smc, SMC_KEY(gp1c), + &value, sizeof(value)); + if (ret < 0) { + dev_err(iface->dchid->dev, + "AFE power sequence failed: %d; reboot required\n", ret); + return ret; + } + if (state == 0) + msleep(50); + } +#endif + dchid_pm2_command(iface->index, state, true, msg2); + return dchid_comm_cmd(iface->dchid, msg2, sizeof(msg2)); +} + +static int dchid_register_ring(struct dockchannel_hid *dchid) +{ + struct { + u8 cmd; + u8 reserved[2]; + u64 addr; + u32 size; + } __packed msg = { + .cmd = CMD_REGISTER_RING, + .addr = dchid->ring_dma, + .size = DCHID_RING_SIZE, + }; + /* Observed in both macOS and standalone T8132 bringup before 0x91. */ + u8 prepare[] = { 0xc1, 0x02 }; + int ret = 0; + + if (!dchid->use_ring) + return 0; + + mutex_lock(&dchid->ring_mutex); + ret = dchid->ring_error; + if (!dchid->ring_registered) { + ret = dchid_comm_cmd(dchid, prepare, sizeof(prepare)); + if (ret < 0) { + mutex_unlock(&dchid->ring_mutex); + return ret; + } + /* The registration ACK itself arrives through the ring. */ + dma_wmb(); + WRITE_ONCE(dchid->ring_registered, true); + ret = dchid_comm_cmd(dchid, &msg, sizeof(msg)); + dchid->ring_error = ret; + } + mutex_unlock(&dchid->ring_mutex); + return ret; } /* @@ -562,6 +660,11 @@ static int dchid_request_gpio(struct dchid_iface *iface) if (iface->firmware_managed_reset) return -EINVAL; + if (iface->dchid->use_ring) { + dev_err_once(iface->dchid->dev, "T8132 SMC reset GPIO operation is not established\n"); + return -EOPNOTSUPP; + } + if (iface->gpio) return 0; @@ -604,14 +707,17 @@ static int dchid_start_interface(struct dchid_iface *iface) dev_dbg(iface->dchid->dev, "Starting interface %s\n", iface->name); iface->starting = true; + ret = dchid_register_ring(iface->dchid); + if (ret < 0) + goto err; /* Look to see if we need firmware */ ret = dchid_get_firmware(iface, &fw, &size); if (ret < 0) goto err; - /* If we need a GPIO, make sure we have it. */ - if (iface->gpio_announced) { + /* T8132 startup does not use the older host GPIO reset. */ + if (!iface->dchid->use_ring && iface->gpio_announced) { ret = dchid_request_gpio(iface); if (ret < 0) goto err; @@ -653,8 +759,10 @@ static int dchid_start_interface(struct dchid_iface *iface) if (ret < 0) goto err; } else { - dchid_reset_interface(iface, DCHID_POWER_STATE_OFF); - dchid_reset_interface(iface, DCHID_POWER_STATE_ON); + ret = dchid_reset_interface(iface, DCHID_POWER_STATE_OFF); + /* After a failed AFE power-off the trackpad stays off: no retry. */ + if (ret >= 0 || !iface->dchid->afe_smc || iface->index != 1) + dchid_reset_interface(iface, DCHID_POWER_STATE_ON); } } @@ -742,6 +850,9 @@ static int dchid_raw_request(struct hid_device *hdev, if (READ_ONCE(iface->dchid->stopping)) return -ESHUTDOWN; + /* Fail fast: an answer that arrives in suspend-to-idle is not waited for. */ + if (READ_ONCE(iface->dchid->suspended)) + return -EBUSY; switch (reqtype) { case HID_REQ_GET_REPORT: @@ -1002,7 +1113,7 @@ static void dchid_handle_init(struct dockchannel_hid *dchid, void *data, size_t break; } - if (hdr->more_packets) + if (hdr->more_packets || (dchid->use_ring && !iface->hid_desc)) return; /* We need to enable STM first, since it'll give us the device IDs */ @@ -1205,73 +1316,179 @@ static void dchid_handle_ack(struct dchid_iface *iface, struct dchid_hdr *hdr, v complete(&iface->out_complete); } -static void dchid_handle_packet(void *cookie, size_t avail) +/* + * Whether a report should wake the system: any keyboard report, and a + * multi-touch report only while the trackpad is clicked. Touches, the + * accelerometer and the other interfaces do not wake it. + */ +static bool dchid_wake_report(struct dchid_iface *iface, void *data) +{ + struct dchid_subhdr *shdr = data; + u8 *payload = data + sizeof(*shdr); + + if (FIELD_GET(FLAGS_GROUP, shdr->flags) != HID_INPUT_REPORT) + return false; + if (!strcmp(iface->name, "keyboard")) + return shdr->length > 0; + return !strcmp(iface->name, "multi-touch") && + shdr->length > MT_REPORT_BUTTON && payload[0] == MT_REPORT_ID && + (payload[MT_REPORT_BUTTON] & 1); +} + +/* Return the complete frame size, or reject a lost framing boundary. */ +static int dchid_frame_size(const struct dchid_hdr *hdr) +{ + size_t length = hdr->length; + + if (hdr->hdr_len != sizeof(*hdr) || !IS_ALIGNED(length, 4)) + return -EPROTO; + return sizeof(*hdr) + length + sizeof(u32); +} + +/* + * Both receive transports pass complete frames here. Header bytes 6/7 and + * application padding are opaque, checksum-covered data, not required zeroes. + */ +static int dchid_dispatch(struct dockchannel_hid *dchid, struct dchid_hdr *hdr, + void *data, bool fifo) { - struct dockchannel_hid *dchid = cookie; - struct dchid_hdr hdr; - struct dchid_work *work; struct dchid_iface *iface; + struct dchid_work *work; u32 checksum; - if (READ_ONCE(dchid->stopping)) - return; - - if (dockchannel_recv(dchid->dc, &hdr, sizeof(hdr)) != sizeof(hdr)) { - dev_err(dchid->dev, "Read failed (header)\n"); - return; + if (!dchid_verify_checksum(hdr, data, &checksum)) { + dev_err(dchid->dev, "Checksum mismatch (iface %d): 0x%08x != 0xffffffff\n", + hdr->iface, checksum); + return 0; } - if (hdr.hdr_len != sizeof(hdr)) { - dev_err(dchid->dev, "Bad header length %d\n", hdr.hdr_len); - goto done; + if (dchid->use_ring && hdr->channel == DCHID_CHANNEL_NOTIFY) { + /* A FIFO notification has no application subheader or report ID. */ + return fifo && !hdr->length && hdr->iface == IFACE_COMM; } - if (dockchannel_recv(dchid->dc, dchid->pkt_buf, hdr.length + 4) != (hdr.length + 4)) { - dev_err(dchid->dev, "Read failed (body)\n"); - goto done; + if (hdr->iface >= MAX_INTERFACES || hdr->length < sizeof(struct dchid_subhdr)) + return 0; + iface = dchid->ifaces[hdr->iface]; + if (!iface) { + dev_err(dchid->dev, "Received packet for uninitialized iface %d\n", hdr->iface); + return 0; } - if (!dchid_verify_checksum(&hdr, dchid->pkt_buf, &checksum)) { - dev_err(dchid->dev, "Checksum mismatch (iface %d): 0x%08x != 0xffffffff\n", - hdr.iface, checksum); - goto done; + switch (hdr->channel) { + case DCHID_CHANNEL_CMD: + dchid_handle_ack(iface, hdr, data); + return 0; + case DCHID_CHANNEL_REPORT: + /* pm_wakeup_hard_event() does nothing while wakeup is disabled. */ + if (READ_ONCE(dchid->suspended) && + hdr->length >= sizeof(struct dchid_subhdr) + + ((struct dchid_subhdr *)data)->length && + dchid_wake_report(iface, data)) + pm_wakeup_hard_event(dchid->dev); + break; + default: + dev_warn(dchid->dev, "Unknown channel 0x%x, treating as report...\n", + hdr->channel); + break; } + work = kzalloc(sizeof(*work) + hdr->length, GFP_KERNEL); + if (!work) + return 0; + work->hdr = *hdr; + work->iface = iface; + memcpy(work->data, data, hdr->length); + INIT_WORK(&work->work, dchid_packet_work); + queue_work(iface->wq, &work->work); + return 0; +} - if (hdr.iface >= MAX_INTERFACES) { - dev_err(dchid->dev, "Bad iface %d\n", hdr.iface); - goto done; - } +static void dchid_ring_copy(struct dockchannel_hid *dchid, void *dst, + u32 offset, size_t length) +{ + size_t first = min_t(size_t, length, + DCHID_RING_SIZE - DCHID_RING_HEADER_SIZE - offset); + void *data = dchid->ring + DCHID_RING_HEADER_SIZE; - iface = dchid->ifaces[hdr.iface]; + memcpy(dst, data + offset, first); + memcpy(dst + first, data, length - first); +} - if (!iface) { - dev_err(dchid->dev, "Received packet for uninitialized iface %d\n", hdr.iface); - goto done; +static int dchid_drain_ring(struct dockchannel_hid *dchid) +{ + const u32 capacity = DCHID_RING_SIZE - DCHID_RING_HEADER_SIZE; + u32 *indices = dchid->ring; + u32 producer, consumer, available; + int size; + + if (!READ_ONCE(dchid->ring_registered)) { + /* The firmware can notify before it has the ring address: nothing to read. */ + dev_dbg(dchid->dev, "Ring notification before registration\n"); + return 0; } - switch (hdr.channel) { - case DCHID_CHANNEL_CMD: - dchid_handle_ack(iface, &hdr, dchid->pkt_buf); - goto done; - case DCHID_CHANNEL_REPORT: - break; - default: - dev_warn(dchid->dev, "Unknown channel 0x%x, treating as report...\n", - hdr.channel); - break; + while (1) { + producer = READ_ONCE(indices[0]); + consumer = READ_ONCE(indices[1]); + if (producer >= capacity || consumer != dchid->ring_read || + !IS_ALIGNED(producer, 4)) + return -EPROTO; + /* Read only the bytes published by this producer snapshot. */ + dma_rmb(); + available = producer >= consumer ? producer - consumer : + capacity - consumer + producer; + if (available < sizeof(struct dchid_hdr)) + return 0; + dchid_ring_copy(dchid, dchid->pkt_buf, consumer, + sizeof(struct dchid_hdr)); + size = dchid_frame_size((void *)dchid->pkt_buf); + if (size < 0) + return size; + if (available < size) + return 0; + dchid_ring_copy(dchid, dchid->pkt_buf, consumer, size); + dchid_dispatch(dchid, (void *)dchid->pkt_buf, + dchid->pkt_buf + sizeof(struct dchid_hdr), false); + consumer += size; + if (consumer >= capacity) + consumer -= capacity; + dchid->ring_read = consumer; + /* All frame reads must complete before the producer can reuse it. */ + mb(); + WRITE_ONCE(indices[1], consumer); + cond_resched(); } + return 0; +} - work = kzalloc(sizeof(*work) + hdr.length, GFP_KERNEL); - if (!work) - goto done; +static void dchid_handle_packet(void *cookie, size_t avail) +{ + struct dockchannel_hid *dchid = cookie; + struct dchid_hdr hdr; + int ret; - work->hdr = hdr; - work->iface = iface; - memcpy(work->data, dchid->pkt_buf, hdr.length); - INIT_WORK(&work->work, dchid_packet_work); + if (READ_ONCE(dchid->stopping)) + return; - queue_work(iface->wq, &work->work); + if (dockchannel_recv(dchid->dc, &hdr, sizeof(hdr)) != sizeof(hdr)) { + dev_err(dchid->dev, "Read failed (header)\n"); + return; + } + if (hdr.hdr_len != sizeof(hdr)) { + dev_err(dchid->dev, "Bad header length %d\n", hdr.hdr_len); + goto done; + } + if (dockchannel_recv(dchid->dc, dchid->pkt_buf, hdr.length + 4) != hdr.length + 4) { + dev_err(dchid->dev, "Read failed (body)\n"); + goto done; + } + + if (dchid_dispatch(dchid, &hdr, dchid->pkt_buf, true)) { + ret = dchid_drain_ring(dchid); + if (ret < 0) + dev_err(dchid->dev, "Receive ring failed: %d\n", ret); + } done: if (!READ_ONCE(dchid->stopping)) @@ -1279,6 +1496,57 @@ static void dchid_handle_packet(void *cookie, size_t avail) sizeof(struct dchid_hdr)); } +/* AFE power through SMC key gp1c, for boards whose device tree links the SMC. */ +static int dchid_bind_afe_smc(struct dockchannel_hid *dchid) +{ + struct device *dev = dchid->dev; +#if IS_REACHABLE(CONFIG_MFD_MACSMC) + struct device_node *node; + struct platform_device *pdev; + struct device_link *link; + struct apple_smc_key_info info; + int ret; +#endif + + if (!of_find_property(dev->of_node, "apple,afe-smc", NULL)) + return 0; + if (!of_device_is_compatible(dev->of_node, "apple,t6041-dockchannel-hid")) + return -EINVAL; +#if !IS_REACHABLE(CONFIG_MFD_MACSMC) + return -EOPNOTSUPP; +#else + node = of_parse_phandle(dev->of_node, "apple,afe-smc", 0); + if (!node) + return -EINVAL; + /* What macsmc binds to: its driver data is read as an apple_smc below */ + if (!of_device_is_compatible(node, "apple,t8103-smc") && + !of_device_is_compatible(node, "apple,smc")) { + of_node_put(node); + return -EINVAL; + } + pdev = of_find_device_by_node(node); + of_node_put(node); + if (!pdev) + return -EPROBE_DEFER; + link = device_link_add(dev, &pdev->dev, DL_FLAG_AUTOREMOVE_CONSUMER); + put_device(&pdev->dev); + if (!link) + return -EINVAL; + if (link->supplier->links.status != DL_DEV_DRIVER_BOUND) + return -EPROBE_DEFER; + dchid->afe_smc = dev_get_drvdata(link->supplier); + if (!dchid->afe_smc) + return -EPROBE_DEFER; + ret = apple_smc_get_key_info(dchid->afe_smc, SMC_KEY(gp1c), &info); + if (ret < 0) + return ret; + if (info.size != 4 || !(info.flags & APPLE_SMC_WRITABLE)) + return -EINVAL; + dev_info(dev, "AFE power sequence via SMC gp1c\n"); + return 0; +#endif +} + static int dockchannel_hid_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -1302,6 +1570,13 @@ static int dockchannel_hid_probe(struct platform_device *pdev) stm = device_get_named_child_node(dev, "stm"); dchid->id_ready = !stm || !fwnode_device_is_available(stm); fwnode_handle_put(stm); + mutex_init(&dchid->ring_mutex); + /* The M4 Pro (J616s) MTP uses the same 26.6.2 firmware transport as T8132. */ + dchid->use_ring = of_device_is_compatible(dev->of_node, "apple,t6041-dockchannel-hid"); + + ret = dchid_bind_afe_smc(dchid); + if (ret) + return dev_err_probe(dev, ret, "AFE power sequence preflight failed\n"); /* * The STM interface reports the machine's vendor and product IDs, @@ -1408,15 +1683,22 @@ static int dockchannel_hid_probe(struct platform_device *pdev) } /* - * Keyboard and trackpad input wakes the system. The coprocessor keeps - * running across suspend-to-idle and keeps sending reports through - * the FIFO, so its receive interrupt is the wake interrupt. Without - * it the machine still works, it just cannot be woken from here. + * A keypress or a trackpad click wakes the system. The coprocessor + * keeps running across suspend-to-idle and keeps sending through the + * FIFO, so received frames are classified (dchid_wake_report()) and + * other traffic does not wake it. Without a wakeup source the machine + * still works, it just cannot be woken from here. */ ret = dockchannel_init_wakeup(dchid->dc); if (ret) dev_warn(dev, "Failed to set up wakeup: %d\n", ret); + if (dchid->use_ring) { + dchid->ring = dmam_alloc_coherent(dev, DCHID_RING_SIZE, &dchid->ring_dma, + GFP_KERNEL); + if (!dchid->ring) + return -ENOMEM; + } dchid->new_iface_wq = alloc_ordered_workqueue("dchid-new", 0); if (!dchid->new_iface_wq) return -ENOMEM; @@ -1465,6 +1747,31 @@ static void dockchannel_hid_remove(struct platform_device *pdev) } } +/* + * The coprocessor keeps running across suspend-to-idle: there is no known + * sleep command for it, and its RTKit session and the trackpad AFE power + * sequence are not restartable. Only mark the sleep window, in which input + * may wake the system and requests from the HID core fail fast. + */ +static int dockchannel_hid_suspend(struct device *dev) +{ + struct dockchannel_hid *dchid = dev_get_drvdata(dev); + + WRITE_ONCE(dchid->suspended, true); + return 0; +} + +static int dockchannel_hid_resume(struct device *dev) +{ + struct dockchannel_hid *dchid = dev_get_drvdata(dev); + + WRITE_ONCE(dchid->suspended, false); + return 0; +} + +static DEFINE_SIMPLE_DEV_PM_OPS(dockchannel_hid_pm_ops, dockchannel_hid_suspend, + dockchannel_hid_resume); + static const struct of_device_id dockchannel_hid_of_match[] = { { .compatible = "apple,dockchannel-hid" }, {}, @@ -1478,6 +1785,7 @@ static struct platform_driver dockchannel_hid_driver = { .of_match_table = dockchannel_hid_of_match, /* The coprocessor announces its interfaces only once per boot. */ .suppress_bind_attrs = true, + .pm = pm_sleep_ptr(&dockchannel_hid_pm_ops), }, .probe = dockchannel_hid_probe, .remove = dockchannel_hid_remove, diff --git a/drivers/soc/apple/dockchannel.c b/drivers/soc/apple/dockchannel.c index cf9e74bef94e3..77dc01f8b4359 100644 --- a/drivers/soc/apple/dockchannel.c +++ b/drivers/soc/apple/dockchannel.c @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -255,8 +254,13 @@ struct dockchannel *dockchannel_init(struct platform_device *pdev) if (ret) return ERR_PTR(dev_err_probe(dev, ret, "Failed to request TX IRQ")); + /* + * Received data keeps being handled through system sleep, so that the + * client can tell input that should wake the system from other traffic. + */ ret = devm_request_threaded_irq(dev, dockchannel->rx_irq, dockchannel_rx_irq, - dockchannel_rx_irq_thread, IRQF_NO_AUTOEN, + dockchannel_rx_irq_thread, + IRQF_NO_AUTOEN | IRQF_NO_SUSPEND, "apple-dockchannel-rx", dockchannel); if (ret) return ERR_PTR(dev_err_probe(dev, ret, "Failed to request RX IRQ")); @@ -266,26 +270,21 @@ struct dockchannel *dockchannel_init(struct platform_device *pdev) EXPORT_SYMBOL(dockchannel_init); /** - * dockchannel_init_wakeup() - Let data from the remote side wake the system + * dockchannel_init_wakeup() - Make the FIFO's device a wakeup source * @dockchannel: FIFO returned by dockchannel_init() * - * Make the FIFO's device a wakeup source, enabled by default, with the - * receive interrupt as its wake interrupt. While wakeup is enabled for the - * device, the PM core keeps that interrupt armed across system sleep, and - * data that arrives then wakes the system. The data itself is read after - * resume, as usual. + * Make the FIFO's device a wakeup source, enabled by default. The receive + * interrupt is not a wake interrupt: it stays enabled through system sleep + * and the client keeps receiving data, so that any traffic does not end a + * suspend-to-idle. The client decides which data wakes the system and + * reports it with pm_wakeup_hard_event() on the FIFO's device, which does + * nothing while wakeup is disabled for the device. * * Return: 0 on success, or a negative error code. */ int dockchannel_init_wakeup(struct dockchannel *dockchannel) { - int ret; - - ret = devm_device_init_wakeup(dockchannel->dev); - if (ret) - return ret; - - return devm_pm_set_wake_irq(dockchannel->dev, dockchannel->rx_irq); + return devm_device_init_wakeup(dockchannel->dev); } EXPORT_SYMBOL(dockchannel_init_wakeup);