Commit a0de56c8 authored by Marc Titinger's avatar Marc Titinger Committed by Guenter Roeck

hwmon: (ina2xx) convert driver to using regmap

Any sysfs "show" read access from the client app will result in reading
all registers (8 with ina226). Depending on the host this can limit the
best achievable read rate.

This changeset allows for individual register accesses through regmap.

Tested with BeagleBone Black (Baylibre-ACME) and ina226.
Signed-off-by: default avatarMarc Titinger <mtitinger@baylibre.com>
Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
parent cc904f9c
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include <linux/of.h> #include <linux/of.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/util_macros.h> #include <linux/util_macros.h>
#include <linux/regmap.h>
#include <linux/platform_data/ina2xx.h> #include <linux/platform_data/ina2xx.h>
...@@ -84,6 +85,11 @@ ...@@ -84,6 +85,11 @@
*/ */
#define INA226_TOTAL_CONV_TIME_DEFAULT 2200 #define INA226_TOTAL_CONV_TIME_DEFAULT 2200
static struct regmap_config ina2xx_regmap_config = {
.reg_bits = 8,
.val_bits = 16,
};
enum ina2xx_ids { ina219, ina226 }; enum ina2xx_ids { ina219, ina226 };
struct ina2xx_config { struct ina2xx_config {
...@@ -97,20 +103,14 @@ struct ina2xx_config { ...@@ -97,20 +103,14 @@ struct ina2xx_config {
}; };
struct ina2xx_data { struct ina2xx_data {
struct i2c_client *client;
const struct ina2xx_config *config; const struct ina2xx_config *config;
long rshunt; long rshunt;
u16 curr_config; struct mutex config_lock;
struct regmap *regmap;
struct mutex update_lock;
bool valid;
unsigned long last_updated;
int update_interval; /* in jiffies */
int kind; int kind;
const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS]; const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
u16 regs[INA2XX_MAX_REGISTERS];
}; };
static const struct ina2xx_config ina2xx_config[] = { static const struct ina2xx_config ina2xx_config[] = {
...@@ -153,7 +153,11 @@ static int ina226_reg_to_interval(u16 config) ...@@ -153,7 +153,11 @@ static int ina226_reg_to_interval(u16 config)
return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000); return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
} }
static u16 ina226_interval_to_reg(int interval, u16 config) /*
* Return the new, shifted AVG field value of CONFIG register,
* to use with regmap_update_bits
*/
static u16 ina226_interval_to_reg(int interval)
{ {
int avg, avg_bits; int avg, avg_bits;
...@@ -162,15 +166,7 @@ static u16 ina226_interval_to_reg(int interval, u16 config) ...@@ -162,15 +166,7 @@ static u16 ina226_interval_to_reg(int interval, u16 config)
avg_bits = find_closest(avg, ina226_avg_tab, avg_bits = find_closest(avg, ina226_avg_tab,
ARRAY_SIZE(ina226_avg_tab)); ARRAY_SIZE(ina226_avg_tab));
return (config & ~INA226_AVG_RD_MASK) | INA226_SHIFT_AVG(avg_bits); return INA226_SHIFT_AVG(avg_bits);
}
static void ina226_set_update_interval(struct ina2xx_data *data)
{
int ms;
ms = ina226_reg_to_interval(data->curr_config);
data->update_interval = msecs_to_jiffies(ms);
} }
static int ina2xx_calibrate(struct ina2xx_data *data) static int ina2xx_calibrate(struct ina2xx_data *data)
...@@ -178,8 +174,7 @@ static int ina2xx_calibrate(struct ina2xx_data *data) ...@@ -178,8 +174,7 @@ static int ina2xx_calibrate(struct ina2xx_data *data)
u16 val = DIV_ROUND_CLOSEST(data->config->calibration_factor, u16 val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
data->rshunt); data->rshunt);
return i2c_smbus_write_word_swapped(data->client, return regmap_write(data->regmap, INA2XX_CALIBRATION, val);
INA2XX_CALIBRATION, val);
} }
/* /*
...@@ -187,12 +182,8 @@ static int ina2xx_calibrate(struct ina2xx_data *data) ...@@ -187,12 +182,8 @@ static int ina2xx_calibrate(struct ina2xx_data *data)
*/ */
static int ina2xx_init(struct ina2xx_data *data) static int ina2xx_init(struct ina2xx_data *data)
{ {
struct i2c_client *client = data->client; int ret = regmap_write(data->regmap, INA2XX_CONFIG,
int ret; data->config->config_default);
/* device configuration */
ret = i2c_smbus_write_word_swapped(client, INA2XX_CONFIG,
data->curr_config);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -203,47 +194,52 @@ static int ina2xx_init(struct ina2xx_data *data) ...@@ -203,47 +194,52 @@ static int ina2xx_init(struct ina2xx_data *data)
return ina2xx_calibrate(data); return ina2xx_calibrate(data);
} }
static int ina2xx_do_update(struct device *dev) static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
{ {
struct ina2xx_data *data = dev_get_drvdata(dev); struct ina2xx_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client; int ret, retry;
int i, rv, retry;
dev_dbg(&client->dev, "Starting ina2xx update\n"); dev_dbg(dev, "Starting register %d read\n", reg);
for (retry = 5; retry; retry--) { for (retry = 5; retry; retry--) {
/* Read all registers */
for (i = 0; i < data->config->registers; i++) { ret = regmap_read(data->regmap, reg, regval);
rv = i2c_smbus_read_word_swapped(client, i); if (ret < 0)
if (rv < 0) return ret;
return rv;
data->regs[i] = rv; dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
}
/* /*
* If the current value in the calibration register is 0, the * If the current value in the calibration register is 0, the
* power and current registers will also remain at 0. In case * power and current registers will also remain at 0. In case
* the chip has been reset let's check the calibration * the chip has been reset let's check the calibration
* register and reinitialize if needed. * register and reinitialize if needed.
* We do that extra read of the calibration register if there
* is some hint of a chip reset.
*/ */
if (data->regs[INA2XX_CALIBRATION] == 0) { if (*regval == 0) {
dev_warn(dev, "chip not calibrated, reinitializing\n"); unsigned int cal;
rv = ina2xx_init(data); ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
if (rv < 0) &cal);
return rv; if (ret < 0)
return ret;
/*
* Let's make sure the power and current registers if (cal == 0) {
* have been updated before trying again. dev_warn(dev, "chip not calibrated, reinitializing\n");
*/
msleep(INA2XX_MAX_DELAY); ret = ina2xx_init(data);
continue; if (ret < 0)
return ret;
/*
* Let's make sure the power and current
* registers have been updated before trying
* again.
*/
msleep(INA2XX_MAX_DELAY);
continue;
}
} }
data->last_updated = jiffies;
data->valid = 1;
return 0; return 0;
} }
...@@ -256,51 +252,31 @@ static int ina2xx_do_update(struct device *dev) ...@@ -256,51 +252,31 @@ static int ina2xx_do_update(struct device *dev)
return -ENODEV; return -ENODEV;
} }
static struct ina2xx_data *ina2xx_update_device(struct device *dev) static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
{ unsigned int regval)
struct ina2xx_data *data = dev_get_drvdata(dev);
struct ina2xx_data *ret = data;
unsigned long after;
int rv;
mutex_lock(&data->update_lock);
after = data->last_updated + data->update_interval;
if (time_after(jiffies, after) || !data->valid) {
rv = ina2xx_do_update(dev);
if (rv < 0)
ret = ERR_PTR(rv);
}
mutex_unlock(&data->update_lock);
return ret;
}
static int ina2xx_get_value(struct ina2xx_data *data, u8 reg)
{ {
int val; int val;
switch (reg) { switch (reg) {
case INA2XX_SHUNT_VOLTAGE: case INA2XX_SHUNT_VOLTAGE:
/* signed register */ /* signed register */
val = DIV_ROUND_CLOSEST((s16)data->regs[reg], val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
data->config->shunt_div);
break; break;
case INA2XX_BUS_VOLTAGE: case INA2XX_BUS_VOLTAGE:
val = (data->regs[reg] >> data->config->bus_voltage_shift) val = (regval >> data->config->bus_voltage_shift)
* data->config->bus_voltage_lsb; * data->config->bus_voltage_lsb;
val = DIV_ROUND_CLOSEST(val, 1000); val = DIV_ROUND_CLOSEST(val, 1000);
break; break;
case INA2XX_POWER: case INA2XX_POWER:
val = data->regs[reg] * data->config->power_lsb; val = regval * data->config->power_lsb;
break; break;
case INA2XX_CURRENT: case INA2XX_CURRENT:
/* signed register, LSB=1mA (selected), in mA */ /* signed register, LSB=1mA (selected), in mA */
val = (s16)data->regs[reg]; val = (s16)regval;
break; break;
case INA2XX_CALIBRATION: case INA2XX_CALIBRATION:
val = DIV_ROUND_CLOSEST(data->config->calibration_factor, val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
data->regs[reg]); regval);
break; break;
default: default:
/* programmer goofed */ /* programmer goofed */
...@@ -316,25 +292,25 @@ static ssize_t ina2xx_show_value(struct device *dev, ...@@ -316,25 +292,25 @@ static ssize_t ina2xx_show_value(struct device *dev,
struct device_attribute *da, char *buf) struct device_attribute *da, char *buf)
{ {
struct sensor_device_attribute *attr = to_sensor_dev_attr(da); struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
struct ina2xx_data *data = ina2xx_update_device(dev); struct ina2xx_data *data = dev_get_drvdata(dev);
unsigned int regval;
if (IS_ERR(data)) int err = ina2xx_read_reg(dev, attr->index, &regval);
return PTR_ERR(data);
if (err < 0)
return err;
return snprintf(buf, PAGE_SIZE, "%d\n", return snprintf(buf, PAGE_SIZE, "%d\n",
ina2xx_get_value(data, attr->index)); ina2xx_get_value(data, attr->index, regval));
} }
static ssize_t ina2xx_set_shunt(struct device *dev, static ssize_t ina2xx_set_shunt(struct device *dev,
struct device_attribute *da, struct device_attribute *da,
const char *buf, size_t count) const char *buf, size_t count)
{ {
struct ina2xx_data *data = ina2xx_update_device(dev);
unsigned long val; unsigned long val;
int status; int status;
struct ina2xx_data *data = dev_get_drvdata(dev);
if (IS_ERR(data))
return PTR_ERR(data);
status = kstrtoul(buf, 10, &val); status = kstrtoul(buf, 10, &val);
if (status < 0) if (status < 0)
...@@ -345,10 +321,10 @@ static ssize_t ina2xx_set_shunt(struct device *dev, ...@@ -345,10 +321,10 @@ static ssize_t ina2xx_set_shunt(struct device *dev,
val > data->config->calibration_factor) val > data->config->calibration_factor)
return -EINVAL; return -EINVAL;
mutex_lock(&data->update_lock); mutex_lock(&data->config_lock);
data->rshunt = val; data->rshunt = val;
status = ina2xx_calibrate(data); status = ina2xx_calibrate(data);
mutex_unlock(&data->update_lock); mutex_unlock(&data->config_lock);
if (status < 0) if (status < 0)
return status; return status;
...@@ -370,17 +346,9 @@ static ssize_t ina226_set_interval(struct device *dev, ...@@ -370,17 +346,9 @@ static ssize_t ina226_set_interval(struct device *dev,
if (val > INT_MAX || val == 0) if (val > INT_MAX || val == 0)
return -EINVAL; return -EINVAL;
mutex_lock(&data->update_lock); status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
data->curr_config = ina226_interval_to_reg(val, INA226_AVG_RD_MASK,
data->regs[INA2XX_CONFIG]); ina226_interval_to_reg(val));
status = i2c_smbus_write_word_swapped(data->client,
INA2XX_CONFIG,
data->curr_config);
ina226_set_update_interval(data);
/* Make sure the next access re-reads all registers. */
data->valid = 0;
mutex_unlock(&data->update_lock);
if (status < 0) if (status < 0)
return status; return status;
...@@ -390,18 +358,15 @@ static ssize_t ina226_set_interval(struct device *dev, ...@@ -390,18 +358,15 @@ static ssize_t ina226_set_interval(struct device *dev,
static ssize_t ina226_show_interval(struct device *dev, static ssize_t ina226_show_interval(struct device *dev,
struct device_attribute *da, char *buf) struct device_attribute *da, char *buf)
{ {
struct ina2xx_data *data = ina2xx_update_device(dev); struct ina2xx_data *data = dev_get_drvdata(dev);
int status;
unsigned int regval;
if (IS_ERR(data)) status = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
return PTR_ERR(data); if (status)
return status;
/* return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
* We don't use data->update_interval here as we want to display
* the actual interval used by the chip and jiffies_to_msecs()
* doesn't seem to be accurate enough.
*/
return snprintf(buf, PAGE_SIZE, "%d\n",
ina226_reg_to_interval(data->regs[INA2XX_CONFIG]));
} }
/* shunt voltage */ /* shunt voltage */
...@@ -455,7 +420,6 @@ static const struct attribute_group ina226_group = { ...@@ -455,7 +420,6 @@ static const struct attribute_group ina226_group = {
static int ina2xx_probe(struct i2c_client *client, static int ina2xx_probe(struct i2c_client *client,
const struct i2c_device_id *id) const struct i2c_device_id *id)
{ {
struct i2c_adapter *adapter = client->adapter;
struct ina2xx_platform_data *pdata; struct ina2xx_platform_data *pdata;
struct device *dev = &client->dev; struct device *dev = &client->dev;
struct ina2xx_data *data; struct ina2xx_data *data;
...@@ -463,9 +427,6 @@ static int ina2xx_probe(struct i2c_client *client, ...@@ -463,9 +427,6 @@ static int ina2xx_probe(struct i2c_client *client,
u32 val; u32 val;
int ret, group = 0; int ret, group = 0;
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
return -ENODEV;
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
if (!data) if (!data)
return -ENOMEM; return -ENOMEM;
...@@ -483,29 +444,26 @@ static int ina2xx_probe(struct i2c_client *client, ...@@ -483,29 +444,26 @@ static int ina2xx_probe(struct i2c_client *client,
/* set the device type */ /* set the device type */
data->kind = id->driver_data; data->kind = id->driver_data;
data->config = &ina2xx_config[data->kind]; data->config = &ina2xx_config[data->kind];
data->curr_config = data->config->config_default;
data->client = client;
/*
* Ina226 has a variable update_interval. For ina219 we
* use a constant value.
*/
if (data->kind == ina226)
ina226_set_update_interval(data);
else
data->update_interval = HZ / INA2XX_CONVERSION_RATE;
if (data->rshunt <= 0 || if (data->rshunt <= 0 ||
data->rshunt > data->config->calibration_factor) data->rshunt > data->config->calibration_factor)
return -ENODEV; return -ENODEV;
ina2xx_regmap_config.max_register = data->config->registers;
data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
if (IS_ERR(data->regmap)) {
dev_err(dev, "failed to allocate register map\n");
return PTR_ERR(data->regmap);
}
ret = ina2xx_init(data); ret = ina2xx_init(data);
if (ret < 0) { if (ret < 0) {
dev_err(dev, "error configuring the device: %d\n", ret); dev_err(dev, "error configuring the device: %d\n", ret);
return -ENODEV; return -ENODEV;
} }
mutex_init(&data->update_lock); mutex_init(&data->config_lock);
data->groups[group++] = &ina2xx_group; data->groups[group++] = &ina2xx_group;
if (data->kind == ina226) if (data->kind == ina226)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment