s390: fix handling of -1 in set{,fs}[gu]id16 syscalls
[pandora-kernel.git] / drivers / hwmon / adt7411.c
1 /*
2  *  Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
3  *
4  *  Copyright (C) 2008, 2010 Pengutronix
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  TODO: SPI, support for external temperature sensor
11  *        use power-down mode for suspend?, interrupt handling?
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/delay.h>
19 #include <linux/mutex.h>
20 #include <linux/jiffies.h>
21 #include <linux/i2c.h>
22 #include <linux/hwmon.h>
23 #include <linux/hwmon-sysfs.h>
24 #include <linux/slab.h>
25
26 #define ADT7411_REG_INT_TEMP_VDD_LSB            0x03
27 #define ADT7411_REG_EXT_TEMP_AIN14_LSB          0x04
28 #define ADT7411_REG_VDD_MSB                     0x06
29 #define ADT7411_REG_INT_TEMP_MSB                0x07
30 #define ADT7411_REG_EXT_TEMP_AIN1_MSB           0x08
31
32 #define ADT7411_REG_CFG1                        0x18
33 #define ADT7411_CFG1_START_MONITOR              (1 << 0)
34 #define ADT7411_CFG1_RESERVED_BIT3              (1 << 3)
35
36 #define ADT7411_REG_CFG2                        0x19
37 #define ADT7411_CFG2_DISABLE_AVG                (1 << 5)
38
39 #define ADT7411_REG_CFG3                        0x1a
40 #define ADT7411_CFG3_ADC_CLK_225                (1 << 0)
41 #define ADT7411_CFG3_REF_VDD                    (1 << 4)
42
43 #define ADT7411_REG_DEVICE_ID                   0x4d
44 #define ADT7411_REG_MANUFACTURER_ID             0x4e
45
46 #define ADT7411_DEVICE_ID                       0x2
47 #define ADT7411_MANUFACTURER_ID                 0x41
48
49 static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };
50
51 struct adt7411_data {
52         struct mutex device_lock;       /* for "atomic" device accesses */
53         struct mutex update_lock;
54         unsigned long next_update;
55         int vref_cached;
56         struct device *hwmon_dev;
57 };
58
59 /*
60  * When reading a register containing (up to 4) lsb, all associated
61  * msb-registers get locked by the hardware. After _one_ of those msb is read,
62  * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
63  * is protected here with a mutex, too.
64  */
65 static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
66                                 u8 msb_reg, u8 lsb_shift)
67 {
68         struct adt7411_data *data = i2c_get_clientdata(client);
69         int val, tmp;
70
71         mutex_lock(&data->device_lock);
72
73         val = i2c_smbus_read_byte_data(client, lsb_reg);
74         if (val < 0)
75                 goto exit_unlock;
76
77         tmp = (val >> lsb_shift) & 3;
78         val = i2c_smbus_read_byte_data(client, msb_reg);
79
80         if (val >= 0)
81                 val = (val << 2) | tmp;
82
83  exit_unlock:
84         mutex_unlock(&data->device_lock);
85
86         return val;
87 }
88
89 static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
90                                 bool flag)
91 {
92         struct adt7411_data *data = i2c_get_clientdata(client);
93         int ret, val;
94
95         mutex_lock(&data->device_lock);
96
97         ret = i2c_smbus_read_byte_data(client, reg);
98         if (ret < 0)
99                 goto exit_unlock;
100
101         if (flag)
102                 val = ret | bit;
103         else
104                 val = ret & ~bit;
105
106         ret = i2c_smbus_write_byte_data(client, reg, val);
107
108  exit_unlock:
109         mutex_unlock(&data->device_lock);
110         return ret;
111 }
112
113 static ssize_t adt7411_show_vdd(struct device *dev,
114                                 struct device_attribute *attr, char *buf)
115 {
116         struct i2c_client *client = to_i2c_client(dev);
117         int ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
118                         ADT7411_REG_VDD_MSB, 2);
119
120         return ret < 0 ? ret : sprintf(buf, "%u\n", ret * 7000 / 1024);
121 }
122
123 static ssize_t adt7411_show_temp(struct device *dev,
124                         struct device_attribute *attr, char *buf)
125 {
126         struct i2c_client *client = to_i2c_client(dev);
127         int val = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
128                         ADT7411_REG_INT_TEMP_MSB, 0);
129
130         if (val < 0)
131                 return val;
132
133         val = val & 0x200 ? val - 0x400 : val; /* 10 bit signed */
134
135         return sprintf(buf, "%d\n", val * 250);
136 }
137
138 static ssize_t adt7411_show_input(struct device *dev,
139                                   struct device_attribute *attr, char *buf)
140 {
141         int nr = to_sensor_dev_attr(attr)->index;
142         struct i2c_client *client = to_i2c_client(dev);
143         struct adt7411_data *data = i2c_get_clientdata(client);
144         int val;
145         u8 lsb_reg, lsb_shift;
146
147         mutex_lock(&data->update_lock);
148         if (time_after_eq(jiffies, data->next_update)) {
149                 val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
150                 if (val < 0)
151                         goto exit_unlock;
152
153                 if (val & ADT7411_CFG3_REF_VDD) {
154                         val = adt7411_read_10_bit(client,
155                                         ADT7411_REG_INT_TEMP_VDD_LSB,
156                                         ADT7411_REG_VDD_MSB, 2);
157                         if (val < 0)
158                                 goto exit_unlock;
159
160                         data->vref_cached = val * 7000 / 1024;
161                 } else {
162                         data->vref_cached = 2250;
163                 }
164
165                 data->next_update = jiffies + HZ;
166         }
167
168         lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
169         lsb_shift = 2 * (nr & 0x03);
170         val = adt7411_read_10_bit(client, lsb_reg,
171                         ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, lsb_shift);
172         if (val < 0)
173                 goto exit_unlock;
174
175         val = sprintf(buf, "%u\n", val * data->vref_cached / 1024);
176  exit_unlock:
177         mutex_unlock(&data->update_lock);
178         return val;
179 }
180
181 static ssize_t adt7411_show_bit(struct device *dev,
182                                 struct device_attribute *attr, char *buf)
183 {
184         struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
185         struct i2c_client *client = to_i2c_client(dev);
186         int ret = i2c_smbus_read_byte_data(client, attr2->index);
187
188         return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
189 }
190
191 static ssize_t adt7411_set_bit(struct device *dev,
192                                struct device_attribute *attr, const char *buf,
193                                size_t count)
194 {
195         struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
196         struct i2c_client *client = to_i2c_client(dev);
197         struct adt7411_data *data = i2c_get_clientdata(client);
198         int ret;
199         unsigned long flag;
200
201         ret = strict_strtoul(buf, 0, &flag);
202         if (ret || flag > 1)
203                 return -EINVAL;
204
205         ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
206
207         /* force update */
208         mutex_lock(&data->update_lock);
209         data->next_update = jiffies;
210         mutex_unlock(&data->update_lock);
211
212         return ret < 0 ? ret : count;
213 }
214
215 #define ADT7411_BIT_ATTR(__name, __reg, __bit) \
216         SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
217         adt7411_set_bit, __bit, __reg)
218
219 static DEVICE_ATTR(temp1_input, S_IRUGO, adt7411_show_temp, NULL);
220 static DEVICE_ATTR(in0_input, S_IRUGO, adt7411_show_vdd, NULL);
221 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, adt7411_show_input, NULL, 0);
222 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, adt7411_show_input, NULL, 1);
223 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, adt7411_show_input, NULL, 2);
224 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, adt7411_show_input, NULL, 3);
225 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, adt7411_show_input, NULL, 4);
226 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, adt7411_show_input, NULL, 5);
227 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, adt7411_show_input, NULL, 6);
228 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, adt7411_show_input, NULL, 7);
229 static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
230 static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
231 static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
232
233 static struct attribute *adt7411_attrs[] = {
234         &dev_attr_temp1_input.attr,
235         &dev_attr_in0_input.attr,
236         &sensor_dev_attr_in1_input.dev_attr.attr,
237         &sensor_dev_attr_in2_input.dev_attr.attr,
238         &sensor_dev_attr_in3_input.dev_attr.attr,
239         &sensor_dev_attr_in4_input.dev_attr.attr,
240         &sensor_dev_attr_in5_input.dev_attr.attr,
241         &sensor_dev_attr_in6_input.dev_attr.attr,
242         &sensor_dev_attr_in7_input.dev_attr.attr,
243         &sensor_dev_attr_in8_input.dev_attr.attr,
244         &sensor_dev_attr_no_average.dev_attr.attr,
245         &sensor_dev_attr_fast_sampling.dev_attr.attr,
246         &sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
247         NULL
248 };
249
250 static const struct attribute_group adt7411_attr_grp = {
251         .attrs = adt7411_attrs,
252 };
253
254 static int adt7411_detect(struct i2c_client *client,
255                           struct i2c_board_info *info)
256 {
257         int val;
258
259         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
260                 return -ENODEV;
261
262         val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
263         if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
264                 dev_dbg(&client->dev, "Wrong manufacturer ID. Got %d, "
265                         "expected %d\n", val, ADT7411_MANUFACTURER_ID);
266                 return -ENODEV;
267         }
268
269         val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
270         if (val < 0 || val != ADT7411_DEVICE_ID) {
271                 dev_dbg(&client->dev, "Wrong device ID. Got %d, "
272                         "expected %d\n", val, ADT7411_DEVICE_ID);
273                 return -ENODEV;
274         }
275
276         strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
277
278         return 0;
279 }
280
281 static int __devinit adt7411_probe(struct i2c_client *client,
282                                    const struct i2c_device_id *id)
283 {
284         struct adt7411_data *data;
285         int ret;
286
287         data = kzalloc(sizeof(*data), GFP_KERNEL);
288         if (!data)
289                 return -ENOMEM;
290
291         i2c_set_clientdata(client, data);
292         mutex_init(&data->device_lock);
293         mutex_init(&data->update_lock);
294
295         /* According to the datasheet, we must only write 1 to bit 3 */
296         ret = adt7411_modify_bit(client, ADT7411_REG_CFG1,
297                                  ADT7411_CFG1_RESERVED_BIT3
298                                  | ADT7411_CFG1_START_MONITOR, 1);
299         if (ret < 0)
300                 goto exit_free;
301
302         /* force update on first occasion */
303         data->next_update = jiffies;
304
305         ret = sysfs_create_group(&client->dev.kobj, &adt7411_attr_grp);
306         if (ret)
307                 goto exit_free;
308
309         data->hwmon_dev = hwmon_device_register(&client->dev);
310         if (IS_ERR(data->hwmon_dev)) {
311                 ret = PTR_ERR(data->hwmon_dev);
312                 goto exit_remove;
313         }
314
315         dev_info(&client->dev, "successfully registered\n");
316
317         return 0;
318
319  exit_remove:
320         sysfs_remove_group(&client->dev.kobj, &adt7411_attr_grp);
321  exit_free:
322         kfree(data);
323         return ret;
324 }
325
326 static int __devexit adt7411_remove(struct i2c_client *client)
327 {
328         struct adt7411_data *data = i2c_get_clientdata(client);
329
330         hwmon_device_unregister(data->hwmon_dev);
331         sysfs_remove_group(&client->dev.kobj, &adt7411_attr_grp);
332         kfree(data);
333         return 0;
334 }
335
336 static const struct i2c_device_id adt7411_id[] = {
337         { "adt7411", 0 },
338         { }
339 };
340 MODULE_DEVICE_TABLE(i2c, adt7411_id);
341
342 static struct i2c_driver adt7411_driver = {
343         .driver         = {
344                 .name           = "adt7411",
345         },
346         .probe  = adt7411_probe,
347         .remove = __devexit_p(adt7411_remove),
348         .id_table = adt7411_id,
349         .detect = adt7411_detect,
350         .address_list = normal_i2c,
351         .class = I2C_CLASS_HWMON,
352 };
353
354 static int __init sensors_adt7411_init(void)
355 {
356         return i2c_add_driver(&adt7411_driver);
357 }
358 module_init(sensors_adt7411_init)
359
360 static void __exit sensors_adt7411_exit(void)
361 {
362         i2c_del_driver(&adt7411_driver);
363 }
364 module_exit(sensors_adt7411_exit)
365
366 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
367         "Wolfram Sang <w.sang@pengutronix.de>");
368 MODULE_DESCRIPTION("ADT7411 driver");
369 MODULE_LICENSE("GPL v2");