// SPDX-License-Identifier: GPL-2.0 /* * IIO DAC emulation driver using a digital potentiometer * * Copyright (C) 2016 Axentia Technologies AB * * Author: Peter Rosin <peda@axentia.se>
*/
/* * It is assumed that the dpot is used as a voltage divider between the * current dpot wiper setting and the maximum resistance of the dpot. The * divided voltage is provided by a vref regulator. * * .------. * .-----------. | | * | vref |--' .---. * | regulator |--. | | * '-----------' | | d | * | | p | * | | o | wiper * | | t |<---------+ * | | | * | '---' dac output voltage * | | * '------+------------+
*/
staticint dpot_dac_channel_max_ohms(struct iio_dev *indio_dev)
{ struct device *dev = &indio_dev->dev; struct dpot_dac *dac = iio_priv(indio_dev); unsignedlonglong tmp; int ret; int val; int val2; int max;
ret = iio_read_max_channel_raw(dac->dpot, &max); if (ret < 0) {
dev_err(dev, "dpot does not indicate its raw maximum value\n"); return ret;
}
switch (iio_read_channel_scale(dac->dpot, &val, &val2)) { case IIO_VAL_INT: return max * val; case IIO_VAL_FRACTIONAL:
tmp = (unsignedlonglong)max * val;
do_div(tmp, val2); return tmp; case IIO_VAL_FRACTIONAL_LOG2:
tmp = val * 1000000000LL * max >> val2;
do_div(tmp, 1000000000LL); return tmp; default:
dev_err(dev, "dpot has a scale that is too weird\n");
}
dac->vref = devm_regulator_get(dev, "vref"); if (IS_ERR(dac->vref)) return dev_err_probe(&pdev->dev, PTR_ERR(dac->vref), "failed to get vref regulator\n");
dac->dpot = devm_iio_channel_get(dev, "dpot"); if (IS_ERR(dac->dpot)) return dev_err_probe(&pdev->dev, PTR_ERR(dac->dpot), "failed to get dpot input channel\n");
ret = iio_get_channel_type(dac->dpot, &type); if (ret < 0) return ret;
if (type != IIO_RESISTANCE) {
dev_err(dev, "dpot is of the wrong type\n"); return -EINVAL;
}
ret = dpot_dac_channel_max_ohms(indio_dev); if (ret < 0) return ret;
dac->max_ohms = ret;
ret = regulator_enable(dac->vref); if (ret) {
dev_err(dev, "failed to enable the vref regulator\n"); return ret;
}
ret = iio_device_register(indio_dev); if (ret) {
dev_err(dev, "failed to register iio device\n"); goto disable_reg;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.