thermal.c 36 KB

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