thermal.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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/dmi.h>
  36. #include <linux/init.h>
  37. #include <linux/types.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/jiffies.h>
  40. #include <linux/kmod.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/reboot.h>
  43. #include <linux/device.h>
  44. #include <asm/uaccess.h>
  45. #include <linux/thermal.h>
  46. #include <acpi/acpi_bus.h>
  47. #include <acpi/acpi_drivers.h>
  48. #define PREFIX "ACPI: "
  49. #define ACPI_THERMAL_CLASS "thermal_zone"
  50. #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
  51. #define ACPI_THERMAL_FILE_STATE "state"
  52. #define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
  53. #define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
  54. #define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
  55. #define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
  56. #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
  57. #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
  58. #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
  59. #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
  60. #define ACPI_THERMAL_NOTIFY_HOT 0xF1
  61. #define ACPI_THERMAL_MODE_ACTIVE 0x00
  62. #define ACPI_THERMAL_MAX_ACTIVE 10
  63. #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
  64. #define _COMPONENT ACPI_THERMAL_COMPONENT
  65. ACPI_MODULE_NAME("thermal");
  66. MODULE_AUTHOR("Paul Diefenbaugh");
  67. MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
  68. MODULE_LICENSE("GPL");
  69. static int act;
  70. module_param(act, int, 0644);
  71. MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
  72. static int crt;
  73. module_param(crt, int, 0644);
  74. MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
  75. static int tzp;
  76. module_param(tzp, int, 0444);
  77. MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
  78. static int nocrt;
  79. module_param(nocrt, int, 0);
  80. MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
  81. static int off;
  82. module_param(off, int, 0);
  83. MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
  84. static int psv;
  85. module_param(psv, int, 0644);
  86. MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
  87. static int acpi_thermal_add(struct acpi_device *device);
  88. static int acpi_thermal_remove(struct acpi_device *device, int type);
  89. static int acpi_thermal_resume(struct acpi_device *device);
  90. static void acpi_thermal_notify(struct acpi_device *device, u32 event);
  91. static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
  92. static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
  93. static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
  94. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
  95. static ssize_t acpi_thermal_write_cooling_mode(struct file *,
  96. const char __user *, size_t,
  97. loff_t *);
  98. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
  99. static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
  100. size_t, loff_t *);
  101. static const struct acpi_device_id thermal_device_ids[] = {
  102. {ACPI_THERMAL_HID, 0},
  103. {"", 0},
  104. };
  105. MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
  106. static struct acpi_driver acpi_thermal_driver = {
  107. .name = "thermal",
  108. .class = ACPI_THERMAL_CLASS,
  109. .ids = thermal_device_ids,
  110. .ops = {
  111. .add = acpi_thermal_add,
  112. .remove = acpi_thermal_remove,
  113. .resume = acpi_thermal_resume,
  114. .notify = acpi_thermal_notify,
  115. },
  116. };
  117. struct acpi_thermal_state {
  118. u8 critical:1;
  119. u8 hot:1;
  120. u8 passive:1;
  121. u8 active:1;
  122. u8 reserved:4;
  123. int active_index;
  124. };
  125. struct acpi_thermal_state_flags {
  126. u8 valid:1;
  127. u8 enabled:1;
  128. u8 reserved:6;
  129. };
  130. struct acpi_thermal_critical {
  131. struct acpi_thermal_state_flags flags;
  132. unsigned long temperature;
  133. };
  134. struct acpi_thermal_hot {
  135. struct acpi_thermal_state_flags flags;
  136. unsigned long temperature;
  137. };
  138. struct acpi_thermal_passive {
  139. struct acpi_thermal_state_flags flags;
  140. unsigned long temperature;
  141. unsigned long tc1;
  142. unsigned long tc2;
  143. unsigned long tsp;
  144. struct acpi_handle_list devices;
  145. };
  146. struct acpi_thermal_active {
  147. struct acpi_thermal_state_flags flags;
  148. unsigned long temperature;
  149. struct acpi_handle_list devices;
  150. };
  151. struct acpi_thermal_trips {
  152. struct acpi_thermal_critical critical;
  153. struct acpi_thermal_hot hot;
  154. struct acpi_thermal_passive passive;
  155. struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
  156. };
  157. struct acpi_thermal_flags {
  158. u8 cooling_mode:1; /* _SCP */
  159. u8 devices:1; /* _TZD */
  160. u8 reserved:6;
  161. };
  162. struct acpi_thermal {
  163. struct acpi_device * device;
  164. acpi_bus_id name;
  165. unsigned long temperature;
  166. unsigned long last_temperature;
  167. unsigned long polling_frequency;
  168. volatile u8 zombie;
  169. struct acpi_thermal_flags flags;
  170. struct acpi_thermal_state state;
  171. struct acpi_thermal_trips trips;
  172. struct acpi_handle_list devices;
  173. struct thermal_zone_device *thermal_zone;
  174. int tz_enabled;
  175. int kelvin_offset;
  176. struct mutex lock;
  177. };
  178. static const struct file_operations acpi_thermal_state_fops = {
  179. .owner = THIS_MODULE,
  180. .open = acpi_thermal_state_open_fs,
  181. .read = seq_read,
  182. .llseek = seq_lseek,
  183. .release = single_release,
  184. };
  185. static const struct file_operations acpi_thermal_temp_fops = {
  186. .owner = THIS_MODULE,
  187. .open = acpi_thermal_temp_open_fs,
  188. .read = seq_read,
  189. .llseek = seq_lseek,
  190. .release = single_release,
  191. };
  192. static const struct file_operations acpi_thermal_trip_fops = {
  193. .owner = THIS_MODULE,
  194. .open = acpi_thermal_trip_open_fs,
  195. .read = seq_read,
  196. .llseek = seq_lseek,
  197. .release = single_release,
  198. };
  199. static const struct file_operations acpi_thermal_cooling_fops = {
  200. .owner = THIS_MODULE,
  201. .open = acpi_thermal_cooling_open_fs,
  202. .read = seq_read,
  203. .write = acpi_thermal_write_cooling_mode,
  204. .llseek = seq_lseek,
  205. .release = single_release,
  206. };
  207. static const struct file_operations acpi_thermal_polling_fops = {
  208. .owner = THIS_MODULE,
  209. .open = acpi_thermal_polling_open_fs,
  210. .read = seq_read,
  211. .write = acpi_thermal_write_polling,
  212. .llseek = seq_lseek,
  213. .release = single_release,
  214. };
  215. /* --------------------------------------------------------------------------
  216. Thermal Zone Management
  217. -------------------------------------------------------------------------- */
  218. static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
  219. {
  220. acpi_status status = AE_OK;
  221. unsigned long long tmp;
  222. if (!tz)
  223. return -EINVAL;
  224. tz->last_temperature = tz->temperature;
  225. status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
  226. if (ACPI_FAILURE(status))
  227. return -ENODEV;
  228. tz->temperature = tmp;
  229. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
  230. tz->temperature));
  231. return 0;
  232. }
  233. static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
  234. {
  235. acpi_status status = AE_OK;
  236. unsigned long long tmp;
  237. if (!tz)
  238. return -EINVAL;
  239. status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
  240. if (ACPI_FAILURE(status))
  241. return -ENODEV;
  242. tz->polling_frequency = tmp;
  243. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
  244. tz->polling_frequency));
  245. return 0;
  246. }
  247. static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
  248. {
  249. if (!tz)
  250. return -EINVAL;
  251. tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
  252. tz->thermal_zone->polling_delay = seconds * 1000;
  253. if (tz->tz_enabled)
  254. thermal_zone_device_update(tz->thermal_zone);
  255. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  256. "Polling frequency set to %lu seconds\n",
  257. tz->polling_frequency/10));
  258. return 0;
  259. }
  260. static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
  261. {
  262. acpi_status status = AE_OK;
  263. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  264. struct acpi_object_list arg_list = { 1, &arg0 };
  265. acpi_handle handle = NULL;
  266. if (!tz)
  267. return -EINVAL;
  268. status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
  269. if (ACPI_FAILURE(status)) {
  270. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
  271. return -ENODEV;
  272. }
  273. arg0.integer.value = mode;
  274. status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
  275. if (ACPI_FAILURE(status))
  276. return -ENODEV;
  277. return 0;
  278. }
  279. #define ACPI_TRIPS_CRITICAL 0x01
  280. #define ACPI_TRIPS_HOT 0x02
  281. #define ACPI_TRIPS_PASSIVE 0x04
  282. #define ACPI_TRIPS_ACTIVE 0x08
  283. #define ACPI_TRIPS_DEVICES 0x10
  284. #define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
  285. #define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
  286. #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
  287. ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
  288. ACPI_TRIPS_DEVICES)
  289. /*
  290. * This exception is thrown out in two cases:
  291. * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
  292. * when re-evaluating the AML code.
  293. * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
  294. * We need to re-bind the cooling devices of a thermal zone when this occurs.
  295. */
  296. #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
  297. do { \
  298. if (flags != ACPI_TRIPS_INIT) \
  299. ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
  300. "ACPI thermal trip point %s changed\n" \
  301. "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
  302. } while (0)
  303. static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
  304. {
  305. acpi_status status = AE_OK;
  306. unsigned long long tmp;
  307. struct acpi_handle_list devices;
  308. int valid = 0;
  309. int i;
  310. /* Critical Shutdown */
  311. if (flag & ACPI_TRIPS_CRITICAL) {
  312. status = acpi_evaluate_integer(tz->device->handle,
  313. "_CRT", NULL, &tmp);
  314. tz->trips.critical.temperature = tmp;
  315. /*
  316. * Treat freezing temperatures as invalid as well; some
  317. * BIOSes return really low values and cause reboots at startup.
  318. * Below zero (Celsius) values clearly aren't right for sure..
  319. * ... so lets discard those as invalid.
  320. */
  321. if (ACPI_FAILURE(status)) {
  322. tz->trips.critical.flags.valid = 0;
  323. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  324. "No critical threshold\n"));
  325. } else if (tmp <= 2732) {
  326. printk(KERN_WARNING FW_BUG "Invalid critical threshold "
  327. "(%llu)\n", tmp);
  328. tz->trips.critical.flags.valid = 0;
  329. } else {
  330. tz->trips.critical.flags.valid = 1;
  331. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  332. "Found critical threshold [%lu]\n",
  333. tz->trips.critical.temperature));
  334. }
  335. if (tz->trips.critical.flags.valid == 1) {
  336. if (crt == -1) {
  337. tz->trips.critical.flags.valid = 0;
  338. } else if (crt > 0) {
  339. unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
  340. /*
  341. * Allow override critical threshold
  342. */
  343. if (crt_k > tz->trips.critical.temperature)
  344. printk(KERN_WARNING PREFIX
  345. "Critical threshold %d C\n", crt);
  346. tz->trips.critical.temperature = crt_k;
  347. }
  348. }
  349. }
  350. /* Critical Sleep (optional) */
  351. if (flag & ACPI_TRIPS_HOT) {
  352. status = acpi_evaluate_integer(tz->device->handle,
  353. "_HOT", NULL, &tmp);
  354. if (ACPI_FAILURE(status)) {
  355. tz->trips.hot.flags.valid = 0;
  356. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  357. "No hot threshold\n"));
  358. } else {
  359. tz->trips.hot.temperature = tmp;
  360. tz->trips.hot.flags.valid = 1;
  361. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  362. "Found hot threshold [%lu]\n",
  363. tz->trips.critical.temperature));
  364. }
  365. }
  366. /* Passive (optional) */
  367. if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
  368. (flag == ACPI_TRIPS_INIT)) {
  369. valid = tz->trips.passive.flags.valid;
  370. if (psv == -1) {
  371. status = AE_SUPPORT;
  372. } else if (psv > 0) {
  373. tmp = CELSIUS_TO_KELVIN(psv);
  374. status = AE_OK;
  375. } else {
  376. status = acpi_evaluate_integer(tz->device->handle,
  377. "_PSV", NULL, &tmp);
  378. }
  379. if (ACPI_FAILURE(status))
  380. tz->trips.passive.flags.valid = 0;
  381. else {
  382. tz->trips.passive.temperature = tmp;
  383. tz->trips.passive.flags.valid = 1;
  384. if (flag == ACPI_TRIPS_INIT) {
  385. status = acpi_evaluate_integer(
  386. tz->device->handle, "_TC1",
  387. NULL, &tmp);
  388. if (ACPI_FAILURE(status))
  389. tz->trips.passive.flags.valid = 0;
  390. else
  391. tz->trips.passive.tc1 = tmp;
  392. status = acpi_evaluate_integer(
  393. tz->device->handle, "_TC2",
  394. NULL, &tmp);
  395. if (ACPI_FAILURE(status))
  396. tz->trips.passive.flags.valid = 0;
  397. else
  398. tz->trips.passive.tc2 = tmp;
  399. status = acpi_evaluate_integer(
  400. tz->device->handle, "_TSP",
  401. NULL, &tmp);
  402. if (ACPI_FAILURE(status))
  403. tz->trips.passive.flags.valid = 0;
  404. else
  405. tz->trips.passive.tsp = tmp;
  406. }
  407. }
  408. }
  409. if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
  410. memset(&devices, 0, sizeof(struct acpi_handle_list));
  411. status = acpi_evaluate_reference(tz->device->handle, "_PSL",
  412. NULL, &devices);
  413. if (ACPI_FAILURE(status)) {
  414. printk(KERN_WARNING PREFIX
  415. "Invalid passive threshold\n");
  416. tz->trips.passive.flags.valid = 0;
  417. }
  418. else
  419. tz->trips.passive.flags.valid = 1;
  420. if (memcmp(&tz->trips.passive.devices, &devices,
  421. sizeof(struct acpi_handle_list))) {
  422. memcpy(&tz->trips.passive.devices, &devices,
  423. sizeof(struct acpi_handle_list));
  424. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  425. }
  426. }
  427. if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
  428. if (valid != tz->trips.passive.flags.valid)
  429. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
  430. }
  431. /* Active (optional) */
  432. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  433. char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
  434. valid = tz->trips.active[i].flags.valid;
  435. if (act == -1)
  436. break; /* disable all active trip points */
  437. if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
  438. tz->trips.active[i].flags.valid)) {
  439. status = acpi_evaluate_integer(tz->device->handle,
  440. name, NULL, &tmp);
  441. if (ACPI_FAILURE(status)) {
  442. tz->trips.active[i].flags.valid = 0;
  443. if (i == 0)
  444. break;
  445. if (act <= 0)
  446. break;
  447. if (i == 1)
  448. tz->trips.active[0].temperature =
  449. CELSIUS_TO_KELVIN(act);
  450. else
  451. /*
  452. * Don't allow override higher than
  453. * the next higher trip point
  454. */
  455. tz->trips.active[i - 1].temperature =
  456. (tz->trips.active[i - 2].temperature <
  457. CELSIUS_TO_KELVIN(act) ?
  458. tz->trips.active[i - 2].temperature :
  459. CELSIUS_TO_KELVIN(act));
  460. break;
  461. } else {
  462. tz->trips.active[i].temperature = tmp;
  463. tz->trips.active[i].flags.valid = 1;
  464. }
  465. }
  466. name[2] = 'L';
  467. if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
  468. memset(&devices, 0, sizeof(struct acpi_handle_list));
  469. status = acpi_evaluate_reference(tz->device->handle,
  470. name, NULL, &devices);
  471. if (ACPI_FAILURE(status)) {
  472. printk(KERN_WARNING PREFIX
  473. "Invalid active%d threshold\n", i);
  474. tz->trips.active[i].flags.valid = 0;
  475. }
  476. else
  477. tz->trips.active[i].flags.valid = 1;
  478. if (memcmp(&tz->trips.active[i].devices, &devices,
  479. sizeof(struct acpi_handle_list))) {
  480. memcpy(&tz->trips.active[i].devices, &devices,
  481. sizeof(struct acpi_handle_list));
  482. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  483. }
  484. }
  485. if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
  486. if (valid != tz->trips.active[i].flags.valid)
  487. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
  488. if (!tz->trips.active[i].flags.valid)
  489. break;
  490. }
  491. if (flag & ACPI_TRIPS_DEVICES) {
  492. memset(&devices, 0, sizeof(struct acpi_handle_list));
  493. status = acpi_evaluate_reference(tz->device->handle, "_TZD",
  494. NULL, &devices);
  495. if (memcmp(&tz->devices, &devices,
  496. sizeof(struct acpi_handle_list))) {
  497. memcpy(&tz->devices, &devices,
  498. sizeof(struct acpi_handle_list));
  499. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  500. }
  501. }
  502. return 0;
  503. }
  504. static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
  505. {
  506. return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
  507. }
  508. static void acpi_thermal_check(void *data)
  509. {
  510. struct acpi_thermal *tz = data;
  511. thermal_zone_device_update(tz->thermal_zone);
  512. }
  513. /* sys I/F for generic thermal sysfs support */
  514. #define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
  515. static int thermal_get_temp(struct thermal_zone_device *thermal,
  516. unsigned long *temp)
  517. {
  518. struct acpi_thermal *tz = thermal->devdata;
  519. int result;
  520. if (!tz)
  521. return -EINVAL;
  522. result = acpi_thermal_get_temperature(tz);
  523. if (result)
  524. return result;
  525. *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
  526. return 0;
  527. }
  528. static const char enabled[] = "kernel";
  529. static const char disabled[] = "user";
  530. static int thermal_get_mode(struct thermal_zone_device *thermal,
  531. enum thermal_device_mode *mode)
  532. {
  533. struct acpi_thermal *tz = thermal->devdata;
  534. if (!tz)
  535. return -EINVAL;
  536. *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
  537. THERMAL_DEVICE_DISABLED;
  538. return 0;
  539. }
  540. static int thermal_set_mode(struct thermal_zone_device *thermal,
  541. enum thermal_device_mode mode)
  542. {
  543. struct acpi_thermal *tz = thermal->devdata;
  544. int enable;
  545. if (!tz)
  546. return -EINVAL;
  547. /*
  548. * enable/disable thermal management from ACPI thermal driver
  549. */
  550. if (mode == THERMAL_DEVICE_ENABLED)
  551. enable = 1;
  552. else if (mode == THERMAL_DEVICE_DISABLED)
  553. enable = 0;
  554. else
  555. return -EINVAL;
  556. if (enable != tz->tz_enabled) {
  557. tz->tz_enabled = enable;
  558. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  559. "%s ACPI thermal control\n",
  560. tz->tz_enabled ? enabled : disabled));
  561. acpi_thermal_check(tz);
  562. }
  563. return 0;
  564. }
  565. static int thermal_get_trip_type(struct thermal_zone_device *thermal,
  566. int trip, enum thermal_trip_type *type)
  567. {
  568. struct acpi_thermal *tz = thermal->devdata;
  569. int i;
  570. if (!tz || trip < 0)
  571. return -EINVAL;
  572. if (tz->trips.critical.flags.valid) {
  573. if (!trip) {
  574. *type = THERMAL_TRIP_CRITICAL;
  575. return 0;
  576. }
  577. trip--;
  578. }
  579. if (tz->trips.hot.flags.valid) {
  580. if (!trip) {
  581. *type = THERMAL_TRIP_HOT;
  582. return 0;
  583. }
  584. trip--;
  585. }
  586. if (tz->trips.passive.flags.valid) {
  587. if (!trip) {
  588. *type = THERMAL_TRIP_PASSIVE;
  589. return 0;
  590. }
  591. trip--;
  592. }
  593. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  594. tz->trips.active[i].flags.valid; i++) {
  595. if (!trip) {
  596. *type = THERMAL_TRIP_ACTIVE;
  597. return 0;
  598. }
  599. trip--;
  600. }
  601. return -EINVAL;
  602. }
  603. static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
  604. int trip, unsigned long *temp)
  605. {
  606. struct acpi_thermal *tz = thermal->devdata;
  607. int i;
  608. if (!tz || trip < 0)
  609. return -EINVAL;
  610. if (tz->trips.critical.flags.valid) {
  611. if (!trip) {
  612. *temp = KELVIN_TO_MILLICELSIUS(
  613. tz->trips.critical.temperature,
  614. tz->kelvin_offset);
  615. return 0;
  616. }
  617. trip--;
  618. }
  619. if (tz->trips.hot.flags.valid) {
  620. if (!trip) {
  621. *temp = KELVIN_TO_MILLICELSIUS(
  622. tz->trips.hot.temperature,
  623. tz->kelvin_offset);
  624. return 0;
  625. }
  626. trip--;
  627. }
  628. if (tz->trips.passive.flags.valid) {
  629. if (!trip) {
  630. *temp = KELVIN_TO_MILLICELSIUS(
  631. tz->trips.passive.temperature,
  632. tz->kelvin_offset);
  633. return 0;
  634. }
  635. trip--;
  636. }
  637. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  638. tz->trips.active[i].flags.valid; i++) {
  639. if (!trip) {
  640. *temp = KELVIN_TO_MILLICELSIUS(
  641. tz->trips.active[i].temperature,
  642. tz->kelvin_offset);
  643. return 0;
  644. }
  645. trip--;
  646. }
  647. return -EINVAL;
  648. }
  649. static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
  650. unsigned long *temperature) {
  651. struct acpi_thermal *tz = thermal->devdata;
  652. if (tz->trips.critical.flags.valid) {
  653. *temperature = KELVIN_TO_MILLICELSIUS(
  654. tz->trips.critical.temperature,
  655. tz->kelvin_offset);
  656. return 0;
  657. } else
  658. return -EINVAL;
  659. }
  660. static int thermal_notify(struct thermal_zone_device *thermal, int trip,
  661. enum thermal_trip_type trip_type)
  662. {
  663. u8 type = 0;
  664. struct acpi_thermal *tz = thermal->devdata;
  665. if (trip_type == THERMAL_TRIP_CRITICAL)
  666. type = ACPI_THERMAL_NOTIFY_CRITICAL;
  667. else if (trip_type == THERMAL_TRIP_HOT)
  668. type = ACPI_THERMAL_NOTIFY_HOT;
  669. else
  670. return 0;
  671. acpi_bus_generate_proc_event(tz->device, type, 1);
  672. acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
  673. dev_name(&tz->device->dev), type, 1);
  674. if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
  675. return 1;
  676. return 0;
  677. }
  678. typedef int (*cb)(struct thermal_zone_device *, int,
  679. struct thermal_cooling_device *);
  680. static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
  681. struct thermal_cooling_device *cdev,
  682. cb action)
  683. {
  684. struct acpi_device *device = cdev->devdata;
  685. struct acpi_thermal *tz = thermal->devdata;
  686. struct acpi_device *dev;
  687. acpi_status status;
  688. acpi_handle handle;
  689. int i;
  690. int j;
  691. int trip = -1;
  692. int result = 0;
  693. if (tz->trips.critical.flags.valid)
  694. trip++;
  695. if (tz->trips.hot.flags.valid)
  696. trip++;
  697. if (tz->trips.passive.flags.valid) {
  698. trip++;
  699. for (i = 0; i < tz->trips.passive.devices.count;
  700. i++) {
  701. handle = tz->trips.passive.devices.handles[i];
  702. status = acpi_bus_get_device(handle, &dev);
  703. if (ACPI_SUCCESS(status) && (dev == device)) {
  704. result = action(thermal, trip, cdev);
  705. if (result)
  706. goto failed;
  707. }
  708. }
  709. }
  710. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  711. if (!tz->trips.active[i].flags.valid)
  712. break;
  713. trip++;
  714. for (j = 0;
  715. j < tz->trips.active[i].devices.count;
  716. j++) {
  717. handle = tz->trips.active[i].devices.handles[j];
  718. status = acpi_bus_get_device(handle, &dev);
  719. if (ACPI_SUCCESS(status) && (dev == device)) {
  720. result = action(thermal, trip, cdev);
  721. if (result)
  722. goto failed;
  723. }
  724. }
  725. }
  726. for (i = 0; i < tz->devices.count; i++) {
  727. handle = tz->devices.handles[i];
  728. status = acpi_bus_get_device(handle, &dev);
  729. if (ACPI_SUCCESS(status) && (dev == device)) {
  730. result = action(thermal, -1, cdev);
  731. if (result)
  732. goto failed;
  733. }
  734. }
  735. failed:
  736. return result;
  737. }
  738. static int
  739. acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
  740. struct thermal_cooling_device *cdev)
  741. {
  742. return acpi_thermal_cooling_device_cb(thermal, cdev,
  743. thermal_zone_bind_cooling_device);
  744. }
  745. static int
  746. acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
  747. struct thermal_cooling_device *cdev)
  748. {
  749. return acpi_thermal_cooling_device_cb(thermal, cdev,
  750. thermal_zone_unbind_cooling_device);
  751. }
  752. static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
  753. .bind = acpi_thermal_bind_cooling_device,
  754. .unbind = acpi_thermal_unbind_cooling_device,
  755. .get_temp = thermal_get_temp,
  756. .get_mode = thermal_get_mode,
  757. .set_mode = thermal_set_mode,
  758. .get_trip_type = thermal_get_trip_type,
  759. .get_trip_temp = thermal_get_trip_temp,
  760. .get_crit_temp = thermal_get_crit_temp,
  761. .notify = thermal_notify,
  762. };
  763. static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
  764. {
  765. int trips = 0;
  766. int result;
  767. acpi_status status;
  768. int i;
  769. if (tz->trips.critical.flags.valid)
  770. trips++;
  771. if (tz->trips.hot.flags.valid)
  772. trips++;
  773. if (tz->trips.passive.flags.valid)
  774. trips++;
  775. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  776. tz->trips.active[i].flags.valid; i++, trips++);
  777. if (tz->trips.passive.flags.valid)
  778. tz->thermal_zone =
  779. thermal_zone_device_register("acpitz", trips, tz,
  780. &acpi_thermal_zone_ops,
  781. tz->trips.passive.tc1,
  782. tz->trips.passive.tc2,
  783. tz->trips.passive.tsp*100,
  784. tz->polling_frequency*100);
  785. else
  786. tz->thermal_zone =
  787. thermal_zone_device_register("acpitz", trips, tz,
  788. &acpi_thermal_zone_ops,
  789. 0, 0, 0,
  790. tz->polling_frequency*100);
  791. if (IS_ERR(tz->thermal_zone))
  792. return -ENODEV;
  793. result = sysfs_create_link(&tz->device->dev.kobj,
  794. &tz->thermal_zone->device.kobj, "thermal_zone");
  795. if (result)
  796. return result;
  797. result = sysfs_create_link(&tz->thermal_zone->device.kobj,
  798. &tz->device->dev.kobj, "device");
  799. if (result)
  800. return result;
  801. status = acpi_attach_data(tz->device->handle,
  802. acpi_bus_private_data_handler,
  803. tz->thermal_zone);
  804. if (ACPI_FAILURE(status)) {
  805. printk(KERN_ERR PREFIX
  806. "Error attaching device data\n");
  807. return -ENODEV;
  808. }
  809. tz->tz_enabled = 1;
  810. dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
  811. tz->thermal_zone->id);
  812. return 0;
  813. }
  814. static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
  815. {
  816. sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
  817. sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
  818. thermal_zone_device_unregister(tz->thermal_zone);
  819. tz->thermal_zone = NULL;
  820. acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
  821. }
  822. /* --------------------------------------------------------------------------
  823. FS Interface (/proc)
  824. -------------------------------------------------------------------------- */
  825. static struct proc_dir_entry *acpi_thermal_dir;
  826. static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
  827. {
  828. struct acpi_thermal *tz = seq->private;
  829. if (!tz)
  830. goto end;
  831. seq_puts(seq, "state: ");
  832. if (!tz->state.critical && !tz->state.hot && !tz->state.passive
  833. && !tz->state.active)
  834. seq_puts(seq, "ok\n");
  835. else {
  836. if (tz->state.critical)
  837. seq_puts(seq, "critical ");
  838. if (tz->state.hot)
  839. seq_puts(seq, "hot ");
  840. if (tz->state.passive)
  841. seq_puts(seq, "passive ");
  842. if (tz->state.active)
  843. seq_printf(seq, "active[%d]", tz->state.active_index);
  844. seq_puts(seq, "\n");
  845. }
  846. end:
  847. return 0;
  848. }
  849. static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
  850. {
  851. return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
  852. }
  853. static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
  854. {
  855. int result = 0;
  856. struct acpi_thermal *tz = seq->private;
  857. if (!tz)
  858. goto end;
  859. result = acpi_thermal_get_temperature(tz);
  860. if (result)
  861. goto end;
  862. seq_printf(seq, "temperature: %ld C\n",
  863. KELVIN_TO_CELSIUS(tz->temperature));
  864. end:
  865. return 0;
  866. }
  867. static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
  868. {
  869. return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
  870. }
  871. static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
  872. {
  873. struct acpi_thermal *tz = seq->private;
  874. struct acpi_device *device;
  875. acpi_status status;
  876. int i = 0;
  877. int j = 0;
  878. if (!tz)
  879. goto end;
  880. if (tz->trips.critical.flags.valid)
  881. seq_printf(seq, "critical (S5): %ld C%s",
  882. KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
  883. nocrt ? " <disabled>\n" : "\n");
  884. if (tz->trips.hot.flags.valid)
  885. seq_printf(seq, "hot (S4): %ld C%s",
  886. KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
  887. nocrt ? " <disabled>\n" : "\n");
  888. if (tz->trips.passive.flags.valid) {
  889. seq_printf(seq,
  890. "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
  891. KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
  892. tz->trips.passive.tc1, tz->trips.passive.tc2,
  893. tz->trips.passive.tsp);
  894. for (j = 0; j < tz->trips.passive.devices.count; j++) {
  895. status = acpi_bus_get_device(tz->trips.passive.devices.
  896. handles[j], &device);
  897. seq_printf(seq, "%4.4s ", status ? "" :
  898. acpi_device_bid(device));
  899. }
  900. seq_puts(seq, "\n");
  901. } else {
  902. seq_printf(seq, "passive (forced):");
  903. if (tz->thermal_zone->forced_passive)
  904. seq_printf(seq, " %i C\n",
  905. tz->thermal_zone->forced_passive / 1000);
  906. else
  907. seq_printf(seq, "<not set>\n");
  908. }
  909. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  910. if (!(tz->trips.active[i].flags.valid))
  911. break;
  912. seq_printf(seq, "active[%d]: %ld C: devices=",
  913. i,
  914. KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
  915. for (j = 0; j < tz->trips.active[i].devices.count; j++){
  916. status = acpi_bus_get_device(tz->trips.active[i].
  917. devices.handles[j],
  918. &device);
  919. seq_printf(seq, "%4.4s ", status ? "" :
  920. acpi_device_bid(device));
  921. }
  922. seq_puts(seq, "\n");
  923. }
  924. end:
  925. return 0;
  926. }
  927. static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
  928. {
  929. return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
  930. }
  931. static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
  932. {
  933. struct acpi_thermal *tz = seq->private;
  934. if (!tz)
  935. goto end;
  936. if (!tz->flags.cooling_mode)
  937. seq_puts(seq, "<setting not supported>\n");
  938. else
  939. seq_puts(seq, "0 - Active; 1 - Passive\n");
  940. end:
  941. return 0;
  942. }
  943. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
  944. {
  945. return single_open(file, acpi_thermal_cooling_seq_show,
  946. PDE(inode)->data);
  947. }
  948. static ssize_t
  949. acpi_thermal_write_cooling_mode(struct file *file,
  950. const char __user * buffer,
  951. size_t count, loff_t * ppos)
  952. {
  953. struct seq_file *m = file->private_data;
  954. struct acpi_thermal *tz = m->private;
  955. int result = 0;
  956. char mode_string[12] = { '\0' };
  957. if (!tz || (count > sizeof(mode_string) - 1))
  958. return -EINVAL;
  959. if (!tz->flags.cooling_mode)
  960. return -ENODEV;
  961. if (copy_from_user(mode_string, buffer, count))
  962. return -EFAULT;
  963. mode_string[count] = '\0';
  964. result = acpi_thermal_set_cooling_mode(tz,
  965. simple_strtoul(mode_string, NULL,
  966. 0));
  967. if (result)
  968. return result;
  969. acpi_thermal_check(tz);
  970. return count;
  971. }
  972. static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
  973. {
  974. struct acpi_thermal *tz = seq->private;
  975. if (!tz)
  976. goto end;
  977. if (!tz->thermal_zone->polling_delay) {
  978. seq_puts(seq, "<polling disabled>\n");
  979. goto end;
  980. }
  981. seq_printf(seq, "polling frequency: %d seconds\n",
  982. (tz->thermal_zone->polling_delay / 1000));
  983. end:
  984. return 0;
  985. }
  986. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
  987. {
  988. return single_open(file, acpi_thermal_polling_seq_show,
  989. PDE(inode)->data);
  990. }
  991. static ssize_t
  992. acpi_thermal_write_polling(struct file *file,
  993. const char __user * buffer,
  994. size_t count, loff_t * ppos)
  995. {
  996. struct seq_file *m = file->private_data;
  997. struct acpi_thermal *tz = m->private;
  998. int result = 0;
  999. char polling_string[12] = { '\0' };
  1000. int seconds = 0;
  1001. if (!tz || (count > sizeof(polling_string) - 1))
  1002. return -EINVAL;
  1003. if (copy_from_user(polling_string, buffer, count))
  1004. return -EFAULT;
  1005. polling_string[count] = '\0';
  1006. seconds = simple_strtoul(polling_string, NULL, 0);
  1007. result = acpi_thermal_set_polling(tz, seconds);
  1008. if (result)
  1009. return result;
  1010. acpi_thermal_check(tz);
  1011. return count;
  1012. }
  1013. static int acpi_thermal_add_fs(struct acpi_device *device)
  1014. {
  1015. struct proc_dir_entry *entry = NULL;
  1016. if (!acpi_device_dir(device)) {
  1017. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  1018. acpi_thermal_dir);
  1019. if (!acpi_device_dir(device))
  1020. return -ENODEV;
  1021. }
  1022. /* 'state' [R] */
  1023. entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
  1024. S_IRUGO, acpi_device_dir(device),
  1025. &acpi_thermal_state_fops,
  1026. acpi_driver_data(device));
  1027. if (!entry)
  1028. return -ENODEV;
  1029. /* 'temperature' [R] */
  1030. entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
  1031. S_IRUGO, acpi_device_dir(device),
  1032. &acpi_thermal_temp_fops,
  1033. acpi_driver_data(device));
  1034. if (!entry)
  1035. return -ENODEV;
  1036. /* 'trip_points' [R] */
  1037. entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
  1038. S_IRUGO,
  1039. acpi_device_dir(device),
  1040. &acpi_thermal_trip_fops,
  1041. acpi_driver_data(device));
  1042. if (!entry)
  1043. return -ENODEV;
  1044. /* 'cooling_mode' [R/W] */
  1045. entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
  1046. S_IFREG | S_IRUGO | S_IWUSR,
  1047. acpi_device_dir(device),
  1048. &acpi_thermal_cooling_fops,
  1049. acpi_driver_data(device));
  1050. if (!entry)
  1051. return -ENODEV;
  1052. /* 'polling_frequency' [R/W] */
  1053. entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
  1054. S_IFREG | S_IRUGO | S_IWUSR,
  1055. acpi_device_dir(device),
  1056. &acpi_thermal_polling_fops,
  1057. acpi_driver_data(device));
  1058. if (!entry)
  1059. return -ENODEV;
  1060. return 0;
  1061. }
  1062. static int acpi_thermal_remove_fs(struct acpi_device *device)
  1063. {
  1064. if (acpi_device_dir(device)) {
  1065. remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  1066. acpi_device_dir(device));
  1067. remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  1068. acpi_device_dir(device));
  1069. remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  1070. acpi_device_dir(device));
  1071. remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  1072. acpi_device_dir(device));
  1073. remove_proc_entry(ACPI_THERMAL_FILE_STATE,
  1074. acpi_device_dir(device));
  1075. remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
  1076. acpi_device_dir(device) = NULL;
  1077. }
  1078. return 0;
  1079. }
  1080. /* --------------------------------------------------------------------------
  1081. Driver Interface
  1082. -------------------------------------------------------------------------- */
  1083. static void acpi_thermal_notify(struct acpi_device *device, u32 event)
  1084. {
  1085. struct acpi_thermal *tz = acpi_driver_data(device);
  1086. if (!tz)
  1087. return;
  1088. switch (event) {
  1089. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  1090. acpi_thermal_check(tz);
  1091. break;
  1092. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  1093. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
  1094. acpi_thermal_check(tz);
  1095. acpi_bus_generate_proc_event(device, event, 0);
  1096. acpi_bus_generate_netlink_event(device->pnp.device_class,
  1097. dev_name(&device->dev), event, 0);
  1098. break;
  1099. case ACPI_THERMAL_NOTIFY_DEVICES:
  1100. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
  1101. acpi_thermal_check(tz);
  1102. acpi_bus_generate_proc_event(device, event, 0);
  1103. acpi_bus_generate_netlink_event(device->pnp.device_class,
  1104. dev_name(&device->dev), event, 0);
  1105. break;
  1106. default:
  1107. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1108. "Unsupported event [0x%x]\n", event));
  1109. break;
  1110. }
  1111. }
  1112. static int acpi_thermal_get_info(struct acpi_thermal *tz)
  1113. {
  1114. int result = 0;
  1115. if (!tz)
  1116. return -EINVAL;
  1117. /* Get temperature [_TMP] (required) */
  1118. result = acpi_thermal_get_temperature(tz);
  1119. if (result)
  1120. return result;
  1121. /* Get trip points [_CRT, _PSV, etc.] (required) */
  1122. result = acpi_thermal_get_trip_points(tz);
  1123. if (result)
  1124. return result;
  1125. /* Set the cooling mode [_SCP] to active cooling (default) */
  1126. result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
  1127. if (!result)
  1128. tz->flags.cooling_mode = 1;
  1129. /* Get default polling frequency [_TZP] (optional) */
  1130. if (tzp)
  1131. tz->polling_frequency = tzp;
  1132. else
  1133. acpi_thermal_get_polling_frequency(tz);
  1134. return 0;
  1135. }
  1136. /*
  1137. * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
  1138. * handles temperature values with a single decimal place. As a consequence,
  1139. * some implementations use an offset of 273.1 and others use an offset of
  1140. * 273.2. Try to find out which one is being used, to present the most
  1141. * accurate and visually appealing number.
  1142. *
  1143. * The heuristic below should work for all ACPI thermal zones which have a
  1144. * critical trip point with a value being a multiple of 0.5 degree Celsius.
  1145. */
  1146. static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
  1147. {
  1148. if (tz->trips.critical.flags.valid &&
  1149. (tz->trips.critical.temperature % 5) == 1)
  1150. tz->kelvin_offset = 2731;
  1151. else
  1152. tz->kelvin_offset = 2732;
  1153. }
  1154. static int acpi_thermal_add(struct acpi_device *device)
  1155. {
  1156. int result = 0;
  1157. struct acpi_thermal *tz = NULL;
  1158. if (!device)
  1159. return -EINVAL;
  1160. tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  1161. if (!tz)
  1162. return -ENOMEM;
  1163. tz->device = device;
  1164. strcpy(tz->name, device->pnp.bus_id);
  1165. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  1166. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  1167. device->driver_data = tz;
  1168. mutex_init(&tz->lock);
  1169. result = acpi_thermal_get_info(tz);
  1170. if (result)
  1171. goto free_memory;
  1172. acpi_thermal_guess_offset(tz);
  1173. result = acpi_thermal_register_thermal_zone(tz);
  1174. if (result)
  1175. goto free_memory;
  1176. result = acpi_thermal_add_fs(device);
  1177. if (result)
  1178. goto unregister_thermal_zone;
  1179. printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
  1180. acpi_device_name(device), acpi_device_bid(device),
  1181. KELVIN_TO_CELSIUS(tz->temperature));
  1182. goto end;
  1183. unregister_thermal_zone:
  1184. thermal_zone_device_unregister(tz->thermal_zone);
  1185. free_memory:
  1186. kfree(tz);
  1187. end:
  1188. return result;
  1189. }
  1190. static int acpi_thermal_remove(struct acpi_device *device, int type)
  1191. {
  1192. struct acpi_thermal *tz = NULL;
  1193. if (!device || !acpi_driver_data(device))
  1194. return -EINVAL;
  1195. tz = acpi_driver_data(device);
  1196. acpi_thermal_remove_fs(device);
  1197. acpi_thermal_unregister_thermal_zone(tz);
  1198. mutex_destroy(&tz->lock);
  1199. kfree(tz);
  1200. return 0;
  1201. }
  1202. static int acpi_thermal_resume(struct acpi_device *device)
  1203. {
  1204. struct acpi_thermal *tz = NULL;
  1205. int i, j, power_state, result;
  1206. if (!device || !acpi_driver_data(device))
  1207. return -EINVAL;
  1208. tz = acpi_driver_data(device);
  1209. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  1210. if (!(&tz->trips.active[i]))
  1211. break;
  1212. if (!tz->trips.active[i].flags.valid)
  1213. break;
  1214. tz->trips.active[i].flags.enabled = 1;
  1215. for (j = 0; j < tz->trips.active[i].devices.count; j++) {
  1216. result = acpi_bus_get_power(tz->trips.active[i].devices.
  1217. handles[j], &power_state);
  1218. if (result || (power_state != ACPI_STATE_D0)) {
  1219. tz->trips.active[i].flags.enabled = 0;
  1220. break;
  1221. }
  1222. }
  1223. tz->state.active |= tz->trips.active[i].flags.enabled;
  1224. }
  1225. acpi_thermal_check(tz);
  1226. return AE_OK;
  1227. }
  1228. static int thermal_act(const struct dmi_system_id *d) {
  1229. if (act == 0) {
  1230. printk(KERN_NOTICE "ACPI: %s detected: "
  1231. "disabling all active thermal trip points\n", d->ident);
  1232. act = -1;
  1233. }
  1234. return 0;
  1235. }
  1236. static int thermal_nocrt(const struct dmi_system_id *d) {
  1237. printk(KERN_NOTICE "ACPI: %s detected: "
  1238. "disabling all critical thermal trip point actions.\n", d->ident);
  1239. nocrt = 1;
  1240. return 0;
  1241. }
  1242. static int thermal_tzp(const struct dmi_system_id *d) {
  1243. if (tzp == 0) {
  1244. printk(KERN_NOTICE "ACPI: %s detected: "
  1245. "enabling thermal zone polling\n", d->ident);
  1246. tzp = 300; /* 300 dS = 30 Seconds */
  1247. }
  1248. return 0;
  1249. }
  1250. static int thermal_psv(const struct dmi_system_id *d) {
  1251. if (psv == 0) {
  1252. printk(KERN_NOTICE "ACPI: %s detected: "
  1253. "disabling all passive thermal trip points\n", d->ident);
  1254. psv = -1;
  1255. }
  1256. return 0;
  1257. }
  1258. static struct dmi_system_id thermal_dmi_table[] __initdata = {
  1259. /*
  1260. * Award BIOS on this AOpen makes thermal control almost worthless.
  1261. * http://bugzilla.kernel.org/show_bug.cgi?id=8842
  1262. */
  1263. {
  1264. .callback = thermal_act,
  1265. .ident = "AOpen i915GMm-HFS",
  1266. .matches = {
  1267. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1268. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1269. },
  1270. },
  1271. {
  1272. .callback = thermal_psv,
  1273. .ident = "AOpen i915GMm-HFS",
  1274. .matches = {
  1275. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1276. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1277. },
  1278. },
  1279. {
  1280. .callback = thermal_tzp,
  1281. .ident = "AOpen i915GMm-HFS",
  1282. .matches = {
  1283. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1284. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1285. },
  1286. },
  1287. {
  1288. .callback = thermal_nocrt,
  1289. .ident = "Gigabyte GA-7ZX",
  1290. .matches = {
  1291. DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
  1292. DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
  1293. },
  1294. },
  1295. {}
  1296. };
  1297. static int __init acpi_thermal_init(void)
  1298. {
  1299. int result = 0;
  1300. dmi_check_system(thermal_dmi_table);
  1301. if (off) {
  1302. printk(KERN_NOTICE "ACPI: thermal control disabled\n");
  1303. return -ENODEV;
  1304. }
  1305. acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
  1306. if (!acpi_thermal_dir)
  1307. return -ENODEV;
  1308. result = acpi_bus_register_driver(&acpi_thermal_driver);
  1309. if (result < 0) {
  1310. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1311. return -ENODEV;
  1312. }
  1313. return 0;
  1314. }
  1315. static void __exit acpi_thermal_exit(void)
  1316. {
  1317. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1318. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1319. return;
  1320. }
  1321. module_init(acpi_thermal_init);
  1322. module_exit(acpi_thermal_exit);