thermal.c 38 KB

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