Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / hwmon / pmbus / mp2891.c
blobf8f4c91ec23ccde85ad84f21fbb05f303df06185
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers(MP2891)
4 */
6 #include <linux/bitfield.h>
7 #include <linux/i2c.h>
8 #include <linux/module.h>
9 #include <linux/of_device.h>
10 #include "pmbus.h"
13 * Vender specific registers, the register MFR_SVI3_IOUT_PRT(0x65),
14 * MFR_VOUT_LOOP_CTRL(0xBD), READ_PIN_EST(0x94)and READ_IIN_EST(0x95)
15 * redefine the standard PMBUS register. The MFR_SVI3_IOUT_PRT(0x65)
16 * is used to identify the iout scale and the MFR_VOUT_LOOP_CTRL(0xBD)
17 * is used to identify the vout scale. The READ_PIN_EST(0x94) is used
18 * to read input power per rail. The MP2891 does not have standard
19 * READ_IIN register(0x89), the iin telemetry can be obtained through
20 * the vendor redefined register READ_IIN_EST(0x95).
22 #define MFR_VOUT_LOOP_CTRL 0xBD
23 #define READ_PIN_EST 0x94
24 #define READ_IIN_EST 0x95
25 #define MFR_SVI3_IOUT_PRT 0x65
27 #define MP2891_TEMP_LIMIT_OFFSET 40
28 #define MP2891_PIN_LIMIT_UINT 2
29 #define MP2891_IOUT_LIMIT_UINT 8
30 #define MP2891_IOUT_SCALE_DIV 32
31 #define MP2891_VOUT_SCALE_DIV 100
32 #define MP2891_OVUV_DELTA_SCALE 50
33 #define MP2891_OV_LIMIT_SCALE 20
34 #define MP2891_UV_LIMIT_SCALE 5
36 #define MP2891_PAGE_NUM 2
38 #define MP2891_RAIL1_FUNC (PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | \
39 PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP | \
40 PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | \
41 PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_VOUT | \
42 PMBUS_HAVE_STATUS_IOUT | \
43 PMBUS_HAVE_STATUS_INPUT | \
44 PMBUS_HAVE_STATUS_TEMP)
46 #define MP2891_RAIL2_FUNC (PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | \
47 PMBUS_HAVE_TEMP | PMBUS_HAVE_POUT | \
48 PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | \
49 PMBUS_HAVE_STATUS_VOUT | \
50 PMBUS_HAVE_STATUS_IOUT | \
51 PMBUS_HAVE_STATUS_INPUT | \
52 PMBUS_HAVE_STATUS_TEMP)
54 struct mp2891_data {
55 struct pmbus_driver_info info;
56 int vout_scale[MP2891_PAGE_NUM];
57 int iout_scale[MP2891_PAGE_NUM];
60 #define to_mp2891_data(x) container_of(x, struct mp2891_data, info)
62 /* Converts a LINEAR11 value to DIRECT format */
63 static u16 mp2891_reg2data_linear11(u16 word)
65 s16 exponent;
66 s32 mantissa;
67 s64 val;
69 exponent = ((s16)word) >> 11;
70 mantissa = ((s16)((word & 0x7ff) << 5)) >> 5;
71 val = mantissa;
73 if (exponent >= 0)
74 val <<= exponent;
75 else
76 val >>= -exponent;
78 return val;
81 static int
82 mp2891_identify_vout_scale(struct i2c_client *client, struct pmbus_driver_info *info,
83 int page)
85 struct mp2891_data *data = to_mp2891_data(info);
86 int ret;
88 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
89 if (ret < 0)
90 return ret;
92 ret = i2c_smbus_read_word_data(client, MFR_VOUT_LOOP_CTRL);
93 if (ret < 0)
94 return ret;
97 * The output voltage is equal to the READ_VOUT(0x8B) register value multiplied
98 * by vout_scale.
99 * Obtain vout scale from the register MFR_VOUT_LOOP_CTRL, bits 15-14,bit 13.
100 * If MFR_VOUT_LOOP_CTRL[13] = 1, the vout scale is below:
101 * 2.5mV/LSB
102 * If MFR_VOUT_LOOP_CTRL[13] = 0, the vout scale is decided by
103 * MFR_VOUT_LOOP_CTRL[15:14]:
104 * 00b - 6.25mV/LSB, 01b - 5mV/LSB, 10b - 2mV/LSB, 11b - 1mV
106 if (ret & GENMASK(13, 13)) {
107 data->vout_scale[page] = 250;
108 } else {
109 ret = FIELD_GET(GENMASK(15, 14), ret);
110 if (ret == 0)
111 data->vout_scale[page] = 625;
112 else if (ret == 1)
113 data->vout_scale[page] = 500;
114 else if (ret == 2)
115 data->vout_scale[page] = 200;
116 else
117 data->vout_scale[page] = 100;
120 return 0;
123 static int
124 mp2891_identify_iout_scale(struct i2c_client *client, struct pmbus_driver_info *info,
125 int page)
127 struct mp2891_data *data = to_mp2891_data(info);
128 int ret;
130 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
131 if (ret < 0)
132 return ret;
134 ret = i2c_smbus_read_word_data(client, MFR_SVI3_IOUT_PRT);
135 if (ret < 0)
136 return ret;
139 * The output current is equal to the READ_IOUT(0x8C) register value
140 * multiplied by iout_scale.
141 * Obtain iout_scale from the register MFR_SVI3_IOUT_PRT[2:0].
142 * The value is selected as below:
143 * 000b - 1A/LSB, 001b - (1/32)A/LSB, 010b - (1/16)A/LSB,
144 * 011b - (1/8)A/LSB, 100b - (1/4)A/LSB, 101b - (1/2)A/LSB
145 * 110b - 1A/LSB, 111b - 2A/LSB
147 switch (ret & GENMASK(2, 0)) {
148 case 0:
149 case 6:
150 data->iout_scale[page] = 32;
151 break;
152 case 1:
153 data->iout_scale[page] = 1;
154 break;
155 case 2:
156 data->iout_scale[page] = 2;
157 break;
158 case 3:
159 data->iout_scale[page] = 4;
160 break;
161 case 4:
162 data->iout_scale[page] = 8;
163 break;
164 case 5:
165 data->iout_scale[page] = 16;
166 break;
167 default:
168 data->iout_scale[page] = 64;
169 break;
172 return 0;
175 static int mp2891_identify(struct i2c_client *client, struct pmbus_driver_info *info)
177 int ret;
179 /* Identify vout scale for rail 1. */
180 ret = mp2891_identify_vout_scale(client, info, 0);
181 if (ret < 0)
182 return ret;
184 /* Identify vout scale for rail 2. */
185 ret = mp2891_identify_vout_scale(client, info, 1);
186 if (ret < 0)
187 return ret;
189 /* Identify iout scale for rail 1. */
190 ret = mp2891_identify_iout_scale(client, info, 0);
191 if (ret < 0)
192 return ret;
194 /* Identify iout scale for rail 2. */
195 return mp2891_identify_iout_scale(client, info, 1);
198 static int mp2891_read_byte_data(struct i2c_client *client, int page, int reg)
200 int ret;
202 switch (reg) {
203 case PMBUS_VOUT_MODE:
205 * The MP2891 does not follow standard PMBus protocol completely, the
206 * PMBUS_VOUT_MODE(0x20) in MP2891 is reserved and 0x00 is always
207 * returned when the register is read. But the calculation of vout in
208 * this driver is based on direct format. As a result, the format of
209 * vout is enforced to direct.
211 ret = PB_VOUT_MODE_DIRECT;
212 break;
213 default:
214 ret = -ENODATA;
215 break;
218 return ret;
221 static int mp2891_read_word_data(struct i2c_client *client, int page,
222 int phase, int reg)
224 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
225 struct mp2891_data *data = to_mp2891_data(info);
226 int ret;
228 switch (reg) {
229 case PMBUS_READ_VIN:
230 ret = pmbus_read_word_data(client, page, phase, reg);
231 if (ret < 0)
232 return ret;
234 ret = ret & GENMASK(9, 0);
235 break;
236 case PMBUS_READ_IIN:
238 * The MP2891 does not have standard PMBUS_READ_IIN register(0x89),
239 * the iin telemetry can be obtained through the vender redefined
240 * register READ_IIN_EST(0x95). The MP2891 PMBUS_READ_IIN register
241 * is linear11 format, But the pout scale is set to 1A/Lsb(using
242 * r/m/b scale). As a result, the iin read from MP2891 should be
243 * calculated to A, then return the result to pmbus core.
245 ret = pmbus_read_word_data(client, page, phase, READ_IIN_EST);
246 if (ret < 0)
247 return ret;
249 ret = mp2891_reg2data_linear11(ret);
250 break;
251 case PMBUS_READ_PIN:
253 * The MP2891 has standard PMBUS_READ_PIN register(0x97), but this
254 * is not used to read the input power per rail. The input power
255 * per rail is read through the vender redefined register
256 * READ_PIN_EST(0x94). The MP2891 PMBUS_READ_PIN register is linear11
257 * format, But the pout scale is set to 1W/Lsb(using r/m/b scale).
258 * As a result, the pin read from MP2891 should be calculated to W,
259 * then return the result to pmbus core.
261 ret = pmbus_read_word_data(client, page, phase, READ_PIN_EST);
262 if (ret < 0)
263 return ret;
265 ret = mp2891_reg2data_linear11(ret);
266 break;
267 case PMBUS_READ_POUT:
269 * The MP2891 PMBUS_READ_POUT register is linear11 format, and the
270 * exponent is not a constant value. But the pout scale is set to
271 * 1W/Lsb(using r/m/b scale). As a result, the pout read from MP2891
272 * should be calculated to W, then return the result to pmbus core.
274 ret = pmbus_read_word_data(client, page, phase, reg);
275 if (ret < 0)
276 return ret;
278 ret = mp2891_reg2data_linear11(ret);
279 break;
280 case PMBUS_READ_VOUT:
281 case PMBUS_VOUT_UV_WARN_LIMIT:
282 ret = pmbus_read_word_data(client, page, phase, reg);
283 if (ret < 0)
284 return ret;
286 ret = DIV_ROUND_CLOSEST(ret * data->vout_scale[page], MP2891_VOUT_SCALE_DIV);
287 break;
288 case PMBUS_READ_IOUT:
289 ret = pmbus_read_word_data(client, page, phase, reg);
290 if (ret < 0)
291 return ret;
293 ret = DIV_ROUND_CLOSEST((ret & GENMASK(10, 0)) * data->iout_scale[page],
294 MP2891_IOUT_SCALE_DIV);
295 break;
296 case PMBUS_OT_FAULT_LIMIT:
297 case PMBUS_OT_WARN_LIMIT:
299 * The scale of MP2891 PMBUS_OT_FAULT_LIMIT and PMBUS_OT_WARN_LIMIT
300 * is 1°C/LSB and they have 40°C offset.
302 ret = pmbus_read_word_data(client, page, phase, reg);
303 if (ret < 0)
304 return ret;
306 ret = (ret & GENMASK(7, 0)) - MP2891_TEMP_LIMIT_OFFSET;
307 break;
308 case PMBUS_VIN_OV_FAULT_LIMIT:
310 * The MP2891 PMBUS_VIN_OV_FAULT_LIMIT scale is 125mV/Lsb.
311 * but the vin scale is set to 31.25mV/Lsb(using r/m/b scale).
312 * As a result, the limit value should be multiplied by 4.
314 ret = pmbus_read_word_data(client, page, phase, reg);
315 if (ret < 0)
316 return ret;
318 ret = (ret & GENMASK(7, 0)) * 4;
319 break;
320 case PMBUS_VOUT_UV_FAULT_LIMIT:
321 ret = pmbus_read_word_data(client, page, phase, reg);
322 if (ret < 0)
323 return ret;
325 if (FIELD_GET(GENMASK(11, 8), ret))
326 ret = FIELD_GET(GENMASK(7, 0), ret) * MP2891_UV_LIMIT_SCALE -
327 (FIELD_GET(GENMASK(11, 8), ret) + 1) * MP2891_OVUV_DELTA_SCALE;
328 else
329 ret = FIELD_GET(GENMASK(7, 0), ret) * MP2891_UV_LIMIT_SCALE;
331 ret = ret < 0 ? 0 : ret;
332 break;
333 case PMBUS_VOUT_OV_FAULT_LIMIT:
334 ret = pmbus_read_word_data(client, page, phase, reg);
335 if (ret < 0)
336 return ret;
338 if (FIELD_GET(GENMASK(11, 8), ret))
339 ret = FIELD_GET(GENMASK(7, 0), ret) * MP2891_OV_LIMIT_SCALE +
340 (FIELD_GET(GENMASK(11, 8), ret) + 1) * MP2891_OVUV_DELTA_SCALE;
341 else
342 ret = FIELD_GET(GENMASK(7, 0), ret) * MP2891_OV_LIMIT_SCALE;
343 break;
344 case PMBUS_IOUT_OC_WARN_LIMIT:
345 case PMBUS_IOUT_OC_FAULT_LIMIT:
346 ret = pmbus_read_word_data(client, page, phase, reg);
347 if (ret < 0)
348 return ret;
350 ret = DIV_ROUND_CLOSEST((ret & GENMASK(7, 0)) * data->iout_scale[page] *
351 MP2891_IOUT_LIMIT_UINT, MP2891_IOUT_SCALE_DIV);
352 break;
353 case PMBUS_IIN_OC_WARN_LIMIT:
355 * The scale of PMBUS_IIN_OC_WARN_LIMIT is 0.5A/Lsb, but the iin scale
356 * is set to 1A/Lsb(using r/m/b scale), so the word data should be
357 * divided by 2.
359 ret = pmbus_read_word_data(client, 0, phase, reg);
360 if (ret < 0)
361 return ret;
363 ret = DIV_ROUND_CLOSEST((ret & GENMASK(9, 0)), 2);
364 break;
365 case PMBUS_PIN_OP_WARN_LIMIT:
367 * The scale of PMBUS_PIN_OP_WARN_LIMIT is 2W/Lsb, but the pin scale
368 * is set to 1W/Lsb(using r/m/b scale), so the word data should be
369 * multiplied by 2.
371 ret = pmbus_read_word_data(client, 0, phase, reg);
372 if (ret < 0)
373 return ret;
375 ret = (ret & GENMASK(9, 0)) * MP2891_PIN_LIMIT_UINT;
376 break;
377 case PMBUS_READ_TEMPERATURE_1:
378 case PMBUS_VIN_UV_FAULT_LIMIT:
379 case PMBUS_VIN_UV_WARN_LIMIT:
380 ret = -ENODATA;
381 break;
382 default:
383 ret = -EINVAL;
384 break;
387 return ret;
390 static int mp2891_write_word_data(struct i2c_client *client, int page, int reg,
391 u16 word)
393 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
394 struct mp2891_data *data = to_mp2891_data(info);
395 int ret;
397 switch (reg) {
398 case PMBUS_VOUT_UV_WARN_LIMIT:
399 ret = pmbus_write_word_data(client, page, reg,
400 DIV_ROUND_CLOSEST(word * MP2891_VOUT_SCALE_DIV,
401 data->vout_scale[page]));
402 break;
403 case PMBUS_VOUT_UV_FAULT_LIMIT:
405 * The PMBUS_VOUT_UV_FAULT_LIMIT[7:0] is the limit value, and bit8-bit15
406 * should not be changed.
408 ret = pmbus_read_word_data(client, page, 0xff, reg);
409 if (ret < 0)
410 return ret;
412 if (FIELD_GET(GENMASK(11, 8), ret))
413 ret = pmbus_write_word_data(client, page, reg,
414 (ret & ~GENMASK(7, 0)) |
415 FIELD_PREP(GENMASK(7, 0),
416 DIV_ROUND_CLOSEST(word +
417 (FIELD_GET(GENMASK(11, 8), ret) + 1) *
418 MP2891_OVUV_DELTA_SCALE,
419 MP2891_UV_LIMIT_SCALE)));
420 else
421 ret = pmbus_write_word_data(client, page, reg,
422 (ret & ~GENMASK(7, 0)) |
423 FIELD_PREP(GENMASK(7, 0),
424 DIV_ROUND_CLOSEST(word,
425 MP2891_UV_LIMIT_SCALE)));
426 break;
427 case PMBUS_VOUT_OV_FAULT_LIMIT:
429 * The PMBUS_VOUT_OV_FAULT_LIMIT[7:0] is the limit value, and bit8-bit15
430 * should not be changed.
432 ret = pmbus_read_word_data(client, page, 0xff, reg);
433 if (ret < 0)
434 return ret;
436 if (FIELD_GET(GENMASK(11, 8), ret))
437 ret = pmbus_write_word_data(client, page, reg,
438 (ret & ~GENMASK(7, 0)) |
439 FIELD_PREP(GENMASK(7, 0),
440 DIV_ROUND_CLOSEST(word -
441 (FIELD_GET(GENMASK(11, 8), ret) + 1) *
442 MP2891_OVUV_DELTA_SCALE,
443 MP2891_OV_LIMIT_SCALE)));
444 else
445 ret = pmbus_write_word_data(client, page, reg,
446 (ret & ~GENMASK(7, 0)) |
447 FIELD_PREP(GENMASK(7, 0),
448 DIV_ROUND_CLOSEST(word,
449 MP2891_OV_LIMIT_SCALE)));
450 break;
451 case PMBUS_VIN_OV_FAULT_LIMIT:
453 * The PMBUS_VIN_OV_FAULT_LIMIT[7:0] is the limit value, and bit8-bit15
454 * should not be changed. The scale of PMBUS_VIN_OV_FAULT_LIMIT is 125mV/Lsb,
455 * but the vin scale is set to 31.25mV/Lsb(using r/m/b scale), so the word data
456 * should be divided by 4.
458 ret = pmbus_read_word_data(client, page, 0xff, reg);
459 if (ret < 0)
460 return ret;
462 ret = pmbus_write_word_data(client, page, reg,
463 (ret & ~GENMASK(7, 0)) |
464 FIELD_PREP(GENMASK(7, 0),
465 DIV_ROUND_CLOSEST(word, 4)));
466 break;
467 case PMBUS_OT_FAULT_LIMIT:
468 case PMBUS_OT_WARN_LIMIT:
470 * The scale of MP2891 PMBUS_OT_FAULT_LIMIT and PMBUS_OT_WARN_LIMIT
471 * have 40°C offset. The bit0-bit7 is the limit value, and bit8-bit15
472 * should not be changed.
474 ret = pmbus_read_word_data(client, page, 0xff, reg);
475 if (ret < 0)
476 return ret;
478 ret = pmbus_write_word_data(client, page, reg,
479 (ret & ~GENMASK(7, 0)) |
480 FIELD_PREP(GENMASK(7, 0), word + MP2891_TEMP_LIMIT_OFFSET));
481 break;
482 case PMBUS_IOUT_OC_WARN_LIMIT:
483 case PMBUS_IOUT_OC_FAULT_LIMIT:
484 ret = pmbus_write_word_data(client, page, reg,
485 DIV_ROUND_CLOSEST(word * MP2891_IOUT_SCALE_DIV,
486 MP2891_IOUT_LIMIT_UINT *
487 data->iout_scale[page]));
488 break;
489 case PMBUS_IIN_OC_WARN_LIMIT:
491 * The scale of PMBUS_IIN_OC_WARN_LIMIT is 0.5A/Lsb, but the iin scale
492 * is set to 1A/Lsb(using r/m/b scale), so the word data should be
493 * multiplied by 2.
495 ret = pmbus_write_word_data(client, page, reg, word * 2);
496 break;
497 case PMBUS_PIN_OP_WARN_LIMIT:
499 * The scale of PMBUS_PIN_OP_WARN_LIMIT is 2W/Lsb, but the pin scale
500 * is set to 1W/Lsb(using r/m/b scale), so the word data should be
501 * divided by 2.
503 ret = pmbus_write_word_data(client, page, reg,
504 DIV_ROUND_CLOSEST(word, MP2891_PIN_LIMIT_UINT));
505 break;
506 case PMBUS_VIN_UV_FAULT_LIMIT:
507 case PMBUS_VIN_UV_WARN_LIMIT:
508 ret = -ENODATA;
509 break;
510 default:
511 ret = -EINVAL;
512 break;
515 return ret;
518 static const struct pmbus_driver_info mp2891_info = {
519 .pages = MP2891_PAGE_NUM,
520 .format[PSC_VOLTAGE_IN] = direct,
521 .format[PSC_CURRENT_IN] = direct,
522 .format[PSC_CURRENT_OUT] = direct,
523 .format[PSC_TEMPERATURE] = direct,
524 .format[PSC_POWER] = direct,
525 .format[PSC_VOLTAGE_OUT] = direct,
527 /* set vin scale 31.25mV/Lsb */
528 .m[PSC_VOLTAGE_IN] = 32,
529 .R[PSC_VOLTAGE_IN] = 0,
530 .b[PSC_VOLTAGE_IN] = 0,
532 /* set temp scale 1000m°C/Lsb */
533 .m[PSC_TEMPERATURE] = 1,
534 .R[PSC_TEMPERATURE] = 0,
535 .b[PSC_TEMPERATURE] = 0,
537 .m[PSC_CURRENT_IN] = 1,
538 .R[PSC_CURRENT_IN] = 0,
539 .b[PSC_CURRENT_IN] = 0,
541 .m[PSC_CURRENT_OUT] = 1,
542 .R[PSC_CURRENT_OUT] = 0,
543 .b[PSC_CURRENT_OUT] = 0,
545 .m[PSC_POWER] = 1,
546 .R[PSC_POWER] = 0,
547 .b[PSC_POWER] = 0,
549 .m[PSC_VOLTAGE_OUT] = 1,
550 .R[PSC_VOLTAGE_OUT] = 3,
551 .b[PSC_VOLTAGE_OUT] = 0,
553 .func[0] = MP2891_RAIL1_FUNC,
554 .func[1] = MP2891_RAIL2_FUNC,
555 .read_word_data = mp2891_read_word_data,
556 .write_word_data = mp2891_write_word_data,
557 .read_byte_data = mp2891_read_byte_data,
558 .identify = mp2891_identify,
561 static int mp2891_probe(struct i2c_client *client)
563 struct mp2891_data *data;
565 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
566 if (!data)
567 return -ENOMEM;
569 memcpy(&data->info, &mp2891_info, sizeof(mp2891_info));
571 return pmbus_do_probe(client, &data->info);
574 static const struct i2c_device_id mp2891_id[] = {
575 { "mp2891" },
578 MODULE_DEVICE_TABLE(i2c, mp2891_id);
580 static const struct of_device_id __maybe_unused mp2891_of_match[] = {
581 {.compatible = "mps,mp2891"},
584 MODULE_DEVICE_TABLE(of, mp2891_of_match);
586 static struct i2c_driver mp2891_driver = {
587 .driver = {
588 .name = "mp2891",
589 .of_match_table = mp2891_of_match,
591 .probe = mp2891_probe,
592 .id_table = mp2891_id,
595 module_i2c_driver(mp2891_driver);
597 MODULE_AUTHOR("Noah Wang <noahwang.wang@outlook.com>");
598 MODULE_DESCRIPTION("PMBus driver for MPS MP2891");
599 MODULE_LICENSE("GPL");
600 MODULE_IMPORT_NS("PMBUS");