diff --git a/drivers/iio/common/aop_sensors/aop_als.rs b/drivers/iio/common/aop_sensors/aop_als.rs index 401f4f2bb32de..375cf57c79384 100644 --- a/drivers/iio/common/aop_sensors/aop_als.rs +++ b/drivers/iio/common/aop_sensors/aop_als.rs @@ -139,14 +139,20 @@ fn arm_ct817(aop: &dyn AOP, dev: &device::Device, svc: &EPICService) -> Result<( set(ALS_PROP_INTERVAL, ALS_INTERVAL_US, "interval") } -/// Arms the CT817 again after system sleep. +/// Stops the reports for system sleep and starts them again after it. /// -/// The firmware stops the light sensor's reports for system sleep and does not -/// start them again: on the J700 every read after the first wake of a boot -/// returned the last sample from before that sleep, while the room's light -/// changed. The sensor starts reporting only on a zero to non-zero transition -/// of its interval, so the wake repeats the arming of the probe, which starts -/// with an interval of zero. +/// The sensor reports on its own every `ALS_INTERVAL_US`, and each report is +/// an AOP mailbox interrupt. In suspend-to-idle those interrupts go on and +/// wake a CPU for a reading nobody takes, so the reports stop before the +/// sleep, as the lid angle driver's do. +/// +/// The CT817 needs more after the sleep: the firmware stops its reports for +/// system sleep and does not start them again. On the J700 every read after +/// the first wake of a boot returned the last sample from before that sleep, +/// while the room's light changed. The sensor starts reporting only on a zero +/// to non-zero transition of its interval, so the wake repeats the arming of +/// the probe, which starts with an interval of zero. The other parts need only +/// their interval back. #[repr(C)] struct PmHook { /// First, so that the callback finds the hook from it. @@ -154,6 +160,12 @@ struct PmHook { dev: ARef, aop: Arc, service: EPICService, + /// The part arms only through the whole CT817 sequence. + ct817: bool, + /// A stop was tried for the sleep in progress. It is set before the + /// command goes out: a stop whose reply was lost may still have been + /// applied, so the wake restores the interval after every attempt. + stop_attempted: Atomic, registered: Atomic, } @@ -167,14 +179,45 @@ unsafe extern "C" fn pm_event( action: usize, _data: *mut c_void, ) -> c_int { - if action as u32 != bindings::PM_POST_SUSPEND { - return bindings::NOTIFY_DONE as c_int; - } // SAFETY: `nb` is the first field of a registered `PmHook`. let hook = unsafe { &*nb.cast::() }; - match arm_ct817(hook.aop.as_ref(), &hook.dev, &hook.service) { - Ok(()) => dev_dbg!(hook.dev, "reports armed again after sleep\n"), - Err(e) => dev_warn!(hook.dev, "reports not armed again after sleep: {:?}\n", e), + let aop = hook.aop.as_ref(); + match action as u32 { + bindings::PM_SUSPEND_PREPARE => { + // A sensor that keeps reporting only costs wakeups: never refuse + // the sleep over it. + hook.stop_attempted.store(true, Relaxed); + match set_als_property(aop, &hook.service, ALS_PROP_INTERVAL, &0u32.to_le_bytes()) { + Ok(_) => dev_dbg!(hook.dev, "reports off for sleep\n"), + Err(e) => dev_warn!(hook.dev, "reports may be left on for sleep: {:?}\n", e), + } + } + bindings::PM_POST_SUSPEND => { + let attempted = hook.stop_attempted.xchg(false, Relaxed); + // The CT817 is stopped by the firmware even when the host did not + // stop it, so it is armed again after every sleep. The other parts + // get their interval back after every attempted stop, whether or + // not the stop's reply arrived; writing the active interval to a + // sensor that kept reporting changes nothing. + let rearm = if hook.ct817 { + arm_ct817(aop, &hook.dev, &hook.service) + } else if attempted { + set_als_property( + aop, + &hook.service, + ALS_PROP_INTERVAL, + &ALS_INTERVAL_US.to_le_bytes(), + ) + .map(|_| ()) + } else { + Ok(()) + }; + match rearm { + Ok(()) => dev_dbg!(hook.dev, "reports back after sleep\n"), + Err(e) => dev_warn!(hook.dev, "reports not back after sleep: {:?}\n", e), + } + } + _ => return bindings::NOTIFY_DONE as c_int, } bindings::NOTIFY_OK as c_int } @@ -224,6 +267,7 @@ impl PmHook { dev: &device::Device, aop: Arc, service: EPICService, + ct817: bool, ) -> Result>> { let hook = KBox::pin( Self { @@ -235,6 +279,8 @@ impl PmHook { dev: dev.into(), aop, service, + ct817, + stop_attempted: Atomic::new(false), registered: Atomic::new(false), }, GFP_KERNEL, @@ -345,9 +391,9 @@ enum AlsIio { } struct IIOAopAlsDriver { - /// Fields are declared in teardown order: the wake hook (CT817 only) - /// first, then the subscription, which ends before the IIO device goes. - pm: Option>>, + /// Fields are declared in teardown order: the sleep hook first, then the + /// subscription, which ends before the IIO device goes. + pm: Pin>, listener: FakehidRegistration, _iio: AlsIio, aop: Arc, @@ -428,11 +474,7 @@ impl platform::Driver for IIOAopAlsDriver { }, ) }; - let pm = if offset == LUX_OFFSET_CT817 { - Some(PmHook::new(dev, adata.clone(), service)?) - } else { - None - }; + let pm = PmHook::new(dev, adata.clone(), service, offset == LUX_OFFSET_CT817)?; Ok(IIOAopAlsDriver { pm, listener, @@ -443,9 +485,7 @@ impl platform::Driver for IIOAopAlsDriver { } fn unbind(dev: &platform::Device>, this: Pin<&Self>) { - if let Some(pm) = &this.pm { - pm.unregister(); - } + this.pm.unregister(); // The sensor keeps reporting until its interval is zero, and every // report is an AOP mailbox interrupt. Stop it before the listener // goes; a failure only costs those interrupts, so unbind goes on.