hwmon: (lm90) Restore configuration on exit
[linux-2.6.git] / drivers / hwmon / smsc47m192.c
1 /*
2     smsc47m192.c - Support for hardware monitoring block of
3                    SMSC LPC47M192 and compatible Super I/O chips
4
5     Copyright (C) 2006  Hartmut Rick <linux@rick.claranet.de>
6
7     Derived from lm78.c and other chip drivers.
8
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/hwmon-vid.h>
32 #include <linux/err.h>
33 #include <linux/sysfs.h>
34 #include <linux/mutex.h>
35
36 /* Addresses to scan */
37 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
38
39 /* SMSC47M192 registers */
40 #define SMSC47M192_REG_IN(nr)           ((nr)<6 ? (0x20 + (nr)) : \
41                                         (0x50 + (nr) - 6))
42 #define SMSC47M192_REG_IN_MAX(nr)       ((nr)<6 ? (0x2b + (nr) * 2) : \
43                                         (0x54 + (((nr) - 6) * 2)))
44 #define SMSC47M192_REG_IN_MIN(nr)       ((nr)<6 ? (0x2c + (nr) * 2) : \
45                                         (0x55 + (((nr) - 6) * 2)))
46 static u8 SMSC47M192_REG_TEMP[3] =      { 0x27, 0x26, 0x52 };
47 static u8 SMSC47M192_REG_TEMP_MAX[3] =  { 0x39, 0x37, 0x58 };
48 static u8 SMSC47M192_REG_TEMP_MIN[3] =  { 0x3A, 0x38, 0x59 };
49 #define SMSC47M192_REG_TEMP_OFFSET(nr)  ((nr)==2 ? 0x1e : 0x1f)
50 #define SMSC47M192_REG_ALARM1           0x41
51 #define SMSC47M192_REG_ALARM2           0x42
52 #define SMSC47M192_REG_VID              0x47
53 #define SMSC47M192_REG_VID4             0x49
54 #define SMSC47M192_REG_CONFIG           0x40
55 #define SMSC47M192_REG_SFR              0x4f
56 #define SMSC47M192_REG_COMPANY_ID       0x3e
57 #define SMSC47M192_REG_VERSION          0x3f
58
59 /* generalised scaling with integer rounding */
60 static inline int SCALE(long val, int mul, int div)
61 {
62         if (val < 0)
63                 return (val * mul - div / 2) / div;
64         else
65                 return (val * mul + div / 2) / div;
66 }
67
68 /* Conversions */
69
70 /* smsc47m192 internally scales voltage measurements */
71 static const u16 nom_mv[] = { 2500, 2250, 3300, 5000, 12000, 3300, 1500, 1800 };
72
73 static inline unsigned int IN_FROM_REG(u8 reg, int n)
74 {
75         return SCALE(reg, nom_mv[n], 192);
76 }
77
78 static inline u8 IN_TO_REG(unsigned long val, int n)
79 {
80         return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
81 }
82
83 /* TEMP: 0.001 degC units (-128C to +127C)
84    REG: 1C/bit, two's complement */
85 static inline s8 TEMP_TO_REG(int val)
86 {
87         return SENSORS_LIMIT(SCALE(val, 1, 1000), -128000, 127000);
88 }
89
90 static inline int TEMP_FROM_REG(s8 val)
91 {
92         return val * 1000;
93 }
94
95 struct smsc47m192_data {
96         struct device *hwmon_dev;
97         struct mutex update_lock;
98         char valid;             /* !=0 if following fields are valid */
99         unsigned long last_updated;     /* In jiffies */
100
101         u8 in[8];               /* Register value */
102         u8 in_max[8];           /* Register value */
103         u8 in_min[8];           /* Register value */
104         s8 temp[3];             /* Register value */
105         s8 temp_max[3];         /* Register value */
106         s8 temp_min[3];         /* Register value */
107         s8 temp_offset[3];      /* Register value */
108         u16 alarms;             /* Register encoding, combined */
109         u8 vid;                 /* Register encoding, combined */
110         u8 vrm;
111 };
112
113 static int smsc47m192_probe(struct i2c_client *client,
114                             const struct i2c_device_id *id);
115 static int smsc47m192_detect(struct i2c_client *client,
116                              struct i2c_board_info *info);
117 static int smsc47m192_remove(struct i2c_client *client);
118 static struct smsc47m192_data *smsc47m192_update_device(struct device *dev);
119
120 static const struct i2c_device_id smsc47m192_id[] = {
121         { "smsc47m192", 0 },
122         { }
123 };
124 MODULE_DEVICE_TABLE(i2c, smsc47m192_id);
125
126 static struct i2c_driver smsc47m192_driver = {
127         .class          = I2C_CLASS_HWMON,
128         .driver = {
129                 .name   = "smsc47m192",
130         },
131         .probe          = smsc47m192_probe,
132         .remove         = smsc47m192_remove,
133         .id_table       = smsc47m192_id,
134         .detect         = smsc47m192_detect,
135         .address_list   = normal_i2c,
136 };
137
138 /* Voltages */
139 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
140                 char *buf)
141 {
142         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
143         int nr = sensor_attr->index;
144         struct smsc47m192_data *data = smsc47m192_update_device(dev);
145         return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr));
146 }
147
148 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
149                 char *buf)
150 {
151         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
152         int nr = sensor_attr->index;
153         struct smsc47m192_data *data = smsc47m192_update_device(dev);
154         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr));
155 }
156
157 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
158                 char *buf)
159 {
160         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
161         int nr = sensor_attr->index;
162         struct smsc47m192_data *data = smsc47m192_update_device(dev);
163         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr));
164 }
165
166 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
167                 const char *buf, size_t count)
168 {
169         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
170         int nr = sensor_attr->index;
171         struct i2c_client *client = to_i2c_client(dev);
172         struct smsc47m192_data *data = i2c_get_clientdata(client);
173         unsigned long val = simple_strtoul(buf, NULL, 10);
174
175         mutex_lock(&data->update_lock);
176         data->in_min[nr] = IN_TO_REG(val, nr);
177         i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MIN(nr),
178                                                         data->in_min[nr]);
179         mutex_unlock(&data->update_lock);
180         return count;
181 }
182
183 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
184                 const char *buf, size_t count)
185 {
186         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
187         int nr = sensor_attr->index;
188         struct i2c_client *client = to_i2c_client(dev);
189         struct smsc47m192_data *data = i2c_get_clientdata(client);
190         unsigned long val = simple_strtoul(buf, NULL, 10);
191
192         mutex_lock(&data->update_lock);
193         data->in_max[nr] = IN_TO_REG(val, nr);
194         i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MAX(nr),
195                                                         data->in_max[nr]);
196         mutex_unlock(&data->update_lock);
197         return count;
198 }
199
200 #define show_in_offset(offset)                                  \
201 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO,          \
202                 show_in, NULL, offset);                         \
203 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,  \
204                 show_in_min, set_in_min, offset);               \
205 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,  \
206                 show_in_max, set_in_max, offset);
207
208 show_in_offset(0)
209 show_in_offset(1)
210 show_in_offset(2)
211 show_in_offset(3)
212 show_in_offset(4)
213 show_in_offset(5)
214 show_in_offset(6)
215 show_in_offset(7)
216
217 /* Temperatures */
218 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
219                 char *buf)
220 {
221         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
222         int nr = sensor_attr->index;
223         struct smsc47m192_data *data = smsc47m192_update_device(dev);
224         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
225 }
226
227 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
228                 char *buf)
229 {
230         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
231         int nr = sensor_attr->index;
232         struct smsc47m192_data *data = smsc47m192_update_device(dev);
233         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
234 }
235
236 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
237                 char *buf)
238 {
239         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
240         int nr = sensor_attr->index;
241         struct smsc47m192_data *data = smsc47m192_update_device(dev);
242         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
243 }
244
245 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
246                 const char *buf, size_t count)
247 {
248         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
249         int nr = sensor_attr->index;
250         struct i2c_client *client = to_i2c_client(dev);
251         struct smsc47m192_data *data = i2c_get_clientdata(client);
252         long val = simple_strtol(buf, NULL, 10);
253
254         mutex_lock(&data->update_lock);
255         data->temp_min[nr] = TEMP_TO_REG(val);
256         i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MIN[nr],
257                                                 data->temp_min[nr]);
258         mutex_unlock(&data->update_lock);
259         return count;
260 }
261
262 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
263                 const char *buf, size_t count)
264 {
265         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
266         int nr = sensor_attr->index;
267         struct i2c_client *client = to_i2c_client(dev);
268         struct smsc47m192_data *data = i2c_get_clientdata(client);
269         long val = simple_strtol(buf, NULL, 10);
270
271         mutex_lock(&data->update_lock);
272         data->temp_max[nr] = TEMP_TO_REG(val);
273         i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MAX[nr],
274                                                 data->temp_max[nr]);
275         mutex_unlock(&data->update_lock);
276         return count;
277 }
278
279 static ssize_t show_temp_offset(struct device *dev, struct device_attribute
280                 *attr, char *buf)
281 {
282         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
283         int nr = sensor_attr->index;
284         struct smsc47m192_data *data = smsc47m192_update_device(dev);
285         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
286 }
287
288 static ssize_t set_temp_offset(struct device *dev, struct device_attribute
289                 *attr, const char *buf, size_t count)
290 {
291         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
292         int nr = sensor_attr->index;
293         struct i2c_client *client = to_i2c_client(dev);
294         struct smsc47m192_data *data = i2c_get_clientdata(client);
295         u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
296         long val = simple_strtol(buf, NULL, 10);
297
298         mutex_lock(&data->update_lock);
299         data->temp_offset[nr] = TEMP_TO_REG(val);
300         if (nr>1)
301                 i2c_smbus_write_byte_data(client,
302                         SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
303         else if (data->temp_offset[nr] != 0) {
304                 /* offset[0] and offset[1] share the same register,
305                         SFR bit 4 activates offset[0] */
306                 i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
307                                         (sfr & 0xef) | (nr==0 ? 0x10 : 0));
308                 data->temp_offset[1-nr] = 0;
309                 i2c_smbus_write_byte_data(client,
310                         SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
311         } else if ((sfr & 0x10) == (nr==0 ? 0x10 : 0))
312                 i2c_smbus_write_byte_data(client,
313                                         SMSC47M192_REG_TEMP_OFFSET(nr), 0);
314         mutex_unlock(&data->update_lock);
315         return count;
316 }
317
318 #define show_temp_index(index)                                          \
319 static SENSOR_DEVICE_ATTR(temp##index##_input, S_IRUGO,                 \
320                 show_temp, NULL, index-1);                              \
321 static SENSOR_DEVICE_ATTR(temp##index##_min, S_IRUGO | S_IWUSR,         \
322                 show_temp_min, set_temp_min, index-1);                  \
323 static SENSOR_DEVICE_ATTR(temp##index##_max, S_IRUGO | S_IWUSR,         \
324                 show_temp_max, set_temp_max, index-1);                  \
325 static SENSOR_DEVICE_ATTR(temp##index##_offset, S_IRUGO | S_IWUSR,      \
326                 show_temp_offset, set_temp_offset, index-1);
327
328 show_temp_index(1)
329 show_temp_index(2)
330 show_temp_index(3)
331
332 /* VID */
333 static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
334                 char *buf)
335 {
336         struct smsc47m192_data *data = smsc47m192_update_device(dev);
337         return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
338 }
339 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
340
341 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
342                 char *buf)
343 {
344         struct smsc47m192_data *data = dev_get_drvdata(dev);
345         return sprintf(buf, "%d\n", data->vrm);
346 }
347
348 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
349                 const char *buf, size_t count)
350 {
351         struct smsc47m192_data *data = dev_get_drvdata(dev);
352         data->vrm = simple_strtoul(buf, NULL, 10);
353         return count;
354 }
355 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
356
357 /* Alarms */
358 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
359                 char *buf)
360 {
361         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
362         int nr = sensor_attr->index;
363         struct smsc47m192_data *data = smsc47m192_update_device(dev);
364         return sprintf(buf, "%u\n", (data->alarms & nr) ? 1 : 0);
365 }
366
367 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0x0010);
368 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0x0020);
369 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0x0040);
370 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 0x4000);
371 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 0x8000);
372 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0x0001);
373 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0x0002);
374 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 0x0004);
375 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 0x0008);
376 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 0x0100);
377 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 0x0200);
378 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 0x0400);
379 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 0x0800);
380
381 static struct attribute *smsc47m192_attributes[] = {
382         &sensor_dev_attr_in0_input.dev_attr.attr,
383         &sensor_dev_attr_in0_min.dev_attr.attr,
384         &sensor_dev_attr_in0_max.dev_attr.attr,
385         &sensor_dev_attr_in0_alarm.dev_attr.attr,
386         &sensor_dev_attr_in1_input.dev_attr.attr,
387         &sensor_dev_attr_in1_min.dev_attr.attr,
388         &sensor_dev_attr_in1_max.dev_attr.attr,
389         &sensor_dev_attr_in1_alarm.dev_attr.attr,
390         &sensor_dev_attr_in2_input.dev_attr.attr,
391         &sensor_dev_attr_in2_min.dev_attr.attr,
392         &sensor_dev_attr_in2_max.dev_attr.attr,
393         &sensor_dev_attr_in2_alarm.dev_attr.attr,
394         &sensor_dev_attr_in3_input.dev_attr.attr,
395         &sensor_dev_attr_in3_min.dev_attr.attr,
396         &sensor_dev_attr_in3_max.dev_attr.attr,
397         &sensor_dev_attr_in3_alarm.dev_attr.attr,
398         &sensor_dev_attr_in5_input.dev_attr.attr,
399         &sensor_dev_attr_in5_min.dev_attr.attr,
400         &sensor_dev_attr_in5_max.dev_attr.attr,
401         &sensor_dev_attr_in5_alarm.dev_attr.attr,
402         &sensor_dev_attr_in6_input.dev_attr.attr,
403         &sensor_dev_attr_in6_min.dev_attr.attr,
404         &sensor_dev_attr_in6_max.dev_attr.attr,
405         &sensor_dev_attr_in6_alarm.dev_attr.attr,
406         &sensor_dev_attr_in7_input.dev_attr.attr,
407         &sensor_dev_attr_in7_min.dev_attr.attr,
408         &sensor_dev_attr_in7_max.dev_attr.attr,
409         &sensor_dev_attr_in7_alarm.dev_attr.attr,
410
411         &sensor_dev_attr_temp1_input.dev_attr.attr,
412         &sensor_dev_attr_temp1_max.dev_attr.attr,
413         &sensor_dev_attr_temp1_min.dev_attr.attr,
414         &sensor_dev_attr_temp1_offset.dev_attr.attr,
415         &sensor_dev_attr_temp1_alarm.dev_attr.attr,
416         &sensor_dev_attr_temp2_input.dev_attr.attr,
417         &sensor_dev_attr_temp2_max.dev_attr.attr,
418         &sensor_dev_attr_temp2_min.dev_attr.attr,
419         &sensor_dev_attr_temp2_offset.dev_attr.attr,
420         &sensor_dev_attr_temp2_alarm.dev_attr.attr,
421         &sensor_dev_attr_temp2_fault.dev_attr.attr,
422         &sensor_dev_attr_temp3_input.dev_attr.attr,
423         &sensor_dev_attr_temp3_max.dev_attr.attr,
424         &sensor_dev_attr_temp3_min.dev_attr.attr,
425         &sensor_dev_attr_temp3_offset.dev_attr.attr,
426         &sensor_dev_attr_temp3_alarm.dev_attr.attr,
427         &sensor_dev_attr_temp3_fault.dev_attr.attr,
428
429         &dev_attr_cpu0_vid.attr,
430         &dev_attr_vrm.attr,
431         NULL
432 };
433
434 static const struct attribute_group smsc47m192_group = {
435         .attrs = smsc47m192_attributes,
436 };
437
438 static struct attribute *smsc47m192_attributes_in4[] = {
439         &sensor_dev_attr_in4_input.dev_attr.attr,
440         &sensor_dev_attr_in4_min.dev_attr.attr,
441         &sensor_dev_attr_in4_max.dev_attr.attr,
442         &sensor_dev_attr_in4_alarm.dev_attr.attr,
443         NULL
444 };
445
446 static const struct attribute_group smsc47m192_group_in4 = {
447         .attrs = smsc47m192_attributes_in4,
448 };
449
450 static void smsc47m192_init_client(struct i2c_client *client)
451 {
452         int i;
453         u8 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
454         u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
455
456         /* select cycle mode (pause 1 sec between updates) */
457         i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
458                                                 (sfr & 0xfd) | 0x02);
459         if (!(config & 0x01)) {
460                 /* initialize alarm limits */
461                 for (i=0; i<8; i++) {
462                         i2c_smbus_write_byte_data(client,
463                                 SMSC47M192_REG_IN_MIN(i), 0);
464                         i2c_smbus_write_byte_data(client,
465                                 SMSC47M192_REG_IN_MAX(i), 0xff);
466                 }
467                 for (i=0; i<3; i++) {
468                         i2c_smbus_write_byte_data(client,
469                                 SMSC47M192_REG_TEMP_MIN[i], 0x80);
470                         i2c_smbus_write_byte_data(client,
471                                 SMSC47M192_REG_TEMP_MAX[i], 0x7f);
472                 }
473
474                 /* start monitoring */
475                 i2c_smbus_write_byte_data(client, SMSC47M192_REG_CONFIG,
476                                                 (config & 0xf7) | 0x01);
477         }
478 }
479
480 /* Return 0 if detection is successful, -ENODEV otherwise */
481 static int smsc47m192_detect(struct i2c_client *client,
482                              struct i2c_board_info *info)
483 {
484         struct i2c_adapter *adapter = client->adapter;
485         int version;
486
487         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
488                 return -ENODEV;
489
490         /* Detection criteria from sensors_detect script */
491         version = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VERSION);
492         if (i2c_smbus_read_byte_data(client,
493                                 SMSC47M192_REG_COMPANY_ID) == 0x55
494          && (version & 0xf0) == 0x20
495          && (i2c_smbus_read_byte_data(client,
496                                 SMSC47M192_REG_VID) & 0x70) == 0x00
497          && (i2c_smbus_read_byte_data(client,
498                                 SMSC47M192_REG_VID4) & 0xfe) == 0x80) {
499                 dev_info(&adapter->dev,
500                          "found SMSC47M192 or compatible, "
501                          "version 2, stepping A%d\n", version & 0x0f);
502         } else {
503                 dev_dbg(&adapter->dev,
504                         "SMSC47M192 detection failed at 0x%02x\n",
505                         client->addr);
506                 return -ENODEV;
507         }
508
509         strlcpy(info->type, "smsc47m192", I2C_NAME_SIZE);
510
511         return 0;
512 }
513
514 static int smsc47m192_probe(struct i2c_client *client,
515                             const struct i2c_device_id *id)
516 {
517         struct smsc47m192_data *data;
518         int config;
519         int err;
520
521         data = kzalloc(sizeof(struct smsc47m192_data), GFP_KERNEL);
522         if (!data) {
523                 err = -ENOMEM;
524                 goto exit;
525         }
526
527         i2c_set_clientdata(client, data);
528         data->vrm = vid_which_vrm();
529         mutex_init(&data->update_lock);
530
531         /* Initialize the SMSC47M192 chip */
532         smsc47m192_init_client(client);
533
534         /* Register sysfs hooks */
535         if ((err = sysfs_create_group(&client->dev.kobj, &smsc47m192_group)))
536                 goto exit_free;
537
538         /* Pin 110 is either in4 (+12V) or VID4 */
539         config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
540         if (!(config & 0x20)) {
541                 if ((err = sysfs_create_group(&client->dev.kobj,
542                                               &smsc47m192_group_in4)))
543                         goto exit_remove_files;
544         }
545
546         data->hwmon_dev = hwmon_device_register(&client->dev);
547         if (IS_ERR(data->hwmon_dev)) {
548                 err = PTR_ERR(data->hwmon_dev);
549                 goto exit_remove_files;
550         }
551
552         return 0;
553
554 exit_remove_files:
555         sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
556         sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
557 exit_free:
558         kfree(data);
559 exit:
560         return err;
561 }
562
563 static int smsc47m192_remove(struct i2c_client *client)
564 {
565         struct smsc47m192_data *data = i2c_get_clientdata(client);
566
567         hwmon_device_unregister(data->hwmon_dev);
568         sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
569         sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
570
571         kfree(data);
572
573         return 0;
574 }
575
576 static struct smsc47m192_data *smsc47m192_update_device(struct device *dev)
577 {
578         struct i2c_client *client = to_i2c_client(dev);
579         struct smsc47m192_data *data = i2c_get_clientdata(client);
580         int i, config;
581
582         mutex_lock(&data->update_lock);
583
584         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
585          || !data->valid) {
586                 u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
587
588                 dev_dbg(&client->dev, "Starting smsc47m192 update\n");
589
590                 for (i = 0; i <= 7; i++) {
591                         data->in[i] = i2c_smbus_read_byte_data(client,
592                                                 SMSC47M192_REG_IN(i));
593                         data->in_min[i] = i2c_smbus_read_byte_data(client,
594                                                 SMSC47M192_REG_IN_MIN(i));
595                         data->in_max[i] = i2c_smbus_read_byte_data(client,
596                                                 SMSC47M192_REG_IN_MAX(i));
597                 }
598                 for (i = 0; i < 3; i++) {
599                         data->temp[i] = i2c_smbus_read_byte_data(client,
600                                                 SMSC47M192_REG_TEMP[i]);
601                         data->temp_max[i] = i2c_smbus_read_byte_data(client,
602                                                 SMSC47M192_REG_TEMP_MAX[i]);
603                         data->temp_min[i] = i2c_smbus_read_byte_data(client,
604                                                 SMSC47M192_REG_TEMP_MIN[i]);
605                 }
606                 for (i = 1; i < 3; i++)
607                         data->temp_offset[i] = i2c_smbus_read_byte_data(client,
608                                                 SMSC47M192_REG_TEMP_OFFSET(i));
609                 /* first offset is temp_offset[0] if SFR bit 4 is set,
610                                         temp_offset[1] otherwise */
611                 if (sfr & 0x10) {
612                         data->temp_offset[0] = data->temp_offset[1];
613                         data->temp_offset[1] = 0;
614                 } else
615                         data->temp_offset[0] = 0;
616
617                 data->vid = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VID)
618                             & 0x0f;
619                 config = i2c_smbus_read_byte_data(client,
620                                                   SMSC47M192_REG_CONFIG);
621                 if (config & 0x20)
622                         data->vid |= (i2c_smbus_read_byte_data(client,
623                                         SMSC47M192_REG_VID4) & 0x01) << 4;
624                 data->alarms = i2c_smbus_read_byte_data(client,
625                                                 SMSC47M192_REG_ALARM1) |
626                                (i2c_smbus_read_byte_data(client,
627                                                 SMSC47M192_REG_ALARM2) << 8);
628
629                 data->last_updated = jiffies;
630                 data->valid = 1;
631         }
632
633         mutex_unlock(&data->update_lock);
634
635         return data;
636 }
637
638 static int __init smsc47m192_init(void)
639 {
640         return i2c_add_driver(&smsc47m192_driver);
641 }
642
643 static void __exit smsc47m192_exit(void)
644 {
645         i2c_del_driver(&smsc47m192_driver);
646 }
647
648 MODULE_AUTHOR("Hartmut Rick <linux@rick.claranet.de>");
649 MODULE_DESCRIPTION("SMSC47M192 driver");
650 MODULE_LICENSE("GPL");
651
652 module_init(smsc47m192_init);
653 module_exit(smsc47m192_exit);