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 u8 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 *dev_info = dev_get_drvdata(dev);
135 struct ad7291_chip_info *chip = iio_priv(dev_info);
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_IN,
200 if (v_status & (1 << (i + 1)))
201 iio_push_event(indio_dev,
202 IIO_UNMOD_EVENT_CODE(IIO_IN,
212 static inline ssize_t ad7291_show_hyst(struct device *dev,
213 struct device_attribute *attr,
216 struct iio_dev *dev_info = dev_get_drvdata(dev);
217 struct ad7291_chip_info *chip = iio_priv(dev_info);
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 *dev_info = dev_get_drvdata(dev);
235 struct ad7291_chip_info *chip = iio_priv(dev_info);
236 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
240 ret = kstrtou16(buf, 10, &data);
247 return ad7291_i2c_write(chip, this_attr->address, data);
250 static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw,
252 ad7291_show_hyst, ad7291_set_hyst,
253 AD7291_T_SENSE_HYST);
254 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
256 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH0_HYST);
257 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
259 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH1_HYST);
260 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
262 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH2_HYST);
263 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
265 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH3_HYST);
266 static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw,
268 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH4_HYST);
269 static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw,
271 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH5_HYST);
272 static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw,
274 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH6_HYST);
275 static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw,
277 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH7_HYST);
279 static struct attribute *ad7291_event_attributes[] = {
280 &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr,
281 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
282 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
283 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
284 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
285 &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr,
286 &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr,
287 &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr,
288 &iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr,
293 static u8 ad7291_limit_regs[9][2] = {
294 { AD7291_CH0_DATA_HIGH, AD7291_CH0_DATA_LOW },
295 { AD7291_CH1_DATA_HIGH, AD7291_CH1_DATA_LOW },
296 { AD7291_CH2_DATA_HIGH, AD7291_CH2_DATA_LOW },
297 { AD7291_CH3_DATA_HIGH, AD7291_CH3_DATA_LOW }, /* FIXME: ? */
298 { AD7291_CH4_DATA_HIGH, AD7291_CH4_DATA_LOW },
299 { AD7291_CH5_DATA_HIGH, AD7291_CH5_DATA_LOW },
300 { AD7291_CH6_DATA_HIGH, AD7291_CH6_DATA_LOW },
301 { AD7291_CH7_DATA_HIGH, AD7291_CH7_DATA_LOW },
303 { AD7291_T_SENSE_HIGH, AD7291_T_SENSE_LOW },
306 static int ad7291_read_event_value(struct iio_dev *indio_dev,
310 struct ad7291_chip_info *chip = iio_priv(indio_dev);
317 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
319 reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_NUM(event_code)]
320 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
323 ret = ad7291_i2c_read(chip, reg, &uval);
326 *val = uval & AD7291_VALUE_MASK;
330 reg = ad7291_limit_regs[8]
331 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
334 ret = ad7291_i2c_read(chip, reg, &signval);
337 signval = (s16)((signval & AD7291_VALUE_MASK) << 4) >> 4;
345 static int ad7291_write_event_value(struct iio_dev *indio_dev,
349 struct ad7291_chip_info *chip = iio_priv(indio_dev);
353 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
355 if (val > 0xFFF || val < 0)
357 reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_NUM(event_code)]
358 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
360 return ad7291_i2c_write(chip, reg, val);
362 if (val > 2047 || val < -2048)
364 reg = ad7291_limit_regs[8]
365 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
368 return ad7291_i2c_write(chip, reg, *(u16 *)&signval);
374 static int ad7291_read_event_config(struct iio_dev *indio_dev,
377 struct ad7291_chip_info *chip = iio_priv(indio_dev);
378 /* To be enabled the channel must simply be on. If any are enabled
379 we are in continuous sampling mode */
381 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
384 (1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code)))
397 static int ad7291_write_event_config(struct iio_dev *indio_dev,
402 struct ad7291_chip_info *chip = iio_priv(indio_dev);
405 mutex_lock(&chip->state_lock);
406 regval = chip->command;
408 * To be enabled the channel must simply be on. If any are enabled
409 * use continuous sampling mode.
410 * Possible to disable temp as well but that makes single read tricky.
413 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
415 if ((!state) && (chip->c_mask &
416 (1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code))))
418 ~(1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code));
419 else if (state && (!(chip->c_mask &
420 (1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code)))))
422 (1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code));
426 regval &= ~AD7291_AUTOCYCLE;
427 regval |= ((u16)chip->c_mask << 8);
428 if (chip->c_mask) /* Enable autocycle? */
429 regval |= AD7291_AUTOCYCLE;
431 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
435 chip->command = regval;
442 mutex_unlock(&chip->state_lock);
446 static int ad7291_read_raw(struct iio_dev *indio_dev,
447 struct iio_chan_spec const *chan,
453 struct ad7291_chip_info *chip = iio_priv(indio_dev);
454 unsigned int scale_uv;
460 switch (chan->type) {
462 mutex_lock(&chip->state_lock);
463 /* If in autocycle mode drop through */
464 if (chip->command & 0x1) {
465 mutex_unlock(&chip->state_lock);
468 /* Enable this channel alone */
469 regval = chip->command & (~AD7291_VOLTAGE_MASK);
470 regval |= 1 << (15 - chan->channel);
471 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
473 mutex_unlock(&chip->state_lock);
477 ret = i2c_smbus_read_word_data(chip->client,
480 mutex_unlock(&chip->state_lock);
483 *val = swab16((u16)ret) & AD7291_VALUE_MASK;
484 mutex_unlock(&chip->state_lock);
487 /* Assumes tsense bit of command register always set */
488 ret = i2c_smbus_read_word_data(chip->client,
492 signval = (s16)((swab16((u16)ret) &
493 AD7291_VALUE_MASK) << 4) >> 4;
499 case (1 << IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE):
500 ret = i2c_smbus_read_word_data(chip->client,
504 signval = (s16)((swab16((u16)ret) &
505 AD7291_VALUE_MASK) << 4) >> 4;
508 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
509 scale_uv = (chip->int_vref_mv * 1000) >> AD7291_BITS;
510 *val = scale_uv / 1000;
511 *val2 = (scale_uv % 1000) * 1000;
512 return IIO_VAL_INT_PLUS_MICRO;
518 #define AD7291_VOLTAGE_CHAN(_chan) \
520 .type = IIO_VOLTAGE, \
521 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED), \
524 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
525 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) \
528 static const struct iio_chan_spec ad7291_channels[] = {
529 AD7291_VOLTAGE_CHAN(0),
530 AD7291_VOLTAGE_CHAN(1),
531 AD7291_VOLTAGE_CHAN(2),
532 AD7291_VOLTAGE_CHAN(3),
533 AD7291_VOLTAGE_CHAN(4),
534 AD7291_VOLTAGE_CHAN(5),
535 AD7291_VOLTAGE_CHAN(6),
536 AD7291_VOLTAGE_CHAN(7),
539 .info_mask = (1 << IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE),
543 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|
544 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)
548 static struct attribute_group ad7291_event_attribute_group = {
549 .attrs = ad7291_event_attributes,
552 static const struct iio_info ad7291_info = {
553 .attrs = &ad7291_attribute_group,
554 .read_raw = &ad7291_read_raw,
555 .read_event_config = &ad7291_read_event_config,
556 .write_event_config = &ad7291_write_event_config,
557 .read_event_value = &ad7291_read_event_value,
558 .write_event_value = &ad7291_write_event_value,
559 .event_attrs = &ad7291_event_attribute_group,
562 static int __devinit ad7291_probe(struct i2c_client *client,
563 const struct i2c_device_id *id)
565 struct ad7291_chip_info *chip;
566 struct iio_dev *indio_dev;
567 int ret = 0, voltage_uv = 0;
569 indio_dev = iio_allocate_device(sizeof(*chip));
570 if (indio_dev == NULL) {
574 chip = iio_priv(indio_dev);
576 chip->reg = regulator_get(&client->dev, "vcc");
577 if (!IS_ERR(chip->reg)) {
578 ret = regulator_enable(chip->reg);
581 voltage_uv = regulator_get_voltage(chip->reg);
584 mutex_init(&chip->state_lock);
585 /* this is only used for device removal purposes */
586 i2c_set_clientdata(client, indio_dev);
588 chip->client = client;
589 /* Tsense always enabled */
590 chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK;
593 chip->int_vref_mv = voltage_uv / 1000;
594 chip->command |= AD7291_EXT_REF;
596 chip->int_vref_mv = 2500; /* Build-in ref */
599 indio_dev->name = id->name;
600 indio_dev->channels = ad7291_channels;
601 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
603 indio_dev->dev.parent = &client->dev;
604 indio_dev->info = &ad7291_info;
605 indio_dev->modes = INDIO_DIRECT_MODE;
607 if (client->irq > 0) {
608 ret = request_threaded_irq(client->irq,
610 &ad7291_event_handler,
611 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
615 goto error_disable_reg;
617 /* set irq polarity low level */
618 chip->command |= AD7291_ALERT_POLARITY;
621 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
624 goto error_unreg_irq;
627 ret = iio_device_register(indio_dev);
629 goto error_unreg_irq;
631 dev_info(&client->dev, "%s ADC registered.\n",
638 free_irq(client->irq, indio_dev);
640 if (!IS_ERR(chip->reg))
641 regulator_disable(chip->reg);
643 if (!IS_ERR(chip->reg))
644 regulator_put(chip->reg);
646 iio_free_device(indio_dev);
651 static int __devexit ad7291_remove(struct i2c_client *client)
653 struct iio_dev *indio_dev = i2c_get_clientdata(client);
654 struct ad7291_chip_info *chip = iio_priv(indio_dev);
657 free_irq(client->irq, indio_dev);
659 if (!IS_ERR(chip->reg)) {
660 regulator_disable(chip->reg);
661 regulator_put(chip->reg);
664 iio_device_unregister(indio_dev);
669 static const struct i2c_device_id ad7291_id[] = {
674 MODULE_DEVICE_TABLE(i2c, ad7291_id);
676 static struct i2c_driver ad7291_driver = {
680 .probe = ad7291_probe,
681 .remove = __devexit_p(ad7291_remove),
682 .id_table = ad7291_id,
685 static __init int ad7291_init(void)
687 return i2c_add_driver(&ad7291_driver);
690 static __exit void ad7291_exit(void)
692 i2c_del_driver(&ad7291_driver);
695 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
696 MODULE_DESCRIPTION("Analog Devices AD7291 digital"
697 " temperature sensor driver");
698 MODULE_LICENSE("GPL v2");
700 module_init(ad7291_init);
701 module_exit(ad7291_exit);