1ceeadc5cSGuenter Roeck /* 2ceeadc5cSGuenter Roeck * Driver for the Texas Instruments / Burr Brown INA209 3ceeadc5cSGuenter Roeck * Bidirectional Current/Power Monitor 4ceeadc5cSGuenter Roeck * 5ceeadc5cSGuenter Roeck * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net> 6ceeadc5cSGuenter Roeck * 7ceeadc5cSGuenter Roeck * Derived from Ira W. Snyder's original driver submission 8ceeadc5cSGuenter Roeck * Copyright (C) 2008 Paul Hays <Paul.Hays@cattail.ca> 9ceeadc5cSGuenter Roeck * Copyright (C) 2008-2009 Ira W. Snyder <iws@ovro.caltech.edu> 10ceeadc5cSGuenter Roeck * 11ceeadc5cSGuenter Roeck * Aligned with ina2xx driver 12ceeadc5cSGuenter Roeck * Copyright (C) 2012 Lothar Felten <l-felten@ti.com> 13ceeadc5cSGuenter Roeck * Thanks to Jan Volkering 14ceeadc5cSGuenter Roeck * 15ceeadc5cSGuenter Roeck * This program is free software; you can redistribute it and/or modify 16ceeadc5cSGuenter Roeck * it under the terms of the GNU General Public License as published by 17ceeadc5cSGuenter Roeck * the Free Software Foundation; version 2 of the License. 18ceeadc5cSGuenter Roeck * 19ceeadc5cSGuenter Roeck * Datasheet: 20ceeadc5cSGuenter Roeck * http://www.ti.com/lit/gpn/ina209 21ceeadc5cSGuenter Roeck */ 22ceeadc5cSGuenter Roeck 23ceeadc5cSGuenter Roeck #include <linux/kernel.h> 24ceeadc5cSGuenter Roeck #include <linux/module.h> 25ceeadc5cSGuenter Roeck #include <linux/init.h> 26ceeadc5cSGuenter Roeck #include <linux/err.h> 27ceeadc5cSGuenter Roeck #include <linux/slab.h> 28ceeadc5cSGuenter Roeck #include <linux/bug.h> 29ceeadc5cSGuenter Roeck #include <linux/i2c.h> 30ceeadc5cSGuenter Roeck #include <linux/hwmon.h> 31ceeadc5cSGuenter Roeck #include <linux/hwmon-sysfs.h> 32ceeadc5cSGuenter Roeck 33ceeadc5cSGuenter Roeck #include <linux/platform_data/ina2xx.h> 34ceeadc5cSGuenter Roeck 35ceeadc5cSGuenter Roeck /* register definitions */ 36ceeadc5cSGuenter Roeck #define INA209_CONFIGURATION 0x00 37ceeadc5cSGuenter Roeck #define INA209_STATUS 0x01 38ceeadc5cSGuenter Roeck #define INA209_STATUS_MASK 0x02 39ceeadc5cSGuenter Roeck #define INA209_SHUNT_VOLTAGE 0x03 40ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE 0x04 41ceeadc5cSGuenter Roeck #define INA209_POWER 0x05 42ceeadc5cSGuenter Roeck #define INA209_CURRENT 0x06 43ceeadc5cSGuenter Roeck #define INA209_SHUNT_VOLTAGE_POS_PEAK 0x07 44ceeadc5cSGuenter Roeck #define INA209_SHUNT_VOLTAGE_NEG_PEAK 0x08 45ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE_MAX_PEAK 0x09 46ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE_MIN_PEAK 0x0a 47ceeadc5cSGuenter Roeck #define INA209_POWER_PEAK 0x0b 48ceeadc5cSGuenter Roeck #define INA209_SHUNT_VOLTAGE_POS_WARN 0x0c 49ceeadc5cSGuenter Roeck #define INA209_SHUNT_VOLTAGE_NEG_WARN 0x0d 50ceeadc5cSGuenter Roeck #define INA209_POWER_WARN 0x0e 51ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE_OVER_WARN 0x0f 52ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE_UNDER_WARN 0x10 53ceeadc5cSGuenter Roeck #define INA209_POWER_OVER_LIMIT 0x11 54ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE_OVER_LIMIT 0x12 55ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE_UNDER_LIMIT 0x13 56ceeadc5cSGuenter Roeck #define INA209_CRITICAL_DAC_POS 0x14 57ceeadc5cSGuenter Roeck #define INA209_CRITICAL_DAC_NEG 0x15 58ceeadc5cSGuenter Roeck #define INA209_CALIBRATION 0x16 59ceeadc5cSGuenter Roeck 60ceeadc5cSGuenter Roeck #define INA209_REGISTERS 0x17 61ceeadc5cSGuenter Roeck 62ceeadc5cSGuenter Roeck #define INA209_CONFIG_DEFAULT 0x3c47 /* PGA=8, full range */ 63ceeadc5cSGuenter Roeck #define INA209_SHUNT_DEFAULT 10000 /* uOhm */ 64ceeadc5cSGuenter Roeck 65ceeadc5cSGuenter Roeck struct ina209_data { 66*89de7344SGuenter Roeck struct i2c_client *client; 67ceeadc5cSGuenter Roeck 68ceeadc5cSGuenter Roeck struct mutex update_lock; 69ceeadc5cSGuenter Roeck bool valid; 70ceeadc5cSGuenter Roeck unsigned long last_updated; /* in jiffies */ 71ceeadc5cSGuenter Roeck 72ceeadc5cSGuenter Roeck u16 regs[INA209_REGISTERS]; /* All chip registers */ 73ceeadc5cSGuenter Roeck 74ceeadc5cSGuenter Roeck u16 config_orig; /* Original configuration */ 75ceeadc5cSGuenter Roeck u16 calibration_orig; /* Original calibration */ 76ceeadc5cSGuenter Roeck u16 update_interval; 77ceeadc5cSGuenter Roeck }; 78ceeadc5cSGuenter Roeck 79ceeadc5cSGuenter Roeck static struct ina209_data *ina209_update_device(struct device *dev) 80ceeadc5cSGuenter Roeck { 81*89de7344SGuenter Roeck struct ina209_data *data = dev_get_drvdata(dev); 82*89de7344SGuenter Roeck struct i2c_client *client = data->client; 83ceeadc5cSGuenter Roeck struct ina209_data *ret = data; 84ceeadc5cSGuenter Roeck s32 val; 85ceeadc5cSGuenter Roeck int i; 86ceeadc5cSGuenter Roeck 87ceeadc5cSGuenter Roeck mutex_lock(&data->update_lock); 88ceeadc5cSGuenter Roeck 89ceeadc5cSGuenter Roeck if (!data->valid || 90ceeadc5cSGuenter Roeck time_after(jiffies, data->last_updated + data->update_interval)) { 91ceeadc5cSGuenter Roeck for (i = 0; i < ARRAY_SIZE(data->regs); i++) { 92ceeadc5cSGuenter Roeck val = i2c_smbus_read_word_swapped(client, i); 93ceeadc5cSGuenter Roeck if (val < 0) { 94ceeadc5cSGuenter Roeck ret = ERR_PTR(val); 95ceeadc5cSGuenter Roeck goto abort; 96ceeadc5cSGuenter Roeck } 97ceeadc5cSGuenter Roeck data->regs[i] = val; 98ceeadc5cSGuenter Roeck } 99ceeadc5cSGuenter Roeck data->last_updated = jiffies; 100ceeadc5cSGuenter Roeck data->valid = true; 101ceeadc5cSGuenter Roeck } 102ceeadc5cSGuenter Roeck abort: 103ceeadc5cSGuenter Roeck mutex_unlock(&data->update_lock); 104ceeadc5cSGuenter Roeck return ret; 105ceeadc5cSGuenter Roeck } 106ceeadc5cSGuenter Roeck 107ceeadc5cSGuenter Roeck /* 108ceeadc5cSGuenter Roeck * Read a value from a device register and convert it to the 109ceeadc5cSGuenter Roeck * appropriate sysfs units 110ceeadc5cSGuenter Roeck */ 111ceeadc5cSGuenter Roeck static long ina209_from_reg(const u8 reg, const u16 val) 112ceeadc5cSGuenter Roeck { 113ceeadc5cSGuenter Roeck switch (reg) { 114ceeadc5cSGuenter Roeck case INA209_SHUNT_VOLTAGE: 115ceeadc5cSGuenter Roeck case INA209_SHUNT_VOLTAGE_POS_PEAK: 116ceeadc5cSGuenter Roeck case INA209_SHUNT_VOLTAGE_NEG_PEAK: 117ceeadc5cSGuenter Roeck case INA209_SHUNT_VOLTAGE_POS_WARN: 118ceeadc5cSGuenter Roeck case INA209_SHUNT_VOLTAGE_NEG_WARN: 119ceeadc5cSGuenter Roeck /* LSB=10 uV. Convert to mV. */ 120ceeadc5cSGuenter Roeck return DIV_ROUND_CLOSEST(val, 100); 121ceeadc5cSGuenter Roeck 122ceeadc5cSGuenter Roeck case INA209_BUS_VOLTAGE: 123ceeadc5cSGuenter Roeck case INA209_BUS_VOLTAGE_MAX_PEAK: 124ceeadc5cSGuenter Roeck case INA209_BUS_VOLTAGE_MIN_PEAK: 125ceeadc5cSGuenter Roeck case INA209_BUS_VOLTAGE_OVER_WARN: 126ceeadc5cSGuenter Roeck case INA209_BUS_VOLTAGE_UNDER_WARN: 127ceeadc5cSGuenter Roeck case INA209_BUS_VOLTAGE_OVER_LIMIT: 128ceeadc5cSGuenter Roeck case INA209_BUS_VOLTAGE_UNDER_LIMIT: 129ceeadc5cSGuenter Roeck /* LSB=4 mV, last 3 bits unused */ 130ceeadc5cSGuenter Roeck return (val >> 3) * 4; 131ceeadc5cSGuenter Roeck 132ceeadc5cSGuenter Roeck case INA209_CRITICAL_DAC_POS: 133ceeadc5cSGuenter Roeck /* LSB=1 mV, in the upper 8 bits */ 134ceeadc5cSGuenter Roeck return val >> 8; 135ceeadc5cSGuenter Roeck 136ceeadc5cSGuenter Roeck case INA209_CRITICAL_DAC_NEG: 137ceeadc5cSGuenter Roeck /* LSB=1 mV, in the upper 8 bits */ 138ceeadc5cSGuenter Roeck return -1 * (val >> 8); 139ceeadc5cSGuenter Roeck 140ceeadc5cSGuenter Roeck case INA209_POWER: 141ceeadc5cSGuenter Roeck case INA209_POWER_PEAK: 142ceeadc5cSGuenter Roeck case INA209_POWER_WARN: 143ceeadc5cSGuenter Roeck case INA209_POWER_OVER_LIMIT: 144ceeadc5cSGuenter Roeck /* LSB=20 mW. Convert to uW */ 145ceeadc5cSGuenter Roeck return val * 20 * 1000L; 146ceeadc5cSGuenter Roeck 147ceeadc5cSGuenter Roeck case INA209_CURRENT: 148ceeadc5cSGuenter Roeck /* LSB=1 mA (selected). Is in mA */ 149ceeadc5cSGuenter Roeck return val; 150ceeadc5cSGuenter Roeck } 151ceeadc5cSGuenter Roeck 152ceeadc5cSGuenter Roeck /* programmer goofed */ 153ceeadc5cSGuenter Roeck WARN_ON_ONCE(1); 154ceeadc5cSGuenter Roeck return 0; 155ceeadc5cSGuenter Roeck } 156ceeadc5cSGuenter Roeck 157ceeadc5cSGuenter Roeck /* 158ceeadc5cSGuenter Roeck * Take a value and convert it to register format, clamping the value 159ceeadc5cSGuenter Roeck * to the appropriate range. 160ceeadc5cSGuenter Roeck */ 161ceeadc5cSGuenter Roeck static int ina209_to_reg(u8 reg, u16 old, long val) 162ceeadc5cSGuenter Roeck { 163ceeadc5cSGuenter Roeck switch (reg) { 164ceeadc5cSGuenter Roeck case INA209_SHUNT_VOLTAGE_POS_WARN: 165ceeadc5cSGuenter Roeck case INA209_SHUNT_VOLTAGE_NEG_WARN: 166ceeadc5cSGuenter Roeck /* Limit to +- 320 mV, 10 uV LSB */ 167ceeadc5cSGuenter Roeck return clamp_val(val, -320, 320) * 100; 168ceeadc5cSGuenter Roeck 169ceeadc5cSGuenter Roeck case INA209_BUS_VOLTAGE_OVER_WARN: 170ceeadc5cSGuenter Roeck case INA209_BUS_VOLTAGE_UNDER_WARN: 171ceeadc5cSGuenter Roeck case INA209_BUS_VOLTAGE_OVER_LIMIT: 172ceeadc5cSGuenter Roeck case INA209_BUS_VOLTAGE_UNDER_LIMIT: 173ceeadc5cSGuenter Roeck /* 174ceeadc5cSGuenter Roeck * Limit to 0-32000 mV, 4 mV LSB 175ceeadc5cSGuenter Roeck * 176ceeadc5cSGuenter Roeck * The last three bits aren't part of the value, but we'll 177ceeadc5cSGuenter Roeck * preserve them in their original state. 178ceeadc5cSGuenter Roeck */ 179ceeadc5cSGuenter Roeck return (DIV_ROUND_CLOSEST(clamp_val(val, 0, 32000), 4) << 3) 180ceeadc5cSGuenter Roeck | (old & 0x7); 181ceeadc5cSGuenter Roeck 182ceeadc5cSGuenter Roeck case INA209_CRITICAL_DAC_NEG: 183ceeadc5cSGuenter Roeck /* 184ceeadc5cSGuenter Roeck * Limit to -255-0 mV, 1 mV LSB 185ceeadc5cSGuenter Roeck * Convert the value to a positive value for the register 186ceeadc5cSGuenter Roeck * 187ceeadc5cSGuenter Roeck * The value lives in the top 8 bits only, be careful 188ceeadc5cSGuenter Roeck * and keep original value of other bits. 189ceeadc5cSGuenter Roeck */ 190ceeadc5cSGuenter Roeck return (clamp_val(-val, 0, 255) << 8) | (old & 0xff); 191ceeadc5cSGuenter Roeck 192ceeadc5cSGuenter Roeck case INA209_CRITICAL_DAC_POS: 193ceeadc5cSGuenter Roeck /* 194ceeadc5cSGuenter Roeck * Limit to 0-255 mV, 1 mV LSB 195ceeadc5cSGuenter Roeck * 196ceeadc5cSGuenter Roeck * The value lives in the top 8 bits only, be careful 197ceeadc5cSGuenter Roeck * and keep original value of other bits. 198ceeadc5cSGuenter Roeck */ 199ceeadc5cSGuenter Roeck return (clamp_val(val, 0, 255) << 8) | (old & 0xff); 200ceeadc5cSGuenter Roeck 201ceeadc5cSGuenter Roeck case INA209_POWER_WARN: 202ceeadc5cSGuenter Roeck case INA209_POWER_OVER_LIMIT: 203ceeadc5cSGuenter Roeck /* 20 mW LSB */ 204ceeadc5cSGuenter Roeck return DIV_ROUND_CLOSEST(val, 20 * 1000); 205ceeadc5cSGuenter Roeck } 206ceeadc5cSGuenter Roeck 207ceeadc5cSGuenter Roeck /* Other registers are read-only, return access error */ 208ceeadc5cSGuenter Roeck return -EACCES; 209ceeadc5cSGuenter Roeck } 210ceeadc5cSGuenter Roeck 211ceeadc5cSGuenter Roeck static int ina209_interval_from_reg(u16 reg) 212ceeadc5cSGuenter Roeck { 213ceeadc5cSGuenter Roeck return 68 >> (15 - ((reg >> 3) & 0x0f)); 214ceeadc5cSGuenter Roeck } 215ceeadc5cSGuenter Roeck 216ceeadc5cSGuenter Roeck static u16 ina209_reg_from_interval(u16 config, long interval) 217ceeadc5cSGuenter Roeck { 218ceeadc5cSGuenter Roeck int i, adc; 219ceeadc5cSGuenter Roeck 220ceeadc5cSGuenter Roeck if (interval <= 0) { 221ceeadc5cSGuenter Roeck adc = 8; 222ceeadc5cSGuenter Roeck } else { 223ceeadc5cSGuenter Roeck adc = 15; 224ceeadc5cSGuenter Roeck for (i = 34 + 34 / 2; i; i >>= 1) { 225ceeadc5cSGuenter Roeck if (i < interval) 226ceeadc5cSGuenter Roeck break; 227ceeadc5cSGuenter Roeck adc--; 228ceeadc5cSGuenter Roeck } 229ceeadc5cSGuenter Roeck } 230ceeadc5cSGuenter Roeck return (config & 0xf807) | (adc << 3) | (adc << 7); 231ceeadc5cSGuenter Roeck } 232ceeadc5cSGuenter Roeck 233ceeadc5cSGuenter Roeck static ssize_t ina209_set_interval(struct device *dev, 234ceeadc5cSGuenter Roeck struct device_attribute *da, 235ceeadc5cSGuenter Roeck const char *buf, size_t count) 236ceeadc5cSGuenter Roeck { 237ceeadc5cSGuenter Roeck struct ina209_data *data = ina209_update_device(dev); 238ceeadc5cSGuenter Roeck long val; 239ceeadc5cSGuenter Roeck u16 regval; 240ceeadc5cSGuenter Roeck int ret; 241ceeadc5cSGuenter Roeck 242ceeadc5cSGuenter Roeck if (IS_ERR(data)) 243ceeadc5cSGuenter Roeck return PTR_ERR(data); 244ceeadc5cSGuenter Roeck 245ceeadc5cSGuenter Roeck ret = kstrtol(buf, 10, &val); 246ceeadc5cSGuenter Roeck if (ret < 0) 247ceeadc5cSGuenter Roeck return ret; 248ceeadc5cSGuenter Roeck 249ceeadc5cSGuenter Roeck mutex_lock(&data->update_lock); 250ceeadc5cSGuenter Roeck regval = ina209_reg_from_interval(data->regs[INA209_CONFIGURATION], 251ceeadc5cSGuenter Roeck val); 252*89de7344SGuenter Roeck i2c_smbus_write_word_swapped(data->client, INA209_CONFIGURATION, 253*89de7344SGuenter Roeck regval); 254ceeadc5cSGuenter Roeck data->regs[INA209_CONFIGURATION] = regval; 255ceeadc5cSGuenter Roeck data->update_interval = ina209_interval_from_reg(regval); 256ceeadc5cSGuenter Roeck mutex_unlock(&data->update_lock); 257ceeadc5cSGuenter Roeck return count; 258ceeadc5cSGuenter Roeck } 259ceeadc5cSGuenter Roeck 260ceeadc5cSGuenter Roeck static ssize_t ina209_show_interval(struct device *dev, 261ceeadc5cSGuenter Roeck struct device_attribute *da, char *buf) 262ceeadc5cSGuenter Roeck { 263*89de7344SGuenter Roeck struct ina209_data *data = dev_get_drvdata(dev); 264ceeadc5cSGuenter Roeck 265ceeadc5cSGuenter Roeck return snprintf(buf, PAGE_SIZE, "%d\n", data->update_interval); 266ceeadc5cSGuenter Roeck } 267ceeadc5cSGuenter Roeck 268ceeadc5cSGuenter Roeck /* 269ceeadc5cSGuenter Roeck * History is reset by writing 1 into bit 0 of the respective peak register. 270ceeadc5cSGuenter Roeck * Since more than one peak register may be affected by the scope of a 271ceeadc5cSGuenter Roeck * reset_history attribute write, use a bit mask in attr->index to identify 272ceeadc5cSGuenter Roeck * which registers are affected. 273ceeadc5cSGuenter Roeck */ 274ceeadc5cSGuenter Roeck static u16 ina209_reset_history_regs[] = { 275ceeadc5cSGuenter Roeck INA209_SHUNT_VOLTAGE_POS_PEAK, 276ceeadc5cSGuenter Roeck INA209_SHUNT_VOLTAGE_NEG_PEAK, 277ceeadc5cSGuenter Roeck INA209_BUS_VOLTAGE_MAX_PEAK, 278ceeadc5cSGuenter Roeck INA209_BUS_VOLTAGE_MIN_PEAK, 279ceeadc5cSGuenter Roeck INA209_POWER_PEAK 280ceeadc5cSGuenter Roeck }; 281ceeadc5cSGuenter Roeck 282ceeadc5cSGuenter Roeck static ssize_t ina209_reset_history(struct device *dev, 283ceeadc5cSGuenter Roeck struct device_attribute *da, 284ceeadc5cSGuenter Roeck const char *buf, 285ceeadc5cSGuenter Roeck size_t count) 286ceeadc5cSGuenter Roeck { 287ceeadc5cSGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 288*89de7344SGuenter Roeck struct ina209_data *data = dev_get_drvdata(dev); 289*89de7344SGuenter Roeck struct i2c_client *client = data->client; 290ceeadc5cSGuenter Roeck u32 mask = attr->index; 291ceeadc5cSGuenter Roeck long val; 292ceeadc5cSGuenter Roeck int i, ret; 293ceeadc5cSGuenter Roeck 294ceeadc5cSGuenter Roeck ret = kstrtol(buf, 10, &val); 295ceeadc5cSGuenter Roeck if (ret < 0) 296ceeadc5cSGuenter Roeck return ret; 297ceeadc5cSGuenter Roeck 298ceeadc5cSGuenter Roeck mutex_lock(&data->update_lock); 299ceeadc5cSGuenter Roeck for (i = 0; i < ARRAY_SIZE(ina209_reset_history_regs); i++) { 300ceeadc5cSGuenter Roeck if (mask & (1 << i)) 301ceeadc5cSGuenter Roeck i2c_smbus_write_word_swapped(client, 302ceeadc5cSGuenter Roeck ina209_reset_history_regs[i], 1); 303ceeadc5cSGuenter Roeck } 304ceeadc5cSGuenter Roeck data->valid = false; 305ceeadc5cSGuenter Roeck mutex_unlock(&data->update_lock); 306ceeadc5cSGuenter Roeck return count; 307ceeadc5cSGuenter Roeck } 308ceeadc5cSGuenter Roeck 309ceeadc5cSGuenter Roeck static ssize_t ina209_set_value(struct device *dev, 310ceeadc5cSGuenter Roeck struct device_attribute *da, 311ceeadc5cSGuenter Roeck const char *buf, 312ceeadc5cSGuenter Roeck size_t count) 313ceeadc5cSGuenter Roeck { 314ceeadc5cSGuenter Roeck struct ina209_data *data = ina209_update_device(dev); 315ceeadc5cSGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 316ceeadc5cSGuenter Roeck int reg = attr->index; 317ceeadc5cSGuenter Roeck long val; 318ceeadc5cSGuenter Roeck int ret; 319ceeadc5cSGuenter Roeck 320ceeadc5cSGuenter Roeck if (IS_ERR(data)) 321ceeadc5cSGuenter Roeck return PTR_ERR(data); 322ceeadc5cSGuenter Roeck 323ceeadc5cSGuenter Roeck ret = kstrtol(buf, 10, &val); 324ceeadc5cSGuenter Roeck if (ret < 0) 325ceeadc5cSGuenter Roeck return ret; 326ceeadc5cSGuenter Roeck 327ceeadc5cSGuenter Roeck mutex_lock(&data->update_lock); 328ceeadc5cSGuenter Roeck ret = ina209_to_reg(reg, data->regs[reg], val); 329ceeadc5cSGuenter Roeck if (ret < 0) { 330ceeadc5cSGuenter Roeck count = ret; 331ceeadc5cSGuenter Roeck goto abort; 332ceeadc5cSGuenter Roeck } 333*89de7344SGuenter Roeck i2c_smbus_write_word_swapped(data->client, reg, ret); 334ceeadc5cSGuenter Roeck data->regs[reg] = ret; 335ceeadc5cSGuenter Roeck abort: 336ceeadc5cSGuenter Roeck mutex_unlock(&data->update_lock); 337ceeadc5cSGuenter Roeck return count; 338ceeadc5cSGuenter Roeck } 339ceeadc5cSGuenter Roeck 340ceeadc5cSGuenter Roeck static ssize_t ina209_show_value(struct device *dev, 341ceeadc5cSGuenter Roeck struct device_attribute *da, 342ceeadc5cSGuenter Roeck char *buf) 343ceeadc5cSGuenter Roeck { 344ceeadc5cSGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 345ceeadc5cSGuenter Roeck struct ina209_data *data = ina209_update_device(dev); 346ceeadc5cSGuenter Roeck long val; 347ceeadc5cSGuenter Roeck 348ceeadc5cSGuenter Roeck if (IS_ERR(data)) 349ceeadc5cSGuenter Roeck return PTR_ERR(data); 350ceeadc5cSGuenter Roeck 351ceeadc5cSGuenter Roeck val = ina209_from_reg(attr->index, data->regs[attr->index]); 352ceeadc5cSGuenter Roeck return snprintf(buf, PAGE_SIZE, "%ld\n", val); 353ceeadc5cSGuenter Roeck } 354ceeadc5cSGuenter Roeck 355ceeadc5cSGuenter Roeck static ssize_t ina209_show_alarm(struct device *dev, 356ceeadc5cSGuenter Roeck struct device_attribute *da, 357ceeadc5cSGuenter Roeck char *buf) 358ceeadc5cSGuenter Roeck { 359ceeadc5cSGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 360ceeadc5cSGuenter Roeck struct ina209_data *data = ina209_update_device(dev); 361ceeadc5cSGuenter Roeck const unsigned int mask = attr->index; 362ceeadc5cSGuenter Roeck u16 status; 363ceeadc5cSGuenter Roeck 364ceeadc5cSGuenter Roeck if (IS_ERR(data)) 365ceeadc5cSGuenter Roeck return PTR_ERR(data); 366ceeadc5cSGuenter Roeck 367ceeadc5cSGuenter Roeck status = data->regs[INA209_STATUS]; 368ceeadc5cSGuenter Roeck 369ceeadc5cSGuenter Roeck /* 370ceeadc5cSGuenter Roeck * All alarms are in the INA209_STATUS register. To avoid a long 371ceeadc5cSGuenter Roeck * switch statement, the mask is passed in attr->index 372ceeadc5cSGuenter Roeck */ 373ceeadc5cSGuenter Roeck return snprintf(buf, PAGE_SIZE, "%u\n", !!(status & mask)); 374ceeadc5cSGuenter Roeck } 375ceeadc5cSGuenter Roeck 376ceeadc5cSGuenter Roeck /* Shunt voltage, history, limits, alarms */ 377ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina209_show_value, NULL, 378ceeadc5cSGuenter Roeck INA209_SHUNT_VOLTAGE); 379ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in0_input_highest, S_IRUGO, ina209_show_value, NULL, 380ceeadc5cSGuenter Roeck INA209_SHUNT_VOLTAGE_POS_PEAK); 381ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in0_input_lowest, S_IRUGO, ina209_show_value, NULL, 382ceeadc5cSGuenter Roeck INA209_SHUNT_VOLTAGE_NEG_PEAK); 383ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in0_reset_history, S_IWUSR, NULL, 384ceeadc5cSGuenter Roeck ina209_reset_history, (1 << 0) | (1 << 1)); 385ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR, ina209_show_value, 386ceeadc5cSGuenter Roeck ina209_set_value, INA209_SHUNT_VOLTAGE_POS_WARN); 387ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR, ina209_show_value, 388ceeadc5cSGuenter Roeck ina209_set_value, INA209_SHUNT_VOLTAGE_NEG_WARN); 389ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in0_crit_max, S_IRUGO | S_IWUSR, ina209_show_value, 390ceeadc5cSGuenter Roeck ina209_set_value, INA209_CRITICAL_DAC_POS); 391ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in0_crit_min, S_IRUGO | S_IWUSR, ina209_show_value, 392ceeadc5cSGuenter Roeck ina209_set_value, INA209_CRITICAL_DAC_NEG); 393ceeadc5cSGuenter Roeck 394ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in0_min_alarm, S_IRUGO, ina209_show_alarm, NULL, 395ceeadc5cSGuenter Roeck 1 << 11); 396ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in0_max_alarm, S_IRUGO, ina209_show_alarm, NULL, 397ceeadc5cSGuenter Roeck 1 << 12); 398ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in0_crit_min_alarm, S_IRUGO, ina209_show_alarm, NULL, 399ceeadc5cSGuenter Roeck 1 << 6); 400ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in0_crit_max_alarm, S_IRUGO, ina209_show_alarm, NULL, 401ceeadc5cSGuenter Roeck 1 << 7); 402ceeadc5cSGuenter Roeck 403ceeadc5cSGuenter Roeck /* Bus voltage, history, limits, alarms */ 404ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina209_show_value, NULL, 405ceeadc5cSGuenter Roeck INA209_BUS_VOLTAGE); 406ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in1_input_highest, S_IRUGO, ina209_show_value, NULL, 407ceeadc5cSGuenter Roeck INA209_BUS_VOLTAGE_MAX_PEAK); 408ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in1_input_lowest, S_IRUGO, ina209_show_value, NULL, 409ceeadc5cSGuenter Roeck INA209_BUS_VOLTAGE_MIN_PEAK); 410ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in1_reset_history, S_IWUSR, NULL, 411ceeadc5cSGuenter Roeck ina209_reset_history, (1 << 2) | (1 << 3)); 412ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR, ina209_show_value, 413ceeadc5cSGuenter Roeck ina209_set_value, INA209_BUS_VOLTAGE_OVER_WARN); 414ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR, ina209_show_value, 415ceeadc5cSGuenter Roeck ina209_set_value, INA209_BUS_VOLTAGE_UNDER_WARN); 416ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in1_crit_max, S_IRUGO | S_IWUSR, ina209_show_value, 417ceeadc5cSGuenter Roeck ina209_set_value, INA209_BUS_VOLTAGE_OVER_LIMIT); 418ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in1_crit_min, S_IRUGO | S_IWUSR, ina209_show_value, 419ceeadc5cSGuenter Roeck ina209_set_value, INA209_BUS_VOLTAGE_UNDER_LIMIT); 420ceeadc5cSGuenter Roeck 421ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in1_min_alarm, S_IRUGO, ina209_show_alarm, NULL, 422ceeadc5cSGuenter Roeck 1 << 14); 423ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in1_max_alarm, S_IRUGO, ina209_show_alarm, NULL, 424ceeadc5cSGuenter Roeck 1 << 15); 425ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in1_crit_min_alarm, S_IRUGO, ina209_show_alarm, NULL, 426ceeadc5cSGuenter Roeck 1 << 9); 427ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(in1_crit_max_alarm, S_IRUGO, ina209_show_alarm, NULL, 428ceeadc5cSGuenter Roeck 1 << 10); 429ceeadc5cSGuenter Roeck 430ceeadc5cSGuenter Roeck /* Power */ 431ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina209_show_value, NULL, 432ceeadc5cSGuenter Roeck INA209_POWER); 433ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(power1_input_highest, S_IRUGO, ina209_show_value, 434ceeadc5cSGuenter Roeck NULL, INA209_POWER_PEAK); 435ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(power1_reset_history, S_IWUSR, NULL, 436ceeadc5cSGuenter Roeck ina209_reset_history, 1 << 4); 437ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(power1_max, S_IRUGO | S_IWUSR, ina209_show_value, 438ceeadc5cSGuenter Roeck ina209_set_value, INA209_POWER_WARN); 439ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(power1_crit, S_IRUGO | S_IWUSR, ina209_show_value, 440ceeadc5cSGuenter Roeck ina209_set_value, INA209_POWER_OVER_LIMIT); 441ceeadc5cSGuenter Roeck 442ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(power1_max_alarm, S_IRUGO, ina209_show_alarm, NULL, 443ceeadc5cSGuenter Roeck 1 << 13); 444ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(power1_crit_alarm, S_IRUGO, ina209_show_alarm, NULL, 445ceeadc5cSGuenter Roeck 1 << 8); 446ceeadc5cSGuenter Roeck 447ceeadc5cSGuenter Roeck /* Current */ 448ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina209_show_value, NULL, 449ceeadc5cSGuenter Roeck INA209_CURRENT); 450ceeadc5cSGuenter Roeck 451ceeadc5cSGuenter Roeck static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, 452ceeadc5cSGuenter Roeck ina209_show_interval, ina209_set_interval, 0); 453ceeadc5cSGuenter Roeck 454ceeadc5cSGuenter Roeck /* 455ceeadc5cSGuenter Roeck * Finally, construct an array of pointers to members of the above objects, 456ceeadc5cSGuenter Roeck * as required for sysfs_create_group() 457ceeadc5cSGuenter Roeck */ 458*89de7344SGuenter Roeck static struct attribute *ina209_attrs[] = { 459ceeadc5cSGuenter Roeck &sensor_dev_attr_in0_input.dev_attr.attr, 460ceeadc5cSGuenter Roeck &sensor_dev_attr_in0_input_highest.dev_attr.attr, 461ceeadc5cSGuenter Roeck &sensor_dev_attr_in0_input_lowest.dev_attr.attr, 462ceeadc5cSGuenter Roeck &sensor_dev_attr_in0_reset_history.dev_attr.attr, 463ceeadc5cSGuenter Roeck &sensor_dev_attr_in0_max.dev_attr.attr, 464ceeadc5cSGuenter Roeck &sensor_dev_attr_in0_min.dev_attr.attr, 465ceeadc5cSGuenter Roeck &sensor_dev_attr_in0_crit_max.dev_attr.attr, 466ceeadc5cSGuenter Roeck &sensor_dev_attr_in0_crit_min.dev_attr.attr, 467ceeadc5cSGuenter Roeck &sensor_dev_attr_in0_max_alarm.dev_attr.attr, 468ceeadc5cSGuenter Roeck &sensor_dev_attr_in0_min_alarm.dev_attr.attr, 469ceeadc5cSGuenter Roeck &sensor_dev_attr_in0_crit_max_alarm.dev_attr.attr, 470ceeadc5cSGuenter Roeck &sensor_dev_attr_in0_crit_min_alarm.dev_attr.attr, 471ceeadc5cSGuenter Roeck 472ceeadc5cSGuenter Roeck &sensor_dev_attr_in1_input.dev_attr.attr, 473ceeadc5cSGuenter Roeck &sensor_dev_attr_in1_input_highest.dev_attr.attr, 474ceeadc5cSGuenter Roeck &sensor_dev_attr_in1_input_lowest.dev_attr.attr, 475ceeadc5cSGuenter Roeck &sensor_dev_attr_in1_reset_history.dev_attr.attr, 476ceeadc5cSGuenter Roeck &sensor_dev_attr_in1_max.dev_attr.attr, 477ceeadc5cSGuenter Roeck &sensor_dev_attr_in1_min.dev_attr.attr, 478ceeadc5cSGuenter Roeck &sensor_dev_attr_in1_crit_max.dev_attr.attr, 479ceeadc5cSGuenter Roeck &sensor_dev_attr_in1_crit_min.dev_attr.attr, 480ceeadc5cSGuenter Roeck &sensor_dev_attr_in1_max_alarm.dev_attr.attr, 481ceeadc5cSGuenter Roeck &sensor_dev_attr_in1_min_alarm.dev_attr.attr, 482ceeadc5cSGuenter Roeck &sensor_dev_attr_in1_crit_max_alarm.dev_attr.attr, 483ceeadc5cSGuenter Roeck &sensor_dev_attr_in1_crit_min_alarm.dev_attr.attr, 484ceeadc5cSGuenter Roeck 485ceeadc5cSGuenter Roeck &sensor_dev_attr_power1_input.dev_attr.attr, 486ceeadc5cSGuenter Roeck &sensor_dev_attr_power1_input_highest.dev_attr.attr, 487ceeadc5cSGuenter Roeck &sensor_dev_attr_power1_reset_history.dev_attr.attr, 488ceeadc5cSGuenter Roeck &sensor_dev_attr_power1_max.dev_attr.attr, 489ceeadc5cSGuenter Roeck &sensor_dev_attr_power1_crit.dev_attr.attr, 490ceeadc5cSGuenter Roeck &sensor_dev_attr_power1_max_alarm.dev_attr.attr, 491ceeadc5cSGuenter Roeck &sensor_dev_attr_power1_crit_alarm.dev_attr.attr, 492ceeadc5cSGuenter Roeck 493ceeadc5cSGuenter Roeck &sensor_dev_attr_curr1_input.dev_attr.attr, 494ceeadc5cSGuenter Roeck 495ceeadc5cSGuenter Roeck &sensor_dev_attr_update_interval.dev_attr.attr, 496ceeadc5cSGuenter Roeck 497ceeadc5cSGuenter Roeck NULL, 498ceeadc5cSGuenter Roeck }; 499*89de7344SGuenter Roeck ATTRIBUTE_GROUPS(ina209); 500ceeadc5cSGuenter Roeck 501ceeadc5cSGuenter Roeck static void ina209_restore_conf(struct i2c_client *client, 502ceeadc5cSGuenter Roeck struct ina209_data *data) 503ceeadc5cSGuenter Roeck { 504ceeadc5cSGuenter Roeck /* Restore initial configuration */ 505ceeadc5cSGuenter Roeck i2c_smbus_write_word_swapped(client, INA209_CONFIGURATION, 506ceeadc5cSGuenter Roeck data->config_orig); 507ceeadc5cSGuenter Roeck i2c_smbus_write_word_swapped(client, INA209_CALIBRATION, 508ceeadc5cSGuenter Roeck data->calibration_orig); 509ceeadc5cSGuenter Roeck } 510ceeadc5cSGuenter Roeck 511ceeadc5cSGuenter Roeck static int ina209_init_client(struct i2c_client *client, 512ceeadc5cSGuenter Roeck struct ina209_data *data) 513ceeadc5cSGuenter Roeck { 514ceeadc5cSGuenter Roeck struct ina2xx_platform_data *pdata = dev_get_platdata(&client->dev); 515ceeadc5cSGuenter Roeck u32 shunt; 516ceeadc5cSGuenter Roeck int reg; 517ceeadc5cSGuenter Roeck 518ceeadc5cSGuenter Roeck reg = i2c_smbus_read_word_swapped(client, INA209_CALIBRATION); 519ceeadc5cSGuenter Roeck if (reg < 0) 520ceeadc5cSGuenter Roeck return reg; 521ceeadc5cSGuenter Roeck data->calibration_orig = reg; 522ceeadc5cSGuenter Roeck 523ceeadc5cSGuenter Roeck reg = i2c_smbus_read_word_swapped(client, INA209_CONFIGURATION); 524ceeadc5cSGuenter Roeck if (reg < 0) 525ceeadc5cSGuenter Roeck return reg; 526ceeadc5cSGuenter Roeck data->config_orig = reg; 527ceeadc5cSGuenter Roeck 528ceeadc5cSGuenter Roeck if (pdata) { 529ceeadc5cSGuenter Roeck if (pdata->shunt_uohms <= 0) 530ceeadc5cSGuenter Roeck return -EINVAL; 531ceeadc5cSGuenter Roeck shunt = pdata->shunt_uohms; 532ceeadc5cSGuenter Roeck } else if (!of_property_read_u32(client->dev.of_node, "shunt-resistor", 533ceeadc5cSGuenter Roeck &shunt)) { 534ceeadc5cSGuenter Roeck if (shunt == 0) 535ceeadc5cSGuenter Roeck return -EINVAL; 536ceeadc5cSGuenter Roeck } else { 537ceeadc5cSGuenter Roeck shunt = data->calibration_orig ? 538ceeadc5cSGuenter Roeck 40960000 / data->calibration_orig : INA209_SHUNT_DEFAULT; 539ceeadc5cSGuenter Roeck } 540ceeadc5cSGuenter Roeck 541ceeadc5cSGuenter Roeck i2c_smbus_write_word_swapped(client, INA209_CONFIGURATION, 542ceeadc5cSGuenter Roeck INA209_CONFIG_DEFAULT); 543ceeadc5cSGuenter Roeck data->update_interval = ina209_interval_from_reg(INA209_CONFIG_DEFAULT); 544ceeadc5cSGuenter Roeck 545ceeadc5cSGuenter Roeck /* 546ceeadc5cSGuenter Roeck * Calibrate current LSB to 1mA. Shunt is in uOhms. 547ceeadc5cSGuenter Roeck * See equation 13 in datasheet. 548ceeadc5cSGuenter Roeck */ 549ceeadc5cSGuenter Roeck i2c_smbus_write_word_swapped(client, INA209_CALIBRATION, 550ceeadc5cSGuenter Roeck clamp_val(40960000 / shunt, 1, 65535)); 551ceeadc5cSGuenter Roeck 552ceeadc5cSGuenter Roeck /* Clear status register */ 553ceeadc5cSGuenter Roeck i2c_smbus_read_word_swapped(client, INA209_STATUS); 554ceeadc5cSGuenter Roeck 555ceeadc5cSGuenter Roeck return 0; 556ceeadc5cSGuenter Roeck } 557ceeadc5cSGuenter Roeck 558ceeadc5cSGuenter Roeck static int ina209_probe(struct i2c_client *client, 559ceeadc5cSGuenter Roeck const struct i2c_device_id *id) 560ceeadc5cSGuenter Roeck { 561ceeadc5cSGuenter Roeck struct i2c_adapter *adapter = client->adapter; 562ceeadc5cSGuenter Roeck struct ina209_data *data; 563*89de7344SGuenter Roeck struct device *hwmon_dev; 564ceeadc5cSGuenter Roeck int ret; 565ceeadc5cSGuenter Roeck 566ceeadc5cSGuenter Roeck if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) 567ceeadc5cSGuenter Roeck return -ENODEV; 568ceeadc5cSGuenter Roeck 569ceeadc5cSGuenter Roeck data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); 570ceeadc5cSGuenter Roeck if (!data) 571ceeadc5cSGuenter Roeck return -ENOMEM; 572ceeadc5cSGuenter Roeck 573ceeadc5cSGuenter Roeck i2c_set_clientdata(client, data); 574*89de7344SGuenter Roeck data->client = client; 575ceeadc5cSGuenter Roeck mutex_init(&data->update_lock); 576ceeadc5cSGuenter Roeck 577ceeadc5cSGuenter Roeck ret = ina209_init_client(client, data); 578ceeadc5cSGuenter Roeck if (ret) 579ceeadc5cSGuenter Roeck return ret; 580ceeadc5cSGuenter Roeck 581*89de7344SGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev, 582*89de7344SGuenter Roeck client->name, 583*89de7344SGuenter Roeck data, ina209_groups); 584*89de7344SGuenter Roeck if (IS_ERR(hwmon_dev)) { 585*89de7344SGuenter Roeck ret = PTR_ERR(hwmon_dev); 586ceeadc5cSGuenter Roeck goto out_restore_conf; 587ceeadc5cSGuenter Roeck } 588ceeadc5cSGuenter Roeck 589ceeadc5cSGuenter Roeck return 0; 590ceeadc5cSGuenter Roeck 591ceeadc5cSGuenter Roeck out_restore_conf: 592ceeadc5cSGuenter Roeck ina209_restore_conf(client, data); 593ceeadc5cSGuenter Roeck return ret; 594ceeadc5cSGuenter Roeck } 595ceeadc5cSGuenter Roeck 596ceeadc5cSGuenter Roeck static int ina209_remove(struct i2c_client *client) 597ceeadc5cSGuenter Roeck { 598ceeadc5cSGuenter Roeck struct ina209_data *data = i2c_get_clientdata(client); 599ceeadc5cSGuenter Roeck 600ceeadc5cSGuenter Roeck ina209_restore_conf(client, data); 601ceeadc5cSGuenter Roeck 602ceeadc5cSGuenter Roeck return 0; 603ceeadc5cSGuenter Roeck } 604ceeadc5cSGuenter Roeck 605ceeadc5cSGuenter Roeck static const struct i2c_device_id ina209_id[] = { 606ceeadc5cSGuenter Roeck { "ina209", 0 }, 607ceeadc5cSGuenter Roeck { } 608ceeadc5cSGuenter Roeck }; 609ceeadc5cSGuenter Roeck MODULE_DEVICE_TABLE(i2c, ina209_id); 610ceeadc5cSGuenter Roeck 611ceeadc5cSGuenter Roeck /* This is the driver that will be inserted */ 612ceeadc5cSGuenter Roeck static struct i2c_driver ina209_driver = { 613ceeadc5cSGuenter Roeck .class = I2C_CLASS_HWMON, 614ceeadc5cSGuenter Roeck .driver = { 615ceeadc5cSGuenter Roeck .name = "ina209", 616ceeadc5cSGuenter Roeck }, 617ceeadc5cSGuenter Roeck .probe = ina209_probe, 618ceeadc5cSGuenter Roeck .remove = ina209_remove, 619ceeadc5cSGuenter Roeck .id_table = ina209_id, 620ceeadc5cSGuenter Roeck }; 621ceeadc5cSGuenter Roeck 622ceeadc5cSGuenter Roeck module_i2c_driver(ina209_driver); 623ceeadc5cSGuenter Roeck 624ceeadc5cSGuenter Roeck MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>, Paul Hays <Paul.Hays@cattail.ca>, Guenter Roeck <linux@roeck-us.net>"); 625ceeadc5cSGuenter Roeck MODULE_DESCRIPTION("INA209 driver"); 626ceeadc5cSGuenter Roeck MODULE_LICENSE("GPL"); 627