thermal.c 40 KB

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