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>
20 #include "../events.h"
23 * ADT7410 registers definition
26 #define ADT7410_TEMPERATURE 0
27 #define ADT7410_STATUS 2
28 #define ADT7410_CONFIG 3
29 #define ADT7410_T_ALARM_HIGH 4
30 #define ADT7410_T_ALARM_LOW 6
31 #define ADT7410_T_CRIT 8
32 #define ADT7410_T_HYST 0xA
33 #define ADT7410_ID 0xB
34 #define ADT7410_RESET 0x2F
39 #define ADT7410_STAT_T_LOW 0x10
40 #define ADT7410_STAT_T_HIGH 0x20
41 #define ADT7410_STAT_T_CRIT 0x40
42 #define ADT7410_STAT_NOT_RDY 0x80
47 #define ADT7410_FAULT_QUEUE_MASK 0x3
48 #define ADT7410_CT_POLARITY 0x4
49 #define ADT7410_INT_POLARITY 0x8
50 #define ADT7410_EVENT_MODE 0x10
51 #define ADT7410_MODE_MASK 0x60
52 #define ADT7410_ONESHOT 0x20
53 #define ADT7410_SPS 0x40
54 #define ADT7410_PD 0x60
55 #define ADT7410_RESOLUTION 0x80
60 #define ADT7410_T16_VALUE_SIGN 0x8000
61 #define ADT7410_T16_VALUE_FLOAT_OFFSET 7
62 #define ADT7410_T16_VALUE_FLOAT_MASK 0x7F
63 #define ADT7410_T13_VALUE_SIGN 0x1000
64 #define ADT7410_T13_VALUE_OFFSET 3
65 #define ADT7410_T13_VALUE_FLOAT_OFFSET 4
66 #define ADT7410_T13_VALUE_FLOAT_MASK 0xF
67 #define ADT7410_T_HYST_MASK 0xF
68 #define ADT7410_DEVICE_ID_MASK 0xF
69 #define ADT7410_MANUFACTORY_ID_MASK 0xF0
70 #define ADT7410_MANUFACTORY_ID_OFFSET 4
72 #define ADT7410_IRQS 2
75 * struct adt7410_chip_info - chip specifc information
78 struct adt7410_chip_info
{
79 struct i2c_client
*client
;
84 * adt7410 register access by I2C
87 static int adt7410_i2c_read_word(struct adt7410_chip_info
*chip
, u8 reg
, u16
*data
)
89 struct i2c_client
*client
= chip
->client
;
92 ret
= i2c_smbus_read_word_data(client
, reg
);
94 dev_err(&client
->dev
, "I2C read error\n");
98 *data
= swab16((u16
)ret
);
103 static int adt7410_i2c_write_word(struct adt7410_chip_info
*chip
, u8 reg
, u16 data
)
105 struct i2c_client
*client
= chip
->client
;
108 ret
= i2c_smbus_write_word_data(client
, reg
, swab16(data
));
110 dev_err(&client
->dev
, "I2C write error\n");
115 static int adt7410_i2c_read_byte(struct adt7410_chip_info
*chip
, u8 reg
, u8
*data
)
117 struct i2c_client
*client
= chip
->client
;
120 ret
= i2c_smbus_read_byte_data(client
, reg
);
122 dev_err(&client
->dev
, "I2C read error\n");
131 static int adt7410_i2c_write_byte(struct adt7410_chip_info
*chip
, u8 reg
, u8 data
)
133 struct i2c_client
*client
= chip
->client
;
136 ret
= i2c_smbus_write_byte_data(client
, reg
, data
);
138 dev_err(&client
->dev
, "I2C write error\n");
143 static ssize_t
adt7410_show_mode(struct device
*dev
,
144 struct device_attribute
*attr
,
147 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
148 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
151 config
= chip
->config
& ADT7410_MODE_MASK
;
155 return sprintf(buf
, "power-down\n");
156 case ADT7410_ONESHOT
:
157 return sprintf(buf
, "one-shot\n");
159 return sprintf(buf
, "sps\n");
161 return sprintf(buf
, "full\n");
165 static ssize_t
adt7410_store_mode(struct device
*dev
,
166 struct device_attribute
*attr
,
170 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
171 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
175 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
179 config
= chip
->config
& (~ADT7410_MODE_MASK
);
180 if (strcmp(buf
, "power-down"))
181 config
|= ADT7410_PD
;
182 else if (strcmp(buf
, "one-shot"))
183 config
|= ADT7410_ONESHOT
;
184 else if (strcmp(buf
, "sps"))
185 config
|= ADT7410_SPS
;
187 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, config
);
191 chip
->config
= config
;
196 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
201 static ssize_t
adt7410_show_available_modes(struct device
*dev
,
202 struct device_attribute
*attr
,
205 return sprintf(buf
, "full\none-shot\nsps\npower-down\n");
208 static IIO_DEVICE_ATTR(available_modes
, S_IRUGO
, adt7410_show_available_modes
, NULL
, 0);
210 static ssize_t
adt7410_show_resolution(struct device
*dev
,
211 struct device_attribute
*attr
,
214 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
215 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
219 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
223 if (chip
->config
& ADT7410_RESOLUTION
)
228 return sprintf(buf
, "%d bits\n", bits
);
231 static ssize_t
adt7410_store_resolution(struct device
*dev
,
232 struct device_attribute
*attr
,
236 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
237 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
242 ret
= strict_strtoul(buf
, 10, &data
);
246 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
250 config
= chip
->config
& (~ADT7410_RESOLUTION
);
252 config
|= ADT7410_RESOLUTION
;
254 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, config
);
258 chip
->config
= config
;
263 static IIO_DEVICE_ATTR(resolution
, S_IRUGO
| S_IWUSR
,
264 adt7410_show_resolution
,
265 adt7410_store_resolution
,
268 static ssize_t
adt7410_show_id(struct device
*dev
,
269 struct device_attribute
*attr
,
272 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
273 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
277 ret
= adt7410_i2c_read_byte(chip
, ADT7410_ID
, &id
);
281 return sprintf(buf
, "device id: 0x%x\nmanufactory id: 0x%x\n",
282 id
& ADT7410_DEVICE_ID_MASK
,
283 (id
& ADT7410_MANUFACTORY_ID_MASK
) >> ADT7410_MANUFACTORY_ID_OFFSET
);
286 static IIO_DEVICE_ATTR(id
, S_IRUGO
| S_IWUSR
,
291 static ssize_t
adt7410_convert_temperature(struct adt7410_chip_info
*chip
,
296 if (chip
->config
& ADT7410_RESOLUTION
) {
297 if (data
& ADT7410_T16_VALUE_SIGN
) {
298 /* convert supplement to positive value */
299 data
= (u16
)((ADT7410_T16_VALUE_SIGN
<< 1) - (u32
)data
);
302 return sprintf(buf
, "%c%d.%.7d\n", sign
,
303 (data
>> ADT7410_T16_VALUE_FLOAT_OFFSET
),
304 (data
& ADT7410_T16_VALUE_FLOAT_MASK
) * 78125);
306 if (data
& ADT7410_T13_VALUE_SIGN
) {
307 /* convert supplement to positive value */
308 data
>>= ADT7410_T13_VALUE_OFFSET
;
309 data
= (ADT7410_T13_VALUE_SIGN
<< 1) - data
;
312 return sprintf(buf
, "%c%d.%.4d\n", sign
,
313 (data
>> ADT7410_T13_VALUE_FLOAT_OFFSET
),
314 (data
& ADT7410_T13_VALUE_FLOAT_MASK
) * 625);
318 static ssize_t
adt7410_show_value(struct device
*dev
,
319 struct device_attribute
*attr
,
322 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
323 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
329 ret
= adt7410_i2c_read_byte(chip
, ADT7410_STATUS
, &status
);
335 } while (status
& ADT7410_STAT_NOT_RDY
);
337 ret
= adt7410_i2c_read_word(chip
, ADT7410_TEMPERATURE
, &data
);
341 return adt7410_convert_temperature(chip
, data
, buf
);
344 static IIO_DEVICE_ATTR(value
, S_IRUGO
, adt7410_show_value
, NULL
, 0);
346 static struct attribute
*adt7410_attributes
[] = {
347 &iio_dev_attr_available_modes
.dev_attr
.attr
,
348 &iio_dev_attr_mode
.dev_attr
.attr
,
349 &iio_dev_attr_resolution
.dev_attr
.attr
,
350 &iio_dev_attr_id
.dev_attr
.attr
,
351 &iio_dev_attr_value
.dev_attr
.attr
,
355 static const struct attribute_group adt7410_attribute_group
= {
356 .attrs
= adt7410_attributes
,
359 static irqreturn_t
adt7410_event_handler(int irq
, void *private)
361 struct iio_dev
*indio_dev
= private;
362 struct adt7410_chip_info
*chip
= iio_priv(indio_dev
);
363 s64 timestamp
= iio_get_time_ns();
366 if (adt7410_i2c_read_byte(chip
, ADT7410_STATUS
, &status
))
369 if (status
& ADT7410_STAT_T_HIGH
)
370 iio_push_event(indio_dev
,
371 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
375 if (status
& ADT7410_STAT_T_LOW
)
376 iio_push_event(indio_dev
,
377 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
381 if (status
& ADT7410_STAT_T_CRIT
)
382 iio_push_event(indio_dev
,
383 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
391 static ssize_t
adt7410_show_event_mode(struct device
*dev
,
392 struct device_attribute
*attr
,
395 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
396 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
399 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
403 if (chip
->config
& ADT7410_EVENT_MODE
)
404 return sprintf(buf
, "interrupt\n");
406 return sprintf(buf
, "comparator\n");
409 static ssize_t
adt7410_set_event_mode(struct device
*dev
,
410 struct device_attribute
*attr
,
414 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
415 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
419 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
423 config
= chip
->config
&= ~ADT7410_EVENT_MODE
;
424 if (strcmp(buf
, "comparator") != 0)
425 config
|= ADT7410_EVENT_MODE
;
427 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, config
);
431 chip
->config
= config
;
436 static ssize_t
adt7410_show_available_event_modes(struct device
*dev
,
437 struct device_attribute
*attr
,
440 return sprintf(buf
, "comparator\ninterrupt\n");
443 static ssize_t
adt7410_show_fault_queue(struct device
*dev
,
444 struct device_attribute
*attr
,
447 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
448 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
451 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
455 return sprintf(buf
, "%d\n", chip
->config
& ADT7410_FAULT_QUEUE_MASK
);
458 static ssize_t
adt7410_set_fault_queue(struct device
*dev
,
459 struct device_attribute
*attr
,
463 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
464 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
469 ret
= strict_strtoul(buf
, 10, &data
);
473 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
477 config
= chip
->config
& ~ADT7410_FAULT_QUEUE_MASK
;
479 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, config
);
483 chip
->config
= config
;
488 static inline ssize_t
adt7410_show_t_bound(struct device
*dev
,
489 struct device_attribute
*attr
,
493 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
494 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
498 ret
= adt7410_i2c_read_word(chip
, bound_reg
, &data
);
502 return adt7410_convert_temperature(chip
, data
, buf
);
505 static inline ssize_t
adt7410_set_t_bound(struct device
*dev
,
506 struct device_attribute
*attr
,
511 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
512 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
518 pos
= strchr(buf
, '.');
520 ret
= strict_strtol(buf
, 10, &tmp1
);
522 if (ret
|| tmp1
> 127 || tmp1
< -128)
528 if (chip
->config
& ADT7410_RESOLUTION
) {
529 if (len
> ADT7410_T16_VALUE_FLOAT_OFFSET
)
530 len
= ADT7410_T16_VALUE_FLOAT_OFFSET
;
532 ret
= strict_strtol(pos
, 10, &tmp2
);
535 tmp2
= (tmp2
/ 78125) * 78125;
537 if (len
> ADT7410_T13_VALUE_FLOAT_OFFSET
)
538 len
= ADT7410_T13_VALUE_FLOAT_OFFSET
;
540 ret
= strict_strtol(pos
, 10, &tmp2
);
543 tmp2
= (tmp2
/ 625) * 625;
552 if (chip
->config
& ADT7410_RESOLUTION
) {
553 data
= (data
<< ADT7410_T16_VALUE_FLOAT_OFFSET
) |
554 (tmp2
& ADT7410_T16_VALUE_FLOAT_MASK
);
557 /* convert positive value to supplyment */
558 data
= (u16
)((ADT7410_T16_VALUE_SIGN
<< 1) - (u32
)data
);
560 data
= (data
<< ADT7410_T13_VALUE_FLOAT_OFFSET
) |
561 (tmp2
& ADT7410_T13_VALUE_FLOAT_MASK
);
564 /* convert positive value to supplyment */
565 data
= (ADT7410_T13_VALUE_SIGN
<< 1) - data
;
566 data
<<= ADT7410_T13_VALUE_OFFSET
;
569 ret
= adt7410_i2c_write_word(chip
, bound_reg
, data
);
576 static ssize_t
adt7410_show_t_alarm_high(struct device
*dev
,
577 struct device_attribute
*attr
,
580 return adt7410_show_t_bound(dev
, attr
,
581 ADT7410_T_ALARM_HIGH
, buf
);
584 static inline ssize_t
adt7410_set_t_alarm_high(struct device
*dev
,
585 struct device_attribute
*attr
,
589 return adt7410_set_t_bound(dev
, attr
,
590 ADT7410_T_ALARM_HIGH
, buf
, len
);
593 static ssize_t
adt7410_show_t_alarm_low(struct device
*dev
,
594 struct device_attribute
*attr
,
597 return adt7410_show_t_bound(dev
, attr
,
598 ADT7410_T_ALARM_LOW
, buf
);
601 static inline ssize_t
adt7410_set_t_alarm_low(struct device
*dev
,
602 struct device_attribute
*attr
,
606 return adt7410_set_t_bound(dev
, attr
,
607 ADT7410_T_ALARM_LOW
, buf
, len
);
610 static ssize_t
adt7410_show_t_crit(struct device
*dev
,
611 struct device_attribute
*attr
,
614 return adt7410_show_t_bound(dev
, attr
,
615 ADT7410_T_CRIT
, buf
);
618 static inline ssize_t
adt7410_set_t_crit(struct device
*dev
,
619 struct device_attribute
*attr
,
623 return adt7410_set_t_bound(dev
, attr
,
624 ADT7410_T_CRIT
, buf
, len
);
627 static ssize_t
adt7410_show_t_hyst(struct device
*dev
,
628 struct device_attribute
*attr
,
631 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
632 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
636 ret
= adt7410_i2c_read_byte(chip
, ADT7410_T_HYST
, &t_hyst
);
640 return sprintf(buf
, "%d\n", t_hyst
& ADT7410_T_HYST_MASK
);
643 static inline ssize_t
adt7410_set_t_hyst(struct device
*dev
,
644 struct device_attribute
*attr
,
648 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
649 struct adt7410_chip_info
*chip
= iio_priv(dev_info
);
654 ret
= strict_strtol(buf
, 10, &data
);
656 if (ret
|| data
> ADT7410_T_HYST_MASK
)
661 ret
= adt7410_i2c_write_byte(chip
, ADT7410_T_HYST
, t_hyst
);
668 static IIO_DEVICE_ATTR(event_mode
,
670 adt7410_show_event_mode
, adt7410_set_event_mode
, 0);
671 static IIO_DEVICE_ATTR(available_event_modes
,
673 adt7410_show_available_event_modes
, NULL
, 0);
674 static IIO_DEVICE_ATTR(fault_queue
,
676 adt7410_show_fault_queue
, adt7410_set_fault_queue
, 0);
677 static IIO_DEVICE_ATTR(t_alarm_high
,
679 adt7410_show_t_alarm_high
, adt7410_set_t_alarm_high
, 0);
680 static IIO_DEVICE_ATTR(t_alarm_low
,
682 adt7410_show_t_alarm_low
, adt7410_set_t_alarm_low
, 0);
683 static IIO_DEVICE_ATTR(t_crit
,
685 adt7410_show_t_crit
, adt7410_set_t_crit
, 0);
686 static IIO_DEVICE_ATTR(t_hyst
,
688 adt7410_show_t_hyst
, adt7410_set_t_hyst
, 0);
690 static struct attribute
*adt7410_event_int_attributes
[] = {
691 &iio_dev_attr_event_mode
.dev_attr
.attr
,
692 &iio_dev_attr_available_event_modes
.dev_attr
.attr
,
693 &iio_dev_attr_fault_queue
.dev_attr
.attr
,
694 &iio_dev_attr_t_alarm_high
.dev_attr
.attr
,
695 &iio_dev_attr_t_alarm_low
.dev_attr
.attr
,
696 &iio_dev_attr_t_hyst
.dev_attr
.attr
,
700 static struct attribute
*adt7410_event_ct_attributes
[] = {
701 &iio_dev_attr_event_mode
.dev_attr
.attr
,
702 &iio_dev_attr_available_event_modes
.dev_attr
.attr
,
703 &iio_dev_attr_fault_queue
.dev_attr
.attr
,
704 &iio_dev_attr_t_crit
.dev_attr
.attr
,
705 &iio_dev_attr_t_hyst
.dev_attr
.attr
,
709 static struct attribute_group adt7410_event_attribute_group
[ADT7410_IRQS
] = {
711 .attrs
= adt7410_event_int_attributes
,
714 .attrs
= adt7410_event_ct_attributes
,
719 static const struct iio_info adt7410_info
= {
720 .attrs
= &adt7410_attribute_group
,
721 .event_attrs
= adt7410_event_attribute_group
,
722 .driver_module
= THIS_MODULE
,
726 * device probe and remove
729 static int __devinit
adt7410_probe(struct i2c_client
*client
,
730 const struct i2c_device_id
*id
)
732 struct adt7410_chip_info
*chip
;
733 struct iio_dev
*indio_dev
;
735 unsigned long *adt7410_platform_data
= client
->dev
.platform_data
;
737 indio_dev
= iio_allocate_device(sizeof(*chip
));
738 if (indio_dev
== NULL
) {
742 chip
= iio_priv(indio_dev
);
743 /* this is only used for device removal purposes */
744 i2c_set_clientdata(client
, indio_dev
);
746 chip
->client
= client
;
748 indio_dev
->name
= id
->name
;
749 indio_dev
->dev
.parent
= &client
->dev
;
750 indio_dev
->info
= &adt7410_info
;
751 indio_dev
->modes
= INDIO_DIRECT_MODE
;
753 /* CT critcal temperature event. line 0 */
755 ret
= request_threaded_irq(client
->irq
,
757 &adt7410_event_handler
,
765 /* INT bound temperature alarm event. line 1 */
766 if (adt7410_platform_data
[0]) {
767 ret
= request_threaded_irq(adt7410_platform_data
[0],
769 &adt7410_event_handler
,
770 adt7410_platform_data
[1],
774 goto error_unreg_ct_irq
;
777 if (client
->irq
&& adt7410_platform_data
[0]) {
779 ret
= adt7410_i2c_read_byte(chip
, ADT7410_CONFIG
, &chip
->config
);
782 goto error_unreg_int_irq
;
785 /* set irq polarity low level */
786 chip
->config
&= ~ADT7410_CT_POLARITY
;
788 if (adt7410_platform_data
[1] & IRQF_TRIGGER_HIGH
)
789 chip
->config
|= ADT7410_INT_POLARITY
;
791 chip
->config
&= ~ADT7410_INT_POLARITY
;
793 ret
= adt7410_i2c_write_byte(chip
, ADT7410_CONFIG
, chip
->config
);
796 goto error_unreg_int_irq
;
799 ret
= iio_device_register(indio_dev
);
801 goto error_unreg_int_irq
;
803 dev_info(&client
->dev
, "%s temperature sensor registered.\n",
809 free_irq(adt7410_platform_data
[0], indio_dev
);
811 free_irq(client
->irq
, 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 iio_device_unregister(indio_dev
);
824 if (adt7410_platform_data
[0])
825 free_irq(adt7410_platform_data
[0], indio_dev
);
827 free_irq(client
->irq
, indio_dev
);
828 iio_free_device(indio_dev
);
833 static const struct i2c_device_id adt7410_id
[] = {
838 MODULE_DEVICE_TABLE(i2c
, adt7410_id
);
840 static struct i2c_driver adt7410_driver
= {
844 .probe
= adt7410_probe
,
845 .remove
= __devexit_p(adt7410_remove
),
846 .id_table
= adt7410_id
,
848 module_i2c_driver(adt7410_driver
);
850 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
851 MODULE_DESCRIPTION("Analog Devices ADT7410 digital"
852 " temperature sensor driver");
853 MODULE_LICENSE("GPL v2");