diff --git a/Documentation/devicetree/bindings/usb/apple,sn201202x.yaml b/Documentation/devicetree/bindings/usb/apple,sn201202x.yaml index a074d56e4c305..3109fc7be764b 100644 --- a/Documentation/devicetree/bindings/usb/apple,sn201202x.yaml +++ b/Documentation/devicetree/bindings/usb/apple,sn201202x.yaml @@ -47,11 +47,23 @@ properties: Tell the controller the system's power state (SSPS) around a firmware-driven suspend, S3 before it and S0 after. + apple,cable-change-ssps: + type: boolean + description: + At a cable change while running, send the controller the system power + state sequence macOS sends it (traced on the J714s). At a detach, write + register 0x50, then SSPS 0x02. At an attach, SSPS 0x00, then USBw 0x00 + after the attach's debounce. + apple,debug-lock-key: $ref: /schemas/types.yaml#/definitions/string + pattern: "^.{4}$" description: - The four-character key that unlocks the controller's debug mode, for - rearming the debug link after a sleep ended it. + The four-character key (the board number, reversed, such as "417J" on + the J714s) that the controller's LOCK command takes to unlock the + commands that need it. These are its debug mode, for rearming the + debug link after a sleep ended it, and a disconnect and reattach of + its partner (DISC). apple,event-irq-in-sleep: type: boolean diff --git a/arch/arm64/boot/dts/apple/t6050-j714s-sleep.dtsi b/arch/arm64/boot/dts/apple/t6050-j714s-sleep.dtsi index 9f5098d072b89..9bdac45852c22 100644 --- a/arch/arm64/boot/dts/apple/t6050-j714s-sleep.dtsi +++ b/arch/arm64/boot/dts/apple/t6050-j714s-sleep.dtsi @@ -140,17 +140,23 @@ status = "okay"; }; -/* The port controllers put in system power state S3 before the commit, then SLEEP */ +/* + * The port controllers put in system power state S3 before the commit, then SLEEP; the USB-C + * ports' also get macOS's power state sequence at a cable change. + */ &hpm0 { apple,sleep-ssps; + apple,cable-change-ssps; }; &hpm1 { apple,sleep-ssps; + apple,cable-change-ssps; }; &hpm2 { apple,sleep-ssps; + apple,cable-change-ssps; }; &hpm5 { diff --git a/arch/arm64/boot/dts/apple/t6050-j714s.dts b/arch/arm64/boot/dts/apple/t6050-j714s.dts index 064e758b07a00..8b2a60a7b117a 100644 --- a/arch/arm64/boot/dts/apple/t6050-j714s.dts +++ b/arch/arm64/boot/dts/apple/t6050-j714s.dts @@ -755,6 +755,22 @@ /* The Neural Engine, started only after the boot loader's preload hand-off */ #include "t6050-j714s-ane.dtsi" +/* + * The USB-C port controllers take LOCK with the board number reversed ("417J"): their debug mode + * and a partner replug ("DISC") need it. + */ +&hpm0 { + apple,debug-lock-key = "417J"; +}; + +&hpm1 { + apple,debug-lock-key = "417J"; +}; + +&hpm2 { + apple,debug-lock-key = "417J"; +}; + #include "t6050-j714s-sleep.dtsi" #include "t6050-j714s-sleep-soc.dtsi" #include "t6050-j714s-sleep-adt.dtsi" diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index 0b72a107a28ac..d5c193da500bf 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -1529,8 +1529,9 @@ static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state); * the attach: the events and status read, then DATA1 = 0x00 and SSPS; ~530 ms later * SYS_PWR_STATE read twice, DATA1 = 0x00 and USBw. Linux sent neither outside a sleep, so after a * cable change the controller was left as the change left it. Here: SSPS 0x02 at a detach, SSPS - * 0x00 at an attach and USBw 0x00 at the update after the attach's debounce (500 ms), on every - * SN201202x of the J714s, only while running (not in a sleep's phases). + * 0x00 at an attach and USBw 0x00 at the update after the attach's debounce (500 ms), for an + * SN201202x whose DT asks for it (apple,cable-change-ssps, the traced J714s's USB-C ports), only + * while running (not in a sleep's phases). */ #define TIPD_SSPS_MACOS_DETACHED 0x02 /* @@ -1540,14 +1541,20 @@ static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state); */ #define TIPD_REG_MACOS_DETACH 0x50 -/* Only the USB-C ports' controllers, as traced: MagSafe's has no connector */ +/* + * An SN201202x whose DT asks for macOS's cable-change sequence (apple,cable-change-ssps), and only + * a USB-C port's, as traced: MagSafe's has no connector. Not a controller in a debug route, from + * before Linux or opened through debugfs: that route is its owner's, and gets no power state + * commands. + */ static bool cd321x_macos_cable_port(struct cd321x *cd321x) { - struct device *dev = cd321x->tps.dev; + struct tps6598x *tps = &cd321x->tps; + struct device *dev = tps->dev; struct fwnode_handle *con; - if (!device_is_compatible(dev, "apple,sn201202x") || - !of_machine_is_compatible("apple,j714s")) + if (!device_is_compatible(dev, "apple,sn201202x") || tps->dbma_at_probe || + tipd_debugfs_dbma_held(tps) || !device_property_read_bool(dev, "apple,cable-change-ssps")) return false; con = device_get_named_child_node(dev, "connector"); fwnode_handle_put(con); @@ -3690,13 +3697,26 @@ static void tipd_port_list_del(struct tps6598x *tps) cancel_work_sync(&tps->replug_work); } -/* The J714s's port controllers take "DISC" unlocked with this key, the reversed target identifier. */ -static const u8 tipd_replug_key_j714s[4] = { '4', '1', '7', 'J' }; +/* + * An SN201202x takes "DISC" unlocked with the key its DT gives it (apple,debug-lock-key, the board + * number reversed). + */ +static int tipd_replug_key(struct tps6598x *tps, u8 key[4]) +{ + const char *s; + + if (!device_is_compatible(tps->dev, "apple,sn201202x") || + device_property_read_string(tps->dev, "apple,debug-lock-key", &s) || strlen(s) != 4) + return -EOPNOTSUPP; + memcpy(key, s, 4); + return 0; +} bool tipd_replug_supported(struct tps6598x *tps) { - return of_machine_is_compatible("apple,j714s") && - device_is_compatible(tps->dev, "apple,sn201202x"); + u8 key[4]; + + return !tipd_replug_key(tps, key); } /* @@ -3716,10 +3736,13 @@ static void tipd_replug_work(struct work_struct *work) unsigned long timeout; int ret, lock_ret, mode = -1; unsigned int tries; + u8 key[4]; mutex_lock(&tps->lock); - ret = tps6598x_exec_cmd_gap(tps, "LOCK", sizeof(tipd_replug_key_j714s), tipd_replug_key_j714s, - sizeof(result), &result, 1000, 0, CD321X_SSPS_READ_GAP_US); + 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); if (!ret && result) ret = -EIO; if (!ret) @@ -3788,7 +3811,8 @@ static int tipd_replug_queue(struct tps6598x *tps, u8 seconds) * DP alt mode hub whose SuperSpeed side does not come back after some wakes (dwc3-apple). A * display on the port goes dark meanwhile. The replug runs on the port's own work, after this * returns: 0 when it is queued, -EBUSY if one already is, -EOPNOTSUPP on a controller that is - * not known to take it (the J714s's SN201202x only), -ENODEV if no port drives @sw. + * not known to take it (an SN201202x whose DT gives apple,debug-lock-key), -ENODEV if no port + * drives @sw. */ int tipd_port_replug(struct usb_role_switch *sw, u8 seconds) { diff --git a/drivers/usb/typec/tipd/debugfs.c b/drivers/usb/typec/tipd/debugfs.c index 498233508f9c6..2214cf305a673 100644 --- a/drivers/usb/typec/tipd/debugfs.c +++ b/drivers/usb/typec/tipd/debugfs.c @@ -15,8 +15,8 @@ * read-only register access: pread() with the register as offset * (0x00..0x7f) and the length (1..64 bytes) as count. * /sys/kernel/debug/usb/tipd//replug - * Only on a port tipd can replug its partner on (the J714s's SN201202x - * with a USB role switch). + * Only on a port tipd can replug its partner on (an SN201202x whose DT + * gives apple,debug-lock-key, with a USB role switch). * write: the seconds (1..10) the partner is disconnected for. One replug, * queued as the driver queues its own after a sleep (see * tipd_port_replug()); -EBUSY while one is queued and not yet diff --git a/drivers/usb/typec/tipd/spmi.c b/drivers/usb/typec/tipd/spmi.c index 38d4a8003dcf0..b1644cf2d988c 100644 --- a/drivers/usb/typec/tipd/spmi.c +++ b/drivers/usb/typec/tipd/spmi.c @@ -1024,7 +1024,8 @@ static int sn201202x_probe(struct spmi_device *device) tps->data->irq_handler(tps->irq, tps); sn201202x_idle_sleep_start(tps); - if (sn201202x_j714s()) { + /* A controller that follows the system's power state can take macOS's USBw at its entry */ + if (device_property_read_bool(tps->dev, "apple,sleep-ssps")) { struct fwnode_handle *con = device_get_named_child_node(tps->dev, "connector"); /* MagSafe's controller has no connector and never gets macOS's USBw */ @@ -1282,8 +1283,12 @@ static bool sn201202x_macos_entry(struct sn201202x *sn) { struct device *dev = sn->cd.tps.dev; - /* A port that arms USB wake (none on the J714s) needs the controller at the suspend */ - return sn201202x_j714s() && sn->polled && + /* + * Keyed on what the controller presents: polled (no interrupt), following the system's + * power state, for a firmware sleep. A port that arms USB wake itself (none on the J714s) + * needs the controller at the suspend. + */ + return sn->polled && pm_suspend_via_firmware() && device_property_read_bool(dev, "apple,sleep-ssps") && !device_property_read_bool(dev, "apple,usb-remote-wake");