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
;
65 u8 channels
; /* Active voltage channels */
69 * struct ad7291_chip_info - chip specifc information
72 struct ad7291_limit_regs
{
79 * ad7291 register access by I2C
81 static int ad7291_i2c_read(struct ad7291_chip_info
*chip
, u8 reg
, u16
*data
)
83 struct i2c_client
*client
= chip
->client
;
86 ret
= i2c_smbus_read_word_data(client
, reg
);
88 dev_err(&client
->dev
, "I2C read error\n");
92 *data
= swab16((u16
)ret
);
97 static int ad7291_i2c_write(struct ad7291_chip_info
*chip
, u8 reg
, u16 data
)
99 struct i2c_client
*client
= chip
->client
;
102 ret
= i2c_smbus_write_word_data(client
, reg
, swab16(data
));
104 dev_err(&client
->dev
, "I2C write error\n");
109 /* Returns negative errno, or else the number of words read. */
110 static int ad7291_i2c_read_data(struct ad7291_chip_info
*chip
, u8 reg
, u16
*data
)
112 struct i2c_client
*client
= chip
->client
;
117 if (reg
== AD7291_T_SENSE
|| reg
== AD7291_T_AVERAGE
)
119 else if (reg
== AD7291_VOLTAGE
) {
120 if (!chip
->channels
) {
121 dev_err(&client
->dev
, "No voltage channel is selected.\n");
124 count
= 2 + chip
->channels
* 2;
126 dev_err(&client
->dev
, "I2C wrong data register\n");
131 commands
[1] = (chip
->command
>> 8) & 0xff;
132 commands
[2] = chip
->command
& 0xff;
135 ret
= i2c_master_send(client
, commands
, 4);
137 dev_err(&client
->dev
, "I2C master send error\n");
141 ret
= i2c_master_recv(client
, (u8
*)data
, count
);
143 dev_err(&client
->dev
, "I2C master receive error\n");
148 for (i
= 0; i
< ret
; i
++)
149 data
[i
] = swab16(data
[i
]);
154 static ssize_t
ad7291_show_mode(struct device
*dev
,
155 struct device_attribute
*attr
,
158 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
159 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
161 if (chip
->command
& AD7291_AUTOCYCLE
)
162 return sprintf(buf
, "autocycle\n");
164 return sprintf(buf
, "command\n");
167 static ssize_t
ad7291_store_mode(struct device
*dev
,
168 struct device_attribute
*attr
,
172 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
173 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
177 command
= chip
->command
& (~AD7291_AUTOCYCLE
);
178 if (strcmp(buf
, "autocycle"))
179 command
|= AD7291_AUTOCYCLE
;
181 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
185 chip
->command
= command
;
190 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
195 static ssize_t
ad7291_show_available_modes(struct device
*dev
,
196 struct device_attribute
*attr
,
199 return sprintf(buf
, "command\nautocycle\n");
202 static IIO_DEVICE_ATTR(available_modes
, S_IRUGO
, ad7291_show_available_modes
, NULL
, 0);
204 static ssize_t
ad7291_store_reset(struct device
*dev
,
205 struct device_attribute
*attr
,
209 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
210 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
214 command
= chip
->command
| AD7291_RESET
;
216 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
223 static IIO_DEVICE_ATTR(reset
, S_IWUSR
,
228 static ssize_t
ad7291_show_ext_ref(struct device
*dev
,
229 struct device_attribute
*attr
,
232 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
233 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
235 return sprintf(buf
, "%d\n", !!(chip
->command
& AD7291_EXT_REF
));
238 static ssize_t
ad7291_store_ext_ref(struct device
*dev
,
239 struct device_attribute
*attr
,
243 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
244 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
248 command
= chip
->command
& (~AD7291_EXT_REF
);
249 if (strcmp(buf
, "1"))
250 command
|= AD7291_EXT_REF
;
252 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
256 chip
->command
= command
;
261 static IIO_DEVICE_ATTR(ext_ref
, S_IRUGO
| S_IWUSR
,
263 ad7291_store_ext_ref
,
266 static ssize_t
ad7291_show_noise_delay(struct device
*dev
,
267 struct device_attribute
*attr
,
270 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
271 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
273 return sprintf(buf
, "%d\n", !!(chip
->command
& AD7291_NOISE_DELAY
));
276 static ssize_t
ad7291_store_noise_delay(struct device
*dev
,
277 struct device_attribute
*attr
,
281 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
282 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
286 command
= chip
->command
& (~AD7291_NOISE_DELAY
);
287 if (strcmp(buf
, "1"))
288 command
|= AD7291_NOISE_DELAY
;
290 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
294 chip
->command
= command
;
299 static IIO_DEVICE_ATTR(noise_delay
, S_IRUGO
| S_IWUSR
,
300 ad7291_show_noise_delay
,
301 ad7291_store_noise_delay
,
304 static ssize_t
ad7291_show_t_sense(struct device
*dev
,
305 struct device_attribute
*attr
,
308 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
309 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
314 ret
= ad7291_i2c_read_data(chip
, AD7291_T_SENSE
, &data
);
318 if (data
& AD7291_T_VALUE_SIGN
) {
319 /* convert supplement to positive value */
320 data
= (AD7291_T_VALUE_SIGN
<< 1) - data
;
324 return sprintf(buf
, "%c%d.%.2d\n", sign
,
325 (data
>> AD7291_T_VALUE_FLOAT_OFFSET
),
326 (data
& AD7291_T_VALUE_FLOAT_MASK
) * 25);
329 static IIO_DEVICE_ATTR(t_sense
, S_IRUGO
, ad7291_show_t_sense
, NULL
, 0);
331 static ssize_t
ad7291_show_t_average(struct device
*dev
,
332 struct device_attribute
*attr
,
335 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
336 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
341 ret
= ad7291_i2c_read_data(chip
, AD7291_T_AVERAGE
, &data
);
345 if (data
& AD7291_T_VALUE_SIGN
) {
346 /* convert supplement to positive value */
347 data
= (AD7291_T_VALUE_SIGN
<< 1) - data
;
351 return sprintf(buf
, "%c%d.%.2d\n", sign
,
352 (data
>> AD7291_T_VALUE_FLOAT_OFFSET
),
353 (data
& AD7291_T_VALUE_FLOAT_MASK
) * 25);
356 static IIO_DEVICE_ATTR(t_average
, S_IRUGO
, ad7291_show_t_average
, NULL
, 0);
358 static ssize_t
ad7291_show_voltage(struct device
*dev
,
359 struct device_attribute
*attr
,
362 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
363 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
364 u16 data
[AD7291_VOLTAGE_LIMIT_COUNT
];
367 ret
= ad7291_i2c_read_data(chip
, AD7291_VOLTAGE
, data
);
371 for (i
= 0; i
< AD7291_VOLTAGE_LIMIT_COUNT
; i
++) {
372 if (chip
->command
& (AD7291_T_SENSE_MASK
<< i
)) {
373 ret
= sprintf(buf
, "channel[%d]=%d\n", i
,
374 data
[i
] & AD7291_VALUE_MASK
);
385 static IIO_DEVICE_ATTR(voltage
, S_IRUGO
, ad7291_show_voltage
, NULL
, 0);
387 static ssize_t
ad7291_show_channel_mask(struct device
*dev
,
388 struct device_attribute
*attr
,
391 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
392 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
394 return sprintf(buf
, "0x%x\n", (chip
->command
& AD7291_VOLTAGE_MASK
) >>
395 AD7291_VOLTAGE_OFFSET
);
398 static ssize_t
ad7291_store_channel_mask(struct device
*dev
,
399 struct device_attribute
*attr
,
403 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
404 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
409 ret
= strict_strtoul(buf
, 16, &data
);
410 if (ret
|| data
> 0xff)
413 command
= chip
->command
& (~AD7291_VOLTAGE_MASK
);
414 command
|= data
<< AD7291_VOLTAGE_OFFSET
;
416 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
420 chip
->command
= command
;
422 for (i
= 0, chip
->channels
= 0; i
< AD7291_VOLTAGE_LIMIT_COUNT
; i
++) {
423 if (chip
->command
& (AD7291_T_SENSE_MASK
<< i
))
430 static IIO_DEVICE_ATTR(channel_mask
, S_IRUGO
| S_IWUSR
,
431 ad7291_show_channel_mask
,
432 ad7291_store_channel_mask
,
435 static struct attribute
*ad7291_attributes
[] = {
436 &iio_dev_attr_available_modes
.dev_attr
.attr
,
437 &iio_dev_attr_mode
.dev_attr
.attr
,
438 &iio_dev_attr_reset
.dev_attr
.attr
,
439 &iio_dev_attr_ext_ref
.dev_attr
.attr
,
440 &iio_dev_attr_noise_delay
.dev_attr
.attr
,
441 &iio_dev_attr_t_sense
.dev_attr
.attr
,
442 &iio_dev_attr_t_average
.dev_attr
.attr
,
443 &iio_dev_attr_voltage
.dev_attr
.attr
,
444 &iio_dev_attr_channel_mask
.dev_attr
.attr
,
448 static const struct attribute_group ad7291_attribute_group
= {
449 .attrs
= ad7291_attributes
,
453 * temperature bound events
456 static irqreturn_t
ad7291_event_handler(int irq
, void *private)
458 struct iio_dev
*indio_dev
= private;
459 struct ad7291_chip_info
*chip
= iio_priv(private);
460 u16 t_status
, v_status
;
463 s64 timestamp
= iio_get_time_ns();
465 if (ad7291_i2c_read(chip
, AD7291_T_ALERT_STATUS
, &t_status
))
468 if (ad7291_i2c_read(chip
, AD7291_VOLTAGE_ALERT_STATUS
, &v_status
))
471 if (!(t_status
|| v_status
))
474 command
= chip
->command
| AD7291_ALART_CLEAR
;
475 ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
477 command
= chip
->command
& ~AD7291_ALART_CLEAR
;
478 ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
480 if (t_status
& (1 << 0))
481 iio_push_event(indio_dev
, 0,
482 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
487 if (t_status
& (1 << 1))
488 iio_push_event(indio_dev
, 0,
489 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
494 if (t_status
& (1 << 2))
495 iio_push_event(indio_dev
, 0,
496 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
501 if (t_status
& (1 << 3))
502 iio_push_event(indio_dev
, 0,
503 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
509 for (i
= 0; i
< AD7291_VOLTAGE_LIMIT_COUNT
*2; i
+= 2) {
510 if (v_status
& (1 << i
))
511 iio_push_event(indio_dev
, 0,
512 IIO_UNMOD_EVENT_CODE(IIO_IN
,
517 if (v_status
& (1 << (i
+ 1)))
518 iio_push_event(indio_dev
, 0,
519 IIO_UNMOD_EVENT_CODE(IIO_IN
,
529 static inline ssize_t
ad7291_show_t_bound(struct device
*dev
,
530 struct device_attribute
*attr
,
533 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
534 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
535 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
540 ret
= ad7291_i2c_read(chip
, this_attr
->address
, &data
);
544 data
&= AD7291_VALUE_MASK
;
545 if (data
& AD7291_T_VALUE_SIGN
) {
546 /* convert supplement to positive value */
547 data
= (AD7291_T_VALUE_SIGN
<< 1) - data
;
551 return sprintf(buf
, "%c%d.%.2d\n", sign
,
552 data
>> AD7291_T_VALUE_FLOAT_OFFSET
,
553 (data
& AD7291_T_VALUE_FLOAT_MASK
) * 25);
556 static inline ssize_t
ad7291_set_t_bound(struct device
*dev
,
557 struct device_attribute
*attr
,
561 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
562 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
563 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
569 pos
= strchr(buf
, '.');
571 ret
= strict_strtol(buf
, 10, &tmp1
);
573 if (ret
|| tmp1
> 127 || tmp1
< -128)
578 if (len
> AD7291_T_VALUE_FLOAT_OFFSET
)
579 len
= AD7291_T_VALUE_FLOAT_OFFSET
;
581 ret
= strict_strtol(pos
, 10, &tmp2
);
584 tmp2
= (tmp2
/ 25) * 25;
591 data
= (data
<< AD7291_T_VALUE_FLOAT_OFFSET
) |
592 (tmp2
& AD7291_T_VALUE_FLOAT_MASK
);
594 /* convert positive value to supplyment */
595 data
= (AD7291_T_VALUE_SIGN
<< 1) - data
;
597 ret
= ad7291_i2c_write(chip
, this_attr
->address
, data
);
604 static inline ssize_t
ad7291_show_v_bound(struct device
*dev
,
605 struct device_attribute
*attr
,
609 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
610 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
614 if (bound_reg
< AD7291_VOLTAGE_LIMIT_BASE
||
615 bound_reg
>= AD7291_VOLTAGE_LIMIT_BASE
+
616 AD7291_VOLTAGE_LIMIT_COUNT
)
619 ret
= ad7291_i2c_read(chip
, bound_reg
, &data
);
623 data
&= AD7291_VALUE_MASK
;
625 return sprintf(buf
, "%d\n", data
);
628 static inline ssize_t
ad7291_set_v_bound(struct device
*dev
,
629 struct device_attribute
*attr
,
634 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
635 struct ad7291_chip_info
*chip
= iio_priv(dev_info
);
640 if (bound_reg
< AD7291_VOLTAGE_LIMIT_BASE
||
641 bound_reg
>= AD7291_VOLTAGE_LIMIT_BASE
+
642 AD7291_VOLTAGE_LIMIT_COUNT
)
645 ret
= strict_strtoul(buf
, 10, &value
);
647 if (ret
|| value
>= 4096)
651 ret
= ad7291_i2c_write(chip
, bound_reg
, data
);
658 static IIO_DEVICE_ATTR(t_sense_high_value
,
660 ad7291_show_t_bound
, ad7291_set_t_bound
,
661 AD7291_T_SENSE_HIGH
);
662 static IIO_DEVICE_ATTR(t_sense_low_value
,
664 ad7291_show_t_bound
, ad7291_set_t_bound
,
666 static IIO_DEVICE_ATTR(t_sense_hyst_value
,
668 ad7291_show_t_bound
, ad7291_set_t_bound
,
669 AD7291_T_SENSE_HYST
);
670 static IIO_DEVICE_ATTR(v0_high
,
672 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x04);
673 static IIO_DEVICE_ATTR(v0_low
,
675 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x05);
676 static IIO_DEVICE_ATTR(v0_hyst
,
678 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x06);
679 static IIO_DEVICE_ATTR(v1_high
,
681 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x07);
682 static IIO_DEVICE_ATTR(v1_low
,
684 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x08);
685 static IIO_DEVICE_ATTR(v1_hyst
,
687 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x09);
688 static IIO_DEVICE_ATTR(v2_high
,
690 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0A);
691 static IIO_DEVICE_ATTR(v2_low
,
693 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0B);
694 static IIO_DEVICE_ATTR(v2_hyst
,
696 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0C);
697 static IIO_DEVICE_ATTR(v3_high
,
699 /* Datasheet suggests this one and this one only
700 has the registers in different order */
701 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0E);
702 static IIO_DEVICE_ATTR(v3_low
,
704 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0D);
705 static IIO_DEVICE_ATTR(v3_hyst
,
707 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0F);
708 static IIO_DEVICE_ATTR(v4_high
,
710 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x10);
711 static IIO_DEVICE_ATTR(v4_low
,
713 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x11);
714 static IIO_DEVICE_ATTR(v4_hyst
,
716 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x12);
717 static IIO_DEVICE_ATTR(v5_high
,
719 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x13);
720 static IIO_DEVICE_ATTR(v5_low
,
722 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x14);
723 static IIO_DEVICE_ATTR(v5_hyst
,
725 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x15);
726 static IIO_DEVICE_ATTR(v6_high
,
728 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x16);
729 static IIO_DEVICE_ATTR(v6_low
,
731 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x17);
732 static IIO_DEVICE_ATTR(v6_hyst
,
734 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x18);
735 static IIO_DEVICE_ATTR(v7_high
,
737 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x19);
738 static IIO_DEVICE_ATTR(v7_low
,
740 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x1A);
741 static IIO_DEVICE_ATTR(v7_hyst
,
743 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x1B);
745 static struct attribute
*ad7291_event_attributes
[] = {
746 &iio_dev_attr_t_sense_high_value
.dev_attr
.attr
,
747 &iio_dev_attr_t_sense_low_value
.dev_attr
.attr
,
748 &iio_dev_attr_t_sense_hyst_value
.dev_attr
.attr
,
749 &iio_dev_attr_v0_high
.dev_attr
.attr
,
750 &iio_dev_attr_v0_low
.dev_attr
.attr
,
751 &iio_dev_attr_v0_hyst
.dev_attr
.attr
,
752 &iio_dev_attr_v1_high
.dev_attr
.attr
,
753 &iio_dev_attr_v1_low
.dev_attr
.attr
,
754 &iio_dev_attr_v1_hyst
.dev_attr
.attr
,
755 &iio_dev_attr_v2_high
.dev_attr
.attr
,
756 &iio_dev_attr_v2_low
.dev_attr
.attr
,
757 &iio_dev_attr_v2_hyst
.dev_attr
.attr
,
758 &iio_dev_attr_v3_high
.dev_attr
.attr
,
759 &iio_dev_attr_v3_low
.dev_attr
.attr
,
760 &iio_dev_attr_v3_hyst
.dev_attr
.attr
,
761 &iio_dev_attr_v4_high
.dev_attr
.attr
,
762 &iio_dev_attr_v4_low
.dev_attr
.attr
,
763 &iio_dev_attr_v4_hyst
.dev_attr
.attr
,
764 &iio_dev_attr_v5_high
.dev_attr
.attr
,
765 &iio_dev_attr_v5_low
.dev_attr
.attr
,
766 &iio_dev_attr_v5_hyst
.dev_attr
.attr
,
767 &iio_dev_attr_v6_high
.dev_attr
.attr
,
768 &iio_dev_attr_v6_low
.dev_attr
.attr
,
769 &iio_dev_attr_v6_hyst
.dev_attr
.attr
,
770 &iio_dev_attr_v7_high
.dev_attr
.attr
,
771 &iio_dev_attr_v7_low
.dev_attr
.attr
,
772 &iio_dev_attr_v7_hyst
.dev_attr
.attr
,
776 static struct attribute_group ad7291_event_attribute_group
= {
777 .attrs
= ad7291_event_attributes
,
780 static const struct iio_info ad7291_info
= {
781 .attrs
= &ad7291_attribute_group
,
782 .num_interrupt_lines
= 1,
783 .event_attrs
= &ad7291_event_attribute_group
,
787 * device probe and remove
790 static int __devinit
ad7291_probe(struct i2c_client
*client
,
791 const struct i2c_device_id
*id
)
793 struct ad7291_chip_info
*chip
;
794 struct iio_dev
*indio_dev
;
797 indio_dev
= iio_allocate_device(sizeof(*chip
));
798 if (indio_dev
== NULL
) {
802 chip
= iio_priv(indio_dev
);
803 /* this is only used for device removal purposes */
804 i2c_set_clientdata(client
, indio_dev
);
806 chip
->client
= client
;
807 chip
->command
= AD7291_NOISE_DELAY
| AD7291_T_SENSE_MASK
;
809 indio_dev
->name
= id
->name
;
810 indio_dev
->dev
.parent
= &client
->dev
;
811 indio_dev
->info
= &ad7291_info
;
812 indio_dev
->modes
= INDIO_DIRECT_MODE
;
814 ret
= iio_device_register(indio_dev
);
818 if (client
->irq
> 0) {
819 ret
= request_threaded_irq(client
->irq
,
821 &ad7291_event_handler
,
822 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
826 goto error_unreg_dev
;
828 /* set irq polarity low level */
829 chip
->command
|= AD7291_ALART_POLARITY
;
832 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, chip
->command
);
835 goto error_unreg_irq
;
838 dev_info(&client
->dev
, "%s temperature sensor registered.\n",
844 free_irq(client
->irq
, indio_dev
);
846 iio_device_unregister(indio_dev
);
848 iio_free_device(indio_dev
);
853 static int __devexit
ad7291_remove(struct i2c_client
*client
)
855 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
858 free_irq(client
->irq
, indio_dev
);
859 iio_device_unregister(indio_dev
);
860 iio_free_device(indio_dev
);
865 static const struct i2c_device_id ad7291_id
[] = {
870 MODULE_DEVICE_TABLE(i2c
, ad7291_id
);
872 static struct i2c_driver ad7291_driver
= {
876 .probe
= ad7291_probe
,
877 .remove
= __devexit_p(ad7291_remove
),
878 .id_table
= ad7291_id
,
881 static __init
int ad7291_init(void)
883 return i2c_add_driver(&ad7291_driver
);
886 static __exit
void ad7291_exit(void)
888 i2c_del_driver(&ad7291_driver
);
891 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
892 MODULE_DESCRIPTION("Analog Devices AD7291 digital"
893 " temperature sensor driver");
894 MODULE_LICENSE("GPL v2");
896 module_init(ad7291_init
);
897 module_exit(ad7291_exit
);