1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
5 * Copyright 2010-2011 Analog Devices Inc.
8 #include <linux/device.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/events.h>
23 #include <linux/platform_data/ad7291.h>
28 * If no events enabled - single polled channel read
29 * If event enabled direct reads disable unless channel
30 * is in the read mask.
32 * The noise-delayed bit as per datasheet suggestion is always enabled.
36 * AD7291 registers definition
38 #define AD7291_COMMAND 0x00
39 #define AD7291_VOLTAGE 0x01
40 #define AD7291_T_SENSE 0x02
41 #define AD7291_T_AVERAGE 0x03
42 #define AD7291_DATA_HIGH(x) ((x) * 3 + 0x4)
43 #define AD7291_DATA_LOW(x) ((x) * 3 + 0x5)
44 #define AD7291_HYST(x) ((x) * 3 + 0x6)
45 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
46 #define AD7291_T_ALERT_STATUS 0x20
48 #define AD7291_BITS 12
49 #define AD7291_VOLTAGE_LIMIT_COUNT 8
55 #define AD7291_AUTOCYCLE BIT(0)
56 #define AD7291_RESET BIT(1)
57 #define AD7291_ALERT_CLEAR BIT(2)
58 #define AD7291_ALERT_POLARITY BIT(3)
59 #define AD7291_EXT_REF BIT(4)
60 #define AD7291_NOISE_DELAY BIT(5)
61 #define AD7291_T_SENSE_MASK BIT(7)
62 #define AD7291_VOLTAGE_MASK GENMASK(15, 8)
63 #define AD7291_VOLTAGE_OFFSET 8
68 #define AD7291_VALUE_MASK GENMASK(11, 0)
71 * AD7291 alert register bits
73 #define AD7291_T_LOW BIT(0)
74 #define AD7291_T_HIGH BIT(1)
75 #define AD7291_T_AVG_LOW BIT(2)
76 #define AD7291_T_AVG_HIGH BIT(3)
77 #define AD7291_V_LOW(x) BIT((x) * 2)
78 #define AD7291_V_HIGH(x) BIT((x) * 2 + 1)
81 struct ad7291_chip_info
{
82 struct i2c_client
*client
;
83 struct regulator
*reg
;
85 u16 c_mask
; /* Active voltage channels for events */
86 struct mutex state_lock
;
89 static int ad7291_i2c_read(struct ad7291_chip_info
*chip
, u8 reg
, u16
*data
)
91 struct i2c_client
*client
= chip
->client
;
94 ret
= i2c_smbus_read_word_swapped(client
, reg
);
96 dev_err(&client
->dev
, "I2C read error\n");
105 static int ad7291_i2c_write(struct ad7291_chip_info
*chip
, u8 reg
, u16 data
)
107 return i2c_smbus_write_word_swapped(chip
->client
, reg
, data
);
110 static irqreturn_t
ad7291_event_handler(int irq
, void *private)
112 struct iio_dev
*indio_dev
= private;
113 struct ad7291_chip_info
*chip
= iio_priv(private);
114 u16 t_status
, v_status
;
117 s64 timestamp
= iio_get_time_ns(indio_dev
);
119 if (ad7291_i2c_read(chip
, AD7291_T_ALERT_STATUS
, &t_status
))
122 if (ad7291_i2c_read(chip
, AD7291_VOLTAGE_ALERT_STATUS
, &v_status
))
125 if (!(t_status
|| v_status
))
128 command
= chip
->command
| AD7291_ALERT_CLEAR
;
129 ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
131 command
= chip
->command
& ~AD7291_ALERT_CLEAR
;
132 ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
134 /* For now treat t_sense and t_sense_average the same */
135 if ((t_status
& AD7291_T_LOW
) || (t_status
& AD7291_T_AVG_LOW
))
136 iio_push_event(indio_dev
,
137 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
142 if ((t_status
& AD7291_T_HIGH
) || (t_status
& AD7291_T_AVG_HIGH
))
143 iio_push_event(indio_dev
,
144 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
150 for (i
= 0; i
< AD7291_VOLTAGE_LIMIT_COUNT
; i
++) {
151 if (v_status
& AD7291_V_LOW(i
))
152 iio_push_event(indio_dev
,
153 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
,
158 if (v_status
& AD7291_V_HIGH(i
))
159 iio_push_event(indio_dev
,
160 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
,
170 static unsigned int ad7291_threshold_reg(const struct iio_chan_spec
*chan
,
171 enum iio_event_direction dir
,
172 enum iio_event_info info
)
176 switch (chan
->type
) {
178 offset
= chan
->channel
;
181 offset
= AD7291_VOLTAGE_OFFSET
;
188 case IIO_EV_INFO_VALUE
:
189 if (dir
== IIO_EV_DIR_FALLING
)
190 return AD7291_DATA_HIGH(offset
);
192 return AD7291_DATA_LOW(offset
);
193 case IIO_EV_INFO_HYSTERESIS
:
194 return AD7291_HYST(offset
);
201 static int ad7291_read_event_value(struct iio_dev
*indio_dev
,
202 const struct iio_chan_spec
*chan
,
203 enum iio_event_type type
,
204 enum iio_event_direction dir
,
205 enum iio_event_info info
,
208 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
212 ret
= ad7291_i2c_read(chip
, ad7291_threshold_reg(chan
, dir
, info
),
217 if (info
== IIO_EV_INFO_HYSTERESIS
|| chan
->type
== IIO_VOLTAGE
)
218 *val
= uval
& AD7291_VALUE_MASK
;
221 *val
= sign_extend32(uval
, 11);
226 static int ad7291_write_event_value(struct iio_dev
*indio_dev
,
227 const struct iio_chan_spec
*chan
,
228 enum iio_event_type type
,
229 enum iio_event_direction dir
,
230 enum iio_event_info info
,
233 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
235 if (info
== IIO_EV_INFO_HYSTERESIS
|| chan
->type
== IIO_VOLTAGE
) {
236 if (val
> AD7291_VALUE_MASK
|| val
< 0)
239 if (val
> 2047 || val
< -2048)
243 return ad7291_i2c_write(chip
, ad7291_threshold_reg(chan
, dir
, info
),
247 static int ad7291_read_event_config(struct iio_dev
*indio_dev
,
248 const struct iio_chan_spec
*chan
,
249 enum iio_event_type type
,
250 enum iio_event_direction dir
)
252 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
254 * To be enabled the channel must simply be on. If any are enabled
255 * we are in continuous sampling mode
258 switch (chan
->type
) {
260 return !!(chip
->c_mask
& BIT(15 - chan
->channel
));
270 static int ad7291_write_event_config(struct iio_dev
*indio_dev
,
271 const struct iio_chan_spec
*chan
,
272 enum iio_event_type type
,
273 enum iio_event_direction dir
,
277 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
281 mutex_lock(&chip
->state_lock
);
282 regval
= chip
->command
;
284 * To be enabled the channel must simply be on. If any are enabled
285 * use continuous sampling mode.
286 * Possible to disable temp as well but that makes single read tricky.
289 mask
= BIT(15 - chan
->channel
);
291 switch (chan
->type
) {
293 if ((!state
) && (chip
->c_mask
& mask
))
294 chip
->c_mask
&= ~mask
;
295 else if (state
&& (!(chip
->c_mask
& mask
)))
296 chip
->c_mask
|= mask
;
300 regval
&= ~AD7291_AUTOCYCLE
;
301 regval
|= chip
->c_mask
;
302 if (chip
->c_mask
) /* Enable autocycle? */
303 regval
|= AD7291_AUTOCYCLE
;
305 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, regval
);
309 chip
->command
= regval
;
316 mutex_unlock(&chip
->state_lock
);
320 static int ad7291_read_raw(struct iio_dev
*indio_dev
,
321 struct iio_chan_spec
const *chan
,
327 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
331 case IIO_CHAN_INFO_RAW
:
332 switch (chan
->type
) {
334 mutex_lock(&chip
->state_lock
);
335 /* If in autocycle mode drop through */
336 if (chip
->command
& AD7291_AUTOCYCLE
) {
337 mutex_unlock(&chip
->state_lock
);
340 /* Enable this channel alone */
341 regval
= chip
->command
& (~AD7291_VOLTAGE_MASK
);
342 regval
|= BIT(15 - chan
->channel
);
343 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, regval
);
345 mutex_unlock(&chip
->state_lock
);
349 ret
= i2c_smbus_read_word_swapped(chip
->client
,
352 mutex_unlock(&chip
->state_lock
);
355 *val
= ret
& AD7291_VALUE_MASK
;
356 mutex_unlock(&chip
->state_lock
);
359 /* Assumes tsense bit of command register always set */
360 ret
= i2c_smbus_read_word_swapped(chip
->client
,
364 *val
= sign_extend32(ret
, 11);
369 case IIO_CHAN_INFO_AVERAGE_RAW
:
370 ret
= i2c_smbus_read_word_swapped(chip
->client
,
374 *val
= sign_extend32(ret
, 11);
376 case IIO_CHAN_INFO_SCALE
:
377 switch (chan
->type
) {
382 vref
= regulator_get_voltage(chip
->reg
);
390 return IIO_VAL_FRACTIONAL_LOG2
;
393 * One LSB of the ADC corresponds to 0.25 deg C.
394 * The temperature reading is in 12-bit twos
407 static const struct iio_event_spec ad7291_events
[] = {
409 .type
= IIO_EV_TYPE_THRESH
,
410 .dir
= IIO_EV_DIR_RISING
,
411 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
412 BIT(IIO_EV_INFO_ENABLE
),
414 .type
= IIO_EV_TYPE_THRESH
,
415 .dir
= IIO_EV_DIR_FALLING
,
416 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
417 BIT(IIO_EV_INFO_ENABLE
),
419 .type
= IIO_EV_TYPE_THRESH
,
420 .dir
= IIO_EV_DIR_EITHER
,
421 .mask_separate
= BIT(IIO_EV_INFO_HYSTERESIS
),
425 #define AD7291_VOLTAGE_CHAN(_chan) \
427 .type = IIO_VOLTAGE, \
428 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
429 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
432 .event_spec = ad7291_events, \
433 .num_event_specs = ARRAY_SIZE(ad7291_events), \
436 static const struct iio_chan_spec ad7291_channels
[] = {
437 AD7291_VOLTAGE_CHAN(0),
438 AD7291_VOLTAGE_CHAN(1),
439 AD7291_VOLTAGE_CHAN(2),
440 AD7291_VOLTAGE_CHAN(3),
441 AD7291_VOLTAGE_CHAN(4),
442 AD7291_VOLTAGE_CHAN(5),
443 AD7291_VOLTAGE_CHAN(6),
444 AD7291_VOLTAGE_CHAN(7),
447 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
448 BIT(IIO_CHAN_INFO_AVERAGE_RAW
) |
449 BIT(IIO_CHAN_INFO_SCALE
),
452 .event_spec
= ad7291_events
,
453 .num_event_specs
= ARRAY_SIZE(ad7291_events
),
457 static const struct iio_info ad7291_info
= {
458 .read_raw
= &ad7291_read_raw
,
459 .read_event_config
= &ad7291_read_event_config
,
460 .write_event_config
= &ad7291_write_event_config
,
461 .read_event_value
= &ad7291_read_event_value
,
462 .write_event_value
= &ad7291_write_event_value
,
465 static int ad7291_probe(struct i2c_client
*client
,
466 const struct i2c_device_id
*id
)
468 struct ad7291_platform_data
*pdata
= client
->dev
.platform_data
;
469 struct ad7291_chip_info
*chip
;
470 struct iio_dev
*indio_dev
;
473 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*chip
));
476 chip
= iio_priv(indio_dev
);
478 if (pdata
&& pdata
->use_external_ref
) {
479 chip
->reg
= devm_regulator_get(&client
->dev
, "vref");
480 if (IS_ERR(chip
->reg
))
481 return PTR_ERR(chip
->reg
);
483 ret
= regulator_enable(chip
->reg
);
488 mutex_init(&chip
->state_lock
);
489 /* this is only used for device removal purposes */
490 i2c_set_clientdata(client
, indio_dev
);
492 chip
->client
= client
;
494 chip
->command
= AD7291_NOISE_DELAY
|
495 AD7291_T_SENSE_MASK
| /* Tsense always enabled */
496 AD7291_ALERT_POLARITY
; /* set irq polarity low level */
498 if (pdata
&& pdata
->use_external_ref
)
499 chip
->command
|= AD7291_EXT_REF
;
501 indio_dev
->name
= id
->name
;
502 indio_dev
->channels
= ad7291_channels
;
503 indio_dev
->num_channels
= ARRAY_SIZE(ad7291_channels
);
505 indio_dev
->dev
.parent
= &client
->dev
;
506 indio_dev
->dev
.of_node
= client
->dev
.of_node
;
507 indio_dev
->info
= &ad7291_info
;
508 indio_dev
->modes
= INDIO_DIRECT_MODE
;
510 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, AD7291_RESET
);
513 goto error_disable_reg
;
516 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, chip
->command
);
519 goto error_disable_reg
;
522 if (client
->irq
> 0) {
523 ret
= request_threaded_irq(client
->irq
,
525 &ad7291_event_handler
,
526 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
530 goto error_disable_reg
;
533 ret
= iio_device_register(indio_dev
);
535 goto error_unreg_irq
;
541 free_irq(client
->irq
, indio_dev
);
544 regulator_disable(chip
->reg
);
549 static int ad7291_remove(struct i2c_client
*client
)
551 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
552 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
554 iio_device_unregister(indio_dev
);
557 free_irq(client
->irq
, indio_dev
);
560 regulator_disable(chip
->reg
);
565 static const struct i2c_device_id ad7291_id
[] = {
570 MODULE_DEVICE_TABLE(i2c
, ad7291_id
);
572 static struct i2c_driver ad7291_driver
= {
574 .name
= KBUILD_MODNAME
,
576 .probe
= ad7291_probe
,
577 .remove
= ad7291_remove
,
578 .id_table
= ad7291_id
,
580 module_i2c_driver(ad7291_driver
);
582 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
583 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
584 MODULE_LICENSE("GPL v2");