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>
24 * If no events enabled - single polled channel read
25 * If event enabled direct reads disable unless channel
26 * is in the read mask.
28 * The noise-delayed bit as per datasheet suggestion is always enabled.
30 * Extref control should be based on regulator provision - not handled.
33 * AD7291 registers definition
35 #define AD7291_COMMAND 0
36 #define AD7291_VOLTAGE 1
37 #define AD7291_T_SENSE 2
38 #define AD7291_T_AVERAGE 3
39 #define AD7291_VOLTAGE_LIMIT_BASE 4
40 #define AD7291_VOLTAGE_LIMIT_COUNT 8
41 #define AD7291_T_SENSE_HIGH 0x1C
42 #define AD7291_T_SENSE_LOW 0x1D
43 #define AD7291_T_SENSE_HYST 0x1E
44 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
45 #define AD7291_T_ALERT_STATUS 0x20
50 #define AD7291_AUTOCYCLE (1 << 0)
51 #define AD7291_RESET (1 << 1)
52 #define AD7291_ALERT_CLEAR (1 << 2)
53 #define AD7291_ALERT_POLARITY (1 << 3)
54 #define AD7291_EXT_REF (1 << 4)
55 #define AD7291_NOISE_DELAY (1 << 5)
56 #define AD7291_T_SENSE_MASK (1 << 7)
57 #define AD7291_VOLTAGE_MASK 0xFF00
58 #define AD7291_VOLTAGE_OFFSET 0x8
63 #define AD7291_CHANNEL_MASK 0xF000
64 #define AD7291_VALUE_MASK 0xFFF
65 #define AD7291_T_VALUE_SIGN 0x400
66 #define AD7291_T_VALUE_FLOAT_OFFSET 2
67 #define AD7291_T_VALUE_FLOAT_MASK 0x2
69 struct ad7291_chip_info {
70 struct i2c_client *client;
72 u8 c_mask; /* Active voltage channels for events */
73 struct mutex state_lock;
76 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
78 struct i2c_client *client = chip->client;
81 ret = i2c_smbus_read_word_data(client, reg);
83 dev_err(&client->dev, "I2C read error\n");
87 *data = swab16((u16)ret);
92 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
94 return i2c_smbus_write_word_data(chip->client, reg, swab16(data));
97 static ssize_t ad7291_store_reset(struct device *dev,
98 struct device_attribute *attr,
102 struct iio_dev *dev_info = dev_get_drvdata(dev);
103 struct ad7291_chip_info *chip = iio_priv(dev_info);
105 return ad7291_i2c_write(chip, AD7291_COMMAND,
106 chip->command | AD7291_RESET);
109 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, ad7291_store_reset, 0);
111 static struct attribute *ad7291_attributes[] = {
112 &iio_dev_attr_reset.dev_attr.attr,
116 static const struct attribute_group ad7291_attribute_group = {
117 .attrs = ad7291_attributes,
120 static irqreturn_t ad7291_event_handler(int irq, void *private)
122 struct iio_dev *indio_dev = private;
123 struct ad7291_chip_info *chip = iio_priv(private);
124 u16 t_status, v_status;
127 s64 timestamp = iio_get_time_ns();
129 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
132 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
135 if (!(t_status || v_status))
138 command = chip->command | AD7291_ALERT_CLEAR;
139 ad7291_i2c_write(chip, AD7291_COMMAND, command);
141 command = chip->command & ~AD7291_ALERT_CLEAR;
142 ad7291_i2c_write(chip, AD7291_COMMAND, command);
144 /* For now treat t_sense and t_sense_average the same */
145 if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
146 iio_push_event(indio_dev,
147 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
152 if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
153 iio_push_event(indio_dev,
154 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
160 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
161 if (v_status & (1 << i))
162 iio_push_event(indio_dev,
163 IIO_UNMOD_EVENT_CODE(IIO_IN,
168 if (v_status & (1 << (i + 1)))
169 iio_push_event(indio_dev,
170 IIO_UNMOD_EVENT_CODE(IIO_IN,
180 static inline ssize_t ad7291_show_hyst(struct device *dev,
181 struct device_attribute *attr,
184 struct iio_dev *dev_info = dev_get_drvdata(dev);
185 struct ad7291_chip_info *chip = iio_priv(dev_info);
186 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
190 ret = ad7291_i2c_read(chip, this_attr->address, &data);
194 return sprintf(buf, "%d\n", data & 0x0FFF);
197 static inline ssize_t ad7291_set_hyst(struct device *dev,
198 struct device_attribute *attr,
202 struct iio_dev *dev_info = dev_get_drvdata(dev);
203 struct ad7291_chip_info *chip = iio_priv(dev_info);
204 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
208 ret = kstrtou16(buf, 10, &data);
215 return ad7291_i2c_write(chip, this_attr->address, data);
218 static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw,
220 ad7291_show_hyst, ad7291_set_hyst,
221 AD7291_T_SENSE_HYST);
222 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
224 ad7291_show_hyst, ad7291_set_hyst, 0x06);
225 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
227 ad7291_show_hyst, ad7291_set_hyst, 0x09);
228 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
230 ad7291_show_hyst, ad7291_set_hyst, 0x0C);
231 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
233 ad7291_show_hyst, ad7291_set_hyst, 0x0F);
234 static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw,
236 ad7291_show_hyst, ad7291_set_hyst, 0x12);
237 static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw,
239 ad7291_show_hyst, ad7291_set_hyst, 0x15);
240 static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw,
242 ad7291_show_hyst, ad7291_set_hyst, 0x18);
243 static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw,
245 ad7291_show_hyst, ad7291_set_hyst, 0x1B);
247 static struct attribute *ad7291_event_attributes[] = {
248 &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr,
249 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
250 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
251 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
252 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
253 &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr,
254 &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr,
255 &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr,
256 &iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr,
261 static u8 ad7291_limit_regs[9][2] = {
265 { 0x0E, 0x0D }, /* note reversed order */
274 static int ad7291_read_event_value(struct iio_dev *indio_dev,
278 struct ad7291_chip_info *chip = iio_priv(indio_dev);
285 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
287 reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_NUM(event_code)]
288 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
291 ret = ad7291_i2c_read(chip, reg, &uval);
294 *val = swab16(uval) & 0x0FFF;
298 reg = ad7291_limit_regs[8]
299 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
302 ret = ad7291_i2c_read(chip, reg, &signval);
305 signval = (s16)((swab16(signval) & 0x0FFF) << 4) >> 4;
313 static int ad7291_write_event_value(struct iio_dev *indio_dev,
317 struct ad7291_chip_info *chip = iio_priv(indio_dev);
321 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
323 if (val > 0xFFF || val < 0)
325 reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_NUM(event_code)]
326 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
329 return ad7291_i2c_write(chip, reg, val);
332 if (val > 2047 || val < -2048)
334 reg = ad7291_limit_regs[8]
335 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
338 return ad7291_i2c_write(chip, reg, *(u16 *)&signval);
344 static int ad7291_read_event_config(struct iio_dev *indio_dev,
347 struct ad7291_chip_info *chip = iio_priv(indio_dev);
348 /* To be enabled the channel must simply be on. If any are enabled
349 we are in continuous sampling mode */
351 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
354 (1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code)))
367 static int ad7291_write_event_config(struct iio_dev *indio_dev,
372 struct ad7291_chip_info *chip = iio_priv(indio_dev);
375 mutex_lock(&chip->state_lock);
376 regval = chip->command;
378 * To be enabled the channel must simply be on. If any are enabled
379 * use continuous sampling mode.
380 * Possible to disable temp as well but that makes single read tricky.
382 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
384 if ((!state) && (chip->c_mask &
385 (1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code))))
387 ~(1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code));
388 else if (state && (!(chip->c_mask &
389 (1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code)))))
391 (1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code));
396 regval |= ((u16)chip->c_mask << 8);
397 if (chip->c_mask) /* Enable autocycle? */
398 regval |= AD7291_AUTOCYCLE;
400 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
404 chip->command = regval;
411 mutex_unlock(&chip->state_lock);
415 static int ad7291_read_raw(struct iio_dev *indio_dev,
416 struct iio_chan_spec const *chan,
422 struct ad7291_chip_info *chip = iio_priv(indio_dev);
428 switch (chan->type) {
430 mutex_lock(&chip->state_lock);
431 /* If in autocycle mode drop through */
432 if (chip->command & 0x1) {
433 mutex_unlock(&chip->state_lock);
436 /* Enable this channel alone */
437 regval = chip->command & (~AD7291_VOLTAGE_MASK);
438 regval |= 1 << (15 - chan->channel);
439 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
441 mutex_unlock(&chip->state_lock);
445 ret = i2c_smbus_read_word_data(chip->client,
448 mutex_unlock(&chip->state_lock);
451 *val = swab16((u16)ret) & 0x0FFF;
452 mutex_unlock(&chip->state_lock);
455 /* Assumes tsense bit of command register always set */
456 ret = i2c_smbus_read_word_data(chip->client,
460 signval = (s16)((swab16((u16)ret) & 0x0FFF) << 4) >> 4;
466 case (1 << IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE):
467 ret = i2c_smbus_read_word_data(chip->client,
471 signval = (s16)((swab16((u16)ret) & 0x0FFF) << 4) >> 4;
479 #define AD7291_VOLTAGE_CHAN(_chan) \
481 .type = IIO_VOLTAGE, \
484 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
485 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) \
488 static const struct iio_chan_spec ad7291_channels[] = {
489 AD7291_VOLTAGE_CHAN(0),
490 AD7291_VOLTAGE_CHAN(1),
491 AD7291_VOLTAGE_CHAN(2),
492 AD7291_VOLTAGE_CHAN(3),
493 AD7291_VOLTAGE_CHAN(4),
494 AD7291_VOLTAGE_CHAN(5),
495 AD7291_VOLTAGE_CHAN(6),
496 AD7291_VOLTAGE_CHAN(7),
499 .info_mask = (1 << IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE),
503 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|
504 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)
508 static struct attribute_group ad7291_event_attribute_group = {
509 .attrs = ad7291_event_attributes,
512 static const struct iio_info ad7291_info = {
513 .attrs = &ad7291_attribute_group,
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,
522 static int __devinit ad7291_probe(struct i2c_client *client,
523 const struct i2c_device_id *id)
525 struct ad7291_chip_info *chip;
526 struct iio_dev *indio_dev;
529 indio_dev = iio_allocate_device(sizeof(*chip));
530 if (indio_dev == NULL) {
534 chip = iio_priv(indio_dev);
535 mutex_init(&chip->state_lock);
536 /* this is only used for device removal purposes */
537 i2c_set_clientdata(client, indio_dev);
539 chip->client = client;
540 /* Tsense always enabled */
541 chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK;
543 indio_dev->name = id->name;
544 indio_dev->channels = ad7291_channels;
545 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
547 indio_dev->dev.parent = &client->dev;
548 indio_dev->info = &ad7291_info;
549 indio_dev->modes = INDIO_DIRECT_MODE;
551 if (client->irq > 0) {
552 ret = request_threaded_irq(client->irq,
554 &ad7291_event_handler,
555 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
561 /* set irq polarity low level */
562 chip->command |= AD7291_ALERT_POLARITY;
565 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
568 goto error_unreg_irq;
571 ret = iio_device_register(indio_dev);
573 goto error_unreg_irq;
575 dev_info(&client->dev, "%s temperature sensor registered.\n",
582 free_irq(client->irq, indio_dev);
584 iio_free_device(indio_dev);
589 static int __devexit ad7291_remove(struct i2c_client *client)
591 struct iio_dev *indio_dev = i2c_get_clientdata(client);
594 free_irq(client->irq, indio_dev);
595 iio_device_unregister(indio_dev);
600 static const struct i2c_device_id ad7291_id[] = {
605 MODULE_DEVICE_TABLE(i2c, ad7291_id);
607 static struct i2c_driver ad7291_driver = {
611 .probe = ad7291_probe,
612 .remove = __devexit_p(ad7291_remove),
613 .id_table = ad7291_id,
616 static __init int ad7291_init(void)
618 return i2c_add_driver(&ad7291_driver);
621 static __exit void ad7291_exit(void)
623 i2c_del_driver(&ad7291_driver);
626 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
627 MODULE_DESCRIPTION("Analog Devices AD7291 digital"
628 " temperature sensor driver");
629 MODULE_LICENSE("GPL v2");
631 module_init(ad7291_init);
632 module_exit(ad7291_exit);