xref: /linux/drivers/hwmon/pmbus/mp2975.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
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 
8d2bc4a99SAndy Shevchenko #include <linux/bitops.h>
92c6fcbb2SVadim Pasternak #include <linux/err.h>
102c6fcbb2SVadim Pasternak #include <linux/i2c.h>
112c6fcbb2SVadim Pasternak #include <linux/init.h>
122c6fcbb2SVadim Pasternak #include <linux/kernel.h>
13*73bc2357SAndy Shevchenko #include <linux/mod_devicetable.h>
142c6fcbb2SVadim Pasternak #include <linux/module.h>
15*73bc2357SAndy Shevchenko 
162c6fcbb2SVadim Pasternak #include "pmbus.h"
172c6fcbb2SVadim Pasternak 
182c6fcbb2SVadim Pasternak /* Vendor specific registers. */
192c6fcbb2SVadim Pasternak #define MP2975_MFR_APS_HYS_R2		0x0d
202c6fcbb2SVadim Pasternak #define MP2975_MFR_SLOPE_TRIM3		0x1d
212c6fcbb2SVadim Pasternak #define MP2975_MFR_VR_MULTI_CONFIG_R1	0x0d
222c6fcbb2SVadim Pasternak #define MP2975_MFR_VR_MULTI_CONFIG_R2	0x1d
232c6fcbb2SVadim Pasternak #define MP2975_MFR_APS_DECAY_ADV	0x56
242c6fcbb2SVadim Pasternak #define MP2975_MFR_DC_LOOP_CTRL		0x59
252c6fcbb2SVadim Pasternak #define MP2975_MFR_OCP_UCP_PHASE_SET	0x65
262c6fcbb2SVadim Pasternak #define MP2975_MFR_VR_CONFIG1		0x68
272c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS1_2		0x82
282c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS3_4		0x83
292c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS5_6		0x84
302c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS7_8		0x85
312c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS9_10		0x86
322c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS11_12		0x87
332c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_IOUT_PK		0x90
342c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_POUT_PK		0x91
352c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_VREF_R1		0xa1
362c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_VREF_R2		0xa3
372c6fcbb2SVadim Pasternak #define MP2975_MFR_OVP_TH_SET		0xe5
382c6fcbb2SVadim Pasternak #define MP2975_MFR_UVP_SET		0xe6
392c6fcbb2SVadim Pasternak 
405239277eSPatrick Rudolph #define MP2973_MFR_RESO_SET		0xc7
415239277eSPatrick Rudolph 
422c6fcbb2SVadim Pasternak #define MP2975_VOUT_FORMAT		BIT(15)
432c6fcbb2SVadim Pasternak #define MP2975_VID_STEP_SEL_R1		BIT(4)
442c6fcbb2SVadim Pasternak #define MP2975_IMVP9_EN_R1		BIT(13)
452c6fcbb2SVadim Pasternak #define MP2975_VID_STEP_SEL_R2		BIT(3)
462c6fcbb2SVadim Pasternak #define MP2975_IMVP9_EN_R2		BIT(12)
472c6fcbb2SVadim Pasternak #define MP2975_PRT_THRES_DIV_OV_EN	BIT(14)
482c6fcbb2SVadim Pasternak #define MP2975_DRMOS_KCS		GENMASK(13, 12)
492c6fcbb2SVadim Pasternak #define MP2975_PROT_DEV_OV_OFF		10
502c6fcbb2SVadim Pasternak #define MP2975_PROT_DEV_OV_ON		5
512c6fcbb2SVadim Pasternak #define MP2975_SENSE_AMPL		BIT(11)
522c6fcbb2SVadim Pasternak #define MP2975_SENSE_AMPL_UNIT		1
532c6fcbb2SVadim Pasternak #define MP2975_SENSE_AMPL_HALF		2
542c6fcbb2SVadim Pasternak #define MP2975_VIN_UV_LIMIT_UNIT	8
552c6fcbb2SVadim Pasternak 
565239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_R1		GENMASK(7, 6)
575239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_R2		GENMASK(4, 3)
585239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_DIRECT_R1	BIT(7)
595239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_LINEAR_R1	BIT(6)
605239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_DIRECT_R2	BIT(4)
615239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_LINEAR_R2	BIT(3)
625239277eSPatrick Rudolph 
635239277eSPatrick Rudolph #define MP2973_MFR_VR_MULTI_CONFIG_R1	0x0d
645239277eSPatrick Rudolph #define MP2973_MFR_VR_MULTI_CONFIG_R2	0x1d
655239277eSPatrick Rudolph #define MP2973_VID_STEP_SEL_R1		BIT(4)
665239277eSPatrick Rudolph #define MP2973_IMVP9_EN_R1		BIT(14)
675239277eSPatrick Rudolph #define MP2973_VID_STEP_SEL_R2		BIT(3)
685239277eSPatrick Rudolph #define MP2973_IMVP9_EN_R2		BIT(13)
695239277eSPatrick Rudolph 
7045f154dcSPatrick Rudolph #define MP2973_MFR_OCP_TOTAL_SET	0x5f
7145f154dcSPatrick Rudolph #define MP2973_OCP_TOTAL_CUR_MASK	GENMASK(6, 0)
7245f154dcSPatrick Rudolph #define MP2973_MFR_OCP_LEVEL_RES	BIT(15)
7345f154dcSPatrick Rudolph 
745239277eSPatrick Rudolph #define MP2973_MFR_READ_IOUT_PK		0x90
755239277eSPatrick Rudolph #define MP2973_MFR_READ_POUT_PK		0x91
765239277eSPatrick Rudolph 
772c6fcbb2SVadim Pasternak #define MP2975_MAX_PHASE_RAIL1	8
782c6fcbb2SVadim Pasternak #define MP2975_MAX_PHASE_RAIL2	4
795239277eSPatrick Rudolph 
805239277eSPatrick Rudolph #define MP2973_MAX_PHASE_RAIL1	14
815239277eSPatrick Rudolph #define MP2973_MAX_PHASE_RAIL2	6
825239277eSPatrick Rudolph 
835239277eSPatrick Rudolph #define MP2971_MAX_PHASE_RAIL1	8
845239277eSPatrick Rudolph #define MP2971_MAX_PHASE_RAIL2	3
855239277eSPatrick Rudolph 
862c6fcbb2SVadim Pasternak #define MP2975_PAGE_NUM		2
872c6fcbb2SVadim Pasternak 
882c6fcbb2SVadim Pasternak #define MP2975_RAIL2_FUNC	(PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | \
892c6fcbb2SVadim Pasternak 				 PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | \
902292e2f6SVadim Pasternak 				 PMBUS_HAVE_POUT | PMBUS_PHASE_VIRTUAL)
912c6fcbb2SVadim Pasternak 
921f6f34d0SPatrick Rudolph enum chips {
935239277eSPatrick Rudolph 	mp2971, mp2973, mp2975
941f6f34d0SPatrick Rudolph };
951f6f34d0SPatrick Rudolph 
96e2c90b48SPatrick Rudolph static const int mp2975_max_phases[][MP2975_PAGE_NUM] = {
97e2c90b48SPatrick Rudolph 	[mp2975] = { MP2975_MAX_PHASE_RAIL1, MP2975_MAX_PHASE_RAIL2 },
985239277eSPatrick Rudolph 	[mp2973] = { MP2973_MAX_PHASE_RAIL1, MP2973_MAX_PHASE_RAIL2 },
995239277eSPatrick Rudolph 	[mp2971] = { MP2971_MAX_PHASE_RAIL1, MP2971_MAX_PHASE_RAIL2 },
100e2c90b48SPatrick Rudolph };
101e2c90b48SPatrick Rudolph 
102*73bc2357SAndy Shevchenko struct mp2975_driver_info {
103*73bc2357SAndy Shevchenko 	const struct pmbus_driver_info *info;
104*73bc2357SAndy Shevchenko 	enum chips chip_id;
105*73bc2357SAndy Shevchenko };
106*73bc2357SAndy Shevchenko 
1072c6fcbb2SVadim Pasternak struct mp2975_data {
1082c6fcbb2SVadim Pasternak 	struct pmbus_driver_info info;
1091f6f34d0SPatrick Rudolph 	enum chips chip_id;
1102c6fcbb2SVadim Pasternak 	int vout_scale;
111e2c90b48SPatrick Rudolph 	int max_phases[MP2975_PAGE_NUM];
1122c6fcbb2SVadim Pasternak 	int vid_step[MP2975_PAGE_NUM];
1132c6fcbb2SVadim Pasternak 	int vref[MP2975_PAGE_NUM];
1142c6fcbb2SVadim Pasternak 	int vref_off[MP2975_PAGE_NUM];
1152c6fcbb2SVadim Pasternak 	int vout_max[MP2975_PAGE_NUM];
1162c6fcbb2SVadim Pasternak 	int vout_ov_fixed[MP2975_PAGE_NUM];
1172c6fcbb2SVadim Pasternak 	int curr_sense_gain[MP2975_PAGE_NUM];
1182c6fcbb2SVadim Pasternak };
1192c6fcbb2SVadim Pasternak 
12088fc1efcSPatrick Rudolph static const struct regulator_desc __maybe_unused mp2975_reg_desc[] = {
12188fc1efcSPatrick Rudolph 	PMBUS_REGULATOR("vout", 0),
12288fc1efcSPatrick Rudolph 	PMBUS_REGULATOR("vout", 1),
12388fc1efcSPatrick Rudolph };
12488fc1efcSPatrick Rudolph 
1252c6fcbb2SVadim Pasternak #define to_mp2975_data(x)  container_of(x, struct mp2975_data, info)
1262c6fcbb2SVadim Pasternak 
mp2975_read_byte_data(struct i2c_client * client,int page,int reg)127c60fe56cSKonstantin Aladyshev static int mp2975_read_byte_data(struct i2c_client *client, int page, int reg)
128c60fe56cSKonstantin Aladyshev {
129c60fe56cSKonstantin Aladyshev 	switch (reg) {
130c60fe56cSKonstantin Aladyshev 	case PMBUS_VOUT_MODE:
131c60fe56cSKonstantin Aladyshev 		/*
13291564418SKonstantin Aladyshev 		 * Report direct format as configured by MFR_DC_LOOP_CTRL.
13391564418SKonstantin Aladyshev 		 * Unlike on MP2971/MP2973 the reported VOUT_MODE isn't automatically
13491564418SKonstantin Aladyshev 		 * internally updated, but always reads as PB_VOUT_MODE_VID.
135c60fe56cSKonstantin Aladyshev 		 */
136c60fe56cSKonstantin Aladyshev 		return PB_VOUT_MODE_DIRECT;
137c60fe56cSKonstantin Aladyshev 	default:
138c60fe56cSKonstantin Aladyshev 		return -ENODATA;
139c60fe56cSKonstantin Aladyshev 	}
140c60fe56cSKonstantin Aladyshev }
141c60fe56cSKonstantin Aladyshev 
1422c6fcbb2SVadim Pasternak static int
mp2975_read_word_helper(struct i2c_client * client,int page,int phase,u8 reg,u16 mask)1432c6fcbb2SVadim Pasternak mp2975_read_word_helper(struct i2c_client *client, int page, int phase, u8 reg,
1442c6fcbb2SVadim Pasternak 			u16 mask)
1452c6fcbb2SVadim Pasternak {
1462c6fcbb2SVadim Pasternak 	int ret = pmbus_read_word_data(client, page, phase, reg);
1472c6fcbb2SVadim Pasternak 
1482c6fcbb2SVadim Pasternak 	return (ret > 0) ? ret & mask : ret;
1492c6fcbb2SVadim Pasternak }
1502c6fcbb2SVadim Pasternak 
1512c6fcbb2SVadim Pasternak static int
mp2975_vid2direct(int vrf,int val)1522c6fcbb2SVadim Pasternak mp2975_vid2direct(int vrf, int val)
1532c6fcbb2SVadim Pasternak {
1542c6fcbb2SVadim Pasternak 	switch (vrf) {
1552c6fcbb2SVadim Pasternak 	case vr12:
1562c6fcbb2SVadim Pasternak 		if (val >= 0x01)
1572c6fcbb2SVadim Pasternak 			return 250 + (val - 1) * 5;
1582c6fcbb2SVadim Pasternak 		break;
1592c6fcbb2SVadim Pasternak 	case vr13:
1602c6fcbb2SVadim Pasternak 		if (val >= 0x01)
1612c6fcbb2SVadim Pasternak 			return 500 + (val - 1) * 10;
1622c6fcbb2SVadim Pasternak 		break;
1632c6fcbb2SVadim Pasternak 	case imvp9:
1642c6fcbb2SVadim Pasternak 		if (val >= 0x01)
1652c6fcbb2SVadim Pasternak 			return 200 + (val - 1) * 10;
1662c6fcbb2SVadim Pasternak 		break;
1672c6fcbb2SVadim Pasternak 	default:
1682c6fcbb2SVadim Pasternak 		return -EINVAL;
1692c6fcbb2SVadim Pasternak 	}
1702c6fcbb2SVadim Pasternak 	return 0;
1712c6fcbb2SVadim Pasternak }
1722c6fcbb2SVadim Pasternak 
17345f154dcSPatrick Rudolph #define MAX_LIN_MANTISSA	(1023 * 1000)
17445f154dcSPatrick Rudolph #define MIN_LIN_MANTISSA	(511 * 1000)
17545f154dcSPatrick Rudolph 
17645f154dcSPatrick Rudolph /* Converts a milli-unit DIRECT value to LINEAR11 format */
mp2975_data2reg_linear11(s64 val)17745f154dcSPatrick Rudolph static u16 mp2975_data2reg_linear11(s64 val)
17845f154dcSPatrick Rudolph {
17945f154dcSPatrick Rudolph 	s16 exponent = 0, mantissa;
18045f154dcSPatrick Rudolph 	bool negative = false;
18145f154dcSPatrick Rudolph 
18245f154dcSPatrick Rudolph 	/* simple case */
18345f154dcSPatrick Rudolph 	if (val == 0)
18445f154dcSPatrick Rudolph 		return 0;
18545f154dcSPatrick Rudolph 
18645f154dcSPatrick Rudolph 	/* Reduce large mantissa until it fits into 10 bit */
18745f154dcSPatrick Rudolph 	while (val >= MAX_LIN_MANTISSA && exponent < 15) {
18845f154dcSPatrick Rudolph 		exponent++;
18945f154dcSPatrick Rudolph 		val >>= 1;
19045f154dcSPatrick Rudolph 	}
19145f154dcSPatrick Rudolph 	/* Increase small mantissa to improve precision */
19245f154dcSPatrick Rudolph 	while (val < MIN_LIN_MANTISSA && exponent > -15) {
19345f154dcSPatrick Rudolph 		exponent--;
19445f154dcSPatrick Rudolph 		val <<= 1;
19545f154dcSPatrick Rudolph 	}
19645f154dcSPatrick Rudolph 
19745f154dcSPatrick Rudolph 	/* Convert mantissa from milli-units to units */
19845f154dcSPatrick Rudolph 	mantissa = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff);
19945f154dcSPatrick Rudolph 
20045f154dcSPatrick Rudolph 	/* restore sign */
20145f154dcSPatrick Rudolph 	if (negative)
20245f154dcSPatrick Rudolph 		mantissa = -mantissa;
20345f154dcSPatrick Rudolph 
20445f154dcSPatrick Rudolph 	/* Convert to 5 bit exponent, 11 bit mantissa */
20545f154dcSPatrick Rudolph 	return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
20645f154dcSPatrick Rudolph }
20745f154dcSPatrick Rudolph 
2082c6fcbb2SVadim Pasternak static int
mp2975_read_phase(struct i2c_client * client,struct mp2975_data * data,int page,int phase,u8 reg)2092c6fcbb2SVadim Pasternak mp2975_read_phase(struct i2c_client *client, struct mp2975_data *data,
2102c6fcbb2SVadim Pasternak 		  int page, int phase, u8 reg)
2112c6fcbb2SVadim Pasternak {
2122c6fcbb2SVadim Pasternak 	int ph_curr, ret;
2132c6fcbb2SVadim Pasternak 
2142c6fcbb2SVadim Pasternak 	ret = pmbus_read_word_data(client, page, phase, reg);
2152c6fcbb2SVadim Pasternak 	if (ret < 0)
2162c6fcbb2SVadim Pasternak 		return ret;
2172c6fcbb2SVadim Pasternak 
2182c6fcbb2SVadim Pasternak 	if (!((phase + 1) % MP2975_PAGE_NUM))
2192c6fcbb2SVadim Pasternak 		ret >>= 8;
2202c6fcbb2SVadim Pasternak 	ret &= 0xff;
2212c6fcbb2SVadim Pasternak 
2222c6fcbb2SVadim Pasternak 	/*
2232c6fcbb2SVadim Pasternak 	 * Output value is calculated as: (READ_CSx / 80 – 1.23) / (Kcs * Rcs)
2242c6fcbb2SVadim Pasternak 	 * where:
2252c6fcbb2SVadim Pasternak 	 * - Kcs is the DrMOS current sense gain of power stage, which is
2262c6fcbb2SVadim Pasternak 	 *   obtained from the register MP2975_MFR_VR_CONFIG1, bits 13-12 with
2272c6fcbb2SVadim Pasternak 	 *   the following selection of DrMOS (data->curr_sense_gain[page]):
2282c6fcbb2SVadim Pasternak 	 *   00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A.
2292c6fcbb2SVadim Pasternak 	 * - Rcs is the internal phase current sense resistor which is constant
2302c6fcbb2SVadim Pasternak 	 *   value 1kΩ.
2312c6fcbb2SVadim Pasternak 	 */
2322c6fcbb2SVadim Pasternak 	ph_curr = ret * 100 - 9800;
2332c6fcbb2SVadim Pasternak 
2342c6fcbb2SVadim Pasternak 	/*
2352c6fcbb2SVadim Pasternak 	 * Current phase sensing, providing by the device is not accurate
2362c6fcbb2SVadim Pasternak 	 * for the light load. This because sampling of current occurrence of
2372c6fcbb2SVadim Pasternak 	 * bit weight has a big deviation for light load. For handling such
2382c6fcbb2SVadim Pasternak 	 * case phase current is represented as the maximum between the value
2392c6fcbb2SVadim Pasternak 	 * calculated  above and total rail current divided by number phases.
2402c6fcbb2SVadim Pasternak 	 */
2412c6fcbb2SVadim Pasternak 	ret = pmbus_read_word_data(client, page, phase, PMBUS_READ_IOUT);
2422c6fcbb2SVadim Pasternak 	if (ret < 0)
2432c6fcbb2SVadim Pasternak 		return ret;
2442c6fcbb2SVadim Pasternak 
2452c6fcbb2SVadim Pasternak 	return max_t(int, DIV_ROUND_CLOSEST(ret, data->info.phases[page]),
2462c6fcbb2SVadim Pasternak 		     DIV_ROUND_CLOSEST(ph_curr, data->curr_sense_gain[page]));
2472c6fcbb2SVadim Pasternak }
2482c6fcbb2SVadim Pasternak 
2492c6fcbb2SVadim Pasternak static int
mp2975_read_phases(struct i2c_client * client,struct mp2975_data * data,int page,int phase)2502c6fcbb2SVadim Pasternak mp2975_read_phases(struct i2c_client *client, struct mp2975_data *data,
2512c6fcbb2SVadim Pasternak 		   int page, int phase)
2522c6fcbb2SVadim Pasternak {
2532c6fcbb2SVadim Pasternak 	int ret;
2542c6fcbb2SVadim Pasternak 
2552c6fcbb2SVadim Pasternak 	if (page) {
2562c6fcbb2SVadim Pasternak 		switch (phase) {
2572c6fcbb2SVadim Pasternak 		case 0 ... 1:
2582c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2592c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS7_8);
2602c6fcbb2SVadim Pasternak 			break;
2612c6fcbb2SVadim Pasternak 		case 2 ... 3:
2622c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2632c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS9_10);
2642c6fcbb2SVadim Pasternak 			break;
2652c6fcbb2SVadim Pasternak 		case 4 ... 5:
2662c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2672c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS11_12);
2682c6fcbb2SVadim Pasternak 			break;
2692c6fcbb2SVadim Pasternak 		default:
2702c6fcbb2SVadim Pasternak 			return -ENODATA;
2712c6fcbb2SVadim Pasternak 		}
2722c6fcbb2SVadim Pasternak 	} else {
2732c6fcbb2SVadim Pasternak 		switch (phase) {
2742c6fcbb2SVadim Pasternak 		case 0 ... 1:
2752c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2762c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS1_2);
2772c6fcbb2SVadim Pasternak 			break;
2782c6fcbb2SVadim Pasternak 		case 2 ... 3:
2792c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2802c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS3_4);
2812c6fcbb2SVadim Pasternak 			break;
2822c6fcbb2SVadim Pasternak 		case 4 ... 5:
2832c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2842c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS5_6);
2852c6fcbb2SVadim Pasternak 			break;
2862c6fcbb2SVadim Pasternak 		case 6 ... 7:
2872c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2882c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS7_8);
2892c6fcbb2SVadim Pasternak 			break;
2902c6fcbb2SVadim Pasternak 		case 8 ... 9:
2912c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2922c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS9_10);
2932c6fcbb2SVadim Pasternak 			break;
2942c6fcbb2SVadim Pasternak 		case 10 ... 11:
2952c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
2962c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS11_12);
2972c6fcbb2SVadim Pasternak 			break;
2982c6fcbb2SVadim Pasternak 		default:
2992c6fcbb2SVadim Pasternak 			return -ENODATA;
3002c6fcbb2SVadim Pasternak 		}
3012c6fcbb2SVadim Pasternak 	}
3022c6fcbb2SVadim Pasternak 	return ret;
3032c6fcbb2SVadim Pasternak }
3042c6fcbb2SVadim Pasternak 
mp2973_read_word_data(struct i2c_client * client,int page,int phase,int reg)3055239277eSPatrick Rudolph static int mp2973_read_word_data(struct i2c_client *client, int page,
3065239277eSPatrick Rudolph 				 int phase, int reg)
3075239277eSPatrick Rudolph {
3085239277eSPatrick Rudolph 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
3095239277eSPatrick Rudolph 	struct mp2975_data *data = to_mp2975_data(info);
3105239277eSPatrick Rudolph 	int ret;
3115239277eSPatrick Rudolph 
3125239277eSPatrick Rudolph 	switch (reg) {
3139da2901cSNaresh Solanki 	case PMBUS_STATUS_WORD:
3149da2901cSNaresh Solanki 		/* MP2973 & MP2971 return PGOOD instead of PB_STATUS_POWER_GOOD_N. */
3159da2901cSNaresh Solanki 		ret = pmbus_read_word_data(client, page, phase, reg);
3169da2901cSNaresh Solanki 		ret ^= PB_STATUS_POWER_GOOD_N;
3179da2901cSNaresh Solanki 		break;
3185239277eSPatrick Rudolph 	case PMBUS_OT_FAULT_LIMIT:
3195239277eSPatrick Rudolph 		ret = mp2975_read_word_helper(client, page, phase, reg,
3205239277eSPatrick Rudolph 					      GENMASK(7, 0));
3215239277eSPatrick Rudolph 		break;
3225239277eSPatrick Rudolph 	case PMBUS_VIN_OV_FAULT_LIMIT:
3235239277eSPatrick Rudolph 		ret = mp2975_read_word_helper(client, page, phase, reg,
3245239277eSPatrick Rudolph 					      GENMASK(7, 0));
3255239277eSPatrick Rudolph 		if (ret < 0)
3265239277eSPatrick Rudolph 			return ret;
3275239277eSPatrick Rudolph 
3285239277eSPatrick Rudolph 		ret = DIV_ROUND_CLOSEST(ret, MP2975_VIN_UV_LIMIT_UNIT);
3295239277eSPatrick Rudolph 		break;
3305239277eSPatrick Rudolph 	case PMBUS_VOUT_OV_FAULT_LIMIT:
3315239277eSPatrick Rudolph 		/*
3325239277eSPatrick Rudolph 		 * MP2971 and mp2973 only supports tracking (ovp1) mode.
3335239277eSPatrick Rudolph 		 */
3345239277eSPatrick Rudolph 		ret = mp2975_read_word_helper(client, page, phase,
3355239277eSPatrick Rudolph 					      MP2975_MFR_OVP_TH_SET,
3365239277eSPatrick Rudolph 					      GENMASK(2, 0));
3375239277eSPatrick Rudolph 		if (ret < 0)
3385239277eSPatrick Rudolph 			return ret;
3395239277eSPatrick Rudolph 
3405239277eSPatrick Rudolph 		ret = data->vout_max[page] + 50 * (ret + 1);
3415239277eSPatrick Rudolph 		break;
3425239277eSPatrick Rudolph 	case PMBUS_VOUT_UV_FAULT_LIMIT:
3435239277eSPatrick Rudolph 		ret = mp2975_read_word_helper(client, page, phase, reg,
3445239277eSPatrick Rudolph 					      GENMASK(8, 0));
3455239277eSPatrick Rudolph 		if (ret < 0)
3465239277eSPatrick Rudolph 			return ret;
3475239277eSPatrick Rudolph 		ret = mp2975_vid2direct(info->vrm_version[page], ret);
3485239277eSPatrick Rudolph 		break;
3495239277eSPatrick Rudolph 	case PMBUS_VIRT_READ_POUT_MAX:
3505239277eSPatrick Rudolph 		ret = pmbus_read_word_data(client, page, phase,
3515239277eSPatrick Rudolph 					   MP2973_MFR_READ_POUT_PK);
3525239277eSPatrick Rudolph 		break;
3535239277eSPatrick Rudolph 	case PMBUS_VIRT_READ_IOUT_MAX:
3545239277eSPatrick Rudolph 		ret = pmbus_read_word_data(client, page, phase,
3555239277eSPatrick Rudolph 					   MP2973_MFR_READ_IOUT_PK);
3565239277eSPatrick Rudolph 		break;
35745f154dcSPatrick Rudolph 	case PMBUS_IOUT_OC_FAULT_LIMIT:
35845f154dcSPatrick Rudolph 		ret = mp2975_read_word_helper(client, page, phase,
35945f154dcSPatrick Rudolph 					      MP2973_MFR_OCP_TOTAL_SET,
36045f154dcSPatrick Rudolph 					      GENMASK(15, 0));
36145f154dcSPatrick Rudolph 		if (ret < 0)
36245f154dcSPatrick Rudolph 			return ret;
36345f154dcSPatrick Rudolph 
36445f154dcSPatrick Rudolph 		if (ret & MP2973_MFR_OCP_LEVEL_RES)
36545f154dcSPatrick Rudolph 			ret = 2 * (ret & MP2973_OCP_TOTAL_CUR_MASK);
36645f154dcSPatrick Rudolph 		else
36745f154dcSPatrick Rudolph 			ret = ret & MP2973_OCP_TOTAL_CUR_MASK;
36845f154dcSPatrick Rudolph 
36945f154dcSPatrick Rudolph 		ret = mp2975_data2reg_linear11(ret * info->phases[page] * 1000);
37045f154dcSPatrick Rudolph 		break;
3715239277eSPatrick Rudolph 	case PMBUS_UT_WARN_LIMIT:
3725239277eSPatrick Rudolph 	case PMBUS_UT_FAULT_LIMIT:
3735239277eSPatrick Rudolph 	case PMBUS_VIN_UV_WARN_LIMIT:
3745239277eSPatrick Rudolph 	case PMBUS_VIN_UV_FAULT_LIMIT:
3755239277eSPatrick Rudolph 	case PMBUS_VOUT_UV_WARN_LIMIT:
3765239277eSPatrick Rudolph 	case PMBUS_VOUT_OV_WARN_LIMIT:
3775239277eSPatrick Rudolph 	case PMBUS_VIN_OV_WARN_LIMIT:
3785239277eSPatrick Rudolph 	case PMBUS_IIN_OC_FAULT_LIMIT:
3795239277eSPatrick Rudolph 	case PMBUS_IOUT_OC_LV_FAULT_LIMIT:
3805239277eSPatrick Rudolph 	case PMBUS_IOUT_OC_WARN_LIMIT:
3815239277eSPatrick Rudolph 	case PMBUS_IOUT_UC_FAULT_LIMIT:
3825239277eSPatrick Rudolph 	case PMBUS_POUT_OP_FAULT_LIMIT:
3835239277eSPatrick Rudolph 	case PMBUS_POUT_OP_WARN_LIMIT:
3845239277eSPatrick Rudolph 	case PMBUS_PIN_OP_WARN_LIMIT:
3855239277eSPatrick Rudolph 		return -ENXIO;
3865239277eSPatrick Rudolph 	default:
3875239277eSPatrick Rudolph 		return -ENODATA;
3885239277eSPatrick Rudolph 	}
3895239277eSPatrick Rudolph 
3905239277eSPatrick Rudolph 	return ret;
3915239277eSPatrick Rudolph }
3925239277eSPatrick Rudolph 
mp2973_write_word_data(struct i2c_client * client,int page,int reg,u16 word)393c7506a2bSPatrick Rudolph static int mp2973_write_word_data(struct i2c_client *client, int page,
394c7506a2bSPatrick Rudolph 				  int reg, u16 word)
395c7506a2bSPatrick Rudolph {
396c7506a2bSPatrick Rudolph 	u8 target, mask;
397d2bc4a99SAndy Shevchenko 	long ret;
398c7506a2bSPatrick Rudolph 
399c7506a2bSPatrick Rudolph 	if (reg != PMBUS_SMBALERT_MASK)
400c7506a2bSPatrick Rudolph 		return -ENODATA;
401c7506a2bSPatrick Rudolph 
402c7506a2bSPatrick Rudolph 	/*
403c7506a2bSPatrick Rudolph 	 * Vendor-specific SMBALERT_MASK register with 16 maskable bits.
404c7506a2bSPatrick Rudolph 	 */
405c7506a2bSPatrick Rudolph 	ret = pmbus_read_word_data(client, 0, 0, PMBUS_SMBALERT_MASK);
406c7506a2bSPatrick Rudolph 	if (ret < 0)
407c7506a2bSPatrick Rudolph 		return ret;
408c7506a2bSPatrick Rudolph 
409c7506a2bSPatrick Rudolph 	target = word & 0xff;
410c7506a2bSPatrick Rudolph 	mask = word >> 8;
411c7506a2bSPatrick Rudolph 
412c7506a2bSPatrick Rudolph /*
413c7506a2bSPatrick Rudolph  * Set/Clear 'bit' in 'ret' based on condition followed by define for each bit in SMBALERT_MASK.
414c7506a2bSPatrick Rudolph  * Also bit 2 & 15 are reserved.
415c7506a2bSPatrick Rudolph  */
416c7506a2bSPatrick Rudolph 
417c7506a2bSPatrick Rudolph #define MP2973_TEMP_OT		0
418c7506a2bSPatrick Rudolph #define MP2973_VIN_UVLO		1
419c7506a2bSPatrick Rudolph #define MP2973_VIN_OVP		3
420c7506a2bSPatrick Rudolph #define MP2973_MTP_FAULT	4
421c7506a2bSPatrick Rudolph #define MP2973_OTHER_COMM	5
422c7506a2bSPatrick Rudolph #define MP2973_MTP_BLK_TRIG	6
423c7506a2bSPatrick Rudolph #define MP2973_PACKET_ERROR	7
424c7506a2bSPatrick Rudolph #define MP2973_INVALID_DATA	8
425c7506a2bSPatrick Rudolph #define MP2973_INVALID_COMMAND	9
426c7506a2bSPatrick Rudolph #define MP2973_IOUT_OC_LV	10
427c7506a2bSPatrick Rudolph #define MP2973_IOUT_OC		11
428c7506a2bSPatrick Rudolph #define MP2973_VOUT_MAX_MIN_WARNING 12
429c7506a2bSPatrick Rudolph #define MP2973_VOLTAGE_UV	13
430c7506a2bSPatrick Rudolph #define MP2973_VOLTAGE_OV	14
431c7506a2bSPatrick Rudolph 
432c7506a2bSPatrick Rudolph 	switch (target) {
433c7506a2bSPatrick Rudolph 	case PMBUS_STATUS_CML:
434d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_INVALID_DATA, &ret, !(mask & PB_CML_FAULT_INVALID_DATA));
435d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_INVALID_COMMAND, &ret, !(mask & PB_CML_FAULT_INVALID_COMMAND));
436d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_OTHER_COMM, &ret, !(mask & PB_CML_FAULT_OTHER_COMM));
437d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_PACKET_ERROR, &ret, !(mask & PB_CML_FAULT_PACKET_ERROR));
438c7506a2bSPatrick Rudolph 		break;
439c7506a2bSPatrick Rudolph 	case PMBUS_STATUS_VOUT:
440d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_VOLTAGE_UV, &ret, !(mask & PB_VOLTAGE_UV_FAULT));
441d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_VOLTAGE_OV, &ret, !(mask & PB_VOLTAGE_OV_FAULT));
442c7506a2bSPatrick Rudolph 		break;
443c7506a2bSPatrick Rudolph 	case PMBUS_STATUS_IOUT:
444d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_IOUT_OC, &ret, !(mask & PB_IOUT_OC_FAULT));
445d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_IOUT_OC_LV, &ret, !(mask & PB_IOUT_OC_LV_FAULT));
446c7506a2bSPatrick Rudolph 		break;
447c7506a2bSPatrick Rudolph 	case PMBUS_STATUS_TEMPERATURE:
448d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_TEMP_OT, &ret, !(mask & PB_TEMP_OT_FAULT));
449c7506a2bSPatrick Rudolph 		break;
450c7506a2bSPatrick Rudolph 	/*
451c7506a2bSPatrick Rudolph 	 * Map remaining bits to MFR specific to let the PMBUS core mask
452c7506a2bSPatrick Rudolph 	 * those bits by default.
453c7506a2bSPatrick Rudolph 	 */
454c7506a2bSPatrick Rudolph 	case PMBUS_STATUS_MFR_SPECIFIC:
455d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_VIN_UVLO, &ret, !(mask & BIT(1)));
456d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_VIN_OVP, &ret, !(mask & BIT(3)));
457d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_MTP_FAULT, &ret, !(mask & BIT(4)));
458d2bc4a99SAndy Shevchenko 		__assign_bit(MP2973_MTP_BLK_TRIG, &ret, !(mask & BIT(6)));
459c7506a2bSPatrick Rudolph 		break;
460c7506a2bSPatrick Rudolph 	default:
461c7506a2bSPatrick Rudolph 		return 0;
462c7506a2bSPatrick Rudolph 	}
463c7506a2bSPatrick Rudolph 
464c7506a2bSPatrick Rudolph 	return pmbus_write_word_data(client, 0, PMBUS_SMBALERT_MASK, ret);
465c7506a2bSPatrick Rudolph }
466c7506a2bSPatrick Rudolph 
mp2975_read_word_data(struct i2c_client * client,int page,int phase,int reg)4672c6fcbb2SVadim Pasternak static int mp2975_read_word_data(struct i2c_client *client, int page,
4682c6fcbb2SVadim Pasternak 				 int phase, int reg)
4692c6fcbb2SVadim Pasternak {
4702c6fcbb2SVadim Pasternak 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
4712c6fcbb2SVadim Pasternak 	struct mp2975_data *data = to_mp2975_data(info);
4722c6fcbb2SVadim Pasternak 	int ret;
4732c6fcbb2SVadim Pasternak 
4742c6fcbb2SVadim Pasternak 	switch (reg) {
4752c6fcbb2SVadim Pasternak 	case PMBUS_OT_FAULT_LIMIT:
4762c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase, reg,
4772c6fcbb2SVadim Pasternak 					      GENMASK(7, 0));
4782c6fcbb2SVadim Pasternak 		break;
4792c6fcbb2SVadim Pasternak 	case PMBUS_VIN_OV_FAULT_LIMIT:
4802c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase, reg,
4812c6fcbb2SVadim Pasternak 					      GENMASK(7, 0));
4822c6fcbb2SVadim Pasternak 		if (ret < 0)
4832c6fcbb2SVadim Pasternak 			return ret;
4842c6fcbb2SVadim Pasternak 
4852c6fcbb2SVadim Pasternak 		ret = DIV_ROUND_CLOSEST(ret, MP2975_VIN_UV_LIMIT_UNIT);
4862c6fcbb2SVadim Pasternak 		break;
4872c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_OV_FAULT_LIMIT:
4882c6fcbb2SVadim Pasternak 		/*
4892c6fcbb2SVadim Pasternak 		 * Register provides two values for over-voltage protection
4902c6fcbb2SVadim Pasternak 		 * threshold for fixed (ovp2) and tracking (ovp1) modes. The
4912c6fcbb2SVadim Pasternak 		 * minimum of these two values is provided as over-voltage
4922c6fcbb2SVadim Pasternak 		 * fault alarm.
4932c6fcbb2SVadim Pasternak 		 */
4942c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase,
4952c6fcbb2SVadim Pasternak 					      MP2975_MFR_OVP_TH_SET,
4962c6fcbb2SVadim Pasternak 					      GENMASK(2, 0));
4972c6fcbb2SVadim Pasternak 		if (ret < 0)
4982c6fcbb2SVadim Pasternak 			return ret;
4992c6fcbb2SVadim Pasternak 
5002c6fcbb2SVadim Pasternak 		ret = min_t(int, data->vout_max[page] + 50 * (ret + 1),
5012c6fcbb2SVadim Pasternak 			    data->vout_ov_fixed[page]);
5022c6fcbb2SVadim Pasternak 		break;
5032c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_UV_FAULT_LIMIT:
5042c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase,
5052c6fcbb2SVadim Pasternak 					      MP2975_MFR_UVP_SET,
5062c6fcbb2SVadim Pasternak 					      GENMASK(2, 0));
5072c6fcbb2SVadim Pasternak 		if (ret < 0)
5082c6fcbb2SVadim Pasternak 			return ret;
5092c6fcbb2SVadim Pasternak 
5102c6fcbb2SVadim Pasternak 		ret = DIV_ROUND_CLOSEST(data->vref[page] * 10 - 50 *
5112c6fcbb2SVadim Pasternak 					(ret + 1) * data->vout_scale, 10);
5122c6fcbb2SVadim Pasternak 		break;
5132c6fcbb2SVadim Pasternak 	case PMBUS_VIRT_READ_POUT_MAX:
5142c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase,
5152c6fcbb2SVadim Pasternak 					      MP2975_MFR_READ_POUT_PK,
5162c6fcbb2SVadim Pasternak 					      GENMASK(12, 0));
5172c6fcbb2SVadim Pasternak 		if (ret < 0)
5182c6fcbb2SVadim Pasternak 			return ret;
5192c6fcbb2SVadim Pasternak 
5202c6fcbb2SVadim Pasternak 		ret = DIV_ROUND_CLOSEST(ret, 4);
5212c6fcbb2SVadim Pasternak 		break;
5222c6fcbb2SVadim Pasternak 	case PMBUS_VIRT_READ_IOUT_MAX:
5232c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase,
5242c6fcbb2SVadim Pasternak 					      MP2975_MFR_READ_IOUT_PK,
5252c6fcbb2SVadim Pasternak 					      GENMASK(12, 0));
5262c6fcbb2SVadim Pasternak 		if (ret < 0)
5272c6fcbb2SVadim Pasternak 			return ret;
5282c6fcbb2SVadim Pasternak 
5292c6fcbb2SVadim Pasternak 		ret = DIV_ROUND_CLOSEST(ret, 4);
5302c6fcbb2SVadim Pasternak 		break;
5312c6fcbb2SVadim Pasternak 	case PMBUS_READ_IOUT:
5322c6fcbb2SVadim Pasternak 		ret = mp2975_read_phases(client, data, page, phase);
5332c6fcbb2SVadim Pasternak 		if (ret < 0)
5342c6fcbb2SVadim Pasternak 			return ret;
5352c6fcbb2SVadim Pasternak 
5362c6fcbb2SVadim Pasternak 		break;
5372c6fcbb2SVadim Pasternak 	case PMBUS_UT_WARN_LIMIT:
5382c6fcbb2SVadim Pasternak 	case PMBUS_UT_FAULT_LIMIT:
5392c6fcbb2SVadim Pasternak 	case PMBUS_VIN_UV_WARN_LIMIT:
5402c6fcbb2SVadim Pasternak 	case PMBUS_VIN_UV_FAULT_LIMIT:
5412c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_UV_WARN_LIMIT:
5422c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_OV_WARN_LIMIT:
5432c6fcbb2SVadim Pasternak 	case PMBUS_VIN_OV_WARN_LIMIT:
5442c6fcbb2SVadim Pasternak 	case PMBUS_IIN_OC_FAULT_LIMIT:
5452c6fcbb2SVadim Pasternak 	case PMBUS_IOUT_OC_LV_FAULT_LIMIT:
5462c6fcbb2SVadim Pasternak 	case PMBUS_IIN_OC_WARN_LIMIT:
5472c6fcbb2SVadim Pasternak 	case PMBUS_IOUT_OC_WARN_LIMIT:
5482c6fcbb2SVadim Pasternak 	case PMBUS_IOUT_OC_FAULT_LIMIT:
5492c6fcbb2SVadim Pasternak 	case PMBUS_IOUT_UC_FAULT_LIMIT:
5502c6fcbb2SVadim Pasternak 	case PMBUS_POUT_OP_FAULT_LIMIT:
5512c6fcbb2SVadim Pasternak 	case PMBUS_POUT_OP_WARN_LIMIT:
5522c6fcbb2SVadim Pasternak 	case PMBUS_PIN_OP_WARN_LIMIT:
5532c6fcbb2SVadim Pasternak 		return -ENXIO;
5542c6fcbb2SVadim Pasternak 	default:
5552c6fcbb2SVadim Pasternak 		return -ENODATA;
5562c6fcbb2SVadim Pasternak 	}
5572c6fcbb2SVadim Pasternak 
5582c6fcbb2SVadim Pasternak 	return ret;
5592c6fcbb2SVadim Pasternak }
5602c6fcbb2SVadim Pasternak 
mp2975_identify_multiphase_rail2(struct i2c_client * client,struct mp2975_data * data)561e2c90b48SPatrick Rudolph static int mp2975_identify_multiphase_rail2(struct i2c_client *client,
562e2c90b48SPatrick Rudolph 					    struct mp2975_data *data)
5632c6fcbb2SVadim Pasternak {
5642c6fcbb2SVadim Pasternak 	int ret;
5652c6fcbb2SVadim Pasternak 
5662c6fcbb2SVadim Pasternak 	/*
567e2c90b48SPatrick Rudolph 	 * Identify multiphase for rail 2 - could be from 0 to data->max_phases[1].
5682c6fcbb2SVadim Pasternak 	 * In case phase number is zero – only page zero is supported
5692c6fcbb2SVadim Pasternak 	 */
5702c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
5712c6fcbb2SVadim Pasternak 	if (ret < 0)
5722c6fcbb2SVadim Pasternak 		return ret;
5732c6fcbb2SVadim Pasternak 
5742c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_VR_MULTI_CONFIG_R2);
5752c6fcbb2SVadim Pasternak 	if (ret < 0)
5762c6fcbb2SVadim Pasternak 		return ret;
5772c6fcbb2SVadim Pasternak 
5782c6fcbb2SVadim Pasternak 	ret &= GENMASK(2, 0);
579e2c90b48SPatrick Rudolph 	return (ret >= data->max_phases[1]) ? data->max_phases[1] : ret;
5802c6fcbb2SVadim Pasternak }
5812c6fcbb2SVadim Pasternak 
mp2975_set_phase_rail1(struct pmbus_driver_info * info)5822c6fcbb2SVadim Pasternak static void mp2975_set_phase_rail1(struct pmbus_driver_info *info)
5832c6fcbb2SVadim Pasternak {
5842c6fcbb2SVadim Pasternak 	int i;
5852c6fcbb2SVadim Pasternak 
5862c6fcbb2SVadim Pasternak 	for (i = 0 ; i < info->phases[0]; i++)
5872c6fcbb2SVadim Pasternak 		info->pfunc[i] = PMBUS_HAVE_IOUT;
5882c6fcbb2SVadim Pasternak }
5892c6fcbb2SVadim Pasternak 
5902c6fcbb2SVadim Pasternak static void
mp2975_set_phase_rail2(struct pmbus_driver_info * info,int num_phases)5912c6fcbb2SVadim Pasternak mp2975_set_phase_rail2(struct pmbus_driver_info *info, int num_phases)
5922c6fcbb2SVadim Pasternak {
5932c6fcbb2SVadim Pasternak 	int i;
5942c6fcbb2SVadim Pasternak 
5952c6fcbb2SVadim Pasternak 	/* Set phases for rail 2 from upper to lower. */
5962c6fcbb2SVadim Pasternak 	for (i = 1; i <= num_phases; i++)
5972c6fcbb2SVadim Pasternak 		info->pfunc[MP2975_MAX_PHASE_RAIL1 - i] = PMBUS_HAVE_IOUT;
5982c6fcbb2SVadim Pasternak }
5992c6fcbb2SVadim Pasternak 
6002c6fcbb2SVadim Pasternak static int
mp2975_identify_multiphase(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info)6012c6fcbb2SVadim Pasternak mp2975_identify_multiphase(struct i2c_client *client, struct mp2975_data *data,
6022c6fcbb2SVadim Pasternak 			   struct pmbus_driver_info *info)
6032c6fcbb2SVadim Pasternak {
6042c6fcbb2SVadim Pasternak 	int num_phases2, ret;
6052c6fcbb2SVadim Pasternak 
6062c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
6072c6fcbb2SVadim Pasternak 	if (ret < 0)
6082c6fcbb2SVadim Pasternak 		return ret;
6092c6fcbb2SVadim Pasternak 
610e2c90b48SPatrick Rudolph 	/* Identify multiphase for rail 1 - could be from 1 to data->max_phases[0]. */
6112c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_VR_MULTI_CONFIG_R1);
6122c6fcbb2SVadim Pasternak 	if (ret <= 0)
6132c6fcbb2SVadim Pasternak 		return ret;
6142c6fcbb2SVadim Pasternak 
6152c6fcbb2SVadim Pasternak 	info->phases[0] = ret & GENMASK(3, 0);
6162c6fcbb2SVadim Pasternak 
6172c6fcbb2SVadim Pasternak 	/*
618e2c90b48SPatrick Rudolph 	 * The device provides a total of $n PWM pins, and can be configured
6192c6fcbb2SVadim Pasternak 	 * to different phase count applications for rail 1 and rail 2.
620e2c90b48SPatrick Rudolph 	 * Rail 1 can be set to $n phases, while rail 2 can be set to less than
621e2c90b48SPatrick Rudolph 	 * that. When rail 1’s phase count is configured as 0, rail
6222c6fcbb2SVadim Pasternak 	 * 1 operates with 1-phase DCM. When rail 2 phase count is configured
6232c6fcbb2SVadim Pasternak 	 * as 0, rail 2 is disabled.
6242c6fcbb2SVadim Pasternak 	 */
625e2c90b48SPatrick Rudolph 	if (info->phases[0] > data->max_phases[0])
6262c6fcbb2SVadim Pasternak 		return -EINVAL;
6272c6fcbb2SVadim Pasternak 
62845f154dcSPatrick Rudolph 	if (data->chip_id == mp2975) {
6292c6fcbb2SVadim Pasternak 		mp2975_set_phase_rail1(info);
630e2c90b48SPatrick Rudolph 		num_phases2 = min(data->max_phases[0] - info->phases[0],
631e2c90b48SPatrick Rudolph 				  data->max_phases[1]);
6322c6fcbb2SVadim Pasternak 		if (info->phases[1] && info->phases[1] <= num_phases2)
6332c6fcbb2SVadim Pasternak 			mp2975_set_phase_rail2(info, num_phases2);
63445f154dcSPatrick Rudolph 	}
6352c6fcbb2SVadim Pasternak 
6362c6fcbb2SVadim Pasternak 	return 0;
6372c6fcbb2SVadim Pasternak }
6382c6fcbb2SVadim Pasternak 
6392c6fcbb2SVadim Pasternak static int
mp2975_identify_vid(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info,u32 reg,int page,u32 imvp_bit,u32 vr_bit)6402c6fcbb2SVadim Pasternak mp2975_identify_vid(struct i2c_client *client, struct mp2975_data *data,
6412c6fcbb2SVadim Pasternak 		    struct pmbus_driver_info *info, u32 reg, int page,
6422c6fcbb2SVadim Pasternak 		    u32 imvp_bit, u32 vr_bit)
6432c6fcbb2SVadim Pasternak {
6442c6fcbb2SVadim Pasternak 	int ret;
6452c6fcbb2SVadim Pasternak 
6462c6fcbb2SVadim Pasternak 	/* Identify VID mode and step selection. */
6472c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, reg);
6482c6fcbb2SVadim Pasternak 	if (ret < 0)
6492c6fcbb2SVadim Pasternak 		return ret;
6502c6fcbb2SVadim Pasternak 
6512c6fcbb2SVadim Pasternak 	if (ret & imvp_bit) {
6522c6fcbb2SVadim Pasternak 		info->vrm_version[page] = imvp9;
6532c6fcbb2SVadim Pasternak 		data->vid_step[page] = MP2975_PROT_DEV_OV_OFF;
6542c6fcbb2SVadim Pasternak 	} else if (ret & vr_bit) {
6552c6fcbb2SVadim Pasternak 		info->vrm_version[page] = vr12;
6562c6fcbb2SVadim Pasternak 		data->vid_step[page] = MP2975_PROT_DEV_OV_ON;
6572c6fcbb2SVadim Pasternak 	} else {
6582c6fcbb2SVadim Pasternak 		info->vrm_version[page] = vr13;
6592c6fcbb2SVadim Pasternak 		data->vid_step[page] = MP2975_PROT_DEV_OV_OFF;
6602c6fcbb2SVadim Pasternak 	}
6612c6fcbb2SVadim Pasternak 
6622c6fcbb2SVadim Pasternak 	return 0;
6632c6fcbb2SVadim Pasternak }
6642c6fcbb2SVadim Pasternak 
6652c6fcbb2SVadim Pasternak static int
mp2975_identify_rails_vid(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info)6662c6fcbb2SVadim Pasternak mp2975_identify_rails_vid(struct i2c_client *client, struct mp2975_data *data,
6672c6fcbb2SVadim Pasternak 			  struct pmbus_driver_info *info)
6682c6fcbb2SVadim Pasternak {
6692c6fcbb2SVadim Pasternak 	int ret;
6702c6fcbb2SVadim Pasternak 
6712c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
6722c6fcbb2SVadim Pasternak 	if (ret < 0)
6732c6fcbb2SVadim Pasternak 		return ret;
6742c6fcbb2SVadim Pasternak 
6752c6fcbb2SVadim Pasternak 	/* Identify VID mode for rail 1. */
6762c6fcbb2SVadim Pasternak 	ret = mp2975_identify_vid(client, data, info,
6772c6fcbb2SVadim Pasternak 				  MP2975_MFR_VR_MULTI_CONFIG_R1, 0,
6782c6fcbb2SVadim Pasternak 				  MP2975_IMVP9_EN_R1, MP2975_VID_STEP_SEL_R1);
6792c6fcbb2SVadim Pasternak 	if (ret < 0)
6802c6fcbb2SVadim Pasternak 		return ret;
6812c6fcbb2SVadim Pasternak 
6822c6fcbb2SVadim Pasternak 	/* Identify VID mode for rail 2, if connected. */
6832c6fcbb2SVadim Pasternak 	if (info->phases[1])
6842c6fcbb2SVadim Pasternak 		ret = mp2975_identify_vid(client, data, info,
6852c6fcbb2SVadim Pasternak 					  MP2975_MFR_VR_MULTI_CONFIG_R2, 1,
6862c6fcbb2SVadim Pasternak 					  MP2975_IMVP9_EN_R2,
6872c6fcbb2SVadim Pasternak 					  MP2975_VID_STEP_SEL_R2);
6885239277eSPatrick Rudolph 
6895239277eSPatrick Rudolph 	return ret;
6905239277eSPatrick Rudolph }
6915239277eSPatrick Rudolph 
6925239277eSPatrick Rudolph static int
mp2973_identify_rails_vid(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info)6935239277eSPatrick Rudolph mp2973_identify_rails_vid(struct i2c_client *client, struct mp2975_data *data,
6945239277eSPatrick Rudolph 			  struct pmbus_driver_info *info)
6955239277eSPatrick Rudolph {
6965239277eSPatrick Rudolph 	int ret;
6975239277eSPatrick Rudolph 
6985239277eSPatrick Rudolph 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
6995239277eSPatrick Rudolph 	if (ret < 0)
7005239277eSPatrick Rudolph 		return ret;
7015239277eSPatrick Rudolph 
7025239277eSPatrick Rudolph 	/* Identify VID mode for rail 1. */
7035239277eSPatrick Rudolph 	ret = mp2975_identify_vid(client, data, info,
7045239277eSPatrick Rudolph 				  MP2973_MFR_VR_MULTI_CONFIG_R1, 0,
7055239277eSPatrick Rudolph 				  MP2973_IMVP9_EN_R1, MP2973_VID_STEP_SEL_R1);
7065239277eSPatrick Rudolph 
7075239277eSPatrick Rudolph 	if (ret < 0)
7085239277eSPatrick Rudolph 		return ret;
7095239277eSPatrick Rudolph 
7105239277eSPatrick Rudolph 	/* Identify VID mode for rail 2, if connected. */
7115239277eSPatrick Rudolph 	if (info->phases[1])
7125239277eSPatrick Rudolph 		ret = mp2975_identify_vid(client, data, info,
7135239277eSPatrick Rudolph 					  MP2973_MFR_VR_MULTI_CONFIG_R2, 1,
7145239277eSPatrick Rudolph 					  MP2973_IMVP9_EN_R2,
7155239277eSPatrick Rudolph 					  MP2973_VID_STEP_SEL_R2);
7165239277eSPatrick Rudolph 
7172c6fcbb2SVadim Pasternak 	return ret;
7182c6fcbb2SVadim Pasternak }
7192c6fcbb2SVadim Pasternak 
7202c6fcbb2SVadim Pasternak static int
mp2975_current_sense_gain_get(struct i2c_client * client,struct mp2975_data * data)7212c6fcbb2SVadim Pasternak mp2975_current_sense_gain_get(struct i2c_client *client,
7222c6fcbb2SVadim Pasternak 			      struct mp2975_data *data)
7232c6fcbb2SVadim Pasternak {
7242c6fcbb2SVadim Pasternak 	int i, ret;
7252c6fcbb2SVadim Pasternak 
7262c6fcbb2SVadim Pasternak 	/*
7272c6fcbb2SVadim Pasternak 	 * Obtain DrMOS current sense gain of power stage from the register
7282c6fcbb2SVadim Pasternak 	 * MP2975_MFR_VR_CONFIG1, bits 13-12. The value is selected as below:
7292c6fcbb2SVadim Pasternak 	 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. Other
7302c6fcbb2SVadim Pasternak 	 * values are invalid.
7312c6fcbb2SVadim Pasternak 	 */
7322c6fcbb2SVadim Pasternak 	for (i = 0 ; i < data->info.pages; i++) {
7332c6fcbb2SVadim Pasternak 		ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
7342c6fcbb2SVadim Pasternak 		if (ret < 0)
7352c6fcbb2SVadim Pasternak 			return ret;
7362c6fcbb2SVadim Pasternak 		ret = i2c_smbus_read_word_data(client,
7372c6fcbb2SVadim Pasternak 					       MP2975_MFR_VR_CONFIG1);
7382c6fcbb2SVadim Pasternak 		if (ret < 0)
7392c6fcbb2SVadim Pasternak 			return ret;
7402c6fcbb2SVadim Pasternak 
7412c6fcbb2SVadim Pasternak 		switch ((ret & MP2975_DRMOS_KCS) >> 12) {
7422c6fcbb2SVadim Pasternak 		case 0:
7432c6fcbb2SVadim Pasternak 			data->curr_sense_gain[i] = 50;
7442c6fcbb2SVadim Pasternak 			break;
7452c6fcbb2SVadim Pasternak 		case 1:
7462c6fcbb2SVadim Pasternak 			data->curr_sense_gain[i] = 85;
7472c6fcbb2SVadim Pasternak 			break;
7482c6fcbb2SVadim Pasternak 		case 2:
7492c6fcbb2SVadim Pasternak 			data->curr_sense_gain[i] = 97;
7502c6fcbb2SVadim Pasternak 			break;
7512c6fcbb2SVadim Pasternak 		default:
7522c6fcbb2SVadim Pasternak 			data->curr_sense_gain[i] = 100;
7532c6fcbb2SVadim Pasternak 			break;
7542c6fcbb2SVadim Pasternak 		}
7552c6fcbb2SVadim Pasternak 	}
7562c6fcbb2SVadim Pasternak 
7572c6fcbb2SVadim Pasternak 	return 0;
7582c6fcbb2SVadim Pasternak }
7592c6fcbb2SVadim Pasternak 
7602c6fcbb2SVadim Pasternak static int
mp2975_vref_get(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info)7612c6fcbb2SVadim Pasternak mp2975_vref_get(struct i2c_client *client, struct mp2975_data *data,
7622c6fcbb2SVadim Pasternak 		struct pmbus_driver_info *info)
7632c6fcbb2SVadim Pasternak {
7642c6fcbb2SVadim Pasternak 	int ret;
7652c6fcbb2SVadim Pasternak 
7662c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 3);
7672c6fcbb2SVadim Pasternak 	if (ret < 0)
7682c6fcbb2SVadim Pasternak 		return ret;
7692c6fcbb2SVadim Pasternak 
7702c6fcbb2SVadim Pasternak 	/* Get voltage reference value for rail 1. */
7712c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_READ_VREF_R1);
7722c6fcbb2SVadim Pasternak 	if (ret < 0)
7732c6fcbb2SVadim Pasternak 		return ret;
7742c6fcbb2SVadim Pasternak 
7752c6fcbb2SVadim Pasternak 	data->vref[0] = ret * data->vid_step[0];
7762c6fcbb2SVadim Pasternak 
7772c6fcbb2SVadim Pasternak 	/* Get voltage reference value for rail 2, if connected. */
7782c6fcbb2SVadim Pasternak 	if (data->info.pages == MP2975_PAGE_NUM) {
7792c6fcbb2SVadim Pasternak 		ret = i2c_smbus_read_word_data(client, MP2975_MFR_READ_VREF_R2);
7802c6fcbb2SVadim Pasternak 		if (ret < 0)
7812c6fcbb2SVadim Pasternak 			return ret;
7822c6fcbb2SVadim Pasternak 
7832c6fcbb2SVadim Pasternak 		data->vref[1] = ret * data->vid_step[1];
7842c6fcbb2SVadim Pasternak 	}
7852c6fcbb2SVadim Pasternak 	return 0;
7862c6fcbb2SVadim Pasternak }
7872c6fcbb2SVadim Pasternak 
7882c6fcbb2SVadim Pasternak static int
mp2975_vref_offset_get(struct i2c_client * client,struct mp2975_data * data,int page)7892c6fcbb2SVadim Pasternak mp2975_vref_offset_get(struct i2c_client *client, struct mp2975_data *data,
7902c6fcbb2SVadim Pasternak 		       int page)
7912c6fcbb2SVadim Pasternak {
7922c6fcbb2SVadim Pasternak 	int ret;
7932c6fcbb2SVadim Pasternak 
7942c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_OVP_TH_SET);
7952c6fcbb2SVadim Pasternak 	if (ret < 0)
7962c6fcbb2SVadim Pasternak 		return ret;
7972c6fcbb2SVadim Pasternak 
7982c6fcbb2SVadim Pasternak 	switch ((ret & GENMASK(5, 3)) >> 3) {
7992c6fcbb2SVadim Pasternak 	case 1:
8002c6fcbb2SVadim Pasternak 		data->vref_off[page] = 140;
8012c6fcbb2SVadim Pasternak 		break;
8022c6fcbb2SVadim Pasternak 	case 2:
8032c6fcbb2SVadim Pasternak 		data->vref_off[page] = 220;
8042c6fcbb2SVadim Pasternak 		break;
8052c6fcbb2SVadim Pasternak 	case 4:
8062c6fcbb2SVadim Pasternak 		data->vref_off[page] = 400;
8072c6fcbb2SVadim Pasternak 		break;
8082c6fcbb2SVadim Pasternak 	default:
8092c6fcbb2SVadim Pasternak 		return -EINVAL;
8102c6fcbb2SVadim Pasternak 	}
8112c6fcbb2SVadim Pasternak 	return 0;
8122c6fcbb2SVadim Pasternak }
8132c6fcbb2SVadim Pasternak 
8142c6fcbb2SVadim Pasternak static int
mp2975_vout_max_get(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info,int page)8152c6fcbb2SVadim Pasternak mp2975_vout_max_get(struct i2c_client *client, struct mp2975_data *data,
8162c6fcbb2SVadim Pasternak 		    struct pmbus_driver_info *info, int page)
8172c6fcbb2SVadim Pasternak {
8182c6fcbb2SVadim Pasternak 	int ret;
8192c6fcbb2SVadim Pasternak 
8202c6fcbb2SVadim Pasternak 	/* Get maximum reference voltage of VID-DAC in VID format. */
8212c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, PMBUS_VOUT_MAX);
8222c6fcbb2SVadim Pasternak 	if (ret < 0)
8232c6fcbb2SVadim Pasternak 		return ret;
8242c6fcbb2SVadim Pasternak 
8252c6fcbb2SVadim Pasternak 	data->vout_max[page] = mp2975_vid2direct(info->vrm_version[page], ret &
8262c6fcbb2SVadim Pasternak 						 GENMASK(8, 0));
8272c6fcbb2SVadim Pasternak 	return 0;
8282c6fcbb2SVadim Pasternak }
8292c6fcbb2SVadim Pasternak 
8302c6fcbb2SVadim Pasternak static int
mp2975_set_vout_format(struct i2c_client * client,struct mp2975_data * data,int page)8311feb31e8SPatrick Rudolph mp2975_set_vout_format(struct i2c_client *client,
8322c6fcbb2SVadim Pasternak 		       struct mp2975_data *data, int page)
8332c6fcbb2SVadim Pasternak {
8345239277eSPatrick Rudolph 	int ret, i;
8352c6fcbb2SVadim Pasternak 
8365239277eSPatrick Rudolph 	/* Enable DIRECT VOUT format 1mV/LSB */
8375239277eSPatrick Rudolph 	if (data->chip_id == mp2975) {
8382c6fcbb2SVadim Pasternak 		ret = i2c_smbus_read_word_data(client, MP2975_MFR_DC_LOOP_CTRL);
8392c6fcbb2SVadim Pasternak 		if (ret < 0)
8402c6fcbb2SVadim Pasternak 			return ret;
8411feb31e8SPatrick Rudolph 		if (ret & MP2975_VOUT_FORMAT) {
8421feb31e8SPatrick Rudolph 			ret &= ~MP2975_VOUT_FORMAT;
8431feb31e8SPatrick Rudolph 			ret = i2c_smbus_write_word_data(client, MP2975_MFR_DC_LOOP_CTRL, ret);
8441feb31e8SPatrick Rudolph 		}
8455239277eSPatrick Rudolph 	} else {
8465239277eSPatrick Rudolph 		ret = i2c_smbus_read_word_data(client, MP2973_MFR_RESO_SET);
8475239277eSPatrick Rudolph 		if (ret < 0)
8485239277eSPatrick Rudolph 			return ret;
8495239277eSPatrick Rudolph 		i = ret;
8505239277eSPatrick Rudolph 
8515239277eSPatrick Rudolph 		if (page == 0) {
8525239277eSPatrick Rudolph 			i &= ~MP2973_VOUT_FORMAT_R1;
8535239277eSPatrick Rudolph 			i |= MP2973_VOUT_FORMAT_DIRECT_R1;
8545239277eSPatrick Rudolph 		} else {
8555239277eSPatrick Rudolph 			i &= ~MP2973_VOUT_FORMAT_R2;
8565239277eSPatrick Rudolph 			i |= MP2973_VOUT_FORMAT_DIRECT_R2;
8575239277eSPatrick Rudolph 		}
8585239277eSPatrick Rudolph 		if (i != ret)
8595239277eSPatrick Rudolph 			ret = i2c_smbus_write_word_data(client, MP2973_MFR_RESO_SET, i);
8605239277eSPatrick Rudolph 	}
8611feb31e8SPatrick Rudolph 	return ret;
8622c6fcbb2SVadim Pasternak }
8632c6fcbb2SVadim Pasternak 
8642c6fcbb2SVadim Pasternak static int
mp2975_vout_ov_scale_get(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info)8652c6fcbb2SVadim Pasternak mp2975_vout_ov_scale_get(struct i2c_client *client, struct mp2975_data *data,
8662c6fcbb2SVadim Pasternak 			 struct pmbus_driver_info *info)
8672c6fcbb2SVadim Pasternak {
8682c6fcbb2SVadim Pasternak 	int thres_dev, sense_ampl, ret;
8692c6fcbb2SVadim Pasternak 
8702c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
8712c6fcbb2SVadim Pasternak 	if (ret < 0)
8722c6fcbb2SVadim Pasternak 		return ret;
8732c6fcbb2SVadim Pasternak 
8742c6fcbb2SVadim Pasternak 	/*
8752c6fcbb2SVadim Pasternak 	 * Get divider for over- and under-voltage protection thresholds
8762c6fcbb2SVadim Pasternak 	 * configuration from the Advanced Options of Auto Phase Shedding and
8772c6fcbb2SVadim Pasternak 	 * decay register.
8782c6fcbb2SVadim Pasternak 	 */
8792c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_APS_DECAY_ADV);
8802c6fcbb2SVadim Pasternak 	if (ret < 0)
8812c6fcbb2SVadim Pasternak 		return ret;
8822c6fcbb2SVadim Pasternak 	thres_dev = ret & MP2975_PRT_THRES_DIV_OV_EN ? MP2975_PROT_DEV_OV_ON :
8832c6fcbb2SVadim Pasternak 						       MP2975_PROT_DEV_OV_OFF;
8842c6fcbb2SVadim Pasternak 
8852c6fcbb2SVadim Pasternak 	/* Select the gain of remote sense amplifier. */
8862c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, PMBUS_VOUT_SCALE_LOOP);
8872c6fcbb2SVadim Pasternak 	if (ret < 0)
8882c6fcbb2SVadim Pasternak 		return ret;
8892c6fcbb2SVadim Pasternak 	sense_ampl = ret & MP2975_SENSE_AMPL ? MP2975_SENSE_AMPL_HALF :
8902c6fcbb2SVadim Pasternak 					       MP2975_SENSE_AMPL_UNIT;
8912c6fcbb2SVadim Pasternak 
8922c6fcbb2SVadim Pasternak 	data->vout_scale = sense_ampl * thres_dev;
8932c6fcbb2SVadim Pasternak 
8942c6fcbb2SVadim Pasternak 	return 0;
8952c6fcbb2SVadim Pasternak }
8962c6fcbb2SVadim Pasternak 
8972c6fcbb2SVadim Pasternak static int
mp2975_vout_per_rail_config_get(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info)8982c6fcbb2SVadim Pasternak mp2975_vout_per_rail_config_get(struct i2c_client *client,
8992c6fcbb2SVadim Pasternak 				struct mp2975_data *data,
9002c6fcbb2SVadim Pasternak 				struct pmbus_driver_info *info)
9012c6fcbb2SVadim Pasternak {
9022c6fcbb2SVadim Pasternak 	int i, ret;
9032c6fcbb2SVadim Pasternak 
9042c6fcbb2SVadim Pasternak 	for (i = 0; i < data->info.pages; i++) {
9052c6fcbb2SVadim Pasternak 		ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
9062c6fcbb2SVadim Pasternak 		if (ret < 0)
9075239277eSPatrick Rudolph 			continue;
9082c6fcbb2SVadim Pasternak 
9095239277eSPatrick Rudolph 		/* Set VOUT format for READ_VOUT command : direct. */
9105239277eSPatrick Rudolph 		ret = mp2975_set_vout_format(client, data, i);
9112c6fcbb2SVadim Pasternak 		if (ret < 0)
9122c6fcbb2SVadim Pasternak 			return ret;
9132c6fcbb2SVadim Pasternak 
9142c6fcbb2SVadim Pasternak 		/* Obtain maximum voltage values. */
9152c6fcbb2SVadim Pasternak 		ret = mp2975_vout_max_get(client, data, info, i);
9162c6fcbb2SVadim Pasternak 		if (ret < 0)
9172c6fcbb2SVadim Pasternak 			return ret;
9182c6fcbb2SVadim Pasternak 
9195239277eSPatrick Rudolph 		/* Skip if reading Vref is unsupported */
9205239277eSPatrick Rudolph 		if (data->chip_id != mp2975)
9215239277eSPatrick Rudolph 			continue;
9225239277eSPatrick Rudolph 
9235239277eSPatrick Rudolph 		/* Obtain voltage reference offsets. */
9245239277eSPatrick Rudolph 		ret = mp2975_vref_offset_get(client, data, i);
9252c6fcbb2SVadim Pasternak 		if (ret < 0)
9262c6fcbb2SVadim Pasternak 			return ret;
9272c6fcbb2SVadim Pasternak 
9282c6fcbb2SVadim Pasternak 		/*
9292c6fcbb2SVadim Pasternak 		 * Set over-voltage fixed value. Thresholds are provided as
9302c6fcbb2SVadim Pasternak 		 * fixed value, and tracking value. The minimum of them are
9312c6fcbb2SVadim Pasternak 		 * exposed as over-voltage critical threshold.
9322c6fcbb2SVadim Pasternak 		 */
9332c6fcbb2SVadim Pasternak 		data->vout_ov_fixed[i] = data->vref[i] +
9342c6fcbb2SVadim Pasternak 					 DIV_ROUND_CLOSEST(data->vref_off[i] *
9352c6fcbb2SVadim Pasternak 							   data->vout_scale,
9362c6fcbb2SVadim Pasternak 							   10);
9372c6fcbb2SVadim Pasternak 	}
9382c6fcbb2SVadim Pasternak 
9392c6fcbb2SVadim Pasternak 	return 0;
9402c6fcbb2SVadim Pasternak }
9412c6fcbb2SVadim Pasternak 
9429050b39bSAndy Shevchenko static const struct pmbus_driver_info mp2975_info = {
9432c6fcbb2SVadim Pasternak 	.pages = 1,
9442c6fcbb2SVadim Pasternak 	.format[PSC_VOLTAGE_IN] = linear,
9452c6fcbb2SVadim Pasternak 	.format[PSC_VOLTAGE_OUT] = direct,
9462c6fcbb2SVadim Pasternak 	.format[PSC_TEMPERATURE] = direct,
9472c6fcbb2SVadim Pasternak 	.format[PSC_CURRENT_IN] = linear,
9482c6fcbb2SVadim Pasternak 	.format[PSC_CURRENT_OUT] = direct,
9492c6fcbb2SVadim Pasternak 	.format[PSC_POWER] = direct,
9502c6fcbb2SVadim Pasternak 	.m[PSC_TEMPERATURE] = 1,
9512c6fcbb2SVadim Pasternak 	.m[PSC_VOLTAGE_OUT] = 1,
9522c6fcbb2SVadim Pasternak 	.R[PSC_VOLTAGE_OUT] = 3,
9532c6fcbb2SVadim Pasternak 	.m[PSC_CURRENT_OUT] = 1,
9542c6fcbb2SVadim Pasternak 	.m[PSC_POWER] = 1,
9552c6fcbb2SVadim Pasternak 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
9562c6fcbb2SVadim Pasternak 		PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
9572c6fcbb2SVadim Pasternak 		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_POUT |
9582c6fcbb2SVadim Pasternak 		PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT | PMBUS_PHASE_VIRTUAL,
959c60fe56cSKonstantin Aladyshev 	.read_byte_data = mp2975_read_byte_data,
9602c6fcbb2SVadim Pasternak 	.read_word_data = mp2975_read_word_data,
96188fc1efcSPatrick Rudolph #if IS_ENABLED(CONFIG_SENSORS_MP2975_REGULATOR)
96288fc1efcSPatrick Rudolph 	.num_regulators = 1,
96388fc1efcSPatrick Rudolph 	.reg_desc = mp2975_reg_desc,
96488fc1efcSPatrick Rudolph #endif
9652c6fcbb2SVadim Pasternak };
9662c6fcbb2SVadim Pasternak 
9679050b39bSAndy Shevchenko static const struct pmbus_driver_info mp2973_info = {
9685239277eSPatrick Rudolph 	.pages = 1,
9695239277eSPatrick Rudolph 	.format[PSC_VOLTAGE_IN] = linear,
9705239277eSPatrick Rudolph 	.format[PSC_VOLTAGE_OUT] = direct,
9715239277eSPatrick Rudolph 	.format[PSC_TEMPERATURE] = linear,
9725239277eSPatrick Rudolph 	.format[PSC_CURRENT_IN] = linear,
9735239277eSPatrick Rudolph 	.format[PSC_CURRENT_OUT] = linear,
9745239277eSPatrick Rudolph 	.format[PSC_POWER] = linear,
9755239277eSPatrick Rudolph 	.m[PSC_VOLTAGE_OUT] = 1,
9765239277eSPatrick Rudolph 	.R[PSC_VOLTAGE_OUT] = 3,
9775239277eSPatrick Rudolph 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
9785239277eSPatrick Rudolph 		PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
9795239277eSPatrick Rudolph 		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_POUT |
9805239277eSPatrick Rudolph 		PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT,
9815239277eSPatrick Rudolph 	.read_word_data = mp2973_read_word_data,
982c7506a2bSPatrick Rudolph 	.write_word_data = mp2973_write_word_data,
98388fc1efcSPatrick Rudolph #if IS_ENABLED(CONFIG_SENSORS_MP2975_REGULATOR)
98488fc1efcSPatrick Rudolph 	.num_regulators = 1,
98588fc1efcSPatrick Rudolph 	.reg_desc = mp2975_reg_desc,
98688fc1efcSPatrick Rudolph #endif
9875239277eSPatrick Rudolph };
9885239277eSPatrick Rudolph 
989*73bc2357SAndy Shevchenko static const struct mp2975_driver_info mp2975_ddinfo[] = {
990*73bc2357SAndy Shevchenko 	[mp2975] = { .info = &mp2975_info, .chip_id = mp2975 },
991*73bc2357SAndy Shevchenko 	[mp2973] = { .info = &mp2973_info, .chip_id = mp2973 },
992*73bc2357SAndy Shevchenko 	[mp2971] = { .info = &mp2973_info, .chip_id = mp2971 },
993*73bc2357SAndy Shevchenko };
994*73bc2357SAndy Shevchenko 
mp2975_probe(struct i2c_client * client)9952c6fcbb2SVadim Pasternak static int mp2975_probe(struct i2c_client *client)
9962c6fcbb2SVadim Pasternak {
997*73bc2357SAndy Shevchenko 	const struct mp2975_driver_info *ddinfo;
9982c6fcbb2SVadim Pasternak 	struct pmbus_driver_info *info;
9992c6fcbb2SVadim Pasternak 	struct mp2975_data *data;
10002c6fcbb2SVadim Pasternak 	int ret;
10012c6fcbb2SVadim Pasternak 
1002*73bc2357SAndy Shevchenko 	ddinfo = i2c_get_match_data(client);
1003*73bc2357SAndy Shevchenko 	if (!ddinfo)
1004*73bc2357SAndy Shevchenko 		return -ENODEV;
1005*73bc2357SAndy Shevchenko 
10062c6fcbb2SVadim Pasternak 	data = devm_kzalloc(&client->dev, sizeof(struct mp2975_data),
10072c6fcbb2SVadim Pasternak 			    GFP_KERNEL);
10082c6fcbb2SVadim Pasternak 	if (!data)
10092c6fcbb2SVadim Pasternak 		return -ENOMEM;
10102c6fcbb2SVadim Pasternak 
1011*73bc2357SAndy Shevchenko 	data->chip_id = ddinfo->chip_id;
10121f6f34d0SPatrick Rudolph 
1013e2c90b48SPatrick Rudolph 	memcpy(data->max_phases, mp2975_max_phases[data->chip_id],
1014e2c90b48SPatrick Rudolph 	       sizeof(data->max_phases));
1015e2c90b48SPatrick Rudolph 
1016*73bc2357SAndy Shevchenko 	memcpy(&data->info, ddinfo->info, sizeof(data->info));
10175239277eSPatrick Rudolph 
10182c6fcbb2SVadim Pasternak 	info = &data->info;
10192c6fcbb2SVadim Pasternak 
10202c6fcbb2SVadim Pasternak 	/* Identify multiphase configuration for rail 2. */
1021e2c90b48SPatrick Rudolph 	ret = mp2975_identify_multiphase_rail2(client, data);
10222c6fcbb2SVadim Pasternak 	if (ret < 0)
10232c6fcbb2SVadim Pasternak 		return ret;
10242c6fcbb2SVadim Pasternak 
10252c6fcbb2SVadim Pasternak 	if (ret) {
10262c6fcbb2SVadim Pasternak 		/* Two rails are connected. */
10272c6fcbb2SVadim Pasternak 		data->info.pages = MP2975_PAGE_NUM;
10282c6fcbb2SVadim Pasternak 		data->info.phases[1] = ret;
10292c6fcbb2SVadim Pasternak 		data->info.func[1] = MP2975_RAIL2_FUNC;
103088fc1efcSPatrick Rudolph 		if (IS_ENABLED(CONFIG_SENSORS_MP2975_REGULATOR))
103188fc1efcSPatrick Rudolph 			data->info.num_regulators = MP2975_PAGE_NUM;
10322c6fcbb2SVadim Pasternak 	}
10332c6fcbb2SVadim Pasternak 
10342c6fcbb2SVadim Pasternak 	/* Identify multiphase configuration. */
10352c6fcbb2SVadim Pasternak 	ret = mp2975_identify_multiphase(client, data, info);
10362c6fcbb2SVadim Pasternak 	if (ret)
10372c6fcbb2SVadim Pasternak 		return ret;
10382c6fcbb2SVadim Pasternak 
103945f154dcSPatrick Rudolph 	if (data->chip_id == mp2975) {
10402c6fcbb2SVadim Pasternak 		/* Identify VID setting per rail. */
10412c6fcbb2SVadim Pasternak 		ret = mp2975_identify_rails_vid(client, data, info);
10422c6fcbb2SVadim Pasternak 		if (ret < 0)
10432c6fcbb2SVadim Pasternak 			return ret;
10442c6fcbb2SVadim Pasternak 
10452c6fcbb2SVadim Pasternak 		/* Obtain current sense gain of power stage. */
10462c6fcbb2SVadim Pasternak 		ret = mp2975_current_sense_gain_get(client, data);
10472c6fcbb2SVadim Pasternak 		if (ret)
10482c6fcbb2SVadim Pasternak 			return ret;
10492c6fcbb2SVadim Pasternak 
10502c6fcbb2SVadim Pasternak 		/* Obtain voltage reference values. */
10512c6fcbb2SVadim Pasternak 		ret = mp2975_vref_get(client, data, info);
10522c6fcbb2SVadim Pasternak 		if (ret)
10532c6fcbb2SVadim Pasternak 			return ret;
10542c6fcbb2SVadim Pasternak 
10552c6fcbb2SVadim Pasternak 		/* Obtain vout over-voltage scales. */
10562c6fcbb2SVadim Pasternak 		ret = mp2975_vout_ov_scale_get(client, data, info);
10572c6fcbb2SVadim Pasternak 		if (ret < 0)
10582c6fcbb2SVadim Pasternak 			return ret;
10595239277eSPatrick Rudolph 	} else {
10605239277eSPatrick Rudolph 		/* Identify VID setting per rail. */
10615239277eSPatrick Rudolph 		ret = mp2973_identify_rails_vid(client, data, info);
10625239277eSPatrick Rudolph 		if (ret < 0)
10635239277eSPatrick Rudolph 			return ret;
10645239277eSPatrick Rudolph 	}
10652c6fcbb2SVadim Pasternak 
10662c6fcbb2SVadim Pasternak 	/* Obtain offsets, maximum and format for vout. */
10672c6fcbb2SVadim Pasternak 	ret = mp2975_vout_per_rail_config_get(client, data, info);
10682c6fcbb2SVadim Pasternak 	if (ret)
10692c6fcbb2SVadim Pasternak 		return ret;
10702c6fcbb2SVadim Pasternak 
10712c6fcbb2SVadim Pasternak 	return pmbus_do_probe(client, info);
10722c6fcbb2SVadim Pasternak }
10732c6fcbb2SVadim Pasternak 
1074*73bc2357SAndy Shevchenko static const struct of_device_id mp2975_of_match[] = {
1075*73bc2357SAndy Shevchenko 	{.compatible = "mps,mp2971", .data = &mp2975_ddinfo[mp2971]},
1076*73bc2357SAndy Shevchenko 	{.compatible = "mps,mp2973", .data = &mp2975_ddinfo[mp2973]},
1077*73bc2357SAndy Shevchenko 	{.compatible = "mps,mp2975", .data = &mp2975_ddinfo[mp2975]},
10782c6fcbb2SVadim Pasternak 	{}
10792c6fcbb2SVadim Pasternak };
10802c6fcbb2SVadim Pasternak MODULE_DEVICE_TABLE(of, mp2975_of_match);
10812c6fcbb2SVadim Pasternak 
1082*73bc2357SAndy Shevchenko static const struct i2c_device_id mp2975_id[] = {
1083*73bc2357SAndy Shevchenko 	{"mp2971", (kernel_ulong_t)&mp2975_ddinfo[mp2971]},
1084*73bc2357SAndy Shevchenko 	{"mp2973", (kernel_ulong_t)&mp2975_ddinfo[mp2973]},
1085*73bc2357SAndy Shevchenko 	{"mp2975", (kernel_ulong_t)&mp2975_ddinfo[mp2975]},
1086*73bc2357SAndy Shevchenko 	{}
1087*73bc2357SAndy Shevchenko };
1088*73bc2357SAndy Shevchenko MODULE_DEVICE_TABLE(i2c, mp2975_id);
1089*73bc2357SAndy Shevchenko 
10902c6fcbb2SVadim Pasternak static struct i2c_driver mp2975_driver = {
10912c6fcbb2SVadim Pasternak 	.driver = {
10922c6fcbb2SVadim Pasternak 		.name = "mp2975",
1093*73bc2357SAndy Shevchenko 		.of_match_table = mp2975_of_match,
10942c6fcbb2SVadim Pasternak 	},
10951975d167SUwe Kleine-König 	.probe = mp2975_probe,
10962c6fcbb2SVadim Pasternak 	.id_table = mp2975_id,
10972c6fcbb2SVadim Pasternak };
10982c6fcbb2SVadim Pasternak 
10992c6fcbb2SVadim Pasternak module_i2c_driver(mp2975_driver);
11002c6fcbb2SVadim Pasternak 
11012c6fcbb2SVadim Pasternak MODULE_AUTHOR("Vadim Pasternak <vadimp@nvidia.com>");
11022c6fcbb2SVadim Pasternak MODULE_DESCRIPTION("PMBus driver for MPS MP2975 device");
11032c6fcbb2SVadim Pasternak MODULE_LICENSE("GPL");
1104b94ca77eSGuenter Roeck MODULE_IMPORT_NS(PMBUS);
1105