RDMA/ucma: Correct option size check using optlen
[pandora-kernel.git] / drivers / hwmon / ad7314.c
1 /*
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>
19
20 /*
21  * AD7314 power mode
22  */
23 #define AD7314_PD               0x2000
24
25 /*
26  * AD7314 temperature masks
27  */
28 #define AD7314_TEMP_SIGN                0x200
29 #define AD7314_TEMP_MASK                0x7FE0
30 #define AD7314_TEMP_OFFSET              5
31
32 /*
33  * ADT7301 and ADT7302 temperature masks
34  */
35 #define ADT7301_TEMP_SIGN               0x2000
36 #define ADT7301_TEMP_MASK               0x3FFF
37
38 enum ad7314_variant {
39         adt7301,
40         adt7302,
41         ad7314,
42 };
43
44 struct ad7314_data {
45         struct spi_device       *spi_dev;
46         struct device           *hwmon_dev;
47         u16 rx ____cacheline_aligned;
48 };
49
50 static int ad7314_spi_read(struct ad7314_data *chip, s16 *data)
51 {
52         int ret;
53
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;
58         }
59
60         *data = be16_to_cpu(chip->rx);
61
62         return ret;
63 }
64
65 static ssize_t ad7314_show_temperature(struct device *dev,
66                 struct device_attribute *attr,
67                 char *buf)
68 {
69         struct ad7314_data *chip = dev_get_drvdata(dev);
70         s16 data;
71         int ret;
72
73         ret = ad7314_spi_read(chip, &data);
74         if (ret < 0)
75                 return ret;
76         switch (spi_get_device_id(chip->spi_dev)->driver_data) {
77         case ad7314:
78                 data = (data & AD7314_TEMP_MASK) >> AD7314_TEMP_OFFSET;
79                 data = (data << 6) >> 6;
80
81                 return sprintf(buf, "%d\n", 250 * data);
82         case adt7301:
83         case adt7302:
84                 /*
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
88                  */
89                 data &= ADT7301_TEMP_MASK;
90                 data = (data << 2) >> 2;
91
92                 return sprintf(buf, "%d\n",
93                                DIV_ROUND_CLOSEST(data * 3125, 100));
94         default:
95                 return -EINVAL;
96         }
97 }
98
99 static ssize_t ad7314_show_name(struct device *dev,
100                                 struct device_attribute *devattr, char *buf)
101 {
102         return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
103 }
104
105 static DEVICE_ATTR(name, S_IRUGO, ad7314_show_name, NULL);
106 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
107                           ad7314_show_temperature, NULL, 0);
108
109 static struct attribute *ad7314_attributes[] = {
110         &dev_attr_name.attr,
111         &sensor_dev_attr_temp1_input.dev_attr.attr,
112         NULL,
113 };
114
115 static const struct attribute_group ad7314_group = {
116         .attrs = ad7314_attributes,
117 };
118
119 static int __devinit ad7314_probe(struct spi_device *spi_dev)
120 {
121         int ret;
122         struct ad7314_data *chip;
123
124         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
125         if (chip == NULL) {
126                 ret = -ENOMEM;
127                 goto error_ret;
128         }
129         dev_set_drvdata(&spi_dev->dev, chip);
130
131         ret = sysfs_create_group(&spi_dev->dev.kobj, &ad7314_group);
132         if (ret < 0)
133                 goto error_free_chip;
134         chip->hwmon_dev = hwmon_device_register(&spi_dev->dev);
135         if (IS_ERR(chip->hwmon_dev)) {
136                 ret = PTR_ERR(chip->hwmon_dev);
137                 goto error_remove_group;
138         }
139
140         return 0;
141 error_remove_group:
142         sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
143 error_free_chip:
144         kfree(chip);
145 error_ret:
146         return ret;
147 }
148
149 static int __devexit ad7314_remove(struct spi_device *spi_dev)
150 {
151         struct ad7314_data *chip = dev_get_drvdata(&spi_dev->dev);
152
153         hwmon_device_unregister(chip->hwmon_dev);
154         sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
155         kfree(chip);
156
157         return 0;
158 }
159
160 static const struct spi_device_id ad7314_id[] = {
161         { "adt7301", adt7301 },
162         { "adt7302", adt7302 },
163         { "ad7314", ad7314 },
164         { }
165 };
166 MODULE_DEVICE_TABLE(spi, ad7314_id);
167
168 static struct spi_driver ad7314_driver = {
169         .driver = {
170                 .name = "ad7314",
171                 .owner = THIS_MODULE,
172         },
173         .probe = ad7314_probe,
174         .remove = __devexit_p(ad7314_remove),
175         .id_table = ad7314_id,
176 };
177
178 static __init int ad7314_init(void)
179 {
180         return spi_register_driver(&ad7314_driver);
181 }
182 module_init(ad7314_init);
183
184 static __exit void ad7314_exit(void)
185 {
186         spi_unregister_driver(&ad7314_driver);
187 }
188 module_exit(ad7314_exit);
189
190 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
191 MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital"
192                         " temperature sensor driver");
193 MODULE_LICENSE("GPL v2");