blob: 8bb5cb532d4d8c121595298729f7b85c03ed3d50 [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 */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050037static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
Hartmut Rick59ac8362006-03-23 16:37:23 +010038
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 {
Tony Jones1beeffe2007-08-20 13:46:20 -070099 struct device *hwmon_dev;
Jean Delvaree4a71672007-06-09 10:11:15 -0400100 struct mutex update_lock;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100101 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
Jean Delvare8fb597b2008-07-16 19:30:16 +0200116static int smsc47m192_probe(struct i2c_client *client,
117 const struct i2c_device_id *id);
118static int smsc47m192_detect(struct i2c_client *client, int kind,
119 struct i2c_board_info *info);
120static int smsc47m192_remove(struct i2c_client *client);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100121static struct smsc47m192_data *smsc47m192_update_device(struct device *dev);
122
Jean Delvare8fb597b2008-07-16 19:30:16 +0200123static const struct i2c_device_id smsc47m192_id[] = {
124 { "smsc47m192", smsc47m192 },
125 { }
126};
127MODULE_DEVICE_TABLE(i2c, smsc47m192_id);
128
Hartmut Rick59ac8362006-03-23 16:37:23 +0100129static struct i2c_driver smsc47m192_driver = {
Jean Delvare8fb597b2008-07-16 19:30:16 +0200130 .class = I2C_CLASS_HWMON,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100131 .driver = {
132 .name = "smsc47m192",
133 },
Jean Delvare8fb597b2008-07-16 19:30:16 +0200134 .probe = smsc47m192_probe,
135 .remove = smsc47m192_remove,
136 .id_table = smsc47m192_id,
137 .detect = smsc47m192_detect,
138 .address_data = &addr_data,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100139};
140
141/* Voltages */
142static ssize_t show_in(struct device *dev, struct device_attribute *attr,
143 char *buf)
144{
145 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
146 int nr = sensor_attr->index;
147 struct smsc47m192_data *data = smsc47m192_update_device(dev);
148 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr));
149}
150
151static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
152 char *buf)
153{
154 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
155 int nr = sensor_attr->index;
156 struct smsc47m192_data *data = smsc47m192_update_device(dev);
157 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr));
158}
159
160static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
161 char *buf)
162{
163 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
164 int nr = sensor_attr->index;
165 struct smsc47m192_data *data = smsc47m192_update_device(dev);
166 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr));
167}
168
169static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
170 const char *buf, size_t count)
171{
172 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
173 int nr = sensor_attr->index;
174 struct i2c_client *client = to_i2c_client(dev);
175 struct smsc47m192_data *data = i2c_get_clientdata(client);
176 unsigned long val = simple_strtoul(buf, NULL, 10);
177
Jean Delvaree4a71672007-06-09 10:11:15 -0400178 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100179 data->in_min[nr] = IN_TO_REG(val, nr);
180 i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MIN(nr),
181 data->in_min[nr]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400182 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100183 return count;
184}
185
186static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
187 const char *buf, size_t count)
188{
189 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
190 int nr = sensor_attr->index;
191 struct i2c_client *client = to_i2c_client(dev);
192 struct smsc47m192_data *data = i2c_get_clientdata(client);
193 unsigned long val = simple_strtoul(buf, NULL, 10);
194
Jean Delvaree4a71672007-06-09 10:11:15 -0400195 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100196 data->in_max[nr] = IN_TO_REG(val, nr);
197 i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MAX(nr),
198 data->in_max[nr]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400199 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100200 return count;
201}
202
203#define show_in_offset(offset) \
204static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
205 show_in, NULL, offset); \
206static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
207 show_in_min, set_in_min, offset); \
208static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
209 show_in_max, set_in_max, offset);
210
211show_in_offset(0)
212show_in_offset(1)
213show_in_offset(2)
214show_in_offset(3)
215show_in_offset(4)
216show_in_offset(5)
217show_in_offset(6)
218show_in_offset(7)
219
220/* Temperatures */
221static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
222 char *buf)
223{
224 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
225 int nr = sensor_attr->index;
226 struct smsc47m192_data *data = smsc47m192_update_device(dev);
227 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
228}
229
230static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
231 char *buf)
232{
233 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
234 int nr = sensor_attr->index;
235 struct smsc47m192_data *data = smsc47m192_update_device(dev);
236 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
237}
238
239static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
240 char *buf)
241{
242 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
243 int nr = sensor_attr->index;
244 struct smsc47m192_data *data = smsc47m192_update_device(dev);
245 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
246}
247
248static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
249 const char *buf, size_t count)
250{
251 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
252 int nr = sensor_attr->index;
253 struct i2c_client *client = to_i2c_client(dev);
254 struct smsc47m192_data *data = i2c_get_clientdata(client);
255 long val = simple_strtol(buf, NULL, 10);
256
Jean Delvaree4a71672007-06-09 10:11:15 -0400257 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100258 data->temp_min[nr] = TEMP_TO_REG(val);
259 i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MIN[nr],
260 data->temp_min[nr]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400261 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100262 return count;
263}
264
265static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
266 const char *buf, size_t count)
267{
268 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
269 int nr = sensor_attr->index;
270 struct i2c_client *client = to_i2c_client(dev);
271 struct smsc47m192_data *data = i2c_get_clientdata(client);
272 long val = simple_strtol(buf, NULL, 10);
273
Jean Delvaree4a71672007-06-09 10:11:15 -0400274 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100275 data->temp_max[nr] = TEMP_TO_REG(val);
276 i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MAX[nr],
277 data->temp_max[nr]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400278 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100279 return count;
280}
281
282static ssize_t show_temp_offset(struct device *dev, struct device_attribute
283 *attr, char *buf)
284{
285 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
286 int nr = sensor_attr->index;
287 struct smsc47m192_data *data = smsc47m192_update_device(dev);
288 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
289}
290
291static ssize_t set_temp_offset(struct device *dev, struct device_attribute
292 *attr, const char *buf, size_t count)
293{
294 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
295 int nr = sensor_attr->index;
296 struct i2c_client *client = to_i2c_client(dev);
297 struct smsc47m192_data *data = i2c_get_clientdata(client);
298 u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
299 long val = simple_strtol(buf, NULL, 10);
300
Jean Delvaree4a71672007-06-09 10:11:15 -0400301 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100302 data->temp_offset[nr] = TEMP_TO_REG(val);
303 if (nr>1)
304 i2c_smbus_write_byte_data(client,
305 SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
306 else if (data->temp_offset[nr] != 0) {
307 /* offset[0] and offset[1] share the same register,
308 SFR bit 4 activates offset[0] */
309 i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
310 (sfr & 0xef) | (nr==0 ? 0x10 : 0));
311 data->temp_offset[1-nr] = 0;
312 i2c_smbus_write_byte_data(client,
313 SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
314 } else if ((sfr & 0x10) == (nr==0 ? 0x10 : 0))
315 i2c_smbus_write_byte_data(client,
316 SMSC47M192_REG_TEMP_OFFSET(nr), 0);
Jean Delvaree4a71672007-06-09 10:11:15 -0400317 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100318 return count;
319}
320
321#define show_temp_index(index) \
322static SENSOR_DEVICE_ATTR(temp##index##_input, S_IRUGO, \
323 show_temp, NULL, index-1); \
324static SENSOR_DEVICE_ATTR(temp##index##_min, S_IRUGO | S_IWUSR, \
325 show_temp_min, set_temp_min, index-1); \
326static SENSOR_DEVICE_ATTR(temp##index##_max, S_IRUGO | S_IWUSR, \
327 show_temp_max, set_temp_max, index-1); \
328static SENSOR_DEVICE_ATTR(temp##index##_offset, S_IRUGO | S_IWUSR, \
329 show_temp_offset, set_temp_offset, index-1);
330
331show_temp_index(1)
332show_temp_index(2)
333show_temp_index(3)
334
335/* VID */
336static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
337 char *buf)
338{
339 struct smsc47m192_data *data = smsc47m192_update_device(dev);
340 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
341}
342static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
343
344static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
345 char *buf)
346{
Jean Delvare90d66192007-10-08 18:24:35 +0200347 struct smsc47m192_data *data = dev_get_drvdata(dev);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100348 return sprintf(buf, "%d\n", data->vrm);
349}
350
351static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
352 const char *buf, size_t count)
353{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100354 struct smsc47m192_data *data = dev_get_drvdata(dev);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100355 data->vrm = simple_strtoul(buf, NULL, 10);
356 return count;
357}
358static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
359
360/* Alarms */
361static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
362 char *buf)
363{
364 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
365 int nr = sensor_attr->index;
366 struct smsc47m192_data *data = smsc47m192_update_device(dev);
367 return sprintf(buf, "%u\n", (data->alarms & nr) ? 1 : 0);
368}
369
370static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0x0010);
371static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0x0020);
372static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0x0040);
Jean Delvare7817a392007-06-09 10:11:16 -0400373static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 0x4000);
374static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 0x8000);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100375static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0x0001);
376static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0x0002);
377static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 0x0004);
378static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 0x0008);
379static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 0x0100);
380static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 0x0200);
381static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 0x0400);
382static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 0x0800);
383
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200384static struct attribute *smsc47m192_attributes[] = {
385 &sensor_dev_attr_in0_input.dev_attr.attr,
386 &sensor_dev_attr_in0_min.dev_attr.attr,
387 &sensor_dev_attr_in0_max.dev_attr.attr,
388 &sensor_dev_attr_in0_alarm.dev_attr.attr,
389 &sensor_dev_attr_in1_input.dev_attr.attr,
390 &sensor_dev_attr_in1_min.dev_attr.attr,
391 &sensor_dev_attr_in1_max.dev_attr.attr,
392 &sensor_dev_attr_in1_alarm.dev_attr.attr,
393 &sensor_dev_attr_in2_input.dev_attr.attr,
394 &sensor_dev_attr_in2_min.dev_attr.attr,
395 &sensor_dev_attr_in2_max.dev_attr.attr,
396 &sensor_dev_attr_in2_alarm.dev_attr.attr,
397 &sensor_dev_attr_in3_input.dev_attr.attr,
398 &sensor_dev_attr_in3_min.dev_attr.attr,
399 &sensor_dev_attr_in3_max.dev_attr.attr,
400 &sensor_dev_attr_in3_alarm.dev_attr.attr,
401 &sensor_dev_attr_in5_input.dev_attr.attr,
402 &sensor_dev_attr_in5_min.dev_attr.attr,
403 &sensor_dev_attr_in5_max.dev_attr.attr,
404 &sensor_dev_attr_in5_alarm.dev_attr.attr,
405 &sensor_dev_attr_in6_input.dev_attr.attr,
406 &sensor_dev_attr_in6_min.dev_attr.attr,
407 &sensor_dev_attr_in6_max.dev_attr.attr,
408 &sensor_dev_attr_in6_alarm.dev_attr.attr,
409 &sensor_dev_attr_in7_input.dev_attr.attr,
410 &sensor_dev_attr_in7_min.dev_attr.attr,
411 &sensor_dev_attr_in7_max.dev_attr.attr,
412 &sensor_dev_attr_in7_alarm.dev_attr.attr,
413
414 &sensor_dev_attr_temp1_input.dev_attr.attr,
415 &sensor_dev_attr_temp1_max.dev_attr.attr,
416 &sensor_dev_attr_temp1_min.dev_attr.attr,
417 &sensor_dev_attr_temp1_offset.dev_attr.attr,
418 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
419 &sensor_dev_attr_temp2_input.dev_attr.attr,
420 &sensor_dev_attr_temp2_max.dev_attr.attr,
421 &sensor_dev_attr_temp2_min.dev_attr.attr,
422 &sensor_dev_attr_temp2_offset.dev_attr.attr,
423 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400424 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200425 &sensor_dev_attr_temp3_input.dev_attr.attr,
426 &sensor_dev_attr_temp3_max.dev_attr.attr,
427 &sensor_dev_attr_temp3_min.dev_attr.attr,
428 &sensor_dev_attr_temp3_offset.dev_attr.attr,
429 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400430 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200431
432 &dev_attr_cpu0_vid.attr,
433 &dev_attr_vrm.attr,
434 NULL
435};
436
437static const struct attribute_group smsc47m192_group = {
438 .attrs = smsc47m192_attributes,
439};
440
441static struct attribute *smsc47m192_attributes_in4[] = {
442 &sensor_dev_attr_in4_input.dev_attr.attr,
443 &sensor_dev_attr_in4_min.dev_attr.attr,
444 &sensor_dev_attr_in4_max.dev_attr.attr,
445 &sensor_dev_attr_in4_alarm.dev_attr.attr,
446 NULL
447};
448
449static const struct attribute_group smsc47m192_group_in4 = {
450 .attrs = smsc47m192_attributes_in4,
451};
452
Hartmut Rick59ac8362006-03-23 16:37:23 +0100453static void smsc47m192_init_client(struct i2c_client *client)
454{
455 int i;
456 u8 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
457 u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
458
459 /* select cycle mode (pause 1 sec between updates) */
460 i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
461 (sfr & 0xfd) | 0x02);
462 if (!(config & 0x01)) {
463 /* initialize alarm limits */
464 for (i=0; i<8; i++) {
465 i2c_smbus_write_byte_data(client,
466 SMSC47M192_REG_IN_MIN(i), 0);
467 i2c_smbus_write_byte_data(client,
468 SMSC47M192_REG_IN_MAX(i), 0xff);
469 }
470 for (i=0; i<3; i++) {
471 i2c_smbus_write_byte_data(client,
472 SMSC47M192_REG_TEMP_MIN[i], 0x80);
473 i2c_smbus_write_byte_data(client,
474 SMSC47M192_REG_TEMP_MAX[i], 0x7f);
475 }
476
477 /* start monitoring */
478 i2c_smbus_write_byte_data(client, SMSC47M192_REG_CONFIG,
479 (config & 0xf7) | 0x01);
480 }
481}
482
Jean Delvare8fb597b2008-07-16 19:30:16 +0200483/* Return 0 if detection is successful, -ENODEV otherwise */
484static int smsc47m192_detect(struct i2c_client *client, int kind,
485 struct i2c_board_info *info)
Hartmut Rick59ac8362006-03-23 16:37:23 +0100486{
Jean Delvare8fb597b2008-07-16 19:30:16 +0200487 struct i2c_adapter *adapter = client->adapter;
488 int version;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100489
490 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare8fb597b2008-07-16 19:30:16 +0200491 return -ENODEV;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100492
493 /* Detection criteria from sensors_detect script */
494 if (kind < 0) {
495 if (i2c_smbus_read_byte_data(client,
496 SMSC47M192_REG_COMPANY_ID) == 0x55
497 && ((version = i2c_smbus_read_byte_data(client,
498 SMSC47M192_REG_VERSION)) & 0xf0) == 0x20
499 && (i2c_smbus_read_byte_data(client,
500 SMSC47M192_REG_VID) & 0x70) == 0x00
501 && (i2c_smbus_read_byte_data(client,
502 SMSC47M192_REG_VID4) & 0xfe) == 0x80) {
503 dev_info(&adapter->dev,
Jean Delvare00cb4732007-05-08 17:21:59 +0200504 "found SMSC47M192 or compatible, "
Hartmut Rick59ac8362006-03-23 16:37:23 +0100505 "version 2, stepping A%d\n", version & 0x0f);
506 } else {
507 dev_dbg(&adapter->dev,
508 "SMSC47M192 detection failed at 0x%02x\n",
Jean Delvare8fb597b2008-07-16 19:30:16 +0200509 client->addr);
510 return -ENODEV;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100511 }
512 }
513
Jean Delvare8fb597b2008-07-16 19:30:16 +0200514 strlcpy(info->type, "smsc47m192", I2C_NAME_SIZE);
515
516 return 0;
517}
518
519static int smsc47m192_probe(struct i2c_client *client,
520 const struct i2c_device_id *id)
521{
522 struct smsc47m192_data *data;
523 int config;
524 int err;
525
526 data = kzalloc(sizeof(struct smsc47m192_data), GFP_KERNEL);
527 if (!data) {
528 err = -ENOMEM;
529 goto exit;
530 }
531
532 i2c_set_clientdata(client, data);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100533 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
Hartmut Rick59ac8362006-03-23 16:37:23 +0100536 /* Initialize the SMSC47M192 chip */
537 smsc47m192_init_client(client);
538
539 /* Register sysfs hooks */
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200540 if ((err = sysfs_create_group(&client->dev.kobj, &smsc47m192_group)))
Jean Delvare8fb597b2008-07-16 19:30:16 +0200541 goto exit_free;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100542
543 /* Pin 110 is either in4 (+12V) or VID4 */
544 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
545 if (!(config & 0x20)) {
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200546 if ((err = sysfs_create_group(&client->dev.kobj,
547 &smsc47m192_group_in4)))
548 goto exit_remove_files;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100549 }
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200550
Tony Jones1beeffe2007-08-20 13:46:20 -0700551 data->hwmon_dev = hwmon_device_register(&client->dev);
552 if (IS_ERR(data->hwmon_dev)) {
553 err = PTR_ERR(data->hwmon_dev);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200554 goto exit_remove_files;
555 }
Hartmut Rick59ac8362006-03-23 16:37:23 +0100556
557 return 0;
558
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200559exit_remove_files:
560 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
561 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100562exit_free:
563 kfree(data);
564exit:
565 return err;
566}
567
Jean Delvare8fb597b2008-07-16 19:30:16 +0200568static int smsc47m192_remove(struct i2c_client *client)
Hartmut Rick59ac8362006-03-23 16:37:23 +0100569{
570 struct smsc47m192_data *data = i2c_get_clientdata(client);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100571
Tony Jones1beeffe2007-08-20 13:46:20 -0700572 hwmon_device_unregister(data->hwmon_dev);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200573 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
574 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100575
Hartmut Rick59ac8362006-03-23 16:37:23 +0100576 kfree(data);
577
578 return 0;
579}
580
581static struct smsc47m192_data *smsc47m192_update_device(struct device *dev)
582{
583 struct i2c_client *client = to_i2c_client(dev);
584 struct smsc47m192_data *data = i2c_get_clientdata(client);
585 int i, config;
586
Jean Delvaree4a71672007-06-09 10:11:15 -0400587 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100588
589 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
590 || !data->valid) {
591 u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
592
593 dev_dbg(&client->dev, "Starting smsc47m192 update\n");
594
595 for (i = 0; i <= 7; i++) {
596 data->in[i] = i2c_smbus_read_byte_data(client,
597 SMSC47M192_REG_IN(i));
598 data->in_min[i] = i2c_smbus_read_byte_data(client,
599 SMSC47M192_REG_IN_MIN(i));
600 data->in_max[i] = i2c_smbus_read_byte_data(client,
601 SMSC47M192_REG_IN_MAX(i));
602 }
603 for (i = 0; i < 3; i++) {
604 data->temp[i] = i2c_smbus_read_byte_data(client,
605 SMSC47M192_REG_TEMP[i]);
606 data->temp_max[i] = i2c_smbus_read_byte_data(client,
607 SMSC47M192_REG_TEMP_MAX[i]);
608 data->temp_min[i] = i2c_smbus_read_byte_data(client,
609 SMSC47M192_REG_TEMP_MIN[i]);
610 }
611 for (i = 1; i < 3; i++)
612 data->temp_offset[i] = i2c_smbus_read_byte_data(client,
613 SMSC47M192_REG_TEMP_OFFSET(i));
614 /* first offset is temp_offset[0] if SFR bit 4 is set,
615 temp_offset[1] otherwise */
616 if (sfr & 0x10) {
617 data->temp_offset[0] = data->temp_offset[1];
618 data->temp_offset[1] = 0;
619 } else
620 data->temp_offset[0] = 0;
621
622 data->vid = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VID)
623 & 0x0f;
624 config = i2c_smbus_read_byte_data(client,
625 SMSC47M192_REG_CONFIG);
626 if (config & 0x20)
627 data->vid |= (i2c_smbus_read_byte_data(client,
628 SMSC47M192_REG_VID4) & 0x01) << 4;
629 data->alarms = i2c_smbus_read_byte_data(client,
630 SMSC47M192_REG_ALARM1) |
631 (i2c_smbus_read_byte_data(client,
632 SMSC47M192_REG_ALARM2) << 8);
633
634 data->last_updated = jiffies;
635 data->valid = 1;
636 }
637
Jean Delvaree4a71672007-06-09 10:11:15 -0400638 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100639
640 return data;
641}
642
643static int __init smsc47m192_init(void)
644{
645 return i2c_add_driver(&smsc47m192_driver);
646}
647
648static void __exit smsc47m192_exit(void)
649{
650 i2c_del_driver(&smsc47m192_driver);
651}
652
653MODULE_AUTHOR("Hartmut Rick <linux@rick.claranet.de>");
654MODULE_DESCRIPTION("SMSC47M192 driver");
655MODULE_LICENSE("GPL");
656
657module_init(smsc47m192_init);
658module_exit(smsc47m192_exit);