thermal.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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. ACPI_FUNCTION_TRACE("acpi_thermal_get_temperature");
  199. if (!tz)
  200. return_VALUE(-EINVAL);
  201. tz->last_temperature = tz->temperature;
  202. status =
  203. acpi_evaluate_integer(tz->handle, "_TMP", NULL, &tz->temperature);
  204. if (ACPI_FAILURE(status))
  205. return_VALUE(-ENODEV);
  206. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
  207. tz->temperature));
  208. return_VALUE(0);
  209. }
  210. static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
  211. {
  212. acpi_status status = AE_OK;
  213. ACPI_FUNCTION_TRACE("acpi_thermal_get_polling_frequency");
  214. if (!tz)
  215. return_VALUE(-EINVAL);
  216. status =
  217. acpi_evaluate_integer(tz->handle, "_TZP", NULL,
  218. &tz->polling_frequency);
  219. if (ACPI_FAILURE(status))
  220. return_VALUE(-ENODEV);
  221. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
  222. tz->polling_frequency));
  223. return_VALUE(0);
  224. }
  225. static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
  226. {
  227. ACPI_FUNCTION_TRACE("acpi_thermal_set_polling");
  228. if (!tz)
  229. return_VALUE(-EINVAL);
  230. tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
  231. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  232. "Polling frequency set to %lu seconds\n",
  233. tz->polling_frequency));
  234. return_VALUE(0);
  235. }
  236. static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
  237. {
  238. acpi_status status = AE_OK;
  239. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  240. struct acpi_object_list arg_list = { 1, &arg0 };
  241. acpi_handle handle = NULL;
  242. ACPI_FUNCTION_TRACE("acpi_thermal_set_cooling_mode");
  243. if (!tz)
  244. return_VALUE(-EINVAL);
  245. status = acpi_get_handle(tz->handle, "_SCP", &handle);
  246. if (ACPI_FAILURE(status)) {
  247. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
  248. return_VALUE(-ENODEV);
  249. }
  250. arg0.integer.value = mode;
  251. status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
  252. if (ACPI_FAILURE(status))
  253. return_VALUE(-ENODEV);
  254. tz->cooling_mode = mode;
  255. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cooling mode [%s]\n",
  256. mode ? "passive" : "active"));
  257. return_VALUE(0);
  258. }
  259. static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
  260. {
  261. acpi_status status = AE_OK;
  262. int i = 0;
  263. ACPI_FUNCTION_TRACE("acpi_thermal_get_trip_points");
  264. if (!tz)
  265. return_VALUE(-EINVAL);
  266. /* Critical Shutdown (required) */
  267. status = acpi_evaluate_integer(tz->handle, "_CRT", NULL,
  268. &tz->trips.critical.temperature);
  269. if (ACPI_FAILURE(status)) {
  270. tz->trips.critical.flags.valid = 0;
  271. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "No critical threshold\n"));
  272. return_VALUE(-ENODEV);
  273. } else {
  274. tz->trips.critical.flags.valid = 1;
  275. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  276. "Found critical threshold [%lu]\n",
  277. tz->trips.critical.temperature));
  278. }
  279. /* Critical Sleep (optional) */
  280. status =
  281. acpi_evaluate_integer(tz->handle, "_HOT", NULL,
  282. &tz->trips.hot.temperature);
  283. if (ACPI_FAILURE(status)) {
  284. tz->trips.hot.flags.valid = 0;
  285. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
  286. } else {
  287. tz->trips.hot.flags.valid = 1;
  288. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
  289. tz->trips.hot.temperature));
  290. }
  291. /* Passive: Processors (optional) */
  292. status =
  293. acpi_evaluate_integer(tz->handle, "_PSV", NULL,
  294. &tz->trips.passive.temperature);
  295. if (ACPI_FAILURE(status)) {
  296. tz->trips.passive.flags.valid = 0;
  297. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
  298. } else {
  299. tz->trips.passive.flags.valid = 1;
  300. status =
  301. acpi_evaluate_integer(tz->handle, "_TC1", NULL,
  302. &tz->trips.passive.tc1);
  303. if (ACPI_FAILURE(status))
  304. tz->trips.passive.flags.valid = 0;
  305. status =
  306. acpi_evaluate_integer(tz->handle, "_TC2", NULL,
  307. &tz->trips.passive.tc2);
  308. if (ACPI_FAILURE(status))
  309. tz->trips.passive.flags.valid = 0;
  310. status =
  311. acpi_evaluate_integer(tz->handle, "_TSP", NULL,
  312. &tz->trips.passive.tsp);
  313. if (ACPI_FAILURE(status))
  314. tz->trips.passive.flags.valid = 0;
  315. status =
  316. acpi_evaluate_reference(tz->handle, "_PSL", NULL,
  317. &tz->trips.passive.devices);
  318. if (ACPI_FAILURE(status))
  319. tz->trips.passive.flags.valid = 0;
  320. if (!tz->trips.passive.flags.valid)
  321. ACPI_DEBUG_PRINT((ACPI_DB_WARN,
  322. "Invalid passive threshold\n"));
  323. else
  324. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  325. "Found passive threshold [%lu]\n",
  326. tz->trips.passive.temperature));
  327. }
  328. /* Active: Fans, etc. (optional) */
  329. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  330. char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
  331. status =
  332. acpi_evaluate_integer(tz->handle, name, NULL,
  333. &tz->trips.active[i].temperature);
  334. if (ACPI_FAILURE(status))
  335. break;
  336. name[2] = 'L';
  337. status =
  338. acpi_evaluate_reference(tz->handle, name, NULL,
  339. &tz->trips.active[i].devices);
  340. if (ACPI_SUCCESS(status)) {
  341. tz->trips.active[i].flags.valid = 1;
  342. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  343. "Found active threshold [%d]:[%lu]\n",
  344. i, tz->trips.active[i].temperature));
  345. } else
  346. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  347. "Invalid active threshold [%d]\n",
  348. i));
  349. }
  350. return_VALUE(0);
  351. }
  352. static int acpi_thermal_get_devices(struct acpi_thermal *tz)
  353. {
  354. acpi_status status = AE_OK;
  355. ACPI_FUNCTION_TRACE("acpi_thermal_get_devices");
  356. if (!tz)
  357. return_VALUE(-EINVAL);
  358. status =
  359. acpi_evaluate_reference(tz->handle, "_TZD", NULL, &tz->devices);
  360. if (ACPI_FAILURE(status))
  361. return_VALUE(-ENODEV);
  362. return_VALUE(0);
  363. }
  364. static int acpi_thermal_call_usermode(char *path)
  365. {
  366. char *argv[2] = { NULL, NULL };
  367. char *envp[3] = { NULL, NULL, NULL };
  368. ACPI_FUNCTION_TRACE("acpi_thermal_call_usermode");
  369. if (!path)
  370. return_VALUE(-EINVAL);
  371. argv[0] = path;
  372. /* minimal command environment */
  373. envp[0] = "HOME=/";
  374. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  375. call_usermodehelper(argv[0], argv, envp, 0);
  376. return_VALUE(0);
  377. }
  378. static int acpi_thermal_critical(struct acpi_thermal *tz)
  379. {
  380. int result = 0;
  381. struct acpi_device *device = NULL;
  382. ACPI_FUNCTION_TRACE("acpi_thermal_critical");
  383. if (!tz || !tz->trips.critical.flags.valid)
  384. return_VALUE(-EINVAL);
  385. if (tz->temperature >= tz->trips.critical.temperature) {
  386. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Critical trip point\n"));
  387. tz->trips.critical.flags.enabled = 1;
  388. } else if (tz->trips.critical.flags.enabled)
  389. tz->trips.critical.flags.enabled = 0;
  390. result = acpi_bus_get_device(tz->handle, &device);
  391. if (result)
  392. return_VALUE(result);
  393. printk(KERN_EMERG
  394. "Critical temperature reached (%ld C), shutting down.\n",
  395. KELVIN_TO_CELSIUS(tz->temperature));
  396. acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_CRITICAL,
  397. tz->trips.critical.flags.enabled);
  398. acpi_thermal_call_usermode(ACPI_THERMAL_PATH_POWEROFF);
  399. return_VALUE(0);
  400. }
  401. static int acpi_thermal_hot(struct acpi_thermal *tz)
  402. {
  403. int result = 0;
  404. struct acpi_device *device = NULL;
  405. ACPI_FUNCTION_TRACE("acpi_thermal_hot");
  406. if (!tz || !tz->trips.hot.flags.valid)
  407. return_VALUE(-EINVAL);
  408. if (tz->temperature >= tz->trips.hot.temperature) {
  409. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Hot trip point\n"));
  410. tz->trips.hot.flags.enabled = 1;
  411. } else if (tz->trips.hot.flags.enabled)
  412. tz->trips.hot.flags.enabled = 0;
  413. result = acpi_bus_get_device(tz->handle, &device);
  414. if (result)
  415. return_VALUE(result);
  416. acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_HOT,
  417. tz->trips.hot.flags.enabled);
  418. /* TBD: Call user-mode "sleep(S4)" function */
  419. return_VALUE(0);
  420. }
  421. static void acpi_thermal_passive(struct acpi_thermal *tz)
  422. {
  423. int result = 1;
  424. struct acpi_thermal_passive *passive = NULL;
  425. int trend = 0;
  426. int i = 0;
  427. ACPI_FUNCTION_TRACE("acpi_thermal_passive");
  428. if (!tz || !tz->trips.passive.flags.valid)
  429. return;
  430. passive = &(tz->trips.passive);
  431. /*
  432. * Above Trip?
  433. * -----------
  434. * Calculate the thermal trend (using the passive cooling equation)
  435. * and modify the performance limit for all passive cooling devices
  436. * accordingly. Note that we assume symmetry.
  437. */
  438. if (tz->temperature >= passive->temperature) {
  439. trend =
  440. (passive->tc1 * (tz->temperature - tz->last_temperature)) +
  441. (passive->tc2 * (tz->temperature - passive->temperature));
  442. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  443. "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
  444. trend, passive->tc1, tz->temperature,
  445. tz->last_temperature, passive->tc2,
  446. tz->temperature, passive->temperature));
  447. passive->flags.enabled = 1;
  448. /* Heating up? */
  449. if (trend > 0)
  450. for (i = 0; i < passive->devices.count; i++)
  451. acpi_processor_set_thermal_limit(passive->
  452. devices.
  453. handles[i],
  454. ACPI_PROCESSOR_LIMIT_INCREMENT);
  455. /* Cooling off? */
  456. else if (trend < 0) {
  457. for (i = 0; i < passive->devices.count; i++)
  458. /*
  459. * assume that we are on highest
  460. * freq/lowest thrott and can leave
  461. * passive mode, even in error case
  462. */
  463. if (!acpi_processor_set_thermal_limit
  464. (passive->devices.handles[i],
  465. ACPI_PROCESSOR_LIMIT_DECREMENT))
  466. result = 0;
  467. /*
  468. * Leave cooling mode, even if the temp might
  469. * higher than trip point This is because some
  470. * machines might have long thermal polling
  471. * frequencies (tsp) defined. We will fall back
  472. * into passive mode in next cycle (probably quicker)
  473. */
  474. if (result) {
  475. passive->flags.enabled = 0;
  476. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  477. "Disabling passive cooling, still above threshold,"
  478. " but we are cooling down\n"));
  479. }
  480. }
  481. return;
  482. }
  483. /*
  484. * Below Trip?
  485. * -----------
  486. * Implement passive cooling hysteresis to slowly increase performance
  487. * and avoid thrashing around the passive trip point. Note that we
  488. * assume symmetry.
  489. */
  490. if (!passive->flags.enabled)
  491. return;
  492. for (i = 0; i < passive->devices.count; i++)
  493. if (!acpi_processor_set_thermal_limit
  494. (passive->devices.handles[i],
  495. ACPI_PROCESSOR_LIMIT_DECREMENT))
  496. result = 0;
  497. if (result) {
  498. passive->flags.enabled = 0;
  499. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  500. "Disabling passive cooling (zone is cool)\n"));
  501. }
  502. }
  503. static void acpi_thermal_active(struct acpi_thermal *tz)
  504. {
  505. int result = 0;
  506. struct acpi_thermal_active *active = NULL;
  507. int i = 0;
  508. int j = 0;
  509. unsigned long maxtemp = 0;
  510. ACPI_FUNCTION_TRACE("acpi_thermal_active");
  511. if (!tz)
  512. return;
  513. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  514. active = &(tz->trips.active[i]);
  515. if (!active || !active->flags.valid)
  516. break;
  517. if (tz->temperature >= active->temperature) {
  518. /*
  519. * Above Threshold?
  520. * ----------------
  521. * If not already enabled, turn ON all cooling devices
  522. * associated with this active threshold.
  523. */
  524. if (active->temperature > maxtemp)
  525. tz->state.active_index = i;
  526. maxtemp = active->temperature;
  527. if (active->flags.enabled)
  528. continue;
  529. for (j = 0; j < active->devices.count; j++) {
  530. result =
  531. acpi_bus_set_power(active->devices.
  532. handles[j],
  533. ACPI_STATE_D0);
  534. if (result) {
  535. ACPI_DEBUG_PRINT((ACPI_DB_WARN,
  536. "Unable to turn cooling device [%p] 'on'\n",
  537. active->devices.
  538. handles[j]));
  539. continue;
  540. }
  541. active->flags.enabled = 1;
  542. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  543. "Cooling device [%p] now 'on'\n",
  544. active->devices.handles[j]));
  545. }
  546. continue;
  547. }
  548. if (!active->flags.enabled)
  549. continue;
  550. /*
  551. * Below Threshold?
  552. * ----------------
  553. * Turn OFF all cooling devices associated with this
  554. * threshold.
  555. */
  556. for (j = 0; j < active->devices.count; j++) {
  557. result = acpi_bus_set_power(active->devices.handles[j],
  558. ACPI_STATE_D3);
  559. if (result) {
  560. ACPI_DEBUG_PRINT((ACPI_DB_WARN,
  561. "Unable to turn cooling device [%p] 'off'\n",
  562. active->devices.handles[j]));
  563. continue;
  564. }
  565. active->flags.enabled = 0;
  566. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  567. "Cooling device [%p] now 'off'\n",
  568. active->devices.handles[j]));
  569. }
  570. }
  571. }
  572. static void acpi_thermal_check(void *context);
  573. static void acpi_thermal_run(unsigned long data)
  574. {
  575. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  576. if (!tz->zombie)
  577. acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
  578. }
  579. static void acpi_thermal_check(void *data)
  580. {
  581. int result = 0;
  582. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  583. unsigned long sleep_time = 0;
  584. int i = 0;
  585. struct acpi_thermal_state state;
  586. ACPI_FUNCTION_TRACE("acpi_thermal_check");
  587. if (!tz) {
  588. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
  589. return_VOID;
  590. }
  591. state = tz->state;
  592. result = acpi_thermal_get_temperature(tz);
  593. if (result)
  594. return_VOID;
  595. memset(&tz->state, 0, sizeof(tz->state));
  596. /*
  597. * Check Trip Points
  598. * -----------------
  599. * Compare the current temperature to the trip point values to see
  600. * if we've entered one of the thermal policy states. Note that
  601. * this function determines when a state is entered, but the
  602. * individual policy decides when it is exited (e.g. hysteresis).
  603. */
  604. if (tz->trips.critical.flags.valid)
  605. state.critical |=
  606. (tz->temperature >= tz->trips.critical.temperature);
  607. if (tz->trips.hot.flags.valid)
  608. state.hot |= (tz->temperature >= tz->trips.hot.temperature);
  609. if (tz->trips.passive.flags.valid)
  610. state.passive |=
  611. (tz->temperature >= tz->trips.passive.temperature);
  612. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  613. if (tz->trips.active[i].flags.valid)
  614. state.active |=
  615. (tz->temperature >=
  616. tz->trips.active[i].temperature);
  617. /*
  618. * Invoke Policy
  619. * -------------
  620. * Separated from the above check to allow individual policy to
  621. * determine when to exit a given state.
  622. */
  623. if (state.critical)
  624. acpi_thermal_critical(tz);
  625. if (state.hot)
  626. acpi_thermal_hot(tz);
  627. if (state.passive)
  628. acpi_thermal_passive(tz);
  629. if (state.active)
  630. acpi_thermal_active(tz);
  631. /*
  632. * Calculate State
  633. * ---------------
  634. * Again, separated from the above two to allow independent policy
  635. * decisions.
  636. */
  637. tz->state.critical = tz->trips.critical.flags.enabled;
  638. tz->state.hot = tz->trips.hot.flags.enabled;
  639. tz->state.passive = tz->trips.passive.flags.enabled;
  640. tz->state.active = 0;
  641. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  642. tz->state.active |= tz->trips.active[i].flags.enabled;
  643. /*
  644. * Calculate Sleep Time
  645. * --------------------
  646. * If we're in the passive state, use _TSP's value. Otherwise
  647. * use the default polling frequency (e.g. _TZP). If no polling
  648. * frequency is specified then we'll wait forever (at least until
  649. * a thermal event occurs). Note that _TSP and _TZD values are
  650. * given in 1/10th seconds (we must covert to milliseconds).
  651. */
  652. if (tz->state.passive)
  653. sleep_time = tz->trips.passive.tsp * 100;
  654. else if (tz->polling_frequency > 0)
  655. sleep_time = tz->polling_frequency * 100;
  656. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
  657. tz->name, tz->temperature, sleep_time));
  658. /*
  659. * Schedule Next Poll
  660. * ------------------
  661. */
  662. if (!sleep_time) {
  663. if (timer_pending(&(tz->timer)))
  664. del_timer(&(tz->timer));
  665. } else {
  666. if (timer_pending(&(tz->timer)))
  667. mod_timer(&(tz->timer), (HZ * sleep_time) / 1000);
  668. else {
  669. tz->timer.data = (unsigned long)tz;
  670. tz->timer.function = acpi_thermal_run;
  671. tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
  672. add_timer(&(tz->timer));
  673. }
  674. }
  675. return_VOID;
  676. }
  677. /* --------------------------------------------------------------------------
  678. FS Interface (/proc)
  679. -------------------------------------------------------------------------- */
  680. static struct proc_dir_entry *acpi_thermal_dir;
  681. static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
  682. {
  683. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  684. ACPI_FUNCTION_TRACE("acpi_thermal_state_seq_show");
  685. if (!tz)
  686. goto end;
  687. seq_puts(seq, "state: ");
  688. if (!tz->state.critical && !tz->state.hot && !tz->state.passive
  689. && !tz->state.active)
  690. seq_puts(seq, "ok\n");
  691. else {
  692. if (tz->state.critical)
  693. seq_puts(seq, "critical ");
  694. if (tz->state.hot)
  695. seq_puts(seq, "hot ");
  696. if (tz->state.passive)
  697. seq_puts(seq, "passive ");
  698. if (tz->state.active)
  699. seq_printf(seq, "active[%d]", tz->state.active_index);
  700. seq_puts(seq, "\n");
  701. }
  702. end:
  703. return_VALUE(0);
  704. }
  705. static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
  706. {
  707. return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
  708. }
  709. static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
  710. {
  711. int result = 0;
  712. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  713. ACPI_FUNCTION_TRACE("acpi_thermal_temp_seq_show");
  714. if (!tz)
  715. goto end;
  716. result = acpi_thermal_get_temperature(tz);
  717. if (result)
  718. goto end;
  719. seq_printf(seq, "temperature: %ld C\n",
  720. KELVIN_TO_CELSIUS(tz->temperature));
  721. end:
  722. return_VALUE(0);
  723. }
  724. static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
  725. {
  726. return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
  727. }
  728. static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
  729. {
  730. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  731. int i = 0;
  732. int j = 0;
  733. ACPI_FUNCTION_TRACE("acpi_thermal_trip_seq_show");
  734. if (!tz)
  735. goto end;
  736. if (tz->trips.critical.flags.valid)
  737. seq_printf(seq, "critical (S5): %ld C\n",
  738. KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
  739. if (tz->trips.hot.flags.valid)
  740. seq_printf(seq, "hot (S4): %ld C\n",
  741. KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
  742. if (tz->trips.passive.flags.valid) {
  743. seq_printf(seq,
  744. "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
  745. KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
  746. tz->trips.passive.tc1, tz->trips.passive.tc2,
  747. tz->trips.passive.tsp);
  748. for (j = 0; j < tz->trips.passive.devices.count; j++) {
  749. seq_printf(seq, "0x%p ",
  750. tz->trips.passive.devices.handles[j]);
  751. }
  752. seq_puts(seq, "\n");
  753. }
  754. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  755. if (!(tz->trips.active[i].flags.valid))
  756. break;
  757. seq_printf(seq, "active[%d]: %ld C: devices=",
  758. i,
  759. KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
  760. for (j = 0; j < tz->trips.active[i].devices.count; j++)
  761. seq_printf(seq, "0x%p ",
  762. tz->trips.active[i].devices.handles[j]);
  763. seq_puts(seq, "\n");
  764. }
  765. end:
  766. return_VALUE(0);
  767. }
  768. static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
  769. {
  770. return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
  771. }
  772. static ssize_t
  773. acpi_thermal_write_trip_points(struct file *file,
  774. const char __user * buffer,
  775. size_t count, loff_t * ppos)
  776. {
  777. struct seq_file *m = (struct seq_file *)file->private_data;
  778. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  779. char *limit_string;
  780. int num, critical, hot, passive;
  781. int *active;
  782. int i = 0;
  783. ACPI_FUNCTION_TRACE("acpi_thermal_write_trip_points");
  784. limit_string = kmalloc(ACPI_THERMAL_MAX_LIMIT_STR_LEN, GFP_KERNEL);
  785. if (!limit_string)
  786. return_VALUE(-ENOMEM);
  787. memset(limit_string, 0, ACPI_THERMAL_MAX_LIMIT_STR_LEN);
  788. active = kmalloc(ACPI_THERMAL_MAX_ACTIVE * sizeof(int), GFP_KERNEL);
  789. if (!active) {
  790. kfree(limit_string);
  791. return_VALUE(-ENOMEM);
  792. }
  793. if (!tz || (count > ACPI_THERMAL_MAX_LIMIT_STR_LEN - 1)) {
  794. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument\n"));
  795. count = -EINVAL;
  796. goto end;
  797. }
  798. if (copy_from_user(limit_string, buffer, count)) {
  799. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data\n"));
  800. count = -EFAULT;
  801. goto end;
  802. }
  803. limit_string[count] = '\0';
  804. num = sscanf(limit_string, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
  805. &critical, &hot, &passive,
  806. &active[0], &active[1], &active[2], &active[3], &active[4],
  807. &active[5], &active[6], &active[7], &active[8],
  808. &active[9]);
  809. if (!(num >= 5 && num < (ACPI_THERMAL_MAX_ACTIVE + 3))) {
  810. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data format\n"));
  811. count = -EINVAL;
  812. goto end;
  813. }
  814. tz->trips.critical.temperature = CELSIUS_TO_KELVIN(critical);
  815. tz->trips.hot.temperature = CELSIUS_TO_KELVIN(hot);
  816. tz->trips.passive.temperature = CELSIUS_TO_KELVIN(passive);
  817. for (i = 0; i < num - 3; i++) {
  818. if (!(tz->trips.active[i].flags.valid))
  819. break;
  820. tz->trips.active[i].temperature = CELSIUS_TO_KELVIN(active[i]);
  821. }
  822. end:
  823. kfree(active);
  824. kfree(limit_string);
  825. return_VALUE(count);
  826. }
  827. static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
  828. {
  829. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  830. ACPI_FUNCTION_TRACE("acpi_thermal_cooling_seq_show");
  831. if (!tz)
  832. goto end;
  833. if (!tz->flags.cooling_mode) {
  834. seq_puts(seq, "<setting not supported>\n");
  835. }
  836. if (tz->cooling_mode == ACPI_THERMAL_MODE_CRITICAL)
  837. seq_printf(seq, "cooling mode: critical\n");
  838. else
  839. seq_printf(seq, "cooling mode: %s\n",
  840. tz->cooling_mode ? "passive" : "active");
  841. end:
  842. return_VALUE(0);
  843. }
  844. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
  845. {
  846. return single_open(file, acpi_thermal_cooling_seq_show,
  847. PDE(inode)->data);
  848. }
  849. static ssize_t
  850. acpi_thermal_write_cooling_mode(struct file *file,
  851. const char __user * buffer,
  852. size_t count, loff_t * ppos)
  853. {
  854. struct seq_file *m = (struct seq_file *)file->private_data;
  855. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  856. int result = 0;
  857. char mode_string[12] = { '\0' };
  858. ACPI_FUNCTION_TRACE("acpi_thermal_write_cooling_mode");
  859. if (!tz || (count > sizeof(mode_string) - 1))
  860. return_VALUE(-EINVAL);
  861. if (!tz->flags.cooling_mode)
  862. return_VALUE(-ENODEV);
  863. if (copy_from_user(mode_string, buffer, count))
  864. return_VALUE(-EFAULT);
  865. mode_string[count] = '\0';
  866. result = acpi_thermal_set_cooling_mode(tz,
  867. simple_strtoul(mode_string, NULL,
  868. 0));
  869. if (result)
  870. return_VALUE(result);
  871. acpi_thermal_check(tz);
  872. return_VALUE(count);
  873. }
  874. static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
  875. {
  876. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  877. ACPI_FUNCTION_TRACE("acpi_thermal_polling_seq_show");
  878. if (!tz)
  879. goto end;
  880. if (!tz->polling_frequency) {
  881. seq_puts(seq, "<polling disabled>\n");
  882. goto end;
  883. }
  884. seq_printf(seq, "polling frequency: %lu seconds\n",
  885. (tz->polling_frequency / 10));
  886. end:
  887. return_VALUE(0);
  888. }
  889. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
  890. {
  891. return single_open(file, acpi_thermal_polling_seq_show,
  892. PDE(inode)->data);
  893. }
  894. static ssize_t
  895. acpi_thermal_write_polling(struct file *file,
  896. const char __user * buffer,
  897. size_t count, loff_t * ppos)
  898. {
  899. struct seq_file *m = (struct seq_file *)file->private_data;
  900. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  901. int result = 0;
  902. char polling_string[12] = { '\0' };
  903. int seconds = 0;
  904. ACPI_FUNCTION_TRACE("acpi_thermal_write_polling");
  905. if (!tz || (count > sizeof(polling_string) - 1))
  906. return_VALUE(-EINVAL);
  907. if (copy_from_user(polling_string, buffer, count))
  908. return_VALUE(-EFAULT);
  909. polling_string[count] = '\0';
  910. seconds = simple_strtoul(polling_string, NULL, 0);
  911. result = acpi_thermal_set_polling(tz, seconds);
  912. if (result)
  913. return_VALUE(result);
  914. acpi_thermal_check(tz);
  915. return_VALUE(count);
  916. }
  917. static int acpi_thermal_add_fs(struct acpi_device *device)
  918. {
  919. struct proc_dir_entry *entry = NULL;
  920. ACPI_FUNCTION_TRACE("acpi_thermal_add_fs");
  921. if (!acpi_device_dir(device)) {
  922. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  923. acpi_thermal_dir);
  924. if (!acpi_device_dir(device))
  925. return_VALUE(-ENODEV);
  926. acpi_device_dir(device)->owner = THIS_MODULE;
  927. }
  928. /* 'state' [R] */
  929. entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
  930. S_IRUGO, acpi_device_dir(device));
  931. if (!entry)
  932. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  933. "Unable to create '%s' fs entry\n",
  934. ACPI_THERMAL_FILE_STATE));
  935. else {
  936. entry->proc_fops = &acpi_thermal_state_fops;
  937. entry->data = acpi_driver_data(device);
  938. entry->owner = THIS_MODULE;
  939. }
  940. /* 'temperature' [R] */
  941. entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  942. S_IRUGO, acpi_device_dir(device));
  943. if (!entry)
  944. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  945. "Unable to create '%s' fs entry\n",
  946. ACPI_THERMAL_FILE_TEMPERATURE));
  947. else {
  948. entry->proc_fops = &acpi_thermal_temp_fops;
  949. entry->data = acpi_driver_data(device);
  950. entry->owner = THIS_MODULE;
  951. }
  952. /* 'trip_points' [R/W] */
  953. entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  954. S_IFREG | S_IRUGO | S_IWUSR,
  955. acpi_device_dir(device));
  956. if (!entry)
  957. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  958. "Unable to create '%s' fs entry\n",
  959. ACPI_THERMAL_FILE_TRIP_POINTS));
  960. else {
  961. entry->proc_fops = &acpi_thermal_trip_fops;
  962. entry->data = acpi_driver_data(device);
  963. entry->owner = THIS_MODULE;
  964. }
  965. /* 'cooling_mode' [R/W] */
  966. entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  967. S_IFREG | S_IRUGO | S_IWUSR,
  968. acpi_device_dir(device));
  969. if (!entry)
  970. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  971. "Unable to create '%s' fs entry\n",
  972. ACPI_THERMAL_FILE_COOLING_MODE));
  973. else {
  974. entry->proc_fops = &acpi_thermal_cooling_fops;
  975. entry->data = acpi_driver_data(device);
  976. entry->owner = THIS_MODULE;
  977. }
  978. /* 'polling_frequency' [R/W] */
  979. entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  980. S_IFREG | S_IRUGO | S_IWUSR,
  981. acpi_device_dir(device));
  982. if (!entry)
  983. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  984. "Unable to create '%s' fs entry\n",
  985. ACPI_THERMAL_FILE_POLLING_FREQ));
  986. else {
  987. entry->proc_fops = &acpi_thermal_polling_fops;
  988. entry->data = acpi_driver_data(device);
  989. entry->owner = THIS_MODULE;
  990. }
  991. return_VALUE(0);
  992. }
  993. static int acpi_thermal_remove_fs(struct acpi_device *device)
  994. {
  995. ACPI_FUNCTION_TRACE("acpi_thermal_remove_fs");
  996. if (acpi_device_dir(device)) {
  997. remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  998. acpi_device_dir(device));
  999. remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  1000. acpi_device_dir(device));
  1001. remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  1002. acpi_device_dir(device));
  1003. remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  1004. acpi_device_dir(device));
  1005. remove_proc_entry(ACPI_THERMAL_FILE_STATE,
  1006. acpi_device_dir(device));
  1007. remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
  1008. acpi_device_dir(device) = NULL;
  1009. }
  1010. return_VALUE(0);
  1011. }
  1012. /* --------------------------------------------------------------------------
  1013. Driver Interface
  1014. -------------------------------------------------------------------------- */
  1015. static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
  1016. {
  1017. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  1018. struct acpi_device *device = NULL;
  1019. ACPI_FUNCTION_TRACE("acpi_thermal_notify");
  1020. if (!tz)
  1021. return_VOID;
  1022. if (acpi_bus_get_device(tz->handle, &device))
  1023. return_VOID;
  1024. switch (event) {
  1025. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  1026. acpi_thermal_check(tz);
  1027. break;
  1028. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  1029. acpi_thermal_get_trip_points(tz);
  1030. acpi_thermal_check(tz);
  1031. acpi_bus_generate_event(device, event, 0);
  1032. break;
  1033. case ACPI_THERMAL_NOTIFY_DEVICES:
  1034. if (tz->flags.devices)
  1035. acpi_thermal_get_devices(tz);
  1036. acpi_bus_generate_event(device, event, 0);
  1037. break;
  1038. default:
  1039. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1040. "Unsupported event [0x%x]\n", event));
  1041. break;
  1042. }
  1043. return_VOID;
  1044. }
  1045. static int acpi_thermal_get_info(struct acpi_thermal *tz)
  1046. {
  1047. int result = 0;
  1048. ACPI_FUNCTION_TRACE("acpi_thermal_get_info");
  1049. if (!tz)
  1050. return_VALUE(-EINVAL);
  1051. /* Get temperature [_TMP] (required) */
  1052. result = acpi_thermal_get_temperature(tz);
  1053. if (result)
  1054. return_VALUE(result);
  1055. /* Get trip points [_CRT, _PSV, etc.] (required) */
  1056. result = acpi_thermal_get_trip_points(tz);
  1057. if (result)
  1058. return_VALUE(result);
  1059. /* Set the cooling mode [_SCP] to active cooling (default) */
  1060. result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
  1061. if (!result)
  1062. tz->flags.cooling_mode = 1;
  1063. else {
  1064. /* Oh,we have not _SCP method.
  1065. Generally show cooling_mode by _ACx, _PSV,spec 12.2 */
  1066. tz->flags.cooling_mode = 0;
  1067. if (tz->trips.active[0].flags.valid
  1068. && tz->trips.passive.flags.valid) {
  1069. if (tz->trips.passive.temperature >
  1070. tz->trips.active[0].temperature)
  1071. tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
  1072. else
  1073. tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
  1074. } else if (!tz->trips.active[0].flags.valid
  1075. && tz->trips.passive.flags.valid) {
  1076. tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
  1077. } else if (tz->trips.active[0].flags.valid
  1078. && !tz->trips.passive.flags.valid) {
  1079. tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
  1080. } else {
  1081. /* _ACx and _PSV are optional, but _CRT is required */
  1082. tz->cooling_mode = ACPI_THERMAL_MODE_CRITICAL;
  1083. }
  1084. }
  1085. /* Get default polling frequency [_TZP] (optional) */
  1086. if (tzp)
  1087. tz->polling_frequency = tzp;
  1088. else
  1089. acpi_thermal_get_polling_frequency(tz);
  1090. /* Get devices in this thermal zone [_TZD] (optional) */
  1091. result = acpi_thermal_get_devices(tz);
  1092. if (!result)
  1093. tz->flags.devices = 1;
  1094. return_VALUE(0);
  1095. }
  1096. static int acpi_thermal_add(struct acpi_device *device)
  1097. {
  1098. int result = 0;
  1099. acpi_status status = AE_OK;
  1100. struct acpi_thermal *tz = NULL;
  1101. ACPI_FUNCTION_TRACE("acpi_thermal_add");
  1102. if (!device)
  1103. return_VALUE(-EINVAL);
  1104. tz = kmalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  1105. if (!tz)
  1106. return_VALUE(-ENOMEM);
  1107. memset(tz, 0, sizeof(struct acpi_thermal));
  1108. tz->handle = device->handle;
  1109. strcpy(tz->name, device->pnp.bus_id);
  1110. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  1111. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  1112. acpi_driver_data(device) = tz;
  1113. result = acpi_thermal_get_info(tz);
  1114. if (result)
  1115. goto end;
  1116. result = acpi_thermal_add_fs(device);
  1117. if (result)
  1118. goto end;
  1119. init_timer(&tz->timer);
  1120. acpi_thermal_check(tz);
  1121. status = acpi_install_notify_handler(tz->handle,
  1122. ACPI_DEVICE_NOTIFY,
  1123. acpi_thermal_notify, tz);
  1124. if (ACPI_FAILURE(status)) {
  1125. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1126. "Error installing notify handler\n"));
  1127. result = -ENODEV;
  1128. goto end;
  1129. }
  1130. printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
  1131. acpi_device_name(device), acpi_device_bid(device),
  1132. KELVIN_TO_CELSIUS(tz->temperature));
  1133. end:
  1134. if (result) {
  1135. acpi_thermal_remove_fs(device);
  1136. kfree(tz);
  1137. }
  1138. return_VALUE(result);
  1139. }
  1140. static int acpi_thermal_remove(struct acpi_device *device, int type)
  1141. {
  1142. acpi_status status = AE_OK;
  1143. struct acpi_thermal *tz = NULL;
  1144. ACPI_FUNCTION_TRACE("acpi_thermal_remove");
  1145. if (!device || !acpi_driver_data(device))
  1146. return_VALUE(-EINVAL);
  1147. tz = (struct acpi_thermal *)acpi_driver_data(device);
  1148. /* avoid timer adding new defer task */
  1149. tz->zombie = 1;
  1150. /* wait for running timer (on other CPUs) finish */
  1151. del_timer_sync(&(tz->timer));
  1152. /* synchronize deferred task */
  1153. acpi_os_wait_events_complete(NULL);
  1154. /* deferred task may reinsert timer */
  1155. del_timer_sync(&(tz->timer));
  1156. status = acpi_remove_notify_handler(tz->handle,
  1157. ACPI_DEVICE_NOTIFY,
  1158. acpi_thermal_notify);
  1159. if (ACPI_FAILURE(status))
  1160. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1161. "Error removing notify handler\n"));
  1162. /* Terminate policy */
  1163. if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
  1164. tz->trips.passive.flags.enabled = 0;
  1165. acpi_thermal_passive(tz);
  1166. }
  1167. if (tz->trips.active[0].flags.valid
  1168. && tz->trips.active[0].flags.enabled) {
  1169. tz->trips.active[0].flags.enabled = 0;
  1170. acpi_thermal_active(tz);
  1171. }
  1172. acpi_thermal_remove_fs(device);
  1173. kfree(tz);
  1174. return_VALUE(0);
  1175. }
  1176. static int acpi_thermal_resume(struct acpi_device *device, int state)
  1177. {
  1178. struct acpi_thermal *tz = NULL;
  1179. if (!device || !acpi_driver_data(device))
  1180. return_VALUE(-EINVAL);
  1181. tz = (struct acpi_thermal *)acpi_driver_data(device);
  1182. acpi_thermal_check(tz);
  1183. return AE_OK;
  1184. }
  1185. static int __init acpi_thermal_init(void)
  1186. {
  1187. int result = 0;
  1188. ACPI_FUNCTION_TRACE("acpi_thermal_init");
  1189. acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
  1190. if (!acpi_thermal_dir)
  1191. return_VALUE(-ENODEV);
  1192. acpi_thermal_dir->owner = THIS_MODULE;
  1193. result = acpi_bus_register_driver(&acpi_thermal_driver);
  1194. if (result < 0) {
  1195. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1196. return_VALUE(-ENODEV);
  1197. }
  1198. return_VALUE(0);
  1199. }
  1200. static void __exit acpi_thermal_exit(void)
  1201. {
  1202. ACPI_FUNCTION_TRACE("acpi_thermal_exit");
  1203. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1204. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1205. return_VOID;
  1206. }
  1207. module_init(acpi_thermal_init);
  1208. module_exit(acpi_thermal_exit);