Linux 3.4.102
[linux/fpc-iii.git] / drivers / hwmon / ad7314.c
blob9815f9cc8f7a5f00f2c14f45950450146d641c9f
1 /*
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>
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>
21 * AD7314 power mode
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
38 enum ad7314_variant {
39 adt7301,
40 adt7302,
41 ad7314,
44 struct ad7314_data {
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)
52 int ret;
54 ret = spi_read(chip->spi_dev, (u8 *)&chip->rx, sizeof(chip->rx));
55 if (ret < 0) {
56 dev_err(&chip->spi_dev->dev, "SPI read error\n");
57 return ret;
60 return be16_to_cpu(chip->rx);
63 static ssize_t ad7314_show_temperature(struct device *dev,
64 struct device_attribute *attr,
65 char *buf)
67 struct ad7314_data *chip = dev_get_drvdata(dev);
68 s16 data;
69 int ret;
71 ret = ad7314_spi_read(chip);
72 if (ret < 0)
73 return ret;
74 switch (spi_get_device_id(chip->spi_dev)->driver_data) {
75 case ad7314:
76 data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_OFFSET;
77 data = (data << 6) >> 6;
79 return sprintf(buf, "%d\n", 250 * data);
80 case adt7301:
81 case adt7302:
83 * Documented as a 13 bit twos complement register
84 * with a sign bit - which is a 14 bit 2's complement
85 * register. 1lsb - 31.25 milli degrees centigrade
87 data = ret & ADT7301_TEMP_MASK;
88 data = (data << 2) >> 2;
90 return sprintf(buf, "%d\n",
91 DIV_ROUND_CLOSEST(data * 3125, 100));
92 default:
93 return -EINVAL;
97 static ssize_t ad7314_show_name(struct device *dev,
98 struct device_attribute *devattr, char *buf)
100 return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
103 static DEVICE_ATTR(name, S_IRUGO, ad7314_show_name, NULL);
104 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
105 ad7314_show_temperature, NULL, 0);
107 static struct attribute *ad7314_attributes[] = {
108 &dev_attr_name.attr,
109 &sensor_dev_attr_temp1_input.dev_attr.attr,
110 NULL,
113 static const struct attribute_group ad7314_group = {
114 .attrs = ad7314_attributes,
117 static int __devinit ad7314_probe(struct spi_device *spi_dev)
119 int ret;
120 struct ad7314_data *chip;
122 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
123 if (chip == NULL) {
124 ret = -ENOMEM;
125 goto error_ret;
127 dev_set_drvdata(&spi_dev->dev, chip);
129 ret = sysfs_create_group(&spi_dev->dev.kobj, &ad7314_group);
130 if (ret < 0)
131 goto error_free_chip;
132 chip->hwmon_dev = hwmon_device_register(&spi_dev->dev);
133 if (IS_ERR(chip->hwmon_dev)) {
134 ret = PTR_ERR(chip->hwmon_dev);
135 goto error_remove_group;
137 chip->spi_dev = spi_dev;
139 return 0;
140 error_remove_group:
141 sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
142 error_free_chip:
143 kfree(chip);
144 error_ret:
145 return ret;
148 static int __devexit ad7314_remove(struct spi_device *spi_dev)
150 struct ad7314_data *chip = dev_get_drvdata(&spi_dev->dev);
152 hwmon_device_unregister(chip->hwmon_dev);
153 sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
154 kfree(chip);
156 return 0;
159 static const struct spi_device_id ad7314_id[] = {
160 { "adt7301", adt7301 },
161 { "adt7302", adt7302 },
162 { "ad7314", ad7314 },
165 MODULE_DEVICE_TABLE(spi, ad7314_id);
167 static struct spi_driver ad7314_driver = {
168 .driver = {
169 .name = "ad7314",
170 .owner = THIS_MODULE,
172 .probe = ad7314_probe,
173 .remove = __devexit_p(ad7314_remove),
174 .id_table = ad7314_id,
177 module_spi_driver(ad7314_driver);
179 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
180 MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital"
181 " temperature sensor driver");
182 MODULE_LICENSE("GPL v2");