1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers
5 * Copyright (C) 2020 Nvidia Technologies Ltd.
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
15 /* Vendor specific registers. */
16 #define MP2975_MFR_APS_HYS_R2 0x0d
17 #define MP2975_MFR_SLOPE_TRIM3 0x1d
18 #define MP2975_MFR_VR_MULTI_CONFIG_R1 0x0d
19 #define MP2975_MFR_VR_MULTI_CONFIG_R2 0x1d
20 #define MP2975_MFR_APS_DECAY_ADV 0x56
21 #define MP2975_MFR_DC_LOOP_CTRL 0x59
22 #define MP2975_MFR_OCP_UCP_PHASE_SET 0x65
23 #define MP2975_MFR_VR_CONFIG1 0x68
24 #define MP2975_MFR_READ_CS1_2 0x82
25 #define MP2975_MFR_READ_CS3_4 0x83
26 #define MP2975_MFR_READ_CS5_6 0x84
27 #define MP2975_MFR_READ_CS7_8 0x85
28 #define MP2975_MFR_READ_CS9_10 0x86
29 #define MP2975_MFR_READ_CS11_12 0x87
30 #define MP2975_MFR_READ_IOUT_PK 0x90
31 #define MP2975_MFR_READ_POUT_PK 0x91
32 #define MP2975_MFR_READ_VREF_R1 0xa1
33 #define MP2975_MFR_READ_VREF_R2 0xa3
34 #define MP2975_MFR_OVP_TH_SET 0xe5
35 #define MP2975_MFR_UVP_SET 0xe6
37 #define MP2975_VOUT_FORMAT BIT(15)
38 #define MP2975_VID_STEP_SEL_R1 BIT(4)
39 #define MP2975_IMVP9_EN_R1 BIT(13)
40 #define MP2975_VID_STEP_SEL_R2 BIT(3)
41 #define MP2975_IMVP9_EN_R2 BIT(12)
42 #define MP2975_PRT_THRES_DIV_OV_EN BIT(14)
43 #define MP2975_DRMOS_KCS GENMASK(13, 12)
44 #define MP2975_PROT_DEV_OV_OFF 10
45 #define MP2975_PROT_DEV_OV_ON 5
46 #define MP2975_SENSE_AMPL BIT(11)
47 #define MP2975_SENSE_AMPL_UNIT 1
48 #define MP2975_SENSE_AMPL_HALF 2
49 #define MP2975_VIN_UV_LIMIT_UNIT 8
51 #define MP2975_MAX_PHASE_RAIL1 8
52 #define MP2975_MAX_PHASE_RAIL2 4
53 #define MP2975_PAGE_NUM 2
55 #define MP2975_RAIL2_FUNC (PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | \
56 PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | \
60 struct pmbus_driver_info info
;
62 int vid_step
[MP2975_PAGE_NUM
];
63 int vref
[MP2975_PAGE_NUM
];
64 int vref_off
[MP2975_PAGE_NUM
];
65 int vout_max
[MP2975_PAGE_NUM
];
66 int vout_ov_fixed
[MP2975_PAGE_NUM
];
67 int vout_format
[MP2975_PAGE_NUM
];
68 int curr_sense_gain
[MP2975_PAGE_NUM
];
71 #define to_mp2975_data(x) container_of(x, struct mp2975_data, info)
73 static int mp2975_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
78 * Enforce VOUT direct format, since device allows to set the
79 * different formats for the different rails. Conversion from
80 * VID to direct provided by driver internally, in case it is
83 return PB_VOUT_MODE_DIRECT
;
90 mp2975_read_word_helper(struct i2c_client
*client
, int page
, int phase
, u8 reg
,
93 int ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
95 return (ret
> 0) ? ret
& mask
: ret
;
99 mp2975_vid2direct(int vrf
, int val
)
104 return 250 + (val
- 1) * 5;
108 return 500 + (val
- 1) * 10;
112 return 200 + (val
- 1) * 10;
121 mp2975_read_phase(struct i2c_client
*client
, struct mp2975_data
*data
,
122 int page
, int phase
, u8 reg
)
126 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
130 if (!((phase
+ 1) % MP2975_PAGE_NUM
))
135 * Output value is calculated as: (READ_CSx / 80 – 1.23) / (Kcs * Rcs)
137 * - Kcs is the DrMOS current sense gain of power stage, which is
138 * obtained from the register MP2975_MFR_VR_CONFIG1, bits 13-12 with
139 * the following selection of DrMOS (data->curr_sense_gain[page]):
140 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A.
141 * - Rcs is the internal phase current sense resistor which is constant
144 ph_curr
= ret
* 100 - 9800;
147 * Current phase sensing, providing by the device is not accurate
148 * for the light load. This because sampling of current occurrence of
149 * bit weight has a big deviation for light load. For handling such
150 * case phase current is represented as the maximum between the value
151 * calculated above and total rail current divided by number phases.
153 ret
= pmbus_read_word_data(client
, page
, phase
, PMBUS_READ_IOUT
);
157 return max_t(int, DIV_ROUND_CLOSEST(ret
, data
->info
.phases
[page
]),
158 DIV_ROUND_CLOSEST(ph_curr
, data
->curr_sense_gain
[page
]));
162 mp2975_read_phases(struct i2c_client
*client
, struct mp2975_data
*data
,
170 ret
= mp2975_read_phase(client
, data
, page
, phase
,
171 MP2975_MFR_READ_CS7_8
);
174 ret
= mp2975_read_phase(client
, data
, page
, phase
,
175 MP2975_MFR_READ_CS9_10
);
178 ret
= mp2975_read_phase(client
, data
, page
, phase
,
179 MP2975_MFR_READ_CS11_12
);
187 ret
= mp2975_read_phase(client
, data
, page
, phase
,
188 MP2975_MFR_READ_CS1_2
);
191 ret
= mp2975_read_phase(client
, data
, page
, phase
,
192 MP2975_MFR_READ_CS3_4
);
195 ret
= mp2975_read_phase(client
, data
, page
, phase
,
196 MP2975_MFR_READ_CS5_6
);
199 ret
= mp2975_read_phase(client
, data
, page
, phase
,
200 MP2975_MFR_READ_CS7_8
);
203 ret
= mp2975_read_phase(client
, data
, page
, phase
,
204 MP2975_MFR_READ_CS9_10
);
207 ret
= mp2975_read_phase(client
, data
, page
, phase
,
208 MP2975_MFR_READ_CS11_12
);
217 static int mp2975_read_word_data(struct i2c_client
*client
, int page
,
220 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
221 struct mp2975_data
*data
= to_mp2975_data(info
);
225 case PMBUS_OT_FAULT_LIMIT
:
226 ret
= mp2975_read_word_helper(client
, page
, phase
, reg
,
229 case PMBUS_VIN_OV_FAULT_LIMIT
:
230 ret
= mp2975_read_word_helper(client
, page
, phase
, reg
,
235 ret
= DIV_ROUND_CLOSEST(ret
, MP2975_VIN_UV_LIMIT_UNIT
);
237 case PMBUS_VOUT_OV_FAULT_LIMIT
:
239 * Register provides two values for over-voltage protection
240 * threshold for fixed (ovp2) and tracking (ovp1) modes. The
241 * minimum of these two values is provided as over-voltage
244 ret
= mp2975_read_word_helper(client
, page
, phase
,
245 MP2975_MFR_OVP_TH_SET
,
250 ret
= min_t(int, data
->vout_max
[page
] + 50 * (ret
+ 1),
251 data
->vout_ov_fixed
[page
]);
253 case PMBUS_VOUT_UV_FAULT_LIMIT
:
254 ret
= mp2975_read_word_helper(client
, page
, phase
,
260 ret
= DIV_ROUND_CLOSEST(data
->vref
[page
] * 10 - 50 *
261 (ret
+ 1) * data
->vout_scale
, 10);
263 case PMBUS_READ_VOUT
:
264 ret
= mp2975_read_word_helper(client
, page
, phase
, reg
,
270 * READ_VOUT can be provided in VID or direct format. The
271 * format type is specified by bit 15 of the register
272 * MP2975_MFR_DC_LOOP_CTRL. The driver enforces VOUT direct
273 * format, since device allows to set the different formats for
274 * the different rails and also all VOUT limits registers are
275 * provided in a direct format. In case format is VID - convert
278 if (data
->vout_format
[page
] == vid
)
279 ret
= mp2975_vid2direct(info
->vrm_version
[page
], ret
);
281 case PMBUS_VIRT_READ_POUT_MAX
:
282 ret
= mp2975_read_word_helper(client
, page
, phase
,
283 MP2975_MFR_READ_POUT_PK
,
288 ret
= DIV_ROUND_CLOSEST(ret
, 4);
290 case PMBUS_VIRT_READ_IOUT_MAX
:
291 ret
= mp2975_read_word_helper(client
, page
, phase
,
292 MP2975_MFR_READ_IOUT_PK
,
297 ret
= DIV_ROUND_CLOSEST(ret
, 4);
299 case PMBUS_READ_IOUT
:
300 ret
= mp2975_read_phases(client
, data
, page
, phase
);
305 case PMBUS_UT_WARN_LIMIT
:
306 case PMBUS_UT_FAULT_LIMIT
:
307 case PMBUS_VIN_UV_WARN_LIMIT
:
308 case PMBUS_VIN_UV_FAULT_LIMIT
:
309 case PMBUS_VOUT_UV_WARN_LIMIT
:
310 case PMBUS_VOUT_OV_WARN_LIMIT
:
311 case PMBUS_VIN_OV_WARN_LIMIT
:
312 case PMBUS_IIN_OC_FAULT_LIMIT
:
313 case PMBUS_IOUT_OC_LV_FAULT_LIMIT
:
314 case PMBUS_IIN_OC_WARN_LIMIT
:
315 case PMBUS_IOUT_OC_WARN_LIMIT
:
316 case PMBUS_IOUT_OC_FAULT_LIMIT
:
317 case PMBUS_IOUT_UC_FAULT_LIMIT
:
318 case PMBUS_POUT_OP_FAULT_LIMIT
:
319 case PMBUS_POUT_OP_WARN_LIMIT
:
320 case PMBUS_PIN_OP_WARN_LIMIT
:
329 static int mp2975_identify_multiphase_rail2(struct i2c_client
*client
)
334 * Identify multiphase for rail 2 - could be from 0 to 4.
335 * In case phase number is zero – only page zero is supported
337 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, 2);
341 /* Identify multiphase for rail 2 - could be from 0 to 4. */
342 ret
= i2c_smbus_read_word_data(client
, MP2975_MFR_VR_MULTI_CONFIG_R2
);
346 ret
&= GENMASK(2, 0);
347 return (ret
>= 4) ? 4 : ret
;
350 static void mp2975_set_phase_rail1(struct pmbus_driver_info
*info
)
354 for (i
= 0 ; i
< info
->phases
[0]; i
++)
355 info
->pfunc
[i
] = PMBUS_HAVE_IOUT
;
359 mp2975_set_phase_rail2(struct pmbus_driver_info
*info
, int num_phases
)
363 /* Set phases for rail 2 from upper to lower. */
364 for (i
= 1; i
<= num_phases
; i
++)
365 info
->pfunc
[MP2975_MAX_PHASE_RAIL1
- i
] = PMBUS_HAVE_IOUT
;
369 mp2975_identify_multiphase(struct i2c_client
*client
, struct mp2975_data
*data
,
370 struct pmbus_driver_info
*info
)
372 int num_phases2
, ret
;
374 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, 2);
378 /* Identify multiphase for rail 1 - could be from 1 to 8. */
379 ret
= i2c_smbus_read_word_data(client
, MP2975_MFR_VR_MULTI_CONFIG_R1
);
383 info
->phases
[0] = ret
& GENMASK(3, 0);
386 * The device provides a total of 8 PWM pins, and can be configured
387 * to different phase count applications for rail 1 and rail 2.
388 * Rail 1 can be set to 8 phases, while rail 2 can only be set to 4
389 * phases at most. When rail 1’s phase count is configured as 0, rail
390 * 1 operates with 1-phase DCM. When rail 2 phase count is configured
391 * as 0, rail 2 is disabled.
393 if (info
->phases
[0] > MP2975_MAX_PHASE_RAIL1
)
396 mp2975_set_phase_rail1(info
);
397 num_phases2
= min(MP2975_MAX_PHASE_RAIL1
- info
->phases
[0],
398 MP2975_MAX_PHASE_RAIL2
);
399 if (info
->phases
[1] && info
->phases
[1] <= num_phases2
)
400 mp2975_set_phase_rail2(info
, num_phases2
);
406 mp2975_identify_vid(struct i2c_client
*client
, struct mp2975_data
*data
,
407 struct pmbus_driver_info
*info
, u32 reg
, int page
,
408 u32 imvp_bit
, u32 vr_bit
)
412 /* Identify VID mode and step selection. */
413 ret
= i2c_smbus_read_word_data(client
, reg
);
417 if (ret
& imvp_bit
) {
418 info
->vrm_version
[page
] = imvp9
;
419 data
->vid_step
[page
] = MP2975_PROT_DEV_OV_OFF
;
420 } else if (ret
& vr_bit
) {
421 info
->vrm_version
[page
] = vr12
;
422 data
->vid_step
[page
] = MP2975_PROT_DEV_OV_ON
;
424 info
->vrm_version
[page
] = vr13
;
425 data
->vid_step
[page
] = MP2975_PROT_DEV_OV_OFF
;
432 mp2975_identify_rails_vid(struct i2c_client
*client
, struct mp2975_data
*data
,
433 struct pmbus_driver_info
*info
)
437 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, 2);
441 /* Identify VID mode for rail 1. */
442 ret
= mp2975_identify_vid(client
, data
, info
,
443 MP2975_MFR_VR_MULTI_CONFIG_R1
, 0,
444 MP2975_IMVP9_EN_R1
, MP2975_VID_STEP_SEL_R1
);
448 /* Identify VID mode for rail 2, if connected. */
450 ret
= mp2975_identify_vid(client
, data
, info
,
451 MP2975_MFR_VR_MULTI_CONFIG_R2
, 1,
453 MP2975_VID_STEP_SEL_R2
);
458 mp2975_current_sense_gain_get(struct i2c_client
*client
,
459 struct mp2975_data
*data
)
464 * Obtain DrMOS current sense gain of power stage from the register
465 * MP2975_MFR_VR_CONFIG1, bits 13-12. The value is selected as below:
466 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. Other
467 * values are invalid.
469 for (i
= 0 ; i
< data
->info
.pages
; i
++) {
470 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, i
);
473 ret
= i2c_smbus_read_word_data(client
,
474 MP2975_MFR_VR_CONFIG1
);
478 switch ((ret
& MP2975_DRMOS_KCS
) >> 12) {
480 data
->curr_sense_gain
[i
] = 50;
483 data
->curr_sense_gain
[i
] = 85;
486 data
->curr_sense_gain
[i
] = 97;
489 data
->curr_sense_gain
[i
] = 100;
498 mp2975_vref_get(struct i2c_client
*client
, struct mp2975_data
*data
,
499 struct pmbus_driver_info
*info
)
503 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, 3);
507 /* Get voltage reference value for rail 1. */
508 ret
= i2c_smbus_read_word_data(client
, MP2975_MFR_READ_VREF_R1
);
512 data
->vref
[0] = ret
* data
->vid_step
[0];
514 /* Get voltage reference value for rail 2, if connected. */
515 if (data
->info
.pages
== MP2975_PAGE_NUM
) {
516 ret
= i2c_smbus_read_word_data(client
, MP2975_MFR_READ_VREF_R2
);
520 data
->vref
[1] = ret
* data
->vid_step
[1];
526 mp2975_vref_offset_get(struct i2c_client
*client
, struct mp2975_data
*data
,
531 ret
= i2c_smbus_read_word_data(client
, MP2975_MFR_OVP_TH_SET
);
535 switch ((ret
& GENMASK(5, 3)) >> 3) {
537 data
->vref_off
[page
] = 140;
540 data
->vref_off
[page
] = 220;
543 data
->vref_off
[page
] = 400;
552 mp2975_vout_max_get(struct i2c_client
*client
, struct mp2975_data
*data
,
553 struct pmbus_driver_info
*info
, int page
)
557 /* Get maximum reference voltage of VID-DAC in VID format. */
558 ret
= i2c_smbus_read_word_data(client
, PMBUS_VOUT_MAX
);
562 data
->vout_max
[page
] = mp2975_vid2direct(info
->vrm_version
[page
], ret
&
568 mp2975_identify_vout_format(struct i2c_client
*client
,
569 struct mp2975_data
*data
, int page
)
573 ret
= i2c_smbus_read_word_data(client
, MP2975_MFR_DC_LOOP_CTRL
);
577 if (ret
& MP2975_VOUT_FORMAT
)
578 data
->vout_format
[page
] = vid
;
580 data
->vout_format
[page
] = direct
;
585 mp2975_vout_ov_scale_get(struct i2c_client
*client
, struct mp2975_data
*data
,
586 struct pmbus_driver_info
*info
)
588 int thres_dev
, sense_ampl
, ret
;
590 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, 0);
595 * Get divider for over- and under-voltage protection thresholds
596 * configuration from the Advanced Options of Auto Phase Shedding and
599 ret
= i2c_smbus_read_word_data(client
, MP2975_MFR_APS_DECAY_ADV
);
602 thres_dev
= ret
& MP2975_PRT_THRES_DIV_OV_EN
? MP2975_PROT_DEV_OV_ON
:
603 MP2975_PROT_DEV_OV_OFF
;
605 /* Select the gain of remote sense amplifier. */
606 ret
= i2c_smbus_read_word_data(client
, PMBUS_VOUT_SCALE_LOOP
);
609 sense_ampl
= ret
& MP2975_SENSE_AMPL
? MP2975_SENSE_AMPL_HALF
:
610 MP2975_SENSE_AMPL_UNIT
;
612 data
->vout_scale
= sense_ampl
* thres_dev
;
618 mp2975_vout_per_rail_config_get(struct i2c_client
*client
,
619 struct mp2975_data
*data
,
620 struct pmbus_driver_info
*info
)
624 for (i
= 0; i
< data
->info
.pages
; i
++) {
625 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, i
);
629 /* Obtain voltage reference offsets. */
630 ret
= mp2975_vref_offset_get(client
, data
, i
);
634 /* Obtain maximum voltage values. */
635 ret
= mp2975_vout_max_get(client
, data
, info
, i
);
640 * Get VOUT format for READ_VOUT command : VID or direct.
641 * Pages on same device can be configured with different
644 ret
= mp2975_identify_vout_format(client
, data
, i
);
649 * Set over-voltage fixed value. Thresholds are provided as
650 * fixed value, and tracking value. The minimum of them are
651 * exposed as over-voltage critical threshold.
653 data
->vout_ov_fixed
[i
] = data
->vref
[i
] +
654 DIV_ROUND_CLOSEST(data
->vref_off
[i
] *
662 static struct pmbus_driver_info mp2975_info
= {
664 .format
[PSC_VOLTAGE_IN
] = linear
,
665 .format
[PSC_VOLTAGE_OUT
] = direct
,
666 .format
[PSC_TEMPERATURE
] = direct
,
667 .format
[PSC_CURRENT_IN
] = linear
,
668 .format
[PSC_CURRENT_OUT
] = direct
,
669 .format
[PSC_POWER
] = direct
,
670 .m
[PSC_TEMPERATURE
] = 1,
671 .m
[PSC_VOLTAGE_OUT
] = 1,
672 .R
[PSC_VOLTAGE_OUT
] = 3,
673 .m
[PSC_CURRENT_OUT
] = 1,
675 .func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
|
676 PMBUS_HAVE_IIN
| PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
|
677 PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
| PMBUS_HAVE_POUT
|
678 PMBUS_HAVE_PIN
| PMBUS_HAVE_STATUS_INPUT
| PMBUS_PHASE_VIRTUAL
,
679 .read_byte_data
= mp2975_read_byte_data
,
680 .read_word_data
= mp2975_read_word_data
,
683 static int mp2975_probe(struct i2c_client
*client
)
685 struct pmbus_driver_info
*info
;
686 struct mp2975_data
*data
;
689 data
= devm_kzalloc(&client
->dev
, sizeof(struct mp2975_data
),
694 memcpy(&data
->info
, &mp2975_info
, sizeof(*info
));
697 /* Identify multiphase configuration for rail 2. */
698 ret
= mp2975_identify_multiphase_rail2(client
);
703 /* Two rails are connected. */
704 data
->info
.pages
= MP2975_PAGE_NUM
;
705 data
->info
.phases
[1] = ret
;
706 data
->info
.func
[1] = MP2975_RAIL2_FUNC
;
709 /* Identify multiphase configuration. */
710 ret
= mp2975_identify_multiphase(client
, data
, info
);
714 /* Identify VID setting per rail. */
715 ret
= mp2975_identify_rails_vid(client
, data
, info
);
719 /* Obtain current sense gain of power stage. */
720 ret
= mp2975_current_sense_gain_get(client
, data
);
724 /* Obtain voltage reference values. */
725 ret
= mp2975_vref_get(client
, data
, info
);
729 /* Obtain vout over-voltage scales. */
730 ret
= mp2975_vout_ov_scale_get(client
, data
, info
);
734 /* Obtain offsets, maximum and format for vout. */
735 ret
= mp2975_vout_per_rail_config_get(client
, data
, info
);
739 return pmbus_do_probe(client
, info
);
742 static const struct i2c_device_id mp2975_id
[] = {
747 MODULE_DEVICE_TABLE(i2c
, mp2975_id
);
749 static const struct of_device_id __maybe_unused mp2975_of_match
[] = {
750 {.compatible
= "mps,mp2975"},
753 MODULE_DEVICE_TABLE(of
, mp2975_of_match
);
755 static struct i2c_driver mp2975_driver
= {
758 .of_match_table
= of_match_ptr(mp2975_of_match
),
760 .probe_new
= mp2975_probe
,
761 .id_table
= mp2975_id
,
764 module_i2c_driver(mp2975_driver
);
766 MODULE_AUTHOR("Vadim Pasternak <vadimp@nvidia.com>");
767 MODULE_DESCRIPTION("PMBus driver for MPS MP2975 device");
768 MODULE_LICENSE("GPL");