|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! Driver for the IMX3112 I2C mux |
| 6 | +
|
| 7 | +use crate::*; |
| 8 | +use drv_i2c_api::{ResponseCode, Segment}; |
| 9 | + |
| 10 | +use bitfield::bitfield; |
| 11 | + |
| 12 | +#[allow(dead_code)] |
| 13 | +#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 14 | +pub enum Register { |
| 15 | + DeviceTypeLo = 0x0, |
| 16 | + DeviceTypeHi = 0x1, |
| 17 | + DeviceRevision = 0x2, |
| 18 | + VendorIdLo = 0x3, |
| 19 | + VendorIdHi = 0x4, |
| 20 | + LocalInterfaceCfg = 0xe, |
| 21 | + PullupResistorConfig = 0xf, |
| 22 | + DeviceCfg = 0x12, |
| 23 | + ClearTempSensorAlarm = 0x13, |
| 24 | + ClearEccError = 0x14, |
| 25 | + TempSensorCfg = 0x1a, |
| 26 | + InterruptCfg = 0x1b, |
| 27 | + TempHiLimitCfgLo = 0x1c, |
| 28 | + TempHiLimitCfgHi = 0x1d, |
| 29 | + TempLoLimitCfgLo = 0x1e, |
| 30 | + TempLoLimitCfgHi = 0x1f, |
| 31 | + TempCritHiLimitCfgLo = 0x20, |
| 32 | + TempCritHiLimitCfgHi = 0x21, |
| 33 | + TempCritLoLimitCfgLo = 0x22, |
| 34 | + TempCritLoLimitCfgHi = 0x23, |
| 35 | + DeviceStatus = 0x30, |
| 36 | + CurrentTemperatureLo = 0x31, |
| 37 | + CurrentTemperatureHi = 0x32, |
| 38 | + TemperatureStatus = 0x33, |
| 39 | + ErrorStatus = 0x34, |
| 40 | + MuxConfig = 0x40, |
| 41 | + MuxSelect = 0x41, |
| 42 | +} |
| 43 | + |
| 44 | +bitfield! { |
| 45 | + #[derive(Copy, Clone, Eq, PartialEq)] |
| 46 | + pub struct MuxSelectRegister(u8); |
| 47 | + channel1_enabled, set_channel1_enabled: 6; |
| 48 | + channel0_enabled, set_channel0_enabled: 7; |
| 49 | +} |
| 50 | + |
| 51 | +pub struct Imx3112; |
| 52 | + |
| 53 | +fn write_reg_u8( |
| 54 | + mux: &I2cMux<'_>, |
| 55 | + controller: &I2cController<'_>, |
| 56 | + reg: Register, |
| 57 | + val: u8, |
| 58 | + ctrl: &I2cControl, |
| 59 | +) -> Result<(), ResponseCode> { |
| 60 | + controller |
| 61 | + .write_read( |
| 62 | + mux.address, |
| 63 | + 2, |
| 64 | + |pos| Some(if pos == 0 { reg as u8 } else { val }), |
| 65 | + ReadLength::Fixed(0), |
| 66 | + |_, _| Some(()), |
| 67 | + ctrl, |
| 68 | + ) |
| 69 | + .map_err(|e| mux.error_code(e)) |
| 70 | +} |
| 71 | + |
| 72 | +impl I2cMuxDriver for Imx3112 { |
| 73 | + fn configure( |
| 74 | + &self, |
| 75 | + mux: &I2cMux<'_>, |
| 76 | + _controller: &I2cController<'_>, |
| 77 | + gpio: &sys_api::Sys, |
| 78 | + _ctrl: &I2cControl, |
| 79 | + ) -> Result<(), drv_i2c_api::ResponseCode> { |
| 80 | + // TODO configure registers? |
| 81 | + mux.configure(gpio) |
| 82 | + } |
| 83 | + |
| 84 | + fn enable_segment( |
| 85 | + &self, |
| 86 | + mux: &I2cMux<'_>, |
| 87 | + controller: &I2cController<'_>, |
| 88 | + segment: Option<Segment>, |
| 89 | + ctrl: &I2cControl, |
| 90 | + ) -> Result<(), ResponseCode> { |
| 91 | + let mut reg = MuxSelectRegister(0); |
| 92 | + match segment { |
| 93 | + Some(Segment::S1) => reg.set_channel0_enabled(true), |
| 94 | + Some(Segment::S2) => reg.set_channel1_enabled(true), |
| 95 | + None => (), |
| 96 | + _ => return Err(ResponseCode::SegmentNotFound), |
| 97 | + } |
| 98 | + // Disable both outputs |
| 99 | + write_reg_u8(mux, controller, Register::MuxConfig, 0, ctrl)?; |
| 100 | + // Select our desired output |
| 101 | + write_reg_u8(mux, controller, Register::MuxSelect, reg.0, ctrl)?; |
| 102 | + // Enable our desired output |
| 103 | + write_reg_u8(mux, controller, Register::MuxConfig, reg.0, ctrl)?; |
| 104 | + Ok(()) |
| 105 | + } |
| 106 | + |
| 107 | + fn reset( |
| 108 | + &self, |
| 109 | + mux: &I2cMux<'_>, |
| 110 | + gpio: &sys_api::Sys, |
| 111 | + ) -> Result<(), drv_i2c_api::ResponseCode> { |
| 112 | + mux.reset(gpio) |
| 113 | + } |
| 114 | +} |
0 commit comments