1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers(MP2891)
6 #include <linux/bitfield.h>
8 #include <linux/module.h>
9 #include <linux/of_device.h>
13 * Vender specific registers, the register MFR_SVI3_IOUT_PRT(0x65),
14 * MFR_VOUT_LOOP_CTRL(0xBD), READ_PIN_EST(0x94)and READ_IIN_EST(0x95)
15 * redefine the standard PMBUS register. The MFR_SVI3_IOUT_PRT(0x65)
16 * is used to identify the iout scale and the MFR_VOUT_LOOP_CTRL(0xBD)
17 * is used to identify the vout scale. The READ_PIN_EST(0x94) is used
18 * to read input power per rail. The MP2891 does not have standard
19 * READ_IIN register(0x89), the iin telemetry can be obtained through
20 * the vendor redefined register READ_IIN_EST(0x95).
22 #define MFR_VOUT_LOOP_CTRL 0xBD
23 #define READ_PIN_EST 0x94
24 #define READ_IIN_EST 0x95
25 #define MFR_SVI3_IOUT_PRT 0x65
27 #define MP2891_TEMP_LIMIT_OFFSET 40
28 #define MP2891_PIN_LIMIT_UINT 2
29 #define MP2891_IOUT_LIMIT_UINT 8
30 #define MP2891_IOUT_SCALE_DIV 32
31 #define MP2891_VOUT_SCALE_DIV 100
32 #define MP2891_OVUV_DELTA_SCALE 50
33 #define MP2891_OV_LIMIT_SCALE 20
34 #define MP2891_UV_LIMIT_SCALE 5
36 #define MP2891_PAGE_NUM 2
38 #define MP2891_RAIL1_FUNC (PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | \
39 PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP | \
40 PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | \
41 PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_VOUT | \
42 PMBUS_HAVE_STATUS_IOUT | \
43 PMBUS_HAVE_STATUS_INPUT | \
44 PMBUS_HAVE_STATUS_TEMP)
46 #define MP2891_RAIL2_FUNC (PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | \
47 PMBUS_HAVE_TEMP | PMBUS_HAVE_POUT | \
48 PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | \
49 PMBUS_HAVE_STATUS_VOUT | \
50 PMBUS_HAVE_STATUS_IOUT | \
51 PMBUS_HAVE_STATUS_INPUT | \
52 PMBUS_HAVE_STATUS_TEMP)
55 struct pmbus_driver_info info
;
56 int vout_scale
[MP2891_PAGE_NUM
];
57 int iout_scale
[MP2891_PAGE_NUM
];
60 #define to_mp2891_data(x) container_of(x, struct mp2891_data, info)
62 /* Converts a LINEAR11 value to DIRECT format */
63 static u16
mp2891_reg2data_linear11(u16 word
)
69 exponent
= ((s16
)word
) >> 11;
70 mantissa
= ((s16
)((word
& 0x7ff) << 5)) >> 5;
82 mp2891_identify_vout_scale(struct i2c_client
*client
, struct pmbus_driver_info
*info
,
85 struct mp2891_data
*data
= to_mp2891_data(info
);
88 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, page
);
92 ret
= i2c_smbus_read_word_data(client
, MFR_VOUT_LOOP_CTRL
);
97 * The output voltage is equal to the READ_VOUT(0x8B) register value multiplied
99 * Obtain vout scale from the register MFR_VOUT_LOOP_CTRL, bits 15-14,bit 13.
100 * If MFR_VOUT_LOOP_CTRL[13] = 1, the vout scale is below:
102 * If MFR_VOUT_LOOP_CTRL[13] = 0, the vout scale is decided by
103 * MFR_VOUT_LOOP_CTRL[15:14]:
104 * 00b - 6.25mV/LSB, 01b - 5mV/LSB, 10b - 2mV/LSB, 11b - 1mV
106 if (ret
& GENMASK(13, 13)) {
107 data
->vout_scale
[page
] = 250;
109 ret
= FIELD_GET(GENMASK(15, 14), ret
);
111 data
->vout_scale
[page
] = 625;
113 data
->vout_scale
[page
] = 500;
115 data
->vout_scale
[page
] = 200;
117 data
->vout_scale
[page
] = 100;
124 mp2891_identify_iout_scale(struct i2c_client
*client
, struct pmbus_driver_info
*info
,
127 struct mp2891_data
*data
= to_mp2891_data(info
);
130 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, page
);
134 ret
= i2c_smbus_read_word_data(client
, MFR_SVI3_IOUT_PRT
);
139 * The output current is equal to the READ_IOUT(0x8C) register value
140 * multiplied by iout_scale.
141 * Obtain iout_scale from the register MFR_SVI3_IOUT_PRT[2:0].
142 * The value is selected as below:
143 * 000b - 1A/LSB, 001b - (1/32)A/LSB, 010b - (1/16)A/LSB,
144 * 011b - (1/8)A/LSB, 100b - (1/4)A/LSB, 101b - (1/2)A/LSB
145 * 110b - 1A/LSB, 111b - 2A/LSB
147 switch (ret
& GENMASK(2, 0)) {
150 data
->iout_scale
[page
] = 32;
153 data
->iout_scale
[page
] = 1;
156 data
->iout_scale
[page
] = 2;
159 data
->iout_scale
[page
] = 4;
162 data
->iout_scale
[page
] = 8;
165 data
->iout_scale
[page
] = 16;
168 data
->iout_scale
[page
] = 64;
175 static int mp2891_identify(struct i2c_client
*client
, struct pmbus_driver_info
*info
)
179 /* Identify vout scale for rail 1. */
180 ret
= mp2891_identify_vout_scale(client
, info
, 0);
184 /* Identify vout scale for rail 2. */
185 ret
= mp2891_identify_vout_scale(client
, info
, 1);
189 /* Identify iout scale for rail 1. */
190 ret
= mp2891_identify_iout_scale(client
, info
, 0);
194 /* Identify iout scale for rail 2. */
195 return mp2891_identify_iout_scale(client
, info
, 1);
198 static int mp2891_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
203 case PMBUS_VOUT_MODE
:
205 * The MP2891 does not follow standard PMBus protocol completely, the
206 * PMBUS_VOUT_MODE(0x20) in MP2891 is reserved and 0x00 is always
207 * returned when the register is read. But the calculation of vout in
208 * this driver is based on direct format. As a result, the format of
209 * vout is enforced to direct.
211 ret
= PB_VOUT_MODE_DIRECT
;
221 static int mp2891_read_word_data(struct i2c_client
*client
, int page
,
224 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
225 struct mp2891_data
*data
= to_mp2891_data(info
);
230 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
234 ret
= ret
& GENMASK(9, 0);
238 * The MP2891 does not have standard PMBUS_READ_IIN register(0x89),
239 * the iin telemetry can be obtained through the vender redefined
240 * register READ_IIN_EST(0x95). The MP2891 PMBUS_READ_IIN register
241 * is linear11 format, But the pout scale is set to 1A/Lsb(using
242 * r/m/b scale). As a result, the iin read from MP2891 should be
243 * calculated to A, then return the result to pmbus core.
245 ret
= pmbus_read_word_data(client
, page
, phase
, READ_IIN_EST
);
249 ret
= mp2891_reg2data_linear11(ret
);
253 * The MP2891 has standard PMBUS_READ_PIN register(0x97), but this
254 * is not used to read the input power per rail. The input power
255 * per rail is read through the vender redefined register
256 * READ_PIN_EST(0x94). The MP2891 PMBUS_READ_PIN register is linear11
257 * format, But the pout scale is set to 1W/Lsb(using r/m/b scale).
258 * As a result, the pin read from MP2891 should be calculated to W,
259 * then return the result to pmbus core.
261 ret
= pmbus_read_word_data(client
, page
, phase
, READ_PIN_EST
);
265 ret
= mp2891_reg2data_linear11(ret
);
267 case PMBUS_READ_POUT
:
269 * The MP2891 PMBUS_READ_POUT register is linear11 format, and the
270 * exponent is not a constant value. But the pout scale is set to
271 * 1W/Lsb(using r/m/b scale). As a result, the pout read from MP2891
272 * should be calculated to W, then return the result to pmbus core.
274 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
278 ret
= mp2891_reg2data_linear11(ret
);
280 case PMBUS_READ_VOUT
:
281 case PMBUS_VOUT_UV_WARN_LIMIT
:
282 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
286 ret
= DIV_ROUND_CLOSEST(ret
* data
->vout_scale
[page
], MP2891_VOUT_SCALE_DIV
);
288 case PMBUS_READ_IOUT
:
289 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
293 ret
= DIV_ROUND_CLOSEST((ret
& GENMASK(10, 0)) * data
->iout_scale
[page
],
294 MP2891_IOUT_SCALE_DIV
);
296 case PMBUS_OT_FAULT_LIMIT
:
297 case PMBUS_OT_WARN_LIMIT
:
299 * The scale of MP2891 PMBUS_OT_FAULT_LIMIT and PMBUS_OT_WARN_LIMIT
300 * is 1°C/LSB and they have 40°C offset.
302 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
306 ret
= (ret
& GENMASK(7, 0)) - MP2891_TEMP_LIMIT_OFFSET
;
308 case PMBUS_VIN_OV_FAULT_LIMIT
:
310 * The MP2891 PMBUS_VIN_OV_FAULT_LIMIT scale is 125mV/Lsb.
311 * but the vin scale is set to 31.25mV/Lsb(using r/m/b scale).
312 * As a result, the limit value should be multiplied by 4.
314 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
318 ret
= (ret
& GENMASK(7, 0)) * 4;
320 case PMBUS_VOUT_UV_FAULT_LIMIT
:
321 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
325 if (FIELD_GET(GENMASK(11, 8), ret
))
326 ret
= FIELD_GET(GENMASK(7, 0), ret
) * MP2891_UV_LIMIT_SCALE
-
327 (FIELD_GET(GENMASK(11, 8), ret
) + 1) * MP2891_OVUV_DELTA_SCALE
;
329 ret
= FIELD_GET(GENMASK(7, 0), ret
) * MP2891_UV_LIMIT_SCALE
;
331 ret
= ret
< 0 ? 0 : ret
;
333 case PMBUS_VOUT_OV_FAULT_LIMIT
:
334 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
338 if (FIELD_GET(GENMASK(11, 8), ret
))
339 ret
= FIELD_GET(GENMASK(7, 0), ret
) * MP2891_OV_LIMIT_SCALE
+
340 (FIELD_GET(GENMASK(11, 8), ret
) + 1) * MP2891_OVUV_DELTA_SCALE
;
342 ret
= FIELD_GET(GENMASK(7, 0), ret
) * MP2891_OV_LIMIT_SCALE
;
344 case PMBUS_IOUT_OC_WARN_LIMIT
:
345 case PMBUS_IOUT_OC_FAULT_LIMIT
:
346 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
350 ret
= DIV_ROUND_CLOSEST((ret
& GENMASK(7, 0)) * data
->iout_scale
[page
] *
351 MP2891_IOUT_LIMIT_UINT
, MP2891_IOUT_SCALE_DIV
);
353 case PMBUS_IIN_OC_WARN_LIMIT
:
355 * The scale of PMBUS_IIN_OC_WARN_LIMIT is 0.5A/Lsb, but the iin scale
356 * is set to 1A/Lsb(using r/m/b scale), so the word data should be
359 ret
= pmbus_read_word_data(client
, 0, phase
, reg
);
363 ret
= DIV_ROUND_CLOSEST((ret
& GENMASK(9, 0)), 2);
365 case PMBUS_PIN_OP_WARN_LIMIT
:
367 * The scale of PMBUS_PIN_OP_WARN_LIMIT is 2W/Lsb, but the pin scale
368 * is set to 1W/Lsb(using r/m/b scale), so the word data should be
371 ret
= pmbus_read_word_data(client
, 0, phase
, reg
);
375 ret
= (ret
& GENMASK(9, 0)) * MP2891_PIN_LIMIT_UINT
;
377 case PMBUS_READ_TEMPERATURE_1
:
378 case PMBUS_VIN_UV_FAULT_LIMIT
:
379 case PMBUS_VIN_UV_WARN_LIMIT
:
390 static int mp2891_write_word_data(struct i2c_client
*client
, int page
, int reg
,
393 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
394 struct mp2891_data
*data
= to_mp2891_data(info
);
398 case PMBUS_VOUT_UV_WARN_LIMIT
:
399 ret
= pmbus_write_word_data(client
, page
, reg
,
400 DIV_ROUND_CLOSEST(word
* MP2891_VOUT_SCALE_DIV
,
401 data
->vout_scale
[page
]));
403 case PMBUS_VOUT_UV_FAULT_LIMIT
:
405 * The PMBUS_VOUT_UV_FAULT_LIMIT[7:0] is the limit value, and bit8-bit15
406 * should not be changed.
408 ret
= pmbus_read_word_data(client
, page
, 0xff, reg
);
412 if (FIELD_GET(GENMASK(11, 8), ret
))
413 ret
= pmbus_write_word_data(client
, page
, reg
,
414 (ret
& ~GENMASK(7, 0)) |
415 FIELD_PREP(GENMASK(7, 0),
416 DIV_ROUND_CLOSEST(word
+
417 (FIELD_GET(GENMASK(11, 8), ret
) + 1) *
418 MP2891_OVUV_DELTA_SCALE
,
419 MP2891_UV_LIMIT_SCALE
)));
421 ret
= pmbus_write_word_data(client
, page
, reg
,
422 (ret
& ~GENMASK(7, 0)) |
423 FIELD_PREP(GENMASK(7, 0),
424 DIV_ROUND_CLOSEST(word
,
425 MP2891_UV_LIMIT_SCALE
)));
427 case PMBUS_VOUT_OV_FAULT_LIMIT
:
429 * The PMBUS_VOUT_OV_FAULT_LIMIT[7:0] is the limit value, and bit8-bit15
430 * should not be changed.
432 ret
= pmbus_read_word_data(client
, page
, 0xff, reg
);
436 if (FIELD_GET(GENMASK(11, 8), ret
))
437 ret
= pmbus_write_word_data(client
, page
, reg
,
438 (ret
& ~GENMASK(7, 0)) |
439 FIELD_PREP(GENMASK(7, 0),
440 DIV_ROUND_CLOSEST(word
-
441 (FIELD_GET(GENMASK(11, 8), ret
) + 1) *
442 MP2891_OVUV_DELTA_SCALE
,
443 MP2891_OV_LIMIT_SCALE
)));
445 ret
= pmbus_write_word_data(client
, page
, reg
,
446 (ret
& ~GENMASK(7, 0)) |
447 FIELD_PREP(GENMASK(7, 0),
448 DIV_ROUND_CLOSEST(word
,
449 MP2891_OV_LIMIT_SCALE
)));
451 case PMBUS_VIN_OV_FAULT_LIMIT
:
453 * The PMBUS_VIN_OV_FAULT_LIMIT[7:0] is the limit value, and bit8-bit15
454 * should not be changed. The scale of PMBUS_VIN_OV_FAULT_LIMIT is 125mV/Lsb,
455 * but the vin scale is set to 31.25mV/Lsb(using r/m/b scale), so the word data
456 * should be divided by 4.
458 ret
= pmbus_read_word_data(client
, page
, 0xff, reg
);
462 ret
= pmbus_write_word_data(client
, page
, reg
,
463 (ret
& ~GENMASK(7, 0)) |
464 FIELD_PREP(GENMASK(7, 0),
465 DIV_ROUND_CLOSEST(word
, 4)));
467 case PMBUS_OT_FAULT_LIMIT
:
468 case PMBUS_OT_WARN_LIMIT
:
470 * The scale of MP2891 PMBUS_OT_FAULT_LIMIT and PMBUS_OT_WARN_LIMIT
471 * have 40°C offset. The bit0-bit7 is the limit value, and bit8-bit15
472 * should not be changed.
474 ret
= pmbus_read_word_data(client
, page
, 0xff, reg
);
478 ret
= pmbus_write_word_data(client
, page
, reg
,
479 (ret
& ~GENMASK(7, 0)) |
480 FIELD_PREP(GENMASK(7, 0), word
+ MP2891_TEMP_LIMIT_OFFSET
));
482 case PMBUS_IOUT_OC_WARN_LIMIT
:
483 case PMBUS_IOUT_OC_FAULT_LIMIT
:
484 ret
= pmbus_write_word_data(client
, page
, reg
,
485 DIV_ROUND_CLOSEST(word
* MP2891_IOUT_SCALE_DIV
,
486 MP2891_IOUT_LIMIT_UINT
*
487 data
->iout_scale
[page
]));
489 case PMBUS_IIN_OC_WARN_LIMIT
:
491 * The scale of PMBUS_IIN_OC_WARN_LIMIT is 0.5A/Lsb, but the iin scale
492 * is set to 1A/Lsb(using r/m/b scale), so the word data should be
495 ret
= pmbus_write_word_data(client
, page
, reg
, word
* 2);
497 case PMBUS_PIN_OP_WARN_LIMIT
:
499 * The scale of PMBUS_PIN_OP_WARN_LIMIT is 2W/Lsb, but the pin scale
500 * is set to 1W/Lsb(using r/m/b scale), so the word data should be
503 ret
= pmbus_write_word_data(client
, page
, reg
,
504 DIV_ROUND_CLOSEST(word
, MP2891_PIN_LIMIT_UINT
));
506 case PMBUS_VIN_UV_FAULT_LIMIT
:
507 case PMBUS_VIN_UV_WARN_LIMIT
:
518 static const struct pmbus_driver_info mp2891_info
= {
519 .pages
= MP2891_PAGE_NUM
,
520 .format
[PSC_VOLTAGE_IN
] = direct
,
521 .format
[PSC_CURRENT_IN
] = direct
,
522 .format
[PSC_CURRENT_OUT
] = direct
,
523 .format
[PSC_TEMPERATURE
] = direct
,
524 .format
[PSC_POWER
] = direct
,
525 .format
[PSC_VOLTAGE_OUT
] = direct
,
527 /* set vin scale 31.25mV/Lsb */
528 .m
[PSC_VOLTAGE_IN
] = 32,
529 .R
[PSC_VOLTAGE_IN
] = 0,
530 .b
[PSC_VOLTAGE_IN
] = 0,
532 /* set temp scale 1000m°C/Lsb */
533 .m
[PSC_TEMPERATURE
] = 1,
534 .R
[PSC_TEMPERATURE
] = 0,
535 .b
[PSC_TEMPERATURE
] = 0,
537 .m
[PSC_CURRENT_IN
] = 1,
538 .R
[PSC_CURRENT_IN
] = 0,
539 .b
[PSC_CURRENT_IN
] = 0,
541 .m
[PSC_CURRENT_OUT
] = 1,
542 .R
[PSC_CURRENT_OUT
] = 0,
543 .b
[PSC_CURRENT_OUT
] = 0,
549 .m
[PSC_VOLTAGE_OUT
] = 1,
550 .R
[PSC_VOLTAGE_OUT
] = 3,
551 .b
[PSC_VOLTAGE_OUT
] = 0,
553 .func
[0] = MP2891_RAIL1_FUNC
,
554 .func
[1] = MP2891_RAIL2_FUNC
,
555 .read_word_data
= mp2891_read_word_data
,
556 .write_word_data
= mp2891_write_word_data
,
557 .read_byte_data
= mp2891_read_byte_data
,
558 .identify
= mp2891_identify
,
561 static int mp2891_probe(struct i2c_client
*client
)
563 struct mp2891_data
*data
;
565 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
569 memcpy(&data
->info
, &mp2891_info
, sizeof(mp2891_info
));
571 return pmbus_do_probe(client
, &data
->info
);
574 static const struct i2c_device_id mp2891_id
[] = {
578 MODULE_DEVICE_TABLE(i2c
, mp2891_id
);
580 static const struct of_device_id __maybe_unused mp2891_of_match
[] = {
581 {.compatible
= "mps,mp2891"},
584 MODULE_DEVICE_TABLE(of
, mp2891_of_match
);
586 static struct i2c_driver mp2891_driver
= {
589 .of_match_table
= mp2891_of_match
,
591 .probe
= mp2891_probe
,
592 .id_table
= mp2891_id
,
595 module_i2c_driver(mp2891_driver
);
597 MODULE_AUTHOR("Noah Wang <noahwang.wang@outlook.com>");
598 MODULE_DESCRIPTION("PMBus driver for MPS MP2891");
599 MODULE_LICENSE("GPL");
600 MODULE_IMPORT_NS("PMBUS");