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 (s1 && s2) { 915 s64 v1, v2; 916 917 if (s1->data < 0) { 918 ret = s1->data; 919 goto unlock; 920 } 921 if (s2->data < 0) { 922 ret = s2->data; 923 goto unlock; 924 } 925 926 v1 = pmbus_reg2data(data, s1); 927 v2 = pmbus_reg2data(data, s2); 928 ret = !!(regval && v1 >= v2); 929 } else { 930 ret = !!regval; 931 } 932 unlock: 933 mutex_unlock(&data->update_lock); 934 return ret; 935 } 936 937 static ssize_t pmbus_show_boolean(struct device *dev, 938 struct device_attribute *da, char *buf) 939 { 940 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 941 struct pmbus_boolean *boolean = to_pmbus_boolean(attr); 942 struct i2c_client *client = to_i2c_client(dev->parent); 943 int val; 944 945 val = pmbus_get_boolean(client, boolean, attr->index); 946 if (val < 0) 947 return val; 948 return sysfs_emit(buf, "%d\n", val); 949 } 950 951 static ssize_t pmbus_show_sensor(struct device *dev, 952 struct device_attribute *devattr, char *buf) 953 { 954 struct i2c_client *client = to_i2c_client(dev->parent); 955 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr); 956 struct pmbus_data *data = i2c_get_clientdata(client); 957 ssize_t ret; 958 959 mutex_lock(&data->update_lock); 960 pmbus_update_sensor_data(client, sensor); 961 if (sensor->data < 0) 962 ret = sensor->data; 963 else 964 ret = sysfs_emit(buf, "%lld\n", pmbus_reg2data(data, sensor)); 965 mutex_unlock(&data->update_lock); 966 return ret; 967 } 968 969 static ssize_t pmbus_set_sensor(struct device *dev, 970 struct device_attribute *devattr, 971 const char *buf, size_t count) 972 { 973 struct i2c_client *client = to_i2c_client(dev->parent); 974 struct pmbus_data *data = i2c_get_clientdata(client); 975 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr); 976 ssize_t rv = count; 977 s64 val; 978 int ret; 979 u16 regval; 980 981 if (kstrtos64(buf, 10, &val) < 0) 982 return -EINVAL; 983 984 mutex_lock(&data->update_lock); 985 regval = pmbus_data2reg(data, sensor, val); 986 ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval); 987 if (ret < 0) 988 rv = ret; 989 else 990 sensor->data = -ENODATA; 991 mutex_unlock(&data->update_lock); 992 return rv; 993 } 994 995 static ssize_t pmbus_show_label(struct device *dev, 996 struct device_attribute *da, char *buf) 997 { 998 struct pmbus_label *label = to_pmbus_label(da); 999 1000 return sysfs_emit(buf, "%s\n", label->label); 1001 } 1002 1003 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr) 1004 { 1005 if (data->num_attributes >= data->max_attributes - 1) { 1006 int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE; 1007 void *new_attrs = devm_krealloc(data->dev, data->group.attrs, 1008 new_max_attrs * sizeof(void *), 1009 GFP_KERNEL); 1010 if (!new_attrs) 1011 return -ENOMEM; 1012 data->group.attrs = new_attrs; 1013 data->max_attributes = new_max_attrs; 1014 } 1015 1016 data->group.attrs[data->num_attributes++] = attr; 1017 data->group.attrs[data->num_attributes] = NULL; 1018 return 0; 1019 } 1020 1021 static void pmbus_dev_attr_init(struct device_attribute *dev_attr, 1022 const char *name, 1023 umode_t mode, 1024 ssize_t (*show)(struct device *dev, 1025 struct device_attribute *attr, 1026 char *buf), 1027 ssize_t (*store)(struct device *dev, 1028 struct device_attribute *attr, 1029 const char *buf, size_t count)) 1030 { 1031 sysfs_attr_init(&dev_attr->attr); 1032 dev_attr->attr.name = name; 1033 dev_attr->attr.mode = mode; 1034 dev_attr->show = show; 1035 dev_attr->store = store; 1036 } 1037 1038 static void pmbus_attr_init(struct sensor_device_attribute *a, 1039 const char *name, 1040 umode_t mode, 1041 ssize_t (*show)(struct device *dev, 1042 struct device_attribute *attr, 1043 char *buf), 1044 ssize_t (*store)(struct device *dev, 1045 struct device_attribute *attr, 1046 const char *buf, size_t count), 1047 int idx) 1048 { 1049 pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store); 1050 a->index = idx; 1051 } 1052 1053 static int pmbus_add_boolean(struct pmbus_data *data, 1054 const char *name, const char *type, int seq, 1055 struct pmbus_sensor *s1, 1056 struct pmbus_sensor *s2, 1057 u8 page, u16 reg, u16 mask) 1058 { 1059 struct pmbus_boolean *boolean; 1060 struct sensor_device_attribute *a; 1061 1062 if (WARN((s1 && !s2) || (!s1 && s2), "Bad s1/s2 parameters\n")) 1063 return -EINVAL; 1064 1065 boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL); 1066 if (!boolean) 1067 return -ENOMEM; 1068 1069 a = &boolean->attribute; 1070 1071 snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s", 1072 name, seq, type); 1073 boolean->s1 = s1; 1074 boolean->s2 = s2; 1075 pmbus_attr_init(a, boolean->name, 0444, pmbus_show_boolean, NULL, 1076 pb_reg_to_index(page, reg, mask)); 1077 1078 return pmbus_add_attribute(data, &a->dev_attr.attr); 1079 } 1080 1081 static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data, 1082 const char *name, const char *type, 1083 int seq, int page, int phase, 1084 int reg, 1085 enum pmbus_sensor_classes class, 1086 bool update, bool readonly, 1087 bool convert) 1088 { 1089 struct pmbus_sensor *sensor; 1090 struct device_attribute *a; 1091 1092 sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL); 1093 if (!sensor) 1094 return NULL; 1095 a = &sensor->attribute; 1096 1097 if (type) 1098 snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s", 1099 name, seq, type); 1100 else 1101 snprintf(sensor->name, sizeof(sensor->name), "%s%d", 1102 name, seq); 1103 1104 if (data->flags & PMBUS_WRITE_PROTECTED) 1105 readonly = true; 1106 1107 sensor->page = page; 1108 sensor->phase = phase; 1109 sensor->reg = reg; 1110 sensor->class = class; 1111 sensor->update = update; 1112 sensor->convert = convert; 1113 sensor->data = -ENODATA; 1114 pmbus_dev_attr_init(a, sensor->name, 1115 readonly ? 0444 : 0644, 1116 pmbus_show_sensor, pmbus_set_sensor); 1117 1118 if (pmbus_add_attribute(data, &a->attr)) 1119 return NULL; 1120 1121 sensor->next = data->sensors; 1122 data->sensors = sensor; 1123 1124 return sensor; 1125 } 1126 1127 static int pmbus_add_label(struct pmbus_data *data, 1128 const char *name, int seq, 1129 const char *lstring, int index, int phase) 1130 { 1131 struct pmbus_label *label; 1132 struct device_attribute *a; 1133 1134 label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL); 1135 if (!label) 1136 return -ENOMEM; 1137 1138 a = &label->attribute; 1139 1140 snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq); 1141 if (!index) { 1142 if (phase == 0xff) 1143 strncpy(label->label, lstring, 1144 sizeof(label->label) - 1); 1145 else 1146 snprintf(label->label, sizeof(label->label), "%s.%d", 1147 lstring, phase); 1148 } else { 1149 if (phase == 0xff) 1150 snprintf(label->label, sizeof(label->label), "%s%d", 1151 lstring, index); 1152 else 1153 snprintf(label->label, sizeof(label->label), "%s%d.%d", 1154 lstring, index, phase); 1155 } 1156 1157 pmbus_dev_attr_init(a, label->name, 0444, pmbus_show_label, NULL); 1158 return pmbus_add_attribute(data, &a->attr); 1159 } 1160 1161 /* 1162 * Search for attributes. Allocate sensors, booleans, and labels as needed. 1163 */ 1164 1165 /* 1166 * The pmbus_limit_attr structure describes a single limit attribute 1167 * and its associated alarm attribute. 1168 */ 1169 struct pmbus_limit_attr { 1170 u16 reg; /* Limit register */ 1171 u16 sbit; /* Alarm attribute status bit */ 1172 bool update; /* True if register needs updates */ 1173 bool low; /* True if low limit; for limits with compare 1174 functions only */ 1175 const char *attr; /* Attribute name */ 1176 const char *alarm; /* Alarm attribute name */ 1177 }; 1178 1179 /* 1180 * The pmbus_sensor_attr structure describes one sensor attribute. This 1181 * description includes a reference to the associated limit attributes. 1182 */ 1183 struct pmbus_sensor_attr { 1184 u16 reg; /* sensor register */ 1185 u16 gbit; /* generic status bit */ 1186 u8 nlimit; /* # of limit registers */ 1187 enum pmbus_sensor_classes class;/* sensor class */ 1188 const char *label; /* sensor label */ 1189 bool paged; /* true if paged sensor */ 1190 bool update; /* true if update needed */ 1191 bool compare; /* true if compare function needed */ 1192 u32 func; /* sensor mask */ 1193 u32 sfunc; /* sensor status mask */ 1194 int sreg; /* status register */ 1195 const struct pmbus_limit_attr *limit;/* limit registers */ 1196 }; 1197 1198 /* 1199 * Add a set of limit attributes and, if supported, the associated 1200 * alarm attributes. 1201 * returns 0 if no alarm register found, 1 if an alarm register was found, 1202 * < 0 on errors. 1203 */ 1204 static int pmbus_add_limit_attrs(struct i2c_client *client, 1205 struct pmbus_data *data, 1206 const struct pmbus_driver_info *info, 1207 const char *name, int index, int page, 1208 struct pmbus_sensor *base, 1209 const struct pmbus_sensor_attr *attr) 1210 { 1211 const struct pmbus_limit_attr *l = attr->limit; 1212 int nlimit = attr->nlimit; 1213 int have_alarm = 0; 1214 int i, ret; 1215 struct pmbus_sensor *curr; 1216 1217 for (i = 0; i < nlimit; i++) { 1218 if (pmbus_check_word_register(client, page, l->reg)) { 1219 curr = pmbus_add_sensor(data, name, l->attr, index, 1220 page, 0xff, l->reg, attr->class, 1221 attr->update || l->update, 1222 false, true); 1223 if (!curr) 1224 return -ENOMEM; 1225 if (l->sbit && (info->func[page] & attr->sfunc)) { 1226 ret = pmbus_add_boolean(data, name, 1227 l->alarm, index, 1228 attr->compare ? l->low ? curr : base 1229 : NULL, 1230 attr->compare ? l->low ? base : curr 1231 : NULL, 1232 page, attr->sreg, l->sbit); 1233 if (ret) 1234 return ret; 1235 have_alarm = 1; 1236 } 1237 } 1238 l++; 1239 } 1240 return have_alarm; 1241 } 1242 1243 static int pmbus_add_sensor_attrs_one(struct i2c_client *client, 1244 struct pmbus_data *data, 1245 const struct pmbus_driver_info *info, 1246 const char *name, 1247 int index, int page, int phase, 1248 const struct pmbus_sensor_attr *attr, 1249 bool paged) 1250 { 1251 struct pmbus_sensor *base; 1252 bool upper = !!(attr->gbit & 0xff00); /* need to check STATUS_WORD */ 1253 int ret; 1254 1255 if (attr->label) { 1256 ret = pmbus_add_label(data, name, index, attr->label, 1257 paged ? page + 1 : 0, phase); 1258 if (ret) 1259 return ret; 1260 } 1261 base = pmbus_add_sensor(data, name, "input", index, page, phase, 1262 attr->reg, attr->class, true, true, true); 1263 if (!base) 1264 return -ENOMEM; 1265 /* No limit and alarm attributes for phase specific sensors */ 1266 if (attr->sfunc && phase == 0xff) { 1267 ret = pmbus_add_limit_attrs(client, data, info, name, 1268 index, page, base, attr); 1269 if (ret < 0) 1270 return ret; 1271 /* 1272 * Add generic alarm attribute only if there are no individual 1273 * alarm attributes, if there is a global alarm bit, and if 1274 * the generic status register (word or byte, depending on 1275 * which global bit is set) for this page is accessible. 1276 */ 1277 if (!ret && attr->gbit && 1278 (!upper || data->has_status_word) && 1279 pmbus_check_status_register(client, page)) { 1280 ret = pmbus_add_boolean(data, name, "alarm", index, 1281 NULL, NULL, 1282 page, PMBUS_STATUS_WORD, 1283 attr->gbit); 1284 if (ret) 1285 return ret; 1286 } 1287 } 1288 return 0; 1289 } 1290 1291 static bool pmbus_sensor_is_paged(const struct pmbus_driver_info *info, 1292 const struct pmbus_sensor_attr *attr) 1293 { 1294 int p; 1295 1296 if (attr->paged) 1297 return true; 1298 1299 /* 1300 * Some attributes may be present on more than one page despite 1301 * not being marked with the paged attribute. If that is the case, 1302 * then treat the sensor as being paged and add the page suffix to the 1303 * attribute name. 1304 * We don't just add the paged attribute to all such attributes, in 1305 * order to maintain the un-suffixed labels in the case where the 1306 * attribute is only on page 0. 1307 */ 1308 for (p = 1; p < info->pages; p++) { 1309 if (info->func[p] & attr->func) 1310 return true; 1311 } 1312 return false; 1313 } 1314 1315 static int pmbus_add_sensor_attrs(struct i2c_client *client, 1316 struct pmbus_data *data, 1317 const char *name, 1318 const struct pmbus_sensor_attr *attrs, 1319 int nattrs) 1320 { 1321 const struct pmbus_driver_info *info = data->info; 1322 int index, i; 1323 int ret; 1324 1325 index = 1; 1326 for (i = 0; i < nattrs; i++) { 1327 int page, pages; 1328 bool paged = pmbus_sensor_is_paged(info, attrs); 1329 1330 pages = paged ? info->pages : 1; 1331 for (page = 0; page < pages; page++) { 1332 if (info->func[page] & attrs->func) { 1333 ret = pmbus_add_sensor_attrs_one(client, data, info, 1334 name, index, page, 1335 0xff, attrs, paged); 1336 if (ret) 1337 return ret; 1338 index++; 1339 } 1340 if (info->phases[page]) { 1341 int phase; 1342 1343 for (phase = 0; phase < info->phases[page]; 1344 phase++) { 1345 if (!(info->pfunc[phase] & attrs->func)) 1346 continue; 1347 ret = pmbus_add_sensor_attrs_one(client, 1348 data, info, name, index, page, 1349 phase, attrs, paged); 1350 if (ret) 1351 return ret; 1352 index++; 1353 } 1354 } 1355 } 1356 attrs++; 1357 } 1358 return 0; 1359 } 1360 1361 static const struct pmbus_limit_attr vin_limit_attrs[] = { 1362 { 1363 .reg = PMBUS_VIN_UV_WARN_LIMIT, 1364 .attr = "min", 1365 .alarm = "min_alarm", 1366 .sbit = PB_VOLTAGE_UV_WARNING, 1367 }, { 1368 .reg = PMBUS_VIN_UV_FAULT_LIMIT, 1369 .attr = "lcrit", 1370 .alarm = "lcrit_alarm", 1371 .sbit = PB_VOLTAGE_UV_FAULT, 1372 }, { 1373 .reg = PMBUS_VIN_OV_WARN_LIMIT, 1374 .attr = "max", 1375 .alarm = "max_alarm", 1376 .sbit = PB_VOLTAGE_OV_WARNING, 1377 }, { 1378 .reg = PMBUS_VIN_OV_FAULT_LIMIT, 1379 .attr = "crit", 1380 .alarm = "crit_alarm", 1381 .sbit = PB_VOLTAGE_OV_FAULT, 1382 }, { 1383 .reg = PMBUS_VIRT_READ_VIN_AVG, 1384 .update = true, 1385 .attr = "average", 1386 }, { 1387 .reg = PMBUS_VIRT_READ_VIN_MIN, 1388 .update = true, 1389 .attr = "lowest", 1390 }, { 1391 .reg = PMBUS_VIRT_READ_VIN_MAX, 1392 .update = true, 1393 .attr = "highest", 1394 }, { 1395 .reg = PMBUS_VIRT_RESET_VIN_HISTORY, 1396 .attr = "reset_history", 1397 }, { 1398 .reg = PMBUS_MFR_VIN_MIN, 1399 .attr = "rated_min", 1400 }, { 1401 .reg = PMBUS_MFR_VIN_MAX, 1402 .attr = "rated_max", 1403 }, 1404 }; 1405 1406 static const struct pmbus_limit_attr vmon_limit_attrs[] = { 1407 { 1408 .reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT, 1409 .attr = "min", 1410 .alarm = "min_alarm", 1411 .sbit = PB_VOLTAGE_UV_WARNING, 1412 }, { 1413 .reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT, 1414 .attr = "lcrit", 1415 .alarm = "lcrit_alarm", 1416 .sbit = PB_VOLTAGE_UV_FAULT, 1417 }, { 1418 .reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT, 1419 .attr = "max", 1420 .alarm = "max_alarm", 1421 .sbit = PB_VOLTAGE_OV_WARNING, 1422 }, { 1423 .reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT, 1424 .attr = "crit", 1425 .alarm = "crit_alarm", 1426 .sbit = PB_VOLTAGE_OV_FAULT, 1427 } 1428 }; 1429 1430 static const struct pmbus_limit_attr vout_limit_attrs[] = { 1431 { 1432 .reg = PMBUS_VOUT_UV_WARN_LIMIT, 1433 .attr = "min", 1434 .alarm = "min_alarm", 1435 .sbit = PB_VOLTAGE_UV_WARNING, 1436 }, { 1437 .reg = PMBUS_VOUT_UV_FAULT_LIMIT, 1438 .attr = "lcrit", 1439 .alarm = "lcrit_alarm", 1440 .sbit = PB_VOLTAGE_UV_FAULT, 1441 }, { 1442 .reg = PMBUS_VOUT_OV_WARN_LIMIT, 1443 .attr = "max", 1444 .alarm = "max_alarm", 1445 .sbit = PB_VOLTAGE_OV_WARNING, 1446 }, { 1447 .reg = PMBUS_VOUT_OV_FAULT_LIMIT, 1448 .attr = "crit", 1449 .alarm = "crit_alarm", 1450 .sbit = PB_VOLTAGE_OV_FAULT, 1451 }, { 1452 .reg = PMBUS_VIRT_READ_VOUT_AVG, 1453 .update = true, 1454 .attr = "average", 1455 }, { 1456 .reg = PMBUS_VIRT_READ_VOUT_MIN, 1457 .update = true, 1458 .attr = "lowest", 1459 }, { 1460 .reg = PMBUS_VIRT_READ_VOUT_MAX, 1461 .update = true, 1462 .attr = "highest", 1463 }, { 1464 .reg = PMBUS_VIRT_RESET_VOUT_HISTORY, 1465 .attr = "reset_history", 1466 }, { 1467 .reg = PMBUS_MFR_VOUT_MIN, 1468 .attr = "rated_min", 1469 }, { 1470 .reg = PMBUS_MFR_VOUT_MAX, 1471 .attr = "rated_max", 1472 }, 1473 }; 1474 1475 static const struct pmbus_sensor_attr voltage_attributes[] = { 1476 { 1477 .reg = PMBUS_READ_VIN, 1478 .class = PSC_VOLTAGE_IN, 1479 .label = "vin", 1480 .func = PMBUS_HAVE_VIN, 1481 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1482 .sreg = PMBUS_STATUS_INPUT, 1483 .gbit = PB_STATUS_VIN_UV, 1484 .limit = vin_limit_attrs, 1485 .nlimit = ARRAY_SIZE(vin_limit_attrs), 1486 }, { 1487 .reg = PMBUS_VIRT_READ_VMON, 1488 .class = PSC_VOLTAGE_IN, 1489 .label = "vmon", 1490 .func = PMBUS_HAVE_VMON, 1491 .sfunc = PMBUS_HAVE_STATUS_VMON, 1492 .sreg = PMBUS_VIRT_STATUS_VMON, 1493 .limit = vmon_limit_attrs, 1494 .nlimit = ARRAY_SIZE(vmon_limit_attrs), 1495 }, { 1496 .reg = PMBUS_READ_VCAP, 1497 .class = PSC_VOLTAGE_IN, 1498 .label = "vcap", 1499 .func = PMBUS_HAVE_VCAP, 1500 }, { 1501 .reg = PMBUS_READ_VOUT, 1502 .class = PSC_VOLTAGE_OUT, 1503 .label = "vout", 1504 .paged = true, 1505 .func = PMBUS_HAVE_VOUT, 1506 .sfunc = PMBUS_HAVE_STATUS_VOUT, 1507 .sreg = PMBUS_STATUS_VOUT, 1508 .gbit = PB_STATUS_VOUT_OV, 1509 .limit = vout_limit_attrs, 1510 .nlimit = ARRAY_SIZE(vout_limit_attrs), 1511 } 1512 }; 1513 1514 /* Current attributes */ 1515 1516 static const struct pmbus_limit_attr iin_limit_attrs[] = { 1517 { 1518 .reg = PMBUS_IIN_OC_WARN_LIMIT, 1519 .attr = "max", 1520 .alarm = "max_alarm", 1521 .sbit = PB_IIN_OC_WARNING, 1522 }, { 1523 .reg = PMBUS_IIN_OC_FAULT_LIMIT, 1524 .attr = "crit", 1525 .alarm = "crit_alarm", 1526 .sbit = PB_IIN_OC_FAULT, 1527 }, { 1528 .reg = PMBUS_VIRT_READ_IIN_AVG, 1529 .update = true, 1530 .attr = "average", 1531 }, { 1532 .reg = PMBUS_VIRT_READ_IIN_MIN, 1533 .update = true, 1534 .attr = "lowest", 1535 }, { 1536 .reg = PMBUS_VIRT_READ_IIN_MAX, 1537 .update = true, 1538 .attr = "highest", 1539 }, { 1540 .reg = PMBUS_VIRT_RESET_IIN_HISTORY, 1541 .attr = "reset_history", 1542 }, { 1543 .reg = PMBUS_MFR_IIN_MAX, 1544 .attr = "rated_max", 1545 }, 1546 }; 1547 1548 static const struct pmbus_limit_attr iout_limit_attrs[] = { 1549 { 1550 .reg = PMBUS_IOUT_OC_WARN_LIMIT, 1551 .attr = "max", 1552 .alarm = "max_alarm", 1553 .sbit = PB_IOUT_OC_WARNING, 1554 }, { 1555 .reg = PMBUS_IOUT_UC_FAULT_LIMIT, 1556 .attr = "lcrit", 1557 .alarm = "lcrit_alarm", 1558 .sbit = PB_IOUT_UC_FAULT, 1559 }, { 1560 .reg = PMBUS_IOUT_OC_FAULT_LIMIT, 1561 .attr = "crit", 1562 .alarm = "crit_alarm", 1563 .sbit = PB_IOUT_OC_FAULT, 1564 }, { 1565 .reg = PMBUS_VIRT_READ_IOUT_AVG, 1566 .update = true, 1567 .attr = "average", 1568 }, { 1569 .reg = PMBUS_VIRT_READ_IOUT_MIN, 1570 .update = true, 1571 .attr = "lowest", 1572 }, { 1573 .reg = PMBUS_VIRT_READ_IOUT_MAX, 1574 .update = true, 1575 .attr = "highest", 1576 }, { 1577 .reg = PMBUS_VIRT_RESET_IOUT_HISTORY, 1578 .attr = "reset_history", 1579 }, { 1580 .reg = PMBUS_MFR_IOUT_MAX, 1581 .attr = "rated_max", 1582 }, 1583 }; 1584 1585 static const struct pmbus_sensor_attr current_attributes[] = { 1586 { 1587 .reg = PMBUS_READ_IIN, 1588 .class = PSC_CURRENT_IN, 1589 .label = "iin", 1590 .func = PMBUS_HAVE_IIN, 1591 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1592 .sreg = PMBUS_STATUS_INPUT, 1593 .gbit = PB_STATUS_INPUT, 1594 .limit = iin_limit_attrs, 1595 .nlimit = ARRAY_SIZE(iin_limit_attrs), 1596 }, { 1597 .reg = PMBUS_READ_IOUT, 1598 .class = PSC_CURRENT_OUT, 1599 .label = "iout", 1600 .paged = true, 1601 .func = PMBUS_HAVE_IOUT, 1602 .sfunc = PMBUS_HAVE_STATUS_IOUT, 1603 .sreg = PMBUS_STATUS_IOUT, 1604 .gbit = PB_STATUS_IOUT_OC, 1605 .limit = iout_limit_attrs, 1606 .nlimit = ARRAY_SIZE(iout_limit_attrs), 1607 } 1608 }; 1609 1610 /* Power attributes */ 1611 1612 static const struct pmbus_limit_attr pin_limit_attrs[] = { 1613 { 1614 .reg = PMBUS_PIN_OP_WARN_LIMIT, 1615 .attr = "max", 1616 .alarm = "alarm", 1617 .sbit = PB_PIN_OP_WARNING, 1618 }, { 1619 .reg = PMBUS_VIRT_READ_PIN_AVG, 1620 .update = true, 1621 .attr = "average", 1622 }, { 1623 .reg = PMBUS_VIRT_READ_PIN_MIN, 1624 .update = true, 1625 .attr = "input_lowest", 1626 }, { 1627 .reg = PMBUS_VIRT_READ_PIN_MAX, 1628 .update = true, 1629 .attr = "input_highest", 1630 }, { 1631 .reg = PMBUS_VIRT_RESET_PIN_HISTORY, 1632 .attr = "reset_history", 1633 }, { 1634 .reg = PMBUS_MFR_PIN_MAX, 1635 .attr = "rated_max", 1636 }, 1637 }; 1638 1639 static const struct pmbus_limit_attr pout_limit_attrs[] = { 1640 { 1641 .reg = PMBUS_POUT_MAX, 1642 .attr = "cap", 1643 .alarm = "cap_alarm", 1644 .sbit = PB_POWER_LIMITING, 1645 }, { 1646 .reg = PMBUS_POUT_OP_WARN_LIMIT, 1647 .attr = "max", 1648 .alarm = "max_alarm", 1649 .sbit = PB_POUT_OP_WARNING, 1650 }, { 1651 .reg = PMBUS_POUT_OP_FAULT_LIMIT, 1652 .attr = "crit", 1653 .alarm = "crit_alarm", 1654 .sbit = PB_POUT_OP_FAULT, 1655 }, { 1656 .reg = PMBUS_VIRT_READ_POUT_AVG, 1657 .update = true, 1658 .attr = "average", 1659 }, { 1660 .reg = PMBUS_VIRT_READ_POUT_MIN, 1661 .update = true, 1662 .attr = "input_lowest", 1663 }, { 1664 .reg = PMBUS_VIRT_READ_POUT_MAX, 1665 .update = true, 1666 .attr = "input_highest", 1667 }, { 1668 .reg = PMBUS_VIRT_RESET_POUT_HISTORY, 1669 .attr = "reset_history", 1670 }, { 1671 .reg = PMBUS_MFR_POUT_MAX, 1672 .attr = "rated_max", 1673 }, 1674 }; 1675 1676 static const struct pmbus_sensor_attr power_attributes[] = { 1677 { 1678 .reg = PMBUS_READ_PIN, 1679 .class = PSC_POWER, 1680 .label = "pin", 1681 .func = PMBUS_HAVE_PIN, 1682 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1683 .sreg = PMBUS_STATUS_INPUT, 1684 .gbit = PB_STATUS_INPUT, 1685 .limit = pin_limit_attrs, 1686 .nlimit = ARRAY_SIZE(pin_limit_attrs), 1687 }, { 1688 .reg = PMBUS_READ_POUT, 1689 .class = PSC_POWER, 1690 .label = "pout", 1691 .paged = true, 1692 .func = PMBUS_HAVE_POUT, 1693 .sfunc = PMBUS_HAVE_STATUS_IOUT, 1694 .sreg = PMBUS_STATUS_IOUT, 1695 .limit = pout_limit_attrs, 1696 .nlimit = ARRAY_SIZE(pout_limit_attrs), 1697 } 1698 }; 1699 1700 /* Temperature atributes */ 1701 1702 static const struct pmbus_limit_attr temp_limit_attrs[] = { 1703 { 1704 .reg = PMBUS_UT_WARN_LIMIT, 1705 .low = true, 1706 .attr = "min", 1707 .alarm = "min_alarm", 1708 .sbit = PB_TEMP_UT_WARNING, 1709 }, { 1710 .reg = PMBUS_UT_FAULT_LIMIT, 1711 .low = true, 1712 .attr = "lcrit", 1713 .alarm = "lcrit_alarm", 1714 .sbit = PB_TEMP_UT_FAULT, 1715 }, { 1716 .reg = PMBUS_OT_WARN_LIMIT, 1717 .attr = "max", 1718 .alarm = "max_alarm", 1719 .sbit = PB_TEMP_OT_WARNING, 1720 }, { 1721 .reg = PMBUS_OT_FAULT_LIMIT, 1722 .attr = "crit", 1723 .alarm = "crit_alarm", 1724 .sbit = PB_TEMP_OT_FAULT, 1725 }, { 1726 .reg = PMBUS_VIRT_READ_TEMP_MIN, 1727 .attr = "lowest", 1728 }, { 1729 .reg = PMBUS_VIRT_READ_TEMP_AVG, 1730 .attr = "average", 1731 }, { 1732 .reg = PMBUS_VIRT_READ_TEMP_MAX, 1733 .attr = "highest", 1734 }, { 1735 .reg = PMBUS_VIRT_RESET_TEMP_HISTORY, 1736 .attr = "reset_history", 1737 }, { 1738 .reg = PMBUS_MFR_MAX_TEMP_1, 1739 .attr = "rated_max", 1740 }, 1741 }; 1742 1743 static const struct pmbus_limit_attr temp_limit_attrs2[] = { 1744 { 1745 .reg = PMBUS_UT_WARN_LIMIT, 1746 .low = true, 1747 .attr = "min", 1748 .alarm = "min_alarm", 1749 .sbit = PB_TEMP_UT_WARNING, 1750 }, { 1751 .reg = PMBUS_UT_FAULT_LIMIT, 1752 .low = true, 1753 .attr = "lcrit", 1754 .alarm = "lcrit_alarm", 1755 .sbit = PB_TEMP_UT_FAULT, 1756 }, { 1757 .reg = PMBUS_OT_WARN_LIMIT, 1758 .attr = "max", 1759 .alarm = "max_alarm", 1760 .sbit = PB_TEMP_OT_WARNING, 1761 }, { 1762 .reg = PMBUS_OT_FAULT_LIMIT, 1763 .attr = "crit", 1764 .alarm = "crit_alarm", 1765 .sbit = PB_TEMP_OT_FAULT, 1766 }, { 1767 .reg = PMBUS_VIRT_READ_TEMP2_MIN, 1768 .attr = "lowest", 1769 }, { 1770 .reg = PMBUS_VIRT_READ_TEMP2_AVG, 1771 .attr = "average", 1772 }, { 1773 .reg = PMBUS_VIRT_READ_TEMP2_MAX, 1774 .attr = "highest", 1775 }, { 1776 .reg = PMBUS_VIRT_RESET_TEMP2_HISTORY, 1777 .attr = "reset_history", 1778 }, { 1779 .reg = PMBUS_MFR_MAX_TEMP_2, 1780 .attr = "rated_max", 1781 }, 1782 }; 1783 1784 static const struct pmbus_limit_attr temp_limit_attrs3[] = { 1785 { 1786 .reg = PMBUS_UT_WARN_LIMIT, 1787 .low = true, 1788 .attr = "min", 1789 .alarm = "min_alarm", 1790 .sbit = PB_TEMP_UT_WARNING, 1791 }, { 1792 .reg = PMBUS_UT_FAULT_LIMIT, 1793 .low = true, 1794 .attr = "lcrit", 1795 .alarm = "lcrit_alarm", 1796 .sbit = PB_TEMP_UT_FAULT, 1797 }, { 1798 .reg = PMBUS_OT_WARN_LIMIT, 1799 .attr = "max", 1800 .alarm = "max_alarm", 1801 .sbit = PB_TEMP_OT_WARNING, 1802 }, { 1803 .reg = PMBUS_OT_FAULT_LIMIT, 1804 .attr = "crit", 1805 .alarm = "crit_alarm", 1806 .sbit = PB_TEMP_OT_FAULT, 1807 }, { 1808 .reg = PMBUS_MFR_MAX_TEMP_3, 1809 .attr = "rated_max", 1810 }, 1811 }; 1812 1813 static const struct pmbus_sensor_attr temp_attributes[] = { 1814 { 1815 .reg = PMBUS_READ_TEMPERATURE_1, 1816 .class = PSC_TEMPERATURE, 1817 .paged = true, 1818 .update = true, 1819 .compare = true, 1820 .func = PMBUS_HAVE_TEMP, 1821 .sfunc = PMBUS_HAVE_STATUS_TEMP, 1822 .sreg = PMBUS_STATUS_TEMPERATURE, 1823 .gbit = PB_STATUS_TEMPERATURE, 1824 .limit = temp_limit_attrs, 1825 .nlimit = ARRAY_SIZE(temp_limit_attrs), 1826 }, { 1827 .reg = PMBUS_READ_TEMPERATURE_2, 1828 .class = PSC_TEMPERATURE, 1829 .paged = true, 1830 .update = true, 1831 .compare = true, 1832 .func = PMBUS_HAVE_TEMP2, 1833 .sfunc = PMBUS_HAVE_STATUS_TEMP, 1834 .sreg = PMBUS_STATUS_TEMPERATURE, 1835 .gbit = PB_STATUS_TEMPERATURE, 1836 .limit = temp_limit_attrs2, 1837 .nlimit = ARRAY_SIZE(temp_limit_attrs2), 1838 }, { 1839 .reg = PMBUS_READ_TEMPERATURE_3, 1840 .class = PSC_TEMPERATURE, 1841 .paged = true, 1842 .update = true, 1843 .compare = true, 1844 .func = PMBUS_HAVE_TEMP3, 1845 .sfunc = PMBUS_HAVE_STATUS_TEMP, 1846 .sreg = PMBUS_STATUS_TEMPERATURE, 1847 .gbit = PB_STATUS_TEMPERATURE, 1848 .limit = temp_limit_attrs3, 1849 .nlimit = ARRAY_SIZE(temp_limit_attrs3), 1850 } 1851 }; 1852 1853 static const int pmbus_fan_registers[] = { 1854 PMBUS_READ_FAN_SPEED_1, 1855 PMBUS_READ_FAN_SPEED_2, 1856 PMBUS_READ_FAN_SPEED_3, 1857 PMBUS_READ_FAN_SPEED_4 1858 }; 1859 1860 static const int pmbus_fan_status_registers[] = { 1861 PMBUS_STATUS_FAN_12, 1862 PMBUS_STATUS_FAN_12, 1863 PMBUS_STATUS_FAN_34, 1864 PMBUS_STATUS_FAN_34 1865 }; 1866 1867 static const u32 pmbus_fan_flags[] = { 1868 PMBUS_HAVE_FAN12, 1869 PMBUS_HAVE_FAN12, 1870 PMBUS_HAVE_FAN34, 1871 PMBUS_HAVE_FAN34 1872 }; 1873 1874 static const u32 pmbus_fan_status_flags[] = { 1875 PMBUS_HAVE_STATUS_FAN12, 1876 PMBUS_HAVE_STATUS_FAN12, 1877 PMBUS_HAVE_STATUS_FAN34, 1878 PMBUS_HAVE_STATUS_FAN34 1879 }; 1880 1881 /* Fans */ 1882 1883 /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */ 1884 static int pmbus_add_fan_ctrl(struct i2c_client *client, 1885 struct pmbus_data *data, int index, int page, int id, 1886 u8 config) 1887 { 1888 struct pmbus_sensor *sensor; 1889 1890 sensor = pmbus_add_sensor(data, "fan", "target", index, page, 1891 0xff, PMBUS_VIRT_FAN_TARGET_1 + id, PSC_FAN, 1892 false, false, true); 1893 1894 if (!sensor) 1895 return -ENOMEM; 1896 1897 if (!((data->info->func[page] & PMBUS_HAVE_PWM12) || 1898 (data->info->func[page] & PMBUS_HAVE_PWM34))) 1899 return 0; 1900 1901 sensor = pmbus_add_sensor(data, "pwm", NULL, index, page, 1902 0xff, PMBUS_VIRT_PWM_1 + id, PSC_PWM, 1903 false, false, true); 1904 1905 if (!sensor) 1906 return -ENOMEM; 1907 1908 sensor = pmbus_add_sensor(data, "pwm", "enable", index, page, 1909 0xff, PMBUS_VIRT_PWM_ENABLE_1 + id, PSC_PWM, 1910 true, false, false); 1911 1912 if (!sensor) 1913 return -ENOMEM; 1914 1915 return 0; 1916 } 1917 1918 static int pmbus_add_fan_attributes(struct i2c_client *client, 1919 struct pmbus_data *data) 1920 { 1921 const struct pmbus_driver_info *info = data->info; 1922 int index = 1; 1923 int page; 1924 int ret; 1925 1926 for (page = 0; page < info->pages; page++) { 1927 int f; 1928 1929 for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) { 1930 int regval; 1931 1932 if (!(info->func[page] & pmbus_fan_flags[f])) 1933 break; 1934 1935 if (!pmbus_check_word_register(client, page, 1936 pmbus_fan_registers[f])) 1937 break; 1938 1939 /* 1940 * Skip fan if not installed. 1941 * Each fan configuration register covers multiple fans, 1942 * so we have to do some magic. 1943 */ 1944 regval = _pmbus_read_byte_data(client, page, 1945 pmbus_fan_config_registers[f]); 1946 if (regval < 0 || 1947 (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4))))) 1948 continue; 1949 1950 if (pmbus_add_sensor(data, "fan", "input", index, 1951 page, 0xff, pmbus_fan_registers[f], 1952 PSC_FAN, true, true, true) == NULL) 1953 return -ENOMEM; 1954 1955 /* Fan control */ 1956 if (pmbus_check_word_register(client, page, 1957 pmbus_fan_command_registers[f])) { 1958 ret = pmbus_add_fan_ctrl(client, data, index, 1959 page, f, regval); 1960 if (ret < 0) 1961 return ret; 1962 } 1963 1964 /* 1965 * Each fan status register covers multiple fans, 1966 * so we have to do some magic. 1967 */ 1968 if ((info->func[page] & pmbus_fan_status_flags[f]) && 1969 pmbus_check_byte_register(client, 1970 page, pmbus_fan_status_registers[f])) { 1971 int reg; 1972 1973 if (f > 1) /* fan 3, 4 */ 1974 reg = PMBUS_STATUS_FAN_34; 1975 else 1976 reg = PMBUS_STATUS_FAN_12; 1977 ret = pmbus_add_boolean(data, "fan", 1978 "alarm", index, NULL, NULL, page, reg, 1979 PB_FAN_FAN1_WARNING >> (f & 1)); 1980 if (ret) 1981 return ret; 1982 ret = pmbus_add_boolean(data, "fan", 1983 "fault", index, NULL, NULL, page, reg, 1984 PB_FAN_FAN1_FAULT >> (f & 1)); 1985 if (ret) 1986 return ret; 1987 } 1988 index++; 1989 } 1990 } 1991 return 0; 1992 } 1993 1994 struct pmbus_samples_attr { 1995 int reg; 1996 char *name; 1997 }; 1998 1999 struct pmbus_samples_reg { 2000 int page; 2001 struct pmbus_samples_attr *attr; 2002 struct device_attribute dev_attr; 2003 }; 2004 2005 static struct pmbus_samples_attr pmbus_samples_registers[] = { 2006 { 2007 .reg = PMBUS_VIRT_SAMPLES, 2008 .name = "samples", 2009 }, { 2010 .reg = PMBUS_VIRT_IN_SAMPLES, 2011 .name = "in_samples", 2012 }, { 2013 .reg = PMBUS_VIRT_CURR_SAMPLES, 2014 .name = "curr_samples", 2015 }, { 2016 .reg = PMBUS_VIRT_POWER_SAMPLES, 2017 .name = "power_samples", 2018 }, { 2019 .reg = PMBUS_VIRT_TEMP_SAMPLES, 2020 .name = "temp_samples", 2021 } 2022 }; 2023 2024 #define to_samples_reg(x) container_of(x, struct pmbus_samples_reg, dev_attr) 2025 2026 static ssize_t pmbus_show_samples(struct device *dev, 2027 struct device_attribute *devattr, char *buf) 2028 { 2029 int val; 2030 struct i2c_client *client = to_i2c_client(dev->parent); 2031 struct pmbus_samples_reg *reg = to_samples_reg(devattr); 2032 struct pmbus_data *data = i2c_get_clientdata(client); 2033 2034 mutex_lock(&data->update_lock); 2035 val = _pmbus_read_word_data(client, reg->page, 0xff, reg->attr->reg); 2036 mutex_unlock(&data->update_lock); 2037 if (val < 0) 2038 return val; 2039 2040 return sysfs_emit(buf, "%d\n", val); 2041 } 2042 2043 static ssize_t pmbus_set_samples(struct device *dev, 2044 struct device_attribute *devattr, 2045 const char *buf, size_t count) 2046 { 2047 int ret; 2048 long val; 2049 struct i2c_client *client = to_i2c_client(dev->parent); 2050 struct pmbus_samples_reg *reg = to_samples_reg(devattr); 2051 struct pmbus_data *data = i2c_get_clientdata(client); 2052 2053 if (kstrtol(buf, 0, &val) < 0) 2054 return -EINVAL; 2055 2056 mutex_lock(&data->update_lock); 2057 ret = _pmbus_write_word_data(client, reg->page, reg->attr->reg, val); 2058 mutex_unlock(&data->update_lock); 2059 2060 return ret ? : count; 2061 } 2062 2063 static int pmbus_add_samples_attr(struct pmbus_data *data, int page, 2064 struct pmbus_samples_attr *attr) 2065 { 2066 struct pmbus_samples_reg *reg; 2067 2068 reg = devm_kzalloc(data->dev, sizeof(*reg), GFP_KERNEL); 2069 if (!reg) 2070 return -ENOMEM; 2071 2072 reg->attr = attr; 2073 reg->page = page; 2074 2075 pmbus_dev_attr_init(®->dev_attr, attr->name, 0644, 2076 pmbus_show_samples, pmbus_set_samples); 2077 2078 return pmbus_add_attribute(data, ®->dev_attr.attr); 2079 } 2080 2081 static int pmbus_add_samples_attributes(struct i2c_client *client, 2082 struct pmbus_data *data) 2083 { 2084 const struct pmbus_driver_info *info = data->info; 2085 int s; 2086 2087 if (!(info->func[0] & PMBUS_HAVE_SAMPLES)) 2088 return 0; 2089 2090 for (s = 0; s < ARRAY_SIZE(pmbus_samples_registers); s++) { 2091 struct pmbus_samples_attr *attr; 2092 int ret; 2093 2094 attr = &pmbus_samples_registers[s]; 2095 if (!pmbus_check_word_register(client, 0, attr->reg)) 2096 continue; 2097 2098 ret = pmbus_add_samples_attr(data, 0, attr); 2099 if (ret) 2100 return ret; 2101 } 2102 2103 return 0; 2104 } 2105 2106 static int pmbus_find_attributes(struct i2c_client *client, 2107 struct pmbus_data *data) 2108 { 2109 int ret; 2110 2111 /* Voltage sensors */ 2112 ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes, 2113 ARRAY_SIZE(voltage_attributes)); 2114 if (ret) 2115 return ret; 2116 2117 /* Current sensors */ 2118 ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes, 2119 ARRAY_SIZE(current_attributes)); 2120 if (ret) 2121 return ret; 2122 2123 /* Power sensors */ 2124 ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes, 2125 ARRAY_SIZE(power_attributes)); 2126 if (ret) 2127 return ret; 2128 2129 /* Temperature sensors */ 2130 ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes, 2131 ARRAY_SIZE(temp_attributes)); 2132 if (ret) 2133 return ret; 2134 2135 /* Fans */ 2136 ret = pmbus_add_fan_attributes(client, data); 2137 if (ret) 2138 return ret; 2139 2140 ret = pmbus_add_samples_attributes(client, data); 2141 return ret; 2142 } 2143 2144 /* 2145 * The pmbus_class_attr_map structure maps one sensor class to 2146 * it's corresponding sensor attributes array. 2147 */ 2148 struct pmbus_class_attr_map { 2149 enum pmbus_sensor_classes class; 2150 int nattr; 2151 const struct pmbus_sensor_attr *attr; 2152 }; 2153 2154 static const struct pmbus_class_attr_map class_attr_map[] = { 2155 { 2156 .class = PSC_VOLTAGE_IN, 2157 .attr = voltage_attributes, 2158 .nattr = ARRAY_SIZE(voltage_attributes), 2159 }, { 2160 .class = PSC_VOLTAGE_OUT, 2161 .attr = voltage_attributes, 2162 .nattr = ARRAY_SIZE(voltage_attributes), 2163 }, { 2164 .class = PSC_CURRENT_IN, 2165 .attr = current_attributes, 2166 .nattr = ARRAY_SIZE(current_attributes), 2167 }, { 2168 .class = PSC_CURRENT_OUT, 2169 .attr = current_attributes, 2170 .nattr = ARRAY_SIZE(current_attributes), 2171 }, { 2172 .class = PSC_POWER, 2173 .attr = power_attributes, 2174 .nattr = ARRAY_SIZE(power_attributes), 2175 }, { 2176 .class = PSC_TEMPERATURE, 2177 .attr = temp_attributes, 2178 .nattr = ARRAY_SIZE(temp_attributes), 2179 } 2180 }; 2181 2182 /* 2183 * Read the coefficients for direct mode. 2184 */ 2185 static int pmbus_read_coefficients(struct i2c_client *client, 2186 struct pmbus_driver_info *info, 2187 const struct pmbus_sensor_attr *attr) 2188 { 2189 int rv; 2190 union i2c_smbus_data data; 2191 enum pmbus_sensor_classes class = attr->class; 2192 s8 R; 2193 s16 m, b; 2194 2195 data.block[0] = 2; 2196 data.block[1] = attr->reg; 2197 data.block[2] = 0x01; 2198 2199 rv = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2200 I2C_SMBUS_WRITE, PMBUS_COEFFICIENTS, 2201 I2C_SMBUS_BLOCK_PROC_CALL, &data); 2202 2203 if (rv < 0) 2204 return rv; 2205 2206 if (data.block[0] != 5) 2207 return -EIO; 2208 2209 m = data.block[1] | (data.block[2] << 8); 2210 b = data.block[3] | (data.block[4] << 8); 2211 R = data.block[5]; 2212 info->m[class] = m; 2213 info->b[class] = b; 2214 info->R[class] = R; 2215 2216 return rv; 2217 } 2218 2219 static int pmbus_init_coefficients(struct i2c_client *client, 2220 struct pmbus_driver_info *info) 2221 { 2222 int i, n, ret = -EINVAL; 2223 const struct pmbus_class_attr_map *map; 2224 const struct pmbus_sensor_attr *attr; 2225 2226 for (i = 0; i < ARRAY_SIZE(class_attr_map); i++) { 2227 map = &class_attr_map[i]; 2228 if (info->format[map->class] != direct) 2229 continue; 2230 for (n = 0; n < map->nattr; n++) { 2231 attr = &map->attr[n]; 2232 if (map->class != attr->class) 2233 continue; 2234 ret = pmbus_read_coefficients(client, info, attr); 2235 if (ret >= 0) 2236 break; 2237 } 2238 if (ret < 0) { 2239 dev_err(&client->dev, 2240 "No coefficients found for sensor class %d\n", 2241 map->class); 2242 return -EINVAL; 2243 } 2244 } 2245 2246 return 0; 2247 } 2248 2249 /* 2250 * Identify chip parameters. 2251 * This function is called for all chips. 2252 */ 2253 static int pmbus_identify_common(struct i2c_client *client, 2254 struct pmbus_data *data, int page) 2255 { 2256 int vout_mode = -1; 2257 2258 if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE)) 2259 vout_mode = _pmbus_read_byte_data(client, page, 2260 PMBUS_VOUT_MODE); 2261 if (vout_mode >= 0 && vout_mode != 0xff) { 2262 /* 2263 * Not all chips support the VOUT_MODE command, 2264 * so a failure to read it is not an error. 2265 */ 2266 switch (vout_mode >> 5) { 2267 case 0: /* linear mode */ 2268 if (data->info->format[PSC_VOLTAGE_OUT] != linear) 2269 return -ENODEV; 2270 2271 data->exponent[page] = ((s8)(vout_mode << 3)) >> 3; 2272 break; 2273 case 1: /* VID mode */ 2274 if (data->info->format[PSC_VOLTAGE_OUT] != vid) 2275 return -ENODEV; 2276 break; 2277 case 2: /* direct mode */ 2278 if (data->info->format[PSC_VOLTAGE_OUT] != direct) 2279 return -ENODEV; 2280 break; 2281 default: 2282 return -ENODEV; 2283 } 2284 } 2285 2286 pmbus_clear_fault_page(client, page); 2287 return 0; 2288 } 2289 2290 static int pmbus_read_status_byte(struct i2c_client *client, int page) 2291 { 2292 return _pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE); 2293 } 2294 2295 static int pmbus_read_status_word(struct i2c_client *client, int page) 2296 { 2297 return _pmbus_read_word_data(client, page, 0xff, PMBUS_STATUS_WORD); 2298 } 2299 2300 static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data, 2301 struct pmbus_driver_info *info) 2302 { 2303 struct device *dev = &client->dev; 2304 int page, ret; 2305 2306 /* 2307 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try 2308 * to use PMBUS_STATUS_BYTE instead if that is the case. 2309 * Bail out if both registers are not supported. 2310 */ 2311 data->read_status = pmbus_read_status_word; 2312 ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD); 2313 if (ret < 0 || ret == 0xffff) { 2314 data->read_status = pmbus_read_status_byte; 2315 ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE); 2316 if (ret < 0 || ret == 0xff) { 2317 dev_err(dev, "PMBus status register not found\n"); 2318 return -ENODEV; 2319 } 2320 } else { 2321 data->has_status_word = true; 2322 } 2323 2324 /* Enable PEC if the controller and bus supports it */ 2325 if (!(data->flags & PMBUS_NO_CAPABILITY)) { 2326 ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY); 2327 if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK)) { 2328 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC)) { 2329 client->flags |= I2C_CLIENT_PEC; 2330 } 2331 } 2332 } 2333 2334 /* 2335 * Check if the chip is write protected. If it is, we can not clear 2336 * faults, and we should not try it. Also, in that case, writes into 2337 * limit registers need to be disabled. 2338 */ 2339 if (!(data->flags & PMBUS_NO_WRITE_PROTECT)) { 2340 ret = i2c_smbus_read_byte_data(client, PMBUS_WRITE_PROTECT); 2341 if (ret > 0 && (ret & PB_WP_ANY)) 2342 data->flags |= PMBUS_WRITE_PROTECTED | PMBUS_SKIP_STATUS_CHECK; 2343 } 2344 2345 if (data->info->pages) 2346 pmbus_clear_faults(client); 2347 else 2348 pmbus_clear_fault_page(client, -1); 2349 2350 if (info->identify) { 2351 ret = (*info->identify)(client, info); 2352 if (ret < 0) { 2353 dev_err(dev, "Chip identification failed\n"); 2354 return ret; 2355 } 2356 } 2357 2358 if (info->pages <= 0 || info->pages > PMBUS_PAGES) { 2359 dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages); 2360 return -ENODEV; 2361 } 2362 2363 for (page = 0; page < info->pages; page++) { 2364 ret = pmbus_identify_common(client, data, page); 2365 if (ret < 0) { 2366 dev_err(dev, "Failed to identify chip capabilities\n"); 2367 return ret; 2368 } 2369 } 2370 2371 if (data->flags & PMBUS_USE_COEFFICIENTS_CMD) { 2372 if (!i2c_check_functionality(client->adapter, 2373 I2C_FUNC_SMBUS_BLOCK_PROC_CALL)) 2374 return -ENODEV; 2375 2376 ret = pmbus_init_coefficients(client, info); 2377 if (ret < 0) 2378 return ret; 2379 } 2380 2381 return 0; 2382 } 2383 2384 #if IS_ENABLED(CONFIG_REGULATOR) 2385 static int pmbus_regulator_is_enabled(struct regulator_dev *rdev) 2386 { 2387 struct device *dev = rdev_get_dev(rdev); 2388 struct i2c_client *client = to_i2c_client(dev->parent); 2389 u8 page = rdev_get_id(rdev); 2390 int ret; 2391 2392 ret = pmbus_read_byte_data(client, page, PMBUS_OPERATION); 2393 if (ret < 0) 2394 return ret; 2395 2396 return !!(ret & PB_OPERATION_CONTROL_ON); 2397 } 2398 2399 static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable) 2400 { 2401 struct device *dev = rdev_get_dev(rdev); 2402 struct i2c_client *client = to_i2c_client(dev->parent); 2403 u8 page = rdev_get_id(rdev); 2404 2405 return pmbus_update_byte_data(client, page, PMBUS_OPERATION, 2406 PB_OPERATION_CONTROL_ON, 2407 enable ? PB_OPERATION_CONTROL_ON : 0); 2408 } 2409 2410 static int pmbus_regulator_enable(struct regulator_dev *rdev) 2411 { 2412 return _pmbus_regulator_on_off(rdev, 1); 2413 } 2414 2415 static int pmbus_regulator_disable(struct regulator_dev *rdev) 2416 { 2417 return _pmbus_regulator_on_off(rdev, 0); 2418 } 2419 2420 const struct regulator_ops pmbus_regulator_ops = { 2421 .enable = pmbus_regulator_enable, 2422 .disable = pmbus_regulator_disable, 2423 .is_enabled = pmbus_regulator_is_enabled, 2424 }; 2425 EXPORT_SYMBOL_NS_GPL(pmbus_regulator_ops, PMBUS); 2426 2427 static int pmbus_regulator_register(struct pmbus_data *data) 2428 { 2429 struct device *dev = data->dev; 2430 const struct pmbus_driver_info *info = data->info; 2431 const struct pmbus_platform_data *pdata = dev_get_platdata(dev); 2432 struct regulator_dev *rdev; 2433 int i; 2434 2435 for (i = 0; i < info->num_regulators; i++) { 2436 struct regulator_config config = { }; 2437 2438 config.dev = dev; 2439 config.driver_data = data; 2440 2441 if (pdata && pdata->reg_init_data) 2442 config.init_data = &pdata->reg_init_data[i]; 2443 2444 rdev = devm_regulator_register(dev, &info->reg_desc[i], 2445 &config); 2446 if (IS_ERR(rdev)) { 2447 dev_err(dev, "Failed to register %s regulator\n", 2448 info->reg_desc[i].name); 2449 return PTR_ERR(rdev); 2450 } 2451 } 2452 2453 return 0; 2454 } 2455 #else 2456 static int pmbus_regulator_register(struct pmbus_data *data) 2457 { 2458 return 0; 2459 } 2460 #endif 2461 2462 static struct dentry *pmbus_debugfs_dir; /* pmbus debugfs directory */ 2463 2464 #if IS_ENABLED(CONFIG_DEBUG_FS) 2465 static int pmbus_debugfs_get(void *data, u64 *val) 2466 { 2467 int rc; 2468 struct pmbus_debugfs_entry *entry = data; 2469 2470 rc = _pmbus_read_byte_data(entry->client, entry->page, entry->reg); 2471 if (rc < 0) 2472 return rc; 2473 2474 *val = rc; 2475 2476 return 0; 2477 } 2478 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL, 2479 "0x%02llx\n"); 2480 2481 static int pmbus_debugfs_get_status(void *data, u64 *val) 2482 { 2483 int rc; 2484 struct pmbus_debugfs_entry *entry = data; 2485 struct pmbus_data *pdata = i2c_get_clientdata(entry->client); 2486 2487 rc = pdata->read_status(entry->client, entry->page); 2488 if (rc < 0) 2489 return rc; 2490 2491 *val = rc; 2492 2493 return 0; 2494 } 2495 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status, 2496 NULL, "0x%04llx\n"); 2497 2498 static int pmbus_debugfs_get_pec(void *data, u64 *val) 2499 { 2500 struct i2c_client *client = data; 2501 2502 *val = !!(client->flags & I2C_CLIENT_PEC); 2503 2504 return 0; 2505 } 2506 2507 static int pmbus_debugfs_set_pec(void *data, u64 val) 2508 { 2509 int rc; 2510 struct i2c_client *client = data; 2511 2512 if (!val) { 2513 client->flags &= ~I2C_CLIENT_PEC; 2514 return 0; 2515 } 2516 2517 if (val != 1) 2518 return -EINVAL; 2519 2520 rc = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY); 2521 if (rc < 0) 2522 return rc; 2523 2524 if (!(rc & PB_CAPABILITY_ERROR_CHECK)) 2525 return -EOPNOTSUPP; 2526 2527 client->flags |= I2C_CLIENT_PEC; 2528 2529 return 0; 2530 } 2531 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_pec, pmbus_debugfs_get_pec, 2532 pmbus_debugfs_set_pec, "%llu\n"); 2533 2534 static void pmbus_remove_debugfs(void *data) 2535 { 2536 struct dentry *entry = data; 2537 2538 debugfs_remove_recursive(entry); 2539 } 2540 2541 static int pmbus_init_debugfs(struct i2c_client *client, 2542 struct pmbus_data *data) 2543 { 2544 int i, idx = 0; 2545 char name[PMBUS_NAME_SIZE]; 2546 struct pmbus_debugfs_entry *entries; 2547 2548 if (!pmbus_debugfs_dir) 2549 return -ENODEV; 2550 2551 /* 2552 * Create the debugfs directory for this device. Use the hwmon device 2553 * name to avoid conflicts (hwmon numbers are globally unique). 2554 */ 2555 data->debugfs = debugfs_create_dir(dev_name(data->hwmon_dev), 2556 pmbus_debugfs_dir); 2557 if (IS_ERR_OR_NULL(data->debugfs)) { 2558 data->debugfs = NULL; 2559 return -ENODEV; 2560 } 2561 2562 /* Allocate the max possible entries we need. */ 2563 entries = devm_kcalloc(data->dev, 2564 data->info->pages * 10, sizeof(*entries), 2565 GFP_KERNEL); 2566 if (!entries) 2567 return -ENOMEM; 2568 2569 debugfs_create_file("pec", 0664, data->debugfs, client, 2570 &pmbus_debugfs_ops_pec); 2571 2572 for (i = 0; i < data->info->pages; ++i) { 2573 /* Check accessibility of status register if it's not page 0 */ 2574 if (!i || pmbus_check_status_register(client, i)) { 2575 /* No need to set reg as we have special read op. */ 2576 entries[idx].client = client; 2577 entries[idx].page = i; 2578 scnprintf(name, PMBUS_NAME_SIZE, "status%d", i); 2579 debugfs_create_file(name, 0444, data->debugfs, 2580 &entries[idx++], 2581 &pmbus_debugfs_ops_status); 2582 } 2583 2584 if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) { 2585 entries[idx].client = client; 2586 entries[idx].page = i; 2587 entries[idx].reg = PMBUS_STATUS_VOUT; 2588 scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i); 2589 debugfs_create_file(name, 0444, data->debugfs, 2590 &entries[idx++], 2591 &pmbus_debugfs_ops); 2592 } 2593 2594 if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) { 2595 entries[idx].client = client; 2596 entries[idx].page = i; 2597 entries[idx].reg = PMBUS_STATUS_IOUT; 2598 scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i); 2599 debugfs_create_file(name, 0444, data->debugfs, 2600 &entries[idx++], 2601 &pmbus_debugfs_ops); 2602 } 2603 2604 if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) { 2605 entries[idx].client = client; 2606 entries[idx].page = i; 2607 entries[idx].reg = PMBUS_STATUS_INPUT; 2608 scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i); 2609 debugfs_create_file(name, 0444, data->debugfs, 2610 &entries[idx++], 2611 &pmbus_debugfs_ops); 2612 } 2613 2614 if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) { 2615 entries[idx].client = client; 2616 entries[idx].page = i; 2617 entries[idx].reg = PMBUS_STATUS_TEMPERATURE; 2618 scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i); 2619 debugfs_create_file(name, 0444, data->debugfs, 2620 &entries[idx++], 2621 &pmbus_debugfs_ops); 2622 } 2623 2624 if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) { 2625 entries[idx].client = client; 2626 entries[idx].page = i; 2627 entries[idx].reg = PMBUS_STATUS_CML; 2628 scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i); 2629 debugfs_create_file(name, 0444, data->debugfs, 2630 &entries[idx++], 2631 &pmbus_debugfs_ops); 2632 } 2633 2634 if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) { 2635 entries[idx].client = client; 2636 entries[idx].page = i; 2637 entries[idx].reg = PMBUS_STATUS_OTHER; 2638 scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i); 2639 debugfs_create_file(name, 0444, data->debugfs, 2640 &entries[idx++], 2641 &pmbus_debugfs_ops); 2642 } 2643 2644 if (pmbus_check_byte_register(client, i, 2645 PMBUS_STATUS_MFR_SPECIFIC)) { 2646 entries[idx].client = client; 2647 entries[idx].page = i; 2648 entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC; 2649 scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i); 2650 debugfs_create_file(name, 0444, data->debugfs, 2651 &entries[idx++], 2652 &pmbus_debugfs_ops); 2653 } 2654 2655 if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) { 2656 entries[idx].client = client; 2657 entries[idx].page = i; 2658 entries[idx].reg = PMBUS_STATUS_FAN_12; 2659 scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i); 2660 debugfs_create_file(name, 0444, data->debugfs, 2661 &entries[idx++], 2662 &pmbus_debugfs_ops); 2663 } 2664 2665 if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) { 2666 entries[idx].client = client; 2667 entries[idx].page = i; 2668 entries[idx].reg = PMBUS_STATUS_FAN_34; 2669 scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i); 2670 debugfs_create_file(name, 0444, data->debugfs, 2671 &entries[idx++], 2672 &pmbus_debugfs_ops); 2673 } 2674 } 2675 2676 return devm_add_action_or_reset(data->dev, 2677 pmbus_remove_debugfs, data->debugfs); 2678 } 2679 #else 2680 static int pmbus_init_debugfs(struct i2c_client *client, 2681 struct pmbus_data *data) 2682 { 2683 return 0; 2684 } 2685 #endif /* IS_ENABLED(CONFIG_DEBUG_FS) */ 2686 2687 int pmbus_do_probe(struct i2c_client *client, struct pmbus_driver_info *info) 2688 { 2689 struct device *dev = &client->dev; 2690 const struct pmbus_platform_data *pdata = dev_get_platdata(dev); 2691 struct pmbus_data *data; 2692 size_t groups_num = 0; 2693 int ret; 2694 char *name; 2695 2696 if (!info) 2697 return -ENODEV; 2698 2699 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE 2700 | I2C_FUNC_SMBUS_BYTE_DATA 2701 | I2C_FUNC_SMBUS_WORD_DATA)) 2702 return -ENODEV; 2703 2704 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 2705 if (!data) 2706 return -ENOMEM; 2707 2708 if (info->groups) 2709 while (info->groups[groups_num]) 2710 groups_num++; 2711 2712 data->groups = devm_kcalloc(dev, groups_num + 2, sizeof(void *), 2713 GFP_KERNEL); 2714 if (!data->groups) 2715 return -ENOMEM; 2716 2717 i2c_set_clientdata(client, data); 2718 mutex_init(&data->update_lock); 2719 data->dev = dev; 2720 2721 if (pdata) 2722 data->flags = pdata->flags; 2723 data->info = info; 2724 data->currpage = -1; 2725 data->currphase = -1; 2726 2727 ret = pmbus_init_common(client, data, info); 2728 if (ret < 0) 2729 return ret; 2730 2731 ret = pmbus_find_attributes(client, data); 2732 if (ret) 2733 return ret; 2734 2735 /* 2736 * If there are no attributes, something is wrong. 2737 * Bail out instead of trying to register nothing. 2738 */ 2739 if (!data->num_attributes) { 2740 dev_err(dev, "No attributes found\n"); 2741 return -ENODEV; 2742 } 2743 2744 name = devm_kstrdup(dev, client->name, GFP_KERNEL); 2745 if (!name) 2746 return -ENOMEM; 2747 strreplace(name, '-', '_'); 2748 2749 data->groups[0] = &data->group; 2750 memcpy(data->groups + 1, info->groups, sizeof(void *) * groups_num); 2751 data->hwmon_dev = devm_hwmon_device_register_with_groups(dev, 2752 name, data, data->groups); 2753 if (IS_ERR(data->hwmon_dev)) { 2754 dev_err(dev, "Failed to register hwmon device\n"); 2755 return PTR_ERR(data->hwmon_dev); 2756 } 2757 2758 ret = pmbus_regulator_register(data); 2759 if (ret) 2760 return ret; 2761 2762 ret = pmbus_init_debugfs(client, data); 2763 if (ret) 2764 dev_warn(dev, "Failed to register debugfs\n"); 2765 2766 return 0; 2767 } 2768 EXPORT_SYMBOL_NS_GPL(pmbus_do_probe, PMBUS); 2769 2770 struct dentry *pmbus_get_debugfs_dir(struct i2c_client *client) 2771 { 2772 struct pmbus_data *data = i2c_get_clientdata(client); 2773 2774 return data->debugfs; 2775 } 2776 EXPORT_SYMBOL_NS_GPL(pmbus_get_debugfs_dir, PMBUS); 2777 2778 static int __init pmbus_core_init(void) 2779 { 2780 pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL); 2781 if (IS_ERR(pmbus_debugfs_dir)) 2782 pmbus_debugfs_dir = NULL; 2783 2784 return 0; 2785 } 2786 2787 static void __exit pmbus_core_exit(void) 2788 { 2789 debugfs_remove_recursive(pmbus_debugfs_dir); 2790 } 2791 2792 module_init(pmbus_core_init); 2793 module_exit(pmbus_core_exit); 2794 2795 MODULE_AUTHOR("Guenter Roeck"); 2796 MODULE_DESCRIPTION("PMBus core driver"); 2797 MODULE_LICENSE("GPL"); 2798