xref: /openbmc/qemu/include/hw/i2c/pmbus_device.h (revision 1580b897)
1 /*
2  * QEMU PMBus device emulation
3  *
4  * Copyright 2021 Google LLC
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #ifndef HW_PMBUS_DEVICE_H
10 #define HW_PMBUS_DEVICE_H
11 
12 #include "qemu/bitops.h"
13 #include "hw/i2c/smbus_slave.h"
14 
15 enum pmbus_registers {
16     PMBUS_PAGE                      = 0x00, /* R/W byte */
17     PMBUS_OPERATION                 = 0x01, /* R/W byte */
18     PMBUS_ON_OFF_CONFIG             = 0x02, /* R/W byte */
19     PMBUS_CLEAR_FAULTS              = 0x03, /* Send Byte */
20     PMBUS_PHASE                     = 0x04, /* R/W byte */
21     PMBUS_PAGE_PLUS_WRITE           = 0x05, /* Block Write-only */
22     PMBUS_PAGE_PLUS_READ            = 0x06, /* Block Read-only */
23     PMBUS_WRITE_PROTECT             = 0x10, /* R/W byte */
24     PMBUS_STORE_DEFAULT_ALL         = 0x11, /* Send Byte */
25     PMBUS_RESTORE_DEFAULT_ALL       = 0x12, /* Send Byte */
26     PMBUS_STORE_DEFAULT_CODE        = 0x13, /* Write-only Byte */
27     PMBUS_RESTORE_DEFAULT_CODE      = 0x14, /* Write-only Byte */
28     PMBUS_STORE_USER_ALL            = 0x15, /* Send Byte */
29     PMBUS_RESTORE_USER_ALL          = 0x16, /* Send Byte */
30     PMBUS_STORE_USER_CODE           = 0x17, /* Write-only Byte */
31     PMBUS_RESTORE_USER_CODE         = 0x18, /* Write-only Byte */
32     PMBUS_CAPABILITY                = 0x19, /* Read-Only byte */
33     PMBUS_QUERY                     = 0x1A, /* Write-Only */
34     PMBUS_SMBALERT_MASK             = 0x1B, /* Block read, Word write */
35     PMBUS_VOUT_MODE                 = 0x20, /* R/W byte */
36     PMBUS_VOUT_COMMAND              = 0x21, /* R/W word */
37     PMBUS_VOUT_TRIM                 = 0x22, /* R/W word */
38     PMBUS_VOUT_CAL_OFFSET           = 0x23, /* R/W word */
39     PMBUS_VOUT_MAX                  = 0x24, /* R/W word */
40     PMBUS_VOUT_MARGIN_HIGH          = 0x25, /* R/W word */
41     PMBUS_VOUT_MARGIN_LOW           = 0x26, /* R/W word */
42     PMBUS_VOUT_TRANSITION_RATE      = 0x27, /* R/W word */
43     PMBUS_VOUT_DROOP                = 0x28, /* R/W word */
44     PMBUS_VOUT_SCALE_LOOP           = 0x29, /* R/W word */
45     PMBUS_VOUT_SCALE_MONITOR        = 0x2A, /* R/W word */
46     PMBUS_COEFFICIENTS              = 0x30, /* Read-only block 5 bytes */
47     PMBUS_POUT_MAX                  = 0x31, /* R/W word */
48     PMBUS_MAX_DUTY                  = 0x32, /* R/W word */
49     PMBUS_FREQUENCY_SWITCH          = 0x33, /* R/W word */
50     PMBUS_VIN_ON                    = 0x35, /* R/W word */
51     PMBUS_VIN_OFF                   = 0x36, /* R/W word */
52     PMBUS_INTERLEAVE                = 0x37, /* R/W word */
53     PMBUS_IOUT_CAL_GAIN             = 0x38, /* R/W word */
54     PMBUS_IOUT_CAL_OFFSET           = 0x39, /* R/W word */
55     PMBUS_FAN_CONFIG_1_2            = 0x3A, /* R/W byte */
56     PMBUS_FAN_COMMAND_1             = 0x3B, /* R/W word */
57     PMBUS_FAN_COMMAND_2             = 0x3C, /* R/W word */
58     PMBUS_FAN_CONFIG_3_4            = 0x3D, /* R/W byte */
59     PMBUS_FAN_COMMAND_3             = 0x3E, /* R/W word */
60     PMBUS_FAN_COMMAND_4             = 0x3F, /* R/W word */
61     PMBUS_VOUT_OV_FAULT_LIMIT       = 0x40, /* R/W word */
62     PMBUS_VOUT_OV_FAULT_RESPONSE    = 0x41, /* R/W byte */
63     PMBUS_VOUT_OV_WARN_LIMIT        = 0x42, /* R/W word */
64     PMBUS_VOUT_UV_WARN_LIMIT        = 0x43, /* R/W word */
65     PMBUS_VOUT_UV_FAULT_LIMIT       = 0x44, /* R/W word */
66     PMBUS_VOUT_UV_FAULT_RESPONSE    = 0x45, /* R/W byte */
67     PMBUS_IOUT_OC_FAULT_LIMIT       = 0x46, /* R/W word */
68     PMBUS_IOUT_OC_FAULT_RESPONSE    = 0x47, /* R/W byte */
69     PMBUS_IOUT_OC_LV_FAULT_LIMIT    = 0x48, /* R/W word */
70     PMBUS_IOUT_OC_LV_FAULT_RESPONSE = 0x49, /* R/W byte */
71     PMBUS_IOUT_OC_WARN_LIMIT        = 0x4A, /* R/W word */
72     PMBUS_IOUT_UC_FAULT_LIMIT       = 0x4B, /* R/W word */
73     PMBUS_IOUT_UC_FAULT_RESPONSE    = 0x4C, /* R/W byte */
74     PMBUS_OT_FAULT_LIMIT            = 0x4F, /* R/W word */
75     PMBUS_OT_FAULT_RESPONSE         = 0x50, /* R/W byte */
76     PMBUS_OT_WARN_LIMIT             = 0x51, /* R/W word */
77     PMBUS_UT_WARN_LIMIT             = 0x52, /* R/W word */
78     PMBUS_UT_FAULT_LIMIT            = 0x53, /* R/W word */
79     PMBUS_UT_FAULT_RESPONSE         = 0x54, /* R/W byte */
80     PMBUS_VIN_OV_FAULT_LIMIT        = 0x55, /* R/W word */
81     PMBUS_VIN_OV_FAULT_RESPONSE     = 0x56, /* R/W byte */
82     PMBUS_VIN_OV_WARN_LIMIT         = 0x57, /* R/W word */
83     PMBUS_VIN_UV_WARN_LIMIT         = 0x58, /* R/W word */
84     PMBUS_VIN_UV_FAULT_LIMIT        = 0x59, /* R/W word */
85     PMBUS_VIN_UV_FAULT_RESPONSE     = 0x5A, /* R/W byte */
86     PMBUS_IIN_OC_FAULT_LIMIT        = 0x5B, /* R/W word */
87     PMBUS_IIN_OC_FAULT_RESPONSE     = 0x5C, /* R/W byte */
88     PMBUS_IIN_OC_WARN_LIMIT         = 0x5D, /* R/W word */
89     PMBUS_POWER_GOOD_ON             = 0x5E, /* R/W word */
90     PMBUS_POWER_GOOD_OFF            = 0x5F, /* R/W word */
91     PMBUS_TON_DELAY                 = 0x60, /* R/W word */
92     PMBUS_TON_RISE                  = 0x61, /* R/W word */
93     PMBUS_TON_MAX_FAULT_LIMIT       = 0x62, /* R/W word */
94     PMBUS_TON_MAX_FAULT_RESPONSE    = 0x63, /* R/W byte */
95     PMBUS_TOFF_DELAY                = 0x64, /* R/W word */
96     PMBUS_TOFF_FALL                 = 0x65, /* R/W word */
97     PMBUS_TOFF_MAX_WARN_LIMIT       = 0x66, /* R/W word */
98     PMBUS_POUT_OP_FAULT_LIMIT       = 0x68, /* R/W word */
99     PMBUS_POUT_OP_FAULT_RESPONSE    = 0x69, /* R/W byte */
100     PMBUS_POUT_OP_WARN_LIMIT        = 0x6A, /* R/W word */
101     PMBUS_PIN_OP_WARN_LIMIT         = 0x6B, /* R/W word */
102     PMBUS_STATUS_BYTE               = 0x78, /* R/W byte */
103     PMBUS_STATUS_WORD               = 0x79, /* R/W word */
104     PMBUS_STATUS_VOUT               = 0x7A, /* R/W byte */
105     PMBUS_STATUS_IOUT               = 0x7B, /* R/W byte */
106     PMBUS_STATUS_INPUT              = 0x7C, /* R/W byte */
107     PMBUS_STATUS_TEMPERATURE        = 0x7D, /* R/W byte */
108     PMBUS_STATUS_CML                = 0x7E, /* R/W byte */
109     PMBUS_STATUS_OTHER              = 0x7F, /* R/W byte */
110     PMBUS_STATUS_MFR_SPECIFIC       = 0x80, /* R/W byte */
111     PMBUS_STATUS_FANS_1_2           = 0x81, /* R/W byte */
112     PMBUS_STATUS_FANS_3_4           = 0x82, /* R/W byte */
113     PMBUS_READ_EIN                  = 0x86, /* Read-Only block 5 bytes */
114     PMBUS_READ_EOUT                 = 0x87, /* Read-Only block 5 bytes */
115     PMBUS_READ_VIN                  = 0x88, /* Read-Only word */
116     PMBUS_READ_IIN                  = 0x89, /* Read-Only word */
117     PMBUS_READ_VCAP                 = 0x8A, /* Read-Only word */
118     PMBUS_READ_VOUT                 = 0x8B, /* Read-Only word */
119     PMBUS_READ_IOUT                 = 0x8C, /* Read-Only word */
120     PMBUS_READ_TEMPERATURE_1        = 0x8D, /* Read-Only word */
121     PMBUS_READ_TEMPERATURE_2        = 0x8E, /* Read-Only word */
122     PMBUS_READ_TEMPERATURE_3        = 0x8F, /* Read-Only word */
123     PMBUS_READ_FAN_SPEED_1          = 0x90, /* Read-Only word */
124     PMBUS_READ_FAN_SPEED_2          = 0x91, /* Read-Only word */
125     PMBUS_READ_FAN_SPEED_3          = 0x92, /* Read-Only word */
126     PMBUS_READ_FAN_SPEED_4          = 0x93, /* Read-Only word */
127     PMBUS_READ_DUTY_CYCLE           = 0x94, /* Read-Only word */
128     PMBUS_READ_FREQUENCY            = 0x95, /* Read-Only word */
129     PMBUS_READ_POUT                 = 0x96, /* Read-Only word */
130     PMBUS_READ_PIN                  = 0x97, /* Read-Only word */
131     PMBUS_REVISION                  = 0x98, /* Read-Only byte */
132     PMBUS_MFR_ID                    = 0x99, /* R/W block */
133     PMBUS_MFR_MODEL                 = 0x9A, /* R/W block */
134     PMBUS_MFR_REVISION              = 0x9B, /* R/W block */
135     PMBUS_MFR_LOCATION              = 0x9C, /* R/W block */
136     PMBUS_MFR_DATE                  = 0x9D, /* R/W block */
137     PMBUS_MFR_SERIAL                = 0x9E, /* R/W block */
138     PMBUS_APP_PROFILE_SUPPORT       = 0x9F, /* Read-Only block-read */
139     PMBUS_MFR_VIN_MIN               = 0xA0, /* Read-Only word */
140     PMBUS_MFR_VIN_MAX               = 0xA1, /* Read-Only word */
141     PMBUS_MFR_IIN_MAX               = 0xA2, /* Read-Only word */
142     PMBUS_MFR_PIN_MAX               = 0xA3, /* Read-Only word */
143     PMBUS_MFR_VOUT_MIN              = 0xA4, /* Read-Only word */
144     PMBUS_MFR_VOUT_MAX              = 0xA5, /* Read-Only word */
145     PMBUS_MFR_IOUT_MAX              = 0xA6, /* Read-Only word */
146     PMBUS_MFR_POUT_MAX              = 0xA7, /* Read-Only word */
147     PMBUS_MFR_TAMBIENT_MAX          = 0xA8, /* Read-Only word */
148     PMBUS_MFR_TAMBIENT_MIN          = 0xA9, /* Read-Only word */
149     PMBUS_MFR_EFFICIENCY_LL         = 0xAA, /* Read-Only block 14 bytes */
150     PMBUS_MFR_EFFICIENCY_HL         = 0xAB, /* Read-Only block 14 bytes */
151     PMBUS_MFR_PIN_ACCURACY          = 0xAC, /* Read-Only byte */
152     PMBUS_IC_DEVICE_ID              = 0xAD, /* Read-Only block-read */
153     PMBUS_IC_DEVICE_REV             = 0xAE, /* Read-Only block-read */
154     PMBUS_MFR_MAX_TEMP_1            = 0xC0, /* R/W word */
155     PMBUS_MFR_MAX_TEMP_2            = 0xC1, /* R/W word */
156     PMBUS_MFR_MAX_TEMP_3            = 0xC2, /* R/W word */
157 };
158 
159 /* STATUS_WORD */
160 #define PB_STATUS_VOUT           BIT(15)
161 #define PB_STATUS_IOUT_POUT      BIT(14)
162 #define PB_STATUS_INPUT          BIT(13)
163 #define PB_STATUS_WORD_MFR       BIT(12)
164 #define PB_STATUS_POWER_GOOD_N   BIT(11)
165 #define PB_STATUS_FAN            BIT(10)
166 #define PB_STATUS_OTHER          BIT(9)
167 #define PB_STATUS_UNKNOWN        BIT(8)
168 /* STATUS_BYTE */
169 #define PB_STATUS_BUSY           BIT(7)
170 #define PB_STATUS_OFF            BIT(6)
171 #define PB_STATUS_VOUT_OV        BIT(5)
172 #define PB_STATUS_IOUT_OC        BIT(4)
173 #define PB_STATUS_VIN_UV         BIT(3)
174 #define PB_STATUS_TEMPERATURE    BIT(2)
175 #define PB_STATUS_CML            BIT(1)
176 #define PB_STATUS_NONE_ABOVE     BIT(0)
177 
178 /* STATUS_VOUT */
179 #define PB_STATUS_VOUT_OV_FAULT         BIT(7) /* Output Overvoltage Fault */
180 #define PB_STATUS_VOUT_OV_WARN          BIT(6) /* Output Overvoltage Warning */
181 #define PB_STATUS_VOUT_UV_WARN          BIT(5) /* Output Undervoltage Warning */
182 #define PB_STATUS_VOUT_UV_FAULT         BIT(4) /* Output Undervoltage Fault */
183 #define PB_STATUS_VOUT_MAX              BIT(3)
184 #define PB_STATUS_VOUT_TON_MAX_FAULT    BIT(2)
185 #define PB_STATUS_VOUT_TOFF_MAX_WARN    BIT(1)
186 
187 /* STATUS_IOUT */
188 #define PB_STATUS_IOUT_OC_FAULT    BIT(7) /* Output Overcurrent Fault */
189 #define PB_STATUS_IOUT_OC_LV_FAULT BIT(6) /* Output OC And Low Voltage Fault */
190 #define PB_STATUS_IOUT_OC_WARN     BIT(5) /* Output Overcurrent Warning */
191 #define PB_STATUS_IOUT_UC_FAULT    BIT(4) /* Output Undercurrent Fault */
192 #define PB_STATUS_CURR_SHARE       BIT(3) /* Current Share Fault */
193 #define PB_STATUS_PWR_LIM_MODE     BIT(2) /* In Power Limiting Mode */
194 #define PB_STATUS_POUT_OP_FAULT    BIT(1) /* Output Overpower Fault */
195 #define PB_STATUS_POUT_OP_WARN     BIT(0) /* Output Overpower Warning */
196 
197 /* STATUS_INPUT */
198 #define PB_STATUS_INPUT_VIN_OV_FAULT    BIT(7) /* Input Overvoltage Fault */
199 #define PB_STATUS_INPUT_VIN_OV_WARN     BIT(6) /* Input Overvoltage Warning */
200 #define PB_STATUS_INPUT_VIN_UV_WARN     BIT(5) /* Input Undervoltage Warning */
201 #define PB_STATUS_INPUT_VIN_UV_FAULT    BIT(4) /* Input Undervoltage Fault */
202 #define PB_STATUS_INPUT_IIN_OC_FAULT    BIT(2) /* Input Overcurrent Fault */
203 #define PB_STATUS_INPUT_IIN_OC_WARN     BIT(1) /* Input Overcurrent Warning */
204 #define PB_STATUS_INPUT_PIN_OP_WARN     BIT(0) /* Input Overpower Warning */
205 
206 /* STATUS_TEMPERATURE */
207 #define PB_STATUS_OT_FAULT              BIT(7) /* Overtemperature Fault */
208 #define PB_STATUS_OT_WARN               BIT(6) /* Overtemperature Warning */
209 #define PB_STATUS_UT_WARN               BIT(5) /* Undertemperature Warning */
210 #define PB_STATUS_UT_FAULT              BIT(4) /* Undertemperature Fault */
211 
212 /* STATUS_CML */
213 #define PB_CML_FAULT_INVALID_CMD     BIT(7) /* Invalid/Unsupported Command */
214 #define PB_CML_FAULT_INVALID_DATA    BIT(6) /* Invalid/Unsupported Data  */
215 #define PB_CML_FAULT_PEC             BIT(5) /* Packet Error Check Failed */
216 #define PB_CML_FAULT_MEMORY          BIT(4) /* Memory Fault Detected */
217 #define PB_CML_FAULT_PROCESSOR       BIT(3) /* Processor Fault Detected */
218 #define PB_CML_FAULT_OTHER_COMM      BIT(1) /* Other communication fault */
219 #define PB_CML_FAULT_OTHER_MEM_LOGIC BIT(0) /* Other Memory Or Logic Fault */
220 
221 /* OPERATION*/
222 #define PB_OP_ON                BIT(7) /* PSU is switched on */
223 #define PB_OP_MARGIN_HIGH       BIT(5) /* PSU vout is set to margin high */
224 #define PB_OP_MARGIN_LOW        BIT(4) /* PSU vout is set to margin low */
225 
226 /* PAGES */
227 #define PB_MAX_PAGES            0x1F
228 #define PB_ALL_PAGES            0xFF
229 
230 #define TYPE_PMBUS_DEVICE "pmbus-device"
231 OBJECT_DECLARE_TYPE(PMBusDevice, PMBusDeviceClass,
232                     PMBUS_DEVICE)
233 
234 /* flags */
235 #define PB_HAS_COEFFICIENTS        BIT_ULL(9)
236 #define PB_HAS_VIN                 BIT_ULL(10)
237 #define PB_HAS_VOUT                BIT_ULL(11)
238 #define PB_HAS_VOUT_MARGIN         BIT_ULL(12)
239 #define PB_HAS_VIN_RATING          BIT_ULL(13)
240 #define PB_HAS_VOUT_RATING         BIT_ULL(14)
241 #define PB_HAS_VOUT_MODE           BIT_ULL(15)
242 #define PB_HAS_IOUT                BIT_ULL(21)
243 #define PB_HAS_IIN                 BIT_ULL(22)
244 #define PB_HAS_IOUT_RATING         BIT_ULL(23)
245 #define PB_HAS_IIN_RATING          BIT_ULL(24)
246 #define PB_HAS_IOUT_GAIN           BIT_ULL(25)
247 #define PB_HAS_POUT                BIT_ULL(30)
248 #define PB_HAS_PIN                 BIT_ULL(31)
249 #define PB_HAS_EIN                 BIT_ULL(32)
250 #define PB_HAS_EOUT                BIT_ULL(33)
251 #define PB_HAS_POUT_RATING         BIT_ULL(34)
252 #define PB_HAS_PIN_RATING          BIT_ULL(35)
253 #define PB_HAS_TEMPERATURE         BIT_ULL(40)
254 #define PB_HAS_TEMP2               BIT_ULL(41)
255 #define PB_HAS_TEMP3               BIT_ULL(42)
256 #define PB_HAS_TEMP_RATING         BIT_ULL(43)
257 #define PB_HAS_MFR_INFO            BIT_ULL(50)
258 
259 struct PMBusDeviceClass {
260     SMBusDeviceClass parent_class;
261     uint8_t device_num_pages;
262 
263     /**
264      * Implement quick_cmd, receive byte, and write_data to support non-standard
265      * PMBus functionality
266      */
267     void (*quick_cmd)(PMBusDevice *dev, uint8_t read);
268     int (*write_data)(PMBusDevice *dev, const uint8_t *buf, uint8_t len);
269     uint8_t (*receive_byte)(PMBusDevice *dev);
270 };
271 
272 /*
273  * According to the spec, each page may offer the full range of PMBus commands
274  * available for each output or non-PMBus device.
275  * Therefore, we can't assume that any registers will always be the same across
276  * all pages.
277  * The page 0xFF is intended for writes to all pages
278  */
279 typedef struct PMBusPage {
280     uint64_t page_flags;
281 
282     uint8_t page;                      /* R/W byte */
283     uint8_t operation;                 /* R/W byte */
284     uint8_t on_off_config;             /* R/W byte */
285     uint8_t write_protect;             /* R/W byte */
286     uint8_t phase;                     /* R/W byte */
287     uint8_t vout_mode;                 /* R/W byte */
288     uint16_t vout_command;             /* R/W word */
289     uint16_t vout_trim;                /* R/W word */
290     uint16_t vout_cal_offset;          /* R/W word */
291     uint16_t vout_max;                 /* R/W word */
292     uint16_t vout_margin_high;         /* R/W word */
293     uint16_t vout_margin_low;          /* R/W word */
294     uint16_t vout_transition_rate;     /* R/W word */
295     uint16_t vout_droop;               /* R/W word */
296     uint16_t vout_scale_loop;          /* R/W word */
297     uint16_t vout_scale_monitor;       /* R/W word */
298     uint8_t coefficients[5];           /* Read-only block 5 bytes */
299     uint16_t pout_max;                 /* R/W word */
300     uint16_t max_duty;                 /* R/W word */
301     uint16_t frequency_switch;         /* R/W word */
302     uint16_t vin_on;                   /* R/W word */
303     uint16_t vin_off;                  /* R/W word */
304     uint16_t iout_cal_gain;            /* R/W word */
305     uint16_t iout_cal_offset;          /* R/W word */
306     uint8_t fan_config_1_2;            /* R/W byte */
307     uint16_t fan_command_1;            /* R/W word */
308     uint16_t fan_command_2;            /* R/W word */
309     uint8_t fan_config_3_4;            /* R/W byte */
310     uint16_t fan_command_3;            /* R/W word */
311     uint16_t fan_command_4;            /* R/W word */
312     uint16_t vout_ov_fault_limit;      /* R/W word */
313     uint8_t vout_ov_fault_response;    /* R/W byte */
314     uint16_t vout_ov_warn_limit;       /* R/W word */
315     uint16_t vout_uv_warn_limit;       /* R/W word */
316     uint16_t vout_uv_fault_limit;      /* R/W word */
317     uint8_t vout_uv_fault_response;    /* R/W byte */
318     uint16_t iout_oc_fault_limit;      /* R/W word */
319     uint8_t iout_oc_fault_response;    /* R/W byte */
320     uint16_t iout_oc_lv_fault_limit;   /* R/W word */
321     uint8_t iout_oc_lv_fault_response; /* R/W byte */
322     uint16_t iout_oc_warn_limit;       /* R/W word */
323     uint16_t iout_uc_fault_limit;      /* R/W word */
324     uint8_t iout_uc_fault_response;    /* R/W byte */
325     uint16_t ot_fault_limit;           /* R/W word */
326     uint8_t ot_fault_response;         /* R/W byte */
327     uint16_t ot_warn_limit;            /* R/W word */
328     uint16_t ut_warn_limit;            /* R/W word */
329     uint16_t ut_fault_limit;           /* R/W word */
330     uint8_t ut_fault_response;         /* R/W byte */
331     uint16_t vin_ov_fault_limit;       /* R/W word */
332     uint8_t vin_ov_fault_response;     /* R/W byte */
333     uint16_t vin_ov_warn_limit;        /* R/W word */
334     uint16_t vin_uv_warn_limit;        /* R/W word */
335     uint16_t vin_uv_fault_limit;       /* R/W word */
336     uint8_t vin_uv_fault_response;     /* R/W byte */
337     uint16_t iin_oc_fault_limit;       /* R/W word */
338     uint8_t iin_oc_fault_response;     /* R/W byte */
339     uint16_t iin_oc_warn_limit;        /* R/W word */
340     uint16_t power_good_on;            /* R/W word */
341     uint16_t power_good_off;           /* R/W word */
342     uint16_t ton_delay;                /* R/W word */
343     uint16_t ton_rise;                 /* R/W word */
344     uint16_t ton_max_fault_limit;      /* R/W word */
345     uint8_t ton_max_fault_response;    /* R/W byte */
346     uint16_t toff_delay;               /* R/W word */
347     uint16_t toff_fall;                /* R/W word */
348     uint16_t toff_max_warn_limit;      /* R/W word */
349     uint16_t pout_op_fault_limit;      /* R/W word */
350     uint8_t pout_op_fault_response;    /* R/W byte */
351     uint16_t pout_op_warn_limit;       /* R/W word */
352     uint16_t pin_op_warn_limit;        /* R/W word */
353     uint16_t status_word;              /* R/W word */
354     uint8_t status_vout;               /* R/W byte */
355     uint8_t status_iout;               /* R/W byte */
356     uint8_t status_input;              /* R/W byte */
357     uint8_t status_temperature;        /* R/W byte */
358     uint8_t status_cml;                /* R/W byte */
359     uint8_t status_other;              /* R/W byte */
360     uint8_t status_mfr_specific;       /* R/W byte */
361     uint8_t status_fans_1_2;           /* R/W byte */
362     uint8_t status_fans_3_4;           /* R/W byte */
363     uint8_t read_ein[5];               /* Read-Only block 5 bytes */
364     uint8_t read_eout[5];              /* Read-Only block 5 bytes */
365     uint16_t read_vin;                 /* Read-Only word */
366     uint16_t read_iin;                 /* Read-Only word */
367     uint16_t read_vcap;                /* Read-Only word */
368     uint16_t read_vout;                /* Read-Only word */
369     uint16_t read_iout;                /* Read-Only word */
370     uint16_t read_temperature_1;       /* Read-Only word */
371     uint16_t read_temperature_2;       /* Read-Only word */
372     uint16_t read_temperature_3;       /* Read-Only word */
373     uint16_t read_fan_speed_1;         /* Read-Only word */
374     uint16_t read_fan_speed_2;         /* Read-Only word */
375     uint16_t read_fan_speed_3;         /* Read-Only word */
376     uint16_t read_fan_speed_4;         /* Read-Only word */
377     uint16_t read_duty_cycle;          /* Read-Only word */
378     uint16_t read_frequency;           /* Read-Only word */
379     uint16_t read_pout;                /* Read-Only word */
380     uint16_t read_pin;                 /* Read-Only word */
381     uint8_t revision;                  /* Read-Only byte */
382     const char *mfr_id;                /* R/W block */
383     const char *mfr_model;             /* R/W block */
384     const char *mfr_revision;          /* R/W block */
385     const char *mfr_location;          /* R/W block */
386     const char *mfr_date;              /* R/W block */
387     const char *mfr_serial;            /* R/W block */
388     const char *app_profile_support;   /* Read-Only block-read */
389     uint16_t mfr_vin_min;              /* Read-Only word */
390     uint16_t mfr_vin_max;              /* Read-Only word */
391     uint16_t mfr_iin_max;              /* Read-Only word */
392     uint16_t mfr_pin_max;              /* Read-Only word */
393     uint16_t mfr_vout_min;             /* Read-Only word */
394     uint16_t mfr_vout_max;             /* Read-Only word */
395     uint16_t mfr_iout_max;             /* Read-Only word */
396     uint16_t mfr_pout_max;             /* Read-Only word */
397     uint16_t mfr_tambient_max;         /* Read-Only word */
398     uint16_t mfr_tambient_min;         /* Read-Only word */
399     uint8_t mfr_efficiency_ll[14];     /* Read-Only block 14 bytes */
400     uint8_t mfr_efficiency_hl[14];     /* Read-Only block 14 bytes */
401     uint8_t mfr_pin_accuracy;          /* Read-Only byte */
402     uint16_t mfr_max_temp_1;           /* R/W word */
403     uint16_t mfr_max_temp_2;           /* R/W word */
404     uint16_t mfr_max_temp_3;           /* R/W word */
405 } PMBusPage;
406 
407 /* State */
408 struct PMBusDevice {
409     SMBusDevice smb;
410 
411     uint8_t num_pages;
412     uint8_t code;
413     uint8_t page;
414 
415     /*
416      * PMBus registers are stored in a PMBusPage structure allocated by
417      * calling pmbus_pages_alloc()
418      */
419     PMBusPage *pages;
420     uint8_t capability;
421 
422 
423     int32_t in_buf_len;
424     uint8_t *in_buf;
425     int32_t out_buf_len;
426     uint8_t out_buf[SMBUS_DATA_MAX_LEN];
427 };
428 
429 /**
430  * Direct mode coefficients
431  * @var m - mantissa
432  * @var b - offset
433  * @var R - exponent
434  */
435 typedef struct PMBusCoefficients {
436     int32_t m;     /* mantissa */
437     int64_t b;     /* offset */
438     int32_t R;     /* exponent */
439 } PMBusCoefficients;
440 
441 /**
442  * Convert sensor values to direct mode format
443  *
444  * Y = (m * x - b) * 10^R
445  *
446  * @return uint32_t
447  */
448 uint16_t pmbus_data2direct_mode(PMBusCoefficients c, uint32_t value);
449 
450 /**
451  * Convert direct mode formatted data into sensor reading
452  *
453  * X = (Y * 10^-R - b) / m
454  *
455  * @return uint32_t
456  */
457 uint32_t pmbus_direct_mode2data(PMBusCoefficients c, uint16_t value);
458 
459 /**
460  * @brief Send a block of data over PMBus
461  * Assumes that the bytes in the block are already ordered correctly,
462  * also assumes the length has been prepended to the block if necessary
463  *     | low_byte | ... | high_byte |
464  * @param state - maintains state of the PMBus device
465  * @param data - byte array to be sent by device
466  * @param len - number
467  */
468 void pmbus_send(PMBusDevice *state, const uint8_t *data, uint16_t len);
469 void pmbus_send8(PMBusDevice *state, uint8_t data);
470 void pmbus_send16(PMBusDevice *state, uint16_t data);
471 void pmbus_send32(PMBusDevice *state, uint32_t data);
472 void pmbus_send64(PMBusDevice *state, uint64_t data);
473 
474 /**
475  * @brief Send a string over PMBus with length prepended.
476  * Length is calculated using str_len()
477  */
478 void pmbus_send_string(PMBusDevice *state, const char *data);
479 
480 /**
481  * @brief Receive data over PMBus
482  * These methods help track how much data is being received over PMBus
483  * Log to GUEST_ERROR if too much or too little is sent.
484  */
485 uint8_t pmbus_receive8(PMBusDevice *pmdev);
486 uint16_t pmbus_receive16(PMBusDevice *pmdev);
487 uint32_t pmbus_receive32(PMBusDevice *pmdev);
488 uint64_t pmbus_receive64(PMBusDevice *pmdev);
489 
490 /**
491  * PMBus page config must be called before any page is first used.
492  * It will allocate memory for all the pages if needed.
493  * Passed in flags overwrite existing flags if any.
494  * @param page_index the page to which the flags are applied, setting page_index
495  * to 0xFF applies the passed in flags to all pages.
496  * @param flags
497  */
498 int pmbus_page_config(PMBusDevice *pmdev, uint8_t page_index, uint64_t flags);
499 
500 /**
501  * Update the status registers when sensor values change.
502  * Useful if modifying sensors through qmp, this way status registers get
503  * updated
504  */
505 void pmbus_check_limits(PMBusDevice *pmdev);
506 
507 extern const VMStateDescription vmstate_pmbus_device;
508 
509 #define VMSTATE_PMBUS_DEVICE(_field, _state) {                       \
510     .name       = (stringify(_field)),                               \
511     .size       = sizeof(PMBusDevice),                               \
512     .vmsd       = &vmstate_pmbus_device,                             \
513     .flags      = VMS_STRUCT,                                        \
514     .offset     = vmstate_offset_value(_state, _field, PMBusDevice), \
515 }
516 
517 #endif
518