2 * AD7291 digital temperature sensor driver supporting AD7291
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/interrupt.h>
10 #include <linux/gpio.h>
11 #include <linux/workqueue.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/list.h>
17 #include <linux/i2c.h>
18 #include <linux/rtc.h>
24 * AD7291 registers definition
26 #define AD7291_COMMAND 0
27 #define AD7291_VOLTAGE 1
28 #define AD7291_T_SENSE 2
29 #define AD7291_T_AVERAGE 3
30 #define AD7291_VOLTAGE_LIMIT_BASE 4
31 #define AD7291_VOLTAGE_LIMIT_COUNT 8
32 #define AD7291_T_SENSE_HIGH 0x1c
33 #define AD7291_T_SENSE_LOW 0x1d
34 #define AD7291_T_SENSE_HYST 0x1e
35 #define AD7291_VOLTAGE_ALERT_STATUS 0x1f
36 #define AD7291_T_ALERT_STATUS 0x20
41 #define AD7291_AUTOCYCLE 0x1
42 #define AD7291_RESET 0x2
43 #define AD7291_ALART_CLEAR 0x4
44 #define AD7291_ALART_POLARITY 0x8
45 #define AD7291_EXT_REF 0x10
46 #define AD7291_NOISE_DELAY 0x20
47 #define AD7291_T_SENSE_MASK 0x40
48 #define AD7291_VOLTAGE_MASK 0xff00
49 #define AD7291_VOLTAGE_OFFSET 0x8
54 #define AD7291_CHANNEL_MASK 0xf000
55 #define AD7291_VALUE_MASK 0xfff
56 #define AD7291_T_VALUE_SIGN 0x400
57 #define AD7291_T_VALUE_FLOAT_OFFSET 2
58 #define AD7291_T_VALUE_FLOAT_MASK 0x2
61 * struct ad7291_chip_info - chip specifc information
64 struct ad7291_chip_info {
66 struct i2c_client *client;
67 struct iio_dev *indio_dev;
68 struct work_struct thresh_work;
71 u8 channels; /* Active voltage channels */
75 * struct ad7291_chip_info - chip specifc information
78 struct ad7291_limit_regs {
85 * ad7291 register access by I2C
87 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
89 struct i2c_client *client = chip->client;
92 ret = i2c_smbus_read_word_data(client, reg);
94 dev_err(&client->dev, "I2C read error\n");
98 *data = swab16((u16)ret);
103 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
105 struct i2c_client *client = chip->client;
108 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
110 dev_err(&client->dev, "I2C write error\n");
115 /* Returns negative errno, or else the number of words read. */
116 static int ad7291_i2c_read_data(struct ad7291_chip_info *chip, u8 reg, u16 *data)
118 struct i2c_client *client = chip->client;
123 if (reg == AD7291_T_SENSE || reg == AD7291_T_AVERAGE)
125 else if (reg == AD7291_VOLTAGE) {
126 if (!chip->channels) {
127 dev_err(&client->dev, "No voltage channel is selected.\n");
130 count = 2 + chip->channels * 2;
132 dev_err(&client->dev, "I2C wrong data register\n");
137 commands[1] = (chip->command >> 8) & 0xff;
138 commands[2] = chip->command & 0xff;
141 ret = i2c_master_send(client, commands, 4);
143 dev_err(&client->dev, "I2C master send error\n");
147 ret = i2c_master_recv(client, (u8 *)data, count);
149 dev_err(&client->dev, "I2C master receive error\n");
154 for (i = 0; i < ret; i++)
155 data[i] = swab16(data[i]);
160 static ssize_t ad7291_show_mode(struct device *dev,
161 struct device_attribute *attr,
164 struct iio_dev *dev_info = dev_get_drvdata(dev);
165 struct ad7291_chip_info *chip = dev_info->dev_data;
167 if (chip->command & AD7291_AUTOCYCLE)
168 return sprintf(buf, "autocycle\n");
170 return sprintf(buf, "command\n");
173 static ssize_t ad7291_store_mode(struct device *dev,
174 struct device_attribute *attr,
178 struct iio_dev *dev_info = dev_get_drvdata(dev);
179 struct ad7291_chip_info *chip = dev_info->dev_data;
183 command = chip->command & (~AD7291_AUTOCYCLE);
184 if (strcmp(buf, "autocycle"))
185 command |= AD7291_AUTOCYCLE;
187 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
191 chip->command = command;
196 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
201 static ssize_t ad7291_show_available_modes(struct device *dev,
202 struct device_attribute *attr,
205 return sprintf(buf, "command\nautocycle\n");
208 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7291_show_available_modes, NULL, 0);
210 static ssize_t ad7291_store_reset(struct device *dev,
211 struct device_attribute *attr,
215 struct iio_dev *dev_info = dev_get_drvdata(dev);
216 struct ad7291_chip_info *chip = dev_info->dev_data;
220 command = chip->command | AD7291_RESET;
222 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
229 static IIO_DEVICE_ATTR(reset, S_IWUSR,
234 static ssize_t ad7291_show_ext_ref(struct device *dev,
235 struct device_attribute *attr,
238 struct iio_dev *dev_info = dev_get_drvdata(dev);
239 struct ad7291_chip_info *chip = dev_info->dev_data;
241 return sprintf(buf, "%d\n", !!(chip->command & AD7291_EXT_REF));
244 static ssize_t ad7291_store_ext_ref(struct device *dev,
245 struct device_attribute *attr,
249 struct iio_dev *dev_info = dev_get_drvdata(dev);
250 struct ad7291_chip_info *chip = dev_info->dev_data;
254 command = chip->command & (~AD7291_EXT_REF);
255 if (strcmp(buf, "1"))
256 command |= AD7291_EXT_REF;
258 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
262 chip->command = command;
267 static IIO_DEVICE_ATTR(ext_ref, S_IRUGO | S_IWUSR,
269 ad7291_store_ext_ref,
272 static ssize_t ad7291_show_noise_delay(struct device *dev,
273 struct device_attribute *attr,
276 struct iio_dev *dev_info = dev_get_drvdata(dev);
277 struct ad7291_chip_info *chip = dev_info->dev_data;
279 return sprintf(buf, "%d\n", !!(chip->command & AD7291_NOISE_DELAY));
282 static ssize_t ad7291_store_noise_delay(struct device *dev,
283 struct device_attribute *attr,
287 struct iio_dev *dev_info = dev_get_drvdata(dev);
288 struct ad7291_chip_info *chip = dev_info->dev_data;
292 command = chip->command & (~AD7291_NOISE_DELAY);
293 if (strcmp(buf, "1"))
294 command |= AD7291_NOISE_DELAY;
296 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
300 chip->command = command;
305 static IIO_DEVICE_ATTR(noise_delay, S_IRUGO | S_IWUSR,
306 ad7291_show_noise_delay,
307 ad7291_store_noise_delay,
310 static ssize_t ad7291_show_t_sense(struct device *dev,
311 struct device_attribute *attr,
314 struct iio_dev *dev_info = dev_get_drvdata(dev);
315 struct ad7291_chip_info *chip = dev_info->dev_data;
320 ret = ad7291_i2c_read_data(chip, AD7291_T_SENSE, &data);
324 if (data & AD7291_T_VALUE_SIGN) {
325 /* convert supplement to positive value */
326 data = (AD7291_T_VALUE_SIGN << 1) - data;
330 return sprintf(buf, "%c%d.%.2d\n", sign,
331 (data >> AD7291_T_VALUE_FLOAT_OFFSET),
332 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
335 static IIO_DEVICE_ATTR(t_sense, S_IRUGO, ad7291_show_t_sense, NULL, 0);
337 static ssize_t ad7291_show_t_average(struct device *dev,
338 struct device_attribute *attr,
341 struct iio_dev *dev_info = dev_get_drvdata(dev);
342 struct ad7291_chip_info *chip = dev_info->dev_data;
347 ret = ad7291_i2c_read_data(chip, AD7291_T_AVERAGE, &data);
351 if (data & AD7291_T_VALUE_SIGN) {
352 /* convert supplement to positive value */
353 data = (AD7291_T_VALUE_SIGN << 1) - data;
357 return sprintf(buf, "%c%d.%.2d\n", sign,
358 (data >> AD7291_T_VALUE_FLOAT_OFFSET),
359 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
362 static IIO_DEVICE_ATTR(t_average, S_IRUGO, ad7291_show_t_average, NULL, 0);
364 static ssize_t ad7291_show_voltage(struct device *dev,
365 struct device_attribute *attr,
368 struct iio_dev *dev_info = dev_get_drvdata(dev);
369 struct ad7291_chip_info *chip = dev_info->dev_data;
370 u16 data[AD7291_VOLTAGE_LIMIT_COUNT];
373 ret = ad7291_i2c_read_data(chip, AD7291_VOLTAGE, data);
377 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
378 if (chip->command & (AD7291_T_SENSE_MASK << i)) {
379 ret = sprintf(buf, "channel[%d]=%d\n", i,
380 data[i] & AD7291_VALUE_MASK);
391 static IIO_DEVICE_ATTR(voltage, S_IRUGO, ad7291_show_voltage, NULL, 0);
393 static ssize_t ad7291_show_channel_mask(struct device *dev,
394 struct device_attribute *attr,
397 struct iio_dev *dev_info = dev_get_drvdata(dev);
398 struct ad7291_chip_info *chip = dev_info->dev_data;
400 return sprintf(buf, "0x%x\n", (chip->command & AD7291_VOLTAGE_MASK) >>
401 AD7291_VOLTAGE_OFFSET);
404 static ssize_t ad7291_store_channel_mask(struct device *dev,
405 struct device_attribute *attr,
409 struct iio_dev *dev_info = dev_get_drvdata(dev);
410 struct ad7291_chip_info *chip = dev_info->dev_data;
415 ret = strict_strtoul(buf, 16, &data);
416 if (ret || data > 0xff)
419 command = chip->command & (~AD7291_VOLTAGE_MASK);
420 command |= data << AD7291_VOLTAGE_OFFSET;
422 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
426 chip->command = command;
428 for (i = 0, chip->channels = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
429 if (chip->command & (AD7291_T_SENSE_MASK << i))
436 static IIO_DEVICE_ATTR(channel_mask, S_IRUGO | S_IWUSR,
437 ad7291_show_channel_mask,
438 ad7291_store_channel_mask,
441 static ssize_t ad7291_show_name(struct device *dev,
442 struct device_attribute *attr,
445 struct iio_dev *dev_info = dev_get_drvdata(dev);
446 struct ad7291_chip_info *chip = dev_info->dev_data;
447 return sprintf(buf, "%s\n", chip->name);
450 static IIO_DEVICE_ATTR(name, S_IRUGO, ad7291_show_name, NULL, 0);
452 static struct attribute *ad7291_attributes[] = {
453 &iio_dev_attr_available_modes.dev_attr.attr,
454 &iio_dev_attr_mode.dev_attr.attr,
455 &iio_dev_attr_reset.dev_attr.attr,
456 &iio_dev_attr_ext_ref.dev_attr.attr,
457 &iio_dev_attr_noise_delay.dev_attr.attr,
458 &iio_dev_attr_t_sense.dev_attr.attr,
459 &iio_dev_attr_t_average.dev_attr.attr,
460 &iio_dev_attr_voltage.dev_attr.attr,
461 &iio_dev_attr_channel_mask.dev_attr.attr,
462 &iio_dev_attr_name.dev_attr.attr,
466 static const struct attribute_group ad7291_attribute_group = {
467 .attrs = ad7291_attributes,
471 * temperature bound events
474 #define IIO_EVENT_CODE_AD7291_T_SENSE_HIGH IIO_BUFFER_EVENT_CODE(0)
475 #define IIO_EVENT_CODE_AD7291_T_SENSE_LOW IIO_BUFFER_EVENT_CODE(1)
476 #define IIO_EVENT_CODE_AD7291_T_AVG_HIGH IIO_BUFFER_EVENT_CODE(2)
477 #define IIO_EVENT_CODE_AD7291_T_AVG_LOW IIO_BUFFER_EVENT_CODE(3)
478 #define IIO_EVENT_CODE_AD7291_VOLTAGE_BASE IIO_BUFFER_EVENT_CODE(4)
480 static void ad7291_interrupt_bh(struct work_struct *work_s)
482 struct ad7291_chip_info *chip =
483 container_of(work_s, struct ad7291_chip_info, thresh_work);
484 u16 t_status, v_status;
488 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
491 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
494 if (!(t_status || v_status))
497 command = chip->command | AD7291_ALART_CLEAR;
498 ad7291_i2c_write(chip, AD7291_COMMAND, command);
500 command = chip->command & ~AD7291_ALART_CLEAR;
501 ad7291_i2c_write(chip, AD7291_COMMAND, command);
503 enable_irq(chip->client->irq);
505 for (i = 0; i < 4; i++) {
506 if (t_status & (1 << i))
507 iio_push_event(chip->indio_dev, 0,
508 IIO_EVENT_CODE_AD7291_T_SENSE_HIGH + i,
509 chip->last_timestamp);
512 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i++) {
513 if (v_status & (1 << i))
514 iio_push_event(chip->indio_dev, 0,
515 IIO_EVENT_CODE_AD7291_VOLTAGE_BASE + i,
516 chip->last_timestamp);
520 static int ad7291_interrupt(struct iio_dev *dev_info,
525 struct ad7291_chip_info *chip = dev_info->dev_data;
527 chip->last_timestamp = timestamp;
528 schedule_work(&chip->thresh_work);
533 IIO_EVENT_SH(ad7291, &ad7291_interrupt);
535 static inline ssize_t ad7291_show_t_bound(struct device *dev,
536 struct device_attribute *attr,
540 struct iio_dev *dev_info = dev_get_drvdata(dev);
541 struct ad7291_chip_info *chip = dev_info->dev_data;
546 ret = ad7291_i2c_read(chip, bound_reg, &data);
550 data &= AD7291_VALUE_MASK;
551 if (data & AD7291_T_VALUE_SIGN) {
552 /* convert supplement to positive value */
553 data = (AD7291_T_VALUE_SIGN << 1) - data;
557 return sprintf(buf, "%c%d.%.2d\n", sign,
558 data >> AD7291_T_VALUE_FLOAT_OFFSET,
559 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
562 static inline ssize_t ad7291_set_t_bound(struct device *dev,
563 struct device_attribute *attr,
568 struct iio_dev *dev_info = dev_get_drvdata(dev);
569 struct ad7291_chip_info *chip = dev_info->dev_data;
575 pos = strchr(buf, '.');
577 ret = strict_strtol(buf, 10, &tmp1);
579 if (ret || tmp1 > 127 || tmp1 < -128)
584 if (len > AD7291_T_VALUE_FLOAT_OFFSET)
585 len = AD7291_T_VALUE_FLOAT_OFFSET;
587 ret = strict_strtol(pos, 10, &tmp2);
590 tmp2 = (tmp2 / 25) * 25;
597 data = (data << AD7291_T_VALUE_FLOAT_OFFSET) |
598 (tmp2 & AD7291_T_VALUE_FLOAT_MASK);
600 /* convert positive value to supplyment */
601 data = (AD7291_T_VALUE_SIGN << 1) - data;
603 ret = ad7291_i2c_write(chip, bound_reg, data);
610 static ssize_t ad7291_show_t_sense_high(struct device *dev,
611 struct device_attribute *attr,
614 return ad7291_show_t_bound(dev, attr,
615 AD7291_T_SENSE_HIGH, buf);
618 static inline ssize_t ad7291_set_t_sense_high(struct device *dev,
619 struct device_attribute *attr,
623 return ad7291_set_t_bound(dev, attr,
624 AD7291_T_SENSE_HIGH, buf, len);
627 static ssize_t ad7291_show_t_sense_low(struct device *dev,
628 struct device_attribute *attr,
631 return ad7291_show_t_bound(dev, attr,
632 AD7291_T_SENSE_LOW, buf);
635 static inline ssize_t ad7291_set_t_sense_low(struct device *dev,
636 struct device_attribute *attr,
640 return ad7291_set_t_bound(dev, attr,
641 AD7291_T_SENSE_LOW, buf, len);
644 static ssize_t ad7291_show_t_sense_hyst(struct device *dev,
645 struct device_attribute *attr,
648 return ad7291_show_t_bound(dev, attr,
649 AD7291_T_SENSE_HYST, buf);
652 static inline ssize_t ad7291_set_t_sense_hyst(struct device *dev,
653 struct device_attribute *attr,
657 return ad7291_set_t_bound(dev, attr,
658 AD7291_T_SENSE_HYST, buf, len);
661 static inline ssize_t ad7291_show_v_bound(struct device *dev,
662 struct device_attribute *attr,
666 struct iio_dev *dev_info = dev_get_drvdata(dev);
667 struct ad7291_chip_info *chip = dev_info->dev_data;
671 if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
672 bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
673 AD7291_VOLTAGE_LIMIT_COUNT)
676 ret = ad7291_i2c_read(chip, bound_reg, &data);
680 data &= AD7291_VALUE_MASK;
682 return sprintf(buf, "%d\n", data);
685 static inline ssize_t ad7291_set_v_bound(struct device *dev,
686 struct device_attribute *attr,
691 struct iio_dev *dev_info = dev_get_drvdata(dev);
692 struct ad7291_chip_info *chip = dev_info->dev_data;
697 if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
698 bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
699 AD7291_VOLTAGE_LIMIT_COUNT)
702 ret = strict_strtoul(buf, 10, &value);
704 if (ret || value >= 4096)
708 ret = ad7291_i2c_write(chip, bound_reg, data);
715 static int ad7291_get_voltage_limit_regs(const char *channel)
719 if (strlen(channel) < 3 && channel[0] != 'v')
722 index = channel[1] - '0';
723 if (index >= AD7291_VOLTAGE_LIMIT_COUNT)
729 static ssize_t ad7291_show_voltage_high(struct device *dev,
730 struct device_attribute *attr,
735 regs = ad7291_get_voltage_limit_regs(attr->attr.name);
740 return ad7291_show_t_bound(dev, attr, regs, buf);
743 static inline ssize_t ad7291_set_voltage_high(struct device *dev,
744 struct device_attribute *attr,
750 regs = ad7291_get_voltage_limit_regs(attr->attr.name);
755 return ad7291_set_t_bound(dev, attr, regs, buf, len);
758 static ssize_t ad7291_show_voltage_low(struct device *dev,
759 struct device_attribute *attr,
764 regs = ad7291_get_voltage_limit_regs(attr->attr.name);
769 return ad7291_show_t_bound(dev, attr, regs+1, buf);
772 static inline ssize_t ad7291_set_voltage_low(struct device *dev,
773 struct device_attribute *attr,
779 regs = ad7291_get_voltage_limit_regs(attr->attr.name);
784 return ad7291_set_t_bound(dev, attr, regs+1, buf, len);
787 static ssize_t ad7291_show_voltage_hyst(struct device *dev,
788 struct device_attribute *attr,
793 regs = ad7291_get_voltage_limit_regs(attr->attr.name);
798 return ad7291_show_t_bound(dev, attr, regs+2, buf);
801 static inline ssize_t ad7291_set_voltage_hyst(struct device *dev,
802 struct device_attribute *attr,
808 regs = ad7291_get_voltage_limit_regs(attr->attr.name);
813 return ad7291_set_t_bound(dev, attr, regs+2, buf, len);
816 IIO_EVENT_ATTR_SH(t_sense_high, iio_event_ad7291,
817 ad7291_show_t_sense_high, ad7291_set_t_sense_high, 0);
818 IIO_EVENT_ATTR_SH(t_sense_low, iio_event_ad7291,
819 ad7291_show_t_sense_low, ad7291_set_t_sense_low, 0);
820 IIO_EVENT_ATTR_SH(t_sense_hyst, iio_event_ad7291,
821 ad7291_show_t_sense_hyst, ad7291_set_t_sense_hyst, 0);
823 IIO_EVENT_ATTR_SH(v0_high, iio_event_ad7291,
824 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
825 IIO_EVENT_ATTR_SH(v0_low, iio_event_ad7291,
826 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
827 IIO_EVENT_ATTR_SH(v0_hyst, iio_event_ad7291,
828 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
829 IIO_EVENT_ATTR_SH(v1_high, iio_event_ad7291,
830 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
831 IIO_EVENT_ATTR_SH(v1_low, iio_event_ad7291,
832 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
833 IIO_EVENT_ATTR_SH(v1_hyst, iio_event_ad7291,
834 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
835 IIO_EVENT_ATTR_SH(v2_high, iio_event_ad7291,
836 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
837 IIO_EVENT_ATTR_SH(v2_low, iio_event_ad7291,
838 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
839 IIO_EVENT_ATTR_SH(v2_hyst, iio_event_ad7291,
840 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
841 IIO_EVENT_ATTR_SH(v3_high, iio_event_ad7291,
842 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
843 IIO_EVENT_ATTR_SH(v3_low, iio_event_ad7291,
844 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
845 IIO_EVENT_ATTR_SH(v3_hyst, iio_event_ad7291,
846 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
847 IIO_EVENT_ATTR_SH(v4_high, iio_event_ad7291,
848 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
849 IIO_EVENT_ATTR_SH(v4_low, iio_event_ad7291,
850 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
851 IIO_EVENT_ATTR_SH(v4_hyst, iio_event_ad7291,
852 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
853 IIO_EVENT_ATTR_SH(v5_high, iio_event_ad7291,
854 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
855 IIO_EVENT_ATTR_SH(v5_low, iio_event_ad7291,
856 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
857 IIO_EVENT_ATTR_SH(v5_hyst, iio_event_ad7291,
858 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
859 IIO_EVENT_ATTR_SH(v6_high, iio_event_ad7291,
860 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
861 IIO_EVENT_ATTR_SH(v6_low, iio_event_ad7291,
862 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
863 IIO_EVENT_ATTR_SH(v6_hyst, iio_event_ad7291,
864 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
865 IIO_EVENT_ATTR_SH(v7_high, iio_event_ad7291,
866 ad7291_show_voltage_high, ad7291_set_voltage_high, 0);
867 IIO_EVENT_ATTR_SH(v7_low, iio_event_ad7291,
868 ad7291_show_voltage_low, ad7291_set_voltage_low, 0);
869 IIO_EVENT_ATTR_SH(v7_hyst, iio_event_ad7291,
870 ad7291_show_voltage_hyst, ad7291_set_voltage_hyst, 0);
872 static struct attribute *ad7291_event_attributes[] = {
873 &iio_event_attr_t_sense_high.dev_attr.attr,
874 &iio_event_attr_t_sense_low.dev_attr.attr,
875 &iio_event_attr_t_sense_hyst.dev_attr.attr,
876 &iio_event_attr_v0_high.dev_attr.attr,
877 &iio_event_attr_v0_low.dev_attr.attr,
878 &iio_event_attr_v0_hyst.dev_attr.attr,
879 &iio_event_attr_v1_high.dev_attr.attr,
880 &iio_event_attr_v1_low.dev_attr.attr,
881 &iio_event_attr_v1_hyst.dev_attr.attr,
882 &iio_event_attr_v2_high.dev_attr.attr,
883 &iio_event_attr_v2_low.dev_attr.attr,
884 &iio_event_attr_v2_hyst.dev_attr.attr,
885 &iio_event_attr_v3_high.dev_attr.attr,
886 &iio_event_attr_v3_low.dev_attr.attr,
887 &iio_event_attr_v3_hyst.dev_attr.attr,
888 &iio_event_attr_v4_high.dev_attr.attr,
889 &iio_event_attr_v4_low.dev_attr.attr,
890 &iio_event_attr_v4_hyst.dev_attr.attr,
891 &iio_event_attr_v5_high.dev_attr.attr,
892 &iio_event_attr_v5_low.dev_attr.attr,
893 &iio_event_attr_v5_hyst.dev_attr.attr,
894 &iio_event_attr_v6_high.dev_attr.attr,
895 &iio_event_attr_v6_low.dev_attr.attr,
896 &iio_event_attr_v6_hyst.dev_attr.attr,
897 &iio_event_attr_v7_high.dev_attr.attr,
898 &iio_event_attr_v7_low.dev_attr.attr,
899 &iio_event_attr_v7_hyst.dev_attr.attr,
903 static struct attribute_group ad7291_event_attribute_group = {
904 .attrs = ad7291_event_attributes,
908 * device probe and remove
911 static int __devinit ad7291_probe(struct i2c_client *client,
912 const struct i2c_device_id *id)
914 struct ad7291_chip_info *chip;
917 chip = kzalloc(sizeof(struct ad7291_chip_info), GFP_KERNEL);
922 /* this is only used for device removal purposes */
923 i2c_set_clientdata(client, chip);
925 chip->client = client;
926 chip->name = id->name;
927 chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK;
929 chip->indio_dev = iio_allocate_device();
930 if (chip->indio_dev == NULL) {
932 goto error_free_chip;
935 chip->indio_dev->dev.parent = &client->dev;
936 chip->indio_dev->attrs = &ad7291_attribute_group;
937 chip->indio_dev->event_attrs = &ad7291_event_attribute_group;
938 chip->indio_dev->dev_data = (void *)chip;
939 chip->indio_dev->driver_module = THIS_MODULE;
940 chip->indio_dev->num_interrupt_lines = 1;
941 chip->indio_dev->modes = INDIO_DIRECT_MODE;
943 ret = iio_device_register(chip->indio_dev);
947 if (client->irq > 0) {
948 ret = iio_register_interrupt_line(client->irq,
954 goto error_unreg_dev;
957 * The event handler list element refer to iio_event_ad7291.
958 * All event attributes bind to the same event handler.
959 * So, only register event handler once.
961 iio_add_event_to_list(&iio_event_ad7291,
962 &chip->indio_dev->interrupts[0]->ev_list);
964 INIT_WORK(&chip->thresh_work, ad7291_interrupt_bh);
966 /* set irq polarity low level */
967 chip->command |= AD7291_ALART_POLARITY;
970 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
973 goto error_unreg_irq;
976 dev_info(&client->dev, "%s temperature sensor registered.\n",
982 iio_unregister_interrupt_line(chip->indio_dev, 0);
984 iio_device_unregister(chip->indio_dev);
986 iio_free_device(chip->indio_dev);
993 static int __devexit ad7291_remove(struct i2c_client *client)
995 struct ad7291_chip_info *chip = i2c_get_clientdata(client);
996 struct iio_dev *indio_dev = chip->indio_dev;
999 iio_unregister_interrupt_line(indio_dev, 0);
1000 iio_device_unregister(indio_dev);
1001 iio_free_device(chip->indio_dev);
1007 static const struct i2c_device_id ad7291_id[] = {
1012 MODULE_DEVICE_TABLE(i2c, ad7291_id);
1014 static struct i2c_driver ad7291_driver = {
1018 .probe = ad7291_probe,
1019 .remove = __devexit_p(ad7291_remove),
1020 .id_table = ad7291_id,
1023 static __init int ad7291_init(void)
1025 return i2c_add_driver(&ad7291_driver);
1028 static __exit void ad7291_exit(void)
1030 i2c_del_driver(&ad7291_driver);
1034 MODULE_DESCRIPTION("Analog Devices AD7291 digital"
1035 " temperature sensor driver");
1036 MODULE_LICENSE("GPL v2");
1038 module_init(ad7291_init);
1039 module_exit(ad7291_exit);