blob: d3a3ba04cb0f1aac62503f96a65cfd3cde4b79ec [file] [log] [blame]
Hartmut Rick59ac8362006-03-23 16:37:23 +01001/*
2 smsc47m192.c - Support for hardware monitoring block of
Jean Delvare00cb4732007-05-08 17:21:59 +02003 SMSC LPC47M192 and compatible Super I/O chips
Hartmut Rick59ac8362006-03-23 16:37:23 +01004
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>
Jean Delvarece8c6ce12006-09-24 21:25:12 +020033#include <linux/sysfs.h>
Jean Delvaree4a71672007-06-09 10:11:15 -040034#include <linux/mutex.h>
Hartmut Rick59ac8362006-03-23 16:37:23 +010035
36/* Addresses to scan */
37static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
38
39/* Insmod parameters */
40I2C_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)))
49static u8 SMSC47M192_REG_TEMP[3] = { 0x27, 0x26, 0x52 };
50static u8 SMSC47M192_REG_TEMP_MAX[3] = { 0x39, 0x37, 0x58 };
51static 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 */
63static 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 */
74static const u16 nom_mv[] = { 2500, 2250, 3300, 5000, 12000, 3300, 1500, 1800 };
75
76static inline unsigned int IN_FROM_REG(u8 reg, int n)
77{
78 return SCALE(reg, nom_mv[n], 192);
79}
80
81static 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 */
88static inline s8 TEMP_TO_REG(int val)
89{
90 return SENSORS_LIMIT(SCALE(val, 1, 1000), -128000, 127000);
91}
92
93static inline int TEMP_FROM_REG(s8 val)
94{
95 return val * 1000;
96}
97
98struct smsc47m192_data {
99 struct i2c_client client;
100 struct class_device *class_dev;
Jean Delvaree4a71672007-06-09 10:11:15 -0400101 struct mutex update_lock;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100102 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
117static int smsc47m192_attach_adapter(struct i2c_adapter *adapter);
118static int smsc47m192_detect(struct i2c_adapter *adapter, int address,
119 int kind);
120static int smsc47m192_detach_client(struct i2c_client *client);
121static struct smsc47m192_data *smsc47m192_update_device(struct device *dev);
122
123static 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 */
132static 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
141static 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
150static 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
159static 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
Jean Delvaree4a71672007-06-09 10:11:15 -0400168 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100169 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]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400172 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100173 return count;
174}
175
176static 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
Jean Delvaree4a71672007-06-09 10:11:15 -0400185 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100186 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]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400189 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100190 return count;
191}
192
193#define show_in_offset(offset) \
194static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
195 show_in, NULL, offset); \
196static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
197 show_in_min, set_in_min, offset); \
198static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
199 show_in_max, set_in_max, offset);
200
201show_in_offset(0)
202show_in_offset(1)
203show_in_offset(2)
204show_in_offset(3)
205show_in_offset(4)
206show_in_offset(5)
207show_in_offset(6)
208show_in_offset(7)
209
210/* Temperatures */
211static 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
220static 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
229static 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
238static 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
Jean Delvaree4a71672007-06-09 10:11:15 -0400247 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100248 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]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400251 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100252 return count;
253}
254
255static 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
Jean Delvaree4a71672007-06-09 10:11:15 -0400264 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100265 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]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400268 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100269 return count;
270}
271
272static 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
281static 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
Jean Delvaree4a71672007-06-09 10:11:15 -0400291 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100292 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);
Jean Delvaree4a71672007-06-09 10:11:15 -0400307 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100308 return count;
309}
310
311#define show_temp_index(index) \
312static SENSOR_DEVICE_ATTR(temp##index##_input, S_IRUGO, \
313 show_temp, NULL, index-1); \
314static SENSOR_DEVICE_ATTR(temp##index##_min, S_IRUGO | S_IWUSR, \
315 show_temp_min, set_temp_min, index-1); \
316static SENSOR_DEVICE_ATTR(temp##index##_max, S_IRUGO | S_IWUSR, \
317 show_temp_max, set_temp_max, index-1); \
318static SENSOR_DEVICE_ATTR(temp##index##_offset, S_IRUGO | S_IWUSR, \
319 show_temp_offset, set_temp_offset, index-1);
320
321show_temp_index(1)
322show_temp_index(2)
323show_temp_index(3)
324
325/* VID */
326static 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}
332static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
333
334static 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
341static 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}
349static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
350
351/* Alarms */
352static 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
361static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0x0010);
362static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0x0020);
363static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0x0040);
Jean Delvare7817a392007-06-09 10:11:16 -0400364static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 0x4000);
365static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 0x8000);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100366static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0x0001);
367static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0x0002);
368static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 0x0004);
369static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 0x0008);
370static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 0x0100);
371static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 0x0200);
372static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 0x0400);
373static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 0x0800);
374
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200375static 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,
Jean Delvare7817a392007-06-09 10:11:16 -0400415 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200416 &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,
Jean Delvare7817a392007-06-09 10:11:16 -0400421 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200422
423 &dev_attr_cpu0_vid.attr,
424 &dev_attr_vrm.attr,
425 NULL
426};
427
428static const struct attribute_group smsc47m192_group = {
429 .attrs = smsc47m192_attributes,
430};
431
432static 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
440static const struct attribute_group smsc47m192_group_in4 = {
441 .attrs = smsc47m192_attributes_in4,
442};
443
Hartmut Rick59ac8362006-03-23 16:37:23 +0100444/* 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) */
448static 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
455static 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 */
486static 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,
Jean Delvare00cb4732007-05-08 17:21:59 +0200522 "found SMSC47M192 or compatible, "
Hartmut Rick59ac8362006-03-23 16:37:23 +0100523 "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();
Jean Delvaree4a71672007-06-09 10:11:15 -0400535 mutex_init(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100536
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 */
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200545 if ((err = sysfs_create_group(&client->dev.kobj, &smsc47m192_group)))
Hartmut Rick59ac8362006-03-23 16:37:23 +0100546 goto exit_detach;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100547
548 /* Pin 110 is either in4 (+12V) or VID4 */
549 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
550 if (!(config & 0x20)) {
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200551 if ((err = sysfs_create_group(&client->dev.kobj,
552 &smsc47m192_group_in4)))
553 goto exit_remove_files;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100554 }
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200555
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 }
Hartmut Rick59ac8362006-03-23 16:37:23 +0100561
562 return 0;
563
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200564exit_remove_files:
565 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
566 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100567exit_detach:
568 i2c_detach_client(client);
569exit_free:
570 kfree(data);
571exit:
572 return err;
573}
574
575static 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);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200581 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
582 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100583
584 if ((err = i2c_detach_client(client)))
585 return err;
586
587 kfree(data);
588
589 return 0;
590}
591
592static 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
Jean Delvaree4a71672007-06-09 10:11:15 -0400598 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100599
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
Jean Delvaree4a71672007-06-09 10:11:15 -0400649 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100650
651 return data;
652}
653
654static int __init smsc47m192_init(void)
655{
656 return i2c_add_driver(&smsc47m192_driver);
657}
658
659static void __exit smsc47m192_exit(void)
660{
661 i2c_del_driver(&smsc47m192_driver);
662}
663
664MODULE_AUTHOR("Hartmut Rick <linux@rick.claranet.de>");
665MODULE_DESCRIPTION("SMSC47M192 driver");
666MODULE_LICENSE("GPL");
667
668module_init(smsc47m192_init);
669module_exit(smsc47m192_exit);