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