Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / lm87.c
blobd4081024d380f00915017d10806f394fafda905e
1 /*
2 * lm87.c
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,2007 Jean Delvare <khali@linux-fr.org>
10 * Original port to Linux 2.6 by Jeff Oliver.
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
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 assumes that
25 * the BIOS configured the chip correctly. In that respect, it differs from
26 * the original driver (from lm_sensors for Linux 2.4), which would force the
27 * LM87 to an arbitrary, compile-time chosen mode, regardless of the actual
28 * chipset wiring.
29 * For reference, here is the list of exclusive functions:
30 * - in0+in5 (default) or temp3
31 * - fan1 (default) or in6
32 * - fan2 (default) or in7
33 * - VID lines (default) or IRQ lines (not handled by this driver)
35 * The LM87 additionally features an analog output, supposedly usable to
36 * control the speed of a fan. All new chips use pulse width modulation
37 * instead. The LM87 is the only hardware monitoring chipset I know of
38 * which uses amplitude modulation. Be careful when using this feature.
40 * This driver also supports the ADM1024, a sensor chip made by Analog
41 * Devices. That chip is fully compatible with the LM87. Complete
42 * datasheet can be obtained from Analog's website at:
43 * http://www.analog.com/en/prod/0,2877,ADM1024,00.html
45 * This program is free software; you can redistribute it and/or modify
46 * it under the terms of the GNU General Public License as published by
47 * the Free Software Foundation; either version 2 of the License, or
48 * (at your option) any later version.
50 * This program is distributed in the hope that it will be useful,
51 * but WITHOUT ANY WARRANTY; without even the implied warranty of
52 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53 * GNU General Public License for more details.
55 * You should have received a copy of the GNU General Public License
56 * along with this program; if not, write to the Free Software
57 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
60 #include <linux/module.h>
61 #include <linux/init.h>
62 #include <linux/slab.h>
63 #include <linux/jiffies.h>
64 #include <linux/i2c.h>
65 #include <linux/hwmon.h>
66 #include <linux/hwmon-sysfs.h>
67 #include <linux/hwmon-vid.h>
68 #include <linux/err.h>
69 #include <linux/mutex.h>
72 * Addresses to scan
73 * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
76 <<<<<<< HEAD:drivers/hwmon/lm87.c
77 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
78 =======
79 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
80 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/lm87.c
83 * Insmod parameters
86 I2C_CLIENT_INSMOD_2(lm87, adm1024);
89 * The LM87 registers
92 /* nr in 0..5 */
93 #define LM87_REG_IN(nr) (0x20 + (nr))
94 #define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
95 #define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
96 /* nr in 0..1 */
97 #define LM87_REG_AIN(nr) (0x28 + (nr))
98 #define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
99 #define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
101 static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
102 static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
103 static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
105 #define LM87_REG_TEMP_HW_INT_LOCK 0x13
106 #define LM87_REG_TEMP_HW_EXT_LOCK 0x14
107 #define LM87_REG_TEMP_HW_INT 0x17
108 #define LM87_REG_TEMP_HW_EXT 0x18
110 /* nr in 0..1 */
111 #define LM87_REG_FAN(nr) (0x28 + (nr))
112 #define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
113 #define LM87_REG_AOUT 0x19
115 #define LM87_REG_CONFIG 0x40
116 #define LM87_REG_CHANNEL_MODE 0x16
117 #define LM87_REG_VID_FAN_DIV 0x47
118 #define LM87_REG_VID4 0x49
120 #define LM87_REG_ALARMS1 0x41
121 #define LM87_REG_ALARMS2 0x42
123 #define LM87_REG_COMPANY_ID 0x3E
124 #define LM87_REG_REVISION 0x3F
127 * Conversions and various macros
128 * The LM87 uses signed 8-bit values for temperatures.
131 #define IN_FROM_REG(reg,scale) (((reg) * (scale) + 96) / 192)
132 #define IN_TO_REG(val,scale) ((val) <= 0 ? 0 : \
133 (val) * 192 >= (scale) * 255 ? 255 : \
134 ((val) * 192 + (scale)/2) / (scale))
136 #define TEMP_FROM_REG(reg) ((reg) * 1000)
137 #define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
138 (val) >= 126500 ? 127 : \
139 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
141 #define FAN_FROM_REG(reg,div) ((reg) == 255 || (reg) == 0 ? 0 : \
142 (1350000 + (reg)*(div) / 2) / ((reg)*(div)))
143 #define FAN_TO_REG(val,div) ((val)*(div) * 255 <= 1350000 ? 255 : \
144 (1350000 + (val)*(div) / 2) / ((val)*(div)))
146 #define FAN_DIV_FROM_REG(reg) (1 << (reg))
148 /* analog out is 9.80mV/LSB */
149 #define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
150 #define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
151 (val) >= 2500 ? 255 : \
152 ((val) * 10 + 49) / 98)
154 /* nr in 0..1 */
155 #define CHAN_NO_FAN(nr) (1 << (nr))
156 #define CHAN_TEMP3 (1 << 2)
157 #define CHAN_VCC_5V (1 << 3)
158 #define CHAN_NO_VID (1 << 7)
161 * Functions declaration
164 static int lm87_attach_adapter(struct i2c_adapter *adapter);
165 static int lm87_detect(struct i2c_adapter *adapter, int address, int kind);
166 static void lm87_init_client(struct i2c_client *client);
167 static int lm87_detach_client(struct i2c_client *client);
168 static struct lm87_data *lm87_update_device(struct device *dev);
171 * Driver data (common to all clients)
174 static struct i2c_driver lm87_driver = {
175 .driver = {
176 .name = "lm87",
178 .attach_adapter = lm87_attach_adapter,
179 .detach_client = lm87_detach_client,
183 * Client data (each client gets its own)
186 struct lm87_data {
187 struct i2c_client client;
188 struct device *hwmon_dev;
189 struct mutex update_lock;
190 char valid; /* zero until following fields are valid */
191 unsigned long last_updated; /* In jiffies */
193 u8 channel; /* register value */
195 u8 in[8]; /* register value */
196 u8 in_max[8]; /* register value */
197 u8 in_min[8]; /* register value */
198 u16 in_scale[8];
200 s8 temp[3]; /* register value */
201 s8 temp_high[3]; /* register value */
202 s8 temp_low[3]; /* register value */
203 s8 temp_crit_int; /* min of two register values */
204 s8 temp_crit_ext; /* min of two register values */
206 u8 fan[2]; /* register value */
207 u8 fan_min[2]; /* register value */
208 u8 fan_div[2]; /* register value, shifted right */
209 u8 aout; /* register value */
211 u16 alarms; /* register values, combined */
212 u8 vid; /* register values, combined */
213 u8 vrm;
217 * Sysfs stuff
220 static inline int lm87_read_value(struct i2c_client *client, u8 reg)
222 return i2c_smbus_read_byte_data(client, reg);
225 static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
227 return i2c_smbus_write_byte_data(client, reg, value);
230 #define show_in(offset) \
231 static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
233 struct lm87_data *data = lm87_update_device(dev); \
234 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
235 data->in_scale[offset])); \
237 static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
239 struct lm87_data *data = lm87_update_device(dev); \
240 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
241 data->in_scale[offset])); \
243 static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
245 struct lm87_data *data = lm87_update_device(dev); \
246 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
247 data->in_scale[offset])); \
249 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
250 show_in##offset##_input, NULL);
251 show_in(0);
252 show_in(1);
253 show_in(2);
254 show_in(3);
255 show_in(4);
256 show_in(5);
257 show_in(6);
258 show_in(7);
260 static void set_in_min(struct device *dev, const char *buf, int nr)
262 struct i2c_client *client = to_i2c_client(dev);
263 struct lm87_data *data = i2c_get_clientdata(client);
264 long val = simple_strtol(buf, NULL, 10);
266 mutex_lock(&data->update_lock);
267 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
268 lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
269 LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
270 mutex_unlock(&data->update_lock);
273 static void set_in_max(struct device *dev, const char *buf, int nr)
275 struct i2c_client *client = to_i2c_client(dev);
276 struct lm87_data *data = i2c_get_clientdata(client);
277 long val = simple_strtol(buf, NULL, 10);
279 mutex_lock(&data->update_lock);
280 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
281 lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
282 LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
283 mutex_unlock(&data->update_lock);
286 #define set_in(offset) \
287 static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, \
288 const char *buf, size_t count) \
290 set_in_min(dev, buf, offset); \
291 return count; \
293 static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, \
294 const char *buf, size_t count) \
296 set_in_max(dev, buf, offset); \
297 return count; \
299 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
300 show_in##offset##_min, set_in##offset##_min); \
301 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
302 show_in##offset##_max, set_in##offset##_max);
303 set_in(0);
304 set_in(1);
305 set_in(2);
306 set_in(3);
307 set_in(4);
308 set_in(5);
309 set_in(6);
310 set_in(7);
312 #define show_temp(offset) \
313 static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
315 struct lm87_data *data = lm87_update_device(dev); \
316 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
318 static ssize_t show_temp##offset##_low(struct device *dev, struct device_attribute *attr, char *buf) \
320 struct lm87_data *data = lm87_update_device(dev); \
321 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
323 static ssize_t show_temp##offset##_high(struct device *dev, struct device_attribute *attr, char *buf) \
325 struct lm87_data *data = lm87_update_device(dev); \
326 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
328 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
329 show_temp##offset##_input, NULL);
330 show_temp(1);
331 show_temp(2);
332 show_temp(3);
334 static void set_temp_low(struct device *dev, const char *buf, int nr)
336 struct i2c_client *client = to_i2c_client(dev);
337 struct lm87_data *data = i2c_get_clientdata(client);
338 long val = simple_strtol(buf, NULL, 10);
340 mutex_lock(&data->update_lock);
341 data->temp_low[nr] = TEMP_TO_REG(val);
342 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
343 mutex_unlock(&data->update_lock);
346 static void set_temp_high(struct device *dev, const char *buf, int nr)
348 struct i2c_client *client = to_i2c_client(dev);
349 struct lm87_data *data = i2c_get_clientdata(client);
350 long val = simple_strtol(buf, NULL, 10);
352 mutex_lock(&data->update_lock);
353 data->temp_high[nr] = TEMP_TO_REG(val);
354 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
355 mutex_unlock(&data->update_lock);
358 #define set_temp(offset) \
359 static ssize_t set_temp##offset##_low(struct device *dev, struct device_attribute *attr, \
360 const char *buf, size_t count) \
362 set_temp_low(dev, buf, offset-1); \
363 return count; \
365 static ssize_t set_temp##offset##_high(struct device *dev, struct device_attribute *attr, \
366 const char *buf, size_t count) \
368 set_temp_high(dev, buf, offset-1); \
369 return count; \
371 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
372 show_temp##offset##_high, set_temp##offset##_high); \
373 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
374 show_temp##offset##_low, set_temp##offset##_low);
375 set_temp(1);
376 set_temp(2);
377 set_temp(3);
379 static ssize_t show_temp_crit_int(struct device *dev, struct device_attribute *attr, char *buf)
381 struct lm87_data *data = lm87_update_device(dev);
382 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
385 static ssize_t show_temp_crit_ext(struct device *dev, struct device_attribute *attr, char *buf)
387 struct lm87_data *data = lm87_update_device(dev);
388 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
391 static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
392 static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
393 static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
395 #define show_fan(offset) \
396 static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
398 struct lm87_data *data = lm87_update_device(dev); \
399 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \
400 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
402 static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
404 struct lm87_data *data = lm87_update_device(dev); \
405 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \
406 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
408 static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
410 struct lm87_data *data = lm87_update_device(dev); \
411 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
413 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
414 show_fan##offset##_input, NULL);
415 show_fan(1);
416 show_fan(2);
418 static void set_fan_min(struct device *dev, const char *buf, int nr)
420 struct i2c_client *client = to_i2c_client(dev);
421 struct lm87_data *data = i2c_get_clientdata(client);
422 long val = simple_strtol(buf, NULL, 10);
424 mutex_lock(&data->update_lock);
425 data->fan_min[nr] = FAN_TO_REG(val,
426 FAN_DIV_FROM_REG(data->fan_div[nr]));
427 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
428 mutex_unlock(&data->update_lock);
431 /* Note: we save and restore the fan minimum here, because its value is
432 determined in part by the fan clock divider. This follows the principle
433 of least surprise; the user doesn't expect the fan minimum to change just
434 because the divider changed. */
435 static ssize_t set_fan_div(struct device *dev, const char *buf,
436 size_t count, int nr)
438 struct i2c_client *client = to_i2c_client(dev);
439 struct lm87_data *data = i2c_get_clientdata(client);
440 long val = simple_strtol(buf, NULL, 10);
441 unsigned long min;
442 u8 reg;
444 mutex_lock(&data->update_lock);
445 min = FAN_FROM_REG(data->fan_min[nr],
446 FAN_DIV_FROM_REG(data->fan_div[nr]));
448 switch (val) {
449 case 1: data->fan_div[nr] = 0; break;
450 case 2: data->fan_div[nr] = 1; break;
451 case 4: data->fan_div[nr] = 2; break;
452 case 8: data->fan_div[nr] = 3; break;
453 default:
454 mutex_unlock(&data->update_lock);
455 return -EINVAL;
458 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
459 switch (nr) {
460 case 0:
461 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
462 break;
463 case 1:
464 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
465 break;
467 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
469 data->fan_min[nr] = FAN_TO_REG(min, val);
470 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
471 data->fan_min[nr]);
472 mutex_unlock(&data->update_lock);
474 return count;
477 #define set_fan(offset) \
478 static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
479 size_t count) \
481 set_fan_min(dev, buf, offset-1); \
482 return count; \
484 static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
485 size_t count) \
487 return set_fan_div(dev, buf, count, offset-1); \
489 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
490 show_fan##offset##_min, set_fan##offset##_min); \
491 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
492 show_fan##offset##_div, set_fan##offset##_div);
493 set_fan(1);
494 set_fan(2);
496 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
498 struct lm87_data *data = lm87_update_device(dev);
499 return sprintf(buf, "%d\n", data->alarms);
501 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
503 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
505 struct lm87_data *data = lm87_update_device(dev);
506 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
508 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
510 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
512 struct lm87_data *data = dev_get_drvdata(dev);
513 return sprintf(buf, "%d\n", data->vrm);
515 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
517 struct lm87_data *data = dev_get_drvdata(dev);
518 data->vrm = simple_strtoul(buf, NULL, 10);
519 return count;
521 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
523 static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
525 struct lm87_data *data = lm87_update_device(dev);
526 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
528 static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
530 struct i2c_client *client = to_i2c_client(dev);
531 struct lm87_data *data = i2c_get_clientdata(client);
532 long val = simple_strtol(buf, NULL, 10);
534 mutex_lock(&data->update_lock);
535 data->aout = AOUT_TO_REG(val);
536 lm87_write_value(client, LM87_REG_AOUT, data->aout);
537 mutex_unlock(&data->update_lock);
538 return count;
540 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
542 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
543 char *buf)
545 struct lm87_data *data = lm87_update_device(dev);
546 int bitnr = to_sensor_dev_attr(attr)->index;
547 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
549 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
550 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
551 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
552 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
553 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
554 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
555 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
556 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
557 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
558 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
559 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
560 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
561 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
562 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
563 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
566 * Real code
569 static int lm87_attach_adapter(struct i2c_adapter *adapter)
571 if (!(adapter->class & I2C_CLASS_HWMON))
572 return 0;
573 return i2c_probe(adapter, &addr_data, lm87_detect);
576 static struct attribute *lm87_attributes[] = {
577 &dev_attr_in1_input.attr,
578 &dev_attr_in1_min.attr,
579 &dev_attr_in1_max.attr,
580 &sensor_dev_attr_in1_alarm.dev_attr.attr,
581 &dev_attr_in2_input.attr,
582 &dev_attr_in2_min.attr,
583 &dev_attr_in2_max.attr,
584 &sensor_dev_attr_in2_alarm.dev_attr.attr,
585 &dev_attr_in3_input.attr,
586 &dev_attr_in3_min.attr,
587 &dev_attr_in3_max.attr,
588 &sensor_dev_attr_in3_alarm.dev_attr.attr,
589 &dev_attr_in4_input.attr,
590 &dev_attr_in4_min.attr,
591 &dev_attr_in4_max.attr,
592 &sensor_dev_attr_in4_alarm.dev_attr.attr,
594 &dev_attr_temp1_input.attr,
595 &dev_attr_temp1_max.attr,
596 &dev_attr_temp1_min.attr,
597 &dev_attr_temp1_crit.attr,
598 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
599 &dev_attr_temp2_input.attr,
600 &dev_attr_temp2_max.attr,
601 &dev_attr_temp2_min.attr,
602 &dev_attr_temp2_crit.attr,
603 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
604 &sensor_dev_attr_temp2_fault.dev_attr.attr,
606 &dev_attr_alarms.attr,
607 &dev_attr_aout_output.attr,
609 NULL
612 static const struct attribute_group lm87_group = {
613 .attrs = lm87_attributes,
616 static struct attribute *lm87_attributes_opt[] = {
617 &dev_attr_in6_input.attr,
618 &dev_attr_in6_min.attr,
619 &dev_attr_in6_max.attr,
620 &sensor_dev_attr_in6_alarm.dev_attr.attr,
622 &dev_attr_fan1_input.attr,
623 &dev_attr_fan1_min.attr,
624 &dev_attr_fan1_div.attr,
625 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
627 &dev_attr_in7_input.attr,
628 &dev_attr_in7_min.attr,
629 &dev_attr_in7_max.attr,
630 &sensor_dev_attr_in7_alarm.dev_attr.attr,
632 &dev_attr_fan2_input.attr,
633 &dev_attr_fan2_min.attr,
634 &dev_attr_fan2_div.attr,
635 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
637 &dev_attr_temp3_input.attr,
638 &dev_attr_temp3_max.attr,
639 &dev_attr_temp3_min.attr,
640 &dev_attr_temp3_crit.attr,
641 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
642 &sensor_dev_attr_temp3_fault.dev_attr.attr,
644 &dev_attr_in0_input.attr,
645 &dev_attr_in0_min.attr,
646 &dev_attr_in0_max.attr,
647 &sensor_dev_attr_in0_alarm.dev_attr.attr,
648 &dev_attr_in5_input.attr,
649 &dev_attr_in5_min.attr,
650 &dev_attr_in5_max.attr,
651 &sensor_dev_attr_in5_alarm.dev_attr.attr,
653 &dev_attr_cpu0_vid.attr,
654 &dev_attr_vrm.attr,
656 NULL
659 static const struct attribute_group lm87_group_opt = {
660 .attrs = lm87_attributes_opt,
664 * The following function does more than just detection. If detection
665 * succeeds, it also registers the new chip.
667 static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
669 struct i2c_client *new_client;
670 struct lm87_data *data;
671 int err = 0;
672 static const char *names[] = { "lm87", "adm1024" };
674 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
675 goto exit;
677 if (!(data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL))) {
678 err = -ENOMEM;
679 goto exit;
682 /* The common I2C client data is placed right before the
683 LM87-specific data. */
684 new_client = &data->client;
685 i2c_set_clientdata(new_client, data);
686 new_client->addr = address;
687 new_client->adapter = adapter;
688 new_client->driver = &lm87_driver;
689 new_client->flags = 0;
691 /* Default to an LM87 if forced */
692 if (kind == 0)
693 kind = lm87;
695 /* Now, we do the remaining detection. */
696 if (kind < 0) {
697 u8 cid = lm87_read_value(new_client, LM87_REG_COMPANY_ID);
698 u8 rev = lm87_read_value(new_client, LM87_REG_REVISION);
700 if (cid == 0x02 /* National Semiconductor */
701 && (rev >= 0x01 && rev <= 0x08))
702 kind = lm87;
703 else if (cid == 0x41 /* Analog Devices */
704 && (rev & 0xf0) == 0x10)
705 kind = adm1024;
707 if (kind < 0
708 || (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)) {
709 dev_dbg(&adapter->dev,
710 "LM87 detection failed at 0x%02x.\n",
711 address);
712 goto exit_free;
716 /* We can fill in the remaining client fields */
717 strlcpy(new_client->name, names[kind - 1], I2C_NAME_SIZE);
718 data->valid = 0;
719 mutex_init(&data->update_lock);
721 /* Tell the I2C layer a new client has arrived */
722 if ((err = i2c_attach_client(new_client)))
723 goto exit_free;
725 /* Initialize the LM87 chip */
726 lm87_init_client(new_client);
728 data->in_scale[0] = 2500;
729 data->in_scale[1] = 2700;
730 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
731 data->in_scale[3] = 5000;
732 data->in_scale[4] = 12000;
733 data->in_scale[5] = 2700;
734 data->in_scale[6] = 1875;
735 data->in_scale[7] = 1875;
737 /* Register sysfs hooks */
738 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm87_group)))
739 goto exit_detach;
741 if (data->channel & CHAN_NO_FAN(0)) {
742 if ((err = device_create_file(&new_client->dev,
743 &dev_attr_in6_input))
744 || (err = device_create_file(&new_client->dev,
745 &dev_attr_in6_min))
746 || (err = device_create_file(&new_client->dev,
747 &dev_attr_in6_max))
748 || (err = device_create_file(&new_client->dev,
749 &sensor_dev_attr_in6_alarm.dev_attr)))
750 goto exit_remove;
751 } else {
752 if ((err = device_create_file(&new_client->dev,
753 &dev_attr_fan1_input))
754 || (err = device_create_file(&new_client->dev,
755 &dev_attr_fan1_min))
756 || (err = device_create_file(&new_client->dev,
757 &dev_attr_fan1_div))
758 || (err = device_create_file(&new_client->dev,
759 &sensor_dev_attr_fan1_alarm.dev_attr)))
760 goto exit_remove;
763 if (data->channel & CHAN_NO_FAN(1)) {
764 if ((err = device_create_file(&new_client->dev,
765 &dev_attr_in7_input))
766 || (err = device_create_file(&new_client->dev,
767 &dev_attr_in7_min))
768 || (err = device_create_file(&new_client->dev,
769 &dev_attr_in7_max))
770 || (err = device_create_file(&new_client->dev,
771 &sensor_dev_attr_in7_alarm.dev_attr)))
772 goto exit_remove;
773 } else {
774 if ((err = device_create_file(&new_client->dev,
775 &dev_attr_fan2_input))
776 || (err = device_create_file(&new_client->dev,
777 &dev_attr_fan2_min))
778 || (err = device_create_file(&new_client->dev,
779 &dev_attr_fan2_div))
780 || (err = device_create_file(&new_client->dev,
781 &sensor_dev_attr_fan2_alarm.dev_attr)))
782 goto exit_remove;
785 if (data->channel & CHAN_TEMP3) {
786 if ((err = device_create_file(&new_client->dev,
787 &dev_attr_temp3_input))
788 || (err = device_create_file(&new_client->dev,
789 &dev_attr_temp3_max))
790 || (err = device_create_file(&new_client->dev,
791 &dev_attr_temp3_min))
792 || (err = device_create_file(&new_client->dev,
793 &dev_attr_temp3_crit))
794 || (err = device_create_file(&new_client->dev,
795 &sensor_dev_attr_temp3_alarm.dev_attr))
796 || (err = device_create_file(&new_client->dev,
797 &sensor_dev_attr_temp3_fault.dev_attr)))
798 goto exit_remove;
799 } else {
800 if ((err = device_create_file(&new_client->dev,
801 &dev_attr_in0_input))
802 || (err = device_create_file(&new_client->dev,
803 &dev_attr_in0_min))
804 || (err = device_create_file(&new_client->dev,
805 &dev_attr_in0_max))
806 || (err = device_create_file(&new_client->dev,
807 &sensor_dev_attr_in0_alarm.dev_attr))
808 || (err = device_create_file(&new_client->dev,
809 &dev_attr_in5_input))
810 || (err = device_create_file(&new_client->dev,
811 &dev_attr_in5_min))
812 || (err = device_create_file(&new_client->dev,
813 &dev_attr_in5_max))
814 || (err = device_create_file(&new_client->dev,
815 &sensor_dev_attr_in5_alarm.dev_attr)))
816 goto exit_remove;
819 if (!(data->channel & CHAN_NO_VID)) {
820 data->vrm = vid_which_vrm();
821 if ((err = device_create_file(&new_client->dev,
822 &dev_attr_cpu0_vid))
823 || (err = device_create_file(&new_client->dev,
824 &dev_attr_vrm)))
825 goto exit_remove;
828 data->hwmon_dev = hwmon_device_register(&new_client->dev);
829 if (IS_ERR(data->hwmon_dev)) {
830 err = PTR_ERR(data->hwmon_dev);
831 goto exit_remove;
834 return 0;
836 exit_remove:
837 sysfs_remove_group(&new_client->dev.kobj, &lm87_group);
838 sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt);
839 exit_detach:
840 i2c_detach_client(new_client);
841 exit_free:
842 kfree(data);
843 exit:
844 return err;
847 static void lm87_init_client(struct i2c_client *client)
849 struct lm87_data *data = i2c_get_clientdata(client);
850 u8 config;
852 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
854 config = lm87_read_value(client, LM87_REG_CONFIG);
855 if (!(config & 0x01)) {
856 int i;
858 /* Limits are left uninitialized after power-up */
859 for (i = 1; i < 6; i++) {
860 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
861 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
863 for (i = 0; i < 2; i++) {
864 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
865 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
866 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
867 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
869 if (data->channel & CHAN_TEMP3) {
870 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
871 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
872 } else {
873 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
874 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
877 if ((config & 0x81) != 0x01) {
878 /* Start monitoring */
879 lm87_write_value(client, LM87_REG_CONFIG,
880 (config & 0xF7) | 0x01);
884 static int lm87_detach_client(struct i2c_client *client)
886 struct lm87_data *data = i2c_get_clientdata(client);
887 int err;
889 hwmon_device_unregister(data->hwmon_dev);
890 sysfs_remove_group(&client->dev.kobj, &lm87_group);
891 sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);
893 if ((err = i2c_detach_client(client)))
894 return err;
896 kfree(data);
897 return 0;
900 static struct lm87_data *lm87_update_device(struct device *dev)
902 struct i2c_client *client = to_i2c_client(dev);
903 struct lm87_data *data = i2c_get_clientdata(client);
905 mutex_lock(&data->update_lock);
907 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
908 int i, j;
910 dev_dbg(&client->dev, "Updating data.\n");
912 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
913 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
914 for (; i < j; i++) {
915 data->in[i] = lm87_read_value(client,
916 LM87_REG_IN(i));
917 data->in_min[i] = lm87_read_value(client,
918 LM87_REG_IN_MIN(i));
919 data->in_max[i] = lm87_read_value(client,
920 LM87_REG_IN_MAX(i));
923 for (i = 0; i < 2; i++) {
924 if (data->channel & CHAN_NO_FAN(i)) {
925 data->in[6+i] = lm87_read_value(client,
926 LM87_REG_AIN(i));
927 data->in_max[6+i] = lm87_read_value(client,
928 LM87_REG_AIN_MAX(i));
929 data->in_min[6+i] = lm87_read_value(client,
930 LM87_REG_AIN_MIN(i));
932 } else {
933 data->fan[i] = lm87_read_value(client,
934 LM87_REG_FAN(i));
935 data->fan_min[i] = lm87_read_value(client,
936 LM87_REG_FAN_MIN(i));
940 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
941 for (i = 0 ; i < j; i++) {
942 data->temp[i] = lm87_read_value(client,
943 LM87_REG_TEMP[i]);
944 data->temp_high[i] = lm87_read_value(client,
945 LM87_REG_TEMP_HIGH[i]);
946 data->temp_low[i] = lm87_read_value(client,
947 LM87_REG_TEMP_LOW[i]);
950 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
951 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
952 data->temp_crit_int = min(i, j);
954 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
955 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
956 data->temp_crit_ext = min(i, j);
958 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
959 data->fan_div[0] = (i >> 4) & 0x03;
960 data->fan_div[1] = (i >> 6) & 0x03;
961 data->vid = (i & 0x0F)
962 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
963 << 4;
965 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
966 | (lm87_read_value(client, LM87_REG_ALARMS2)
967 << 8);
968 data->aout = lm87_read_value(client, LM87_REG_AOUT);
970 data->last_updated = jiffies;
971 data->valid = 1;
974 mutex_unlock(&data->update_lock);
976 return data;
979 static int __init sensors_lm87_init(void)
981 return i2c_add_driver(&lm87_driver);
984 static void __exit sensors_lm87_exit(void)
986 i2c_del_driver(&lm87_driver);
989 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
990 MODULE_DESCRIPTION("LM87 driver");
991 MODULE_LICENSE("GPL");
993 module_init(sensors_lm87_init);
994 module_exit(sensors_lm87_exit);