-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathadm1272.rs
302 lines (261 loc) · 8.71 KB
/
adm1272.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Driver for the ADM1272 hot-swap controller
use core::cell::Cell;
use crate::{
pmbus_validate, BadValidation, CurrentSensor, TempSensor, Validate,
VoltageSensor,
};
use drv_i2c_api::{I2cDevice, ResponseCode};
use num_traits::float::FloatCore;
use pmbus::commands::*;
use ringbuf::*;
use userlib::units::*;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Error {
BadRead { cmd: u8, code: ResponseCode },
BadWrite { cmd: u8, code: ResponseCode },
BadData { cmd: u8 },
BadValidation { cmd: u8, code: ResponseCode },
InvalidData { err: pmbus::Error },
InvalidConfig,
}
impl From<BadValidation> for Error {
fn from(value: BadValidation) -> Self {
Self::BadValidation {
cmd: value.cmd,
code: value.code,
}
}
}
impl From<pmbus::Error> for Error {
fn from(err: pmbus::Error) -> Self {
Error::InvalidData { err }
}
}
impl From<Error> for ResponseCode {
fn from(err: Error) -> Self {
match err {
Error::BadRead { code, .. } => code,
Error::BadWrite { code, .. } => code,
Error::BadValidation { code, .. } => code,
_ => panic!(),
}
}
}
#[derive(Copy, Clone)]
#[allow(dead_code)]
struct Coefficients {
voltage: pmbus::Coefficients,
current: pmbus::Coefficients,
power: pmbus::Coefficients,
}
pub struct Adm1272 {
/// Underlying I2C device
device: I2cDevice,
/// Value of the rsense resistor, in milliohms
rsense: i32,
/// Our (cached) coefficients
coefficients: Cell<Option<Coefficients>>,
/// Our (cached) configuration
config: Cell<Option<adm1272::PMON_CONFIG::CommandData>>,
}
impl core::fmt::Display for Adm1272 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "adm1272: {}", &self.device)
}
}
#[derive(Copy, Clone, PartialEq)]
enum Trace {
Coefficients(pmbus::Coefficients),
Config(adm1272::PMON_CONFIG::CommandData),
WriteConfig(adm1272::PMON_CONFIG::CommandData),
None,
}
ringbuf!(Trace, 32, Trace::None);
impl Adm1272 {
pub fn new(device: &I2cDevice, rsense: Ohms) -> Self {
Self {
device: *device,
rsense: (rsense.0 * 1000.0).round() as i32,
coefficients: Cell::new(None),
config: Cell::new(None),
}
}
fn read_config(&self) -> Result<adm1272::PMON_CONFIG::CommandData, Error> {
if let Some(ref config) = self.config.get() {
return Ok(*config);
}
let config = pmbus_read!(self.device, adm1272::PMON_CONFIG)?;
ringbuf_entry!(Trace::Config(config));
self.config.set(Some(config));
Ok(config)
}
fn write_config(
&self,
config: adm1272::PMON_CONFIG::CommandData,
) -> Result<(), Error> {
ringbuf_entry!(Trace::WriteConfig(config));
let out = pmbus_write!(self.device, adm1272::PMON_CONFIG, config);
if out.is_err() {
// If the write fails, invalidate the cache, since we don't
// know exactly what state the remote system ended up in.
self.config.set(None);
}
out
}
//
// Unlike many/most PMBus devices that have one set of coefficients, the
// coefficients for the ADM1272 depends on the mode of the device. We
// therefore determine these dynamically -- but cache the results.
//
fn load_coefficients(&self) -> Result<Coefficients, Error> {
use adm1272::PMON_CONFIG::*;
if let Some(coefficients) = self.coefficients.get() {
return Ok(coefficients);
}
let config = self.read_config()?;
let vrange = config.get_v_range().ok_or(Error::InvalidConfig)?;
let irange = config.get_i_range().ok_or(Error::InvalidConfig)?;
//
// From Table 10 (columns 1 and 2) of the ADM1272 datasheet.
//
let voltage = match vrange {
VRange::Range100V => pmbus::Coefficients {
m: 4062,
b: 0,
R: -2,
},
VRange::Range60V => pmbus::Coefficients {
m: 6770,
b: 0,
R: -2,
},
};
ringbuf_entry!(Trace::Coefficients(voltage));
//
// From Table 10 (columns 3 and 4) of the ADM1272 datasheet.
//
let current = match irange {
IRange::Range30mV => pmbus::Coefficients {
m: 663 * self.rsense,
b: 20480,
R: -1,
},
IRange::Range15mV => pmbus::Coefficients {
m: 1326 * self.rsense,
b: 20480,
R: -1,
},
};
ringbuf_entry!(Trace::Coefficients(current));
//
// From Table 10 (columns 5 through 8) of the ADM1272 datasheet.
//
let power = match (irange, vrange) {
(IRange::Range15mV, VRange::Range60V) => pmbus::Coefficients {
m: 3512 * self.rsense,
b: 0,
R: -2,
},
(IRange::Range15mV, VRange::Range100V) => pmbus::Coefficients {
m: 21071 * self.rsense,
b: 0,
R: -3,
},
(IRange::Range30mV, VRange::Range60V) => pmbus::Coefficients {
m: 17561 * self.rsense,
b: 0,
R: -3,
},
(IRange::Range30mV, VRange::Range100V) => pmbus::Coefficients {
m: 10535 * self.rsense,
b: 0,
R: -3,
},
};
ringbuf_entry!(Trace::Coefficients(power));
self.coefficients.set(Some(Coefficients {
voltage,
current,
power,
}));
Ok(self.coefficients.get().unwrap())
}
fn enable_vin_sampling(&self) -> Result<(), Error> {
use adm1272::PMON_CONFIG::*;
let mut config = self.read_config()?;
match config.get_v_in_enable() {
None => Err(Error::InvalidConfig),
Some(VInEnable::Disabled) => {
config.set_v_in_enable(VInEnable::Enabled);
self.write_config(config)
}
_ => Ok(()),
}
}
fn enable_vout_sampling(&self) -> Result<(), Error> {
use adm1272::PMON_CONFIG::*;
let mut config = self.read_config()?;
match config.get_v_out_enable() {
None => Err(Error::InvalidConfig),
Some(VOutEnable::Disabled) => {
config.set_v_out_enable(VOutEnable::Enabled);
self.write_config(config)
}
_ => Ok(()),
}
}
fn enable_temp1_sampling(&self) -> Result<(), Error> {
use adm1272::PMON_CONFIG::*;
let mut config = self.read_config()?;
match config.get_temp_1_enable() {
None => Err(Error::InvalidConfig),
Some(Temp1Enable::Disabled) => {
config.set_temp_1_enable(Temp1Enable::Enabled);
self.write_config(config)
}
_ => Ok(()),
}
}
pub fn read_vin(&self) -> Result<Volts, Error> {
self.enable_vin_sampling()?;
let vin = pmbus_read!(self.device, adm1272::READ_VIN)?;
Ok(Volts(vin.get(&self.load_coefficients()?.voltage)?.0))
}
pub fn peak_iout(&self) -> Result<Amperes, Error> {
let iout = pmbus_read!(self.device, adm1272::PEAK_IOUT)?;
Ok(Amperes(iout.get(&self.load_coefficients()?.current)?.0))
}
pub fn i2c_device(&self) -> &I2cDevice {
&self.device
}
}
impl Validate<Error> for Adm1272 {
fn validate(device: &I2cDevice) -> Result<bool, Error> {
let expected = b"ADM1272-2A";
pmbus_validate(device, CommandCode::MFR_MODEL, expected)
.map_err(Into::into)
}
}
impl TempSensor<Error> for Adm1272 {
fn read_temperature(&self) -> Result<Celsius, Error> {
self.enable_temp1_sampling()?;
let temp = pmbus_read!(self.device, adm1272::READ_TEMPERATURE_1)?;
Ok(Celsius(temp.get()?.0))
}
}
impl CurrentSensor<Error> for Adm1272 {
fn read_iout(&self) -> Result<Amperes, Error> {
let iout = pmbus_read!(self.device, adm1272::READ_IOUT)?;
Ok(Amperes(iout.get(&self.load_coefficients()?.current)?.0))
}
}
impl VoltageSensor<Error> for Adm1272 {
fn read_vout(&self) -> Result<Volts, Error> {
self.enable_vout_sampling()?;
let vout = pmbus_read!(self.device, adm1272::READ_VOUT)?;
Ok(Volts(vout.get(&self.load_coefficients()?.voltage)?.0))
}
}