diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index d9ece372d36a4..2168dbc58fce8 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -3766,7 +3767,12 @@ static void tipd_replug_work(struct work_struct *work) u8 key[4]; mutex_lock(&tps->lock); - ret = tipd_replug_key(tps, key); + if (tps->kept_attach_replug) { + memcpy(key, tps->kept_attach_key, sizeof(key)); + ret = 0; + } else { + ret = tipd_replug_key(tps, key); + } if (!ret) ret = tps6598x_exec_cmd_gap(tps, "LOCK", sizeof(key), key, sizeof(result), &result, 1000, 0, CD321X_SSPS_READ_GAP_US); @@ -3802,6 +3808,7 @@ static void tipd_replug_work(struct work_struct *work) msleep(500); } + tps->kept_attach_replug = false; if (ret || lock_ret) dev_warn(tps->dev, "port replug for its partner failed: %d (unlock result %u), locked again: %d (try %u)\n", ret, result, lock_ret, tries); @@ -3859,6 +3866,77 @@ int tipd_port_replug(struct usb_role_switch *sw, u8 seconds) } EXPORT_SYMBOL_GPL(tipd_port_replug); +/* + * The CD321x attach state, byte 7 of register 0x7e: bit 3 is set once the controller has + * completed an attach with the OS running (after a replug, and under macOS), and bit 2 keeps an + * attach across a warm reset. + */ +#define CD321X_ATTACH_STATE_REG 0x7e +#define CD321X_ATTACH_STATE_BYTE 7 +#define CD321X_ATTACH_STATE_KEPT BIT(2) +#define CD321X_ATTACH_STATE_DONE BIT(3) +#define CD321X_KEPT_ATTACH_REPLUG_S 5 + +/* + * A CD321x takes the board number reversed as its LOCK key, as the J716c's does ("617J" in + * cd321x_inherited_boards). It comes from the machine's compatible: "apple,j414c" gives "414J". + */ +static int cd321x_board_key(u8 key[4]) +{ + struct device_node *root = of_find_node_by_path("/"); + const char *s; + int ret = -EOPNOTSUPP; + + if (root && !of_property_read_string(root, "compatible", &s) && + !strncmp(s, "apple,j", 7) && isdigit(s[7]) && isdigit(s[8]) && isdigit(s[9])) { + key[0] = s[9]; + key[1] = s[8]; + key[2] = s[7]; + key[3] = 'J'; + ret = 0; + } + of_node_put(root); + return ret; +} + +/* + * A partner attached while the OS ran, then a reboot: the controller keeps that attach (bits 2 + * and 3 of the attach state) but reports no plug after the warm reset, with the partner still + * there, until something makes it detect the partner again. On a J414c with a Thunderbolt dock + * that is every reboot after a replug: no USB4 link, no displays, no charging, until the cable + * is pulled and plugged back. Replug such a port at the probe, as that cable does. + */ +static void cd321x_replug_kept_attach(struct tps6598x *tps) +{ + const u8 kept = CD321X_ATTACH_STATE_KEPT | CD321X_ATTACH_STATE_DONE; + u8 state[CD321X_ATTACH_STATE_BYTE + 1]; + u32 status; + int ret; + + if (!device_is_compatible(tps->dev, "apple,cd321x")) + return; + scoped_guard(mutex, &tps->lock) { + if (!tps6598x_read_status(tps, &status) || (status & TPS_STATUS_PLUG_PRESENT)) + return; + if (tps6598x_block_read(tps, CD321X_ATTACH_STATE_REG, state, sizeof(state)) || + (state[CD321X_ATTACH_STATE_BYTE] & kept) != kept) + return; + } + if (cd321x_board_key(tps->kept_attach_key)) + return; + + guard(mutex)(&tipd_ports_lock); + if (!tps->port_node.next || list_empty(&tps->port_node) || work_pending(&tps->replug_work)) + return; + tps->kept_attach_replug = true; + tps->replug_seconds = CD321X_KEPT_ATTACH_REPLUG_S; + ret = queue_work(system_freezable_wq, &tps->replug_work) ? 0 : -EBUSY; + if (ret) + tps->kept_attach_replug = false; + dev_info(tps->dev, "no partner, but the controller kept an earlier attach (%#04x): replugging the port: %d\n", + state[CD321X_ATTACH_STATE_BYTE], ret); +} + /* * The same replug for this port, asked for through debugfs ("replug"). -ENODEV while the port is * not on tipd_ports: without a role switch, and from tipd_port_list_del() on, which cancels the @@ -4205,6 +4283,7 @@ int tipd_init(struct tps6598x *tps) } tipd_port_list_add(tps); + cd321x_replug_kept_attach(tps); tipd_debugfs_register(tps); if (tps->data->ready) tps->data->ready(tps); diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h index eb77f76379ca4..62b1b682364de 100644 --- a/drivers/usb/typec/tipd/tps6598x.h +++ b/drivers/usb/typec/tipd/tps6598x.h @@ -361,6 +361,9 @@ struct tps6598x { struct list_head port_node; struct work_struct replug_work; u8 replug_seconds; + /* The unlock key of a replug queued for a kept attach (cd321x_replug_kept_attach()). */ + u8 kept_attach_key[4]; + bool kept_attach_replug; struct device *dev; struct regmap *regmap; struct mutex lock; /* device lock */