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
,
156 struct ad7091r_state
*st
= iio_priv(indio_dev
);
159 return regmap_set_bits(st
->map
, AD7091R_REG_CONF
,
160 AD7091R_REG_CONF_ALERT_EN
);
163 * Set thresholds either to 0 or to 2^12 - 1 as appropriate to
164 * prevent alerts and thus disable event generation.
167 case IIO_EV_DIR_RISING
:
168 return regmap_write(st
->map
,
169 AD7091R_REG_CH_HIGH_LIMIT(chan
->channel
),
171 case IIO_EV_DIR_FALLING
:
172 return regmap_write(st
->map
,
173 AD7091R_REG_CH_LOW_LIMIT(chan
->channel
),
181 static int ad7091r_read_event_value(struct iio_dev
*indio_dev
,
182 const struct iio_chan_spec
*chan
,
183 enum iio_event_type type
,
184 enum iio_event_direction dir
,
185 enum iio_event_info info
, int *val
, int *val2
)
187 struct ad7091r_state
*st
= iio_priv(indio_dev
);
191 case IIO_EV_INFO_VALUE
:
193 case IIO_EV_DIR_RISING
:
194 ret
= regmap_read(st
->map
,
195 AD7091R_REG_CH_HIGH_LIMIT(chan
->channel
),
200 case IIO_EV_DIR_FALLING
:
201 ret
= regmap_read(st
->map
,
202 AD7091R_REG_CH_LOW_LIMIT(chan
->channel
),
210 case IIO_EV_INFO_HYSTERESIS
:
211 ret
= regmap_read(st
->map
,
212 AD7091R_REG_CH_HYSTERESIS(chan
->channel
),
222 static int ad7091r_write_event_value(struct iio_dev
*indio_dev
,
223 const struct iio_chan_spec
*chan
,
224 enum iio_event_type type
,
225 enum iio_event_direction dir
,
226 enum iio_event_info info
, int val
, int val2
)
228 struct ad7091r_state
*st
= iio_priv(indio_dev
);
231 case IIO_EV_INFO_VALUE
:
233 case IIO_EV_DIR_RISING
:
234 return regmap_write(st
->map
,
235 AD7091R_REG_CH_HIGH_LIMIT(chan
->channel
),
237 case IIO_EV_DIR_FALLING
:
238 return regmap_write(st
->map
,
239 AD7091R_REG_CH_LOW_LIMIT(chan
->channel
),
244 case IIO_EV_INFO_HYSTERESIS
:
245 return regmap_write(st
->map
,
246 AD7091R_REG_CH_HYSTERESIS(chan
->channel
),
253 static const struct iio_info ad7091r_info
= {
254 .read_raw
= ad7091r_read_raw
,
255 .read_event_config
= &ad7091r_read_event_config
,
256 .write_event_config
= &ad7091r_write_event_config
,
257 .read_event_value
= &ad7091r_read_event_value
,
258 .write_event_value
= &ad7091r_write_event_value
,
261 static irqreturn_t
ad7091r_event_handler(int irq
, void *private)
263 struct iio_dev
*iio_dev
= private;
264 struct ad7091r_state
*st
= iio_priv(iio_dev
);
265 unsigned int i
, read_val
;
267 s64 timestamp
= iio_get_time_ns(iio_dev
);
269 ret
= regmap_read(st
->map
, AD7091R_REG_ALERT
, &read_val
);
273 for (i
= 0; i
< st
->chip_info
->num_channels
; i
++) {
274 if (read_val
& BIT(i
* 2))
275 iio_push_event(iio_dev
,
276 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
, i
,
278 IIO_EV_DIR_RISING
), timestamp
);
279 if (read_val
& BIT(i
* 2 + 1))
280 iio_push_event(iio_dev
,
281 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
, i
,
283 IIO_EV_DIR_FALLING
), timestamp
);
289 static void ad7091r_remove(void *data
)
291 struct ad7091r_state
*st
= data
;
293 regulator_disable(st
->vref
);
296 int ad7091r_probe(struct device
*dev
, const struct ad7091r_init_info
*init_info
,
299 struct iio_dev
*iio_dev
;
300 struct ad7091r_state
*st
;
303 iio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
307 st
= iio_priv(iio_dev
);
309 init_info
->init_adc_regmap(st
, init_info
->regmap_config
);
311 return dev_err_probe(st
->dev
, PTR_ERR(st
->map
),
312 "Error initializing regmap\n");
314 iio_dev
->info
= &ad7091r_info
;
315 iio_dev
->modes
= INDIO_DIRECT_MODE
;
317 if (init_info
->setup
) {
318 ret
= init_info
->setup(st
);
324 st
->chip_info
= init_info
->info_irq
;
325 ret
= regmap_update_bits(st
->map
, AD7091R_REG_CONF
,
326 AD7091R_REG_CONF_ALERT_EN
, BIT(4));
330 ret
= devm_request_threaded_irq(dev
, irq
, NULL
,
331 ad7091r_event_handler
,
332 IRQF_TRIGGER_FALLING
|
334 st
->chip_info
->name
, iio_dev
);
338 st
->chip_info
= init_info
->info_no_irq
;
341 iio_dev
->name
= st
->chip_info
->name
;
342 iio_dev
->num_channels
= st
->chip_info
->num_channels
;
343 iio_dev
->channels
= st
->chip_info
->channels
;
345 st
->vref
= devm_regulator_get_optional(dev
, "vref");
346 if (IS_ERR(st
->vref
)) {
347 if (PTR_ERR(st
->vref
) == -EPROBE_DEFER
)
348 return -EPROBE_DEFER
;
351 /* Enable internal vref */
352 ret
= regmap_set_bits(st
->map
, AD7091R_REG_CONF
,
353 AD7091R_REG_CONF_INT_VREF
);
355 return dev_err_probe(st
->dev
, ret
,
356 "Error on enable internal reference\n");
358 ret
= regulator_enable(st
->vref
);
361 ret
= devm_add_action_or_reset(dev
, ad7091r_remove
, st
);
366 /* Use command mode by default to convert only desired channels*/
367 ret
= st
->chip_info
->set_mode(st
, AD7091R_MODE_COMMAND
);
371 return devm_iio_device_register(dev
, iio_dev
);
373 EXPORT_SYMBOL_NS_GPL(ad7091r_probe
, "IIO_AD7091R");
375 bool ad7091r_writeable_reg(struct device
*dev
, unsigned int reg
)
378 case AD7091R_REG_RESULT
:
379 case AD7091R_REG_ALERT
:
385 EXPORT_SYMBOL_NS_GPL(ad7091r_writeable_reg
, "IIO_AD7091R");
387 bool ad7091r_volatile_reg(struct device
*dev
, unsigned int reg
)
390 case AD7091R_REG_RESULT
:
391 case AD7091R_REG_ALERT
:
397 EXPORT_SYMBOL_NS_GPL(ad7091r_volatile_reg
, "IIO_AD7091R");
399 MODULE_AUTHOR("Beniamin Bia <beniamin.bia@analog.com>");
400 MODULE_DESCRIPTION("Analog Devices AD7091Rx multi-channel converters");
401 MODULE_LICENSE("GPL v2");