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