2 * Analog Devices ADM1272 High Voltage Positive Hot Swap Controller and Digital
3 * Power Monitor with PMBus
5 * Copyright 2021 Google LLC
7 * SPDX-License-Identifier: GPL-2.0-or-later
10 #include "qemu/osdep.h"
12 #include "hw/i2c/pmbus_device.h"
14 #include "migration/vmstate.h"
15 #include "qapi/error.h"
16 #include "qapi/visitor.h"
18 #include "qemu/module.h"
20 #define TYPE_ADM1272 "adm1272"
21 #define ADM1272(obj) OBJECT_CHECK(ADM1272State, (obj), TYPE_ADM1272)
23 #define ADM1272_RESTART_TIME 0xCC
24 #define ADM1272_MFR_PEAK_IOUT 0xD0
25 #define ADM1272_MFR_PEAK_VIN 0xD1
26 #define ADM1272_MFR_PEAK_VOUT 0xD2
27 #define ADM1272_MFR_PMON_CONTROL 0xD3
28 #define ADM1272_MFR_PMON_CONFIG 0xD4
29 #define ADM1272_MFR_ALERT1_CONFIG 0xD5
30 #define ADM1272_MFR_ALERT2_CONFIG 0xD6
31 #define ADM1272_MFR_PEAK_TEMPERATURE 0xD7
32 #define ADM1272_MFR_DEVICE_CONFIG 0xD8
33 #define ADM1272_MFR_POWER_CYCLE 0xD9
34 #define ADM1272_MFR_PEAK_PIN 0xDA
35 #define ADM1272_MFR_READ_PIN_EXT 0xDB
36 #define ADM1272_MFR_READ_EIN_EXT 0xDC
38 #define ADM1272_HYSTERESIS_LOW 0xF2
39 #define ADM1272_HYSTERESIS_HIGH 0xF3
40 #define ADM1272_STATUS_HYSTERESIS 0xF4
41 #define ADM1272_STATUS_GPIO 0xF5
42 #define ADM1272_STRT_UP_IOUT_LIM 0xF6
45 #define ADM1272_OPERATION_DEFAULT 0x80
46 #define ADM1272_CAPABILITY_DEFAULT 0xB0
47 #define ADM1272_CAPABILITY_NO_PEC 0x30
48 #define ADM1272_DIRECT_MODE 0x40
49 #define ADM1272_HIGH_LIMIT_DEFAULT 0x0FFF
50 #define ADM1272_PIN_OP_DEFAULT 0x7FFF
51 #define ADM1272_PMBUS_REVISION_DEFAULT 0x22
52 #define ADM1272_MFR_ID_DEFAULT "ADI"
53 #define ADM1272_MODEL_DEFAULT "ADM1272-A1"
54 #define ADM1272_MFR_DEFAULT_REVISION "25"
55 #define ADM1272_DEFAULT_DATE "160301"
56 #define ADM1272_RESTART_TIME_DEFAULT 0x64
57 #define ADM1272_PMON_CONTROL_DEFAULT 0x1
58 #define ADM1272_PMON_CONFIG_DEFAULT 0x3F35
59 #define ADM1272_DEVICE_CONFIG_DEFAULT 0x8
60 #define ADM1272_HYSTERESIS_HIGH_DEFAULT 0xFFFF
61 #define ADM1272_STRT_UP_IOUT_LIM_DEFAULT 0x000F
62 #define ADM1272_VOLT_DEFAULT 12000
63 #define ADM1272_IOUT_DEFAULT 25000
64 #define ADM1272_PWR_DEFAULT 300 /* 12V 25A */
65 #define ADM1272_SHUNT 300 /* micro-ohms */
66 #define ADM1272_VOLTAGE_COEFF_DEFAULT 1
67 #define ADM1272_CURRENT_COEFF_DEFAULT 3
68 #define ADM1272_PWR_COEFF_DEFAULT 7
69 #define ADM1272_IOUT_OFFSET 0x5000
70 #define ADM1272_IOUT_OFFSET 0x5000
73 typedef struct ADM1272State
{
83 uint16_t peak_temperature
;
88 uint16_t alert1_config
;
89 uint16_t alert2_config
;
90 uint16_t device_config
;
92 uint16_t hysteresis_low
;
93 uint16_t hysteresis_high
;
94 uint8_t status_hysteresis
;
97 uint16_t strt_up_iout_lim
;
101 static const PMBusCoefficients adm1272_coefficients
[] = {
102 [0] = { 6770, 0, -2 }, /* voltage, vrange 60V */
103 [1] = { 4062, 0, -2 }, /* voltage, vrange 100V */
104 [2] = { 1326, 20480, -1 }, /* current, vsense range 15mV */
105 [3] = { 663, 20480, -1 }, /* current, vsense range 30mV */
106 [4] = { 3512, 0, -2 }, /* power, vrange 60V, irange 15mV */
107 [5] = { 21071, 0, -3 }, /* power, vrange 100V, irange 15mV */
108 [6] = { 17561, 0, -3 }, /* power, vrange 60V, irange 30mV */
109 [7] = { 10535, 0, -3 }, /* power, vrange 100V, irange 30mV */
110 [8] = { 42, 31871, -1 }, /* temperature */
113 static void adm1272_check_limits(ADM1272State
*s
)
115 PMBusDevice
*pmdev
= PMBUS_DEVICE(s
);
117 pmbus_check_limits(pmdev
);
119 if (pmdev
->pages
[0].read_vout
> s
->peak_vout
) {
120 s
->peak_vout
= pmdev
->pages
[0].read_vout
;
123 if (pmdev
->pages
[0].read_vin
> s
->peak_vin
) {
124 s
->peak_vin
= pmdev
->pages
[0].read_vin
;
127 if (pmdev
->pages
[0].read_iout
> s
->peak_iout
) {
128 s
->peak_iout
= pmdev
->pages
[0].read_iout
;
131 if (pmdev
->pages
[0].read_temperature_1
> s
->peak_temperature
) {
132 s
->peak_temperature
= pmdev
->pages
[0].read_temperature_1
;
135 if (pmdev
->pages
[0].read_pin
> s
->peak_pin
) {
136 s
->peak_pin
= pmdev
->pages
[0].read_pin
;
140 static uint16_t adm1272_millivolts_to_direct(uint32_t value
)
142 PMBusCoefficients c
= adm1272_coefficients
[ADM1272_VOLTAGE_COEFF_DEFAULT
];
145 return pmbus_data2direct_mode(c
, value
);
148 static uint32_t adm1272_direct_to_millivolts(uint16_t value
)
150 PMBusCoefficients c
= adm1272_coefficients
[ADM1272_VOLTAGE_COEFF_DEFAULT
];
153 return pmbus_direct_mode2data(c
, value
);
156 static uint16_t adm1272_milliamps_to_direct(uint32_t value
)
158 PMBusCoefficients c
= adm1272_coefficients
[ADM1272_CURRENT_COEFF_DEFAULT
];
159 /* Y = (m * r_sense * x - b) * 10^R */
160 c
.m
= c
.m
* ADM1272_SHUNT
/ 1000; /* micro-ohms */
163 return pmbus_data2direct_mode(c
, value
);
166 static uint32_t adm1272_direct_to_milliamps(uint16_t value
)
168 PMBusCoefficients c
= adm1272_coefficients
[ADM1272_CURRENT_COEFF_DEFAULT
];
169 c
.m
= c
.m
* ADM1272_SHUNT
/ 1000;
172 return pmbus_direct_mode2data(c
, value
);
175 static uint16_t adm1272_watts_to_direct(uint32_t value
)
177 PMBusCoefficients c
= adm1272_coefficients
[ADM1272_PWR_COEFF_DEFAULT
];
178 c
.m
= c
.m
* ADM1272_SHUNT
/ 1000;
179 return pmbus_data2direct_mode(c
, value
);
182 static uint32_t adm1272_direct_to_watts(uint16_t value
)
184 PMBusCoefficients c
= adm1272_coefficients
[ADM1272_PWR_COEFF_DEFAULT
];
185 c
.m
= c
.m
* ADM1272_SHUNT
/ 1000;
186 return pmbus_direct_mode2data(c
, value
);
189 static void adm1272_exit_reset(Object
*obj
)
191 ADM1272State
*s
= ADM1272(obj
);
192 PMBusDevice
*pmdev
= PMBUS_DEVICE(obj
);
195 pmdev
->pages
[0].operation
= ADM1272_OPERATION_DEFAULT
;
198 pmdev
->capability
= ADM1272_CAPABILITY_NO_PEC
;
199 pmdev
->pages
[0].revision
= ADM1272_PMBUS_REVISION_DEFAULT
;
200 pmdev
->pages
[0].vout_mode
= ADM1272_DIRECT_MODE
;
201 pmdev
->pages
[0].vout_ov_warn_limit
= ADM1272_HIGH_LIMIT_DEFAULT
;
202 pmdev
->pages
[0].vout_uv_warn_limit
= 0;
203 pmdev
->pages
[0].iout_oc_warn_limit
= ADM1272_HIGH_LIMIT_DEFAULT
;
204 pmdev
->pages
[0].ot_fault_limit
= ADM1272_HIGH_LIMIT_DEFAULT
;
205 pmdev
->pages
[0].ot_warn_limit
= ADM1272_HIGH_LIMIT_DEFAULT
;
206 pmdev
->pages
[0].vin_ov_warn_limit
= ADM1272_HIGH_LIMIT_DEFAULT
;
207 pmdev
->pages
[0].vin_uv_warn_limit
= 0;
208 pmdev
->pages
[0].pin_op_warn_limit
= ADM1272_PIN_OP_DEFAULT
;
210 pmdev
->pages
[0].status_word
= 0;
211 pmdev
->pages
[0].status_vout
= 0;
212 pmdev
->pages
[0].status_iout
= 0;
213 pmdev
->pages
[0].status_input
= 0;
214 pmdev
->pages
[0].status_temperature
= 0;
215 pmdev
->pages
[0].status_mfr_specific
= 0;
217 pmdev
->pages
[0].read_vin
218 = adm1272_millivolts_to_direct(ADM1272_VOLT_DEFAULT
);
219 pmdev
->pages
[0].read_vout
220 = adm1272_millivolts_to_direct(ADM1272_VOLT_DEFAULT
);
221 pmdev
->pages
[0].read_iout
222 = adm1272_milliamps_to_direct(ADM1272_IOUT_DEFAULT
);
223 pmdev
->pages
[0].read_temperature_1
= 0;
224 pmdev
->pages
[0].read_pin
= adm1272_watts_to_direct(ADM1272_PWR_DEFAULT
);
225 pmdev
->pages
[0].revision
= ADM1272_PMBUS_REVISION_DEFAULT
;
226 pmdev
->pages
[0].mfr_id
= ADM1272_MFR_ID_DEFAULT
;
227 pmdev
->pages
[0].mfr_model
= ADM1272_MODEL_DEFAULT
;
228 pmdev
->pages
[0].mfr_revision
= ADM1272_MFR_DEFAULT_REVISION
;
229 pmdev
->pages
[0].mfr_date
= ADM1272_DEFAULT_DATE
;
233 s
->restart_time
= ADM1272_RESTART_TIME_DEFAULT
;
238 s
->peak_temperature
= 0;
241 s
->pmon_control
= ADM1272_PMON_CONTROL_DEFAULT
;
242 s
->pmon_config
= ADM1272_PMON_CONFIG_DEFAULT
;
243 s
->alert1_config
= 0;
244 s
->alert2_config
= 0;
245 s
->device_config
= ADM1272_DEVICE_CONFIG_DEFAULT
;
247 s
->hysteresis_low
= 0;
248 s
->hysteresis_high
= ADM1272_HYSTERESIS_HIGH_DEFAULT
;
249 s
->status_hysteresis
= 0;
252 s
->strt_up_iout_lim
= ADM1272_STRT_UP_IOUT_LIM_DEFAULT
;
255 static uint8_t adm1272_read_byte(PMBusDevice
*pmdev
)
257 ADM1272State
*s
= ADM1272(pmdev
);
259 switch (pmdev
->code
) {
260 case ADM1272_RESTART_TIME
:
261 pmbus_send8(pmdev
, s
->restart_time
);
264 case ADM1272_MFR_PEAK_IOUT
:
265 pmbus_send16(pmdev
, s
->peak_iout
);
268 case ADM1272_MFR_PEAK_VIN
:
269 pmbus_send16(pmdev
, s
->peak_vin
);
272 case ADM1272_MFR_PEAK_VOUT
:
273 pmbus_send16(pmdev
, s
->peak_vout
);
276 case ADM1272_MFR_PMON_CONTROL
:
277 pmbus_send8(pmdev
, s
->pmon_control
);
280 case ADM1272_MFR_PMON_CONFIG
:
281 pmbus_send16(pmdev
, s
->pmon_config
);
284 case ADM1272_MFR_ALERT1_CONFIG
:
285 pmbus_send16(pmdev
, s
->alert1_config
);
288 case ADM1272_MFR_ALERT2_CONFIG
:
289 pmbus_send16(pmdev
, s
->alert2_config
);
292 case ADM1272_MFR_PEAK_TEMPERATURE
:
293 pmbus_send16(pmdev
, s
->peak_temperature
);
296 case ADM1272_MFR_DEVICE_CONFIG
:
297 pmbus_send16(pmdev
, s
->device_config
);
300 case ADM1272_MFR_PEAK_PIN
:
301 pmbus_send16(pmdev
, s
->peak_pin
);
304 case ADM1272_MFR_READ_PIN_EXT
:
305 pmbus_send32(pmdev
, s
->pin_ext
);
308 case ADM1272_MFR_READ_EIN_EXT
:
309 pmbus_send64(pmdev
, s
->ein_ext
);
312 case ADM1272_HYSTERESIS_LOW
:
313 pmbus_send16(pmdev
, s
->hysteresis_low
);
316 case ADM1272_HYSTERESIS_HIGH
:
317 pmbus_send16(pmdev
, s
->hysteresis_high
);
320 case ADM1272_STATUS_HYSTERESIS
:
321 pmbus_send16(pmdev
, s
->status_hysteresis
);
324 case ADM1272_STATUS_GPIO
:
325 pmbus_send16(pmdev
, s
->status_gpio
);
328 case ADM1272_STRT_UP_IOUT_LIM
:
329 pmbus_send16(pmdev
, s
->strt_up_iout_lim
);
333 qemu_log_mask(LOG_GUEST_ERROR
,
334 "%s: reading from unsupported register: 0x%02x\n",
335 __func__
, pmdev
->code
);
343 static int adm1272_write_data(PMBusDevice
*pmdev
, const uint8_t *buf
,
346 ADM1272State
*s
= ADM1272(pmdev
);
349 qemu_log_mask(LOG_GUEST_ERROR
, "%s: writing empty data\n", __func__
);
353 pmdev
->code
= buf
[0]; /* PMBus command code */
359 /* Exclude command code from buffer */
363 switch (pmdev
->code
) {
365 case ADM1272_RESTART_TIME
:
366 s
->restart_time
= pmbus_receive8(pmdev
);
369 case ADM1272_MFR_PMON_CONTROL
:
370 s
->pmon_control
= pmbus_receive8(pmdev
);
373 case ADM1272_MFR_PMON_CONFIG
:
374 s
->pmon_config
= pmbus_receive16(pmdev
);
377 case ADM1272_MFR_ALERT1_CONFIG
:
378 s
->alert1_config
= pmbus_receive16(pmdev
);
381 case ADM1272_MFR_ALERT2_CONFIG
:
382 s
->alert2_config
= pmbus_receive16(pmdev
);
385 case ADM1272_MFR_DEVICE_CONFIG
:
386 s
->device_config
= pmbus_receive16(pmdev
);
389 case ADM1272_MFR_POWER_CYCLE
:
390 adm1272_exit_reset((Object
*)s
);
393 case ADM1272_HYSTERESIS_LOW
:
394 s
->hysteresis_low
= pmbus_receive16(pmdev
);
397 case ADM1272_HYSTERESIS_HIGH
:
398 s
->hysteresis_high
= pmbus_receive16(pmdev
);
401 case ADM1272_STRT_UP_IOUT_LIM
:
402 s
->strt_up_iout_lim
= pmbus_receive16(pmdev
);
403 adm1272_check_limits(s
);
407 qemu_log_mask(LOG_GUEST_ERROR
,
408 "%s: writing to unsupported register: 0x%02x\n",
409 __func__
, pmdev
->code
);
415 static void adm1272_get(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
420 if (strcmp(name
, "vin") == 0 || strcmp(name
, "vout") == 0) {
421 value
= adm1272_direct_to_millivolts(*(uint16_t *)opaque
);
422 } else if (strcmp(name
, "iout") == 0) {
423 value
= adm1272_direct_to_milliamps(*(uint16_t *)opaque
);
424 } else if (strcmp(name
, "pin") == 0) {
425 value
= adm1272_direct_to_watts(*(uint16_t *)opaque
);
427 value
= *(uint16_t *)opaque
;
430 visit_type_uint16(v
, name
, &value
, errp
);
433 static void adm1272_set(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
436 ADM1272State
*s
= ADM1272(obj
);
437 uint16_t *internal
= opaque
;
440 if (!visit_type_uint16(v
, name
, &value
, errp
)) {
444 if (strcmp(name
, "vin") == 0 || strcmp(name
, "vout") == 0) {
445 *internal
= adm1272_millivolts_to_direct(value
);
446 } else if (strcmp(name
, "iout") == 0) {
447 *internal
= adm1272_milliamps_to_direct(value
);
448 } else if (strcmp(name
, "pin") == 0) {
449 *internal
= adm1272_watts_to_direct(value
);
454 adm1272_check_limits(s
);
457 static const VMStateDescription vmstate_adm1272
= {
460 .minimum_version_id
= 0,
461 .fields
= (VMStateField
[]){
462 VMSTATE_PMBUS_DEVICE(parent
, ADM1272State
),
463 VMSTATE_UINT64(ein_ext
, ADM1272State
),
464 VMSTATE_UINT32(pin_ext
, ADM1272State
),
465 VMSTATE_UINT8(restart_time
, ADM1272State
),
467 VMSTATE_UINT16(peak_vin
, ADM1272State
),
468 VMSTATE_UINT16(peak_vout
, ADM1272State
),
469 VMSTATE_UINT16(peak_iout
, ADM1272State
),
470 VMSTATE_UINT16(peak_temperature
, ADM1272State
),
471 VMSTATE_UINT16(peak_pin
, ADM1272State
),
473 VMSTATE_UINT8(pmon_control
, ADM1272State
),
474 VMSTATE_UINT16(pmon_config
, ADM1272State
),
475 VMSTATE_UINT16(alert1_config
, ADM1272State
),
476 VMSTATE_UINT16(alert2_config
, ADM1272State
),
477 VMSTATE_UINT16(device_config
, ADM1272State
),
479 VMSTATE_UINT16(hysteresis_low
, ADM1272State
),
480 VMSTATE_UINT16(hysteresis_high
, ADM1272State
),
481 VMSTATE_UINT8(status_hysteresis
, ADM1272State
),
482 VMSTATE_UINT8(status_gpio
, ADM1272State
),
484 VMSTATE_UINT16(strt_up_iout_lim
, ADM1272State
),
485 VMSTATE_END_OF_LIST()
489 static void adm1272_init(Object
*obj
)
491 PMBusDevice
*pmdev
= PMBUS_DEVICE(obj
);
492 uint64_t flags
= PB_HAS_VOUT_MODE
| PB_HAS_VOUT
| PB_HAS_VIN
| PB_HAS_IOUT
|
493 PB_HAS_PIN
| PB_HAS_TEMPERATURE
| PB_HAS_MFR_INFO
;
495 pmbus_page_config(pmdev
, 0, flags
);
497 object_property_add(obj
, "vin", "uint16",
499 adm1272_set
, NULL
, &pmdev
->pages
[0].read_vin
);
501 object_property_add(obj
, "vout", "uint16",
503 adm1272_set
, NULL
, &pmdev
->pages
[0].read_vout
);
505 object_property_add(obj
, "iout", "uint16",
507 adm1272_set
, NULL
, &pmdev
->pages
[0].read_iout
);
509 object_property_add(obj
, "pin", "uint16",
511 adm1272_set
, NULL
, &pmdev
->pages
[0].read_pin
);
515 static void adm1272_class_init(ObjectClass
*klass
, void *data
)
517 ResettableClass
*rc
= RESETTABLE_CLASS(klass
);
518 DeviceClass
*dc
= DEVICE_CLASS(klass
);
519 PMBusDeviceClass
*k
= PMBUS_DEVICE_CLASS(klass
);
521 dc
->desc
= "Analog Devices ADM1272 Hot Swap controller";
522 dc
->vmsd
= &vmstate_adm1272
;
523 k
->write_data
= adm1272_write_data
;
524 k
->receive_byte
= adm1272_read_byte
;
525 k
->device_num_pages
= 1;
527 rc
->phases
.exit
= adm1272_exit_reset
;
530 static const TypeInfo adm1272_info
= {
531 .name
= TYPE_ADM1272
,
532 .parent
= TYPE_PMBUS_DEVICE
,
533 .instance_size
= sizeof(ADM1272State
),
534 .instance_init
= adm1272_init
,
535 .class_init
= adm1272_class_init
,
538 static void adm1272_register_types(void)
540 type_register_static(&adm1272_info
);
543 type_init(adm1272_register_types
)