Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Driver for Texas Instruments INA219, INA226 power monitor chips |
| 4 | * |
| 5 | * INA219: |
| 6 | * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface |
| 7 | * Datasheet: http://www.ti.com/product/ina219 |
| 8 | * |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 9 | * INA220: |
| 10 | * Bi-Directional Current/Power Monitor with I2C Interface |
| 11 | * Datasheet: http://www.ti.com/product/ina220 |
| 12 | * |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 13 | * INA226: |
| 14 | * Bi-Directional Current/Power Monitor with I2C Interface |
| 15 | * Datasheet: http://www.ti.com/product/ina226 |
| 16 | * |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 17 | * INA230: |
| 18 | * Bi-directional Current/Power Monitor with I2C Interface |
| 19 | * Datasheet: http://www.ti.com/product/ina230 |
| 20 | * |
Lothar Felten | 3ad8670 | 2018-08-14 09:09:37 +0200 | [diff] [blame] | 21 | * Copyright (C) 2012 Lothar Felten <lothar.felten@gmail.com> |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 22 | * Thanks to Jan Volkering |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/err.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/i2c.h> |
| 31 | #include <linux/hwmon.h> |
| 32 | #include <linux/hwmon-sysfs.h> |
Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 33 | #include <linux/jiffies.h> |
Javier Martinez Canillas | bd0ddd4 | 2017-02-24 10:13:00 -0300 | [diff] [blame] | 34 | #include <linux/of_device.h> |
Tang Yuantian | 31e7ad7 | 2013-06-19 14:50:20 +0800 | [diff] [blame] | 35 | #include <linux/of.h> |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 36 | #include <linux/delay.h> |
Bartosz Golaszewski | d38df34 | 2015-04-16 12:43:34 -0700 | [diff] [blame] | 37 | #include <linux/util_macros.h> |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 38 | #include <linux/regmap.h> |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 39 | |
| 40 | #include <linux/platform_data/ina2xx.h> |
| 41 | |
| 42 | /* common register definitions */ |
| 43 | #define INA2XX_CONFIG 0x00 |
| 44 | #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */ |
| 45 | #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */ |
| 46 | #define INA2XX_POWER 0x03 /* readonly */ |
| 47 | #define INA2XX_CURRENT 0x04 /* readonly */ |
| 48 | #define INA2XX_CALIBRATION 0x05 |
| 49 | |
| 50 | /* INA226 register definitions */ |
| 51 | #define INA226_MASK_ENABLE 0x06 |
| 52 | #define INA226_ALERT_LIMIT 0x07 |
| 53 | #define INA226_DIE_ID 0xFF |
| 54 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 55 | /* register count */ |
| 56 | #define INA219_REGISTERS 6 |
| 57 | #define INA226_REGISTERS 8 |
| 58 | |
| 59 | #define INA2XX_MAX_REGISTERS 8 |
| 60 | |
| 61 | /* settings - depend on use case */ |
| 62 | #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */ |
| 63 | #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */ |
| 64 | |
| 65 | /* worst case is 68.10 ms (~14.6Hz, ina219) */ |
| 66 | #define INA2XX_CONVERSION_RATE 15 |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 67 | #define INA2XX_MAX_DELAY 69 /* worst case delay in ms */ |
| 68 | |
| 69 | #define INA2XX_RSHUNT_DEFAULT 10000 |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 70 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 71 | /* bit mask for reading the averaging setting in the configuration register */ |
| 72 | #define INA226_AVG_RD_MASK 0x0E00 |
| 73 | |
| 74 | #define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9) |
| 75 | #define INA226_SHIFT_AVG(val) ((val) << 9) |
| 76 | |
| 77 | /* common attrs, ina226 attrs and NULL */ |
| 78 | #define INA2XX_MAX_ATTRIBUTE_GROUPS 3 |
| 79 | |
| 80 | /* |
| 81 | * Both bus voltage and shunt voltage conversion times for ina226 are set |
| 82 | * to 0b0100 on POR, which translates to 2200 microseconds in total. |
| 83 | */ |
| 84 | #define INA226_TOTAL_CONV_TIME_DEFAULT 2200 |
| 85 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 86 | static struct regmap_config ina2xx_regmap_config = { |
| 87 | .reg_bits = 8, |
| 88 | .val_bits = 16, |
| 89 | }; |
| 90 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 91 | enum ina2xx_ids { ina219, ina226 }; |
| 92 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 93 | struct ina2xx_config { |
| 94 | u16 config_default; |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 95 | int calibration_value; |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 96 | int registers; |
| 97 | int shunt_div; |
| 98 | int bus_voltage_shift; |
| 99 | int bus_voltage_lsb; /* uV */ |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 100 | int power_lsb_factor; |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 103 | struct ina2xx_data { |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 104 | const struct ina2xx_config *config; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 105 | |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 106 | long rshunt; |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 107 | long current_lsb_uA; |
| 108 | long power_lsb_uW; |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 109 | struct mutex config_lock; |
| 110 | struct regmap *regmap; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 111 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 112 | const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS]; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 113 | }; |
| 114 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 115 | static const struct ina2xx_config ina2xx_config[] = { |
| 116 | [ina219] = { |
| 117 | .config_default = INA219_CONFIG_DEFAULT, |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 118 | .calibration_value = 4096, |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 119 | .registers = INA219_REGISTERS, |
| 120 | .shunt_div = 100, |
| 121 | .bus_voltage_shift = 3, |
| 122 | .bus_voltage_lsb = 4000, |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 123 | .power_lsb_factor = 20, |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 124 | }, |
| 125 | [ina226] = { |
| 126 | .config_default = INA226_CONFIG_DEFAULT, |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 127 | .calibration_value = 2048, |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 128 | .registers = INA226_REGISTERS, |
| 129 | .shunt_div = 400, |
| 130 | .bus_voltage_shift = 0, |
| 131 | .bus_voltage_lsb = 1250, |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 132 | .power_lsb_factor = 25, |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 133 | }, |
| 134 | }; |
| 135 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 136 | /* |
| 137 | * Available averaging rates for ina226. The indices correspond with |
| 138 | * the bit values expected by the chip (according to the ina226 datasheet, |
| 139 | * table 3 AVG bit settings, found at |
| 140 | * http://www.ti.com/lit/ds/symlink/ina226.pdf. |
| 141 | */ |
| 142 | static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 }; |
| 143 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 144 | static int ina226_reg_to_interval(u16 config) |
| 145 | { |
| 146 | int avg = ina226_avg_tab[INA226_READ_AVG(config)]; |
| 147 | |
| 148 | /* |
| 149 | * Multiply the total conversion time by the number of averages. |
| 150 | * Return the result in milliseconds. |
| 151 | */ |
| 152 | return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000); |
| 153 | } |
| 154 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 155 | /* |
| 156 | * Return the new, shifted AVG field value of CONFIG register, |
| 157 | * to use with regmap_update_bits |
| 158 | */ |
| 159 | static u16 ina226_interval_to_reg(int interval) |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 160 | { |
| 161 | int avg, avg_bits; |
| 162 | |
| 163 | avg = DIV_ROUND_CLOSEST(interval * 1000, |
| 164 | INA226_TOTAL_CONV_TIME_DEFAULT); |
Bartosz Golaszewski | d38df34 | 2015-04-16 12:43:34 -0700 | [diff] [blame] | 165 | avg_bits = find_closest(avg, ina226_avg_tab, |
| 166 | ARRAY_SIZE(ina226_avg_tab)); |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 167 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 168 | return INA226_SHIFT_AVG(avg_bits); |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 171 | /* |
| 172 | * Calibration register is set to the best value, which eliminates |
| 173 | * truncation errors on calculating current register in hardware. |
| 174 | * According to datasheet (eq. 3) the best values are 2048 for |
| 175 | * ina226 and 4096 for ina219. They are hardcoded as calibration_value. |
| 176 | */ |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 177 | static int ina2xx_calibrate(struct ina2xx_data *data) |
| 178 | { |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 179 | return regmap_write(data->regmap, INA2XX_CALIBRATION, |
| 180 | data->config->calibration_value); |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 183 | /* |
| 184 | * Initialize the configuration and calibration registers. |
| 185 | */ |
| 186 | static int ina2xx_init(struct ina2xx_data *data) |
| 187 | { |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 188 | int ret = regmap_write(data->regmap, INA2XX_CONFIG, |
| 189 | data->config->config_default); |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 190 | if (ret < 0) |
| 191 | return ret; |
| 192 | |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 193 | return ina2xx_calibrate(data); |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 196 | static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval) |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 197 | { |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 198 | struct ina2xx_data *data = dev_get_drvdata(dev); |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 199 | int ret, retry; |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 200 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 201 | dev_dbg(dev, "Starting register %d read\n", reg); |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 202 | |
| 203 | for (retry = 5; retry; retry--) { |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 204 | |
| 205 | ret = regmap_read(data->regmap, reg, regval); |
| 206 | if (ret < 0) |
| 207 | return ret; |
| 208 | |
| 209 | dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval); |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * If the current value in the calibration register is 0, the |
| 213 | * power and current registers will also remain at 0. In case |
| 214 | * the chip has been reset let's check the calibration |
| 215 | * register and reinitialize if needed. |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 216 | * We do that extra read of the calibration register if there |
| 217 | * is some hint of a chip reset. |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 218 | */ |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 219 | if (*regval == 0) { |
| 220 | unsigned int cal; |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 221 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 222 | ret = regmap_read(data->regmap, INA2XX_CALIBRATION, |
| 223 | &cal); |
| 224 | if (ret < 0) |
| 225 | return ret; |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 226 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 227 | if (cal == 0) { |
| 228 | dev_warn(dev, "chip not calibrated, reinitializing\n"); |
| 229 | |
| 230 | ret = ina2xx_init(data); |
| 231 | if (ret < 0) |
| 232 | return ret; |
| 233 | /* |
| 234 | * Let's make sure the power and current |
| 235 | * registers have been updated before trying |
| 236 | * again. |
| 237 | */ |
| 238 | msleep(INA2XX_MAX_DELAY); |
| 239 | continue; |
| 240 | } |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 241 | } |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * If we're here then although all write operations succeeded, the |
| 247 | * chip still returns 0 in the calibration register. Nothing more we |
| 248 | * can do here. |
| 249 | */ |
| 250 | dev_err(dev, "unable to reinitialize the chip\n"); |
| 251 | return -ENODEV; |
| 252 | } |
| 253 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 254 | static int ina2xx_get_value(struct ina2xx_data *data, u8 reg, |
| 255 | unsigned int regval) |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 256 | { |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 257 | int val; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 258 | |
| 259 | switch (reg) { |
| 260 | case INA2XX_SHUNT_VOLTAGE: |
Fabio Baltieri | c0214f9 | 2014-06-08 22:06:24 +0100 | [diff] [blame] | 261 | /* signed register */ |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 262 | val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 263 | break; |
| 264 | case INA2XX_BUS_VOLTAGE: |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 265 | val = (regval >> data->config->bus_voltage_shift) |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 266 | * data->config->bus_voltage_lsb; |
| 267 | val = DIV_ROUND_CLOSEST(val, 1000); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 268 | break; |
| 269 | case INA2XX_POWER: |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 270 | val = regval * data->power_lsb_uW; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 271 | break; |
| 272 | case INA2XX_CURRENT: |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 273 | /* signed register, result in mA */ |
Nicolin Chen | 38cd989e | 2018-11-13 19:48:54 -0800 | [diff] [blame] | 274 | val = (s16)regval * data->current_lsb_uA; |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 275 | val = DIV_ROUND_CLOSEST(val, 1000); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 276 | break; |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 277 | case INA2XX_CALIBRATION: |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 278 | val = regval; |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 279 | break; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 280 | default: |
| 281 | /* programmer goofed */ |
| 282 | WARN_ON_ONCE(1); |
| 283 | val = 0; |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | return val; |
| 288 | } |
| 289 | |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 290 | static ssize_t ina2xx_value_show(struct device *dev, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 291 | struct device_attribute *da, char *buf) |
| 292 | { |
| 293 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 294 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 295 | unsigned int regval; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 296 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 297 | int err = ina2xx_read_reg(dev, attr->index, ®val); |
| 298 | |
| 299 | if (err < 0) |
| 300 | return err; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 301 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 302 | return snprintf(buf, PAGE_SIZE, "%d\n", |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 303 | ina2xx_get_value(data, attr->index, regval)); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 304 | } |
| 305 | |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 306 | /* |
| 307 | * In order to keep calibration register value fixed, the product |
| 308 | * of current_lsb and shunt_resistor should also be fixed and equal |
| 309 | * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order |
| 310 | * to keep the scale. |
| 311 | */ |
| 312 | static int ina2xx_set_shunt(struct ina2xx_data *data, long val) |
| 313 | { |
| 314 | unsigned int dividend = DIV_ROUND_CLOSEST(1000000000, |
| 315 | data->config->shunt_div); |
| 316 | if (val <= 0 || val > dividend) |
| 317 | return -EINVAL; |
| 318 | |
| 319 | mutex_lock(&data->config_lock); |
| 320 | data->rshunt = val; |
| 321 | data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val); |
| 322 | data->power_lsb_uW = data->config->power_lsb_factor * |
| 323 | data->current_lsb_uA; |
| 324 | mutex_unlock(&data->config_lock); |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 329 | static ssize_t ina2xx_shunt_show(struct device *dev, |
| 330 | struct device_attribute *da, char *buf) |
Lothar Felten | 3ad8670 | 2018-08-14 09:09:37 +0200 | [diff] [blame] | 331 | { |
| 332 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 333 | |
| 334 | return snprintf(buf, PAGE_SIZE, "%li\n", data->rshunt); |
| 335 | } |
| 336 | |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 337 | static ssize_t ina2xx_shunt_store(struct device *dev, |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 338 | struct device_attribute *da, |
| 339 | const char *buf, size_t count) |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 340 | { |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 341 | unsigned long val; |
| 342 | int status; |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 343 | struct ina2xx_data *data = dev_get_drvdata(dev); |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 344 | |
| 345 | status = kstrtoul(buf, 10, &val); |
| 346 | if (status < 0) |
| 347 | return status; |
| 348 | |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 349 | status = ina2xx_set_shunt(data, val); |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 350 | if (status < 0) |
| 351 | return status; |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 352 | return count; |
| 353 | } |
| 354 | |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 355 | static ssize_t ina226_interval_store(struct device *dev, |
| 356 | struct device_attribute *da, |
| 357 | const char *buf, size_t count) |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 358 | { |
| 359 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 360 | unsigned long val; |
| 361 | int status; |
| 362 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 363 | status = kstrtoul(buf, 10, &val); |
| 364 | if (status < 0) |
| 365 | return status; |
| 366 | |
| 367 | if (val > INT_MAX || val == 0) |
| 368 | return -EINVAL; |
| 369 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 370 | status = regmap_update_bits(data->regmap, INA2XX_CONFIG, |
| 371 | INA226_AVG_RD_MASK, |
| 372 | ina226_interval_to_reg(val)); |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 373 | if (status < 0) |
| 374 | return status; |
| 375 | |
| 376 | return count; |
| 377 | } |
| 378 | |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 379 | static ssize_t ina226_interval_show(struct device *dev, |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 380 | struct device_attribute *da, char *buf) |
| 381 | { |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 382 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 383 | int status; |
| 384 | unsigned int regval; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 385 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 386 | status = regmap_read(data->regmap, INA2XX_CONFIG, ®val); |
| 387 | if (status) |
| 388 | return status; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 389 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 390 | return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval)); |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 393 | /* shunt voltage */ |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 394 | static SENSOR_DEVICE_ATTR_RO(in0_input, ina2xx_value, INA2XX_SHUNT_VOLTAGE); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 395 | |
| 396 | /* bus voltage */ |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 397 | static SENSOR_DEVICE_ATTR_RO(in1_input, ina2xx_value, INA2XX_BUS_VOLTAGE); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 398 | |
| 399 | /* calculated current */ |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 400 | static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 401 | |
| 402 | /* calculated power */ |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 403 | static SENSOR_DEVICE_ATTR_RO(power1_input, ina2xx_value, INA2XX_POWER); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 404 | |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 405 | /* shunt resistance */ |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 406 | static SENSOR_DEVICE_ATTR_RW(shunt_resistor, ina2xx_shunt, INA2XX_CALIBRATION); |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 407 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 408 | /* update interval (ina226 only) */ |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 409 | static SENSOR_DEVICE_ATTR_RW(update_interval, ina226_interval, 0); |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 410 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 411 | /* pointers to created device attributes */ |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 412 | static struct attribute *ina2xx_attrs[] = { |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 413 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 414 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 415 | &sensor_dev_attr_curr1_input.dev_attr.attr, |
| 416 | &sensor_dev_attr_power1_input.dev_attr.attr, |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 417 | &sensor_dev_attr_shunt_resistor.dev_attr.attr, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 418 | NULL, |
| 419 | }; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 420 | |
| 421 | static const struct attribute_group ina2xx_group = { |
| 422 | .attrs = ina2xx_attrs, |
| 423 | }; |
| 424 | |
| 425 | static struct attribute *ina226_attrs[] = { |
| 426 | &sensor_dev_attr_update_interval.dev_attr.attr, |
| 427 | NULL, |
| 428 | }; |
| 429 | |
| 430 | static const struct attribute_group ina226_group = { |
| 431 | .attrs = ina226_attrs, |
| 432 | }; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 433 | |
| 434 | static int ina2xx_probe(struct i2c_client *client, |
| 435 | const struct i2c_device_id *id) |
| 436 | { |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 437 | struct device *dev = &client->dev; |
| 438 | struct ina2xx_data *data; |
| 439 | struct device *hwmon_dev; |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 440 | u32 val; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 441 | int ret, group = 0; |
Javier Martinez Canillas | bd0ddd4 | 2017-02-24 10:13:00 -0300 | [diff] [blame] | 442 | enum ina2xx_ids chip; |
| 443 | |
| 444 | if (client->dev.of_node) |
| 445 | chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev); |
| 446 | else |
| 447 | chip = id->driver_data; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 448 | |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 449 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 450 | if (!data) |
| 451 | return -ENOMEM; |
| 452 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 453 | /* set the device type */ |
Javier Martinez Canillas | bd0ddd4 | 2017-02-24 10:13:00 -0300 | [diff] [blame] | 454 | data->config = &ina2xx_config[chip]; |
Marek Szyprowski | 0c4c586 | 2018-01-15 14:58:21 +0100 | [diff] [blame] | 455 | mutex_init(&data->config_lock); |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 456 | |
Marc Titinger | 001e2e7 | 2015-10-27 10:51:08 +0100 | [diff] [blame] | 457 | if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) { |
| 458 | struct ina2xx_platform_data *pdata = dev_get_platdata(dev); |
| 459 | |
| 460 | if (pdata) |
| 461 | val = pdata->shunt_uohms; |
| 462 | else |
| 463 | val = INA2XX_RSHUNT_DEFAULT; |
| 464 | } |
| 465 | |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 466 | ina2xx_set_shunt(data, val); |
Marc Titinger | 001e2e7 | 2015-10-27 10:51:08 +0100 | [diff] [blame] | 467 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 468 | ina2xx_regmap_config.max_register = data->config->registers; |
| 469 | |
| 470 | data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config); |
| 471 | if (IS_ERR(data->regmap)) { |
| 472 | dev_err(dev, "failed to allocate register map\n"); |
| 473 | return PTR_ERR(data->regmap); |
| 474 | } |
| 475 | |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 476 | ret = ina2xx_init(data); |
| 477 | if (ret < 0) { |
| 478 | dev_err(dev, "error configuring the device: %d\n", ret); |
| 479 | return -ENODEV; |
| 480 | } |
| 481 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 482 | data->groups[group++] = &ina2xx_group; |
Nicolin Chen | 70df9eb | 2018-11-09 16:42:14 -0800 | [diff] [blame] | 483 | if (chip == ina226) |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 484 | data->groups[group++] = &ina226_group; |
| 485 | |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 486 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 487 | data, data->groups); |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 488 | if (IS_ERR(hwmon_dev)) |
| 489 | return PTR_ERR(hwmon_dev); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 490 | |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 491 | dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n", |
Nicolin Chen | 70df9eb | 2018-11-09 16:42:14 -0800 | [diff] [blame] | 492 | client->name, data->rshunt); |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 493 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 494 | return 0; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static const struct i2c_device_id ina2xx_id[] = { |
| 498 | { "ina219", ina219 }, |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 499 | { "ina220", ina219 }, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 500 | { "ina226", ina226 }, |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 501 | { "ina230", ina226 }, |
Kevin Hilman | add513b | 2015-01-14 17:34:58 -0800 | [diff] [blame] | 502 | { "ina231", ina226 }, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 503 | { } |
| 504 | }; |
| 505 | MODULE_DEVICE_TABLE(i2c, ina2xx_id); |
| 506 | |
Guenter Roeck | df6b8c7 | 2019-04-04 08:00:57 -0700 | [diff] [blame] | 507 | static const struct of_device_id __maybe_unused ina2xx_of_match[] = { |
Javier Martinez Canillas | bd0ddd4 | 2017-02-24 10:13:00 -0300 | [diff] [blame] | 508 | { |
| 509 | .compatible = "ti,ina219", |
| 510 | .data = (void *)ina219 |
| 511 | }, |
| 512 | { |
| 513 | .compatible = "ti,ina220", |
| 514 | .data = (void *)ina219 |
| 515 | }, |
| 516 | { |
| 517 | .compatible = "ti,ina226", |
| 518 | .data = (void *)ina226 |
| 519 | }, |
| 520 | { |
| 521 | .compatible = "ti,ina230", |
| 522 | .data = (void *)ina226 |
| 523 | }, |
| 524 | { |
| 525 | .compatible = "ti,ina231", |
| 526 | .data = (void *)ina226 |
| 527 | }, |
| 528 | { }, |
| 529 | }; |
| 530 | MODULE_DEVICE_TABLE(of, ina2xx_of_match); |
| 531 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 532 | static struct i2c_driver ina2xx_driver = { |
| 533 | .driver = { |
| 534 | .name = "ina2xx", |
Javier Martinez Canillas | bd0ddd4 | 2017-02-24 10:13:00 -0300 | [diff] [blame] | 535 | .of_match_table = of_match_ptr(ina2xx_of_match), |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 536 | }, |
| 537 | .probe = ina2xx_probe, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 538 | .id_table = ina2xx_id, |
| 539 | }; |
| 540 | |
Wei Yongjun | d835ca0 | 2012-10-08 20:40:35 +0800 | [diff] [blame] | 541 | module_i2c_driver(ina2xx_driver); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 542 | |
| 543 | MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>"); |
| 544 | MODULE_DESCRIPTION("ina2xx driver"); |
| 545 | MODULE_LICENSE("GPL"); |