aboutsummaryrefslogtreecommitdiff
path: root/hw/sensor/max31785.c
blob: 3577a7c2180294b5ba09a72dadcdb1fac055e9d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Maxim MAX31785 PMBus 6-Channel Fan Controller
 *
 * Datasheet:
 * https://datasheets.maximintegrated.com/en/ds/MAX31785.pdf
 *
 * Copyright(c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
 */

#include "qemu/osdep.h"
#include "hw/i2c/pmbus_device.h"
#include "hw/irq.h"
#include "migration/vmstate.h"
#include "qapi/error.h"
#include "qapi/visitor.h"
#include "qemu/log.h"
#include "qemu/module.h"

#define TYPE_MAX31785 "max31785"
#define MAX31785(obj) OBJECT_CHECK(MAX31785State, (obj), TYPE_MAX31785)

/* MAX31785 mfr specific PMBus commands */
#define MAX31785_MFR_MODE               0xD1
#define MAX31785_MFR_PSEN_CONFIG        0xD2
#define MAX31785_MFR_VOUT_PEAK          0xD4
#define MAX31785_MFR_TEMPERATURE_PEAK   0xD6
#define MAX31785_MFR_VOUT_MIN           0xD7
#define MAX31785_MFR_FAULT_RESPONSE     0xD9
#define MAX31785_MFR_NV_FAULT_LOG       0xDC
#define MAX31785_MFR_TIME_COUNT         0xDD
#define MAX31785_MFR_TEMP_SENSOR_CONFIG 0xF0
#define MAX31785_MFR_FAN_CONFIG         0xF1
#define MAX31785_MFR_FAN_LUT            0xF2
#define MAX31785_MFR_READ_FAN_PWM       0xF3
#define MAX31785_MFR_FAN_FAULT_LIMIT    0xF5
#define MAX31785_MFR_FAN_WARN_LIMIT     0xF6
#define MAX31785_MFR_FAN_RUN_TIME       0xF7
#define MAX31785_MFR_FAN_PWM_AVG        0xF8
#define MAX31785_MFR_FAN_PWM2RPM        0xF9

/* defaults as per the data sheet */
#define MAX31785_DEFAULT_CAPABILITY            0x10
#define MAX31785_DEFAULT_VOUT_MODE             0x40
#define MAX31785_DEFAULT_VOUT_SCALE_MONITOR    0x7FFF
#define MAX31785_DEFAULT_FAN_COMMAND_1         0x7FFF
#define MAX31785_DEFAULT_OV_FAULT_LIMIT        0x7FFF
#define MAX31785_DEFAULT_OV_WARN_LIMIT         0x7FFF
#define MAX31785_DEFAULT_OT_FAULT_LIMIT        0x7FFF
#define MAX31785_DEFAULT_OT_WARN_LIMIT         0x7FFF
#define MAX31785_DEFAULT_PMBUS_REVISION        0x11
#define MAX31785_DEFAULT_MFR_ID                0x4D
#define MAX31785_DEFAULT_MFR_MODEL             0x53
#define MAX31785_DEFAULT_MFR_REVISION          0x3030
#define MAX31785A_DEFAULT_MFR_REVISION         0x3040
#define MAX31785B_DEFAULT_MFR_REVISION         0x3061
#define MAX31785B_DEFAULT_MFR_TEMPERATURE_PEAK 0x8000
#define MAX31785B_DEFAULT_MFR_VOUT_MIN         0x7FFF
#define MAX31785_DEFAULT_TEXT                  0x3130313031303130

/* MAX31785 pages */
#define MAX31785_TOTAL_NUM_PAGES      23
#define MAX31785_FAN_PAGES            6
#define MAX31785_MIN_FAN_PAGE         0
#define MAX31785_MAX_FAN_PAGE         5
#define MAX31785_MIN_TEMP_PAGE        6
#define MAX31785_MAX_TEMP_PAGE        16
#define MAX31785_MIN_ADC_VOLTAGE_PAGE 17
#define MAX31785_MAX_ADC_VOLTAGE_PAGE 22

/* FAN_CONFIG_1_2 */
#define MAX31785_MFR_FAN_CONFIG                0xF1
#define MAX31785_FAN_CONFIG_ENABLE             BIT(7)
#define MAX31785_FAN_CONFIG_RPM_PWM            BIT(6)
#define MAX31785_FAN_CONFIG_PULSE(pulse)       (pulse << 4)
#define MAX31785_DEFAULT_FAN_CONFIG_1_2(pulse)                                 \
    (MAX31785_FAN_CONFIG_ENABLE | MAX31785_FAN_CONFIG_PULSE(pulse))
#define MAX31785_DEFAULT_MFR_FAN_CONFIG        0x0000

/* fan speed in RPM */
#define MAX31785_DEFAULT_FAN_SPEED   0x7fff
#define MAX31785_DEFAULT_FAN_STATUS  0x00

#define MAX31785_DEFAULT_FAN_MAX_PWM 0x2710

/*
 * MAX31785State:
 * @code: The command code received
 * @page: Each page corresponds to a device monitored by the Max 31785
 * The page register determines the available commands depending on device
 * _____________________________________________________________________________
 * |   0   |  Fan Connected to PWM0                                            |
 * |_______|___________________________________________________________________|
 * |   1   |  Fan Connected to PWM1                                            |
 * |_______|___________________________________________________________________|
 * |   2   |  Fan Connected to PWM2                                            |
 * |_______|___________________________________________________________________|
 * |   3   |  Fan Connected to PWM3                                            |
 * |_______|___________________________________________________________________|
 * |   4   |  Fan Connected to PWM4                                            |
 * |_______|___________________________________________________________________|
 * |   5   |  Fan Connected to PWM5                                            |
 * |_______|___________________________________________________________________|
 * |   6   |  Remote Thermal Diode Connected to ADC 0                          |
 * |_______|___________________________________________________________________|
 * |   7   |  Remote Thermal Diode Connected to ADC 1                          |
 * |_______|___________________________________________________________________|
 * |   8   |  Remote Thermal Diode Connected to ADC 2                          |
 * |_______|___________________________________________________________________|
 * |   9   |  Remote Thermal Diode Connected to ADC 3                          |
 * |_______|___________________________________________________________________|
 * |  10   |  Remote Thermal Diode Connected to ADC 4                          |
 * |_______|___________________________________________________________________|
 * |  11   |  Remote Thermal Diode Connected to ADC 5                          |
 * |_______|___________________________________________________________________|
 * |  12   |  Internal Temperature Sensor                                      |
 * |_______|___________________________________________________________________|
 * |  13   |  Remote I2C Temperature Sensor with Address 0                     |
 * |_______|___________________________________________________________________|
 * |  14   |  Remote I2C Temperature Sensor with Address 1                     |
 * |_______|___________________________________________________________________|
 * |  15   |  Remote I2C Temperature Sensor with Address 2                     |
 * |_______|___________________________________________________________________|
 * |  16   |  Remote I2C Temperature Sensor with Address 3                     |
 * |_______|___________________________________________________________________|
 * |  17   |  Remote I2C Temperature Sensor with Address 4                     |
 * |_______|___________________________________________________________________|
 * |  17   |  Remote Voltage Connected to ADC0                                 |
 * |_______|___________________________________________________________________|
 * |  18   |  Remote Voltage Connected to ADC1                                 |
 * |_______|___________________________________________________________________|
 * |  19   |  Remote Voltage Connected to ADC2                                 |
 * |_______|___________________________________________________________________|
 * |  20   |  Remote Voltage Connected to ADC3                                 |
 * |_______|___________________________________________________________________|
 * |  21   |  Remote Voltage Connected to ADC4                                 |
 * |_______|___________________________________________________________________|
 * |  22   |  Remote Voltage Connected to ADC5                                 |
 * |_______|___________________________________________________________________|
 * |23-254 |  Reserved                                                         |
 * |_______|___________________________________________________________________|
 * |  255  |  Applies to all pages                                             |
 * |_______|___________________________________________________________________|
 */

/* Place holder to save the max31785 mfr specific registers */
typedef struct MAX31785State {
    PMBusDevice parent;
    uint16_t mfr_mode[MAX31785_TOTAL_NUM_PAGES];
    uint16_t vout_peak[MAX31785_TOTAL_NUM_PAGES];
    uint16_t temperature_peak[MAX31785_TOTAL_NUM_PAGES];
    uint16_t vout_min[MAX31785_TOTAL_NUM_PAGES];
    uint8_t  fault_response[MAX31785_TOTAL_NUM_PAGES];
    uint32_t time_count[MAX31785_TOTAL_NUM_PAGES];
    uint16_t temp_sensor_config[MAX31785_TOTAL_NUM_PAGES];
    uint16_t fan_config[MAX31785_TOTAL_NUM_PAGES];
    uint16_t read_fan_pwm[MAX31785_TOTAL_NUM_PAGES];
    uint16_t fan_fault_limit[MAX31785_TOTAL_NUM_PAGES];
    uint16_t fan_warn_limit[MAX31785_TOTAL_NUM_PAGES];
    uint16_t fan_run_time[MAX31785_TOTAL_NUM_PAGES];
    uint16_t fan_pwm_avg[MAX31785_TOTAL_NUM_PAGES];
    uint64_t fan_pwm2rpm[MAX31785_TOTAL_NUM_PAGES];
    uint64_t mfr_location;
    uint64_t mfr_date;
    uint64_t mfr_serial;
    uint16_t mfr_revision;
} MAX31785State;

static uint8_t max31785_read_byte(PMBusDevice *pmdev)
{
    MAX31785State *s = MAX31785(pmdev);
    switch (pmdev->code) {

    case PMBUS_FAN_CONFIG_1_2:
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmbus_send8(pmdev, pmdev->pages[pmdev->page].fan_config_1_2);
        }
        break;

    case PMBUS_FAN_COMMAND_1:
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmbus_send16(pmdev, pmdev->pages[pmdev->page].fan_command_1);
        }
        break;

    case PMBUS_READ_FAN_SPEED_1:
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmbus_send16(pmdev, pmdev->pages[pmdev->page].read_fan_speed_1);
        }
        break;

    case PMBUS_STATUS_FANS_1_2:
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmbus_send16(pmdev, pmdev->pages[pmdev->page].status_fans_1_2);
        }
        break;

    case PMBUS_MFR_REVISION:
        pmbus_send16(pmdev, MAX31785_DEFAULT_MFR_REVISION);
        break;

    case PMBUS_MFR_ID:
        pmbus_send8(pmdev, 0x4d); /* Maxim */
        break;

    case PMBUS_MFR_MODEL:
        pmbus_send8(pmdev, 0x53);
        break;

    case PMBUS_MFR_LOCATION:
        pmbus_send64(pmdev, s->mfr_location);
        break;

    case PMBUS_MFR_DATE:
        pmbus_send64(pmdev, s->mfr_date);
        break;

    case PMBUS_MFR_SERIAL:
        pmbus_send64(pmdev, s->mfr_serial);
        break;

    case MAX31785_MFR_MODE:
        pmbus_send16(pmdev, s->mfr_mode[pmdev->page]);
        break;

    case MAX31785_MFR_VOUT_PEAK:
        if ((pmdev->page >= MAX31785_MIN_ADC_VOLTAGE_PAGE) &&
            (pmdev->page <= MAX31785_MAX_ADC_VOLTAGE_PAGE)) {
            pmbus_send16(pmdev, s->vout_peak[pmdev->page]);
        }
        break;

    case MAX31785_MFR_TEMPERATURE_PEAK:
        if ((pmdev->page >= MAX31785_MIN_TEMP_PAGE) &&
            (pmdev->page <= MAX31785_MAX_TEMP_PAGE)) {
            pmbus_send16(pmdev, s->temperature_peak[pmdev->page]);
        }
        break;

    case MAX31785_MFR_VOUT_MIN:
        if ((pmdev->page >= MAX31785_MIN_ADC_VOLTAGE_PAGE) &&
            (pmdev->page <= MAX31785_MAX_ADC_VOLTAGE_PAGE)) {
            pmbus_send16(pmdev, s->vout_min[pmdev->page]);
        }
        break;

    case MAX31785_MFR_FAULT_RESPONSE:
        pmbus_send8(pmdev, s->fault_response[pmdev->page]);
        break;

    case MAX31785_MFR_TIME_COUNT: /* R/W 32 */
        pmbus_send32(pmdev, s->time_count[pmdev->page]);
        break;

    case MAX31785_MFR_TEMP_SENSOR_CONFIG: /* R/W 16 */
        if ((pmdev->page >= MAX31785_MIN_TEMP_PAGE) &&
            (pmdev->page <= MAX31785_MAX_TEMP_PAGE)) {
            pmbus_send16(pmdev, s->temp_sensor_config[pmdev->page]);
        }
        break;

    case MAX31785_MFR_FAN_CONFIG: /* R/W 16 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmbus_send16(pmdev, s->fan_config[pmdev->page]);
        }
        break;

    case MAX31785_MFR_READ_FAN_PWM: /* R/W 16 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmbus_send16(pmdev, s->read_fan_pwm[pmdev->page]);
        }
        break;

    case MAX31785_MFR_FAN_FAULT_LIMIT: /* R/W 16 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmbus_send16(pmdev, s->fan_fault_limit[pmdev->page]);
        }
        break;

    case MAX31785_MFR_FAN_WARN_LIMIT: /* R/W 16 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmbus_send16(pmdev, s->fan_warn_limit[pmdev->page]);
        }
        break;

    case MAX31785_MFR_FAN_RUN_TIME: /* R/W 16 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmbus_send16(pmdev, s->fan_run_time[pmdev->page]);
        }
        break;

    case MAX31785_MFR_FAN_PWM_AVG: /* R/W 16 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmbus_send16(pmdev, s->fan_pwm_avg[pmdev->page]);
        }
        break;

    case MAX31785_MFR_FAN_PWM2RPM: /* R/W 64 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmbus_send64(pmdev, s->fan_pwm2rpm[pmdev->page]);
        }
        break;

    default:
        qemu_log_mask(LOG_GUEST_ERROR,
        "%s: reading from unsupported register: 0x%02x\n",
        __func__, pmdev->code);
        break;
    }

    return 0xFF;
}

static int max31785_write_data(PMBusDevice *pmdev, const uint8_t *buf,
                               uint8_t len)
{
    MAX31785State *s = MAX31785(pmdev);
    if (len == 0) {
        qemu_log_mask(LOG_GUEST_ERROR, "%s: writing empty data\n", __func__);
        return -1;
    }

    pmdev->code = buf[0]; /* PMBus command code */

    if (len == 1) {
        return 0;
    }

    /* Exclude command code from buffer */
    buf++;
    len--;

    switch (pmdev->code) {

    case PMBUS_FAN_CONFIG_1_2:
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmdev->pages[pmdev->page].fan_config_1_2 = pmbus_receive8(pmdev);
        }
        break;

    case PMBUS_FAN_COMMAND_1:
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            pmdev->pages[pmdev->page].fan_command_1 = pmbus_receive16(pmdev);
            pmdev->pages[pmdev->page].read_fan_speed_1 =
                ((MAX31785_DEFAULT_FAN_SPEED / MAX31785_DEFAULT_FAN_MAX_PWM) *
                pmdev->pages[pmdev->page].fan_command_1);
        }
        break;

    case PMBUS_MFR_LOCATION: /* R/W 64 */
        s->mfr_location = pmbus_receive64(pmdev);
        break;

    case PMBUS_MFR_DATE: /* R/W 64 */
        s->mfr_date = pmbus_receive64(pmdev);
        break;

    case PMBUS_MFR_SERIAL: /* R/W 64 */
        s->mfr_serial = pmbus_receive64(pmdev);
        break;

    case MAX31785_MFR_MODE: /* R/W word */
        s->mfr_mode[pmdev->page] = pmbus_receive16(pmdev);
        break;

    case MAX31785_MFR_VOUT_PEAK: /* R/W word */
        if ((pmdev->page >= MAX31785_MIN_ADC_VOLTAGE_PAGE) &&
            (pmdev->page <= MAX31785_MAX_ADC_VOLTAGE_PAGE)) {
            s->vout_peak[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case MAX31785_MFR_TEMPERATURE_PEAK: /* R/W word */
        if ((pmdev->page >= 6) && (pmdev->page <= 16)) {
            s->temperature_peak[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case MAX31785_MFR_VOUT_MIN: /* R/W word */
        if ((pmdev->page >= MAX31785_MIN_ADC_VOLTAGE_PAGE) &&
            (pmdev->page <= MAX31785_MAX_ADC_VOLTAGE_PAGE)) {
            s->vout_min[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case MAX31785_MFR_FAULT_RESPONSE: /* R/W 8 */
        s->fault_response[pmdev->page] = pmbus_receive8(pmdev);
        break;

    case MAX31785_MFR_TIME_COUNT: /* R/W 32 */
        s->time_count[pmdev->page] = pmbus_receive32(pmdev);
        break;

    case MAX31785_MFR_TEMP_SENSOR_CONFIG: /* R/W 16 */
        if ((pmdev->page >= MAX31785_MIN_TEMP_PAGE) &&
            (pmdev->page <= MAX31785_MAX_TEMP_PAGE)) {
            s->temp_sensor_config[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case MAX31785_MFR_FAN_CONFIG: /* R/W 16 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            s->fan_config[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case MAX31785_MFR_FAN_FAULT_LIMIT: /* R/W 16 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            s->fan_fault_limit[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case MAX31785_MFR_FAN_WARN_LIMIT: /* R/W 16 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            s->fan_warn_limit[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case MAX31785_MFR_FAN_RUN_TIME: /* R/W 16 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            s->fan_run_time[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case MAX31785_MFR_FAN_PWM_AVG: /* R/W 16 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            s->fan_pwm_avg[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case MAX31785_MFR_FAN_PWM2RPM: /* R/W 64 */
        if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
            s->fan_pwm2rpm[pmdev->page] = pmbus_receive64(pmdev);
        }
        break;

    default:
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: writing to unsupported register: 0x%02x\n",
                      __func__, pmdev->code);
        break;
    }

    return 0;
}

static void max31785_exit_reset(Object *obj, ResetType type)
{
    PMBusDevice *pmdev = PMBUS_DEVICE(obj);
    MAX31785State *s = MAX31785(obj);

    pmdev->capability = MAX31785_DEFAULT_CAPABILITY;

    for (int i = MAX31785_MIN_FAN_PAGE; i <= MAX31785_MAX_FAN_PAGE; i++) {
        pmdev->pages[i].vout_mode = MAX31785_DEFAULT_VOUT_MODE;
        pmdev->pages[i].fan_command_1 = MAX31785_DEFAULT_FAN_COMMAND_1;
        pmdev->pages[i].revision = MAX31785_DEFAULT_PMBUS_REVISION;
        pmdev->pages[i].fan_config_1_2 = MAX31785_DEFAULT_FAN_CONFIG_1_2(0);
        pmdev->pages[i].read_fan_speed_1 = MAX31785_DEFAULT_FAN_SPEED;
        pmdev->pages[i].status_fans_1_2 = MAX31785_DEFAULT_FAN_STATUS;
    }

    for (int i = MAX31785_MIN_TEMP_PAGE; i <= MAX31785_MAX_TEMP_PAGE; i++) {
        pmdev->pages[i].vout_mode = MAX31785_DEFAULT_VOUT_MODE;
        pmdev->pages[i].revision = MAX31785_DEFAULT_PMBUS_REVISION;
        pmdev->pages[i].ot_fault_limit = MAX31785_DEFAULT_OT_FAULT_LIMIT;
        pmdev->pages[i].ot_warn_limit = MAX31785_DEFAULT_OT_WARN_LIMIT;
    }

    for (int i = MAX31785_MIN_ADC_VOLTAGE_PAGE;
         i <= MAX31785_MAX_ADC_VOLTAGE_PAGE;
         i++) {
        pmdev->pages[i].vout_mode = MAX31785_DEFAULT_VOUT_MODE;
        pmdev->pages[i].revision = MAX31785_DEFAULT_PMBUS_REVISION;
        pmdev->pages[i].vout_scale_monitor =
            MAX31785_DEFAULT_VOUT_SCALE_MONITOR;
        pmdev->pages[i].vout_ov_fault_limit = MAX31785_DEFAULT_OV_FAULT_LIMIT;
        pmdev->pages[i].vout_ov_warn_limit = MAX31785_DEFAULT_OV_WARN_LIMIT;
    }

    s->mfr_location = MAX31785_DEFAULT_TEXT;
    s->mfr_date = MAX31785_DEFAULT_TEXT;
    s->mfr_serial = MAX31785_DEFAULT_TEXT;
}

static const VMStateDescription vmstate_max31785 = {
    .name = TYPE_MAX31785,
    .version_id = 0,
    .minimum_version_id = 0,
    .fields = (const VMStateField[]){
        VMSTATE_PMBUS_DEVICE(parent, MAX31785State),
        VMSTATE_UINT16_ARRAY(mfr_mode, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT16_ARRAY(vout_peak, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT16_ARRAY(temperature_peak, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT16_ARRAY(vout_min, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT8_ARRAY(fault_response, MAX31785State,
                            MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT32_ARRAY(time_count, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT16_ARRAY(temp_sensor_config, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT16_ARRAY(fan_config, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT16_ARRAY(read_fan_pwm, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT16_ARRAY(fan_fault_limit, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT16_ARRAY(fan_warn_limit, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT16_ARRAY(fan_run_time, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT16_ARRAY(fan_pwm_avg, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT64_ARRAY(fan_pwm2rpm, MAX31785State,
                             MAX31785_TOTAL_NUM_PAGES),
        VMSTATE_UINT64(mfr_location, MAX31785State),
        VMSTATE_UINT64(mfr_date, MAX31785State),
        VMSTATE_UINT64(mfr_serial, MAX31785State),
        VMSTATE_END_OF_LIST()
    }
};

static void max31785_init(Object *obj)
{
    PMBusDevice *pmdev = PMBUS_DEVICE(obj);

    for (int i = MAX31785_MIN_FAN_PAGE; i <= MAX31785_MAX_FAN_PAGE; i++) {
        pmbus_page_config(pmdev, i, PB_HAS_VOUT_MODE);
    }

    for (int i = MAX31785_MIN_TEMP_PAGE; i <= MAX31785_MAX_TEMP_PAGE; i++) {
        pmbus_page_config(pmdev, i, PB_HAS_VOUT_MODE | PB_HAS_TEMPERATURE);
    }

    for (int i = MAX31785_MIN_ADC_VOLTAGE_PAGE;
        i <= MAX31785_MAX_ADC_VOLTAGE_PAGE;
        i++) {
        pmbus_page_config(pmdev, i, PB_HAS_VOUT_MODE | PB_HAS_VOUT |
                                    PB_HAS_VOUT_RATING);
    }
}

static void max31785_class_init(ObjectClass *klass, void *data)
{
    ResettableClass *rc = RESETTABLE_CLASS(klass);
    DeviceClass *dc = DEVICE_CLASS(klass);
    PMBusDeviceClass *k = PMBUS_DEVICE_CLASS(klass);
    dc->desc = "Maxim MAX31785 6-Channel Fan Controller";
    dc->vmsd = &vmstate_max31785;
    k->write_data = max31785_write_data;
    k->receive_byte = max31785_read_byte;
    k->device_num_pages = MAX31785_TOTAL_NUM_PAGES;
    rc->phases.exit = max31785_exit_reset;
}

static const TypeInfo max31785_info = {
    .name = TYPE_MAX31785,
    .parent = TYPE_PMBUS_DEVICE,
    .instance_size = sizeof(MAX31785State),
    .instance_init = max31785_init,
    .class_init = max31785_class_init,
};

static void max31785_register_types(void)
{
    type_register_static(&max31785_info);
}

type_init(max31785_register_types)