xref: /linux/drivers/hwmon/pmbus/mp9941.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1*dc5abc2fSNoah Wang // SPDX-License-Identifier: GPL-2.0-or-later
2*dc5abc2fSNoah Wang /*
3*dc5abc2fSNoah Wang  * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers(MP9941)
4*dc5abc2fSNoah Wang  */
5*dc5abc2fSNoah Wang 
6*dc5abc2fSNoah Wang #include <linux/bitfield.h>
7*dc5abc2fSNoah Wang #include <linux/bits.h>
8*dc5abc2fSNoah Wang #include <linux/i2c.h>
9*dc5abc2fSNoah Wang #include <linux/module.h>
10*dc5abc2fSNoah Wang #include <linux/of_device.h>
11*dc5abc2fSNoah Wang #include "pmbus.h"
12*dc5abc2fSNoah Wang 
13*dc5abc2fSNoah Wang /*
14*dc5abc2fSNoah Wang  * Vender specific registers. The MFR_ICC_MAX(0x02) is used to
15*dc5abc2fSNoah Wang  * config the iin scale. The MFR_RESO_SET(0xC7) is used to
16*dc5abc2fSNoah Wang  * config the vout format. The MFR_VR_MULTI_CONFIG_R1(0x0D) is
17*dc5abc2fSNoah Wang  * used to identify the vout vid step.
18*dc5abc2fSNoah Wang  */
19*dc5abc2fSNoah Wang #define MFR_ICC_MAX	0x02
20*dc5abc2fSNoah Wang #define MFR_RESO_SET	0xC7
21*dc5abc2fSNoah Wang #define MFR_VR_MULTI_CONFIG_R1	0x0D
22*dc5abc2fSNoah Wang 
23*dc5abc2fSNoah Wang #define MP9941_VIN_LIMIT_UINT	1
24*dc5abc2fSNoah Wang #define MP9941_VIN_LIMIT_DIV	8
25*dc5abc2fSNoah Wang #define MP9941_READ_VIN_UINT	1
26*dc5abc2fSNoah Wang #define MP9941_READ_VIN_DIV	32
27*dc5abc2fSNoah Wang 
28*dc5abc2fSNoah Wang #define MP9941_PAGE_NUM	1
29*dc5abc2fSNoah Wang 
30*dc5abc2fSNoah Wang #define MP9941_RAIL1_FUNC	(PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | \
31*dc5abc2fSNoah Wang 							PMBUS_HAVE_IOUT | PMBUS_HAVE_POUT | \
32*dc5abc2fSNoah Wang 							PMBUS_HAVE_TEMP | PMBUS_HAVE_PIN | \
33*dc5abc2fSNoah Wang 							PMBUS_HAVE_IIN | \
34*dc5abc2fSNoah Wang 							PMBUS_HAVE_STATUS_VOUT | \
35*dc5abc2fSNoah Wang 							PMBUS_HAVE_STATUS_IOUT | \
36*dc5abc2fSNoah Wang 							PMBUS_HAVE_STATUS_TEMP | \
37*dc5abc2fSNoah Wang 							PMBUS_HAVE_STATUS_INPUT)
38*dc5abc2fSNoah Wang 
39*dc5abc2fSNoah Wang struct mp9941_data {
40*dc5abc2fSNoah Wang 	struct pmbus_driver_info info;
41*dc5abc2fSNoah Wang 	int vid_resolution;
42*dc5abc2fSNoah Wang };
43*dc5abc2fSNoah Wang 
44*dc5abc2fSNoah Wang #define to_mp9941_data(x) container_of(x, struct mp9941_data, info)
45*dc5abc2fSNoah Wang 
mp9941_set_vout_format(struct i2c_client * client)46*dc5abc2fSNoah Wang static int mp9941_set_vout_format(struct i2c_client *client)
47*dc5abc2fSNoah Wang {
48*dc5abc2fSNoah Wang 	int ret;
49*dc5abc2fSNoah Wang 
50*dc5abc2fSNoah Wang 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
51*dc5abc2fSNoah Wang 	if (ret < 0)
52*dc5abc2fSNoah Wang 		return ret;
53*dc5abc2fSNoah Wang 
54*dc5abc2fSNoah Wang 	ret = i2c_smbus_read_word_data(client, MFR_RESO_SET);
55*dc5abc2fSNoah Wang 	if (ret < 0)
56*dc5abc2fSNoah Wang 		return ret;
57*dc5abc2fSNoah Wang 
58*dc5abc2fSNoah Wang 	/*
59*dc5abc2fSNoah Wang 	 * page = 0, MFR_RESO_SET[7:6] defines the vout format
60*dc5abc2fSNoah Wang 	 * 2'b11 set the vout format as direct
61*dc5abc2fSNoah Wang 	 */
62*dc5abc2fSNoah Wang 	ret = (ret & ~GENMASK(7, 6)) | FIELD_PREP(GENMASK(7, 6), 3);
63*dc5abc2fSNoah Wang 
64*dc5abc2fSNoah Wang 	return i2c_smbus_write_word_data(client, MFR_RESO_SET, ret);
65*dc5abc2fSNoah Wang }
66*dc5abc2fSNoah Wang 
67*dc5abc2fSNoah Wang static int
mp9941_identify_vid_resolution(struct i2c_client * client,struct pmbus_driver_info * info)68*dc5abc2fSNoah Wang mp9941_identify_vid_resolution(struct i2c_client *client, struct pmbus_driver_info *info)
69*dc5abc2fSNoah Wang {
70*dc5abc2fSNoah Wang 	struct mp9941_data *data = to_mp9941_data(info);
71*dc5abc2fSNoah Wang 	int ret;
72*dc5abc2fSNoah Wang 
73*dc5abc2fSNoah Wang 	/*
74*dc5abc2fSNoah Wang 	 * page = 2, MFR_VR_MULTI_CONFIG_R1[4:4] defines rail1 vid step value
75*dc5abc2fSNoah Wang 	 * 1'b0 represents the vid step value is 10mV
76*dc5abc2fSNoah Wang 	 * 1'b1 represents the vid step value is 5mV
77*dc5abc2fSNoah Wang 	 */
78*dc5abc2fSNoah Wang 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
79*dc5abc2fSNoah Wang 	if (ret < 0)
80*dc5abc2fSNoah Wang 		return ret;
81*dc5abc2fSNoah Wang 
82*dc5abc2fSNoah Wang 	ret = i2c_smbus_read_word_data(client, MFR_VR_MULTI_CONFIG_R1);
83*dc5abc2fSNoah Wang 	if (ret < 0)
84*dc5abc2fSNoah Wang 		return ret;
85*dc5abc2fSNoah Wang 
86*dc5abc2fSNoah Wang 	if (FIELD_GET(GENMASK(4, 4), ret))
87*dc5abc2fSNoah Wang 		data->vid_resolution = 5;
88*dc5abc2fSNoah Wang 	else
89*dc5abc2fSNoah Wang 		data->vid_resolution = 10;
90*dc5abc2fSNoah Wang 
91*dc5abc2fSNoah Wang 	return 0;
92*dc5abc2fSNoah Wang }
93*dc5abc2fSNoah Wang 
mp9941_identify_iin_scale(struct i2c_client * client)94*dc5abc2fSNoah Wang static int mp9941_identify_iin_scale(struct i2c_client *client)
95*dc5abc2fSNoah Wang {
96*dc5abc2fSNoah Wang 	int ret;
97*dc5abc2fSNoah Wang 
98*dc5abc2fSNoah Wang 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
99*dc5abc2fSNoah Wang 	if (ret < 0)
100*dc5abc2fSNoah Wang 		return ret;
101*dc5abc2fSNoah Wang 
102*dc5abc2fSNoah Wang 	ret = i2c_smbus_read_word_data(client, MFR_RESO_SET);
103*dc5abc2fSNoah Wang 	if (ret < 0)
104*dc5abc2fSNoah Wang 		return ret;
105*dc5abc2fSNoah Wang 
106*dc5abc2fSNoah Wang 	ret = (ret & ~GENMASK(3, 2)) | FIELD_PREP(GENMASK(3, 2), 0);
107*dc5abc2fSNoah Wang 
108*dc5abc2fSNoah Wang 	ret = i2c_smbus_write_word_data(client, MFR_RESO_SET, ret);
109*dc5abc2fSNoah Wang 	if (ret < 0)
110*dc5abc2fSNoah Wang 		return ret;
111*dc5abc2fSNoah Wang 
112*dc5abc2fSNoah Wang 	/*
113*dc5abc2fSNoah Wang 	 * page = 2, MFR_ICC_MAX[15:13] defines the iin scale
114*dc5abc2fSNoah Wang 	 * 3'b000 set the iout scale as 0.5A/Lsb
115*dc5abc2fSNoah Wang 	 */
116*dc5abc2fSNoah Wang 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
117*dc5abc2fSNoah Wang 	if (ret < 0)
118*dc5abc2fSNoah Wang 		return ret;
119*dc5abc2fSNoah Wang 
120*dc5abc2fSNoah Wang 	ret = i2c_smbus_read_word_data(client, MFR_ICC_MAX);
121*dc5abc2fSNoah Wang 	if (ret < 0)
122*dc5abc2fSNoah Wang 		return ret;
123*dc5abc2fSNoah Wang 
124*dc5abc2fSNoah Wang 	ret = (ret & ~GENMASK(15, 13)) | FIELD_PREP(GENMASK(15, 13), 0);
125*dc5abc2fSNoah Wang 
126*dc5abc2fSNoah Wang 	return i2c_smbus_write_word_data(client, MFR_ICC_MAX, ret);
127*dc5abc2fSNoah Wang }
128*dc5abc2fSNoah Wang 
mp9941_identify(struct i2c_client * client,struct pmbus_driver_info * info)129*dc5abc2fSNoah Wang static int mp9941_identify(struct i2c_client *client, struct pmbus_driver_info *info)
130*dc5abc2fSNoah Wang {
131*dc5abc2fSNoah Wang 	int ret;
132*dc5abc2fSNoah Wang 
133*dc5abc2fSNoah Wang 	ret = mp9941_identify_iin_scale(client);
134*dc5abc2fSNoah Wang 	if (ret < 0)
135*dc5abc2fSNoah Wang 		return ret;
136*dc5abc2fSNoah Wang 
137*dc5abc2fSNoah Wang 	ret = mp9941_identify_vid_resolution(client, info);
138*dc5abc2fSNoah Wang 	if (ret < 0)
139*dc5abc2fSNoah Wang 		return ret;
140*dc5abc2fSNoah Wang 
141*dc5abc2fSNoah Wang 	return mp9941_set_vout_format(client);
142*dc5abc2fSNoah Wang }
143*dc5abc2fSNoah Wang 
mp9941_read_word_data(struct i2c_client * client,int page,int phase,int reg)144*dc5abc2fSNoah Wang static int mp9941_read_word_data(struct i2c_client *client, int page, int phase,
145*dc5abc2fSNoah Wang 				 int reg)
146*dc5abc2fSNoah Wang {
147*dc5abc2fSNoah Wang 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
148*dc5abc2fSNoah Wang 	struct mp9941_data *data = to_mp9941_data(info);
149*dc5abc2fSNoah Wang 	int ret;
150*dc5abc2fSNoah Wang 
151*dc5abc2fSNoah Wang 	switch (reg) {
152*dc5abc2fSNoah Wang 	case PMBUS_READ_VIN:
153*dc5abc2fSNoah Wang 		/* The MP9941 vin scale is (1/32V)/Lsb */
154*dc5abc2fSNoah Wang 		ret = pmbus_read_word_data(client, page, phase, reg);
155*dc5abc2fSNoah Wang 		if (ret < 0)
156*dc5abc2fSNoah Wang 			return ret;
157*dc5abc2fSNoah Wang 
158*dc5abc2fSNoah Wang 		ret = DIV_ROUND_CLOSEST((ret & GENMASK(9, 0)) * MP9941_READ_VIN_UINT,
159*dc5abc2fSNoah Wang 					MP9941_READ_VIN_DIV);
160*dc5abc2fSNoah Wang 		break;
161*dc5abc2fSNoah Wang 	case PMBUS_READ_IIN:
162*dc5abc2fSNoah Wang 		ret = pmbus_read_word_data(client, page, phase, reg);
163*dc5abc2fSNoah Wang 		if (ret < 0)
164*dc5abc2fSNoah Wang 			return ret;
165*dc5abc2fSNoah Wang 
166*dc5abc2fSNoah Wang 		ret = ret & GENMASK(10, 0);
167*dc5abc2fSNoah Wang 		break;
168*dc5abc2fSNoah Wang 	case PMBUS_VIN_OV_FAULT_LIMIT:
169*dc5abc2fSNoah Wang 		/* The MP9941 vin ov limit scale is (1/8V)/Lsb */
170*dc5abc2fSNoah Wang 		ret = pmbus_read_word_data(client, page, phase, reg);
171*dc5abc2fSNoah Wang 		if (ret < 0)
172*dc5abc2fSNoah Wang 			return ret;
173*dc5abc2fSNoah Wang 
174*dc5abc2fSNoah Wang 		ret = DIV_ROUND_CLOSEST((ret & GENMASK(7, 0)) * MP9941_VIN_LIMIT_UINT,
175*dc5abc2fSNoah Wang 					MP9941_VIN_LIMIT_DIV);
176*dc5abc2fSNoah Wang 		break;
177*dc5abc2fSNoah Wang 	case PMBUS_IIN_OC_WARN_LIMIT:
178*dc5abc2fSNoah Wang 		ret = pmbus_read_word_data(client, page, phase, reg);
179*dc5abc2fSNoah Wang 		if (ret < 0)
180*dc5abc2fSNoah Wang 			return ret;
181*dc5abc2fSNoah Wang 
182*dc5abc2fSNoah Wang 		ret = ret & GENMASK(7, 0);
183*dc5abc2fSNoah Wang 		break;
184*dc5abc2fSNoah Wang 	case PMBUS_VOUT_UV_FAULT_LIMIT:
185*dc5abc2fSNoah Wang 	case PMBUS_MFR_VOUT_MIN:
186*dc5abc2fSNoah Wang 	case PMBUS_MFR_VOUT_MAX:
187*dc5abc2fSNoah Wang 		/*
188*dc5abc2fSNoah Wang 		 * The vout scale is set to 1mV/Lsb(using r/m/b scale).
189*dc5abc2fSNoah Wang 		 * But the vout uv limit and vout max/min scale is 1VID/Lsb,
190*dc5abc2fSNoah Wang 		 * so the vout uv limit and vout max/min value should be
191*dc5abc2fSNoah Wang 		 * multiplied by vid resolution.
192*dc5abc2fSNoah Wang 		 */
193*dc5abc2fSNoah Wang 		ret = pmbus_read_word_data(client, page, phase, reg);
194*dc5abc2fSNoah Wang 		if (ret < 0)
195*dc5abc2fSNoah Wang 			return ret;
196*dc5abc2fSNoah Wang 
197*dc5abc2fSNoah Wang 		ret = ret * data->vid_resolution;
198*dc5abc2fSNoah Wang 		break;
199*dc5abc2fSNoah Wang 	case PMBUS_READ_IOUT:
200*dc5abc2fSNoah Wang 	case PMBUS_READ_POUT:
201*dc5abc2fSNoah Wang 	case PMBUS_READ_TEMPERATURE_1:
202*dc5abc2fSNoah Wang 	case PMBUS_READ_VOUT:
203*dc5abc2fSNoah Wang 	case PMBUS_READ_PIN:
204*dc5abc2fSNoah Wang 	case PMBUS_OT_FAULT_LIMIT:
205*dc5abc2fSNoah Wang 	case PMBUS_OT_WARN_LIMIT:
206*dc5abc2fSNoah Wang 		ret = -ENODATA;
207*dc5abc2fSNoah Wang 		break;
208*dc5abc2fSNoah Wang 	default:
209*dc5abc2fSNoah Wang 		ret = -EINVAL;
210*dc5abc2fSNoah Wang 		break;
211*dc5abc2fSNoah Wang 	}
212*dc5abc2fSNoah Wang 
213*dc5abc2fSNoah Wang 	return ret;
214*dc5abc2fSNoah Wang }
215*dc5abc2fSNoah Wang 
mp9941_write_word_data(struct i2c_client * client,int page,int reg,u16 word)216*dc5abc2fSNoah Wang static int mp9941_write_word_data(struct i2c_client *client, int page, int reg,
217*dc5abc2fSNoah Wang 				  u16 word)
218*dc5abc2fSNoah Wang {
219*dc5abc2fSNoah Wang 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
220*dc5abc2fSNoah Wang 	struct mp9941_data *data = to_mp9941_data(info);
221*dc5abc2fSNoah Wang 	int ret;
222*dc5abc2fSNoah Wang 
223*dc5abc2fSNoah Wang 	switch (reg) {
224*dc5abc2fSNoah Wang 	case PMBUS_VIN_OV_FAULT_LIMIT:
225*dc5abc2fSNoah Wang 		/* The MP9941 vin ov limit scale is (1/8V)/Lsb */
226*dc5abc2fSNoah Wang 		ret = pmbus_write_word_data(client, page, reg,
227*dc5abc2fSNoah Wang 					    DIV_ROUND_CLOSEST(word * MP9941_VIN_LIMIT_DIV,
228*dc5abc2fSNoah Wang 							      MP9941_VIN_LIMIT_UINT));
229*dc5abc2fSNoah Wang 		break;
230*dc5abc2fSNoah Wang 	case PMBUS_VOUT_UV_FAULT_LIMIT:
231*dc5abc2fSNoah Wang 	case PMBUS_MFR_VOUT_MIN:
232*dc5abc2fSNoah Wang 	case PMBUS_MFR_VOUT_MAX:
233*dc5abc2fSNoah Wang 		ret = pmbus_write_word_data(client, page, reg,
234*dc5abc2fSNoah Wang 					    DIV_ROUND_CLOSEST(word, data->vid_resolution));
235*dc5abc2fSNoah Wang 		break;
236*dc5abc2fSNoah Wang 	case PMBUS_IIN_OC_WARN_LIMIT:
237*dc5abc2fSNoah Wang 	case PMBUS_OT_FAULT_LIMIT:
238*dc5abc2fSNoah Wang 	case PMBUS_OT_WARN_LIMIT:
239*dc5abc2fSNoah Wang 		ret = -ENODATA;
240*dc5abc2fSNoah Wang 		break;
241*dc5abc2fSNoah Wang 	default:
242*dc5abc2fSNoah Wang 		ret = -EINVAL;
243*dc5abc2fSNoah Wang 		break;
244*dc5abc2fSNoah Wang 	}
245*dc5abc2fSNoah Wang 
246*dc5abc2fSNoah Wang 	return ret;
247*dc5abc2fSNoah Wang }
248*dc5abc2fSNoah Wang 
249*dc5abc2fSNoah Wang static const struct pmbus_driver_info mp9941_info = {
250*dc5abc2fSNoah Wang 	.pages = MP9941_PAGE_NUM,
251*dc5abc2fSNoah Wang 	.format[PSC_VOLTAGE_IN] = direct,
252*dc5abc2fSNoah Wang 	.format[PSC_CURRENT_IN] = direct,
253*dc5abc2fSNoah Wang 	.format[PSC_CURRENT_OUT] = linear,
254*dc5abc2fSNoah Wang 	.format[PSC_POWER] = linear,
255*dc5abc2fSNoah Wang 	.format[PSC_TEMPERATURE] = direct,
256*dc5abc2fSNoah Wang 	.format[PSC_VOLTAGE_OUT] = direct,
257*dc5abc2fSNoah Wang 
258*dc5abc2fSNoah Wang 	.m[PSC_TEMPERATURE] = 1,
259*dc5abc2fSNoah Wang 	.R[PSC_TEMPERATURE] = 0,
260*dc5abc2fSNoah Wang 	.b[PSC_TEMPERATURE] = 0,
261*dc5abc2fSNoah Wang 
262*dc5abc2fSNoah Wang 	.m[PSC_VOLTAGE_IN] = 1,
263*dc5abc2fSNoah Wang 	.R[PSC_VOLTAGE_IN] = 0,
264*dc5abc2fSNoah Wang 	.b[PSC_VOLTAGE_IN] = 0,
265*dc5abc2fSNoah Wang 
266*dc5abc2fSNoah Wang 	.m[PSC_CURRENT_IN] = 2,
267*dc5abc2fSNoah Wang 	.R[PSC_CURRENT_IN] = 0,
268*dc5abc2fSNoah Wang 	.b[PSC_CURRENT_IN] = 0,
269*dc5abc2fSNoah Wang 
270*dc5abc2fSNoah Wang 	.m[PSC_VOLTAGE_OUT] = 1,
271*dc5abc2fSNoah Wang 	.R[PSC_VOLTAGE_OUT] = 3,
272*dc5abc2fSNoah Wang 	.b[PSC_VOLTAGE_OUT] = 0,
273*dc5abc2fSNoah Wang 
274*dc5abc2fSNoah Wang 	.func[0] = MP9941_RAIL1_FUNC,
275*dc5abc2fSNoah Wang 	.read_word_data = mp9941_read_word_data,
276*dc5abc2fSNoah Wang 	.write_word_data = mp9941_write_word_data,
277*dc5abc2fSNoah Wang 	.identify = mp9941_identify,
278*dc5abc2fSNoah Wang };
279*dc5abc2fSNoah Wang 
mp9941_probe(struct i2c_client * client)280*dc5abc2fSNoah Wang static int mp9941_probe(struct i2c_client *client)
281*dc5abc2fSNoah Wang {
282*dc5abc2fSNoah Wang 	struct mp9941_data *data;
283*dc5abc2fSNoah Wang 
284*dc5abc2fSNoah Wang 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
285*dc5abc2fSNoah Wang 	if (!data)
286*dc5abc2fSNoah Wang 		return -ENOMEM;
287*dc5abc2fSNoah Wang 
288*dc5abc2fSNoah Wang 	memcpy(&data->info, &mp9941_info, sizeof(mp9941_info));
289*dc5abc2fSNoah Wang 
290*dc5abc2fSNoah Wang 	return pmbus_do_probe(client, &data->info);
291*dc5abc2fSNoah Wang }
292*dc5abc2fSNoah Wang 
293*dc5abc2fSNoah Wang static const struct i2c_device_id mp9941_id[] = {
294*dc5abc2fSNoah Wang 	{"mp9941", 0},
295*dc5abc2fSNoah Wang 	{}
296*dc5abc2fSNoah Wang };
297*dc5abc2fSNoah Wang MODULE_DEVICE_TABLE(i2c, mp9941_id);
298*dc5abc2fSNoah Wang 
299*dc5abc2fSNoah Wang static const struct of_device_id __maybe_unused mp9941_of_match[] = {
300*dc5abc2fSNoah Wang 	{.compatible = "mps,mp9941"},
301*dc5abc2fSNoah Wang 	{}
302*dc5abc2fSNoah Wang };
303*dc5abc2fSNoah Wang MODULE_DEVICE_TABLE(of, mp9941_of_match);
304*dc5abc2fSNoah Wang 
305*dc5abc2fSNoah Wang static struct i2c_driver mp9941_driver = {
306*dc5abc2fSNoah Wang 	.driver = {
307*dc5abc2fSNoah Wang 		.name = "mp9941",
308*dc5abc2fSNoah Wang 		.of_match_table = mp9941_of_match,
309*dc5abc2fSNoah Wang 	},
310*dc5abc2fSNoah Wang 	.probe = mp9941_probe,
311*dc5abc2fSNoah Wang 	.id_table = mp9941_id,
312*dc5abc2fSNoah Wang };
313*dc5abc2fSNoah Wang 
314*dc5abc2fSNoah Wang module_i2c_driver(mp9941_driver);
315*dc5abc2fSNoah Wang 
316*dc5abc2fSNoah Wang MODULE_AUTHOR("Noah Wang <noahwang.wang@outlook.com>");
317*dc5abc2fSNoah Wang MODULE_DESCRIPTION("PMBus driver for MPS MP9941");
318*dc5abc2fSNoah Wang MODULE_LICENSE("GPL");
319*dc5abc2fSNoah Wang MODULE_IMPORT_NS(PMBUS);
320