Skip to content

soc: apple: aop: Share sensor on-demand streaming helper and fix 1 Hz idle race - #179

Merged
henrikklee merged 1 commit into
OrchardLinux:mainfrom
grierj:apple-aop-stream-helper
Dec 5, 2026
Merged

henrikklee merged 1 commit into
OrchardLinux:mainfrom
grierj:apple-aop-stream-helper

Conversation

@grierj

@grierj grierj commented Dec 4, 2026

Copy link
Copy Markdown

Follow-up to #171 addressing both review notes:

  1. Shared streaming helper (struct apple_aop_stream): Move the duplicated interval/start/stop/idle-work/wait state machine from apple_aop_accel, apple_aop_gyro, apple_aop_compass, and apple_aop_pressure into drivers/soc/apple/aop.c and include/linux/soc/apple/aop.h (-318 net lines), keeping all four IIO drivers in step.
  2. Fix 1 Hz idle-stop race:
    • Track active waiters and cancel idle_work while apple_aop_stream_ensure_sample() is waiting for a sample, arming the idle stop only after the wait finishes (and preventing buffer_disable() from stopping the stream out from under an in-flight read).
    • Scale the idle stop delay to max(2000 ms, 2 * period + 1000 ms) (3 s at 1 Hz) and refresh it when the sampling frequency changes while streaming.

Built cleanly for apple-16k with zero warnings via orchard build --device j207 --kernel-tree.

Assisted-by: LLM

@grierj
grierj force-pushed the apple-aop-stream-helper branch from 6522c2e to 267fb2f Compare December 4, 2026 23:50
@grierj

grierj commented Dec 5, 2026

Copy link
Copy Markdown
Author

Updated (267fb2f79) to address review findings and live-verified on J207 (iPad Pro 10.5-inch, Wi-Fi):

  • Hardening added:
    • Synchronized IIO buffer delivery via push_lock / push_enabled (opened in postenable, closed and drained in predisable) instead of unsynchronized iio_buffer_enabled().
    • Authoritative idle_deadline under stream->lock so an already-queued idle worker cannot stop a freshly rearmed stream.
    • Paired apple_aop_stream_sample_get() / apple_aop_stream_sample_put() to keep waiters counted across the sample copy, plus wait-deadline refresh if a slower rate is programmed mid-wait.
    • Preserved firmware state on failed/partial start or stop property writes.
  • Live J207 verification:
    • apple-aop, apple_aop_accel, apple_aop_gyro, and apple_aop_pressure loaded cleanly with zero dmesg errors.
    • Mailbox IRQs (210804000.mbox-recv): ~11 Hz idle (accel via iio-sensor-proxy only) -> ~21 Hz during pressure read -> back to ~11 Hz after 4 s idle stop.
    • 1 Hz sysfs polling on apple-aop-pressure over 7 s: 7/7 live samples, zero -EAGAIN.
    • Concurrent buffer enable/disable + raw reads + sampling_frequency changes on apple-aop-gyro: completed cleanly with zero warnings or deadlocks.

@henrikklee

Copy link
Copy Markdown

Thanks @grierj, this looks good: one shared streaming helper instead of four copies, and the slow-polling race fixed. The locking checks out.

Before we merge, could you do a quick check on your J207? This rewrites four drivers, and the PR only mentions a clean build. Reading each of the four AOP sensors once (accelerometer, gyroscope, compass, barometer) plus one buffer start/stop on one of them would be enough, ideally with a read at 1 Hz. Please paste the output or the dmesg here. We'll merge as soon as you confirm.

One small non-blocking note: the commit message says the idle delay is "max(3 s, ...)", but the code (and the PR description) use max(2 s, 2 periods + 1 s). If you're touching the branch anyway, feel free to fix the message; if not, that's fine.

… idle race

All four AOP IIO sensor drivers (accel, gyro, compass, pressure) carried
identical copies of the on-demand streaming state machine: start/stop
property sequencing, idle delayed_work, waiter tracking, sample-ready
synchronization, and interval updates.

Extract this state machine into a shared struct apple_aop_stream helper
in drivers/soc/apple/aop.c and include/linux/soc/apple/aop.h, and switch
the four IIO drivers to use it.

While consolidating, fix four edge cases in the streaming helper:
- Scale the idle stop delay to at least two sample periods plus one full
  second (max(2 s, 2 * interval + 1 s)) and track an authoritative
  idle_deadline under stream->lock so a 1 Hz sysfs poller cannot race
  with an idle stop and an already-queued idle worker cannot stop a
  freshly rearmed stream.
- Keep waiters counted across the sample copy via sample_get/sample_put
  so a concurrent buffer disable cannot clear have_sample before read_raw
  copies the sample, and refresh the wait deadline if a slower rate is
  programmed while waiting.
- Synchronize IIO buffer delivery with a dedicated push_lock/push_enabled
  gate opened in postenable and closed/drained in predisable, instead of
  relying on unsynchronized iio_buffer_enabled() during reconfiguration.
- Preserve confirmed firmware state if a start/rate-change or stop
  property write fails.

Assisted-by: LLM
Signed-off-by: Grier Johnson <grier@scooterfarm.com>
@henrikklee
henrikklee force-pushed the apple-aop-stream-helper branch from 267fb2f to 7d8c080 Compare December 5, 2026 01:25
@henrikklee
henrikklee merged commit b807e19 into OrchardLinux:main Dec 5, 2026
4 checks passed
@henrikklee

Copy link
Copy Markdown

Merged, thank you Grier! One shared streaming helper instead of four copies, and the 1 Hz race is gone: really nice cleanup.

What we changed on the branch: only the commit message, where the idle delay now reads max(2 s, 2 * interval + 1 s), matching the code and your PR description. The code is exactly yours.

It reaches the nightly once orchard takes the new kernel. A quick report from your J207 after that (each sensor read once, a buffer start and stop) would be very welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants