blob: d0d2464c1b731ca9684adc919b4c71a2d9fbea73 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lm87.c
3 *
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>
9 *
10 * Original port to Linux 2.6 by Jeff Oliver.
11 *
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
22 *
23 * Some functions share pins, so not all functions are available at the same
24 * time. Which are depends on the hardware setup. This driver assumes that
25 * the BIOS configured the chip correctly. In that respect, it differs from
26 * the original driver (from lm_sensors for Linux 2.4), which would force the
27 * LM87 to an arbitrary, compile-time chosen mode, regardless of the actual
28 * chipset wiring.
29 * For reference, here is the list of exclusive functions:
30 * - in0+in5 (default) or temp3
31 * - fan1 (default) or in6
32 * - fan2 (default) or in7
33 * - VID lines (default) or IRQ lines (not handled by this driver)
34 *
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.
39 *
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.
44 *
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.
49 *
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.
53 */
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/module.h>
56#include <linux/init.h>
57#include <linux/slab.h>
58#include <linux/jiffies.h>
59#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/i2c-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040061#include <linux/hwmon.h>
62#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * Addresses to scan
66 * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
67 */
68
69static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * Insmod parameters
73 */
74
Jean Delvaref4b50262005-07-31 21:49:03 +020075I2C_CLIENT_INSMOD_1(lm87);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * The LM87 registers
79 */
80
81/* nr in 0..5 */
82#define LM87_REG_IN(nr) (0x20 + (nr))
83#define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
84#define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
85/* nr in 0..1 */
86#define LM87_REG_AIN(nr) (0x28 + (nr))
87#define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
88#define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
89
90static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
91static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
92static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
93
94#define LM87_REG_TEMP_HW_INT_LOCK 0x13
95#define LM87_REG_TEMP_HW_EXT_LOCK 0x14
96#define LM87_REG_TEMP_HW_INT 0x17
97#define LM87_REG_TEMP_HW_EXT 0x18
98
99/* nr in 0..1 */
100#define LM87_REG_FAN(nr) (0x28 + (nr))
101#define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
102#define LM87_REG_AOUT 0x19
103
104#define LM87_REG_CONFIG 0x40
105#define LM87_REG_CHANNEL_MODE 0x16
106#define LM87_REG_VID_FAN_DIV 0x47
107#define LM87_REG_VID4 0x49
108
109#define LM87_REG_ALARMS1 0x41
110#define LM87_REG_ALARMS2 0x42
111
112#define LM87_REG_COMPANY_ID 0x3E
113#define LM87_REG_REVISION 0x3F
114
115/*
116 * Conversions and various macros
117 * The LM87 uses signed 8-bit values for temperatures.
118 */
119
120#define IN_FROM_REG(reg,scale) (((reg) * (scale) + 96) / 192)
121#define IN_TO_REG(val,scale) ((val) <= 0 ? 0 : \
122 (val) * 192 >= (scale) * 255 ? 255 : \
123 ((val) * 192 + (scale)/2) / (scale))
124
125#define TEMP_FROM_REG(reg) ((reg) * 1000)
126#define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
127 (val) >= 126500 ? 127 : \
128 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
129
130#define FAN_FROM_REG(reg,div) ((reg) == 255 || (reg) == 0 ? 0 : \
131 1350000 + (reg)*(div) / 2) / ((reg)*(div))
132#define FAN_TO_REG(val,div) ((val)*(div) * 255 <= 1350000 ? 255 : \
133 (1350000 + (val)*(div) / 2) / ((val)*(div)))
134
135#define FAN_DIV_FROM_REG(reg) (1 << (reg))
136
137/* analog out is 9.80mV/LSB */
138#define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
139#define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
140 (val) >= 2500 ? 255 : \
141 ((val) * 10 + 49) / 98)
142
143/* nr in 0..1 */
144#define CHAN_NO_FAN(nr) (1 << (nr))
145#define CHAN_TEMP3 (1 << 2)
146#define CHAN_VCC_5V (1 << 3)
147#define CHAN_NO_VID (1 << 8)
148
149/*
150 * Functions declaration
151 */
152
153static int lm87_attach_adapter(struct i2c_adapter *adapter);
154static int lm87_detect(struct i2c_adapter *adapter, int address, int kind);
155static void lm87_init_client(struct i2c_client *client);
156static int lm87_detach_client(struct i2c_client *client);
157static struct lm87_data *lm87_update_device(struct device *dev);
158
159/*
160 * Driver data (common to all clients)
161 */
162
163static struct i2c_driver lm87_driver = {
164 .owner = THIS_MODULE,
165 .name = "lm87",
166 .id = I2C_DRIVERID_LM87,
167 .flags = I2C_DF_NOTIFY,
168 .attach_adapter = lm87_attach_adapter,
169 .detach_client = lm87_detach_client,
170};
171
172/*
173 * Client data (each client gets its own)
174 */
175
176struct lm87_data {
177 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400178 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct semaphore update_lock;
180 char valid; /* zero until following fields are valid */
181 unsigned long last_updated; /* In jiffies */
182
183 u8 channel; /* register value */
184
185 u8 in[8]; /* register value */
186 u8 in_max[8]; /* register value */
187 u8 in_min[8]; /* register value */
188 u16 in_scale[8];
189
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 */
195
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 */
200
201 u16 alarms; /* register values, combined */
202 u8 vid; /* register values, combined */
203 u8 vrm;
204};
205
206/*
207 * Sysfs stuff
208 */
209
210static inline int lm87_read_value(struct i2c_client *client, u8 reg)
211{
212 return i2c_smbus_read_byte_data(client, reg);
213}
214
215static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
216{
217 return i2c_smbus_write_byte_data(client, reg, value);
218}
219
220#define show_in(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400221static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{ \
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])); \
226} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400227static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{ \
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])); \
232} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400233static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{ \
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])); \
238} \
239static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
240 show_in##offset##_input, NULL);
241show_in(0);
242show_in(1);
243show_in(2);
244show_in(3);
245show_in(4);
246show_in(5);
247show_in(6);
248show_in(7);
249
250static void set_in_min(struct device *dev, const char *buf, int nr)
251{
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);
255
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);
261}
262
263static void set_in_max(struct device *dev, const char *buf, int nr)
264{
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);
268
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);
274}
275
276#define set_in(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400277static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 const char *buf, size_t count) \
279{ \
280 set_in_min(dev, buf, offset); \
281 return count; \
282} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400283static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 const char *buf, size_t count) \
285{ \
286 set_in_max(dev, buf, offset); \
287 return count; \
288} \
289static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
290 show_in##offset##_min, set_in##offset##_min); \
291static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
292 show_in##offset##_max, set_in##offset##_max);
293set_in(0);
294set_in(1);
295set_in(2);
296set_in(3);
297set_in(4);
298set_in(5);
299set_in(6);
300set_in(7);
301
302#define show_temp(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400303static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{ \
305 struct lm87_data *data = lm87_update_device(dev); \
306 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
307} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400308static ssize_t show_temp##offset##_low(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{ \
310 struct lm87_data *data = lm87_update_device(dev); \
311 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
312} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400313static ssize_t show_temp##offset##_high(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{ \
315 struct lm87_data *data = lm87_update_device(dev); \
316 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
317}\
318static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
319 show_temp##offset##_input, NULL);
320show_temp(1);
321show_temp(2);
322show_temp(3);
323
324static void set_temp_low(struct device *dev, const char *buf, int nr)
325{
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);
329
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);
334}
335
336static void set_temp_high(struct device *dev, const char *buf, int nr)
337{
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);
341
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);
346}
347
348#define set_temp(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400349static ssize_t set_temp##offset##_low(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 const char *buf, size_t count) \
351{ \
352 set_temp_low(dev, buf, offset-1); \
353 return count; \
354} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400355static ssize_t set_temp##offset##_high(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 const char *buf, size_t count) \
357{ \
358 set_temp_high(dev, buf, offset-1); \
359 return count; \
360} \
361static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
362 show_temp##offset##_high, set_temp##offset##_high); \
363static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
364 show_temp##offset##_low, set_temp##offset##_low);
365set_temp(1);
366set_temp(2);
367set_temp(3);
368
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400369static ssize_t show_temp_crit_int(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct lm87_data *data = lm87_update_device(dev);
372 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
373}
374
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400375static ssize_t show_temp_crit_ext(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct lm87_data *data = lm87_update_device(dev);
378 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
379}
380
381static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
382static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
383static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
384
385#define show_fan(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400386static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{ \
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]))); \
391} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400392static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{ \
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]))); \
397} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400398static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{ \
400 struct lm87_data *data = lm87_update_device(dev); \
401 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
402} \
403static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
404 show_fan##offset##_input, NULL);
405show_fan(1);
406show_fan(2);
407
408static void set_fan_min(struct device *dev, const char *buf, int nr)
409{
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);
413
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);
419}
420
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. */
425static ssize_t set_fan_div(struct device *dev, const char *buf,
426 size_t count, int nr)
427{
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);
431 unsigned long min;
432 u8 reg;
433
434 down(&data->update_lock);
435 min = FAN_FROM_REG(data->fan_min[nr],
436 FAN_DIV_FROM_REG(data->fan_div[nr]));
437
438 switch (val) {
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;
443 default:
444 up(&data->update_lock);
445 return -EINVAL;
446 }
447
448 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
449 switch (nr) {
450 case 0:
451 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
452 break;
453 case 1:
454 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
455 break;
456 }
457 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
458
459 data->fan_min[nr] = FAN_TO_REG(min, val);
460 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
461 data->fan_min[nr]);
462 up(&data->update_lock);
463
464 return count;
465}
466
467#define set_fan(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400468static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 size_t count) \
470{ \
471 set_fan_min(dev, buf, offset-1); \
472 return count; \
473} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400474static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 size_t count) \
476{ \
477 return set_fan_div(dev, buf, count, offset-1); \
478} \
479static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
480 show_fan##offset##_min, set_fan##offset##_min); \
481static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
482 show_fan##offset##_div, set_fan##offset##_div);
483set_fan(1);
484set_fan(2);
485
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400486static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct lm87_data *data = lm87_update_device(dev);
489 return sprintf(buf, "%d\n", data->alarms);
490}
491static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
492
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400493static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct lm87_data *data = lm87_update_device(dev);
496 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
497}
498static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
499
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400500static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct lm87_data *data = lm87_update_device(dev);
503 return sprintf(buf, "%d\n", data->vrm);
504}
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400505static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
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);
510 return count;
511}
512static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
513
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400514static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 struct lm87_data *data = lm87_update_device(dev);
517 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
518}
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400519static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
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);
524
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);
529 return count;
530}
531static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
532
533/*
534 * Real code
535 */
536
537static int lm87_attach_adapter(struct i2c_adapter *adapter)
538{
539 if (!(adapter->class & I2C_CLASS_HWMON))
540 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200541 return i2c_probe(adapter, &addr_data, lm87_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/*
545 * The following function does more than just detection. If detection
546 * succeeds, it also registers the new chip.
547 */
548static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
549{
550 struct i2c_client *new_client;
551 struct lm87_data *data;
552 int err = 0;
553
554 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
555 goto exit;
556
557 if (!(data = kmalloc(sizeof(struct lm87_data), GFP_KERNEL))) {
558 err = -ENOMEM;
559 goto exit;
560 }
561 memset(data, 0, sizeof(struct lm87_data));
562
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;
571
572 /* Default to an LM87 if forced */
573 if (kind == 0)
574 kind = lm87;
575
576 /* Now, we do the remaining detection. */
577 if (kind < 0) {
578 u8 rev = lm87_read_value(new_client, LM87_REG_REVISION);
579
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",
585 address);
586 goto exit_free;
587 }
588 }
589
590 /* We can fill in the remaining client fields */
591 strlcpy(new_client->name, "lm87", I2C_NAME_SIZE);
592 data->valid = 0;
593 init_MUTEX(&data->update_lock);
594
595 /* Tell the I2C layer a new client has arrived */
596 if ((err = i2c_attach_client(new_client)))
597 goto exit_free;
598
599 /* Initialize the LM87 chip */
600 lm87_init_client(new_client);
601
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;
610
611 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400612 data->class_dev = hwmon_device_register(&new_client->dev);
613 if (IS_ERR(data->class_dev)) {
614 err = PTR_ERR(data->class_dev);
615 goto exit_detach;
616 }
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 device_create_file(&new_client->dev, &dev_attr_in1_input);
619 device_create_file(&new_client->dev, &dev_attr_in1_min);
620 device_create_file(&new_client->dev, &dev_attr_in1_max);
621 device_create_file(&new_client->dev, &dev_attr_in2_input);
622 device_create_file(&new_client->dev, &dev_attr_in2_min);
623 device_create_file(&new_client->dev, &dev_attr_in2_max);
624 device_create_file(&new_client->dev, &dev_attr_in3_input);
625 device_create_file(&new_client->dev, &dev_attr_in3_min);
626 device_create_file(&new_client->dev, &dev_attr_in3_max);
627 device_create_file(&new_client->dev, &dev_attr_in4_input);
628 device_create_file(&new_client->dev, &dev_attr_in4_min);
629 device_create_file(&new_client->dev, &dev_attr_in4_max);
630
631 if (data->channel & CHAN_NO_FAN(0)) {
632 device_create_file(&new_client->dev, &dev_attr_in6_input);
633 device_create_file(&new_client->dev, &dev_attr_in6_min);
634 device_create_file(&new_client->dev, &dev_attr_in6_max);
635 } else {
636 device_create_file(&new_client->dev, &dev_attr_fan1_input);
637 device_create_file(&new_client->dev, &dev_attr_fan1_min);
638 device_create_file(&new_client->dev, &dev_attr_fan1_div);
639 }
640 if (data->channel & CHAN_NO_FAN(1)) {
641 device_create_file(&new_client->dev, &dev_attr_in7_input);
642 device_create_file(&new_client->dev, &dev_attr_in7_min);
643 device_create_file(&new_client->dev, &dev_attr_in7_max);
644 } else {
645 device_create_file(&new_client->dev, &dev_attr_fan2_input);
646 device_create_file(&new_client->dev, &dev_attr_fan2_min);
647 device_create_file(&new_client->dev, &dev_attr_fan2_div);
648 }
649
650 device_create_file(&new_client->dev, &dev_attr_temp1_input);
651 device_create_file(&new_client->dev, &dev_attr_temp1_max);
652 device_create_file(&new_client->dev, &dev_attr_temp1_min);
653 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
654 device_create_file(&new_client->dev, &dev_attr_temp2_input);
655 device_create_file(&new_client->dev, &dev_attr_temp2_max);
656 device_create_file(&new_client->dev, &dev_attr_temp2_min);
657 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
658
659 if (data->channel & CHAN_TEMP3) {
660 device_create_file(&new_client->dev, &dev_attr_temp3_input);
661 device_create_file(&new_client->dev, &dev_attr_temp3_max);
662 device_create_file(&new_client->dev, &dev_attr_temp3_min);
663 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
664 } else {
665 device_create_file(&new_client->dev, &dev_attr_in0_input);
666 device_create_file(&new_client->dev, &dev_attr_in0_min);
667 device_create_file(&new_client->dev, &dev_attr_in0_max);
668 device_create_file(&new_client->dev, &dev_attr_in5_input);
669 device_create_file(&new_client->dev, &dev_attr_in5_min);
670 device_create_file(&new_client->dev, &dev_attr_in5_max);
671 }
672
673 if (!(data->channel & CHAN_NO_VID)) {
674 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
675 device_create_file(&new_client->dev, &dev_attr_vrm);
676 }
677
678 device_create_file(&new_client->dev, &dev_attr_alarms);
679 device_create_file(&new_client->dev, &dev_attr_aout_output);
680
681 return 0;
682
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400683exit_detach:
684 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685exit_free:
686 kfree(data);
687exit:
688 return err;
689}
690
691static void lm87_init_client(struct i2c_client *client)
692{
693 struct lm87_data *data = i2c_get_clientdata(client);
694 u8 config;
695
696 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
697 data->vrm = i2c_which_vrm();
698
699 config = lm87_read_value(client, LM87_REG_CONFIG);
700 if (!(config & 0x01)) {
701 int i;
702
703 /* Limits are left uninitialized after power-up */
704 for (i = 1; i < 6; i++) {
705 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
706 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
707 }
708 for (i = 0; i < 2; i++) {
709 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
710 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
711 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
712 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
713 }
714 if (data->channel & CHAN_TEMP3) {
715 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
716 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
717 } else {
718 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
719 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
720 }
721 }
722 if ((config & 0x81) != 0x01) {
723 /* Start monitoring */
724 lm87_write_value(client, LM87_REG_CONFIG,
725 (config & 0xF7) | 0x01);
726 }
727}
728
729static int lm87_detach_client(struct i2c_client *client)
730{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400731 struct lm87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 int err;
733
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400734 hwmon_device_unregister(data->class_dev);
735
Jean Delvare7bef5592005-07-27 22:14:49 +0200736 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400739 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return 0;
741}
742
743static struct lm87_data *lm87_update_device(struct device *dev)
744{
745 struct i2c_client *client = to_i2c_client(dev);
746 struct lm87_data *data = i2c_get_clientdata(client);
747
748 down(&data->update_lock);
749
750 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
751 int i, j;
752
753 dev_dbg(&client->dev, "Updating data.\n");
754
755 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
756 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
757 for (; i < j; i++) {
758 data->in[i] = lm87_read_value(client,
759 LM87_REG_IN(i));
760 data->in_min[i] = lm87_read_value(client,
761 LM87_REG_IN_MIN(i));
762 data->in_max[i] = lm87_read_value(client,
763 LM87_REG_IN_MAX(i));
764 }
765
766 for (i = 0; i < 2; i++) {
767 if (data->channel & CHAN_NO_FAN(i)) {
768 data->in[6+i] = lm87_read_value(client,
769 LM87_REG_AIN(i));
770 data->in_max[6+i] = lm87_read_value(client,
771 LM87_REG_AIN_MAX(i));
772 data->in_min[6+i] = lm87_read_value(client,
773 LM87_REG_AIN_MIN(i));
774
775 } else {
776 data->fan[i] = lm87_read_value(client,
777 LM87_REG_FAN(i));
778 data->fan_min[i] = lm87_read_value(client,
779 LM87_REG_FAN_MIN(i));
780 }
781 }
782
783 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
784 for (i = 0 ; i < j; i++) {
785 data->temp[i] = lm87_read_value(client,
786 LM87_REG_TEMP[i]);
787 data->temp_high[i] = lm87_read_value(client,
788 LM87_REG_TEMP_HIGH[i]);
789 data->temp_low[i] = lm87_read_value(client,
790 LM87_REG_TEMP_LOW[i]);
791 }
792
793 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
794 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
795 data->temp_crit_int = min(i, j);
796
797 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
798 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
799 data->temp_crit_ext = min(i, j);
800
801 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
802 data->fan_div[0] = (i >> 4) & 0x03;
803 data->fan_div[1] = (i >> 6) & 0x03;
804 data->vid = (i & 0x0F)
805 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
806 << 4;
807
808 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
809 | (lm87_read_value(client, LM87_REG_ALARMS2)
810 << 8);
811 data->aout = lm87_read_value(client, LM87_REG_AOUT);
812
813 data->last_updated = jiffies;
814 data->valid = 1;
815 }
816
817 up(&data->update_lock);
818
819 return data;
820}
821
822static int __init sensors_lm87_init(void)
823{
824 return i2c_add_driver(&lm87_driver);
825}
826
827static void __exit sensors_lm87_exit(void)
828{
829 i2c_del_driver(&lm87_driver);
830}
831
832MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
833MODULE_DESCRIPTION("LM87 driver");
834MODULE_LICENSE("GPL");
835
836module_init(sensors_lm87_init);
837module_exit(sensors_lm87_exit);