blob: 3ce825489e349c0c106646356397aa87eae69531 [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 Delvare303760b2005-07-31 21:52:01 +020061#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040062#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010063#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/*
66 * Addresses to scan
67 * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
68 */
69
70static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/*
73 * Insmod parameters
74 */
75
Jean Delvaref4b50262005-07-31 21:49:03 +020076I2C_CLIENT_INSMOD_1(lm87);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * The LM87 registers
80 */
81
82/* nr in 0..5 */
83#define LM87_REG_IN(nr) (0x20 + (nr))
84#define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
85#define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
86/* nr in 0..1 */
87#define LM87_REG_AIN(nr) (0x28 + (nr))
88#define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
89#define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
90
91static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
92static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
93static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
94
95#define LM87_REG_TEMP_HW_INT_LOCK 0x13
96#define LM87_REG_TEMP_HW_EXT_LOCK 0x14
97#define LM87_REG_TEMP_HW_INT 0x17
98#define LM87_REG_TEMP_HW_EXT 0x18
99
100/* nr in 0..1 */
101#define LM87_REG_FAN(nr) (0x28 + (nr))
102#define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
103#define LM87_REG_AOUT 0x19
104
105#define LM87_REG_CONFIG 0x40
106#define LM87_REG_CHANNEL_MODE 0x16
107#define LM87_REG_VID_FAN_DIV 0x47
108#define LM87_REG_VID4 0x49
109
110#define LM87_REG_ALARMS1 0x41
111#define LM87_REG_ALARMS2 0x42
112
113#define LM87_REG_COMPANY_ID 0x3E
114#define LM87_REG_REVISION 0x3F
115
116/*
117 * Conversions and various macros
118 * The LM87 uses signed 8-bit values for temperatures.
119 */
120
121#define IN_FROM_REG(reg,scale) (((reg) * (scale) + 96) / 192)
122#define IN_TO_REG(val,scale) ((val) <= 0 ? 0 : \
123 (val) * 192 >= (scale) * 255 ? 255 : \
124 ((val) * 192 + (scale)/2) / (scale))
125
126#define TEMP_FROM_REG(reg) ((reg) * 1000)
127#define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
128 (val) >= 126500 ? 127 : \
129 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
130
131#define FAN_FROM_REG(reg,div) ((reg) == 255 || (reg) == 0 ? 0 : \
132 1350000 + (reg)*(div) / 2) / ((reg)*(div))
133#define FAN_TO_REG(val,div) ((val)*(div) * 255 <= 1350000 ? 255 : \
134 (1350000 + (val)*(div) / 2) / ((val)*(div)))
135
136#define FAN_DIV_FROM_REG(reg) (1 << (reg))
137
138/* analog out is 9.80mV/LSB */
139#define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
140#define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
141 (val) >= 2500 ? 255 : \
142 ((val) * 10 + 49) / 98)
143
144/* nr in 0..1 */
145#define CHAN_NO_FAN(nr) (1 << (nr))
146#define CHAN_TEMP3 (1 << 2)
147#define CHAN_VCC_5V (1 << 3)
148#define CHAN_NO_VID (1 << 8)
149
150/*
151 * Functions declaration
152 */
153
154static int lm87_attach_adapter(struct i2c_adapter *adapter);
155static int lm87_detect(struct i2c_adapter *adapter, int address, int kind);
156static void lm87_init_client(struct i2c_client *client);
157static int lm87_detach_client(struct i2c_client *client);
158static struct lm87_data *lm87_update_device(struct device *dev);
159
160/*
161 * Driver data (common to all clients)
162 */
163
164static struct i2c_driver lm87_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100165 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100166 .name = "lm87",
167 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .id = I2C_DRIVERID_LM87,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 .attach_adapter = lm87_attach_adapter,
170 .detach_client = lm87_detach_client,
171};
172
173/*
174 * Client data (each client gets its own)
175 */
176
177struct lm87_data {
178 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400179 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100180 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 char valid; /* zero until following fields are valid */
182 unsigned long last_updated; /* In jiffies */
183
184 u8 channel; /* register value */
185
186 u8 in[8]; /* register value */
187 u8 in_max[8]; /* register value */
188 u8 in_min[8]; /* register value */
189 u16 in_scale[8];
190
191 s8 temp[3]; /* register value */
192 s8 temp_high[3]; /* register value */
193 s8 temp_low[3]; /* register value */
194 s8 temp_crit_int; /* min of two register values */
195 s8 temp_crit_ext; /* min of two register values */
196
197 u8 fan[2]; /* register value */
198 u8 fan_min[2]; /* register value */
199 u8 fan_div[2]; /* register value, shifted right */
200 u8 aout; /* register value */
201
202 u16 alarms; /* register values, combined */
203 u8 vid; /* register values, combined */
204 u8 vrm;
205};
206
207/*
208 * Sysfs stuff
209 */
210
211static inline int lm87_read_value(struct i2c_client *client, u8 reg)
212{
213 return i2c_smbus_read_byte_data(client, reg);
214}
215
216static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
217{
218 return i2c_smbus_write_byte_data(client, reg, value);
219}
220
221#define show_in(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400222static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{ \
224 struct lm87_data *data = lm87_update_device(dev); \
225 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
226 data->in_scale[offset])); \
227} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400228static ssize_t show_in##offset##_min(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_min[offset], \
232 data->in_scale[offset])); \
233} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400234static ssize_t show_in##offset##_max(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_max[offset], \
238 data->in_scale[offset])); \
239} \
240static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
241 show_in##offset##_input, NULL);
242show_in(0);
243show_in(1);
244show_in(2);
245show_in(3);
246show_in(4);
247show_in(5);
248show_in(6);
249show_in(7);
250
251static void set_in_min(struct device *dev, const char *buf, int nr)
252{
253 struct i2c_client *client = to_i2c_client(dev);
254 struct lm87_data *data = i2c_get_clientdata(client);
255 long val = simple_strtol(buf, NULL, 10);
256
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100257 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
259 lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
260 LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100261 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264static void set_in_max(struct device *dev, const char *buf, int nr)
265{
266 struct i2c_client *client = to_i2c_client(dev);
267 struct lm87_data *data = i2c_get_clientdata(client);
268 long val = simple_strtol(buf, NULL, 10);
269
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100270 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
272 lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
273 LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100274 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277#define set_in(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400278static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 const char *buf, size_t count) \
280{ \
281 set_in_min(dev, buf, offset); \
282 return count; \
283} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400284static ssize_t set_in##offset##_max(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_max(dev, buf, offset); \
288 return count; \
289} \
290static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
291 show_in##offset##_min, set_in##offset##_min); \
292static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
293 show_in##offset##_max, set_in##offset##_max);
294set_in(0);
295set_in(1);
296set_in(2);
297set_in(3);
298set_in(4);
299set_in(5);
300set_in(6);
301set_in(7);
302
303#define show_temp(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400304static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{ \
306 struct lm87_data *data = lm87_update_device(dev); \
307 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
308} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400309static ssize_t show_temp##offset##_low(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{ \
311 struct lm87_data *data = lm87_update_device(dev); \
312 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
313} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400314static ssize_t show_temp##offset##_high(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{ \
316 struct lm87_data *data = lm87_update_device(dev); \
317 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
318}\
319static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
320 show_temp##offset##_input, NULL);
321show_temp(1);
322show_temp(2);
323show_temp(3);
324
325static void set_temp_low(struct device *dev, const char *buf, int nr)
326{
327 struct i2c_client *client = to_i2c_client(dev);
328 struct lm87_data *data = i2c_get_clientdata(client);
329 long val = simple_strtol(buf, NULL, 10);
330
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100331 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 data->temp_low[nr] = TEMP_TO_REG(val);
333 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100334 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337static void set_temp_high(struct device *dev, const char *buf, int nr)
338{
339 struct i2c_client *client = to_i2c_client(dev);
340 struct lm87_data *data = i2c_get_clientdata(client);
341 long val = simple_strtol(buf, NULL, 10);
342
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100343 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 data->temp_high[nr] = TEMP_TO_REG(val);
345 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100346 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349#define set_temp(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400350static ssize_t set_temp##offset##_low(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 const char *buf, size_t count) \
352{ \
353 set_temp_low(dev, buf, offset-1); \
354 return count; \
355} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400356static ssize_t set_temp##offset##_high(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_high(dev, buf, offset-1); \
360 return count; \
361} \
362static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
363 show_temp##offset##_high, set_temp##offset##_high); \
364static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
365 show_temp##offset##_low, set_temp##offset##_low);
366set_temp(1);
367set_temp(2);
368set_temp(3);
369
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400370static ssize_t show_temp_crit_int(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct lm87_data *data = lm87_update_device(dev);
373 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
374}
375
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400376static ssize_t show_temp_crit_ext(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_ext));
380}
381
382static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
383static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
384static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
385
386#define show_fan(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400387static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{ \
389 struct lm87_data *data = lm87_update_device(dev); \
390 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \
391 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
392} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400393static ssize_t show_fan##offset##_min(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_min[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##_div(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_DIV_FROM_REG(data->fan_div[offset-1])); \
403} \
404static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
405 show_fan##offset##_input, NULL);
406show_fan(1);
407show_fan(2);
408
409static void set_fan_min(struct device *dev, const char *buf, int nr)
410{
411 struct i2c_client *client = to_i2c_client(dev);
412 struct lm87_data *data = i2c_get_clientdata(client);
413 long val = simple_strtol(buf, NULL, 10);
414
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100415 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 data->fan_min[nr] = FAN_TO_REG(val,
417 FAN_DIV_FROM_REG(data->fan_div[nr]));
418 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100419 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422/* Note: we save and restore the fan minimum here, because its value is
423 determined in part by the fan clock divider. This follows the principle
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200424 of least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 because the divider changed. */
426static ssize_t set_fan_div(struct device *dev, const char *buf,
427 size_t count, int nr)
428{
429 struct i2c_client *client = to_i2c_client(dev);
430 struct lm87_data *data = i2c_get_clientdata(client);
431 long val = simple_strtol(buf, NULL, 10);
432 unsigned long min;
433 u8 reg;
434
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100435 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 min = FAN_FROM_REG(data->fan_min[nr],
437 FAN_DIV_FROM_REG(data->fan_div[nr]));
438
439 switch (val) {
440 case 1: data->fan_div[nr] = 0; break;
441 case 2: data->fan_div[nr] = 1; break;
442 case 4: data->fan_div[nr] = 2; break;
443 case 8: data->fan_div[nr] = 3; break;
444 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100445 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return -EINVAL;
447 }
448
449 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
450 switch (nr) {
451 case 0:
452 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
453 break;
454 case 1:
455 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
456 break;
457 }
458 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
459
460 data->fan_min[nr] = FAN_TO_REG(min, val);
461 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
462 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100463 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 return count;
466}
467
468#define set_fan(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400469static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 size_t count) \
471{ \
472 set_fan_min(dev, buf, offset-1); \
473 return count; \
474} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400475static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 size_t count) \
477{ \
478 return set_fan_div(dev, buf, count, offset-1); \
479} \
480static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
481 show_fan##offset##_min, set_fan##offset##_min); \
482static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
483 show_fan##offset##_div, set_fan##offset##_div);
484set_fan(1);
485set_fan(2);
486
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400487static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct lm87_data *data = lm87_update_device(dev);
490 return sprintf(buf, "%d\n", data->alarms);
491}
492static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
493
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400494static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct lm87_data *data = lm87_update_device(dev);
497 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
498}
499static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
500
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400501static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct lm87_data *data = lm87_update_device(dev);
504 return sprintf(buf, "%d\n", data->vrm);
505}
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400506static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 struct i2c_client *client = to_i2c_client(dev);
509 struct lm87_data *data = i2c_get_clientdata(client);
510 data->vrm = simple_strtoul(buf, NULL, 10);
511 return count;
512}
513static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
514
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400515static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 struct lm87_data *data = lm87_update_device(dev);
518 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
519}
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400520static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 struct i2c_client *client = to_i2c_client(dev);
523 struct lm87_data *data = i2c_get_clientdata(client);
524 long val = simple_strtol(buf, NULL, 10);
525
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100526 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 data->aout = AOUT_TO_REG(val);
528 lm87_write_value(client, LM87_REG_AOUT, data->aout);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100529 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return count;
531}
532static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
533
534/*
535 * Real code
536 */
537
538static int lm87_attach_adapter(struct i2c_adapter *adapter)
539{
540 if (!(adapter->class & I2C_CLASS_HWMON))
541 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200542 return i2c_probe(adapter, &addr_data, lm87_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200545static struct attribute *lm87_attributes[] = {
546 &dev_attr_in1_input.attr,
547 &dev_attr_in1_min.attr,
548 &dev_attr_in1_max.attr,
549 &dev_attr_in2_input.attr,
550 &dev_attr_in2_min.attr,
551 &dev_attr_in2_max.attr,
552 &dev_attr_in3_input.attr,
553 &dev_attr_in3_min.attr,
554 &dev_attr_in3_max.attr,
555 &dev_attr_in4_input.attr,
556 &dev_attr_in4_min.attr,
557 &dev_attr_in4_max.attr,
558
559 &dev_attr_temp1_input.attr,
560 &dev_attr_temp1_max.attr,
561 &dev_attr_temp1_min.attr,
562 &dev_attr_temp1_crit.attr,
563 &dev_attr_temp2_input.attr,
564 &dev_attr_temp2_max.attr,
565 &dev_attr_temp2_min.attr,
566 &dev_attr_temp2_crit.attr,
567
568 &dev_attr_alarms.attr,
569 &dev_attr_aout_output.attr,
570
571 NULL
572};
573
574static const struct attribute_group lm87_group = {
575 .attrs = lm87_attributes,
576};
577
578static struct attribute *lm87_attributes_opt[] = {
579 &dev_attr_in6_input.attr,
580 &dev_attr_in6_min.attr,
581 &dev_attr_in6_max.attr,
582
583 &dev_attr_fan1_input.attr,
584 &dev_attr_fan1_min.attr,
585 &dev_attr_fan1_div.attr,
586
587 &dev_attr_in7_input.attr,
588 &dev_attr_in7_min.attr,
589 &dev_attr_in7_max.attr,
590
591 &dev_attr_fan2_input.attr,
592 &dev_attr_fan2_min.attr,
593 &dev_attr_fan2_div.attr,
594
595 &dev_attr_temp3_input.attr,
596 &dev_attr_temp3_max.attr,
597 &dev_attr_temp3_min.attr,
598 &dev_attr_temp3_crit.attr,
599
600 &dev_attr_in0_input.attr,
601 &dev_attr_in0_min.attr,
602 &dev_attr_in0_max.attr,
603 &dev_attr_in5_input.attr,
604 &dev_attr_in5_min.attr,
605 &dev_attr_in5_max.attr,
606
607 &dev_attr_cpu0_vid.attr,
608 &dev_attr_vrm.attr,
609
610 NULL
611};
612
613static const struct attribute_group lm87_group_opt = {
614 .attrs = lm87_attributes_opt,
615};
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617/*
618 * The following function does more than just detection. If detection
619 * succeeds, it also registers the new chip.
620 */
621static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
622{
623 struct i2c_client *new_client;
624 struct lm87_data *data;
625 int err = 0;
626
627 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
628 goto exit;
629
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200630 if (!(data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 err = -ENOMEM;
632 goto exit;
633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /* The common I2C client data is placed right before the
636 LM87-specific data. */
637 new_client = &data->client;
638 i2c_set_clientdata(new_client, data);
639 new_client->addr = address;
640 new_client->adapter = adapter;
641 new_client->driver = &lm87_driver;
642 new_client->flags = 0;
643
644 /* Default to an LM87 if forced */
645 if (kind == 0)
646 kind = lm87;
647
648 /* Now, we do the remaining detection. */
649 if (kind < 0) {
650 u8 rev = lm87_read_value(new_client, LM87_REG_REVISION);
651
652 if (rev < 0x01 || rev > 0x08
653 || (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)
654 || lm87_read_value(new_client, LM87_REG_COMPANY_ID) != 0x02) {
655 dev_dbg(&adapter->dev,
656 "LM87 detection failed at 0x%02x.\n",
657 address);
658 goto exit_free;
659 }
660 }
661
662 /* We can fill in the remaining client fields */
663 strlcpy(new_client->name, "lm87", I2C_NAME_SIZE);
664 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100665 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* Tell the I2C layer a new client has arrived */
668 if ((err = i2c_attach_client(new_client)))
669 goto exit_free;
670
671 /* Initialize the LM87 chip */
672 lm87_init_client(new_client);
673
674 data->in_scale[0] = 2500;
675 data->in_scale[1] = 2700;
676 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
677 data->in_scale[3] = 5000;
678 data->in_scale[4] = 12000;
679 data->in_scale[5] = 2700;
680 data->in_scale[6] = 1875;
681 data->in_scale[7] = 1875;
682
683 /* Register sysfs hooks */
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200684 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm87_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400685 goto exit_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (data->channel & CHAN_NO_FAN(0)) {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200688 if ((err = device_create_file(&new_client->dev,
689 &dev_attr_in6_input))
690 || (err = device_create_file(&new_client->dev,
691 &dev_attr_in6_min))
692 || (err = device_create_file(&new_client->dev,
693 &dev_attr_in6_max)))
694 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 } else {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200696 if ((err = device_create_file(&new_client->dev,
697 &dev_attr_fan1_input))
698 || (err = device_create_file(&new_client->dev,
699 &dev_attr_fan1_min))
700 || (err = device_create_file(&new_client->dev,
701 &dev_attr_fan1_div)))
702 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200705 if (data->channel & CHAN_NO_FAN(1)) {
706 if ((err = device_create_file(&new_client->dev,
707 &dev_attr_in7_input))
708 || (err = device_create_file(&new_client->dev,
709 &dev_attr_in7_min))
710 || (err = device_create_file(&new_client->dev,
711 &dev_attr_in7_max)))
712 goto exit_remove;
713 } else {
714 if ((err = device_create_file(&new_client->dev,
715 &dev_attr_fan2_input))
716 || (err = device_create_file(&new_client->dev,
717 &dev_attr_fan2_min))
718 || (err = device_create_file(&new_client->dev,
719 &dev_attr_fan2_div)))
720 goto exit_remove;
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (data->channel & CHAN_TEMP3) {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200724 if ((err = device_create_file(&new_client->dev,
725 &dev_attr_temp3_input))
726 || (err = device_create_file(&new_client->dev,
727 &dev_attr_temp3_max))
728 || (err = device_create_file(&new_client->dev,
729 &dev_attr_temp3_min))
730 || (err = device_create_file(&new_client->dev,
731 &dev_attr_temp3_crit)))
732 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 } else {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200734 if ((err = device_create_file(&new_client->dev,
735 &dev_attr_in0_input))
736 || (err = device_create_file(&new_client->dev,
737 &dev_attr_in0_min))
738 || (err = device_create_file(&new_client->dev,
739 &dev_attr_in0_max))
740 || (err = device_create_file(&new_client->dev,
741 &dev_attr_in5_input))
742 || (err = device_create_file(&new_client->dev,
743 &dev_attr_in5_min))
744 || (err = device_create_file(&new_client->dev,
745 &dev_attr_in5_max)))
746 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
749 if (!(data->channel & CHAN_NO_VID)) {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200750 if ((err = device_create_file(&new_client->dev,
751 &dev_attr_cpu0_vid))
752 || (err = device_create_file(&new_client->dev,
753 &dev_attr_vrm)))
754 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200757 data->class_dev = hwmon_device_register(&new_client->dev);
758 if (IS_ERR(data->class_dev)) {
759 err = PTR_ERR(data->class_dev);
760 goto exit_remove;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 return 0;
764
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200765exit_remove:
766 sysfs_remove_group(&new_client->dev.kobj, &lm87_group);
767 sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400768exit_detach:
769 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770exit_free:
771 kfree(data);
772exit:
773 return err;
774}
775
776static void lm87_init_client(struct i2c_client *client)
777{
778 struct lm87_data *data = i2c_get_clientdata(client);
779 u8 config;
780
781 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
Jean Delvare303760b2005-07-31 21:52:01 +0200782 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 config = lm87_read_value(client, LM87_REG_CONFIG);
785 if (!(config & 0x01)) {
786 int i;
787
788 /* Limits are left uninitialized after power-up */
789 for (i = 1; i < 6; i++) {
790 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
791 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
792 }
793 for (i = 0; i < 2; i++) {
794 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
795 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
796 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
797 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
798 }
799 if (data->channel & CHAN_TEMP3) {
800 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
801 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
802 } else {
803 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
804 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
805 }
806 }
807 if ((config & 0x81) != 0x01) {
808 /* Start monitoring */
809 lm87_write_value(client, LM87_REG_CONFIG,
810 (config & 0xF7) | 0x01);
811 }
812}
813
814static int lm87_detach_client(struct i2c_client *client)
815{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400816 struct lm87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int err;
818
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400819 hwmon_device_unregister(data->class_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200820 sysfs_remove_group(&client->dev.kobj, &lm87_group);
821 sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400822
Jean Delvare7bef5592005-07-27 22:14:49 +0200823 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400826 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return 0;
828}
829
830static struct lm87_data *lm87_update_device(struct device *dev)
831{
832 struct i2c_client *client = to_i2c_client(dev);
833 struct lm87_data *data = i2c_get_clientdata(client);
834
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100835 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
838 int i, j;
839
840 dev_dbg(&client->dev, "Updating data.\n");
841
842 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
843 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
844 for (; i < j; i++) {
845 data->in[i] = lm87_read_value(client,
846 LM87_REG_IN(i));
847 data->in_min[i] = lm87_read_value(client,
848 LM87_REG_IN_MIN(i));
849 data->in_max[i] = lm87_read_value(client,
850 LM87_REG_IN_MAX(i));
851 }
852
853 for (i = 0; i < 2; i++) {
854 if (data->channel & CHAN_NO_FAN(i)) {
855 data->in[6+i] = lm87_read_value(client,
856 LM87_REG_AIN(i));
857 data->in_max[6+i] = lm87_read_value(client,
858 LM87_REG_AIN_MAX(i));
859 data->in_min[6+i] = lm87_read_value(client,
860 LM87_REG_AIN_MIN(i));
861
862 } else {
863 data->fan[i] = lm87_read_value(client,
864 LM87_REG_FAN(i));
865 data->fan_min[i] = lm87_read_value(client,
866 LM87_REG_FAN_MIN(i));
867 }
868 }
869
870 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
871 for (i = 0 ; i < j; i++) {
872 data->temp[i] = lm87_read_value(client,
873 LM87_REG_TEMP[i]);
874 data->temp_high[i] = lm87_read_value(client,
875 LM87_REG_TEMP_HIGH[i]);
876 data->temp_low[i] = lm87_read_value(client,
877 LM87_REG_TEMP_LOW[i]);
878 }
879
880 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
881 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
882 data->temp_crit_int = min(i, j);
883
884 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
885 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
886 data->temp_crit_ext = min(i, j);
887
888 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
889 data->fan_div[0] = (i >> 4) & 0x03;
890 data->fan_div[1] = (i >> 6) & 0x03;
891 data->vid = (i & 0x0F)
892 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
893 << 4;
894
895 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
896 | (lm87_read_value(client, LM87_REG_ALARMS2)
897 << 8);
898 data->aout = lm87_read_value(client, LM87_REG_AOUT);
899
900 data->last_updated = jiffies;
901 data->valid = 1;
902 }
903
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100904 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 return data;
907}
908
909static int __init sensors_lm87_init(void)
910{
911 return i2c_add_driver(&lm87_driver);
912}
913
914static void __exit sensors_lm87_exit(void)
915{
916 i2c_del_driver(&lm87_driver);
917}
918
919MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
920MODULE_DESCRIPTION("LM87 driver");
921MODULE_LICENSE("GPL");
922
923module_init(sensors_lm87_init);
924module_exit(sensors_lm87_exit);