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/module.h>
56 #include <linux/init.h>
57 #include <linux/slab.h>
58 #include <linux/jiffies.h>
59 #include <linux/i2c.h>
60 #include <linux/i2c-sensor.h>
61 #include <linux/i2c-vid.h>
62 #include <linux/hwmon.h>
63 #include <linux/err.h>
67 * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
70 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
71 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
77 SENSORS_INSMOD_1(lm87);
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)
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))
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 };
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
102 #define LM87_REG_FAN(nr) (0x28 + (nr))
103 #define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
104 #define LM87_REG_AOUT 0x19
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
111 #define LM87_REG_ALARMS1 0x41
112 #define LM87_REG_ALARMS2 0x42
114 #define LM87_REG_COMPANY_ID 0x3E
115 #define LM87_REG_REVISION 0x3F
118 * Conversions and various macros
119 * The LM87 uses signed 8-bit values for temperatures.
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))
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))
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)))
137 #define FAN_DIV_FROM_REG(reg) (1 << (reg))
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)
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 << 8)
152 * Functions declaration
155 static int lm87_attach_adapter(struct i2c_adapter *adapter);
156 static int lm87_detect(struct i2c_adapter *adapter, int address, int kind);
157 static void lm87_init_client(struct i2c_client *client);
158 static int lm87_detach_client(struct i2c_client *client);
159 static struct lm87_data *lm87_update_device(struct device *dev);
162 * Driver data (common to all clients)
165 static struct i2c_driver lm87_driver = {
166 .owner = THIS_MODULE,
168 .id = I2C_DRIVERID_LM87,
169 .flags = I2C_DF_NOTIFY,
170 .attach_adapter = lm87_attach_adapter,
171 .detach_client = lm87_detach_client,
175 * Client data (each client gets its own)
179 struct i2c_client client;
180 struct class_device *class_dev;
181 struct semaphore update_lock;
182 char valid; /* zero until following fields are valid */
183 unsigned long last_updated; /* In jiffies */
185 u8 channel; /* register value */
187 u8 in[8]; /* register value */
188 u8 in_max[8]; /* register value */
189 u8 in_min[8]; /* register value */
192 s8 temp[3]; /* register value */
193 s8 temp_high[3]; /* register value */
194 s8 temp_low[3]; /* register value */
195 s8 temp_crit_int; /* min of two register values */
196 s8 temp_crit_ext; /* min of two register values */
198 u8 fan[2]; /* register value */
199 u8 fan_min[2]; /* register value */
200 u8 fan_div[2]; /* register value, shifted right */
201 u8 aout; /* register value */
203 u16 alarms; /* register values, combined */
204 u8 vid; /* register values, combined */
212 static inline int lm87_read_value(struct i2c_client *client, u8 reg)
214 return i2c_smbus_read_byte_data(client, reg);
217 static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
219 return i2c_smbus_write_byte_data(client, reg, value);
222 #define show_in(offset) \
223 static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
225 struct lm87_data *data = lm87_update_device(dev); \
226 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
227 data->in_scale[offset])); \
229 static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
231 struct lm87_data *data = lm87_update_device(dev); \
232 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
233 data->in_scale[offset])); \
235 static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
237 struct lm87_data *data = lm87_update_device(dev); \
238 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
239 data->in_scale[offset])); \
241 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
242 show_in##offset##_input, NULL);
252 static void set_in_min(struct device *dev, const char *buf, int nr)
254 struct i2c_client *client = to_i2c_client(dev);
255 struct lm87_data *data = i2c_get_clientdata(client);
256 long val = simple_strtol(buf, NULL, 10);
258 down(&data->update_lock);
259 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
260 lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
261 LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
262 up(&data->update_lock);
265 static void set_in_max(struct device *dev, const char *buf, int nr)
267 struct i2c_client *client = to_i2c_client(dev);
268 struct lm87_data *data = i2c_get_clientdata(client);
269 long val = simple_strtol(buf, NULL, 10);
271 down(&data->update_lock);
272 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
273 lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
274 LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
275 up(&data->update_lock);
278 #define set_in(offset) \
279 static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, \
280 const char *buf, size_t count) \
282 set_in_min(dev, buf, offset); \
285 static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, \
286 const char *buf, size_t count) \
288 set_in_max(dev, buf, offset); \
291 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
292 show_in##offset##_min, set_in##offset##_min); \
293 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
294 show_in##offset##_max, set_in##offset##_max);
304 #define show_temp(offset) \
305 static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
307 struct lm87_data *data = lm87_update_device(dev); \
308 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
310 static ssize_t show_temp##offset##_low(struct device *dev, struct device_attribute *attr, char *buf) \
312 struct lm87_data *data = lm87_update_device(dev); \
313 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
315 static ssize_t show_temp##offset##_high(struct device *dev, struct device_attribute *attr, char *buf) \
317 struct lm87_data *data = lm87_update_device(dev); \
318 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
320 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
321 show_temp##offset##_input, NULL);
326 static void set_temp_low(struct device *dev, const char *buf, int nr)
328 struct i2c_client *client = to_i2c_client(dev);
329 struct lm87_data *data = i2c_get_clientdata(client);
330 long val = simple_strtol(buf, NULL, 10);
332 down(&data->update_lock);
333 data->temp_low[nr] = TEMP_TO_REG(val);
334 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
335 up(&data->update_lock);
338 static void set_temp_high(struct device *dev, const char *buf, int nr)
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);
344 down(&data->update_lock);
345 data->temp_high[nr] = TEMP_TO_REG(val);
346 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
347 up(&data->update_lock);
350 #define set_temp(offset) \
351 static ssize_t set_temp##offset##_low(struct device *dev, struct device_attribute *attr, \
352 const char *buf, size_t count) \
354 set_temp_low(dev, buf, offset-1); \
357 static ssize_t set_temp##offset##_high(struct device *dev, struct device_attribute *attr, \
358 const char *buf, size_t count) \
360 set_temp_high(dev, buf, offset-1); \
363 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
364 show_temp##offset##_high, set_temp##offset##_high); \
365 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
366 show_temp##offset##_low, set_temp##offset##_low);
371 static ssize_t show_temp_crit_int(struct device *dev, struct device_attribute *attr, char *buf)
373 struct lm87_data *data = lm87_update_device(dev);
374 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
377 static ssize_t show_temp_crit_ext(struct device *dev, struct device_attribute *attr, char *buf)
379 struct lm87_data *data = lm87_update_device(dev);
380 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
383 static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
384 static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
385 static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
387 #define show_fan(offset) \
388 static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
390 struct lm87_data *data = lm87_update_device(dev); \
391 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \
392 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
394 static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
396 struct lm87_data *data = lm87_update_device(dev); \
397 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \
398 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
400 static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
402 struct lm87_data *data = lm87_update_device(dev); \
403 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
405 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
406 show_fan##offset##_input, NULL);
410 static void set_fan_min(struct device *dev, const char *buf, int nr)
412 struct i2c_client *client = to_i2c_client(dev);
413 struct lm87_data *data = i2c_get_clientdata(client);
414 long val = simple_strtol(buf, NULL, 10);
416 down(&data->update_lock);
417 data->fan_min[nr] = FAN_TO_REG(val,
418 FAN_DIV_FROM_REG(data->fan_div[nr]));
419 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
420 up(&data->update_lock);
423 /* Note: we save and restore the fan minimum here, because its value is
424 determined in part by the fan clock divider. This follows the principle
425 of least suprise; the user doesn't expect the fan minimum to change just
426 because the divider changed. */
427 static ssize_t set_fan_div(struct device *dev, const char *buf,
428 size_t count, int nr)
430 struct i2c_client *client = to_i2c_client(dev);
431 struct lm87_data *data = i2c_get_clientdata(client);
432 long val = simple_strtol(buf, NULL, 10);
436 down(&data->update_lock);
437 min = FAN_FROM_REG(data->fan_min[nr],
438 FAN_DIV_FROM_REG(data->fan_div[nr]));
441 case 1: data->fan_div[nr] = 0; break;
442 case 2: data->fan_div[nr] = 1; break;
443 case 4: data->fan_div[nr] = 2; break;
444 case 8: data->fan_div[nr] = 3; break;
446 up(&data->update_lock);
450 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
453 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
456 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
459 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
461 data->fan_min[nr] = FAN_TO_REG(min, val);
462 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
464 up(&data->update_lock);
469 #define set_fan(offset) \
470 static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
473 set_fan_min(dev, buf, offset-1); \
476 static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
479 return set_fan_div(dev, buf, count, offset-1); \
481 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
482 show_fan##offset##_min, set_fan##offset##_min); \
483 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
484 show_fan##offset##_div, set_fan##offset##_div);
488 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
490 struct lm87_data *data = lm87_update_device(dev);
491 return sprintf(buf, "%d\n", data->alarms);
493 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
495 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
497 struct lm87_data *data = lm87_update_device(dev);
498 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
500 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
502 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
504 struct lm87_data *data = lm87_update_device(dev);
505 return sprintf(buf, "%d\n", data->vrm);
507 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
509 struct i2c_client *client = to_i2c_client(dev);
510 struct lm87_data *data = i2c_get_clientdata(client);
511 data->vrm = simple_strtoul(buf, NULL, 10);
514 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
516 static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
518 struct lm87_data *data = lm87_update_device(dev);
519 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
521 static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
523 struct i2c_client *client = to_i2c_client(dev);
524 struct lm87_data *data = i2c_get_clientdata(client);
525 long val = simple_strtol(buf, NULL, 10);
527 down(&data->update_lock);
528 data->aout = AOUT_TO_REG(val);
529 lm87_write_value(client, LM87_REG_AOUT, data->aout);
530 up(&data->update_lock);
533 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
539 static int lm87_attach_adapter(struct i2c_adapter *adapter)
541 if (!(adapter->class & I2C_CLASS_HWMON))
543 return i2c_detect(adapter, &addr_data, lm87_detect);
547 * The following function does more than just detection. If detection
548 * succeeds, it also registers the new chip.
550 static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
552 struct i2c_client *new_client;
553 struct lm87_data *data;
556 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
559 if (!(data = kmalloc(sizeof(struct lm87_data), GFP_KERNEL))) {
563 memset(data, 0, sizeof(struct lm87_data));
565 /* The common I2C client data is placed right before the
566 LM87-specific data. */
567 new_client = &data->client;
568 i2c_set_clientdata(new_client, data);
569 new_client->addr = address;
570 new_client->adapter = adapter;
571 new_client->driver = &lm87_driver;
572 new_client->flags = 0;
574 /* Default to an LM87 if forced */
578 /* Now, we do the remaining detection. */
580 u8 rev = lm87_read_value(new_client, LM87_REG_REVISION);
582 if (rev < 0x01 || rev > 0x08
583 || (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)
584 || lm87_read_value(new_client, LM87_REG_COMPANY_ID) != 0x02) {
585 dev_dbg(&adapter->dev,
586 "LM87 detection failed at 0x%02x.\n",
592 /* We can fill in the remaining client fields */
593 strlcpy(new_client->name, "lm87", I2C_NAME_SIZE);
595 init_MUTEX(&data->update_lock);
597 /* Tell the I2C layer a new client has arrived */
598 if ((err = i2c_attach_client(new_client)))
601 /* Initialize the LM87 chip */
602 lm87_init_client(new_client);
604 data->in_scale[0] = 2500;
605 data->in_scale[1] = 2700;
606 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
607 data->in_scale[3] = 5000;
608 data->in_scale[4] = 12000;
609 data->in_scale[5] = 2700;
610 data->in_scale[6] = 1875;
611 data->in_scale[7] = 1875;
613 /* Register sysfs hooks */
614 data->class_dev = hwmon_device_register(&new_client->dev);
615 if (IS_ERR(data->class_dev)) {
616 err = PTR_ERR(data->class_dev);
620 device_create_file(&new_client->dev, &dev_attr_in1_input);
621 device_create_file(&new_client->dev, &dev_attr_in1_min);
622 device_create_file(&new_client->dev, &dev_attr_in1_max);
623 device_create_file(&new_client->dev, &dev_attr_in2_input);
624 device_create_file(&new_client->dev, &dev_attr_in2_min);
625 device_create_file(&new_client->dev, &dev_attr_in2_max);
626 device_create_file(&new_client->dev, &dev_attr_in3_input);
627 device_create_file(&new_client->dev, &dev_attr_in3_min);
628 device_create_file(&new_client->dev, &dev_attr_in3_max);
629 device_create_file(&new_client->dev, &dev_attr_in4_input);
630 device_create_file(&new_client->dev, &dev_attr_in4_min);
631 device_create_file(&new_client->dev, &dev_attr_in4_max);
633 if (data->channel & CHAN_NO_FAN(0)) {
634 device_create_file(&new_client->dev, &dev_attr_in6_input);
635 device_create_file(&new_client->dev, &dev_attr_in6_min);
636 device_create_file(&new_client->dev, &dev_attr_in6_max);
638 device_create_file(&new_client->dev, &dev_attr_fan1_input);
639 device_create_file(&new_client->dev, &dev_attr_fan1_min);
640 device_create_file(&new_client->dev, &dev_attr_fan1_div);
642 if (data->channel & CHAN_NO_FAN(1)) {
643 device_create_file(&new_client->dev, &dev_attr_in7_input);
644 device_create_file(&new_client->dev, &dev_attr_in7_min);
645 device_create_file(&new_client->dev, &dev_attr_in7_max);
647 device_create_file(&new_client->dev, &dev_attr_fan2_input);
648 device_create_file(&new_client->dev, &dev_attr_fan2_min);
649 device_create_file(&new_client->dev, &dev_attr_fan2_div);
652 device_create_file(&new_client->dev, &dev_attr_temp1_input);
653 device_create_file(&new_client->dev, &dev_attr_temp1_max);
654 device_create_file(&new_client->dev, &dev_attr_temp1_min);
655 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
656 device_create_file(&new_client->dev, &dev_attr_temp2_input);
657 device_create_file(&new_client->dev, &dev_attr_temp2_max);
658 device_create_file(&new_client->dev, &dev_attr_temp2_min);
659 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
661 if (data->channel & CHAN_TEMP3) {
662 device_create_file(&new_client->dev, &dev_attr_temp3_input);
663 device_create_file(&new_client->dev, &dev_attr_temp3_max);
664 device_create_file(&new_client->dev, &dev_attr_temp3_min);
665 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
667 device_create_file(&new_client->dev, &dev_attr_in0_input);
668 device_create_file(&new_client->dev, &dev_attr_in0_min);
669 device_create_file(&new_client->dev, &dev_attr_in0_max);
670 device_create_file(&new_client->dev, &dev_attr_in5_input);
671 device_create_file(&new_client->dev, &dev_attr_in5_min);
672 device_create_file(&new_client->dev, &dev_attr_in5_max);
675 if (!(data->channel & CHAN_NO_VID)) {
676 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
677 device_create_file(&new_client->dev, &dev_attr_vrm);
680 device_create_file(&new_client->dev, &dev_attr_alarms);
681 device_create_file(&new_client->dev, &dev_attr_aout_output);
686 i2c_detach_client(new_client);
693 static void lm87_init_client(struct i2c_client *client)
695 struct lm87_data *data = i2c_get_clientdata(client);
698 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
699 data->vrm = i2c_which_vrm();
701 config = lm87_read_value(client, LM87_REG_CONFIG);
702 if (!(config & 0x01)) {
705 /* Limits are left uninitialized after power-up */
706 for (i = 1; i < 6; i++) {
707 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
708 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
710 for (i = 0; i < 2; i++) {
711 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
712 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
713 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
714 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
716 if (data->channel & CHAN_TEMP3) {
717 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
718 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
720 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
721 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
724 if ((config & 0x81) != 0x01) {
725 /* Start monitoring */
726 lm87_write_value(client, LM87_REG_CONFIG,
727 (config & 0xF7) | 0x01);
731 static int lm87_detach_client(struct i2c_client *client)
733 struct lm87_data *data = i2c_get_clientdata(client);
736 hwmon_device_unregister(data->class_dev);
738 if ((err = i2c_detach_client(client))) {
739 dev_err(&client->dev, "Client deregistration failed, "
740 "client not detached.\n");
748 static struct lm87_data *lm87_update_device(struct device *dev)
750 struct i2c_client *client = to_i2c_client(dev);
751 struct lm87_data *data = i2c_get_clientdata(client);
753 down(&data->update_lock);
755 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
758 dev_dbg(&client->dev, "Updating data.\n");
760 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
761 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
763 data->in[i] = lm87_read_value(client,
765 data->in_min[i] = lm87_read_value(client,
767 data->in_max[i] = lm87_read_value(client,
771 for (i = 0; i < 2; i++) {
772 if (data->channel & CHAN_NO_FAN(i)) {
773 data->in[6+i] = lm87_read_value(client,
775 data->in_max[6+i] = lm87_read_value(client,
776 LM87_REG_AIN_MAX(i));
777 data->in_min[6+i] = lm87_read_value(client,
778 LM87_REG_AIN_MIN(i));
781 data->fan[i] = lm87_read_value(client,
783 data->fan_min[i] = lm87_read_value(client,
784 LM87_REG_FAN_MIN(i));
788 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
789 for (i = 0 ; i < j; i++) {
790 data->temp[i] = lm87_read_value(client,
792 data->temp_high[i] = lm87_read_value(client,
793 LM87_REG_TEMP_HIGH[i]);
794 data->temp_low[i] = lm87_read_value(client,
795 LM87_REG_TEMP_LOW[i]);
798 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
799 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
800 data->temp_crit_int = min(i, j);
802 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
803 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
804 data->temp_crit_ext = min(i, j);
806 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
807 data->fan_div[0] = (i >> 4) & 0x03;
808 data->fan_div[1] = (i >> 6) & 0x03;
809 data->vid = (i & 0x0F)
810 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
813 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
814 | (lm87_read_value(client, LM87_REG_ALARMS2)
816 data->aout = lm87_read_value(client, LM87_REG_AOUT);
818 data->last_updated = jiffies;
822 up(&data->update_lock);
827 static int __init sensors_lm87_init(void)
829 return i2c_add_driver(&lm87_driver);
832 static void __exit sensors_lm87_exit(void)
834 i2c_del_driver(&lm87_driver);
837 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
838 MODULE_DESCRIPTION("LM87 driver");
839 MODULE_LICENSE("GPL");
841 module_init(sensors_lm87_init);
842 module_exit(sensors_lm87_exit);