Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
0004  * and Digital Power Monitor
0005  *
0006  * Copyright (c) 2011 Ericsson AB.
0007  * Copyright (c) 2018 Guenter Roeck
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/err.h>
0014 #include <linux/slab.h>
0015 #include <linux/i2c.h>
0016 #include <linux/bitops.h>
0017 #include <linux/bitfield.h>
0018 #include <linux/log2.h>
0019 #include "pmbus.h"
0020 
0021 enum chips { adm1075, adm1272, adm1275, adm1276, adm1278, adm1293, adm1294 };
0022 
0023 #define ADM1275_MFR_STATUS_IOUT_WARN2   BIT(0)
0024 #define ADM1293_MFR_STATUS_VAUX_UV_WARN BIT(5)
0025 #define ADM1293_MFR_STATUS_VAUX_OV_WARN BIT(6)
0026 
0027 #define ADM1275_PEAK_IOUT       0xd0
0028 #define ADM1275_PEAK_VIN        0xd1
0029 #define ADM1275_PEAK_VOUT       0xd2
0030 #define ADM1275_PMON_CONFIG     0xd4
0031 
0032 #define ADM1275_VIN_VOUT_SELECT     BIT(6)
0033 #define ADM1275_VRANGE          BIT(5)
0034 #define ADM1075_IRANGE_50       BIT(4)
0035 #define ADM1075_IRANGE_25       BIT(3)
0036 #define ADM1075_IRANGE_MASK     (BIT(3) | BIT(4))
0037 
0038 #define ADM1272_IRANGE          BIT(0)
0039 
0040 #define ADM1278_TEMP1_EN        BIT(3)
0041 #define ADM1278_VIN_EN          BIT(2)
0042 #define ADM1278_VOUT_EN         BIT(1)
0043 
0044 #define ADM1293_IRANGE_25       0
0045 #define ADM1293_IRANGE_50       BIT(6)
0046 #define ADM1293_IRANGE_100      BIT(7)
0047 #define ADM1293_IRANGE_200      (BIT(6) | BIT(7))
0048 #define ADM1293_IRANGE_MASK     (BIT(6) | BIT(7))
0049 
0050 #define ADM1293_VIN_SEL_012     BIT(2)
0051 #define ADM1293_VIN_SEL_074     BIT(3)
0052 #define ADM1293_VIN_SEL_210     (BIT(2) | BIT(3))
0053 #define ADM1293_VIN_SEL_MASK        (BIT(2) | BIT(3))
0054 
0055 #define ADM1293_VAUX_EN         BIT(1)
0056 
0057 #define ADM1278_PEAK_TEMP       0xd7
0058 #define ADM1275_IOUT_WARN2_LIMIT    0xd7
0059 #define ADM1275_DEVICE_CONFIG       0xd8
0060 
0061 #define ADM1275_IOUT_WARN2_SELECT   BIT(4)
0062 
0063 #define ADM1276_PEAK_PIN        0xda
0064 #define ADM1075_READ_VAUX       0xdd
0065 #define ADM1075_VAUX_OV_WARN_LIMIT  0xde
0066 #define ADM1075_VAUX_UV_WARN_LIMIT  0xdf
0067 #define ADM1293_IOUT_MIN        0xe3
0068 #define ADM1293_PIN_MIN         0xe4
0069 #define ADM1075_VAUX_STATUS     0xf6
0070 
0071 #define ADM1075_VAUX_OV_WARN        BIT(7)
0072 #define ADM1075_VAUX_UV_WARN        BIT(6)
0073 
0074 #define ADM1275_VI_AVG_SHIFT        0
0075 #define ADM1275_VI_AVG_MASK     GENMASK(ADM1275_VI_AVG_SHIFT + 2, \
0076                         ADM1275_VI_AVG_SHIFT)
0077 #define ADM1275_SAMPLES_AVG_MAX     128
0078 
0079 #define ADM1278_PWR_AVG_SHIFT       11
0080 #define ADM1278_PWR_AVG_MASK        GENMASK(ADM1278_PWR_AVG_SHIFT + 2, \
0081                         ADM1278_PWR_AVG_SHIFT)
0082 #define ADM1278_VI_AVG_SHIFT        8
0083 #define ADM1278_VI_AVG_MASK     GENMASK(ADM1278_VI_AVG_SHIFT + 2, \
0084                         ADM1278_VI_AVG_SHIFT)
0085 
0086 struct adm1275_data {
0087     int id;
0088     bool have_oc_fault;
0089     bool have_uc_fault;
0090     bool have_vout;
0091     bool have_vaux_status;
0092     bool have_mfr_vaux_status;
0093     bool have_iout_min;
0094     bool have_pin_min;
0095     bool have_pin_max;
0096     bool have_temp_max;
0097     bool have_power_sampling;
0098     struct pmbus_driver_info info;
0099 };
0100 
0101 #define to_adm1275_data(x)  container_of(x, struct adm1275_data, info)
0102 
0103 struct coefficients {
0104     s16 m;
0105     s16 b;
0106     s16 R;
0107 };
0108 
0109 static const struct coefficients adm1075_coefficients[] = {
0110     [0] = { 27169, 0, -1 },     /* voltage */
0111     [1] = { 806, 20475, -1 },   /* current, irange25 */
0112     [2] = { 404, 20475, -1 },   /* current, irange50 */
0113     [3] = { 8549, 0, -1 },      /* power, irange25 */
0114     [4] = { 4279, 0, -1 },      /* power, irange50 */
0115 };
0116 
0117 static const struct coefficients adm1272_coefficients[] = {
0118     [0] = { 6770, 0, -2 },      /* voltage, vrange 60V */
0119     [1] = { 4062, 0, -2 },      /* voltage, vrange 100V */
0120     [2] = { 1326, 20480, -1 },  /* current, vsense range 15mV */
0121     [3] = { 663, 20480, -1 },   /* current, vsense range 30mV */
0122     [4] = { 3512, 0, -2 },      /* power, vrange 60V, irange 15mV */
0123     [5] = { 21071, 0, -3 },     /* power, vrange 100V, irange 15mV */
0124     [6] = { 17561, 0, -3 },     /* power, vrange 60V, irange 30mV */
0125     [7] = { 10535, 0, -3 },     /* power, vrange 100V, irange 30mV */
0126     [8] = { 42, 31871, -1 },    /* temperature */
0127 
0128 };
0129 
0130 static const struct coefficients adm1275_coefficients[] = {
0131     [0] = { 19199, 0, -2 },     /* voltage, vrange set */
0132     [1] = { 6720, 0, -1 },      /* voltage, vrange not set */
0133     [2] = { 807, 20475, -1 },   /* current */
0134 };
0135 
0136 static const struct coefficients adm1276_coefficients[] = {
0137     [0] = { 19199, 0, -2 },     /* voltage, vrange set */
0138     [1] = { 6720, 0, -1 },      /* voltage, vrange not set */
0139     [2] = { 807, 20475, -1 },   /* current */
0140     [3] = { 6043, 0, -2 },      /* power, vrange set */
0141     [4] = { 2115, 0, -1 },      /* power, vrange not set */
0142 };
0143 
0144 static const struct coefficients adm1278_coefficients[] = {
0145     [0] = { 19599, 0, -2 },     /* voltage */
0146     [1] = { 800, 20475, -1 },   /* current */
0147     [2] = { 6123, 0, -2 },      /* power */
0148     [3] = { 42, 31880, -1 },    /* temperature */
0149 };
0150 
0151 static const struct coefficients adm1293_coefficients[] = {
0152     [0] = { 3333, -1, 0 },      /* voltage, vrange 1.2V */
0153     [1] = { 5552, -5, -1 },     /* voltage, vrange 7.4V */
0154     [2] = { 19604, -50, -2 },   /* voltage, vrange 21V */
0155     [3] = { 8000, -100, -2 },   /* current, irange25 */
0156     [4] = { 4000, -100, -2 },   /* current, irange50 */
0157     [5] = { 20000, -1000, -3 }, /* current, irange100 */
0158     [6] = { 10000, -1000, -3 }, /* current, irange200 */
0159     [7] = { 10417, 0, -1 },     /* power, 1.2V, irange25 */
0160     [8] = { 5208, 0, -1 },      /* power, 1.2V, irange50 */
0161     [9] = { 26042, 0, -2 },     /* power, 1.2V, irange100 */
0162     [10] = { 13021, 0, -2 },    /* power, 1.2V, irange200 */
0163     [11] = { 17351, 0, -2 },    /* power, 7.4V, irange25 */
0164     [12] = { 8676, 0, -2 },     /* power, 7.4V, irange50 */
0165     [13] = { 4338, 0, -2 },     /* power, 7.4V, irange100 */
0166     [14] = { 21689, 0, -3 },    /* power, 7.4V, irange200 */
0167     [15] = { 6126, 0, -2 },     /* power, 21V, irange25 */
0168     [16] = { 30631, 0, -3 },    /* power, 21V, irange50 */
0169     [17] = { 15316, 0, -3 },    /* power, 21V, irange100 */
0170     [18] = { 7658, 0, -3 },     /* power, 21V, irange200 */
0171 };
0172 
0173 static int adm1275_read_pmon_config(const struct adm1275_data *data,
0174                     struct i2c_client *client, bool is_power)
0175 {
0176     int shift, ret;
0177     u16 mask;
0178 
0179     /*
0180      * The PMON configuration register is a 16-bit register only on chips
0181      * supporting power average sampling. On other chips it is an 8-bit
0182      * register.
0183      */
0184     if (data->have_power_sampling) {
0185         ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG);
0186         mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK;
0187         shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT;
0188     } else {
0189         ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
0190         mask = ADM1275_VI_AVG_MASK;
0191         shift = ADM1275_VI_AVG_SHIFT;
0192     }
0193     if (ret < 0)
0194         return ret;
0195 
0196     return (ret & mask) >> shift;
0197 }
0198 
0199 static int adm1275_write_pmon_config(const struct adm1275_data *data,
0200                      struct i2c_client *client,
0201                      bool is_power, u16 word)
0202 {
0203     int shift, ret;
0204     u16 mask;
0205 
0206     if (data->have_power_sampling) {
0207         ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG);
0208         mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK;
0209         shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT;
0210     } else {
0211         ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
0212         mask = ADM1275_VI_AVG_MASK;
0213         shift = ADM1275_VI_AVG_SHIFT;
0214     }
0215     if (ret < 0)
0216         return ret;
0217 
0218     word = (ret & ~mask) | ((word << shift) & mask);
0219     if (data->have_power_sampling)
0220         ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG,
0221                         word);
0222     else
0223         ret = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONFIG,
0224                         word);
0225 
0226     return ret;
0227 }
0228 
0229 static int adm1275_read_word_data(struct i2c_client *client, int page,
0230                   int phase, int reg)
0231 {
0232     const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
0233     const struct adm1275_data *data = to_adm1275_data(info);
0234     int ret = 0;
0235 
0236     if (page > 0)
0237         return -ENXIO;
0238 
0239     switch (reg) {
0240     case PMBUS_IOUT_UC_FAULT_LIMIT:
0241         if (!data->have_uc_fault)
0242             return -ENXIO;
0243         ret = pmbus_read_word_data(client, 0, 0xff,
0244                        ADM1275_IOUT_WARN2_LIMIT);
0245         break;
0246     case PMBUS_IOUT_OC_FAULT_LIMIT:
0247         if (!data->have_oc_fault)
0248             return -ENXIO;
0249         ret = pmbus_read_word_data(client, 0, 0xff,
0250                        ADM1275_IOUT_WARN2_LIMIT);
0251         break;
0252     case PMBUS_VOUT_OV_WARN_LIMIT:
0253         if (data->have_vout)
0254             return -ENODATA;
0255         ret = pmbus_read_word_data(client, 0, 0xff,
0256                        ADM1075_VAUX_OV_WARN_LIMIT);
0257         break;
0258     case PMBUS_VOUT_UV_WARN_LIMIT:
0259         if (data->have_vout)
0260             return -ENODATA;
0261         ret = pmbus_read_word_data(client, 0, 0xff,
0262                        ADM1075_VAUX_UV_WARN_LIMIT);
0263         break;
0264     case PMBUS_READ_VOUT:
0265         if (data->have_vout)
0266             return -ENODATA;
0267         ret = pmbus_read_word_data(client, 0, 0xff,
0268                        ADM1075_READ_VAUX);
0269         break;
0270     case PMBUS_VIRT_READ_IOUT_MIN:
0271         if (!data->have_iout_min)
0272             return -ENXIO;
0273         ret = pmbus_read_word_data(client, 0, 0xff,
0274                        ADM1293_IOUT_MIN);
0275         break;
0276     case PMBUS_VIRT_READ_IOUT_MAX:
0277         ret = pmbus_read_word_data(client, 0, 0xff,
0278                        ADM1275_PEAK_IOUT);
0279         break;
0280     case PMBUS_VIRT_READ_VOUT_MAX:
0281         ret = pmbus_read_word_data(client, 0, 0xff,
0282                        ADM1275_PEAK_VOUT);
0283         break;
0284     case PMBUS_VIRT_READ_VIN_MAX:
0285         ret = pmbus_read_word_data(client, 0, 0xff,
0286                        ADM1275_PEAK_VIN);
0287         break;
0288     case PMBUS_VIRT_READ_PIN_MIN:
0289         if (!data->have_pin_min)
0290             return -ENXIO;
0291         ret = pmbus_read_word_data(client, 0, 0xff,
0292                        ADM1293_PIN_MIN);
0293         break;
0294     case PMBUS_VIRT_READ_PIN_MAX:
0295         if (!data->have_pin_max)
0296             return -ENXIO;
0297         ret = pmbus_read_word_data(client, 0, 0xff,
0298                        ADM1276_PEAK_PIN);
0299         break;
0300     case PMBUS_VIRT_READ_TEMP_MAX:
0301         if (!data->have_temp_max)
0302             return -ENXIO;
0303         ret = pmbus_read_word_data(client, 0, 0xff,
0304                        ADM1278_PEAK_TEMP);
0305         break;
0306     case PMBUS_VIRT_RESET_IOUT_HISTORY:
0307     case PMBUS_VIRT_RESET_VOUT_HISTORY:
0308     case PMBUS_VIRT_RESET_VIN_HISTORY:
0309         break;
0310     case PMBUS_VIRT_RESET_PIN_HISTORY:
0311         if (!data->have_pin_max)
0312             return -ENXIO;
0313         break;
0314     case PMBUS_VIRT_RESET_TEMP_HISTORY:
0315         if (!data->have_temp_max)
0316             return -ENXIO;
0317         break;
0318     case PMBUS_VIRT_POWER_SAMPLES:
0319         if (!data->have_power_sampling)
0320             return -ENXIO;
0321         ret = adm1275_read_pmon_config(data, client, true);
0322         if (ret < 0)
0323             break;
0324         ret = BIT(ret);
0325         break;
0326     case PMBUS_VIRT_IN_SAMPLES:
0327     case PMBUS_VIRT_CURR_SAMPLES:
0328         ret = adm1275_read_pmon_config(data, client, false);
0329         if (ret < 0)
0330             break;
0331         ret = BIT(ret);
0332         break;
0333     default:
0334         ret = -ENODATA;
0335         break;
0336     }
0337     return ret;
0338 }
0339 
0340 static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
0341                    u16 word)
0342 {
0343     const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
0344     const struct adm1275_data *data = to_adm1275_data(info);
0345     int ret;
0346 
0347     if (page > 0)
0348         return -ENXIO;
0349 
0350     switch (reg) {
0351     case PMBUS_IOUT_UC_FAULT_LIMIT:
0352     case PMBUS_IOUT_OC_FAULT_LIMIT:
0353         ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
0354                         word);
0355         break;
0356     case PMBUS_VIRT_RESET_IOUT_HISTORY:
0357         ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
0358         if (!ret && data->have_iout_min)
0359             ret = pmbus_write_word_data(client, 0,
0360                             ADM1293_IOUT_MIN, 0);
0361         break;
0362     case PMBUS_VIRT_RESET_VOUT_HISTORY:
0363         ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
0364         break;
0365     case PMBUS_VIRT_RESET_VIN_HISTORY:
0366         ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
0367         break;
0368     case PMBUS_VIRT_RESET_PIN_HISTORY:
0369         ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0);
0370         if (!ret && data->have_pin_min)
0371             ret = pmbus_write_word_data(client, 0,
0372                             ADM1293_PIN_MIN, 0);
0373         break;
0374     case PMBUS_VIRT_RESET_TEMP_HISTORY:
0375         ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0);
0376         break;
0377     case PMBUS_VIRT_POWER_SAMPLES:
0378         if (!data->have_power_sampling)
0379             return -ENXIO;
0380         word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
0381         ret = adm1275_write_pmon_config(data, client, true,
0382                         ilog2(word));
0383         break;
0384     case PMBUS_VIRT_IN_SAMPLES:
0385     case PMBUS_VIRT_CURR_SAMPLES:
0386         word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
0387         ret = adm1275_write_pmon_config(data, client, false,
0388                         ilog2(word));
0389         break;
0390     default:
0391         ret = -ENODATA;
0392         break;
0393     }
0394     return ret;
0395 }
0396 
0397 static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
0398 {
0399     const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
0400     const struct adm1275_data *data = to_adm1275_data(info);
0401     int mfr_status, ret;
0402 
0403     if (page > 0)
0404         return -ENXIO;
0405 
0406     switch (reg) {
0407     case PMBUS_STATUS_IOUT:
0408         ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
0409         if (ret < 0)
0410             break;
0411         if (!data->have_oc_fault && !data->have_uc_fault)
0412             break;
0413         mfr_status = pmbus_read_byte_data(client, page,
0414                           PMBUS_STATUS_MFR_SPECIFIC);
0415         if (mfr_status < 0)
0416             return mfr_status;
0417         if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
0418             ret |= data->have_oc_fault ?
0419               PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
0420         }
0421         break;
0422     case PMBUS_STATUS_VOUT:
0423         if (data->have_vout)
0424             return -ENODATA;
0425         ret = 0;
0426         if (data->have_vaux_status) {
0427             mfr_status = pmbus_read_byte_data(client, 0,
0428                               ADM1075_VAUX_STATUS);
0429             if (mfr_status < 0)
0430                 return mfr_status;
0431             if (mfr_status & ADM1075_VAUX_OV_WARN)
0432                 ret |= PB_VOLTAGE_OV_WARNING;
0433             if (mfr_status & ADM1075_VAUX_UV_WARN)
0434                 ret |= PB_VOLTAGE_UV_WARNING;
0435         } else if (data->have_mfr_vaux_status) {
0436             mfr_status = pmbus_read_byte_data(client, page,
0437                         PMBUS_STATUS_MFR_SPECIFIC);
0438             if (mfr_status < 0)
0439                 return mfr_status;
0440             if (mfr_status & ADM1293_MFR_STATUS_VAUX_OV_WARN)
0441                 ret |= PB_VOLTAGE_OV_WARNING;
0442             if (mfr_status & ADM1293_MFR_STATUS_VAUX_UV_WARN)
0443                 ret |= PB_VOLTAGE_UV_WARNING;
0444         }
0445         break;
0446     default:
0447         ret = -ENODATA;
0448         break;
0449     }
0450     return ret;
0451 }
0452 
0453 static const struct i2c_device_id adm1275_id[] = {
0454     { "adm1075", adm1075 },
0455     { "adm1272", adm1272 },
0456     { "adm1275", adm1275 },
0457     { "adm1276", adm1276 },
0458     { "adm1278", adm1278 },
0459     { "adm1293", adm1293 },
0460     { "adm1294", adm1294 },
0461     { }
0462 };
0463 MODULE_DEVICE_TABLE(i2c, adm1275_id);
0464 
0465 static int adm1275_probe(struct i2c_client *client)
0466 {
0467     s32 (*config_read_fn)(const struct i2c_client *client, u8 reg);
0468     u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
0469     int config, device_config;
0470     int ret;
0471     struct pmbus_driver_info *info;
0472     struct adm1275_data *data;
0473     const struct i2c_device_id *mid;
0474     const struct coefficients *coefficients;
0475     int vindex = -1, voindex = -1, cindex = -1, pindex = -1;
0476     int tindex = -1;
0477     u32 shunt;
0478     u32 avg;
0479 
0480     if (!i2c_check_functionality(client->adapter,
0481                      I2C_FUNC_SMBUS_READ_BYTE_DATA
0482                      | I2C_FUNC_SMBUS_BLOCK_DATA))
0483         return -ENODEV;
0484 
0485     ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
0486     if (ret < 0) {
0487         dev_err(&client->dev, "Failed to read Manufacturer ID\n");
0488         return ret;
0489     }
0490     if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
0491         dev_err(&client->dev, "Unsupported Manufacturer ID\n");
0492         return -ENODEV;
0493     }
0494 
0495     ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
0496     if (ret < 0) {
0497         dev_err(&client->dev, "Failed to read Manufacturer Model\n");
0498         return ret;
0499     }
0500     for (mid = adm1275_id; mid->name[0]; mid++) {
0501         if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
0502             break;
0503     }
0504     if (!mid->name[0]) {
0505         dev_err(&client->dev, "Unsupported device\n");
0506         return -ENODEV;
0507     }
0508 
0509     if (strcmp(client->name, mid->name) != 0)
0510         dev_notice(&client->dev,
0511                "Device mismatch: Configured %s, detected %s\n",
0512                client->name, mid->name);
0513 
0514     if (mid->driver_data == adm1272 || mid->driver_data == adm1278 ||
0515         mid->driver_data == adm1293 || mid->driver_data == adm1294)
0516         config_read_fn = i2c_smbus_read_word_data;
0517     else
0518         config_read_fn = i2c_smbus_read_byte_data;
0519     config = config_read_fn(client, ADM1275_PMON_CONFIG);
0520     if (config < 0)
0521         return config;
0522 
0523     device_config = config_read_fn(client, ADM1275_DEVICE_CONFIG);
0524     if (device_config < 0)
0525         return device_config;
0526 
0527     data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
0528                 GFP_KERNEL);
0529     if (!data)
0530         return -ENOMEM;
0531 
0532     if (of_property_read_u32(client->dev.of_node,
0533                  "shunt-resistor-micro-ohms", &shunt))
0534         shunt = 1000; /* 1 mOhm if not set via DT */
0535 
0536     if (shunt == 0)
0537         return -EINVAL;
0538 
0539     data->id = mid->driver_data;
0540 
0541     info = &data->info;
0542 
0543     info->pages = 1;
0544     info->format[PSC_VOLTAGE_IN] = direct;
0545     info->format[PSC_VOLTAGE_OUT] = direct;
0546     info->format[PSC_CURRENT_OUT] = direct;
0547     info->format[PSC_POWER] = direct;
0548     info->format[PSC_TEMPERATURE] = direct;
0549     info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
0550             PMBUS_HAVE_SAMPLES;
0551 
0552     info->read_word_data = adm1275_read_word_data;
0553     info->read_byte_data = adm1275_read_byte_data;
0554     info->write_word_data = adm1275_write_word_data;
0555 
0556     switch (data->id) {
0557     case adm1075:
0558         if (device_config & ADM1275_IOUT_WARN2_SELECT)
0559             data->have_oc_fault = true;
0560         else
0561             data->have_uc_fault = true;
0562         data->have_pin_max = true;
0563         data->have_vaux_status = true;
0564 
0565         coefficients = adm1075_coefficients;
0566         vindex = 0;
0567         switch (config & ADM1075_IRANGE_MASK) {
0568         case ADM1075_IRANGE_25:
0569             cindex = 1;
0570             pindex = 3;
0571             break;
0572         case ADM1075_IRANGE_50:
0573             cindex = 2;
0574             pindex = 4;
0575             break;
0576         default:
0577             dev_err(&client->dev, "Invalid input current range");
0578             break;
0579         }
0580 
0581         info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
0582           | PMBUS_HAVE_STATUS_INPUT;
0583         if (config & ADM1275_VIN_VOUT_SELECT)
0584             info->func[0] |=
0585               PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
0586         break;
0587     case adm1272:
0588         data->have_vout = true;
0589         data->have_pin_max = true;
0590         data->have_temp_max = true;
0591         data->have_power_sampling = true;
0592 
0593         coefficients = adm1272_coefficients;
0594         vindex = (config & ADM1275_VRANGE) ? 1 : 0;
0595         cindex = (config & ADM1272_IRANGE) ? 3 : 2;
0596         /* pindex depends on the combination of the above */
0597         switch (config & (ADM1275_VRANGE | ADM1272_IRANGE)) {
0598         case 0:
0599         default:
0600             pindex = 4;
0601             break;
0602         case ADM1275_VRANGE:
0603             pindex = 5;
0604             break;
0605         case ADM1272_IRANGE:
0606             pindex = 6;
0607             break;
0608         case ADM1275_VRANGE | ADM1272_IRANGE:
0609             pindex = 7;
0610             break;
0611         }
0612         tindex = 8;
0613 
0614         info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
0615             PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
0616             PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
0617 
0618         /* Enable VOUT & TEMP1 if not enabled (disabled by default) */
0619         if ((config & (ADM1278_VOUT_EN | ADM1278_TEMP1_EN)) !=
0620             (ADM1278_VOUT_EN | ADM1278_TEMP1_EN)) {
0621             config |= ADM1278_VOUT_EN | ADM1278_TEMP1_EN;
0622             ret = i2c_smbus_write_byte_data(client,
0623                             ADM1275_PMON_CONFIG,
0624                             config);
0625             if (ret < 0) {
0626                 dev_err(&client->dev,
0627                     "Failed to enable VOUT monitoring\n");
0628                 return -ENODEV;
0629             }
0630         }
0631         if (config & ADM1278_VIN_EN)
0632             info->func[0] |= PMBUS_HAVE_VIN;
0633         break;
0634     case adm1275:
0635         if (device_config & ADM1275_IOUT_WARN2_SELECT)
0636             data->have_oc_fault = true;
0637         else
0638             data->have_uc_fault = true;
0639         data->have_vout = true;
0640 
0641         coefficients = adm1275_coefficients;
0642         vindex = (config & ADM1275_VRANGE) ? 0 : 1;
0643         cindex = 2;
0644 
0645         if (config & ADM1275_VIN_VOUT_SELECT)
0646             info->func[0] |=
0647               PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
0648         else
0649             info->func[0] |=
0650               PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
0651         break;
0652     case adm1276:
0653         if (device_config & ADM1275_IOUT_WARN2_SELECT)
0654             data->have_oc_fault = true;
0655         else
0656             data->have_uc_fault = true;
0657         data->have_vout = true;
0658         data->have_pin_max = true;
0659 
0660         coefficients = adm1276_coefficients;
0661         vindex = (config & ADM1275_VRANGE) ? 0 : 1;
0662         cindex = 2;
0663         pindex = (config & ADM1275_VRANGE) ? 3 : 4;
0664 
0665         info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
0666           | PMBUS_HAVE_STATUS_INPUT;
0667         if (config & ADM1275_VIN_VOUT_SELECT)
0668             info->func[0] |=
0669               PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
0670         break;
0671     case adm1278:
0672         data->have_vout = true;
0673         data->have_pin_max = true;
0674         data->have_temp_max = true;
0675         data->have_power_sampling = true;
0676 
0677         coefficients = adm1278_coefficients;
0678         vindex = 0;
0679         cindex = 1;
0680         pindex = 2;
0681         tindex = 3;
0682 
0683         info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
0684             PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
0685             PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
0686 
0687         /* Enable VOUT & TEMP1 if not enabled (disabled by default) */
0688         if ((config & (ADM1278_VOUT_EN | ADM1278_TEMP1_EN)) !=
0689             (ADM1278_VOUT_EN | ADM1278_TEMP1_EN)) {
0690             config |= ADM1278_VOUT_EN | ADM1278_TEMP1_EN;
0691             ret = i2c_smbus_write_word_data(client,
0692                             ADM1275_PMON_CONFIG,
0693                             config);
0694             if (ret < 0) {
0695                 dev_err(&client->dev,
0696                     "Failed to enable VOUT monitoring\n");
0697                 return -ENODEV;
0698             }
0699         }
0700 
0701         if (config & ADM1278_VIN_EN)
0702             info->func[0] |= PMBUS_HAVE_VIN;
0703         break;
0704     case adm1293:
0705     case adm1294:
0706         data->have_iout_min = true;
0707         data->have_pin_min = true;
0708         data->have_pin_max = true;
0709         data->have_mfr_vaux_status = true;
0710         data->have_power_sampling = true;
0711 
0712         coefficients = adm1293_coefficients;
0713 
0714         voindex = 0;
0715         switch (config & ADM1293_VIN_SEL_MASK) {
0716         case ADM1293_VIN_SEL_012:   /* 1.2V */
0717             vindex = 0;
0718             break;
0719         case ADM1293_VIN_SEL_074:   /* 7.4V */
0720             vindex = 1;
0721             break;
0722         case ADM1293_VIN_SEL_210:   /* 21V */
0723             vindex = 2;
0724             break;
0725         default:            /* disabled */
0726             break;
0727         }
0728 
0729         switch (config & ADM1293_IRANGE_MASK) {
0730         case ADM1293_IRANGE_25:
0731             cindex = 3;
0732             break;
0733         case ADM1293_IRANGE_50:
0734             cindex = 4;
0735             break;
0736         case ADM1293_IRANGE_100:
0737             cindex = 5;
0738             break;
0739         case ADM1293_IRANGE_200:
0740             cindex = 6;
0741             break;
0742         }
0743 
0744         if (vindex >= 0)
0745             pindex = 7 + vindex * 4 + (cindex - 3);
0746 
0747         if (config & ADM1293_VAUX_EN)
0748             info->func[0] |=
0749                 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
0750 
0751         info->func[0] |= PMBUS_HAVE_PIN |
0752             PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
0753 
0754         break;
0755     default:
0756         dev_err(&client->dev, "Unsupported device\n");
0757         return -ENODEV;
0758     }
0759 
0760     if (data->have_power_sampling &&
0761         of_property_read_u32(client->dev.of_node,
0762                  "adi,power-sample-average", &avg) == 0) {
0763         if (!avg || avg > ADM1275_SAMPLES_AVG_MAX ||
0764             BIT(__fls(avg)) != avg) {
0765             dev_err(&client->dev,
0766                 "Invalid number of power samples");
0767             return -EINVAL;
0768         }
0769         ret = adm1275_write_pmon_config(data, client, true,
0770                         ilog2(avg));
0771         if (ret < 0) {
0772             dev_err(&client->dev,
0773                 "Setting power sample averaging failed with error %d",
0774                 ret);
0775             return ret;
0776         }
0777     }
0778 
0779     if (of_property_read_u32(client->dev.of_node,
0780                 "adi,volt-curr-sample-average", &avg) == 0) {
0781         if (!avg || avg > ADM1275_SAMPLES_AVG_MAX ||
0782             BIT(__fls(avg)) != avg) {
0783             dev_err(&client->dev,
0784                 "Invalid number of voltage/current samples");
0785             return -EINVAL;
0786         }
0787         ret = adm1275_write_pmon_config(data, client, false,
0788                         ilog2(avg));
0789         if (ret < 0) {
0790             dev_err(&client->dev,
0791                 "Setting voltage and current sample averaging failed with error %d",
0792                 ret);
0793             return ret;
0794         }
0795     }
0796 
0797     if (voindex < 0)
0798         voindex = vindex;
0799     if (vindex >= 0) {
0800         info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m;
0801         info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b;
0802         info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R;
0803     }
0804     if (voindex >= 0) {
0805         info->m[PSC_VOLTAGE_OUT] = coefficients[voindex].m;
0806         info->b[PSC_VOLTAGE_OUT] = coefficients[voindex].b;
0807         info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R;
0808     }
0809     if (cindex >= 0) {
0810         /* Scale current with sense resistor value */
0811         info->m[PSC_CURRENT_OUT] =
0812             coefficients[cindex].m * shunt / 1000;
0813         info->b[PSC_CURRENT_OUT] = coefficients[cindex].b;
0814         info->R[PSC_CURRENT_OUT] = coefficients[cindex].R;
0815     }
0816     if (pindex >= 0) {
0817         info->m[PSC_POWER] =
0818             coefficients[pindex].m * shunt / 1000;
0819         info->b[PSC_POWER] = coefficients[pindex].b;
0820         info->R[PSC_POWER] = coefficients[pindex].R;
0821     }
0822     if (tindex >= 0) {
0823         info->m[PSC_TEMPERATURE] = coefficients[tindex].m;
0824         info->b[PSC_TEMPERATURE] = coefficients[tindex].b;
0825         info->R[PSC_TEMPERATURE] = coefficients[tindex].R;
0826     }
0827 
0828     return pmbus_do_probe(client, info);
0829 }
0830 
0831 static struct i2c_driver adm1275_driver = {
0832     .driver = {
0833            .name = "adm1275",
0834            },
0835     .probe_new = adm1275_probe,
0836     .id_table = adm1275_id,
0837 };
0838 
0839 module_i2c_driver(adm1275_driver);
0840 
0841 MODULE_AUTHOR("Guenter Roeck");
0842 MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
0843 MODULE_LICENSE("GPL");
0844 MODULE_IMPORT_NS(PMBUS);