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