xref: /linux/drivers/hwmon/pmbus/mp2975.c (revision 45f154dc966322948725af1004f53a5531f03b7f)
12c6fcbb2SVadim Pasternak // SPDX-License-Identifier: GPL-2.0-or-later
22c6fcbb2SVadim Pasternak /*
32c6fcbb2SVadim Pasternak  * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers
42c6fcbb2SVadim Pasternak  *
52c6fcbb2SVadim Pasternak  * Copyright (C) 2020 Nvidia Technologies Ltd.
62c6fcbb2SVadim Pasternak  */
72c6fcbb2SVadim Pasternak 
82c6fcbb2SVadim Pasternak #include <linux/err.h>
92c6fcbb2SVadim Pasternak #include <linux/i2c.h>
102c6fcbb2SVadim Pasternak #include <linux/init.h>
112c6fcbb2SVadim Pasternak #include <linux/kernel.h>
122c6fcbb2SVadim Pasternak #include <linux/module.h>
131f6f34d0SPatrick Rudolph #include <linux/of_device.h>
142c6fcbb2SVadim Pasternak #include "pmbus.h"
152c6fcbb2SVadim Pasternak 
162c6fcbb2SVadim Pasternak /* Vendor specific registers. */
172c6fcbb2SVadim Pasternak #define MP2975_MFR_APS_HYS_R2		0x0d
182c6fcbb2SVadim Pasternak #define MP2975_MFR_SLOPE_TRIM3		0x1d
192c6fcbb2SVadim Pasternak #define MP2975_MFR_VR_MULTI_CONFIG_R1	0x0d
202c6fcbb2SVadim Pasternak #define MP2975_MFR_VR_MULTI_CONFIG_R2	0x1d
212c6fcbb2SVadim Pasternak #define MP2975_MFR_APS_DECAY_ADV	0x56
222c6fcbb2SVadim Pasternak #define MP2975_MFR_DC_LOOP_CTRL		0x59
232c6fcbb2SVadim Pasternak #define MP2975_MFR_OCP_UCP_PHASE_SET	0x65
242c6fcbb2SVadim Pasternak #define MP2975_MFR_VR_CONFIG1		0x68
252c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS1_2		0x82
262c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS3_4		0x83
272c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS5_6		0x84
282c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS7_8		0x85
292c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS9_10		0x86
302c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS11_12		0x87
312c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_IOUT_PK		0x90
322c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_POUT_PK		0x91
332c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_VREF_R1		0xa1
342c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_VREF_R2		0xa3
352c6fcbb2SVadim Pasternak #define MP2975_MFR_OVP_TH_SET		0xe5
362c6fcbb2SVadim Pasternak #define MP2975_MFR_UVP_SET		0xe6
372c6fcbb2SVadim Pasternak 
385239277eSPatrick Rudolph #define MP2973_MFR_RESO_SET		0xc7
395239277eSPatrick Rudolph 
402c6fcbb2SVadim Pasternak #define MP2975_VOUT_FORMAT		BIT(15)
412c6fcbb2SVadim Pasternak #define MP2975_VID_STEP_SEL_R1		BIT(4)
422c6fcbb2SVadim Pasternak #define MP2975_IMVP9_EN_R1		BIT(13)
432c6fcbb2SVadim Pasternak #define MP2975_VID_STEP_SEL_R2		BIT(3)
442c6fcbb2SVadim Pasternak #define MP2975_IMVP9_EN_R2		BIT(12)
452c6fcbb2SVadim Pasternak #define MP2975_PRT_THRES_DIV_OV_EN	BIT(14)
462c6fcbb2SVadim Pasternak #define MP2975_DRMOS_KCS		GENMASK(13, 12)
472c6fcbb2SVadim Pasternak #define MP2975_PROT_DEV_OV_OFF		10
482c6fcbb2SVadim Pasternak #define MP2975_PROT_DEV_OV_ON		5
492c6fcbb2SVadim Pasternak #define MP2975_SENSE_AMPL		BIT(11)
502c6fcbb2SVadim Pasternak #define MP2975_SENSE_AMPL_UNIT		1
512c6fcbb2SVadim Pasternak #define MP2975_SENSE_AMPL_HALF		2
522c6fcbb2SVadim Pasternak #define MP2975_VIN_UV_LIMIT_UNIT	8
532c6fcbb2SVadim Pasternak 
545239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_R1		GENMASK(7, 6)
555239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_R2		GENMASK(4, 3)
565239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_DIRECT_R1	BIT(7)
575239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_LINEAR_R1	BIT(6)
585239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_DIRECT_R2	BIT(4)
595239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_LINEAR_R2	BIT(3)
605239277eSPatrick Rudolph 
615239277eSPatrick Rudolph #define MP2973_MFR_VR_MULTI_CONFIG_R1	0x0d
625239277eSPatrick Rudolph #define MP2973_MFR_VR_MULTI_CONFIG_R2	0x1d
635239277eSPatrick Rudolph #define MP2973_VID_STEP_SEL_R1		BIT(4)
645239277eSPatrick Rudolph #define MP2973_IMVP9_EN_R1		BIT(14)
655239277eSPatrick Rudolph #define MP2973_VID_STEP_SEL_R2		BIT(3)
665239277eSPatrick Rudolph #define MP2973_IMVP9_EN_R2		BIT(13)
675239277eSPatrick Rudolph 
68*45f154dcSPatrick Rudolph #define MP2973_MFR_OCP_TOTAL_SET	0x5f
69*45f154dcSPatrick Rudolph #define MP2973_OCP_TOTAL_CUR_MASK	GENMASK(6, 0)
70*45f154dcSPatrick Rudolph #define MP2973_MFR_OCP_LEVEL_RES	BIT(15)
71*45f154dcSPatrick Rudolph 
725239277eSPatrick Rudolph #define MP2973_MFR_READ_IOUT_PK		0x90
735239277eSPatrick Rudolph #define MP2973_MFR_READ_POUT_PK		0x91
745239277eSPatrick Rudolph 
752c6fcbb2SVadim Pasternak #define MP2975_MAX_PHASE_RAIL1	8
762c6fcbb2SVadim Pasternak #define MP2975_MAX_PHASE_RAIL2	4
775239277eSPatrick Rudolph 
785239277eSPatrick Rudolph #define MP2973_MAX_PHASE_RAIL1	14
795239277eSPatrick Rudolph #define MP2973_MAX_PHASE_RAIL2	6
805239277eSPatrick Rudolph 
815239277eSPatrick Rudolph #define MP2971_MAX_PHASE_RAIL1	8
825239277eSPatrick Rudolph #define MP2971_MAX_PHASE_RAIL2	3
835239277eSPatrick Rudolph 
842c6fcbb2SVadim Pasternak #define MP2975_PAGE_NUM		2
852c6fcbb2SVadim Pasternak 
862c6fcbb2SVadim Pasternak #define MP2975_RAIL2_FUNC	(PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | \
872c6fcbb2SVadim Pasternak 				 PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | \
882292e2f6SVadim Pasternak 				 PMBUS_HAVE_POUT | PMBUS_PHASE_VIRTUAL)
892c6fcbb2SVadim Pasternak 
901f6f34d0SPatrick Rudolph enum chips {
915239277eSPatrick Rudolph 	mp2971, mp2973, mp2975
921f6f34d0SPatrick Rudolph };
931f6f34d0SPatrick Rudolph 
94e2c90b48SPatrick Rudolph static const int mp2975_max_phases[][MP2975_PAGE_NUM] = {
95e2c90b48SPatrick Rudolph 	[mp2975] = { MP2975_MAX_PHASE_RAIL1, MP2975_MAX_PHASE_RAIL2 },
965239277eSPatrick Rudolph 	[mp2973] = { MP2973_MAX_PHASE_RAIL1, MP2973_MAX_PHASE_RAIL2 },
975239277eSPatrick Rudolph 	[mp2971] = { MP2971_MAX_PHASE_RAIL1, MP2971_MAX_PHASE_RAIL2 },
98e2c90b48SPatrick Rudolph };
99e2c90b48SPatrick Rudolph 
1002c6fcbb2SVadim Pasternak struct mp2975_data {
1012c6fcbb2SVadim Pasternak 	struct pmbus_driver_info info;
1021f6f34d0SPatrick Rudolph 	enum chips chip_id;
1032c6fcbb2SVadim Pasternak 	int vout_scale;
104e2c90b48SPatrick Rudolph 	int max_phases[MP2975_PAGE_NUM];
1052c6fcbb2SVadim Pasternak 	int vid_step[MP2975_PAGE_NUM];
1062c6fcbb2SVadim Pasternak 	int vref[MP2975_PAGE_NUM];
1072c6fcbb2SVadim Pasternak 	int vref_off[MP2975_PAGE_NUM];
1082c6fcbb2SVadim Pasternak 	int vout_max[MP2975_PAGE_NUM];
1092c6fcbb2SVadim Pasternak 	int vout_ov_fixed[MP2975_PAGE_NUM];
1102c6fcbb2SVadim Pasternak 	int curr_sense_gain[MP2975_PAGE_NUM];
1112c6fcbb2SVadim Pasternak };
1122c6fcbb2SVadim Pasternak 
1131f6f34d0SPatrick Rudolph static const struct i2c_device_id mp2975_id[] = {
1145239277eSPatrick Rudolph 	{"mp2971", mp2971},
1155239277eSPatrick Rudolph 	{"mp2973", mp2973},
1161f6f34d0SPatrick Rudolph 	{"mp2975", mp2975},
1171f6f34d0SPatrick Rudolph 	{}
1181f6f34d0SPatrick Rudolph };
1191f6f34d0SPatrick Rudolph 
1201f6f34d0SPatrick Rudolph MODULE_DEVICE_TABLE(i2c, mp2975_id);
1211f6f34d0SPatrick Rudolph 
12288fc1efcSPatrick Rudolph static const struct regulator_desc __maybe_unused mp2975_reg_desc[] = {
12388fc1efcSPatrick Rudolph 	PMBUS_REGULATOR("vout", 0),
12488fc1efcSPatrick Rudolph 	PMBUS_REGULATOR("vout", 1),
12588fc1efcSPatrick Rudolph };
12688fc1efcSPatrick Rudolph 
1272c6fcbb2SVadim Pasternak #define to_mp2975_data(x)  container_of(x, struct mp2975_data, info)
1282c6fcbb2SVadim Pasternak 
1292c6fcbb2SVadim Pasternak static int
1302c6fcbb2SVadim Pasternak mp2975_read_word_helper(struct i2c_client *client, int page, int phase, u8 reg,
1312c6fcbb2SVadim Pasternak 			u16 mask)
1322c6fcbb2SVadim Pasternak {
1332c6fcbb2SVadim Pasternak 	int ret = pmbus_read_word_data(client, page, phase, reg);
1342c6fcbb2SVadim Pasternak 
1352c6fcbb2SVadim Pasternak 	return (ret > 0) ? ret & mask : ret;
1362c6fcbb2SVadim Pasternak }
1372c6fcbb2SVadim Pasternak 
1382c6fcbb2SVadim Pasternak static int
1392c6fcbb2SVadim Pasternak mp2975_vid2direct(int vrf, int val)
1402c6fcbb2SVadim Pasternak {
1412c6fcbb2SVadim Pasternak 	switch (vrf) {
1422c6fcbb2SVadim Pasternak 	case vr12:
1432c6fcbb2SVadim Pasternak 		if (val >= 0x01)
1442c6fcbb2SVadim Pasternak 			return 250 + (val - 1) * 5;
1452c6fcbb2SVadim Pasternak 		break;
1462c6fcbb2SVadim Pasternak 	case vr13:
1472c6fcbb2SVadim Pasternak 		if (val >= 0x01)
1482c6fcbb2SVadim Pasternak 			return 500 + (val - 1) * 10;
1492c6fcbb2SVadim Pasternak 		break;
1502c6fcbb2SVadim Pasternak 	case imvp9:
1512c6fcbb2SVadim Pasternak 		if (val >= 0x01)
1522c6fcbb2SVadim Pasternak 			return 200 + (val - 1) * 10;
1532c6fcbb2SVadim Pasternak 		break;
1542c6fcbb2SVadim Pasternak 	default:
1552c6fcbb2SVadim Pasternak 		return -EINVAL;
1562c6fcbb2SVadim Pasternak 	}
1572c6fcbb2SVadim Pasternak 	return 0;
1582c6fcbb2SVadim Pasternak }
1592c6fcbb2SVadim Pasternak 
160*45f154dcSPatrick Rudolph #define MAX_LIN_MANTISSA	(1023 * 1000)
161*45f154dcSPatrick Rudolph #define MIN_LIN_MANTISSA	(511 * 1000)
162*45f154dcSPatrick Rudolph 
163*45f154dcSPatrick Rudolph /* Converts a milli-unit DIRECT value to LINEAR11 format */
164*45f154dcSPatrick Rudolph static u16 mp2975_data2reg_linear11(s64 val)
165*45f154dcSPatrick Rudolph {
166*45f154dcSPatrick Rudolph 	s16 exponent = 0, mantissa;
167*45f154dcSPatrick Rudolph 	bool negative = false;
168*45f154dcSPatrick Rudolph 
169*45f154dcSPatrick Rudolph 	/* simple case */
170*45f154dcSPatrick Rudolph 	if (val == 0)
171*45f154dcSPatrick Rudolph 		return 0;
172*45f154dcSPatrick Rudolph 
173*45f154dcSPatrick Rudolph 	/* Reduce large mantissa until it fits into 10 bit */
174*45f154dcSPatrick Rudolph 	while (val >= MAX_LIN_MANTISSA && exponent < 15) {
175*45f154dcSPatrick Rudolph 		exponent++;
176*45f154dcSPatrick Rudolph 		val >>= 1;
177*45f154dcSPatrick Rudolph 	}
178*45f154dcSPatrick Rudolph 	/* Increase small mantissa to improve precision */
179*45f154dcSPatrick Rudolph 	while (val < MIN_LIN_MANTISSA && exponent > -15) {
180*45f154dcSPatrick Rudolph 		exponent--;
181*45f154dcSPatrick Rudolph 		val <<= 1;
182*45f154dcSPatrick Rudolph 	}
183*45f154dcSPatrick Rudolph 
184*45f154dcSPatrick Rudolph 	/* Convert mantissa from milli-units to units */
185*45f154dcSPatrick Rudolph 	mantissa = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff);
186*45f154dcSPatrick Rudolph 
187*45f154dcSPatrick Rudolph 	/* restore sign */
188*45f154dcSPatrick Rudolph 	if (negative)
189*45f154dcSPatrick Rudolph 		mantissa = -mantissa;
190*45f154dcSPatrick Rudolph 
191*45f154dcSPatrick Rudolph 	/* Convert to 5 bit exponent, 11 bit mantissa */
192*45f154dcSPatrick Rudolph 	return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
193*45f154dcSPatrick Rudolph }
194*45f154dcSPatrick Rudolph 
1952c6fcbb2SVadim Pasternak static int
1962c6fcbb2SVadim Pasternak mp2975_read_phase(struct i2c_client *client, struct mp2975_data *data,
1972c6fcbb2SVadim Pasternak 		  int page, int phase, u8 reg)
1982c6fcbb2SVadim Pasternak {
1992c6fcbb2SVadim Pasternak 	int ph_curr, ret;
2002c6fcbb2SVadim Pasternak 
2012c6fcbb2SVadim Pasternak 	ret = pmbus_read_word_data(client, page, phase, reg);
2022c6fcbb2SVadim Pasternak 	if (ret < 0)
2032c6fcbb2SVadim Pasternak 		return ret;
2042c6fcbb2SVadim Pasternak 
2052c6fcbb2SVadim Pasternak 	if (!((phase + 1) % MP2975_PAGE_NUM))
2062c6fcbb2SVadim Pasternak 		ret >>= 8;
2072c6fcbb2SVadim Pasternak 	ret &= 0xff;
2082c6fcbb2SVadim Pasternak 
2092c6fcbb2SVadim Pasternak 	/*
2102c6fcbb2SVadim Pasternak 	 * Output value is calculated as: (READ_CSx / 80 – 1.23) / (Kcs * Rcs)
2112c6fcbb2SVadim Pasternak 	 * where:
2122c6fcbb2SVadim Pasternak 	 * - Kcs is the DrMOS current sense gain of power stage, which is
2132c6fcbb2SVadim Pasternak 	 *   obtained from the register MP2975_MFR_VR_CONFIG1, bits 13-12 with
2142c6fcbb2SVadim Pasternak 	 *   the following selection of DrMOS (data->curr_sense_gain[page]):
2152c6fcbb2SVadim Pasternak 	 *   00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A.
2162c6fcbb2SVadim Pasternak 	 * - Rcs is the internal phase current sense resistor which is constant
2172c6fcbb2SVadim Pasternak 	 *   value 1kΩ.
2182c6fcbb2SVadim Pasternak 	 */
2192c6fcbb2SVadim Pasternak 	ph_curr = ret * 100 - 9800;
2202c6fcbb2SVadim Pasternak 
2212c6fcbb2SVadim Pasternak 	/*
2222c6fcbb2SVadim Pasternak 	 * Current phase sensing, providing by the device is not accurate
2232c6fcbb2SVadim Pasternak 	 * for the light load. This because sampling of current occurrence of
2242c6fcbb2SVadim Pasternak 	 * bit weight has a big deviation for light load. For handling such
2252c6fcbb2SVadim Pasternak 	 * case phase current is represented as the maximum between the value
2262c6fcbb2SVadim Pasternak 	 * calculated  above and total rail current divided by number phases.
2272c6fcbb2SVadim Pasternak 	 */
2282c6fcbb2SVadim Pasternak 	ret = pmbus_read_word_data(client, page, phase, PMBUS_READ_IOUT);
2292c6fcbb2SVadim Pasternak 	if (ret < 0)
2302c6fcbb2SVadim Pasternak 		return ret;
2312c6fcbb2SVadim Pasternak 
2322c6fcbb2SVadim Pasternak 	return max_t(int, DIV_ROUND_CLOSEST(ret, data->info.phases[page]),
2332c6fcbb2SVadim Pasternak 		     DIV_ROUND_CLOSEST(ph_curr, data->curr_sense_gain[page]));
2342c6fcbb2SVadim Pasternak }
2352c6fcbb2SVadim Pasternak 
2362c6fcbb2SVadim Pasternak static int
2372c6fcbb2SVadim Pasternak mp2975_read_phases(struct i2c_client *client, struct mp2975_data *data,
2382c6fcbb2SVadim Pasternak 		   int page, int phase)
2392c6fcbb2SVadim Pasternak {
2402c6fcbb2SVadim Pasternak 	int ret;
2412c6fcbb2SVadim Pasternak 
2422c6fcbb2SVadim Pasternak 	if (page) {
2432c6fcbb2SVadim Pasternak 		switch (phase) {
2442c6fcbb2SVadim Pasternak 		case 0 ... 1:
2452c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2462c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS7_8);
2472c6fcbb2SVadim Pasternak 			break;
2482c6fcbb2SVadim Pasternak 		case 2 ... 3:
2492c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2502c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS9_10);
2512c6fcbb2SVadim Pasternak 			break;
2522c6fcbb2SVadim Pasternak 		case 4 ... 5:
2532c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2542c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS11_12);
2552c6fcbb2SVadim Pasternak 			break;
2562c6fcbb2SVadim Pasternak 		default:
2572c6fcbb2SVadim Pasternak 			return -ENODATA;
2582c6fcbb2SVadim Pasternak 		}
2592c6fcbb2SVadim Pasternak 	} else {
2602c6fcbb2SVadim Pasternak 		switch (phase) {
2612c6fcbb2SVadim Pasternak 		case 0 ... 1:
2622c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2632c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS1_2);
2642c6fcbb2SVadim Pasternak 			break;
2652c6fcbb2SVadim Pasternak 		case 2 ... 3:
2662c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2672c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS3_4);
2682c6fcbb2SVadim Pasternak 			break;
2692c6fcbb2SVadim Pasternak 		case 4 ... 5:
2702c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2712c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS5_6);
2722c6fcbb2SVadim Pasternak 			break;
2732c6fcbb2SVadim Pasternak 		case 6 ... 7:
2742c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2752c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS7_8);
2762c6fcbb2SVadim Pasternak 			break;
2772c6fcbb2SVadim Pasternak 		case 8 ... 9:
2782c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2792c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS9_10);
2802c6fcbb2SVadim Pasternak 			break;
2812c6fcbb2SVadim Pasternak 		case 10 ... 11:
2822c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2832c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS11_12);
2842c6fcbb2SVadim Pasternak 			break;
2852c6fcbb2SVadim Pasternak 		default:
2862c6fcbb2SVadim Pasternak 			return -ENODATA;
2872c6fcbb2SVadim Pasternak 		}
2882c6fcbb2SVadim Pasternak 	}
2892c6fcbb2SVadim Pasternak 	return ret;
2902c6fcbb2SVadim Pasternak }
2912c6fcbb2SVadim Pasternak 
2925239277eSPatrick Rudolph static int mp2973_read_word_data(struct i2c_client *client, int page,
2935239277eSPatrick Rudolph 				 int phase, int reg)
2945239277eSPatrick Rudolph {
2955239277eSPatrick Rudolph 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
2965239277eSPatrick Rudolph 	struct mp2975_data *data = to_mp2975_data(info);
2975239277eSPatrick Rudolph 	int ret;
2985239277eSPatrick Rudolph 
2995239277eSPatrick Rudolph 	switch (reg) {
3005239277eSPatrick Rudolph 	case PMBUS_OT_FAULT_LIMIT:
3015239277eSPatrick Rudolph 		ret = mp2975_read_word_helper(client, page, phase, reg,
3025239277eSPatrick Rudolph 					      GENMASK(7, 0));
3035239277eSPatrick Rudolph 		break;
3045239277eSPatrick Rudolph 	case PMBUS_VIN_OV_FAULT_LIMIT:
3055239277eSPatrick Rudolph 		ret = mp2975_read_word_helper(client, page, phase, reg,
3065239277eSPatrick Rudolph 					      GENMASK(7, 0));
3075239277eSPatrick Rudolph 		if (ret < 0)
3085239277eSPatrick Rudolph 			return ret;
3095239277eSPatrick Rudolph 
3105239277eSPatrick Rudolph 		ret = DIV_ROUND_CLOSEST(ret, MP2975_VIN_UV_LIMIT_UNIT);
3115239277eSPatrick Rudolph 		break;
3125239277eSPatrick Rudolph 	case PMBUS_VOUT_OV_FAULT_LIMIT:
3135239277eSPatrick Rudolph 		/*
3145239277eSPatrick Rudolph 		 * MP2971 and mp2973 only supports tracking (ovp1) mode.
3155239277eSPatrick Rudolph 		 */
3165239277eSPatrick Rudolph 		ret = mp2975_read_word_helper(client, page, phase,
3175239277eSPatrick Rudolph 					      MP2975_MFR_OVP_TH_SET,
3185239277eSPatrick Rudolph 					      GENMASK(2, 0));
3195239277eSPatrick Rudolph 		if (ret < 0)
3205239277eSPatrick Rudolph 			return ret;
3215239277eSPatrick Rudolph 
3225239277eSPatrick Rudolph 		ret = data->vout_max[page] + 50 * (ret + 1);
3235239277eSPatrick Rudolph 		break;
3245239277eSPatrick Rudolph 	case PMBUS_VOUT_UV_FAULT_LIMIT:
3255239277eSPatrick Rudolph 		ret = mp2975_read_word_helper(client, page, phase, reg,
3265239277eSPatrick Rudolph 					      GENMASK(8, 0));
3275239277eSPatrick Rudolph 		if (ret < 0)
3285239277eSPatrick Rudolph 			return ret;
3295239277eSPatrick Rudolph 		ret = mp2975_vid2direct(info->vrm_version[page], ret);
3305239277eSPatrick Rudolph 		break;
3315239277eSPatrick Rudolph 	case PMBUS_VIRT_READ_POUT_MAX:
3325239277eSPatrick Rudolph 		ret = pmbus_read_word_data(client, page, phase,
3335239277eSPatrick Rudolph 					   MP2973_MFR_READ_POUT_PK);
3345239277eSPatrick Rudolph 		break;
3355239277eSPatrick Rudolph 	case PMBUS_VIRT_READ_IOUT_MAX:
3365239277eSPatrick Rudolph 		ret = pmbus_read_word_data(client, page, phase,
3375239277eSPatrick Rudolph 					   MP2973_MFR_READ_IOUT_PK);
3385239277eSPatrick Rudolph 		break;
339*45f154dcSPatrick Rudolph 	case PMBUS_IOUT_OC_FAULT_LIMIT:
340*45f154dcSPatrick Rudolph 		ret = mp2975_read_word_helper(client, page, phase,
341*45f154dcSPatrick Rudolph 					      MP2973_MFR_OCP_TOTAL_SET,
342*45f154dcSPatrick Rudolph 					      GENMASK(15, 0));
343*45f154dcSPatrick Rudolph 		if (ret < 0)
344*45f154dcSPatrick Rudolph 			return ret;
345*45f154dcSPatrick Rudolph 
346*45f154dcSPatrick Rudolph 		if (ret & MP2973_MFR_OCP_LEVEL_RES)
347*45f154dcSPatrick Rudolph 			ret = 2 * (ret & MP2973_OCP_TOTAL_CUR_MASK);
348*45f154dcSPatrick Rudolph 		else
349*45f154dcSPatrick Rudolph 			ret = ret & MP2973_OCP_TOTAL_CUR_MASK;
350*45f154dcSPatrick Rudolph 
351*45f154dcSPatrick Rudolph 		ret = mp2975_data2reg_linear11(ret * info->phases[page] * 1000);
352*45f154dcSPatrick Rudolph 		break;
3535239277eSPatrick Rudolph 	case PMBUS_UT_WARN_LIMIT:
3545239277eSPatrick Rudolph 	case PMBUS_UT_FAULT_LIMIT:
3555239277eSPatrick Rudolph 	case PMBUS_VIN_UV_WARN_LIMIT:
3565239277eSPatrick Rudolph 	case PMBUS_VIN_UV_FAULT_LIMIT:
3575239277eSPatrick Rudolph 	case PMBUS_VOUT_UV_WARN_LIMIT:
3585239277eSPatrick Rudolph 	case PMBUS_VOUT_OV_WARN_LIMIT:
3595239277eSPatrick Rudolph 	case PMBUS_VIN_OV_WARN_LIMIT:
3605239277eSPatrick Rudolph 	case PMBUS_IIN_OC_FAULT_LIMIT:
3615239277eSPatrick Rudolph 	case PMBUS_IOUT_OC_LV_FAULT_LIMIT:
3625239277eSPatrick Rudolph 	case PMBUS_IOUT_OC_WARN_LIMIT:
3635239277eSPatrick Rudolph 	case PMBUS_IOUT_UC_FAULT_LIMIT:
3645239277eSPatrick Rudolph 	case PMBUS_POUT_OP_FAULT_LIMIT:
3655239277eSPatrick Rudolph 	case PMBUS_POUT_OP_WARN_LIMIT:
3665239277eSPatrick Rudolph 	case PMBUS_PIN_OP_WARN_LIMIT:
3675239277eSPatrick Rudolph 		return -ENXIO;
3685239277eSPatrick Rudolph 	default:
3695239277eSPatrick Rudolph 		return -ENODATA;
3705239277eSPatrick Rudolph 	}
3715239277eSPatrick Rudolph 
3725239277eSPatrick Rudolph 	return ret;
3735239277eSPatrick Rudolph }
3745239277eSPatrick Rudolph 
3752c6fcbb2SVadim Pasternak static int mp2975_read_word_data(struct i2c_client *client, int page,
3762c6fcbb2SVadim Pasternak 				 int phase, int reg)
3772c6fcbb2SVadim Pasternak {
3782c6fcbb2SVadim Pasternak 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
3792c6fcbb2SVadim Pasternak 	struct mp2975_data *data = to_mp2975_data(info);
3802c6fcbb2SVadim Pasternak 	int ret;
3812c6fcbb2SVadim Pasternak 
3822c6fcbb2SVadim Pasternak 	switch (reg) {
3832c6fcbb2SVadim Pasternak 	case PMBUS_OT_FAULT_LIMIT:
3842c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase, reg,
3852c6fcbb2SVadim Pasternak 					      GENMASK(7, 0));
3862c6fcbb2SVadim Pasternak 		break;
3872c6fcbb2SVadim Pasternak 	case PMBUS_VIN_OV_FAULT_LIMIT:
3882c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase, reg,
3892c6fcbb2SVadim Pasternak 					      GENMASK(7, 0));
3902c6fcbb2SVadim Pasternak 		if (ret < 0)
3912c6fcbb2SVadim Pasternak 			return ret;
3922c6fcbb2SVadim Pasternak 
3932c6fcbb2SVadim Pasternak 		ret = DIV_ROUND_CLOSEST(ret, MP2975_VIN_UV_LIMIT_UNIT);
3942c6fcbb2SVadim Pasternak 		break;
3952c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_OV_FAULT_LIMIT:
3962c6fcbb2SVadim Pasternak 		/*
3972c6fcbb2SVadim Pasternak 		 * Register provides two values for over-voltage protection
3982c6fcbb2SVadim Pasternak 		 * threshold for fixed (ovp2) and tracking (ovp1) modes. The
3992c6fcbb2SVadim Pasternak 		 * minimum of these two values is provided as over-voltage
4002c6fcbb2SVadim Pasternak 		 * fault alarm.
4012c6fcbb2SVadim Pasternak 		 */
4022c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase,
4032c6fcbb2SVadim Pasternak 					      MP2975_MFR_OVP_TH_SET,
4042c6fcbb2SVadim Pasternak 					      GENMASK(2, 0));
4052c6fcbb2SVadim Pasternak 		if (ret < 0)
4062c6fcbb2SVadim Pasternak 			return ret;
4072c6fcbb2SVadim Pasternak 
4082c6fcbb2SVadim Pasternak 		ret = min_t(int, data->vout_max[page] + 50 * (ret + 1),
4092c6fcbb2SVadim Pasternak 			    data->vout_ov_fixed[page]);
4102c6fcbb2SVadim Pasternak 		break;
4112c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_UV_FAULT_LIMIT:
4122c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase,
4132c6fcbb2SVadim Pasternak 					      MP2975_MFR_UVP_SET,
4142c6fcbb2SVadim Pasternak 					      GENMASK(2, 0));
4152c6fcbb2SVadim Pasternak 		if (ret < 0)
4162c6fcbb2SVadim Pasternak 			return ret;
4172c6fcbb2SVadim Pasternak 
4182c6fcbb2SVadim Pasternak 		ret = DIV_ROUND_CLOSEST(data->vref[page] * 10 - 50 *
4192c6fcbb2SVadim Pasternak 					(ret + 1) * data->vout_scale, 10);
4202c6fcbb2SVadim Pasternak 		break;
4212c6fcbb2SVadim Pasternak 	case PMBUS_VIRT_READ_POUT_MAX:
4222c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase,
4232c6fcbb2SVadim Pasternak 					      MP2975_MFR_READ_POUT_PK,
4242c6fcbb2SVadim Pasternak 					      GENMASK(12, 0));
4252c6fcbb2SVadim Pasternak 		if (ret < 0)
4262c6fcbb2SVadim Pasternak 			return ret;
4272c6fcbb2SVadim Pasternak 
4282c6fcbb2SVadim Pasternak 		ret = DIV_ROUND_CLOSEST(ret, 4);
4292c6fcbb2SVadim Pasternak 		break;
4302c6fcbb2SVadim Pasternak 	case PMBUS_VIRT_READ_IOUT_MAX:
4312c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase,
4322c6fcbb2SVadim Pasternak 					      MP2975_MFR_READ_IOUT_PK,
4332c6fcbb2SVadim Pasternak 					      GENMASK(12, 0));
4342c6fcbb2SVadim Pasternak 		if (ret < 0)
4352c6fcbb2SVadim Pasternak 			return ret;
4362c6fcbb2SVadim Pasternak 
4372c6fcbb2SVadim Pasternak 		ret = DIV_ROUND_CLOSEST(ret, 4);
4382c6fcbb2SVadim Pasternak 		break;
4392c6fcbb2SVadim Pasternak 	case PMBUS_READ_IOUT:
4402c6fcbb2SVadim Pasternak 		ret = mp2975_read_phases(client, data, page, phase);
4412c6fcbb2SVadim Pasternak 		if (ret < 0)
4422c6fcbb2SVadim Pasternak 			return ret;
4432c6fcbb2SVadim Pasternak 
4442c6fcbb2SVadim Pasternak 		break;
4452c6fcbb2SVadim Pasternak 	case PMBUS_UT_WARN_LIMIT:
4462c6fcbb2SVadim Pasternak 	case PMBUS_UT_FAULT_LIMIT:
4472c6fcbb2SVadim Pasternak 	case PMBUS_VIN_UV_WARN_LIMIT:
4482c6fcbb2SVadim Pasternak 	case PMBUS_VIN_UV_FAULT_LIMIT:
4492c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_UV_WARN_LIMIT:
4502c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_OV_WARN_LIMIT:
4512c6fcbb2SVadim Pasternak 	case PMBUS_VIN_OV_WARN_LIMIT:
4522c6fcbb2SVadim Pasternak 	case PMBUS_IIN_OC_FAULT_LIMIT:
4532c6fcbb2SVadim Pasternak 	case PMBUS_IOUT_OC_LV_FAULT_LIMIT:
4542c6fcbb2SVadim Pasternak 	case PMBUS_IIN_OC_WARN_LIMIT:
4552c6fcbb2SVadim Pasternak 	case PMBUS_IOUT_OC_WARN_LIMIT:
4562c6fcbb2SVadim Pasternak 	case PMBUS_IOUT_OC_FAULT_LIMIT:
4572c6fcbb2SVadim Pasternak 	case PMBUS_IOUT_UC_FAULT_LIMIT:
4582c6fcbb2SVadim Pasternak 	case PMBUS_POUT_OP_FAULT_LIMIT:
4592c6fcbb2SVadim Pasternak 	case PMBUS_POUT_OP_WARN_LIMIT:
4602c6fcbb2SVadim Pasternak 	case PMBUS_PIN_OP_WARN_LIMIT:
4612c6fcbb2SVadim Pasternak 		return -ENXIO;
4622c6fcbb2SVadim Pasternak 	default:
4632c6fcbb2SVadim Pasternak 		return -ENODATA;
4642c6fcbb2SVadim Pasternak 	}
4652c6fcbb2SVadim Pasternak 
4662c6fcbb2SVadim Pasternak 	return ret;
4672c6fcbb2SVadim Pasternak }
4682c6fcbb2SVadim Pasternak 
469e2c90b48SPatrick Rudolph static int mp2975_identify_multiphase_rail2(struct i2c_client *client,
470e2c90b48SPatrick Rudolph 					    struct mp2975_data *data)
4712c6fcbb2SVadim Pasternak {
4722c6fcbb2SVadim Pasternak 	int ret;
4732c6fcbb2SVadim Pasternak 
4742c6fcbb2SVadim Pasternak 	/*
475e2c90b48SPatrick Rudolph 	 * Identify multiphase for rail 2 - could be from 0 to data->max_phases[1].
4762c6fcbb2SVadim Pasternak 	 * In case phase number is zero – only page zero is supported
4772c6fcbb2SVadim Pasternak 	 */
4782c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
4792c6fcbb2SVadim Pasternak 	if (ret < 0)
4802c6fcbb2SVadim Pasternak 		return ret;
4812c6fcbb2SVadim Pasternak 
4822c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_VR_MULTI_CONFIG_R2);
4832c6fcbb2SVadim Pasternak 	if (ret < 0)
4842c6fcbb2SVadim Pasternak 		return ret;
4852c6fcbb2SVadim Pasternak 
4862c6fcbb2SVadim Pasternak 	ret &= GENMASK(2, 0);
487e2c90b48SPatrick Rudolph 	return (ret >= data->max_phases[1]) ? data->max_phases[1] : ret;
4882c6fcbb2SVadim Pasternak }
4892c6fcbb2SVadim Pasternak 
4902c6fcbb2SVadim Pasternak static void mp2975_set_phase_rail1(struct pmbus_driver_info *info)
4912c6fcbb2SVadim Pasternak {
4922c6fcbb2SVadim Pasternak 	int i;
4932c6fcbb2SVadim Pasternak 
4942c6fcbb2SVadim Pasternak 	for (i = 0 ; i < info->phases[0]; i++)
4952c6fcbb2SVadim Pasternak 		info->pfunc[i] = PMBUS_HAVE_IOUT;
4962c6fcbb2SVadim Pasternak }
4972c6fcbb2SVadim Pasternak 
4982c6fcbb2SVadim Pasternak static void
4992c6fcbb2SVadim Pasternak mp2975_set_phase_rail2(struct pmbus_driver_info *info, int num_phases)
5002c6fcbb2SVadim Pasternak {
5012c6fcbb2SVadim Pasternak 	int i;
5022c6fcbb2SVadim Pasternak 
5032c6fcbb2SVadim Pasternak 	/* Set phases for rail 2 from upper to lower. */
5042c6fcbb2SVadim Pasternak 	for (i = 1; i <= num_phases; i++)
5052c6fcbb2SVadim Pasternak 		info->pfunc[MP2975_MAX_PHASE_RAIL1 - i] = PMBUS_HAVE_IOUT;
5062c6fcbb2SVadim Pasternak }
5072c6fcbb2SVadim Pasternak 
5082c6fcbb2SVadim Pasternak static int
5092c6fcbb2SVadim Pasternak mp2975_identify_multiphase(struct i2c_client *client, struct mp2975_data *data,
5102c6fcbb2SVadim Pasternak 			   struct pmbus_driver_info *info)
5112c6fcbb2SVadim Pasternak {
5122c6fcbb2SVadim Pasternak 	int num_phases2, ret;
5132c6fcbb2SVadim Pasternak 
5142c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
5152c6fcbb2SVadim Pasternak 	if (ret < 0)
5162c6fcbb2SVadim Pasternak 		return ret;
5172c6fcbb2SVadim Pasternak 
518e2c90b48SPatrick Rudolph 	/* Identify multiphase for rail 1 - could be from 1 to data->max_phases[0]. */
5192c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_VR_MULTI_CONFIG_R1);
5202c6fcbb2SVadim Pasternak 	if (ret <= 0)
5212c6fcbb2SVadim Pasternak 		return ret;
5222c6fcbb2SVadim Pasternak 
5232c6fcbb2SVadim Pasternak 	info->phases[0] = ret & GENMASK(3, 0);
5242c6fcbb2SVadim Pasternak 
5252c6fcbb2SVadim Pasternak 	/*
526e2c90b48SPatrick Rudolph 	 * The device provides a total of $n PWM pins, and can be configured
5272c6fcbb2SVadim Pasternak 	 * to different phase count applications for rail 1 and rail 2.
528e2c90b48SPatrick Rudolph 	 * Rail 1 can be set to $n phases, while rail 2 can be set to less than
529e2c90b48SPatrick Rudolph 	 * that. When rail 1’s phase count is configured as 0, rail
5302c6fcbb2SVadim Pasternak 	 * 1 operates with 1-phase DCM. When rail 2 phase count is configured
5312c6fcbb2SVadim Pasternak 	 * as 0, rail 2 is disabled.
5322c6fcbb2SVadim Pasternak 	 */
533e2c90b48SPatrick Rudolph 	if (info->phases[0] > data->max_phases[0])
5342c6fcbb2SVadim Pasternak 		return -EINVAL;
5352c6fcbb2SVadim Pasternak 
536*45f154dcSPatrick Rudolph 	if (data->chip_id == mp2975) {
5372c6fcbb2SVadim Pasternak 		mp2975_set_phase_rail1(info);
538e2c90b48SPatrick Rudolph 		num_phases2 = min(data->max_phases[0] - info->phases[0],
539e2c90b48SPatrick Rudolph 				  data->max_phases[1]);
5402c6fcbb2SVadim Pasternak 		if (info->phases[1] && info->phases[1] <= num_phases2)
5412c6fcbb2SVadim Pasternak 			mp2975_set_phase_rail2(info, num_phases2);
542*45f154dcSPatrick Rudolph 	}
5432c6fcbb2SVadim Pasternak 
5442c6fcbb2SVadim Pasternak 	return 0;
5452c6fcbb2SVadim Pasternak }
5462c6fcbb2SVadim Pasternak 
5472c6fcbb2SVadim Pasternak static int
5482c6fcbb2SVadim Pasternak mp2975_identify_vid(struct i2c_client *client, struct mp2975_data *data,
5492c6fcbb2SVadim Pasternak 		    struct pmbus_driver_info *info, u32 reg, int page,
5502c6fcbb2SVadim Pasternak 		    u32 imvp_bit, u32 vr_bit)
5512c6fcbb2SVadim Pasternak {
5522c6fcbb2SVadim Pasternak 	int ret;
5532c6fcbb2SVadim Pasternak 
5542c6fcbb2SVadim Pasternak 	/* Identify VID mode and step selection. */
5552c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, reg);
5562c6fcbb2SVadim Pasternak 	if (ret < 0)
5572c6fcbb2SVadim Pasternak 		return ret;
5582c6fcbb2SVadim Pasternak 
5592c6fcbb2SVadim Pasternak 	if (ret & imvp_bit) {
5602c6fcbb2SVadim Pasternak 		info->vrm_version[page] = imvp9;
5612c6fcbb2SVadim Pasternak 		data->vid_step[page] = MP2975_PROT_DEV_OV_OFF;
5622c6fcbb2SVadim Pasternak 	} else if (ret & vr_bit) {
5632c6fcbb2SVadim Pasternak 		info->vrm_version[page] = vr12;
5642c6fcbb2SVadim Pasternak 		data->vid_step[page] = MP2975_PROT_DEV_OV_ON;
5652c6fcbb2SVadim Pasternak 	} else {
5662c6fcbb2SVadim Pasternak 		info->vrm_version[page] = vr13;
5672c6fcbb2SVadim Pasternak 		data->vid_step[page] = MP2975_PROT_DEV_OV_OFF;
5682c6fcbb2SVadim Pasternak 	}
5692c6fcbb2SVadim Pasternak 
5702c6fcbb2SVadim Pasternak 	return 0;
5712c6fcbb2SVadim Pasternak }
5722c6fcbb2SVadim Pasternak 
5732c6fcbb2SVadim Pasternak static int
5742c6fcbb2SVadim Pasternak mp2975_identify_rails_vid(struct i2c_client *client, struct mp2975_data *data,
5752c6fcbb2SVadim Pasternak 			  struct pmbus_driver_info *info)
5762c6fcbb2SVadim Pasternak {
5772c6fcbb2SVadim Pasternak 	int ret;
5782c6fcbb2SVadim Pasternak 
5792c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
5802c6fcbb2SVadim Pasternak 	if (ret < 0)
5812c6fcbb2SVadim Pasternak 		return ret;
5822c6fcbb2SVadim Pasternak 
5832c6fcbb2SVadim Pasternak 	/* Identify VID mode for rail 1. */
5842c6fcbb2SVadim Pasternak 	ret = mp2975_identify_vid(client, data, info,
5852c6fcbb2SVadim Pasternak 				  MP2975_MFR_VR_MULTI_CONFIG_R1, 0,
5862c6fcbb2SVadim Pasternak 				  MP2975_IMVP9_EN_R1, MP2975_VID_STEP_SEL_R1);
5872c6fcbb2SVadim Pasternak 	if (ret < 0)
5882c6fcbb2SVadim Pasternak 		return ret;
5892c6fcbb2SVadim Pasternak 
5902c6fcbb2SVadim Pasternak 	/* Identify VID mode for rail 2, if connected. */
5912c6fcbb2SVadim Pasternak 	if (info->phases[1])
5922c6fcbb2SVadim Pasternak 		ret = mp2975_identify_vid(client, data, info,
5932c6fcbb2SVadim Pasternak 					  MP2975_MFR_VR_MULTI_CONFIG_R2, 1,
5942c6fcbb2SVadim Pasternak 					  MP2975_IMVP9_EN_R2,
5952c6fcbb2SVadim Pasternak 					  MP2975_VID_STEP_SEL_R2);
5965239277eSPatrick Rudolph 
5975239277eSPatrick Rudolph 	return ret;
5985239277eSPatrick Rudolph }
5995239277eSPatrick Rudolph 
6005239277eSPatrick Rudolph static int
6015239277eSPatrick Rudolph mp2973_identify_rails_vid(struct i2c_client *client, struct mp2975_data *data,
6025239277eSPatrick Rudolph 			  struct pmbus_driver_info *info)
6035239277eSPatrick Rudolph {
6045239277eSPatrick Rudolph 	int ret;
6055239277eSPatrick Rudolph 
6065239277eSPatrick Rudolph 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
6075239277eSPatrick Rudolph 	if (ret < 0)
6085239277eSPatrick Rudolph 		return ret;
6095239277eSPatrick Rudolph 
6105239277eSPatrick Rudolph 	/* Identify VID mode for rail 1. */
6115239277eSPatrick Rudolph 	ret = mp2975_identify_vid(client, data, info,
6125239277eSPatrick Rudolph 				  MP2973_MFR_VR_MULTI_CONFIG_R1, 0,
6135239277eSPatrick Rudolph 				  MP2973_IMVP9_EN_R1, MP2973_VID_STEP_SEL_R1);
6145239277eSPatrick Rudolph 
6155239277eSPatrick Rudolph 	if (ret < 0)
6165239277eSPatrick Rudolph 		return ret;
6175239277eSPatrick Rudolph 
6185239277eSPatrick Rudolph 	/* Identify VID mode for rail 2, if connected. */
6195239277eSPatrick Rudolph 	if (info->phases[1])
6205239277eSPatrick Rudolph 		ret = mp2975_identify_vid(client, data, info,
6215239277eSPatrick Rudolph 					  MP2973_MFR_VR_MULTI_CONFIG_R2, 1,
6225239277eSPatrick Rudolph 					  MP2973_IMVP9_EN_R2,
6235239277eSPatrick Rudolph 					  MP2973_VID_STEP_SEL_R2);
6245239277eSPatrick Rudolph 
6252c6fcbb2SVadim Pasternak 	return ret;
6262c6fcbb2SVadim Pasternak }
6272c6fcbb2SVadim Pasternak 
6282c6fcbb2SVadim Pasternak static int
6292c6fcbb2SVadim Pasternak mp2975_current_sense_gain_get(struct i2c_client *client,
6302c6fcbb2SVadim Pasternak 			      struct mp2975_data *data)
6312c6fcbb2SVadim Pasternak {
6322c6fcbb2SVadim Pasternak 	int i, ret;
6332c6fcbb2SVadim Pasternak 
6342c6fcbb2SVadim Pasternak 	/*
6352c6fcbb2SVadim Pasternak 	 * Obtain DrMOS current sense gain of power stage from the register
6362c6fcbb2SVadim Pasternak 	 * MP2975_MFR_VR_CONFIG1, bits 13-12. The value is selected as below:
6372c6fcbb2SVadim Pasternak 	 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. Other
6382c6fcbb2SVadim Pasternak 	 * values are invalid.
6392c6fcbb2SVadim Pasternak 	 */
6402c6fcbb2SVadim Pasternak 	for (i = 0 ; i < data->info.pages; i++) {
6412c6fcbb2SVadim Pasternak 		ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
6422c6fcbb2SVadim Pasternak 		if (ret < 0)
6432c6fcbb2SVadim Pasternak 			return ret;
6442c6fcbb2SVadim Pasternak 		ret = i2c_smbus_read_word_data(client,
6452c6fcbb2SVadim Pasternak 					       MP2975_MFR_VR_CONFIG1);
6462c6fcbb2SVadim Pasternak 		if (ret < 0)
6472c6fcbb2SVadim Pasternak 			return ret;
6482c6fcbb2SVadim Pasternak 
6492c6fcbb2SVadim Pasternak 		switch ((ret & MP2975_DRMOS_KCS) >> 12) {
6502c6fcbb2SVadim Pasternak 		case 0:
6512c6fcbb2SVadim Pasternak 			data->curr_sense_gain[i] = 50;
6522c6fcbb2SVadim Pasternak 			break;
6532c6fcbb2SVadim Pasternak 		case 1:
6542c6fcbb2SVadim Pasternak 			data->curr_sense_gain[i] = 85;
6552c6fcbb2SVadim Pasternak 			break;
6562c6fcbb2SVadim Pasternak 		case 2:
6572c6fcbb2SVadim Pasternak 			data->curr_sense_gain[i] = 97;
6582c6fcbb2SVadim Pasternak 			break;
6592c6fcbb2SVadim Pasternak 		default:
6602c6fcbb2SVadim Pasternak 			data->curr_sense_gain[i] = 100;
6612c6fcbb2SVadim Pasternak 			break;
6622c6fcbb2SVadim Pasternak 		}
6632c6fcbb2SVadim Pasternak 	}
6642c6fcbb2SVadim Pasternak 
6652c6fcbb2SVadim Pasternak 	return 0;
6662c6fcbb2SVadim Pasternak }
6672c6fcbb2SVadim Pasternak 
6682c6fcbb2SVadim Pasternak static int
6692c6fcbb2SVadim Pasternak mp2975_vref_get(struct i2c_client *client, struct mp2975_data *data,
6702c6fcbb2SVadim Pasternak 		struct pmbus_driver_info *info)
6712c6fcbb2SVadim Pasternak {
6722c6fcbb2SVadim Pasternak 	int ret;
6732c6fcbb2SVadim Pasternak 
6742c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 3);
6752c6fcbb2SVadim Pasternak 	if (ret < 0)
6762c6fcbb2SVadim Pasternak 		return ret;
6772c6fcbb2SVadim Pasternak 
6782c6fcbb2SVadim Pasternak 	/* Get voltage reference value for rail 1. */
6792c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_READ_VREF_R1);
6802c6fcbb2SVadim Pasternak 	if (ret < 0)
6812c6fcbb2SVadim Pasternak 		return ret;
6822c6fcbb2SVadim Pasternak 
6832c6fcbb2SVadim Pasternak 	data->vref[0] = ret * data->vid_step[0];
6842c6fcbb2SVadim Pasternak 
6852c6fcbb2SVadim Pasternak 	/* Get voltage reference value for rail 2, if connected. */
6862c6fcbb2SVadim Pasternak 	if (data->info.pages == MP2975_PAGE_NUM) {
6872c6fcbb2SVadim Pasternak 		ret = i2c_smbus_read_word_data(client, MP2975_MFR_READ_VREF_R2);
6882c6fcbb2SVadim Pasternak 		if (ret < 0)
6892c6fcbb2SVadim Pasternak 			return ret;
6902c6fcbb2SVadim Pasternak 
6912c6fcbb2SVadim Pasternak 		data->vref[1] = ret * data->vid_step[1];
6922c6fcbb2SVadim Pasternak 	}
6932c6fcbb2SVadim Pasternak 	return 0;
6942c6fcbb2SVadim Pasternak }
6952c6fcbb2SVadim Pasternak 
6962c6fcbb2SVadim Pasternak static int
6972c6fcbb2SVadim Pasternak mp2975_vref_offset_get(struct i2c_client *client, struct mp2975_data *data,
6982c6fcbb2SVadim Pasternak 		       int page)
6992c6fcbb2SVadim Pasternak {
7002c6fcbb2SVadim Pasternak 	int ret;
7012c6fcbb2SVadim Pasternak 
7022c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_OVP_TH_SET);
7032c6fcbb2SVadim Pasternak 	if (ret < 0)
7042c6fcbb2SVadim Pasternak 		return ret;
7052c6fcbb2SVadim Pasternak 
7062c6fcbb2SVadim Pasternak 	switch ((ret & GENMASK(5, 3)) >> 3) {
7072c6fcbb2SVadim Pasternak 	case 1:
7082c6fcbb2SVadim Pasternak 		data->vref_off[page] = 140;
7092c6fcbb2SVadim Pasternak 		break;
7102c6fcbb2SVadim Pasternak 	case 2:
7112c6fcbb2SVadim Pasternak 		data->vref_off[page] = 220;
7122c6fcbb2SVadim Pasternak 		break;
7132c6fcbb2SVadim Pasternak 	case 4:
7142c6fcbb2SVadim Pasternak 		data->vref_off[page] = 400;
7152c6fcbb2SVadim Pasternak 		break;
7162c6fcbb2SVadim Pasternak 	default:
7172c6fcbb2SVadim Pasternak 		return -EINVAL;
7182c6fcbb2SVadim Pasternak 	}
7192c6fcbb2SVadim Pasternak 	return 0;
7202c6fcbb2SVadim Pasternak }
7212c6fcbb2SVadim Pasternak 
7222c6fcbb2SVadim Pasternak static int
7232c6fcbb2SVadim Pasternak mp2975_vout_max_get(struct i2c_client *client, struct mp2975_data *data,
7242c6fcbb2SVadim Pasternak 		    struct pmbus_driver_info *info, int page)
7252c6fcbb2SVadim Pasternak {
7262c6fcbb2SVadim Pasternak 	int ret;
7272c6fcbb2SVadim Pasternak 
7282c6fcbb2SVadim Pasternak 	/* Get maximum reference voltage of VID-DAC in VID format. */
7292c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, PMBUS_VOUT_MAX);
7302c6fcbb2SVadim Pasternak 	if (ret < 0)
7312c6fcbb2SVadim Pasternak 		return ret;
7322c6fcbb2SVadim Pasternak 
7332c6fcbb2SVadim Pasternak 	data->vout_max[page] = mp2975_vid2direct(info->vrm_version[page], ret &
7342c6fcbb2SVadim Pasternak 						 GENMASK(8, 0));
7352c6fcbb2SVadim Pasternak 	return 0;
7362c6fcbb2SVadim Pasternak }
7372c6fcbb2SVadim Pasternak 
7382c6fcbb2SVadim Pasternak static int
7391feb31e8SPatrick Rudolph mp2975_set_vout_format(struct i2c_client *client,
7402c6fcbb2SVadim Pasternak 		       struct mp2975_data *data, int page)
7412c6fcbb2SVadim Pasternak {
7425239277eSPatrick Rudolph 	int ret, i;
7432c6fcbb2SVadim Pasternak 
7445239277eSPatrick Rudolph 	/* Enable DIRECT VOUT format 1mV/LSB */
7455239277eSPatrick Rudolph 	if (data->chip_id == mp2975) {
7462c6fcbb2SVadim Pasternak 		ret = i2c_smbus_read_word_data(client, MP2975_MFR_DC_LOOP_CTRL);
7472c6fcbb2SVadim Pasternak 		if (ret < 0)
7482c6fcbb2SVadim Pasternak 			return ret;
7491feb31e8SPatrick Rudolph 		if (ret & MP2975_VOUT_FORMAT) {
7501feb31e8SPatrick Rudolph 			ret &= ~MP2975_VOUT_FORMAT;
7511feb31e8SPatrick Rudolph 			ret = i2c_smbus_write_word_data(client, MP2975_MFR_DC_LOOP_CTRL, ret);
7521feb31e8SPatrick Rudolph 		}
7535239277eSPatrick Rudolph 	} else {
7545239277eSPatrick Rudolph 		ret = i2c_smbus_read_word_data(client, MP2973_MFR_RESO_SET);
7555239277eSPatrick Rudolph 		if (ret < 0)
7565239277eSPatrick Rudolph 			return ret;
7575239277eSPatrick Rudolph 		i = ret;
7585239277eSPatrick Rudolph 
7595239277eSPatrick Rudolph 		if (page == 0) {
7605239277eSPatrick Rudolph 			i &= ~MP2973_VOUT_FORMAT_R1;
7615239277eSPatrick Rudolph 			i |= MP2973_VOUT_FORMAT_DIRECT_R1;
7625239277eSPatrick Rudolph 		} else {
7635239277eSPatrick Rudolph 			i &= ~MP2973_VOUT_FORMAT_R2;
7645239277eSPatrick Rudolph 			i |= MP2973_VOUT_FORMAT_DIRECT_R2;
7655239277eSPatrick Rudolph 		}
7665239277eSPatrick Rudolph 		if (i != ret)
7675239277eSPatrick Rudolph 			ret = i2c_smbus_write_word_data(client, MP2973_MFR_RESO_SET, i);
7685239277eSPatrick Rudolph 	}
7691feb31e8SPatrick Rudolph 	return ret;
7702c6fcbb2SVadim Pasternak }
7712c6fcbb2SVadim Pasternak 
7722c6fcbb2SVadim Pasternak static int
7732c6fcbb2SVadim Pasternak mp2975_vout_ov_scale_get(struct i2c_client *client, struct mp2975_data *data,
7742c6fcbb2SVadim Pasternak 			 struct pmbus_driver_info *info)
7752c6fcbb2SVadim Pasternak {
7762c6fcbb2SVadim Pasternak 	int thres_dev, sense_ampl, ret;
7772c6fcbb2SVadim Pasternak 
7782c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
7792c6fcbb2SVadim Pasternak 	if (ret < 0)
7802c6fcbb2SVadim Pasternak 		return ret;
7812c6fcbb2SVadim Pasternak 
7822c6fcbb2SVadim Pasternak 	/*
7832c6fcbb2SVadim Pasternak 	 * Get divider for over- and under-voltage protection thresholds
7842c6fcbb2SVadim Pasternak 	 * configuration from the Advanced Options of Auto Phase Shedding and
7852c6fcbb2SVadim Pasternak 	 * decay register.
7862c6fcbb2SVadim Pasternak 	 */
7872c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_APS_DECAY_ADV);
7882c6fcbb2SVadim Pasternak 	if (ret < 0)
7892c6fcbb2SVadim Pasternak 		return ret;
7902c6fcbb2SVadim Pasternak 	thres_dev = ret & MP2975_PRT_THRES_DIV_OV_EN ? MP2975_PROT_DEV_OV_ON :
7912c6fcbb2SVadim Pasternak 						       MP2975_PROT_DEV_OV_OFF;
7922c6fcbb2SVadim Pasternak 
7932c6fcbb2SVadim Pasternak 	/* Select the gain of remote sense amplifier. */
7942c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, PMBUS_VOUT_SCALE_LOOP);
7952c6fcbb2SVadim Pasternak 	if (ret < 0)
7962c6fcbb2SVadim Pasternak 		return ret;
7972c6fcbb2SVadim Pasternak 	sense_ampl = ret & MP2975_SENSE_AMPL ? MP2975_SENSE_AMPL_HALF :
7982c6fcbb2SVadim Pasternak 					       MP2975_SENSE_AMPL_UNIT;
7992c6fcbb2SVadim Pasternak 
8002c6fcbb2SVadim Pasternak 	data->vout_scale = sense_ampl * thres_dev;
8012c6fcbb2SVadim Pasternak 
8022c6fcbb2SVadim Pasternak 	return 0;
8032c6fcbb2SVadim Pasternak }
8042c6fcbb2SVadim Pasternak 
8052c6fcbb2SVadim Pasternak static int
8062c6fcbb2SVadim Pasternak mp2975_vout_per_rail_config_get(struct i2c_client *client,
8072c6fcbb2SVadim Pasternak 				struct mp2975_data *data,
8082c6fcbb2SVadim Pasternak 				struct pmbus_driver_info *info)
8092c6fcbb2SVadim Pasternak {
8102c6fcbb2SVadim Pasternak 	int i, ret;
8112c6fcbb2SVadim Pasternak 
8122c6fcbb2SVadim Pasternak 	for (i = 0; i < data->info.pages; i++) {
8132c6fcbb2SVadim Pasternak 		ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
8142c6fcbb2SVadim Pasternak 		if (ret < 0)
8155239277eSPatrick Rudolph 			continue;
8162c6fcbb2SVadim Pasternak 
8175239277eSPatrick Rudolph 		/* Set VOUT format for READ_VOUT command : direct. */
8185239277eSPatrick Rudolph 		ret = mp2975_set_vout_format(client, data, i);
8192c6fcbb2SVadim Pasternak 		if (ret < 0)
8202c6fcbb2SVadim Pasternak 			return ret;
8212c6fcbb2SVadim Pasternak 
8222c6fcbb2SVadim Pasternak 		/* Obtain maximum voltage values. */
8232c6fcbb2SVadim Pasternak 		ret = mp2975_vout_max_get(client, data, info, i);
8242c6fcbb2SVadim Pasternak 		if (ret < 0)
8252c6fcbb2SVadim Pasternak 			return ret;
8262c6fcbb2SVadim Pasternak 
8275239277eSPatrick Rudolph 		/* Skip if reading Vref is unsupported */
8285239277eSPatrick Rudolph 		if (data->chip_id != mp2975)
8295239277eSPatrick Rudolph 			continue;
8305239277eSPatrick Rudolph 
8315239277eSPatrick Rudolph 		/* Obtain voltage reference offsets. */
8325239277eSPatrick Rudolph 		ret = mp2975_vref_offset_get(client, data, i);
8332c6fcbb2SVadim Pasternak 		if (ret < 0)
8342c6fcbb2SVadim Pasternak 			return ret;
8352c6fcbb2SVadim Pasternak 
8362c6fcbb2SVadim Pasternak 		/*
8372c6fcbb2SVadim Pasternak 		 * Set over-voltage fixed value. Thresholds are provided as
8382c6fcbb2SVadim Pasternak 		 * fixed value, and tracking value. The minimum of them are
8392c6fcbb2SVadim Pasternak 		 * exposed as over-voltage critical threshold.
8402c6fcbb2SVadim Pasternak 		 */
8412c6fcbb2SVadim Pasternak 		data->vout_ov_fixed[i] = data->vref[i] +
8422c6fcbb2SVadim Pasternak 					 DIV_ROUND_CLOSEST(data->vref_off[i] *
8432c6fcbb2SVadim Pasternak 							   data->vout_scale,
8442c6fcbb2SVadim Pasternak 							   10);
8452c6fcbb2SVadim Pasternak 	}
8462c6fcbb2SVadim Pasternak 
8472c6fcbb2SVadim Pasternak 	return 0;
8482c6fcbb2SVadim Pasternak }
8492c6fcbb2SVadim Pasternak 
8502c6fcbb2SVadim Pasternak static struct pmbus_driver_info mp2975_info = {
8512c6fcbb2SVadim Pasternak 	.pages = 1,
8522c6fcbb2SVadim Pasternak 	.format[PSC_VOLTAGE_IN] = linear,
8532c6fcbb2SVadim Pasternak 	.format[PSC_VOLTAGE_OUT] = direct,
8542c6fcbb2SVadim Pasternak 	.format[PSC_TEMPERATURE] = direct,
8552c6fcbb2SVadim Pasternak 	.format[PSC_CURRENT_IN] = linear,
8562c6fcbb2SVadim Pasternak 	.format[PSC_CURRENT_OUT] = direct,
8572c6fcbb2SVadim Pasternak 	.format[PSC_POWER] = direct,
8582c6fcbb2SVadim Pasternak 	.m[PSC_TEMPERATURE] = 1,
8592c6fcbb2SVadim Pasternak 	.m[PSC_VOLTAGE_OUT] = 1,
8602c6fcbb2SVadim Pasternak 	.R[PSC_VOLTAGE_OUT] = 3,
8612c6fcbb2SVadim Pasternak 	.m[PSC_CURRENT_OUT] = 1,
8622c6fcbb2SVadim Pasternak 	.m[PSC_POWER] = 1,
8632c6fcbb2SVadim Pasternak 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
8642c6fcbb2SVadim Pasternak 		PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
8652c6fcbb2SVadim Pasternak 		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_POUT |
8662c6fcbb2SVadim Pasternak 		PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT | PMBUS_PHASE_VIRTUAL,
8672c6fcbb2SVadim Pasternak 	.read_word_data = mp2975_read_word_data,
86888fc1efcSPatrick Rudolph #if IS_ENABLED(CONFIG_SENSORS_MP2975_REGULATOR)
86988fc1efcSPatrick Rudolph 	.num_regulators = 1,
87088fc1efcSPatrick Rudolph 	.reg_desc = mp2975_reg_desc,
87188fc1efcSPatrick Rudolph #endif
8722c6fcbb2SVadim Pasternak };
8732c6fcbb2SVadim Pasternak 
8745239277eSPatrick Rudolph static struct pmbus_driver_info mp2973_info = {
8755239277eSPatrick Rudolph 	.pages = 1,
8765239277eSPatrick Rudolph 	.format[PSC_VOLTAGE_IN] = linear,
8775239277eSPatrick Rudolph 	.format[PSC_VOLTAGE_OUT] = direct,
8785239277eSPatrick Rudolph 	.format[PSC_TEMPERATURE] = linear,
8795239277eSPatrick Rudolph 	.format[PSC_CURRENT_IN] = linear,
8805239277eSPatrick Rudolph 	.format[PSC_CURRENT_OUT] = linear,
8815239277eSPatrick Rudolph 	.format[PSC_POWER] = linear,
8825239277eSPatrick Rudolph 	.m[PSC_VOLTAGE_OUT] = 1,
8835239277eSPatrick Rudolph 	.R[PSC_VOLTAGE_OUT] = 3,
8845239277eSPatrick Rudolph 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
8855239277eSPatrick Rudolph 		PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
8865239277eSPatrick Rudolph 		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_POUT |
8875239277eSPatrick Rudolph 		PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT,
8885239277eSPatrick Rudolph 	.read_word_data = mp2973_read_word_data,
88988fc1efcSPatrick Rudolph #if IS_ENABLED(CONFIG_SENSORS_MP2975_REGULATOR)
89088fc1efcSPatrick Rudolph 	.num_regulators = 1,
89188fc1efcSPatrick Rudolph 	.reg_desc = mp2975_reg_desc,
89288fc1efcSPatrick Rudolph #endif
8935239277eSPatrick Rudolph };
8945239277eSPatrick Rudolph 
8952c6fcbb2SVadim Pasternak static int mp2975_probe(struct i2c_client *client)
8962c6fcbb2SVadim Pasternak {
8972c6fcbb2SVadim Pasternak 	struct pmbus_driver_info *info;
8982c6fcbb2SVadim Pasternak 	struct mp2975_data *data;
8992c6fcbb2SVadim Pasternak 	int ret;
9002c6fcbb2SVadim Pasternak 
9012c6fcbb2SVadim Pasternak 	data = devm_kzalloc(&client->dev, sizeof(struct mp2975_data),
9022c6fcbb2SVadim Pasternak 			    GFP_KERNEL);
9032c6fcbb2SVadim Pasternak 	if (!data)
9042c6fcbb2SVadim Pasternak 		return -ENOMEM;
9052c6fcbb2SVadim Pasternak 
9061f6f34d0SPatrick Rudolph 	if (client->dev.of_node)
9071f6f34d0SPatrick Rudolph 		data->chip_id = (enum chips)(unsigned long)of_device_get_match_data(&client->dev);
9081f6f34d0SPatrick Rudolph 	else
9091f6f34d0SPatrick Rudolph 		data->chip_id = i2c_match_id(mp2975_id, client)->driver_data;
9101f6f34d0SPatrick Rudolph 
911e2c90b48SPatrick Rudolph 	memcpy(data->max_phases, mp2975_max_phases[data->chip_id],
912e2c90b48SPatrick Rudolph 	       sizeof(data->max_phases));
913e2c90b48SPatrick Rudolph 
9145239277eSPatrick Rudolph 	if (data->chip_id == mp2975)
9155239277eSPatrick Rudolph 		memcpy(&data->info, &mp2975_info, sizeof(*info));
9165239277eSPatrick Rudolph 	else
9175239277eSPatrick Rudolph 		memcpy(&data->info, &mp2973_info, sizeof(*info));
9185239277eSPatrick Rudolph 
9192c6fcbb2SVadim Pasternak 	info = &data->info;
9202c6fcbb2SVadim Pasternak 
9212c6fcbb2SVadim Pasternak 	/* Identify multiphase configuration for rail 2. */
922e2c90b48SPatrick Rudolph 	ret = mp2975_identify_multiphase_rail2(client, data);
9232c6fcbb2SVadim Pasternak 	if (ret < 0)
9242c6fcbb2SVadim Pasternak 		return ret;
9252c6fcbb2SVadim Pasternak 
9262c6fcbb2SVadim Pasternak 	if (ret) {
9272c6fcbb2SVadim Pasternak 		/* Two rails are connected. */
9282c6fcbb2SVadim Pasternak 		data->info.pages = MP2975_PAGE_NUM;
9292c6fcbb2SVadim Pasternak 		data->info.phases[1] = ret;
9302c6fcbb2SVadim Pasternak 		data->info.func[1] = MP2975_RAIL2_FUNC;
93188fc1efcSPatrick Rudolph 		if (IS_ENABLED(CONFIG_SENSORS_MP2975_REGULATOR))
93288fc1efcSPatrick Rudolph 			data->info.num_regulators = MP2975_PAGE_NUM;
9332c6fcbb2SVadim Pasternak 	}
9342c6fcbb2SVadim Pasternak 
9352c6fcbb2SVadim Pasternak 	/* Identify multiphase configuration. */
9362c6fcbb2SVadim Pasternak 	ret = mp2975_identify_multiphase(client, data, info);
9372c6fcbb2SVadim Pasternak 	if (ret)
9382c6fcbb2SVadim Pasternak 		return ret;
9392c6fcbb2SVadim Pasternak 
940*45f154dcSPatrick Rudolph 	if (data->chip_id == mp2975) {
9412c6fcbb2SVadim Pasternak 		/* Identify VID setting per rail. */
9422c6fcbb2SVadim Pasternak 		ret = mp2975_identify_rails_vid(client, data, info);
9432c6fcbb2SVadim Pasternak 		if (ret < 0)
9442c6fcbb2SVadim Pasternak 			return ret;
9452c6fcbb2SVadim Pasternak 
9462c6fcbb2SVadim Pasternak 		/* Obtain current sense gain of power stage. */
9472c6fcbb2SVadim Pasternak 		ret = mp2975_current_sense_gain_get(client, data);
9482c6fcbb2SVadim Pasternak 		if (ret)
9492c6fcbb2SVadim Pasternak 			return ret;
9502c6fcbb2SVadim Pasternak 
9512c6fcbb2SVadim Pasternak 		/* Obtain voltage reference values. */
9522c6fcbb2SVadim Pasternak 		ret = mp2975_vref_get(client, data, info);
9532c6fcbb2SVadim Pasternak 		if (ret)
9542c6fcbb2SVadim Pasternak 			return ret;
9552c6fcbb2SVadim Pasternak 
9562c6fcbb2SVadim Pasternak 		/* Obtain vout over-voltage scales. */
9572c6fcbb2SVadim Pasternak 		ret = mp2975_vout_ov_scale_get(client, data, info);
9582c6fcbb2SVadim Pasternak 		if (ret < 0)
9592c6fcbb2SVadim Pasternak 			return ret;
9605239277eSPatrick Rudolph 	} else {
9615239277eSPatrick Rudolph 		/* Identify VID setting per rail. */
9625239277eSPatrick Rudolph 		ret = mp2973_identify_rails_vid(client, data, info);
9635239277eSPatrick Rudolph 		if (ret < 0)
9645239277eSPatrick Rudolph 			return ret;
9655239277eSPatrick Rudolph 	}
9662c6fcbb2SVadim Pasternak 
9672c6fcbb2SVadim Pasternak 	/* Obtain offsets, maximum and format for vout. */
9682c6fcbb2SVadim Pasternak 	ret = mp2975_vout_per_rail_config_get(client, data, info);
9692c6fcbb2SVadim Pasternak 	if (ret)
9702c6fcbb2SVadim Pasternak 		return ret;
9712c6fcbb2SVadim Pasternak 
9722c6fcbb2SVadim Pasternak 	return pmbus_do_probe(client, info);
9732c6fcbb2SVadim Pasternak }
9742c6fcbb2SVadim Pasternak 
9752c6fcbb2SVadim Pasternak static const struct of_device_id __maybe_unused mp2975_of_match[] = {
9765239277eSPatrick Rudolph 	{.compatible = "mps,mp2971", .data = (void *)mp2971},
9775239277eSPatrick Rudolph 	{.compatible = "mps,mp2973", .data = (void *)mp2973},
9781f6f34d0SPatrick Rudolph 	{.compatible = "mps,mp2975", .data = (void *)mp2975},
9792c6fcbb2SVadim Pasternak 	{}
9802c6fcbb2SVadim Pasternak };
9812c6fcbb2SVadim Pasternak MODULE_DEVICE_TABLE(of, mp2975_of_match);
9822c6fcbb2SVadim Pasternak 
9832c6fcbb2SVadim Pasternak static struct i2c_driver mp2975_driver = {
9842c6fcbb2SVadim Pasternak 	.driver = {
9852c6fcbb2SVadim Pasternak 		.name = "mp2975",
9862c6fcbb2SVadim Pasternak 		.of_match_table = of_match_ptr(mp2975_of_match),
9872c6fcbb2SVadim Pasternak 	},
9881975d167SUwe Kleine-König 	.probe = mp2975_probe,
9892c6fcbb2SVadim Pasternak 	.id_table = mp2975_id,
9902c6fcbb2SVadim Pasternak };
9912c6fcbb2SVadim Pasternak 
9922c6fcbb2SVadim Pasternak module_i2c_driver(mp2975_driver);
9932c6fcbb2SVadim Pasternak 
9942c6fcbb2SVadim Pasternak MODULE_AUTHOR("Vadim Pasternak <vadimp@nvidia.com>");
9952c6fcbb2SVadim Pasternak MODULE_DESCRIPTION("PMBus driver for MPS MP2975 device");
9962c6fcbb2SVadim Pasternak MODULE_LICENSE("GPL");
997b94ca77eSGuenter Roeck MODULE_IMPORT_NS(PMBUS);
998