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