xref: /openbmc/qemu/include/hw/i2c/pmbus_device.h (revision 6832189f)
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     PMBUS_IDLE_STATE                = 0xFF,
159 };
160 
161 /* STATUS_WORD */
162 #define PB_STATUS_VOUT           BIT(15)
163 #define PB_STATUS_IOUT_POUT      BIT(14)
164 #define PB_STATUS_INPUT          BIT(13)
165 #define PB_STATUS_WORD_MFR       BIT(12)
166 #define PB_STATUS_POWER_GOOD_N   BIT(11)
167 #define PB_STATUS_FAN            BIT(10)
168 #define PB_STATUS_OTHER          BIT(9)
169 #define PB_STATUS_UNKNOWN        BIT(8)
170 /* STATUS_BYTE */
171 #define PB_STATUS_BUSY           BIT(7)
172 #define PB_STATUS_OFF            BIT(6)
173 #define PB_STATUS_VOUT_OV        BIT(5)
174 #define PB_STATUS_IOUT_OC        BIT(4)
175 #define PB_STATUS_VIN_UV         BIT(3)
176 #define PB_STATUS_TEMPERATURE    BIT(2)
177 #define PB_STATUS_CML            BIT(1)
178 #define PB_STATUS_NONE_ABOVE     BIT(0)
179 
180 /* STATUS_VOUT */
181 #define PB_STATUS_VOUT_OV_FAULT         BIT(7) /* Output Overvoltage Fault */
182 #define PB_STATUS_VOUT_OV_WARN          BIT(6) /* Output Overvoltage Warning */
183 #define PB_STATUS_VOUT_UV_WARN          BIT(5) /* Output Undervoltage Warning */
184 #define PB_STATUS_VOUT_UV_FAULT         BIT(4) /* Output Undervoltage Fault */
185 #define PB_STATUS_VOUT_MAX              BIT(3)
186 #define PB_STATUS_VOUT_TON_MAX_FAULT    BIT(2)
187 #define PB_STATUS_VOUT_TOFF_MAX_WARN    BIT(1)
188 
189 /* STATUS_IOUT */
190 #define PB_STATUS_IOUT_OC_FAULT    BIT(7) /* Output Overcurrent Fault */
191 #define PB_STATUS_IOUT_OC_LV_FAULT BIT(6) /* Output OC And Low Voltage Fault */
192 #define PB_STATUS_IOUT_OC_WARN     BIT(5) /* Output Overcurrent Warning */
193 #define PB_STATUS_IOUT_UC_FAULT    BIT(4) /* Output Undercurrent Fault */
194 #define PB_STATUS_CURR_SHARE       BIT(3) /* Current Share Fault */
195 #define PB_STATUS_PWR_LIM_MODE     BIT(2) /* In Power Limiting Mode */
196 #define PB_STATUS_POUT_OP_FAULT    BIT(1) /* Output Overpower Fault */
197 #define PB_STATUS_POUT_OP_WARN     BIT(0) /* Output Overpower Warning */
198 
199 /* STATUS_INPUT */
200 #define PB_STATUS_INPUT_VIN_OV_FAULT    BIT(7) /* Input Overvoltage Fault */
201 #define PB_STATUS_INPUT_VIN_OV_WARN     BIT(6) /* Input Overvoltage Warning */
202 #define PB_STATUS_INPUT_VIN_UV_WARN     BIT(5) /* Input Undervoltage Warning */
203 #define PB_STATUS_INPUT_VIN_UV_FAULT    BIT(4) /* Input Undervoltage Fault */
204 #define PB_STATUS_INPUT_IIN_OC_FAULT    BIT(2) /* Input Overcurrent Fault */
205 #define PB_STATUS_INPUT_IIN_OC_WARN     BIT(1) /* Input Overcurrent Warning */
206 #define PB_STATUS_INPUT_PIN_OP_WARN     BIT(0) /* Input Overpower Warning */
207 
208 /* STATUS_TEMPERATURE */
209 #define PB_STATUS_OT_FAULT              BIT(7) /* Overtemperature Fault */
210 #define PB_STATUS_OT_WARN               BIT(6) /* Overtemperature Warning */
211 #define PB_STATUS_UT_WARN               BIT(5) /* Undertemperature Warning */
212 #define PB_STATUS_UT_FAULT              BIT(4) /* Undertemperature Fault */
213 
214 /* STATUS_CML */
215 #define PB_CML_FAULT_INVALID_CMD     BIT(7) /* Invalid/Unsupported Command */
216 #define PB_CML_FAULT_INVALID_DATA    BIT(6) /* Invalid/Unsupported Data  */
217 #define PB_CML_FAULT_PEC             BIT(5) /* Packet Error Check Failed */
218 #define PB_CML_FAULT_MEMORY          BIT(4) /* Memory Fault Detected */
219 #define PB_CML_FAULT_PROCESSOR       BIT(3) /* Processor Fault Detected */
220 #define PB_CML_FAULT_OTHER_COMM      BIT(1) /* Other communication fault */
221 #define PB_CML_FAULT_OTHER_MEM_LOGIC BIT(0) /* Other Memory Or Logic Fault */
222 
223 /* OPERATION*/
224 #define PB_OP_ON                BIT(7) /* PSU is switched on */
225 #define PB_OP_MARGIN_HIGH       BIT(5) /* PSU vout is set to margin high */
226 #define PB_OP_MARGIN_LOW        BIT(4) /* PSU vout is set to margin low */
227 
228 /* PAGES */
229 #define PB_MAX_PAGES            0x1F
230 #define PB_ALL_PAGES            0xFF
231 
232 #define PMBUS_ERR_BYTE          0xFF
233 
234 #define TYPE_PMBUS_DEVICE "pmbus-device"
235 OBJECT_DECLARE_TYPE(PMBusDevice, PMBusDeviceClass,
236                     PMBUS_DEVICE)
237 
238 /* flags */
239 #define PB_HAS_COEFFICIENTS        BIT_ULL(9)
240 #define PB_HAS_VIN                 BIT_ULL(10)
241 #define PB_HAS_VOUT                BIT_ULL(11)
242 #define PB_HAS_VOUT_MARGIN         BIT_ULL(12)
243 #define PB_HAS_VIN_RATING          BIT_ULL(13)
244 #define PB_HAS_VOUT_RATING         BIT_ULL(14)
245 #define PB_HAS_VOUT_MODE           BIT_ULL(15)
246 #define PB_HAS_IOUT                BIT_ULL(21)
247 #define PB_HAS_IIN                 BIT_ULL(22)
248 #define PB_HAS_IOUT_RATING         BIT_ULL(23)
249 #define PB_HAS_IIN_RATING          BIT_ULL(24)
250 #define PB_HAS_IOUT_GAIN           BIT_ULL(25)
251 #define PB_HAS_POUT                BIT_ULL(30)
252 #define PB_HAS_PIN                 BIT_ULL(31)
253 #define PB_HAS_EIN                 BIT_ULL(32)
254 #define PB_HAS_EOUT                BIT_ULL(33)
255 #define PB_HAS_POUT_RATING         BIT_ULL(34)
256 #define PB_HAS_PIN_RATING          BIT_ULL(35)
257 #define PB_HAS_TEMPERATURE         BIT_ULL(40)
258 #define PB_HAS_TEMP2               BIT_ULL(41)
259 #define PB_HAS_TEMP3               BIT_ULL(42)
260 #define PB_HAS_TEMP_RATING         BIT_ULL(43)
261 #define PB_HAS_MFR_INFO            BIT_ULL(50)
262 #define PB_HAS_STATUS_MFR_SPECIFIC BIT_ULL(51)
263 
264 struct PMBusDeviceClass {
265     SMBusDeviceClass parent_class;
266     uint8_t device_num_pages;
267 
268     /**
269      * Implement quick_cmd, receive byte, and write_data to support non-standard
270      * PMBus functionality
271      */
272     void (*quick_cmd)(PMBusDevice *dev, uint8_t read);
273     int (*write_data)(PMBusDevice *dev, const uint8_t *buf, uint8_t len);
274     uint8_t (*receive_byte)(PMBusDevice *dev);
275 };
276 
277 /*
278  * According to the spec, each page may offer the full range of PMBus commands
279  * available for each output or non-PMBus device.
280  * Therefore, we can't assume that any registers will always be the same across
281  * all pages.
282  * The page 0xFF is intended for writes to all pages
283  */
284 typedef struct PMBusPage {
285     uint64_t page_flags;
286 
287     uint8_t page;                      /* R/W byte */
288     uint8_t operation;                 /* R/W byte */
289     uint8_t on_off_config;             /* R/W byte */
290     uint8_t write_protect;             /* R/W byte */
291     uint8_t phase;                     /* R/W byte */
292     uint8_t vout_mode;                 /* R/W byte */
293     uint16_t vout_command;             /* R/W word */
294     uint16_t vout_trim;                /* R/W word */
295     uint16_t vout_cal_offset;          /* R/W word */
296     uint16_t vout_max;                 /* R/W word */
297     uint16_t vout_margin_high;         /* R/W word */
298     uint16_t vout_margin_low;          /* R/W word */
299     uint16_t vout_transition_rate;     /* R/W word */
300     uint16_t vout_droop;               /* R/W word */
301     uint16_t vout_scale_loop;          /* R/W word */
302     uint16_t vout_scale_monitor;       /* R/W word */
303     uint16_t vout_min;                 /* R/W word */
304     uint8_t coefficients[5];           /* Read-only block 5 bytes */
305     uint16_t pout_max;                 /* R/W word */
306     uint16_t max_duty;                 /* R/W word */
307     uint16_t frequency_switch;         /* R/W word */
308     uint16_t vin_on;                   /* R/W word */
309     uint16_t vin_off;                  /* R/W word */
310     uint16_t iout_cal_gain;            /* R/W word */
311     uint16_t iout_cal_offset;          /* R/W word */
312     uint8_t fan_config_1_2;            /* R/W byte */
313     uint16_t fan_command_1;            /* R/W word */
314     uint16_t fan_command_2;            /* R/W word */
315     uint8_t fan_config_3_4;            /* R/W byte */
316     uint16_t fan_command_3;            /* R/W word */
317     uint16_t fan_command_4;            /* R/W word */
318     uint16_t vout_ov_fault_limit;      /* R/W word */
319     uint8_t vout_ov_fault_response;    /* R/W byte */
320     uint16_t vout_ov_warn_limit;       /* R/W word */
321     uint16_t vout_uv_warn_limit;       /* R/W word */
322     uint16_t vout_uv_fault_limit;      /* R/W word */
323     uint8_t vout_uv_fault_response;    /* R/W byte */
324     uint16_t iout_oc_fault_limit;      /* R/W word */
325     uint8_t iout_oc_fault_response;    /* R/W byte */
326     uint16_t iout_oc_lv_fault_limit;   /* R/W word */
327     uint8_t iout_oc_lv_fault_response; /* R/W byte */
328     uint16_t iout_oc_warn_limit;       /* R/W word */
329     uint16_t iout_uc_fault_limit;      /* R/W word */
330     uint8_t iout_uc_fault_response;    /* R/W byte */
331     uint16_t ot_fault_limit;           /* R/W word */
332     uint8_t ot_fault_response;         /* R/W byte */
333     uint16_t ot_warn_limit;            /* R/W word */
334     uint16_t ut_warn_limit;            /* R/W word */
335     uint16_t ut_fault_limit;           /* R/W word */
336     uint8_t ut_fault_response;         /* R/W byte */
337     uint16_t vin_ov_fault_limit;       /* R/W word */
338     uint8_t vin_ov_fault_response;     /* R/W byte */
339     uint16_t vin_ov_warn_limit;        /* R/W word */
340     uint16_t vin_uv_warn_limit;        /* R/W word */
341     uint16_t vin_uv_fault_limit;       /* R/W word */
342     uint8_t vin_uv_fault_response;     /* R/W byte */
343     uint16_t iin_oc_fault_limit;       /* R/W word */
344     uint8_t iin_oc_fault_response;     /* R/W byte */
345     uint16_t iin_oc_warn_limit;        /* R/W word */
346     uint16_t power_good_on;            /* R/W word */
347     uint16_t power_good_off;           /* R/W word */
348     uint16_t ton_delay;                /* R/W word */
349     uint16_t ton_rise;                 /* R/W word */
350     uint16_t ton_max_fault_limit;      /* R/W word */
351     uint8_t ton_max_fault_response;    /* R/W byte */
352     uint16_t toff_delay;               /* R/W word */
353     uint16_t toff_fall;                /* R/W word */
354     uint16_t toff_max_warn_limit;      /* R/W word */
355     uint16_t pout_op_fault_limit;      /* R/W word */
356     uint8_t pout_op_fault_response;    /* R/W byte */
357     uint16_t pout_op_warn_limit;       /* R/W word */
358     uint16_t pin_op_warn_limit;        /* R/W word */
359     uint16_t status_word;              /* R/W word */
360     uint8_t status_vout;               /* R/W byte */
361     uint8_t status_iout;               /* R/W byte */
362     uint8_t status_input;              /* R/W byte */
363     uint8_t status_temperature;        /* R/W byte */
364     uint8_t status_cml;                /* R/W byte */
365     uint8_t status_other;              /* R/W byte */
366     uint8_t status_mfr_specific;       /* R/W byte */
367     uint8_t status_fans_1_2;           /* R/W byte */
368     uint8_t status_fans_3_4;           /* R/W byte */
369     uint8_t read_ein[5];               /* Read-Only block 5 bytes */
370     uint8_t read_eout[5];              /* Read-Only block 5 bytes */
371     uint16_t read_vin;                 /* Read-Only word */
372     uint16_t read_iin;                 /* Read-Only word */
373     uint16_t read_vcap;                /* Read-Only word */
374     uint16_t read_vout;                /* Read-Only word */
375     uint16_t read_iout;                /* Read-Only word */
376     uint16_t read_temperature_1;       /* Read-Only word */
377     uint16_t read_temperature_2;       /* Read-Only word */
378     uint16_t read_temperature_3;       /* Read-Only word */
379     uint16_t read_fan_speed_1;         /* Read-Only word */
380     uint16_t read_fan_speed_2;         /* Read-Only word */
381     uint16_t read_fan_speed_3;         /* Read-Only word */
382     uint16_t read_fan_speed_4;         /* Read-Only word */
383     uint16_t read_duty_cycle;          /* Read-Only word */
384     uint16_t read_frequency;           /* Read-Only word */
385     uint16_t read_pout;                /* Read-Only word */
386     uint16_t read_pin;                 /* Read-Only word */
387     uint8_t revision;                  /* Read-Only byte */
388     const char *mfr_id;                /* R/W block */
389     const char *mfr_model;             /* R/W block */
390     const char *mfr_revision;          /* R/W block */
391     const char *mfr_location;          /* R/W block */
392     const char *mfr_date;              /* R/W block */
393     const char *mfr_serial;            /* R/W block */
394     const char *app_profile_support;   /* Read-Only block-read */
395     uint16_t mfr_vin_min;              /* Read-Only word */
396     uint16_t mfr_vin_max;              /* Read-Only word */
397     uint16_t mfr_iin_max;              /* Read-Only word */
398     uint16_t mfr_pin_max;              /* Read-Only word */
399     uint16_t mfr_vout_min;             /* Read-Only word */
400     uint16_t mfr_vout_max;             /* Read-Only word */
401     uint16_t mfr_iout_max;             /* Read-Only word */
402     uint16_t mfr_pout_max;             /* Read-Only word */
403     uint16_t mfr_tambient_max;         /* Read-Only word */
404     uint16_t mfr_tambient_min;         /* Read-Only word */
405     uint8_t mfr_efficiency_ll[14];     /* Read-Only block 14 bytes */
406     uint8_t mfr_efficiency_hl[14];     /* Read-Only block 14 bytes */
407     uint8_t mfr_pin_accuracy;          /* Read-Only byte */
408     uint16_t mfr_max_temp_1;           /* R/W word */
409     uint16_t mfr_max_temp_2;           /* R/W word */
410     uint16_t mfr_max_temp_3;           /* R/W word */
411 } PMBusPage;
412 
413 /* State */
414 struct PMBusDevice {
415     SMBusDevice smb;
416 
417     uint8_t num_pages;
418     uint8_t code;
419     uint8_t page;
420 
421     /*
422      * PMBus registers are stored in a PMBusPage structure allocated by
423      * calling pmbus_pages_alloc()
424      */
425     PMBusPage *pages;
426     uint8_t capability;
427 
428 
429     int32_t in_buf_len;
430     uint8_t *in_buf;
431     int32_t out_buf_len;
432     uint8_t out_buf[SMBUS_DATA_MAX_LEN];
433 };
434 
435 /**
436  * Direct mode coefficients
437  * @var m - mantissa
438  * @var b - offset
439  * @var R - exponent
440  */
441 typedef struct PMBusCoefficients {
442     int32_t m;     /* mantissa */
443     int64_t b;     /* offset */
444     int32_t R;     /* exponent */
445 } PMBusCoefficients;
446 
447 /**
448  * Convert sensor values to direct mode format
449  *
450  * Y = (m * x - b) * 10^R
451  *
452  * @return uint16_t
453  */
454 uint16_t pmbus_data2direct_mode(PMBusCoefficients c, uint32_t value);
455 
456 /**
457  * Convert direct mode formatted data into sensor reading
458  *
459  * X = (Y * 10^-R - b) / m
460  *
461  * @return uint32_t
462  */
463 uint32_t pmbus_direct_mode2data(PMBusCoefficients c, uint16_t value);
464 
465 /**
466  * Convert sensor values to linear mode format
467  *
468  * L = D * 2^(-e)
469  *
470  * @return uint16
471  */
472 uint16_t pmbus_data2linear_mode(uint16_t value, int exp);
473 
474 /**
475  * Convert linear mode formatted data into sensor reading
476  *
477  * D = L * 2^e
478  *
479  * @return uint16
480  */
481 uint16_t pmbus_linear_mode2data(uint16_t value, int exp);
482 
483 /**
484  * @brief Send a block of data over PMBus
485  * Assumes that the bytes in the block are already ordered correctly,
486  * also assumes the length has been prepended to the block if necessary
487  *     | low_byte | ... | high_byte |
488  * @param state - maintains state of the PMBus device
489  * @param data - byte array to be sent by device
490  * @param len - number
491  */
492 void pmbus_send(PMBusDevice *state, const uint8_t *data, uint16_t len);
493 void pmbus_send8(PMBusDevice *state, uint8_t data);
494 void pmbus_send16(PMBusDevice *state, uint16_t data);
495 void pmbus_send32(PMBusDevice *state, uint32_t data);
496 void pmbus_send64(PMBusDevice *state, uint64_t data);
497 
498 /**
499  * @brief Send a string over PMBus with length prepended.
500  * Length is calculated using str_len()
501  */
502 void pmbus_send_string(PMBusDevice *state, const char *data);
503 
504 /**
505  * @brief Receive data over PMBus
506  * These methods help track how much data is being received over PMBus
507  * Log to GUEST_ERROR if too much or too little is sent.
508  */
509 uint8_t pmbus_receive8(PMBusDevice *pmdev);
510 uint16_t pmbus_receive16(PMBusDevice *pmdev);
511 uint32_t pmbus_receive32(PMBusDevice *pmdev);
512 uint64_t pmbus_receive64(PMBusDevice *pmdev);
513 
514 /**
515  * PMBus page config must be called before any page is first used.
516  * It will allocate memory for all the pages if needed.
517  * Passed in flags overwrite existing flags if any.
518  * @param page_index the page to which the flags are applied, setting page_index
519  * to 0xFF applies the passed in flags to all pages.
520  * @param flags
521  */
522 int pmbus_page_config(PMBusDevice *pmdev, uint8_t page_index, uint64_t flags);
523 
524 /**
525  * Update the status registers when sensor values change.
526  * Useful if modifying sensors through qmp, this way status registers get
527  * updated
528  */
529 void pmbus_check_limits(PMBusDevice *pmdev);
530 
531 /**
532  * Enter an idle state where only the PMBUS_ERR_BYTE will be returned
533  * indefinitely until a new command is issued.
534  */
535 void pmbus_idle(PMBusDevice *pmdev);
536 
537 extern const VMStateDescription vmstate_pmbus_device;
538 
539 #define VMSTATE_PMBUS_DEVICE(_field, _state) {                       \
540     .name       = (stringify(_field)),                               \
541     .size       = sizeof(PMBusDevice),                               \
542     .vmsd       = &vmstate_pmbus_device,                             \
543     .flags      = VMS_STRUCT,                                        \
544     .offset     = vmstate_offset_value(_state, _field, PMBusDevice), \
545 }
546 
547 #endif
548