thermal.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. *
  25. * This driver fully implements the ACPI thermal policy as described in the
  26. * ACPI 2.0 Specification.
  27. *
  28. * TBD: 1. Implement passive cooling hysteresis.
  29. * 2. Enhance passive cooling (CPU) states/limit interface to support
  30. * concepts of 'multiple limiters', upper/lower limits, etc.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/types.h>
  37. #include <linux/proc_fs.h>
  38. #include <linux/sched.h>
  39. #include <linux/kmod.h>
  40. #include <linux/seq_file.h>
  41. #include <asm/uaccess.h>
  42. #include <acpi/acpi_bus.h>
  43. #include <acpi/acpi_drivers.h>
  44. #define ACPI_THERMAL_COMPONENT 0x04000000
  45. #define ACPI_THERMAL_CLASS "thermal_zone"
  46. #define ACPI_THERMAL_DRIVER_NAME "ACPI Thermal Zone Driver"
  47. #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
  48. #define ACPI_THERMAL_FILE_STATE "state"
  49. #define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
  50. #define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
  51. #define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
  52. #define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
  53. #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
  54. #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
  55. #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
  56. #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
  57. #define ACPI_THERMAL_NOTIFY_HOT 0xF1
  58. #define ACPI_THERMAL_MODE_ACTIVE 0x00
  59. #define ACPI_THERMAL_MODE_PASSIVE 0x01
  60. #define ACPI_THERMAL_MODE_CRITICAL 0xff
  61. #define ACPI_THERMAL_PATH_POWEROFF "/sbin/poweroff"
  62. #define ACPI_THERMAL_MAX_ACTIVE 10
  63. #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
  64. #define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
  65. #define CELSIUS_TO_KELVIN(t) ((t+273)*10)
  66. #define _COMPONENT ACPI_THERMAL_COMPONENT
  67. ACPI_MODULE_NAME("acpi_thermal")
  68. MODULE_AUTHOR("Paul Diefenbaugh");
  69. MODULE_DESCRIPTION(ACPI_THERMAL_DRIVER_NAME);
  70. MODULE_LICENSE("GPL");
  71. static int tzp;
  72. module_param(tzp, int, 0);
  73. MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
  74. static int acpi_thermal_add(struct acpi_device *device);
  75. static int acpi_thermal_remove(struct acpi_device *device, int type);
  76. static int acpi_thermal_resume(struct acpi_device *device, int state);
  77. static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
  78. static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
  79. static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
  80. static ssize_t acpi_thermal_write_trip_points(struct file *,
  81. const char __user *, size_t,
  82. loff_t *);
  83. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
  84. static ssize_t acpi_thermal_write_cooling_mode(struct file *,
  85. const char __user *, size_t,
  86. loff_t *);
  87. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
  88. static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
  89. size_t, loff_t *);
  90. static struct acpi_driver acpi_thermal_driver = {
  91. .name = ACPI_THERMAL_DRIVER_NAME,
  92. .class = ACPI_THERMAL_CLASS,
  93. .ids = ACPI_THERMAL_HID,
  94. .ops = {
  95. .add = acpi_thermal_add,
  96. .remove = acpi_thermal_remove,
  97. .resume = acpi_thermal_resume,
  98. },
  99. };
  100. struct acpi_thermal_state {
  101. u8 critical:1;
  102. u8 hot:1;
  103. u8 passive:1;
  104. u8 active:1;
  105. u8 reserved:4;
  106. int active_index;
  107. };
  108. struct acpi_thermal_state_flags {
  109. u8 valid:1;
  110. u8 enabled:1;
  111. u8 reserved:6;
  112. };
  113. struct acpi_thermal_critical {
  114. struct acpi_thermal_state_flags flags;
  115. unsigned long temperature;
  116. };
  117. struct acpi_thermal_hot {
  118. struct acpi_thermal_state_flags flags;
  119. unsigned long temperature;
  120. };
  121. struct acpi_thermal_passive {
  122. struct acpi_thermal_state_flags flags;
  123. unsigned long temperature;
  124. unsigned long tc1;
  125. unsigned long tc2;
  126. unsigned long tsp;
  127. struct acpi_handle_list devices;
  128. };
  129. struct acpi_thermal_active {
  130. struct acpi_thermal_state_flags flags;
  131. unsigned long temperature;
  132. struct acpi_handle_list devices;
  133. };
  134. struct acpi_thermal_trips {
  135. struct acpi_thermal_critical critical;
  136. struct acpi_thermal_hot hot;
  137. struct acpi_thermal_passive passive;
  138. struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
  139. };
  140. struct acpi_thermal_flags {
  141. u8 cooling_mode:1; /* _SCP */
  142. u8 devices:1; /* _TZD */
  143. u8 reserved:6;
  144. };
  145. struct acpi_thermal {
  146. acpi_handle handle;
  147. acpi_bus_id name;
  148. unsigned long temperature;
  149. unsigned long last_temperature;
  150. unsigned long polling_frequency;
  151. u8 cooling_mode;
  152. volatile u8 zombie;
  153. struct acpi_thermal_flags flags;
  154. struct acpi_thermal_state state;
  155. struct acpi_thermal_trips trips;
  156. struct acpi_handle_list devices;
  157. struct timer_list timer;
  158. };
  159. static struct file_operations acpi_thermal_state_fops = {
  160. .open = acpi_thermal_state_open_fs,
  161. .read = seq_read,
  162. .llseek = seq_lseek,
  163. .release = single_release,
  164. };
  165. static struct file_operations acpi_thermal_temp_fops = {
  166. .open = acpi_thermal_temp_open_fs,
  167. .read = seq_read,
  168. .llseek = seq_lseek,
  169. .release = single_release,
  170. };
  171. static struct file_operations acpi_thermal_trip_fops = {
  172. .open = acpi_thermal_trip_open_fs,
  173. .read = seq_read,
  174. .write = acpi_thermal_write_trip_points,
  175. .llseek = seq_lseek,
  176. .release = single_release,
  177. };
  178. static struct file_operations acpi_thermal_cooling_fops = {
  179. .open = acpi_thermal_cooling_open_fs,
  180. .read = seq_read,
  181. .write = acpi_thermal_write_cooling_mode,
  182. .llseek = seq_lseek,
  183. .release = single_release,
  184. };
  185. static struct file_operations acpi_thermal_polling_fops = {
  186. .open = acpi_thermal_polling_open_fs,
  187. .read = seq_read,
  188. .write = acpi_thermal_write_polling,
  189. .llseek = seq_lseek,
  190. .release = single_release,
  191. };
  192. /* --------------------------------------------------------------------------
  193. Thermal Zone Management
  194. -------------------------------------------------------------------------- */
  195. static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
  196. {
  197. acpi_status status = AE_OK;
  198. if (!tz)
  199. return -EINVAL;
  200. tz->last_temperature = tz->temperature;
  201. status =
  202. acpi_evaluate_integer(tz->handle, "_TMP", NULL, &tz->temperature);
  203. if (ACPI_FAILURE(status))
  204. return -ENODEV;
  205. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
  206. tz->temperature));
  207. return 0;
  208. }
  209. static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
  210. {
  211. acpi_status status = AE_OK;
  212. if (!tz)
  213. return -EINVAL;
  214. status =
  215. acpi_evaluate_integer(tz->handle, "_TZP", NULL,
  216. &tz->polling_frequency);
  217. if (ACPI_FAILURE(status))
  218. return -ENODEV;
  219. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
  220. tz->polling_frequency));
  221. return 0;
  222. }
  223. static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
  224. {
  225. if (!tz)
  226. return -EINVAL;
  227. tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
  228. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  229. "Polling frequency set to %lu seconds\n",
  230. tz->polling_frequency));
  231. return 0;
  232. }
  233. static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
  234. {
  235. acpi_status status = AE_OK;
  236. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  237. struct acpi_object_list arg_list = { 1, &arg0 };
  238. acpi_handle handle = NULL;
  239. if (!tz)
  240. return -EINVAL;
  241. status = acpi_get_handle(tz->handle, "_SCP", &handle);
  242. if (ACPI_FAILURE(status)) {
  243. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
  244. return -ENODEV;
  245. }
  246. arg0.integer.value = mode;
  247. status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
  248. if (ACPI_FAILURE(status))
  249. return -ENODEV;
  250. tz->cooling_mode = mode;
  251. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cooling mode [%s]\n",
  252. mode ? "passive" : "active"));
  253. return 0;
  254. }
  255. static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
  256. {
  257. acpi_status status = AE_OK;
  258. int i = 0;
  259. if (!tz)
  260. return -EINVAL;
  261. /* Critical Shutdown (required) */
  262. status = acpi_evaluate_integer(tz->handle, "_CRT", NULL,
  263. &tz->trips.critical.temperature);
  264. if (ACPI_FAILURE(status)) {
  265. tz->trips.critical.flags.valid = 0;
  266. ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
  267. return -ENODEV;
  268. } else {
  269. tz->trips.critical.flags.valid = 1;
  270. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  271. "Found critical threshold [%lu]\n",
  272. tz->trips.critical.temperature));
  273. }
  274. /* Critical Sleep (optional) */
  275. status =
  276. acpi_evaluate_integer(tz->handle, "_HOT", NULL,
  277. &tz->trips.hot.temperature);
  278. if (ACPI_FAILURE(status)) {
  279. tz->trips.hot.flags.valid = 0;
  280. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
  281. } else {
  282. tz->trips.hot.flags.valid = 1;
  283. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
  284. tz->trips.hot.temperature));
  285. }
  286. /* Passive: Processors (optional) */
  287. status =
  288. acpi_evaluate_integer(tz->handle, "_PSV", NULL,
  289. &tz->trips.passive.temperature);
  290. if (ACPI_FAILURE(status)) {
  291. tz->trips.passive.flags.valid = 0;
  292. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
  293. } else {
  294. tz->trips.passive.flags.valid = 1;
  295. status =
  296. acpi_evaluate_integer(tz->handle, "_TC1", NULL,
  297. &tz->trips.passive.tc1);
  298. if (ACPI_FAILURE(status))
  299. tz->trips.passive.flags.valid = 0;
  300. status =
  301. acpi_evaluate_integer(tz->handle, "_TC2", NULL,
  302. &tz->trips.passive.tc2);
  303. if (ACPI_FAILURE(status))
  304. tz->trips.passive.flags.valid = 0;
  305. status =
  306. acpi_evaluate_integer(tz->handle, "_TSP", NULL,
  307. &tz->trips.passive.tsp);
  308. if (ACPI_FAILURE(status))
  309. tz->trips.passive.flags.valid = 0;
  310. status =
  311. acpi_evaluate_reference(tz->handle, "_PSL", NULL,
  312. &tz->trips.passive.devices);
  313. if (ACPI_FAILURE(status))
  314. tz->trips.passive.flags.valid = 0;
  315. if (!tz->trips.passive.flags.valid)
  316. printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
  317. else
  318. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  319. "Found passive threshold [%lu]\n",
  320. tz->trips.passive.temperature));
  321. }
  322. /* Active: Fans, etc. (optional) */
  323. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  324. char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
  325. status =
  326. acpi_evaluate_integer(tz->handle, name, NULL,
  327. &tz->trips.active[i].temperature);
  328. if (ACPI_FAILURE(status))
  329. break;
  330. name[2] = 'L';
  331. status =
  332. acpi_evaluate_reference(tz->handle, name, NULL,
  333. &tz->trips.active[i].devices);
  334. if (ACPI_SUCCESS(status)) {
  335. tz->trips.active[i].flags.valid = 1;
  336. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  337. "Found active threshold [%d]:[%lu]\n",
  338. i, tz->trips.active[i].temperature));
  339. } else
  340. ACPI_EXCEPTION((AE_INFO, status,
  341. "Invalid active threshold [%d]", i));
  342. }
  343. return 0;
  344. }
  345. static int acpi_thermal_get_devices(struct acpi_thermal *tz)
  346. {
  347. acpi_status status = AE_OK;
  348. if (!tz)
  349. return -EINVAL;
  350. status =
  351. acpi_evaluate_reference(tz->handle, "_TZD", NULL, &tz->devices);
  352. if (ACPI_FAILURE(status))
  353. return -ENODEV;
  354. return 0;
  355. }
  356. static int acpi_thermal_call_usermode(char *path)
  357. {
  358. char *argv[2] = { NULL, NULL };
  359. char *envp[3] = { NULL, NULL, NULL };
  360. if (!path)
  361. return -EINVAL;
  362. argv[0] = path;
  363. /* minimal command environment */
  364. envp[0] = "HOME=/";
  365. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  366. call_usermodehelper(argv[0], argv, envp, 0);
  367. return 0;
  368. }
  369. static int acpi_thermal_critical(struct acpi_thermal *tz)
  370. {
  371. int result = 0;
  372. struct acpi_device *device = NULL;
  373. if (!tz || !tz->trips.critical.flags.valid)
  374. return -EINVAL;
  375. if (tz->temperature >= tz->trips.critical.temperature) {
  376. printk(KERN_WARNING PREFIX "Critical trip point\n");
  377. tz->trips.critical.flags.enabled = 1;
  378. } else if (tz->trips.critical.flags.enabled)
  379. tz->trips.critical.flags.enabled = 0;
  380. result = acpi_bus_get_device(tz->handle, &device);
  381. if (result)
  382. return result;
  383. printk(KERN_EMERG
  384. "Critical temperature reached (%ld C), shutting down.\n",
  385. KELVIN_TO_CELSIUS(tz->temperature));
  386. acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_CRITICAL,
  387. tz->trips.critical.flags.enabled);
  388. acpi_thermal_call_usermode(ACPI_THERMAL_PATH_POWEROFF);
  389. return 0;
  390. }
  391. static int acpi_thermal_hot(struct acpi_thermal *tz)
  392. {
  393. int result = 0;
  394. struct acpi_device *device = NULL;
  395. if (!tz || !tz->trips.hot.flags.valid)
  396. return -EINVAL;
  397. if (tz->temperature >= tz->trips.hot.temperature) {
  398. printk(KERN_WARNING PREFIX "Hot trip point\n");
  399. tz->trips.hot.flags.enabled = 1;
  400. } else if (tz->trips.hot.flags.enabled)
  401. tz->trips.hot.flags.enabled = 0;
  402. result = acpi_bus_get_device(tz->handle, &device);
  403. if (result)
  404. return result;
  405. acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_HOT,
  406. tz->trips.hot.flags.enabled);
  407. /* TBD: Call user-mode "sleep(S4)" function */
  408. return 0;
  409. }
  410. static void acpi_thermal_passive(struct acpi_thermal *tz)
  411. {
  412. int result = 1;
  413. struct acpi_thermal_passive *passive = NULL;
  414. int trend = 0;
  415. int i = 0;
  416. if (!tz || !tz->trips.passive.flags.valid)
  417. return;
  418. passive = &(tz->trips.passive);
  419. /*
  420. * Above Trip?
  421. * -----------
  422. * Calculate the thermal trend (using the passive cooling equation)
  423. * and modify the performance limit for all passive cooling devices
  424. * accordingly. Note that we assume symmetry.
  425. */
  426. if (tz->temperature >= passive->temperature) {
  427. trend =
  428. (passive->tc1 * (tz->temperature - tz->last_temperature)) +
  429. (passive->tc2 * (tz->temperature - passive->temperature));
  430. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  431. "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
  432. trend, passive->tc1, tz->temperature,
  433. tz->last_temperature, passive->tc2,
  434. tz->temperature, passive->temperature));
  435. passive->flags.enabled = 1;
  436. /* Heating up? */
  437. if (trend > 0)
  438. for (i = 0; i < passive->devices.count; i++)
  439. acpi_processor_set_thermal_limit(passive->
  440. devices.
  441. handles[i],
  442. ACPI_PROCESSOR_LIMIT_INCREMENT);
  443. /* Cooling off? */
  444. else if (trend < 0) {
  445. for (i = 0; i < passive->devices.count; i++)
  446. /*
  447. * assume that we are on highest
  448. * freq/lowest thrott and can leave
  449. * passive mode, even in error case
  450. */
  451. if (!acpi_processor_set_thermal_limit
  452. (passive->devices.handles[i],
  453. ACPI_PROCESSOR_LIMIT_DECREMENT))
  454. result = 0;
  455. /*
  456. * Leave cooling mode, even if the temp might
  457. * higher than trip point This is because some
  458. * machines might have long thermal polling
  459. * frequencies (tsp) defined. We will fall back
  460. * into passive mode in next cycle (probably quicker)
  461. */
  462. if (result) {
  463. passive->flags.enabled = 0;
  464. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  465. "Disabling passive cooling, still above threshold,"
  466. " but we are cooling down\n"));
  467. }
  468. }
  469. return;
  470. }
  471. /*
  472. * Below Trip?
  473. * -----------
  474. * Implement passive cooling hysteresis to slowly increase performance
  475. * and avoid thrashing around the passive trip point. Note that we
  476. * assume symmetry.
  477. */
  478. if (!passive->flags.enabled)
  479. return;
  480. for (i = 0; i < passive->devices.count; i++)
  481. if (!acpi_processor_set_thermal_limit
  482. (passive->devices.handles[i],
  483. ACPI_PROCESSOR_LIMIT_DECREMENT))
  484. result = 0;
  485. if (result) {
  486. passive->flags.enabled = 0;
  487. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  488. "Disabling passive cooling (zone is cool)\n"));
  489. }
  490. }
  491. static void acpi_thermal_active(struct acpi_thermal *tz)
  492. {
  493. int result = 0;
  494. struct acpi_thermal_active *active = NULL;
  495. int i = 0;
  496. int j = 0;
  497. unsigned long maxtemp = 0;
  498. if (!tz)
  499. return;
  500. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  501. active = &(tz->trips.active[i]);
  502. if (!active || !active->flags.valid)
  503. break;
  504. if (tz->temperature >= active->temperature) {
  505. /*
  506. * Above Threshold?
  507. * ----------------
  508. * If not already enabled, turn ON all cooling devices
  509. * associated with this active threshold.
  510. */
  511. if (active->temperature > maxtemp)
  512. tz->state.active_index = i;
  513. maxtemp = active->temperature;
  514. if (active->flags.enabled)
  515. continue;
  516. for (j = 0; j < active->devices.count; j++) {
  517. result =
  518. acpi_bus_set_power(active->devices.
  519. handles[j],
  520. ACPI_STATE_D0);
  521. if (result) {
  522. printk(KERN_WARNING PREFIX
  523. "Unable to turn cooling device [%p] 'on'\n",
  524. active->devices.
  525. handles[j]);
  526. continue;
  527. }
  528. active->flags.enabled = 1;
  529. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  530. "Cooling device [%p] now 'on'\n",
  531. active->devices.handles[j]));
  532. }
  533. continue;
  534. }
  535. if (!active->flags.enabled)
  536. continue;
  537. /*
  538. * Below Threshold?
  539. * ----------------
  540. * Turn OFF all cooling devices associated with this
  541. * threshold.
  542. */
  543. for (j = 0; j < active->devices.count; j++) {
  544. result = acpi_bus_set_power(active->devices.handles[j],
  545. ACPI_STATE_D3);
  546. if (result) {
  547. printk(KERN_WARNING PREFIX
  548. "Unable to turn cooling device [%p] 'off'\n",
  549. active->devices.handles[j]);
  550. continue;
  551. }
  552. active->flags.enabled = 0;
  553. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  554. "Cooling device [%p] now 'off'\n",
  555. active->devices.handles[j]));
  556. }
  557. }
  558. }
  559. static void acpi_thermal_check(void *context);
  560. static void acpi_thermal_run(unsigned long data)
  561. {
  562. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  563. if (!tz->zombie)
  564. acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
  565. }
  566. static void acpi_thermal_check(void *data)
  567. {
  568. int result = 0;
  569. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  570. unsigned long sleep_time = 0;
  571. int i = 0;
  572. struct acpi_thermal_state state;
  573. if (!tz) {
  574. printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
  575. return;
  576. }
  577. state = tz->state;
  578. result = acpi_thermal_get_temperature(tz);
  579. if (result)
  580. return;
  581. memset(&tz->state, 0, sizeof(tz->state));
  582. /*
  583. * Check Trip Points
  584. * -----------------
  585. * Compare the current temperature to the trip point values to see
  586. * if we've entered one of the thermal policy states. Note that
  587. * this function determines when a state is entered, but the
  588. * individual policy decides when it is exited (e.g. hysteresis).
  589. */
  590. if (tz->trips.critical.flags.valid)
  591. state.critical |=
  592. (tz->temperature >= tz->trips.critical.temperature);
  593. if (tz->trips.hot.flags.valid)
  594. state.hot |= (tz->temperature >= tz->trips.hot.temperature);
  595. if (tz->trips.passive.flags.valid)
  596. state.passive |=
  597. (tz->temperature >= tz->trips.passive.temperature);
  598. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  599. if (tz->trips.active[i].flags.valid)
  600. state.active |=
  601. (tz->temperature >=
  602. tz->trips.active[i].temperature);
  603. /*
  604. * Invoke Policy
  605. * -------------
  606. * Separated from the above check to allow individual policy to
  607. * determine when to exit a given state.
  608. */
  609. if (state.critical)
  610. acpi_thermal_critical(tz);
  611. if (state.hot)
  612. acpi_thermal_hot(tz);
  613. if (state.passive)
  614. acpi_thermal_passive(tz);
  615. if (state.active)
  616. acpi_thermal_active(tz);
  617. /*
  618. * Calculate State
  619. * ---------------
  620. * Again, separated from the above two to allow independent policy
  621. * decisions.
  622. */
  623. tz->state.critical = tz->trips.critical.flags.enabled;
  624. tz->state.hot = tz->trips.hot.flags.enabled;
  625. tz->state.passive = tz->trips.passive.flags.enabled;
  626. tz->state.active = 0;
  627. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  628. tz->state.active |= tz->trips.active[i].flags.enabled;
  629. /*
  630. * Calculate Sleep Time
  631. * --------------------
  632. * If we're in the passive state, use _TSP's value. Otherwise
  633. * use the default polling frequency (e.g. _TZP). If no polling
  634. * frequency is specified then we'll wait forever (at least until
  635. * a thermal event occurs). Note that _TSP and _TZD values are
  636. * given in 1/10th seconds (we must covert to milliseconds).
  637. */
  638. if (tz->state.passive)
  639. sleep_time = tz->trips.passive.tsp * 100;
  640. else if (tz->polling_frequency > 0)
  641. sleep_time = tz->polling_frequency * 100;
  642. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
  643. tz->name, tz->temperature, sleep_time));
  644. /*
  645. * Schedule Next Poll
  646. * ------------------
  647. */
  648. if (!sleep_time) {
  649. if (timer_pending(&(tz->timer)))
  650. del_timer(&(tz->timer));
  651. } else {
  652. if (timer_pending(&(tz->timer)))
  653. mod_timer(&(tz->timer), (HZ * sleep_time) / 1000);
  654. else {
  655. tz->timer.data = (unsigned long)tz;
  656. tz->timer.function = acpi_thermal_run;
  657. tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
  658. add_timer(&(tz->timer));
  659. }
  660. }
  661. return;
  662. }
  663. /* --------------------------------------------------------------------------
  664. FS Interface (/proc)
  665. -------------------------------------------------------------------------- */
  666. static struct proc_dir_entry *acpi_thermal_dir;
  667. static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
  668. {
  669. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  670. if (!tz)
  671. goto end;
  672. seq_puts(seq, "state: ");
  673. if (!tz->state.critical && !tz->state.hot && !tz->state.passive
  674. && !tz->state.active)
  675. seq_puts(seq, "ok\n");
  676. else {
  677. if (tz->state.critical)
  678. seq_puts(seq, "critical ");
  679. if (tz->state.hot)
  680. seq_puts(seq, "hot ");
  681. if (tz->state.passive)
  682. seq_puts(seq, "passive ");
  683. if (tz->state.active)
  684. seq_printf(seq, "active[%d]", tz->state.active_index);
  685. seq_puts(seq, "\n");
  686. }
  687. end:
  688. return 0;
  689. }
  690. static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
  691. {
  692. return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
  693. }
  694. static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
  695. {
  696. int result = 0;
  697. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  698. if (!tz)
  699. goto end;
  700. result = acpi_thermal_get_temperature(tz);
  701. if (result)
  702. goto end;
  703. seq_printf(seq, "temperature: %ld C\n",
  704. KELVIN_TO_CELSIUS(tz->temperature));
  705. end:
  706. return 0;
  707. }
  708. static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
  709. {
  710. return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
  711. }
  712. static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
  713. {
  714. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  715. int i = 0;
  716. int j = 0;
  717. if (!tz)
  718. goto end;
  719. if (tz->trips.critical.flags.valid)
  720. seq_printf(seq, "critical (S5): %ld C\n",
  721. KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
  722. if (tz->trips.hot.flags.valid)
  723. seq_printf(seq, "hot (S4): %ld C\n",
  724. KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
  725. if (tz->trips.passive.flags.valid) {
  726. seq_printf(seq,
  727. "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
  728. KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
  729. tz->trips.passive.tc1, tz->trips.passive.tc2,
  730. tz->trips.passive.tsp);
  731. for (j = 0; j < tz->trips.passive.devices.count; j++) {
  732. seq_printf(seq, "0x%p ",
  733. tz->trips.passive.devices.handles[j]);
  734. }
  735. seq_puts(seq, "\n");
  736. }
  737. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  738. if (!(tz->trips.active[i].flags.valid))
  739. break;
  740. seq_printf(seq, "active[%d]: %ld C: devices=",
  741. i,
  742. KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
  743. for (j = 0; j < tz->trips.active[i].devices.count; j++)
  744. seq_printf(seq, "0x%p ",
  745. tz->trips.active[i].devices.handles[j]);
  746. seq_puts(seq, "\n");
  747. }
  748. end:
  749. return 0;
  750. }
  751. static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
  752. {
  753. return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
  754. }
  755. static ssize_t
  756. acpi_thermal_write_trip_points(struct file *file,
  757. const char __user * buffer,
  758. size_t count, loff_t * ppos)
  759. {
  760. struct seq_file *m = (struct seq_file *)file->private_data;
  761. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  762. char *limit_string;
  763. int num, critical, hot, passive;
  764. int *active;
  765. int i = 0;
  766. limit_string = kmalloc(ACPI_THERMAL_MAX_LIMIT_STR_LEN, GFP_KERNEL);
  767. if (!limit_string)
  768. return -ENOMEM;
  769. memset(limit_string, 0, ACPI_THERMAL_MAX_LIMIT_STR_LEN);
  770. active = kmalloc(ACPI_THERMAL_MAX_ACTIVE * sizeof(int), GFP_KERNEL);
  771. if (!active) {
  772. kfree(limit_string);
  773. return -ENOMEM;
  774. }
  775. if (!tz || (count > ACPI_THERMAL_MAX_LIMIT_STR_LEN - 1)) {
  776. count = -EINVAL;
  777. goto end;
  778. }
  779. if (copy_from_user(limit_string, buffer, count)) {
  780. count = -EFAULT;
  781. goto end;
  782. }
  783. limit_string[count] = '\0';
  784. num = sscanf(limit_string, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
  785. &critical, &hot, &passive,
  786. &active[0], &active[1], &active[2], &active[3], &active[4],
  787. &active[5], &active[6], &active[7], &active[8],
  788. &active[9]);
  789. if (!(num >= 5 && num < (ACPI_THERMAL_MAX_ACTIVE + 3))) {
  790. count = -EINVAL;
  791. goto end;
  792. }
  793. tz->trips.critical.temperature = CELSIUS_TO_KELVIN(critical);
  794. tz->trips.hot.temperature = CELSIUS_TO_KELVIN(hot);
  795. tz->trips.passive.temperature = CELSIUS_TO_KELVIN(passive);
  796. for (i = 0; i < num - 3; i++) {
  797. if (!(tz->trips.active[i].flags.valid))
  798. break;
  799. tz->trips.active[i].temperature = CELSIUS_TO_KELVIN(active[i]);
  800. }
  801. end:
  802. kfree(active);
  803. kfree(limit_string);
  804. return count;
  805. }
  806. static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
  807. {
  808. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  809. if (!tz)
  810. goto end;
  811. if (!tz->flags.cooling_mode) {
  812. seq_puts(seq, "<setting not supported>\n");
  813. }
  814. if (tz->cooling_mode == ACPI_THERMAL_MODE_CRITICAL)
  815. seq_printf(seq, "cooling mode: critical\n");
  816. else
  817. seq_printf(seq, "cooling mode: %s\n",
  818. tz->cooling_mode ? "passive" : "active");
  819. end:
  820. return 0;
  821. }
  822. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
  823. {
  824. return single_open(file, acpi_thermal_cooling_seq_show,
  825. PDE(inode)->data);
  826. }
  827. static ssize_t
  828. acpi_thermal_write_cooling_mode(struct file *file,
  829. const char __user * buffer,
  830. size_t count, loff_t * ppos)
  831. {
  832. struct seq_file *m = (struct seq_file *)file->private_data;
  833. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  834. int result = 0;
  835. char mode_string[12] = { '\0' };
  836. if (!tz || (count > sizeof(mode_string) - 1))
  837. return -EINVAL;
  838. if (!tz->flags.cooling_mode)
  839. return -ENODEV;
  840. if (copy_from_user(mode_string, buffer, count))
  841. return -EFAULT;
  842. mode_string[count] = '\0';
  843. result = acpi_thermal_set_cooling_mode(tz,
  844. simple_strtoul(mode_string, NULL,
  845. 0));
  846. if (result)
  847. return result;
  848. acpi_thermal_check(tz);
  849. return count;
  850. }
  851. static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
  852. {
  853. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  854. if (!tz)
  855. goto end;
  856. if (!tz->polling_frequency) {
  857. seq_puts(seq, "<polling disabled>\n");
  858. goto end;
  859. }
  860. seq_printf(seq, "polling frequency: %lu seconds\n",
  861. (tz->polling_frequency / 10));
  862. end:
  863. return 0;
  864. }
  865. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
  866. {
  867. return single_open(file, acpi_thermal_polling_seq_show,
  868. PDE(inode)->data);
  869. }
  870. static ssize_t
  871. acpi_thermal_write_polling(struct file *file,
  872. const char __user * buffer,
  873. size_t count, loff_t * ppos)
  874. {
  875. struct seq_file *m = (struct seq_file *)file->private_data;
  876. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  877. int result = 0;
  878. char polling_string[12] = { '\0' };
  879. int seconds = 0;
  880. if (!tz || (count > sizeof(polling_string) - 1))
  881. return -EINVAL;
  882. if (copy_from_user(polling_string, buffer, count))
  883. return -EFAULT;
  884. polling_string[count] = '\0';
  885. seconds = simple_strtoul(polling_string, NULL, 0);
  886. result = acpi_thermal_set_polling(tz, seconds);
  887. if (result)
  888. return result;
  889. acpi_thermal_check(tz);
  890. return count;
  891. }
  892. static int acpi_thermal_add_fs(struct acpi_device *device)
  893. {
  894. struct proc_dir_entry *entry = NULL;
  895. if (!acpi_device_dir(device)) {
  896. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  897. acpi_thermal_dir);
  898. if (!acpi_device_dir(device))
  899. return -ENODEV;
  900. acpi_device_dir(device)->owner = THIS_MODULE;
  901. }
  902. /* 'state' [R] */
  903. entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
  904. S_IRUGO, acpi_device_dir(device));
  905. if (!entry)
  906. return -ENODEV;
  907. else {
  908. entry->proc_fops = &acpi_thermal_state_fops;
  909. entry->data = acpi_driver_data(device);
  910. entry->owner = THIS_MODULE;
  911. }
  912. /* 'temperature' [R] */
  913. entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  914. S_IRUGO, acpi_device_dir(device));
  915. if (!entry)
  916. return -ENODEV;
  917. else {
  918. entry->proc_fops = &acpi_thermal_temp_fops;
  919. entry->data = acpi_driver_data(device);
  920. entry->owner = THIS_MODULE;
  921. }
  922. /* 'trip_points' [R/W] */
  923. entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  924. S_IFREG | S_IRUGO | S_IWUSR,
  925. acpi_device_dir(device));
  926. if (!entry)
  927. return -ENODEV;
  928. else {
  929. entry->proc_fops = &acpi_thermal_trip_fops;
  930. entry->data = acpi_driver_data(device);
  931. entry->owner = THIS_MODULE;
  932. }
  933. /* 'cooling_mode' [R/W] */
  934. entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  935. S_IFREG | S_IRUGO | S_IWUSR,
  936. acpi_device_dir(device));
  937. if (!entry)
  938. return -ENODEV;
  939. else {
  940. entry->proc_fops = &acpi_thermal_cooling_fops;
  941. entry->data = acpi_driver_data(device);
  942. entry->owner = THIS_MODULE;
  943. }
  944. /* 'polling_frequency' [R/W] */
  945. entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  946. S_IFREG | S_IRUGO | S_IWUSR,
  947. acpi_device_dir(device));
  948. if (!entry)
  949. return -ENODEV;
  950. else {
  951. entry->proc_fops = &acpi_thermal_polling_fops;
  952. entry->data = acpi_driver_data(device);
  953. entry->owner = THIS_MODULE;
  954. }
  955. return 0;
  956. }
  957. static int acpi_thermal_remove_fs(struct acpi_device *device)
  958. {
  959. if (acpi_device_dir(device)) {
  960. remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  961. acpi_device_dir(device));
  962. remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  963. acpi_device_dir(device));
  964. remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  965. acpi_device_dir(device));
  966. remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  967. acpi_device_dir(device));
  968. remove_proc_entry(ACPI_THERMAL_FILE_STATE,
  969. acpi_device_dir(device));
  970. remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
  971. acpi_device_dir(device) = NULL;
  972. }
  973. return 0;
  974. }
  975. /* --------------------------------------------------------------------------
  976. Driver Interface
  977. -------------------------------------------------------------------------- */
  978. static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
  979. {
  980. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  981. struct acpi_device *device = NULL;
  982. if (!tz)
  983. return;
  984. if (acpi_bus_get_device(tz->handle, &device))
  985. return;
  986. switch (event) {
  987. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  988. acpi_thermal_check(tz);
  989. break;
  990. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  991. acpi_thermal_get_trip_points(tz);
  992. acpi_thermal_check(tz);
  993. acpi_bus_generate_event(device, event, 0);
  994. break;
  995. case ACPI_THERMAL_NOTIFY_DEVICES:
  996. if (tz->flags.devices)
  997. acpi_thermal_get_devices(tz);
  998. acpi_bus_generate_event(device, event, 0);
  999. break;
  1000. default:
  1001. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1002. "Unsupported event [0x%x]\n", event));
  1003. break;
  1004. }
  1005. return;
  1006. }
  1007. static int acpi_thermal_get_info(struct acpi_thermal *tz)
  1008. {
  1009. int result = 0;
  1010. if (!tz)
  1011. return -EINVAL;
  1012. /* Get temperature [_TMP] (required) */
  1013. result = acpi_thermal_get_temperature(tz);
  1014. if (result)
  1015. return result;
  1016. /* Get trip points [_CRT, _PSV, etc.] (required) */
  1017. result = acpi_thermal_get_trip_points(tz);
  1018. if (result)
  1019. return result;
  1020. /* Set the cooling mode [_SCP] to active cooling (default) */
  1021. result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
  1022. if (!result)
  1023. tz->flags.cooling_mode = 1;
  1024. else {
  1025. /* Oh,we have not _SCP method.
  1026. Generally show cooling_mode by _ACx, _PSV,spec 12.2 */
  1027. tz->flags.cooling_mode = 0;
  1028. if (tz->trips.active[0].flags.valid
  1029. && tz->trips.passive.flags.valid) {
  1030. if (tz->trips.passive.temperature >
  1031. tz->trips.active[0].temperature)
  1032. tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
  1033. else
  1034. tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
  1035. } else if (!tz->trips.active[0].flags.valid
  1036. && tz->trips.passive.flags.valid) {
  1037. tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
  1038. } else if (tz->trips.active[0].flags.valid
  1039. && !tz->trips.passive.flags.valid) {
  1040. tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
  1041. } else {
  1042. /* _ACx and _PSV are optional, but _CRT is required */
  1043. tz->cooling_mode = ACPI_THERMAL_MODE_CRITICAL;
  1044. }
  1045. }
  1046. /* Get default polling frequency [_TZP] (optional) */
  1047. if (tzp)
  1048. tz->polling_frequency = tzp;
  1049. else
  1050. acpi_thermal_get_polling_frequency(tz);
  1051. /* Get devices in this thermal zone [_TZD] (optional) */
  1052. result = acpi_thermal_get_devices(tz);
  1053. if (!result)
  1054. tz->flags.devices = 1;
  1055. return 0;
  1056. }
  1057. static int acpi_thermal_add(struct acpi_device *device)
  1058. {
  1059. int result = 0;
  1060. acpi_status status = AE_OK;
  1061. struct acpi_thermal *tz = NULL;
  1062. if (!device)
  1063. return -EINVAL;
  1064. tz = kmalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  1065. if (!tz)
  1066. return -ENOMEM;
  1067. memset(tz, 0, sizeof(struct acpi_thermal));
  1068. tz->handle = device->handle;
  1069. strcpy(tz->name, device->pnp.bus_id);
  1070. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  1071. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  1072. acpi_driver_data(device) = tz;
  1073. result = acpi_thermal_get_info(tz);
  1074. if (result)
  1075. goto end;
  1076. result = acpi_thermal_add_fs(device);
  1077. if (result)
  1078. goto end;
  1079. init_timer(&tz->timer);
  1080. acpi_thermal_check(tz);
  1081. status = acpi_install_notify_handler(tz->handle,
  1082. ACPI_DEVICE_NOTIFY,
  1083. acpi_thermal_notify, tz);
  1084. if (ACPI_FAILURE(status)) {
  1085. result = -ENODEV;
  1086. goto end;
  1087. }
  1088. printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
  1089. acpi_device_name(device), acpi_device_bid(device),
  1090. KELVIN_TO_CELSIUS(tz->temperature));
  1091. end:
  1092. if (result) {
  1093. acpi_thermal_remove_fs(device);
  1094. kfree(tz);
  1095. }
  1096. return result;
  1097. }
  1098. static int acpi_thermal_remove(struct acpi_device *device, int type)
  1099. {
  1100. acpi_status status = AE_OK;
  1101. struct acpi_thermal *tz = NULL;
  1102. if (!device || !acpi_driver_data(device))
  1103. return -EINVAL;
  1104. tz = (struct acpi_thermal *)acpi_driver_data(device);
  1105. /* avoid timer adding new defer task */
  1106. tz->zombie = 1;
  1107. /* wait for running timer (on other CPUs) finish */
  1108. del_timer_sync(&(tz->timer));
  1109. /* synchronize deferred task */
  1110. acpi_os_wait_events_complete(NULL);
  1111. /* deferred task may reinsert timer */
  1112. del_timer_sync(&(tz->timer));
  1113. status = acpi_remove_notify_handler(tz->handle,
  1114. ACPI_DEVICE_NOTIFY,
  1115. acpi_thermal_notify);
  1116. /* Terminate policy */
  1117. if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
  1118. tz->trips.passive.flags.enabled = 0;
  1119. acpi_thermal_passive(tz);
  1120. }
  1121. if (tz->trips.active[0].flags.valid
  1122. && tz->trips.active[0].flags.enabled) {
  1123. tz->trips.active[0].flags.enabled = 0;
  1124. acpi_thermal_active(tz);
  1125. }
  1126. acpi_thermal_remove_fs(device);
  1127. kfree(tz);
  1128. return 0;
  1129. }
  1130. static int acpi_thermal_resume(struct acpi_device *device, int state)
  1131. {
  1132. struct acpi_thermal *tz = NULL;
  1133. if (!device || !acpi_driver_data(device))
  1134. return -EINVAL;
  1135. tz = (struct acpi_thermal *)acpi_driver_data(device);
  1136. acpi_thermal_check(tz);
  1137. return AE_OK;
  1138. }
  1139. static int __init acpi_thermal_init(void)
  1140. {
  1141. int result = 0;
  1142. acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
  1143. if (!acpi_thermal_dir)
  1144. return -ENODEV;
  1145. acpi_thermal_dir->owner = THIS_MODULE;
  1146. result = acpi_bus_register_driver(&acpi_thermal_driver);
  1147. if (result < 0) {
  1148. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1149. return -ENODEV;
  1150. }
  1151. return 0;
  1152. }
  1153. static void __exit acpi_thermal_exit(void)
  1154. {
  1155. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1156. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1157. return;
  1158. }
  1159. module_init(acpi_thermal_init);
  1160. module_exit(acpi_thermal_exit);