thermal.c 40 KB

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