device and dynamic_debug: Use dev_vprintk_emit and dev_printk_emit
[linux-2.6/libata-dev.git] / drivers / hwmon / ad7314.c
blobcfec802cf9ca949b705d24a1d862a702af223193
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 temperature masks
23 #define AD7314_TEMP_MASK 0x7FE0
24 #define AD7314_TEMP_SHIFT 5
27 * ADT7301 and ADT7302 temperature masks
29 #define ADT7301_TEMP_MASK 0x3FFF
31 enum ad7314_variant {
32 adt7301,
33 adt7302,
34 ad7314,
37 struct ad7314_data {
38 struct spi_device *spi_dev;
39 struct device *hwmon_dev;
40 u16 rx ____cacheline_aligned;
43 static int ad7314_spi_read(struct ad7314_data *chip)
45 int ret;
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;
53 return be16_to_cpu(chip->rx);
56 static ssize_t ad7314_show_temperature(struct device *dev,
57 struct device_attribute *attr,
58 char *buf)
60 struct ad7314_data *chip = dev_get_drvdata(dev);
61 s16 data;
62 int ret;
64 ret = ad7314_spi_read(chip);
65 if (ret < 0)
66 return ret;
67 switch (spi_get_device_id(chip->spi_dev)->driver_data) {
68 case ad7314:
69 data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT;
70 data = (data << 6) >> 6;
72 return sprintf(buf, "%d\n", 250 * data);
73 case adt7301:
74 case adt7302:
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
80 data = ret & ADT7301_TEMP_MASK;
81 data = (data << 2) >> 2;
83 return sprintf(buf, "%d\n",
84 DIV_ROUND_CLOSEST(data * 3125, 100));
85 default:
86 return -EINVAL;
90 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
91 ad7314_show_temperature, NULL, 0);
93 static struct attribute *ad7314_attributes[] = {
94 &sensor_dev_attr_temp1_input.dev_attr.attr,
95 NULL,
98 static const struct attribute_group ad7314_group = {
99 .attrs = ad7314_attributes,
102 static int __devinit ad7314_probe(struct spi_device *spi_dev)
104 int ret;
105 struct ad7314_data *chip;
107 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
108 if (chip == NULL) {
109 ret = -ENOMEM;
110 goto error_ret;
112 dev_set_drvdata(&spi_dev->dev, chip);
114 ret = sysfs_create_group(&spi_dev->dev.kobj, &ad7314_group);
115 if (ret < 0)
116 goto error_free_chip;
117 chip->hwmon_dev = hwmon_device_register(&spi_dev->dev);
118 if (IS_ERR(chip->hwmon_dev)) {
119 ret = PTR_ERR(chip->hwmon_dev);
120 goto error_remove_group;
122 chip->spi_dev = spi_dev;
124 return 0;
125 error_remove_group:
126 sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
127 error_free_chip:
128 kfree(chip);
129 error_ret:
130 return ret;
133 static int __devexit ad7314_remove(struct spi_device *spi_dev)
135 struct ad7314_data *chip = dev_get_drvdata(&spi_dev->dev);
137 hwmon_device_unregister(chip->hwmon_dev);
138 sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
139 kfree(chip);
141 return 0;
144 static const struct spi_device_id ad7314_id[] = {
145 { "adt7301", adt7301 },
146 { "adt7302", adt7302 },
147 { "ad7314", ad7314 },
150 MODULE_DEVICE_TABLE(spi, ad7314_id);
152 static struct spi_driver ad7314_driver = {
153 .driver = {
154 .name = "ad7314",
155 .owner = THIS_MODULE,
157 .probe = ad7314_probe,
158 .remove = __devexit_p(ad7314_remove),
159 .id_table = ad7314_id,
162 module_spi_driver(ad7314_driver);
164 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
165 MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital"
166 " temperature sensor driver");
167 MODULE_LICENSE("GPL v2");