From 7f1220a150c47dcf2998553601c30a9b1da35fa7 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 28 Nov 2026 00:17:56 +1100 Subject: [PATCH] iio: aop_als: stop the reports at unbind, arm the CT817 from zero The light sensor reports on its own every 200 ms, and every report is an AOP mailbox interrupt. unbind() only dropped the listener, so the sensor went on reporting to nobody: on the J700 (T8140, CT817) the AOP mailbox kept taking 5 interrupts a second after the driver was unbound, the same rate as with the driver bound. Write the interval to zero at unbind, before the listener goes, as the motion driver already stops its sensor at unbind. A failure is logged and unbind goes on; the AOP transport refuses calls once it is closing. The CT817 arms only on a zero to non-zero transition of its interval. Probe assumed the interval was zero, which holds at a cold boot but not after a previous instance of the driver (or a kexec) left the sensor running. Make the zero write the first step of arm_ct817(), so probe and the wake path share one sequence; the wake path already wrote zero first, so its behaviour does not change. This takes over the unbind part of #2713 by Kyle Arrington, who handed the branch over. Its suspend and resume part is left out: #2528 now arms the CT817 again after a sleep, and whether reports still wake the CPU in suspend-to-idle needs its own measurement. Link: https://github.com/aurora-silicon/linux/pull/2713 Signed-off-by: Ryan Murray --- drivers/iio/common/aop_sensors/aop_als.rs | 38 ++++++++++++++++------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/drivers/iio/common/aop_sensors/aop_als.rs b/drivers/iio/common/aop_sensors/aop_als.rs index cefd01640bc96..401f4f2bb32de 100644 --- a/drivers/iio/common/aop_sensors/aop_als.rs +++ b/drivers/iio/common/aop_sensors/aop_als.rs @@ -110,7 +110,8 @@ fn enable_als( arm_ct817(aop, dev.as_ref(), svc) } -/// Arms the CT817: its configuration, then the interval that starts the reports. +/// Arms the CT817: interval zero, its configuration, then the interval that +/// starts the reports. fn arm_ct817(aop: &dyn AOP, dev: &device::Device, svc: &EPICService) -> Result<()> { // The CT817 gets its calibration from the AOP driver over the setup port, // not as an ALS property. Only the transport can fail here; the sensor's @@ -123,6 +124,10 @@ fn arm_ct817(aop: &dyn AOP, dev: &device::Device, svc: &EPICService) -> Result<( dev_dbg!(dev, "{} = {:#x}: status {:#x}\n", what, val, status); Ok(()) }; + // The sensor arms only on a zero to non-zero transition of the interval. + // It is zero at boot and stopped after a sleep, but a previous driver + // instance may have left it running, so write zero first every time. + set(ALS_PROP_INTERVAL, 0, "interval")?; set(ALS_PROP_VERBOSITY, ALS_VERBOSITY, "verbosity")?; set( ALS_PROP_BATCH_INTERVAL, @@ -130,8 +135,7 @@ fn arm_ct817(aop: &dyn AOP, dev: &device::Device, svc: &EPICService) -> Result<( "batch interval", )?; set(ALS_PROP_RAW_INTERVAL, ALS_RAW_INTERVAL, "raw interval")?; - // The interval is zero at probe, and the wake writes it to zero first, so - // this is the transition that arms the sensor. + // The transition that arms the sensor. set(ALS_PROP_INTERVAL, ALS_INTERVAL_US, "interval") } @@ -141,8 +145,8 @@ fn arm_ct817(aop: &dyn AOP, dev: &device::Device, svc: &EPICService) -> Result<( /// 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 writes the interval to zero and then repeats -/// the arming of the probe. +/// of its interval, so the wake repeats the arming of the probe, which starts +/// with an interval of zero. #[repr(C)] struct PmHook { /// First, so that the callback finds the hook from it. @@ -168,10 +172,7 @@ unsafe extern "C" fn pm_event( } // SAFETY: `nb` is the first field of a registered `PmHook`. let hook = unsafe { &*nb.cast::() }; - let aop = hook.aop.as_ref(); - let rearm = set_als_property(aop, &hook.service, ALS_PROP_INTERVAL, &0u32.to_le_bytes()) - .and_then(|_| arm_ct817(aop, &hook.dev, &hook.service)); - match rearm { + 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), } @@ -349,6 +350,8 @@ struct IIOAopAlsDriver { pm: Option>>, listener: FakehidRegistration, _iio: AlsIio, + aop: Arc, + service: EPICService, } fn register_channels( @@ -426,7 +429,7 @@ impl platform::Driver for IIOAopAlsDriver { ) }; let pm = if offset == LUX_OFFSET_CT817 { - Some(PmHook::new(dev, adata, service)?) + Some(PmHook::new(dev, adata.clone(), service)?) } else { None }; @@ -434,13 +437,26 @@ impl platform::Driver for IIOAopAlsDriver { pm, listener, _iio: iio, + aop: adata, + service, }) } - fn unbind(_dev: &platform::Device>, this: Pin<&Self>) { + fn unbind(dev: &platform::Device>, this: Pin<&Self>) { if let Some(pm) = &this.pm { 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. + if let Err(e) = set_als_property( + this.aop.as_ref(), + &this.service, + ALS_PROP_INTERVAL, + &0u32.to_le_bytes(), + ) { + dev_warn!(dev.as_ref(), "reports not stopped at unbind: {:?}\n", e); + } this.listener.unregister(); } }