2 * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
4 * Copyright 2010-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/err.h>
26 * If no events enabled - single polled channel read
27 * If event enabled direct reads disable unless channel
28 * is in the read mask.
30 * The noise-delayed bit as per datasheet suggestion is always enabled.
35 * AD7291 registers definition
37 #define AD7291_COMMAND 0x00
38 #define AD7291_VOLTAGE 0x01
39 #define AD7291_T_SENSE 0x02
40 #define AD7291_T_AVERAGE 0x03
41 #define AD7291_CH0_DATA_HIGH 0x04
42 #define AD7291_CH0_DATA_LOW 0x05
43 #define AD7291_CH0_HYST 0x06
44 #define AD7291_CH1_DATA_HIGH 0x07
45 #define AD7291_CH1_DATA_LOW 0x08
46 #define AD7291_CH1_HYST 0x09
47 #define AD7291_CH2_DATA_HIGH 0x0A
48 #define AD7291_CH2_DATA_LOW 0x0B
49 #define AD7291_CH2_HYST 0x0C
50 #define AD7291_CH3_DATA_HIGH 0x0D
51 #define AD7291_CH3_DATA_LOW 0x0E
52 #define AD7291_CH3_HYST 0x0F
53 #define AD7291_CH4_DATA_HIGH 0x10
54 #define AD7291_CH4_DATA_LOW 0x11
55 #define AD7291_CH4_HYST 0x12
56 #define AD7291_CH5_DATA_HIGH 0x13
57 #define AD7291_CH5_DATA_LOW 0x14
58 #define AD7291_CH5_HYST 0x15
59 #define AD7291_CH6_DATA_HIGH 0x16
60 #define AD7291_CH6_DATA_LOW 0x17
61 #define AD7291_CH6_HYST 0x18
62 #define AD7291_CH7_DATA_HIGH 0x19
63 #define AD7291_CH7_DATA_LOW 0x1A
64 #define AD7291_CH7_HYST 0x2B
65 #define AD7291_T_SENSE_HIGH 0x1C
66 #define AD7291_T_SENSE_LOW 0x1D
67 #define AD7291_T_SENSE_HYST 0x1E
68 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
69 #define AD7291_T_ALERT_STATUS 0x20
71 #define AD7291_VOLTAGE_LIMIT_COUNT 8
77 #define AD7291_AUTOCYCLE (1 << 0)
78 #define AD7291_RESET (1 << 1)
79 #define AD7291_ALERT_CLEAR (1 << 2)
80 #define AD7291_ALERT_POLARITY (1 << 3)
81 #define AD7291_EXT_REF (1 << 4)
82 #define AD7291_NOISE_DELAY (1 << 5)
83 #define AD7291_T_SENSE_MASK (1 << 7)
84 #define AD7291_VOLTAGE_MASK 0xFF00
85 #define AD7291_VOLTAGE_OFFSET 0x8
90 #define AD7291_CHANNEL_MASK 0xF000
91 #define AD7291_BITS 12
92 #define AD7291_VALUE_MASK 0xFFF
93 #define AD7291_T_VALUE_SIGN 0x400
94 #define AD7291_T_VALUE_FLOAT_OFFSET 2
95 #define AD7291_T_VALUE_FLOAT_MASK 0x2
97 #define AD7291_BITS 12
99 struct ad7291_chip_info
{
100 struct i2c_client
*client
;
101 struct regulator
*reg
;
104 u16 c_mask
; /* Active voltage channels for events */
105 struct mutex state_lock
;
108 static int ad7291_i2c_read(struct ad7291_chip_info
*chip
, u8 reg
, u16
*data
)
110 struct i2c_client
*client
= chip
->client
;
113 ret
= i2c_smbus_read_word_data(client
, reg
);
115 dev_err(&client
->dev
, "I2C read error\n");
119 *data
= swab16((u16
)ret
);
124 static int ad7291_i2c_write(struct ad7291_chip_info
*chip
, u8 reg
, u16 data
)
126 return i2c_smbus_write_word_data(chip
->client
, reg
, swab16(data
));
129 static ssize_t
ad7291_store_reset(struct device
*dev
,
130 struct device_attribute
*attr
,
134 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
135 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
137 return ad7291_i2c_write(chip
, AD7291_COMMAND
,
138 chip
->command
| AD7291_RESET
);
141 static IIO_DEVICE_ATTR(reset
, S_IWUSR
, NULL
, ad7291_store_reset
, 0);
143 static struct attribute
*ad7291_attributes
[] = {
144 &iio_dev_attr_reset
.dev_attr
.attr
,
148 static const struct attribute_group ad7291_attribute_group
= {
149 .attrs
= ad7291_attributes
,
152 static irqreturn_t
ad7291_event_handler(int irq
, void *private)
154 struct iio_dev
*indio_dev
= private;
155 struct ad7291_chip_info
*chip
= iio_priv(private);
156 u16 t_status
, v_status
;
159 s64 timestamp
= iio_get_time_ns();
161 if (ad7291_i2c_read(chip
, AD7291_T_ALERT_STATUS
, &t_status
))
164 if (ad7291_i2c_read(chip
, AD7291_VOLTAGE_ALERT_STATUS
, &v_status
))
167 if (!(t_status
|| v_status
))
170 command
= chip
->command
| AD7291_ALERT_CLEAR
;
171 ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
173 command
= chip
->command
& ~AD7291_ALERT_CLEAR
;
174 ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
176 /* For now treat t_sense and t_sense_average the same */
177 if ((t_status
& (1 << 0)) || (t_status
& (1 << 2)))
178 iio_push_event(indio_dev
,
179 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
184 if ((t_status
& (1 << 1)) || (t_status
& (1 << 3)))
185 iio_push_event(indio_dev
,
186 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
192 for (i
= 0; i
< AD7291_VOLTAGE_LIMIT_COUNT
*2; i
+= 2) {
193 if (v_status
& (1 << i
))
194 iio_push_event(indio_dev
,
195 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
,
200 if (v_status
& (1 << (i
+ 1)))
201 iio_push_event(indio_dev
,
202 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
,
212 static inline ssize_t
ad7291_show_hyst(struct device
*dev
,
213 struct device_attribute
*attr
,
216 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
217 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
218 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
222 ret
= ad7291_i2c_read(chip
, this_attr
->address
, &data
);
226 return sprintf(buf
, "%d\n", data
& AD7291_VALUE_MASK
);
229 static inline ssize_t
ad7291_set_hyst(struct device
*dev
,
230 struct device_attribute
*attr
,
234 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
235 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
236 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
240 ret
= kstrtou16(buf
, 10, &data
);
244 if (data
> AD7291_VALUE_MASK
)
247 ret
= ad7291_i2c_write(chip
, this_attr
->address
, data
);
254 static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw
,
256 ad7291_show_hyst
, ad7291_set_hyst
,
257 AD7291_T_SENSE_HYST
);
258 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw
,
260 ad7291_show_hyst
, ad7291_set_hyst
, AD7291_CH0_HYST
);
261 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw
,
263 ad7291_show_hyst
, ad7291_set_hyst
, AD7291_CH1_HYST
);
264 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw
,
266 ad7291_show_hyst
, ad7291_set_hyst
, AD7291_CH2_HYST
);
267 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw
,
269 ad7291_show_hyst
, ad7291_set_hyst
, AD7291_CH3_HYST
);
270 static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw
,
272 ad7291_show_hyst
, ad7291_set_hyst
, AD7291_CH4_HYST
);
273 static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw
,
275 ad7291_show_hyst
, ad7291_set_hyst
, AD7291_CH5_HYST
);
276 static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw
,
278 ad7291_show_hyst
, ad7291_set_hyst
, AD7291_CH6_HYST
);
279 static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw
,
281 ad7291_show_hyst
, ad7291_set_hyst
, AD7291_CH7_HYST
);
283 static struct attribute
*ad7291_event_attributes
[] = {
284 &iio_dev_attr_in_temp0_thresh_both_hyst_raw
.dev_attr
.attr
,
285 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw
.dev_attr
.attr
,
286 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw
.dev_attr
.attr
,
287 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw
.dev_attr
.attr
,
288 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw
.dev_attr
.attr
,
289 &iio_dev_attr_in_voltage4_thresh_both_hyst_raw
.dev_attr
.attr
,
290 &iio_dev_attr_in_voltage5_thresh_both_hyst_raw
.dev_attr
.attr
,
291 &iio_dev_attr_in_voltage6_thresh_both_hyst_raw
.dev_attr
.attr
,
292 &iio_dev_attr_in_voltage7_thresh_both_hyst_raw
.dev_attr
.attr
,
297 static u8 ad7291_limit_regs
[9][2] = {
298 { AD7291_CH0_DATA_HIGH
, AD7291_CH0_DATA_LOW
},
299 { AD7291_CH1_DATA_HIGH
, AD7291_CH1_DATA_LOW
},
300 { AD7291_CH2_DATA_HIGH
, AD7291_CH2_DATA_LOW
},
301 { AD7291_CH3_DATA_HIGH
, AD7291_CH3_DATA_LOW
}, /* FIXME: ? */
302 { AD7291_CH4_DATA_HIGH
, AD7291_CH4_DATA_LOW
},
303 { AD7291_CH5_DATA_HIGH
, AD7291_CH5_DATA_LOW
},
304 { AD7291_CH6_DATA_HIGH
, AD7291_CH6_DATA_LOW
},
305 { AD7291_CH7_DATA_HIGH
, AD7291_CH7_DATA_LOW
},
307 { AD7291_T_SENSE_HIGH
, AD7291_T_SENSE_LOW
},
310 static int ad7291_read_event_value(struct iio_dev
*indio_dev
,
314 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
321 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
)) {
323 reg
= ad7291_limit_regs
[IIO_EVENT_CODE_EXTRACT_NUM(event_code
)]
324 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code
) ==
327 ret
= ad7291_i2c_read(chip
, reg
, &uval
);
330 *val
= uval
& AD7291_VALUE_MASK
;
334 reg
= ad7291_limit_regs
[8]
335 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code
) ==
338 ret
= ad7291_i2c_read(chip
, reg
, &signval
);
341 signval
= (s16
)((signval
& AD7291_VALUE_MASK
) << 4) >> 4;
349 static int ad7291_write_event_value(struct iio_dev
*indio_dev
,
353 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
357 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
)) {
359 if (val
> AD7291_VALUE_MASK
|| val
< 0)
361 reg
= ad7291_limit_regs
[IIO_EVENT_CODE_EXTRACT_NUM(event_code
)]
362 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code
) ==
364 return ad7291_i2c_write(chip
, reg
, val
);
366 if (val
> 2047 || val
< -2048)
368 reg
= ad7291_limit_regs
[8]
369 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code
) ==
372 return ad7291_i2c_write(chip
, reg
, *(u16
*)&signval
);
378 static int ad7291_read_event_config(struct iio_dev
*indio_dev
,
381 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
382 /* To be enabled the channel must simply be on. If any are enabled
383 we are in continuous sampling mode */
385 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
)) {
388 (1 << (15 - IIO_EVENT_CODE_EXTRACT_NUM(event_code
))))
401 static int ad7291_write_event_config(struct iio_dev
*indio_dev
,
406 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
409 mutex_lock(&chip
->state_lock
);
410 regval
= chip
->command
;
412 * To be enabled the channel must simply be on. If any are enabled
413 * use continuous sampling mode.
414 * Possible to disable temp as well but that makes single read tricky.
417 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code
)) {
419 if ((!state
) && (chip
->c_mask
& (1 << (15 -
420 IIO_EVENT_CODE_EXTRACT_NUM(event_code
)))))
421 chip
->c_mask
&= ~(1 << (15 - IIO_EVENT_CODE_EXTRACT_NUM
423 else if (state
&& (!(chip
->c_mask
& (1 << (15 -
424 IIO_EVENT_CODE_EXTRACT_NUM(event_code
))))))
425 chip
->c_mask
|= (1 << (15 - IIO_EVENT_CODE_EXTRACT_NUM
430 regval
&= ~AD7291_AUTOCYCLE
;
431 regval
|= chip
->c_mask
;
432 if (chip
->c_mask
) /* Enable autocycle? */
433 regval
|= AD7291_AUTOCYCLE
;
435 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, regval
);
439 chip
->command
= regval
;
446 mutex_unlock(&chip
->state_lock
);
450 static int ad7291_read_raw(struct iio_dev
*indio_dev
,
451 struct iio_chan_spec
const *chan
,
457 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
458 unsigned int scale_uv
;
464 switch (chan
->type
) {
466 mutex_lock(&chip
->state_lock
);
467 /* If in autocycle mode drop through */
468 if (chip
->command
& AD7291_AUTOCYCLE
) {
469 mutex_unlock(&chip
->state_lock
);
472 /* Enable this channel alone */
473 regval
= chip
->command
& (~AD7291_VOLTAGE_MASK
);
474 regval
|= 1 << (15 - chan
->channel
);
475 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, regval
);
477 mutex_unlock(&chip
->state_lock
);
481 ret
= i2c_smbus_read_word_data(chip
->client
,
484 mutex_unlock(&chip
->state_lock
);
487 *val
= swab16((u16
)ret
) & AD7291_VALUE_MASK
;
488 mutex_unlock(&chip
->state_lock
);
491 /* Assumes tsense bit of command register always set */
492 ret
= i2c_smbus_read_word_data(chip
->client
,
496 signval
= (s16
)((swab16((u16
)ret
) &
497 AD7291_VALUE_MASK
) << 4) >> 4;
503 case (1 << IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE
):
504 ret
= i2c_smbus_read_word_data(chip
->client
,
508 signval
= (s16
)((swab16((u16
)ret
) &
509 AD7291_VALUE_MASK
) << 4) >> 4;
512 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
513 scale_uv
= (chip
->int_vref_mv
* 1000) >> AD7291_BITS
;
514 *val
= scale_uv
/ 1000;
515 *val2
= (scale_uv
% 1000) * 1000;
516 return IIO_VAL_INT_PLUS_MICRO
;
517 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE
):
519 * One LSB of the ADC corresponds to 0.25 deg C.
520 * The temperature reading is in 12-bit twos complement format
529 #define AD7291_VOLTAGE_CHAN(_chan) \
531 .type = IIO_VOLTAGE, \
532 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED), \
535 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
536 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) \
539 static const struct iio_chan_spec ad7291_channels
[] = {
540 AD7291_VOLTAGE_CHAN(0),
541 AD7291_VOLTAGE_CHAN(1),
542 AD7291_VOLTAGE_CHAN(2),
543 AD7291_VOLTAGE_CHAN(3),
544 AD7291_VOLTAGE_CHAN(4),
545 AD7291_VOLTAGE_CHAN(5),
546 AD7291_VOLTAGE_CHAN(6),
547 AD7291_VOLTAGE_CHAN(7),
550 .info_mask
= (1 << IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE
) |
551 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
555 IIO_EV_BIT(IIO_EV_TYPE_THRESH
, IIO_EV_DIR_RISING
)|
556 IIO_EV_BIT(IIO_EV_TYPE_THRESH
, IIO_EV_DIR_FALLING
)
560 static struct attribute_group ad7291_event_attribute_group
= {
561 .attrs
= ad7291_event_attributes
,
564 static const struct iio_info ad7291_info
= {
565 .attrs
= &ad7291_attribute_group
,
566 .read_raw
= &ad7291_read_raw
,
567 .read_event_config
= &ad7291_read_event_config
,
568 .write_event_config
= &ad7291_write_event_config
,
569 .read_event_value
= &ad7291_read_event_value
,
570 .write_event_value
= &ad7291_write_event_value
,
571 .event_attrs
= &ad7291_event_attribute_group
,
574 static int __devinit
ad7291_probe(struct i2c_client
*client
,
575 const struct i2c_device_id
*id
)
577 struct ad7291_chip_info
*chip
;
578 struct iio_dev
*indio_dev
;
579 int ret
= 0, voltage_uv
= 0;
581 indio_dev
= iio_allocate_device(sizeof(*chip
));
582 if (indio_dev
== NULL
) {
586 chip
= iio_priv(indio_dev
);
588 chip
->reg
= regulator_get(&client
->dev
, "vcc");
589 if (!IS_ERR(chip
->reg
)) {
590 ret
= regulator_enable(chip
->reg
);
593 voltage_uv
= regulator_get_voltage(chip
->reg
);
596 mutex_init(&chip
->state_lock
);
597 /* this is only used for device removal purposes */
598 i2c_set_clientdata(client
, indio_dev
);
600 chip
->client
= client
;
602 chip
->command
= AD7291_NOISE_DELAY
|
603 AD7291_T_SENSE_MASK
| /* Tsense always enabled */
604 AD7291_ALERT_POLARITY
; /* set irq polarity low level */
607 chip
->int_vref_mv
= voltage_uv
/ 1000;
608 chip
->command
|= AD7291_EXT_REF
;
610 chip
->int_vref_mv
= 2500; /* Build-in ref */
613 indio_dev
->name
= id
->name
;
614 indio_dev
->channels
= ad7291_channels
;
615 indio_dev
->num_channels
= ARRAY_SIZE(ad7291_channels
);
617 indio_dev
->dev
.parent
= &client
->dev
;
618 indio_dev
->info
= &ad7291_info
;
619 indio_dev
->modes
= INDIO_DIRECT_MODE
;
621 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, AD7291_RESET
);
624 goto error_disable_reg
;
627 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, chip
->command
);
630 goto error_disable_reg
;
633 if (client
->irq
> 0) {
634 ret
= request_threaded_irq(client
->irq
,
636 &ad7291_event_handler
,
637 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
641 goto error_disable_reg
;
644 ret
= iio_device_register(indio_dev
);
646 goto error_unreg_irq
;
648 dev_info(&client
->dev
, "%s ADC registered.\n",
655 free_irq(client
->irq
, indio_dev
);
657 if (!IS_ERR(chip
->reg
))
658 regulator_disable(chip
->reg
);
660 if (!IS_ERR(chip
->reg
))
661 regulator_put(chip
->reg
);
663 iio_free_device(indio_dev
);
668 static int __devexit
ad7291_remove(struct i2c_client
*client
)
670 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
671 struct ad7291_chip_info
*chip
= iio_priv(indio_dev
);
673 iio_device_unregister(indio_dev
);
676 free_irq(client
->irq
, indio_dev
);
678 if (!IS_ERR(chip
->reg
)) {
679 regulator_disable(chip
->reg
);
680 regulator_put(chip
->reg
);
683 iio_free_device(indio_dev
);
688 static const struct i2c_device_id ad7291_id
[] = {
693 MODULE_DEVICE_TABLE(i2c
, ad7291_id
);
695 static struct i2c_driver ad7291_driver
= {
697 .name
= KBUILD_MODNAME
,
699 .probe
= ad7291_probe
,
700 .remove
= __devexit_p(ad7291_remove
),
701 .id_table
= ad7291_id
,
704 static __init
int ad7291_init(void)
706 return i2c_add_driver(&ad7291_driver
);
709 static __exit
void ad7291_exit(void)
711 i2c_del_driver(&ad7291_driver
);
714 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
715 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
716 MODULE_LICENSE("GPL v2");
718 module_init(ad7291_init
);
719 module_exit(ad7291_exit
);