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