2 smsc47m1.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
5 Supports the SMSC LPC47B27x, LPC47M10x, LPC47M13x and LPC47M14x
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>
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.
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.
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.
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>
33 #include <linux/i2c-isa.h>
34 #include <linux/i2c-sensor.h>
35 #include <linux/hwmon.h>
36 #include <linux/err.h>
37 #include <linux/init.h>
40 /* Address is autodetected, there is no default value */
41 static unsigned short address;
43 /* Super-I/0 registers and commands */
45 #define REG 0x2e /* The register to read/write */
46 #define VAL 0x2f /* The value to read/write */
49 superio_outb(int reg, int val)
62 /* logical device for fans is 0x0A */
63 #define superio_select() superio_outb(0x07, 0x0A)
77 #define SUPERIO_REG_ACT 0x30
78 #define SUPERIO_REG_BASE 0x60
79 #define SUPERIO_REG_DEVID 0x20
81 /* Logical device registers */
83 #define SMSC_EXTENT 0x80
85 /* nr is 0 or 1 in the macros below */
86 #define SMSC47M1_REG_ALARM 0x04
87 #define SMSC47M1_REG_TPIN(nr) (0x34 - (nr))
88 #define SMSC47M1_REG_PPIN(nr) (0x36 - (nr))
89 #define SMSC47M1_REG_PWM(nr) (0x56 + (nr))
90 #define SMSC47M1_REG_FANDIV 0x58
91 #define SMSC47M1_REG_FAN(nr) (0x59 + (nr))
92 #define SMSC47M1_REG_FAN_PRELOAD(nr) (0x5B + (nr))
94 #define MIN_FROM_REG(reg,div) ((reg)>=192 ? 0 : \
95 983040/((192-(reg))*(div)))
96 #define FAN_FROM_REG(reg,div,preload) ((reg)<=(preload) || (reg)==255 ? 0 : \
97 983040/(((reg)-(preload))*(div)))
98 #define DIV_FROM_REG(reg) (1 << (reg))
99 #define PWM_FROM_REG(reg) (((reg) & 0x7E) << 1)
100 #define PWM_EN_FROM_REG(reg) ((~(reg)) & 0x01)
101 #define PWM_TO_REG(reg) (((reg) >> 1) & 0x7E)
103 struct smsc47m1_data {
104 struct i2c_client client;
105 struct class_device *class_dev;
106 struct semaphore lock;
108 struct semaphore update_lock;
109 unsigned long last_updated; /* In jiffies */
111 u8 fan[2]; /* Register value */
112 u8 fan_preload[2]; /* Register value */
113 u8 fan_div[2]; /* Register encoding, shifted right */
114 u8 alarms; /* Register encoding */
115 u8 pwm[2]; /* Register value (bit 7 is enable) */
119 static int smsc47m1_detect(struct i2c_adapter *adapter);
120 static int smsc47m1_detach_client(struct i2c_client *client);
122 static int smsc47m1_read_value(struct i2c_client *client, u8 reg);
123 static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value);
125 static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
129 static struct i2c_driver smsc47m1_driver = {
130 .owner = THIS_MODULE,
132 .attach_adapter = smsc47m1_detect,
133 .detach_client = smsc47m1_detach_client,
136 /* nr is 0 or 1 in the callback functions below */
138 static ssize_t get_fan(struct device *dev, char *buf, int nr)
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);
152 static ssize_t get_fan_min(struct device *dev, char *buf, int nr)
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);
160 static ssize_t get_fan_div(struct device *dev, char *buf, int nr)
162 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
163 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
166 static ssize_t get_pwm(struct device *dev, char *buf, int nr)
168 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
169 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
172 static ssize_t get_pwm_en(struct device *dev, char *buf, int nr)
174 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
175 return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[nr]));
178 static ssize_t get_alarms(struct device *dev, struct device_attribute *attr, char *buf)
180 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
181 return sprintf(buf, "%d\n", data->alarms);
184 static ssize_t set_fan_min(struct device *dev, const char *buf,
185 size_t count, int nr)
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);
191 down(&data->update_lock);
192 rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]);
194 if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) {
195 up(&data->update_lock);
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);
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. */
211 static ssize_t set_fan_div(struct device *dev, const char *buf,
212 size_t count, int nr)
214 struct i2c_client *client = to_i2c_client(dev);
215 struct smsc47m1_data *data = i2c_get_clientdata(client);
217 long new_div = simple_strtol(buf, NULL, 10), tmp;
218 u8 old_div = DIV_FROM_REG(data->fan_div[nr]);
220 if (new_div == old_div) /* No change */
223 down(&data->update_lock);
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;
230 up(&data->update_lock);
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);
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);
249 static ssize_t set_pwm(struct device *dev, const char *buf,
250 size_t count, int nr)
252 struct i2c_client *client = to_i2c_client(dev);
253 struct smsc47m1_data *data = i2c_get_clientdata(client);
255 long val = simple_strtol(buf, NULL, 10);
257 if (val < 0 || val > 255)
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),
265 up(&data->update_lock);
270 static ssize_t set_pwm_en(struct device *dev, const char *buf,
271 size_t count, int nr)
273 struct i2c_client *client = to_i2c_client(dev);
274 struct smsc47m1_data *data = i2c_get_clientdata(client);
276 long val = simple_strtol(buf, NULL, 10);
278 if (val != 0 && val != 1)
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),
286 up(&data->update_lock);
291 #define fan_present(offset) \
292 static ssize_t get_fan##offset (struct device *dev, struct device_attribute *attr, char *buf) \
294 return get_fan(dev, buf, offset - 1); \
296 static ssize_t get_fan##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
298 return get_fan_min(dev, buf, offset - 1); \
300 static ssize_t set_fan##offset##_min (struct device *dev, struct device_attribute *attr, \
301 const char *buf, size_t count) \
303 return set_fan_min(dev, buf, count, offset - 1); \
305 static ssize_t get_fan##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
307 return get_fan_div(dev, buf, offset - 1); \
309 static ssize_t set_fan##offset##_div (struct device *dev, struct device_attribute *attr, \
310 const char *buf, size_t count) \
312 return set_fan_div(dev, buf, count, offset - 1); \
314 static ssize_t get_pwm##offset (struct device *dev, struct device_attribute *attr, char *buf) \
316 return get_pwm(dev, buf, offset - 1); \
318 static ssize_t set_pwm##offset (struct device *dev, struct device_attribute *attr, \
319 const char *buf, size_t count) \
321 return set_pwm(dev, buf, count, offset - 1); \
323 static ssize_t get_pwm##offset##_en (struct device *dev, struct device_attribute *attr, char *buf) \
325 return get_pwm_en(dev, buf, offset - 1); \
327 static ssize_t set_pwm##offset##_en (struct device *dev, struct device_attribute *attr, \
328 const char *buf, size_t count) \
330 return set_pwm_en(dev, buf, count, offset - 1); \
332 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan##offset, \
334 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
335 get_fan##offset##_min, set_fan##offset##_min); \
336 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
337 get_fan##offset##_div, set_fan##offset##_div); \
338 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
339 get_pwm##offset, set_pwm##offset); \
340 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
341 get_pwm##offset##_en, set_pwm##offset##_en);
346 static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL);
348 static int __init smsc47m1_find(unsigned short *addr)
353 val = superio_inb(SUPERIO_REG_DEVID);
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"
359 * can do much more besides (device id 0x60).
362 printk(KERN_INFO "smsc47m1: Found SMSC LPC47B27x\n");
363 else if (val == 0x59)
364 printk(KERN_INFO "smsc47m1: Found SMSC LPC47M10x/LPC47M13x\n");
365 else if (val == 0x5F)
366 printk(KERN_INFO "smsc47m1: Found SMSC LPC47M14x\n");
367 else if (val == 0x60)
368 printk(KERN_INFO "smsc47m1: Found SMSC LPC47M15x/LPC47M192\n");
375 *addr = (superio_inb(SUPERIO_REG_BASE) << 8)
376 | superio_inb(SUPERIO_REG_BASE + 1);
377 val = superio_inb(SUPERIO_REG_ACT);
378 if (*addr == 0 || (val & 0x01) == 0) {
379 printk(KERN_INFO "smsc47m1: Device is disabled, will not use\n");
388 static int smsc47m1_detect(struct i2c_adapter *adapter)
390 struct i2c_client *new_client;
391 struct smsc47m1_data *data;
393 int fan1, fan2, pwm1, pwm2;
395 if (!request_region(address, SMSC_EXTENT, smsc47m1_driver.name)) {
396 dev_err(&adapter->dev, "Region 0x%x already in use!\n", address);
400 if (!(data = kmalloc(sizeof(struct smsc47m1_data), GFP_KERNEL))) {
404 memset(data, 0x00, sizeof(struct smsc47m1_data));
406 new_client = &data->client;
407 i2c_set_clientdata(new_client, data);
408 new_client->addr = address;
409 init_MUTEX(&data->lock);
410 new_client->adapter = adapter;
411 new_client->driver = &smsc47m1_driver;
412 new_client->flags = 0;
414 strlcpy(new_client->name, "smsc47m1", I2C_NAME_SIZE);
415 init_MUTEX(&data->update_lock);
417 /* If no function is properly configured, there's no point in
418 actually registering the chip. */
419 fan1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(0)) & 0x05)
421 fan2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(1)) & 0x05)
423 pwm1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(0)) & 0x05)
425 pwm2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(1)) & 0x05)
427 if (!(fan1 || fan2 || pwm1 || pwm2)) {
428 dev_warn(&new_client->dev, "Device is not configured, will not use\n");
433 if ((err = i2c_attach_client(new_client)))
436 /* Some values (fan min, clock dividers, pwm registers) may be
437 needed before any update is triggered, so we better read them
438 at least once here. We don't usually do it that way, but in
439 this particular case, manually reading 5 registers out of 8
440 doesn't make much sense and we're better using the existing
442 smsc47m1_update_device(&new_client->dev, 1);
444 /* Register sysfs hooks */
445 data->class_dev = hwmon_device_register(&new_client->dev);
446 if (IS_ERR(data->class_dev)) {
447 err = PTR_ERR(data->class_dev);
452 device_create_file(&new_client->dev, &dev_attr_fan1_input);
453 device_create_file(&new_client->dev, &dev_attr_fan1_min);
454 device_create_file(&new_client->dev, &dev_attr_fan1_div);
456 dev_dbg(&new_client->dev, "Fan 1 not enabled by hardware, "
460 device_create_file(&new_client->dev, &dev_attr_fan2_input);
461 device_create_file(&new_client->dev, &dev_attr_fan2_min);
462 device_create_file(&new_client->dev, &dev_attr_fan2_div);
464 dev_dbg(&new_client->dev, "Fan 2 not enabled by hardware, "
468 device_create_file(&new_client->dev, &dev_attr_pwm1);
469 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
471 dev_dbg(&new_client->dev, "PWM 1 not enabled by hardware, "
474 device_create_file(&new_client->dev, &dev_attr_pwm2);
475 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
477 dev_dbg(&new_client->dev, "PWM 2 not enabled by hardware, "
480 device_create_file(&new_client->dev, &dev_attr_alarms);
485 i2c_detach_client(new_client);
489 release_region(address, SMSC_EXTENT);
493 static int smsc47m1_detach_client(struct i2c_client *client)
495 struct smsc47m1_data *data = i2c_get_clientdata(client);
498 hwmon_device_unregister(data->class_dev);
500 if ((err = i2c_detach_client(client)))
503 release_region(client->addr, SMSC_EXTENT);
509 static int smsc47m1_read_value(struct i2c_client *client, u8 reg)
513 down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
514 res = inb_p(client->addr + reg);
515 up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
519 static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value)
521 down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
522 outb_p(value, client->addr + reg);
523 up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
526 static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
529 struct i2c_client *client = to_i2c_client(dev);
530 struct smsc47m1_data *data = i2c_get_clientdata(client);
532 down(&data->update_lock);
534 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) || init) {
537 for (i = 0; i < 2; i++) {
538 data->fan[i] = smsc47m1_read_value(client,
539 SMSC47M1_REG_FAN(i));
540 data->fan_preload[i] = smsc47m1_read_value(client,
541 SMSC47M1_REG_FAN_PRELOAD(i));
542 data->pwm[i] = smsc47m1_read_value(client,
543 SMSC47M1_REG_PWM(i));
546 i = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV);
547 data->fan_div[0] = (i >> 4) & 0x03;
548 data->fan_div[1] = i >> 6;
550 data->alarms = smsc47m1_read_value(client,
551 SMSC47M1_REG_ALARM) >> 6;
552 /* Clear alarms if needed */
554 smsc47m1_write_value(client, SMSC47M1_REG_ALARM, 0xC0);
556 data->last_updated = jiffies;
559 up(&data->update_lock);
563 static int __init sm_smsc47m1_init(void)
565 if (smsc47m1_find(&address)) {
569 return i2c_isa_add_driver(&smsc47m1_driver);
572 static void __exit sm_smsc47m1_exit(void)
574 i2c_isa_del_driver(&smsc47m1_driver);
577 MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
578 MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
579 MODULE_LICENSE("GPL");
581 module_init(sm_smsc47m1_init);
582 module_exit(sm_smsc47m1_exit);