Exemplo n.º 1
0
static s32 show_bus_voltage(struct device *dev,
			struct device_attribute *attr,
			char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct ina230_data *data = i2c_get_clientdata(client);
	s32 voltage_mV;
	int retval;

	mutex_lock(&data->mutex);
	retval = ensure_enabled_start(client);
	if (retval < 0) {
		mutex_unlock(&data->mutex);
		return retval;
	}

	/* getting voltage readings in milli volts*/
	voltage_mV =
		(s16)be16_to_cpu(i2c_smbus_read_word_data(client,
							  INA230_VOLTAGE));

	ensure_enabled_end(client);
	mutex_unlock(&data->mutex);

	if (voltage_mV < 0) {
		dev_err(dev, "%s: failed\n", __func__);
		return -1;
	}

	voltage_mV = busv_register_to_mv(voltage_mV);

	return sprintf(buf, "%d mV\n", voltage_mV);
}
Exemplo n.º 2
0
static s32 show_shunt_voltage(struct device *dev,
			struct device_attribute *attr,
			char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct ina230_data *data = i2c_get_clientdata(client);
	s32 voltage_uV;
	int retval;

	mutex_lock(&data->mutex);
	retval = ensure_enabled_start(client);
	if (retval < 0) {
		mutex_unlock(&data->mutex);
		return retval;
	}

	voltage_uV =
		(s16)be16_to_cpu(i2c_smbus_read_word_data(client,
							  INA230_SHUNT));

	ensure_enabled_end(client);
	mutex_unlock(&data->mutex);

	voltage_uV = shuntv_register_to_uv(voltage_uV);

	return sprintf(buf, "%d uV\n", voltage_uV);
}
s32 ina230_get_batt_volt(void)
{
	struct i2c_client *client = to_i2c_client(this_dev);
	struct ina230_data *data = i2c_get_clientdata(client);
	s32 voltage_mV;
	int retval;

	mutex_lock(&data->mutex);
	retval = ensure_enabled_start(client);
	if (retval < 0) {
		mutex_unlock(&data->mutex);
		return retval;
	}

	/* getting voltage readings in milli volts*/
	voltage_mV =
		(s16)be16_to_cpu(i2c_smbus_read_word_data(client,
							  INA230_VOLTAGE));

	ensure_enabled_end(client);
	mutex_unlock(&data->mutex);

	if (voltage_mV < 0) {
		dev_err(this_dev, "%s: failed\n", __func__);
		return -1;
	}

	voltage_mV = busv_register_to_mv(voltage_mV);
	return voltage_mV;
}
s32 ina230_get_batt_curr(void)
{
	struct i2c_client *client = to_i2c_client(this_dev);
	struct ina230_data *data = i2c_get_clientdata(client);
	s32 current_mA;
	int retval;

	mutex_lock(&data->mutex);
	retval = ensure_enabled_start(client);
	if (retval < 0) {
		mutex_unlock(&data->mutex);
		return 0;
	}

	/* getting current readings in milli amps */
	current_mA =
		(s16)be16_to_cpu(i2c_smbus_read_word_data(client,
							  INA230_CURRENT));

	ensure_enabled_end(client);
	mutex_unlock(&data->mutex);

	current_mA = currenta_register_to_ma(current_mA);
	return current_mA;
}
Exemplo n.º 5
0
s32 ina230_get_current(void)
{
	struct i2c_client *client;
	struct ina230_data *data;
	s32 current_mA;
	int retval;

	if (!p_ina230_data)
		return INA230_ERROR;

	client = p_ina230_data->client;
	data = p_ina230_data;

	mutex_lock(&data->mutex);
	/* fill calib data */
	retval = i2c_smbus_write_word_data(client, INA230_CAL,
		__constant_cpu_to_be16(data->pdata->calibration_data));
	if (retval < 0) {
		dev_err(&client->dev, "calibration data write failed sts: 0x%x\n",
			retval);
		mutex_unlock(&data->mutex);
		return INA230_ERROR;
	}

	retval = ensure_enabled_start(client);
	if (retval < 0) {
		mutex_unlock(&data->mutex);
		return INA230_ERROR;
	}

	retval = __locked_wait_for_conversion(&client->dev);
	if (retval) {
		mutex_unlock(&data->mutex);
		return INA230_ERROR;
	}

	/* getting current readings in milli amps*/
	retval = i2c_smbus_read_word_data(client, INA230_CURRENT);
	if (retval < 0) {
		mutex_unlock(&data->mutex);
		return INA230_ERROR;
	}
	current_mA = (s16) be16_to_cpu(retval);

	ensure_enabled_end(client);
	mutex_unlock(&data->mutex);

	if (data->pdata->shunt_polarity_inverted)
		current_mA *= -1;

	current_mA *= (s16) data->pdata->power_lsb;
	if (data->pdata->divisor)
		current_mA /= (s16) data->pdata->divisor;
	if (data->pdata->precision_multiplier)
		current_mA /= (s16) data->pdata->precision_multiplier;

	return current_mA;
}
Exemplo n.º 6
0
static s32 show_current(struct device *dev,
			struct device_attribute *attr,
			char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct ina230_data *data = i2c_get_clientdata(client);
	s32 current_mA;
	int retval;

	mutex_lock(&data->mutex);
	/* fill calib data */
	retval = i2c_smbus_write_word_data(client, INA230_CAL,
		__constant_cpu_to_be16(data->pdata->calibration_data));
	if (retval < 0) {
		dev_err(dev, "calibration data write failed sts: 0x%x\n",
			retval);
		mutex_unlock(&data->mutex);
		return retval;
	}

	retval = ensure_enabled_start(client);
	if (retval < 0) {
		mutex_unlock(&data->mutex);
		return retval;
	}

	retval = __locked_wait_for_conversion(dev);
	if (retval) {
		mutex_unlock(&data->mutex);
		return retval;
	}

	/* getting current readings in milli amps*/
	retval = i2c_smbus_read_word_data(client, INA230_CURRENT);
	if (retval < 0) {
		mutex_unlock(&data->mutex);
		return retval;
	}
	current_mA = (s16) be16_to_cpu(retval);

	ensure_enabled_end(client);
	mutex_unlock(&data->mutex);

	if (data->pdata->shunt_polarity_inverted)
		current_mA *= -1;

	current_mA *= (s16) data->pdata->power_lsb;
	if (data->pdata->divisor)
		current_mA /= (s16) data->pdata->divisor;
	if (data->pdata->precision_multiplier)
		current_mA /= (s16) data->pdata->precision_multiplier;

	return sprintf(buf, "%d mA\n", current_mA);
}
Exemplo n.º 7
0
static s32 show_power(struct device *dev,
			struct device_attribute *attr,
			char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct ina230_data *data = i2c_get_clientdata(client);
	s32 power_mW;
	int retval;

	mutex_lock(&data->mutex);

	/* fill calib data */
	retval = i2c_smbus_write_word_data(client, INA230_CAL,
		__constant_cpu_to_be16(data->pdata->calibration_data));
	if (retval < 0) {
		dev_err(dev, "calibration data write failed sts: 0x%x\n",
			retval);
		mutex_unlock(&data->mutex);
		return retval;
	}

	retval = ensure_enabled_start(client);
	if (retval < 0) {
		mutex_unlock(&data->mutex);
		return retval;
	}

	retval = __locked_wait_for_conversion(dev);
	if (retval) {
		mutex_unlock(&data->mutex);
		return retval;
	}

	/* getting power readings in milli watts*/
	power_mW = be16_to_cpu(i2c_smbus_read_word_data(client,
		INA230_POWER));
	if (power_mW < 0) {
		mutex_unlock(&data->mutex);
		return -EINVAL;
	}

	ensure_enabled_end(client);
	mutex_unlock(&data->mutex);

	power_mW =
		power_mW * data->pdata->power_lsb;
	if (data->pdata->precision_multiplier)
		power_mW /= data->pdata->precision_multiplier;

	return sprintf(buf, "%d mW\n", power_mW);
}
s32 ina230_get_bus_voltage_multi(s32 *voltage_mV, u32 num)
{
	struct i2c_client *client = to_i2c_client(this_dev);
	struct ina230_data *data = i2c_get_clientdata(client);
	int retval = 0;
	unsigned int i = 0;

	if (!voltage_mV || num == 0) {
		dev_err(this_dev, "%s: failed with bad parameter (%p, %u)\n", __func__, voltage_mV, num);
		retval = -1;
		goto fault;
	}

	mutex_lock(&data->mutex);
	i = 0;
	do {
		retval = ensure_enabled_start(client);
		if (retval < 0) {
			mutex_unlock(&data->mutex);
			goto fault;
		}

		/* getting voltage readings in milli volts*/
		voltage_mV[i] =
			(s16)be16_to_cpu(i2c_smbus_read_word_data(client,
						INA230_VOLTAGE));

		ensure_enabled_end(client);
		mdelay(1);
	} while (++i < num);
	mutex_unlock(&data->mutex);

	i = 0;
	do {
		if (voltage_mV[i] < 0) {
			dev_err(this_dev, "%s: failed at %u\n", __func__, i);
			retval = -1;
			goto fault;
		}
		voltage_mV[i] = busv_register_to_mv(voltage_mV[i]);
	} while (++i < num);

fault:
	return retval;
}
s32 ina230_get_batt_voltage_current(s32 *voltage_mV, s32 *current_mA)
{
	struct i2c_client *client = to_i2c_client(this_dev);
	struct ina230_data *data = i2c_get_clientdata(client);
	int retval = 0;

	if (!voltage_mV || !current_mA) {
		dev_err(this_dev, "%s: failed with bad parameter (%p, %p)\n", __func__, voltage_mV, current_mA);
		retval = -1;
		goto fault;
	}

	mutex_lock(&data->mutex);
	retval = ensure_enabled_start(client);
	if (retval < 0) {
		mutex_unlock(&data->mutex);
		goto fault;
	}

	/* getting voltage readings in milli volts*/
	*voltage_mV =
		(s16)be16_to_cpu(i2c_smbus_read_word_data(client,
							  INA230_VOLTAGE));

	/* getting current readings in milli amps */
	*current_mA =
		(s16)be16_to_cpu(i2c_smbus_read_word_data(client,
							  INA230_CURRENT));

	ensure_enabled_end(client);
	mutex_unlock(&data->mutex);

	if (*voltage_mV < 0) {
		dev_err(this_dev, "%s: failed\n", __func__);
		retval = -1;
		goto fault;
	}

	*voltage_mV = busv_register_to_mv(*voltage_mV);
	*current_mA = currenta_register_to_ma(*current_mA);
fault:
	return retval;
}