blob: 40b26673d87f3563946dd34a8cac909b31ddfdaa [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
Hartmut Rick59ac8362006-03-23 16:37:23 +010039/* SMSC47M192 registers */
40#define SMSC47M192_REG_IN(nr) ((nr)<6 ? (0x20 + (nr)) : \
41 (0x50 + (nr) - 6))
42#define SMSC47M192_REG_IN_MAX(nr) ((nr)<6 ? (0x2b + (nr) * 2) : \
43 (0x54 + (((nr) - 6) * 2)))
44#define SMSC47M192_REG_IN_MIN(nr) ((nr)<6 ? (0x2c + (nr) * 2) : \
45 (0x55 + (((nr) - 6) * 2)))
46static u8 SMSC47M192_REG_TEMP[3] = { 0x27, 0x26, 0x52 };
47static u8 SMSC47M192_REG_TEMP_MAX[3] = { 0x39, 0x37, 0x58 };
48static u8 SMSC47M192_REG_TEMP_MIN[3] = { 0x3A, 0x38, 0x59 };
49#define SMSC47M192_REG_TEMP_OFFSET(nr) ((nr)==2 ? 0x1e : 0x1f)
50#define SMSC47M192_REG_ALARM1 0x41
51#define SMSC47M192_REG_ALARM2 0x42
52#define SMSC47M192_REG_VID 0x47
53#define SMSC47M192_REG_VID4 0x49
54#define SMSC47M192_REG_CONFIG 0x40
55#define SMSC47M192_REG_SFR 0x4f
56#define SMSC47M192_REG_COMPANY_ID 0x3e
57#define SMSC47M192_REG_VERSION 0x3f
58
59/* generalised scaling with integer rounding */
60static inline int SCALE(long val, int mul, int div)
61{
62 if (val < 0)
63 return (val * mul - div / 2) / div;
64 else
65 return (val * mul + div / 2) / div;
66}
67
68/* Conversions */
69
70/* smsc47m192 internally scales voltage measurements */
71static const u16 nom_mv[] = { 2500, 2250, 3300, 5000, 12000, 3300, 1500, 1800 };
72
73static inline unsigned int IN_FROM_REG(u8 reg, int n)
74{
75 return SCALE(reg, nom_mv[n], 192);
76}
77
78static inline u8 IN_TO_REG(unsigned long val, int n)
79{
80 return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
81}
82
83/* TEMP: 0.001 degC units (-128C to +127C)
84 REG: 1C/bit, two's complement */
85static inline s8 TEMP_TO_REG(int val)
86{
87 return SENSORS_LIMIT(SCALE(val, 1, 1000), -128000, 127000);
88}
89
90static inline int TEMP_FROM_REG(s8 val)
91{
92 return val * 1000;
93}
94
95struct smsc47m192_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070096 struct device *hwmon_dev;
Jean Delvaree4a71672007-06-09 10:11:15 -040097 struct mutex update_lock;
Hartmut Rick59ac8362006-03-23 16:37:23 +010098 char valid; /* !=0 if following fields are valid */
99 unsigned long last_updated; /* In jiffies */
100
101 u8 in[8]; /* Register value */
102 u8 in_max[8]; /* Register value */
103 u8 in_min[8]; /* Register value */
104 s8 temp[3]; /* Register value */
105 s8 temp_max[3]; /* Register value */
106 s8 temp_min[3]; /* Register value */
107 s8 temp_offset[3]; /* Register value */
108 u16 alarms; /* Register encoding, combined */
109 u8 vid; /* Register encoding, combined */
110 u8 vrm;
111};
112
Jean Delvare8fb597b2008-07-16 19:30:16 +0200113static int smsc47m192_probe(struct i2c_client *client,
114 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100115static int smsc47m192_detect(struct i2c_client *client,
Jean Delvare8fb597b2008-07-16 19:30:16 +0200116 struct i2c_board_info *info);
117static int smsc47m192_remove(struct i2c_client *client);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100118static struct smsc47m192_data *smsc47m192_update_device(struct device *dev);
119
Jean Delvare8fb597b2008-07-16 19:30:16 +0200120static const struct i2c_device_id smsc47m192_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100121 { "smsc47m192", 0 },
Jean Delvare8fb597b2008-07-16 19:30:16 +0200122 { }
123};
124MODULE_DEVICE_TABLE(i2c, smsc47m192_id);
125
Hartmut Rick59ac8362006-03-23 16:37:23 +0100126static struct i2c_driver smsc47m192_driver = {
Jean Delvare8fb597b2008-07-16 19:30:16 +0200127 .class = I2C_CLASS_HWMON,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100128 .driver = {
129 .name = "smsc47m192",
130 },
Jean Delvare8fb597b2008-07-16 19:30:16 +0200131 .probe = smsc47m192_probe,
132 .remove = smsc47m192_remove,
133 .id_table = smsc47m192_id,
134 .detect = smsc47m192_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100135 .address_list = normal_i2c,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100136};
137
138/* Voltages */
139static ssize_t show_in(struct device *dev, struct device_attribute *attr,
140 char *buf)
141{
142 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
143 int nr = sensor_attr->index;
144 struct smsc47m192_data *data = smsc47m192_update_device(dev);
145 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr));
146}
147
148static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
149 char *buf)
150{
151 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
152 int nr = sensor_attr->index;
153 struct smsc47m192_data *data = smsc47m192_update_device(dev);
154 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr));
155}
156
157static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
158 char *buf)
159{
160 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
161 int nr = sensor_attr->index;
162 struct smsc47m192_data *data = smsc47m192_update_device(dev);
163 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr));
164}
165
166static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
167 const char *buf, size_t count)
168{
169 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
170 int nr = sensor_attr->index;
171 struct i2c_client *client = to_i2c_client(dev);
172 struct smsc47m192_data *data = i2c_get_clientdata(client);
173 unsigned long val = simple_strtoul(buf, NULL, 10);
174
Jean Delvaree4a71672007-06-09 10:11:15 -0400175 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100176 data->in_min[nr] = IN_TO_REG(val, nr);
177 i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MIN(nr),
178 data->in_min[nr]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400179 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100180 return count;
181}
182
183static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
184 const char *buf, size_t count)
185{
186 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
187 int nr = sensor_attr->index;
188 struct i2c_client *client = to_i2c_client(dev);
189 struct smsc47m192_data *data = i2c_get_clientdata(client);
190 unsigned long val = simple_strtoul(buf, NULL, 10);
191
Jean Delvaree4a71672007-06-09 10:11:15 -0400192 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100193 data->in_max[nr] = IN_TO_REG(val, nr);
194 i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MAX(nr),
195 data->in_max[nr]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400196 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100197 return count;
198}
199
200#define show_in_offset(offset) \
201static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
202 show_in, NULL, offset); \
203static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
204 show_in_min, set_in_min, offset); \
205static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
206 show_in_max, set_in_max, offset);
207
208show_in_offset(0)
209show_in_offset(1)
210show_in_offset(2)
211show_in_offset(3)
212show_in_offset(4)
213show_in_offset(5)
214show_in_offset(6)
215show_in_offset(7)
216
217/* Temperatures */
218static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
219 char *buf)
220{
221 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
222 int nr = sensor_attr->index;
223 struct smsc47m192_data *data = smsc47m192_update_device(dev);
224 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
225}
226
227static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
228 char *buf)
229{
230 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
231 int nr = sensor_attr->index;
232 struct smsc47m192_data *data = smsc47m192_update_device(dev);
233 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
234}
235
236static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
237 char *buf)
238{
239 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
240 int nr = sensor_attr->index;
241 struct smsc47m192_data *data = smsc47m192_update_device(dev);
242 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
243}
244
245static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
246 const char *buf, size_t count)
247{
248 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
249 int nr = sensor_attr->index;
250 struct i2c_client *client = to_i2c_client(dev);
251 struct smsc47m192_data *data = i2c_get_clientdata(client);
252 long val = simple_strtol(buf, NULL, 10);
253
Jean Delvaree4a71672007-06-09 10:11:15 -0400254 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100255 data->temp_min[nr] = TEMP_TO_REG(val);
256 i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MIN[nr],
257 data->temp_min[nr]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400258 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100259 return count;
260}
261
262static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
263 const char *buf, size_t count)
264{
265 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
266 int nr = sensor_attr->index;
267 struct i2c_client *client = to_i2c_client(dev);
268 struct smsc47m192_data *data = i2c_get_clientdata(client);
269 long val = simple_strtol(buf, NULL, 10);
270
Jean Delvaree4a71672007-06-09 10:11:15 -0400271 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100272 data->temp_max[nr] = TEMP_TO_REG(val);
273 i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MAX[nr],
274 data->temp_max[nr]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400275 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100276 return count;
277}
278
279static ssize_t show_temp_offset(struct device *dev, struct device_attribute
280 *attr, char *buf)
281{
282 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
283 int nr = sensor_attr->index;
284 struct smsc47m192_data *data = smsc47m192_update_device(dev);
285 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
286}
287
288static ssize_t set_temp_offset(struct device *dev, struct device_attribute
289 *attr, const char *buf, size_t count)
290{
291 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
292 int nr = sensor_attr->index;
293 struct i2c_client *client = to_i2c_client(dev);
294 struct smsc47m192_data *data = i2c_get_clientdata(client);
295 u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
296 long val = simple_strtol(buf, NULL, 10);
297
Jean Delvaree4a71672007-06-09 10:11:15 -0400298 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100299 data->temp_offset[nr] = TEMP_TO_REG(val);
300 if (nr>1)
301 i2c_smbus_write_byte_data(client,
302 SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
303 else if (data->temp_offset[nr] != 0) {
304 /* offset[0] and offset[1] share the same register,
305 SFR bit 4 activates offset[0] */
306 i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
307 (sfr & 0xef) | (nr==0 ? 0x10 : 0));
308 data->temp_offset[1-nr] = 0;
309 i2c_smbus_write_byte_data(client,
310 SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
311 } else if ((sfr & 0x10) == (nr==0 ? 0x10 : 0))
312 i2c_smbus_write_byte_data(client,
313 SMSC47M192_REG_TEMP_OFFSET(nr), 0);
Jean Delvaree4a71672007-06-09 10:11:15 -0400314 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100315 return count;
316}
317
318#define show_temp_index(index) \
319static SENSOR_DEVICE_ATTR(temp##index##_input, S_IRUGO, \
320 show_temp, NULL, index-1); \
321static SENSOR_DEVICE_ATTR(temp##index##_min, S_IRUGO | S_IWUSR, \
322 show_temp_min, set_temp_min, index-1); \
323static SENSOR_DEVICE_ATTR(temp##index##_max, S_IRUGO | S_IWUSR, \
324 show_temp_max, set_temp_max, index-1); \
325static SENSOR_DEVICE_ATTR(temp##index##_offset, S_IRUGO | S_IWUSR, \
326 show_temp_offset, set_temp_offset, index-1);
327
328show_temp_index(1)
329show_temp_index(2)
330show_temp_index(3)
331
332/* VID */
333static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
334 char *buf)
335{
336 struct smsc47m192_data *data = smsc47m192_update_device(dev);
337 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
338}
339static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
340
341static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
342 char *buf)
343{
Jean Delvare90d66192007-10-08 18:24:35 +0200344 struct smsc47m192_data *data = dev_get_drvdata(dev);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100345 return sprintf(buf, "%d\n", data->vrm);
346}
347
348static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
349 const char *buf, size_t count)
350{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100351 struct smsc47m192_data *data = dev_get_drvdata(dev);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100352 data->vrm = simple_strtoul(buf, NULL, 10);
353 return count;
354}
355static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
356
357/* Alarms */
358static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
359 char *buf)
360{
361 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
362 int nr = sensor_attr->index;
363 struct smsc47m192_data *data = smsc47m192_update_device(dev);
364 return sprintf(buf, "%u\n", (data->alarms & nr) ? 1 : 0);
365}
366
367static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0x0010);
368static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0x0020);
369static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0x0040);
Jean Delvare7817a392007-06-09 10:11:16 -0400370static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 0x4000);
371static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 0x8000);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100372static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0x0001);
373static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0x0002);
374static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 0x0004);
375static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 0x0008);
376static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 0x0100);
377static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 0x0200);
378static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 0x0400);
379static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 0x0800);
380
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200381static struct attribute *smsc47m192_attributes[] = {
382 &sensor_dev_attr_in0_input.dev_attr.attr,
383 &sensor_dev_attr_in0_min.dev_attr.attr,
384 &sensor_dev_attr_in0_max.dev_attr.attr,
385 &sensor_dev_attr_in0_alarm.dev_attr.attr,
386 &sensor_dev_attr_in1_input.dev_attr.attr,
387 &sensor_dev_attr_in1_min.dev_attr.attr,
388 &sensor_dev_attr_in1_max.dev_attr.attr,
389 &sensor_dev_attr_in1_alarm.dev_attr.attr,
390 &sensor_dev_attr_in2_input.dev_attr.attr,
391 &sensor_dev_attr_in2_min.dev_attr.attr,
392 &sensor_dev_attr_in2_max.dev_attr.attr,
393 &sensor_dev_attr_in2_alarm.dev_attr.attr,
394 &sensor_dev_attr_in3_input.dev_attr.attr,
395 &sensor_dev_attr_in3_min.dev_attr.attr,
396 &sensor_dev_attr_in3_max.dev_attr.attr,
397 &sensor_dev_attr_in3_alarm.dev_attr.attr,
398 &sensor_dev_attr_in5_input.dev_attr.attr,
399 &sensor_dev_attr_in5_min.dev_attr.attr,
400 &sensor_dev_attr_in5_max.dev_attr.attr,
401 &sensor_dev_attr_in5_alarm.dev_attr.attr,
402 &sensor_dev_attr_in6_input.dev_attr.attr,
403 &sensor_dev_attr_in6_min.dev_attr.attr,
404 &sensor_dev_attr_in6_max.dev_attr.attr,
405 &sensor_dev_attr_in6_alarm.dev_attr.attr,
406 &sensor_dev_attr_in7_input.dev_attr.attr,
407 &sensor_dev_attr_in7_min.dev_attr.attr,
408 &sensor_dev_attr_in7_max.dev_attr.attr,
409 &sensor_dev_attr_in7_alarm.dev_attr.attr,
410
411 &sensor_dev_attr_temp1_input.dev_attr.attr,
412 &sensor_dev_attr_temp1_max.dev_attr.attr,
413 &sensor_dev_attr_temp1_min.dev_attr.attr,
414 &sensor_dev_attr_temp1_offset.dev_attr.attr,
415 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
416 &sensor_dev_attr_temp2_input.dev_attr.attr,
417 &sensor_dev_attr_temp2_max.dev_attr.attr,
418 &sensor_dev_attr_temp2_min.dev_attr.attr,
419 &sensor_dev_attr_temp2_offset.dev_attr.attr,
420 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400421 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200422 &sensor_dev_attr_temp3_input.dev_attr.attr,
423 &sensor_dev_attr_temp3_max.dev_attr.attr,
424 &sensor_dev_attr_temp3_min.dev_attr.attr,
425 &sensor_dev_attr_temp3_offset.dev_attr.attr,
426 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400427 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200428
429 &dev_attr_cpu0_vid.attr,
430 &dev_attr_vrm.attr,
431 NULL
432};
433
434static const struct attribute_group smsc47m192_group = {
435 .attrs = smsc47m192_attributes,
436};
437
438static struct attribute *smsc47m192_attributes_in4[] = {
439 &sensor_dev_attr_in4_input.dev_attr.attr,
440 &sensor_dev_attr_in4_min.dev_attr.attr,
441 &sensor_dev_attr_in4_max.dev_attr.attr,
442 &sensor_dev_attr_in4_alarm.dev_attr.attr,
443 NULL
444};
445
446static const struct attribute_group smsc47m192_group_in4 = {
447 .attrs = smsc47m192_attributes_in4,
448};
449
Hartmut Rick59ac8362006-03-23 16:37:23 +0100450static void smsc47m192_init_client(struct i2c_client *client)
451{
452 int i;
453 u8 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
454 u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
455
456 /* select cycle mode (pause 1 sec between updates) */
457 i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
458 (sfr & 0xfd) | 0x02);
459 if (!(config & 0x01)) {
460 /* initialize alarm limits */
461 for (i=0; i<8; i++) {
462 i2c_smbus_write_byte_data(client,
463 SMSC47M192_REG_IN_MIN(i), 0);
464 i2c_smbus_write_byte_data(client,
465 SMSC47M192_REG_IN_MAX(i), 0xff);
466 }
467 for (i=0; i<3; i++) {
468 i2c_smbus_write_byte_data(client,
469 SMSC47M192_REG_TEMP_MIN[i], 0x80);
470 i2c_smbus_write_byte_data(client,
471 SMSC47M192_REG_TEMP_MAX[i], 0x7f);
472 }
473
474 /* start monitoring */
475 i2c_smbus_write_byte_data(client, SMSC47M192_REG_CONFIG,
476 (config & 0xf7) | 0x01);
477 }
478}
479
Jean Delvare8fb597b2008-07-16 19:30:16 +0200480/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100481static int smsc47m192_detect(struct i2c_client *client,
Jean Delvare8fb597b2008-07-16 19:30:16 +0200482 struct i2c_board_info *info)
Hartmut Rick59ac8362006-03-23 16:37:23 +0100483{
Jean Delvare8fb597b2008-07-16 19:30:16 +0200484 struct i2c_adapter *adapter = client->adapter;
485 int version;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100486
487 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare8fb597b2008-07-16 19:30:16 +0200488 return -ENODEV;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100489
490 /* Detection criteria from sensors_detect script */
Jean Delvare52df6442009-12-09 20:35:57 +0100491 version = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VERSION);
492 if (i2c_smbus_read_byte_data(client,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100493 SMSC47M192_REG_COMPANY_ID) == 0x55
Jean Delvare52df6442009-12-09 20:35:57 +0100494 && (version & 0xf0) == 0x20
495 && (i2c_smbus_read_byte_data(client,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100496 SMSC47M192_REG_VID) & 0x70) == 0x00
Jean Delvare52df6442009-12-09 20:35:57 +0100497 && (i2c_smbus_read_byte_data(client,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100498 SMSC47M192_REG_VID4) & 0xfe) == 0x80) {
Jean Delvare52df6442009-12-09 20:35:57 +0100499 dev_info(&adapter->dev,
500 "found SMSC47M192 or compatible, "
501 "version 2, stepping A%d\n", version & 0x0f);
502 } else {
503 dev_dbg(&adapter->dev,
504 "SMSC47M192 detection failed at 0x%02x\n",
505 client->addr);
506 return -ENODEV;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100507 }
508
Jean Delvare8fb597b2008-07-16 19:30:16 +0200509 strlcpy(info->type, "smsc47m192", I2C_NAME_SIZE);
510
511 return 0;
512}
513
514static int smsc47m192_probe(struct i2c_client *client,
515 const struct i2c_device_id *id)
516{
517 struct smsc47m192_data *data;
518 int config;
519 int err;
520
521 data = kzalloc(sizeof(struct smsc47m192_data), GFP_KERNEL);
522 if (!data) {
523 err = -ENOMEM;
524 goto exit;
525 }
526
527 i2c_set_clientdata(client, data);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100528 data->vrm = vid_which_vrm();
Jean Delvaree4a71672007-06-09 10:11:15 -0400529 mutex_init(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100530
Hartmut Rick59ac8362006-03-23 16:37:23 +0100531 /* Initialize the SMSC47M192 chip */
532 smsc47m192_init_client(client);
533
534 /* Register sysfs hooks */
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200535 if ((err = sysfs_create_group(&client->dev.kobj, &smsc47m192_group)))
Jean Delvare8fb597b2008-07-16 19:30:16 +0200536 goto exit_free;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100537
538 /* Pin 110 is either in4 (+12V) or VID4 */
539 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
540 if (!(config & 0x20)) {
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200541 if ((err = sysfs_create_group(&client->dev.kobj,
542 &smsc47m192_group_in4)))
543 goto exit_remove_files;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100544 }
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200545
Tony Jones1beeffe2007-08-20 13:46:20 -0700546 data->hwmon_dev = hwmon_device_register(&client->dev);
547 if (IS_ERR(data->hwmon_dev)) {
548 err = PTR_ERR(data->hwmon_dev);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200549 goto exit_remove_files;
550 }
Hartmut Rick59ac8362006-03-23 16:37:23 +0100551
552 return 0;
553
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200554exit_remove_files:
555 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
556 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100557exit_free:
558 kfree(data);
559exit:
560 return err;
561}
562
Jean Delvare8fb597b2008-07-16 19:30:16 +0200563static int smsc47m192_remove(struct i2c_client *client)
Hartmut Rick59ac8362006-03-23 16:37:23 +0100564{
565 struct smsc47m192_data *data = i2c_get_clientdata(client);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100566
Tony Jones1beeffe2007-08-20 13:46:20 -0700567 hwmon_device_unregister(data->hwmon_dev);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200568 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
569 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100570
Hartmut Rick59ac8362006-03-23 16:37:23 +0100571 kfree(data);
572
573 return 0;
574}
575
576static struct smsc47m192_data *smsc47m192_update_device(struct device *dev)
577{
578 struct i2c_client *client = to_i2c_client(dev);
579 struct smsc47m192_data *data = i2c_get_clientdata(client);
580 int i, config;
581
Jean Delvaree4a71672007-06-09 10:11:15 -0400582 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100583
584 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
585 || !data->valid) {
586 u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
587
588 dev_dbg(&client->dev, "Starting smsc47m192 update\n");
589
590 for (i = 0; i <= 7; i++) {
591 data->in[i] = i2c_smbus_read_byte_data(client,
592 SMSC47M192_REG_IN(i));
593 data->in_min[i] = i2c_smbus_read_byte_data(client,
594 SMSC47M192_REG_IN_MIN(i));
595 data->in_max[i] = i2c_smbus_read_byte_data(client,
596 SMSC47M192_REG_IN_MAX(i));
597 }
598 for (i = 0; i < 3; i++) {
599 data->temp[i] = i2c_smbus_read_byte_data(client,
600 SMSC47M192_REG_TEMP[i]);
601 data->temp_max[i] = i2c_smbus_read_byte_data(client,
602 SMSC47M192_REG_TEMP_MAX[i]);
603 data->temp_min[i] = i2c_smbus_read_byte_data(client,
604 SMSC47M192_REG_TEMP_MIN[i]);
605 }
606 for (i = 1; i < 3; i++)
607 data->temp_offset[i] = i2c_smbus_read_byte_data(client,
608 SMSC47M192_REG_TEMP_OFFSET(i));
609 /* first offset is temp_offset[0] if SFR bit 4 is set,
610 temp_offset[1] otherwise */
611 if (sfr & 0x10) {
612 data->temp_offset[0] = data->temp_offset[1];
613 data->temp_offset[1] = 0;
614 } else
615 data->temp_offset[0] = 0;
616
617 data->vid = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VID)
618 & 0x0f;
619 config = i2c_smbus_read_byte_data(client,
620 SMSC47M192_REG_CONFIG);
621 if (config & 0x20)
622 data->vid |= (i2c_smbus_read_byte_data(client,
623 SMSC47M192_REG_VID4) & 0x01) << 4;
624 data->alarms = i2c_smbus_read_byte_data(client,
625 SMSC47M192_REG_ALARM1) |
626 (i2c_smbus_read_byte_data(client,
627 SMSC47M192_REG_ALARM2) << 8);
628
629 data->last_updated = jiffies;
630 data->valid = 1;
631 }
632
Jean Delvaree4a71672007-06-09 10:11:15 -0400633 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100634
635 return data;
636}
637
638static int __init smsc47m192_init(void)
639{
640 return i2c_add_driver(&smsc47m192_driver);
641}
642
643static void __exit smsc47m192_exit(void)
644{
645 i2c_del_driver(&smsc47m192_driver);
646}
647
648MODULE_AUTHOR("Hartmut Rick <linux@rick.claranet.de>");
649MODULE_DESCRIPTION("SMSC47M192 driver");
650MODULE_LICENSE("GPL");
651
652module_init(smsc47m192_init);
653module_exit(smsc47m192_exit);