aboutsummaryrefslogtreecommitdiff
path: root/hw/sensor/max34451.c
blob: a91d8bd487c988d3c47586abcde0411f89142a0d (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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/*
 * Maxim MAX34451 PMBus 16-Channel V/I monitor and 12-Channel Sequencer/Marginer
 *
 * Copyright 2021 Google LLC
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#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_MAX34451 "max34451"
#define MAX34451(obj) OBJECT_CHECK(MAX34451State, (obj), TYPE_MAX34451)

#define MAX34451_MFR_MODE               0xD1
#define MAX34451_MFR_PSEN_CONFIG        0xD2
#define MAX34451_MFR_VOUT_PEAK          0xD4
#define MAX34451_MFR_IOUT_PEAK          0xD5
#define MAX34451_MFR_TEMPERATURE_PEAK   0xD6
#define MAX34451_MFR_VOUT_MIN           0xD7
#define MAX34451_MFR_NV_LOG_CONFIG      0xD8
#define MAX34451_MFR_FAULT_RESPONSE     0xD9
#define MAX34451_MFR_FAULT_RETRY        0xDA
#define MAX34451_MFR_NV_FAULT_LOG       0xDC
#define MAX34451_MFR_TIME_COUNT         0xDD
#define MAX34451_MFR_MARGIN_CONFIG      0xDF
#define MAX34451_MFR_FW_SERIAL          0xE0
#define MAX34451_MFR_IOUT_AVG           0xE2
#define MAX34451_MFR_CHANNEL_CONFIG     0xE4
#define MAX34451_MFR_TON_SEQ_MAX        0xE6
#define MAX34451_MFR_PWM_CONFIG         0xE7
#define MAX34451_MFR_SEQ_CONFIG         0xE8
#define MAX34451_MFR_STORE_ALL          0xEE
#define MAX34451_MFR_RESTORE_ALL        0xEF
#define MAX34451_MFR_TEMP_SENSOR_CONFIG 0xF0
#define MAX34451_MFR_STORE_SINGLE       0xFC
#define MAX34451_MFR_CRC                0xFE

#define MAX34451_NUM_MARGINED_PSU       12
#define MAX34451_NUM_PWR_DEVICES        16
#define MAX34451_NUM_TEMP_DEVICES       5
#define MAX34451_NUM_PAGES              21

#define DEFAULT_OP_ON                   0x80
#define DEFAULT_CAPABILITY              0x20
#define DEFAULT_ON_OFF_CONFIG           0x1a
#define DEFAULT_VOUT_MODE               0x40
#define DEFAULT_TEMPERATURE             2500
#define DEFAULT_SCALE                   0x7FFF
#define DEFAULT_OV_LIMIT                0x7FFF
#define DEFAULT_OC_LIMIT                0x7FFF
#define DEFAULT_OT_LIMIT                0x7FFF
#define DEFAULT_VMIN                    0x7FFF
#define DEFAULT_TON_FAULT_LIMIT         0xFFFF
#define DEFAULT_CHANNEL_CONFIG          0x20
#define DEFAULT_TEXT                    0x3130313031303130

/**
 * MAX34451State:
 * @code: The command code received
 * @page: Each page corresponds to a device monitored by the Max 34451
 * The page register determines the available commands depending on device
  ___________________________________________________________________________
 |   0   |  Power supply monitored by RS0, controlled by PSEN0, and          |
 |       |  margined with PWM0.                                              |
 |_______|___________________________________________________________________|
 |   1   |  Power supply monitored by RS1, controlled by PSEN1, and          |
 |       |  margined with PWM1.                                              |
 |_______|___________________________________________________________________|
 |   2   |  Power supply monitored by RS2, controlled by PSEN2, and          |
 |       |  margined with PWM2.                                              |
 |_______|___________________________________________________________________|
 |   3   |  Power supply monitored by RS3, controlled by PSEN3, and          |
 |       |  margined with PWM3.                                              |
 |_______|___________________________________________________________________|
 |   4   |  Power supply monitored by RS4, controlled by PSEN4, and          |
 |       |  margined with PWM4.                                              |
 |_______|___________________________________________________________________|
 |   5   |  Power supply monitored by RS5, controlled by PSEN5, and          |
 |       |  margined with PWM5.                                              |
 |_______|___________________________________________________________________|
 |   6   |  Power supply monitored by RS6, controlled by PSEN6, and          |
 |       |  margined with PWM6.                                              |
 |_______|___________________________________________________________________|
 |   7   |  Power supply monitored by RS7, controlled by PSEN7, and          |
 |       |  margined with PWM7.                                              |
 |_______|___________________________________________________________________|
 |   8   |  Power supply monitored by RS8, controlled by PSEN8, and          |
 |       | optionally margined by OUT0 of external DS4424 at I2C address A0h.|
 |_______|___________________________________________________________________|
 |   9   |  Power supply monitored by RS9, controlled by PSEN9, and          |
 |       | optionally margined by OUT1 of external DS4424 at I2C address A0h.|
 |_______|___________________________________________________________________|
 |   10  |  Power supply monitored by RS10, controlled by PSEN10, and        |
 |       | optionally margined by OUT2 of external DS4424 at I2C address A0h.|
 |_______|___________________________________________________________________|
 |   11  |  Power supply monitored by RS11, controlled by PSEN11, and        |
 |       | optionally margined by OUT3 of external DS4424 at I2C address A0h.|
 |_______|___________________________________________________________________|
 |   12  |  ADC channel 12 (monitors voltage or current) or GPI.             |
 |_______|___________________________________________________________________|
 |   13  |  ADC channel 13 (monitors voltage or current) or GPI.             |
 |_______|___________________________________________________________________|
 |   14  |  ADC channel 14 (monitors voltage or current) or GPI.             |
 |_______|___________________________________________________________________|
 |   15  |  ADC channel 15 (monitors voltage or current) or GPI.             |
 |_______|___________________________________________________________________|
 |   16  |  Internal temperature sensor.                                     |
 |_______|___________________________________________________________________|
 |   17  |  External DS75LV temperature sensor with I2C address 90h.         |
 |_______|___________________________________________________________________|
 |   18  |  External DS75LV temperature sensor with I2C address 92h.         |
 |_______|___________________________________________________________________|
 |   19  |  External DS75LV temperature sensor with I2C address 94h.         |
 |_______|___________________________________________________________________|
 |   20  |  External DS75LV temperature sensor with I2C address 96h.         |
 |_______|___________________________________________________________________|
 | 21=E2=80=93254|  Reserved.                                                        |
 |_______|___________________________________________________________________|
 |   255 |  Applies to all pages.                                            |
 |_______|___________________________________________________________________|
 *
 * @operation: Turn on and off power supplies
 * @on_off_config: Configure the power supply on and off transition behaviour
 * @write_protect: protect against changes to the device's memory
 * @vout_margin_high: the voltage when OPERATION is set to margin high
 * @vout_margin_low: the voltage when OPERATION is set to margin low
 * @vout_scale: scale ADC reading to actual device reading if different
 * @iout_cal_gain: set ratio of the voltage at the ADC input to sensed current
 */
typedef struct MAX34451State {
    PMBusDevice parent;

    uint16_t power_good_on[MAX34451_NUM_PWR_DEVICES];
    uint16_t power_good_off[MAX34451_NUM_PWR_DEVICES];
    uint16_t ton_delay[MAX34451_NUM_MARGINED_PSU];
    uint16_t ton_max_fault_limit[MAX34451_NUM_MARGINED_PSU];
    uint16_t toff_delay[MAX34451_NUM_MARGINED_PSU];
    uint8_t status_mfr_specific[MAX34451_NUM_PWR_DEVICES];
    /* Manufacturer specific function */
    uint64_t mfr_location;
    uint64_t mfr_date;
    uint64_t mfr_serial;
    uint16_t mfr_mode;
    uint32_t psen_config[MAX34451_NUM_MARGINED_PSU];
    uint16_t vout_peak[MAX34451_NUM_PWR_DEVICES];
    uint16_t iout_peak[MAX34451_NUM_PWR_DEVICES];
    uint16_t temperature_peak[MAX34451_NUM_TEMP_DEVICES];
    uint16_t vout_min[MAX34451_NUM_PWR_DEVICES];
    uint16_t nv_log_config;
    uint32_t fault_response[MAX34451_NUM_PWR_DEVICES];
    uint16_t fault_retry;
    uint32_t fault_log;
    uint32_t time_count;
    uint16_t margin_config[MAX34451_NUM_MARGINED_PSU];
    uint16_t fw_serial;
    uint16_t iout_avg[MAX34451_NUM_PWR_DEVICES];
    uint16_t channel_config[MAX34451_NUM_PWR_DEVICES];
    uint16_t ton_seq_max[MAX34451_NUM_MARGINED_PSU];
    uint32_t pwm_config[MAX34451_NUM_MARGINED_PSU];
    uint32_t seq_config[MAX34451_NUM_MARGINED_PSU];
    uint16_t temp_sensor_config[MAX34451_NUM_TEMP_DEVICES];
    uint16_t store_single;
    uint16_t crc;
} MAX34451State;


static void max34451_check_limits(MAX34451State *s)
{
    PMBusDevice *pmdev = PMBUS_DEVICE(s);

    pmbus_check_limits(pmdev);

    for (int i = 0; i < MAX34451_NUM_PWR_DEVICES; i++) {
        if (pmdev->pages[i].read_vout == 0) { /* PSU disabled */
            continue;
        }

        if (pmdev->pages[i].read_vout > s->vout_peak[i]) {
            s->vout_peak[i] = pmdev->pages[i].read_vout;
        }

        if (pmdev->pages[i].read_vout < s->vout_min[i]) {
            s->vout_min[i] = pmdev->pages[i].read_vout;
        }

        if (pmdev->pages[i].read_iout > s->iout_peak[i]) {
            s->iout_peak[i] = pmdev->pages[i].read_iout;
        }
    }

    for (int i = 0; i < MAX34451_NUM_TEMP_DEVICES; i++) {
        if (pmdev->pages[i + 16].read_temperature_1 > s->temperature_peak[i]) {
            s->temperature_peak[i] = pmdev->pages[i + 16].read_temperature_1;
        }
    }
}

static uint8_t max34451_read_byte(PMBusDevice *pmdev)
{
    MAX34451State *s = MAX34451(pmdev);
    switch (pmdev->code) {

    case PMBUS_POWER_GOOD_ON:
        if (pmdev->page < 16) {
            pmbus_send16(pmdev, s->power_good_on[pmdev->page]);
        }
        break;

    case PMBUS_POWER_GOOD_OFF:
        if (pmdev->page < 16) {
            pmbus_send16(pmdev, s->power_good_off[pmdev->page]);
        }
        break;

    case PMBUS_TON_DELAY:
        if (pmdev->page < 12) {
            pmbus_send16(pmdev, s->ton_delay[pmdev->page]);
        }
        break;

    case PMBUS_TON_MAX_FAULT_LIMIT:
        if (pmdev->page < 12) {
            pmbus_send16(pmdev, s->ton_max_fault_limit[pmdev->page]);
        }
        break;

    case PMBUS_TOFF_DELAY:
        if (pmdev->page < 12) {
            pmbus_send16(pmdev, s->toff_delay[pmdev->page]);
        }
        break;

    case PMBUS_STATUS_MFR_SPECIFIC:
        if (pmdev->page < 16) {
            pmbus_send8(pmdev, s->status_mfr_specific[pmdev->page]);
        }
        break;

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

    case PMBUS_MFR_MODEL:
        pmbus_send8(pmdev, 0x59);
        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 MAX34451_MFR_MODE:
        pmbus_send16(pmdev, s->mfr_mode);
        break;

    case MAX34451_MFR_PSEN_CONFIG:
        if (pmdev->page < 12) {
            pmbus_send32(pmdev, s->psen_config[pmdev->page]);
        }
        break;

    case MAX34451_MFR_VOUT_PEAK:
        if (pmdev->page < 16) {
            pmbus_send16(pmdev, s->vout_peak[pmdev->page]);
        }
        break;

    case MAX34451_MFR_IOUT_PEAK:
        if (pmdev->page < 16) {
            pmbus_send16(pmdev, s->iout_peak[pmdev->page]);
        }
        break;

    case MAX34451_MFR_TEMPERATURE_PEAK:
        if (15 < pmdev->page && pmdev->page < 21) {
            pmbus_send16(pmdev, s->temperature_peak[pmdev->page % 16]);
        } else {
            pmbus_send16(pmdev, s->temperature_peak[0]);
        }
        break;

    case MAX34451_MFR_VOUT_MIN:
        if (pmdev->page < 16) {
            pmbus_send16(pmdev, s->vout_min[pmdev->page]);
        }
        break;

    case MAX34451_MFR_NV_LOG_CONFIG:
        pmbus_send16(pmdev, s->nv_log_config);
        break;

    case MAX34451_MFR_FAULT_RESPONSE:
        if (pmdev->page < 16) {
            pmbus_send32(pmdev, s->fault_response[pmdev->page]);
        }
        break;

    case MAX34451_MFR_FAULT_RETRY:
        pmbus_send32(pmdev, s->fault_retry);
        break;

    case MAX34451_MFR_NV_FAULT_LOG:
        pmbus_send32(pmdev, s->fault_log);
        break;

    case MAX34451_MFR_TIME_COUNT:
        pmbus_send32(pmdev, s->time_count);
        break;

    case MAX34451_MFR_MARGIN_CONFIG:
        if (pmdev->page < 12) {
            pmbus_send16(pmdev, s->margin_config[pmdev->page]);
        }
        break;

    case MAX34451_MFR_FW_SERIAL:
        if (pmdev->page == 255) {
            pmbus_send16(pmdev, 1); /* Firmware revision */
        }
        break;

    case MAX34451_MFR_IOUT_AVG:
        if (pmdev->page < 16) {
            pmbus_send16(pmdev, s->iout_avg[pmdev->page]);
        }
        break;

    case MAX34451_MFR_CHANNEL_CONFIG:
        if (pmdev->page < 16) {
            pmbus_send16(pmdev, s->channel_config[pmdev->page]);
        }
        break;

    case MAX34451_MFR_TON_SEQ_MAX:
        if (pmdev->page < 12) {
            pmbus_send16(pmdev, s->ton_seq_max[pmdev->page]);
        }
        break;

    case MAX34451_MFR_PWM_CONFIG:
        if (pmdev->page < 12) {
            pmbus_send32(pmdev, s->pwm_config[pmdev->page]);
        }
        break;

    case MAX34451_MFR_SEQ_CONFIG:
        if (pmdev->page < 12) {
            pmbus_send32(pmdev, s->seq_config[pmdev->page]);
        }
        break;

    case MAX34451_MFR_TEMP_SENSOR_CONFIG:
        if (15 < pmdev->page && pmdev->page < 21) {
            pmbus_send32(pmdev, s->temp_sensor_config[pmdev->page % 16]);
        }
        break;

    case MAX34451_MFR_STORE_SINGLE:
        pmbus_send32(pmdev, s->store_single);
        break;

    case MAX34451_MFR_CRC:
        pmbus_send32(pmdev, s->crc);
        break;

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

static int max34451_write_data(PMBusDevice *pmdev, const uint8_t *buf,
                               uint8_t len)
{
    MAX34451State *s = MAX34451(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--;
    uint8_t index = pmdev->page;

    switch (pmdev->code) {
    case MAX34451_MFR_STORE_ALL:
    case MAX34451_MFR_RESTORE_ALL:
    case MAX34451_MFR_STORE_SINGLE:
        /*
         * TODO: hardware behaviour is to move the contents of volatile
         * memory to non-volatile memory.
         */
        break;

    case PMBUS_POWER_GOOD_ON: /* R/W word */
        if (pmdev->page < MAX34451_NUM_PWR_DEVICES) {
            s->power_good_on[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case PMBUS_POWER_GOOD_OFF: /* R/W word */
        if (pmdev->page < MAX34451_NUM_PWR_DEVICES) {
            s->power_good_off[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case PMBUS_TON_DELAY: /* R/W word */
        if (pmdev->page < 12) {
            s->ton_delay[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case PMBUS_TON_MAX_FAULT_LIMIT: /* R/W word */
        if (pmdev->page < 12) {
            s->ton_max_fault_limit[pmdev->page]
                = pmbus_receive16(pmdev);
        }
        break;

    case PMBUS_TOFF_DELAY: /* R/W word */
        if (pmdev->page < 12) {
            s->toff_delay[pmdev->page] = pmbus_receive16(pmdev);
        }
        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 MAX34451_MFR_MODE: /* R/W word */
         s->mfr_mode = pmbus_receive16(pmdev);
        break;

    case MAX34451_MFR_PSEN_CONFIG: /* R/W 32 */
        if (pmdev->page < 12) {
            s->psen_config[pmdev->page] = pmbus_receive32(pmdev);
        }
        break;

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

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

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

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

    case MAX34451_MFR_NV_LOG_CONFIG: /* R/W word */
         s->nv_log_config = pmbus_receive16(pmdev);
        break;

    case MAX34451_MFR_FAULT_RESPONSE: /* R/W 32 */
        if (pmdev->page < 16) {
            s->fault_response[pmdev->page] = pmbus_receive32(pmdev);
        }
        break;

    case MAX34451_MFR_FAULT_RETRY: /* R/W word */
        s->fault_retry = pmbus_receive16(pmdev);
        break;

    case MAX34451_MFR_TIME_COUNT: /* R/W 32 */
        s->time_count = pmbus_receive32(pmdev);
        break;

    case MAX34451_MFR_MARGIN_CONFIG: /* R/W word */
        if (pmdev->page < 12) {
            s->margin_config[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

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

    case MAX34451_MFR_TON_SEQ_MAX: /* R/W word */
        if (pmdev->page < 12) {
            s->ton_seq_max[pmdev->page] = pmbus_receive16(pmdev);
        }
        break;

    case MAX34451_MFR_PWM_CONFIG: /* R/W 32 */
        if (pmdev->page < 12) {
            s->pwm_config[pmdev->page] = pmbus_receive32(pmdev);
        }
        break;

    case MAX34451_MFR_SEQ_CONFIG:  /* R/W 32 */
        if (pmdev->page < 12) {
            s->seq_config[pmdev->page] = pmbus_receive32(pmdev);
        }
        break;

    case MAX34451_MFR_TEMP_SENSOR_CONFIG:  /* R/W word */
        if (15 < pmdev->page && pmdev->page < 21) {
            s->temp_sensor_config[pmdev->page % 16]
                = pmbus_receive16(pmdev);
        }
        break;

    case MAX34451_MFR_CRC: /* R/W word */
        s->crc = pmbus_receive16(pmdev);
        break;

    case MAX34451_MFR_NV_FAULT_LOG:
    case MAX34451_MFR_FW_SERIAL:
    case MAX34451_MFR_IOUT_AVG:
        /* Read only commands */
        pmdev->pages[index].status_word |= PMBUS_STATUS_CML;
        pmdev->pages[index].status_cml |= PB_CML_FAULT_INVALID_DATA;
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: writing to read-only register 0x%02x\n",
                      __func__, pmdev->code);
        break;

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

    return 0;
}

static void max34451_get(Object *obj, Visitor *v, const char *name,
                                     void *opaque, Error **errp)
{
    visit_type_uint16(v, name, (uint16_t *)opaque, errp);
}

static void max34451_set(Object *obj, Visitor *v, const char *name,
                                 void *opaque, Error **errp)
{
    MAX34451State *s = MAX34451(obj);
    uint16_t *internal = opaque;
    uint16_t value;
    if (!visit_type_uint16(v, name, &value, errp)) {
        return;
    }

    *internal = value;
    max34451_check_limits(s);
}

/* used to init uint16_t arrays */
static inline void *memset_word(void *s, uint16_t c, size_t n)
{
    size_t i;
    uint16_t *p = s;

    for (i = 0; i < n; i++) {
        p[i] = c;
    }

    return s;
}

static void max34451_exit_reset(Object *obj)
{
    PMBusDevice *pmdev = PMBUS_DEVICE(obj);
    MAX34451State *s = MAX34451(obj);
    pmdev->capability = DEFAULT_CAPABILITY;

    for (int i = 0; i < MAX34451_NUM_PAGES; i++) {
        pmdev->pages[i].operation = DEFAULT_OP_ON;
        pmdev->pages[i].on_off_config = DEFAULT_ON_OFF_CONFIG;
        pmdev->pages[i].revision = 0x11;
        pmdev->pages[i].vout_mode = DEFAULT_VOUT_MODE;
    }

    for (int i = 0; i < MAX34451_NUM_PWR_DEVICES; i++) {
        pmdev->pages[i].vout_scale_monitor = DEFAULT_SCALE;
        pmdev->pages[i].vout_ov_fault_limit = DEFAULT_OV_LIMIT;
        pmdev->pages[i].vout_ov_warn_limit = DEFAULT_OV_LIMIT;
        pmdev->pages[i].iout_oc_warn_limit = DEFAULT_OC_LIMIT;
        pmdev->pages[i].iout_oc_fault_limit = DEFAULT_OC_LIMIT;
    }

    for (int i = 0; i < MAX34451_NUM_MARGINED_PSU; i++) {
        pmdev->pages[i].ton_max_fault_limit = DEFAULT_TON_FAULT_LIMIT;
    }

    for (int i = 16; i < MAX34451_NUM_TEMP_DEVICES + 16; i++) {
        pmdev->pages[i].read_temperature_1 = DEFAULT_TEMPERATURE;
        pmdev->pages[i].ot_warn_limit = DEFAULT_OT_LIMIT;
        pmdev->pages[i].ot_fault_limit = DEFAULT_OT_LIMIT;
    }

    memset_word(s->ton_max_fault_limit, DEFAULT_TON_FAULT_LIMIT,
                MAX34451_NUM_MARGINED_PSU);
    memset_word(s->channel_config, DEFAULT_CHANNEL_CONFIG,
                MAX34451_NUM_PWR_DEVICES);
    memset_word(s->vout_min, DEFAULT_VMIN, MAX34451_NUM_PWR_DEVICES);

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

static const VMStateDescription vmstate_max34451 = {
    .name = TYPE_MAX34451,
    .version_id = 0,
    .minimum_version_id = 0,
    .fields = (VMStateField[]){
        VMSTATE_PMBUS_DEVICE(parent, MAX34451State),
        VMSTATE_UINT16_ARRAY(power_good_on, MAX34451State,
                             MAX34451_NUM_PWR_DEVICES),
        VMSTATE_UINT16_ARRAY(power_good_off, MAX34451State,
                             MAX34451_NUM_PWR_DEVICES),
        VMSTATE_UINT16_ARRAY(ton_delay, MAX34451State,
                             MAX34451_NUM_MARGINED_PSU),
        VMSTATE_UINT16_ARRAY(ton_max_fault_limit, MAX34451State,
                             MAX34451_NUM_MARGINED_PSU),
        VMSTATE_UINT16_ARRAY(toff_delay, MAX34451State,
                             MAX34451_NUM_MARGINED_PSU),
        VMSTATE_UINT8_ARRAY(status_mfr_specific, MAX34451State,
                             MAX34451_NUM_PWR_DEVICES),
        VMSTATE_UINT64(mfr_location, MAX34451State),
        VMSTATE_UINT64(mfr_date, MAX34451State),
        VMSTATE_UINT64(mfr_serial, MAX34451State),
        VMSTATE_UINT16(mfr_mode, MAX34451State),
        VMSTATE_UINT32_ARRAY(psen_config, MAX34451State,
                             MAX34451_NUM_MARGINED_PSU),
        VMSTATE_UINT16_ARRAY(vout_peak, MAX34451State,
                             MAX34451_NUM_PWR_DEVICES),
        VMSTATE_UINT16_ARRAY(iout_peak, MAX34451State,
                             MAX34451_NUM_PWR_DEVICES),
        VMSTATE_UINT16_ARRAY(temperature_peak, MAX34451State,
                             MAX34451_NUM_TEMP_DEVICES),
        VMSTATE_UINT16_ARRAY(vout_min, MAX34451State, MAX34451_NUM_PWR_DEVICES),
        VMSTATE_UINT16(nv_log_config, MAX34451State),
        VMSTATE_UINT32_ARRAY(fault_response, MAX34451State,
                             MAX34451_NUM_PWR_DEVICES),
        VMSTATE_UINT16(fault_retry, MAX34451State),
        VMSTATE_UINT32(fault_log, MAX34451State),
        VMSTATE_UINT32(time_count, MAX34451State),
        VMSTATE_UINT16_ARRAY(margin_config, MAX34451State,
                             MAX34451_NUM_MARGINED_PSU),
        VMSTATE_UINT16(fw_serial, MAX34451State),
        VMSTATE_UINT16_ARRAY(iout_avg, MAX34451State, MAX34451_NUM_PWR_DEVICES),
        VMSTATE_UINT16_ARRAY(channel_config, MAX34451State,
                             MAX34451_NUM_PWR_DEVICES),
        VMSTATE_UINT16_ARRAY(ton_seq_max, MAX34451State,
                             MAX34451_NUM_MARGINED_PSU),
        VMSTATE_UINT32_ARRAY(pwm_config, MAX34451State,
                             MAX34451_NUM_MARGINED_PSU),
        VMSTATE_UINT32_ARRAY(seq_config, MAX34451State,
                             MAX34451_NUM_MARGINED_PSU),
        VMSTATE_UINT16_ARRAY(temp_sensor_config, MAX34451State,
                             MAX34451_NUM_TEMP_DEVICES),
        VMSTATE_UINT16(store_single, MAX34451State),
        VMSTATE_UINT16(crc, MAX34451State),
        VMSTATE_END_OF_LIST()
    }
};

static void max34451_init(Object *obj)
{
    PMBusDevice *pmdev = PMBUS_DEVICE(obj);
    uint64_t psu_flags = PB_HAS_VOUT | PB_HAS_IOUT | PB_HAS_VOUT_MODE |
                         PB_HAS_IOUT_GAIN;

    for (int i = 0; i < MAX34451_NUM_PWR_DEVICES; i++) {
        pmbus_page_config(pmdev, i, psu_flags);
    }

    for (int i = 0; i < MAX34451_NUM_MARGINED_PSU; i++) {
        pmbus_page_config(pmdev, i, psu_flags | PB_HAS_VOUT_MARGIN);
    }

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

    /* get and set the voltage in millivolts, max is 32767 mV */
    for (int i = 0; i < MAX34451_NUM_PWR_DEVICES; i++) {
        object_property_add(obj, "vout[*]", "uint16",
                            max34451_get,
                            max34451_set, NULL, &pmdev->pages[i].read_vout);
    }

    /*
     * get and set the temperature of the internal temperature sensor in
     * centidegrees Celcius i.e.: 2500 -> 25.00 C, max is 327.67 C
     */
    for (int i = 0; i < MAX34451_NUM_TEMP_DEVICES; i++) {
        object_property_add(obj, "temperature[*]", "uint16",
                            max34451_get,
                            max34451_set,
                            NULL,
                            &pmdev->pages[i + 16].read_temperature_1);
    }

}

static void max34451_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 MAX34451 16-Channel V/I monitor";
    dc->vmsd = &vmstate_max34451;
    k->write_data = max34451_write_data;
    k->receive_byte = max34451_read_byte;
    k->device_num_pages = MAX34451_NUM_PAGES;
    rc->phases.exit = max34451_exit_reset;
}

static const TypeInfo max34451_info = {
    .name = TYPE_MAX34451,
    .parent = TYPE_PMBUS_DEVICE,
    .instance_size = sizeof(MAX34451State),
    .instance_init = max34451_init,
    .class_init = max34451_class_init,
};

static void max34451_register_types(void)
{
    type_register_static(&max34451_info);
}

type_init(max34451_register_types)