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> |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 37 | |
| 38 | #include <linux/platform_data/ina2xx.h> |
| 39 | |
| 40 | /* common register definitions */ |
| 41 | #define INA2XX_CONFIG 0x00 |
| 42 | #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */ |
| 43 | #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */ |
| 44 | #define INA2XX_POWER 0x03 /* readonly */ |
| 45 | #define INA2XX_CURRENT 0x04 /* readonly */ |
| 46 | #define INA2XX_CALIBRATION 0x05 |
| 47 | |
| 48 | /* INA226 register definitions */ |
| 49 | #define INA226_MASK_ENABLE 0x06 |
| 50 | #define INA226_ALERT_LIMIT 0x07 |
| 51 | #define INA226_DIE_ID 0xFF |
| 52 | |
| 53 | |
| 54 | /* register count */ |
| 55 | #define INA219_REGISTERS 6 |
| 56 | #define INA226_REGISTERS 8 |
| 57 | |
| 58 | #define INA2XX_MAX_REGISTERS 8 |
| 59 | |
| 60 | /* settings - depend on use case */ |
| 61 | #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */ |
| 62 | #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */ |
| 63 | |
| 64 | /* worst case is 68.10 ms (~14.6Hz, ina219) */ |
| 65 | #define INA2XX_CONVERSION_RATE 15 |
| 66 | |
| 67 | enum ina2xx_ids { ina219, ina226 }; |
| 68 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 69 | struct ina2xx_config { |
| 70 | u16 config_default; |
| 71 | int calibration_factor; |
| 72 | int registers; |
| 73 | int shunt_div; |
| 74 | int bus_voltage_shift; |
| 75 | int bus_voltage_lsb; /* uV */ |
| 76 | int power_lsb; /* uW */ |
| 77 | }; |
| 78 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 79 | struct ina2xx_data { |
| 80 | struct device *hwmon_dev; |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 81 | const struct ina2xx_config *config; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 82 | |
| 83 | struct mutex update_lock; |
| 84 | bool valid; |
| 85 | unsigned long last_updated; |
| 86 | |
| 87 | int kind; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 88 | u16 regs[INA2XX_MAX_REGISTERS]; |
| 89 | }; |
| 90 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 91 | static const struct ina2xx_config ina2xx_config[] = { |
| 92 | [ina219] = { |
| 93 | .config_default = INA219_CONFIG_DEFAULT, |
| 94 | .calibration_factor = 40960000, |
| 95 | .registers = INA219_REGISTERS, |
| 96 | .shunt_div = 100, |
| 97 | .bus_voltage_shift = 3, |
| 98 | .bus_voltage_lsb = 4000, |
| 99 | .power_lsb = 20000, |
| 100 | }, |
| 101 | [ina226] = { |
| 102 | .config_default = INA226_CONFIG_DEFAULT, |
| 103 | .calibration_factor = 5120000, |
| 104 | .registers = INA226_REGISTERS, |
| 105 | .shunt_div = 400, |
| 106 | .bus_voltage_shift = 0, |
| 107 | .bus_voltage_lsb = 1250, |
| 108 | .power_lsb = 25000, |
| 109 | }, |
| 110 | }; |
| 111 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 112 | static struct ina2xx_data *ina2xx_update_device(struct device *dev) |
| 113 | { |
| 114 | struct i2c_client *client = to_i2c_client(dev); |
| 115 | struct ina2xx_data *data = i2c_get_clientdata(client); |
| 116 | struct ina2xx_data *ret = data; |
| 117 | |
| 118 | mutex_lock(&data->update_lock); |
| 119 | |
| 120 | if (time_after(jiffies, data->last_updated + |
| 121 | HZ / INA2XX_CONVERSION_RATE) || !data->valid) { |
| 122 | |
| 123 | int i; |
| 124 | |
| 125 | dev_dbg(&client->dev, "Starting ina2xx update\n"); |
| 126 | |
| 127 | /* Read all registers */ |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 128 | for (i = 0; i < data->config->registers; i++) { |
Guenter Roeck | 080b98e | 2012-09-11 08:22:14 -0700 | [diff] [blame] | 129 | int rv = i2c_smbus_read_word_swapped(client, i); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 130 | if (rv < 0) { |
| 131 | ret = ERR_PTR(rv); |
| 132 | goto abort; |
| 133 | } |
| 134 | data->regs[i] = rv; |
| 135 | } |
| 136 | data->last_updated = jiffies; |
| 137 | data->valid = 1; |
| 138 | } |
| 139 | abort: |
| 140 | mutex_unlock(&data->update_lock); |
| 141 | return ret; |
| 142 | } |
| 143 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 144 | static int ina2xx_get_value(struct ina2xx_data *data, u8 reg) |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 145 | { |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 146 | int val; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 147 | |
| 148 | switch (reg) { |
| 149 | case INA2XX_SHUNT_VOLTAGE: |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 150 | val = DIV_ROUND_CLOSEST(data->regs[reg], |
| 151 | data->config->shunt_div); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 152 | break; |
| 153 | case INA2XX_BUS_VOLTAGE: |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 154 | val = (data->regs[reg] >> data->config->bus_voltage_shift) |
| 155 | * data->config->bus_voltage_lsb; |
| 156 | val = DIV_ROUND_CLOSEST(val, 1000); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 157 | break; |
| 158 | case INA2XX_POWER: |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 159 | val = data->regs[reg] * data->config->power_lsb; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 160 | break; |
| 161 | case INA2XX_CURRENT: |
| 162 | /* LSB=1mA (selected). Is in mA */ |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 163 | val = data->regs[reg]; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 164 | break; |
| 165 | default: |
| 166 | /* programmer goofed */ |
| 167 | WARN_ON_ONCE(1); |
| 168 | val = 0; |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | return val; |
| 173 | } |
| 174 | |
| 175 | static ssize_t ina2xx_show_value(struct device *dev, |
| 176 | struct device_attribute *da, char *buf) |
| 177 | { |
| 178 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 179 | struct ina2xx_data *data = ina2xx_update_device(dev); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 180 | |
| 181 | if (IS_ERR(data)) |
| 182 | return PTR_ERR(data); |
| 183 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 184 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 185 | ina2xx_get_value(data, attr->index)); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* shunt voltage */ |
Guenter Roeck | f0df0fd | 2013-01-10 10:04:06 -0800 | [diff] [blame] | 189 | static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL, |
| 190 | INA2XX_SHUNT_VOLTAGE); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 191 | |
| 192 | /* bus voltage */ |
Guenter Roeck | f0df0fd | 2013-01-10 10:04:06 -0800 | [diff] [blame] | 193 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL, |
| 194 | INA2XX_BUS_VOLTAGE); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 195 | |
| 196 | /* calculated current */ |
Guenter Roeck | f0df0fd | 2013-01-10 10:04:06 -0800 | [diff] [blame] | 197 | static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL, |
| 198 | INA2XX_CURRENT); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 199 | |
| 200 | /* calculated power */ |
Guenter Roeck | f0df0fd | 2013-01-10 10:04:06 -0800 | [diff] [blame] | 201 | static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL, |
| 202 | INA2XX_POWER); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 203 | |
| 204 | /* pointers to created device attributes */ |
| 205 | static struct attribute *ina2xx_attributes[] = { |
| 206 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 207 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 208 | &sensor_dev_attr_curr1_input.dev_attr.attr, |
| 209 | &sensor_dev_attr_power1_input.dev_attr.attr, |
| 210 | NULL, |
| 211 | }; |
| 212 | |
| 213 | static const struct attribute_group ina2xx_group = { |
| 214 | .attrs = ina2xx_attributes, |
| 215 | }; |
| 216 | |
| 217 | static int ina2xx_probe(struct i2c_client *client, |
| 218 | const struct i2c_device_id *id) |
| 219 | { |
| 220 | struct i2c_adapter *adapter = client->adapter; |
| 221 | struct ina2xx_data *data; |
| 222 | struct ina2xx_platform_data *pdata; |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 223 | int ret; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 224 | long shunt = 10000; /* default shunt value 10mOhms */ |
| 225 | |
| 226 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) |
| 227 | return -ENODEV; |
| 228 | |
| 229 | data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); |
| 230 | if (!data) |
| 231 | return -ENOMEM; |
| 232 | |
| 233 | if (client->dev.platform_data) { |
| 234 | pdata = |
| 235 | (struct ina2xx_platform_data *)client->dev.platform_data; |
| 236 | shunt = pdata->shunt_uohms; |
| 237 | } |
| 238 | |
| 239 | if (shunt <= 0) |
| 240 | return -ENODEV; |
| 241 | |
| 242 | /* set the device type */ |
| 243 | data->kind = id->driver_data; |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 244 | data->config = &ina2xx_config[data->kind]; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 245 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 246 | /* device configuration */ |
| 247 | i2c_smbus_write_word_swapped(client, INA2XX_CONFIG, |
| 248 | data->config->config_default); |
| 249 | /* set current LSB to 1mA, shunt is in uOhms */ |
| 250 | /* (equation 13 in datasheet) */ |
| 251 | i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION, |
| 252 | data->config->calibration_factor / shunt); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 253 | |
| 254 | i2c_set_clientdata(client, data); |
| 255 | mutex_init(&data->update_lock); |
| 256 | |
| 257 | ret = sysfs_create_group(&client->dev.kobj, &ina2xx_group); |
| 258 | if (ret) |
| 259 | return ret; |
| 260 | |
| 261 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 262 | if (IS_ERR(data->hwmon_dev)) { |
| 263 | ret = PTR_ERR(data->hwmon_dev); |
| 264 | goto out_err_hwmon; |
| 265 | } |
| 266 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 267 | dev_info(&client->dev, "power monitor %s (Rshunt = %li uOhm)\n", |
| 268 | id->name, shunt); |
| 269 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 270 | return 0; |
| 271 | |
| 272 | out_err_hwmon: |
| 273 | sysfs_remove_group(&client->dev.kobj, &ina2xx_group); |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | static int ina2xx_remove(struct i2c_client *client) |
| 278 | { |
| 279 | struct ina2xx_data *data = i2c_get_clientdata(client); |
| 280 | |
| 281 | hwmon_device_unregister(data->hwmon_dev); |
| 282 | sysfs_remove_group(&client->dev.kobj, &ina2xx_group); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static const struct i2c_device_id ina2xx_id[] = { |
| 288 | { "ina219", ina219 }, |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 289 | { "ina220", ina219 }, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 290 | { "ina226", ina226 }, |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 291 | { "ina230", ina226 }, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 292 | { } |
| 293 | }; |
| 294 | MODULE_DEVICE_TABLE(i2c, ina2xx_id); |
| 295 | |
| 296 | static struct i2c_driver ina2xx_driver = { |
| 297 | .driver = { |
| 298 | .name = "ina2xx", |
| 299 | }, |
| 300 | .probe = ina2xx_probe, |
| 301 | .remove = ina2xx_remove, |
| 302 | .id_table = ina2xx_id, |
| 303 | }; |
| 304 | |
Wei Yongjun | d835ca0 | 2012-10-08 20:40:35 +0800 | [diff] [blame] | 305 | module_i2c_driver(ina2xx_driver); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 306 | |
| 307 | MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>"); |
| 308 | MODULE_DESCRIPTION("ina2xx driver"); |
| 309 | MODULE_LICENSE("GPL"); |