diff options
| author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
|---|---|---|
| committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
| commit | fcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch) | |
| tree | a57612d1888735a2ec7972891b68c1ac5ec8faea /drivers/hwmon/ina219.c | |
| parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) | |
Diffstat (limited to 'drivers/hwmon/ina219.c')
| -rw-r--r-- | drivers/hwmon/ina219.c | 414 |
1 files changed, 414 insertions, 0 deletions
diff --git a/drivers/hwmon/ina219.c b/drivers/hwmon/ina219.c new file mode 100644 index 00000000000..cc5b85fdcf8 --- /dev/null +++ b/drivers/hwmon/ina219.c | |||
| @@ -0,0 +1,414 @@ | |||
| 1 | /* | ||
| 2 | * ina219.c - driver for TI INA219 current / power monitor sensor | ||
| 3 | * | ||
| 4 | * Copyright (c) 2011, NVIDIA Corporation. | ||
| 5 | * | ||
| 6 | * The INA219 is a sensor chip made by Texas Instruments. It measures | ||
| 7 | * power, voltage and current on a power rail. | ||
| 8 | * Complete datasheet can be obtained from website: | ||
| 9 | * http://focus.ti.com/lit/ds/symlink/ina219.pdf | ||
| 10 | * | ||
| 11 | * This program is free software. you can redistribute it and/or modify | ||
| 12 | * it under the terms of the GNU General Public License as published by | ||
| 13 | * the Free Software Foundation. | ||
| 14 | * | ||
| 15 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 18 | * more details. | ||
| 19 | * | ||
| 20 | * You should have received a copy of the GNU General Public License along | ||
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 23 | */ | ||
| 24 | |||
| 25 | #include <linux/module.h> | ||
| 26 | #include <linux/kernel.h> | ||
| 27 | #include <linux/spinlock.h> | ||
| 28 | #include <linux/sysfs.h> | ||
| 29 | #include <linux/kobject.h> | ||
| 30 | #include <linux/hrtimer.h> | ||
| 31 | #include <linux/slab.h> | ||
| 32 | #include <linux/interrupt.h> | ||
| 33 | #include <linux/mutex.h> | ||
| 34 | #include <linux/i2c.h> | ||
| 35 | #include <linux/slab.h> | ||
| 36 | #include <linux/err.h> | ||
| 37 | #include <linux/gpio.h> | ||
| 38 | #include <linux/device.h> | ||
| 39 | #include <linux/sysdev.h> | ||
| 40 | #include "linux/ina219.h" | ||
| 41 | #include <linux/init.h> | ||
| 42 | #include <linux/hwmon-sysfs.h> | ||
| 43 | #include <linux/hwmon.h> | ||
| 44 | |||
| 45 | #define DRIVER_NAME "ina219" | ||
| 46 | |||
| 47 | /* INA219 register offsets */ | ||
| 48 | #define INA219_CONFIG 0 | ||
| 49 | #define INA219_SHUNT 1 | ||
| 50 | #define INA219_VOLTAGE 2 | ||
| 51 | #define INA219_POWER 3 | ||
| 52 | #define INA219_CURRENT 4 | ||
| 53 | #define INA219_CAL 5 | ||
| 54 | |||
| 55 | /* | ||
| 56 | INA219 Sensor defines | ||
| 57 | Config info for ina219s | ||
| 58 | D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 | ||
| 59 | rst BRNG PG1 PG0 BADC4 .-.BADC1 SADC4 - SADC1 MODE3 - MODE1 | ||
| 60 | reset D15 | ||
| 61 | bus_range=0 (d13) | ||
| 62 | pga_gain=0 (D12:D11) | ||
| 63 | bus_adc_setting=0x3 (d10:D7) 12-bit w/o oversampling (532uS) | ||
| 64 | shunt_adc_setting=0xb (D6:D3) 8x oversampling (4.26ms) | ||
| 65 | mode=0x7 (D2:D0) continuous shunt & bus | ||
| 66 | */ | ||
| 67 | #define INA219_CONFIG_DATA 0x1df | ||
| 68 | #define INA219_RESET 0x8000 | ||
| 69 | |||
| 70 | struct power_mon_data { | ||
| 71 | s32 voltage; | ||
| 72 | s32 currentInMillis; | ||
| 73 | s32 power; | ||
| 74 | }; | ||
| 75 | |||
| 76 | struct ina219_data { | ||
| 77 | struct device *hwmon_dev; | ||
| 78 | struct i2c_client *client; | ||
| 79 | struct ina219_platform_data *pInfo; | ||
| 80 | struct power_mon_data pm_data; | ||
| 81 | struct mutex mutex; | ||
| 82 | }; | ||
| 83 | |||
| 84 | /* Set non-zero to enable debug prints */ | ||
| 85 | #define INA219_DEBUG_PRINTS 0 | ||
| 86 | |||
| 87 | #if INA219_DEBUG_PRINTS | ||
| 88 | #define DEBUG_INA219(x) printk x | ||
| 89 | #else | ||
| 90 | #define DEBUG_INA219(x) | ||
| 91 | #endif | ||
| 92 | |||
| 93 | static s16 reorder_bytes(s16 a) | ||
| 94 | { | ||
| 95 | s16 ret = ((a >> 8) & 0xff) | ((a & 0xff) << 8); | ||
| 96 | return ret; | ||
| 97 | } | ||
| 98 | |||
| 99 | /* set ina219 to power down mode */ | ||
| 100 | static s32 power_down_INA219(struct i2c_client *client) | ||
| 101 | { | ||
| 102 | s32 retval; | ||
| 103 | retval = i2c_smbus_write_word_data(client, INA219_CONFIG, 0); | ||
| 104 | if (retval < 0) | ||
| 105 | dev_err(&client->dev, "power down failure sts: 0x%x\n", retval); | ||
| 106 | return retval; | ||
| 107 | } | ||
| 108 | |||
| 109 | static s32 show_rail_name(struct device *dev, | ||
| 110 | struct device_attribute *attr, | ||
| 111 | char *buf) | ||
| 112 | { | ||
| 113 | struct i2c_client *client = to_i2c_client(dev); | ||
| 114 | struct ina219_data *data = i2c_get_clientdata(client); | ||
| 115 | return sprintf(buf, "%s\n", data->pInfo->rail_name); | ||
| 116 | } | ||
| 117 | |||
| 118 | static s32 show_voltage(struct device *dev, | ||
| 119 | struct device_attribute *attr, | ||
| 120 | char *buf) | ||
| 121 | { | ||
| 122 | struct i2c_client *client = to_i2c_client(dev); | ||
| 123 | struct ina219_data *data = i2c_get_clientdata(client); | ||
| 124 | s32 retval; | ||
| 125 | s32 voltage_mV; | ||
| 126 | |||
| 127 | /* fill config data */ | ||
| 128 | retval = i2c_smbus_write_word_data(client, INA219_CONFIG, | ||
| 129 | reorder_bytes(INA219_CONFIG_DATA)); | ||
| 130 | if (retval < 0) { | ||
| 131 | dev_err(dev, "config data write failed sts: 0x%x\n", retval); | ||
| 132 | goto error; | ||
| 133 | } | ||
| 134 | |||
| 135 | /* fill calibration data */ | ||
| 136 | retval = i2c_smbus_write_word_data(client, INA219_CAL, | ||
| 137 | reorder_bytes(data->pInfo->calibration_data)); | ||
| 138 | if (retval < 0) { | ||
| 139 | dev_err(dev, "calib data write failed sts: 0x%x\n", retval); | ||
| 140 | goto error; | ||
| 141 | } | ||
| 142 | |||
| 143 | /* getting voltage readings in milli volts*/ | ||
| 144 | voltage_mV = | ||
| 145 | reorder_bytes(i2c_smbus_read_word_data(client, | ||
| 146 | INA219_VOLTAGE)); | ||
| 147 | DEBUG_INA219(("Ina219 voltage reg Value: 0x%x\n", voltage_mV)); | ||
| 148 | if (voltage_mV < 0) | ||
| 149 | goto error; | ||
| 150 | voltage_mV = voltage_mV >> 1; | ||
| 151 | DEBUG_INA219(("Ina219 voltage in mv: %d\n", voltage_mV)); | ||
| 152 | |||
| 153 | /* set ina219 to power down mode */ | ||
| 154 | retval = power_down_INA219(client); | ||
| 155 | if (retval < 0) | ||
| 156 | goto error; | ||
| 157 | |||
| 158 | DEBUG_INA219(("%s volt = %d\n", __func__, voltage_mV)); | ||
| 159 | return sprintf(buf, "%d mV\n", voltage_mV); | ||
| 160 | error: | ||
| 161 | dev_err(dev, "%s: failed\n", __func__); | ||
| 162 | return retval; | ||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | static s32 show_power(struct device *dev, | ||
| 167 | struct device_attribute *attr, | ||
| 168 | char *buf) | ||
| 169 | { | ||
| 170 | struct i2c_client *client = to_i2c_client(dev); | ||
| 171 | struct ina219_data *data = i2c_get_clientdata(client); | ||
| 172 | s32 retval; | ||
| 173 | s32 power_mW; | ||
| 174 | s32 voltage_mV; | ||
| 175 | s32 overflow, conversion; | ||
| 176 | |||
| 177 | /* fill config data */ | ||
| 178 | retval = i2c_smbus_write_word_data(client, INA219_CONFIG, | ||
| 179 | reorder_bytes(INA219_CONFIG_DATA)); | ||
| 180 | if (retval < 0) { | ||
| 181 | dev_err(dev, "config data write failed sts: 0x%x\n", retval); | ||
| 182 | goto error; | ||
| 183 | } | ||
| 184 | |||
| 185 | /* fill calib data */ | ||
| 186 | retval = i2c_smbus_write_word_data(client, INA219_CAL, | ||
| 187 | reorder_bytes(data->pInfo->calibration_data)); | ||
| 188 | if (retval < 0) { | ||
| 189 | dev_err(dev, "calibration data write failed sts: 0x%x\n", | ||
| 190 | retval); | ||
| 191 | goto error; | ||
| 192 | } | ||
| 193 | |||
| 194 | /* check if the readings are valid */ | ||
| 195 | do { | ||
| 196 | /* read power register to clear conversion bit */ | ||
| 197 | retval = reorder_bytes(i2c_smbus_read_word_data(client, | ||
| 198 | INA219_POWER)); | ||
| 199 | if (retval < 0) { | ||
| 200 | dev_err(dev, "CNVR bit clearing failure sts: 0x%x\n", | ||
| 201 | retval); | ||
| 202 | goto error; | ||
| 203 | } | ||
| 204 | |||
| 205 | voltage_mV = | ||
| 206 | reorder_bytes(i2c_smbus_read_word_data(client, | ||
| 207 | INA219_VOLTAGE)); | ||
| 208 | DEBUG_INA219(("Ina219 voltage reg Value: 0x%x\n", voltage_mV)); | ||
| 209 | overflow = voltage_mV & 1; | ||
| 210 | if (overflow) { | ||
| 211 | dev_err(dev, "overflow error\n"); | ||
| 212 | return 0; | ||
| 213 | } | ||
| 214 | conversion = (voltage_mV >> 1) & 1; | ||
| 215 | DEBUG_INA219(("\n ina219 CNVR value:%d", conversion)); | ||
| 216 | } while (!conversion); | ||
| 217 | |||
| 218 | /* getting power readings in milli watts*/ | ||
| 219 | power_mW = reorder_bytes(i2c_smbus_read_word_data(client, | ||
| 220 | INA219_POWER)); | ||
| 221 | DEBUG_INA219(("Ina219 power Reg: 0x%x\n", power_mW)); | ||
| 222 | power_mW *= data->pInfo->power_lsb; | ||
| 223 | DEBUG_INA219(("Ina219 power Val: %d\n", power_mW)); | ||
| 224 | if (power_mW < 0) | ||
| 225 | goto error; | ||
| 226 | |||
| 227 | /* set ina219 to power down mode */ | ||
| 228 | retval = power_down_INA219(client); | ||
| 229 | if (retval < 0) | ||
| 230 | goto error; | ||
| 231 | |||
| 232 | DEBUG_INA219(("%s pow = %d\n", __func__, power_mW)); | ||
| 233 | return sprintf(buf, "%d mW\n", power_mW); | ||
| 234 | error: | ||
| 235 | dev_err(dev, "%s: failed\n", __func__); | ||
| 236 | return retval; | ||
| 237 | } | ||
| 238 | |||
| 239 | static s32 show_current(struct device *dev, | ||
| 240 | struct device_attribute *attr, | ||
| 241 | char *buf) | ||
| 242 | { | ||
| 243 | struct i2c_client *client = to_i2c_client(dev); | ||
| 244 | struct ina219_data *data = i2c_get_clientdata(client); | ||
| 245 | s32 retval; | ||
| 246 | s32 current_mA; | ||
| 247 | s32 voltage_mV; | ||
| 248 | s32 overflow, conversion; | ||
| 249 | |||
| 250 | /* fill config data */ | ||
| 251 | retval = i2c_smbus_write_word_data(client, INA219_CONFIG, | ||
| 252 | reorder_bytes(INA219_CONFIG_DATA)); | ||
| 253 | if (retval < 0) { | ||
| 254 | dev_err(dev, "config data write failed sts: 0x%x\n", retval); | ||
| 255 | goto error; | ||
| 256 | } | ||
| 257 | |||
| 258 | /* fill calib data */ | ||
| 259 | retval = i2c_smbus_write_word_data(client, INA219_CAL, | ||
| 260 | reorder_bytes(data->pInfo->calibration_data)); | ||
| 261 | if (retval < 0) { | ||
| 262 | dev_err(dev, "calibration data write failed sts: 0x%x\n", | ||
| 263 | retval); | ||
| 264 | goto error; | ||
| 265 | } | ||
| 266 | |||
| 267 | /* check if the readings are valid */ | ||
| 268 | do { | ||
| 269 | /* read power register to clear conversion bit */ | ||
| 270 | retval = reorder_bytes(i2c_smbus_read_word_data(client, | ||
| 271 | INA219_POWER)); | ||
| 272 | if (retval < 0) { | ||
| 273 | dev_err(dev, "CNVR bit clearing failure sts: 0x%x\n", | ||
| 274 | retval); | ||
| 275 | goto error; | ||
| 276 | } | ||
| 277 | |||
| 278 | voltage_mV = | ||
| 279 | reorder_bytes(i2c_smbus_read_word_data(client, | ||
| 280 | INA219_VOLTAGE)); | ||
| 281 | DEBUG_INA219(("Ina219 voltage reg Value: 0x%x\n", voltage_mV)); | ||
| 282 | overflow = voltage_mV & 1; | ||
| 283 | if (overflow) { | ||
| 284 | dev_err(dev, "overflow error\n"); | ||
| 285 | return 0; | ||
| 286 | } | ||
| 287 | conversion = (voltage_mV >> 1) & 1; | ||
| 288 | DEBUG_INA219(("\n ina219 CNVR value:%d", conversion)); | ||
| 289 | } while (!conversion); | ||
| 290 | |||
| 291 | /* getting current readings in milli amps*/ | ||
| 292 | current_mA = reorder_bytes(i2c_smbus_read_word_data(client, | ||
| 293 | INA219_CURRENT)); | ||
| 294 | DEBUG_INA219(("Ina219 current Reg: 0x%x\n", current_mA)); | ||
| 295 | if (current_mA < 0) | ||
| 296 | goto error; | ||
| 297 | current_mA = | ||
| 298 | (current_mA * data->pInfo->power_lsb) / data->pInfo->divisor; | ||
| 299 | DEBUG_INA219(("Ina219 current Value: %d\n", current_mA)); | ||
| 300 | |||
| 301 | /* set ina219 to power down mode */ | ||
| 302 | retval = power_down_INA219(client); | ||
| 303 | if (retval < 0) | ||
| 304 | goto error; | ||
| 305 | |||
| 306 | |||
| 307 | DEBUG_INA219(("%s current = %d\n", __func__, current_mA)); | ||
| 308 | return sprintf(buf, "%d mA\n", current_mA); | ||
| 309 | error: | ||
| 310 | dev_err(dev, "%s: failed\n", __func__); | ||
| 311 | return retval; | ||
| 312 | } | ||
| 313 | |||
| 314 | static struct sensor_device_attribute ina219[] = { | ||
| 315 | SENSOR_ATTR(rail_name, S_IRUGO, show_rail_name, NULL, 0), | ||
| 316 | SENSOR_ATTR(in1_input, S_IRUGO, show_voltage, NULL, 0), | ||
| 317 | SENSOR_ATTR(curr1_input, S_IRUGO, show_current, NULL, 0), | ||
| 318 | SENSOR_ATTR(power1_input, S_IRUGO, show_power, NULL, 0), | ||
| 319 | }; | ||
| 320 | |||
| 321 | static int __devinit ina219_probe(struct i2c_client *client, | ||
| 322 | const struct i2c_device_id *id) | ||
| 323 | { | ||
| 324 | struct ina219_data *data; | ||
| 325 | int err; | ||
| 326 | u8 i; | ||
| 327 | data = kzalloc(sizeof(struct ina219_data), GFP_KERNEL); | ||
| 328 | if (!data) { | ||
| 329 | err = -ENOMEM; | ||
| 330 | goto exit; | ||
| 331 | } | ||
| 332 | |||
| 333 | i2c_set_clientdata(client, data); | ||
| 334 | data->pInfo = client->dev.platform_data; | ||
| 335 | mutex_init(&data->mutex); | ||
| 336 | /* reset ina219 */ | ||
| 337 | err = i2c_smbus_write_word_data(client, INA219_CONFIG, | ||
| 338 | reorder_bytes(INA219_RESET)); | ||
| 339 | if (err < 0) { | ||
| 340 | dev_err(&client->dev, "ina219 reset failure status: 0x%x\n", | ||
| 341 | err); | ||
| 342 | goto exit_free; | ||
| 343 | } | ||
| 344 | |||
| 345 | for (i = 0; i < ARRAY_SIZE(ina219); i++) { | ||
| 346 | err = device_create_file(&client->dev, &ina219[i].dev_attr); | ||
| 347 | if (err) { | ||
| 348 | dev_err(&client->dev, "device_create_file failed.\n"); | ||
| 349 | goto exit_free; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | data->hwmon_dev = hwmon_device_register(&client->dev); | ||
| 354 | if (IS_ERR(data->hwmon_dev)) { | ||
| 355 | err = PTR_ERR(data->hwmon_dev); | ||
| 356 | goto exit_remove; | ||
| 357 | } | ||
| 358 | |||
| 359 | /* set ina219 to power down mode */ | ||
| 360 | err = power_down_INA219(client); | ||
| 361 | if (err < 0) | ||
| 362 | goto exit_remove; | ||
| 363 | |||
| 364 | return 0; | ||
| 365 | |||
| 366 | exit_remove: | ||
| 367 | for (i = 0; i < ARRAY_SIZE(ina219); i++) | ||
| 368 | device_remove_file(&client->dev, &ina219[i].dev_attr); | ||
| 369 | exit_free: | ||
| 370 | kfree(data); | ||
| 371 | exit: | ||
| 372 | return err; | ||
| 373 | } | ||
| 374 | |||
| 375 | static int __devexit ina219_remove(struct i2c_client *client) | ||
| 376 | { | ||
| 377 | u8 i; | ||
| 378 | struct ina219_data *data = i2c_get_clientdata(client); | ||
| 379 | hwmon_device_unregister(data->hwmon_dev); | ||
| 380 | for (i = 0; i < ARRAY_SIZE(ina219); i++) | ||
| 381 | device_remove_file(&client->dev, &ina219[i].dev_attr); | ||
| 382 | kfree(data); | ||
| 383 | return 0; | ||
| 384 | } | ||
| 385 | |||
| 386 | static const struct i2c_device_id ina219_id[] = { | ||
| 387 | {DRIVER_NAME, 0 }, | ||
| 388 | {} | ||
| 389 | }; | ||
| 390 | MODULE_DEVICE_TABLE(i2c, ina219_id); | ||
| 391 | |||
| 392 | static struct i2c_driver ina219_driver = { | ||
| 393 | .class = I2C_CLASS_HWMON, | ||
| 394 | .driver = { | ||
| 395 | .name = DRIVER_NAME, | ||
| 396 | }, | ||
| 397 | .probe = ina219_probe, | ||
| 398 | .remove = __devexit_p(ina219_remove), | ||
| 399 | .id_table = ina219_id, | ||
| 400 | }; | ||
| 401 | |||
| 402 | static int __init ina219_init(void) | ||
| 403 | { | ||
| 404 | return i2c_add_driver(&ina219_driver); | ||
| 405 | } | ||
| 406 | |||
| 407 | static void __exit ina219_exit(void) | ||
| 408 | { | ||
| 409 | i2c_del_driver(&ina219_driver); | ||
| 410 | } | ||
| 411 | |||
| 412 | module_init(ina219_init); | ||
| 413 | module_exit(ina219_exit); | ||
| 414 | MODULE_LICENSE("GPL"); | ||
