2 * ADT7410 digital temperature sensor driver supporting ADT7410
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/i2c.h>
21 * ADT7410 registers definition
24 #define ADT7410_TEMPERATURE 0
25 #define ADT7410_STATUS 2
26 #define ADT7410_CONFIG 3
27 #define ADT7410_T_ALARM_HIGH 4
28 #define ADT7410_T_ALARM_LOW 6
29 #define ADT7410_T_CRIT 8
30 #define ADT7410_T_HYST 0xA
31 #define ADT7410_ID 0xB
32 #define ADT7410_RESET 0x2F
37 #define ADT7410_STAT_T_LOW 0x10
38 #define ADT7410_STAT_T_HIGH 0x20
39 #define ADT7410_STAT_T_CRIT 0x40
40 #define ADT7410_STAT_NOT_RDY 0x80
45 #define ADT7410_FAULT_QUEUE_MASK 0x3
46 #define ADT7410_CT_POLARITY 0x4
47 #define ADT7410_INT_POLARITY 0x8
48 #define ADT7410_EVENT_MODE 0x10
49 #define ADT7410_MODE_MASK 0x60
50 #define ADT7410_ONESHOT 0x20
51 #define ADT7410_SPS 0x40
52 #define ADT7410_PD 0x60
53 #define ADT7410_RESOLUTION 0x80
58 #define ADT7410_T16_VALUE_SIGN 0x8000
59 #define ADT7410_T16_VALUE_FLOAT_OFFSET 7
60 #define ADT7410_T16_VALUE_FLOAT_MASK 0x7F
61 #define ADT7410_T13_VALUE_SIGN 0x1000
62 #define ADT7410_T13_VALUE_OFFSET 3
63 #define ADT7410_T13_VALUE_FLOAT_OFFSET 4
64 #define ADT7410_T13_VALUE_FLOAT_MASK 0xF
65 #define ADT7410_T_HYST_MASK 0xF
66 #define ADT7410_DEVICE_ID_MASK 0xF
67 #define ADT7410_MANUFACTORY_ID_MASK 0xF0
68 #define ADT7410_MANUFACTORY_ID_OFFSET 4
70 #define ADT7410_IRQS 2
73 * struct adt7410_chip_info - chip specifc information
76 struct adt7410_chip_info
{
77 struct i2c_client
*client
;
82 * adt7410 register access by I2C
85 static int adt7410_i2c_read_word(struct adt7410_chip_info
*chip
, u8 reg
, u16
*data
)
87 struct i2c_client
*client
= chip
->client
;
90 ret
= i2c_smbus_read_word_data(client
, reg
);
92 dev_err(&client
->dev
, "I2C read error\n");
96 *data
= swab16((u16
)ret
);
101 static int adt7410_i2c_write_word(struct adt7410_chip_info
*chip
, u8 reg
, u16 data
)
103 struct i2c_client
*client
= chip
->client
;
106 ret
= i2c_smbus_write_word_data(client
, reg
, swab16(data
));
108 dev_err(&client
->dev
, "I2C write error\n");
113 static int adt7410_i2c_read_byte(struct adt7410_chip_info
*chip
, u8 reg
, u8
*data
)
115 struct i2c_client
*client
= chip
->client
;
118 ret
= i2c_smbus_read_byte_data(client
, reg
);
120 dev_err(&client
->dev
, "I2C read error\n");
129 static int adt7410_i2c_write_byte(struct adt7410_chip_info
*chip
, u8 reg
, u8 data
)
131 struct i2c_client
*client
= chip
->client
;
134 ret
= i2c_smbus_write_byte_data(client
, reg
, data
);
136 dev_err(&client
->dev
, "I2C write error\n");
141 static ssize_t
adt7410_show_mode(struct device
*dev
,
142 struct device_attribute
*attr
,
145 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
146 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
149 config
= chip
->config
& ADT7410_MODE_MASK
;
153 return sprintf(buf
, "power-down\n");
154 case ADT7410_ONESHOT
:
155 return sprintf(buf
, "one-shot\n");
157 return sprintf(buf
, "sps\n");
159 return sprintf(buf
, "full\n");
163 static ssize_t
adt7410_store_mode(struct device
*dev
,
164 struct device_attribute
*attr
,
168 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
169 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
173 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
177 config
= chip
->config
& (~ADT7410_MODE_MASK
);
178 if (strcmp(buf
, "power-down"))
179 config
|= ADT7410_PD
;
180 else if (strcmp(buf
, "one-shot"))
181 config
|= ADT7410_ONESHOT
;
182 else if (strcmp(buf
, "sps"))
183 config
|= ADT7410_SPS
;
185 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, config
);
189 chip
->config
= config
;
194 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
199 static ssize_t
adt7410_show_available_modes(struct device
*dev
,
200 struct device_attribute
*attr
,
203 return sprintf(buf
, "full\none-shot\nsps\npower-down\n");
206 static IIO_DEVICE_ATTR(available_modes
, S_IRUGO
, adt7410_show_available_modes
, NULL
, 0);
208 static ssize_t
adt7410_show_resolution(struct device
*dev
,
209 struct device_attribute
*attr
,
212 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
213 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
217 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
221 if (chip
->config
& ADT7410_RESOLUTION
)
226 return sprintf(buf
, "%d bits\n", bits
);
229 static ssize_t
adt7410_store_resolution(struct device
*dev
,
230 struct device_attribute
*attr
,
234 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
235 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
240 ret
= strict_strtoul(buf
, 10, &data
);
244 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
248 config
= chip
->config
& (~ADT7410_RESOLUTION
);
250 config
|= ADT7410_RESOLUTION
;
252 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, config
);
256 chip
->config
= config
;
261 static IIO_DEVICE_ATTR(resolution
, S_IRUGO
| S_IWUSR
,
262 adt7410_show_resolution
,
263 adt7410_store_resolution
,
266 static ssize_t
adt7410_show_id(struct device
*dev
,
267 struct device_attribute
*attr
,
270 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
271 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
275 ret
= adt7410_i2c_read_byte(chip
, ADT7410_ID
, &id
);
279 return sprintf(buf
, "device id: 0x%x\nmanufactory id: 0x%x\n",
280 id
& ADT7410_DEVICE_ID_MASK
,
281 (id
& ADT7410_MANUFACTORY_ID_MASK
) >> ADT7410_MANUFACTORY_ID_OFFSET
);
284 static IIO_DEVICE_ATTR(id
, S_IRUGO
| S_IWUSR
,
289 static ssize_t
adt7410_convert_temperature(struct adt7410_chip_info
*chip
,
294 if (chip
->config
& ADT7410_RESOLUTION
) {
295 if (data
& ADT7410_T16_VALUE_SIGN
) {
296 /* convert supplement to positive value */
297 data
= (u16
)((ADT7410_T16_VALUE_SIGN
<< 1) - (u32
)data
);
300 return sprintf(buf
, "%c%d.%.7d\n", sign
,
301 (data
>> ADT7410_T16_VALUE_FLOAT_OFFSET
),
302 (data
& ADT7410_T16_VALUE_FLOAT_MASK
) * 78125);
304 if (data
& ADT7410_T13_VALUE_SIGN
) {
305 /* convert supplement to positive value */
306 data
>>= ADT7410_T13_VALUE_OFFSET
;
307 data
= (ADT7410_T13_VALUE_SIGN
<< 1) - data
;
310 return sprintf(buf
, "%c%d.%.4d\n", sign
,
311 (data
>> ADT7410_T13_VALUE_FLOAT_OFFSET
),
312 (data
& ADT7410_T13_VALUE_FLOAT_MASK
) * 625);
316 static ssize_t
adt7410_show_value(struct device
*dev
,
317 struct device_attribute
*attr
,
320 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
321 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
327 ret
= adt7410_i2c_read_byte(chip
, ADT7410_STATUS
, &status
);
333 } while (status
& ADT7410_STAT_NOT_RDY
);
335 ret
= adt7410_i2c_read_word(chip
, ADT7410_TEMPERATURE
, &data
);
339 return adt7410_convert_temperature(chip
, data
, buf
);
342 static IIO_DEVICE_ATTR(value
, S_IRUGO
, adt7410_show_value
, NULL
, 0);
344 static struct attribute
*adt7410_attributes
[] = {
345 &iio_dev_attr_available_modes
.dev_attr
.attr
,
346 &iio_dev_attr_mode
.dev_attr
.attr
,
347 &iio_dev_attr_resolution
.dev_attr
.attr
,
348 &iio_dev_attr_id
.dev_attr
.attr
,
349 &iio_dev_attr_value
.dev_attr
.attr
,
353 static const struct attribute_group adt7410_attribute_group
= {
354 .attrs
= adt7410_attributes
,
357 static irqreturn_t
adt7410_event_handler(int irq
, void *private)
359 struct iio_dev
*indio_dev
= private;
360 struct adt7410_chip_info
*chip
= iio_priv(indio_dev
);
361 s64 timestamp
= iio_get_time_ns();
364 if (adt7410_i2c_read_byte(chip
, ADT7410_STATUS
, &status
))
367 if (status
& ADT7410_STAT_T_HIGH
)
368 iio_push_event(indio_dev
, 0,
369 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
373 if (status
& ADT7410_STAT_T_LOW
)
374 iio_push_event(indio_dev
, 0,
375 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
379 if (status
& ADT7410_STAT_T_CRIT
)
380 iio_push_event(indio_dev
, 0,
381 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
389 static ssize_t
adt7410_show_event_mode(struct device
*dev
,
390 struct device_attribute
*attr
,
393 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
394 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
397 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
401 if (chip
->config
& ADT7410_EVENT_MODE
)
402 return sprintf(buf
, "interrupt\n");
404 return sprintf(buf
, "comparator\n");
407 static ssize_t
adt7410_set_event_mode(struct device
*dev
,
408 struct device_attribute
*attr
,
412 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
413 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
417 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
421 config
= chip
->config
&= ~ADT7410_EVENT_MODE
;
422 if (strcmp(buf
, "comparator") != 0)
423 config
|= ADT7410_EVENT_MODE
;
425 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, config
);
429 chip
->config
= config
;
434 static ssize_t
adt7410_show_available_event_modes(struct device
*dev
,
435 struct device_attribute
*attr
,
438 return sprintf(buf
, "comparator\ninterrupt\n");
441 static ssize_t
adt7410_show_fault_queue(struct device
*dev
,
442 struct device_attribute
*attr
,
445 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
446 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
449 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
453 return sprintf(buf
, "%d\n", chip
->config
& ADT7410_FAULT_QUEUE_MASK
);
456 static ssize_t
adt7410_set_fault_queue(struct device
*dev
,
457 struct device_attribute
*attr
,
461 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
462 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
467 ret
= strict_strtoul(buf
, 10, &data
);
471 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
475 config
= chip
->config
& ~ADT7410_FAULT_QUEUE_MASK
;
477 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, config
);
481 chip
->config
= config
;
486 static inline ssize_t
adt7410_show_t_bound(struct device
*dev
,
487 struct device_attribute
*attr
,
491 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
492 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
496 ret
= adt7410_i2c_read_word(chip
, bound_reg
, &data
);
500 return adt7410_convert_temperature(chip
, data
, buf
);
503 static inline ssize_t
adt7410_set_t_bound(struct device
*dev
,
504 struct device_attribute
*attr
,
509 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
510 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
516 pos
= strchr(buf
, '.');
518 ret
= strict_strtol(buf
, 10, &tmp1
);
520 if (ret
|| tmp1
> 127 || tmp1
< -128)
526 if (chip
->config
& ADT7410_RESOLUTION
) {
527 if (len
> ADT7410_T16_VALUE_FLOAT_OFFSET
)
528 len
= ADT7410_T16_VALUE_FLOAT_OFFSET
;
530 ret
= strict_strtol(pos
, 10, &tmp2
);
533 tmp2
= (tmp2
/ 78125) * 78125;
535 if (len
> ADT7410_T13_VALUE_FLOAT_OFFSET
)
536 len
= ADT7410_T13_VALUE_FLOAT_OFFSET
;
538 ret
= strict_strtol(pos
, 10, &tmp2
);
541 tmp2
= (tmp2
/ 625) * 625;
550 if (chip
->config
& ADT7410_RESOLUTION
) {
551 data
= (data
<< ADT7410_T16_VALUE_FLOAT_OFFSET
) |
552 (tmp2
& ADT7410_T16_VALUE_FLOAT_MASK
);
555 /* convert positive value to supplyment */
556 data
= (u16
)((ADT7410_T16_VALUE_SIGN
<< 1) - (u32
)data
);
558 data
= (data
<< ADT7410_T13_VALUE_FLOAT_OFFSET
) |
559 (tmp2
& ADT7410_T13_VALUE_FLOAT_MASK
);
562 /* convert positive value to supplyment */
563 data
= (ADT7410_T13_VALUE_SIGN
<< 1) - data
;
564 data
<<= ADT7410_T13_VALUE_OFFSET
;
567 ret
= adt7410_i2c_write_word(chip
, bound_reg
, data
);
574 static ssize_t
adt7410_show_t_alarm_high(struct device
*dev
,
575 struct device_attribute
*attr
,
578 return adt7410_show_t_bound(dev
, attr
,
579 ADT7410_T_ALARM_HIGH
, buf
);
582 static inline ssize_t
adt7410_set_t_alarm_high(struct device
*dev
,
583 struct device_attribute
*attr
,
587 return adt7410_set_t_bound(dev
, attr
,
588 ADT7410_T_ALARM_HIGH
, buf
, len
);
591 static ssize_t
adt7410_show_t_alarm_low(struct device
*dev
,
592 struct device_attribute
*attr
,
595 return adt7410_show_t_bound(dev
, attr
,
596 ADT7410_T_ALARM_LOW
, buf
);
599 static inline ssize_t
adt7410_set_t_alarm_low(struct device
*dev
,
600 struct device_attribute
*attr
,
604 return adt7410_set_t_bound(dev
, attr
,
605 ADT7410_T_ALARM_LOW
, buf
, len
);
608 static ssize_t
adt7410_show_t_crit(struct device
*dev
,
609 struct device_attribute
*attr
,
612 return adt7410_show_t_bound(dev
, attr
,
613 ADT7410_T_CRIT
, buf
);
616 static inline ssize_t
adt7410_set_t_crit(struct device
*dev
,
617 struct device_attribute
*attr
,
621 return adt7410_set_t_bound(dev
, attr
,
622 ADT7410_T_CRIT
, buf
, len
);
625 static ssize_t
adt7410_show_t_hyst(struct device
*dev
,
626 struct device_attribute
*attr
,
629 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
630 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
634 ret
= adt7410_i2c_read_byte(chip
, ADT7410_T_HYST
, &t_hyst
);
638 return sprintf(buf
, "%d\n", t_hyst
& ADT7410_T_HYST_MASK
);
641 static inline ssize_t
adt7410_set_t_hyst(struct device
*dev
,
642 struct device_attribute
*attr
,
646 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
647 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
652 ret
= strict_strtol(buf
, 10, &data
);
654 if (ret
|| data
> ADT7410_T_HYST_MASK
)
659 ret
= adt7410_i2c_write_byte(chip
, ADT7410_T_HYST
, t_hyst
);
666 static IIO_DEVICE_ATTR(event_mode
,
668 adt7410_show_event_mode
, adt7410_set_event_mode
, 0);
669 static IIO_DEVICE_ATTR(available_event_modes
,
671 adt7410_show_available_event_modes
, NULL
, 0);
672 static IIO_DEVICE_ATTR(fault_queue
,
674 adt7410_show_fault_queue
, adt7410_set_fault_queue
, 0);
675 static IIO_DEVICE_ATTR(t_alarm_high
,
677 adt7410_show_t_alarm_high
, adt7410_set_t_alarm_high
, 0);
678 static IIO_DEVICE_ATTR(t_alarm_low
,
680 adt7410_show_t_alarm_low
, adt7410_set_t_alarm_low
, 0);
681 static IIO_DEVICE_ATTR(t_crit
,
683 adt7410_show_t_crit
, adt7410_set_t_crit
, 0);
684 static IIO_DEVICE_ATTR(t_hyst
,
686 adt7410_show_t_hyst
, adt7410_set_t_hyst
, 0);
688 static struct attribute
*adt7410_event_int_attributes
[] = {
689 &iio_dev_attr_event_mode
.dev_attr
.attr
,
690 &iio_dev_attr_available_event_modes
.dev_attr
.attr
,
691 &iio_dev_attr_fault_queue
.dev_attr
.attr
,
692 &iio_dev_attr_t_alarm_high
.dev_attr
.attr
,
693 &iio_dev_attr_t_alarm_low
.dev_attr
.attr
,
694 &iio_dev_attr_t_hyst
.dev_attr
.attr
,
698 static struct attribute
*adt7410_event_ct_attributes
[] = {
699 &iio_dev_attr_event_mode
.dev_attr
.attr
,
700 &iio_dev_attr_available_event_modes
.dev_attr
.attr
,
701 &iio_dev_attr_fault_queue
.dev_attr
.attr
,
702 &iio_dev_attr_t_crit
.dev_attr
.attr
,
703 &iio_dev_attr_t_hyst
.dev_attr
.attr
,
707 static struct attribute_group adt7410_event_attribute_group
[ADT7410_IRQS
] = {
709 .attrs
= adt7410_event_int_attributes
,
711 .attrs
= adt7410_event_ct_attributes
,
715 static const struct iio_info adt7410_info
= {
716 .attrs
= &adt7410_attribute_group
,
717 .num_interrupt_lines
= ADT7410_IRQS
,
718 .event_attrs
= adt7410_event_attribute_group
,
719 .driver_module
= THIS_MODULE
,
723 * device probe and remove
726 static int __devinit
adt7410_probe(struct i2c_client
*client
,
727 const struct i2c_device_id
*id
)
729 struct adt7410_chip_info
*chip
;
730 struct iio_dev
*indio_dev
;
732 unsigned long *adt7410_platform_data
= client
->dev
.platform_data
;
734 indio_dev
= iio_allocate_device(sizeof(*chip
));
735 if (indio_dev
== NULL
) {
739 chip
= iio_priv(indio_dev
);
740 /* this is only used for device removal purposes */
741 i2c_set_clientdata(client
, indio_dev
);
743 chip
->client
= client
;
745 indio_dev
->name
= id
->name
;
746 indio_dev
->dev
.parent
= &client
->dev
;
747 indio_dev
->info
= &adt7410_info
;
748 indio_dev
->modes
= INDIO_DIRECT_MODE
;
750 ret
= iio_device_register(indio_dev
);
754 /* CT critcal temperature event. line 0 */
756 ret
= request_threaded_irq(client
->irq
,
758 &adt7410_event_handler
,
763 goto error_unreg_dev
;
766 /* INT bound temperature alarm event. line 1 */
767 if (adt7410_platform_data
[0]) {
768 ret
= request_threaded_irq(adt7410_platform_data
[0],
770 &adt7410_event_handler
,
771 adt7410_platform_data
[1],
775 goto error_unreg_ct_irq
;
778 if (client
->irq
&& adt7410_platform_data
[0]) {
780 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
783 goto error_unreg_int_irq
;
786 /* set irq polarity low level */
787 chip
->config
&= ~ADT7410_CT_POLARITY
;
789 if (adt7410_platform_data
[1] & IRQF_TRIGGER_HIGH
)
790 chip
->config
|= ADT7410_INT_POLARITY
;
792 chip
->config
&= ~ADT7410_INT_POLARITY
;
794 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, chip
->config
);
797 goto error_unreg_int_irq
;
801 dev_info(&client
->dev
, "%s temperature sensor registered.\n",
807 free_irq(adt7410_platform_data
[0], indio_dev
);
809 free_irq(client
->irq
, indio_dev
);
811 iio_device_unregister(indio_dev
);
813 iio_free_device(indio_dev
);
818 static int __devexit
adt7410_remove(struct i2c_client
*client
)
820 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
821 unsigned long *adt7410_platform_data
= client
->dev
.platform_data
;
823 if (adt7410_platform_data
[0])
824 free_irq(adt7410_platform_data
[0], indio_dev
);
826 free_irq(client
->irq
, indio_dev
);
827 iio_device_unregister(indio_dev
);
832 static const struct i2c_device_id adt7410_id
[] = {
837 MODULE_DEVICE_TABLE(i2c
, adt7410_id
);
839 static struct i2c_driver adt7410_driver
= {
843 .probe
= adt7410_probe
,
844 .remove
= __devexit_p(adt7410_remove
),
845 .id_table
= adt7410_id
,
848 static __init
int adt7410_init(void)
850 return i2c_add_driver(&adt7410_driver
);
853 static __exit
void adt7410_exit(void)
855 i2c_del_driver(&adt7410_driver
);
858 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
859 MODULE_DESCRIPTION("Analog Devices ADT7410 digital"
860 " temperature sensor driver");
861 MODULE_LICENSE("GPL v2");
863 module_init(adt7410_init
);
864 module_exit(adt7410_exit
);