dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / iio / adc / ad7091r-base.c
blob33c40357bd5e32d0a22ab8275c260779fe3accf0
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * AD7091RX Analog to Digital converter driver
5 * Copyright 2014-2019 Analog Devices Inc.
6 */
8 #include <linux/bitops.h>
9 #include <linux/iio/events.h>
10 #include <linux/iio/iio.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/regulator/consumer.h>
16 #include "ad7091r-base.h"
18 #define AD7091R_REG_RESULT 0
19 #define AD7091R_REG_CHANNEL 1
20 #define AD7091R_REG_CONF 2
21 #define AD7091R_REG_ALERT 3
22 #define AD7091R_REG_CH_LOW_LIMIT(ch) ((ch) * 3 + 4)
23 #define AD7091R_REG_CH_HIGH_LIMIT(ch) ((ch) * 3 + 5)
24 #define AD7091R_REG_CH_HYSTERESIS(ch) ((ch) * 3 + 6)
26 /* AD7091R_REG_RESULT */
27 #define AD7091R_REG_RESULT_CH_ID(x) (((x) >> 13) & 0x3)
28 #define AD7091R_REG_RESULT_CONV_RESULT(x) ((x) & 0xfff)
30 /* AD7091R_REG_CONF */
31 #define AD7091R_REG_CONF_AUTO BIT(8)
32 #define AD7091R_REG_CONF_CMD BIT(10)
34 #define AD7091R_REG_CONF_MODE_MASK \
35 (AD7091R_REG_CONF_AUTO | AD7091R_REG_CONF_CMD)
37 enum ad7091r_mode {
38 AD7091R_MODE_SAMPLE,
39 AD7091R_MODE_COMMAND,
40 AD7091R_MODE_AUTOCYCLE,
43 struct ad7091r_state {
44 struct device *dev;
45 struct regmap *map;
46 struct regulator *vref;
47 const struct ad7091r_chip_info *chip_info;
48 enum ad7091r_mode mode;
49 struct mutex lock; /*lock to prevent concurent reads */
52 static int ad7091r_set_mode(struct ad7091r_state *st, enum ad7091r_mode mode)
54 int ret, conf;
56 switch (mode) {
57 case AD7091R_MODE_SAMPLE:
58 conf = 0;
59 break;
60 case AD7091R_MODE_COMMAND:
61 conf = AD7091R_REG_CONF_CMD;
62 break;
63 case AD7091R_MODE_AUTOCYCLE:
64 conf = AD7091R_REG_CONF_AUTO;
65 break;
66 default:
67 return -EINVAL;
70 ret = regmap_update_bits(st->map, AD7091R_REG_CONF,
71 AD7091R_REG_CONF_MODE_MASK, conf);
72 if (ret)
73 return ret;
75 st->mode = mode;
77 return 0;
80 static int ad7091r_set_channel(struct ad7091r_state *st, unsigned int channel)
82 unsigned int dummy;
83 int ret;
85 /* AD7091R_REG_CHANNEL specified which channels to be converted */
86 ret = regmap_write(st->map, AD7091R_REG_CHANNEL,
87 BIT(channel) | (BIT(channel) << 8));
88 if (ret)
89 return ret;
92 * There is a latency of one conversion before the channel conversion
93 * sequence is updated
95 return regmap_read(st->map, AD7091R_REG_RESULT, &dummy);
98 static int ad7091r_read_one(struct iio_dev *iio_dev,
99 unsigned int channel, unsigned int *read_val)
101 struct ad7091r_state *st = iio_priv(iio_dev);
102 unsigned int val;
103 int ret;
105 ret = ad7091r_set_channel(st, channel);
106 if (ret)
107 return ret;
109 ret = regmap_read(st->map, AD7091R_REG_RESULT, &val);
110 if (ret)
111 return ret;
113 if (AD7091R_REG_RESULT_CH_ID(val) != channel)
114 return -EIO;
116 *read_val = AD7091R_REG_RESULT_CONV_RESULT(val);
118 return 0;
121 static int ad7091r_read_raw(struct iio_dev *iio_dev,
122 struct iio_chan_spec const *chan,
123 int *val, int *val2, long m)
125 struct ad7091r_state *st = iio_priv(iio_dev);
126 unsigned int read_val;
127 int ret;
129 mutex_lock(&st->lock);
131 switch (m) {
132 case IIO_CHAN_INFO_RAW:
133 if (st->mode != AD7091R_MODE_COMMAND) {
134 ret = -EBUSY;
135 goto unlock;
138 ret = ad7091r_read_one(iio_dev, chan->channel, &read_val);
139 if (ret)
140 goto unlock;
142 *val = read_val;
143 ret = IIO_VAL_INT;
144 break;
146 case IIO_CHAN_INFO_SCALE:
147 if (st->vref) {
148 ret = regulator_get_voltage(st->vref);
149 if (ret < 0)
150 goto unlock;
152 *val = ret / 1000;
153 } else {
154 *val = st->chip_info->vref_mV;
157 *val2 = chan->scan_type.realbits;
158 ret = IIO_VAL_FRACTIONAL_LOG2;
159 break;
161 default:
162 ret = -EINVAL;
163 break;
166 unlock:
167 mutex_unlock(&st->lock);
168 return ret;
171 static const struct iio_info ad7091r_info = {
172 .read_raw = ad7091r_read_raw,
175 static irqreturn_t ad7091r_event_handler(int irq, void *private)
177 struct ad7091r_state *st = (struct ad7091r_state *) private;
178 struct iio_dev *iio_dev = dev_get_drvdata(st->dev);
179 unsigned int i, read_val;
180 int ret;
181 s64 timestamp = iio_get_time_ns(iio_dev);
183 ret = regmap_read(st->map, AD7091R_REG_ALERT, &read_val);
184 if (ret)
185 return IRQ_HANDLED;
187 for (i = 0; i < st->chip_info->num_channels; i++) {
188 if (read_val & BIT(i * 2))
189 iio_push_event(iio_dev,
190 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
191 IIO_EV_TYPE_THRESH,
192 IIO_EV_DIR_RISING), timestamp);
193 if (read_val & BIT(i * 2 + 1))
194 iio_push_event(iio_dev,
195 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
196 IIO_EV_TYPE_THRESH,
197 IIO_EV_DIR_FALLING), timestamp);
200 return IRQ_HANDLED;
203 static void ad7091r_remove(void *data)
205 struct ad7091r_state *st = data;
207 regulator_disable(st->vref);
210 int ad7091r_probe(struct device *dev, const char *name,
211 const struct ad7091r_chip_info *chip_info,
212 struct regmap *map, int irq)
214 struct iio_dev *iio_dev;
215 struct ad7091r_state *st;
216 int ret;
218 iio_dev = devm_iio_device_alloc(dev, sizeof(*st));
219 if (!iio_dev)
220 return -ENOMEM;
222 st = iio_priv(iio_dev);
223 st->dev = dev;
224 st->chip_info = chip_info;
225 st->map = map;
227 iio_dev->dev.parent = dev;
228 iio_dev->name = name;
229 iio_dev->info = &ad7091r_info;
230 iio_dev->modes = INDIO_DIRECT_MODE;
232 iio_dev->num_channels = chip_info->num_channels;
233 iio_dev->channels = chip_info->channels;
235 if (irq) {
236 ret = devm_request_threaded_irq(dev, irq, NULL,
237 ad7091r_event_handler,
238 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, name, st);
239 if (ret)
240 return ret;
243 st->vref = devm_regulator_get_optional(dev, "vref");
244 if (IS_ERR(st->vref)) {
245 if (PTR_ERR(st->vref) == -EPROBE_DEFER)
246 return -EPROBE_DEFER;
247 st->vref = NULL;
248 } else {
249 ret = regulator_enable(st->vref);
250 if (ret)
251 return ret;
252 ret = devm_add_action_or_reset(dev, ad7091r_remove, st);
253 if (ret)
254 return ret;
257 /* Use command mode by default to convert only desired channels*/
258 ret = ad7091r_set_mode(st, AD7091R_MODE_COMMAND);
259 if (ret)
260 return ret;
262 return devm_iio_device_register(dev, iio_dev);
264 EXPORT_SYMBOL_GPL(ad7091r_probe);
266 static bool ad7091r_writeable_reg(struct device *dev, unsigned int reg)
268 switch (reg) {
269 case AD7091R_REG_RESULT:
270 case AD7091R_REG_ALERT:
271 return false;
272 default:
273 return true;
277 static bool ad7091r_volatile_reg(struct device *dev, unsigned int reg)
279 switch (reg) {
280 case AD7091R_REG_RESULT:
281 case AD7091R_REG_ALERT:
282 return true;
283 default:
284 return false;
288 const struct regmap_config ad7091r_regmap_config = {
289 .reg_bits = 8,
290 .val_bits = 16,
291 .writeable_reg = ad7091r_writeable_reg,
292 .volatile_reg = ad7091r_volatile_reg,
294 EXPORT_SYMBOL_GPL(ad7091r_regmap_config);
296 MODULE_AUTHOR("Beniamin Bia <beniamin.bia@analog.com>");
297 MODULE_DESCRIPTION("Analog Devices AD7091Rx multi-channel converters");
298 MODULE_LICENSE("GPL v2");