1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller 4 * and Digital Power Monitor 5 * 6 * Copyright (c) 2011 Ericsson AB. 7 * Copyright (c) 2018 Guenter Roeck 8 */ 9 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/bitops.h> 17 #include <linux/bitfield.h> 18 #include <linux/log2.h> 19 #include "pmbus.h" 20 21 enum chips { adm1075, adm1272, adm1273, adm1275, adm1276, adm1278, adm1281, 22 adm1293, adm1294, sq24905c }; 23 24 #define ADM1275_MFR_STATUS_IOUT_WARN2 BIT(0) 25 #define ADM1293_MFR_STATUS_VAUX_UV_WARN BIT(5) 26 #define ADM1293_MFR_STATUS_VAUX_OV_WARN BIT(6) 27 28 #define ADM1275_PEAK_IOUT 0xd0 29 #define ADM1275_PEAK_VIN 0xd1 30 #define ADM1275_PEAK_VOUT 0xd2 31 #define ADM1275_PMON_CONTROL 0xd3 32 #define ADM1275_PMON_CONFIG 0xd4 33 34 #define ADM1275_CONVERT_EN BIT(0) 35 36 #define ADM1275_VIN_VOUT_SELECT BIT(6) 37 #define ADM1275_VRANGE BIT(5) 38 #define ADM1075_IRANGE_50 BIT(4) 39 #define ADM1075_IRANGE_25 BIT(3) 40 #define ADM1075_IRANGE_MASK (BIT(3) | BIT(4)) 41 42 #define ADM1272_IRANGE BIT(0) 43 44 #define ADM1278_TSFILT BIT(15) 45 #define ADM1278_TEMP1_EN BIT(3) 46 #define ADM1278_VIN_EN BIT(2) 47 #define ADM1278_VOUT_EN BIT(1) 48 49 #define ADM1278_PMON_DEFCONFIG (ADM1278_VOUT_EN | ADM1278_TEMP1_EN | ADM1278_TSFILT) 50 51 #define ADM1293_IRANGE_25 0 52 #define ADM1293_IRANGE_50 BIT(6) 53 #define ADM1293_IRANGE_100 BIT(7) 54 #define ADM1293_IRANGE_200 (BIT(6) | BIT(7)) 55 #define ADM1293_IRANGE_MASK (BIT(6) | BIT(7)) 56 57 #define ADM1293_VIN_SEL_012 BIT(2) 58 #define ADM1293_VIN_SEL_074 BIT(3) 59 #define ADM1293_VIN_SEL_210 (BIT(2) | BIT(3)) 60 #define ADM1293_VIN_SEL_MASK (BIT(2) | BIT(3)) 61 62 #define ADM1293_VAUX_EN BIT(1) 63 64 #define ADM1278_PEAK_TEMP 0xd7 65 #define ADM1275_IOUT_WARN2_LIMIT 0xd7 66 #define ADM1275_DEVICE_CONFIG 0xd8 67 68 #define ADM1275_IOUT_WARN2_SELECT BIT(4) 69 70 #define ADM1276_PEAK_PIN 0xda 71 #define ADM1075_READ_VAUX 0xdd 72 #define ADM1075_VAUX_OV_WARN_LIMIT 0xde 73 #define ADM1075_VAUX_UV_WARN_LIMIT 0xdf 74 #define ADM1293_IOUT_MIN 0xe3 75 #define ADM1293_PIN_MIN 0xe4 76 #define ADM1075_VAUX_STATUS 0xf6 77 78 #define ADM1075_VAUX_OV_WARN BIT(7) 79 #define ADM1075_VAUX_UV_WARN BIT(6) 80 81 #define ADM1275_VI_AVG_SHIFT 0 82 #define ADM1275_VI_AVG_MASK GENMASK(ADM1275_VI_AVG_SHIFT + 2, \ 83 ADM1275_VI_AVG_SHIFT) 84 #define ADM1275_SAMPLES_AVG_MAX 128 85 86 #define ADM1278_PWR_AVG_SHIFT 11 87 #define ADM1278_PWR_AVG_MASK GENMASK(ADM1278_PWR_AVG_SHIFT + 2, \ 88 ADM1278_PWR_AVG_SHIFT) 89 #define ADM1278_VI_AVG_SHIFT 8 90 #define ADM1278_VI_AVG_MASK GENMASK(ADM1278_VI_AVG_SHIFT + 2, \ 91 ADM1278_VI_AVG_SHIFT) 92 93 struct adm1275_data { 94 int id; 95 bool have_oc_fault; 96 bool have_uc_fault; 97 bool have_vout; 98 bool have_vaux_status; 99 bool have_mfr_vaux_status; 100 bool have_iout_min; 101 bool have_pin_min; 102 bool have_pin_max; 103 bool have_temp_max; 104 bool have_power_sampling; 105 struct pmbus_driver_info info; 106 }; 107 108 #define to_adm1275_data(x) container_of(x, struct adm1275_data, info) 109 110 struct coefficients { 111 s16 m; 112 s16 b; 113 s16 R; 114 }; 115 116 static const struct coefficients adm1075_coefficients[] = { 117 [0] = { 27169, 0, -1 }, /* voltage */ 118 [1] = { 806, 20475, -1 }, /* current, irange25 */ 119 [2] = { 404, 20475, -1 }, /* current, irange50 */ 120 [3] = { 8549, 0, -1 }, /* power, irange25 */ 121 [4] = { 4279, 0, -1 }, /* power, irange50 */ 122 }; 123 124 static const struct coefficients adm1272_coefficients[] = { 125 [0] = { 6770, 0, -2 }, /* voltage, vrange 60V */ 126 [1] = { 4062, 0, -2 }, /* voltage, vrange 100V */ 127 [2] = { 1326, 20480, -1 }, /* current, vsense range 15mV */ 128 [3] = { 663, 20480, -1 }, /* current, vsense range 30mV */ 129 [4] = { 3512, 0, -2 }, /* power, vrange 60V, irange 15mV */ 130 [5] = { 21071, 0, -3 }, /* power, vrange 100V, irange 15mV */ 131 [6] = { 17561, 0, -3 }, /* power, vrange 60V, irange 30mV */ 132 [7] = { 10535, 0, -3 }, /* power, vrange 100V, irange 30mV */ 133 [8] = { 42, 31871, -1 }, /* temperature */ 134 135 }; 136 137 static const struct coefficients adm1275_coefficients[] = { 138 [0] = { 19199, 0, -2 }, /* voltage, vrange set */ 139 [1] = { 6720, 0, -1 }, /* voltage, vrange not set */ 140 [2] = { 807, 20475, -1 }, /* current */ 141 }; 142 143 static const struct coefficients adm1276_coefficients[] = { 144 [0] = { 19199, 0, -2 }, /* voltage, vrange set */ 145 [1] = { 6720, 0, -1 }, /* voltage, vrange not set */ 146 [2] = { 807, 20475, -1 }, /* current */ 147 [3] = { 6043, 0, -2 }, /* power, vrange set */ 148 [4] = { 2115, 0, -1 }, /* power, vrange not set */ 149 }; 150 151 static const struct coefficients adm1278_coefficients[] = { 152 [0] = { 19599, 0, -2 }, /* voltage */ 153 [1] = { 800, 20475, -1 }, /* current */ 154 [2] = { 6123, 0, -2 }, /* power */ 155 [3] = { 42, 31880, -1 }, /* temperature */ 156 }; 157 158 static const struct coefficients adm1293_coefficients[] = { 159 [0] = { 3333, -1, 0 }, /* voltage, vrange 1.2V */ 160 [1] = { 5552, -5, -1 }, /* voltage, vrange 7.4V */ 161 [2] = { 19604, -50, -2 }, /* voltage, vrange 21V */ 162 [3] = { 8000, -100, -2 }, /* current, irange25 */ 163 [4] = { 4000, -100, -2 }, /* current, irange50 */ 164 [5] = { 20000, -1000, -3 }, /* current, irange100 */ 165 [6] = { 10000, -1000, -3 }, /* current, irange200 */ 166 [7] = { 10417, 0, -1 }, /* power, 1.2V, irange25 */ 167 [8] = { 5208, 0, -1 }, /* power, 1.2V, irange50 */ 168 [9] = { 26042, 0, -2 }, /* power, 1.2V, irange100 */ 169 [10] = { 13021, 0, -2 }, /* power, 1.2V, irange200 */ 170 [11] = { 17351, 0, -2 }, /* power, 7.4V, irange25 */ 171 [12] = { 8676, 0, -2 }, /* power, 7.4V, irange50 */ 172 [13] = { 4338, 0, -2 }, /* power, 7.4V, irange100 */ 173 [14] = { 21689, 0, -3 }, /* power, 7.4V, irange200 */ 174 [15] = { 6126, 0, -2 }, /* power, 21V, irange25 */ 175 [16] = { 30631, 0, -3 }, /* power, 21V, irange50 */ 176 [17] = { 15316, 0, -3 }, /* power, 21V, irange100 */ 177 [18] = { 7658, 0, -3 }, /* power, 21V, irange200 */ 178 }; 179 180 static int adm1275_read_samples(const struct adm1275_data *data, 181 struct i2c_client *client, bool is_power) 182 { 183 int shift, ret; 184 u16 mask; 185 186 /* 187 * The PMON configuration register is a 16-bit register only on chips 188 * supporting power average sampling. On other chips it is an 8-bit 189 * register. 190 */ 191 if (data->have_power_sampling) { 192 ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG); 193 mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK; 194 shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT; 195 } else { 196 ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG); 197 mask = ADM1275_VI_AVG_MASK; 198 shift = ADM1275_VI_AVG_SHIFT; 199 } 200 if (ret < 0) 201 return ret; 202 203 return (ret & mask) >> shift; 204 } 205 206 static int adm1275_write_pmon_config(const struct adm1275_data *data, 207 struct i2c_client *client, u16 word) 208 { 209 int ret, ret2; 210 211 ret = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONTROL, 0); 212 if (ret) 213 return ret; 214 215 if (data->have_power_sampling) 216 ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG, 217 word); 218 else 219 ret = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONFIG, 220 word); 221 222 /* 223 * We still want to re-enable conversions if writing into 224 * ADM1275_PMON_CONFIG failed. 225 */ 226 ret2 = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONTROL, 227 ADM1275_CONVERT_EN); 228 if (!ret) 229 ret = ret2; 230 231 return ret; 232 } 233 234 static int adm1275_write_samples(const struct adm1275_data *data, 235 struct i2c_client *client, 236 bool is_power, u16 word) 237 { 238 int shift, ret; 239 u16 mask; 240 241 if (data->have_power_sampling) { 242 ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG); 243 mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK; 244 shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT; 245 } else { 246 ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG); 247 mask = ADM1275_VI_AVG_MASK; 248 shift = ADM1275_VI_AVG_SHIFT; 249 } 250 if (ret < 0) 251 return ret; 252 253 word = (ret & ~mask) | ((word << shift) & mask); 254 255 return adm1275_write_pmon_config(data, client, word); 256 } 257 258 static int adm1275_read_word_data(struct i2c_client *client, int page, 259 int phase, int reg) 260 { 261 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 262 const struct adm1275_data *data = to_adm1275_data(info); 263 int ret = 0; 264 265 if (page > 0) 266 return -ENXIO; 267 268 switch (reg) { 269 case PMBUS_IOUT_UC_FAULT_LIMIT: 270 if (!data->have_uc_fault) 271 return -ENXIO; 272 ret = pmbus_read_word_data(client, 0, 0xff, 273 ADM1275_IOUT_WARN2_LIMIT); 274 break; 275 case PMBUS_IOUT_OC_FAULT_LIMIT: 276 if (!data->have_oc_fault) 277 return -ENXIO; 278 ret = pmbus_read_word_data(client, 0, 0xff, 279 ADM1275_IOUT_WARN2_LIMIT); 280 break; 281 case PMBUS_VOUT_OV_WARN_LIMIT: 282 if (data->have_vout) 283 return -ENODATA; 284 ret = pmbus_read_word_data(client, 0, 0xff, 285 ADM1075_VAUX_OV_WARN_LIMIT); 286 break; 287 case PMBUS_VOUT_UV_WARN_LIMIT: 288 if (data->have_vout) 289 return -ENODATA; 290 ret = pmbus_read_word_data(client, 0, 0xff, 291 ADM1075_VAUX_UV_WARN_LIMIT); 292 break; 293 case PMBUS_READ_VOUT: 294 if (data->have_vout) 295 return -ENODATA; 296 ret = pmbus_read_word_data(client, 0, 0xff, 297 ADM1075_READ_VAUX); 298 break; 299 case PMBUS_VIRT_READ_IOUT_MIN: 300 if (!data->have_iout_min) 301 return -ENXIO; 302 ret = pmbus_read_word_data(client, 0, 0xff, 303 ADM1293_IOUT_MIN); 304 break; 305 case PMBUS_VIRT_READ_IOUT_MAX: 306 ret = pmbus_read_word_data(client, 0, 0xff, 307 ADM1275_PEAK_IOUT); 308 break; 309 case PMBUS_VIRT_READ_VOUT_MAX: 310 ret = pmbus_read_word_data(client, 0, 0xff, 311 ADM1275_PEAK_VOUT); 312 break; 313 case PMBUS_VIRT_READ_VIN_MAX: 314 ret = pmbus_read_word_data(client, 0, 0xff, 315 ADM1275_PEAK_VIN); 316 break; 317 case PMBUS_VIRT_READ_PIN_MIN: 318 if (!data->have_pin_min) 319 return -ENXIO; 320 ret = pmbus_read_word_data(client, 0, 0xff, 321 ADM1293_PIN_MIN); 322 break; 323 case PMBUS_VIRT_READ_PIN_MAX: 324 if (!data->have_pin_max) 325 return -ENXIO; 326 ret = pmbus_read_word_data(client, 0, 0xff, 327 ADM1276_PEAK_PIN); 328 break; 329 case PMBUS_VIRT_READ_TEMP_MAX: 330 if (!data->have_temp_max) 331 return -ENXIO; 332 ret = pmbus_read_word_data(client, 0, 0xff, 333 ADM1278_PEAK_TEMP); 334 break; 335 case PMBUS_VIRT_RESET_IOUT_HISTORY: 336 case PMBUS_VIRT_RESET_VOUT_HISTORY: 337 case PMBUS_VIRT_RESET_VIN_HISTORY: 338 break; 339 case PMBUS_VIRT_RESET_PIN_HISTORY: 340 if (!data->have_pin_max) 341 return -ENXIO; 342 break; 343 case PMBUS_VIRT_RESET_TEMP_HISTORY: 344 if (!data->have_temp_max) 345 return -ENXIO; 346 break; 347 case PMBUS_VIRT_POWER_SAMPLES: 348 if (!data->have_power_sampling) 349 return -ENXIO; 350 ret = adm1275_read_samples(data, client, true); 351 if (ret < 0) 352 break; 353 ret = BIT(ret); 354 break; 355 case PMBUS_VIRT_IN_SAMPLES: 356 case PMBUS_VIRT_CURR_SAMPLES: 357 ret = adm1275_read_samples(data, client, false); 358 if (ret < 0) 359 break; 360 ret = BIT(ret); 361 break; 362 default: 363 ret = -ENODATA; 364 break; 365 } 366 return ret; 367 } 368 369 static int adm1275_write_word_data(struct i2c_client *client, int page, int reg, 370 u16 word) 371 { 372 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 373 const struct adm1275_data *data = to_adm1275_data(info); 374 int ret; 375 376 if (page > 0) 377 return -ENXIO; 378 379 switch (reg) { 380 case PMBUS_IOUT_UC_FAULT_LIMIT: 381 case PMBUS_IOUT_OC_FAULT_LIMIT: 382 ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT, 383 word); 384 break; 385 case PMBUS_VIRT_RESET_IOUT_HISTORY: 386 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0); 387 if (!ret && data->have_iout_min) 388 ret = pmbus_write_word_data(client, 0, 389 ADM1293_IOUT_MIN, 0); 390 break; 391 case PMBUS_VIRT_RESET_VOUT_HISTORY: 392 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0); 393 break; 394 case PMBUS_VIRT_RESET_VIN_HISTORY: 395 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0); 396 break; 397 case PMBUS_VIRT_RESET_PIN_HISTORY: 398 ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0); 399 if (!ret && data->have_pin_min) 400 ret = pmbus_write_word_data(client, 0, 401 ADM1293_PIN_MIN, 0); 402 break; 403 case PMBUS_VIRT_RESET_TEMP_HISTORY: 404 ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0); 405 break; 406 case PMBUS_VIRT_POWER_SAMPLES: 407 if (!data->have_power_sampling) 408 return -ENXIO; 409 word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX); 410 ret = adm1275_write_samples(data, client, true, ilog2(word)); 411 break; 412 case PMBUS_VIRT_IN_SAMPLES: 413 case PMBUS_VIRT_CURR_SAMPLES: 414 word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX); 415 ret = adm1275_write_samples(data, client, false, ilog2(word)); 416 break; 417 default: 418 ret = -ENODATA; 419 break; 420 } 421 return ret; 422 } 423 424 static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg) 425 { 426 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 427 const struct adm1275_data *data = to_adm1275_data(info); 428 int mfr_status, ret; 429 430 if (page > 0) 431 return -ENXIO; 432 433 switch (reg) { 434 case PMBUS_STATUS_IOUT: 435 ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT); 436 if (ret < 0) 437 break; 438 if (!data->have_oc_fault && !data->have_uc_fault) 439 break; 440 mfr_status = pmbus_read_byte_data(client, page, 441 PMBUS_STATUS_MFR_SPECIFIC); 442 if (mfr_status < 0) 443 return mfr_status; 444 if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) { 445 ret |= data->have_oc_fault ? 446 PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT; 447 } 448 break; 449 case PMBUS_STATUS_VOUT: 450 if (data->have_vout) 451 return -ENODATA; 452 ret = 0; 453 if (data->have_vaux_status) { 454 mfr_status = pmbus_read_byte_data(client, 0, 455 ADM1075_VAUX_STATUS); 456 if (mfr_status < 0) 457 return mfr_status; 458 if (mfr_status & ADM1075_VAUX_OV_WARN) 459 ret |= PB_VOLTAGE_OV_WARNING; 460 if (mfr_status & ADM1075_VAUX_UV_WARN) 461 ret |= PB_VOLTAGE_UV_WARNING; 462 } else if (data->have_mfr_vaux_status) { 463 mfr_status = pmbus_read_byte_data(client, page, 464 PMBUS_STATUS_MFR_SPECIFIC); 465 if (mfr_status < 0) 466 return mfr_status; 467 if (mfr_status & ADM1293_MFR_STATUS_VAUX_OV_WARN) 468 ret |= PB_VOLTAGE_OV_WARNING; 469 if (mfr_status & ADM1293_MFR_STATUS_VAUX_UV_WARN) 470 ret |= PB_VOLTAGE_UV_WARNING; 471 } 472 break; 473 default: 474 ret = -ENODATA; 475 break; 476 } 477 return ret; 478 } 479 480 static const struct i2c_device_id adm1275_id[] = { 481 { "adm1075", adm1075 }, 482 { "adm1272", adm1272 }, 483 { "adm1273", adm1273 }, 484 { "adm1275", adm1275 }, 485 { "adm1276", adm1276 }, 486 { "adm1278", adm1278 }, 487 { "adm1281", adm1281 }, 488 { "adm1293", adm1293 }, 489 { "adm1294", adm1294 }, 490 { "mc09c", sq24905c }, 491 { } 492 }; 493 MODULE_DEVICE_TABLE(i2c, adm1275_id); 494 495 /* Enable VOUT & TEMP1 if not enabled (disabled by default) */ 496 static int adm1275_enable_vout_temp(struct adm1275_data *data, 497 struct i2c_client *client, int config) 498 { 499 int ret; 500 501 if ((config & ADM1278_PMON_DEFCONFIG) != ADM1278_PMON_DEFCONFIG) { 502 config |= ADM1278_PMON_DEFCONFIG; 503 ret = adm1275_write_pmon_config(data, client, config); 504 if (ret < 0) { 505 dev_err(&client->dev, "Failed to enable VOUT/TEMP1 monitoring\n"); 506 return ret; 507 } 508 } 509 return 0; 510 } 511 512 static int adm1275_probe(struct i2c_client *client) 513 { 514 s32 (*config_read_fn)(const struct i2c_client *client, u8 reg); 515 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; 516 int config, device_config; 517 int ret; 518 struct pmbus_driver_info *info; 519 struct adm1275_data *data; 520 const struct i2c_device_id *mid; 521 const struct coefficients *coefficients; 522 int vindex = -1, voindex = -1, cindex = -1, pindex = -1; 523 int tindex = -1; 524 u32 shunt; 525 u32 avg; 526 527 if (!i2c_check_functionality(client->adapter, 528 I2C_FUNC_SMBUS_READ_BYTE_DATA 529 | I2C_FUNC_SMBUS_BLOCK_DATA)) 530 return -ENODEV; 531 532 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer); 533 if (ret < 0) { 534 dev_err(&client->dev, "Failed to read Manufacturer ID\n"); 535 return ret; 536 } 537 if ((ret != 3 || strncmp(block_buffer, "ADI", 3)) && 538 (ret != 2 || strncmp(block_buffer, "SY", 2))) { 539 dev_err(&client->dev, "Unsupported Manufacturer ID\n"); 540 return -ENODEV; 541 } 542 543 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer); 544 if (ret < 0) { 545 dev_err(&client->dev, "Failed to read Manufacturer Model\n"); 546 return ret; 547 } 548 for (mid = adm1275_id; mid->name[0]; mid++) { 549 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name))) 550 break; 551 } 552 if (!mid->name[0]) { 553 dev_err(&client->dev, "Unsupported device\n"); 554 return -ENODEV; 555 } 556 557 if (strcmp(client->name, mid->name) != 0) 558 dev_notice(&client->dev, 559 "Device mismatch: Configured %s, detected %s\n", 560 client->name, mid->name); 561 562 if (mid->driver_data == adm1272 || mid->driver_data == adm1273 || 563 mid->driver_data == adm1278 || mid->driver_data == adm1281 || 564 mid->driver_data == adm1293 || mid->driver_data == adm1294 || 565 mid->driver_data == sq24905c) 566 config_read_fn = i2c_smbus_read_word_data; 567 else 568 config_read_fn = i2c_smbus_read_byte_data; 569 config = config_read_fn(client, ADM1275_PMON_CONFIG); 570 if (config < 0) 571 return config; 572 573 device_config = config_read_fn(client, ADM1275_DEVICE_CONFIG); 574 if (device_config < 0) 575 return device_config; 576 577 data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data), 578 GFP_KERNEL); 579 if (!data) 580 return -ENOMEM; 581 582 if (of_property_read_u32(client->dev.of_node, 583 "shunt-resistor-micro-ohms", &shunt)) 584 shunt = 1000; /* 1 mOhm if not set via DT */ 585 586 if (shunt == 0) 587 return -EINVAL; 588 589 data->id = mid->driver_data; 590 591 info = &data->info; 592 593 info->pages = 1; 594 info->format[PSC_VOLTAGE_IN] = direct; 595 info->format[PSC_VOLTAGE_OUT] = direct; 596 info->format[PSC_CURRENT_OUT] = direct; 597 info->format[PSC_POWER] = direct; 598 info->format[PSC_TEMPERATURE] = direct; 599 info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | 600 PMBUS_HAVE_SAMPLES; 601 602 info->read_word_data = adm1275_read_word_data; 603 info->read_byte_data = adm1275_read_byte_data; 604 info->write_word_data = adm1275_write_word_data; 605 606 switch (data->id) { 607 case adm1075: 608 if (device_config & ADM1275_IOUT_WARN2_SELECT) 609 data->have_oc_fault = true; 610 else 611 data->have_uc_fault = true; 612 data->have_pin_max = true; 613 data->have_vaux_status = true; 614 615 coefficients = adm1075_coefficients; 616 vindex = 0; 617 switch (config & ADM1075_IRANGE_MASK) { 618 case ADM1075_IRANGE_25: 619 cindex = 1; 620 pindex = 3; 621 break; 622 case ADM1075_IRANGE_50: 623 cindex = 2; 624 pindex = 4; 625 break; 626 default: 627 dev_err(&client->dev, "Invalid input current range"); 628 break; 629 } 630 631 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN 632 | PMBUS_HAVE_STATUS_INPUT; 633 if (config & ADM1275_VIN_VOUT_SELECT) 634 info->func[0] |= 635 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; 636 break; 637 case adm1272: 638 case adm1273: 639 data->have_vout = true; 640 data->have_pin_max = true; 641 data->have_temp_max = true; 642 data->have_power_sampling = true; 643 644 coefficients = adm1272_coefficients; 645 vindex = (config & ADM1275_VRANGE) ? 1 : 0; 646 cindex = (config & ADM1272_IRANGE) ? 3 : 2; 647 /* pindex depends on the combination of the above */ 648 switch (config & (ADM1275_VRANGE | ADM1272_IRANGE)) { 649 case 0: 650 default: 651 pindex = 4; 652 break; 653 case ADM1275_VRANGE: 654 pindex = 5; 655 break; 656 case ADM1272_IRANGE: 657 pindex = 6; 658 break; 659 case ADM1275_VRANGE | ADM1272_IRANGE: 660 pindex = 7; 661 break; 662 } 663 tindex = 8; 664 665 info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT | 666 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | 667 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP; 668 669 ret = adm1275_enable_vout_temp(data, client, config); 670 if (ret) 671 return ret; 672 673 if (config & ADM1278_VIN_EN) 674 info->func[0] |= PMBUS_HAVE_VIN; 675 break; 676 case adm1275: 677 if (device_config & ADM1275_IOUT_WARN2_SELECT) 678 data->have_oc_fault = true; 679 else 680 data->have_uc_fault = true; 681 data->have_vout = true; 682 683 coefficients = adm1275_coefficients; 684 vindex = (config & ADM1275_VRANGE) ? 0 : 1; 685 cindex = 2; 686 687 if (config & ADM1275_VIN_VOUT_SELECT) 688 info->func[0] |= 689 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; 690 else 691 info->func[0] |= 692 PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT; 693 break; 694 case adm1276: 695 if (device_config & ADM1275_IOUT_WARN2_SELECT) 696 data->have_oc_fault = true; 697 else 698 data->have_uc_fault = true; 699 data->have_vout = true; 700 data->have_pin_max = true; 701 702 coefficients = adm1276_coefficients; 703 vindex = (config & ADM1275_VRANGE) ? 0 : 1; 704 cindex = 2; 705 pindex = (config & ADM1275_VRANGE) ? 3 : 4; 706 707 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN 708 | PMBUS_HAVE_STATUS_INPUT; 709 if (config & ADM1275_VIN_VOUT_SELECT) 710 info->func[0] |= 711 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; 712 break; 713 case adm1278: 714 case adm1281: 715 case sq24905c: 716 data->have_vout = true; 717 data->have_pin_max = true; 718 data->have_temp_max = true; 719 data->have_power_sampling = true; 720 721 coefficients = adm1278_coefficients; 722 vindex = 0; 723 cindex = 1; 724 pindex = 2; 725 tindex = 3; 726 727 info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT | 728 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | 729 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP; 730 731 ret = adm1275_enable_vout_temp(data, client, config); 732 if (ret) 733 return ret; 734 735 if (config & ADM1278_VIN_EN) 736 info->func[0] |= PMBUS_HAVE_VIN; 737 break; 738 case adm1293: 739 case adm1294: 740 data->have_iout_min = true; 741 data->have_pin_min = true; 742 data->have_pin_max = true; 743 data->have_mfr_vaux_status = true; 744 data->have_power_sampling = true; 745 746 coefficients = adm1293_coefficients; 747 748 voindex = 0; 749 switch (config & ADM1293_VIN_SEL_MASK) { 750 case ADM1293_VIN_SEL_012: /* 1.2V */ 751 vindex = 0; 752 break; 753 case ADM1293_VIN_SEL_074: /* 7.4V */ 754 vindex = 1; 755 break; 756 case ADM1293_VIN_SEL_210: /* 21V */ 757 vindex = 2; 758 break; 759 default: /* disabled */ 760 break; 761 } 762 763 switch (config & ADM1293_IRANGE_MASK) { 764 case ADM1293_IRANGE_25: 765 cindex = 3; 766 break; 767 case ADM1293_IRANGE_50: 768 cindex = 4; 769 break; 770 case ADM1293_IRANGE_100: 771 cindex = 5; 772 break; 773 case ADM1293_IRANGE_200: 774 cindex = 6; 775 break; 776 } 777 778 if (vindex >= 0) 779 pindex = 7 + vindex * 4 + (cindex - 3); 780 781 if (config & ADM1293_VAUX_EN) 782 info->func[0] |= 783 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; 784 785 info->func[0] |= PMBUS_HAVE_PIN | 786 PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT; 787 788 break; 789 default: 790 dev_err(&client->dev, "Unsupported device\n"); 791 return -ENODEV; 792 } 793 794 if (data->have_power_sampling && 795 of_property_read_u32(client->dev.of_node, 796 "adi,power-sample-average", &avg) == 0) { 797 if (!avg || avg > ADM1275_SAMPLES_AVG_MAX || 798 BIT(__fls(avg)) != avg) { 799 dev_err(&client->dev, 800 "Invalid number of power samples"); 801 return -EINVAL; 802 } 803 ret = adm1275_write_samples(data, client, true, ilog2(avg)); 804 if (ret < 0) { 805 dev_err(&client->dev, 806 "Setting power sample averaging failed with error %d", 807 ret); 808 return ret; 809 } 810 } 811 812 if (of_property_read_u32(client->dev.of_node, 813 "adi,volt-curr-sample-average", &avg) == 0) { 814 if (!avg || avg > ADM1275_SAMPLES_AVG_MAX || 815 BIT(__fls(avg)) != avg) { 816 dev_err(&client->dev, 817 "Invalid number of voltage/current samples"); 818 return -EINVAL; 819 } 820 ret = adm1275_write_samples(data, client, false, ilog2(avg)); 821 if (ret < 0) { 822 dev_err(&client->dev, 823 "Setting voltage and current sample averaging failed with error %d", 824 ret); 825 return ret; 826 } 827 } 828 829 if (voindex < 0) 830 voindex = vindex; 831 if (vindex >= 0) { 832 info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m; 833 info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b; 834 info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R; 835 } 836 if (voindex >= 0) { 837 info->m[PSC_VOLTAGE_OUT] = coefficients[voindex].m; 838 info->b[PSC_VOLTAGE_OUT] = coefficients[voindex].b; 839 info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R; 840 } 841 if (cindex >= 0) { 842 /* Scale current with sense resistor value */ 843 info->m[PSC_CURRENT_OUT] = 844 coefficients[cindex].m * shunt / 1000; 845 info->b[PSC_CURRENT_OUT] = coefficients[cindex].b; 846 info->R[PSC_CURRENT_OUT] = coefficients[cindex].R; 847 } 848 if (pindex >= 0) { 849 info->m[PSC_POWER] = 850 coefficients[pindex].m * shunt / 1000; 851 info->b[PSC_POWER] = coefficients[pindex].b; 852 info->R[PSC_POWER] = coefficients[pindex].R; 853 } 854 if (tindex >= 0) { 855 info->m[PSC_TEMPERATURE] = coefficients[tindex].m; 856 info->b[PSC_TEMPERATURE] = coefficients[tindex].b; 857 info->R[PSC_TEMPERATURE] = coefficients[tindex].R; 858 } 859 860 return pmbus_do_probe(client, info); 861 } 862 863 static struct i2c_driver adm1275_driver = { 864 .driver = { 865 .name = "adm1275", 866 }, 867 .probe = adm1275_probe, 868 .id_table = adm1275_id, 869 }; 870 871 module_i2c_driver(adm1275_driver); 872 873 MODULE_AUTHOR("Guenter Roeck"); 874 MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles"); 875 MODULE_LICENSE("GPL"); 876 MODULE_IMPORT_NS("PMBUS"); 877