thermal.c 39 KB

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