1 /* 2 smsc47m1.c - Part of lm_sensors, Linux kernel modules 3 for hardware monitoring 4 5 Supports the SMSC LPC47B27x, LPC47M10x, LPC47M13x and LPC47M14x 6 Super-I/O chips. 7 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> 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> 38 #include <asm/io.h> 39 40 static unsigned short normal_i2c[] = { I2C_CLIENT_END }; 41 /* Address is autodetected, there is no default value */ 42 static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END }; 43 static struct i2c_force_data forces[] = {{NULL}}; 44 45 enum chips { any_chip, smsc47m1 }; 46 static struct i2c_address_data addr_data = { 47 .normal_i2c = normal_i2c, 48 .normal_isa = normal_isa, 49 .forces = forces, 50 }; 51 52 /* Super-I/0 registers and commands */ 53 54 #define REG 0x2e /* The register to read/write */ 55 #define VAL 0x2f /* The value to read/write */ 56 57 static inline void 58 superio_outb(int reg, int val) 59 { 60 outb(reg, REG); 61 outb(val, VAL); 62 } 63 64 static inline int 65 superio_inb(int reg) 66 { 67 outb(reg, REG); 68 return inb(VAL); 69 } 70 71 /* logical device for fans is 0x0A */ 72 #define superio_select() superio_outb(0x07, 0x0A) 73 74 static inline void 75 superio_enter(void) 76 { 77 outb(0x55, REG); 78 } 79 80 static inline void 81 superio_exit(void) 82 { 83 outb(0xAA, REG); 84 } 85 86 #define SUPERIO_REG_ACT 0x30 87 #define SUPERIO_REG_BASE 0x60 88 #define SUPERIO_REG_DEVID 0x20 89 90 /* Logical device registers */ 91 92 #define SMSC_EXTENT 0x80 93 94 /* nr is 0 or 1 in the macros below */ 95 #define SMSC47M1_REG_ALARM 0x04 96 #define SMSC47M1_REG_TPIN(nr) (0x34 - (nr)) 97 #define SMSC47M1_REG_PPIN(nr) (0x36 - (nr)) 98 #define SMSC47M1_REG_PWM(nr) (0x56 + (nr)) 99 #define SMSC47M1_REG_FANDIV 0x58 100 #define SMSC47M1_REG_FAN(nr) (0x59 + (nr)) 101 #define SMSC47M1_REG_FAN_PRELOAD(nr) (0x5B + (nr)) 102 103 #define MIN_FROM_REG(reg,div) ((reg)>=192 ? 0 : \ 104 983040/((192-(reg))*(div))) 105 #define FAN_FROM_REG(reg,div,preload) ((reg)<=(preload) || (reg)==255 ? 0 : \ 106 983040/(((reg)-(preload))*(div))) 107 #define DIV_FROM_REG(reg) (1 << (reg)) 108 #define PWM_FROM_REG(reg) (((reg) & 0x7E) << 1) 109 #define PWM_EN_FROM_REG(reg) ((~(reg)) & 0x01) 110 #define PWM_TO_REG(reg) (((reg) >> 1) & 0x7E) 111 112 struct smsc47m1_data { 113 struct i2c_client client; 114 struct class_device *class_dev; 115 struct semaphore lock; 116 117 struct semaphore update_lock; 118 unsigned long last_updated; /* In jiffies */ 119 120 u8 fan[2]; /* Register value */ 121 u8 fan_preload[2]; /* Register value */ 122 u8 fan_div[2]; /* Register encoding, shifted right */ 123 u8 alarms; /* Register encoding */ 124 u8 pwm[2]; /* Register value (bit 7 is enable) */ 125 }; 126 127 128 static int smsc47m1_attach_adapter(struct i2c_adapter *adapter); 129 static int smsc47m1_find(int *address); 130 static int smsc47m1_detect(struct i2c_adapter *adapter, int address, int kind); 131 static int smsc47m1_detach_client(struct i2c_client *client); 132 133 static int smsc47m1_read_value(struct i2c_client *client, u8 reg); 134 static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value); 135 136 static struct smsc47m1_data *smsc47m1_update_device(struct device *dev, 137 int init); 138 139 140 static struct i2c_driver smsc47m1_driver = { 141 .owner = THIS_MODULE, 142 .name = "smsc47m1", 143 .id = I2C_DRIVERID_SMSC47M1, 144 .flags = I2C_DF_NOTIFY, 145 .attach_adapter = smsc47m1_attach_adapter, 146 .detach_client = smsc47m1_detach_client, 147 }; 148 149 /* nr is 0 or 1 in the callback functions below */ 150 151 static ssize_t get_fan(struct device *dev, char *buf, int nr) 152 { 153 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); 154 /* This chip (stupidly) stops monitoring fan speed if PWM is 155 enabled and duty cycle is 0%. This is fine if the monitoring 156 and control concern the same fan, but troublesome if they are 157 not (which could as well happen). */ 158 int rpm = (data->pwm[nr] & 0x7F) == 0x00 ? 0 : 159 FAN_FROM_REG(data->fan[nr], 160 DIV_FROM_REG(data->fan_div[nr]), 161 data->fan_preload[nr]); 162 return sprintf(buf, "%d\n", rpm); 163 } 164 165 static ssize_t get_fan_min(struct device *dev, char *buf, int nr) 166 { 167 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); 168 int rpm = MIN_FROM_REG(data->fan_preload[nr], 169 DIV_FROM_REG(data->fan_div[nr])); 170 return sprintf(buf, "%d\n", rpm); 171 } 172 173 static ssize_t get_fan_div(struct device *dev, char *buf, int nr) 174 { 175 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); 176 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); 177 } 178 179 static ssize_t get_pwm(struct device *dev, char *buf, int nr) 180 { 181 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); 182 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr])); 183 } 184 185 static ssize_t get_pwm_en(struct device *dev, char *buf, int nr) 186 { 187 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); 188 return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[nr])); 189 } 190 191 static ssize_t get_alarms(struct device *dev, struct device_attribute *attr, char *buf) 192 { 193 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); 194 return sprintf(buf, "%d\n", data->alarms); 195 } 196 197 static ssize_t set_fan_min(struct device *dev, const char *buf, 198 size_t count, int nr) 199 { 200 struct i2c_client *client = to_i2c_client(dev); 201 struct smsc47m1_data *data = i2c_get_clientdata(client); 202 long rpmdiv, val = simple_strtol(buf, NULL, 10); 203 204 down(&data->update_lock); 205 rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]); 206 207 if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) { 208 up(&data->update_lock); 209 return -EINVAL; 210 } 211 212 data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv); 213 smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD(nr), 214 data->fan_preload[nr]); 215 up(&data->update_lock); 216 217 return count; 218 } 219 220 /* Note: we save and restore the fan minimum here, because its value is 221 determined in part by the fan clock divider. This follows the principle 222 of least suprise; the user doesn't expect the fan minimum to change just 223 because the divider changed. */ 224 static ssize_t set_fan_div(struct device *dev, const char *buf, 225 size_t count, int nr) 226 { 227 struct i2c_client *client = to_i2c_client(dev); 228 struct smsc47m1_data *data = i2c_get_clientdata(client); 229 230 long new_div = simple_strtol(buf, NULL, 10), tmp; 231 u8 old_div = DIV_FROM_REG(data->fan_div[nr]); 232 233 if (new_div == old_div) /* No change */ 234 return count; 235 236 down(&data->update_lock); 237 switch (new_div) { 238 case 1: data->fan_div[nr] = 0; break; 239 case 2: data->fan_div[nr] = 1; break; 240 case 4: data->fan_div[nr] = 2; break; 241 case 8: data->fan_div[nr] = 3; break; 242 default: 243 up(&data->update_lock); 244 return -EINVAL; 245 } 246 247 tmp = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV) & 0x0F; 248 tmp |= (data->fan_div[0] << 4) | (data->fan_div[1] << 6); 249 smsc47m1_write_value(client, SMSC47M1_REG_FANDIV, tmp); 250 251 /* Preserve fan min */ 252 tmp = 192 - (old_div * (192 - data->fan_preload[nr]) 253 + new_div / 2) / new_div; 254 data->fan_preload[nr] = SENSORS_LIMIT(tmp, 0, 191); 255 smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD(nr), 256 data->fan_preload[nr]); 257 up(&data->update_lock); 258 259 return count; 260 } 261 262 static ssize_t set_pwm(struct device *dev, const char *buf, 263 size_t count, int nr) 264 { 265 struct i2c_client *client = to_i2c_client(dev); 266 struct smsc47m1_data *data = i2c_get_clientdata(client); 267 268 long val = simple_strtol(buf, NULL, 10); 269 270 if (val < 0 || val > 255) 271 return -EINVAL; 272 273 down(&data->update_lock); 274 data->pwm[nr] &= 0x81; /* Preserve additional bits */ 275 data->pwm[nr] |= PWM_TO_REG(val); 276 smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr), 277 data->pwm[nr]); 278 up(&data->update_lock); 279 280 return count; 281 } 282 283 static ssize_t set_pwm_en(struct device *dev, const char *buf, 284 size_t count, int nr) 285 { 286 struct i2c_client *client = to_i2c_client(dev); 287 struct smsc47m1_data *data = i2c_get_clientdata(client); 288 289 long val = simple_strtol(buf, NULL, 10); 290 291 if (val != 0 && val != 1) 292 return -EINVAL; 293 294 down(&data->update_lock); 295 data->pwm[nr] &= 0xFE; /* preserve the other bits */ 296 data->pwm[nr] |= !val; 297 smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr), 298 data->pwm[nr]); 299 up(&data->update_lock); 300 301 return count; 302 } 303 304 #define fan_present(offset) \ 305 static ssize_t get_fan##offset (struct device *dev, struct device_attribute *attr, char *buf) \ 306 { \ 307 return get_fan(dev, buf, offset - 1); \ 308 } \ 309 static ssize_t get_fan##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \ 310 { \ 311 return get_fan_min(dev, buf, offset - 1); \ 312 } \ 313 static ssize_t set_fan##offset##_min (struct device *dev, struct device_attribute *attr, \ 314 const char *buf, size_t count) \ 315 { \ 316 return set_fan_min(dev, buf, count, offset - 1); \ 317 } \ 318 static ssize_t get_fan##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \ 319 { \ 320 return get_fan_div(dev, buf, offset - 1); \ 321 } \ 322 static ssize_t set_fan##offset##_div (struct device *dev, struct device_attribute *attr, \ 323 const char *buf, size_t count) \ 324 { \ 325 return set_fan_div(dev, buf, count, offset - 1); \ 326 } \ 327 static ssize_t get_pwm##offset (struct device *dev, struct device_attribute *attr, char *buf) \ 328 { \ 329 return get_pwm(dev, buf, offset - 1); \ 330 } \ 331 static ssize_t set_pwm##offset (struct device *dev, struct device_attribute *attr, \ 332 const char *buf, size_t count) \ 333 { \ 334 return set_pwm(dev, buf, count, offset - 1); \ 335 } \ 336 static ssize_t get_pwm##offset##_en (struct device *dev, struct device_attribute *attr, char *buf) \ 337 { \ 338 return get_pwm_en(dev, buf, offset - 1); \ 339 } \ 340 static ssize_t set_pwm##offset##_en (struct device *dev, struct device_attribute *attr, \ 341 const char *buf, size_t count) \ 342 { \ 343 return set_pwm_en(dev, buf, count, offset - 1); \ 344 } \ 345 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan##offset, \ 346 NULL); \ 347 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ 348 get_fan##offset##_min, set_fan##offset##_min); \ 349 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ 350 get_fan##offset##_div, set_fan##offset##_div); \ 351 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \ 352 get_pwm##offset, set_pwm##offset); \ 353 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \ 354 get_pwm##offset##_en, set_pwm##offset##_en); 355 356 fan_present(1); 357 fan_present(2); 358 359 static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL); 360 361 static int smsc47m1_attach_adapter(struct i2c_adapter *adapter) 362 { 363 if (!(adapter->class & I2C_CLASS_HWMON)) 364 return 0; 365 return i2c_detect(adapter, &addr_data, smsc47m1_detect); 366 } 367 368 static int smsc47m1_find(int *address) 369 { 370 u8 val; 371 372 superio_enter(); 373 val = superio_inb(SUPERIO_REG_DEVID); 374 375 /* 376 * SMSC LPC47M10x/LPC47M13x (device id 0x59), LPC47M14x (device id 377 * 0x5F) and LPC47B27x (device id 0x51) have fan control. 378 * The LPC47M15x and LPC47M192 chips "with hardware monitoring block" 379 * can do much more besides (device id 0x60). 380 */ 381 if (val == 0x51) 382 printk(KERN_INFO "smsc47m1: Found SMSC LPC47B27x\n"); 383 else if (val == 0x59) 384 printk(KERN_INFO "smsc47m1: Found SMSC LPC47M10x/LPC47M13x\n"); 385 else if (val == 0x5F) 386 printk(KERN_INFO "smsc47m1: Found SMSC LPC47M14x\n"); 387 else if (val == 0x60) 388 printk(KERN_INFO "smsc47m1: Found SMSC LPC47M15x/LPC47M192\n"); 389 else { 390 superio_exit(); 391 return -ENODEV; 392 } 393 394 superio_select(); 395 *address = (superio_inb(SUPERIO_REG_BASE) << 8) 396 | superio_inb(SUPERIO_REG_BASE + 1); 397 val = superio_inb(SUPERIO_REG_ACT); 398 if (*address == 0 || (val & 0x01) == 0) { 399 printk(KERN_INFO "smsc47m1: Device is disabled, will not use\n"); 400 superio_exit(); 401 return -ENODEV; 402 } 403 404 superio_exit(); 405 return 0; 406 } 407 408 static int smsc47m1_detect(struct i2c_adapter *adapter, int address, int kind) 409 { 410 struct i2c_client *new_client; 411 struct smsc47m1_data *data; 412 int err = 0; 413 int fan1, fan2, pwm1, pwm2; 414 415 if (!i2c_is_isa_adapter(adapter)) { 416 return 0; 417 } 418 419 if (!request_region(address, SMSC_EXTENT, smsc47m1_driver.name)) { 420 dev_err(&adapter->dev, "Region 0x%x already in use!\n", address); 421 return -EBUSY; 422 } 423 424 if (!(data = kmalloc(sizeof(struct smsc47m1_data), GFP_KERNEL))) { 425 err = -ENOMEM; 426 goto error_release; 427 } 428 memset(data, 0x00, sizeof(struct smsc47m1_data)); 429 430 new_client = &data->client; 431 i2c_set_clientdata(new_client, data); 432 new_client->addr = address; 433 init_MUTEX(&data->lock); 434 new_client->adapter = adapter; 435 new_client->driver = &smsc47m1_driver; 436 new_client->flags = 0; 437 438 strlcpy(new_client->name, "smsc47m1", I2C_NAME_SIZE); 439 init_MUTEX(&data->update_lock); 440 441 /* If no function is properly configured, there's no point in 442 actually registering the chip. */ 443 fan1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(0)) & 0x05) 444 == 0x05; 445 fan2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(1)) & 0x05) 446 == 0x05; 447 pwm1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(0)) & 0x05) 448 == 0x04; 449 pwm2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(1)) & 0x05) 450 == 0x04; 451 if (!(fan1 || fan2 || pwm1 || pwm2)) { 452 dev_warn(&new_client->dev, "Device is not configured, will not use\n"); 453 err = -ENODEV; 454 goto error_free; 455 } 456 457 if ((err = i2c_attach_client(new_client))) 458 goto error_free; 459 460 /* Some values (fan min, clock dividers, pwm registers) may be 461 needed before any update is triggered, so we better read them 462 at least once here. We don't usually do it that way, but in 463 this particular case, manually reading 5 registers out of 8 464 doesn't make much sense and we're better using the existing 465 function. */ 466 smsc47m1_update_device(&new_client->dev, 1); 467 468 /* Register sysfs hooks */ 469 data->class_dev = hwmon_device_register(&new_client->dev); 470 if (IS_ERR(data->class_dev)) { 471 err = PTR_ERR(data->class_dev); 472 goto error_detach; 473 } 474 475 if (fan1) { 476 device_create_file(&new_client->dev, &dev_attr_fan1_input); 477 device_create_file(&new_client->dev, &dev_attr_fan1_min); 478 device_create_file(&new_client->dev, &dev_attr_fan1_div); 479 } else 480 dev_dbg(&new_client->dev, "Fan 1 not enabled by hardware, " 481 "skipping\n"); 482 483 if (fan2) { 484 device_create_file(&new_client->dev, &dev_attr_fan2_input); 485 device_create_file(&new_client->dev, &dev_attr_fan2_min); 486 device_create_file(&new_client->dev, &dev_attr_fan2_div); 487 } else 488 dev_dbg(&new_client->dev, "Fan 2 not enabled by hardware, " 489 "skipping\n"); 490 491 if (pwm1) { 492 device_create_file(&new_client->dev, &dev_attr_pwm1); 493 device_create_file(&new_client->dev, &dev_attr_pwm1_enable); 494 } else 495 dev_dbg(&new_client->dev, "PWM 1 not enabled by hardware, " 496 "skipping\n"); 497 if (pwm2) { 498 device_create_file(&new_client->dev, &dev_attr_pwm2); 499 device_create_file(&new_client->dev, &dev_attr_pwm2_enable); 500 } else 501 dev_dbg(&new_client->dev, "PWM 2 not enabled by hardware, " 502 "skipping\n"); 503 504 device_create_file(&new_client->dev, &dev_attr_alarms); 505 506 return 0; 507 508 error_detach: 509 i2c_detach_client(new_client); 510 error_free: 511 kfree(data); 512 error_release: 513 release_region(address, SMSC_EXTENT); 514 return err; 515 } 516 517 static int smsc47m1_detach_client(struct i2c_client *client) 518 { 519 struct smsc47m1_data *data = i2c_get_clientdata(client); 520 int err; 521 522 hwmon_device_unregister(data->class_dev); 523 524 if ((err = i2c_detach_client(client))) { 525 dev_err(&client->dev, "Client deregistration failed, " 526 "client not detached.\n"); 527 return err; 528 } 529 530 release_region(client->addr, SMSC_EXTENT); 531 kfree(data); 532 533 return 0; 534 } 535 536 static int smsc47m1_read_value(struct i2c_client *client, u8 reg) 537 { 538 int res; 539 540 down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock); 541 res = inb_p(client->addr + reg); 542 up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock); 543 return res; 544 } 545 546 static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value) 547 { 548 down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock); 549 outb_p(value, client->addr + reg); 550 up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock); 551 } 552 553 static struct smsc47m1_data *smsc47m1_update_device(struct device *dev, 554 int init) 555 { 556 struct i2c_client *client = to_i2c_client(dev); 557 struct smsc47m1_data *data = i2c_get_clientdata(client); 558 559 down(&data->update_lock); 560 561 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) || init) { 562 int i; 563 564 for (i = 0; i < 2; i++) { 565 data->fan[i] = smsc47m1_read_value(client, 566 SMSC47M1_REG_FAN(i)); 567 data->fan_preload[i] = smsc47m1_read_value(client, 568 SMSC47M1_REG_FAN_PRELOAD(i)); 569 data->pwm[i] = smsc47m1_read_value(client, 570 SMSC47M1_REG_PWM(i)); 571 } 572 573 i = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV); 574 data->fan_div[0] = (i >> 4) & 0x03; 575 data->fan_div[1] = i >> 6; 576 577 data->alarms = smsc47m1_read_value(client, 578 SMSC47M1_REG_ALARM) >> 6; 579 /* Clear alarms if needed */ 580 if (data->alarms) 581 smsc47m1_write_value(client, SMSC47M1_REG_ALARM, 0xC0); 582 583 data->last_updated = jiffies; 584 } 585 586 up(&data->update_lock); 587 return data; 588 } 589 590 static int __init sm_smsc47m1_init(void) 591 { 592 if (smsc47m1_find(normal_isa)) { 593 return -ENODEV; 594 } 595 596 return i2c_isa_add_driver(&smsc47m1_driver); 597 } 598 599 static void __exit sm_smsc47m1_exit(void) 600 { 601 i2c_isa_del_driver(&smsc47m1_driver); 602 } 603 604 MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>"); 605 MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver"); 606 MODULE_LICENSE("GPL"); 607 608 module_init(sm_smsc47m1_init); 609 module_exit(sm_smsc47m1_exit); 610