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
{
82 struct spi_device
*spi_dev
;
83 struct iio_dev
*indio_dev
;
88 * adt7310 register access by SPI
91 static int adt7310_spi_read_word(struct adt7310_chip_info
*chip
, u8 reg
, u16
*data
)
93 struct spi_device
*spi_dev
= chip
->spi_dev
;
94 u8 command
= (reg
<< ADT7310_CMD_REG_OFFSET
) & ADT7310_CMD_REG_MASK
;
97 command
|= ADT7310_CMD_READ
;
98 ret
= spi_write(spi_dev
, &command
, sizeof(command
));
100 dev_err(&spi_dev
->dev
, "SPI write command error\n");
104 ret
= spi_read(spi_dev
, (u8
*)data
, sizeof(*data
));
106 dev_err(&spi_dev
->dev
, "SPI read word error\n");
110 *data
= be16_to_cpu(*data
);
115 static int adt7310_spi_write_word(struct adt7310_chip_info
*chip
, u8 reg
, u16 data
)
117 struct spi_device
*spi_dev
= chip
->spi_dev
;
121 buf
[0] = (reg
<< ADT7310_CMD_REG_OFFSET
) & ADT7310_CMD_REG_MASK
;
122 buf
[1] = (u8
)(data
>> 8);
123 buf
[2] = (u8
)(data
& 0xFF);
125 ret
= spi_write(spi_dev
, buf
, 3);
127 dev_err(&spi_dev
->dev
, "SPI write word error\n");
134 static int adt7310_spi_read_byte(struct adt7310_chip_info
*chip
, u8 reg
, u8
*data
)
136 struct spi_device
*spi_dev
= chip
->spi_dev
;
137 u8 command
= (reg
<< ADT7310_CMD_REG_OFFSET
) & ADT7310_CMD_REG_MASK
;
140 command
|= ADT7310_CMD_READ
;
141 ret
= spi_write(spi_dev
, &command
, sizeof(command
));
143 dev_err(&spi_dev
->dev
, "SPI write command error\n");
147 ret
= spi_read(spi_dev
, data
, sizeof(*data
));
149 dev_err(&spi_dev
->dev
, "SPI read byte error\n");
156 static int adt7310_spi_write_byte(struct adt7310_chip_info
*chip
, u8 reg
, u8 data
)
158 struct spi_device
*spi_dev
= chip
->spi_dev
;
162 buf
[0] = (reg
<< ADT7310_CMD_REG_OFFSET
) & ADT7310_CMD_REG_MASK
;
165 ret
= spi_write(spi_dev
, buf
, 2);
167 dev_err(&spi_dev
->dev
, "SPI write byte error\n");
174 static ssize_t
adt7310_show_mode(struct device
*dev
,
175 struct device_attribute
*attr
,
178 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
179 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
182 config
= chip
->config
& ADT7310_MODE_MASK
;
186 return sprintf(buf
, "power-down\n");
187 case ADT7310_ONESHOT
:
188 return sprintf(buf
, "one-shot\n");
190 return sprintf(buf
, "sps\n");
192 return sprintf(buf
, "full\n");
196 static ssize_t
adt7310_store_mode(struct device
*dev
,
197 struct device_attribute
*attr
,
201 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
202 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
206 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
210 config
= chip
->config
& (~ADT7310_MODE_MASK
);
211 if (strcmp(buf
, "power-down"))
212 config
|= ADT7310_PD
;
213 else if (strcmp(buf
, "one-shot"))
214 config
|= ADT7310_ONESHOT
;
215 else if (strcmp(buf
, "sps"))
216 config
|= ADT7310_SPS
;
218 ret
= adt7310_spi_write_byte(chip
, ADT7310_CONFIG
, config
);
222 chip
->config
= config
;
227 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
232 static ssize_t
adt7310_show_available_modes(struct device
*dev
,
233 struct device_attribute
*attr
,
236 return sprintf(buf
, "full\none-shot\nsps\npower-down\n");
239 static IIO_DEVICE_ATTR(available_modes
, S_IRUGO
, adt7310_show_available_modes
, NULL
, 0);
241 static ssize_t
adt7310_show_resolution(struct device
*dev
,
242 struct device_attribute
*attr
,
245 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
246 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
250 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
254 if (chip
->config
& ADT7310_RESOLUTION
)
259 return sprintf(buf
, "%d bits\n", bits
);
262 static ssize_t
adt7310_store_resolution(struct device
*dev
,
263 struct device_attribute
*attr
,
267 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
268 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
273 ret
= strict_strtoul(buf
, 10, &data
);
277 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
281 config
= chip
->config
& (~ADT7310_RESOLUTION
);
283 config
|= ADT7310_RESOLUTION
;
285 ret
= adt7310_spi_write_byte(chip
, ADT7310_CONFIG
, config
);
289 chip
->config
= config
;
294 static IIO_DEVICE_ATTR(resolution
, S_IRUGO
| S_IWUSR
,
295 adt7310_show_resolution
,
296 adt7310_store_resolution
,
299 static ssize_t
adt7310_show_id(struct device
*dev
,
300 struct device_attribute
*attr
,
303 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
304 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
308 ret
= adt7310_spi_read_byte(chip
, ADT7310_ID
, &id
);
312 return sprintf(buf
, "device id: 0x%x\nmanufactory id: 0x%x\n",
313 id
& ADT7310_DEVICE_ID_MASK
,
314 (id
& ADT7310_MANUFACTORY_ID_MASK
) >> ADT7310_MANUFACTORY_ID_OFFSET
);
317 static IIO_DEVICE_ATTR(id
, S_IRUGO
| S_IWUSR
,
322 static ssize_t
adt7310_convert_temperature(struct adt7310_chip_info
*chip
,
327 if (chip
->config
& ADT7310_RESOLUTION
) {
328 if (data
& ADT7310_T16_VALUE_SIGN
) {
329 /* convert supplement to positive value */
330 data
= (u16
)((ADT7310_T16_VALUE_SIGN
<< 1) - (u32
)data
);
333 return sprintf(buf
, "%c%d.%.7d\n", sign
,
334 (data
>> ADT7310_T16_VALUE_FLOAT_OFFSET
),
335 (data
& ADT7310_T16_VALUE_FLOAT_MASK
) * 78125);
337 if (data
& ADT7310_T13_VALUE_SIGN
) {
338 /* convert supplement to positive value */
339 data
>>= ADT7310_T13_VALUE_OFFSET
;
340 data
= (ADT7310_T13_VALUE_SIGN
<< 1) - data
;
343 return sprintf(buf
, "%c%d.%.4d\n", sign
,
344 (data
>> ADT7310_T13_VALUE_FLOAT_OFFSET
),
345 (data
& ADT7310_T13_VALUE_FLOAT_MASK
) * 625);
349 static ssize_t
adt7310_show_value(struct device
*dev
,
350 struct device_attribute
*attr
,
353 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
354 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
360 ret
= adt7310_spi_read_byte(chip
, ADT7310_STATUS
, &status
);
366 } while (status
& ADT7310_STAT_NOT_RDY
);
368 ret
= adt7310_spi_read_word(chip
, ADT7310_TEMPERATURE
, &data
);
372 return adt7310_convert_temperature(chip
, data
, buf
);
375 static IIO_DEVICE_ATTR(value
, S_IRUGO
, adt7310_show_value
, NULL
, 0);
377 static struct attribute
*adt7310_attributes
[] = {
378 &iio_dev_attr_available_modes
.dev_attr
.attr
,
379 &iio_dev_attr_mode
.dev_attr
.attr
,
380 &iio_dev_attr_resolution
.dev_attr
.attr
,
381 &iio_dev_attr_id
.dev_attr
.attr
,
382 &iio_dev_attr_value
.dev_attr
.attr
,
386 static const struct attribute_group adt7310_attribute_group
= {
387 .attrs
= adt7310_attributes
,
391 * temperature bound events
394 #define IIO_EVENT_CODE_ADT7310_ABOVE_ALARM IIO_BUFFER_EVENT_CODE(0)
395 #define IIO_EVENT_CODE_ADT7310_BELLOW_ALARM IIO_BUFFER_EVENT_CODE(1)
396 #define IIO_EVENT_CODE_ADT7310_ABOVE_CRIT IIO_BUFFER_EVENT_CODE(2)
398 static irqreturn_t
adt7310_event_handler(int irq
, void *private)
400 struct iio_dev
*indio_dev
= private;
401 struct adt7310_chip_info
*chip
= iio_dev_get_devdata(indio_dev
);
402 s64 timestamp
= iio_get_time_ns();
406 ret
= adt7310_spi_read_byte(chip
, ADT7310_STATUS
, &status
);
410 if (status
& ADT7310_STAT_T_HIGH
)
411 iio_push_event(indio_dev
, 0,
412 IIO_EVENT_CODE_ADT7310_ABOVE_ALARM
,
414 if (status
& ADT7310_STAT_T_LOW
)
415 iio_push_event(indio_dev
, 0,
416 IIO_EVENT_CODE_ADT7310_BELLOW_ALARM
,
418 if (status
& ADT7310_STAT_T_CRIT
)
419 iio_push_event(indio_dev
, 0,
420 IIO_EVENT_CODE_ADT7310_ABOVE_CRIT
,
425 static ssize_t
adt7310_show_event_mode(struct device
*dev
,
426 struct device_attribute
*attr
,
429 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
430 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
433 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
437 if (chip
->config
& ADT7310_EVENT_MODE
)
438 return sprintf(buf
, "interrupt\n");
440 return sprintf(buf
, "comparator\n");
443 static ssize_t
adt7310_set_event_mode(struct device
*dev
,
444 struct device_attribute
*attr
,
448 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
449 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
453 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
457 config
= chip
->config
&= ~ADT7310_EVENT_MODE
;
458 if (strcmp(buf
, "comparator") != 0)
459 config
|= ADT7310_EVENT_MODE
;
461 ret
= adt7310_spi_write_byte(chip
, ADT7310_CONFIG
, config
);
465 chip
->config
= config
;
470 static ssize_t
adt7310_show_available_event_modes(struct device
*dev
,
471 struct device_attribute
*attr
,
474 return sprintf(buf
, "comparator\ninterrupt\n");
477 static ssize_t
adt7310_show_fault_queue(struct device
*dev
,
478 struct device_attribute
*attr
,
481 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
482 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
485 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
489 return sprintf(buf
, "%d\n", chip
->config
& ADT7310_FAULT_QUEUE_MASK
);
492 static ssize_t
adt7310_set_fault_queue(struct device
*dev
,
493 struct device_attribute
*attr
,
497 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
498 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
503 ret
= strict_strtoul(buf
, 10, &data
);
507 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
511 config
= chip
->config
& ~ADT7310_FAULT_QUEUE_MASK
;
513 ret
= adt7310_spi_write_byte(chip
, ADT7310_CONFIG
, config
);
517 chip
->config
= config
;
522 static inline ssize_t
adt7310_show_t_bound(struct device
*dev
,
523 struct device_attribute
*attr
,
527 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
528 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
532 ret
= adt7310_spi_read_word(chip
, bound_reg
, &data
);
536 return adt7310_convert_temperature(chip
, data
, buf
);
539 static inline ssize_t
adt7310_set_t_bound(struct device
*dev
,
540 struct device_attribute
*attr
,
545 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
546 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
552 pos
= strchr(buf
, '.');
554 ret
= strict_strtol(buf
, 10, &tmp1
);
556 if (ret
|| tmp1
> 127 || tmp1
< -128)
562 if (chip
->config
& ADT7310_RESOLUTION
) {
563 if (len
> ADT7310_T16_VALUE_FLOAT_OFFSET
)
564 len
= ADT7310_T16_VALUE_FLOAT_OFFSET
;
566 ret
= strict_strtol(pos
, 10, &tmp2
);
569 tmp2
= (tmp2
/ 78125) * 78125;
571 if (len
> ADT7310_T13_VALUE_FLOAT_OFFSET
)
572 len
= ADT7310_T13_VALUE_FLOAT_OFFSET
;
574 ret
= strict_strtol(pos
, 10, &tmp2
);
577 tmp2
= (tmp2
/ 625) * 625;
586 if (chip
->config
& ADT7310_RESOLUTION
) {
587 data
= (data
<< ADT7310_T16_VALUE_FLOAT_OFFSET
) |
588 (tmp2
& ADT7310_T16_VALUE_FLOAT_MASK
);
591 /* convert positive value to supplyment */
592 data
= (u16
)((ADT7310_T16_VALUE_SIGN
<< 1) - (u32
)data
);
594 data
= (data
<< ADT7310_T13_VALUE_FLOAT_OFFSET
) |
595 (tmp2
& ADT7310_T13_VALUE_FLOAT_MASK
);
598 /* convert positive value to supplyment */
599 data
= (ADT7310_T13_VALUE_SIGN
<< 1) - data
;
600 data
<<= ADT7310_T13_VALUE_OFFSET
;
603 ret
= adt7310_spi_write_word(chip
, bound_reg
, data
);
610 static ssize_t
adt7310_show_t_alarm_high(struct device
*dev
,
611 struct device_attribute
*attr
,
614 return adt7310_show_t_bound(dev
, attr
,
615 ADT7310_T_ALARM_HIGH
, buf
);
618 static inline ssize_t
adt7310_set_t_alarm_high(struct device
*dev
,
619 struct device_attribute
*attr
,
623 return adt7310_set_t_bound(dev
, attr
,
624 ADT7310_T_ALARM_HIGH
, buf
, len
);
627 static ssize_t
adt7310_show_t_alarm_low(struct device
*dev
,
628 struct device_attribute
*attr
,
631 return adt7310_show_t_bound(dev
, attr
,
632 ADT7310_T_ALARM_LOW
, buf
);
635 static inline ssize_t
adt7310_set_t_alarm_low(struct device
*dev
,
636 struct device_attribute
*attr
,
640 return adt7310_set_t_bound(dev
, attr
,
641 ADT7310_T_ALARM_LOW
, buf
, len
);
644 static ssize_t
adt7310_show_t_crit(struct device
*dev
,
645 struct device_attribute
*attr
,
648 return adt7310_show_t_bound(dev
, attr
,
649 ADT7310_T_CRIT
, buf
);
652 static inline ssize_t
adt7310_set_t_crit(struct device
*dev
,
653 struct device_attribute
*attr
,
657 return adt7310_set_t_bound(dev
, attr
,
658 ADT7310_T_CRIT
, buf
, len
);
661 static ssize_t
adt7310_show_t_hyst(struct device
*dev
,
662 struct device_attribute
*attr
,
665 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
666 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
670 ret
= adt7310_spi_read_byte(chip
, ADT7310_T_HYST
, &t_hyst
);
674 return sprintf(buf
, "%d\n", t_hyst
& ADT7310_T_HYST_MASK
);
677 static inline ssize_t
adt7310_set_t_hyst(struct device
*dev
,
678 struct device_attribute
*attr
,
682 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
683 struct adt7310_chip_info
*chip
= dev_info
->dev_data
;
688 ret
= strict_strtol(buf
, 10, &data
);
690 if (ret
|| data
> ADT7310_T_HYST_MASK
)
695 ret
= adt7310_spi_write_byte(chip
, ADT7310_T_HYST
, t_hyst
);
702 static IIO_DEVICE_ATTR(event_mode
,
704 adt7310_show_event_mode
, adt7310_set_event_mode
, 0);
705 static IIO_DEVICE_ATTR(available_event_modes
,
707 adt7310_show_available_event_modes
, NULL
, 0);
708 static IIO_DEVICE_ATTR(fault_queue
,
710 adt7310_show_fault_queue
, adt7310_set_fault_queue
, 0);
711 static IIO_DEVICE_ATTR(t_alarm_high
,
713 adt7310_show_t_alarm_high
, adt7310_set_t_alarm_high
, 0);
714 static IIO_DEVICE_ATTR(t_alarm_low
,
716 adt7310_show_t_alarm_low
, adt7310_set_t_alarm_low
, 0);
717 static IIO_DEVICE_ATTR(t_crit
,
719 adt7310_show_t_crit
, adt7310_set_t_crit
, 0);
720 static IIO_DEVICE_ATTR(t_hyst
,
722 adt7310_show_t_hyst
, adt7310_set_t_hyst
, 0);
724 static struct attribute
*adt7310_event_int_attributes
[] = {
725 &iio_dev_attr_event_mode
.dev_attr
.attr
,
726 &iio_dev_attr_available_event_modes
.dev_attr
.attr
,
727 &iio_dev_attr_fault_queue
.dev_attr
.attr
,
728 &iio_dev_attr_t_alarm_high
.dev_attr
.attr
,
729 &iio_dev_attr_t_alarm_low
.dev_attr
.attr
,
730 &iio_dev_attr_t_hyst
.dev_attr
.attr
,
734 static struct attribute
*adt7310_event_ct_attributes
[] = {
735 &iio_dev_attr_event_mode
.dev_attr
.attr
,
736 &iio_dev_attr_available_event_modes
.dev_attr
.attr
,
737 &iio_dev_attr_fault_queue
.dev_attr
.attr
,
738 &iio_dev_attr_t_crit
.dev_attr
.attr
,
739 &iio_dev_attr_t_hyst
.dev_attr
.attr
,
743 static struct attribute_group adt7310_event_attribute_group
[ADT7310_IRQS
] = {
745 .attrs
= adt7310_event_int_attributes
,
747 .attrs
= adt7310_event_ct_attributes
,
752 * device probe and remove
755 static int __devinit
adt7310_probe(struct spi_device
*spi_dev
)
757 struct adt7310_chip_info
*chip
;
759 unsigned long *adt7310_platform_data
= spi_dev
->dev
.platform_data
;
760 unsigned long irq_flags
;
762 chip
= kzalloc(sizeof(struct adt7310_chip_info
), GFP_KERNEL
);
767 /* this is only used for device removal purposes */
768 dev_set_drvdata(&spi_dev
->dev
, chip
);
770 chip
->spi_dev
= spi_dev
;
772 chip
->indio_dev
= iio_allocate_device(0);
773 if (chip
->indio_dev
== NULL
) {
775 goto error_free_chip
;
778 chip
->indio_dev
->dev
.parent
= &spi_dev
->dev
;
779 chip
->indio_dev
->name
= spi_get_device_id(spi_dev
)->name
;
780 chip
->indio_dev
->attrs
= &adt7310_attribute_group
;
781 chip
->indio_dev
->event_attrs
= adt7310_event_attribute_group
;
782 chip
->indio_dev
->dev_data
= (void *)chip
;
783 chip
->indio_dev
->driver_module
= THIS_MODULE
;
784 chip
->indio_dev
->num_interrupt_lines
= ADT7310_IRQS
;
785 chip
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
787 ret
= iio_device_register(chip
->indio_dev
);
791 /* CT critcal temperature event. line 0 */
793 if (adt7310_platform_data
[2])
794 irq_flags
= adt7310_platform_data
[2];
796 irq_flags
= IRQF_TRIGGER_LOW
;
797 ret
= request_threaded_irq(spi_dev
->irq
,
799 &adt7310_event_handler
,
801 chip
->indio_dev
->name
,
804 goto error_unreg_dev
;
807 /* INT bound temperature alarm event. line 1 */
808 if (adt7310_platform_data
[0]) {
809 ret
= request_threaded_irq(adt7310_platform_data
[0],
811 &adt7310_event_handler
,
812 adt7310_platform_data
[1],
813 chip
->indio_dev
->name
,
816 goto error_unreg_ct_irq
;
819 if (spi_dev
->irq
&& adt7310_platform_data
[0]) {
820 ret
= adt7310_spi_read_byte(chip
, ADT7310_CONFIG
, &chip
->config
);
823 goto error_unreg_int_irq
;
826 /* set irq polarity low level */
827 chip
->config
&= ~ADT7310_CT_POLARITY
;
829 if (adt7310_platform_data
[1] & IRQF_TRIGGER_HIGH
)
830 chip
->config
|= ADT7310_INT_POLARITY
;
832 chip
->config
&= ~ADT7310_INT_POLARITY
;
834 ret
= adt7310_spi_write_byte(chip
, ADT7310_CONFIG
, chip
->config
);
837 goto error_unreg_int_irq
;
841 dev_info(&spi_dev
->dev
, "%s temperature sensor registered.\n",
842 chip
->indio_dev
->name
);
847 free_irq(adt7310_platform_data
[0], chip
->indio_dev
);
849 free_irq(spi_dev
->irq
, chip
->indio_dev
);
851 iio_device_unregister(chip
->indio_dev
);
853 iio_free_device(chip
->indio_dev
);
860 static int __devexit
adt7310_remove(struct spi_device
*spi_dev
)
862 struct adt7310_chip_info
*chip
= dev_get_drvdata(&spi_dev
->dev
);
863 struct iio_dev
*indio_dev
= chip
->indio_dev
;
864 unsigned long *adt7310_platform_data
= spi_dev
->dev
.platform_data
;
866 dev_set_drvdata(&spi_dev
->dev
, NULL
);
867 if (adt7310_platform_data
[0])
868 free_irq(adt7310_platform_data
[0], chip
->indio_dev
);
870 free_irq(spi_dev
->irq
, chip
->indio_dev
);
871 iio_device_unregister(indio_dev
);
872 iio_free_device(chip
->indio_dev
);
878 static const struct spi_device_id adt7310_id
[] = {
883 MODULE_DEVICE_TABLE(spi
, adt7310_id
);
885 static struct spi_driver adt7310_driver
= {
888 .bus
= &spi_bus_type
,
889 .owner
= THIS_MODULE
,
891 .probe
= adt7310_probe
,
892 .remove
= __devexit_p(adt7310_remove
),
893 .id_table
= adt7310_id
,
896 static __init
int adt7310_init(void)
898 return spi_register_driver(&adt7310_driver
);
901 static __exit
void adt7310_exit(void)
903 spi_unregister_driver(&adt7310_driver
);
906 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
907 MODULE_DESCRIPTION("Analog Devices ADT7310 digital"
908 " temperature sensor driver");
909 MODULE_LICENSE("GPL v2");
911 module_init(adt7310_init
);
912 module_exit(adt7310_exit
);