iwlwifi: introduce host commands callbacks
[linux/fpc-iii.git] / drivers / sbus / char / bbc_envctrl.c
blob0bde26989a23a2ebaf89251414fe58cf384ea84a
1 /* $Id: bbc_envctrl.c,v 1.4 2001/04/06 16:48:08 davem Exp $
2 * bbc_envctrl.c: UltraSPARC-III environment control driver.
4 * Copyright (C) 2001 David S. Miller (davem@redhat.com)
5 */
7 #include <linux/kthread.h>
8 #include <linux/delay.h>
9 #include <linux/kmod.h>
10 #include <linux/reboot.h>
11 #include <asm/oplib.h>
12 #include <asm/ebus.h>
14 #include "bbc_i2c.h"
15 #include "max1617.h"
17 #undef ENVCTRL_TRACE
19 /* WARNING: Making changes to this driver is very dangerous.
20 * If you misprogram the sensor chips they can
21 * cut the power on you instantly.
24 /* Two temperature sensors exist in the SunBLADE-1000 enclosure.
25 * Both are implemented using max1617 i2c devices. Each max1617
26 * monitors 2 temperatures, one for one of the cpu dies and the other
27 * for the ambient temperature.
29 * The max1617 is capable of being programmed with power-off
30 * temperature values, one low limit and one high limit. These
31 * can be controlled independently for the cpu or ambient temperature.
32 * If a limit is violated, the power is simply shut off. The frequency
33 * with which the max1617 does temperature sampling can be controlled
34 * as well.
36 * Three fans exist inside the machine, all three are controlled with
37 * an i2c digital to analog converter. There is a fan directed at the
38 * two processor slots, another for the rest of the enclosure, and the
39 * third is for the power supply. The first two fans may be speed
40 * controlled by changing the voltage fed to them. The third fan may
41 * only be completely off or on. The third fan is meant to only be
42 * disabled/enabled when entering/exiting the lowest power-saving
43 * mode of the machine.
45 * An environmental control kernel thread periodically monitors all
46 * temperature sensors. Based upon the samples it will adjust the
47 * fan speeds to try and keep the system within a certain temperature
48 * range (the goal being to make the fans as quiet as possible without
49 * allowing the system to get too hot).
51 * If the temperature begins to rise/fall outside of the acceptable
52 * operating range, a periodic warning will be sent to the kernel log.
53 * The fans will be put on full blast to attempt to deal with this
54 * situation. After exceeding the acceptable operating range by a
55 * certain threshold, the kernel thread will shut down the system.
56 * Here, the thread is attempting to shut the machine down cleanly
57 * before the hardware based power-off event is triggered.
60 /* These settings are in Celsius. We use these defaults only
61 * if we cannot interrogate the cpu-fru SEEPROM.
63 struct temp_limits {
64 s8 high_pwroff, high_shutdown, high_warn;
65 s8 low_warn, low_shutdown, low_pwroff;
68 static struct temp_limits cpu_temp_limits[2] = {
69 { 100, 85, 80, 5, -5, -10 },
70 { 100, 85, 80, 5, -5, -10 },
73 static struct temp_limits amb_temp_limits[2] = {
74 { 65, 55, 40, 5, -5, -10 },
75 { 65, 55, 40, 5, -5, -10 },
78 enum fan_action { FAN_SLOWER, FAN_SAME, FAN_FASTER, FAN_FULLBLAST, FAN_STATE_MAX };
80 struct bbc_cpu_temperature {
81 struct bbc_cpu_temperature *next;
83 struct bbc_i2c_client *client;
84 int index;
86 /* Current readings, and history. */
87 s8 curr_cpu_temp;
88 s8 curr_amb_temp;
89 s8 prev_cpu_temp;
90 s8 prev_amb_temp;
91 s8 avg_cpu_temp;
92 s8 avg_amb_temp;
94 int sample_tick;
96 enum fan_action fan_todo[2];
97 #define FAN_AMBIENT 0
98 #define FAN_CPU 1
101 struct bbc_cpu_temperature *all_bbc_temps;
103 struct bbc_fan_control {
104 struct bbc_fan_control *next;
106 struct bbc_i2c_client *client;
107 int index;
109 int psupply_fan_on;
110 int cpu_fan_speed;
111 int system_fan_speed;
114 struct bbc_fan_control *all_bbc_fans;
116 #define CPU_FAN_REG 0xf0
117 #define SYS_FAN_REG 0xf2
118 #define PSUPPLY_FAN_REG 0xf4
120 #define FAN_SPEED_MIN 0x0c
121 #define FAN_SPEED_MAX 0x3f
123 #define PSUPPLY_FAN_ON 0x1f
124 #define PSUPPLY_FAN_OFF 0x00
126 static void set_fan_speeds(struct bbc_fan_control *fp)
128 /* Put temperatures into range so we don't mis-program
129 * the hardware.
131 if (fp->cpu_fan_speed < FAN_SPEED_MIN)
132 fp->cpu_fan_speed = FAN_SPEED_MIN;
133 if (fp->cpu_fan_speed > FAN_SPEED_MAX)
134 fp->cpu_fan_speed = FAN_SPEED_MAX;
135 if (fp->system_fan_speed < FAN_SPEED_MIN)
136 fp->system_fan_speed = FAN_SPEED_MIN;
137 if (fp->system_fan_speed > FAN_SPEED_MAX)
138 fp->system_fan_speed = FAN_SPEED_MAX;
139 #ifdef ENVCTRL_TRACE
140 printk("fan%d: Changed fan speed to cpu(%02x) sys(%02x)\n",
141 fp->index,
142 fp->cpu_fan_speed, fp->system_fan_speed);
143 #endif
145 bbc_i2c_writeb(fp->client, fp->cpu_fan_speed, CPU_FAN_REG);
146 bbc_i2c_writeb(fp->client, fp->system_fan_speed, SYS_FAN_REG);
147 bbc_i2c_writeb(fp->client,
148 (fp->psupply_fan_on ?
149 PSUPPLY_FAN_ON : PSUPPLY_FAN_OFF),
150 PSUPPLY_FAN_REG);
153 static void get_current_temps(struct bbc_cpu_temperature *tp)
155 tp->prev_amb_temp = tp->curr_amb_temp;
156 bbc_i2c_readb(tp->client,
157 (unsigned char *) &tp->curr_amb_temp,
158 MAX1617_AMB_TEMP);
159 tp->prev_cpu_temp = tp->curr_cpu_temp;
160 bbc_i2c_readb(tp->client,
161 (unsigned char *) &tp->curr_cpu_temp,
162 MAX1617_CPU_TEMP);
163 #ifdef ENVCTRL_TRACE
164 printk("temp%d: cpu(%d C) amb(%d C)\n",
165 tp->index,
166 (int) tp->curr_cpu_temp, (int) tp->curr_amb_temp);
167 #endif
171 static void do_envctrl_shutdown(struct bbc_cpu_temperature *tp)
173 static int shutting_down = 0;
174 char *type = "???";
175 s8 val = -1;
177 if (shutting_down != 0)
178 return;
180 if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown ||
181 tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) {
182 type = "ambient";
183 val = tp->curr_amb_temp;
184 } else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown ||
185 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) {
186 type = "CPU";
187 val = tp->curr_cpu_temp;
190 printk(KERN_CRIT "temp%d: Outside of safe %s "
191 "operating temperature, %d C.\n",
192 tp->index, type, val);
194 printk(KERN_CRIT "kenvctrld: Shutting down the system now.\n");
196 shutting_down = 1;
197 if (orderly_poweroff(true) < 0)
198 printk(KERN_CRIT "envctrl: shutdown execution failed\n");
201 #define WARN_INTERVAL (30 * HZ)
203 static void analyze_ambient_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick)
205 int ret = 0;
207 if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) {
208 if (tp->curr_amb_temp >=
209 amb_temp_limits[tp->index].high_warn) {
210 printk(KERN_WARNING "temp%d: "
211 "Above safe ambient operating temperature, %d C.\n",
212 tp->index, (int) tp->curr_amb_temp);
213 ret = 1;
214 } else if (tp->curr_amb_temp <
215 amb_temp_limits[tp->index].low_warn) {
216 printk(KERN_WARNING "temp%d: "
217 "Below safe ambient operating temperature, %d C.\n",
218 tp->index, (int) tp->curr_amb_temp);
219 ret = 1;
221 if (ret)
222 *last_warn = jiffies;
223 } else if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_warn ||
224 tp->curr_amb_temp < amb_temp_limits[tp->index].low_warn)
225 ret = 1;
227 /* Now check the shutdown limits. */
228 if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown ||
229 tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) {
230 do_envctrl_shutdown(tp);
231 ret = 1;
234 if (ret) {
235 tp->fan_todo[FAN_AMBIENT] = FAN_FULLBLAST;
236 } else if ((tick & (8 - 1)) == 0) {
237 s8 amb_goal_hi = amb_temp_limits[tp->index].high_warn - 10;
238 s8 amb_goal_lo;
240 amb_goal_lo = amb_goal_hi - 3;
242 /* We do not try to avoid 'too cold' events. Basically we
243 * only try to deal with over-heating and fan noise reduction.
245 if (tp->avg_amb_temp < amb_goal_hi) {
246 if (tp->avg_amb_temp >= amb_goal_lo)
247 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
248 else
249 tp->fan_todo[FAN_AMBIENT] = FAN_SLOWER;
250 } else {
251 tp->fan_todo[FAN_AMBIENT] = FAN_FASTER;
253 } else {
254 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
258 static void analyze_cpu_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick)
260 int ret = 0;
262 if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) {
263 if (tp->curr_cpu_temp >=
264 cpu_temp_limits[tp->index].high_warn) {
265 printk(KERN_WARNING "temp%d: "
266 "Above safe CPU operating temperature, %d C.\n",
267 tp->index, (int) tp->curr_cpu_temp);
268 ret = 1;
269 } else if (tp->curr_cpu_temp <
270 cpu_temp_limits[tp->index].low_warn) {
271 printk(KERN_WARNING "temp%d: "
272 "Below safe CPU operating temperature, %d C.\n",
273 tp->index, (int) tp->curr_cpu_temp);
274 ret = 1;
276 if (ret)
277 *last_warn = jiffies;
278 } else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_warn ||
279 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_warn)
280 ret = 1;
282 /* Now check the shutdown limits. */
283 if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown ||
284 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) {
285 do_envctrl_shutdown(tp);
286 ret = 1;
289 if (ret) {
290 tp->fan_todo[FAN_CPU] = FAN_FULLBLAST;
291 } else if ((tick & (8 - 1)) == 0) {
292 s8 cpu_goal_hi = cpu_temp_limits[tp->index].high_warn - 10;
293 s8 cpu_goal_lo;
295 cpu_goal_lo = cpu_goal_hi - 3;
297 /* We do not try to avoid 'too cold' events. Basically we
298 * only try to deal with over-heating and fan noise reduction.
300 if (tp->avg_cpu_temp < cpu_goal_hi) {
301 if (tp->avg_cpu_temp >= cpu_goal_lo)
302 tp->fan_todo[FAN_CPU] = FAN_SAME;
303 else
304 tp->fan_todo[FAN_CPU] = FAN_SLOWER;
305 } else {
306 tp->fan_todo[FAN_CPU] = FAN_FASTER;
308 } else {
309 tp->fan_todo[FAN_CPU] = FAN_SAME;
313 static void analyze_temps(struct bbc_cpu_temperature *tp, unsigned long *last_warn)
315 tp->avg_amb_temp = (s8)((int)((int)tp->avg_amb_temp + (int)tp->curr_amb_temp) / 2);
316 tp->avg_cpu_temp = (s8)((int)((int)tp->avg_cpu_temp + (int)tp->curr_cpu_temp) / 2);
318 analyze_ambient_temp(tp, last_warn, tp->sample_tick);
319 analyze_cpu_temp(tp, last_warn, tp->sample_tick);
321 tp->sample_tick++;
324 static enum fan_action prioritize_fan_action(int which_fan)
326 struct bbc_cpu_temperature *tp;
327 enum fan_action decision = FAN_STATE_MAX;
329 /* Basically, prioritize what the temperature sensors
330 * recommend we do, and perform that action on all the
331 * fans.
333 for (tp = all_bbc_temps; tp; tp = tp->next) {
334 if (tp->fan_todo[which_fan] == FAN_FULLBLAST) {
335 decision = FAN_FULLBLAST;
336 break;
338 if (tp->fan_todo[which_fan] == FAN_SAME &&
339 decision != FAN_FASTER)
340 decision = FAN_SAME;
341 else if (tp->fan_todo[which_fan] == FAN_FASTER)
342 decision = FAN_FASTER;
343 else if (decision != FAN_FASTER &&
344 decision != FAN_SAME &&
345 tp->fan_todo[which_fan] == FAN_SLOWER)
346 decision = FAN_SLOWER;
348 if (decision == FAN_STATE_MAX)
349 decision = FAN_SAME;
351 return decision;
354 static int maybe_new_ambient_fan_speed(struct bbc_fan_control *fp)
356 enum fan_action decision = prioritize_fan_action(FAN_AMBIENT);
357 int ret;
359 if (decision == FAN_SAME)
360 return 0;
362 ret = 1;
363 if (decision == FAN_FULLBLAST) {
364 if (fp->system_fan_speed >= FAN_SPEED_MAX)
365 ret = 0;
366 else
367 fp->system_fan_speed = FAN_SPEED_MAX;
368 } else {
369 if (decision == FAN_FASTER) {
370 if (fp->system_fan_speed >= FAN_SPEED_MAX)
371 ret = 0;
372 else
373 fp->system_fan_speed += 2;
374 } else {
375 int orig_speed = fp->system_fan_speed;
377 if (orig_speed <= FAN_SPEED_MIN ||
378 orig_speed <= (fp->cpu_fan_speed - 3))
379 ret = 0;
380 else
381 fp->system_fan_speed -= 1;
385 return ret;
388 static int maybe_new_cpu_fan_speed(struct bbc_fan_control *fp)
390 enum fan_action decision = prioritize_fan_action(FAN_CPU);
391 int ret;
393 if (decision == FAN_SAME)
394 return 0;
396 ret = 1;
397 if (decision == FAN_FULLBLAST) {
398 if (fp->cpu_fan_speed >= FAN_SPEED_MAX)
399 ret = 0;
400 else
401 fp->cpu_fan_speed = FAN_SPEED_MAX;
402 } else {
403 if (decision == FAN_FASTER) {
404 if (fp->cpu_fan_speed >= FAN_SPEED_MAX)
405 ret = 0;
406 else {
407 fp->cpu_fan_speed += 2;
408 if (fp->system_fan_speed <
409 (fp->cpu_fan_speed - 3))
410 fp->system_fan_speed =
411 fp->cpu_fan_speed - 3;
413 } else {
414 if (fp->cpu_fan_speed <= FAN_SPEED_MIN)
415 ret = 0;
416 else
417 fp->cpu_fan_speed -= 1;
421 return ret;
424 static void maybe_new_fan_speeds(struct bbc_fan_control *fp)
426 int new;
428 new = maybe_new_ambient_fan_speed(fp);
429 new |= maybe_new_cpu_fan_speed(fp);
431 if (new)
432 set_fan_speeds(fp);
435 static void fans_full_blast(void)
437 struct bbc_fan_control *fp;
439 /* Since we will not be monitoring things anymore, put
440 * the fans on full blast.
442 for (fp = all_bbc_fans; fp; fp = fp->next) {
443 fp->cpu_fan_speed = FAN_SPEED_MAX;
444 fp->system_fan_speed = FAN_SPEED_MAX;
445 fp->psupply_fan_on = 1;
446 set_fan_speeds(fp);
450 #define POLL_INTERVAL (5 * 1000)
451 static unsigned long last_warning_jiffies;
452 static struct task_struct *kenvctrld_task;
454 static int kenvctrld(void *__unused)
456 printk(KERN_INFO "bbc_envctrl: kenvctrld starting...\n");
457 last_warning_jiffies = jiffies - WARN_INTERVAL;
458 for (;;) {
459 struct bbc_cpu_temperature *tp;
460 struct bbc_fan_control *fp;
462 msleep_interruptible(POLL_INTERVAL);
463 if (kthread_should_stop())
464 break;
466 for (tp = all_bbc_temps; tp; tp = tp->next) {
467 get_current_temps(tp);
468 analyze_temps(tp, &last_warning_jiffies);
470 for (fp = all_bbc_fans; fp; fp = fp->next)
471 maybe_new_fan_speeds(fp);
473 printk(KERN_INFO "bbc_envctrl: kenvctrld exiting...\n");
475 fans_full_blast();
477 return 0;
480 static void attach_one_temp(struct linux_ebus_child *echild, int temp_idx)
482 struct bbc_cpu_temperature *tp;
484 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
485 if (!tp)
486 return;
488 tp->client = bbc_i2c_attach(echild);
489 if (!tp->client) {
490 kfree(tp);
491 return;
494 tp->index = temp_idx;
496 struct bbc_cpu_temperature **tpp = &all_bbc_temps;
497 while (*tpp)
498 tpp = &((*tpp)->next);
499 tp->next = NULL;
500 *tpp = tp;
503 /* Tell it to convert once every 5 seconds, clear all cfg
504 * bits.
506 bbc_i2c_writeb(tp->client, 0x00, MAX1617_WR_CFG_BYTE);
507 bbc_i2c_writeb(tp->client, 0x02, MAX1617_WR_CVRATE_BYTE);
509 /* Program the hard temperature limits into the chip. */
510 bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].high_pwroff,
511 MAX1617_WR_AMB_HIGHLIM);
512 bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].low_pwroff,
513 MAX1617_WR_AMB_LOWLIM);
514 bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].high_pwroff,
515 MAX1617_WR_CPU_HIGHLIM);
516 bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].low_pwroff,
517 MAX1617_WR_CPU_LOWLIM);
519 get_current_temps(tp);
520 tp->prev_cpu_temp = tp->avg_cpu_temp = tp->curr_cpu_temp;
521 tp->prev_amb_temp = tp->avg_amb_temp = tp->curr_amb_temp;
523 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
524 tp->fan_todo[FAN_CPU] = FAN_SAME;
527 static void attach_one_fan(struct linux_ebus_child *echild, int fan_idx)
529 struct bbc_fan_control *fp;
531 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
532 if (!fp)
533 return;
535 fp->client = bbc_i2c_attach(echild);
536 if (!fp->client) {
537 kfree(fp);
538 return;
541 fp->index = fan_idx;
544 struct bbc_fan_control **fpp = &all_bbc_fans;
545 while (*fpp)
546 fpp = &((*fpp)->next);
547 fp->next = NULL;
548 *fpp = fp;
551 /* The i2c device controlling the fans is write-only.
552 * So the only way to keep track of the current power
553 * level fed to the fans is via software. Choose half
554 * power for cpu/system and 'on' fo the powersupply fan
555 * and set it now.
557 fp->psupply_fan_on = 1;
558 fp->cpu_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2;
559 fp->cpu_fan_speed += FAN_SPEED_MIN;
560 fp->system_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2;
561 fp->system_fan_speed += FAN_SPEED_MIN;
563 set_fan_speeds(fp);
566 int bbc_envctrl_init(void)
568 struct linux_ebus_child *echild;
569 int temp_index = 0;
570 int fan_index = 0;
571 int devidx = 0;
573 while ((echild = bbc_i2c_getdev(devidx++)) != NULL) {
574 if (!strcmp(echild->prom_node->name, "temperature"))
575 attach_one_temp(echild, temp_index++);
576 if (!strcmp(echild->prom_node->name, "fan-control"))
577 attach_one_fan(echild, fan_index++);
579 if (temp_index != 0 && fan_index != 0) {
580 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
581 if (IS_ERR(kenvctrld_task))
582 return PTR_ERR(kenvctrld_task);
585 return 0;
588 static void destroy_one_temp(struct bbc_cpu_temperature *tp)
590 bbc_i2c_detach(tp->client);
591 kfree(tp);
594 static void destroy_one_fan(struct bbc_fan_control *fp)
596 bbc_i2c_detach(fp->client);
597 kfree(fp);
600 void bbc_envctrl_cleanup(void)
602 struct bbc_cpu_temperature *tp;
603 struct bbc_fan_control *fp;
605 kthread_stop(kenvctrld_task);
607 tp = all_bbc_temps;
608 while (tp != NULL) {
609 struct bbc_cpu_temperature *next = tp->next;
610 destroy_one_temp(tp);
611 tp = next;
613 all_bbc_temps = NULL;
615 fp = all_bbc_fans;
616 while (fp != NULL) {
617 struct bbc_fan_control *next = fp->next;
618 destroy_one_fan(fp);
619 fp = next;
621 all_bbc_fans = NULL;