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>
19 #include <linux/bitops.h>
22 * AD7314 temperature masks
24 #define AD7314_TEMP_MASK 0x7FE0
25 #define AD7314_TEMP_SHIFT 5
28 * ADT7301 and ADT7302 temperature masks
30 #define ADT7301_TEMP_MASK 0x3FFF
39 struct spi_device
*spi_dev
;
40 struct device
*hwmon_dev
;
41 u16 rx ____cacheline_aligned
;
44 static int ad7314_spi_read(struct ad7314_data
*chip
)
48 ret
= spi_read(chip
->spi_dev
, (u8
*)&chip
->rx
, sizeof(chip
->rx
));
50 dev_err(&chip
->spi_dev
->dev
, "SPI read error\n");
54 return be16_to_cpu(chip
->rx
);
57 static ssize_t
ad7314_show_temperature(struct device
*dev
,
58 struct device_attribute
*attr
,
61 struct ad7314_data
*chip
= dev_get_drvdata(dev
);
65 ret
= ad7314_spi_read(chip
);
68 switch (spi_get_device_id(chip
->spi_dev
)->driver_data
) {
70 data
= (ret
& AD7314_TEMP_MASK
) >> AD7314_TEMP_SHIFT
;
71 data
= sign_extend32(data
, 9);
73 return sprintf(buf
, "%d\n", 250 * data
);
77 * Documented as a 13 bit twos complement register
78 * with a sign bit - which is a 14 bit 2's complement
79 * register. 1lsb - 31.25 milli degrees centigrade
81 data
= ret
& ADT7301_TEMP_MASK
;
82 data
= sign_extend32(data
, 13);
84 return sprintf(buf
, "%d\n",
85 DIV_ROUND_CLOSEST(data
* 3125, 100));
91 static ssize_t
ad7314_show_name(struct device
*dev
,
92 struct device_attribute
*devattr
, char *buf
)
94 return sprintf(buf
, "%s\n", to_spi_device(dev
)->modalias
);
97 static DEVICE_ATTR(name
, S_IRUGO
, ad7314_show_name
, NULL
);
98 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
,
99 ad7314_show_temperature
, NULL
, 0);
101 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 ad7314_probe(struct spi_device
*spi_dev
)
114 struct ad7314_data
*chip
;
116 chip
= devm_kzalloc(&spi_dev
->dev
, sizeof(*chip
), GFP_KERNEL
);
120 spi_set_drvdata(spi_dev
, chip
);
122 ret
= sysfs_create_group(&spi_dev
->dev
.kobj
, &ad7314_group
);
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
;
131 chip
->spi_dev
= spi_dev
;
135 sysfs_remove_group(&spi_dev
->dev
.kobj
, &ad7314_group
);
139 static int ad7314_remove(struct spi_device
*spi_dev
)
141 struct ad7314_data
*chip
= spi_get_drvdata(spi_dev
);
143 hwmon_device_unregister(chip
->hwmon_dev
);
144 sysfs_remove_group(&spi_dev
->dev
.kobj
, &ad7314_group
);
149 static const struct spi_device_id ad7314_id
[] = {
150 { "adt7301", adt7301
},
151 { "adt7302", adt7302
},
152 { "ad7314", ad7314
},
155 MODULE_DEVICE_TABLE(spi
, ad7314_id
);
157 static struct spi_driver ad7314_driver
= {
161 .probe
= ad7314_probe
,
162 .remove
= ad7314_remove
,
163 .id_table
= ad7314_id
,
166 module_spi_driver(ad7314_driver
);
168 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
169 MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital temperature sensor driver");
170 MODULE_LICENSE("GPL v2");