blob: 8ea35932fbaa2cd648a217c18fddea8cdd47f1a1 [file] [log] [blame]
Jonathan Cameron4f3a6592011-09-29 12:50:04 -04001/*
2 * AD7314 digital temperature sensor driver for AD7314, ADT7301 and ADT7302
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 *
8 * Conversion to hwmon from IIO done by Jonathan Cameron <jic23@cam.ac.uk>
9 */
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>
Rasmus Villemoes984faa12015-01-22 23:44:13 +010019#include <linux/bitops.h>
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040020
21/*
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040022 * AD7314 temperature masks
23 */
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040024#define AD7314_TEMP_MASK 0x7FE0
Guenter Roeck1137a9a2012-04-22 00:22:00 -040025#define AD7314_TEMP_SHIFT 5
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040026
27/*
28 * ADT7301 and ADT7302 temperature masks
29 */
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040030#define ADT7301_TEMP_MASK 0x3FFF
31
32enum ad7314_variant {
33 adt7301,
34 adt7302,
35 ad7314,
36};
37
38struct ad7314_data {
39 struct spi_device *spi_dev;
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040040 u16 rx ____cacheline_aligned;
41};
42
Guenter Roeckeae14152012-04-20 11:39:17 -040043static int ad7314_spi_read(struct ad7314_data *chip)
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040044{
45 int ret;
46
47 ret = spi_read(chip->spi_dev, (u8 *)&chip->rx, sizeof(chip->rx));
48 if (ret < 0) {
49 dev_err(&chip->spi_dev->dev, "SPI read error\n");
50 return ret;
51 }
52
Guenter Roeckeae14152012-04-20 11:39:17 -040053 return be16_to_cpu(chip->rx);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040054}
55
56static ssize_t ad7314_show_temperature(struct device *dev,
57 struct device_attribute *attr,
58 char *buf)
59{
60 struct ad7314_data *chip = dev_get_drvdata(dev);
61 s16 data;
62 int ret;
63
Guenter Roeckeae14152012-04-20 11:39:17 -040064 ret = ad7314_spi_read(chip);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040065 if (ret < 0)
66 return ret;
67 switch (spi_get_device_id(chip->spi_dev)->driver_data) {
68 case ad7314:
Guenter Roeck1137a9a2012-04-22 00:22:00 -040069 data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT;
Rasmus Villemoes984faa12015-01-22 23:44:13 +010070 data = sign_extend32(data, 9);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040071
72 return sprintf(buf, "%d\n", 250 * data);
73 case adt7301:
74 case adt7302:
75 /*
76 * Documented as a 13 bit twos complement register
77 * with a sign bit - which is a 14 bit 2's complement
78 * register. 1lsb - 31.25 milli degrees centigrade
79 */
Guenter Roeckeae14152012-04-20 11:39:17 -040080 data = ret & ADT7301_TEMP_MASK;
Rasmus Villemoes984faa12015-01-22 23:44:13 +010081 data = sign_extend32(data, 13);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040082
83 return sprintf(buf, "%d\n",
84 DIV_ROUND_CLOSEST(data * 3125, 100));
85 default:
86 return -EINVAL;
87 }
88}
89
90static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
91 ad7314_show_temperature, NULL, 0);
92
Axel Lin157926c2014-07-01 22:29:28 +080093static struct attribute *ad7314_attrs[] = {
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040094 &sensor_dev_attr_temp1_input.dev_attr.attr,
95 NULL,
96};
97
Axel Lin157926c2014-07-01 22:29:28 +080098ATTRIBUTE_GROUPS(ad7314);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040099
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500100static int ad7314_probe(struct spi_device *spi_dev)
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400101{
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400102 struct ad7314_data *chip;
Axel Lin157926c2014-07-01 22:29:28 +0800103 struct device *hwmon_dev;
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400104
Guenter Roeckbe923042012-06-02 09:57:57 -0700105 chip = devm_kzalloc(&spi_dev->dev, sizeof(*chip), GFP_KERNEL);
106 if (chip == NULL)
107 return -ENOMEM;
108
Graeme Smechere16de912012-04-03 19:42:21 -0400109 chip->spi_dev = spi_dev;
Axel Lin157926c2014-07-01 22:29:28 +0800110 hwmon_dev = devm_hwmon_device_register_with_groups(&spi_dev->dev,
111 spi_dev->modalias,
112 chip, ad7314_groups);
113 return PTR_ERR_OR_ZERO(hwmon_dev);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400114}
115
116static const struct spi_device_id ad7314_id[] = {
117 { "adt7301", adt7301 },
118 { "adt7302", adt7302 },
119 { "ad7314", ad7314 },
120 { }
121};
122MODULE_DEVICE_TABLE(spi, ad7314_id);
123
124static struct spi_driver ad7314_driver = {
125 .driver = {
126 .name = "ad7314",
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400127 },
128 .probe = ad7314_probe,
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400129 .id_table = ad7314_id,
130};
131
Axel Lin91efffe2012-01-20 15:53:47 +0800132module_spi_driver(ad7314_driver);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400133
134MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
Guenter Roeckb55f3752013-01-10 10:01:24 -0800135MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital temperature sensor driver");
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400136MODULE_LICENSE("GPL v2");