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/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/list.h>
16 #include <linux/i2c.h>
22 * AD7291 registers definition
24 #define AD7291_COMMAND 0
25 #define AD7291_VOLTAGE 1
26 #define AD7291_T_SENSE 2
27 #define AD7291_T_AVERAGE 3
28 #define AD7291_VOLTAGE_LIMIT_BASE 4
29 #define AD7291_VOLTAGE_LIMIT_COUNT 8
30 #define AD7291_T_SENSE_HIGH 0x1c
31 #define AD7291_T_SENSE_LOW 0x1d
32 #define AD7291_T_SENSE_HYST 0x1e
33 #define AD7291_VOLTAGE_ALERT_STATUS 0x1f
34 #define AD7291_T_ALERT_STATUS 0x20
39 #define AD7291_AUTOCYCLE 0x1
40 #define AD7291_RESET 0x2
41 #define AD7291_ALART_CLEAR 0x4
42 #define AD7291_ALART_POLARITY 0x8
43 #define AD7291_EXT_REF 0x10
44 #define AD7291_NOISE_DELAY 0x20
45 #define AD7291_T_SENSE_MASK 0x40
46 #define AD7291_VOLTAGE_MASK 0xff00
47 #define AD7291_VOLTAGE_OFFSET 0x8
52 #define AD7291_CHANNEL_MASK 0xf000
53 #define AD7291_VALUE_MASK 0xfff
54 #define AD7291_T_VALUE_SIGN 0x400
55 #define AD7291_T_VALUE_FLOAT_OFFSET 2
56 #define AD7291_T_VALUE_FLOAT_MASK 0x2
59 * struct ad7291_chip_info - chip specifc information
62 struct ad7291_chip_info
{
63 struct i2c_client
*client
;
64 struct iio_dev
*indio_dev
;
66 u8 channels
; /* Active voltage channels */
70 * struct ad7291_chip_info - chip specifc information
73 struct ad7291_limit_regs
{
80 * ad7291 register access by I2C
82 static int ad7291_i2c_read(struct ad7291_chip_info
*chip
, u8 reg
, u16
*data
)
84 struct i2c_client
*client
= chip
->client
;
87 ret
= i2c_smbus_read_word_data(client
, reg
);
89 dev_err(&client
->dev
, "I2C read error\n");
93 *data
= swab16((u16
)ret
);
98 static int ad7291_i2c_write(struct ad7291_chip_info
*chip
, u8 reg
, u16 data
)
100 struct i2c_client
*client
= chip
->client
;
103 ret
= i2c_smbus_write_word_data(client
, reg
, swab16(data
));
105 dev_err(&client
->dev
, "I2C write error\n");
110 /* Returns negative errno, or else the number of words read. */
111 static int ad7291_i2c_read_data(struct ad7291_chip_info
*chip
, u8 reg
, u16
*data
)
113 struct i2c_client
*client
= chip
->client
;
118 if (reg
== AD7291_T_SENSE
|| reg
== AD7291_T_AVERAGE
)
120 else if (reg
== AD7291_VOLTAGE
) {
121 if (!chip
->channels
) {
122 dev_err(&client
->dev
, "No voltage channel is selected.\n");
125 count
= 2 + chip
->channels
* 2;
127 dev_err(&client
->dev
, "I2C wrong data register\n");
132 commands
[1] = (chip
->command
>> 8) & 0xff;
133 commands
[2] = chip
->command
& 0xff;
136 ret
= i2c_master_send(client
, commands
, 4);
138 dev_err(&client
->dev
, "I2C master send error\n");
142 ret
= i2c_master_recv(client
, (u8
*)data
, count
);
144 dev_err(&client
->dev
, "I2C master receive error\n");
149 for (i
= 0; i
< ret
; i
++)
150 data
[i
] = swab16(data
[i
]);
155 static ssize_t
ad7291_show_mode(struct device
*dev
,
156 struct device_attribute
*attr
,
159 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
160 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
162 if (chip
->command
& AD7291_AUTOCYCLE
)
163 return sprintf(buf
, "autocycle\n");
165 return sprintf(buf
, "command\n");
168 static ssize_t
ad7291_store_mode(struct device
*dev
,
169 struct device_attribute
*attr
,
173 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
174 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
178 command
= chip
->command
& (~AD7291_AUTOCYCLE
);
179 if (strcmp(buf
, "autocycle"))
180 command
|= AD7291_AUTOCYCLE
;
182 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
186 chip
->command
= command
;
191 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
196 static ssize_t
ad7291_show_available_modes(struct device
*dev
,
197 struct device_attribute
*attr
,
200 return sprintf(buf
, "command\nautocycle\n");
203 static IIO_DEVICE_ATTR(available_modes
, S_IRUGO
, ad7291_show_available_modes
, NULL
, 0);
205 static ssize_t
ad7291_store_reset(struct device
*dev
,
206 struct device_attribute
*attr
,
210 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
211 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
215 command
= chip
->command
| AD7291_RESET
;
217 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
224 static IIO_DEVICE_ATTR(reset
, S_IWUSR
,
229 static ssize_t
ad7291_show_ext_ref(struct device
*dev
,
230 struct device_attribute
*attr
,
233 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
234 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
236 return sprintf(buf
, "%d\n", !!(chip
->command
& AD7291_EXT_REF
));
239 static ssize_t
ad7291_store_ext_ref(struct device
*dev
,
240 struct device_attribute
*attr
,
244 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
245 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
249 command
= chip
->command
& (~AD7291_EXT_REF
);
250 if (strcmp(buf
, "1"))
251 command
|= AD7291_EXT_REF
;
253 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
257 chip
->command
= command
;
262 static IIO_DEVICE_ATTR(ext_ref
, S_IRUGO
| S_IWUSR
,
264 ad7291_store_ext_ref
,
267 static ssize_t
ad7291_show_noise_delay(struct device
*dev
,
268 struct device_attribute
*attr
,
271 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
272 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
274 return sprintf(buf
, "%d\n", !!(chip
->command
& AD7291_NOISE_DELAY
));
277 static ssize_t
ad7291_store_noise_delay(struct device
*dev
,
278 struct device_attribute
*attr
,
282 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
283 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
287 command
= chip
->command
& (~AD7291_NOISE_DELAY
);
288 if (strcmp(buf
, "1"))
289 command
|= AD7291_NOISE_DELAY
;
291 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
295 chip
->command
= command
;
300 static IIO_DEVICE_ATTR(noise_delay
, S_IRUGO
| S_IWUSR
,
301 ad7291_show_noise_delay
,
302 ad7291_store_noise_delay
,
305 static ssize_t
ad7291_show_t_sense(struct device
*dev
,
306 struct device_attribute
*attr
,
309 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
310 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
315 ret
= ad7291_i2c_read_data(chip
, AD7291_T_SENSE
, &data
);
319 if (data
& AD7291_T_VALUE_SIGN
) {
320 /* convert supplement to positive value */
321 data
= (AD7291_T_VALUE_SIGN
<< 1) - data
;
325 return sprintf(buf
, "%c%d.%.2d\n", sign
,
326 (data
>> AD7291_T_VALUE_FLOAT_OFFSET
),
327 (data
& AD7291_T_VALUE_FLOAT_MASK
) * 25);
330 static IIO_DEVICE_ATTR(t_sense
, S_IRUGO
, ad7291_show_t_sense
, NULL
, 0);
332 static ssize_t
ad7291_show_t_average(struct device
*dev
,
333 struct device_attribute
*attr
,
336 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
337 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
342 ret
= ad7291_i2c_read_data(chip
, AD7291_T_AVERAGE
, &data
);
346 if (data
& AD7291_T_VALUE_SIGN
) {
347 /* convert supplement to positive value */
348 data
= (AD7291_T_VALUE_SIGN
<< 1) - data
;
352 return sprintf(buf
, "%c%d.%.2d\n", sign
,
353 (data
>> AD7291_T_VALUE_FLOAT_OFFSET
),
354 (data
& AD7291_T_VALUE_FLOAT_MASK
) * 25);
357 static IIO_DEVICE_ATTR(t_average
, S_IRUGO
, ad7291_show_t_average
, NULL
, 0);
359 static ssize_t
ad7291_show_voltage(struct device
*dev
,
360 struct device_attribute
*attr
,
363 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
364 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
365 u16 data
[AD7291_VOLTAGE_LIMIT_COUNT
];
368 ret
= ad7291_i2c_read_data(chip
, AD7291_VOLTAGE
, data
);
372 for (i
= 0; i
< AD7291_VOLTAGE_LIMIT_COUNT
; i
++) {
373 if (chip
->command
& (AD7291_T_SENSE_MASK
<< i
)) {
374 ret
= sprintf(buf
, "channel[%d]=%d\n", i
,
375 data
[i
] & AD7291_VALUE_MASK
);
386 static IIO_DEVICE_ATTR(voltage
, S_IRUGO
, ad7291_show_voltage
, NULL
, 0);
388 static ssize_t
ad7291_show_channel_mask(struct device
*dev
,
389 struct device_attribute
*attr
,
392 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
393 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
395 return sprintf(buf
, "0x%x\n", (chip
->command
& AD7291_VOLTAGE_MASK
) >>
396 AD7291_VOLTAGE_OFFSET
);
399 static ssize_t
ad7291_store_channel_mask(struct device
*dev
,
400 struct device_attribute
*attr
,
404 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
405 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
410 ret
= strict_strtoul(buf
, 16, &data
);
411 if (ret
|| data
> 0xff)
414 command
= chip
->command
& (~AD7291_VOLTAGE_MASK
);
415 command
|= data
<< AD7291_VOLTAGE_OFFSET
;
417 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
421 chip
->command
= command
;
423 for (i
= 0, chip
->channels
= 0; i
< AD7291_VOLTAGE_LIMIT_COUNT
; i
++) {
424 if (chip
->command
& (AD7291_T_SENSE_MASK
<< i
))
431 static IIO_DEVICE_ATTR(channel_mask
, S_IRUGO
| S_IWUSR
,
432 ad7291_show_channel_mask
,
433 ad7291_store_channel_mask
,
436 static struct attribute
*ad7291_attributes
[] = {
437 &iio_dev_attr_available_modes
.dev_attr
.attr
,
438 &iio_dev_attr_mode
.dev_attr
.attr
,
439 &iio_dev_attr_reset
.dev_attr
.attr
,
440 &iio_dev_attr_ext_ref
.dev_attr
.attr
,
441 &iio_dev_attr_noise_delay
.dev_attr
.attr
,
442 &iio_dev_attr_t_sense
.dev_attr
.attr
,
443 &iio_dev_attr_t_average
.dev_attr
.attr
,
444 &iio_dev_attr_voltage
.dev_attr
.attr
,
445 &iio_dev_attr_channel_mask
.dev_attr
.attr
,
449 static const struct attribute_group ad7291_attribute_group
= {
450 .attrs
= ad7291_attributes
,
454 * temperature bound events
457 #define IIO_EVENT_CODE_AD7291_T_SENSE_HIGH IIO_BUFFER_EVENT_CODE(0)
458 #define IIO_EVENT_CODE_AD7291_T_SENSE_LOW IIO_BUFFER_EVENT_CODE(1)
459 #define IIO_EVENT_CODE_AD7291_T_AVG_HIGH IIO_BUFFER_EVENT_CODE(2)
460 #define IIO_EVENT_CODE_AD7291_T_AVG_LOW IIO_BUFFER_EVENT_CODE(3)
461 #define IIO_EVENT_CODE_AD7291_VOLTAGE_BASE IIO_BUFFER_EVENT_CODE(4)
463 static irqreturn_t
ad7291_event_handler(int irq
, void *private)
465 struct iio_dev
*indio_dev
= private;
466 struct ad7291_chip_info
*chip
= iio_dev_get_devdata(private);
467 u16 t_status
, v_status
;
470 s64 timestamp
= iio_get_time_ns();
472 if (ad7291_i2c_read(chip
, AD7291_T_ALERT_STATUS
, &t_status
))
475 if (ad7291_i2c_read(chip
, AD7291_VOLTAGE_ALERT_STATUS
, &v_status
))
478 if (!(t_status
|| v_status
))
481 command
= chip
->command
| AD7291_ALART_CLEAR
;
482 ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
484 command
= chip
->command
& ~AD7291_ALART_CLEAR
;
485 ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
487 for (i
= 0; i
< 4; i
++) {
488 if (t_status
& (1 << i
))
489 iio_push_event(indio_dev
, 0,
490 IIO_EVENT_CODE_AD7291_T_SENSE_HIGH
+ i
,
494 for (i
= 0; i
< AD7291_VOLTAGE_LIMIT_COUNT
*2; i
++) {
495 if (v_status
& (1 << i
))
496 iio_push_event(indio_dev
, 0,
497 IIO_EVENT_CODE_AD7291_VOLTAGE_BASE
+ i
,
504 static inline ssize_t
ad7291_show_t_bound(struct device
*dev
,
505 struct device_attribute
*attr
,
508 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
509 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
510 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
515 ret
= ad7291_i2c_read(chip
, this_attr
->address
, &data
);
519 data
&= AD7291_VALUE_MASK
;
520 if (data
& AD7291_T_VALUE_SIGN
) {
521 /* convert supplement to positive value */
522 data
= (AD7291_T_VALUE_SIGN
<< 1) - data
;
526 return sprintf(buf
, "%c%d.%.2d\n", sign
,
527 data
>> AD7291_T_VALUE_FLOAT_OFFSET
,
528 (data
& AD7291_T_VALUE_FLOAT_MASK
) * 25);
531 static inline ssize_t
ad7291_set_t_bound(struct device
*dev
,
532 struct device_attribute
*attr
,
536 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
537 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
538 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
544 pos
= strchr(buf
, '.');
546 ret
= strict_strtol(buf
, 10, &tmp1
);
548 if (ret
|| tmp1
> 127 || tmp1
< -128)
553 if (len
> AD7291_T_VALUE_FLOAT_OFFSET
)
554 len
= AD7291_T_VALUE_FLOAT_OFFSET
;
556 ret
= strict_strtol(pos
, 10, &tmp2
);
559 tmp2
= (tmp2
/ 25) * 25;
566 data
= (data
<< AD7291_T_VALUE_FLOAT_OFFSET
) |
567 (tmp2
& AD7291_T_VALUE_FLOAT_MASK
);
569 /* convert positive value to supplyment */
570 data
= (AD7291_T_VALUE_SIGN
<< 1) - data
;
572 ret
= ad7291_i2c_write(chip
, this_attr
->address
, data
);
579 static inline ssize_t
ad7291_show_v_bound(struct device
*dev
,
580 struct device_attribute
*attr
,
584 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
585 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
589 if (bound_reg
< AD7291_VOLTAGE_LIMIT_BASE
||
590 bound_reg
>= AD7291_VOLTAGE_LIMIT_BASE
+
591 AD7291_VOLTAGE_LIMIT_COUNT
)
594 ret
= ad7291_i2c_read(chip
, bound_reg
, &data
);
598 data
&= AD7291_VALUE_MASK
;
600 return sprintf(buf
, "%d\n", data
);
603 static inline ssize_t
ad7291_set_v_bound(struct device
*dev
,
604 struct device_attribute
*attr
,
609 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
610 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
615 if (bound_reg
< AD7291_VOLTAGE_LIMIT_BASE
||
616 bound_reg
>= AD7291_VOLTAGE_LIMIT_BASE
+
617 AD7291_VOLTAGE_LIMIT_COUNT
)
620 ret
= strict_strtoul(buf
, 10, &value
);
622 if (ret
|| value
>= 4096)
626 ret
= ad7291_i2c_write(chip
, bound_reg
, data
);
633 static IIO_DEVICE_ATTR(t_sense_high_value
,
635 ad7291_show_t_bound
, ad7291_set_t_bound
,
636 AD7291_T_SENSE_HIGH
);
637 static IIO_DEVICE_ATTR(t_sense_low_value
,
639 ad7291_show_t_bound
, ad7291_set_t_bound
,
641 static IIO_DEVICE_ATTR(t_sense_hyst_value
,
643 ad7291_show_t_bound
, ad7291_set_t_bound
,
644 AD7291_T_SENSE_HYST
);
645 static IIO_DEVICE_ATTR(v0_high
,
647 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x04);
648 static IIO_DEVICE_ATTR(v0_low
,
650 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x05);
651 static IIO_DEVICE_ATTR(v0_hyst
,
653 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x06);
654 static IIO_DEVICE_ATTR(v1_high
,
656 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x07);
657 static IIO_DEVICE_ATTR(v1_low
,
659 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x08);
660 static IIO_DEVICE_ATTR(v1_hyst
,
662 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x09);
663 static IIO_DEVICE_ATTR(v2_high
,
665 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0A);
666 static IIO_DEVICE_ATTR(v2_low
,
668 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0B);
669 static IIO_DEVICE_ATTR(v2_hyst
,
671 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0C);
672 static IIO_DEVICE_ATTR(v3_high
,
674 /* Datasheet suggests this one and this one only
675 has the registers in different order */
676 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0E);
677 static IIO_DEVICE_ATTR(v3_low
,
679 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0D);
680 static IIO_DEVICE_ATTR(v3_hyst
,
682 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0F);
683 static IIO_DEVICE_ATTR(v4_high
,
685 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x10);
686 static IIO_DEVICE_ATTR(v4_low
,
688 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x11);
689 static IIO_DEVICE_ATTR(v4_hyst
,
691 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x12);
692 static IIO_DEVICE_ATTR(v5_high
,
694 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x13);
695 static IIO_DEVICE_ATTR(v5_low
,
697 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x14);
698 static IIO_DEVICE_ATTR(v5_hyst
,
700 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x15);
701 static IIO_DEVICE_ATTR(v6_high
,
703 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x16);
704 static IIO_DEVICE_ATTR(v6_low
,
706 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x17);
707 static IIO_DEVICE_ATTR(v6_hyst
,
709 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x18);
710 static IIO_DEVICE_ATTR(v7_high
,
712 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x19);
713 static IIO_DEVICE_ATTR(v7_low
,
715 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x1A);
716 static IIO_DEVICE_ATTR(v7_hyst
,
718 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x1B);
720 static struct attribute
*ad7291_event_attributes
[] = {
721 &iio_dev_attr_t_sense_high_value
.dev_attr
.attr
,
722 &iio_dev_attr_t_sense_low_value
.dev_attr
.attr
,
723 &iio_dev_attr_t_sense_hyst_value
.dev_attr
.attr
,
724 &iio_dev_attr_v0_high
.dev_attr
.attr
,
725 &iio_dev_attr_v0_low
.dev_attr
.attr
,
726 &iio_dev_attr_v0_hyst
.dev_attr
.attr
,
727 &iio_dev_attr_v1_high
.dev_attr
.attr
,
728 &iio_dev_attr_v1_low
.dev_attr
.attr
,
729 &iio_dev_attr_v1_hyst
.dev_attr
.attr
,
730 &iio_dev_attr_v2_high
.dev_attr
.attr
,
731 &iio_dev_attr_v2_low
.dev_attr
.attr
,
732 &iio_dev_attr_v2_hyst
.dev_attr
.attr
,
733 &iio_dev_attr_v3_high
.dev_attr
.attr
,
734 &iio_dev_attr_v3_low
.dev_attr
.attr
,
735 &iio_dev_attr_v3_hyst
.dev_attr
.attr
,
736 &iio_dev_attr_v4_high
.dev_attr
.attr
,
737 &iio_dev_attr_v4_low
.dev_attr
.attr
,
738 &iio_dev_attr_v4_hyst
.dev_attr
.attr
,
739 &iio_dev_attr_v5_high
.dev_attr
.attr
,
740 &iio_dev_attr_v5_low
.dev_attr
.attr
,
741 &iio_dev_attr_v5_hyst
.dev_attr
.attr
,
742 &iio_dev_attr_v6_high
.dev_attr
.attr
,
743 &iio_dev_attr_v6_low
.dev_attr
.attr
,
744 &iio_dev_attr_v6_hyst
.dev_attr
.attr
,
745 &iio_dev_attr_v7_high
.dev_attr
.attr
,
746 &iio_dev_attr_v7_low
.dev_attr
.attr
,
747 &iio_dev_attr_v7_hyst
.dev_attr
.attr
,
751 static struct attribute_group ad7291_event_attribute_group
= {
752 .attrs
= ad7291_event_attributes
,
756 * device probe and remove
759 static int __devinit
ad7291_probe(struct i2c_client
*client
,
760 const struct i2c_device_id
*id
)
762 struct ad7291_chip_info
*chip
;
765 chip
= kzalloc(sizeof(struct ad7291_chip_info
), GFP_KERNEL
);
770 /* this is only used for device removal purposes */
771 i2c_set_clientdata(client
, chip
);
773 chip
->client
= client
;
774 chip
->command
= AD7291_NOISE_DELAY
| AD7291_T_SENSE_MASK
;
776 chip
->indio_dev
= iio_allocate_device(0);
777 if (chip
->indio_dev
== NULL
) {
779 goto error_free_chip
;
782 chip
->indio_dev
->name
= id
->name
;
783 chip
->indio_dev
->dev
.parent
= &client
->dev
;
784 chip
->indio_dev
->attrs
= &ad7291_attribute_group
;
785 chip
->indio_dev
->event_attrs
= &ad7291_event_attribute_group
;
786 chip
->indio_dev
->dev_data
= (void *)chip
;
787 chip
->indio_dev
->driver_module
= THIS_MODULE
;
788 chip
->indio_dev
->num_interrupt_lines
= 1;
789 chip
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
791 ret
= iio_device_register(chip
->indio_dev
);
795 if (client
->irq
> 0) {
796 ret
= request_threaded_irq(client
->irq
,
798 &ad7291_event_handler
,
799 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
803 goto error_unreg_dev
;
805 /* set irq polarity low level */
806 chip
->command
|= AD7291_ALART_POLARITY
;
809 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, chip
->command
);
812 goto error_unreg_irq
;
815 dev_info(&client
->dev
, "%s temperature sensor registered.\n",
821 free_irq(client
->irq
, chip
->indio_dev
);
823 iio_device_unregister(chip
->indio_dev
);
825 iio_free_device(chip
->indio_dev
);
832 static int __devexit
ad7291_remove(struct i2c_client
*client
)
834 struct ad7291_chip_info
*chip
= i2c_get_clientdata(client
);
835 struct iio_dev
*indio_dev
= chip
->indio_dev
;
838 free_irq(client
->irq
, chip
->indio_dev
);
839 iio_device_unregister(indio_dev
);
840 iio_free_device(chip
->indio_dev
);
846 static const struct i2c_device_id ad7291_id
[] = {
851 MODULE_DEVICE_TABLE(i2c
, ad7291_id
);
853 static struct i2c_driver ad7291_driver
= {
857 .probe
= ad7291_probe
,
858 .remove
= __devexit_p(ad7291_remove
),
859 .id_table
= ad7291_id
,
862 static __init
int ad7291_init(void)
864 return i2c_add_driver(&ad7291_driver
);
867 static __exit
void ad7291_exit(void)
869 i2c_del_driver(&ad7291_driver
);
872 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
873 MODULE_DESCRIPTION("Analog Devices AD7291 digital"
874 " temperature sensor driver");
875 MODULE_LICENSE("GPL v2");
877 module_init(ad7291_init
);
878 module_exit(ad7291_exit
);