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>
29 * If no events enabled - single polled channel read
30 * If event enabled direct reads disable unless channel
31 * is in the read mask.
33 * The noise-delayed bit as per datasheet suggestion is always enabled.
38 * AD7291 registers definition
40 #define AD7291_COMMAND 0x00
41 #define AD7291_VOLTAGE 0x01
42 #define AD7291_T_SENSE 0x02
43 #define AD7291_T_AVERAGE 0x03
44 #define AD7291_DATA_HIGH(x) ((x) * 3 + 0x4)
45 #define AD7291_DATA_LOW(x) ((x) * 3 + 0x5)
46 #define AD7291_HYST(x) ((x) * 3 + 0x6)
47 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
48 #define AD7291_T_ALERT_STATUS 0x20
50 #define AD7291_VOLTAGE_LIMIT_COUNT 8
56 #define AD7291_AUTOCYCLE (1 << 0)
57 #define AD7291_RESET (1 << 1)
58 #define AD7291_ALERT_CLEAR (1 << 2)
59 #define AD7291_ALERT_POLARITY (1 << 3)
60 #define AD7291_EXT_REF (1 << 4)
61 #define AD7291_NOISE_DELAY (1 << 5)
62 #define AD7291_T_SENSE_MASK (1 << 7)
63 #define AD7291_VOLTAGE_MASK 0xFF00
64 #define AD7291_VOLTAGE_OFFSET 0x8
69 #define AD7291_CHANNEL_MASK 0xF000
70 #define AD7291_BITS 12
71 #define AD7291_VALUE_MASK 0xFFF
72 #define AD7291_T_VALUE_SIGN 0x400
73 #define AD7291_T_VALUE_FLOAT_OFFSET 2
74 #define AD7291_T_VALUE_FLOAT_MASK 0x2
76 #define AD7291_BITS 12
78 struct ad7291_chip_info {
79 struct i2c_client *client;
80 struct regulator *reg;
82 u16 c_mask; /* Active voltage channels for events */
83 struct mutex state_lock;
86 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
88 struct i2c_client *client = chip->client;
91 ret = i2c_smbus_read_word_swapped(client, reg);
93 dev_err(&client->dev, "I2C read error\n");
102 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
104 return i2c_smbus_write_word_swapped(chip->client, reg, data);
107 static irqreturn_t ad7291_event_handler(int irq, void *private)
109 struct iio_dev *indio_dev = private;
110 struct ad7291_chip_info *chip = iio_priv(private);
111 u16 t_status, v_status;
114 s64 timestamp = iio_get_time_ns();
116 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
119 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
122 if (!(t_status || v_status))
125 command = chip->command | AD7291_ALERT_CLEAR;
126 ad7291_i2c_write(chip, AD7291_COMMAND, command);
128 command = chip->command & ~AD7291_ALERT_CLEAR;
129 ad7291_i2c_write(chip, AD7291_COMMAND, command);
131 /* For now treat t_sense and t_sense_average the same */
132 if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
133 iio_push_event(indio_dev,
134 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
139 if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
140 iio_push_event(indio_dev,
141 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
147 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
148 if (v_status & (1 << i))
149 iio_push_event(indio_dev,
150 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
155 if (v_status & (1 << (i + 1)))
156 iio_push_event(indio_dev,
157 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
167 static inline ssize_t ad7291_show_hyst(struct device *dev,
168 struct device_attribute *attr,
171 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
172 struct ad7291_chip_info *chip = iio_priv(indio_dev);
173 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
177 ret = ad7291_i2c_read(chip, this_attr->address, &data);
181 return sprintf(buf, "%d\n", data & AD7291_VALUE_MASK);
184 static inline ssize_t ad7291_set_hyst(struct device *dev,
185 struct device_attribute *attr,
189 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
190 struct ad7291_chip_info *chip = iio_priv(indio_dev);
191 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
195 ret = kstrtou16(buf, 10, &data);
199 if (data > AD7291_VALUE_MASK)
202 ret = ad7291_i2c_write(chip, this_attr->address, data);
209 static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw,
211 ad7291_show_hyst, ad7291_set_hyst,
213 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
215 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(0));
216 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
218 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(1));
219 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
221 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(2));
222 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
224 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(3));
225 static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw,
227 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(4));
228 static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw,
230 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(5));
231 static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw,
233 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(6));
234 static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw,
236 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(7));
238 static struct attribute *ad7291_event_attributes[] = {
239 &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr,
240 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
241 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
242 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
243 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
244 &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr,
245 &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr,
246 &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr,
247 &iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr,
251 static unsigned int ad7291_threshold_reg(u64 event_code)
255 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
257 offset = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
266 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING)
267 return AD7291_DATA_LOW(offset);
269 return AD7291_DATA_HIGH(offset);
272 static int ad7291_read_event_value(struct iio_dev *indio_dev,
276 struct ad7291_chip_info *chip = iio_priv(indio_dev);
280 ret = ad7291_i2c_read(chip, ad7291_threshold_reg(event_code), &uval);
284 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
286 *val = uval & AD7291_VALUE_MASK;
289 *val = sign_extend32(uval, 11);
296 static int ad7291_write_event_value(struct iio_dev *indio_dev,
300 struct ad7291_chip_info *chip = iio_priv(indio_dev);
302 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
304 if (val > AD7291_VALUE_MASK || val < 0)
308 if (val > 2047 || val < -2048)
315 return ad7291_i2c_write(chip, ad7291_threshold_reg(event_code), val);
318 static int ad7291_read_event_config(struct iio_dev *indio_dev,
321 struct ad7291_chip_info *chip = iio_priv(indio_dev);
322 /* To be enabled the channel must simply be on. If any are enabled
323 we are in continuous sampling mode */
325 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
328 (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))
341 static int ad7291_write_event_config(struct iio_dev *indio_dev,
346 struct ad7291_chip_info *chip = iio_priv(indio_dev);
349 mutex_lock(&chip->state_lock);
350 regval = chip->command;
352 * To be enabled the channel must simply be on. If any are enabled
353 * use continuous sampling mode.
354 * Possible to disable temp as well but that makes single read tricky.
357 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
359 if ((!state) && (chip->c_mask & (1 << (15 -
360 IIO_EVENT_CODE_EXTRACT_CHAN(event_code)))))
361 chip->c_mask &= ~(1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN
363 else if (state && (!(chip->c_mask & (1 << (15 -
364 IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))))
365 chip->c_mask |= (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN
370 regval &= ~AD7291_AUTOCYCLE;
371 regval |= chip->c_mask;
372 if (chip->c_mask) /* Enable autocycle? */
373 regval |= AD7291_AUTOCYCLE;
375 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
379 chip->command = regval;
386 mutex_unlock(&chip->state_lock);
390 static int ad7291_read_raw(struct iio_dev *indio_dev,
391 struct iio_chan_spec const *chan,
397 struct ad7291_chip_info *chip = iio_priv(indio_dev);
401 case IIO_CHAN_INFO_RAW:
402 switch (chan->type) {
404 mutex_lock(&chip->state_lock);
405 /* If in autocycle mode drop through */
406 if (chip->command & AD7291_AUTOCYCLE) {
407 mutex_unlock(&chip->state_lock);
410 /* Enable this channel alone */
411 regval = chip->command & (~AD7291_VOLTAGE_MASK);
412 regval |= 1 << (15 - chan->channel);
413 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
415 mutex_unlock(&chip->state_lock);
419 ret = i2c_smbus_read_word_swapped(chip->client,
422 mutex_unlock(&chip->state_lock);
425 *val = ret & AD7291_VALUE_MASK;
426 mutex_unlock(&chip->state_lock);
429 /* Assumes tsense bit of command register always set */
430 ret = i2c_smbus_read_word_swapped(chip->client,
434 *val = sign_extend32(ret, 11);
439 case IIO_CHAN_INFO_AVERAGE_RAW:
440 ret = i2c_smbus_read_word_swapped(chip->client,
444 *val = sign_extend32(ret, 11);
446 case IIO_CHAN_INFO_SCALE:
447 switch (chan->type) {
451 vref = regulator_get_voltage(chip->reg);
459 return IIO_VAL_FRACTIONAL_LOG2;
462 * One LSB of the ADC corresponds to 0.25 deg C.
463 * The temperature reading is in 12-bit twos
476 #define AD7291_VOLTAGE_CHAN(_chan) \
478 .type = IIO_VOLTAGE, \
479 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
480 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
483 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
484 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) \
487 static const struct iio_chan_spec ad7291_channels[] = {
488 AD7291_VOLTAGE_CHAN(0),
489 AD7291_VOLTAGE_CHAN(1),
490 AD7291_VOLTAGE_CHAN(2),
491 AD7291_VOLTAGE_CHAN(3),
492 AD7291_VOLTAGE_CHAN(4),
493 AD7291_VOLTAGE_CHAN(5),
494 AD7291_VOLTAGE_CHAN(6),
495 AD7291_VOLTAGE_CHAN(7),
498 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
499 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
500 BIT(IIO_CHAN_INFO_SCALE),
504 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|
505 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)
509 static struct attribute_group ad7291_event_attribute_group = {
510 .attrs = ad7291_event_attributes,
513 static const struct iio_info ad7291_info = {
514 .read_raw = &ad7291_read_raw,
515 .read_event_config = &ad7291_read_event_config,
516 .write_event_config = &ad7291_write_event_config,
517 .read_event_value = &ad7291_read_event_value,
518 .write_event_value = &ad7291_write_event_value,
519 .event_attrs = &ad7291_event_attribute_group,
520 .driver_module = THIS_MODULE,
523 static int ad7291_probe(struct i2c_client *client,
524 const struct i2c_device_id *id)
526 struct ad7291_platform_data *pdata = client->dev.platform_data;
527 struct ad7291_chip_info *chip;
528 struct iio_dev *indio_dev;
531 indio_dev = iio_device_alloc(sizeof(*chip));
532 if (indio_dev == NULL) {
536 chip = iio_priv(indio_dev);
538 if (pdata && pdata->use_external_ref) {
539 chip->reg = regulator_get(&client->dev, "vref");
540 if (IS_ERR(chip->reg))
543 ret = regulator_enable(chip->reg);
548 mutex_init(&chip->state_lock);
549 /* this is only used for device removal purposes */
550 i2c_set_clientdata(client, indio_dev);
552 chip->client = client;
554 chip->command = AD7291_NOISE_DELAY |
555 AD7291_T_SENSE_MASK | /* Tsense always enabled */
556 AD7291_ALERT_POLARITY; /* set irq polarity low level */
558 if (pdata && pdata->use_external_ref)
559 chip->command |= AD7291_EXT_REF;
561 indio_dev->name = id->name;
562 indio_dev->channels = ad7291_channels;
563 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
565 indio_dev->dev.parent = &client->dev;
566 indio_dev->info = &ad7291_info;
567 indio_dev->modes = INDIO_DIRECT_MODE;
569 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
572 goto error_disable_reg;
575 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
578 goto error_disable_reg;
581 if (client->irq > 0) {
582 ret = request_threaded_irq(client->irq,
584 &ad7291_event_handler,
585 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
589 goto error_disable_reg;
592 ret = iio_device_register(indio_dev);
594 goto error_unreg_irq;
600 free_irq(client->irq, indio_dev);
603 regulator_disable(chip->reg);
606 regulator_put(chip->reg);
608 iio_device_free(indio_dev);
613 static int ad7291_remove(struct i2c_client *client)
615 struct iio_dev *indio_dev = i2c_get_clientdata(client);
616 struct ad7291_chip_info *chip = iio_priv(indio_dev);
618 iio_device_unregister(indio_dev);
621 free_irq(client->irq, indio_dev);
624 regulator_disable(chip->reg);
625 regulator_put(chip->reg);
628 iio_device_free(indio_dev);
633 static const struct i2c_device_id ad7291_id[] = {
638 MODULE_DEVICE_TABLE(i2c, ad7291_id);
640 static struct i2c_driver ad7291_driver = {
642 .name = KBUILD_MODNAME,
644 .probe = ad7291_probe,
645 .remove = ad7291_remove,
646 .id_table = ad7291_id,
648 module_i2c_driver(ad7291_driver);
650 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
651 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
652 MODULE_LICENSE("GPL v2");