thermal.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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. return_VALUE(-ENOMEM);
  790. if (!tz || (count > ACPI_THERMAL_MAX_LIMIT_STR_LEN - 1)) {
  791. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument\n"));
  792. count = -EINVAL;
  793. goto end;
  794. }
  795. if (copy_from_user(limit_string, buffer, count)) {
  796. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data\n"));
  797. count = -EFAULT;
  798. goto end;
  799. }
  800. limit_string[count] = '\0';
  801. num = sscanf(limit_string, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
  802. &critical, &hot, &passive,
  803. &active[0], &active[1], &active[2], &active[3], &active[4],
  804. &active[5], &active[6], &active[7], &active[8],
  805. &active[9]);
  806. if (!(num >= 5 && num < (ACPI_THERMAL_MAX_ACTIVE + 3))) {
  807. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data format\n"));
  808. count = -EINVAL;
  809. goto end;
  810. }
  811. tz->trips.critical.temperature = CELSIUS_TO_KELVIN(critical);
  812. tz->trips.hot.temperature = CELSIUS_TO_KELVIN(hot);
  813. tz->trips.passive.temperature = CELSIUS_TO_KELVIN(passive);
  814. for (i = 0; i < num - 3; i++) {
  815. if (!(tz->trips.active[i].flags.valid))
  816. break;
  817. tz->trips.active[i].temperature = CELSIUS_TO_KELVIN(active[i]);
  818. }
  819. end:
  820. kfree(active);
  821. kfree(limit_string);
  822. return_VALUE(count);
  823. }
  824. static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
  825. {
  826. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  827. ACPI_FUNCTION_TRACE("acpi_thermal_cooling_seq_show");
  828. if (!tz)
  829. goto end;
  830. if (!tz->flags.cooling_mode) {
  831. seq_puts(seq, "<setting not supported>\n");
  832. }
  833. if (tz->cooling_mode == ACPI_THERMAL_MODE_CRITICAL)
  834. seq_printf(seq, "cooling mode: critical\n");
  835. else
  836. seq_printf(seq, "cooling mode: %s\n",
  837. tz->cooling_mode ? "passive" : "active");
  838. end:
  839. return_VALUE(0);
  840. }
  841. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
  842. {
  843. return single_open(file, acpi_thermal_cooling_seq_show,
  844. PDE(inode)->data);
  845. }
  846. static ssize_t
  847. acpi_thermal_write_cooling_mode(struct file *file,
  848. const char __user * buffer,
  849. size_t count, loff_t * ppos)
  850. {
  851. struct seq_file *m = (struct seq_file *)file->private_data;
  852. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  853. int result = 0;
  854. char mode_string[12] = { '\0' };
  855. ACPI_FUNCTION_TRACE("acpi_thermal_write_cooling_mode");
  856. if (!tz || (count > sizeof(mode_string) - 1))
  857. return_VALUE(-EINVAL);
  858. if (!tz->flags.cooling_mode)
  859. return_VALUE(-ENODEV);
  860. if (copy_from_user(mode_string, buffer, count))
  861. return_VALUE(-EFAULT);
  862. mode_string[count] = '\0';
  863. result = acpi_thermal_set_cooling_mode(tz,
  864. simple_strtoul(mode_string, NULL,
  865. 0));
  866. if (result)
  867. return_VALUE(result);
  868. acpi_thermal_check(tz);
  869. return_VALUE(count);
  870. }
  871. static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
  872. {
  873. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  874. ACPI_FUNCTION_TRACE("acpi_thermal_polling_seq_show");
  875. if (!tz)
  876. goto end;
  877. if (!tz->polling_frequency) {
  878. seq_puts(seq, "<polling disabled>\n");
  879. goto end;
  880. }
  881. seq_printf(seq, "polling frequency: %lu seconds\n",
  882. (tz->polling_frequency / 10));
  883. end:
  884. return_VALUE(0);
  885. }
  886. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
  887. {
  888. return single_open(file, acpi_thermal_polling_seq_show,
  889. PDE(inode)->data);
  890. }
  891. static ssize_t
  892. acpi_thermal_write_polling(struct file *file,
  893. const char __user * buffer,
  894. size_t count, loff_t * ppos)
  895. {
  896. struct seq_file *m = (struct seq_file *)file->private_data;
  897. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  898. int result = 0;
  899. char polling_string[12] = { '\0' };
  900. int seconds = 0;
  901. ACPI_FUNCTION_TRACE("acpi_thermal_write_polling");
  902. if (!tz || (count > sizeof(polling_string) - 1))
  903. return_VALUE(-EINVAL);
  904. if (copy_from_user(polling_string, buffer, count))
  905. return_VALUE(-EFAULT);
  906. polling_string[count] = '\0';
  907. seconds = simple_strtoul(polling_string, NULL, 0);
  908. result = acpi_thermal_set_polling(tz, seconds);
  909. if (result)
  910. return_VALUE(result);
  911. acpi_thermal_check(tz);
  912. return_VALUE(count);
  913. }
  914. static int acpi_thermal_add_fs(struct acpi_device *device)
  915. {
  916. struct proc_dir_entry *entry = NULL;
  917. ACPI_FUNCTION_TRACE("acpi_thermal_add_fs");
  918. if (!acpi_device_dir(device)) {
  919. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  920. acpi_thermal_dir);
  921. if (!acpi_device_dir(device))
  922. return_VALUE(-ENODEV);
  923. acpi_device_dir(device)->owner = THIS_MODULE;
  924. }
  925. /* 'state' [R] */
  926. entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
  927. S_IRUGO, acpi_device_dir(device));
  928. if (!entry)
  929. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  930. "Unable to create '%s' fs entry\n",
  931. ACPI_THERMAL_FILE_STATE));
  932. else {
  933. entry->proc_fops = &acpi_thermal_state_fops;
  934. entry->data = acpi_driver_data(device);
  935. entry->owner = THIS_MODULE;
  936. }
  937. /* 'temperature' [R] */
  938. entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  939. S_IRUGO, acpi_device_dir(device));
  940. if (!entry)
  941. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  942. "Unable to create '%s' fs entry\n",
  943. ACPI_THERMAL_FILE_TEMPERATURE));
  944. else {
  945. entry->proc_fops = &acpi_thermal_temp_fops;
  946. entry->data = acpi_driver_data(device);
  947. entry->owner = THIS_MODULE;
  948. }
  949. /* 'trip_points' [R/W] */
  950. entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  951. S_IFREG | S_IRUGO | S_IWUSR,
  952. acpi_device_dir(device));
  953. if (!entry)
  954. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  955. "Unable to create '%s' fs entry\n",
  956. ACPI_THERMAL_FILE_TRIP_POINTS));
  957. else {
  958. entry->proc_fops = &acpi_thermal_trip_fops;
  959. entry->data = acpi_driver_data(device);
  960. entry->owner = THIS_MODULE;
  961. }
  962. /* 'cooling_mode' [R/W] */
  963. entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  964. S_IFREG | S_IRUGO | S_IWUSR,
  965. acpi_device_dir(device));
  966. if (!entry)
  967. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  968. "Unable to create '%s' fs entry\n",
  969. ACPI_THERMAL_FILE_COOLING_MODE));
  970. else {
  971. entry->proc_fops = &acpi_thermal_cooling_fops;
  972. entry->data = acpi_driver_data(device);
  973. entry->owner = THIS_MODULE;
  974. }
  975. /* 'polling_frequency' [R/W] */
  976. entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  977. S_IFREG | S_IRUGO | S_IWUSR,
  978. acpi_device_dir(device));
  979. if (!entry)
  980. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  981. "Unable to create '%s' fs entry\n",
  982. ACPI_THERMAL_FILE_POLLING_FREQ));
  983. else {
  984. entry->proc_fops = &acpi_thermal_polling_fops;
  985. entry->data = acpi_driver_data(device);
  986. entry->owner = THIS_MODULE;
  987. }
  988. return_VALUE(0);
  989. }
  990. static int acpi_thermal_remove_fs(struct acpi_device *device)
  991. {
  992. ACPI_FUNCTION_TRACE("acpi_thermal_remove_fs");
  993. if (acpi_device_dir(device)) {
  994. remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  995. acpi_device_dir(device));
  996. remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  997. acpi_device_dir(device));
  998. remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  999. acpi_device_dir(device));
  1000. remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  1001. acpi_device_dir(device));
  1002. remove_proc_entry(ACPI_THERMAL_FILE_STATE,
  1003. acpi_device_dir(device));
  1004. remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
  1005. acpi_device_dir(device) = NULL;
  1006. }
  1007. return_VALUE(0);
  1008. }
  1009. /* --------------------------------------------------------------------------
  1010. Driver Interface
  1011. -------------------------------------------------------------------------- */
  1012. static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
  1013. {
  1014. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  1015. struct acpi_device *device = NULL;
  1016. ACPI_FUNCTION_TRACE("acpi_thermal_notify");
  1017. if (!tz)
  1018. return_VOID;
  1019. if (acpi_bus_get_device(tz->handle, &device))
  1020. return_VOID;
  1021. switch (event) {
  1022. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  1023. acpi_thermal_check(tz);
  1024. break;
  1025. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  1026. acpi_thermal_get_trip_points(tz);
  1027. acpi_thermal_check(tz);
  1028. acpi_bus_generate_event(device, event, 0);
  1029. break;
  1030. case ACPI_THERMAL_NOTIFY_DEVICES:
  1031. if (tz->flags.devices)
  1032. acpi_thermal_get_devices(tz);
  1033. acpi_bus_generate_event(device, event, 0);
  1034. break;
  1035. default:
  1036. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1037. "Unsupported event [0x%x]\n", event));
  1038. break;
  1039. }
  1040. return_VOID;
  1041. }
  1042. static int acpi_thermal_get_info(struct acpi_thermal *tz)
  1043. {
  1044. int result = 0;
  1045. ACPI_FUNCTION_TRACE("acpi_thermal_get_info");
  1046. if (!tz)
  1047. return_VALUE(-EINVAL);
  1048. /* Get temperature [_TMP] (required) */
  1049. result = acpi_thermal_get_temperature(tz);
  1050. if (result)
  1051. return_VALUE(result);
  1052. /* Get trip points [_CRT, _PSV, etc.] (required) */
  1053. result = acpi_thermal_get_trip_points(tz);
  1054. if (result)
  1055. return_VALUE(result);
  1056. /* Set the cooling mode [_SCP] to active cooling (default) */
  1057. result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
  1058. if (!result)
  1059. tz->flags.cooling_mode = 1;
  1060. else {
  1061. /* Oh,we have not _SCP method.
  1062. Generally show cooling_mode by _ACx, _PSV,spec 12.2 */
  1063. tz->flags.cooling_mode = 0;
  1064. if (tz->trips.active[0].flags.valid
  1065. && tz->trips.passive.flags.valid) {
  1066. if (tz->trips.passive.temperature >
  1067. tz->trips.active[0].temperature)
  1068. tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
  1069. else
  1070. tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
  1071. } else if (!tz->trips.active[0].flags.valid
  1072. && tz->trips.passive.flags.valid) {
  1073. tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
  1074. } else if (tz->trips.active[0].flags.valid
  1075. && !tz->trips.passive.flags.valid) {
  1076. tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
  1077. } else {
  1078. /* _ACx and _PSV are optional, but _CRT is required */
  1079. tz->cooling_mode = ACPI_THERMAL_MODE_CRITICAL;
  1080. }
  1081. }
  1082. /* Get default polling frequency [_TZP] (optional) */
  1083. if (tzp)
  1084. tz->polling_frequency = tzp;
  1085. else
  1086. acpi_thermal_get_polling_frequency(tz);
  1087. /* Get devices in this thermal zone [_TZD] (optional) */
  1088. result = acpi_thermal_get_devices(tz);
  1089. if (!result)
  1090. tz->flags.devices = 1;
  1091. return_VALUE(0);
  1092. }
  1093. static int acpi_thermal_add(struct acpi_device *device)
  1094. {
  1095. int result = 0;
  1096. acpi_status status = AE_OK;
  1097. struct acpi_thermal *tz = NULL;
  1098. ACPI_FUNCTION_TRACE("acpi_thermal_add");
  1099. if (!device)
  1100. return_VALUE(-EINVAL);
  1101. tz = kmalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  1102. if (!tz)
  1103. return_VALUE(-ENOMEM);
  1104. memset(tz, 0, sizeof(struct acpi_thermal));
  1105. tz->handle = device->handle;
  1106. strcpy(tz->name, device->pnp.bus_id);
  1107. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  1108. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  1109. acpi_driver_data(device) = tz;
  1110. result = acpi_thermal_get_info(tz);
  1111. if (result)
  1112. goto end;
  1113. result = acpi_thermal_add_fs(device);
  1114. if (result)
  1115. return_VALUE(result);
  1116. init_timer(&tz->timer);
  1117. acpi_thermal_check(tz);
  1118. status = acpi_install_notify_handler(tz->handle,
  1119. ACPI_DEVICE_NOTIFY,
  1120. acpi_thermal_notify, tz);
  1121. if (ACPI_FAILURE(status)) {
  1122. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1123. "Error installing notify handler\n"));
  1124. result = -ENODEV;
  1125. goto end;
  1126. }
  1127. printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
  1128. acpi_device_name(device), acpi_device_bid(device),
  1129. KELVIN_TO_CELSIUS(tz->temperature));
  1130. end:
  1131. if (result) {
  1132. acpi_thermal_remove_fs(device);
  1133. kfree(tz);
  1134. }
  1135. return_VALUE(result);
  1136. }
  1137. static int acpi_thermal_remove(struct acpi_device *device, int type)
  1138. {
  1139. acpi_status status = AE_OK;
  1140. struct acpi_thermal *tz = NULL;
  1141. ACPI_FUNCTION_TRACE("acpi_thermal_remove");
  1142. if (!device || !acpi_driver_data(device))
  1143. return_VALUE(-EINVAL);
  1144. tz = (struct acpi_thermal *)acpi_driver_data(device);
  1145. /* avoid timer adding new defer task */
  1146. tz->zombie = 1;
  1147. /* wait for running timer (on other CPUs) finish */
  1148. del_timer_sync(&(tz->timer));
  1149. /* synchronize deferred task */
  1150. acpi_os_wait_events_complete(NULL);
  1151. /* deferred task may reinsert timer */
  1152. del_timer_sync(&(tz->timer));
  1153. status = acpi_remove_notify_handler(tz->handle,
  1154. ACPI_DEVICE_NOTIFY,
  1155. acpi_thermal_notify);
  1156. if (ACPI_FAILURE(status))
  1157. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1158. "Error removing notify handler\n"));
  1159. /* Terminate policy */
  1160. if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
  1161. tz->trips.passive.flags.enabled = 0;
  1162. acpi_thermal_passive(tz);
  1163. }
  1164. if (tz->trips.active[0].flags.valid
  1165. && tz->trips.active[0].flags.enabled) {
  1166. tz->trips.active[0].flags.enabled = 0;
  1167. acpi_thermal_active(tz);
  1168. }
  1169. acpi_thermal_remove_fs(device);
  1170. kfree(tz);
  1171. return_VALUE(0);
  1172. }
  1173. static int __init acpi_thermal_init(void)
  1174. {
  1175. int result = 0;
  1176. ACPI_FUNCTION_TRACE("acpi_thermal_init");
  1177. acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
  1178. if (!acpi_thermal_dir)
  1179. return_VALUE(-ENODEV);
  1180. acpi_thermal_dir->owner = THIS_MODULE;
  1181. result = acpi_bus_register_driver(&acpi_thermal_driver);
  1182. if (result < 0) {
  1183. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1184. return_VALUE(-ENODEV);
  1185. }
  1186. return_VALUE(0);
  1187. }
  1188. static void __exit acpi_thermal_exit(void)
  1189. {
  1190. ACPI_FUNCTION_TRACE("acpi_thermal_exit");
  1191. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1192. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1193. return_VOID;
  1194. }
  1195. module_init(acpi_thermal_init);
  1196. module_exit(acpi_thermal_exit);