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