blob: 8b0c188e60f689624fd3701f201f0c7f8868c6d6 [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;
Tony Jones1beeffe2007-08-20 13:46:20 -0700100 struct device *hwmon_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{
Jean Delvare90d66192007-10-08 18:24:35 +0200337 struct smsc47m192_data *data = dev_get_drvdata(dev);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100338 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{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100344 struct smsc47m192_data *data = dev_get_drvdata(dev);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100345 data->vrm = simple_strtoul(buf, NULL, 10);
346 return count;
347}
348static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
349
350/* Alarms */
351static 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
360static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0x0010);
361static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0x0020);
362static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0x0040);
Jean Delvare7817a392007-06-09 10:11:16 -0400363static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 0x4000);
364static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 0x8000);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100365static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0x0001);
366static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0x0002);
367static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 0x0004);
368static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 0x0008);
369static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 0x0100);
370static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 0x0200);
371static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 0x0400);
372static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 0x0800);
373
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200374static 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,
Jean Delvare7817a392007-06-09 10:11:16 -0400414 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200415 &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,
Jean Delvare7817a392007-06-09 10:11:16 -0400420 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200421
422 &dev_attr_cpu0_vid.attr,
423 &dev_attr_vrm.attr,
424 NULL
425};
426
427static const struct attribute_group smsc47m192_group = {
428 .attrs = smsc47m192_attributes,
429};
430
431static 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
439static const struct attribute_group smsc47m192_group_in4 = {
440 .attrs = smsc47m192_attributes_in4,
441};
442
Hartmut Rick59ac8362006-03-23 16:37:23 +0100443/* 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) */
447static 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
454static 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 */
485static 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,
Jean Delvare00cb4732007-05-08 17:21:59 +0200521 "found SMSC47M192 or compatible, "
Hartmut Rick59ac8362006-03-23 16:37:23 +0100522 "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();
Jean Delvaree4a71672007-06-09 10:11:15 -0400534 mutex_init(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100535
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 */
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200544 if ((err = sysfs_create_group(&client->dev.kobj, &smsc47m192_group)))
Hartmut Rick59ac8362006-03-23 16:37:23 +0100545 goto exit_detach;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100546
547 /* Pin 110 is either in4 (+12V) or VID4 */
548 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
549 if (!(config & 0x20)) {
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200550 if ((err = sysfs_create_group(&client->dev.kobj,
551 &smsc47m192_group_in4)))
552 goto exit_remove_files;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100553 }
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200554
Tony Jones1beeffe2007-08-20 13:46:20 -0700555 data->hwmon_dev = hwmon_device_register(&client->dev);
556 if (IS_ERR(data->hwmon_dev)) {
557 err = PTR_ERR(data->hwmon_dev);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200558 goto exit_remove_files;
559 }
Hartmut Rick59ac8362006-03-23 16:37:23 +0100560
561 return 0;
562
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200563exit_remove_files:
564 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
565 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100566exit_detach:
567 i2c_detach_client(client);
568exit_free:
569 kfree(data);
570exit:
571 return err;
572}
573
574static int smsc47m192_detach_client(struct i2c_client *client)
575{
576 struct smsc47m192_data *data = i2c_get_clientdata(client);
577 int err;
578
Tony Jones1beeffe2007-08-20 13:46:20 -0700579 hwmon_device_unregister(data->hwmon_dev);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200580 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
581 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100582
583 if ((err = i2c_detach_client(client)))
584 return err;
585
586 kfree(data);
587
588 return 0;
589}
590
591static 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
Jean Delvaree4a71672007-06-09 10:11:15 -0400597 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100598
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
Jean Delvaree4a71672007-06-09 10:11:15 -0400648 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100649
650 return data;
651}
652
653static int __init smsc47m192_init(void)
654{
655 return i2c_add_driver(&smsc47m192_driver);
656}
657
658static void __exit smsc47m192_exit(void)
659{
660 i2c_del_driver(&smsc47m192_driver);
661}
662
663MODULE_AUTHOR("Hartmut Rick <linux@rick.claranet.de>");
664MODULE_DESCRIPTION("SMSC47M192 driver");
665MODULE_LICENSE("GPL");
666
667module_init(smsc47m192_init);
668module_exit(smsc47m192_exit);