1 // Copyright 2021 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //! `amp` crate provides `Amp` trait for amplifier initializations and `AmpBuilder`
5 //! to create `Amp` objects.
6 #![deny(missing_docs)]
7 
8 mod max98373d;
9 mod max98390d;
10 use std::path::PathBuf;
11 
12 use dsm::Error;
13 
14 use max98373d::Max98373;
15 use max98390d::Max98390;
16 
17 type Result<T> = std::result::Result<T, Error>;
18 const CONF_DIR: &str = "/etc/sound_card_init";
19 
20 /// It creates `Amp` object based on the sound card name.
21 pub struct AmpBuilder<'a> {
22     sound_card_id: &'a str,
23     config_path: PathBuf,
24 }
25 
26 impl<'a> AmpBuilder<'a> {
27     /// Creates an `AmpBuilder`.
28     /// # Arguments
29     ///
30     /// * `card_name` - card name.
31     /// * `conf_file` - config file name.
new(sound_card_id: &'a str, conf_file: &'a str) -> Self32     pub fn new(sound_card_id: &'a str, conf_file: &'a str) -> Self {
33         let config_path = PathBuf::from(CONF_DIR).join(conf_file);
34         AmpBuilder {
35             sound_card_id,
36             config_path,
37         }
38     }
39 
40     /// Creates an `Amp` based on the sound card name.
build(&self) -> Result<Box<dyn Amp>>41     pub fn build(&self) -> Result<Box<dyn Amp>> {
42         match self.sound_card_id {
43             "sofcmlmax98390d" => {
44                 Ok(Box::new(Max98390::new(self.sound_card_id, &self.config_path)?) as Box<dyn Amp>)
45             }
46             "sofrt5682" => {
47                 Ok(Box::new(Max98373::new(self.sound_card_id, &self.config_path)?) as Box<dyn Amp>)
48             }
49             _ => Err(Error::UnsupportedSoundCard(self.sound_card_id.to_owned())),
50         }
51     }
52 }
53 
54 /// It defines the required functions of amplifier objects.
55 pub trait Amp {
56     /// The amplifier boot time calibration flow.
boot_time_calibration(&mut self) -> Result<()>57     fn boot_time_calibration(&mut self) -> Result<()>;
58 }
59