xref: /linux/drivers/hwmon/pmbus/mp2975.c (revision 2c6fcbb211494f1ff6ef384776944b9e04f4c14c)
1*2c6fcbb2SVadim Pasternak // SPDX-License-Identifier: GPL-2.0-or-later
2*2c6fcbb2SVadim Pasternak /*
3*2c6fcbb2SVadim Pasternak  * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers
4*2c6fcbb2SVadim Pasternak  *
5*2c6fcbb2SVadim Pasternak  * Copyright (C) 2020 Nvidia Technologies Ltd.
6*2c6fcbb2SVadim Pasternak  */
7*2c6fcbb2SVadim Pasternak 
8*2c6fcbb2SVadim Pasternak #include <linux/err.h>
9*2c6fcbb2SVadim Pasternak #include <linux/i2c.h>
10*2c6fcbb2SVadim Pasternak #include <linux/init.h>
11*2c6fcbb2SVadim Pasternak #include <linux/kernel.h>
12*2c6fcbb2SVadim Pasternak #include <linux/module.h>
13*2c6fcbb2SVadim Pasternak #include "pmbus.h"
14*2c6fcbb2SVadim Pasternak 
15*2c6fcbb2SVadim Pasternak /* Vendor specific registers. */
16*2c6fcbb2SVadim Pasternak #define MP2975_MFR_APS_HYS_R2		0x0d
17*2c6fcbb2SVadim Pasternak #define MP2975_MFR_SLOPE_TRIM3		0x1d
18*2c6fcbb2SVadim Pasternak #define MP2975_MFR_VR_MULTI_CONFIG_R1	0x0d
19*2c6fcbb2SVadim Pasternak #define MP2975_MFR_VR_MULTI_CONFIG_R2	0x1d
20*2c6fcbb2SVadim Pasternak #define MP2975_MFR_APS_DECAY_ADV	0x56
21*2c6fcbb2SVadim Pasternak #define MP2975_MFR_DC_LOOP_CTRL		0x59
22*2c6fcbb2SVadim Pasternak #define MP2975_MFR_OCP_UCP_PHASE_SET	0x65
23*2c6fcbb2SVadim Pasternak #define MP2975_MFR_VR_CONFIG1		0x68
24*2c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS1_2		0x82
25*2c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS3_4		0x83
26*2c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS5_6		0x84
27*2c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS7_8		0x85
28*2c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS9_10		0x86
29*2c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS11_12		0x87
30*2c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_IOUT_PK		0x90
31*2c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_POUT_PK		0x91
32*2c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_VREF_R1		0xa1
33*2c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_VREF_R2		0xa3
34*2c6fcbb2SVadim Pasternak #define MP2975_MFR_OVP_TH_SET		0xe5
35*2c6fcbb2SVadim Pasternak #define MP2975_MFR_UVP_SET		0xe6
36*2c6fcbb2SVadim Pasternak 
37*2c6fcbb2SVadim Pasternak #define MP2975_VOUT_FORMAT		BIT(15)
38*2c6fcbb2SVadim Pasternak #define MP2975_VID_STEP_SEL_R1		BIT(4)
39*2c6fcbb2SVadim Pasternak #define MP2975_IMVP9_EN_R1		BIT(13)
40*2c6fcbb2SVadim Pasternak #define MP2975_VID_STEP_SEL_R2		BIT(3)
41*2c6fcbb2SVadim Pasternak #define MP2975_IMVP9_EN_R2		BIT(12)
42*2c6fcbb2SVadim Pasternak #define MP2975_PRT_THRES_DIV_OV_EN	BIT(14)
43*2c6fcbb2SVadim Pasternak #define MP2975_DRMOS_KCS		GENMASK(13, 12)
44*2c6fcbb2SVadim Pasternak #define MP2975_PROT_DEV_OV_OFF		10
45*2c6fcbb2SVadim Pasternak #define MP2975_PROT_DEV_OV_ON		5
46*2c6fcbb2SVadim Pasternak #define MP2975_SENSE_AMPL		BIT(11)
47*2c6fcbb2SVadim Pasternak #define MP2975_SENSE_AMPL_UNIT		1
48*2c6fcbb2SVadim Pasternak #define MP2975_SENSE_AMPL_HALF		2
49*2c6fcbb2SVadim Pasternak #define MP2975_VIN_UV_LIMIT_UNIT	8
50*2c6fcbb2SVadim Pasternak 
51*2c6fcbb2SVadim Pasternak #define MP2975_MAX_PHASE_RAIL1	8
52*2c6fcbb2SVadim Pasternak #define MP2975_MAX_PHASE_RAIL2	4
53*2c6fcbb2SVadim Pasternak #define MP2975_PAGE_NUM		2
54*2c6fcbb2SVadim Pasternak 
55*2c6fcbb2SVadim Pasternak #define MP2975_RAIL2_FUNC	(PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | \
56*2c6fcbb2SVadim Pasternak 				 PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | \
57*2c6fcbb2SVadim Pasternak 				 PMBUS_PHASE_VIRTUAL)
58*2c6fcbb2SVadim Pasternak 
59*2c6fcbb2SVadim Pasternak struct mp2975_data {
60*2c6fcbb2SVadim Pasternak 	struct pmbus_driver_info info;
61*2c6fcbb2SVadim Pasternak 	int vout_scale;
62*2c6fcbb2SVadim Pasternak 	int vid_step[MP2975_PAGE_NUM];
63*2c6fcbb2SVadim Pasternak 	int vref[MP2975_PAGE_NUM];
64*2c6fcbb2SVadim Pasternak 	int vref_off[MP2975_PAGE_NUM];
65*2c6fcbb2SVadim Pasternak 	int vout_max[MP2975_PAGE_NUM];
66*2c6fcbb2SVadim Pasternak 	int vout_ov_fixed[MP2975_PAGE_NUM];
67*2c6fcbb2SVadim Pasternak 	int vout_format[MP2975_PAGE_NUM];
68*2c6fcbb2SVadim Pasternak 	int curr_sense_gain[MP2975_PAGE_NUM];
69*2c6fcbb2SVadim Pasternak };
70*2c6fcbb2SVadim Pasternak 
71*2c6fcbb2SVadim Pasternak #define to_mp2975_data(x)  container_of(x, struct mp2975_data, info)
72*2c6fcbb2SVadim Pasternak 
73*2c6fcbb2SVadim Pasternak static int mp2975_read_byte_data(struct i2c_client *client, int page, int reg)
74*2c6fcbb2SVadim Pasternak {
75*2c6fcbb2SVadim Pasternak 	switch (reg) {
76*2c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_MODE:
77*2c6fcbb2SVadim Pasternak 		/*
78*2c6fcbb2SVadim Pasternak 		 * Enforce VOUT direct format, since device allows to set the
79*2c6fcbb2SVadim Pasternak 		 * different formats for the different rails. Conversion from
80*2c6fcbb2SVadim Pasternak 		 * VID to direct provided by driver internally, in case it is
81*2c6fcbb2SVadim Pasternak 		 * necessary.
82*2c6fcbb2SVadim Pasternak 		 */
83*2c6fcbb2SVadim Pasternak 		return PB_VOUT_MODE_DIRECT;
84*2c6fcbb2SVadim Pasternak 	default:
85*2c6fcbb2SVadim Pasternak 		return -ENODATA;
86*2c6fcbb2SVadim Pasternak 	}
87*2c6fcbb2SVadim Pasternak }
88*2c6fcbb2SVadim Pasternak 
89*2c6fcbb2SVadim Pasternak static int
90*2c6fcbb2SVadim Pasternak mp2975_read_word_helper(struct i2c_client *client, int page, int phase, u8 reg,
91*2c6fcbb2SVadim Pasternak 			u16 mask)
92*2c6fcbb2SVadim Pasternak {
93*2c6fcbb2SVadim Pasternak 	int ret = pmbus_read_word_data(client, page, phase, reg);
94*2c6fcbb2SVadim Pasternak 
95*2c6fcbb2SVadim Pasternak 	return (ret > 0) ? ret & mask : ret;
96*2c6fcbb2SVadim Pasternak }
97*2c6fcbb2SVadim Pasternak 
98*2c6fcbb2SVadim Pasternak static int
99*2c6fcbb2SVadim Pasternak mp2975_vid2direct(int vrf, int val)
100*2c6fcbb2SVadim Pasternak {
101*2c6fcbb2SVadim Pasternak 	switch (vrf) {
102*2c6fcbb2SVadim Pasternak 	case vr12:
103*2c6fcbb2SVadim Pasternak 		if (val >= 0x01)
104*2c6fcbb2SVadim Pasternak 			return 250 + (val - 1) * 5;
105*2c6fcbb2SVadim Pasternak 		break;
106*2c6fcbb2SVadim Pasternak 	case vr13:
107*2c6fcbb2SVadim Pasternak 		if (val >= 0x01)
108*2c6fcbb2SVadim Pasternak 			return 500 + (val - 1) * 10;
109*2c6fcbb2SVadim Pasternak 		break;
110*2c6fcbb2SVadim Pasternak 	case imvp9:
111*2c6fcbb2SVadim Pasternak 		if (val >= 0x01)
112*2c6fcbb2SVadim Pasternak 			return 200 + (val - 1) * 10;
113*2c6fcbb2SVadim Pasternak 		break;
114*2c6fcbb2SVadim Pasternak 	default:
115*2c6fcbb2SVadim Pasternak 		return -EINVAL;
116*2c6fcbb2SVadim Pasternak 	}
117*2c6fcbb2SVadim Pasternak 	return 0;
118*2c6fcbb2SVadim Pasternak }
119*2c6fcbb2SVadim Pasternak 
120*2c6fcbb2SVadim Pasternak static int
121*2c6fcbb2SVadim Pasternak mp2975_read_phase(struct i2c_client *client, struct mp2975_data *data,
122*2c6fcbb2SVadim Pasternak 		  int page, int phase, u8 reg)
123*2c6fcbb2SVadim Pasternak {
124*2c6fcbb2SVadim Pasternak 	int ph_curr, ret;
125*2c6fcbb2SVadim Pasternak 
126*2c6fcbb2SVadim Pasternak 	ret = pmbus_read_word_data(client, page, phase, reg);
127*2c6fcbb2SVadim Pasternak 	if (ret < 0)
128*2c6fcbb2SVadim Pasternak 		return ret;
129*2c6fcbb2SVadim Pasternak 
130*2c6fcbb2SVadim Pasternak 	if (!((phase + 1) % MP2975_PAGE_NUM))
131*2c6fcbb2SVadim Pasternak 		ret >>= 8;
132*2c6fcbb2SVadim Pasternak 	ret &= 0xff;
133*2c6fcbb2SVadim Pasternak 
134*2c6fcbb2SVadim Pasternak 	/*
135*2c6fcbb2SVadim Pasternak 	 * Output value is calculated as: (READ_CSx / 80 – 1.23) / (Kcs * Rcs)
136*2c6fcbb2SVadim Pasternak 	 * where:
137*2c6fcbb2SVadim Pasternak 	 * - Kcs is the DrMOS current sense gain of power stage, which is
138*2c6fcbb2SVadim Pasternak 	 *   obtained from the register MP2975_MFR_VR_CONFIG1, bits 13-12 with
139*2c6fcbb2SVadim Pasternak 	 *   the following selection of DrMOS (data->curr_sense_gain[page]):
140*2c6fcbb2SVadim Pasternak 	 *   00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A.
141*2c6fcbb2SVadim Pasternak 	 * - Rcs is the internal phase current sense resistor which is constant
142*2c6fcbb2SVadim Pasternak 	 *   value 1kΩ.
143*2c6fcbb2SVadim Pasternak 	 */
144*2c6fcbb2SVadim Pasternak 	ph_curr = ret * 100 - 9800;
145*2c6fcbb2SVadim Pasternak 
146*2c6fcbb2SVadim Pasternak 	/*
147*2c6fcbb2SVadim Pasternak 	 * Current phase sensing, providing by the device is not accurate
148*2c6fcbb2SVadim Pasternak 	 * for the light load. This because sampling of current occurrence of
149*2c6fcbb2SVadim Pasternak 	 * bit weight has a big deviation for light load. For handling such
150*2c6fcbb2SVadim Pasternak 	 * case phase current is represented as the maximum between the value
151*2c6fcbb2SVadim Pasternak 	 * calculated  above and total rail current divided by number phases.
152*2c6fcbb2SVadim Pasternak 	 */
153*2c6fcbb2SVadim Pasternak 	ret = pmbus_read_word_data(client, page, phase, PMBUS_READ_IOUT);
154*2c6fcbb2SVadim Pasternak 	if (ret < 0)
155*2c6fcbb2SVadim Pasternak 		return ret;
156*2c6fcbb2SVadim Pasternak 
157*2c6fcbb2SVadim Pasternak 	return max_t(int, DIV_ROUND_CLOSEST(ret, data->info.phases[page]),
158*2c6fcbb2SVadim Pasternak 		     DIV_ROUND_CLOSEST(ph_curr, data->curr_sense_gain[page]));
159*2c6fcbb2SVadim Pasternak }
160*2c6fcbb2SVadim Pasternak 
161*2c6fcbb2SVadim Pasternak static int
162*2c6fcbb2SVadim Pasternak mp2975_read_phases(struct i2c_client *client, struct mp2975_data *data,
163*2c6fcbb2SVadim Pasternak 		   int page, int phase)
164*2c6fcbb2SVadim Pasternak {
165*2c6fcbb2SVadim Pasternak 	int ret;
166*2c6fcbb2SVadim Pasternak 
167*2c6fcbb2SVadim Pasternak 	if (page) {
168*2c6fcbb2SVadim Pasternak 		switch (phase) {
169*2c6fcbb2SVadim Pasternak 		case 0 ... 1:
170*2c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
171*2c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS7_8);
172*2c6fcbb2SVadim Pasternak 			break;
173*2c6fcbb2SVadim Pasternak 		case 2 ... 3:
174*2c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
175*2c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS9_10);
176*2c6fcbb2SVadim Pasternak 			break;
177*2c6fcbb2SVadim Pasternak 		case 4 ... 5:
178*2c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
179*2c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS11_12);
180*2c6fcbb2SVadim Pasternak 			break;
181*2c6fcbb2SVadim Pasternak 		default:
182*2c6fcbb2SVadim Pasternak 			return -ENODATA;
183*2c6fcbb2SVadim Pasternak 		}
184*2c6fcbb2SVadim Pasternak 	} else {
185*2c6fcbb2SVadim Pasternak 		switch (phase) {
186*2c6fcbb2SVadim Pasternak 		case 0 ... 1:
187*2c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
188*2c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS1_2);
189*2c6fcbb2SVadim Pasternak 			break;
190*2c6fcbb2SVadim Pasternak 		case 2 ... 3:
191*2c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
192*2c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS3_4);
193*2c6fcbb2SVadim Pasternak 			break;
194*2c6fcbb2SVadim Pasternak 		case 4 ... 5:
195*2c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
196*2c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS5_6);
197*2c6fcbb2SVadim Pasternak 			break;
198*2c6fcbb2SVadim Pasternak 		case 6 ... 7:
199*2c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
200*2c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS7_8);
201*2c6fcbb2SVadim Pasternak 			break;
202*2c6fcbb2SVadim Pasternak 		case 8 ... 9:
203*2c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
204*2c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS9_10);
205*2c6fcbb2SVadim Pasternak 			break;
206*2c6fcbb2SVadim Pasternak 		case 10 ... 11:
207*2c6fcbb2SVadim Pasternak 			ret = mp2975_read_phase(client, data, page, phase,
208*2c6fcbb2SVadim Pasternak 						MP2975_MFR_READ_CS11_12);
209*2c6fcbb2SVadim Pasternak 			break;
210*2c6fcbb2SVadim Pasternak 		default:
211*2c6fcbb2SVadim Pasternak 			return -ENODATA;
212*2c6fcbb2SVadim Pasternak 		}
213*2c6fcbb2SVadim Pasternak 	}
214*2c6fcbb2SVadim Pasternak 	return ret;
215*2c6fcbb2SVadim Pasternak }
216*2c6fcbb2SVadim Pasternak 
217*2c6fcbb2SVadim Pasternak static int mp2975_read_word_data(struct i2c_client *client, int page,
218*2c6fcbb2SVadim Pasternak 				 int phase, int reg)
219*2c6fcbb2SVadim Pasternak {
220*2c6fcbb2SVadim Pasternak 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
221*2c6fcbb2SVadim Pasternak 	struct mp2975_data *data = to_mp2975_data(info);
222*2c6fcbb2SVadim Pasternak 	int ret;
223*2c6fcbb2SVadim Pasternak 
224*2c6fcbb2SVadim Pasternak 	switch (reg) {
225*2c6fcbb2SVadim Pasternak 	case PMBUS_OT_FAULT_LIMIT:
226*2c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase, reg,
227*2c6fcbb2SVadim Pasternak 					      GENMASK(7, 0));
228*2c6fcbb2SVadim Pasternak 		break;
229*2c6fcbb2SVadim Pasternak 	case PMBUS_VIN_OV_FAULT_LIMIT:
230*2c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase, reg,
231*2c6fcbb2SVadim Pasternak 					      GENMASK(7, 0));
232*2c6fcbb2SVadim Pasternak 		if (ret < 0)
233*2c6fcbb2SVadim Pasternak 			return ret;
234*2c6fcbb2SVadim Pasternak 
235*2c6fcbb2SVadim Pasternak 		ret = DIV_ROUND_CLOSEST(ret, MP2975_VIN_UV_LIMIT_UNIT);
236*2c6fcbb2SVadim Pasternak 		break;
237*2c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_OV_FAULT_LIMIT:
238*2c6fcbb2SVadim Pasternak 		/*
239*2c6fcbb2SVadim Pasternak 		 * Register provides two values for over-voltage protection
240*2c6fcbb2SVadim Pasternak 		 * threshold for fixed (ovp2) and tracking (ovp1) modes. The
241*2c6fcbb2SVadim Pasternak 		 * minimum of these two values is provided as over-voltage
242*2c6fcbb2SVadim Pasternak 		 * fault alarm.
243*2c6fcbb2SVadim Pasternak 		 */
244*2c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase,
245*2c6fcbb2SVadim Pasternak 					      MP2975_MFR_OVP_TH_SET,
246*2c6fcbb2SVadim Pasternak 					      GENMASK(2, 0));
247*2c6fcbb2SVadim Pasternak 		if (ret < 0)
248*2c6fcbb2SVadim Pasternak 			return ret;
249*2c6fcbb2SVadim Pasternak 
250*2c6fcbb2SVadim Pasternak 		ret = min_t(int, data->vout_max[page] + 50 * (ret + 1),
251*2c6fcbb2SVadim Pasternak 			    data->vout_ov_fixed[page]);
252*2c6fcbb2SVadim Pasternak 		break;
253*2c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_UV_FAULT_LIMIT:
254*2c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase,
255*2c6fcbb2SVadim Pasternak 					      MP2975_MFR_UVP_SET,
256*2c6fcbb2SVadim Pasternak 					      GENMASK(2, 0));
257*2c6fcbb2SVadim Pasternak 		if (ret < 0)
258*2c6fcbb2SVadim Pasternak 			return ret;
259*2c6fcbb2SVadim Pasternak 
260*2c6fcbb2SVadim Pasternak 		ret = DIV_ROUND_CLOSEST(data->vref[page] * 10 - 50 *
261*2c6fcbb2SVadim Pasternak 					(ret + 1) * data->vout_scale, 10);
262*2c6fcbb2SVadim Pasternak 		break;
263*2c6fcbb2SVadim Pasternak 	case PMBUS_READ_VOUT:
264*2c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase, reg,
265*2c6fcbb2SVadim Pasternak 					      GENMASK(11, 0));
266*2c6fcbb2SVadim Pasternak 		if (ret < 0)
267*2c6fcbb2SVadim Pasternak 			return ret;
268*2c6fcbb2SVadim Pasternak 
269*2c6fcbb2SVadim Pasternak 		/*
270*2c6fcbb2SVadim Pasternak 		 * READ_VOUT can be provided in VID or direct format. The
271*2c6fcbb2SVadim Pasternak 		 * format type is specified by bit 15 of the register
272*2c6fcbb2SVadim Pasternak 		 * MP2975_MFR_DC_LOOP_CTRL. The driver enforces VOUT direct
273*2c6fcbb2SVadim Pasternak 		 * format, since device allows to set the different formats for
274*2c6fcbb2SVadim Pasternak 		 * the different rails and also all VOUT limits registers are
275*2c6fcbb2SVadim Pasternak 		 * provided in a direct format. In case format is VID - convert
276*2c6fcbb2SVadim Pasternak 		 * to direct.
277*2c6fcbb2SVadim Pasternak 		 */
278*2c6fcbb2SVadim Pasternak 		if (data->vout_format[page] == vid)
279*2c6fcbb2SVadim Pasternak 			ret = mp2975_vid2direct(info->vrm_version[page], ret);
280*2c6fcbb2SVadim Pasternak 		break;
281*2c6fcbb2SVadim Pasternak 	case PMBUS_VIRT_READ_POUT_MAX:
282*2c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase,
283*2c6fcbb2SVadim Pasternak 					      MP2975_MFR_READ_POUT_PK,
284*2c6fcbb2SVadim Pasternak 					      GENMASK(12, 0));
285*2c6fcbb2SVadim Pasternak 		if (ret < 0)
286*2c6fcbb2SVadim Pasternak 			return ret;
287*2c6fcbb2SVadim Pasternak 
288*2c6fcbb2SVadim Pasternak 		ret = DIV_ROUND_CLOSEST(ret, 4);
289*2c6fcbb2SVadim Pasternak 		break;
290*2c6fcbb2SVadim Pasternak 	case PMBUS_VIRT_READ_IOUT_MAX:
291*2c6fcbb2SVadim Pasternak 		ret = mp2975_read_word_helper(client, page, phase,
292*2c6fcbb2SVadim Pasternak 					      MP2975_MFR_READ_IOUT_PK,
293*2c6fcbb2SVadim Pasternak 					      GENMASK(12, 0));
294*2c6fcbb2SVadim Pasternak 		if (ret < 0)
295*2c6fcbb2SVadim Pasternak 			return ret;
296*2c6fcbb2SVadim Pasternak 
297*2c6fcbb2SVadim Pasternak 		ret = DIV_ROUND_CLOSEST(ret, 4);
298*2c6fcbb2SVadim Pasternak 		break;
299*2c6fcbb2SVadim Pasternak 	case PMBUS_READ_IOUT:
300*2c6fcbb2SVadim Pasternak 		ret = mp2975_read_phases(client, data, page, phase);
301*2c6fcbb2SVadim Pasternak 		if (ret < 0)
302*2c6fcbb2SVadim Pasternak 			return ret;
303*2c6fcbb2SVadim Pasternak 
304*2c6fcbb2SVadim Pasternak 		break;
305*2c6fcbb2SVadim Pasternak 	case PMBUS_UT_WARN_LIMIT:
306*2c6fcbb2SVadim Pasternak 	case PMBUS_UT_FAULT_LIMIT:
307*2c6fcbb2SVadim Pasternak 	case PMBUS_VIN_UV_WARN_LIMIT:
308*2c6fcbb2SVadim Pasternak 	case PMBUS_VIN_UV_FAULT_LIMIT:
309*2c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_UV_WARN_LIMIT:
310*2c6fcbb2SVadim Pasternak 	case PMBUS_VOUT_OV_WARN_LIMIT:
311*2c6fcbb2SVadim Pasternak 	case PMBUS_VIN_OV_WARN_LIMIT:
312*2c6fcbb2SVadim Pasternak 	case PMBUS_IIN_OC_FAULT_LIMIT:
313*2c6fcbb2SVadim Pasternak 	case PMBUS_IOUT_OC_LV_FAULT_LIMIT:
314*2c6fcbb2SVadim Pasternak 	case PMBUS_IIN_OC_WARN_LIMIT:
315*2c6fcbb2SVadim Pasternak 	case PMBUS_IOUT_OC_WARN_LIMIT:
316*2c6fcbb2SVadim Pasternak 	case PMBUS_IOUT_OC_FAULT_LIMIT:
317*2c6fcbb2SVadim Pasternak 	case PMBUS_IOUT_UC_FAULT_LIMIT:
318*2c6fcbb2SVadim Pasternak 	case PMBUS_POUT_OP_FAULT_LIMIT:
319*2c6fcbb2SVadim Pasternak 	case PMBUS_POUT_OP_WARN_LIMIT:
320*2c6fcbb2SVadim Pasternak 	case PMBUS_PIN_OP_WARN_LIMIT:
321*2c6fcbb2SVadim Pasternak 		return -ENXIO;
322*2c6fcbb2SVadim Pasternak 	default:
323*2c6fcbb2SVadim Pasternak 		return -ENODATA;
324*2c6fcbb2SVadim Pasternak 	}
325*2c6fcbb2SVadim Pasternak 
326*2c6fcbb2SVadim Pasternak 	return ret;
327*2c6fcbb2SVadim Pasternak }
328*2c6fcbb2SVadim Pasternak 
329*2c6fcbb2SVadim Pasternak static int mp2975_identify_multiphase_rail2(struct i2c_client *client)
330*2c6fcbb2SVadim Pasternak {
331*2c6fcbb2SVadim Pasternak 	int ret;
332*2c6fcbb2SVadim Pasternak 
333*2c6fcbb2SVadim Pasternak 	/*
334*2c6fcbb2SVadim Pasternak 	 * Identify multiphase for rail 2 - could be from 0 to 4.
335*2c6fcbb2SVadim Pasternak 	 * In case phase number is zero – only page zero is supported
336*2c6fcbb2SVadim Pasternak 	 */
337*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
338*2c6fcbb2SVadim Pasternak 	if (ret < 0)
339*2c6fcbb2SVadim Pasternak 		return ret;
340*2c6fcbb2SVadim Pasternak 
341*2c6fcbb2SVadim Pasternak 	/* Identify multiphase for rail 2 - could be from 0 to 4. */
342*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_VR_MULTI_CONFIG_R2);
343*2c6fcbb2SVadim Pasternak 	if (ret < 0)
344*2c6fcbb2SVadim Pasternak 		return ret;
345*2c6fcbb2SVadim Pasternak 
346*2c6fcbb2SVadim Pasternak 	ret &= GENMASK(2, 0);
347*2c6fcbb2SVadim Pasternak 	return (ret >= 4) ? 4 : ret;
348*2c6fcbb2SVadim Pasternak }
349*2c6fcbb2SVadim Pasternak 
350*2c6fcbb2SVadim Pasternak static void mp2975_set_phase_rail1(struct pmbus_driver_info *info)
351*2c6fcbb2SVadim Pasternak {
352*2c6fcbb2SVadim Pasternak 	int i;
353*2c6fcbb2SVadim Pasternak 
354*2c6fcbb2SVadim Pasternak 	for (i = 0 ; i < info->phases[0]; i++)
355*2c6fcbb2SVadim Pasternak 		info->pfunc[i] = PMBUS_HAVE_IOUT;
356*2c6fcbb2SVadim Pasternak }
357*2c6fcbb2SVadim Pasternak 
358*2c6fcbb2SVadim Pasternak static void
359*2c6fcbb2SVadim Pasternak mp2975_set_phase_rail2(struct pmbus_driver_info *info, int num_phases)
360*2c6fcbb2SVadim Pasternak {
361*2c6fcbb2SVadim Pasternak 	int i;
362*2c6fcbb2SVadim Pasternak 
363*2c6fcbb2SVadim Pasternak 	/* Set phases for rail 2 from upper to lower. */
364*2c6fcbb2SVadim Pasternak 	for (i = 1; i <= num_phases; i++)
365*2c6fcbb2SVadim Pasternak 		info->pfunc[MP2975_MAX_PHASE_RAIL1 - i] = PMBUS_HAVE_IOUT;
366*2c6fcbb2SVadim Pasternak }
367*2c6fcbb2SVadim Pasternak 
368*2c6fcbb2SVadim Pasternak static int
369*2c6fcbb2SVadim Pasternak mp2975_identify_multiphase(struct i2c_client *client, struct mp2975_data *data,
370*2c6fcbb2SVadim Pasternak 			   struct pmbus_driver_info *info)
371*2c6fcbb2SVadim Pasternak {
372*2c6fcbb2SVadim Pasternak 	int num_phases2, ret;
373*2c6fcbb2SVadim Pasternak 
374*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
375*2c6fcbb2SVadim Pasternak 	if (ret < 0)
376*2c6fcbb2SVadim Pasternak 		return ret;
377*2c6fcbb2SVadim Pasternak 
378*2c6fcbb2SVadim Pasternak 	/* Identify multiphase for rail 1 - could be from 1 to 8. */
379*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_VR_MULTI_CONFIG_R1);
380*2c6fcbb2SVadim Pasternak 	if (ret <= 0)
381*2c6fcbb2SVadim Pasternak 		return ret;
382*2c6fcbb2SVadim Pasternak 
383*2c6fcbb2SVadim Pasternak 	info->phases[0] = ret & GENMASK(3, 0);
384*2c6fcbb2SVadim Pasternak 
385*2c6fcbb2SVadim Pasternak 	/*
386*2c6fcbb2SVadim Pasternak 	 * The device provides a total of 8 PWM pins, and can be configured
387*2c6fcbb2SVadim Pasternak 	 * to different phase count applications for rail 1 and rail 2.
388*2c6fcbb2SVadim Pasternak 	 * Rail 1 can be set to 8 phases, while rail 2 can only be set to 4
389*2c6fcbb2SVadim Pasternak 	 * phases at most. When rail 1’s phase count is configured as 0, rail
390*2c6fcbb2SVadim Pasternak 	 * 1 operates with 1-phase DCM. When rail 2 phase count is configured
391*2c6fcbb2SVadim Pasternak 	 * as 0, rail 2 is disabled.
392*2c6fcbb2SVadim Pasternak 	 */
393*2c6fcbb2SVadim Pasternak 	if (info->phases[0] > MP2975_MAX_PHASE_RAIL1)
394*2c6fcbb2SVadim Pasternak 		return -EINVAL;
395*2c6fcbb2SVadim Pasternak 
396*2c6fcbb2SVadim Pasternak 	mp2975_set_phase_rail1(info);
397*2c6fcbb2SVadim Pasternak 	num_phases2 = min(MP2975_MAX_PHASE_RAIL1 - info->phases[0],
398*2c6fcbb2SVadim Pasternak 			  MP2975_MAX_PHASE_RAIL2);
399*2c6fcbb2SVadim Pasternak 	if (info->phases[1] && info->phases[1] <= num_phases2)
400*2c6fcbb2SVadim Pasternak 		mp2975_set_phase_rail2(info, num_phases2);
401*2c6fcbb2SVadim Pasternak 
402*2c6fcbb2SVadim Pasternak 	return 0;
403*2c6fcbb2SVadim Pasternak }
404*2c6fcbb2SVadim Pasternak 
405*2c6fcbb2SVadim Pasternak static int
406*2c6fcbb2SVadim Pasternak mp2975_identify_vid(struct i2c_client *client, struct mp2975_data *data,
407*2c6fcbb2SVadim Pasternak 		    struct pmbus_driver_info *info, u32 reg, int page,
408*2c6fcbb2SVadim Pasternak 		    u32 imvp_bit, u32 vr_bit)
409*2c6fcbb2SVadim Pasternak {
410*2c6fcbb2SVadim Pasternak 	int ret;
411*2c6fcbb2SVadim Pasternak 
412*2c6fcbb2SVadim Pasternak 	/* Identify VID mode and step selection. */
413*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, reg);
414*2c6fcbb2SVadim Pasternak 	if (ret < 0)
415*2c6fcbb2SVadim Pasternak 		return ret;
416*2c6fcbb2SVadim Pasternak 
417*2c6fcbb2SVadim Pasternak 	if (ret & imvp_bit) {
418*2c6fcbb2SVadim Pasternak 		info->vrm_version[page] = imvp9;
419*2c6fcbb2SVadim Pasternak 		data->vid_step[page] = MP2975_PROT_DEV_OV_OFF;
420*2c6fcbb2SVadim Pasternak 	} else if (ret & vr_bit) {
421*2c6fcbb2SVadim Pasternak 		info->vrm_version[page] = vr12;
422*2c6fcbb2SVadim Pasternak 		data->vid_step[page] = MP2975_PROT_DEV_OV_ON;
423*2c6fcbb2SVadim Pasternak 	} else {
424*2c6fcbb2SVadim Pasternak 		info->vrm_version[page] = vr13;
425*2c6fcbb2SVadim Pasternak 		data->vid_step[page] = MP2975_PROT_DEV_OV_OFF;
426*2c6fcbb2SVadim Pasternak 	}
427*2c6fcbb2SVadim Pasternak 
428*2c6fcbb2SVadim Pasternak 	return 0;
429*2c6fcbb2SVadim Pasternak }
430*2c6fcbb2SVadim Pasternak 
431*2c6fcbb2SVadim Pasternak static int
432*2c6fcbb2SVadim Pasternak mp2975_identify_rails_vid(struct i2c_client *client, struct mp2975_data *data,
433*2c6fcbb2SVadim Pasternak 			  struct pmbus_driver_info *info)
434*2c6fcbb2SVadim Pasternak {
435*2c6fcbb2SVadim Pasternak 	int ret;
436*2c6fcbb2SVadim Pasternak 
437*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
438*2c6fcbb2SVadim Pasternak 	if (ret < 0)
439*2c6fcbb2SVadim Pasternak 		return ret;
440*2c6fcbb2SVadim Pasternak 
441*2c6fcbb2SVadim Pasternak 	/* Identify VID mode for rail 1. */
442*2c6fcbb2SVadim Pasternak 	ret = mp2975_identify_vid(client, data, info,
443*2c6fcbb2SVadim Pasternak 				  MP2975_MFR_VR_MULTI_CONFIG_R1, 0,
444*2c6fcbb2SVadim Pasternak 				  MP2975_IMVP9_EN_R1, MP2975_VID_STEP_SEL_R1);
445*2c6fcbb2SVadim Pasternak 	if (ret < 0)
446*2c6fcbb2SVadim Pasternak 		return ret;
447*2c6fcbb2SVadim Pasternak 
448*2c6fcbb2SVadim Pasternak 	/* Identify VID mode for rail 2, if connected. */
449*2c6fcbb2SVadim Pasternak 	if (info->phases[1])
450*2c6fcbb2SVadim Pasternak 		ret = mp2975_identify_vid(client, data, info,
451*2c6fcbb2SVadim Pasternak 					  MP2975_MFR_VR_MULTI_CONFIG_R2, 1,
452*2c6fcbb2SVadim Pasternak 					  MP2975_IMVP9_EN_R2,
453*2c6fcbb2SVadim Pasternak 					  MP2975_VID_STEP_SEL_R2);
454*2c6fcbb2SVadim Pasternak 	return ret;
455*2c6fcbb2SVadim Pasternak }
456*2c6fcbb2SVadim Pasternak 
457*2c6fcbb2SVadim Pasternak static int
458*2c6fcbb2SVadim Pasternak mp2975_current_sense_gain_get(struct i2c_client *client,
459*2c6fcbb2SVadim Pasternak 			      struct mp2975_data *data)
460*2c6fcbb2SVadim Pasternak {
461*2c6fcbb2SVadim Pasternak 	int i, ret;
462*2c6fcbb2SVadim Pasternak 
463*2c6fcbb2SVadim Pasternak 	/*
464*2c6fcbb2SVadim Pasternak 	 * Obtain DrMOS current sense gain of power stage from the register
465*2c6fcbb2SVadim Pasternak 	 * MP2975_MFR_VR_CONFIG1, bits 13-12. The value is selected as below:
466*2c6fcbb2SVadim Pasternak 	 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. Other
467*2c6fcbb2SVadim Pasternak 	 * values are invalid.
468*2c6fcbb2SVadim Pasternak 	 */
469*2c6fcbb2SVadim Pasternak 	for (i = 0 ; i < data->info.pages; i++) {
470*2c6fcbb2SVadim Pasternak 		ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
471*2c6fcbb2SVadim Pasternak 		if (ret < 0)
472*2c6fcbb2SVadim Pasternak 			return ret;
473*2c6fcbb2SVadim Pasternak 		ret = i2c_smbus_read_word_data(client,
474*2c6fcbb2SVadim Pasternak 					       MP2975_MFR_VR_CONFIG1);
475*2c6fcbb2SVadim Pasternak 		if (ret < 0)
476*2c6fcbb2SVadim Pasternak 			return ret;
477*2c6fcbb2SVadim Pasternak 
478*2c6fcbb2SVadim Pasternak 		switch ((ret & MP2975_DRMOS_KCS) >> 12) {
479*2c6fcbb2SVadim Pasternak 		case 0:
480*2c6fcbb2SVadim Pasternak 			data->curr_sense_gain[i] = 50;
481*2c6fcbb2SVadim Pasternak 			break;
482*2c6fcbb2SVadim Pasternak 		case 1:
483*2c6fcbb2SVadim Pasternak 			data->curr_sense_gain[i] = 85;
484*2c6fcbb2SVadim Pasternak 			break;
485*2c6fcbb2SVadim Pasternak 		case 2:
486*2c6fcbb2SVadim Pasternak 			data->curr_sense_gain[i] = 97;
487*2c6fcbb2SVadim Pasternak 			break;
488*2c6fcbb2SVadim Pasternak 		default:
489*2c6fcbb2SVadim Pasternak 			data->curr_sense_gain[i] = 100;
490*2c6fcbb2SVadim Pasternak 			break;
491*2c6fcbb2SVadim Pasternak 		}
492*2c6fcbb2SVadim Pasternak 	}
493*2c6fcbb2SVadim Pasternak 
494*2c6fcbb2SVadim Pasternak 	return 0;
495*2c6fcbb2SVadim Pasternak }
496*2c6fcbb2SVadim Pasternak 
497*2c6fcbb2SVadim Pasternak static int
498*2c6fcbb2SVadim Pasternak mp2975_vref_get(struct i2c_client *client, struct mp2975_data *data,
499*2c6fcbb2SVadim Pasternak 		struct pmbus_driver_info *info)
500*2c6fcbb2SVadim Pasternak {
501*2c6fcbb2SVadim Pasternak 	int ret;
502*2c6fcbb2SVadim Pasternak 
503*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 3);
504*2c6fcbb2SVadim Pasternak 	if (ret < 0)
505*2c6fcbb2SVadim Pasternak 		return ret;
506*2c6fcbb2SVadim Pasternak 
507*2c6fcbb2SVadim Pasternak 	/* Get voltage reference value for rail 1. */
508*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_READ_VREF_R1);
509*2c6fcbb2SVadim Pasternak 	if (ret < 0)
510*2c6fcbb2SVadim Pasternak 		return ret;
511*2c6fcbb2SVadim Pasternak 
512*2c6fcbb2SVadim Pasternak 	data->vref[0] = ret * data->vid_step[0];
513*2c6fcbb2SVadim Pasternak 
514*2c6fcbb2SVadim Pasternak 	/* Get voltage reference value for rail 2, if connected. */
515*2c6fcbb2SVadim Pasternak 	if (data->info.pages == MP2975_PAGE_NUM) {
516*2c6fcbb2SVadim Pasternak 		ret = i2c_smbus_read_word_data(client, MP2975_MFR_READ_VREF_R2);
517*2c6fcbb2SVadim Pasternak 		if (ret < 0)
518*2c6fcbb2SVadim Pasternak 			return ret;
519*2c6fcbb2SVadim Pasternak 
520*2c6fcbb2SVadim Pasternak 		data->vref[1] = ret * data->vid_step[1];
521*2c6fcbb2SVadim Pasternak 	}
522*2c6fcbb2SVadim Pasternak 	return 0;
523*2c6fcbb2SVadim Pasternak }
524*2c6fcbb2SVadim Pasternak 
525*2c6fcbb2SVadim Pasternak static int
526*2c6fcbb2SVadim Pasternak mp2975_vref_offset_get(struct i2c_client *client, struct mp2975_data *data,
527*2c6fcbb2SVadim Pasternak 		       int page)
528*2c6fcbb2SVadim Pasternak {
529*2c6fcbb2SVadim Pasternak 	int ret;
530*2c6fcbb2SVadim Pasternak 
531*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_OVP_TH_SET);
532*2c6fcbb2SVadim Pasternak 	if (ret < 0)
533*2c6fcbb2SVadim Pasternak 		return ret;
534*2c6fcbb2SVadim Pasternak 
535*2c6fcbb2SVadim Pasternak 	switch ((ret & GENMASK(5, 3)) >> 3) {
536*2c6fcbb2SVadim Pasternak 	case 1:
537*2c6fcbb2SVadim Pasternak 		data->vref_off[page] = 140;
538*2c6fcbb2SVadim Pasternak 		break;
539*2c6fcbb2SVadim Pasternak 	case 2:
540*2c6fcbb2SVadim Pasternak 		data->vref_off[page] = 220;
541*2c6fcbb2SVadim Pasternak 		break;
542*2c6fcbb2SVadim Pasternak 	case 4:
543*2c6fcbb2SVadim Pasternak 		data->vref_off[page] = 400;
544*2c6fcbb2SVadim Pasternak 		break;
545*2c6fcbb2SVadim Pasternak 	default:
546*2c6fcbb2SVadim Pasternak 		return -EINVAL;
547*2c6fcbb2SVadim Pasternak 	}
548*2c6fcbb2SVadim Pasternak 	return 0;
549*2c6fcbb2SVadim Pasternak }
550*2c6fcbb2SVadim Pasternak 
551*2c6fcbb2SVadim Pasternak static int
552*2c6fcbb2SVadim Pasternak mp2975_vout_max_get(struct i2c_client *client, struct mp2975_data *data,
553*2c6fcbb2SVadim Pasternak 		    struct pmbus_driver_info *info, int page)
554*2c6fcbb2SVadim Pasternak {
555*2c6fcbb2SVadim Pasternak 	int ret;
556*2c6fcbb2SVadim Pasternak 
557*2c6fcbb2SVadim Pasternak 	/* Get maximum reference voltage of VID-DAC in VID format. */
558*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, PMBUS_VOUT_MAX);
559*2c6fcbb2SVadim Pasternak 	if (ret < 0)
560*2c6fcbb2SVadim Pasternak 		return ret;
561*2c6fcbb2SVadim Pasternak 
562*2c6fcbb2SVadim Pasternak 	data->vout_max[page] = mp2975_vid2direct(info->vrm_version[page], ret &
563*2c6fcbb2SVadim Pasternak 						 GENMASK(8, 0));
564*2c6fcbb2SVadim Pasternak 	return 0;
565*2c6fcbb2SVadim Pasternak }
566*2c6fcbb2SVadim Pasternak 
567*2c6fcbb2SVadim Pasternak static int
568*2c6fcbb2SVadim Pasternak mp2975_identify_vout_format(struct i2c_client *client,
569*2c6fcbb2SVadim Pasternak 			    struct mp2975_data *data, int page)
570*2c6fcbb2SVadim Pasternak {
571*2c6fcbb2SVadim Pasternak 	int ret;
572*2c6fcbb2SVadim Pasternak 
573*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_DC_LOOP_CTRL);
574*2c6fcbb2SVadim Pasternak 	if (ret < 0)
575*2c6fcbb2SVadim Pasternak 		return ret;
576*2c6fcbb2SVadim Pasternak 
577*2c6fcbb2SVadim Pasternak 	if (ret & MP2975_VOUT_FORMAT)
578*2c6fcbb2SVadim Pasternak 		data->vout_format[page] = vid;
579*2c6fcbb2SVadim Pasternak 	else
580*2c6fcbb2SVadim Pasternak 		data->vout_format[page] = direct;
581*2c6fcbb2SVadim Pasternak 	return 0;
582*2c6fcbb2SVadim Pasternak }
583*2c6fcbb2SVadim Pasternak 
584*2c6fcbb2SVadim Pasternak static int
585*2c6fcbb2SVadim Pasternak mp2975_vout_ov_scale_get(struct i2c_client *client, struct mp2975_data *data,
586*2c6fcbb2SVadim Pasternak 			 struct pmbus_driver_info *info)
587*2c6fcbb2SVadim Pasternak {
588*2c6fcbb2SVadim Pasternak 	int thres_dev, sense_ampl, ret;
589*2c6fcbb2SVadim Pasternak 
590*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
591*2c6fcbb2SVadim Pasternak 	if (ret < 0)
592*2c6fcbb2SVadim Pasternak 		return ret;
593*2c6fcbb2SVadim Pasternak 
594*2c6fcbb2SVadim Pasternak 	/*
595*2c6fcbb2SVadim Pasternak 	 * Get divider for over- and under-voltage protection thresholds
596*2c6fcbb2SVadim Pasternak 	 * configuration from the Advanced Options of Auto Phase Shedding and
597*2c6fcbb2SVadim Pasternak 	 * decay register.
598*2c6fcbb2SVadim Pasternak 	 */
599*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, MP2975_MFR_APS_DECAY_ADV);
600*2c6fcbb2SVadim Pasternak 	if (ret < 0)
601*2c6fcbb2SVadim Pasternak 		return ret;
602*2c6fcbb2SVadim Pasternak 	thres_dev = ret & MP2975_PRT_THRES_DIV_OV_EN ? MP2975_PROT_DEV_OV_ON :
603*2c6fcbb2SVadim Pasternak 	                                               MP2975_PROT_DEV_OV_OFF;
604*2c6fcbb2SVadim Pasternak 
605*2c6fcbb2SVadim Pasternak 	/* Select the gain of remote sense amplifier. */
606*2c6fcbb2SVadim Pasternak 	ret = i2c_smbus_read_word_data(client, PMBUS_VOUT_SCALE_LOOP);
607*2c6fcbb2SVadim Pasternak 	if (ret < 0)
608*2c6fcbb2SVadim Pasternak 		return ret;
609*2c6fcbb2SVadim Pasternak 	sense_ampl = ret & MP2975_SENSE_AMPL ? MP2975_SENSE_AMPL_HALF :
610*2c6fcbb2SVadim Pasternak 					       MP2975_SENSE_AMPL_UNIT;
611*2c6fcbb2SVadim Pasternak 
612*2c6fcbb2SVadim Pasternak 	data->vout_scale = sense_ampl * thres_dev;
613*2c6fcbb2SVadim Pasternak 
614*2c6fcbb2SVadim Pasternak 	return 0;
615*2c6fcbb2SVadim Pasternak }
616*2c6fcbb2SVadim Pasternak 
617*2c6fcbb2SVadim Pasternak static int
618*2c6fcbb2SVadim Pasternak mp2975_vout_per_rail_config_get(struct i2c_client *client,
619*2c6fcbb2SVadim Pasternak 				struct mp2975_data *data,
620*2c6fcbb2SVadim Pasternak 				struct pmbus_driver_info *info)
621*2c6fcbb2SVadim Pasternak {
622*2c6fcbb2SVadim Pasternak 	int i, ret;
623*2c6fcbb2SVadim Pasternak 
624*2c6fcbb2SVadim Pasternak 	for (i = 0; i < data->info.pages; i++) {
625*2c6fcbb2SVadim Pasternak 		ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
626*2c6fcbb2SVadim Pasternak 		if (ret < 0)
627*2c6fcbb2SVadim Pasternak 			return ret;
628*2c6fcbb2SVadim Pasternak 
629*2c6fcbb2SVadim Pasternak 		/* Obtain voltage reference offsets. */
630*2c6fcbb2SVadim Pasternak 		ret = mp2975_vref_offset_get(client, data, i);
631*2c6fcbb2SVadim Pasternak 		if (ret < 0)
632*2c6fcbb2SVadim Pasternak 			return ret;
633*2c6fcbb2SVadim Pasternak 
634*2c6fcbb2SVadim Pasternak 		/* Obtain maximum voltage values. */
635*2c6fcbb2SVadim Pasternak 		ret = mp2975_vout_max_get(client, data, info, i);
636*2c6fcbb2SVadim Pasternak 		if (ret < 0)
637*2c6fcbb2SVadim Pasternak 			return ret;
638*2c6fcbb2SVadim Pasternak 
639*2c6fcbb2SVadim Pasternak 		/*
640*2c6fcbb2SVadim Pasternak 		 * Get VOUT format for READ_VOUT command : VID or direct.
641*2c6fcbb2SVadim Pasternak 		 * Pages on same device can be configured with different
642*2c6fcbb2SVadim Pasternak 		 * formats.
643*2c6fcbb2SVadim Pasternak 		 */
644*2c6fcbb2SVadim Pasternak 		ret = mp2975_identify_vout_format(client, data, i);
645*2c6fcbb2SVadim Pasternak 		if (ret < 0)
646*2c6fcbb2SVadim Pasternak 			return ret;
647*2c6fcbb2SVadim Pasternak 
648*2c6fcbb2SVadim Pasternak 		/*
649*2c6fcbb2SVadim Pasternak 		 * Set over-voltage fixed value. Thresholds are provided as
650*2c6fcbb2SVadim Pasternak 		 * fixed value, and tracking value. The minimum of them are
651*2c6fcbb2SVadim Pasternak 		 * exposed as over-voltage critical threshold.
652*2c6fcbb2SVadim Pasternak 		 */
653*2c6fcbb2SVadim Pasternak 		data->vout_ov_fixed[i] = data->vref[i] +
654*2c6fcbb2SVadim Pasternak 					 DIV_ROUND_CLOSEST(data->vref_off[i] *
655*2c6fcbb2SVadim Pasternak 							   data->vout_scale,
656*2c6fcbb2SVadim Pasternak 							   10);
657*2c6fcbb2SVadim Pasternak 	}
658*2c6fcbb2SVadim Pasternak 
659*2c6fcbb2SVadim Pasternak 	return 0;
660*2c6fcbb2SVadim Pasternak }
661*2c6fcbb2SVadim Pasternak 
662*2c6fcbb2SVadim Pasternak static struct pmbus_driver_info mp2975_info = {
663*2c6fcbb2SVadim Pasternak 	.pages = 1,
664*2c6fcbb2SVadim Pasternak 	.format[PSC_VOLTAGE_IN] = linear,
665*2c6fcbb2SVadim Pasternak 	.format[PSC_VOLTAGE_OUT] = direct,
666*2c6fcbb2SVadim Pasternak 	.format[PSC_TEMPERATURE] = direct,
667*2c6fcbb2SVadim Pasternak 	.format[PSC_CURRENT_IN] = linear,
668*2c6fcbb2SVadim Pasternak 	.format[PSC_CURRENT_OUT] = direct,
669*2c6fcbb2SVadim Pasternak 	.format[PSC_POWER] = direct,
670*2c6fcbb2SVadim Pasternak 	.m[PSC_TEMPERATURE] = 1,
671*2c6fcbb2SVadim Pasternak 	.m[PSC_VOLTAGE_OUT] = 1,
672*2c6fcbb2SVadim Pasternak 	.R[PSC_VOLTAGE_OUT] = 3,
673*2c6fcbb2SVadim Pasternak 	.m[PSC_CURRENT_OUT] = 1,
674*2c6fcbb2SVadim Pasternak 	.m[PSC_POWER] = 1,
675*2c6fcbb2SVadim Pasternak 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
676*2c6fcbb2SVadim Pasternak 		PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
677*2c6fcbb2SVadim Pasternak 		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_POUT |
678*2c6fcbb2SVadim Pasternak 		PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT | PMBUS_PHASE_VIRTUAL,
679*2c6fcbb2SVadim Pasternak 	.read_byte_data = mp2975_read_byte_data,
680*2c6fcbb2SVadim Pasternak 	.read_word_data = mp2975_read_word_data,
681*2c6fcbb2SVadim Pasternak };
682*2c6fcbb2SVadim Pasternak 
683*2c6fcbb2SVadim Pasternak static int mp2975_probe(struct i2c_client *client)
684*2c6fcbb2SVadim Pasternak {
685*2c6fcbb2SVadim Pasternak 	struct pmbus_driver_info *info;
686*2c6fcbb2SVadim Pasternak 	struct mp2975_data *data;
687*2c6fcbb2SVadim Pasternak 	int ret;
688*2c6fcbb2SVadim Pasternak 
689*2c6fcbb2SVadim Pasternak 	data = devm_kzalloc(&client->dev, sizeof(struct mp2975_data),
690*2c6fcbb2SVadim Pasternak 			    GFP_KERNEL);
691*2c6fcbb2SVadim Pasternak 	if (!data)
692*2c6fcbb2SVadim Pasternak 		return -ENOMEM;
693*2c6fcbb2SVadim Pasternak 
694*2c6fcbb2SVadim Pasternak 	memcpy(&data->info, &mp2975_info, sizeof(*info));
695*2c6fcbb2SVadim Pasternak 	info = &data->info;
696*2c6fcbb2SVadim Pasternak 
697*2c6fcbb2SVadim Pasternak 	/* Identify multiphase configuration for rail 2. */
698*2c6fcbb2SVadim Pasternak 	ret = mp2975_identify_multiphase_rail2(client);
699*2c6fcbb2SVadim Pasternak 	if (ret < 0)
700*2c6fcbb2SVadim Pasternak 		return ret;
701*2c6fcbb2SVadim Pasternak 
702*2c6fcbb2SVadim Pasternak 	if (ret) {
703*2c6fcbb2SVadim Pasternak 		/* Two rails are connected. */
704*2c6fcbb2SVadim Pasternak 		data->info.pages = MP2975_PAGE_NUM;
705*2c6fcbb2SVadim Pasternak 		data->info.phases[1] = ret;
706*2c6fcbb2SVadim Pasternak 		data->info.func[1] = MP2975_RAIL2_FUNC;
707*2c6fcbb2SVadim Pasternak 	}
708*2c6fcbb2SVadim Pasternak 
709*2c6fcbb2SVadim Pasternak 	/* Identify multiphase configuration. */
710*2c6fcbb2SVadim Pasternak 	ret = mp2975_identify_multiphase(client, data, info);
711*2c6fcbb2SVadim Pasternak 	if (ret)
712*2c6fcbb2SVadim Pasternak 		return ret;
713*2c6fcbb2SVadim Pasternak 
714*2c6fcbb2SVadim Pasternak 	/* Identify VID setting per rail. */
715*2c6fcbb2SVadim Pasternak 	ret = mp2975_identify_rails_vid(client, data, info);
716*2c6fcbb2SVadim Pasternak 	if (ret < 0)
717*2c6fcbb2SVadim Pasternak 		return ret;
718*2c6fcbb2SVadim Pasternak 
719*2c6fcbb2SVadim Pasternak 	/* Obtain current sense gain of power stage. */
720*2c6fcbb2SVadim Pasternak 	ret = mp2975_current_sense_gain_get(client, data);
721*2c6fcbb2SVadim Pasternak 	if (ret)
722*2c6fcbb2SVadim Pasternak 		return ret;
723*2c6fcbb2SVadim Pasternak 
724*2c6fcbb2SVadim Pasternak 	/* Obtain voltage reference values. */
725*2c6fcbb2SVadim Pasternak 	ret = mp2975_vref_get(client, data, info);
726*2c6fcbb2SVadim Pasternak 	if (ret)
727*2c6fcbb2SVadim Pasternak 		return ret;
728*2c6fcbb2SVadim Pasternak 
729*2c6fcbb2SVadim Pasternak 	/* Obtain vout over-voltage scales. */
730*2c6fcbb2SVadim Pasternak 	ret = mp2975_vout_ov_scale_get(client, data, info);
731*2c6fcbb2SVadim Pasternak 	if (ret < 0)
732*2c6fcbb2SVadim Pasternak 		return ret;
733*2c6fcbb2SVadim Pasternak 
734*2c6fcbb2SVadim Pasternak 	/* Obtain offsets, maximum and format for vout. */
735*2c6fcbb2SVadim Pasternak 	ret = mp2975_vout_per_rail_config_get(client, data, info);
736*2c6fcbb2SVadim Pasternak 	if (ret)
737*2c6fcbb2SVadim Pasternak 		return ret;
738*2c6fcbb2SVadim Pasternak 
739*2c6fcbb2SVadim Pasternak 	return pmbus_do_probe(client, info);
740*2c6fcbb2SVadim Pasternak }
741*2c6fcbb2SVadim Pasternak 
742*2c6fcbb2SVadim Pasternak static const struct i2c_device_id mp2975_id[] = {
743*2c6fcbb2SVadim Pasternak 	{"mp2975", 0},
744*2c6fcbb2SVadim Pasternak 	{}
745*2c6fcbb2SVadim Pasternak };
746*2c6fcbb2SVadim Pasternak 
747*2c6fcbb2SVadim Pasternak MODULE_DEVICE_TABLE(i2c, mp2975_id);
748*2c6fcbb2SVadim Pasternak 
749*2c6fcbb2SVadim Pasternak static const struct of_device_id __maybe_unused mp2975_of_match[] = {
750*2c6fcbb2SVadim Pasternak 	{.compatible = "mps,mp2975"},
751*2c6fcbb2SVadim Pasternak 	{}
752*2c6fcbb2SVadim Pasternak };
753*2c6fcbb2SVadim Pasternak MODULE_DEVICE_TABLE(of, mp2975_of_match);
754*2c6fcbb2SVadim Pasternak 
755*2c6fcbb2SVadim Pasternak static struct i2c_driver mp2975_driver = {
756*2c6fcbb2SVadim Pasternak 	.driver = {
757*2c6fcbb2SVadim Pasternak 		.name = "mp2975",
758*2c6fcbb2SVadim Pasternak 		.of_match_table = of_match_ptr(mp2975_of_match),
759*2c6fcbb2SVadim Pasternak 	},
760*2c6fcbb2SVadim Pasternak 	.probe_new = mp2975_probe,
761*2c6fcbb2SVadim Pasternak 	.remove = pmbus_do_remove,
762*2c6fcbb2SVadim Pasternak 	.id_table = mp2975_id,
763*2c6fcbb2SVadim Pasternak };
764*2c6fcbb2SVadim Pasternak 
765*2c6fcbb2SVadim Pasternak module_i2c_driver(mp2975_driver);
766*2c6fcbb2SVadim Pasternak 
767*2c6fcbb2SVadim Pasternak MODULE_AUTHOR("Vadim Pasternak <vadimp@nvidia.com>");
768*2c6fcbb2SVadim Pasternak MODULE_DESCRIPTION("PMBus driver for MPS MP2975 device");
769*2c6fcbb2SVadim Pasternak MODULE_LICENSE("GPL");
770