thermal.c 40 KB

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