diff options
author | Timo Alho <talho@nvidia.com> | 2014-05-15 11:18:53 +0300 |
---|---|---|
committer | Mandar Padmawar <mpadmawar@nvidia.com> | 2014-05-20 02:27:31 -0700 |
commit | a3335cba56e7c877b0363b255044c0dde1ec471d (patch) | |
tree | be87a4c4c8b653ebd63fc8cfb8adfa0c0e7ae0d2 /drivers/staging | |
parent | d14f836bc5df928082c3748650a3923fe3b25fe4 (diff) |
iio: meter: ina230: add reading for simultaneous U&I
Add ui_input node to allow simultaneously reading current and voltage
from one triggered conversion cycle. Reading from this node will
force the INA to operate on triggered mode for the duration of the
conversion.
Bug 1513829
Change-Id: I779e0d88340bc68bd161ddd00423be941559d5df
Signed-off-by: Timo Alho <talho@nvidia.com>
Reviewed-on: http://git-master/r/410119
(cherry picked from commit 52a570a11902ba9aac1fee4bbf8789c994f82dae)
Reviewed-on: http://git-master/r/410145
Reviewed-by: Juha Tukkinen <jtukkinen@nvidia.com>
Diffstat (limited to 'drivers/staging')
-rw-r--r-- | drivers/staging/iio/meter/ina230.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/drivers/staging/iio/meter/ina230.c b/drivers/staging/iio/meter/ina230.c index 20e26f155db6..413db56a8d26 100644 --- a/drivers/staging/iio/meter/ina230.c +++ b/drivers/staging/iio/meter/ina230.c @@ -74,6 +74,7 @@ enum { CHANNEL_NAME = 0, CURRENT_THRESHOLD, ALERT_FLAG, + VBUS_VOLTAGE_CURRENT, }; struct ina230_platform_data { @@ -480,6 +481,53 @@ static int ina230_get_shunt_power(struct ina230_chip *chip, int *power_mw) return 0; } +static int ina230_get_vbus_voltage_current(struct ina230_chip *chip, + int *current_ma, int *voltage_mv) +{ + int ret = 0, val; + int ma; + + mutex_lock(&chip->mutex); + /* ensure that triggered mode will be used */ + chip->running = 0; + ret = ina230_ensure_enabled_start(chip); + if (ret < 0) + goto out; + + ret = __locked_wait_for_conversion(chip); + if (ret) + goto out; + + val = i2c_smbus_read_word_data(chip->client, INA230_VOLTAGE); + if (val < 0) { + ret = val; + goto out; + } + *voltage_mv = busv_register_to_mv(be16_to_cpu(val)); + + if (chip->pdata->resistor) { + val = i2c_smbus_read_word_data(chip->client, INA230_SHUNT); + if (val < 0) { + ret = val; + goto out; + } + ma = shuntv_register_to_uv((s16)be16_to_cpu(val)); + ma = DIV_ROUND_CLOSEST(ma, chip->pdata->resistor); + if (chip->pdata->shunt_polarity_inverted) + ma *= -1; + *current_ma = ma; + } else { + *current_ma = 0; + } +out: + /* restart continuous current monitoring, if enabled */ + if (chip->pdata->current_threshold) + __locked_ina230_evaluate_state(chip); + mutex_unlock(&chip->mutex); + return ret; +} + + static int ina230_set_current_threshold(struct ina230_chip *chip, int current_ma) { @@ -603,6 +651,9 @@ static ssize_t ina230_show_channel(struct device *dev, struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ina230_chip *chip = iio_priv(indio_dev); struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); + int current_ma = 0; + int voltage_mv = 0; + int ret; switch (this_attr->address) { case CHANNEL_NAME: @@ -614,6 +665,13 @@ static ssize_t ina230_show_channel(struct device *dev, case ALERT_FLAG: return ina230_show_alert_flag(chip, buf); + case VBUS_VOLTAGE_CURRENT: + ret = ina230_get_vbus_voltage_current(chip, ¤t_ma, + &voltage_mv); + if (!ret) + return sprintf(buf, "%d %d\n", voltage_mv, current_ma); + return ret; + default: break; } @@ -653,11 +711,16 @@ static IIO_DEVICE_ATTR(current_threshold, S_IRUGO | S_IWUSR, static IIO_DEVICE_ATTR(alert_flag, S_IRUGO, ina230_show_channel, NULL, ALERT_FLAG); +static IIO_DEVICE_ATTR(ui_input, S_IRUGO, + ina230_show_channel, NULL, + VBUS_VOLTAGE_CURRENT); + static struct attribute *ina230_attributes[] = { &iio_dev_attr_rail_name.dev_attr.attr, &iio_dev_attr_current_threshold.dev_attr.attr, &iio_dev_attr_alert_flag.dev_attr.attr, + &iio_dev_attr_ui_input.dev_attr.attr, NULL, }; |