Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers
0004  *
0005  * Copyright (C) 2020 Nvidia Technologies Ltd.
0006  */
0007 
0008 #include <linux/err.h>
0009 #include <linux/i2c.h>
0010 #include <linux/init.h>
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include "pmbus.h"
0014 
0015 /* Vendor specific registers. */
0016 #define MP2888_MFR_SYS_CONFIG   0x44
0017 #define MP2888_MFR_READ_CS1_2   0x73
0018 #define MP2888_MFR_READ_CS3_4   0x74
0019 #define MP2888_MFR_READ_CS5_6   0x75
0020 #define MP2888_MFR_READ_CS7_8   0x76
0021 #define MP2888_MFR_READ_CS9_10  0x77
0022 #define MP2888_MFR_VR_CONFIG1   0xe1
0023 
0024 #define MP2888_TOTAL_CURRENT_RESOLUTION BIT(3)
0025 #define MP2888_PHASE_CURRENT_RESOLUTION BIT(4)
0026 #define MP2888_DRMOS_KCS        GENMASK(2, 0)
0027 #define MP2888_TEMP_UNIT        10
0028 #define MP2888_MAX_PHASE        10
0029 
0030 struct mp2888_data {
0031     struct pmbus_driver_info info;
0032     int total_curr_resolution;
0033     int phase_curr_resolution;
0034     int curr_sense_gain;
0035 };
0036 
0037 #define to_mp2888_data(x)  container_of(x, struct mp2888_data, info)
0038 
0039 static int mp2888_read_byte_data(struct i2c_client *client, int page, int reg)
0040 {
0041     switch (reg) {
0042     case PMBUS_VOUT_MODE:
0043         /* Enforce VOUT direct format. */
0044         return PB_VOUT_MODE_DIRECT;
0045     default:
0046         return -ENODATA;
0047     }
0048 }
0049 
0050 static int
0051 mp2888_current_sense_gain_and_resolution_get(struct i2c_client *client, struct mp2888_data *data)
0052 {
0053     int ret;
0054 
0055     /*
0056      * Obtain DrMOS current sense gain of power stage from the register
0057      * , bits 0-2. The value is selected as below:
0058      * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. Other
0059      * values are reserved.
0060      */
0061     ret = i2c_smbus_read_word_data(client, MP2888_MFR_SYS_CONFIG);
0062     if (ret < 0)
0063         return ret;
0064 
0065     switch (ret & MP2888_DRMOS_KCS) {
0066     case 0:
0067         data->curr_sense_gain = 85;
0068         break;
0069     case 1:
0070         data->curr_sense_gain = 97;
0071         break;
0072     case 2:
0073         data->curr_sense_gain = 100;
0074         break;
0075     case 3:
0076         data->curr_sense_gain = 50;
0077         break;
0078     default:
0079         return -EINVAL;
0080     }
0081 
0082     /*
0083      * Obtain resolution selector for total and phase current report and protection.
0084      * 0: original resolution; 1: half resolution (in such case phase current value should
0085      * be doubled.
0086      */
0087     data->total_curr_resolution = (ret & MP2888_TOTAL_CURRENT_RESOLUTION) >> 3;
0088     data->phase_curr_resolution = (ret & MP2888_PHASE_CURRENT_RESOLUTION) >> 4;
0089 
0090     return 0;
0091 }
0092 
0093 static int
0094 mp2888_read_phase(struct i2c_client *client, struct mp2888_data *data, int page, int phase, u8 reg)
0095 {
0096     int ret;
0097 
0098     ret = pmbus_read_word_data(client, page, phase, reg);
0099     if (ret < 0)
0100         return ret;
0101 
0102     if (!((phase + 1) % 2))
0103         ret >>= 8;
0104     ret &= 0xff;
0105 
0106     /*
0107      * Output value is calculated as: (READ_CSx / 80 – 1.23) / (Kcs * Rcs)
0108      * where:
0109      * - Kcs is the DrMOS current sense gain of power stage, which is obtained from the
0110      *   register MP2888_MFR_VR_CONFIG1, bits 13-12 with the following selection of DrMOS
0111      *   (data->curr_sense_gain):
0112      *   00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A.
0113      * - Rcs is the internal phase current sense resistor. This parameter depends on hardware
0114      *   assembly. By default it is set to 1kΩ. In case of different assembly, user should
0115      *   scale this parameter by dividing it by Rcs.
0116      * If phase current resolution bit is set to 1, READ_CSx value should be doubled.
0117      * Note, that current phase sensing, providing by the device is not accurate. This is
0118      * because sampling of current occurrence of bit weight has a big deviation, especially for
0119      * light load.
0120      */
0121     ret = DIV_ROUND_CLOSEST(ret * 100 - 9800, data->curr_sense_gain);
0122     ret = (data->phase_curr_resolution) ? ret * 2 : ret;
0123     /* Scale according to total current resolution. */
0124     ret = (data->total_curr_resolution) ? ret * 8 : ret * 4;
0125     return ret;
0126 }
0127 
0128 static int
0129 mp2888_read_phases(struct i2c_client *client, struct mp2888_data *data, int page, int phase)
0130 {
0131     int ret;
0132 
0133     switch (phase) {
0134     case 0 ... 1:
0135         ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS1_2);
0136         break;
0137     case 2 ... 3:
0138         ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS3_4);
0139         break;
0140     case 4 ... 5:
0141         ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS5_6);
0142         break;
0143     case 6 ... 7:
0144         ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS7_8);
0145         break;
0146     case 8 ... 9:
0147         ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS9_10);
0148         break;
0149     default:
0150         return -ENODATA;
0151     }
0152     return ret;
0153 }
0154 
0155 static int mp2888_read_word_data(struct i2c_client *client, int page, int phase, int reg)
0156 {
0157     const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
0158     struct mp2888_data *data = to_mp2888_data(info);
0159     int ret;
0160 
0161     switch (reg) {
0162     case PMBUS_READ_VIN:
0163         ret = pmbus_read_word_data(client, page, phase, reg);
0164         if (ret <= 0)
0165             return ret;
0166 
0167         /*
0168          * READ_VIN requires fixup to scale it to linear11 format. Register data format
0169          * provides 10 bits for mantissa and 6 bits for exponent. Bits 15:10 are set with
0170          * the fixed value 111011b.
0171          */
0172         ret = (ret & GENMASK(9, 0)) | ((ret & GENMASK(31, 10)) << 1);
0173         break;
0174     case PMBUS_OT_WARN_LIMIT:
0175         ret = pmbus_read_word_data(client, page, phase, reg);
0176         if (ret < 0)
0177             return ret;
0178         /*
0179          * Chip reports limits in degrees C, but the actual temperature in 10th of
0180          * degrees C - scaling is needed to match both.
0181          */
0182         ret *= MP2888_TEMP_UNIT;
0183         break;
0184     case PMBUS_READ_IOUT:
0185         if (phase != 0xff)
0186             return mp2888_read_phases(client, data, page, phase);
0187 
0188         ret = pmbus_read_word_data(client, page, phase, reg);
0189         if (ret < 0)
0190             return ret;
0191         /*
0192          * READ_IOUT register has unused bits 15:12 with fixed value 1110b. Clear these
0193          * bits and scale with total current resolution. Data is provided in direct format.
0194          */
0195         ret &= GENMASK(11, 0);
0196         ret = data->total_curr_resolution ? ret * 2 : ret;
0197         break;
0198     case PMBUS_IOUT_OC_WARN_LIMIT:
0199         ret = pmbus_read_word_data(client, page, phase, reg);
0200         if (ret < 0)
0201             return ret;
0202         ret &= GENMASK(9, 0);
0203         /*
0204          * Chip reports limits with resolution 1A or 2A, if total current resolution bit is
0205          * set 1. Actual current is reported with 0.25A or respectively 0.5A resolution.
0206          * Scaling is needed to match both.
0207          */
0208         ret = data->total_curr_resolution ? ret * 8 : ret * 4;
0209         break;
0210     case PMBUS_READ_POUT:
0211     case PMBUS_READ_PIN:
0212         ret = pmbus_read_word_data(client, page, phase, reg);
0213         if (ret < 0)
0214             return ret;
0215         ret = data->total_curr_resolution ? ret * 2 : ret;
0216         break;
0217     case PMBUS_POUT_OP_WARN_LIMIT:
0218         ret = pmbus_read_word_data(client, page, phase, reg);
0219         if (ret < 0)
0220             return ret;
0221         /*
0222          * Chip reports limits with resolution 1W or 2W, if total current resolution bit is
0223          * set 1. Actual power is reported with 0.5W or 1W respectively resolution. Scaling
0224          * is needed to match both.
0225          */
0226         ret = data->total_curr_resolution ? ret * 4 : ret * 2;
0227         break;
0228     /*
0229      * The below registers are not implemented by device or implemented not according to the
0230      * spec. Skip all of them to avoid exposing non-relevant inputs to sysfs.
0231      */
0232     case PMBUS_OT_FAULT_LIMIT:
0233     case PMBUS_UT_WARN_LIMIT:
0234     case PMBUS_UT_FAULT_LIMIT:
0235     case PMBUS_VIN_UV_FAULT_LIMIT:
0236     case PMBUS_VOUT_UV_WARN_LIMIT:
0237     case PMBUS_VOUT_OV_WARN_LIMIT:
0238     case PMBUS_VOUT_UV_FAULT_LIMIT:
0239     case PMBUS_VOUT_OV_FAULT_LIMIT:
0240     case PMBUS_VIN_OV_WARN_LIMIT:
0241     case PMBUS_IOUT_OC_LV_FAULT_LIMIT:
0242     case PMBUS_IOUT_OC_FAULT_LIMIT:
0243     case PMBUS_POUT_MAX:
0244     case PMBUS_IOUT_UC_FAULT_LIMIT:
0245     case PMBUS_POUT_OP_FAULT_LIMIT:
0246     case PMBUS_PIN_OP_WARN_LIMIT:
0247     case PMBUS_MFR_VIN_MIN:
0248     case PMBUS_MFR_VOUT_MIN:
0249     case PMBUS_MFR_VIN_MAX:
0250     case PMBUS_MFR_VOUT_MAX:
0251     case PMBUS_MFR_IIN_MAX:
0252     case PMBUS_MFR_IOUT_MAX:
0253     case PMBUS_MFR_PIN_MAX:
0254     case PMBUS_MFR_POUT_MAX:
0255     case PMBUS_MFR_MAX_TEMP_1:
0256         return -ENXIO;
0257     default:
0258         return -ENODATA;
0259     }
0260 
0261     return ret;
0262 }
0263 
0264 static int mp2888_write_word_data(struct i2c_client *client, int page, int reg, u16 word)
0265 {
0266     const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
0267     struct mp2888_data *data = to_mp2888_data(info);
0268 
0269     switch (reg) {
0270     case PMBUS_OT_WARN_LIMIT:
0271         word = DIV_ROUND_CLOSEST(word, MP2888_TEMP_UNIT);
0272         /* Drop unused bits 15:8. */
0273         word = clamp_val(word, 0, GENMASK(7, 0));
0274         break;
0275     case PMBUS_IOUT_OC_WARN_LIMIT:
0276         /* Fix limit according to total curent resolution. */
0277         word = data->total_curr_resolution ? DIV_ROUND_CLOSEST(word, 8) :
0278                DIV_ROUND_CLOSEST(word, 4);
0279         /* Drop unused bits 15:10. */
0280         word = clamp_val(word, 0, GENMASK(9, 0));
0281         break;
0282     case PMBUS_POUT_OP_WARN_LIMIT:
0283         /* Fix limit according to total curent resolution. */
0284         word = data->total_curr_resolution ? DIV_ROUND_CLOSEST(word, 4) :
0285                DIV_ROUND_CLOSEST(word, 2);
0286         /* Drop unused bits 15:10. */
0287         word = clamp_val(word, 0, GENMASK(9, 0));
0288         break;
0289     default:
0290         return -ENODATA;
0291     }
0292     return pmbus_write_word_data(client, page, reg, word);
0293 }
0294 
0295 static int
0296 mp2888_identify_multiphase(struct i2c_client *client, struct mp2888_data *data,
0297                struct pmbus_driver_info *info)
0298 {
0299     int ret;
0300 
0301     ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
0302     if (ret < 0)
0303         return ret;
0304 
0305     /* Identify multiphase number - could be from 1 to 10. */
0306     ret = i2c_smbus_read_word_data(client, MP2888_MFR_VR_CONFIG1);
0307     if (ret <= 0)
0308         return ret;
0309 
0310     info->phases[0] = ret & GENMASK(3, 0);
0311 
0312     /*
0313      * The device provides a total of 10 PWM pins, and can be configured to different phase
0314      * count applications for rail.
0315      */
0316     if (info->phases[0] > MP2888_MAX_PHASE)
0317         return -EINVAL;
0318 
0319     return 0;
0320 }
0321 
0322 static struct pmbus_driver_info mp2888_info = {
0323     .pages = 1,
0324     .format[PSC_VOLTAGE_IN] = linear,
0325     .format[PSC_VOLTAGE_OUT] = direct,
0326     .format[PSC_TEMPERATURE] = direct,
0327     .format[PSC_CURRENT_IN] = linear,
0328     .format[PSC_CURRENT_OUT] = direct,
0329     .format[PSC_POWER] = direct,
0330     .m[PSC_TEMPERATURE] = 1,
0331     .R[PSC_TEMPERATURE] = 1,
0332     .m[PSC_VOLTAGE_OUT] = 1,
0333     .R[PSC_VOLTAGE_OUT] = 3,
0334     .m[PSC_CURRENT_OUT] = 4,
0335     .m[PSC_POWER] = 1,
0336     .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_IOUT |
0337            PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
0338            PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
0339            PMBUS_PHASE_VIRTUAL,
0340     .pfunc[0] = PMBUS_HAVE_IOUT,
0341     .pfunc[1] = PMBUS_HAVE_IOUT,
0342     .pfunc[2] = PMBUS_HAVE_IOUT,
0343     .pfunc[3] = PMBUS_HAVE_IOUT,
0344     .pfunc[4] = PMBUS_HAVE_IOUT,
0345     .pfunc[5] = PMBUS_HAVE_IOUT,
0346     .pfunc[6] = PMBUS_HAVE_IOUT,
0347     .pfunc[7] = PMBUS_HAVE_IOUT,
0348     .pfunc[8] = PMBUS_HAVE_IOUT,
0349     .pfunc[9] = PMBUS_HAVE_IOUT,
0350     .read_byte_data = mp2888_read_byte_data,
0351     .read_word_data = mp2888_read_word_data,
0352     .write_word_data = mp2888_write_word_data,
0353 };
0354 
0355 static int mp2888_probe(struct i2c_client *client)
0356 {
0357     struct pmbus_driver_info *info;
0358     struct mp2888_data *data;
0359     int ret;
0360 
0361     data = devm_kzalloc(&client->dev, sizeof(struct mp2888_data), GFP_KERNEL);
0362     if (!data)
0363         return -ENOMEM;
0364 
0365     memcpy(&data->info, &mp2888_info, sizeof(*info));
0366     info = &data->info;
0367 
0368     /* Identify multiphase configuration. */
0369     ret = mp2888_identify_multiphase(client, data, info);
0370     if (ret)
0371         return ret;
0372 
0373     /* Obtain current sense gain of power stage and current resolution. */
0374     ret = mp2888_current_sense_gain_and_resolution_get(client, data);
0375     if (ret)
0376         return ret;
0377 
0378     return pmbus_do_probe(client, info);
0379 }
0380 
0381 static const struct i2c_device_id mp2888_id[] = {
0382     {"mp2888", 0},
0383     {}
0384 };
0385 
0386 MODULE_DEVICE_TABLE(i2c, mp2888_id);
0387 
0388 static const struct of_device_id __maybe_unused mp2888_of_match[] = {
0389     {.compatible = "mps,mp2888"},
0390     {}
0391 };
0392 MODULE_DEVICE_TABLE(of, mp2888_of_match);
0393 
0394 static struct i2c_driver mp2888_driver = {
0395     .driver = {
0396         .name = "mp2888",
0397         .of_match_table = of_match_ptr(mp2888_of_match),
0398     },
0399     .probe_new = mp2888_probe,
0400     .id_table = mp2888_id,
0401 };
0402 
0403 module_i2c_driver(mp2888_driver);
0404 
0405 MODULE_AUTHOR("Vadim Pasternak <vadimp@nvidia.com>");
0406 MODULE_DESCRIPTION("PMBus driver for MPS MP2888 device");
0407 MODULE_LICENSE("GPL");
0408 MODULE_IMPORT_NS(PMBUS);