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 "pmbus.h" 23 24 /* 25 * Number of additional attribute pointers to allocate 26 * with each call to krealloc 27 */ 28 #define PMBUS_ATTR_ALLOC_SIZE 32 29 #define PMBUS_NAME_SIZE 24 30 31 struct pmbus_sensor { 32 struct pmbus_sensor *next; 33 char name[PMBUS_NAME_SIZE]; /* sysfs sensor name */ 34 struct device_attribute attribute; 35 u8 page; /* page number */ 36 u8 phase; /* phase number, 0xff for all phases */ 37 u16 reg; /* register */ 38 enum pmbus_sensor_classes class; /* sensor class */ 39 bool update; /* runtime sensor update needed */ 40 bool convert; /* Whether or not to apply linear/vid/direct */ 41 int data; /* Sensor data. 42 Negative if there was a read error */ 43 }; 44 #define to_pmbus_sensor(_attr) \ 45 container_of(_attr, struct pmbus_sensor, attribute) 46 47 struct pmbus_boolean { 48 char name[PMBUS_NAME_SIZE]; /* sysfs boolean name */ 49 struct sensor_device_attribute attribute; 50 struct pmbus_sensor *s1; 51 struct pmbus_sensor *s2; 52 }; 53 #define to_pmbus_boolean(_attr) \ 54 container_of(_attr, struct pmbus_boolean, attribute) 55 56 struct pmbus_label { 57 char name[PMBUS_NAME_SIZE]; /* sysfs label name */ 58 struct device_attribute attribute; 59 char label[PMBUS_NAME_SIZE]; /* label */ 60 }; 61 #define to_pmbus_label(_attr) \ 62 container_of(_attr, struct pmbus_label, attribute) 63 64 /* Macros for converting between sensor index and register/page/status mask */ 65 66 #define PB_STATUS_MASK 0xffff 67 #define PB_REG_SHIFT 16 68 #define PB_REG_MASK 0x3ff 69 #define PB_PAGE_SHIFT 26 70 #define PB_PAGE_MASK 0x3f 71 72 #define pb_reg_to_index(page, reg, mask) (((page) << PB_PAGE_SHIFT) | \ 73 ((reg) << PB_REG_SHIFT) | (mask)) 74 75 #define pb_index_to_page(index) (((index) >> PB_PAGE_SHIFT) & PB_PAGE_MASK) 76 #define pb_index_to_reg(index) (((index) >> PB_REG_SHIFT) & PB_REG_MASK) 77 #define pb_index_to_mask(index) ((index) & PB_STATUS_MASK) 78 79 struct pmbus_data { 80 struct device *dev; 81 struct device *hwmon_dev; 82 83 u32 flags; /* from platform data */ 84 85 int exponent[PMBUS_PAGES]; 86 /* linear mode: exponent for output voltages */ 87 88 const struct pmbus_driver_info *info; 89 90 int max_attributes; 91 int num_attributes; 92 struct attribute_group group; 93 const struct attribute_group **groups; 94 struct dentry *debugfs; /* debugfs device directory */ 95 96 struct pmbus_sensor *sensors; 97 98 struct mutex update_lock; 99 100 bool has_status_word; /* device uses STATUS_WORD register */ 101 int (*read_status)(struct i2c_client *client, int page); 102 103 s16 currpage; /* current page, -1 for unknown/unset */ 104 s16 currphase; /* current phase, 0xff for all, -1 for unknown/unset */ 105 }; 106 107 struct pmbus_debugfs_entry { 108 struct i2c_client *client; 109 u8 page; 110 u8 reg; 111 }; 112 113 static const int pmbus_fan_rpm_mask[] = { 114 PB_FAN_1_RPM, 115 PB_FAN_2_RPM, 116 PB_FAN_1_RPM, 117 PB_FAN_2_RPM, 118 }; 119 120 static const int pmbus_fan_config_registers[] = { 121 PMBUS_FAN_CONFIG_12, 122 PMBUS_FAN_CONFIG_12, 123 PMBUS_FAN_CONFIG_34, 124 PMBUS_FAN_CONFIG_34 125 }; 126 127 static const int pmbus_fan_command_registers[] = { 128 PMBUS_FAN_COMMAND_1, 129 PMBUS_FAN_COMMAND_2, 130 PMBUS_FAN_COMMAND_3, 131 PMBUS_FAN_COMMAND_4, 132 }; 133 134 void pmbus_clear_cache(struct i2c_client *client) 135 { 136 struct pmbus_data *data = i2c_get_clientdata(client); 137 struct pmbus_sensor *sensor; 138 139 for (sensor = data->sensors; sensor; sensor = sensor->next) 140 sensor->data = -ENODATA; 141 } 142 EXPORT_SYMBOL_NS_GPL(pmbus_clear_cache, PMBUS); 143 144 void pmbus_set_update(struct i2c_client *client, u8 reg, bool update) 145 { 146 struct pmbus_data *data = i2c_get_clientdata(client); 147 struct pmbus_sensor *sensor; 148 149 for (sensor = data->sensors; sensor; sensor = sensor->next) 150 if (sensor->reg == reg) 151 sensor->update = update; 152 } 153 EXPORT_SYMBOL_NS_GPL(pmbus_set_update, PMBUS); 154 155 int pmbus_set_page(struct i2c_client *client, int page, int phase) 156 { 157 struct pmbus_data *data = i2c_get_clientdata(client); 158 int rv; 159 160 if (page < 0) 161 return 0; 162 163 if (!(data->info->func[page] & PMBUS_PAGE_VIRTUAL) && 164 data->info->pages > 1 && page != data->currpage) { 165 rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page); 166 if (rv < 0) 167 return rv; 168 169 rv = i2c_smbus_read_byte_data(client, PMBUS_PAGE); 170 if (rv < 0) 171 return rv; 172 173 if (rv != page) 174 return -EIO; 175 } 176 data->currpage = page; 177 178 if (data->info->phases[page] && data->currphase != phase && 179 !(data->info->func[page] & PMBUS_PHASE_VIRTUAL)) { 180 rv = i2c_smbus_write_byte_data(client, PMBUS_PHASE, 181 phase); 182 if (rv) 183 return rv; 184 } 185 data->currphase = phase; 186 187 return 0; 188 } 189 EXPORT_SYMBOL_NS_GPL(pmbus_set_page, PMBUS); 190 191 int pmbus_write_byte(struct i2c_client *client, int page, u8 value) 192 { 193 int rv; 194 195 rv = pmbus_set_page(client, page, 0xff); 196 if (rv < 0) 197 return rv; 198 199 return i2c_smbus_write_byte(client, value); 200 } 201 EXPORT_SYMBOL_NS_GPL(pmbus_write_byte, PMBUS); 202 203 /* 204 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if 205 * a device specific mapping function exists and calls it if necessary. 206 */ 207 static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value) 208 { 209 struct pmbus_data *data = i2c_get_clientdata(client); 210 const struct pmbus_driver_info *info = data->info; 211 int status; 212 213 if (info->write_byte) { 214 status = info->write_byte(client, page, value); 215 if (status != -ENODATA) 216 return status; 217 } 218 return pmbus_write_byte(client, page, value); 219 } 220 221 int pmbus_write_word_data(struct i2c_client *client, int page, u8 reg, 222 u16 word) 223 { 224 int rv; 225 226 rv = pmbus_set_page(client, page, 0xff); 227 if (rv < 0) 228 return rv; 229 230 return i2c_smbus_write_word_data(client, reg, word); 231 } 232 EXPORT_SYMBOL_NS_GPL(pmbus_write_word_data, PMBUS); 233 234 235 static int pmbus_write_virt_reg(struct i2c_client *client, int page, int reg, 236 u16 word) 237 { 238 int bit; 239 int id; 240 int rv; 241 242 switch (reg) { 243 case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4: 244 id = reg - PMBUS_VIRT_FAN_TARGET_1; 245 bit = pmbus_fan_rpm_mask[id]; 246 rv = pmbus_update_fan(client, page, id, bit, bit, word); 247 break; 248 default: 249 rv = -ENXIO; 250 break; 251 } 252 253 return rv; 254 } 255 256 /* 257 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if 258 * a device specific mapping function exists and calls it if necessary. 259 */ 260 static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg, 261 u16 word) 262 { 263 struct pmbus_data *data = i2c_get_clientdata(client); 264 const struct pmbus_driver_info *info = data->info; 265 int status; 266 267 if (info->write_word_data) { 268 status = info->write_word_data(client, page, reg, word); 269 if (status != -ENODATA) 270 return status; 271 } 272 273 if (reg >= PMBUS_VIRT_BASE) 274 return pmbus_write_virt_reg(client, page, reg, word); 275 276 return pmbus_write_word_data(client, page, reg, word); 277 } 278 279 int pmbus_update_fan(struct i2c_client *client, int page, int id, 280 u8 config, u8 mask, u16 command) 281 { 282 int from; 283 int rv; 284 u8 to; 285 286 from = pmbus_read_byte_data(client, page, 287 pmbus_fan_config_registers[id]); 288 if (from < 0) 289 return from; 290 291 to = (from & ~mask) | (config & mask); 292 if (to != from) { 293 rv = pmbus_write_byte_data(client, page, 294 pmbus_fan_config_registers[id], to); 295 if (rv < 0) 296 return rv; 297 } 298 299 return _pmbus_write_word_data(client, page, 300 pmbus_fan_command_registers[id], command); 301 } 302 EXPORT_SYMBOL_NS_GPL(pmbus_update_fan, PMBUS); 303 304 int pmbus_read_word_data(struct i2c_client *client, int page, int phase, u8 reg) 305 { 306 int rv; 307 308 rv = pmbus_set_page(client, page, phase); 309 if (rv < 0) 310 return rv; 311 312 return i2c_smbus_read_word_data(client, reg); 313 } 314 EXPORT_SYMBOL_NS_GPL(pmbus_read_word_data, PMBUS); 315 316 static int pmbus_read_virt_reg(struct i2c_client *client, int page, int reg) 317 { 318 int rv; 319 int id; 320 321 switch (reg) { 322 case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4: 323 id = reg - PMBUS_VIRT_FAN_TARGET_1; 324 rv = pmbus_get_fan_rate_device(client, page, id, rpm); 325 break; 326 default: 327 rv = -ENXIO; 328 break; 329 } 330 331 return rv; 332 } 333 334 /* 335 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if 336 * a device specific mapping function exists and calls it if necessary. 337 */ 338 static int _pmbus_read_word_data(struct i2c_client *client, int page, 339 int phase, int reg) 340 { 341 struct pmbus_data *data = i2c_get_clientdata(client); 342 const struct pmbus_driver_info *info = data->info; 343 int status; 344 345 if (info->read_word_data) { 346 status = info->read_word_data(client, page, phase, reg); 347 if (status != -ENODATA) 348 return status; 349 } 350 351 if (reg >= PMBUS_VIRT_BASE) 352 return pmbus_read_virt_reg(client, page, reg); 353 354 return pmbus_read_word_data(client, page, phase, reg); 355 } 356 357 /* Same as above, but without phase parameter, for use in check functions */ 358 static int __pmbus_read_word_data(struct i2c_client *client, int page, int reg) 359 { 360 return _pmbus_read_word_data(client, page, 0xff, reg); 361 } 362 363 int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg) 364 { 365 int rv; 366 367 rv = pmbus_set_page(client, page, 0xff); 368 if (rv < 0) 369 return rv; 370 371 return i2c_smbus_read_byte_data(client, reg); 372 } 373 EXPORT_SYMBOL_NS_GPL(pmbus_read_byte_data, PMBUS); 374 375 int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg, u8 value) 376 { 377 int rv; 378 379 rv = pmbus_set_page(client, page, 0xff); 380 if (rv < 0) 381 return rv; 382 383 return i2c_smbus_write_byte_data(client, reg, value); 384 } 385 EXPORT_SYMBOL_NS_GPL(pmbus_write_byte_data, PMBUS); 386 387 int pmbus_update_byte_data(struct i2c_client *client, int page, u8 reg, 388 u8 mask, u8 value) 389 { 390 unsigned int tmp; 391 int rv; 392 393 rv = pmbus_read_byte_data(client, page, reg); 394 if (rv < 0) 395 return rv; 396 397 tmp = (rv & ~mask) | (value & mask); 398 399 if (tmp != rv) 400 rv = pmbus_write_byte_data(client, page, reg, tmp); 401 402 return rv; 403 } 404 EXPORT_SYMBOL_NS_GPL(pmbus_update_byte_data, PMBUS); 405 406 /* 407 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if 408 * a device specific mapping function exists and calls it if necessary. 409 */ 410 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg) 411 { 412 struct pmbus_data *data = i2c_get_clientdata(client); 413 const struct pmbus_driver_info *info = data->info; 414 int status; 415 416 if (info->read_byte_data) { 417 status = info->read_byte_data(client, page, reg); 418 if (status != -ENODATA) 419 return status; 420 } 421 return pmbus_read_byte_data(client, page, reg); 422 } 423 424 static struct pmbus_sensor *pmbus_find_sensor(struct pmbus_data *data, int page, 425 int reg) 426 { 427 struct pmbus_sensor *sensor; 428 429 for (sensor = data->sensors; sensor; sensor = sensor->next) { 430 if (sensor->page == page && sensor->reg == reg) 431 return sensor; 432 } 433 434 return ERR_PTR(-EINVAL); 435 } 436 437 static int pmbus_get_fan_rate(struct i2c_client *client, int page, int id, 438 enum pmbus_fan_mode mode, 439 bool from_cache) 440 { 441 struct pmbus_data *data = i2c_get_clientdata(client); 442 bool want_rpm, have_rpm; 443 struct pmbus_sensor *s; 444 int config; 445 int reg; 446 447 want_rpm = (mode == rpm); 448 449 if (from_cache) { 450 reg = want_rpm ? PMBUS_VIRT_FAN_TARGET_1 : PMBUS_VIRT_PWM_1; 451 s = pmbus_find_sensor(data, page, reg + id); 452 if (IS_ERR(s)) 453 return PTR_ERR(s); 454 455 return s->data; 456 } 457 458 config = pmbus_read_byte_data(client, page, 459 pmbus_fan_config_registers[id]); 460 if (config < 0) 461 return config; 462 463 have_rpm = !!(config & pmbus_fan_rpm_mask[id]); 464 if (want_rpm == have_rpm) 465 return pmbus_read_word_data(client, page, 0xff, 466 pmbus_fan_command_registers[id]); 467 468 /* Can't sensibly map between RPM and PWM, just return zero */ 469 return 0; 470 } 471 472 int pmbus_get_fan_rate_device(struct i2c_client *client, int page, int id, 473 enum pmbus_fan_mode mode) 474 { 475 return pmbus_get_fan_rate(client, page, id, mode, false); 476 } 477 EXPORT_SYMBOL_NS_GPL(pmbus_get_fan_rate_device, PMBUS); 478 479 int pmbus_get_fan_rate_cached(struct i2c_client *client, int page, int id, 480 enum pmbus_fan_mode mode) 481 { 482 return pmbus_get_fan_rate(client, page, id, mode, true); 483 } 484 EXPORT_SYMBOL_NS_GPL(pmbus_get_fan_rate_cached, PMBUS); 485 486 static void pmbus_clear_fault_page(struct i2c_client *client, int page) 487 { 488 _pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS); 489 } 490 491 void pmbus_clear_faults(struct i2c_client *client) 492 { 493 struct pmbus_data *data = i2c_get_clientdata(client); 494 int i; 495 496 for (i = 0; i < data->info->pages; i++) 497 pmbus_clear_fault_page(client, i); 498 } 499 EXPORT_SYMBOL_NS_GPL(pmbus_clear_faults, PMBUS); 500 501 static int pmbus_check_status_cml(struct i2c_client *client) 502 { 503 struct pmbus_data *data = i2c_get_clientdata(client); 504 int status, status2; 505 506 status = data->read_status(client, -1); 507 if (status < 0 || (status & PB_STATUS_CML)) { 508 status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML); 509 if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND)) 510 return -EIO; 511 } 512 return 0; 513 } 514 515 static bool pmbus_check_register(struct i2c_client *client, 516 int (*func)(struct i2c_client *client, 517 int page, int reg), 518 int page, int reg) 519 { 520 int rv; 521 struct pmbus_data *data = i2c_get_clientdata(client); 522 523 rv = func(client, page, reg); 524 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK)) 525 rv = pmbus_check_status_cml(client); 526 if (rv < 0 && (data->flags & PMBUS_READ_STATUS_AFTER_FAILED_CHECK)) 527 data->read_status(client, -1); 528 pmbus_clear_fault_page(client, -1); 529 return rv >= 0; 530 } 531 532 static bool pmbus_check_status_register(struct i2c_client *client, int page) 533 { 534 int status; 535 struct pmbus_data *data = i2c_get_clientdata(client); 536 537 status = data->read_status(client, page); 538 if (status >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK) && 539 (status & PB_STATUS_CML)) { 540 status = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML); 541 if (status < 0 || (status & PB_CML_FAULT_INVALID_COMMAND)) 542 status = -EIO; 543 } 544 545 pmbus_clear_fault_page(client, -1); 546 return status >= 0; 547 } 548 549 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg) 550 { 551 return pmbus_check_register(client, _pmbus_read_byte_data, page, reg); 552 } 553 EXPORT_SYMBOL_NS_GPL(pmbus_check_byte_register, PMBUS); 554 555 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg) 556 { 557 return pmbus_check_register(client, __pmbus_read_word_data, page, reg); 558 } 559 EXPORT_SYMBOL_NS_GPL(pmbus_check_word_register, PMBUS); 560 561 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client) 562 { 563 struct pmbus_data *data = i2c_get_clientdata(client); 564 565 return data->info; 566 } 567 EXPORT_SYMBOL_NS_GPL(pmbus_get_driver_info, PMBUS); 568 569 static int pmbus_get_status(struct i2c_client *client, int page, int reg) 570 { 571 struct pmbus_data *data = i2c_get_clientdata(client); 572 int status; 573 574 switch (reg) { 575 case PMBUS_STATUS_WORD: 576 status = data->read_status(client, page); 577 break; 578 default: 579 status = _pmbus_read_byte_data(client, page, reg); 580 break; 581 } 582 if (status < 0) 583 pmbus_clear_faults(client); 584 return status; 585 } 586 587 static void pmbus_update_sensor_data(struct i2c_client *client, struct pmbus_sensor *sensor) 588 { 589 if (sensor->data < 0 || sensor->update) 590 sensor->data = _pmbus_read_word_data(client, sensor->page, 591 sensor->phase, sensor->reg); 592 } 593 594 /* 595 * Convert linear sensor values to milli- or micro-units 596 * depending on sensor type. 597 */ 598 static s64 pmbus_reg2data_linear(struct pmbus_data *data, 599 struct pmbus_sensor *sensor) 600 { 601 s16 exponent; 602 s32 mantissa; 603 s64 val; 604 605 if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */ 606 exponent = data->exponent[sensor->page]; 607 mantissa = (u16) sensor->data; 608 } else { /* LINEAR11 */ 609 exponent = ((s16)sensor->data) >> 11; 610 mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5; 611 } 612 613 val = mantissa; 614 615 /* scale result to milli-units for all sensors except fans */ 616 if (sensor->class != PSC_FAN) 617 val = val * 1000LL; 618 619 /* scale result to micro-units for power sensors */ 620 if (sensor->class == PSC_POWER) 621 val = val * 1000LL; 622 623 if (exponent >= 0) 624 val <<= exponent; 625 else 626 val >>= -exponent; 627 628 return val; 629 } 630 631 /* 632 * Convert direct sensor values to milli- or micro-units 633 * depending on sensor type. 634 */ 635 static s64 pmbus_reg2data_direct(struct pmbus_data *data, 636 struct pmbus_sensor *sensor) 637 { 638 s64 b, val = (s16)sensor->data; 639 s32 m, R; 640 641 m = data->info->m[sensor->class]; 642 b = data->info->b[sensor->class]; 643 R = data->info->R[sensor->class]; 644 645 if (m == 0) 646 return 0; 647 648 /* X = 1/m * (Y * 10^-R - b) */ 649 R = -R; 650 /* scale result to milli-units for everything but fans */ 651 if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) { 652 R += 3; 653 b *= 1000; 654 } 655 656 /* scale result to micro-units for power sensors */ 657 if (sensor->class == PSC_POWER) { 658 R += 3; 659 b *= 1000; 660 } 661 662 while (R > 0) { 663 val *= 10; 664 R--; 665 } 666 while (R < 0) { 667 val = div_s64(val + 5LL, 10L); /* round closest */ 668 R++; 669 } 670 671 val = div_s64(val - b, m); 672 return val; 673 } 674 675 /* 676 * Convert VID sensor values to milli- or micro-units 677 * depending on sensor type. 678 */ 679 static s64 pmbus_reg2data_vid(struct pmbus_data *data, 680 struct pmbus_sensor *sensor) 681 { 682 long val = sensor->data; 683 long rv = 0; 684 685 switch (data->info->vrm_version[sensor->page]) { 686 case vr11: 687 if (val >= 0x02 && val <= 0xb2) 688 rv = DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100); 689 break; 690 case vr12: 691 if (val >= 0x01) 692 rv = 250 + (val - 1) * 5; 693 break; 694 case vr13: 695 if (val >= 0x01) 696 rv = 500 + (val - 1) * 10; 697 break; 698 case imvp9: 699 if (val >= 0x01) 700 rv = 200 + (val - 1) * 10; 701 break; 702 case amd625mv: 703 if (val >= 0x0 && val <= 0xd8) 704 rv = DIV_ROUND_CLOSEST(155000 - val * 625, 100); 705 break; 706 } 707 return rv; 708 } 709 710 static s64 pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor) 711 { 712 s64 val; 713 714 if (!sensor->convert) 715 return sensor->data; 716 717 switch (data->info->format[sensor->class]) { 718 case direct: 719 val = pmbus_reg2data_direct(data, sensor); 720 break; 721 case vid: 722 val = pmbus_reg2data_vid(data, sensor); 723 break; 724 case linear: 725 default: 726 val = pmbus_reg2data_linear(data, sensor); 727 break; 728 } 729 return val; 730 } 731 732 #define MAX_MANTISSA (1023 * 1000) 733 #define MIN_MANTISSA (511 * 1000) 734 735 static u16 pmbus_data2reg_linear(struct pmbus_data *data, 736 struct pmbus_sensor *sensor, s64 val) 737 { 738 s16 exponent = 0, mantissa; 739 bool negative = false; 740 741 /* simple case */ 742 if (val == 0) 743 return 0; 744 745 if (sensor->class == PSC_VOLTAGE_OUT) { 746 /* LINEAR16 does not support negative voltages */ 747 if (val < 0) 748 return 0; 749 750 /* 751 * For a static exponents, we don't have a choice 752 * but to adjust the value to it. 753 */ 754 if (data->exponent[sensor->page] < 0) 755 val <<= -data->exponent[sensor->page]; 756 else 757 val >>= data->exponent[sensor->page]; 758 val = DIV_ROUND_CLOSEST_ULL(val, 1000); 759 return clamp_val(val, 0, 0xffff); 760 } 761 762 if (val < 0) { 763 negative = true; 764 val = -val; 765 } 766 767 /* Power is in uW. Convert to mW before converting. */ 768 if (sensor->class == PSC_POWER) 769 val = DIV_ROUND_CLOSEST_ULL(val, 1000); 770 771 /* 772 * For simplicity, convert fan data to milli-units 773 * before calculating the exponent. 774 */ 775 if (sensor->class == PSC_FAN) 776 val = val * 1000LL; 777 778 /* Reduce large mantissa until it fits into 10 bit */ 779 while (val >= MAX_MANTISSA && exponent < 15) { 780 exponent++; 781 val >>= 1; 782 } 783 /* Increase small mantissa to improve precision */ 784 while (val < MIN_MANTISSA && exponent > -15) { 785 exponent--; 786 val <<= 1; 787 } 788 789 /* Convert mantissa from milli-units to units */ 790 mantissa = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff); 791 792 /* restore sign */ 793 if (negative) 794 mantissa = -mantissa; 795 796 /* Convert to 5 bit exponent, 11 bit mantissa */ 797 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800); 798 } 799 800 static u16 pmbus_data2reg_direct(struct pmbus_data *data, 801 struct pmbus_sensor *sensor, s64 val) 802 { 803 s64 b; 804 s32 m, R; 805 806 m = data->info->m[sensor->class]; 807 b = data->info->b[sensor->class]; 808 R = data->info->R[sensor->class]; 809 810 /* Power is in uW. Adjust R and b. */ 811 if (sensor->class == PSC_POWER) { 812 R -= 3; 813 b *= 1000; 814 } 815 816 /* Calculate Y = (m * X + b) * 10^R */ 817 if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) { 818 R -= 3; /* Adjust R and b for data in milli-units */ 819 b *= 1000; 820 } 821 val = val * m + b; 822 823 while (R > 0) { 824 val *= 10; 825 R--; 826 } 827 while (R < 0) { 828 val = div_s64(val + 5LL, 10L); /* round closest */ 829 R++; 830 } 831 832 return (u16)clamp_val(val, S16_MIN, S16_MAX); 833 } 834 835 static u16 pmbus_data2reg_vid(struct pmbus_data *data, 836 struct pmbus_sensor *sensor, s64 val) 837 { 838 val = clamp_val(val, 500, 1600); 839 840 return 2 + DIV_ROUND_CLOSEST_ULL((1600LL - val) * 100LL, 625); 841 } 842 843 static u16 pmbus_data2reg(struct pmbus_data *data, 844 struct pmbus_sensor *sensor, s64 val) 845 { 846 u16 regval; 847 848 if (!sensor->convert) 849 return val; 850 851 switch (data->info->format[sensor->class]) { 852 case direct: 853 regval = pmbus_data2reg_direct(data, sensor, val); 854 break; 855 case vid: 856 regval = pmbus_data2reg_vid(data, sensor, val); 857 break; 858 case linear: 859 default: 860 regval = pmbus_data2reg_linear(data, sensor, val); 861 break; 862 } 863 return regval; 864 } 865 866 /* 867 * Return boolean calculated from converted data. 868 * <index> defines a status register index and mask. 869 * The mask is in the lower 8 bits, the register index is in bits 8..23. 870 * 871 * The associated pmbus_boolean structure contains optional pointers to two 872 * sensor attributes. If specified, those attributes are compared against each 873 * other to determine if a limit has been exceeded. 874 * 875 * If the sensor attribute pointers are NULL, the function returns true if 876 * (status[reg] & mask) is true. 877 * 878 * If sensor attribute pointers are provided, a comparison against a specified 879 * limit has to be performed to determine the boolean result. 880 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are 881 * sensor values referenced by sensor attribute pointers s1 and s2). 882 * 883 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>. 884 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>. 885 * 886 * If a negative value is stored in any of the referenced registers, this value 887 * reflects an error code which will be returned. 888 */ 889 static int pmbus_get_boolean(struct i2c_client *client, struct pmbus_boolean *b, 890 int index) 891 { 892 struct pmbus_data *data = i2c_get_clientdata(client); 893 struct pmbus_sensor *s1 = b->s1; 894 struct pmbus_sensor *s2 = b->s2; 895 u16 mask = pb_index_to_mask(index); 896 u8 page = pb_index_to_page(index); 897 u16 reg = pb_index_to_reg(index); 898 int ret, status; 899 u16 regval; 900 901 mutex_lock(&data->update_lock); 902 status = pmbus_get_status(client, page, reg); 903 if (status < 0) { 904 ret = status; 905 goto unlock; 906 } 907 908 if (s1) 909 pmbus_update_sensor_data(client, s1); 910 if (s2) 911 pmbus_update_sensor_data(client, s2); 912 913 regval = status & mask; 914 if (regval) { 915 ret = pmbus_write_byte_data(client, page, reg, regval); 916 if (ret) 917 goto unlock; 918 } 919 if (s1 && s2) { 920 s64 v1, v2; 921 922 if (s1->data < 0) { 923 ret = s1->data; 924 goto unlock; 925 } 926 if (s2->data < 0) { 927 ret = s2->data; 928 goto unlock; 929 } 930 931 v1 = pmbus_reg2data(data, s1); 932 v2 = pmbus_reg2data(data, s2); 933 ret = !!(regval && v1 >= v2); 934 } else { 935 ret = !!regval; 936 } 937 unlock: 938 mutex_unlock(&data->update_lock); 939 return ret; 940 } 941 942 static ssize_t pmbus_show_boolean(struct device *dev, 943 struct device_attribute *da, char *buf) 944 { 945 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 946 struct pmbus_boolean *boolean = to_pmbus_boolean(attr); 947 struct i2c_client *client = to_i2c_client(dev->parent); 948 int val; 949 950 val = pmbus_get_boolean(client, boolean, attr->index); 951 if (val < 0) 952 return val; 953 return sysfs_emit(buf, "%d\n", val); 954 } 955 956 static ssize_t pmbus_show_sensor(struct device *dev, 957 struct device_attribute *devattr, char *buf) 958 { 959 struct i2c_client *client = to_i2c_client(dev->parent); 960 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr); 961 struct pmbus_data *data = i2c_get_clientdata(client); 962 ssize_t ret; 963 964 mutex_lock(&data->update_lock); 965 pmbus_update_sensor_data(client, sensor); 966 if (sensor->data < 0) 967 ret = sensor->data; 968 else 969 ret = sysfs_emit(buf, "%lld\n", pmbus_reg2data(data, sensor)); 970 mutex_unlock(&data->update_lock); 971 return ret; 972 } 973 974 static ssize_t pmbus_set_sensor(struct device *dev, 975 struct device_attribute *devattr, 976 const char *buf, size_t count) 977 { 978 struct i2c_client *client = to_i2c_client(dev->parent); 979 struct pmbus_data *data = i2c_get_clientdata(client); 980 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr); 981 ssize_t rv = count; 982 s64 val; 983 int ret; 984 u16 regval; 985 986 if (kstrtos64(buf, 10, &val) < 0) 987 return -EINVAL; 988 989 mutex_lock(&data->update_lock); 990 regval = pmbus_data2reg(data, sensor, val); 991 ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval); 992 if (ret < 0) 993 rv = ret; 994 else 995 sensor->data = -ENODATA; 996 mutex_unlock(&data->update_lock); 997 return rv; 998 } 999 1000 static ssize_t pmbus_show_label(struct device *dev, 1001 struct device_attribute *da, char *buf) 1002 { 1003 struct pmbus_label *label = to_pmbus_label(da); 1004 1005 return sysfs_emit(buf, "%s\n", label->label); 1006 } 1007 1008 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr) 1009 { 1010 if (data->num_attributes >= data->max_attributes - 1) { 1011 int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE; 1012 void *new_attrs = devm_krealloc(data->dev, data->group.attrs, 1013 new_max_attrs * sizeof(void *), 1014 GFP_KERNEL); 1015 if (!new_attrs) 1016 return -ENOMEM; 1017 data->group.attrs = new_attrs; 1018 data->max_attributes = new_max_attrs; 1019 } 1020 1021 data->group.attrs[data->num_attributes++] = attr; 1022 data->group.attrs[data->num_attributes] = NULL; 1023 return 0; 1024 } 1025 1026 static void pmbus_dev_attr_init(struct device_attribute *dev_attr, 1027 const char *name, 1028 umode_t mode, 1029 ssize_t (*show)(struct device *dev, 1030 struct device_attribute *attr, 1031 char *buf), 1032 ssize_t (*store)(struct device *dev, 1033 struct device_attribute *attr, 1034 const char *buf, size_t count)) 1035 { 1036 sysfs_attr_init(&dev_attr->attr); 1037 dev_attr->attr.name = name; 1038 dev_attr->attr.mode = mode; 1039 dev_attr->show = show; 1040 dev_attr->store = store; 1041 } 1042 1043 static void pmbus_attr_init(struct sensor_device_attribute *a, 1044 const char *name, 1045 umode_t mode, 1046 ssize_t (*show)(struct device *dev, 1047 struct device_attribute *attr, 1048 char *buf), 1049 ssize_t (*store)(struct device *dev, 1050 struct device_attribute *attr, 1051 const char *buf, size_t count), 1052 int idx) 1053 { 1054 pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store); 1055 a->index = idx; 1056 } 1057 1058 static int pmbus_add_boolean(struct pmbus_data *data, 1059 const char *name, const char *type, int seq, 1060 struct pmbus_sensor *s1, 1061 struct pmbus_sensor *s2, 1062 u8 page, u16 reg, u16 mask) 1063 { 1064 struct pmbus_boolean *boolean; 1065 struct sensor_device_attribute *a; 1066 1067 if (WARN((s1 && !s2) || (!s1 && s2), "Bad s1/s2 parameters\n")) 1068 return -EINVAL; 1069 1070 boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL); 1071 if (!boolean) 1072 return -ENOMEM; 1073 1074 a = &boolean->attribute; 1075 1076 snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s", 1077 name, seq, type); 1078 boolean->s1 = s1; 1079 boolean->s2 = s2; 1080 pmbus_attr_init(a, boolean->name, 0444, pmbus_show_boolean, NULL, 1081 pb_reg_to_index(page, reg, mask)); 1082 1083 return pmbus_add_attribute(data, &a->dev_attr.attr); 1084 } 1085 1086 static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data, 1087 const char *name, const char *type, 1088 int seq, int page, int phase, 1089 int reg, 1090 enum pmbus_sensor_classes class, 1091 bool update, bool readonly, 1092 bool convert) 1093 { 1094 struct pmbus_sensor *sensor; 1095 struct device_attribute *a; 1096 1097 sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL); 1098 if (!sensor) 1099 return NULL; 1100 a = &sensor->attribute; 1101 1102 if (type) 1103 snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s", 1104 name, seq, type); 1105 else 1106 snprintf(sensor->name, sizeof(sensor->name), "%s%d", 1107 name, seq); 1108 1109 if (data->flags & PMBUS_WRITE_PROTECTED) 1110 readonly = true; 1111 1112 sensor->page = page; 1113 sensor->phase = phase; 1114 sensor->reg = reg; 1115 sensor->class = class; 1116 sensor->update = update; 1117 sensor->convert = convert; 1118 sensor->data = -ENODATA; 1119 pmbus_dev_attr_init(a, sensor->name, 1120 readonly ? 0444 : 0644, 1121 pmbus_show_sensor, pmbus_set_sensor); 1122 1123 if (pmbus_add_attribute(data, &a->attr)) 1124 return NULL; 1125 1126 sensor->next = data->sensors; 1127 data->sensors = sensor; 1128 1129 return sensor; 1130 } 1131 1132 static int pmbus_add_label(struct pmbus_data *data, 1133 const char *name, int seq, 1134 const char *lstring, int index, int phase) 1135 { 1136 struct pmbus_label *label; 1137 struct device_attribute *a; 1138 1139 label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL); 1140 if (!label) 1141 return -ENOMEM; 1142 1143 a = &label->attribute; 1144 1145 snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq); 1146 if (!index) { 1147 if (phase == 0xff) 1148 strncpy(label->label, lstring, 1149 sizeof(label->label) - 1); 1150 else 1151 snprintf(label->label, sizeof(label->label), "%s.%d", 1152 lstring, phase); 1153 } else { 1154 if (phase == 0xff) 1155 snprintf(label->label, sizeof(label->label), "%s%d", 1156 lstring, index); 1157 else 1158 snprintf(label->label, sizeof(label->label), "%s%d.%d", 1159 lstring, index, phase); 1160 } 1161 1162 pmbus_dev_attr_init(a, label->name, 0444, pmbus_show_label, NULL); 1163 return pmbus_add_attribute(data, &a->attr); 1164 } 1165 1166 /* 1167 * Search for attributes. Allocate sensors, booleans, and labels as needed. 1168 */ 1169 1170 /* 1171 * The pmbus_limit_attr structure describes a single limit attribute 1172 * and its associated alarm attribute. 1173 */ 1174 struct pmbus_limit_attr { 1175 u16 reg; /* Limit register */ 1176 u16 sbit; /* Alarm attribute status bit */ 1177 bool update; /* True if register needs updates */ 1178 bool low; /* True if low limit; for limits with compare 1179 functions only */ 1180 const char *attr; /* Attribute name */ 1181 const char *alarm; /* Alarm attribute name */ 1182 }; 1183 1184 /* 1185 * The pmbus_sensor_attr structure describes one sensor attribute. This 1186 * description includes a reference to the associated limit attributes. 1187 */ 1188 struct pmbus_sensor_attr { 1189 u16 reg; /* sensor register */ 1190 u16 gbit; /* generic status bit */ 1191 u8 nlimit; /* # of limit registers */ 1192 enum pmbus_sensor_classes class;/* sensor class */ 1193 const char *label; /* sensor label */ 1194 bool paged; /* true if paged sensor */ 1195 bool update; /* true if update needed */ 1196 bool compare; /* true if compare function needed */ 1197 u32 func; /* sensor mask */ 1198 u32 sfunc; /* sensor status mask */ 1199 int sreg; /* status register */ 1200 const struct pmbus_limit_attr *limit;/* limit registers */ 1201 }; 1202 1203 /* 1204 * Add a set of limit attributes and, if supported, the associated 1205 * alarm attributes. 1206 * returns 0 if no alarm register found, 1 if an alarm register was found, 1207 * < 0 on errors. 1208 */ 1209 static int pmbus_add_limit_attrs(struct i2c_client *client, 1210 struct pmbus_data *data, 1211 const struct pmbus_driver_info *info, 1212 const char *name, int index, int page, 1213 struct pmbus_sensor *base, 1214 const struct pmbus_sensor_attr *attr) 1215 { 1216 const struct pmbus_limit_attr *l = attr->limit; 1217 int nlimit = attr->nlimit; 1218 int have_alarm = 0; 1219 int i, ret; 1220 struct pmbus_sensor *curr; 1221 1222 for (i = 0; i < nlimit; i++) { 1223 if (pmbus_check_word_register(client, page, l->reg)) { 1224 curr = pmbus_add_sensor(data, name, l->attr, index, 1225 page, 0xff, l->reg, attr->class, 1226 attr->update || l->update, 1227 false, true); 1228 if (!curr) 1229 return -ENOMEM; 1230 if (l->sbit && (info->func[page] & attr->sfunc)) { 1231 ret = pmbus_add_boolean(data, name, 1232 l->alarm, index, 1233 attr->compare ? l->low ? curr : base 1234 : NULL, 1235 attr->compare ? l->low ? base : curr 1236 : NULL, 1237 page, attr->sreg, l->sbit); 1238 if (ret) 1239 return ret; 1240 have_alarm = 1; 1241 } 1242 } 1243 l++; 1244 } 1245 return have_alarm; 1246 } 1247 1248 static int pmbus_add_sensor_attrs_one(struct i2c_client *client, 1249 struct pmbus_data *data, 1250 const struct pmbus_driver_info *info, 1251 const char *name, 1252 int index, int page, int phase, 1253 const struct pmbus_sensor_attr *attr, 1254 bool paged) 1255 { 1256 struct pmbus_sensor *base; 1257 bool upper = !!(attr->gbit & 0xff00); /* need to check STATUS_WORD */ 1258 int ret; 1259 1260 if (attr->label) { 1261 ret = pmbus_add_label(data, name, index, attr->label, 1262 paged ? page + 1 : 0, phase); 1263 if (ret) 1264 return ret; 1265 } 1266 base = pmbus_add_sensor(data, name, "input", index, page, phase, 1267 attr->reg, attr->class, true, true, true); 1268 if (!base) 1269 return -ENOMEM; 1270 /* No limit and alarm attributes for phase specific sensors */ 1271 if (attr->sfunc && phase == 0xff) { 1272 ret = pmbus_add_limit_attrs(client, data, info, name, 1273 index, page, base, attr); 1274 if (ret < 0) 1275 return ret; 1276 /* 1277 * Add generic alarm attribute only if there are no individual 1278 * alarm attributes, if there is a global alarm bit, and if 1279 * the generic status register (word or byte, depending on 1280 * which global bit is set) for this page is accessible. 1281 */ 1282 if (!ret && attr->gbit && 1283 (!upper || data->has_status_word) && 1284 pmbus_check_status_register(client, page)) { 1285 ret = pmbus_add_boolean(data, name, "alarm", index, 1286 NULL, NULL, 1287 page, PMBUS_STATUS_WORD, 1288 attr->gbit); 1289 if (ret) 1290 return ret; 1291 } 1292 } 1293 return 0; 1294 } 1295 1296 static bool pmbus_sensor_is_paged(const struct pmbus_driver_info *info, 1297 const struct pmbus_sensor_attr *attr) 1298 { 1299 int p; 1300 1301 if (attr->paged) 1302 return true; 1303 1304 /* 1305 * Some attributes may be present on more than one page despite 1306 * not being marked with the paged attribute. If that is the case, 1307 * then treat the sensor as being paged and add the page suffix to the 1308 * attribute name. 1309 * We don't just add the paged attribute to all such attributes, in 1310 * order to maintain the un-suffixed labels in the case where the 1311 * attribute is only on page 0. 1312 */ 1313 for (p = 1; p < info->pages; p++) { 1314 if (info->func[p] & attr->func) 1315 return true; 1316 } 1317 return false; 1318 } 1319 1320 static int pmbus_add_sensor_attrs(struct i2c_client *client, 1321 struct pmbus_data *data, 1322 const char *name, 1323 const struct pmbus_sensor_attr *attrs, 1324 int nattrs) 1325 { 1326 const struct pmbus_driver_info *info = data->info; 1327 int index, i; 1328 int ret; 1329 1330 index = 1; 1331 for (i = 0; i < nattrs; i++) { 1332 int page, pages; 1333 bool paged = pmbus_sensor_is_paged(info, attrs); 1334 1335 pages = paged ? info->pages : 1; 1336 for (page = 0; page < pages; page++) { 1337 if (info->func[page] & attrs->func) { 1338 ret = pmbus_add_sensor_attrs_one(client, data, info, 1339 name, index, page, 1340 0xff, attrs, paged); 1341 if (ret) 1342 return ret; 1343 index++; 1344 } 1345 if (info->phases[page]) { 1346 int phase; 1347 1348 for (phase = 0; phase < info->phases[page]; 1349 phase++) { 1350 if (!(info->pfunc[phase] & attrs->func)) 1351 continue; 1352 ret = pmbus_add_sensor_attrs_one(client, 1353 data, info, name, index, page, 1354 phase, attrs, paged); 1355 if (ret) 1356 return ret; 1357 index++; 1358 } 1359 } 1360 } 1361 attrs++; 1362 } 1363 return 0; 1364 } 1365 1366 static const struct pmbus_limit_attr vin_limit_attrs[] = { 1367 { 1368 .reg = PMBUS_VIN_UV_WARN_LIMIT, 1369 .attr = "min", 1370 .alarm = "min_alarm", 1371 .sbit = PB_VOLTAGE_UV_WARNING, 1372 }, { 1373 .reg = PMBUS_VIN_UV_FAULT_LIMIT, 1374 .attr = "lcrit", 1375 .alarm = "lcrit_alarm", 1376 .sbit = PB_VOLTAGE_UV_FAULT | PB_VOLTAGE_VIN_OFF, 1377 }, { 1378 .reg = PMBUS_VIN_OV_WARN_LIMIT, 1379 .attr = "max", 1380 .alarm = "max_alarm", 1381 .sbit = PB_VOLTAGE_OV_WARNING, 1382 }, { 1383 .reg = PMBUS_VIN_OV_FAULT_LIMIT, 1384 .attr = "crit", 1385 .alarm = "crit_alarm", 1386 .sbit = PB_VOLTAGE_OV_FAULT, 1387 }, { 1388 .reg = PMBUS_VIRT_READ_VIN_AVG, 1389 .update = true, 1390 .attr = "average", 1391 }, { 1392 .reg = PMBUS_VIRT_READ_VIN_MIN, 1393 .update = true, 1394 .attr = "lowest", 1395 }, { 1396 .reg = PMBUS_VIRT_READ_VIN_MAX, 1397 .update = true, 1398 .attr = "highest", 1399 }, { 1400 .reg = PMBUS_VIRT_RESET_VIN_HISTORY, 1401 .attr = "reset_history", 1402 }, { 1403 .reg = PMBUS_MFR_VIN_MIN, 1404 .attr = "rated_min", 1405 }, { 1406 .reg = PMBUS_MFR_VIN_MAX, 1407 .attr = "rated_max", 1408 }, 1409 }; 1410 1411 static const struct pmbus_limit_attr vmon_limit_attrs[] = { 1412 { 1413 .reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT, 1414 .attr = "min", 1415 .alarm = "min_alarm", 1416 .sbit = PB_VOLTAGE_UV_WARNING, 1417 }, { 1418 .reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT, 1419 .attr = "lcrit", 1420 .alarm = "lcrit_alarm", 1421 .sbit = PB_VOLTAGE_UV_FAULT, 1422 }, { 1423 .reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT, 1424 .attr = "max", 1425 .alarm = "max_alarm", 1426 .sbit = PB_VOLTAGE_OV_WARNING, 1427 }, { 1428 .reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT, 1429 .attr = "crit", 1430 .alarm = "crit_alarm", 1431 .sbit = PB_VOLTAGE_OV_FAULT, 1432 } 1433 }; 1434 1435 static const struct pmbus_limit_attr vout_limit_attrs[] = { 1436 { 1437 .reg = PMBUS_VOUT_UV_WARN_LIMIT, 1438 .attr = "min", 1439 .alarm = "min_alarm", 1440 .sbit = PB_VOLTAGE_UV_WARNING, 1441 }, { 1442 .reg = PMBUS_VOUT_UV_FAULT_LIMIT, 1443 .attr = "lcrit", 1444 .alarm = "lcrit_alarm", 1445 .sbit = PB_VOLTAGE_UV_FAULT, 1446 }, { 1447 .reg = PMBUS_VOUT_OV_WARN_LIMIT, 1448 .attr = "max", 1449 .alarm = "max_alarm", 1450 .sbit = PB_VOLTAGE_OV_WARNING, 1451 }, { 1452 .reg = PMBUS_VOUT_OV_FAULT_LIMIT, 1453 .attr = "crit", 1454 .alarm = "crit_alarm", 1455 .sbit = PB_VOLTAGE_OV_FAULT, 1456 }, { 1457 .reg = PMBUS_VIRT_READ_VOUT_AVG, 1458 .update = true, 1459 .attr = "average", 1460 }, { 1461 .reg = PMBUS_VIRT_READ_VOUT_MIN, 1462 .update = true, 1463 .attr = "lowest", 1464 }, { 1465 .reg = PMBUS_VIRT_READ_VOUT_MAX, 1466 .update = true, 1467 .attr = "highest", 1468 }, { 1469 .reg = PMBUS_VIRT_RESET_VOUT_HISTORY, 1470 .attr = "reset_history", 1471 }, { 1472 .reg = PMBUS_MFR_VOUT_MIN, 1473 .attr = "rated_min", 1474 }, { 1475 .reg = PMBUS_MFR_VOUT_MAX, 1476 .attr = "rated_max", 1477 }, 1478 }; 1479 1480 static const struct pmbus_sensor_attr voltage_attributes[] = { 1481 { 1482 .reg = PMBUS_READ_VIN, 1483 .class = PSC_VOLTAGE_IN, 1484 .label = "vin", 1485 .func = PMBUS_HAVE_VIN, 1486 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1487 .sreg = PMBUS_STATUS_INPUT, 1488 .gbit = PB_STATUS_VIN_UV, 1489 .limit = vin_limit_attrs, 1490 .nlimit = ARRAY_SIZE(vin_limit_attrs), 1491 }, { 1492 .reg = PMBUS_VIRT_READ_VMON, 1493 .class = PSC_VOLTAGE_IN, 1494 .label = "vmon", 1495 .func = PMBUS_HAVE_VMON, 1496 .sfunc = PMBUS_HAVE_STATUS_VMON, 1497 .sreg = PMBUS_VIRT_STATUS_VMON, 1498 .limit = vmon_limit_attrs, 1499 .nlimit = ARRAY_SIZE(vmon_limit_attrs), 1500 }, { 1501 .reg = PMBUS_READ_VCAP, 1502 .class = PSC_VOLTAGE_IN, 1503 .label = "vcap", 1504 .func = PMBUS_HAVE_VCAP, 1505 }, { 1506 .reg = PMBUS_READ_VOUT, 1507 .class = PSC_VOLTAGE_OUT, 1508 .label = "vout", 1509 .paged = true, 1510 .func = PMBUS_HAVE_VOUT, 1511 .sfunc = PMBUS_HAVE_STATUS_VOUT, 1512 .sreg = PMBUS_STATUS_VOUT, 1513 .gbit = PB_STATUS_VOUT_OV, 1514 .limit = vout_limit_attrs, 1515 .nlimit = ARRAY_SIZE(vout_limit_attrs), 1516 } 1517 }; 1518 1519 /* Current attributes */ 1520 1521 static const struct pmbus_limit_attr iin_limit_attrs[] = { 1522 { 1523 .reg = PMBUS_IIN_OC_WARN_LIMIT, 1524 .attr = "max", 1525 .alarm = "max_alarm", 1526 .sbit = PB_IIN_OC_WARNING, 1527 }, { 1528 .reg = PMBUS_IIN_OC_FAULT_LIMIT, 1529 .attr = "crit", 1530 .alarm = "crit_alarm", 1531 .sbit = PB_IIN_OC_FAULT, 1532 }, { 1533 .reg = PMBUS_VIRT_READ_IIN_AVG, 1534 .update = true, 1535 .attr = "average", 1536 }, { 1537 .reg = PMBUS_VIRT_READ_IIN_MIN, 1538 .update = true, 1539 .attr = "lowest", 1540 }, { 1541 .reg = PMBUS_VIRT_READ_IIN_MAX, 1542 .update = true, 1543 .attr = "highest", 1544 }, { 1545 .reg = PMBUS_VIRT_RESET_IIN_HISTORY, 1546 .attr = "reset_history", 1547 }, { 1548 .reg = PMBUS_MFR_IIN_MAX, 1549 .attr = "rated_max", 1550 }, 1551 }; 1552 1553 static const struct pmbus_limit_attr iout_limit_attrs[] = { 1554 { 1555 .reg = PMBUS_IOUT_OC_WARN_LIMIT, 1556 .attr = "max", 1557 .alarm = "max_alarm", 1558 .sbit = PB_IOUT_OC_WARNING, 1559 }, { 1560 .reg = PMBUS_IOUT_UC_FAULT_LIMIT, 1561 .attr = "lcrit", 1562 .alarm = "lcrit_alarm", 1563 .sbit = PB_IOUT_UC_FAULT, 1564 }, { 1565 .reg = PMBUS_IOUT_OC_FAULT_LIMIT, 1566 .attr = "crit", 1567 .alarm = "crit_alarm", 1568 .sbit = PB_IOUT_OC_FAULT, 1569 }, { 1570 .reg = PMBUS_VIRT_READ_IOUT_AVG, 1571 .update = true, 1572 .attr = "average", 1573 }, { 1574 .reg = PMBUS_VIRT_READ_IOUT_MIN, 1575 .update = true, 1576 .attr = "lowest", 1577 }, { 1578 .reg = PMBUS_VIRT_READ_IOUT_MAX, 1579 .update = true, 1580 .attr = "highest", 1581 }, { 1582 .reg = PMBUS_VIRT_RESET_IOUT_HISTORY, 1583 .attr = "reset_history", 1584 }, { 1585 .reg = PMBUS_MFR_IOUT_MAX, 1586 .attr = "rated_max", 1587 }, 1588 }; 1589 1590 static const struct pmbus_sensor_attr current_attributes[] = { 1591 { 1592 .reg = PMBUS_READ_IIN, 1593 .class = PSC_CURRENT_IN, 1594 .label = "iin", 1595 .func = PMBUS_HAVE_IIN, 1596 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1597 .sreg = PMBUS_STATUS_INPUT, 1598 .gbit = PB_STATUS_INPUT, 1599 .limit = iin_limit_attrs, 1600 .nlimit = ARRAY_SIZE(iin_limit_attrs), 1601 }, { 1602 .reg = PMBUS_READ_IOUT, 1603 .class = PSC_CURRENT_OUT, 1604 .label = "iout", 1605 .paged = true, 1606 .func = PMBUS_HAVE_IOUT, 1607 .sfunc = PMBUS_HAVE_STATUS_IOUT, 1608 .sreg = PMBUS_STATUS_IOUT, 1609 .gbit = PB_STATUS_IOUT_OC, 1610 .limit = iout_limit_attrs, 1611 .nlimit = ARRAY_SIZE(iout_limit_attrs), 1612 } 1613 }; 1614 1615 /* Power attributes */ 1616 1617 static const struct pmbus_limit_attr pin_limit_attrs[] = { 1618 { 1619 .reg = PMBUS_PIN_OP_WARN_LIMIT, 1620 .attr = "max", 1621 .alarm = "alarm", 1622 .sbit = PB_PIN_OP_WARNING, 1623 }, { 1624 .reg = PMBUS_VIRT_READ_PIN_AVG, 1625 .update = true, 1626 .attr = "average", 1627 }, { 1628 .reg = PMBUS_VIRT_READ_PIN_MIN, 1629 .update = true, 1630 .attr = "input_lowest", 1631 }, { 1632 .reg = PMBUS_VIRT_READ_PIN_MAX, 1633 .update = true, 1634 .attr = "input_highest", 1635 }, { 1636 .reg = PMBUS_VIRT_RESET_PIN_HISTORY, 1637 .attr = "reset_history", 1638 }, { 1639 .reg = PMBUS_MFR_PIN_MAX, 1640 .attr = "rated_max", 1641 }, 1642 }; 1643 1644 static const struct pmbus_limit_attr pout_limit_attrs[] = { 1645 { 1646 .reg = PMBUS_POUT_MAX, 1647 .attr = "cap", 1648 .alarm = "cap_alarm", 1649 .sbit = PB_POWER_LIMITING, 1650 }, { 1651 .reg = PMBUS_POUT_OP_WARN_LIMIT, 1652 .attr = "max", 1653 .alarm = "max_alarm", 1654 .sbit = PB_POUT_OP_WARNING, 1655 }, { 1656 .reg = PMBUS_POUT_OP_FAULT_LIMIT, 1657 .attr = "crit", 1658 .alarm = "crit_alarm", 1659 .sbit = PB_POUT_OP_FAULT, 1660 }, { 1661 .reg = PMBUS_VIRT_READ_POUT_AVG, 1662 .update = true, 1663 .attr = "average", 1664 }, { 1665 .reg = PMBUS_VIRT_READ_POUT_MIN, 1666 .update = true, 1667 .attr = "input_lowest", 1668 }, { 1669 .reg = PMBUS_VIRT_READ_POUT_MAX, 1670 .update = true, 1671 .attr = "input_highest", 1672 }, { 1673 .reg = PMBUS_VIRT_RESET_POUT_HISTORY, 1674 .attr = "reset_history", 1675 }, { 1676 .reg = PMBUS_MFR_POUT_MAX, 1677 .attr = "rated_max", 1678 }, 1679 }; 1680 1681 static const struct pmbus_sensor_attr power_attributes[] = { 1682 { 1683 .reg = PMBUS_READ_PIN, 1684 .class = PSC_POWER, 1685 .label = "pin", 1686 .func = PMBUS_HAVE_PIN, 1687 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1688 .sreg = PMBUS_STATUS_INPUT, 1689 .gbit = PB_STATUS_INPUT, 1690 .limit = pin_limit_attrs, 1691 .nlimit = ARRAY_SIZE(pin_limit_attrs), 1692 }, { 1693 .reg = PMBUS_READ_POUT, 1694 .class = PSC_POWER, 1695 .label = "pout", 1696 .paged = true, 1697 .func = PMBUS_HAVE_POUT, 1698 .sfunc = PMBUS_HAVE_STATUS_IOUT, 1699 .sreg = PMBUS_STATUS_IOUT, 1700 .limit = pout_limit_attrs, 1701 .nlimit = ARRAY_SIZE(pout_limit_attrs), 1702 } 1703 }; 1704 1705 /* Temperature atributes */ 1706 1707 static const struct pmbus_limit_attr temp_limit_attrs[] = { 1708 { 1709 .reg = PMBUS_UT_WARN_LIMIT, 1710 .low = true, 1711 .attr = "min", 1712 .alarm = "min_alarm", 1713 .sbit = PB_TEMP_UT_WARNING, 1714 }, { 1715 .reg = PMBUS_UT_FAULT_LIMIT, 1716 .low = true, 1717 .attr = "lcrit", 1718 .alarm = "lcrit_alarm", 1719 .sbit = PB_TEMP_UT_FAULT, 1720 }, { 1721 .reg = PMBUS_OT_WARN_LIMIT, 1722 .attr = "max", 1723 .alarm = "max_alarm", 1724 .sbit = PB_TEMP_OT_WARNING, 1725 }, { 1726 .reg = PMBUS_OT_FAULT_LIMIT, 1727 .attr = "crit", 1728 .alarm = "crit_alarm", 1729 .sbit = PB_TEMP_OT_FAULT, 1730 }, { 1731 .reg = PMBUS_VIRT_READ_TEMP_MIN, 1732 .attr = "lowest", 1733 }, { 1734 .reg = PMBUS_VIRT_READ_TEMP_AVG, 1735 .attr = "average", 1736 }, { 1737 .reg = PMBUS_VIRT_READ_TEMP_MAX, 1738 .attr = "highest", 1739 }, { 1740 .reg = PMBUS_VIRT_RESET_TEMP_HISTORY, 1741 .attr = "reset_history", 1742 }, { 1743 .reg = PMBUS_MFR_MAX_TEMP_1, 1744 .attr = "rated_max", 1745 }, 1746 }; 1747 1748 static const struct pmbus_limit_attr temp_limit_attrs2[] = { 1749 { 1750 .reg = PMBUS_UT_WARN_LIMIT, 1751 .low = true, 1752 .attr = "min", 1753 .alarm = "min_alarm", 1754 .sbit = PB_TEMP_UT_WARNING, 1755 }, { 1756 .reg = PMBUS_UT_FAULT_LIMIT, 1757 .low = true, 1758 .attr = "lcrit", 1759 .alarm = "lcrit_alarm", 1760 .sbit = PB_TEMP_UT_FAULT, 1761 }, { 1762 .reg = PMBUS_OT_WARN_LIMIT, 1763 .attr = "max", 1764 .alarm = "max_alarm", 1765 .sbit = PB_TEMP_OT_WARNING, 1766 }, { 1767 .reg = PMBUS_OT_FAULT_LIMIT, 1768 .attr = "crit", 1769 .alarm = "crit_alarm", 1770 .sbit = PB_TEMP_OT_FAULT, 1771 }, { 1772 .reg = PMBUS_VIRT_READ_TEMP2_MIN, 1773 .attr = "lowest", 1774 }, { 1775 .reg = PMBUS_VIRT_READ_TEMP2_AVG, 1776 .attr = "average", 1777 }, { 1778 .reg = PMBUS_VIRT_READ_TEMP2_MAX, 1779 .attr = "highest", 1780 }, { 1781 .reg = PMBUS_VIRT_RESET_TEMP2_HISTORY, 1782 .attr = "reset_history", 1783 }, { 1784 .reg = PMBUS_MFR_MAX_TEMP_2, 1785 .attr = "rated_max", 1786 }, 1787 }; 1788 1789 static const struct pmbus_limit_attr temp_limit_attrs3[] = { 1790 { 1791 .reg = PMBUS_UT_WARN_LIMIT, 1792 .low = true, 1793 .attr = "min", 1794 .alarm = "min_alarm", 1795 .sbit = PB_TEMP_UT_WARNING, 1796 }, { 1797 .reg = PMBUS_UT_FAULT_LIMIT, 1798 .low = true, 1799 .attr = "lcrit", 1800 .alarm = "lcrit_alarm", 1801 .sbit = PB_TEMP_UT_FAULT, 1802 }, { 1803 .reg = PMBUS_OT_WARN_LIMIT, 1804 .attr = "max", 1805 .alarm = "max_alarm", 1806 .sbit = PB_TEMP_OT_WARNING, 1807 }, { 1808 .reg = PMBUS_OT_FAULT_LIMIT, 1809 .attr = "crit", 1810 .alarm = "crit_alarm", 1811 .sbit = PB_TEMP_OT_FAULT, 1812 }, { 1813 .reg = PMBUS_MFR_MAX_TEMP_3, 1814 .attr = "rated_max", 1815 }, 1816 }; 1817 1818 static const struct pmbus_sensor_attr temp_attributes[] = { 1819 { 1820 .reg = PMBUS_READ_TEMPERATURE_1, 1821 .class = PSC_TEMPERATURE, 1822 .paged = true, 1823 .update = true, 1824 .compare = true, 1825 .func = PMBUS_HAVE_TEMP, 1826 .sfunc = PMBUS_HAVE_STATUS_TEMP, 1827 .sreg = PMBUS_STATUS_TEMPERATURE, 1828 .gbit = PB_STATUS_TEMPERATURE, 1829 .limit = temp_limit_attrs, 1830 .nlimit = ARRAY_SIZE(temp_limit_attrs), 1831 }, { 1832 .reg = PMBUS_READ_TEMPERATURE_2, 1833 .class = PSC_TEMPERATURE, 1834 .paged = true, 1835 .update = true, 1836 .compare = true, 1837 .func = PMBUS_HAVE_TEMP2, 1838 .sfunc = PMBUS_HAVE_STATUS_TEMP, 1839 .sreg = PMBUS_STATUS_TEMPERATURE, 1840 .gbit = PB_STATUS_TEMPERATURE, 1841 .limit = temp_limit_attrs2, 1842 .nlimit = ARRAY_SIZE(temp_limit_attrs2), 1843 }, { 1844 .reg = PMBUS_READ_TEMPERATURE_3, 1845 .class = PSC_TEMPERATURE, 1846 .paged = true, 1847 .update = true, 1848 .compare = true, 1849 .func = PMBUS_HAVE_TEMP3, 1850 .sfunc = PMBUS_HAVE_STATUS_TEMP, 1851 .sreg = PMBUS_STATUS_TEMPERATURE, 1852 .gbit = PB_STATUS_TEMPERATURE, 1853 .limit = temp_limit_attrs3, 1854 .nlimit = ARRAY_SIZE(temp_limit_attrs3), 1855 } 1856 }; 1857 1858 static const int pmbus_fan_registers[] = { 1859 PMBUS_READ_FAN_SPEED_1, 1860 PMBUS_READ_FAN_SPEED_2, 1861 PMBUS_READ_FAN_SPEED_3, 1862 PMBUS_READ_FAN_SPEED_4 1863 }; 1864 1865 static const int pmbus_fan_status_registers[] = { 1866 PMBUS_STATUS_FAN_12, 1867 PMBUS_STATUS_FAN_12, 1868 PMBUS_STATUS_FAN_34, 1869 PMBUS_STATUS_FAN_34 1870 }; 1871 1872 static const u32 pmbus_fan_flags[] = { 1873 PMBUS_HAVE_FAN12, 1874 PMBUS_HAVE_FAN12, 1875 PMBUS_HAVE_FAN34, 1876 PMBUS_HAVE_FAN34 1877 }; 1878 1879 static const u32 pmbus_fan_status_flags[] = { 1880 PMBUS_HAVE_STATUS_FAN12, 1881 PMBUS_HAVE_STATUS_FAN12, 1882 PMBUS_HAVE_STATUS_FAN34, 1883 PMBUS_HAVE_STATUS_FAN34 1884 }; 1885 1886 /* Fans */ 1887 1888 /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */ 1889 static int pmbus_add_fan_ctrl(struct i2c_client *client, 1890 struct pmbus_data *data, int index, int page, int id, 1891 u8 config) 1892 { 1893 struct pmbus_sensor *sensor; 1894 1895 sensor = pmbus_add_sensor(data, "fan", "target", index, page, 1896 0xff, PMBUS_VIRT_FAN_TARGET_1 + id, PSC_FAN, 1897 false, false, true); 1898 1899 if (!sensor) 1900 return -ENOMEM; 1901 1902 if (!((data->info->func[page] & PMBUS_HAVE_PWM12) || 1903 (data->info->func[page] & PMBUS_HAVE_PWM34))) 1904 return 0; 1905 1906 sensor = pmbus_add_sensor(data, "pwm", NULL, index, page, 1907 0xff, PMBUS_VIRT_PWM_1 + id, PSC_PWM, 1908 false, false, true); 1909 1910 if (!sensor) 1911 return -ENOMEM; 1912 1913 sensor = pmbus_add_sensor(data, "pwm", "enable", index, page, 1914 0xff, PMBUS_VIRT_PWM_ENABLE_1 + id, PSC_PWM, 1915 true, false, false); 1916 1917 if (!sensor) 1918 return -ENOMEM; 1919 1920 return 0; 1921 } 1922 1923 static int pmbus_add_fan_attributes(struct i2c_client *client, 1924 struct pmbus_data *data) 1925 { 1926 const struct pmbus_driver_info *info = data->info; 1927 int index = 1; 1928 int page; 1929 int ret; 1930 1931 for (page = 0; page < info->pages; page++) { 1932 int f; 1933 1934 for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) { 1935 int regval; 1936 1937 if (!(info->func[page] & pmbus_fan_flags[f])) 1938 break; 1939 1940 if (!pmbus_check_word_register(client, page, 1941 pmbus_fan_registers[f])) 1942 break; 1943 1944 /* 1945 * Skip fan if not installed. 1946 * Each fan configuration register covers multiple fans, 1947 * so we have to do some magic. 1948 */ 1949 regval = _pmbus_read_byte_data(client, page, 1950 pmbus_fan_config_registers[f]); 1951 if (regval < 0 || 1952 (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4))))) 1953 continue; 1954 1955 if (pmbus_add_sensor(data, "fan", "input", index, 1956 page, 0xff, pmbus_fan_registers[f], 1957 PSC_FAN, true, true, true) == NULL) 1958 return -ENOMEM; 1959 1960 /* Fan control */ 1961 if (pmbus_check_word_register(client, page, 1962 pmbus_fan_command_registers[f])) { 1963 ret = pmbus_add_fan_ctrl(client, data, index, 1964 page, f, regval); 1965 if (ret < 0) 1966 return ret; 1967 } 1968 1969 /* 1970 * Each fan status register covers multiple fans, 1971 * so we have to do some magic. 1972 */ 1973 if ((info->func[page] & pmbus_fan_status_flags[f]) && 1974 pmbus_check_byte_register(client, 1975 page, pmbus_fan_status_registers[f])) { 1976 int reg; 1977 1978 if (f > 1) /* fan 3, 4 */ 1979 reg = PMBUS_STATUS_FAN_34; 1980 else 1981 reg = PMBUS_STATUS_FAN_12; 1982 ret = pmbus_add_boolean(data, "fan", 1983 "alarm", index, NULL, NULL, page, reg, 1984 PB_FAN_FAN1_WARNING >> (f & 1)); 1985 if (ret) 1986 return ret; 1987 ret = pmbus_add_boolean(data, "fan", 1988 "fault", index, NULL, NULL, page, reg, 1989 PB_FAN_FAN1_FAULT >> (f & 1)); 1990 if (ret) 1991 return ret; 1992 } 1993 index++; 1994 } 1995 } 1996 return 0; 1997 } 1998 1999 struct pmbus_samples_attr { 2000 int reg; 2001 char *name; 2002 }; 2003 2004 struct pmbus_samples_reg { 2005 int page; 2006 struct pmbus_samples_attr *attr; 2007 struct device_attribute dev_attr; 2008 }; 2009 2010 static struct pmbus_samples_attr pmbus_samples_registers[] = { 2011 { 2012 .reg = PMBUS_VIRT_SAMPLES, 2013 .name = "samples", 2014 }, { 2015 .reg = PMBUS_VIRT_IN_SAMPLES, 2016 .name = "in_samples", 2017 }, { 2018 .reg = PMBUS_VIRT_CURR_SAMPLES, 2019 .name = "curr_samples", 2020 }, { 2021 .reg = PMBUS_VIRT_POWER_SAMPLES, 2022 .name = "power_samples", 2023 }, { 2024 .reg = PMBUS_VIRT_TEMP_SAMPLES, 2025 .name = "temp_samples", 2026 } 2027 }; 2028 2029 #define to_samples_reg(x) container_of(x, struct pmbus_samples_reg, dev_attr) 2030 2031 static ssize_t pmbus_show_samples(struct device *dev, 2032 struct device_attribute *devattr, char *buf) 2033 { 2034 int val; 2035 struct i2c_client *client = to_i2c_client(dev->parent); 2036 struct pmbus_samples_reg *reg = to_samples_reg(devattr); 2037 struct pmbus_data *data = i2c_get_clientdata(client); 2038 2039 mutex_lock(&data->update_lock); 2040 val = _pmbus_read_word_data(client, reg->page, 0xff, reg->attr->reg); 2041 mutex_unlock(&data->update_lock); 2042 if (val < 0) 2043 return val; 2044 2045 return sysfs_emit(buf, "%d\n", val); 2046 } 2047 2048 static ssize_t pmbus_set_samples(struct device *dev, 2049 struct device_attribute *devattr, 2050 const char *buf, size_t count) 2051 { 2052 int ret; 2053 long val; 2054 struct i2c_client *client = to_i2c_client(dev->parent); 2055 struct pmbus_samples_reg *reg = to_samples_reg(devattr); 2056 struct pmbus_data *data = i2c_get_clientdata(client); 2057 2058 if (kstrtol(buf, 0, &val) < 0) 2059 return -EINVAL; 2060 2061 mutex_lock(&data->update_lock); 2062 ret = _pmbus_write_word_data(client, reg->page, reg->attr->reg, val); 2063 mutex_unlock(&data->update_lock); 2064 2065 return ret ? : count; 2066 } 2067 2068 static int pmbus_add_samples_attr(struct pmbus_data *data, int page, 2069 struct pmbus_samples_attr *attr) 2070 { 2071 struct pmbus_samples_reg *reg; 2072 2073 reg = devm_kzalloc(data->dev, sizeof(*reg), GFP_KERNEL); 2074 if (!reg) 2075 return -ENOMEM; 2076 2077 reg->attr = attr; 2078 reg->page = page; 2079 2080 pmbus_dev_attr_init(®->dev_attr, attr->name, 0644, 2081 pmbus_show_samples, pmbus_set_samples); 2082 2083 return pmbus_add_attribute(data, ®->dev_attr.attr); 2084 } 2085 2086 static int pmbus_add_samples_attributes(struct i2c_client *client, 2087 struct pmbus_data *data) 2088 { 2089 const struct pmbus_driver_info *info = data->info; 2090 int s; 2091 2092 if (!(info->func[0] & PMBUS_HAVE_SAMPLES)) 2093 return 0; 2094 2095 for (s = 0; s < ARRAY_SIZE(pmbus_samples_registers); s++) { 2096 struct pmbus_samples_attr *attr; 2097 int ret; 2098 2099 attr = &pmbus_samples_registers[s]; 2100 if (!pmbus_check_word_register(client, 0, attr->reg)) 2101 continue; 2102 2103 ret = pmbus_add_samples_attr(data, 0, attr); 2104 if (ret) 2105 return ret; 2106 } 2107 2108 return 0; 2109 } 2110 2111 static int pmbus_find_attributes(struct i2c_client *client, 2112 struct pmbus_data *data) 2113 { 2114 int ret; 2115 2116 /* Voltage sensors */ 2117 ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes, 2118 ARRAY_SIZE(voltage_attributes)); 2119 if (ret) 2120 return ret; 2121 2122 /* Current sensors */ 2123 ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes, 2124 ARRAY_SIZE(current_attributes)); 2125 if (ret) 2126 return ret; 2127 2128 /* Power sensors */ 2129 ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes, 2130 ARRAY_SIZE(power_attributes)); 2131 if (ret) 2132 return ret; 2133 2134 /* Temperature sensors */ 2135 ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes, 2136 ARRAY_SIZE(temp_attributes)); 2137 if (ret) 2138 return ret; 2139 2140 /* Fans */ 2141 ret = pmbus_add_fan_attributes(client, data); 2142 if (ret) 2143 return ret; 2144 2145 ret = pmbus_add_samples_attributes(client, data); 2146 return ret; 2147 } 2148 2149 /* 2150 * The pmbus_class_attr_map structure maps one sensor class to 2151 * it's corresponding sensor attributes array. 2152 */ 2153 struct pmbus_class_attr_map { 2154 enum pmbus_sensor_classes class; 2155 int nattr; 2156 const struct pmbus_sensor_attr *attr; 2157 }; 2158 2159 static const struct pmbus_class_attr_map class_attr_map[] = { 2160 { 2161 .class = PSC_VOLTAGE_IN, 2162 .attr = voltage_attributes, 2163 .nattr = ARRAY_SIZE(voltage_attributes), 2164 }, { 2165 .class = PSC_VOLTAGE_OUT, 2166 .attr = voltage_attributes, 2167 .nattr = ARRAY_SIZE(voltage_attributes), 2168 }, { 2169 .class = PSC_CURRENT_IN, 2170 .attr = current_attributes, 2171 .nattr = ARRAY_SIZE(current_attributes), 2172 }, { 2173 .class = PSC_CURRENT_OUT, 2174 .attr = current_attributes, 2175 .nattr = ARRAY_SIZE(current_attributes), 2176 }, { 2177 .class = PSC_POWER, 2178 .attr = power_attributes, 2179 .nattr = ARRAY_SIZE(power_attributes), 2180 }, { 2181 .class = PSC_TEMPERATURE, 2182 .attr = temp_attributes, 2183 .nattr = ARRAY_SIZE(temp_attributes), 2184 } 2185 }; 2186 2187 /* 2188 * Read the coefficients for direct mode. 2189 */ 2190 static int pmbus_read_coefficients(struct i2c_client *client, 2191 struct pmbus_driver_info *info, 2192 const struct pmbus_sensor_attr *attr) 2193 { 2194 int rv; 2195 union i2c_smbus_data data; 2196 enum pmbus_sensor_classes class = attr->class; 2197 s8 R; 2198 s16 m, b; 2199 2200 data.block[0] = 2; 2201 data.block[1] = attr->reg; 2202 data.block[2] = 0x01; 2203 2204 rv = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2205 I2C_SMBUS_WRITE, PMBUS_COEFFICIENTS, 2206 I2C_SMBUS_BLOCK_PROC_CALL, &data); 2207 2208 if (rv < 0) 2209 return rv; 2210 2211 if (data.block[0] != 5) 2212 return -EIO; 2213 2214 m = data.block[1] | (data.block[2] << 8); 2215 b = data.block[3] | (data.block[4] << 8); 2216 R = data.block[5]; 2217 info->m[class] = m; 2218 info->b[class] = b; 2219 info->R[class] = R; 2220 2221 return rv; 2222 } 2223 2224 static int pmbus_init_coefficients(struct i2c_client *client, 2225 struct pmbus_driver_info *info) 2226 { 2227 int i, n, ret = -EINVAL; 2228 const struct pmbus_class_attr_map *map; 2229 const struct pmbus_sensor_attr *attr; 2230 2231 for (i = 0; i < ARRAY_SIZE(class_attr_map); i++) { 2232 map = &class_attr_map[i]; 2233 if (info->format[map->class] != direct) 2234 continue; 2235 for (n = 0; n < map->nattr; n++) { 2236 attr = &map->attr[n]; 2237 if (map->class != attr->class) 2238 continue; 2239 ret = pmbus_read_coefficients(client, info, attr); 2240 if (ret >= 0) 2241 break; 2242 } 2243 if (ret < 0) { 2244 dev_err(&client->dev, 2245 "No coefficients found for sensor class %d\n", 2246 map->class); 2247 return -EINVAL; 2248 } 2249 } 2250 2251 return 0; 2252 } 2253 2254 /* 2255 * Identify chip parameters. 2256 * This function is called for all chips. 2257 */ 2258 static int pmbus_identify_common(struct i2c_client *client, 2259 struct pmbus_data *data, int page) 2260 { 2261 int vout_mode = -1; 2262 2263 if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE)) 2264 vout_mode = _pmbus_read_byte_data(client, page, 2265 PMBUS_VOUT_MODE); 2266 if (vout_mode >= 0 && vout_mode != 0xff) { 2267 /* 2268 * Not all chips support the VOUT_MODE command, 2269 * so a failure to read it is not an error. 2270 */ 2271 switch (vout_mode >> 5) { 2272 case 0: /* linear mode */ 2273 if (data->info->format[PSC_VOLTAGE_OUT] != linear) 2274 return -ENODEV; 2275 2276 data->exponent[page] = ((s8)(vout_mode << 3)) >> 3; 2277 break; 2278 case 1: /* VID mode */ 2279 if (data->info->format[PSC_VOLTAGE_OUT] != vid) 2280 return -ENODEV; 2281 break; 2282 case 2: /* direct mode */ 2283 if (data->info->format[PSC_VOLTAGE_OUT] != direct) 2284 return -ENODEV; 2285 break; 2286 default: 2287 return -ENODEV; 2288 } 2289 } 2290 2291 pmbus_clear_fault_page(client, page); 2292 return 0; 2293 } 2294 2295 static int pmbus_read_status_byte(struct i2c_client *client, int page) 2296 { 2297 return _pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE); 2298 } 2299 2300 static int pmbus_read_status_word(struct i2c_client *client, int page) 2301 { 2302 return _pmbus_read_word_data(client, page, 0xff, PMBUS_STATUS_WORD); 2303 } 2304 2305 static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data, 2306 struct pmbus_driver_info *info) 2307 { 2308 struct device *dev = &client->dev; 2309 int page, ret; 2310 2311 /* 2312 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try 2313 * to use PMBUS_STATUS_BYTE instead if that is the case. 2314 * Bail out if both registers are not supported. 2315 */ 2316 data->read_status = pmbus_read_status_word; 2317 ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD); 2318 if (ret < 0 || ret == 0xffff) { 2319 data->read_status = pmbus_read_status_byte; 2320 ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE); 2321 if (ret < 0 || ret == 0xff) { 2322 dev_err(dev, "PMBus status register not found\n"); 2323 return -ENODEV; 2324 } 2325 } else { 2326 data->has_status_word = true; 2327 } 2328 2329 /* Enable PEC if the controller and bus supports it */ 2330 if (!(data->flags & PMBUS_NO_CAPABILITY)) { 2331 ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY); 2332 if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK)) { 2333 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC)) { 2334 client->flags |= I2C_CLIENT_PEC; 2335 } 2336 } 2337 } 2338 2339 /* 2340 * Check if the chip is write protected. If it is, we can not clear 2341 * faults, and we should not try it. Also, in that case, writes into 2342 * limit registers need to be disabled. 2343 */ 2344 if (!(data->flags & PMBUS_NO_WRITE_PROTECT)) { 2345 ret = i2c_smbus_read_byte_data(client, PMBUS_WRITE_PROTECT); 2346 if (ret > 0 && (ret & PB_WP_ANY)) 2347 data->flags |= PMBUS_WRITE_PROTECTED | PMBUS_SKIP_STATUS_CHECK; 2348 } 2349 2350 if (data->info->pages) 2351 pmbus_clear_faults(client); 2352 else 2353 pmbus_clear_fault_page(client, -1); 2354 2355 if (info->identify) { 2356 ret = (*info->identify)(client, info); 2357 if (ret < 0) { 2358 dev_err(dev, "Chip identification failed\n"); 2359 return ret; 2360 } 2361 } 2362 2363 if (info->pages <= 0 || info->pages > PMBUS_PAGES) { 2364 dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages); 2365 return -ENODEV; 2366 } 2367 2368 for (page = 0; page < info->pages; page++) { 2369 ret = pmbus_identify_common(client, data, page); 2370 if (ret < 0) { 2371 dev_err(dev, "Failed to identify chip capabilities\n"); 2372 return ret; 2373 } 2374 } 2375 2376 if (data->flags & PMBUS_USE_COEFFICIENTS_CMD) { 2377 if (!i2c_check_functionality(client->adapter, 2378 I2C_FUNC_SMBUS_BLOCK_PROC_CALL)) 2379 return -ENODEV; 2380 2381 ret = pmbus_init_coefficients(client, info); 2382 if (ret < 0) 2383 return ret; 2384 } 2385 2386 return 0; 2387 } 2388 2389 #if IS_ENABLED(CONFIG_REGULATOR) 2390 static int pmbus_regulator_is_enabled(struct regulator_dev *rdev) 2391 { 2392 struct device *dev = rdev_get_dev(rdev); 2393 struct i2c_client *client = to_i2c_client(dev->parent); 2394 struct pmbus_data *data = i2c_get_clientdata(client); 2395 u8 page = rdev_get_id(rdev); 2396 int ret; 2397 2398 mutex_lock(&data->update_lock); 2399 ret = pmbus_read_byte_data(client, page, PMBUS_OPERATION); 2400 mutex_unlock(&data->update_lock); 2401 2402 if (ret < 0) 2403 return ret; 2404 2405 return !!(ret & PB_OPERATION_CONTROL_ON); 2406 } 2407 2408 static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable) 2409 { 2410 struct device *dev = rdev_get_dev(rdev); 2411 struct i2c_client *client = to_i2c_client(dev->parent); 2412 struct pmbus_data *data = i2c_get_clientdata(client); 2413 u8 page = rdev_get_id(rdev); 2414 int ret; 2415 2416 mutex_lock(&data->update_lock); 2417 ret = pmbus_update_byte_data(client, page, PMBUS_OPERATION, 2418 PB_OPERATION_CONTROL_ON, 2419 enable ? PB_OPERATION_CONTROL_ON : 0); 2420 mutex_unlock(&data->update_lock); 2421 2422 return ret; 2423 } 2424 2425 static int pmbus_regulator_enable(struct regulator_dev *rdev) 2426 { 2427 return _pmbus_regulator_on_off(rdev, 1); 2428 } 2429 2430 static int pmbus_regulator_disable(struct regulator_dev *rdev) 2431 { 2432 return _pmbus_regulator_on_off(rdev, 0); 2433 } 2434 2435 /* A PMBus status flag and the corresponding REGULATOR_ERROR_* flag */ 2436 struct pmbus_regulator_status_assoc { 2437 int pflag, rflag; 2438 }; 2439 2440 /* PMBus->regulator bit mappings for a PMBus status register */ 2441 struct pmbus_regulator_status_category { 2442 int func; 2443 int reg; 2444 const struct pmbus_regulator_status_assoc *bits; /* zero-terminated */ 2445 }; 2446 2447 static const struct pmbus_regulator_status_category pmbus_regulator_flag_map[] = { 2448 { 2449 .func = PMBUS_HAVE_STATUS_VOUT, 2450 .reg = PMBUS_STATUS_VOUT, 2451 .bits = (const struct pmbus_regulator_status_assoc[]) { 2452 { PB_VOLTAGE_UV_WARNING, REGULATOR_ERROR_UNDER_VOLTAGE_WARN }, 2453 { PB_VOLTAGE_UV_FAULT, REGULATOR_ERROR_UNDER_VOLTAGE }, 2454 { PB_VOLTAGE_OV_WARNING, REGULATOR_ERROR_OVER_VOLTAGE_WARN }, 2455 { PB_VOLTAGE_OV_FAULT, REGULATOR_ERROR_REGULATION_OUT }, 2456 { }, 2457 }, 2458 }, { 2459 .func = PMBUS_HAVE_STATUS_IOUT, 2460 .reg = PMBUS_STATUS_IOUT, 2461 .bits = (const struct pmbus_regulator_status_assoc[]) { 2462 { PB_IOUT_OC_WARNING, REGULATOR_ERROR_OVER_CURRENT_WARN }, 2463 { PB_IOUT_OC_FAULT, REGULATOR_ERROR_OVER_CURRENT }, 2464 { PB_IOUT_OC_LV_FAULT, REGULATOR_ERROR_OVER_CURRENT }, 2465 { }, 2466 }, 2467 }, { 2468 .func = PMBUS_HAVE_STATUS_TEMP, 2469 .reg = PMBUS_STATUS_TEMPERATURE, 2470 .bits = (const struct pmbus_regulator_status_assoc[]) { 2471 { PB_TEMP_OT_WARNING, REGULATOR_ERROR_OVER_TEMP_WARN }, 2472 { PB_TEMP_OT_FAULT, REGULATOR_ERROR_OVER_TEMP }, 2473 { }, 2474 }, 2475 }, 2476 }; 2477 2478 static int pmbus_regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags) 2479 { 2480 int i, status; 2481 const struct pmbus_regulator_status_category *cat; 2482 const struct pmbus_regulator_status_assoc *bit; 2483 struct device *dev = rdev_get_dev(rdev); 2484 struct i2c_client *client = to_i2c_client(dev->parent); 2485 struct pmbus_data *data = i2c_get_clientdata(client); 2486 u8 page = rdev_get_id(rdev); 2487 int func = data->info->func[page]; 2488 2489 *flags = 0; 2490 2491 mutex_lock(&data->update_lock); 2492 2493 for (i = 0; i < ARRAY_SIZE(pmbus_regulator_flag_map); i++) { 2494 cat = &pmbus_regulator_flag_map[i]; 2495 if (!(func & cat->func)) 2496 continue; 2497 2498 status = pmbus_read_byte_data(client, page, cat->reg); 2499 if (status < 0) { 2500 mutex_unlock(&data->update_lock); 2501 return status; 2502 } 2503 2504 for (bit = cat->bits; bit->pflag; bit++) { 2505 if (status & bit->pflag) 2506 *flags |= bit->rflag; 2507 } 2508 } 2509 2510 /* 2511 * Map what bits of STATUS_{WORD,BYTE} we can to REGULATOR_ERROR_* 2512 * bits. Some of the other bits are tempting (especially for cases 2513 * where we don't have the relevant PMBUS_HAVE_STATUS_* 2514 * functionality), but there's an unfortunate ambiguity in that 2515 * they're defined as indicating a fault *or* a warning, so we can't 2516 * easily determine whether to report REGULATOR_ERROR_<foo> or 2517 * REGULATOR_ERROR_<foo>_WARN. 2518 */ 2519 status = pmbus_get_status(client, page, PMBUS_STATUS_WORD); 2520 mutex_unlock(&data->update_lock); 2521 if (status < 0) 2522 return status; 2523 2524 if (pmbus_regulator_is_enabled(rdev) && (status & PB_STATUS_OFF)) 2525 *flags |= REGULATOR_ERROR_FAIL; 2526 2527 /* 2528 * Unlike most other status bits, PB_STATUS_{IOUT_OC,VOUT_OV} are 2529 * defined strictly as fault indicators (not warnings). 2530 */ 2531 if (status & PB_STATUS_IOUT_OC) 2532 *flags |= REGULATOR_ERROR_OVER_CURRENT; 2533 if (status & PB_STATUS_VOUT_OV) 2534 *flags |= REGULATOR_ERROR_REGULATION_OUT; 2535 2536 /* 2537 * If we haven't discovered any thermal faults or warnings via 2538 * PMBUS_STATUS_TEMPERATURE, map PB_STATUS_TEMPERATURE to a warning as 2539 * a (conservative) best-effort interpretation. 2540 */ 2541 if (!(*flags & (REGULATOR_ERROR_OVER_TEMP | REGULATOR_ERROR_OVER_TEMP_WARN)) && 2542 (status & PB_STATUS_TEMPERATURE)) 2543 *flags |= REGULATOR_ERROR_OVER_TEMP_WARN; 2544 2545 return 0; 2546 } 2547 2548 const struct regulator_ops pmbus_regulator_ops = { 2549 .enable = pmbus_regulator_enable, 2550 .disable = pmbus_regulator_disable, 2551 .is_enabled = pmbus_regulator_is_enabled, 2552 .get_error_flags = pmbus_regulator_get_error_flags, 2553 }; 2554 EXPORT_SYMBOL_NS_GPL(pmbus_regulator_ops, PMBUS); 2555 2556 static int pmbus_regulator_register(struct pmbus_data *data) 2557 { 2558 struct device *dev = data->dev; 2559 const struct pmbus_driver_info *info = data->info; 2560 const struct pmbus_platform_data *pdata = dev_get_platdata(dev); 2561 struct regulator_dev *rdev; 2562 int i; 2563 2564 for (i = 0; i < info->num_regulators; i++) { 2565 struct regulator_config config = { }; 2566 2567 config.dev = dev; 2568 config.driver_data = data; 2569 2570 if (pdata && pdata->reg_init_data) 2571 config.init_data = &pdata->reg_init_data[i]; 2572 2573 rdev = devm_regulator_register(dev, &info->reg_desc[i], 2574 &config); 2575 if (IS_ERR(rdev)) { 2576 dev_err(dev, "Failed to register %s regulator\n", 2577 info->reg_desc[i].name); 2578 return PTR_ERR(rdev); 2579 } 2580 } 2581 2582 return 0; 2583 } 2584 #else 2585 static int pmbus_regulator_register(struct pmbus_data *data) 2586 { 2587 return 0; 2588 } 2589 #endif 2590 2591 static struct dentry *pmbus_debugfs_dir; /* pmbus debugfs directory */ 2592 2593 #if IS_ENABLED(CONFIG_DEBUG_FS) 2594 static int pmbus_debugfs_get(void *data, u64 *val) 2595 { 2596 int rc; 2597 struct pmbus_debugfs_entry *entry = data; 2598 2599 rc = _pmbus_read_byte_data(entry->client, entry->page, entry->reg); 2600 if (rc < 0) 2601 return rc; 2602 2603 *val = rc; 2604 2605 return 0; 2606 } 2607 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL, 2608 "0x%02llx\n"); 2609 2610 static int pmbus_debugfs_get_status(void *data, u64 *val) 2611 { 2612 int rc; 2613 struct pmbus_debugfs_entry *entry = data; 2614 struct pmbus_data *pdata = i2c_get_clientdata(entry->client); 2615 2616 rc = pdata->read_status(entry->client, entry->page); 2617 if (rc < 0) 2618 return rc; 2619 2620 *val = rc; 2621 2622 return 0; 2623 } 2624 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status, 2625 NULL, "0x%04llx\n"); 2626 2627 static int pmbus_debugfs_get_pec(void *data, u64 *val) 2628 { 2629 struct i2c_client *client = data; 2630 2631 *val = !!(client->flags & I2C_CLIENT_PEC); 2632 2633 return 0; 2634 } 2635 2636 static int pmbus_debugfs_set_pec(void *data, u64 val) 2637 { 2638 int rc; 2639 struct i2c_client *client = data; 2640 2641 if (!val) { 2642 client->flags &= ~I2C_CLIENT_PEC; 2643 return 0; 2644 } 2645 2646 if (val != 1) 2647 return -EINVAL; 2648 2649 rc = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY); 2650 if (rc < 0) 2651 return rc; 2652 2653 if (!(rc & PB_CAPABILITY_ERROR_CHECK)) 2654 return -EOPNOTSUPP; 2655 2656 client->flags |= I2C_CLIENT_PEC; 2657 2658 return 0; 2659 } 2660 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_pec, pmbus_debugfs_get_pec, 2661 pmbus_debugfs_set_pec, "%llu\n"); 2662 2663 static void pmbus_remove_debugfs(void *data) 2664 { 2665 struct dentry *entry = data; 2666 2667 debugfs_remove_recursive(entry); 2668 } 2669 2670 static int pmbus_init_debugfs(struct i2c_client *client, 2671 struct pmbus_data *data) 2672 { 2673 int i, idx = 0; 2674 char name[PMBUS_NAME_SIZE]; 2675 struct pmbus_debugfs_entry *entries; 2676 2677 if (!pmbus_debugfs_dir) 2678 return -ENODEV; 2679 2680 /* 2681 * Create the debugfs directory for this device. Use the hwmon device 2682 * name to avoid conflicts (hwmon numbers are globally unique). 2683 */ 2684 data->debugfs = debugfs_create_dir(dev_name(data->hwmon_dev), 2685 pmbus_debugfs_dir); 2686 if (IS_ERR_OR_NULL(data->debugfs)) { 2687 data->debugfs = NULL; 2688 return -ENODEV; 2689 } 2690 2691 /* Allocate the max possible entries we need. */ 2692 entries = devm_kcalloc(data->dev, 2693 data->info->pages * 10, sizeof(*entries), 2694 GFP_KERNEL); 2695 if (!entries) 2696 return -ENOMEM; 2697 2698 debugfs_create_file("pec", 0664, data->debugfs, client, 2699 &pmbus_debugfs_ops_pec); 2700 2701 for (i = 0; i < data->info->pages; ++i) { 2702 /* Check accessibility of status register if it's not page 0 */ 2703 if (!i || pmbus_check_status_register(client, i)) { 2704 /* No need to set reg as we have special read op. */ 2705 entries[idx].client = client; 2706 entries[idx].page = i; 2707 scnprintf(name, PMBUS_NAME_SIZE, "status%d", i); 2708 debugfs_create_file(name, 0444, data->debugfs, 2709 &entries[idx++], 2710 &pmbus_debugfs_ops_status); 2711 } 2712 2713 if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) { 2714 entries[idx].client = client; 2715 entries[idx].page = i; 2716 entries[idx].reg = PMBUS_STATUS_VOUT; 2717 scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i); 2718 debugfs_create_file(name, 0444, data->debugfs, 2719 &entries[idx++], 2720 &pmbus_debugfs_ops); 2721 } 2722 2723 if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) { 2724 entries[idx].client = client; 2725 entries[idx].page = i; 2726 entries[idx].reg = PMBUS_STATUS_IOUT; 2727 scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i); 2728 debugfs_create_file(name, 0444, data->debugfs, 2729 &entries[idx++], 2730 &pmbus_debugfs_ops); 2731 } 2732 2733 if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) { 2734 entries[idx].client = client; 2735 entries[idx].page = i; 2736 entries[idx].reg = PMBUS_STATUS_INPUT; 2737 scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i); 2738 debugfs_create_file(name, 0444, data->debugfs, 2739 &entries[idx++], 2740 &pmbus_debugfs_ops); 2741 } 2742 2743 if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) { 2744 entries[idx].client = client; 2745 entries[idx].page = i; 2746 entries[idx].reg = PMBUS_STATUS_TEMPERATURE; 2747 scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i); 2748 debugfs_create_file(name, 0444, data->debugfs, 2749 &entries[idx++], 2750 &pmbus_debugfs_ops); 2751 } 2752 2753 if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) { 2754 entries[idx].client = client; 2755 entries[idx].page = i; 2756 entries[idx].reg = PMBUS_STATUS_CML; 2757 scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i); 2758 debugfs_create_file(name, 0444, data->debugfs, 2759 &entries[idx++], 2760 &pmbus_debugfs_ops); 2761 } 2762 2763 if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) { 2764 entries[idx].client = client; 2765 entries[idx].page = i; 2766 entries[idx].reg = PMBUS_STATUS_OTHER; 2767 scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i); 2768 debugfs_create_file(name, 0444, data->debugfs, 2769 &entries[idx++], 2770 &pmbus_debugfs_ops); 2771 } 2772 2773 if (pmbus_check_byte_register(client, i, 2774 PMBUS_STATUS_MFR_SPECIFIC)) { 2775 entries[idx].client = client; 2776 entries[idx].page = i; 2777 entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC; 2778 scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i); 2779 debugfs_create_file(name, 0444, data->debugfs, 2780 &entries[idx++], 2781 &pmbus_debugfs_ops); 2782 } 2783 2784 if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) { 2785 entries[idx].client = client; 2786 entries[idx].page = i; 2787 entries[idx].reg = PMBUS_STATUS_FAN_12; 2788 scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i); 2789 debugfs_create_file(name, 0444, data->debugfs, 2790 &entries[idx++], 2791 &pmbus_debugfs_ops); 2792 } 2793 2794 if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) { 2795 entries[idx].client = client; 2796 entries[idx].page = i; 2797 entries[idx].reg = PMBUS_STATUS_FAN_34; 2798 scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i); 2799 debugfs_create_file(name, 0444, data->debugfs, 2800 &entries[idx++], 2801 &pmbus_debugfs_ops); 2802 } 2803 } 2804 2805 return devm_add_action_or_reset(data->dev, 2806 pmbus_remove_debugfs, data->debugfs); 2807 } 2808 #else 2809 static int pmbus_init_debugfs(struct i2c_client *client, 2810 struct pmbus_data *data) 2811 { 2812 return 0; 2813 } 2814 #endif /* IS_ENABLED(CONFIG_DEBUG_FS) */ 2815 2816 int pmbus_do_probe(struct i2c_client *client, struct pmbus_driver_info *info) 2817 { 2818 struct device *dev = &client->dev; 2819 const struct pmbus_platform_data *pdata = dev_get_platdata(dev); 2820 struct pmbus_data *data; 2821 size_t groups_num = 0; 2822 int ret; 2823 char *name; 2824 2825 if (!info) 2826 return -ENODEV; 2827 2828 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE 2829 | I2C_FUNC_SMBUS_BYTE_DATA 2830 | I2C_FUNC_SMBUS_WORD_DATA)) 2831 return -ENODEV; 2832 2833 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 2834 if (!data) 2835 return -ENOMEM; 2836 2837 if (info->groups) 2838 while (info->groups[groups_num]) 2839 groups_num++; 2840 2841 data->groups = devm_kcalloc(dev, groups_num + 2, sizeof(void *), 2842 GFP_KERNEL); 2843 if (!data->groups) 2844 return -ENOMEM; 2845 2846 i2c_set_clientdata(client, data); 2847 mutex_init(&data->update_lock); 2848 data->dev = dev; 2849 2850 if (pdata) 2851 data->flags = pdata->flags; 2852 data->info = info; 2853 data->currpage = -1; 2854 data->currphase = -1; 2855 2856 ret = pmbus_init_common(client, data, info); 2857 if (ret < 0) 2858 return ret; 2859 2860 ret = pmbus_find_attributes(client, data); 2861 if (ret) 2862 return ret; 2863 2864 /* 2865 * If there are no attributes, something is wrong. 2866 * Bail out instead of trying to register nothing. 2867 */ 2868 if (!data->num_attributes) { 2869 dev_err(dev, "No attributes found\n"); 2870 return -ENODEV; 2871 } 2872 2873 name = devm_kstrdup(dev, client->name, GFP_KERNEL); 2874 if (!name) 2875 return -ENOMEM; 2876 strreplace(name, '-', '_'); 2877 2878 data->groups[0] = &data->group; 2879 memcpy(data->groups + 1, info->groups, sizeof(void *) * groups_num); 2880 data->hwmon_dev = devm_hwmon_device_register_with_groups(dev, 2881 name, data, data->groups); 2882 if (IS_ERR(data->hwmon_dev)) { 2883 dev_err(dev, "Failed to register hwmon device\n"); 2884 return PTR_ERR(data->hwmon_dev); 2885 } 2886 2887 ret = pmbus_regulator_register(data); 2888 if (ret) 2889 return ret; 2890 2891 ret = pmbus_init_debugfs(client, data); 2892 if (ret) 2893 dev_warn(dev, "Failed to register debugfs\n"); 2894 2895 return 0; 2896 } 2897 EXPORT_SYMBOL_NS_GPL(pmbus_do_probe, PMBUS); 2898 2899 struct dentry *pmbus_get_debugfs_dir(struct i2c_client *client) 2900 { 2901 struct pmbus_data *data = i2c_get_clientdata(client); 2902 2903 return data->debugfs; 2904 } 2905 EXPORT_SYMBOL_NS_GPL(pmbus_get_debugfs_dir, PMBUS); 2906 2907 static int __init pmbus_core_init(void) 2908 { 2909 pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL); 2910 if (IS_ERR(pmbus_debugfs_dir)) 2911 pmbus_debugfs_dir = NULL; 2912 2913 return 0; 2914 } 2915 2916 static void __exit pmbus_core_exit(void) 2917 { 2918 debugfs_remove_recursive(pmbus_debugfs_dir); 2919 } 2920 2921 module_init(pmbus_core_init); 2922 module_exit(pmbus_core_exit); 2923 2924 MODULE_AUTHOR("Guenter Roeck"); 2925 MODULE_DESCRIPTION("PMBus core driver"); 2926 MODULE_LICENSE("GPL"); 2927