xref: /linux/drivers/hwmon/lm87.c (revision 60e13231561b3a4c5269bfa1ef6c0569ad6f28ec)
1 /*
2  * lm87.c
3  *
4  * Copyright (C) 2000       Frodo Looijaard <frodol@dds.nl>
5  *                          Philip Edelbrock <phil@netroedge.com>
6  *                          Stephen Rousset <stephen.rousset@rocketlogix.com>
7  *                          Dan Eaton <dan.eaton@rocketlogix.com>
8  * Copyright (C) 2004-2008  Jean Delvare <khali@linux-fr.org>
9  *
10  * Original port to Linux 2.6 by Jeff Oliver.
11  *
12  * The LM87 is a sensor chip made by National Semiconductor. It monitors up
13  * to 8 voltages (including its own power source), up to three temperatures
14  * (its own plus up to two external ones) and up to two fans. The default
15  * configuration is 6 voltages, two temperatures and two fans (see below).
16  * Voltages are scaled internally with ratios such that the nominal value of
17  * each voltage correspond to a register value of 192 (which means a
18  * resolution of about 0.5% of the nominal value). Temperature values are
19  * reported with a 1 deg resolution and a 3-4 deg accuracy. Complete
20  * datasheet can be obtained from National's website at:
21  *   http://www.national.com/pf/LM/LM87.html
22  *
23  * Some functions share pins, so not all functions are available at the same
24  * time. Which are depends on the hardware setup. This driver normally
25  * assumes that firmware configured the chip correctly. Where this is not
26  * the case, platform code must set the I2C client's platform_data to point
27  * to a u8 value to be written to the channel register.
28  * For reference, here is the list of exclusive functions:
29  *  - in0+in5 (default) or temp3
30  *  - fan1 (default) or in6
31  *  - fan2 (default) or in7
32  *  - VID lines (default) or IRQ lines (not handled by this driver)
33  *
34  * The LM87 additionally features an analog output, supposedly usable to
35  * control the speed of a fan. All new chips use pulse width modulation
36  * instead. The LM87 is the only hardware monitoring chipset I know of
37  * which uses amplitude modulation. Be careful when using this feature.
38  *
39  * This driver also supports the ADM1024, a sensor chip made by Analog
40  * Devices. That chip is fully compatible with the LM87. Complete
41  * datasheet can be obtained from Analog's website at:
42  *   http://www.analog.com/en/prod/0,2877,ADM1024,00.html
43  *
44  * This program is free software; you can redistribute it and/or modify
45  * it under the terms of the GNU General Public License as published by
46  * the Free Software Foundation; either version 2 of the License, or
47  * (at your option) any later version.
48  *
49  * This program is distributed in the hope that it will be useful,
50  * but WITHOUT ANY WARRANTY; without even the implied warranty of
51  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
52  * GNU General Public License for more details.
53  *
54  * You should have received a copy of the GNU General Public License
55  * along with this program; if not, write to the Free Software
56  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
57  */
58 
59 #include <linux/module.h>
60 #include <linux/init.h>
61 #include <linux/slab.h>
62 #include <linux/jiffies.h>
63 #include <linux/i2c.h>
64 #include <linux/hwmon.h>
65 #include <linux/hwmon-sysfs.h>
66 #include <linux/hwmon-vid.h>
67 #include <linux/err.h>
68 #include <linux/mutex.h>
69 
70 /*
71  * Addresses to scan
72  * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
73  */
74 
75 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
76 
77 enum chips { lm87, adm1024 };
78 
79 /*
80  * The LM87 registers
81  */
82 
83 /* nr in 0..5 */
84 #define LM87_REG_IN(nr)			(0x20 + (nr))
85 #define LM87_REG_IN_MAX(nr)		(0x2B + (nr) * 2)
86 #define LM87_REG_IN_MIN(nr)		(0x2C + (nr) * 2)
87 /* nr in 0..1 */
88 #define LM87_REG_AIN(nr)		(0x28 + (nr))
89 #define LM87_REG_AIN_MIN(nr)		(0x1A + (nr))
90 #define LM87_REG_AIN_MAX(nr)		(0x3B + (nr))
91 
92 static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
93 static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
94 static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
95 
96 #define LM87_REG_TEMP_HW_INT_LOCK	0x13
97 #define LM87_REG_TEMP_HW_EXT_LOCK	0x14
98 #define LM87_REG_TEMP_HW_INT		0x17
99 #define LM87_REG_TEMP_HW_EXT		0x18
100 
101 /* nr in 0..1 */
102 #define LM87_REG_FAN(nr)		(0x28 + (nr))
103 #define LM87_REG_FAN_MIN(nr)		(0x3B + (nr))
104 #define LM87_REG_AOUT			0x19
105 
106 #define LM87_REG_CONFIG			0x40
107 #define LM87_REG_CHANNEL_MODE		0x16
108 #define LM87_REG_VID_FAN_DIV		0x47
109 #define LM87_REG_VID4			0x49
110 
111 #define LM87_REG_ALARMS1		0x41
112 #define LM87_REG_ALARMS2		0x42
113 
114 #define LM87_REG_COMPANY_ID		0x3E
115 #define LM87_REG_REVISION		0x3F
116 
117 /*
118  * Conversions and various macros
119  * The LM87 uses signed 8-bit values for temperatures.
120  */
121 
122 #define IN_FROM_REG(reg,scale)	(((reg) * (scale) + 96) / 192)
123 #define IN_TO_REG(val,scale)	((val) <= 0 ? 0 : \
124 				 (val) * 192 >= (scale) * 255 ? 255 : \
125 				 ((val) * 192 + (scale)/2) / (scale))
126 
127 #define TEMP_FROM_REG(reg)	((reg) * 1000)
128 #define TEMP_TO_REG(val)	((val) <= -127500 ? -128 : \
129 				 (val) >= 126500 ? 127 : \
130 				 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
131 
132 #define FAN_FROM_REG(reg,div)	((reg) == 255 || (reg) == 0 ? 0 : \
133 				 (1350000 + (reg)*(div) / 2) / ((reg)*(div)))
134 #define FAN_TO_REG(val,div)	((val)*(div) * 255 <= 1350000 ? 255 : \
135 				 (1350000 + (val)*(div) / 2) / ((val)*(div)))
136 
137 #define FAN_DIV_FROM_REG(reg)	(1 << (reg))
138 
139 /* analog out is 9.80mV/LSB */
140 #define AOUT_FROM_REG(reg)	(((reg) * 98 + 5) / 10)
141 #define AOUT_TO_REG(val)	((val) <= 0 ? 0 : \
142 				 (val) >= 2500 ? 255 : \
143 				 ((val) * 10 + 49) / 98)
144 
145 /* nr in 0..1 */
146 #define CHAN_NO_FAN(nr)		(1 << (nr))
147 #define CHAN_TEMP3		(1 << 2)
148 #define CHAN_VCC_5V		(1 << 3)
149 #define CHAN_NO_VID		(1 << 7)
150 
151 /*
152  * Functions declaration
153  */
154 
155 static int lm87_probe(struct i2c_client *client,
156 		      const struct i2c_device_id *id);
157 static int lm87_detect(struct i2c_client *new_client,
158 		       struct i2c_board_info *info);
159 static void lm87_init_client(struct i2c_client *client);
160 static int lm87_remove(struct i2c_client *client);
161 static struct lm87_data *lm87_update_device(struct device *dev);
162 
163 /*
164  * Driver data (common to all clients)
165  */
166 
167 static const struct i2c_device_id lm87_id[] = {
168 	{ "lm87", lm87 },
169 	{ "adm1024", adm1024 },
170 	{ }
171 };
172 MODULE_DEVICE_TABLE(i2c, lm87_id);
173 
174 static struct i2c_driver lm87_driver = {
175 	.class		= I2C_CLASS_HWMON,
176 	.driver = {
177 		.name	= "lm87",
178 	},
179 	.probe		= lm87_probe,
180 	.remove		= lm87_remove,
181 	.id_table	= lm87_id,
182 	.detect		= lm87_detect,
183 	.address_list	= normal_i2c,
184 };
185 
186 /*
187  * Client data (each client gets its own)
188  */
189 
190 struct lm87_data {
191 	struct device *hwmon_dev;
192 	struct mutex update_lock;
193 	char valid; /* zero until following fields are valid */
194 	unsigned long last_updated; /* In jiffies */
195 
196 	u8 channel;		/* register value */
197 	u8 config;		/* original register value */
198 
199 	u8 in[8];		/* register value */
200 	u8 in_max[8];		/* register value */
201 	u8 in_min[8];		/* register value */
202 	u16 in_scale[8];
203 
204 	s8 temp[3];		/* register value */
205 	s8 temp_high[3];	/* register value */
206 	s8 temp_low[3];		/* register value */
207 	s8 temp_crit_int;	/* min of two register values */
208 	s8 temp_crit_ext;	/* min of two register values */
209 
210 	u8 fan[2];		/* register value */
211 	u8 fan_min[2];		/* register value */
212 	u8 fan_div[2];		/* register value, shifted right */
213 	u8 aout;		/* register value */
214 
215 	u16 alarms;		/* register values, combined */
216 	u8 vid;			/* register values, combined */
217 	u8 vrm;
218 };
219 
220 /*
221  * Sysfs stuff
222  */
223 
224 static inline int lm87_read_value(struct i2c_client *client, u8 reg)
225 {
226 	return i2c_smbus_read_byte_data(client, reg);
227 }
228 
229 static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
230 {
231 	return i2c_smbus_write_byte_data(client, reg, value);
232 }
233 
234 #define show_in(offset) \
235 static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
236 { \
237 	struct lm87_data *data = lm87_update_device(dev); \
238 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
239 		       data->in_scale[offset])); \
240 } \
241 static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
242 { \
243 	struct lm87_data *data = lm87_update_device(dev); \
244 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
245 		       data->in_scale[offset])); \
246 } \
247 static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
248 { \
249 	struct lm87_data *data = lm87_update_device(dev); \
250 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
251 		       data->in_scale[offset])); \
252 } \
253 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
254 		show_in##offset##_input, NULL);
255 show_in(0);
256 show_in(1);
257 show_in(2);
258 show_in(3);
259 show_in(4);
260 show_in(5);
261 show_in(6);
262 show_in(7);
263 
264 static void set_in_min(struct device *dev, const char *buf, int nr)
265 {
266 	struct i2c_client *client = to_i2c_client(dev);
267 	struct lm87_data *data = i2c_get_clientdata(client);
268 	long val = simple_strtol(buf, NULL, 10);
269 
270 	mutex_lock(&data->update_lock);
271 	data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
272 	lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
273 			 LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
274 	mutex_unlock(&data->update_lock);
275 }
276 
277 static void set_in_max(struct device *dev, const char *buf, int nr)
278 {
279 	struct i2c_client *client = to_i2c_client(dev);
280 	struct lm87_data *data = i2c_get_clientdata(client);
281 	long val = simple_strtol(buf, NULL, 10);
282 
283 	mutex_lock(&data->update_lock);
284 	data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
285 	lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
286 			 LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
287 	mutex_unlock(&data->update_lock);
288 }
289 
290 #define set_in(offset) \
291 static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, \
292 		const char *buf, size_t count) \
293 { \
294 	set_in_min(dev, buf, offset); \
295 	return count; \
296 } \
297 static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, \
298 		const char *buf, size_t count) \
299 { \
300 	set_in_max(dev, buf, offset); \
301 	return count; \
302 } \
303 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
304 		show_in##offset##_min, set_in##offset##_min); \
305 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
306 		show_in##offset##_max, set_in##offset##_max);
307 set_in(0);
308 set_in(1);
309 set_in(2);
310 set_in(3);
311 set_in(4);
312 set_in(5);
313 set_in(6);
314 set_in(7);
315 
316 #define show_temp(offset) \
317 static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
318 { \
319 	struct lm87_data *data = lm87_update_device(dev); \
320 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
321 } \
322 static ssize_t show_temp##offset##_low(struct device *dev, struct device_attribute *attr, char *buf) \
323 { \
324 	struct lm87_data *data = lm87_update_device(dev); \
325 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
326 } \
327 static ssize_t show_temp##offset##_high(struct device *dev, struct device_attribute *attr, char *buf) \
328 { \
329 	struct lm87_data *data = lm87_update_device(dev); \
330 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
331 }\
332 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
333 		show_temp##offset##_input, NULL);
334 show_temp(1);
335 show_temp(2);
336 show_temp(3);
337 
338 static void set_temp_low(struct device *dev, const char *buf, int nr)
339 {
340 	struct i2c_client *client = to_i2c_client(dev);
341 	struct lm87_data *data = i2c_get_clientdata(client);
342 	long val = simple_strtol(buf, NULL, 10);
343 
344 	mutex_lock(&data->update_lock);
345 	data->temp_low[nr] = TEMP_TO_REG(val);
346 	lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
347 	mutex_unlock(&data->update_lock);
348 }
349 
350 static void set_temp_high(struct device *dev, const char *buf, int nr)
351 {
352 	struct i2c_client *client = to_i2c_client(dev);
353 	struct lm87_data *data = i2c_get_clientdata(client);
354 	long val = simple_strtol(buf, NULL, 10);
355 
356 	mutex_lock(&data->update_lock);
357 	data->temp_high[nr] = TEMP_TO_REG(val);
358 	lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
359 	mutex_unlock(&data->update_lock);
360 }
361 
362 #define set_temp(offset) \
363 static ssize_t set_temp##offset##_low(struct device *dev, struct device_attribute *attr, \
364 		const char *buf, size_t count) \
365 { \
366 	set_temp_low(dev, buf, offset-1); \
367 	return count; \
368 } \
369 static ssize_t set_temp##offset##_high(struct device *dev, struct device_attribute *attr, \
370 		const char *buf, size_t count) \
371 { \
372 	set_temp_high(dev, buf, offset-1); \
373 	return count; \
374 } \
375 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
376 		show_temp##offset##_high, set_temp##offset##_high); \
377 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
378 		show_temp##offset##_low, set_temp##offset##_low);
379 set_temp(1);
380 set_temp(2);
381 set_temp(3);
382 
383 static ssize_t show_temp_crit_int(struct device *dev, struct device_attribute *attr, char *buf)
384 {
385 	struct lm87_data *data = lm87_update_device(dev);
386 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
387 }
388 
389 static ssize_t show_temp_crit_ext(struct device *dev, struct device_attribute *attr, char *buf)
390 {
391 	struct lm87_data *data = lm87_update_device(dev);
392 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
393 }
394 
395 static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
396 static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
397 static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
398 
399 #define show_fan(offset) \
400 static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
401 { \
402 	struct lm87_data *data = lm87_update_device(dev); \
403 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \
404 		       FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
405 } \
406 static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
407 { \
408 	struct lm87_data *data = lm87_update_device(dev); \
409 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \
410 		       FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
411 } \
412 static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
413 { \
414 	struct lm87_data *data = lm87_update_device(dev); \
415 	return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
416 } \
417 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
418 		show_fan##offset##_input, NULL);
419 show_fan(1);
420 show_fan(2);
421 
422 static void set_fan_min(struct device *dev, const char *buf, int nr)
423 {
424 	struct i2c_client *client = to_i2c_client(dev);
425 	struct lm87_data *data = i2c_get_clientdata(client);
426 	long val = simple_strtol(buf, NULL, 10);
427 
428 	mutex_lock(&data->update_lock);
429 	data->fan_min[nr] = FAN_TO_REG(val,
430 			    FAN_DIV_FROM_REG(data->fan_div[nr]));
431 	lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
432 	mutex_unlock(&data->update_lock);
433 }
434 
435 /* Note: we save and restore the fan minimum here, because its value is
436    determined in part by the fan clock divider.  This follows the principle
437    of least surprise; the user doesn't expect the fan minimum to change just
438    because the divider changed. */
439 static ssize_t set_fan_div(struct device *dev, const char *buf,
440 		size_t count, int nr)
441 {
442 	struct i2c_client *client = to_i2c_client(dev);
443 	struct lm87_data *data = i2c_get_clientdata(client);
444 	long val = simple_strtol(buf, NULL, 10);
445 	unsigned long min;
446 	u8 reg;
447 
448 	mutex_lock(&data->update_lock);
449 	min = FAN_FROM_REG(data->fan_min[nr],
450 			   FAN_DIV_FROM_REG(data->fan_div[nr]));
451 
452 	switch (val) {
453 	case 1: data->fan_div[nr] = 0; break;
454 	case 2: data->fan_div[nr] = 1; break;
455 	case 4: data->fan_div[nr] = 2; break;
456 	case 8: data->fan_div[nr] = 3; break;
457 	default:
458 		mutex_unlock(&data->update_lock);
459 		return -EINVAL;
460 	}
461 
462 	reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
463 	switch (nr) {
464 	case 0:
465 	    reg = (reg & 0xCF) | (data->fan_div[0] << 4);
466 	    break;
467 	case 1:
468 	    reg = (reg & 0x3F) | (data->fan_div[1] << 6);
469 	    break;
470 	}
471 	lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
472 
473 	data->fan_min[nr] = FAN_TO_REG(min, val);
474 	lm87_write_value(client, LM87_REG_FAN_MIN(nr),
475 			 data->fan_min[nr]);
476 	mutex_unlock(&data->update_lock);
477 
478 	return count;
479 }
480 
481 #define set_fan(offset) \
482 static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
483 		size_t count) \
484 { \
485 	set_fan_min(dev, buf, offset-1); \
486 	return count; \
487 } \
488 static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
489 		size_t count) \
490 { \
491 	return set_fan_div(dev, buf, count, offset-1); \
492 } \
493 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
494 		show_fan##offset##_min, set_fan##offset##_min); \
495 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
496 		show_fan##offset##_div, set_fan##offset##_div);
497 set_fan(1);
498 set_fan(2);
499 
500 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
501 {
502 	struct lm87_data *data = lm87_update_device(dev);
503 	return sprintf(buf, "%d\n", data->alarms);
504 }
505 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
506 
507 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
508 {
509 	struct lm87_data *data = lm87_update_device(dev);
510 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
511 }
512 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
513 
514 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
515 {
516 	struct lm87_data *data = dev_get_drvdata(dev);
517 	return sprintf(buf, "%d\n", data->vrm);
518 }
519 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
520 {
521 	struct lm87_data *data = dev_get_drvdata(dev);
522 	data->vrm = simple_strtoul(buf, NULL, 10);
523 	return count;
524 }
525 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
526 
527 static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
528 {
529 	struct lm87_data *data = lm87_update_device(dev);
530 	return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
531 }
532 static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
533 {
534 	struct i2c_client *client = to_i2c_client(dev);
535 	struct lm87_data *data = i2c_get_clientdata(client);
536 	long val = simple_strtol(buf, NULL, 10);
537 
538 	mutex_lock(&data->update_lock);
539 	data->aout = AOUT_TO_REG(val);
540 	lm87_write_value(client, LM87_REG_AOUT, data->aout);
541 	mutex_unlock(&data->update_lock);
542 	return count;
543 }
544 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
545 
546 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
547 			  char *buf)
548 {
549 	struct lm87_data *data = lm87_update_device(dev);
550 	int bitnr = to_sensor_dev_attr(attr)->index;
551 	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
552 }
553 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
554 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
555 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
556 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
557 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
558 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
559 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
560 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
561 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
562 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
563 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
564 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
565 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
566 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
567 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
568 
569 /*
570  * Real code
571  */
572 
573 static struct attribute *lm87_attributes[] = {
574 	&dev_attr_in1_input.attr,
575 	&dev_attr_in1_min.attr,
576 	&dev_attr_in1_max.attr,
577 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
578 	&dev_attr_in2_input.attr,
579 	&dev_attr_in2_min.attr,
580 	&dev_attr_in2_max.attr,
581 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
582 	&dev_attr_in3_input.attr,
583 	&dev_attr_in3_min.attr,
584 	&dev_attr_in3_max.attr,
585 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
586 	&dev_attr_in4_input.attr,
587 	&dev_attr_in4_min.attr,
588 	&dev_attr_in4_max.attr,
589 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
590 
591 	&dev_attr_temp1_input.attr,
592 	&dev_attr_temp1_max.attr,
593 	&dev_attr_temp1_min.attr,
594 	&dev_attr_temp1_crit.attr,
595 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
596 	&dev_attr_temp2_input.attr,
597 	&dev_attr_temp2_max.attr,
598 	&dev_attr_temp2_min.attr,
599 	&dev_attr_temp2_crit.attr,
600 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
601 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
602 
603 	&dev_attr_alarms.attr,
604 	&dev_attr_aout_output.attr,
605 
606 	NULL
607 };
608 
609 static const struct attribute_group lm87_group = {
610 	.attrs = lm87_attributes,
611 };
612 
613 static struct attribute *lm87_attributes_opt[] = {
614 	&dev_attr_in6_input.attr,
615 	&dev_attr_in6_min.attr,
616 	&dev_attr_in6_max.attr,
617 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
618 
619 	&dev_attr_fan1_input.attr,
620 	&dev_attr_fan1_min.attr,
621 	&dev_attr_fan1_div.attr,
622 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
623 
624 	&dev_attr_in7_input.attr,
625 	&dev_attr_in7_min.attr,
626 	&dev_attr_in7_max.attr,
627 	&sensor_dev_attr_in7_alarm.dev_attr.attr,
628 
629 	&dev_attr_fan2_input.attr,
630 	&dev_attr_fan2_min.attr,
631 	&dev_attr_fan2_div.attr,
632 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
633 
634 	&dev_attr_temp3_input.attr,
635 	&dev_attr_temp3_max.attr,
636 	&dev_attr_temp3_min.attr,
637 	&dev_attr_temp3_crit.attr,
638 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
639 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
640 
641 	&dev_attr_in0_input.attr,
642 	&dev_attr_in0_min.attr,
643 	&dev_attr_in0_max.attr,
644 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
645 	&dev_attr_in5_input.attr,
646 	&dev_attr_in5_min.attr,
647 	&dev_attr_in5_max.attr,
648 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
649 
650 	&dev_attr_cpu0_vid.attr,
651 	&dev_attr_vrm.attr,
652 
653 	NULL
654 };
655 
656 static const struct attribute_group lm87_group_opt = {
657 	.attrs = lm87_attributes_opt,
658 };
659 
660 /* Return 0 if detection is successful, -ENODEV otherwise */
661 static int lm87_detect(struct i2c_client *new_client,
662 		       struct i2c_board_info *info)
663 {
664 	struct i2c_adapter *adapter = new_client->adapter;
665 	const char *name;
666 	u8 cid, rev;
667 
668 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
669 		return -ENODEV;
670 
671 	if (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)
672 		return -ENODEV;
673 
674 	/* Now, we do the remaining detection. */
675 	cid = lm87_read_value(new_client, LM87_REG_COMPANY_ID);
676 	rev = lm87_read_value(new_client, LM87_REG_REVISION);
677 
678 	if (cid == 0x02			/* National Semiconductor */
679 	 && (rev >= 0x01 && rev <= 0x08))
680 		name = "lm87";
681 	else if (cid == 0x41		/* Analog Devices */
682 	      && (rev & 0xf0) == 0x10)
683 		name = "adm1024";
684 	else {
685 		dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n",
686 			new_client->addr);
687 		return -ENODEV;
688 	}
689 
690 	strlcpy(info->type, name, I2C_NAME_SIZE);
691 
692 	return 0;
693 }
694 
695 static int lm87_probe(struct i2c_client *new_client,
696 		      const struct i2c_device_id *id)
697 {
698 	struct lm87_data *data;
699 	int err;
700 
701 	data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL);
702 	if (!data) {
703 		err = -ENOMEM;
704 		goto exit;
705 	}
706 
707 	i2c_set_clientdata(new_client, data);
708 	data->valid = 0;
709 	mutex_init(&data->update_lock);
710 
711 	/* Initialize the LM87 chip */
712 	lm87_init_client(new_client);
713 
714 	data->in_scale[0] = 2500;
715 	data->in_scale[1] = 2700;
716 	data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
717 	data->in_scale[3] = 5000;
718 	data->in_scale[4] = 12000;
719 	data->in_scale[5] = 2700;
720 	data->in_scale[6] = 1875;
721 	data->in_scale[7] = 1875;
722 
723 	/* Register sysfs hooks */
724 	if ((err = sysfs_create_group(&new_client->dev.kobj, &lm87_group)))
725 		goto exit_free;
726 
727 	if (data->channel & CHAN_NO_FAN(0)) {
728 		if ((err = device_create_file(&new_client->dev,
729 					&dev_attr_in6_input))
730 		 || (err = device_create_file(&new_client->dev,
731 					&dev_attr_in6_min))
732 		 || (err = device_create_file(&new_client->dev,
733 					&dev_attr_in6_max))
734 		 || (err = device_create_file(&new_client->dev,
735 					&sensor_dev_attr_in6_alarm.dev_attr)))
736 			goto exit_remove;
737 	} else {
738 		if ((err = device_create_file(&new_client->dev,
739 					&dev_attr_fan1_input))
740 		 || (err = device_create_file(&new_client->dev,
741 					&dev_attr_fan1_min))
742 		 || (err = device_create_file(&new_client->dev,
743 					&dev_attr_fan1_div))
744 		 || (err = device_create_file(&new_client->dev,
745 					&sensor_dev_attr_fan1_alarm.dev_attr)))
746 			goto exit_remove;
747 	}
748 
749 	if (data->channel & CHAN_NO_FAN(1)) {
750 		if ((err = device_create_file(&new_client->dev,
751 					&dev_attr_in7_input))
752 		 || (err = device_create_file(&new_client->dev,
753 					&dev_attr_in7_min))
754 		 || (err = device_create_file(&new_client->dev,
755 					&dev_attr_in7_max))
756 		 || (err = device_create_file(&new_client->dev,
757 					&sensor_dev_attr_in7_alarm.dev_attr)))
758 			goto exit_remove;
759 	} else {
760 		if ((err = device_create_file(&new_client->dev,
761 					&dev_attr_fan2_input))
762 		 || (err = device_create_file(&new_client->dev,
763 					&dev_attr_fan2_min))
764 		 || (err = device_create_file(&new_client->dev,
765 					&dev_attr_fan2_div))
766 		 || (err = device_create_file(&new_client->dev,
767 					&sensor_dev_attr_fan2_alarm.dev_attr)))
768 			goto exit_remove;
769 	}
770 
771 	if (data->channel & CHAN_TEMP3) {
772 		if ((err = device_create_file(&new_client->dev,
773 					&dev_attr_temp3_input))
774 		 || (err = device_create_file(&new_client->dev,
775 					&dev_attr_temp3_max))
776 		 || (err = device_create_file(&new_client->dev,
777 					&dev_attr_temp3_min))
778 		 || (err = device_create_file(&new_client->dev,
779 					&dev_attr_temp3_crit))
780 		 || (err = device_create_file(&new_client->dev,
781 					&sensor_dev_attr_temp3_alarm.dev_attr))
782 		 || (err = device_create_file(&new_client->dev,
783 					&sensor_dev_attr_temp3_fault.dev_attr)))
784 			goto exit_remove;
785 	} else {
786 		if ((err = device_create_file(&new_client->dev,
787 					&dev_attr_in0_input))
788 		 || (err = device_create_file(&new_client->dev,
789 					&dev_attr_in0_min))
790 		 || (err = device_create_file(&new_client->dev,
791 					&dev_attr_in0_max))
792 		 || (err = device_create_file(&new_client->dev,
793 					&sensor_dev_attr_in0_alarm.dev_attr))
794 		 || (err = device_create_file(&new_client->dev,
795 					&dev_attr_in5_input))
796 		 || (err = device_create_file(&new_client->dev,
797 					&dev_attr_in5_min))
798 		 || (err = device_create_file(&new_client->dev,
799 					&dev_attr_in5_max))
800 		 || (err = device_create_file(&new_client->dev,
801 					&sensor_dev_attr_in5_alarm.dev_attr)))
802 			goto exit_remove;
803 	}
804 
805 	if (!(data->channel & CHAN_NO_VID)) {
806 		data->vrm = vid_which_vrm();
807 		if ((err = device_create_file(&new_client->dev,
808 					&dev_attr_cpu0_vid))
809 		 || (err = device_create_file(&new_client->dev,
810 					&dev_attr_vrm)))
811 			goto exit_remove;
812 	}
813 
814 	data->hwmon_dev = hwmon_device_register(&new_client->dev);
815 	if (IS_ERR(data->hwmon_dev)) {
816 		err = PTR_ERR(data->hwmon_dev);
817 		goto exit_remove;
818 	}
819 
820 	return 0;
821 
822 exit_remove:
823 	sysfs_remove_group(&new_client->dev.kobj, &lm87_group);
824 	sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt);
825 exit_free:
826 	lm87_write_value(new_client, LM87_REG_CONFIG, data->config);
827 	kfree(data);
828 exit:
829 	return err;
830 }
831 
832 static void lm87_init_client(struct i2c_client *client)
833 {
834 	struct lm87_data *data = i2c_get_clientdata(client);
835 
836 	if (client->dev.platform_data) {
837 		data->channel = *(u8 *)client->dev.platform_data;
838 		lm87_write_value(client,
839 				 LM87_REG_CHANNEL_MODE, data->channel);
840 	} else {
841 		data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
842 	}
843 	data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F;
844 
845 	if (!(data->config & 0x01)) {
846 		int i;
847 
848 		/* Limits are left uninitialized after power-up */
849 		for (i = 1; i < 6; i++) {
850 			lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
851 			lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
852 		}
853 		for (i = 0; i < 2; i++) {
854 			lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
855 			lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
856 			lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
857 			lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
858 		}
859 		if (data->channel & CHAN_TEMP3) {
860 			lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
861 			lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
862 		} else {
863 			lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
864 			lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
865 		}
866 	}
867 
868 	/* Make sure Start is set and INT#_Clear is clear */
869 	if ((data->config & 0x09) != 0x01)
870 		lm87_write_value(client, LM87_REG_CONFIG,
871 				 (data->config & 0x77) | 0x01);
872 }
873 
874 static int lm87_remove(struct i2c_client *client)
875 {
876 	struct lm87_data *data = i2c_get_clientdata(client);
877 
878 	hwmon_device_unregister(data->hwmon_dev);
879 	sysfs_remove_group(&client->dev.kobj, &lm87_group);
880 	sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);
881 
882 	lm87_write_value(client, LM87_REG_CONFIG, data->config);
883 	kfree(data);
884 	return 0;
885 }
886 
887 static struct lm87_data *lm87_update_device(struct device *dev)
888 {
889 	struct i2c_client *client = to_i2c_client(dev);
890 	struct lm87_data *data = i2c_get_clientdata(client);
891 
892 	mutex_lock(&data->update_lock);
893 
894 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
895 		int i, j;
896 
897 		dev_dbg(&client->dev, "Updating data.\n");
898 
899 		i = (data->channel & CHAN_TEMP3) ? 1 : 0;
900 		j = (data->channel & CHAN_TEMP3) ? 5 : 6;
901 		for (; i < j; i++) {
902 			data->in[i] = lm87_read_value(client,
903 				      LM87_REG_IN(i));
904 			data->in_min[i] = lm87_read_value(client,
905 					  LM87_REG_IN_MIN(i));
906 			data->in_max[i] = lm87_read_value(client,
907 					  LM87_REG_IN_MAX(i));
908 		}
909 
910 		for (i = 0; i < 2; i++) {
911 			if (data->channel & CHAN_NO_FAN(i)) {
912 				data->in[6+i] = lm87_read_value(client,
913 						LM87_REG_AIN(i));
914 				data->in_max[6+i] = lm87_read_value(client,
915 						    LM87_REG_AIN_MAX(i));
916 				data->in_min[6+i] = lm87_read_value(client,
917 						    LM87_REG_AIN_MIN(i));
918 
919 			} else {
920 				data->fan[i] = lm87_read_value(client,
921 					       LM87_REG_FAN(i));
922 				data->fan_min[i] = lm87_read_value(client,
923 						   LM87_REG_FAN_MIN(i));
924 			}
925 		}
926 
927 		j = (data->channel & CHAN_TEMP3) ? 3 : 2;
928 		for (i = 0 ; i < j; i++) {
929 			data->temp[i] = lm87_read_value(client,
930 					LM87_REG_TEMP[i]);
931 			data->temp_high[i] = lm87_read_value(client,
932 					     LM87_REG_TEMP_HIGH[i]);
933 			data->temp_low[i] = lm87_read_value(client,
934 					    LM87_REG_TEMP_LOW[i]);
935 		}
936 
937 		i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
938 		j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
939 		data->temp_crit_int = min(i, j);
940 
941 		i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
942 		j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
943 		data->temp_crit_ext = min(i, j);
944 
945 		i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
946 		data->fan_div[0] = (i >> 4) & 0x03;
947 		data->fan_div[1] = (i >> 6) & 0x03;
948 		data->vid = (i & 0x0F)
949 			  | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
950 			     << 4;
951 
952 		data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
953 			     | (lm87_read_value(client, LM87_REG_ALARMS2)
954 				<< 8);
955 		data->aout = lm87_read_value(client, LM87_REG_AOUT);
956 
957 		data->last_updated = jiffies;
958 		data->valid = 1;
959 	}
960 
961 	mutex_unlock(&data->update_lock);
962 
963 	return data;
964 }
965 
966 static int __init sensors_lm87_init(void)
967 {
968 	return i2c_add_driver(&lm87_driver);
969 }
970 
971 static void __exit sensors_lm87_exit(void)
972 {
973 	i2c_del_driver(&lm87_driver);
974 }
975 
976 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
977 MODULE_DESCRIPTION("LM87 driver");
978 MODULE_LICENSE("GPL");
979 
980 module_init(sensors_lm87_init);
981 module_exit(sensors_lm87_exit);
982