601 lines · c
1// SPDX-License-Identifier: GPL-2.0-or-later2/*3 * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers(MP2891)4 */5 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"11 12/*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 used18 * to read input power per rail. The MP2891 does not have standard19 * READ_IIN register(0x89), the iin telemetry can be obtained through20 * the vendor redefined register READ_IIN_EST(0x95).21 */22#define MFR_VOUT_LOOP_CTRL 0xBD23#define READ_PIN_EST 0x9424#define READ_IIN_EST 0x9525#define MFR_SVI3_IOUT_PRT 0x6526 27#define MP2891_TEMP_LIMIT_OFFSET 4028#define MP2891_PIN_LIMIT_UINT 229#define MP2891_IOUT_LIMIT_UINT 830#define MP2891_IOUT_SCALE_DIV 3231#define MP2891_VOUT_SCALE_DIV 10032#define MP2891_OVUV_DELTA_SCALE 5033#define MP2891_OV_LIMIT_SCALE 2034#define MP2891_UV_LIMIT_SCALE 535 36#define MP2891_PAGE_NUM 237 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)45 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)53 54struct mp2891_data {55 struct pmbus_driver_info info;56 int vout_scale[MP2891_PAGE_NUM];57 int iout_scale[MP2891_PAGE_NUM];58};59 60#define to_mp2891_data(x) container_of(x, struct mp2891_data, info)61 62/* Converts a LINEAR11 value to DIRECT format */63static u16 mp2891_reg2data_linear11(u16 word)64{65 s16 exponent;66 s32 mantissa;67 s64 val;68 69 exponent = ((s16)word) >> 11;70 mantissa = ((s16)((word & 0x7ff) << 5)) >> 5;71 val = mantissa;72 73 if (exponent >= 0)74 val <<= exponent;75 else76 val >>= -exponent;77 78 return val;79}80 81static int82mp2891_identify_vout_scale(struct i2c_client *client, struct pmbus_driver_info *info,83 int page)84{85 struct mp2891_data *data = to_mp2891_data(info);86 int ret;87 88 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);89 if (ret < 0)90 return ret;91 92 ret = i2c_smbus_read_word_data(client, MFR_VOUT_LOOP_CTRL);93 if (ret < 0)94 return ret;95 96 /*97 * The output voltage is equal to the READ_VOUT(0x8B) register value multiplied98 * 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/LSB102 * If MFR_VOUT_LOOP_CTRL[13] = 0, the vout scale is decided by103 * MFR_VOUT_LOOP_CTRL[15:14]:104 * 00b - 6.25mV/LSB, 01b - 5mV/LSB, 10b - 2mV/LSB, 11b - 1mV105 */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 else117 data->vout_scale[page] = 100;118 }119 120 return 0;121}122 123static int124mp2891_identify_iout_scale(struct i2c_client *client, struct pmbus_driver_info *info,125 int page)126{127 struct mp2891_data *data = to_mp2891_data(info);128 int ret;129 130 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);131 if (ret < 0)132 return ret;133 134 ret = i2c_smbus_read_word_data(client, MFR_SVI3_IOUT_PRT);135 if (ret < 0)136 return ret;137 138 /*139 * The output current is equal to the READ_IOUT(0x8C) register value140 * 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/LSB145 * 110b - 1A/LSB, 111b - 2A/LSB146 */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;170 }171 172 return 0;173}174 175static int mp2891_identify(struct i2c_client *client, struct pmbus_driver_info *info)176{177 int ret;178 179 /* Identify vout scale for rail 1. */180 ret = mp2891_identify_vout_scale(client, info, 0);181 if (ret < 0)182 return ret;183 184 /* Identify vout scale for rail 2. */185 ret = mp2891_identify_vout_scale(client, info, 1);186 if (ret < 0)187 return ret;188 189 /* Identify iout scale for rail 1. */190 ret = mp2891_identify_iout_scale(client, info, 0);191 if (ret < 0)192 return ret;193 194 /* Identify iout scale for rail 2. */195 return mp2891_identify_iout_scale(client, info, 1);196}197 198static int mp2891_read_byte_data(struct i2c_client *client, int page, int reg)199{200 int ret;201 202 switch (reg) {203 case PMBUS_VOUT_MODE:204 /*205 * The MP2891 does not follow standard PMBus protocol completely, the206 * PMBUS_VOUT_MODE(0x20) in MP2891 is reserved and 0x00 is always207 * returned when the register is read. But the calculation of vout in208 * this driver is based on direct format. As a result, the format of209 * vout is enforced to direct.210 */211 ret = PB_VOUT_MODE_DIRECT;212 break;213 default:214 ret = -ENODATA;215 break;216 }217 218 return ret;219}220 221static int mp2891_read_word_data(struct i2c_client *client, int page,222 int phase, int reg)223{224 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);225 struct mp2891_data *data = to_mp2891_data(info);226 int ret;227 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;233 234 ret = ret & GENMASK(9, 0);235 break;236 case PMBUS_READ_IIN:237 /*238 * The MP2891 does not have standard PMBUS_READ_IIN register(0x89),239 * the iin telemetry can be obtained through the vender redefined240 * register READ_IIN_EST(0x95). The MP2891 PMBUS_READ_IIN register241 * is linear11 format, But the pout scale is set to 1A/Lsb(using242 * r/m/b scale). As a result, the iin read from MP2891 should be243 * calculated to A, then return the result to pmbus core.244 */245 ret = pmbus_read_word_data(client, page, phase, READ_IIN_EST);246 if (ret < 0)247 return ret;248 249 ret = mp2891_reg2data_linear11(ret);250 break;251 case PMBUS_READ_PIN:252 /*253 * The MP2891 has standard PMBUS_READ_PIN register(0x97), but this254 * is not used to read the input power per rail. The input power255 * per rail is read through the vender redefined register256 * READ_PIN_EST(0x94). The MP2891 PMBUS_READ_PIN register is linear11257 * 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.260 */261 ret = pmbus_read_word_data(client, page, phase, READ_PIN_EST);262 if (ret < 0)263 return ret;264 265 ret = mp2891_reg2data_linear11(ret);266 break;267 case PMBUS_READ_POUT:268 /*269 * The MP2891 PMBUS_READ_POUT register is linear11 format, and the270 * exponent is not a constant value. But the pout scale is set to271 * 1W/Lsb(using r/m/b scale). As a result, the pout read from MP2891272 * should be calculated to W, then return the result to pmbus core.273 */274 ret = pmbus_read_word_data(client, page, phase, reg);275 if (ret < 0)276 return ret;277 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;285 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;292 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:298 /*299 * The scale of MP2891 PMBUS_OT_FAULT_LIMIT and PMBUS_OT_WARN_LIMIT300 * is 1°C/LSB and they have 40°C offset.301 */302 ret = pmbus_read_word_data(client, page, phase, reg);303 if (ret < 0)304 return ret;305 306 ret = (ret & GENMASK(7, 0)) - MP2891_TEMP_LIMIT_OFFSET;307 break;308 case PMBUS_VIN_OV_FAULT_LIMIT:309 /*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.313 */314 ret = pmbus_read_word_data(client, page, phase, reg);315 if (ret < 0)316 return ret;317 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;324 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 else329 ret = FIELD_GET(GENMASK(7, 0), ret) * MP2891_UV_LIMIT_SCALE;330 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;337 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 else342 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;349 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:354 /*355 * The scale of PMBUS_IIN_OC_WARN_LIMIT is 0.5A/Lsb, but the iin scale356 * is set to 1A/Lsb(using r/m/b scale), so the word data should be357 * divided by 2.358 */359 ret = pmbus_read_word_data(client, 0, phase, reg);360 if (ret < 0)361 return ret;362 363 ret = DIV_ROUND_CLOSEST((ret & GENMASK(9, 0)), 2);364 break;365 case PMBUS_PIN_OP_WARN_LIMIT:366 /*367 * The scale of PMBUS_PIN_OP_WARN_LIMIT is 2W/Lsb, but the pin scale368 * is set to 1W/Lsb(using r/m/b scale), so the word data should be369 * multiplied by 2.370 */371 ret = pmbus_read_word_data(client, 0, phase, reg);372 if (ret < 0)373 return ret;374 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;385 }386 387 return ret;388}389 390static int mp2891_write_word_data(struct i2c_client *client, int page, int reg,391 u16 word)392{393 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);394 struct mp2891_data *data = to_mp2891_data(info);395 int ret;396 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:404 /*405 * The PMBUS_VOUT_UV_FAULT_LIMIT[7:0] is the limit value, and bit8-bit15406 * should not be changed.407 */408 ret = pmbus_read_word_data(client, page, 0xff, reg);409 if (ret < 0)410 return ret;411 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 else421 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:428 /*429 * The PMBUS_VOUT_OV_FAULT_LIMIT[7:0] is the limit value, and bit8-bit15430 * should not be changed.431 */432 ret = pmbus_read_word_data(client, page, 0xff, reg);433 if (ret < 0)434 return ret;435 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 else445 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:452 /*453 * The PMBUS_VIN_OV_FAULT_LIMIT[7:0] is the limit value, and bit8-bit15454 * 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 data456 * should be divided by 4.457 */458 ret = pmbus_read_word_data(client, page, 0xff, reg);459 if (ret < 0)460 return ret;461 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:469 /*470 * The scale of MP2891 PMBUS_OT_FAULT_LIMIT and PMBUS_OT_WARN_LIMIT471 * have 40°C offset. The bit0-bit7 is the limit value, and bit8-bit15472 * should not be changed.473 */474 ret = pmbus_read_word_data(client, page, 0xff, reg);475 if (ret < 0)476 return ret;477 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:490 /*491 * The scale of PMBUS_IIN_OC_WARN_LIMIT is 0.5A/Lsb, but the iin scale492 * is set to 1A/Lsb(using r/m/b scale), so the word data should be493 * multiplied by 2.494 */495 ret = pmbus_write_word_data(client, page, reg, word * 2);496 break;497 case PMBUS_PIN_OP_WARN_LIMIT:498 /*499 * The scale of PMBUS_PIN_OP_WARN_LIMIT is 2W/Lsb, but the pin scale500 * is set to 1W/Lsb(using r/m/b scale), so the word data should be501 * divided by 2.502 */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;513 }514 515 return ret;516}517 518static 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,526 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,531 532 /* set temp scale 1000m°C/Lsb */533 .m[PSC_TEMPERATURE] = 1,534 .R[PSC_TEMPERATURE] = 0,535 .b[PSC_TEMPERATURE] = 0,536 537 .m[PSC_CURRENT_IN] = 1,538 .R[PSC_CURRENT_IN] = 0,539 .b[PSC_CURRENT_IN] = 0,540 541 .m[PSC_CURRENT_OUT] = 1,542 .R[PSC_CURRENT_OUT] = 0,543 .b[PSC_CURRENT_OUT] = 0,544 545 .m[PSC_POWER] = 1,546 .R[PSC_POWER] = 0,547 .b[PSC_POWER] = 0,548 549 .m[PSC_VOLTAGE_OUT] = 1,550 .R[PSC_VOLTAGE_OUT] = 3,551 .b[PSC_VOLTAGE_OUT] = 0,552 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,559};560 561static int mp2891_probe(struct i2c_client *client)562{563 struct mp2891_data *data;564 565 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);566 if (!data)567 return -ENOMEM;568 569 memcpy(&data->info, &mp2891_info, sizeof(mp2891_info));570 571 return pmbus_do_probe(client, &data->info);572}573 574static const struct i2c_device_id mp2891_id[] = {575 {"mp2891", 0},576 {}577};578MODULE_DEVICE_TABLE(i2c, mp2891_id);579 580static const struct of_device_id __maybe_unused mp2891_of_match[] = {581 {.compatible = "mps,mp2891"},582 {}583};584MODULE_DEVICE_TABLE(of, mp2891_of_match);585 586static struct i2c_driver mp2891_driver = {587 .driver = {588 .name = "mp2891",589 .of_match_table = mp2891_of_match,590 },591 .probe = mp2891_probe,592 .id_table = mp2891_id,593};594 595module_i2c_driver(mp2891_driver);596 597MODULE_AUTHOR("Noah Wang <noahwang.wang@outlook.com>");598MODULE_DESCRIPTION("PMBus driver for MPS MP2891");599MODULE_LICENSE("GPL");600MODULE_IMPORT_NS(PMBUS);601