blob: 4d88c045781c6cbac9ebebb83037bc9364318adc [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 */
Jean Delvare52df6442009-12-09 20:35:57 +0100494 version = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VERSION);
495 if (i2c_smbus_read_byte_data(client,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100496 SMSC47M192_REG_COMPANY_ID) == 0x55
Jean Delvare52df6442009-12-09 20:35:57 +0100497 && (version & 0xf0) == 0x20
498 && (i2c_smbus_read_byte_data(client,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100499 SMSC47M192_REG_VID) & 0x70) == 0x00
Jean Delvare52df6442009-12-09 20:35:57 +0100500 && (i2c_smbus_read_byte_data(client,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100501 SMSC47M192_REG_VID4) & 0xfe) == 0x80) {
Jean Delvare52df6442009-12-09 20:35:57 +0100502 dev_info(&adapter->dev,
503 "found SMSC47M192 or compatible, "
504 "version 2, stepping A%d\n", version & 0x0f);
505 } else {
506 dev_dbg(&adapter->dev,
507 "SMSC47M192 detection failed at 0x%02x\n",
508 client->addr);
509 return -ENODEV;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100510 }
511
Jean Delvare8fb597b2008-07-16 19:30:16 +0200512 strlcpy(info->type, "smsc47m192", I2C_NAME_SIZE);
513
514 return 0;
515}
516
517static int smsc47m192_probe(struct i2c_client *client,
518 const struct i2c_device_id *id)
519{
520 struct smsc47m192_data *data;
521 int config;
522 int err;
523
524 data = kzalloc(sizeof(struct smsc47m192_data), GFP_KERNEL);
525 if (!data) {
526 err = -ENOMEM;
527 goto exit;
528 }
529
530 i2c_set_clientdata(client, data);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100531 data->vrm = vid_which_vrm();
Jean Delvaree4a71672007-06-09 10:11:15 -0400532 mutex_init(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100533
Hartmut Rick59ac8362006-03-23 16:37:23 +0100534 /* Initialize the SMSC47M192 chip */
535 smsc47m192_init_client(client);
536
537 /* Register sysfs hooks */
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200538 if ((err = sysfs_create_group(&client->dev.kobj, &smsc47m192_group)))
Jean Delvare8fb597b2008-07-16 19:30:16 +0200539 goto exit_free;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100540
541 /* Pin 110 is either in4 (+12V) or VID4 */
542 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
543 if (!(config & 0x20)) {
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200544 if ((err = sysfs_create_group(&client->dev.kobj,
545 &smsc47m192_group_in4)))
546 goto exit_remove_files;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100547 }
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200548
Tony Jones1beeffe2007-08-20 13:46:20 -0700549 data->hwmon_dev = hwmon_device_register(&client->dev);
550 if (IS_ERR(data->hwmon_dev)) {
551 err = PTR_ERR(data->hwmon_dev);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200552 goto exit_remove_files;
553 }
Hartmut Rick59ac8362006-03-23 16:37:23 +0100554
555 return 0;
556
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200557exit_remove_files:
558 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
559 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100560exit_free:
561 kfree(data);
562exit:
563 return err;
564}
565
Jean Delvare8fb597b2008-07-16 19:30:16 +0200566static int smsc47m192_remove(struct i2c_client *client)
Hartmut Rick59ac8362006-03-23 16:37:23 +0100567{
568 struct smsc47m192_data *data = i2c_get_clientdata(client);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100569
Tony Jones1beeffe2007-08-20 13:46:20 -0700570 hwmon_device_unregister(data->hwmon_dev);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200571 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
572 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100573
Hartmut Rick59ac8362006-03-23 16:37:23 +0100574 kfree(data);
575
576 return 0;
577}
578
579static struct smsc47m192_data *smsc47m192_update_device(struct device *dev)
580{
581 struct i2c_client *client = to_i2c_client(dev);
582 struct smsc47m192_data *data = i2c_get_clientdata(client);
583 int i, config;
584
Jean Delvaree4a71672007-06-09 10:11:15 -0400585 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100586
587 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
588 || !data->valid) {
589 u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
590
591 dev_dbg(&client->dev, "Starting smsc47m192 update\n");
592
593 for (i = 0; i <= 7; i++) {
594 data->in[i] = i2c_smbus_read_byte_data(client,
595 SMSC47M192_REG_IN(i));
596 data->in_min[i] = i2c_smbus_read_byte_data(client,
597 SMSC47M192_REG_IN_MIN(i));
598 data->in_max[i] = i2c_smbus_read_byte_data(client,
599 SMSC47M192_REG_IN_MAX(i));
600 }
601 for (i = 0; i < 3; i++) {
602 data->temp[i] = i2c_smbus_read_byte_data(client,
603 SMSC47M192_REG_TEMP[i]);
604 data->temp_max[i] = i2c_smbus_read_byte_data(client,
605 SMSC47M192_REG_TEMP_MAX[i]);
606 data->temp_min[i] = i2c_smbus_read_byte_data(client,
607 SMSC47M192_REG_TEMP_MIN[i]);
608 }
609 for (i = 1; i < 3; i++)
610 data->temp_offset[i] = i2c_smbus_read_byte_data(client,
611 SMSC47M192_REG_TEMP_OFFSET(i));
612 /* first offset is temp_offset[0] if SFR bit 4 is set,
613 temp_offset[1] otherwise */
614 if (sfr & 0x10) {
615 data->temp_offset[0] = data->temp_offset[1];
616 data->temp_offset[1] = 0;
617 } else
618 data->temp_offset[0] = 0;
619
620 data->vid = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VID)
621 & 0x0f;
622 config = i2c_smbus_read_byte_data(client,
623 SMSC47M192_REG_CONFIG);
624 if (config & 0x20)
625 data->vid |= (i2c_smbus_read_byte_data(client,
626 SMSC47M192_REG_VID4) & 0x01) << 4;
627 data->alarms = i2c_smbus_read_byte_data(client,
628 SMSC47M192_REG_ALARM1) |
629 (i2c_smbus_read_byte_data(client,
630 SMSC47M192_REG_ALARM2) << 8);
631
632 data->last_updated = jiffies;
633 data->valid = 1;
634 }
635
Jean Delvaree4a71672007-06-09 10:11:15 -0400636 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100637
638 return data;
639}
640
641static int __init smsc47m192_init(void)
642{
643 return i2c_add_driver(&smsc47m192_driver);
644}
645
646static void __exit smsc47m192_exit(void)
647{
648 i2c_del_driver(&smsc47m192_driver);
649}
650
651MODULE_AUTHOR("Hartmut Rick <linux@rick.claranet.de>");
652MODULE_DESCRIPTION("SMSC47M192 driver");
653MODULE_LICENSE("GPL");
654
655module_init(smsc47m192_init);
656module_exit(smsc47m192_exit);