thermal.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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 (required) */
  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.temperature <= 2732) {
  323. tz->trips.critical.flags.valid = 0;
  324. ACPI_EXCEPTION((AE_INFO, status,
  325. "No or invalid critical threshold"));
  326. return -ENODEV;
  327. } else {
  328. tz->trips.critical.flags.valid = 1;
  329. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  330. "Found critical threshold [%lu]\n",
  331. tz->trips.critical.temperature));
  332. }
  333. if (tz->trips.critical.flags.valid == 1) {
  334. if (crt == -1) {
  335. tz->trips.critical.flags.valid = 0;
  336. } else if (crt > 0) {
  337. unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
  338. /*
  339. * Allow override critical threshold
  340. */
  341. if (crt_k > tz->trips.critical.temperature)
  342. printk(KERN_WARNING PREFIX
  343. "Critical threshold %d C\n", crt);
  344. tz->trips.critical.temperature = crt_k;
  345. }
  346. }
  347. }
  348. /* Critical Sleep (optional) */
  349. if (flag & ACPI_TRIPS_HOT) {
  350. status = acpi_evaluate_integer(tz->device->handle,
  351. "_HOT", NULL, &tmp);
  352. if (ACPI_FAILURE(status)) {
  353. tz->trips.hot.flags.valid = 0;
  354. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  355. "No hot threshold\n"));
  356. } else {
  357. tz->trips.hot.temperature = tmp;
  358. tz->trips.hot.flags.valid = 1;
  359. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  360. "Found hot threshold [%lu]\n",
  361. tz->trips.critical.temperature));
  362. }
  363. }
  364. /* Passive (optional) */
  365. if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
  366. (flag == ACPI_TRIPS_INIT)) {
  367. valid = tz->trips.passive.flags.valid;
  368. if (psv == -1) {
  369. status = AE_SUPPORT;
  370. } else if (psv > 0) {
  371. tmp = CELSIUS_TO_KELVIN(psv);
  372. status = AE_OK;
  373. } else {
  374. status = acpi_evaluate_integer(tz->device->handle,
  375. "_PSV", NULL, &tmp);
  376. }
  377. if (ACPI_FAILURE(status))
  378. tz->trips.passive.flags.valid = 0;
  379. else {
  380. tz->trips.passive.temperature = tmp;
  381. tz->trips.passive.flags.valid = 1;
  382. if (flag == ACPI_TRIPS_INIT) {
  383. status = acpi_evaluate_integer(
  384. tz->device->handle, "_TC1",
  385. NULL, &tmp);
  386. if (ACPI_FAILURE(status))
  387. tz->trips.passive.flags.valid = 0;
  388. else
  389. tz->trips.passive.tc1 = tmp;
  390. status = acpi_evaluate_integer(
  391. tz->device->handle, "_TC2",
  392. NULL, &tmp);
  393. if (ACPI_FAILURE(status))
  394. tz->trips.passive.flags.valid = 0;
  395. else
  396. tz->trips.passive.tc2 = tmp;
  397. status = acpi_evaluate_integer(
  398. tz->device->handle, "_TSP",
  399. NULL, &tmp);
  400. if (ACPI_FAILURE(status))
  401. tz->trips.passive.flags.valid = 0;
  402. else
  403. tz->trips.passive.tsp = tmp;
  404. }
  405. }
  406. }
  407. if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
  408. memset(&devices, 0, sizeof(struct acpi_handle_list));
  409. status = acpi_evaluate_reference(tz->device->handle, "_PSL",
  410. NULL, &devices);
  411. if (ACPI_FAILURE(status)) {
  412. printk(KERN_WARNING PREFIX
  413. "Invalid passive threshold\n");
  414. tz->trips.passive.flags.valid = 0;
  415. }
  416. else
  417. tz->trips.passive.flags.valid = 1;
  418. if (memcmp(&tz->trips.passive.devices, &devices,
  419. sizeof(struct acpi_handle_list))) {
  420. memcpy(&tz->trips.passive.devices, &devices,
  421. sizeof(struct acpi_handle_list));
  422. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  423. }
  424. }
  425. if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
  426. if (valid != tz->trips.passive.flags.valid)
  427. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
  428. }
  429. /* Active (optional) */
  430. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  431. char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
  432. valid = tz->trips.active[i].flags.valid;
  433. if (act == -1)
  434. break; /* disable all active trip points */
  435. if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
  436. tz->trips.active[i].flags.valid)) {
  437. status = acpi_evaluate_integer(tz->device->handle,
  438. name, NULL, &tmp);
  439. if (ACPI_FAILURE(status)) {
  440. tz->trips.active[i].flags.valid = 0;
  441. if (i == 0)
  442. break;
  443. if (act <= 0)
  444. break;
  445. if (i == 1)
  446. tz->trips.active[0].temperature =
  447. CELSIUS_TO_KELVIN(act);
  448. else
  449. /*
  450. * Don't allow override higher than
  451. * the next higher trip point
  452. */
  453. tz->trips.active[i - 1].temperature =
  454. (tz->trips.active[i - 2].temperature <
  455. CELSIUS_TO_KELVIN(act) ?
  456. tz->trips.active[i - 2].temperature :
  457. CELSIUS_TO_KELVIN(act));
  458. break;
  459. } else {
  460. tz->trips.active[i].temperature = tmp;
  461. tz->trips.active[i].flags.valid = 1;
  462. }
  463. }
  464. name[2] = 'L';
  465. if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
  466. memset(&devices, 0, sizeof(struct acpi_handle_list));
  467. status = acpi_evaluate_reference(tz->device->handle,
  468. name, NULL, &devices);
  469. if (ACPI_FAILURE(status)) {
  470. printk(KERN_WARNING PREFIX
  471. "Invalid active%d threshold\n", i);
  472. tz->trips.active[i].flags.valid = 0;
  473. }
  474. else
  475. tz->trips.active[i].flags.valid = 1;
  476. if (memcmp(&tz->trips.active[i].devices, &devices,
  477. sizeof(struct acpi_handle_list))) {
  478. memcpy(&tz->trips.active[i].devices, &devices,
  479. sizeof(struct acpi_handle_list));
  480. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  481. }
  482. }
  483. if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
  484. if (valid != tz->trips.active[i].flags.valid)
  485. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
  486. if (!tz->trips.active[i].flags.valid)
  487. break;
  488. }
  489. if (flag & ACPI_TRIPS_DEVICES) {
  490. memset(&devices, 0, sizeof(struct acpi_handle_list));
  491. status = acpi_evaluate_reference(tz->device->handle, "_TZD",
  492. NULL, &devices);
  493. if (memcmp(&tz->devices, &devices,
  494. sizeof(struct acpi_handle_list))) {
  495. memcpy(&tz->devices, &devices,
  496. sizeof(struct acpi_handle_list));
  497. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  498. }
  499. }
  500. return 0;
  501. }
  502. static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
  503. {
  504. return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
  505. }
  506. static void acpi_thermal_check(void *data)
  507. {
  508. struct acpi_thermal *tz = data;
  509. thermal_zone_device_update(tz->thermal_zone);
  510. }
  511. /* sys I/F for generic thermal sysfs support */
  512. #define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
  513. static int thermal_get_temp(struct thermal_zone_device *thermal,
  514. unsigned long *temp)
  515. {
  516. struct acpi_thermal *tz = thermal->devdata;
  517. int result;
  518. if (!tz)
  519. return -EINVAL;
  520. result = acpi_thermal_get_temperature(tz);
  521. if (result)
  522. return result;
  523. *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
  524. return 0;
  525. }
  526. static const char enabled[] = "kernel";
  527. static const char disabled[] = "user";
  528. static int thermal_get_mode(struct thermal_zone_device *thermal,
  529. enum thermal_device_mode *mode)
  530. {
  531. struct acpi_thermal *tz = thermal->devdata;
  532. if (!tz)
  533. return -EINVAL;
  534. *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
  535. THERMAL_DEVICE_DISABLED;
  536. return 0;
  537. }
  538. static int thermal_set_mode(struct thermal_zone_device *thermal,
  539. enum thermal_device_mode mode)
  540. {
  541. struct acpi_thermal *tz = thermal->devdata;
  542. int enable;
  543. if (!tz)
  544. return -EINVAL;
  545. /*
  546. * enable/disable thermal management from ACPI thermal driver
  547. */
  548. if (mode == THERMAL_DEVICE_ENABLED)
  549. enable = 1;
  550. else if (mode == THERMAL_DEVICE_DISABLED)
  551. enable = 0;
  552. else
  553. return -EINVAL;
  554. if (enable != tz->tz_enabled) {
  555. tz->tz_enabled = enable;
  556. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  557. "%s ACPI thermal control\n",
  558. tz->tz_enabled ? enabled : disabled));
  559. acpi_thermal_check(tz);
  560. }
  561. return 0;
  562. }
  563. static int thermal_get_trip_type(struct thermal_zone_device *thermal,
  564. int trip, enum thermal_trip_type *type)
  565. {
  566. struct acpi_thermal *tz = thermal->devdata;
  567. int i;
  568. if (!tz || trip < 0)
  569. return -EINVAL;
  570. if (tz->trips.critical.flags.valid) {
  571. if (!trip) {
  572. *type = THERMAL_TRIP_CRITICAL;
  573. return 0;
  574. }
  575. trip--;
  576. }
  577. if (tz->trips.hot.flags.valid) {
  578. if (!trip) {
  579. *type = THERMAL_TRIP_HOT;
  580. return 0;
  581. }
  582. trip--;
  583. }
  584. if (tz->trips.passive.flags.valid) {
  585. if (!trip) {
  586. *type = THERMAL_TRIP_PASSIVE;
  587. return 0;
  588. }
  589. trip--;
  590. }
  591. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  592. tz->trips.active[i].flags.valid; i++) {
  593. if (!trip) {
  594. *type = THERMAL_TRIP_ACTIVE;
  595. return 0;
  596. }
  597. trip--;
  598. }
  599. return -EINVAL;
  600. }
  601. static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
  602. int trip, unsigned long *temp)
  603. {
  604. struct acpi_thermal *tz = thermal->devdata;
  605. int i;
  606. if (!tz || trip < 0)
  607. return -EINVAL;
  608. if (tz->trips.critical.flags.valid) {
  609. if (!trip) {
  610. *temp = KELVIN_TO_MILLICELSIUS(
  611. tz->trips.critical.temperature,
  612. tz->kelvin_offset);
  613. return 0;
  614. }
  615. trip--;
  616. }
  617. if (tz->trips.hot.flags.valid) {
  618. if (!trip) {
  619. *temp = KELVIN_TO_MILLICELSIUS(
  620. tz->trips.hot.temperature,
  621. tz->kelvin_offset);
  622. return 0;
  623. }
  624. trip--;
  625. }
  626. if (tz->trips.passive.flags.valid) {
  627. if (!trip) {
  628. *temp = KELVIN_TO_MILLICELSIUS(
  629. tz->trips.passive.temperature,
  630. tz->kelvin_offset);
  631. return 0;
  632. }
  633. trip--;
  634. }
  635. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  636. tz->trips.active[i].flags.valid; i++) {
  637. if (!trip) {
  638. *temp = KELVIN_TO_MILLICELSIUS(
  639. tz->trips.active[i].temperature,
  640. tz->kelvin_offset);
  641. return 0;
  642. }
  643. trip--;
  644. }
  645. return -EINVAL;
  646. }
  647. static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
  648. unsigned long *temperature) {
  649. struct acpi_thermal *tz = thermal->devdata;
  650. if (tz->trips.critical.flags.valid) {
  651. *temperature = KELVIN_TO_MILLICELSIUS(
  652. tz->trips.critical.temperature,
  653. tz->kelvin_offset);
  654. return 0;
  655. } else
  656. return -EINVAL;
  657. }
  658. static int thermal_notify(struct thermal_zone_device *thermal, int trip,
  659. enum thermal_trip_type trip_type)
  660. {
  661. u8 type = 0;
  662. struct acpi_thermal *tz = thermal->devdata;
  663. if (trip_type == THERMAL_TRIP_CRITICAL)
  664. type = ACPI_THERMAL_NOTIFY_CRITICAL;
  665. else if (trip_type == THERMAL_TRIP_HOT)
  666. type = ACPI_THERMAL_NOTIFY_HOT;
  667. else
  668. return 0;
  669. acpi_bus_generate_proc_event(tz->device, type, 1);
  670. acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
  671. dev_name(&tz->device->dev), type, 1);
  672. if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
  673. return 1;
  674. return 0;
  675. }
  676. typedef int (*cb)(struct thermal_zone_device *, int,
  677. struct thermal_cooling_device *);
  678. static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
  679. struct thermal_cooling_device *cdev,
  680. cb action)
  681. {
  682. struct acpi_device *device = cdev->devdata;
  683. struct acpi_thermal *tz = thermal->devdata;
  684. struct acpi_device *dev;
  685. acpi_status status;
  686. acpi_handle handle;
  687. int i;
  688. int j;
  689. int trip = -1;
  690. int result = 0;
  691. if (tz->trips.critical.flags.valid)
  692. trip++;
  693. if (tz->trips.hot.flags.valid)
  694. trip++;
  695. if (tz->trips.passive.flags.valid) {
  696. trip++;
  697. for (i = 0; i < tz->trips.passive.devices.count;
  698. i++) {
  699. handle = tz->trips.passive.devices.handles[i];
  700. status = acpi_bus_get_device(handle, &dev);
  701. if (ACPI_SUCCESS(status) && (dev == device)) {
  702. result = action(thermal, trip, cdev);
  703. if (result)
  704. goto failed;
  705. }
  706. }
  707. }
  708. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  709. if (!tz->trips.active[i].flags.valid)
  710. break;
  711. trip++;
  712. for (j = 0;
  713. j < tz->trips.active[i].devices.count;
  714. j++) {
  715. handle = tz->trips.active[i].devices.handles[j];
  716. status = acpi_bus_get_device(handle, &dev);
  717. if (ACPI_SUCCESS(status) && (dev == device)) {
  718. result = action(thermal, trip, cdev);
  719. if (result)
  720. goto failed;
  721. }
  722. }
  723. }
  724. for (i = 0; i < tz->devices.count; i++) {
  725. handle = tz->devices.handles[i];
  726. status = acpi_bus_get_device(handle, &dev);
  727. if (ACPI_SUCCESS(status) && (dev == device)) {
  728. result = action(thermal, -1, cdev);
  729. if (result)
  730. goto failed;
  731. }
  732. }
  733. failed:
  734. return result;
  735. }
  736. static int
  737. acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
  738. struct thermal_cooling_device *cdev)
  739. {
  740. return acpi_thermal_cooling_device_cb(thermal, cdev,
  741. thermal_zone_bind_cooling_device);
  742. }
  743. static int
  744. acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
  745. struct thermal_cooling_device *cdev)
  746. {
  747. return acpi_thermal_cooling_device_cb(thermal, cdev,
  748. thermal_zone_unbind_cooling_device);
  749. }
  750. static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
  751. .bind = acpi_thermal_bind_cooling_device,
  752. .unbind = acpi_thermal_unbind_cooling_device,
  753. .get_temp = thermal_get_temp,
  754. .get_mode = thermal_get_mode,
  755. .set_mode = thermal_set_mode,
  756. .get_trip_type = thermal_get_trip_type,
  757. .get_trip_temp = thermal_get_trip_temp,
  758. .get_crit_temp = thermal_get_crit_temp,
  759. .notify = thermal_notify,
  760. };
  761. static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
  762. {
  763. int trips = 0;
  764. int result;
  765. acpi_status status;
  766. int i;
  767. if (tz->trips.critical.flags.valid)
  768. trips++;
  769. if (tz->trips.hot.flags.valid)
  770. trips++;
  771. if (tz->trips.passive.flags.valid)
  772. trips++;
  773. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  774. tz->trips.active[i].flags.valid; i++, trips++);
  775. if (tz->trips.passive.flags.valid)
  776. tz->thermal_zone =
  777. thermal_zone_device_register("acpitz", trips, tz,
  778. &acpi_thermal_zone_ops,
  779. tz->trips.passive.tc1,
  780. tz->trips.passive.tc2,
  781. tz->trips.passive.tsp*100,
  782. tz->polling_frequency*100);
  783. else
  784. tz->thermal_zone =
  785. thermal_zone_device_register("acpitz", trips, tz,
  786. &acpi_thermal_zone_ops,
  787. 0, 0, 0,
  788. tz->polling_frequency*100);
  789. if (IS_ERR(tz->thermal_zone))
  790. return -ENODEV;
  791. result = sysfs_create_link(&tz->device->dev.kobj,
  792. &tz->thermal_zone->device.kobj, "thermal_zone");
  793. if (result)
  794. return result;
  795. result = sysfs_create_link(&tz->thermal_zone->device.kobj,
  796. &tz->device->dev.kobj, "device");
  797. if (result)
  798. return result;
  799. status = acpi_attach_data(tz->device->handle,
  800. acpi_bus_private_data_handler,
  801. tz->thermal_zone);
  802. if (ACPI_FAILURE(status)) {
  803. printk(KERN_ERR PREFIX
  804. "Error attaching device data\n");
  805. return -ENODEV;
  806. }
  807. tz->tz_enabled = 1;
  808. dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
  809. tz->thermal_zone->id);
  810. return 0;
  811. }
  812. static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
  813. {
  814. sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
  815. sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
  816. thermal_zone_device_unregister(tz->thermal_zone);
  817. tz->thermal_zone = NULL;
  818. acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
  819. }
  820. /* --------------------------------------------------------------------------
  821. FS Interface (/proc)
  822. -------------------------------------------------------------------------- */
  823. static struct proc_dir_entry *acpi_thermal_dir;
  824. static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
  825. {
  826. struct acpi_thermal *tz = seq->private;
  827. if (!tz)
  828. goto end;
  829. seq_puts(seq, "state: ");
  830. if (!tz->state.critical && !tz->state.hot && !tz->state.passive
  831. && !tz->state.active)
  832. seq_puts(seq, "ok\n");
  833. else {
  834. if (tz->state.critical)
  835. seq_puts(seq, "critical ");
  836. if (tz->state.hot)
  837. seq_puts(seq, "hot ");
  838. if (tz->state.passive)
  839. seq_puts(seq, "passive ");
  840. if (tz->state.active)
  841. seq_printf(seq, "active[%d]", tz->state.active_index);
  842. seq_puts(seq, "\n");
  843. }
  844. end:
  845. return 0;
  846. }
  847. static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
  848. {
  849. return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
  850. }
  851. static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
  852. {
  853. int result = 0;
  854. struct acpi_thermal *tz = seq->private;
  855. if (!tz)
  856. goto end;
  857. result = acpi_thermal_get_temperature(tz);
  858. if (result)
  859. goto end;
  860. seq_printf(seq, "temperature: %ld C\n",
  861. KELVIN_TO_CELSIUS(tz->temperature));
  862. end:
  863. return 0;
  864. }
  865. static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
  866. {
  867. return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
  868. }
  869. static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
  870. {
  871. struct acpi_thermal *tz = seq->private;
  872. struct acpi_device *device;
  873. acpi_status status;
  874. int i = 0;
  875. int j = 0;
  876. if (!tz)
  877. goto end;
  878. if (tz->trips.critical.flags.valid)
  879. seq_printf(seq, "critical (S5): %ld C%s",
  880. KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
  881. nocrt ? " <disabled>\n" : "\n");
  882. if (tz->trips.hot.flags.valid)
  883. seq_printf(seq, "hot (S4): %ld C%s",
  884. KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
  885. nocrt ? " <disabled>\n" : "\n");
  886. if (tz->trips.passive.flags.valid) {
  887. seq_printf(seq,
  888. "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
  889. KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
  890. tz->trips.passive.tc1, tz->trips.passive.tc2,
  891. tz->trips.passive.tsp);
  892. for (j = 0; j < tz->trips.passive.devices.count; j++) {
  893. status = acpi_bus_get_device(tz->trips.passive.devices.
  894. handles[j], &device);
  895. seq_printf(seq, "%4.4s ", status ? "" :
  896. acpi_device_bid(device));
  897. }
  898. seq_puts(seq, "\n");
  899. }
  900. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  901. if (!(tz->trips.active[i].flags.valid))
  902. break;
  903. seq_printf(seq, "active[%d]: %ld C: devices=",
  904. i,
  905. KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
  906. for (j = 0; j < tz->trips.active[i].devices.count; j++){
  907. status = acpi_bus_get_device(tz->trips.active[i].
  908. devices.handles[j],
  909. &device);
  910. seq_printf(seq, "%4.4s ", status ? "" :
  911. acpi_device_bid(device));
  912. }
  913. seq_puts(seq, "\n");
  914. }
  915. end:
  916. return 0;
  917. }
  918. static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
  919. {
  920. return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
  921. }
  922. static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
  923. {
  924. struct acpi_thermal *tz = seq->private;
  925. if (!tz)
  926. goto end;
  927. if (!tz->flags.cooling_mode)
  928. seq_puts(seq, "<setting not supported>\n");
  929. else
  930. seq_puts(seq, "0 - Active; 1 - Passive\n");
  931. end:
  932. return 0;
  933. }
  934. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
  935. {
  936. return single_open(file, acpi_thermal_cooling_seq_show,
  937. PDE(inode)->data);
  938. }
  939. static ssize_t
  940. acpi_thermal_write_cooling_mode(struct file *file,
  941. const char __user * buffer,
  942. size_t count, loff_t * ppos)
  943. {
  944. struct seq_file *m = file->private_data;
  945. struct acpi_thermal *tz = m->private;
  946. int result = 0;
  947. char mode_string[12] = { '\0' };
  948. if (!tz || (count > sizeof(mode_string) - 1))
  949. return -EINVAL;
  950. if (!tz->flags.cooling_mode)
  951. return -ENODEV;
  952. if (copy_from_user(mode_string, buffer, count))
  953. return -EFAULT;
  954. mode_string[count] = '\0';
  955. result = acpi_thermal_set_cooling_mode(tz,
  956. simple_strtoul(mode_string, NULL,
  957. 0));
  958. if (result)
  959. return result;
  960. acpi_thermal_check(tz);
  961. return count;
  962. }
  963. static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
  964. {
  965. struct acpi_thermal *tz = seq->private;
  966. if (!tz)
  967. goto end;
  968. if (!tz->thermal_zone->polling_delay) {
  969. seq_puts(seq, "<polling disabled>\n");
  970. goto end;
  971. }
  972. seq_printf(seq, "polling frequency: %d seconds\n",
  973. (tz->thermal_zone->polling_delay / 1000));
  974. end:
  975. return 0;
  976. }
  977. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
  978. {
  979. return single_open(file, acpi_thermal_polling_seq_show,
  980. PDE(inode)->data);
  981. }
  982. static ssize_t
  983. acpi_thermal_write_polling(struct file *file,
  984. const char __user * buffer,
  985. size_t count, loff_t * ppos)
  986. {
  987. struct seq_file *m = file->private_data;
  988. struct acpi_thermal *tz = m->private;
  989. int result = 0;
  990. char polling_string[12] = { '\0' };
  991. int seconds = 0;
  992. if (!tz || (count > sizeof(polling_string) - 1))
  993. return -EINVAL;
  994. if (copy_from_user(polling_string, buffer, count))
  995. return -EFAULT;
  996. polling_string[count] = '\0';
  997. seconds = simple_strtoul(polling_string, NULL, 0);
  998. result = acpi_thermal_set_polling(tz, seconds);
  999. if (result)
  1000. return result;
  1001. acpi_thermal_check(tz);
  1002. return count;
  1003. }
  1004. static int acpi_thermal_add_fs(struct acpi_device *device)
  1005. {
  1006. struct proc_dir_entry *entry = NULL;
  1007. if (!acpi_device_dir(device)) {
  1008. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  1009. acpi_thermal_dir);
  1010. if (!acpi_device_dir(device))
  1011. return -ENODEV;
  1012. }
  1013. /* 'state' [R] */
  1014. entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
  1015. S_IRUGO, acpi_device_dir(device),
  1016. &acpi_thermal_state_fops,
  1017. acpi_driver_data(device));
  1018. if (!entry)
  1019. return -ENODEV;
  1020. /* 'temperature' [R] */
  1021. entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
  1022. S_IRUGO, acpi_device_dir(device),
  1023. &acpi_thermal_temp_fops,
  1024. acpi_driver_data(device));
  1025. if (!entry)
  1026. return -ENODEV;
  1027. /* 'trip_points' [R] */
  1028. entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
  1029. S_IRUGO,
  1030. acpi_device_dir(device),
  1031. &acpi_thermal_trip_fops,
  1032. acpi_driver_data(device));
  1033. if (!entry)
  1034. return -ENODEV;
  1035. /* 'cooling_mode' [R/W] */
  1036. entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
  1037. S_IFREG | S_IRUGO | S_IWUSR,
  1038. acpi_device_dir(device),
  1039. &acpi_thermal_cooling_fops,
  1040. acpi_driver_data(device));
  1041. if (!entry)
  1042. return -ENODEV;
  1043. /* 'polling_frequency' [R/W] */
  1044. entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
  1045. S_IFREG | S_IRUGO | S_IWUSR,
  1046. acpi_device_dir(device),
  1047. &acpi_thermal_polling_fops,
  1048. acpi_driver_data(device));
  1049. if (!entry)
  1050. return -ENODEV;
  1051. return 0;
  1052. }
  1053. static int acpi_thermal_remove_fs(struct acpi_device *device)
  1054. {
  1055. if (acpi_device_dir(device)) {
  1056. remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  1057. acpi_device_dir(device));
  1058. remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  1059. acpi_device_dir(device));
  1060. remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  1061. acpi_device_dir(device));
  1062. remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  1063. acpi_device_dir(device));
  1064. remove_proc_entry(ACPI_THERMAL_FILE_STATE,
  1065. acpi_device_dir(device));
  1066. remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
  1067. acpi_device_dir(device) = NULL;
  1068. }
  1069. return 0;
  1070. }
  1071. /* --------------------------------------------------------------------------
  1072. Driver Interface
  1073. -------------------------------------------------------------------------- */
  1074. static void acpi_thermal_notify(struct acpi_device *device, u32 event)
  1075. {
  1076. struct acpi_thermal *tz = acpi_driver_data(device);
  1077. if (!tz)
  1078. return;
  1079. switch (event) {
  1080. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  1081. acpi_thermal_check(tz);
  1082. break;
  1083. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  1084. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
  1085. acpi_thermal_check(tz);
  1086. acpi_bus_generate_proc_event(device, event, 0);
  1087. acpi_bus_generate_netlink_event(device->pnp.device_class,
  1088. dev_name(&device->dev), event, 0);
  1089. break;
  1090. case ACPI_THERMAL_NOTIFY_DEVICES:
  1091. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
  1092. acpi_thermal_check(tz);
  1093. acpi_bus_generate_proc_event(device, event, 0);
  1094. acpi_bus_generate_netlink_event(device->pnp.device_class,
  1095. dev_name(&device->dev), event, 0);
  1096. break;
  1097. default:
  1098. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1099. "Unsupported event [0x%x]\n", event));
  1100. break;
  1101. }
  1102. }
  1103. static int acpi_thermal_get_info(struct acpi_thermal *tz)
  1104. {
  1105. int result = 0;
  1106. if (!tz)
  1107. return -EINVAL;
  1108. /* Get temperature [_TMP] (required) */
  1109. result = acpi_thermal_get_temperature(tz);
  1110. if (result)
  1111. return result;
  1112. /* Get trip points [_CRT, _PSV, etc.] (required) */
  1113. result = acpi_thermal_get_trip_points(tz);
  1114. if (result)
  1115. return result;
  1116. /* Set the cooling mode [_SCP] to active cooling (default) */
  1117. result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
  1118. if (!result)
  1119. tz->flags.cooling_mode = 1;
  1120. /* Get default polling frequency [_TZP] (optional) */
  1121. if (tzp)
  1122. tz->polling_frequency = tzp;
  1123. else
  1124. acpi_thermal_get_polling_frequency(tz);
  1125. return 0;
  1126. }
  1127. /*
  1128. * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
  1129. * handles temperature values with a single decimal place. As a consequence,
  1130. * some implementations use an offset of 273.1 and others use an offset of
  1131. * 273.2. Try to find out which one is being used, to present the most
  1132. * accurate and visually appealing number.
  1133. *
  1134. * The heuristic below should work for all ACPI thermal zones which have a
  1135. * critical trip point with a value being a multiple of 0.5 degree Celsius.
  1136. */
  1137. static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
  1138. {
  1139. if (tz->trips.critical.flags.valid &&
  1140. (tz->trips.critical.temperature % 5) == 1)
  1141. tz->kelvin_offset = 2731;
  1142. else
  1143. tz->kelvin_offset = 2732;
  1144. }
  1145. static int acpi_thermal_add(struct acpi_device *device)
  1146. {
  1147. int result = 0;
  1148. struct acpi_thermal *tz = NULL;
  1149. if (!device)
  1150. return -EINVAL;
  1151. tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  1152. if (!tz)
  1153. return -ENOMEM;
  1154. tz->device = device;
  1155. strcpy(tz->name, device->pnp.bus_id);
  1156. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  1157. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  1158. device->driver_data = tz;
  1159. mutex_init(&tz->lock);
  1160. result = acpi_thermal_get_info(tz);
  1161. if (result)
  1162. goto free_memory;
  1163. acpi_thermal_guess_offset(tz);
  1164. result = acpi_thermal_register_thermal_zone(tz);
  1165. if (result)
  1166. goto free_memory;
  1167. result = acpi_thermal_add_fs(device);
  1168. if (result)
  1169. goto unregister_thermal_zone;
  1170. printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
  1171. acpi_device_name(device), acpi_device_bid(device),
  1172. KELVIN_TO_CELSIUS(tz->temperature));
  1173. goto end;
  1174. unregister_thermal_zone:
  1175. thermal_zone_device_unregister(tz->thermal_zone);
  1176. free_memory:
  1177. kfree(tz);
  1178. end:
  1179. return result;
  1180. }
  1181. static int acpi_thermal_remove(struct acpi_device *device, int type)
  1182. {
  1183. struct acpi_thermal *tz = NULL;
  1184. if (!device || !acpi_driver_data(device))
  1185. return -EINVAL;
  1186. tz = acpi_driver_data(device);
  1187. acpi_thermal_remove_fs(device);
  1188. acpi_thermal_unregister_thermal_zone(tz);
  1189. mutex_destroy(&tz->lock);
  1190. kfree(tz);
  1191. return 0;
  1192. }
  1193. static int acpi_thermal_resume(struct acpi_device *device)
  1194. {
  1195. struct acpi_thermal *tz = NULL;
  1196. int i, j, power_state, result;
  1197. if (!device || !acpi_driver_data(device))
  1198. return -EINVAL;
  1199. tz = acpi_driver_data(device);
  1200. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  1201. if (!(&tz->trips.active[i]))
  1202. break;
  1203. if (!tz->trips.active[i].flags.valid)
  1204. break;
  1205. tz->trips.active[i].flags.enabled = 1;
  1206. for (j = 0; j < tz->trips.active[i].devices.count; j++) {
  1207. result = acpi_bus_get_power(tz->trips.active[i].devices.
  1208. handles[j], &power_state);
  1209. if (result || (power_state != ACPI_STATE_D0)) {
  1210. tz->trips.active[i].flags.enabled = 0;
  1211. break;
  1212. }
  1213. }
  1214. tz->state.active |= tz->trips.active[i].flags.enabled;
  1215. }
  1216. acpi_thermal_check(tz);
  1217. return AE_OK;
  1218. }
  1219. static int thermal_act(const struct dmi_system_id *d) {
  1220. if (act == 0) {
  1221. printk(KERN_NOTICE "ACPI: %s detected: "
  1222. "disabling all active thermal trip points\n", d->ident);
  1223. act = -1;
  1224. }
  1225. return 0;
  1226. }
  1227. static int thermal_nocrt(const struct dmi_system_id *d) {
  1228. printk(KERN_NOTICE "ACPI: %s detected: "
  1229. "disabling all critical thermal trip point actions.\n", d->ident);
  1230. nocrt = 1;
  1231. return 0;
  1232. }
  1233. static int thermal_tzp(const struct dmi_system_id *d) {
  1234. if (tzp == 0) {
  1235. printk(KERN_NOTICE "ACPI: %s detected: "
  1236. "enabling thermal zone polling\n", d->ident);
  1237. tzp = 300; /* 300 dS = 30 Seconds */
  1238. }
  1239. return 0;
  1240. }
  1241. static int thermal_psv(const struct dmi_system_id *d) {
  1242. if (psv == 0) {
  1243. printk(KERN_NOTICE "ACPI: %s detected: "
  1244. "disabling all passive thermal trip points\n", d->ident);
  1245. psv = -1;
  1246. }
  1247. return 0;
  1248. }
  1249. static struct dmi_system_id thermal_dmi_table[] __initdata = {
  1250. /*
  1251. * Award BIOS on this AOpen makes thermal control almost worthless.
  1252. * http://bugzilla.kernel.org/show_bug.cgi?id=8842
  1253. */
  1254. {
  1255. .callback = thermal_act,
  1256. .ident = "AOpen i915GMm-HFS",
  1257. .matches = {
  1258. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1259. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1260. },
  1261. },
  1262. {
  1263. .callback = thermal_psv,
  1264. .ident = "AOpen i915GMm-HFS",
  1265. .matches = {
  1266. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1267. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1268. },
  1269. },
  1270. {
  1271. .callback = thermal_tzp,
  1272. .ident = "AOpen i915GMm-HFS",
  1273. .matches = {
  1274. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1275. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1276. },
  1277. },
  1278. {
  1279. .callback = thermal_nocrt,
  1280. .ident = "Gigabyte GA-7ZX",
  1281. .matches = {
  1282. DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
  1283. DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
  1284. },
  1285. },
  1286. {}
  1287. };
  1288. static int __init acpi_thermal_init(void)
  1289. {
  1290. int result = 0;
  1291. dmi_check_system(thermal_dmi_table);
  1292. if (off) {
  1293. printk(KERN_NOTICE "ACPI: thermal control disabled\n");
  1294. return -ENODEV;
  1295. }
  1296. acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
  1297. if (!acpi_thermal_dir)
  1298. return -ENODEV;
  1299. result = acpi_bus_register_driver(&acpi_thermal_driver);
  1300. if (result < 0) {
  1301. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1302. return -ENODEV;
  1303. }
  1304. return 0;
  1305. }
  1306. static void __exit acpi_thermal_exit(void)
  1307. {
  1308. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1309. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1310. return;
  1311. }
  1312. module_init(acpi_thermal_init);
  1313. module_exit(acpi_thermal_exit);