1 /* bbc_envctrl.c: UltraSPARC-III environment control driver.
3 * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net)
6 #include <linux/kthread.h>
7 #include <linux/delay.h>
8 #include <linux/kmod.h>
9 #include <linux/reboot.h>
11 #include <linux/of_device.h>
12 #include <asm/oplib.h>
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
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.
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 static LIST_HEAD(all_temps);
79 static LIST_HEAD(all_fans);
81 #define CPU_FAN_REG 0xf0
82 #define SYS_FAN_REG 0xf2
83 #define PSUPPLY_FAN_REG 0xf4
85 #define FAN_SPEED_MIN 0x0c
86 #define FAN_SPEED_MAX 0x3f
88 #define PSUPPLY_FAN_ON 0x1f
89 #define PSUPPLY_FAN_OFF 0x00
91 static void set_fan_speeds(struct bbc_fan_control *fp)
93 /* Put temperatures into range so we don't mis-program
96 if (fp->cpu_fan_speed < FAN_SPEED_MIN)
97 fp->cpu_fan_speed = FAN_SPEED_MIN;
98 if (fp->cpu_fan_speed > FAN_SPEED_MAX)
99 fp->cpu_fan_speed = FAN_SPEED_MAX;
100 if (fp->system_fan_speed < FAN_SPEED_MIN)
101 fp->system_fan_speed = FAN_SPEED_MIN;
102 if (fp->system_fan_speed > FAN_SPEED_MAX)
103 fp->system_fan_speed = FAN_SPEED_MAX;
105 printk("fan%d: Changed fan speed to cpu(%02x) sys(%02x)\n",
107 fp->cpu_fan_speed, fp->system_fan_speed);
110 bbc_i2c_writeb(fp->client, fp->cpu_fan_speed, CPU_FAN_REG);
111 bbc_i2c_writeb(fp->client, fp->system_fan_speed, SYS_FAN_REG);
112 bbc_i2c_writeb(fp->client,
113 (fp->psupply_fan_on ?
114 PSUPPLY_FAN_ON : PSUPPLY_FAN_OFF),
118 static void get_current_temps(struct bbc_cpu_temperature *tp)
120 tp->prev_amb_temp = tp->curr_amb_temp;
121 bbc_i2c_readb(tp->client,
122 (unsigned char *) &tp->curr_amb_temp,
124 tp->prev_cpu_temp = tp->curr_cpu_temp;
125 bbc_i2c_readb(tp->client,
126 (unsigned char *) &tp->curr_cpu_temp,
129 printk("temp%d: cpu(%d C) amb(%d C)\n",
131 (int) tp->curr_cpu_temp, (int) tp->curr_amb_temp);
136 static void do_envctrl_shutdown(struct bbc_cpu_temperature *tp)
138 static int shutting_down = 0;
142 if (shutting_down != 0)
145 if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown ||
146 tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) {
148 val = tp->curr_amb_temp;
149 } else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown ||
150 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) {
152 val = tp->curr_cpu_temp;
155 printk(KERN_CRIT "temp%d: Outside of safe %s "
156 "operating temperature, %d C.\n",
157 tp->index, type, val);
159 printk(KERN_CRIT "kenvctrld: Shutting down the system now.\n");
162 if (orderly_poweroff(true) < 0)
163 printk(KERN_CRIT "envctrl: shutdown execution failed\n");
166 #define WARN_INTERVAL (30 * HZ)
168 static void analyze_ambient_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick)
172 if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) {
173 if (tp->curr_amb_temp >=
174 amb_temp_limits[tp->index].high_warn) {
175 printk(KERN_WARNING "temp%d: "
176 "Above safe ambient operating temperature, %d C.\n",
177 tp->index, (int) tp->curr_amb_temp);
179 } else if (tp->curr_amb_temp <
180 amb_temp_limits[tp->index].low_warn) {
181 printk(KERN_WARNING "temp%d: "
182 "Below safe ambient operating temperature, %d C.\n",
183 tp->index, (int) tp->curr_amb_temp);
187 *last_warn = jiffies;
188 } else if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_warn ||
189 tp->curr_amb_temp < amb_temp_limits[tp->index].low_warn)
192 /* Now check the shutdown limits. */
193 if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown ||
194 tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) {
195 do_envctrl_shutdown(tp);
200 tp->fan_todo[FAN_AMBIENT] = FAN_FULLBLAST;
201 } else if ((tick & (8 - 1)) == 0) {
202 s8 amb_goal_hi = amb_temp_limits[tp->index].high_warn - 10;
205 amb_goal_lo = amb_goal_hi - 3;
207 /* We do not try to avoid 'too cold' events. Basically we
208 * only try to deal with over-heating and fan noise reduction.
210 if (tp->avg_amb_temp < amb_goal_hi) {
211 if (tp->avg_amb_temp >= amb_goal_lo)
212 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
214 tp->fan_todo[FAN_AMBIENT] = FAN_SLOWER;
216 tp->fan_todo[FAN_AMBIENT] = FAN_FASTER;
219 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
223 static void analyze_cpu_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick)
227 if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) {
228 if (tp->curr_cpu_temp >=
229 cpu_temp_limits[tp->index].high_warn) {
230 printk(KERN_WARNING "temp%d: "
231 "Above safe CPU operating temperature, %d C.\n",
232 tp->index, (int) tp->curr_cpu_temp);
234 } else if (tp->curr_cpu_temp <
235 cpu_temp_limits[tp->index].low_warn) {
236 printk(KERN_WARNING "temp%d: "
237 "Below safe CPU operating temperature, %d C.\n",
238 tp->index, (int) tp->curr_cpu_temp);
242 *last_warn = jiffies;
243 } else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_warn ||
244 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_warn)
247 /* Now check the shutdown limits. */
248 if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown ||
249 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) {
250 do_envctrl_shutdown(tp);
255 tp->fan_todo[FAN_CPU] = FAN_FULLBLAST;
256 } else if ((tick & (8 - 1)) == 0) {
257 s8 cpu_goal_hi = cpu_temp_limits[tp->index].high_warn - 10;
260 cpu_goal_lo = cpu_goal_hi - 3;
262 /* We do not try to avoid 'too cold' events. Basically we
263 * only try to deal with over-heating and fan noise reduction.
265 if (tp->avg_cpu_temp < cpu_goal_hi) {
266 if (tp->avg_cpu_temp >= cpu_goal_lo)
267 tp->fan_todo[FAN_CPU] = FAN_SAME;
269 tp->fan_todo[FAN_CPU] = FAN_SLOWER;
271 tp->fan_todo[FAN_CPU] = FAN_FASTER;
274 tp->fan_todo[FAN_CPU] = FAN_SAME;
278 static void analyze_temps(struct bbc_cpu_temperature *tp, unsigned long *last_warn)
280 tp->avg_amb_temp = (s8)((int)((int)tp->avg_amb_temp + (int)tp->curr_amb_temp) / 2);
281 tp->avg_cpu_temp = (s8)((int)((int)tp->avg_cpu_temp + (int)tp->curr_cpu_temp) / 2);
283 analyze_ambient_temp(tp, last_warn, tp->sample_tick);
284 analyze_cpu_temp(tp, last_warn, tp->sample_tick);
289 static enum fan_action prioritize_fan_action(int which_fan)
291 struct bbc_cpu_temperature *tp;
292 enum fan_action decision = FAN_STATE_MAX;
294 /* Basically, prioritize what the temperature sensors
295 * recommend we do, and perform that action on all the
298 list_for_each_entry(tp, &all_temps, glob_list) {
299 if (tp->fan_todo[which_fan] == FAN_FULLBLAST) {
300 decision = FAN_FULLBLAST;
303 if (tp->fan_todo[which_fan] == FAN_SAME &&
304 decision != FAN_FASTER)
306 else if (tp->fan_todo[which_fan] == FAN_FASTER)
307 decision = FAN_FASTER;
308 else if (decision != FAN_FASTER &&
309 decision != FAN_SAME &&
310 tp->fan_todo[which_fan] == FAN_SLOWER)
311 decision = FAN_SLOWER;
313 if (decision == FAN_STATE_MAX)
319 static int maybe_new_ambient_fan_speed(struct bbc_fan_control *fp)
321 enum fan_action decision = prioritize_fan_action(FAN_AMBIENT);
324 if (decision == FAN_SAME)
328 if (decision == FAN_FULLBLAST) {
329 if (fp->system_fan_speed >= FAN_SPEED_MAX)
332 fp->system_fan_speed = FAN_SPEED_MAX;
334 if (decision == FAN_FASTER) {
335 if (fp->system_fan_speed >= FAN_SPEED_MAX)
338 fp->system_fan_speed += 2;
340 int orig_speed = fp->system_fan_speed;
342 if (orig_speed <= FAN_SPEED_MIN ||
343 orig_speed <= (fp->cpu_fan_speed - 3))
346 fp->system_fan_speed -= 1;
353 static int maybe_new_cpu_fan_speed(struct bbc_fan_control *fp)
355 enum fan_action decision = prioritize_fan_action(FAN_CPU);
358 if (decision == FAN_SAME)
362 if (decision == FAN_FULLBLAST) {
363 if (fp->cpu_fan_speed >= FAN_SPEED_MAX)
366 fp->cpu_fan_speed = FAN_SPEED_MAX;
368 if (decision == FAN_FASTER) {
369 if (fp->cpu_fan_speed >= FAN_SPEED_MAX)
372 fp->cpu_fan_speed += 2;
373 if (fp->system_fan_speed <
374 (fp->cpu_fan_speed - 3))
375 fp->system_fan_speed =
376 fp->cpu_fan_speed - 3;
379 if (fp->cpu_fan_speed <= FAN_SPEED_MIN)
382 fp->cpu_fan_speed -= 1;
389 static void maybe_new_fan_speeds(struct bbc_fan_control *fp)
393 new = maybe_new_ambient_fan_speed(fp);
394 new |= maybe_new_cpu_fan_speed(fp);
400 static void fans_full_blast(void)
402 struct bbc_fan_control *fp;
404 /* Since we will not be monitoring things anymore, put
405 * the fans on full blast.
407 list_for_each_entry(fp, &all_fans, glob_list) {
408 fp->cpu_fan_speed = FAN_SPEED_MAX;
409 fp->system_fan_speed = FAN_SPEED_MAX;
410 fp->psupply_fan_on = 1;
415 #define POLL_INTERVAL (5 * 1000)
416 static unsigned long last_warning_jiffies;
417 static struct task_struct *kenvctrld_task;
419 static int kenvctrld(void *__unused)
421 printk(KERN_INFO "bbc_envctrl: kenvctrld starting...\n");
422 last_warning_jiffies = jiffies - WARN_INTERVAL;
424 struct bbc_cpu_temperature *tp;
425 struct bbc_fan_control *fp;
427 msleep_interruptible(POLL_INTERVAL);
428 if (kthread_should_stop())
431 list_for_each_entry(tp, &all_temps, glob_list) {
432 get_current_temps(tp);
433 analyze_temps(tp, &last_warning_jiffies);
435 list_for_each_entry(fp, &all_fans, glob_list)
436 maybe_new_fan_speeds(fp);
438 printk(KERN_INFO "bbc_envctrl: kenvctrld exiting...\n");
445 static void attach_one_temp(struct bbc_i2c_bus *bp, struct of_device *op,
448 struct bbc_cpu_temperature *tp;
450 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
454 tp->client = bbc_i2c_attach(bp, op);
461 tp->index = temp_idx;
463 list_add(&tp->glob_list, &all_temps);
464 list_add(&tp->bp_list, &bp->temps);
466 /* Tell it to convert once every 5 seconds, clear all cfg
469 bbc_i2c_writeb(tp->client, 0x00, MAX1617_WR_CFG_BYTE);
470 bbc_i2c_writeb(tp->client, 0x02, MAX1617_WR_CVRATE_BYTE);
472 /* Program the hard temperature limits into the chip. */
473 bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].high_pwroff,
474 MAX1617_WR_AMB_HIGHLIM);
475 bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].low_pwroff,
476 MAX1617_WR_AMB_LOWLIM);
477 bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].high_pwroff,
478 MAX1617_WR_CPU_HIGHLIM);
479 bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].low_pwroff,
480 MAX1617_WR_CPU_LOWLIM);
482 get_current_temps(tp);
483 tp->prev_cpu_temp = tp->avg_cpu_temp = tp->curr_cpu_temp;
484 tp->prev_amb_temp = tp->avg_amb_temp = tp->curr_amb_temp;
486 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
487 tp->fan_todo[FAN_CPU] = FAN_SAME;
490 static void attach_one_fan(struct bbc_i2c_bus *bp, struct of_device *op,
493 struct bbc_fan_control *fp;
495 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
499 fp->client = bbc_i2c_attach(bp, op);
507 list_add(&fp->glob_list, &all_fans);
508 list_add(&fp->bp_list, &bp->fans);
510 /* The i2c device controlling the fans is write-only.
511 * So the only way to keep track of the current power
512 * level fed to the fans is via software. Choose half
513 * power for cpu/system and 'on' fo the powersupply fan
516 fp->psupply_fan_on = 1;
517 fp->cpu_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2;
518 fp->cpu_fan_speed += FAN_SPEED_MIN;
519 fp->system_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2;
520 fp->system_fan_speed += FAN_SPEED_MIN;
525 int bbc_envctrl_init(struct bbc_i2c_bus *bp)
527 struct of_device *op;
532 while ((op = bbc_i2c_getdev(bp, devidx++)) != NULL) {
533 if (!strcmp(op->node->name, "temperature"))
534 attach_one_temp(bp, op, temp_index++);
535 if (!strcmp(op->node->name, "fan-control"))
536 attach_one_fan(bp, op, fan_index++);
538 if (temp_index != 0 && fan_index != 0) {
539 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
540 if (IS_ERR(kenvctrld_task))
541 return PTR_ERR(kenvctrld_task);
547 static void destroy_one_temp(struct bbc_cpu_temperature *tp)
549 bbc_i2c_detach(tp->client);
553 static void destroy_one_fan(struct bbc_fan_control *fp)
555 bbc_i2c_detach(fp->client);
559 void bbc_envctrl_cleanup(struct bbc_i2c_bus *bp)
561 struct bbc_cpu_temperature *tp, *tpos;
562 struct bbc_fan_control *fp, *fpos;
564 kthread_stop(kenvctrld_task);
566 list_for_each_entry_safe(tp, tpos, &bp->temps, bp_list) {
567 list_del(&tp->bp_list);
568 list_del(&tp->glob_list);
569 destroy_one_temp(tp);
572 list_for_each_entry_safe(fp, fpos, &bp->fans, bp_list) {
573 list_del(&fp->bp_list);
574 list_del(&fp->glob_list);