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_DATA_HIGH(x) ((x) * 3 + 0x4)
43 #define AD7291_DATA_LOW(x) ((x) * 3 + 0x5)
44 #define AD7291_HYST(x) ((x) * 3 + 0x6)
45 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
46 #define AD7291_T_ALERT_STATUS 0x20
48 #define AD7291_VOLTAGE_LIMIT_COUNT 8
54 #define AD7291_AUTOCYCLE (1 << 0)
55 #define AD7291_RESET (1 << 1)
56 #define AD7291_ALERT_CLEAR (1 << 2)
57 #define AD7291_ALERT_POLARITY (1 << 3)
58 #define AD7291_EXT_REF (1 << 4)
59 #define AD7291_NOISE_DELAY (1 << 5)
60 #define AD7291_T_SENSE_MASK (1 << 7)
61 #define AD7291_VOLTAGE_MASK 0xFF00
62 #define AD7291_VOLTAGE_OFFSET 0x8
67 #define AD7291_CHANNEL_MASK 0xF000
68 #define AD7291_BITS 12
69 #define AD7291_VALUE_MASK 0xFFF
70 #define AD7291_T_VALUE_SIGN 0x400
71 #define AD7291_T_VALUE_FLOAT_OFFSET 2
72 #define AD7291_T_VALUE_FLOAT_MASK 0x2
74 #define AD7291_BITS 12
76 struct ad7291_chip_info {
77 struct i2c_client *client;
78 struct regulator *reg;
81 u16 c_mask; /* Active voltage channels for events */
82 struct mutex state_lock;
85 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
87 struct i2c_client *client = chip->client;
90 ret = i2c_smbus_read_word_swapped(client, reg);
92 dev_err(&client->dev, "I2C read error\n");
101 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
103 return i2c_smbus_write_word_swapped(chip->client, reg, data);
106 static irqreturn_t ad7291_event_handler(int irq, void *private)
108 struct iio_dev *indio_dev = private;
109 struct ad7291_chip_info *chip = iio_priv(private);
110 u16 t_status, v_status;
113 s64 timestamp = iio_get_time_ns();
115 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
118 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
121 if (!(t_status || v_status))
124 command = chip->command | AD7291_ALERT_CLEAR;
125 ad7291_i2c_write(chip, AD7291_COMMAND, command);
127 command = chip->command & ~AD7291_ALERT_CLEAR;
128 ad7291_i2c_write(chip, AD7291_COMMAND, command);
130 /* For now treat t_sense and t_sense_average the same */
131 if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
132 iio_push_event(indio_dev,
133 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
138 if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
139 iio_push_event(indio_dev,
140 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
146 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
147 if (v_status & (1 << i))
148 iio_push_event(indio_dev,
149 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
154 if (v_status & (1 << (i + 1)))
155 iio_push_event(indio_dev,
156 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
166 static inline ssize_t ad7291_show_hyst(struct device *dev,
167 struct device_attribute *attr,
170 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
171 struct ad7291_chip_info *chip = iio_priv(indio_dev);
172 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
176 ret = ad7291_i2c_read(chip, this_attr->address, &data);
180 return sprintf(buf, "%d\n", data & AD7291_VALUE_MASK);
183 static inline ssize_t ad7291_set_hyst(struct device *dev,
184 struct device_attribute *attr,
188 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
189 struct ad7291_chip_info *chip = iio_priv(indio_dev);
190 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
194 ret = kstrtou16(buf, 10, &data);
198 if (data > AD7291_VALUE_MASK)
201 ret = ad7291_i2c_write(chip, this_attr->address, data);
208 static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw,
210 ad7291_show_hyst, ad7291_set_hyst,
212 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
214 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(0));
215 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
217 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(1));
218 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
220 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(2));
221 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
223 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(3));
224 static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw,
226 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(4));
227 static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw,
229 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(5));
230 static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw,
232 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(6));
233 static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw,
235 ad7291_show_hyst, ad7291_set_hyst, AD7291_HYST(7));
237 static struct attribute *ad7291_event_attributes[] = {
238 &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr,
239 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
240 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
241 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
242 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
243 &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr,
244 &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr,
245 &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr,
246 &iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr,
250 static unsigned int ad7291_threshold_reg(u64 event_code)
254 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
256 offset = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
265 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING)
266 return AD7291_DATA_LOW(offset);
268 return AD7291_DATA_HIGH(offset);
271 static int ad7291_read_event_value(struct iio_dev *indio_dev,
275 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 signval = (s16)((uval & AD7291_VALUE_MASK) << 4) >> 4;
297 static int ad7291_write_event_value(struct iio_dev *indio_dev,
301 struct ad7291_chip_info *chip = iio_priv(indio_dev);
303 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
305 if (val > AD7291_VALUE_MASK || val < 0)
309 if (val > 2047 || val < -2048)
316 return ad7291_i2c_write(chip, ad7291_threshold_reg(event_code), val);
319 static int ad7291_read_event_config(struct iio_dev *indio_dev,
322 struct ad7291_chip_info *chip = iio_priv(indio_dev);
323 /* To be enabled the channel must simply be on. If any are enabled
324 we are in continuous sampling mode */
326 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
329 (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))
342 static int ad7291_write_event_config(struct iio_dev *indio_dev,
347 struct ad7291_chip_info *chip = iio_priv(indio_dev);
350 mutex_lock(&chip->state_lock);
351 regval = chip->command;
353 * To be enabled the channel must simply be on. If any are enabled
354 * use continuous sampling mode.
355 * Possible to disable temp as well but that makes single read tricky.
358 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
360 if ((!state) && (chip->c_mask & (1 << (15 -
361 IIO_EVENT_CODE_EXTRACT_CHAN(event_code)))))
362 chip->c_mask &= ~(1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN
364 else if (state && (!(chip->c_mask & (1 << (15 -
365 IIO_EVENT_CODE_EXTRACT_CHAN(event_code))))))
366 chip->c_mask |= (1 << (15 - IIO_EVENT_CODE_EXTRACT_CHAN
371 regval &= ~AD7291_AUTOCYCLE;
372 regval |= chip->c_mask;
373 if (chip->c_mask) /* Enable autocycle? */
374 regval |= AD7291_AUTOCYCLE;
376 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
380 chip->command = regval;
387 mutex_unlock(&chip->state_lock);
391 static int ad7291_read_raw(struct iio_dev *indio_dev,
392 struct iio_chan_spec const *chan,
398 struct ad7291_chip_info *chip = iio_priv(indio_dev);
403 case IIO_CHAN_INFO_RAW:
404 switch (chan->type) {
406 mutex_lock(&chip->state_lock);
407 /* If in autocycle mode drop through */
408 if (chip->command & AD7291_AUTOCYCLE) {
409 mutex_unlock(&chip->state_lock);
412 /* Enable this channel alone */
413 regval = chip->command & (~AD7291_VOLTAGE_MASK);
414 regval |= 1 << (15 - chan->channel);
415 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
417 mutex_unlock(&chip->state_lock);
421 ret = i2c_smbus_read_word_swapped(chip->client,
424 mutex_unlock(&chip->state_lock);
427 *val = ret & AD7291_VALUE_MASK;
428 mutex_unlock(&chip->state_lock);
431 /* Assumes tsense bit of command register always set */
432 ret = i2c_smbus_read_word_swapped(chip->client,
436 signval = (s16)((ret & AD7291_VALUE_MASK) << 4) >> 4;
442 case IIO_CHAN_INFO_AVERAGE_RAW:
443 ret = i2c_smbus_read_word_swapped(chip->client,
447 signval = (s16)((ret & AD7291_VALUE_MASK) << 4) >> 4;
450 case IIO_CHAN_INFO_SCALE:
451 switch (chan->type) {
453 *val = chip->int_vref_mv;
455 return IIO_VAL_FRACTIONAL_LOG2;
458 * One LSB of the ADC corresponds to 0.25 deg C.
459 * The temperature reading is in 12-bit twos
472 #define AD7291_VOLTAGE_CHAN(_chan) \
474 .type = IIO_VOLTAGE, \
475 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
476 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
479 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
480 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) \
483 static const struct iio_chan_spec ad7291_channels[] = {
484 AD7291_VOLTAGE_CHAN(0),
485 AD7291_VOLTAGE_CHAN(1),
486 AD7291_VOLTAGE_CHAN(2),
487 AD7291_VOLTAGE_CHAN(3),
488 AD7291_VOLTAGE_CHAN(4),
489 AD7291_VOLTAGE_CHAN(5),
490 AD7291_VOLTAGE_CHAN(6),
491 AD7291_VOLTAGE_CHAN(7),
494 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
495 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
496 BIT(IIO_CHAN_INFO_SCALE),
500 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|
501 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)
505 static struct attribute_group ad7291_event_attribute_group = {
506 .attrs = ad7291_event_attributes,
509 static const struct iio_info ad7291_info = {
510 .read_raw = &ad7291_read_raw,
511 .read_event_config = &ad7291_read_event_config,
512 .write_event_config = &ad7291_write_event_config,
513 .read_event_value = &ad7291_read_event_value,
514 .write_event_value = &ad7291_write_event_value,
515 .event_attrs = &ad7291_event_attribute_group,
518 static int ad7291_probe(struct i2c_client *client,
519 const struct i2c_device_id *id)
521 struct ad7291_chip_info *chip;
522 struct iio_dev *indio_dev;
523 int ret = 0, voltage_uv = 0;
525 indio_dev = iio_device_alloc(sizeof(*chip));
526 if (indio_dev == NULL) {
530 chip = iio_priv(indio_dev);
532 chip->reg = regulator_get(&client->dev, "vcc");
533 if (!IS_ERR(chip->reg)) {
534 ret = regulator_enable(chip->reg);
537 voltage_uv = regulator_get_voltage(chip->reg);
540 mutex_init(&chip->state_lock);
541 /* this is only used for device removal purposes */
542 i2c_set_clientdata(client, indio_dev);
544 chip->client = client;
546 chip->command = AD7291_NOISE_DELAY |
547 AD7291_T_SENSE_MASK | /* Tsense always enabled */
548 AD7291_ALERT_POLARITY; /* set irq polarity low level */
551 chip->int_vref_mv = voltage_uv / 1000;
552 chip->command |= AD7291_EXT_REF;
554 chip->int_vref_mv = 2500; /* Build-in ref */
557 indio_dev->name = id->name;
558 indio_dev->channels = ad7291_channels;
559 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
561 indio_dev->dev.parent = &client->dev;
562 indio_dev->info = &ad7291_info;
563 indio_dev->modes = INDIO_DIRECT_MODE;
565 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
568 goto error_disable_reg;
571 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
574 goto error_disable_reg;
577 if (client->irq > 0) {
578 ret = request_threaded_irq(client->irq,
580 &ad7291_event_handler,
581 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
585 goto error_disable_reg;
588 ret = iio_device_register(indio_dev);
590 goto error_unreg_irq;
596 free_irq(client->irq, indio_dev);
598 if (!IS_ERR(chip->reg))
599 regulator_disable(chip->reg);
601 if (!IS_ERR(chip->reg))
602 regulator_put(chip->reg);
604 iio_device_free(indio_dev);
609 static int ad7291_remove(struct i2c_client *client)
611 struct iio_dev *indio_dev = i2c_get_clientdata(client);
612 struct ad7291_chip_info *chip = iio_priv(indio_dev);
614 iio_device_unregister(indio_dev);
617 free_irq(client->irq, indio_dev);
619 if (!IS_ERR(chip->reg)) {
620 regulator_disable(chip->reg);
621 regulator_put(chip->reg);
624 iio_device_free(indio_dev);
629 static const struct i2c_device_id ad7291_id[] = {
634 MODULE_DEVICE_TABLE(i2c, ad7291_id);
636 static struct i2c_driver ad7291_driver = {
638 .name = KBUILD_MODNAME,
640 .probe = ad7291_probe,
641 .remove = ad7291_remove,
642 .id_table = ad7291_id,
644 module_i2c_driver(ad7291_driver);
646 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
647 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
648 MODULE_LICENSE("GPL v2");