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