Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Driver for Texas Instruments INA219, INA226 power monitor chips
0004  *
0005  * INA219:
0006  * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
0007  * Datasheet: https://www.ti.com/product/ina219
0008  *
0009  * INA220:
0010  * Bi-Directional Current/Power Monitor with I2C Interface
0011  * Datasheet: https://www.ti.com/product/ina220
0012  *
0013  * INA226:
0014  * Bi-Directional Current/Power Monitor with I2C Interface
0015  * Datasheet: https://www.ti.com/product/ina226
0016  *
0017  * INA230:
0018  * Bi-directional Current/Power Monitor with I2C Interface
0019  * Datasheet: https://www.ti.com/product/ina230
0020  *
0021  * Copyright (C) 2012 Lothar Felten <lothar.felten@gmail.com>
0022  * Thanks to Jan Volkering
0023  */
0024 
0025 #include <linux/kernel.h>
0026 #include <linux/module.h>
0027 #include <linux/init.h>
0028 #include <linux/err.h>
0029 #include <linux/slab.h>
0030 #include <linux/i2c.h>
0031 #include <linux/hwmon.h>
0032 #include <linux/hwmon-sysfs.h>
0033 #include <linux/jiffies.h>
0034 #include <linux/of_device.h>
0035 #include <linux/of.h>
0036 #include <linux/delay.h>
0037 #include <linux/util_macros.h>
0038 #include <linux/regmap.h>
0039 
0040 #include <linux/platform_data/ina2xx.h>
0041 
0042 /* common register definitions */
0043 #define INA2XX_CONFIG           0x00
0044 #define INA2XX_SHUNT_VOLTAGE        0x01 /* readonly */
0045 #define INA2XX_BUS_VOLTAGE      0x02 /* readonly */
0046 #define INA2XX_POWER            0x03 /* readonly */
0047 #define INA2XX_CURRENT          0x04 /* readonly */
0048 #define INA2XX_CALIBRATION      0x05
0049 
0050 /* INA226 register definitions */
0051 #define INA226_MASK_ENABLE      0x06
0052 #define INA226_ALERT_LIMIT      0x07
0053 #define INA226_DIE_ID           0xFF
0054 
0055 /* register count */
0056 #define INA219_REGISTERS        6
0057 #define INA226_REGISTERS        8
0058 
0059 #define INA2XX_MAX_REGISTERS        8
0060 
0061 /* settings - depend on use case */
0062 #define INA219_CONFIG_DEFAULT       0x399F  /* PGA=8 */
0063 #define INA226_CONFIG_DEFAULT       0x4527  /* averages=16 */
0064 
0065 /* worst case is 68.10 ms (~14.6Hz, ina219) */
0066 #define INA2XX_CONVERSION_RATE      15
0067 #define INA2XX_MAX_DELAY        69 /* worst case delay in ms */
0068 
0069 #define INA2XX_RSHUNT_DEFAULT       10000
0070 
0071 /* bit mask for reading the averaging setting in the configuration register */
0072 #define INA226_AVG_RD_MASK      0x0E00
0073 
0074 #define INA226_READ_AVG(reg)        (((reg) & INA226_AVG_RD_MASK) >> 9)
0075 #define INA226_SHIFT_AVG(val)       ((val) << 9)
0076 
0077 /* bit number of alert functions in Mask/Enable Register */
0078 #define INA226_SHUNT_OVER_VOLTAGE_BIT   15
0079 #define INA226_SHUNT_UNDER_VOLTAGE_BIT  14
0080 #define INA226_BUS_OVER_VOLTAGE_BIT 13
0081 #define INA226_BUS_UNDER_VOLTAGE_BIT    12
0082 #define INA226_POWER_OVER_LIMIT_BIT 11
0083 
0084 /* bit mask for alert config bits of Mask/Enable Register */
0085 #define INA226_ALERT_CONFIG_MASK    0xFC00
0086 #define INA226_ALERT_FUNCTION_FLAG  BIT(4)
0087 
0088 /* common attrs, ina226 attrs and NULL */
0089 #define INA2XX_MAX_ATTRIBUTE_GROUPS 3
0090 
0091 /*
0092  * Both bus voltage and shunt voltage conversion times for ina226 are set
0093  * to 0b0100 on POR, which translates to 2200 microseconds in total.
0094  */
0095 #define INA226_TOTAL_CONV_TIME_DEFAULT  2200
0096 
0097 static struct regmap_config ina2xx_regmap_config = {
0098     .reg_bits = 8,
0099     .val_bits = 16,
0100 };
0101 
0102 enum ina2xx_ids { ina219, ina226 };
0103 
0104 struct ina2xx_config {
0105     u16 config_default;
0106     int calibration_value;
0107     int registers;
0108     int shunt_div;
0109     int bus_voltage_shift;
0110     int bus_voltage_lsb;    /* uV */
0111     int power_lsb_factor;
0112 };
0113 
0114 struct ina2xx_data {
0115     const struct ina2xx_config *config;
0116 
0117     long rshunt;
0118     long current_lsb_uA;
0119     long power_lsb_uW;
0120     struct mutex config_lock;
0121     struct regmap *regmap;
0122 
0123     const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
0124 };
0125 
0126 static const struct ina2xx_config ina2xx_config[] = {
0127     [ina219] = {
0128         .config_default = INA219_CONFIG_DEFAULT,
0129         .calibration_value = 4096,
0130         .registers = INA219_REGISTERS,
0131         .shunt_div = 100,
0132         .bus_voltage_shift = 3,
0133         .bus_voltage_lsb = 4000,
0134         .power_lsb_factor = 20,
0135     },
0136     [ina226] = {
0137         .config_default = INA226_CONFIG_DEFAULT,
0138         .calibration_value = 2048,
0139         .registers = INA226_REGISTERS,
0140         .shunt_div = 400,
0141         .bus_voltage_shift = 0,
0142         .bus_voltage_lsb = 1250,
0143         .power_lsb_factor = 25,
0144     },
0145 };
0146 
0147 /*
0148  * Available averaging rates for ina226. The indices correspond with
0149  * the bit values expected by the chip (according to the ina226 datasheet,
0150  * table 3 AVG bit settings, found at
0151  * https://www.ti.com/lit/ds/symlink/ina226.pdf.
0152  */
0153 static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
0154 
0155 static int ina226_reg_to_interval(u16 config)
0156 {
0157     int avg = ina226_avg_tab[INA226_READ_AVG(config)];
0158 
0159     /*
0160      * Multiply the total conversion time by the number of averages.
0161      * Return the result in milliseconds.
0162      */
0163     return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
0164 }
0165 
0166 /*
0167  * Return the new, shifted AVG field value of CONFIG register,
0168  * to use with regmap_update_bits
0169  */
0170 static u16 ina226_interval_to_reg(int interval)
0171 {
0172     int avg, avg_bits;
0173 
0174     avg = DIV_ROUND_CLOSEST(interval * 1000,
0175                 INA226_TOTAL_CONV_TIME_DEFAULT);
0176     avg_bits = find_closest(avg, ina226_avg_tab,
0177                 ARRAY_SIZE(ina226_avg_tab));
0178 
0179     return INA226_SHIFT_AVG(avg_bits);
0180 }
0181 
0182 /*
0183  * Calibration register is set to the best value, which eliminates
0184  * truncation errors on calculating current register in hardware.
0185  * According to datasheet (eq. 3) the best values are 2048 for
0186  * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
0187  */
0188 static int ina2xx_calibrate(struct ina2xx_data *data)
0189 {
0190     return regmap_write(data->regmap, INA2XX_CALIBRATION,
0191                 data->config->calibration_value);
0192 }
0193 
0194 /*
0195  * Initialize the configuration and calibration registers.
0196  */
0197 static int ina2xx_init(struct ina2xx_data *data)
0198 {
0199     int ret = regmap_write(data->regmap, INA2XX_CONFIG,
0200                    data->config->config_default);
0201     if (ret < 0)
0202         return ret;
0203 
0204     return ina2xx_calibrate(data);
0205 }
0206 
0207 static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
0208 {
0209     struct ina2xx_data *data = dev_get_drvdata(dev);
0210     int ret, retry;
0211 
0212     dev_dbg(dev, "Starting register %d read\n", reg);
0213 
0214     for (retry = 5; retry; retry--) {
0215 
0216         ret = regmap_read(data->regmap, reg, regval);
0217         if (ret < 0)
0218             return ret;
0219 
0220         dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
0221 
0222         /*
0223          * If the current value in the calibration register is 0, the
0224          * power and current registers will also remain at 0. In case
0225          * the chip has been reset let's check the calibration
0226          * register and reinitialize if needed.
0227          * We do that extra read of the calibration register if there
0228          * is some hint of a chip reset.
0229          */
0230         if (*regval == 0) {
0231             unsigned int cal;
0232 
0233             ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
0234                       &cal);
0235             if (ret < 0)
0236                 return ret;
0237 
0238             if (cal == 0) {
0239                 dev_warn(dev, "chip not calibrated, reinitializing\n");
0240 
0241                 ret = ina2xx_init(data);
0242                 if (ret < 0)
0243                     return ret;
0244                 /*
0245                  * Let's make sure the power and current
0246                  * registers have been updated before trying
0247                  * again.
0248                  */
0249                 msleep(INA2XX_MAX_DELAY);
0250                 continue;
0251             }
0252         }
0253         return 0;
0254     }
0255 
0256     /*
0257      * If we're here then although all write operations succeeded, the
0258      * chip still returns 0 in the calibration register. Nothing more we
0259      * can do here.
0260      */
0261     dev_err(dev, "unable to reinitialize the chip\n");
0262     return -ENODEV;
0263 }
0264 
0265 static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
0266                 unsigned int regval)
0267 {
0268     int val;
0269 
0270     switch (reg) {
0271     case INA2XX_SHUNT_VOLTAGE:
0272         /* signed register */
0273         val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
0274         break;
0275     case INA2XX_BUS_VOLTAGE:
0276         val = (regval >> data->config->bus_voltage_shift)
0277           * data->config->bus_voltage_lsb;
0278         val = DIV_ROUND_CLOSEST(val, 1000);
0279         break;
0280     case INA2XX_POWER:
0281         val = regval * data->power_lsb_uW;
0282         break;
0283     case INA2XX_CURRENT:
0284         /* signed register, result in mA */
0285         val = (s16)regval * data->current_lsb_uA;
0286         val = DIV_ROUND_CLOSEST(val, 1000);
0287         break;
0288     case INA2XX_CALIBRATION:
0289         val = regval;
0290         break;
0291     default:
0292         /* programmer goofed */
0293         WARN_ON_ONCE(1);
0294         val = 0;
0295         break;
0296     }
0297 
0298     return val;
0299 }
0300 
0301 static ssize_t ina2xx_value_show(struct device *dev,
0302                  struct device_attribute *da, char *buf)
0303 {
0304     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0305     struct ina2xx_data *data = dev_get_drvdata(dev);
0306     unsigned int regval;
0307 
0308     int err = ina2xx_read_reg(dev, attr->index, &regval);
0309 
0310     if (err < 0)
0311         return err;
0312 
0313     return sysfs_emit(buf, "%d\n", ina2xx_get_value(data, attr->index, regval));
0314 }
0315 
0316 static int ina226_reg_to_alert(struct ina2xx_data *data, u8 bit, u16 regval)
0317 {
0318     int reg;
0319 
0320     switch (bit) {
0321     case INA226_SHUNT_OVER_VOLTAGE_BIT:
0322     case INA226_SHUNT_UNDER_VOLTAGE_BIT:
0323         reg = INA2XX_SHUNT_VOLTAGE;
0324         break;
0325     case INA226_BUS_OVER_VOLTAGE_BIT:
0326     case INA226_BUS_UNDER_VOLTAGE_BIT:
0327         reg = INA2XX_BUS_VOLTAGE;
0328         break;
0329     case INA226_POWER_OVER_LIMIT_BIT:
0330         reg = INA2XX_POWER;
0331         break;
0332     default:
0333         /* programmer goofed */
0334         WARN_ON_ONCE(1);
0335         return 0;
0336     }
0337 
0338     return ina2xx_get_value(data, reg, regval);
0339 }
0340 
0341 /*
0342  * Turns alert limit values into register values.
0343  * Opposite of the formula in ina2xx_get_value().
0344  */
0345 static s16 ina226_alert_to_reg(struct ina2xx_data *data, u8 bit, int val)
0346 {
0347     switch (bit) {
0348     case INA226_SHUNT_OVER_VOLTAGE_BIT:
0349     case INA226_SHUNT_UNDER_VOLTAGE_BIT:
0350         val *= data->config->shunt_div;
0351         return clamp_val(val, SHRT_MIN, SHRT_MAX);
0352     case INA226_BUS_OVER_VOLTAGE_BIT:
0353     case INA226_BUS_UNDER_VOLTAGE_BIT:
0354         val = (val * 1000) << data->config->bus_voltage_shift;
0355         val = DIV_ROUND_CLOSEST(val, data->config->bus_voltage_lsb);
0356         return clamp_val(val, 0, SHRT_MAX);
0357     case INA226_POWER_OVER_LIMIT_BIT:
0358         val = DIV_ROUND_CLOSEST(val, data->power_lsb_uW);
0359         return clamp_val(val, 0, USHRT_MAX);
0360     default:
0361         /* programmer goofed */
0362         WARN_ON_ONCE(1);
0363         return 0;
0364     }
0365 }
0366 
0367 static ssize_t ina226_alert_show(struct device *dev,
0368                  struct device_attribute *da, char *buf)
0369 {
0370     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0371     struct ina2xx_data *data = dev_get_drvdata(dev);
0372     int regval;
0373     int val = 0;
0374     int ret;
0375 
0376     mutex_lock(&data->config_lock);
0377     ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
0378     if (ret)
0379         goto abort;
0380 
0381     if (regval & BIT(attr->index)) {
0382         ret = regmap_read(data->regmap, INA226_ALERT_LIMIT, &regval);
0383         if (ret)
0384             goto abort;
0385         val = ina226_reg_to_alert(data, attr->index, regval);
0386     }
0387 
0388     ret = sysfs_emit(buf, "%d\n", val);
0389 abort:
0390     mutex_unlock(&data->config_lock);
0391     return ret;
0392 }
0393 
0394 static ssize_t ina226_alert_store(struct device *dev,
0395                   struct device_attribute *da,
0396                   const char *buf, size_t count)
0397 {
0398     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0399     struct ina2xx_data *data = dev_get_drvdata(dev);
0400     unsigned long val;
0401     int ret;
0402 
0403     ret = kstrtoul(buf, 10, &val);
0404     if (ret < 0)
0405         return ret;
0406 
0407     /*
0408      * Clear all alerts first to avoid accidentally triggering ALERT pin
0409      * due to register write sequence. Then, only enable the alert
0410      * if the value is non-zero.
0411      */
0412     mutex_lock(&data->config_lock);
0413     ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
0414                  INA226_ALERT_CONFIG_MASK, 0);
0415     if (ret < 0)
0416         goto abort;
0417 
0418     ret = regmap_write(data->regmap, INA226_ALERT_LIMIT,
0419                ina226_alert_to_reg(data, attr->index, val));
0420     if (ret < 0)
0421         goto abort;
0422 
0423     if (val != 0) {
0424         ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
0425                      INA226_ALERT_CONFIG_MASK,
0426                      BIT(attr->index));
0427         if (ret < 0)
0428             goto abort;
0429     }
0430 
0431     ret = count;
0432 abort:
0433     mutex_unlock(&data->config_lock);
0434     return ret;
0435 }
0436 
0437 static ssize_t ina226_alarm_show(struct device *dev,
0438                  struct device_attribute *da, char *buf)
0439 {
0440     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0441     struct ina2xx_data *data = dev_get_drvdata(dev);
0442     int regval;
0443     int alarm = 0;
0444     int ret;
0445 
0446     ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
0447     if (ret)
0448         return ret;
0449 
0450     alarm = (regval & BIT(attr->index)) &&
0451         (regval & INA226_ALERT_FUNCTION_FLAG);
0452     return sysfs_emit(buf, "%d\n", alarm);
0453 }
0454 
0455 /*
0456  * In order to keep calibration register value fixed, the product
0457  * of current_lsb and shunt_resistor should also be fixed and equal
0458  * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
0459  * to keep the scale.
0460  */
0461 static int ina2xx_set_shunt(struct ina2xx_data *data, long val)
0462 {
0463     unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
0464                           data->config->shunt_div);
0465     if (val <= 0 || val > dividend)
0466         return -EINVAL;
0467 
0468     mutex_lock(&data->config_lock);
0469     data->rshunt = val;
0470     data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
0471     data->power_lsb_uW = data->config->power_lsb_factor *
0472                  data->current_lsb_uA;
0473     mutex_unlock(&data->config_lock);
0474 
0475     return 0;
0476 }
0477 
0478 static ssize_t ina2xx_shunt_show(struct device *dev,
0479                  struct device_attribute *da, char *buf)
0480 {
0481     struct ina2xx_data *data = dev_get_drvdata(dev);
0482 
0483     return sysfs_emit(buf, "%li\n", data->rshunt);
0484 }
0485 
0486 static ssize_t ina2xx_shunt_store(struct device *dev,
0487                   struct device_attribute *da,
0488                   const char *buf, size_t count)
0489 {
0490     unsigned long val;
0491     int status;
0492     struct ina2xx_data *data = dev_get_drvdata(dev);
0493 
0494     status = kstrtoul(buf, 10, &val);
0495     if (status < 0)
0496         return status;
0497 
0498     status = ina2xx_set_shunt(data, val);
0499     if (status < 0)
0500         return status;
0501     return count;
0502 }
0503 
0504 static ssize_t ina226_interval_store(struct device *dev,
0505                      struct device_attribute *da,
0506                      const char *buf, size_t count)
0507 {
0508     struct ina2xx_data *data = dev_get_drvdata(dev);
0509     unsigned long val;
0510     int status;
0511 
0512     status = kstrtoul(buf, 10, &val);
0513     if (status < 0)
0514         return status;
0515 
0516     if (val > INT_MAX || val == 0)
0517         return -EINVAL;
0518 
0519     status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
0520                     INA226_AVG_RD_MASK,
0521                     ina226_interval_to_reg(val));
0522     if (status < 0)
0523         return status;
0524 
0525     return count;
0526 }
0527 
0528 static ssize_t ina226_interval_show(struct device *dev,
0529                     struct device_attribute *da, char *buf)
0530 {
0531     struct ina2xx_data *data = dev_get_drvdata(dev);
0532     int status;
0533     unsigned int regval;
0534 
0535     status = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
0536     if (status)
0537         return status;
0538 
0539     return sysfs_emit(buf, "%d\n", ina226_reg_to_interval(regval));
0540 }
0541 
0542 /* shunt voltage */
0543 static SENSOR_DEVICE_ATTR_RO(in0_input, ina2xx_value, INA2XX_SHUNT_VOLTAGE);
0544 /* shunt voltage over/under voltage alert setting and alarm */
0545 static SENSOR_DEVICE_ATTR_RW(in0_crit, ina226_alert,
0546                  INA226_SHUNT_OVER_VOLTAGE_BIT);
0547 static SENSOR_DEVICE_ATTR_RW(in0_lcrit, ina226_alert,
0548                  INA226_SHUNT_UNDER_VOLTAGE_BIT);
0549 static SENSOR_DEVICE_ATTR_RO(in0_crit_alarm, ina226_alarm,
0550                  INA226_SHUNT_OVER_VOLTAGE_BIT);
0551 static SENSOR_DEVICE_ATTR_RO(in0_lcrit_alarm, ina226_alarm,
0552                  INA226_SHUNT_UNDER_VOLTAGE_BIT);
0553 
0554 /* bus voltage */
0555 static SENSOR_DEVICE_ATTR_RO(in1_input, ina2xx_value, INA2XX_BUS_VOLTAGE);
0556 /* bus voltage over/under voltage alert setting and alarm */
0557 static SENSOR_DEVICE_ATTR_RW(in1_crit, ina226_alert,
0558                  INA226_BUS_OVER_VOLTAGE_BIT);
0559 static SENSOR_DEVICE_ATTR_RW(in1_lcrit, ina226_alert,
0560                  INA226_BUS_UNDER_VOLTAGE_BIT);
0561 static SENSOR_DEVICE_ATTR_RO(in1_crit_alarm, ina226_alarm,
0562                  INA226_BUS_OVER_VOLTAGE_BIT);
0563 static SENSOR_DEVICE_ATTR_RO(in1_lcrit_alarm, ina226_alarm,
0564                  INA226_BUS_UNDER_VOLTAGE_BIT);
0565 
0566 /* calculated current */
0567 static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT);
0568 
0569 /* calculated power */
0570 static SENSOR_DEVICE_ATTR_RO(power1_input, ina2xx_value, INA2XX_POWER);
0571 /* over-limit power alert setting and alarm */
0572 static SENSOR_DEVICE_ATTR_RW(power1_crit, ina226_alert,
0573                  INA226_POWER_OVER_LIMIT_BIT);
0574 static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina226_alarm,
0575                  INA226_POWER_OVER_LIMIT_BIT);
0576 
0577 /* shunt resistance */
0578 static SENSOR_DEVICE_ATTR_RW(shunt_resistor, ina2xx_shunt, INA2XX_CALIBRATION);
0579 
0580 /* update interval (ina226 only) */
0581 static SENSOR_DEVICE_ATTR_RW(update_interval, ina226_interval, 0);
0582 
0583 /* pointers to created device attributes */
0584 static struct attribute *ina2xx_attrs[] = {
0585     &sensor_dev_attr_in0_input.dev_attr.attr,
0586     &sensor_dev_attr_in1_input.dev_attr.attr,
0587     &sensor_dev_attr_curr1_input.dev_attr.attr,
0588     &sensor_dev_attr_power1_input.dev_attr.attr,
0589     &sensor_dev_attr_shunt_resistor.dev_attr.attr,
0590     NULL,
0591 };
0592 
0593 static const struct attribute_group ina2xx_group = {
0594     .attrs = ina2xx_attrs,
0595 };
0596 
0597 static struct attribute *ina226_attrs[] = {
0598     &sensor_dev_attr_in0_crit.dev_attr.attr,
0599     &sensor_dev_attr_in0_lcrit.dev_attr.attr,
0600     &sensor_dev_attr_in0_crit_alarm.dev_attr.attr,
0601     &sensor_dev_attr_in0_lcrit_alarm.dev_attr.attr,
0602     &sensor_dev_attr_in1_crit.dev_attr.attr,
0603     &sensor_dev_attr_in1_lcrit.dev_attr.attr,
0604     &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
0605     &sensor_dev_attr_in1_lcrit_alarm.dev_attr.attr,
0606     &sensor_dev_attr_power1_crit.dev_attr.attr,
0607     &sensor_dev_attr_power1_crit_alarm.dev_attr.attr,
0608     &sensor_dev_attr_update_interval.dev_attr.attr,
0609     NULL,
0610 };
0611 
0612 static const struct attribute_group ina226_group = {
0613     .attrs = ina226_attrs,
0614 };
0615 
0616 static const struct i2c_device_id ina2xx_id[];
0617 
0618 static int ina2xx_probe(struct i2c_client *client)
0619 {
0620     struct device *dev = &client->dev;
0621     struct ina2xx_data *data;
0622     struct device *hwmon_dev;
0623     u32 val;
0624     int ret, group = 0;
0625     enum ina2xx_ids chip;
0626 
0627     if (client->dev.of_node)
0628         chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
0629     else
0630         chip = i2c_match_id(ina2xx_id, client)->driver_data;
0631 
0632     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0633     if (!data)
0634         return -ENOMEM;
0635 
0636     /* set the device type */
0637     data->config = &ina2xx_config[chip];
0638     mutex_init(&data->config_lock);
0639 
0640     if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
0641         struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
0642 
0643         if (pdata)
0644             val = pdata->shunt_uohms;
0645         else
0646             val = INA2XX_RSHUNT_DEFAULT;
0647     }
0648 
0649     ina2xx_set_shunt(data, val);
0650 
0651     ina2xx_regmap_config.max_register = data->config->registers;
0652 
0653     data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
0654     if (IS_ERR(data->regmap)) {
0655         dev_err(dev, "failed to allocate register map\n");
0656         return PTR_ERR(data->regmap);
0657     }
0658 
0659     ret = ina2xx_init(data);
0660     if (ret < 0) {
0661         dev_err(dev, "error configuring the device: %d\n", ret);
0662         return -ENODEV;
0663     }
0664 
0665     data->groups[group++] = &ina2xx_group;
0666     if (chip == ina226)
0667         data->groups[group++] = &ina226_group;
0668 
0669     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0670                                data, data->groups);
0671     if (IS_ERR(hwmon_dev))
0672         return PTR_ERR(hwmon_dev);
0673 
0674     dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
0675          client->name, data->rshunt);
0676 
0677     return 0;
0678 }
0679 
0680 static const struct i2c_device_id ina2xx_id[] = {
0681     { "ina219", ina219 },
0682     { "ina220", ina219 },
0683     { "ina226", ina226 },
0684     { "ina230", ina226 },
0685     { "ina231", ina226 },
0686     { }
0687 };
0688 MODULE_DEVICE_TABLE(i2c, ina2xx_id);
0689 
0690 static const struct of_device_id __maybe_unused ina2xx_of_match[] = {
0691     {
0692         .compatible = "ti,ina219",
0693         .data = (void *)ina219
0694     },
0695     {
0696         .compatible = "ti,ina220",
0697         .data = (void *)ina219
0698     },
0699     {
0700         .compatible = "ti,ina226",
0701         .data = (void *)ina226
0702     },
0703     {
0704         .compatible = "ti,ina230",
0705         .data = (void *)ina226
0706     },
0707     {
0708         .compatible = "ti,ina231",
0709         .data = (void *)ina226
0710     },
0711     { },
0712 };
0713 MODULE_DEVICE_TABLE(of, ina2xx_of_match);
0714 
0715 static struct i2c_driver ina2xx_driver = {
0716     .driver = {
0717         .name   = "ina2xx",
0718         .of_match_table = of_match_ptr(ina2xx_of_match),
0719     },
0720     .probe_new  = ina2xx_probe,
0721     .id_table   = ina2xx_id,
0722 };
0723 
0724 module_i2c_driver(ina2xx_driver);
0725 
0726 MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
0727 MODULE_DESCRIPTION("ina2xx driver");
0728 MODULE_LICENSE("GPL");