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