thermal.c 39 KB

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