diff options
Diffstat (limited to 'drivers/hwmon/ina2xx.c')
| -rw-r--r-- | drivers/hwmon/ina2xx.c | 270 |
1 files changed, 97 insertions, 173 deletions
diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c index 7f3f4a38572..bfd3f3eeabc 100644 --- a/drivers/hwmon/ina2xx.c +++ b/drivers/hwmon/ina2xx.c @@ -5,10 +5,18 @@ * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface * Datasheet: http://www.ti.com/product/ina219 * + * INA220: + * Bi-Directional Current/Power Monitor with I2C Interface + * Datasheet: http://www.ti.com/product/ina220 + * * INA226: * Bi-Directional Current/Power Monitor with I2C Interface * Datasheet: http://www.ti.com/product/ina226 * + * INA230: + * Bi-directional Current/Power Monitor with I2C Interface + * Datasheet: http://www.ti.com/product/ina230 + * * Copyright (C) 2012 Lothar Felten <l-felten@ti.com> * Thanks to Jan Volkering * @@ -25,6 +33,8 @@ #include <linux/i2c.h> #include <linux/hwmon.h> #include <linux/hwmon-sysfs.h> +#include <linux/jiffies.h> +#include <linux/of.h> #include <linux/platform_data/ina2xx.h> @@ -57,38 +67,53 @@ enum ina2xx_ids { ina219, ina226 }; +struct ina2xx_config { + u16 config_default; + int calibration_factor; + int registers; + int shunt_div; + int bus_voltage_shift; + int bus_voltage_lsb; /* uV */ + int power_lsb; /* uW */ +}; + struct ina2xx_data { - struct device *hwmon_dev; + struct i2c_client *client; + const struct ina2xx_config *config; struct mutex update_lock; bool valid; unsigned long last_updated; int kind; - int registers; u16 regs[INA2XX_MAX_REGISTERS]; }; -int ina2xx_read_word(struct i2c_client *client, int reg) -{ - int val = i2c_smbus_read_word_data(client, reg); - if (unlikely(val < 0)) { - dev_dbg(&client->dev, - "Failed to read register: %d\n", reg); - return val; - } - return be16_to_cpu(val); -} - -void ina2xx_write_word(struct i2c_client *client, int reg, int data) -{ - i2c_smbus_write_word_data(client, reg, cpu_to_be16(data)); -} +static const struct ina2xx_config ina2xx_config[] = { + [ina219] = { + .config_default = INA219_CONFIG_DEFAULT, + .calibration_factor = 40960000, + .registers = INA219_REGISTERS, + .shunt_div = 100, + .bus_voltage_shift = 3, + .bus_voltage_lsb = 4000, + .power_lsb = 20000, + }, + [ina226] = { + .config_default = INA226_CONFIG_DEFAULT, + .calibration_factor = 5120000, + .registers = INA226_REGISTERS, + .shunt_div = 400, + .bus_voltage_shift = 0, + .bus_voltage_lsb = 1250, + .power_lsb = 25000, + }, +}; static struct ina2xx_data *ina2xx_update_device(struct device *dev) { - struct i2c_client *client = to_i2c_client(dev); - struct ina2xx_data *data = i2c_get_clientdata(client); + struct ina2xx_data *data = dev_get_drvdata(dev); + struct i2c_client *client = data->client; struct ina2xx_data *ret = data; mutex_lock(&data->update_lock); @@ -101,8 +126,8 @@ static struct ina2xx_data *ina2xx_update_device(struct device *dev) dev_dbg(&client->dev, "Starting ina2xx update\n"); /* Read all registers */ - for (i = 0; i < data->registers; i++) { - int rv = ina2xx_read_word(client, i); + for (i = 0; i < data->config->registers; i++) { + int rv = i2c_smbus_read_word_swapped(client, i); if (rv < 0) { ret = ERR_PTR(rv); goto abort; @@ -117,73 +142,27 @@ abort: return ret; } -static int ina219_get_value(struct ina2xx_data *data, u8 reg) +static int ina2xx_get_value(struct ina2xx_data *data, u8 reg) { - /* - * calculate exact value for the given register - * we assume default power-on reset settings: - * bus voltage range 32V - * gain = /8 - * adc 1 & 2 -> conversion time 532uS - * mode is continuous shunt and bus - * calibration value is INA219_CALIBRATION_VALUE - */ - int val = data->regs[reg]; + int val; switch (reg) { case INA2XX_SHUNT_VOLTAGE: - /* LSB=10uV. Convert to mV. */ - val = DIV_ROUND_CLOSEST(val, 100); + /* signed register */ + val = DIV_ROUND_CLOSEST((s16)data->regs[reg], + data->config->shunt_div); break; case INA2XX_BUS_VOLTAGE: - /* LSB=4mV. Register is not right aligned, convert to mV. */ - val = (val >> 3) * 4; + val = (data->regs[reg] >> data->config->bus_voltage_shift) + * data->config->bus_voltage_lsb; + val = DIV_ROUND_CLOSEST(val, 1000); break; case INA2XX_POWER: - /* LSB=20mW. Convert to uW */ - val = val * 20 * 1000; + val = data->regs[reg] * data->config->power_lsb; break; case INA2XX_CURRENT: - /* LSB=1mA (selected). Is in mA */ - break; - default: - /* programmer goofed */ - WARN_ON_ONCE(1); - val = 0; - break; - } - - return val; -} - -static int ina226_get_value(struct ina2xx_data *data, u8 reg) -{ - /* - * calculate exact value for the given register - * we assume default power-on reset settings: - * bus voltage range 32V - * gain = /8 - * adc 1 & 2 -> conversion time 532uS - * mode is continuous shunt and bus - * calibration value is INA226_CALIBRATION_VALUE - */ - int val = data->regs[reg]; - - switch (reg) { - case INA2XX_SHUNT_VOLTAGE: - /* LSB=2.5uV. Convert to mV. */ - val = DIV_ROUND_CLOSEST(val, 400); - break; - case INA2XX_BUS_VOLTAGE: - /* LSB=1.25mV. Convert to mV. */ - val = val + DIV_ROUND_CLOSEST(val, 4); - break; - case INA2XX_POWER: - /* LSB=25mW. Convert to uW */ - val = val * 25 * 1000; - break; - case INA2XX_CURRENT: - /* LSB=1mA (selected). Is in mA */ + /* signed register, LSB=1mA (selected), in mA */ + val = (s16)data->regs[reg]; break; default: /* programmer goofed */ @@ -200,74 +179,64 @@ static ssize_t ina2xx_show_value(struct device *dev, { struct sensor_device_attribute *attr = to_sensor_dev_attr(da); struct ina2xx_data *data = ina2xx_update_device(dev); - int value = 0; if (IS_ERR(data)) return PTR_ERR(data); - switch (data->kind) { - case ina219: - value = ina219_get_value(data, attr->index); - break; - case ina226: - value = ina226_get_value(data, attr->index); - break; - default: - WARN_ON_ONCE(1); - break; - } - return snprintf(buf, PAGE_SIZE, "%d\n", value); + return snprintf(buf, PAGE_SIZE, "%d\n", + ina2xx_get_value(data, attr->index)); } /* shunt voltage */ -static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, \ - ina2xx_show_value, NULL, INA2XX_SHUNT_VOLTAGE); +static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL, + INA2XX_SHUNT_VOLTAGE); /* bus voltage */ -static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, \ - ina2xx_show_value, NULL, INA2XX_BUS_VOLTAGE); +static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL, + INA2XX_BUS_VOLTAGE); /* calculated current */ -static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, \ - ina2xx_show_value, NULL, INA2XX_CURRENT); +static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL, + INA2XX_CURRENT); /* calculated power */ -static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, \ - ina2xx_show_value, NULL, INA2XX_POWER); +static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL, + INA2XX_POWER); /* pointers to created device attributes */ -static struct attribute *ina2xx_attributes[] = { +static struct attribute *ina2xx_attrs[] = { &sensor_dev_attr_in0_input.dev_attr.attr, &sensor_dev_attr_in1_input.dev_attr.attr, &sensor_dev_attr_curr1_input.dev_attr.attr, &sensor_dev_attr_power1_input.dev_attr.attr, NULL, }; - -static const struct attribute_group ina2xx_group = { - .attrs = ina2xx_attributes, -}; +ATTRIBUTE_GROUPS(ina2xx); static int ina2xx_probe(struct i2c_client *client, const struct i2c_device_id *id) { struct i2c_adapter *adapter = client->adapter; - struct ina2xx_data *data; struct ina2xx_platform_data *pdata; - int ret = 0; + struct device *dev = &client->dev; + struct ina2xx_data *data; + struct device *hwmon_dev; long shunt = 10000; /* default shunt value 10mOhms */ + u32 val; if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) return -ENODEV; - data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); if (!data) return -ENOMEM; - if (client->dev.platform_data) { - pdata = - (struct ina2xx_platform_data *)client->dev.platform_data; + if (dev_get_platdata(dev)) { + pdata = dev_get_platdata(dev); shunt = pdata->shunt_uohms; + } else if (!of_property_read_u32(dev->of_node, + "shunt-resistor", &val)) { + shunt = val; } if (shunt <= 0) @@ -275,68 +244,35 @@ static int ina2xx_probe(struct i2c_client *client, /* set the device type */ data->kind = id->driver_data; + data->config = &ina2xx_config[data->kind]; - switch (data->kind) { - case ina219: - /* device configuration */ - ina2xx_write_word(client, INA2XX_CONFIG, INA219_CONFIG_DEFAULT); - - /* set current LSB to 1mA, shunt is in uOhms */ - /* (equation 13 in datasheet) */ - ina2xx_write_word(client, INA2XX_CALIBRATION, 40960000 / shunt); - dev_info(&client->dev, - "power monitor INA219 (Rshunt = %li uOhm)\n", shunt); - data->registers = INA219_REGISTERS; - break; - case ina226: - /* device configuration */ - ina2xx_write_word(client, INA2XX_CONFIG, INA226_CONFIG_DEFAULT); - - /* set current LSB to 1mA, shunt is in uOhms */ - /* (equation 1 in datasheet)*/ - ina2xx_write_word(client, INA2XX_CALIBRATION, 5120000 / shunt); - dev_info(&client->dev, - "power monitor INA226 (Rshunt = %li uOhm)\n", shunt); - data->registers = INA226_REGISTERS; - break; - default: - /* unknown device id */ - return -ENODEV; - } + /* device configuration */ + i2c_smbus_write_word_swapped(client, INA2XX_CONFIG, + data->config->config_default); + /* set current LSB to 1mA, shunt is in uOhms */ + /* (equation 13 in datasheet) */ + i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION, + data->config->calibration_factor / shunt); - i2c_set_clientdata(client, data); + data->client = client; mutex_init(&data->update_lock); - ret = sysfs_create_group(&client->dev.kobj, &ina2xx_group); - if (ret) - return ret; + hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, + data, ina2xx_groups); + if (IS_ERR(hwmon_dev)) + return PTR_ERR(hwmon_dev); - data->hwmon_dev = hwmon_device_register(&client->dev); - if (IS_ERR(data->hwmon_dev)) { - ret = PTR_ERR(data->hwmon_dev); - goto out_err_hwmon; - } - - return 0; - -out_err_hwmon: - sysfs_remove_group(&client->dev.kobj, &ina2xx_group); - return ret; -} - -static int ina2xx_remove(struct i2c_client *client) -{ - struct ina2xx_data *data = i2c_get_clientdata(client); - - hwmon_device_unregister(data->hwmon_dev); - sysfs_remove_group(&client->dev.kobj, &ina2xx_group); + dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n", + id->name, shunt); return 0; } static const struct i2c_device_id ina2xx_id[] = { { "ina219", ina219 }, + { "ina220", ina219 }, { "ina226", ina226 }, + { "ina230", ina226 }, { } }; MODULE_DEVICE_TABLE(i2c, ina2xx_id); @@ -346,23 +282,11 @@ static struct i2c_driver ina2xx_driver = { .name = "ina2xx", }, .probe = ina2xx_probe, - .remove = ina2xx_remove, .id_table = ina2xx_id, }; -static int __init ina2xx_init(void) -{ - return i2c_add_driver(&ina2xx_driver); -} - -static void __exit ina2xx_exit(void) -{ - i2c_del_driver(&ina2xx_driver); -} +module_i2c_driver(ina2xx_driver); MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>"); MODULE_DESCRIPTION("ina2xx driver"); MODULE_LICENSE("GPL"); - -module_init(ina2xx_init); -module_exit(ina2xx_exit); |
