Merge branch 'for-linus' of git://git.o-hand.com/linux-rpurdie-leds
[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/init.h>
37 #include <linux/types.h>
38 #include <linux/proc_fs.h>
39 #include <linux/timer.h>
40 #include <linux/jiffies.h>
41 #include <linux/kmod.h>
42 #include <linux/seq_file.h>
43 #include <linux/reboot.h>
44 #include <asm/uaccess.h>
45
46 #include <acpi/acpi_bus.h>
47 #include <acpi/acpi_drivers.h>
48
49 #define ACPI_THERMAL_COMPONENT          0x04000000
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 KELVIN_TO_CELSIUS(t)    (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
68 #define CELSIUS_TO_KELVIN(t)    ((t+273)*10)
69
70 #define _COMPONENT              ACPI_THERMAL_COMPONENT
71 ACPI_MODULE_NAME("thermal");
72
73 MODULE_AUTHOR("Paul Diefenbaugh");
74 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
75 MODULE_LICENSE("GPL");
76
77 static int tzp;
78 module_param(tzp, int, 0);
79 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
80
81 static int acpi_thermal_add(struct acpi_device *device);
82 static int acpi_thermal_remove(struct acpi_device *device, int type);
83 static int acpi_thermal_resume(struct acpi_device *device);
84 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
85 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
86 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
87 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
88 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
89                                                const char __user *, size_t,
90                                                loff_t *);
91 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
92 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
93                                           size_t, loff_t *);
94
95 static struct acpi_driver acpi_thermal_driver = {
96         .name = "thermal",
97         .class = ACPI_THERMAL_CLASS,
98         .ids = ACPI_THERMAL_HID,
99         .ops = {
100                 .add = acpi_thermal_add,
101                 .remove = acpi_thermal_remove,
102                 .resume = acpi_thermal_resume,
103                 },
104 };
105
106 struct acpi_thermal_state {
107         u8 critical:1;
108         u8 hot:1;
109         u8 passive:1;
110         u8 active:1;
111         u8 reserved:4;
112         int active_index;
113 };
114
115 struct acpi_thermal_state_flags {
116         u8 valid:1;
117         u8 enabled:1;
118         u8 reserved:6;
119 };
120
121 struct acpi_thermal_critical {
122         struct acpi_thermal_state_flags flags;
123         unsigned long temperature;
124 };
125
126 struct acpi_thermal_hot {
127         struct acpi_thermal_state_flags flags;
128         unsigned long temperature;
129 };
130
131 struct acpi_thermal_passive {
132         struct acpi_thermal_state_flags flags;
133         unsigned long temperature;
134         unsigned long tc1;
135         unsigned long tc2;
136         unsigned long tsp;
137         struct acpi_handle_list devices;
138 };
139
140 struct acpi_thermal_active {
141         struct acpi_thermal_state_flags flags;
142         unsigned long temperature;
143         struct acpi_handle_list devices;
144 };
145
146 struct acpi_thermal_trips {
147         struct acpi_thermal_critical critical;
148         struct acpi_thermal_hot hot;
149         struct acpi_thermal_passive passive;
150         struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
151 };
152
153 struct acpi_thermal_flags {
154         u8 cooling_mode:1;      /* _SCP */
155         u8 devices:1;           /* _TZD */
156         u8 reserved:6;
157 };
158
159 struct acpi_thermal {
160         struct acpi_device * device;
161         acpi_bus_id name;
162         unsigned long temperature;
163         unsigned long last_temperature;
164         unsigned long polling_frequency;
165         volatile u8 zombie;
166         struct acpi_thermal_flags flags;
167         struct acpi_thermal_state state;
168         struct acpi_thermal_trips trips;
169         struct acpi_handle_list devices;
170         struct timer_list timer;
171 };
172
173 static const struct file_operations acpi_thermal_state_fops = {
174         .open = acpi_thermal_state_open_fs,
175         .read = seq_read,
176         .llseek = seq_lseek,
177         .release = single_release,
178 };
179
180 static const struct file_operations acpi_thermal_temp_fops = {
181         .open = acpi_thermal_temp_open_fs,
182         .read = seq_read,
183         .llseek = seq_lseek,
184         .release = single_release,
185 };
186
187 static const struct file_operations acpi_thermal_trip_fops = {
188         .open = acpi_thermal_trip_open_fs,
189         .read = seq_read,
190         .llseek = seq_lseek,
191         .release = single_release,
192 };
193
194 static const struct file_operations acpi_thermal_cooling_fops = {
195         .open = acpi_thermal_cooling_open_fs,
196         .read = seq_read,
197         .write = acpi_thermal_write_cooling_mode,
198         .llseek = seq_lseek,
199         .release = single_release,
200 };
201
202 static const struct file_operations acpi_thermal_polling_fops = {
203         .open = acpi_thermal_polling_open_fs,
204         .read = seq_read,
205         .write = acpi_thermal_write_polling,
206         .llseek = seq_lseek,
207         .release = single_release,
208 };
209
210 /* --------------------------------------------------------------------------
211                              Thermal Zone Management
212    -------------------------------------------------------------------------- */
213
214 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
215 {
216         acpi_status status = AE_OK;
217
218
219         if (!tz)
220                 return -EINVAL;
221
222         tz->last_temperature = tz->temperature;
223
224         status =
225             acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
226         if (ACPI_FAILURE(status))
227                 return -ENODEV;
228
229         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
230                           tz->temperature));
231
232         return 0;
233 }
234
235 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
236 {
237         acpi_status status = AE_OK;
238
239
240         if (!tz)
241                 return -EINVAL;
242
243         status =
244             acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
245                                   &tz->polling_frequency);
246         if (ACPI_FAILURE(status))
247                 return -ENODEV;
248
249         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
250                           tz->polling_frequency));
251
252         return 0;
253 }
254
255 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
256 {
257
258         if (!tz)
259                 return -EINVAL;
260
261         tz->polling_frequency = seconds * 10;   /* Convert value to deci-seconds */
262
263         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
264                           "Polling frequency set to %lu seconds\n",
265                           tz->polling_frequency/10));
266
267         return 0;
268 }
269
270 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
271 {
272         acpi_status status = AE_OK;
273         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
274         struct acpi_object_list arg_list = { 1, &arg0 };
275         acpi_handle handle = NULL;
276
277
278         if (!tz)
279                 return -EINVAL;
280
281         status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
282         if (ACPI_FAILURE(status)) {
283                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
284                 return -ENODEV;
285         }
286
287         arg0.integer.value = mode;
288
289         status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
290         if (ACPI_FAILURE(status))
291                 return -ENODEV;
292
293         return 0;
294 }
295
296 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
297 {
298         acpi_status status = AE_OK;
299         int i = 0;
300
301
302         if (!tz)
303                 return -EINVAL;
304
305         /* Critical Shutdown (required) */
306
307         status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
308                                        &tz->trips.critical.temperature);
309         if (ACPI_FAILURE(status)) {
310                 tz->trips.critical.flags.valid = 0;
311                 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
312                 return -ENODEV;
313         } else {
314                 tz->trips.critical.flags.valid = 1;
315                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
316                                   "Found critical threshold [%lu]\n",
317                                   tz->trips.critical.temperature));
318         }
319
320         /* Critical Sleep (optional) */
321
322         status =
323             acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
324                                   &tz->trips.hot.temperature);
325         if (ACPI_FAILURE(status)) {
326                 tz->trips.hot.flags.valid = 0;
327                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
328         } else {
329                 tz->trips.hot.flags.valid = 1;
330                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
331                                   tz->trips.hot.temperature));
332         }
333
334         /* Passive: Processors (optional) */
335
336         status =
337             acpi_evaluate_integer(tz->device->handle, "_PSV", NULL,
338                                   &tz->trips.passive.temperature);
339         if (ACPI_FAILURE(status)) {
340                 tz->trips.passive.flags.valid = 0;
341                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
342         } else {
343                 tz->trips.passive.flags.valid = 1;
344
345                 status =
346                     acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
347                                           &tz->trips.passive.tc1);
348                 if (ACPI_FAILURE(status))
349                         tz->trips.passive.flags.valid = 0;
350
351                 status =
352                     acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
353                                           &tz->trips.passive.tc2);
354                 if (ACPI_FAILURE(status))
355                         tz->trips.passive.flags.valid = 0;
356
357                 status =
358                     acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
359                                           &tz->trips.passive.tsp);
360                 if (ACPI_FAILURE(status))
361                         tz->trips.passive.flags.valid = 0;
362
363                 status =
364                     acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
365                                             &tz->trips.passive.devices);
366                 if (ACPI_FAILURE(status))
367                         tz->trips.passive.flags.valid = 0;
368
369                 if (!tz->trips.passive.flags.valid)
370                         printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
371                 else
372                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
373                                           "Found passive threshold [%lu]\n",
374                                           tz->trips.passive.temperature));
375         }
376
377         /* Active: Fans, etc. (optional) */
378
379         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
380
381                 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
382
383                 status =
384                     acpi_evaluate_integer(tz->device->handle, name, NULL,
385                                           &tz->trips.active[i].temperature);
386                 if (ACPI_FAILURE(status))
387                         break;
388
389                 name[2] = 'L';
390                 status =
391                     acpi_evaluate_reference(tz->device->handle, name, NULL,
392                                             &tz->trips.active[i].devices);
393                 if (ACPI_SUCCESS(status)) {
394                         tz->trips.active[i].flags.valid = 1;
395                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
396                                           "Found active threshold [%d]:[%lu]\n",
397                                           i, tz->trips.active[i].temperature));
398                 } else
399                         ACPI_EXCEPTION((AE_INFO, status,
400                                         "Invalid active threshold [%d]", i));
401         }
402
403         return 0;
404 }
405
406 static int acpi_thermal_get_devices(struct acpi_thermal *tz)
407 {
408         acpi_status status = AE_OK;
409
410
411         if (!tz)
412                 return -EINVAL;
413
414         status =
415             acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
416         if (ACPI_FAILURE(status))
417                 return -ENODEV;
418
419         return 0;
420 }
421
422 static int acpi_thermal_critical(struct acpi_thermal *tz)
423 {
424         if (!tz || !tz->trips.critical.flags.valid)
425                 return -EINVAL;
426
427         if (tz->temperature >= tz->trips.critical.temperature) {
428                 printk(KERN_WARNING PREFIX "Critical trip point\n");
429                 tz->trips.critical.flags.enabled = 1;
430         } else if (tz->trips.critical.flags.enabled)
431                 tz->trips.critical.flags.enabled = 0;
432
433         printk(KERN_EMERG
434                "Critical temperature reached (%ld C), shutting down.\n",
435                KELVIN_TO_CELSIUS(tz->temperature));
436         acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
437                                 tz->trips.critical.flags.enabled);
438
439         orderly_poweroff(true);
440
441         return 0;
442 }
443
444 static int acpi_thermal_hot(struct acpi_thermal *tz)
445 {
446         if (!tz || !tz->trips.hot.flags.valid)
447                 return -EINVAL;
448
449         if (tz->temperature >= tz->trips.hot.temperature) {
450                 printk(KERN_WARNING PREFIX "Hot trip point\n");
451                 tz->trips.hot.flags.enabled = 1;
452         } else if (tz->trips.hot.flags.enabled)
453                 tz->trips.hot.flags.enabled = 0;
454
455         acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
456                                 tz->trips.hot.flags.enabled);
457
458         /* TBD: Call user-mode "sleep(S4)" function */
459
460         return 0;
461 }
462
463 static void acpi_thermal_passive(struct acpi_thermal *tz)
464 {
465         int result = 1;
466         struct acpi_thermal_passive *passive = NULL;
467         int trend = 0;
468         int i = 0;
469
470
471         if (!tz || !tz->trips.passive.flags.valid)
472                 return;
473
474         passive = &(tz->trips.passive);
475
476         /*
477          * Above Trip?
478          * -----------
479          * Calculate the thermal trend (using the passive cooling equation)
480          * and modify the performance limit for all passive cooling devices
481          * accordingly.  Note that we assume symmetry.
482          */
483         if (tz->temperature >= passive->temperature) {
484                 trend =
485                     (passive->tc1 * (tz->temperature - tz->last_temperature)) +
486                     (passive->tc2 * (tz->temperature - passive->temperature));
487                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
488                                   "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
489                                   trend, passive->tc1, tz->temperature,
490                                   tz->last_temperature, passive->tc2,
491                                   tz->temperature, passive->temperature));
492                 passive->flags.enabled = 1;
493                 /* Heating up? */
494                 if (trend > 0)
495                         for (i = 0; i < passive->devices.count; i++)
496                                 acpi_processor_set_thermal_limit(passive->
497                                                                  devices.
498                                                                  handles[i],
499                                                                  ACPI_PROCESSOR_LIMIT_INCREMENT);
500                 /* Cooling off? */
501                 else if (trend < 0) {
502                         for (i = 0; i < passive->devices.count; i++)
503                                 /*
504                                  * assume that we are on highest
505                                  * freq/lowest thrott and can leave
506                                  * passive mode, even in error case
507                                  */
508                                 if (!acpi_processor_set_thermal_limit
509                                     (passive->devices.handles[i],
510                                      ACPI_PROCESSOR_LIMIT_DECREMENT))
511                                         result = 0;
512                         /*
513                          * Leave cooling mode, even if the temp might
514                          * higher than trip point This is because some
515                          * machines might have long thermal polling
516                          * frequencies (tsp) defined. We will fall back
517                          * into passive mode in next cycle (probably quicker)
518                          */
519                         if (result) {
520                                 passive->flags.enabled = 0;
521                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
522                                                   "Disabling passive cooling, still above threshold,"
523                                                   " but we are cooling down\n"));
524                         }
525                 }
526                 return;
527         }
528
529         /*
530          * Below Trip?
531          * -----------
532          * Implement passive cooling hysteresis to slowly increase performance
533          * and avoid thrashing around the passive trip point.  Note that we
534          * assume symmetry.
535          */
536         if (!passive->flags.enabled)
537                 return;
538         for (i = 0; i < passive->devices.count; i++)
539                 if (!acpi_processor_set_thermal_limit
540                     (passive->devices.handles[i],
541                      ACPI_PROCESSOR_LIMIT_DECREMENT))
542                         result = 0;
543         if (result) {
544                 passive->flags.enabled = 0;
545                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
546                                   "Disabling passive cooling (zone is cool)\n"));
547         }
548 }
549
550 static void acpi_thermal_active(struct acpi_thermal *tz)
551 {
552         int result = 0;
553         struct acpi_thermal_active *active = NULL;
554         int i = 0;
555         int j = 0;
556         unsigned long maxtemp = 0;
557
558
559         if (!tz)
560                 return;
561
562         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
563                 active = &(tz->trips.active[i]);
564                 if (!active || !active->flags.valid)
565                         break;
566                 if (tz->temperature >= active->temperature) {
567                         /*
568                          * Above Threshold?
569                          * ----------------
570                          * If not already enabled, turn ON all cooling devices
571                          * associated with this active threshold.
572                          */
573                         if (active->temperature > maxtemp)
574                                 tz->state.active_index = i;
575                         maxtemp = active->temperature;
576                         if (active->flags.enabled)
577                                 continue;
578                         for (j = 0; j < active->devices.count; j++) {
579                                 result =
580                                     acpi_bus_set_power(active->devices.
581                                                        handles[j],
582                                                        ACPI_STATE_D0);
583                                 if (result) {
584                                         printk(KERN_WARNING PREFIX
585                                                       "Unable to turn cooling device [%p] 'on'\n",
586                                                       active->devices.
587                                                       handles[j]);
588                                         continue;
589                                 }
590                                 active->flags.enabled = 1;
591                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
592                                                   "Cooling device [%p] now 'on'\n",
593                                                   active->devices.handles[j]));
594                         }
595                         continue;
596                 }
597                 if (!active->flags.enabled)
598                         continue;
599                 /*
600                  * Below Threshold?
601                  * ----------------
602                  * Turn OFF all cooling devices associated with this
603                  * threshold.
604                  */
605                 for (j = 0; j < active->devices.count; j++) {
606                         result = acpi_bus_set_power(active->devices.handles[j],
607                                                     ACPI_STATE_D3);
608                         if (result) {
609                                 printk(KERN_WARNING PREFIX
610                                               "Unable to turn cooling device [%p] 'off'\n",
611                                               active->devices.handles[j]);
612                                 continue;
613                         }
614                         active->flags.enabled = 0;
615                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
616                                           "Cooling device [%p] now 'off'\n",
617                                           active->devices.handles[j]));
618                 }
619         }
620 }
621
622 static void acpi_thermal_check(void *context);
623
624 static void acpi_thermal_run(unsigned long data)
625 {
626         struct acpi_thermal *tz = (struct acpi_thermal *)data;
627         if (!tz->zombie)
628                 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
629 }
630
631 static void acpi_thermal_check(void *data)
632 {
633         int result = 0;
634         struct acpi_thermal *tz = data;
635         unsigned long sleep_time = 0;
636         int i = 0;
637         struct acpi_thermal_state state;
638
639
640         if (!tz) {
641                 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
642                 return;
643         }
644
645         state = tz->state;
646
647         result = acpi_thermal_get_temperature(tz);
648         if (result)
649                 return;
650
651         memset(&tz->state, 0, sizeof(tz->state));
652
653         /*
654          * Check Trip Points
655          * -----------------
656          * Compare the current temperature to the trip point values to see
657          * if we've entered one of the thermal policy states.  Note that
658          * this function determines when a state is entered, but the 
659          * individual policy decides when it is exited (e.g. hysteresis).
660          */
661         if (tz->trips.critical.flags.valid)
662                 state.critical |=
663                     (tz->temperature >= tz->trips.critical.temperature);
664         if (tz->trips.hot.flags.valid)
665                 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
666         if (tz->trips.passive.flags.valid)
667                 state.passive |=
668                     (tz->temperature >= tz->trips.passive.temperature);
669         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
670                 if (tz->trips.active[i].flags.valid)
671                         state.active |=
672                             (tz->temperature >=
673                              tz->trips.active[i].temperature);
674
675         /*
676          * Invoke Policy
677          * -------------
678          * Separated from the above check to allow individual policy to 
679          * determine when to exit a given state.
680          */
681         if (state.critical)
682                 acpi_thermal_critical(tz);
683         if (state.hot)
684                 acpi_thermal_hot(tz);
685         if (state.passive)
686                 acpi_thermal_passive(tz);
687         if (state.active)
688                 acpi_thermal_active(tz);
689
690         /*
691          * Calculate State
692          * ---------------
693          * Again, separated from the above two to allow independent policy
694          * decisions.
695          */
696         tz->state.critical = tz->trips.critical.flags.enabled;
697         tz->state.hot = tz->trips.hot.flags.enabled;
698         tz->state.passive = tz->trips.passive.flags.enabled;
699         tz->state.active = 0;
700         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
701                 tz->state.active |= tz->trips.active[i].flags.enabled;
702
703         /*
704          * Calculate Sleep Time
705          * --------------------
706          * If we're in the passive state, use _TSP's value.  Otherwise
707          * use the default polling frequency (e.g. _TZP).  If no polling
708          * frequency is specified then we'll wait forever (at least until
709          * a thermal event occurs).  Note that _TSP and _TZD values are
710          * given in 1/10th seconds (we must covert to milliseconds).
711          */
712         if (tz->state.passive)
713                 sleep_time = tz->trips.passive.tsp * 100;
714         else if (tz->polling_frequency > 0)
715                 sleep_time = tz->polling_frequency * 100;
716
717         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
718                           tz->name, tz->temperature, sleep_time));
719
720         /*
721          * Schedule Next Poll
722          * ------------------
723          */
724         if (!sleep_time) {
725                 if (timer_pending(&(tz->timer)))
726                         del_timer(&(tz->timer));
727         } else {
728                 if (timer_pending(&(tz->timer)))
729                         mod_timer(&(tz->timer),
730                                         jiffies + (HZ * sleep_time) / 1000);
731                 else {
732                         tz->timer.data = (unsigned long)tz;
733                         tz->timer.function = acpi_thermal_run;
734                         tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
735                         add_timer(&(tz->timer));
736                 }
737         }
738
739         return;
740 }
741
742 /* --------------------------------------------------------------------------
743                               FS Interface (/proc)
744    -------------------------------------------------------------------------- */
745
746 static struct proc_dir_entry *acpi_thermal_dir;
747
748 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
749 {
750         struct acpi_thermal *tz = seq->private;
751
752
753         if (!tz)
754                 goto end;
755
756         seq_puts(seq, "state:                   ");
757
758         if (!tz->state.critical && !tz->state.hot && !tz->state.passive
759             && !tz->state.active)
760                 seq_puts(seq, "ok\n");
761         else {
762                 if (tz->state.critical)
763                         seq_puts(seq, "critical ");
764                 if (tz->state.hot)
765                         seq_puts(seq, "hot ");
766                 if (tz->state.passive)
767                         seq_puts(seq, "passive ");
768                 if (tz->state.active)
769                         seq_printf(seq, "active[%d]", tz->state.active_index);
770                 seq_puts(seq, "\n");
771         }
772
773       end:
774         return 0;
775 }
776
777 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
778 {
779         return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
780 }
781
782 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
783 {
784         int result = 0;
785         struct acpi_thermal *tz = seq->private;
786
787
788         if (!tz)
789                 goto end;
790
791         result = acpi_thermal_get_temperature(tz);
792         if (result)
793                 goto end;
794
795         seq_printf(seq, "temperature:             %ld C\n",
796                    KELVIN_TO_CELSIUS(tz->temperature));
797
798       end:
799         return 0;
800 }
801
802 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
803 {
804         return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
805 }
806
807 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
808 {
809         struct acpi_thermal *tz = seq->private;
810         struct acpi_device *device;
811         acpi_status status;
812
813         int i = 0;
814         int j = 0;
815
816
817         if (!tz)
818                 goto end;
819
820         if (tz->trips.critical.flags.valid)
821                 seq_printf(seq, "critical (S5):           %ld C\n",
822                            KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
823
824         if (tz->trips.hot.flags.valid)
825                 seq_printf(seq, "hot (S4):                %ld C\n",
826                            KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
827
828         if (tz->trips.passive.flags.valid) {
829                 seq_printf(seq,
830                            "passive:                 %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
831                            KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
832                            tz->trips.passive.tc1, tz->trips.passive.tc2,
833                            tz->trips.passive.tsp);
834                 for (j = 0; j < tz->trips.passive.devices.count; j++) {
835                         status = acpi_bus_get_device(tz->trips.passive.devices.
836                                                      handles[j], &device);
837                         seq_printf(seq, "%4.4s ", status ? "" :
838                                    acpi_device_bid(device));
839                 }
840                 seq_puts(seq, "\n");
841         }
842
843         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
844                 if (!(tz->trips.active[i].flags.valid))
845                         break;
846                 seq_printf(seq, "active[%d]:               %ld C: devices=",
847                            i,
848                            KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
849                 for (j = 0; j < tz->trips.active[i].devices.count; j++){
850                         status = acpi_bus_get_device(tz->trips.active[i].
851                                                      devices.handles[j],
852                                                      &device);
853                         seq_printf(seq, "%4.4s ", status ? "" :
854                                    acpi_device_bid(device));
855                 }
856                 seq_puts(seq, "\n");
857         }
858
859       end:
860         return 0;
861 }
862
863 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
864 {
865         return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
866 }
867
868 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
869 {
870         struct acpi_thermal *tz = seq->private;
871
872
873         if (!tz)
874                 goto end;
875
876         if (!tz->flags.cooling_mode)
877                 seq_puts(seq, "<setting not supported>\n");
878         else
879                 seq_puts(seq, "0 - Active; 1 - Passive\n");
880
881       end:
882         return 0;
883 }
884
885 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
886 {
887         return single_open(file, acpi_thermal_cooling_seq_show,
888                            PDE(inode)->data);
889 }
890
891 static ssize_t
892 acpi_thermal_write_cooling_mode(struct file *file,
893                                 const char __user * buffer,
894                                 size_t count, loff_t * ppos)
895 {
896         struct seq_file *m = file->private_data;
897         struct acpi_thermal *tz = m->private;
898         int result = 0;
899         char mode_string[12] = { '\0' };
900
901
902         if (!tz || (count > sizeof(mode_string) - 1))
903                 return -EINVAL;
904
905         if (!tz->flags.cooling_mode)
906                 return -ENODEV;
907
908         if (copy_from_user(mode_string, buffer, count))
909                 return -EFAULT;
910
911         mode_string[count] = '\0';
912
913         result = acpi_thermal_set_cooling_mode(tz,
914                                                simple_strtoul(mode_string, NULL,
915                                                               0));
916         if (result)
917                 return result;
918
919         acpi_thermal_check(tz);
920
921         return count;
922 }
923
924 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
925 {
926         struct acpi_thermal *tz = seq->private;
927
928
929         if (!tz)
930                 goto end;
931
932         if (!tz->polling_frequency) {
933                 seq_puts(seq, "<polling disabled>\n");
934                 goto end;
935         }
936
937         seq_printf(seq, "polling frequency:       %lu seconds\n",
938                    (tz->polling_frequency / 10));
939
940       end:
941         return 0;
942 }
943
944 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
945 {
946         return single_open(file, acpi_thermal_polling_seq_show,
947                            PDE(inode)->data);
948 }
949
950 static ssize_t
951 acpi_thermal_write_polling(struct file *file,
952                            const char __user * buffer,
953                            size_t count, loff_t * ppos)
954 {
955         struct seq_file *m = file->private_data;
956         struct acpi_thermal *tz = m->private;
957         int result = 0;
958         char polling_string[12] = { '\0' };
959         int seconds = 0;
960
961
962         if (!tz || (count > sizeof(polling_string) - 1))
963                 return -EINVAL;
964
965         if (copy_from_user(polling_string, buffer, count))
966                 return -EFAULT;
967
968         polling_string[count] = '\0';
969
970         seconds = simple_strtoul(polling_string, NULL, 0);
971
972         result = acpi_thermal_set_polling(tz, seconds);
973         if (result)
974                 return result;
975
976         acpi_thermal_check(tz);
977
978         return count;
979 }
980
981 static int acpi_thermal_add_fs(struct acpi_device *device)
982 {
983         struct proc_dir_entry *entry = NULL;
984
985
986         if (!acpi_device_dir(device)) {
987                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
988                                                      acpi_thermal_dir);
989                 if (!acpi_device_dir(device))
990                         return -ENODEV;
991                 acpi_device_dir(device)->owner = THIS_MODULE;
992         }
993
994         /* 'state' [R] */
995         entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
996                                   S_IRUGO, acpi_device_dir(device));
997         if (!entry)
998                 return -ENODEV;
999         else {
1000                 entry->proc_fops = &acpi_thermal_state_fops;
1001                 entry->data = acpi_driver_data(device);
1002                 entry->owner = THIS_MODULE;
1003         }
1004
1005         /* 'temperature' [R] */
1006         entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1007                                   S_IRUGO, acpi_device_dir(device));
1008         if (!entry)
1009                 return -ENODEV;
1010         else {
1011                 entry->proc_fops = &acpi_thermal_temp_fops;
1012                 entry->data = acpi_driver_data(device);
1013                 entry->owner = THIS_MODULE;
1014         }
1015
1016         /* 'trip_points' [R/W] */
1017         entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1018                                   S_IFREG | S_IRUGO | S_IWUSR,
1019                                   acpi_device_dir(device));
1020         if (!entry)
1021                 return -ENODEV;
1022         else {
1023                 entry->proc_fops = &acpi_thermal_trip_fops;
1024                 entry->data = acpi_driver_data(device);
1025                 entry->owner = THIS_MODULE;
1026         }
1027
1028         /* 'cooling_mode' [R/W] */
1029         entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1030                                   S_IFREG | S_IRUGO | S_IWUSR,
1031                                   acpi_device_dir(device));
1032         if (!entry)
1033                 return -ENODEV;
1034         else {
1035                 entry->proc_fops = &acpi_thermal_cooling_fops;
1036                 entry->data = acpi_driver_data(device);
1037                 entry->owner = THIS_MODULE;
1038         }
1039
1040         /* 'polling_frequency' [R/W] */
1041         entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1042                                   S_IFREG | S_IRUGO | S_IWUSR,
1043                                   acpi_device_dir(device));
1044         if (!entry)
1045                 return -ENODEV;
1046         else {
1047                 entry->proc_fops = &acpi_thermal_polling_fops;
1048                 entry->data = acpi_driver_data(device);
1049                 entry->owner = THIS_MODULE;
1050         }
1051
1052         return 0;
1053 }
1054
1055 static int acpi_thermal_remove_fs(struct acpi_device *device)
1056 {
1057
1058         if (acpi_device_dir(device)) {
1059                 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1060                                   acpi_device_dir(device));
1061                 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1062                                   acpi_device_dir(device));
1063                 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1064                                   acpi_device_dir(device));
1065                 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1066                                   acpi_device_dir(device));
1067                 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1068                                   acpi_device_dir(device));
1069                 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1070                 acpi_device_dir(device) = NULL;
1071         }
1072
1073         return 0;
1074 }
1075
1076 /* --------------------------------------------------------------------------
1077                                  Driver Interface
1078    -------------------------------------------------------------------------- */
1079
1080 static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
1081 {
1082         struct acpi_thermal *tz = data;
1083         struct acpi_device *device = NULL;
1084
1085
1086         if (!tz)
1087                 return;
1088
1089         device = tz->device;
1090
1091         switch (event) {
1092         case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1093                 acpi_thermal_check(tz);
1094                 break;
1095         case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1096                 acpi_thermal_get_trip_points(tz);
1097                 acpi_thermal_check(tz);
1098                 acpi_bus_generate_event(device, event, 0);
1099                 break;
1100         case ACPI_THERMAL_NOTIFY_DEVICES:
1101                 if (tz->flags.devices)
1102                         acpi_thermal_get_devices(tz);
1103                 acpi_bus_generate_event(device, event, 0);
1104                 break;
1105         default:
1106                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1107                                   "Unsupported event [0x%x]\n", event));
1108                 break;
1109         }
1110
1111         return;
1112 }
1113
1114 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1115 {
1116         int result = 0;
1117
1118
1119         if (!tz)
1120                 return -EINVAL;
1121
1122         /* Get temperature [_TMP] (required) */
1123         result = acpi_thermal_get_temperature(tz);
1124         if (result)
1125                 return result;
1126
1127         /* Get trip points [_CRT, _PSV, etc.] (required) */
1128         result = acpi_thermal_get_trip_points(tz);
1129         if (result)
1130                 return result;
1131
1132         /* Set the cooling mode [_SCP] to active cooling (default) */
1133         result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1134         if (!result)
1135                 tz->flags.cooling_mode = 1;
1136
1137         /* Get default polling frequency [_TZP] (optional) */
1138         if (tzp)
1139                 tz->polling_frequency = tzp;
1140         else
1141                 acpi_thermal_get_polling_frequency(tz);
1142
1143         /* Get devices in this thermal zone [_TZD] (optional) */
1144         result = acpi_thermal_get_devices(tz);
1145         if (!result)
1146                 tz->flags.devices = 1;
1147
1148         return 0;
1149 }
1150
1151 static int acpi_thermal_add(struct acpi_device *device)
1152 {
1153         int result = 0;
1154         acpi_status status = AE_OK;
1155         struct acpi_thermal *tz = NULL;
1156
1157
1158         if (!device)
1159                 return -EINVAL;
1160
1161         tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1162         if (!tz)
1163                 return -ENOMEM;
1164
1165         tz->device = device;
1166         strcpy(tz->name, device->pnp.bus_id);
1167         strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1168         strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1169         acpi_driver_data(device) = tz;
1170
1171         result = acpi_thermal_get_info(tz);
1172         if (result)
1173                 goto end;
1174
1175         result = acpi_thermal_add_fs(device);
1176         if (result)
1177                 goto end;
1178
1179         init_timer(&tz->timer);
1180
1181         acpi_thermal_check(tz);
1182
1183         status = acpi_install_notify_handler(device->handle,
1184                                              ACPI_DEVICE_NOTIFY,
1185                                              acpi_thermal_notify, tz);
1186         if (ACPI_FAILURE(status)) {
1187                 result = -ENODEV;
1188                 goto end;
1189         }
1190
1191         printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1192                acpi_device_name(device), acpi_device_bid(device),
1193                KELVIN_TO_CELSIUS(tz->temperature));
1194
1195       end:
1196         if (result) {
1197                 acpi_thermal_remove_fs(device);
1198                 kfree(tz);
1199         }
1200
1201         return result;
1202 }
1203
1204 static int acpi_thermal_remove(struct acpi_device *device, int type)
1205 {
1206         acpi_status status = AE_OK;
1207         struct acpi_thermal *tz = NULL;
1208
1209
1210         if (!device || !acpi_driver_data(device))
1211                 return -EINVAL;
1212
1213         tz = acpi_driver_data(device);
1214
1215         /* avoid timer adding new defer task */
1216         tz->zombie = 1;
1217         /* wait for running timer (on other CPUs) finish */
1218         del_timer_sync(&(tz->timer));
1219         /* synchronize deferred task */
1220         acpi_os_wait_events_complete(NULL);
1221         /* deferred task may reinsert timer */
1222         del_timer_sync(&(tz->timer));
1223
1224         status = acpi_remove_notify_handler(device->handle,
1225                                             ACPI_DEVICE_NOTIFY,
1226                                             acpi_thermal_notify);
1227
1228         /* Terminate policy */
1229         if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
1230                 tz->trips.passive.flags.enabled = 0;
1231                 acpi_thermal_passive(tz);
1232         }
1233         if (tz->trips.active[0].flags.valid
1234             && tz->trips.active[0].flags.enabled) {
1235                 tz->trips.active[0].flags.enabled = 0;
1236                 acpi_thermal_active(tz);
1237         }
1238
1239         acpi_thermal_remove_fs(device);
1240
1241         kfree(tz);
1242         return 0;
1243 }
1244
1245 static int acpi_thermal_resume(struct acpi_device *device)
1246 {
1247         struct acpi_thermal *tz = NULL;
1248         int i, j, power_state, result;
1249
1250
1251         if (!device || !acpi_driver_data(device))
1252                 return -EINVAL;
1253
1254         tz = acpi_driver_data(device);
1255
1256         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1257                 if (!(&tz->trips.active[i]))
1258                         break;
1259                 if (!tz->trips.active[i].flags.valid)
1260                         break;
1261                 tz->trips.active[i].flags.enabled = 1;
1262                 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1263                         result = acpi_bus_get_power(tz->trips.active[i].devices.
1264                             handles[j], &power_state);
1265                         if (result || (power_state != ACPI_STATE_D0)) {
1266                                 tz->trips.active[i].flags.enabled = 0;
1267                                 break;
1268                         }
1269                 }
1270                 tz->state.active |= tz->trips.active[i].flags.enabled;
1271         }
1272
1273         acpi_thermal_check(tz);
1274
1275         return AE_OK;
1276 }
1277
1278 static int __init acpi_thermal_init(void)
1279 {
1280         int result = 0;
1281
1282
1283         acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1284         if (!acpi_thermal_dir)
1285                 return -ENODEV;
1286         acpi_thermal_dir->owner = THIS_MODULE;
1287
1288         result = acpi_bus_register_driver(&acpi_thermal_driver);
1289         if (result < 0) {
1290                 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1291                 return -ENODEV;
1292         }
1293
1294         return 0;
1295 }
1296
1297 static void __exit acpi_thermal_exit(void)
1298 {
1299
1300         acpi_bus_unregister_driver(&acpi_thermal_driver);
1301
1302         remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1303
1304         return;
1305 }
1306
1307 module_init(acpi_thermal_init);
1308 module_exit(acpi_thermal_exit);