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
{
64 struct i2c_client
*client
;
65 struct iio_dev
*indio_dev
;
67 u8 channels
; /* Active voltage channels */
71 * struct ad7291_chip_info - chip specifc information
74 struct ad7291_limit_regs
{
81 * ad7291 register access by I2C
83 static int ad7291_i2c_read(struct ad7291_chip_info
*chip
, u8 reg
, u16
*data
)
85 struct i2c_client
*client
= chip
->client
;
88 ret
= i2c_smbus_read_word_data(client
, reg
);
90 dev_err(&client
->dev
, "I2C read error\n");
94 *data
= swab16((u16
)ret
);
99 static int ad7291_i2c_write(struct ad7291_chip_info
*chip
, u8 reg
, u16 data
)
101 struct i2c_client
*client
= chip
->client
;
104 ret
= i2c_smbus_write_word_data(client
, reg
, swab16(data
));
106 dev_err(&client
->dev
, "I2C write error\n");
111 /* Returns negative errno, or else the number of words read. */
112 static int ad7291_i2c_read_data(struct ad7291_chip_info
*chip
, u8 reg
, u16
*data
)
114 struct i2c_client
*client
= chip
->client
;
119 if (reg
== AD7291_T_SENSE
|| reg
== AD7291_T_AVERAGE
)
121 else if (reg
== AD7291_VOLTAGE
) {
122 if (!chip
->channels
) {
123 dev_err(&client
->dev
, "No voltage channel is selected.\n");
126 count
= 2 + chip
->channels
* 2;
128 dev_err(&client
->dev
, "I2C wrong data register\n");
133 commands
[1] = (chip
->command
>> 8) & 0xff;
134 commands
[2] = chip
->command
& 0xff;
137 ret
= i2c_master_send(client
, commands
, 4);
139 dev_err(&client
->dev
, "I2C master send error\n");
143 ret
= i2c_master_recv(client
, (u8
*)data
, count
);
145 dev_err(&client
->dev
, "I2C master receive error\n");
150 for (i
= 0; i
< ret
; i
++)
151 data
[i
] = swab16(data
[i
]);
156 static ssize_t
ad7291_show_mode(struct device
*dev
,
157 struct device_attribute
*attr
,
160 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
161 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
163 if (chip
->command
& AD7291_AUTOCYCLE
)
164 return sprintf(buf
, "autocycle\n");
166 return sprintf(buf
, "command\n");
169 static ssize_t
ad7291_store_mode(struct device
*dev
,
170 struct device_attribute
*attr
,
174 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
175 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
179 command
= chip
->command
& (~AD7291_AUTOCYCLE
);
180 if (strcmp(buf
, "autocycle"))
181 command
|= AD7291_AUTOCYCLE
;
183 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
187 chip
->command
= command
;
192 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
197 static ssize_t
ad7291_show_available_modes(struct device
*dev
,
198 struct device_attribute
*attr
,
201 return sprintf(buf
, "command\nautocycle\n");
204 static IIO_DEVICE_ATTR(available_modes
, S_IRUGO
, ad7291_show_available_modes
, NULL
, 0);
206 static ssize_t
ad7291_store_reset(struct device
*dev
,
207 struct device_attribute
*attr
,
211 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
212 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
216 command
= chip
->command
| AD7291_RESET
;
218 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
225 static IIO_DEVICE_ATTR(reset
, S_IWUSR
,
230 static ssize_t
ad7291_show_ext_ref(struct device
*dev
,
231 struct device_attribute
*attr
,
234 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
235 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
237 return sprintf(buf
, "%d\n", !!(chip
->command
& AD7291_EXT_REF
));
240 static ssize_t
ad7291_store_ext_ref(struct device
*dev
,
241 struct device_attribute
*attr
,
245 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
246 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
250 command
= chip
->command
& (~AD7291_EXT_REF
);
251 if (strcmp(buf
, "1"))
252 command
|= AD7291_EXT_REF
;
254 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
258 chip
->command
= command
;
263 static IIO_DEVICE_ATTR(ext_ref
, S_IRUGO
| S_IWUSR
,
265 ad7291_store_ext_ref
,
268 static ssize_t
ad7291_show_noise_delay(struct device
*dev
,
269 struct device_attribute
*attr
,
272 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
273 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
275 return sprintf(buf
, "%d\n", !!(chip
->command
& AD7291_NOISE_DELAY
));
278 static ssize_t
ad7291_store_noise_delay(struct device
*dev
,
279 struct device_attribute
*attr
,
283 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
284 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
288 command
= chip
->command
& (~AD7291_NOISE_DELAY
);
289 if (strcmp(buf
, "1"))
290 command
|= AD7291_NOISE_DELAY
;
292 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
296 chip
->command
= command
;
301 static IIO_DEVICE_ATTR(noise_delay
, S_IRUGO
| S_IWUSR
,
302 ad7291_show_noise_delay
,
303 ad7291_store_noise_delay
,
306 static ssize_t
ad7291_show_t_sense(struct device
*dev
,
307 struct device_attribute
*attr
,
310 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
311 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
316 ret
= ad7291_i2c_read_data(chip
, AD7291_T_SENSE
, &data
);
320 if (data
& AD7291_T_VALUE_SIGN
) {
321 /* convert supplement to positive value */
322 data
= (AD7291_T_VALUE_SIGN
<< 1) - data
;
326 return sprintf(buf
, "%c%d.%.2d\n", sign
,
327 (data
>> AD7291_T_VALUE_FLOAT_OFFSET
),
328 (data
& AD7291_T_VALUE_FLOAT_MASK
) * 25);
331 static IIO_DEVICE_ATTR(t_sense
, S_IRUGO
, ad7291_show_t_sense
, NULL
, 0);
333 static ssize_t
ad7291_show_t_average(struct device
*dev
,
334 struct device_attribute
*attr
,
337 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
338 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
343 ret
= ad7291_i2c_read_data(chip
, AD7291_T_AVERAGE
, &data
);
347 if (data
& AD7291_T_VALUE_SIGN
) {
348 /* convert supplement to positive value */
349 data
= (AD7291_T_VALUE_SIGN
<< 1) - data
;
353 return sprintf(buf
, "%c%d.%.2d\n", sign
,
354 (data
>> AD7291_T_VALUE_FLOAT_OFFSET
),
355 (data
& AD7291_T_VALUE_FLOAT_MASK
) * 25);
358 static IIO_DEVICE_ATTR(t_average
, S_IRUGO
, ad7291_show_t_average
, NULL
, 0);
360 static ssize_t
ad7291_show_voltage(struct device
*dev
,
361 struct device_attribute
*attr
,
364 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
365 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
366 u16 data
[AD7291_VOLTAGE_LIMIT_COUNT
];
369 ret
= ad7291_i2c_read_data(chip
, AD7291_VOLTAGE
, data
);
373 for (i
= 0; i
< AD7291_VOLTAGE_LIMIT_COUNT
; i
++) {
374 if (chip
->command
& (AD7291_T_SENSE_MASK
<< i
)) {
375 ret
= sprintf(buf
, "channel[%d]=%d\n", i
,
376 data
[i
] & AD7291_VALUE_MASK
);
387 static IIO_DEVICE_ATTR(voltage
, S_IRUGO
, ad7291_show_voltage
, NULL
, 0);
389 static ssize_t
ad7291_show_channel_mask(struct device
*dev
,
390 struct device_attribute
*attr
,
393 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
394 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
396 return sprintf(buf
, "0x%x\n", (chip
->command
& AD7291_VOLTAGE_MASK
) >>
397 AD7291_VOLTAGE_OFFSET
);
400 static ssize_t
ad7291_store_channel_mask(struct device
*dev
,
401 struct device_attribute
*attr
,
405 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
406 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
411 ret
= strict_strtoul(buf
, 16, &data
);
412 if (ret
|| data
> 0xff)
415 command
= chip
->command
& (~AD7291_VOLTAGE_MASK
);
416 command
|= data
<< AD7291_VOLTAGE_OFFSET
;
418 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
422 chip
->command
= command
;
424 for (i
= 0, chip
->channels
= 0; i
< AD7291_VOLTAGE_LIMIT_COUNT
; i
++) {
425 if (chip
->command
& (AD7291_T_SENSE_MASK
<< i
))
432 static IIO_DEVICE_ATTR(channel_mask
, S_IRUGO
| S_IWUSR
,
433 ad7291_show_channel_mask
,
434 ad7291_store_channel_mask
,
437 static ssize_t
ad7291_show_name(struct device
*dev
,
438 struct device_attribute
*attr
,
441 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
442 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
443 return sprintf(buf
, "%s\n", chip
->name
);
446 static IIO_DEVICE_ATTR(name
, S_IRUGO
, ad7291_show_name
, NULL
, 0);
448 static struct attribute
*ad7291_attributes
[] = {
449 &iio_dev_attr_available_modes
.dev_attr
.attr
,
450 &iio_dev_attr_mode
.dev_attr
.attr
,
451 &iio_dev_attr_reset
.dev_attr
.attr
,
452 &iio_dev_attr_ext_ref
.dev_attr
.attr
,
453 &iio_dev_attr_noise_delay
.dev_attr
.attr
,
454 &iio_dev_attr_t_sense
.dev_attr
.attr
,
455 &iio_dev_attr_t_average
.dev_attr
.attr
,
456 &iio_dev_attr_voltage
.dev_attr
.attr
,
457 &iio_dev_attr_channel_mask
.dev_attr
.attr
,
458 &iio_dev_attr_name
.dev_attr
.attr
,
462 static const struct attribute_group ad7291_attribute_group
= {
463 .attrs
= ad7291_attributes
,
467 * temperature bound events
470 #define IIO_EVENT_CODE_AD7291_T_SENSE_HIGH IIO_BUFFER_EVENT_CODE(0)
471 #define IIO_EVENT_CODE_AD7291_T_SENSE_LOW IIO_BUFFER_EVENT_CODE(1)
472 #define IIO_EVENT_CODE_AD7291_T_AVG_HIGH IIO_BUFFER_EVENT_CODE(2)
473 #define IIO_EVENT_CODE_AD7291_T_AVG_LOW IIO_BUFFER_EVENT_CODE(3)
474 #define IIO_EVENT_CODE_AD7291_VOLTAGE_BASE IIO_BUFFER_EVENT_CODE(4)
476 static irqreturn_t
ad7291_event_handler(int irq
, void *private)
478 struct iio_dev
*indio_dev
= private;
479 struct ad7291_chip_info
*chip
= iio_dev_get_devdata(private);
480 u16 t_status
, v_status
;
483 s64 timestamp
= iio_get_time_ns();
485 if (ad7291_i2c_read(chip
, AD7291_T_ALERT_STATUS
, &t_status
))
488 if (ad7291_i2c_read(chip
, AD7291_VOLTAGE_ALERT_STATUS
, &v_status
))
491 if (!(t_status
|| v_status
))
494 command
= chip
->command
| AD7291_ALART_CLEAR
;
495 ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
497 command
= chip
->command
& ~AD7291_ALART_CLEAR
;
498 ad7291_i2c_write(chip
, AD7291_COMMAND
, command
);
500 for (i
= 0; i
< 4; i
++) {
501 if (t_status
& (1 << i
))
502 iio_push_event(indio_dev
, 0,
503 IIO_EVENT_CODE_AD7291_T_SENSE_HIGH
+ i
,
507 for (i
= 0; i
< AD7291_VOLTAGE_LIMIT_COUNT
*2; i
++) {
508 if (v_status
& (1 << i
))
509 iio_push_event(indio_dev
, 0,
510 IIO_EVENT_CODE_AD7291_VOLTAGE_BASE
+ i
,
517 static inline ssize_t
ad7291_show_t_bound(struct device
*dev
,
518 struct device_attribute
*attr
,
521 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
522 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
523 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
528 ret
= ad7291_i2c_read(chip
, this_attr
->address
, &data
);
532 data
&= AD7291_VALUE_MASK
;
533 if (data
& AD7291_T_VALUE_SIGN
) {
534 /* convert supplement to positive value */
535 data
= (AD7291_T_VALUE_SIGN
<< 1) - data
;
539 return sprintf(buf
, "%c%d.%.2d\n", sign
,
540 data
>> AD7291_T_VALUE_FLOAT_OFFSET
,
541 (data
& AD7291_T_VALUE_FLOAT_MASK
) * 25);
544 static inline ssize_t
ad7291_set_t_bound(struct device
*dev
,
545 struct device_attribute
*attr
,
549 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
550 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
551 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
557 pos
= strchr(buf
, '.');
559 ret
= strict_strtol(buf
, 10, &tmp1
);
561 if (ret
|| tmp1
> 127 || tmp1
< -128)
566 if (len
> AD7291_T_VALUE_FLOAT_OFFSET
)
567 len
= AD7291_T_VALUE_FLOAT_OFFSET
;
569 ret
= strict_strtol(pos
, 10, &tmp2
);
572 tmp2
= (tmp2
/ 25) * 25;
579 data
= (data
<< AD7291_T_VALUE_FLOAT_OFFSET
) |
580 (tmp2
& AD7291_T_VALUE_FLOAT_MASK
);
582 /* convert positive value to supplyment */
583 data
= (AD7291_T_VALUE_SIGN
<< 1) - data
;
585 ret
= ad7291_i2c_write(chip
, this_attr
->address
, data
);
592 static inline ssize_t
ad7291_show_v_bound(struct device
*dev
,
593 struct device_attribute
*attr
,
597 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
598 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
602 if (bound_reg
< AD7291_VOLTAGE_LIMIT_BASE
||
603 bound_reg
>= AD7291_VOLTAGE_LIMIT_BASE
+
604 AD7291_VOLTAGE_LIMIT_COUNT
)
607 ret
= ad7291_i2c_read(chip
, bound_reg
, &data
);
611 data
&= AD7291_VALUE_MASK
;
613 return sprintf(buf
, "%d\n", data
);
616 static inline ssize_t
ad7291_set_v_bound(struct device
*dev
,
617 struct device_attribute
*attr
,
622 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
623 struct ad7291_chip_info
*chip
= dev_info
->dev_data
;
628 if (bound_reg
< AD7291_VOLTAGE_LIMIT_BASE
||
629 bound_reg
>= AD7291_VOLTAGE_LIMIT_BASE
+
630 AD7291_VOLTAGE_LIMIT_COUNT
)
633 ret
= strict_strtoul(buf
, 10, &value
);
635 if (ret
|| value
>= 4096)
639 ret
= ad7291_i2c_write(chip
, bound_reg
, data
);
646 static IIO_DEVICE_ATTR(t_sense_high_value
,
648 ad7291_show_t_bound
, ad7291_set_t_bound
,
649 AD7291_T_SENSE_HIGH
);
650 static IIO_DEVICE_ATTR(t_sense_low_value
,
652 ad7291_show_t_bound
, ad7291_set_t_bound
,
654 static IIO_DEVICE_ATTR(t_sense_hyst_value
,
656 ad7291_show_t_bound
, ad7291_set_t_bound
,
657 AD7291_T_SENSE_HYST
);
658 static IIO_DEVICE_ATTR(v0_high
,
660 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x04);
661 static IIO_DEVICE_ATTR(v0_low
,
663 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x05);
664 static IIO_DEVICE_ATTR(v0_hyst
,
666 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x06);
667 static IIO_DEVICE_ATTR(v1_high
,
669 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x07);
670 static IIO_DEVICE_ATTR(v1_low
,
672 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x08);
673 static IIO_DEVICE_ATTR(v1_hyst
,
675 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x09);
676 static IIO_DEVICE_ATTR(v2_high
,
678 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0A);
679 static IIO_DEVICE_ATTR(v2_low
,
681 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0B);
682 static IIO_DEVICE_ATTR(v2_hyst
,
684 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0C);
685 static IIO_DEVICE_ATTR(v3_high
,
687 /* Datasheet suggests this one and this one only
688 has the registers in different order */
689 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0E);
690 static IIO_DEVICE_ATTR(v3_low
,
692 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0D);
693 static IIO_DEVICE_ATTR(v3_hyst
,
695 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x0F);
696 static IIO_DEVICE_ATTR(v4_high
,
698 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x10);
699 static IIO_DEVICE_ATTR(v4_low
,
701 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x11);
702 static IIO_DEVICE_ATTR(v4_hyst
,
704 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x12);
705 static IIO_DEVICE_ATTR(v5_high
,
707 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x13);
708 static IIO_DEVICE_ATTR(v5_low
,
710 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x14);
711 static IIO_DEVICE_ATTR(v5_hyst
,
713 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x15);
714 static IIO_DEVICE_ATTR(v6_high
,
716 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x16);
717 static IIO_DEVICE_ATTR(v6_low
,
719 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x17);
720 static IIO_DEVICE_ATTR(v6_hyst
,
722 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x18);
723 static IIO_DEVICE_ATTR(v7_high
,
725 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x19);
726 static IIO_DEVICE_ATTR(v7_low
,
728 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x1A);
729 static IIO_DEVICE_ATTR(v7_hyst
,
731 ad7291_show_t_bound
, ad7291_set_t_bound
, 0x1B);
733 static struct attribute
*ad7291_event_attributes
[] = {
734 &iio_dev_attr_t_sense_high_value
.dev_attr
.attr
,
735 &iio_dev_attr_t_sense_low_value
.dev_attr
.attr
,
736 &iio_dev_attr_t_sense_hyst_value
.dev_attr
.attr
,
737 &iio_dev_attr_v0_high
.dev_attr
.attr
,
738 &iio_dev_attr_v0_low
.dev_attr
.attr
,
739 &iio_dev_attr_v0_hyst
.dev_attr
.attr
,
740 &iio_dev_attr_v1_high
.dev_attr
.attr
,
741 &iio_dev_attr_v1_low
.dev_attr
.attr
,
742 &iio_dev_attr_v1_hyst
.dev_attr
.attr
,
743 &iio_dev_attr_v2_high
.dev_attr
.attr
,
744 &iio_dev_attr_v2_low
.dev_attr
.attr
,
745 &iio_dev_attr_v2_hyst
.dev_attr
.attr
,
746 &iio_dev_attr_v3_high
.dev_attr
.attr
,
747 &iio_dev_attr_v3_low
.dev_attr
.attr
,
748 &iio_dev_attr_v3_hyst
.dev_attr
.attr
,
749 &iio_dev_attr_v4_high
.dev_attr
.attr
,
750 &iio_dev_attr_v4_low
.dev_attr
.attr
,
751 &iio_dev_attr_v4_hyst
.dev_attr
.attr
,
752 &iio_dev_attr_v5_high
.dev_attr
.attr
,
753 &iio_dev_attr_v5_low
.dev_attr
.attr
,
754 &iio_dev_attr_v5_hyst
.dev_attr
.attr
,
755 &iio_dev_attr_v6_high
.dev_attr
.attr
,
756 &iio_dev_attr_v6_low
.dev_attr
.attr
,
757 &iio_dev_attr_v6_hyst
.dev_attr
.attr
,
758 &iio_dev_attr_v7_high
.dev_attr
.attr
,
759 &iio_dev_attr_v7_low
.dev_attr
.attr
,
760 &iio_dev_attr_v7_hyst
.dev_attr
.attr
,
764 static struct attribute_group ad7291_event_attribute_group
= {
765 .attrs
= ad7291_event_attributes
,
769 * device probe and remove
772 static int __devinit
ad7291_probe(struct i2c_client
*client
,
773 const struct i2c_device_id
*id
)
775 struct ad7291_chip_info
*chip
;
778 chip
= kzalloc(sizeof(struct ad7291_chip_info
), GFP_KERNEL
);
783 /* this is only used for device removal purposes */
784 i2c_set_clientdata(client
, chip
);
786 chip
->client
= client
;
787 chip
->name
= id
->name
;
788 chip
->command
= AD7291_NOISE_DELAY
| AD7291_T_SENSE_MASK
;
790 chip
->indio_dev
= iio_allocate_device(0);
791 if (chip
->indio_dev
== NULL
) {
793 goto error_free_chip
;
796 chip
->indio_dev
->dev
.parent
= &client
->dev
;
797 chip
->indio_dev
->attrs
= &ad7291_attribute_group
;
798 chip
->indio_dev
->event_attrs
= &ad7291_event_attribute_group
;
799 chip
->indio_dev
->dev_data
= (void *)chip
;
800 chip
->indio_dev
->driver_module
= THIS_MODULE
;
801 chip
->indio_dev
->num_interrupt_lines
= 1;
802 chip
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
804 ret
= iio_device_register(chip
->indio_dev
);
808 if (client
->irq
> 0) {
809 ret
= request_threaded_irq(client
->irq
,
811 &ad7291_event_handler
,
812 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
816 goto error_unreg_dev
;
818 /* set irq polarity low level */
819 chip
->command
|= AD7291_ALART_POLARITY
;
822 ret
= ad7291_i2c_write(chip
, AD7291_COMMAND
, chip
->command
);
825 goto error_unreg_irq
;
828 dev_info(&client
->dev
, "%s temperature sensor registered.\n",
834 free_irq(client
->irq
, chip
->indio_dev
);
836 iio_device_unregister(chip
->indio_dev
);
838 iio_free_device(chip
->indio_dev
);
845 static int __devexit
ad7291_remove(struct i2c_client
*client
)
847 struct ad7291_chip_info
*chip
= i2c_get_clientdata(client
);
848 struct iio_dev
*indio_dev
= chip
->indio_dev
;
851 free_irq(client
->irq
, chip
->indio_dev
);
852 iio_device_unregister(indio_dev
);
853 iio_free_device(chip
->indio_dev
);
859 static const struct i2c_device_id ad7291_id
[] = {
864 MODULE_DEVICE_TABLE(i2c
, ad7291_id
);
866 static struct i2c_driver ad7291_driver
= {
870 .probe
= ad7291_probe
,
871 .remove
= __devexit_p(ad7291_remove
),
872 .id_table
= ad7291_id
,
875 static __init
int ad7291_init(void)
877 return i2c_add_driver(&ad7291_driver
);
880 static __exit
void ad7291_exit(void)
882 i2c_del_driver(&ad7291_driver
);
885 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
886 MODULE_DESCRIPTION("Analog Devices AD7291 digital"
887 " temperature sensor driver");
888 MODULE_LICENSE("GPL v2");
890 module_init(ad7291_init
);
891 module_exit(ad7291_exit
);