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/kernel.h> 23 #include <linux/module.h> 24 #include <linux/init.h> 25 #include <linux/err.h> 26 #include <linux/slab.h> 27 #include <linux/i2c.h> 28 #include <linux/hwmon.h> 29 #include <linux/hwmon-sysfs.h> 30 #include <linux/jiffies.h> 31 #include <linux/i2c/pmbus.h> 32 #include "pmbus.h" 33 34 /* 35 * Number of additional attribute pointers to allocate 36 * with each call to krealloc 37 */ 38 #define PMBUS_ATTR_ALLOC_SIZE 32 39 40 /* 41 * Index into status register array, per status register group 42 */ 43 #define PB_STATUS_BASE 0 44 #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES) 45 #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES) 46 #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES) 47 #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES) 48 #define PB_STATUS_TEMP_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES) 49 #define PB_STATUS_INPUT_BASE (PB_STATUS_TEMP_BASE + PMBUS_PAGES) 50 #define PB_STATUS_VMON_BASE (PB_STATUS_INPUT_BASE + 1) 51 52 #define PB_NUM_STATUS_REG (PB_STATUS_VMON_BASE + 1) 53 54 #define PMBUS_NAME_SIZE 24 55 56 struct pmbus_sensor { 57 struct pmbus_sensor *next; 58 char name[PMBUS_NAME_SIZE]; /* sysfs sensor name */ 59 struct device_attribute attribute; 60 u8 page; /* page number */ 61 u16 reg; /* register */ 62 enum pmbus_sensor_classes class; /* sensor class */ 63 bool update; /* runtime sensor update needed */ 64 int data; /* Sensor data. 65 Negative if there was a read error */ 66 }; 67 #define to_pmbus_sensor(_attr) \ 68 container_of(_attr, struct pmbus_sensor, attribute) 69 70 struct pmbus_boolean { 71 char name[PMBUS_NAME_SIZE]; /* sysfs boolean name */ 72 struct sensor_device_attribute attribute; 73 struct pmbus_sensor *s1; 74 struct pmbus_sensor *s2; 75 }; 76 #define to_pmbus_boolean(_attr) \ 77 container_of(_attr, struct pmbus_boolean, attribute) 78 79 struct pmbus_label { 80 char name[PMBUS_NAME_SIZE]; /* sysfs label name */ 81 struct device_attribute attribute; 82 char label[PMBUS_NAME_SIZE]; /* label */ 83 }; 84 #define to_pmbus_label(_attr) \ 85 container_of(_attr, struct pmbus_label, attribute) 86 87 struct pmbus_data { 88 struct device *dev; 89 struct device *hwmon_dev; 90 91 u32 flags; /* from platform data */ 92 93 int exponent[PMBUS_PAGES]; 94 /* linear mode: exponent for output voltages */ 95 96 const struct pmbus_driver_info *info; 97 98 int max_attributes; 99 int num_attributes; 100 struct attribute_group group; 101 const struct attribute_group *groups[2]; 102 103 struct pmbus_sensor *sensors; 104 105 struct mutex update_lock; 106 bool valid; 107 unsigned long last_updated; /* in jiffies */ 108 109 /* 110 * A single status register covers multiple attributes, 111 * so we keep them all together. 112 */ 113 u8 status[PB_NUM_STATUS_REG]; 114 u8 status_register; 115 116 u8 currpage; 117 }; 118 119 void pmbus_clear_cache(struct i2c_client *client) 120 { 121 struct pmbus_data *data = i2c_get_clientdata(client); 122 123 data->valid = false; 124 } 125 EXPORT_SYMBOL_GPL(pmbus_clear_cache); 126 127 int pmbus_set_page(struct i2c_client *client, u8 page) 128 { 129 struct pmbus_data *data = i2c_get_clientdata(client); 130 int rv = 0; 131 int newpage; 132 133 if (page != data->currpage) { 134 rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page); 135 newpage = i2c_smbus_read_byte_data(client, PMBUS_PAGE); 136 if (newpage != page) 137 rv = -EIO; 138 else 139 data->currpage = page; 140 } 141 return rv; 142 } 143 EXPORT_SYMBOL_GPL(pmbus_set_page); 144 145 int pmbus_write_byte(struct i2c_client *client, int page, u8 value) 146 { 147 int rv; 148 149 if (page >= 0) { 150 rv = pmbus_set_page(client, page); 151 if (rv < 0) 152 return rv; 153 } 154 155 return i2c_smbus_write_byte(client, value); 156 } 157 EXPORT_SYMBOL_GPL(pmbus_write_byte); 158 159 /* 160 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if 161 * a device specific mapping function exists and calls it if necessary. 162 */ 163 static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value) 164 { 165 struct pmbus_data *data = i2c_get_clientdata(client); 166 const struct pmbus_driver_info *info = data->info; 167 int status; 168 169 if (info->write_byte) { 170 status = info->write_byte(client, page, value); 171 if (status != -ENODATA) 172 return status; 173 } 174 return pmbus_write_byte(client, page, value); 175 } 176 177 int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg, u16 word) 178 { 179 int rv; 180 181 rv = pmbus_set_page(client, page); 182 if (rv < 0) 183 return rv; 184 185 return i2c_smbus_write_word_data(client, reg, word); 186 } 187 EXPORT_SYMBOL_GPL(pmbus_write_word_data); 188 189 /* 190 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if 191 * a device specific mapping function exists and calls it if necessary. 192 */ 193 static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg, 194 u16 word) 195 { 196 struct pmbus_data *data = i2c_get_clientdata(client); 197 const struct pmbus_driver_info *info = data->info; 198 int status; 199 200 if (info->write_word_data) { 201 status = info->write_word_data(client, page, reg, word); 202 if (status != -ENODATA) 203 return status; 204 } 205 if (reg >= PMBUS_VIRT_BASE) 206 return -ENXIO; 207 return pmbus_write_word_data(client, page, reg, word); 208 } 209 210 int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg) 211 { 212 int rv; 213 214 rv = pmbus_set_page(client, page); 215 if (rv < 0) 216 return rv; 217 218 return i2c_smbus_read_word_data(client, reg); 219 } 220 EXPORT_SYMBOL_GPL(pmbus_read_word_data); 221 222 /* 223 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if 224 * a device specific mapping function exists and calls it if necessary. 225 */ 226 static int _pmbus_read_word_data(struct i2c_client *client, int page, int reg) 227 { 228 struct pmbus_data *data = i2c_get_clientdata(client); 229 const struct pmbus_driver_info *info = data->info; 230 int status; 231 232 if (info->read_word_data) { 233 status = info->read_word_data(client, page, reg); 234 if (status != -ENODATA) 235 return status; 236 } 237 if (reg >= PMBUS_VIRT_BASE) 238 return -ENXIO; 239 return pmbus_read_word_data(client, page, reg); 240 } 241 242 int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg) 243 { 244 int rv; 245 246 if (page >= 0) { 247 rv = pmbus_set_page(client, page); 248 if (rv < 0) 249 return rv; 250 } 251 252 return i2c_smbus_read_byte_data(client, reg); 253 } 254 EXPORT_SYMBOL_GPL(pmbus_read_byte_data); 255 256 /* 257 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if 258 * a device specific mapping function exists and calls it if necessary. 259 */ 260 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg) 261 { 262 struct pmbus_data *data = i2c_get_clientdata(client); 263 const struct pmbus_driver_info *info = data->info; 264 int status; 265 266 if (info->read_byte_data) { 267 status = info->read_byte_data(client, page, reg); 268 if (status != -ENODATA) 269 return status; 270 } 271 return pmbus_read_byte_data(client, page, reg); 272 } 273 274 static void pmbus_clear_fault_page(struct i2c_client *client, int page) 275 { 276 _pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS); 277 } 278 279 void pmbus_clear_faults(struct i2c_client *client) 280 { 281 struct pmbus_data *data = i2c_get_clientdata(client); 282 int i; 283 284 for (i = 0; i < data->info->pages; i++) 285 pmbus_clear_fault_page(client, i); 286 } 287 EXPORT_SYMBOL_GPL(pmbus_clear_faults); 288 289 static int pmbus_check_status_cml(struct i2c_client *client) 290 { 291 struct pmbus_data *data = i2c_get_clientdata(client); 292 int status, status2; 293 294 status = _pmbus_read_byte_data(client, -1, data->status_register); 295 if (status < 0 || (status & PB_STATUS_CML)) { 296 status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML); 297 if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND)) 298 return -EIO; 299 } 300 return 0; 301 } 302 303 static bool pmbus_check_register(struct i2c_client *client, 304 int (*func)(struct i2c_client *client, 305 int page, int reg), 306 int page, int reg) 307 { 308 int rv; 309 struct pmbus_data *data = i2c_get_clientdata(client); 310 311 rv = func(client, page, reg); 312 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK)) 313 rv = pmbus_check_status_cml(client); 314 pmbus_clear_fault_page(client, -1); 315 return rv >= 0; 316 } 317 318 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg) 319 { 320 return pmbus_check_register(client, _pmbus_read_byte_data, page, reg); 321 } 322 EXPORT_SYMBOL_GPL(pmbus_check_byte_register); 323 324 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg) 325 { 326 return pmbus_check_register(client, _pmbus_read_word_data, page, reg); 327 } 328 EXPORT_SYMBOL_GPL(pmbus_check_word_register); 329 330 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client) 331 { 332 struct pmbus_data *data = i2c_get_clientdata(client); 333 334 return data->info; 335 } 336 EXPORT_SYMBOL_GPL(pmbus_get_driver_info); 337 338 static struct _pmbus_status { 339 u32 func; 340 u16 base; 341 u16 reg; 342 } pmbus_status[] = { 343 { PMBUS_HAVE_STATUS_VOUT, PB_STATUS_VOUT_BASE, PMBUS_STATUS_VOUT }, 344 { PMBUS_HAVE_STATUS_IOUT, PB_STATUS_IOUT_BASE, PMBUS_STATUS_IOUT }, 345 { PMBUS_HAVE_STATUS_TEMP, PB_STATUS_TEMP_BASE, 346 PMBUS_STATUS_TEMPERATURE }, 347 { PMBUS_HAVE_STATUS_FAN12, PB_STATUS_FAN_BASE, PMBUS_STATUS_FAN_12 }, 348 { PMBUS_HAVE_STATUS_FAN34, PB_STATUS_FAN34_BASE, PMBUS_STATUS_FAN_34 }, 349 }; 350 351 static struct pmbus_data *pmbus_update_device(struct device *dev) 352 { 353 struct i2c_client *client = to_i2c_client(dev->parent); 354 struct pmbus_data *data = i2c_get_clientdata(client); 355 const struct pmbus_driver_info *info = data->info; 356 struct pmbus_sensor *sensor; 357 358 mutex_lock(&data->update_lock); 359 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 360 int i, j; 361 362 for (i = 0; i < info->pages; i++) { 363 data->status[PB_STATUS_BASE + i] 364 = _pmbus_read_byte_data(client, i, 365 data->status_register); 366 for (j = 0; j < ARRAY_SIZE(pmbus_status); j++) { 367 struct _pmbus_status *s = &pmbus_status[j]; 368 369 if (!(info->func[i] & s->func)) 370 continue; 371 data->status[s->base + i] 372 = _pmbus_read_byte_data(client, i, 373 s->reg); 374 } 375 } 376 377 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) 378 data->status[PB_STATUS_INPUT_BASE] 379 = _pmbus_read_byte_data(client, 0, 380 PMBUS_STATUS_INPUT); 381 382 if (info->func[0] & PMBUS_HAVE_STATUS_VMON) 383 data->status[PB_STATUS_VMON_BASE] 384 = _pmbus_read_byte_data(client, 0, 385 PMBUS_VIRT_STATUS_VMON); 386 387 for (sensor = data->sensors; sensor; sensor = sensor->next) { 388 if (!data->valid || sensor->update) 389 sensor->data 390 = _pmbus_read_word_data(client, 391 sensor->page, 392 sensor->reg); 393 } 394 pmbus_clear_faults(client); 395 data->last_updated = jiffies; 396 data->valid = 1; 397 } 398 mutex_unlock(&data->update_lock); 399 return data; 400 } 401 402 /* 403 * Convert linear sensor values to milli- or micro-units 404 * depending on sensor type. 405 */ 406 static long pmbus_reg2data_linear(struct pmbus_data *data, 407 struct pmbus_sensor *sensor) 408 { 409 s16 exponent; 410 s32 mantissa; 411 long val; 412 413 if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */ 414 exponent = data->exponent[sensor->page]; 415 mantissa = (u16) sensor->data; 416 } else { /* LINEAR11 */ 417 exponent = ((s16)sensor->data) >> 11; 418 mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5; 419 } 420 421 val = mantissa; 422 423 /* scale result to milli-units for all sensors except fans */ 424 if (sensor->class != PSC_FAN) 425 val = val * 1000L; 426 427 /* scale result to micro-units for power sensors */ 428 if (sensor->class == PSC_POWER) 429 val = val * 1000L; 430 431 if (exponent >= 0) 432 val <<= exponent; 433 else 434 val >>= -exponent; 435 436 return val; 437 } 438 439 /* 440 * Convert direct sensor values to milli- or micro-units 441 * depending on sensor type. 442 */ 443 static long pmbus_reg2data_direct(struct pmbus_data *data, 444 struct pmbus_sensor *sensor) 445 { 446 long val = (s16) sensor->data; 447 long m, b, R; 448 449 m = data->info->m[sensor->class]; 450 b = data->info->b[sensor->class]; 451 R = data->info->R[sensor->class]; 452 453 if (m == 0) 454 return 0; 455 456 /* X = 1/m * (Y * 10^-R - b) */ 457 R = -R; 458 /* scale result to milli-units for everything but fans */ 459 if (sensor->class != PSC_FAN) { 460 R += 3; 461 b *= 1000; 462 } 463 464 /* scale result to micro-units for power sensors */ 465 if (sensor->class == PSC_POWER) { 466 R += 3; 467 b *= 1000; 468 } 469 470 while (R > 0) { 471 val *= 10; 472 R--; 473 } 474 while (R < 0) { 475 val = DIV_ROUND_CLOSEST(val, 10); 476 R++; 477 } 478 479 return (val - b) / m; 480 } 481 482 /* 483 * Convert VID sensor values to milli- or micro-units 484 * depending on sensor type. 485 * We currently only support VR11. 486 */ 487 static long pmbus_reg2data_vid(struct pmbus_data *data, 488 struct pmbus_sensor *sensor) 489 { 490 long val = sensor->data; 491 492 if (val < 0x02 || val > 0xb2) 493 return 0; 494 return DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100); 495 } 496 497 static long pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor) 498 { 499 long val; 500 501 switch (data->info->format[sensor->class]) { 502 case direct: 503 val = pmbus_reg2data_direct(data, sensor); 504 break; 505 case vid: 506 val = pmbus_reg2data_vid(data, sensor); 507 break; 508 case linear: 509 default: 510 val = pmbus_reg2data_linear(data, sensor); 511 break; 512 } 513 return val; 514 } 515 516 #define MAX_MANTISSA (1023 * 1000) 517 #define MIN_MANTISSA (511 * 1000) 518 519 static u16 pmbus_data2reg_linear(struct pmbus_data *data, 520 struct pmbus_sensor *sensor, long val) 521 { 522 s16 exponent = 0, mantissa; 523 bool negative = false; 524 525 /* simple case */ 526 if (val == 0) 527 return 0; 528 529 if (sensor->class == PSC_VOLTAGE_OUT) { 530 /* LINEAR16 does not support negative voltages */ 531 if (val < 0) 532 return 0; 533 534 /* 535 * For a static exponents, we don't have a choice 536 * but to adjust the value to it. 537 */ 538 if (data->exponent[sensor->page] < 0) 539 val <<= -data->exponent[sensor->page]; 540 else 541 val >>= data->exponent[sensor->page]; 542 val = DIV_ROUND_CLOSEST(val, 1000); 543 return val & 0xffff; 544 } 545 546 if (val < 0) { 547 negative = true; 548 val = -val; 549 } 550 551 /* Power is in uW. Convert to mW before converting. */ 552 if (sensor->class == PSC_POWER) 553 val = DIV_ROUND_CLOSEST(val, 1000L); 554 555 /* 556 * For simplicity, convert fan data to milli-units 557 * before calculating the exponent. 558 */ 559 if (sensor->class == PSC_FAN) 560 val = val * 1000; 561 562 /* Reduce large mantissa until it fits into 10 bit */ 563 while (val >= MAX_MANTISSA && exponent < 15) { 564 exponent++; 565 val >>= 1; 566 } 567 /* Increase small mantissa to improve precision */ 568 while (val < MIN_MANTISSA && exponent > -15) { 569 exponent--; 570 val <<= 1; 571 } 572 573 /* Convert mantissa from milli-units to units */ 574 mantissa = DIV_ROUND_CLOSEST(val, 1000); 575 576 /* Ensure that resulting number is within range */ 577 if (mantissa > 0x3ff) 578 mantissa = 0x3ff; 579 580 /* restore sign */ 581 if (negative) 582 mantissa = -mantissa; 583 584 /* Convert to 5 bit exponent, 11 bit mantissa */ 585 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800); 586 } 587 588 static u16 pmbus_data2reg_direct(struct pmbus_data *data, 589 struct pmbus_sensor *sensor, long val) 590 { 591 long m, b, R; 592 593 m = data->info->m[sensor->class]; 594 b = data->info->b[sensor->class]; 595 R = data->info->R[sensor->class]; 596 597 /* Power is in uW. Adjust R and b. */ 598 if (sensor->class == PSC_POWER) { 599 R -= 3; 600 b *= 1000; 601 } 602 603 /* Calculate Y = (m * X + b) * 10^R */ 604 if (sensor->class != PSC_FAN) { 605 R -= 3; /* Adjust R and b for data in milli-units */ 606 b *= 1000; 607 } 608 val = val * m + b; 609 610 while (R > 0) { 611 val *= 10; 612 R--; 613 } 614 while (R < 0) { 615 val = DIV_ROUND_CLOSEST(val, 10); 616 R++; 617 } 618 619 return val; 620 } 621 622 static u16 pmbus_data2reg_vid(struct pmbus_data *data, 623 struct pmbus_sensor *sensor, long val) 624 { 625 val = clamp_val(val, 500, 1600); 626 627 return 2 + DIV_ROUND_CLOSEST((1600 - val) * 100, 625); 628 } 629 630 static u16 pmbus_data2reg(struct pmbus_data *data, 631 struct pmbus_sensor *sensor, long val) 632 { 633 u16 regval; 634 635 switch (data->info->format[sensor->class]) { 636 case direct: 637 regval = pmbus_data2reg_direct(data, sensor, val); 638 break; 639 case vid: 640 regval = pmbus_data2reg_vid(data, sensor, val); 641 break; 642 case linear: 643 default: 644 regval = pmbus_data2reg_linear(data, sensor, val); 645 break; 646 } 647 return regval; 648 } 649 650 /* 651 * Return boolean calculated from converted data. 652 * <index> defines a status register index and mask. 653 * The mask is in the lower 8 bits, the register index is in bits 8..23. 654 * 655 * The associated pmbus_boolean structure contains optional pointers to two 656 * sensor attributes. If specified, those attributes are compared against each 657 * other to determine if a limit has been exceeded. 658 * 659 * If the sensor attribute pointers are NULL, the function returns true if 660 * (status[reg] & mask) is true. 661 * 662 * If sensor attribute pointers are provided, a comparison against a specified 663 * limit has to be performed to determine the boolean result. 664 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are 665 * sensor values referenced by sensor attribute pointers s1 and s2). 666 * 667 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>. 668 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>. 669 * 670 * If a negative value is stored in any of the referenced registers, this value 671 * reflects an error code which will be returned. 672 */ 673 static int pmbus_get_boolean(struct pmbus_data *data, struct pmbus_boolean *b, 674 int index) 675 { 676 struct pmbus_sensor *s1 = b->s1; 677 struct pmbus_sensor *s2 = b->s2; 678 u16 reg = (index >> 8) & 0xffff; 679 u8 mask = index & 0xff; 680 int ret, status; 681 u8 regval; 682 683 status = data->status[reg]; 684 if (status < 0) 685 return status; 686 687 regval = status & mask; 688 if (!s1 && !s2) { 689 ret = !!regval; 690 } else if (!s1 || !s2) { 691 WARN(1, "Bad boolean descriptor %p: s1=%p, s2=%p\n", b, s1, s2); 692 return 0; 693 } else { 694 long v1, v2; 695 696 if (s1->data < 0) 697 return s1->data; 698 if (s2->data < 0) 699 return s2->data; 700 701 v1 = pmbus_reg2data(data, s1); 702 v2 = pmbus_reg2data(data, s2); 703 ret = !!(regval && v1 >= v2); 704 } 705 return ret; 706 } 707 708 static ssize_t pmbus_show_boolean(struct device *dev, 709 struct device_attribute *da, char *buf) 710 { 711 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 712 struct pmbus_boolean *boolean = to_pmbus_boolean(attr); 713 struct pmbus_data *data = pmbus_update_device(dev); 714 int val; 715 716 val = pmbus_get_boolean(data, boolean, attr->index); 717 if (val < 0) 718 return val; 719 return snprintf(buf, PAGE_SIZE, "%d\n", val); 720 } 721 722 static ssize_t pmbus_show_sensor(struct device *dev, 723 struct device_attribute *devattr, char *buf) 724 { 725 struct pmbus_data *data = pmbus_update_device(dev); 726 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr); 727 728 if (sensor->data < 0) 729 return sensor->data; 730 731 return snprintf(buf, PAGE_SIZE, "%ld\n", pmbus_reg2data(data, sensor)); 732 } 733 734 static ssize_t pmbus_set_sensor(struct device *dev, 735 struct device_attribute *devattr, 736 const char *buf, size_t count) 737 { 738 struct i2c_client *client = to_i2c_client(dev->parent); 739 struct pmbus_data *data = i2c_get_clientdata(client); 740 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr); 741 ssize_t rv = count; 742 long val = 0; 743 int ret; 744 u16 regval; 745 746 if (kstrtol(buf, 10, &val) < 0) 747 return -EINVAL; 748 749 mutex_lock(&data->update_lock); 750 regval = pmbus_data2reg(data, sensor, val); 751 ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval); 752 if (ret < 0) 753 rv = ret; 754 else 755 sensor->data = regval; 756 mutex_unlock(&data->update_lock); 757 return rv; 758 } 759 760 static ssize_t pmbus_show_label(struct device *dev, 761 struct device_attribute *da, char *buf) 762 { 763 struct pmbus_label *label = to_pmbus_label(da); 764 765 return snprintf(buf, PAGE_SIZE, "%s\n", label->label); 766 } 767 768 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr) 769 { 770 if (data->num_attributes >= data->max_attributes - 1) { 771 int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE; 772 void *new_attrs = krealloc(data->group.attrs, 773 new_max_attrs * sizeof(void *), 774 GFP_KERNEL); 775 if (!new_attrs) 776 return -ENOMEM; 777 data->group.attrs = new_attrs; 778 data->max_attributes = new_max_attrs; 779 } 780 781 data->group.attrs[data->num_attributes++] = attr; 782 data->group.attrs[data->num_attributes] = NULL; 783 return 0; 784 } 785 786 static void pmbus_dev_attr_init(struct device_attribute *dev_attr, 787 const char *name, 788 umode_t mode, 789 ssize_t (*show)(struct device *dev, 790 struct device_attribute *attr, 791 char *buf), 792 ssize_t (*store)(struct device *dev, 793 struct device_attribute *attr, 794 const char *buf, size_t count)) 795 { 796 sysfs_attr_init(&dev_attr->attr); 797 dev_attr->attr.name = name; 798 dev_attr->attr.mode = mode; 799 dev_attr->show = show; 800 dev_attr->store = store; 801 } 802 803 static void pmbus_attr_init(struct sensor_device_attribute *a, 804 const char *name, 805 umode_t mode, 806 ssize_t (*show)(struct device *dev, 807 struct device_attribute *attr, 808 char *buf), 809 ssize_t (*store)(struct device *dev, 810 struct device_attribute *attr, 811 const char *buf, size_t count), 812 int idx) 813 { 814 pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store); 815 a->index = idx; 816 } 817 818 static int pmbus_add_boolean(struct pmbus_data *data, 819 const char *name, const char *type, int seq, 820 struct pmbus_sensor *s1, 821 struct pmbus_sensor *s2, 822 u16 reg, u8 mask) 823 { 824 struct pmbus_boolean *boolean; 825 struct sensor_device_attribute *a; 826 827 boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL); 828 if (!boolean) 829 return -ENOMEM; 830 831 a = &boolean->attribute; 832 833 snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s", 834 name, seq, type); 835 boolean->s1 = s1; 836 boolean->s2 = s2; 837 pmbus_attr_init(a, boolean->name, S_IRUGO, pmbus_show_boolean, NULL, 838 (reg << 8) | mask); 839 840 return pmbus_add_attribute(data, &a->dev_attr.attr); 841 } 842 843 static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data, 844 const char *name, const char *type, 845 int seq, int page, int reg, 846 enum pmbus_sensor_classes class, 847 bool update, bool readonly) 848 { 849 struct pmbus_sensor *sensor; 850 struct device_attribute *a; 851 852 sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL); 853 if (!sensor) 854 return NULL; 855 a = &sensor->attribute; 856 857 snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s", 858 name, seq, type); 859 sensor->page = page; 860 sensor->reg = reg; 861 sensor->class = class; 862 sensor->update = update; 863 pmbus_dev_attr_init(a, sensor->name, 864 readonly ? S_IRUGO : S_IRUGO | S_IWUSR, 865 pmbus_show_sensor, pmbus_set_sensor); 866 867 if (pmbus_add_attribute(data, &a->attr)) 868 return NULL; 869 870 sensor->next = data->sensors; 871 data->sensors = sensor; 872 873 return sensor; 874 } 875 876 static int pmbus_add_label(struct pmbus_data *data, 877 const char *name, int seq, 878 const char *lstring, int index) 879 { 880 struct pmbus_label *label; 881 struct device_attribute *a; 882 883 label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL); 884 if (!label) 885 return -ENOMEM; 886 887 a = &label->attribute; 888 889 snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq); 890 if (!index) 891 strncpy(label->label, lstring, sizeof(label->label) - 1); 892 else 893 snprintf(label->label, sizeof(label->label), "%s%d", lstring, 894 index); 895 896 pmbus_dev_attr_init(a, label->name, S_IRUGO, pmbus_show_label, NULL); 897 return pmbus_add_attribute(data, &a->attr); 898 } 899 900 /* 901 * Search for attributes. Allocate sensors, booleans, and labels as needed. 902 */ 903 904 /* 905 * The pmbus_limit_attr structure describes a single limit attribute 906 * and its associated alarm attribute. 907 */ 908 struct pmbus_limit_attr { 909 u16 reg; /* Limit register */ 910 u16 sbit; /* Alarm attribute status bit */ 911 bool update; /* True if register needs updates */ 912 bool low; /* True if low limit; for limits with compare 913 functions only */ 914 const char *attr; /* Attribute name */ 915 const char *alarm; /* Alarm attribute name */ 916 }; 917 918 /* 919 * The pmbus_sensor_attr structure describes one sensor attribute. This 920 * description includes a reference to the associated limit attributes. 921 */ 922 struct pmbus_sensor_attr { 923 u16 reg; /* sensor register */ 924 u8 gbit; /* generic status bit */ 925 u8 nlimit; /* # of limit registers */ 926 enum pmbus_sensor_classes class;/* sensor class */ 927 const char *label; /* sensor label */ 928 bool paged; /* true if paged sensor */ 929 bool update; /* true if update needed */ 930 bool compare; /* true if compare function needed */ 931 u32 func; /* sensor mask */ 932 u32 sfunc; /* sensor status mask */ 933 int sbase; /* status base register */ 934 const struct pmbus_limit_attr *limit;/* limit registers */ 935 }; 936 937 /* 938 * Add a set of limit attributes and, if supported, the associated 939 * alarm attributes. 940 * returns 0 if no alarm register found, 1 if an alarm register was found, 941 * < 0 on errors. 942 */ 943 static int pmbus_add_limit_attrs(struct i2c_client *client, 944 struct pmbus_data *data, 945 const struct pmbus_driver_info *info, 946 const char *name, int index, int page, 947 struct pmbus_sensor *base, 948 const struct pmbus_sensor_attr *attr) 949 { 950 const struct pmbus_limit_attr *l = attr->limit; 951 int nlimit = attr->nlimit; 952 int have_alarm = 0; 953 int i, ret; 954 struct pmbus_sensor *curr; 955 956 for (i = 0; i < nlimit; i++) { 957 if (pmbus_check_word_register(client, page, l->reg)) { 958 curr = pmbus_add_sensor(data, name, l->attr, index, 959 page, l->reg, attr->class, 960 attr->update || l->update, 961 false); 962 if (!curr) 963 return -ENOMEM; 964 if (l->sbit && (info->func[page] & attr->sfunc)) { 965 ret = pmbus_add_boolean(data, name, 966 l->alarm, index, 967 attr->compare ? l->low ? curr : base 968 : NULL, 969 attr->compare ? l->low ? base : curr 970 : NULL, 971 attr->sbase + page, l->sbit); 972 if (ret) 973 return ret; 974 have_alarm = 1; 975 } 976 } 977 l++; 978 } 979 return have_alarm; 980 } 981 982 static int pmbus_add_sensor_attrs_one(struct i2c_client *client, 983 struct pmbus_data *data, 984 const struct pmbus_driver_info *info, 985 const char *name, 986 int index, int page, 987 const struct pmbus_sensor_attr *attr) 988 { 989 struct pmbus_sensor *base; 990 int ret; 991 992 if (attr->label) { 993 ret = pmbus_add_label(data, name, index, attr->label, 994 attr->paged ? page + 1 : 0); 995 if (ret) 996 return ret; 997 } 998 base = pmbus_add_sensor(data, name, "input", index, page, attr->reg, 999 attr->class, true, true); 1000 if (!base) 1001 return -ENOMEM; 1002 if (attr->sfunc) { 1003 ret = pmbus_add_limit_attrs(client, data, info, name, 1004 index, page, base, attr); 1005 if (ret < 0) 1006 return ret; 1007 /* 1008 * Add generic alarm attribute only if there are no individual 1009 * alarm attributes, if there is a global alarm bit, and if 1010 * the generic status register for this page is accessible. 1011 */ 1012 if (!ret && attr->gbit && 1013 pmbus_check_byte_register(client, page, 1014 data->status_register)) { 1015 ret = pmbus_add_boolean(data, name, "alarm", index, 1016 NULL, NULL, 1017 PB_STATUS_BASE + page, 1018 attr->gbit); 1019 if (ret) 1020 return ret; 1021 } 1022 } 1023 return 0; 1024 } 1025 1026 static int pmbus_add_sensor_attrs(struct i2c_client *client, 1027 struct pmbus_data *data, 1028 const char *name, 1029 const struct pmbus_sensor_attr *attrs, 1030 int nattrs) 1031 { 1032 const struct pmbus_driver_info *info = data->info; 1033 int index, i; 1034 int ret; 1035 1036 index = 1; 1037 for (i = 0; i < nattrs; i++) { 1038 int page, pages; 1039 1040 pages = attrs->paged ? info->pages : 1; 1041 for (page = 0; page < pages; page++) { 1042 if (!(info->func[page] & attrs->func)) 1043 continue; 1044 ret = pmbus_add_sensor_attrs_one(client, data, info, 1045 name, index, page, 1046 attrs); 1047 if (ret) 1048 return ret; 1049 index++; 1050 } 1051 attrs++; 1052 } 1053 return 0; 1054 } 1055 1056 static const struct pmbus_limit_attr vin_limit_attrs[] = { 1057 { 1058 .reg = PMBUS_VIN_UV_WARN_LIMIT, 1059 .attr = "min", 1060 .alarm = "min_alarm", 1061 .sbit = PB_VOLTAGE_UV_WARNING, 1062 }, { 1063 .reg = PMBUS_VIN_UV_FAULT_LIMIT, 1064 .attr = "lcrit", 1065 .alarm = "lcrit_alarm", 1066 .sbit = PB_VOLTAGE_UV_FAULT, 1067 }, { 1068 .reg = PMBUS_VIN_OV_WARN_LIMIT, 1069 .attr = "max", 1070 .alarm = "max_alarm", 1071 .sbit = PB_VOLTAGE_OV_WARNING, 1072 }, { 1073 .reg = PMBUS_VIN_OV_FAULT_LIMIT, 1074 .attr = "crit", 1075 .alarm = "crit_alarm", 1076 .sbit = PB_VOLTAGE_OV_FAULT, 1077 }, { 1078 .reg = PMBUS_VIRT_READ_VIN_AVG, 1079 .update = true, 1080 .attr = "average", 1081 }, { 1082 .reg = PMBUS_VIRT_READ_VIN_MIN, 1083 .update = true, 1084 .attr = "lowest", 1085 }, { 1086 .reg = PMBUS_VIRT_READ_VIN_MAX, 1087 .update = true, 1088 .attr = "highest", 1089 }, { 1090 .reg = PMBUS_VIRT_RESET_VIN_HISTORY, 1091 .attr = "reset_history", 1092 }, 1093 }; 1094 1095 static const struct pmbus_limit_attr vmon_limit_attrs[] = { 1096 { 1097 .reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT, 1098 .attr = "min", 1099 .alarm = "min_alarm", 1100 .sbit = PB_VOLTAGE_UV_WARNING, 1101 }, { 1102 .reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT, 1103 .attr = "lcrit", 1104 .alarm = "lcrit_alarm", 1105 .sbit = PB_VOLTAGE_UV_FAULT, 1106 }, { 1107 .reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT, 1108 .attr = "max", 1109 .alarm = "max_alarm", 1110 .sbit = PB_VOLTAGE_OV_WARNING, 1111 }, { 1112 .reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT, 1113 .attr = "crit", 1114 .alarm = "crit_alarm", 1115 .sbit = PB_VOLTAGE_OV_FAULT, 1116 } 1117 }; 1118 1119 static const struct pmbus_limit_attr vout_limit_attrs[] = { 1120 { 1121 .reg = PMBUS_VOUT_UV_WARN_LIMIT, 1122 .attr = "min", 1123 .alarm = "min_alarm", 1124 .sbit = PB_VOLTAGE_UV_WARNING, 1125 }, { 1126 .reg = PMBUS_VOUT_UV_FAULT_LIMIT, 1127 .attr = "lcrit", 1128 .alarm = "lcrit_alarm", 1129 .sbit = PB_VOLTAGE_UV_FAULT, 1130 }, { 1131 .reg = PMBUS_VOUT_OV_WARN_LIMIT, 1132 .attr = "max", 1133 .alarm = "max_alarm", 1134 .sbit = PB_VOLTAGE_OV_WARNING, 1135 }, { 1136 .reg = PMBUS_VOUT_OV_FAULT_LIMIT, 1137 .attr = "crit", 1138 .alarm = "crit_alarm", 1139 .sbit = PB_VOLTAGE_OV_FAULT, 1140 }, { 1141 .reg = PMBUS_VIRT_READ_VOUT_AVG, 1142 .update = true, 1143 .attr = "average", 1144 }, { 1145 .reg = PMBUS_VIRT_READ_VOUT_MIN, 1146 .update = true, 1147 .attr = "lowest", 1148 }, { 1149 .reg = PMBUS_VIRT_READ_VOUT_MAX, 1150 .update = true, 1151 .attr = "highest", 1152 }, { 1153 .reg = PMBUS_VIRT_RESET_VOUT_HISTORY, 1154 .attr = "reset_history", 1155 } 1156 }; 1157 1158 static const struct pmbus_sensor_attr voltage_attributes[] = { 1159 { 1160 .reg = PMBUS_READ_VIN, 1161 .class = PSC_VOLTAGE_IN, 1162 .label = "vin", 1163 .func = PMBUS_HAVE_VIN, 1164 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1165 .sbase = PB_STATUS_INPUT_BASE, 1166 .gbit = PB_STATUS_VIN_UV, 1167 .limit = vin_limit_attrs, 1168 .nlimit = ARRAY_SIZE(vin_limit_attrs), 1169 }, { 1170 .reg = PMBUS_VIRT_READ_VMON, 1171 .class = PSC_VOLTAGE_IN, 1172 .label = "vmon", 1173 .func = PMBUS_HAVE_VMON, 1174 .sfunc = PMBUS_HAVE_STATUS_VMON, 1175 .sbase = PB_STATUS_VMON_BASE, 1176 .limit = vmon_limit_attrs, 1177 .nlimit = ARRAY_SIZE(vmon_limit_attrs), 1178 }, { 1179 .reg = PMBUS_READ_VCAP, 1180 .class = PSC_VOLTAGE_IN, 1181 .label = "vcap", 1182 .func = PMBUS_HAVE_VCAP, 1183 }, { 1184 .reg = PMBUS_READ_VOUT, 1185 .class = PSC_VOLTAGE_OUT, 1186 .label = "vout", 1187 .paged = true, 1188 .func = PMBUS_HAVE_VOUT, 1189 .sfunc = PMBUS_HAVE_STATUS_VOUT, 1190 .sbase = PB_STATUS_VOUT_BASE, 1191 .gbit = PB_STATUS_VOUT_OV, 1192 .limit = vout_limit_attrs, 1193 .nlimit = ARRAY_SIZE(vout_limit_attrs), 1194 } 1195 }; 1196 1197 /* Current attributes */ 1198 1199 static const struct pmbus_limit_attr iin_limit_attrs[] = { 1200 { 1201 .reg = PMBUS_IIN_OC_WARN_LIMIT, 1202 .attr = "max", 1203 .alarm = "max_alarm", 1204 .sbit = PB_IIN_OC_WARNING, 1205 }, { 1206 .reg = PMBUS_IIN_OC_FAULT_LIMIT, 1207 .attr = "crit", 1208 .alarm = "crit_alarm", 1209 .sbit = PB_IIN_OC_FAULT, 1210 }, { 1211 .reg = PMBUS_VIRT_READ_IIN_AVG, 1212 .update = true, 1213 .attr = "average", 1214 }, { 1215 .reg = PMBUS_VIRT_READ_IIN_MIN, 1216 .update = true, 1217 .attr = "lowest", 1218 }, { 1219 .reg = PMBUS_VIRT_READ_IIN_MAX, 1220 .update = true, 1221 .attr = "highest", 1222 }, { 1223 .reg = PMBUS_VIRT_RESET_IIN_HISTORY, 1224 .attr = "reset_history", 1225 } 1226 }; 1227 1228 static const struct pmbus_limit_attr iout_limit_attrs[] = { 1229 { 1230 .reg = PMBUS_IOUT_OC_WARN_LIMIT, 1231 .attr = "max", 1232 .alarm = "max_alarm", 1233 .sbit = PB_IOUT_OC_WARNING, 1234 }, { 1235 .reg = PMBUS_IOUT_UC_FAULT_LIMIT, 1236 .attr = "lcrit", 1237 .alarm = "lcrit_alarm", 1238 .sbit = PB_IOUT_UC_FAULT, 1239 }, { 1240 .reg = PMBUS_IOUT_OC_FAULT_LIMIT, 1241 .attr = "crit", 1242 .alarm = "crit_alarm", 1243 .sbit = PB_IOUT_OC_FAULT, 1244 }, { 1245 .reg = PMBUS_VIRT_READ_IOUT_AVG, 1246 .update = true, 1247 .attr = "average", 1248 }, { 1249 .reg = PMBUS_VIRT_READ_IOUT_MIN, 1250 .update = true, 1251 .attr = "lowest", 1252 }, { 1253 .reg = PMBUS_VIRT_READ_IOUT_MAX, 1254 .update = true, 1255 .attr = "highest", 1256 }, { 1257 .reg = PMBUS_VIRT_RESET_IOUT_HISTORY, 1258 .attr = "reset_history", 1259 } 1260 }; 1261 1262 static const struct pmbus_sensor_attr current_attributes[] = { 1263 { 1264 .reg = PMBUS_READ_IIN, 1265 .class = PSC_CURRENT_IN, 1266 .label = "iin", 1267 .func = PMBUS_HAVE_IIN, 1268 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1269 .sbase = PB_STATUS_INPUT_BASE, 1270 .limit = iin_limit_attrs, 1271 .nlimit = ARRAY_SIZE(iin_limit_attrs), 1272 }, { 1273 .reg = PMBUS_READ_IOUT, 1274 .class = PSC_CURRENT_OUT, 1275 .label = "iout", 1276 .paged = true, 1277 .func = PMBUS_HAVE_IOUT, 1278 .sfunc = PMBUS_HAVE_STATUS_IOUT, 1279 .sbase = PB_STATUS_IOUT_BASE, 1280 .gbit = PB_STATUS_IOUT_OC, 1281 .limit = iout_limit_attrs, 1282 .nlimit = ARRAY_SIZE(iout_limit_attrs), 1283 } 1284 }; 1285 1286 /* Power attributes */ 1287 1288 static const struct pmbus_limit_attr pin_limit_attrs[] = { 1289 { 1290 .reg = PMBUS_PIN_OP_WARN_LIMIT, 1291 .attr = "max", 1292 .alarm = "alarm", 1293 .sbit = PB_PIN_OP_WARNING, 1294 }, { 1295 .reg = PMBUS_VIRT_READ_PIN_AVG, 1296 .update = true, 1297 .attr = "average", 1298 }, { 1299 .reg = PMBUS_VIRT_READ_PIN_MAX, 1300 .update = true, 1301 .attr = "input_highest", 1302 }, { 1303 .reg = PMBUS_VIRT_RESET_PIN_HISTORY, 1304 .attr = "reset_history", 1305 } 1306 }; 1307 1308 static const struct pmbus_limit_attr pout_limit_attrs[] = { 1309 { 1310 .reg = PMBUS_POUT_MAX, 1311 .attr = "cap", 1312 .alarm = "cap_alarm", 1313 .sbit = PB_POWER_LIMITING, 1314 }, { 1315 .reg = PMBUS_POUT_OP_WARN_LIMIT, 1316 .attr = "max", 1317 .alarm = "max_alarm", 1318 .sbit = PB_POUT_OP_WARNING, 1319 }, { 1320 .reg = PMBUS_POUT_OP_FAULT_LIMIT, 1321 .attr = "crit", 1322 .alarm = "crit_alarm", 1323 .sbit = PB_POUT_OP_FAULT, 1324 }, { 1325 .reg = PMBUS_VIRT_READ_POUT_AVG, 1326 .update = true, 1327 .attr = "average", 1328 }, { 1329 .reg = PMBUS_VIRT_READ_POUT_MAX, 1330 .update = true, 1331 .attr = "input_highest", 1332 }, { 1333 .reg = PMBUS_VIRT_RESET_POUT_HISTORY, 1334 .attr = "reset_history", 1335 } 1336 }; 1337 1338 static const struct pmbus_sensor_attr power_attributes[] = { 1339 { 1340 .reg = PMBUS_READ_PIN, 1341 .class = PSC_POWER, 1342 .label = "pin", 1343 .func = PMBUS_HAVE_PIN, 1344 .sfunc = PMBUS_HAVE_STATUS_INPUT, 1345 .sbase = PB_STATUS_INPUT_BASE, 1346 .limit = pin_limit_attrs, 1347 .nlimit = ARRAY_SIZE(pin_limit_attrs), 1348 }, { 1349 .reg = PMBUS_READ_POUT, 1350 .class = PSC_POWER, 1351 .label = "pout", 1352 .paged = true, 1353 .func = PMBUS_HAVE_POUT, 1354 .sfunc = PMBUS_HAVE_STATUS_IOUT, 1355 .sbase = PB_STATUS_IOUT_BASE, 1356 .limit = pout_limit_attrs, 1357 .nlimit = ARRAY_SIZE(pout_limit_attrs), 1358 } 1359 }; 1360 1361 /* Temperature atributes */ 1362 1363 static const struct pmbus_limit_attr temp_limit_attrs[] = { 1364 { 1365 .reg = PMBUS_UT_WARN_LIMIT, 1366 .low = true, 1367 .attr = "min", 1368 .alarm = "min_alarm", 1369 .sbit = PB_TEMP_UT_WARNING, 1370 }, { 1371 .reg = PMBUS_UT_FAULT_LIMIT, 1372 .low = true, 1373 .attr = "lcrit", 1374 .alarm = "lcrit_alarm", 1375 .sbit = PB_TEMP_UT_FAULT, 1376 }, { 1377 .reg = PMBUS_OT_WARN_LIMIT, 1378 .attr = "max", 1379 .alarm = "max_alarm", 1380 .sbit = PB_TEMP_OT_WARNING, 1381 }, { 1382 .reg = PMBUS_OT_FAULT_LIMIT, 1383 .attr = "crit", 1384 .alarm = "crit_alarm", 1385 .sbit = PB_TEMP_OT_FAULT, 1386 }, { 1387 .reg = PMBUS_VIRT_READ_TEMP_MIN, 1388 .attr = "lowest", 1389 }, { 1390 .reg = PMBUS_VIRT_READ_TEMP_AVG, 1391 .attr = "average", 1392 }, { 1393 .reg = PMBUS_VIRT_READ_TEMP_MAX, 1394 .attr = "highest", 1395 }, { 1396 .reg = PMBUS_VIRT_RESET_TEMP_HISTORY, 1397 .attr = "reset_history", 1398 } 1399 }; 1400 1401 static const struct pmbus_limit_attr temp_limit_attrs2[] = { 1402 { 1403 .reg = PMBUS_UT_WARN_LIMIT, 1404 .low = true, 1405 .attr = "min", 1406 .alarm = "min_alarm", 1407 .sbit = PB_TEMP_UT_WARNING, 1408 }, { 1409 .reg = PMBUS_UT_FAULT_LIMIT, 1410 .low = true, 1411 .attr = "lcrit", 1412 .alarm = "lcrit_alarm", 1413 .sbit = PB_TEMP_UT_FAULT, 1414 }, { 1415 .reg = PMBUS_OT_WARN_LIMIT, 1416 .attr = "max", 1417 .alarm = "max_alarm", 1418 .sbit = PB_TEMP_OT_WARNING, 1419 }, { 1420 .reg = PMBUS_OT_FAULT_LIMIT, 1421 .attr = "crit", 1422 .alarm = "crit_alarm", 1423 .sbit = PB_TEMP_OT_FAULT, 1424 }, { 1425 .reg = PMBUS_VIRT_READ_TEMP2_MIN, 1426 .attr = "lowest", 1427 }, { 1428 .reg = PMBUS_VIRT_READ_TEMP2_AVG, 1429 .attr = "average", 1430 }, { 1431 .reg = PMBUS_VIRT_READ_TEMP2_MAX, 1432 .attr = "highest", 1433 }, { 1434 .reg = PMBUS_VIRT_RESET_TEMP2_HISTORY, 1435 .attr = "reset_history", 1436 } 1437 }; 1438 1439 static const struct pmbus_limit_attr temp_limit_attrs3[] = { 1440 { 1441 .reg = PMBUS_UT_WARN_LIMIT, 1442 .low = true, 1443 .attr = "min", 1444 .alarm = "min_alarm", 1445 .sbit = PB_TEMP_UT_WARNING, 1446 }, { 1447 .reg = PMBUS_UT_FAULT_LIMIT, 1448 .low = true, 1449 .attr = "lcrit", 1450 .alarm = "lcrit_alarm", 1451 .sbit = PB_TEMP_UT_FAULT, 1452 }, { 1453 .reg = PMBUS_OT_WARN_LIMIT, 1454 .attr = "max", 1455 .alarm = "max_alarm", 1456 .sbit = PB_TEMP_OT_WARNING, 1457 }, { 1458 .reg = PMBUS_OT_FAULT_LIMIT, 1459 .attr = "crit", 1460 .alarm = "crit_alarm", 1461 .sbit = PB_TEMP_OT_FAULT, 1462 } 1463 }; 1464 1465 static const struct pmbus_sensor_attr temp_attributes[] = { 1466 { 1467 .reg = PMBUS_READ_TEMPERATURE_1, 1468 .class = PSC_TEMPERATURE, 1469 .paged = true, 1470 .update = true, 1471 .compare = true, 1472 .func = PMBUS_HAVE_TEMP, 1473 .sfunc = PMBUS_HAVE_STATUS_TEMP, 1474 .sbase = PB_STATUS_TEMP_BASE, 1475 .gbit = PB_STATUS_TEMPERATURE, 1476 .limit = temp_limit_attrs, 1477 .nlimit = ARRAY_SIZE(temp_limit_attrs), 1478 }, { 1479 .reg = PMBUS_READ_TEMPERATURE_2, 1480 .class = PSC_TEMPERATURE, 1481 .paged = true, 1482 .update = true, 1483 .compare = true, 1484 .func = PMBUS_HAVE_TEMP2, 1485 .sfunc = PMBUS_HAVE_STATUS_TEMP, 1486 .sbase = PB_STATUS_TEMP_BASE, 1487 .gbit = PB_STATUS_TEMPERATURE, 1488 .limit = temp_limit_attrs2, 1489 .nlimit = ARRAY_SIZE(temp_limit_attrs2), 1490 }, { 1491 .reg = PMBUS_READ_TEMPERATURE_3, 1492 .class = PSC_TEMPERATURE, 1493 .paged = true, 1494 .update = true, 1495 .compare = true, 1496 .func = PMBUS_HAVE_TEMP3, 1497 .sfunc = PMBUS_HAVE_STATUS_TEMP, 1498 .sbase = PB_STATUS_TEMP_BASE, 1499 .gbit = PB_STATUS_TEMPERATURE, 1500 .limit = temp_limit_attrs3, 1501 .nlimit = ARRAY_SIZE(temp_limit_attrs3), 1502 } 1503 }; 1504 1505 static const int pmbus_fan_registers[] = { 1506 PMBUS_READ_FAN_SPEED_1, 1507 PMBUS_READ_FAN_SPEED_2, 1508 PMBUS_READ_FAN_SPEED_3, 1509 PMBUS_READ_FAN_SPEED_4 1510 }; 1511 1512 static const int pmbus_fan_config_registers[] = { 1513 PMBUS_FAN_CONFIG_12, 1514 PMBUS_FAN_CONFIG_12, 1515 PMBUS_FAN_CONFIG_34, 1516 PMBUS_FAN_CONFIG_34 1517 }; 1518 1519 static const int pmbus_fan_status_registers[] = { 1520 PMBUS_STATUS_FAN_12, 1521 PMBUS_STATUS_FAN_12, 1522 PMBUS_STATUS_FAN_34, 1523 PMBUS_STATUS_FAN_34 1524 }; 1525 1526 static const u32 pmbus_fan_flags[] = { 1527 PMBUS_HAVE_FAN12, 1528 PMBUS_HAVE_FAN12, 1529 PMBUS_HAVE_FAN34, 1530 PMBUS_HAVE_FAN34 1531 }; 1532 1533 static const u32 pmbus_fan_status_flags[] = { 1534 PMBUS_HAVE_STATUS_FAN12, 1535 PMBUS_HAVE_STATUS_FAN12, 1536 PMBUS_HAVE_STATUS_FAN34, 1537 PMBUS_HAVE_STATUS_FAN34 1538 }; 1539 1540 /* Fans */ 1541 static int pmbus_add_fan_attributes(struct i2c_client *client, 1542 struct pmbus_data *data) 1543 { 1544 const struct pmbus_driver_info *info = data->info; 1545 int index = 1; 1546 int page; 1547 int ret; 1548 1549 for (page = 0; page < info->pages; page++) { 1550 int f; 1551 1552 for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) { 1553 int regval; 1554 1555 if (!(info->func[page] & pmbus_fan_flags[f])) 1556 break; 1557 1558 if (!pmbus_check_word_register(client, page, 1559 pmbus_fan_registers[f])) 1560 break; 1561 1562 /* 1563 * Skip fan if not installed. 1564 * Each fan configuration register covers multiple fans, 1565 * so we have to do some magic. 1566 */ 1567 regval = _pmbus_read_byte_data(client, page, 1568 pmbus_fan_config_registers[f]); 1569 if (regval < 0 || 1570 (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4))))) 1571 continue; 1572 1573 if (pmbus_add_sensor(data, "fan", "input", index, 1574 page, pmbus_fan_registers[f], 1575 PSC_FAN, true, true) == NULL) 1576 return -ENOMEM; 1577 1578 /* 1579 * Each fan status register covers multiple fans, 1580 * so we have to do some magic. 1581 */ 1582 if ((info->func[page] & pmbus_fan_status_flags[f]) && 1583 pmbus_check_byte_register(client, 1584 page, pmbus_fan_status_registers[f])) { 1585 int base; 1586 1587 if (f > 1) /* fan 3, 4 */ 1588 base = PB_STATUS_FAN34_BASE + page; 1589 else 1590 base = PB_STATUS_FAN_BASE + page; 1591 ret = pmbus_add_boolean(data, "fan", 1592 "alarm", index, NULL, NULL, base, 1593 PB_FAN_FAN1_WARNING >> (f & 1)); 1594 if (ret) 1595 return ret; 1596 ret = pmbus_add_boolean(data, "fan", 1597 "fault", index, NULL, NULL, base, 1598 PB_FAN_FAN1_FAULT >> (f & 1)); 1599 if (ret) 1600 return ret; 1601 } 1602 index++; 1603 } 1604 } 1605 return 0; 1606 } 1607 1608 static int pmbus_find_attributes(struct i2c_client *client, 1609 struct pmbus_data *data) 1610 { 1611 int ret; 1612 1613 /* Voltage sensors */ 1614 ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes, 1615 ARRAY_SIZE(voltage_attributes)); 1616 if (ret) 1617 return ret; 1618 1619 /* Current sensors */ 1620 ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes, 1621 ARRAY_SIZE(current_attributes)); 1622 if (ret) 1623 return ret; 1624 1625 /* Power sensors */ 1626 ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes, 1627 ARRAY_SIZE(power_attributes)); 1628 if (ret) 1629 return ret; 1630 1631 /* Temperature sensors */ 1632 ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes, 1633 ARRAY_SIZE(temp_attributes)); 1634 if (ret) 1635 return ret; 1636 1637 /* Fans */ 1638 ret = pmbus_add_fan_attributes(client, data); 1639 return ret; 1640 } 1641 1642 /* 1643 * Identify chip parameters. 1644 * This function is called for all chips. 1645 */ 1646 static int pmbus_identify_common(struct i2c_client *client, 1647 struct pmbus_data *data, int page) 1648 { 1649 int vout_mode = -1; 1650 1651 if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE)) 1652 vout_mode = _pmbus_read_byte_data(client, page, 1653 PMBUS_VOUT_MODE); 1654 if (vout_mode >= 0 && vout_mode != 0xff) { 1655 /* 1656 * Not all chips support the VOUT_MODE command, 1657 * so a failure to read it is not an error. 1658 */ 1659 switch (vout_mode >> 5) { 1660 case 0: /* linear mode */ 1661 if (data->info->format[PSC_VOLTAGE_OUT] != linear) 1662 return -ENODEV; 1663 1664 data->exponent[page] = ((s8)(vout_mode << 3)) >> 3; 1665 break; 1666 case 1: /* VID mode */ 1667 if (data->info->format[PSC_VOLTAGE_OUT] != vid) 1668 return -ENODEV; 1669 break; 1670 case 2: /* direct mode */ 1671 if (data->info->format[PSC_VOLTAGE_OUT] != direct) 1672 return -ENODEV; 1673 break; 1674 default: 1675 return -ENODEV; 1676 } 1677 } 1678 1679 pmbus_clear_fault_page(client, page); 1680 return 0; 1681 } 1682 1683 static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data, 1684 struct pmbus_driver_info *info) 1685 { 1686 struct device *dev = &client->dev; 1687 int page, ret; 1688 1689 /* 1690 * Some PMBus chips don't support PMBUS_STATUS_BYTE, so try 1691 * to use PMBUS_STATUS_WORD instead if that is the case. 1692 * Bail out if both registers are not supported. 1693 */ 1694 data->status_register = PMBUS_STATUS_BYTE; 1695 ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE); 1696 if (ret < 0 || ret == 0xff) { 1697 data->status_register = PMBUS_STATUS_WORD; 1698 ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD); 1699 if (ret < 0 || ret == 0xffff) { 1700 dev_err(dev, "PMBus status register not found\n"); 1701 return -ENODEV; 1702 } 1703 } 1704 1705 pmbus_clear_faults(client); 1706 1707 if (info->identify) { 1708 ret = (*info->identify)(client, info); 1709 if (ret < 0) { 1710 dev_err(dev, "Chip identification failed\n"); 1711 return ret; 1712 } 1713 } 1714 1715 if (info->pages <= 0 || info->pages > PMBUS_PAGES) { 1716 dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages); 1717 return -ENODEV; 1718 } 1719 1720 for (page = 0; page < info->pages; page++) { 1721 ret = pmbus_identify_common(client, data, page); 1722 if (ret < 0) { 1723 dev_err(dev, "Failed to identify chip capabilities\n"); 1724 return ret; 1725 } 1726 } 1727 return 0; 1728 } 1729 1730 int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id, 1731 struct pmbus_driver_info *info) 1732 { 1733 struct device *dev = &client->dev; 1734 const struct pmbus_platform_data *pdata = dev_get_platdata(dev); 1735 struct pmbus_data *data; 1736 int ret; 1737 1738 if (!info) 1739 return -ENODEV; 1740 1741 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE 1742 | I2C_FUNC_SMBUS_BYTE_DATA 1743 | I2C_FUNC_SMBUS_WORD_DATA)) 1744 return -ENODEV; 1745 1746 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 1747 if (!data) 1748 return -ENOMEM; 1749 1750 i2c_set_clientdata(client, data); 1751 mutex_init(&data->update_lock); 1752 data->dev = dev; 1753 1754 if (pdata) 1755 data->flags = pdata->flags; 1756 data->info = info; 1757 1758 ret = pmbus_init_common(client, data, info); 1759 if (ret < 0) 1760 return ret; 1761 1762 ret = pmbus_find_attributes(client, data); 1763 if (ret) 1764 goto out_kfree; 1765 1766 /* 1767 * If there are no attributes, something is wrong. 1768 * Bail out instead of trying to register nothing. 1769 */ 1770 if (!data->num_attributes) { 1771 dev_err(dev, "No attributes found\n"); 1772 ret = -ENODEV; 1773 goto out_kfree; 1774 } 1775 1776 data->groups[0] = &data->group; 1777 data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name, 1778 data, data->groups); 1779 if (IS_ERR(data->hwmon_dev)) { 1780 ret = PTR_ERR(data->hwmon_dev); 1781 dev_err(dev, "Failed to register hwmon device\n"); 1782 goto out_kfree; 1783 } 1784 return 0; 1785 1786 out_kfree: 1787 kfree(data->group.attrs); 1788 return ret; 1789 } 1790 EXPORT_SYMBOL_GPL(pmbus_do_probe); 1791 1792 int pmbus_do_remove(struct i2c_client *client) 1793 { 1794 struct pmbus_data *data = i2c_get_clientdata(client); 1795 hwmon_device_unregister(data->hwmon_dev); 1796 kfree(data->group.attrs); 1797 return 0; 1798 } 1799 EXPORT_SYMBOL_GPL(pmbus_do_remove); 1800 1801 MODULE_AUTHOR("Guenter Roeck"); 1802 MODULE_DESCRIPTION("PMBus core driver"); 1803 MODULE_LICENSE("GPL"); 1804