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>
13 #include <linux/of_device.h>
16 /* Vendor specific registers. */
17 #define MP2975_MFR_APS_HYS_R2 0x0d
18 #define MP2975_MFR_SLOPE_TRIM3 0x1d
19 #define MP2975_MFR_VR_MULTI_CONFIG_R1 0x0d
20 #define MP2975_MFR_VR_MULTI_CONFIG_R2 0x1d
21 #define MP2975_MFR_APS_DECAY_ADV 0x56
22 #define MP2975_MFR_DC_LOOP_CTRL 0x59
23 #define MP2975_MFR_OCP_UCP_PHASE_SET 0x65
24 #define MP2975_MFR_VR_CONFIG1 0x68
25 #define MP2975_MFR_READ_CS1_2 0x82
26 #define MP2975_MFR_READ_CS3_4 0x83
27 #define MP2975_MFR_READ_CS5_6 0x84
28 #define MP2975_MFR_READ_CS7_8 0x85
29 #define MP2975_MFR_READ_CS9_10 0x86
30 #define MP2975_MFR_READ_CS11_12 0x87
31 #define MP2975_MFR_READ_IOUT_PK 0x90
32 #define MP2975_MFR_READ_POUT_PK 0x91
33 #define MP2975_MFR_READ_VREF_R1 0xa1
34 #define MP2975_MFR_READ_VREF_R2 0xa3
35 #define MP2975_MFR_OVP_TH_SET 0xe5
36 #define MP2975_MFR_UVP_SET 0xe6
38 #define MP2975_VOUT_FORMAT BIT(15)
39 #define MP2975_VID_STEP_SEL_R1 BIT(4)
40 #define MP2975_IMVP9_EN_R1 BIT(13)
41 #define MP2975_VID_STEP_SEL_R2 BIT(3)
42 #define MP2975_IMVP9_EN_R2 BIT(12)
43 #define MP2975_PRT_THRES_DIV_OV_EN BIT(14)
44 #define MP2975_DRMOS_KCS GENMASK(13, 12)
45 #define MP2975_PROT_DEV_OV_OFF 10
46 #define MP2975_PROT_DEV_OV_ON 5
47 #define MP2975_SENSE_AMPL BIT(11)
48 #define MP2975_SENSE_AMPL_UNIT 1
49 #define MP2975_SENSE_AMPL_HALF 2
50 #define MP2975_VIN_UV_LIMIT_UNIT 8
52 #define MP2975_MAX_PHASE_RAIL1 8
53 #define MP2975_MAX_PHASE_RAIL2 4
54 #define MP2975_PAGE_NUM 2
56 #define MP2975_RAIL2_FUNC (PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | \
57 PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | \
58 PMBUS_HAVE_POUT | PMBUS_PHASE_VIRTUAL)
65 struct pmbus_driver_info info;
68 int vid_step[MP2975_PAGE_NUM];
69 int vref[MP2975_PAGE_NUM];
70 int vref_off[MP2975_PAGE_NUM];
71 int vout_max[MP2975_PAGE_NUM];
72 int vout_ov_fixed[MP2975_PAGE_NUM];
73 int curr_sense_gain[MP2975_PAGE_NUM];
76 static const struct i2c_device_id mp2975_id[] = {
81 MODULE_DEVICE_TABLE(i2c, mp2975_id);
83 #define to_mp2975_data(x) container_of(x, struct mp2975_data, info)
86 mp2975_read_word_helper(struct i2c_client *client, int page, int phase, u8 reg,
89 int ret = pmbus_read_word_data(client, page, phase, reg);
91 return (ret > 0) ? ret & mask : ret;
95 mp2975_vid2direct(int vrf, int val)
100 return 250 + (val - 1) * 5;
104 return 500 + (val - 1) * 10;
108 return 200 + (val - 1) * 10;
117 mp2975_read_phase(struct i2c_client *client, struct mp2975_data *data,
118 int page, int phase, u8 reg)
122 ret = pmbus_read_word_data(client, page, phase, reg);
126 if (!((phase + 1) % MP2975_PAGE_NUM))
131 * Output value is calculated as: (READ_CSx / 80 – 1.23) / (Kcs * Rcs)
133 * - Kcs is the DrMOS current sense gain of power stage, which is
134 * obtained from the register MP2975_MFR_VR_CONFIG1, bits 13-12 with
135 * the following selection of DrMOS (data->curr_sense_gain[page]):
136 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A.
137 * - Rcs is the internal phase current sense resistor which is constant
140 ph_curr = ret * 100 - 9800;
143 * Current phase sensing, providing by the device is not accurate
144 * for the light load. This because sampling of current occurrence of
145 * bit weight has a big deviation for light load. For handling such
146 * case phase current is represented as the maximum between the value
147 * calculated above and total rail current divided by number phases.
149 ret = pmbus_read_word_data(client, page, phase, PMBUS_READ_IOUT);
153 return max_t(int, DIV_ROUND_CLOSEST(ret, data->info.phases[page]),
154 DIV_ROUND_CLOSEST(ph_curr, data->curr_sense_gain[page]));
158 mp2975_read_phases(struct i2c_client *client, struct mp2975_data *data,
166 ret = mp2975_read_phase(client, data, page, phase,
167 MP2975_MFR_READ_CS7_8);
170 ret = mp2975_read_phase(client, data, page, phase,
171 MP2975_MFR_READ_CS9_10);
174 ret = mp2975_read_phase(client, data, page, phase,
175 MP2975_MFR_READ_CS11_12);
183 ret = mp2975_read_phase(client, data, page, phase,
184 MP2975_MFR_READ_CS1_2);
187 ret = mp2975_read_phase(client, data, page, phase,
188 MP2975_MFR_READ_CS3_4);
191 ret = mp2975_read_phase(client, data, page, phase,
192 MP2975_MFR_READ_CS5_6);
195 ret = mp2975_read_phase(client, data, page, phase,
196 MP2975_MFR_READ_CS7_8);
199 ret = mp2975_read_phase(client, data, page, phase,
200 MP2975_MFR_READ_CS9_10);
203 ret = mp2975_read_phase(client, data, page, phase,
204 MP2975_MFR_READ_CS11_12);
213 static int mp2975_read_word_data(struct i2c_client *client, int page,
216 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
217 struct mp2975_data *data = to_mp2975_data(info);
221 case PMBUS_OT_FAULT_LIMIT:
222 ret = mp2975_read_word_helper(client, page, phase, reg,
225 case PMBUS_VIN_OV_FAULT_LIMIT:
226 ret = mp2975_read_word_helper(client, page, phase, reg,
231 ret = DIV_ROUND_CLOSEST(ret, MP2975_VIN_UV_LIMIT_UNIT);
233 case PMBUS_VOUT_OV_FAULT_LIMIT:
235 * Register provides two values for over-voltage protection
236 * threshold for fixed (ovp2) and tracking (ovp1) modes. The
237 * minimum of these two values is provided as over-voltage
240 ret = mp2975_read_word_helper(client, page, phase,
241 MP2975_MFR_OVP_TH_SET,
246 ret = min_t(int, data->vout_max[page] + 50 * (ret + 1),
247 data->vout_ov_fixed[page]);
249 case PMBUS_VOUT_UV_FAULT_LIMIT:
250 ret = mp2975_read_word_helper(client, page, phase,
256 ret = DIV_ROUND_CLOSEST(data->vref[page] * 10 - 50 *
257 (ret + 1) * data->vout_scale, 10);
259 case PMBUS_VIRT_READ_POUT_MAX:
260 ret = mp2975_read_word_helper(client, page, phase,
261 MP2975_MFR_READ_POUT_PK,
266 ret = DIV_ROUND_CLOSEST(ret, 4);
268 case PMBUS_VIRT_READ_IOUT_MAX:
269 ret = mp2975_read_word_helper(client, page, phase,
270 MP2975_MFR_READ_IOUT_PK,
275 ret = DIV_ROUND_CLOSEST(ret, 4);
277 case PMBUS_READ_IOUT:
278 ret = mp2975_read_phases(client, data, page, phase);
283 case PMBUS_UT_WARN_LIMIT:
284 case PMBUS_UT_FAULT_LIMIT:
285 case PMBUS_VIN_UV_WARN_LIMIT:
286 case PMBUS_VIN_UV_FAULT_LIMIT:
287 case PMBUS_VOUT_UV_WARN_LIMIT:
288 case PMBUS_VOUT_OV_WARN_LIMIT:
289 case PMBUS_VIN_OV_WARN_LIMIT:
290 case PMBUS_IIN_OC_FAULT_LIMIT:
291 case PMBUS_IOUT_OC_LV_FAULT_LIMIT:
292 case PMBUS_IIN_OC_WARN_LIMIT:
293 case PMBUS_IOUT_OC_WARN_LIMIT:
294 case PMBUS_IOUT_OC_FAULT_LIMIT:
295 case PMBUS_IOUT_UC_FAULT_LIMIT:
296 case PMBUS_POUT_OP_FAULT_LIMIT:
297 case PMBUS_POUT_OP_WARN_LIMIT:
298 case PMBUS_PIN_OP_WARN_LIMIT:
307 static int mp2975_identify_multiphase_rail2(struct i2c_client *client)
312 * Identify multiphase for rail 2 - could be from 0 to 4.
313 * In case phase number is zero – only page zero is supported
315 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
319 /* Identify multiphase for rail 2 - could be from 0 to 4. */
320 ret = i2c_smbus_read_word_data(client, MP2975_MFR_VR_MULTI_CONFIG_R2);
324 ret &= GENMASK(2, 0);
325 return (ret >= 4) ? 4 : ret;
328 static void mp2975_set_phase_rail1(struct pmbus_driver_info *info)
332 for (i = 0 ; i < info->phases[0]; i++)
333 info->pfunc[i] = PMBUS_HAVE_IOUT;
337 mp2975_set_phase_rail2(struct pmbus_driver_info *info, int num_phases)
341 /* Set phases for rail 2 from upper to lower. */
342 for (i = 1; i <= num_phases; i++)
343 info->pfunc[MP2975_MAX_PHASE_RAIL1 - i] = PMBUS_HAVE_IOUT;
347 mp2975_identify_multiphase(struct i2c_client *client, struct mp2975_data *data,
348 struct pmbus_driver_info *info)
350 int num_phases2, ret;
352 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
356 /* Identify multiphase for rail 1 - could be from 1 to 8. */
357 ret = i2c_smbus_read_word_data(client, MP2975_MFR_VR_MULTI_CONFIG_R1);
361 info->phases[0] = ret & GENMASK(3, 0);
364 * The device provides a total of 8 PWM pins, and can be configured
365 * to different phase count applications for rail 1 and rail 2.
366 * Rail 1 can be set to 8 phases, while rail 2 can only be set to 4
367 * phases at most. When rail 1’s phase count is configured as 0, rail
368 * 1 operates with 1-phase DCM. When rail 2 phase count is configured
369 * as 0, rail 2 is disabled.
371 if (info->phases[0] > MP2975_MAX_PHASE_RAIL1)
374 mp2975_set_phase_rail1(info);
375 num_phases2 = min(MP2975_MAX_PHASE_RAIL1 - info->phases[0],
376 MP2975_MAX_PHASE_RAIL2);
377 if (info->phases[1] && info->phases[1] <= num_phases2)
378 mp2975_set_phase_rail2(info, num_phases2);
384 mp2975_identify_vid(struct i2c_client *client, struct mp2975_data *data,
385 struct pmbus_driver_info *info, u32 reg, int page,
386 u32 imvp_bit, u32 vr_bit)
390 /* Identify VID mode and step selection. */
391 ret = i2c_smbus_read_word_data(client, reg);
395 if (ret & imvp_bit) {
396 info->vrm_version[page] = imvp9;
397 data->vid_step[page] = MP2975_PROT_DEV_OV_OFF;
398 } else if (ret & vr_bit) {
399 info->vrm_version[page] = vr12;
400 data->vid_step[page] = MP2975_PROT_DEV_OV_ON;
402 info->vrm_version[page] = vr13;
403 data->vid_step[page] = MP2975_PROT_DEV_OV_OFF;
410 mp2975_identify_rails_vid(struct i2c_client *client, struct mp2975_data *data,
411 struct pmbus_driver_info *info)
415 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
419 /* Identify VID mode for rail 1. */
420 ret = mp2975_identify_vid(client, data, info,
421 MP2975_MFR_VR_MULTI_CONFIG_R1, 0,
422 MP2975_IMVP9_EN_R1, MP2975_VID_STEP_SEL_R1);
426 /* Identify VID mode for rail 2, if connected. */
428 ret = mp2975_identify_vid(client, data, info,
429 MP2975_MFR_VR_MULTI_CONFIG_R2, 1,
431 MP2975_VID_STEP_SEL_R2);
436 mp2975_current_sense_gain_get(struct i2c_client *client,
437 struct mp2975_data *data)
442 * Obtain DrMOS current sense gain of power stage from the register
443 * MP2975_MFR_VR_CONFIG1, bits 13-12. The value is selected as below:
444 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. Other
445 * values are invalid.
447 for (i = 0 ; i < data->info.pages; i++) {
448 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
451 ret = i2c_smbus_read_word_data(client,
452 MP2975_MFR_VR_CONFIG1);
456 switch ((ret & MP2975_DRMOS_KCS) >> 12) {
458 data->curr_sense_gain[i] = 50;
461 data->curr_sense_gain[i] = 85;
464 data->curr_sense_gain[i] = 97;
467 data->curr_sense_gain[i] = 100;
476 mp2975_vref_get(struct i2c_client *client, struct mp2975_data *data,
477 struct pmbus_driver_info *info)
481 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 3);
485 /* Get voltage reference value for rail 1. */
486 ret = i2c_smbus_read_word_data(client, MP2975_MFR_READ_VREF_R1);
490 data->vref[0] = ret * data->vid_step[0];
492 /* Get voltage reference value for rail 2, if connected. */
493 if (data->info.pages == MP2975_PAGE_NUM) {
494 ret = i2c_smbus_read_word_data(client, MP2975_MFR_READ_VREF_R2);
498 data->vref[1] = ret * data->vid_step[1];
504 mp2975_vref_offset_get(struct i2c_client *client, struct mp2975_data *data,
509 ret = i2c_smbus_read_word_data(client, MP2975_MFR_OVP_TH_SET);
513 switch ((ret & GENMASK(5, 3)) >> 3) {
515 data->vref_off[page] = 140;
518 data->vref_off[page] = 220;
521 data->vref_off[page] = 400;
530 mp2975_vout_max_get(struct i2c_client *client, struct mp2975_data *data,
531 struct pmbus_driver_info *info, int page)
535 /* Get maximum reference voltage of VID-DAC in VID format. */
536 ret = i2c_smbus_read_word_data(client, PMBUS_VOUT_MAX);
540 data->vout_max[page] = mp2975_vid2direct(info->vrm_version[page], ret &
546 mp2975_set_vout_format(struct i2c_client *client,
547 struct mp2975_data *data, int page)
551 ret = i2c_smbus_read_word_data(client, MP2975_MFR_DC_LOOP_CTRL);
554 /* Enable DIRECT VOUT format 1mV/LSB */
555 if (ret & MP2975_VOUT_FORMAT) {
556 ret &= ~MP2975_VOUT_FORMAT;
557 ret = i2c_smbus_write_word_data(client, MP2975_MFR_DC_LOOP_CTRL, ret);
563 mp2975_vout_ov_scale_get(struct i2c_client *client, struct mp2975_data *data,
564 struct pmbus_driver_info *info)
566 int thres_dev, sense_ampl, ret;
568 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
573 * Get divider for over- and under-voltage protection thresholds
574 * configuration from the Advanced Options of Auto Phase Shedding and
577 ret = i2c_smbus_read_word_data(client, MP2975_MFR_APS_DECAY_ADV);
580 thres_dev = ret & MP2975_PRT_THRES_DIV_OV_EN ? MP2975_PROT_DEV_OV_ON :
581 MP2975_PROT_DEV_OV_OFF;
583 /* Select the gain of remote sense amplifier. */
584 ret = i2c_smbus_read_word_data(client, PMBUS_VOUT_SCALE_LOOP);
587 sense_ampl = ret & MP2975_SENSE_AMPL ? MP2975_SENSE_AMPL_HALF :
588 MP2975_SENSE_AMPL_UNIT;
590 data->vout_scale = sense_ampl * thres_dev;
596 mp2975_vout_per_rail_config_get(struct i2c_client *client,
597 struct mp2975_data *data,
598 struct pmbus_driver_info *info)
602 for (i = 0; i < data->info.pages; i++) {
603 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
607 /* Obtain voltage reference offsets. */
608 ret = mp2975_vref_offset_get(client, data, i);
612 /* Obtain maximum voltage values. */
613 ret = mp2975_vout_max_get(client, data, info, i);
617 /* Set VOUT format for READ_VOUT command : direct. */
618 ret = mp2975_set_vout_format(client, data, i);
623 * Set over-voltage fixed value. Thresholds are provided as
624 * fixed value, and tracking value. The minimum of them are
625 * exposed as over-voltage critical threshold.
627 data->vout_ov_fixed[i] = data->vref[i] +
628 DIV_ROUND_CLOSEST(data->vref_off[i] *
636 static struct pmbus_driver_info mp2975_info = {
638 .format[PSC_VOLTAGE_IN] = linear,
639 .format[PSC_VOLTAGE_OUT] = direct,
640 .format[PSC_TEMPERATURE] = direct,
641 .format[PSC_CURRENT_IN] = linear,
642 .format[PSC_CURRENT_OUT] = direct,
643 .format[PSC_POWER] = direct,
644 .m[PSC_TEMPERATURE] = 1,
645 .m[PSC_VOLTAGE_OUT] = 1,
646 .R[PSC_VOLTAGE_OUT] = 3,
647 .m[PSC_CURRENT_OUT] = 1,
649 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
650 PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
651 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_POUT |
652 PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT | PMBUS_PHASE_VIRTUAL,
653 .read_word_data = mp2975_read_word_data,
656 static int mp2975_probe(struct i2c_client *client)
658 struct pmbus_driver_info *info;
659 struct mp2975_data *data;
662 data = devm_kzalloc(&client->dev, sizeof(struct mp2975_data),
667 if (client->dev.of_node)
668 data->chip_id = (enum chips)(unsigned long)of_device_get_match_data(&client->dev);
670 data->chip_id = i2c_match_id(mp2975_id, client)->driver_data;
672 memcpy(&data->info, &mp2975_info, sizeof(*info));
675 /* Identify multiphase configuration for rail 2. */
676 ret = mp2975_identify_multiphase_rail2(client);
681 /* Two rails are connected. */
682 data->info.pages = MP2975_PAGE_NUM;
683 data->info.phases[1] = ret;
684 data->info.func[1] = MP2975_RAIL2_FUNC;
687 /* Identify multiphase configuration. */
688 ret = mp2975_identify_multiphase(client, data, info);
692 /* Identify VID setting per rail. */
693 ret = mp2975_identify_rails_vid(client, data, info);
697 /* Obtain current sense gain of power stage. */
698 ret = mp2975_current_sense_gain_get(client, data);
702 /* Obtain voltage reference values. */
703 ret = mp2975_vref_get(client, data, info);
707 /* Obtain vout over-voltage scales. */
708 ret = mp2975_vout_ov_scale_get(client, data, info);
712 /* Obtain offsets, maximum and format for vout. */
713 ret = mp2975_vout_per_rail_config_get(client, data, info);
717 return pmbus_do_probe(client, info);
720 static const struct of_device_id __maybe_unused mp2975_of_match[] = {
721 {.compatible = "mps,mp2975", .data = (void *)mp2975},
724 MODULE_DEVICE_TABLE(of, mp2975_of_match);
726 static struct i2c_driver mp2975_driver = {
729 .of_match_table = of_match_ptr(mp2975_of_match),
731 .probe = mp2975_probe,
732 .id_table = mp2975_id,
735 module_i2c_driver(mp2975_driver);
737 MODULE_AUTHOR("Vadim Pasternak <vadimp@nvidia.com>");
738 MODULE_DESCRIPTION("PMBus driver for MPS MP2975 device");
739 MODULE_LICENSE("GPL");
740 MODULE_IMPORT_NS(PMBUS);