Skip to content

Commit ee6a726

Browse files
baili0411Chromeos LUCI
authored and
Chromeos LUCI
committed
sound_card_init: Refactor for CalibData and VPD
CalibData and VPD previously worked on the assumption that all smart amps just requires an r0 and temp value, and the initialization was similarly hard coded. Adjusted CalibData to use the VPDTrait interface instead, which only requires the channel count to adapt to TAS2563 and other smart amps better. VPD is adjusted to implement the VPDTrait interface. BUG=b:346949244 TEST=tast run $DUT audio.SoundCardInit.* TEST=sound_card_init debug --id sofrt5682 --amp $(cros_config /audio/main speaker-amp) --conf $(cros_config /audio/main sound-card-init-conf) on delbin and redrix Change-Id: I73d6c3c904beea3c1b0ce6c68bf1dfa3e71bf343 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5743138 Tested-by: [email protected] <[email protected]> Commit-Queue: Baili Deng <[email protected]> Reviewed-by: Judy Hsiao <[email protected]>
1 parent c391dcf commit ee6a726

File tree

8 files changed

+75
-40
lines changed

8 files changed

+75
-40
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ compile_commands.json
55
.cache
66
# vscode
77
*.code-workspace
8+
*.vscode
89

910
# bazel
1011
/external

sound_card_init/amp/src/alc1011/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::path::Path;
1414

1515
use cros_alsa::Card;
1616
use cros_alsa::IntControl;
17+
use dsm::vpd::VPD;
1718
use dsm::CalibData;
1819
use dsm::Error;
1920
use dsm::RDCRange;
@@ -41,9 +42,7 @@ struct ALC1011CalibData {
4142
}
4243

4344
impl CalibData for ALC1011CalibData {
44-
fn new(rdc: i32, temp: f32) -> Self {
45-
Self { rdc, temp }
46-
}
45+
type VPDType = VPD;
4746

4847
fn rdc(&self) -> i32 {
4948
self.rdc
@@ -65,6 +64,15 @@ impl CalibData for ALC1011CalibData {
6564
}
6665
}
6766

67+
impl From<VPD> for ALC1011CalibData {
68+
fn from(vpd: VPD) -> Self {
69+
Self {
70+
rdc: vpd.dsm_calib_r0,
71+
temp: vpd.dsm_calib_temp as f32, // // ALC1011 VPD stores the temperature in celsius.
72+
}
73+
}
74+
}
75+
6876
impl ALC1011CalibData {
6977
/// Converts the ambient temperature from celsius to the DSM unit.
7078
#[inline]

sound_card_init/amp/src/cs35l41/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use cros_alsa::Ctl;
2323
use cros_alsa::ElemId;
2424
use cros_alsa::SimpleEnumControl;
2525
use cros_alsa::SwitchControl;
26+
use dsm::vpd::VPD;
2627
use dsm::CalibData;
2728
use dsm::RDCRange;
2829
use dsm::ZeroPlayer;
@@ -108,9 +109,7 @@ struct CS35L41CalibData {
108109
}
109110

110111
impl CalibData for CS35L41CalibData {
111-
fn new(rdc: i32, temp: f32) -> Self {
112-
Self { rdc, temp }
113-
}
112+
type VPDType = VPD;
114113

115114
fn rdc(&self) -> i32 {
116115
self.rdc
@@ -132,6 +131,15 @@ impl CalibData for CS35L41CalibData {
132131
}
133132
}
134133

134+
impl From<VPD> for CS35L41CalibData {
135+
fn from(vpd: VPD) -> Self {
136+
Self {
137+
rdc: vpd.dsm_calib_r0,
138+
temp: vpd.dsm_calib_temp as f32, // // CS35L41 VPD stores the temperature in celsius.
139+
}
140+
}
141+
}
142+
135143
impl fmt::Debug for CS35L41CalibData {
136144
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
137145
write!(f, "rdc: {} ohm", self.rdc_ohm(),)

sound_card_init/amp/src/max98373d/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use cros_alsa::Card;
2121
use cros_alsa::IntControl;
2222
use dsm::metrics::log_uma_enum;
2323
use dsm::metrics::UMACalibrationResult;
24+
use dsm::vpd::VPD;
2425
use dsm::CalibData;
2526
use dsm::RDCRange;
2627
use dsm::SpeakerStatus;
@@ -51,9 +52,7 @@ struct Max98373CalibData {
5152
}
5253

5354
impl CalibData for Max98373CalibData {
54-
fn new(rdc: i32, temp: f32) -> Self {
55-
Self { rdc, temp }
56-
}
55+
type VPDType = VPD;
5756

5857
fn rdc(&self) -> i32 {
5958
self.rdc
@@ -76,6 +75,15 @@ impl CalibData for Max98373CalibData {
7675
}
7776
}
7877

78+
impl From<VPD> for Max98373CalibData {
79+
fn from(vpd: VPD) -> Self {
80+
Self {
81+
rdc: vpd.dsm_calib_r0,
82+
temp: vpd.dsm_calib_temp as f32, // // 98373 VPD stores the temperature in celsius.
83+
}
84+
}
85+
}
86+
7987
impl Max98373CalibData {
8088
/// Converts the ambient temperature from celsius to the DsmSetAPI::DsmAmbientTemp unit.
8189
#[inline]

sound_card_init/amp/src/max98390d/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use cros_alsa::IntControl;
2020
use cros_alsa::SwitchControl;
2121
use dsm::metrics::log_uma_enum;
2222
use dsm::metrics::UMACalibrationResult;
23+
use dsm::vpd::VPD;
2324
use dsm::CalibData;
2425
use dsm::RDCRange;
2526
use dsm::SpeakerStatus;
@@ -61,9 +62,7 @@ struct Max98390CalibData {
6162
}
6263

6364
impl CalibData for Max98390CalibData {
64-
fn new(rdc: i32, temp: f32) -> Self {
65-
Self { rdc, temp }
66-
}
65+
type VPDType = VPD;
6766

6867
fn rdc(&self) -> i32 {
6968
self.rdc
@@ -86,6 +85,15 @@ impl CalibData for Max98390CalibData {
8685
}
8786
}
8887

88+
impl From<VPD> for Max98390CalibData {
89+
fn from(vpd: VPD) -> Self {
90+
Self {
91+
rdc: vpd.dsm_calib_r0,
92+
temp: Self::dsm_unit_to_celsius(vpd.dsm_calib_temp), // 98390 VPD stores the temperature in raw data.
93+
}
94+
}
95+
}
96+
8997
impl Max98390CalibData {
9098
/// Converts the ambient temperature from celsius to the DSM unit.
9199
#[inline]

sound_card_init/dsm/src/error.rs

-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use std::time;
1111
use remain::sorted;
1212
use thiserror::Error as ThisError;
1313

14-
use crate::CalibData;
15-
1614
pub type Result<T> = std::result::Result<T, Error>;
1715

1816
#[sorted]
@@ -36,8 +34,6 @@ pub enum Error {
3634
InvalidDatastore,
3735
#[error("invalid shutdown time")]
3836
InvalidShutDownTime,
39-
#[error("calibration difference is too large, calib: {0:?}")]
40-
LargeCalibrationDiff(Box<dyn CalibData>),
4137
#[error("mutex is poisoned")]
4238
MutexPoisonError,
4339
#[error("{0}")]

sound_card_init/dsm/src/lib.rs

+15-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod datastore;
77
mod error;
88
pub mod metrics;
99
pub mod utils;
10-
mod vpd;
10+
pub mod vpd;
1111
mod zero_player;
1212

1313
use std::fmt;
@@ -29,7 +29,6 @@ pub use crate::error::Result;
2929
use crate::metrics::*;
3030
use crate::utils::run_time;
3131
use crate::utils::shutdown_time;
32-
use crate::vpd::VPD;
3332
pub use crate::zero_player::ZeroPlayer;
3433

3534
#[derive(Debug, Default, PartialEq, Serialize, Deserialize, Clone, Copy)]
@@ -39,13 +38,8 @@ pub struct RDCRange {
3938
}
4039

4140
/// `CalibData` is the trait for the calibration data.
42-
pub trait CalibData: Send + fmt::Debug {
43-
/// Creates `CalibData`. rdc is the DC resistance in raw data.
44-
/// temp is the ambient temperature in celsius unit at which the
45-
/// rdc is measured.
46-
fn new(rdc: i32, temp: f32) -> Self
47-
where
48-
Self: Sized;
41+
pub trait CalibData: Send + fmt::Debug + From<Self::VPDType> {
42+
type VPDType: VPDTrait;
4943
/// The function to convert the rdc to ohm unit.
5044
fn rdc_to_ohm(rdc: i32) -> f32
5145
where
@@ -82,6 +76,16 @@ pub trait CalibData: Send + fmt::Debug {
8276
}
8377
}
8478

79+
// `VPDTrait` defines that any VPD can be created by giving the channel number.
80+
pub trait VPDTrait {
81+
fn new(channel: usize) -> Result<Self>
82+
where
83+
Self: Sized;
84+
fn new_from_datastore(datastore: Datastore, channel: usize) -> Result<Self>
85+
where
86+
Self: Sized;
87+
}
88+
8589
/// `TempConverter` converts the temperature value between celsius and unit in VPD::dsm_calib_temp.
8690
pub struct TempConverter {
8791
vpd_to_celsius: fn(i32) -> f32,
@@ -224,7 +228,6 @@ impl DSM {
224228
/// logic:
225229
/// * Returns the previous value if the ambient temperature is not within a valid range.
226230
/// If previous value does not exist, use VPD value instead.
227-
/// * Returns Error::LargeCalibrationDiff if rdc is not within the `rdc_range`.
228231
/// * Returns the previous value if the rdc difference is smaller than `CALI_ERROR_LOWER_LIMIT`.
229232
/// * Returns the boot time calibration value and updates the datastore value if the rdc
230233
/// difference is within the `rdc_range`and larger than the `CALI_ERROR_LOWER_LIMIT`.
@@ -398,20 +401,10 @@ impl DSM {
398401

399402
fn get_previous_calibration_value<T: CalibData>(&self, ch: usize) -> Result<T> {
400403
let sci_calib = Datastore::from_file(&self.snd_card, ch)?;
401-
match sci_calib {
402-
Datastore::UseVPD => self.get_vpd_calibration_value(ch),
403-
Datastore::DSM { rdc, temp } => Ok(CalibData::new(
404-
rdc,
405-
(self.temp_converter.vpd_to_celsius)(temp),
406-
)),
407-
}
404+
Ok(T::from(T::VPDType::new_from_datastore(sci_calib, ch)?))
408405
}
409406

410407
fn get_vpd_calibration_value<T: CalibData>(&self, channel: usize) -> Result<T> {
411-
let vpd = VPD::new(channel)?;
412-
Ok(CalibData::new(
413-
vpd.dsm_calib_r0,
414-
(self.temp_converter.vpd_to_celsius)(vpd.dsm_calib_temp),
415-
))
408+
Ok(T::from(T::VPDType::new(channel)?))
416409
}
417410
}

sound_card_init/dsm/src/vpd.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use std::path::PathBuf;
88

99
use crate::error::Error;
1010
use crate::error::Result;
11+
use crate::Datastore;
12+
use crate::VPDTrait;
1113

1214
const VPD_DIR: &str = "/sys/firmware/vpd/ro/vpdfile";
1315

@@ -18,17 +20,28 @@ pub struct VPD {
1820
pub dsm_calib_temp: i32,
1921
}
2022

21-
impl VPD {
23+
impl VPDTrait for VPD {
2224
/// Creates a `VPD` and initializes its fields from VPD_DIR/dsm_calib_r0_{channel}.
2325
/// # Arguments
2426
///
2527
/// * `channel` - channel number.
26-
pub fn new(channel: usize) -> Result<VPD> {
28+
fn new(channel: usize) -> Result<VPD> {
2729
let mut vpd: VPD = Default::default();
2830
vpd.dsm_calib_r0 = read_vpd_files(&format!("dsm_calib_r0_{}", channel))?;
2931
vpd.dsm_calib_temp = read_vpd_files(&format!("dsm_calib_temp_{}", channel))?;
3032
Ok(vpd)
3133
}
34+
fn new_from_datastore(datastore: Datastore, channel: usize) -> Result<VPD> {
35+
match datastore {
36+
Datastore::UseVPD => VPD::new(channel),
37+
Datastore::DSM { rdc, temp } => {
38+
let mut vpd: VPD = Default::default();
39+
vpd.dsm_calib_r0 = rdc;
40+
vpd.dsm_calib_temp = temp as i32;
41+
Ok(vpd)
42+
}
43+
}
44+
}
3245
}
3346

3447
fn read_vpd_files(file: &str) -> Result<i32> {

0 commit comments

Comments
 (0)