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> |
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> |
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 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 86 | enum ina2xx_ids { ina219, ina226 }; |
| 87 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 88 | struct ina2xx_config { |
| 89 | u16 config_default; |
| 90 | int calibration_factor; |
| 91 | int registers; |
| 92 | int shunt_div; |
| 93 | int bus_voltage_shift; |
| 94 | int bus_voltage_lsb; /* uV */ |
| 95 | int power_lsb; /* uW */ |
| 96 | }; |
| 97 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 98 | struct ina2xx_data { |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 99 | struct i2c_client *client; |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 100 | const struct ina2xx_config *config; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 101 | |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 102 | long rshunt; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 103 | u16 curr_config; |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 104 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 105 | struct mutex update_lock; |
| 106 | bool valid; |
| 107 | unsigned long last_updated; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 108 | int update_interval; /* in jiffies */ |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 109 | |
| 110 | int kind; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 111 | const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS]; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 112 | u16 regs[INA2XX_MAX_REGISTERS]; |
| 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, |
| 118 | .calibration_factor = 40960000, |
| 119 | .registers = INA219_REGISTERS, |
| 120 | .shunt_div = 100, |
| 121 | .bus_voltage_shift = 3, |
| 122 | .bus_voltage_lsb = 4000, |
| 123 | .power_lsb = 20000, |
| 124 | }, |
| 125 | [ina226] = { |
| 126 | .config_default = INA226_CONFIG_DEFAULT, |
| 127 | .calibration_factor = 5120000, |
| 128 | .registers = INA226_REGISTERS, |
| 129 | .shunt_div = 400, |
| 130 | .bus_voltage_shift = 0, |
| 131 | .bus_voltage_lsb = 1250, |
| 132 | .power_lsb = 25000, |
| 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 | |
| 144 | static int ina226_avg_bits(int avg) |
| 145 | { |
| 146 | int i; |
| 147 | |
| 148 | /* Get the closest average from the tab. */ |
| 149 | for (i = 0; i < ARRAY_SIZE(ina226_avg_tab) - 1; i++) { |
| 150 | if (avg <= (ina226_avg_tab[i] + ina226_avg_tab[i + 1]) / 2) |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | return i; /* Return 0b0111 for values greater than 1024. */ |
| 155 | } |
| 156 | |
| 157 | static int ina226_reg_to_interval(u16 config) |
| 158 | { |
| 159 | int avg = ina226_avg_tab[INA226_READ_AVG(config)]; |
| 160 | |
| 161 | /* |
| 162 | * Multiply the total conversion time by the number of averages. |
| 163 | * Return the result in milliseconds. |
| 164 | */ |
| 165 | return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000); |
| 166 | } |
| 167 | |
| 168 | static u16 ina226_interval_to_reg(int interval, u16 config) |
| 169 | { |
| 170 | int avg, avg_bits; |
| 171 | |
| 172 | avg = DIV_ROUND_CLOSEST(interval * 1000, |
| 173 | INA226_TOTAL_CONV_TIME_DEFAULT); |
| 174 | avg_bits = ina226_avg_bits(avg); |
| 175 | |
| 176 | return (config & ~INA226_AVG_RD_MASK) | INA226_SHIFT_AVG(avg_bits); |
| 177 | } |
| 178 | |
| 179 | static void ina226_set_update_interval(struct ina2xx_data *data) |
| 180 | { |
| 181 | int ms; |
| 182 | |
| 183 | ms = ina226_reg_to_interval(data->curr_config); |
| 184 | data->update_interval = msecs_to_jiffies(ms); |
| 185 | } |
| 186 | |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 187 | static int ina2xx_calibrate(struct ina2xx_data *data) |
| 188 | { |
Bartosz Golaszewski | b721fe2 | 2015-01-12 14:47:22 +0100 | [diff] [blame] | 189 | u16 val = DIV_ROUND_CLOSEST(data->config->calibration_factor, |
| 190 | data->rshunt); |
| 191 | |
| 192 | return i2c_smbus_write_word_swapped(data->client, |
| 193 | INA2XX_CALIBRATION, val); |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 196 | /* |
| 197 | * Initialize the configuration and calibration registers. |
| 198 | */ |
| 199 | static int ina2xx_init(struct ina2xx_data *data) |
| 200 | { |
| 201 | struct i2c_client *client = data->client; |
| 202 | int ret; |
| 203 | |
| 204 | /* device configuration */ |
| 205 | ret = i2c_smbus_write_word_swapped(client, INA2XX_CONFIG, |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 206 | data->curr_config); |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 207 | if (ret < 0) |
| 208 | return ret; |
| 209 | |
| 210 | /* |
| 211 | * Set current LSB to 1mA, shunt is in uOhms |
| 212 | * (equation 13 in datasheet). |
| 213 | */ |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 214 | return ina2xx_calibrate(data); |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static int ina2xx_do_update(struct device *dev) |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 218 | { |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 219 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 220 | struct i2c_client *client = data->client; |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 221 | int i, rv, retry; |
| 222 | |
| 223 | dev_dbg(&client->dev, "Starting ina2xx update\n"); |
| 224 | |
| 225 | for (retry = 5; retry; retry--) { |
| 226 | /* Read all registers */ |
| 227 | for (i = 0; i < data->config->registers; i++) { |
| 228 | rv = i2c_smbus_read_word_swapped(client, i); |
| 229 | if (rv < 0) |
| 230 | return rv; |
| 231 | data->regs[i] = rv; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * If the current value in the calibration register is 0, the |
| 236 | * power and current registers will also remain at 0. In case |
| 237 | * the chip has been reset let's check the calibration |
| 238 | * register and reinitialize if needed. |
| 239 | */ |
| 240 | if (data->regs[INA2XX_CALIBRATION] == 0) { |
| 241 | dev_warn(dev, "chip not calibrated, reinitializing\n"); |
| 242 | |
| 243 | rv = ina2xx_init(data); |
| 244 | if (rv < 0) |
| 245 | return rv; |
| 246 | |
| 247 | /* |
| 248 | * Let's make sure the power and current registers |
| 249 | * have been updated before trying again. |
| 250 | */ |
| 251 | msleep(INA2XX_MAX_DELAY); |
| 252 | continue; |
| 253 | } |
| 254 | |
| 255 | data->last_updated = jiffies; |
| 256 | data->valid = 1; |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * If we're here then although all write operations succeeded, the |
| 263 | * chip still returns 0 in the calibration register. Nothing more we |
| 264 | * can do here. |
| 265 | */ |
| 266 | dev_err(dev, "unable to reinitialize the chip\n"); |
| 267 | return -ENODEV; |
| 268 | } |
| 269 | |
| 270 | static struct ina2xx_data *ina2xx_update_device(struct device *dev) |
| 271 | { |
| 272 | struct ina2xx_data *data = dev_get_drvdata(dev); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 273 | struct ina2xx_data *ret = data; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 274 | unsigned long after; |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 275 | int rv; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 276 | |
| 277 | mutex_lock(&data->update_lock); |
| 278 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 279 | after = data->last_updated + data->update_interval; |
| 280 | if (time_after(jiffies, after) || !data->valid) { |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 281 | rv = ina2xx_do_update(dev); |
| 282 | if (rv < 0) |
| 283 | ret = ERR_PTR(rv); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 284 | } |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 285 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 286 | mutex_unlock(&data->update_lock); |
| 287 | return ret; |
| 288 | } |
| 289 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 290 | static int ina2xx_get_value(struct ina2xx_data *data, u8 reg) |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 291 | { |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 292 | int val; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 293 | |
| 294 | switch (reg) { |
| 295 | case INA2XX_SHUNT_VOLTAGE: |
Fabio Baltieri | c0214f9 | 2014-06-08 22:06:24 +0100 | [diff] [blame] | 296 | /* signed register */ |
| 297 | val = DIV_ROUND_CLOSEST((s16)data->regs[reg], |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 298 | data->config->shunt_div); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 299 | break; |
| 300 | case INA2XX_BUS_VOLTAGE: |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 301 | val = (data->regs[reg] >> data->config->bus_voltage_shift) |
| 302 | * data->config->bus_voltage_lsb; |
| 303 | val = DIV_ROUND_CLOSEST(val, 1000); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 304 | break; |
| 305 | case INA2XX_POWER: |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 306 | val = data->regs[reg] * data->config->power_lsb; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 307 | break; |
| 308 | case INA2XX_CURRENT: |
Fabio Baltieri | c0214f9 | 2014-06-08 22:06:24 +0100 | [diff] [blame] | 309 | /* signed register, LSB=1mA (selected), in mA */ |
| 310 | val = (s16)data->regs[reg]; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 311 | break; |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 312 | case INA2XX_CALIBRATION: |
Bartosz Golaszewski | b721fe2 | 2015-01-12 14:47:22 +0100 | [diff] [blame] | 313 | val = DIV_ROUND_CLOSEST(data->config->calibration_factor, |
| 314 | data->regs[reg]); |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 315 | break; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 316 | default: |
| 317 | /* programmer goofed */ |
| 318 | WARN_ON_ONCE(1); |
| 319 | val = 0; |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | return val; |
| 324 | } |
| 325 | |
| 326 | static ssize_t ina2xx_show_value(struct device *dev, |
| 327 | struct device_attribute *da, char *buf) |
| 328 | { |
| 329 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 330 | struct ina2xx_data *data = ina2xx_update_device(dev); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 331 | |
| 332 | if (IS_ERR(data)) |
| 333 | return PTR_ERR(data); |
| 334 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 335 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 336 | ina2xx_get_value(data, attr->index)); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 337 | } |
| 338 | |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 339 | static ssize_t ina2xx_set_shunt(struct device *dev, |
| 340 | struct device_attribute *da, |
| 341 | const char *buf, size_t count) |
| 342 | { |
| 343 | struct ina2xx_data *data = ina2xx_update_device(dev); |
| 344 | unsigned long val; |
| 345 | int status; |
| 346 | |
| 347 | if (IS_ERR(data)) |
| 348 | return PTR_ERR(data); |
| 349 | |
| 350 | status = kstrtoul(buf, 10, &val); |
| 351 | if (status < 0) |
| 352 | return status; |
| 353 | |
| 354 | if (val == 0 || |
| 355 | /* Values greater than the calibration factor make no sense. */ |
| 356 | val > data->config->calibration_factor) |
| 357 | return -EINVAL; |
| 358 | |
| 359 | mutex_lock(&data->update_lock); |
| 360 | data->rshunt = val; |
| 361 | status = ina2xx_calibrate(data); |
| 362 | mutex_unlock(&data->update_lock); |
| 363 | if (status < 0) |
| 364 | return status; |
| 365 | |
| 366 | return count; |
| 367 | } |
| 368 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 369 | static ssize_t ina226_set_interval(struct device *dev, |
| 370 | struct device_attribute *da, |
| 371 | const char *buf, size_t count) |
| 372 | { |
| 373 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 374 | unsigned long val; |
| 375 | int status; |
| 376 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 377 | status = kstrtoul(buf, 10, &val); |
| 378 | if (status < 0) |
| 379 | return status; |
| 380 | |
| 381 | if (val > INT_MAX || val == 0) |
| 382 | return -EINVAL; |
| 383 | |
| 384 | mutex_lock(&data->update_lock); |
| 385 | data->curr_config = ina226_interval_to_reg(val, |
| 386 | data->regs[INA2XX_CONFIG]); |
| 387 | status = i2c_smbus_write_word_swapped(data->client, |
| 388 | INA2XX_CONFIG, |
| 389 | data->curr_config); |
| 390 | |
| 391 | ina226_set_update_interval(data); |
| 392 | /* Make sure the next access re-reads all registers. */ |
| 393 | data->valid = 0; |
| 394 | mutex_unlock(&data->update_lock); |
| 395 | if (status < 0) |
| 396 | return status; |
| 397 | |
| 398 | return count; |
| 399 | } |
| 400 | |
| 401 | static ssize_t ina226_show_interval(struct device *dev, |
| 402 | struct device_attribute *da, char *buf) |
| 403 | { |
| 404 | struct ina2xx_data *data = ina2xx_update_device(dev); |
| 405 | |
| 406 | if (IS_ERR(data)) |
| 407 | return PTR_ERR(data); |
| 408 | |
| 409 | /* |
| 410 | * We don't use data->update_interval here as we want to display |
| 411 | * the actual interval used by the chip and jiffies_to_msecs() |
| 412 | * doesn't seem to be accurate enough. |
| 413 | */ |
| 414 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 415 | ina226_reg_to_interval(data->regs[INA2XX_CONFIG])); |
| 416 | } |
| 417 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 418 | /* shunt voltage */ |
Guenter Roeck | f0df0fd | 2013-01-10 10:04:06 -0800 | [diff] [blame] | 419 | static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL, |
| 420 | INA2XX_SHUNT_VOLTAGE); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 421 | |
| 422 | /* bus voltage */ |
Guenter Roeck | f0df0fd | 2013-01-10 10:04:06 -0800 | [diff] [blame] | 423 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL, |
| 424 | INA2XX_BUS_VOLTAGE); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 425 | |
| 426 | /* calculated current */ |
Guenter Roeck | f0df0fd | 2013-01-10 10:04:06 -0800 | [diff] [blame] | 427 | static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL, |
| 428 | INA2XX_CURRENT); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 429 | |
| 430 | /* calculated power */ |
Guenter Roeck | f0df0fd | 2013-01-10 10:04:06 -0800 | [diff] [blame] | 431 | static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL, |
| 432 | INA2XX_POWER); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 433 | |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 434 | /* shunt resistance */ |
| 435 | static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR, |
| 436 | ina2xx_show_value, ina2xx_set_shunt, |
| 437 | INA2XX_CALIBRATION); |
| 438 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 439 | /* update interval (ina226 only) */ |
| 440 | static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, |
| 441 | ina226_show_interval, ina226_set_interval, 0); |
| 442 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 443 | /* pointers to created device attributes */ |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 444 | static struct attribute *ina2xx_attrs[] = { |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 445 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 446 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 447 | &sensor_dev_attr_curr1_input.dev_attr.attr, |
| 448 | &sensor_dev_attr_power1_input.dev_attr.attr, |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 449 | &sensor_dev_attr_shunt_resistor.dev_attr.attr, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 450 | NULL, |
| 451 | }; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 452 | |
| 453 | static const struct attribute_group ina2xx_group = { |
| 454 | .attrs = ina2xx_attrs, |
| 455 | }; |
| 456 | |
| 457 | static struct attribute *ina226_attrs[] = { |
| 458 | &sensor_dev_attr_update_interval.dev_attr.attr, |
| 459 | NULL, |
| 460 | }; |
| 461 | |
| 462 | static const struct attribute_group ina226_group = { |
| 463 | .attrs = ina226_attrs, |
| 464 | }; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 465 | |
| 466 | static int ina2xx_probe(struct i2c_client *client, |
| 467 | const struct i2c_device_id *id) |
| 468 | { |
| 469 | struct i2c_adapter *adapter = client->adapter; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 470 | struct ina2xx_platform_data *pdata; |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 471 | struct device *dev = &client->dev; |
| 472 | struct ina2xx_data *data; |
| 473 | struct device *hwmon_dev; |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 474 | u32 val; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 475 | int ret, group = 0; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 476 | |
| 477 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) |
| 478 | return -ENODEV; |
| 479 | |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 480 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 481 | if (!data) |
| 482 | return -ENOMEM; |
| 483 | |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 484 | if (dev_get_platdata(dev)) { |
| 485 | pdata = dev_get_platdata(dev); |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 486 | data->rshunt = pdata->shunt_uohms; |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 487 | } else if (!of_property_read_u32(dev->of_node, |
| 488 | "shunt-resistor", &val)) { |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 489 | data->rshunt = val; |
| 490 | } else { |
| 491 | data->rshunt = INA2XX_RSHUNT_DEFAULT; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 492 | } |
| 493 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 494 | /* set the device type */ |
| 495 | data->kind = id->driver_data; |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 496 | data->config = &ina2xx_config[data->kind]; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 497 | data->curr_config = data->config->config_default; |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 498 | data->client = client; |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 499 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 500 | /* |
| 501 | * Ina226 has a variable update_interval. For ina219 we |
| 502 | * use a constant value. |
| 503 | */ |
| 504 | if (data->kind == ina226) |
| 505 | ina226_set_update_interval(data); |
| 506 | else |
| 507 | data->update_interval = HZ / INA2XX_CONVERSION_RATE; |
| 508 | |
Bartosz Golaszewski | e794704 | 2015-01-05 15:20:54 +0100 | [diff] [blame] | 509 | if (data->rshunt <= 0 || |
| 510 | data->rshunt > data->config->calibration_factor) |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 511 | return -ENODEV; |
| 512 | |
| 513 | ret = ina2xx_init(data); |
| 514 | if (ret < 0) { |
| 515 | dev_err(dev, "error configuring the device: %d\n", ret); |
| 516 | return -ENODEV; |
| 517 | } |
| 518 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 519 | mutex_init(&data->update_lock); |
| 520 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 521 | data->groups[group++] = &ina2xx_group; |
| 522 | if (data->kind == ina226) |
| 523 | data->groups[group++] = &ina226_group; |
| 524 | |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 525 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 526 | data, data->groups); |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 527 | if (IS_ERR(hwmon_dev)) |
| 528 | return PTR_ERR(hwmon_dev); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 529 | |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 530 | dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n", |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 531 | id->name, data->rshunt); |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 532 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 533 | return 0; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static const struct i2c_device_id ina2xx_id[] = { |
| 537 | { "ina219", ina219 }, |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 538 | { "ina220", ina219 }, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 539 | { "ina226", ina226 }, |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 540 | { "ina230", ina226 }, |
Kevin Hilman | add513b | 2015-01-14 17:34:58 -0800 | [diff] [blame] | 541 | { "ina231", ina226 }, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 542 | { } |
| 543 | }; |
| 544 | MODULE_DEVICE_TABLE(i2c, ina2xx_id); |
| 545 | |
| 546 | static struct i2c_driver ina2xx_driver = { |
| 547 | .driver = { |
| 548 | .name = "ina2xx", |
| 549 | }, |
| 550 | .probe = ina2xx_probe, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 551 | .id_table = ina2xx_id, |
| 552 | }; |
| 553 | |
Wei Yongjun | d835ca0 | 2012-10-08 20:40:35 +0800 | [diff] [blame] | 554 | module_i2c_driver(ina2xx_driver); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 555 | |
| 556 | MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>"); |
| 557 | MODULE_DESCRIPTION("ina2xx driver"); |
| 558 | MODULE_LICENSE("GPL"); |