1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Hardware monitoring driver for LM25056 / LM25066 / LM5064 / LM5066 4 * 5 * Copyright (c) 2011 Ericsson AB. 6 * Copyright (c) 2013 Guenter Roeck 7 */ 8 9 #include <linux/bitops.h> 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/init.h> 13 #include <linux/err.h> 14 #include <linux/slab.h> 15 #include <linux/i2c.h> 16 #include <linux/log2.h> 17 #include "pmbus.h" 18 19 enum chips { lm25056, lm25066, lm5064, lm5066, lm5066i }; 20 21 #define LM25066_READ_VAUX 0xd0 22 #define LM25066_MFR_READ_IIN 0xd1 23 #define LM25066_MFR_READ_PIN 0xd2 24 #define LM25066_MFR_IIN_OC_WARN_LIMIT 0xd3 25 #define LM25066_MFR_PIN_OP_WARN_LIMIT 0xd4 26 #define LM25066_READ_PIN_PEAK 0xd5 27 #define LM25066_CLEAR_PIN_PEAK 0xd6 28 #define LM25066_DEVICE_SETUP 0xd9 29 #define LM25066_READ_AVG_VIN 0xdc 30 #define LM25066_SAMPLES_FOR_AVG 0xdb 31 #define LM25066_READ_AVG_VOUT 0xdd 32 #define LM25066_READ_AVG_IIN 0xde 33 #define LM25066_READ_AVG_PIN 0xdf 34 35 #define LM25066_DEV_SETUP_CL BIT(4) /* Current limit */ 36 37 #define LM25066_SAMPLES_FOR_AVG_MAX 4096 38 39 /* LM25056 only */ 40 41 #define LM25056_VAUX_OV_WARN_LIMIT 0xe3 42 #define LM25056_VAUX_UV_WARN_LIMIT 0xe4 43 44 #define LM25056_MFR_STS_VAUX_OV_WARN BIT(1) 45 #define LM25056_MFR_STS_VAUX_UV_WARN BIT(0) 46 47 struct __coeff { 48 short m, b, R; 49 }; 50 51 #define PSC_CURRENT_IN_L (PSC_NUM_CLASSES) 52 #define PSC_POWER_L (PSC_NUM_CLASSES + 1) 53 54 static struct __coeff lm25066_coeff[6][PSC_NUM_CLASSES + 2] = { 55 [lm25056] = { 56 [PSC_VOLTAGE_IN] = { 57 .m = 16296, 58 .R = -2, 59 }, 60 [PSC_CURRENT_IN] = { 61 .m = 13797, 62 .R = -2, 63 }, 64 [PSC_CURRENT_IN_L] = { 65 .m = 6726, 66 .R = -2, 67 }, 68 [PSC_POWER] = { 69 .m = 5501, 70 .R = -3, 71 }, 72 [PSC_POWER_L] = { 73 .m = 26882, 74 .R = -4, 75 }, 76 [PSC_TEMPERATURE] = { 77 .m = 1580, 78 .b = -14500, 79 .R = -2, 80 }, 81 }, 82 [lm25066] = { 83 [PSC_VOLTAGE_IN] = { 84 .m = 22070, 85 .R = -2, 86 }, 87 [PSC_VOLTAGE_OUT] = { 88 .m = 22070, 89 .R = -2, 90 }, 91 [PSC_CURRENT_IN] = { 92 .m = 13661, 93 .R = -2, 94 }, 95 [PSC_CURRENT_IN_L] = { 96 .m = 6852, 97 .R = -2, 98 }, 99 [PSC_POWER] = { 100 .m = 736, 101 .R = -2, 102 }, 103 [PSC_POWER_L] = { 104 .m = 369, 105 .R = -2, 106 }, 107 [PSC_TEMPERATURE] = { 108 .m = 16, 109 }, 110 }, 111 [lm5064] = { 112 [PSC_VOLTAGE_IN] = { 113 .m = 4611, 114 .R = -2, 115 }, 116 [PSC_VOLTAGE_OUT] = { 117 .m = 4621, 118 .R = -2, 119 }, 120 [PSC_CURRENT_IN] = { 121 .m = 10742, 122 .R = -2, 123 }, 124 [PSC_CURRENT_IN_L] = { 125 .m = 5456, 126 .R = -2, 127 }, 128 [PSC_POWER] = { 129 .m = 1204, 130 .R = -3, 131 }, 132 [PSC_POWER_L] = { 133 .m = 612, 134 .R = -3, 135 }, 136 [PSC_TEMPERATURE] = { 137 .m = 16, 138 }, 139 }, 140 [lm5066] = { 141 [PSC_VOLTAGE_IN] = { 142 .m = 4587, 143 .R = -2, 144 }, 145 [PSC_VOLTAGE_OUT] = { 146 .m = 4587, 147 .R = -2, 148 }, 149 [PSC_CURRENT_IN] = { 150 .m = 10753, 151 .R = -2, 152 }, 153 [PSC_CURRENT_IN_L] = { 154 .m = 5405, 155 .R = -2, 156 }, 157 [PSC_POWER] = { 158 .m = 1204, 159 .R = -3, 160 }, 161 [PSC_POWER_L] = { 162 .m = 605, 163 .R = -3, 164 }, 165 [PSC_TEMPERATURE] = { 166 .m = 16, 167 }, 168 }, 169 [lm5066i] = { 170 [PSC_VOLTAGE_IN] = { 171 .m = 4617, 172 .b = -140, 173 .R = -2, 174 }, 175 [PSC_VOLTAGE_OUT] = { 176 .m = 4602, 177 .b = 500, 178 .R = -2, 179 }, 180 [PSC_CURRENT_IN] = { 181 .m = 15076, 182 .b = -504, 183 .R = -2, 184 }, 185 [PSC_CURRENT_IN_L] = { 186 .m = 7645, 187 .b = 100, 188 .R = -2, 189 }, 190 [PSC_POWER] = { 191 .m = 1701, 192 .b = -4000, 193 .R = -3, 194 }, 195 [PSC_POWER_L] = { 196 .m = 861, 197 .b = -965, 198 .R = -3, 199 }, 200 [PSC_TEMPERATURE] = { 201 .m = 16, 202 }, 203 }, 204 }; 205 206 struct lm25066_data { 207 int id; 208 u16 rlimit; /* Maximum register value */ 209 struct pmbus_driver_info info; 210 }; 211 212 #define to_lm25066_data(x) container_of(x, struct lm25066_data, info) 213 214 static int lm25066_read_word_data(struct i2c_client *client, int page, 215 int phase, int reg) 216 { 217 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 218 const struct lm25066_data *data = to_lm25066_data(info); 219 int ret; 220 221 switch (reg) { 222 case PMBUS_VIRT_READ_VMON: 223 ret = pmbus_read_word_data(client, 0, 0xff, LM25066_READ_VAUX); 224 if (ret < 0) 225 break; 226 /* Adjust returned value to match VIN coefficients */ 227 switch (data->id) { 228 case lm25056: 229 /* VIN: 6.14 mV VAUX: 293 uV LSB */ 230 ret = DIV_ROUND_CLOSEST(ret * 293, 6140); 231 break; 232 case lm25066: 233 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */ 234 ret = DIV_ROUND_CLOSEST(ret * 2832, 45400); 235 break; 236 case lm5064: 237 /* VIN: 4.53 mV VAUX: 700 uV LSB */ 238 ret = DIV_ROUND_CLOSEST(ret * 70, 453); 239 break; 240 case lm5066: 241 case lm5066i: 242 /* VIN: 2.18 mV VAUX: 725 uV LSB */ 243 ret = DIV_ROUND_CLOSEST(ret * 725, 2180); 244 break; 245 } 246 break; 247 case PMBUS_READ_IIN: 248 ret = pmbus_read_word_data(client, 0, 0xff, 249 LM25066_MFR_READ_IIN); 250 break; 251 case PMBUS_READ_PIN: 252 ret = pmbus_read_word_data(client, 0, 0xff, 253 LM25066_MFR_READ_PIN); 254 break; 255 case PMBUS_IIN_OC_WARN_LIMIT: 256 ret = pmbus_read_word_data(client, 0, 0xff, 257 LM25066_MFR_IIN_OC_WARN_LIMIT); 258 break; 259 case PMBUS_PIN_OP_WARN_LIMIT: 260 ret = pmbus_read_word_data(client, 0, 0xff, 261 LM25066_MFR_PIN_OP_WARN_LIMIT); 262 break; 263 case PMBUS_VIRT_READ_VIN_AVG: 264 ret = pmbus_read_word_data(client, 0, 0xff, 265 LM25066_READ_AVG_VIN); 266 break; 267 case PMBUS_VIRT_READ_VOUT_AVG: 268 ret = pmbus_read_word_data(client, 0, 0xff, 269 LM25066_READ_AVG_VOUT); 270 break; 271 case PMBUS_VIRT_READ_IIN_AVG: 272 ret = pmbus_read_word_data(client, 0, 0xff, 273 LM25066_READ_AVG_IIN); 274 break; 275 case PMBUS_VIRT_READ_PIN_AVG: 276 ret = pmbus_read_word_data(client, 0, 0xff, 277 LM25066_READ_AVG_PIN); 278 break; 279 case PMBUS_VIRT_READ_PIN_MAX: 280 ret = pmbus_read_word_data(client, 0, 0xff, 281 LM25066_READ_PIN_PEAK); 282 break; 283 case PMBUS_VIRT_RESET_PIN_HISTORY: 284 ret = 0; 285 break; 286 case PMBUS_VIRT_SAMPLES: 287 ret = pmbus_read_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG); 288 if (ret < 0) 289 break; 290 ret = 1 << ret; 291 break; 292 default: 293 ret = -ENODATA; 294 break; 295 } 296 return ret; 297 } 298 299 static int lm25056_read_word_data(struct i2c_client *client, int page, 300 int phase, int reg) 301 { 302 int ret; 303 304 switch (reg) { 305 case PMBUS_VIRT_VMON_UV_WARN_LIMIT: 306 ret = pmbus_read_word_data(client, 0, 0xff, 307 LM25056_VAUX_UV_WARN_LIMIT); 308 if (ret < 0) 309 break; 310 /* Adjust returned value to match VIN coefficients */ 311 ret = DIV_ROUND_CLOSEST(ret * 293, 6140); 312 break; 313 case PMBUS_VIRT_VMON_OV_WARN_LIMIT: 314 ret = pmbus_read_word_data(client, 0, 0xff, 315 LM25056_VAUX_OV_WARN_LIMIT); 316 if (ret < 0) 317 break; 318 /* Adjust returned value to match VIN coefficients */ 319 ret = DIV_ROUND_CLOSEST(ret * 293, 6140); 320 break; 321 default: 322 ret = lm25066_read_word_data(client, page, phase, reg); 323 break; 324 } 325 return ret; 326 } 327 328 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg) 329 { 330 int ret, s; 331 332 switch (reg) { 333 case PMBUS_VIRT_STATUS_VMON: 334 ret = pmbus_read_byte_data(client, 0, 335 PMBUS_STATUS_MFR_SPECIFIC); 336 if (ret < 0) 337 break; 338 s = 0; 339 if (ret & LM25056_MFR_STS_VAUX_UV_WARN) 340 s |= PB_VOLTAGE_UV_WARNING; 341 if (ret & LM25056_MFR_STS_VAUX_OV_WARN) 342 s |= PB_VOLTAGE_OV_WARNING; 343 ret = s; 344 break; 345 default: 346 ret = -ENODATA; 347 break; 348 } 349 return ret; 350 } 351 352 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg, 353 u16 word) 354 { 355 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 356 const struct lm25066_data *data = to_lm25066_data(info); 357 int ret; 358 359 switch (reg) { 360 case PMBUS_POUT_OP_FAULT_LIMIT: 361 case PMBUS_POUT_OP_WARN_LIMIT: 362 case PMBUS_VOUT_UV_WARN_LIMIT: 363 case PMBUS_OT_FAULT_LIMIT: 364 case PMBUS_OT_WARN_LIMIT: 365 case PMBUS_IIN_OC_FAULT_LIMIT: 366 case PMBUS_VIN_UV_WARN_LIMIT: 367 case PMBUS_VIN_UV_FAULT_LIMIT: 368 case PMBUS_VIN_OV_FAULT_LIMIT: 369 case PMBUS_VIN_OV_WARN_LIMIT: 370 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit); 371 ret = pmbus_write_word_data(client, 0, reg, word); 372 pmbus_clear_cache(client); 373 break; 374 case PMBUS_IIN_OC_WARN_LIMIT: 375 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit); 376 ret = pmbus_write_word_data(client, 0, 377 LM25066_MFR_IIN_OC_WARN_LIMIT, 378 word); 379 pmbus_clear_cache(client); 380 break; 381 case PMBUS_PIN_OP_WARN_LIMIT: 382 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit); 383 ret = pmbus_write_word_data(client, 0, 384 LM25066_MFR_PIN_OP_WARN_LIMIT, 385 word); 386 pmbus_clear_cache(client); 387 break; 388 case PMBUS_VIRT_VMON_UV_WARN_LIMIT: 389 /* Adjust from VIN coefficients (for LM25056) */ 390 word = DIV_ROUND_CLOSEST((int)word * 6140, 293); 391 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit); 392 ret = pmbus_write_word_data(client, 0, 393 LM25056_VAUX_UV_WARN_LIMIT, word); 394 pmbus_clear_cache(client); 395 break; 396 case PMBUS_VIRT_VMON_OV_WARN_LIMIT: 397 /* Adjust from VIN coefficients (for LM25056) */ 398 word = DIV_ROUND_CLOSEST((int)word * 6140, 293); 399 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit); 400 ret = pmbus_write_word_data(client, 0, 401 LM25056_VAUX_OV_WARN_LIMIT, word); 402 pmbus_clear_cache(client); 403 break; 404 case PMBUS_VIRT_RESET_PIN_HISTORY: 405 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK); 406 break; 407 case PMBUS_VIRT_SAMPLES: 408 word = clamp_val(word, 1, LM25066_SAMPLES_FOR_AVG_MAX); 409 ret = pmbus_write_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG, 410 ilog2(word)); 411 break; 412 default: 413 ret = -ENODATA; 414 break; 415 } 416 return ret; 417 } 418 419 static int lm25066_probe(struct i2c_client *client, 420 const struct i2c_device_id *id) 421 { 422 int config; 423 struct lm25066_data *data; 424 struct pmbus_driver_info *info; 425 struct __coeff *coeff; 426 427 if (!i2c_check_functionality(client->adapter, 428 I2C_FUNC_SMBUS_READ_BYTE_DATA)) 429 return -ENODEV; 430 431 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data), 432 GFP_KERNEL); 433 if (!data) 434 return -ENOMEM; 435 436 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP); 437 if (config < 0) 438 return config; 439 440 data->id = id->driver_data; 441 info = &data->info; 442 443 info->pages = 1; 444 info->format[PSC_VOLTAGE_IN] = direct; 445 info->format[PSC_VOLTAGE_OUT] = direct; 446 info->format[PSC_CURRENT_IN] = direct; 447 info->format[PSC_TEMPERATURE] = direct; 448 info->format[PSC_POWER] = direct; 449 450 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON 451 | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT 452 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_SAMPLES; 453 454 if (data->id == lm25056) { 455 info->func[0] |= PMBUS_HAVE_STATUS_VMON; 456 info->read_word_data = lm25056_read_word_data; 457 info->read_byte_data = lm25056_read_byte_data; 458 data->rlimit = 0x0fff; 459 } else { 460 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; 461 info->read_word_data = lm25066_read_word_data; 462 data->rlimit = 0x0fff; 463 } 464 info->write_word_data = lm25066_write_word_data; 465 466 coeff = &lm25066_coeff[data->id][0]; 467 info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m; 468 info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b; 469 info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R; 470 info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m; 471 info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b; 472 info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R; 473 info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m; 474 info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b; 475 info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R; 476 info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R; 477 info->R[PSC_POWER] = coeff[PSC_POWER].R; 478 if (config & LM25066_DEV_SETUP_CL) { 479 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m; 480 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b; 481 info->m[PSC_POWER] = coeff[PSC_POWER_L].m; 482 info->b[PSC_POWER] = coeff[PSC_POWER_L].b; 483 } else { 484 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m; 485 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b; 486 info->m[PSC_POWER] = coeff[PSC_POWER].m; 487 info->b[PSC_POWER] = coeff[PSC_POWER].b; 488 } 489 490 return pmbus_do_probe(client, id, info); 491 } 492 493 static const struct i2c_device_id lm25066_id[] = { 494 {"lm25056", lm25056}, 495 {"lm25066", lm25066}, 496 {"lm5064", lm5064}, 497 {"lm5066", lm5066}, 498 {"lm5066i", lm5066i}, 499 { } 500 }; 501 502 MODULE_DEVICE_TABLE(i2c, lm25066_id); 503 504 /* This is the driver that will be inserted */ 505 static struct i2c_driver lm25066_driver = { 506 .driver = { 507 .name = "lm25066", 508 }, 509 .probe = lm25066_probe, 510 .remove = pmbus_do_remove, 511 .id_table = lm25066_id, 512 }; 513 514 module_i2c_driver(lm25066_driver); 515 516 MODULE_AUTHOR("Guenter Roeck"); 517 MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips"); 518 MODULE_LICENSE("GPL"); 519