Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 64 additions & 24 deletions drivers/iio/common/aop_sensors/aop_als.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,33 @@ 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.
nb: Opaque<bindings::notifier_block>,
dev: ARef<device::Device>,
aop: Arc<dyn AOP>,
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<bool>,
registered: Atomic<bool>,
}

Expand All @@ -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::<PmHook>() };
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
}
Expand Down Expand Up @@ -224,6 +267,7 @@ impl PmHook {
dev: &device::Device,
aop: Arc<dyn AOP>,
service: EPICService,
ct817: bool,
) -> Result<Pin<KBox<Self>>> {
let hook = KBox::pin(
Self {
Expand All @@ -235,6 +279,8 @@ impl PmHook {
dev: dev.into(),
aop,
service,
ct817,
stop_attempted: Atomic::new(false),
registered: Atomic::new(false),
},
GFP_KERNEL,
Expand Down Expand Up @@ -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<Pin<KBox<PmHook>>>,
/// Fields are declared in teardown order: the sleep hook first, then the
/// subscription, which ends before the IIO device goes.
pm: Pin<KBox<PmHook>>,
listener: FakehidRegistration,
_iio: AlsIio,
aop: Arc<dyn AOP>,
Expand Down Expand Up @@ -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,
Expand All @@ -443,9 +485,7 @@ impl platform::Driver for IIOAopAlsDriver {
}

fn unbind(dev: &platform::Device<Core<'_>>, 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.
Expand Down
Loading