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 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
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 program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation; either version 2 of the License, or
43 * (at your option) any later version.
45 * This program is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU General Public License for more details.
50 * You should have received a copy of the GNU General Public License
51 * along with this program; if not, write to the Free Software
52 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
55 #include <linux/config.h>
56 #include <linux/module.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
59 #include <linux/jiffies.h>
60 #include <linux/i2c.h>
61 #include <linux/i2c-sensor.h>
62 #include <linux/i2c-vid.h>
66 * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
69 static unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
70 static unsigned int normal_isa
[] = { I2C_CLIENT_ISA_END
};
76 SENSORS_INSMOD_1(lm87
);
83 #define LM87_REG_IN(nr) (0x20 + (nr))
84 #define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
85 #define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
87 #define LM87_REG_AIN(nr) (0x28 + (nr))
88 #define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
89 #define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
91 static u8 LM87_REG_TEMP
[3] = { 0x27, 0x26, 0x20 };
92 static u8 LM87_REG_TEMP_HIGH
[3] = { 0x39, 0x37, 0x2B };
93 static u8 LM87_REG_TEMP_LOW
[3] = { 0x3A, 0x38, 0x2C };
95 #define LM87_REG_TEMP_HW_INT_LOCK 0x13
96 #define LM87_REG_TEMP_HW_EXT_LOCK 0x14
97 #define LM87_REG_TEMP_HW_INT 0x17
98 #define LM87_REG_TEMP_HW_EXT 0x18
101 #define LM87_REG_FAN(nr) (0x28 + (nr))
102 #define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
103 #define LM87_REG_AOUT 0x19
105 #define LM87_REG_CONFIG 0x40
106 #define LM87_REG_CHANNEL_MODE 0x16
107 #define LM87_REG_VID_FAN_DIV 0x47
108 #define LM87_REG_VID4 0x49
110 #define LM87_REG_ALARMS1 0x41
111 #define LM87_REG_ALARMS2 0x42
113 #define LM87_REG_COMPANY_ID 0x3E
114 #define LM87_REG_REVISION 0x3F
117 * Conversions and various macros
118 * The LM87 uses signed 8-bit values for temperatures.
121 #define IN_FROM_REG(reg,scale) (((reg) * (scale) + 96) / 192)
122 #define IN_TO_REG(val,scale) ((val) <= 0 ? 0 : \
123 (val) * 192 >= (scale) * 255 ? 255 : \
124 ((val) * 192 + (scale)/2) / (scale))
126 #define TEMP_FROM_REG(reg) ((reg) * 1000)
127 #define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
128 (val) >= 126500 ? 127 : \
129 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
131 #define FAN_FROM_REG(reg,div) ((reg) == 255 || (reg) == 0 ? 0 : \
132 1350000 + (reg)*(div) / 2) / ((reg)*(div))
133 #define FAN_TO_REG(val,div) ((val)*(div) * 255 <= 1350000 ? 255 : \
134 (1350000 + (val)*(div) / 2) / ((val)*(div)))
136 #define FAN_DIV_FROM_REG(reg) (1 << (reg))
138 /* analog out is 9.80mV/LSB */
139 #define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
140 #define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
141 (val) >= 2500 ? 255 : \
142 ((val) * 10 + 49) / 98)
145 #define CHAN_NO_FAN(nr) (1 << (nr))
146 #define CHAN_TEMP3 (1 << 2)
147 #define CHAN_VCC_5V (1 << 3)
148 #define CHAN_NO_VID (1 << 8)
151 * Functions declaration
154 static int lm87_attach_adapter(struct i2c_adapter
*adapter
);
155 static int lm87_detect(struct i2c_adapter
*adapter
, int address
, int kind
);
156 static void lm87_init_client(struct i2c_client
*client
);
157 static int lm87_detach_client(struct i2c_client
*client
);
158 static struct lm87_data
*lm87_update_device(struct device
*dev
);
161 * Driver data (common to all clients)
164 static struct i2c_driver lm87_driver
= {
165 .owner
= THIS_MODULE
,
167 .id
= I2C_DRIVERID_LM87
,
168 .flags
= I2C_DF_NOTIFY
,
169 .attach_adapter
= lm87_attach_adapter
,
170 .detach_client
= lm87_detach_client
,
174 * Client data (each client gets its own)
178 struct i2c_client client
;
179 struct semaphore update_lock
;
180 char valid
; /* zero until following fields are valid */
181 unsigned long last_updated
; /* In jiffies */
183 u8 channel
; /* register value */
185 u8 in
[8]; /* register value */
186 u8 in_max
[8]; /* register value */
187 u8 in_min
[8]; /* register value */
190 s8 temp
[3]; /* register value */
191 s8 temp_high
[3]; /* register value */
192 s8 temp_low
[3]; /* register value */
193 s8 temp_crit_int
; /* min of two register values */
194 s8 temp_crit_ext
; /* min of two register values */
196 u8 fan
[2]; /* register value */
197 u8 fan_min
[2]; /* register value */
198 u8 fan_div
[2]; /* register value, shifted right */
199 u8 aout
; /* register value */
201 u16 alarms
; /* register values, combined */
202 u8 vid
; /* register values, combined */
210 static inline int lm87_read_value(struct i2c_client
*client
, u8 reg
)
212 return i2c_smbus_read_byte_data(client
, reg
);
215 static inline int lm87_write_value(struct i2c_client
*client
, u8 reg
, u8 value
)
217 return i2c_smbus_write_byte_data(client
, reg
, value
);
220 #define show_in(offset) \
221 static ssize_t show_in##offset##_input(struct device *dev, char *buf) \
223 struct lm87_data *data = lm87_update_device(dev); \
224 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
225 data->in_scale[offset])); \
227 static ssize_t show_in##offset##_min(struct device *dev, char *buf) \
229 struct lm87_data *data = lm87_update_device(dev); \
230 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
231 data->in_scale[offset])); \
233 static ssize_t show_in##offset##_max(struct device *dev, char *buf) \
235 struct lm87_data *data = lm87_update_device(dev); \
236 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
237 data->in_scale[offset])); \
239 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
240 show_in##offset##_input, NULL);
250 static void set_in_min(struct device
*dev
, const char *buf
, int nr
)
252 struct i2c_client
*client
= to_i2c_client(dev
);
253 struct lm87_data
*data
= i2c_get_clientdata(client
);
254 long val
= simple_strtol(buf
, NULL
, 10);
256 down(&data
->update_lock
);
257 data
->in_min
[nr
] = IN_TO_REG(val
, data
->in_scale
[nr
]);
258 lm87_write_value(client
, nr
<6 ? LM87_REG_IN_MIN(nr
) :
259 LM87_REG_AIN_MIN(nr
-6), data
->in_min
[nr
]);
260 up(&data
->update_lock
);
263 static void set_in_max(struct device
*dev
, const char *buf
, int nr
)
265 struct i2c_client
*client
= to_i2c_client(dev
);
266 struct lm87_data
*data
= i2c_get_clientdata(client
);
267 long val
= simple_strtol(buf
, NULL
, 10);
269 down(&data
->update_lock
);
270 data
->in_max
[nr
] = IN_TO_REG(val
, data
->in_scale
[nr
]);
271 lm87_write_value(client
, nr
<6 ? LM87_REG_IN_MAX(nr
) :
272 LM87_REG_AIN_MAX(nr
-6), data
->in_max
[nr
]);
273 up(&data
->update_lock
);
276 #define set_in(offset) \
277 static ssize_t set_in##offset##_min(struct device *dev, \
278 const char *buf, size_t count) \
280 set_in_min(dev, buf, offset); \
283 static ssize_t set_in##offset##_max(struct device *dev, \
284 const char *buf, size_t count) \
286 set_in_max(dev, buf, offset); \
289 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
290 show_in##offset##_min, set_in##offset##_min); \
291 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
292 show_in##offset##_max, set_in##offset##_max);
302 #define show_temp(offset) \
303 static ssize_t show_temp##offset##_input(struct device *dev, char *buf) \
305 struct lm87_data *data = lm87_update_device(dev); \
306 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
308 static ssize_t show_temp##offset##_low(struct device *dev, char *buf) \
310 struct lm87_data *data = lm87_update_device(dev); \
311 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
313 static ssize_t show_temp##offset##_high(struct device *dev, char *buf) \
315 struct lm87_data *data = lm87_update_device(dev); \
316 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
318 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
319 show_temp##offset##_input, NULL);
324 static void set_temp_low(struct device
*dev
, const char *buf
, int nr
)
326 struct i2c_client
*client
= to_i2c_client(dev
);
327 struct lm87_data
*data
= i2c_get_clientdata(client
);
328 long val
= simple_strtol(buf
, NULL
, 10);
330 down(&data
->update_lock
);
331 data
->temp_low
[nr
] = TEMP_TO_REG(val
);
332 lm87_write_value(client
, LM87_REG_TEMP_LOW
[nr
], data
->temp_low
[nr
]);
333 up(&data
->update_lock
);
336 static void set_temp_high(struct device
*dev
, const char *buf
, int nr
)
338 struct i2c_client
*client
= to_i2c_client(dev
);
339 struct lm87_data
*data
= i2c_get_clientdata(client
);
340 long val
= simple_strtol(buf
, NULL
, 10);
342 down(&data
->update_lock
);
343 data
->temp_high
[nr
] = TEMP_TO_REG(val
);
344 lm87_write_value(client
, LM87_REG_TEMP_HIGH
[nr
], data
->temp_high
[nr
]);
345 up(&data
->update_lock
);
348 #define set_temp(offset) \
349 static ssize_t set_temp##offset##_low(struct device *dev, \
350 const char *buf, size_t count) \
352 set_temp_low(dev, buf, offset-1); \
355 static ssize_t set_temp##offset##_high(struct device *dev, \
356 const char *buf, size_t count) \
358 set_temp_high(dev, buf, offset-1); \
361 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
362 show_temp##offset##_high, set_temp##offset##_high); \
363 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
364 show_temp##offset##_low, set_temp##offset##_low);
369 static ssize_t
show_temp_crit_int(struct device
*dev
, char *buf
)
371 struct lm87_data
*data
= lm87_update_device(dev
);
372 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_crit_int
));
375 static ssize_t
show_temp_crit_ext(struct device
*dev
, char *buf
)
377 struct lm87_data
*data
= lm87_update_device(dev
);
378 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_crit_ext
));
381 static DEVICE_ATTR(temp1_crit
, S_IRUGO
, show_temp_crit_int
, NULL
);
382 static DEVICE_ATTR(temp2_crit
, S_IRUGO
, show_temp_crit_ext
, NULL
);
383 static DEVICE_ATTR(temp3_crit
, S_IRUGO
, show_temp_crit_ext
, NULL
);
385 #define show_fan(offset) \
386 static ssize_t show_fan##offset##_input(struct device *dev, char *buf) \
388 struct lm87_data *data = lm87_update_device(dev); \
389 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \
390 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
392 static ssize_t show_fan##offset##_min(struct device *dev, char *buf) \
394 struct lm87_data *data = lm87_update_device(dev); \
395 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \
396 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
398 static ssize_t show_fan##offset##_div(struct device *dev, char *buf) \
400 struct lm87_data *data = lm87_update_device(dev); \
401 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
403 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
404 show_fan##offset##_input, NULL);
408 static void set_fan_min(struct device
*dev
, const char *buf
, int nr
)
410 struct i2c_client
*client
= to_i2c_client(dev
);
411 struct lm87_data
*data
= i2c_get_clientdata(client
);
412 long val
= simple_strtol(buf
, NULL
, 10);
414 down(&data
->update_lock
);
415 data
->fan_min
[nr
] = FAN_TO_REG(val
,
416 FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
417 lm87_write_value(client
, LM87_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
418 up(&data
->update_lock
);
421 /* Note: we save and restore the fan minimum here, because its value is
422 determined in part by the fan clock divider. This follows the principle
423 of least suprise; the user doesn't expect the fan minimum to change just
424 because the divider changed. */
425 static ssize_t
set_fan_div(struct device
*dev
, const char *buf
,
426 size_t count
, int nr
)
428 struct i2c_client
*client
= to_i2c_client(dev
);
429 struct lm87_data
*data
= i2c_get_clientdata(client
);
430 long val
= simple_strtol(buf
, NULL
, 10);
434 down(&data
->update_lock
);
435 min
= FAN_FROM_REG(data
->fan_min
[nr
],
436 FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
439 case 1: data
->fan_div
[nr
] = 0; break;
440 case 2: data
->fan_div
[nr
] = 1; break;
441 case 4: data
->fan_div
[nr
] = 2; break;
442 case 8: data
->fan_div
[nr
] = 3; break;
444 up(&data
->update_lock
);
448 reg
= lm87_read_value(client
, LM87_REG_VID_FAN_DIV
);
451 reg
= (reg
& 0xCF) | (data
->fan_div
[0] << 4);
454 reg
= (reg
& 0x3F) | (data
->fan_div
[1] << 6);
457 lm87_write_value(client
, LM87_REG_VID_FAN_DIV
, reg
);
459 data
->fan_min
[nr
] = FAN_TO_REG(min
, val
);
460 lm87_write_value(client
, LM87_REG_FAN_MIN(nr
),
462 up(&data
->update_lock
);
467 #define set_fan(offset) \
468 static ssize_t set_fan##offset##_min(struct device *dev, const char *buf, \
471 set_fan_min(dev, buf, offset-1); \
474 static ssize_t set_fan##offset##_div(struct device *dev, const char *buf, \
477 return set_fan_div(dev, buf, count, offset-1); \
479 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
480 show_fan##offset##_min, set_fan##offset##_min); \
481 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
482 show_fan##offset##_div, set_fan##offset##_div);
486 static ssize_t
show_alarms(struct device
*dev
, char *buf
)
488 struct lm87_data
*data
= lm87_update_device(dev
);
489 return sprintf(buf
, "%d\n", data
->alarms
);
491 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
493 static ssize_t
show_vid(struct device
*dev
, char *buf
)
495 struct lm87_data
*data
= lm87_update_device(dev
);
496 return sprintf(buf
, "%d\n", vid_from_reg(data
->vid
, data
->vrm
));
498 static DEVICE_ATTR(cpu0_vid
, S_IRUGO
, show_vid
, NULL
);
500 static ssize_t
show_vrm(struct device
*dev
, char *buf
)
502 struct lm87_data
*data
= lm87_update_device(dev
);
503 return sprintf(buf
, "%d\n", data
->vrm
);
505 static ssize_t
set_vrm(struct device
*dev
, const char *buf
, size_t count
)
507 struct i2c_client
*client
= to_i2c_client(dev
);
508 struct lm87_data
*data
= i2c_get_clientdata(client
);
509 data
->vrm
= simple_strtoul(buf
, NULL
, 10);
512 static DEVICE_ATTR(vrm
, S_IRUGO
| S_IWUSR
, show_vrm
, set_vrm
);
514 static ssize_t
show_aout(struct device
*dev
, char *buf
)
516 struct lm87_data
*data
= lm87_update_device(dev
);
517 return sprintf(buf
, "%d\n", AOUT_FROM_REG(data
->aout
));
519 static ssize_t
set_aout(struct device
*dev
, const char *buf
, size_t count
)
521 struct i2c_client
*client
= to_i2c_client(dev
);
522 struct lm87_data
*data
= i2c_get_clientdata(client
);
523 long val
= simple_strtol(buf
, NULL
, 10);
525 down(&data
->update_lock
);
526 data
->aout
= AOUT_TO_REG(val
);
527 lm87_write_value(client
, LM87_REG_AOUT
, data
->aout
);
528 up(&data
->update_lock
);
531 static DEVICE_ATTR(aout_output
, S_IRUGO
| S_IWUSR
, show_aout
, set_aout
);
537 static int lm87_attach_adapter(struct i2c_adapter
*adapter
)
539 if (!(adapter
->class & I2C_CLASS_HWMON
))
541 return i2c_detect(adapter
, &addr_data
, lm87_detect
);
545 * The following function does more than just detection. If detection
546 * succeeds, it also registers the new chip.
548 static int lm87_detect(struct i2c_adapter
*adapter
, int address
, int kind
)
550 struct i2c_client
*new_client
;
551 struct lm87_data
*data
;
554 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
557 if (!(data
= kmalloc(sizeof(struct lm87_data
), GFP_KERNEL
))) {
561 memset(data
, 0, sizeof(struct lm87_data
));
563 /* The common I2C client data is placed right before the
564 LM87-specific data. */
565 new_client
= &data
->client
;
566 i2c_set_clientdata(new_client
, data
);
567 new_client
->addr
= address
;
568 new_client
->adapter
= adapter
;
569 new_client
->driver
= &lm87_driver
;
570 new_client
->flags
= 0;
572 /* Default to an LM87 if forced */
576 /* Now, we do the remaining detection. */
578 u8 rev
= lm87_read_value(new_client
, LM87_REG_REVISION
);
580 if (rev
< 0x01 || rev
> 0x08
581 || (lm87_read_value(new_client
, LM87_REG_CONFIG
) & 0x80)
582 || lm87_read_value(new_client
, LM87_REG_COMPANY_ID
) != 0x02) {
583 dev_dbg(&adapter
->dev
,
584 "LM87 detection failed at 0x%02x.\n",
590 /* We can fill in the remaining client fields */
591 strlcpy(new_client
->name
, "lm87", I2C_NAME_SIZE
);
593 init_MUTEX(&data
->update_lock
);
595 /* Tell the I2C layer a new client has arrived */
596 if ((err
= i2c_attach_client(new_client
)))
599 /* Initialize the LM87 chip */
600 lm87_init_client(new_client
);
602 data
->in_scale
[0] = 2500;
603 data
->in_scale
[1] = 2700;
604 data
->in_scale
[2] = (data
->channel
& CHAN_VCC_5V
) ? 5000 : 3300;
605 data
->in_scale
[3] = 5000;
606 data
->in_scale
[4] = 12000;
607 data
->in_scale
[5] = 2700;
608 data
->in_scale
[6] = 1875;
609 data
->in_scale
[7] = 1875;
611 /* Register sysfs hooks */
612 device_create_file(&new_client
->dev
, &dev_attr_in1_input
);
613 device_create_file(&new_client
->dev
, &dev_attr_in1_min
);
614 device_create_file(&new_client
->dev
, &dev_attr_in1_max
);
615 device_create_file(&new_client
->dev
, &dev_attr_in2_input
);
616 device_create_file(&new_client
->dev
, &dev_attr_in2_min
);
617 device_create_file(&new_client
->dev
, &dev_attr_in2_max
);
618 device_create_file(&new_client
->dev
, &dev_attr_in3_input
);
619 device_create_file(&new_client
->dev
, &dev_attr_in3_min
);
620 device_create_file(&new_client
->dev
, &dev_attr_in3_max
);
621 device_create_file(&new_client
->dev
, &dev_attr_in4_input
);
622 device_create_file(&new_client
->dev
, &dev_attr_in4_min
);
623 device_create_file(&new_client
->dev
, &dev_attr_in4_max
);
625 if (data
->channel
& CHAN_NO_FAN(0)) {
626 device_create_file(&new_client
->dev
, &dev_attr_in6_input
);
627 device_create_file(&new_client
->dev
, &dev_attr_in6_min
);
628 device_create_file(&new_client
->dev
, &dev_attr_in6_max
);
630 device_create_file(&new_client
->dev
, &dev_attr_fan1_input
);
631 device_create_file(&new_client
->dev
, &dev_attr_fan1_min
);
632 device_create_file(&new_client
->dev
, &dev_attr_fan1_div
);
634 if (data
->channel
& CHAN_NO_FAN(1)) {
635 device_create_file(&new_client
->dev
, &dev_attr_in7_input
);
636 device_create_file(&new_client
->dev
, &dev_attr_in7_min
);
637 device_create_file(&new_client
->dev
, &dev_attr_in7_max
);
639 device_create_file(&new_client
->dev
, &dev_attr_fan2_input
);
640 device_create_file(&new_client
->dev
, &dev_attr_fan2_min
);
641 device_create_file(&new_client
->dev
, &dev_attr_fan2_div
);
644 device_create_file(&new_client
->dev
, &dev_attr_temp1_input
);
645 device_create_file(&new_client
->dev
, &dev_attr_temp1_max
);
646 device_create_file(&new_client
->dev
, &dev_attr_temp1_min
);
647 device_create_file(&new_client
->dev
, &dev_attr_temp1_crit
);
648 device_create_file(&new_client
->dev
, &dev_attr_temp2_input
);
649 device_create_file(&new_client
->dev
, &dev_attr_temp2_max
);
650 device_create_file(&new_client
->dev
, &dev_attr_temp2_min
);
651 device_create_file(&new_client
->dev
, &dev_attr_temp2_crit
);
653 if (data
->channel
& CHAN_TEMP3
) {
654 device_create_file(&new_client
->dev
, &dev_attr_temp3_input
);
655 device_create_file(&new_client
->dev
, &dev_attr_temp3_max
);
656 device_create_file(&new_client
->dev
, &dev_attr_temp3_min
);
657 device_create_file(&new_client
->dev
, &dev_attr_temp3_crit
);
659 device_create_file(&new_client
->dev
, &dev_attr_in0_input
);
660 device_create_file(&new_client
->dev
, &dev_attr_in0_min
);
661 device_create_file(&new_client
->dev
, &dev_attr_in0_max
);
662 device_create_file(&new_client
->dev
, &dev_attr_in5_input
);
663 device_create_file(&new_client
->dev
, &dev_attr_in5_min
);
664 device_create_file(&new_client
->dev
, &dev_attr_in5_max
);
667 if (!(data
->channel
& CHAN_NO_VID
)) {
668 device_create_file(&new_client
->dev
, &dev_attr_cpu0_vid
);
669 device_create_file(&new_client
->dev
, &dev_attr_vrm
);
672 device_create_file(&new_client
->dev
, &dev_attr_alarms
);
673 device_create_file(&new_client
->dev
, &dev_attr_aout_output
);
683 static void lm87_init_client(struct i2c_client
*client
)
685 struct lm87_data
*data
= i2c_get_clientdata(client
);
688 data
->channel
= lm87_read_value(client
, LM87_REG_CHANNEL_MODE
);
689 data
->vrm
= i2c_which_vrm();
691 config
= lm87_read_value(client
, LM87_REG_CONFIG
);
692 if (!(config
& 0x01)) {
695 /* Limits are left uninitialized after power-up */
696 for (i
= 1; i
< 6; i
++) {
697 lm87_write_value(client
, LM87_REG_IN_MIN(i
), 0x00);
698 lm87_write_value(client
, LM87_REG_IN_MAX(i
), 0xFF);
700 for (i
= 0; i
< 2; i
++) {
701 lm87_write_value(client
, LM87_REG_TEMP_HIGH
[i
], 0x7F);
702 lm87_write_value(client
, LM87_REG_TEMP_LOW
[i
], 0x00);
703 lm87_write_value(client
, LM87_REG_AIN_MIN(i
), 0x00);
704 lm87_write_value(client
, LM87_REG_AIN_MAX(i
), 0xFF);
706 if (data
->channel
& CHAN_TEMP3
) {
707 lm87_write_value(client
, LM87_REG_TEMP_HIGH
[2], 0x7F);
708 lm87_write_value(client
, LM87_REG_TEMP_LOW
[2], 0x00);
710 lm87_write_value(client
, LM87_REG_IN_MIN(0), 0x00);
711 lm87_write_value(client
, LM87_REG_IN_MAX(0), 0xFF);
714 if ((config
& 0x81) != 0x01) {
715 /* Start monitoring */
716 lm87_write_value(client
, LM87_REG_CONFIG
,
717 (config
& 0xF7) | 0x01);
721 static int lm87_detach_client(struct i2c_client
*client
)
725 if ((err
= i2c_detach_client(client
))) {
726 dev_err(&client
->dev
, "Client deregistration failed, "
727 "client not detached.\n");
731 kfree(i2c_get_clientdata(client
));
735 static struct lm87_data
*lm87_update_device(struct device
*dev
)
737 struct i2c_client
*client
= to_i2c_client(dev
);
738 struct lm87_data
*data
= i2c_get_clientdata(client
);
740 down(&data
->update_lock
);
742 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
745 dev_dbg(&client
->dev
, "Updating data.\n");
747 i
= (data
->channel
& CHAN_TEMP3
) ? 1 : 0;
748 j
= (data
->channel
& CHAN_TEMP3
) ? 5 : 6;
750 data
->in
[i
] = lm87_read_value(client
,
752 data
->in_min
[i
] = lm87_read_value(client
,
754 data
->in_max
[i
] = lm87_read_value(client
,
758 for (i
= 0; i
< 2; i
++) {
759 if (data
->channel
& CHAN_NO_FAN(i
)) {
760 data
->in
[6+i
] = lm87_read_value(client
,
762 data
->in_max
[6+i
] = lm87_read_value(client
,
763 LM87_REG_AIN_MAX(i
));
764 data
->in_min
[6+i
] = lm87_read_value(client
,
765 LM87_REG_AIN_MIN(i
));
768 data
->fan
[i
] = lm87_read_value(client
,
770 data
->fan_min
[i
] = lm87_read_value(client
,
771 LM87_REG_FAN_MIN(i
));
775 j
= (data
->channel
& CHAN_TEMP3
) ? 3 : 2;
776 for (i
= 0 ; i
< j
; i
++) {
777 data
->temp
[i
] = lm87_read_value(client
,
779 data
->temp_high
[i
] = lm87_read_value(client
,
780 LM87_REG_TEMP_HIGH
[i
]);
781 data
->temp_low
[i
] = lm87_read_value(client
,
782 LM87_REG_TEMP_LOW
[i
]);
785 i
= lm87_read_value(client
, LM87_REG_TEMP_HW_INT_LOCK
);
786 j
= lm87_read_value(client
, LM87_REG_TEMP_HW_INT
);
787 data
->temp_crit_int
= min(i
, j
);
789 i
= lm87_read_value(client
, LM87_REG_TEMP_HW_EXT_LOCK
);
790 j
= lm87_read_value(client
, LM87_REG_TEMP_HW_EXT
);
791 data
->temp_crit_ext
= min(i
, j
);
793 i
= lm87_read_value(client
, LM87_REG_VID_FAN_DIV
);
794 data
->fan_div
[0] = (i
>> 4) & 0x03;
795 data
->fan_div
[1] = (i
>> 6) & 0x03;
796 data
->vid
= (i
& 0x0F)
797 | (lm87_read_value(client
, LM87_REG_VID4
) & 0x01)
800 data
->alarms
= lm87_read_value(client
, LM87_REG_ALARMS1
)
801 | (lm87_read_value(client
, LM87_REG_ALARMS2
)
803 data
->aout
= lm87_read_value(client
, LM87_REG_AOUT
);
805 data
->last_updated
= jiffies
;
809 up(&data
->update_lock
);
814 static int __init
sensors_lm87_init(void)
816 return i2c_add_driver(&lm87_driver
);
819 static void __exit
sensors_lm87_exit(void)
821 i2c_del_driver(&lm87_driver
);
824 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
825 MODULE_DESCRIPTION("LM87 driver");
826 MODULE_LICENSE("GPL");
828 module_init(sensors_lm87_init
);
829 module_exit(sensors_lm87_exit
);