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>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/events.h>
27 * If no events enabled - single polled channel read
28 * If event enabled direct reads disable unless channel
29 * is in the read mask.
31 * 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_CH0_DATA_HIGH 0x04
43 #define AD7291_CH0_DATA_LOW 0x05
44 #define AD7291_CH0_HYST 0x06
45 #define AD7291_CH1_DATA_HIGH 0x07
46 #define AD7291_CH1_DATA_LOW 0x08
47 #define AD7291_CH1_HYST 0x09
48 #define AD7291_CH2_DATA_HIGH 0x0A
49 #define AD7291_CH2_DATA_LOW 0x0B
50 #define AD7291_CH2_HYST 0x0C
51 #define AD7291_CH3_DATA_HIGH 0x0D
52 #define AD7291_CH3_DATA_LOW 0x0E
53 #define AD7291_CH3_HYST 0x0F
54 #define AD7291_CH4_DATA_HIGH 0x10
55 #define AD7291_CH4_DATA_LOW 0x11
56 #define AD7291_CH4_HYST 0x12
57 #define AD7291_CH5_DATA_HIGH 0x13
58 #define AD7291_CH5_DATA_LOW 0x14
59 #define AD7291_CH5_HYST 0x15
60 #define AD7291_CH6_DATA_HIGH 0x16
61 #define AD7291_CH6_DATA_LOW 0x17
62 #define AD7291_CH6_HYST 0x18
63 #define AD7291_CH7_DATA_HIGH 0x19
64 #define AD7291_CH7_DATA_LOW 0x1A
65 #define AD7291_CH7_HYST 0x2B
66 #define AD7291_T_SENSE_HIGH 0x1C
67 #define AD7291_T_SENSE_LOW 0x1D
68 #define AD7291_T_SENSE_HYST 0x1E
69 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
70 #define AD7291_T_ALERT_STATUS 0x20
72 #define AD7291_VOLTAGE_LIMIT_COUNT 8
78 #define AD7291_AUTOCYCLE (1 << 0)
79 #define AD7291_RESET (1 << 1)
80 #define AD7291_ALERT_CLEAR (1 << 2)
81 #define AD7291_ALERT_POLARITY (1 << 3)
82 #define AD7291_EXT_REF (1 << 4)
83 #define AD7291_NOISE_DELAY (1 << 5)
84 #define AD7291_T_SENSE_MASK (1 << 7)
85 #define AD7291_VOLTAGE_MASK 0xFF00
86 #define AD7291_VOLTAGE_OFFSET 0x8
91 #define AD7291_CHANNEL_MASK 0xF000
92 #define AD7291_BITS 12
93 #define AD7291_VALUE_MASK 0xFFF
94 #define AD7291_T_VALUE_SIGN 0x400
95 #define AD7291_T_VALUE_FLOAT_OFFSET 2
96 #define AD7291_T_VALUE_FLOAT_MASK 0x2
98 #define AD7291_BITS 12
100 struct ad7291_chip_info {
101 struct i2c_client *client;
102 struct regulator *reg;
105 u16 c_mask; /* Active voltage channels for events */
106 struct mutex state_lock;
109 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
111 struct i2c_client *client = chip->client;
114 ret = i2c_smbus_read_word_data(client, reg);
116 dev_err(&client->dev, "I2C read error\n");
120 *data = swab16((u16)ret);
125 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
127 return i2c_smbus_write_word_data(chip->client, reg, swab16(data));
130 static irqreturn_t ad7291_event_handler(int irq, void *private)
132 struct iio_dev *indio_dev = private;
133 struct ad7291_chip_info *chip = iio_priv(private);
134 u16 t_status, v_status;
137 s64 timestamp = iio_get_time_ns();
139 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
142 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
145 if (!(t_status || v_status))
148 command = chip->command | AD7291_ALERT_CLEAR;
149 ad7291_i2c_write(chip, AD7291_COMMAND, command);
151 command = chip->command & ~AD7291_ALERT_CLEAR;
152 ad7291_i2c_write(chip, AD7291_COMMAND, command);
154 /* For now treat t_sense and t_sense_average the same */
155 if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
156 iio_push_event(indio_dev,
157 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
162 if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
163 iio_push_event(indio_dev,
164 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
170 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
171 if (v_status & (1 << i))
172 iio_push_event(indio_dev,
173 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
178 if (v_status & (1 << (i + 1)))
179 iio_push_event(indio_dev,
180 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
190 static inline ssize_t ad7291_show_hyst(struct device *dev,
191 struct device_attribute *attr,
194 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
195 struct ad7291_chip_info *chip = iio_priv(indio_dev);
196 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
200 ret = ad7291_i2c_read(chip, this_attr->address, &data);
204 return sprintf(buf, "%d\n", data & AD7291_VALUE_MASK);
207 static inline ssize_t ad7291_set_hyst(struct device *dev,
208 struct device_attribute *attr,
212 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
213 struct ad7291_chip_info *chip = iio_priv(indio_dev);
214 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
218 ret = kstrtou16(buf, 10, &data);
222 if (data > AD7291_VALUE_MASK)
225 ret = ad7291_i2c_write(chip, this_attr->address, data);
232 static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw,
234 ad7291_show_hyst, ad7291_set_hyst,
235 AD7291_T_SENSE_HYST);
236 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
238 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH0_HYST);
239 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
241 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH1_HYST);
242 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
244 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH2_HYST);
245 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
247 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH3_HYST);
248 static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw,
250 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH4_HYST);
251 static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw,
253 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH5_HYST);
254 static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw,
256 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH6_HYST);
257 static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw,
259 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH7_HYST);
261 static struct attribute *ad7291_event_attributes[] = {
262 &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr,
263 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
264 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
265 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
266 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
267 &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr,
268 &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr,
269 &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr,
270 &iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr,
275 static u8 ad7291_limit_regs[9][2] = {
276 { AD7291_CH0_DATA_HIGH, AD7291_CH0_DATA_LOW },
277 { AD7291_CH1_DATA_HIGH, AD7291_CH1_DATA_LOW },
278 { AD7291_CH2_DATA_HIGH, AD7291_CH2_DATA_LOW },
279 { AD7291_CH3_DATA_HIGH, AD7291_CH3_DATA_LOW }, /* FIXME: ? */
280 { AD7291_CH4_DATA_HIGH, AD7291_CH4_DATA_LOW },
281 { AD7291_CH5_DATA_HIGH, AD7291_CH5_DATA_LOW },
282 { AD7291_CH6_DATA_HIGH, AD7291_CH6_DATA_LOW },
283 { AD7291_CH7_DATA_HIGH, AD7291_CH7_DATA_LOW },
285 { AD7291_T_SENSE_HIGH, AD7291_T_SENSE_LOW },
288 static int ad7291_read_event_value(struct iio_dev *indio_dev,
292 struct ad7291_chip_info *chip = iio_priv(indio_dev);
299 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
301 reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)]
302 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
305 ret = ad7291_i2c_read(chip, reg, &uval);
308 *val = uval & AD7291_VALUE_MASK;
312 reg = ad7291_limit_regs[8]
313 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
316 ret = ad7291_i2c_read(chip, reg, &signval);
319 signval = (s16)((signval & AD7291_VALUE_MASK) << 4) >> 4;
327 static int ad7291_write_event_value(struct iio_dev *indio_dev,
331 struct ad7291_chip_info *chip = iio_priv(indio_dev);
335 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
337 if (val > AD7291_VALUE_MASK || val < 0)
339 reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)]
340 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
342 return ad7291_i2c_write(chip, reg, val);
344 if (val > 2047 || val < -2048)
346 reg = ad7291_limit_regs[8]
347 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
350 return ad7291_i2c_write(chip, reg, *(u16 *)&signval);
356 static int ad7291_read_event_config(struct iio_dev *indio_dev,
359 struct ad7291_chip_info *chip = iio_priv(indio_dev);
360 /* To be enabled the channel must simply be on. If any are enabled
361 we are in continuous sampling mode */
363 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
366 (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))
379 static int ad7291_write_event_config(struct iio_dev *indio_dev,
384 struct ad7291_chip_info *chip = iio_priv(indio_dev);
387 mutex_lock(&chip->state_lock);
388 regval = chip->command;
390 * To be enabled the channel must simply be on. If any are enabled
391 * use continuous sampling mode.
392 * Possible to disable temp as well but that makes single read tricky.
395 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
397 if ((!state) && (chip->c_mask & (1 << (15 -
398 IIO_EVENT_CODE_EXTRACT_CHAN(event_code)))))
399 chip->c_mask &= ~(1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN
401 else if (state && (!(chip->c_mask & (1 << (15 -
402 IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))))
403 chip->c_mask |= (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN
408 regval &= ~AD7291_AUTOCYCLE;
409 regval |= chip->c_mask;
410 if (chip->c_mask) /* Enable autocycle? */
411 regval |= AD7291_AUTOCYCLE;
413 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
417 chip->command = regval;
424 mutex_unlock(&chip->state_lock);
428 static int ad7291_read_raw(struct iio_dev *indio_dev,
429 struct iio_chan_spec const *chan,
435 struct ad7291_chip_info *chip = iio_priv(indio_dev);
440 case IIO_CHAN_INFO_RAW:
441 switch (chan->type) {
443 mutex_lock(&chip->state_lock);
444 /* If in autocycle mode drop through */
445 if (chip->command & AD7291_AUTOCYCLE) {
446 mutex_unlock(&chip->state_lock);
449 /* Enable this channel alone */
450 regval = chip->command & (~AD7291_VOLTAGE_MASK);
451 regval |= 1 << (15 - chan->channel);
452 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
454 mutex_unlock(&chip->state_lock);
458 ret = i2c_smbus_read_word_data(chip->client,
461 mutex_unlock(&chip->state_lock);
464 *val = swab16((u16)ret) & AD7291_VALUE_MASK;
465 mutex_unlock(&chip->state_lock);
468 /* Assumes tsense bit of command register always set */
469 ret = i2c_smbus_read_word_data(chip->client,
473 signval = (s16)((swab16((u16)ret) &
474 AD7291_VALUE_MASK) << 4) >> 4;
480 case IIO_CHAN_INFO_AVERAGE_RAW:
481 ret = i2c_smbus_read_word_data(chip->client,
485 signval = (s16)((swab16((u16)ret) &
486 AD7291_VALUE_MASK) << 4) >> 4;
489 case IIO_CHAN_INFO_SCALE:
490 switch (chan->type) {
492 *val = chip->int_vref_mv;
494 return IIO_VAL_FRACTIONAL_LOG2;
497 * One LSB of the ADC corresponds to 0.25 deg C.
498 * The temperature reading is in 12-bit twos
511 #define AD7291_VOLTAGE_CHAN(_chan) \
513 .type = IIO_VOLTAGE, \
514 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
515 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
518 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
519 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) \
522 static const struct iio_chan_spec ad7291_channels[] = {
523 AD7291_VOLTAGE_CHAN(0),
524 AD7291_VOLTAGE_CHAN(1),
525 AD7291_VOLTAGE_CHAN(2),
526 AD7291_VOLTAGE_CHAN(3),
527 AD7291_VOLTAGE_CHAN(4),
528 AD7291_VOLTAGE_CHAN(5),
529 AD7291_VOLTAGE_CHAN(6),
530 AD7291_VOLTAGE_CHAN(7),
533 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
534 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
535 BIT(IIO_CHAN_INFO_SCALE),
539 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|
540 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)
544 static struct attribute_group ad7291_event_attribute_group = {
545 .attrs = ad7291_event_attributes,
548 static const struct iio_info ad7291_info = {
549 .read_raw = &ad7291_read_raw,
550 .read_event_config = &ad7291_read_event_config,
551 .write_event_config = &ad7291_write_event_config,
552 .read_event_value = &ad7291_read_event_value,
553 .write_event_value = &ad7291_write_event_value,
554 .event_attrs = &ad7291_event_attribute_group,
557 static int ad7291_probe(struct i2c_client *client,
558 const struct i2c_device_id *id)
560 struct ad7291_chip_info *chip;
561 struct iio_dev *indio_dev;
562 int ret = 0, voltage_uv = 0;
564 indio_dev = iio_device_alloc(sizeof(*chip));
565 if (indio_dev == NULL) {
569 chip = iio_priv(indio_dev);
571 chip->reg = regulator_get(&client->dev, "vcc");
572 if (!IS_ERR(chip->reg)) {
573 ret = regulator_enable(chip->reg);
576 voltage_uv = regulator_get_voltage(chip->reg);
579 mutex_init(&chip->state_lock);
580 /* this is only used for device removal purposes */
581 i2c_set_clientdata(client, indio_dev);
583 chip->client = client;
585 chip->command = AD7291_NOISE_DELAY |
586 AD7291_T_SENSE_MASK | /* Tsense always enabled */
587 AD7291_ALERT_POLARITY; /* set irq polarity low level */
590 chip->int_vref_mv = voltage_uv / 1000;
591 chip->command |= AD7291_EXT_REF;
593 chip->int_vref_mv = 2500; /* Build-in ref */
596 indio_dev->name = id->name;
597 indio_dev->channels = ad7291_channels;
598 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
600 indio_dev->dev.parent = &client->dev;
601 indio_dev->info = &ad7291_info;
602 indio_dev->modes = INDIO_DIRECT_MODE;
604 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
607 goto error_disable_reg;
610 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
613 goto error_disable_reg;
616 if (client->irq > 0) {
617 ret = request_threaded_irq(client->irq,
619 &ad7291_event_handler,
620 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
624 goto error_disable_reg;
627 ret = iio_device_register(indio_dev);
629 goto error_unreg_irq;
635 free_irq(client->irq, indio_dev);
637 if (!IS_ERR(chip->reg))
638 regulator_disable(chip->reg);
640 if (!IS_ERR(chip->reg))
641 regulator_put(chip->reg);
643 iio_device_free(indio_dev);
648 static int ad7291_remove(struct i2c_client *client)
650 struct iio_dev *indio_dev = i2c_get_clientdata(client);
651 struct ad7291_chip_info *chip = iio_priv(indio_dev);
653 iio_device_unregister(indio_dev);
656 free_irq(client->irq, indio_dev);
658 if (!IS_ERR(chip->reg)) {
659 regulator_disable(chip->reg);
660 regulator_put(chip->reg);
663 iio_device_free(indio_dev);
668 static const struct i2c_device_id ad7291_id[] = {
673 MODULE_DEVICE_TABLE(i2c, ad7291_id);
675 static struct i2c_driver ad7291_driver = {
677 .name = KBUILD_MODNAME,
679 .probe = ad7291_probe,
680 .remove = ad7291_remove,
681 .id_table = ad7291_id,
683 module_i2c_driver(ad7291_driver);
685 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
686 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
687 MODULE_LICENSE("GPL v2");