1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Hardware monitoring driver for MPS2856/2857 4 * Monolithic Power Systems VR Controllers 5 * 6 * Copyright (C) 2023 Quanta Computer lnc. 7 */ 8 9 #include <linux/err.h> 10 #include <linux/i2c.h> 11 #include <linux/init.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/pmbus.h> 15 #include "pmbus.h" 16 17 /* Vendor specific registers. */ 18 #define MP2856_MFR_VR_MULTI_CONFIG_R1 0x0d 19 #define MP2856_MFR_VR_MULTI_CONFIG_R2 0x1d 20 21 #define MP2856_MUL1_BOOT_SR_R2 0x10 22 #define MP2856_VR_ACTIVE BIT(15) 23 24 #define MP2856_MFR_VR_CONFIG2 0x5e 25 #define MP2856_VOUT_MODE BIT(11) 26 27 #define MP2856_MFR_VR_CONFIG1 0x68 28 #define MP2856_DRMOS_KCS GENMASK(13, 12) 29 30 #define MP2856_MFR_READ_CS1_2_R1 0x82 31 #define MP2856_MFR_READ_CS3_4_R1 0x83 32 #define MP2856_MFR_READ_CS5_6_R1 0x84 33 #define MP2856_MFR_READ_CS7_8_R1 0x85 34 #define MP2856_MFR_READ_CS9_10_R1 0x86 35 #define MP2856_MFR_READ_CS11_12_R1 0x87 36 37 #define MP2856_MFR_READ_CS1_2_R2 0x85 38 #define MP2856_MFR_READ_CS3_4_R2 0x86 39 #define MP2856_MFR_READ_CS5_6_R2 0x87 40 41 #define MP2856_MAX_PHASE_RAIL1 8 42 #define MP2856_MAX_PHASE_RAIL2 4 43 44 #define MP2857_MAX_PHASE_RAIL1 12 45 #define MP2857_MAX_PHASE_RAIL2 4 46 47 #define MP2856_PAGE_NUM 2 48 49 enum chips { mp2856 = 1, mp2857 }; 50 51 static const int mp2856_max_phases[][MP2856_PAGE_NUM] = { 52 [mp2856] = { MP2856_MAX_PHASE_RAIL1, MP2856_MAX_PHASE_RAIL2 }, 53 [mp2857] = { MP2857_MAX_PHASE_RAIL1, MP2857_MAX_PHASE_RAIL2 }, 54 }; 55 56 static const struct i2c_device_id mp2856_id[] = { 57 {"mp2856", mp2856}, 58 {"mp2857", mp2857}, 59 {} 60 }; 61 62 MODULE_DEVICE_TABLE(i2c, mp2856_id); 63 64 struct mp2856_data { 65 struct pmbus_driver_info info; 66 int vout_format[MP2856_PAGE_NUM]; 67 int curr_sense_gain[MP2856_PAGE_NUM]; 68 int max_phases[MP2856_PAGE_NUM]; 69 enum chips chip_id; 70 }; 71 72 #define to_mp2856_data(x) container_of(x, struct mp2856_data, info) 73 74 #define MAX_LIN_MANTISSA (1023 * 1000) 75 #define MIN_LIN_MANTISSA (511 * 1000) 76 77 static u16 val2linear11(s64 val) 78 { 79 s16 exponent = 0, mantissa; 80 bool negative = false; 81 82 if (val == 0) 83 return 0; 84 85 if (val < 0) { 86 negative = true; 87 val = -val; 88 } 89 90 /* Reduce large mantissa until it fits into 10 bit */ 91 while (val >= MAX_LIN_MANTISSA && exponent < 15) { 92 exponent++; 93 val >>= 1; 94 } 95 /* Increase small mantissa to improve precision */ 96 while (val < MIN_LIN_MANTISSA && exponent > -15) { 97 exponent--; 98 val <<= 1; 99 } 100 101 /* Convert mantissa from milli-units to units */ 102 mantissa = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff); 103 104 /* restore sign */ 105 if (negative) 106 mantissa = -mantissa; 107 108 /* Convert to 5 bit exponent, 11 bit mantissa */ 109 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800); 110 } 111 112 static int 113 mp2856_read_word_helper(struct i2c_client *client, int page, int phase, u8 reg, 114 u16 mask) 115 { 116 int ret = pmbus_read_word_data(client, page, phase, reg); 117 118 return (ret > 0) ? ret & mask : ret; 119 } 120 121 static int 122 mp2856_read_vout(struct i2c_client *client, struct mp2856_data *data, int page, 123 int phase, u8 reg) 124 { 125 int ret; 126 127 ret = mp2856_read_word_helper(client, page, phase, reg, 128 GENMASK(9, 0)); 129 if (ret < 0) 130 return ret; 131 132 /* convert vout result to direct format */ 133 ret = (data->vout_format[page] == vid) ? 134 ((ret + 49) * 5) : ((ret * 1000) >> 8); 135 136 return ret; 137 } 138 139 static int 140 mp2856_read_phase(struct i2c_client *client, struct mp2856_data *data, 141 int page, int phase, u8 reg) 142 { 143 int ret; 144 int val; 145 146 ret = pmbus_read_word_data(client, page, phase, reg); 147 if (ret < 0) 148 return ret; 149 150 if (!((phase + 1) % MP2856_PAGE_NUM)) 151 ret >>= 8; 152 ret &= 0xff; 153 154 /* 155 * Output value is calculated as: (READ_CSx * 12.5mV - 1.23V) / (Kcs * Rcs) 156 */ 157 val = (ret * 125) - 12300; 158 159 return val2linear11(val); 160 } 161 162 static int 163 mp2856_read_phases(struct i2c_client *client, struct mp2856_data *data, 164 int page, int phase) 165 { 166 int ret; 167 168 if (page == 0) { 169 switch (phase) { 170 case 0 ... 1: 171 ret = mp2856_read_phase(client, data, page, phase, 172 MP2856_MFR_READ_CS1_2_R1); 173 break; 174 case 2 ... 3: 175 ret = mp2856_read_phase(client, data, page, phase, 176 MP2856_MFR_READ_CS3_4_R1); 177 break; 178 case 4 ... 5: 179 ret = mp2856_read_phase(client, data, page, phase, 180 MP2856_MFR_READ_CS5_6_R1); 181 break; 182 case 6 ... 7: 183 ret = mp2856_read_phase(client, data, page, phase, 184 MP2856_MFR_READ_CS7_8_R1); 185 break; 186 default: 187 return -ENODATA; 188 } 189 } else { 190 switch (phase) { 191 case 0 ... 1: 192 ret = mp2856_read_phase(client, data, page, phase, 193 MP2856_MFR_READ_CS1_2_R2); 194 break; 195 case 2 ... 3: 196 ret = mp2856_read_phase(client, data, page, phase, 197 MP2856_MFR_READ_CS1_2_R2); 198 break; 199 default: 200 return -ENODATA; 201 } 202 } 203 return ret; 204 } 205 206 static int 207 mp2856_read_word_data(struct i2c_client *client, int page, 208 int phase, int reg) 209 { 210 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 211 struct mp2856_data *data = to_mp2856_data(info); 212 int ret; 213 214 switch (reg) { 215 case PMBUS_READ_VOUT: 216 ret = mp2856_read_vout(client, data, page, phase, reg); 217 break; 218 case PMBUS_READ_IOUT: 219 if (phase != 0xff) 220 ret = mp2856_read_phases(client, data, page, phase); 221 else 222 ret = pmbus_read_word_data(client, page, phase, reg); 223 break; 224 default: 225 return -ENODATA; 226 } 227 228 return ret; 229 } 230 231 static int 232 mp2856_read_byte_data(struct i2c_client *client, int page, int reg) 233 { 234 switch (reg) { 235 case PMBUS_VOUT_MODE: 236 /* Enforce VOUT direct format. */ 237 return PB_VOUT_MODE_DIRECT; 238 default: 239 return -ENODATA; 240 } 241 } 242 243 static int 244 mp2856_identify_multiphase(struct i2c_client *client, u8 reg, u8 max_phase, 245 u16 mask) 246 { 247 int ret; 248 249 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2); 250 if (ret < 0) 251 return ret; 252 253 ret = i2c_smbus_read_word_data(client, reg); 254 if (ret < 0) 255 return ret; 256 257 ret &= mask; 258 return (ret >= max_phase) ? max_phase : ret; 259 } 260 261 static int 262 mp2856_identify_multiphase_rail1(struct i2c_client *client, 263 struct mp2856_data *data) 264 { 265 int ret, i; 266 267 ret = mp2856_identify_multiphase(client, MP2856_MFR_VR_MULTI_CONFIG_R1, 268 MP2856_MAX_PHASE_RAIL1, GENMASK(3, 0)); 269 if (ret < 0) 270 return ret; 271 272 data->info.phases[0] = (ret > data->max_phases[0]) ? 273 data->max_phases[0] : ret; 274 275 for (i = 0 ; i < data->info.phases[0]; i++) 276 data->info.pfunc[i] |= PMBUS_HAVE_IOUT; 277 278 return 0; 279 } 280 281 static int 282 mp2856_identify_multiphase_rail2(struct i2c_client *client, 283 struct mp2856_data *data) 284 { 285 int ret, i; 286 287 ret = mp2856_identify_multiphase(client, MP2856_MFR_VR_MULTI_CONFIG_R2, 288 MP2856_MAX_PHASE_RAIL2, GENMASK(2, 0)); 289 if (ret < 0) 290 return ret; 291 292 data->info.phases[1] = (ret > data->max_phases[1]) ? 293 data->max_phases[1] : ret; 294 295 for (i = 0 ; i < data->info.phases[0]; i++) 296 data->info.pfunc[i] |= PMBUS_HAVE_IOUT; 297 298 return 0; 299 } 300 301 static int 302 mp2856_current_sense_gain_get(struct i2c_client *client, 303 struct mp2856_data *data) 304 { 305 int i, ret; 306 307 /* 308 * Obtain DrMOS current sense gain of power stage from the register 309 * MP2856_MFR_VR_CONFIG1, bits 13-12. The value is selected as below: 310 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. Other 311 * values are invalid. 312 */ 313 for (i = 0 ; i < data->info.pages; i++) { 314 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i); 315 if (ret < 0) 316 return ret; 317 ret = i2c_smbus_read_word_data(client, 318 MP2856_MFR_VR_CONFIG1); 319 if (ret < 0) 320 return ret; 321 322 switch ((ret & MP2856_DRMOS_KCS) >> 12) { 323 case 0: 324 data->curr_sense_gain[i] = 50; 325 break; 326 case 1: 327 data->curr_sense_gain[i] = 85; 328 break; 329 case 2: 330 data->curr_sense_gain[i] = 97; 331 break; 332 default: 333 data->curr_sense_gain[i] = 100; 334 break; 335 } 336 } 337 return 0; 338 } 339 340 static int 341 mp2856_identify_vout_format(struct i2c_client *client, 342 struct mp2856_data *data) 343 { 344 int i, ret; 345 346 for (i = 0; i < data->info.pages; i++) { 347 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i); 348 if (ret < 0) 349 return ret; 350 351 ret = i2c_smbus_read_word_data(client, MP2856_MFR_VR_CONFIG2); 352 if (ret < 0) 353 return ret; 354 355 data->vout_format[i] = (ret & MP2856_VOUT_MODE) ? linear : vid; 356 } 357 return 0; 358 } 359 360 static bool 361 mp2856_is_rail2_active(struct i2c_client *client) 362 { 363 int ret; 364 365 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2); 366 if (ret < 0) 367 return true; 368 369 ret = i2c_smbus_read_word_data(client, MP2856_MUL1_BOOT_SR_R2); 370 if (ret < 0) 371 return true; 372 373 return (ret & MP2856_VR_ACTIVE) ? true : false; 374 } 375 376 static struct pmbus_driver_info mp2856_info = { 377 .pages = MP2856_PAGE_NUM, 378 .format[PSC_VOLTAGE_IN] = linear, 379 .format[PSC_VOLTAGE_OUT] = direct, 380 .format[PSC_TEMPERATURE] = linear, 381 .format[PSC_CURRENT_IN] = linear, 382 .format[PSC_CURRENT_OUT] = linear, 383 .format[PSC_POWER] = linear, 384 .m[PSC_VOLTAGE_OUT] = 1, 385 .R[PSC_VOLTAGE_OUT] = 3, 386 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | 387 PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | 388 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_POUT | 389 PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT, 390 .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_IOUT | 391 PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP, 392 .read_byte_data = mp2856_read_byte_data, 393 .read_word_data = mp2856_read_word_data, 394 }; 395 396 static int mp2856_probe(struct i2c_client *client) 397 { 398 struct pmbus_driver_info *info; 399 struct mp2856_data *data; 400 int ret; 401 402 data = devm_kzalloc(&client->dev, sizeof(struct mp2856_data), 403 GFP_KERNEL); 404 if (!data) 405 return -ENOMEM; 406 407 data->chip_id = (enum chips)(uintptr_t)i2c_get_match_data(client); 408 409 memcpy(data->max_phases, mp2856_max_phases[data->chip_id], 410 sizeof(data->max_phases)); 411 412 memcpy(&data->info, &mp2856_info, sizeof(*info)); 413 info = &data->info; 414 415 /* Identify multiphase configuration. */ 416 ret = mp2856_identify_multiphase_rail1(client, data); 417 if (ret < 0) 418 return ret; 419 420 if (mp2856_is_rail2_active(client)) { 421 ret = mp2856_identify_multiphase_rail2(client, data); 422 if (ret < 0) 423 return ret; 424 } else { 425 /* rail2 is not active */ 426 info->pages = 1; 427 } 428 429 /* Obtain current sense gain of power stage. */ 430 ret = mp2856_current_sense_gain_get(client, data); 431 if (ret) 432 return ret; 433 434 /* Identify vout format. */ 435 ret = mp2856_identify_vout_format(client, data); 436 if (ret) 437 return ret; 438 439 /* set the device to page 0 */ 440 i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0); 441 442 return pmbus_do_probe(client, info); 443 } 444 445 static const struct of_device_id __maybe_unused mp2856_of_match[] = { 446 {.compatible = "mps,mp2856", .data = (void *)mp2856}, 447 {.compatible = "mps,mp2857", .data = (void *)mp2857}, 448 {} 449 }; 450 MODULE_DEVICE_TABLE(of, mp2856_of_match); 451 452 static struct i2c_driver mp2856_driver = { 453 .driver = { 454 .name = "mp2856", 455 .of_match_table = mp2856_of_match, 456 }, 457 .probe = mp2856_probe, 458 .id_table = mp2856_id, 459 }; 460 461 module_i2c_driver(mp2856_driver); 462 463 MODULE_AUTHOR("Peter Yin <peter.yin@quantatw.com>"); 464 MODULE_DESCRIPTION("PMBus driver for MPS MP2856/MP2857 device"); 465 MODULE_LICENSE("GPL"); 466 MODULE_IMPORT_NS(PMBUS); 467