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