| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. | 
 | 3 |  * | 
 | 4 |  * Copyright (C) 2001 David S. Miller (davem@redhat.com) | 
 | 5 |  */ | 
 | 6 |  | 
| David S. Miller | 4b50242 | 2005-07-24 19:35:08 -0700 | [diff] [blame] | 7 | #define __KERNEL_SYSCALLS__ | 
 | 8 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/kernel.h> | 
 | 10 | #include <linux/sched.h> | 
 | 11 | #include <linux/slab.h> | 
 | 12 | #include <linux/delay.h> | 
 | 13 | #include <asm/oplib.h> | 
 | 14 | #include <asm/ebus.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | static int errno; | 
 | 16 | #include <asm/unistd.h> | 
 | 17 |  | 
 | 18 | #include "bbc_i2c.h" | 
 | 19 | #include "max1617.h" | 
 | 20 |  | 
 | 21 | #undef ENVCTRL_TRACE | 
 | 22 |  | 
 | 23 | /* WARNING: Making changes to this driver is very dangerous. | 
 | 24 |  *          If you misprogram the sensor chips they can | 
 | 25 |  *          cut the power on you instantly. | 
 | 26 |  */ | 
 | 27 |  | 
 | 28 | /* Two temperature sensors exist in the SunBLADE-1000 enclosure. | 
 | 29 |  * Both are implemented using max1617 i2c devices.  Each max1617 | 
 | 30 |  * monitors 2 temperatures, one for one of the cpu dies and the other | 
 | 31 |  * for the ambient temperature. | 
 | 32 |  * | 
 | 33 |  * The max1617 is capable of being programmed with power-off | 
 | 34 |  * temperature values, one low limit and one high limit.  These | 
 | 35 |  * can be controlled independently for the cpu or ambient temperature. | 
 | 36 |  * If a limit is violated, the power is simply shut off.  The frequency | 
 | 37 |  * with which the max1617 does temperature sampling can be controlled | 
 | 38 |  * as well. | 
 | 39 |  * | 
 | 40 |  * Three fans exist inside the machine, all three are controlled with | 
 | 41 |  * an i2c digital to analog converter.  There is a fan directed at the | 
 | 42 |  * two processor slots, another for the rest of the enclosure, and the | 
 | 43 |  * third is for the power supply.  The first two fans may be speed | 
 | 44 |  * controlled by changing the voltage fed to them.  The third fan may | 
 | 45 |  * only be completely off or on.  The third fan is meant to only be | 
 | 46 |  * disabled/enabled when entering/exiting the lowest power-saving | 
 | 47 |  * mode of the machine. | 
 | 48 |  * | 
 | 49 |  * An environmental control kernel thread periodically monitors all | 
 | 50 |  * temperature sensors.  Based upon the samples it will adjust the | 
 | 51 |  * fan speeds to try and keep the system within a certain temperature | 
 | 52 |  * range (the goal being to make the fans as quiet as possible without | 
 | 53 |  * allowing the system to get too hot). | 
 | 54 |  * | 
 | 55 |  * If the temperature begins to rise/fall outside of the acceptable | 
 | 56 |  * operating range, a periodic warning will be sent to the kernel log. | 
 | 57 |  * The fans will be put on full blast to attempt to deal with this | 
 | 58 |  * situation.  After exceeding the acceptable operating range by a | 
 | 59 |  * certain threshold, the kernel thread will shut down the system. | 
 | 60 |  * Here, the thread is attempting to shut the machine down cleanly | 
 | 61 |  * before the hardware based power-off event is triggered. | 
 | 62 |  */ | 
 | 63 |  | 
 | 64 | /* These settings are in Celsius.  We use these defaults only | 
 | 65 |  * if we cannot interrogate the cpu-fru SEEPROM. | 
 | 66 |  */ | 
 | 67 | struct temp_limits { | 
 | 68 | 	s8 high_pwroff, high_shutdown, high_warn; | 
 | 69 | 	s8 low_warn, low_shutdown, low_pwroff; | 
 | 70 | }; | 
 | 71 |  | 
 | 72 | static struct temp_limits cpu_temp_limits[2] = { | 
 | 73 | 	{ 100, 85, 80, 5, -5, -10 }, | 
 | 74 | 	{ 100, 85, 80, 5, -5, -10 }, | 
 | 75 | }; | 
 | 76 |  | 
 | 77 | static struct temp_limits amb_temp_limits[2] = { | 
 | 78 | 	{ 65, 55, 40, 5, -5, -10 }, | 
 | 79 | 	{ 65, 55, 40, 5, -5, -10 }, | 
 | 80 | }; | 
 | 81 |  | 
 | 82 | enum fan_action { FAN_SLOWER, FAN_SAME, FAN_FASTER, FAN_FULLBLAST, FAN_STATE_MAX }; | 
 | 83 |  | 
 | 84 | struct bbc_cpu_temperature { | 
 | 85 | 	struct bbc_cpu_temperature	*next; | 
 | 86 |  | 
 | 87 | 	struct bbc_i2c_client		*client; | 
 | 88 | 	int				index; | 
 | 89 |  | 
 | 90 | 	/* Current readings, and history. */ | 
 | 91 | 	s8				curr_cpu_temp; | 
 | 92 | 	s8				curr_amb_temp; | 
 | 93 | 	s8				prev_cpu_temp; | 
 | 94 | 	s8				prev_amb_temp; | 
 | 95 | 	s8				avg_cpu_temp; | 
 | 96 | 	s8				avg_amb_temp; | 
 | 97 |  | 
 | 98 | 	int				sample_tick; | 
 | 99 |  | 
 | 100 | 	enum fan_action			fan_todo[2]; | 
 | 101 | #define FAN_AMBIENT	0 | 
 | 102 | #define FAN_CPU		1 | 
 | 103 | }; | 
 | 104 |  | 
 | 105 | struct bbc_cpu_temperature *all_bbc_temps; | 
 | 106 |  | 
 | 107 | struct bbc_fan_control { | 
 | 108 | 	struct bbc_fan_control 	*next; | 
 | 109 |  | 
 | 110 | 	struct bbc_i2c_client 	*client; | 
 | 111 | 	int 			index; | 
 | 112 |  | 
 | 113 | 	int			psupply_fan_on; | 
 | 114 | 	int			cpu_fan_speed; | 
 | 115 | 	int			system_fan_speed; | 
 | 116 | }; | 
 | 117 |  | 
 | 118 | struct bbc_fan_control *all_bbc_fans; | 
 | 119 |  | 
 | 120 | #define CPU_FAN_REG	0xf0 | 
 | 121 | #define SYS_FAN_REG	0xf2 | 
 | 122 | #define PSUPPLY_FAN_REG	0xf4 | 
 | 123 |  | 
 | 124 | #define FAN_SPEED_MIN	0x0c | 
 | 125 | #define FAN_SPEED_MAX	0x3f | 
 | 126 |  | 
 | 127 | #define PSUPPLY_FAN_ON	0x1f | 
 | 128 | #define PSUPPLY_FAN_OFF	0x00 | 
 | 129 |  | 
 | 130 | static void set_fan_speeds(struct bbc_fan_control *fp) | 
 | 131 | { | 
 | 132 | 	/* Put temperatures into range so we don't mis-program | 
 | 133 | 	 * the hardware. | 
 | 134 | 	 */ | 
 | 135 | 	if (fp->cpu_fan_speed < FAN_SPEED_MIN) | 
 | 136 | 		fp->cpu_fan_speed = FAN_SPEED_MIN; | 
 | 137 | 	if (fp->cpu_fan_speed > FAN_SPEED_MAX) | 
 | 138 | 		fp->cpu_fan_speed = FAN_SPEED_MAX; | 
 | 139 | 	if (fp->system_fan_speed < FAN_SPEED_MIN) | 
 | 140 | 		fp->system_fan_speed = FAN_SPEED_MIN; | 
 | 141 | 	if (fp->system_fan_speed > FAN_SPEED_MAX) | 
 | 142 | 		fp->system_fan_speed = FAN_SPEED_MAX; | 
 | 143 | #ifdef ENVCTRL_TRACE | 
 | 144 | 	printk("fan%d: Changed fan speed to cpu(%02x) sys(%02x)\n", | 
 | 145 | 	       fp->index, | 
 | 146 | 	       fp->cpu_fan_speed, fp->system_fan_speed); | 
 | 147 | #endif | 
 | 148 |  | 
 | 149 | 	bbc_i2c_writeb(fp->client, fp->cpu_fan_speed, CPU_FAN_REG); | 
 | 150 | 	bbc_i2c_writeb(fp->client, fp->system_fan_speed, SYS_FAN_REG); | 
 | 151 | 	bbc_i2c_writeb(fp->client, | 
 | 152 | 		       (fp->psupply_fan_on ? | 
 | 153 | 			PSUPPLY_FAN_ON : PSUPPLY_FAN_OFF), | 
 | 154 | 		       PSUPPLY_FAN_REG); | 
 | 155 | } | 
 | 156 |  | 
 | 157 | static void get_current_temps(struct bbc_cpu_temperature *tp) | 
 | 158 | { | 
 | 159 | 	tp->prev_amb_temp = tp->curr_amb_temp; | 
 | 160 | 	bbc_i2c_readb(tp->client, | 
 | 161 | 		      (unsigned char *) &tp->curr_amb_temp, | 
 | 162 | 		      MAX1617_AMB_TEMP); | 
 | 163 | 	tp->prev_cpu_temp = tp->curr_cpu_temp; | 
 | 164 | 	bbc_i2c_readb(tp->client, | 
 | 165 | 		      (unsigned char *) &tp->curr_cpu_temp, | 
 | 166 | 		      MAX1617_CPU_TEMP); | 
 | 167 | #ifdef ENVCTRL_TRACE | 
 | 168 | 	printk("temp%d: cpu(%d C) amb(%d C)\n", | 
 | 169 | 	       tp->index, | 
 | 170 | 	       (int) tp->curr_cpu_temp, (int) tp->curr_amb_temp); | 
 | 171 | #endif | 
 | 172 | } | 
 | 173 |  | 
 | 174 |  | 
 | 175 | static void do_envctrl_shutdown(struct bbc_cpu_temperature *tp) | 
 | 176 | { | 
 | 177 | 	static int shutting_down = 0; | 
 | 178 | 	static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL }; | 
 | 179 | 	char *argv[] = { "/sbin/shutdown", "-h", "now", NULL }; | 
 | 180 | 	char *type = "???"; | 
 | 181 | 	s8 val = -1; | 
 | 182 |  | 
 | 183 | 	if (shutting_down != 0) | 
 | 184 | 		return; | 
 | 185 |  | 
 | 186 | 	if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown || | 
 | 187 | 	    tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) { | 
 | 188 | 		type = "ambient"; | 
 | 189 | 		val = tp->curr_amb_temp; | 
 | 190 | 	} else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown || | 
 | 191 | 		   tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) { | 
 | 192 | 		type = "CPU"; | 
 | 193 | 		val = tp->curr_cpu_temp; | 
 | 194 | 	} | 
 | 195 |  | 
 | 196 | 	printk(KERN_CRIT "temp%d: Outside of safe %s " | 
 | 197 | 	       "operating temperature, %d C.\n", | 
 | 198 | 	       tp->index, type, val); | 
 | 199 |  | 
 | 200 | 	printk(KERN_CRIT "kenvctrld: Shutting down the system now.\n"); | 
 | 201 |  | 
 | 202 | 	shutting_down = 1; | 
 | 203 | 	if (execve("/sbin/shutdown", argv, envp) < 0) | 
 | 204 | 		printk(KERN_CRIT "envctrl: shutdown execution failed\n"); | 
 | 205 | } | 
 | 206 |  | 
 | 207 | #define WARN_INTERVAL	(30 * HZ) | 
 | 208 |  | 
 | 209 | static void analyze_ambient_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick) | 
 | 210 | { | 
 | 211 | 	int ret = 0; | 
 | 212 |  | 
 | 213 | 	if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) { | 
 | 214 | 		if (tp->curr_amb_temp >= | 
 | 215 | 		    amb_temp_limits[tp->index].high_warn) { | 
 | 216 | 			printk(KERN_WARNING "temp%d: " | 
 | 217 | 			       "Above safe ambient operating temperature, %d C.\n", | 
 | 218 | 			       tp->index, (int) tp->curr_amb_temp); | 
 | 219 | 			ret = 1; | 
 | 220 | 		} else if (tp->curr_amb_temp < | 
 | 221 | 			   amb_temp_limits[tp->index].low_warn) { | 
 | 222 | 			printk(KERN_WARNING "temp%d: " | 
 | 223 | 			       "Below safe ambient operating temperature, %d C.\n", | 
 | 224 | 			       tp->index, (int) tp->curr_amb_temp); | 
 | 225 | 			ret = 1; | 
 | 226 | 		} | 
 | 227 | 		if (ret) | 
 | 228 | 			*last_warn = jiffies; | 
 | 229 | 	} else if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_warn || | 
 | 230 | 		   tp->curr_amb_temp < amb_temp_limits[tp->index].low_warn) | 
 | 231 | 		ret = 1; | 
 | 232 |  | 
 | 233 | 	/* Now check the shutdown limits. */ | 
 | 234 | 	if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown || | 
 | 235 | 	    tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) { | 
 | 236 | 		do_envctrl_shutdown(tp); | 
 | 237 | 		ret = 1; | 
 | 238 | 	} | 
 | 239 |  | 
 | 240 | 	if (ret) { | 
 | 241 | 		tp->fan_todo[FAN_AMBIENT] = FAN_FULLBLAST; | 
 | 242 | 	} else if ((tick & (8 - 1)) == 0) { | 
 | 243 | 		s8 amb_goal_hi = amb_temp_limits[tp->index].high_warn - 10; | 
 | 244 | 		s8 amb_goal_lo; | 
 | 245 |  | 
 | 246 | 		amb_goal_lo = amb_goal_hi - 3; | 
 | 247 |  | 
 | 248 | 		/* We do not try to avoid 'too cold' events.  Basically we | 
 | 249 | 		 * only try to deal with over-heating and fan noise reduction. | 
 | 250 | 		 */ | 
 | 251 | 		if (tp->avg_amb_temp < amb_goal_hi) { | 
 | 252 | 			if (tp->avg_amb_temp >= amb_goal_lo) | 
 | 253 | 				tp->fan_todo[FAN_AMBIENT] = FAN_SAME; | 
 | 254 | 			else | 
 | 255 | 				tp->fan_todo[FAN_AMBIENT] = FAN_SLOWER; | 
 | 256 | 		} else { | 
 | 257 | 			tp->fan_todo[FAN_AMBIENT] = FAN_FASTER; | 
 | 258 | 		} | 
 | 259 | 	} else { | 
 | 260 | 		tp->fan_todo[FAN_AMBIENT] = FAN_SAME; | 
 | 261 | 	} | 
 | 262 | } | 
 | 263 |  | 
 | 264 | static void analyze_cpu_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick) | 
 | 265 | { | 
 | 266 | 	int ret = 0; | 
 | 267 |  | 
 | 268 | 	if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) { | 
 | 269 | 		if (tp->curr_cpu_temp >= | 
 | 270 | 		    cpu_temp_limits[tp->index].high_warn) { | 
 | 271 | 			printk(KERN_WARNING "temp%d: " | 
 | 272 | 			       "Above safe CPU operating temperature, %d C.\n", | 
 | 273 | 			       tp->index, (int) tp->curr_cpu_temp); | 
 | 274 | 			ret = 1; | 
 | 275 | 		} else if (tp->curr_cpu_temp < | 
 | 276 | 			   cpu_temp_limits[tp->index].low_warn) { | 
 | 277 | 			printk(KERN_WARNING "temp%d: " | 
 | 278 | 			       "Below safe CPU operating temperature, %d C.\n", | 
 | 279 | 			       tp->index, (int) tp->curr_cpu_temp); | 
 | 280 | 			ret = 1; | 
 | 281 | 		} | 
 | 282 | 		if (ret) | 
 | 283 | 			*last_warn = jiffies; | 
 | 284 | 	} else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_warn || | 
 | 285 | 		   tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_warn) | 
 | 286 | 		ret = 1; | 
 | 287 |  | 
 | 288 | 	/* Now check the shutdown limits. */ | 
 | 289 | 	if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown || | 
 | 290 | 	    tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) { | 
 | 291 | 		do_envctrl_shutdown(tp); | 
 | 292 | 		ret = 1; | 
 | 293 | 	} | 
 | 294 |  | 
 | 295 | 	if (ret) { | 
 | 296 | 		tp->fan_todo[FAN_CPU] = FAN_FULLBLAST; | 
 | 297 | 	} else if ((tick & (8 - 1)) == 0) { | 
 | 298 | 		s8 cpu_goal_hi = cpu_temp_limits[tp->index].high_warn - 10; | 
 | 299 | 		s8 cpu_goal_lo; | 
 | 300 |  | 
 | 301 | 		cpu_goal_lo = cpu_goal_hi - 3; | 
 | 302 |  | 
 | 303 | 		/* We do not try to avoid 'too cold' events.  Basically we | 
 | 304 | 		 * only try to deal with over-heating and fan noise reduction. | 
 | 305 | 		 */ | 
 | 306 | 		if (tp->avg_cpu_temp < cpu_goal_hi) { | 
 | 307 | 			if (tp->avg_cpu_temp >= cpu_goal_lo) | 
 | 308 | 				tp->fan_todo[FAN_CPU] = FAN_SAME; | 
 | 309 | 			else | 
 | 310 | 				tp->fan_todo[FAN_CPU] = FAN_SLOWER; | 
 | 311 | 		} else { | 
 | 312 | 			tp->fan_todo[FAN_CPU] = FAN_FASTER; | 
 | 313 | 		} | 
 | 314 | 	} else { | 
 | 315 | 		tp->fan_todo[FAN_CPU] = FAN_SAME; | 
 | 316 | 	} | 
 | 317 | } | 
 | 318 |  | 
 | 319 | static void analyze_temps(struct bbc_cpu_temperature *tp, unsigned long *last_warn) | 
 | 320 | { | 
 | 321 | 	tp->avg_amb_temp = (s8)((int)((int)tp->avg_amb_temp + (int)tp->curr_amb_temp) / 2); | 
 | 322 | 	tp->avg_cpu_temp = (s8)((int)((int)tp->avg_cpu_temp + (int)tp->curr_cpu_temp) / 2); | 
 | 323 |  | 
 | 324 | 	analyze_ambient_temp(tp, last_warn, tp->sample_tick); | 
 | 325 | 	analyze_cpu_temp(tp, last_warn, tp->sample_tick); | 
 | 326 |  | 
 | 327 | 	tp->sample_tick++; | 
 | 328 | } | 
 | 329 |  | 
 | 330 | static enum fan_action prioritize_fan_action(int which_fan) | 
 | 331 | { | 
 | 332 | 	struct bbc_cpu_temperature *tp; | 
 | 333 | 	enum fan_action decision = FAN_STATE_MAX; | 
 | 334 |  | 
 | 335 | 	/* Basically, prioritize what the temperature sensors | 
 | 336 | 	 * recommend we do, and perform that action on all the | 
 | 337 | 	 * fans. | 
 | 338 | 	 */ | 
 | 339 | 	for (tp = all_bbc_temps; tp; tp = tp->next) { | 
 | 340 | 		if (tp->fan_todo[which_fan] == FAN_FULLBLAST) { | 
 | 341 | 			decision = FAN_FULLBLAST; | 
 | 342 | 			break; | 
 | 343 | 		} | 
 | 344 | 		if (tp->fan_todo[which_fan] == FAN_SAME && | 
 | 345 | 		    decision != FAN_FASTER) | 
 | 346 | 			decision = FAN_SAME; | 
 | 347 | 		else if (tp->fan_todo[which_fan] == FAN_FASTER) | 
 | 348 | 			decision = FAN_FASTER; | 
 | 349 | 		else if (decision != FAN_FASTER && | 
 | 350 | 			 decision != FAN_SAME && | 
 | 351 | 			 tp->fan_todo[which_fan] == FAN_SLOWER) | 
 | 352 | 			decision = FAN_SLOWER; | 
 | 353 | 	} | 
 | 354 | 	if (decision == FAN_STATE_MAX) | 
 | 355 | 		decision = FAN_SAME; | 
 | 356 |  | 
 | 357 | 	return decision; | 
 | 358 | } | 
 | 359 |  | 
 | 360 | static int maybe_new_ambient_fan_speed(struct bbc_fan_control *fp) | 
 | 361 | { | 
 | 362 | 	enum fan_action decision = prioritize_fan_action(FAN_AMBIENT); | 
 | 363 | 	int ret; | 
 | 364 |  | 
 | 365 | 	if (decision == FAN_SAME) | 
 | 366 | 		return 0; | 
 | 367 |  | 
 | 368 | 	ret = 1; | 
 | 369 | 	if (decision == FAN_FULLBLAST) { | 
 | 370 | 		if (fp->system_fan_speed >= FAN_SPEED_MAX) | 
 | 371 | 			ret = 0; | 
 | 372 | 		else | 
 | 373 | 			fp->system_fan_speed = FAN_SPEED_MAX; | 
 | 374 | 	} else { | 
 | 375 | 		if (decision == FAN_FASTER) { | 
 | 376 | 			if (fp->system_fan_speed >= FAN_SPEED_MAX) | 
 | 377 | 				ret = 0; | 
 | 378 | 			else | 
 | 379 | 				fp->system_fan_speed += 2; | 
 | 380 | 		} else { | 
 | 381 | 			int orig_speed = fp->system_fan_speed; | 
 | 382 |  | 
 | 383 | 			if (orig_speed <= FAN_SPEED_MIN || | 
 | 384 | 			    orig_speed <= (fp->cpu_fan_speed - 3)) | 
 | 385 | 				ret = 0; | 
 | 386 | 			else | 
 | 387 | 				fp->system_fan_speed -= 1; | 
 | 388 | 		} | 
 | 389 | 	} | 
 | 390 |  | 
 | 391 | 	return ret; | 
 | 392 | } | 
 | 393 |  | 
 | 394 | static int maybe_new_cpu_fan_speed(struct bbc_fan_control *fp) | 
 | 395 | { | 
 | 396 | 	enum fan_action decision = prioritize_fan_action(FAN_CPU); | 
 | 397 | 	int ret; | 
 | 398 |  | 
 | 399 | 	if (decision == FAN_SAME) | 
 | 400 | 		return 0; | 
 | 401 |  | 
 | 402 | 	ret = 1; | 
 | 403 | 	if (decision == FAN_FULLBLAST) { | 
 | 404 | 		if (fp->cpu_fan_speed >= FAN_SPEED_MAX) | 
 | 405 | 			ret = 0; | 
 | 406 | 		else | 
 | 407 | 			fp->cpu_fan_speed = FAN_SPEED_MAX; | 
 | 408 | 	} else { | 
 | 409 | 		if (decision == FAN_FASTER) { | 
 | 410 | 			if (fp->cpu_fan_speed >= FAN_SPEED_MAX) | 
 | 411 | 				ret = 0; | 
 | 412 | 			else { | 
 | 413 | 				fp->cpu_fan_speed += 2; | 
 | 414 | 				if (fp->system_fan_speed < | 
 | 415 | 				    (fp->cpu_fan_speed - 3)) | 
 | 416 | 					fp->system_fan_speed = | 
 | 417 | 						fp->cpu_fan_speed - 3; | 
 | 418 | 			} | 
 | 419 | 		} else { | 
 | 420 | 			if (fp->cpu_fan_speed <= FAN_SPEED_MIN) | 
 | 421 | 				ret = 0; | 
 | 422 | 			else | 
 | 423 | 				fp->cpu_fan_speed -= 1; | 
 | 424 | 		} | 
 | 425 | 	} | 
 | 426 |  | 
 | 427 | 	return ret; | 
 | 428 | } | 
 | 429 |  | 
 | 430 | static void maybe_new_fan_speeds(struct bbc_fan_control *fp) | 
 | 431 | { | 
 | 432 | 	int new; | 
 | 433 |  | 
 | 434 | 	new  = maybe_new_ambient_fan_speed(fp); | 
 | 435 | 	new |= maybe_new_cpu_fan_speed(fp); | 
 | 436 |  | 
 | 437 | 	if (new) | 
 | 438 | 		set_fan_speeds(fp); | 
 | 439 | } | 
 | 440 |  | 
 | 441 | static void fans_full_blast(void) | 
 | 442 | { | 
 | 443 | 	struct bbc_fan_control *fp; | 
 | 444 |  | 
 | 445 | 	/* Since we will not be monitoring things anymore, put | 
 | 446 | 	 * the fans on full blast. | 
 | 447 | 	 */ | 
 | 448 | 	for (fp = all_bbc_fans; fp; fp = fp->next) { | 
 | 449 | 		fp->cpu_fan_speed = FAN_SPEED_MAX; | 
 | 450 | 		fp->system_fan_speed = FAN_SPEED_MAX; | 
 | 451 | 		fp->psupply_fan_on = 1; | 
 | 452 | 		set_fan_speeds(fp); | 
 | 453 | 	} | 
 | 454 | } | 
 | 455 |  | 
 | 456 | #define POLL_INTERVAL	(5 * 1000) | 
 | 457 | static unsigned long last_warning_jiffies; | 
 | 458 | static struct task_struct *kenvctrld_task; | 
 | 459 |  | 
 | 460 | static int kenvctrld(void *__unused) | 
 | 461 | { | 
 | 462 | 	daemonize("kenvctrld"); | 
 | 463 | 	allow_signal(SIGKILL); | 
 | 464 | 	kenvctrld_task = current; | 
 | 465 |  | 
 | 466 | 	printk(KERN_INFO "bbc_envctrl: kenvctrld starting...\n"); | 
 | 467 | 	last_warning_jiffies = jiffies - WARN_INTERVAL; | 
 | 468 | 	for (;;) { | 
 | 469 | 		struct bbc_cpu_temperature *tp; | 
 | 470 | 		struct bbc_fan_control *fp; | 
 | 471 |  | 
 | 472 | 		msleep_interruptible(POLL_INTERVAL); | 
 | 473 | 		if (signal_pending(current)) | 
 | 474 | 			break; | 
 | 475 |  | 
 | 476 | 		for (tp = all_bbc_temps; tp; tp = tp->next) { | 
 | 477 | 			get_current_temps(tp); | 
 | 478 | 			analyze_temps(tp, &last_warning_jiffies); | 
 | 479 | 		} | 
 | 480 | 		for (fp = all_bbc_fans; fp; fp = fp->next) | 
 | 481 | 			maybe_new_fan_speeds(fp); | 
 | 482 | 	} | 
 | 483 | 	printk(KERN_INFO "bbc_envctrl: kenvctrld exiting...\n"); | 
 | 484 |  | 
 | 485 | 	fans_full_blast(); | 
 | 486 |  | 
 | 487 | 	return 0; | 
 | 488 | } | 
 | 489 |  | 
 | 490 | static void attach_one_temp(struct linux_ebus_child *echild, int temp_idx) | 
 | 491 | { | 
 | 492 | 	struct bbc_cpu_temperature *tp = kmalloc(sizeof(*tp), GFP_KERNEL); | 
 | 493 |  | 
 | 494 | 	if (!tp) | 
 | 495 | 		return; | 
 | 496 | 	memset(tp, 0, sizeof(*tp)); | 
 | 497 | 	tp->client = bbc_i2c_attach(echild); | 
 | 498 | 	if (!tp->client) { | 
 | 499 | 		kfree(tp); | 
 | 500 | 		return; | 
 | 501 | 	} | 
 | 502 |  | 
 | 503 | 	tp->index = temp_idx; | 
 | 504 | 	{ | 
 | 505 | 		struct bbc_cpu_temperature **tpp = &all_bbc_temps; | 
 | 506 | 		while (*tpp) | 
 | 507 | 			tpp = &((*tpp)->next); | 
 | 508 | 		tp->next = NULL; | 
 | 509 | 		*tpp = tp; | 
 | 510 | 	} | 
 | 511 |  | 
 | 512 | 	/* Tell it to convert once every 5 seconds, clear all cfg | 
 | 513 | 	 * bits. | 
 | 514 | 	 */ | 
 | 515 | 	bbc_i2c_writeb(tp->client, 0x00, MAX1617_WR_CFG_BYTE); | 
 | 516 | 	bbc_i2c_writeb(tp->client, 0x02, MAX1617_WR_CVRATE_BYTE); | 
 | 517 |  | 
 | 518 | 	/* Program the hard temperature limits into the chip. */ | 
 | 519 | 	bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].high_pwroff, | 
 | 520 | 		       MAX1617_WR_AMB_HIGHLIM); | 
 | 521 | 	bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].low_pwroff, | 
 | 522 | 		       MAX1617_WR_AMB_LOWLIM); | 
 | 523 | 	bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].high_pwroff, | 
 | 524 | 		       MAX1617_WR_CPU_HIGHLIM); | 
 | 525 | 	bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].low_pwroff, | 
 | 526 | 		       MAX1617_WR_CPU_LOWLIM); | 
 | 527 |  | 
 | 528 | 	get_current_temps(tp); | 
 | 529 | 	tp->prev_cpu_temp = tp->avg_cpu_temp = tp->curr_cpu_temp; | 
 | 530 | 	tp->prev_amb_temp = tp->avg_amb_temp = tp->curr_amb_temp; | 
 | 531 |  | 
 | 532 | 	tp->fan_todo[FAN_AMBIENT] = FAN_SAME; | 
 | 533 | 	tp->fan_todo[FAN_CPU] = FAN_SAME; | 
 | 534 | } | 
 | 535 |  | 
 | 536 | static void attach_one_fan(struct linux_ebus_child *echild, int fan_idx) | 
 | 537 | { | 
 | 538 | 	struct bbc_fan_control *fp = kmalloc(sizeof(*fp), GFP_KERNEL); | 
 | 539 |  | 
 | 540 | 	if (!fp) | 
 | 541 | 		return; | 
 | 542 | 	memset(fp, 0, sizeof(*fp)); | 
 | 543 | 	fp->client = bbc_i2c_attach(echild); | 
 | 544 | 	if (!fp->client) { | 
 | 545 | 		kfree(fp); | 
 | 546 | 		return; | 
 | 547 | 	} | 
 | 548 |  | 
 | 549 | 	fp->index = fan_idx; | 
 | 550 |  | 
 | 551 | 	{ | 
 | 552 | 		struct bbc_fan_control **fpp = &all_bbc_fans; | 
 | 553 | 		while (*fpp) | 
 | 554 | 			fpp = &((*fpp)->next); | 
 | 555 | 		fp->next = NULL; | 
 | 556 | 		*fpp = fp; | 
 | 557 | 	} | 
 | 558 |  | 
 | 559 | 	/* The i2c device controlling the fans is write-only. | 
 | 560 | 	 * So the only way to keep track of the current power | 
 | 561 | 	 * level fed to the fans is via software.  Choose half | 
 | 562 | 	 * power for cpu/system and 'on' fo the powersupply fan | 
 | 563 | 	 * and set it now. | 
 | 564 | 	 */ | 
 | 565 | 	fp->psupply_fan_on = 1; | 
 | 566 | 	fp->cpu_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2; | 
 | 567 | 	fp->cpu_fan_speed += FAN_SPEED_MIN; | 
 | 568 | 	fp->system_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2; | 
 | 569 | 	fp->system_fan_speed += FAN_SPEED_MIN; | 
 | 570 |  | 
 | 571 | 	set_fan_speeds(fp); | 
 | 572 | } | 
 | 573 |  | 
 | 574 | int bbc_envctrl_init(void) | 
 | 575 | { | 
 | 576 | 	struct linux_ebus_child *echild; | 
 | 577 | 	int temp_index = 0; | 
 | 578 | 	int fan_index = 0; | 
 | 579 | 	int devidx = 0; | 
 | 580 | 	int err = 0; | 
 | 581 |  | 
 | 582 | 	while ((echild = bbc_i2c_getdev(devidx++)) != NULL) { | 
 | 583 | 		if (!strcmp(echild->prom_name, "temperature")) | 
 | 584 | 			attach_one_temp(echild, temp_index++); | 
 | 585 | 		if (!strcmp(echild->prom_name, "fan-control")) | 
 | 586 | 			attach_one_fan(echild, fan_index++); | 
 | 587 | 	} | 
 | 588 | 	if (temp_index != 0 && fan_index != 0) | 
 | 589 | 		err = kernel_thread(kenvctrld, NULL, CLONE_FS | CLONE_FILES); | 
 | 590 | 	return err; | 
 | 591 | } | 
 | 592 |  | 
 | 593 | static void destroy_one_temp(struct bbc_cpu_temperature *tp) | 
 | 594 | { | 
 | 595 | 	bbc_i2c_detach(tp->client); | 
 | 596 | 	kfree(tp); | 
 | 597 | } | 
 | 598 |  | 
 | 599 | static void destroy_one_fan(struct bbc_fan_control *fp) | 
 | 600 | { | 
 | 601 | 	bbc_i2c_detach(fp->client); | 
 | 602 | 	kfree(fp); | 
 | 603 | } | 
 | 604 |  | 
 | 605 | void bbc_envctrl_cleanup(void) | 
 | 606 | { | 
 | 607 | 	struct bbc_cpu_temperature *tp; | 
 | 608 | 	struct bbc_fan_control *fp; | 
 | 609 |  | 
 | 610 | 	if (kenvctrld_task != NULL) { | 
 | 611 | 		force_sig(SIGKILL, kenvctrld_task); | 
 | 612 | 		for (;;) { | 
 | 613 | 			struct task_struct *p; | 
 | 614 | 			int found = 0; | 
 | 615 |  | 
 | 616 | 			read_lock(&tasklist_lock); | 
 | 617 | 			for_each_process(p) { | 
 | 618 | 				if (p == kenvctrld_task) { | 
 | 619 | 					found = 1; | 
 | 620 | 					break; | 
 | 621 | 				} | 
 | 622 | 			} | 
 | 623 | 			read_unlock(&tasklist_lock); | 
 | 624 | 			if (!found) | 
 | 625 | 				break; | 
 | 626 | 			msleep(1000); | 
 | 627 | 		} | 
 | 628 | 		kenvctrld_task = NULL; | 
 | 629 | 	} | 
 | 630 |  | 
 | 631 | 	tp = all_bbc_temps; | 
 | 632 | 	while (tp != NULL) { | 
 | 633 | 		struct bbc_cpu_temperature *next = tp->next; | 
 | 634 | 		destroy_one_temp(tp); | 
 | 635 | 		tp = next; | 
 | 636 | 	} | 
 | 637 | 	all_bbc_temps = NULL; | 
 | 638 |  | 
 | 639 | 	fp = all_bbc_fans; | 
 | 640 | 	while (fp != NULL) { | 
 | 641 | 		struct bbc_fan_control *next = fp->next; | 
 | 642 | 		destroy_one_fan(fp); | 
 | 643 | 		fp = next; | 
 | 644 | 	} | 
 | 645 | 	all_bbc_fans = NULL; | 
 | 646 | } |