Skip to content

Commit fce34c8

Browse files
committed
Merge branch 'bits/250-aop' into asahi-wip
2 parents 0f8915a + 407530e commit fce34c8

File tree

28 files changed

+2602
-5
lines changed

28 files changed

+2602
-5
lines changed

drivers/iio/common/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# IIO common modules
44
#
55

6+
source "drivers/iio/common/aop_sensors/Kconfig"
67
source "drivers/iio/common/cros_ec_sensors/Kconfig"
78
source "drivers/iio/common/hid-sensors/Kconfig"
89
source "drivers/iio/common/inv_sensors/Kconfig"

drivers/iio/common/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#
99

1010
# When adding new entries keep the list in alphabetical order
11+
obj-y += aop_sensors/
1112
obj-y += cros_ec_sensors/
1213
obj-y += hid-sensors/
1314
obj-y += inv_sensors/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SPDX-License-Identifier: GPL-2.0-only OR MIT
2+
3+
config IIO_AOP_SENSOR_LAS
4+
tristate "AOP Lid angle sensor"
5+
depends on ARCH_APPLE || COMPILE_TEST
6+
depends on RUST
7+
depends on SYSFS
8+
select APPLE_AOP
9+
default m if ARCH_APPLE
10+
help
11+
Module to handle the lid angle sensor attached to the AOP
12+
coprocessor on Apple laptops.
13+
14+
config IIO_AOP_SENSOR_ALS
15+
tristate "AOP Ambient light sensor"
16+
depends on ARCH_APPLE || COMPILE_TEST
17+
depends on RUST
18+
depends on SYSFS
19+
select APPLE_AOP
20+
default m if ARCH_APPLE
21+
help
22+
Module to handle the ambient light sensor attached to the AOP
23+
coprocessor on Apple laptops.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-License-Identifier: GPL-2.0-only OR MIT
2+
3+
obj-$(CONFIG_IIO_AOP_SENSOR_LAS) += aop_las.o
4+
obj-$(CONFIG_IIO_AOP_SENSOR_ALS) += aop_als.o
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// SPDX-License-Identifier: GPL-2.0-only OR MIT
2+
3+
//! Apple AOP ambient light sensor driver
4+
//!
5+
//! Copyright (C) The Asahi Linux Contributors
6+
7+
use kernel::{
8+
bindings, c_str, device,
9+
iio::common::aop_sensors::{AopSensorData, IIORegistration, MessageProcessor},
10+
module_platform_driver,
11+
of::{self, Node},
12+
platform,
13+
prelude::*,
14+
soc::apple::aop::{EPICService, AOP},
15+
sync::Arc,
16+
types::{ARef, ForeignOwnable},
17+
};
18+
19+
const EPIC_SUBTYPE_SET_ALS_PROPERTY: u16 = 0x4;
20+
21+
fn enable_als(
22+
aop: &dyn AOP,
23+
dev: &ARef<device::Device>,
24+
of: &Node,
25+
svc: &EPICService,
26+
) -> Result<()> {
27+
if let Some(prop) = of.find_property(c_str!("apple,als-calibration")) {
28+
set_als_property(aop, svc, 0xb, prop.value())?;
29+
set_als_property(aop, svc, 0, &200000u32.to_le_bytes())?;
30+
} else {
31+
dev_warn!(dev, "ALS Calibration not found, will not enable it");
32+
}
33+
Ok(())
34+
}
35+
fn set_als_property(aop: &dyn AOP, svc: &EPICService, tag: u32, data: &[u8]) -> Result<u32> {
36+
let mut buf = KVec::new();
37+
buf.resize(data.len() + 8, 0, GFP_KERNEL)?;
38+
buf[8..].copy_from_slice(data);
39+
buf[4..8].copy_from_slice(&tag.to_le_bytes());
40+
aop.epic_call(svc, EPIC_SUBTYPE_SET_ALS_PROPERTY, &buf)
41+
}
42+
43+
fn f32_to_u32(f: u32) -> u32 {
44+
if f & 0x80000000 != 0 {
45+
return 0;
46+
}
47+
let exp = ((f & 0x7f800000) >> 23) as i32 - 127;
48+
if exp < 0 {
49+
return 0;
50+
}
51+
if exp == 128 && f & 0x7fffff != 0 {
52+
return 0;
53+
}
54+
let mant = f & 0x7fffff | 0x800000;
55+
if exp <= 23 {
56+
return mant >> (23 - exp);
57+
}
58+
if exp >= 32 {
59+
return u32::MAX;
60+
}
61+
mant << (exp - 23)
62+
}
63+
64+
struct MsgProc(usize);
65+
66+
impl MessageProcessor for MsgProc {
67+
fn process(&self, message: &[u8]) -> u32 {
68+
let offset = self.0;
69+
let raw = u32::from_le_bytes(message[offset..offset + 4].try_into().unwrap());
70+
f32_to_u32(raw)
71+
}
72+
}
73+
74+
#[repr(transparent)]
75+
struct IIOAopAlsDriver(IIORegistration<MsgProc>);
76+
77+
kernel::of_device_table!(OF_TABLE, MODULE_OF_TABLE, (), [] as [(of::DeviceId, ()); 0]);
78+
79+
impl platform::Driver for IIOAopAlsDriver {
80+
type IdInfo = ();
81+
82+
const ID_TABLE: platform::IdTable<()> = &OF_TABLE;
83+
84+
fn probe(
85+
pdev: &mut platform::Device,
86+
_info: Option<&()>,
87+
) -> Result<Pin<KBox<IIOAopAlsDriver>>> {
88+
let dev = pdev.get_device();
89+
let parent = dev.parent().unwrap();
90+
// SAFETY: our parent is AOP, and AopDriver is repr(transparent) for Arc<dyn Aop>
91+
let adata_ptr = unsafe { Pin::<KBox<Arc<dyn AOP>>>::borrow(parent.get_drvdata()) };
92+
let adata = (&*adata_ptr).clone();
93+
// SAFETY: AOP sets the platform data correctly
94+
let service = unsafe { *((*dev.as_raw()).platform_data as *const EPICService) };
95+
let of = parent
96+
.of_node()
97+
.ok_or(EIO)?
98+
.get_child_by_name(c_str!("als"))
99+
.ok_or(EIO)?;
100+
let ty = bindings::BINDINGS_IIO_LIGHT;
101+
let data = AopSensorData::new(dev.clone(), ty, MsgProc(40))?;
102+
adata.add_fakehid_listener(service, data.clone())?;
103+
enable_als(adata.as_ref(), &dev, &of, &service)?;
104+
let info_mask = 1 << bindings::BINDINGS_IIO_CHAN_INFO_PROCESSED;
105+
Ok(KBox::pin(
106+
IIOAopAlsDriver(IIORegistration::<MsgProc>::new(
107+
data,
108+
c_str!("aop-sensors-als"),
109+
ty,
110+
info_mask,
111+
&THIS_MODULE,
112+
)?),
113+
GFP_KERNEL,
114+
)?)
115+
}
116+
}
117+
118+
module_platform_driver! {
119+
type: IIOAopAlsDriver,
120+
name: "iio_aop_als",
121+
license: "Dual MIT/GPL",
122+
alias: ["platform:iio_aop_als"],
123+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// SPDX-License-Identifier: GPL-2.0-only OR MIT
2+
3+
//! Apple AOP lid angle sensor driver
4+
//!
5+
//! Copyright (C) The Asahi Linux Contributors
6+
7+
use kernel::{
8+
bindings, c_str,
9+
iio::common::aop_sensors::{AopSensorData, IIORegistration, MessageProcessor},
10+
module_platform_driver, of, platform,
11+
prelude::*,
12+
soc::apple::aop::{EPICService, AOP},
13+
sync::Arc,
14+
types::ForeignOwnable,
15+
};
16+
17+
struct MsgProc;
18+
19+
impl MessageProcessor for MsgProc {
20+
fn process(&self, message: &[u8]) -> u32 {
21+
message[1] as u32
22+
}
23+
}
24+
25+
#[repr(transparent)]
26+
struct IIOAopLasDriver(IIORegistration<MsgProc>);
27+
28+
kernel::of_device_table!(OF_TABLE, MODULE_OF_TABLE, (), [] as [(of::DeviceId, ()); 0]);
29+
30+
impl platform::Driver for IIOAopLasDriver {
31+
type IdInfo = ();
32+
33+
const ID_TABLE: platform::IdTable<()> = &OF_TABLE;
34+
35+
fn probe(
36+
pdev: &mut platform::Device,
37+
_info: Option<&()>,
38+
) -> Result<Pin<KBox<IIOAopLasDriver>>> {
39+
let dev = pdev.get_device();
40+
let parent = dev.parent().unwrap();
41+
// SAFETY: our parent is AOP, and AopDriver is repr(transparent) for Arc<dyn Aop>
42+
let adata_ptr = unsafe { Pin::<KBox<Arc<dyn AOP>>>::borrow(parent.get_drvdata()) };
43+
let adata = (&*adata_ptr).clone();
44+
// SAFETY: AOP sets the platform data correctly
45+
let service = unsafe { *((*dev.as_raw()).platform_data as *const EPICService) };
46+
47+
let ty = bindings::BINDINGS_IIO_ANGL;
48+
let data = AopSensorData::new(dev, ty, MsgProc)?;
49+
adata.add_fakehid_listener(service, data.clone())?;
50+
let info_mask = 1 << bindings::BINDINGS_IIO_CHAN_INFO_RAW;
51+
Ok(KBox::pin(
52+
IIOAopLasDriver(IIORegistration::<MsgProc>::new(
53+
data,
54+
c_str!("aop-sensors-las"),
55+
ty,
56+
info_mask,
57+
&THIS_MODULE,
58+
)?),
59+
GFP_KERNEL,
60+
)?)
61+
}
62+
}
63+
64+
module_platform_driver! {
65+
type: IIOAopLasDriver,
66+
name: "iio_aop_las",
67+
license: "Dual MIT/GPL",
68+
alias: ["platform:iio_aop_las"],
69+
}

drivers/soc/apple/Kconfig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,30 @@ config RUST_APPLE_RTKIT
7979
depends on RUST
8080
depends on APPLE_RTKIT
8181

82+
config APPLE_AOP
83+
tristate "Apple \"Always-on\" Processor"
84+
depends on ARCH_APPLE || COMPILE_TEST
85+
depends on RUST
86+
select RUST_APPLE_RTKIT
87+
default m if ARCH_APPLE
88+
help
89+
A co-processor persent on certain Apple SoCs controlling accelerometers,
90+
gyros, ambient light sensors and microphones. Is not actually always on.
91+
92+
Say 'y' here if you have an Apple laptop.
93+
94+
config APPLE_SEP
95+
tristate "Apple Secure Element Processor"
96+
depends on ARCH_APPLE || COMPILE_TEST
97+
depends on RUST
98+
select RUST_APPLE_RTKIT
99+
default y if ARCH_APPLE
100+
help
101+
A security co-processor persent on Apple SoCs, controlling transparent
102+
disk encryption, secure boot, HDCP, biometric auth and probably more.
103+
104+
Say 'y' here if you have an Apple SoC.
105+
82106
endmenu
83107

84108
endif

drivers/soc/apple/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ apple-rtkit-helper-y = rtkit-helper.o
1616

1717
obj-$(CONFIG_APPLE_SART) += apple-sart.o
1818
apple-sart-y = sart.o
19+
20+
obj-$(CONFIG_APPLE_AOP) += aop.o
21+
22+
obj-$(CONFIG_APPLE_SEP) += sep.o

0 commit comments

Comments
 (0)