blob: d1e3ec0fe4dfa284570695784f0b2a19c742120d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 smsc47m1.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
Jean Delvare0db97142005-09-06 21:21:54 +02005 Supports the SMSC LPC47B27x, LPC47M10x, LPC47M13x, LPC47M14x,
Jean Delvareb890a072005-10-26 22:21:24 +02006 LPC47M15x, LPC47M192 and LPC47M997 Super-I/O chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8 Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
9 Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
10 Ported to Linux 2.6 by Gabriele Gorla <gorlik@yahoo.com>
11 and Jean Delvare
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26*/
27
28#include <linux/module.h>
29#include <linux/slab.h>
30#include <linux/ioport.h>
31#include <linux/jiffies.h>
32#include <linux/i2c.h>
Jean Delvarefde09502005-07-19 23:51:07 +020033#include <linux/i2c-isa.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040034#include <linux/hwmon.h>
35#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/init.h>
37#include <asm/io.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* Address is autodetected, there is no default value */
Jean Delvare2d8672c2005-07-19 23:56:35 +020040static unsigned short address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/* Super-I/0 registers and commands */
43
44#define REG 0x2e /* The register to read/write */
45#define VAL 0x2f /* The value to read/write */
46
47static inline void
48superio_outb(int reg, int val)
49{
50 outb(reg, REG);
51 outb(val, VAL);
52}
53
54static inline int
55superio_inb(int reg)
56{
57 outb(reg, REG);
58 return inb(VAL);
59}
60
61/* logical device for fans is 0x0A */
62#define superio_select() superio_outb(0x07, 0x0A)
63
64static inline void
65superio_enter(void)
66{
67 outb(0x55, REG);
68}
69
70static inline void
71superio_exit(void)
72{
73 outb(0xAA, REG);
74}
75
76#define SUPERIO_REG_ACT 0x30
77#define SUPERIO_REG_BASE 0x60
78#define SUPERIO_REG_DEVID 0x20
79
80/* Logical device registers */
81
82#define SMSC_EXTENT 0x80
83
84/* nr is 0 or 1 in the macros below */
85#define SMSC47M1_REG_ALARM 0x04
86#define SMSC47M1_REG_TPIN(nr) (0x34 - (nr))
87#define SMSC47M1_REG_PPIN(nr) (0x36 - (nr))
88#define SMSC47M1_REG_PWM(nr) (0x56 + (nr))
89#define SMSC47M1_REG_FANDIV 0x58
90#define SMSC47M1_REG_FAN(nr) (0x59 + (nr))
91#define SMSC47M1_REG_FAN_PRELOAD(nr) (0x5B + (nr))
92
93#define MIN_FROM_REG(reg,div) ((reg)>=192 ? 0 : \
94 983040/((192-(reg))*(div)))
95#define FAN_FROM_REG(reg,div,preload) ((reg)<=(preload) || (reg)==255 ? 0 : \
96 983040/(((reg)-(preload))*(div)))
97#define DIV_FROM_REG(reg) (1 << (reg))
98#define PWM_FROM_REG(reg) (((reg) & 0x7E) << 1)
99#define PWM_EN_FROM_REG(reg) ((~(reg)) & 0x01)
100#define PWM_TO_REG(reg) (((reg) >> 1) & 0x7E)
101
102struct smsc47m1_data {
103 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400104 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct semaphore lock;
106
107 struct semaphore update_lock;
108 unsigned long last_updated; /* In jiffies */
109
110 u8 fan[2]; /* Register value */
111 u8 fan_preload[2]; /* Register value */
112 u8 fan_div[2]; /* Register encoding, shifted right */
113 u8 alarms; /* Register encoding */
114 u8 pwm[2]; /* Register value (bit 7 is enable) */
115};
116
117
Jean Delvare2d8672c2005-07-19 23:56:35 +0200118static int smsc47m1_detect(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static int smsc47m1_detach_client(struct i2c_client *client);
120
121static int smsc47m1_read_value(struct i2c_client *client, u8 reg);
122static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value);
123
124static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
125 int init);
126
127
128static struct i2c_driver smsc47m1_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100129 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100130 .name = "smsc47m1",
131 },
Jean Delvare2d8672c2005-07-19 23:56:35 +0200132 .attach_adapter = smsc47m1_detect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .detach_client = smsc47m1_detach_client,
134};
135
136/* nr is 0 or 1 in the callback functions below */
137
138static ssize_t get_fan(struct device *dev, char *buf, int nr)
139{
140 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
141 /* This chip (stupidly) stops monitoring fan speed if PWM is
142 enabled and duty cycle is 0%. This is fine if the monitoring
143 and control concern the same fan, but troublesome if they are
144 not (which could as well happen). */
145 int rpm = (data->pwm[nr] & 0x7F) == 0x00 ? 0 :
146 FAN_FROM_REG(data->fan[nr],
147 DIV_FROM_REG(data->fan_div[nr]),
148 data->fan_preload[nr]);
149 return sprintf(buf, "%d\n", rpm);
150}
151
152static ssize_t get_fan_min(struct device *dev, char *buf, int nr)
153{
154 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
155 int rpm = MIN_FROM_REG(data->fan_preload[nr],
156 DIV_FROM_REG(data->fan_div[nr]));
157 return sprintf(buf, "%d\n", rpm);
158}
159
160static ssize_t get_fan_div(struct device *dev, char *buf, int nr)
161{
162 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
163 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
164}
165
166static ssize_t get_pwm(struct device *dev, char *buf, int nr)
167{
168 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
169 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
170}
171
172static ssize_t get_pwm_en(struct device *dev, char *buf, int nr)
173{
174 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
175 return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[nr]));
176}
177
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400178static ssize_t get_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
181 return sprintf(buf, "%d\n", data->alarms);
182}
183
184static ssize_t set_fan_min(struct device *dev, const char *buf,
185 size_t count, int nr)
186{
187 struct i2c_client *client = to_i2c_client(dev);
188 struct smsc47m1_data *data = i2c_get_clientdata(client);
189 long rpmdiv, val = simple_strtol(buf, NULL, 10);
190
191 down(&data->update_lock);
192 rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]);
193
194 if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) {
195 up(&data->update_lock);
196 return -EINVAL;
197 }
198
199 data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv);
200 smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD(nr),
201 data->fan_preload[nr]);
202 up(&data->update_lock);
203
204 return count;
205}
206
207/* Note: we save and restore the fan minimum here, because its value is
208 determined in part by the fan clock divider. This follows the principle
209 of least suprise; the user doesn't expect the fan minimum to change just
210 because the divider changed. */
211static ssize_t set_fan_div(struct device *dev, const char *buf,
212 size_t count, int nr)
213{
214 struct i2c_client *client = to_i2c_client(dev);
215 struct smsc47m1_data *data = i2c_get_clientdata(client);
216
217 long new_div = simple_strtol(buf, NULL, 10), tmp;
218 u8 old_div = DIV_FROM_REG(data->fan_div[nr]);
219
220 if (new_div == old_div) /* No change */
221 return count;
222
223 down(&data->update_lock);
224 switch (new_div) {
225 case 1: data->fan_div[nr] = 0; break;
226 case 2: data->fan_div[nr] = 1; break;
227 case 4: data->fan_div[nr] = 2; break;
228 case 8: data->fan_div[nr] = 3; break;
229 default:
230 up(&data->update_lock);
231 return -EINVAL;
232 }
233
234 tmp = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV) & 0x0F;
235 tmp |= (data->fan_div[0] << 4) | (data->fan_div[1] << 6);
236 smsc47m1_write_value(client, SMSC47M1_REG_FANDIV, tmp);
237
238 /* Preserve fan min */
239 tmp = 192 - (old_div * (192 - data->fan_preload[nr])
240 + new_div / 2) / new_div;
241 data->fan_preload[nr] = SENSORS_LIMIT(tmp, 0, 191);
242 smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD(nr),
243 data->fan_preload[nr]);
244 up(&data->update_lock);
245
246 return count;
247}
248
249static ssize_t set_pwm(struct device *dev, const char *buf,
250 size_t count, int nr)
251{
252 struct i2c_client *client = to_i2c_client(dev);
253 struct smsc47m1_data *data = i2c_get_clientdata(client);
254
255 long val = simple_strtol(buf, NULL, 10);
256
257 if (val < 0 || val > 255)
258 return -EINVAL;
259
260 down(&data->update_lock);
261 data->pwm[nr] &= 0x81; /* Preserve additional bits */
262 data->pwm[nr] |= PWM_TO_REG(val);
263 smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr),
264 data->pwm[nr]);
265 up(&data->update_lock);
266
267 return count;
268}
269
270static ssize_t set_pwm_en(struct device *dev, const char *buf,
271 size_t count, int nr)
272{
273 struct i2c_client *client = to_i2c_client(dev);
274 struct smsc47m1_data *data = i2c_get_clientdata(client);
275
276 long val = simple_strtol(buf, NULL, 10);
277
278 if (val != 0 && val != 1)
279 return -EINVAL;
280
281 down(&data->update_lock);
282 data->pwm[nr] &= 0xFE; /* preserve the other bits */
283 data->pwm[nr] |= !val;
284 smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr),
285 data->pwm[nr]);
286 up(&data->update_lock);
287
288 return count;
289}
290
291#define fan_present(offset) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400292static ssize_t get_fan##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{ \
294 return get_fan(dev, buf, offset - 1); \
295} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400296static ssize_t get_fan##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{ \
298 return get_fan_min(dev, buf, offset - 1); \
299} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400300static ssize_t set_fan##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 const char *buf, size_t count) \
302{ \
303 return set_fan_min(dev, buf, count, offset - 1); \
304} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400305static ssize_t get_fan##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{ \
307 return get_fan_div(dev, buf, offset - 1); \
308} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400309static ssize_t set_fan##offset##_div (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 const char *buf, size_t count) \
311{ \
312 return set_fan_div(dev, buf, count, offset - 1); \
313} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400314static ssize_t get_pwm##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{ \
316 return get_pwm(dev, buf, offset - 1); \
317} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400318static ssize_t set_pwm##offset (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 const char *buf, size_t count) \
320{ \
321 return set_pwm(dev, buf, count, offset - 1); \
322} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400323static ssize_t get_pwm##offset##_en (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{ \
325 return get_pwm_en(dev, buf, offset - 1); \
326} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400327static ssize_t set_pwm##offset##_en (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 const char *buf, size_t count) \
329{ \
330 return set_pwm_en(dev, buf, count, offset - 1); \
331} \
332static DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan##offset, \
333 NULL); \
334static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
335 get_fan##offset##_min, set_fan##offset##_min); \
336static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
337 get_fan##offset##_div, set_fan##offset##_div); \
338static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
339 get_pwm##offset, set_pwm##offset); \
340static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
341 get_pwm##offset##_en, set_pwm##offset##_en);
342
343fan_present(1);
344fan_present(2);
345
346static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL);
347
Jean Delvaree6cfb3a2005-07-27 21:32:02 +0200348static int __init smsc47m1_find(unsigned short *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 u8 val;
351
352 superio_enter();
353 val = superio_inb(SUPERIO_REG_DEVID);
354
355 /*
356 * SMSC LPC47M10x/LPC47M13x (device id 0x59), LPC47M14x (device id
357 * 0x5F) and LPC47B27x (device id 0x51) have fan control.
358 * The LPC47M15x and LPC47M192 chips "with hardware monitoring block"
Jean Delvareec5ce552005-04-26 22:09:43 +0200359 * can do much more besides (device id 0x60).
Jean Delvareb890a072005-10-26 22:21:24 +0200360 * The LPC47M997 is undocumented, but seems to be compatible with
361 * the LPC47M192, and has the same device id.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
363 if (val == 0x51)
Jean Delvareec5ce552005-04-26 22:09:43 +0200364 printk(KERN_INFO "smsc47m1: Found SMSC LPC47B27x\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 else if (val == 0x59)
Jean Delvareec5ce552005-04-26 22:09:43 +0200366 printk(KERN_INFO "smsc47m1: Found SMSC LPC47M10x/LPC47M13x\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 else if (val == 0x5F)
Jean Delvareec5ce552005-04-26 22:09:43 +0200368 printk(KERN_INFO "smsc47m1: Found SMSC LPC47M14x\n");
369 else if (val == 0x60)
Jean Delvareb890a072005-10-26 22:21:24 +0200370 printk(KERN_INFO "smsc47m1: Found SMSC "
371 "LPC47M15x/LPC47M192/LPC47M997\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 else {
373 superio_exit();
374 return -ENODEV;
375 }
376
377 superio_select();
Jean Delvare2d8672c2005-07-19 23:56:35 +0200378 *addr = (superio_inb(SUPERIO_REG_BASE) << 8)
379 | superio_inb(SUPERIO_REG_BASE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 val = superio_inb(SUPERIO_REG_ACT);
Jean Delvare2d8672c2005-07-19 23:56:35 +0200381 if (*addr == 0 || (val & 0x01) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 printk(KERN_INFO "smsc47m1: Device is disabled, will not use\n");
383 superio_exit();
384 return -ENODEV;
385 }
386
387 superio_exit();
388 return 0;
389}
390
Jean Delvare2d8672c2005-07-19 23:56:35 +0200391static int smsc47m1_detect(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct i2c_client *new_client;
394 struct smsc47m1_data *data;
395 int err = 0;
396 int fan1, fan2, pwm1, pwm2;
397
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100398 if (!request_region(address, SMSC_EXTENT, smsc47m1_driver.driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 dev_err(&adapter->dev, "Region 0x%x already in use!\n", address);
400 return -EBUSY;
401 }
402
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200403 if (!(data = kzalloc(sizeof(struct smsc47m1_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 err = -ENOMEM;
405 goto error_release;
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 new_client = &data->client;
409 i2c_set_clientdata(new_client, data);
410 new_client->addr = address;
411 init_MUTEX(&data->lock);
412 new_client->adapter = adapter;
413 new_client->driver = &smsc47m1_driver;
414 new_client->flags = 0;
415
416 strlcpy(new_client->name, "smsc47m1", I2C_NAME_SIZE);
417 init_MUTEX(&data->update_lock);
418
419 /* If no function is properly configured, there's no point in
420 actually registering the chip. */
421 fan1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(0)) & 0x05)
422 == 0x05;
423 fan2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(1)) & 0x05)
424 == 0x05;
425 pwm1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(0)) & 0x05)
426 == 0x04;
427 pwm2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(1)) & 0x05)
428 == 0x04;
429 if (!(fan1 || fan2 || pwm1 || pwm2)) {
430 dev_warn(&new_client->dev, "Device is not configured, will not use\n");
431 err = -ENODEV;
432 goto error_free;
433 }
434
435 if ((err = i2c_attach_client(new_client)))
436 goto error_free;
437
438 /* Some values (fan min, clock dividers, pwm registers) may be
439 needed before any update is triggered, so we better read them
440 at least once here. We don't usually do it that way, but in
441 this particular case, manually reading 5 registers out of 8
442 doesn't make much sense and we're better using the existing
443 function. */
444 smsc47m1_update_device(&new_client->dev, 1);
445
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400446 /* Register sysfs hooks */
447 data->class_dev = hwmon_device_register(&new_client->dev);
448 if (IS_ERR(data->class_dev)) {
449 err = PTR_ERR(data->class_dev);
450 goto error_detach;
451 }
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (fan1) {
454 device_create_file(&new_client->dev, &dev_attr_fan1_input);
455 device_create_file(&new_client->dev, &dev_attr_fan1_min);
456 device_create_file(&new_client->dev, &dev_attr_fan1_div);
457 } else
458 dev_dbg(&new_client->dev, "Fan 1 not enabled by hardware, "
459 "skipping\n");
460
461 if (fan2) {
462 device_create_file(&new_client->dev, &dev_attr_fan2_input);
463 device_create_file(&new_client->dev, &dev_attr_fan2_min);
464 device_create_file(&new_client->dev, &dev_attr_fan2_div);
465 } else
466 dev_dbg(&new_client->dev, "Fan 2 not enabled by hardware, "
467 "skipping\n");
468
469 if (pwm1) {
470 device_create_file(&new_client->dev, &dev_attr_pwm1);
471 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
472 } else
473 dev_dbg(&new_client->dev, "PWM 1 not enabled by hardware, "
474 "skipping\n");
475 if (pwm2) {
476 device_create_file(&new_client->dev, &dev_attr_pwm2);
477 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
478 } else
479 dev_dbg(&new_client->dev, "PWM 2 not enabled by hardware, "
480 "skipping\n");
481
482 device_create_file(&new_client->dev, &dev_attr_alarms);
483
484 return 0;
485
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400486error_detach:
487 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488error_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400489 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490error_release:
491 release_region(address, SMSC_EXTENT);
492 return err;
493}
494
495static int smsc47m1_detach_client(struct i2c_client *client)
496{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400497 struct smsc47m1_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int err;
499
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400500 hwmon_device_unregister(data->class_dev);
501
Jean Delvare7bef5592005-07-27 22:14:49 +0200502 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 release_region(client->addr, SMSC_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400506 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 return 0;
509}
510
511static int smsc47m1_read_value(struct i2c_client *client, u8 reg)
512{
513 int res;
514
515 down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
516 res = inb_p(client->addr + reg);
517 up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
518 return res;
519}
520
521static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value)
522{
523 down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
524 outb_p(value, client->addr + reg);
525 up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
526}
527
528static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
529 int init)
530{
531 struct i2c_client *client = to_i2c_client(dev);
532 struct smsc47m1_data *data = i2c_get_clientdata(client);
533
534 down(&data->update_lock);
535
536 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) || init) {
537 int i;
538
539 for (i = 0; i < 2; i++) {
540 data->fan[i] = smsc47m1_read_value(client,
541 SMSC47M1_REG_FAN(i));
542 data->fan_preload[i] = smsc47m1_read_value(client,
543 SMSC47M1_REG_FAN_PRELOAD(i));
544 data->pwm[i] = smsc47m1_read_value(client,
545 SMSC47M1_REG_PWM(i));
546 }
547
548 i = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV);
549 data->fan_div[0] = (i >> 4) & 0x03;
550 data->fan_div[1] = i >> 6;
551
552 data->alarms = smsc47m1_read_value(client,
553 SMSC47M1_REG_ALARM) >> 6;
554 /* Clear alarms if needed */
555 if (data->alarms)
556 smsc47m1_write_value(client, SMSC47M1_REG_ALARM, 0xC0);
557
558 data->last_updated = jiffies;
559 }
560
561 up(&data->update_lock);
562 return data;
563}
564
565static int __init sm_smsc47m1_init(void)
566{
Jean Delvare2d8672c2005-07-19 23:56:35 +0200567 if (smsc47m1_find(&address)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return -ENODEV;
569 }
570
Jean Delvarefde09502005-07-19 23:51:07 +0200571 return i2c_isa_add_driver(&smsc47m1_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574static void __exit sm_smsc47m1_exit(void)
575{
Jean Delvarefde09502005-07-19 23:51:07 +0200576 i2c_isa_del_driver(&smsc47m1_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
580MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
581MODULE_LICENSE("GPL");
582
583module_init(sm_smsc47m1_init);
584module_exit(sm_smsc47m1_exit);