blob: 7f806a05890dfe1d93f80902951e5ae918d92d82 [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>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040060#include <linux/hwmon.h>
Jean Delvarec2803b92007-09-26 23:39:01 +020061#include <linux/hwmon-sysfs.h>
Jean Delvare303760b2005-07-31 21:52:01 +020062#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040063#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010064#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
67 * Addresses to scan
68 * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
69 */
70
71static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * Insmod parameters
75 */
76
Jean Delvaref4b50262005-07-31 21:49:03 +020077I2C_CLIENT_INSMOD_1(lm87);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * The LM87 registers
81 */
82
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))
91
92static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
93static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
94static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
95
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
100
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
105
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
110
111#define LM87_REG_ALARMS1 0x41
112#define LM87_REG_ALARMS2 0x42
113
114#define LM87_REG_COMPANY_ID 0x3E
115#define LM87_REG_REVISION 0x3F
116
117/*
118 * Conversions and various macros
119 * The LM87 uses signed 8-bit values for temperatures.
120 */
121
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))
126
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))
131
132#define FAN_FROM_REG(reg,div) ((reg) == 255 || (reg) == 0 ? 0 : \
Jean Delvareb965d4b2007-09-26 22:16:56 +0200133 (1350000 + (reg)*(div) / 2) / ((reg)*(div)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define FAN_TO_REG(val,div) ((val)*(div) * 255 <= 1350000 ? 255 : \
135 (1350000 + (val)*(div) / 2) / ((val)*(div)))
136
137#define FAN_DIV_FROM_REG(reg) (1 << (reg))
138
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)
144
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 << 8)
150
151/*
152 * Functions declaration
153 */
154
155static int lm87_attach_adapter(struct i2c_adapter *adapter);
156static int lm87_detect(struct i2c_adapter *adapter, int address, int kind);
157static void lm87_init_client(struct i2c_client *client);
158static int lm87_detach_client(struct i2c_client *client);
159static struct lm87_data *lm87_update_device(struct device *dev);
160
161/*
162 * Driver data (common to all clients)
163 */
164
165static struct i2c_driver lm87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100166 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100167 .name = "lm87",
168 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 .id = I2C_DRIVERID_LM87,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .attach_adapter = lm87_attach_adapter,
171 .detach_client = lm87_detach_client,
172};
173
174/*
175 * Client data (each client gets its own)
176 */
177
178struct lm87_data {
179 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700180 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100181 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 char valid; /* zero until following fields are valid */
183 unsigned long last_updated; /* In jiffies */
184
185 u8 channel; /* register value */
186
187 u8 in[8]; /* register value */
188 u8 in_max[8]; /* register value */
189 u8 in_min[8]; /* register value */
190 u16 in_scale[8];
191
192 s8 temp[3]; /* register value */
193 s8 temp_high[3]; /* register value */
194 s8 temp_low[3]; /* register value */
195 s8 temp_crit_int; /* min of two register values */
196 s8 temp_crit_ext; /* min of two register values */
197
198 u8 fan[2]; /* register value */
199 u8 fan_min[2]; /* register value */
200 u8 fan_div[2]; /* register value, shifted right */
201 u8 aout; /* register value */
202
203 u16 alarms; /* register values, combined */
204 u8 vid; /* register values, combined */
205 u8 vrm;
206};
207
208/*
209 * Sysfs stuff
210 */
211
212static inline int lm87_read_value(struct i2c_client *client, u8 reg)
213{
214 return i2c_smbus_read_byte_data(client, reg);
215}
216
217static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
218{
219 return i2c_smbus_write_byte_data(client, reg, value);
220}
221
222#define show_in(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400223static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{ \
225 struct lm87_data *data = lm87_update_device(dev); \
226 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
227 data->in_scale[offset])); \
228} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400229static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{ \
231 struct lm87_data *data = lm87_update_device(dev); \
232 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
233 data->in_scale[offset])); \
234} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400235static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{ \
237 struct lm87_data *data = lm87_update_device(dev); \
238 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
239 data->in_scale[offset])); \
240} \
241static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
242 show_in##offset##_input, NULL);
243show_in(0);
244show_in(1);
245show_in(2);
246show_in(3);
247show_in(4);
248show_in(5);
249show_in(6);
250show_in(7);
251
252static void set_in_min(struct device *dev, const char *buf, int nr)
253{
254 struct i2c_client *client = to_i2c_client(dev);
255 struct lm87_data *data = i2c_get_clientdata(client);
256 long val = simple_strtol(buf, NULL, 10);
257
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100258 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
260 lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
261 LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100262 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265static void set_in_max(struct device *dev, const char *buf, int nr)
266{
267 struct i2c_client *client = to_i2c_client(dev);
268 struct lm87_data *data = i2c_get_clientdata(client);
269 long val = simple_strtol(buf, NULL, 10);
270
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100271 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
273 lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
274 LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100275 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278#define set_in(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400279static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 const char *buf, size_t count) \
281{ \
282 set_in_min(dev, buf, offset); \
283 return count; \
284} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400285static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 const char *buf, size_t count) \
287{ \
288 set_in_max(dev, buf, offset); \
289 return count; \
290} \
291static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
292 show_in##offset##_min, set_in##offset##_min); \
293static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
294 show_in##offset##_max, set_in##offset##_max);
295set_in(0);
296set_in(1);
297set_in(2);
298set_in(3);
299set_in(4);
300set_in(5);
301set_in(6);
302set_in(7);
303
304#define show_temp(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400305static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{ \
307 struct lm87_data *data = lm87_update_device(dev); \
308 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
309} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400310static ssize_t show_temp##offset##_low(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{ \
312 struct lm87_data *data = lm87_update_device(dev); \
313 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
314} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400315static ssize_t show_temp##offset##_high(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{ \
317 struct lm87_data *data = lm87_update_device(dev); \
318 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
319}\
320static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
321 show_temp##offset##_input, NULL);
322show_temp(1);
323show_temp(2);
324show_temp(3);
325
326static void set_temp_low(struct device *dev, const char *buf, int nr)
327{
328 struct i2c_client *client = to_i2c_client(dev);
329 struct lm87_data *data = i2c_get_clientdata(client);
330 long val = simple_strtol(buf, NULL, 10);
331
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100332 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 data->temp_low[nr] = TEMP_TO_REG(val);
334 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100335 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338static void set_temp_high(struct device *dev, const char *buf, int nr)
339{
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);
343
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100344 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 data->temp_high[nr] = TEMP_TO_REG(val);
346 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100347 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350#define set_temp(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400351static ssize_t set_temp##offset##_low(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 const char *buf, size_t count) \
353{ \
354 set_temp_low(dev, buf, offset-1); \
355 return count; \
356} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400357static ssize_t set_temp##offset##_high(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 const char *buf, size_t count) \
359{ \
360 set_temp_high(dev, buf, offset-1); \
361 return count; \
362} \
363static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
364 show_temp##offset##_high, set_temp##offset##_high); \
365static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
366 show_temp##offset##_low, set_temp##offset##_low);
367set_temp(1);
368set_temp(2);
369set_temp(3);
370
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400371static ssize_t show_temp_crit_int(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct lm87_data *data = lm87_update_device(dev);
374 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
375}
376
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400377static ssize_t show_temp_crit_ext(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 struct lm87_data *data = lm87_update_device(dev);
380 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
381}
382
383static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
384static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
385static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
386
387#define show_fan(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400388static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{ \
390 struct lm87_data *data = lm87_update_device(dev); \
391 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \
392 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
393} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400394static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{ \
396 struct lm87_data *data = lm87_update_device(dev); \
397 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \
398 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
399} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400400static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{ \
402 struct lm87_data *data = lm87_update_device(dev); \
403 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
404} \
405static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
406 show_fan##offset##_input, NULL);
407show_fan(1);
408show_fan(2);
409
410static void set_fan_min(struct device *dev, const char *buf, int nr)
411{
412 struct i2c_client *client = to_i2c_client(dev);
413 struct lm87_data *data = i2c_get_clientdata(client);
414 long val = simple_strtol(buf, NULL, 10);
415
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100416 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 data->fan_min[nr] = FAN_TO_REG(val,
418 FAN_DIV_FROM_REG(data->fan_div[nr]));
419 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100420 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423/* Note: we save and restore the fan minimum here, because its value is
424 determined in part by the fan clock divider. This follows the principle
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200425 of least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 because the divider changed. */
427static ssize_t set_fan_div(struct device *dev, const char *buf,
428 size_t count, int nr)
429{
430 struct i2c_client *client = to_i2c_client(dev);
431 struct lm87_data *data = i2c_get_clientdata(client);
432 long val = simple_strtol(buf, NULL, 10);
433 unsigned long min;
434 u8 reg;
435
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100436 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 min = FAN_FROM_REG(data->fan_min[nr],
438 FAN_DIV_FROM_REG(data->fan_div[nr]));
439
440 switch (val) {
441 case 1: data->fan_div[nr] = 0; break;
442 case 2: data->fan_div[nr] = 1; break;
443 case 4: data->fan_div[nr] = 2; break;
444 case 8: data->fan_div[nr] = 3; break;
445 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100446 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -EINVAL;
448 }
449
450 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
451 switch (nr) {
452 case 0:
453 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
454 break;
455 case 1:
456 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
457 break;
458 }
459 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
460
461 data->fan_min[nr] = FAN_TO_REG(min, val);
462 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
463 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100464 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 return count;
467}
468
469#define set_fan(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400470static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 size_t count) \
472{ \
473 set_fan_min(dev, buf, offset-1); \
474 return count; \
475} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400476static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 size_t count) \
478{ \
479 return set_fan_div(dev, buf, count, offset-1); \
480} \
481static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
482 show_fan##offset##_min, set_fan##offset##_min); \
483static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
484 show_fan##offset##_div, set_fan##offset##_div);
485set_fan(1);
486set_fan(2);
487
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400488static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct lm87_data *data = lm87_update_device(dev);
491 return sprintf(buf, "%d\n", data->alarms);
492}
493static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
494
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400495static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct lm87_data *data = lm87_update_device(dev);
498 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
499}
500static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
501
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400502static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Jean Delvare90d66192007-10-08 18:24:35 +0200504 struct lm87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return sprintf(buf, "%d\n", data->vrm);
506}
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400507static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 struct i2c_client *client = to_i2c_client(dev);
510 struct lm87_data *data = i2c_get_clientdata(client);
511 data->vrm = simple_strtoul(buf, NULL, 10);
512 return count;
513}
514static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
515
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400516static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct lm87_data *data = lm87_update_device(dev);
519 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
520}
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400521static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct i2c_client *client = to_i2c_client(dev);
524 struct lm87_data *data = i2c_get_clientdata(client);
525 long val = simple_strtol(buf, NULL, 10);
526
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100527 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 data->aout = AOUT_TO_REG(val);
529 lm87_write_value(client, LM87_REG_AOUT, data->aout);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100530 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return count;
532}
533static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
534
Jean Delvarec2803b92007-09-26 23:39:01 +0200535static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
536 char *buf)
537{
538 struct lm87_data *data = lm87_update_device(dev);
539 int bitnr = to_sensor_dev_attr(attr)->index;
540 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
541}
542static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
543static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
544static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
545static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
546static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
547static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
548static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
549static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
550static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
551static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
552static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
553static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
554static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
555static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
556static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/*
559 * Real code
560 */
561
562static int lm87_attach_adapter(struct i2c_adapter *adapter)
563{
564 if (!(adapter->class & I2C_CLASS_HWMON))
565 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200566 return i2c_probe(adapter, &addr_data, lm87_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200569static struct attribute *lm87_attributes[] = {
570 &dev_attr_in1_input.attr,
571 &dev_attr_in1_min.attr,
572 &dev_attr_in1_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200573 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200574 &dev_attr_in2_input.attr,
575 &dev_attr_in2_min.attr,
576 &dev_attr_in2_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200577 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200578 &dev_attr_in3_input.attr,
579 &dev_attr_in3_min.attr,
580 &dev_attr_in3_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200581 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200582 &dev_attr_in4_input.attr,
583 &dev_attr_in4_min.attr,
584 &dev_attr_in4_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200585 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200586
587 &dev_attr_temp1_input.attr,
588 &dev_attr_temp1_max.attr,
589 &dev_attr_temp1_min.attr,
590 &dev_attr_temp1_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200591 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200592 &dev_attr_temp2_input.attr,
593 &dev_attr_temp2_max.attr,
594 &dev_attr_temp2_min.attr,
595 &dev_attr_temp2_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200596 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
597 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200598
599 &dev_attr_alarms.attr,
600 &dev_attr_aout_output.attr,
601
602 NULL
603};
604
605static const struct attribute_group lm87_group = {
606 .attrs = lm87_attributes,
607};
608
609static struct attribute *lm87_attributes_opt[] = {
610 &dev_attr_in6_input.attr,
611 &dev_attr_in6_min.attr,
612 &dev_attr_in6_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200613 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200614
615 &dev_attr_fan1_input.attr,
616 &dev_attr_fan1_min.attr,
617 &dev_attr_fan1_div.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200618 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200619
620 &dev_attr_in7_input.attr,
621 &dev_attr_in7_min.attr,
622 &dev_attr_in7_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200623 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200624
625 &dev_attr_fan2_input.attr,
626 &dev_attr_fan2_min.attr,
627 &dev_attr_fan2_div.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200628 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200629
630 &dev_attr_temp3_input.attr,
631 &dev_attr_temp3_max.attr,
632 &dev_attr_temp3_min.attr,
633 &dev_attr_temp3_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200634 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
635 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200636
637 &dev_attr_in0_input.attr,
638 &dev_attr_in0_min.attr,
639 &dev_attr_in0_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200640 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200641 &dev_attr_in5_input.attr,
642 &dev_attr_in5_min.attr,
643 &dev_attr_in5_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200644 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200645
646 &dev_attr_cpu0_vid.attr,
647 &dev_attr_vrm.attr,
648
649 NULL
650};
651
652static const struct attribute_group lm87_group_opt = {
653 .attrs = lm87_attributes_opt,
654};
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/*
657 * The following function does more than just detection. If detection
658 * succeeds, it also registers the new chip.
659 */
660static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
661{
662 struct i2c_client *new_client;
663 struct lm87_data *data;
664 int err = 0;
665
666 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
667 goto exit;
668
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200669 if (!(data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 err = -ENOMEM;
671 goto exit;
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /* The common I2C client data is placed right before the
675 LM87-specific data. */
676 new_client = &data->client;
677 i2c_set_clientdata(new_client, data);
678 new_client->addr = address;
679 new_client->adapter = adapter;
680 new_client->driver = &lm87_driver;
681 new_client->flags = 0;
682
683 /* Default to an LM87 if forced */
684 if (kind == 0)
685 kind = lm87;
686
687 /* Now, we do the remaining detection. */
688 if (kind < 0) {
689 u8 rev = lm87_read_value(new_client, LM87_REG_REVISION);
690
691 if (rev < 0x01 || rev > 0x08
692 || (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)
693 || lm87_read_value(new_client, LM87_REG_COMPANY_ID) != 0x02) {
694 dev_dbg(&adapter->dev,
695 "LM87 detection failed at 0x%02x.\n",
696 address);
697 goto exit_free;
698 }
699 }
700
701 /* We can fill in the remaining client fields */
702 strlcpy(new_client->name, "lm87", I2C_NAME_SIZE);
703 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100704 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 /* Tell the I2C layer a new client has arrived */
707 if ((err = i2c_attach_client(new_client)))
708 goto exit_free;
709
710 /* Initialize the LM87 chip */
711 lm87_init_client(new_client);
712
713 data->in_scale[0] = 2500;
714 data->in_scale[1] = 2700;
715 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
716 data->in_scale[3] = 5000;
717 data->in_scale[4] = 12000;
718 data->in_scale[5] = 2700;
719 data->in_scale[6] = 1875;
720 data->in_scale[7] = 1875;
721
722 /* Register sysfs hooks */
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200723 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm87_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400724 goto exit_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 if (data->channel & CHAN_NO_FAN(0)) {
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200727 if ((err = device_create_file(&new_client->dev,
728 &dev_attr_in6_input))
729 || (err = device_create_file(&new_client->dev,
730 &dev_attr_in6_min))
731 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200732 &dev_attr_in6_max))
733 || (err = device_create_file(&new_client->dev,
734 &sensor_dev_attr_in6_alarm.dev_attr)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200735 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 } else {
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200737 if ((err = device_create_file(&new_client->dev,
738 &dev_attr_fan1_input))
739 || (err = device_create_file(&new_client->dev,
740 &dev_attr_fan1_min))
741 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200742 &dev_attr_fan1_div))
743 || (err = device_create_file(&new_client->dev,
744 &sensor_dev_attr_fan1_alarm.dev_attr)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200745 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200748 if (data->channel & CHAN_NO_FAN(1)) {
749 if ((err = device_create_file(&new_client->dev,
750 &dev_attr_in7_input))
751 || (err = device_create_file(&new_client->dev,
752 &dev_attr_in7_min))
753 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200754 &dev_attr_in7_max))
755 || (err = device_create_file(&new_client->dev,
756 &sensor_dev_attr_in7_alarm.dev_attr)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200757 goto exit_remove;
758 } else {
759 if ((err = device_create_file(&new_client->dev,
760 &dev_attr_fan2_input))
761 || (err = device_create_file(&new_client->dev,
762 &dev_attr_fan2_min))
763 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200764 &dev_attr_fan2_div))
765 || (err = device_create_file(&new_client->dev,
766 &sensor_dev_attr_fan2_alarm.dev_attr)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200767 goto exit_remove;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if (data->channel & CHAN_TEMP3) {
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200771 if ((err = device_create_file(&new_client->dev,
772 &dev_attr_temp3_input))
773 || (err = device_create_file(&new_client->dev,
774 &dev_attr_temp3_max))
775 || (err = device_create_file(&new_client->dev,
776 &dev_attr_temp3_min))
777 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200778 &dev_attr_temp3_crit))
779 || (err = device_create_file(&new_client->dev,
780 &sensor_dev_attr_temp3_alarm.dev_attr))
781 || (err = device_create_file(&new_client->dev,
782 &sensor_dev_attr_temp3_fault.dev_attr)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200783 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 } else {
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200785 if ((err = device_create_file(&new_client->dev,
786 &dev_attr_in0_input))
787 || (err = device_create_file(&new_client->dev,
788 &dev_attr_in0_min))
789 || (err = device_create_file(&new_client->dev,
790 &dev_attr_in0_max))
791 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200792 &sensor_dev_attr_in0_alarm.dev_attr))
793 || (err = device_create_file(&new_client->dev,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200794 &dev_attr_in5_input))
795 || (err = device_create_file(&new_client->dev,
796 &dev_attr_in5_min))
797 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200798 &dev_attr_in5_max))
799 || (err = device_create_file(&new_client->dev,
800 &sensor_dev_attr_in5_alarm.dev_attr)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200801 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
804 if (!(data->channel & CHAN_NO_VID)) {
Jean Delvare8a665a02007-05-08 17:21:59 +0200805 data->vrm = vid_which_vrm();
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200806 if ((err = device_create_file(&new_client->dev,
807 &dev_attr_cpu0_vid))
808 || (err = device_create_file(&new_client->dev,
809 &dev_attr_vrm)))
810 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Tony Jones1beeffe2007-08-20 13:46:20 -0700813 data->hwmon_dev = hwmon_device_register(&new_client->dev);
814 if (IS_ERR(data->hwmon_dev)) {
815 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200816 goto exit_remove;
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 return 0;
820
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200821exit_remove:
822 sysfs_remove_group(&new_client->dev.kobj, &lm87_group);
823 sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400824exit_detach:
825 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826exit_free:
827 kfree(data);
828exit:
829 return err;
830}
831
832static void lm87_init_client(struct i2c_client *client)
833{
834 struct lm87_data *data = i2c_get_clientdata(client);
835 u8 config;
836
837 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 config = lm87_read_value(client, LM87_REG_CONFIG);
840 if (!(config & 0x01)) {
841 int i;
842
843 /* Limits are left uninitialized after power-up */
844 for (i = 1; i < 6; i++) {
845 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
846 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
847 }
848 for (i = 0; i < 2; i++) {
849 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
850 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
851 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
852 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
853 }
854 if (data->channel & CHAN_TEMP3) {
855 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
856 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
857 } else {
858 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
859 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
860 }
861 }
862 if ((config & 0x81) != 0x01) {
863 /* Start monitoring */
864 lm87_write_value(client, LM87_REG_CONFIG,
865 (config & 0xF7) | 0x01);
866 }
867}
868
869static int lm87_detach_client(struct i2c_client *client)
870{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400871 struct lm87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 int err;
873
Tony Jones1beeffe2007-08-20 13:46:20 -0700874 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200875 sysfs_remove_group(&client->dev.kobj, &lm87_group);
876 sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400877
Jean Delvare7bef5592005-07-27 22:14:49 +0200878 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400881 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return 0;
883}
884
885static struct lm87_data *lm87_update_device(struct device *dev)
886{
887 struct i2c_client *client = to_i2c_client(dev);
888 struct lm87_data *data = i2c_get_clientdata(client);
889
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100890 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
893 int i, j;
894
895 dev_dbg(&client->dev, "Updating data.\n");
896
897 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
898 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
899 for (; i < j; i++) {
900 data->in[i] = lm87_read_value(client,
901 LM87_REG_IN(i));
902 data->in_min[i] = lm87_read_value(client,
903 LM87_REG_IN_MIN(i));
904 data->in_max[i] = lm87_read_value(client,
905 LM87_REG_IN_MAX(i));
906 }
907
908 for (i = 0; i < 2; i++) {
909 if (data->channel & CHAN_NO_FAN(i)) {
910 data->in[6+i] = lm87_read_value(client,
911 LM87_REG_AIN(i));
912 data->in_max[6+i] = lm87_read_value(client,
913 LM87_REG_AIN_MAX(i));
914 data->in_min[6+i] = lm87_read_value(client,
915 LM87_REG_AIN_MIN(i));
916
917 } else {
918 data->fan[i] = lm87_read_value(client,
919 LM87_REG_FAN(i));
920 data->fan_min[i] = lm87_read_value(client,
921 LM87_REG_FAN_MIN(i));
922 }
923 }
924
925 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
926 for (i = 0 ; i < j; i++) {
927 data->temp[i] = lm87_read_value(client,
928 LM87_REG_TEMP[i]);
929 data->temp_high[i] = lm87_read_value(client,
930 LM87_REG_TEMP_HIGH[i]);
931 data->temp_low[i] = lm87_read_value(client,
932 LM87_REG_TEMP_LOW[i]);
933 }
934
935 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
936 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
937 data->temp_crit_int = min(i, j);
938
939 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
940 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
941 data->temp_crit_ext = min(i, j);
942
943 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
944 data->fan_div[0] = (i >> 4) & 0x03;
945 data->fan_div[1] = (i >> 6) & 0x03;
946 data->vid = (i & 0x0F)
947 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
948 << 4;
949
950 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
951 | (lm87_read_value(client, LM87_REG_ALARMS2)
952 << 8);
953 data->aout = lm87_read_value(client, LM87_REG_AOUT);
954
955 data->last_updated = jiffies;
956 data->valid = 1;
957 }
958
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100959 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 return data;
962}
963
964static int __init sensors_lm87_init(void)
965{
966 return i2c_add_driver(&lm87_driver);
967}
968
969static void __exit sensors_lm87_exit(void)
970{
971 i2c_del_driver(&lm87_driver);
972}
973
974MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
975MODULE_DESCRIPTION("LM87 driver");
976MODULE_LICENSE("GPL");
977
978module_init(sensors_lm87_init);
979module_exit(sensors_lm87_exit);