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