Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions Documentation/iio/aop-sensors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ Both devices have a buffer that takes X, Y and Z together, with an optional
timestamp taken when the report arrives. The reports stop when the system
goes to sleep and start again, with the calibration resent, when it wakes.

If both devices bind but every raw read returns ``ENODATA``, check the AOP
core's ``nCal`` boot argument for the SoC. The T6040 firmware accepts the
calibration and the report interval with ``nCal`` 0, but sends no reports;
its own default, which the core keeps, is 1.


Host regression tests require Python 3 and a Rust compiler::

Expand Down
73 changes: 59 additions & 14 deletions drivers/iio/common/aop_sensors/aop_motion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ const CALIBRATION: [(u32, &CStr); 3] = [
/// at byte 6. The other bytes carry no sample.
const REPORT_LEN: usize = 22;
const XYZ_OFFSET: usize = 6;
/// The status a calibration write is answered with when the service already
/// holds a calibration from an earlier start in the same AOP boot, as after a
/// rebind or a resume. The firmware keeps that calibration, so the reports can
/// be started without it.
const STATUS_UNSUPPORTED: u32 = 0xe000_02c7;

/// What the status of a calibration write means for the start of the reports.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CalStatus {
/// The firmware took the packet.
Taken,
/// The firmware holds a calibration from an earlier start.
Held,
/// Anything else: the reports are not started.
Refused,
}

fn cal_status(status: u32) -> CalStatus {
match status {
0 => CalStatus::Taken,
STATUS_UNSUPPORTED => CalStatus::Held,
_ => CalStatus::Refused,
}
}

/// What one of the two services measures.
struct Kind {
Expand Down Expand Up @@ -146,30 +170,51 @@ impl Control {
)?)
}

fn set(&self, prop: u32, value: &[u8]) -> Result {
/// Writes a property and returns the firmware's status.
fn write(&self, prop: u32, value: &[u8]) -> Result<u32> {
let mut msg = KVec::with_capacity(8 + value.len(), GFP_KERNEL)?;
msg.extend_from_slice(&0u32.to_le_bytes(), GFP_KERNEL)?;
msg.extend_from_slice(&prop.to_le_bytes(), GFP_KERNEL)?;
msg.extend_from_slice(value, GFP_KERNEL)?;
let status = self
.aop
.epic_call(&self.service, SUBTYPE_SET_PROPERTY, &msg)?;
if status != 0 {
dev_err!(
self.dev,
"property {} refused: status {:#x}\n",
prop,
status
);
return Err(EIO);
self.aop
.epic_call(&self.service, SUBTYPE_SET_PROPERTY, &msg)
}

fn refused(&self, prop: u32, status: u32) -> Error {
dev_err!(
self.dev,
"property {} refused: status {:#x}\n",
prop,
status
);
EIO
}

fn set(&self, prop: u32, value: &[u8]) -> Result {
match self.write(prop, value)? {
0 => Ok(()),
status => Err(self.refused(prop, status)),
}
Ok(())
}

/// Sends the calibration, then the interval that starts the reports.
fn start(&self) -> Result {
let mut held = 0;
for ((prop, _), packet) in CALIBRATION.iter().zip(self.packets.iter()) {
self.set(*prop, packet)?;
let status = self.write(*prop, packet)?;
match cal_status(status) {
CalStatus::Taken => {}
CalStatus::Held => held += 1,
CalStatus::Refused => return Err(self.refused(*prop, status)),
}
}
if held > 0 {
dev_info!(
self.dev,
"{} of {} calibration packets kept from an earlier start\n",
held,
CALIBRATION.len()
);
}
self.set(PROP_INTERVAL, &INTERVAL_US.to_le_bytes())
}
Expand Down
19 changes: 14 additions & 5 deletions drivers/soc/apple/aop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2511,6 +2511,8 @@ struct AopDriver(Arc<dyn AOP>);

struct AopHwConfig {
ec0p: u64,
/// The `nCal` boot argument.
ncal: u64,
alig: u64,
aopt: u64,
/// Complete the firmware's boot arguments before starting it.
Expand All @@ -2534,6 +2536,7 @@ struct AopHwConfig {

const HW_CFG_T8103: AopHwConfig = AopHwConfig {
ec0p: 0x020000,
ncal: 0,
aopt: 1,
alig: 128,
patch_bootargs: true,
Expand All @@ -2546,6 +2549,7 @@ const HW_CFG_T8103: AopHwConfig = AopHwConfig {
};
const HW_CFG_T8112: AopHwConfig = AopHwConfig {
ec0p: 0x020000,
ncal: 0,
aopt: 0,
alig: 128,
patch_bootargs: true,
Expand All @@ -2558,6 +2562,7 @@ const HW_CFG_T8112: AopHwConfig = AopHwConfig {
};
const HW_CFG_T6000: AopHwConfig = AopHwConfig {
ec0p: 0x020000,
ncal: 0,
aopt: 0,
alig: 64,
patch_bootargs: true,
Expand All @@ -2570,6 +2575,7 @@ const HW_CFG_T6000: AopHwConfig = AopHwConfig {
};
const HW_CFG_T6020: AopHwConfig = AopHwConfig {
ec0p: 0x0100_00000000,
ncal: 0,
aopt: 0,
alig: 64,
patch_bootargs: true,
Expand All @@ -2580,12 +2586,14 @@ const HW_CFG_T6020: AopHwConfig = AopHwConfig {
fw_regions: false,
halt_on_failed_start: false,
};
/// T6040 (J616s): macOS writes the same four boot arguments as for the
/// T6020. The firmware's external code and log live in DRAM, mapped through
/// the reserved regions the bootloader describes; the RTKit firmware is shut
/// down on unbind.
/// T6040 (J616s): the firmware's external code and log live in DRAM, mapped
/// through the reserved regions the bootloader describes; the RTKit firmware
/// is shut down on unbind. `nCal` keeps the firmware's own default of 1:
/// with 0 the accelerometer and gyroscope services accept their calibration
/// and report interval but never send a report.
const HW_CFG_T6040: AopHwConfig = AopHwConfig {
ec0p: 0x0100_00000000,
ncal: 1,
aopt: 0,
alig: 64,
patch_bootargs: true,
Expand All @@ -2603,6 +2611,7 @@ const HW_CFG_T6040: AopHwConfig = AopHwConfig {
/// are started if they will.
const HW_CFG_T8140: AopHwConfig = AopHwConfig {
ec0p: 0,
ncal: 0,
aopt: 0,
alig: 0,
patch_bootargs: false,
Expand Down Expand Up @@ -2666,7 +2675,7 @@ impl platform::Driver for AopDriver {
&aop_mmio,
&[
(from_fourcc(b"EC0p"), cfg.ec0p),
(from_fourcc(b"nCal"), 0x0),
(from_fourcc(b"nCal"), cfg.ncal),
(from_fourcc(b"alig"), cfg.alig),
(from_fourcc(b"AOPt"), cfg.aopt),
],
Expand Down
13 changes: 12 additions & 1 deletion tools/testing/selftests/drivers/apple/aop-motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def main():
motion = (root / "drivers/iio/common/aop_sensors/aop_motion.rs").read_text()
parts = [const(motion, "REPORT_LEN"), const(motion, "XYZ_OFFSET"),
block(motion, "fn decode("), block(motion, "fn le_u32("),
block(motion, "fn location0_packet("), r'''
block(motion, "fn location0_packet("),
const(motion, "STATUS_UNSUPPORTED"),
block(motion, "#[derive(Clone, Copy, Debug, PartialEq, Eq)]\nenum CalStatus"), block(motion, "fn cal_status("), r'''
fn report(prefix: u8, xyz: [i32; 3], suffix: u8) -> Vec<u8> {
let mut r = vec![prefix; 6];
for v in xyz {
Expand Down Expand Up @@ -69,6 +71,15 @@ def main():
c
}
#[test]
fn calibration_status() {
assert_eq!(cal_status(0), CalStatus::Taken);
// Answered once the service holds a calibration from an earlier start.
assert_eq!(cal_status(0xe000_02c7), CalStatus::Held);
for status in [1, 0xe000_02bc, 0xe000_02c2, 0xe000_02e2, 0xe000_02c6, u32::MAX] {
assert_eq!(cal_status(status), CalStatus::Refused, "{status:#x}");
}
}
#[test]
fn selects_location_zero() {
let a = [1u8; 44];
let b = [2u8; 40];
Expand Down