blob: 3ab4c3f0efdd2f10bd8e3653bc7c66dd3f6f1895 [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>
Jean Delvarec7fa3732007-10-09 15:22:22 +02008 * Copyright (C) 2004,2007 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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 *
Jean Delvarec7fa3732007-10-09 15:22:22 +020040 * This driver also supports the ADM1024, a sensor chip made by Analog
41 * Devices. That chip is fully compatible with the LM87. Complete
42 * datasheet can be obtained from Analog's website at:
43 * http://www.analog.com/en/prod/0,2877,ADM1024,00.html
44 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * This program is free software; you can redistribute it and/or modify
46 * it under the terms of the GNU General Public License as published by
47 * the Free Software Foundation; either version 2 of the License, or
48 * (at your option) any later version.
49 *
50 * This program is distributed in the hope that it will be useful,
51 * but WITHOUT ANY WARRANTY; without even the implied warranty of
52 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53 * GNU General Public License for more details.
54 *
55 * You should have received a copy of the GNU General Public License
56 * along with this program; if not, write to the Free Software
57 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
58 */
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/module.h>
61#include <linux/init.h>
62#include <linux/slab.h>
63#include <linux/jiffies.h>
64#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040065#include <linux/hwmon.h>
Jean Delvarec2803b92007-09-26 23:39:01 +020066#include <linux/hwmon-sysfs.h>
Jean Delvare303760b2005-07-31 21:52:01 +020067#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040068#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010069#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * Addresses to scan
73 * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
74 */
75
76static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * Insmod parameters
80 */
81
Jean Delvarec7fa3732007-10-09 15:22:22 +020082I2C_CLIENT_INSMOD_2(lm87, adm1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
85 * The LM87 registers
86 */
87
88/* nr in 0..5 */
89#define LM87_REG_IN(nr) (0x20 + (nr))
90#define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
91#define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
92/* nr in 0..1 */
93#define LM87_REG_AIN(nr) (0x28 + (nr))
94#define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
95#define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
96
97static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
98static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
99static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
100
101#define LM87_REG_TEMP_HW_INT_LOCK 0x13
102#define LM87_REG_TEMP_HW_EXT_LOCK 0x14
103#define LM87_REG_TEMP_HW_INT 0x17
104#define LM87_REG_TEMP_HW_EXT 0x18
105
106/* nr in 0..1 */
107#define LM87_REG_FAN(nr) (0x28 + (nr))
108#define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
109#define LM87_REG_AOUT 0x19
110
111#define LM87_REG_CONFIG 0x40
112#define LM87_REG_CHANNEL_MODE 0x16
113#define LM87_REG_VID_FAN_DIV 0x47
114#define LM87_REG_VID4 0x49
115
116#define LM87_REG_ALARMS1 0x41
117#define LM87_REG_ALARMS2 0x42
118
119#define LM87_REG_COMPANY_ID 0x3E
120#define LM87_REG_REVISION 0x3F
121
122/*
123 * Conversions and various macros
124 * The LM87 uses signed 8-bit values for temperatures.
125 */
126
127#define IN_FROM_REG(reg,scale) (((reg) * (scale) + 96) / 192)
128#define IN_TO_REG(val,scale) ((val) <= 0 ? 0 : \
129 (val) * 192 >= (scale) * 255 ? 255 : \
130 ((val) * 192 + (scale)/2) / (scale))
131
132#define TEMP_FROM_REG(reg) ((reg) * 1000)
133#define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
134 (val) >= 126500 ? 127 : \
135 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
136
137#define FAN_FROM_REG(reg,div) ((reg) == 255 || (reg) == 0 ? 0 : \
Jean Delvareb965d4b2007-09-26 22:16:56 +0200138 (1350000 + (reg)*(div) / 2) / ((reg)*(div)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define FAN_TO_REG(val,div) ((val)*(div) * 255 <= 1350000 ? 255 : \
140 (1350000 + (val)*(div) / 2) / ((val)*(div)))
141
142#define FAN_DIV_FROM_REG(reg) (1 << (reg))
143
144/* analog out is 9.80mV/LSB */
145#define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
146#define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
147 (val) >= 2500 ? 255 : \
148 ((val) * 10 + 49) / 98)
149
150/* nr in 0..1 */
151#define CHAN_NO_FAN(nr) (1 << (nr))
152#define CHAN_TEMP3 (1 << 2)
153#define CHAN_VCC_5V (1 << 3)
Jean Delvare889af3d2007-10-08 22:48:10 +0200154#define CHAN_NO_VID (1 << 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156/*
157 * Functions declaration
158 */
159
160static int lm87_attach_adapter(struct i2c_adapter *adapter);
161static int lm87_detect(struct i2c_adapter *adapter, int address, int kind);
162static void lm87_init_client(struct i2c_client *client);
163static int lm87_detach_client(struct i2c_client *client);
164static struct lm87_data *lm87_update_device(struct device *dev);
165
166/*
167 * Driver data (common to all clients)
168 */
169
170static struct i2c_driver lm87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100171 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100172 .name = "lm87",
173 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .id = I2C_DRIVERID_LM87,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 .attach_adapter = lm87_attach_adapter,
176 .detach_client = lm87_detach_client,
177};
178
179/*
180 * Client data (each client gets its own)
181 */
182
183struct lm87_data {
184 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700185 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100186 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 char valid; /* zero until following fields are valid */
188 unsigned long last_updated; /* In jiffies */
189
190 u8 channel; /* register value */
191
192 u8 in[8]; /* register value */
193 u8 in_max[8]; /* register value */
194 u8 in_min[8]; /* register value */
195 u16 in_scale[8];
196
197 s8 temp[3]; /* register value */
198 s8 temp_high[3]; /* register value */
199 s8 temp_low[3]; /* register value */
200 s8 temp_crit_int; /* min of two register values */
201 s8 temp_crit_ext; /* min of two register values */
202
203 u8 fan[2]; /* register value */
204 u8 fan_min[2]; /* register value */
205 u8 fan_div[2]; /* register value, shifted right */
206 u8 aout; /* register value */
207
208 u16 alarms; /* register values, combined */
209 u8 vid; /* register values, combined */
210 u8 vrm;
211};
212
213/*
214 * Sysfs stuff
215 */
216
217static inline int lm87_read_value(struct i2c_client *client, u8 reg)
218{
219 return i2c_smbus_read_byte_data(client, reg);
220}
221
222static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
223{
224 return i2c_smbus_write_byte_data(client, reg, value);
225}
226
227#define show_in(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400228static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{ \
230 struct lm87_data *data = lm87_update_device(dev); \
231 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
232 data->in_scale[offset])); \
233} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400234static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{ \
236 struct lm87_data *data = lm87_update_device(dev); \
237 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
238 data->in_scale[offset])); \
239} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400240static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{ \
242 struct lm87_data *data = lm87_update_device(dev); \
243 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
244 data->in_scale[offset])); \
245} \
246static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
247 show_in##offset##_input, NULL);
248show_in(0);
249show_in(1);
250show_in(2);
251show_in(3);
252show_in(4);
253show_in(5);
254show_in(6);
255show_in(7);
256
257static void set_in_min(struct device *dev, const char *buf, int nr)
258{
259 struct i2c_client *client = to_i2c_client(dev);
260 struct lm87_data *data = i2c_get_clientdata(client);
261 long val = simple_strtol(buf, NULL, 10);
262
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100263 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
265 lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
266 LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100267 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270static void set_in_max(struct device *dev, const char *buf, int nr)
271{
272 struct i2c_client *client = to_i2c_client(dev);
273 struct lm87_data *data = i2c_get_clientdata(client);
274 long val = simple_strtol(buf, NULL, 10);
275
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100276 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
278 lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
279 LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100280 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283#define set_in(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400284static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 const char *buf, size_t count) \
286{ \
287 set_in_min(dev, buf, offset); \
288 return count; \
289} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400290static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 const char *buf, size_t count) \
292{ \
293 set_in_max(dev, buf, offset); \
294 return count; \
295} \
296static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
297 show_in##offset##_min, set_in##offset##_min); \
298static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
299 show_in##offset##_max, set_in##offset##_max);
300set_in(0);
301set_in(1);
302set_in(2);
303set_in(3);
304set_in(4);
305set_in(5);
306set_in(6);
307set_in(7);
308
309#define show_temp(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400310static ssize_t show_temp##offset##_input(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[offset-1])); \
314} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400315static ssize_t show_temp##offset##_low(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_low[offset-1])); \
319} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400320static ssize_t show_temp##offset##_high(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{ \
322 struct lm87_data *data = lm87_update_device(dev); \
323 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
324}\
325static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
326 show_temp##offset##_input, NULL);
327show_temp(1);
328show_temp(2);
329show_temp(3);
330
331static void set_temp_low(struct device *dev, const char *buf, int nr)
332{
333 struct i2c_client *client = to_i2c_client(dev);
334 struct lm87_data *data = i2c_get_clientdata(client);
335 long val = simple_strtol(buf, NULL, 10);
336
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100337 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 data->temp_low[nr] = TEMP_TO_REG(val);
339 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100340 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343static void set_temp_high(struct device *dev, const char *buf, int nr)
344{
345 struct i2c_client *client = to_i2c_client(dev);
346 struct lm87_data *data = i2c_get_clientdata(client);
347 long val = simple_strtol(buf, NULL, 10);
348
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100349 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 data->temp_high[nr] = TEMP_TO_REG(val);
351 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100352 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355#define set_temp(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400356static ssize_t set_temp##offset##_low(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 const char *buf, size_t count) \
358{ \
359 set_temp_low(dev, buf, offset-1); \
360 return count; \
361} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400362static ssize_t set_temp##offset##_high(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 const char *buf, size_t count) \
364{ \
365 set_temp_high(dev, buf, offset-1); \
366 return count; \
367} \
368static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
369 show_temp##offset##_high, set_temp##offset##_high); \
370static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
371 show_temp##offset##_low, set_temp##offset##_low);
372set_temp(1);
373set_temp(2);
374set_temp(3);
375
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400376static ssize_t show_temp_crit_int(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct lm87_data *data = lm87_update_device(dev);
379 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
380}
381
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400382static ssize_t show_temp_crit_ext(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct lm87_data *data = lm87_update_device(dev);
385 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
386}
387
388static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
389static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
390static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
391
392#define show_fan(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400393static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{ \
395 struct lm87_data *data = lm87_update_device(dev); \
396 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \
397 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
398} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400399static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{ \
401 struct lm87_data *data = lm87_update_device(dev); \
402 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \
403 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
404} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400405static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{ \
407 struct lm87_data *data = lm87_update_device(dev); \
408 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
409} \
410static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
411 show_fan##offset##_input, NULL);
412show_fan(1);
413show_fan(2);
414
415static void set_fan_min(struct device *dev, const char *buf, int nr)
416{
417 struct i2c_client *client = to_i2c_client(dev);
418 struct lm87_data *data = i2c_get_clientdata(client);
419 long val = simple_strtol(buf, NULL, 10);
420
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100421 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 data->fan_min[nr] = FAN_TO_REG(val,
423 FAN_DIV_FROM_REG(data->fan_div[nr]));
424 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100425 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428/* Note: we save and restore the fan minimum here, because its value is
429 determined in part by the fan clock divider. This follows the principle
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200430 of least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 because the divider changed. */
432static ssize_t set_fan_div(struct device *dev, const char *buf,
433 size_t count, int nr)
434{
435 struct i2c_client *client = to_i2c_client(dev);
436 struct lm87_data *data = i2c_get_clientdata(client);
437 long val = simple_strtol(buf, NULL, 10);
438 unsigned long min;
439 u8 reg;
440
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100441 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 min = FAN_FROM_REG(data->fan_min[nr],
443 FAN_DIV_FROM_REG(data->fan_div[nr]));
444
445 switch (val) {
446 case 1: data->fan_div[nr] = 0; break;
447 case 2: data->fan_div[nr] = 1; break;
448 case 4: data->fan_div[nr] = 2; break;
449 case 8: data->fan_div[nr] = 3; break;
450 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100451 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return -EINVAL;
453 }
454
455 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
456 switch (nr) {
457 case 0:
458 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
459 break;
460 case 1:
461 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
462 break;
463 }
464 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
465
466 data->fan_min[nr] = FAN_TO_REG(min, val);
467 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
468 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100469 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 return count;
472}
473
474#define set_fan(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400475static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 size_t count) \
477{ \
478 set_fan_min(dev, buf, offset-1); \
479 return count; \
480} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400481static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 size_t count) \
483{ \
484 return set_fan_div(dev, buf, count, offset-1); \
485} \
486static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
487 show_fan##offset##_min, set_fan##offset##_min); \
488static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
489 show_fan##offset##_div, set_fan##offset##_div);
490set_fan(1);
491set_fan(2);
492
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400493static ssize_t show_alarms(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", data->alarms);
497}
498static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
499
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400500static ssize_t show_vid(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", vid_from_reg(data->vid, data->vrm));
504}
505static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
506
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400507static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Jean Delvare90d66192007-10-08 18:24:35 +0200509 struct lm87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return sprintf(buf, "%d\n", data->vrm);
511}
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400512static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct i2c_client *client = to_i2c_client(dev);
515 struct lm87_data *data = i2c_get_clientdata(client);
516 data->vrm = simple_strtoul(buf, NULL, 10);
517 return count;
518}
519static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
520
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400521static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct lm87_data *data = lm87_update_device(dev);
524 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
525}
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400526static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct i2c_client *client = to_i2c_client(dev);
529 struct lm87_data *data = i2c_get_clientdata(client);
530 long val = simple_strtol(buf, NULL, 10);
531
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100532 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 data->aout = AOUT_TO_REG(val);
534 lm87_write_value(client, LM87_REG_AOUT, data->aout);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100535 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return count;
537}
538static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
539
Jean Delvarec2803b92007-09-26 23:39:01 +0200540static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
541 char *buf)
542{
543 struct lm87_data *data = lm87_update_device(dev);
544 int bitnr = to_sensor_dev_attr(attr)->index;
545 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
546}
547static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
548static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
549static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
550static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
551static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
552static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
553static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
554static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
555static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
556static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
557static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
558static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
559static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
560static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
561static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/*
564 * Real code
565 */
566
567static int lm87_attach_adapter(struct i2c_adapter *adapter)
568{
569 if (!(adapter->class & I2C_CLASS_HWMON))
570 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200571 return i2c_probe(adapter, &addr_data, lm87_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200574static struct attribute *lm87_attributes[] = {
575 &dev_attr_in1_input.attr,
576 &dev_attr_in1_min.attr,
577 &dev_attr_in1_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200578 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200579 &dev_attr_in2_input.attr,
580 &dev_attr_in2_min.attr,
581 &dev_attr_in2_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200582 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200583 &dev_attr_in3_input.attr,
584 &dev_attr_in3_min.attr,
585 &dev_attr_in3_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200586 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200587 &dev_attr_in4_input.attr,
588 &dev_attr_in4_min.attr,
589 &dev_attr_in4_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200590 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200591
592 &dev_attr_temp1_input.attr,
593 &dev_attr_temp1_max.attr,
594 &dev_attr_temp1_min.attr,
595 &dev_attr_temp1_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200596 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200597 &dev_attr_temp2_input.attr,
598 &dev_attr_temp2_max.attr,
599 &dev_attr_temp2_min.attr,
600 &dev_attr_temp2_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200601 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
602 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200603
604 &dev_attr_alarms.attr,
605 &dev_attr_aout_output.attr,
606
607 NULL
608};
609
610static const struct attribute_group lm87_group = {
611 .attrs = lm87_attributes,
612};
613
614static struct attribute *lm87_attributes_opt[] = {
615 &dev_attr_in6_input.attr,
616 &dev_attr_in6_min.attr,
617 &dev_attr_in6_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200618 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200619
620 &dev_attr_fan1_input.attr,
621 &dev_attr_fan1_min.attr,
622 &dev_attr_fan1_div.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200623 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200624
625 &dev_attr_in7_input.attr,
626 &dev_attr_in7_min.attr,
627 &dev_attr_in7_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200628 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200629
630 &dev_attr_fan2_input.attr,
631 &dev_attr_fan2_min.attr,
632 &dev_attr_fan2_div.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200633 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200634
635 &dev_attr_temp3_input.attr,
636 &dev_attr_temp3_max.attr,
637 &dev_attr_temp3_min.attr,
638 &dev_attr_temp3_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200639 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
640 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200641
642 &dev_attr_in0_input.attr,
643 &dev_attr_in0_min.attr,
644 &dev_attr_in0_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200645 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200646 &dev_attr_in5_input.attr,
647 &dev_attr_in5_min.attr,
648 &dev_attr_in5_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200649 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200650
651 &dev_attr_cpu0_vid.attr,
652 &dev_attr_vrm.attr,
653
654 NULL
655};
656
657static const struct attribute_group lm87_group_opt = {
658 .attrs = lm87_attributes_opt,
659};
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661/*
662 * The following function does more than just detection. If detection
663 * succeeds, it also registers the new chip.
664 */
665static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
666{
667 struct i2c_client *new_client;
668 struct lm87_data *data;
669 int err = 0;
Jean Delvarec7fa3732007-10-09 15:22:22 +0200670 static const char *names[] = { "lm87", "adm1024" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
673 goto exit;
674
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200675 if (!(data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 err = -ENOMEM;
677 goto exit;
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 /* The common I2C client data is placed right before the
681 LM87-specific data. */
682 new_client = &data->client;
683 i2c_set_clientdata(new_client, data);
684 new_client->addr = address;
685 new_client->adapter = adapter;
686 new_client->driver = &lm87_driver;
687 new_client->flags = 0;
688
689 /* Default to an LM87 if forced */
690 if (kind == 0)
691 kind = lm87;
692
693 /* Now, we do the remaining detection. */
694 if (kind < 0) {
Jean Delvarec7fa3732007-10-09 15:22:22 +0200695 u8 cid = lm87_read_value(new_client, LM87_REG_COMPANY_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 u8 rev = lm87_read_value(new_client, LM87_REG_REVISION);
697
Jean Delvarec7fa3732007-10-09 15:22:22 +0200698 if (cid == 0x02 /* National Semiconductor */
699 && (rev >= 0x01 && rev <= 0x08))
700 kind = lm87;
701 else if (cid == 0x41 /* Analog Devices */
702 && (rev & 0xf0) == 0x10)
703 kind = adm1024;
704
705 if (kind < 0
706 || (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 dev_dbg(&adapter->dev,
708 "LM87 detection failed at 0x%02x.\n",
709 address);
710 goto exit_free;
711 }
712 }
713
714 /* We can fill in the remaining client fields */
Jean Delvarec7fa3732007-10-09 15:22:22 +0200715 strlcpy(new_client->name, names[kind - 1], I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100717 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 /* Tell the I2C layer a new client has arrived */
720 if ((err = i2c_attach_client(new_client)))
721 goto exit_free;
722
723 /* Initialize the LM87 chip */
724 lm87_init_client(new_client);
725
726 data->in_scale[0] = 2500;
727 data->in_scale[1] = 2700;
728 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
729 data->in_scale[3] = 5000;
730 data->in_scale[4] = 12000;
731 data->in_scale[5] = 2700;
732 data->in_scale[6] = 1875;
733 data->in_scale[7] = 1875;
734
735 /* Register sysfs hooks */
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200736 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm87_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400737 goto exit_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 if (data->channel & CHAN_NO_FAN(0)) {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200740 if ((err = device_create_file(&new_client->dev,
741 &dev_attr_in6_input))
742 || (err = device_create_file(&new_client->dev,
743 &dev_attr_in6_min))
744 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200745 &dev_attr_in6_max))
746 || (err = device_create_file(&new_client->dev,
747 &sensor_dev_attr_in6_alarm.dev_attr)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200748 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 } else {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200750 if ((err = device_create_file(&new_client->dev,
751 &dev_attr_fan1_input))
752 || (err = device_create_file(&new_client->dev,
753 &dev_attr_fan1_min))
754 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200755 &dev_attr_fan1_div))
756 || (err = device_create_file(&new_client->dev,
757 &sensor_dev_attr_fan1_alarm.dev_attr)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200758 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200761 if (data->channel & CHAN_NO_FAN(1)) {
762 if ((err = device_create_file(&new_client->dev,
763 &dev_attr_in7_input))
764 || (err = device_create_file(&new_client->dev,
765 &dev_attr_in7_min))
766 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200767 &dev_attr_in7_max))
768 || (err = device_create_file(&new_client->dev,
769 &sensor_dev_attr_in7_alarm.dev_attr)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200770 goto exit_remove;
771 } else {
772 if ((err = device_create_file(&new_client->dev,
773 &dev_attr_fan2_input))
774 || (err = device_create_file(&new_client->dev,
775 &dev_attr_fan2_min))
776 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200777 &dev_attr_fan2_div))
778 || (err = device_create_file(&new_client->dev,
779 &sensor_dev_attr_fan2_alarm.dev_attr)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200780 goto exit_remove;
781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 if (data->channel & CHAN_TEMP3) {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200784 if ((err = device_create_file(&new_client->dev,
785 &dev_attr_temp3_input))
786 || (err = device_create_file(&new_client->dev,
787 &dev_attr_temp3_max))
788 || (err = device_create_file(&new_client->dev,
789 &dev_attr_temp3_min))
790 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200791 &dev_attr_temp3_crit))
792 || (err = device_create_file(&new_client->dev,
793 &sensor_dev_attr_temp3_alarm.dev_attr))
794 || (err = device_create_file(&new_client->dev,
795 &sensor_dev_attr_temp3_fault.dev_attr)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200796 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 } else {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200798 if ((err = device_create_file(&new_client->dev,
799 &dev_attr_in0_input))
800 || (err = device_create_file(&new_client->dev,
801 &dev_attr_in0_min))
802 || (err = device_create_file(&new_client->dev,
803 &dev_attr_in0_max))
804 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200805 &sensor_dev_attr_in0_alarm.dev_attr))
806 || (err = device_create_file(&new_client->dev,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200807 &dev_attr_in5_input))
808 || (err = device_create_file(&new_client->dev,
809 &dev_attr_in5_min))
810 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200811 &dev_attr_in5_max))
812 || (err = device_create_file(&new_client->dev,
813 &sensor_dev_attr_in5_alarm.dev_attr)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200814 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
817 if (!(data->channel & CHAN_NO_VID)) {
Jean Delvare8a665a02007-05-08 17:21:59 +0200818 data->vrm = vid_which_vrm();
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200819 if ((err = device_create_file(&new_client->dev,
820 &dev_attr_cpu0_vid))
821 || (err = device_create_file(&new_client->dev,
822 &dev_attr_vrm)))
823 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825
Tony Jones1beeffe2007-08-20 13:46:20 -0700826 data->hwmon_dev = hwmon_device_register(&new_client->dev);
827 if (IS_ERR(data->hwmon_dev)) {
828 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200829 goto exit_remove;
830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 return 0;
833
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200834exit_remove:
835 sysfs_remove_group(&new_client->dev.kobj, &lm87_group);
836 sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400837exit_detach:
838 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839exit_free:
840 kfree(data);
841exit:
842 return err;
843}
844
845static void lm87_init_client(struct i2c_client *client)
846{
847 struct lm87_data *data = i2c_get_clientdata(client);
848 u8 config;
849
850 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 config = lm87_read_value(client, LM87_REG_CONFIG);
853 if (!(config & 0x01)) {
854 int i;
855
856 /* Limits are left uninitialized after power-up */
857 for (i = 1; i < 6; i++) {
858 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
859 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
860 }
861 for (i = 0; i < 2; i++) {
862 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
863 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
864 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
865 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
866 }
867 if (data->channel & CHAN_TEMP3) {
868 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
869 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
870 } else {
871 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
872 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
873 }
874 }
875 if ((config & 0x81) != 0x01) {
876 /* Start monitoring */
877 lm87_write_value(client, LM87_REG_CONFIG,
878 (config & 0xF7) | 0x01);
879 }
880}
881
882static int lm87_detach_client(struct i2c_client *client)
883{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400884 struct lm87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 int err;
886
Tony Jones1beeffe2007-08-20 13:46:20 -0700887 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200888 sysfs_remove_group(&client->dev.kobj, &lm87_group);
889 sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400890
Jean Delvare7bef5592005-07-27 22:14:49 +0200891 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400894 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return 0;
896}
897
898static struct lm87_data *lm87_update_device(struct device *dev)
899{
900 struct i2c_client *client = to_i2c_client(dev);
901 struct lm87_data *data = i2c_get_clientdata(client);
902
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100903 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
906 int i, j;
907
908 dev_dbg(&client->dev, "Updating data.\n");
909
910 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
911 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
912 for (; i < j; i++) {
913 data->in[i] = lm87_read_value(client,
914 LM87_REG_IN(i));
915 data->in_min[i] = lm87_read_value(client,
916 LM87_REG_IN_MIN(i));
917 data->in_max[i] = lm87_read_value(client,
918 LM87_REG_IN_MAX(i));
919 }
920
921 for (i = 0; i < 2; i++) {
922 if (data->channel & CHAN_NO_FAN(i)) {
923 data->in[6+i] = lm87_read_value(client,
924 LM87_REG_AIN(i));
925 data->in_max[6+i] = lm87_read_value(client,
926 LM87_REG_AIN_MAX(i));
927 data->in_min[6+i] = lm87_read_value(client,
928 LM87_REG_AIN_MIN(i));
929
930 } else {
931 data->fan[i] = lm87_read_value(client,
932 LM87_REG_FAN(i));
933 data->fan_min[i] = lm87_read_value(client,
934 LM87_REG_FAN_MIN(i));
935 }
936 }
937
938 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
939 for (i = 0 ; i < j; i++) {
940 data->temp[i] = lm87_read_value(client,
941 LM87_REG_TEMP[i]);
942 data->temp_high[i] = lm87_read_value(client,
943 LM87_REG_TEMP_HIGH[i]);
944 data->temp_low[i] = lm87_read_value(client,
945 LM87_REG_TEMP_LOW[i]);
946 }
947
948 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
949 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
950 data->temp_crit_int = min(i, j);
951
952 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
953 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
954 data->temp_crit_ext = min(i, j);
955
956 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
957 data->fan_div[0] = (i >> 4) & 0x03;
958 data->fan_div[1] = (i >> 6) & 0x03;
959 data->vid = (i & 0x0F)
960 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
961 << 4;
962
963 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
964 | (lm87_read_value(client, LM87_REG_ALARMS2)
965 << 8);
966 data->aout = lm87_read_value(client, LM87_REG_AOUT);
967
968 data->last_updated = jiffies;
969 data->valid = 1;
970 }
971
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100972 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 return data;
975}
976
977static int __init sensors_lm87_init(void)
978{
979 return i2c_add_driver(&lm87_driver);
980}
981
982static void __exit sensors_lm87_exit(void)
983{
984 i2c_del_driver(&lm87_driver);
985}
986
987MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
988MODULE_DESCRIPTION("LM87 driver");
989MODULE_LICENSE("GPL");
990
991module_init(sensors_lm87_init);
992module_exit(sensors_lm87_exit);