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>
16 #include <linux/module.h>
22 * ADT7410 registers definition
25 #define ADT7410_TEMPERATURE 0
26 #define ADT7410_STATUS 2
27 #define ADT7410_CONFIG 3
28 #define ADT7410_T_ALARM_HIGH 4
29 #define ADT7410_T_ALARM_LOW 6
30 #define ADT7410_T_CRIT 8
31 #define ADT7410_T_HYST 0xA
32 #define ADT7410_ID 0xB
33 #define ADT7410_RESET 0x2F
38 #define ADT7410_STAT_T_LOW 0x10
39 #define ADT7410_STAT_T_HIGH 0x20
40 #define ADT7410_STAT_T_CRIT 0x40
41 #define ADT7410_STAT_NOT_RDY 0x80
46 #define ADT7410_FAULT_QUEUE_MASK 0x3
47 #define ADT7410_CT_POLARITY 0x4
48 #define ADT7410_INT_POLARITY 0x8
49 #define ADT7410_EVENT_MODE 0x10
50 #define ADT7410_MODE_MASK 0x60
51 #define ADT7410_ONESHOT 0x20
52 #define ADT7410_SPS 0x40
53 #define ADT7410_PD 0x60
54 #define ADT7410_RESOLUTION 0x80
59 #define ADT7410_T16_VALUE_SIGN 0x8000
60 #define ADT7410_T16_VALUE_FLOAT_OFFSET 7
61 #define ADT7410_T16_VALUE_FLOAT_MASK 0x7F
62 #define ADT7410_T13_VALUE_SIGN 0x1000
63 #define ADT7410_T13_VALUE_OFFSET 3
64 #define ADT7410_T13_VALUE_FLOAT_OFFSET 4
65 #define ADT7410_T13_VALUE_FLOAT_MASK 0xF
66 #define ADT7410_T_HYST_MASK 0xF
67 #define ADT7410_DEVICE_ID_MASK 0xF
68 #define ADT7410_MANUFACTORY_ID_MASK 0xF0
69 #define ADT7410_MANUFACTORY_ID_OFFSET 4
71 #define ADT7410_IRQS 2
74 * struct adt7410_chip_info - chip specifc information
77 struct adt7410_chip_info
{
78 struct i2c_client
*client
;
83 * adt7410 register access by I2C
86 static int adt7410_i2c_read_word(struct adt7410_chip_info
*chip
, u8 reg
, u16
*data
)
88 struct i2c_client
*client
= chip
->client
;
91 ret
= i2c_smbus_read_word_data(client
, reg
);
93 dev_err(&client
->dev
, "I2C read error\n");
97 *data
= swab16((u16
)ret
);
102 static int adt7410_i2c_write_word(struct adt7410_chip_info
*chip
, u8 reg
, u16 data
)
104 struct i2c_client
*client
= chip
->client
;
107 ret
= i2c_smbus_write_word_data(client
, reg
, swab16(data
));
109 dev_err(&client
->dev
, "I2C write error\n");
114 static int adt7410_i2c_read_byte(struct adt7410_chip_info
*chip
, u8 reg
, u8
*data
)
116 struct i2c_client
*client
= chip
->client
;
119 ret
= i2c_smbus_read_byte_data(client
, reg
);
121 dev_err(&client
->dev
, "I2C read error\n");
130 static int adt7410_i2c_write_byte(struct adt7410_chip_info
*chip
, u8 reg
, u8 data
)
132 struct i2c_client
*client
= chip
->client
;
135 ret
= i2c_smbus_write_byte_data(client
, reg
, data
);
137 dev_err(&client
->dev
, "I2C write error\n");
142 static ssize_t
adt7410_show_mode(struct device
*dev
,
143 struct device_attribute
*attr
,
146 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
147 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
150 config
= chip
->config
& ADT7410_MODE_MASK
;
154 return sprintf(buf
, "power-down\n");
155 case ADT7410_ONESHOT
:
156 return sprintf(buf
, "one-shot\n");
158 return sprintf(buf
, "sps\n");
160 return sprintf(buf
, "full\n");
164 static ssize_t
adt7410_store_mode(struct device
*dev
,
165 struct device_attribute
*attr
,
169 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
170 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
174 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
178 config
= chip
->config
& (~ADT7410_MODE_MASK
);
179 if (strcmp(buf
, "power-down"))
180 config
|= ADT7410_PD
;
181 else if (strcmp(buf
, "one-shot"))
182 config
|= ADT7410_ONESHOT
;
183 else if (strcmp(buf
, "sps"))
184 config
|= ADT7410_SPS
;
186 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, config
);
190 chip
->config
= config
;
195 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
200 static ssize_t
adt7410_show_available_modes(struct device
*dev
,
201 struct device_attribute
*attr
,
204 return sprintf(buf
, "full\none-shot\nsps\npower-down\n");
207 static IIO_DEVICE_ATTR(available_modes
, S_IRUGO
, adt7410_show_available_modes
, NULL
, 0);
209 static ssize_t
adt7410_show_resolution(struct device
*dev
,
210 struct device_attribute
*attr
,
213 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
214 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
218 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
222 if (chip
->config
& ADT7410_RESOLUTION
)
227 return sprintf(buf
, "%d bits\n", bits
);
230 static ssize_t
adt7410_store_resolution(struct device
*dev
,
231 struct device_attribute
*attr
,
235 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
236 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
241 ret
= strict_strtoul(buf
, 10, &data
);
245 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
249 config
= chip
->config
& (~ADT7410_RESOLUTION
);
251 config
|= ADT7410_RESOLUTION
;
253 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, config
);
257 chip
->config
= config
;
262 static IIO_DEVICE_ATTR(resolution
, S_IRUGO
| S_IWUSR
,
263 adt7410_show_resolution
,
264 adt7410_store_resolution
,
267 static ssize_t
adt7410_show_id(struct device
*dev
,
268 struct device_attribute
*attr
,
271 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
272 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
276 ret
= adt7410_i2c_read_byte(chip
, ADT7410_ID
, &id
);
280 return sprintf(buf
, "device id: 0x%x\nmanufactory id: 0x%x\n",
281 id
& ADT7410_DEVICE_ID_MASK
,
282 (id
& ADT7410_MANUFACTORY_ID_MASK
) >> ADT7410_MANUFACTORY_ID_OFFSET
);
285 static IIO_DEVICE_ATTR(id
, S_IRUGO
| S_IWUSR
,
290 static ssize_t
adt7410_convert_temperature(struct adt7410_chip_info
*chip
,
295 if (chip
->config
& ADT7410_RESOLUTION
) {
296 if (data
& ADT7410_T16_VALUE_SIGN
) {
297 /* convert supplement to positive value */
298 data
= (u16
)((ADT7410_T16_VALUE_SIGN
<< 1) - (u32
)data
);
301 return sprintf(buf
, "%c%d.%.7d\n", sign
,
302 (data
>> ADT7410_T16_VALUE_FLOAT_OFFSET
),
303 (data
& ADT7410_T16_VALUE_FLOAT_MASK
) * 78125);
305 if (data
& ADT7410_T13_VALUE_SIGN
) {
306 /* convert supplement to positive value */
307 data
>>= ADT7410_T13_VALUE_OFFSET
;
308 data
= (ADT7410_T13_VALUE_SIGN
<< 1) - data
;
311 return sprintf(buf
, "%c%d.%.4d\n", sign
,
312 (data
>> ADT7410_T13_VALUE_FLOAT_OFFSET
),
313 (data
& ADT7410_T13_VALUE_FLOAT_MASK
) * 625);
317 static ssize_t
adt7410_show_value(struct device
*dev
,
318 struct device_attribute
*attr
,
321 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
322 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
328 ret
= adt7410_i2c_read_byte(chip
, ADT7410_STATUS
, &status
);
334 } while (status
& ADT7410_STAT_NOT_RDY
);
336 ret
= adt7410_i2c_read_word(chip
, ADT7410_TEMPERATURE
, &data
);
340 return adt7410_convert_temperature(chip
, data
, buf
);
343 static IIO_DEVICE_ATTR(value
, S_IRUGO
, adt7410_show_value
, NULL
, 0);
345 static struct attribute
*adt7410_attributes
[] = {
346 &iio_dev_attr_available_modes
.dev_attr
.attr
,
347 &iio_dev_attr_mode
.dev_attr
.attr
,
348 &iio_dev_attr_resolution
.dev_attr
.attr
,
349 &iio_dev_attr_id
.dev_attr
.attr
,
350 &iio_dev_attr_value
.dev_attr
.attr
,
354 static const struct attribute_group adt7410_attribute_group
= {
355 .attrs
= adt7410_attributes
,
358 static irqreturn_t
adt7410_event_handler(int irq
, void *private)
360 struct iio_dev
*indio_dev
= private;
361 struct adt7410_chip_info
*chip
= iio_priv(indio_dev
);
362 s64 timestamp
= iio_get_time_ns();
365 if (adt7410_i2c_read_byte(chip
, ADT7410_STATUS
, &status
))
368 if (status
& ADT7410_STAT_T_HIGH
)
369 iio_push_event(indio_dev
,
370 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
374 if (status
& ADT7410_STAT_T_LOW
)
375 iio_push_event(indio_dev
,
376 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
380 if (status
& ADT7410_STAT_T_CRIT
)
381 iio_push_event(indio_dev
,
382 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
390 static ssize_t
adt7410_show_event_mode(struct device
*dev
,
391 struct device_attribute
*attr
,
394 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
395 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
398 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
402 if (chip
->config
& ADT7410_EVENT_MODE
)
403 return sprintf(buf
, "interrupt\n");
405 return sprintf(buf
, "comparator\n");
408 static ssize_t
adt7410_set_event_mode(struct device
*dev
,
409 struct device_attribute
*attr
,
413 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
414 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
418 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
422 config
= chip
->config
&= ~ADT7410_EVENT_MODE
;
423 if (strcmp(buf
, "comparator") != 0)
424 config
|= ADT7410_EVENT_MODE
;
426 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, config
);
430 chip
->config
= config
;
435 static ssize_t
adt7410_show_available_event_modes(struct device
*dev
,
436 struct device_attribute
*attr
,
439 return sprintf(buf
, "comparator\ninterrupt\n");
442 static ssize_t
adt7410_show_fault_queue(struct device
*dev
,
443 struct device_attribute
*attr
,
446 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
447 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
450 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
454 return sprintf(buf
, "%d\n", chip
->config
& ADT7410_FAULT_QUEUE_MASK
);
457 static ssize_t
adt7410_set_fault_queue(struct device
*dev
,
458 struct device_attribute
*attr
,
462 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
463 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
468 ret
= strict_strtoul(buf
, 10, &data
);
472 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
476 config
= chip
->config
& ~ADT7410_FAULT_QUEUE_MASK
;
478 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, config
);
482 chip
->config
= config
;
487 static inline ssize_t
adt7410_show_t_bound(struct device
*dev
,
488 struct device_attribute
*attr
,
492 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
493 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
497 ret
= adt7410_i2c_read_word(chip
, bound_reg
, &data
);
501 return adt7410_convert_temperature(chip
, data
, buf
);
504 static inline ssize_t
adt7410_set_t_bound(struct device
*dev
,
505 struct device_attribute
*attr
,
510 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
511 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
517 pos
= strchr(buf
, '.');
519 ret
= strict_strtol(buf
, 10, &tmp1
);
521 if (ret
|| tmp1
> 127 || tmp1
< -128)
527 if (chip
->config
& ADT7410_RESOLUTION
) {
528 if (len
> ADT7410_T16_VALUE_FLOAT_OFFSET
)
529 len
= ADT7410_T16_VALUE_FLOAT_OFFSET
;
531 ret
= strict_strtol(pos
, 10, &tmp2
);
534 tmp2
= (tmp2
/ 78125) * 78125;
536 if (len
> ADT7410_T13_VALUE_FLOAT_OFFSET
)
537 len
= ADT7410_T13_VALUE_FLOAT_OFFSET
;
539 ret
= strict_strtol(pos
, 10, &tmp2
);
542 tmp2
= (tmp2
/ 625) * 625;
551 if (chip
->config
& ADT7410_RESOLUTION
) {
552 data
= (data
<< ADT7410_T16_VALUE_FLOAT_OFFSET
) |
553 (tmp2
& ADT7410_T16_VALUE_FLOAT_MASK
);
556 /* convert positive value to supplyment */
557 data
= (u16
)((ADT7410_T16_VALUE_SIGN
<< 1) - (u32
)data
);
559 data
= (data
<< ADT7410_T13_VALUE_FLOAT_OFFSET
) |
560 (tmp2
& ADT7410_T13_VALUE_FLOAT_MASK
);
563 /* convert positive value to supplyment */
564 data
= (ADT7410_T13_VALUE_SIGN
<< 1) - data
;
565 data
<<= ADT7410_T13_VALUE_OFFSET
;
568 ret
= adt7410_i2c_write_word(chip
, bound_reg
, data
);
575 static ssize_t
adt7410_show_t_alarm_high(struct device
*dev
,
576 struct device_attribute
*attr
,
579 return adt7410_show_t_bound(dev
, attr
,
580 ADT7410_T_ALARM_HIGH
, buf
);
583 static inline ssize_t
adt7410_set_t_alarm_high(struct device
*dev
,
584 struct device_attribute
*attr
,
588 return adt7410_set_t_bound(dev
, attr
,
589 ADT7410_T_ALARM_HIGH
, buf
, len
);
592 static ssize_t
adt7410_show_t_alarm_low(struct device
*dev
,
593 struct device_attribute
*attr
,
596 return adt7410_show_t_bound(dev
, attr
,
597 ADT7410_T_ALARM_LOW
, buf
);
600 static inline ssize_t
adt7410_set_t_alarm_low(struct device
*dev
,
601 struct device_attribute
*attr
,
605 return adt7410_set_t_bound(dev
, attr
,
606 ADT7410_T_ALARM_LOW
, buf
, len
);
609 static ssize_t
adt7410_show_t_crit(struct device
*dev
,
610 struct device_attribute
*attr
,
613 return adt7410_show_t_bound(dev
, attr
,
614 ADT7410_T_CRIT
, buf
);
617 static inline ssize_t
adt7410_set_t_crit(struct device
*dev
,
618 struct device_attribute
*attr
,
622 return adt7410_set_t_bound(dev
, attr
,
623 ADT7410_T_CRIT
, buf
, len
);
626 static ssize_t
adt7410_show_t_hyst(struct device
*dev
,
627 struct device_attribute
*attr
,
630 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
631 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
635 ret
= adt7410_i2c_read_byte(chip
, ADT7410_T_HYST
, &t_hyst
);
639 return sprintf(buf
, "%d\n", t_hyst
& ADT7410_T_HYST_MASK
);
642 static inline ssize_t
adt7410_set_t_hyst(struct device
*dev
,
643 struct device_attribute
*attr
,
647 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
648 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
653 ret
= strict_strtol(buf
, 10, &data
);
655 if (ret
|| data
> ADT7410_T_HYST_MASK
)
660 ret
= adt7410_i2c_write_byte(chip
, ADT7410_T_HYST
, t_hyst
);
667 static IIO_DEVICE_ATTR(event_mode
,
669 adt7410_show_event_mode
, adt7410_set_event_mode
, 0);
670 static IIO_DEVICE_ATTR(available_event_modes
,
672 adt7410_show_available_event_modes
, NULL
, 0);
673 static IIO_DEVICE_ATTR(fault_queue
,
675 adt7410_show_fault_queue
, adt7410_set_fault_queue
, 0);
676 static IIO_DEVICE_ATTR(t_alarm_high
,
678 adt7410_show_t_alarm_high
, adt7410_set_t_alarm_high
, 0);
679 static IIO_DEVICE_ATTR(t_alarm_low
,
681 adt7410_show_t_alarm_low
, adt7410_set_t_alarm_low
, 0);
682 static IIO_DEVICE_ATTR(t_crit
,
684 adt7410_show_t_crit
, adt7410_set_t_crit
, 0);
685 static IIO_DEVICE_ATTR(t_hyst
,
687 adt7410_show_t_hyst
, adt7410_set_t_hyst
, 0);
689 static struct attribute
*adt7410_event_int_attributes
[] = {
690 &iio_dev_attr_event_mode
.dev_attr
.attr
,
691 &iio_dev_attr_available_event_modes
.dev_attr
.attr
,
692 &iio_dev_attr_fault_queue
.dev_attr
.attr
,
693 &iio_dev_attr_t_alarm_high
.dev_attr
.attr
,
694 &iio_dev_attr_t_alarm_low
.dev_attr
.attr
,
695 &iio_dev_attr_t_hyst
.dev_attr
.attr
,
699 static struct attribute
*adt7410_event_ct_attributes
[] = {
700 &iio_dev_attr_event_mode
.dev_attr
.attr
,
701 &iio_dev_attr_available_event_modes
.dev_attr
.attr
,
702 &iio_dev_attr_fault_queue
.dev_attr
.attr
,
703 &iio_dev_attr_t_crit
.dev_attr
.attr
,
704 &iio_dev_attr_t_hyst
.dev_attr
.attr
,
708 static struct attribute_group adt7410_event_attribute_group
[ADT7410_IRQS
] = {
710 .attrs
= adt7410_event_int_attributes
,
713 .attrs
= adt7410_event_ct_attributes
,
718 static const struct iio_info adt7410_info
= {
719 .attrs
= &adt7410_attribute_group
,
720 .event_attrs
= adt7410_event_attribute_group
,
721 .driver_module
= THIS_MODULE
,
725 * device probe and remove
728 static int __devinit
adt7410_probe(struct i2c_client
*client
,
729 const struct i2c_device_id
*id
)
731 struct adt7410_chip_info
*chip
;
732 struct iio_dev
*indio_dev
;
734 unsigned long *adt7410_platform_data
= client
->dev
.platform_data
;
736 indio_dev
= iio_allocate_device(sizeof(*chip
));
737 if (indio_dev
== NULL
) {
741 chip
= iio_priv(indio_dev
);
742 /* this is only used for device removal purposes */
743 i2c_set_clientdata(client
, indio_dev
);
745 chip
->client
= client
;
747 indio_dev
->name
= id
->name
;
748 indio_dev
->dev
.parent
= &client
->dev
;
749 indio_dev
->info
= &adt7410_info
;
750 indio_dev
->modes
= INDIO_DIRECT_MODE
;
752 /* CT critcal temperature event. line 0 */
754 ret
= request_threaded_irq(client
->irq
,
756 &adt7410_event_handler
,
764 /* INT bound temperature alarm event. line 1 */
765 if (adt7410_platform_data
[0]) {
766 ret
= request_threaded_irq(adt7410_platform_data
[0],
768 &adt7410_event_handler
,
769 adt7410_platform_data
[1],
773 goto error_unreg_ct_irq
;
776 if (client
->irq
&& adt7410_platform_data
[0]) {
778 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
781 goto error_unreg_int_irq
;
784 /* set irq polarity low level */
785 chip
->config
&= ~ADT7410_CT_POLARITY
;
787 if (adt7410_platform_data
[1] & IRQF_TRIGGER_HIGH
)
788 chip
->config
|= ADT7410_INT_POLARITY
;
790 chip
->config
&= ~ADT7410_INT_POLARITY
;
792 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, chip
->config
);
795 goto error_unreg_int_irq
;
798 ret
= iio_device_register(indio_dev
);
800 goto error_unreg_int_irq
;
802 dev_info(&client
->dev
, "%s temperature sensor registered.\n",
808 free_irq(adt7410_platform_data
[0], indio_dev
);
810 free_irq(client
->irq
, indio_dev
);
812 iio_free_device(indio_dev
);
817 static int __devexit
adt7410_remove(struct i2c_client
*client
)
819 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
820 unsigned long *adt7410_platform_data
= client
->dev
.platform_data
;
822 iio_device_unregister(indio_dev
);
823 if (adt7410_platform_data
[0])
824 free_irq(adt7410_platform_data
[0], indio_dev
);
826 free_irq(client
->irq
, indio_dev
);
827 iio_free_device(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
);