perf buildid: Fix possible unterminated readlink() result buffer
[linux-2.6/linux-mips.git] / drivers / hwmon / lm87.c
blobf1e6e7512ffa2e8f308b3de9483b943507d19913
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-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>
71 * Addresses to scan
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 };
80 * The LM87 registers
83 /* nr in 0..5 */
84 #define LM87_REG_IN(nr) (0x20 + (nr))
85 #define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
86 #define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
87 /* nr in 0..1 */
88 #define LM87_REG_AIN(nr) (0x28 + (nr))
89 #define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
90 #define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
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
101 /* nr in 0..1 */
102 #define LM87_REG_FAN(nr) (0x28 + (nr))
103 #define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
104 #define LM87_REG_AOUT 0x19
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)
145 /* nr in 0..1 */
146 #define CHAN_NO_FAN(nr) (1 << (nr))
147 #define CHAN_TEMP3 (1 << 2)
148 #define CHAN_VCC_5V (1 << 3)
149 #define CHAN_NO_VID (1 << 7)
152 * Functions declaration
155 static int lm87_probe(struct i2c_client *client,
156 const struct i2c_device_id *id);
157 static int lm87_detect(struct i2c_client *new_client,
158 struct i2c_board_info *info);
159 static void lm87_init_client(struct i2c_client *client);
160 static int lm87_remove(struct i2c_client *client);
161 static struct lm87_data *lm87_update_device(struct device *dev);
164 * Driver data (common to all clients)
167 static const struct i2c_device_id lm87_id[] = {
168 { "lm87", lm87 },
169 { "adm1024", adm1024 },
172 MODULE_DEVICE_TABLE(i2c, lm87_id);
174 static struct i2c_driver lm87_driver = {
175 .class = I2C_CLASS_HWMON,
176 .driver = {
177 .name = "lm87",
179 .probe = lm87_probe,
180 .remove = lm87_remove,
181 .id_table = lm87_id,
182 .detect = lm87_detect,
183 .address_list = normal_i2c,
187 * Client data (each client gets its own)
190 struct lm87_data {
191 struct device *hwmon_dev;
192 struct mutex update_lock;
193 char valid; /* zero until following fields are valid */
194 unsigned long last_updated; /* In jiffies */
196 u8 channel; /* register value */
197 u8 config; /* original register value */
199 u8 in[8]; /* register value */
200 u8 in_max[8]; /* register value */
201 u8 in_min[8]; /* register value */
202 u16 in_scale[8];
204 s8 temp[3]; /* register value */
205 s8 temp_high[3]; /* register value */
206 s8 temp_low[3]; /* register value */
207 s8 temp_crit_int; /* min of two register values */
208 s8 temp_crit_ext; /* min of two register values */
210 u8 fan[2]; /* register value */
211 u8 fan_min[2]; /* register value */
212 u8 fan_div[2]; /* register value, shifted right */
213 u8 aout; /* register value */
215 u16 alarms; /* register values, combined */
216 u8 vid; /* register values, combined */
217 u8 vrm;
221 * Sysfs stuff
224 static inline int lm87_read_value(struct i2c_client *client, u8 reg)
226 return i2c_smbus_read_byte_data(client, reg);
229 static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
231 return i2c_smbus_write_byte_data(client, reg, value);
234 #define show_in(offset) \
235 static ssize_t show_in##offset##_input(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[offset], \
239 data->in_scale[offset])); \
241 static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
243 struct lm87_data *data = lm87_update_device(dev); \
244 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
245 data->in_scale[offset])); \
247 static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
249 struct lm87_data *data = lm87_update_device(dev); \
250 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
251 data->in_scale[offset])); \
253 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
254 show_in##offset##_input, NULL);
255 show_in(0);
256 show_in(1);
257 show_in(2);
258 show_in(3);
259 show_in(4);
260 show_in(5);
261 show_in(6);
262 show_in(7);
264 static void set_in_min(struct device *dev, const char *buf, int nr)
266 struct i2c_client *client = to_i2c_client(dev);
267 struct lm87_data *data = i2c_get_clientdata(client);
268 long val = simple_strtol(buf, NULL, 10);
270 mutex_lock(&data->update_lock);
271 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
272 lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
273 LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
274 mutex_unlock(&data->update_lock);
277 static void set_in_max(struct device *dev, const char *buf, int nr)
279 struct i2c_client *client = to_i2c_client(dev);
280 struct lm87_data *data = i2c_get_clientdata(client);
281 long val = simple_strtol(buf, NULL, 10);
283 mutex_lock(&data->update_lock);
284 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
285 lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
286 LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
287 mutex_unlock(&data->update_lock);
290 #define set_in(offset) \
291 static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, \
292 const char *buf, size_t count) \
294 set_in_min(dev, buf, offset); \
295 return count; \
297 static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, \
298 const char *buf, size_t count) \
300 set_in_max(dev, buf, offset); \
301 return count; \
303 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
304 show_in##offset##_min, set_in##offset##_min); \
305 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
306 show_in##offset##_max, set_in##offset##_max);
307 set_in(0);
308 set_in(1);
309 set_in(2);
310 set_in(3);
311 set_in(4);
312 set_in(5);
313 set_in(6);
314 set_in(7);
316 #define show_temp(offset) \
317 static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
319 struct lm87_data *data = lm87_update_device(dev); \
320 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
322 static ssize_t show_temp##offset##_low(struct device *dev, struct device_attribute *attr, char *buf) \
324 struct lm87_data *data = lm87_update_device(dev); \
325 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
327 static ssize_t show_temp##offset##_high(struct device *dev, struct device_attribute *attr, char *buf) \
329 struct lm87_data *data = lm87_update_device(dev); \
330 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
332 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
333 show_temp##offset##_input, NULL);
334 show_temp(1);
335 show_temp(2);
336 show_temp(3);
338 static void set_temp_low(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 mutex_lock(&data->update_lock);
345 data->temp_low[nr] = TEMP_TO_REG(val);
346 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
347 mutex_unlock(&data->update_lock);
350 static void set_temp_high(struct device *dev, const char *buf, int nr)
352 struct i2c_client *client = to_i2c_client(dev);
353 struct lm87_data *data = i2c_get_clientdata(client);
354 long val = simple_strtol(buf, NULL, 10);
356 mutex_lock(&data->update_lock);
357 data->temp_high[nr] = TEMP_TO_REG(val);
358 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
359 mutex_unlock(&data->update_lock);
362 #define set_temp(offset) \
363 static ssize_t set_temp##offset##_low(struct device *dev, struct device_attribute *attr, \
364 const char *buf, size_t count) \
366 set_temp_low(dev, buf, offset-1); \
367 return count; \
369 static ssize_t set_temp##offset##_high(struct device *dev, struct device_attribute *attr, \
370 const char *buf, size_t count) \
372 set_temp_high(dev, buf, offset-1); \
373 return count; \
375 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
376 show_temp##offset##_high, set_temp##offset##_high); \
377 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
378 show_temp##offset##_low, set_temp##offset##_low);
379 set_temp(1);
380 set_temp(2);
381 set_temp(3);
383 static ssize_t show_temp_crit_int(struct device *dev, struct device_attribute *attr, char *buf)
385 struct lm87_data *data = lm87_update_device(dev);
386 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
389 static ssize_t show_temp_crit_ext(struct device *dev, struct device_attribute *attr, char *buf)
391 struct lm87_data *data = lm87_update_device(dev);
392 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
395 static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
396 static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
397 static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
399 #define show_fan(offset) \
400 static ssize_t show_fan##offset##_input(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_FROM_REG(data->fan[offset-1], \
404 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
406 static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
408 struct lm87_data *data = lm87_update_device(dev); \
409 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \
410 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
412 static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
414 struct lm87_data *data = lm87_update_device(dev); \
415 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
417 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
418 show_fan##offset##_input, NULL);
419 show_fan(1);
420 show_fan(2);
422 static void set_fan_min(struct device *dev, const char *buf, int nr)
424 struct i2c_client *client = to_i2c_client(dev);
425 struct lm87_data *data = i2c_get_clientdata(client);
426 long val = simple_strtol(buf, NULL, 10);
428 mutex_lock(&data->update_lock);
429 data->fan_min[nr] = FAN_TO_REG(val,
430 FAN_DIV_FROM_REG(data->fan_div[nr]));
431 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
432 mutex_unlock(&data->update_lock);
435 /* Note: we save and restore the fan minimum here, because its value is
436 determined in part by the fan clock divider. This follows the principle
437 of least surprise; the user doesn't expect the fan minimum to change just
438 because the divider changed. */
439 static ssize_t set_fan_div(struct device *dev, const char *buf,
440 size_t count, int nr)
442 struct i2c_client *client = to_i2c_client(dev);
443 struct lm87_data *data = i2c_get_clientdata(client);
444 long val = simple_strtol(buf, NULL, 10);
445 unsigned long min;
446 u8 reg;
448 mutex_lock(&data->update_lock);
449 min = FAN_FROM_REG(data->fan_min[nr],
450 FAN_DIV_FROM_REG(data->fan_div[nr]));
452 switch (val) {
453 case 1: data->fan_div[nr] = 0; break;
454 case 2: data->fan_div[nr] = 1; break;
455 case 4: data->fan_div[nr] = 2; break;
456 case 8: data->fan_div[nr] = 3; break;
457 default:
458 mutex_unlock(&data->update_lock);
459 return -EINVAL;
462 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
463 switch (nr) {
464 case 0:
465 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
466 break;
467 case 1:
468 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
469 break;
471 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
473 data->fan_min[nr] = FAN_TO_REG(min, val);
474 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
475 data->fan_min[nr]);
476 mutex_unlock(&data->update_lock);
478 return count;
481 #define set_fan(offset) \
482 static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
483 size_t count) \
485 set_fan_min(dev, buf, offset-1); \
486 return count; \
488 static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
489 size_t count) \
491 return set_fan_div(dev, buf, count, offset-1); \
493 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
494 show_fan##offset##_min, set_fan##offset##_min); \
495 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
496 show_fan##offset##_div, set_fan##offset##_div);
497 set_fan(1);
498 set_fan(2);
500 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
502 struct lm87_data *data = lm87_update_device(dev);
503 return sprintf(buf, "%d\n", data->alarms);
505 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
507 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
509 struct lm87_data *data = lm87_update_device(dev);
510 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
512 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
514 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
516 struct lm87_data *data = dev_get_drvdata(dev);
517 return sprintf(buf, "%d\n", data->vrm);
519 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
521 struct lm87_data *data = dev_get_drvdata(dev);
522 data->vrm = simple_strtoul(buf, NULL, 10);
523 return count;
525 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
527 static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
529 struct lm87_data *data = lm87_update_device(dev);
530 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
532 static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
534 struct i2c_client *client = to_i2c_client(dev);
535 struct lm87_data *data = i2c_get_clientdata(client);
536 long val = simple_strtol(buf, NULL, 10);
538 mutex_lock(&data->update_lock);
539 data->aout = AOUT_TO_REG(val);
540 lm87_write_value(client, LM87_REG_AOUT, data->aout);
541 mutex_unlock(&data->update_lock);
542 return count;
544 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
546 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
547 char *buf)
549 struct lm87_data *data = lm87_update_device(dev);
550 int bitnr = to_sensor_dev_attr(attr)->index;
551 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
553 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
554 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
555 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
556 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
557 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
558 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
559 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
560 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
561 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
562 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
563 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
564 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
565 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
566 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
567 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
570 * Real code
573 static struct attribute *lm87_attributes[] = {
574 &dev_attr_in1_input.attr,
575 &dev_attr_in1_min.attr,
576 &dev_attr_in1_max.attr,
577 &sensor_dev_attr_in1_alarm.dev_attr.attr,
578 &dev_attr_in2_input.attr,
579 &dev_attr_in2_min.attr,
580 &dev_attr_in2_max.attr,
581 &sensor_dev_attr_in2_alarm.dev_attr.attr,
582 &dev_attr_in3_input.attr,
583 &dev_attr_in3_min.attr,
584 &dev_attr_in3_max.attr,
585 &sensor_dev_attr_in3_alarm.dev_attr.attr,
586 &dev_attr_in4_input.attr,
587 &dev_attr_in4_min.attr,
588 &dev_attr_in4_max.attr,
589 &sensor_dev_attr_in4_alarm.dev_attr.attr,
591 &dev_attr_temp1_input.attr,
592 &dev_attr_temp1_max.attr,
593 &dev_attr_temp1_min.attr,
594 &dev_attr_temp1_crit.attr,
595 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
596 &dev_attr_temp2_input.attr,
597 &dev_attr_temp2_max.attr,
598 &dev_attr_temp2_min.attr,
599 &dev_attr_temp2_crit.attr,
600 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
601 &sensor_dev_attr_temp2_fault.dev_attr.attr,
603 &dev_attr_alarms.attr,
604 &dev_attr_aout_output.attr,
606 NULL
609 static const struct attribute_group lm87_group = {
610 .attrs = lm87_attributes,
613 static struct attribute *lm87_attributes_opt[] = {
614 &dev_attr_in6_input.attr,
615 &dev_attr_in6_min.attr,
616 &dev_attr_in6_max.attr,
617 &sensor_dev_attr_in6_alarm.dev_attr.attr,
619 &dev_attr_fan1_input.attr,
620 &dev_attr_fan1_min.attr,
621 &dev_attr_fan1_div.attr,
622 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
624 &dev_attr_in7_input.attr,
625 &dev_attr_in7_min.attr,
626 &dev_attr_in7_max.attr,
627 &sensor_dev_attr_in7_alarm.dev_attr.attr,
629 &dev_attr_fan2_input.attr,
630 &dev_attr_fan2_min.attr,
631 &dev_attr_fan2_div.attr,
632 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
634 &dev_attr_temp3_input.attr,
635 &dev_attr_temp3_max.attr,
636 &dev_attr_temp3_min.attr,
637 &dev_attr_temp3_crit.attr,
638 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
639 &sensor_dev_attr_temp3_fault.dev_attr.attr,
641 &dev_attr_in0_input.attr,
642 &dev_attr_in0_min.attr,
643 &dev_attr_in0_max.attr,
644 &sensor_dev_attr_in0_alarm.dev_attr.attr,
645 &dev_attr_in5_input.attr,
646 &dev_attr_in5_min.attr,
647 &dev_attr_in5_max.attr,
648 &sensor_dev_attr_in5_alarm.dev_attr.attr,
650 &dev_attr_cpu0_vid.attr,
651 &dev_attr_vrm.attr,
653 NULL
656 static const struct attribute_group lm87_group_opt = {
657 .attrs = lm87_attributes_opt,
660 /* Return 0 if detection is successful, -ENODEV otherwise */
661 static int lm87_detect(struct i2c_client *new_client,
662 struct i2c_board_info *info)
664 struct i2c_adapter *adapter = new_client->adapter;
665 const char *name;
666 u8 cid, rev;
668 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
669 return -ENODEV;
671 if (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)
672 return -ENODEV;
674 /* Now, we do the remaining detection. */
675 cid = lm87_read_value(new_client, LM87_REG_COMPANY_ID);
676 rev = lm87_read_value(new_client, LM87_REG_REVISION);
678 if (cid == 0x02 /* National Semiconductor */
679 && (rev >= 0x01 && rev <= 0x08))
680 name = "lm87";
681 else if (cid == 0x41 /* Analog Devices */
682 && (rev & 0xf0) == 0x10)
683 name = "adm1024";
684 else {
685 dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n",
686 new_client->addr);
687 return -ENODEV;
690 strlcpy(info->type, name, I2C_NAME_SIZE);
692 return 0;
695 static int lm87_probe(struct i2c_client *new_client,
696 const struct i2c_device_id *id)
698 struct lm87_data *data;
699 int err;
701 data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL);
702 if (!data) {
703 err = -ENOMEM;
704 goto exit;
707 i2c_set_clientdata(new_client, data);
708 data->valid = 0;
709 mutex_init(&data->update_lock);
711 /* Initialize the LM87 chip */
712 lm87_init_client(new_client);
714 data->in_scale[0] = 2500;
715 data->in_scale[1] = 2700;
716 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
717 data->in_scale[3] = 5000;
718 data->in_scale[4] = 12000;
719 data->in_scale[5] = 2700;
720 data->in_scale[6] = 1875;
721 data->in_scale[7] = 1875;
723 /* Register sysfs hooks */
724 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm87_group)))
725 goto exit_free;
727 if (data->channel & CHAN_NO_FAN(0)) {
728 if ((err = device_create_file(&new_client->dev,
729 &dev_attr_in6_input))
730 || (err = device_create_file(&new_client->dev,
731 &dev_attr_in6_min))
732 || (err = device_create_file(&new_client->dev,
733 &dev_attr_in6_max))
734 || (err = device_create_file(&new_client->dev,
735 &sensor_dev_attr_in6_alarm.dev_attr)))
736 goto exit_remove;
737 } else {
738 if ((err = device_create_file(&new_client->dev,
739 &dev_attr_fan1_input))
740 || (err = device_create_file(&new_client->dev,
741 &dev_attr_fan1_min))
742 || (err = device_create_file(&new_client->dev,
743 &dev_attr_fan1_div))
744 || (err = device_create_file(&new_client->dev,
745 &sensor_dev_attr_fan1_alarm.dev_attr)))
746 goto exit_remove;
749 if (data->channel & CHAN_NO_FAN(1)) {
750 if ((err = device_create_file(&new_client->dev,
751 &dev_attr_in7_input))
752 || (err = device_create_file(&new_client->dev,
753 &dev_attr_in7_min))
754 || (err = device_create_file(&new_client->dev,
755 &dev_attr_in7_max))
756 || (err = device_create_file(&new_client->dev,
757 &sensor_dev_attr_in7_alarm.dev_attr)))
758 goto exit_remove;
759 } else {
760 if ((err = device_create_file(&new_client->dev,
761 &dev_attr_fan2_input))
762 || (err = device_create_file(&new_client->dev,
763 &dev_attr_fan2_min))
764 || (err = device_create_file(&new_client->dev,
765 &dev_attr_fan2_div))
766 || (err = device_create_file(&new_client->dev,
767 &sensor_dev_attr_fan2_alarm.dev_attr)))
768 goto exit_remove;
771 if (data->channel & CHAN_TEMP3) {
772 if ((err = device_create_file(&new_client->dev,
773 &dev_attr_temp3_input))
774 || (err = device_create_file(&new_client->dev,
775 &dev_attr_temp3_max))
776 || (err = device_create_file(&new_client->dev,
777 &dev_attr_temp3_min))
778 || (err = device_create_file(&new_client->dev,
779 &dev_attr_temp3_crit))
780 || (err = device_create_file(&new_client->dev,
781 &sensor_dev_attr_temp3_alarm.dev_attr))
782 || (err = device_create_file(&new_client->dev,
783 &sensor_dev_attr_temp3_fault.dev_attr)))
784 goto exit_remove;
785 } else {
786 if ((err = device_create_file(&new_client->dev,
787 &dev_attr_in0_input))
788 || (err = device_create_file(&new_client->dev,
789 &dev_attr_in0_min))
790 || (err = device_create_file(&new_client->dev,
791 &dev_attr_in0_max))
792 || (err = device_create_file(&new_client->dev,
793 &sensor_dev_attr_in0_alarm.dev_attr))
794 || (err = device_create_file(&new_client->dev,
795 &dev_attr_in5_input))
796 || (err = device_create_file(&new_client->dev,
797 &dev_attr_in5_min))
798 || (err = device_create_file(&new_client->dev,
799 &dev_attr_in5_max))
800 || (err = device_create_file(&new_client->dev,
801 &sensor_dev_attr_in5_alarm.dev_attr)))
802 goto exit_remove;
805 if (!(data->channel & CHAN_NO_VID)) {
806 data->vrm = vid_which_vrm();
807 if ((err = device_create_file(&new_client->dev,
808 &dev_attr_cpu0_vid))
809 || (err = device_create_file(&new_client->dev,
810 &dev_attr_vrm)))
811 goto exit_remove;
814 data->hwmon_dev = hwmon_device_register(&new_client->dev);
815 if (IS_ERR(data->hwmon_dev)) {
816 err = PTR_ERR(data->hwmon_dev);
817 goto exit_remove;
820 return 0;
822 exit_remove:
823 sysfs_remove_group(&new_client->dev.kobj, &lm87_group);
824 sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt);
825 exit_free:
826 lm87_write_value(new_client, LM87_REG_CONFIG, data->config);
827 kfree(data);
828 exit:
829 return err;
832 static void lm87_init_client(struct i2c_client *client)
834 struct lm87_data *data = i2c_get_clientdata(client);
836 if (client->dev.platform_data) {
837 data->channel = *(u8 *)client->dev.platform_data;
838 lm87_write_value(client,
839 LM87_REG_CHANNEL_MODE, data->channel);
840 } else {
841 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
843 data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F;
845 if (!(data->config & 0x01)) {
846 int i;
848 /* Limits are left uninitialized after power-up */
849 for (i = 1; i < 6; i++) {
850 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
851 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
853 for (i = 0; i < 2; i++) {
854 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
855 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
856 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
857 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
859 if (data->channel & CHAN_TEMP3) {
860 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
861 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
862 } else {
863 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
864 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
868 /* Make sure Start is set and INT#_Clear is clear */
869 if ((data->config & 0x09) != 0x01)
870 lm87_write_value(client, LM87_REG_CONFIG,
871 (data->config & 0x77) | 0x01);
874 static int lm87_remove(struct i2c_client *client)
876 struct lm87_data *data = i2c_get_clientdata(client);
878 hwmon_device_unregister(data->hwmon_dev);
879 sysfs_remove_group(&client->dev.kobj, &lm87_group);
880 sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);
882 lm87_write_value(client, LM87_REG_CONFIG, data->config);
883 kfree(data);
884 return 0;
887 static struct lm87_data *lm87_update_device(struct device *dev)
889 struct i2c_client *client = to_i2c_client(dev);
890 struct lm87_data *data = i2c_get_clientdata(client);
892 mutex_lock(&data->update_lock);
894 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
895 int i, j;
897 dev_dbg(&client->dev, "Updating data.\n");
899 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
900 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
901 for (; i < j; i++) {
902 data->in[i] = lm87_read_value(client,
903 LM87_REG_IN(i));
904 data->in_min[i] = lm87_read_value(client,
905 LM87_REG_IN_MIN(i));
906 data->in_max[i] = lm87_read_value(client,
907 LM87_REG_IN_MAX(i));
910 for (i = 0; i < 2; i++) {
911 if (data->channel & CHAN_NO_FAN(i)) {
912 data->in[6+i] = lm87_read_value(client,
913 LM87_REG_AIN(i));
914 data->in_max[6+i] = lm87_read_value(client,
915 LM87_REG_AIN_MAX(i));
916 data->in_min[6+i] = lm87_read_value(client,
917 LM87_REG_AIN_MIN(i));
919 } else {
920 data->fan[i] = lm87_read_value(client,
921 LM87_REG_FAN(i));
922 data->fan_min[i] = lm87_read_value(client,
923 LM87_REG_FAN_MIN(i));
927 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
928 for (i = 0 ; i < j; i++) {
929 data->temp[i] = lm87_read_value(client,
930 LM87_REG_TEMP[i]);
931 data->temp_high[i] = lm87_read_value(client,
932 LM87_REG_TEMP_HIGH[i]);
933 data->temp_low[i] = lm87_read_value(client,
934 LM87_REG_TEMP_LOW[i]);
937 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
938 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
939 data->temp_crit_int = min(i, j);
941 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
942 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
943 data->temp_crit_ext = min(i, j);
945 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
946 data->fan_div[0] = (i >> 4) & 0x03;
947 data->fan_div[1] = (i >> 6) & 0x03;
948 data->vid = (i & 0x0F)
949 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
950 << 4;
952 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
953 | (lm87_read_value(client, LM87_REG_ALARMS2)
954 << 8);
955 data->aout = lm87_read_value(client, LM87_REG_AOUT);
957 data->last_updated = jiffies;
958 data->valid = 1;
961 mutex_unlock(&data->update_lock);
963 return data;
966 static int __init sensors_lm87_init(void)
968 return i2c_add_driver(&lm87_driver);
971 static void __exit sensors_lm87_exit(void)
973 i2c_del_driver(&lm87_driver);
976 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
977 MODULE_DESCRIPTION("LM87 driver");
978 MODULE_LICENSE("GPL");
980 module_init(sensors_lm87_init);
981 module_exit(sensors_lm87_exit);