Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
c7fa3737 | 8 | * Copyright (C) 2004,2007 Jean Delvare <khali@linux-fr.org> |
1da177e4 LT |
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 | * | |
c7fa3737 JD |
40 | * 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 | * | |
1da177e4 LT |
45 | * 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 | ||
1da177e4 LT |
60 | #include <linux/module.h> |
61 | #include <linux/init.h> | |
62 | #include <linux/slab.h> | |
63 | #include <linux/jiffies.h> | |
64 | #include <linux/i2c.h> | |
943b0830 | 65 | #include <linux/hwmon.h> |
c2803b98 | 66 | #include <linux/hwmon-sysfs.h> |
303760b4 | 67 | #include <linux/hwmon-vid.h> |
943b0830 | 68 | #include <linux/err.h> |
9a61bf63 | 69 | #include <linux/mutex.h> |
1da177e4 LT |
70 | |
71 | /* | |
72 | * Addresses to scan | |
73 | * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e. | |
74 | */ | |
75 | ||
76 | static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; | |
1da177e4 LT |
77 | |
78 | /* | |
79 | * Insmod parameters | |
80 | */ | |
81 | ||
c7fa3737 | 82 | I2C_CLIENT_INSMOD_2(lm87, adm1024); |
1da177e4 LT |
83 | |
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 | ||
97 | static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 }; | |
98 | static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B }; | |
99 | static 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 : \ | |
b965d4b7 | 138 | (1350000 + (reg)*(div) / 2) / ((reg)*(div))) |
1da177e4 LT |
139 | #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) | |
889af3d5 | 154 | #define CHAN_NO_VID (1 << 7) |
1da177e4 LT |
155 | |
156 | /* | |
157 | * Functions declaration | |
158 | */ | |
159 | ||
160 | static int lm87_attach_adapter(struct i2c_adapter *adapter); | |
161 | static int lm87_detect(struct i2c_adapter *adapter, int address, int kind); | |
162 | static void lm87_init_client(struct i2c_client *client); | |
163 | static int lm87_detach_client(struct i2c_client *client); | |
164 | static struct lm87_data *lm87_update_device(struct device *dev); | |
165 | ||
166 | /* | |
167 | * Driver data (common to all clients) | |
168 | */ | |
169 | ||
170 | static struct i2c_driver lm87_driver = { | |
cdaf7934 | 171 | .driver = { |
cdaf7934 LR |
172 | .name = "lm87", |
173 | }, | |
1da177e4 LT |
174 | .attach_adapter = lm87_attach_adapter, |
175 | .detach_client = lm87_detach_client, | |
176 | }; | |
177 | ||
178 | /* | |
179 | * Client data (each client gets its own) | |
180 | */ | |
181 | ||
182 | struct lm87_data { | |
183 | struct i2c_client client; | |
1beeffe4 | 184 | struct device *hwmon_dev; |
9a61bf63 | 185 | struct mutex update_lock; |
1da177e4 LT |
186 | char valid; /* zero until following fields are valid */ |
187 | unsigned long last_updated; /* In jiffies */ | |
188 | ||
189 | u8 channel; /* register value */ | |
190 | ||
191 | u8 in[8]; /* register value */ | |
192 | u8 in_max[8]; /* register value */ | |
193 | u8 in_min[8]; /* register value */ | |
194 | u16 in_scale[8]; | |
195 | ||
196 | s8 temp[3]; /* register value */ | |
197 | s8 temp_high[3]; /* register value */ | |
198 | s8 temp_low[3]; /* register value */ | |
199 | s8 temp_crit_int; /* min of two register values */ | |
200 | s8 temp_crit_ext; /* min of two register values */ | |
201 | ||
202 | u8 fan[2]; /* register value */ | |
203 | u8 fan_min[2]; /* register value */ | |
204 | u8 fan_div[2]; /* register value, shifted right */ | |
205 | u8 aout; /* register value */ | |
206 | ||
207 | u16 alarms; /* register values, combined */ | |
208 | u8 vid; /* register values, combined */ | |
209 | u8 vrm; | |
210 | }; | |
211 | ||
212 | /* | |
213 | * Sysfs stuff | |
214 | */ | |
215 | ||
216 | static inline int lm87_read_value(struct i2c_client *client, u8 reg) | |
217 | { | |
218 | return i2c_smbus_read_byte_data(client, reg); | |
219 | } | |
220 | ||
221 | static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value) | |
222 | { | |
223 | return i2c_smbus_write_byte_data(client, reg, value); | |
224 | } | |
225 | ||
226 | #define show_in(offset) \ | |
8627f9ba | 227 | static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
228 | { \ |
229 | struct lm87_data *data = lm87_update_device(dev); \ | |
230 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \ | |
231 | data->in_scale[offset])); \ | |
232 | } \ | |
8627f9ba | 233 | static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
234 | { \ |
235 | struct lm87_data *data = lm87_update_device(dev); \ | |
236 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \ | |
237 | data->in_scale[offset])); \ | |
238 | } \ | |
8627f9ba | 239 | static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
240 | { \ |
241 | struct lm87_data *data = lm87_update_device(dev); \ | |
242 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \ | |
243 | data->in_scale[offset])); \ | |
244 | } \ | |
245 | static DEVICE_ATTR(in##offset##_input, S_IRUGO, \ | |
246 | show_in##offset##_input, NULL); | |
247 | show_in(0); | |
248 | show_in(1); | |
249 | show_in(2); | |
250 | show_in(3); | |
251 | show_in(4); | |
252 | show_in(5); | |
253 | show_in(6); | |
254 | show_in(7); | |
255 | ||
256 | static void set_in_min(struct device *dev, const char *buf, int nr) | |
257 | { | |
258 | struct i2c_client *client = to_i2c_client(dev); | |
259 | struct lm87_data *data = i2c_get_clientdata(client); | |
260 | long val = simple_strtol(buf, NULL, 10); | |
261 | ||
9a61bf63 | 262 | mutex_lock(&data->update_lock); |
1da177e4 LT |
263 | data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]); |
264 | lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) : | |
265 | LM87_REG_AIN_MIN(nr-6), data->in_min[nr]); | |
9a61bf63 | 266 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
267 | } |
268 | ||
269 | static void set_in_max(struct device *dev, const char *buf, int nr) | |
270 | { | |
271 | struct i2c_client *client = to_i2c_client(dev); | |
272 | struct lm87_data *data = i2c_get_clientdata(client); | |
273 | long val = simple_strtol(buf, NULL, 10); | |
274 | ||
9a61bf63 | 275 | mutex_lock(&data->update_lock); |
1da177e4 LT |
276 | data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]); |
277 | lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) : | |
278 | LM87_REG_AIN_MAX(nr-6), data->in_max[nr]); | |
9a61bf63 | 279 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
280 | } |
281 | ||
282 | #define set_in(offset) \ | |
8627f9ba | 283 | static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, \ |
1da177e4 LT |
284 | const char *buf, size_t count) \ |
285 | { \ | |
286 | set_in_min(dev, buf, offset); \ | |
287 | return count; \ | |
288 | } \ | |
8627f9ba | 289 | static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, \ |
1da177e4 LT |
290 | const char *buf, size_t count) \ |
291 | { \ | |
292 | set_in_max(dev, buf, offset); \ | |
293 | return count; \ | |
294 | } \ | |
295 | static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ | |
296 | show_in##offset##_min, set_in##offset##_min); \ | |
297 | static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ | |
298 | show_in##offset##_max, set_in##offset##_max); | |
299 | set_in(0); | |
300 | set_in(1); | |
301 | set_in(2); | |
302 | set_in(3); | |
303 | set_in(4); | |
304 | set_in(5); | |
305 | set_in(6); | |
306 | set_in(7); | |
307 | ||
308 | #define show_temp(offset) \ | |
8627f9ba | 309 | static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
310 | { \ |
311 | struct lm87_data *data = lm87_update_device(dev); \ | |
312 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \ | |
313 | } \ | |
8627f9ba | 314 | static ssize_t show_temp##offset##_low(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
315 | { \ |
316 | struct lm87_data *data = lm87_update_device(dev); \ | |
317 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \ | |
318 | } \ | |
8627f9ba | 319 | static ssize_t show_temp##offset##_high(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
320 | { \ |
321 | struct lm87_data *data = lm87_update_device(dev); \ | |
322 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \ | |
323 | }\ | |
324 | static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ | |
325 | show_temp##offset##_input, NULL); | |
326 | show_temp(1); | |
327 | show_temp(2); | |
328 | show_temp(3); | |
329 | ||
330 | static void set_temp_low(struct device *dev, const char *buf, int nr) | |
331 | { | |
332 | struct i2c_client *client = to_i2c_client(dev); | |
333 | struct lm87_data *data = i2c_get_clientdata(client); | |
334 | long val = simple_strtol(buf, NULL, 10); | |
335 | ||
9a61bf63 | 336 | mutex_lock(&data->update_lock); |
1da177e4 LT |
337 | data->temp_low[nr] = TEMP_TO_REG(val); |
338 | lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]); | |
9a61bf63 | 339 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
340 | } |
341 | ||
342 | static void set_temp_high(struct device *dev, const char *buf, int nr) | |
343 | { | |
344 | struct i2c_client *client = to_i2c_client(dev); | |
345 | struct lm87_data *data = i2c_get_clientdata(client); | |
346 | long val = simple_strtol(buf, NULL, 10); | |
347 | ||
9a61bf63 | 348 | mutex_lock(&data->update_lock); |
1da177e4 LT |
349 | data->temp_high[nr] = TEMP_TO_REG(val); |
350 | lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]); | |
9a61bf63 | 351 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
352 | } |
353 | ||
354 | #define set_temp(offset) \ | |
8627f9ba | 355 | static ssize_t set_temp##offset##_low(struct device *dev, struct device_attribute *attr, \ |
1da177e4 LT |
356 | const char *buf, size_t count) \ |
357 | { \ | |
358 | set_temp_low(dev, buf, offset-1); \ | |
359 | return count; \ | |
360 | } \ | |
8627f9ba | 361 | static ssize_t set_temp##offset##_high(struct device *dev, struct device_attribute *attr, \ |
1da177e4 LT |
362 | const char *buf, size_t count) \ |
363 | { \ | |
364 | set_temp_high(dev, buf, offset-1); \ | |
365 | return count; \ | |
366 | } \ | |
367 | static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ | |
368 | show_temp##offset##_high, set_temp##offset##_high); \ | |
369 | static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ | |
370 | show_temp##offset##_low, set_temp##offset##_low); | |
371 | set_temp(1); | |
372 | set_temp(2); | |
373 | set_temp(3); | |
374 | ||
8627f9ba | 375 | static ssize_t show_temp_crit_int(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
376 | { |
377 | struct lm87_data *data = lm87_update_device(dev); | |
378 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int)); | |
379 | } | |
380 | ||
8627f9ba | 381 | static ssize_t show_temp_crit_ext(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
382 | { |
383 | struct lm87_data *data = lm87_update_device(dev); | |
384 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext)); | |
385 | } | |
386 | ||
387 | static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL); | |
388 | static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL); | |
389 | static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL); | |
390 | ||
391 | #define show_fan(offset) \ | |
8627f9ba | 392 | static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
393 | { \ |
394 | struct lm87_data *data = lm87_update_device(dev); \ | |
395 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \ | |
396 | FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \ | |
397 | } \ | |
8627f9ba | 398 | static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
399 | { \ |
400 | struct lm87_data *data = lm87_update_device(dev); \ | |
401 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \ | |
402 | FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \ | |
403 | } \ | |
8627f9ba | 404 | static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
405 | { \ |
406 | struct lm87_data *data = lm87_update_device(dev); \ | |
407 | return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \ | |
408 | } \ | |
409 | static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ | |
410 | show_fan##offset##_input, NULL); | |
411 | show_fan(1); | |
412 | show_fan(2); | |
413 | ||
414 | static void set_fan_min(struct device *dev, const char *buf, int nr) | |
415 | { | |
416 | struct i2c_client *client = to_i2c_client(dev); | |
417 | struct lm87_data *data = i2c_get_clientdata(client); | |
418 | long val = simple_strtol(buf, NULL, 10); | |
419 | ||
9a61bf63 | 420 | mutex_lock(&data->update_lock); |
1da177e4 LT |
421 | data->fan_min[nr] = FAN_TO_REG(val, |
422 | FAN_DIV_FROM_REG(data->fan_div[nr])); | |
423 | lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]); | |
9a61bf63 | 424 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
425 | } |
426 | ||
427 | /* Note: we save and restore the fan minimum here, because its value is | |
428 | determined in part by the fan clock divider. This follows the principle | |
d6e05edc | 429 | of least surprise; the user doesn't expect the fan minimum to change just |
1da177e4 LT |
430 | because the divider changed. */ |
431 | static ssize_t set_fan_div(struct device *dev, const char *buf, | |
432 | size_t count, int nr) | |
433 | { | |
434 | struct i2c_client *client = to_i2c_client(dev); | |
435 | struct lm87_data *data = i2c_get_clientdata(client); | |
436 | long val = simple_strtol(buf, NULL, 10); | |
437 | unsigned long min; | |
438 | u8 reg; | |
439 | ||
9a61bf63 | 440 | mutex_lock(&data->update_lock); |
1da177e4 LT |
441 | min = FAN_FROM_REG(data->fan_min[nr], |
442 | FAN_DIV_FROM_REG(data->fan_div[nr])); | |
443 | ||
444 | switch (val) { | |
445 | case 1: data->fan_div[nr] = 0; break; | |
446 | case 2: data->fan_div[nr] = 1; break; | |
447 | case 4: data->fan_div[nr] = 2; break; | |
448 | case 8: data->fan_div[nr] = 3; break; | |
449 | default: | |
9a61bf63 | 450 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
451 | return -EINVAL; |
452 | } | |
453 | ||
454 | reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV); | |
455 | switch (nr) { | |
456 | case 0: | |
457 | reg = (reg & 0xCF) | (data->fan_div[0] << 4); | |
458 | break; | |
459 | case 1: | |
460 | reg = (reg & 0x3F) | (data->fan_div[1] << 6); | |
461 | break; | |
462 | } | |
463 | lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg); | |
464 | ||
465 | data->fan_min[nr] = FAN_TO_REG(min, val); | |
466 | lm87_write_value(client, LM87_REG_FAN_MIN(nr), | |
467 | data->fan_min[nr]); | |
9a61bf63 | 468 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
469 | |
470 | return count; | |
471 | } | |
472 | ||
473 | #define set_fan(offset) \ | |
8627f9ba | 474 | static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \ |
1da177e4 LT |
475 | size_t count) \ |
476 | { \ | |
477 | set_fan_min(dev, buf, offset-1); \ | |
478 | return count; \ | |
479 | } \ | |
8627f9ba | 480 | static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \ |
1da177e4 LT |
481 | size_t count) \ |
482 | { \ | |
483 | return set_fan_div(dev, buf, count, offset-1); \ | |
484 | } \ | |
485 | static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ | |
486 | show_fan##offset##_min, set_fan##offset##_min); \ | |
487 | static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ | |
488 | show_fan##offset##_div, set_fan##offset##_div); | |
489 | set_fan(1); | |
490 | set_fan(2); | |
491 | ||
8627f9ba | 492 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
493 | { |
494 | struct lm87_data *data = lm87_update_device(dev); | |
495 | return sprintf(buf, "%d\n", data->alarms); | |
496 | } | |
497 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | |
498 | ||
8627f9ba | 499 | static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
500 | { |
501 | struct lm87_data *data = lm87_update_device(dev); | |
502 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); | |
503 | } | |
504 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); | |
505 | ||
8627f9ba | 506 | static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 | 507 | { |
90d6619a | 508 | struct lm87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
509 | return sprintf(buf, "%d\n", data->vrm); |
510 | } | |
8627f9ba | 511 | static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 LT |
512 | { |
513 | struct i2c_client *client = to_i2c_client(dev); | |
514 | struct lm87_data *data = i2c_get_clientdata(client); | |
515 | data->vrm = simple_strtoul(buf, NULL, 10); | |
516 | return count; | |
517 | } | |
518 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm); | |
519 | ||
8627f9ba | 520 | static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
521 | { |
522 | struct lm87_data *data = lm87_update_device(dev); | |
523 | return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); | |
524 | } | |
8627f9ba | 525 | static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 LT |
526 | { |
527 | struct i2c_client *client = to_i2c_client(dev); | |
528 | struct lm87_data *data = i2c_get_clientdata(client); | |
529 | long val = simple_strtol(buf, NULL, 10); | |
530 | ||
9a61bf63 | 531 | mutex_lock(&data->update_lock); |
1da177e4 LT |
532 | data->aout = AOUT_TO_REG(val); |
533 | lm87_write_value(client, LM87_REG_AOUT, data->aout); | |
9a61bf63 | 534 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
535 | return count; |
536 | } | |
537 | static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout); | |
538 | ||
c2803b98 JD |
539 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
540 | char *buf) | |
541 | { | |
542 | struct lm87_data *data = lm87_update_device(dev); | |
543 | int bitnr = to_sensor_dev_attr(attr)->index; | |
544 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); | |
545 | } | |
546 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); | |
547 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); | |
548 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); | |
549 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); | |
550 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); | |
551 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9); | |
552 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6); | |
553 | static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7); | |
554 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); | |
555 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5); | |
556 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5); | |
557 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); | |
558 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); | |
559 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14); | |
560 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15); | |
561 | ||
1da177e4 LT |
562 | /* |
563 | * Real code | |
564 | */ | |
565 | ||
566 | static int lm87_attach_adapter(struct i2c_adapter *adapter) | |
567 | { | |
568 | if (!(adapter->class & I2C_CLASS_HWMON)) | |
569 | return 0; | |
2ed2dc3c | 570 | return i2c_probe(adapter, &addr_data, lm87_detect); |
1da177e4 LT |
571 | } |
572 | ||
0501a381 MH |
573 | static struct attribute *lm87_attributes[] = { |
574 | &dev_attr_in1_input.attr, | |
575 | &dev_attr_in1_min.attr, | |
576 | &dev_attr_in1_max.attr, | |
c2803b98 | 577 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
0501a381 MH |
578 | &dev_attr_in2_input.attr, |
579 | &dev_attr_in2_min.attr, | |
580 | &dev_attr_in2_max.attr, | |
c2803b98 | 581 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
0501a381 MH |
582 | &dev_attr_in3_input.attr, |
583 | &dev_attr_in3_min.attr, | |
584 | &dev_attr_in3_max.attr, | |
c2803b98 | 585 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
0501a381 MH |
586 | &dev_attr_in4_input.attr, |
587 | &dev_attr_in4_min.attr, | |
588 | &dev_attr_in4_max.attr, | |
c2803b98 | 589 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
0501a381 MH |
590 | |
591 | &dev_attr_temp1_input.attr, | |
592 | &dev_attr_temp1_max.attr, | |
593 | &dev_attr_temp1_min.attr, | |
594 | &dev_attr_temp1_crit.attr, | |
c2803b98 | 595 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
0501a381 MH |
596 | &dev_attr_temp2_input.attr, |
597 | &dev_attr_temp2_max.attr, | |
598 | &dev_attr_temp2_min.attr, | |
599 | &dev_attr_temp2_crit.attr, | |
c2803b98 JD |
600 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
601 | &sensor_dev_attr_temp2_fault.dev_attr.attr, | |
0501a381 MH |
602 | |
603 | &dev_attr_alarms.attr, | |
604 | &dev_attr_aout_output.attr, | |
605 | ||
606 | NULL | |
607 | }; | |
608 | ||
609 | static const struct attribute_group lm87_group = { | |
610 | .attrs = lm87_attributes, | |
611 | }; | |
612 | ||
613 | static struct attribute *lm87_attributes_opt[] = { | |
614 | &dev_attr_in6_input.attr, | |
615 | &dev_attr_in6_min.attr, | |
616 | &dev_attr_in6_max.attr, | |
c2803b98 | 617 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
0501a381 MH |
618 | |
619 | &dev_attr_fan1_input.attr, | |
620 | &dev_attr_fan1_min.attr, | |
621 | &dev_attr_fan1_div.attr, | |
c2803b98 | 622 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
0501a381 MH |
623 | |
624 | &dev_attr_in7_input.attr, | |
625 | &dev_attr_in7_min.attr, | |
626 | &dev_attr_in7_max.attr, | |
c2803b98 | 627 | &sensor_dev_attr_in7_alarm.dev_attr.attr, |
0501a381 MH |
628 | |
629 | &dev_attr_fan2_input.attr, | |
630 | &dev_attr_fan2_min.attr, | |
631 | &dev_attr_fan2_div.attr, | |
c2803b98 | 632 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
0501a381 MH |
633 | |
634 | &dev_attr_temp3_input.attr, | |
635 | &dev_attr_temp3_max.attr, | |
636 | &dev_attr_temp3_min.attr, | |
637 | &dev_attr_temp3_crit.attr, | |
c2803b98 JD |
638 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
639 | &sensor_dev_attr_temp3_fault.dev_attr.attr, | |
0501a381 MH |
640 | |
641 | &dev_attr_in0_input.attr, | |
642 | &dev_attr_in0_min.attr, | |
643 | &dev_attr_in0_max.attr, | |
c2803b98 | 644 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
0501a381 MH |
645 | &dev_attr_in5_input.attr, |
646 | &dev_attr_in5_min.attr, | |
647 | &dev_attr_in5_max.attr, | |
c2803b98 | 648 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
0501a381 MH |
649 | |
650 | &dev_attr_cpu0_vid.attr, | |
651 | &dev_attr_vrm.attr, | |
652 | ||
653 | NULL | |
654 | }; | |
655 | ||
656 | static const struct attribute_group lm87_group_opt = { | |
657 | .attrs = lm87_attributes_opt, | |
658 | }; | |
659 | ||
1da177e4 LT |
660 | /* |
661 | * The following function does more than just detection. If detection | |
662 | * succeeds, it also registers the new chip. | |
663 | */ | |
664 | static int lm87_detect(struct i2c_adapter *adapter, int address, int kind) | |
665 | { | |
666 | struct i2c_client *new_client; | |
667 | struct lm87_data *data; | |
668 | int err = 0; | |
c7fa3737 | 669 | static const char *names[] = { "lm87", "adm1024" }; |
1da177e4 LT |
670 | |
671 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
672 | goto exit; | |
673 | ||
ba9c2e8d | 674 | if (!(data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL))) { |
1da177e4 LT |
675 | err = -ENOMEM; |
676 | goto exit; | |
677 | } | |
1da177e4 LT |
678 | |
679 | /* The common I2C client data is placed right before the | |
680 | LM87-specific data. */ | |
681 | new_client = &data->client; | |
682 | i2c_set_clientdata(new_client, data); | |
683 | new_client->addr = address; | |
684 | new_client->adapter = adapter; | |
685 | new_client->driver = &lm87_driver; | |
686 | new_client->flags = 0; | |
687 | ||
688 | /* Default to an LM87 if forced */ | |
689 | if (kind == 0) | |
690 | kind = lm87; | |
691 | ||
692 | /* Now, we do the remaining detection. */ | |
693 | if (kind < 0) { | |
c7fa3737 | 694 | u8 cid = lm87_read_value(new_client, LM87_REG_COMPANY_ID); |
1da177e4 LT |
695 | u8 rev = lm87_read_value(new_client, LM87_REG_REVISION); |
696 | ||
c7fa3737 JD |
697 | if (cid == 0x02 /* National Semiconductor */ |
698 | && (rev >= 0x01 && rev <= 0x08)) | |
699 | kind = lm87; | |
700 | else if (cid == 0x41 /* Analog Devices */ | |
701 | && (rev & 0xf0) == 0x10) | |
702 | kind = adm1024; | |
703 | ||
704 | if (kind < 0 | |
705 | || (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)) { | |
1da177e4 LT |
706 | dev_dbg(&adapter->dev, |
707 | "LM87 detection failed at 0x%02x.\n", | |
708 | address); | |
709 | goto exit_free; | |
710 | } | |
711 | } | |
712 | ||
713 | /* We can fill in the remaining client fields */ | |
c7fa3737 | 714 | strlcpy(new_client->name, names[kind - 1], I2C_NAME_SIZE); |
1da177e4 | 715 | data->valid = 0; |
9a61bf63 | 716 | mutex_init(&data->update_lock); |
1da177e4 LT |
717 | |
718 | /* Tell the I2C layer a new client has arrived */ | |
719 | if ((err = i2c_attach_client(new_client))) | |
720 | goto exit_free; | |
721 | ||
722 | /* Initialize the LM87 chip */ | |
723 | lm87_init_client(new_client); | |
724 | ||
725 | data->in_scale[0] = 2500; | |
726 | data->in_scale[1] = 2700; | |
727 | data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300; | |
728 | data->in_scale[3] = 5000; | |
729 | data->in_scale[4] = 12000; | |
730 | data->in_scale[5] = 2700; | |
731 | data->in_scale[6] = 1875; | |
732 | data->in_scale[7] = 1875; | |
733 | ||
734 | /* Register sysfs hooks */ | |
0501a381 | 735 | if ((err = sysfs_create_group(&new_client->dev.kobj, &lm87_group))) |
943b0830 | 736 | goto exit_detach; |
1da177e4 LT |
737 | |
738 | if (data->channel & CHAN_NO_FAN(0)) { | |
0501a381 MH |
739 | if ((err = device_create_file(&new_client->dev, |
740 | &dev_attr_in6_input)) | |
741 | || (err = device_create_file(&new_client->dev, | |
742 | &dev_attr_in6_min)) | |
743 | || (err = device_create_file(&new_client->dev, | |
c2803b98 JD |
744 | &dev_attr_in6_max)) |
745 | || (err = device_create_file(&new_client->dev, | |
746 | &sensor_dev_attr_in6_alarm.dev_attr))) | |
0501a381 | 747 | goto exit_remove; |
1da177e4 | 748 | } else { |
0501a381 MH |
749 | if ((err = device_create_file(&new_client->dev, |
750 | &dev_attr_fan1_input)) | |
751 | || (err = device_create_file(&new_client->dev, | |
752 | &dev_attr_fan1_min)) | |
753 | || (err = device_create_file(&new_client->dev, | |
c2803b98 JD |
754 | &dev_attr_fan1_div)) |
755 | || (err = device_create_file(&new_client->dev, | |
756 | &sensor_dev_attr_fan1_alarm.dev_attr))) | |
0501a381 | 757 | goto exit_remove; |
1da177e4 | 758 | } |
0501a381 | 759 | |
1da177e4 | 760 | if (data->channel & CHAN_NO_FAN(1)) { |
0501a381 MH |
761 | if ((err = device_create_file(&new_client->dev, |
762 | &dev_attr_in7_input)) | |
763 | || (err = device_create_file(&new_client->dev, | |
764 | &dev_attr_in7_min)) | |
765 | || (err = device_create_file(&new_client->dev, | |
c2803b98 JD |
766 | &dev_attr_in7_max)) |
767 | || (err = device_create_file(&new_client->dev, | |
768 | &sensor_dev_attr_in7_alarm.dev_attr))) | |
0501a381 | 769 | goto exit_remove; |
1da177e4 | 770 | } else { |
0501a381 MH |
771 | if ((err = device_create_file(&new_client->dev, |
772 | &dev_attr_fan2_input)) | |
773 | || (err = device_create_file(&new_client->dev, | |
774 | &dev_attr_fan2_min)) | |
775 | || (err = device_create_file(&new_client->dev, | |
c2803b98 JD |
776 | &dev_attr_fan2_div)) |
777 | || (err = device_create_file(&new_client->dev, | |
778 | &sensor_dev_attr_fan2_alarm.dev_attr))) | |
0501a381 | 779 | goto exit_remove; |
1da177e4 LT |
780 | } |
781 | ||
1da177e4 | 782 | if (data->channel & CHAN_TEMP3) { |
0501a381 MH |
783 | if ((err = device_create_file(&new_client->dev, |
784 | &dev_attr_temp3_input)) | |
785 | || (err = device_create_file(&new_client->dev, | |
786 | &dev_attr_temp3_max)) | |
787 | || (err = device_create_file(&new_client->dev, | |
788 | &dev_attr_temp3_min)) | |
789 | || (err = device_create_file(&new_client->dev, | |
c2803b98 JD |
790 | &dev_attr_temp3_crit)) |
791 | || (err = device_create_file(&new_client->dev, | |
792 | &sensor_dev_attr_temp3_alarm.dev_attr)) | |
793 | || (err = device_create_file(&new_client->dev, | |
794 | &sensor_dev_attr_temp3_fault.dev_attr))) | |
0501a381 | 795 | goto exit_remove; |
1da177e4 | 796 | } else { |
0501a381 MH |
797 | if ((err = device_create_file(&new_client->dev, |
798 | &dev_attr_in0_input)) | |
799 | || (err = device_create_file(&new_client->dev, | |
800 | &dev_attr_in0_min)) | |
801 | || (err = device_create_file(&new_client->dev, | |
802 | &dev_attr_in0_max)) | |
c2803b98 JD |
803 | || (err = device_create_file(&new_client->dev, |
804 | &sensor_dev_attr_in0_alarm.dev_attr)) | |
0501a381 MH |
805 | || (err = device_create_file(&new_client->dev, |
806 | &dev_attr_in5_input)) | |
807 | || (err = device_create_file(&new_client->dev, | |
808 | &dev_attr_in5_min)) | |
809 | || (err = device_create_file(&new_client->dev, | |
c2803b98 JD |
810 | &dev_attr_in5_max)) |
811 | || (err = device_create_file(&new_client->dev, | |
812 | &sensor_dev_attr_in5_alarm.dev_attr))) | |
0501a381 | 813 | goto exit_remove; |
1da177e4 LT |
814 | } |
815 | ||
816 | if (!(data->channel & CHAN_NO_VID)) { | |
8a665a05 | 817 | data->vrm = vid_which_vrm(); |
0501a381 MH |
818 | if ((err = device_create_file(&new_client->dev, |
819 | &dev_attr_cpu0_vid)) | |
820 | || (err = device_create_file(&new_client->dev, | |
821 | &dev_attr_vrm))) | |
822 | goto exit_remove; | |
1da177e4 LT |
823 | } |
824 | ||
1beeffe4 TJ |
825 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
826 | if (IS_ERR(data->hwmon_dev)) { | |
827 | err = PTR_ERR(data->hwmon_dev); | |
0501a381 MH |
828 | goto exit_remove; |
829 | } | |
1da177e4 LT |
830 | |
831 | return 0; | |
832 | ||
0501a381 MH |
833 | exit_remove: |
834 | sysfs_remove_group(&new_client->dev.kobj, &lm87_group); | |
835 | sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt); | |
943b0830 MH |
836 | exit_detach: |
837 | i2c_detach_client(new_client); | |
1da177e4 LT |
838 | exit_free: |
839 | kfree(data); | |
840 | exit: | |
841 | return err; | |
842 | } | |
843 | ||
844 | static void lm87_init_client(struct i2c_client *client) | |
845 | { | |
846 | struct lm87_data *data = i2c_get_clientdata(client); | |
847 | u8 config; | |
848 | ||
849 | data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE); | |
1da177e4 LT |
850 | |
851 | config = lm87_read_value(client, LM87_REG_CONFIG); | |
852 | if (!(config & 0x01)) { | |
853 | int i; | |
854 | ||
855 | /* Limits are left uninitialized after power-up */ | |
856 | for (i = 1; i < 6; i++) { | |
857 | lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00); | |
858 | lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF); | |
859 | } | |
860 | for (i = 0; i < 2; i++) { | |
861 | lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F); | |
862 | lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00); | |
863 | lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00); | |
864 | lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF); | |
865 | } | |
866 | if (data->channel & CHAN_TEMP3) { | |
867 | lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F); | |
868 | lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00); | |
869 | } else { | |
870 | lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00); | |
871 | lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF); | |
872 | } | |
873 | } | |
874 | if ((config & 0x81) != 0x01) { | |
875 | /* Start monitoring */ | |
876 | lm87_write_value(client, LM87_REG_CONFIG, | |
877 | (config & 0xF7) | 0x01); | |
878 | } | |
879 | } | |
880 | ||
881 | static int lm87_detach_client(struct i2c_client *client) | |
882 | { | |
943b0830 | 883 | struct lm87_data *data = i2c_get_clientdata(client); |
1da177e4 LT |
884 | int err; |
885 | ||
1beeffe4 | 886 | hwmon_device_unregister(data->hwmon_dev); |
0501a381 MH |
887 | sysfs_remove_group(&client->dev.kobj, &lm87_group); |
888 | sysfs_remove_group(&client->dev.kobj, &lm87_group_opt); | |
943b0830 | 889 | |
7bef5594 | 890 | if ((err = i2c_detach_client(client))) |
1da177e4 | 891 | return err; |
1da177e4 | 892 | |
943b0830 | 893 | kfree(data); |
1da177e4 LT |
894 | return 0; |
895 | } | |
896 | ||
897 | static struct lm87_data *lm87_update_device(struct device *dev) | |
898 | { | |
899 | struct i2c_client *client = to_i2c_client(dev); | |
900 | struct lm87_data *data = i2c_get_clientdata(client); | |
901 | ||
9a61bf63 | 902 | mutex_lock(&data->update_lock); |
1da177e4 LT |
903 | |
904 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { | |
905 | int i, j; | |
906 | ||
907 | dev_dbg(&client->dev, "Updating data.\n"); | |
908 | ||
909 | i = (data->channel & CHAN_TEMP3) ? 1 : 0; | |
910 | j = (data->channel & CHAN_TEMP3) ? 5 : 6; | |
911 | for (; i < j; i++) { | |
912 | data->in[i] = lm87_read_value(client, | |
913 | LM87_REG_IN(i)); | |
914 | data->in_min[i] = lm87_read_value(client, | |
915 | LM87_REG_IN_MIN(i)); | |
916 | data->in_max[i] = lm87_read_value(client, | |
917 | LM87_REG_IN_MAX(i)); | |
918 | } | |
919 | ||
920 | for (i = 0; i < 2; i++) { | |
921 | if (data->channel & CHAN_NO_FAN(i)) { | |
922 | data->in[6+i] = lm87_read_value(client, | |
923 | LM87_REG_AIN(i)); | |
924 | data->in_max[6+i] = lm87_read_value(client, | |
925 | LM87_REG_AIN_MAX(i)); | |
926 | data->in_min[6+i] = lm87_read_value(client, | |
927 | LM87_REG_AIN_MIN(i)); | |
928 | ||
929 | } else { | |
930 | data->fan[i] = lm87_read_value(client, | |
931 | LM87_REG_FAN(i)); | |
932 | data->fan_min[i] = lm87_read_value(client, | |
933 | LM87_REG_FAN_MIN(i)); | |
934 | } | |
935 | } | |
936 | ||
937 | j = (data->channel & CHAN_TEMP3) ? 3 : 2; | |
938 | for (i = 0 ; i < j; i++) { | |
939 | data->temp[i] = lm87_read_value(client, | |
940 | LM87_REG_TEMP[i]); | |
941 | data->temp_high[i] = lm87_read_value(client, | |
942 | LM87_REG_TEMP_HIGH[i]); | |
943 | data->temp_low[i] = lm87_read_value(client, | |
944 | LM87_REG_TEMP_LOW[i]); | |
945 | } | |
946 | ||
947 | i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK); | |
948 | j = lm87_read_value(client, LM87_REG_TEMP_HW_INT); | |
949 | data->temp_crit_int = min(i, j); | |
950 | ||
951 | i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK); | |
952 | j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT); | |
953 | data->temp_crit_ext = min(i, j); | |
954 | ||
955 | i = lm87_read_value(client, LM87_REG_VID_FAN_DIV); | |
956 | data->fan_div[0] = (i >> 4) & 0x03; | |
957 | data->fan_div[1] = (i >> 6) & 0x03; | |
958 | data->vid = (i & 0x0F) | |
959 | | (lm87_read_value(client, LM87_REG_VID4) & 0x01) | |
960 | << 4; | |
961 | ||
962 | data->alarms = lm87_read_value(client, LM87_REG_ALARMS1) | |
963 | | (lm87_read_value(client, LM87_REG_ALARMS2) | |
964 | << 8); | |
965 | data->aout = lm87_read_value(client, LM87_REG_AOUT); | |
966 | ||
967 | data->last_updated = jiffies; | |
968 | data->valid = 1; | |
969 | } | |
970 | ||
9a61bf63 | 971 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
972 | |
973 | return data; | |
974 | } | |
975 | ||
976 | static int __init sensors_lm87_init(void) | |
977 | { | |
978 | return i2c_add_driver(&lm87_driver); | |
979 | } | |
980 | ||
981 | static void __exit sensors_lm87_exit(void) | |
982 | { | |
983 | i2c_del_driver(&lm87_driver); | |
984 | } | |
985 | ||
986 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others"); | |
987 | MODULE_DESCRIPTION("LM87 driver"); | |
988 | MODULE_LICENSE("GPL"); | |
989 | ||
990 | module_init(sensors_lm87_init); | |
991 | module_exit(sensors_lm87_exit); |