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