bbc_envctrl.c 16 KB

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