Lines Matching +full:dpot +full:- +full:dac

1 // SPDX-License-Identifier: GPL-2.0
3 * IIO DAC emulation driver using a digital potentiometer
11 * It is assumed that the dpot is used as a voltage divider between the
12 * current dpot wiper setting and the maximum resistance of the dpot. The
15 * .------.
16 * .-----------. | |
17 * | vref |--' .---.
18 * | regulator |--. | |
19 * '-----------' | | d |
22 * | | t |<---------+
24 * | '---' dac output voltage
26 * '------+------------+
39 struct iio_channel *dpot; member
56 struct dpot_dac *dac = iio_priv(indio_dev); in dpot_dac_read_raw() local
62 return iio_read_channel_raw(dac->dpot, val); in dpot_dac_read_raw()
65 ret = iio_read_channel_scale(dac->dpot, val, val2); in dpot_dac_read_raw()
69 do_div(tmp, dac->max_ohms); in dpot_dac_read_raw()
70 tmp *= regulator_get_voltage(dac->vref) / 1000; in dpot_dac_read_raw()
84 *val *= regulator_get_voltage(dac->vref) / 1000; in dpot_dac_read_raw()
85 *val2 *= dac->max_ohms; in dpot_dac_read_raw()
92 return -EINVAL; in dpot_dac_read_raw()
100 struct dpot_dac *dac = iio_priv(indio_dev); in dpot_dac_read_avail() local
105 return iio_read_avail_channel_raw(dac->dpot, vals, length); in dpot_dac_read_avail()
108 return -EINVAL; in dpot_dac_read_avail()
115 struct dpot_dac *dac = iio_priv(indio_dev); in dpot_dac_write_raw() local
119 return iio_write_channel_raw(dac->dpot, val); in dpot_dac_write_raw()
122 return -EINVAL; in dpot_dac_write_raw()
133 struct device *dev = &indio_dev->dev; in dpot_dac_channel_max_ohms()
134 struct dpot_dac *dac = iio_priv(indio_dev); in dpot_dac_channel_max_ohms() local
141 ret = iio_read_max_channel_raw(dac->dpot, &max); in dpot_dac_channel_max_ohms()
143 dev_err(dev, "dpot does not indicate its raw maximum value\n"); in dpot_dac_channel_max_ohms()
147 switch (iio_read_channel_scale(dac->dpot, &val, &val2)) { in dpot_dac_channel_max_ohms()
159 dev_err(dev, "dpot has a scale that is too weird\n"); in dpot_dac_channel_max_ohms()
162 return -EINVAL; in dpot_dac_channel_max_ohms()
167 struct device *dev = &pdev->dev; in dpot_dac_probe()
169 struct dpot_dac *dac; in dpot_dac_probe() local
173 indio_dev = devm_iio_device_alloc(dev, sizeof(*dac)); in dpot_dac_probe()
175 return -ENOMEM; in dpot_dac_probe()
178 dac = iio_priv(indio_dev); in dpot_dac_probe()
180 indio_dev->name = dev_name(dev); in dpot_dac_probe()
181 indio_dev->info = &dpot_dac_info; in dpot_dac_probe()
182 indio_dev->modes = INDIO_DIRECT_MODE; in dpot_dac_probe()
183 indio_dev->channels = &dpot_dac_iio_channel; in dpot_dac_probe()
184 indio_dev->num_channels = 1; in dpot_dac_probe()
186 dac->vref = devm_regulator_get(dev, "vref"); in dpot_dac_probe()
187 if (IS_ERR(dac->vref)) in dpot_dac_probe()
188 return dev_err_probe(&pdev->dev, PTR_ERR(dac->vref), in dpot_dac_probe()
191 dac->dpot = devm_iio_channel_get(dev, "dpot"); in dpot_dac_probe()
192 if (IS_ERR(dac->dpot)) in dpot_dac_probe()
193 return dev_err_probe(&pdev->dev, PTR_ERR(dac->dpot), in dpot_dac_probe()
194 "failed to get dpot input channel\n"); in dpot_dac_probe()
196 ret = iio_get_channel_type(dac->dpot, &type); in dpot_dac_probe()
201 dev_err(dev, "dpot is of the wrong type\n"); in dpot_dac_probe()
202 return -EINVAL; in dpot_dac_probe()
208 dac->max_ohms = ret; in dpot_dac_probe()
210 ret = regulator_enable(dac->vref); in dpot_dac_probe()
225 regulator_disable(dac->vref); in dpot_dac_probe()
232 struct dpot_dac *dac = iio_priv(indio_dev); in dpot_dac_remove() local
235 regulator_disable(dac->vref); in dpot_dac_remove()
239 { .compatible = "dpot-dac" },
248 .name = "iio-dpot-dac",
254 MODULE_DESCRIPTION("DAC emulation driver using a digital potentiometer");