thermal.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  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_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
  576. }
  577. static void acpi_thermal_check(void *data)
  578. {
  579. int result = 0;
  580. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  581. unsigned long sleep_time = 0;
  582. int i = 0;
  583. struct acpi_thermal_state state;
  584. ACPI_FUNCTION_TRACE("acpi_thermal_check");
  585. if (!tz) {
  586. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
  587. return_VOID;
  588. }
  589. state = tz->state;
  590. result = acpi_thermal_get_temperature(tz);
  591. if (result)
  592. return_VOID;
  593. memset(&tz->state, 0, sizeof(tz->state));
  594. /*
  595. * Check Trip Points
  596. * -----------------
  597. * Compare the current temperature to the trip point values to see
  598. * if we've entered one of the thermal policy states. Note that
  599. * this function determines when a state is entered, but the
  600. * individual policy decides when it is exited (e.g. hysteresis).
  601. */
  602. if (tz->trips.critical.flags.valid)
  603. state.critical |=
  604. (tz->temperature >= tz->trips.critical.temperature);
  605. if (tz->trips.hot.flags.valid)
  606. state.hot |= (tz->temperature >= tz->trips.hot.temperature);
  607. if (tz->trips.passive.flags.valid)
  608. state.passive |=
  609. (tz->temperature >= tz->trips.passive.temperature);
  610. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  611. if (tz->trips.active[i].flags.valid)
  612. state.active |=
  613. (tz->temperature >=
  614. tz->trips.active[i].temperature);
  615. /*
  616. * Invoke Policy
  617. * -------------
  618. * Separated from the above check to allow individual policy to
  619. * determine when to exit a given state.
  620. */
  621. if (state.critical)
  622. acpi_thermal_critical(tz);
  623. if (state.hot)
  624. acpi_thermal_hot(tz);
  625. if (state.passive)
  626. acpi_thermal_passive(tz);
  627. if (state.active)
  628. acpi_thermal_active(tz);
  629. /*
  630. * Calculate State
  631. * ---------------
  632. * Again, separated from the above two to allow independent policy
  633. * decisions.
  634. */
  635. tz->state.critical = tz->trips.critical.flags.enabled;
  636. tz->state.hot = tz->trips.hot.flags.enabled;
  637. tz->state.passive = tz->trips.passive.flags.enabled;
  638. tz->state.active = 0;
  639. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  640. tz->state.active |= tz->trips.active[i].flags.enabled;
  641. /*
  642. * Calculate Sleep Time
  643. * --------------------
  644. * If we're in the passive state, use _TSP's value. Otherwise
  645. * use the default polling frequency (e.g. _TZP). If no polling
  646. * frequency is specified then we'll wait forever (at least until
  647. * a thermal event occurs). Note that _TSP and _TZD values are
  648. * given in 1/10th seconds (we must covert to milliseconds).
  649. */
  650. if (tz->state.passive)
  651. sleep_time = tz->trips.passive.tsp * 100;
  652. else if (tz->polling_frequency > 0)
  653. sleep_time = tz->polling_frequency * 100;
  654. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
  655. tz->name, tz->temperature, sleep_time));
  656. /*
  657. * Schedule Next Poll
  658. * ------------------
  659. */
  660. if (!sleep_time) {
  661. if (timer_pending(&(tz->timer)))
  662. del_timer(&(tz->timer));
  663. } else {
  664. if (timer_pending(&(tz->timer)))
  665. mod_timer(&(tz->timer), (HZ * sleep_time) / 1000);
  666. else {
  667. tz->timer.data = (unsigned long)tz;
  668. tz->timer.function = acpi_thermal_run;
  669. tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
  670. add_timer(&(tz->timer));
  671. }
  672. }
  673. return_VOID;
  674. }
  675. /* --------------------------------------------------------------------------
  676. FS Interface (/proc)
  677. -------------------------------------------------------------------------- */
  678. static struct proc_dir_entry *acpi_thermal_dir;
  679. static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
  680. {
  681. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  682. ACPI_FUNCTION_TRACE("acpi_thermal_state_seq_show");
  683. if (!tz)
  684. goto end;
  685. seq_puts(seq, "state: ");
  686. if (!tz->state.critical && !tz->state.hot && !tz->state.passive
  687. && !tz->state.active)
  688. seq_puts(seq, "ok\n");
  689. else {
  690. if (tz->state.critical)
  691. seq_puts(seq, "critical ");
  692. if (tz->state.hot)
  693. seq_puts(seq, "hot ");
  694. if (tz->state.passive)
  695. seq_puts(seq, "passive ");
  696. if (tz->state.active)
  697. seq_printf(seq, "active[%d]", tz->state.active_index);
  698. seq_puts(seq, "\n");
  699. }
  700. end:
  701. return_VALUE(0);
  702. }
  703. static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
  704. {
  705. return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
  706. }
  707. static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
  708. {
  709. int result = 0;
  710. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  711. ACPI_FUNCTION_TRACE("acpi_thermal_temp_seq_show");
  712. if (!tz)
  713. goto end;
  714. result = acpi_thermal_get_temperature(tz);
  715. if (result)
  716. goto end;
  717. seq_printf(seq, "temperature: %ld C\n",
  718. KELVIN_TO_CELSIUS(tz->temperature));
  719. end:
  720. return_VALUE(0);
  721. }
  722. static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
  723. {
  724. return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
  725. }
  726. static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
  727. {
  728. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  729. int i = 0;
  730. int j = 0;
  731. ACPI_FUNCTION_TRACE("acpi_thermal_trip_seq_show");
  732. if (!tz)
  733. goto end;
  734. if (tz->trips.critical.flags.valid)
  735. seq_printf(seq, "critical (S5): %ld C\n",
  736. KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
  737. if (tz->trips.hot.flags.valid)
  738. seq_printf(seq, "hot (S4): %ld C\n",
  739. KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
  740. if (tz->trips.passive.flags.valid) {
  741. seq_printf(seq,
  742. "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
  743. KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
  744. tz->trips.passive.tc1, tz->trips.passive.tc2,
  745. tz->trips.passive.tsp);
  746. for (j = 0; j < tz->trips.passive.devices.count; j++) {
  747. seq_printf(seq, "0x%p ",
  748. tz->trips.passive.devices.handles[j]);
  749. }
  750. seq_puts(seq, "\n");
  751. }
  752. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  753. if (!(tz->trips.active[i].flags.valid))
  754. break;
  755. seq_printf(seq, "active[%d]: %ld C: devices=",
  756. i,
  757. KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
  758. for (j = 0; j < tz->trips.active[i].devices.count; j++)
  759. seq_printf(seq, "0x%p ",
  760. tz->trips.active[i].devices.handles[j]);
  761. seq_puts(seq, "\n");
  762. }
  763. end:
  764. return_VALUE(0);
  765. }
  766. static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
  767. {
  768. return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
  769. }
  770. static ssize_t
  771. acpi_thermal_write_trip_points(struct file *file,
  772. const char __user * buffer,
  773. size_t count, loff_t * ppos)
  774. {
  775. struct seq_file *m = (struct seq_file *)file->private_data;
  776. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  777. char *limit_string;
  778. int num, critical, hot, passive;
  779. int *active;
  780. int i = 0;
  781. ACPI_FUNCTION_TRACE("acpi_thermal_write_trip_points");
  782. limit_string = kmalloc(ACPI_THERMAL_MAX_LIMIT_STR_LEN, GFP_KERNEL);
  783. if (!limit_string)
  784. return_VALUE(-ENOMEM);
  785. memset(limit_string, 0, ACPI_THERMAL_MAX_LIMIT_STR_LEN);
  786. active = kmalloc(ACPI_THERMAL_MAX_ACTIVE * sizeof(int), GFP_KERNEL);
  787. if (!active) {
  788. kfree(limit_string);
  789. return_VALUE(-ENOMEM);
  790. }
  791. if (!tz || (count > ACPI_THERMAL_MAX_LIMIT_STR_LEN - 1)) {
  792. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument\n"));
  793. count = -EINVAL;
  794. goto end;
  795. }
  796. if (copy_from_user(limit_string, buffer, count)) {
  797. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data\n"));
  798. count = -EFAULT;
  799. goto end;
  800. }
  801. limit_string[count] = '\0';
  802. num = sscanf(limit_string, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
  803. &critical, &hot, &passive,
  804. &active[0], &active[1], &active[2], &active[3], &active[4],
  805. &active[5], &active[6], &active[7], &active[8],
  806. &active[9]);
  807. if (!(num >= 5 && num < (ACPI_THERMAL_MAX_ACTIVE + 3))) {
  808. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data format\n"));
  809. count = -EINVAL;
  810. goto end;
  811. }
  812. tz->trips.critical.temperature = CELSIUS_TO_KELVIN(critical);
  813. tz->trips.hot.temperature = CELSIUS_TO_KELVIN(hot);
  814. tz->trips.passive.temperature = CELSIUS_TO_KELVIN(passive);
  815. for (i = 0; i < num - 3; i++) {
  816. if (!(tz->trips.active[i].flags.valid))
  817. break;
  818. tz->trips.active[i].temperature = CELSIUS_TO_KELVIN(active[i]);
  819. }
  820. end:
  821. kfree(active);
  822. kfree(limit_string);
  823. return_VALUE(count);
  824. }
  825. static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
  826. {
  827. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  828. ACPI_FUNCTION_TRACE("acpi_thermal_cooling_seq_show");
  829. if (!tz)
  830. goto end;
  831. if (!tz->flags.cooling_mode) {
  832. seq_puts(seq, "<setting not supported>\n");
  833. }
  834. if (tz->cooling_mode == ACPI_THERMAL_MODE_CRITICAL)
  835. seq_printf(seq, "cooling mode: critical\n");
  836. else
  837. seq_printf(seq, "cooling mode: %s\n",
  838. tz->cooling_mode ? "passive" : "active");
  839. end:
  840. return_VALUE(0);
  841. }
  842. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
  843. {
  844. return single_open(file, acpi_thermal_cooling_seq_show,
  845. PDE(inode)->data);
  846. }
  847. static ssize_t
  848. acpi_thermal_write_cooling_mode(struct file *file,
  849. const char __user * buffer,
  850. size_t count, loff_t * ppos)
  851. {
  852. struct seq_file *m = (struct seq_file *)file->private_data;
  853. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  854. int result = 0;
  855. char mode_string[12] = { '\0' };
  856. ACPI_FUNCTION_TRACE("acpi_thermal_write_cooling_mode");
  857. if (!tz || (count > sizeof(mode_string) - 1))
  858. return_VALUE(-EINVAL);
  859. if (!tz->flags.cooling_mode)
  860. return_VALUE(-ENODEV);
  861. if (copy_from_user(mode_string, buffer, count))
  862. return_VALUE(-EFAULT);
  863. mode_string[count] = '\0';
  864. result = acpi_thermal_set_cooling_mode(tz,
  865. simple_strtoul(mode_string, NULL,
  866. 0));
  867. if (result)
  868. return_VALUE(result);
  869. acpi_thermal_check(tz);
  870. return_VALUE(count);
  871. }
  872. static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
  873. {
  874. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  875. ACPI_FUNCTION_TRACE("acpi_thermal_polling_seq_show");
  876. if (!tz)
  877. goto end;
  878. if (!tz->polling_frequency) {
  879. seq_puts(seq, "<polling disabled>\n");
  880. goto end;
  881. }
  882. seq_printf(seq, "polling frequency: %lu seconds\n",
  883. (tz->polling_frequency / 10));
  884. end:
  885. return_VALUE(0);
  886. }
  887. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
  888. {
  889. return single_open(file, acpi_thermal_polling_seq_show,
  890. PDE(inode)->data);
  891. }
  892. static ssize_t
  893. acpi_thermal_write_polling(struct file *file,
  894. const char __user * buffer,
  895. size_t count, loff_t * ppos)
  896. {
  897. struct seq_file *m = (struct seq_file *)file->private_data;
  898. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  899. int result = 0;
  900. char polling_string[12] = { '\0' };
  901. int seconds = 0;
  902. ACPI_FUNCTION_TRACE("acpi_thermal_write_polling");
  903. if (!tz || (count > sizeof(polling_string) - 1))
  904. return_VALUE(-EINVAL);
  905. if (copy_from_user(polling_string, buffer, count))
  906. return_VALUE(-EFAULT);
  907. polling_string[count] = '\0';
  908. seconds = simple_strtoul(polling_string, NULL, 0);
  909. result = acpi_thermal_set_polling(tz, seconds);
  910. if (result)
  911. return_VALUE(result);
  912. acpi_thermal_check(tz);
  913. return_VALUE(count);
  914. }
  915. static int acpi_thermal_add_fs(struct acpi_device *device)
  916. {
  917. struct proc_dir_entry *entry = NULL;
  918. ACPI_FUNCTION_TRACE("acpi_thermal_add_fs");
  919. if (!acpi_device_dir(device)) {
  920. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  921. acpi_thermal_dir);
  922. if (!acpi_device_dir(device))
  923. return_VALUE(-ENODEV);
  924. acpi_device_dir(device)->owner = THIS_MODULE;
  925. }
  926. /* 'state' [R] */
  927. entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
  928. S_IRUGO, acpi_device_dir(device));
  929. if (!entry)
  930. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  931. "Unable to create '%s' fs entry\n",
  932. ACPI_THERMAL_FILE_STATE));
  933. else {
  934. entry->proc_fops = &acpi_thermal_state_fops;
  935. entry->data = acpi_driver_data(device);
  936. entry->owner = THIS_MODULE;
  937. }
  938. /* 'temperature' [R] */
  939. entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  940. S_IRUGO, acpi_device_dir(device));
  941. if (!entry)
  942. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  943. "Unable to create '%s' fs entry\n",
  944. ACPI_THERMAL_FILE_TEMPERATURE));
  945. else {
  946. entry->proc_fops = &acpi_thermal_temp_fops;
  947. entry->data = acpi_driver_data(device);
  948. entry->owner = THIS_MODULE;
  949. }
  950. /* 'trip_points' [R/W] */
  951. entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  952. S_IFREG | S_IRUGO | S_IWUSR,
  953. acpi_device_dir(device));
  954. if (!entry)
  955. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  956. "Unable to create '%s' fs entry\n",
  957. ACPI_THERMAL_FILE_TRIP_POINTS));
  958. else {
  959. entry->proc_fops = &acpi_thermal_trip_fops;
  960. entry->data = acpi_driver_data(device);
  961. entry->owner = THIS_MODULE;
  962. }
  963. /* 'cooling_mode' [R/W] */
  964. entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  965. S_IFREG | S_IRUGO | S_IWUSR,
  966. acpi_device_dir(device));
  967. if (!entry)
  968. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  969. "Unable to create '%s' fs entry\n",
  970. ACPI_THERMAL_FILE_COOLING_MODE));
  971. else {
  972. entry->proc_fops = &acpi_thermal_cooling_fops;
  973. entry->data = acpi_driver_data(device);
  974. entry->owner = THIS_MODULE;
  975. }
  976. /* 'polling_frequency' [R/W] */
  977. entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  978. S_IFREG | S_IRUGO | S_IWUSR,
  979. acpi_device_dir(device));
  980. if (!entry)
  981. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  982. "Unable to create '%s' fs entry\n",
  983. ACPI_THERMAL_FILE_POLLING_FREQ));
  984. else {
  985. entry->proc_fops = &acpi_thermal_polling_fops;
  986. entry->data = acpi_driver_data(device);
  987. entry->owner = THIS_MODULE;
  988. }
  989. return_VALUE(0);
  990. }
  991. static int acpi_thermal_remove_fs(struct acpi_device *device)
  992. {
  993. ACPI_FUNCTION_TRACE("acpi_thermal_remove_fs");
  994. if (acpi_device_dir(device)) {
  995. remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  996. acpi_device_dir(device));
  997. remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  998. acpi_device_dir(device));
  999. remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  1000. acpi_device_dir(device));
  1001. remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  1002. acpi_device_dir(device));
  1003. remove_proc_entry(ACPI_THERMAL_FILE_STATE,
  1004. acpi_device_dir(device));
  1005. remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
  1006. acpi_device_dir(device) = NULL;
  1007. }
  1008. return_VALUE(0);
  1009. }
  1010. /* --------------------------------------------------------------------------
  1011. Driver Interface
  1012. -------------------------------------------------------------------------- */
  1013. static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
  1014. {
  1015. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  1016. struct acpi_device *device = NULL;
  1017. ACPI_FUNCTION_TRACE("acpi_thermal_notify");
  1018. if (!tz)
  1019. return_VOID;
  1020. if (acpi_bus_get_device(tz->handle, &device))
  1021. return_VOID;
  1022. switch (event) {
  1023. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  1024. acpi_thermal_check(tz);
  1025. break;
  1026. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  1027. acpi_thermal_get_trip_points(tz);
  1028. acpi_thermal_check(tz);
  1029. acpi_bus_generate_event(device, event, 0);
  1030. break;
  1031. case ACPI_THERMAL_NOTIFY_DEVICES:
  1032. if (tz->flags.devices)
  1033. acpi_thermal_get_devices(tz);
  1034. acpi_bus_generate_event(device, event, 0);
  1035. break;
  1036. default:
  1037. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1038. "Unsupported event [0x%x]\n", event));
  1039. break;
  1040. }
  1041. return_VOID;
  1042. }
  1043. static int acpi_thermal_get_info(struct acpi_thermal *tz)
  1044. {
  1045. int result = 0;
  1046. ACPI_FUNCTION_TRACE("acpi_thermal_get_info");
  1047. if (!tz)
  1048. return_VALUE(-EINVAL);
  1049. /* Get temperature [_TMP] (required) */
  1050. result = acpi_thermal_get_temperature(tz);
  1051. if (result)
  1052. return_VALUE(result);
  1053. /* Get trip points [_CRT, _PSV, etc.] (required) */
  1054. result = acpi_thermal_get_trip_points(tz);
  1055. if (result)
  1056. return_VALUE(result);
  1057. /* Set the cooling mode [_SCP] to active cooling (default) */
  1058. result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
  1059. if (!result)
  1060. tz->flags.cooling_mode = 1;
  1061. else {
  1062. /* Oh,we have not _SCP method.
  1063. Generally show cooling_mode by _ACx, _PSV,spec 12.2 */
  1064. tz->flags.cooling_mode = 0;
  1065. if (tz->trips.active[0].flags.valid
  1066. && tz->trips.passive.flags.valid) {
  1067. if (tz->trips.passive.temperature >
  1068. tz->trips.active[0].temperature)
  1069. tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
  1070. else
  1071. tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
  1072. } else if (!tz->trips.active[0].flags.valid
  1073. && tz->trips.passive.flags.valid) {
  1074. tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
  1075. } else if (tz->trips.active[0].flags.valid
  1076. && !tz->trips.passive.flags.valid) {
  1077. tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
  1078. } else {
  1079. /* _ACx and _PSV are optional, but _CRT is required */
  1080. tz->cooling_mode = ACPI_THERMAL_MODE_CRITICAL;
  1081. }
  1082. }
  1083. /* Get default polling frequency [_TZP] (optional) */
  1084. if (tzp)
  1085. tz->polling_frequency = tzp;
  1086. else
  1087. acpi_thermal_get_polling_frequency(tz);
  1088. /* Get devices in this thermal zone [_TZD] (optional) */
  1089. result = acpi_thermal_get_devices(tz);
  1090. if (!result)
  1091. tz->flags.devices = 1;
  1092. return_VALUE(0);
  1093. }
  1094. static int acpi_thermal_add(struct acpi_device *device)
  1095. {
  1096. int result = 0;
  1097. acpi_status status = AE_OK;
  1098. struct acpi_thermal *tz = NULL;
  1099. ACPI_FUNCTION_TRACE("acpi_thermal_add");
  1100. if (!device)
  1101. return_VALUE(-EINVAL);
  1102. tz = kmalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  1103. if (!tz)
  1104. return_VALUE(-ENOMEM);
  1105. memset(tz, 0, sizeof(struct acpi_thermal));
  1106. tz->handle = device->handle;
  1107. strcpy(tz->name, device->pnp.bus_id);
  1108. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  1109. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  1110. acpi_driver_data(device) = tz;
  1111. result = acpi_thermal_get_info(tz);
  1112. if (result)
  1113. goto end;
  1114. result = acpi_thermal_add_fs(device);
  1115. if (result)
  1116. return_VALUE(result);
  1117. init_timer(&tz->timer);
  1118. acpi_thermal_check(tz);
  1119. status = acpi_install_notify_handler(tz->handle,
  1120. ACPI_DEVICE_NOTIFY,
  1121. acpi_thermal_notify, tz);
  1122. if (ACPI_FAILURE(status)) {
  1123. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1124. "Error installing notify handler\n"));
  1125. result = -ENODEV;
  1126. goto end;
  1127. }
  1128. printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
  1129. acpi_device_name(device), acpi_device_bid(device),
  1130. KELVIN_TO_CELSIUS(tz->temperature));
  1131. end:
  1132. if (result) {
  1133. acpi_thermal_remove_fs(device);
  1134. kfree(tz);
  1135. }
  1136. return_VALUE(result);
  1137. }
  1138. static int acpi_thermal_remove(struct acpi_device *device, int type)
  1139. {
  1140. acpi_status status = AE_OK;
  1141. struct acpi_thermal *tz = NULL;
  1142. ACPI_FUNCTION_TRACE("acpi_thermal_remove");
  1143. if (!device || !acpi_driver_data(device))
  1144. return_VALUE(-EINVAL);
  1145. tz = (struct acpi_thermal *)acpi_driver_data(device);
  1146. /* avoid timer adding new defer task */
  1147. tz->zombie = 1;
  1148. /* wait for running timer (on other CPUs) finish */
  1149. del_timer_sync(&(tz->timer));
  1150. /* synchronize deferred task */
  1151. acpi_os_wait_events_complete(NULL);
  1152. /* deferred task may reinsert timer */
  1153. del_timer_sync(&(tz->timer));
  1154. status = acpi_remove_notify_handler(tz->handle,
  1155. ACPI_DEVICE_NOTIFY,
  1156. acpi_thermal_notify);
  1157. if (ACPI_FAILURE(status))
  1158. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1159. "Error removing notify handler\n"));
  1160. /* Terminate policy */
  1161. if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
  1162. tz->trips.passive.flags.enabled = 0;
  1163. acpi_thermal_passive(tz);
  1164. }
  1165. if (tz->trips.active[0].flags.valid
  1166. && tz->trips.active[0].flags.enabled) {
  1167. tz->trips.active[0].flags.enabled = 0;
  1168. acpi_thermal_active(tz);
  1169. }
  1170. acpi_thermal_remove_fs(device);
  1171. kfree(tz);
  1172. return_VALUE(0);
  1173. }
  1174. static int __init acpi_thermal_init(void)
  1175. {
  1176. int result = 0;
  1177. ACPI_FUNCTION_TRACE("acpi_thermal_init");
  1178. acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
  1179. if (!acpi_thermal_dir)
  1180. return_VALUE(-ENODEV);
  1181. acpi_thermal_dir->owner = THIS_MODULE;
  1182. result = acpi_bus_register_driver(&acpi_thermal_driver);
  1183. if (result < 0) {
  1184. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1185. return_VALUE(-ENODEV);
  1186. }
  1187. return_VALUE(0);
  1188. }
  1189. static void __exit acpi_thermal_exit(void)
  1190. {
  1191. ACPI_FUNCTION_TRACE("acpi_thermal_exit");
  1192. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1193. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1194. return_VOID;
  1195. }
  1196. module_init(acpi_thermal_init);
  1197. module_exit(acpi_thermal_exit);