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 ssize_t ad7291_store_reset(struct device *dev,
131 struct device_attribute *attr,
135 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
136 struct ad7291_chip_info *chip = iio_priv(indio_dev);
138 return ad7291_i2c_write(chip, AD7291_COMMAND,
139 chip->command | AD7291_RESET);
142 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, ad7291_store_reset, 0);
144 static struct attribute *ad7291_attributes[] = {
145 &iio_dev_attr_reset.dev_attr.attr,
149 static const struct attribute_group ad7291_attribute_group = {
150 .attrs = ad7291_attributes,
153 static irqreturn_t ad7291_event_handler(int irq, void *private)
155 struct iio_dev *indio_dev = private;
156 struct ad7291_chip_info *chip = iio_priv(private);
157 u16 t_status, v_status;
160 s64 timestamp = iio_get_time_ns();
162 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
165 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
168 if (!(t_status || v_status))
171 command = chip->command | AD7291_ALERT_CLEAR;
172 ad7291_i2c_write(chip, AD7291_COMMAND, command);
174 command = chip->command & ~AD7291_ALERT_CLEAR;
175 ad7291_i2c_write(chip, AD7291_COMMAND, command);
177 /* For now treat t_sense and t_sense_average the same */
178 if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
179 iio_push_event(indio_dev,
180 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
185 if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
186 iio_push_event(indio_dev,
187 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
193 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
194 if (v_status & (1 << i))
195 iio_push_event(indio_dev,
196 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
201 if (v_status & (1 << (i + 1)))
202 iio_push_event(indio_dev,
203 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
213 static inline ssize_t ad7291_show_hyst(struct device *dev,
214 struct device_attribute *attr,
217 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
218 struct ad7291_chip_info *chip = iio_priv(indio_dev);
219 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
223 ret = ad7291_i2c_read(chip, this_attr->address, &data);
227 return sprintf(buf, "%d\n", data & AD7291_VALUE_MASK);
230 static inline ssize_t ad7291_set_hyst(struct device *dev,
231 struct device_attribute *attr,
235 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
236 struct ad7291_chip_info *chip = iio_priv(indio_dev);
237 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
241 ret = kstrtou16(buf, 10, &data);
245 if (data > AD7291_VALUE_MASK)
248 ret = ad7291_i2c_write(chip, this_attr->address, data);
255 static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw,
257 ad7291_show_hyst, ad7291_set_hyst,
258 AD7291_T_SENSE_HYST);
259 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
261 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH0_HYST);
262 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
264 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH1_HYST);
265 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
267 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH2_HYST);
268 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
270 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH3_HYST);
271 static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw,
273 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH4_HYST);
274 static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw,
276 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH5_HYST);
277 static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw,
279 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH6_HYST);
280 static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw,
282 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH7_HYST);
284 static struct attribute *ad7291_event_attributes[] = {
285 &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr,
286 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
287 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
288 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
289 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
290 &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr,
291 &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr,
292 &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr,
293 &iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr,
298 static u8 ad7291_limit_regs[9][2] = {
299 { AD7291_CH0_DATA_HIGH, AD7291_CH0_DATA_LOW },
300 { AD7291_CH1_DATA_HIGH, AD7291_CH1_DATA_LOW },
301 { AD7291_CH2_DATA_HIGH, AD7291_CH2_DATA_LOW },
302 { AD7291_CH3_DATA_HIGH, AD7291_CH3_DATA_LOW }, /* FIXME: ? */
303 { AD7291_CH4_DATA_HIGH, AD7291_CH4_DATA_LOW },
304 { AD7291_CH5_DATA_HIGH, AD7291_CH5_DATA_LOW },
305 { AD7291_CH6_DATA_HIGH, AD7291_CH6_DATA_LOW },
306 { AD7291_CH7_DATA_HIGH, AD7291_CH7_DATA_LOW },
308 { AD7291_T_SENSE_HIGH, AD7291_T_SENSE_LOW },
311 static int ad7291_read_event_value(struct iio_dev *indio_dev,
315 struct ad7291_chip_info *chip = iio_priv(indio_dev);
322 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
324 reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)]
325 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
328 ret = ad7291_i2c_read(chip, reg, &uval);
331 *val = uval & AD7291_VALUE_MASK;
335 reg = ad7291_limit_regs[8]
336 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
339 ret = ad7291_i2c_read(chip, reg, &signval);
342 signval = (s16)((signval & AD7291_VALUE_MASK) << 4) >> 4;
350 static int ad7291_write_event_value(struct iio_dev *indio_dev,
354 struct ad7291_chip_info *chip = iio_priv(indio_dev);
358 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
360 if (val > AD7291_VALUE_MASK || val < 0)
362 reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)]
363 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
365 return ad7291_i2c_write(chip, reg, val);
367 if (val > 2047 || val < -2048)
369 reg = ad7291_limit_regs[8]
370 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
373 return ad7291_i2c_write(chip, reg, *(u16 *)&signval);
379 static int ad7291_read_event_config(struct iio_dev *indio_dev,
382 struct ad7291_chip_info *chip = iio_priv(indio_dev);
383 /* To be enabled the channel must simply be on. If any are enabled
384 we are in continuous sampling mode */
386 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
389 (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))
402 static int ad7291_write_event_config(struct iio_dev *indio_dev,
407 struct ad7291_chip_info *chip = iio_priv(indio_dev);
410 mutex_lock(&chip->state_lock);
411 regval = chip->command;
413 * To be enabled the channel must simply be on. If any are enabled
414 * use continuous sampling mode.
415 * Possible to disable temp as well but that makes single read tricky.
418 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
420 if ((!state) && (chip->c_mask & (1 << (15 -
421 IIO_EVENT_CODE_EXTRACT_CHAN(event_code)))))
422 chip->c_mask &= ~(1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN
424 else if (state && (!(chip->c_mask & (1 << (15 -
425 IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))))
426 chip->c_mask |= (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN
431 regval &= ~AD7291_AUTOCYCLE;
432 regval |= chip->c_mask;
433 if (chip->c_mask) /* Enable autocycle? */
434 regval |= AD7291_AUTOCYCLE;
436 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
440 chip->command = regval;
447 mutex_unlock(&chip->state_lock);
451 static int ad7291_read_raw(struct iio_dev *indio_dev,
452 struct iio_chan_spec const *chan,
458 struct ad7291_chip_info *chip = iio_priv(indio_dev);
459 unsigned int scale_uv;
464 case IIO_CHAN_INFO_RAW:
465 switch (chan->type) {
467 mutex_lock(&chip->state_lock);
468 /* If in autocycle mode drop through */
469 if (chip->command & AD7291_AUTOCYCLE) {
470 mutex_unlock(&chip->state_lock);
473 /* Enable this channel alone */
474 regval = chip->command & (~AD7291_VOLTAGE_MASK);
475 regval |= 1 << (15 - chan->channel);
476 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
478 mutex_unlock(&chip->state_lock);
482 ret = i2c_smbus_read_word_data(chip->client,
485 mutex_unlock(&chip->state_lock);
488 *val = swab16((u16)ret) & AD7291_VALUE_MASK;
489 mutex_unlock(&chip->state_lock);
492 /* Assumes tsense bit of command register always set */
493 ret = i2c_smbus_read_word_data(chip->client,
497 signval = (s16)((swab16((u16)ret) &
498 AD7291_VALUE_MASK) << 4) >> 4;
504 case IIO_CHAN_INFO_AVERAGE_RAW:
505 ret = i2c_smbus_read_word_data(chip->client,
509 signval = (s16)((swab16((u16)ret) &
510 AD7291_VALUE_MASK) << 4) >> 4;
513 case IIO_CHAN_INFO_SCALE:
514 switch (chan->type) {
516 scale_uv = (chip->int_vref_mv * 1000) >> AD7291_BITS;
517 *val = scale_uv / 1000;
518 *val2 = (scale_uv % 1000) * 1000;
519 return IIO_VAL_INT_PLUS_MICRO;
522 * One LSB of the ADC corresponds to 0.25 deg C.
523 * The temperature reading is in 12-bit twos
536 #define AD7291_VOLTAGE_CHAN(_chan) \
538 .type = IIO_VOLTAGE, \
539 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
540 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
543 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
544 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) \
547 static const struct iio_chan_spec ad7291_channels[] = {
548 AD7291_VOLTAGE_CHAN(0),
549 AD7291_VOLTAGE_CHAN(1),
550 AD7291_VOLTAGE_CHAN(2),
551 AD7291_VOLTAGE_CHAN(3),
552 AD7291_VOLTAGE_CHAN(4),
553 AD7291_VOLTAGE_CHAN(5),
554 AD7291_VOLTAGE_CHAN(6),
555 AD7291_VOLTAGE_CHAN(7),
558 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
559 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
560 BIT(IIO_CHAN_INFO_SCALE),
564 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|
565 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)
569 static struct attribute_group ad7291_event_attribute_group = {
570 .attrs = ad7291_event_attributes,
573 static const struct iio_info ad7291_info = {
574 .attrs = &ad7291_attribute_group,
575 .read_raw = &ad7291_read_raw,
576 .read_event_config = &ad7291_read_event_config,
577 .write_event_config = &ad7291_write_event_config,
578 .read_event_value = &ad7291_read_event_value,
579 .write_event_value = &ad7291_write_event_value,
580 .event_attrs = &ad7291_event_attribute_group,
583 static int ad7291_probe(struct i2c_client *client,
584 const struct i2c_device_id *id)
586 struct ad7291_chip_info *chip;
587 struct iio_dev *indio_dev;
588 int ret = 0, voltage_uv = 0;
590 indio_dev = iio_device_alloc(sizeof(*chip));
591 if (indio_dev == NULL) {
595 chip = iio_priv(indio_dev);
597 chip->reg = regulator_get(&client->dev, "vcc");
598 if (!IS_ERR(chip->reg)) {
599 ret = regulator_enable(chip->reg);
602 voltage_uv = regulator_get_voltage(chip->reg);
605 mutex_init(&chip->state_lock);
606 /* this is only used for device removal purposes */
607 i2c_set_clientdata(client, indio_dev);
609 chip->client = client;
611 chip->command = AD7291_NOISE_DELAY |
612 AD7291_T_SENSE_MASK | /* Tsense always enabled */
613 AD7291_ALERT_POLARITY; /* set irq polarity low level */
616 chip->int_vref_mv = voltage_uv / 1000;
617 chip->command |= AD7291_EXT_REF;
619 chip->int_vref_mv = 2500; /* Build-in ref */
622 indio_dev->name = id->name;
623 indio_dev->channels = ad7291_channels;
624 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
626 indio_dev->dev.parent = &client->dev;
627 indio_dev->info = &ad7291_info;
628 indio_dev->modes = INDIO_DIRECT_MODE;
630 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
633 goto error_disable_reg;
636 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
639 goto error_disable_reg;
642 if (client->irq > 0) {
643 ret = request_threaded_irq(client->irq,
645 &ad7291_event_handler,
646 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
650 goto error_disable_reg;
653 ret = iio_device_register(indio_dev);
655 goto error_unreg_irq;
657 dev_info(&client->dev, "%s ADC registered.\n",
664 free_irq(client->irq, indio_dev);
666 if (!IS_ERR(chip->reg))
667 regulator_disable(chip->reg);
669 if (!IS_ERR(chip->reg))
670 regulator_put(chip->reg);
672 iio_device_free(indio_dev);
677 static int ad7291_remove(struct i2c_client *client)
679 struct iio_dev *indio_dev = i2c_get_clientdata(client);
680 struct ad7291_chip_info *chip = iio_priv(indio_dev);
682 iio_device_unregister(indio_dev);
685 free_irq(client->irq, indio_dev);
687 if (!IS_ERR(chip->reg)) {
688 regulator_disable(chip->reg);
689 regulator_put(chip->reg);
692 iio_device_free(indio_dev);
697 static const struct i2c_device_id ad7291_id[] = {
702 MODULE_DEVICE_TABLE(i2c, ad7291_id);
704 static struct i2c_driver ad7291_driver = {
706 .name = KBUILD_MODNAME,
708 .probe = ad7291_probe,
709 .remove = ad7291_remove,
710 .id_table = ad7291_id,
712 module_i2c_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");