blob: 34b9a601ad078c394513e67a5dccc345c6c02fef [file] [log] [blame]
Hartmut Rick59ac8362006-03-23 16:37:23 +01001/*
Guenter Roeck7cc3cb62012-01-14 22:31:08 -08002 * smsc47m192.c - Support for hardware monitoring block of
3 * SMSC LPC47M192 and compatible Super I/O chips
4 *
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 */
Hartmut Rick59ac8362006-03-23 16:37:23 +010023
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 */
Guenter Roeck7cc3cb62012-01-14 22:31:08 -080040#define SMSC47M192_REG_IN(nr) ((nr) < 6 ? (0x20 + (nr)) : \
Hartmut Rick59ac8362006-03-23 16:37:23 +010041 (0x50 + (nr) - 6))
Guenter Roeck7cc3cb62012-01-14 22:31:08 -080042#define SMSC47M192_REG_IN_MAX(nr) ((nr) < 6 ? (0x2b + (nr) * 2) : \
Hartmut Rick59ac8362006-03-23 16:37:23 +010043 (0x54 + (((nr) - 6) * 2)))
Guenter Roeck7cc3cb62012-01-14 22:31:08 -080044#define SMSC47M192_REG_IN_MIN(nr) ((nr) < 6 ? (0x2c + (nr) * 2) : \
Hartmut Rick59ac8362006-03-23 16:37:23 +010045 (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 };
Guenter Roeck7cc3cb62012-01-14 22:31:08 -080049#define SMSC47M192_REG_TEMP_OFFSET(nr) ((nr) == 2 ? 0x1e : 0x1f)
Hartmut Rick59ac8362006-03-23 16:37:23 +010050#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{
Guenter Roeck2a844c12013-01-09 08:09:34 -080080 return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255);
Hartmut Rick59ac8362006-03-23 16:37:23 +010081}
82
Guenter Roeck7cc3cb62012-01-14 22:31:08 -080083/*
84 * TEMP: 0.001 degC units (-128C to +127C)
85 * REG: 1C/bit, two's complement
86 */
Hartmut Rick59ac8362006-03-23 16:37:23 +010087static inline s8 TEMP_TO_REG(int val)
88{
Guenter Roeck043572d2014-07-18 07:31:18 -070089 return SCALE(clamp_val(val, -128000, 127000), 1, 1000);
Hartmut Rick59ac8362006-03-23 16:37:23 +010090}
91
92static inline int TEMP_FROM_REG(s8 val)
93{
94 return val * 1000;
95}
96
97struct smsc47m192_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070098 struct device *hwmon_dev;
Jean Delvaree4a71672007-06-09 10:11:15 -040099 struct mutex update_lock;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100100 char valid; /* !=0 if following fields are valid */
101 unsigned long last_updated; /* In jiffies */
102
103 u8 in[8]; /* Register value */
104 u8 in_max[8]; /* Register value */
105 u8 in_min[8]; /* Register value */
106 s8 temp[3]; /* Register value */
107 s8 temp_max[3]; /* Register value */
108 s8 temp_min[3]; /* Register value */
109 s8 temp_offset[3]; /* Register value */
110 u16 alarms; /* Register encoding, combined */
111 u8 vid; /* Register encoding, combined */
112 u8 vrm;
113};
114
Jean Delvare8fb597b2008-07-16 19:30:16 +0200115static int smsc47m192_probe(struct i2c_client *client,
116 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100117static int smsc47m192_detect(struct i2c_client *client,
Jean Delvare8fb597b2008-07-16 19:30:16 +0200118 struct i2c_board_info *info);
119static int smsc47m192_remove(struct i2c_client *client);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100120static struct smsc47m192_data *smsc47m192_update_device(struct device *dev);
121
Jean Delvare8fb597b2008-07-16 19:30:16 +0200122static const struct i2c_device_id smsc47m192_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100123 { "smsc47m192", 0 },
Jean Delvare8fb597b2008-07-16 19:30:16 +0200124 { }
125};
126MODULE_DEVICE_TABLE(i2c, smsc47m192_id);
127
Hartmut Rick59ac8362006-03-23 16:37:23 +0100128static struct i2c_driver smsc47m192_driver = {
Jean Delvare8fb597b2008-07-16 19:30:16 +0200129 .class = I2C_CLASS_HWMON,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100130 .driver = {
131 .name = "smsc47m192",
132 },
Jean Delvare8fb597b2008-07-16 19:30:16 +0200133 .probe = smsc47m192_probe,
134 .remove = smsc47m192_remove,
135 .id_table = smsc47m192_id,
136 .detect = smsc47m192_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100137 .address_list = normal_i2c,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100138};
139
140/* Voltages */
141static ssize_t show_in(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[nr], nr));
148}
149
150static ssize_t show_in_min(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_min[nr], nr));
157}
158
159static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
160 char *buf)
161{
162 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
163 int nr = sensor_attr->index;
164 struct smsc47m192_data *data = smsc47m192_update_device(dev);
165 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr));
166}
167
168static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
169 const char *buf, size_t count)
170{
171 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
172 int nr = sensor_attr->index;
173 struct i2c_client *client = to_i2c_client(dev);
174 struct smsc47m192_data *data = i2c_get_clientdata(client);
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800175 unsigned long val;
176 int err;
177
178 err = kstrtoul(buf, 10, &val);
179 if (err)
180 return err;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100181
Jean Delvaree4a71672007-06-09 10:11:15 -0400182 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100183 data->in_min[nr] = IN_TO_REG(val, nr);
184 i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MIN(nr),
185 data->in_min[nr]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400186 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100187 return count;
188}
189
190static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
191 const char *buf, size_t count)
192{
193 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
194 int nr = sensor_attr->index;
195 struct i2c_client *client = to_i2c_client(dev);
196 struct smsc47m192_data *data = i2c_get_clientdata(client);
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800197 unsigned long val;
198 int err;
199
200 err = kstrtoul(buf, 10, &val);
201 if (err)
202 return err;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100203
Jean Delvaree4a71672007-06-09 10:11:15 -0400204 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100205 data->in_max[nr] = IN_TO_REG(val, nr);
206 i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MAX(nr),
207 data->in_max[nr]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400208 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100209 return count;
210}
211
212#define show_in_offset(offset) \
213static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
214 show_in, NULL, offset); \
215static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
216 show_in_min, set_in_min, offset); \
217static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
218 show_in_max, set_in_max, offset);
219
220show_in_offset(0)
221show_in_offset(1)
222show_in_offset(2)
223show_in_offset(3)
224show_in_offset(4)
225show_in_offset(5)
226show_in_offset(6)
227show_in_offset(7)
228
229/* Temperatures */
230static ssize_t show_temp(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[nr]));
237}
238
239static ssize_t show_temp_min(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_min[nr]));
246}
247
248static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
249 char *buf)
250{
251 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
252 int nr = sensor_attr->index;
253 struct smsc47m192_data *data = smsc47m192_update_device(dev);
254 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
255}
256
257static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
258 const char *buf, size_t count)
259{
260 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
261 int nr = sensor_attr->index;
262 struct i2c_client *client = to_i2c_client(dev);
263 struct smsc47m192_data *data = i2c_get_clientdata(client);
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800264 long val;
265 int err;
266
267 err = kstrtol(buf, 10, &val);
268 if (err)
269 return err;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100270
Jean Delvaree4a71672007-06-09 10:11:15 -0400271 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100272 data->temp_min[nr] = TEMP_TO_REG(val);
273 i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MIN[nr],
274 data->temp_min[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 set_temp_max(struct device *dev, struct device_attribute *attr,
280 const char *buf, size_t count)
281{
282 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
283 int nr = sensor_attr->index;
284 struct i2c_client *client = to_i2c_client(dev);
285 struct smsc47m192_data *data = i2c_get_clientdata(client);
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800286 long val;
287 int err;
288
289 err = kstrtol(buf, 10, &val);
290 if (err)
291 return err;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100292
Jean Delvaree4a71672007-06-09 10:11:15 -0400293 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100294 data->temp_max[nr] = TEMP_TO_REG(val);
295 i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MAX[nr],
296 data->temp_max[nr]);
Jean Delvaree4a71672007-06-09 10:11:15 -0400297 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100298 return count;
299}
300
301static ssize_t show_temp_offset(struct device *dev, struct device_attribute
302 *attr, char *buf)
303{
304 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
305 int nr = sensor_attr->index;
306 struct smsc47m192_data *data = smsc47m192_update_device(dev);
307 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
308}
309
310static ssize_t set_temp_offset(struct device *dev, struct device_attribute
311 *attr, const char *buf, size_t count)
312{
313 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
314 int nr = sensor_attr->index;
315 struct i2c_client *client = to_i2c_client(dev);
316 struct smsc47m192_data *data = i2c_get_clientdata(client);
317 u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800318 long val;
319 int err;
320
321 err = kstrtol(buf, 10, &val);
322 if (err)
323 return err;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100324
Jean Delvaree4a71672007-06-09 10:11:15 -0400325 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100326 data->temp_offset[nr] = TEMP_TO_REG(val);
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800327 if (nr > 1)
Hartmut Rick59ac8362006-03-23 16:37:23 +0100328 i2c_smbus_write_byte_data(client,
329 SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
330 else if (data->temp_offset[nr] != 0) {
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800331 /*
332 * offset[0] and offset[1] share the same register,
333 * SFR bit 4 activates offset[0]
334 */
Hartmut Rick59ac8362006-03-23 16:37:23 +0100335 i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800336 (sfr & 0xef) | (nr == 0 ? 0x10 : 0));
Hartmut Rick59ac8362006-03-23 16:37:23 +0100337 data->temp_offset[1-nr] = 0;
338 i2c_smbus_write_byte_data(client,
339 SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800340 } else if ((sfr & 0x10) == (nr == 0 ? 0x10 : 0))
Hartmut Rick59ac8362006-03-23 16:37:23 +0100341 i2c_smbus_write_byte_data(client,
342 SMSC47M192_REG_TEMP_OFFSET(nr), 0);
Jean Delvaree4a71672007-06-09 10:11:15 -0400343 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100344 return count;
345}
346
347#define show_temp_index(index) \
348static SENSOR_DEVICE_ATTR(temp##index##_input, S_IRUGO, \
349 show_temp, NULL, index-1); \
350static SENSOR_DEVICE_ATTR(temp##index##_min, S_IRUGO | S_IWUSR, \
351 show_temp_min, set_temp_min, index-1); \
352static SENSOR_DEVICE_ATTR(temp##index##_max, S_IRUGO | S_IWUSR, \
353 show_temp_max, set_temp_max, index-1); \
354static SENSOR_DEVICE_ATTR(temp##index##_offset, S_IRUGO | S_IWUSR, \
355 show_temp_offset, set_temp_offset, index-1);
356
357show_temp_index(1)
358show_temp_index(2)
359show_temp_index(3)
360
361/* VID */
362static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
363 char *buf)
364{
365 struct smsc47m192_data *data = smsc47m192_update_device(dev);
366 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
367}
368static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
369
370static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
371 char *buf)
372{
Jean Delvare90d66192007-10-08 18:24:35 +0200373 struct smsc47m192_data *data = dev_get_drvdata(dev);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100374 return sprintf(buf, "%d\n", data->vrm);
375}
376
377static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
378 const char *buf, size_t count)
379{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100380 struct smsc47m192_data *data = dev_get_drvdata(dev);
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800381 unsigned long val;
382 int err;
383
384 err = kstrtoul(buf, 10, &val);
385 if (err)
386 return err;
Guenter Roeck043572d2014-07-18 07:31:18 -0700387 if (val > 255)
388 return -EINVAL;
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800389
390 data->vrm = val;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100391 return count;
392}
393static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
394
395/* Alarms */
396static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
397 char *buf)
398{
399 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
400 int nr = sensor_attr->index;
401 struct smsc47m192_data *data = smsc47m192_update_device(dev);
402 return sprintf(buf, "%u\n", (data->alarms & nr) ? 1 : 0);
403}
404
405static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0x0010);
406static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0x0020);
407static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0x0040);
Jean Delvare7817a392007-06-09 10:11:16 -0400408static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 0x4000);
409static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 0x8000);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100410static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0x0001);
411static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0x0002);
412static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 0x0004);
413static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 0x0008);
414static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 0x0100);
415static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 0x0200);
416static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 0x0400);
417static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 0x0800);
418
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200419static struct attribute *smsc47m192_attributes[] = {
420 &sensor_dev_attr_in0_input.dev_attr.attr,
421 &sensor_dev_attr_in0_min.dev_attr.attr,
422 &sensor_dev_attr_in0_max.dev_attr.attr,
423 &sensor_dev_attr_in0_alarm.dev_attr.attr,
424 &sensor_dev_attr_in1_input.dev_attr.attr,
425 &sensor_dev_attr_in1_min.dev_attr.attr,
426 &sensor_dev_attr_in1_max.dev_attr.attr,
427 &sensor_dev_attr_in1_alarm.dev_attr.attr,
428 &sensor_dev_attr_in2_input.dev_attr.attr,
429 &sensor_dev_attr_in2_min.dev_attr.attr,
430 &sensor_dev_attr_in2_max.dev_attr.attr,
431 &sensor_dev_attr_in2_alarm.dev_attr.attr,
432 &sensor_dev_attr_in3_input.dev_attr.attr,
433 &sensor_dev_attr_in3_min.dev_attr.attr,
434 &sensor_dev_attr_in3_max.dev_attr.attr,
435 &sensor_dev_attr_in3_alarm.dev_attr.attr,
436 &sensor_dev_attr_in5_input.dev_attr.attr,
437 &sensor_dev_attr_in5_min.dev_attr.attr,
438 &sensor_dev_attr_in5_max.dev_attr.attr,
439 &sensor_dev_attr_in5_alarm.dev_attr.attr,
440 &sensor_dev_attr_in6_input.dev_attr.attr,
441 &sensor_dev_attr_in6_min.dev_attr.attr,
442 &sensor_dev_attr_in6_max.dev_attr.attr,
443 &sensor_dev_attr_in6_alarm.dev_attr.attr,
444 &sensor_dev_attr_in7_input.dev_attr.attr,
445 &sensor_dev_attr_in7_min.dev_attr.attr,
446 &sensor_dev_attr_in7_max.dev_attr.attr,
447 &sensor_dev_attr_in7_alarm.dev_attr.attr,
448
449 &sensor_dev_attr_temp1_input.dev_attr.attr,
450 &sensor_dev_attr_temp1_max.dev_attr.attr,
451 &sensor_dev_attr_temp1_min.dev_attr.attr,
452 &sensor_dev_attr_temp1_offset.dev_attr.attr,
453 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
454 &sensor_dev_attr_temp2_input.dev_attr.attr,
455 &sensor_dev_attr_temp2_max.dev_attr.attr,
456 &sensor_dev_attr_temp2_min.dev_attr.attr,
457 &sensor_dev_attr_temp2_offset.dev_attr.attr,
458 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400459 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200460 &sensor_dev_attr_temp3_input.dev_attr.attr,
461 &sensor_dev_attr_temp3_max.dev_attr.attr,
462 &sensor_dev_attr_temp3_min.dev_attr.attr,
463 &sensor_dev_attr_temp3_offset.dev_attr.attr,
464 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400465 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200466
467 &dev_attr_cpu0_vid.attr,
468 &dev_attr_vrm.attr,
469 NULL
470};
471
472static const struct attribute_group smsc47m192_group = {
473 .attrs = smsc47m192_attributes,
474};
475
476static struct attribute *smsc47m192_attributes_in4[] = {
477 &sensor_dev_attr_in4_input.dev_attr.attr,
478 &sensor_dev_attr_in4_min.dev_attr.attr,
479 &sensor_dev_attr_in4_max.dev_attr.attr,
480 &sensor_dev_attr_in4_alarm.dev_attr.attr,
481 NULL
482};
483
484static const struct attribute_group smsc47m192_group_in4 = {
485 .attrs = smsc47m192_attributes_in4,
486};
487
Hartmut Rick59ac8362006-03-23 16:37:23 +0100488static void smsc47m192_init_client(struct i2c_client *client)
489{
490 int i;
491 u8 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
492 u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
493
494 /* select cycle mode (pause 1 sec between updates) */
495 i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
496 (sfr & 0xfd) | 0x02);
497 if (!(config & 0x01)) {
498 /* initialize alarm limits */
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800499 for (i = 0; i < 8; i++) {
Hartmut Rick59ac8362006-03-23 16:37:23 +0100500 i2c_smbus_write_byte_data(client,
501 SMSC47M192_REG_IN_MIN(i), 0);
502 i2c_smbus_write_byte_data(client,
503 SMSC47M192_REG_IN_MAX(i), 0xff);
504 }
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800505 for (i = 0; i < 3; i++) {
Hartmut Rick59ac8362006-03-23 16:37:23 +0100506 i2c_smbus_write_byte_data(client,
507 SMSC47M192_REG_TEMP_MIN[i], 0x80);
508 i2c_smbus_write_byte_data(client,
509 SMSC47M192_REG_TEMP_MAX[i], 0x7f);
510 }
511
512 /* start monitoring */
513 i2c_smbus_write_byte_data(client, SMSC47M192_REG_CONFIG,
514 (config & 0xf7) | 0x01);
515 }
516}
517
Jean Delvare8fb597b2008-07-16 19:30:16 +0200518/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100519static int smsc47m192_detect(struct i2c_client *client,
Jean Delvare8fb597b2008-07-16 19:30:16 +0200520 struct i2c_board_info *info)
Hartmut Rick59ac8362006-03-23 16:37:23 +0100521{
Jean Delvare8fb597b2008-07-16 19:30:16 +0200522 struct i2c_adapter *adapter = client->adapter;
523 int version;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100524
525 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare8fb597b2008-07-16 19:30:16 +0200526 return -ENODEV;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100527
528 /* Detection criteria from sensors_detect script */
Jean Delvare52df6442009-12-09 20:35:57 +0100529 version = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VERSION);
530 if (i2c_smbus_read_byte_data(client,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100531 SMSC47M192_REG_COMPANY_ID) == 0x55
Jean Delvare52df6442009-12-09 20:35:57 +0100532 && (version & 0xf0) == 0x20
533 && (i2c_smbus_read_byte_data(client,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100534 SMSC47M192_REG_VID) & 0x70) == 0x00
Jean Delvare52df6442009-12-09 20:35:57 +0100535 && (i2c_smbus_read_byte_data(client,
Hartmut Rick59ac8362006-03-23 16:37:23 +0100536 SMSC47M192_REG_VID4) & 0xfe) == 0x80) {
Jean Delvare52df6442009-12-09 20:35:57 +0100537 dev_info(&adapter->dev,
538 "found SMSC47M192 or compatible, "
539 "version 2, stepping A%d\n", version & 0x0f);
540 } else {
541 dev_dbg(&adapter->dev,
542 "SMSC47M192 detection failed at 0x%02x\n",
543 client->addr);
544 return -ENODEV;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100545 }
546
Jean Delvare8fb597b2008-07-16 19:30:16 +0200547 strlcpy(info->type, "smsc47m192", I2C_NAME_SIZE);
548
549 return 0;
550}
551
552static int smsc47m192_probe(struct i2c_client *client,
553 const struct i2c_device_id *id)
554{
555 struct smsc47m192_data *data;
556 int config;
557 int err;
558
Guenter Roeck26259962012-06-02 12:04:07 -0700559 data = devm_kzalloc(&client->dev, sizeof(struct smsc47m192_data),
560 GFP_KERNEL);
561 if (!data)
562 return -ENOMEM;
Jean Delvare8fb597b2008-07-16 19:30:16 +0200563
564 i2c_set_clientdata(client, data);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100565 data->vrm = vid_which_vrm();
Jean Delvaree4a71672007-06-09 10:11:15 -0400566 mutex_init(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100567
Hartmut Rick59ac8362006-03-23 16:37:23 +0100568 /* Initialize the SMSC47M192 chip */
569 smsc47m192_init_client(client);
570
571 /* Register sysfs hooks */
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800572 err = sysfs_create_group(&client->dev.kobj, &smsc47m192_group);
573 if (err)
Guenter Roeck26259962012-06-02 12:04:07 -0700574 return err;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100575
576 /* Pin 110 is either in4 (+12V) or VID4 */
577 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
578 if (!(config & 0x20)) {
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800579 err = sysfs_create_group(&client->dev.kobj,
580 &smsc47m192_group_in4);
581 if (err)
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200582 goto exit_remove_files;
Hartmut Rick59ac8362006-03-23 16:37:23 +0100583 }
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200584
Tony Jones1beeffe2007-08-20 13:46:20 -0700585 data->hwmon_dev = hwmon_device_register(&client->dev);
586 if (IS_ERR(data->hwmon_dev)) {
587 err = PTR_ERR(data->hwmon_dev);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200588 goto exit_remove_files;
589 }
Hartmut Rick59ac8362006-03-23 16:37:23 +0100590
591 return 0;
592
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200593exit_remove_files:
594 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
595 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100596 return err;
597}
598
Jean Delvare8fb597b2008-07-16 19:30:16 +0200599static int smsc47m192_remove(struct i2c_client *client)
Hartmut Rick59ac8362006-03-23 16:37:23 +0100600{
601 struct smsc47m192_data *data = i2c_get_clientdata(client);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100602
Tony Jones1beeffe2007-08-20 13:46:20 -0700603 hwmon_device_unregister(data->hwmon_dev);
Jean Delvarece8c6ce12006-09-24 21:25:12 +0200604 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group);
605 sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100606
Hartmut Rick59ac8362006-03-23 16:37:23 +0100607 return 0;
608}
609
610static struct smsc47m192_data *smsc47m192_update_device(struct device *dev)
611{
612 struct i2c_client *client = to_i2c_client(dev);
613 struct smsc47m192_data *data = i2c_get_clientdata(client);
614 int i, config;
615
Jean Delvaree4a71672007-06-09 10:11:15 -0400616 mutex_lock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100617
618 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
619 || !data->valid) {
620 u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
621
622 dev_dbg(&client->dev, "Starting smsc47m192 update\n");
623
624 for (i = 0; i <= 7; i++) {
625 data->in[i] = i2c_smbus_read_byte_data(client,
626 SMSC47M192_REG_IN(i));
627 data->in_min[i] = i2c_smbus_read_byte_data(client,
628 SMSC47M192_REG_IN_MIN(i));
629 data->in_max[i] = i2c_smbus_read_byte_data(client,
630 SMSC47M192_REG_IN_MAX(i));
631 }
632 for (i = 0; i < 3; i++) {
633 data->temp[i] = i2c_smbus_read_byte_data(client,
634 SMSC47M192_REG_TEMP[i]);
635 data->temp_max[i] = i2c_smbus_read_byte_data(client,
636 SMSC47M192_REG_TEMP_MAX[i]);
637 data->temp_min[i] = i2c_smbus_read_byte_data(client,
638 SMSC47M192_REG_TEMP_MIN[i]);
639 }
640 for (i = 1; i < 3; i++)
641 data->temp_offset[i] = i2c_smbus_read_byte_data(client,
642 SMSC47M192_REG_TEMP_OFFSET(i));
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800643 /*
644 * first offset is temp_offset[0] if SFR bit 4 is set,
645 * temp_offset[1] otherwise
646 */
Hartmut Rick59ac8362006-03-23 16:37:23 +0100647 if (sfr & 0x10) {
648 data->temp_offset[0] = data->temp_offset[1];
649 data->temp_offset[1] = 0;
650 } else
651 data->temp_offset[0] = 0;
652
653 data->vid = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VID)
654 & 0x0f;
655 config = i2c_smbus_read_byte_data(client,
656 SMSC47M192_REG_CONFIG);
657 if (config & 0x20)
658 data->vid |= (i2c_smbus_read_byte_data(client,
659 SMSC47M192_REG_VID4) & 0x01) << 4;
660 data->alarms = i2c_smbus_read_byte_data(client,
661 SMSC47M192_REG_ALARM1) |
662 (i2c_smbus_read_byte_data(client,
Guenter Roeck7cc3cb62012-01-14 22:31:08 -0800663 SMSC47M192_REG_ALARM2) << 8);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100664
665 data->last_updated = jiffies;
666 data->valid = 1;
667 }
668
Jean Delvaree4a71672007-06-09 10:11:15 -0400669 mutex_unlock(&data->update_lock);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100670
671 return data;
672}
673
Axel Linf0967ee2012-01-20 15:38:18 +0800674module_i2c_driver(smsc47m192_driver);
Hartmut Rick59ac8362006-03-23 16:37:23 +0100675
676MODULE_AUTHOR("Hartmut Rick <linux@rick.claranet.de>");
677MODULE_DESCRIPTION("SMSC47M192 driver");
678MODULE_LICENSE("GPL");