1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Hardware monitoring driver for PMBus devices 4 * 5 * Copyright (c) 2010, 2011 Ericsson AB. 6 * Copyright (c) 2012 Guenter Roeck 7 */ 8 9 #include <linux/debugfs.h> 10 #include <linux/kernel.h> 11 #include <linux/math64.h> 12 #include <linux/module.h> 13 #include <linux/init.h> 14 #include <linux/err.h> 15 #include <linux/slab.h> 16 #include <linux/i2c.h> 17 #include <linux/hwmon.h> 18 #include <linux/hwmon-sysfs.h> 19 #include <linux/pmbus.h> 20 #include <linux/regulator/driver.h> 21 #include <linux/regulator/machine.h> 22 #include <linux/of.h> 23 #include <linux/thermal.h> 24 #include "pmbus.h" 25 26 /* 27 * Number of additional attribute pointers to allocate 28 * with each call to krealloc 29 */ 30 #define PMBUS_ATTR_ALLOC_SIZE 32 31 #define PMBUS_NAME_SIZE 24 32 33 struct pmbus_sensor { 34 struct pmbus_sensor *next; 35 char name[PMBUS_NAME_SIZE]; /* sysfs sensor name */ 36 struct device_attribute attribute; 37 u8 page; /* page number */ 38 u8 phase; /* phase number, 0xff for all phases */ 39 u16 reg; /* register */ 40 enum pmbus_sensor_classes class; /* sensor class */ 41 bool update; /* runtime sensor update needed */ 42 bool convert; /* Whether or not to apply linear/vid/direct */ 43 int data; /* Sensor data. 44 Negative if there was a read error */ 45 }; 46 #define to_pmbus_sensor(_attr) \ 47 container_of(_attr, struct pmbus_sensor, attribute) 48 49 struct pmbus_boolean { 50 char name[PMBUS_NAME_SIZE]; /* sysfs boolean name */ 51 struct sensor_device_attribute attribute; 52 struct pmbus_sensor *s1; 53 struct pmbus_sensor *s2; 54 }; 55 #define to_pmbus_boolean(_attr) \ 56 container_of(_attr, struct pmbus_boolean, attribute) 57 58 struct pmbus_label { 59 char name[PMBUS_NAME_SIZE]; /* sysfs label name */ 60 struct device_attribute attribute; 61 char label[PMBUS_NAME_SIZE]; /* label */ 62 }; 63 #define to_pmbus_label(_attr) \ 64 container_of(_attr, struct pmbus_label, attribute) 65 66 /* Macros for converting between sensor index and register/page/status mask */ 67 68 #define PB_STATUS_MASK 0xffff 69 #define PB_REG_SHIFT 16 70 #define PB_REG_MASK 0x3ff 71 #define PB_PAGE_SHIFT 26 72 #define PB_PAGE_MASK 0x3f 73 74 #define pb_reg_to_index(page, reg, mask) (((page) << PB_PAGE_SHIFT) | \ 75 ((reg) << PB_REG_SHIFT) | (mask)) 76 77 #define pb_index_to_page(index) (((index) >> PB_PAGE_SHIFT) & PB_PAGE_MASK) 78 #define pb_index_to_reg(index) (((index) >> PB_REG_SHIFT) & PB_REG_MASK) 79 #define pb_index_to_mask(index) ((index) & PB_STATUS_MASK) 80 81 struct pmbus_data { 82 struct device *dev; 83 struct device *hwmon_dev; 84 struct regulator_dev **rdevs; 85 86 u32 flags; /* from platform data */ 87 88 int exponent[PMBUS_PAGES]; 89 /* linear mode: exponent for output voltages */ 90 91 const struct pmbus_driver_info *info; 92 93 int max_attributes; 94 int num_attributes; 95 struct attribute_group group; 96 const struct attribute_group **groups; 97 struct dentry *debugfs; /* debugfs device directory */ 98 99 struct pmbus_sensor *sensors; 100 101 struct mutex update_lock; 102 103 bool has_status_word; /* device uses STATUS_WORD register */ 104 int (*read_status)(struct i2c_client *client, int page); 105 106 s16 currpage; /* current page, -1 for unknown/unset */ 107 s16 currphase; /* current phase, 0xff for all, -1 for unknown/unset */ 108 109 int vout_low[PMBUS_PAGES]; /* voltage low margin */ 110 int vout_high[PMBUS_PAGES]; /* voltage high margin */ 111 }; 112 113 struct pmbus_debugfs_entry { 114 struct i2c_client *client; 115 u8 page; 116 u8 reg; 117 }; 118 119 static const int pmbus_fan_rpm_mask[] = { 120 PB_FAN_1_RPM, 121 PB_FAN_2_RPM, 122 PB_FAN_1_RPM, 123 PB_FAN_2_RPM, 124 }; 125 126 static const int pmbus_fan_config_registers[] = { 127 PMBUS_FAN_CONFIG_12, 128 PMBUS_FAN_CONFIG_12, 129 PMBUS_FAN_CONFIG_34, 130 PMBUS_FAN_CONFIG_34 131 }; 132 133 static const int pmbus_fan_command_registers[] = { 134 PMBUS_FAN_COMMAND_1, 135 PMBUS_FAN_COMMAND_2, 136 PMBUS_FAN_COMMAND_3, 137 PMBUS_FAN_COMMAND_4, 138 }; 139 140 void pmbus_clear_cache(struct i2c_client *client) 141 { 142 struct pmbus_data *data = i2c_get_clientdata(client); 143 struct pmbus_sensor *sensor; 144 145 for (sensor = data->sensors; sensor; sensor = sensor->next) 146 sensor->data = -ENODATA; 147 } 148 EXPORT_SYMBOL_NS_GPL(pmbus_clear_cache, PMBUS); 149 150 void pmbus_set_update(struct i2c_client *client, u8 reg, bool update) 151 { 152 struct pmbus_data *data = i2c_get_clientdata(client); 153 struct pmbus_sensor *sensor; 154 155 for (sensor = data->sensors; sensor; sensor = sensor->next) 156 if (sensor->reg == reg) 157 sensor->update = update; 158 } 159 EXPORT_SYMBOL_NS_GPL(pmbus_set_update, PMBUS); 160 161 int pmbus_set_page(struct i2c_client *client, int page, int phase) 162 { 163 struct pmbus_data *data = i2c_get_clientdata(client); 164 int rv; 165 166 if (page < 0) 167 return 0; 168 169 if (!(data->info->func[page] & PMBUS_PAGE_VIRTUAL) && 170 data->info->pages > 1 && page != data->currpage) { 171 dev_dbg(&client->dev, "Want page %u, %u cached\n", page, 172 data->currpage); 173 174 rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page); 175 if (rv < 0) { 176 rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 177 page); 178 dev_dbg(&client->dev, 179 "Failed to set page %u, performed one-shot retry %s: %d\n", 180 page, rv ? "and failed" : "with success", rv); 181 if (rv < 0) 182 return rv; 183 } 184 185 rv = i2c_smbus_read_byte_data(client, PMBUS_PAGE); 186 if (rv < 0) 187 return rv; 188 189 if (rv != page) 190 return -EIO; 191 } 192 data->currpage = page; 193 194 if (data->info->phases[page] && data->currphase != phase && 195 !(data->info->func[page] & PMBUS_PHASE_VIRTUAL)) { 196 rv = i2c_smbus_write_byte_data(client, PMBUS_PHASE, 197 phase); 198 if (rv) 199 return rv; 200 } 201 data->currphase = phase; 202 203 return 0; 204 } 205 EXPORT_SYMBOL_NS_GPL(pmbus_set_page, PMBUS); 206 207 int pmbus_write_byte(struct i2c_client *client, int page, u8 value) 208 { 209 int rv; 210 211 rv = pmbus_set_page(client, page, 0xff); 212 if (rv < 0) 213 return rv; 214 215 return i2c_smbus_write_byte(client, value); 216 } 217 EXPORT_SYMBOL_NS_GPL(pmbus_write_byte, PMBUS); 218 219 /* 220 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if 221 * a device specific mapping function exists and calls it if necessary. 222 */ 223 static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value) 224 { 225 struct pmbus_data *data = i2c_get_clientdata(client); 226 const struct pmbus_driver_info *info = data->info; 227 int status; 228 229 if (info->write_byte) { 230 status = info->write_byte(client, page, value); 231 if (status != -ENODATA) 232 return status; 233 } 234 return pmbus_write_byte(client, page, value); 235 } 236 237 int pmbus_write_word_data(struct i2c_client *client, int page, u8 reg, 238 u16 word) 239 { 240 int rv; 241 242 rv = pmbus_set_page(client, page, 0xff); 243 if (rv < 0) 244 return rv; 245 246 return i2c_smbus_write_word_data(client, reg, word); 247 } 248 EXPORT_SYMBOL_NS_GPL(pmbus_write_word_data, PMBUS); 249 250 251 static int pmbus_write_virt_reg(struct i2c_client *client, int page, int reg, 252 u16 word) 253 { 254 int bit; 255 int id; 256 int rv; 257 258 switch (reg) { 259 case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4: 260 id = reg - PMBUS_VIRT_FAN_TARGET_1; 261 bit = pmbus_fan_rpm_mask[id]; 262 rv = pmbus_update_fan(client, page, id, bit, bit, word); 263 break; 264 default: 265 rv = -ENXIO; 266 break; 267 } 268 269 return rv; 270 } 271 272 /* 273 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if 274 * a device specific mapping function exists and calls it if necessary. 275 */ 276 static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg, 277 u16 word) 278 { 279 struct pmbus_data *data = i2c_get_clientdata(client); 280 const struct pmbus_driver_info *info = data->info; 281 int status; 282 283 if (info->write_word_data) { 284 status = info->write_word_data(client, page, reg, word); 285 if (status != -ENODATA) 286 return status; 287 } 288 289 if (reg >= PMBUS_VIRT_BASE) 290 return pmbus_write_virt_reg(client, page, reg, word); 291 292 return pmbus_write_word_data(client, page, reg, word); 293 } 294 295 /* 296 * _pmbus_write_byte_data() is similar to pmbus_write_byte_data(), but checks if 297 * a device specific mapping function exists and calls it if necessary. 298 */ 299 static int _pmbus_write_byte_data(struct i2c_client *client, int page, int reg, u8 value) 300 { 301 struct pmbus_data *data = i2c_get_clientdata(client); 302 const struct pmbus_driver_info *info = data->info; 303 int status; 304 305 if (info->write_byte_data) { 306 status = info->write_byte_data(client, page, reg, value); 307 if (status != -ENODATA) 308 return status; 309 } 310 return pmbus_write_byte_data(client, page, reg, value); 311 } 312 313 /* 314 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if 315 * a device specific mapping function exists and calls it if necessary. 316 */ 317 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg) 318 { 319 struct pmbus_data *data = i2c_get_clientdata(client); 320 const struct pmbus_driver_info *info = data->info; 321 int status; 322 323 if (info->read_byte_data) { 324 status = info->read_byte_data(client, page, reg); 325 if (status != -ENODATA) 326 return status; 327 } 328 return pmbus_read_byte_data(client, page, reg); 329 } 330 331 int pmbus_update_fan(struct i2c_client *client, int page, int id, 332 u8 config, u8 mask, u16 command) 333 { 334 int from; 335 int rv; 336 u8 to; 337 338 from = _pmbus_read_byte_data(client, page, 339 pmbus_fan_config_registers[id]); 340 if (from < 0) 341 return from; 342 343 to = (from & ~mask) | (config & mask); 344 if (to != from) { 345 rv = _pmbus_write_byte_data(client, page, 346 pmbus_fan_config_registers[id], to); 347 if (rv < 0) 348 return rv; 349 } 350 351 return _pmbus_write_word_data(client, page, 352 pmbus_fan_command_registers[id], command); 353 } 354 EXPORT_SYMBOL_NS_GPL(pmbus_update_fan, PMBUS); 355 356 int pmbus_read_word_data(struct i2c_client *client, int page, int phase, u8 reg) 357 { 358 int rv; 359 360 rv = pmbus_set_page(client, page, phase); 361 if (rv < 0) 362 return rv; 363 364 return i2c_smbus_read_word_data(client, reg); 365 } 366 EXPORT_SYMBOL_NS_GPL(pmbus_read_word_data, PMBUS); 367 368 static int pmbus_read_virt_reg(struct i2c_client *client, int page, int reg) 369 { 370 int rv; 371 int id; 372 373 switch (reg) { 374 case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4: 375 id = reg - PMBUS_VIRT_FAN_TARGET_1; 376 rv = pmbus_get_fan_rate_device(client, page, id, rpm); 377 break; 378 default: 379 rv = -ENXIO; 380 break; 381 } 382 383 return rv; 384 } 385 386 /* 387 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if 388 * a device specific mapping function exists and calls it if necessary. 389 */ 390 static int _pmbus_read_word_data(struct i2c_client *client, int page, 391 int phase, int reg) 392 { 393 struct pmbus_data *data = i2c_get_clientdata(client); 394 const struct pmbus_driver_info *info = data->info; 395 int status; 396 397 if (info->read_word_data) { 398 status = info->read_word_data(client, page, phase, reg); 399 if (status != -ENODATA) 400 return status; 401 } 402 403 if (reg >= PMBUS_VIRT_BASE) 404 return pmbus_read_virt_reg(client, page, reg); 405 406 return pmbus_read_word_data(client, page, phase, reg); 407 } 408 409 /* Same as above, but without phase parameter, for use in check functions */ 410 static int __pmbus_read_word_data(struct i2c_client *client, int page, int reg) 411 { 412 return _pmbus_read_word_data(client, page, 0xff, reg); 413 } 414 415 int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg) 416 { 417 int rv; 418 419 rv = pmbus_set_page(client, page, 0xff); 420 if (rv < 0) 421 return rv; 422 423 return i2c_smbus_read_byte_data(client, reg); 424 } 425 EXPORT_SYMBOL_NS_GPL(pmbus_read_byte_data, PMBUS); 426 427 int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg, u8 value) 428 { 429 int rv; 430 431 rv = pmbus_set_page(client, page, 0xff); 432 if (rv < 0) 433 return rv; 434 435 return i2c_smbus_write_byte_data(client, reg, value); 436 } 437 EXPORT_SYMBOL_NS_GPL(pmbus_write_byte_data, PMBUS); 438 439 int pmbus_update_byte_data(struct i2c_client *client, int page, u8 reg, 440 u8 mask, u8 value) 441 { 442 unsigned int tmp; 443 int rv; 444 445 rv = _pmbus_read_byte_data(client, page, reg); 446 if (rv < 0) 447 return rv; 448 449 tmp = (rv & ~mask) | (value & mask); 450 451 if (tmp != rv) 452 rv = _pmbus_write_byte_data(client, page, reg, tmp); 453 454 return rv; 455 } 456 EXPORT_SYMBOL_NS_GPL(pmbus_update_byte_data, PMBUS); 457 458 static int pmbus_read_block_data(struct i2c_client *client, int page, u8 reg, 459 char *data_buf) 460 { 461 int rv; 462 463 rv = pmbus_set_page(client, page, 0xff); 464 if (rv < 0) 465 return rv; 466 467 return i2c_smbus_read_block_data(client, reg, data_buf); 468 } 469 470 static struct pmbus_sensor *pmbus_find_sensor(struct pmbus_data *data, int page, 471 int reg) 472 { 473 struct pmbus_sensor *sensor; 474 475 for (sensor = data->sensors; sensor; sensor = sensor->next) { 476 if (sensor->page == page && sensor->reg == reg) 477 return sensor; 478 } 479 480 return ERR_PTR(-EINVAL); 481 } 482 483 static int pmbus_get_fan_rate(struct i2c_client *client, int page, int id, 484 enum pmbus_fan_mode mode, 485 bool from_cache) 486 { 487 struct pmbus_data *data = i2c_get_clientdata(client); 488 bool want_rpm, have_rpm; 489 struct pmbus_sensor *s; 490 int config; 491 int reg; 492 493 want_rpm = (mode == rpm); 494 495 if (from_cache) { 496 reg = want_rpm ? PMBUS_VIRT_FAN_TARGET_1 : PMBUS_VIRT_PWM_1; 497 s = pmbus_find_sensor(data, page, reg + id); 498 if (IS_ERR(s)) 499 return PTR_ERR(s); 500 501 return s->data; 502 } 503 504 config = _pmbus_read_byte_data(client, page, 505 pmbus_fan_config_registers[id]); 506 if (config < 0) 507 return config; 508 509 have_rpm = !!(config & pmbus_fan_rpm_mask[id]); 510 if (want_rpm == have_rpm) 511 return pmbus_read_word_data(client, page, 0xff, 512 pmbus_fan_command_registers[id]); 513 514 /* Can't sensibly map between RPM and PWM, just return zero */ 515 return 0; 516 } 517 518 int pmbus_get_fan_rate_device(struct i2c_client *client, int page, int id, 519 enum pmbus_fan_mode mode) 520 { 521 return pmbus_get_fan_rate(client, page, id, mode, false); 522 } 523 EXPORT_SYMBOL_NS_GPL(pmbus_get_fan_rate_device, PMBUS); 524 525 int pmbus_get_fan_rate_cached(struct i2c_client *client, int page, int id, 526 enum pmbus_fan_mode mode) 527 { 528 return pmbus_get_fan_rate(client, page, id, mode, true); 529 } 530 EXPORT_SYMBOL_NS_GPL(pmbus_get_fan_rate_cached, PMBUS); 531 532 static void pmbus_clear_fault_page(struct i2c_client *client, int page) 533 { 534 _pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS); 535 } 536 537 void pmbus_clear_faults(struct i2c_client *client) 538 { 539 struct pmbus_data *data = i2c_get_clientdata(client); 540 int i; 541 542 for (i = 0; i < data->info->pages; i++) 543 pmbus_clear_fault_page(client, i); 544 } 545 EXPORT_SYMBOL_NS_GPL(pmbus_clear_faults, PMBUS); 546 547 static int pmbus_check_status_cml(struct i2c_client *client) 548 { 549 struct pmbus_data *data = i2c_get_clientdata(client); 550 int status, status2; 551 552 status = data->read_status(client, -1); 553 if (status < 0 || (status & PB_STATUS_CML)) { 554 status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML); 555 if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND)) 556 return -EIO; 557 } 558 return 0; 559 } 560 561 static bool pmbus_check_register(struct i2c_client *client, 562 int (*func)(struct i2c_client *client, 563 int page, int reg), 564 int page, int reg) 565 { 566 int rv; 567 struct pmbus_data *data = i2c_get_clientdata(client); 568 569 rv = func(client, page, reg); 570 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK)) 571 rv = pmbus_check_status_cml(client); 572 if (rv < 0 && (data->flags & PMBUS_READ_STATUS_AFTER_FAILED_CHECK)) 573 data->read_status(client, -1); 574 if (reg < PMBUS_VIRT_BASE) 575 pmbus_clear_fault_page(client, -1); 576 return rv >= 0; 577 } 578 579 static bool pmbus_check_status_register(struct i2c_client *client, int page) 580 { 581 int status; 582 struct pmbus_data *data = i2c_get_clientdata(client); 583 584 status = data->read_status(client, page); 585 if (status >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK) && 586 (status & PB_STATUS_CML)) { 587 status = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML); 588 if (status < 0 || (status & PB_CML_FAULT_INVALID_COMMAND)) 589 status = -EIO; 590 } 591 592 pmbus_clear_fault_page(client, -1); 593 return status >= 0; 594 } 595 596 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg) 597 { 598 return pmbus_check_register(client, _pmbus_read_byte_data, page, reg); 599 } 600 EXPORT_SYMBOL_NS_GPL(pmbus_check_byte_register, PMBUS); 601 602 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg) 603 { 604 return pmbus_check_register(client, __pmbus_read_word_data, page, reg); 605 } 606 EXPORT_SYMBOL_NS_GPL(pmbus_check_word_register, PMBUS); 607 608 static bool __maybe_unused pmbus_check_block_register(struct i2c_client *client, 609 int page, int reg) 610 { 611 int rv; 612 struct pmbus_data *data = i2c_get_clientdata(client); 613 char data_buf[I2C_SMBUS_BLOCK_MAX + 2]; 614 615 rv = pmbus_read_block_data(client, page, reg, data_buf); 616 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK)) 617 rv = pmbus_check_status_cml(client); 618 if (rv < 0 && (data->flags & PMBUS_READ_STATUS_AFTER_FAILED_CHECK)) 619 data->read_status(client, -1); 620 pmbus_clear_fault_page(client, -1); 621 return rv >= 0; 622 } 623 624 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client) 625 { 626 struct pmbus_data *data = i2c_get_clientdata(client); 627 628 return data->info; 629 } 630 EXPORT_SYMBOL_NS_GPL(pmbus_get_driver_info, PMBUS); 631 632 static int pmbus_get_status(struct i2c_client *client, int page, int reg) 633 { 634 struct pmbus_data *data = i2c_get_clientdata(client); 635 int status; 636 637 switch (reg) { 638 case PMBUS_STATUS_WORD: 639 status = data->read_status(client, page); 640 break; 641 default: 642 status = _pmbus_read_byte_data(client, page, reg); 643 break; 644 } 645 if (status < 0) 646 pmbus_clear_faults(client); 647 return status; 648 } 649 650 static void pmbus_update_sensor_data(struct i2c_client *client, struct pmbus_sensor *sensor) 651 { 652 if (sensor->data < 0 || sensor->update) 653 sensor->data = _pmbus_read_word_data(client, sensor->page, 654 sensor->phase, sensor->reg); 655 } 656 657 /* 658 * Convert ieee754 sensor values to milli- or micro-units 659 * depending on sensor type. 660 * 661 * ieee754 data format: 662 * bit 15: sign 663 * bit 10..14: exponent 664 * bit 0..9: mantissa 665 * exponent=0: 666 * v=(−1)^signbit * 2^(−14) * 0.significantbits 667 * exponent=1..30: 668 * v=(−1)^signbit * 2^(exponent - 15) * 1.significantbits 669 * exponent=31: 670 * v=NaN 671 * 672 * Add the number mantissa bits into the calculations for simplicity. 673 * To do that, add '10' to the exponent. By doing that, we can just add 674 * 0x400 to normal values and get the expected result. 675 */ 676 static long pmbus_reg2data_ieee754(struct pmbus_data *data, 677 struct pmbus_sensor *sensor) 678 { 679 int exponent; 680 bool sign; 681 long val; 682 683 /* only support half precision for now */ 684 sign = sensor->data & 0x8000; 685 exponent = (sensor->data >> 10) & 0x1f; 686 val = sensor->data & 0x3ff; 687 688 if (exponent == 0) { /* subnormal */ 689 exponent = -(14 + 10); 690 } else if (exponent == 0x1f) { /* NaN, convert to min/max */ 691 exponent = 0; 692 val = 65504; 693 } else { 694 exponent -= (15 + 10); /* normal */ 695 val |= 0x400; 696 } 697 698 /* scale result to milli-units for all sensors except fans */ 699 if (sensor->class != PSC_FAN) 700 val = val * 1000L; 701 702 /* scale result to micro-units for power sensors */ 703 if (sensor->class == PSC_POWER) 704 val = val * 1000L; 705 706 if (exponent >= 0) 707 val <<= exponent; 708 else 709 val >>= -exponent; 710 711 if (sign) 712 val = -val; 713 714 return val; 715 } 716 717 /* 718 * Convert linear sensor values to milli- or micro-units 719 * depending on sensor type. 720 */ 721 static s64 pmbus_reg2data_linear(struct pmbus_data *data, 722 struct pmbus_sensor *sensor) 723 { 724 s16 exponent; 725 s32 mantissa; 726 s64 val; 727 728 if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */ 729 exponent = data->exponent[sensor->page]; 730 mantissa = (u16) sensor->data; 731 } else { /* LINEAR11 */ 732 exponent = ((s16)sensor->data) >> 11; 733 mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5; 734 } 735 736 val = mantissa; 737 738 /* scale result to milli-units for all sensors except fans */ 739 if (sensor->class != PSC_FAN) 740 val = val * 1000LL; 741 742 /* scale result to micro-units for power sensors */ 743 if (sensor->class == PSC_POWER) 744 val = val * 1000LL; 745 746 if (exponent >= 0) 747 val <<= exponent; 748 else 749 val >>= -exponent; 750 751 return val; 752 } 753 754 /* 755 * Convert direct sensor values to milli- or micro-units 756 * depending on sensor type. 757 */ 758 static s64 pmbus_reg2data_direct(struct pmbus_data *data, 759 struct pmbus_sensor *sensor) 760 { 761 s64 b, val = (s16)sensor->data; 762 s32 m, R; 763 764 m = data->info->m[sensor->class]; 765 b = data->info->b[sensor->class]; 766 R = data->info->R[sensor->class]; 767 768 if (m == 0) 769 return 0; 770 771 /* X = 1/m * (Y * 10^-R - b) */ 772 R = -R; 773 /* scale result to milli-units for everything but fans */ 774 if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) { 775 R += 3; 776 b *= 1000; 777 } 778 779 /* scale result to micro-units for power sensors */ 780 if (sensor->class == PSC_POWER) { 781 R += 3; 782 b *= 1000; 783 } 784 785 while (R > 0) { 786 val *= 10; 787 R--; 788 } 789 while (R < 0) { 790 val = div_s64(val + 5LL, 10L); /* round closest */ 791 R++; 792 } 793 794 val = div_s64(val - b, m); 795 return val; 796 } 797 798 /* 799 * Convert VID sensor values to milli- or micro-units 800 * depending on sensor type. 801 */ 802 static s64 pmbus_reg2data_vid(struct pmbus_data *data, 803 struct pmbus_sensor *sensor) 804 { 805 long val = sensor->data; 806 long rv = 0; 807 808 switch (data->info->vrm_version[sensor->page]) { 809 case vr11: 810 if (val >= 0x02 && val <= 0xb2) 811 rv = DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100); 812 break; 813 case vr12: 814 if (val >= 0x01) 815 rv = 250 + (val - 1) * 5; 816 break; 817 case vr13: 818 if (val >= 0x01) 819 rv = 500 + (val - 1) * 10; 820 break; 821 case imvp9: 822 if (val >= 0x01) 823 rv = 200 + (val - 1) * 10; 824 break; 825 case amd625mv: 826 if (val >= 0x0 && val <= 0xd8) 827 rv = DIV_ROUND_CLOSEST(155000 - val * 625, 100); 828 break; 829 } 830 return rv; 831 } 832 833 static s64 pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor) 834 { 835 s64 val; 836 837 if (!sensor->convert) 838 return sensor->data; 839 840 switch (data->info->format[sensor->class]) { 841 case direct: 842 val = pmbus_reg2data_direct(data, sensor); 843 break; 844 case vid: 845 val = pmbus_reg2data_vid(data, sensor); 846 break; 847 case ieee754: 848 val = pmbus_reg2data_ieee754(data, sensor); 849 break; 850 case linear: 851 default: 852 val = pmbus_reg2data_linear(data, sensor); 853 break; 854 } 855 return val; 856 } 857 858 #define MAX_IEEE_MANTISSA (0x7ff * 1000) 859 #define MIN_IEEE_MANTISSA (0x400 * 1000) 860 861 static u16 pmbus_data2reg_ieee754(struct pmbus_data *data, 862 struct pmbus_sensor *sensor, long val) 863 { 864 u16 exponent = (15 + 10); 865 long mantissa; 866 u16 sign = 0; 867 868 /* simple case */ 869 if (val == 0) 870 return 0; 871 872 if (val < 0) { 873 sign = 0x8000; 874 val = -val; 875 } 876 877 /* Power is in uW. Convert to mW before converting. */ 878 if (sensor->class == PSC_POWER) 879 val = DIV_ROUND_CLOSEST(val, 1000L); 880 881 /* 882 * For simplicity, convert fan data to milli-units 883 * before calculating the exponent. 884 */ 885 if (sensor->class == PSC_FAN) 886 val = val * 1000; 887 888 /* Reduce large mantissa until it fits into 10 bit */ 889 while (val > MAX_IEEE_MANTISSA && exponent < 30) { 890 exponent++; 891 val >>= 1; 892 } 893 /* 894 * Increase small mantissa to generate valid 'normal' 895 * number 896 */ 897 while (val < MIN_IEEE_MANTISSA && exponent > 1) { 898 exponent--; 899 val <<= 1; 900 } 901 902 /* Convert mantissa from milli-units to units */ 903 mantissa = DIV_ROUND_CLOSEST(val, 1000); 904 905 /* 906 * Ensure that the resulting number is within range. 907 * Valid range is 0x400..0x7ff, where bit 10 reflects 908 * the implied high bit in normalized ieee754 numbers. 909 * Set the range to 0x400..0x7ff to reflect this. 910 * The upper bit is then removed by the mask against 911 * 0x3ff in the final assignment. 912 */ 913 if (mantissa > 0x7ff) 914 mantissa = 0x7ff; 915 else if (mantissa < 0x400) 916 mantissa = 0x400; 917 918 /* Convert to sign, 5 bit exponent, 10 bit mantissa */ 919 return sign | (mantissa & 0x3ff) | ((exponent << 10) & 0x7c00); 920 } 921 922 #define MAX_LIN_MANTISSA (1023 * 1000) 923 #define MIN_LIN_MANTISSA (511 * 1000) 924 925 static u16 pmbus_data2reg_linear(struct pmbus_data *data, 926 struct pmbus_sensor *sensor, s64 val) 927 { 928 s16 exponent = 0, mantissa; 929 bool negative = false; 930 931 /* simple case */ 932 if (val == 0) 933 return 0; 934 935 if (sensor->class == PSC_VOLTAGE_OUT) { 936 /* LINEAR16 does not support negative voltages */ 937 if (val < 0) 938 return 0; 939 940 /* 941 * For a static exponents, we don't have a choice 942 * but to adjust the value to it. 943 */ 944 if (data->exponent[sensor->page] < 0) 945 val <<= -data->exponent[sensor->page]; 946 else 947 val >>= data->exponent[sensor->page]; 948 val = DIV_ROUND_CLOSEST_ULL(val, 1000); 949 return clamp_val(val, 0, 0xffff); 950 } 951 952 if (val < 0) { 953 negative = true; 954 val = -val; 955 } 956 957 /* Power is in uW. Convert to mW before converting. */ 958 if (sensor->class == PSC_POWER) 959 val = DIV_ROUND_CLOSEST_ULL(val, 1000); 960 961 /* 962 * For simplicity, convert fan data to milli-units 963 * before calculating the exponent. 964 */ 965 if (sensor->class == PSC_FAN) 966 val = val * 1000LL; 967 968 /* Reduce large mantissa until it fits into 10 bit */ 969 while (val >= MAX_LIN_MANTISSA && exponent < 15) { 970 exponent++; 971 val >>= 1; 972 } 973 /* Increase small mantissa to improve precision */ 974 while (val < MIN_LIN_MANTISSA && exponent > -15) { 975 exponent--; 976 val <<= 1; 977 } 978 979 /* Convert mantissa from milli-units to units */ 980 mantissa = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff); 981 982 /* restore sign */ 983 if (negative) 984 mantissa = -mantissa; 985 986 /* Convert to 5 bit exponent, 11 bit mantissa */ 987 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800); 988 } 989 990 static u16 pmbus_data2reg_direct(struct pmbus_data *data, 991 struct pmbus_sensor *sensor, s64 val) 992 { 993 s64 b; 994 s32 m, R; 995 996 m = data->info->m[sensor->class]; 997 b = data->info->b[sensor->class]; 998 R = data->info->R[sensor->class]; 999 1000 /* Power is in uW. Adjust R and b. */ 1001 if (sensor->class == PSC_POWER) { 1002 R -= 3; 1003 b *= 1000; 1004 } 1005 1006 /* Calculate Y = (m * X + b) * 10^R */ 1007 if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) { 1008 R -= 3; /* Adjust R and b for data in milli-units */ 1009 b *= 1000; 1010 } 1011 val = val * m + b; 1012 1013 while (R > 0) { 1014 val *= 10; 1015 R--; 1016 } 1017 while (R < 0) { 1018 val = div_s64(val + 5LL, 10L); /* round closest */ 1019 R++; 1020 } 1021 1022 return (u16)clamp_val(val, S16_MIN, S16_MAX); 1023 } 1024 1025 static u16 pmbus_data2reg_vid(struct pmbus_data *data, 1026 struct pmbus_sensor *sensor, s64 val) 1027 { 1028 val = clamp_val(val, 500, 1600); 1029 1030 return 2 + DIV_ROUND_CLOSEST_ULL((1600LL - val) * 100LL, 625); 1031 } 1032 1033 static u16 pmbus_data2reg(struct pmbus_data *data, 1034 struct pmbus_sensor *sensor, s64 val) 1035 { 1036 u16 regval; 1037 1038 if (!sensor->convert) 1039 return val; 1040 1041 switch (data->info->format[sensor->class]) { 1042 case direct: 1043 regval = pmbus_data2reg_direct(data, sensor, val); 1044 break; 1045 case vid: 1046 regval = pmbus_data2reg_vid(data, sensor, val); 1047 break; 1048 case ieee754: 1049 regval = pmbus_data2reg_ieee754(data, sensor, val); 1050 break; 1051 case linear: 1052 default: 1053 regval = pmbus_data2reg_linear(data, sensor, val); 1054 break; 1055 } 1056 return regval; 1057 } 1058 1059 /* 1060 * Return boolean calculated from converted data. 1061 * <index> defines a status register index and mask. 1062 * The mask is in the lower 8 bits, the register index is in bits 8..23. 1063 * 1064 * The associated pmbus_boolean structure contains optional pointers to two 1065 * sensor attributes. If specified, those attributes are compared against each 1066 * other to determine if a limit has been exceeded. 1067 * 1068 * If the sensor attribute pointers are NULL, the function returns true if 1069 * (status[reg] & mask) is true. 1070 * 1071 * If sensor attribute pointers are provided, a comparison against a specified 1072 * limit has to be performed to determine the boolean result. 1073 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are 1074 * sensor values referenced by sensor attribute pointers s1 and s2). 1075 * 1076 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>. 1077 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>. 1078 * 1079 * If a negative value is stored in any of the referenced registers, this value 1080 * reflects an error code which will be returned. 1081 */ 1082 static int pmbus_get_boolean(struct i2c_client *client, struct pmbus_boolean *b, 1083 int index) 1084 { 1085 struct pmbus_data *data = i2c_get_clientdata(client); 1086 struct pmbus_sensor *s1 = b->s1; 1087 struct pmbus_sensor *s2 = b->s2; 1088 u16 mask = pb_index_to_mask(index); 1089 u8 page = pb_index_to_page(index); 1090 u16 reg = pb_index_to_reg(index); 1091 int ret, status; 1092 u16 regval; 1093 1094 mutex_lock(&data->update_lock); 1095 status = pmbus_get_status(client, page, reg); 1096 if (status < 0) { 1097 ret = status; 1098 goto unlock; 1099 } 1100 1101 if (s1) 1102 pmbus_update_sensor_data(client, s1); 1103 if (s2) 1104 pmbus_update_sensor_data(client, s2); 1105 1106 regval = status & mask; 1107 if (regval) { 1108 ret = _pmbus_write_byte_data(client, page, reg, regval); 1109 if (ret) 1110 goto unlock; 1111 } 1112 if (s1 && s2) { 1113 s64 v1, v2; 1114 1115 if (s1->data < 0) { 1116 ret = s1->data; 1117 goto unlock; 1118 } 1119 if (s2->data < 0) { 1120 ret = s2->data; 1121 goto unlock; 1122 } 1123 1124 v1 = pmbus_reg2data(data, s1); 1125 v2 = pmbus_reg2data(data, s2); 1126 ret = !!(regval && v1 >= v2); 1127 } else { 1128 ret = !!regval; 1129 } 1130 unlock: 1131 mutex_unlock(&data->update_lock); 1132 return ret; 1133 } 1134 1135 static ssize_t pmbus_show_boolean(struct device *dev, 1136 struct device_attribute *da, char *buf) 1137 { 1138 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 1139 struct pmbus_boolean *boolean = to_pmbus_boolean(attr); 1140 struct i2c_client *client = to_i2c_client(dev->parent); 1141 int val; 1142 1143 val = pmbus_get_boolean(client, boolean, attr->index); 1144 if (val < 0) 1145 return val; 1146 return sysfs_emit(buf, "%d\n", val); 1147 } 1148 1149 static ssize_t pmbus_show_sensor(struct device *dev, 1150 struct device_attribute *devattr, char *buf) 1151 { 1152 struct i2c_client *client = to_i2c_client(dev->parent); 1153 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr); 1154 struct pmbus_data *data = i2c_get_clientdata(client); 1155 ssize_t ret; 1156 1157 mutex_lock(&data->update_lock); 1158 pmbus_update_sensor_data(client, sensor); 1159 if (sensor->data < 0) 1160 ret = sensor->data; 1161 else 1162 ret = sysfs_emit(buf, "%lld\n", pmbus_reg2data(data, sensor)); 1163 mutex_unlock(&data->update_lock); 1164 return ret; 1165 } 1166 1167 static ssize_t pmbus_set_sensor(struct device *dev, 1168 struct device_attribute *devattr, 1169 const char *buf, size_t count) 1170 { 1171 struct i2c_client *client = to_i2c_client(dev->parent); 1172 struct pmbus_data *data = i2c_get_clientdata(client); 1173 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr); 1174 ssize_t rv = count; 1175 s64 val; 1176 int ret; 1177 u16 regval; 1178 1179 if (kstrtos64(buf, 10, &val) < 0) 1180 return -EINVAL; 1181 1182 mutex_lock(&data->update_lock); 1183 regval = pmbus_data2reg(data, sensor, val); 1184 ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval); 1185 if (ret < 0) 1186 rv = ret; 1187 else 1188 sensor->data = -ENODATA; 1189 mutex_unlock(&data->update_lock); 1190 return rv; 1191 } 1192 1193 static ssize_t pmbus_show_label(struct device *dev, 1194 struct device_attribute *da, char *buf) 1195 { 1196 struct pmbus_label *label = to_pmbus_label(da); 1197 1198 return sysfs_emit(buf, "%s\n", label->label); 1199 } 1200 1201 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr) 1202 { 1203 if (data->num_attributes >= data->max_attributes - 1) { 1204 int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE; 1205 void *new_attrs = devm_krealloc_array(data->dev, data->group.attrs, 1206 new_max_attrs, sizeof(void *), 1207 GFP_KERNEL); 1208 if (!new_attrs) 1209 return -ENOMEM; 1210 data->group.attrs = new_attrs; 1211 data->max_attributes = new_max_attrs; 1212 } 1213 1214 data->group.attrs[data->num_attributes++] = attr; 1215 data->group.attrs[data->num_attributes] = NULL; 1216 return 0; 1217 } 1218 1219 static void pmbus_dev_attr_init(struct device_attribute *dev_attr, 1220 const char *name, 1221 umode_t mode, 1222 ssize_t (*show)(struct device *dev, 1223 struct device_attribute *attr, 1224 char *buf), 1225 ssize_t (*store)(struct device *dev, 1226 struct device_attribute *attr, 1227 const char *buf, size_t count)) 1228 { 1229 sysfs_attr_init(&dev_attr->attr); 1230 dev_attr->attr.name = name; 1231 dev_attr->attr.mode = mode; 1232 dev_attr->show = show; 1233 dev_attr->store = store; 1234 } 1235 1236 static void pmbus_attr_init(struct sensor_device_attribute *a, 1237 const char *name, 1238 umode_t mode, 1239 ssize_t (*show)(struct device *dev, 1240 struct device_attribute *attr, 1241 char *buf), 1242 ssize_t (*store)(struct device *dev, 1243 struct device_attribute *attr, 1244 const char *buf, size_t count), 1245 int idx) 1246 { 1247 pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store); 1248 a->index = idx; 1249 } 1250 1251 static int pmbus_add_boolean(struct pmbus_data *data, 1252 const char *name, const char *type, int seq, 1253 struct pmbus_sensor *s1, 1254 struct pmbus_sensor *s2, 1255 u8 page, u16 reg, u16 mask) 1256 { 1257 struct pmbus_boolean *boolean; 1258 struct sensor_device_attribute *a; 1259 1260 if (WARN((s1 && !s2) || (!s1 && s2), "Bad s1/s2 parameters\n")) 1261 return -EINVAL; 1262 1263 boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL); 1264 if (!boolean) 1265 return -ENOMEM; 1266 1267 a = &boolean->attribute; 1268 1269 snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s", 1270 name, seq, type); 1271 boolean->s1 = s1; 1272 boolean->s2 = s2; 1273 pmbus_attr_init(a, boolean->name, 0444, pmbus_show_boolean, NULL, 1274 pb_reg_to_index(page, reg, mask)); 1275 1276 return pmbus_add_attribute(data, &a->dev_attr.attr); 1277 } 1278 1279 /* of thermal for pmbus temperature sensors */ 1280 struct pmbus_thermal_data { 1281 struct pmbus_data *pmbus_data; 1282 struct pmbus_sensor *sensor; 1283 }; 1284 1285 static int pmbus_thermal_get_temp(struct thermal_zone_device *tz, int *temp) 1286 { 1287 struct pmbus_thermal_data *tdata = thermal_zone_device_priv(tz); 1288 struct pmbus_sensor *sensor = tdata->sensor; 1289 struct pmbus_data *pmbus_data = tdata->pmbus_data; 1290 struct i2c_client *client = to_i2c_client(pmbus_data->dev); 1291 struct device *dev = pmbus_data->hwmon_dev; 1292 int ret = 0; 1293 1294 if (!dev) { 1295 /* May not even get to hwmon yet */ 1296 *temp = 0; 1297 return 0; 1298 } 1299 1300 mutex_lock(&pmbus_data->update_lock); 1301 pmbus_update_sensor_data(client, sensor); 1302 if (sensor->data < 0) 1303 ret = sensor->data; 1304 else 1305 *temp = (int)pmbus_reg2data(pmbus_data, sensor); 1306 mutex_unlock(&pmbus_data->update_lock); 1307 1308 return ret; 1309 } 1310 1311 static const struct thermal_zone_device_ops pmbus_thermal_ops = { 1312 .get_temp = pmbus_thermal_get_temp, 1313 }; 1314 1315 static int pmbus_thermal_add_sensor(struct pmbus_data *pmbus_data, 1316 struct pmbus_sensor *sensor, int index) 1317 { 1318 struct device *dev = pmbus_data->dev; 1319 struct pmbus_thermal_data *tdata; 1320 struct thermal_zone_device *tzd; 1321 1322 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); 1323 if (!tdata) 1324 return -ENOMEM; 1325 1326 tdata->sensor = sensor; 1327 tdata->pmbus_data = pmbus_data; 1328 1329 tzd = devm_thermal_of_zone_register(dev, index, tdata, 1330 &pmbus_thermal_ops); 1331 /* 1332 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV, 1333 * so ignore that error but forward any other error. 1334 */ 1335 if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV)) 1336 return PTR_ERR(tzd); 1337 1338 return 0; 1339 } 1340 1341 static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data, 1342 const char *name, const char *type, 1343 int seq, int page, int phase, 1344 int reg, 1345 enum pmbus_sensor_classes class, 1346 bool update, bool readonly, 1347 bool convert) 1348 { 1349 struct pmbus_sensor *sensor; 1350 struct device_attribute *a; 1351 1352 sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL); 1353 if (!sensor) 1354 return NULL; 1355 a = &sensor->attribute; 1356 1357 if (type) 1358 snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s", 1359 name, seq, type); 1360 else 1361 snprintf(sensor->name, sizeof(sensor->name), "%s%d", 1362 name, seq); 1363 1364 if (data->flags & PMBUS_WRITE_PROTECTED) 1365 readonly = true; 1366 1367 sensor->page = page; 1368 sensor->phase = phase; 1369 sensor->reg = reg; 1370 sensor->class = class; 1371 sensor->update = update; 1372 sensor->convert = convert; 1373 sensor->data = -ENODATA; 1374 pmbus_dev_attr_init(a, sensor->name, 1375 readonly ? 0444 : 0644, 1376 pmbus_show_sensor, pmbus_set_sensor); 1377 1378 if (pmbus_add_attribute(data, &a->attr)) 1379 return NULL; 1380 1381 sensor->next = data->sensors; 1382 data->sensors = sensor; 1383 1384 /* temperature sensors with _input values are registered with thermal */ 1385 if (class == PSC_TEMPERATURE && strcmp(type, "input") == 0) 1386 pmbus_thermal_add_sensor(data, sensor, seq); 1387 1388 return sensor; 1389 } 1390 1391 static int pmbus_add_label(struct pmbus_data *data, 1392 const char *name, int seq, 1393 const char *lstring, int index, int phase) 1394 { 1395 struct pmbus_label *label; 1396 struct device_attribute *a; 1397 1398 label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL); 1399 if (!label) 1400 return -ENOMEM; 1401 1402 a = &label->attribute; 1403 1404 snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq); 1405 if (!index) { 1406 if (phase == 0xff) 1407 strncpy(label->label, lstring, 1408 sizeof(label->label) - 1); 1409 else 1410 snprintf(label->label, sizeof(label->label), "%s.%d", 1411 lstring, phase); 1412 } else { 1413 if (phase == 0xff) 1414 snprintf(label->label, sizeof(label->label), "%s%d", 1415 lstring, index); 1416 else 1417 snprintf(label->label, sizeof(label->label), "%s%d.%d", 1418 lstring, index, phase); 1419 } 1420 1421 pmbus_dev_attr_init(a, label->name, 0444, pmbus_show_label, NULL); 1422 return pmbus_add_attribute(data, &a->attr); 1423 } 1424 1425 /* 1426 * Search for attributes. Allocate sensors, booleans, and labels as needed. 1427 */ 1428 1429 /* 1430 * The pmbus_limit_attr structure describes a single limit attribute 1431 * and its associated alarm attribute. 1432 */ 1433 struct pmbus_limit_attr { 1434 u16 reg; /* Limit register */ 1435 u16 sbit; /* Alarm attribute status bit */ 1436 bool update; /* True if register needs updates */ 1437 bool low; /* True if low limit; for limits with compare 1438 functions only */ 1439 const char *attr; /* Attribute name */ 1440 const char *alarm; /* Alarm attribute name */ 1441 }; 1442 1443 /* 1444 * The pmbus_sensor_attr structure describes one sensor attribute. This 1445 * description includes a reference to the associated limit attributes. 1446 */ 1447 struct pmbus_sensor_attr { 1448 u16 reg; /* sensor register */ 1449 u16 gbit; /* generic status bit */ 1450 u8 nlimit; /* # of limit registers */ 1451 enum pmbus_sensor_classes class;/* sensor class */ 1452 const char *label; /* sensor label */ 1453 bool paged; /* true if paged sensor */ 1454 bool update; /* true if update needed */ 1455 bool compare; /* true if compare function needed */ 1456 u32 func; /* sensor mask */ 1457 u32 sfunc; /* sensor status mask */ 1458 int sreg; /* status register */ 1459 const struct pmbus_limit_attr *limit;/* limit registers */ 1460 }; 1461 1462 /* 1463 * Add a set of limit attributes and, if supported, the associated 1464 * alarm attributes. 1465 * returns 0 if no alarm register found, 1 if an alarm register was found, 1466 * < 0 on errors. 1467 */ 1468 static int pmbus_add_limit_attrs(struct i2c_client *client, 1469 struct pmbus_data *data, 1470 const struct pmbus_driver_info *info, 1471 const char *name, int index, int page, 1472 struct pmbus_sensor *base, 1473 const struct pmbus_sensor_attr *attr) 1474 { 1475 const struct pmbus_limit_attr *l = attr->limit; 1476 int nlimit = attr->nlimit; 1477 int have_alarm = 0; 1478 int i, ret; 1479 struct pmbus_sensor *curr; 1480 1481 for (i = 0; i < nlimit; i++) { 1482 if (pmbus_check_word_register(client, page, l->reg)) { 1483 curr = pmbus_add_sensor(data, name, l->attr, index, 1484 page, 0xff, l->reg, attr->class, 1485 attr->update || l->update, 1486 false, true); 1487 if (!curr) 1488 return -ENOMEM; 1489 if (l->sbit && (info->func[page] & attr->sfunc)) { 1490 ret = pmbus_add_boolean(data, name, 1491 l->alarm, index, 1492 attr->compare ? l->low ? curr : base 1493 : NULL, 1494 attr->compare ? l->low ? base : curr 1495 : NULL, 1496 page, attr->sreg, l->sbit); 1497 if (ret) 1498 return ret; 1499 have_alarm = 1; 1500 } 1501 } 1502 l++; 1503 } 1504 return have_alarm; 1505 } 1506 1507 static int pmbus_add_sensor_attrs_one(struct i2c_client *client, 1508 struct pmbus_data *data, 1509 const struct pmbus_driver_info *info, 1510 const char *name, 1511 int index, int page, int phase, 1512 const struct pmbus_sensor_attr *attr, 1513 bool paged) 1514 { 1515 struct pmbus_sensor *base; 1516 bool upper = !!(attr->gbit & 0xff00); /* need to check STATUS_WORD */ 1517 int ret; 1518 1519 if (attr->label) { 1520 ret = pmbus_add_label(data, name, index, attr->label, 1521 paged ? page + 1 : 0, phase); 1522 if (ret) 1523 return ret; 1524 } 1525 base = pmbus_add_sensor(data, name, "input", index, page, phase, 1526 attr->reg, attr->class, true, true, true); 1527 if (!base) 1528 return -ENOMEM; 1529 /* No limit and alarm attributes for phase specific sensors */ 1530 if (attr->sfunc && phase == 0xff) { 1531 ret = pmbus_add_limit_attrs(client, data, info, name, 1532 index, page, base, attr); 1533 if (ret < 0) 1534 return ret; 1535 /* 1536 * Add generic alarm attribute only if there are no individual 1537 * alarm attributes, if there is a global alarm bit, and if 1538 * the generic status register (word or byte, depending on 1539 * which global bit is set) for this page is accessible. 1540 */ 1541 if (!ret && attr->gbit && 1542 (!upper || data->has_status_word) && 1543 pmbus_check_status_register(client, page)) { 1544 ret = pmbus_add_boolean(data, name, "alarm", index, 1545 NULL, NULL, 1546 page, PMBUS_STATUS_WORD, 1547 attr->gbit); 1548 if (ret) 1549 return ret; 1550 } 1551 } 1552 return 0; 1553 } 1554 1555 static bool pmbus_sensor_is_paged(const struct pmbus_driver_info *info, 1556 const struct pmbus_sensor_attr *attr) 1557 { 1558 int p; 1559 1560 if (attr->paged) 1561 return true; 1562 1563 /* 1564 * Some attributes may be present on more than one page despite 1565 * not being marked with the paged attribute. If that is the case, 1566 * then treat the sensor as being paged and add the page suffix to the 1567 * attribute name. 1568 * We don't just add the paged attribute to all such attributes, in 1569 * order to maintain the un-suffixed labels in the case where the 1570 * attribute is only on page 0. 1571 */ 1572 for (p = 1; p < info->pages; p++) { 1573 if (info->func[p] & attr->func) 1574 return true; 1575 } 1576 return false; 1577 } 1578 1579 static int pmbus_add_sensor_attrs(struct i2c_client *client, 1580 struct pmbus_data *data, 1581 const char *name, 1582 const struct pmbus_sensor_attr *attrs, 1583 int nattrs) 1584 { 1585 const struct pmbus_driver_info *info = data->info; 1586 int index, i; 1587 int ret; 1588 1589 index = 1; 1590 for (i = 0; i < nattrs; i++) { 1591 int page, pages; 1592 bool paged = pmbus_sensor_is_paged(info, attrs); 1593 1594 pages = paged ? info->pages : 1; 1595 for (page = 0; page < pages; page++) { 1596 if (info->func[page] & attrs->func) { 1597 ret = pmbus_add_sensor_attrs_one(client, data, info, 1598 name, index, page, 1599 0xff, attrs, paged); 1600 if (ret) 1601 return ret; 1602 index++; 1603 } 1604 if (info->phases[page]) { 1605 int phase; 1606 1607 for (phase = 0; phase < info->phases[page]; 1608 phase++) { 1609 if (!(info->pfunc[phase] & attrs->func)) 1610 continue; 1611 ret = pmbus_add_sensor_attrs_one(client, 1612 data, info, name, index, page, 1613 phase, attrs, paged); 1614 if (ret) 1615 return ret; 1616 index++; 1617 } 1618 } 1619 } 1620 attrs++; 1621 } 1622 return 0; 1623 } 1624 1625 static const struct pmbus_limit_attr vin_limit_attrs[] = { 1626 { 1627 .reg = PMBUS_VIN_UV_WARN_LIMIT, 1628 .attr = "min", 1629 .alarm = "min_alarm", 1630 .sbit = PB_VOLTAGE_UV_WARNING, 1631 }, { 1632 .reg = PMBUS_VIN_UV_FAULT_LIMIT, 1633 .attr = "lcrit", 1634 .alarm = "lcrit_alarm", 1635 .sbit = PB_VOLTAGE_UV_FAULT | PB_VOLTAGE_VIN_OFF, 1636 }, { 1637 .reg = PMBUS_VIN_OV_WARN_LIMIT, 1638 .attr = "max", 1639 .alarm = "max_alarm", 1640 .sbit = PB_VOLTAGE_OV_WARNING, 1641 }, { 1642 .reg = PMBUS_VIN_OV_FAULT_LIMIT, 1643 .attr = "crit", 1644 .alarm = "crit_alarm", 1645 .sbit = PB_VOLTAGE_OV_FAULT, 1646 }, { 1647 .reg = PMBUS_VIRT_READ_VIN_AVG, 1648 .update = true, 1649 .attr = "average", 1650 }, { 1651 .reg = PMBUS_VIRT_READ_VIN_MIN, 1652 .update = true, 1653 .attr = "lowest", 1654 }, { 1655 .reg = PMBUS_VIRT_READ_VIN_MAX, 1656 .update = true, 1657 .attr = "highest", 1658 }, { 1659 .reg = PMBUS_VIRT_RESET_VIN_HISTORY, 1660 .attr = "reset_history", 1661 }, { 1662 .reg = PMBUS_MFR_VIN_MIN, 1663 .attr = "rated_min", 1664 }, { 1665 .reg = PMBUS_MFR_VIN_MAX, 1666 .attr = "rated_max", 1667 }, 1668 }; 1669 1670 static const struct pmbus_limit_attr vmon_limit_attrs[] = { 1671 { 1672 .reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT, 1673 .attr = "min", 1674 .alarm = "min_alarm", 1675 .sbit = PB_VOLTAGE_UV_WARNING, 1676 }, { 1677 .reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT, 1678 .attr = "lcrit", 1679 .alarm = "lcrit_alarm", 1680 .sbit = PB_VOLTAGE_UV_FAULT, 1681 }, { 1682 .reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT, 1683 .attr = "max", 1684 .alarm = "max_alarm", 1685 .sbit = PB_VOLTAGE_OV_WARNING, 1686 }, { 1687 .reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT, 1688 .attr = "crit", 1689 .alarm = "crit_alarm", 1690 .sbit = PB_VOLTAGE_OV_FAULT, 1691 } 1692 }; 1693 1694 static const struct pmbus_limit_attr vout_limit_attrs[] = { 1695 { 1696 .reg = PMBUS_VOUT_UV_WARN_LIMIT, 1697 .attr = "min", 1698 .alarm = "min_alarm", 1699 .sbit = PB_VOLTAGE_UV_WARNING, 1700 }, { 1701 .reg = PMBUS_VOUT_UV_FAULT_LIMIT, 1702 .attr = "lcrit", 1703 .alarm = "lcrit_alarm", 1704 .sbit = PB_VOLTAGE_UV_FAULT, 1705 }, { 1706 .reg = PMBUS_VOUT_OV_WARN_LIMIT, 1707 .attr = "max", 1708 .alarm = "max_alarm", 1709 .sbit = PB_VOLTAGE_OV_WARNING, 1710 }, { 1711 .reg = PMBUS_VOUT_OV_FAULT_LIMIT, 1712 .attr = "crit", 1713 .alarm = "crit_alarm", 1714 .sbit = PB_VOLTAGE_OV_FAULT, 1715 }, { 1716 .reg = PMBUS_VIRT_READ_VOUT_AVG, 1717 .update = true, 1718 .attr = "average", 1719 }, { 1720 .reg = PMBUS_VIRT_READ_VOUT_MIN, 1721 .update = true, 1722 .attr = "lowest", 1723 }, { 1724 .reg = PMBUS_VIRT_READ_VOUT_MAX, 1725 .update = true, 1726 .attr = "highest", 1727 }, { 1728 .reg = PMBUS_VIRT_RESET_VOUT_HISTORY, 1729 .attr = "reset_history", 1730 }, { 1731 .reg = PMBUS_MFR_VOUT_MIN, 1732 .attr = "rated_min", 1733 }, { 1734 .reg = PMBUS_MFR_VOUT_MAX, 1735 .attr = "rated_max", 1736 }, 1737 }; 1738 1739 static const struct pmbus_sensor_attr voltage_attributes[] = { 1740 { 1741 .reg = PMBUS_READ_VIN, 1742 .class = PSC_VOLTAGE_IN, 1743 .label = "vin", 1744 .func = PMBUS_HAVE_VIN, 1745 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1746 .sreg = PMBUS_STATUS_INPUT, 1747 .gbit = PB_STATUS_VIN_UV, 1748 .limit = vin_limit_attrs, 1749 .nlimit = ARRAY_SIZE(vin_limit_attrs), 1750 }, { 1751 .reg = PMBUS_VIRT_READ_VMON, 1752 .class = PSC_VOLTAGE_IN, 1753 .label = "vmon", 1754 .func = PMBUS_HAVE_VMON, 1755 .sfunc = PMBUS_HAVE_STATUS_VMON, 1756 .sreg = PMBUS_VIRT_STATUS_VMON, 1757 .limit = vmon_limit_attrs, 1758 .nlimit = ARRAY_SIZE(vmon_limit_attrs), 1759 }, { 1760 .reg = PMBUS_READ_VCAP, 1761 .class = PSC_VOLTAGE_IN, 1762 .label = "vcap", 1763 .func = PMBUS_HAVE_VCAP, 1764 }, { 1765 .reg = PMBUS_READ_VOUT, 1766 .class = PSC_VOLTAGE_OUT, 1767 .label = "vout", 1768 .paged = true, 1769 .func = PMBUS_HAVE_VOUT, 1770 .sfunc = PMBUS_HAVE_STATUS_VOUT, 1771 .sreg = PMBUS_STATUS_VOUT, 1772 .gbit = PB_STATUS_VOUT_OV, 1773 .limit = vout_limit_attrs, 1774 .nlimit = ARRAY_SIZE(vout_limit_attrs), 1775 } 1776 }; 1777 1778 /* Current attributes */ 1779 1780 static const struct pmbus_limit_attr iin_limit_attrs[] = { 1781 { 1782 .reg = PMBUS_IIN_OC_WARN_LIMIT, 1783 .attr = "max", 1784 .alarm = "max_alarm", 1785 .sbit = PB_IIN_OC_WARNING, 1786 }, { 1787 .reg = PMBUS_IIN_OC_FAULT_LIMIT, 1788 .attr = "crit", 1789 .alarm = "crit_alarm", 1790 .sbit = PB_IIN_OC_FAULT, 1791 }, { 1792 .reg = PMBUS_VIRT_READ_IIN_AVG, 1793 .update = true, 1794 .attr = "average", 1795 }, { 1796 .reg = PMBUS_VIRT_READ_IIN_MIN, 1797 .update = true, 1798 .attr = "lowest", 1799 }, { 1800 .reg = PMBUS_VIRT_READ_IIN_MAX, 1801 .update = true, 1802 .attr = "highest", 1803 }, { 1804 .reg = PMBUS_VIRT_RESET_IIN_HISTORY, 1805 .attr = "reset_history", 1806 }, { 1807 .reg = PMBUS_MFR_IIN_MAX, 1808 .attr = "rated_max", 1809 }, 1810 }; 1811 1812 static const struct pmbus_limit_attr iout_limit_attrs[] = { 1813 { 1814 .reg = PMBUS_IOUT_OC_WARN_LIMIT, 1815 .attr = "max", 1816 .alarm = "max_alarm", 1817 .sbit = PB_IOUT_OC_WARNING, 1818 }, { 1819 .reg = PMBUS_IOUT_UC_FAULT_LIMIT, 1820 .attr = "lcrit", 1821 .alarm = "lcrit_alarm", 1822 .sbit = PB_IOUT_UC_FAULT, 1823 }, { 1824 .reg = PMBUS_IOUT_OC_FAULT_LIMIT, 1825 .attr = "crit", 1826 .alarm = "crit_alarm", 1827 .sbit = PB_IOUT_OC_FAULT, 1828 }, { 1829 .reg = PMBUS_VIRT_READ_IOUT_AVG, 1830 .update = true, 1831 .attr = "average", 1832 }, { 1833 .reg = PMBUS_VIRT_READ_IOUT_MIN, 1834 .update = true, 1835 .attr = "lowest", 1836 }, { 1837 .reg = PMBUS_VIRT_READ_IOUT_MAX, 1838 .update = true, 1839 .attr = "highest", 1840 }, { 1841 .reg = PMBUS_VIRT_RESET_IOUT_HISTORY, 1842 .attr = "reset_history", 1843 }, { 1844 .reg = PMBUS_MFR_IOUT_MAX, 1845 .attr = "rated_max", 1846 }, 1847 }; 1848 1849 static const struct pmbus_sensor_attr current_attributes[] = { 1850 { 1851 .reg = PMBUS_READ_IIN, 1852 .class = PSC_CURRENT_IN, 1853 .label = "iin", 1854 .func = PMBUS_HAVE_IIN, 1855 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1856 .sreg = PMBUS_STATUS_INPUT, 1857 .gbit = PB_STATUS_INPUT, 1858 .limit = iin_limit_attrs, 1859 .nlimit = ARRAY_SIZE(iin_limit_attrs), 1860 }, { 1861 .reg = PMBUS_READ_IOUT, 1862 .class = PSC_CURRENT_OUT, 1863 .label = "iout", 1864 .paged = true, 1865 .func = PMBUS_HAVE_IOUT, 1866 .sfunc = PMBUS_HAVE_STATUS_IOUT, 1867 .sreg = PMBUS_STATUS_IOUT, 1868 .gbit = PB_STATUS_IOUT_OC, 1869 .limit = iout_limit_attrs, 1870 .nlimit = ARRAY_SIZE(iout_limit_attrs), 1871 } 1872 }; 1873 1874 /* Power attributes */ 1875 1876 static const struct pmbus_limit_attr pin_limit_attrs[] = { 1877 { 1878 .reg = PMBUS_PIN_OP_WARN_LIMIT, 1879 .attr = "max", 1880 .alarm = "alarm", 1881 .sbit = PB_PIN_OP_WARNING, 1882 }, { 1883 .reg = PMBUS_VIRT_READ_PIN_AVG, 1884 .update = true, 1885 .attr = "average", 1886 }, { 1887 .reg = PMBUS_VIRT_READ_PIN_MIN, 1888 .update = true, 1889 .attr = "input_lowest", 1890 }, { 1891 .reg = PMBUS_VIRT_READ_PIN_MAX, 1892 .update = true, 1893 .attr = "input_highest", 1894 }, { 1895 .reg = PMBUS_VIRT_RESET_PIN_HISTORY, 1896 .attr = "reset_history", 1897 }, { 1898 .reg = PMBUS_MFR_PIN_MAX, 1899 .attr = "rated_max", 1900 }, 1901 }; 1902 1903 static const struct pmbus_limit_attr pout_limit_attrs[] = { 1904 { 1905 .reg = PMBUS_POUT_MAX, 1906 .attr = "cap", 1907 .alarm = "cap_alarm", 1908 .sbit = PB_POWER_LIMITING, 1909 }, { 1910 .reg = PMBUS_POUT_OP_WARN_LIMIT, 1911 .attr = "max", 1912 .alarm = "max_alarm", 1913 .sbit = PB_POUT_OP_WARNING, 1914 }, { 1915 .reg = PMBUS_POUT_OP_FAULT_LIMIT, 1916 .attr = "crit", 1917 .alarm = "crit_alarm", 1918 .sbit = PB_POUT_OP_FAULT, 1919 }, { 1920 .reg = PMBUS_VIRT_READ_POUT_AVG, 1921 .update = true, 1922 .attr = "average", 1923 }, { 1924 .reg = PMBUS_VIRT_READ_POUT_MIN, 1925 .update = true, 1926 .attr = "input_lowest", 1927 }, { 1928 .reg = PMBUS_VIRT_READ_POUT_MAX, 1929 .update = true, 1930 .attr = "input_highest", 1931 }, { 1932 .reg = PMBUS_VIRT_RESET_POUT_HISTORY, 1933 .attr = "reset_history", 1934 }, { 1935 .reg = PMBUS_MFR_POUT_MAX, 1936 .attr = "rated_max", 1937 }, 1938 }; 1939 1940 static const struct pmbus_sensor_attr power_attributes[] = { 1941 { 1942 .reg = PMBUS_READ_PIN, 1943 .class = PSC_POWER, 1944 .label = "pin", 1945 .func = PMBUS_HAVE_PIN, 1946 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1947 .sreg = PMBUS_STATUS_INPUT, 1948 .gbit = PB_STATUS_INPUT, 1949 .limit = pin_limit_attrs, 1950 .nlimit = ARRAY_SIZE(pin_limit_attrs), 1951 }, { 1952 .reg = PMBUS_READ_POUT, 1953 .class = PSC_POWER, 1954 .label = "pout", 1955 .paged = true, 1956 .func = PMBUS_HAVE_POUT, 1957 .sfunc = PMBUS_HAVE_STATUS_IOUT, 1958 .sreg = PMBUS_STATUS_IOUT, 1959 .limit = pout_limit_attrs, 1960 .nlimit = ARRAY_SIZE(pout_limit_attrs), 1961 } 1962 }; 1963 1964 /* Temperature atributes */ 1965 1966 static const struct pmbus_limit_attr temp_limit_attrs[] = { 1967 { 1968 .reg = PMBUS_UT_WARN_LIMIT, 1969 .low = true, 1970 .attr = "min", 1971 .alarm = "min_alarm", 1972 .sbit = PB_TEMP_UT_WARNING, 1973 }, { 1974 .reg = PMBUS_UT_FAULT_LIMIT, 1975 .low = true, 1976 .attr = "lcrit", 1977 .alarm = "lcrit_alarm", 1978 .sbit = PB_TEMP_UT_FAULT, 1979 }, { 1980 .reg = PMBUS_OT_WARN_LIMIT, 1981 .attr = "max", 1982 .alarm = "max_alarm", 1983 .sbit = PB_TEMP_OT_WARNING, 1984 }, { 1985 .reg = PMBUS_OT_FAULT_LIMIT, 1986 .attr = "crit", 1987 .alarm = "crit_alarm", 1988 .sbit = PB_TEMP_OT_FAULT, 1989 }, { 1990 .reg = PMBUS_VIRT_READ_TEMP_MIN, 1991 .attr = "lowest", 1992 }, { 1993 .reg = PMBUS_VIRT_READ_TEMP_AVG, 1994 .attr = "average", 1995 }, { 1996 .reg = PMBUS_VIRT_READ_TEMP_MAX, 1997 .attr = "highest", 1998 }, { 1999 .reg = PMBUS_VIRT_RESET_TEMP_HISTORY, 2000 .attr = "reset_history", 2001 }, { 2002 .reg = PMBUS_MFR_MAX_TEMP_1, 2003 .attr = "rated_max", 2004 }, 2005 }; 2006 2007 static const struct pmbus_limit_attr temp_limit_attrs2[] = { 2008 { 2009 .reg = PMBUS_UT_WARN_LIMIT, 2010 .low = true, 2011 .attr = "min", 2012 .alarm = "min_alarm", 2013 .sbit = PB_TEMP_UT_WARNING, 2014 }, { 2015 .reg = PMBUS_UT_FAULT_LIMIT, 2016 .low = true, 2017 .attr = "lcrit", 2018 .alarm = "lcrit_alarm", 2019 .sbit = PB_TEMP_UT_FAULT, 2020 }, { 2021 .reg = PMBUS_OT_WARN_LIMIT, 2022 .attr = "max", 2023 .alarm = "max_alarm", 2024 .sbit = PB_TEMP_OT_WARNING, 2025 }, { 2026 .reg = PMBUS_OT_FAULT_LIMIT, 2027 .attr = "crit", 2028 .alarm = "crit_alarm", 2029 .sbit = PB_TEMP_OT_FAULT, 2030 }, { 2031 .reg = PMBUS_VIRT_READ_TEMP2_MIN, 2032 .attr = "lowest", 2033 }, { 2034 .reg = PMBUS_VIRT_READ_TEMP2_AVG, 2035 .attr = "average", 2036 }, { 2037 .reg = PMBUS_VIRT_READ_TEMP2_MAX, 2038 .attr = "highest", 2039 }, { 2040 .reg = PMBUS_VIRT_RESET_TEMP2_HISTORY, 2041 .attr = "reset_history", 2042 }, { 2043 .reg = PMBUS_MFR_MAX_TEMP_2, 2044 .attr = "rated_max", 2045 }, 2046 }; 2047 2048 static const struct pmbus_limit_attr temp_limit_attrs3[] = { 2049 { 2050 .reg = PMBUS_UT_WARN_LIMIT, 2051 .low = true, 2052 .attr = "min", 2053 .alarm = "min_alarm", 2054 .sbit = PB_TEMP_UT_WARNING, 2055 }, { 2056 .reg = PMBUS_UT_FAULT_LIMIT, 2057 .low = true, 2058 .attr = "lcrit", 2059 .alarm = "lcrit_alarm", 2060 .sbit = PB_TEMP_UT_FAULT, 2061 }, { 2062 .reg = PMBUS_OT_WARN_LIMIT, 2063 .attr = "max", 2064 .alarm = "max_alarm", 2065 .sbit = PB_TEMP_OT_WARNING, 2066 }, { 2067 .reg = PMBUS_OT_FAULT_LIMIT, 2068 .attr = "crit", 2069 .alarm = "crit_alarm", 2070 .sbit = PB_TEMP_OT_FAULT, 2071 }, { 2072 .reg = PMBUS_MFR_MAX_TEMP_3, 2073 .attr = "rated_max", 2074 }, 2075 }; 2076 2077 static const struct pmbus_sensor_attr temp_attributes[] = { 2078 { 2079 .reg = PMBUS_READ_TEMPERATURE_1, 2080 .class = PSC_TEMPERATURE, 2081 .paged = true, 2082 .update = true, 2083 .compare = true, 2084 .func = PMBUS_HAVE_TEMP, 2085 .sfunc = PMBUS_HAVE_STATUS_TEMP, 2086 .sreg = PMBUS_STATUS_TEMPERATURE, 2087 .gbit = PB_STATUS_TEMPERATURE, 2088 .limit = temp_limit_attrs, 2089 .nlimit = ARRAY_SIZE(temp_limit_attrs), 2090 }, { 2091 .reg = PMBUS_READ_TEMPERATURE_2, 2092 .class = PSC_TEMPERATURE, 2093 .paged = true, 2094 .update = true, 2095 .compare = true, 2096 .func = PMBUS_HAVE_TEMP2, 2097 .sfunc = PMBUS_HAVE_STATUS_TEMP, 2098 .sreg = PMBUS_STATUS_TEMPERATURE, 2099 .gbit = PB_STATUS_TEMPERATURE, 2100 .limit = temp_limit_attrs2, 2101 .nlimit = ARRAY_SIZE(temp_limit_attrs2), 2102 }, { 2103 .reg = PMBUS_READ_TEMPERATURE_3, 2104 .class = PSC_TEMPERATURE, 2105 .paged = true, 2106 .update = true, 2107 .compare = true, 2108 .func = PMBUS_HAVE_TEMP3, 2109 .sfunc = PMBUS_HAVE_STATUS_TEMP, 2110 .sreg = PMBUS_STATUS_TEMPERATURE, 2111 .gbit = PB_STATUS_TEMPERATURE, 2112 .limit = temp_limit_attrs3, 2113 .nlimit = ARRAY_SIZE(temp_limit_attrs3), 2114 } 2115 }; 2116 2117 static const int pmbus_fan_registers[] = { 2118 PMBUS_READ_FAN_SPEED_1, 2119 PMBUS_READ_FAN_SPEED_2, 2120 PMBUS_READ_FAN_SPEED_3, 2121 PMBUS_READ_FAN_SPEED_4 2122 }; 2123 2124 static const int pmbus_fan_status_registers[] = { 2125 PMBUS_STATUS_FAN_12, 2126 PMBUS_STATUS_FAN_12, 2127 PMBUS_STATUS_FAN_34, 2128 PMBUS_STATUS_FAN_34 2129 }; 2130 2131 static const u32 pmbus_fan_flags[] = { 2132 PMBUS_HAVE_FAN12, 2133 PMBUS_HAVE_FAN12, 2134 PMBUS_HAVE_FAN34, 2135 PMBUS_HAVE_FAN34 2136 }; 2137 2138 static const u32 pmbus_fan_status_flags[] = { 2139 PMBUS_HAVE_STATUS_FAN12, 2140 PMBUS_HAVE_STATUS_FAN12, 2141 PMBUS_HAVE_STATUS_FAN34, 2142 PMBUS_HAVE_STATUS_FAN34 2143 }; 2144 2145 /* Fans */ 2146 2147 /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */ 2148 static int pmbus_add_fan_ctrl(struct i2c_client *client, 2149 struct pmbus_data *data, int index, int page, int id, 2150 u8 config) 2151 { 2152 struct pmbus_sensor *sensor; 2153 2154 sensor = pmbus_add_sensor(data, "fan", "target", index, page, 2155 0xff, PMBUS_VIRT_FAN_TARGET_1 + id, PSC_FAN, 2156 false, false, true); 2157 2158 if (!sensor) 2159 return -ENOMEM; 2160 2161 if (!((data->info->func[page] & PMBUS_HAVE_PWM12) || 2162 (data->info->func[page] & PMBUS_HAVE_PWM34))) 2163 return 0; 2164 2165 sensor = pmbus_add_sensor(data, "pwm", NULL, index, page, 2166 0xff, PMBUS_VIRT_PWM_1 + id, PSC_PWM, 2167 false, false, true); 2168 2169 if (!sensor) 2170 return -ENOMEM; 2171 2172 sensor = pmbus_add_sensor(data, "pwm", "enable", index, page, 2173 0xff, PMBUS_VIRT_PWM_ENABLE_1 + id, PSC_PWM, 2174 true, false, false); 2175 2176 if (!sensor) 2177 return -ENOMEM; 2178 2179 return 0; 2180 } 2181 2182 static int pmbus_add_fan_attributes(struct i2c_client *client, 2183 struct pmbus_data *data) 2184 { 2185 const struct pmbus_driver_info *info = data->info; 2186 int index = 1; 2187 int page; 2188 int ret; 2189 2190 for (page = 0; page < info->pages; page++) { 2191 int f; 2192 2193 for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) { 2194 int regval; 2195 2196 if (!(info->func[page] & pmbus_fan_flags[f])) 2197 break; 2198 2199 if (!pmbus_check_word_register(client, page, 2200 pmbus_fan_registers[f])) 2201 break; 2202 2203 /* 2204 * Skip fan if not installed. 2205 * Each fan configuration register covers multiple fans, 2206 * so we have to do some magic. 2207 */ 2208 regval = _pmbus_read_byte_data(client, page, 2209 pmbus_fan_config_registers[f]); 2210 if (regval < 0 || 2211 (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4))))) 2212 continue; 2213 2214 if (pmbus_add_sensor(data, "fan", "input", index, 2215 page, 0xff, pmbus_fan_registers[f], 2216 PSC_FAN, true, true, true) == NULL) 2217 return -ENOMEM; 2218 2219 /* Fan control */ 2220 if (pmbus_check_word_register(client, page, 2221 pmbus_fan_command_registers[f])) { 2222 ret = pmbus_add_fan_ctrl(client, data, index, 2223 page, f, regval); 2224 if (ret < 0) 2225 return ret; 2226 } 2227 2228 /* 2229 * Each fan status register covers multiple fans, 2230 * so we have to do some magic. 2231 */ 2232 if ((info->func[page] & pmbus_fan_status_flags[f]) && 2233 pmbus_check_byte_register(client, 2234 page, pmbus_fan_status_registers[f])) { 2235 int reg; 2236 2237 if (f > 1) /* fan 3, 4 */ 2238 reg = PMBUS_STATUS_FAN_34; 2239 else 2240 reg = PMBUS_STATUS_FAN_12; 2241 ret = pmbus_add_boolean(data, "fan", 2242 "alarm", index, NULL, NULL, page, reg, 2243 PB_FAN_FAN1_WARNING >> (f & 1)); 2244 if (ret) 2245 return ret; 2246 ret = pmbus_add_boolean(data, "fan", 2247 "fault", index, NULL, NULL, page, reg, 2248 PB_FAN_FAN1_FAULT >> (f & 1)); 2249 if (ret) 2250 return ret; 2251 } 2252 index++; 2253 } 2254 } 2255 return 0; 2256 } 2257 2258 struct pmbus_samples_attr { 2259 int reg; 2260 char *name; 2261 }; 2262 2263 struct pmbus_samples_reg { 2264 int page; 2265 struct pmbus_samples_attr *attr; 2266 struct device_attribute dev_attr; 2267 }; 2268 2269 static struct pmbus_samples_attr pmbus_samples_registers[] = { 2270 { 2271 .reg = PMBUS_VIRT_SAMPLES, 2272 .name = "samples", 2273 }, { 2274 .reg = PMBUS_VIRT_IN_SAMPLES, 2275 .name = "in_samples", 2276 }, { 2277 .reg = PMBUS_VIRT_CURR_SAMPLES, 2278 .name = "curr_samples", 2279 }, { 2280 .reg = PMBUS_VIRT_POWER_SAMPLES, 2281 .name = "power_samples", 2282 }, { 2283 .reg = PMBUS_VIRT_TEMP_SAMPLES, 2284 .name = "temp_samples", 2285 } 2286 }; 2287 2288 #define to_samples_reg(x) container_of(x, struct pmbus_samples_reg, dev_attr) 2289 2290 static ssize_t pmbus_show_samples(struct device *dev, 2291 struct device_attribute *devattr, char *buf) 2292 { 2293 int val; 2294 struct i2c_client *client = to_i2c_client(dev->parent); 2295 struct pmbus_samples_reg *reg = to_samples_reg(devattr); 2296 struct pmbus_data *data = i2c_get_clientdata(client); 2297 2298 mutex_lock(&data->update_lock); 2299 val = _pmbus_read_word_data(client, reg->page, 0xff, reg->attr->reg); 2300 mutex_unlock(&data->update_lock); 2301 if (val < 0) 2302 return val; 2303 2304 return sysfs_emit(buf, "%d\n", val); 2305 } 2306 2307 static ssize_t pmbus_set_samples(struct device *dev, 2308 struct device_attribute *devattr, 2309 const char *buf, size_t count) 2310 { 2311 int ret; 2312 long val; 2313 struct i2c_client *client = to_i2c_client(dev->parent); 2314 struct pmbus_samples_reg *reg = to_samples_reg(devattr); 2315 struct pmbus_data *data = i2c_get_clientdata(client); 2316 2317 if (kstrtol(buf, 0, &val) < 0) 2318 return -EINVAL; 2319 2320 mutex_lock(&data->update_lock); 2321 ret = _pmbus_write_word_data(client, reg->page, reg->attr->reg, val); 2322 mutex_unlock(&data->update_lock); 2323 2324 return ret ? : count; 2325 } 2326 2327 static int pmbus_add_samples_attr(struct pmbus_data *data, int page, 2328 struct pmbus_samples_attr *attr) 2329 { 2330 struct pmbus_samples_reg *reg; 2331 2332 reg = devm_kzalloc(data->dev, sizeof(*reg), GFP_KERNEL); 2333 if (!reg) 2334 return -ENOMEM; 2335 2336 reg->attr = attr; 2337 reg->page = page; 2338 2339 pmbus_dev_attr_init(®->dev_attr, attr->name, 0644, 2340 pmbus_show_samples, pmbus_set_samples); 2341 2342 return pmbus_add_attribute(data, ®->dev_attr.attr); 2343 } 2344 2345 static int pmbus_add_samples_attributes(struct i2c_client *client, 2346 struct pmbus_data *data) 2347 { 2348 const struct pmbus_driver_info *info = data->info; 2349 int s; 2350 2351 if (!(info->func[0] & PMBUS_HAVE_SAMPLES)) 2352 return 0; 2353 2354 for (s = 0; s < ARRAY_SIZE(pmbus_samples_registers); s++) { 2355 struct pmbus_samples_attr *attr; 2356 int ret; 2357 2358 attr = &pmbus_samples_registers[s]; 2359 if (!pmbus_check_word_register(client, 0, attr->reg)) 2360 continue; 2361 2362 ret = pmbus_add_samples_attr(data, 0, attr); 2363 if (ret) 2364 return ret; 2365 } 2366 2367 return 0; 2368 } 2369 2370 static int pmbus_find_attributes(struct i2c_client *client, 2371 struct pmbus_data *data) 2372 { 2373 int ret; 2374 2375 /* Voltage sensors */ 2376 ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes, 2377 ARRAY_SIZE(voltage_attributes)); 2378 if (ret) 2379 return ret; 2380 2381 /* Current sensors */ 2382 ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes, 2383 ARRAY_SIZE(current_attributes)); 2384 if (ret) 2385 return ret; 2386 2387 /* Power sensors */ 2388 ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes, 2389 ARRAY_SIZE(power_attributes)); 2390 if (ret) 2391 return ret; 2392 2393 /* Temperature sensors */ 2394 ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes, 2395 ARRAY_SIZE(temp_attributes)); 2396 if (ret) 2397 return ret; 2398 2399 /* Fans */ 2400 ret = pmbus_add_fan_attributes(client, data); 2401 if (ret) 2402 return ret; 2403 2404 ret = pmbus_add_samples_attributes(client, data); 2405 return ret; 2406 } 2407 2408 /* 2409 * The pmbus_class_attr_map structure maps one sensor class to 2410 * it's corresponding sensor attributes array. 2411 */ 2412 struct pmbus_class_attr_map { 2413 enum pmbus_sensor_classes class; 2414 int nattr; 2415 const struct pmbus_sensor_attr *attr; 2416 }; 2417 2418 static const struct pmbus_class_attr_map class_attr_map[] = { 2419 { 2420 .class = PSC_VOLTAGE_IN, 2421 .attr = voltage_attributes, 2422 .nattr = ARRAY_SIZE(voltage_attributes), 2423 }, { 2424 .class = PSC_VOLTAGE_OUT, 2425 .attr = voltage_attributes, 2426 .nattr = ARRAY_SIZE(voltage_attributes), 2427 }, { 2428 .class = PSC_CURRENT_IN, 2429 .attr = current_attributes, 2430 .nattr = ARRAY_SIZE(current_attributes), 2431 }, { 2432 .class = PSC_CURRENT_OUT, 2433 .attr = current_attributes, 2434 .nattr = ARRAY_SIZE(current_attributes), 2435 }, { 2436 .class = PSC_POWER, 2437 .attr = power_attributes, 2438 .nattr = ARRAY_SIZE(power_attributes), 2439 }, { 2440 .class = PSC_TEMPERATURE, 2441 .attr = temp_attributes, 2442 .nattr = ARRAY_SIZE(temp_attributes), 2443 } 2444 }; 2445 2446 /* 2447 * Read the coefficients for direct mode. 2448 */ 2449 static int pmbus_read_coefficients(struct i2c_client *client, 2450 struct pmbus_driver_info *info, 2451 const struct pmbus_sensor_attr *attr) 2452 { 2453 int rv; 2454 union i2c_smbus_data data; 2455 enum pmbus_sensor_classes class = attr->class; 2456 s8 R; 2457 s16 m, b; 2458 2459 data.block[0] = 2; 2460 data.block[1] = attr->reg; 2461 data.block[2] = 0x01; 2462 2463 rv = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2464 I2C_SMBUS_WRITE, PMBUS_COEFFICIENTS, 2465 I2C_SMBUS_BLOCK_PROC_CALL, &data); 2466 2467 if (rv < 0) 2468 return rv; 2469 2470 if (data.block[0] != 5) 2471 return -EIO; 2472 2473 m = data.block[1] | (data.block[2] << 8); 2474 b = data.block[3] | (data.block[4] << 8); 2475 R = data.block[5]; 2476 info->m[class] = m; 2477 info->b[class] = b; 2478 info->R[class] = R; 2479 2480 return rv; 2481 } 2482 2483 static int pmbus_init_coefficients(struct i2c_client *client, 2484 struct pmbus_driver_info *info) 2485 { 2486 int i, n, ret = -EINVAL; 2487 const struct pmbus_class_attr_map *map; 2488 const struct pmbus_sensor_attr *attr; 2489 2490 for (i = 0; i < ARRAY_SIZE(class_attr_map); i++) { 2491 map = &class_attr_map[i]; 2492 if (info->format[map->class] != direct) 2493 continue; 2494 for (n = 0; n < map->nattr; n++) { 2495 attr = &map->attr[n]; 2496 if (map->class != attr->class) 2497 continue; 2498 ret = pmbus_read_coefficients(client, info, attr); 2499 if (ret >= 0) 2500 break; 2501 } 2502 if (ret < 0) { 2503 dev_err(&client->dev, 2504 "No coefficients found for sensor class %d\n", 2505 map->class); 2506 return -EINVAL; 2507 } 2508 } 2509 2510 return 0; 2511 } 2512 2513 /* 2514 * Identify chip parameters. 2515 * This function is called for all chips. 2516 */ 2517 static int pmbus_identify_common(struct i2c_client *client, 2518 struct pmbus_data *data, int page) 2519 { 2520 int vout_mode = -1; 2521 2522 if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE)) 2523 vout_mode = _pmbus_read_byte_data(client, page, 2524 PMBUS_VOUT_MODE); 2525 if (vout_mode >= 0 && vout_mode != 0xff) { 2526 /* 2527 * Not all chips support the VOUT_MODE command, 2528 * so a failure to read it is not an error. 2529 */ 2530 switch (vout_mode >> 5) { 2531 case 0: /* linear mode */ 2532 if (data->info->format[PSC_VOLTAGE_OUT] != linear) 2533 return -ENODEV; 2534 2535 data->exponent[page] = ((s8)(vout_mode << 3)) >> 3; 2536 break; 2537 case 1: /* VID mode */ 2538 if (data->info->format[PSC_VOLTAGE_OUT] != vid) 2539 return -ENODEV; 2540 break; 2541 case 2: /* direct mode */ 2542 if (data->info->format[PSC_VOLTAGE_OUT] != direct) 2543 return -ENODEV; 2544 break; 2545 case 3: /* ieee 754 half precision */ 2546 if (data->info->format[PSC_VOLTAGE_OUT] != ieee754) 2547 return -ENODEV; 2548 break; 2549 default: 2550 return -ENODEV; 2551 } 2552 } 2553 2554 return 0; 2555 } 2556 2557 static int pmbus_read_status_byte(struct i2c_client *client, int page) 2558 { 2559 return _pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE); 2560 } 2561 2562 static int pmbus_read_status_word(struct i2c_client *client, int page) 2563 { 2564 return _pmbus_read_word_data(client, page, 0xff, PMBUS_STATUS_WORD); 2565 } 2566 2567 /* PEC attribute support */ 2568 2569 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy, 2570 char *buf) 2571 { 2572 struct i2c_client *client = to_i2c_client(dev); 2573 2574 return sysfs_emit(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC)); 2575 } 2576 2577 static ssize_t pec_store(struct device *dev, struct device_attribute *dummy, 2578 const char *buf, size_t count) 2579 { 2580 struct i2c_client *client = to_i2c_client(dev); 2581 bool enable; 2582 int err; 2583 2584 err = kstrtobool(buf, &enable); 2585 if (err < 0) 2586 return err; 2587 2588 if (enable) 2589 client->flags |= I2C_CLIENT_PEC; 2590 else 2591 client->flags &= ~I2C_CLIENT_PEC; 2592 2593 return count; 2594 } 2595 2596 static DEVICE_ATTR_RW(pec); 2597 2598 static void pmbus_remove_pec(void *dev) 2599 { 2600 device_remove_file(dev, &dev_attr_pec); 2601 } 2602 2603 static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data, 2604 struct pmbus_driver_info *info) 2605 { 2606 struct device *dev = &client->dev; 2607 int page, ret; 2608 2609 /* 2610 * Figure out if PEC is enabled before accessing any other register. 2611 * Make sure PEC is disabled, will be enabled later if needed. 2612 */ 2613 client->flags &= ~I2C_CLIENT_PEC; 2614 2615 /* Enable PEC if the controller and bus supports it */ 2616 if (!(data->flags & PMBUS_NO_CAPABILITY)) { 2617 ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY); 2618 if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK)) { 2619 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC)) 2620 client->flags |= I2C_CLIENT_PEC; 2621 } 2622 } 2623 2624 /* 2625 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try 2626 * to use PMBUS_STATUS_BYTE instead if that is the case. 2627 * Bail out if both registers are not supported. 2628 */ 2629 data->read_status = pmbus_read_status_word; 2630 ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD); 2631 if (ret < 0 || ret == 0xffff) { 2632 data->read_status = pmbus_read_status_byte; 2633 ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE); 2634 if (ret < 0 || ret == 0xff) { 2635 dev_err(dev, "PMBus status register not found\n"); 2636 return -ENODEV; 2637 } 2638 } else { 2639 data->has_status_word = true; 2640 } 2641 2642 /* 2643 * Check if the chip is write protected. If it is, we can not clear 2644 * faults, and we should not try it. Also, in that case, writes into 2645 * limit registers need to be disabled. 2646 */ 2647 if (!(data->flags & PMBUS_NO_WRITE_PROTECT)) { 2648 ret = i2c_smbus_read_byte_data(client, PMBUS_WRITE_PROTECT); 2649 if (ret > 0 && (ret & PB_WP_ANY)) 2650 data->flags |= PMBUS_WRITE_PROTECTED | PMBUS_SKIP_STATUS_CHECK; 2651 } 2652 2653 if (data->info->pages) 2654 pmbus_clear_faults(client); 2655 else 2656 pmbus_clear_fault_page(client, -1); 2657 2658 if (info->identify) { 2659 ret = (*info->identify)(client, info); 2660 if (ret < 0) { 2661 dev_err(dev, "Chip identification failed\n"); 2662 return ret; 2663 } 2664 } 2665 2666 if (info->pages <= 0 || info->pages > PMBUS_PAGES) { 2667 dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages); 2668 return -ENODEV; 2669 } 2670 2671 for (page = 0; page < info->pages; page++) { 2672 ret = pmbus_identify_common(client, data, page); 2673 if (ret < 0) { 2674 dev_err(dev, "Failed to identify chip capabilities\n"); 2675 return ret; 2676 } 2677 } 2678 2679 if (data->flags & PMBUS_USE_COEFFICIENTS_CMD) { 2680 if (!i2c_check_functionality(client->adapter, 2681 I2C_FUNC_SMBUS_BLOCK_PROC_CALL)) 2682 return -ENODEV; 2683 2684 ret = pmbus_init_coefficients(client, info); 2685 if (ret < 0) 2686 return ret; 2687 } 2688 2689 if (client->flags & I2C_CLIENT_PEC) { 2690 /* 2691 * If I2C_CLIENT_PEC is set here, both the I2C adapter and the 2692 * chip support PEC. Add 'pec' attribute to client device to let 2693 * the user control it. 2694 */ 2695 ret = device_create_file(dev, &dev_attr_pec); 2696 if (ret) 2697 return ret; 2698 ret = devm_add_action_or_reset(dev, pmbus_remove_pec, dev); 2699 if (ret) 2700 return ret; 2701 } 2702 2703 return 0; 2704 } 2705 2706 /* A PMBus status flag and the corresponding REGULATOR_ERROR_* and REGULATOR_EVENTS_* flag */ 2707 struct pmbus_status_assoc { 2708 int pflag, rflag, eflag; 2709 }; 2710 2711 /* PMBus->regulator bit mappings for a PMBus status register */ 2712 struct pmbus_status_category { 2713 int func; 2714 int reg; 2715 const struct pmbus_status_assoc *bits; /* zero-terminated */ 2716 }; 2717 2718 static const struct pmbus_status_category __maybe_unused pmbus_status_flag_map[] = { 2719 { 2720 .func = PMBUS_HAVE_STATUS_VOUT, 2721 .reg = PMBUS_STATUS_VOUT, 2722 .bits = (const struct pmbus_status_assoc[]) { 2723 { PB_VOLTAGE_UV_WARNING, REGULATOR_ERROR_UNDER_VOLTAGE_WARN, 2724 REGULATOR_EVENT_UNDER_VOLTAGE_WARN }, 2725 { PB_VOLTAGE_UV_FAULT, REGULATOR_ERROR_UNDER_VOLTAGE, 2726 REGULATOR_EVENT_UNDER_VOLTAGE }, 2727 { PB_VOLTAGE_OV_WARNING, REGULATOR_ERROR_OVER_VOLTAGE_WARN, 2728 REGULATOR_EVENT_OVER_VOLTAGE_WARN }, 2729 { PB_VOLTAGE_OV_FAULT, REGULATOR_ERROR_REGULATION_OUT, 2730 REGULATOR_EVENT_OVER_VOLTAGE_WARN }, 2731 { }, 2732 }, 2733 }, { 2734 .func = PMBUS_HAVE_STATUS_IOUT, 2735 .reg = PMBUS_STATUS_IOUT, 2736 .bits = (const struct pmbus_status_assoc[]) { 2737 { PB_IOUT_OC_WARNING, REGULATOR_ERROR_OVER_CURRENT_WARN, 2738 REGULATOR_EVENT_OVER_CURRENT_WARN }, 2739 { PB_IOUT_OC_FAULT, REGULATOR_ERROR_OVER_CURRENT, 2740 REGULATOR_EVENT_OVER_CURRENT }, 2741 { PB_IOUT_OC_LV_FAULT, REGULATOR_ERROR_OVER_CURRENT, 2742 REGULATOR_EVENT_OVER_CURRENT }, 2743 { }, 2744 }, 2745 }, { 2746 .func = PMBUS_HAVE_STATUS_TEMP, 2747 .reg = PMBUS_STATUS_TEMPERATURE, 2748 .bits = (const struct pmbus_status_assoc[]) { 2749 { PB_TEMP_OT_WARNING, REGULATOR_ERROR_OVER_TEMP_WARN, 2750 REGULATOR_EVENT_OVER_TEMP_WARN }, 2751 { PB_TEMP_OT_FAULT, REGULATOR_ERROR_OVER_TEMP, 2752 REGULATOR_EVENT_OVER_TEMP }, 2753 { }, 2754 }, 2755 }, 2756 }; 2757 2758 static int _pmbus_is_enabled(struct i2c_client *client, u8 page) 2759 { 2760 int ret; 2761 2762 ret = _pmbus_read_byte_data(client, page, PMBUS_OPERATION); 2763 2764 if (ret < 0) 2765 return ret; 2766 2767 return !!(ret & PB_OPERATION_CONTROL_ON); 2768 } 2769 2770 static int __maybe_unused pmbus_is_enabled(struct i2c_client *client, u8 page) 2771 { 2772 struct pmbus_data *data = i2c_get_clientdata(client); 2773 int ret; 2774 2775 mutex_lock(&data->update_lock); 2776 ret = _pmbus_is_enabled(client, page); 2777 mutex_unlock(&data->update_lock); 2778 2779 return ret; 2780 } 2781 2782 #define to_dev_attr(_dev_attr) \ 2783 container_of(_dev_attr, struct device_attribute, attr) 2784 2785 static void pmbus_notify(struct pmbus_data *data, int page, int reg, int flags) 2786 { 2787 int i; 2788 2789 for (i = 0; i < data->num_attributes; i++) { 2790 struct device_attribute *da = to_dev_attr(data->group.attrs[i]); 2791 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 2792 int index = attr->index; 2793 u16 smask = pb_index_to_mask(index); 2794 u8 spage = pb_index_to_page(index); 2795 u16 sreg = pb_index_to_reg(index); 2796 2797 if (reg == sreg && page == spage && (smask & flags)) { 2798 dev_dbg(data->dev, "sysfs notify: %s", da->attr.name); 2799 sysfs_notify(&data->dev->kobj, NULL, da->attr.name); 2800 kobject_uevent(&data->dev->kobj, KOBJ_CHANGE); 2801 flags &= ~smask; 2802 } 2803 2804 if (!flags) 2805 break; 2806 } 2807 } 2808 2809 static int _pmbus_get_flags(struct pmbus_data *data, u8 page, unsigned int *flags, 2810 unsigned int *event, bool notify) 2811 { 2812 int i, status; 2813 const struct pmbus_status_category *cat; 2814 const struct pmbus_status_assoc *bit; 2815 struct device *dev = data->dev; 2816 struct i2c_client *client = to_i2c_client(dev); 2817 int func = data->info->func[page]; 2818 2819 *flags = 0; 2820 *event = 0; 2821 2822 for (i = 0; i < ARRAY_SIZE(pmbus_status_flag_map); i++) { 2823 cat = &pmbus_status_flag_map[i]; 2824 if (!(func & cat->func)) 2825 continue; 2826 2827 status = _pmbus_read_byte_data(client, page, cat->reg); 2828 if (status < 0) 2829 return status; 2830 2831 for (bit = cat->bits; bit->pflag; bit++) 2832 if (status & bit->pflag) { 2833 *flags |= bit->rflag; 2834 *event |= bit->eflag; 2835 } 2836 2837 if (notify && status) 2838 pmbus_notify(data, page, cat->reg, status); 2839 2840 } 2841 2842 /* 2843 * Map what bits of STATUS_{WORD,BYTE} we can to REGULATOR_ERROR_* 2844 * bits. Some of the other bits are tempting (especially for cases 2845 * where we don't have the relevant PMBUS_HAVE_STATUS_* 2846 * functionality), but there's an unfortunate ambiguity in that 2847 * they're defined as indicating a fault *or* a warning, so we can't 2848 * easily determine whether to report REGULATOR_ERROR_<foo> or 2849 * REGULATOR_ERROR_<foo>_WARN. 2850 */ 2851 status = pmbus_get_status(client, page, PMBUS_STATUS_WORD); 2852 if (status < 0) 2853 return status; 2854 2855 if (_pmbus_is_enabled(client, page)) { 2856 if (status & PB_STATUS_OFF) { 2857 *flags |= REGULATOR_ERROR_FAIL; 2858 *event |= REGULATOR_EVENT_FAIL; 2859 } 2860 2861 if (status & PB_STATUS_POWER_GOOD_N) { 2862 *flags |= REGULATOR_ERROR_REGULATION_OUT; 2863 *event |= REGULATOR_EVENT_REGULATION_OUT; 2864 } 2865 } 2866 /* 2867 * Unlike most other status bits, PB_STATUS_{IOUT_OC,VOUT_OV} are 2868 * defined strictly as fault indicators (not warnings). 2869 */ 2870 if (status & PB_STATUS_IOUT_OC) { 2871 *flags |= REGULATOR_ERROR_OVER_CURRENT; 2872 *event |= REGULATOR_EVENT_OVER_CURRENT; 2873 } 2874 if (status & PB_STATUS_VOUT_OV) { 2875 *flags |= REGULATOR_ERROR_REGULATION_OUT; 2876 *event |= REGULATOR_EVENT_FAIL; 2877 } 2878 2879 /* 2880 * If we haven't discovered any thermal faults or warnings via 2881 * PMBUS_STATUS_TEMPERATURE, map PB_STATUS_TEMPERATURE to a warning as 2882 * a (conservative) best-effort interpretation. 2883 */ 2884 if (!(*flags & (REGULATOR_ERROR_OVER_TEMP | REGULATOR_ERROR_OVER_TEMP_WARN)) && 2885 (status & PB_STATUS_TEMPERATURE)) { 2886 *flags |= REGULATOR_ERROR_OVER_TEMP_WARN; 2887 *event |= REGULATOR_EVENT_OVER_TEMP_WARN; 2888 } 2889 2890 2891 return 0; 2892 } 2893 2894 static int __maybe_unused pmbus_get_flags(struct pmbus_data *data, u8 page, unsigned int *flags, 2895 unsigned int *event, bool notify) 2896 { 2897 int ret; 2898 2899 mutex_lock(&data->update_lock); 2900 ret = _pmbus_get_flags(data, page, flags, event, notify); 2901 mutex_unlock(&data->update_lock); 2902 2903 return ret; 2904 } 2905 2906 #if IS_ENABLED(CONFIG_REGULATOR) 2907 static int pmbus_regulator_is_enabled(struct regulator_dev *rdev) 2908 { 2909 struct device *dev = rdev_get_dev(rdev); 2910 struct i2c_client *client = to_i2c_client(dev->parent); 2911 2912 return pmbus_is_enabled(client, rdev_get_id(rdev)); 2913 } 2914 2915 static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable) 2916 { 2917 struct device *dev = rdev_get_dev(rdev); 2918 struct i2c_client *client = to_i2c_client(dev->parent); 2919 struct pmbus_data *data = i2c_get_clientdata(client); 2920 u8 page = rdev_get_id(rdev); 2921 int ret; 2922 2923 mutex_lock(&data->update_lock); 2924 ret = pmbus_update_byte_data(client, page, PMBUS_OPERATION, 2925 PB_OPERATION_CONTROL_ON, 2926 enable ? PB_OPERATION_CONTROL_ON : 0); 2927 mutex_unlock(&data->update_lock); 2928 2929 return ret; 2930 } 2931 2932 static int pmbus_regulator_enable(struct regulator_dev *rdev) 2933 { 2934 return _pmbus_regulator_on_off(rdev, 1); 2935 } 2936 2937 static int pmbus_regulator_disable(struct regulator_dev *rdev) 2938 { 2939 return _pmbus_regulator_on_off(rdev, 0); 2940 } 2941 2942 static int pmbus_regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags) 2943 { 2944 struct device *dev = rdev_get_dev(rdev); 2945 struct i2c_client *client = to_i2c_client(dev->parent); 2946 struct pmbus_data *data = i2c_get_clientdata(client); 2947 int event; 2948 2949 return pmbus_get_flags(data, rdev_get_id(rdev), flags, &event, false); 2950 } 2951 2952 static int pmbus_regulator_get_status(struct regulator_dev *rdev) 2953 { 2954 struct device *dev = rdev_get_dev(rdev); 2955 struct i2c_client *client = to_i2c_client(dev->parent); 2956 struct pmbus_data *data = i2c_get_clientdata(client); 2957 u8 page = rdev_get_id(rdev); 2958 int status, ret; 2959 int event; 2960 2961 mutex_lock(&data->update_lock); 2962 status = pmbus_get_status(client, page, PMBUS_STATUS_WORD); 2963 if (status < 0) { 2964 ret = status; 2965 goto unlock; 2966 } 2967 2968 if (status & PB_STATUS_OFF) { 2969 ret = REGULATOR_STATUS_OFF; 2970 goto unlock; 2971 } 2972 2973 /* If regulator is ON & reports power good then return ON */ 2974 if (!(status & PB_STATUS_POWER_GOOD_N)) { 2975 ret = REGULATOR_STATUS_ON; 2976 goto unlock; 2977 } 2978 2979 ret = _pmbus_get_flags(data, rdev_get_id(rdev), &status, &event, false); 2980 if (ret) 2981 goto unlock; 2982 2983 if (status & (REGULATOR_ERROR_UNDER_VOLTAGE | REGULATOR_ERROR_OVER_CURRENT | 2984 REGULATOR_ERROR_REGULATION_OUT | REGULATOR_ERROR_FAIL | REGULATOR_ERROR_OVER_TEMP)) { 2985 ret = REGULATOR_STATUS_ERROR; 2986 goto unlock; 2987 } 2988 2989 ret = REGULATOR_STATUS_UNDEFINED; 2990 2991 unlock: 2992 mutex_unlock(&data->update_lock); 2993 return ret; 2994 } 2995 2996 static int pmbus_regulator_get_low_margin(struct i2c_client *client, int page) 2997 { 2998 struct pmbus_data *data = i2c_get_clientdata(client); 2999 struct pmbus_sensor s = { 3000 .page = page, 3001 .class = PSC_VOLTAGE_OUT, 3002 .convert = true, 3003 .data = -1, 3004 }; 3005 3006 if (data->vout_low[page] < 0) { 3007 if (pmbus_check_word_register(client, page, PMBUS_MFR_VOUT_MIN)) 3008 s.data = _pmbus_read_word_data(client, page, 0xff, 3009 PMBUS_MFR_VOUT_MIN); 3010 if (s.data < 0) { 3011 s.data = _pmbus_read_word_data(client, page, 0xff, 3012 PMBUS_VOUT_MARGIN_LOW); 3013 if (s.data < 0) 3014 return s.data; 3015 } 3016 data->vout_low[page] = pmbus_reg2data(data, &s); 3017 } 3018 3019 return data->vout_low[page]; 3020 } 3021 3022 static int pmbus_regulator_get_high_margin(struct i2c_client *client, int page) 3023 { 3024 struct pmbus_data *data = i2c_get_clientdata(client); 3025 struct pmbus_sensor s = { 3026 .page = page, 3027 .class = PSC_VOLTAGE_OUT, 3028 .convert = true, 3029 .data = -1, 3030 }; 3031 3032 if (data->vout_high[page] < 0) { 3033 if (pmbus_check_word_register(client, page, PMBUS_MFR_VOUT_MAX)) 3034 s.data = _pmbus_read_word_data(client, page, 0xff, 3035 PMBUS_MFR_VOUT_MAX); 3036 if (s.data < 0) { 3037 s.data = _pmbus_read_word_data(client, page, 0xff, 3038 PMBUS_VOUT_MARGIN_HIGH); 3039 if (s.data < 0) 3040 return s.data; 3041 } 3042 data->vout_high[page] = pmbus_reg2data(data, &s); 3043 } 3044 3045 return data->vout_high[page]; 3046 } 3047 3048 static int pmbus_regulator_get_voltage(struct regulator_dev *rdev) 3049 { 3050 struct device *dev = rdev_get_dev(rdev); 3051 struct i2c_client *client = to_i2c_client(dev->parent); 3052 struct pmbus_data *data = i2c_get_clientdata(client); 3053 struct pmbus_sensor s = { 3054 .page = rdev_get_id(rdev), 3055 .class = PSC_VOLTAGE_OUT, 3056 .convert = true, 3057 }; 3058 3059 s.data = _pmbus_read_word_data(client, s.page, 0xff, PMBUS_READ_VOUT); 3060 if (s.data < 0) 3061 return s.data; 3062 3063 return (int)pmbus_reg2data(data, &s) * 1000; /* unit is uV */ 3064 } 3065 3066 static int pmbus_regulator_set_voltage(struct regulator_dev *rdev, int min_uv, 3067 int max_uv, unsigned int *selector) 3068 { 3069 struct device *dev = rdev_get_dev(rdev); 3070 struct i2c_client *client = to_i2c_client(dev->parent); 3071 struct pmbus_data *data = i2c_get_clientdata(client); 3072 struct pmbus_sensor s = { 3073 .page = rdev_get_id(rdev), 3074 .class = PSC_VOLTAGE_OUT, 3075 .convert = true, 3076 .data = -1, 3077 }; 3078 int val = DIV_ROUND_CLOSEST(min_uv, 1000); /* convert to mV */ 3079 int low, high; 3080 3081 *selector = 0; 3082 3083 low = pmbus_regulator_get_low_margin(client, s.page); 3084 if (low < 0) 3085 return low; 3086 3087 high = pmbus_regulator_get_high_margin(client, s.page); 3088 if (high < 0) 3089 return high; 3090 3091 /* Make sure we are within margins */ 3092 if (low > val) 3093 val = low; 3094 if (high < val) 3095 val = high; 3096 3097 val = pmbus_data2reg(data, &s, val); 3098 3099 return _pmbus_write_word_data(client, s.page, PMBUS_VOUT_COMMAND, (u16)val); 3100 } 3101 3102 static int pmbus_regulator_list_voltage(struct regulator_dev *rdev, 3103 unsigned int selector) 3104 { 3105 struct device *dev = rdev_get_dev(rdev); 3106 struct i2c_client *client = to_i2c_client(dev->parent); 3107 int val, low, high; 3108 3109 if (selector >= rdev->desc->n_voltages || 3110 selector < rdev->desc->linear_min_sel) 3111 return -EINVAL; 3112 3113 selector -= rdev->desc->linear_min_sel; 3114 val = DIV_ROUND_CLOSEST(rdev->desc->min_uV + 3115 (rdev->desc->uV_step * selector), 1000); /* convert to mV */ 3116 3117 low = pmbus_regulator_get_low_margin(client, rdev_get_id(rdev)); 3118 if (low < 0) 3119 return low; 3120 3121 high = pmbus_regulator_get_high_margin(client, rdev_get_id(rdev)); 3122 if (high < 0) 3123 return high; 3124 3125 if (val >= low && val <= high) 3126 return val * 1000; /* unit is uV */ 3127 3128 return 0; 3129 } 3130 3131 const struct regulator_ops pmbus_regulator_ops = { 3132 .enable = pmbus_regulator_enable, 3133 .disable = pmbus_regulator_disable, 3134 .is_enabled = pmbus_regulator_is_enabled, 3135 .get_error_flags = pmbus_regulator_get_error_flags, 3136 .get_status = pmbus_regulator_get_status, 3137 .get_voltage = pmbus_regulator_get_voltage, 3138 .set_voltage = pmbus_regulator_set_voltage, 3139 .list_voltage = pmbus_regulator_list_voltage, 3140 }; 3141 EXPORT_SYMBOL_NS_GPL(pmbus_regulator_ops, PMBUS); 3142 3143 static int pmbus_regulator_register(struct pmbus_data *data) 3144 { 3145 struct device *dev = data->dev; 3146 const struct pmbus_driver_info *info = data->info; 3147 const struct pmbus_platform_data *pdata = dev_get_platdata(dev); 3148 int i; 3149 3150 data->rdevs = devm_kzalloc(dev, sizeof(struct regulator_dev *) * info->num_regulators, 3151 GFP_KERNEL); 3152 if (!data->rdevs) 3153 return -ENOMEM; 3154 3155 for (i = 0; i < info->num_regulators; i++) { 3156 struct regulator_config config = { }; 3157 3158 config.dev = dev; 3159 config.driver_data = data; 3160 3161 if (pdata && pdata->reg_init_data) 3162 config.init_data = &pdata->reg_init_data[i]; 3163 3164 data->rdevs[i] = devm_regulator_register(dev, &info->reg_desc[i], 3165 &config); 3166 if (IS_ERR(data->rdevs[i])) 3167 return dev_err_probe(dev, PTR_ERR(data->rdevs[i]), 3168 "Failed to register %s regulator\n", 3169 info->reg_desc[i].name); 3170 } 3171 3172 return 0; 3173 } 3174 3175 static int pmbus_regulator_notify(struct pmbus_data *data, int page, int event) 3176 { 3177 int j; 3178 3179 for (j = 0; j < data->info->num_regulators; j++) { 3180 if (page == rdev_get_id(data->rdevs[j])) { 3181 regulator_notifier_call_chain(data->rdevs[j], event, NULL); 3182 break; 3183 } 3184 } 3185 return 0; 3186 } 3187 #else 3188 static int pmbus_regulator_register(struct pmbus_data *data) 3189 { 3190 return 0; 3191 } 3192 3193 static int pmbus_regulator_notify(struct pmbus_data *data, int page, int event) 3194 { 3195 return 0; 3196 } 3197 #endif 3198 3199 static int pmbus_write_smbalert_mask(struct i2c_client *client, u8 page, u8 reg, u8 val) 3200 { 3201 return pmbus_write_word_data(client, page, PMBUS_SMBALERT_MASK, reg | (val << 8)); 3202 } 3203 3204 static irqreturn_t pmbus_fault_handler(int irq, void *pdata) 3205 { 3206 struct pmbus_data *data = pdata; 3207 struct i2c_client *client = to_i2c_client(data->dev); 3208 3209 int i, status, event; 3210 mutex_lock(&data->update_lock); 3211 for (i = 0; i < data->info->pages; i++) { 3212 _pmbus_get_flags(data, i, &status, &event, true); 3213 3214 if (event) 3215 pmbus_regulator_notify(data, i, event); 3216 } 3217 3218 pmbus_clear_faults(client); 3219 mutex_unlock(&data->update_lock); 3220 3221 return IRQ_HANDLED; 3222 } 3223 3224 static int pmbus_irq_setup(struct i2c_client *client, struct pmbus_data *data) 3225 { 3226 struct device *dev = &client->dev; 3227 const struct pmbus_status_category *cat; 3228 const struct pmbus_status_assoc *bit; 3229 int i, j, err, func; 3230 u8 mask; 3231 3232 static const u8 misc_status[] = {PMBUS_STATUS_CML, PMBUS_STATUS_OTHER, 3233 PMBUS_STATUS_MFR_SPECIFIC, PMBUS_STATUS_FAN_12, 3234 PMBUS_STATUS_FAN_34}; 3235 3236 if (!client->irq) 3237 return 0; 3238 3239 for (i = 0; i < data->info->pages; i++) { 3240 func = data->info->func[i]; 3241 3242 for (j = 0; j < ARRAY_SIZE(pmbus_status_flag_map); j++) { 3243 cat = &pmbus_status_flag_map[j]; 3244 if (!(func & cat->func)) 3245 continue; 3246 mask = 0; 3247 for (bit = cat->bits; bit->pflag; bit++) 3248 mask |= bit->pflag; 3249 3250 err = pmbus_write_smbalert_mask(client, i, cat->reg, ~mask); 3251 if (err) 3252 dev_dbg_once(dev, "Failed to set smbalert for reg 0x%02x\n", 3253 cat->reg); 3254 } 3255 3256 for (j = 0; j < ARRAY_SIZE(misc_status); j++) 3257 pmbus_write_smbalert_mask(client, i, misc_status[j], 0xff); 3258 } 3259 3260 /* Register notifiers */ 3261 err = devm_request_threaded_irq(dev, client->irq, NULL, pmbus_fault_handler, 3262 IRQF_ONESHOT, "pmbus-irq", data); 3263 if (err) { 3264 dev_err(dev, "failed to request an irq %d\n", err); 3265 return err; 3266 } 3267 3268 return 0; 3269 } 3270 3271 static struct dentry *pmbus_debugfs_dir; /* pmbus debugfs directory */ 3272 3273 #if IS_ENABLED(CONFIG_DEBUG_FS) 3274 static int pmbus_debugfs_get(void *data, u64 *val) 3275 { 3276 int rc; 3277 struct pmbus_debugfs_entry *entry = data; 3278 struct pmbus_data *pdata = i2c_get_clientdata(entry->client); 3279 3280 rc = mutex_lock_interruptible(&pdata->update_lock); 3281 if (rc) 3282 return rc; 3283 rc = _pmbus_read_byte_data(entry->client, entry->page, entry->reg); 3284 mutex_unlock(&pdata->update_lock); 3285 if (rc < 0) 3286 return rc; 3287 3288 *val = rc; 3289 3290 return 0; 3291 } 3292 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL, 3293 "0x%02llx\n"); 3294 3295 static int pmbus_debugfs_get_status(void *data, u64 *val) 3296 { 3297 int rc; 3298 struct pmbus_debugfs_entry *entry = data; 3299 struct pmbus_data *pdata = i2c_get_clientdata(entry->client); 3300 3301 rc = mutex_lock_interruptible(&pdata->update_lock); 3302 if (rc) 3303 return rc; 3304 rc = pdata->read_status(entry->client, entry->page); 3305 mutex_unlock(&pdata->update_lock); 3306 if (rc < 0) 3307 return rc; 3308 3309 *val = rc; 3310 3311 return 0; 3312 } 3313 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status, 3314 NULL, "0x%04llx\n"); 3315 3316 static ssize_t pmbus_debugfs_mfr_read(struct file *file, char __user *buf, 3317 size_t count, loff_t *ppos) 3318 { 3319 int rc; 3320 struct pmbus_debugfs_entry *entry = file->private_data; 3321 struct pmbus_data *pdata = i2c_get_clientdata(entry->client); 3322 char data[I2C_SMBUS_BLOCK_MAX + 2] = { 0 }; 3323 3324 rc = mutex_lock_interruptible(&pdata->update_lock); 3325 if (rc) 3326 return rc; 3327 rc = pmbus_read_block_data(entry->client, entry->page, entry->reg, 3328 data); 3329 mutex_unlock(&pdata->update_lock); 3330 if (rc < 0) 3331 return rc; 3332 3333 /* Add newline at the end of a read data */ 3334 data[rc] = '\n'; 3335 3336 /* Include newline into the length */ 3337 rc += 1; 3338 3339 return simple_read_from_buffer(buf, count, ppos, data, rc); 3340 } 3341 3342 static const struct file_operations pmbus_debugfs_ops_mfr = { 3343 .llseek = noop_llseek, 3344 .read = pmbus_debugfs_mfr_read, 3345 .write = NULL, 3346 .open = simple_open, 3347 }; 3348 3349 static void pmbus_remove_debugfs(void *data) 3350 { 3351 struct dentry *entry = data; 3352 3353 debugfs_remove_recursive(entry); 3354 } 3355 3356 static int pmbus_init_debugfs(struct i2c_client *client, 3357 struct pmbus_data *data) 3358 { 3359 int i, idx = 0; 3360 char name[PMBUS_NAME_SIZE]; 3361 struct pmbus_debugfs_entry *entries; 3362 3363 if (!pmbus_debugfs_dir) 3364 return -ENODEV; 3365 3366 /* 3367 * Create the debugfs directory for this device. Use the hwmon device 3368 * name to avoid conflicts (hwmon numbers are globally unique). 3369 */ 3370 data->debugfs = debugfs_create_dir(dev_name(data->hwmon_dev), 3371 pmbus_debugfs_dir); 3372 if (IS_ERR_OR_NULL(data->debugfs)) { 3373 data->debugfs = NULL; 3374 return -ENODEV; 3375 } 3376 3377 /* 3378 * Allocate the max possible entries we need. 3379 * 6 entries device-specific 3380 * 10 entries page-specific 3381 */ 3382 entries = devm_kcalloc(data->dev, 3383 6 + data->info->pages * 10, sizeof(*entries), 3384 GFP_KERNEL); 3385 if (!entries) 3386 return -ENOMEM; 3387 3388 /* 3389 * Add device-specific entries. 3390 * Please note that the PMBUS standard allows all registers to be 3391 * page-specific. 3392 * To reduce the number of debugfs entries for devices with many pages 3393 * assume that values of the following registers are the same for all 3394 * pages and report values only for page 0. 3395 */ 3396 if (pmbus_check_block_register(client, 0, PMBUS_MFR_ID)) { 3397 entries[idx].client = client; 3398 entries[idx].page = 0; 3399 entries[idx].reg = PMBUS_MFR_ID; 3400 debugfs_create_file("mfr_id", 0444, data->debugfs, 3401 &entries[idx++], 3402 &pmbus_debugfs_ops_mfr); 3403 } 3404 3405 if (pmbus_check_block_register(client, 0, PMBUS_MFR_MODEL)) { 3406 entries[idx].client = client; 3407 entries[idx].page = 0; 3408 entries[idx].reg = PMBUS_MFR_MODEL; 3409 debugfs_create_file("mfr_model", 0444, data->debugfs, 3410 &entries[idx++], 3411 &pmbus_debugfs_ops_mfr); 3412 } 3413 3414 if (pmbus_check_block_register(client, 0, PMBUS_MFR_REVISION)) { 3415 entries[idx].client = client; 3416 entries[idx].page = 0; 3417 entries[idx].reg = PMBUS_MFR_REVISION; 3418 debugfs_create_file("mfr_revision", 0444, data->debugfs, 3419 &entries[idx++], 3420 &pmbus_debugfs_ops_mfr); 3421 } 3422 3423 if (pmbus_check_block_register(client, 0, PMBUS_MFR_LOCATION)) { 3424 entries[idx].client = client; 3425 entries[idx].page = 0; 3426 entries[idx].reg = PMBUS_MFR_LOCATION; 3427 debugfs_create_file("mfr_location", 0444, data->debugfs, 3428 &entries[idx++], 3429 &pmbus_debugfs_ops_mfr); 3430 } 3431 3432 if (pmbus_check_block_register(client, 0, PMBUS_MFR_DATE)) { 3433 entries[idx].client = client; 3434 entries[idx].page = 0; 3435 entries[idx].reg = PMBUS_MFR_DATE; 3436 debugfs_create_file("mfr_date", 0444, data->debugfs, 3437 &entries[idx++], 3438 &pmbus_debugfs_ops_mfr); 3439 } 3440 3441 if (pmbus_check_block_register(client, 0, PMBUS_MFR_SERIAL)) { 3442 entries[idx].client = client; 3443 entries[idx].page = 0; 3444 entries[idx].reg = PMBUS_MFR_SERIAL; 3445 debugfs_create_file("mfr_serial", 0444, data->debugfs, 3446 &entries[idx++], 3447 &pmbus_debugfs_ops_mfr); 3448 } 3449 3450 /* Add page specific entries */ 3451 for (i = 0; i < data->info->pages; ++i) { 3452 /* Check accessibility of status register if it's not page 0 */ 3453 if (!i || pmbus_check_status_register(client, i)) { 3454 /* No need to set reg as we have special read op. */ 3455 entries[idx].client = client; 3456 entries[idx].page = i; 3457 scnprintf(name, PMBUS_NAME_SIZE, "status%d", i); 3458 debugfs_create_file(name, 0444, data->debugfs, 3459 &entries[idx++], 3460 &pmbus_debugfs_ops_status); 3461 } 3462 3463 if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) { 3464 entries[idx].client = client; 3465 entries[idx].page = i; 3466 entries[idx].reg = PMBUS_STATUS_VOUT; 3467 scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i); 3468 debugfs_create_file(name, 0444, data->debugfs, 3469 &entries[idx++], 3470 &pmbus_debugfs_ops); 3471 } 3472 3473 if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) { 3474 entries[idx].client = client; 3475 entries[idx].page = i; 3476 entries[idx].reg = PMBUS_STATUS_IOUT; 3477 scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i); 3478 debugfs_create_file(name, 0444, data->debugfs, 3479 &entries[idx++], 3480 &pmbus_debugfs_ops); 3481 } 3482 3483 if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) { 3484 entries[idx].client = client; 3485 entries[idx].page = i; 3486 entries[idx].reg = PMBUS_STATUS_INPUT; 3487 scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i); 3488 debugfs_create_file(name, 0444, data->debugfs, 3489 &entries[idx++], 3490 &pmbus_debugfs_ops); 3491 } 3492 3493 if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) { 3494 entries[idx].client = client; 3495 entries[idx].page = i; 3496 entries[idx].reg = PMBUS_STATUS_TEMPERATURE; 3497 scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i); 3498 debugfs_create_file(name, 0444, data->debugfs, 3499 &entries[idx++], 3500 &pmbus_debugfs_ops); 3501 } 3502 3503 if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) { 3504 entries[idx].client = client; 3505 entries[idx].page = i; 3506 entries[idx].reg = PMBUS_STATUS_CML; 3507 scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i); 3508 debugfs_create_file(name, 0444, data->debugfs, 3509 &entries[idx++], 3510 &pmbus_debugfs_ops); 3511 } 3512 3513 if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) { 3514 entries[idx].client = client; 3515 entries[idx].page = i; 3516 entries[idx].reg = PMBUS_STATUS_OTHER; 3517 scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i); 3518 debugfs_create_file(name, 0444, data->debugfs, 3519 &entries[idx++], 3520 &pmbus_debugfs_ops); 3521 } 3522 3523 if (pmbus_check_byte_register(client, i, 3524 PMBUS_STATUS_MFR_SPECIFIC)) { 3525 entries[idx].client = client; 3526 entries[idx].page = i; 3527 entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC; 3528 scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i); 3529 debugfs_create_file(name, 0444, data->debugfs, 3530 &entries[idx++], 3531 &pmbus_debugfs_ops); 3532 } 3533 3534 if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) { 3535 entries[idx].client = client; 3536 entries[idx].page = i; 3537 entries[idx].reg = PMBUS_STATUS_FAN_12; 3538 scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i); 3539 debugfs_create_file(name, 0444, data->debugfs, 3540 &entries[idx++], 3541 &pmbus_debugfs_ops); 3542 } 3543 3544 if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) { 3545 entries[idx].client = client; 3546 entries[idx].page = i; 3547 entries[idx].reg = PMBUS_STATUS_FAN_34; 3548 scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i); 3549 debugfs_create_file(name, 0444, data->debugfs, 3550 &entries[idx++], 3551 &pmbus_debugfs_ops); 3552 } 3553 } 3554 3555 return devm_add_action_or_reset(data->dev, 3556 pmbus_remove_debugfs, data->debugfs); 3557 } 3558 #else 3559 static int pmbus_init_debugfs(struct i2c_client *client, 3560 struct pmbus_data *data) 3561 { 3562 return 0; 3563 } 3564 #endif /* IS_ENABLED(CONFIG_DEBUG_FS) */ 3565 3566 int pmbus_do_probe(struct i2c_client *client, struct pmbus_driver_info *info) 3567 { 3568 struct device *dev = &client->dev; 3569 const struct pmbus_platform_data *pdata = dev_get_platdata(dev); 3570 struct pmbus_data *data; 3571 size_t groups_num = 0; 3572 int ret; 3573 int i; 3574 char *name; 3575 3576 if (!info) 3577 return -ENODEV; 3578 3579 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE 3580 | I2C_FUNC_SMBUS_BYTE_DATA 3581 | I2C_FUNC_SMBUS_WORD_DATA)) 3582 return -ENODEV; 3583 3584 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 3585 if (!data) 3586 return -ENOMEM; 3587 3588 if (info->groups) 3589 while (info->groups[groups_num]) 3590 groups_num++; 3591 3592 data->groups = devm_kcalloc(dev, groups_num + 2, sizeof(void *), 3593 GFP_KERNEL); 3594 if (!data->groups) 3595 return -ENOMEM; 3596 3597 i2c_set_clientdata(client, data); 3598 mutex_init(&data->update_lock); 3599 data->dev = dev; 3600 3601 if (pdata) 3602 data->flags = pdata->flags; 3603 data->info = info; 3604 data->currpage = -1; 3605 data->currphase = -1; 3606 3607 for (i = 0; i < ARRAY_SIZE(data->vout_low); i++) { 3608 data->vout_low[i] = -1; 3609 data->vout_high[i] = -1; 3610 } 3611 3612 ret = pmbus_init_common(client, data, info); 3613 if (ret < 0) 3614 return ret; 3615 3616 ret = pmbus_find_attributes(client, data); 3617 if (ret) 3618 return ret; 3619 3620 /* 3621 * If there are no attributes, something is wrong. 3622 * Bail out instead of trying to register nothing. 3623 */ 3624 if (!data->num_attributes) { 3625 dev_err(dev, "No attributes found\n"); 3626 return -ENODEV; 3627 } 3628 3629 name = devm_kstrdup(dev, client->name, GFP_KERNEL); 3630 if (!name) 3631 return -ENOMEM; 3632 strreplace(name, '-', '_'); 3633 3634 data->groups[0] = &data->group; 3635 memcpy(data->groups + 1, info->groups, sizeof(void *) * groups_num); 3636 data->hwmon_dev = devm_hwmon_device_register_with_groups(dev, 3637 name, data, data->groups); 3638 if (IS_ERR(data->hwmon_dev)) { 3639 dev_err(dev, "Failed to register hwmon device\n"); 3640 return PTR_ERR(data->hwmon_dev); 3641 } 3642 3643 ret = pmbus_regulator_register(data); 3644 if (ret) 3645 return ret; 3646 3647 ret = pmbus_irq_setup(client, data); 3648 if (ret) 3649 return ret; 3650 3651 ret = pmbus_init_debugfs(client, data); 3652 if (ret) 3653 dev_warn(dev, "Failed to register debugfs\n"); 3654 3655 return 0; 3656 } 3657 EXPORT_SYMBOL_NS_GPL(pmbus_do_probe, PMBUS); 3658 3659 struct dentry *pmbus_get_debugfs_dir(struct i2c_client *client) 3660 { 3661 struct pmbus_data *data = i2c_get_clientdata(client); 3662 3663 return data->debugfs; 3664 } 3665 EXPORT_SYMBOL_NS_GPL(pmbus_get_debugfs_dir, PMBUS); 3666 3667 int pmbus_lock_interruptible(struct i2c_client *client) 3668 { 3669 struct pmbus_data *data = i2c_get_clientdata(client); 3670 3671 return mutex_lock_interruptible(&data->update_lock); 3672 } 3673 EXPORT_SYMBOL_NS_GPL(pmbus_lock_interruptible, PMBUS); 3674 3675 void pmbus_unlock(struct i2c_client *client) 3676 { 3677 struct pmbus_data *data = i2c_get_clientdata(client); 3678 3679 mutex_unlock(&data->update_lock); 3680 } 3681 EXPORT_SYMBOL_NS_GPL(pmbus_unlock, PMBUS); 3682 3683 static int __init pmbus_core_init(void) 3684 { 3685 pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL); 3686 if (IS_ERR(pmbus_debugfs_dir)) 3687 pmbus_debugfs_dir = NULL; 3688 3689 return 0; 3690 } 3691 3692 static void __exit pmbus_core_exit(void) 3693 { 3694 debugfs_remove_recursive(pmbus_debugfs_dir); 3695 } 3696 3697 module_init(pmbus_core_init); 3698 module_exit(pmbus_core_exit); 3699 3700 MODULE_AUTHOR("Guenter Roeck"); 3701 MODULE_DESCRIPTION("PMBus core driver"); 3702 MODULE_LICENSE("GPL"); 3703