1 /* 2 * Hardware monitoring driver for PMBus devices 3 * 4 * Copyright (c) 2010, 2011 Ericsson AB. 5 * Copyright (c) 2012 Guenter Roeck 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 */ 21 22 #include <linux/debugfs.h> 23 #include <linux/kernel.h> 24 #include <linux/math64.h> 25 #include <linux/module.h> 26 #include <linux/init.h> 27 #include <linux/err.h> 28 #include <linux/slab.h> 29 #include <linux/i2c.h> 30 #include <linux/hwmon.h> 31 #include <linux/hwmon-sysfs.h> 32 #include <linux/jiffies.h> 33 #include <linux/pmbus.h> 34 #include <linux/regulator/driver.h> 35 #include <linux/regulator/machine.h> 36 #include "pmbus.h" 37 38 /* 39 * Number of additional attribute pointers to allocate 40 * with each call to krealloc 41 */ 42 #define PMBUS_ATTR_ALLOC_SIZE 32 43 44 /* 45 * Index into status register array, per status register group 46 */ 47 #define PB_STATUS_BASE 0 48 #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES) 49 #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES) 50 #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES) 51 #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES) 52 #define PB_STATUS_TEMP_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES) 53 #define PB_STATUS_INPUT_BASE (PB_STATUS_TEMP_BASE + PMBUS_PAGES) 54 #define PB_STATUS_VMON_BASE (PB_STATUS_INPUT_BASE + 1) 55 56 #define PB_NUM_STATUS_REG (PB_STATUS_VMON_BASE + 1) 57 58 #define PMBUS_NAME_SIZE 24 59 60 struct pmbus_sensor { 61 struct pmbus_sensor *next; 62 char name[PMBUS_NAME_SIZE]; /* sysfs sensor name */ 63 struct device_attribute attribute; 64 u8 page; /* page number */ 65 u16 reg; /* register */ 66 enum pmbus_sensor_classes class; /* sensor class */ 67 bool update; /* runtime sensor update needed */ 68 bool convert; /* Whether or not to apply linear/vid/direct */ 69 int data; /* Sensor data. 70 Negative if there was a read error */ 71 }; 72 #define to_pmbus_sensor(_attr) \ 73 container_of(_attr, struct pmbus_sensor, attribute) 74 75 struct pmbus_boolean { 76 char name[PMBUS_NAME_SIZE]; /* sysfs boolean name */ 77 struct sensor_device_attribute attribute; 78 struct pmbus_sensor *s1; 79 struct pmbus_sensor *s2; 80 }; 81 #define to_pmbus_boolean(_attr) \ 82 container_of(_attr, struct pmbus_boolean, attribute) 83 84 struct pmbus_label { 85 char name[PMBUS_NAME_SIZE]; /* sysfs label name */ 86 struct device_attribute attribute; 87 char label[PMBUS_NAME_SIZE]; /* label */ 88 }; 89 #define to_pmbus_label(_attr) \ 90 container_of(_attr, struct pmbus_label, attribute) 91 92 struct pmbus_data { 93 struct device *dev; 94 struct device *hwmon_dev; 95 96 u32 flags; /* from platform data */ 97 98 int exponent[PMBUS_PAGES]; 99 /* linear mode: exponent for output voltages */ 100 101 const struct pmbus_driver_info *info; 102 103 int max_attributes; 104 int num_attributes; 105 struct attribute_group group; 106 const struct attribute_group *groups[2]; 107 struct dentry *debugfs; /* debugfs device directory */ 108 109 struct pmbus_sensor *sensors; 110 111 struct mutex update_lock; 112 bool valid; 113 unsigned long last_updated; /* in jiffies */ 114 115 /* 116 * A single status register covers multiple attributes, 117 * so we keep them all together. 118 */ 119 u16 status[PB_NUM_STATUS_REG]; 120 121 bool has_status_word; /* device uses STATUS_WORD register */ 122 int (*read_status)(struct i2c_client *client, int page); 123 124 u8 currpage; 125 }; 126 127 struct pmbus_debugfs_entry { 128 struct i2c_client *client; 129 u8 page; 130 u8 reg; 131 }; 132 133 static const int pmbus_fan_rpm_mask[] = { 134 PB_FAN_1_RPM, 135 PB_FAN_2_RPM, 136 PB_FAN_1_RPM, 137 PB_FAN_2_RPM, 138 }; 139 140 static const int pmbus_fan_config_registers[] = { 141 PMBUS_FAN_CONFIG_12, 142 PMBUS_FAN_CONFIG_12, 143 PMBUS_FAN_CONFIG_34, 144 PMBUS_FAN_CONFIG_34 145 }; 146 147 static const int pmbus_fan_command_registers[] = { 148 PMBUS_FAN_COMMAND_1, 149 PMBUS_FAN_COMMAND_2, 150 PMBUS_FAN_COMMAND_3, 151 PMBUS_FAN_COMMAND_4, 152 }; 153 154 void pmbus_clear_cache(struct i2c_client *client) 155 { 156 struct pmbus_data *data = i2c_get_clientdata(client); 157 158 data->valid = false; 159 } 160 EXPORT_SYMBOL_GPL(pmbus_clear_cache); 161 162 int pmbus_set_page(struct i2c_client *client, int page) 163 { 164 struct pmbus_data *data = i2c_get_clientdata(client); 165 int rv; 166 167 if (page < 0 || page == data->currpage) 168 return 0; 169 170 if (!(data->info->func[page] & PMBUS_PAGE_VIRTUAL)) { 171 rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page); 172 if (rv < 0) 173 return rv; 174 175 rv = i2c_smbus_read_byte_data(client, PMBUS_PAGE); 176 if (rv < 0) 177 return rv; 178 179 if (rv != page) 180 return -EIO; 181 } 182 183 data->currpage = page; 184 185 return 0; 186 } 187 EXPORT_SYMBOL_GPL(pmbus_set_page); 188 189 int pmbus_write_byte(struct i2c_client *client, int page, u8 value) 190 { 191 int rv; 192 193 rv = pmbus_set_page(client, page); 194 if (rv < 0) 195 return rv; 196 197 return i2c_smbus_write_byte(client, value); 198 } 199 EXPORT_SYMBOL_GPL(pmbus_write_byte); 200 201 /* 202 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if 203 * a device specific mapping function exists and calls it if necessary. 204 */ 205 static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value) 206 { 207 struct pmbus_data *data = i2c_get_clientdata(client); 208 const struct pmbus_driver_info *info = data->info; 209 int status; 210 211 if (info->write_byte) { 212 status = info->write_byte(client, page, value); 213 if (status != -ENODATA) 214 return status; 215 } 216 return pmbus_write_byte(client, page, value); 217 } 218 219 int pmbus_write_word_data(struct i2c_client *client, int page, u8 reg, 220 u16 word) 221 { 222 int rv; 223 224 rv = pmbus_set_page(client, page); 225 if (rv < 0) 226 return rv; 227 228 return i2c_smbus_write_word_data(client, reg, word); 229 } 230 EXPORT_SYMBOL_GPL(pmbus_write_word_data); 231 232 233 static int pmbus_write_virt_reg(struct i2c_client *client, int page, int reg, 234 u16 word) 235 { 236 int bit; 237 int id; 238 int rv; 239 240 switch (reg) { 241 case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4: 242 id = reg - PMBUS_VIRT_FAN_TARGET_1; 243 bit = pmbus_fan_rpm_mask[id]; 244 rv = pmbus_update_fan(client, page, id, bit, bit, word); 245 break; 246 default: 247 rv = -ENXIO; 248 break; 249 } 250 251 return rv; 252 } 253 254 /* 255 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if 256 * a device specific mapping function exists and calls it if necessary. 257 */ 258 static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg, 259 u16 word) 260 { 261 struct pmbus_data *data = i2c_get_clientdata(client); 262 const struct pmbus_driver_info *info = data->info; 263 int status; 264 265 if (info->write_word_data) { 266 status = info->write_word_data(client, page, reg, word); 267 if (status != -ENODATA) 268 return status; 269 } 270 271 if (reg >= PMBUS_VIRT_BASE) 272 return pmbus_write_virt_reg(client, page, reg, word); 273 274 return pmbus_write_word_data(client, page, reg, word); 275 } 276 277 int pmbus_update_fan(struct i2c_client *client, int page, int id, 278 u8 config, u8 mask, u16 command) 279 { 280 int from; 281 int rv; 282 u8 to; 283 284 from = pmbus_read_byte_data(client, page, 285 pmbus_fan_config_registers[id]); 286 if (from < 0) 287 return from; 288 289 to = (from & ~mask) | (config & mask); 290 if (to != from) { 291 rv = pmbus_write_byte_data(client, page, 292 pmbus_fan_config_registers[id], to); 293 if (rv < 0) 294 return rv; 295 } 296 297 return _pmbus_write_word_data(client, page, 298 pmbus_fan_command_registers[id], command); 299 } 300 EXPORT_SYMBOL_GPL(pmbus_update_fan); 301 302 int pmbus_read_word_data(struct i2c_client *client, int page, u8 reg) 303 { 304 int rv; 305 306 rv = pmbus_set_page(client, page); 307 if (rv < 0) 308 return rv; 309 310 return i2c_smbus_read_word_data(client, reg); 311 } 312 EXPORT_SYMBOL_GPL(pmbus_read_word_data); 313 314 static int pmbus_read_virt_reg(struct i2c_client *client, int page, int reg) 315 { 316 int rv; 317 int id; 318 319 switch (reg) { 320 case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4: 321 id = reg - PMBUS_VIRT_FAN_TARGET_1; 322 rv = pmbus_get_fan_rate_device(client, page, id, rpm); 323 break; 324 default: 325 rv = -ENXIO; 326 break; 327 } 328 329 return rv; 330 } 331 332 /* 333 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if 334 * a device specific mapping function exists and calls it if necessary. 335 */ 336 static int _pmbus_read_word_data(struct i2c_client *client, int page, int reg) 337 { 338 struct pmbus_data *data = i2c_get_clientdata(client); 339 const struct pmbus_driver_info *info = data->info; 340 int status; 341 342 if (info->read_word_data) { 343 status = info->read_word_data(client, page, reg); 344 if (status != -ENODATA) 345 return status; 346 } 347 348 if (reg >= PMBUS_VIRT_BASE) 349 return pmbus_read_virt_reg(client, page, reg); 350 351 return pmbus_read_word_data(client, page, reg); 352 } 353 354 int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg) 355 { 356 int rv; 357 358 rv = pmbus_set_page(client, page); 359 if (rv < 0) 360 return rv; 361 362 return i2c_smbus_read_byte_data(client, reg); 363 } 364 EXPORT_SYMBOL_GPL(pmbus_read_byte_data); 365 366 int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg, u8 value) 367 { 368 int rv; 369 370 rv = pmbus_set_page(client, page); 371 if (rv < 0) 372 return rv; 373 374 return i2c_smbus_write_byte_data(client, reg, value); 375 } 376 EXPORT_SYMBOL_GPL(pmbus_write_byte_data); 377 378 int pmbus_update_byte_data(struct i2c_client *client, int page, u8 reg, 379 u8 mask, u8 value) 380 { 381 unsigned int tmp; 382 int rv; 383 384 rv = pmbus_read_byte_data(client, page, reg); 385 if (rv < 0) 386 return rv; 387 388 tmp = (rv & ~mask) | (value & mask); 389 390 if (tmp != rv) 391 rv = pmbus_write_byte_data(client, page, reg, tmp); 392 393 return rv; 394 } 395 EXPORT_SYMBOL_GPL(pmbus_update_byte_data); 396 397 /* 398 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if 399 * a device specific mapping function exists and calls it if necessary. 400 */ 401 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg) 402 { 403 struct pmbus_data *data = i2c_get_clientdata(client); 404 const struct pmbus_driver_info *info = data->info; 405 int status; 406 407 if (info->read_byte_data) { 408 status = info->read_byte_data(client, page, reg); 409 if (status != -ENODATA) 410 return status; 411 } 412 return pmbus_read_byte_data(client, page, reg); 413 } 414 415 static struct pmbus_sensor *pmbus_find_sensor(struct pmbus_data *data, int page, 416 int reg) 417 { 418 struct pmbus_sensor *sensor; 419 420 for (sensor = data->sensors; sensor; sensor = sensor->next) { 421 if (sensor->page == page && sensor->reg == reg) 422 return sensor; 423 } 424 425 return ERR_PTR(-EINVAL); 426 } 427 428 static int pmbus_get_fan_rate(struct i2c_client *client, int page, int id, 429 enum pmbus_fan_mode mode, 430 bool from_cache) 431 { 432 struct pmbus_data *data = i2c_get_clientdata(client); 433 bool want_rpm, have_rpm; 434 struct pmbus_sensor *s; 435 int config; 436 int reg; 437 438 want_rpm = (mode == rpm); 439 440 if (from_cache) { 441 reg = want_rpm ? PMBUS_VIRT_FAN_TARGET_1 : PMBUS_VIRT_PWM_1; 442 s = pmbus_find_sensor(data, page, reg + id); 443 if (IS_ERR(s)) 444 return PTR_ERR(s); 445 446 return s->data; 447 } 448 449 config = pmbus_read_byte_data(client, page, 450 pmbus_fan_config_registers[id]); 451 if (config < 0) 452 return config; 453 454 have_rpm = !!(config & pmbus_fan_rpm_mask[id]); 455 if (want_rpm == have_rpm) 456 return pmbus_read_word_data(client, page, 457 pmbus_fan_command_registers[id]); 458 459 /* Can't sensibly map between RPM and PWM, just return zero */ 460 return 0; 461 } 462 463 int pmbus_get_fan_rate_device(struct i2c_client *client, int page, int id, 464 enum pmbus_fan_mode mode) 465 { 466 return pmbus_get_fan_rate(client, page, id, mode, false); 467 } 468 EXPORT_SYMBOL_GPL(pmbus_get_fan_rate_device); 469 470 int pmbus_get_fan_rate_cached(struct i2c_client *client, int page, int id, 471 enum pmbus_fan_mode mode) 472 { 473 return pmbus_get_fan_rate(client, page, id, mode, true); 474 } 475 EXPORT_SYMBOL_GPL(pmbus_get_fan_rate_cached); 476 477 static void pmbus_clear_fault_page(struct i2c_client *client, int page) 478 { 479 _pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS); 480 } 481 482 void pmbus_clear_faults(struct i2c_client *client) 483 { 484 struct pmbus_data *data = i2c_get_clientdata(client); 485 int i; 486 487 for (i = 0; i < data->info->pages; i++) 488 pmbus_clear_fault_page(client, i); 489 } 490 EXPORT_SYMBOL_GPL(pmbus_clear_faults); 491 492 static int pmbus_check_status_cml(struct i2c_client *client) 493 { 494 struct pmbus_data *data = i2c_get_clientdata(client); 495 int status, status2; 496 497 status = data->read_status(client, -1); 498 if (status < 0 || (status & PB_STATUS_CML)) { 499 status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML); 500 if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND)) 501 return -EIO; 502 } 503 return 0; 504 } 505 506 static bool pmbus_check_register(struct i2c_client *client, 507 int (*func)(struct i2c_client *client, 508 int page, int reg), 509 int page, int reg) 510 { 511 int rv; 512 struct pmbus_data *data = i2c_get_clientdata(client); 513 514 rv = func(client, page, reg); 515 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK)) 516 rv = pmbus_check_status_cml(client); 517 pmbus_clear_fault_page(client, -1); 518 return rv >= 0; 519 } 520 521 static bool pmbus_check_status_register(struct i2c_client *client, int page) 522 { 523 int status; 524 struct pmbus_data *data = i2c_get_clientdata(client); 525 526 status = data->read_status(client, page); 527 if (status >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK) && 528 (status & PB_STATUS_CML)) { 529 status = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML); 530 if (status < 0 || (status & PB_CML_FAULT_INVALID_COMMAND)) 531 status = -EIO; 532 } 533 534 pmbus_clear_fault_page(client, -1); 535 return status >= 0; 536 } 537 538 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg) 539 { 540 return pmbus_check_register(client, _pmbus_read_byte_data, page, reg); 541 } 542 EXPORT_SYMBOL_GPL(pmbus_check_byte_register); 543 544 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg) 545 { 546 return pmbus_check_register(client, _pmbus_read_word_data, page, reg); 547 } 548 EXPORT_SYMBOL_GPL(pmbus_check_word_register); 549 550 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client) 551 { 552 struct pmbus_data *data = i2c_get_clientdata(client); 553 554 return data->info; 555 } 556 EXPORT_SYMBOL_GPL(pmbus_get_driver_info); 557 558 static struct _pmbus_status { 559 u32 func; 560 u16 base; 561 u16 reg; 562 } pmbus_status[] = { 563 { PMBUS_HAVE_STATUS_VOUT, PB_STATUS_VOUT_BASE, PMBUS_STATUS_VOUT }, 564 { PMBUS_HAVE_STATUS_IOUT, PB_STATUS_IOUT_BASE, PMBUS_STATUS_IOUT }, 565 { PMBUS_HAVE_STATUS_TEMP, PB_STATUS_TEMP_BASE, 566 PMBUS_STATUS_TEMPERATURE }, 567 { PMBUS_HAVE_STATUS_FAN12, PB_STATUS_FAN_BASE, PMBUS_STATUS_FAN_12 }, 568 { PMBUS_HAVE_STATUS_FAN34, PB_STATUS_FAN34_BASE, PMBUS_STATUS_FAN_34 }, 569 }; 570 571 static struct pmbus_data *pmbus_update_device(struct device *dev) 572 { 573 struct i2c_client *client = to_i2c_client(dev->parent); 574 struct pmbus_data *data = i2c_get_clientdata(client); 575 const struct pmbus_driver_info *info = data->info; 576 struct pmbus_sensor *sensor; 577 578 mutex_lock(&data->update_lock); 579 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 580 int i, j; 581 582 for (i = 0; i < info->pages; i++) { 583 data->status[PB_STATUS_BASE + i] 584 = data->read_status(client, i); 585 for (j = 0; j < ARRAY_SIZE(pmbus_status); j++) { 586 struct _pmbus_status *s = &pmbus_status[j]; 587 588 if (!(info->func[i] & s->func)) 589 continue; 590 data->status[s->base + i] 591 = _pmbus_read_byte_data(client, i, 592 s->reg); 593 } 594 } 595 596 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) 597 data->status[PB_STATUS_INPUT_BASE] 598 = _pmbus_read_byte_data(client, 0, 599 PMBUS_STATUS_INPUT); 600 601 if (info->func[0] & PMBUS_HAVE_STATUS_VMON) 602 data->status[PB_STATUS_VMON_BASE] 603 = _pmbus_read_byte_data(client, 0, 604 PMBUS_VIRT_STATUS_VMON); 605 606 for (sensor = data->sensors; sensor; sensor = sensor->next) { 607 if (!data->valid || sensor->update) 608 sensor->data 609 = _pmbus_read_word_data(client, 610 sensor->page, 611 sensor->reg); 612 } 613 pmbus_clear_faults(client); 614 data->last_updated = jiffies; 615 data->valid = 1; 616 } 617 mutex_unlock(&data->update_lock); 618 return data; 619 } 620 621 /* 622 * Convert linear sensor values to milli- or micro-units 623 * depending on sensor type. 624 */ 625 static long pmbus_reg2data_linear(struct pmbus_data *data, 626 struct pmbus_sensor *sensor) 627 { 628 s16 exponent; 629 s32 mantissa; 630 long val; 631 632 if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */ 633 exponent = data->exponent[sensor->page]; 634 mantissa = (u16) sensor->data; 635 } else { /* LINEAR11 */ 636 exponent = ((s16)sensor->data) >> 11; 637 mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5; 638 } 639 640 val = mantissa; 641 642 /* scale result to milli-units for all sensors except fans */ 643 if (sensor->class != PSC_FAN) 644 val = val * 1000L; 645 646 /* scale result to micro-units for power sensors */ 647 if (sensor->class == PSC_POWER) 648 val = val * 1000L; 649 650 if (exponent >= 0) 651 val <<= exponent; 652 else 653 val >>= -exponent; 654 655 return val; 656 } 657 658 /* 659 * Convert direct sensor values to milli- or micro-units 660 * depending on sensor type. 661 */ 662 static long pmbus_reg2data_direct(struct pmbus_data *data, 663 struct pmbus_sensor *sensor) 664 { 665 s64 b, val = (s16)sensor->data; 666 s32 m, R; 667 668 m = data->info->m[sensor->class]; 669 b = data->info->b[sensor->class]; 670 R = data->info->R[sensor->class]; 671 672 if (m == 0) 673 return 0; 674 675 /* X = 1/m * (Y * 10^-R - b) */ 676 R = -R; 677 /* scale result to milli-units for everything but fans */ 678 if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) { 679 R += 3; 680 b *= 1000; 681 } 682 683 /* scale result to micro-units for power sensors */ 684 if (sensor->class == PSC_POWER) { 685 R += 3; 686 b *= 1000; 687 } 688 689 while (R > 0) { 690 val *= 10; 691 R--; 692 } 693 while (R < 0) { 694 val = div_s64(val + 5LL, 10L); /* round closest */ 695 R++; 696 } 697 698 val = div_s64(val - b, m); 699 return clamp_val(val, LONG_MIN, LONG_MAX); 700 } 701 702 /* 703 * Convert VID sensor values to milli- or micro-units 704 * depending on sensor type. 705 */ 706 static long pmbus_reg2data_vid(struct pmbus_data *data, 707 struct pmbus_sensor *sensor) 708 { 709 long val = sensor->data; 710 long rv = 0; 711 712 switch (data->info->vrm_version) { 713 case vr11: 714 if (val >= 0x02 && val <= 0xb2) 715 rv = DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100); 716 break; 717 case vr12: 718 if (val >= 0x01) 719 rv = 250 + (val - 1) * 5; 720 break; 721 case vr13: 722 if (val >= 0x01) 723 rv = 500 + (val - 1) * 10; 724 break; 725 } 726 return rv; 727 } 728 729 static long pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor) 730 { 731 long val; 732 733 if (!sensor->convert) 734 return sensor->data; 735 736 switch (data->info->format[sensor->class]) { 737 case direct: 738 val = pmbus_reg2data_direct(data, sensor); 739 break; 740 case vid: 741 val = pmbus_reg2data_vid(data, sensor); 742 break; 743 case linear: 744 default: 745 val = pmbus_reg2data_linear(data, sensor); 746 break; 747 } 748 return val; 749 } 750 751 #define MAX_MANTISSA (1023 * 1000) 752 #define MIN_MANTISSA (511 * 1000) 753 754 static u16 pmbus_data2reg_linear(struct pmbus_data *data, 755 struct pmbus_sensor *sensor, long val) 756 { 757 s16 exponent = 0, mantissa; 758 bool negative = false; 759 760 /* simple case */ 761 if (val == 0) 762 return 0; 763 764 if (sensor->class == PSC_VOLTAGE_OUT) { 765 /* LINEAR16 does not support negative voltages */ 766 if (val < 0) 767 return 0; 768 769 /* 770 * For a static exponents, we don't have a choice 771 * but to adjust the value to it. 772 */ 773 if (data->exponent[sensor->page] < 0) 774 val <<= -data->exponent[sensor->page]; 775 else 776 val >>= data->exponent[sensor->page]; 777 val = DIV_ROUND_CLOSEST(val, 1000); 778 return val & 0xffff; 779 } 780 781 if (val < 0) { 782 negative = true; 783 val = -val; 784 } 785 786 /* Power is in uW. Convert to mW before converting. */ 787 if (sensor->class == PSC_POWER) 788 val = DIV_ROUND_CLOSEST(val, 1000L); 789 790 /* 791 * For simplicity, convert fan data to milli-units 792 * before calculating the exponent. 793 */ 794 if (sensor->class == PSC_FAN) 795 val = val * 1000; 796 797 /* Reduce large mantissa until it fits into 10 bit */ 798 while (val >= MAX_MANTISSA && exponent < 15) { 799 exponent++; 800 val >>= 1; 801 } 802 /* Increase small mantissa to improve precision */ 803 while (val < MIN_MANTISSA && exponent > -15) { 804 exponent--; 805 val <<= 1; 806 } 807 808 /* Convert mantissa from milli-units to units */ 809 mantissa = DIV_ROUND_CLOSEST(val, 1000); 810 811 /* Ensure that resulting number is within range */ 812 if (mantissa > 0x3ff) 813 mantissa = 0x3ff; 814 815 /* restore sign */ 816 if (negative) 817 mantissa = -mantissa; 818 819 /* Convert to 5 bit exponent, 11 bit mantissa */ 820 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800); 821 } 822 823 static u16 pmbus_data2reg_direct(struct pmbus_data *data, 824 struct pmbus_sensor *sensor, long val) 825 { 826 s64 b, val64 = val; 827 s32 m, R; 828 829 m = data->info->m[sensor->class]; 830 b = data->info->b[sensor->class]; 831 R = data->info->R[sensor->class]; 832 833 /* Power is in uW. Adjust R and b. */ 834 if (sensor->class == PSC_POWER) { 835 R -= 3; 836 b *= 1000; 837 } 838 839 /* Calculate Y = (m * X + b) * 10^R */ 840 if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) { 841 R -= 3; /* Adjust R and b for data in milli-units */ 842 b *= 1000; 843 } 844 val64 = val64 * m + b; 845 846 while (R > 0) { 847 val64 *= 10; 848 R--; 849 } 850 while (R < 0) { 851 val64 = div_s64(val64 + 5LL, 10L); /* round closest */ 852 R++; 853 } 854 855 return (u16)clamp_val(val64, S16_MIN, S16_MAX); 856 } 857 858 static u16 pmbus_data2reg_vid(struct pmbus_data *data, 859 struct pmbus_sensor *sensor, long val) 860 { 861 val = clamp_val(val, 500, 1600); 862 863 return 2 + DIV_ROUND_CLOSEST((1600 - val) * 100, 625); 864 } 865 866 static u16 pmbus_data2reg(struct pmbus_data *data, 867 struct pmbus_sensor *sensor, long val) 868 { 869 u16 regval; 870 871 if (!sensor->convert) 872 return val; 873 874 switch (data->info->format[sensor->class]) { 875 case direct: 876 regval = pmbus_data2reg_direct(data, sensor, val); 877 break; 878 case vid: 879 regval = pmbus_data2reg_vid(data, sensor, val); 880 break; 881 case linear: 882 default: 883 regval = pmbus_data2reg_linear(data, sensor, val); 884 break; 885 } 886 return regval; 887 } 888 889 /* 890 * Return boolean calculated from converted data. 891 * <index> defines a status register index and mask. 892 * The mask is in the lower 8 bits, the register index is in bits 8..23. 893 * 894 * The associated pmbus_boolean structure contains optional pointers to two 895 * sensor attributes. If specified, those attributes are compared against each 896 * other to determine if a limit has been exceeded. 897 * 898 * If the sensor attribute pointers are NULL, the function returns true if 899 * (status[reg] & mask) is true. 900 * 901 * If sensor attribute pointers are provided, a comparison against a specified 902 * limit has to be performed to determine the boolean result. 903 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are 904 * sensor values referenced by sensor attribute pointers s1 and s2). 905 * 906 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>. 907 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>. 908 * 909 * If a negative value is stored in any of the referenced registers, this value 910 * reflects an error code which will be returned. 911 */ 912 static int pmbus_get_boolean(struct pmbus_data *data, struct pmbus_boolean *b, 913 int index) 914 { 915 struct pmbus_sensor *s1 = b->s1; 916 struct pmbus_sensor *s2 = b->s2; 917 u16 reg = (index >> 16) & 0xffff; 918 u16 mask = index & 0xffff; 919 int ret, status; 920 u16 regval; 921 922 status = data->status[reg]; 923 if (status < 0) 924 return status; 925 926 regval = status & mask; 927 if (!s1 && !s2) { 928 ret = !!regval; 929 } else if (!s1 || !s2) { 930 WARN(1, "Bad boolean descriptor %p: s1=%p, s2=%p\n", b, s1, s2); 931 return 0; 932 } else { 933 long v1, v2; 934 935 if (s1->data < 0) 936 return s1->data; 937 if (s2->data < 0) 938 return s2->data; 939 940 v1 = pmbus_reg2data(data, s1); 941 v2 = pmbus_reg2data(data, s2); 942 ret = !!(regval && v1 >= v2); 943 } 944 return ret; 945 } 946 947 static ssize_t pmbus_show_boolean(struct device *dev, 948 struct device_attribute *da, char *buf) 949 { 950 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 951 struct pmbus_boolean *boolean = to_pmbus_boolean(attr); 952 struct pmbus_data *data = pmbus_update_device(dev); 953 int val; 954 955 val = pmbus_get_boolean(data, boolean, attr->index); 956 if (val < 0) 957 return val; 958 return snprintf(buf, PAGE_SIZE, "%d\n", val); 959 } 960 961 static ssize_t pmbus_show_sensor(struct device *dev, 962 struct device_attribute *devattr, char *buf) 963 { 964 struct pmbus_data *data = pmbus_update_device(dev); 965 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr); 966 967 if (sensor->data < 0) 968 return sensor->data; 969 970 return snprintf(buf, PAGE_SIZE, "%ld\n", pmbus_reg2data(data, sensor)); 971 } 972 973 static ssize_t pmbus_set_sensor(struct device *dev, 974 struct device_attribute *devattr, 975 const char *buf, size_t count) 976 { 977 struct i2c_client *client = to_i2c_client(dev->parent); 978 struct pmbus_data *data = i2c_get_clientdata(client); 979 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr); 980 ssize_t rv = count; 981 long val = 0; 982 int ret; 983 u16 regval; 984 985 if (kstrtol(buf, 10, &val) < 0) 986 return -EINVAL; 987 988 mutex_lock(&data->update_lock); 989 regval = pmbus_data2reg(data, sensor, val); 990 ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval); 991 if (ret < 0) 992 rv = ret; 993 else 994 sensor->data = regval; 995 mutex_unlock(&data->update_lock); 996 return rv; 997 } 998 999 static ssize_t pmbus_show_label(struct device *dev, 1000 struct device_attribute *da, char *buf) 1001 { 1002 struct pmbus_label *label = to_pmbus_label(da); 1003 1004 return snprintf(buf, PAGE_SIZE, "%s\n", label->label); 1005 } 1006 1007 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr) 1008 { 1009 if (data->num_attributes >= data->max_attributes - 1) { 1010 int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE; 1011 void *new_attrs = krealloc(data->group.attrs, 1012 new_max_attrs * sizeof(void *), 1013 GFP_KERNEL); 1014 if (!new_attrs) 1015 return -ENOMEM; 1016 data->group.attrs = new_attrs; 1017 data->max_attributes = new_max_attrs; 1018 } 1019 1020 data->group.attrs[data->num_attributes++] = attr; 1021 data->group.attrs[data->num_attributes] = NULL; 1022 return 0; 1023 } 1024 1025 static void pmbus_dev_attr_init(struct device_attribute *dev_attr, 1026 const char *name, 1027 umode_t mode, 1028 ssize_t (*show)(struct device *dev, 1029 struct device_attribute *attr, 1030 char *buf), 1031 ssize_t (*store)(struct device *dev, 1032 struct device_attribute *attr, 1033 const char *buf, size_t count)) 1034 { 1035 sysfs_attr_init(&dev_attr->attr); 1036 dev_attr->attr.name = name; 1037 dev_attr->attr.mode = mode; 1038 dev_attr->show = show; 1039 dev_attr->store = store; 1040 } 1041 1042 static void pmbus_attr_init(struct sensor_device_attribute *a, 1043 const char *name, 1044 umode_t mode, 1045 ssize_t (*show)(struct device *dev, 1046 struct device_attribute *attr, 1047 char *buf), 1048 ssize_t (*store)(struct device *dev, 1049 struct device_attribute *attr, 1050 const char *buf, size_t count), 1051 int idx) 1052 { 1053 pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store); 1054 a->index = idx; 1055 } 1056 1057 static int pmbus_add_boolean(struct pmbus_data *data, 1058 const char *name, const char *type, int seq, 1059 struct pmbus_sensor *s1, 1060 struct pmbus_sensor *s2, 1061 u16 reg, u16 mask) 1062 { 1063 struct pmbus_boolean *boolean; 1064 struct sensor_device_attribute *a; 1065 1066 boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL); 1067 if (!boolean) 1068 return -ENOMEM; 1069 1070 a = &boolean->attribute; 1071 1072 snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s", 1073 name, seq, type); 1074 boolean->s1 = s1; 1075 boolean->s2 = s2; 1076 pmbus_attr_init(a, boolean->name, S_IRUGO, pmbus_show_boolean, NULL, 1077 (reg << 16) | mask); 1078 1079 return pmbus_add_attribute(data, &a->dev_attr.attr); 1080 } 1081 1082 static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data, 1083 const char *name, const char *type, 1084 int seq, int page, 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 sensor->page = page; 1105 sensor->reg = reg; 1106 sensor->class = class; 1107 sensor->update = update; 1108 sensor->convert = convert; 1109 pmbus_dev_attr_init(a, sensor->name, 1110 readonly ? S_IRUGO : S_IRUGO | S_IWUSR, 1111 pmbus_show_sensor, pmbus_set_sensor); 1112 1113 if (pmbus_add_attribute(data, &a->attr)) 1114 return NULL; 1115 1116 sensor->next = data->sensors; 1117 data->sensors = sensor; 1118 1119 return sensor; 1120 } 1121 1122 static int pmbus_add_label(struct pmbus_data *data, 1123 const char *name, int seq, 1124 const char *lstring, int index) 1125 { 1126 struct pmbus_label *label; 1127 struct device_attribute *a; 1128 1129 label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL); 1130 if (!label) 1131 return -ENOMEM; 1132 1133 a = &label->attribute; 1134 1135 snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq); 1136 if (!index) 1137 strncpy(label->label, lstring, sizeof(label->label) - 1); 1138 else 1139 snprintf(label->label, sizeof(label->label), "%s%d", lstring, 1140 index); 1141 1142 pmbus_dev_attr_init(a, label->name, S_IRUGO, pmbus_show_label, NULL); 1143 return pmbus_add_attribute(data, &a->attr); 1144 } 1145 1146 /* 1147 * Search for attributes. Allocate sensors, booleans, and labels as needed. 1148 */ 1149 1150 /* 1151 * The pmbus_limit_attr structure describes a single limit attribute 1152 * and its associated alarm attribute. 1153 */ 1154 struct pmbus_limit_attr { 1155 u16 reg; /* Limit register */ 1156 u16 sbit; /* Alarm attribute status bit */ 1157 bool update; /* True if register needs updates */ 1158 bool low; /* True if low limit; for limits with compare 1159 functions only */ 1160 const char *attr; /* Attribute name */ 1161 const char *alarm; /* Alarm attribute name */ 1162 }; 1163 1164 /* 1165 * The pmbus_sensor_attr structure describes one sensor attribute. This 1166 * description includes a reference to the associated limit attributes. 1167 */ 1168 struct pmbus_sensor_attr { 1169 u16 reg; /* sensor register */ 1170 u16 gbit; /* generic status bit */ 1171 u8 nlimit; /* # of limit registers */ 1172 enum pmbus_sensor_classes class;/* sensor class */ 1173 const char *label; /* sensor label */ 1174 bool paged; /* true if paged sensor */ 1175 bool update; /* true if update needed */ 1176 bool compare; /* true if compare function needed */ 1177 u32 func; /* sensor mask */ 1178 u32 sfunc; /* sensor status mask */ 1179 int sbase; /* status base register */ 1180 const struct pmbus_limit_attr *limit;/* limit registers */ 1181 }; 1182 1183 /* 1184 * Add a set of limit attributes and, if supported, the associated 1185 * alarm attributes. 1186 * returns 0 if no alarm register found, 1 if an alarm register was found, 1187 * < 0 on errors. 1188 */ 1189 static int pmbus_add_limit_attrs(struct i2c_client *client, 1190 struct pmbus_data *data, 1191 const struct pmbus_driver_info *info, 1192 const char *name, int index, int page, 1193 struct pmbus_sensor *base, 1194 const struct pmbus_sensor_attr *attr) 1195 { 1196 const struct pmbus_limit_attr *l = attr->limit; 1197 int nlimit = attr->nlimit; 1198 int have_alarm = 0; 1199 int i, ret; 1200 struct pmbus_sensor *curr; 1201 1202 for (i = 0; i < nlimit; i++) { 1203 if (pmbus_check_word_register(client, page, l->reg)) { 1204 curr = pmbus_add_sensor(data, name, l->attr, index, 1205 page, l->reg, attr->class, 1206 attr->update || l->update, 1207 false, true); 1208 if (!curr) 1209 return -ENOMEM; 1210 if (l->sbit && (info->func[page] & attr->sfunc)) { 1211 ret = pmbus_add_boolean(data, name, 1212 l->alarm, index, 1213 attr->compare ? l->low ? curr : base 1214 : NULL, 1215 attr->compare ? l->low ? base : curr 1216 : NULL, 1217 attr->sbase + page, l->sbit); 1218 if (ret) 1219 return ret; 1220 have_alarm = 1; 1221 } 1222 } 1223 l++; 1224 } 1225 return have_alarm; 1226 } 1227 1228 static int pmbus_add_sensor_attrs_one(struct i2c_client *client, 1229 struct pmbus_data *data, 1230 const struct pmbus_driver_info *info, 1231 const char *name, 1232 int index, int page, 1233 const struct pmbus_sensor_attr *attr) 1234 { 1235 struct pmbus_sensor *base; 1236 bool upper = !!(attr->gbit & 0xff00); /* need to check STATUS_WORD */ 1237 int ret; 1238 1239 if (attr->label) { 1240 ret = pmbus_add_label(data, name, index, attr->label, 1241 attr->paged ? page + 1 : 0); 1242 if (ret) 1243 return ret; 1244 } 1245 base = pmbus_add_sensor(data, name, "input", index, page, attr->reg, 1246 attr->class, true, true, true); 1247 if (!base) 1248 return -ENOMEM; 1249 if (attr->sfunc) { 1250 ret = pmbus_add_limit_attrs(client, data, info, name, 1251 index, page, base, attr); 1252 if (ret < 0) 1253 return ret; 1254 /* 1255 * Add generic alarm attribute only if there are no individual 1256 * alarm attributes, if there is a global alarm bit, and if 1257 * the generic status register (word or byte, depending on 1258 * which global bit is set) for this page is accessible. 1259 */ 1260 if (!ret && attr->gbit && 1261 (!upper || (upper && data->has_status_word)) && 1262 pmbus_check_status_register(client, page)) { 1263 ret = pmbus_add_boolean(data, name, "alarm", index, 1264 NULL, NULL, 1265 PB_STATUS_BASE + page, 1266 attr->gbit); 1267 if (ret) 1268 return ret; 1269 } 1270 } 1271 return 0; 1272 } 1273 1274 static int pmbus_add_sensor_attrs(struct i2c_client *client, 1275 struct pmbus_data *data, 1276 const char *name, 1277 const struct pmbus_sensor_attr *attrs, 1278 int nattrs) 1279 { 1280 const struct pmbus_driver_info *info = data->info; 1281 int index, i; 1282 int ret; 1283 1284 index = 1; 1285 for (i = 0; i < nattrs; i++) { 1286 int page, pages; 1287 1288 pages = attrs->paged ? info->pages : 1; 1289 for (page = 0; page < pages; page++) { 1290 if (!(info->func[page] & attrs->func)) 1291 continue; 1292 ret = pmbus_add_sensor_attrs_one(client, data, info, 1293 name, index, page, 1294 attrs); 1295 if (ret) 1296 return ret; 1297 index++; 1298 } 1299 attrs++; 1300 } 1301 return 0; 1302 } 1303 1304 static const struct pmbus_limit_attr vin_limit_attrs[] = { 1305 { 1306 .reg = PMBUS_VIN_UV_WARN_LIMIT, 1307 .attr = "min", 1308 .alarm = "min_alarm", 1309 .sbit = PB_VOLTAGE_UV_WARNING, 1310 }, { 1311 .reg = PMBUS_VIN_UV_FAULT_LIMIT, 1312 .attr = "lcrit", 1313 .alarm = "lcrit_alarm", 1314 .sbit = PB_VOLTAGE_UV_FAULT, 1315 }, { 1316 .reg = PMBUS_VIN_OV_WARN_LIMIT, 1317 .attr = "max", 1318 .alarm = "max_alarm", 1319 .sbit = PB_VOLTAGE_OV_WARNING, 1320 }, { 1321 .reg = PMBUS_VIN_OV_FAULT_LIMIT, 1322 .attr = "crit", 1323 .alarm = "crit_alarm", 1324 .sbit = PB_VOLTAGE_OV_FAULT, 1325 }, { 1326 .reg = PMBUS_VIRT_READ_VIN_AVG, 1327 .update = true, 1328 .attr = "average", 1329 }, { 1330 .reg = PMBUS_VIRT_READ_VIN_MIN, 1331 .update = true, 1332 .attr = "lowest", 1333 }, { 1334 .reg = PMBUS_VIRT_READ_VIN_MAX, 1335 .update = true, 1336 .attr = "highest", 1337 }, { 1338 .reg = PMBUS_VIRT_RESET_VIN_HISTORY, 1339 .attr = "reset_history", 1340 }, 1341 }; 1342 1343 static const struct pmbus_limit_attr vmon_limit_attrs[] = { 1344 { 1345 .reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT, 1346 .attr = "min", 1347 .alarm = "min_alarm", 1348 .sbit = PB_VOLTAGE_UV_WARNING, 1349 }, { 1350 .reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT, 1351 .attr = "lcrit", 1352 .alarm = "lcrit_alarm", 1353 .sbit = PB_VOLTAGE_UV_FAULT, 1354 }, { 1355 .reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT, 1356 .attr = "max", 1357 .alarm = "max_alarm", 1358 .sbit = PB_VOLTAGE_OV_WARNING, 1359 }, { 1360 .reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT, 1361 .attr = "crit", 1362 .alarm = "crit_alarm", 1363 .sbit = PB_VOLTAGE_OV_FAULT, 1364 } 1365 }; 1366 1367 static const struct pmbus_limit_attr vout_limit_attrs[] = { 1368 { 1369 .reg = PMBUS_VOUT_UV_WARN_LIMIT, 1370 .attr = "min", 1371 .alarm = "min_alarm", 1372 .sbit = PB_VOLTAGE_UV_WARNING, 1373 }, { 1374 .reg = PMBUS_VOUT_UV_FAULT_LIMIT, 1375 .attr = "lcrit", 1376 .alarm = "lcrit_alarm", 1377 .sbit = PB_VOLTAGE_UV_FAULT, 1378 }, { 1379 .reg = PMBUS_VOUT_OV_WARN_LIMIT, 1380 .attr = "max", 1381 .alarm = "max_alarm", 1382 .sbit = PB_VOLTAGE_OV_WARNING, 1383 }, { 1384 .reg = PMBUS_VOUT_OV_FAULT_LIMIT, 1385 .attr = "crit", 1386 .alarm = "crit_alarm", 1387 .sbit = PB_VOLTAGE_OV_FAULT, 1388 }, { 1389 .reg = PMBUS_VIRT_READ_VOUT_AVG, 1390 .update = true, 1391 .attr = "average", 1392 }, { 1393 .reg = PMBUS_VIRT_READ_VOUT_MIN, 1394 .update = true, 1395 .attr = "lowest", 1396 }, { 1397 .reg = PMBUS_VIRT_READ_VOUT_MAX, 1398 .update = true, 1399 .attr = "highest", 1400 }, { 1401 .reg = PMBUS_VIRT_RESET_VOUT_HISTORY, 1402 .attr = "reset_history", 1403 } 1404 }; 1405 1406 static const struct pmbus_sensor_attr voltage_attributes[] = { 1407 { 1408 .reg = PMBUS_READ_VIN, 1409 .class = PSC_VOLTAGE_IN, 1410 .label = "vin", 1411 .func = PMBUS_HAVE_VIN, 1412 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1413 .sbase = PB_STATUS_INPUT_BASE, 1414 .gbit = PB_STATUS_VIN_UV, 1415 .limit = vin_limit_attrs, 1416 .nlimit = ARRAY_SIZE(vin_limit_attrs), 1417 }, { 1418 .reg = PMBUS_VIRT_READ_VMON, 1419 .class = PSC_VOLTAGE_IN, 1420 .label = "vmon", 1421 .func = PMBUS_HAVE_VMON, 1422 .sfunc = PMBUS_HAVE_STATUS_VMON, 1423 .sbase = PB_STATUS_VMON_BASE, 1424 .limit = vmon_limit_attrs, 1425 .nlimit = ARRAY_SIZE(vmon_limit_attrs), 1426 }, { 1427 .reg = PMBUS_READ_VCAP, 1428 .class = PSC_VOLTAGE_IN, 1429 .label = "vcap", 1430 .func = PMBUS_HAVE_VCAP, 1431 }, { 1432 .reg = PMBUS_READ_VOUT, 1433 .class = PSC_VOLTAGE_OUT, 1434 .label = "vout", 1435 .paged = true, 1436 .func = PMBUS_HAVE_VOUT, 1437 .sfunc = PMBUS_HAVE_STATUS_VOUT, 1438 .sbase = PB_STATUS_VOUT_BASE, 1439 .gbit = PB_STATUS_VOUT_OV, 1440 .limit = vout_limit_attrs, 1441 .nlimit = ARRAY_SIZE(vout_limit_attrs), 1442 } 1443 }; 1444 1445 /* Current attributes */ 1446 1447 static const struct pmbus_limit_attr iin_limit_attrs[] = { 1448 { 1449 .reg = PMBUS_IIN_OC_WARN_LIMIT, 1450 .attr = "max", 1451 .alarm = "max_alarm", 1452 .sbit = PB_IIN_OC_WARNING, 1453 }, { 1454 .reg = PMBUS_IIN_OC_FAULT_LIMIT, 1455 .attr = "crit", 1456 .alarm = "crit_alarm", 1457 .sbit = PB_IIN_OC_FAULT, 1458 }, { 1459 .reg = PMBUS_VIRT_READ_IIN_AVG, 1460 .update = true, 1461 .attr = "average", 1462 }, { 1463 .reg = PMBUS_VIRT_READ_IIN_MIN, 1464 .update = true, 1465 .attr = "lowest", 1466 }, { 1467 .reg = PMBUS_VIRT_READ_IIN_MAX, 1468 .update = true, 1469 .attr = "highest", 1470 }, { 1471 .reg = PMBUS_VIRT_RESET_IIN_HISTORY, 1472 .attr = "reset_history", 1473 } 1474 }; 1475 1476 static const struct pmbus_limit_attr iout_limit_attrs[] = { 1477 { 1478 .reg = PMBUS_IOUT_OC_WARN_LIMIT, 1479 .attr = "max", 1480 .alarm = "max_alarm", 1481 .sbit = PB_IOUT_OC_WARNING, 1482 }, { 1483 .reg = PMBUS_IOUT_UC_FAULT_LIMIT, 1484 .attr = "lcrit", 1485 .alarm = "lcrit_alarm", 1486 .sbit = PB_IOUT_UC_FAULT, 1487 }, { 1488 .reg = PMBUS_IOUT_OC_FAULT_LIMIT, 1489 .attr = "crit", 1490 .alarm = "crit_alarm", 1491 .sbit = PB_IOUT_OC_FAULT, 1492 }, { 1493 .reg = PMBUS_VIRT_READ_IOUT_AVG, 1494 .update = true, 1495 .attr = "average", 1496 }, { 1497 .reg = PMBUS_VIRT_READ_IOUT_MIN, 1498 .update = true, 1499 .attr = "lowest", 1500 }, { 1501 .reg = PMBUS_VIRT_READ_IOUT_MAX, 1502 .update = true, 1503 .attr = "highest", 1504 }, { 1505 .reg = PMBUS_VIRT_RESET_IOUT_HISTORY, 1506 .attr = "reset_history", 1507 } 1508 }; 1509 1510 static const struct pmbus_sensor_attr current_attributes[] = { 1511 { 1512 .reg = PMBUS_READ_IIN, 1513 .class = PSC_CURRENT_IN, 1514 .label = "iin", 1515 .func = PMBUS_HAVE_IIN, 1516 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1517 .sbase = PB_STATUS_INPUT_BASE, 1518 .gbit = PB_STATUS_INPUT, 1519 .limit = iin_limit_attrs, 1520 .nlimit = ARRAY_SIZE(iin_limit_attrs), 1521 }, { 1522 .reg = PMBUS_READ_IOUT, 1523 .class = PSC_CURRENT_OUT, 1524 .label = "iout", 1525 .paged = true, 1526 .func = PMBUS_HAVE_IOUT, 1527 .sfunc = PMBUS_HAVE_STATUS_IOUT, 1528 .sbase = PB_STATUS_IOUT_BASE, 1529 .gbit = PB_STATUS_IOUT_OC, 1530 .limit = iout_limit_attrs, 1531 .nlimit = ARRAY_SIZE(iout_limit_attrs), 1532 } 1533 }; 1534 1535 /* Power attributes */ 1536 1537 static const struct pmbus_limit_attr pin_limit_attrs[] = { 1538 { 1539 .reg = PMBUS_PIN_OP_WARN_LIMIT, 1540 .attr = "max", 1541 .alarm = "alarm", 1542 .sbit = PB_PIN_OP_WARNING, 1543 }, { 1544 .reg = PMBUS_VIRT_READ_PIN_AVG, 1545 .update = true, 1546 .attr = "average", 1547 }, { 1548 .reg = PMBUS_VIRT_READ_PIN_MIN, 1549 .update = true, 1550 .attr = "input_lowest", 1551 }, { 1552 .reg = PMBUS_VIRT_READ_PIN_MAX, 1553 .update = true, 1554 .attr = "input_highest", 1555 }, { 1556 .reg = PMBUS_VIRT_RESET_PIN_HISTORY, 1557 .attr = "reset_history", 1558 } 1559 }; 1560 1561 static const struct pmbus_limit_attr pout_limit_attrs[] = { 1562 { 1563 .reg = PMBUS_POUT_MAX, 1564 .attr = "cap", 1565 .alarm = "cap_alarm", 1566 .sbit = PB_POWER_LIMITING, 1567 }, { 1568 .reg = PMBUS_POUT_OP_WARN_LIMIT, 1569 .attr = "max", 1570 .alarm = "max_alarm", 1571 .sbit = PB_POUT_OP_WARNING, 1572 }, { 1573 .reg = PMBUS_POUT_OP_FAULT_LIMIT, 1574 .attr = "crit", 1575 .alarm = "crit_alarm", 1576 .sbit = PB_POUT_OP_FAULT, 1577 }, { 1578 .reg = PMBUS_VIRT_READ_POUT_AVG, 1579 .update = true, 1580 .attr = "average", 1581 }, { 1582 .reg = PMBUS_VIRT_READ_POUT_MIN, 1583 .update = true, 1584 .attr = "input_lowest", 1585 }, { 1586 .reg = PMBUS_VIRT_READ_POUT_MAX, 1587 .update = true, 1588 .attr = "input_highest", 1589 }, { 1590 .reg = PMBUS_VIRT_RESET_POUT_HISTORY, 1591 .attr = "reset_history", 1592 } 1593 }; 1594 1595 static const struct pmbus_sensor_attr power_attributes[] = { 1596 { 1597 .reg = PMBUS_READ_PIN, 1598 .class = PSC_POWER, 1599 .label = "pin", 1600 .func = PMBUS_HAVE_PIN, 1601 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1602 .sbase = PB_STATUS_INPUT_BASE, 1603 .gbit = PB_STATUS_INPUT, 1604 .limit = pin_limit_attrs, 1605 .nlimit = ARRAY_SIZE(pin_limit_attrs), 1606 }, { 1607 .reg = PMBUS_READ_POUT, 1608 .class = PSC_POWER, 1609 .label = "pout", 1610 .paged = true, 1611 .func = PMBUS_HAVE_POUT, 1612 .sfunc = PMBUS_HAVE_STATUS_IOUT, 1613 .sbase = PB_STATUS_IOUT_BASE, 1614 .limit = pout_limit_attrs, 1615 .nlimit = ARRAY_SIZE(pout_limit_attrs), 1616 } 1617 }; 1618 1619 /* Temperature atributes */ 1620 1621 static const struct pmbus_limit_attr temp_limit_attrs[] = { 1622 { 1623 .reg = PMBUS_UT_WARN_LIMIT, 1624 .low = true, 1625 .attr = "min", 1626 .alarm = "min_alarm", 1627 .sbit = PB_TEMP_UT_WARNING, 1628 }, { 1629 .reg = PMBUS_UT_FAULT_LIMIT, 1630 .low = true, 1631 .attr = "lcrit", 1632 .alarm = "lcrit_alarm", 1633 .sbit = PB_TEMP_UT_FAULT, 1634 }, { 1635 .reg = PMBUS_OT_WARN_LIMIT, 1636 .attr = "max", 1637 .alarm = "max_alarm", 1638 .sbit = PB_TEMP_OT_WARNING, 1639 }, { 1640 .reg = PMBUS_OT_FAULT_LIMIT, 1641 .attr = "crit", 1642 .alarm = "crit_alarm", 1643 .sbit = PB_TEMP_OT_FAULT, 1644 }, { 1645 .reg = PMBUS_VIRT_READ_TEMP_MIN, 1646 .attr = "lowest", 1647 }, { 1648 .reg = PMBUS_VIRT_READ_TEMP_AVG, 1649 .attr = "average", 1650 }, { 1651 .reg = PMBUS_VIRT_READ_TEMP_MAX, 1652 .attr = "highest", 1653 }, { 1654 .reg = PMBUS_VIRT_RESET_TEMP_HISTORY, 1655 .attr = "reset_history", 1656 } 1657 }; 1658 1659 static const struct pmbus_limit_attr temp_limit_attrs2[] = { 1660 { 1661 .reg = PMBUS_UT_WARN_LIMIT, 1662 .low = true, 1663 .attr = "min", 1664 .alarm = "min_alarm", 1665 .sbit = PB_TEMP_UT_WARNING, 1666 }, { 1667 .reg = PMBUS_UT_FAULT_LIMIT, 1668 .low = true, 1669 .attr = "lcrit", 1670 .alarm = "lcrit_alarm", 1671 .sbit = PB_TEMP_UT_FAULT, 1672 }, { 1673 .reg = PMBUS_OT_WARN_LIMIT, 1674 .attr = "max", 1675 .alarm = "max_alarm", 1676 .sbit = PB_TEMP_OT_WARNING, 1677 }, { 1678 .reg = PMBUS_OT_FAULT_LIMIT, 1679 .attr = "crit", 1680 .alarm = "crit_alarm", 1681 .sbit = PB_TEMP_OT_FAULT, 1682 }, { 1683 .reg = PMBUS_VIRT_READ_TEMP2_MIN, 1684 .attr = "lowest", 1685 }, { 1686 .reg = PMBUS_VIRT_READ_TEMP2_AVG, 1687 .attr = "average", 1688 }, { 1689 .reg = PMBUS_VIRT_READ_TEMP2_MAX, 1690 .attr = "highest", 1691 }, { 1692 .reg = PMBUS_VIRT_RESET_TEMP2_HISTORY, 1693 .attr = "reset_history", 1694 } 1695 }; 1696 1697 static const struct pmbus_limit_attr temp_limit_attrs3[] = { 1698 { 1699 .reg = PMBUS_UT_WARN_LIMIT, 1700 .low = true, 1701 .attr = "min", 1702 .alarm = "min_alarm", 1703 .sbit = PB_TEMP_UT_WARNING, 1704 }, { 1705 .reg = PMBUS_UT_FAULT_LIMIT, 1706 .low = true, 1707 .attr = "lcrit", 1708 .alarm = "lcrit_alarm", 1709 .sbit = PB_TEMP_UT_FAULT, 1710 }, { 1711 .reg = PMBUS_OT_WARN_LIMIT, 1712 .attr = "max", 1713 .alarm = "max_alarm", 1714 .sbit = PB_TEMP_OT_WARNING, 1715 }, { 1716 .reg = PMBUS_OT_FAULT_LIMIT, 1717 .attr = "crit", 1718 .alarm = "crit_alarm", 1719 .sbit = PB_TEMP_OT_FAULT, 1720 } 1721 }; 1722 1723 static const struct pmbus_sensor_attr temp_attributes[] = { 1724 { 1725 .reg = PMBUS_READ_TEMPERATURE_1, 1726 .class = PSC_TEMPERATURE, 1727 .paged = true, 1728 .update = true, 1729 .compare = true, 1730 .func = PMBUS_HAVE_TEMP, 1731 .sfunc = PMBUS_HAVE_STATUS_TEMP, 1732 .sbase = PB_STATUS_TEMP_BASE, 1733 .gbit = PB_STATUS_TEMPERATURE, 1734 .limit = temp_limit_attrs, 1735 .nlimit = ARRAY_SIZE(temp_limit_attrs), 1736 }, { 1737 .reg = PMBUS_READ_TEMPERATURE_2, 1738 .class = PSC_TEMPERATURE, 1739 .paged = true, 1740 .update = true, 1741 .compare = true, 1742 .func = PMBUS_HAVE_TEMP2, 1743 .sfunc = PMBUS_HAVE_STATUS_TEMP, 1744 .sbase = PB_STATUS_TEMP_BASE, 1745 .gbit = PB_STATUS_TEMPERATURE, 1746 .limit = temp_limit_attrs2, 1747 .nlimit = ARRAY_SIZE(temp_limit_attrs2), 1748 }, { 1749 .reg = PMBUS_READ_TEMPERATURE_3, 1750 .class = PSC_TEMPERATURE, 1751 .paged = true, 1752 .update = true, 1753 .compare = true, 1754 .func = PMBUS_HAVE_TEMP3, 1755 .sfunc = PMBUS_HAVE_STATUS_TEMP, 1756 .sbase = PB_STATUS_TEMP_BASE, 1757 .gbit = PB_STATUS_TEMPERATURE, 1758 .limit = temp_limit_attrs3, 1759 .nlimit = ARRAY_SIZE(temp_limit_attrs3), 1760 } 1761 }; 1762 1763 static const int pmbus_fan_registers[] = { 1764 PMBUS_READ_FAN_SPEED_1, 1765 PMBUS_READ_FAN_SPEED_2, 1766 PMBUS_READ_FAN_SPEED_3, 1767 PMBUS_READ_FAN_SPEED_4 1768 }; 1769 1770 static const int pmbus_fan_status_registers[] = { 1771 PMBUS_STATUS_FAN_12, 1772 PMBUS_STATUS_FAN_12, 1773 PMBUS_STATUS_FAN_34, 1774 PMBUS_STATUS_FAN_34 1775 }; 1776 1777 static const u32 pmbus_fan_flags[] = { 1778 PMBUS_HAVE_FAN12, 1779 PMBUS_HAVE_FAN12, 1780 PMBUS_HAVE_FAN34, 1781 PMBUS_HAVE_FAN34 1782 }; 1783 1784 static const u32 pmbus_fan_status_flags[] = { 1785 PMBUS_HAVE_STATUS_FAN12, 1786 PMBUS_HAVE_STATUS_FAN12, 1787 PMBUS_HAVE_STATUS_FAN34, 1788 PMBUS_HAVE_STATUS_FAN34 1789 }; 1790 1791 /* Fans */ 1792 1793 /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */ 1794 static int pmbus_add_fan_ctrl(struct i2c_client *client, 1795 struct pmbus_data *data, int index, int page, int id, 1796 u8 config) 1797 { 1798 struct pmbus_sensor *sensor; 1799 1800 sensor = pmbus_add_sensor(data, "fan", "target", index, page, 1801 PMBUS_VIRT_FAN_TARGET_1 + id, PSC_FAN, 1802 false, false, true); 1803 1804 if (!sensor) 1805 return -ENOMEM; 1806 1807 if (!((data->info->func[page] & PMBUS_HAVE_PWM12) || 1808 (data->info->func[page] & PMBUS_HAVE_PWM34))) 1809 return 0; 1810 1811 sensor = pmbus_add_sensor(data, "pwm", NULL, index, page, 1812 PMBUS_VIRT_PWM_1 + id, PSC_PWM, 1813 false, false, true); 1814 1815 if (!sensor) 1816 return -ENOMEM; 1817 1818 sensor = pmbus_add_sensor(data, "pwm", "enable", index, page, 1819 PMBUS_VIRT_PWM_ENABLE_1 + id, PSC_PWM, 1820 true, false, false); 1821 1822 if (!sensor) 1823 return -ENOMEM; 1824 1825 return 0; 1826 } 1827 1828 static int pmbus_add_fan_attributes(struct i2c_client *client, 1829 struct pmbus_data *data) 1830 { 1831 const struct pmbus_driver_info *info = data->info; 1832 int index = 1; 1833 int page; 1834 int ret; 1835 1836 for (page = 0; page < info->pages; page++) { 1837 int f; 1838 1839 for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) { 1840 int regval; 1841 1842 if (!(info->func[page] & pmbus_fan_flags[f])) 1843 break; 1844 1845 if (!pmbus_check_word_register(client, page, 1846 pmbus_fan_registers[f])) 1847 break; 1848 1849 /* 1850 * Skip fan if not installed. 1851 * Each fan configuration register covers multiple fans, 1852 * so we have to do some magic. 1853 */ 1854 regval = _pmbus_read_byte_data(client, page, 1855 pmbus_fan_config_registers[f]); 1856 if (regval < 0 || 1857 (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4))))) 1858 continue; 1859 1860 if (pmbus_add_sensor(data, "fan", "input", index, 1861 page, pmbus_fan_registers[f], 1862 PSC_FAN, true, true, true) == NULL) 1863 return -ENOMEM; 1864 1865 /* Fan control */ 1866 if (pmbus_check_word_register(client, page, 1867 pmbus_fan_command_registers[f])) { 1868 ret = pmbus_add_fan_ctrl(client, data, index, 1869 page, f, regval); 1870 if (ret < 0) 1871 return ret; 1872 } 1873 1874 /* 1875 * Each fan status register covers multiple fans, 1876 * so we have to do some magic. 1877 */ 1878 if ((info->func[page] & pmbus_fan_status_flags[f]) && 1879 pmbus_check_byte_register(client, 1880 page, pmbus_fan_status_registers[f])) { 1881 int base; 1882 1883 if (f > 1) /* fan 3, 4 */ 1884 base = PB_STATUS_FAN34_BASE + page; 1885 else 1886 base = PB_STATUS_FAN_BASE + page; 1887 ret = pmbus_add_boolean(data, "fan", 1888 "alarm", index, NULL, NULL, base, 1889 PB_FAN_FAN1_WARNING >> (f & 1)); 1890 if (ret) 1891 return ret; 1892 ret = pmbus_add_boolean(data, "fan", 1893 "fault", index, NULL, NULL, base, 1894 PB_FAN_FAN1_FAULT >> (f & 1)); 1895 if (ret) 1896 return ret; 1897 } 1898 index++; 1899 } 1900 } 1901 return 0; 1902 } 1903 1904 static int pmbus_find_attributes(struct i2c_client *client, 1905 struct pmbus_data *data) 1906 { 1907 int ret; 1908 1909 /* Voltage sensors */ 1910 ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes, 1911 ARRAY_SIZE(voltage_attributes)); 1912 if (ret) 1913 return ret; 1914 1915 /* Current sensors */ 1916 ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes, 1917 ARRAY_SIZE(current_attributes)); 1918 if (ret) 1919 return ret; 1920 1921 /* Power sensors */ 1922 ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes, 1923 ARRAY_SIZE(power_attributes)); 1924 if (ret) 1925 return ret; 1926 1927 /* Temperature sensors */ 1928 ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes, 1929 ARRAY_SIZE(temp_attributes)); 1930 if (ret) 1931 return ret; 1932 1933 /* Fans */ 1934 ret = pmbus_add_fan_attributes(client, data); 1935 return ret; 1936 } 1937 1938 /* 1939 * Identify chip parameters. 1940 * This function is called for all chips. 1941 */ 1942 static int pmbus_identify_common(struct i2c_client *client, 1943 struct pmbus_data *data, int page) 1944 { 1945 int vout_mode = -1; 1946 1947 if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE)) 1948 vout_mode = _pmbus_read_byte_data(client, page, 1949 PMBUS_VOUT_MODE); 1950 if (vout_mode >= 0 && vout_mode != 0xff) { 1951 /* 1952 * Not all chips support the VOUT_MODE command, 1953 * so a failure to read it is not an error. 1954 */ 1955 switch (vout_mode >> 5) { 1956 case 0: /* linear mode */ 1957 if (data->info->format[PSC_VOLTAGE_OUT] != linear) 1958 return -ENODEV; 1959 1960 data->exponent[page] = ((s8)(vout_mode << 3)) >> 3; 1961 break; 1962 case 1: /* VID mode */ 1963 if (data->info->format[PSC_VOLTAGE_OUT] != vid) 1964 return -ENODEV; 1965 break; 1966 case 2: /* direct mode */ 1967 if (data->info->format[PSC_VOLTAGE_OUT] != direct) 1968 return -ENODEV; 1969 break; 1970 default: 1971 return -ENODEV; 1972 } 1973 } 1974 1975 pmbus_clear_fault_page(client, page); 1976 return 0; 1977 } 1978 1979 static int pmbus_read_status_byte(struct i2c_client *client, int page) 1980 { 1981 return _pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE); 1982 } 1983 1984 static int pmbus_read_status_word(struct i2c_client *client, int page) 1985 { 1986 return _pmbus_read_word_data(client, page, PMBUS_STATUS_WORD); 1987 } 1988 1989 static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data, 1990 struct pmbus_driver_info *info) 1991 { 1992 struct device *dev = &client->dev; 1993 int page, ret; 1994 1995 /* 1996 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try 1997 * to use PMBUS_STATUS_BYTE instead if that is the case. 1998 * Bail out if both registers are not supported. 1999 */ 2000 data->read_status = pmbus_read_status_word; 2001 ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD); 2002 if (ret < 0 || ret == 0xffff) { 2003 data->read_status = pmbus_read_status_byte; 2004 ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE); 2005 if (ret < 0 || ret == 0xff) { 2006 dev_err(dev, "PMBus status register not found\n"); 2007 return -ENODEV; 2008 } 2009 } else { 2010 data->has_status_word = true; 2011 } 2012 2013 /* Enable PEC if the controller supports it */ 2014 ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY); 2015 if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK)) 2016 client->flags |= I2C_CLIENT_PEC; 2017 2018 if (data->info->pages) 2019 pmbus_clear_faults(client); 2020 else 2021 pmbus_clear_fault_page(client, -1); 2022 2023 if (info->identify) { 2024 ret = (*info->identify)(client, info); 2025 if (ret < 0) { 2026 dev_err(dev, "Chip identification failed\n"); 2027 return ret; 2028 } 2029 } 2030 2031 if (info->pages <= 0 || info->pages > PMBUS_PAGES) { 2032 dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages); 2033 return -ENODEV; 2034 } 2035 2036 for (page = 0; page < info->pages; page++) { 2037 ret = pmbus_identify_common(client, data, page); 2038 if (ret < 0) { 2039 dev_err(dev, "Failed to identify chip capabilities\n"); 2040 return ret; 2041 } 2042 } 2043 return 0; 2044 } 2045 2046 #if IS_ENABLED(CONFIG_REGULATOR) 2047 static int pmbus_regulator_is_enabled(struct regulator_dev *rdev) 2048 { 2049 struct device *dev = rdev_get_dev(rdev); 2050 struct i2c_client *client = to_i2c_client(dev->parent); 2051 u8 page = rdev_get_id(rdev); 2052 int ret; 2053 2054 ret = pmbus_read_byte_data(client, page, PMBUS_OPERATION); 2055 if (ret < 0) 2056 return ret; 2057 2058 return !!(ret & PB_OPERATION_CONTROL_ON); 2059 } 2060 2061 static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable) 2062 { 2063 struct device *dev = rdev_get_dev(rdev); 2064 struct i2c_client *client = to_i2c_client(dev->parent); 2065 u8 page = rdev_get_id(rdev); 2066 2067 return pmbus_update_byte_data(client, page, PMBUS_OPERATION, 2068 PB_OPERATION_CONTROL_ON, 2069 enable ? PB_OPERATION_CONTROL_ON : 0); 2070 } 2071 2072 static int pmbus_regulator_enable(struct regulator_dev *rdev) 2073 { 2074 return _pmbus_regulator_on_off(rdev, 1); 2075 } 2076 2077 static int pmbus_regulator_disable(struct regulator_dev *rdev) 2078 { 2079 return _pmbus_regulator_on_off(rdev, 0); 2080 } 2081 2082 const struct regulator_ops pmbus_regulator_ops = { 2083 .enable = pmbus_regulator_enable, 2084 .disable = pmbus_regulator_disable, 2085 .is_enabled = pmbus_regulator_is_enabled, 2086 }; 2087 EXPORT_SYMBOL_GPL(pmbus_regulator_ops); 2088 2089 static int pmbus_regulator_register(struct pmbus_data *data) 2090 { 2091 struct device *dev = data->dev; 2092 const struct pmbus_driver_info *info = data->info; 2093 const struct pmbus_platform_data *pdata = dev_get_platdata(dev); 2094 struct regulator_dev *rdev; 2095 int i; 2096 2097 for (i = 0; i < info->num_regulators; i++) { 2098 struct regulator_config config = { }; 2099 2100 config.dev = dev; 2101 config.driver_data = data; 2102 2103 if (pdata && pdata->reg_init_data) 2104 config.init_data = &pdata->reg_init_data[i]; 2105 2106 rdev = devm_regulator_register(dev, &info->reg_desc[i], 2107 &config); 2108 if (IS_ERR(rdev)) { 2109 dev_err(dev, "Failed to register %s regulator\n", 2110 info->reg_desc[i].name); 2111 return PTR_ERR(rdev); 2112 } 2113 } 2114 2115 return 0; 2116 } 2117 #else 2118 static int pmbus_regulator_register(struct pmbus_data *data) 2119 { 2120 return 0; 2121 } 2122 #endif 2123 2124 static struct dentry *pmbus_debugfs_dir; /* pmbus debugfs directory */ 2125 2126 #if IS_ENABLED(CONFIG_DEBUG_FS) 2127 static int pmbus_debugfs_get(void *data, u64 *val) 2128 { 2129 int rc; 2130 struct pmbus_debugfs_entry *entry = data; 2131 2132 rc = _pmbus_read_byte_data(entry->client, entry->page, entry->reg); 2133 if (rc < 0) 2134 return rc; 2135 2136 *val = rc; 2137 2138 return 0; 2139 } 2140 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL, 2141 "0x%02llx\n"); 2142 2143 static int pmbus_debugfs_get_status(void *data, u64 *val) 2144 { 2145 int rc; 2146 struct pmbus_debugfs_entry *entry = data; 2147 struct pmbus_data *pdata = i2c_get_clientdata(entry->client); 2148 2149 rc = pdata->read_status(entry->client, entry->page); 2150 if (rc < 0) 2151 return rc; 2152 2153 *val = rc; 2154 2155 return 0; 2156 } 2157 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status, 2158 NULL, "0x%04llx\n"); 2159 2160 static int pmbus_init_debugfs(struct i2c_client *client, 2161 struct pmbus_data *data) 2162 { 2163 int i, idx = 0; 2164 char name[PMBUS_NAME_SIZE]; 2165 struct pmbus_debugfs_entry *entries; 2166 2167 if (!pmbus_debugfs_dir) 2168 return -ENODEV; 2169 2170 /* 2171 * Create the debugfs directory for this device. Use the hwmon device 2172 * name to avoid conflicts (hwmon numbers are globally unique). 2173 */ 2174 data->debugfs = debugfs_create_dir(dev_name(data->hwmon_dev), 2175 pmbus_debugfs_dir); 2176 if (IS_ERR_OR_NULL(data->debugfs)) { 2177 data->debugfs = NULL; 2178 return -ENODEV; 2179 } 2180 2181 /* Allocate the max possible entries we need. */ 2182 entries = devm_kcalloc(data->dev, 2183 data->info->pages * 10, sizeof(*entries), 2184 GFP_KERNEL); 2185 if (!entries) 2186 return -ENOMEM; 2187 2188 for (i = 0; i < data->info->pages; ++i) { 2189 /* Check accessibility of status register if it's not page 0 */ 2190 if (!i || pmbus_check_status_register(client, i)) { 2191 /* No need to set reg as we have special read op. */ 2192 entries[idx].client = client; 2193 entries[idx].page = i; 2194 scnprintf(name, PMBUS_NAME_SIZE, "status%d", i); 2195 debugfs_create_file(name, 0444, data->debugfs, 2196 &entries[idx++], 2197 &pmbus_debugfs_ops_status); 2198 } 2199 2200 if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) { 2201 entries[idx].client = client; 2202 entries[idx].page = i; 2203 entries[idx].reg = PMBUS_STATUS_VOUT; 2204 scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i); 2205 debugfs_create_file(name, 0444, data->debugfs, 2206 &entries[idx++], 2207 &pmbus_debugfs_ops); 2208 } 2209 2210 if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) { 2211 entries[idx].client = client; 2212 entries[idx].page = i; 2213 entries[idx].reg = PMBUS_STATUS_IOUT; 2214 scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i); 2215 debugfs_create_file(name, 0444, data->debugfs, 2216 &entries[idx++], 2217 &pmbus_debugfs_ops); 2218 } 2219 2220 if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) { 2221 entries[idx].client = client; 2222 entries[idx].page = i; 2223 entries[idx].reg = PMBUS_STATUS_INPUT; 2224 scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i); 2225 debugfs_create_file(name, 0444, data->debugfs, 2226 &entries[idx++], 2227 &pmbus_debugfs_ops); 2228 } 2229 2230 if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) { 2231 entries[idx].client = client; 2232 entries[idx].page = i; 2233 entries[idx].reg = PMBUS_STATUS_TEMPERATURE; 2234 scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i); 2235 debugfs_create_file(name, 0444, data->debugfs, 2236 &entries[idx++], 2237 &pmbus_debugfs_ops); 2238 } 2239 2240 if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) { 2241 entries[idx].client = client; 2242 entries[idx].page = i; 2243 entries[idx].reg = PMBUS_STATUS_CML; 2244 scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i); 2245 debugfs_create_file(name, 0444, data->debugfs, 2246 &entries[idx++], 2247 &pmbus_debugfs_ops); 2248 } 2249 2250 if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) { 2251 entries[idx].client = client; 2252 entries[idx].page = i; 2253 entries[idx].reg = PMBUS_STATUS_OTHER; 2254 scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i); 2255 debugfs_create_file(name, 0444, data->debugfs, 2256 &entries[idx++], 2257 &pmbus_debugfs_ops); 2258 } 2259 2260 if (pmbus_check_byte_register(client, i, 2261 PMBUS_STATUS_MFR_SPECIFIC)) { 2262 entries[idx].client = client; 2263 entries[idx].page = i; 2264 entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC; 2265 scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i); 2266 debugfs_create_file(name, 0444, data->debugfs, 2267 &entries[idx++], 2268 &pmbus_debugfs_ops); 2269 } 2270 2271 if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) { 2272 entries[idx].client = client; 2273 entries[idx].page = i; 2274 entries[idx].reg = PMBUS_STATUS_FAN_12; 2275 scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i); 2276 debugfs_create_file(name, 0444, data->debugfs, 2277 &entries[idx++], 2278 &pmbus_debugfs_ops); 2279 } 2280 2281 if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) { 2282 entries[idx].client = client; 2283 entries[idx].page = i; 2284 entries[idx].reg = PMBUS_STATUS_FAN_34; 2285 scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i); 2286 debugfs_create_file(name, 0444, data->debugfs, 2287 &entries[idx++], 2288 &pmbus_debugfs_ops); 2289 } 2290 } 2291 2292 return 0; 2293 } 2294 #else 2295 static int pmbus_init_debugfs(struct i2c_client *client, 2296 struct pmbus_data *data) 2297 { 2298 return 0; 2299 } 2300 #endif /* IS_ENABLED(CONFIG_DEBUG_FS) */ 2301 2302 int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id, 2303 struct pmbus_driver_info *info) 2304 { 2305 struct device *dev = &client->dev; 2306 const struct pmbus_platform_data *pdata = dev_get_platdata(dev); 2307 struct pmbus_data *data; 2308 int ret; 2309 2310 if (!info) 2311 return -ENODEV; 2312 2313 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE 2314 | I2C_FUNC_SMBUS_BYTE_DATA 2315 | I2C_FUNC_SMBUS_WORD_DATA)) 2316 return -ENODEV; 2317 2318 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 2319 if (!data) 2320 return -ENOMEM; 2321 2322 i2c_set_clientdata(client, data); 2323 mutex_init(&data->update_lock); 2324 data->dev = dev; 2325 2326 if (pdata) 2327 data->flags = pdata->flags; 2328 data->info = info; 2329 2330 ret = pmbus_init_common(client, data, info); 2331 if (ret < 0) 2332 return ret; 2333 2334 ret = pmbus_find_attributes(client, data); 2335 if (ret) 2336 goto out_kfree; 2337 2338 /* 2339 * If there are no attributes, something is wrong. 2340 * Bail out instead of trying to register nothing. 2341 */ 2342 if (!data->num_attributes) { 2343 dev_err(dev, "No attributes found\n"); 2344 ret = -ENODEV; 2345 goto out_kfree; 2346 } 2347 2348 data->groups[0] = &data->group; 2349 data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name, 2350 data, data->groups); 2351 if (IS_ERR(data->hwmon_dev)) { 2352 ret = PTR_ERR(data->hwmon_dev); 2353 dev_err(dev, "Failed to register hwmon device\n"); 2354 goto out_kfree; 2355 } 2356 2357 ret = pmbus_regulator_register(data); 2358 if (ret) 2359 goto out_unregister; 2360 2361 ret = pmbus_init_debugfs(client, data); 2362 if (ret) 2363 dev_warn(dev, "Failed to register debugfs\n"); 2364 2365 return 0; 2366 2367 out_unregister: 2368 hwmon_device_unregister(data->hwmon_dev); 2369 out_kfree: 2370 kfree(data->group.attrs); 2371 return ret; 2372 } 2373 EXPORT_SYMBOL_GPL(pmbus_do_probe); 2374 2375 int pmbus_do_remove(struct i2c_client *client) 2376 { 2377 struct pmbus_data *data = i2c_get_clientdata(client); 2378 2379 debugfs_remove_recursive(data->debugfs); 2380 2381 hwmon_device_unregister(data->hwmon_dev); 2382 kfree(data->group.attrs); 2383 return 0; 2384 } 2385 EXPORT_SYMBOL_GPL(pmbus_do_remove); 2386 2387 struct dentry *pmbus_get_debugfs_dir(struct i2c_client *client) 2388 { 2389 struct pmbus_data *data = i2c_get_clientdata(client); 2390 2391 return data->debugfs; 2392 } 2393 EXPORT_SYMBOL_GPL(pmbus_get_debugfs_dir); 2394 2395 static int __init pmbus_core_init(void) 2396 { 2397 pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL); 2398 if (IS_ERR(pmbus_debugfs_dir)) 2399 pmbus_debugfs_dir = NULL; 2400 2401 return 0; 2402 } 2403 2404 static void __exit pmbus_core_exit(void) 2405 { 2406 debugfs_remove_recursive(pmbus_debugfs_dir); 2407 } 2408 2409 module_init(pmbus_core_init); 2410 module_exit(pmbus_core_exit); 2411 2412 MODULE_AUTHOR("Guenter Roeck"); 2413 MODULE_DESCRIPTION("PMBus core driver"); 2414 MODULE_LICENSE("GPL"); 2415