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