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