xref: /openbmc/linux/drivers/hwmon/smsc47m192.c (revision 151f4e2b)
1 /*
2  * 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  */
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>
33 #include <linux/sysfs.h>
34 #include <linux/mutex.h>
35 
36 /* Addresses to scan */
37 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
38 
39 /* 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)))
46 static u8 SMSC47M192_REG_TEMP[3] =	{ 0x27, 0x26, 0x52 };
47 static u8 SMSC47M192_REG_TEMP_MAX[3] =	{ 0x39, 0x37, 0x58 };
48 static 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 */
60 static 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 */
71 static const u16 nom_mv[] = { 2500, 2250, 3300, 5000, 12000, 3300, 1500, 1800 };
72 
73 static inline unsigned int IN_FROM_REG(u8 reg, int n)
74 {
75 	return SCALE(reg, nom_mv[n], 192);
76 }
77 
78 static inline u8 IN_TO_REG(unsigned long val, int n)
79 {
80 	val = clamp_val(val, 0, nom_mv[n] * 255 / 192);
81 	return SCALE(val, 192, nom_mv[n]);
82 }
83 
84 /*
85  * TEMP: 0.001 degC units (-128C to +127C)
86  * REG: 1C/bit, two's complement
87  */
88 static inline s8 TEMP_TO_REG(long val)
89 {
90 	return SCALE(clamp_val(val, -128000, 127000), 1, 1000);
91 }
92 
93 static inline int TEMP_FROM_REG(s8 val)
94 {
95 	return val * 1000;
96 }
97 
98 struct smsc47m192_data {
99 	struct i2c_client *client;
100 	const struct attribute_group *groups[3];
101 	struct mutex update_lock;
102 	char valid;		/* !=0 if following fields are valid */
103 	unsigned long last_updated;	/* In jiffies */
104 
105 	u8 in[8];		/* Register value */
106 	u8 in_max[8];		/* Register value */
107 	u8 in_min[8];		/* Register value */
108 	s8 temp[3];		/* Register value */
109 	s8 temp_max[3];		/* Register value */
110 	s8 temp_min[3];		/* Register value */
111 	s8 temp_offset[3];	/* Register value */
112 	u16 alarms;		/* Register encoding, combined */
113 	u8 vid;			/* Register encoding, combined */
114 	u8 vrm;
115 };
116 
117 static struct smsc47m192_data *smsc47m192_update_device(struct device *dev)
118 {
119 	struct smsc47m192_data *data = dev_get_drvdata(dev);
120 	struct i2c_client *client = data->client;
121 	int i, config;
122 
123 	mutex_lock(&data->update_lock);
124 
125 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
126 	 || !data->valid) {
127 		u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
128 
129 		dev_dbg(&client->dev, "Starting smsc47m192 update\n");
130 
131 		for (i = 0; i <= 7; i++) {
132 			data->in[i] = i2c_smbus_read_byte_data(client,
133 						SMSC47M192_REG_IN(i));
134 			data->in_min[i] = i2c_smbus_read_byte_data(client,
135 						SMSC47M192_REG_IN_MIN(i));
136 			data->in_max[i] = i2c_smbus_read_byte_data(client,
137 						SMSC47M192_REG_IN_MAX(i));
138 		}
139 		for (i = 0; i < 3; i++) {
140 			data->temp[i] = i2c_smbus_read_byte_data(client,
141 						SMSC47M192_REG_TEMP[i]);
142 			data->temp_max[i] = i2c_smbus_read_byte_data(client,
143 						SMSC47M192_REG_TEMP_MAX[i]);
144 			data->temp_min[i] = i2c_smbus_read_byte_data(client,
145 						SMSC47M192_REG_TEMP_MIN[i]);
146 		}
147 		for (i = 1; i < 3; i++)
148 			data->temp_offset[i] = i2c_smbus_read_byte_data(client,
149 						SMSC47M192_REG_TEMP_OFFSET(i));
150 		/*
151 		 * first offset is temp_offset[0] if SFR bit 4 is set,
152 		 * temp_offset[1] otherwise
153 		 */
154 		if (sfr & 0x10) {
155 			data->temp_offset[0] = data->temp_offset[1];
156 			data->temp_offset[1] = 0;
157 		} else
158 			data->temp_offset[0] = 0;
159 
160 		data->vid = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VID)
161 			    & 0x0f;
162 		config = i2c_smbus_read_byte_data(client,
163 						  SMSC47M192_REG_CONFIG);
164 		if (config & 0x20)
165 			data->vid |= (i2c_smbus_read_byte_data(client,
166 					SMSC47M192_REG_VID4) & 0x01) << 4;
167 		data->alarms = i2c_smbus_read_byte_data(client,
168 						SMSC47M192_REG_ALARM1) |
169 			       (i2c_smbus_read_byte_data(client,
170 						SMSC47M192_REG_ALARM2) << 8);
171 
172 		data->last_updated = jiffies;
173 		data->valid = 1;
174 	}
175 
176 	mutex_unlock(&data->update_lock);
177 
178 	return data;
179 }
180 
181 /* Voltages */
182 static ssize_t in_show(struct device *dev, struct device_attribute *attr,
183 		       char *buf)
184 {
185 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
186 	int nr = sensor_attr->index;
187 	struct smsc47m192_data *data = smsc47m192_update_device(dev);
188 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr));
189 }
190 
191 static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
192 			   char *buf)
193 {
194 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
195 	int nr = sensor_attr->index;
196 	struct smsc47m192_data *data = smsc47m192_update_device(dev);
197 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr));
198 }
199 
200 static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
201 			   char *buf)
202 {
203 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
204 	int nr = sensor_attr->index;
205 	struct smsc47m192_data *data = smsc47m192_update_device(dev);
206 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr));
207 }
208 
209 static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
210 			    const char *buf, size_t count)
211 {
212 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
213 	int nr = sensor_attr->index;
214 	struct smsc47m192_data *data = dev_get_drvdata(dev);
215 	struct i2c_client *client = data->client;
216 	unsigned long val;
217 	int err;
218 
219 	err = kstrtoul(buf, 10, &val);
220 	if (err)
221 		return err;
222 
223 	mutex_lock(&data->update_lock);
224 	data->in_min[nr] = IN_TO_REG(val, nr);
225 	i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MIN(nr),
226 							data->in_min[nr]);
227 	mutex_unlock(&data->update_lock);
228 	return count;
229 }
230 
231 static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
232 			    const char *buf, size_t count)
233 {
234 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
235 	int nr = sensor_attr->index;
236 	struct smsc47m192_data *data = dev_get_drvdata(dev);
237 	struct i2c_client *client = data->client;
238 	unsigned long val;
239 	int err;
240 
241 	err = kstrtoul(buf, 10, &val);
242 	if (err)
243 		return err;
244 
245 	mutex_lock(&data->update_lock);
246 	data->in_max[nr] = IN_TO_REG(val, nr);
247 	i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MAX(nr),
248 							data->in_max[nr]);
249 	mutex_unlock(&data->update_lock);
250 	return count;
251 }
252 
253 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
254 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
255 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
256 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
257 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
258 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
259 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
260 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
261 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
262 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
263 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
264 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
265 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
266 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
267 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
268 static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
269 static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
270 static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
271 static SENSOR_DEVICE_ATTR_RO(in6_input, in, 6);
272 static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
273 static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
274 static SENSOR_DEVICE_ATTR_RO(in7_input, in, 7);
275 static SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 7);
276 static SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 7);
277 
278 /* Temperatures */
279 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
280 			 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[nr]));
286 }
287 
288 static ssize_t temp_min_show(struct device *dev,
289 			     struct device_attribute *attr, char *buf)
290 {
291 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
292 	int nr = sensor_attr->index;
293 	struct smsc47m192_data *data = smsc47m192_update_device(dev);
294 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
295 }
296 
297 static ssize_t temp_max_show(struct device *dev,
298 			     struct device_attribute *attr, char *buf)
299 {
300 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
301 	int nr = sensor_attr->index;
302 	struct smsc47m192_data *data = smsc47m192_update_device(dev);
303 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
304 }
305 
306 static ssize_t temp_min_store(struct device *dev,
307 			      struct device_attribute *attr, const char *buf,
308 			      size_t count)
309 {
310 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
311 	int nr = sensor_attr->index;
312 	struct smsc47m192_data *data = dev_get_drvdata(dev);
313 	struct i2c_client *client = data->client;
314 	long val;
315 	int err;
316 
317 	err = kstrtol(buf, 10, &val);
318 	if (err)
319 		return err;
320 
321 	mutex_lock(&data->update_lock);
322 	data->temp_min[nr] = TEMP_TO_REG(val);
323 	i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MIN[nr],
324 						data->temp_min[nr]);
325 	mutex_unlock(&data->update_lock);
326 	return count;
327 }
328 
329 static ssize_t temp_max_store(struct device *dev,
330 			      struct device_attribute *attr, const char *buf,
331 			      size_t count)
332 {
333 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
334 	int nr = sensor_attr->index;
335 	struct smsc47m192_data *data = dev_get_drvdata(dev);
336 	struct i2c_client *client = data->client;
337 	long val;
338 	int err;
339 
340 	err = kstrtol(buf, 10, &val);
341 	if (err)
342 		return err;
343 
344 	mutex_lock(&data->update_lock);
345 	data->temp_max[nr] = TEMP_TO_REG(val);
346 	i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MAX[nr],
347 						data->temp_max[nr]);
348 	mutex_unlock(&data->update_lock);
349 	return count;
350 }
351 
352 static ssize_t temp_offset_show(struct device *dev,
353 				struct device_attribute *attr, char *buf)
354 {
355 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
356 	int nr = sensor_attr->index;
357 	struct smsc47m192_data *data = smsc47m192_update_device(dev);
358 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
359 }
360 
361 static ssize_t temp_offset_store(struct device *dev,
362 				 struct device_attribute *attr,
363 				 const char *buf, size_t count)
364 {
365 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
366 	int nr = sensor_attr->index;
367 	struct smsc47m192_data *data = dev_get_drvdata(dev);
368 	struct i2c_client *client = data->client;
369 	u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
370 	long val;
371 	int err;
372 
373 	err = kstrtol(buf, 10, &val);
374 	if (err)
375 		return err;
376 
377 	mutex_lock(&data->update_lock);
378 	data->temp_offset[nr] = TEMP_TO_REG(val);
379 	if (nr > 1)
380 		i2c_smbus_write_byte_data(client,
381 			SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
382 	else if (data->temp_offset[nr] != 0) {
383 		/*
384 		 * offset[0] and offset[1] share the same register,
385 		 * SFR bit 4 activates offset[0]
386 		 */
387 		i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
388 					(sfr & 0xef) | (nr == 0 ? 0x10 : 0));
389 		data->temp_offset[1-nr] = 0;
390 		i2c_smbus_write_byte_data(client,
391 			SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
392 	} else if ((sfr & 0x10) == (nr == 0 ? 0x10 : 0))
393 		i2c_smbus_write_byte_data(client,
394 					SMSC47M192_REG_TEMP_OFFSET(nr), 0);
395 	mutex_unlock(&data->update_lock);
396 	return count;
397 }
398 
399 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
400 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
401 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
402 static SENSOR_DEVICE_ATTR_RW(temp1_offset, temp_offset, 0);
403 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
404 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
405 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
406 static SENSOR_DEVICE_ATTR_RW(temp2_offset, temp_offset, 1);
407 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
408 static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
409 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
410 static SENSOR_DEVICE_ATTR_RW(temp3_offset, temp_offset, 2);
411 
412 /* VID */
413 static ssize_t cpu0_vid_show(struct device *dev,
414 			     struct device_attribute *attr, char *buf)
415 {
416 	struct smsc47m192_data *data = smsc47m192_update_device(dev);
417 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
418 }
419 static DEVICE_ATTR_RO(cpu0_vid);
420 
421 static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
422 		char *buf)
423 {
424 	struct smsc47m192_data *data = dev_get_drvdata(dev);
425 	return sprintf(buf, "%d\n", data->vrm);
426 }
427 
428 static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
429 			 const char *buf, size_t count)
430 {
431 	struct smsc47m192_data *data = dev_get_drvdata(dev);
432 	unsigned long val;
433 	int err;
434 
435 	err = kstrtoul(buf, 10, &val);
436 	if (err)
437 		return err;
438 	if (val > 255)
439 		return -EINVAL;
440 
441 	data->vrm = val;
442 	return count;
443 }
444 static DEVICE_ATTR_RW(vrm);
445 
446 /* Alarms */
447 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
448 			  char *buf)
449 {
450 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
451 	int nr = sensor_attr->index;
452 	struct smsc47m192_data *data = smsc47m192_update_device(dev);
453 	return sprintf(buf, "%u\n", (data->alarms & nr) ? 1 : 0);
454 }
455 
456 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 0x0010);
457 static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 0x0020);
458 static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 0x0040);
459 static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 0x4000);
460 static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 0x8000);
461 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0x0001);
462 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 0x0002);
463 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 0x0004);
464 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 0x0008);
465 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 0x0100);
466 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 0x0200);
467 static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 0x0400);
468 static SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm, 0x0800);
469 
470 static struct attribute *smsc47m192_attributes[] = {
471 	&sensor_dev_attr_in0_input.dev_attr.attr,
472 	&sensor_dev_attr_in0_min.dev_attr.attr,
473 	&sensor_dev_attr_in0_max.dev_attr.attr,
474 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
475 	&sensor_dev_attr_in1_input.dev_attr.attr,
476 	&sensor_dev_attr_in1_min.dev_attr.attr,
477 	&sensor_dev_attr_in1_max.dev_attr.attr,
478 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
479 	&sensor_dev_attr_in2_input.dev_attr.attr,
480 	&sensor_dev_attr_in2_min.dev_attr.attr,
481 	&sensor_dev_attr_in2_max.dev_attr.attr,
482 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
483 	&sensor_dev_attr_in3_input.dev_attr.attr,
484 	&sensor_dev_attr_in3_min.dev_attr.attr,
485 	&sensor_dev_attr_in3_max.dev_attr.attr,
486 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
487 	&sensor_dev_attr_in5_input.dev_attr.attr,
488 	&sensor_dev_attr_in5_min.dev_attr.attr,
489 	&sensor_dev_attr_in5_max.dev_attr.attr,
490 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
491 	&sensor_dev_attr_in6_input.dev_attr.attr,
492 	&sensor_dev_attr_in6_min.dev_attr.attr,
493 	&sensor_dev_attr_in6_max.dev_attr.attr,
494 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
495 	&sensor_dev_attr_in7_input.dev_attr.attr,
496 	&sensor_dev_attr_in7_min.dev_attr.attr,
497 	&sensor_dev_attr_in7_max.dev_attr.attr,
498 	&sensor_dev_attr_in7_alarm.dev_attr.attr,
499 
500 	&sensor_dev_attr_temp1_input.dev_attr.attr,
501 	&sensor_dev_attr_temp1_max.dev_attr.attr,
502 	&sensor_dev_attr_temp1_min.dev_attr.attr,
503 	&sensor_dev_attr_temp1_offset.dev_attr.attr,
504 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
505 	&sensor_dev_attr_temp2_input.dev_attr.attr,
506 	&sensor_dev_attr_temp2_max.dev_attr.attr,
507 	&sensor_dev_attr_temp2_min.dev_attr.attr,
508 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
509 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
510 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
511 	&sensor_dev_attr_temp3_input.dev_attr.attr,
512 	&sensor_dev_attr_temp3_max.dev_attr.attr,
513 	&sensor_dev_attr_temp3_min.dev_attr.attr,
514 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
515 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
516 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
517 
518 	&dev_attr_cpu0_vid.attr,
519 	&dev_attr_vrm.attr,
520 	NULL
521 };
522 
523 static const struct attribute_group smsc47m192_group = {
524 	.attrs = smsc47m192_attributes,
525 };
526 
527 static struct attribute *smsc47m192_attributes_in4[] = {
528 	&sensor_dev_attr_in4_input.dev_attr.attr,
529 	&sensor_dev_attr_in4_min.dev_attr.attr,
530 	&sensor_dev_attr_in4_max.dev_attr.attr,
531 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
532 	NULL
533 };
534 
535 static const struct attribute_group smsc47m192_group_in4 = {
536 	.attrs = smsc47m192_attributes_in4,
537 };
538 
539 static void smsc47m192_init_client(struct i2c_client *client)
540 {
541 	int i;
542 	u8 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
543 	u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
544 
545 	/* select cycle mode (pause 1 sec between updates) */
546 	i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
547 						(sfr & 0xfd) | 0x02);
548 	if (!(config & 0x01)) {
549 		/* initialize alarm limits */
550 		for (i = 0; i < 8; i++) {
551 			i2c_smbus_write_byte_data(client,
552 				SMSC47M192_REG_IN_MIN(i), 0);
553 			i2c_smbus_write_byte_data(client,
554 				SMSC47M192_REG_IN_MAX(i), 0xff);
555 		}
556 		for (i = 0; i < 3; i++) {
557 			i2c_smbus_write_byte_data(client,
558 				SMSC47M192_REG_TEMP_MIN[i], 0x80);
559 			i2c_smbus_write_byte_data(client,
560 				SMSC47M192_REG_TEMP_MAX[i], 0x7f);
561 		}
562 
563 		/* start monitoring */
564 		i2c_smbus_write_byte_data(client, SMSC47M192_REG_CONFIG,
565 						(config & 0xf7) | 0x01);
566 	}
567 }
568 
569 /* Return 0 if detection is successful, -ENODEV otherwise */
570 static int smsc47m192_detect(struct i2c_client *client,
571 			     struct i2c_board_info *info)
572 {
573 	struct i2c_adapter *adapter = client->adapter;
574 	int version;
575 
576 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
577 		return -ENODEV;
578 
579 	/* Detection criteria from sensors_detect script */
580 	version = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VERSION);
581 	if (i2c_smbus_read_byte_data(client,
582 				SMSC47M192_REG_COMPANY_ID) == 0x55
583 	 && (version & 0xf0) == 0x20
584 	 && (i2c_smbus_read_byte_data(client,
585 				SMSC47M192_REG_VID) & 0x70) == 0x00
586 	 && (i2c_smbus_read_byte_data(client,
587 				SMSC47M192_REG_VID4) & 0xfe) == 0x80) {
588 		dev_info(&adapter->dev,
589 			 "found SMSC47M192 or compatible, "
590 			 "version 2, stepping A%d\n", version & 0x0f);
591 	} else {
592 		dev_dbg(&adapter->dev,
593 			"SMSC47M192 detection failed at 0x%02x\n",
594 			client->addr);
595 		return -ENODEV;
596 	}
597 
598 	strlcpy(info->type, "smsc47m192", I2C_NAME_SIZE);
599 
600 	return 0;
601 }
602 
603 static int smsc47m192_probe(struct i2c_client *client,
604 			    const struct i2c_device_id *id)
605 {
606 	struct device *dev = &client->dev;
607 	struct device *hwmon_dev;
608 	struct smsc47m192_data *data;
609 	int config;
610 
611 	data = devm_kzalloc(dev, sizeof(struct smsc47m192_data), GFP_KERNEL);
612 	if (!data)
613 		return -ENOMEM;
614 
615 	data->client = client;
616 	data->vrm = vid_which_vrm();
617 	mutex_init(&data->update_lock);
618 
619 	/* Initialize the SMSC47M192 chip */
620 	smsc47m192_init_client(client);
621 
622 	/* sysfs hooks */
623 	data->groups[0] = &smsc47m192_group;
624 	/* Pin 110 is either in4 (+12V) or VID4 */
625 	config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
626 	if (!(config & 0x20))
627 		data->groups[1] = &smsc47m192_group_in4;
628 
629 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
630 							   data, data->groups);
631 	return PTR_ERR_OR_ZERO(hwmon_dev);
632 }
633 
634 static const struct i2c_device_id smsc47m192_id[] = {
635 	{ "smsc47m192", 0 },
636 	{ }
637 };
638 MODULE_DEVICE_TABLE(i2c, smsc47m192_id);
639 
640 static struct i2c_driver smsc47m192_driver = {
641 	.class		= I2C_CLASS_HWMON,
642 	.driver = {
643 		.name	= "smsc47m192",
644 	},
645 	.probe		= smsc47m192_probe,
646 	.id_table	= smsc47m192_id,
647 	.detect		= smsc47m192_detect,
648 	.address_list	= normal_i2c,
649 };
650 
651 module_i2c_driver(smsc47m192_driver);
652 
653 MODULE_AUTHOR("Hartmut Rick <linux@rick.claranet.de>");
654 MODULE_DESCRIPTION("SMSC47M192 driver");
655 MODULE_LICENSE("GPL");
656