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/bitfield.h>
10 #include <linux/iio/events.h>
11 #include <linux/iio/iio.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/consumer.h>
17 #include "ad7091r-base.h"
19 const struct iio_event_spec ad7091r_events
[] = {
21 .type
= IIO_EV_TYPE_THRESH
,
22 .dir
= IIO_EV_DIR_RISING
,
23 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
24 BIT(IIO_EV_INFO_ENABLE
),
27 .type
= IIO_EV_TYPE_THRESH
,
28 .dir
= IIO_EV_DIR_FALLING
,
29 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
30 BIT(IIO_EV_INFO_ENABLE
),
33 .type
= IIO_EV_TYPE_THRESH
,
34 .dir
= IIO_EV_DIR_EITHER
,
35 .mask_separate
= BIT(IIO_EV_INFO_HYSTERESIS
),
38 EXPORT_SYMBOL_NS_GPL(ad7091r_events
, IIO_AD7091R
);
40 static int ad7091r_set_channel(struct ad7091r_state
*st
, unsigned int channel
)
45 /* AD7091R_REG_CHANNEL specified which channels to be converted */
46 ret
= regmap_write(st
->map
, AD7091R_REG_CHANNEL
,
47 BIT(channel
) | (BIT(channel
) << 8));
52 * There is a latency of one conversion before the channel conversion
55 return regmap_read(st
->map
, AD7091R_REG_RESULT
, &dummy
);
58 static int ad7091r_read_one(struct iio_dev
*iio_dev
,
59 unsigned int channel
, unsigned int *read_val
)
61 struct ad7091r_state
*st
= iio_priv(iio_dev
);
65 ret
= ad7091r_set_channel(st
, channel
);
69 ret
= regmap_read(st
->map
, AD7091R_REG_RESULT
, &val
);
73 if (st
->chip_info
->reg_result_chan_id(val
) != channel
)
76 *read_val
= AD7091R_REG_RESULT_CONV_RESULT(val
);
81 static int ad7091r_read_raw(struct iio_dev
*iio_dev
,
82 struct iio_chan_spec
const *chan
,
83 int *val
, int *val2
, long m
)
85 struct ad7091r_state
*st
= iio_priv(iio_dev
);
86 unsigned int read_val
;
89 guard(mutex
)(&st
->lock
);
92 case IIO_CHAN_INFO_RAW
:
93 if (st
->mode
!= AD7091R_MODE_COMMAND
)
96 ret
= ad7091r_read_one(iio_dev
, chan
->channel
, &read_val
);
103 case IIO_CHAN_INFO_SCALE
:
105 ret
= regulator_get_voltage(st
->vref
);
111 *val
= st
->chip_info
->vref_mV
;
114 *val2
= chan
->scan_type
.realbits
;
115 return IIO_VAL_FRACTIONAL_LOG2
;
122 static int ad7091r_read_event_config(struct iio_dev
*indio_dev
,
123 const struct iio_chan_spec
*chan
,
124 enum iio_event_type type
,
125 enum iio_event_direction dir
)
127 struct ad7091r_state
*st
= iio_priv(indio_dev
);
131 case IIO_EV_DIR_RISING
:
132 ret
= regmap_read(st
->map
,
133 AD7091R_REG_CH_HIGH_LIMIT(chan
->channel
),
137 return val
!= AD7091R_HIGH_LIMIT
;
138 case IIO_EV_DIR_FALLING
:
139 ret
= regmap_read(st
->map
,
140 AD7091R_REG_CH_LOW_LIMIT(chan
->channel
),
144 return val
!= AD7091R_LOW_LIMIT
;
150 static int ad7091r_write_event_config(struct iio_dev
*indio_dev
,
151 const struct iio_chan_spec
*chan
,
152 enum iio_event_type type
,
153 enum iio_event_direction dir
, int state
)
155 struct ad7091r_state
*st
= iio_priv(indio_dev
);
158 return regmap_set_bits(st
->map
, AD7091R_REG_CONF
,
159 AD7091R_REG_CONF_ALERT_EN
);
162 * Set thresholds either to 0 or to 2^12 - 1 as appropriate to
163 * prevent alerts and thus disable event generation.
166 case IIO_EV_DIR_RISING
:
167 return regmap_write(st
->map
,
168 AD7091R_REG_CH_HIGH_LIMIT(chan
->channel
),
170 case IIO_EV_DIR_FALLING
:
171 return regmap_write(st
->map
,
172 AD7091R_REG_CH_LOW_LIMIT(chan
->channel
),
180 static int ad7091r_read_event_value(struct iio_dev
*indio_dev
,
181 const struct iio_chan_spec
*chan
,
182 enum iio_event_type type
,
183 enum iio_event_direction dir
,
184 enum iio_event_info info
, int *val
, int *val2
)
186 struct ad7091r_state
*st
= iio_priv(indio_dev
);
190 case IIO_EV_INFO_VALUE
:
192 case IIO_EV_DIR_RISING
:
193 ret
= regmap_read(st
->map
,
194 AD7091R_REG_CH_HIGH_LIMIT(chan
->channel
),
199 case IIO_EV_DIR_FALLING
:
200 ret
= regmap_read(st
->map
,
201 AD7091R_REG_CH_LOW_LIMIT(chan
->channel
),
209 case IIO_EV_INFO_HYSTERESIS
:
210 ret
= regmap_read(st
->map
,
211 AD7091R_REG_CH_HYSTERESIS(chan
->channel
),
221 static int ad7091r_write_event_value(struct iio_dev
*indio_dev
,
222 const struct iio_chan_spec
*chan
,
223 enum iio_event_type type
,
224 enum iio_event_direction dir
,
225 enum iio_event_info info
, int val
, int val2
)
227 struct ad7091r_state
*st
= iio_priv(indio_dev
);
230 case IIO_EV_INFO_VALUE
:
232 case IIO_EV_DIR_RISING
:
233 return regmap_write(st
->map
,
234 AD7091R_REG_CH_HIGH_LIMIT(chan
->channel
),
236 case IIO_EV_DIR_FALLING
:
237 return regmap_write(st
->map
,
238 AD7091R_REG_CH_LOW_LIMIT(chan
->channel
),
243 case IIO_EV_INFO_HYSTERESIS
:
244 return regmap_write(st
->map
,
245 AD7091R_REG_CH_HYSTERESIS(chan
->channel
),
252 static const struct iio_info ad7091r_info
= {
253 .read_raw
= ad7091r_read_raw
,
254 .read_event_config
= &ad7091r_read_event_config
,
255 .write_event_config
= &ad7091r_write_event_config
,
256 .read_event_value
= &ad7091r_read_event_value
,
257 .write_event_value
= &ad7091r_write_event_value
,
260 static irqreturn_t
ad7091r_event_handler(int irq
, void *private)
262 struct iio_dev
*iio_dev
= private;
263 struct ad7091r_state
*st
= iio_priv(iio_dev
);
264 unsigned int i
, read_val
;
266 s64 timestamp
= iio_get_time_ns(iio_dev
);
268 ret
= regmap_read(st
->map
, AD7091R_REG_ALERT
, &read_val
);
272 for (i
= 0; i
< st
->chip_info
->num_channels
; i
++) {
273 if (read_val
& BIT(i
* 2))
274 iio_push_event(iio_dev
,
275 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
, i
,
277 IIO_EV_DIR_RISING
), timestamp
);
278 if (read_val
& BIT(i
* 2 + 1))
279 iio_push_event(iio_dev
,
280 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
, i
,
282 IIO_EV_DIR_FALLING
), timestamp
);
288 static void ad7091r_remove(void *data
)
290 struct ad7091r_state
*st
= data
;
292 regulator_disable(st
->vref
);
295 int ad7091r_probe(struct device
*dev
, const struct ad7091r_init_info
*init_info
,
298 struct iio_dev
*iio_dev
;
299 struct ad7091r_state
*st
;
302 iio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
306 st
= iio_priv(iio_dev
);
308 init_info
->init_adc_regmap(st
, init_info
->regmap_config
);
310 return dev_err_probe(st
->dev
, PTR_ERR(st
->map
),
311 "Error initializing regmap\n");
313 iio_dev
->info
= &ad7091r_info
;
314 iio_dev
->modes
= INDIO_DIRECT_MODE
;
316 if (init_info
->setup
) {
317 ret
= init_info
->setup(st
);
323 st
->chip_info
= init_info
->info_irq
;
324 ret
= regmap_update_bits(st
->map
, AD7091R_REG_CONF
,
325 AD7091R_REG_CONF_ALERT_EN
, BIT(4));
329 ret
= devm_request_threaded_irq(dev
, irq
, NULL
,
330 ad7091r_event_handler
,
331 IRQF_TRIGGER_FALLING
|
333 st
->chip_info
->name
, iio_dev
);
337 st
->chip_info
= init_info
->info_no_irq
;
340 iio_dev
->name
= st
->chip_info
->name
;
341 iio_dev
->num_channels
= st
->chip_info
->num_channels
;
342 iio_dev
->channels
= st
->chip_info
->channels
;
344 st
->vref
= devm_regulator_get_optional(dev
, "vref");
345 if (IS_ERR(st
->vref
)) {
346 if (PTR_ERR(st
->vref
) == -EPROBE_DEFER
)
347 return -EPROBE_DEFER
;
350 /* Enable internal vref */
351 ret
= regmap_set_bits(st
->map
, AD7091R_REG_CONF
,
352 AD7091R_REG_CONF_INT_VREF
);
354 return dev_err_probe(st
->dev
, ret
,
355 "Error on enable internal reference\n");
357 ret
= regulator_enable(st
->vref
);
360 ret
= devm_add_action_or_reset(dev
, ad7091r_remove
, st
);
365 /* Use command mode by default to convert only desired channels*/
366 ret
= st
->chip_info
->set_mode(st
, AD7091R_MODE_COMMAND
);
370 return devm_iio_device_register(dev
, iio_dev
);
372 EXPORT_SYMBOL_NS_GPL(ad7091r_probe
, IIO_AD7091R
);
374 bool ad7091r_writeable_reg(struct device
*dev
, unsigned int reg
)
377 case AD7091R_REG_RESULT
:
378 case AD7091R_REG_ALERT
:
384 EXPORT_SYMBOL_NS_GPL(ad7091r_writeable_reg
, IIO_AD7091R
);
386 bool ad7091r_volatile_reg(struct device
*dev
, unsigned int reg
)
389 case AD7091R_REG_RESULT
:
390 case AD7091R_REG_ALERT
:
396 EXPORT_SYMBOL_NS_GPL(ad7091r_volatile_reg
, IIO_AD7091R
);
398 MODULE_AUTHOR("Beniamin Bia <beniamin.bia@analog.com>");
399 MODULE_DESCRIPTION("Analog Devices AD7091Rx multi-channel converters");
400 MODULE_LICENSE("GPL v2");