thermal.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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 (required) */
  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.temperature <= 2732) {
  323. tz->trips.critical.flags.valid = 0;
  324. ACPI_EXCEPTION((AE_INFO, status,
  325. "No or invalid critical threshold"));
  326. return -ENODEV;
  327. } else {
  328. tz->trips.critical.flags.valid = 1;
  329. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  330. "Found critical threshold [%lu]\n",
  331. tz->trips.critical.temperature));
  332. }
  333. if (tz->trips.critical.flags.valid == 1) {
  334. if (crt == -1) {
  335. tz->trips.critical.flags.valid = 0;
  336. } else if (crt > 0) {
  337. unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
  338. /*
  339. * Allow override critical threshold
  340. */
  341. if (crt_k > tz->trips.critical.temperature)
  342. printk(KERN_WARNING PREFIX
  343. "Critical threshold %d C\n", crt);
  344. tz->trips.critical.temperature = crt_k;
  345. }
  346. }
  347. }
  348. /* Critical Sleep (optional) */
  349. if (flag & ACPI_TRIPS_HOT) {
  350. status = acpi_evaluate_integer(tz->device->handle,
  351. "_HOT", NULL, &tmp);
  352. if (ACPI_FAILURE(status)) {
  353. tz->trips.hot.flags.valid = 0;
  354. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  355. "No hot threshold\n"));
  356. } else {
  357. tz->trips.hot.temperature = tmp;
  358. tz->trips.hot.flags.valid = 1;
  359. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  360. "Found hot threshold [%lu]\n",
  361. tz->trips.critical.temperature));
  362. }
  363. }
  364. /* Passive (optional) */
  365. if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
  366. (flag == ACPI_TRIPS_INIT)) {
  367. valid = tz->trips.passive.flags.valid;
  368. if (psv == -1) {
  369. status = AE_SUPPORT;
  370. } else if (psv > 0) {
  371. tmp = CELSIUS_TO_KELVIN(psv);
  372. status = AE_OK;
  373. } else {
  374. status = acpi_evaluate_integer(tz->device->handle,
  375. "_PSV", NULL, &tmp);
  376. }
  377. if (ACPI_FAILURE(status))
  378. tz->trips.passive.flags.valid = 0;
  379. else {
  380. tz->trips.passive.temperature = tmp;
  381. tz->trips.passive.flags.valid = 1;
  382. if (flag == ACPI_TRIPS_INIT) {
  383. status = acpi_evaluate_integer(
  384. tz->device->handle, "_TC1",
  385. NULL, &tmp);
  386. if (ACPI_FAILURE(status))
  387. tz->trips.passive.flags.valid = 0;
  388. else
  389. tz->trips.passive.tc1 = tmp;
  390. status = acpi_evaluate_integer(
  391. tz->device->handle, "_TC2",
  392. NULL, &tmp);
  393. if (ACPI_FAILURE(status))
  394. tz->trips.passive.flags.valid = 0;
  395. else
  396. tz->trips.passive.tc2 = tmp;
  397. status = acpi_evaluate_integer(
  398. tz->device->handle, "_TSP",
  399. NULL, &tmp);
  400. if (ACPI_FAILURE(status))
  401. tz->trips.passive.flags.valid = 0;
  402. else
  403. tz->trips.passive.tsp = tmp;
  404. }
  405. }
  406. }
  407. if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
  408. memset(&devices, 0, sizeof(struct acpi_handle_list));
  409. status = acpi_evaluate_reference(tz->device->handle, "_PSL",
  410. NULL, &devices);
  411. if (ACPI_FAILURE(status)) {
  412. printk(KERN_WARNING PREFIX
  413. "Invalid passive threshold\n");
  414. tz->trips.passive.flags.valid = 0;
  415. }
  416. else
  417. tz->trips.passive.flags.valid = 1;
  418. if (memcmp(&tz->trips.passive.devices, &devices,
  419. sizeof(struct acpi_handle_list))) {
  420. memcpy(&tz->trips.passive.devices, &devices,
  421. sizeof(struct acpi_handle_list));
  422. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  423. }
  424. }
  425. if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
  426. if (valid != tz->trips.passive.flags.valid)
  427. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
  428. }
  429. /* Active (optional) */
  430. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  431. char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
  432. valid = tz->trips.active[i].flags.valid;
  433. if (act == -1)
  434. break; /* disable all active trip points */
  435. if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
  436. tz->trips.active[i].flags.valid)) {
  437. status = acpi_evaluate_integer(tz->device->handle,
  438. name, NULL, &tmp);
  439. if (ACPI_FAILURE(status)) {
  440. tz->trips.active[i].flags.valid = 0;
  441. if (i == 0)
  442. break;
  443. if (act <= 0)
  444. break;
  445. if (i == 1)
  446. tz->trips.active[0].temperature =
  447. CELSIUS_TO_KELVIN(act);
  448. else
  449. /*
  450. * Don't allow override higher than
  451. * the next higher trip point
  452. */
  453. tz->trips.active[i - 1].temperature =
  454. (tz->trips.active[i - 2].temperature <
  455. CELSIUS_TO_KELVIN(act) ?
  456. tz->trips.active[i - 2].temperature :
  457. CELSIUS_TO_KELVIN(act));
  458. break;
  459. } else {
  460. tz->trips.active[i].temperature = tmp;
  461. tz->trips.active[i].flags.valid = 1;
  462. }
  463. }
  464. name[2] = 'L';
  465. if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
  466. memset(&devices, 0, sizeof(struct acpi_handle_list));
  467. status = acpi_evaluate_reference(tz->device->handle,
  468. name, NULL, &devices);
  469. if (ACPI_FAILURE(status)) {
  470. printk(KERN_WARNING PREFIX
  471. "Invalid active%d threshold\n", i);
  472. tz->trips.active[i].flags.valid = 0;
  473. }
  474. else
  475. tz->trips.active[i].flags.valid = 1;
  476. if (memcmp(&tz->trips.active[i].devices, &devices,
  477. sizeof(struct acpi_handle_list))) {
  478. memcpy(&tz->trips.active[i].devices, &devices,
  479. sizeof(struct acpi_handle_list));
  480. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  481. }
  482. }
  483. if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
  484. if (valid != tz->trips.active[i].flags.valid)
  485. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
  486. if (!tz->trips.active[i].flags.valid)
  487. break;
  488. }
  489. if (flag & ACPI_TRIPS_DEVICES) {
  490. memset(&devices, 0, sizeof(struct acpi_handle_list));
  491. status = acpi_evaluate_reference(tz->device->handle, "_TZD",
  492. NULL, &devices);
  493. if (memcmp(&tz->devices, &devices,
  494. sizeof(struct acpi_handle_list))) {
  495. memcpy(&tz->devices, &devices,
  496. sizeof(struct acpi_handle_list));
  497. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  498. }
  499. }
  500. return 0;
  501. }
  502. static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
  503. {
  504. return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
  505. }
  506. static void acpi_thermal_check(void *data)
  507. {
  508. struct acpi_thermal *tz = data;
  509. thermal_zone_device_update(tz->thermal_zone);
  510. }
  511. /* sys I/F for generic thermal sysfs support */
  512. #define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
  513. static int thermal_get_temp(struct thermal_zone_device *thermal,
  514. unsigned long *temp)
  515. {
  516. struct acpi_thermal *tz = thermal->devdata;
  517. int result;
  518. if (!tz)
  519. return -EINVAL;
  520. result = acpi_thermal_get_temperature(tz);
  521. if (result)
  522. return result;
  523. *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
  524. return 0;
  525. }
  526. static const char enabled[] = "kernel";
  527. static const char disabled[] = "user";
  528. static int thermal_get_mode(struct thermal_zone_device *thermal,
  529. enum thermal_device_mode *mode)
  530. {
  531. struct acpi_thermal *tz = thermal->devdata;
  532. if (!tz)
  533. return -EINVAL;
  534. *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
  535. THERMAL_DEVICE_DISABLED;
  536. return 0;
  537. }
  538. static int thermal_set_mode(struct thermal_zone_device *thermal,
  539. enum thermal_device_mode mode)
  540. {
  541. struct acpi_thermal *tz = thermal->devdata;
  542. int enable;
  543. if (!tz)
  544. return -EINVAL;
  545. /*
  546. * enable/disable thermal management from ACPI thermal driver
  547. */
  548. if (mode == THERMAL_DEVICE_ENABLED)
  549. enable = 1;
  550. else if (mode == THERMAL_DEVICE_DISABLED)
  551. enable = 0;
  552. else
  553. return -EINVAL;
  554. if (enable != tz->tz_enabled) {
  555. tz->tz_enabled = enable;
  556. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  557. "%s ACPI thermal control\n",
  558. tz->tz_enabled ? enabled : disabled));
  559. acpi_thermal_check(tz);
  560. }
  561. return 0;
  562. }
  563. static int thermal_get_trip_type(struct thermal_zone_device *thermal,
  564. int trip, enum thermal_trip_type *type)
  565. {
  566. struct acpi_thermal *tz = thermal->devdata;
  567. int i;
  568. if (!tz || trip < 0)
  569. return -EINVAL;
  570. if (tz->trips.critical.flags.valid) {
  571. if (!trip) {
  572. *type = THERMAL_TRIP_CRITICAL;
  573. return 0;
  574. }
  575. trip--;
  576. }
  577. if (tz->trips.hot.flags.valid) {
  578. if (!trip) {
  579. *type = THERMAL_TRIP_HOT;
  580. return 0;
  581. }
  582. trip--;
  583. }
  584. if (tz->trips.passive.flags.valid) {
  585. if (!trip) {
  586. *type = THERMAL_TRIP_PASSIVE;
  587. return 0;
  588. }
  589. trip--;
  590. }
  591. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  592. tz->trips.active[i].flags.valid; i++) {
  593. if (!trip) {
  594. *type = THERMAL_TRIP_ACTIVE;
  595. return 0;
  596. }
  597. trip--;
  598. }
  599. return -EINVAL;
  600. }
  601. static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
  602. int trip, unsigned long *temp)
  603. {
  604. struct acpi_thermal *tz = thermal->devdata;
  605. int i;
  606. if (!tz || trip < 0)
  607. return -EINVAL;
  608. if (tz->trips.critical.flags.valid) {
  609. if (!trip) {
  610. *temp = KELVIN_TO_MILLICELSIUS(
  611. tz->trips.critical.temperature,
  612. tz->kelvin_offset);
  613. return 0;
  614. }
  615. trip--;
  616. }
  617. if (tz->trips.hot.flags.valid) {
  618. if (!trip) {
  619. *temp = KELVIN_TO_MILLICELSIUS(
  620. tz->trips.hot.temperature,
  621. tz->kelvin_offset);
  622. return 0;
  623. }
  624. trip--;
  625. }
  626. if (tz->trips.passive.flags.valid) {
  627. if (!trip) {
  628. *temp = KELVIN_TO_MILLICELSIUS(
  629. tz->trips.passive.temperature,
  630. tz->kelvin_offset);
  631. return 0;
  632. }
  633. trip--;
  634. }
  635. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  636. tz->trips.active[i].flags.valid; i++) {
  637. if (!trip) {
  638. *temp = KELVIN_TO_MILLICELSIUS(
  639. tz->trips.active[i].temperature,
  640. tz->kelvin_offset);
  641. return 0;
  642. }
  643. trip--;
  644. }
  645. return -EINVAL;
  646. }
  647. static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
  648. unsigned long *temperature) {
  649. struct acpi_thermal *tz = thermal->devdata;
  650. if (tz->trips.critical.flags.valid) {
  651. *temperature = KELVIN_TO_MILLICELSIUS(
  652. tz->trips.critical.temperature,
  653. tz->kelvin_offset);
  654. return 0;
  655. } else
  656. return -EINVAL;
  657. }
  658. static int thermal_notify(struct thermal_zone_device *thermal, int trip,
  659. enum thermal_trip_type trip_type)
  660. {
  661. u8 type = 0;
  662. struct acpi_thermal *tz = thermal->devdata;
  663. if (trip_type == THERMAL_TRIP_CRITICAL)
  664. type = ACPI_THERMAL_NOTIFY_CRITICAL;
  665. else if (trip_type == THERMAL_TRIP_HOT)
  666. type = ACPI_THERMAL_NOTIFY_HOT;
  667. else
  668. return 0;
  669. acpi_bus_generate_proc_event(tz->device, type, 1);
  670. acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
  671. dev_name(&tz->device->dev), type, 1);
  672. if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
  673. return 1;
  674. return 0;
  675. }
  676. typedef int (*cb)(struct thermal_zone_device *, int,
  677. struct thermal_cooling_device *);
  678. static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
  679. struct thermal_cooling_device *cdev,
  680. cb action)
  681. {
  682. struct acpi_device *device = cdev->devdata;
  683. struct acpi_thermal *tz = thermal->devdata;
  684. struct acpi_device *dev;
  685. acpi_status status;
  686. acpi_handle handle;
  687. int i;
  688. int j;
  689. int trip = -1;
  690. int result = 0;
  691. if (tz->trips.critical.flags.valid)
  692. trip++;
  693. if (tz->trips.hot.flags.valid)
  694. trip++;
  695. if (tz->trips.passive.flags.valid) {
  696. trip++;
  697. for (i = 0; i < tz->trips.passive.devices.count;
  698. i++) {
  699. handle = tz->trips.passive.devices.handles[i];
  700. status = acpi_bus_get_device(handle, &dev);
  701. if (ACPI_SUCCESS(status) && (dev == device)) {
  702. result = action(thermal, trip, cdev);
  703. if (result)
  704. goto failed;
  705. }
  706. }
  707. }
  708. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  709. if (!tz->trips.active[i].flags.valid)
  710. break;
  711. trip++;
  712. for (j = 0;
  713. j < tz->trips.active[i].devices.count;
  714. j++) {
  715. handle = tz->trips.active[i].devices.handles[j];
  716. status = acpi_bus_get_device(handle, &dev);
  717. if (ACPI_SUCCESS(status) && (dev == device)) {
  718. result = action(thermal, trip, cdev);
  719. if (result)
  720. goto failed;
  721. }
  722. }
  723. }
  724. for (i = 0; i < tz->devices.count; i++) {
  725. handle = tz->devices.handles[i];
  726. status = acpi_bus_get_device(handle, &dev);
  727. if (ACPI_SUCCESS(status) && (dev == device)) {
  728. result = action(thermal, -1, cdev);
  729. if (result)
  730. goto failed;
  731. }
  732. }
  733. failed:
  734. return result;
  735. }
  736. static int
  737. acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
  738. struct thermal_cooling_device *cdev)
  739. {
  740. return acpi_thermal_cooling_device_cb(thermal, cdev,
  741. thermal_zone_bind_cooling_device);
  742. }
  743. static int
  744. acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
  745. struct thermal_cooling_device *cdev)
  746. {
  747. return acpi_thermal_cooling_device_cb(thermal, cdev,
  748. thermal_zone_unbind_cooling_device);
  749. }
  750. static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
  751. .bind = acpi_thermal_bind_cooling_device,
  752. .unbind = acpi_thermal_unbind_cooling_device,
  753. .get_temp = thermal_get_temp,
  754. .get_mode = thermal_get_mode,
  755. .set_mode = thermal_set_mode,
  756. .get_trip_type = thermal_get_trip_type,
  757. .get_trip_temp = thermal_get_trip_temp,
  758. .get_crit_temp = thermal_get_crit_temp,
  759. .notify = thermal_notify,
  760. };
  761. static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
  762. {
  763. int trips = 0;
  764. int result;
  765. acpi_status status;
  766. int i;
  767. if (tz->trips.critical.flags.valid)
  768. trips++;
  769. if (tz->trips.hot.flags.valid)
  770. trips++;
  771. if (tz->trips.passive.flags.valid)
  772. trips++;
  773. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  774. tz->trips.active[i].flags.valid; i++, trips++);
  775. if (tz->trips.passive.flags.valid)
  776. tz->thermal_zone =
  777. thermal_zone_device_register("acpitz", trips, tz,
  778. &acpi_thermal_zone_ops,
  779. tz->trips.passive.tc1,
  780. tz->trips.passive.tc2,
  781. tz->trips.passive.tsp*100,
  782. tz->polling_frequency*100);
  783. else
  784. tz->thermal_zone =
  785. thermal_zone_device_register("acpitz", trips, tz,
  786. &acpi_thermal_zone_ops,
  787. 0, 0, 0,
  788. tz->polling_frequency*100);
  789. if (IS_ERR(tz->thermal_zone))
  790. return -ENODEV;
  791. result = sysfs_create_link(&tz->device->dev.kobj,
  792. &tz->thermal_zone->device.kobj, "thermal_zone");
  793. if (result)
  794. return result;
  795. result = sysfs_create_link(&tz->thermal_zone->device.kobj,
  796. &tz->device->dev.kobj, "device");
  797. if (result)
  798. return result;
  799. status = acpi_attach_data(tz->device->handle,
  800. acpi_bus_private_data_handler,
  801. tz->thermal_zone);
  802. if (ACPI_FAILURE(status)) {
  803. printk(KERN_ERR PREFIX
  804. "Error attaching device data\n");
  805. return -ENODEV;
  806. }
  807. tz->tz_enabled = 1;
  808. dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
  809. tz->thermal_zone->id);
  810. return 0;
  811. }
  812. static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
  813. {
  814. sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
  815. sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
  816. thermal_zone_device_unregister(tz->thermal_zone);
  817. tz->thermal_zone = NULL;
  818. acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
  819. }
  820. /* --------------------------------------------------------------------------
  821. FS Interface (/proc)
  822. -------------------------------------------------------------------------- */
  823. static struct proc_dir_entry *acpi_thermal_dir;
  824. static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
  825. {
  826. struct acpi_thermal *tz = seq->private;
  827. if (!tz)
  828. goto end;
  829. seq_puts(seq, "state: ");
  830. if (!tz->state.critical && !tz->state.hot && !tz->state.passive
  831. && !tz->state.active)
  832. seq_puts(seq, "ok\n");
  833. else {
  834. if (tz->state.critical)
  835. seq_puts(seq, "critical ");
  836. if (tz->state.hot)
  837. seq_puts(seq, "hot ");
  838. if (tz->state.passive)
  839. seq_puts(seq, "passive ");
  840. if (tz->state.active)
  841. seq_printf(seq, "active[%d]", tz->state.active_index);
  842. seq_puts(seq, "\n");
  843. }
  844. end:
  845. return 0;
  846. }
  847. static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
  848. {
  849. return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
  850. }
  851. static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
  852. {
  853. int result = 0;
  854. struct acpi_thermal *tz = seq->private;
  855. if (!tz)
  856. goto end;
  857. result = acpi_thermal_get_temperature(tz);
  858. if (result)
  859. goto end;
  860. seq_printf(seq, "temperature: %ld C\n",
  861. KELVIN_TO_CELSIUS(tz->temperature));
  862. end:
  863. return 0;
  864. }
  865. static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
  866. {
  867. return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
  868. }
  869. static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
  870. {
  871. struct acpi_thermal *tz = seq->private;
  872. struct acpi_device *device;
  873. acpi_status status;
  874. int i = 0;
  875. int j = 0;
  876. if (!tz)
  877. goto end;
  878. if (tz->trips.critical.flags.valid)
  879. seq_printf(seq, "critical (S5): %ld C%s",
  880. KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
  881. nocrt ? " <disabled>\n" : "\n");
  882. if (tz->trips.hot.flags.valid)
  883. seq_printf(seq, "hot (S4): %ld C%s",
  884. KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
  885. nocrt ? " <disabled>\n" : "\n");
  886. if (tz->trips.passive.flags.valid) {
  887. seq_printf(seq,
  888. "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
  889. KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
  890. tz->trips.passive.tc1, tz->trips.passive.tc2,
  891. tz->trips.passive.tsp);
  892. for (j = 0; j < tz->trips.passive.devices.count; j++) {
  893. status = acpi_bus_get_device(tz->trips.passive.devices.
  894. handles[j], &device);
  895. seq_printf(seq, "%4.4s ", status ? "" :
  896. acpi_device_bid(device));
  897. }
  898. seq_puts(seq, "\n");
  899. } else {
  900. seq_printf(seq, "passive (forced):");
  901. if (tz->thermal_zone->forced_passive)
  902. seq_printf(seq, " %i C\n",
  903. tz->thermal_zone->forced_passive / 1000);
  904. else
  905. seq_printf(seq, "<not set>\n");
  906. }
  907. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  908. if (!(tz->trips.active[i].flags.valid))
  909. break;
  910. seq_printf(seq, "active[%d]: %ld C: devices=",
  911. i,
  912. KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
  913. for (j = 0; j < tz->trips.active[i].devices.count; j++){
  914. status = acpi_bus_get_device(tz->trips.active[i].
  915. devices.handles[j],
  916. &device);
  917. seq_printf(seq, "%4.4s ", status ? "" :
  918. acpi_device_bid(device));
  919. }
  920. seq_puts(seq, "\n");
  921. }
  922. end:
  923. return 0;
  924. }
  925. static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
  926. {
  927. return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
  928. }
  929. static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
  930. {
  931. struct acpi_thermal *tz = seq->private;
  932. if (!tz)
  933. goto end;
  934. if (!tz->flags.cooling_mode)
  935. seq_puts(seq, "<setting not supported>\n");
  936. else
  937. seq_puts(seq, "0 - Active; 1 - Passive\n");
  938. end:
  939. return 0;
  940. }
  941. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
  942. {
  943. return single_open(file, acpi_thermal_cooling_seq_show,
  944. PDE(inode)->data);
  945. }
  946. static ssize_t
  947. acpi_thermal_write_cooling_mode(struct file *file,
  948. const char __user * buffer,
  949. size_t count, loff_t * ppos)
  950. {
  951. struct seq_file *m = file->private_data;
  952. struct acpi_thermal *tz = m->private;
  953. int result = 0;
  954. char mode_string[12] = { '\0' };
  955. if (!tz || (count > sizeof(mode_string) - 1))
  956. return -EINVAL;
  957. if (!tz->flags.cooling_mode)
  958. return -ENODEV;
  959. if (copy_from_user(mode_string, buffer, count))
  960. return -EFAULT;
  961. mode_string[count] = '\0';
  962. result = acpi_thermal_set_cooling_mode(tz,
  963. simple_strtoul(mode_string, NULL,
  964. 0));
  965. if (result)
  966. return result;
  967. acpi_thermal_check(tz);
  968. return count;
  969. }
  970. static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
  971. {
  972. struct acpi_thermal *tz = seq->private;
  973. if (!tz)
  974. goto end;
  975. if (!tz->thermal_zone->polling_delay) {
  976. seq_puts(seq, "<polling disabled>\n");
  977. goto end;
  978. }
  979. seq_printf(seq, "polling frequency: %d seconds\n",
  980. (tz->thermal_zone->polling_delay / 1000));
  981. end:
  982. return 0;
  983. }
  984. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
  985. {
  986. return single_open(file, acpi_thermal_polling_seq_show,
  987. PDE(inode)->data);
  988. }
  989. static ssize_t
  990. acpi_thermal_write_polling(struct file *file,
  991. const char __user * buffer,
  992. size_t count, loff_t * ppos)
  993. {
  994. struct seq_file *m = file->private_data;
  995. struct acpi_thermal *tz = m->private;
  996. int result = 0;
  997. char polling_string[12] = { '\0' };
  998. int seconds = 0;
  999. if (!tz || (count > sizeof(polling_string) - 1))
  1000. return -EINVAL;
  1001. if (copy_from_user(polling_string, buffer, count))
  1002. return -EFAULT;
  1003. polling_string[count] = '\0';
  1004. seconds = simple_strtoul(polling_string, NULL, 0);
  1005. result = acpi_thermal_set_polling(tz, seconds);
  1006. if (result)
  1007. return result;
  1008. acpi_thermal_check(tz);
  1009. return count;
  1010. }
  1011. static int acpi_thermal_add_fs(struct acpi_device *device)
  1012. {
  1013. struct proc_dir_entry *entry = NULL;
  1014. if (!acpi_device_dir(device)) {
  1015. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  1016. acpi_thermal_dir);
  1017. if (!acpi_device_dir(device))
  1018. return -ENODEV;
  1019. }
  1020. /* 'state' [R] */
  1021. entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
  1022. S_IRUGO, acpi_device_dir(device),
  1023. &acpi_thermal_state_fops,
  1024. acpi_driver_data(device));
  1025. if (!entry)
  1026. return -ENODEV;
  1027. /* 'temperature' [R] */
  1028. entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
  1029. S_IRUGO, acpi_device_dir(device),
  1030. &acpi_thermal_temp_fops,
  1031. acpi_driver_data(device));
  1032. if (!entry)
  1033. return -ENODEV;
  1034. /* 'trip_points' [R] */
  1035. entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
  1036. S_IRUGO,
  1037. acpi_device_dir(device),
  1038. &acpi_thermal_trip_fops,
  1039. acpi_driver_data(device));
  1040. if (!entry)
  1041. return -ENODEV;
  1042. /* 'cooling_mode' [R/W] */
  1043. entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
  1044. S_IFREG | S_IRUGO | S_IWUSR,
  1045. acpi_device_dir(device),
  1046. &acpi_thermal_cooling_fops,
  1047. acpi_driver_data(device));
  1048. if (!entry)
  1049. return -ENODEV;
  1050. /* 'polling_frequency' [R/W] */
  1051. entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
  1052. S_IFREG | S_IRUGO | S_IWUSR,
  1053. acpi_device_dir(device),
  1054. &acpi_thermal_polling_fops,
  1055. acpi_driver_data(device));
  1056. if (!entry)
  1057. return -ENODEV;
  1058. return 0;
  1059. }
  1060. static int acpi_thermal_remove_fs(struct acpi_device *device)
  1061. {
  1062. if (acpi_device_dir(device)) {
  1063. remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  1064. acpi_device_dir(device));
  1065. remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  1066. acpi_device_dir(device));
  1067. remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  1068. acpi_device_dir(device));
  1069. remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  1070. acpi_device_dir(device));
  1071. remove_proc_entry(ACPI_THERMAL_FILE_STATE,
  1072. acpi_device_dir(device));
  1073. remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
  1074. acpi_device_dir(device) = NULL;
  1075. }
  1076. return 0;
  1077. }
  1078. /* --------------------------------------------------------------------------
  1079. Driver Interface
  1080. -------------------------------------------------------------------------- */
  1081. static void acpi_thermal_notify(struct acpi_device *device, u32 event)
  1082. {
  1083. struct acpi_thermal *tz = acpi_driver_data(device);
  1084. if (!tz)
  1085. return;
  1086. switch (event) {
  1087. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  1088. acpi_thermal_check(tz);
  1089. break;
  1090. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  1091. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
  1092. acpi_thermal_check(tz);
  1093. acpi_bus_generate_proc_event(device, event, 0);
  1094. acpi_bus_generate_netlink_event(device->pnp.device_class,
  1095. dev_name(&device->dev), event, 0);
  1096. break;
  1097. case ACPI_THERMAL_NOTIFY_DEVICES:
  1098. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
  1099. acpi_thermal_check(tz);
  1100. acpi_bus_generate_proc_event(device, event, 0);
  1101. acpi_bus_generate_netlink_event(device->pnp.device_class,
  1102. dev_name(&device->dev), event, 0);
  1103. break;
  1104. default:
  1105. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1106. "Unsupported event [0x%x]\n", event));
  1107. break;
  1108. }
  1109. }
  1110. static int acpi_thermal_get_info(struct acpi_thermal *tz)
  1111. {
  1112. int result = 0;
  1113. if (!tz)
  1114. return -EINVAL;
  1115. /* Get temperature [_TMP] (required) */
  1116. result = acpi_thermal_get_temperature(tz);
  1117. if (result)
  1118. return result;
  1119. /* Get trip points [_CRT, _PSV, etc.] (required) */
  1120. result = acpi_thermal_get_trip_points(tz);
  1121. if (result)
  1122. return result;
  1123. /* Set the cooling mode [_SCP] to active cooling (default) */
  1124. result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
  1125. if (!result)
  1126. tz->flags.cooling_mode = 1;
  1127. /* Get default polling frequency [_TZP] (optional) */
  1128. if (tzp)
  1129. tz->polling_frequency = tzp;
  1130. else
  1131. acpi_thermal_get_polling_frequency(tz);
  1132. return 0;
  1133. }
  1134. /*
  1135. * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
  1136. * handles temperature values with a single decimal place. As a consequence,
  1137. * some implementations use an offset of 273.1 and others use an offset of
  1138. * 273.2. Try to find out which one is being used, to present the most
  1139. * accurate and visually appealing number.
  1140. *
  1141. * The heuristic below should work for all ACPI thermal zones which have a
  1142. * critical trip point with a value being a multiple of 0.5 degree Celsius.
  1143. */
  1144. static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
  1145. {
  1146. if (tz->trips.critical.flags.valid &&
  1147. (tz->trips.critical.temperature % 5) == 1)
  1148. tz->kelvin_offset = 2731;
  1149. else
  1150. tz->kelvin_offset = 2732;
  1151. }
  1152. static int acpi_thermal_add(struct acpi_device *device)
  1153. {
  1154. int result = 0;
  1155. struct acpi_thermal *tz = NULL;
  1156. if (!device)
  1157. return -EINVAL;
  1158. tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  1159. if (!tz)
  1160. return -ENOMEM;
  1161. tz->device = device;
  1162. strcpy(tz->name, device->pnp.bus_id);
  1163. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  1164. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  1165. device->driver_data = tz;
  1166. mutex_init(&tz->lock);
  1167. result = acpi_thermal_get_info(tz);
  1168. if (result)
  1169. goto free_memory;
  1170. acpi_thermal_guess_offset(tz);
  1171. result = acpi_thermal_register_thermal_zone(tz);
  1172. if (result)
  1173. goto free_memory;
  1174. result = acpi_thermal_add_fs(device);
  1175. if (result)
  1176. goto unregister_thermal_zone;
  1177. printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
  1178. acpi_device_name(device), acpi_device_bid(device),
  1179. KELVIN_TO_CELSIUS(tz->temperature));
  1180. goto end;
  1181. unregister_thermal_zone:
  1182. thermal_zone_device_unregister(tz->thermal_zone);
  1183. free_memory:
  1184. kfree(tz);
  1185. end:
  1186. return result;
  1187. }
  1188. static int acpi_thermal_remove(struct acpi_device *device, int type)
  1189. {
  1190. struct acpi_thermal *tz = NULL;
  1191. if (!device || !acpi_driver_data(device))
  1192. return -EINVAL;
  1193. tz = acpi_driver_data(device);
  1194. acpi_thermal_remove_fs(device);
  1195. acpi_thermal_unregister_thermal_zone(tz);
  1196. mutex_destroy(&tz->lock);
  1197. kfree(tz);
  1198. return 0;
  1199. }
  1200. static int acpi_thermal_resume(struct acpi_device *device)
  1201. {
  1202. struct acpi_thermal *tz = NULL;
  1203. int i, j, power_state, result;
  1204. if (!device || !acpi_driver_data(device))
  1205. return -EINVAL;
  1206. tz = acpi_driver_data(device);
  1207. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  1208. if (!(&tz->trips.active[i]))
  1209. break;
  1210. if (!tz->trips.active[i].flags.valid)
  1211. break;
  1212. tz->trips.active[i].flags.enabled = 1;
  1213. for (j = 0; j < tz->trips.active[i].devices.count; j++) {
  1214. result = acpi_bus_get_power(tz->trips.active[i].devices.
  1215. handles[j], &power_state);
  1216. if (result || (power_state != ACPI_STATE_D0)) {
  1217. tz->trips.active[i].flags.enabled = 0;
  1218. break;
  1219. }
  1220. }
  1221. tz->state.active |= tz->trips.active[i].flags.enabled;
  1222. }
  1223. acpi_thermal_check(tz);
  1224. return AE_OK;
  1225. }
  1226. static int thermal_act(const struct dmi_system_id *d) {
  1227. if (act == 0) {
  1228. printk(KERN_NOTICE "ACPI: %s detected: "
  1229. "disabling all active thermal trip points\n", d->ident);
  1230. act = -1;
  1231. }
  1232. return 0;
  1233. }
  1234. static int thermal_nocrt(const struct dmi_system_id *d) {
  1235. printk(KERN_NOTICE "ACPI: %s detected: "
  1236. "disabling all critical thermal trip point actions.\n", d->ident);
  1237. nocrt = 1;
  1238. return 0;
  1239. }
  1240. static int thermal_tzp(const struct dmi_system_id *d) {
  1241. if (tzp == 0) {
  1242. printk(KERN_NOTICE "ACPI: %s detected: "
  1243. "enabling thermal zone polling\n", d->ident);
  1244. tzp = 300; /* 300 dS = 30 Seconds */
  1245. }
  1246. return 0;
  1247. }
  1248. static int thermal_psv(const struct dmi_system_id *d) {
  1249. if (psv == 0) {
  1250. printk(KERN_NOTICE "ACPI: %s detected: "
  1251. "disabling all passive thermal trip points\n", d->ident);
  1252. psv = -1;
  1253. }
  1254. return 0;
  1255. }
  1256. static struct dmi_system_id thermal_dmi_table[] __initdata = {
  1257. /*
  1258. * Award BIOS on this AOpen makes thermal control almost worthless.
  1259. * http://bugzilla.kernel.org/show_bug.cgi?id=8842
  1260. */
  1261. {
  1262. .callback = thermal_act,
  1263. .ident = "AOpen i915GMm-HFS",
  1264. .matches = {
  1265. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1266. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1267. },
  1268. },
  1269. {
  1270. .callback = thermal_psv,
  1271. .ident = "AOpen i915GMm-HFS",
  1272. .matches = {
  1273. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1274. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1275. },
  1276. },
  1277. {
  1278. .callback = thermal_tzp,
  1279. .ident = "AOpen i915GMm-HFS",
  1280. .matches = {
  1281. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1282. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1283. },
  1284. },
  1285. {
  1286. .callback = thermal_nocrt,
  1287. .ident = "Gigabyte GA-7ZX",
  1288. .matches = {
  1289. DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
  1290. DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
  1291. },
  1292. },
  1293. {}
  1294. };
  1295. static int __init acpi_thermal_init(void)
  1296. {
  1297. int result = 0;
  1298. dmi_check_system(thermal_dmi_table);
  1299. if (off) {
  1300. printk(KERN_NOTICE "ACPI: thermal control disabled\n");
  1301. return -ENODEV;
  1302. }
  1303. acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
  1304. if (!acpi_thermal_dir)
  1305. return -ENODEV;
  1306. result = acpi_bus_register_driver(&acpi_thermal_driver);
  1307. if (result < 0) {
  1308. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1309. return -ENODEV;
  1310. }
  1311. return 0;
  1312. }
  1313. static void __exit acpi_thermal_exit(void)
  1314. {
  1315. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1316. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1317. return;
  1318. }
  1319. module_init(acpi_thermal_init);
  1320. module_exit(acpi_thermal_exit);