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