1 // SPDX-License-Identifier: GPL-2.0
3 * AD7091RX Analog to Digital converter driver
5 * Copyright 2014-2019 Analog Devices Inc.
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)
40 AD7091R_MODE_AUTOCYCLE,
43 struct ad7091r_state {
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)
57 case AD7091R_MODE_SAMPLE:
60 case AD7091R_MODE_COMMAND:
61 conf = AD7091R_REG_CONF_CMD;
63 case AD7091R_MODE_AUTOCYCLE:
64 conf = AD7091R_REG_CONF_AUTO;
70 ret = regmap_update_bits(st->map, AD7091R_REG_CONF,
71 AD7091R_REG_CONF_MODE_MASK, conf);
80 static int ad7091r_set_channel(struct ad7091r_state *st, unsigned int channel)
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));
92 * There is a latency of one conversion before the channel conversion
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);
105 ret = ad7091r_set_channel(st, channel);
109 ret = regmap_read(st->map, AD7091R_REG_RESULT, &val);
113 if (AD7091R_REG_RESULT_CH_ID(val) != channel)
116 *read_val = AD7091R_REG_RESULT_CONV_RESULT(val);
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;
129 mutex_lock(&st->lock);
132 case IIO_CHAN_INFO_RAW:
133 if (st->mode != AD7091R_MODE_COMMAND) {
138 ret = ad7091r_read_one(iio_dev, chan->channel, &read_val);
146 case IIO_CHAN_INFO_SCALE:
148 ret = regulator_get_voltage(st->vref);
154 *val = st->chip_info->vref_mV;
157 *val2 = chan->scan_type.realbits;
158 ret = IIO_VAL_FRACTIONAL_LOG2;
167 mutex_unlock(&st->lock);
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;
181 s64 timestamp = iio_get_time_ns(iio_dev);
183 ret = regmap_read(st->map, AD7091R_REG_ALERT, &read_val);
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,
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,
197 IIO_EV_DIR_FALLING), timestamp);
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;
218 iio_dev = devm_iio_device_alloc(dev, sizeof(*st));
222 st = iio_priv(iio_dev);
224 st->chip_info = chip_info;
227 iio_dev->name = name;
228 iio_dev->info = &ad7091r_info;
229 iio_dev->modes = INDIO_DIRECT_MODE;
231 iio_dev->num_channels = chip_info->num_channels;
232 iio_dev->channels = chip_info->channels;
235 ret = devm_request_threaded_irq(dev, irq, NULL,
236 ad7091r_event_handler,
237 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, name, st);
242 st->vref = devm_regulator_get_optional(dev, "vref");
243 if (IS_ERR(st->vref)) {
244 if (PTR_ERR(st->vref) == -EPROBE_DEFER)
245 return -EPROBE_DEFER;
248 ret = regulator_enable(st->vref);
251 ret = devm_add_action_or_reset(dev, ad7091r_remove, st);
256 /* Use command mode by default to convert only desired channels*/
257 ret = ad7091r_set_mode(st, AD7091R_MODE_COMMAND);
261 return devm_iio_device_register(dev, iio_dev);
263 EXPORT_SYMBOL_NS_GPL(ad7091r_probe, IIO_AD7091R);
265 static bool ad7091r_writeable_reg(struct device *dev, unsigned int reg)
268 case AD7091R_REG_RESULT:
269 case AD7091R_REG_ALERT:
276 static bool ad7091r_volatile_reg(struct device *dev, unsigned int reg)
279 case AD7091R_REG_RESULT:
280 case AD7091R_REG_ALERT:
287 const struct regmap_config ad7091r_regmap_config = {
290 .writeable_reg = ad7091r_writeable_reg,
291 .volatile_reg = ad7091r_volatile_reg,
293 EXPORT_SYMBOL_NS_GPL(ad7091r_regmap_config, IIO_AD7091R);
296 MODULE_DESCRIPTION("Analog Devices AD7091Rx multi-channel converters");
297 MODULE_LICENSE("GPL v2");