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>
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 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)
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.
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
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.
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.
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.
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>
72 * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
75 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
77 enum chips { lm87, adm1024 };
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 : \
131 (val) + 500) / 1000))
133 #define FAN_FROM_REG(reg, div) ((reg) == 255 || (reg) == 0 ? 0 : \
134 (1350000 + (reg)*(div) / 2) / ((reg) * (div)))
135 #define FAN_TO_REG(val, div) ((val) * (div) * 255 <= 1350000 ? 255 : \
136 (1350000 + (val)*(div) / 2) / ((val) * (div)))
138 #define FAN_DIV_FROM_REG(reg) (1 << (reg))
140 /* analog out is 9.80mV/LSB */
141 #define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
142 #define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
143 (val) >= 2500 ? 255 : \
144 ((val) * 10 + 49) / 98)
147 #define CHAN_NO_FAN(nr) (1 << (nr))
148 #define CHAN_TEMP3 (1 << 2)
149 #define CHAN_VCC_5V (1 << 3)
150 #define CHAN_NO_VID (1 << 7)
153 * Client data (each client gets its own)
157 struct device *hwmon_dev;
158 struct mutex update_lock;
159 char valid; /* zero until following fields are valid */
160 unsigned long last_updated; /* In jiffies */
162 u8 channel; /* register value */
163 u8 config; /* original register value */
165 u8 in[8]; /* register value */
166 u8 in_max[8]; /* register value */
167 u8 in_min[8]; /* register value */
170 s8 temp[3]; /* register value */
171 s8 temp_high[3]; /* register value */
172 s8 temp_low[3]; /* register value */
173 s8 temp_crit_int; /* min of two register values */
174 s8 temp_crit_ext; /* min of two register values */
176 u8 fan[2]; /* register value */
177 u8 fan_min[2]; /* register value */
178 u8 fan_div[2]; /* register value, shifted right */
179 u8 aout; /* register value */
181 u16 alarms; /* register values, combined */
182 u8 vid; /* register values, combined */
186 static inline int lm87_read_value(struct i2c_client *client, u8 reg)
188 return i2c_smbus_read_byte_data(client, reg);
191 static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
193 return i2c_smbus_write_byte_data(client, reg, value);
196 static struct lm87_data *lm87_update_device(struct device *dev)
198 struct i2c_client *client = to_i2c_client(dev);
199 struct lm87_data *data = i2c_get_clientdata(client);
201 mutex_lock(&data->update_lock);
203 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
206 dev_dbg(&client->dev, "Updating data.\n");
208 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
209 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
211 data->in[i] = lm87_read_value(client,
213 data->in_min[i] = lm87_read_value(client,
215 data->in_max[i] = lm87_read_value(client,
219 for (i = 0; i < 2; i++) {
220 if (data->channel & CHAN_NO_FAN(i)) {
221 data->in[6+i] = lm87_read_value(client,
223 data->in_max[6+i] = lm87_read_value(client,
224 LM87_REG_AIN_MAX(i));
225 data->in_min[6+i] = lm87_read_value(client,
226 LM87_REG_AIN_MIN(i));
229 data->fan[i] = lm87_read_value(client,
231 data->fan_min[i] = lm87_read_value(client,
232 LM87_REG_FAN_MIN(i));
236 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
237 for (i = 0 ; i < j; i++) {
238 data->temp[i] = lm87_read_value(client,
240 data->temp_high[i] = lm87_read_value(client,
241 LM87_REG_TEMP_HIGH[i]);
242 data->temp_low[i] = lm87_read_value(client,
243 LM87_REG_TEMP_LOW[i]);
246 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
247 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
248 data->temp_crit_int = min(i, j);
250 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
251 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
252 data->temp_crit_ext = min(i, j);
254 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
255 data->fan_div[0] = (i >> 4) & 0x03;
256 data->fan_div[1] = (i >> 6) & 0x03;
257 data->vid = (i & 0x0F)
258 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
261 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
262 | (lm87_read_value(client, LM87_REG_ALARMS2)
264 data->aout = lm87_read_value(client, LM87_REG_AOUT);
266 data->last_updated = jiffies;
270 mutex_unlock(&data->update_lock);
279 static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
282 struct lm87_data *data = lm87_update_device(dev);
283 int nr = to_sensor_dev_attr(attr)->index;
285 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[nr],
286 data->in_scale[nr]));
289 static ssize_t show_in_min(struct device *dev,
290 struct device_attribute *attr, char *buf)
292 struct lm87_data *data = lm87_update_device(dev);
293 int nr = to_sensor_dev_attr(attr)->index;
295 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[nr],
296 data->in_scale[nr]));
299 static ssize_t show_in_max(struct device *dev,
300 struct device_attribute *attr, char *buf)
302 struct lm87_data *data = lm87_update_device(dev);
303 int nr = to_sensor_dev_attr(attr)->index;
305 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[nr],
306 data->in_scale[nr]));
309 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
310 const char *buf, size_t count)
312 struct i2c_client *client = to_i2c_client(dev);
313 struct lm87_data *data = i2c_get_clientdata(client);
314 int nr = to_sensor_dev_attr(attr)->index;
318 err = kstrtol(buf, 10, &val);
322 mutex_lock(&data->update_lock);
323 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
324 lm87_write_value(client, nr < 6 ? LM87_REG_IN_MIN(nr) :
325 LM87_REG_AIN_MIN(nr - 6), data->in_min[nr]);
326 mutex_unlock(&data->update_lock);
330 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
331 const char *buf, size_t count)
333 struct i2c_client *client = to_i2c_client(dev);
334 struct lm87_data *data = i2c_get_clientdata(client);
335 int nr = to_sensor_dev_attr(attr)->index;
339 err = kstrtol(buf, 10, &val);
343 mutex_lock(&data->update_lock);
344 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
345 lm87_write_value(client, nr < 6 ? LM87_REG_IN_MAX(nr) :
346 LM87_REG_AIN_MAX(nr - 6), data->in_max[nr]);
347 mutex_unlock(&data->update_lock);
351 #define set_in(offset) \
352 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
353 show_in_input, NULL, offset); \
354 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
355 show_in_min, set_in_min, offset); \
356 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
357 show_in_max, set_in_max, offset)
367 static ssize_t show_temp_input(struct device *dev,
368 struct device_attribute *attr, char *buf)
370 struct lm87_data *data = lm87_update_device(dev);
371 int nr = to_sensor_dev_attr(attr)->index;
373 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
376 static ssize_t show_temp_low(struct device *dev,
377 struct device_attribute *attr, char *buf)
379 struct lm87_data *data = lm87_update_device(dev);
380 int nr = to_sensor_dev_attr(attr)->index;
382 return sprintf(buf, "%d\n",
383 TEMP_FROM_REG(data->temp_low[nr]));
386 static ssize_t show_temp_high(struct device *dev,
387 struct device_attribute *attr, char *buf)
389 struct lm87_data *data = lm87_update_device(dev);
390 int nr = to_sensor_dev_attr(attr)->index;
392 return sprintf(buf, "%d\n",
393 TEMP_FROM_REG(data->temp_high[nr]));
396 static ssize_t set_temp_low(struct device *dev, struct device_attribute *attr,
397 const char *buf, size_t count)
399 struct i2c_client *client = to_i2c_client(dev);
400 struct lm87_data *data = i2c_get_clientdata(client);
401 int nr = to_sensor_dev_attr(attr)->index;
405 err = kstrtol(buf, 10, &val);
409 mutex_lock(&data->update_lock);
410 data->temp_low[nr] = TEMP_TO_REG(val);
411 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
412 mutex_unlock(&data->update_lock);
416 static ssize_t set_temp_high(struct device *dev, struct device_attribute *attr,
417 const char *buf, size_t count)
419 struct i2c_client *client = to_i2c_client(dev);
420 struct lm87_data *data = i2c_get_clientdata(client);
421 int nr = to_sensor_dev_attr(attr)->index;
425 err = kstrtol(buf, 10, &val);
429 mutex_lock(&data->update_lock);
430 data->temp_high[nr] = TEMP_TO_REG(val);
431 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
432 mutex_unlock(&data->update_lock);
436 #define set_temp(offset) \
437 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
438 show_temp_input, NULL, offset - 1); \
439 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
440 show_temp_high, set_temp_high, offset - 1); \
441 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
442 show_temp_low, set_temp_low, offset - 1)
447 static ssize_t show_temp_crit_int(struct device *dev,
448 struct device_attribute *attr, char *buf)
450 struct lm87_data *data = lm87_update_device(dev);
451 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
454 static ssize_t show_temp_crit_ext(struct device *dev,
455 struct device_attribute *attr, char *buf)
457 struct lm87_data *data = lm87_update_device(dev);
458 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
461 static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
462 static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
463 static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
465 static ssize_t show_fan_input(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct lm87_data *data = lm87_update_device(dev);
469 int nr = to_sensor_dev_attr(attr)->index;
471 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
472 FAN_DIV_FROM_REG(data->fan_div[nr])));
475 static ssize_t show_fan_min(struct device *dev,
476 struct device_attribute *attr, char *buf)
478 struct lm87_data *data = lm87_update_device(dev);
479 int nr = to_sensor_dev_attr(attr)->index;
481 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
482 FAN_DIV_FROM_REG(data->fan_div[nr])));
485 static ssize_t show_fan_div(struct device *dev,
486 struct device_attribute *attr, char *buf)
488 struct lm87_data *data = lm87_update_device(dev);
489 int nr = to_sensor_dev_attr(attr)->index;
491 return sprintf(buf, "%d\n",
492 FAN_DIV_FROM_REG(data->fan_div[nr]));
495 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
496 const char *buf, size_t count)
498 struct i2c_client *client = to_i2c_client(dev);
499 struct lm87_data *data = i2c_get_clientdata(client);
500 int nr = to_sensor_dev_attr(attr)->index;
504 err = kstrtol(buf, 10, &val);
508 mutex_lock(&data->update_lock);
509 data->fan_min[nr] = FAN_TO_REG(val,
510 FAN_DIV_FROM_REG(data->fan_div[nr]));
511 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
512 mutex_unlock(&data->update_lock);
517 * Note: we save and restore the fan minimum here, because its value is
518 * determined in part by the fan clock divider. This follows the principle
519 * of least surprise; the user doesn't expect the fan minimum to change just
520 * because the divider changed.
522 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
523 const char *buf, size_t count)
525 struct i2c_client *client = to_i2c_client(dev);
526 struct lm87_data *data = i2c_get_clientdata(client);
527 int nr = to_sensor_dev_attr(attr)->index;
533 err = kstrtol(buf, 10, &val);
537 mutex_lock(&data->update_lock);
538 min = FAN_FROM_REG(data->fan_min[nr],
539 FAN_DIV_FROM_REG(data->fan_div[nr]));
543 data->fan_div[nr] = 0;
546 data->fan_div[nr] = 1;
549 data->fan_div[nr] = 2;
552 data->fan_div[nr] = 3;
555 mutex_unlock(&data->update_lock);
559 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
562 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
565 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
568 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
570 data->fan_min[nr] = FAN_TO_REG(min, val);
571 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
573 mutex_unlock(&data->update_lock);
578 #define set_fan(offset) \
579 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
580 show_fan_input, NULL, offset - 1); \
581 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
582 show_fan_min, set_fan_min, offset - 1); \
583 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
584 show_fan_div, set_fan_div, offset - 1)
588 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
591 struct lm87_data *data = lm87_update_device(dev);
592 return sprintf(buf, "%d\n", data->alarms);
594 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
596 static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
599 struct lm87_data *data = lm87_update_device(dev);
600 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
602 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
604 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
607 struct lm87_data *data = dev_get_drvdata(dev);
608 return sprintf(buf, "%d\n", data->vrm);
610 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
611 const char *buf, size_t count)
613 struct lm87_data *data = dev_get_drvdata(dev);
617 err = kstrtoul(buf, 10, &val);
623 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
625 static ssize_t show_aout(struct device *dev, struct device_attribute *attr,
628 struct lm87_data *data = lm87_update_device(dev);
629 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
631 static ssize_t set_aout(struct device *dev, struct device_attribute *attr,
632 const char *buf, size_t count)
634 struct i2c_client *client = to_i2c_client(dev);
635 struct lm87_data *data = i2c_get_clientdata(client);
639 err = kstrtol(buf, 10, &val);
643 mutex_lock(&data->update_lock);
644 data->aout = AOUT_TO_REG(val);
645 lm87_write_value(client, LM87_REG_AOUT, data->aout);
646 mutex_unlock(&data->update_lock);
649 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
651 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
654 struct lm87_data *data = lm87_update_device(dev);
655 int bitnr = to_sensor_dev_attr(attr)->index;
656 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
658 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
659 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
660 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
661 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
662 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
663 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
664 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
665 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
666 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
667 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
668 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
669 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
670 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
671 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
672 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
678 static struct attribute *lm87_attributes[] = {
679 &sensor_dev_attr_in1_input.dev_attr.attr,
680 &sensor_dev_attr_in1_min.dev_attr.attr,
681 &sensor_dev_attr_in1_max.dev_attr.attr,
682 &sensor_dev_attr_in1_alarm.dev_attr.attr,
683 &sensor_dev_attr_in2_input.dev_attr.attr,
684 &sensor_dev_attr_in2_min.dev_attr.attr,
685 &sensor_dev_attr_in2_max.dev_attr.attr,
686 &sensor_dev_attr_in2_alarm.dev_attr.attr,
687 &sensor_dev_attr_in3_input.dev_attr.attr,
688 &sensor_dev_attr_in3_min.dev_attr.attr,
689 &sensor_dev_attr_in3_max.dev_attr.attr,
690 &sensor_dev_attr_in3_alarm.dev_attr.attr,
691 &sensor_dev_attr_in4_input.dev_attr.attr,
692 &sensor_dev_attr_in4_min.dev_attr.attr,
693 &sensor_dev_attr_in4_max.dev_attr.attr,
694 &sensor_dev_attr_in4_alarm.dev_attr.attr,
696 &sensor_dev_attr_temp1_input.dev_attr.attr,
697 &sensor_dev_attr_temp1_max.dev_attr.attr,
698 &sensor_dev_attr_temp1_min.dev_attr.attr,
699 &dev_attr_temp1_crit.attr,
700 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
701 &sensor_dev_attr_temp2_input.dev_attr.attr,
702 &sensor_dev_attr_temp2_max.dev_attr.attr,
703 &sensor_dev_attr_temp2_min.dev_attr.attr,
704 &dev_attr_temp2_crit.attr,
705 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
706 &sensor_dev_attr_temp2_fault.dev_attr.attr,
708 &dev_attr_alarms.attr,
709 &dev_attr_aout_output.attr,
714 static const struct attribute_group lm87_group = {
715 .attrs = lm87_attributes,
718 static struct attribute *lm87_attributes_in6[] = {
719 &sensor_dev_attr_in6_input.dev_attr.attr,
720 &sensor_dev_attr_in6_min.dev_attr.attr,
721 &sensor_dev_attr_in6_max.dev_attr.attr,
722 &sensor_dev_attr_in6_alarm.dev_attr.attr,
726 static const struct attribute_group lm87_group_in6 = {
727 .attrs = lm87_attributes_in6,
730 static struct attribute *lm87_attributes_fan1[] = {
731 &sensor_dev_attr_fan1_input.dev_attr.attr,
732 &sensor_dev_attr_fan1_min.dev_attr.attr,
733 &sensor_dev_attr_fan1_div.dev_attr.attr,
734 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
738 static const struct attribute_group lm87_group_fan1 = {
739 .attrs = lm87_attributes_fan1,
742 static struct attribute *lm87_attributes_in7[] = {
743 &sensor_dev_attr_in7_input.dev_attr.attr,
744 &sensor_dev_attr_in7_min.dev_attr.attr,
745 &sensor_dev_attr_in7_max.dev_attr.attr,
746 &sensor_dev_attr_in7_alarm.dev_attr.attr,
750 static const struct attribute_group lm87_group_in7 = {
751 .attrs = lm87_attributes_in7,
754 static struct attribute *lm87_attributes_fan2[] = {
755 &sensor_dev_attr_fan2_input.dev_attr.attr,
756 &sensor_dev_attr_fan2_min.dev_attr.attr,
757 &sensor_dev_attr_fan2_div.dev_attr.attr,
758 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
762 static const struct attribute_group lm87_group_fan2 = {
763 .attrs = lm87_attributes_fan2,
766 static struct attribute *lm87_attributes_temp3[] = {
767 &sensor_dev_attr_temp3_input.dev_attr.attr,
768 &sensor_dev_attr_temp3_max.dev_attr.attr,
769 &sensor_dev_attr_temp3_min.dev_attr.attr,
770 &dev_attr_temp3_crit.attr,
771 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
772 &sensor_dev_attr_temp3_fault.dev_attr.attr,
776 static const struct attribute_group lm87_group_temp3 = {
777 .attrs = lm87_attributes_temp3,
780 static struct attribute *lm87_attributes_in0_5[] = {
781 &sensor_dev_attr_in0_input.dev_attr.attr,
782 &sensor_dev_attr_in0_min.dev_attr.attr,
783 &sensor_dev_attr_in0_max.dev_attr.attr,
784 &sensor_dev_attr_in0_alarm.dev_attr.attr,
785 &sensor_dev_attr_in5_input.dev_attr.attr,
786 &sensor_dev_attr_in5_min.dev_attr.attr,
787 &sensor_dev_attr_in5_max.dev_attr.attr,
788 &sensor_dev_attr_in5_alarm.dev_attr.attr,
792 static const struct attribute_group lm87_group_in0_5 = {
793 .attrs = lm87_attributes_in0_5,
796 static struct attribute *lm87_attributes_vid[] = {
797 &dev_attr_cpu0_vid.attr,
802 static const struct attribute_group lm87_group_vid = {
803 .attrs = lm87_attributes_vid,
806 /* Return 0 if detection is successful, -ENODEV otherwise */
807 static int lm87_detect(struct i2c_client *client, struct i2c_board_info *info)
809 struct i2c_adapter *adapter = client->adapter;
813 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
816 if (lm87_read_value(client, LM87_REG_CONFIG) & 0x80)
819 /* Now, we do the remaining detection. */
820 cid = lm87_read_value(client, LM87_REG_COMPANY_ID);
821 rev = lm87_read_value(client, LM87_REG_REVISION);
823 if (cid == 0x02 /* National Semiconductor */
824 && (rev >= 0x01 && rev <= 0x08))
826 else if (cid == 0x41 /* Analog Devices */
827 && (rev & 0xf0) == 0x10)
830 dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n",
835 strlcpy(info->type, name, I2C_NAME_SIZE);
840 static void lm87_remove_files(struct i2c_client *client)
842 struct device *dev = &client->dev;
844 sysfs_remove_group(&dev->kobj, &lm87_group);
845 sysfs_remove_group(&dev->kobj, &lm87_group_in6);
846 sysfs_remove_group(&dev->kobj, &lm87_group_fan1);
847 sysfs_remove_group(&dev->kobj, &lm87_group_in7);
848 sysfs_remove_group(&dev->kobj, &lm87_group_fan2);
849 sysfs_remove_group(&dev->kobj, &lm87_group_temp3);
850 sysfs_remove_group(&dev->kobj, &lm87_group_in0_5);
851 sysfs_remove_group(&dev->kobj, &lm87_group_vid);
854 static void lm87_init_client(struct i2c_client *client)
856 struct lm87_data *data = i2c_get_clientdata(client);
858 if (client->dev.platform_data) {
859 data->channel = *(u8 *)client->dev.platform_data;
860 lm87_write_value(client,
861 LM87_REG_CHANNEL_MODE, data->channel);
863 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
865 data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F;
867 if (!(data->config & 0x01)) {
870 /* Limits are left uninitialized after power-up */
871 for (i = 1; i < 6; i++) {
872 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
873 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
875 for (i = 0; i < 2; i++) {
876 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
877 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
878 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
879 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
881 if (data->channel & CHAN_TEMP3) {
882 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
883 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
885 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
886 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
890 /* Make sure Start is set and INT#_Clear is clear */
891 if ((data->config & 0x09) != 0x01)
892 lm87_write_value(client, LM87_REG_CONFIG,
893 (data->config & 0x77) | 0x01);
896 static int lm87_probe(struct i2c_client *client, const struct i2c_device_id *id)
898 struct lm87_data *data;
901 data = devm_kzalloc(&client->dev, sizeof(struct lm87_data), GFP_KERNEL);
905 i2c_set_clientdata(client, data);
907 mutex_init(&data->update_lock);
909 /* Initialize the LM87 chip */
910 lm87_init_client(client);
912 data->in_scale[0] = 2500;
913 data->in_scale[1] = 2700;
914 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
915 data->in_scale[3] = 5000;
916 data->in_scale[4] = 12000;
917 data->in_scale[5] = 2700;
918 data->in_scale[6] = 1875;
919 data->in_scale[7] = 1875;
921 /* Register sysfs hooks */
922 err = sysfs_create_group(&client->dev.kobj, &lm87_group);
926 if (data->channel & CHAN_NO_FAN(0)) {
927 err = sysfs_create_group(&client->dev.kobj, &lm87_group_in6);
931 err = sysfs_create_group(&client->dev.kobj, &lm87_group_fan1);
936 if (data->channel & CHAN_NO_FAN(1)) {
937 err = sysfs_create_group(&client->dev.kobj, &lm87_group_in7);
941 err = sysfs_create_group(&client->dev.kobj, &lm87_group_fan2);
946 if (data->channel & CHAN_TEMP3) {
947 err = sysfs_create_group(&client->dev.kobj, &lm87_group_temp3);
951 err = sysfs_create_group(&client->dev.kobj, &lm87_group_in0_5);
956 if (!(data->channel & CHAN_NO_VID)) {
957 data->vrm = vid_which_vrm();
958 err = sysfs_create_group(&client->dev.kobj, &lm87_group_vid);
963 data->hwmon_dev = hwmon_device_register(&client->dev);
964 if (IS_ERR(data->hwmon_dev)) {
965 err = PTR_ERR(data->hwmon_dev);
972 lm87_remove_files(client);
974 lm87_write_value(client, LM87_REG_CONFIG, data->config);
978 static int lm87_remove(struct i2c_client *client)
980 struct lm87_data *data = i2c_get_clientdata(client);
982 hwmon_device_unregister(data->hwmon_dev);
983 lm87_remove_files(client);
985 lm87_write_value(client, LM87_REG_CONFIG, data->config);
990 * Driver data (common to all clients)
993 static const struct i2c_device_id lm87_id[] = {
995 { "adm1024", adm1024 },
998 MODULE_DEVICE_TABLE(i2c, lm87_id);
1000 static struct i2c_driver lm87_driver = {
1001 .class = I2C_CLASS_HWMON,
1005 .probe = lm87_probe,
1006 .remove = lm87_remove,
1007 .id_table = lm87_id,
1008 .detect = lm87_detect,
1009 .address_list = normal_i2c,
1012 module_i2c_driver(lm87_driver);
1014 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
1015 MODULE_DESCRIPTION("LM87 driver");
1016 MODULE_LICENSE("GPL");