Skip to content

Commit

Permalink
Merge tag 'hwmon-for-linus-v3.20' of git://git.kernel.org/pub/scm/lin…
Browse files Browse the repository at this point in the history
…ux/kernel/git/groeck/linux-staging

Pull hwmon updates from Guenter Roeck:
 "Explicit support for ina231 added to ina2xx driver.

  Minor improvements, cleanup and fixes in various drivers"

* tag 'hwmon-for-linus-v3.20' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging:
  hwmon: (tmp102) add hibernation callbacks
  hwmon: (ads2828) Only keep data in device data structure if needed
  hwmon: (ads2828) Convert to use regmap
  hwmon: (jc42) Allow negative hysteresis temperatures
  hwmon: (adc128d818) Do proper sign extension
  hwmon: (ad7314) Do proper sign extension
  hwmon: (abx500) Fix format string warnings
  hwmon: (jc42) Fix integer overflow when writing hysteresis value
  hwmon: (jc42) Fix integer overflow
  hwmon: (jc42) Use sign_extend32 for sign extension
  hwmon: (ina2xx) Add ina231 compatible string
  hwmon: (ina2xx) use DIV_ROUND_CLOSEST() to avoid rounding errors
  hwmon: (ina2xx) remove an unnecessary dev_get_drvdata() result check
  hwmon: (ina2xx) implement update_interval attribute for ina226
  hwmon: (ina2xx) make shunt resistance configurable at run-time
  hwmon: (ina2xx) don't accept shunt values greater than the calibration factor
  hwmon: (ina2xx) remove a stray new line
  hwmon: (ina2xx) reinitialize the chip in case it's been reset
  hwmon: (nct7802) Constify struct regmap_config
  • Loading branch information
Linus Torvalds committed Feb 9, 2015
2 parents 19acc77 + dd378b1 commit 5c30c3c
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 135 deletions.
23 changes: 19 additions & 4 deletions Documentation/hwmon/ina2xx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Supported chips:
Datasheet: Publicly available at the Texas Instruments website
http://www.ti.com/

* Texas Instruments INA231
Prefix: 'ina231'
Addresses: I2C 0x40 - 0x4f
Datasheet: Publicly available at the Texas Instruments website
http://www.ti.com/

Author: Lothar Felten <l-felten@ti.com>

Description
Expand All @@ -41,9 +47,18 @@ interface. The INA220 monitors both shunt drop and supply voltage.
The INA226 is a current shunt and power monitor with an I2C interface.
The INA226 monitors both a shunt voltage drop and bus supply voltage.

The INA230 is a high or low side current shunt and power monitor with an I2C
interface. The INA230 monitors both a shunt voltage drop and bus supply voltage.
INA230 and INA231 are high or low side current shunt and power monitors
with an I2C interface. The chips monitor both a shunt voltage drop and
bus supply voltage.

The shunt value in micro-ohms can be set via platform data or device tree.
Please refer to the Documentation/devicetree/bindings/i2c/ina2xx.txt for bindings
The shunt value in micro-ohms can be set via platform data or device tree at
compile-time or via the shunt_resistor attribute in sysfs at run-time. Please
refer to the Documentation/devicetree/bindings/i2c/ina2xx.txt for bindings
if the device tree is used.

Additionally ina226 supports update_interval attribute as described in
Documentation/hwmon/sysfs-interface. Internally the interval is the sum of
bus and shunt voltage conversion times multiplied by the averaging rate. We
don't touch the conversion times and only modify the number of averages. The
lower limit of the update_interval is 2 ms, the upper limit is 2253 ms.
The actual programmed interval may vary from the desired value.
5 changes: 3 additions & 2 deletions drivers/hwmon/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,7 @@ config SENSORS_ADS1015
config SENSORS_ADS7828
tristate "Texas Instruments ADS7828 and compatibles"
depends on I2C
select REGMAP_I2C
help
If you say yes here you get support for Texas Instruments ADS7828 and
ADS7830 8-channel A/D converters. ADS7828 resolution is 12-bit, while
Expand Down Expand Up @@ -1430,8 +1431,8 @@ config SENSORS_INA2XX
tristate "Texas Instruments INA219 and compatibles"
depends on I2C
help
If you say yes here you get support for INA219, INA220, INA226, and
INA230 power monitor chips.
If you say yes here you get support for INA219, INA220, INA226,
INA230, and INA231 power monitor chips.

The INA2xx driver is configured for the default configuration of
the part as described in the datasheet.
Expand Down
6 changes: 3 additions & 3 deletions drivers/hwmon/abx500.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ static ssize_t show_min(struct device *dev,
struct abx500_temp *data = dev_get_drvdata(dev);
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);

return sprintf(buf, "%ld\n", data->min[attr->index]);
return sprintf(buf, "%lu\n", data->min[attr->index]);
}

static ssize_t show_max(struct device *dev,
Expand All @@ -230,7 +230,7 @@ static ssize_t show_max(struct device *dev,
struct abx500_temp *data = dev_get_drvdata(dev);
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);

return sprintf(buf, "%ld\n", data->max[attr->index]);
return sprintf(buf, "%lu\n", data->max[attr->index]);
}

static ssize_t show_max_hyst(struct device *dev,
Expand All @@ -239,7 +239,7 @@ static ssize_t show_max_hyst(struct device *dev,
struct abx500_temp *data = dev_get_drvdata(dev);
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);

return sprintf(buf, "%ld\n", data->max_hyst[attr->index]);
return sprintf(buf, "%lu\n", data->max_hyst[attr->index]);
}

static ssize_t show_min_alarm(struct device *dev,
Expand Down
5 changes: 3 additions & 2 deletions drivers/hwmon/ad7314.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <linux/err.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/bitops.h>

/*
* AD7314 temperature masks
Expand Down Expand Up @@ -67,7 +68,7 @@ static ssize_t ad7314_show_temperature(struct device *dev,
switch (spi_get_device_id(chip->spi_dev)->driver_data) {
case ad7314:
data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT;
data = (data << 6) >> 6;
data = sign_extend32(data, 9);

return sprintf(buf, "%d\n", 250 * data);
case adt7301:
Expand All @@ -78,7 +79,7 @@ static ssize_t ad7314_show_temperature(struct device *dev,
* register. 1lsb - 31.25 milli degrees centigrade
*/
data = ret & ADT7301_TEMP_MASK;
data = (data << 2) >> 2;
data = sign_extend32(data, 13);

return sprintf(buf, "%d\n",
DIV_ROUND_CLOSEST(data * 3125, 100));
Expand Down
3 changes: 2 additions & 1 deletion drivers/hwmon/adc128d818.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <linux/err.h>
#include <linux/regulator/consumer.h>
#include <linux/mutex.h>
#include <linux/bitops.h>

/* Addresses to scan
* The chip also supports addresses 0x35..0x37. Don't scan those addresses
Expand Down Expand Up @@ -189,7 +190,7 @@ static ssize_t adc128_show_temp(struct device *dev,
if (IS_ERR(data))
return PTR_ERR(data);

temp = (data->temp[index] << 7) >> 7; /* sign extend */
temp = sign_extend32(data->temp[index], 8);
return sprintf(buf, "%d\n", temp * 500);/* 0.5 degrees C resolution */
}

Expand Down
102 changes: 40 additions & 62 deletions drivers/hwmon/ads7828.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@
#include <linux/hwmon-sysfs.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_data/ads7828.h>
#include <linux/regmap.h>
#include <linux/slab.h>

/* The ADS7828 registers */
#define ADS7828_NCH 8 /* 8 channels supported */
#define ADS7828_CMD_SD_SE 0x80 /* Single ended inputs */
#define ADS7828_CMD_PD1 0x04 /* Internal vref OFF && A/D ON */
#define ADS7828_CMD_PD3 0x0C /* Internal vref ON && A/D ON */
Expand All @@ -50,17 +48,9 @@ enum ads7828_chips { ads7828, ads7830 };

/* Client specific data */
struct ads7828_data {
struct i2c_client *client;
struct mutex update_lock; /* Mutex protecting updates */
unsigned long last_updated; /* Last updated time (in jiffies) */
u16 adc_input[ADS7828_NCH]; /* ADS7828_NCH samples */
bool valid; /* Validity flag */
bool diff_input; /* Differential input */
bool ext_vref; /* External voltage reference */
unsigned int vref_mv; /* voltage reference value */
struct regmap *regmap;
u8 cmd_byte; /* Command byte without channel bits */
unsigned int lsb_resol; /* Resolution of the ADC sample LSB */
s32 (*read_channel)(const struct i2c_client *client, u8 command);
};

/* Command byte C2,C1,C0 - see datasheet */
Expand All @@ -69,42 +59,22 @@ static inline u8 ads7828_cmd_byte(u8 cmd, int ch)
return cmd | (((ch >> 1) | (ch & 0x01) << 2) << 4);
}

/* Update data for the device (all 8 channels) */
static struct ads7828_data *ads7828_update_device(struct device *dev)
{
struct ads7828_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;

mutex_lock(&data->update_lock);

if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
|| !data->valid) {
unsigned int ch;
dev_dbg(&client->dev, "Starting ads7828 update\n");

for (ch = 0; ch < ADS7828_NCH; ch++) {
u8 cmd = ads7828_cmd_byte(data->cmd_byte, ch);
data->adc_input[ch] = data->read_channel(client, cmd);
}
data->last_updated = jiffies;
data->valid = true;
}

mutex_unlock(&data->update_lock);

return data;
}

/* sysfs callback function */
static ssize_t ads7828_show_in(struct device *dev, struct device_attribute *da,
char *buf)
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
struct ads7828_data *data = ads7828_update_device(dev);
unsigned int value = DIV_ROUND_CLOSEST(data->adc_input[attr->index] *
data->lsb_resol, 1000);
struct ads7828_data *data = dev_get_drvdata(dev);
u8 cmd = ads7828_cmd_byte(data->cmd_byte, attr->index);
unsigned int regval;
int err;

return sprintf(buf, "%d\n", value);
err = regmap_read(data->regmap, cmd, &regval);
if (err < 0)
return err;

return sprintf(buf, "%d\n",
DIV_ROUND_CLOSEST(regval * data->lsb_resol, 1000));
}

static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ads7828_show_in, NULL, 0);
Expand All @@ -130,49 +100,57 @@ static struct attribute *ads7828_attrs[] = {

ATTRIBUTE_GROUPS(ads7828);

static const struct regmap_config ads2828_regmap_config = {
.reg_bits = 8,
.val_bits = 16,
};

static const struct regmap_config ads2830_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
};

static int ads7828_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct device *dev = &client->dev;
struct ads7828_platform_data *pdata = dev_get_platdata(dev);
struct ads7828_data *data;
struct device *hwmon_dev;
unsigned int vref_mv = ADS7828_INT_VREF_MV;
bool diff_input = false;
bool ext_vref = false;

data = devm_kzalloc(dev, sizeof(struct ads7828_data), GFP_KERNEL);
if (!data)
return -ENOMEM;

if (pdata) {
data->diff_input = pdata->diff_input;
data->ext_vref = pdata->ext_vref;
if (data->ext_vref)
data->vref_mv = pdata->vref_mv;
diff_input = pdata->diff_input;
ext_vref = pdata->ext_vref;
if (ext_vref && pdata->vref_mv)
vref_mv = pdata->vref_mv;
}

/* Bound Vref with min/max values if it was provided */
if (data->vref_mv)
data->vref_mv = clamp_val(data->vref_mv,
ADS7828_EXT_VREF_MV_MIN,
ADS7828_EXT_VREF_MV_MAX);
else
data->vref_mv = ADS7828_INT_VREF_MV;
/* Bound Vref with min/max values */
vref_mv = clamp_val(vref_mv, ADS7828_EXT_VREF_MV_MIN,
ADS7828_EXT_VREF_MV_MAX);

/* ADS7828 uses 12-bit samples, while ADS7830 is 8-bit */
if (id->driver_data == ads7828) {
data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 4096);
data->read_channel = i2c_smbus_read_word_swapped;
data->lsb_resol = DIV_ROUND_CLOSEST(vref_mv * 1000, 4096);
data->regmap = devm_regmap_init_i2c(client,
&ads2828_regmap_config);
} else {
data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 256);
data->read_channel = i2c_smbus_read_byte_data;
data->lsb_resol = DIV_ROUND_CLOSEST(vref_mv * 1000, 256);
data->regmap = devm_regmap_init_i2c(client,
&ads2830_regmap_config);
}

data->cmd_byte = data->ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3;
if (!data->diff_input)
data->cmd_byte = ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3;
if (!diff_input)
data->cmd_byte |= ADS7828_CMD_SD_SE;

data->client = client;
mutex_init(&data->update_lock);

hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
data,
ads7828_groups);
Expand Down
Loading

0 comments on commit 5c30c3c

Please sign in to comment.