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