2 * ADT7310 digital temperature sensor driver supporting ADT7310
4 * Copyright 2010 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/list.h>
15 #include <linux/spi/spi.h>
21 * ADT7310 registers definition
24 #define ADT7310_STATUS 0
25 #define ADT7310_CONFIG 1
26 #define ADT7310_TEMPERATURE 2
28 #define ADT7310_T_CRIT 4
29 #define ADT7310_T_HYST 5
30 #define ADT7310_T_ALARM_HIGH 6
31 #define ADT7310_T_ALARM_LOW 7
36 #define ADT7310_STAT_T_LOW 0x10
37 #define ADT7310_STAT_T_HIGH 0x20
38 #define ADT7310_STAT_T_CRIT 0x40
39 #define ADT7310_STAT_NOT_RDY 0x80
44 #define ADT7310_FAULT_QUEUE_MASK 0x3
45 #define ADT7310_CT_POLARITY 0x4
46 #define ADT7310_INT_POLARITY 0x8
47 #define ADT7310_EVENT_MODE 0x10
48 #define ADT7310_MODE_MASK 0x60
49 #define ADT7310_ONESHOT 0x20
50 #define ADT7310_SPS 0x40
51 #define ADT7310_PD 0x60
52 #define ADT7310_RESOLUTION 0x80
57 #define ADT7310_T16_VALUE_SIGN 0x8000
58 #define ADT7310_T16_VALUE_FLOAT_OFFSET 7
59 #define ADT7310_T16_VALUE_FLOAT_MASK 0x7F
60 #define ADT7310_T13_VALUE_SIGN 0x1000
61 #define ADT7310_T13_VALUE_OFFSET 3
62 #define ADT7310_T13_VALUE_FLOAT_OFFSET 4
63 #define ADT7310_T13_VALUE_FLOAT_MASK 0xF
64 #define ADT7310_T_HYST_MASK 0xF
65 #define ADT7310_DEVICE_ID_MASK 0x7
66 #define ADT7310_MANUFACTORY_ID_MASK 0xF8
67 #define ADT7310_MANUFACTORY_ID_OFFSET 3
70 #define ADT7310_CMD_REG_MASK 0x28
71 #define ADT7310_CMD_REG_OFFSET 3
72 #define ADT7310_CMD_READ 0x40
73 #define ADT7310_CMD_CON_READ 0x4
75 #define ADT7310_IRQS 2
78 * struct adt7310_chip_info - chip specifc information
81 struct adt7310_chip_info
{
83 struct spi_device
*spi_dev
;
84 struct iio_dev
*indio_dev
;
89 * adt7310 register access by SPI
92 static int adt7310_spi_read_word(struct adt7310_chip_info
*chip
, u8 reg
, u16
*data
)
94 struct spi_device
*spi_dev
= chip
->spi_dev
;
95 u8 command
= (reg
<< ADT7310_CMD_REG_OFFSET
) & ADT7310_CMD_REG_MASK
;
98 command
|= ADT7310_CMD_READ
;
99 ret
= spi_write(spi_dev
, &command
, sizeof(command
));
101 dev_err(&spi_dev
->dev
, "SPI write command error\n");
105 ret
= spi_read(spi_dev
, (u8
*)data
, sizeof(*data
));
107 dev_err(&spi_dev
->dev
, "SPI read word error\n");
111 *data
= be16_to_cpu(*data
);
116 static int adt7310_spi_write_word(struct adt7310_chip_info
*chip
, u8 reg
, u16 data
)
118 struct spi_device
*spi_dev
= chip
->spi_dev
;
122 buf
[0] = (reg
<< ADT7310_CMD_REG_OFFSET
) & ADT7310_CMD_REG_MASK
;
123 buf
[1] = (u8
)(data
>> 8);
124 buf
[2] = (u8
)(data
& 0xFF);
126 ret
= spi_write(spi_dev
, buf
, 3);
128 dev_err(&spi_dev
->dev
, "SPI write word error\n");
135 static int adt7310_spi_read_byte(struct adt7310_chip_info
*chip
, u8 reg
, u8
*data
)
137 struct spi_device
*spi_dev
= chip
->spi_dev
;
138 u8 command
= (reg
<< ADT7310_CMD_REG_OFFSET
) & ADT7310_CMD_REG_MASK
;
141 command
|= ADT7310_CMD_READ
;
142 ret
= spi_write(spi_dev
, &command
, sizeof(command
));
144 dev_err(&spi_dev
->dev
, "SPI write command error\n");
148 ret
= spi_read(spi_dev
, data
, sizeof(*data
));
150 dev_err(&spi_dev
->dev
, "SPI read byte error\n");
157 static int adt7310_spi_write_byte(struct adt7310_chip_info
*chip
, u8 reg
, u8 data
)
159 struct spi_device
*spi_dev
= chip
->spi_dev
;
163 buf
[0] = (reg
<< ADT7310_CMD_REG_OFFSET
) & ADT7310_CMD_REG_MASK
;
166 ret
= spi_write(spi_dev
, buf
, 2);
168 dev_err(&spi_dev
->dev
, "SPI write byte error\n");
175 static ssize_t
adt7310_show_mode(struct device
*dev
,
176 struct device_attribute
*attr
,
179 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
180 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
183 config
= chip
->config
& ADT7310_MODE_MASK
;
187 return sprintf(buf
, "power-down\n");
188 case ADT7310_ONESHOT
:
189 return sprintf(buf
, "one-shot\n");
191 return sprintf(buf
, "sps\n");
193 return sprintf(buf
, "full\n");
197 static ssize_t
adt7310_store_mode(struct device
*dev
,
198 struct device_attribute
*attr
,
202 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
203 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
207 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
211 config
= chip
->config
& (~ADT7310_MODE_MASK
);
212 if (strcmp(buf
, "power-down"))
213 config
|= ADT7310_PD
;
214 else if (strcmp(buf
, "one-shot"))
215 config
|= ADT7310_ONESHOT
;
216 else if (strcmp(buf
, "sps"))
217 config
|= ADT7310_SPS
;
219 ret
= adt7310_spi_write_byte(chip
, ADT7310_CONFIG
, config
);
223 chip
->config
= config
;
228 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
233 static ssize_t
adt7310_show_available_modes(struct device
*dev
,
234 struct device_attribute
*attr
,
237 return sprintf(buf
, "full\none-shot\nsps\npower-down\n");
240 static IIO_DEVICE_ATTR(available_modes
, S_IRUGO
, adt7310_show_available_modes
, NULL
, 0);
242 static ssize_t
adt7310_show_resolution(struct device
*dev
,
243 struct device_attribute
*attr
,
246 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
247 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
251 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
255 if (chip
->config
& ADT7310_RESOLUTION
)
260 return sprintf(buf
, "%d bits\n", bits
);
263 static ssize_t
adt7310_store_resolution(struct device
*dev
,
264 struct device_attribute
*attr
,
268 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
269 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
274 ret
= strict_strtoul(buf
, 10, &data
);
278 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
282 config
= chip
->config
& (~ADT7310_RESOLUTION
);
284 config
|= ADT7310_RESOLUTION
;
286 ret
= adt7310_spi_write_byte(chip
, ADT7310_CONFIG
, config
);
290 chip
->config
= config
;
295 static IIO_DEVICE_ATTR(resolution
, S_IRUGO
| S_IWUSR
,
296 adt7310_show_resolution
,
297 adt7310_store_resolution
,
300 static ssize_t
adt7310_show_id(struct device
*dev
,
301 struct device_attribute
*attr
,
304 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
305 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
309 ret
= adt7310_spi_read_byte(chip
, ADT7310_ID
, &id
);
313 return sprintf(buf
, "device id: 0x%x\nmanufactory id: 0x%x\n",
314 id
& ADT7310_DEVICE_ID_MASK
,
315 (id
& ADT7310_MANUFACTORY_ID_MASK
) >> ADT7310_MANUFACTORY_ID_OFFSET
);
318 static IIO_DEVICE_ATTR(id
, S_IRUGO
| S_IWUSR
,
323 static ssize_t
adt7310_convert_temperature(struct adt7310_chip_info
*chip
,
328 if (chip
->config
& ADT7310_RESOLUTION
) {
329 if (data
& ADT7310_T16_VALUE_SIGN
) {
330 /* convert supplement to positive value */
331 data
= (u16
)((ADT7310_T16_VALUE_SIGN
<< 1) - (u32
)data
);
334 return sprintf(buf
, "%c%d.%.7d\n", sign
,
335 (data
>> ADT7310_T16_VALUE_FLOAT_OFFSET
),
336 (data
& ADT7310_T16_VALUE_FLOAT_MASK
) * 78125);
338 if (data
& ADT7310_T13_VALUE_SIGN
) {
339 /* convert supplement to positive value */
340 data
>>= ADT7310_T13_VALUE_OFFSET
;
341 data
= (ADT7310_T13_VALUE_SIGN
<< 1) - data
;
344 return sprintf(buf
, "%c%d.%.4d\n", sign
,
345 (data
>> ADT7310_T13_VALUE_FLOAT_OFFSET
),
346 (data
& ADT7310_T13_VALUE_FLOAT_MASK
) * 625);
350 static ssize_t
adt7310_show_value(struct device
*dev
,
351 struct device_attribute
*attr
,
354 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
355 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
361 ret
= adt7310_spi_read_byte(chip
, ADT7310_STATUS
, &status
);
367 } while (status
& ADT7310_STAT_NOT_RDY
);
369 ret
= adt7310_spi_read_word(chip
, ADT7310_TEMPERATURE
, &data
);
373 return adt7310_convert_temperature(chip
, data
, buf
);
376 static IIO_DEVICE_ATTR(value
, S_IRUGO
, adt7310_show_value
, NULL
, 0);
378 static ssize_t
adt7310_show_name(struct device
*dev
,
379 struct device_attribute
*attr
,
382 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
383 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
384 return sprintf(buf
, "%s\n", chip
->name
);
387 static IIO_DEVICE_ATTR(name
, S_IRUGO
, adt7310_show_name
, NULL
, 0);
389 static struct attribute
*adt7310_attributes
[] = {
390 &iio_dev_attr_available_modes
.dev_attr
.attr
,
391 &iio_dev_attr_mode
.dev_attr
.attr
,
392 &iio_dev_attr_resolution
.dev_attr
.attr
,
393 &iio_dev_attr_id
.dev_attr
.attr
,
394 &iio_dev_attr_value
.dev_attr
.attr
,
395 &iio_dev_attr_name
.dev_attr
.attr
,
399 static const struct attribute_group adt7310_attribute_group
= {
400 .attrs
= adt7310_attributes
,
404 * temperature bound events
407 #define IIO_EVENT_CODE_ADT7310_ABOVE_ALARM IIO_BUFFER_EVENT_CODE(0)
408 #define IIO_EVENT_CODE_ADT7310_BELLOW_ALARM IIO_BUFFER_EVENT_CODE(1)
409 #define IIO_EVENT_CODE_ADT7310_ABOVE_CRIT IIO_BUFFER_EVENT_CODE(2)
411 static irqreturn_t
adt7310_event_handler(int irq
, void *private)
413 struct iio_dev
*indio_dev
= private;
414 struct adt7310_chip_info
*chip
= iio_dev_get_devdata(indio_dev
);
415 s64 timestamp
= iio_get_time_ns();
419 ret
= adt7310_spi_read_byte(chip
, ADT7310_STATUS
, &status
);
423 if (status
& ADT7310_STAT_T_HIGH
)
424 iio_push_event(indio_dev
, 0,
425 IIO_EVENT_CODE_ADT7310_ABOVE_ALARM
,
427 if (status
& ADT7310_STAT_T_LOW
)
428 iio_push_event(indio_dev
, 0,
429 IIO_EVENT_CODE_ADT7310_BELLOW_ALARM
,
431 if (status
& ADT7310_STAT_T_CRIT
)
432 iio_push_event(indio_dev
, 0,
433 IIO_EVENT_CODE_ADT7310_ABOVE_CRIT
,
438 static ssize_t
adt7310_show_event_mode(struct device
*dev
,
439 struct device_attribute
*attr
,
442 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
443 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
446 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
450 if (chip
->config
& ADT7310_EVENT_MODE
)
451 return sprintf(buf
, "interrupt\n");
453 return sprintf(buf
, "comparator\n");
456 static ssize_t
adt7310_set_event_mode(struct device
*dev
,
457 struct device_attribute
*attr
,
461 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
462 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
466 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
470 config
= chip
->config
&= ~ADT7310_EVENT_MODE
;
471 if (strcmp(buf
, "comparator") != 0)
472 config
|= ADT7310_EVENT_MODE
;
474 ret
= adt7310_spi_write_byte(chip
, ADT7310_CONFIG
, config
);
478 chip
->config
= config
;
483 static ssize_t
adt7310_show_available_event_modes(struct device
*dev
,
484 struct device_attribute
*attr
,
487 return sprintf(buf
, "comparator\ninterrupt\n");
490 static ssize_t
adt7310_show_fault_queue(struct device
*dev
,
491 struct device_attribute
*attr
,
494 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
495 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
498 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
502 return sprintf(buf
, "%d\n", chip
->config
& ADT7310_FAULT_QUEUE_MASK
);
505 static ssize_t
adt7310_set_fault_queue(struct device
*dev
,
506 struct device_attribute
*attr
,
510 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
511 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
516 ret
= strict_strtoul(buf
, 10, &data
);
520 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
524 config
= chip
->config
& ~ADT7310_FAULT_QUEUE_MASK
;
526 ret
= adt7310_spi_write_byte(chip
, ADT7310_CONFIG
, config
);
530 chip
->config
= config
;
535 static inline ssize_t
adt7310_show_t_bound(struct device
*dev
,
536 struct device_attribute
*attr
,
540 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
541 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
545 ret
= adt7310_spi_read_word(chip
, bound_reg
, &data
);
549 return adt7310_convert_temperature(chip
, data
, buf
);
552 static inline ssize_t
adt7310_set_t_bound(struct device
*dev
,
553 struct device_attribute
*attr
,
558 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
559 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
565 pos
= strchr(buf
, '.');
567 ret
= strict_strtol(buf
, 10, &tmp1
);
569 if (ret
|| tmp1
> 127 || tmp1
< -128)
575 if (chip
->config
& ADT7310_RESOLUTION
) {
576 if (len
> ADT7310_T16_VALUE_FLOAT_OFFSET
)
577 len
= ADT7310_T16_VALUE_FLOAT_OFFSET
;
579 ret
= strict_strtol(pos
, 10, &tmp2
);
582 tmp2
= (tmp2
/ 78125) * 78125;
584 if (len
> ADT7310_T13_VALUE_FLOAT_OFFSET
)
585 len
= ADT7310_T13_VALUE_FLOAT_OFFSET
;
587 ret
= strict_strtol(pos
, 10, &tmp2
);
590 tmp2
= (tmp2
/ 625) * 625;
599 if (chip
->config
& ADT7310_RESOLUTION
) {
600 data
= (data
<< ADT7310_T16_VALUE_FLOAT_OFFSET
) |
601 (tmp2
& ADT7310_T16_VALUE_FLOAT_MASK
);
604 /* convert positive value to supplyment */
605 data
= (u16
)((ADT7310_T16_VALUE_SIGN
<< 1) - (u32
)data
);
607 data
= (data
<< ADT7310_T13_VALUE_FLOAT_OFFSET
) |
608 (tmp2
& ADT7310_T13_VALUE_FLOAT_MASK
);
611 /* convert positive value to supplyment */
612 data
= (ADT7310_T13_VALUE_SIGN
<< 1) - data
;
613 data
<<= ADT7310_T13_VALUE_OFFSET
;
616 ret
= adt7310_spi_write_word(chip
, bound_reg
, data
);
623 static ssize_t
adt7310_show_t_alarm_high(struct device
*dev
,
624 struct device_attribute
*attr
,
627 return adt7310_show_t_bound(dev
, attr
,
628 ADT7310_T_ALARM_HIGH
, buf
);
631 static inline ssize_t
adt7310_set_t_alarm_high(struct device
*dev
,
632 struct device_attribute
*attr
,
636 return adt7310_set_t_bound(dev
, attr
,
637 ADT7310_T_ALARM_HIGH
, buf
, len
);
640 static ssize_t
adt7310_show_t_alarm_low(struct device
*dev
,
641 struct device_attribute
*attr
,
644 return adt7310_show_t_bound(dev
, attr
,
645 ADT7310_T_ALARM_LOW
, buf
);
648 static inline ssize_t
adt7310_set_t_alarm_low(struct device
*dev
,
649 struct device_attribute
*attr
,
653 return adt7310_set_t_bound(dev
, attr
,
654 ADT7310_T_ALARM_LOW
, buf
, len
);
657 static ssize_t
adt7310_show_t_crit(struct device
*dev
,
658 struct device_attribute
*attr
,
661 return adt7310_show_t_bound(dev
, attr
,
662 ADT7310_T_CRIT
, buf
);
665 static inline ssize_t
adt7310_set_t_crit(struct device
*dev
,
666 struct device_attribute
*attr
,
670 return adt7310_set_t_bound(dev
, attr
,
671 ADT7310_T_CRIT
, buf
, len
);
674 static ssize_t
adt7310_show_t_hyst(struct device
*dev
,
675 struct device_attribute
*attr
,
678 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
679 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
683 ret
= adt7310_spi_read_byte(chip
, ADT7310_T_HYST
, &t_hyst
);
687 return sprintf(buf
, "%d\n", t_hyst
& ADT7310_T_HYST_MASK
);
690 static inline ssize_t
adt7310_set_t_hyst(struct device
*dev
,
691 struct device_attribute
*attr
,
695 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
696 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
701 ret
= strict_strtol(buf
, 10, &data
);
703 if (ret
|| data
> ADT7310_T_HYST_MASK
)
708 ret
= adt7310_spi_write_byte(chip
, ADT7310_T_HYST
, t_hyst
);
715 static IIO_DEVICE_ATTR(event_mode
,
717 adt7310_show_event_mode
, adt7310_set_event_mode
, 0);
718 static IIO_DEVICE_ATTR(available_event_modes
,
720 adt7310_show_available_event_modes
, NULL
, 0);
721 static IIO_DEVICE_ATTR(fault_queue
,
723 adt7310_show_fault_queue
, adt7310_set_fault_queue
, 0);
724 static IIO_DEVICE_ATTR(t_alarm_high
,
726 adt7310_show_t_alarm_high
, adt7310_set_t_alarm_high
, 0);
727 static IIO_DEVICE_ATTR(t_alarm_low
,
729 adt7310_show_t_alarm_low
, adt7310_set_t_alarm_low
, 0);
730 static IIO_DEVICE_ATTR(t_crit
,
732 adt7310_show_t_crit
, adt7310_set_t_crit
, 0);
733 static IIO_DEVICE_ATTR(t_hyst
,
735 adt7310_show_t_hyst
, adt7310_set_t_hyst
, 0);
737 static struct attribute
*adt7310_event_int_attributes
[] = {
738 &iio_dev_attr_event_mode
.dev_attr
.attr
,
739 &iio_dev_attr_available_event_modes
.dev_attr
.attr
,
740 &iio_dev_attr_fault_queue
.dev_attr
.attr
,
741 &iio_dev_attr_t_alarm_high
.dev_attr
.attr
,
742 &iio_dev_attr_t_alarm_low
.dev_attr
.attr
,
743 &iio_dev_attr_t_hyst
.dev_attr
.attr
,
747 static struct attribute
*adt7310_event_ct_attributes
[] = {
748 &iio_dev_attr_event_mode
.dev_attr
.attr
,
749 &iio_dev_attr_available_event_modes
.dev_attr
.attr
,
750 &iio_dev_attr_fault_queue
.dev_attr
.attr
,
751 &iio_dev_attr_t_crit
.dev_attr
.attr
,
752 &iio_dev_attr_t_hyst
.dev_attr
.attr
,
756 static struct attribute_group adt7310_event_attribute_group
[ADT7310_IRQS
] = {
758 .attrs
= adt7310_event_int_attributes
,
760 .attrs
= adt7310_event_ct_attributes
,
765 * device probe and remove
768 static int __devinit
adt7310_probe(struct spi_device
*spi_dev
)
770 struct adt7310_chip_info
*chip
;
772 unsigned long *adt7310_platform_data
= spi_dev
->dev
.platform_data
;
773 unsigned long irq_flags
;
775 chip
= kzalloc(sizeof(struct adt7310_chip_info
), GFP_KERNEL
);
780 /* this is only used for device removal purposes */
781 dev_set_drvdata(&spi_dev
->dev
, chip
);
783 chip
->spi_dev
= spi_dev
;
784 chip
->name
= spi_dev
->modalias
;
786 chip
->indio_dev
= iio_allocate_device(0);
787 if (chip
->indio_dev
== NULL
) {
789 goto error_free_chip
;
792 chip
->indio_dev
->dev
.parent
= &spi_dev
->dev
;
793 chip
->indio_dev
->attrs
= &adt7310_attribute_group
;
794 chip
->indio_dev
->event_attrs
= adt7310_event_attribute_group
;
795 chip
->indio_dev
->dev_data
= (void *)chip
;
796 chip
->indio_dev
->driver_module
= THIS_MODULE
;
797 chip
->indio_dev
->num_interrupt_lines
= ADT7310_IRQS
;
798 chip
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
800 ret
= iio_device_register(chip
->indio_dev
);
804 /* CT critcal temperature event. line 0 */
806 if (adt7310_platform_data
[2])
807 irq_flags
= adt7310_platform_data
[2];
809 irq_flags
= IRQF_TRIGGER_LOW
;
810 ret
= request_threaded_irq(spi_dev
->irq
,
812 &adt7310_event_handler
,
817 goto error_unreg_dev
;
820 /* INT bound temperature alarm event. line 1 */
821 if (adt7310_platform_data
[0]) {
822 ret
= request_threaded_irq(adt7310_platform_data
[0],
824 &adt7310_event_handler
,
825 adt7310_platform_data
[1],
829 goto error_unreg_ct_irq
;
832 if (spi_dev
->irq
&& adt7310_platform_data
[0]) {
833 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
836 goto error_unreg_int_irq
;
839 /* set irq polarity low level */
840 chip
->config
&= ~ADT7310_CT_POLARITY
;
842 if (adt7310_platform_data
[1] & IRQF_TRIGGER_HIGH
)
843 chip
->config
|= ADT7310_INT_POLARITY
;
845 chip
->config
&= ~ADT7310_INT_POLARITY
;
847 ret
= adt7310_spi_write_byte(chip
, ADT7310_CONFIG
, chip
->config
);
850 goto error_unreg_int_irq
;
854 dev_info(&spi_dev
->dev
, "%s temperature sensor registered.\n",
860 free_irq(adt7310_platform_data
[0], chip
->indio_dev
);
862 free_irq(spi_dev
->irq
, chip
->indio_dev
);
864 iio_device_unregister(chip
->indio_dev
);
866 iio_free_device(chip
->indio_dev
);
873 static int __devexit
adt7310_remove(struct spi_device
*spi_dev
)
875 struct adt7310_chip_info
*chip
= dev_get_drvdata(&spi_dev
->dev
);
876 struct iio_dev
*indio_dev
= chip
->indio_dev
;
877 unsigned long *adt7310_platform_data
= spi_dev
->dev
.platform_data
;
879 dev_set_drvdata(&spi_dev
->dev
, NULL
);
880 if (adt7310_platform_data
[0])
881 free_irq(adt7310_platform_data
[0], chip
->indio_dev
);
883 free_irq(spi_dev
->irq
, chip
->indio_dev
);
884 iio_device_unregister(indio_dev
);
885 iio_free_device(chip
->indio_dev
);
891 static const struct spi_device_id adt7310_id
[] = {
896 MODULE_DEVICE_TABLE(spi
, adt7310_id
);
898 static struct spi_driver adt7310_driver
= {
901 .bus
= &spi_bus_type
,
902 .owner
= THIS_MODULE
,
904 .probe
= adt7310_probe
,
905 .remove
= __devexit_p(adt7310_remove
),
906 .id_table
= adt7310_id
,
909 static __init
int adt7310_init(void)
911 return spi_register_driver(&adt7310_driver
);
914 static __exit
void adt7310_exit(void)
916 spi_unregister_driver(&adt7310_driver
);
919 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
920 MODULE_DESCRIPTION("Analog Devices ADT7310 digital"
921 " temperature sensor driver");
922 MODULE_LICENSE("GPL v2");
924 module_init(adt7310_init
);
925 module_exit(adt7310_exit
);