2 * AD7314 digital temperature sensor driver for AD7314, ADT7301 and ADT7302
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
8 * Conversion to hwmon from IIO done by Jonathan Cameron <jic23@cam.ac.uk>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/spi/spi.h>
15 #include <linux/module.h>
16 #include <linux/err.h>
17 #include <linux/hwmon.h>
18 #include <linux/hwmon-sysfs.h>
23 #define AD7314_PD 0x2000
26 * AD7314 temperature masks
28 #define AD7314_TEMP_SIGN 0x200
29 #define AD7314_TEMP_MASK 0x7FE0
30 #define AD7314_TEMP_OFFSET 5
33 * ADT7301 and ADT7302 temperature masks
35 #define ADT7301_TEMP_SIGN 0x2000
36 #define ADT7301_TEMP_MASK 0x3FFF
45 struct spi_device
*spi_dev
;
46 struct device
*hwmon_dev
;
47 u16 rx ____cacheline_aligned
;
50 static int ad7314_spi_read(struct ad7314_data
*chip
, s16
*data
)
54 ret
= spi_read(chip
->spi_dev
, (u8
*)&chip
->rx
, sizeof(chip
->rx
));
56 dev_err(&chip
->spi_dev
->dev
, "SPI read error\n");
60 *data
= be16_to_cpu(chip
->rx
);
65 static ssize_t
ad7314_show_temperature(struct device
*dev
,
66 struct device_attribute
*attr
,
69 struct ad7314_data
*chip
= dev_get_drvdata(dev
);
73 ret
= ad7314_spi_read(chip
, &data
);
76 switch (spi_get_device_id(chip
->spi_dev
)->driver_data
) {
78 data
= (data
& AD7314_TEMP_MASK
) >> AD7314_TEMP_OFFSET
;
79 data
= (data
<< 6) >> 6;
81 return sprintf(buf
, "%d\n", 250 * data
);
85 * Documented as a 13 bit twos complement register
86 * with a sign bit - which is a 14 bit 2's complement
87 * register. 1lsb - 31.25 milli degrees centigrade
89 data
&= ADT7301_TEMP_MASK
;
90 data
= (data
<< 2) >> 2;
92 return sprintf(buf
, "%d\n",
93 DIV_ROUND_CLOSEST(data
* 3125, 100));
99 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
,
100 ad7314_show_temperature
, NULL
, 0);
102 static struct attribute
*ad7314_attributes
[] = {
103 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
107 static const struct attribute_group ad7314_group
= {
108 .attrs
= ad7314_attributes
,
111 static int __devinit
ad7314_probe(struct spi_device
*spi_dev
)
114 struct ad7314_data
*chip
;
116 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
121 dev_set_drvdata(&spi_dev
->dev
, chip
);
123 ret
= sysfs_create_group(&spi_dev
->dev
.kobj
, &ad7314_group
);
125 goto error_free_chip
;
126 chip
->hwmon_dev
= hwmon_device_register(&spi_dev
->dev
);
127 if (IS_ERR(chip
->hwmon_dev
)) {
128 ret
= PTR_ERR(chip
->hwmon_dev
);
129 goto error_remove_group
;
134 sysfs_remove_group(&spi_dev
->dev
.kobj
, &ad7314_group
);
141 static int __devexit
ad7314_remove(struct spi_device
*spi_dev
)
143 struct ad7314_data
*chip
= dev_get_drvdata(&spi_dev
->dev
);
145 hwmon_device_unregister(chip
->hwmon_dev
);
146 sysfs_remove_group(&spi_dev
->dev
.kobj
, &ad7314_group
);
152 static const struct spi_device_id ad7314_id
[] = {
153 { "adt7301", adt7301
},
154 { "adt7302", adt7302
},
155 { "ad7314", ad7314
},
158 MODULE_DEVICE_TABLE(spi
, ad7314_id
);
160 static struct spi_driver ad7314_driver
= {
163 .owner
= THIS_MODULE
,
165 .probe
= ad7314_probe
,
166 .remove
= __devexit_p(ad7314_remove
),
167 .id_table
= ad7314_id
,
170 static __init
int ad7314_init(void)
172 return spi_register_driver(&ad7314_driver
);
174 module_init(ad7314_init
);
176 static __exit
void ad7314_exit(void)
178 spi_unregister_driver(&ad7314_driver
);
180 module_exit(ad7314_exit
);
182 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
183 MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital"
184 " temperature sensor driver");
185 MODULE_LICENSE("GPL v2");