Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v2.6.17-rc3 2071 lines 57 kB view raw
1/* 2 * Device driver for the thermostats & fan controller of the 3 * Apple G5 "PowerMac7,2" desktop machines. 4 * 5 * (c) Copyright IBM Corp. 2003-2004 6 * 7 * Maintained by: Benjamin Herrenschmidt 8 * <benh@kernel.crashing.org> 9 * 10 * 11 * The algorithm used is the PID control algorithm, used the same 12 * way the published Darwin code does, using the same values that 13 * are present in the Darwin 7.0 snapshot property lists. 14 * 15 * As far as the CPUs control loops are concerned, I use the 16 * calibration & PID constants provided by the EEPROM, 17 * I do _not_ embed any value from the property lists, as the ones 18 * provided by Darwin 7.0 seem to always have an older version that 19 * what I've seen on the actual computers. 20 * It would be interesting to verify that though. Darwin has a 21 * version code of 1.0.0d11 for all control loops it seems, while 22 * so far, the machines EEPROMs contain a dataset versioned 1.0.0f 23 * 24 * Darwin doesn't provide source to all parts, some missing 25 * bits like the AppleFCU driver or the actual scale of some 26 * of the values returned by sensors had to be "guessed" some 27 * way... or based on what Open Firmware does. 28 * 29 * I didn't yet figure out how to get the slots power consumption 30 * out of the FCU, so that part has not been implemented yet and 31 * the slots fan is set to a fixed 50% PWM, hoping this value is 32 * safe enough ... 33 * 34 * Note: I have observed strange oscillations of the CPU control 35 * loop on a dual G5 here. When idle, the CPU exhaust fan tend to 36 * oscillates slowly (over several minutes) between the minimum 37 * of 300RPMs and approx. 1000 RPMs. I don't know what is causing 38 * this, it could be some incorrect constant or an error in the 39 * way I ported the algorithm, or it could be just normal. I 40 * don't have full understanding on the way Apple tweaked the PID 41 * algorithm for the CPU control, it is definitely not a standard 42 * implementation... 43 * 44 * TODO: - Check MPU structure version/signature 45 * - Add things like /sbin/overtemp for non-critical 46 * overtemp conditions so userland can take some policy 47 * decisions, like slewing down CPUs 48 * - Deal with fan and i2c failures in a better way 49 * - Maybe do a generic PID based on params used for 50 * U3 and Drives ? Definitely need to factor code a bit 51 * bettter... also make sensor detection more robust using 52 * the device-tree to probe for them 53 * - Figure out how to get the slots consumption and set the 54 * slots fan accordingly 55 * 56 * History: 57 * 58 * Nov. 13, 2003 : 0.5 59 * - First release 60 * 61 * Nov. 14, 2003 : 0.6 62 * - Read fan speed from FCU, low level fan routines now deal 63 * with errors & check fan status, though higher level don't 64 * do much. 65 * - Move a bunch of definitions to .h file 66 * 67 * Nov. 18, 2003 : 0.7 68 * - Fix build on ppc64 kernel 69 * - Move back statics definitions to .c file 70 * - Avoid calling schedule_timeout with a negative number 71 * 72 * Dec. 18, 2003 : 0.8 73 * - Fix typo when reading back fan speed on 2 CPU machines 74 * 75 * Mar. 11, 2004 : 0.9 76 * - Rework code accessing the ADC chips, make it more robust and 77 * closer to the chip spec. Also make sure it is configured properly, 78 * I've seen yet unexplained cases where on startup, I would have stale 79 * values in the configuration register 80 * - Switch back to use of target fan speed for PID, thus lowering 81 * pressure on i2c 82 * 83 * Oct. 20, 2004 : 1.1 84 * - Add device-tree lookup for fan IDs, should detect liquid cooling 85 * pumps when present 86 * - Enable driver for PowerMac7,3 machines 87 * - Split the U3/Backside cooling on U3 & U3H versions as Darwin does 88 * - Add new CPU cooling algorithm for machines with liquid cooling 89 * - Workaround for some PowerMac7,3 with empty "fan" node in the devtree 90 * - Fix a signed/unsigned compare issue in some PID loops 91 * 92 * Mar. 10, 2005 : 1.2 93 * - Add basic support for Xserve G5 94 * - Retreive pumps min/max from EEPROM image in device-tree (broken) 95 * - Use min/max macros here or there 96 * - Latest darwin updated U3H min fan speed to 20% PWM 97 * 98 */ 99 100#include <linux/config.h> 101#include <linux/types.h> 102#include <linux/module.h> 103#include <linux/errno.h> 104#include <linux/kernel.h> 105#include <linux/delay.h> 106#include <linux/sched.h> 107#include <linux/slab.h> 108#include <linux/init.h> 109#include <linux/spinlock.h> 110#include <linux/smp_lock.h> 111#include <linux/wait.h> 112#include <linux/reboot.h> 113#include <linux/kmod.h> 114#include <linux/i2c.h> 115#include <asm/prom.h> 116#include <asm/machdep.h> 117#include <asm/io.h> 118#include <asm/system.h> 119#include <asm/sections.h> 120#include <asm/of_device.h> 121#include <asm/macio.h> 122 123#include "therm_pm72.h" 124 125#define VERSION "1.2b2" 126 127#undef DEBUG 128 129#ifdef DEBUG 130#define DBG(args...) printk(args) 131#else 132#define DBG(args...) do { } while(0) 133#endif 134 135 136/* 137 * Driver statics 138 */ 139 140static struct of_device * of_dev; 141static struct i2c_adapter * u3_0; 142static struct i2c_adapter * u3_1; 143static struct i2c_adapter * k2; 144static struct i2c_client * fcu; 145static struct cpu_pid_state cpu_state[2]; 146static struct basckside_pid_params backside_params; 147static struct backside_pid_state backside_state; 148static struct drives_pid_state drives_state; 149static struct dimm_pid_state dimms_state; 150static int state; 151static int cpu_count; 152static int cpu_pid_type; 153static pid_t ctrl_task; 154static struct completion ctrl_complete; 155static int critical_state; 156static int rackmac; 157static s32 dimm_output_clamp; 158 159static DECLARE_MUTEX(driver_lock); 160 161/* 162 * We have 3 types of CPU PID control. One is "split" old style control 163 * for intake & exhaust fans, the other is "combined" control for both 164 * CPUs that also deals with the pumps when present. To be "compatible" 165 * with OS X at this point, we only use "COMBINED" on the machines that 166 * are identified as having the pumps (though that identification is at 167 * least dodgy). Ultimately, we could probably switch completely to this 168 * algorithm provided we hack it to deal with the UP case 169 */ 170#define CPU_PID_TYPE_SPLIT 0 171#define CPU_PID_TYPE_COMBINED 1 172#define CPU_PID_TYPE_RACKMAC 2 173 174/* 175 * This table describes all fans in the FCU. The "id" and "type" values 176 * are defaults valid for all earlier machines. Newer machines will 177 * eventually override the table content based on the device-tree 178 */ 179struct fcu_fan_table 180{ 181 char* loc; /* location code */ 182 int type; /* 0 = rpm, 1 = pwm, 2 = pump */ 183 int id; /* id or -1 */ 184}; 185 186#define FCU_FAN_RPM 0 187#define FCU_FAN_PWM 1 188 189#define FCU_FAN_ABSENT_ID -1 190 191#define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans) 192 193struct fcu_fan_table fcu_fans[] = { 194 [BACKSIDE_FAN_PWM_INDEX] = { 195 .loc = "BACKSIDE,SYS CTRLR FAN", 196 .type = FCU_FAN_PWM, 197 .id = BACKSIDE_FAN_PWM_DEFAULT_ID, 198 }, 199 [DRIVES_FAN_RPM_INDEX] = { 200 .loc = "DRIVE BAY", 201 .type = FCU_FAN_RPM, 202 .id = DRIVES_FAN_RPM_DEFAULT_ID, 203 }, 204 [SLOTS_FAN_PWM_INDEX] = { 205 .loc = "SLOT,PCI FAN", 206 .type = FCU_FAN_PWM, 207 .id = SLOTS_FAN_PWM_DEFAULT_ID, 208 }, 209 [CPUA_INTAKE_FAN_RPM_INDEX] = { 210 .loc = "CPU A INTAKE", 211 .type = FCU_FAN_RPM, 212 .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID, 213 }, 214 [CPUA_EXHAUST_FAN_RPM_INDEX] = { 215 .loc = "CPU A EXHAUST", 216 .type = FCU_FAN_RPM, 217 .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID, 218 }, 219 [CPUB_INTAKE_FAN_RPM_INDEX] = { 220 .loc = "CPU B INTAKE", 221 .type = FCU_FAN_RPM, 222 .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID, 223 }, 224 [CPUB_EXHAUST_FAN_RPM_INDEX] = { 225 .loc = "CPU B EXHAUST", 226 .type = FCU_FAN_RPM, 227 .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID, 228 }, 229 /* pumps aren't present by default, have to be looked up in the 230 * device-tree 231 */ 232 [CPUA_PUMP_RPM_INDEX] = { 233 .loc = "CPU A PUMP", 234 .type = FCU_FAN_RPM, 235 .id = FCU_FAN_ABSENT_ID, 236 }, 237 [CPUB_PUMP_RPM_INDEX] = { 238 .loc = "CPU B PUMP", 239 .type = FCU_FAN_RPM, 240 .id = FCU_FAN_ABSENT_ID, 241 }, 242 /* Xserve fans */ 243 [CPU_A1_FAN_RPM_INDEX] = { 244 .loc = "CPU A 1", 245 .type = FCU_FAN_RPM, 246 .id = FCU_FAN_ABSENT_ID, 247 }, 248 [CPU_A2_FAN_RPM_INDEX] = { 249 .loc = "CPU A 2", 250 .type = FCU_FAN_RPM, 251 .id = FCU_FAN_ABSENT_ID, 252 }, 253 [CPU_A3_FAN_RPM_INDEX] = { 254 .loc = "CPU A 3", 255 .type = FCU_FAN_RPM, 256 .id = FCU_FAN_ABSENT_ID, 257 }, 258 [CPU_B1_FAN_RPM_INDEX] = { 259 .loc = "CPU B 1", 260 .type = FCU_FAN_RPM, 261 .id = FCU_FAN_ABSENT_ID, 262 }, 263 [CPU_B2_FAN_RPM_INDEX] = { 264 .loc = "CPU B 2", 265 .type = FCU_FAN_RPM, 266 .id = FCU_FAN_ABSENT_ID, 267 }, 268 [CPU_B3_FAN_RPM_INDEX] = { 269 .loc = "CPU B 3", 270 .type = FCU_FAN_RPM, 271 .id = FCU_FAN_ABSENT_ID, 272 }, 273}; 274 275/* 276 * i2c_driver structure to attach to the host i2c controller 277 */ 278 279static int therm_pm72_attach(struct i2c_adapter *adapter); 280static int therm_pm72_detach(struct i2c_adapter *adapter); 281 282static struct i2c_driver therm_pm72_driver = 283{ 284 .driver = { 285 .name = "therm_pm72", 286 }, 287 .attach_adapter = therm_pm72_attach, 288 .detach_adapter = therm_pm72_detach, 289}; 290 291/* 292 * Utility function to create an i2c_client structure and 293 * attach it to one of u3 adapters 294 */ 295static struct i2c_client *attach_i2c_chip(int id, const char *name) 296{ 297 struct i2c_client *clt; 298 struct i2c_adapter *adap; 299 300 if (id & 0x200) 301 adap = k2; 302 else if (id & 0x100) 303 adap = u3_1; 304 else 305 adap = u3_0; 306 if (adap == NULL) 307 return NULL; 308 309 clt = kmalloc(sizeof(struct i2c_client), GFP_KERNEL); 310 if (clt == NULL) 311 return NULL; 312 memset(clt, 0, sizeof(struct i2c_client)); 313 314 clt->addr = (id >> 1) & 0x7f; 315 clt->adapter = adap; 316 clt->driver = &therm_pm72_driver; 317 strncpy(clt->name, name, I2C_NAME_SIZE-1); 318 319 if (i2c_attach_client(clt)) { 320 printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id); 321 kfree(clt); 322 return NULL; 323 } 324 return clt; 325} 326 327/* 328 * Utility function to get rid of the i2c_client structure 329 * (will also detach from the adapter hopepfully) 330 */ 331static void detach_i2c_chip(struct i2c_client *clt) 332{ 333 i2c_detach_client(clt); 334 kfree(clt); 335} 336 337/* 338 * Here are the i2c chip access wrappers 339 */ 340 341static void initialize_adc(struct cpu_pid_state *state) 342{ 343 int rc; 344 u8 buf[2]; 345 346 /* Read ADC the configuration register and cache it. We 347 * also make sure Config2 contains proper values, I've seen 348 * cases where we got stale grabage in there, thus preventing 349 * proper reading of conv. values 350 */ 351 352 /* Clear Config2 */ 353 buf[0] = 5; 354 buf[1] = 0; 355 i2c_master_send(state->monitor, buf, 2); 356 357 /* Read & cache Config1 */ 358 buf[0] = 1; 359 rc = i2c_master_send(state->monitor, buf, 1); 360 if (rc > 0) { 361 rc = i2c_master_recv(state->monitor, buf, 1); 362 if (rc > 0) { 363 state->adc_config = buf[0]; 364 DBG("ADC config reg: %02x\n", state->adc_config); 365 /* Disable shutdown mode */ 366 state->adc_config &= 0xfe; 367 buf[0] = 1; 368 buf[1] = state->adc_config; 369 rc = i2c_master_send(state->monitor, buf, 2); 370 } 371 } 372 if (rc <= 0) 373 printk(KERN_ERR "therm_pm72: Error reading ADC config" 374 " register !\n"); 375} 376 377static int read_smon_adc(struct cpu_pid_state *state, int chan) 378{ 379 int rc, data, tries = 0; 380 u8 buf[2]; 381 382 for (;;) { 383 /* Set channel */ 384 buf[0] = 1; 385 buf[1] = (state->adc_config & 0x1f) | (chan << 5); 386 rc = i2c_master_send(state->monitor, buf, 2); 387 if (rc <= 0) 388 goto error; 389 /* Wait for convertion */ 390 msleep(1); 391 /* Switch to data register */ 392 buf[0] = 4; 393 rc = i2c_master_send(state->monitor, buf, 1); 394 if (rc <= 0) 395 goto error; 396 /* Read result */ 397 rc = i2c_master_recv(state->monitor, buf, 2); 398 if (rc < 0) 399 goto error; 400 data = ((u16)buf[0]) << 8 | (u16)buf[1]; 401 return data >> 6; 402 error: 403 DBG("Error reading ADC, retrying...\n"); 404 if (++tries > 10) { 405 printk(KERN_ERR "therm_pm72: Error reading ADC !\n"); 406 return -1; 407 } 408 msleep(10); 409 } 410} 411 412static int read_lm87_reg(struct i2c_client * chip, int reg) 413{ 414 int rc, tries = 0; 415 u8 buf; 416 417 for (;;) { 418 /* Set address */ 419 buf = (u8)reg; 420 rc = i2c_master_send(chip, &buf, 1); 421 if (rc <= 0) 422 goto error; 423 rc = i2c_master_recv(chip, &buf, 1); 424 if (rc <= 0) 425 goto error; 426 return (int)buf; 427 error: 428 DBG("Error reading LM87, retrying...\n"); 429 if (++tries > 10) { 430 printk(KERN_ERR "therm_pm72: Error reading LM87 !\n"); 431 return -1; 432 } 433 msleep(10); 434 } 435} 436 437static int fan_read_reg(int reg, unsigned char *buf, int nb) 438{ 439 int tries, nr, nw; 440 441 buf[0] = reg; 442 tries = 0; 443 for (;;) { 444 nw = i2c_master_send(fcu, buf, 1); 445 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100) 446 break; 447 msleep(10); 448 ++tries; 449 } 450 if (nw <= 0) { 451 printk(KERN_ERR "Failure writing address to FCU: %d", nw); 452 return -EIO; 453 } 454 tries = 0; 455 for (;;) { 456 nr = i2c_master_recv(fcu, buf, nb); 457 if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100) 458 break; 459 msleep(10); 460 ++tries; 461 } 462 if (nr <= 0) 463 printk(KERN_ERR "Failure reading data from FCU: %d", nw); 464 return nr; 465} 466 467static int fan_write_reg(int reg, const unsigned char *ptr, int nb) 468{ 469 int tries, nw; 470 unsigned char buf[16]; 471 472 buf[0] = reg; 473 memcpy(buf+1, ptr, nb); 474 ++nb; 475 tries = 0; 476 for (;;) { 477 nw = i2c_master_send(fcu, buf, nb); 478 if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100) 479 break; 480 msleep(10); 481 ++tries; 482 } 483 if (nw < 0) 484 printk(KERN_ERR "Failure writing to FCU: %d", nw); 485 return nw; 486} 487 488static int start_fcu(void) 489{ 490 unsigned char buf = 0xff; 491 int rc; 492 493 rc = fan_write_reg(0xe, &buf, 1); 494 if (rc < 0) 495 return -EIO; 496 rc = fan_write_reg(0x2e, &buf, 1); 497 if (rc < 0) 498 return -EIO; 499 return 0; 500} 501 502static int set_rpm_fan(int fan_index, int rpm) 503{ 504 unsigned char buf[2]; 505 int rc, id; 506 507 if (fcu_fans[fan_index].type != FCU_FAN_RPM) 508 return -EINVAL; 509 id = fcu_fans[fan_index].id; 510 if (id == FCU_FAN_ABSENT_ID) 511 return -EINVAL; 512 513 if (rpm < 300) 514 rpm = 300; 515 else if (rpm > 8191) 516 rpm = 8191; 517 buf[0] = rpm >> 5; 518 buf[1] = rpm << 3; 519 rc = fan_write_reg(0x10 + (id * 2), buf, 2); 520 if (rc < 0) 521 return -EIO; 522 return 0; 523} 524 525static int get_rpm_fan(int fan_index, int programmed) 526{ 527 unsigned char failure; 528 unsigned char active; 529 unsigned char buf[2]; 530 int rc, id, reg_base; 531 532 if (fcu_fans[fan_index].type != FCU_FAN_RPM) 533 return -EINVAL; 534 id = fcu_fans[fan_index].id; 535 if (id == FCU_FAN_ABSENT_ID) 536 return -EINVAL; 537 538 rc = fan_read_reg(0xb, &failure, 1); 539 if (rc != 1) 540 return -EIO; 541 if ((failure & (1 << id)) != 0) 542 return -EFAULT; 543 rc = fan_read_reg(0xd, &active, 1); 544 if (rc != 1) 545 return -EIO; 546 if ((active & (1 << id)) == 0) 547 return -ENXIO; 548 549 /* Programmed value or real current speed */ 550 reg_base = programmed ? 0x10 : 0x11; 551 rc = fan_read_reg(reg_base + (id * 2), buf, 2); 552 if (rc != 2) 553 return -EIO; 554 555 return (buf[0] << 5) | buf[1] >> 3; 556} 557 558static int set_pwm_fan(int fan_index, int pwm) 559{ 560 unsigned char buf[2]; 561 int rc, id; 562 563 if (fcu_fans[fan_index].type != FCU_FAN_PWM) 564 return -EINVAL; 565 id = fcu_fans[fan_index].id; 566 if (id == FCU_FAN_ABSENT_ID) 567 return -EINVAL; 568 569 if (pwm < 10) 570 pwm = 10; 571 else if (pwm > 100) 572 pwm = 100; 573 pwm = (pwm * 2559) / 1000; 574 buf[0] = pwm; 575 rc = fan_write_reg(0x30 + (id * 2), buf, 1); 576 if (rc < 0) 577 return rc; 578 return 0; 579} 580 581static int get_pwm_fan(int fan_index) 582{ 583 unsigned char failure; 584 unsigned char active; 585 unsigned char buf[2]; 586 int rc, id; 587 588 if (fcu_fans[fan_index].type != FCU_FAN_PWM) 589 return -EINVAL; 590 id = fcu_fans[fan_index].id; 591 if (id == FCU_FAN_ABSENT_ID) 592 return -EINVAL; 593 594 rc = fan_read_reg(0x2b, &failure, 1); 595 if (rc != 1) 596 return -EIO; 597 if ((failure & (1 << id)) != 0) 598 return -EFAULT; 599 rc = fan_read_reg(0x2d, &active, 1); 600 if (rc != 1) 601 return -EIO; 602 if ((active & (1 << id)) == 0) 603 return -ENXIO; 604 605 /* Programmed value or real current speed */ 606 rc = fan_read_reg(0x30 + (id * 2), buf, 1); 607 if (rc != 1) 608 return -EIO; 609 610 return (buf[0] * 1000) / 2559; 611} 612 613/* 614 * Utility routine to read the CPU calibration EEPROM data 615 * from the device-tree 616 */ 617static int read_eeprom(int cpu, struct mpu_data *out) 618{ 619 struct device_node *np; 620 char nodename[64]; 621 u8 *data; 622 int len; 623 624 /* prom.c routine for finding a node by path is a bit brain dead 625 * and requires exact @xxx unit numbers. This is a bit ugly but 626 * will work for these machines 627 */ 628 sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0); 629 np = of_find_node_by_path(nodename); 630 if (np == NULL) { 631 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n"); 632 return -ENODEV; 633 } 634 data = (u8 *)get_property(np, "cpuid", &len); 635 if (data == NULL) { 636 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n"); 637 of_node_put(np); 638 return -ENODEV; 639 } 640 memcpy(out, data, sizeof(struct mpu_data)); 641 of_node_put(np); 642 643 return 0; 644} 645 646static void fetch_cpu_pumps_minmax(void) 647{ 648 struct cpu_pid_state *state0 = &cpu_state[0]; 649 struct cpu_pid_state *state1 = &cpu_state[1]; 650 u16 pump_min = 0, pump_max = 0xffff; 651 u16 tmp[4]; 652 653 /* Try to fetch pumps min/max infos from eeprom */ 654 655 memcpy(&tmp, &state0->mpu.processor_part_num, 8); 656 if (tmp[0] != 0xffff && tmp[1] != 0xffff) { 657 pump_min = max(pump_min, tmp[0]); 658 pump_max = min(pump_max, tmp[1]); 659 } 660 if (tmp[2] != 0xffff && tmp[3] != 0xffff) { 661 pump_min = max(pump_min, tmp[2]); 662 pump_max = min(pump_max, tmp[3]); 663 } 664 665 /* Double check the values, this _IS_ needed as the EEPROM on 666 * some dual 2.5Ghz G5s seem, at least, to have both min & max 667 * same to the same value ... (grrrr) 668 */ 669 if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) { 670 pump_min = CPU_PUMP_OUTPUT_MIN; 671 pump_max = CPU_PUMP_OUTPUT_MAX; 672 } 673 674 state0->pump_min = state1->pump_min = pump_min; 675 state0->pump_max = state1->pump_max = pump_max; 676} 677 678/* 679 * Now, unfortunately, sysfs doesn't give us a nice void * we could 680 * pass around to the attribute functions, so we don't really have 681 * choice but implement a bunch of them... 682 * 683 * That sucks a bit, we take the lock because FIX32TOPRINT evaluates 684 * the input twice... I accept patches :) 685 */ 686#define BUILD_SHOW_FUNC_FIX(name, data) \ 687static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \ 688{ \ 689 ssize_t r; \ 690 down(&driver_lock); \ 691 r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \ 692 up(&driver_lock); \ 693 return r; \ 694} 695#define BUILD_SHOW_FUNC_INT(name, data) \ 696static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \ 697{ \ 698 return sprintf(buf, "%d", data); \ 699} 700 701BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp) 702BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage) 703BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a) 704BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm) 705BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm) 706 707BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp) 708BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage) 709BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a) 710BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm) 711BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm) 712 713BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp) 714BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm) 715 716BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp) 717BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm) 718 719BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp) 720 721static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL); 722static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL); 723static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL); 724static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL); 725static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL); 726 727static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL); 728static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL); 729static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL); 730static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL); 731static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL); 732 733static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL); 734static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL); 735 736static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL); 737static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL); 738 739static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL); 740 741/* 742 * CPUs fans control loop 743 */ 744 745static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power) 746{ 747 s32 ltemp, volts, amps; 748 int index, rc = 0; 749 750 /* Default (in case of error) */ 751 *temp = state->cur_temp; 752 *power = state->cur_power; 753 754 if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) 755 index = (state->index == 0) ? 756 CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX; 757 else 758 index = (state->index == 0) ? 759 CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX; 760 761 /* Read current fan status */ 762 rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED); 763 if (rc < 0) { 764 /* XXX What do we do now ? Nothing for now, keep old value, but 765 * return error upstream 766 */ 767 DBG(" cpu %d, fan reading error !\n", state->index); 768 } else { 769 state->rpm = rc; 770 DBG(" cpu %d, exhaust RPM: %d\n", state->index, state->rpm); 771 } 772 773 /* Get some sensor readings and scale it */ 774 ltemp = read_smon_adc(state, 1); 775 if (ltemp == -1) { 776 /* XXX What do we do now ? */ 777 state->overtemp++; 778 if (rc == 0) 779 rc = -EIO; 780 DBG(" cpu %d, temp reading error !\n", state->index); 781 } else { 782 /* Fixup temperature according to diode calibration 783 */ 784 DBG(" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n", 785 state->index, 786 ltemp, state->mpu.mdiode, state->mpu.bdiode); 787 *temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2; 788 state->last_temp = *temp; 789 DBG(" temp: %d.%03d\n", FIX32TOPRINT((*temp))); 790 } 791 792 /* 793 * Read voltage & current and calculate power 794 */ 795 volts = read_smon_adc(state, 3); 796 amps = read_smon_adc(state, 4); 797 798 /* Scale voltage and current raw sensor values according to fixed scales 799 * obtained in Darwin and calculate power from I and V 800 */ 801 volts *= ADC_CPU_VOLTAGE_SCALE; 802 amps *= ADC_CPU_CURRENT_SCALE; 803 *power = (((u64)volts) * ((u64)amps)) >> 16; 804 state->voltage = volts; 805 state->current_a = amps; 806 state->last_power = *power; 807 808 DBG(" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n", 809 state->index, FIX32TOPRINT(state->current_a), 810 FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power)); 811 812 return 0; 813} 814 815static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power) 816{ 817 s32 power_target, integral, derivative, proportional, adj_in_target, sval; 818 s64 integ_p, deriv_p, prop_p, sum; 819 int i; 820 821 /* Calculate power target value (could be done once for all) 822 * and convert to a 16.16 fp number 823 */ 824 power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16; 825 DBG(" power target: %d.%03d, error: %d.%03d\n", 826 FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power)); 827 828 /* Store temperature and power in history array */ 829 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE; 830 state->temp_history[state->cur_temp] = temp; 831 state->cur_power = (state->cur_power + 1) % state->count_power; 832 state->power_history[state->cur_power] = power; 833 state->error_history[state->cur_power] = power_target - power; 834 835 /* If first loop, fill the history table */ 836 if (state->first) { 837 for (i = 0; i < (state->count_power - 1); i++) { 838 state->cur_power = (state->cur_power + 1) % state->count_power; 839 state->power_history[state->cur_power] = power; 840 state->error_history[state->cur_power] = power_target - power; 841 } 842 for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) { 843 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE; 844 state->temp_history[state->cur_temp] = temp; 845 } 846 state->first = 0; 847 } 848 849 /* Calculate the integral term normally based on the "power" values */ 850 sum = 0; 851 integral = 0; 852 for (i = 0; i < state->count_power; i++) 853 integral += state->error_history[i]; 854 integral *= CPU_PID_INTERVAL; 855 DBG(" integral: %08x\n", integral); 856 857 /* Calculate the adjusted input (sense value). 858 * G_r is 12.20 859 * integ is 16.16 860 * so the result is 28.36 861 * 862 * input target is mpu.ttarget, input max is mpu.tmax 863 */ 864 integ_p = ((s64)state->mpu.pid_gr) * (s64)integral; 865 DBG(" integ_p: %d\n", (int)(integ_p >> 36)); 866 sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff); 867 adj_in_target = (state->mpu.ttarget << 16); 868 if (adj_in_target > sval) 869 adj_in_target = sval; 870 DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target), 871 state->mpu.ttarget); 872 873 /* Calculate the derivative term */ 874 derivative = state->temp_history[state->cur_temp] - 875 state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1) 876 % CPU_TEMP_HISTORY_SIZE]; 877 derivative /= CPU_PID_INTERVAL; 878 deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative; 879 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36)); 880 sum += deriv_p; 881 882 /* Calculate the proportional term */ 883 proportional = temp - adj_in_target; 884 prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional; 885 DBG(" prop_p: %d\n", (int)(prop_p >> 36)); 886 sum += prop_p; 887 888 /* Scale sum */ 889 sum >>= 36; 890 891 DBG(" sum: %d\n", (int)sum); 892 state->rpm += (s32)sum; 893} 894 895static void do_monitor_cpu_combined(void) 896{ 897 struct cpu_pid_state *state0 = &cpu_state[0]; 898 struct cpu_pid_state *state1 = &cpu_state[1]; 899 s32 temp0, power0, temp1, power1; 900 s32 temp_combi, power_combi; 901 int rc, intake, pump; 902 903 rc = do_read_one_cpu_values(state0, &temp0, &power0); 904 if (rc < 0) { 905 /* XXX What do we do now ? */ 906 } 907 state1->overtemp = 0; 908 rc = do_read_one_cpu_values(state1, &temp1, &power1); 909 if (rc < 0) { 910 /* XXX What do we do now ? */ 911 } 912 if (state1->overtemp) 913 state0->overtemp++; 914 915 temp_combi = max(temp0, temp1); 916 power_combi = max(power0, power1); 917 918 /* Check tmax, increment overtemp if we are there. At tmax+8, we go 919 * full blown immediately and try to trigger a shutdown 920 */ 921 if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) { 922 printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n", 923 temp_combi >> 16); 924 state0->overtemp += CPU_MAX_OVERTEMP / 4; 925 } else if (temp_combi > (state0->mpu.tmax << 16)) 926 state0->overtemp++; 927 else 928 state0->overtemp = 0; 929 if (state0->overtemp >= CPU_MAX_OVERTEMP) 930 critical_state = 1; 931 if (state0->overtemp > 0) { 932 state0->rpm = state0->mpu.rmaxn_exhaust_fan; 933 state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan; 934 pump = state0->pump_max; 935 goto do_set_fans; 936 } 937 938 /* Do the PID */ 939 do_cpu_pid(state0, temp_combi, power_combi); 940 941 /* Range check */ 942 state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan); 943 state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan); 944 945 /* Calculate intake fan speed */ 946 intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16; 947 intake = max(intake, (int)state0->mpu.rminn_intake_fan); 948 intake = min(intake, (int)state0->mpu.rmaxn_intake_fan); 949 state0->intake_rpm = intake; 950 951 /* Calculate pump speed */ 952 pump = (state0->rpm * state0->pump_max) / 953 state0->mpu.rmaxn_exhaust_fan; 954 pump = min(pump, state0->pump_max); 955 pump = max(pump, state0->pump_min); 956 957 do_set_fans: 958 /* We copy values from state 0 to state 1 for /sysfs */ 959 state1->rpm = state0->rpm; 960 state1->intake_rpm = state0->intake_rpm; 961 962 DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n", 963 state1->index, (int)state1->rpm, intake, pump, state1->overtemp); 964 965 /* We should check for errors, shouldn't we ? But then, what 966 * do we do once the error occurs ? For FCU notified fan 967 * failures (-EFAULT) we probably want to notify userland 968 * some way... 969 */ 970 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake); 971 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm); 972 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake); 973 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm); 974 975 if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) 976 set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump); 977 if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) 978 set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump); 979} 980 981static void do_monitor_cpu_split(struct cpu_pid_state *state) 982{ 983 s32 temp, power; 984 int rc, intake; 985 986 /* Read current fan status */ 987 rc = do_read_one_cpu_values(state, &temp, &power); 988 if (rc < 0) { 989 /* XXX What do we do now ? */ 990 } 991 992 /* Check tmax, increment overtemp if we are there. At tmax+8, we go 993 * full blown immediately and try to trigger a shutdown 994 */ 995 if (temp >= ((state->mpu.tmax + 8) << 16)) { 996 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum" 997 " (%d) !\n", 998 state->index, temp >> 16); 999 state->overtemp += CPU_MAX_OVERTEMP / 4; 1000 } else if (temp > (state->mpu.tmax << 16)) 1001 state->overtemp++; 1002 else 1003 state->overtemp = 0; 1004 if (state->overtemp >= CPU_MAX_OVERTEMP) 1005 critical_state = 1; 1006 if (state->overtemp > 0) { 1007 state->rpm = state->mpu.rmaxn_exhaust_fan; 1008 state->intake_rpm = intake = state->mpu.rmaxn_intake_fan; 1009 goto do_set_fans; 1010 } 1011 1012 /* Do the PID */ 1013 do_cpu_pid(state, temp, power); 1014 1015 /* Range check */ 1016 state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan); 1017 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan); 1018 1019 /* Calculate intake fan */ 1020 intake = (state->rpm * CPU_INTAKE_SCALE) >> 16; 1021 intake = max(intake, (int)state->mpu.rminn_intake_fan); 1022 intake = min(intake, (int)state->mpu.rmaxn_intake_fan); 1023 state->intake_rpm = intake; 1024 1025 do_set_fans: 1026 DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n", 1027 state->index, (int)state->rpm, intake, state->overtemp); 1028 1029 /* We should check for errors, shouldn't we ? But then, what 1030 * do we do once the error occurs ? For FCU notified fan 1031 * failures (-EFAULT) we probably want to notify userland 1032 * some way... 1033 */ 1034 if (state->index == 0) { 1035 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake); 1036 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm); 1037 } else { 1038 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake); 1039 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm); 1040 } 1041} 1042 1043static void do_monitor_cpu_rack(struct cpu_pid_state *state) 1044{ 1045 s32 temp, power, fan_min; 1046 int rc; 1047 1048 /* Read current fan status */ 1049 rc = do_read_one_cpu_values(state, &temp, &power); 1050 if (rc < 0) { 1051 /* XXX What do we do now ? */ 1052 } 1053 1054 /* Check tmax, increment overtemp if we are there. At tmax+8, we go 1055 * full blown immediately and try to trigger a shutdown 1056 */ 1057 if (temp >= ((state->mpu.tmax + 8) << 16)) { 1058 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum" 1059 " (%d) !\n", 1060 state->index, temp >> 16); 1061 state->overtemp = CPU_MAX_OVERTEMP / 4; 1062 } else if (temp > (state->mpu.tmax << 16)) 1063 state->overtemp++; 1064 else 1065 state->overtemp = 0; 1066 if (state->overtemp >= CPU_MAX_OVERTEMP) 1067 critical_state = 1; 1068 if (state->overtemp > 0) { 1069 state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan; 1070 goto do_set_fans; 1071 } 1072 1073 /* Do the PID */ 1074 do_cpu_pid(state, temp, power); 1075 1076 /* Check clamp from dimms */ 1077 fan_min = dimm_output_clamp; 1078 fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan); 1079 1080 state->rpm = max(state->rpm, (int)fan_min); 1081 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan); 1082 state->intake_rpm = state->rpm; 1083 1084 do_set_fans: 1085 DBG("** CPU %d RPM: %d overtemp: %d\n", 1086 state->index, (int)state->rpm, state->overtemp); 1087 1088 /* We should check for errors, shouldn't we ? But then, what 1089 * do we do once the error occurs ? For FCU notified fan 1090 * failures (-EFAULT) we probably want to notify userland 1091 * some way... 1092 */ 1093 if (state->index == 0) { 1094 set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm); 1095 set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm); 1096 set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm); 1097 } else { 1098 set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm); 1099 set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm); 1100 set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm); 1101 } 1102} 1103 1104/* 1105 * Initialize the state structure for one CPU control loop 1106 */ 1107static int init_cpu_state(struct cpu_pid_state *state, int index) 1108{ 1109 state->index = index; 1110 state->first = 1; 1111 state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000; 1112 state->overtemp = 0; 1113 state->adc_config = 0x00; 1114 1115 1116 if (index == 0) 1117 state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor"); 1118 else if (index == 1) 1119 state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor"); 1120 if (state->monitor == NULL) 1121 goto fail; 1122 1123 if (read_eeprom(index, &state->mpu)) 1124 goto fail; 1125 1126 state->count_power = state->mpu.tguardband; 1127 if (state->count_power > CPU_POWER_HISTORY_SIZE) { 1128 printk(KERN_WARNING "Warning ! too many power history slots\n"); 1129 state->count_power = CPU_POWER_HISTORY_SIZE; 1130 } 1131 DBG("CPU %d Using %d power history entries\n", index, state->count_power); 1132 1133 if (index == 0) { 1134 device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature); 1135 device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage); 1136 device_create_file(&of_dev->dev, &dev_attr_cpu0_current); 1137 device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm); 1138 device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm); 1139 } else { 1140 device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature); 1141 device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage); 1142 device_create_file(&of_dev->dev, &dev_attr_cpu1_current); 1143 device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm); 1144 device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm); 1145 } 1146 1147 return 0; 1148 fail: 1149 if (state->monitor) 1150 detach_i2c_chip(state->monitor); 1151 state->monitor = NULL; 1152 1153 return -ENODEV; 1154} 1155 1156/* 1157 * Dispose of the state data for one CPU control loop 1158 */ 1159static void dispose_cpu_state(struct cpu_pid_state *state) 1160{ 1161 if (state->monitor == NULL) 1162 return; 1163 1164 if (state->index == 0) { 1165 device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature); 1166 device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage); 1167 device_remove_file(&of_dev->dev, &dev_attr_cpu0_current); 1168 device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm); 1169 device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm); 1170 } else { 1171 device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature); 1172 device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage); 1173 device_remove_file(&of_dev->dev, &dev_attr_cpu1_current); 1174 device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm); 1175 device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm); 1176 } 1177 1178 detach_i2c_chip(state->monitor); 1179 state->monitor = NULL; 1180} 1181 1182/* 1183 * Motherboard backside & U3 heatsink fan control loop 1184 */ 1185static void do_monitor_backside(struct backside_pid_state *state) 1186{ 1187 s32 temp, integral, derivative, fan_min; 1188 s64 integ_p, deriv_p, prop_p, sum; 1189 int i, rc; 1190 1191 if (--state->ticks != 0) 1192 return; 1193 state->ticks = backside_params.interval; 1194 1195 DBG("backside:\n"); 1196 1197 /* Check fan status */ 1198 rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX); 1199 if (rc < 0) { 1200 printk(KERN_WARNING "Error %d reading backside fan !\n", rc); 1201 /* XXX What do we do now ? */ 1202 } else 1203 state->pwm = rc; 1204 DBG(" current pwm: %d\n", state->pwm); 1205 1206 /* Get some sensor readings */ 1207 temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16; 1208 state->last_temp = temp; 1209 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp), 1210 FIX32TOPRINT(backside_params.input_target)); 1211 1212 /* Store temperature and error in history array */ 1213 state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE; 1214 state->sample_history[state->cur_sample] = temp; 1215 state->error_history[state->cur_sample] = temp - backside_params.input_target; 1216 1217 /* If first loop, fill the history table */ 1218 if (state->first) { 1219 for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) { 1220 state->cur_sample = (state->cur_sample + 1) % 1221 BACKSIDE_PID_HISTORY_SIZE; 1222 state->sample_history[state->cur_sample] = temp; 1223 state->error_history[state->cur_sample] = 1224 temp - backside_params.input_target; 1225 } 1226 state->first = 0; 1227 } 1228 1229 /* Calculate the integral term */ 1230 sum = 0; 1231 integral = 0; 1232 for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++) 1233 integral += state->error_history[i]; 1234 integral *= backside_params.interval; 1235 DBG(" integral: %08x\n", integral); 1236 integ_p = ((s64)backside_params.G_r) * (s64)integral; 1237 DBG(" integ_p: %d\n", (int)(integ_p >> 36)); 1238 sum += integ_p; 1239 1240 /* Calculate the derivative term */ 1241 derivative = state->error_history[state->cur_sample] - 1242 state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1) 1243 % BACKSIDE_PID_HISTORY_SIZE]; 1244 derivative /= backside_params.interval; 1245 deriv_p = ((s64)backside_params.G_d) * (s64)derivative; 1246 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36)); 1247 sum += deriv_p; 1248 1249 /* Calculate the proportional term */ 1250 prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]); 1251 DBG(" prop_p: %d\n", (int)(prop_p >> 36)); 1252 sum += prop_p; 1253 1254 /* Scale sum */ 1255 sum >>= 36; 1256 1257 DBG(" sum: %d\n", (int)sum); 1258 if (backside_params.additive) 1259 state->pwm += (s32)sum; 1260 else 1261 state->pwm = sum; 1262 1263 /* Check for clamp */ 1264 fan_min = (dimm_output_clamp * 100) / 14000; 1265 fan_min = max(fan_min, backside_params.output_min); 1266 1267 state->pwm = max(state->pwm, fan_min); 1268 state->pwm = min(state->pwm, backside_params.output_max); 1269 1270 DBG("** BACKSIDE PWM: %d\n", (int)state->pwm); 1271 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm); 1272} 1273 1274/* 1275 * Initialize the state structure for the backside fan control loop 1276 */ 1277static int init_backside_state(struct backside_pid_state *state) 1278{ 1279 struct device_node *u3; 1280 int u3h = 1; /* conservative by default */ 1281 1282 /* 1283 * There are different PID params for machines with U3 and machines 1284 * with U3H, pick the right ones now 1285 */ 1286 u3 = of_find_node_by_path("/u3@0,f8000000"); 1287 if (u3 != NULL) { 1288 u32 *vers = (u32 *)get_property(u3, "device-rev", NULL); 1289 if (vers) 1290 if (((*vers) & 0x3f) < 0x34) 1291 u3h = 0; 1292 of_node_put(u3); 1293 } 1294 1295 if (rackmac) { 1296 backside_params.G_d = BACKSIDE_PID_RACK_G_d; 1297 backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET; 1298 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN; 1299 backside_params.interval = BACKSIDE_PID_RACK_INTERVAL; 1300 backside_params.G_p = BACKSIDE_PID_RACK_G_p; 1301 backside_params.G_r = BACKSIDE_PID_G_r; 1302 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX; 1303 backside_params.additive = 0; 1304 } else if (u3h) { 1305 backside_params.G_d = BACKSIDE_PID_U3H_G_d; 1306 backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET; 1307 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN; 1308 backside_params.interval = BACKSIDE_PID_INTERVAL; 1309 backside_params.G_p = BACKSIDE_PID_G_p; 1310 backside_params.G_r = BACKSIDE_PID_G_r; 1311 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX; 1312 backside_params.additive = 1; 1313 } else { 1314 backside_params.G_d = BACKSIDE_PID_U3_G_d; 1315 backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET; 1316 backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN; 1317 backside_params.interval = BACKSIDE_PID_INTERVAL; 1318 backside_params.G_p = BACKSIDE_PID_G_p; 1319 backside_params.G_r = BACKSIDE_PID_G_r; 1320 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX; 1321 backside_params.additive = 1; 1322 } 1323 1324 state->ticks = 1; 1325 state->first = 1; 1326 state->pwm = 50; 1327 1328 state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp"); 1329 if (state->monitor == NULL) 1330 return -ENODEV; 1331 1332 device_create_file(&of_dev->dev, &dev_attr_backside_temperature); 1333 device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm); 1334 1335 return 0; 1336} 1337 1338/* 1339 * Dispose of the state data for the backside control loop 1340 */ 1341static void dispose_backside_state(struct backside_pid_state *state) 1342{ 1343 if (state->monitor == NULL) 1344 return; 1345 1346 device_remove_file(&of_dev->dev, &dev_attr_backside_temperature); 1347 device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm); 1348 1349 detach_i2c_chip(state->monitor); 1350 state->monitor = NULL; 1351} 1352 1353/* 1354 * Drives bay fan control loop 1355 */ 1356static void do_monitor_drives(struct drives_pid_state *state) 1357{ 1358 s32 temp, integral, derivative; 1359 s64 integ_p, deriv_p, prop_p, sum; 1360 int i, rc; 1361 1362 if (--state->ticks != 0) 1363 return; 1364 state->ticks = DRIVES_PID_INTERVAL; 1365 1366 DBG("drives:\n"); 1367 1368 /* Check fan status */ 1369 rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED); 1370 if (rc < 0) { 1371 printk(KERN_WARNING "Error %d reading drives fan !\n", rc); 1372 /* XXX What do we do now ? */ 1373 } else 1374 state->rpm = rc; 1375 DBG(" current rpm: %d\n", state->rpm); 1376 1377 /* Get some sensor readings */ 1378 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor, DS1775_TEMP)) << 8; 1379 state->last_temp = temp; 1380 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp), 1381 FIX32TOPRINT(DRIVES_PID_INPUT_TARGET)); 1382 1383 /* Store temperature and error in history array */ 1384 state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE; 1385 state->sample_history[state->cur_sample] = temp; 1386 state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET; 1387 1388 /* If first loop, fill the history table */ 1389 if (state->first) { 1390 for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) { 1391 state->cur_sample = (state->cur_sample + 1) % 1392 DRIVES_PID_HISTORY_SIZE; 1393 state->sample_history[state->cur_sample] = temp; 1394 state->error_history[state->cur_sample] = 1395 temp - DRIVES_PID_INPUT_TARGET; 1396 } 1397 state->first = 0; 1398 } 1399 1400 /* Calculate the integral term */ 1401 sum = 0; 1402 integral = 0; 1403 for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++) 1404 integral += state->error_history[i]; 1405 integral *= DRIVES_PID_INTERVAL; 1406 DBG(" integral: %08x\n", integral); 1407 integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral; 1408 DBG(" integ_p: %d\n", (int)(integ_p >> 36)); 1409 sum += integ_p; 1410 1411 /* Calculate the derivative term */ 1412 derivative = state->error_history[state->cur_sample] - 1413 state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1) 1414 % DRIVES_PID_HISTORY_SIZE]; 1415 derivative /= DRIVES_PID_INTERVAL; 1416 deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative; 1417 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36)); 1418 sum += deriv_p; 1419 1420 /* Calculate the proportional term */ 1421 prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]); 1422 DBG(" prop_p: %d\n", (int)(prop_p >> 36)); 1423 sum += prop_p; 1424 1425 /* Scale sum */ 1426 sum >>= 36; 1427 1428 DBG(" sum: %d\n", (int)sum); 1429 state->rpm += (s32)sum; 1430 1431 state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN); 1432 state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX); 1433 1434 DBG("** DRIVES RPM: %d\n", (int)state->rpm); 1435 set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm); 1436} 1437 1438/* 1439 * Initialize the state structure for the drives bay fan control loop 1440 */ 1441static int init_drives_state(struct drives_pid_state *state) 1442{ 1443 state->ticks = 1; 1444 state->first = 1; 1445 state->rpm = 1000; 1446 1447 state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp"); 1448 if (state->monitor == NULL) 1449 return -ENODEV; 1450 1451 device_create_file(&of_dev->dev, &dev_attr_drives_temperature); 1452 device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm); 1453 1454 return 0; 1455} 1456 1457/* 1458 * Dispose of the state data for the drives control loop 1459 */ 1460static void dispose_drives_state(struct drives_pid_state *state) 1461{ 1462 if (state->monitor == NULL) 1463 return; 1464 1465 device_remove_file(&of_dev->dev, &dev_attr_drives_temperature); 1466 device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm); 1467 1468 detach_i2c_chip(state->monitor); 1469 state->monitor = NULL; 1470} 1471 1472/* 1473 * DIMMs temp control loop 1474 */ 1475static void do_monitor_dimms(struct dimm_pid_state *state) 1476{ 1477 s32 temp, integral, derivative, fan_min; 1478 s64 integ_p, deriv_p, prop_p, sum; 1479 int i; 1480 1481 if (--state->ticks != 0) 1482 return; 1483 state->ticks = DIMM_PID_INTERVAL; 1484 1485 DBG("DIMM:\n"); 1486 1487 DBG(" current value: %d\n", state->output); 1488 1489 temp = read_lm87_reg(state->monitor, LM87_INT_TEMP); 1490 if (temp < 0) 1491 return; 1492 temp <<= 16; 1493 state->last_temp = temp; 1494 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp), 1495 FIX32TOPRINT(DIMM_PID_INPUT_TARGET)); 1496 1497 /* Store temperature and error in history array */ 1498 state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE; 1499 state->sample_history[state->cur_sample] = temp; 1500 state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET; 1501 1502 /* If first loop, fill the history table */ 1503 if (state->first) { 1504 for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) { 1505 state->cur_sample = (state->cur_sample + 1) % 1506 DIMM_PID_HISTORY_SIZE; 1507 state->sample_history[state->cur_sample] = temp; 1508 state->error_history[state->cur_sample] = 1509 temp - DIMM_PID_INPUT_TARGET; 1510 } 1511 state->first = 0; 1512 } 1513 1514 /* Calculate the integral term */ 1515 sum = 0; 1516 integral = 0; 1517 for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++) 1518 integral += state->error_history[i]; 1519 integral *= DIMM_PID_INTERVAL; 1520 DBG(" integral: %08x\n", integral); 1521 integ_p = ((s64)DIMM_PID_G_r) * (s64)integral; 1522 DBG(" integ_p: %d\n", (int)(integ_p >> 36)); 1523 sum += integ_p; 1524 1525 /* Calculate the derivative term */ 1526 derivative = state->error_history[state->cur_sample] - 1527 state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1) 1528 % DIMM_PID_HISTORY_SIZE]; 1529 derivative /= DIMM_PID_INTERVAL; 1530 deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative; 1531 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36)); 1532 sum += deriv_p; 1533 1534 /* Calculate the proportional term */ 1535 prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]); 1536 DBG(" prop_p: %d\n", (int)(prop_p >> 36)); 1537 sum += prop_p; 1538 1539 /* Scale sum */ 1540 sum >>= 36; 1541 1542 DBG(" sum: %d\n", (int)sum); 1543 state->output = (s32)sum; 1544 state->output = max(state->output, DIMM_PID_OUTPUT_MIN); 1545 state->output = min(state->output, DIMM_PID_OUTPUT_MAX); 1546 dimm_output_clamp = state->output; 1547 1548 DBG("** DIMM clamp value: %d\n", (int)state->output); 1549 1550 /* Backside PID is only every 5 seconds, force backside fan clamping now */ 1551 fan_min = (dimm_output_clamp * 100) / 14000; 1552 fan_min = max(fan_min, backside_params.output_min); 1553 if (backside_state.pwm < fan_min) { 1554 backside_state.pwm = fan_min; 1555 DBG(" -> applying clamp to backside fan now: %d !\n", fan_min); 1556 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min); 1557 } 1558} 1559 1560/* 1561 * Initialize the state structure for the DIMM temp control loop 1562 */ 1563static int init_dimms_state(struct dimm_pid_state *state) 1564{ 1565 state->ticks = 1; 1566 state->first = 1; 1567 state->output = 4000; 1568 1569 state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp"); 1570 if (state->monitor == NULL) 1571 return -ENODEV; 1572 1573 device_create_file(&of_dev->dev, &dev_attr_dimms_temperature); 1574 1575 return 0; 1576} 1577 1578/* 1579 * Dispose of the state data for the drives control loop 1580 */ 1581static void dispose_dimms_state(struct dimm_pid_state *state) 1582{ 1583 if (state->monitor == NULL) 1584 return; 1585 1586 device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature); 1587 1588 detach_i2c_chip(state->monitor); 1589 state->monitor = NULL; 1590} 1591 1592static int call_critical_overtemp(void) 1593{ 1594 char *argv[] = { critical_overtemp_path, NULL }; 1595 static char *envp[] = { "HOME=/", 1596 "TERM=linux", 1597 "PATH=/sbin:/usr/sbin:/bin:/usr/bin", 1598 NULL }; 1599 1600 return call_usermodehelper(critical_overtemp_path, argv, envp, 0); 1601} 1602 1603 1604/* 1605 * Here's the kernel thread that calls the various control loops 1606 */ 1607static int main_control_loop(void *x) 1608{ 1609 daemonize("kfand"); 1610 1611 DBG("main_control_loop started\n"); 1612 1613 down(&driver_lock); 1614 1615 if (start_fcu() < 0) { 1616 printk(KERN_ERR "kfand: failed to start FCU\n"); 1617 up(&driver_lock); 1618 goto out; 1619 } 1620 1621 /* Set the PCI fan once for now */ 1622 set_pwm_fan(SLOTS_FAN_PWM_INDEX, SLOTS_FAN_DEFAULT_PWM); 1623 1624 /* Initialize ADCs */ 1625 initialize_adc(&cpu_state[0]); 1626 if (cpu_state[1].monitor != NULL) 1627 initialize_adc(&cpu_state[1]); 1628 1629 up(&driver_lock); 1630 1631 while (state == state_attached) { 1632 unsigned long elapsed, start; 1633 1634 start = jiffies; 1635 1636 down(&driver_lock); 1637 1638 /* First, we always calculate the new DIMMs state on an Xserve */ 1639 if (rackmac) 1640 do_monitor_dimms(&dimms_state); 1641 1642 /* Then, the CPUs */ 1643 if (cpu_pid_type == CPU_PID_TYPE_COMBINED) 1644 do_monitor_cpu_combined(); 1645 else if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) { 1646 do_monitor_cpu_rack(&cpu_state[0]); 1647 if (cpu_state[1].monitor != NULL) 1648 do_monitor_cpu_rack(&cpu_state[1]); 1649 // better deal with UP 1650 } else { 1651 do_monitor_cpu_split(&cpu_state[0]); 1652 if (cpu_state[1].monitor != NULL) 1653 do_monitor_cpu_split(&cpu_state[1]); 1654 // better deal with UP 1655 } 1656 /* Then, the rest */ 1657 do_monitor_backside(&backside_state); 1658 if (!rackmac) 1659 do_monitor_drives(&drives_state); 1660 up(&driver_lock); 1661 1662 if (critical_state == 1) { 1663 printk(KERN_WARNING "Temperature control detected a critical condition\n"); 1664 printk(KERN_WARNING "Attempting to shut down...\n"); 1665 if (call_critical_overtemp()) { 1666 printk(KERN_WARNING "Can't call %s, power off now!\n", 1667 critical_overtemp_path); 1668 machine_power_off(); 1669 } 1670 } 1671 if (critical_state > 0) 1672 critical_state++; 1673 if (critical_state > MAX_CRITICAL_STATE) { 1674 printk(KERN_WARNING "Shutdown timed out, power off now !\n"); 1675 machine_power_off(); 1676 } 1677 1678 // FIXME: Deal with signals 1679 elapsed = jiffies - start; 1680 if (elapsed < HZ) 1681 schedule_timeout_interruptible(HZ - elapsed); 1682 } 1683 1684 out: 1685 DBG("main_control_loop ended\n"); 1686 1687 ctrl_task = 0; 1688 complete_and_exit(&ctrl_complete, 0); 1689} 1690 1691/* 1692 * Dispose the control loops when tearing down 1693 */ 1694static void dispose_control_loops(void) 1695{ 1696 dispose_cpu_state(&cpu_state[0]); 1697 dispose_cpu_state(&cpu_state[1]); 1698 dispose_backside_state(&backside_state); 1699 dispose_drives_state(&drives_state); 1700 dispose_dimms_state(&dimms_state); 1701} 1702 1703/* 1704 * Create the control loops. U3-0 i2c bus is up, so we can now 1705 * get to the various sensors 1706 */ 1707static int create_control_loops(void) 1708{ 1709 struct device_node *np; 1710 1711 /* Count CPUs from the device-tree, we don't care how many are 1712 * actually used by Linux 1713 */ 1714 cpu_count = 0; 1715 for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));) 1716 cpu_count++; 1717 1718 DBG("counted %d CPUs in the device-tree\n", cpu_count); 1719 1720 /* Decide the type of PID algorithm to use based on the presence of 1721 * the pumps, though that may not be the best way, that is good enough 1722 * for now 1723 */ 1724 if (rackmac) 1725 cpu_pid_type = CPU_PID_TYPE_RACKMAC; 1726 else if (machine_is_compatible("PowerMac7,3") 1727 && (cpu_count > 1) 1728 && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID 1729 && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) { 1730 printk(KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n"); 1731 cpu_pid_type = CPU_PID_TYPE_COMBINED; 1732 } else 1733 cpu_pid_type = CPU_PID_TYPE_SPLIT; 1734 1735 /* Create control loops for everything. If any fail, everything 1736 * fails 1737 */ 1738 if (init_cpu_state(&cpu_state[0], 0)) 1739 goto fail; 1740 if (cpu_pid_type == CPU_PID_TYPE_COMBINED) 1741 fetch_cpu_pumps_minmax(); 1742 1743 if (cpu_count > 1 && init_cpu_state(&cpu_state[1], 1)) 1744 goto fail; 1745 if (init_backside_state(&backside_state)) 1746 goto fail; 1747 if (rackmac && init_dimms_state(&dimms_state)) 1748 goto fail; 1749 if (!rackmac && init_drives_state(&drives_state)) 1750 goto fail; 1751 1752 DBG("all control loops up !\n"); 1753 1754 return 0; 1755 1756 fail: 1757 DBG("failure creating control loops, disposing\n"); 1758 1759 dispose_control_loops(); 1760 1761 return -ENODEV; 1762} 1763 1764/* 1765 * Start the control loops after everything is up, that is create 1766 * the thread that will make them run 1767 */ 1768static void start_control_loops(void) 1769{ 1770 init_completion(&ctrl_complete); 1771 1772 ctrl_task = kernel_thread(main_control_loop, NULL, SIGCHLD | CLONE_KERNEL); 1773} 1774 1775/* 1776 * Stop the control loops when tearing down 1777 */ 1778static void stop_control_loops(void) 1779{ 1780 if (ctrl_task != 0) 1781 wait_for_completion(&ctrl_complete); 1782} 1783 1784/* 1785 * Attach to the i2c FCU after detecting U3-1 bus 1786 */ 1787static int attach_fcu(void) 1788{ 1789 fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu"); 1790 if (fcu == NULL) 1791 return -ENODEV; 1792 1793 DBG("FCU attached\n"); 1794 1795 return 0; 1796} 1797 1798/* 1799 * Detach from the i2c FCU when tearing down 1800 */ 1801static void detach_fcu(void) 1802{ 1803 if (fcu) 1804 detach_i2c_chip(fcu); 1805 fcu = NULL; 1806} 1807 1808/* 1809 * Attach to the i2c controller. We probe the various chips based 1810 * on the device-tree nodes and build everything for the driver to 1811 * run, we then kick the driver monitoring thread 1812 */ 1813static int therm_pm72_attach(struct i2c_adapter *adapter) 1814{ 1815 down(&driver_lock); 1816 1817 /* Check state */ 1818 if (state == state_detached) 1819 state = state_attaching; 1820 if (state != state_attaching) { 1821 up(&driver_lock); 1822 return 0; 1823 } 1824 1825 /* Check if we are looking for one of these */ 1826 if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) { 1827 u3_0 = adapter; 1828 DBG("found U3-0\n"); 1829 if (k2 || !rackmac) 1830 if (create_control_loops()) 1831 u3_0 = NULL; 1832 } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) { 1833 u3_1 = adapter; 1834 DBG("found U3-1, attaching FCU\n"); 1835 if (attach_fcu()) 1836 u3_1 = NULL; 1837 } else if (k2 == NULL && !strcmp(adapter->name, "mac-io 0")) { 1838 k2 = adapter; 1839 DBG("Found K2\n"); 1840 if (u3_0 && rackmac) 1841 if (create_control_loops()) 1842 k2 = NULL; 1843 } 1844 /* We got all we need, start control loops */ 1845 if (u3_0 != NULL && u3_1 != NULL && (k2 || !rackmac)) { 1846 DBG("everything up, starting control loops\n"); 1847 state = state_attached; 1848 start_control_loops(); 1849 } 1850 up(&driver_lock); 1851 1852 return 0; 1853} 1854 1855/* 1856 * Called on every adapter when the driver or the i2c controller 1857 * is going away. 1858 */ 1859static int therm_pm72_detach(struct i2c_adapter *adapter) 1860{ 1861 down(&driver_lock); 1862 1863 if (state != state_detached) 1864 state = state_detaching; 1865 1866 /* Stop control loops if any */ 1867 DBG("stopping control loops\n"); 1868 up(&driver_lock); 1869 stop_control_loops(); 1870 down(&driver_lock); 1871 1872 if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) { 1873 DBG("lost U3-0, disposing control loops\n"); 1874 dispose_control_loops(); 1875 u3_0 = NULL; 1876 } 1877 1878 if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) { 1879 DBG("lost U3-1, detaching FCU\n"); 1880 detach_fcu(); 1881 u3_1 = NULL; 1882 } 1883 if (u3_0 == NULL && u3_1 == NULL) 1884 state = state_detached; 1885 1886 up(&driver_lock); 1887 1888 return 0; 1889} 1890 1891static int fan_check_loc_match(const char *loc, int fan) 1892{ 1893 char tmp[64]; 1894 char *c, *e; 1895 1896 strlcpy(tmp, fcu_fans[fan].loc, 64); 1897 1898 c = tmp; 1899 for (;;) { 1900 e = strchr(c, ','); 1901 if (e) 1902 *e = 0; 1903 if (strcmp(loc, c) == 0) 1904 return 1; 1905 if (e == NULL) 1906 break; 1907 c = e + 1; 1908 } 1909 return 0; 1910} 1911 1912static void fcu_lookup_fans(struct device_node *fcu_node) 1913{ 1914 struct device_node *np = NULL; 1915 int i; 1916 1917 /* The table is filled by default with values that are suitable 1918 * for the old machines without device-tree informations. We scan 1919 * the device-tree and override those values with whatever is 1920 * there 1921 */ 1922 1923 DBG("Looking up FCU controls in device-tree...\n"); 1924 1925 while ((np = of_get_next_child(fcu_node, np)) != NULL) { 1926 int type = -1; 1927 char *loc; 1928 u32 *reg; 1929 1930 DBG(" control: %s, type: %s\n", np->name, np->type); 1931 1932 /* Detect control type */ 1933 if (!strcmp(np->type, "fan-rpm-control") || 1934 !strcmp(np->type, "fan-rpm")) 1935 type = FCU_FAN_RPM; 1936 if (!strcmp(np->type, "fan-pwm-control") || 1937 !strcmp(np->type, "fan-pwm")) 1938 type = FCU_FAN_PWM; 1939 /* Only care about fans for now */ 1940 if (type == -1) 1941 continue; 1942 1943 /* Lookup for a matching location */ 1944 loc = (char *)get_property(np, "location", NULL); 1945 reg = (u32 *)get_property(np, "reg", NULL); 1946 if (loc == NULL || reg == NULL) 1947 continue; 1948 DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg); 1949 1950 for (i = 0; i < FCU_FAN_COUNT; i++) { 1951 int fan_id; 1952 1953 if (!fan_check_loc_match(loc, i)) 1954 continue; 1955 DBG(" location match, index: %d\n", i); 1956 fcu_fans[i].id = FCU_FAN_ABSENT_ID; 1957 if (type != fcu_fans[i].type) { 1958 printk(KERN_WARNING "therm_pm72: Fan type mismatch " 1959 "in device-tree for %s\n", np->full_name); 1960 break; 1961 } 1962 if (type == FCU_FAN_RPM) 1963 fan_id = ((*reg) - 0x10) / 2; 1964 else 1965 fan_id = ((*reg) - 0x30) / 2; 1966 if (fan_id > 7) { 1967 printk(KERN_WARNING "therm_pm72: Can't parse " 1968 "fan ID in device-tree for %s\n", np->full_name); 1969 break; 1970 } 1971 DBG(" fan id -> %d, type -> %d\n", fan_id, type); 1972 fcu_fans[i].id = fan_id; 1973 } 1974 } 1975 1976 /* Now dump the array */ 1977 printk(KERN_INFO "Detected fan controls:\n"); 1978 for (i = 0; i < FCU_FAN_COUNT; i++) { 1979 if (fcu_fans[i].id == FCU_FAN_ABSENT_ID) 1980 continue; 1981 printk(KERN_INFO " %d: %s fan, id %d, location: %s\n", i, 1982 fcu_fans[i].type == FCU_FAN_RPM ? "RPM" : "PWM", 1983 fcu_fans[i].id, fcu_fans[i].loc); 1984 } 1985} 1986 1987static int fcu_of_probe(struct of_device* dev, const struct of_device_id *match) 1988{ 1989 state = state_detached; 1990 1991 /* Lookup the fans in the device tree */ 1992 fcu_lookup_fans(dev->node); 1993 1994 /* Add the driver */ 1995 return i2c_add_driver(&therm_pm72_driver); 1996} 1997 1998static int fcu_of_remove(struct of_device* dev) 1999{ 2000 i2c_del_driver(&therm_pm72_driver); 2001 2002 return 0; 2003} 2004 2005static struct of_device_id fcu_match[] = 2006{ 2007 { 2008 .type = "fcu", 2009 }, 2010 {}, 2011}; 2012 2013static struct of_platform_driver fcu_of_platform_driver = 2014{ 2015 .name = "temperature", 2016 .match_table = fcu_match, 2017 .probe = fcu_of_probe, 2018 .remove = fcu_of_remove 2019}; 2020 2021/* 2022 * Check machine type, attach to i2c controller 2023 */ 2024static int __init therm_pm72_init(void) 2025{ 2026 struct device_node *np; 2027 2028 rackmac = machine_is_compatible("RackMac3,1"); 2029 2030 if (!machine_is_compatible("PowerMac7,2") && 2031 !machine_is_compatible("PowerMac7,3") && 2032 !rackmac) 2033 return -ENODEV; 2034 2035 printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION); 2036 2037 np = of_find_node_by_type(NULL, "fcu"); 2038 if (np == NULL) { 2039 /* Some machines have strangely broken device-tree */ 2040 np = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/fan@15e"); 2041 if (np == NULL) { 2042 printk(KERN_ERR "Can't find FCU in device-tree !\n"); 2043 return -ENODEV; 2044 } 2045 } 2046 of_dev = of_platform_device_create(np, "temperature", NULL); 2047 if (of_dev == NULL) { 2048 printk(KERN_ERR "Can't register FCU platform device !\n"); 2049 return -ENODEV; 2050 } 2051 2052 of_register_driver(&fcu_of_platform_driver); 2053 2054 return 0; 2055} 2056 2057static void __exit therm_pm72_exit(void) 2058{ 2059 of_unregister_driver(&fcu_of_platform_driver); 2060 2061 if (of_dev) 2062 of_device_unregister(of_dev); 2063} 2064 2065module_init(therm_pm72_init); 2066module_exit(therm_pm72_exit); 2067 2068MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>"); 2069MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control"); 2070MODULE_LICENSE("GPL"); 2071