1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers(MP9941)
6 #include <linux/bitfield.h>
7 #include <linux/bits.h>
9 #include <linux/module.h>
10 #include <linux/of_device.h>
14 * Vender specific registers. The MFR_ICC_MAX(0x02) is used to
15 * config the iin scale. The MFR_RESO_SET(0xC7) is used to
16 * config the vout format. The MFR_VR_MULTI_CONFIG_R1(0x0D) is
17 * used to identify the vout vid step.
19 #define MFR_ICC_MAX 0x02
20 #define MFR_RESO_SET 0xC7
21 #define MFR_VR_MULTI_CONFIG_R1 0x0D
23 #define MP9941_VIN_LIMIT_UINT 1
24 #define MP9941_VIN_LIMIT_DIV 8
25 #define MP9941_READ_VIN_UINT 1
26 #define MP9941_READ_VIN_DIV 32
28 #define MP9941_PAGE_NUM 1
30 #define MP9941_RAIL1_FUNC (PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | \
31 PMBUS_HAVE_IOUT | PMBUS_HAVE_POUT | \
32 PMBUS_HAVE_TEMP | PMBUS_HAVE_PIN | \
34 PMBUS_HAVE_STATUS_VOUT | \
35 PMBUS_HAVE_STATUS_IOUT | \
36 PMBUS_HAVE_STATUS_TEMP | \
37 PMBUS_HAVE_STATUS_INPUT)
40 struct pmbus_driver_info info
;
44 #define to_mp9941_data(x) container_of(x, struct mp9941_data, info)
46 static int mp9941_set_vout_format(struct i2c_client
*client
)
50 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, 0);
54 ret
= i2c_smbus_read_word_data(client
, MFR_RESO_SET
);
59 * page = 0, MFR_RESO_SET[7:6] defines the vout format
60 * 2'b11 set the vout format as direct
62 ret
= (ret
& ~GENMASK(7, 6)) | FIELD_PREP(GENMASK(7, 6), 3);
64 return i2c_smbus_write_word_data(client
, MFR_RESO_SET
, ret
);
68 mp9941_identify_vid_resolution(struct i2c_client
*client
, struct pmbus_driver_info
*info
)
70 struct mp9941_data
*data
= to_mp9941_data(info
);
74 * page = 2, MFR_VR_MULTI_CONFIG_R1[4:4] defines rail1 vid step value
75 * 1'b0 represents the vid step value is 10mV
76 * 1'b1 represents the vid step value is 5mV
78 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, 2);
82 ret
= i2c_smbus_read_word_data(client
, MFR_VR_MULTI_CONFIG_R1
);
86 if (FIELD_GET(GENMASK(4, 4), ret
))
87 data
->vid_resolution
= 5;
89 data
->vid_resolution
= 10;
94 static int mp9941_identify_iin_scale(struct i2c_client
*client
)
98 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, 0);
102 ret
= i2c_smbus_read_word_data(client
, MFR_RESO_SET
);
106 ret
= (ret
& ~GENMASK(3, 2)) | FIELD_PREP(GENMASK(3, 2), 0);
108 ret
= i2c_smbus_write_word_data(client
, MFR_RESO_SET
, ret
);
113 * page = 2, MFR_ICC_MAX[15:13] defines the iin scale
114 * 3'b000 set the iout scale as 0.5A/Lsb
116 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, 2);
120 ret
= i2c_smbus_read_word_data(client
, MFR_ICC_MAX
);
124 ret
= (ret
& ~GENMASK(15, 13)) | FIELD_PREP(GENMASK(15, 13), 0);
126 return i2c_smbus_write_word_data(client
, MFR_ICC_MAX
, ret
);
129 static int mp9941_identify(struct i2c_client
*client
, struct pmbus_driver_info
*info
)
133 ret
= mp9941_identify_iin_scale(client
);
137 ret
= mp9941_identify_vid_resolution(client
, info
);
141 return mp9941_set_vout_format(client
);
144 static int mp9941_read_word_data(struct i2c_client
*client
, int page
, int phase
,
147 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
148 struct mp9941_data
*data
= to_mp9941_data(info
);
153 /* The MP9941 vin scale is (1/32V)/Lsb */
154 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
158 ret
= DIV_ROUND_CLOSEST((ret
& GENMASK(9, 0)) * MP9941_READ_VIN_UINT
,
159 MP9941_READ_VIN_DIV
);
162 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
166 ret
= ret
& GENMASK(10, 0);
168 case PMBUS_VIN_OV_FAULT_LIMIT
:
169 /* The MP9941 vin ov limit scale is (1/8V)/Lsb */
170 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
174 ret
= DIV_ROUND_CLOSEST((ret
& GENMASK(7, 0)) * MP9941_VIN_LIMIT_UINT
,
175 MP9941_VIN_LIMIT_DIV
);
177 case PMBUS_IIN_OC_WARN_LIMIT
:
178 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
182 ret
= ret
& GENMASK(7, 0);
184 case PMBUS_VOUT_UV_FAULT_LIMIT
:
185 case PMBUS_MFR_VOUT_MIN
:
186 case PMBUS_MFR_VOUT_MAX
:
188 * The vout scale is set to 1mV/Lsb(using r/m/b scale).
189 * But the vout uv limit and vout max/min scale is 1VID/Lsb,
190 * so the vout uv limit and vout max/min value should be
191 * multiplied by vid resolution.
193 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
197 ret
= ret
* data
->vid_resolution
;
199 case PMBUS_READ_IOUT
:
200 case PMBUS_READ_POUT
:
201 case PMBUS_READ_TEMPERATURE_1
:
202 case PMBUS_READ_VOUT
:
204 case PMBUS_OT_FAULT_LIMIT
:
205 case PMBUS_OT_WARN_LIMIT
:
216 static int mp9941_write_word_data(struct i2c_client
*client
, int page
, int reg
,
219 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
220 struct mp9941_data
*data
= to_mp9941_data(info
);
224 case PMBUS_VIN_OV_FAULT_LIMIT
:
225 /* The MP9941 vin ov limit scale is (1/8V)/Lsb */
226 ret
= pmbus_write_word_data(client
, page
, reg
,
227 DIV_ROUND_CLOSEST(word
* MP9941_VIN_LIMIT_DIV
,
228 MP9941_VIN_LIMIT_UINT
));
230 case PMBUS_VOUT_UV_FAULT_LIMIT
:
231 case PMBUS_MFR_VOUT_MIN
:
232 case PMBUS_MFR_VOUT_MAX
:
233 ret
= pmbus_write_word_data(client
, page
, reg
,
234 DIV_ROUND_CLOSEST(word
, data
->vid_resolution
));
236 case PMBUS_IIN_OC_WARN_LIMIT
:
237 case PMBUS_OT_FAULT_LIMIT
:
238 case PMBUS_OT_WARN_LIMIT
:
249 static const struct pmbus_driver_info mp9941_info
= {
250 .pages
= MP9941_PAGE_NUM
,
251 .format
[PSC_VOLTAGE_IN
] = direct
,
252 .format
[PSC_CURRENT_IN
] = direct
,
253 .format
[PSC_CURRENT_OUT
] = linear
,
254 .format
[PSC_POWER
] = linear
,
255 .format
[PSC_TEMPERATURE
] = direct
,
256 .format
[PSC_VOLTAGE_OUT
] = direct
,
258 .m
[PSC_TEMPERATURE
] = 1,
259 .R
[PSC_TEMPERATURE
] = 0,
260 .b
[PSC_TEMPERATURE
] = 0,
262 .m
[PSC_VOLTAGE_IN
] = 1,
263 .R
[PSC_VOLTAGE_IN
] = 0,
264 .b
[PSC_VOLTAGE_IN
] = 0,
266 .m
[PSC_CURRENT_IN
] = 2,
267 .R
[PSC_CURRENT_IN
] = 0,
268 .b
[PSC_CURRENT_IN
] = 0,
270 .m
[PSC_VOLTAGE_OUT
] = 1,
271 .R
[PSC_VOLTAGE_OUT
] = 3,
272 .b
[PSC_VOLTAGE_OUT
] = 0,
274 .func
[0] = MP9941_RAIL1_FUNC
,
275 .read_word_data
= mp9941_read_word_data
,
276 .write_word_data
= mp9941_write_word_data
,
277 .identify
= mp9941_identify
,
280 static int mp9941_probe(struct i2c_client
*client
)
282 struct mp9941_data
*data
;
284 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
288 memcpy(&data
->info
, &mp9941_info
, sizeof(mp9941_info
));
290 return pmbus_do_probe(client
, &data
->info
);
293 static const struct i2c_device_id mp9941_id
[] = {
297 MODULE_DEVICE_TABLE(i2c
, mp9941_id
);
299 static const struct of_device_id __maybe_unused mp9941_of_match
[] = {
300 {.compatible
= "mps,mp9941"},
303 MODULE_DEVICE_TABLE(of
, mp9941_of_match
);
305 static struct i2c_driver mp9941_driver
= {
308 .of_match_table
= mp9941_of_match
,
310 .probe
= mp9941_probe
,
311 .id_table
= mp9941_id
,
314 module_i2c_driver(mp9941_driver
);
316 MODULE_AUTHOR("Noah Wang <noahwang.wang@outlook.com>");
317 MODULE_DESCRIPTION("PMBus driver for MPS MP9941");
318 MODULE_LICENSE("GPL");
319 MODULE_IMPORT_NS(PMBUS
);