linux/drivers/hwmon/pmbus/ltc2978.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Hardware monitoring driver for LTC2978 and compatible chips.
   4 *
   5 * Copyright (c) 2011 Ericsson AB.
   6 * Copyright (c) 2013, 2014, 2015 Guenter Roeck
   7 * Copyright (c) 2015 Linear Technology
   8 * Copyright (c) 2018 Analog Devices Inc.
   9 */
  10
  11#include <linux/delay.h>
  12#include <linux/jiffies.h>
  13#include <linux/kernel.h>
  14#include <linux/module.h>
  15#include <linux/init.h>
  16#include <linux/err.h>
  17#include <linux/slab.h>
  18#include <linux/i2c.h>
  19#include <linux/regulator/driver.h>
  20#include "pmbus.h"
  21
  22enum chips {
  23        /* Managers */
  24        ltc2972, ltc2974, ltc2975, ltc2977, ltc2978, ltc2979, ltc2980,
  25        /* Controllers */
  26        ltc3880, ltc3882, ltc3883, ltc3884, ltc3886, ltc3887, ltc3889, ltc7880,
  27        /* Modules */
  28        ltm2987, ltm4664, ltm4675, ltm4676, ltm4677, ltm4678, ltm4680, ltm4686,
  29        ltm4700,
  30};
  31
  32/* Common for all chips */
  33#define LTC2978_MFR_VOUT_PEAK           0xdd
  34#define LTC2978_MFR_VIN_PEAK            0xde
  35#define LTC2978_MFR_TEMPERATURE_PEAK    0xdf
  36#define LTC2978_MFR_SPECIAL_ID          0xe7    /* Undocumented on LTC3882 */
  37#define LTC2978_MFR_COMMON              0xef
  38
  39/* LTC2974, LTC2975, LCT2977, LTC2980, LTC2978, and LTM2987 */
  40#define LTC2978_MFR_VOUT_MIN            0xfb
  41#define LTC2978_MFR_VIN_MIN             0xfc
  42#define LTC2978_MFR_TEMPERATURE_MIN     0xfd
  43
  44/* LTC2974, LTC2975 */
  45#define LTC2974_MFR_IOUT_PEAK           0xd7
  46#define LTC2974_MFR_IOUT_MIN            0xd8
  47
  48/* LTC3880, LTC3882, LTC3883, LTC3887, LTM4675, and LTM4676 */
  49#define LTC3880_MFR_IOUT_PEAK           0xd7
  50#define LTC3880_MFR_CLEAR_PEAKS         0xe3
  51#define LTC3880_MFR_TEMPERATURE2_PEAK   0xf4
  52
  53/* LTC3883, LTC3884, LTC3886, LTC3889 and LTC7880 only */
  54#define LTC3883_MFR_IIN_PEAK            0xe1
  55
  56
  57/* LTC2975 only */
  58#define LTC2975_MFR_IIN_PEAK            0xc4
  59#define LTC2975_MFR_IIN_MIN             0xc5
  60#define LTC2975_MFR_PIN_PEAK            0xc6
  61#define LTC2975_MFR_PIN_MIN             0xc7
  62
  63#define LTC2978_ID_MASK                 0xfff0
  64
  65#define LTC2972_ID                      0x0310
  66#define LTC2974_ID                      0x0210
  67#define LTC2975_ID                      0x0220
  68#define LTC2977_ID                      0x0130
  69#define LTC2978_ID_REV1                 0x0110  /* Early revision */
  70#define LTC2978_ID_REV2                 0x0120
  71#define LTC2979_ID_A                    0x8060
  72#define LTC2979_ID_B                    0x8070
  73#define LTC2980_ID_A                    0x8030  /* A/B for two die IDs */
  74#define LTC2980_ID_B                    0x8040
  75#define LTC3880_ID                      0x4020
  76#define LTC3882_ID                      0x4200
  77#define LTC3882_ID_D1                   0x4240  /* Dash 1 */
  78#define LTC3883_ID                      0x4300
  79#define LTC3884_ID                      0x4C00
  80#define LTC3886_ID                      0x4600
  81#define LTC3887_ID                      0x4700
  82#define LTM2987_ID_A                    0x8010  /* A/B for two die IDs */
  83#define LTM2987_ID_B                    0x8020
  84#define LTC3889_ID                      0x4900
  85#define LTC7880_ID                      0x49E0
  86#define LTM4664_ID                      0x4120
  87#define LTM4675_ID                      0x47a0
  88#define LTM4676_ID_REV1                 0x4400
  89#define LTM4676_ID_REV2                 0x4480
  90#define LTM4676A_ID                     0x47e0
  91#define LTM4677_ID_REV1                 0x47B0
  92#define LTM4677_ID_REV2                 0x47D0
  93#define LTM4678_ID_REV1                 0x4100
  94#define LTM4678_ID_REV2                 0x4110
  95#define LTM4680_ID                      0x4140
  96#define LTM4686_ID                      0x4770
  97#define LTM4700_ID                      0x4130
  98
  99#define LTC2972_NUM_PAGES               2
 100#define LTC2974_NUM_PAGES               4
 101#define LTC2978_NUM_PAGES               8
 102#define LTC3880_NUM_PAGES               2
 103#define LTC3883_NUM_PAGES               1
 104
 105#define LTC_POLL_TIMEOUT                100     /* in milli-seconds */
 106
 107#define LTC_NOT_BUSY                    BIT(6)
 108#define LTC_NOT_PENDING                 BIT(5)
 109
 110/*
 111 * LTC2978 clears peak data whenever the CLEAR_FAULTS command is executed, which
 112 * happens pretty much each time chip data is updated. Raw peak data therefore
 113 * does not provide much value. To be able to provide useful peak data, keep an
 114 * internal cache of measured peak data, which is only cleared if an explicit
 115 * "clear peak" command is executed for the sensor in question.
 116 */
 117
 118struct ltc2978_data {
 119        enum chips id;
 120        u16 vin_min, vin_max;
 121        u16 temp_min[LTC2974_NUM_PAGES], temp_max[LTC2974_NUM_PAGES];
 122        u16 vout_min[LTC2978_NUM_PAGES], vout_max[LTC2978_NUM_PAGES];
 123        u16 iout_min[LTC2974_NUM_PAGES], iout_max[LTC2974_NUM_PAGES];
 124        u16 iin_min, iin_max;
 125        u16 pin_min, pin_max;
 126        u16 temp2_max;
 127        struct pmbus_driver_info info;
 128        u32 features;
 129};
 130#define to_ltc2978_data(x)  container_of(x, struct ltc2978_data, info)
 131
 132#define FEAT_CLEAR_PEAKS        BIT(0)
 133#define FEAT_NEEDS_POLLING      BIT(1)
 134
 135#define has_clear_peaks(d)      ((d)->features & FEAT_CLEAR_PEAKS)
 136#define needs_polling(d)        ((d)->features & FEAT_NEEDS_POLLING)
 137
 138static int ltc_wait_ready(struct i2c_client *client)
 139{
 140        unsigned long timeout = jiffies + msecs_to_jiffies(LTC_POLL_TIMEOUT);
 141        const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
 142        struct ltc2978_data *data = to_ltc2978_data(info);
 143        int status;
 144        u8 mask;
 145
 146        if (!needs_polling(data))
 147                return 0;
 148
 149        /*
 150         * LTC3883 does not support LTC_NOT_PENDING, even though
 151         * the datasheet claims that it does.
 152         */
 153        mask = LTC_NOT_BUSY;
 154        if (data->id != ltc3883)
 155                mask |= LTC_NOT_PENDING;
 156
 157        do {
 158                status = pmbus_read_byte_data(client, 0, LTC2978_MFR_COMMON);
 159                if (status == -EBADMSG || status == -ENXIO) {
 160                        /* PEC error or NACK: chip may be busy, try again */
 161                        usleep_range(50, 100);
 162                        continue;
 163                }
 164                if (status < 0)
 165                        return status;
 166
 167                if ((status & mask) == mask)
 168                        return 0;
 169
 170                usleep_range(50, 100);
 171        } while (time_before(jiffies, timeout));
 172
 173        return -ETIMEDOUT;
 174}
 175
 176static int ltc_read_word_data(struct i2c_client *client, int page, int phase,
 177                              int reg)
 178{
 179        int ret;
 180
 181        ret = ltc_wait_ready(client);
 182        if (ret < 0)
 183                return ret;
 184
 185        return pmbus_read_word_data(client, page, 0xff, reg);
 186}
 187
 188static int ltc_read_byte_data(struct i2c_client *client, int page, int reg)
 189{
 190        int ret;
 191
 192        ret = ltc_wait_ready(client);
 193        if (ret < 0)
 194                return ret;
 195
 196        return pmbus_read_byte_data(client, page, reg);
 197}
 198
 199static int ltc_write_byte(struct i2c_client *client, int page, u8 byte)
 200{
 201        int ret;
 202
 203        ret = ltc_wait_ready(client);
 204        if (ret < 0)
 205                return ret;
 206
 207        return pmbus_write_byte(client, page, byte);
 208}
 209
 210static inline int lin11_to_val(int data)
 211{
 212        s16 e = ((s16)data) >> 11;
 213        s32 m = (((s16)(data << 5)) >> 5);
 214
 215        /*
 216         * mantissa is 10 bit + sign, exponent adds up to 15 bit.
 217         * Add 6 bit to exponent for maximum accuracy (10 + 15 + 6 = 31).
 218         */
 219        e += 6;
 220        return (e < 0 ? m >> -e : m << e);
 221}
 222
 223static int ltc_get_max(struct ltc2978_data *data, struct i2c_client *client,
 224                       int page, int reg, u16 *pmax)
 225{
 226        int ret;
 227
 228        ret = ltc_read_word_data(client, page, 0xff, reg);
 229        if (ret >= 0) {
 230                if (lin11_to_val(ret) > lin11_to_val(*pmax))
 231                        *pmax = ret;
 232                ret = *pmax;
 233        }
 234        return ret;
 235}
 236
 237static int ltc_get_min(struct ltc2978_data *data, struct i2c_client *client,
 238                       int page, int reg, u16 *pmin)
 239{
 240        int ret;
 241
 242        ret = ltc_read_word_data(client, page, 0xff, reg);
 243        if (ret >= 0) {
 244                if (lin11_to_val(ret) < lin11_to_val(*pmin))
 245                        *pmin = ret;
 246                ret = *pmin;
 247        }
 248        return ret;
 249}
 250
 251static int ltc2978_read_word_data_common(struct i2c_client *client, int page,
 252                                         int reg)
 253{
 254        const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
 255        struct ltc2978_data *data = to_ltc2978_data(info);
 256        int ret;
 257
 258        switch (reg) {
 259        case PMBUS_VIRT_READ_VIN_MAX:
 260                ret = ltc_get_max(data, client, page, LTC2978_MFR_VIN_PEAK,
 261                                  &data->vin_max);
 262                break;
 263        case PMBUS_VIRT_READ_VOUT_MAX:
 264                ret = ltc_read_word_data(client, page, 0xff,
 265                                         LTC2978_MFR_VOUT_PEAK);
 266                if (ret >= 0) {
 267                        /*
 268                         * VOUT is 16 bit unsigned with fixed exponent,
 269                         * so we can compare it directly
 270                         */
 271                        if (ret > data->vout_max[page])
 272                                data->vout_max[page] = ret;
 273                        ret = data->vout_max[page];
 274                }
 275                break;
 276        case PMBUS_VIRT_READ_TEMP_MAX:
 277                ret = ltc_get_max(data, client, page,
 278                                  LTC2978_MFR_TEMPERATURE_PEAK,
 279                                  &data->temp_max[page]);
 280                break;
 281        case PMBUS_VIRT_RESET_VOUT_HISTORY:
 282        case PMBUS_VIRT_RESET_VIN_HISTORY:
 283        case PMBUS_VIRT_RESET_TEMP_HISTORY:
 284                ret = 0;
 285                break;
 286        default:
 287                ret = ltc_wait_ready(client);
 288                if (ret < 0)
 289                        return ret;
 290                ret = -ENODATA;
 291                break;
 292        }
 293        return ret;
 294}
 295
 296static int ltc2978_read_word_data(struct i2c_client *client, int page,
 297                                  int phase, int reg)
 298{
 299        const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
 300        struct ltc2978_data *data = to_ltc2978_data(info);
 301        int ret;
 302
 303        switch (reg) {
 304        case PMBUS_VIRT_READ_VIN_MIN:
 305                ret = ltc_get_min(data, client, page, LTC2978_MFR_VIN_MIN,
 306                                  &data->vin_min);
 307                break;
 308        case PMBUS_VIRT_READ_VOUT_MIN:
 309                ret = ltc_read_word_data(client, page, phase,
 310                                         LTC2978_MFR_VOUT_MIN);
 311                if (ret >= 0) {
 312                        /*
 313                         * VOUT_MIN is known to not be supported on some lots
 314                         * of LTC2978 revision 1, and will return the maximum
 315                         * possible voltage if read. If VOUT_MAX is valid and
 316                         * lower than the reading of VOUT_MIN, use it instead.
 317                         */
 318                        if (data->vout_max[page] && ret > data->vout_max[page])
 319                                ret = data->vout_max[page];
 320                        if (ret < data->vout_min[page])
 321                                data->vout_min[page] = ret;
 322                        ret = data->vout_min[page];
 323                }
 324                break;
 325        case PMBUS_VIRT_READ_TEMP_MIN:
 326                ret = ltc_get_min(data, client, page,
 327                                  LTC2978_MFR_TEMPERATURE_MIN,
 328                                  &data->temp_min[page]);
 329                break;
 330        case PMBUS_VIRT_READ_IOUT_MAX:
 331        case PMBUS_VIRT_RESET_IOUT_HISTORY:
 332        case PMBUS_VIRT_READ_TEMP2_MAX:
 333        case PMBUS_VIRT_RESET_TEMP2_HISTORY:
 334                ret = -ENXIO;
 335                break;
 336        default:
 337                ret = ltc2978_read_word_data_common(client, page, reg);
 338                break;
 339        }
 340        return ret;
 341}
 342
 343static int ltc2974_read_word_data(struct i2c_client *client, int page,
 344                                  int phase, int reg)
 345{
 346        const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
 347        struct ltc2978_data *data = to_ltc2978_data(info);
 348        int ret;
 349
 350        switch (reg) {
 351        case PMBUS_VIRT_READ_IOUT_MAX:
 352                ret = ltc_get_max(data, client, page, LTC2974_MFR_IOUT_PEAK,
 353                                  &data->iout_max[page]);
 354                break;
 355        case PMBUS_VIRT_READ_IOUT_MIN:
 356                ret = ltc_get_min(data, client, page, LTC2974_MFR_IOUT_MIN,
 357                                  &data->iout_min[page]);
 358                break;
 359        case PMBUS_VIRT_RESET_IOUT_HISTORY:
 360                ret = 0;
 361                break;
 362        default:
 363                ret = ltc2978_read_word_data(client, page, phase, reg);
 364                break;
 365        }
 366        return ret;
 367}
 368
 369static int ltc2975_read_word_data(struct i2c_client *client, int page,
 370                                  int phase, int reg)
 371{
 372        const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
 373        struct ltc2978_data *data = to_ltc2978_data(info);
 374        int ret;
 375
 376        switch (reg) {
 377        case PMBUS_VIRT_READ_IIN_MAX:
 378                ret = ltc_get_max(data, client, page, LTC2975_MFR_IIN_PEAK,
 379                                  &data->iin_max);
 380                break;
 381        case PMBUS_VIRT_READ_IIN_MIN:
 382                ret = ltc_get_min(data, client, page, LTC2975_MFR_IIN_MIN,
 383                                  &data->iin_min);
 384                break;
 385        case PMBUS_VIRT_READ_PIN_MAX:
 386                ret = ltc_get_max(data, client, page, LTC2975_MFR_PIN_PEAK,
 387                                  &data->pin_max);
 388                break;
 389        case PMBUS_VIRT_READ_PIN_MIN:
 390                ret = ltc_get_min(data, client, page, LTC2975_MFR_PIN_MIN,
 391                                  &data->pin_min);
 392                break;
 393        case PMBUS_VIRT_RESET_IIN_HISTORY:
 394        case PMBUS_VIRT_RESET_PIN_HISTORY:
 395                ret = 0;
 396                break;
 397        default:
 398                ret = ltc2978_read_word_data(client, page, phase, reg);
 399                break;
 400        }
 401        return ret;
 402}
 403
 404static int ltc3880_read_word_data(struct i2c_client *client, int page,
 405                                  int phase, int reg)
 406{
 407        const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
 408        struct ltc2978_data *data = to_ltc2978_data(info);
 409        int ret;
 410
 411        switch (reg) {
 412        case PMBUS_VIRT_READ_IOUT_MAX:
 413                ret = ltc_get_max(data, client, page, LTC3880_MFR_IOUT_PEAK,
 414                                  &data->iout_max[page]);
 415                break;
 416        case PMBUS_VIRT_READ_TEMP2_MAX:
 417                ret = ltc_get_max(data, client, page,
 418                                  LTC3880_MFR_TEMPERATURE2_PEAK,
 419                                  &data->temp2_max);
 420                break;
 421        case PMBUS_VIRT_READ_VIN_MIN:
 422        case PMBUS_VIRT_READ_VOUT_MIN:
 423        case PMBUS_VIRT_READ_TEMP_MIN:
 424                ret = -ENXIO;
 425                break;
 426        case PMBUS_VIRT_RESET_IOUT_HISTORY:
 427        case PMBUS_VIRT_RESET_TEMP2_HISTORY:
 428                ret = 0;
 429                break;
 430        default:
 431                ret = ltc2978_read_word_data_common(client, page, reg);
 432                break;
 433        }
 434        return ret;
 435}
 436
 437static int ltc3883_read_word_data(struct i2c_client *client, int page,
 438                                  int phase, int reg)
 439{
 440        const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
 441        struct ltc2978_data *data = to_ltc2978_data(info);
 442        int ret;
 443
 444        switch (reg) {
 445        case PMBUS_VIRT_READ_IIN_MAX:
 446                ret = ltc_get_max(data, client, page, LTC3883_MFR_IIN_PEAK,
 447                                  &data->iin_max);
 448                break;
 449        case PMBUS_VIRT_RESET_IIN_HISTORY:
 450                ret = 0;
 451                break;
 452        default:
 453                ret = ltc3880_read_word_data(client, page, phase, reg);
 454                break;
 455        }
 456        return ret;
 457}
 458
 459static int ltc2978_clear_peaks(struct ltc2978_data *data,
 460                               struct i2c_client *client, int page)
 461{
 462        int ret;
 463
 464        if (has_clear_peaks(data))
 465                ret = ltc_write_byte(client, 0, LTC3880_MFR_CLEAR_PEAKS);
 466        else
 467                ret = ltc_write_byte(client, page, PMBUS_CLEAR_FAULTS);
 468
 469        return ret;
 470}
 471
 472static int ltc2978_write_word_data(struct i2c_client *client, int page,
 473                                    int reg, u16 word)
 474{
 475        const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
 476        struct ltc2978_data *data = to_ltc2978_data(info);
 477        int ret;
 478
 479        switch (reg) {
 480        case PMBUS_VIRT_RESET_IIN_HISTORY:
 481                data->iin_max = 0x7c00;
 482                data->iin_min = 0x7bff;
 483                ret = ltc2978_clear_peaks(data, client, 0);
 484                break;
 485        case PMBUS_VIRT_RESET_PIN_HISTORY:
 486                data->pin_max = 0x7c00;
 487                data->pin_min = 0x7bff;
 488                ret = ltc2978_clear_peaks(data, client, 0);
 489                break;
 490        case PMBUS_VIRT_RESET_IOUT_HISTORY:
 491                data->iout_max[page] = 0x7c00;
 492                data->iout_min[page] = 0xfbff;
 493                ret = ltc2978_clear_peaks(data, client, page);
 494                break;
 495        case PMBUS_VIRT_RESET_TEMP2_HISTORY:
 496                data->temp2_max = 0x7c00;
 497                ret = ltc2978_clear_peaks(data, client, page);
 498                break;
 499        case PMBUS_VIRT_RESET_VOUT_HISTORY:
 500                data->vout_min[page] = 0xffff;
 501                data->vout_max[page] = 0;
 502                ret = ltc2978_clear_peaks(data, client, page);
 503                break;
 504        case PMBUS_VIRT_RESET_VIN_HISTORY:
 505                data->vin_min = 0x7bff;
 506                data->vin_max = 0x7c00;
 507                ret = ltc2978_clear_peaks(data, client, page);
 508                break;
 509        case PMBUS_VIRT_RESET_TEMP_HISTORY:
 510                data->temp_min[page] = 0x7bff;
 511                data->temp_max[page] = 0x7c00;
 512                ret = ltc2978_clear_peaks(data, client, page);
 513                break;
 514        default:
 515                ret = ltc_wait_ready(client);
 516                if (ret < 0)
 517                        return ret;
 518                ret = -ENODATA;
 519                break;
 520        }
 521        return ret;
 522}
 523
 524static const struct i2c_device_id ltc2978_id[] = {
 525        {"ltc2972", ltc2972},
 526        {"ltc2974", ltc2974},
 527        {"ltc2975", ltc2975},
 528        {"ltc2977", ltc2977},
 529        {"ltc2978", ltc2978},
 530        {"ltc2979", ltc2979},
 531        {"ltc2980", ltc2980},
 532        {"ltc3880", ltc3880},
 533        {"ltc3882", ltc3882},
 534        {"ltc3883", ltc3883},
 535        {"ltc3884", ltc3884},
 536        {"ltc3886", ltc3886},
 537        {"ltc3887", ltc3887},
 538        {"ltc3889", ltc3889},
 539        {"ltc7880", ltc7880},
 540        {"ltm2987", ltm2987},
 541        {"ltm4664", ltm4664},
 542        {"ltm4675", ltm4675},
 543        {"ltm4676", ltm4676},
 544        {"ltm4677", ltm4677},
 545        {"ltm4678", ltm4678},
 546        {"ltm4680", ltm4680},
 547        {"ltm4686", ltm4686},
 548        {"ltm4700", ltm4700},
 549        {}
 550};
 551MODULE_DEVICE_TABLE(i2c, ltc2978_id);
 552
 553#if IS_ENABLED(CONFIG_SENSORS_LTC2978_REGULATOR)
 554static const struct regulator_desc ltc2978_reg_desc[] = {
 555        PMBUS_REGULATOR("vout", 0),
 556        PMBUS_REGULATOR("vout", 1),
 557        PMBUS_REGULATOR("vout", 2),
 558        PMBUS_REGULATOR("vout", 3),
 559        PMBUS_REGULATOR("vout", 4),
 560        PMBUS_REGULATOR("vout", 5),
 561        PMBUS_REGULATOR("vout", 6),
 562        PMBUS_REGULATOR("vout", 7),
 563};
 564#endif /* CONFIG_SENSORS_LTC2978_REGULATOR */
 565
 566static int ltc2978_get_id(struct i2c_client *client)
 567{
 568        int chip_id;
 569
 570        chip_id = i2c_smbus_read_word_data(client, LTC2978_MFR_SPECIAL_ID);
 571        if (chip_id < 0) {
 572                const struct i2c_device_id *id;
 573                u8 buf[I2C_SMBUS_BLOCK_MAX];
 574                int ret;
 575
 576                if (!i2c_check_functionality(client->adapter,
 577                                             I2C_FUNC_SMBUS_READ_BLOCK_DATA))
 578                        return -ENODEV;
 579
 580                ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
 581                if (ret < 0)
 582                        return ret;
 583                if (ret < 3 || strncmp(buf, "LTC", 3))
 584                        return -ENODEV;
 585
 586                ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
 587                if (ret < 0)
 588                        return ret;
 589                for (id = &ltc2978_id[0]; strlen(id->name); id++) {
 590                        if (!strncasecmp(id->name, buf, strlen(id->name)))
 591                                return (int)id->driver_data;
 592                }
 593                return -ENODEV;
 594        }
 595
 596        chip_id &= LTC2978_ID_MASK;
 597
 598        if (chip_id == LTC2972_ID)
 599                return ltc2972;
 600        else if (chip_id == LTC2974_ID)
 601                return ltc2974;
 602        else if (chip_id == LTC2975_ID)
 603                return ltc2975;
 604        else if (chip_id == LTC2977_ID)
 605                return ltc2977;
 606        else if (chip_id == LTC2978_ID_REV1 || chip_id == LTC2978_ID_REV2)
 607                return ltc2978;
 608        else if (chip_id == LTC2979_ID_A || chip_id == LTC2979_ID_B)
 609                return ltc2979;
 610        else if (chip_id == LTC2980_ID_A || chip_id == LTC2980_ID_B)
 611                return ltc2980;
 612        else if (chip_id == LTC3880_ID)
 613                return ltc3880;
 614        else if (chip_id == LTC3882_ID || chip_id == LTC3882_ID_D1)
 615                return ltc3882;
 616        else if (chip_id == LTC3883_ID)
 617                return ltc3883;
 618        else if (chip_id == LTC3884_ID)
 619                return ltc3884;
 620        else if (chip_id == LTC3886_ID)
 621                return ltc3886;
 622        else if (chip_id == LTC3887_ID)
 623                return ltc3887;
 624        else if (chip_id == LTC3889_ID)
 625                return ltc3889;
 626        else if (chip_id == LTC7880_ID)
 627                return ltc7880;
 628        else if (chip_id == LTM2987_ID_A || chip_id == LTM2987_ID_B)
 629                return ltm2987;
 630        else if (chip_id == LTM4664_ID)
 631                return ltm4664;
 632        else if (chip_id == LTM4675_ID)
 633                return ltm4675;
 634        else if (chip_id == LTM4676_ID_REV1 || chip_id == LTM4676_ID_REV2 ||
 635                 chip_id == LTM4676A_ID)
 636                return ltm4676;
 637        else if (chip_id == LTM4677_ID_REV1 || chip_id == LTM4677_ID_REV2)
 638                return ltm4677;
 639        else if (chip_id == LTM4678_ID_REV1 || chip_id == LTM4678_ID_REV2)
 640                return ltm4678;
 641        else if (chip_id == LTM4680_ID)
 642                return ltm4680;
 643        else if (chip_id == LTM4686_ID)
 644                return ltm4686;
 645        else if (chip_id == LTM4700_ID)
 646                return ltm4700;
 647
 648        dev_err(&client->dev, "Unsupported chip ID 0x%x\n", chip_id);
 649        return -ENODEV;
 650}
 651
 652static int ltc2978_probe(struct i2c_client *client)
 653{
 654        int i, chip_id;
 655        struct ltc2978_data *data;
 656        struct pmbus_driver_info *info;
 657        const struct i2c_device_id *id;
 658
 659        if (!i2c_check_functionality(client->adapter,
 660                                     I2C_FUNC_SMBUS_READ_WORD_DATA))
 661                return -ENODEV;
 662
 663        data = devm_kzalloc(&client->dev, sizeof(struct ltc2978_data),
 664                            GFP_KERNEL);
 665        if (!data)
 666                return -ENOMEM;
 667
 668        chip_id = ltc2978_get_id(client);
 669        if (chip_id < 0)
 670                return chip_id;
 671
 672        data->id = chip_id;
 673        id = i2c_match_id(ltc2978_id, client);
 674        if (data->id != id->driver_data)
 675                dev_warn(&client->dev,
 676                         "Device mismatch: Configured %s (%d), detected %d\n",
 677                         id->name,
 678                         (int) id->driver_data,
 679                         chip_id);
 680
 681        info = &data->info;
 682        info->write_word_data = ltc2978_write_word_data;
 683        info->write_byte = ltc_write_byte;
 684        info->read_word_data = ltc_read_word_data;
 685        info->read_byte_data = ltc_read_byte_data;
 686
 687        data->vin_min = 0x7bff;
 688        data->vin_max = 0x7c00;
 689        for (i = 0; i < ARRAY_SIZE(data->vout_min); i++)
 690                data->vout_min[i] = 0xffff;
 691        for (i = 0; i < ARRAY_SIZE(data->iout_min); i++)
 692                data->iout_min[i] = 0xfbff;
 693        for (i = 0; i < ARRAY_SIZE(data->iout_max); i++)
 694                data->iout_max[i] = 0x7c00;
 695        for (i = 0; i < ARRAY_SIZE(data->temp_min); i++)
 696                data->temp_min[i] = 0x7bff;
 697        for (i = 0; i < ARRAY_SIZE(data->temp_max); i++)
 698                data->temp_max[i] = 0x7c00;
 699        data->temp2_max = 0x7c00;
 700
 701        switch (data->id) {
 702        case ltc2972:
 703                info->read_word_data = ltc2975_read_word_data;
 704                info->pages = LTC2972_NUM_PAGES;
 705                info->func[0] = PMBUS_HAVE_IIN | PMBUS_HAVE_PIN
 706                  | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
 707                  | PMBUS_HAVE_TEMP2;
 708                for (i = 0; i < info->pages; i++) {
 709                        info->func[i] |= PMBUS_HAVE_VOUT
 710                          | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
 711                          | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
 712                          | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
 713                }
 714                break;
 715        case ltc2974:
 716                info->read_word_data = ltc2974_read_word_data;
 717                info->pages = LTC2974_NUM_PAGES;
 718                info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
 719                  | PMBUS_HAVE_TEMP2;
 720                for (i = 0; i < info->pages; i++) {
 721                        info->func[i] |= PMBUS_HAVE_VOUT
 722                          | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
 723                          | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
 724                          | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
 725                }
 726                break;
 727        case ltc2975:
 728                info->read_word_data = ltc2975_read_word_data;
 729                info->pages = LTC2974_NUM_PAGES;
 730                info->func[0] = PMBUS_HAVE_IIN | PMBUS_HAVE_PIN
 731                  | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
 732                  | PMBUS_HAVE_TEMP2;
 733                for (i = 0; i < info->pages; i++) {
 734                        info->func[i] |= PMBUS_HAVE_VOUT
 735                          | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
 736                          | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
 737                          | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
 738                }
 739                break;
 740
 741        case ltc2977:
 742        case ltc2978:
 743        case ltc2979:
 744        case ltc2980:
 745        case ltm2987:
 746                info->read_word_data = ltc2978_read_word_data;
 747                info->pages = LTC2978_NUM_PAGES;
 748                info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
 749                  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
 750                  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
 751                for (i = 1; i < LTC2978_NUM_PAGES; i++) {
 752                        info->func[i] = PMBUS_HAVE_VOUT
 753                          | PMBUS_HAVE_STATUS_VOUT;
 754                }
 755                break;
 756        case ltc3880:
 757        case ltc3887:
 758        case ltm4675:
 759        case ltm4676:
 760        case ltm4677:
 761        case ltm4686:
 762                data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
 763                info->read_word_data = ltc3880_read_word_data;
 764                info->pages = LTC3880_NUM_PAGES;
 765                info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
 766                  | PMBUS_HAVE_STATUS_INPUT
 767                  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
 768                  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
 769                  | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
 770                  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
 771                info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
 772                  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
 773                  | PMBUS_HAVE_POUT
 774                  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
 775                break;
 776        case ltc3882:
 777                data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
 778                info->read_word_data = ltc3880_read_word_data;
 779                info->pages = LTC3880_NUM_PAGES;
 780                info->func[0] = PMBUS_HAVE_VIN
 781                  | PMBUS_HAVE_STATUS_INPUT
 782                  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
 783                  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
 784                  | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
 785                  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
 786                info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
 787                  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
 788                  | PMBUS_HAVE_POUT
 789                  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
 790                break;
 791        case ltc3883:
 792                data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
 793                info->read_word_data = ltc3883_read_word_data;
 794                info->pages = LTC3883_NUM_PAGES;
 795                info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
 796                  | PMBUS_HAVE_STATUS_INPUT
 797                  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
 798                  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
 799                  | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
 800                  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
 801                break;
 802        case ltc3884:
 803        case ltc3886:
 804        case ltc3889:
 805        case ltc7880:
 806        case ltm4664:
 807        case ltm4678:
 808        case ltm4680:
 809        case ltm4700:
 810                data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
 811                info->read_word_data = ltc3883_read_word_data;
 812                info->pages = LTC3880_NUM_PAGES;
 813                info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
 814                  | PMBUS_HAVE_STATUS_INPUT
 815                  | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
 816                  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
 817                  | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
 818                  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
 819                info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
 820                  | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
 821                  | PMBUS_HAVE_POUT
 822                  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
 823                break;
 824        default:
 825                return -ENODEV;
 826        }
 827
 828#if IS_ENABLED(CONFIG_SENSORS_LTC2978_REGULATOR)
 829        info->num_regulators = info->pages;
 830        info->reg_desc = ltc2978_reg_desc;
 831        if (info->num_regulators > ARRAY_SIZE(ltc2978_reg_desc)) {
 832                dev_err(&client->dev, "num_regulators too large!");
 833                info->num_regulators = ARRAY_SIZE(ltc2978_reg_desc);
 834        }
 835#endif
 836
 837        return pmbus_do_probe(client, info);
 838}
 839
 840
 841#ifdef CONFIG_OF
 842static const struct of_device_id ltc2978_of_match[] = {
 843        { .compatible = "lltc,ltc2972" },
 844        { .compatible = "lltc,ltc2974" },
 845        { .compatible = "lltc,ltc2975" },
 846        { .compatible = "lltc,ltc2977" },
 847        { .compatible = "lltc,ltc2978" },
 848        { .compatible = "lltc,ltc2979" },
 849        { .compatible = "lltc,ltc2980" },
 850        { .compatible = "lltc,ltc3880" },
 851        { .compatible = "lltc,ltc3882" },
 852        { .compatible = "lltc,ltc3883" },
 853        { .compatible = "lltc,ltc3884" },
 854        { .compatible = "lltc,ltc3886" },
 855        { .compatible = "lltc,ltc3887" },
 856        { .compatible = "lltc,ltc3889" },
 857        { .compatible = "lltc,ltc7880" },
 858        { .compatible = "lltc,ltm2987" },
 859        { .compatible = "lltc,ltm4664" },
 860        { .compatible = "lltc,ltm4675" },
 861        { .compatible = "lltc,ltm4676" },
 862        { .compatible = "lltc,ltm4677" },
 863        { .compatible = "lltc,ltm4678" },
 864        { .compatible = "lltc,ltm4680" },
 865        { .compatible = "lltc,ltm4686" },
 866        { .compatible = "lltc,ltm4700" },
 867        { }
 868};
 869MODULE_DEVICE_TABLE(of, ltc2978_of_match);
 870#endif
 871
 872static struct i2c_driver ltc2978_driver = {
 873        .driver = {
 874                   .name = "ltc2978",
 875                   .of_match_table = of_match_ptr(ltc2978_of_match),
 876                   },
 877        .probe_new = ltc2978_probe,
 878        .remove = pmbus_do_remove,
 879        .id_table = ltc2978_id,
 880};
 881
 882module_i2c_driver(ltc2978_driver);
 883
 884MODULE_AUTHOR("Guenter Roeck");
 885MODULE_DESCRIPTION("PMBus driver for LTC2978 and compatible chips");
 886MODULE_LICENSE("GPL");
 887