Linux 4.16.11
[linux/fpc-iii.git] / drivers / iio / dac / dpot-dac.c
blobaaa2103d7c2b7229709609af7f71c1ad7fa8cc8e
1 /*
2 * IIO DAC emulation driver using a digital potentiometer
4 * Copyright (C) 2016 Axentia Technologies AB
6 * Author: Peter Rosin <peda@axentia.se>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 * It is assumed that the dpot is used as a voltage divider between the
15 * current dpot wiper setting and the maximum resistance of the dpot. The
16 * divided voltage is provided by a vref regulator.
18 * .------.
19 * .-----------. | |
20 * | vref |--' .---.
21 * | regulator |--. | |
22 * '-----------' | | d |
23 * | | p |
24 * | | o | wiper
25 * | | t |<---------+
26 * | | |
27 * | '---' dac output voltage
28 * | |
29 * '------+------------+
32 #include <linux/err.h>
33 #include <linux/iio/consumer.h>
34 #include <linux/iio/iio.h>
35 #include <linux/module.h>
36 #include <linux/of.h>
37 #include <linux/platform_device.h>
38 #include <linux/regulator/consumer.h>
40 struct dpot_dac {
41 struct regulator *vref;
42 struct iio_channel *dpot;
43 u32 max_ohms;
46 static const struct iio_chan_spec dpot_dac_iio_channel = {
47 .type = IIO_VOLTAGE,
48 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW)
49 | BIT(IIO_CHAN_INFO_SCALE),
50 .info_mask_separate_available = BIT(IIO_CHAN_INFO_RAW),
51 .output = 1,
52 .indexed = 1,
55 static int dpot_dac_read_raw(struct iio_dev *indio_dev,
56 struct iio_chan_spec const *chan,
57 int *val, int *val2, long mask)
59 struct dpot_dac *dac = iio_priv(indio_dev);
60 int ret;
61 unsigned long long tmp;
63 switch (mask) {
64 case IIO_CHAN_INFO_RAW:
65 return iio_read_channel_raw(dac->dpot, val);
67 case IIO_CHAN_INFO_SCALE:
68 ret = iio_read_channel_scale(dac->dpot, val, val2);
69 switch (ret) {
70 case IIO_VAL_FRACTIONAL_LOG2:
71 tmp = *val * 1000000000LL;
72 do_div(tmp, dac->max_ohms);
73 tmp *= regulator_get_voltage(dac->vref) / 1000;
74 do_div(tmp, 1000000000LL);
75 *val = tmp;
76 return ret;
77 case IIO_VAL_INT:
79 * Convert integer scale to fractional scale by
80 * setting the denominator (val2) to one...
82 *val2 = 1;
83 ret = IIO_VAL_FRACTIONAL;
84 /* ...and fall through. */
85 case IIO_VAL_FRACTIONAL:
86 *val *= regulator_get_voltage(dac->vref) / 1000;
87 *val2 *= dac->max_ohms;
88 break;
91 return ret;
94 return -EINVAL;
97 static int dpot_dac_read_avail(struct iio_dev *indio_dev,
98 struct iio_chan_spec const *chan,
99 const int **vals, int *type, int *length,
100 long mask)
102 struct dpot_dac *dac = iio_priv(indio_dev);
104 switch (mask) {
105 case IIO_CHAN_INFO_RAW:
106 *type = IIO_VAL_INT;
107 return iio_read_avail_channel_raw(dac->dpot, vals, length);
110 return -EINVAL;
113 static int dpot_dac_write_raw(struct iio_dev *indio_dev,
114 struct iio_chan_spec const *chan,
115 int val, int val2, long mask)
117 struct dpot_dac *dac = iio_priv(indio_dev);
119 switch (mask) {
120 case IIO_CHAN_INFO_RAW:
121 return iio_write_channel_raw(dac->dpot, val);
124 return -EINVAL;
127 static const struct iio_info dpot_dac_info = {
128 .read_raw = dpot_dac_read_raw,
129 .read_avail = dpot_dac_read_avail,
130 .write_raw = dpot_dac_write_raw,
133 static int dpot_dac_channel_max_ohms(struct iio_dev *indio_dev)
135 struct device *dev = &indio_dev->dev;
136 struct dpot_dac *dac = iio_priv(indio_dev);
137 unsigned long long tmp;
138 int ret;
139 int val;
140 int val2;
141 int max;
143 ret = iio_read_max_channel_raw(dac->dpot, &max);
144 if (ret < 0) {
145 dev_err(dev, "dpot does not indicate its raw maximum value\n");
146 return ret;
149 switch (iio_read_channel_scale(dac->dpot, &val, &val2)) {
150 case IIO_VAL_INT:
151 return max * val;
152 case IIO_VAL_FRACTIONAL:
153 tmp = (unsigned long long)max * val;
154 do_div(tmp, val2);
155 return tmp;
156 case IIO_VAL_FRACTIONAL_LOG2:
157 tmp = val * 1000000000LL * max >> val2;
158 do_div(tmp, 1000000000LL);
159 return tmp;
160 default:
161 dev_err(dev, "dpot has a scale that is too weird\n");
164 return -EINVAL;
167 static int dpot_dac_probe(struct platform_device *pdev)
169 struct device *dev = &pdev->dev;
170 struct iio_dev *indio_dev;
171 struct dpot_dac *dac;
172 enum iio_chan_type type;
173 int ret;
175 indio_dev = devm_iio_device_alloc(dev, sizeof(*dac));
176 if (!indio_dev)
177 return -ENOMEM;
179 platform_set_drvdata(pdev, indio_dev);
180 dac = iio_priv(indio_dev);
182 indio_dev->name = dev_name(dev);
183 indio_dev->dev.parent = dev;
184 indio_dev->info = &dpot_dac_info;
185 indio_dev->modes = INDIO_DIRECT_MODE;
186 indio_dev->channels = &dpot_dac_iio_channel;
187 indio_dev->num_channels = 1;
189 dac->vref = devm_regulator_get(dev, "vref");
190 if (IS_ERR(dac->vref)) {
191 if (PTR_ERR(dac->vref) != -EPROBE_DEFER)
192 dev_err(&pdev->dev, "failed to get vref regulator\n");
193 return PTR_ERR(dac->vref);
196 dac->dpot = devm_iio_channel_get(dev, "dpot");
197 if (IS_ERR(dac->dpot)) {
198 if (PTR_ERR(dac->dpot) != -EPROBE_DEFER)
199 dev_err(dev, "failed to get dpot input channel\n");
200 return PTR_ERR(dac->dpot);
203 ret = iio_get_channel_type(dac->dpot, &type);
204 if (ret < 0)
205 return ret;
207 if (type != IIO_RESISTANCE) {
208 dev_err(dev, "dpot is of the wrong type\n");
209 return -EINVAL;
212 ret = dpot_dac_channel_max_ohms(indio_dev);
213 if (ret < 0)
214 return ret;
215 dac->max_ohms = ret;
217 ret = regulator_enable(dac->vref);
218 if (ret) {
219 dev_err(dev, "failed to enable the vref regulator\n");
220 return ret;
223 ret = iio_device_register(indio_dev);
224 if (ret) {
225 dev_err(dev, "failed to register iio device\n");
226 goto disable_reg;
229 return 0;
231 disable_reg:
232 regulator_disable(dac->vref);
233 return ret;
236 static int dpot_dac_remove(struct platform_device *pdev)
238 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
239 struct dpot_dac *dac = iio_priv(indio_dev);
241 iio_device_unregister(indio_dev);
242 regulator_disable(dac->vref);
244 return 0;
247 static const struct of_device_id dpot_dac_match[] = {
248 { .compatible = "dpot-dac" },
249 { /* sentinel */ }
251 MODULE_DEVICE_TABLE(of, dpot_dac_match);
253 static struct platform_driver dpot_dac_driver = {
254 .probe = dpot_dac_probe,
255 .remove = dpot_dac_remove,
256 .driver = {
257 .name = "iio-dpot-dac",
258 .of_match_table = dpot_dac_match,
261 module_platform_driver(dpot_dac_driver);
263 MODULE_DESCRIPTION("DAC emulation driver using a digital potentiometer");
264 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
265 MODULE_LICENSE("GPL v2");